Skip to content

Commit f96167b

Browse files
committed
Added Tests
1 parent a562e1d commit f96167b

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

beacon_node/lighthouse_network/src/peer_manager/peerdb.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,66 @@ mod tests {
19171917
p
19181918
}
19191919

1920+
#[test]
1921+
fn test_add_penalty_record() {
1922+
let mut pdb = get_db();
1923+
let random_peer = PeerId::random();
1924+
1925+
pdb.connect_ingoing(&random_peer, "/ip4/0.0.0.0".parse().unwrap(), None);
1926+
1927+
// Check to see if get_penalty_records is able to get the associated peer info
1928+
assert!(pdb.get_penalty_records(&random_peer).is_some());
1929+
1930+
// Check to see if there are no initial penalty records
1931+
assert_eq!(pdb.get_penalty_records(&random_peer).unwrap().into_iter().count(),0);
1932+
1933+
let _ = pdb.report_peer(
1934+
&random_peer,
1935+
PeerAction::HighToleranceError,
1936+
ReportSource::PeerManager,
1937+
"Minor report action",
1938+
);
1939+
1940+
// Check to see if there was a penalty record added
1941+
assert_eq!(pdb.get_penalty_records(&random_peer).unwrap().into_iter().count(),1);
1942+
1943+
let first_record = pdb.get_penalty_records(&random_peer).unwrap().into_iter().next().unwrap();
1944+
1945+
// Check to see the correct report action for the penalty record
1946+
assert!(matches!(first_record.action(),PeerAction::HighToleranceError));
1947+
1948+
// Check to see the correct report source for the penalty record
1949+
assert!(matches!(first_record.source(),ReportSource::PeerManager));
1950+
1951+
// Check to see the correct message for the penalty record
1952+
assert_eq!(*first_record.msg(),"Minor report action".to_string());
1953+
1954+
// Check to see the correct result for the penalty record
1955+
assert!(matches!(first_record.result(),ScoreUpdateResult::NoAction));
1956+
1957+
let _ = pdb.report_peer(
1958+
&random_peer,
1959+
PeerAction::HighToleranceError,
1960+
ReportSource::PeerManager,
1961+
"Minor report action",
1962+
);
1963+
1964+
// Check to see if there was a penalty record added
1965+
assert_eq!(pdb.get_penalty_records(&random_peer).unwrap().into_iter().count(),2);
1966+
1967+
for _ in 1..(MAX_STORED_PENALTY_RECORDS+1) {
1968+
let _ = pdb.report_peer(
1969+
&random_peer,
1970+
PeerAction::HighToleranceError,
1971+
ReportSource::PeerManager,
1972+
"Minor report action",
1973+
);
1974+
}
1975+
1976+
// Check to make sure that the number of penalty records don't exceed the maximum
1977+
assert_eq!(pdb.get_penalty_records(&random_peer).unwrap().into_iter().count(),MAX_STORED_PENALTY_RECORDS);
1978+
}
1979+
19201980
#[test]
19211981
fn test_ban_address() {
19221982
let mut pdb = get_db();

beacon_node/lighthouse_network/src/peer_manager/peerdb/peer_info.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ impl<E: EthSpec> PeerInfo<E> {
399399
}
400400
}
401401

402+
/// Gets an iterator with the penalty records
403+
// VISIBILITY: The peer manager is able to add a penalty record
404+
pub(in crate::peer_manager) fn get_penalty_records(&self) -> impl Iterator<Item = &PenaltyRecord>{
405+
self.penalty_records.iter()
406+
}
407+
402408
/// Sets the connection status of the peer.
403409
pub(super) fn set_connection_status(&mut self, connection_status: PeerConnectionStatus) {
404410
self.connection_status = connection_status

beacon_node/lighthouse_network/src/peer_manager/peerdb/score.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ impl PenaltyRecord {
7070
time_stamp: time_stamp
7171
}
7272
}
73+
74+
/// Obtains the action from the penalty record
75+
pub fn action(&self) -> &PeerAction {
76+
&self.action
77+
}
78+
79+
/// Obtains the report source from the penalty record
80+
pub fn source(&self) -> &ReportSource {
81+
&self.source
82+
}
83+
84+
/// Obtains the message associated with the penalty record
85+
pub fn msg(&self) -> &String {
86+
&self.msg
87+
}
88+
89+
/// Obtains the result of the penalty record
90+
pub fn result(&self) -> &ScoreUpdateResult {
91+
&self.result
92+
}
93+
94+
/// Obtains the time stamp of when the penalty record was made
95+
pub fn time_stamp(&self) -> &Option<u128> {
96+
&self.time_stamp
97+
}
7398
}
7499

75100
/// A collection of actions a peer can perform which will adjust its score.

0 commit comments

Comments
 (0)