2017-10-18 28 views
0

在本教程中,提到節點可以要求對方查詢其庫並提供所需的結果。是否有可用於將此邏輯集成到流程中的API?另外,是否有可能要求我們的對手方收集對方的投入並返回累計結果。請分享示例代碼,如果有的話。謝謝。Corda:收集來自網絡中其他方的輸入

回答

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