brk_query 0.3.0-beta.2

An interface to find and format data from BRK
Documentation
use brk_error::{OptionData, Result};
use brk_types::{BlockHash, BlockStatus, Height};
use vecdb::AnyVec;

use crate::Query;

impl Query {
    pub fn block_status(&self, hash: &BlockHash) -> Result<BlockStatus> {
        let height = self.height_by_hash(hash)?;
        self.block_status_by_height(height)
    }

    fn block_status_by_height(&self, height: Height) -> Result<BlockStatus> {
        let indexer = self.indexer();

        let max_height = Height::from(indexer.vecs.blocks.blockhash.len().saturating_sub(1));

        if height > max_height {
            return Ok(BlockStatus::not_in_best_chain());
        }

        let next_best = if height < max_height {
            Some(
                indexer
                    .vecs
                    .blocks
                    .blockhash
                    .get(height.incremented())
                    .data()?,
            )
        } else {
            None
        };

        Ok(BlockStatus::in_best_chain(height, next_best))
    }
}