Refactor debug_assertions checks for LocalChain

This commit is contained in:
志宇 2023-07-22 14:43:19 +08:00
parent 315e7e0b4b
commit 8bf7a997f7
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8

View File

@ -191,8 +191,8 @@ impl LocalChain {
let mut chain = Self::default(); let mut chain = Self::default();
chain.apply_changeset(&changeset); chain.apply_changeset(&changeset);
#[cfg(debug_assertions)] debug_assert!(chain._check_index_is_consistent_with_tip());
chain._check_consistency(Some(&changeset)); debug_assert!(chain._check_changeset_is_applied(&changeset));
chain chain
} }
@ -204,10 +204,7 @@ impl LocalChain {
..Default::default() ..Default::default()
}; };
_self.reindex(0); _self.reindex(0);
debug_assert!(_self._check_index_is_consistent_with_tip());
#[cfg(debug_assertions)]
_self._check_consistency(None);
_self _self
} }
@ -232,8 +229,7 @@ impl LocalChain {
let chain = Self { index: blocks, tip }; let chain = Self { index: blocks, tip };
#[cfg(debug_assertions)] debug_assert!(chain._check_index_is_consistent_with_tip());
chain._check_consistency(None);
chain chain
} }
@ -299,8 +295,8 @@ impl LocalChain {
*self = Self::from_tip(update.tip); *self = Self::from_tip(update.tip);
let changeset = self.initial_changeset(); let changeset = self.initial_changeset();
#[cfg(debug_assertions)] debug_assert!(self._check_index_is_consistent_with_tip());
self._check_consistency(Some(&changeset)); debug_assert!(self._check_changeset_is_applied(&changeset));
Ok(changeset) Ok(changeset)
} }
} }
@ -340,8 +336,8 @@ impl LocalChain {
self.tip = new_tip; self.tip = new_tip;
self.reindex(start_height); self.reindex(start_height);
#[cfg(debug_assertions)] debug_assert!(self._check_index_is_consistent_with_tip());
self._check_consistency(Some(changeset)); debug_assert!(self._check_changeset_is_applied(changeset));
} }
} }
@ -398,31 +394,23 @@ impl LocalChain {
&self.index &self.index
} }
/// Checkpoints that exist under `self.tip` and blocks indexed in `self.index` should be equal. fn _check_index_is_consistent_with_tip(&self) -> bool {
/// Additionally, if a `changeset` is provided, the changes specified in the `changeset` should let tip_history = self
/// be reflected in `self.index`. .tip
#[cfg(debug_assertions)]
fn _check_consistency(&self, changeset: Option<&ChangeSet>) {
debug_assert_eq!(
self.tip
.iter() .iter()
.flat_map(CheckPoint::iter) .flat_map(CheckPoint::iter)
.map(|cp| (cp.height(), cp.hash())) .map(|cp| (cp.height(), cp.hash()))
.collect::<BTreeMap<_, _>>(), .collect::<BTreeMap<_, _>>();
self.index, self.index == tip_history
"checkpoint history and index must be consistent" }
);
if let Some(changeset) = changeset { fn _check_changeset_is_applied(&self, changeset: &ChangeSet) -> bool {
for (height, exp_hash) in changeset { for (height, exp_hash) in changeset {
let hash = self.index.get(height); if self.index.get(height) != exp_hash.as_ref() {
assert_eq!( return false;
hash,
exp_hash.as_ref(),
"changeset changes should be reflected in the internal index"
);
} }
} }
true
} }
} }