Как добавить FlowException в Flow в Corda? - PullRequest
0 голосов
/ 03 апреля 2020

Я пытаюсь добавить FlowException в Corda Flow.

Я добавил следующую часть в мой код. Это всегда правда. Я просто хочу отобразить сообщение «Идентификатор сделки уже доступен»

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

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

Когда я запускаю код, поскольку потоки заканчиваются преждевременно из-за исключений, он должен отображать ошибку исключения потока. Но единственное, что он делает, это завершает поток, не отображая сообщение

This is how flow is ending

Мой полный код:


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))
        }
    }
}

1 Ответ

0 голосов
/ 03 апреля 2020

Это сработало для меня, я использовал тот же код, что и ваш:

@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()))
}

Вот оболочка узла:

enter image description here

...