Skip to content

Commit d635035

Browse files
varun-doshimattsse
andauthored
feat: punish malicious peers (#16818)
Co-authored-by: Matthias Seitz <[email protected]>
1 parent 3c2ef0e commit d635035

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

crates/net/network/src/transactions/fetcher.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,15 +882,19 @@ impl<N: NetworkPrimitives> TransactionFetcher<N> {
882882
if unsolicited > 0 {
883883
self.metrics.unsolicited_transactions.increment(unsolicited as u64);
884884
}
885-
if verification_outcome == VerificationOutcome::ReportPeer {
886-
// todo: report peer for sending hashes that weren't requested
885+
886+
let report_peer = if verification_outcome == VerificationOutcome::ReportPeer {
887887
trace!(target: "net::tx",
888888
peer_id=format!("{peer_id:#}"),
889889
unverified_len,
890890
verified_payload_len=verified_payload.len(),
891891
"received `PooledTransactions` response from peer with entries that didn't verify against request, filtered out transactions"
892892
);
893-
}
893+
true
894+
} else {
895+
false
896+
};
897+
894898
// peer has only sent hashes that we didn't request
895899
if verified_payload.is_empty() {
896900
return FetchEvent::FetchError { peer_id, error: RequestError::BadResponse }
@@ -952,7 +956,7 @@ impl<N: NetworkPrimitives> TransactionFetcher<N> {
952956

953957
let transactions = valid_payload.into_data().into_values().collect();
954958

955-
FetchEvent::TransactionsFetched { peer_id, transactions }
959+
FetchEvent::TransactionsFetched { peer_id, transactions, report_peer }
956960
}
957961
Ok(Err(req_err)) => {
958962
self.try_buffer_hashes_for_retry(requested_hashes, &peer_id);
@@ -1039,6 +1043,9 @@ pub enum FetchEvent<T = PooledTransaction> {
10391043
peer_id: PeerId,
10401044
/// The transactions that were fetched, if available.
10411045
transactions: PooledTransactions<T>,
1046+
/// Whether the peer should be penalized for sending unsolicited transactions or for
1047+
/// misbehavior.
1048+
report_peer: bool,
10421049
},
10431050
/// Triggered when there is an error in fetching transactions.
10441051
FetchError {

crates/net/network/src/transactions/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,8 +1456,11 @@ where
14561456
/// Processes a [`FetchEvent`].
14571457
fn on_fetch_event(&mut self, fetch_event: FetchEvent<N::PooledTransaction>) {
14581458
match fetch_event {
1459-
FetchEvent::TransactionsFetched { peer_id, transactions } => {
1459+
FetchEvent::TransactionsFetched { peer_id, transactions, report_peer } => {
14601460
self.import_transactions(peer_id, transactions, TransactionSource::Response);
1461+
if report_peer {
1462+
self.report_peer(peer_id, ReputationChangeKind::BadTransactions);
1463+
}
14611464
}
14621465
FetchEvent::FetchError { peer_id, error } => {
14631466
trace!(target: "net::tx", ?peer_id, %error, "requesting transactions from peer failed");

0 commit comments

Comments
 (0)