gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:23:36 25 4
gpt4 key购买 nike

我有方案:

  • 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/

    25 4 0