Skip to content

Commit c2887fb

Browse files
committed
Merge branch 'unstable' of github.com:jxs/lighthouse into move-to-workspace
2 parents 771e547 + f06305a commit c2887fb

File tree

6 files changed

+74
-92
lines changed

6 files changed

+74
-92
lines changed

beacon_node/lighthouse_network/src/discovery/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,6 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
550550
if let Ok(node_id) = peer_id_to_node_id(peer_id) {
551551
// If we could convert this peer id, remove it from the DHT and ban it from discovery.
552552
self.discv5.ban_node(&node_id, None);
553-
// Remove the node from the routing table.
554-
self.discv5.remove_node(&node_id);
555553
}
556554

557555
for ip_address in ip_addresses {

beacon_node/lighthouse_network/src/peer_manager/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
414414
/// Reports if a peer is banned or not.
415415
///
416416
/// This is used to determine if we should accept incoming connections.
417-
pub fn ban_status(&self, peer_id: &PeerId) -> BanResult {
417+
pub fn ban_status(&self, peer_id: &PeerId) -> Option<BanResult> {
418418
self.network_globals.peers.read().ban_status(peer_id)
419419
}
420420

@@ -802,7 +802,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
802802
) -> bool {
803803
{
804804
let mut peerdb = self.network_globals.peers.write();
805-
if !matches!(peerdb.ban_status(peer_id), BanResult::NotBanned) {
805+
if peerdb.ban_status(peer_id).is_some() {
806806
// don't connect if the peer is banned
807807
error!(self.log, "Connection has been allowed to a banned peer"; "peer_id" => %peer_id);
808808
}

beacon_node/lighthouse_network/src/peer_manager/network_behaviour.rs

+23-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use libp2p::identity::PeerId;
88
use libp2p::swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
99
use libp2p::swarm::dial_opts::{DialOpts, PeerCondition};
1010
use libp2p::swarm::dummy::ConnectionHandler;
11-
use libp2p::swarm::{ConnectionId, NetworkBehaviour, PollParameters, ToSwarm};
11+
use libp2p::swarm::{ConnectionDenied, ConnectionId, NetworkBehaviour, PollParameters, ToSwarm};
1212
use slog::{debug, error};
1313
use types::EthSpec;
1414

@@ -157,12 +157,30 @@ impl<TSpec: EthSpec> NetworkBehaviour for PeerManager<TSpec> {
157157
fn handle_established_inbound_connection(
158158
&mut self,
159159
_connection_id: ConnectionId,
160-
_peer: PeerId,
160+
peer_id: PeerId,
161161
_local_addr: &libp2p::Multiaddr,
162162
_remote_addr: &libp2p::Multiaddr,
163-
) -> Result<libp2p::swarm::THandler<Self>, libp2p::swarm::ConnectionDenied> {
164-
// TODO: we might want to check if we accept this peer or not in the future.
165-
Ok(ConnectionHandler)
163+
) -> Result<libp2p::swarm::THandler<Self>, ConnectionDenied> {
164+
// Check to make sure the peer is not supposed to be banned
165+
match self.ban_status(&peer_id) {
166+
Some(cause @ BanResult::BadScore) => {
167+
// This is a faulty state
168+
error!(self.log, "Connected to a banned peer. Re-banning"; "peer_id" => %peer_id);
169+
// Disconnect the peer.
170+
self.goodbye_peer(&peer_id, GoodbyeReason::Banned, ReportSource::PeerManager);
171+
// Re-ban the peer to prevent repeated errors.
172+
self.events.push(PeerManagerEvent::Banned(peer_id, vec![]));
173+
Err(ConnectionDenied::new(cause))
174+
}
175+
Some(cause @ BanResult::BannedIp(ip_addr)) => {
176+
// A good peer has connected to us via a banned IP address. We ban the peer and
177+
// prevent future connections.
178+
debug!(self.log, "Peer connected via banned IP. Banning"; "peer_id" => %peer_id, "banned_ip" => %ip_addr);
179+
self.goodbye_peer(&peer_id, GoodbyeReason::BannedIP, ReportSource::PeerManager);
180+
Err(ConnectionDenied::new(cause))
181+
}
182+
None => Ok(ConnectionHandler),
183+
}
166184
}
167185

168186
fn handle_established_outbound_connection(
@@ -194,28 +212,6 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
194212
metrics::check_nat();
195213
}
196214

197-
// Check to make sure the peer is not supposed to be banned
198-
match self.ban_status(&peer_id) {
199-
// TODO: directly emit the ban event?
200-
BanResult::BadScore => {
201-
// This is a faulty state
202-
error!(self.log, "Connected to a banned peer. Re-banning"; "peer_id" => %peer_id);
203-
// Disconnect the peer.
204-
self.goodbye_peer(&peer_id, GoodbyeReason::Banned, ReportSource::PeerManager);
205-
// Re-ban the peer to prevent repeated errors.
206-
self.events.push(PeerManagerEvent::Banned(peer_id, vec![]));
207-
return;
208-
}
209-
BanResult::BannedIp(ip_addr) => {
210-
// A good peer has connected to us via a banned IP address. We ban the peer and
211-
// prevent future connections.
212-
debug!(self.log, "Peer connected via banned IP. Banning"; "peer_id" => %peer_id, "banned_ip" => %ip_addr);
213-
self.goodbye_peer(&peer_id, GoodbyeReason::BannedIP, ReportSource::PeerManager);
214-
return;
215-
}
216-
BanResult::NotBanned => {}
217-
}
218-
219215
// Count dialing peers in the limit if the peer dialed us.
220216
let count_dialing = endpoint.is_listener();
221217
// Check the connection limits

beacon_node/lighthouse_network/src/peer_manager/peerdb.rs

+49-50
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ use peer_info::{ConnectionDirection, PeerConnectionStatus, PeerInfo};
88
use rand::seq::SliceRandom;
99
use score::{PeerAction, ReportSource, Score, ScoreState};
1010
use slog::{crit, debug, error, trace, warn};
11-
use std::cmp::Ordering;
12-
use std::collections::{HashMap, HashSet};
1311
use std::net::{IpAddr, SocketAddr};
1412
use std::time::Instant;
13+
use std::{cmp::Ordering, fmt::Display};
14+
use std::{
15+
collections::{HashMap, HashSet},
16+
fmt::Formatter,
17+
};
1518
use sync_status::SyncStatus;
1619
use types::EthSpec;
1720

@@ -141,25 +144,19 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
141144
}
142145
}
143146

144-
/// Returns the current [`BanResult`] of the peer. This doesn't check the connection state, rather the
147+
/// Returns the current [`BanResult`] of the peer if banned. This doesn't check the connection state, rather the
145148
/// underlying score of the peer. A peer may be banned but still in the connected state
146149
/// temporarily.
147150
///
148151
/// This is used to determine if we should accept incoming connections or not.
149-
pub fn ban_status(&self, peer_id: &PeerId) -> BanResult {
152+
pub fn ban_status(&self, peer_id: &PeerId) -> Option<BanResult> {
150153
if let Some(peer) = self.peers.get(peer_id) {
151154
match peer.score_state() {
152-
ScoreState::Banned => BanResult::BadScore,
153-
_ => {
154-
if let Some(ip) = self.ip_is_banned(peer) {
155-
BanResult::BannedIp(ip)
156-
} else {
157-
BanResult::NotBanned
158-
}
159-
}
155+
ScoreState::Banned => Some(BanResult::BadScore),
156+
_ => self.ip_is_banned(peer).map(BanResult::BannedIp),
160157
}
161158
} else {
162-
BanResult::NotBanned
159+
None
163160
}
164161
}
165162

@@ -1206,23 +1203,25 @@ pub enum BanOperation {
12061203
}
12071204

12081205
/// When checking if a peer is banned, it can be banned for multiple reasons.
1206+
#[derive(Copy, Clone, Debug)]
12091207
pub enum BanResult {
12101208
/// The peer's score is too low causing it to be banned.
12111209
BadScore,
12121210
/// The peer should be banned because it is connecting from a banned IP address.
12131211
BannedIp(IpAddr),
1214-
/// The peer is not banned.
1215-
NotBanned,
12161212
}
12171213

1218-
// Helper function for unit tests
1219-
#[cfg(test)]
1220-
impl BanResult {
1221-
pub fn is_banned(&self) -> bool {
1222-
!matches!(self, BanResult::NotBanned)
1214+
impl Display for BanResult {
1215+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1216+
match self {
1217+
BanResult::BadScore => write!(f, "Peer has a bad score"),
1218+
BanResult::BannedIp(addr) => write!(f, "Peer address: {} is banned", addr),
1219+
}
12231220
}
12241221
}
12251222

1223+
impl std::error::Error for BanResult {}
1224+
12261225
#[derive(Default)]
12271226
pub struct BannedPeersCount {
12281227
/// The number of banned peers in the database.
@@ -1874,11 +1873,11 @@ mod tests {
18741873
}
18751874

18761875
//check that ip1 and ip2 are banned but ip3-5 not
1877-
assert!(pdb.ban_status(&p1).is_banned());
1878-
assert!(pdb.ban_status(&p2).is_banned());
1879-
assert!(!pdb.ban_status(&p3).is_banned());
1880-
assert!(!pdb.ban_status(&p4).is_banned());
1881-
assert!(!pdb.ban_status(&p5).is_banned());
1876+
assert!(pdb.ban_status(&p1).is_some());
1877+
assert!(pdb.ban_status(&p2).is_some());
1878+
assert!(pdb.ban_status(&p3).is_none());
1879+
assert!(pdb.ban_status(&p4).is_none());
1880+
assert!(pdb.ban_status(&p5).is_none());
18821881

18831882
//ban also the last peer in peers
18841883
let _ = pdb.report_peer(
@@ -1890,35 +1889,35 @@ mod tests {
18901889
pdb.inject_disconnect(&peers[BANNED_PEERS_PER_IP_THRESHOLD + 1]);
18911890

18921891
//check that ip1-ip4 are banned but ip5 not
1893-
assert!(pdb.ban_status(&p1).is_banned());
1894-
assert!(pdb.ban_status(&p2).is_banned());
1895-
assert!(pdb.ban_status(&p3).is_banned());
1896-
assert!(pdb.ban_status(&p4).is_banned());
1897-
assert!(!pdb.ban_status(&p5).is_banned());
1892+
assert!(pdb.ban_status(&p1).is_some());
1893+
assert!(pdb.ban_status(&p2).is_some());
1894+
assert!(pdb.ban_status(&p3).is_some());
1895+
assert!(pdb.ban_status(&p4).is_some());
1896+
assert!(pdb.ban_status(&p5).is_none());
18981897

18991898
//peers[0] gets unbanned
19001899
reset_score(&mut pdb, &peers[0]);
19011900
pdb.update_connection_state(&peers[0], NewConnectionState::Unbanned);
19021901
let _ = pdb.shrink_to_fit();
19031902

19041903
//nothing changed
1905-
assert!(pdb.ban_status(&p1).is_banned());
1906-
assert!(pdb.ban_status(&p2).is_banned());
1907-
assert!(pdb.ban_status(&p3).is_banned());
1908-
assert!(pdb.ban_status(&p4).is_banned());
1909-
assert!(!pdb.ban_status(&p5).is_banned());
1904+
assert!(pdb.ban_status(&p1).is_some());
1905+
assert!(pdb.ban_status(&p2).is_some());
1906+
assert!(pdb.ban_status(&p3).is_some());
1907+
assert!(pdb.ban_status(&p4).is_some());
1908+
assert!(pdb.ban_status(&p5).is_none());
19101909

19111910
//peers[1] gets unbanned
19121911
reset_score(&mut pdb, &peers[1]);
19131912
pdb.update_connection_state(&peers[1], NewConnectionState::Unbanned);
19141913
let _ = pdb.shrink_to_fit();
19151914

19161915
//all ips are unbanned
1917-
assert!(!pdb.ban_status(&p1).is_banned());
1918-
assert!(!pdb.ban_status(&p2).is_banned());
1919-
assert!(!pdb.ban_status(&p3).is_banned());
1920-
assert!(!pdb.ban_status(&p4).is_banned());
1921-
assert!(!pdb.ban_status(&p5).is_banned());
1916+
assert!(pdb.ban_status(&p1).is_none());
1917+
assert!(pdb.ban_status(&p2).is_none());
1918+
assert!(pdb.ban_status(&p3).is_none());
1919+
assert!(pdb.ban_status(&p4).is_none());
1920+
assert!(pdb.ban_status(&p5).is_none());
19221921
}
19231922

19241923
#[test]
@@ -1943,17 +1942,17 @@ mod tests {
19431942
}
19441943

19451944
// check ip is banned
1946-
assert!(pdb.ban_status(&p1).is_banned());
1947-
assert!(!pdb.ban_status(&p2).is_banned());
1945+
assert!(pdb.ban_status(&p1).is_some());
1946+
assert!(pdb.ban_status(&p2).is_none());
19481947

19491948
// unban a peer
19501949
reset_score(&mut pdb, &peers[0]);
19511950
pdb.update_connection_state(&peers[0], NewConnectionState::Unbanned);
19521951
let _ = pdb.shrink_to_fit();
19531952

19541953
// check not banned anymore
1955-
assert!(!pdb.ban_status(&p1).is_banned());
1956-
assert!(!pdb.ban_status(&p2).is_banned());
1954+
assert!(pdb.ban_status(&p1).is_none());
1955+
assert!(pdb.ban_status(&p2).is_none());
19571956

19581957
// unban all peers
19591958
for p in &peers {
@@ -1972,8 +1971,8 @@ mod tests {
19721971
}
19731972

19741973
// both IP's are now banned
1975-
assert!(pdb.ban_status(&p1).is_banned());
1976-
assert!(pdb.ban_status(&p2).is_banned());
1974+
assert!(pdb.ban_status(&p1).is_some());
1975+
assert!(pdb.ban_status(&p2).is_some());
19771976

19781977
// unban all peers
19791978
for p in &peers {
@@ -1989,16 +1988,16 @@ mod tests {
19891988
}
19901989

19911990
// nothing is banned
1992-
assert!(!pdb.ban_status(&p1).is_banned());
1993-
assert!(!pdb.ban_status(&p2).is_banned());
1991+
assert!(pdb.ban_status(&p1).is_none());
1992+
assert!(pdb.ban_status(&p2).is_none());
19941993

19951994
// reban last peer
19961995
let _ = pdb.report_peer(&peers[0], PeerAction::Fatal, ReportSource::PeerManager, "");
19971996
pdb.inject_disconnect(&peers[0]);
19981997

19991998
//Ip's are banned again
2000-
assert!(pdb.ban_status(&p1).is_banned());
2001-
assert!(pdb.ban_status(&p2).is_banned());
1999+
assert!(pdb.ban_status(&p1).is_some());
2000+
assert!(pdb.ban_status(&p2).is_some());
20022001
}
20032002

20042003
#[test]

beacon_node/lighthouse_network/src/service/behaviour.rs

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ where
2020
AppReqId: ReqId,
2121
TSpec: EthSpec,
2222
{
23-
/// Peers banned.
24-
pub banned_peers: libp2p::allow_block_list::Behaviour<libp2p::allow_block_list::BlockedPeers>,
2523
/// Keep track of active and pending connections to enforce hard limits.
2624
pub connection_limits: libp2p::connection_limits::Behaviour,
2725
/// The routing pub-sub mechanism for eth2.

beacon_node/lighthouse_network/src/service/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,8 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
339339
libp2p::connection_limits::Behaviour::new(limits)
340340
};
341341

342-
let banned_peers = libp2p::allow_block_list::Behaviour::default();
343-
344342
let behaviour = {
345343
Behaviour {
346-
banned_peers,
347344
gossipsub,
348345
eth2_rpc,
349346
discovery,
@@ -1401,15 +1398,10 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
14011398
Some(NetworkEvent::PeerDisconnected(peer_id))
14021399
}
14031400
PeerManagerEvent::Banned(peer_id, associated_ips) => {
1404-
self.swarm.behaviour_mut().banned_peers.block_peer(peer_id);
14051401
self.discovery_mut().ban_peer(&peer_id, associated_ips);
14061402
None
14071403
}
14081404
PeerManagerEvent::UnBanned(peer_id, associated_ips) => {
1409-
self.swarm
1410-
.behaviour_mut()
1411-
.banned_peers
1412-
.unblock_peer(peer_id);
14131405
self.discovery_mut().unban_peer(&peer_id, associated_ips);
14141406
None
14151407
}
@@ -1458,7 +1450,6 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
14581450
let maybe_event = match swarm_event {
14591451
SwarmEvent::Behaviour(behaviour_event) => match behaviour_event {
14601452
// Handle sub-behaviour events.
1461-
BehaviourEvent::BannedPeers(void) => void::unreachable(void),
14621453
BehaviourEvent::Gossipsub(ge) => self.inject_gs_event(ge),
14631454
BehaviourEvent::Eth2Rpc(re) => self.inject_rpc_event(re),
14641455
BehaviourEvent::Discovery(de) => self.inject_discovery_event(de),

0 commit comments

Comments
 (0)