From 56b2df5f0da3edc14cd4682b58309053cbcaa12f Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Tue, 16 Jul 2024 08:16:58 +0700 Subject: [PATCH] feat(hardano): add fuzzy block search by slot in Immutable db (#484) Co-authored-by: Mr-Leshiy --- pallas-hardano/src/storage/immutable/mod.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pallas-hardano/src/storage/immutable/mod.rs b/pallas-hardano/src/storage/immutable/mod.rs index b3d6a97..42f4bcd 100644 --- a/pallas-hardano/src/storage/immutable/mod.rs +++ b/pallas-hardano/src/storage/immutable/mod.rs @@ -75,6 +75,13 @@ fn chunk_binary_search( /// Iterates through the blocks until the given slot and block hash are reached. /// Returns an iterator over the blocks if the specific block is found, /// otherwise returns an error. +/// +/// IFF the `block_hash` is zero length, then the search is "fuzzy", +/// meaning that it will return the first block whose slot is greater than or +/// equal to `slot`. +/// +/// Fuzzy Search allows a block to be found by an "expected slot#" without +/// knowing precisely which block is being retrieved. fn iterate_till_point( iter: impl Iterator, slot: u64, @@ -99,7 +106,9 @@ fn iterate_till_point( } } - if block.hash().as_ref().eq(block_hash) && block.slot() == slot { + if (block_hash.is_empty() && block.slot() >= slot) + || (block.hash().as_ref().eq(block_hash) && block.slot() == slot) + { Ok(iter) } else { Err(Error::CannotFindBlock(Point::Specific( @@ -169,6 +178,16 @@ pub fn read_blocks(dir: &Path) -> Result, Er /// Returns an iterator over the chain from the given point if the specific /// block is found, otherwise returns an error. /// +/// # Note: +/// +/// If the given `point` is not the Origin, AND the BlockHash of the point is +/// zero length. then the search for the first block is "Fuzzy". +/// Only the Slot# of the point will be used and the first block to be returned +/// will be the block whose slot is >= the given slot# in the point. +/// +/// This allows iteration to commence from a calculated slot# where the precise +/// block is unknown, and then continue iterating blocks after that point. +/// /// # Errors /// /// * `Error::OriginMissing` - If the first block in the chain is not the