From f38aa0d2de5082768d112f4656123ca13cc65bbc Mon Sep 17 00:00:00 2001 From: Arkadiy Paronyan Date: Thu, 24 Jan 2019 16:52:48 +0100 Subject: [PATCH] Test syncing to all forks (#1544) --- substrate/core/client/src/in_mem.rs | 5 +++++ substrate/core/network/src/test/sync.rs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/substrate/core/client/src/in_mem.rs b/substrate/core/client/src/in_mem.rs index ae6703a78d..7102426b8f 100644 --- a/substrate/core/client/src/in_mem.rs +++ b/substrate/core/client/src/in_mem.rs @@ -222,6 +222,11 @@ impl Blockchain { Ok(()) } + /// Get total number of blocks. + pub fn blocks_count(&self) -> usize { + self.storage.read().blocks.len() + } + /// Compare this blockchain with another in-mem blockchain pub fn equals_to(&self, other: &Self) -> bool { self.canon_equals_to(other) && self.storage.read().blocks == other.storage.read().blocks diff --git a/substrate/core/network/src/test/sync.rs b/substrate/core/network/src/test/sync.rs index 54cf157eef..fbff1463f8 100644 --- a/substrate/core/network/src/test/sync.rs +++ b/substrate/core/network/src/test/sync.rs @@ -110,6 +110,23 @@ fn sync_after_fork_works() { assert!(net.peer(2).client.backend().blockchain().canon_equals_to(&peer1_chain)); } +#[test] +fn syncs_all_forks() { + ::env_logger::init().ok(); + let mut net = TestNet::new(4); + net.sync_step(); + net.peer(0).push_blocks(2, false); + net.peer(1).push_blocks(2, false); + + net.peer(0).push_blocks(2, true); + net.peer(1).push_blocks(4, false); + + net.sync(); + // Check that all peers have all of the blocks. + assert_eq!(9, net.peer(0).client.backend().blockchain().blocks_count()); + assert_eq!(9, net.peer(1).client.backend().blockchain().blocks_count()); +} + #[test] fn own_blocks_are_announced() { ::env_logger::init().ok();