Add exponential search for ancestor search (#1875)

* feat: add exponential search

* add tests

* chore: improve code
This commit is contained in:
Marcio Diaz
2019-03-15 11:15:08 +01:00
committed by GitHub
parent a57f6bbe6a
commit 23177ca8a4
2 changed files with 169 additions and 46 deletions
+78
View File
@@ -24,6 +24,24 @@ use std::thread;
use std::time::Duration;
use super::*;
fn test_ancestor_search_when_common_is(n: usize) {
let _ = ::env_logger::try_init();
let mut net = TestNet::new(3);
net.peer(0).push_blocks(n, false);
net.peer(1).push_blocks(n, false);
net.peer(2).push_blocks(n, false);
net.peer(0).push_blocks(10, true);
net.peer(1).push_blocks(100, false);
net.peer(2).push_blocks(100, false);
net.restart_peer(0);
net.sync();
assert!(net.peer(0).client.backend().as_in_memory().blockchain()
.canon_equals_to(net.peer(1).client.backend().as_in_memory().blockchain()));
}
#[test]
fn sync_peers_works() {
let _ = ::env_logger::try_init();
@@ -140,6 +158,66 @@ fn sync_from_two_peers_with_ancestry_search_works() {
.canon_equals_to(net.peer(1).client.backend().as_in_memory().blockchain()));
}
#[test]
fn ancestry_search_works_when_common_is_hundred() {
let _ = ::env_logger::try_init();
let mut net = TestNet::new(3);
net.peer(0).push_blocks(100, false);
net.peer(1).push_blocks(100, false);
net.peer(2).push_blocks(100, false);
net.peer(0).push_blocks(10, true);
net.peer(1).push_blocks(100, false);
net.peer(2).push_blocks(100, false);
net.restart_peer(0);
net.sync();
assert!(net.peer(0).client.backend().as_in_memory().blockchain()
.canon_equals_to(net.peer(1).client.backend().as_in_memory().blockchain()));
}
#[test]
fn ancestry_search_works_when_backoff_is_one() {
let _ = ::env_logger::try_init();
let mut net = TestNet::new(3);
net.peer(0).push_blocks(1, false);
net.peer(1).push_blocks(2, false);
net.peer(2).push_blocks(2, false);
net.restart_peer(0);
net.sync();
assert!(net.peer(0).client.backend().as_in_memory().blockchain()
.canon_equals_to(net.peer(1).client.backend().as_in_memory().blockchain()));
}
#[test]
fn ancestry_search_works_when_ancestor_is_genesis() {
let _ = ::env_logger::try_init();
let mut net = TestNet::new(3);
net.peer(0).push_blocks(13, true);
net.peer(1).push_blocks(100, false);
net.peer(2).push_blocks(100, false);
net.restart_peer(0);
net.sync();
assert!(net.peer(0).client.backend().as_in_memory().blockchain()
.canon_equals_to(net.peer(1).client.backend().as_in_memory().blockchain()));
}
#[test]
fn ancestry_search_works_when_common_is_one() {
test_ancestor_search_when_common_is(1);
}
#[test]
fn ancestry_search_works_when_common_is_two() {
test_ancestor_search_when_common_is(2);
}
#[test]
fn sync_long_chain_works() {
let mut net = TestNet::new(2);