@@ -8,10 +8,13 @@ use peer_info::{ConnectionDirection, PeerConnectionStatus, PeerInfo};
8
8
use rand:: seq:: SliceRandom ;
9
9
use score:: { PeerAction , ReportSource , Score , ScoreState } ;
10
10
use slog:: { crit, debug, error, trace, warn} ;
11
- use std:: cmp:: Ordering ;
12
- use std:: collections:: { HashMap , HashSet } ;
13
11
use std:: net:: { IpAddr , SocketAddr } ;
14
12
use std:: time:: Instant ;
13
+ use std:: { cmp:: Ordering , fmt:: Display } ;
14
+ use std:: {
15
+ collections:: { HashMap , HashSet } ,
16
+ fmt:: Formatter ,
17
+ } ;
15
18
use sync_status:: SyncStatus ;
16
19
use types:: EthSpec ;
17
20
@@ -141,25 +144,19 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
141
144
}
142
145
}
143
146
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
145
148
/// underlying score of the peer. A peer may be banned but still in the connected state
146
149
/// temporarily.
147
150
///
148
151
/// 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 > {
150
153
if let Some ( peer) = self . peers . get ( peer_id) {
151
154
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 ) ,
160
157
}
161
158
} else {
162
- BanResult :: NotBanned
159
+ None
163
160
}
164
161
}
165
162
@@ -1206,23 +1203,25 @@ pub enum BanOperation {
1206
1203
}
1207
1204
1208
1205
/// When checking if a peer is banned, it can be banned for multiple reasons.
1206
+ #[ derive( Copy , Clone , Debug ) ]
1209
1207
pub enum BanResult {
1210
1208
/// The peer's score is too low causing it to be banned.
1211
1209
BadScore ,
1212
1210
/// The peer should be banned because it is connecting from a banned IP address.
1213
1211
BannedIp ( IpAddr ) ,
1214
- /// The peer is not banned.
1215
- NotBanned ,
1216
1212
}
1217
1213
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
+ }
1223
1220
}
1224
1221
}
1225
1222
1223
+ impl std:: error:: Error for BanResult { }
1224
+
1226
1225
#[ derive( Default ) ]
1227
1226
pub struct BannedPeersCount {
1228
1227
/// The number of banned peers in the database.
@@ -1874,11 +1873,11 @@ mod tests {
1874
1873
}
1875
1874
1876
1875
//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 ( ) ) ;
1882
1881
1883
1882
//ban also the last peer in peers
1884
1883
let _ = pdb. report_peer (
@@ -1890,35 +1889,35 @@ mod tests {
1890
1889
pdb. inject_disconnect ( & peers[ BANNED_PEERS_PER_IP_THRESHOLD + 1 ] ) ;
1891
1890
1892
1891
//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 ( ) ) ;
1898
1897
1899
1898
//peers[0] gets unbanned
1900
1899
reset_score ( & mut pdb, & peers[ 0 ] ) ;
1901
1900
pdb. update_connection_state ( & peers[ 0 ] , NewConnectionState :: Unbanned ) ;
1902
1901
let _ = pdb. shrink_to_fit ( ) ;
1903
1902
1904
1903
//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 ( ) ) ;
1910
1909
1911
1910
//peers[1] gets unbanned
1912
1911
reset_score ( & mut pdb, & peers[ 1 ] ) ;
1913
1912
pdb. update_connection_state ( & peers[ 1 ] , NewConnectionState :: Unbanned ) ;
1914
1913
let _ = pdb. shrink_to_fit ( ) ;
1915
1914
1916
1915
//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 ( ) ) ;
1922
1921
}
1923
1922
1924
1923
#[ test]
@@ -1943,17 +1942,17 @@ mod tests {
1943
1942
}
1944
1943
1945
1944
// 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 ( ) ) ;
1948
1947
1949
1948
// unban a peer
1950
1949
reset_score ( & mut pdb, & peers[ 0 ] ) ;
1951
1950
pdb. update_connection_state ( & peers[ 0 ] , NewConnectionState :: Unbanned ) ;
1952
1951
let _ = pdb. shrink_to_fit ( ) ;
1953
1952
1954
1953
// 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 ( ) ) ;
1957
1956
1958
1957
// unban all peers
1959
1958
for p in & peers {
@@ -1972,8 +1971,8 @@ mod tests {
1972
1971
}
1973
1972
1974
1973
// 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 ( ) ) ;
1977
1976
1978
1977
// unban all peers
1979
1978
for p in & peers {
@@ -1989,16 +1988,16 @@ mod tests {
1989
1988
}
1990
1989
1991
1990
// 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 ( ) ) ;
1994
1993
1995
1994
// reban last peer
1996
1995
let _ = pdb. report_peer ( & peers[ 0 ] , PeerAction :: Fatal , ReportSource :: PeerManager , "" ) ;
1997
1996
pdb. inject_disconnect ( & peers[ 0 ] ) ;
1998
1997
1999
1998
//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 ( ) ) ;
2002
2001
}
2003
2002
2004
2003
#[ test]
0 commit comments