From cd51499ab912174304e6980ecc5f8eb38bc027ea Mon Sep 17 00:00:00 2001 From: Kevin Ji <1146876+kevinji@users.noreply.github.com> Date: Thu, 2 Feb 2023 15:48:21 -0800 Subject: [PATCH] Use Ipv4Addr constants in socketaddr! (#30095) --- faucet/src/bin/faucet.rs | 2 +- faucet/src/faucet.rs | 2 +- gossip/src/cluster_info.rs | 6 +-- gossip/src/crds.rs | 4 +- gossip/src/legacy_contact_info.rs | 61 ++++++++++++++++--------------- rpc/src/rpc.rs | 10 +++-- test-validator/src/lib.rs | 2 +- 7 files changed, 45 insertions(+), 42 deletions(-) diff --git a/faucet/src/bin/faucet.rs b/faucet/src/bin/faucet.rs index c8badee402..8e45ef9815 100644 --- a/faucet/src/bin/faucet.rs +++ b/faucet/src/bin/faucet.rs @@ -81,7 +81,7 @@ async fn main() { .into_iter() .collect(); - let faucet_addr = socketaddr!(0, FAUCET_PORT); + let faucet_addr = socketaddr!(Ipv4Addr::UNSPECIFIED, FAUCET_PORT); let faucet = Arc::new(Mutex::new(Faucet::new_with_allowed_ips( faucet_keypair, diff --git a/faucet/src/faucet.rs b/faucet/src/faucet.rs index 9f66c6988e..98d8e665ac 100644 --- a/faucet/src/faucet.rs +++ b/faucet/src/faucet.rs @@ -335,7 +335,7 @@ pub fn run_local_faucet_with_port( port: u16, // 0 => auto assign ) { thread::spawn(move || { - let faucet_addr = socketaddr!(0, port); + let faucet_addr = socketaddr!(Ipv4Addr::UNSPECIFIED, port); let faucet = Arc::new(Mutex::new(Faucet::new( faucet_keypair, time_input, diff --git a/gossip/src/cluster_info.rs b/gossip/src/cluster_info.rs index 845e321d61..8db9e59145 100644 --- a/gossip/src/cluster_info.rs +++ b/gossip/src/cluster_info.rs @@ -3623,7 +3623,7 @@ RPC Enabled Nodes: 1"#; #[test] fn new_with_external_ip_test_random() { - let ip = Ipv4Addr::from(0); + let ip = Ipv4Addr::UNSPECIFIED; let node = Node::new_with_external_ip( &solana_sdk::pubkey::new_rand(), &socketaddr!(ip, 0), @@ -3644,11 +3644,11 @@ RPC Enabled Nodes: 1"#; VALIDATOR_PORT_RANGE.1 + (2 * MINIMUM_VALIDATOR_PORT_RANGE_WIDTH), ); - let ip = IpAddr::V4(Ipv4Addr::from(0)); + let ip = IpAddr::V4(Ipv4Addr::UNSPECIFIED); 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!(0, port), + &socketaddr!(Ipv4Addr::UNSPECIFIED, port), port_range, ip, None, diff --git a/gossip/src/crds.rs b/gossip/src/crds.rs index 5ee14c2e19..778f938fcb 100644 --- a/gossip/src/crds.rs +++ b/gossip/src/crds.rs @@ -723,7 +723,7 @@ mod tests { signature::{Keypair, Signer}, timing::timestamp, }, - std::{collections::HashSet, iter::repeat_with}, + std::{collections::HashSet, iter::repeat_with, net::Ipv4Addr}, }; #[test] @@ -1376,7 +1376,7 @@ mod tests { let v2 = VersionedCrdsValue::new( { let mut contact_info = ContactInfo::new_localhost(&Pubkey::default(), 0); - contact_info.rpc = socketaddr!("0.0.0.0:0"); + contact_info.rpc = socketaddr!(Ipv4Addr::UNSPECIFIED, 0); CrdsValue::new_unsigned(CrdsData::LegacyContactInfo(contact_info)) }, Cursor::default(), diff --git a/gossip/src/legacy_contact_info.rs b/gossip/src/legacy_contact_info.rs index f478ce3b22..ba4755dcba 100644 --- a/gossip/src/legacy_contact_info.rs +++ b/gossip/src/legacy_contact_info.rs @@ -7,7 +7,7 @@ use { timing::timestamp, }, solana_streamer::socket::SocketAddrSpace, - std::net::{IpAddr, SocketAddr}, + std::net::{IpAddr, Ipv4Addr, SocketAddr}, }; /// Structure representing a node on the network @@ -60,10 +60,11 @@ macro_rules! socketaddr { $str.parse::().unwrap() }}; } + #[macro_export] macro_rules! socketaddr_any { () => { - socketaddr!(0, 0) + socketaddr!(std::net::Ipv4Addr::UNSPECIFIED, 0) }; } @@ -91,16 +92,16 @@ impl LegacyContactInfo { pub fn new_localhost(id: &Pubkey, now: u64) -> Self { Self { id: *id, - gossip: socketaddr!("127.0.0.1:1234"), - tvu: socketaddr!("127.0.0.1:1235"), - tvu_forwards: socketaddr!("127.0.0.1:1236"), - repair: socketaddr!("127.0.0.1:1237"), - tpu: socketaddr!("127.0.0.1:1238"), - tpu_forwards: socketaddr!("127.0.0.1:1239"), - tpu_vote: socketaddr!("127.0.0.1:1240"), - rpc: socketaddr!("127.0.0.1:1241"), - rpc_pubsub: socketaddr!("127.0.0.1:1242"), - serve_repair: socketaddr!("127.0.0.1:1243"), + gossip: socketaddr!(Ipv4Addr::LOCALHOST, 1234), + tvu: socketaddr!(Ipv4Addr::LOCALHOST, 1235), + tvu_forwards: socketaddr!(Ipv4Addr::LOCALHOST, 1236), + repair: socketaddr!(Ipv4Addr::LOCALHOST, 1237), + tpu: socketaddr!(Ipv4Addr::LOCALHOST, 1238), + tpu_forwards: socketaddr!(Ipv4Addr::LOCALHOST, 1239), + tpu_vote: socketaddr!(Ipv4Addr::LOCALHOST, 1240), + rpc: socketaddr!(Ipv4Addr::LOCALHOST, 1241), + rpc_pubsub: socketaddr!(Ipv4Addr::LOCALHOST, 1242), + serve_repair: socketaddr!(Ipv4Addr::LOCALHOST, 1243), wallclock: now, shred_version: 0, } @@ -202,12 +203,12 @@ mod tests { #[test] fn test_is_valid_address() { - let bad_address_port = socketaddr!("127.0.0.1:0"); + let bad_address_port = socketaddr!(Ipv4Addr::LOCALHOST, 0); assert!(!LegacyContactInfo::is_valid_address( &bad_address_port, &SocketAddrSpace::Unspecified )); - let bad_address_unspecified = socketaddr!(0, 1234); + let bad_address_unspecified = socketaddr!(Ipv4Addr::UNSPECIFIED, 1234); assert!(!LegacyContactInfo::is_valid_address( &bad_address_unspecified, &SocketAddrSpace::Unspecified @@ -217,7 +218,7 @@ mod tests { &bad_address_multicast, &SocketAddrSpace::Unspecified )); - let loopback = socketaddr!("127.0.0.1:1234"); + let loopback = socketaddr!(Ipv4Addr::LOCALHOST, 1234); assert!(LegacyContactInfo::is_valid_address( &loopback, &SocketAddrSpace::Unspecified @@ -240,7 +241,7 @@ mod tests { #[test] fn test_entry_point() { - let addr = socketaddr!("127.0.0.1:10"); + let addr = socketaddr!(Ipv4Addr::LOCALHOST, 10); let ci = LegacyContactInfo::new_gossip_entry_point(&addr); assert_eq!(ci.gossip, addr); assert!(ci.tvu.ip().is_unspecified()); @@ -253,7 +254,7 @@ mod tests { } #[test] fn test_socketaddr() { - let addr = socketaddr!("127.0.0.1:10"); + let addr = socketaddr!(Ipv4Addr::LOCALHOST, 10); let ci = LegacyContactInfo::new_with_socketaddr(&Keypair::new().pubkey(), &addr); assert_eq!(ci.tpu, addr); assert_eq!(ci.tpu_vote.port(), 17); @@ -270,25 +271,25 @@ mod tests { let keypair = Keypair::new(); let d1 = LegacyContactInfo::new_with_socketaddr( &keypair.pubkey(), - &socketaddr!("127.0.0.1:1234"), + &socketaddr!(Ipv4Addr::LOCALHOST, 1234), ); assert_eq!(d1.id, keypair.pubkey()); - assert_eq!(d1.gossip, socketaddr!("127.0.0.1:1235")); - assert_eq!(d1.tvu, socketaddr!("127.0.0.1:1236")); - assert_eq!(d1.tpu_forwards, socketaddr!("127.0.0.1:1237")); - assert_eq!(d1.tpu, socketaddr!("127.0.0.1:1234")); + assert_eq!(d1.gossip, socketaddr!(Ipv4Addr::LOCALHOST, 1235)); + assert_eq!(d1.tvu, socketaddr!(Ipv4Addr::LOCALHOST, 1236)); + assert_eq!(d1.tpu_forwards, socketaddr!(Ipv4Addr::LOCALHOST, 1237)); + assert_eq!(d1.tpu, socketaddr!(Ipv4Addr::LOCALHOST, 1234)); assert_eq!( d1.rpc, - socketaddr!(format!("127.0.0.1:{}", rpc_port::DEFAULT_RPC_PORT)) + socketaddr!(Ipv4Addr::LOCALHOST, rpc_port::DEFAULT_RPC_PORT) ); assert_eq!( d1.rpc_pubsub, - socketaddr!(format!("127.0.0.1:{}", rpc_port::DEFAULT_RPC_PUBSUB_PORT)) + socketaddr!(Ipv4Addr::LOCALHOST, rpc_port::DEFAULT_RPC_PUBSUB_PORT) ); - assert_eq!(d1.tvu_forwards, socketaddr!("127.0.0.1:1238")); - assert_eq!(d1.repair, socketaddr!("127.0.0.1:1239")); - assert_eq!(d1.serve_repair, socketaddr!("127.0.0.1:1240")); - assert_eq!(d1.tpu_vote, socketaddr!("127.0.0.1:1241")); + assert_eq!(d1.tvu_forwards, socketaddr!(Ipv4Addr::LOCALHOST, 1238)); + assert_eq!(d1.repair, socketaddr!(Ipv4Addr::LOCALHOST, 1239)); + assert_eq!(d1.serve_repair, socketaddr!(Ipv4Addr::LOCALHOST, 1240)); + assert_eq!(d1.tpu_vote, socketaddr!(Ipv4Addr::LOCALHOST, 1241)); } #[test] @@ -298,12 +299,12 @@ mod tests { ci.valid_client_facing_addr(&SocketAddrSpace::Unspecified), None ); - ci.tpu = socketaddr!("127.0.0.1:123"); + ci.tpu = socketaddr!(Ipv4Addr::LOCALHOST, 123); assert_eq!( ci.valid_client_facing_addr(&SocketAddrSpace::Unspecified), None ); - ci.rpc = socketaddr!("127.0.0.1:234"); + ci.rpc = socketaddr!(Ipv4Addr::LOCALHOST, 234); assert!(ci .valid_client_facing_addr(&SocketAddrSpace::Unspecified) .is_some()); diff --git a/rpc/src/rpc.rs b/rpc/src/rpc.rs index 415ebf91ca..d624621b6a 100644 --- a/rpc/src/rpc.rs +++ b/rpc/src/rpc.rs @@ -4677,7 +4677,7 @@ pub mod tests { solana_program::{program_option::COption, pubkey::Pubkey as SplTokenPubkey}, state::{AccountState as TokenAccountState, Mint}, }, - std::{borrow::Cow, collections::HashMap}, + std::{borrow::Cow, collections::HashMap, net::Ipv4Addr}, }; const TEST_MINT_LAMPORTS: u64 = 1_000_000_000; @@ -4767,7 +4767,7 @@ pub mod tests { let identity = cluster_info.id(); cluster_info.insert_info(ContactInfo::new_with_socketaddr( &leader_pubkey, - &socketaddr!("127.0.0.1:1234"), + &socketaddr!(Ipv4Addr::LOCALHOST, 1234), )); let max_slots = Arc::new(MaxSlots::default()); // note that this means that slot 0 will always be considered complete @@ -6384,8 +6384,10 @@ pub mod tests { io.extend_with(rpc_full::FullImpl.to_delegate()); let cluster_info = Arc::new({ let keypair = Arc::new(Keypair::new()); - let contact_info = - ContactInfo::new_with_socketaddr(&keypair.pubkey(), &socketaddr!("127.0.0.1:1234")); + let contact_info = ContactInfo::new_with_socketaddr( + &keypair.pubkey(), + &socketaddr!(Ipv4Addr::LOCALHOST, 1234), + ); ClusterInfo::new(contact_info, keypair, SocketAddrSpace::Unspecified) }); let tpu_address = cluster_info.my_contact_info().tpu; diff --git a/test-validator/src/lib.rs b/test-validator/src/lib.rs index 1d20306d6c..700f4469b0 100644 --- a/test-validator/src/lib.rs +++ b/test-validator/src/lib.rs @@ -91,7 +91,7 @@ impl Default for TestValidatorNodeConfig { let port_range = (MIN_PORT_RANGE, MAX_PORT_RANGE); Self { - gossip_addr: socketaddr!("127.0.0.1:0"), + gossip_addr: socketaddr!(Ipv4Addr::LOCALHOST, 0), port_range, bind_ip_addr, }