我有一个Corda流,其中响应者Party
需要向交易中添加组件(例如,响应者可能必须向交易中添加自己的现金以支付交易中的资产)。 / p>
如何允许响应者添加这些输入?
答案 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..");
}
}