Minor sync refactoring (#2767)

* Make maintain_sync private

* Remove sync::Context::peer_info

* Print errors if sync state mismatch

* Line width
This commit is contained in:
Pierre Krieger
2019-06-05 09:15:54 +02:00
committed by Gavin Wood
parent 4d9efbc1d5
commit 9ad9f7eebd
2 changed files with 170 additions and 150 deletions
+7 -11
View File
@@ -119,7 +119,7 @@ pub struct ProtocolStatus<B: BlockT> {
}
/// Peer information
#[derive(Debug)]
#[derive(Debug, Clone)]
struct Peer<B: BlockT, H: ExHashT> {
info: PeerInfo<B>,
/// Current block request, if any.
@@ -342,10 +342,6 @@ impl<'a, B: BlockT + 'a, H: ExHashT + 'a> SyncContext<B> for ProtocolContext<'a,
self.network_out.disconnect_peer(who)
}
fn peer_info(&self, who: &PeerId) -> Option<PeerInfo<B>> {
self.context_data.peers.get(who).map(|p| p.info.clone())
}
fn client(&self) -> &dyn Client<B> {
&*self.context_data.chain
}
@@ -906,9 +902,10 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
status.version
};
let info = self.context_data.peers.get(&who).expect("We just inserted above; QED").info.clone();
self.on_demand_core.on_connect(&mut network_out, who.clone(), status.roles, status.best_number);
let mut context = ProtocolContext::new(&mut self.context_data, network_out);
self.sync.new_peer(&mut context, who.clone());
self.sync.new_peer(&mut context, who.clone(), info);
if protocol_version > 2 {
self.consensus_gossip.new_peer(&mut context, who.clone(), status.roles);
}
@@ -1188,16 +1185,15 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
processed_blocks: Vec<B::Hash>,
has_error: bool
) {
self.sync.blocks_processed(processed_blocks, has_error);
let mut context =
ProtocolContext::new(&mut self.context_data, network_out);
self.sync.maintain_sync(&mut context);
let mut context = ProtocolContext::new(&mut self.context_data, network_out);
self.sync.blocks_processed(&mut context, processed_blocks, has_error);
}
/// Restart the sync process.
pub fn restart(&mut self, network_out: &mut dyn NetworkOut<B>) {
let peers = self.context_data.peers.clone();
let mut context = ProtocolContext::new(&mut self.context_data, network_out);
self.sync.restart(&mut context);
self.sync.restart(&mut context, |peer_id| peers.get(peer_id).map(|i| i.info.clone()));
}
/// Notify about successful import of the given block.