Skip to content

Commit

Permalink
Add a flag to disable peer scoring (sigp#4135)
Browse files Browse the repository at this point in the history
## Issue Addressed

N/A

## Proposed Changes

Adds a flag for disabling peer scoring. This is useful for local testing and testing small networks for new features.
  • Loading branch information
pawanjay176 authored and isaac.asimov committed Jul 13, 2023
1 parent ba82ddd commit cda5f06
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions beacon_node/http_api/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub async fn create_api_server_on_port<T: BeaconChainTypes>(
None,
meta_data,
vec![],
false,
&log,
));

Expand Down
4 changes: 4 additions & 0 deletions beacon_node/lighthouse_network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ pub struct Config {
/// List of trusted libp2p nodes which are not scored.
pub trusted_peers: Vec<PeerIdSerialized>,

/// Disables peer scoring altogether.
pub disable_peer_scoring: bool,

/// Client version
pub client_version: String,

Expand Down Expand Up @@ -309,6 +312,7 @@ impl Default for Config {
boot_nodes_multiaddr: vec![],
libp2p_nodes: vec![],
trusted_peers: vec![],
disable_peer_scoring: false,
client_version: lighthouse_version::version_with_platform(),
disable_discovery: false,
upnp_enabled: true,
Expand Down
1 change: 1 addition & 0 deletions beacon_node/lighthouse_network/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@ mod tests {
syncnets: Default::default(),
}),
vec![],
false,
&log,
);
Discovery::new(&keypair, &config, Arc::new(globals), &log)
Expand Down
39 changes: 35 additions & 4 deletions beacon_node/lighthouse_network/src/peer_manager/peerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ pub struct PeerDB<TSpec: EthSpec> {
disconnected_peers: usize,
/// Counts banned peers in total and per ip
banned_peers_count: BannedPeersCount,
/// Specifies if peer scoring is disabled.
disable_peer_scoring: bool,
/// PeerDB's logger
log: slog::Logger,
}

impl<TSpec: EthSpec> PeerDB<TSpec> {
pub fn new(trusted_peers: Vec<PeerId>, log: &slog::Logger) -> Self {
pub fn new(trusted_peers: Vec<PeerId>, disable_peer_scoring: bool, log: &slog::Logger) -> Self {
// Initialize the peers hashmap with trusted peers
let peers = trusted_peers
.into_iter()
Expand All @@ -56,6 +58,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
log: log.clone(),
disconnected_peers: 0,
banned_peers_count: BannedPeersCount::default(),
disable_peer_scoring,
peers,
}
}
Expand Down Expand Up @@ -704,7 +707,11 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
warn!(log_ref, "Updating state of unknown peer";
"peer_id" => %peer_id, "new_state" => ?new_state);
}
PeerInfo::default()
if self.disable_peer_scoring {
PeerInfo::trusted_peer_info()
} else {
PeerInfo::default()
}
});

// Ban the peer if the score is not already low enough.
Expand Down Expand Up @@ -1300,7 +1307,7 @@ mod tests {

fn get_db() -> PeerDB<M> {
let log = build_log(slog::Level::Debug, false);
PeerDB::new(vec![], &log)
PeerDB::new(vec![], false, &log)
}

#[test]
Expand Down Expand Up @@ -1999,7 +2006,7 @@ mod tests {
fn test_trusted_peers_score() {
let trusted_peer = PeerId::random();
let log = build_log(slog::Level::Debug, false);
let mut pdb: PeerDB<M> = PeerDB::new(vec![trusted_peer], &log);
let mut pdb: PeerDB<M> = PeerDB::new(vec![trusted_peer], false, &log);

pdb.connect_ingoing(&trusted_peer, "/ip4/0.0.0.0".parse().unwrap(), None);

Expand All @@ -2018,4 +2025,28 @@ mod tests {
Score::max_score().score()
);
}

#[test]
fn test_disable_peer_scoring() {
let peer = PeerId::random();
let log = build_log(slog::Level::Debug, false);
let mut pdb: PeerDB<M> = PeerDB::new(vec![], true, &log);

pdb.connect_ingoing(&peer, "/ip4/0.0.0.0".parse().unwrap(), None);

// Check trusted status and score
assert!(pdb.peer_info(&peer).unwrap().is_trusted());
assert_eq!(
pdb.peer_info(&peer).unwrap().score().score(),
Score::max_score().score()
);

// Adding/Subtracting score should have no effect on a trusted peer
add_score(&mut pdb, &peer, -50.0);

assert_eq!(
pdb.peer_info(&peer).unwrap().score().score(),
Score::max_score().score()
);
}
}
1 change: 1 addition & 0 deletions beacon_node/lighthouse_network/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
.iter()
.map(|x| PeerId::from(x.clone()))
.collect(),
config.disable_peer_scoring,
&log,
);
Arc::new(globals)
Expand Down
4 changes: 3 additions & 1 deletion beacon_node/lighthouse_network/src/types/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<TSpec: EthSpec> NetworkGlobals<TSpec> {
listen_port_tcp6: Option<u16>,
local_metadata: MetaData<TSpec>,
trusted_peers: Vec<PeerId>,
disable_peer_scoring: bool,
log: &slog::Logger,
) -> Self {
NetworkGlobals {
Expand All @@ -48,7 +49,7 @@ impl<TSpec: EthSpec> NetworkGlobals<TSpec> {
listen_port_tcp4,
listen_port_tcp6,
local_metadata: RwLock::new(local_metadata),
peers: RwLock::new(PeerDB::new(trusted_peers, log)),
peers: RwLock::new(PeerDB::new(trusted_peers, disable_peer_scoring, log)),
gossipsub_subscriptions: RwLock::new(HashSet::new()),
sync_state: RwLock::new(SyncState::Stalled),
backfill_state: RwLock::new(BackFillState::NotRequired),
Expand Down Expand Up @@ -144,6 +145,7 @@ impl<TSpec: EthSpec> NetworkGlobals<TSpec> {
syncnets: Default::default(),
}),
vec![],
false,
log,
)
}
Expand Down
1 change: 1 addition & 0 deletions beacon_node/network/src/beacon_processor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl TestRig {
None,
meta_data,
vec![],
false,
&log,
));

Expand Down
8 changes: 8 additions & 0 deletions beacon_node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.help("Disables the discv5 discovery protocol. The node will not search for new peers or participate in the discovery protocol.")
.takes_value(false),
)
.arg(
Arg::with_name("disable-peer-scoring")
.long("disable-peer-scoring")
.help("Disables peer scoring in lighthouse. WARNING: This is a dev only flag is only meant to be used in local testing scenarios \
Using this flag on a real network may cause your node to become eclipsed and see a different view of the network")
.takes_value(false)
.hidden(true),
)
.arg(
Arg::with_name("trusted-peers")
.long("trusted-peers")
Expand Down
4 changes: 4 additions & 0 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,10 @@ pub fn set_network_config(
.collect::<Result<Vec<Multiaddr>, _>>()?;
}

if cli_args.is_present("disable-peer-scoring") {
config.disable_peer_scoring = true;
}

if let Some(trusted_peers_str) = cli_args.value_of("trusted-peers") {
config.trusted_peers = trusted_peers_str
.split(',')
Expand Down
7 changes: 7 additions & 0 deletions lighthouse/tests/beacon_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,13 @@ fn disable_discovery_flag() {
.with_config(|config| assert!(config.network.disable_discovery));
}
#[test]
fn disable_peer_scoring_flag() {
CommandLineTest::new()
.flag("disable-peer-scoring", None)
.run_with_zero_port()
.with_config(|config| assert!(config.network.disable_peer_scoring));
}
#[test]
fn disable_upnp_flag() {
CommandLineTest::new()
.flag("disable-upnp", None)
Expand Down

0 comments on commit cda5f06

Please sign in to comment.