Use Ipv4Addr constants in socketaddr! (#30095)

This commit is contained in:
Kevin Ji 2023-02-02 15:48:21 -08:00 committed by GitHub
parent 37461976e0
commit cd51499ab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 42 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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(),

View File

@ -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::<std::net::SocketAddr>().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());

View File

@ -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;

View File

@ -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,
}