在科达,一方如何与另一方共享其金库中的现有状态



假设有两个节点,Alice和Bob。爱丽丝有一种状态,她想让鲍勃知道。爱丽丝怎么能把状态发给鲍勃,让他把它保存在他的保险库里?

您将需要一个启动器和一个响应程序流:

  • 启动器将从其保险库中检索状态,检索创建此状态的交易,并将交易发送给要记录的交易对手
  • 响应程序将记录事务,存储其所有状态

启动器流

@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;
}
}

相关内容

最新更新