Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Configure p2p port and bootnode ENRs via command line #20

Merged
merged 2 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use std::ffi::OsString;
pub struct TrinConfig {
pub web3_transport: String,
pub web3_ipc_path: String,
pub web3_http_port: u32,
pub web3_http_port: u16,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is basically like a hangover coming from python land when I'm used to treating all integers as the same type (u32) - need to drink some coffee and sober up here in rust world.

pub pool_size: u32,
pub discovery_port: u16,
pub bootnodes: Vec<String>,
}

const DEFAULT_WEB3_IPC_PATH: &str = "/tmp/trin-jsonrpc.ipc";
const DEFAULT_WEB3_HTTP_PORT: &str = "8545";
const DEFAULT_DISCOVERY_PORT: &str = "9000";

impl TrinConfig {
pub fn new() -> Self {
Expand Down Expand Up @@ -57,13 +60,29 @@ impl TrinConfig {
.takes_value(true)
.default_value("2"),
)
.arg(
Arg::with_name("discovery_port")
.long("discovery-port")
.help("The UDP port to listen on.")
.default_value(&DEFAULT_DISCOVERY_PORT)
.takes_value(true),
)
.arg(
Arg::with_name("bootnodes")
.long("bootnodes")
.help("One or more comma-delimited base64-encoded ENR's or multiaddr strings of peers to initially add to the local routing table")
.default_value("")
.takes_value(true),
)
.get_matches_from(args);

println!("Launching trin...");
let pool_size = value_t!(matches.value_of("pool_size"), u32)?;
let web3_http_port = value_t!(matches.value_of("web3_http_port"), u32)?;
let web3_http_port = value_t!(matches.value_of("web3_http_port"), u16)?;
let web3_transport = value_t!(matches.value_of("web3_transport"), String)?;
let web3_ipc_path = value_t!(matches.value_of("web3_ipc_path"), String)?;
let discovery_port = value_t!(matches.value_of("discovery_port"), u16)?;
let bootnodes = value_t!(matches.value_of("bootnodes"), String)?;

match web3_transport.as_str() {
"http" => match &web3_ipc_path[..] {
Expand All @@ -86,11 +105,19 @@ impl TrinConfig {

println!("Pool Size: {}", pool_size);

println!("Bootnodes: {}", bootnodes);
let bootnodes: Vec<String> = bootnodes
.split(',')
.map(|bootnode| bootnode.to_string())
.collect();

Ok(TrinConfig {
web3_transport,
web3_ipc_path,
web3_http_port,
pool_size,
discovery_port,
bootnodes,
})
}
}
Expand All @@ -111,10 +138,12 @@ mod test {
fn test_default_args() {
assert!(env_is_set());
let expected_config = TrinConfig {
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u32>().unwrap(),
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 2,
web3_transport: "ipc".to_string(),
discovery_port: DEFAULT_DISCOVERY_PORT.parse().unwrap(),
bootnodes: vec![],
};
let actual_config = TrinConfig::new_from(["trin"].iter()).unwrap();
assert_eq!(actual_config.web3_transport, expected_config.web3_transport);
Expand All @@ -130,6 +159,8 @@ mod test {
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 3,
web3_transport: "http".to_string(),
discovery_port: DEFAULT_DISCOVERY_PORT.parse().unwrap(),
bootnodes: vec![],
};
let actual_config = TrinConfig::new_from(
[
Expand All @@ -155,10 +186,12 @@ mod test {
let actual_config =
TrinConfig::new_from(["trin", "--web3-transport", "ipc"].iter()).unwrap();
let expected_config = TrinConfig {
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u32>().unwrap(),
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 2,
web3_transport: "ipc".to_string(),
discovery_port: DEFAULT_DISCOVERY_PORT.parse().unwrap(),
bootnodes: vec![],
};
assert_eq!(actual_config.web3_transport, expected_config.web3_transport);
assert_eq!(actual_config.web3_http_port, expected_config.web3_http_port);
Expand All @@ -180,10 +213,12 @@ mod test {
)
.unwrap();
let expected_config = TrinConfig {
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u32>().unwrap(),
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: "/path/test.ipc".to_string(),
pool_size: 2,
web3_transport: "ipc".to_string(),
discovery_port: DEFAULT_DISCOVERY_PORT.parse().unwrap(),
bootnodes: vec![],
};
assert_eq!(actual_config.web3_transport, expected_config.web3_transport);
assert_eq!(actual_config.web3_http_port, expected_config.web3_http_port);
Expand Down Expand Up @@ -224,4 +259,36 @@ mod test {
)
.unwrap_err();
}

#[test]
fn test_custom_discovery_port() {
assert!(env_is_set());
let expected_config = TrinConfig {
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 2,
web3_transport: "ipc".to_string(),
discovery_port: 999,
bootnodes: vec![],
};
let actual_config =
TrinConfig::new_from(["trin", "--discovery-port", "999"].iter()).unwrap();
assert_eq!(actual_config.discovery_port, expected_config.discovery_port);
}

#[test]
fn test_custom_bootnodes() {
assert!(env_is_set());
let expected_config = TrinConfig {
web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::<u16>().unwrap(),
web3_ipc_path: DEFAULT_WEB3_IPC_PATH.to_string(),
pool_size: 2,
web3_transport: "ipc".to_string(),
discovery_port: DEFAULT_DISCOVERY_PORT.parse().unwrap(),
bootnodes: vec!["enr:-aoeu".to_string(), "enr:-htns".to_string()],
};
let actual_config =
TrinConfig::new_from(["trin", "--bootnodes", "enr:-aoeu,enr:-htns"].iter()).unwrap();
assert_eq!(actual_config.bootnodes, expected_config.bootnodes);
}
}
20 changes: 15 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
),
};

let listen_port = trin_config.discovery_port;
let bootnode_enrs = trin_config
.bootnodes
.iter()
.map(|nodestr| nodestr.parse().unwrap())
.collect();

let portalnet_config = PortalnetConfig {
listen_port,
bootnode_enrs,
..Default::default()
};

let web3_server_task = tokio::task::spawn_blocking(|| {
launch_trin(trin_config, infura_project_id);
});

// TODO populate portal config from cli args
let config: PortalnetConfig = Default::default();

info!(
"About to spawn portal p2p with boot nodes: {:?}",
config.bootnode_enrs
portalnet_config.bootnode_enrs
);
tokio::spawn(async move {
let mut p2p = PortalnetProtocol::new(config).await.unwrap();
let mut p2p = PortalnetProtocol::new(portalnet_config).await.unwrap();
// hacky test: make sure we establish a session with the boot node
p2p.ping_bootnodes().await.unwrap();

Expand Down