Skip to content
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
85 changes: 50 additions & 35 deletions sync/src/block/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::mem::discriminant;
use std::sync::Arc;
use std::time::Duration;

Expand Down Expand Up @@ -173,6 +174,48 @@ impl Extension {
}
}

fn send_status(&mut self, id: &NodeId) {
if discriminant(&self.state) != discriminant(&State::Full) {
return
}

let chain_info = self.client.chain_info();
self.api.send(
id,
Arc::new(
Message::Status {
nonce: U256::from(self.nonce),
best_hash: chain_info.best_proposal_block_hash,
genesis_hash: chain_info.genesis_hash,
}
.rlp_bytes(),
),
);
self.nonce += 1;
}

fn send_status_broadcast(&mut self) {
if discriminant(&self.state) != discriminant(&State::Full) {
return
}

let chain_info = self.client.chain_info();
for id in self.connected_nodes.iter() {
self.api.send(
id,
Arc::new(
Message::Status {
nonce: U256::from(self.nonce),
best_hash: chain_info.best_proposal_block_hash,
genesis_hash: chain_info.genesis_hash,
}
.rlp_bytes(),
),
);
self.nonce += 1;
}
}

fn send_header_request(&mut self, id: &NodeId, request: RequestMessage) {
if let Some(requests) = self.requests.get_mut(id) {
ctrace!(SYNC, "Send header request to {}", id);
Expand Down Expand Up @@ -325,19 +368,8 @@ impl NetworkExtension<Event> for Extension {

fn on_node_added(&mut self, id: &NodeId, _version: u64) {
cinfo!(SYNC, "New peer detected #{}", id);
let chain_info = self.client.chain_info();
self.api.send(
id,
Arc::new(
Message::Status {
nonce: U256::from(self.nonce),
best_hash: chain_info.best_proposal_block_hash,
genesis_hash: chain_info.genesis_hash,
}
.rlp_bytes(),
),
);
self.nonce += 1;
self.send_status(id);

let t = self.connected_nodes.insert(*id);
debug_assert!(t, "{} is already added to peer list", id);

Expand Down Expand Up @@ -540,11 +572,8 @@ pub enum Event {
impl Extension {
fn new_headers(&mut self, imported: Vec<BlockHash>, enacted: Vec<BlockHash>, retracted: Vec<BlockHash>) {
if let State::Full = self.state {
let peer_ids: Vec<_> = self.header_downloaders.keys().cloned().collect();
for id in peer_ids {
if let Some(peer) = self.header_downloaders.get_mut(&id) {
peer.mark_as_imported(imported.clone());
}
for peer in self.header_downloaders.values_mut() {
peer.mark_as_imported(imported.clone());
}
let mut headers_to_download: Vec<_> = enacted
.into_iter()
Expand Down Expand Up @@ -575,22 +604,7 @@ impl Extension {
self.body_downloader.remove_target(&imported);
self.body_downloader.remove_target(&invalid);

let chain_info = self.client.chain_info();

for id in &self.connected_nodes {
self.api.send(
id,
Arc::new(
Message::Status {
nonce: U256::from(self.nonce),
best_hash: chain_info.best_proposal_block_hash,
genesis_hash: chain_info.genesis_hash,
}
.rlp_bytes(),
),
);
self.nonce += 1;
}
self.send_status_broadcast();
}
}

Expand Down Expand Up @@ -618,7 +632,7 @@ impl Extension {
}

fn on_peer_request(&self, from: &NodeId, id: u64, request: RequestMessage) {
if !self.header_downloaders.contains_key(from) {
if !self.connected_nodes.contains(from) {
cinfo!(SYNC, "Request from invalid peer #{} received", from);
return
}
Expand Down Expand Up @@ -1049,6 +1063,7 @@ impl Extension {
downloader.update_pivot(best_hash);
}
self.state = State::Full;
self.send_status_broadcast();
}
}

Expand Down