Skip to content

Commit 8bf7a99

Browse files
committed
Refactor debug_assertions checks for LocalChain
1 parent 315e7e0 commit 8bf7a99

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

crates/chain/src/local_chain.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ impl LocalChain {
191191
let mut chain = Self::default();
192192
chain.apply_changeset(&changeset);
193193

194-
#[cfg(debug_assertions)]
195-
chain._check_consistency(Some(&changeset));
194+
debug_assert!(chain._check_index_is_consistent_with_tip());
195+
debug_assert!(chain._check_changeset_is_applied(&changeset));
196196

197197
chain
198198
}
@@ -204,10 +204,7 @@ impl LocalChain {
204204
..Default::default()
205205
};
206206
_self.reindex(0);
207-
208-
#[cfg(debug_assertions)]
209-
_self._check_consistency(None);
210-
207+
debug_assert!(_self._check_index_is_consistent_with_tip());
211208
_self
212209
}
213210

@@ -232,8 +229,7 @@ impl LocalChain {
232229

233230
let chain = Self { index: blocks, tip };
234231

235-
#[cfg(debug_assertions)]
236-
chain._check_consistency(None);
232+
debug_assert!(chain._check_index_is_consistent_with_tip());
237233

238234
chain
239235
}
@@ -299,8 +295,8 @@ impl LocalChain {
299295
*self = Self::from_tip(update.tip);
300296
let changeset = self.initial_changeset();
301297

302-
#[cfg(debug_assertions)]
303-
self._check_consistency(Some(&changeset));
298+
debug_assert!(self._check_index_is_consistent_with_tip());
299+
debug_assert!(self._check_changeset_is_applied(&changeset));
304300
Ok(changeset)
305301
}
306302
}
@@ -340,8 +336,8 @@ impl LocalChain {
340336
self.tip = new_tip;
341337
self.reindex(start_height);
342338

343-
#[cfg(debug_assertions)]
344-
self._check_consistency(Some(changeset));
339+
debug_assert!(self._check_index_is_consistent_with_tip());
340+
debug_assert!(self._check_changeset_is_applied(changeset));
345341
}
346342
}
347343

@@ -398,31 +394,23 @@ impl LocalChain {
398394
&self.index
399395
}
400396

401-
/// Checkpoints that exist under `self.tip` and blocks indexed in `self.index` should be equal.
402-
/// Additionally, if a `changeset` is provided, the changes specified in the `changeset` should
403-
/// be reflected in `self.index`.
404-
#[cfg(debug_assertions)]
405-
fn _check_consistency(&self, changeset: Option<&ChangeSet>) {
406-
debug_assert_eq!(
407-
self.tip
408-
.iter()
409-
.flat_map(CheckPoint::iter)
410-
.map(|cp| (cp.height(), cp.hash()))
411-
.collect::<BTreeMap<_, _>>(),
412-
self.index,
413-
"checkpoint history and index must be consistent"
414-
);
415-
416-
if let Some(changeset) = changeset {
417-
for (height, exp_hash) in changeset {
418-
let hash = self.index.get(height);
419-
assert_eq!(
420-
hash,
421-
exp_hash.as_ref(),
422-
"changeset changes should be reflected in the internal index"
423-
);
397+
fn _check_index_is_consistent_with_tip(&self) -> bool {
398+
let tip_history = self
399+
.tip
400+
.iter()
401+
.flat_map(CheckPoint::iter)
402+
.map(|cp| (cp.height(), cp.hash()))
403+
.collect::<BTreeMap<_, _>>();
404+
self.index == tip_history
405+
}
406+
407+
fn _check_changeset_is_applied(&self, changeset: &ChangeSet) -> bool {
408+
for (height, exp_hash) in changeset {
409+
if self.index.get(height) != exp_hash.as_ref() {
410+
return false;
424411
}
425412
}
413+
true
426414
}
427415
}
428416

0 commit comments

Comments
 (0)