Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion substrate/cli/src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ args:
long: dev
help: Run in development mode; implies --chain=dev --validator --key Alice
takes_value: false
- listen-addr:
long: listen-addr
value_name: LISTEN_ADDR
help: Listen on this multiaddress
takes_value: true
multiple: true
- port:
long: port
value_name: PORT
help: Specify p2p protocol TCP port
help: Specify p2p protocol TCP port. Only used if --listen-addr is not specified.
takes_value: true
- rpc-external:
long: rpc-external
Expand Down
26 changes: 18 additions & 8 deletions substrate/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,25 @@ where
config.network.non_reserved_mode = NonReservedPeerMode::Deny;
}

let port = match matches.value_of("port") {
Some(port) => port.parse().map_err(|_| "Invalid p2p port value specified.")?,
None => 30333,
};
config.network.listen_addresses = Vec::new();
for addr in matches.values_of("listen-addr").unwrap_or_default() {
let addr = addr.parse().map_err(|_| "Invalid listen multiaddress")?;
config.network.listen_addresses.push(addr);
}
if config.network.listen_addresses.is_empty() {
let port = match matches.value_of("port") {
Some(port) => port.parse().map_err(|_| "Invalid p2p port value specified.")?,
None => 30333,
};
config.network.listen_addresses = vec![
iter::once(AddrComponent::IP4(Ipv4Addr::new(0, 0, 0, 0)))
.chain(iter::once(AddrComponent::TCP(port)))
.collect()
];
}

config.network.public_addresses = Vec::new();

config.network.listen_address = iter::once(AddrComponent::IP4(Ipv4Addr::new(0, 0, 0, 0)))
.chain(iter::once(AddrComponent::TCP(port)))
.collect();
config.network.public_address = None;
config.network.client_version = config.client_id();
config.network.use_secret = match matches.value_of("node-key").map(|s| s.parse()) {
Some(Ok(secret)) => Some(secret),
Expand Down
35 changes: 17 additions & 18 deletions substrate/network-libp2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ impl NetworkService {

let local_peer_id = network_state.local_public_key().clone()
.into_peer_id();
let mut listen_addr = config.listen_address.clone();
listen_addr.append(AddrComponent::P2P(local_peer_id.clone().into_bytes()));
info!(target: "sub-libp2p", "Local node address is: {}", listen_addr);
for mut addr in config.listen_addresses.iter().cloned() {
addr.append(AddrComponent::P2P(local_peer_id.clone().into_bytes()));
info!(target: "sub-libp2p", "Local node address is: {}", addr);
}

let kad_system = KadSystem::without_init(KadSystemConfig {
parallelism: 3,
Expand All @@ -129,10 +130,7 @@ impl NetworkService {
let (close_tx, close_rx) = oneshot::channel();
let (timeouts_register_tx, timeouts_register_rx) = mpsc::unbounded();

let mut listened_addrs = Vec::new();
if let Some(ref addr) = config.public_address {
listened_addrs.push(addr.clone());
}
let listened_addrs = config.public_addresses.clone();

let shared = Arc::new(Shared {
network_state,
Expand Down Expand Up @@ -445,17 +443,18 @@ fn init_thread(
)
};

// Listen on multiaddress.
match swarm_controller.listen_on(shared.config.listen_address.clone()) {
Ok(new_addr) => {
debug!(target: "sub-libp2p", "Libp2p listening on {}", new_addr);
*shared.original_listened_addr.write() = Some(new_addr.clone());
},
Err(_) => {
warn!(target: "sub-libp2p", "Can't listen on {}, protocol not supported",
shared.config.listen_address);
return Err(ErrorKind::BadProtocol.into())
},
// Listen on multiaddresses.
for addr in &shared.config.listen_addresses {
match swarm_controller.listen_on(addr.clone()) {
Ok(new_addr) => {
debug!(target: "sub-libp2p", "Libp2p listening on {}", new_addr);
*shared.original_listened_addr.write() = Some(new_addr.clone());
},
Err(_) => {
warn!(target: "sub-libp2p", "Can't listen on {}, protocol not supported", addr);
return Err(ErrorKind::BadProtocol.into())
},
}
}

// Explicitely connect to _all_ the boostrap nodes as a temporary measure.
Expand Down
26 changes: 15 additions & 11 deletions substrate/network-libp2p/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ pub struct NetworkConfiguration {
pub config_path: Option<String>,
/// Directory path to store network-specific configuration. None means nothing will be saved
pub net_config_path: Option<String>,
/// IP address to listen for incoming connections. Listen to all connections by default
pub listen_address: Multiaddr,
/// IP address to advertise. Detected automatically if none.
pub public_address: Option<Multiaddr>,
/// Multiaddresses to listen for incoming connections.
pub listen_addresses: Vec<Multiaddr>,
/// Multiaddresses to advertise. Detected automatically if empty.
pub public_addresses: Vec<Multiaddr>,
/// List of initial node addresses
pub boot_nodes: Vec<String>,
/// Use provided node key instead of default
Expand Down Expand Up @@ -136,10 +136,12 @@ impl NetworkConfiguration {
NetworkConfiguration {
config_path: None,
net_config_path: None,
listen_address: iter::once(AddrComponent::IP4(Ipv4Addr::new(0, 0, 0, 0)))
.chain(iter::once(AddrComponent::TCP(30333)))
.collect(),
public_address: None,
listen_addresses: vec![
iter::once(AddrComponent::IP4(Ipv4Addr::new(0, 0, 0, 0)))
.chain(iter::once(AddrComponent::TCP(30333)))
.collect()
],
public_addresses: Vec::new(),
boot_nodes: Vec::new(),
use_secret: None,
min_peers: 25,
Expand All @@ -153,9 +155,11 @@ impl NetworkConfiguration {
/// Create new default configuration for localhost-only connection with random port (useful for testing)
pub fn new_local() -> NetworkConfiguration {
let mut config = NetworkConfiguration::new();
config.listen_address = iter::once(AddrComponent::IP4(Ipv4Addr::new(127, 0, 0, 1)))
.chain(iter::once(AddrComponent::TCP(0)))
.collect();
config.listen_addresses = vec![
iter::once(AddrComponent::IP4(Ipv4Addr::new(127, 0, 0, 1)))
.chain(iter::once(AddrComponent::TCP(0)))
.collect()
];
config
}
}
Expand Down