Skip to content
Snippets Groups Projects
Commit b31bcdfc authored by Arkadiy Paronyan's avatar Arkadiy Paronyan Committed by GitHub
Browse files

Keep peer info up to date (#2067)


* Keep peer info up to date

* Docs

Co-Authored-By: default avatararkpar <arkady.paronyan@gmail.com>
parent e6c73e89
Branches
No related merge requests found
...@@ -445,6 +445,20 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> { ...@@ -445,6 +445,20 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
None None
} }
fn update_peer_info(&mut self, who: NodeIndex) {
if let Some(info) = self.sync.peer_info(who) {
if let Some(ref mut peer) = self.context_data.peers.get_mut(&who) {
peer.info.best_hash = info.best_hash;
peer.info.best_number = info.best_number;
}
let mut peers = self.connected_peers.write();
if let Some(ref mut peer) = peers.get_mut(&who) {
peer.peer_info.best_hash = info.best_hash;
peer.peer_info.best_number = info.best_number;
}
}
}
/// Propagates protocol statuses. /// Propagates protocol statuses.
fn on_status(&mut self) { fn on_status(&mut self) {
let status = ProtocolStatus { let status = ProtocolStatus {
...@@ -467,9 +481,13 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> { ...@@ -467,9 +481,13 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
GenericMessage::BlockResponse(r) => { GenericMessage::BlockResponse(r) => {
if let Some(request) = self.handle_response(who, &r) { if let Some(request) = self.handle_response(who, &r) {
self.on_block_response(who, request, r); self.on_block_response(who, request, r);
self.update_peer_info(who);
} }
}, },
GenericMessage::BlockAnnounce(announce) => self.on_block_announce(who, announce), GenericMessage::BlockAnnounce(announce) => {
self.on_block_announce(who, announce);
self.update_peer_info(who);
},
GenericMessage::Transactions(m) => self.on_extrinsics(who, m), GenericMessage::Transactions(m) => self.on_extrinsics(who, m),
GenericMessage::RemoteCallRequest(request) => self.on_remote_call_request(who, request), GenericMessage::RemoteCallRequest(request) => self.on_remote_call_request(who, request),
GenericMessage::RemoteCallResponse(response) => self.on_remote_call_response(who, response), GenericMessage::RemoteCallResponse(response) => self.on_remote_call_response(who, response),
......
...@@ -58,6 +58,15 @@ struct PeerSync<B: BlockT> { ...@@ -58,6 +58,15 @@ struct PeerSync<B: BlockT> {
pub recently_announced: VecDeque<B::Hash>, pub recently_announced: VecDeque<B::Hash>,
} }
#[derive(Debug)]
/// Peer sync status.
pub(crate) struct PeerInfo<B: BlockT> {
/// Their best block hash.
pub best_hash: B::Hash,
/// Their best block number.
pub best_number: NumberFor<B>,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum AncestorSearchState<B: BlockT> { enum AncestorSearchState<B: BlockT> {
/// Use exponential backoff to find an ancestor, then switch to binary search. /// Use exponential backoff to find an ancestor, then switch to binary search.
...@@ -408,6 +417,16 @@ impl<B: BlockT> ChainSync<B> { ...@@ -408,6 +417,16 @@ impl<B: BlockT> ChainSync<B> {
} }
} }
/// Returns peer sync status (if any).
pub(crate) fn peer_info(&self, who: NodeIndex) -> Option<PeerInfo<B>> {
self.peers.get(&who).map(|peer| {
PeerInfo {
best_hash: peer.best_hash,
best_number: peer.best_number,
}
})
}
/// Returns sync status. /// Returns sync status.
pub(crate) fn status(&self) -> Status<B> { pub(crate) fn status(&self) -> Status<B> {
let best_seen = self.best_seen_block(); let best_seen = self.best_seen_block();
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment