在Corda中,响应者如何将组件添加到事务中?

时间:2018-11-16 13:21:42

标签: corda

我有一个Corda流,其中响应者Party需要向交易中添加组件(例如,响应者可能必须向交易中添加自己的现金以支付交易中的资产)。 / p>

如何允许响应者添加这些输入?

1 个答案:

答案 0 :(得分:1)

下面是如何完成此操作的示例。总结:

  • 发起方使用send向响应方发送要添加状态的参数
  • 响应者使用receive来接收这些参数并创建状态
  • 响应者使用send将创建的状态发送回发起者
  • 发起者将状态添加到事务构建器,并像往常一样继续进行流程
  • 当要求响应者作为CollectSignaturesFlow的一部分签署交易时,他们会检查他们提供的状态和交易通常是否符合预期

启动器代码

@InitiatingFlow
@StartableByRPC
public class IOUFlowInitiator extends FlowLogic<SignedTransaction> {
    private final int iouValue;
    private final Party otherParty;

    private final ProgressTracker progressTracker = new ProgressTracker();

    public IOUFlowInitiator(int iouValue, Party otherParty) {
        this.iouValue = iouValue;
        this.otherParty = otherParty;
    }

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

    @Suspendable
    @Override
    public SignedTransaction call() throws FlowException {
        // Get the counterparty to create the output IOUState.
        final FlowSession otherPartySession = initiateFlow(otherParty);
        final IOUState iouState = otherPartySession.sendAndReceive(IOUState.class, iouValue).unwrap(wrappedIOU -> wrappedIOU);

        // Create the command.
        final List<PublicKey> requiredSigners = Arrays.asList(iouState.getLender().getOwningKey(), iouState.getBorrower().getOwningKey());
        final Command<IOUContract.Commands.Create> txCommand = new Command<>(
                new IOUContract.Commands.Create(), requiredSigners);

        // Build the transaction.
        final Party notary = getServiceHub().getNetworkMapCache().getNotaryIdentities().get(0);
        final TransactionBuilder txBuilder = new TransactionBuilder(notary)
                .addOutputState(iouState, IOU_CONTRACT_ID)
                .addCommand(txCommand);

        // Verify, sign and finalise.
        txBuilder.verify(getServiceHub());
        final SignedTransaction partSignedTx = getServiceHub().signInitialTransaction(txBuilder);
        final SignedTransaction fullySignedTx = subFlow(new CollectSignaturesFlow(partSignedTx, ImmutableSet.of(otherPartySession), CollectSignaturesFlow.Companion.tracker()));
        return subFlow(new FinalityFlow(fullySignedTx));
    }
}

响应者代码

@InitiatedBy(IOUFlowInitiator.class)
public class IOUFlowResponder extends FlowLogic<SignedTransaction> {
    private final FlowSession counterpartySession;

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

    @Suspendable
    @Override
    public SignedTransaction call() throws FlowException {
        // Receive the IOU's value from the counterparty.
        Integer iouValue = counterpartySession.receive(Integer.class).unwrap(wrappedInt -> wrappedInt);

        // Create the output IOUState.
        Party otherParty = counterpartySession.getCounterparty();
        IOUState iouState = new IOUState(iouValue, getOurIdentity(), otherParty, new UniqueIdentifier());

        // Send the output IOUState to the counterparty.
        counterpartySession.send(iouState);

        // Sign the resulting transaction if it meets expectations.
        return subFlow(new CheckIOUAndSignFlow(iouState, counterpartySession, SignTransactionFlow.Companion.tracker()));
    }
}

class CheckIOUAndSignFlow extends SignTransactionFlow {
    private final IOUState expectedIOU;

    CheckIOUAndSignFlow(IOUState expectedIOU, FlowSession otherPartyFlow, ProgressTracker progressTracker) {
        super(otherPartyFlow, progressTracker);
        this.expectedIOU = expectedIOU;
    }

    @Override
    protected void checkTransaction(SignedTransaction stx) throws FlowException {
        LedgerTransaction ltx = null;
        try {
            ltx = stx.toLedgerTransaction(getServiceHub(), false);
        } catch (SignatureException e) {
            throw new FlowException("Transaction had invalid signature.");
        }

        // Check that the resulting transaction meets expectations.
        List<IOUState> outputs = ltx.outputsOfType(IOUState.class);
        List<Command<IOUContract.Commands.Create>> commands = ltx.commandsOfType(IOUContract.Commands.Create.class);

        if (outputs.size() != 1) throw new FlowException("Expected single IOU output in the transaction.");
        if (commands.size() != 1) throw new FlowException("Expected single IOU Create command in the transaction.");

        IOUState outputState = outputs.get(0);

        if (!outputState.equals(expectedIOU)) throw new FlowException("IOU does not match expected IOU..");
    }
}