diff --git a/src/cli.rs b/src/cli.rs index b99ba648e..399a868ea 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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, pub pool_size: u32, + pub discovery_port: u16, + pub bootnodes: Vec, } 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 { @@ -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[..] { @@ -86,11 +105,19 @@ impl TrinConfig { println!("Pool Size: {}", pool_size); + println!("Bootnodes: {}", bootnodes); + let bootnodes: Vec = bootnodes + .split(',') + .map(|bootnode| bootnode.to_string()) + .collect(); + Ok(TrinConfig { web3_transport, web3_ipc_path, web3_http_port, pool_size, + discovery_port, + bootnodes, }) } } @@ -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::().unwrap(), + web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::().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); @@ -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( [ @@ -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::().unwrap(), + web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::().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); @@ -180,10 +213,12 @@ mod test { ) .unwrap(); let expected_config = TrinConfig { - web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::().unwrap(), + web3_http_port: DEFAULT_WEB3_HTTP_PORT.parse::().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); @@ -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::().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::().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); + } } diff --git a/src/main.rs b/src/main.rs index 3a8509df7..76b87e1b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,19 +27,29 @@ async fn main() -> Result<(), Box> { ), }; + 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();