Fix ancient blocks sync (#9531)
* Log block set in block_sync for easier debugging * logging macros * Match no args in sync logging macros * Add QueueFull error * Only allow importing headers if the first matches requested * WIP * Test for chain head gaps and log * Calc distance even with 2 heads * Revert previous commits, preparing simple fix This reverts commit 5f38aa885b22ebb0e3a1d60120cea69f9f322628. * Reject headers with no gaps when ChainHead * Reset block sync download when queue full * Simplify check for subchain heads * Add comment to explain subchain heads filter * Fix is_subchain_heads check and comment * Prevent premature round completion after restart This is a problem on mainnet where multiple stale peer requests will force many rounds to complete quickly, forcing the retraction. * Reset stale old blocks request after queue full * Revert "Reject headers with no gaps when ChainHead" This reverts commit 0eb865539e5dee37ab34f168f5fb643300de5ace. * Add BlockSet to BlockDownloader logging Currently it is difficult to debug this because there are two instances, one for OldBlocks and one for NewBlocks. This adds the BlockSet to all log messages for easy log filtering. * Reset OldBlocks download from last enqueued Previously when the ancient block queue was full it would restart the download from the last imported block, so the ones still in the queue would be redownloaded. Keeping the existing downloader instance and just resetting it will start again from the last enqueued block.:wq * Ignore expired Body and Receipt requests * Log when ancient block download being restarted * Only request old blocks from peers with >= difficulty https://github.com/paritytech/parity-ethereum/pull/9226 might be too permissive and causing the behaviour of the retraction soon after the fork block. With this change the peer difficulty has to be greater than or euqal to our syncing difficulty, so should still fix https://github.com/paritytech/parity-ethereum/issues/9225 * Some logging and clear stalled blocks head * Revert "Some logging and clear stalled blocks head" This reverts commit 757641d9b817ae8b63fec684759b0815af9c4d0e. * Reset stalled header if useless more than once * Store useless headers in HashSet * Add sync target to logging macro * Don't disable useless peer and fix log macro * Clear useless headers on reset and comments * Use custom error for collecting blocks Previously we resued BlockImportError, however only the Invalid case and this made little sense with the QueueFull error. * Remove blank line * Test for reset sync after consecutive useless headers * Don't reset after consecutive headers when chain head * Delete commented out imports * Return DownloadAction from collect_blocks instead of error * Don't reset after round complete, was causing test hangs * Add comment explaining reset after useless * Replace HashSet with counter for useless headers * Refactor sync reset on bad block/queue full * Add missing target for log message * Fix compiler errors and test after merge * ethcore: revert ethereum tests submodule update
This commit is contained in:
committed by
Afri Schoedon
parent
bc056c41bc
commit
4b6ebcbb61
@@ -293,7 +293,9 @@ impl SyncHandler {
|
||||
let block_set = sync.peers.get(&peer_id)
|
||||
.and_then(|p| p.block_set)
|
||||
.unwrap_or(BlockSet::NewBlocks);
|
||||
if !sync.reset_peer_asking(peer_id, PeerAsking::BlockBodies) {
|
||||
let allowed = sync.peers.get(&peer_id).map(|p| p.is_allowed()).unwrap_or(false);
|
||||
|
||||
if !sync.reset_peer_asking(peer_id, PeerAsking::BlockBodies) || !allowed {
|
||||
trace!(target: "sync", "{}: Ignored unexpected bodies", peer_id);
|
||||
return Ok(());
|
||||
}
|
||||
@@ -420,12 +422,8 @@ impl SyncHandler {
|
||||
downloader.import_headers(io, r, expected_hash)?
|
||||
};
|
||||
|
||||
if let DownloadAction::Reset = result {
|
||||
// mark all outstanding requests as expired
|
||||
trace!("Resetting downloads for {:?}", block_set);
|
||||
for (_, ref mut p) in sync.peers.iter_mut().filter(|&(_, ref p)| p.block_set == Some(block_set)) {
|
||||
p.reset_asking();
|
||||
}
|
||||
if result == DownloadAction::Reset {
|
||||
sync.reset_downloads(block_set);
|
||||
}
|
||||
|
||||
sync.collect_blocks(io, block_set);
|
||||
@@ -436,7 +434,8 @@ impl SyncHandler {
|
||||
fn on_peer_block_receipts(sync: &mut ChainSync, io: &mut SyncIo, peer_id: PeerId, r: &Rlp) -> Result<(), DownloaderImportError> {
|
||||
sync.clear_peer_download(peer_id);
|
||||
let block_set = sync.peers.get(&peer_id).and_then(|p| p.block_set).unwrap_or(BlockSet::NewBlocks);
|
||||
if !sync.reset_peer_asking(peer_id, PeerAsking::BlockReceipts) {
|
||||
let allowed = sync.peers.get(&peer_id).map(|p| p.is_allowed()).unwrap_or(false);
|
||||
if !sync.reset_peer_asking(peer_id, PeerAsking::BlockReceipts) || !allowed {
|
||||
trace!(target: "sync", "{}: Ignored unexpected receipts", peer_id);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ use ethcore::client::{BlockChainClient, BlockStatus, BlockId, BlockChainInfo, Bl
|
||||
use ethcore::snapshot::{RestorationStatus};
|
||||
use sync_io::SyncIo;
|
||||
use super::{WarpSync, SyncConfig};
|
||||
use block_sync::{BlockDownloader, BlockDownloaderImportError as DownloaderImportError};
|
||||
use block_sync::{BlockDownloader, DownloadAction};
|
||||
use rand::Rng;
|
||||
use snapshot::{Snapshot};
|
||||
use api::{EthProtocolInfo as PeerInfoDigest, WARP_SYNC_PROTOCOL_ID};
|
||||
@@ -429,7 +429,7 @@ impl ChainSync {
|
||||
peers: HashMap::new(),
|
||||
handshaking_peers: HashMap::new(),
|
||||
active_peers: HashSet::new(),
|
||||
new_blocks: BlockDownloader::new(false, &chain_info.best_block_hash, chain_info.best_block_number),
|
||||
new_blocks: BlockDownloader::new(BlockSet::NewBlocks, &chain_info.best_block_hash, chain_info.best_block_number),
|
||||
old_blocks: None,
|
||||
last_sent_block_number: 0,
|
||||
network_id: config.network_id,
|
||||
@@ -638,13 +638,13 @@ impl ChainSync {
|
||||
pub fn update_targets(&mut self, chain: &BlockChainClient) {
|
||||
// Do not assume that the block queue/chain still has our last_imported_block
|
||||
let chain = chain.chain_info();
|
||||
self.new_blocks = BlockDownloader::new(false, &chain.best_block_hash, chain.best_block_number);
|
||||
self.new_blocks = BlockDownloader::new(BlockSet::NewBlocks, &chain.best_block_hash, chain.best_block_number);
|
||||
self.old_blocks = None;
|
||||
if self.download_old_blocks {
|
||||
if let (Some(ancient_block_hash), Some(ancient_block_number)) = (chain.ancient_block_hash, chain.ancient_block_number) {
|
||||
|
||||
trace!(target: "sync", "Downloading old blocks from {:?} (#{}) till {:?} (#{:?})", ancient_block_hash, ancient_block_number, chain.first_block_hash, chain.first_block_number);
|
||||
let mut downloader = BlockDownloader::with_unlimited_reorg(true, &ancient_block_hash, ancient_block_number);
|
||||
let mut downloader = BlockDownloader::new(BlockSet::OldBlocks, &ancient_block_hash, ancient_block_number);
|
||||
if let Some(hash) = chain.first_block_hash {
|
||||
trace!(target: "sync", "Downloader target set to {:?}", hash);
|
||||
downloader.set_target(&hash);
|
||||
@@ -763,12 +763,10 @@ impl ChainSync {
|
||||
}
|
||||
}
|
||||
|
||||
// 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()))
|
||||
});
|
||||
// Only ask for old blocks if the peer has an equal or higher difficulty
|
||||
let equal_or_higher_difficulty = peer_difficulty.map_or(false, |pd| pd >= syncing_difficulty);
|
||||
|
||||
if force || last_imported_old_block_difficulty.map_or(true, |ld| peer_difficulty.map_or(true, |pd| pd > ld)) {
|
||||
if force || equal_or_higher_difficulty {
|
||||
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;
|
||||
@@ -776,9 +774,9 @@ impl ChainSync {
|
||||
} else {
|
||||
trace!(
|
||||
target: "sync",
|
||||
"peer {:?} is not suitable for requesting old blocks, last_imported_old_block_difficulty={:?}, peer_difficulty={:?}",
|
||||
"peer {:?} is not suitable for requesting old blocks, syncing_difficulty={:?}, peer_difficulty={:?}",
|
||||
peer_id,
|
||||
last_imported_old_block_difficulty,
|
||||
syncing_difficulty,
|
||||
peer_difficulty
|
||||
);
|
||||
self.deactivate_peer(io, peer_id);
|
||||
@@ -856,18 +854,39 @@ impl ChainSync {
|
||||
fn collect_blocks(&mut self, io: &mut SyncIo, block_set: BlockSet) {
|
||||
match block_set {
|
||||
BlockSet::NewBlocks => {
|
||||
if self.new_blocks.collect_blocks(io, self.state == SyncState::NewBlocks) == Err(DownloaderImportError::Invalid) {
|
||||
self.restart(io);
|
||||
if self.new_blocks.collect_blocks(io, self.state == SyncState::NewBlocks) == DownloadAction::Reset {
|
||||
self.reset_downloads(block_set);
|
||||
self.new_blocks.reset();
|
||||
}
|
||||
},
|
||||
BlockSet::OldBlocks => {
|
||||
if self.old_blocks.as_mut().map_or(false, |downloader| { downloader.collect_blocks(io, false) == Err(DownloaderImportError::Invalid) }) {
|
||||
self.restart(io);
|
||||
} else if self.old_blocks.as_ref().map_or(false, |downloader| { downloader.is_complete() }) {
|
||||
let mut is_complete = false;
|
||||
let mut download_action = DownloadAction::None;
|
||||
if let Some(downloader) = self.old_blocks.as_mut() {
|
||||
download_action = downloader.collect_blocks(io, false);
|
||||
is_complete = downloader.is_complete();
|
||||
}
|
||||
|
||||
if download_action == DownloadAction::Reset {
|
||||
self.reset_downloads(block_set);
|
||||
if let Some(downloader) = self.old_blocks.as_mut() {
|
||||
downloader.reset();
|
||||
}
|
||||
}
|
||||
|
||||
if is_complete {
|
||||
trace!(target: "sync", "Background block download is complete");
|
||||
self.old_blocks = None;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Mark all outstanding requests as expired
|
||||
fn reset_downloads(&mut self, block_set: BlockSet) {
|
||||
trace!(target: "sync", "Resetting downloads for {:?}", block_set);
|
||||
for (_, ref mut p) in self.peers.iter_mut().filter(|&(_, ref p)| p.block_set == Some(block_set)) {
|
||||
p.reset_asking();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user