From 14667bfad6e2dc9ab5152e1356a63e5beb404b86 Mon Sep 17 00:00:00 2001 From: Antioch Peverell <30642645+antiochp@users.noreply.github.com> Date: Tue, 26 Jun 2018 09:42:12 -0400 Subject: [PATCH] cleanup path fn and add test coverage (#1201) --- core/src/core/pmmr.rs | 9 +++------ core/tests/pmmr.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/src/core/pmmr.rs b/core/src/core/pmmr.rs index eb96c2c6..b4b77518 100644 --- a/core/src/core/pmmr.rs +++ b/core/src/core/pmmr.rs @@ -712,14 +712,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 7538cd6e..da6ef633 100644 --- a/core/tests/pmmr.rs +++ b/core/tests/pmmr.rs @@ -97,6 +97,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);