Don't return the same block twice in ancestor binary search (#11067)

* Don't return the same block in ancestor search

* Add regression test for ancestor search repeat
This commit is contained in:
Nathan Whitaker authored and GitHub committed 2022-03-21 12:21:53 +00:00
1 parent 434eaffd4c
commit dd35c571f6
1 file changed
+10 -1
+10 -1
View File
@@ -2320,7 +2320,11 @@ fn handle_ancestor_search_state<B: BlockT>(
}
assert!(right >= left);
let middle = left + (right - left) / two;
Some((AncestorSearchState::BinarySearch(left, right), middle))
if middle == curr_block_num {
None
} else {
Some((AncestorSearchState::BinarySearch(left, right), middle))
}
},
}
}
@@ -3238,4 +3242,9 @@ mod test {
sync.on_block_data(&peer_id1, Some(request), response).unwrap();
assert_eq!(sync.best_queued_number, 4);
}
#[test]
fn ancestor_search_repeat() {
let state = AncestorSearchState::<Block>::BinarySearch(1, 3);
assert!(handle_ancestor_search_state(&state, 2, true).is_none());
}
}