use datasize::DataSize;
use super::State;
use crate::components::consensus::traits::Context;
#[derive(Clone, DataSize, Debug, Eq, PartialEq)]
pub(crate) struct Block<C>
where
C: Context,
{
pub(crate) height: u64,
pub(crate) value: C::ConsensusValue,
pub(crate) skip_idx: Vec<C::Hash>,
}
impl<C: Context> Block<C> {
pub(crate) fn new(
parent_hash: Option<C::Hash>,
value: C::ConsensusValue,
state: &State<C>,
) -> Block<C> {
let (parent, mut skip_idx) = match parent_hash {
None => return Block::initial(value),
Some(hash) => (state.block(&hash), vec![hash]),
};
#[allow(clippy::integer_arithmetic)]
let height = parent.height + 1;
for i in 0..height.trailing_zeros() as usize {
let ancestor = state.block(&skip_idx[i]);
skip_idx.push(ancestor.skip_idx[i]);
}
Block {
height,
value,
skip_idx,
}
}
pub(crate) fn parent(&self) -> Option<&C::Hash> {
self.skip_idx.first()
}
fn initial(value: C::ConsensusValue) -> Block<C> {
Block {
height: 0,
value,
skip_idx: vec![],
}
}
}