use crate::merkle_util::merkle_tree_root;
use crate::primitives::Bytes32;
use crate::types::checkpoint::Checkpoint;
pub struct CheckpointBuilder {
epoch: u64,
prev_checkpoint: Bytes32,
state_root: Bytes32,
block_hashes: Vec<Bytes32>,
tx_count: u64,
total_fees: u64,
withdrawal_hashes: Vec<Bytes32>,
}
impl CheckpointBuilder {
#[must_use]
pub fn new(epoch: u64, prev_checkpoint: Bytes32) -> Self {
Self {
epoch,
prev_checkpoint,
state_root: Bytes32::default(),
block_hashes: Vec::new(),
tx_count: 0,
total_fees: 0,
withdrawal_hashes: Vec::new(),
}
}
pub fn add_block(&mut self, block_hash: Bytes32, tx_count: u64, fees: u64) {
self.block_hashes.push(block_hash);
self.tx_count += tx_count;
self.total_fees += fees;
}
pub fn set_state_root(&mut self, state_root: Bytes32) {
self.state_root = state_root;
}
pub fn add_withdrawal(&mut self, withdrawal_hash: Bytes32) {
self.withdrawal_hashes.push(withdrawal_hash);
}
#[must_use]
pub fn build(self) -> Checkpoint {
let block_root = merkle_tree_root(&self.block_hashes);
let withdrawals_root = merkle_tree_root(&self.withdrawal_hashes);
Checkpoint {
epoch: self.epoch,
state_root: self.state_root,
block_root,
block_count: self.block_hashes.len() as u32,
tx_count: self.tx_count,
total_fees: self.total_fees,
prev_checkpoint: self.prev_checkpoint,
withdrawals_root,
withdrawal_count: self.withdrawal_hashes.len() as u32,
}
}
}