Rudrakshi
Rudrakshi

Reputation: 55

Corda: Gathering inputs from other parties in the network

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.

Upvotes: 0

Views: 754

Answers (1)

Joel
Joel

Reputation: 23140

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

Upvotes: 1

Related Questions