From 0e42de5043df62123047ec37cc6561486b5c70a7 Mon Sep 17 00:00:00 2001 From: Aaro Altonen Date: Mon, 7 Nov 2022 13:28:37 +0200 Subject: [PATCH 1/2] Do not update peer information if ancestor search is in progress If block announcement is received from a peer while ancestor search for that same peer is still in progress, do not update the peer's best hash and best number as that causes the ancestor search to yield different information from what was expected and can cause, for example, a fork of lower height not be be downloaded. --- client/network/sync/src/lib.rs | 10 +++++----- client/network/test/src/sync.rs | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/client/network/sync/src/lib.rs b/client/network/sync/src/lib.rs index f369bdb47e1c6..5bedf1c0a0a0a 100644 --- a/client/network/sync/src/lib.rs +++ b/client/network/sync/src/lib.rs @@ -2043,17 +2043,17 @@ where return PollBlockAnnounceValidation::Nothing { is_best, who, announce } }; + if let PeerSyncState::AncestorSearch { .. } = peer.state { + trace!(target: "sync", "Peer state is ancestor search."); + return PollBlockAnnounceValidation::Nothing { is_best, who, announce } + } + if is_best { // update their best block peer.best_number = number; peer.best_hash = hash; } - if let PeerSyncState::AncestorSearch { .. } = peer.state { - trace!(target: "sync", "Peer state is ancestor search."); - return PollBlockAnnounceValidation::Nothing { is_best, who, announce } - } - // If the announced block is the best they have and is not ahead of us, our common number // is either one further ahead or it's the one they just announced, if we know about it. if is_best { diff --git a/client/network/test/src/sync.rs b/client/network/test/src/sync.rs index 9ae3014e497ce..1dcddf32f948d 100644 --- a/client/network/test/src/sync.rs +++ b/client/network/test/src/sync.rs @@ -558,6 +558,9 @@ fn syncs_header_only_forks() { while !net.peer(1).has_block(&small_hash) { net.block_until_idle(); } + + assert_eq!(net.peer(0).client().info().best_hash, net.peer(1).client().info().best_hash); + assert_ne!(small_hash, net.peer(0).client().info().best_hash); } #[test] From 37660781483ba4d159f67616dca95736a7fd76aa Mon Sep 17 00:00:00 2001 From: Aaro Altonen Date: Mon, 7 Nov 2022 15:46:04 +0200 Subject: [PATCH 2/2] Block until peers are in sync --- client/network/test/src/sync.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/client/network/test/src/sync.rs b/client/network/test/src/sync.rs index 1dcddf32f948d..60d6e7db49a52 100644 --- a/client/network/test/src/sync.rs +++ b/client/network/test/src/sync.rs @@ -559,6 +559,7 @@ fn syncs_header_only_forks() { net.block_until_idle(); } + net.block_until_sync(); assert_eq!(net.peer(0).client().info().best_hash, net.peer(1).client().info().best_hash); assert_ne!(small_hash, net.peer(0).client().info().best_hash); }