Apply suggestions from code review & fix clippy complaints.

This commit is contained in:
Kris Nuttycombe 2021-12-09 16:28:59 -07:00
parent e4573b81c1
commit 0a8854f1b0
1 changed files with 28 additions and 33 deletions

View File

@ -118,7 +118,7 @@ impl<H: Hashable + Clone> NonEmptyFrontier<H> {
self.leaf = Leaf::Right(a.clone(), value); self.leaf = Leaf::Right(a.clone(), value);
} }
Leaf::Right(a, b) => { Leaf::Right(a, b) => {
carry = Some((H::combine(Altitude::zero(), &a, &b), Altitude::one())); carry = Some((H::combine(Altitude::zero(), a, b), Altitude::one()));
self.leaf = Leaf::Left(value); self.leaf = Leaf::Left(value);
} }
}; };
@ -128,7 +128,7 @@ impl<H: Hashable + Clone> NonEmptyFrontier<H> {
for (ommer, ommer_lvl) in self.ommers.iter().zip(self.position.ommer_altitudes()) { for (ommer, ommer_lvl) in self.ommers.iter().zip(self.position.ommer_altitudes()) {
if let Some((carry_ommer, carry_lvl)) = carry.as_ref() { if let Some((carry_ommer, carry_lvl)) = carry.as_ref() {
if *carry_lvl == ommer_lvl { if *carry_lvl == ommer_lvl {
carry = Some((H::combine(ommer_lvl, &ommer, &carry_ommer), ommer_lvl + 1)) carry = Some((H::combine(ommer_lvl, ommer, carry_ommer), ommer_lvl + 1))
} else { } else {
// insert the carry at the first empty slot; then the rest of the // insert the carry at the first empty slot; then the rest of the
// ommers will remain unchanged // ommers will remain unchanged
@ -225,7 +225,7 @@ impl<H: Hashable + Clone> NonEmptyFrontier<H> {
digest = H::combine( digest = H::combine(
ommer_lvl, ommer_lvl,
&ommer, ommer,
// fold up to ommer.lvl pairing with empty roots; if // fold up to ommer.lvl pairing with empty roots; if
// complete_lvl == ommer.lvl this is just the complete // complete_lvl == ommer.lvl this is just the complete
// digest to this point // digest to this point
@ -560,7 +560,7 @@ impl<H: Hashable + Clone + PartialEq> MerkleBridge<H> {
/// `self` had had every leaf used to construct `next` appended to it /// `self` had had every leaf used to construct `next` appended to it
/// directly. /// directly.
fn fuse(&self, next: &Self) -> Option<MerkleBridge<H>> { fn fuse(&self, next: &Self) -> Option<MerkleBridge<H>> {
if next.can_follow(&self) { if next.can_follow(self) {
let fused = MerkleBridge { let fused = MerkleBridge {
prior_position: self.prior_position, prior_position: self.prior_position,
auth_fragments: self auth_fragments: self
@ -763,37 +763,32 @@ impl<H: Hashable + Hash + Eq + Clone, const DEPTH: u8> BridgeTree<H, DEPTH> {
} }
fn witness_internal(&mut self, btype: BoundaryType) -> Option<usize> { fn witness_internal(&mut self, btype: BoundaryType) -> Option<usize> {
let next = self.bridges.last().map(|current| { let blen = self.bridges.len();
( let (leaf, succ) = self
current.leaf_value().clone(), .bridges
current.successor(self.bridges.len() - 1), .last()
) .map(|current| (current.leaf_value().clone(), current.successor(blen - 1)))?;
});
next.map(|(leaf, succ)| { // a duplicate frontier might occur when we observe a previously witnessed
let blen = self.bridges.len(); // value where that value was subsequently removed.
let is_duplicate_frontier =
blen > 1 && self.bridges[blen - 1].frontier == self.bridges[blen - 2].frontier;
// a duplicate frontier might occur when we observe a previously witnessed let save_idx = if is_duplicate_frontier {
// value where that value was subsequently removed. // By saving at `blen - 2` we effectively restore the original witness.
let is_duplicate_frontier = // We do not push new duplicate frontiers.
blen > 1 && self.bridges[blen - 1].frontier == self.bridges[blen - 2].frontier; blen - 2
} else {
// only push the successor if its frontier is not a duplicate
self.bridges.push(succ);
blen - 1
};
let save_idx = if is_duplicate_frontier { if btype == BoundaryType::Witness {
// By saving at `blen - 2` we effectively restore the original witness. self.saved.entry(leaf).or_insert(save_idx);
// We do not push new duplicate frontiers. }
blen - 2
} else {
// only push the successor if its frontier is not a duplicate
self.bridges.push(succ);
blen - 1
};
if btype == BoundaryType::Witness { Some(save_idx)
self.saved.entry(leaf).or_insert(save_idx);
}
save_idx
})
} }
} }
@ -913,7 +908,7 @@ impl<H: Hashable + Hash + Eq + Clone, const DEPTH: u8> Tree<H> for BridgeTree<H,
fn checkpoint(&mut self) { fn checkpoint(&mut self) {
let new_checkpoint = self let new_checkpoint = self
.witness_internal(BoundaryType::Checkpoint) .witness_internal(BoundaryType::Checkpoint)
.map_or(Checkpoint::Empty, |save_idx| Checkpoint::AtIndex(save_idx)); .map_or(Checkpoint::Empty, Checkpoint::AtIndex);
self.checkpoints.push(new_checkpoint); self.checkpoints.push(new_checkpoint);
if self.checkpoints.len() > self.max_checkpoints { if self.checkpoints.len() > self.max_checkpoints {
@ -1038,7 +1033,7 @@ impl<H: Hashable + Clone + PartialEq, const DEPTH: u8> Recording<H> for BridgeRe
fn play(&mut self, recording: &Self) -> bool { fn play(&mut self, recording: &Self) -> bool {
if let Some((current, next)) = self.bridge.as_ref().zip(recording.bridge.as_ref()) { if let Some((current, next)) = self.bridge.as_ref().zip(recording.bridge.as_ref()) {
if let Some(fused) = current.fuse(&next) { if let Some(fused) = current.fuse(next) {
self.bridge = Some(fused); self.bridge = Some(fused);
true true
} else { } else {