首页 > 解决方案 > 验证交易

问题描述

我需要一些帮助来了解交易验证在 Corda 中的工作原理。如果我理解正确,交易中的所有各方都有责任自己验证。

我有一个与两方之间的协商 cordapp非常相似的用例。在编写流程时,我会在发起方中验证未签名的交易:

// build the state that will be used as an output
val utx = TransactionBuilder(notary)
    .addInputState(input)
    .addOutputState(output)
    .addCommand(RequestContract.Commands.Accept(), output.participants.map { it.owningKey })

// verify the transaction against the smart contract
utx.verify(serviceHub)

// Sign the transaction.
val ptx = serviceHub.signInitialTransaction(utx, ourIdentity.owningKey)

在响应者中,我还需要验证交易。

val signTransactionFlow = object : SignTransactionFlow(counterpartySession) {
    override fun checkTransaction(stx: SignedTransaction) {
        val state = stx.tx.outputStates.single() as RequestState
        requireThat {
            "Fulfilling a request has to be initiated by the fulfilling party" using (state.requestingParty == ourIdentity)
        }
    }
}

val stx = subFlow(signTransactionFlow)

// verify the transaction against the smart contract
stx.verify(serviceHub, checkSufficientSignatures = false)

return subFlow(ReceiveFinalityFlow(counterpartySession, stx.id))

除非我设置checkSufficientSignatures = false,否则我会收到签名丢失的错误 ( SignedTransaction$SignaturesMissingException)。

为什么我会收到错误消息?我在发起者和响应者流程中签署了交易。checkSufficientSignatures如果我设置为 false ,如何确保所有签名都存在?最后,为什么在创建时没有出现错误RequestState,双方都是签名者,并且流程的构建几乎相同?

标签: corda

解决方案


  1. 我没有看到你打电话CollectSignaturesFlow给发起人;没有那个电话,你将如何要求对方签字?
  2. 不需要调用verify()响应者,如果你打开代码SignTransactionFlow()你会在这里看到它收到交易时;它解决并验证导致此交易的所有交易(包括此交易)。因此,您可以安全地删除该呼叫。

推荐阅读