如何在同一流程中多次获得同一交易对手的签名?

时间:2018-11-26 12:45:58

标签: corda

我需要实现类似CentralParty,PartyA,PartyB的方案。 CentralParty需要启动一个流程,该流程中将有3个交易循环,其中发起方始终是中央方,而另一方将是A方2次。

即我正在尝试通过引用以下链接从同一交易方多次从同一交易对手收集签名

Corda returning Multiple transactions in a single flow call ()

val flowSessionMap = mutableMapOf<Party, FlowSession>()
var ftx:MutableList<SignedTransaction> = mutableListOf<SignedTransaction>()
var signedTransaction:SignedTransaction

val fullySignedTransactions = matchingStateList.forEach { matchingState ->

    val txCommand = Command(IOUContract.Commands.Matching(), matchingState.participants.map { it.owningKey })
    val txBuilder = TransactionBuilder(notary)
            .addOutputState(matchingState, IOU_CONTRACT_ID)
            .addCommand(txCommand)

    // Stage 2.
    progressTracker.currentStep = VERIFYING_TRANSACTION
    // Verify that the transaction is valid.
    txBuilder.verify(serviceHub)

    // Stage 3.
    progressTracker.currentStep = SIGNING_TRANSACTION
    // Sign the transaction.
    val partSignedTx = serviceHub.signInitialTransaction(txBuilder, ourIdentity.owningKey)


    val sessions = (listOf(PartyA).map { signer ->
        flowSessionMap.getOrPut(signer) {
            initiateFlow(signer)
        }
    })

    val fullySignedTransaction = subFlow(CollectSignaturesInitiatingFlow(
        partSignedTx, sessions)
    )

    signedTransaction = fullySignedTransaction

    ftx.add(signedTransaction)
}

for (transaction in ftx) {
    subFlow(FinalityFlow(transaction))
}

响应者流定义如下:

 class Acceptor(val otherPartyFlow: FlowSession) : FlowLogic<SignedTransaction>() {
    @Suspendable
    override fun call(): SignedTransaction {
        val signTransactionFlow = object : SignTransactionFlow(otherPartyFlow) {
            override fun checkTransaction(stx: SignedTransaction) = requireThat {
                val output = stx.tx.outputs.single().data
                "This must be an Matching State transaction." using (output is MatchingState)
                val Match = output as MatchingState
                "I won't accept IOUs with a value over 100." using (iou.value <= 100)
            }
        }

        return subFlow(signTransactionFlow)
    }
}

其中CollectSignaturesInitiatingFlow的定义如下:

 @InitiatingFlow
class CollectSignaturesInitiatingFlow(val signedTransaction: SignedTransaction, val sessions: List<FlowSession>): FlowLogic<SignedTransaction>() {
    override fun call(): SignedTransaction {
        return subFlow(CollectSignaturesFlow(signedTransaction, sessions))
    }
}

CollectSignaturesInitiatingFlow的响应者定义如下:

    @InitiatedBy(CollectSignaturesInitiatingFlow::class)
class CollectSignaturesInitiatingFlowResponder(val otherPartyFlow: FlowSession) : FlowLogic<SignedTransaction>() {
    @Suspendable
    override fun call(): SignedTransaction {
        val signTransactionFlow = object : SignTransactionFlow(otherPartyFlow) {
            override fun checkTransaction(stx: SignedTransaction) {
                TODO("Check the transaction here.")
            }
        }

        return subFlow(signTransactionFlow)
    }
}

在运行相同的代码时,我遇到了类似的错误

[WARN ] 15:43:18,817 [Node thread-1] (FlowStateMachineImpl.kt:111) flow.[82900238-9223-4ace-ba78-9ecc45121b11].run - Terminated by unexpected exception {}
net.corda.core.flows.UnexpectedFlowEndException: Counterparty flow on O=PartyA, L=London, C=GB has completed without sending data
    at net.corda.node.services.statemachine.FlowStateMachineImpl.confirmNoError(FlowStateMachineImpl.kt:488) ~[corda-node-3.2-corda.jar:?]
    at net.corda.node.services.statemachine.FlowStateMachineImpl.waitForMessage(FlowStateMachineImpl.kt:444) ~[corda-node-3.2-corda.jar:?]
    at net.corda.node.services.statemachine.FlowStateMachineImpl.sendAndReceiveInternal(FlowStateMachineImpl.kt:385) ~[corda-node-3.2-corda.jar:?]
    at net.corda.node.services.statemachine.FlowStateMachineImpl.sendAndReceive(FlowStateMachineImpl.kt:203) ~[corda-node-3.2-corda.jar:?]
    at net.corda.node.services.statemachine.FlowSessionImpl.sendAndReceive(FlowSessionImpl.kt:29) ~[corda-node-3.2-corda.jar:?]
    at net.corda.node.services.statemachine.FlowSessionImpl.sendAndReceive(FlowSessionImpl.kt:40) ~[corda-node-3.2-corda.jar:?]
    at net.corda.core.flows.DataVendingFlow.sendPayloadAndReceiveDataRequest(SendTransactionFlow.kt:70) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.DataVendingFlow.call(SendTransactionFlow.kt:48) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.DataVendingFlow.call(SendTransactionFlow.kt:31) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.FlowLogic.subFlow(FlowLogic.kt:290) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.CollectSignatureFlow.call(CollectSignaturesFlow.kt:142) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.CollectSignatureFlow.call(CollectSignaturesFlow.kt:135) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.FlowLogic.subFlow(FlowLogic.kt:290) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.CollectSignaturesFlow.call(CollectSignaturesFlow.kt:114) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.CollectSignaturesFlow.call(CollectSignaturesFlow.kt:64) ~[corda-core-3.2-corda.jar:?]
    at net.corda.core.flows.FlowLogic.subFlow(FlowLogic.kt:290) ~[corda-core-3.2-corda.jar:?]
    at com.example.flow.ExampleFlowMatching$CollectSignaturesInitiatingFlow.call(ExampleFlowMatching.kt:260) ~[classes/:?]
    at com.example.flow.ExampleFlowMatching$CollectSignaturesInitiatingFlow.call(ExampleFlowMatching.kt:258) ~[classes/:?]
    at net.corda.core.flows.FlowLogic.subFlow(FlowLogic.kt:290) ~[corda-core-3.2-corda.jar:?]
    at com.example.flow.ExampleFlowMatching$ExampleFlowMatchingInitiator.call(ExampleFlowMatching.kt:202) ~[classes/:?]
    at com.example.flow.ExampleFlowMatching$ExampleFlowMatchingInitiator.call(ExampleFlowMatching.kt:45) ~[classes/:?]
    at net.corda.node.services.statemachine.FlowStateMachineImpl.run(FlowStateMachineImpl.kt:96) [corda-node-3.2-corda.jar:?]
    at net.corda.node.services.statemachine.FlowStateMachineImpl.run(FlowStateMachineImpl.kt:44) [corda-node-3.2-corda.jar:?]
    at co.paralleluniverse.fibers.Fiber.run1(Fiber.java:1092) [quasar-core-0.7.9-jdk8.jar:0.7.9]
    at co.paralleluniverse.fibers.Fiber.exec(Fiber.java:788) [quasar-core-0.7.9-jdk8.jar:0.7.9]
    at co.paralleluniverse.fibers.RunnableFiberTask.doExec(RunnableFiberTask.java:100) [quasar-core-0.7.9-jdk8.jar:0.7.9]
    at co.paralleluniverse.fibers.RunnableFiberTask.run(RunnableFiberTask.java:91) [quasar-core-0.7.9-jdk8.jar:0.7.9]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_191]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_191]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_191]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:1.8.0_191]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_191]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_191]
    at net.corda.node.utilities.AffinityExecutor$ServiceAffinityExecutor$1$thread$1.run(AffinityExecutor.kt:62) [corda-node-3.2-corda.jar:?]

1 个答案:

答案 0 :(得分:0)

问题在于,您是在主流程ExampleFlowMatchingInitiator中而不是在CollectSignaturesInitiatingFlow内部启动流程会话。

调用initiateFlow时,它将在当前InitiatingFlow的上下文中与交易对手开始流会话。因此,当您在initiateFlow中呼叫ExampleFlowMatchingInitiator时,对方会检查其是否为ExampleFlowMatchingInitiator注册了任何响应者。它确实-Acceptor-因此开始了一个流程会话,以使用Acceptor与交易对手进行通话。

如果您改为在initiateFlow内致电CollectSignaturesInitiatingFlow,则交易对手将检查其是否为CollectSignaturesInitiatingFlow注册了任何响应者。它确实-CollectSignaturesInitiatingFlowResponder-因此,它开始使用CollectSignaturesInitiatingFlowResponder来与对方进行对话的流会话。