Skip to content

Commit

Permalink
Merge pull request #4741 from driftluo/open-ws-listen-by-default
Browse files Browse the repository at this point in the history
feat: let old config node open ws listen by default
  • Loading branch information
zhangsoledad authored Dec 5, 2024
2 parents 9df053c + fc8e0d6 commit 14e769b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 44 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.

81 changes: 58 additions & 23 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,30 +1163,65 @@ 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();
if config.reuse_tcp_with_ws {
let ws_listens = addresses
.iter()
.cloned()
.filter_map(|mut addr| {
if matches!(find_type(&addr), TransportType::Tcp) {
addr.push(Protocol::Ws);
Some(addr)
} else {
None
}
})
.collect::<Vec<_>>();

addresses.extend(ws_listens);
}
let mut addresses = addresses
.into_iter()
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
addresses.sort_by(|a, b| {
let ty_a = find_type(a);
let ty_b = find_type(b);

ty_a.cmp(&ty_b)
});

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 Expand Up @@ -1485,10 +1520,10 @@ pub(crate) async fn async_disconnect_with_message(
control.disconnect(peer_index).await
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) enum TransportType {
Ws,
Tcp,
Ws,
}

pub(crate) fn find_type(addr: &Multiaddr) -> TransportType {
Expand Down
6 changes: 4 additions & 2 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ cache_size = 268435456
options_file = "default.db-options"

[network]
listen_addresses = ["/ip4/0.0.0.0/tcp/8115", "/ip4/0.0.0.0/tcp/8115/ws"] # {{
# _ => listen_addresses = ["/ip4/0.0.0.0/tcp/{p2p_port}", "/ip4/0.0.0.0/tcp/{p2p_port}/ws"]
listen_addresses = ["/ip4/0.0.0.0/tcp/8115"] # {{
# _ => listen_addresses = ["/ip4/0.0.0.0/tcp/{p2p_port}"]
# }}
### Specify the public and routable network addresses
# public_addresses = []
Expand All @@ -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
8 changes: 8 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
#[serde(default = "default_reuse_tcp_with_ws")]
pub reuse_tcp_with_ws: bool,
/// Chain synchronization config options.
#[serde(default)]
pub sync: SyncConfig,
Expand Down Expand Up @@ -353,3 +356,8 @@ impl Config {
const fn default_reuse() -> bool {
true
}

/// By default, allow ckb to upgrade tcp listening to tcp + ws listening
const fn default_reuse_tcp_with_ws() -> bool {
true
}
20 changes: 4 additions & 16 deletions util/app-config/src/tests/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ fn test_export_dev_config_files() {
);
assert_eq!(
ckb_config.network.listen_addresses,
vec![
"/ip4/0.0.0.0/tcp/8000".parse().unwrap(),
"/ip4/0.0.0.0/tcp/8000/ws".parse().unwrap()
]
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
);
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 15);
assert_eq!(ckb_config.rpc.listen_address, "127.0.0.1:7000");
Expand Down Expand Up @@ -151,10 +148,7 @@ fn test_export_testnet_config_files() {
);
assert_eq!(
ckb_config.network.listen_addresses,
vec![
"/ip4/0.0.0.0/tcp/8000".parse().unwrap(),
"/ip4/0.0.0.0/tcp/8000/ws".parse().unwrap()
]
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
);
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 15);
assert_eq!(ckb_config.rpc.listen_address, "127.0.0.1:7000");
Expand Down Expand Up @@ -206,10 +200,7 @@ fn test_export_integration_config_files() {
);
assert_eq!(
ckb_config.network.listen_addresses,
vec![
"/ip4/0.0.0.0/tcp/8000".parse().unwrap(),
"/ip4/0.0.0.0/tcp/8000/ws".parse().unwrap()
]
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
);
assert_eq!(ckb_config.rpc.listen_address, "127.0.0.1:7000");
}
Expand Down Expand Up @@ -261,10 +252,7 @@ fn test_export_dev_config_files_assembly() {
);
assert_eq!(
ckb_config.network.listen_addresses,
vec![
"/ip4/0.0.0.0/tcp/8000".parse().unwrap(),
"/ip4/0.0.0.0/tcp/8000/ws".parse().unwrap()
]
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
);
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 15);
assert_eq!(ckb_config.rpc.listen_address, "127.0.0.1:7000");
Expand Down

0 comments on commit 14e769b

Please sign in to comment.