Skip to content

Commit fc8e0d6

Browse files
committed
feat: let old nodes open ws listen by default
1 parent 9df053c commit fc8e0d6

File tree

5 files changed

+77
-44
lines changed

5 files changed

+77
-44
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

network/src/network.rs

+58-23
Original file line numberDiff line numberDiff line change
@@ -1163,30 +1163,65 @@ impl NetworkService {
11631163
let p2p_control: ServiceAsyncControl = p2p_control.clone().into();
11641164
handle.spawn_task(async move {
11651165
#[cfg(not(target_family = "wasm"))]
1166-
for addr in &config.listen_addresses {
1167-
match p2p_service.listen(addr.to_owned()).await {
1168-
Ok(listen_address) => {
1169-
info!("Listen on address: {}", listen_address);
1170-
network_state
1171-
.listened_addrs
1172-
.write()
1173-
.push(listen_address.clone());
1174-
}
1175-
Err(err) => {
1176-
warn!(
1177-
"Listen on address {} failed, due to error: {}",
1178-
addr.clone(),
1179-
err
1180-
);
1181-
start_sender
1182-
.send(Err(Error::P2P(P2PError::Transport(err))))
1183-
.expect("channel abnormal shutdown");
1184-
return;
1166+
{
1167+
let listen_addresses = {
1168+
let mut addresses = config.listen_addresses.clone();
1169+
if config.reuse_tcp_with_ws {
1170+
let ws_listens = addresses
1171+
.iter()
1172+
.cloned()
1173+
.filter_map(|mut addr| {
1174+
if matches!(find_type(&addr), TransportType::Tcp) {
1175+
addr.push(Protocol::Ws);
1176+
Some(addr)
1177+
} else {
1178+
None
1179+
}
1180+
})
1181+
.collect::<Vec<_>>();
1182+
1183+
addresses.extend(ws_listens);
11851184
}
1185+
let mut addresses = addresses
1186+
.into_iter()
1187+
.collect::<HashSet<_>>()
1188+
.into_iter()
1189+
.collect::<Vec<_>>();
1190+
addresses.sort_by(|a, b| {
1191+
let ty_a = find_type(a);
1192+
let ty_b = find_type(b);
1193+
1194+
ty_a.cmp(&ty_b)
1195+
});
1196+
1197+
addresses
11861198
};
1199+
1200+
for addr in &listen_addresses {
1201+
match p2p_service.listen(addr.to_owned()).await {
1202+
Ok(listen_address) => {
1203+
info!("Listen on address: {}", listen_address);
1204+
network_state
1205+
.listened_addrs
1206+
.write()
1207+
.push(listen_address.clone());
1208+
}
1209+
Err(err) => {
1210+
warn!(
1211+
"Listen on address {} failed, due to error: {}",
1212+
addr.clone(),
1213+
err
1214+
);
1215+
start_sender
1216+
.send(Err(Error::P2P(P2PError::Transport(err))))
1217+
.expect("channel abnormal shutdown");
1218+
return;
1219+
}
1220+
};
1221+
}
1222+
start_sender.send(Ok(())).unwrap();
11871223
}
1188-
#[cfg(not(target_family = "wasm"))]
1189-
start_sender.send(Ok(())).unwrap();
1224+
11901225
p2p::runtime::spawn(async move { p2p_service.run().await });
11911226
tokio::select! {
11921227
_ = receiver.cancelled() => {
@@ -1485,10 +1520,10 @@ pub(crate) async fn async_disconnect_with_message(
14851520
control.disconnect(peer_index).await
14861521
}
14871522

1488-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1523+
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
14891524
pub(crate) enum TransportType {
1490-
Ws,
14911525
Tcp,
1526+
Ws,
14921527
}
14931528

14941529
pub(crate) fn find_type(addr: &Multiaddr) -> TransportType {

resource/ckb.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ cache_size = 268435456
6363
options_file = "default.db-options"
6464

6565
[network]
66-
listen_addresses = ["/ip4/0.0.0.0/tcp/8115", "/ip4/0.0.0.0/tcp/8115/ws"] # {{
67-
# _ => listen_addresses = ["/ip4/0.0.0.0/tcp/{p2p_port}", "/ip4/0.0.0.0/tcp/{p2p_port}/ws"]
66+
listen_addresses = ["/ip4/0.0.0.0/tcp/8115"] # {{
67+
# _ => listen_addresses = ["/ip4/0.0.0.0/tcp/{p2p_port}"]
6868
# }}
6969
### Specify the public and routable network addresses
7070
# public_addresses = []
@@ -84,6 +84,8 @@ bootnodes = [] # {{
8484
# whitelist_peers = []
8585
### Enable `SO_REUSEPORT` feature to reuse port on Linux, not supported on other OS yet
8686
# reuse_port_on_linux = true
87+
### Allow ckb to upgrade tcp listening to tcp + ws listening when only tcp listening is found
88+
# reuse_tcp_with_ws = true
8789

8890
max_peers = 125
8991
max_outbound_peers = 8

util/app-config/src/configs/network.rs

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub struct Config {
8383
/// Network use reuse port or not
8484
#[serde(default = "default_reuse")]
8585
pub reuse_port_on_linux: bool,
86+
/// Allow ckb to upgrade tcp listening to tcp + ws listening
87+
#[serde(default = "default_reuse_tcp_with_ws")]
88+
pub reuse_tcp_with_ws: bool,
8689
/// Chain synchronization config options.
8790
#[serde(default)]
8891
pub sync: SyncConfig,
@@ -353,3 +356,8 @@ impl Config {
353356
const fn default_reuse() -> bool {
354357
true
355358
}
359+
360+
/// By default, allow ckb to upgrade tcp listening to tcp + ws listening
361+
const fn default_reuse_tcp_with_ws() -> bool {
362+
true
363+
}

util/app-config/src/tests/app_config.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ fn test_export_dev_config_files() {
5555
);
5656
assert_eq!(
5757
ckb_config.network.listen_addresses,
58-
vec![
59-
"/ip4/0.0.0.0/tcp/8000".parse().unwrap(),
60-
"/ip4/0.0.0.0/tcp/8000/ws".parse().unwrap()
61-
]
58+
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
6259
);
6360
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 15);
6461
assert_eq!(ckb_config.rpc.listen_address, "127.0.0.1:7000");
@@ -151,10 +148,7 @@ fn test_export_testnet_config_files() {
151148
);
152149
assert_eq!(
153150
ckb_config.network.listen_addresses,
154-
vec![
155-
"/ip4/0.0.0.0/tcp/8000".parse().unwrap(),
156-
"/ip4/0.0.0.0/tcp/8000/ws".parse().unwrap()
157-
]
151+
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
158152
);
159153
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 15);
160154
assert_eq!(ckb_config.rpc.listen_address, "127.0.0.1:7000");
@@ -206,10 +200,7 @@ fn test_export_integration_config_files() {
206200
);
207201
assert_eq!(
208202
ckb_config.network.listen_addresses,
209-
vec![
210-
"/ip4/0.0.0.0/tcp/8000".parse().unwrap(),
211-
"/ip4/0.0.0.0/tcp/8000/ws".parse().unwrap()
212-
]
203+
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
213204
);
214205
assert_eq!(ckb_config.rpc.listen_address, "127.0.0.1:7000");
215206
}
@@ -261,10 +252,7 @@ fn test_export_dev_config_files_assembly() {
261252
);
262253
assert_eq!(
263254
ckb_config.network.listen_addresses,
264-
vec![
265-
"/ip4/0.0.0.0/tcp/8000".parse().unwrap(),
266-
"/ip4/0.0.0.0/tcp/8000/ws".parse().unwrap()
267-
]
255+
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
268256
);
269257
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 15);
270258
assert_eq!(ckb_config.rpc.listen_address, "127.0.0.1:7000");

0 commit comments

Comments
 (0)