From 64765bf817d220c13c7d9939c645779c1d8e2ba2 Mon Sep 17 00:00:00 2001 From: steviez Date: Tue, 2 Apr 2024 11:59:03 -0500 Subject: [PATCH] Introduce NodeConfig for parameters to Node type (#533) The parameter list is already kind of long, so squash the parameters into a config struct --- gossip/src/cluster_info.rs | 60 ++++++++++++++++++++++---------------- validator/src/main.rs | 22 ++++++++------ 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/gossip/src/cluster_info.rs b/gossip/src/cluster_info.rs index 783f8a067..7d737d313 100644 --- a/gossip/src/cluster_info.rs +++ b/gossip/src/cluster_info.rs @@ -2786,6 +2786,14 @@ pub struct Sockets { pub tpu_forwards_quic: UdpSocket, } +pub struct NodeConfig { + pub gossip_addr: SocketAddr, + pub port_range: PortRange, + pub bind_ip_addr: IpAddr, + pub public_tpu_addr: Option, + pub public_tpu_forwards_addr: Option, +} + #[derive(Debug)] pub struct Node { pub info: ContactInfo, @@ -2978,16 +2986,17 @@ impl Node { } } - pub fn new_with_external_ip( - pubkey: &Pubkey, - gossip_addr: &SocketAddr, - port_range: PortRange, - bind_ip_addr: IpAddr, - public_tpu_addr: Option, - public_tpu_forwards_addr: Option, - ) -> Node { + pub fn new_with_external_ip(pubkey: &Pubkey, config: NodeConfig) -> Node { + let NodeConfig { + gossip_addr, + port_range, + bind_ip_addr, + public_tpu_addr, + public_tpu_forwards_addr, + } = config; + let (gossip_port, (gossip, ip_echo)) = - Self::get_gossip_port(gossip_addr, port_range, bind_ip_addr); + Self::get_gossip_port(&gossip_addr, port_range, bind_ip_addr); let (tvu_port, tvu_sockets) = multi_bind_in_range(bind_ip_addr, port_range, 8).expect("tvu multi_bind"); @@ -3593,14 +3602,15 @@ mod tests { #[test] fn new_with_external_ip_test_random() { let ip = Ipv4Addr::LOCALHOST; - let node = Node::new_with_external_ip( - &solana_sdk::pubkey::new_rand(), - &socketaddr!(ip, 0), - VALIDATOR_PORT_RANGE, - IpAddr::V4(ip), - None, - None, - ); + let config = NodeConfig { + gossip_addr: socketaddr!(ip, 0), + port_range: VALIDATOR_PORT_RANGE, + bind_ip_addr: IpAddr::V4(ip), + public_tpu_addr: None, + public_tpu_forwards_addr: None, + }; + + let node = Node::new_with_external_ip(&solana_sdk::pubkey::new_rand(), config); check_node_sockets(&node, IpAddr::V4(ip), VALIDATOR_PORT_RANGE); } @@ -3613,17 +3623,17 @@ mod tests { VALIDATOR_PORT_RANGE.1 + MINIMUM_VALIDATOR_PORT_RANGE_WIDTH, VALIDATOR_PORT_RANGE.1 + (2 * MINIMUM_VALIDATOR_PORT_RANGE_WIDTH), ); - let ip = IpAddr::V4(Ipv4Addr::LOCALHOST); let port = bind_in_range(ip, port_range).expect("Failed to bind").0; - let node = Node::new_with_external_ip( - &solana_sdk::pubkey::new_rand(), - &socketaddr!(Ipv4Addr::LOCALHOST, port), + let config = NodeConfig { + gossip_addr: socketaddr!(Ipv4Addr::LOCALHOST, port), port_range, - ip, - None, - None, - ); + bind_ip_addr: ip, + public_tpu_addr: None, + public_tpu_forwards_addr: None, + }; + + let node = Node::new_with_external_ip(&solana_sdk::pubkey::new_rand(), config); check_node_sockets(&node, ip, port_range); diff --git a/validator/src/main.rs b/validator/src/main.rs index 151281bc8..c8494221d 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -36,7 +36,10 @@ use { ValidatorConfig, ValidatorStartProgress, }, }, - solana_gossip::{cluster_info::Node, legacy_contact_info::LegacyContactInfo as ContactInfo}, + solana_gossip::{ + cluster_info::{Node, NodeConfig}, + legacy_contact_info::LegacyContactInfo as ContactInfo, + }, solana_ledger::{ blockstore_cleanup_service::{DEFAULT_MAX_LEDGER_SHREDS, DEFAULT_MIN_MAX_LEDGER_SHREDS}, blockstore_options::{ @@ -1844,19 +1847,20 @@ pub fn main() { }) }); + let node_config = NodeConfig { + gossip_addr, + port_range: dynamic_port_range, + bind_ip_addr: bind_address, + public_tpu_addr, + public_tpu_forwards_addr, + }; + let cluster_entrypoints = entrypoint_addrs .iter() .map(ContactInfo::new_gossip_entry_point) .collect::>(); - let mut node = Node::new_with_external_ip( - &identity_keypair.pubkey(), - &gossip_addr, - dynamic_port_range, - bind_address, - public_tpu_addr, - public_tpu_forwards_addr, - ); + let mut node = Node::new_with_external_ip(&identity_keypair.pubkey(), node_config); if restricted_repair_only_mode { // When in --restricted_repair_only_mode is enabled only the gossip and repair ports