【问题标题】:How to add FlowException inside Flow in Corda?如何在 Corda 的 Flow 中添加 FlowException?
【发布时间】:2020-04-03 21:16:59
【问题描述】:

我正在尝试在 Corda Flow 中添加 FlowException。

我在我的代码中添加了以下部分。这总是正确的。我只想显示消息“Trade id already available”

 val a : Int = 10
            val b : Int = 10

            if (a == b){
                throw FlowException("Trade id already available")
            }

当我运行代码时,由于 Flows 由于异常而提前结束,它应该显示 Flow 异常错误。但它唯一要做的就是结束流程而不显示消息

我的完整代码是:


object ExampleFlow {
    @InitiatingFlow
    @StartableByRPC
    class Initiator(val iouValue: Int,
                    val otherParty: Party) : FlowLogic<SignedTransaction>() {



        @Suspendable
        override fun call(): SignedTransaction {
            // Obtain a reference to the notary we want to use.
            val notary = serviceHub.networkMapCache.notaryIdentities[0]

            val a : Int = 10
            val b : Int = 10

            if (a == b){
                throw FlowException("Trade id already available")
            }


            val iouState = IOUState(iouValue, serviceHub.myInfo.legalIdentities.first(), otherParty)
            val txCommand = Command(IOUContract.Commands.Create(), iouState.participants.map { it.owningKey })
            val txBuilder = TransactionBuilder(notary)
                    .addOutputState(iouState, IOUContract.ID)
                    .addCommand(txCommand)


            txBuilder.verify(serviceHub)


            val partSignedTx = serviceHub.signInitialTransaction(txBuilder)


            val otherPartySession = initiateFlow(otherParty)
            val fullySignedTx = subFlow(CollectSignaturesFlow(partSignedTx, setOf(otherPartySession), GATHERING_SIGS.childProgressTracker()))


            return subFlow(FinalityFlow(fullySignedTx, setOf(otherPartySession), FINALISING_TRANSACTION.childProgressTracker()))
        }
    }

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

            return subFlow(ReceiveFinalityFlow(otherPartySession, expectedTxId = txId))
        }
    }
}

【问题讨论】:

    标签: corda


    【解决方案1】:

    它对我有用,我使用了与你相同的代码:

    @Suspendable
    override fun call(): SignedTransaction {
        // Obtain a reference to the notary we want to use.
        val notary = serviceHub.networkMapCache.notaryIdentities[0]
    
        val a : Int = 10
        val b : Int = 10
    
        if (a == b){
            throw FlowException("Trade id already available")
        }
    
        // Stage 1.
        progressTracker.currentStep = GENERATING_TRANSACTION
        // Generate an unsigned transaction.
        val iouState = IOUState(iouValue, serviceHub.myInfo.legalIdentities.first(), otherParty)
        val txCommand = Command(IOUContract.Commands.Create(), iouState.participants.map { it.owningKey })
        val txBuilder = TransactionBuilder(notary)
                .addOutputState(iouState, IOUContract.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)
    
        // Stage 4.
        progressTracker.currentStep = GATHERING_SIGS
        // Send the state to the counterparty, and receive it back with their signature.
        val otherPartySession = initiateFlow(otherParty)
        val fullySignedTx = subFlow(CollectSignaturesFlow(partSignedTx, setOf(otherPartySession), GATHERING_SIGS.childProgressTracker()))
    
        // Stage 5.
        progressTracker.currentStep = FINALISING_TRANSACTION
        // Notarise and record the transaction in both parties' vaults.
        return subFlow(FinalityFlow(fullySignedTx, setOf(otherPartySession), FINALISING_TRANSACTION.childProgressTracker()))
    }
    

    这是节点外壳:

    【讨论】:

    • 这可能是什么原因?不仅仅是这个项目,我在不同的项目中也面临同样的问题。如果不满足某些条件但不抛出 flowexception 消息,则流过早结束。
    • 由于您的流程基于cordapp-example 的ExampleFlow;我建议您重新开始,这意味着将流程的示例代码复制/粘贴到您的ExmapleFlow 文件中,在 shell 中运行流程(以确保代码正常工作并且您没有遗漏任何内容);然后添加引发异常的行并再次运行流程以查看会发生什么。
    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2020-02-25
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多