From ee93cb8598c6efb30ef31b79c0fd363238d535a1 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Thu, 21 Jul 2022 18:12:39 -0600 Subject: [PATCH] Rename witness->mark --- src/bridgetree.rs | 119 ++++++++++---------- src/lib.rs | 274 +++++++++++++++++++++++----------------------- src/sample.rs | 46 ++++---- 3 files changed, 216 insertions(+), 223 deletions(-) diff --git a/src/bridgetree.rs b/src/bridgetree.rs index 545d1189..dc059d0b 100644 --- a/src/bridgetree.rs +++ b/src/bridgetree.rs @@ -20,7 +20,7 @@ pub enum FrontierError { #[derive(Clone, Debug, PartialEq, Eq)] pub enum PathError { - PositionNotWitnessed(Position), + PositionNotMarked(Position), BridgeFusionError, FrontierAddressInvalid(Address), BridgeAddressInvalid(Address), @@ -348,11 +348,11 @@ impl MerkleBridge { } impl<'a, H: Hashable + Ord + Clone + 'a> MerkleBridge { - /// Constructs a new bridge to follow this one. If witness_current_leaf is true, the successor + /// Constructs a new bridge to follow this one. If mark_current_leaf is true, the successor /// will track the information necessary to create an authentication path for the leaf most /// recently appended to this bridge's frontier. #[must_use] - pub fn successor(&self, witness_current_leaf: bool) -> Self { + pub fn successor(&self, mark_current_leaf: bool) -> Self { let mut result = Self { prior_position: Some(self.frontier.position()), tracking: self.tracking.clone(), @@ -360,7 +360,7 @@ impl<'a, H: Hashable + Ord + Clone + 'a> MerkleBridge { frontier: self.frontier.clone(), }; - if witness_current_leaf { + if mark_current_leaf { result.track_current_leaf(); } @@ -479,14 +479,14 @@ pub struct Checkpoint { /// The number of bridges that will be retained in a rewind. bridges_len: usize, /// A flag indicating whether or not the current state of the tree - /// had been witnessed at the time the checkpoint was created. - is_witnessed: bool, - /// A set of the positions that have been witnessed during the period that this + /// had been marked at the time the checkpoint was created. + is_marked: bool, + /// A set of the positions that have been marked during the period that this /// checkpoint is the current checkpoint. - witnessed: BTreeSet, + marked: BTreeSet, /// When a mark is forgotten, if the index of the forgotten mark is <= bridge_idx we /// record it in the current checkpoint so that on rollback, we restore the forgotten - /// witnesses to the BridgeTree's "saved" list. If the mark was newly created since the + /// marks to the BridgeTree's "saved" list. If the mark was newly created since the /// checkpoint, we don't need to remember when we forget it because both the mark /// creation and removal will be reverted in the rollback. forgotten: BTreeMap, @@ -496,24 +496,24 @@ impl Checkpoint { /// Creates a new checkpoint from its constituent parts. pub fn from_parts( bridges_len: usize, - is_witnessed: bool, - witnessed: BTreeSet, + is_marked: bool, + marked: BTreeSet, forgotten: BTreeMap, ) -> Self { Self { bridges_len, - is_witnessed, - witnessed, + is_marked, + marked, forgotten, } } /// Creates a new empty checkpoint for the specified [`BridgeTree`] state. - pub fn at_length(bridges_len: usize, is_witnessed: bool) -> Self { + pub fn at_length(bridges_len: usize, is_marked: bool) -> Self { Checkpoint { bridges_len, - is_witnessed, - witnessed: BTreeSet::new(), + is_marked, + marked: BTreeSet::new(), forgotten: BTreeMap::new(), } } @@ -527,22 +527,22 @@ impl Checkpoint { self.bridges_len } - /// Returns whether the current state of the tree had been witnessed at the point that + /// Returns whether the current state of the tree had been marked at the point that /// this checkpoint was made. /// /// In the event of a rewind, the rewind logic will ensure that mark information is /// properly reconstituted for the checkpointed tree state. - pub fn is_witnessed(&self) -> bool { - self.is_witnessed + pub fn is_marked(&self) -> bool { + self.is_marked } - /// Returns a set of the positions that have been witnessed during the period that this + /// Returns a set of the positions that have been marked during the period that this /// checkpoint is the current checkpoint. - pub fn witnessed(&self) -> &BTreeSet { - &self.witnessed + pub fn marked(&self) -> &BTreeSet { + &self.marked } - /// Returns the set of previously-witnessed positions that have had their witnesses removed + /// Returns the set of previously-marked positions that have had their marks removed /// during the period that this checkpoint is the current checkpoint. pub fn forgotten(&self) -> &BTreeMap { &self.forgotten @@ -615,7 +615,7 @@ impl Debug for BridgeTree #[derive(Debug, Clone, PartialEq, Eq)] pub enum BridgeTreeError { IncorrectIncompleteIndex, - InvalidWitnessIndex(usize), + InvalidMarkIndex(usize), PositionMismatch { expected: Position, found: Position }, InvalidSavePoints, ContinuityError, @@ -644,7 +644,7 @@ impl BridgeTree { &self.current_bridge } - pub fn witnessed_indices(&self) -> &BTreeMap { + pub fn marked_indices(&self) -> &BTreeMap { &self.saved } @@ -686,7 +686,7 @@ impl BridgeTree { // check that saved values correspond to bridges for (pos, i) in saved { if i >= &prior_bridges.len() { - return Err(BridgeTreeError::InvalidWitnessIndex(*i)); + return Err(BridgeTreeError::InvalidMarkIndex(*i)); } let found = prior_bridges[*i].position(); if &found != pos { @@ -816,17 +816,17 @@ impl BridgeTree { // so look for it in each of the subsequent checkpoints' forgotten // items. self.checkpoints[i..].iter().find_map(|c| { - // restore the forgotten position, if that position was not also witnessed + // restore the forgotten position, if that position was not also marked // in the same checkpoint c.forgotten .get(&position) - .filter(|_| !c.witnessed.contains(&position)) + .filter(|_| !c.marked.contains(&position)) }) } else { None } }) - .ok_or(PathError::PositionNotWitnessed(position))?; + .ok_or(PathError::PositionNotMarked(position))?; let prior_frontier = &self.prior_bridges[*saved_idx].frontier; @@ -908,7 +908,7 @@ impl Tree for BridgeTree Option { + fn mark(&mut self) -> Option { match self.current_bridge.take() { Some(mut cur_b) => { cur_b.track_current_leaf(); @@ -922,7 +922,7 @@ impl Tree for BridgeTree Tree for BridgeTree Tree for BridgeTree BTreeSet { + fn marked_positions(&self) -> BTreeSet { self.saved.keys().cloned().collect() } - fn get_witnessed_leaf(&self, position: Position) -> Option<&H> { + fn get_marked_leaf(&self, position: Position) -> Option<&H> { self.saved .get(&position) .and_then(|idx| self.prior_bridges.get(*idx).map(|b| b.current_leaf())) } - fn remove_witness(&mut self, position: Position) -> bool { + fn remove_mark(&mut self, position: Position) -> bool { if let Some(idx) = self.saved.remove(&position) { - // If the position is one that has *not* just been witnessed since the last checkpoint, + // If the position is one that has *not* just been marked since the last checkpoint, // then add it to the set of those forgotten during the current checkpoint span so that // it can be restored on rollback. if let Some(c) = self.checkpoints.last_mut() { - if !c.witnessed.contains(&position) { + if !c.marked.contains(&position) { c.forgotten.insert(position, idx); } } @@ -976,7 +976,7 @@ impl Tree for BridgeTree { - let is_witnessed = self.get_witnessed_leaf(cur_b.position()).is_some(); + let is_marked = self.get_marked_leaf(cur_b.position()).is_some(); // Do not create a duplicate bridge if self @@ -990,10 +990,8 @@ impl Tree for BridgeTree { self.checkpoints.push(Checkpoint::at_length(0, false)); @@ -1008,17 +1006,14 @@ impl Tree for BridgeTree bool { match self.checkpoints.pop() { Some(mut c) => { - // drop witnessed values at and above the checkpoint height; + // drop marked values at and above the checkpoint height; // we will re-mark if necessary. self.saved.append(&mut c.forgotten); self.saved.retain(|_, i| *i + 1 < c.bridges_len); self.prior_bridges.truncate(c.bridges_len); - self.current_bridge = self - .prior_bridges - .last() - .map(|b| b.successor(c.is_witnessed)); - if c.is_witnessed { - self.witness(); + self.current_bridge = self.prior_bridges.last().map(|b| b.successor(c.is_marked)); + if c.is_marked { + self.mark(); } true } @@ -1245,7 +1240,7 @@ mod tests { let mut t = BridgeTree::::new(100); t.checkpoint(); t.append(&"a".to_string()); - t.witness(); + t.mark(); t.append(&"b".to_string()); t.append(&"c".to_string()); assert!( @@ -1271,14 +1266,14 @@ mod tests { } #[test] - fn rewind_remove_witness() { - crate::tests::check_rewind_remove_witness(BridgeTree::::new); + fn rewind_remove_mark() { + crate::tests::check_rewind_remove_mark(BridgeTree::::new); } #[test] fn garbage_collect() { let mut t = BridgeTree::::new(10); - let mut to_unwitness = vec![]; + let mut to_unmark = vec![]; let mut has_auth_path = vec![]; for i in 0usize..100 { let elem: String = format!("{},", i); @@ -1287,19 +1282,19 @@ mod tests { t.checkpoint(); } if i % 7 == 0 { - t.witness(); + t.mark(); if i > 0 && i % 2 == 0 { - to_unwitness.push(Position::from(i)); + to_unmark.push(Position::from(i)); } else { has_auth_path.push(Position::from(i)); } } - if i % 11 == 0 && !to_unwitness.is_empty() { - let pos = to_unwitness.remove(0); - t.remove_witness(pos); + if i % 11 == 0 && !to_unmark.is_empty() { + let pos = to_unmark.remove(0); + t.remove_mark(pos); } } - // 32 = 20 (checkpointed) + 14 (witnessed) - 2 (witnessed & checkpointed) + // 32 = 20 (checkpointed) + 14 (marked) - 2 (marked & checkpointed) assert_eq!(t.prior_bridges().len(), 20 + 14 - 2); let auth_paths = has_auth_path .iter() @@ -1311,7 +1306,7 @@ mod tests { ) .collect::>(); t.garbage_collect(); - // 20 = 32 - 10 (removed checkpoints) + 1 (not removed due to mark) - 3 (removed witnesses) + // 20 = 32 - 10 (removed checkpoints) + 1 (not removed due to mark) - 3 (removed marks) assert_eq!(t.prior_bridges().len(), 32 - 10 + 1 - 3); let retained_auth_paths = has_auth_path .iter() diff --git a/src/lib.rs b/src/lib.rs index e20916b6..3c351b51 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ //! continue to function; other information should be pruned eagerly to avoid //! waste when the tree state is encoded. //! -//! ## Witnessing +//! ## Marking //! //! Merkle trees are typically used to show that a value exists in the tree via //! an authentication path. We need an API that allows us to identify the @@ -347,8 +347,8 @@ pub trait Frontier { fn root(&self) -> H; } -/// A Merkle tree that supports incremental appends, witnessing of -/// leaf nodes, checkpoints and rollbacks. +/// A Merkle tree that supports incremental appends, marking of +/// leaf nodes for construction of witnesses, checkpoints and rollbacks. pub trait Tree { /// Appends a new value to the tree at the next available slot. /// Returns true if successful and false if the tree would exceed @@ -363,16 +363,16 @@ pub trait Tree { /// Returns the leaf at the specified position if the tree can produce /// an authentication path for it. - fn get_witnessed_leaf(&self, position: Position) -> Option<&H>; + fn get_marked_leaf(&self, position: Position) -> Option<&H>; /// Marks the current leaf as one for which we're interested in producing /// an authentication path. Returns an optional value containing the /// current position if successful or if the current value was already /// marked, or None if the tree is empty. - fn witness(&mut self) -> Option; + fn mark(&mut self) -> Option; - /// Return a set of all the positions for which we have witnessed. - fn witnessed_positions(&self) -> BTreeSet; + /// Return a set of all the positions for which we have marked. + fn marked_positions(&self) -> BTreeSet; /// Obtains the root of the Merkle tree at the specified checkpoint depth /// by hashing against empty nodes up to the maximum height of the tree. @@ -388,9 +388,9 @@ pub trait Tree { fn authentication_path(&self, position: Position, as_of_root: &H) -> Option>; /// Marks the value at the specified position as a value we're no longer - /// interested in maintaining a witness for. Returns true if successful and - /// false if we were already not maintaining a witness at this position. - fn remove_witness(&mut self, position: Position) -> bool; + /// interested in maintaining a mark for. Returns true if successful and + /// false if we were already not maintaining a mark at this position. + fn remove_mark(&mut self, position: Position) -> bool; /// Creates a new checkpoint for the current tree state. It is valid to /// have multiple checkpoints for the same tree state, and each `rewind` @@ -405,7 +405,7 @@ pub trait Tree { fn rewind(&mut self) -> bool; /// Remove state from the tree that no longer needs to be maintained - /// because it is associated with checkpoints or witnesses that + /// because it is associated with checkpoints or marks that /// have been removed from the tree at positions deeper than those /// reachable by calls to `rewind`. It is always safe to implement /// this as a no-op operation @@ -570,7 +570,7 @@ pub(crate) mod tests { let mut t = new_tree(100); t.append(&"a".to_string()); t.checkpoint(); - t.witness(); + t.mark(); t.append(&"a".to_string()); t.append(&"a".to_string()); t.append(&"a".to_string()); @@ -582,7 +582,7 @@ pub(crate) mod tests { ) { let mut tree = new_tree(100); tree.append(&"a".to_string()); - tree.witness(); + tree.mark(); assert_eq!( tree.authentication_path(Position(0), &tree.root(0).unwrap()), Some(vec![ @@ -605,7 +605,7 @@ pub(crate) mod tests { ); tree.append(&"c".to_string()); - tree.witness(); + tree.mark(); assert_eq!( tree.authentication_path(Position(2), &tree.root(0).unwrap()), Some(vec![ @@ -640,11 +640,11 @@ pub(crate) mod tests { let mut tree = new_tree(100); tree.append(&"a".to_string()); - tree.witness(); + tree.mark(); for c in 'b'..'h' { tree.append(&c.to_string()); } - tree.witness(); + tree.mark(); tree.append(&"h".to_string()); assert_eq!( @@ -659,15 +659,15 @@ pub(crate) mod tests { let mut tree = new_tree(100); tree.append(&"a".to_string()); - tree.witness(); + tree.mark(); tree.append(&"b".to_string()); tree.append(&"c".to_string()); tree.append(&"d".to_string()); - tree.witness(); + tree.mark(); tree.append(&"e".to_string()); - tree.witness(); + tree.mark(); tree.append(&"f".to_string()); - tree.witness(); + tree.mark(); tree.append(&"g".to_string()); assert_eq!( @@ -684,7 +684,7 @@ pub(crate) mod tests { for c in 'a'..'l' { tree.append(&c.to_string()); } - tree.witness(); + tree.mark(); tree.append(&'l'.to_string()); assert_eq!( @@ -699,13 +699,13 @@ pub(crate) mod tests { let mut tree = new_tree(100); tree.append(&'a'.to_string()); - tree.witness(); + tree.mark(); tree.checkpoint(); assert!(tree.rewind()); for c in 'b'..'f' { tree.append(&c.to_string()); } - tree.witness(); + tree.mark(); for c in 'f'..'i' { tree.append(&c.to_string()); } @@ -724,12 +724,12 @@ pub(crate) mod tests { tree.append(&'a'.to_string()); tree.append(&'b'.to_string()); tree.append(&'c'.to_string()); - tree.witness(); + tree.mark(); tree.append(&'d'.to_string()); tree.append(&'e'.to_string()); tree.append(&'f'.to_string()); tree.append(&'g'.to_string()); - tree.witness(); + tree.mark(); tree.checkpoint(); tree.append(&'h'.to_string()); assert!(tree.rewind()); @@ -747,7 +747,7 @@ pub(crate) mod tests { let mut tree = new_tree(100); tree.append(&'a'.to_string()); tree.append(&'b'.to_string()); - tree.witness(); + tree.mark(); assert_eq!( tree.authentication_path(Position(0), &tree.root(0).unwrap()), None @@ -757,9 +757,9 @@ pub(crate) mod tests { for c in 'a'..'n' { tree.append(&c.to_string()); } - tree.witness(); + tree.mark(); tree.append(&'n'.to_string()); - tree.witness(); + tree.mark(); tree.append(&'o'.to_string()); tree.append(&'p'.to_string()); @@ -776,7 +776,7 @@ pub(crate) mod tests { let ops = ('a'..='l') .into_iter() .map(|c| Append(c.to_string())) - .chain(Some(Witness)) + .chain(Some(Mark)) .chain(Some(Append('m'.to_string()))) .chain(Some(Append('n'.to_string()))) .chain(Some(Authpath(11usize.into(), 0))) @@ -809,20 +809,20 @@ pub(crate) mod tests { t.append(&"a".to_string()); t.checkpoint(); t.append(&"b".to_string()); - t.witness(); + t.mark(); assert!(t.rewind()); assert_eq!(Some(Position(0)), t.current_position()); let mut t = new_tree(100); t.append(&"a".to_string()); - t.witness(); + t.mark(); t.checkpoint(); assert!(t.rewind()); let mut t = new_tree(100); t.append(&"a".to_string()); t.checkpoint(); - t.witness(); + t.mark(); t.append(&"a".to_string()); assert!(t.rewind()); assert_eq!(Some(Position(0)), t.current_position()); @@ -838,84 +838,84 @@ pub(crate) mod tests { assert_eq!(t.root(0).unwrap(), "ab______________"); } - pub(crate) fn check_rewind_remove_witness, F: Fn(usize) -> T>(new_tree: F) { + pub(crate) fn check_rewind_remove_mark, F: Fn(usize) -> T>(new_tree: F) { let mut tree = new_tree(100); tree.append(&"e".to_string()); - tree.witness(); + tree.mark(); tree.checkpoint(); assert!(tree.rewind()); - assert!(tree.remove_witness(0usize.into())); + assert!(tree.remove_mark(0usize.into())); let mut tree = new_tree(100); tree.append(&"e".to_string()); tree.checkpoint(); - tree.witness(); + tree.mark(); assert!(tree.rewind()); - assert!(!tree.remove_witness(0usize.into())); + assert!(!tree.remove_mark(0usize.into())); let mut tree = new_tree(100); tree.append(&"e".to_string()); - tree.witness(); + tree.mark(); tree.checkpoint(); - assert!(tree.remove_witness(0usize.into())); + assert!(tree.remove_mark(0usize.into())); assert!(tree.rewind()); - assert!(tree.remove_witness(0usize.into())); + assert!(tree.remove_mark(0usize.into())); let mut tree = new_tree(100); tree.append(&"e".to_string()); - tree.witness(); - assert!(tree.remove_witness(0usize.into())); + tree.mark(); + assert!(tree.remove_mark(0usize.into())); tree.checkpoint(); assert!(tree.rewind()); - assert!(!tree.remove_witness(0usize.into())); + assert!(!tree.remove_mark(0usize.into())); let mut tree = new_tree(100); tree.append(&"a".to_string()); - assert!(!tree.remove_witness(0usize.into())); + assert!(!tree.remove_mark(0usize.into())); tree.checkpoint(); - assert!(tree.witness().is_some()); + assert!(tree.mark().is_some()); assert!(tree.rewind()); let mut tree = new_tree(100); tree.append(&"a".to_string()); tree.checkpoint(); - assert!(tree.witness().is_some()); - assert!(tree.remove_witness(0usize.into())); + assert!(tree.mark().is_some()); + assert!(tree.remove_mark(0usize.into())); assert!(tree.rewind()); - assert!(!tree.remove_witness(0usize.into())); + assert!(!tree.remove_mark(0usize.into())); // The following check_operations tests cover errors where the // test framework itself previously did not correctly handle // chain state restoration. let samples = vec![ - vec![append("x"), Checkpoint, Witness, Rewind, unwitness(0)], + vec![append("x"), Checkpoint, Mark, Rewind, unmark(0)], vec![ append("d"), Checkpoint, - Witness, - unwitness(0), + Mark, + unmark(0), Rewind, - unwitness(0), + unmark(0), ], vec![ append("o"), Checkpoint, - Witness, + Mark, Checkpoint, - unwitness(0), + unmark(0), Rewind, Rewind, ], vec![ append("s"), - Witness, + Mark, append("m"), Checkpoint, - unwitness(0), + unmark(0), Rewind, - unwitness(0), - unwitness(0), + unmark(0), + unmark(0), ], ]; @@ -978,26 +978,26 @@ pub(crate) mod tests { a } - fn get_witnessed_leaf(&self, position: Position) -> Option<&H> { - let a = self.inefficient.get_witnessed_leaf(position); - let b = self.efficient.get_witnessed_leaf(position); + fn get_marked_leaf(&self, position: Position) -> Option<&H> { + let a = self.inefficient.get_marked_leaf(position); + let b = self.efficient.get_marked_leaf(position); assert_eq!(a, b); a } - fn witness(&mut self) -> Option { - let a = self.inefficient.witness(); - let b = self.efficient.witness(); + fn mark(&mut self) -> Option { + let a = self.inefficient.mark(); + let b = self.efficient.mark(); assert_eq!(a, b); - let apos = self.inefficient.witnessed_positions(); - let bpos = self.efficient.witnessed_positions(); + let apos = self.inefficient.marked_positions(); + let bpos = self.efficient.marked_positions(); assert_eq!(apos, bpos); a } - fn witnessed_positions(&self) -> BTreeSet { - let a = self.inefficient.witnessed_positions(); - let b = self.efficient.witnessed_positions(); + fn marked_positions(&self) -> BTreeSet { + let a = self.inefficient.marked_positions(); + let b = self.efficient.marked_positions(); assert_eq!(a, b); a } @@ -1009,9 +1009,9 @@ pub(crate) mod tests { a } - fn remove_witness(&mut self, position: Position) -> bool { - let a = self.inefficient.remove_witness(position); - let b = self.efficient.remove_witness(position); + fn remove_mark(&mut self, position: Position) -> bool { + let a = self.inefficient.remove_mark(position); + let b = self.efficient.remove_mark(position); assert_eq!(a, b); a } @@ -1043,10 +1043,10 @@ pub(crate) mod tests { Append(A), CurrentPosition, CurrentLeaf, - Witness, - WitnessedLeaf(Position), - WitnessedPositions, - Unwitness(Position), + Mark, + MarkedLeaf(Position), + MarkedPositions, + Unmark(Position), Checkpoint, Rewind, Authpath(Position, usize), @@ -1059,8 +1059,8 @@ pub(crate) mod tests { Operation::Append(x.to_string()) } - fn unwitness(pos: usize) -> Operation { - Operation::Unwitness(Position(pos)) + fn unmark(pos: usize) -> Operation { + Operation::Unmark(Position(pos)) } fn authpath(pos: usize, depth: usize) -> Operation { @@ -1076,14 +1076,14 @@ pub(crate) mod tests { } CurrentPosition => None, CurrentLeaf => None, - Witness => { - assert!(tree.witness().is_some(), "witness failed"); + Mark => { + assert!(tree.mark().is_some(), "mark failed"); None } - WitnessedLeaf(_) => None, - WitnessedPositions => None, - Unwitness(p) => { - assert!(tree.remove_witness(*p), "remove witness failed"); + MarkedLeaf(_) => None, + MarkedPositions => None, + Unmark(p) => { + assert!(tree.remove_mark(*p), "remove mark failed"); None } Checkpoint => { @@ -1195,42 +1195,42 @@ pub(crate) mod tests { append("a"), append("b"), Checkpoint, - Witness, + Mark, authpath(0, 1), ], vec![ append("c"), append("d"), - Witness, + Mark, Checkpoint, authpath(1, 1), ], vec![ append("e"), Checkpoint, - Witness, + Mark, append("f"), authpath(0, 1), ], vec![ append("g"), - Witness, + Mark, Checkpoint, - unwitness(0), + unmark(0), append("h"), authpath(0, 0), ], vec![ append("i"), Checkpoint, - Witness, - unwitness(0), + Mark, + unmark(0), append("j"), authpath(0, 0), ], vec![ append("i"), - Witness, + Mark, append("j"), Checkpoint, append("k"), @@ -1239,35 +1239,35 @@ pub(crate) mod tests { vec![ append("l"), Checkpoint, - Witness, + Mark, Checkpoint, append("m"), Checkpoint, authpath(0, 2), ], - vec![Checkpoint, append("n"), Witness, authpath(0, 1)], + vec![Checkpoint, append("n"), Mark, authpath(0, 1)], vec![ append("a"), - Witness, + Mark, Checkpoint, - unwitness(0), + unmark(0), Checkpoint, append("b"), authpath(0, 1), ], vec![ append("a"), - Witness, + Mark, append("b"), - unwitness(0), + unmark(0), Checkpoint, authpath(0, 0), ], vec![ append("a"), - Witness, + Mark, Checkpoint, - unwitness(0), + unmark(0), Checkpoint, Rewind, append("b"), @@ -1275,41 +1275,41 @@ pub(crate) mod tests { ], vec![ append("a"), - Witness, + Mark, Checkpoint, Checkpoint, Rewind, append("a"), - unwitness(0), + unmark(0), authpath(0, 1), ], // Unreduced examples vec![ append("o"), append("p"), - Witness, + Mark, append("q"), Checkpoint, - unwitness(1), + unmark(1), authpath(1, 1), ], vec![ append("r"), append("s"), append("t"), - Witness, + Mark, Checkpoint, - unwitness(2), + unmark(2), Checkpoint, authpath(2, 2), ], vec![ append("u"), - Witness, + Mark, append("v"), append("w"), Checkpoint, - unwitness(0), + unmark(0), append("x"), Checkpoint, Checkpoint, @@ -1331,35 +1331,35 @@ pub(crate) mod tests { // These check_operations tests cover errors where the test framework itself previously did not // correctly handle chain state restoration. #[test] - fn test_rewind_remove_witness_consistency() { + fn test_rewind_remove_mark_consistency() { let samples = vec![ - vec![append("x"), Checkpoint, Witness, Rewind, unwitness(0)], + vec![append("x"), Checkpoint, Mark, Rewind, unmark(0)], vec![ append("d"), Checkpoint, - Witness, - unwitness(0), + Mark, + unmark(0), Rewind, - unwitness(0), + unmark(0), ], vec![ append("o"), Checkpoint, - Witness, + Mark, Checkpoint, - unwitness(0), + unmark(0), Rewind, Rewind, ], vec![ append("s"), - Witness, + Mark, append("m"), Checkpoint, - unwitness(0), + unmark(0), Rewind, - unwitness(0), - unwitness(0), + unmark(0), + unmark(0), ], ]; for (i, sample) in samples.iter().enumerate() { @@ -1384,19 +1384,17 @@ pub(crate) mod tests { { prop_oneof![ item_gen.prop_map(Operation::Append), - Just(Operation::Witness), + Just(Operation::Mark), prop_oneof![ Just(Operation::CurrentLeaf), Just(Operation::CurrentPosition), - Just(Operation::WitnessedPositions), + Just(Operation::MarkedPositions), ], Just(Operation::GarbageCollect), pos_gen .clone() - .prop_map(|i| Operation::WitnessedLeaf(Position(i))), - pos_gen - .clone() - .prop_map(|i| Operation::Unwitness(Position(i))), + .prop_map(|i| Operation::MarkedLeaf(Position(i))), + pos_gen.clone().prop_map(|i| Operation::Unmark(Position(i))), Just(Operation::Checkpoint), Just(Operation::Rewind), pos_gen.prop_flat_map( @@ -1410,11 +1408,11 @@ pub(crate) mod tests { Append(value) => { tree.append(&value); } - Witness => { - tree.witness(); + Mark => { + tree.mark(); } - Unwitness(position) => { - tree.remove_witness(position); + Unmark(position) => { + tree.remove_mark(position); } Checkpoint => { tree.checkpoint(); @@ -1425,8 +1423,8 @@ pub(crate) mod tests { CurrentPosition => {} CurrentLeaf => {} Authpath(_, _) => {} - WitnessedLeaf(_) => {} - WitnessedPositions => {} + MarkedLeaf(_) => {} + MarkedPositions => {} GarbageCollect => {} } } @@ -1463,22 +1461,22 @@ pub(crate) mod tests { CurrentLeaf => { prop_assert_eq!(tree_values.last(), tree.current_leaf()); } - Witness => { - if tree.witness().is_some() { + Mark => { + if tree.mark().is_some() { prop_assert!(tree_size != 0); } else { prop_assert_eq!(tree_size, 0); } } - WitnessedLeaf(position) => { - if tree.get_witnessed_leaf(*position).is_some() { + MarkedLeaf(position) => { + if tree.get_marked_leaf(*position).is_some() { prop_assert!(::from(*position) < tree_size); } } - Unwitness(position) => { - tree.remove_witness(*position); + Unmark(position) => { + tree.remove_mark(*position); } - WitnessedPositions => {} + MarkedPositions => {} Checkpoint => { tree_checkpoints.push(tree_size); tree.checkpoint(); diff --git a/src/sample.rs b/src/sample.rs index 2383b3f0..9f228261 100644 --- a/src/sample.rs +++ b/src/sample.rs @@ -6,7 +6,7 @@ use std::collections::BTreeSet; pub struct TreeState { leaves: Vec, current_offset: usize, - witnesses: BTreeSet, + marks: BTreeSet, depth: usize, } @@ -17,7 +17,7 @@ impl TreeState { Self { leaves: vec![H::empty_leaf(); 1 << depth], current_offset: 0, - witnesses: BTreeSet::new(), + marks: BTreeSet::new(), depth, } } @@ -57,8 +57,8 @@ impl TreeState { /// Returns the leaf at the specified position if the tree can produce /// an authentication path for it. - fn get_witnessed_leaf(&self, position: Position) -> Option<&H> { - if self.witnesses.contains(&position) { + fn get_marked_leaf(&self, position: Position) -> Option<&H> { + if self.marks.contains(&position) { self.leaves.get(::from(position)) } else { None @@ -66,10 +66,10 @@ impl TreeState { } /// Marks the current tree state leaf as a value that we're interested in - /// witnessing. Returns the current position if the tree is non-empty. - fn witness(&mut self) -> Option { + /// marking. Returns the current position if the tree is non-empty. + fn mark(&mut self) -> Option { self.current_position().map(|pos| { - self.witnesses.insert(pos); + self.marks.insert(pos); pos }) } @@ -97,10 +97,10 @@ impl TreeState { } /// Marks the value at the specified position as a value we're no longer - /// interested in maintaining a witness for. Returns true if successful and - /// false if we were already not maintaining a witness at this position. - fn remove_witness(&mut self, position: Position) -> bool { - self.witnesses.remove(&position) + /// interested in maintaining a mark for. Returns true if successful and + /// false if we were already not maintaining a mark at this position. + fn remove_mark(&mut self, position: Position) -> bool { + self.marks.remove(&position) } } @@ -166,16 +166,16 @@ impl Tree for CompleteTree self.tree_state.current_leaf() } - fn get_witnessed_leaf(&self, position: Position) -> Option<&H> { - self.tree_state.get_witnessed_leaf(position) + fn get_marked_leaf(&self, position: Position) -> Option<&H> { + self.tree_state.get_marked_leaf(position) } - fn witness(&mut self) -> Option { - self.tree_state.witness() + fn mark(&mut self) -> Option { + self.tree_state.mark() } - fn witnessed_positions(&self) -> BTreeSet { - self.tree_state.witnesses.clone() + fn marked_positions(&self) -> BTreeSet { + self.tree_state.marks.clone() } fn root(&self, checkpoint_depth: usize) -> Option { @@ -190,7 +190,7 @@ impl Tree for CompleteTree .iter() .chain(Some(&self.tree_state)) .rev() - .skip_while(|c| !c.witnesses.contains(&position)) + .skip_while(|c| !c.marks.contains(&position)) .find_map(|c| { if &c.root() == root { c.authentication_path(position) @@ -200,8 +200,8 @@ impl Tree for CompleteTree }) } - fn remove_witness(&mut self, position: Position) -> bool { - self.tree_state.remove_witness(position) + fn remove_mark(&mut self, position: Position) -> bool { + self.tree_state.remove_mark(position) } fn checkpoint(&mut self) { @@ -315,7 +315,7 @@ mod tests { let mut tree = CompleteTree::::new(DEPTH, 100); for value in values { assert!(tree.append(&value)); - tree.witness(); + tree.mark(); } assert!(!tree.append(&SipHashable(0))); @@ -353,7 +353,7 @@ mod tests { } #[test] - fn rewind_remove_witness() { - crate::tests::check_rewind_remove_witness(|max_c| CompleteTree::::new(4, max_c)); + fn rewind_remove_mark() { + crate::tests::check_rewind_remove_mark(|max_c| CompleteTree::::new(4, max_c)); } }