Corda: Gathering inputs from other parties in the network

試著忘記壹切 提交于 2021-02-08 11:24:40

问题


In the tutorial, it is mentioned that a node can ask the other party to query its vault and give the required results. Is there an API that can be used to integrate this logic in the flow? Also, is it possible to ask our counter parties to gather inputs from their counter parties and return the cumulative results. Please share the example code, if any. Thanks.


回答1:


There is no specialised API. You just use the standard FlowSession.send / FlowSession.receive / FlowSession.sendAndReceive calls.

However, upon receiving the data from the counterparty (usually either a SignedTransaction or a StateAndRef), make sure you resolve its dependency chain using ResolveTransactionsFlow so that you can verify it was created through a valid sequence of transactions.

There is also a built-in SendTransactionFlow / ReceiveTransactionFlow pair that automates the process of receiving a transaction, unwrapping it, and resolving its dependencies.

Here's an example of a node receiving a StateAndRef<ContractState> sent by a counterparty:

@InitiatingFlow
@StartableByRPC
class Initiator(private val counterparty: Party) : 
FlowLogic<StateAndRef<ContractState>>() {
    @Suspendable
    override fun call(): StateAndRef<ContractState> {
        val counterpartySession = initiateFlow(counterparty)
        // Our flow will suspend and wait for a StateAndRef from the counterparty.
        val untrustedData = counterpartySession.receive<StateAndRef<ContractState>>()
        // Data received off the wire is considered untrustworthy, and must be unwrapped.
        val stateAndRef = untrustedData.unwrap { stateAndRef ->
            // We resolve the chain of transactions that generated this StateAndRef.
            subFlow(ResolveTransactionsFlow(setOf(stateAndRef.ref.txhash), counterpartySession))
            // TODO: More checking of what we've received.
            stateAndRef
        }
        return stateAndRef
    }
}

@InitiatedBy(Initiator::class)
class Responder(val counterpartySession: FlowSession) : FlowLogic<Unit>() {
    @Suspendable
    override fun call() {
        // We extract the first StateAndRef in our vault...
        val stateAndRef = serviceHub.vaultService.queryBy(ContractState::class.java).states.first()
        // ...and send it to our counterparty.
        counterpartySession.send(stateAndRef)
    }
}


来源:https://stackoverflow.com/questions/46808685/corda-gathering-inputs-from-other-parties-in-the-network

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!