接受者如何向状态添加值并发送回发起者

How acceptor can add value to state and send back to initiator

我有场景:

  1. NodeA向NodeB发送状态(State:value=100,CustomerFlag=0)

  2. NodeB 审核 state/value 并发送已批准标志以及 signature.(State: value=100, CustomerFlag=1)

你能帮我在 NodeB 响应 approve/reject 时如何添加该值吗?

发起人class:

    @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
    }
}

接受者class:

@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 class 然后可以在状态中更改此标志,并 create/sign 交易。然后可以将其发送回发起方 class 以验证标志已更改,签署交易然后将其提交到账本。

@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 来收集签名,而不是像我上面那样来回发送 stx - 参见 https://docs.corda.net/flow-state-machines.html

我创建了 2 个独立的流程客户和客户。

  1. 客户流程1: 创建批准标志为 0 的状态并提交到账本。

    subFlow(FinalityFlow(fullySignedTx, FINALISING_TRANSACTION.childProgressTracker()))

  2. 客户流量2:

    • 提取客户提交的状态并将该状态用作客户流的输入。 val criteria = QueryCriteria.VaultQueryCriteria(status = Vault.StateStatus.UNCONSUMED) val results = serviceHub.vaultService.queryBy<POCState>(criteria) val pocState = results.states.last().state.data

    • 重置批准标志 =1 并提交到分类帐。

    val ourOtherOutputState: POCState = pocState.copy(stateCode = pocStateCode)

    • 建立交易并提交到账本。

    val txCommand = Command(BankContract.Commands.Create(), pocState.participants.map { it.owningKey }) val txBuilder = TransactionBuilder(notary).withItems(StateAndContract(ourOtherOutputState, POC_CONTRACT_ID), txCommand) val partSignedTx = serviceHub.signInitialTransaction(txBuilder) val fullySignedTx = subFlow(CollectSignaturesFlow(partSignedTx, setOf(otherPartyFlow), GATHERING_SIGS.childProgressTracker())) return subFlow(FinalityFlow(fullySignedTx, FINALISING_TRANSACTION.childProgressTracker()))

感谢@Raymond Mogg 的支持!