Corda在单个流调用中返回多个事务() [英] Corda returning Multiple transactions in a single flow call ()

查看:69
本文介绍了Corda在单个流调用中返回多个事务()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,其中一方需要读取状态列表(例如DEAL STATE),然后遍历该列表以查找匹配的记录.如果匹配,则需要通过组合匹配的字段来创建新的输出状态.

I have the scenario where One party needs to read a list of states ( DEAL STATE for ex.), Then iterate through the list to find out the matching records. If it is matching, we need to create new output state by combining the matched fields.

因此,一旦在循环中执行了匹配,我们就可以获取输入和输出状态的列表.

So once the matching is performed within the loop, we can get the list of input and output states.

但是我对从其他方收集签名感到困惑,因为每个记录的交易对手都会有所不同.另外,如何在单个流程调用方法中调用多个事务的最终流程?

But I am confused on collecting the signature from other parties since the counterparties will be different for each of the record. Also, how will I call the finality flow for multiple transactions within single flow call method?

@joel,另一个问题-假设outputstate1参加者为A,B,C,而outputstate2参加者为B,C,D,即B&C参与2笔交易.因此,在匹配状态循环中,当我们创建flowsessions映射时,其签名者为A,B,C,D.但是,当我们调用CollectSignaturesFlow时,我们需要传递每个partialsigned trxn和会话.那么如何传递与trxn对应的会话?

@joel, Another problem - Suppose outputstate1 particicpants are say A, B, C, and outputstate2 paticipants are B, C ,D ,ie B & C are involved in 2 transactions. So within matching states loop, when we make flowsessions map, it will have the signers are A, B, C, D. But when we call CollectSignaturesFlow, we need to pass each of the partialsigned trxn and sessions. So how to pass the session corresponding to a trxn ?

推荐答案

以下是我们如何收集每种状态的签名的示例:

Here's an example of how we could collect the signatures for each state:

val flowSessionMap = mutableMapOf<Party, FlowSession>()

val fullySignedTransactions = matchingStates.forEach { matchingState ->
    val requiredSigners: List<Party> = TODO("Derive this from the matching state somehow.")
    val signedTransaction: SignedTransaction = TODO("Build transaction.")

    val sessions = requiredSigners.map { signer ->
        flowSessionMap.getOrPut(signer) {
            initiateFlow(signer)
        }
    }

    val fullySignedTransaction = subFlow(CollectSignaturesInitiatingFlow(
        signedTransaction, sessions)
    )
}

CollectSignaturesInitiatingFlow 的定义如下:

@InitiatingFlow
class CollectSignaturesInitiatingFlow(val signedTransaction: SignedTransaction, val sessions: List<FlowSession>): FlowLogic<SignedTransaction>() {
    override fun call(): SignedTransaction {
        return subFlow(CollectSignaturesFlow(signedTransaction, sessions))
    }
}

CollectSignaturesInitiatingFlow 的响应者定义如下:

@InitiatedBy(CollectSignaturesInitiatingFlow::class)
class CollectSignaturesInitiatingFlowResponder(val otherPartyFlow: FlowSession) : FlowLogic<SignedTransaction>() {
    @Suspendable
    override fun call(): SignedTransaction {
        val signTransactionFlow = object : SignTransactionFlow(otherPartyFlow) {
            override fun checkTransaction(stx: SignedTransaction) {
                TODO("Check the transaction here.")
            }
        }

        return subFlow(signTransactionFlow)
    }
}

请注意:

  • 我们非常谨慎,每个交易对手只能创建一个会话.从Corda 3开始,如果流程中每个交易对手创建多个会话,则会引发错误
  • 我们将对 CollectSignaturesFlow 的调用包装在 CollectSignaturesInitiatingFlow 中.为什么?在Corda中,有两种类型的流: Initiating 和内联.每个 Initiating 流实例都有一个唯一的ID,而每个内联流都继承将其称为子流的流ID.从Corda 3开始,如果为相同的流ID两次调用响应者,则会引发异常.通过将 CollectSignaturesFlow (一个内联流)包装在 CollectSignaturesInitiatingFlow (一个 Initiating 流)内,我们为每次收集签名的尝试创建一个新的流ID,不会抛出异常
  • We're being careful to only create one session per counterparty. As of Corda 3, an error will be thrown if a flow creates multiple sessions per counterparty
  • We're wrapping the call to CollectSignaturesFlow in CollectSignaturesInitiatingFlow. Why? In Corda, there are two types of flow: Initiating and inlined. Each Initiating flow instance has a unique ID, while each inlined flow inherits the ID of the flow that called it as a subflow. As of Corda 3, an exception is thrown if a responder is invoked twice for the same flow ID. By wrapping CollectSignaturesFlow (an inlined flow) inside CollectSignaturesInitiatingFlow (an Initiating flow), we create a new flow ID for each attempt to gather signatures, and no exception is thrown

一旦您拥有完全签名的交易,就可以循环调用 FinalityFlow :

Once you have the fully-signed transactions, you can call FinalityFlow in a loop:

for (transaction in fullySignedTransactions) {
    subFlow(FinalityFlow(transaction))
}

这篇关于Corda在单个流调用中返回多个事务()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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