首页 > 解决方案 > 升级一些 Corda3 源代码以在 v4 上运行

问题描述

首先,我三个月前才开始学习corda,所以我还有一些学习要做。

我继承了一些在 Corda v3.3 下运行良好的代码,但客户希望它在 v4 上运行。我正在尝试按照主网站上的说明进行操作。我有一个启动流,它调用一个子流,它又调用一个转发器流。

启动流程:

@InitiatingFlow(version = 2)
@StartableByRPC
class TransferFlow(private val issuerName: String = "",
                   private val seller: String = "",
                   private val amount: BigDecimal = BigDecimal("0"),
                   private val buyer: String = "",
                   private val custodianNameOfBuyer: String = "",
                   private val notaryName: String = "") : FlowLogic<SignedTransaction>() {
    @Suspendable
    override fun call(): SignedTransaction {

        subFlow(UpdateStatusOfTransferFlow(
                sessions,
                tokenTransferAgreement.linearId,
                "Removed Tokens From Seller"))
  }
}

class UpdateStatusOfTransferFlow(
        private val sessions: Set<FlowSession>,
        private val tokenTransferAgreementID: UniqueIdentifier,
        private val newStatus: String) : FlowLogic<SignedTransaction>() {
    @Suspendable
    override fun call(): SignedTransaction {
        sessions.size
        val idQueryCriteria = QueryCriteria.LinearStateQueryCriteria(linearId = listOf(tokenTransferAgreementID))
        val states = serviceHub.vaultService.queryBy<TokenTransferAgreement>(idQueryCriteria).states

        if (states.size != 1) throw FlowException("Can not find a unique state for $tokenTransferAgreementID")

        val inputStateAndRef = states.single()
        val inputState = inputStateAndRef.state.data

        val notary = inputStateAndRef.state.notary

        val outputState = inputState.withNewStatus(newStatus)

        val cmd = Command(TokenContract.Commands.UpdateStatusOfTransfer(),
                inputState.participants.map { it.owningKey })


        val txBuilder = TransactionBuilder(notary = notary)
        txBuilder.addCommand(cmd)
        txBuilder.addInputState(inputStateAndRef)
        txBuilder.addOutputState(outputState, TokenContract.ID)

        txBuilder.verify(serviceHub)
        val ptx = serviceHub.signInitialTransaction(txBuilder)
        val sessions2 = (inputState.participants.toSet() - ourIdentity).map { initiateFlow(it) }

       return subFlow(CollectSignaturesFlow(ptx, sessions2))

    }

}

And the responder:

@InitiatedBy(TransferFlowResponder::class)
class UpdateStatusOfTransferFlowResponder(private val session: FlowSession) : FlowLogic<Unit>() {
    @Suspendable
    override fun call() {
        val tokenTransferAgreements = mutableListOf<TokenTransferAgreement>()
        var isBuyer = true
        var notary = CordaUtility.getNotary(serviceHub) ?: throw FlowException("An notary is expected!")

        val signedTransactionFlow = subFlow(object : SignTransactionFlow(session) {
            override fun checkTransaction(stx: SignedTransaction) = requireThat {
                "There must be one output!" using (stx.tx.outputStates.size == 1)

                val tokenTransferAgreement = stx.tx.outputStates.first() as TokenTransferAgreement
                tokenTransferAgreements.add(tokenTransferAgreement)

                notary = stx.notary ?: throw FlowException("An notary is expected!")

                if (ourIdentity == tokenTransferAgreement.issuer) {
                   //checks go here
                 }

        })
    }

}

我相信我应该在某个时候添加对 ReceiveFinality 流的调用,但是它只需要 1 个会话作为参数,而不是我在这里的列表。我应该打多个电话,每个会话一个吗?我也不确定呼叫是否应该进入转发器或 UpdateStatusOfTransferFlow 类。

在这里的帮助将不胜感激。

标签: kotlincorda

解决方案


FinalityFlow 主要负责确保交易经过公证,相应地分发并保存到本地保险库。

在以前的 Corda 版本中,所有节点默认都会接受传入的最终请求。

从 V4 开始,您需要编写 ReceiveFinalityFlow 以在最终确定之前编写自己的处理逻辑。

最终性目前在 Corda 中运行的方式是发起节点,作为最终性的中间步骤,将经过公证的交易分发给所有其他参与者。它发送到的每个参与节点只期望从该节点接收会话。

因此,您可能会向发起的 FinalityFlow 提交多个会话以包含所有参与者,响应节点将只会收到来自发起者的一个会话。

将来,我们可能会考虑让公证人将经过公证的交易分发给所有参与者,但即便如此,ReceiveFinalityFlow 仍然只期望一个会话,这次来自公证人。


推荐阅读