Crate sidechain_block_search

Source
Expand description

Binary search queries for Partner Chain slots and epochs

§Purpose of this crate

Standard Substrate block storage allows for retrieving blocks based on their number and hash. However, Partner Chains toolkit introduces two new categories that are not supported by this lookup: slot and epoch. This crate provides a mechanism to quickly query for blocks based on their Partner Chain epoch or slot by applying a binary search over historical blocks.

§Usage

The binary search feature is provided via the FindSidechainBlock trait. This trait is implemented for any runtime client that implements the [GetSidechainStatus] runtime API. To query the blockchain, a predicate must be passed to the query that defines the searched block. Some predefined targets are defined in the predicates module, otherwise a new target type can be defined by implementing the CompareStrategy trait.

Given a runtime client that satisfies the trait bounds, the blockchain can be queried like this:

use sidechain_block_search::predicates::AnyBlockInEpoch;
use sidechain_block_search::{ FindSidechainBlock, Client };
use sidechain_domain::*;
use sp_api::ProvideRuntimeApi;
use sp_runtime::traits::{ Block as BlockT, NumberFor };
use sp_sidechain::GetSidechainStatus;

fn query_example<B, C>(client: C)
where
    B: BlockT,
    NumberFor<B>: From<u32> + Into<u32>,
    C: ProvideRuntimeApi<B> + Client<B> + Send + Sync + 'static,
    C::Api: GetSidechainStatus<B>
{
    let search_target = AnyBlockInEpoch {
        epoch: ScEpochNumber(42)
    };
    let result = client.find_block(search_target);
}

Modules§

predicates
Types of binary search queries over Partner Chain blocks

Traits§

Client
Runtime API client used by the block queries in this crate
CompareStrategy
Comparator used for binary searching the block history
FindSidechainBlock
Runtime client capable of finding Partner Chain blocks via binary search using some CompareStrategy.
SidechainInfo
Interface for retrieving information about slot and epoch of Partner Chain blocks

Functions§

binary_search_by
Performs binary search over range using ordering provided by f