Skip to content

Commit

Permalink
fix: use default angelfood bootnodes if network is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Sep 18, 2024
1 parent 0ff1b83 commit 7825729
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
48 changes: 47 additions & 1 deletion ethportal-api/src/types/bootnodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ lazy_static! {
Enr::from_str("enr:-IS4QA5hpJikeDFf1DD1_Le6_ylgrLGpdwn3SRaneGu9hY2HUI7peHep0f28UUMzbC0PvlWjN8zSfnqMG07WVcCyBhADgmlkgnY0gmlwhKRc9-KJc2VjcDI1NmsxoQJMpHmGj1xSP1O-Mffk_jYIHVcg6tY5_CjmWVg1gJEsPIN1ZHCCE4o").expect("Parsing static bootnode enr to work"),
alias: "ultralight-4".to_string()
}];

// AngelFood bootstrap nodes
pub static ref ANGELFOOD_BOOTNODES: Vec<Bootnode> = vec![
Bootnode{
enr: Enr::from_str("enr:-LC4QMnoW2m4YYQRPjZhJ5hEpcA6a3V7iQs3slQ1TepzKBIVWQtjpcHsPINc0TcheMCbx6I2n5aax8M3AtUObt74ySUCY6p0IDVhYzI2NzViNGRmMjNhNmEwOWVjNDFkZTRlYTQ2ODQxNjk2ZTQ1YzSCaWSCdjSCaXCEQONKaYlzZWNwMjU2azGhAvZgYbpA9G8NQ6X4agu-R7Ymtu0hcX6xBQ--UEel_b6Pg3VkcIIjKA").expect("Parsing static bootnode enr to work"),
alias: "angelfood-trin-1".to_string()
}];
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum Bootnodes {
#[default]
Default,
Angelfood,
// use explicit None here instead of Option<Bootnodes>, since default value is
// DEFAULT_BOOTNODES
None,
Expand All @@ -100,6 +108,10 @@ impl From<Bootnodes> for Vec<Enr> {
fn from(bootnodes: Bootnodes) -> Self {
match bootnodes {
Bootnodes::Default => DEFAULT_BOOTNODES.iter().map(|bn| bn.enr.clone()).collect(),
Bootnodes::Angelfood => ANGELFOOD_BOOTNODES
.iter()
.map(|bn| bn.enr.clone())
.collect(),
Bootnodes::None => vec![],
Bootnodes::Custom(bootnodes) => bootnodes.iter().map(|bn| bn.enr.clone()).collect(),
}
Expand All @@ -112,6 +124,7 @@ impl FromStr for Bootnodes {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"default" => Ok(Bootnodes::Default),
"angelfood" => Ok(Bootnodes::Angelfood),
"none" => Ok(Bootnodes::None),
_ => {
let bootnodes: Result<Vec<Enr>, _> = s.split(',').map(Enr::from_str).collect();
Expand All @@ -135,13 +148,21 @@ mod test {
use rstest::rstest;

#[test_log::test]
fn test_bootnodes_default_with_angelfood_bootnodes() {
fn test_bootnodes_default_with_default_bootnodes() {
let config = TrinConfig::new_from(["trin"].iter()).unwrap();
assert_eq!(config.bootnodes, Bootnodes::Default);
let bootnodes: Vec<Enr> = config.bootnodes.into();
assert_eq!(bootnodes.len(), 11);
}

#[test_log::test]
fn test_bootnodes_default_with_angelfood_bootnodes() {
let config = TrinConfig::new_from(["trin", "--bootnodes", "angelfood"].iter()).unwrap();
assert_eq!(config.bootnodes, Bootnodes::Angelfood);
let bootnodes: Vec<Enr> = config.bootnodes.into();
assert_eq!(bootnodes.len(), 1);
}

#[test_log::test]
fn test_bootnodes_default_with_no_bootnodes() {
let config = TrinConfig::new_from(["trin", "--bootnodes", "none"].iter()).unwrap();
Expand Down Expand Up @@ -173,4 +194,29 @@ mod test {
let bootnodes: Vec<Enr> = config.bootnodes.into();
assert_eq!(bootnodes.len(), expected_length);
}

#[rstest]
fn test_angelfood_network_defaults_to_correct_bootnodes() {
let config = TrinConfig::new_from(["trin", "--network", "angelfood"].iter()).unwrap();
assert_eq!(config.bootnodes, Bootnodes::Angelfood);
let bootnodes: Vec<Enr> = config.bootnodes.into();
assert_eq!(bootnodes.len(), 1);
}

#[rstest]
fn test_custom_bootnodes_override_angelfood_default() {
let enr = "enr:-IS4QBISSFfBzsBrjq61iSIxPMfp5ShBTW6KQUglzH_tj8_SJaehXdlnZI-NAkTGeoclwnTB-pU544BQA44BiDZ2rkMBgmlkgnY0gmlwhKEjVaWJc2VjcDI1NmsxoQOSGugH1jSdiE_fRK1FIBe9oLxaWH8D_7xXSnaOVBe-SYN1ZHCCIyg";
let config =
TrinConfig::new_from(["trin", "--network", "angelfood", "--bootnodes", enr].iter())
.unwrap();
assert_eq!(
config.bootnodes,
Bootnodes::Custom(vec![Bootnode {
enr: Enr::from_str(enr).unwrap(),
alias: "custom".to_string(),
}])
);
let bootnodes: Vec<Enr> = config.bootnodes.into();
assert_eq!(bootnodes.len(), 1);
}
}
10 changes: 9 additions & 1 deletion ethportal-api/src/types/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl TrinConfig {
I: Iterator<Item = T>,
T: Into<OsString> + Clone,
{
let config = Self::try_parse_from(args)?;
let mut config = Self::try_parse_from(args)?;

if let Some(TrinConfigCommands::CreateDashboard(dashboard_config)) = config.command {
if let Err(err) = create_dashboard(dashboard_config) {
Expand Down Expand Up @@ -312,6 +312,14 @@ impl TrinConfig {
));
}

// change to default angelfood bootnodes if angelfood is selected,
// and custom bootnodes are not provided
if config.network.get_network_name() == "angelfood"
&& config.bootnodes == Bootnodes::Default
{
config.bootnodes = Bootnodes::Angelfood;
}

Ok(config)
}
}
Expand Down

0 comments on commit 7825729

Please sign in to comment.