Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 4bc45ee

Browse files
arkpargavofyork
authored andcommitted
Fixed common block tracking when syncing (#1235)
* Fixed common block tracking when syncing * Fixed fork resolution
1 parent 18d818b commit 4bc45ee

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

core/network/src/import_queue.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,12 @@ impl<B: BlockT, E: ExecuteInContext<B>> Link<B> for NetworkLink<B, E> {
311311
}
312312

313313
fn useless_peer(&self, who: NodeIndex, reason: &str) {
314+
trace!(target:"sync", "Useless peer {}, {}", who, reason);
314315
self.with_sync(|_, protocol| protocol.report_peer(who, Severity::Useless(reason)))
315316
}
316317

317318
fn note_useless_and_restart_sync(&self, who: NodeIndex, reason: &str) {
319+
trace!(target:"sync", "Bad peer {}, {}", who, reason);
318320
self.with_sync(|sync, protocol| {
319321
protocol.report_peer(who, Severity::Useless(reason)); // is this actually malign or just useless?
320322
sync.restart(protocol);

core/network/src/sync.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,16 @@ impl<B: BlockT> ChainSync<B> {
147147
(Ok(BlockStatus::Unknown), _) => {
148148
let our_best = self.best_queued_number;
149149
if our_best > As::sa(0) {
150+
let common_best = ::std::cmp::min(our_best, info.best_number);
150151
debug!(target:"sync", "New peer with unknown best hash {} ({}), searching for common ancestor.", info.best_hash, info.best_number);
151152
self.peers.insert(who, PeerSync {
152153
common_hash: self.genesis_hash,
153154
common_number: As::sa(0),
154155
best_hash: info.best_hash,
155156
best_number: info.best_number,
156-
state: PeerSyncState::AncestorSearch(our_best),
157+
state: PeerSyncState::AncestorSearch(common_best),
157158
});
158-
Self::request_ancestry(protocol, who, our_best)
159+
Self::request_ancestry(protocol, who, common_best)
159160
} else {
160161
// We are at genesis, just start downloading
161162
debug!(target:"sync", "New peer with best hash {} ({}).", info.best_hash, info.best_number);
@@ -254,13 +255,11 @@ impl<B: BlockT> ChainSync<B> {
254255
let best_seen = self.best_seen_block();
255256
let is_best = new_blocks.first().and_then(|b| b.block.header.as_ref()).map(|h| best_seen.as_ref().map_or(false, |n| h.number() >= n));
256257
let origin = if is_best.unwrap_or_default() { BlockOrigin::NetworkBroadcast } else { BlockOrigin::NetworkInitialSync };
258+
257259
if let Some((hash, number)) = new_blocks.last()
258-
.and_then(|b| b.block.header.as_ref().map(|h|(b.block.hash.clone(), *h.number())))
260+
.and_then(|b| b.block.header.as_ref().map(|h| (b.block.hash.clone(), *h.number())))
259261
{
260-
if number > self.best_queued_number {
261-
self.best_queued_number = number;
262-
self.best_queued_hash = hash;
263-
}
262+
self.block_queued(&hash, number);
264263
}
265264
self.maintain_sync(protocol);
266265
Some((origin, new_blocks))
@@ -274,6 +273,10 @@ impl<B: BlockT> ChainSync<B> {
274273
}
275274

276275
pub fn block_imported(&mut self, hash: &B::Hash, number: NumberFor<B>) {
276+
trace!(target: "sync", "Block imported successfully {} ({})", number, hash);
277+
}
278+
279+
fn block_queued(&mut self, hash: &B::Hash, number: NumberFor<B>) {
277280
if number > self.best_queued_number {
278281
self.best_queued_number = number;
279282
self.best_queued_hash = *hash;
@@ -284,13 +287,16 @@ impl<B: BlockT> ChainSync<B> {
284287
if peer.best_number >= number {
285288
peer.common_number = number;
286289
peer.common_hash = *hash;
290+
} else {
291+
peer.common_number = peer.best_number;
292+
peer.common_hash = peer.best_hash;
287293
}
288294
}
289295
}
290296

291297
pub(crate) fn update_chain_info(&mut self, best_header: &B::Header) {
292298
let hash = best_header.hash();
293-
self.block_imported(&hash, best_header.number().clone())
299+
self.block_queued(&hash, best_header.number().clone())
294300
}
295301

296302
pub(crate) fn on_block_announce(&mut self, protocol: &mut Context<B>, who: NodeIndex, hash: B::Hash, header: &B::Header) {
@@ -347,6 +353,7 @@ impl<B: BlockT> ChainSync<B> {
347353
Ok(info) => {
348354
self.best_queued_hash = info.best_queued_hash.unwrap_or(info.chain.best_hash);
349355
self.best_queued_number = info.best_queued_number.unwrap_or(info.chain.best_number);
356+
debug!(target:"sync", "Restarted with {} ({})", self.best_queued_number, self.best_queued_hash);
350357
},
351358
Err(e) => {
352359
debug!(target:"sync", "Error reading blockchain: {:?}", e);
@@ -391,13 +398,10 @@ impl<B: BlockT> ChainSync<B> {
391398
trace!(target: "sync", "Too many blocks in the queue.");
392399
return;
393400
}
394-
// we should not download already queued blocks
395-
let common_number = ::std::cmp::max(peer.common_number, import_status.best_importing_number);
396-
397-
trace!(target: "sync", "Considering new block download from {}, common block is {}, best is {:?}", who, common_number, peer.best_number);
398401
match peer.state {
399402
PeerSyncState::Available => {
400-
if let Some(range) = self.blocks.needed_blocks(who, MAX_BLOCKS_TO_REQUEST, peer.best_number, common_number) {
403+
trace!(target: "sync", "Considering new block download from {}, common block is {}, best is {:?}", who, peer.common_number, peer.best_number);
404+
if let Some(range) = self.blocks.needed_blocks(who, MAX_BLOCKS_TO_REQUEST, peer.best_number, peer.common_number) {
401405
trace!(target: "sync", "Requesting blocks from {}, ({} to {})", who, range.start, range.end);
402406
let request = message::generic::BlockRequest {
403407
id: 0,

0 commit comments

Comments
 (0)