use std::hash::{DefaultHasher, Hash, Hasher};
use brk_types::RecommendedFees;
use super::{
super::block_builder::Package,
fees,
stats::{self, BlockStats},
};
use crate::stores::{Entry, TxIndex};
#[derive(Debug, Clone, Default)]
pub struct Snapshot {
pub blocks: Vec<Vec<TxIndex>>,
pub block_stats: Vec<BlockStats>,
pub fees: RecommendedFees,
pub next_block_hash: u64,
}
impl Snapshot {
pub fn build(blocks: Vec<Vec<Package>>, entries: &[Option<Entry>]) -> Self {
let block_stats: Vec<BlockStats> = blocks
.iter()
.map(|block| stats::compute_block_stats(block, entries))
.collect();
let fees = fees::compute_recommended_fees(&block_stats);
let blocks: Vec<Vec<TxIndex>> = blocks
.into_iter()
.map(|block| block.into_iter().flat_map(|pkg| pkg.txs).collect())
.collect();
let next_block_hash = Self::hash_next_block(&blocks);
Self {
blocks,
block_stats,
fees,
next_block_hash,
}
}
fn hash_next_block(blocks: &[Vec<TxIndex>]) -> u64 {
let Some(block) = blocks.first() else {
return 0;
};
let mut hasher = DefaultHasher::new();
block.hash(&mut hasher);
hasher.finish()
}
}