From 3f8e0755aa3c3018b07c31c3c51119c60d695958 Mon Sep 17 00:00:00 2001 From: driftluo Date: Thu, 5 Dec 2024 15:34:12 +0800 Subject: [PATCH] feat: let old nodes open ws listen by default --- Cargo.lock | 6 +-- network/src/network.rs | 75 ++++++++++++++++++-------- resource/ckb.toml | 2 + util/app-config/src/configs/network.rs | 3 ++ 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5bd3e580f6..9c6446cfce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3007,9 +3007,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.0" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" [[package]] name = "hashlink" @@ -3431,7 +3431,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown 0.15.0", + "hashbrown 0.15.2", ] [[package]] diff --git a/network/src/network.rs b/network/src/network.rs index 6134e7cb12..046cad9adb 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -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::>() + .into_iter() + .collect::>(); + 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() => { diff --git a/resource/ckb.toml b/resource/ckb.toml index 3df8e09dde..dba35b9757 100644 --- a/resource/ckb.toml +++ b/resource/ckb.toml @@ -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 diff --git a/util/app-config/src/configs/network.rs b/util/app-config/src/configs/network.rs index 70d8cb3550..0a19523fb2 100644 --- a/util/app-config/src/configs/network.rs +++ b/util/app-config/src/configs/network.rs @@ -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,