Add a flag to disable peer scoring (#4135)

## 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.
This commit is contained in:
Pawan Dhananjay 2023-04-12 01:48:19 +00:00
parent 4d17fb3af6
commit 3b117f4bf6
10 changed files with 65 additions and 5 deletions

View File

@ -155,6 +155,7 @@ pub async fn create_api_server_on_port<T: BeaconChainTypes>(
None,
meta_data,
vec![],
false,
&log,
));

View File

@ -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,
@ -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,

View File

@ -1162,6 +1162,7 @@ mod tests {
syncnets: Default::default(),
}),
vec![],
false,
&log,
);
Discovery::new(&keypair, &config, Arc::new(globals), &log)

View File

@ -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()
@ -56,6 +58,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
log: log.clone(),
disconnected_peers: 0,
banned_peers_count: BannedPeersCount::default(),
disable_peer_scoring,
peers,
}
}
@ -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.
@ -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]
@ -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);
@ -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()
);
}
}

View File

@ -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)

View File

@ -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 {
@ -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),
@ -144,6 +145,7 @@ impl<TSpec: EthSpec> NetworkGlobals<TSpec> {
syncnets: Default::default(),
}),
vec![],
false,
log,
)
}

View File

@ -185,6 +185,7 @@ impl TestRig {
None,
meta_data,
vec![],
false,
&log,
));

View File

@ -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")

View File

@ -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(',')

View File

@ -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)