Fixed sync skipping some block announcements (#8459)

* Fixed sync missing some block announcements

* Apply suggestions from code review

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Arkadiy Paronyan
2021-04-01 00:02:31 +03:00
committed by GitHub
parent 0e6481d01a
commit 484a630db9
4 changed files with 144 additions and 31 deletions
@@ -144,6 +144,26 @@ pub struct RemoteReadResponse {
pub proof: StorageProof,
}
/// Announcement summary used for debug logging.
#[derive(Debug)]
pub struct AnnouncementSummary<H: HeaderT> {
block_hash: H::Hash,
number: H::Number,
parent_hash: H::Hash,
state: Option<BlockState>,
}
impl<H: HeaderT> generic::BlockAnnounce<H> {
pub fn summary(&self) -> AnnouncementSummary<H> {
AnnouncementSummary {
block_hash: self.header.hash(),
number: *self.header.number(),
parent_hash: self.header.parent_hash().clone(),
state: self.state,
}
}
}
/// Generic types.
pub mod generic {
use bitflags::bitflags;
+39 -28
View File
@@ -505,9 +505,10 @@ impl<B: BlockT> ChainSync<B> {
}
}
/// Number of active sync requests.
/// Number of active forks requests. This includes
/// requests that are pending or could be issued right away.
pub fn num_sync_requests(&self) -> usize {
self.fork_targets.len()
self.fork_targets.values().filter(|f| f.number <= self.best_queued_number).count()
}
/// Number of downloaded blocks.
@@ -1421,23 +1422,36 @@ impl<B: BlockT> ChainSync<B> {
&mut self,
pre_validation_result: PreValidateBlockAnnounce<B::Header>,
) -> PollBlockAnnounceValidation<B::Header> {
trace!(
target: "sync",
"Finished block announce validation: {:?}",
pre_validation_result,
);
let (announce, is_best, who) = match pre_validation_result {
PreValidateBlockAnnounce::Failure { who, disconnect } => {
debug!(
target: "sync",
"Failed announce validation: {:?}, disconnect: {}",
who,
disconnect,
);
return PollBlockAnnounceValidation::Failure { who, disconnect }
},
PreValidateBlockAnnounce::Process { announce, is_new_best, who } => {
(announce, is_new_best, who)
},
PreValidateBlockAnnounce::Error { .. } | PreValidateBlockAnnounce::Skip =>
return PollBlockAnnounceValidation::Skip,
PreValidateBlockAnnounce::Error { .. } | PreValidateBlockAnnounce::Skip => {
debug!(
target: "sync",
"Ignored announce validation",
);
return PollBlockAnnounceValidation::Skip
},
};
trace!(
target: "sync",
"Finished block announce validation: from {:?}: {:?}. local_best={}",
who,
announce.summary(),
is_best,
);
let number = *announce.header.number();
let hash = announce.header.hash();
let parent_status = self.block_status(announce.header.parent_hash()).unwrap_or(BlockStatus::Unknown);
@@ -1508,25 +1522,22 @@ impl<B: BlockT> ChainSync<B> {
return PollBlockAnnounceValidation::ImportHeader { is_best, announce, who }
}
if number <= self.best_queued_number {
trace!(
target: "sync",
"Added sync target for block announced from {}: {} {:?}",
who,
hash,
announce.header,
);
self.fork_targets
.entry(hash.clone())
.or_insert_with(|| ForkTarget {
number,
parent_hash: Some(*announce.header.parent_hash()),
peers: Default::default(),
})
.peers.insert(who.clone());
}
trace!(
target: "sync",
"Added sync target for block announced from {}: {} {:?}",
who,
hash,
announce.summary(),
);
self.fork_targets
.entry(hash.clone())
.or_insert_with(|| ForkTarget {
number,
parent_hash: Some(*announce.header.parent_hash()),
peers: Default::default(),
})
.peers.insert(who.clone());
trace!(target: "sync", "Announce validation result is nothing");
PollBlockAnnounceValidation::Nothing { is_best, who, announce }
}