mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-19 05:21:03 +00:00
positive updates to reputations when good things happen (#258)
* positive updates to reputations when good things happen * address review * branch on good or bad pov blocks * add a new constant for bad pov blocks
This commit is contained in:
committed by
Gavin Wood
parent
e8fcb43fcf
commit
c6aa447a50
+47
-15
@@ -80,6 +80,17 @@ mod cost {
|
|||||||
pub(super) const UNKNOWN_PEER: i32 = -50;
|
pub(super) const UNKNOWN_PEER: i32 = -50;
|
||||||
pub(super) const COLLATOR_ALREADY_KNOWN: i32 = -100;
|
pub(super) const COLLATOR_ALREADY_KNOWN: i32 = -100;
|
||||||
pub(super) const BAD_COLLATION: i32 = -1000;
|
pub(super) const BAD_COLLATION: i32 = -1000;
|
||||||
|
pub(super) const BAD_POV_BLOCK: i32 = -1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
mod benefit {
|
||||||
|
pub(super) const EXPECTED_MESSAGE: i32 = 20;
|
||||||
|
pub(super) const VALID_FORMAT: i32 = 20;
|
||||||
|
|
||||||
|
pub(super) const KNOWN_PEER: i32 = 5;
|
||||||
|
pub(super) const NEW_COLLATOR: i32 = 10;
|
||||||
|
pub(super) const GOOD_COLLATION: i32 = 100;
|
||||||
|
pub(super) const GOOD_POV_BLOCK: i32 = 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
type FullStatus = GenericFullStatus<Block>;
|
type FullStatus = GenericFullStatus<Block>;
|
||||||
@@ -382,6 +393,7 @@ impl PolkadotProtocol {
|
|||||||
ctx.report_peer(who, cost::UNEXPECTED_MESSAGE);
|
ctx.report_peer(who, cost::UNEXPECTED_MESSAGE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
ctx.report_peer(who.clone(), benefit::EXPECTED_MESSAGE);
|
||||||
|
|
||||||
let local_collations = &mut self.local_collations;
|
let local_collations = &mut self.local_collations;
|
||||||
let new_collations = match info.validator_keys.insert(key.clone()) {
|
let new_collations = match info.validator_keys.insert(key.clone()) {
|
||||||
@@ -418,10 +430,21 @@ impl PolkadotProtocol {
|
|||||||
) {
|
) {
|
||||||
match self.in_flight.remove(&(req_id, who.clone())) {
|
match self.in_flight.remove(&(req_id, who.clone())) {
|
||||||
Some(mut req) => {
|
Some(mut req) => {
|
||||||
if let Some(pov_block) = pov_block {
|
match pov_block {
|
||||||
match req.process_response(pov_block) {
|
Some(pov_block) => {
|
||||||
Ok(()) => return,
|
match req.process_response(pov_block) {
|
||||||
Err(r) => { req = r; }
|
Ok(()) => {
|
||||||
|
ctx.report_peer(who, benefit::GOOD_POV_BLOCK);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Err(r) => {
|
||||||
|
ctx.report_peer(who, cost::BAD_POV_BLOCK);
|
||||||
|
req = r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
ctx.report_peer(who, benefit::EXPECTED_MESSAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -493,6 +516,7 @@ impl Specialization<Block> for PolkadotProtocol {
|
|||||||
ctx.report_peer(who, cost::COLLATOR_ALREADY_KNOWN);
|
ctx.report_peer(who, cost::COLLATOR_ALREADY_KNOWN);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
ctx.report_peer(who.clone(), benefit::NEW_COLLATOR);
|
||||||
|
|
||||||
let collator_role = self.collators.on_new_collator(acc_id.clone(), para_id.clone());
|
let collator_role = self.collators.on_new_collator(acc_id.clone(), para_id.clone());
|
||||||
|
|
||||||
@@ -566,7 +590,10 @@ impl Specialization<Block> for PolkadotProtocol {
|
|||||||
match message.take() {
|
match message.take() {
|
||||||
Some(generic_message::Message::ChainSpecific(raw)) => {
|
Some(generic_message::Message::ChainSpecific(raw)) => {
|
||||||
match Message::decode(&mut raw.as_slice()) {
|
match Message::decode(&mut raw.as_slice()) {
|
||||||
Some(msg) => self.on_polkadot_message(ctx, who, msg),
|
Some(msg) => {
|
||||||
|
ctx.report_peer(who.clone(), benefit::VALID_FORMAT);
|
||||||
|
self.on_polkadot_message(ctx, who, msg)
|
||||||
|
},
|
||||||
None => {
|
None => {
|
||||||
trace!(target: "p_net", "Bad message from {}", who);
|
trace!(target: "p_net", "Bad message from {}", who);
|
||||||
ctx.report_peer(who, cost::INVALID_FORMAT);
|
ctx.report_peer(who, cost::INVALID_FORMAT);
|
||||||
@@ -614,16 +641,21 @@ impl PolkadotProtocol {
|
|||||||
|
|
||||||
match self.peers.get(&from) {
|
match self.peers.get(&from) {
|
||||||
None => ctx.report_peer(from, cost::UNKNOWN_PEER),
|
None => ctx.report_peer(from, cost::UNKNOWN_PEER),
|
||||||
Some(peer_info) => match peer_info.collating_for {
|
Some(peer_info) => {
|
||||||
None => ctx.report_peer(from, cost::UNEXPECTED_MESSAGE),
|
ctx.report_peer(from.clone(), benefit::KNOWN_PEER);
|
||||||
Some((ref acc_id, ref para_id)) => {
|
match peer_info.collating_for {
|
||||||
let structurally_valid = para_id == &collation_para && acc_id == &collated_acc;
|
None => ctx.report_peer(from, cost::UNEXPECTED_MESSAGE),
|
||||||
if structurally_valid && collation.receipt.check_signature().is_ok() {
|
Some((ref acc_id, ref para_id)) => {
|
||||||
debug!(target: "p_net", "Received collation for parachain {:?} from peer {}", para_id, from);
|
ctx.report_peer(from.clone(), benefit::EXPECTED_MESSAGE);
|
||||||
self.collators.on_collation(acc_id.clone(), relay_parent, collation)
|
let structurally_valid = para_id == &collation_para && acc_id == &collated_acc;
|
||||||
} else {
|
if structurally_valid && collation.receipt.check_signature().is_ok() {
|
||||||
ctx.report_peer(from, cost::INVALID_FORMAT)
|
debug!(target: "p_net", "Received collation for parachain {:?} from peer {}", para_id, from);
|
||||||
};
|
ctx.report_peer(from, benefit::GOOD_COLLATION);
|
||||||
|
self.collators.on_collation(acc_id.clone(), relay_parent, collation)
|
||||||
|
} else {
|
||||||
|
ctx.report_peer(from, cost::INVALID_FORMAT)
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
//! Tests for polkadot and validation network.
|
//! Tests for polkadot and validation network.
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use super::{PolkadotProtocol, Status, Message, FullStatus};
|
use super::{PolkadotProtocol, Status, Message, FullStatus};
|
||||||
use validation::SessionParams;
|
use validation::SessionParams;
|
||||||
|
|
||||||
@@ -41,6 +42,7 @@ mod validation;
|
|||||||
struct TestContext {
|
struct TestContext {
|
||||||
disabled: Vec<PeerId>,
|
disabled: Vec<PeerId>,
|
||||||
disconnected: Vec<PeerId>,
|
disconnected: Vec<PeerId>,
|
||||||
|
reputations: HashMap<PeerId, i32>,
|
||||||
messages: Vec<(PeerId, Vec<u8>)>,
|
messages: Vec<(PeerId, Vec<u8>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +52,9 @@ impl Context<Block> for TestContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn report_peer(&mut self, peer: PeerId, reputation: i32) {
|
fn report_peer(&mut self, peer: PeerId, reputation: i32) {
|
||||||
|
let reputation = self.reputations.get(&peer).map_or(reputation, |v| v + reputation);
|
||||||
|
self.reputations.insert(peer.clone(), reputation);
|
||||||
|
|
||||||
match reputation {
|
match reputation {
|
||||||
i if i < -100 => self.disabled.push(peer),
|
i if i < -100 => self.disabled.push(peer),
|
||||||
i if i < 0 => self.disconnected.push(peer),
|
i if i < 0 => self.disconnected.push(peer),
|
||||||
@@ -306,6 +311,22 @@ fn remove_bad_collator() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn kick_collator() {
|
||||||
|
let mut protocol = PolkadotProtocol::new(None);
|
||||||
|
|
||||||
|
let who = PeerId::random();
|
||||||
|
let collator_id: CollatorId = [2; 32].unchecked_into();
|
||||||
|
|
||||||
|
let mut ctx = TestContext::default();
|
||||||
|
let status = Status { collating_for: Some((collator_id.clone(), 5.into())) };
|
||||||
|
protocol.on_connect(&mut ctx, who.clone(), make_status(&status, Roles::NONE));
|
||||||
|
assert!(!ctx.disconnected.contains(&who));
|
||||||
|
|
||||||
|
protocol.on_connect(&mut ctx, who.clone(), make_status(&status, Roles::NONE));
|
||||||
|
assert!(ctx.disconnected.contains(&who));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn many_session_keys() {
|
fn many_session_keys() {
|
||||||
let mut protocol = PolkadotProtocol::new(None);
|
let mut protocol = PolkadotProtocol::new(None);
|
||||||
|
|||||||
Reference in New Issue
Block a user