Skip to content

Commit

Permalink
feat: let old nodes open ws listen by default
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Dec 5, 2024
1 parent 9df053c commit 3f8e075
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 54 additions & 21 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,30 +1163,63 @@ impl NetworkService {
let p2p_control: ServiceAsyncControl = p2p_control.clone().into();
handle.spawn_task(async move {
#[cfg(not(target_family = "wasm"))]
for addr in &config.listen_addresses {
match p2p_service.listen(addr.to_owned()).await {
Ok(listen_address) => {
info!("Listen on address: {}", listen_address);
network_state
.listened_addrs
.write()
.push(listen_address.clone());
}
Err(err) => {
warn!(
"Listen on address {} failed, due to error: {}",
addr.clone(),
err
);
start_sender
.send(Err(Error::P2P(P2PError::Transport(err))))
.expect("channel abnormal shutdown");
return;
{
let listen_addresses = {
let mut addresses = config
.listen_addresses
.clone()
.into_iter()
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
if config.reuse_tcp_with_ws {
let has_ws = addresses
.iter()
.any(|a| matches!(find_type(a), TransportType::Ws));
if !has_ws {
let ws_listen = {
let mut addr = addresses
.iter()
.find(|a| matches!(find_type(a), TransportType::Tcp))
.expect("Tcp listen must exsit")
.clone();
addr.push(Protocol::Ws);
addr
};
addresses.push(ws_listen);
}

addresses
} else {
addresses
}
};

for addr in &listen_addresses {
match p2p_service.listen(addr.to_owned()).await {
Ok(listen_address) => {
info!("Listen on address: {}", listen_address);
network_state
.listened_addrs
.write()
.push(listen_address.clone());
}
Err(err) => {
warn!(
"Listen on address {} failed, due to error: {}",
addr.clone(),
err
);
start_sender
.send(Err(Error::P2P(P2PError::Transport(err))))
.expect("channel abnormal shutdown");
return;
}
};
}
start_sender.send(Ok(())).unwrap();
}
#[cfg(not(target_family = "wasm"))]
start_sender.send(Ok(())).unwrap();

p2p::runtime::spawn(async move { p2p_service.run().await });
tokio::select! {
_ = receiver.cancelled() => {
Expand Down
2 changes: 2 additions & 0 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ bootnodes = [] # {{
# whitelist_peers = []
### Enable `SO_REUSEPORT` feature to reuse port on Linux, not supported on other OS yet
# reuse_port_on_linux = true
### Allow ckb to upgrade tcp listening to tcp + ws listening when only tcp listening is found
# reuse_tcp_with_ws = true

max_peers = 125
max_outbound_peers = 8
Expand Down
3 changes: 3 additions & 0 deletions util/app-config/src/configs/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub struct Config {
/// Network use reuse port or not
#[serde(default = "default_reuse")]
pub reuse_port_on_linux: bool,
/// Allow ckb to upgrade tcp listening to tcp + ws listening when only tcp listening is found
#[serde(default = "default_reuse")]
pub reuse_tcp_with_ws: bool,
/// Chain synchronization config options.
#[serde(default)]
pub sync: SyncConfig,
Expand Down

0 comments on commit 3f8e075

Please sign in to comment.