mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 09:21:05 +00:00
Delay reputation updates (#7214)
* Add futures-timer * Make cost_or_benefit public * Update ReportPeer message format * Add delay to reputation updates (dirtywork) * Update ReputationAggregator * Update tests * Fix flucky tests * Move reputation to state * Use the main loop for handling reputation sendings * Update * Move reputation to utils * Update reputation sending * Fix arguments order * Update state * Remove new from state * Add constant * Add failing test for delay * Change mocking approach * Fix type errors * Fix comments * Add message handling to select * Fix bitfields-distribution tests * Add docs to reputation aggregator * Replace .into_base_rep * Use one REPUTATION_CHANGE_INTERVAL by default * Add reputation change to statement-distribution * Update polkadot-availability-bitfield-distribution * Update futures selecting in subsystems * Update reputation adding * Send malicious changes right away without adding to state * Add reputation to StatementDistributionSubsystem * Handle reputation in statement distribution * Add delay test for polkadot-statement-distribution * Fix collator-protocol tests before applying reputation delay * Remove into_base_rep * Add reputation to State * Fix failed tests * Add reputation delay * Update tests * Add batched network message for peer reporting * Update approval-distribution tests * Update bitfield-distribution tests * Update statement-distribution tests * Update collator-protocol tests * Remove levels in matching * Address clippy errors * Fix overseer test * Add a metric for original count of rep changes * Update Reputation * Revert "Add a metric for original count of rep changes" This reverts commit 6c9b0c1ec34491d16e562bdcba8db6b9dcf484db. * Update node/subsystem-util/src/reputation.rs Co-authored-by: Vsevolod Stakhov <vsevolod.stakhov@parity.io> * Remove redundant vec --------- Co-authored-by: Vsevolod Stakhov <vsevolod.stakhov@parity.io>
This commit is contained in:
@@ -24,13 +24,13 @@ use parity_scale_codec::Encode;
|
||||
use sc_network::{
|
||||
config::parse_addr, multiaddr::Multiaddr, types::ProtocolName, Event as NetworkEvent,
|
||||
IfDisconnected, NetworkEventStream, NetworkNotification, NetworkPeers, NetworkRequest,
|
||||
NetworkService, OutboundFailure, RequestFailure,
|
||||
NetworkService, OutboundFailure, ReputationChange, RequestFailure,
|
||||
};
|
||||
|
||||
use polkadot_node_network_protocol::{
|
||||
peer_set::{PeerSet, PeerSetProtocolNames, ProtocolVersion},
|
||||
request_response::{OutgoingRequest, Recipient, ReqProtocolNames, Requests},
|
||||
PeerId, UnifiedReputationChange as Rep,
|
||||
PeerId,
|
||||
};
|
||||
use polkadot_primitives::{AuthorityDiscoveryId, Block, Hash};
|
||||
|
||||
@@ -106,7 +106,7 @@ pub trait Network: Clone + Send + 'static {
|
||||
);
|
||||
|
||||
/// Report a given peer as either beneficial (+) or costly (-) according to the given scalar.
|
||||
fn report_peer(&self, who: PeerId, cost_benefit: Rep);
|
||||
fn report_peer(&self, who: PeerId, rep: ReputationChange);
|
||||
|
||||
/// Disconnect a given peer from the protocol specified without harming reputation.
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName);
|
||||
@@ -133,8 +133,8 @@ impl Network for Arc<NetworkService<Block, Hash>> {
|
||||
NetworkService::remove_peers_from_reserved_set(&**self, protocol, peers);
|
||||
}
|
||||
|
||||
fn report_peer(&self, who: PeerId, cost_benefit: Rep) {
|
||||
NetworkService::report_peer(&**self, who, cost_benefit.into_base_rep());
|
||||
fn report_peer(&self, who: PeerId, rep: ReputationChange) {
|
||||
NetworkService::report_peer(&**self, who, rep);
|
||||
}
|
||||
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
|
||||
|
||||
@@ -364,7 +364,7 @@ where
|
||||
let v_messages = match v_messages {
|
||||
Err(rep) => {
|
||||
gum::debug!(target: LOG_TARGET, action = "ReportPeer");
|
||||
network_service.report_peer(remote, rep);
|
||||
network_service.report_peer(remote, rep.into());
|
||||
|
||||
continue
|
||||
},
|
||||
@@ -395,7 +395,7 @@ where
|
||||
let c_messages = match c_messages {
|
||||
Err(rep) => {
|
||||
gum::debug!(target: LOG_TARGET, action = "ReportPeer");
|
||||
network_service.report_peer(remote, rep);
|
||||
network_service.report_peer(remote, rep.into());
|
||||
|
||||
continue
|
||||
},
|
||||
@@ -441,7 +441,7 @@ where
|
||||
};
|
||||
|
||||
for report in reports {
|
||||
network_service.report_peer(remote, report);
|
||||
network_service.report_peer(remote, report.into());
|
||||
}
|
||||
|
||||
dispatch_validation_events_to_all(events, &mut sender).await;
|
||||
@@ -474,7 +474,7 @@ where
|
||||
};
|
||||
|
||||
for report in reports {
|
||||
network_service.report_peer(remote, report);
|
||||
network_service.report_peer(remote, report.into());
|
||||
}
|
||||
|
||||
dispatch_collation_events_to_all(events, &mut sender).await;
|
||||
|
||||
@@ -27,7 +27,7 @@ use std::{
|
||||
sync::atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName};
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName, ReputationChange};
|
||||
|
||||
use polkadot_node_network_protocol::{
|
||||
peer_set::PeerSetProtocolNames,
|
||||
@@ -51,12 +51,12 @@ use polkadot_primitives::{AuthorityDiscoveryId, Hash};
|
||||
use sc_network::Multiaddr;
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
|
||||
use crate::{network::Network, validator_discovery::AuthorityDiscovery, Rep};
|
||||
use crate::{network::Network, validator_discovery::AuthorityDiscovery};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum NetworkAction {
|
||||
/// Note a change in reputation for a peer.
|
||||
ReputationChange(PeerId, Rep),
|
||||
ReputationChange(PeerId, ReputationChange),
|
||||
/// Disconnect a peer from the given peer-set.
|
||||
DisconnectPeer(PeerId, PeerSet),
|
||||
/// Write a notification to a given peer on the given peer-set.
|
||||
@@ -128,10 +128,10 @@ impl Network for TestNetwork {
|
||||
) {
|
||||
}
|
||||
|
||||
fn report_peer(&self, who: PeerId, cost_benefit: Rep) {
|
||||
fn report_peer(&self, who: PeerId, rep: ReputationChange) {
|
||||
self.action_tx
|
||||
.lock()
|
||||
.unbounded_send(NetworkAction::ReputationChange(who, cost_benefit))
|
||||
.unbounded_send(NetworkAction::ReputationChange(who, rep))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -947,7 +947,7 @@ fn relays_collation_protocol_messages() {
|
||||
let actions = network_handle.next_network_actions(3).await;
|
||||
assert_network_actions_contains(
|
||||
&actions,
|
||||
&NetworkAction::ReputationChange(peer_a.clone(), UNCONNECTED_PEERSET_COST),
|
||||
&NetworkAction::ReputationChange(peer_a.clone(), UNCONNECTED_PEERSET_COST.into()),
|
||||
);
|
||||
|
||||
// peer B has the message relayed.
|
||||
@@ -1142,7 +1142,7 @@ fn view_finalized_number_can_not_go_down() {
|
||||
let actions = network_handle.next_network_actions(2).await;
|
||||
assert_network_actions_contains(
|
||||
&actions,
|
||||
&NetworkAction::ReputationChange(peer_a.clone(), MALFORMED_VIEW_COST),
|
||||
&NetworkAction::ReputationChange(peer_a.clone(), MALFORMED_VIEW_COST.into()),
|
||||
);
|
||||
virtual_overseer
|
||||
});
|
||||
|
||||
@@ -24,14 +24,16 @@ use polkadot_node_network_protocol::{
|
||||
};
|
||||
|
||||
use polkadot_node_subsystem::{
|
||||
errors::SubsystemError, messages::NetworkBridgeTxMessage, overseer, FromOrchestra,
|
||||
OverseerSignal, SpawnedSubsystem,
|
||||
errors::SubsystemError,
|
||||
messages::{NetworkBridgeTxMessage, ReportPeerMessage},
|
||||
overseer, FromOrchestra, OverseerSignal, SpawnedSubsystem,
|
||||
};
|
||||
|
||||
/// Peer set info for network initialization.
|
||||
///
|
||||
/// To be added to [`NetworkConfiguration::extra_sets`].
|
||||
pub use polkadot_node_network_protocol::peer_set::{peer_sets_info, IsAuthority};
|
||||
use sc_network::ReputationChange;
|
||||
|
||||
use crate::validator_discovery;
|
||||
|
||||
@@ -148,14 +150,25 @@ where
|
||||
AD: validator_discovery::AuthorityDiscovery + Clone,
|
||||
{
|
||||
match msg {
|
||||
NetworkBridgeTxMessage::ReportPeer(peer, rep) => {
|
||||
if !rep.is_benefit() {
|
||||
NetworkBridgeTxMessage::ReportPeer(ReportPeerMessage::Single(peer, rep)) => {
|
||||
if !rep.value.is_positive() {
|
||||
gum::debug!(target: LOG_TARGET, ?peer, ?rep, action = "ReportPeer");
|
||||
}
|
||||
|
||||
metrics.on_report_event();
|
||||
network_service.report_peer(peer, rep);
|
||||
},
|
||||
NetworkBridgeTxMessage::ReportPeer(ReportPeerMessage::Batch(batch)) => {
|
||||
for (peer, score) in batch {
|
||||
let rep = ReputationChange::new(score, "Aggregated reputation change");
|
||||
if !rep.value.is_positive() {
|
||||
gum::debug!(target: LOG_TARGET, ?peer, ?rep, action = "ReportPeer");
|
||||
}
|
||||
|
||||
metrics.on_report_event();
|
||||
network_service.report_peer(peer, rep);
|
||||
}
|
||||
},
|
||||
NetworkBridgeTxMessage::DisconnectPeer(peer, peer_set) => {
|
||||
gum::trace!(
|
||||
target: LOG_TARGET,
|
||||
|
||||
@@ -22,7 +22,7 @@ use async_trait::async_trait;
|
||||
use parking_lot::Mutex;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName};
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName, ReputationChange};
|
||||
|
||||
use polkadot_node_network_protocol::{
|
||||
peer_set::PeerSetProtocolNames,
|
||||
@@ -39,12 +39,12 @@ use sp_keyring::Sr25519Keyring;
|
||||
|
||||
const TIMEOUT: std::time::Duration = polkadot_node_subsystem_test_helpers::TestSubsystemContextHandle::<NetworkBridgeTxMessage>::TIMEOUT;
|
||||
|
||||
use crate::{network::Network, validator_discovery::AuthorityDiscovery, Rep};
|
||||
use crate::{network::Network, validator_discovery::AuthorityDiscovery};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum NetworkAction {
|
||||
/// Note a change in reputation for a peer.
|
||||
ReputationChange(PeerId, Rep),
|
||||
ReputationChange(PeerId, ReputationChange),
|
||||
/// Disconnect a peer from the given peer-set.
|
||||
DisconnectPeer(PeerId, PeerSet),
|
||||
/// Write a notification to a given peer on the given peer-set.
|
||||
@@ -116,10 +116,10 @@ impl Network for TestNetwork {
|
||||
) {
|
||||
}
|
||||
|
||||
fn report_peer(&self, who: PeerId, cost_benefit: Rep) {
|
||||
fn report_peer(&self, who: PeerId, rep: ReputationChange) {
|
||||
self.action_tx
|
||||
.lock()
|
||||
.unbounded_send(NetworkAction::ReputationChange(who, cost_benefit))
|
||||
.unbounded_send(NetworkAction::ReputationChange(who, rep))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ mod tests {
|
||||
PeerId,
|
||||
};
|
||||
use polkadot_primitives::Hash;
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName};
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName, ReputationChange};
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
@@ -249,7 +249,7 @@ mod tests {
|
||||
) {
|
||||
}
|
||||
|
||||
fn report_peer(&self, _: PeerId, _: crate::Rep) {
|
||||
fn report_peer(&self, _: PeerId, _: ReputationChange) {
|
||||
panic!()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user