Function ic_ledger_types::query_blocks
source · pub async fn query_blocks(
ledger_canister_id: Principal,
args: GetBlocksArgs
) -> CallResult<QueryBlocksResponse>
Expand description
Calls the “query_block” method on the specified canister.
Example
use candid::Principal;
use ic_cdk::api::call::CallResult;
use ic_ledger_types::{BlockIndex, Block, GetBlocksArgs, query_blocks, query_archived_blocks};
async fn query_one_block(ledger: Principal, block_index: BlockIndex) -> CallResult<Option<Block>> {
let args = GetBlocksArgs { start: block_index, length: 1 };
let blocks_result = query_blocks(ledger, args.clone()).await?;
if blocks_result.blocks.len() >= 1 {
debug_assert_eq!(blocks_result.first_block_index, block_index);
return Ok(blocks_result.blocks.into_iter().next());
}
if let Some(func) = blocks_result
.archived_blocks
.into_iter()
.find_map(|b| (b.start <= block_index && (block_index - b.start) < b.length).then(|| b.callback)) {
match query_archived_blocks(&func, args).await? {
Ok(range) => return Ok(range.blocks.into_iter().next()),
_ => (),
}
}
Ok(None)
}