Introduce NodeConfig for parameters to Node type (#533)

The parameter list is already kind of long, so squash the parameters
into a config struct
This commit is contained in:
steviez 2024-04-02 11:59:03 -05:00 committed by GitHub
parent c59143b980
commit 64765bf817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 34 deletions

View File

@ -2786,6 +2786,14 @@ pub struct Sockets {
pub tpu_forwards_quic: UdpSocket, 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<SocketAddr>,
pub public_tpu_forwards_addr: Option<SocketAddr>,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Node { pub struct Node {
pub info: ContactInfo, pub info: ContactInfo,
@ -2978,16 +2986,17 @@ impl Node {
} }
} }
pub fn new_with_external_ip( pub fn new_with_external_ip(pubkey: &Pubkey, config: NodeConfig) -> Node {
pubkey: &Pubkey, let NodeConfig {
gossip_addr: &SocketAddr, gossip_addr,
port_range: PortRange, port_range,
bind_ip_addr: IpAddr, bind_ip_addr,
public_tpu_addr: Option<SocketAddr>, public_tpu_addr,
public_tpu_forwards_addr: Option<SocketAddr>, public_tpu_forwards_addr,
) -> Node { } = config;
let (gossip_port, (gossip, ip_echo)) = 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) = let (tvu_port, tvu_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, 8).expect("tvu multi_bind"); multi_bind_in_range(bind_ip_addr, port_range, 8).expect("tvu multi_bind");
@ -3593,14 +3602,15 @@ mod tests {
#[test] #[test]
fn new_with_external_ip_test_random() { fn new_with_external_ip_test_random() {
let ip = Ipv4Addr::LOCALHOST; let ip = Ipv4Addr::LOCALHOST;
let node = Node::new_with_external_ip( let config = NodeConfig {
&solana_sdk::pubkey::new_rand(), gossip_addr: socketaddr!(ip, 0),
&socketaddr!(ip, 0), port_range: VALIDATOR_PORT_RANGE,
VALIDATOR_PORT_RANGE, bind_ip_addr: IpAddr::V4(ip),
IpAddr::V4(ip), public_tpu_addr: None,
None, public_tpu_forwards_addr: None,
None, };
);
let node = Node::new_with_external_ip(&solana_sdk::pubkey::new_rand(), config);
check_node_sockets(&node, IpAddr::V4(ip), VALIDATOR_PORT_RANGE); 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 + MINIMUM_VALIDATOR_PORT_RANGE_WIDTH,
VALIDATOR_PORT_RANGE.1 + (2 * MINIMUM_VALIDATOR_PORT_RANGE_WIDTH), VALIDATOR_PORT_RANGE.1 + (2 * MINIMUM_VALIDATOR_PORT_RANGE_WIDTH),
); );
let ip = IpAddr::V4(Ipv4Addr::LOCALHOST); let ip = IpAddr::V4(Ipv4Addr::LOCALHOST);
let port = bind_in_range(ip, port_range).expect("Failed to bind").0; let port = bind_in_range(ip, port_range).expect("Failed to bind").0;
let node = Node::new_with_external_ip( let config = NodeConfig {
&solana_sdk::pubkey::new_rand(), gossip_addr: socketaddr!(Ipv4Addr::LOCALHOST, port),
&socketaddr!(Ipv4Addr::LOCALHOST, port),
port_range, port_range,
ip, bind_ip_addr: ip,
None, public_tpu_addr: None,
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); check_node_sockets(&node, ip, port_range);

View File

@ -36,7 +36,10 @@ use {
ValidatorConfig, ValidatorStartProgress, 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::{ solana_ledger::{
blockstore_cleanup_service::{DEFAULT_MAX_LEDGER_SHREDS, DEFAULT_MIN_MAX_LEDGER_SHREDS}, blockstore_cleanup_service::{DEFAULT_MAX_LEDGER_SHREDS, DEFAULT_MIN_MAX_LEDGER_SHREDS},
blockstore_options::{ 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 let cluster_entrypoints = entrypoint_addrs
.iter() .iter()
.map(ContactInfo::new_gossip_entry_point) .map(ContactInfo::new_gossip_entry_point)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut node = Node::new_with_external_ip( let mut node = Node::new_with_external_ip(&identity_keypair.pubkey(), node_config);
&identity_keypair.pubkey(),
&gossip_addr,
dynamic_port_range,
bind_address,
public_tpu_addr,
public_tpu_forwards_addr,
);
if restricted_repair_only_mode { if restricted_repair_only_mode {
// When in --restricted_repair_only_mode is enabled only the gossip and repair ports // When in --restricted_repair_only_mode is enabled only the gossip and repair ports