diff --git a/crates/chain/src/local_chain.rs b/crates/chain/src/local_chain.rs index e16131db3..af7950064 100644 --- a/crates/chain/src/local_chain.rs +++ b/crates/chain/src/local_chain.rs @@ -22,17 +22,17 @@ fn apply_changeset_to_checkpoint( for cp in init_cp.iter() { if cp.height() >= start_height { - extension.insert(cp.height(), cp.hash()); + extension.insert(cp.height(), *cp.data()); } else { base = Some(cp); break; } } - for (&height, &hash) in &changeset.blocks { - match hash { - Some(hash) => { - extension.insert(height, hash); + for (&height, &data) in &changeset.blocks { + match data { + Some(data) => { + extension.insert(height, data); } None => { extension.remove(&height); @@ -42,7 +42,7 @@ fn apply_changeset_to_checkpoint( let new_tip = match base { Some(base) => base - .extend(extension.into_iter().map(BlockId::from)) + .extend_data(extension) .expect("extension is strictly greater than base"), None => LocalChain::from_blocks(extension)?.tip(), }; @@ -53,12 +53,24 @@ fn apply_changeset_to_checkpoint( } /// This is a local implementation of [`ChainOracle`]. -#[derive(Debug, Clone, PartialEq)] -pub struct LocalChain { - tip: CheckPoint, +#[derive(Debug, Clone)] +pub struct LocalChain { + tip: CheckPoint, } -impl ChainOracle for LocalChain { +impl PartialEq for LocalChain +where + B: Copy + core::cmp::PartialEq, +{ + fn eq(&self, other: &Self) -> bool { + self.tip == other.tip + } +} + +impl ChainOracle for LocalChain +where + B: Copy, +{ type Error = Infallible; fn is_block_in_chain( @@ -83,10 +95,10 @@ impl ChainOracle for LocalChain { } } -impl LocalChain { +impl LocalChain { /// Get the genesis hash. pub fn genesis_hash(&self) -> BlockHash { - self.tip.get(0).expect("genesis must exist").hash() + self.tip.get(0).expect("genesis must exist").block_id().hash } /// Construct [`LocalChain`] from genesis `hash`. @@ -94,7 +106,7 @@ impl LocalChain { pub fn from_genesis_hash(hash: BlockHash) -> (Self, ChangeSet) { let height = 0; let chain = Self { - tip: CheckPoint::new(BlockId { height, hash }), + tip: CheckPoint::from_data(height, hash), }; let changeset = chain.initial_changeset(); (chain, changeset) @@ -134,7 +146,7 @@ impl LocalChain { return Err(MissingGenesisError); } - let mut tip: Option = None; + let mut tip: Option> = None; for block in &blocks { match tip { Some(curr) => { @@ -153,7 +165,7 @@ impl LocalChain { } /// Get the highest checkpoint. - pub fn tip(&self) -> CheckPoint { + pub fn tip(&self) -> CheckPoint { self.tip.clone() } @@ -405,17 +417,25 @@ impl LocalChain { } /// The [`ChangeSet`] represents changes to [`LocalChain`]. -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -pub struct ChangeSet { +pub struct ChangeSet { /// Changes to the [`LocalChain`] blocks. /// /// The key represents the block height, and the value either represents added a new [`CheckPoint`] /// (if [`Some`]), or removing a [`CheckPoint`] (if [`None`]). - pub blocks: BTreeMap>, + pub blocks: BTreeMap>, +} + +impl Default for ChangeSet { + fn default() -> Self { + ChangeSet { + blocks: BTreeMap::default(), + } + } } -impl Merge for ChangeSet { +impl Merge for ChangeSet { fn merge(&mut self, other: Self) { Merge::merge(&mut self.blocks, other.blocks) } @@ -425,24 +445,24 @@ impl Merge for ChangeSet { } } -impl)>> From for ChangeSet { - fn from(blocks: B) -> Self { +impl)>> From for ChangeSet { + fn from(blocks: I) -> Self { Self { blocks: blocks.into_iter().collect(), } } } -impl FromIterator<(u32, Option)> for ChangeSet { - fn from_iter)>>(iter: T) -> Self { +impl FromIterator<(u32, Option)> for ChangeSet { + fn from_iter)>>(iter: T) -> Self { Self { blocks: iter.into_iter().collect(), } } } -impl FromIterator<(u32, BlockHash)> for ChangeSet { - fn from_iter>(iter: T) -> Self { +impl FromIterator<(u32, B)> for ChangeSet { + fn from_iter>(iter: T) -> Self { Self { blocks: iter .into_iter()