Corda Race Condition,从电流中调用其他流,但输入状态来自电流 [英] Corda Race Condition, Invoking some other flow from current flow, but input state is from current flow

查看:48
本文介绍了Corda Race Condition,从电流中调用其他流,但输入状态来自电流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在讨论从流中进行异步HTTP调用时提出了这个问题

This question is raised while discussing Making asynchronous HTTP calls from flows

假设我们正在实施贷款申请。收到 LoanRequest 后,Corda流将进行HTTP调用以验证请求,我们希望根据HTTP调用的结果自动调用其他事务,即记录 ApprovedLoan RejectedLoan State。

Suppose, we are implementing a Loan Application. After a LoanRequest is received, Corda flow will make an HTTP call to verify the request and we want to invoke other transaction automatically according to the result of HTTP call i.e to record ApprovedLoan or RejectedLoan State.

这种情况下的问题是, ApprovedLoan RejectedLoan 交易将需要输入状态为 LoanRequest 。因此,我们无法从 LoanRequest 流的Acceptor中调用其他流,因为尚未提交输入状态,从而导致竞争状态。

Now problem in this scenario is, ApprovedLoan or RejectedLoan transaction will need input state as LoanRequest. So we can't invoke the other flow from Acceptor of LoanRequest flow as the input state is not committed yet and thus resulting in race condition.

任何可以实现此建议的建议或示例。

Any suggestion or examples on how this can be implemented would be appreciated.

谢谢。

推荐答案

您需要先将 LoanRequest 事务提交到每个节点的存储,然后再在接受器中进行调用以决定是否批准或拒绝该请求。您还需要使用 FlowLogic.waitForLedgerCommit 来确保您不会在 LoanRequest 获得批准或拒绝之前开始已存储。例如:

You need to commit the LoanRequest transaction to each node's storage first, before making the call in the acceptor to decide whether to approve or reject the request. You also need to use FlowLogic.waitForLedgerCommit to ensure you don't kick off the approval or rejection before the LoanRequest has been stored. Here's an example:

@InitiatingFlow
@StartableByRPC
class Initiator(val otherParty: Party) : FlowLogic<SignedTransaction>() {

    /**
     * The flow logic is encapsulated within the call() method.
     */
    @Suspendable
    override fun call(): SignedTransaction {
        val session = initiateFlow(otherParty)

        val fullySignedTx: SignedTransaction = TODO("Build fully signed transaction.")

        subFlow(FinalityFlow(fullySignedTx))

        session.send(fullySignedTx.id)
    }
}

@InitiatedBy(Initiator::class)
class Acceptor(val session: FlowSession) : FlowLogic<SignedTransaction>() {
    @Suspendable
    override fun call(): SignedTransaction {
        TODO("Response logic for building fully signed transaction.")

        val txId = session.receive<SecureHash>().unwrap { secureHash -> secureHash }

        waitForLedgerCommit(txId)

        val approve: Boolean = TODO("Make HTTP call to decide whether to approve or reject.")

        if (approve) {
            TODO("Response logic for building approval transaction.")
        } else {
            TODO("Response logic for building rejection transaction.")
        }
    }
}

这篇关于Corda Race Condition,从电流中调用其他流,但输入状态来自电流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆