Skip to content

Commit

Permalink
fix: blacklist peers who are send bad blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Dec 30, 2024
1 parent c0979fb commit 3d0e3e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,24 +743,26 @@ where S: ShareChain
return false;
}

for addr in &payload.public_addresses() {
self.swarm.add_peer_address(peer, addr.clone());
}
let public_addresses = payload.public_addresses();
let add_status = self.network_peer_store.write().await.add(peer, payload).await;

match add_status {
AddPeerStatus::NewPeer => {
// self.initiate_direct_peer_exchange(&peer).await;
// self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer);
// let _unused = self.swarm.dial(peer);
for addr in &public_addresses {
self.swarm.add_peer_address(peer, addr.clone());
}

return true;
},
AddPeerStatus::Existing => {},
AddPeerStatus::Greylisted => {
debug!(target: LOG_TARGET, "Added peer but it was grey listed");
},
AddPeerStatus::Blacklisted => {
info!(target: LOG_TARGET, "Added peer but it was black listed");
debug!(target: LOG_TARGET, "Added peer {} but it was black listed", peer);
},
}

Expand Down Expand Up @@ -2134,12 +2136,12 @@ where S: ShareChain
}
},
Err(error) => {
error!(target: SYNC_REQUEST_LOG_TARGET, "Failed to add Catchup synced blocks to share
chain: {error:?}");
// network_peer_store
// .write()
// .await
// .move_to_grey_list(peer, format!("Block failed validation: {error}"));
error!(target: SYNC_REQUEST_LOG_TARGET, "Failed to add Catchup synced blocks from {source_peer:?} to share chain: {error:?}");
let _unused = self.swarm.disconnect_peer_id(source_peer);
self.network_peer_store
.write()
.await
.move_to_blacklist(&source_peer, format!("Block failed validation: {error}"));
},
}
// info!(target: LOG_TARGET, "[{:?}] Blocks via catchup sync added {:?}", algo, blocks_added);
Expand Down
20 changes: 20 additions & 0 deletions src/server/p2p/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,26 @@ impl PeerStore {
}
}

pub fn move_to_blacklist(&mut self, peer: &PeerId, reason: String) {
let mut record = None;
if self.whitelist_peers.contains_key(&peer.to_base58()) {
record = self.whitelist_peers.remove(&peer.to_base58());
}
if self.greylist_peers.contains_key(&peer.to_base58()) {
record = self.greylist_peers.remove(&peer.to_base58());
}

if let Some(record) = record {
warn!(target: LOG_TARGET, "Blacklisting peer {} because of: {}", peer, reason);
self.blacklist_peers.insert(peer.to_base58(), record);
let _unused = self.stats_broadcast_client.send_new_peer(
self.whitelist_peers.len() as u64,
self.greylist_peers.len() as u64,
self.blacklist_peers.len() as u64,
);
}
}

// pub fn is_blacklisted(&self, peer_id: &PeerId) -> bool {
// self.blacklist_peers.contains_key(&peer_id.to_base58())
// }
Expand Down

0 comments on commit 3d0e3e6

Please sign in to comment.