在Corda中,基于Java中的自定义架构字段查询状态

时间:2018-07-17 17:23:35

标签: corda

在Corda中,我想基于自定义架构字段查询节点的保管库。

例如,在Example CorDapp中,我想根据IOU的值查询IOU。

如何用Java执行此查询?

1 个答案:

答案 0 :(得分:0)

基于相同的Example CorDapp,这是一个RPC客户端,它在节点的保管库中查询值为99的IOU:

public class ExampleClientRPC {

    public static void main(String[] args) {
        final NetworkHostAndPort nodeAddress = NetworkHostAndPort.parse("localhost:10005");
        final CordaRPCClient client = new CordaRPCClient(nodeAddress, CordaRPCClientConfiguration.DEFAULT);
        final CordaRPCConnection connection = client.start("user1", "test");
        final CordaRPCOps proxy = connection.getProxy();

        CordaX500Name partyBName = new CordaX500Name("PartyB", "London", "GB");
        Party partyB = proxy.wellKnownPartyFromX500Name(partyBName);

        try {
            proxy.startFlowDynamic(ExampleFlow.Initiator.class, 97, partyB).getReturnValue().get();
            proxy.startFlowDynamic(ExampleFlow.Initiator.class, 98, partyB).getReturnValue().get();
            proxy.startFlowDynamic(ExampleFlow.Initiator.class, 99, partyB).getReturnValue().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        QueryCriteria generalCriteria = new QueryCriteria.VaultQueryCriteria(Vault.StateStatus.UNCONSUMED);
        Field valueField = null;
        try {
            valueField = IOUSchemaV1.PersistentIOU.class.getDeclaredField("value");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        QueryCriteria iouQuery = new VaultCustomQueryCriteria(Builder.equal(valueField, 99));
        generalCriteria = generalCriteria.and(iouQuery);

        List<StateAndRef<IOUState>> results = proxy.vaultQueryBy(generalCriteria,
                new PageSpecification(1,  10),
                new Sort(Collections.emptySet()),
                IOUState.class).getStates();

        for (StateAndRef<IOUState> result : results) {
            System.out.println(result);
        }

        connection.notifyServerAndClose();
    }
}