在Corda中,如何查询保管库中特定状态之后记录的所有状态?

时间:2018-08-01 16:50:52

标签: corda

对于节点记录的状态,我有一个StateRef。自StateRef被记录以来,如何获得节点记录的所有状态的流?

1 个答案:

答案 0 :(得分:1)

您需要做两件事:

  1. 确定记录您的StateRef的时间
  2. 从那时起开始流式传输更新

这是一个可以执行此操作的示例RPC客户端:

fun main(args: Array<String>) {
    // Getting an RPC connection to the node.
    require(args.size == 1) { "Usage: ExampleClientRPC <node address>" }
    val nodeAddress = NetworkHostAndPort.parse(args[0])
    val client = CordaRPCClient(nodeAddress)
    val rpcOps = client.start("user1", "test").proxy

    // Change this to an actual StateRef.
    val dummyStateRef = StateRef(SecureHash.zeroHash, 0)

    // Getting the time the state was recorded.
    val queryByStateRefCriteria = VaultQueryCriteria(stateRefs = listOf(dummyStateRef))
    val queryByStateRefResults = rpcOps.vaultQueryBy<ContractState>(queryByStateRefCriteria)
    val queryByStateRefMetadata = queryByStateRefResults.statesMetadata
    val dummyStateRefRecordedTime = queryByStateRefMetadata.single().recordedTime

    // Getting the states recorded after that time.
    val queryAfterTimeExpression = TimeCondition(
            RECORDED, BinaryComparison(BinaryComparisonOperator.GREATER_THAN_OR_EQUAL, dummyStateRefRecordedTime))
    val queryAfterTimeCriteria = VaultQueryCriteria(
            status = ALL,
            timeCondition = queryAfterTimeExpression)
    val queryAfterTimeResults = rpcOps.vaultTrackBy<ContractState>(queryAfterTimeCriteria)
    val afterTimeStates = queryAfterTimeResults.states
}