Corda:收集网络中其他各方的意见

时间:2017-10-18 11:02:34

标签: corda

在本教程中,提到节点可以要求对方查询其保管库并提供所需的结果。是否有可用于在流程中集成此逻辑的API?此外,是否可以要求我们的对手方收集其对方的输入并返回累积结果。如果有的话,请分享示例代码。感谢。

1 个答案:

答案 0 :(得分:1)

没有专门的API。您只需使用标准的FlowSession.send / FlowSession.receive / FlowSession.sendAndReceive来电。

但是,在收到来自对方(通常是SignedTransactionStateAndRef)的数据后,请确保使用ResolveTransactionsFlow解析其依赖关系链,以便您可以验证它是通过有效的交易顺序创建。

还有一个内置的SendTransactionFlow / ReceiveTransactionFlow对,可以自动完成接收事务,解包和解析依赖关系的过程。

以下是接收对方发送的StateAndRef<ContractState>节点的示例:

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