Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Allow download of old blocks from peers with equal or lower difficulty #9226

Merged
merged 1 commit into from
Jul 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions ethcore/sync/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,14 +761,24 @@ impl ChainSync {
}
}

// Only ask for old blocks if the peer has a higher difficulty
if force || higher_difficulty {
// Only ask for old blocks if the peer has a higher difficulty than the last imported old block
let last_imported_old_block_difficulty = self.old_blocks.as_mut().and_then(|d| {
io.chain().block_total_difficulty(BlockId::Number(d.last_imported_block_number()))
});

if force || last_imported_old_block_difficulty.map_or(true, |ld| peer_difficulty.map_or(true, |pd| pd > ld)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should request old blocks if the peer's difficulty is unknown, or our last imported old block diff. is unknown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of the peer's difficulty being unknown, I was copying the existing behaviour: https://github.com/paritytech/parity-ethereum/blob/82a7ed2f912f22df78ee2417964f82d5a2ecfc43/ethcore/sync/src/chain/mod.rs#L731. So I guess we're being optimistic and sending the request anyway, we're assuming that most of the time we know the peer difficulty.

Not so sure about the case where the imported block diff is not known. That probably would mean (assuming old_blocks is Some) that last_imported_block_number has not been imported for whatever reason. So what to do in that case? Better to send the request anyway or not? Might be okay since it should almost always be Some, and in case it is None it might be okay to ask for it again.

if let Some(request) = self.old_blocks.as_mut().and_then(|d| d.request_blocks(io, num_active_peers)) {
SyncRequester::request_blocks(self, io, peer_id, request, BlockSet::OldBlocks);
return;
}
} else {
trace!(target: "sync", "peer {} is not suitable for asking old blocks", peer_id);
trace!(
target: "sync",
"peer {:?} is not suitable for requesting old blocks, last_imported_old_block_difficulty={:?}, peer_difficulty={:?}",
peer_id,
last_imported_old_block_difficulty,
peer_difficulty
);
self.deactivate_peer(io, peer_id);
}
},
Expand Down