验证交易

Verifying a transaction

我需要一些帮助来了解交易验证在 Corda 中的工作原理。如果我没理解错的话,交易各方自己负责验证。

我有一个与两方之间的 negotiation 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 时没有出现错误,其中双方也是签名者并且流程的构建几乎完全相同?

  1. 我没看到你在启动器中调用 CollectSignaturesFlow;没有那个电话,你怎么让对方签字?
  2. 无需在响应者中调用verify(),如果你打开SignTransactionFlow()的代码你会看到here当它收到交易时;它解决并验证导致此交易(包括此交易)的所有交易。所以你可以安全地删除那个电话。