kotlin - 接受者如何为状态添加值(value)并发送回发起者

标签 kotlin corda

我有方案:

  • NodeA将状态发送到NodeB(状态:值= 100,CustomerFlag = 0 )
  • NodeB审查状态/值,并与一起发送批准的标志
    签名。(状态:值= 100,CustomerFlag = 1 )

  • 您能帮我如何在NodeB批准/拒绝响应时增加该值吗?

    启动器类:
        @Suspendable
        override fun call(): SignedTransaction {
    
            val notary = serviceHub.networkMapCache.notaryIdentities[0]
    
            val iouState = IOUState(iouValue, serviceHub.myInfo.legalIdentities.first(), otherParty)
            val txCommand = Command(IOUContract.Commands.Create(), iouState.participants.map { it.owningKey })
            val txBuilder = TransactionBuilder(notary).withItems(StateAndContract(iouState, IOU_CONTRACT_ID), txCommand)
    
            txBuilder.verify(serviceHub)
            otherPartyFlow.send(partSignedTx)
            val partSignedTx = serviceHub.signInitialTransaction(txBuilder)
            val otherPartyFlow = initiateFlow(otherParty)
            otherPartyFlow.send(partSignedTx)
            return partSignedTx
        }
    }
    

    接受者类别:
    @InitiatedBy(Initiator::class)
    class Acceptor(val otherPartyFlow: FlowSession) : FlowLogic<SignedTransaction>() {
        @Suspendable
        override fun call(): SignedTransaction {
    
            val receivedData = otherPartyFlow.receive<SignedTransaction>()
    
            receivedData.unwrap {data -> data }
    <<<<< Need help here how to add value and send back >>>>
    return updatedData
    }
    }
    

    最佳答案

    您可能需要稍微重新考虑一下流程以使其正常工作。我想说,您最好的选择不是发送已签名的交易给接受方,而是发送IOU状态。然后,Acceptor类可以更改状态中的此标志,并创建/签名事务。然后可以将其发送回启动器类,以验证标志已更改,对事务进行签名,然后将其提交到分类帐。

    @Suspendable
    override fun call(): SignedTransaction {
    
        val iouState = IOUState(iouValue, serviceHub.myInfo.legalIdentities.first(), otherParty)
    
        val otherPartyFlow = initiateFlow(otherParty)
        otherPartyFlow.send(iouState)
    
        //Receieve back the signedTransaction
        val ptx = otherPartyFlow.receive<SignedTransaction>().unwrap{it}
        val stx = serviceHub.addSignature(ptx)
        //Resolve and commit to the ledger
        subFlow(ResolveTransactionsFlow(stx, otherPartyFlow))
        return subFlow(FinalityFlow(stx))
        }
    }
    
    @InitiatedBy(Initiator::class)
    class Acceptor(val otherPartyFlow: FlowSession) : FlowLogic<SignedTransaction>() {
        @Suspendable
        override fun call(): SignedTransaction {
            val notary = serviceHub.networkMapCache.notaryIdentities[0]
            val receivedData = otherPartyFlow.receive<IOUState>().unwrap{
                //Do any state verification here
                it
            }
            receivedData.customerFlag = 1
    
           //Create the txn
           val txCommand = Command(IOUContract.Commands.Create(), receivedData.participants.map { it.owningKey })
           val txBuilder = TransactionBuilder(notary).withItems(StateAndContract(receivedData, IOU_CONTRACT_ID), txCommand)
           //Sign the transaction
           val partSignedTx = serviceHub.signInitialTransaction(txBuilder)
           otherPartyFlow.send(partSignedTx)
           return waitForLedgerCommit(partSignedTx.id)
    }
    }
    

    您也可以考虑使用signTransactionsFlow收集签名,而不是像上面的Ive那样来回发送stx-请参阅https://docs.corda.net/flow-state-machines.html

    关于kotlin - 接受者如何为状态添加值(value)并发送回发起者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47703462/

    相关文章:

    java - 在 Corda 中用其他关系数据库替换 h2 数据库

    gradle - 我如何将修改后的Corda Core项目的jar文件引用到Tutorial项目中

    java - 使用 Kotlin 在 Android 中发送用于 Volley 授权的 header

    kotlin - 将号码分为不同的范围

    android - 无法使用 lambda 找到数据绑定(bind)事件的方法

    Kotlin 协程压缩三个流程

    rpc - Corda 查看终端中的消费状态

    corda - 如果我在Corda上创建代币,如何确保发行人知道每个人的余额?

    java - 为 Cordapp 多次调用 Controller 类中的 HTTP 路由

    android - 导航 Jetpack Compose 时应用程序崩溃