Skip to content

Commit c993106

Browse files
committed
refactor(chain): LocalChain takes a generic
1 parent cd2cd40 commit c993106

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

crates/chain/src/local_chain.rs

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ fn apply_changeset_to_checkpoint(
2222

2323
for cp in init_cp.iter() {
2424
if cp.height() >= start_height {
25-
extension.insert(cp.height(), cp.hash());
25+
extension.insert(cp.height(), *cp.data());
2626
} else {
2727
base = Some(cp);
2828
break;
2929
}
3030
}
3131

32-
for (&height, &hash) in &changeset.blocks {
33-
match hash {
34-
Some(hash) => {
35-
extension.insert(height, hash);
32+
for (&height, &data) in &changeset.blocks {
33+
match data {
34+
Some(data) => {
35+
extension.insert(height, data);
3636
}
3737
None => {
3838
extension.remove(&height);
@@ -42,7 +42,7 @@ fn apply_changeset_to_checkpoint(
4242

4343
let new_tip = match base {
4444
Some(base) => base
45-
.extend(extension.into_iter().map(BlockId::from))
45+
.extend_data(extension)
4646
.expect("extension is strictly greater than base"),
4747
None => LocalChain::from_blocks(extension)?.tip(),
4848
};
@@ -53,12 +53,24 @@ fn apply_changeset_to_checkpoint(
5353
}
5454

5555
/// This is a local implementation of [`ChainOracle`].
56-
#[derive(Debug, Clone, PartialEq)]
57-
pub struct LocalChain {
58-
tip: CheckPoint,
56+
#[derive(Debug, Clone)]
57+
pub struct LocalChain<B = BlockHash> {
58+
tip: CheckPoint<B>,
5959
}
6060

61-
impl ChainOracle for LocalChain {
61+
impl<B> PartialEq for LocalChain<B>
62+
where
63+
B: Copy + core::cmp::PartialEq,
64+
{
65+
fn eq(&self, other: &Self) -> bool {
66+
self.tip == other.tip
67+
}
68+
}
69+
70+
impl<B> ChainOracle for LocalChain<B>
71+
where
72+
B: Copy,
73+
{
6274
type Error = Infallible;
6375

6476
fn is_block_in_chain(
@@ -83,18 +95,18 @@ impl ChainOracle for LocalChain {
8395
}
8496
}
8597

86-
impl LocalChain {
98+
impl LocalChain<BlockHash> {
8799
/// Get the genesis hash.
88100
pub fn genesis_hash(&self) -> BlockHash {
89-
self.tip.get(0).expect("genesis must exist").hash()
101+
self.tip.get(0).expect("genesis must exist").block_id().hash
90102
}
91103

92104
/// Construct [`LocalChain`] from genesis `hash`.
93105
#[must_use]
94106
pub fn from_genesis_hash(hash: BlockHash) -> (Self, ChangeSet) {
95107
let height = 0;
96108
let chain = Self {
97-
tip: CheckPoint::new(BlockId { height, hash }),
109+
tip: CheckPoint::from_data(height, hash),
98110
};
99111
let changeset = chain.initial_changeset();
100112
(chain, changeset)
@@ -134,7 +146,7 @@ impl LocalChain {
134146
return Err(MissingGenesisError);
135147
}
136148

137-
let mut tip: Option<CheckPoint> = None;
149+
let mut tip: Option<CheckPoint<BlockHash>> = None;
138150
for block in &blocks {
139151
match tip {
140152
Some(curr) => {
@@ -153,7 +165,7 @@ impl LocalChain {
153165
}
154166

155167
/// Get the highest checkpoint.
156-
pub fn tip(&self) -> CheckPoint {
168+
pub fn tip(&self) -> CheckPoint<BlockHash> {
157169
self.tip.clone()
158170
}
159171

@@ -405,17 +417,25 @@ impl LocalChain {
405417
}
406418

407419
/// The [`ChangeSet`] represents changes to [`LocalChain`].
408-
#[derive(Debug, Default, Clone, PartialEq)]
420+
#[derive(Debug, Clone, PartialEq)]
409421
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
410-
pub struct ChangeSet {
422+
pub struct ChangeSet<B = BlockHash> {
411423
/// Changes to the [`LocalChain`] blocks.
412424
///
413425
/// The key represents the block height, and the value either represents added a new [`CheckPoint`]
414426
/// (if [`Some`]), or removing a [`CheckPoint`] (if [`None`]).
415-
pub blocks: BTreeMap<u32, Option<BlockHash>>,
427+
pub blocks: BTreeMap<u32, Option<B>>,
428+
}
429+
430+
impl<B> Default for ChangeSet<B> {
431+
fn default() -> Self {
432+
ChangeSet {
433+
blocks: BTreeMap::default(),
434+
}
435+
}
416436
}
417437

418-
impl Merge for ChangeSet {
438+
impl<B> Merge for ChangeSet<B> {
419439
fn merge(&mut self, other: Self) {
420440
Merge::merge(&mut self.blocks, other.blocks)
421441
}
@@ -425,24 +445,24 @@ impl Merge for ChangeSet {
425445
}
426446
}
427447

428-
impl<B: IntoIterator<Item = (u32, Option<BlockHash>)>> From<B> for ChangeSet {
429-
fn from(blocks: B) -> Self {
448+
impl<B, I: IntoIterator<Item = (u32, Option<B>)>> From<I> for ChangeSet<B> {
449+
fn from(blocks: I) -> Self {
430450
Self {
431451
blocks: blocks.into_iter().collect(),
432452
}
433453
}
434454
}
435455

436-
impl FromIterator<(u32, Option<BlockHash>)> for ChangeSet {
437-
fn from_iter<T: IntoIterator<Item = (u32, Option<BlockHash>)>>(iter: T) -> Self {
456+
impl<B> FromIterator<(u32, Option<B>)> for ChangeSet<B> {
457+
fn from_iter<T: IntoIterator<Item = (u32, Option<B>)>>(iter: T) -> Self {
438458
Self {
439459
blocks: iter.into_iter().collect(),
440460
}
441461
}
442462
}
443463

444-
impl FromIterator<(u32, BlockHash)> for ChangeSet {
445-
fn from_iter<T: IntoIterator<Item = (u32, BlockHash)>>(iter: T) -> Self {
464+
impl<B> FromIterator<(u32, B)> for ChangeSet<B> {
465+
fn from_iter<T: IntoIterator<Item = (u32, B)>>(iter: T) -> Self {
446466
Self {
447467
blocks: iter
448468
.into_iter()

0 commit comments

Comments
 (0)