Add port and gossip options to solana-test-validator (#16696)
This commit is contained in:
@ -3358,6 +3358,7 @@ impl Node {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_gossip_port(
|
||||
gossip_addr: &SocketAddr,
|
||||
port_range: PortRange,
|
||||
@ -3378,6 +3379,60 @@ impl Node {
|
||||
bind_in_range(bind_ip_addr, port_range).expect("Failed to bind")
|
||||
}
|
||||
|
||||
pub fn new_single_bind(
|
||||
pubkey: &Pubkey,
|
||||
gossip_addr: &SocketAddr,
|
||||
port_range: PortRange,
|
||||
bind_ip_addr: IpAddr,
|
||||
) -> Self {
|
||||
let (gossip_port, (gossip, ip_echo)) =
|
||||
Self::get_gossip_port(gossip_addr, port_range, bind_ip_addr);
|
||||
let (tvu_port, tvu) = Self::bind(bind_ip_addr, port_range);
|
||||
let (tvu_forwards_port, tvu_forwards) = Self::bind(bind_ip_addr, port_range);
|
||||
let (tpu_port, tpu) = Self::bind(bind_ip_addr, port_range);
|
||||
let (tpu_forwards_port, tpu_forwards) = Self::bind(bind_ip_addr, port_range);
|
||||
let (_, retransmit_socket) = Self::bind(bind_ip_addr, port_range);
|
||||
let (repair_port, repair) = Self::bind(bind_ip_addr, port_range);
|
||||
let (serve_repair_port, serve_repair) = Self::bind(bind_ip_addr, port_range);
|
||||
let (_, broadcast) = Self::bind(bind_ip_addr, port_range);
|
||||
|
||||
let rpc_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap();
|
||||
let rpc_pubsub_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap();
|
||||
|
||||
let info = ContactInfo {
|
||||
id: *pubkey,
|
||||
gossip: SocketAddr::new(gossip_addr.ip(), gossip_port),
|
||||
tvu: SocketAddr::new(gossip_addr.ip(), tvu_port),
|
||||
tvu_forwards: SocketAddr::new(gossip_addr.ip(), tvu_forwards_port),
|
||||
repair: SocketAddr::new(gossip_addr.ip(), repair_port),
|
||||
tpu: SocketAddr::new(gossip_addr.ip(), tpu_port),
|
||||
tpu_forwards: SocketAddr::new(gossip_addr.ip(), tpu_forwards_port),
|
||||
unused: socketaddr_any!(),
|
||||
rpc: SocketAddr::new(gossip_addr.ip(), rpc_port),
|
||||
rpc_pubsub: SocketAddr::new(gossip_addr.ip(), rpc_pubsub_port),
|
||||
serve_repair: SocketAddr::new(gossip_addr.ip(), serve_repair_port),
|
||||
wallclock: timestamp(),
|
||||
shred_version: 0,
|
||||
};
|
||||
trace!("new ContactInfo: {:?}", info);
|
||||
|
||||
Node {
|
||||
info,
|
||||
sockets: Sockets {
|
||||
gossip,
|
||||
ip_echo: Some(ip_echo),
|
||||
tvu: vec![tvu],
|
||||
tvu_forwards: vec![tvu_forwards],
|
||||
tpu: vec![tpu],
|
||||
tpu_forwards: vec![tpu_forwards],
|
||||
broadcast: vec![broadcast],
|
||||
repair,
|
||||
retransmit_sockets: vec![retransmit_socket],
|
||||
serve_repair,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_external_ip(
|
||||
pubkey: &Pubkey,
|
||||
gossip_addr: &SocketAddr,
|
||||
|
@ -7,6 +7,7 @@ use {
|
||||
},
|
||||
solana_client::rpc_client::RpcClient,
|
||||
solana_ledger::{blockstore::create_new_ledger, create_new_tmp_ledger},
|
||||
solana_net_utils::PortRange,
|
||||
solana_runtime::{
|
||||
bank_forks::{ArchiveFormat, SnapshotConfig, SnapshotVersion},
|
||||
genesis_utils::create_genesis_config_with_leader_ex,
|
||||
@ -42,6 +43,29 @@ pub struct ProgramInfo {
|
||||
pub program_path: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TestValidatorNodeConfig {
|
||||
gossip_addr: SocketAddr,
|
||||
port_range: PortRange,
|
||||
bind_ip_addr: IpAddr,
|
||||
}
|
||||
|
||||
impl Default for TestValidatorNodeConfig {
|
||||
fn default() -> Self {
|
||||
const MIN_PORT_RANGE: u16 = 1024;
|
||||
const MAX_PORT_RANGE: u16 = 65535;
|
||||
|
||||
let bind_ip_addr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0));
|
||||
let port_range = (MIN_PORT_RANGE, MAX_PORT_RANGE);
|
||||
|
||||
Self {
|
||||
gossip_addr: socketaddr!("127.0.0.1:0"),
|
||||
port_range,
|
||||
bind_ip_addr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TestValidatorGenesis {
|
||||
fee_rate_governor: FeeRateGovernor,
|
||||
@ -54,6 +78,7 @@ pub struct TestValidatorGenesis {
|
||||
accounts: HashMap<Pubkey, AccountSharedData>,
|
||||
programs: Vec<ProgramInfo>,
|
||||
epoch_schedule: Option<EpochSchedule>,
|
||||
node_config: TestValidatorNodeConfig,
|
||||
pub validator_exit: Arc<RwLock<ValidatorExit>>,
|
||||
pub start_progress: Arc<RwLock<ValidatorStartProgress>>,
|
||||
pub authorized_voter_keypairs: Arc<RwLock<Vec<Arc<Keypair>>>>,
|
||||
@ -110,6 +135,26 @@ impl TestValidatorGenesis {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn gossip_host(&mut self, gossip_host: IpAddr) -> &mut Self {
|
||||
self.node_config.gossip_addr.set_ip(gossip_host);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn gossip_port(&mut self, gossip_port: u16) -> &mut Self {
|
||||
self.node_config.gossip_addr.set_port(gossip_port);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn port_range(&mut self, port_range: PortRange) -> &mut Self {
|
||||
self.node_config.port_range = port_range;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn bind_ip_addr(&mut self, bind_ip_addr: IpAddr) -> &mut Self {
|
||||
self.node_config.bind_ip_addr = bind_ip_addr;
|
||||
self
|
||||
}
|
||||
|
||||
/// Add an account to the test environment
|
||||
pub fn add_account(&mut self, address: Pubkey, account: AccountSharedData) -> &mut Self {
|
||||
self.accounts.insert(address, account);
|
||||
@ -398,7 +443,12 @@ impl TestValidator {
|
||||
.unwrap(),
|
||||
)?;
|
||||
|
||||
let mut node = Node::new_localhost_with_pubkey(&validator_identity.pubkey());
|
||||
let mut node = Node::new_single_bind(
|
||||
&validator_identity.pubkey(),
|
||||
&config.node_config.gossip_addr,
|
||||
config.node_config.port_range,
|
||||
config.node_config.bind_ip_addr,
|
||||
);
|
||||
if let Some((rpc, rpc_pubsub)) = config.rpc_ports {
|
||||
node.info.rpc = SocketAddr::new(node.info.gossip.ip(), rpc);
|
||||
node.info.rpc_pubsub = SocketAddr::new(node.info.gossip.ip(), rpc_pubsub);
|
||||
|
Reference in New Issue
Block a user