From 43540b36f6a440cc85d5354f150e47a3f06f9fa5 Mon Sep 17 00:00:00 2001 From: Antioch Peverell <30642645+antiochp@users.noreply.github.com> Date: Mon, 25 Jun 2018 16:28:06 -0400 Subject: [PATCH] refactor path fn to be cleaner and more readable (#1195) --- core/src/core/pmmr.rs | 9 +++------ core/tests/pmmr.rs | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/core/src/core/pmmr.rs b/core/src/core/pmmr.rs index f35d9826..9c626f85 100644 --- a/core/src/core/pmmr.rs +++ b/core/src/core/pmmr.rs @@ -838,14 +838,11 @@ pub fn is_left_sibling(pos: u64) -> bool { /// The size (and therefore the set of peaks) of the MMR /// is defined by last_pos. pub fn path(pos: u64, last_pos: u64) -> Vec { - let mut path = vec![pos]; + let mut path = vec![]; let mut current = pos; - while current + 1 <= last_pos { + while current <= last_pos { + path.push(current); let (parent, _) = family(current); - if parent > last_pos { - break; - } - path.push(parent); current = parent; } path diff --git a/core/tests/pmmr.rs b/core/tests/pmmr.rs index a2ee50a8..811f1096 100644 --- a/core/tests/pmmr.rs +++ b/core/tests/pmmr.rs @@ -93,18 +93,18 @@ where fn rewind( &mut self, - position: u64, - rewind_add_pos: &Bitmap, - rewind_rm_pos: &Bitmap, + _position: u64, + _rewind_add_pos: &Bitmap, + _rewind_rm_pos: &Bitmap, ) -> Result<(), String> { - panic!("not yet implemented for vec backend..."); + panic!("not implemented for vec backend..."); } fn get_data_file_path(&self) -> String { "".to_string() } - fn snapshot(&self, header: &BlockHeader) -> Result<(), String> { + fn snapshot(&self, _header: &BlockHeader) -> Result<(), String> { Ok(()) } @@ -208,6 +208,14 @@ fn various_families() { assert_eq!(pmmr::family(1_000), (1_001, 997)); } +#[test] +fn test_paths() { + assert_eq!(pmmr::path(1, 1), [1]); + assert_eq!(pmmr::path(1, 3), [1, 3]); + assert_eq!(pmmr::path(2, 3), [2, 3]); + assert_eq!(pmmr::path(4, 16), [4, 6, 7, 15]); +} + #[test] fn test_is_left_sibling() { assert_eq!(pmmr::is_left_sibling(1), true);