mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-28 16:49:58 -07:00
38 lines
1004 B
Rust
38 lines
1004 B
Rust
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))
|
|
}
|
|
}
|