Don't answer peers with a low reputation (#9008)

* Init architecture for not answering peers with a low reputation

* Get reputation inside of RequestResponsesBehaviour::poll

* Filter reputation in RequestResponsesBehaviour

* Pass PeersetHandle to RequestResponsesBehaviour

* Add more docs

* Fix tests compilation

* Fix compiler warnings (still FIXME)

* Fix tests

* Fmt code
This commit is contained in:
Roman
2021-09-21 14:32:13 +03:00
committed by GitHub
parent 0930948580
commit f6ab53f3c3
4 changed files with 192 additions and 47 deletions
+20 -2
View File
@@ -34,7 +34,7 @@
mod peersstate;
use futures::prelude::*;
use futures::{channel::oneshot, prelude::*};
use log::{debug, error, trace};
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use serde_json::json;
@@ -49,7 +49,7 @@ use wasm_timer::Delay;
pub use libp2p::PeerId;
/// We don't accept nodes whose reputation is under this value.
const BANNED_THRESHOLD: i32 = 82 * (i32::MIN / 100);
pub const BANNED_THRESHOLD: i32 = 82 * (i32::MIN / 100);
/// Reputation change for a node when we get disconnected from it.
const DISCONNECT_REPUTATION_CHANGE: i32 = -256;
/// Amount of time between the moment we disconnect from a node and the moment we remove it from
@@ -65,6 +65,7 @@ enum Action {
ReportPeer(PeerId, ReputationChange),
AddToPeersSet(SetId, PeerId),
RemoveFromPeersSet(SetId, PeerId),
PeerReputation(PeerId, oneshot::Sender<i32>),
}
/// Identifier of a set in the peerset.
@@ -165,6 +166,16 @@ impl PeersetHandle {
pub fn remove_from_peers_set(&self, set_id: SetId, peer_id: PeerId) {
let _ = self.tx.unbounded_send(Action::RemoveFromPeersSet(set_id, peer_id));
}
/// Returns the reputation value of the peer.
pub async fn peer_reputation(self, peer_id: PeerId) -> Result<i32, ()> {
let (tx, rx) = oneshot::channel();
let _ = self.tx.unbounded_send(Action::PeerReputation(peer_id, tx));
// The channel can only be closed if the peerset no longer exists.
rx.await.map_err(|_| ())
}
}
/// Message that can be sent by the peer set manager (PSM).
@@ -454,6 +465,11 @@ impl Peerset {
}
}
fn on_peer_reputation(&mut self, peer_id: PeerId, pending_response: oneshot::Sender<i32>) {
let reputation = self.data.peer_reputation(peer_id);
let _ = pending_response.send(reputation.reputation());
}
/// Updates the value of `self.latest_time_update` and performs all the updates that happen
/// over time, such as reputation increases for staying connected.
fn update_time(&mut self) {
@@ -744,6 +760,8 @@ impl Stream for Peerset {
self.add_to_peers_set(sets_name, peer_id),
Action::RemoveFromPeersSet(sets_name, peer_id) =>
self.on_remove_from_peers_set(sets_name, peer_id),
Action::PeerReputation(peer_id, pending_response) =>
self.on_peer_reputation(peer_id, pending_response),
}
}
}