Skip to content

Commit 2d1d95a

Browse files
committed
feat(chain): impl PartialEq on CheckPoint
We impl `PartialEq` on `CheckPoint` instead of directly on `LocalChain`. We also made the implementation more efficient.
1 parent 9a62d56 commit 2d1d95a

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

crates/chain/src/local_chain.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::ops::RangeBounds;
66
use crate::collections::BTreeMap;
77
use crate::{BlockId, ChainOracle};
88
use alloc::sync::Arc;
9-
use alloc::vec::Vec;
109
use bitcoin::block::Header;
1110
use bitcoin::BlockHash;
1211

@@ -36,6 +35,14 @@ struct CPInner {
3635
prev: Option<Arc<CPInner>>,
3736
}
3837

38+
impl PartialEq for CheckPoint {
39+
fn eq(&self, other: &Self) -> bool {
40+
let self_cps = self.iter().map(|cp| cp.block_id());
41+
let other_cps = other.iter().map(|cp| cp.block_id());
42+
self_cps.eq(other_cps)
43+
}
44+
}
45+
3946
impl CheckPoint {
4047
/// Construct a new base block at the front of a linked list.
4148
pub fn new(block: BlockId) -> Self {
@@ -220,7 +227,7 @@ impl IntoIterator for CheckPoint {
220227
/// Script-pubkey based syncing mechanisms may not introduce transactions in a chronological order
221228
/// so some updates require introducing older blocks (to anchor older transactions). For
222229
/// script-pubkey based syncing, `introduce_older_blocks` would typically be `true`.
223-
#[derive(Debug, Clone)]
230+
#[derive(Debug, Clone, PartialEq)]
224231
pub struct Update {
225232
/// The update chain's new tip.
226233
pub tip: CheckPoint,
@@ -234,23 +241,11 @@ pub struct Update {
234241
}
235242

236243
/// This is a local implementation of [`ChainOracle`].
237-
#[derive(Debug, Clone)]
244+
#[derive(Debug, Clone, PartialEq)]
238245
pub struct LocalChain {
239246
tip: CheckPoint,
240247
}
241248

242-
impl PartialEq for LocalChain {
243-
fn eq(&self, other: &Self) -> bool {
244-
self.iter_checkpoints()
245-
.map(|cp| cp.block_id())
246-
.collect::<Vec<_>>()
247-
== other
248-
.iter_checkpoints()
249-
.map(|cp| cp.block_id())
250-
.collect::<Vec<_>>()
251-
}
252-
}
253-
254249
// TODO: Figure out whether we can get rid of this
255250
impl From<LocalChain> for BTreeMap<u32, BlockHash> {
256251
fn from(value: LocalChain) -> Self {

0 commit comments

Comments
 (0)