В Корде, как одна сторона может поделиться с другой существующим государством в своем хранилище? - PullRequest
0 голосов
/ 16 ноября 2018

Предположим, есть два узла, Алиса и Боб. У Алисы есть состояние, о котором она хочет, чтобы Боб знал. Как Алиса может отправить состояние Бобу и заставить его сохранить его в своем хранилище?

1 Ответ

0 голосов
/ 16 ноября 2018

Вам понадобится как инициатор, так и поток ответчика:

  • Инициатор извлекает состояние из своего хранилища, извлекает транзакцию, которая создала это состояние, и отправляет транзакцию контрагенту для записи
  • Ответчик запишет транзакцию, сохранив все ее состояния

Поток инициатора

@InitiatingFlow
@StartableByRPC
public class Initiator extends FlowLogic<Void> {
    private final UUID stateId;
    private final Party otherParty;

    private final ProgressTracker progressTracker = new ProgressTracker();

    public Initiator(UUID stateId, Party otherParty) {
        this.stateId = stateId;
        this.otherParty = otherParty;
    }

    @Override
    public ProgressTracker getProgressTracker() {
        return progressTracker;
    }

    @Suspendable
    @Override
    public Void call() throws FlowException {
        // Find the correct state.
        LinearStateQueryCriteria criteria = new LinearStateQueryCriteria(null, Collections.singletonList(stateId));
        Vault.Page<IOUState> queryResults = getServiceHub().getVaultService().queryBy(IOUState.class, criteria);
        if (queryResults.getStates().size() != 1)
            throw new IllegalStateException("Not exactly one match for the provided ID.");
        StateAndRef<IOUState> stateAndRef = queryResults.getStates().get(0);

        // Find the transaction that created this state.
        SecureHash creatingTransactionHash = stateAndRef.getRef().getTxhash();
        SignedTransaction creatingTransaction = getServiceHub().getValidatedTransactions().getTransaction(creatingTransactionHash);

        // Send the transaction to the counterparty.
        final FlowSession counterpartySession = initiateFlow(otherParty);
        subFlow(new SendTransactionFlow(counterpartySession, creatingTransaction));

        return null;
    }
}

Поток ответчика

@InitiatedBy(Initiator.class)
public class Responder extends FlowLogic<Void> {
    private final FlowSession counterpartySession;

    public Responder(FlowSession counterpartySession) {
        this.counterpartySession = counterpartySession;
    }

    @Suspendable
    @Override
    public Void call() throws FlowException {
        // Receive the transaction and store all its states.
        // If we don't pass `ALL_VISIBLE`, only the states for which the node is one of the `participants` will be stored.
        subFlow(new ReceiveTransactionFlow(counterpartySession, true, StatesToRecord.ALL_VISIBLE));

        return null;
    }
}
...