From d8214899c57a7ab3126c1ba8fa26d2ae75dc8bd6 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 6 Nov 2019 22:00:03 +0100 Subject: [PATCH 1/2] Decrease reputation on bad transactions --- core/network/src/protocol.rs | 7 +++++-- core/network/src/service.rs | 3 ++- core/network/src/test/mod.rs | 9 ++++++++- core/service/src/lib.rs | 22 ++++++++++++++++------ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/core/network/src/protocol.rs b/core/network/src/protocol.rs index 8b3b5a49e40ec..35de679489e25 100644 --- a/core/network/src/protocol.rs +++ b/core/network/src/protocol.rs @@ -90,7 +90,9 @@ const UNEXPECTED_STATUS_REPUTATION_CHANGE: i32 = -(1 << 20); /// Reputation change when we are a light client and a peer is behind us. const PEER_BEHIND_US_LIGHT_REPUTATION_CHANGE: i32 = -(1 << 8); /// Reputation change when a peer sends us an extrinsic that we didn't know about. -const NEW_EXTRINSIC_REPUTATION_CHANGE: i32 = 1 << 7; +const GOOD_EXTRINSIC_REPUTATION_CHANGE: i32 = 1 << 7; +/// Reputation change when a peer sends us a bad extrinsic. +const BAD_EXTRINSIC_REPUTATION_CHANGE: i32 = -(1 << 12); /// We sent an RPC query to the given node, but it failed. const RPC_FAILED_REPUTATION_CHANGE: i32 = -(1 << 12); /// We received a message that failed to decode. @@ -1019,7 +1021,8 @@ impl, H: ExHashT> Protocol { self.transaction_pool.import( self.peerset_handle.clone().into(), who.clone(), - NEW_EXTRINSIC_REPUTATION_CHANGE, + GOOD_EXTRINSIC_REPUTATION_CHANGE, + BAD_EXTRINSIC_REPUTATION_CHANGE, t, ); } diff --git a/core/network/src/service.rs b/core/network/src/service.rs index a88c163a691e9..e73bff9b1cd2e 100644 --- a/core/network/src/service.rs +++ b/core/network/src/service.rs @@ -71,7 +71,8 @@ pub trait TransactionPool: Send + Sync { &self, report_handle: ReportHandle, who: PeerId, - reputation_change: i32, + reputation_change_good: i32, + reputation_change_bad: i32, transaction: B::Extrinsic, ); /// Notify the pool about transactions broadcast. diff --git a/core/network/src/test/mod.rs b/core/network/src/test/mod.rs index 0c50179f10ace..330988169db9b 100644 --- a/core/network/src/test/mod.rs +++ b/core/network/src/test/mod.rs @@ -399,7 +399,14 @@ impl TransactionPool for EmptyTransactionPool { Hash::default() } - fn import(&self, _report_handle: ReportHandle, _who: PeerId, _rep_change: i32, _transaction: Extrinsic) {} + fn import( + &self, + _report_handle: ReportHandle, + _who: PeerId, + _rep_change_good: i32, + _rep_change_bad: i32, + _transaction: Extrinsic + ) {} fn on_broadcasted(&self, _: HashMap>) {} } diff --git a/core/service/src/lib.rs b/core/service/src/lib.rs index d09fcb093b202..c3e1f39a4dfa3 100644 --- a/core/service/src/lib.rs +++ b/core/service/src/lib.rs @@ -615,7 +615,14 @@ where self.pool.hash_of(transaction) } - fn import(&self, report_handle: ReportHandle, who: PeerId, reputation_change: i32, transaction: B::Extrinsic) { + fn import( + &self, + report_handle: ReportHandle, + who: PeerId, + reputation_change_good: i32, + reputation_change_bad: i32, + transaction: B::Extrinsic + ) { if !self.imports_external_transactions { debug!("Transaction rejected"); return; @@ -629,11 +636,14 @@ where let import_future = import_future .then(move |import_result| { match import_result { - Ok(_) => report_handle.report_peer(who, reputation_change), - Err(e) => match e.into_pool_error() { - Ok(txpool::error::Error::AlreadyImported(_)) => (), - Ok(e) => debug!("Error adding transaction to the pool: {:?}", e), - Err(e) => debug!("Error converting pool error: {:?}", e), + Ok(_) => report_handle.report_peer(who, reputation_change_good), + Err(e) => { + report_handle.report_peer(who, reputation_change_bad); + match e.into_pool_error() { + Ok(txpool::error::Error::AlreadyImported(_)) => (), + Ok(e) => debug!("Error adding transaction to the pool: {:?}", e), + Err(e) => debug!("Error converting pool error: {:?}", e), + } } } ready(Ok(())) From 109029318c78cc29f64fed0ec9a3e8283d4b4452 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 6 Nov 2019 22:31:05 +0100 Subject: [PATCH 2/2] Don't punish on duplicate transactions --- core/service/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/service/src/lib.rs b/core/service/src/lib.rs index c3e1f39a4dfa3..b267e0a635b23 100644 --- a/core/service/src/lib.rs +++ b/core/service/src/lib.rs @@ -637,13 +637,13 @@ where .then(move |import_result| { match import_result { Ok(_) => report_handle.report_peer(who, reputation_change_good), - Err(e) => { - report_handle.report_peer(who, reputation_change_bad); - match e.into_pool_error() { - Ok(txpool::error::Error::AlreadyImported(_)) => (), - Ok(e) => debug!("Error adding transaction to the pool: {:?}", e), - Err(e) => debug!("Error converting pool error: {:?}", e), + Err(e) => match e.into_pool_error() { + Ok(txpool::error::Error::AlreadyImported(_)) => (), + Ok(e) => { + report_handle.report_peer(who, reputation_change_bad); + debug!("Error adding transaction to the pool: {:?}", e) } + Err(e) => debug!("Error converting pool error: {:?}", e), } } ready(Ok(()))