Skip to content

Commit 9f3c67b

Browse files
nazar-pcbkchr
andauthored
Support querying peer reputation (#2392)
# Description Trivial change that resolves #2185. Since there was a mix of `who` and `peer_id` argument names nearby I changed them all to `peer_id`. # Checklist - [x] My PR includes a detailed description as outlined in the "Description" section above - [x] My PR follows the [labeling requirements](CONTRIBUTING.md#Process) of this project (at minimum one label for `T` required) - [x] I have made corresponding changes to the documentation (if applicable) - [ ] I have added tests that prove my fix is effective or that my feature works (if applicable) --------- Co-authored-by: Bastian Köcher <[email protected]>
1 parent cd63276 commit 9f3c67b

File tree

8 files changed

+60
-33
lines changed

8 files changed

+60
-33
lines changed

substrate/client/consensus/grandpa/src/communication/tests.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,15 @@ impl NetworkPeers for TestNetwork {
7575
unimplemented!();
7676
}
7777

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

82-
fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {}
82+
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
83+
unimplemented!()
84+
}
85+
86+
fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {}
8387

8488
fn accept_unreserved_peers(&self) {
8589
unimplemented!();

substrate/client/network-gossip/src/bridge.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,13 @@ mod tests {
394394
unimplemented!();
395395
}
396396

397-
fn report_peer(&self, _who: PeerId, _cost_benefit: ReputationChange) {}
397+
fn report_peer(&self, _peer_id: PeerId, _cost_benefit: ReputationChange) {}
398398

399-
fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
399+
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
400+
unimplemented!()
401+
}
402+
403+
fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {
400404
unimplemented!();
401405
}
402406

substrate/client/network-gossip/src/state_machine.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,15 @@ mod tests {
621621
unimplemented!();
622622
}
623623

624-
fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange) {
625-
self.inner.lock().unwrap().peer_reports.push((who, cost_benefit));
624+
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
625+
self.inner.lock().unwrap().peer_reports.push((peer_id, cost_benefit));
626626
}
627627

628-
fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
628+
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
629+
unimplemented!()
630+
}
631+
632+
fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {
629633
unimplemented!();
630634
}
631635

substrate/client/network/src/service.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ pub struct NetworkService<B: BlockT + 'static, H: ExHashT> {
120120
local_identity: Keypair,
121121
/// Bandwidth logging system. Can be queried to know the average bandwidth consumed.
122122
bandwidth: Arc<transport::BandwidthSinks>,
123+
/// Used to query and report reputation changes.
124+
peer_store_handle: PeerStoreHandle,
123125
/// Channel that sends messages to the actual worker.
124126
to_worker: TracingUnboundedSender<ServiceToWorkerMsg>,
125127
/// Protocol name -> `SetId` mapping for notification protocols. The map never changes after
@@ -130,8 +132,6 @@ pub struct NetworkService<B: BlockT + 'static, H: ExHashT> {
130132
protocol_handles: Vec<protocol_controller::ProtocolHandle>,
131133
/// Shortcut to sync protocol handle (`protocol_handles[0]`).
132134
sync_protocol_handle: protocol_controller::ProtocolHandle,
133-
/// Handle to `PeerStore`.
134-
peer_store_handle: PeerStoreHandle,
135135
/// Marker to pin the `H` generic. Serves no purpose except to not break backwards
136136
/// compatibility.
137137
_marker: PhantomData<H>,
@@ -865,12 +865,18 @@ where
865865
.unbounded_send(ServiceToWorkerMsg::AddKnownAddress(peer_id, addr));
866866
}
867867

868-
fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange) {
869-
let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::ReportPeer(who, cost_benefit));
868+
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
869+
self.peer_store_handle.clone().report_peer(peer_id, cost_benefit);
870+
}
871+
872+
fn peer_reputation(&self, peer_id: &PeerId) -> i32 {
873+
self.peer_store_handle.peer_reputation(peer_id)
870874
}
871875

872-
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
873-
let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::DisconnectPeer(who, protocol));
876+
fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName) {
877+
let _ = self
878+
.to_worker
879+
.unbounded_send(ServiceToWorkerMsg::DisconnectPeer(peer_id, protocol));
874880
}
875881

876882
fn accept_unreserved_peers(&self) {
@@ -1149,7 +1155,6 @@ enum ServiceToWorkerMsg {
11491155
GetValue(KademliaKey),
11501156
PutValue(KademliaKey, Vec<u8>),
11511157
AddKnownAddress(PeerId, Multiaddr),
1152-
ReportPeer(PeerId, ReputationChange),
11531158
EventStream(out_events::Sender),
11541159
Request {
11551160
target: PeerId,
@@ -1277,8 +1282,6 @@ where
12771282
self.network_service.behaviour_mut().put_value(key, value),
12781283
ServiceToWorkerMsg::AddKnownAddress(peer_id, addr) =>
12791284
self.network_service.behaviour_mut().add_known_address(peer_id, addr),
1280-
ServiceToWorkerMsg::ReportPeer(peer_id, reputation_change) =>
1281-
self.peer_store_handle.report_peer(peer_id, reputation_change),
12821285
ServiceToWorkerMsg::EventStream(sender) => self.event_streams.push(sender),
12831286
ServiceToWorkerMsg::Request {
12841287
target,

substrate/client/network/src/service/traits.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,15 @@ pub trait NetworkPeers {
155155

156156
/// Report a given peer as either beneficial (+) or costly (-) according to the
157157
/// given scalar.
158-
fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange);
158+
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange);
159+
160+
/// Get peer reputation.
161+
fn peer_reputation(&self, peer_id: &PeerId) -> i32;
159162

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

165168
/// Connect to unreserved peers and allow unreserved peers to connect for syncing purposes.
166169
fn accept_unreserved_peers(&self);
@@ -254,16 +257,16 @@ where
254257
T::add_known_address(self, peer_id, addr)
255258
}
256259

257-
fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange) {
258-
// TODO: when we get rid of `Peerset`, we'll likely need to add some kind of async
259-
// interface to `PeerStore`, otherwise we'll have trouble calling functions accepting
260-
// `&mut self` via `Arc`.
261-
// See https://github.com/paritytech/substrate/issues/14170.
262-
T::report_peer(self, who, cost_benefit)
260+
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
261+
T::report_peer(self, peer_id, cost_benefit)
262+
}
263+
264+
fn peer_reputation(&self, peer_id: &PeerId) -> i32 {
265+
T::peer_reputation(self, peer_id)
263266
}
264267

265-
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
266-
T::disconnect_peer(self, who, protocol)
268+
fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName) {
269+
T::disconnect_peer(self, peer_id, protocol)
267270
}
268271

269272
fn accept_unreserved_peers(&self) {

substrate/client/network/sync/src/service/mock.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ mockall::mock! {
8484
fn set_authorized_peers(&self, peers: HashSet<PeerId>);
8585
fn set_authorized_only(&self, reserved_only: bool);
8686
fn add_known_address(&self, peer_id: PeerId, addr: Multiaddr);
87-
fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange);
88-
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName);
87+
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange);
88+
fn peer_reputation(&self, peer_id: &PeerId) -> i32;
89+
fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName);
8990
fn accept_unreserved_peers(&self);
9091
fn deny_unreserved_peers(&self);
9192
fn add_reserved_peer(&self, peer: MultiaddrWithPeerId) -> Result<(), String>;

substrate/client/offchain/src/api.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,15 @@ mod tests {
243243
unimplemented!();
244244
}
245245

246-
fn report_peer(&self, _who: PeerId, _cost_benefit: ReputationChange) {
246+
fn report_peer(&self, _peer_id: PeerId, _cost_benefit: ReputationChange) {
247247
unimplemented!();
248248
}
249249

250-
fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
250+
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
251+
unimplemented!()
252+
}
253+
254+
fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {
251255
unimplemented!();
252256
}
253257

substrate/client/offchain/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,15 @@ mod tests {
374374
unimplemented!();
375375
}
376376

377-
fn report_peer(&self, _who: PeerId, _cost_benefit: ReputationChange) {
377+
fn report_peer(&self, _peer_id: PeerId, _cost_benefit: ReputationChange) {
378378
unimplemented!();
379379
}
380380

381-
fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
381+
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
382+
unimplemented!()
383+
}
384+
385+
fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {
382386
unimplemented!();
383387
}
384388

0 commit comments

Comments
 (0)