升级一些Corda3源代码以在v4上运行

时间:2019-05-07 04:40:56

标签: kotlin corda

首先,我3个月前才开始学习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类。

这里的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

在Corda的早期版本中,默认情况下,所有节点都将接受传入的请求以求确定性。

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

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

因此,在您可能向发起方FinalityFlow提交多个会话以包括所有参与者的情况下,响应节点将仅从发起方接收到一个会话。

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