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 |
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();
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()
.
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());
});
Mono<BlockHeightQueryResult> blockHeightQueryMono = queryClient.executeQuery(new BlockHeightQuery());
BlockHeightQueryResult blockHeightQueryResult = blockHeightQueryMono.block(Duration.ofSeconds(10));
Mono<ChainPointQueryResult> chainPointQueryMono = queryClient.executeQuery(new ChainPointQuery());
ChainPointQueryResult chainPointQueryResult = chainPointQueryMono.block(Duration.ofSeconds(10));
Mono<CurrentProtocolParamQueryResult> mono = queryClient.executeQuery(new CurrentProtocolParamsQuery());
CurrentProtocolParamQueryResult protocolParams = mono.block(Duration.ofSeconds(10));
Mono<EpochNoQueryResult> queryResultMono = queryClient.executeQuery(new EpochNoQuery());
EpochNoQueryResult epochNoQueryResult = queryResultMono.block(Duration.ofSeconds(10));
Mono<UtxoByAddressQueryResult> queryResultMono = queryClient.executeQuery(new UtxoByAddressQuery(new Address("addr1vpf...")));
UtxoByAddressQueryResult utxoByAddressQueryResult = queryResultMono.block(Duration.ofSeconds(20));