Skip to content

Commit

Permalink
Support querying peer reputation
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Nov 18, 2023
1 parent 794ee98 commit ecd99a5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 27 deletions.
10 changes: 7 additions & 3 deletions substrate/client/consensus/grandpa/src/communication/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ impl NetworkPeers for TestNetwork {
unimplemented!();
}

fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange) {
let _ = self.sender.unbounded_send(Event::Report(who, cost_benefit));
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
let _ = self.sender.unbounded_send(Event::Report(peer_id, cost_benefit));
}

fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {}
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
unimplemented!()
}

fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {}

fn accept_unreserved_peers(&self) {
unimplemented!();
Expand Down
8 changes: 6 additions & 2 deletions substrate/client/network-gossip/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,13 @@ mod tests {
unimplemented!();
}

fn report_peer(&self, _who: PeerId, _cost_benefit: ReputationChange) {}
fn report_peer(&self, _peer_id: PeerId, _cost_benefit: ReputationChange) {}

fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
unimplemented!()
}

fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {
unimplemented!();
}

Expand Down
10 changes: 7 additions & 3 deletions substrate/client/network-gossip/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,15 @@ mod tests {
unimplemented!();
}

fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange) {
self.inner.lock().unwrap().peer_reports.push((who, cost_benefit));
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
self.inner.lock().unwrap().peer_reports.push((peer_id, cost_benefit));
}

fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
unimplemented!()
}

fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {
unimplemented!();
}

Expand Down
20 changes: 13 additions & 7 deletions substrate/client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ pub struct NetworkService<B: BlockT + 'static, H: ExHashT> {
local_identity: Keypair,
/// Bandwidth logging system. Can be queried to know the average bandwidth consumed.
bandwidth: Arc<transport::BandwidthSinks>,
/// Used to query and report reputation changes.
peer_store_handle: PeerStoreHandle,
/// Channel that sends messages to the actual worker.
to_worker: TracingUnboundedSender<ServiceToWorkerMsg>,
/// For each peer and protocol combination, an object that allows sending notifications to
Expand Down Expand Up @@ -527,6 +529,7 @@ where
sync_protocol_handle,
_marker: PhantomData,
_block: Default::default(),
peer_store_handle: params.peer_store.clone(),
});

Ok(NetworkWorker {
Expand Down Expand Up @@ -871,12 +874,18 @@ where
.unbounded_send(ServiceToWorkerMsg::AddKnownAddress(peer_id, addr));
}

fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange) {
let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::ReportPeer(who, cost_benefit));
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
self.peer_store_handle.clone().report_peer(peer_id, cost_benefit);
}

fn peer_reputation(&self, peer_id: &PeerId) -> i32 {
self.peer_store_handle.peer_reputation(peer_id)
}

fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::DisconnectPeer(who, protocol));
fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName) {
let _ = self
.to_worker
.unbounded_send(ServiceToWorkerMsg::DisconnectPeer(peer_id, protocol));
}

fn accept_unreserved_peers(&self) {
Expand Down Expand Up @@ -1193,7 +1202,6 @@ enum ServiceToWorkerMsg {
GetValue(KademliaKey),
PutValue(KademliaKey, Vec<u8>),
AddKnownAddress(PeerId, Multiaddr),
ReportPeer(PeerId, ReputationChange),
EventStream(out_events::Sender),
Request {
target: PeerId,
Expand Down Expand Up @@ -1324,8 +1332,6 @@ where
self.network_service.behaviour_mut().put_value(key, value),
ServiceToWorkerMsg::AddKnownAddress(peer_id, addr) =>
self.network_service.behaviour_mut().add_known_address(peer_id, addr),
ServiceToWorkerMsg::ReportPeer(peer_id, reputation_change) =>
self.peer_store_handle.report_peer(peer_id, reputation_change),
ServiceToWorkerMsg::EventStream(sender) => self.event_streams.push(sender),
ServiceToWorkerMsg::Request {
target,
Expand Down
19 changes: 13 additions & 6 deletions substrate/client/network/src/service/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,15 @@ pub trait NetworkPeers {

/// Report a given peer as either beneficial (+) or costly (-) according to the
/// given scalar.
fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange);
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange);

/// Get peer reputation.
fn peer_reputation(&self, peer_id: &PeerId) -> i32;

/// Disconnect from a node as soon as possible.
///
/// This triggers the same effects as if the connection had closed itself spontaneously.
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName);
fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName);

/// Connect to unreserved peers and allow unreserved peers to connect for syncing purposes.
fn accept_unreserved_peers(&self);
Expand Down Expand Up @@ -241,16 +244,20 @@ where
T::add_known_address(self, peer_id, addr)
}

fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange) {
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
// TODO: when we get rid of `Peerset`, we'll likely need to add some kind of async
// interface to `PeerStore`, otherwise we'll have trouble calling functions accepting
// `&mut self` via `Arc`.
// See https://github.com/paritytech/substrate/issues/14170.
T::report_peer(self, who, cost_benefit)
T::report_peer(self, peer_id, cost_benefit)
}

fn peer_reputation(&self, peer_id: &PeerId) -> i32 {
T::peer_reputation(self, peer_id)
}

fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
T::disconnect_peer(self, who, protocol)
fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName) {
T::disconnect_peer(self, peer_id, protocol)
}

fn accept_unreserved_peers(&self) {
Expand Down
5 changes: 3 additions & 2 deletions substrate/client/network/sync/src/service/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ mockall::mock! {
fn set_authorized_peers(&self, peers: HashSet<PeerId>);
fn set_authorized_only(&self, reserved_only: bool);
fn add_known_address(&self, peer_id: PeerId, addr: Multiaddr);
fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange);
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName);
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange);
fn peer_reputation(&self, peer_id: &PeerId) -> i32;
fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName);
fn accept_unreserved_peers(&self);
fn deny_unreserved_peers(&self);
fn add_reserved_peer(&self, peer: MultiaddrWithPeerId) -> Result<(), String>;
Expand Down
8 changes: 6 additions & 2 deletions substrate/client/offchain/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,15 @@ mod tests {
unimplemented!();
}

fn report_peer(&self, _who: PeerId, _cost_benefit: ReputationChange) {
fn report_peer(&self, _peer_id: PeerId, _cost_benefit: ReputationChange) {
unimplemented!();
}

fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
unimplemented!()
}

fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {
unimplemented!();
}

Expand Down
8 changes: 6 additions & 2 deletions substrate/client/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,15 @@ mod tests {
unimplemented!();
}

fn report_peer(&self, _who: PeerId, _cost_benefit: ReputationChange) {
fn report_peer(&self, _peer_id: PeerId, _cost_benefit: ReputationChange) {
unimplemented!();
}

fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
unimplemented!()
}

fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {
unimplemented!();
}

Expand Down

0 comments on commit ecd99a5

Please sign in to comment.