yaci

Local State Queries

Local State Query mini-protocol allows to query the consensus / ledger state. This mini protocol is part of the Node-to-Client protocol, hence it is only used by local (and thus trusted) clients. Possible queries depend on the era (Byron, Shelly, etc)

Yaci provides implementation of Local State mini-protocol. Using LocalStateQueryClient you can execute supported queries.

The following queries are currently supported by Yaci-core. More queries will be supported in the future. But you can also easily add support for new queries by providing a Request and Reply implementation for the specific query. To implement a new query, you can refer to an existing query’s request and reply classes. (Example: ChainPointQuery and ChainPointQueryResult - query for chain block pointed at)

Query Description
SystemStartQuery System start time
BlockHeightQuery Current block height
ChainPointQuery Current block point at (slot, blockhash)
CurrentProtocolParamsQuery Current protocol parameters
EpochNoQuery Current epoch no query
UtxoByAddressQuery Get current utxos for an address

Connect to Local Node using LocalState protocol and start the agent

To create a LocalStateQueryClient, you need to first create LocalClientProvider.

LocalClientProvider localClientProvider = new LocalClientProvider(nodeSocketFile, protocolMagic);
LocalStateQueryClient localStateQueryClient = localClientProvider.getLocalStateQueryClient();

//Start localClientProvider
localClientProvider.start();

To shutdown the LocalClientProvider

localClientProvider.shutdown();

To reacquire at chain tip

Mono<Point> reAcquireMono = queryClient.reAcquire();
reAcquireMono.block();

To acquire at a point

 Mono<Point> acquireMono = queryClient.acquire(point);
 acquireMono.block();

Queries

Invoke LocalQueryClient.executeQuery(Query) to get an instance of Mono. The result can then be received through a blocking call Mono.block(timeout) or through a non-block call using Mono.subscribe().

1. Get Start time

Use LocalStateQueryClient to get the start time.

 Mono<SystemStartResult> queryResultMono = queryClient.executeQuery(new SystemStartQuery());
 SystemStartResult systemStartResult = queryResultMono.block(Duration.ofSeconds(10)); //with timeout 10sec

For non-blocking call, use Mono.subscribe().

 queryResultMono.subscribe(result -> {
     System.out.println("Start time >> " + result.getLocalDateTime());
 });

2. Get Block Height

 Mono<BlockHeightQueryResult> blockHeightQueryMono = queryClient.executeQuery(new BlockHeightQuery());
 BlockHeightQueryResult blockHeightQueryResult = blockHeightQueryMono.block(Duration.ofSeconds(10));

3. Get current chain point

 Mono<ChainPointQueryResult> chainPointQueryMono = queryClient.executeQuery(new ChainPointQuery());
 ChainPointQueryResult chainPointQueryResult = chainPointQueryMono.block(Duration.ofSeconds(10));

4. Get current protocol parameters

Mono<CurrentProtocolParamQueryResult> mono = queryClient.executeQuery(new CurrentProtocolParamsQuery());
CurrentProtocolParamQueryResult protocolParams = mono.block(Duration.ofSeconds(10));

5. Get Current Epoch No

Mono<EpochNoQueryResult> queryResultMono = queryClient.executeQuery(new EpochNoQuery());
EpochNoQueryResult epochNoQueryResult = queryResultMono.block(Duration.ofSeconds(10));

6. Get Utxos by an address

Mono<UtxoByAddressQueryResult> queryResultMono = queryClient.executeQuery(new UtxoByAddressQuery(new Address("addr1vpf...")));
UtxoByAddressQueryResult utxoByAddressQueryResult = queryResultMono.block(Duration.ofSeconds(20));