From bb920341a6bcca7d67f1e1ff5803c9faed8cba73 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 12 Jul 2023 17:04:31 +0100 Subject: [PATCH] Fix bug in `RightFirstDisjoint` insertion logic --- zcash_client_sqlite/src/wallet/scanning.rs | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/zcash_client_sqlite/src/wallet/scanning.rs b/zcash_client_sqlite/src/wallet/scanning.rs index a34d18703..d5df3d6c7 100644 --- a/zcash_client_sqlite/src/wallet/scanning.rs +++ b/zcash_client_sqlite/src/wallet/scanning.rs @@ -429,7 +429,7 @@ impl SpanningTree { RightFirstDisjoint => { // extend the left-hand branch SpanningTree::Parent { - span: to_insert.block_range().start..left.span().end, + span: to_insert.block_range().start..right.span().end, left: Box::new(left.insert(to_insert)), right, } @@ -881,6 +881,40 @@ mod tests { ); } + #[test] + fn spanning_tree_insert_rfd_span() { + use ScanPriority::*; + + // This sequence of insertions causes a RightFirstDisjoint on the last insertion, + // which originally had a bug that caused the parent's span to only cover its left + // child. The bug was otherwise unobservable as the insertion logic was able to + // heal this specific kind of bug. + let t = spanning_tree(&[ + // 6..8 + (6..8, Scanned), + // 6..12 + // 6..8 8..12 + // 8..10 10..12 + (10..12, ChainTip), + // 3..12 + // 3..8 8..12 + // 3..6 6..8 8..10 10..12 + (3..6, Historic), + ]) + .unwrap(); + + assert_eq!(t.span(), (3.into())..(12.into())); + assert_eq!( + t.into_vec(), + vec![ + scan_range(3..6, Historic), + scan_range(6..8, Scanned), + scan_range(8..10, Historic), + scan_range(10..12, ChainTip), + ] + ); + } + #[test] fn spanning_tree_dominance() { use ScanPriority::*;