mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 22:41:02 +00:00
Reputation changes requires reason (#4277)
This commit is contained in:
committed by
Gavin Wood
parent
5edc4350b4
commit
5ec0923285
@@ -84,7 +84,7 @@
|
||||
|
||||
use sp_runtime::traits::{NumberFor, Block as BlockT, Zero};
|
||||
use network::consensus_gossip::{self as network_gossip, MessageIntent, ValidatorContext};
|
||||
use network::{config::Roles, PeerId};
|
||||
use network::{config::Roles, PeerId, ReputationChange};
|
||||
use codec::{Encode, Decode};
|
||||
use fg_primitives::AuthorityId;
|
||||
|
||||
@@ -114,7 +114,7 @@ const ROUND_DURATION: u32 = 4; // measured in gossip durations
|
||||
|
||||
const MIN_LUCKY: usize = 5;
|
||||
|
||||
type Report = (PeerId, i32);
|
||||
type Report = (PeerId, ReputationChange);
|
||||
|
||||
/// An outcome of examining a message.
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
@@ -384,14 +384,19 @@ pub(super) enum Misbehavior {
|
||||
}
|
||||
|
||||
impl Misbehavior {
|
||||
pub(super) fn cost(&self) -> i32 {
|
||||
pub(super) fn cost(&self) -> ReputationChange {
|
||||
use Misbehavior::*;
|
||||
|
||||
match *self {
|
||||
InvalidViewChange => cost::INVALID_VIEW_CHANGE,
|
||||
UndecodablePacket(bytes) => bytes.saturating_mul(cost::PER_UNDECODABLE_BYTE),
|
||||
BadCatchUpMessage { signatures_checked } =>
|
||||
UndecodablePacket(bytes) => ReputationChange::new(
|
||||
bytes.saturating_mul(cost::PER_UNDECODABLE_BYTE),
|
||||
"Grandpa: Bad packet",
|
||||
),
|
||||
BadCatchUpMessage { signatures_checked } => ReputationChange::new(
|
||||
cost::PER_SIGNATURE_CHECKED.saturating_mul(signatures_checked),
|
||||
"Grandpa: Bad cath-up message",
|
||||
),
|
||||
BadCommitMessage { signatures_checked, blocks_loaded, equivocations_caught } => {
|
||||
let cost = cost::PER_SIGNATURE_CHECKED
|
||||
.saturating_mul(signatures_checked)
|
||||
@@ -399,7 +404,7 @@ impl Misbehavior {
|
||||
|
||||
let benefit = equivocations_caught.saturating_mul(benefit::PER_EQUIVOCATION);
|
||||
|
||||
(benefit as i32).saturating_add(cost as i32)
|
||||
ReputationChange::new((benefit as i32).saturating_add(cost as i32), "Grandpa: Bad commit")
|
||||
},
|
||||
FutureMessage => cost::FUTURE_MESSAGE,
|
||||
OutOfScopeMessage => cost::OUT_OF_SCOPE_MESSAGE,
|
||||
@@ -551,11 +556,11 @@ impl<N: Ord> Peers<N> {
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(super) enum Action<H> {
|
||||
// repropagate under given topic, to the given peers, applying cost/benefit to originator.
|
||||
Keep(H, i32),
|
||||
Keep(H, ReputationChange),
|
||||
// discard and process.
|
||||
ProcessAndDiscard(H, i32),
|
||||
ProcessAndDiscard(H, ReputationChange),
|
||||
// discard, applying cost/benefit to originator.
|
||||
Discard(i32),
|
||||
Discard(ReputationChange),
|
||||
}
|
||||
|
||||
/// State of catch up request handling.
|
||||
@@ -731,7 +736,7 @@ impl<Block: BlockT> Inner<Block> {
|
||||
.unwrap_or(Consider::RejectOutOfScope)
|
||||
}
|
||||
|
||||
fn cost_past_rejection(&self, _who: &PeerId, _round: Round, _set_id: SetId) -> i32 {
|
||||
fn cost_past_rejection(&self, _who: &PeerId, _round: Round, _set_id: SetId) -> ReputationChange {
|
||||
// hardcoded for now.
|
||||
cost::PAST_REJECTION
|
||||
}
|
||||
@@ -783,7 +788,6 @@ impl<Block: BlockT> Inner<Block> {
|
||||
return Action::Discard(self.cost_past_rejection(who, full.round, full.set_id)),
|
||||
Consider::RejectOutOfScope => return Action::Discard(Misbehavior::OutOfScopeMessage.cost()),
|
||||
Consider::Accept => {},
|
||||
|
||||
}
|
||||
|
||||
if full.message.precommits.len() != full.message.auth_data.len() || full.message.precommits.is_empty() {
|
||||
@@ -1221,7 +1225,7 @@ impl<Block: BlockT> GossipValidator<Block> {
|
||||
self.inner.write().note_catch_up_message_processed();
|
||||
}
|
||||
|
||||
fn report(&self, who: PeerId, cost_benefit: i32) {
|
||||
fn report(&self, who: PeerId, cost_benefit: ReputationChange) {
|
||||
let _ = self.report_sender.unbounded_send(PeerReport { who, cost_benefit });
|
||||
}
|
||||
|
||||
@@ -1443,7 +1447,7 @@ impl<Block: BlockT> network_gossip::Validator<Block> for GossipValidator<Block>
|
||||
|
||||
struct PeerReport {
|
||||
who: PeerId,
|
||||
cost_benefit: i32,
|
||||
cost_benefit: ReputationChange,
|
||||
}
|
||||
|
||||
// wrapper around a stream of reports.
|
||||
|
||||
@@ -35,7 +35,7 @@ use futures03::stream::{StreamExt, TryStreamExt};
|
||||
use grandpa::Message::{Prevote, Precommit, PrimaryPropose};
|
||||
use grandpa::{voter, voter_set::VoterSet};
|
||||
use log::{debug, trace};
|
||||
use network::{consensus_gossip as network_gossip, NetworkService};
|
||||
use network::{consensus_gossip as network_gossip, NetworkService, ReputationChange};
|
||||
use network_gossip::ConsensusMessage;
|
||||
use codec::{Encode, Decode};
|
||||
use primitives::Pair;
|
||||
@@ -65,33 +65,35 @@ pub use fg_primitives::GRANDPA_ENGINE_ID;
|
||||
|
||||
// cost scalars for reporting peers.
|
||||
mod cost {
|
||||
pub(super) const PAST_REJECTION: i32 = -50;
|
||||
pub(super) const BAD_SIGNATURE: i32 = -100;
|
||||
pub(super) const MALFORMED_CATCH_UP: i32 = -1000;
|
||||
pub(super) const MALFORMED_COMMIT: i32 = -1000;
|
||||
pub(super) const FUTURE_MESSAGE: i32 = -500;
|
||||
pub(super) const UNKNOWN_VOTER: i32 = -150;
|
||||
use network::ReputationChange as Rep;
|
||||
pub(super) const PAST_REJECTION: Rep = Rep::new(-50, "Grandpa: Past message");
|
||||
pub(super) const BAD_SIGNATURE: Rep = Rep::new(-100, "Grandpa: Bad signature");
|
||||
pub(super) const MALFORMED_CATCH_UP: Rep = Rep::new(-1000, "Grandpa: Malformed cath-up");
|
||||
pub(super) const MALFORMED_COMMIT: Rep = Rep::new(-1000, "Grandpa: Malformed commit");
|
||||
pub(super) const FUTURE_MESSAGE: Rep = Rep::new(-500, "Grandpa: Future message");
|
||||
pub(super) const UNKNOWN_VOTER: Rep = Rep::new(-150, "Grandpa: Uknown voter");
|
||||
|
||||
pub(super) const INVALID_VIEW_CHANGE: i32 = -500;
|
||||
pub(super) const INVALID_VIEW_CHANGE: Rep = Rep::new(-500, "Grandpa: Invalid view change");
|
||||
pub(super) const PER_UNDECODABLE_BYTE: i32 = -5;
|
||||
pub(super) const PER_SIGNATURE_CHECKED: i32 = -25;
|
||||
pub(super) const PER_BLOCK_LOADED: i32 = -10;
|
||||
pub(super) const INVALID_CATCH_UP: i32 = -5000;
|
||||
pub(super) const INVALID_COMMIT: i32 = -5000;
|
||||
pub(super) const OUT_OF_SCOPE_MESSAGE: i32 = -500;
|
||||
pub(super) const CATCH_UP_REQUEST_TIMEOUT: i32 = -200;
|
||||
pub(super) const INVALID_CATCH_UP: Rep = Rep::new(-5000, "Grandpa: Invalid catch-up");
|
||||
pub(super) const INVALID_COMMIT: Rep = Rep::new(-5000, "Grandpa: Invalid commit");
|
||||
pub(super) const OUT_OF_SCOPE_MESSAGE: Rep = Rep::new(-500, "Grandpa: Out-of-scope message");
|
||||
pub(super) const CATCH_UP_REQUEST_TIMEOUT: Rep = Rep::new(-200, "Grandpa: Catch-up reqeust timeout");
|
||||
|
||||
// cost of answering a catch up request
|
||||
pub(super) const CATCH_UP_REPLY: i32 = -200;
|
||||
pub(super) const HONEST_OUT_OF_SCOPE_CATCH_UP: i32 = -200;
|
||||
pub(super) const CATCH_UP_REPLY: Rep = Rep::new(-200, "Grandpa: Catch-up reply");
|
||||
pub(super) const HONEST_OUT_OF_SCOPE_CATCH_UP: Rep = Rep::new(-200, "Grandpa: Out-of-scope catch-up");
|
||||
}
|
||||
|
||||
// benefit scalars for reporting peers.
|
||||
mod benefit {
|
||||
pub(super) const NEIGHBOR_MESSAGE: i32 = 100;
|
||||
pub(super) const ROUND_MESSAGE: i32 = 100;
|
||||
pub(super) const BASIC_VALIDATED_CATCH_UP: i32 = 200;
|
||||
pub(super) const BASIC_VALIDATED_COMMIT: i32 = 100;
|
||||
use network::ReputationChange as Rep;
|
||||
pub(super) const NEIGHBOR_MESSAGE: Rep = Rep::new(100, "Grandpa: Neighbor message");
|
||||
pub(super) const ROUND_MESSAGE: Rep = Rep::new(100, "Grandpa: Round message");
|
||||
pub(super) const BASIC_VALIDATED_CATCH_UP: Rep = Rep::new(200, "Grandpa: Catch-up message");
|
||||
pub(super) const BASIC_VALIDATED_COMMIT: Rep = Rep::new(100, "Grandpa: Commit");
|
||||
pub(super) const PER_EQUIVOCATION: i32 = 10;
|
||||
}
|
||||
|
||||
@@ -125,7 +127,7 @@ pub trait Network<Block: BlockT>: Clone + Send + 'static {
|
||||
fn send_message(&self, who: Vec<network::PeerId>, data: Vec<u8>);
|
||||
|
||||
/// Report a peer's cost or benefit after some action.
|
||||
fn report(&self, who: network::PeerId, cost_benefit: i32);
|
||||
fn report(&self, who: network::PeerId, cost_benefit: ReputationChange);
|
||||
|
||||
/// Inform peers that a block with given hash should be downloaded.
|
||||
fn announce(&self, block: Block::Hash, associated_data: Vec<u8>);
|
||||
@@ -217,7 +219,7 @@ impl<B, S, H> Network<B> for Arc<NetworkService<B, S, H>> where
|
||||
})
|
||||
}
|
||||
|
||||
fn report(&self, who: network::PeerId, cost_benefit: i32) {
|
||||
fn report(&self, who: network::PeerId, cost_benefit: ReputationChange) {
|
||||
self.report_peer(who, cost_benefit)
|
||||
}
|
||||
|
||||
@@ -803,7 +805,7 @@ fn check_compact_commit<Block: BlockT>(
|
||||
voters: &VoterSet<AuthorityId>,
|
||||
round: Round,
|
||||
set_id: SetId,
|
||||
) -> Result<(), i32> {
|
||||
) -> Result<(), ReputationChange> {
|
||||
// 4f + 1 = equivocations from f voters.
|
||||
let f = voters.total_weight() - voters.threshold();
|
||||
let full_threshold = voters.total_weight() + f;
|
||||
@@ -862,7 +864,7 @@ fn check_catch_up<Block: BlockT>(
|
||||
msg: &CatchUp<Block>,
|
||||
voters: &VoterSet<AuthorityId>,
|
||||
set_id: SetId,
|
||||
) -> Result<(), i32> {
|
||||
) -> Result<(), ReputationChange> {
|
||||
// 4f + 1 = equivocations from f voters.
|
||||
let f = voters.total_weight() - voters.threshold();
|
||||
let full_threshold = voters.total_weight() + f;
|
||||
@@ -872,7 +874,7 @@ fn check_catch_up<Block: BlockT>(
|
||||
voters: &'a VoterSet<AuthorityId>,
|
||||
votes: impl Iterator<Item=&'a AuthorityId>,
|
||||
full_threshold: u64,
|
||||
) -> Result<(), i32> {
|
||||
) -> Result<(), ReputationChange> {
|
||||
let mut total_weight = 0;
|
||||
|
||||
for id in votes {
|
||||
@@ -911,7 +913,7 @@ fn check_catch_up<Block: BlockT>(
|
||||
round: RoundNumber,
|
||||
set_id: SetIdNumber,
|
||||
mut signatures_checked: usize,
|
||||
) -> Result<usize, i32> where
|
||||
) -> Result<usize, ReputationChange> where
|
||||
B: BlockT,
|
||||
I: Iterator<Item=(Message<B>, &'a AuthorityId, &'a AuthoritySignature)>,
|
||||
{
|
||||
|
||||
@@ -37,7 +37,7 @@ enum Event {
|
||||
RegisterValidator(Arc<dyn network_gossip::Validator<Block>>),
|
||||
GossipMessage(Hash, Vec<u8>, bool),
|
||||
SendMessage(Vec<network::PeerId>, Vec<u8>),
|
||||
Report(network::PeerId, i32),
|
||||
Report(network::PeerId, network::ReputationChange),
|
||||
Announce(Hash),
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ impl super::Network<Block> for TestNetwork {
|
||||
}
|
||||
|
||||
/// Report a peer's cost or benefit after some action.
|
||||
fn report(&self, who: network::PeerId, cost_benefit: i32) {
|
||||
fn report(&self, who: network::PeerId, cost_benefit: network::ReputationChange) {
|
||||
let _ = self.sender.unbounded_send(Event::Report(who, cost_benefit));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user