Skip to content

Commit dd2bede

Browse files
authored
Replace the TCP swarm with QUIC. (#1585)
* Replace the TCP swarm with a QUIC swarm. * Removed libp2p:tcp feature. * Altered startup scripts and configs to use UDP/3333 instead of TCP. * Corrected copy-paste mistake. * Trigger hole-punching. * Revert "Trigger hole-punching." This reverts commit c04d5a8. * Minor tweak.
1 parent d983e98 commit dd2bede

11 files changed

+58
-64
lines changed

infra/config_networking_test.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
otlp_collector_endpoint = "http://otel-collector:4317"
22
bootstrap_address = [
33
"12D3KooWPXw2dXBRH1bT4vcNos9f6W2KoFTiarqptBuTzxaXg7zu",
4-
"/ip4/198.51.100.103/tcp/5643",
4+
"/ip4/198.51.100.103/udp/5643/quic-v1",
55
]
66
p2p_port = 5643
77

z2/docs/converter.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
3030
cat > config.toml <<-EOF
3131
p2p_port = 3333
32-
bootstrap_address = [ "12D3KooWNYaasyfY1wFrSHga3WBdZkb7bGhQiUz9926bQvk4HQj2", "/ip4/10.40.5.16/tcp/3333" ]
32+
bootstrap_address = [ "12D3KooWNYaasyfY1wFrSHga3WBdZkb7bGhQiUz9926bQvk4HQj2", "/ip4/10.40.5.16/udp/3333/quic-v1" ]
3333
3434
[[nodes]]
3535
eth_chain_id = 33101 # change the chain it for your conversion

z2/resources/chain-specs/zq2-devnet.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
p2p_port = 3333
2-
bootstrap_address = [ "12D3KooWJNUNgs1UM9mw1Pfskxk4o3VWz37cpneSHZsUecGGZgkb", "/ip4/35.198.245.220/tcp/3333" ]
2+
bootstrap_address = [ "12D3KooWJNUNgs1UM9mw1Pfskxk4o3VWz37cpneSHZsUecGGZgkb", "/ip4/35.198.245.220/udp/3333/quic-v1" ]
33

44
[[nodes]]
55
eth_chain_id = 33469

z2/resources/chain-specs/zq2-perftest.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
p2p_port = 3333
2-
bootstrap_address = [ "12D3KooWLQErYMNECpTd22VtTQeGXRf18yeeEM32rVAWZ9qXdLUB", "/ip4/34.143.150.198/tcp/3333" ]
2+
bootstrap_address = [ "12D3KooWLQErYMNECpTd22VtTQeGXRf18yeeEM32rVAWZ9qXdLUB", "/ip4/34.143.150.198/udp/3333/quic-v1" ]
33

44
[[nodes]]
55
eth_chain_id = 33469

z2/resources/chain-specs/zq2-prototestnet.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
p2p_port = 3333
2-
bootstrap_address = [ "12D3KooWEpeCSbza6zYXJ8PY8v6iEZYzYD4mhk4QbmHGbjx186dS", "/ip4/34.87.62.126/tcp/3333" ]
2+
bootstrap_address = [ "12D3KooWEpeCSbza6zYXJ8PY8v6iEZYzYD4mhk4QbmHGbjx186dS", "/ip4/34.87.62.126/udp/3333/quic-v1" ]
33

44
[[nodes]]
55
eth_chain_id = 33103

z2/resources/chain-specs/zq2-uccbtest.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
p2p_port = 3333
2-
bootstrap_address = [ "12D3KooWReRperXgN12FUH6yRTXrcV3u98E753356AR1sAEJefup", "/ip4/34.87.127.118/tcp/3333" ]
2+
bootstrap_address = [ "12D3KooWReRperXgN12FUH6yRTXrcV3u98E753356AR1sAEJefup", "/ip4/34.87.127.118/udp/3333/quic-v1" ]
33

44
[[nodes]]
55
eth_chain_id = 33469

z2/resources/config.tera.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
p2p_port = 3333
22

33
{%- if role != "bootstrap" %}
4-
bootstrap_address = [ "{{ bootstrap_peer_id }}", "/ip4/{{ bootstrap_public_ip }}/tcp/3333" ]
4+
bootstrap_address = [ "{{ bootstrap_peer_id }}", "/ip4/{{ bootstrap_public_ip }}/udp/3333/quic-v1" ]
55
{%- endif %}
66

77
[[nodes]]

z2/resources/node_provision.tera.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def health():
107107
108108
start() {
109109
docker rm zilliqa-""" + VERSIONS.get('zilliqa') + """ &> /dev/null || echo 0
110-
docker run -td -p 3333:3333 -p 4201:4201 --net=host --name zilliqa-""" + VERSIONS.get('zilliqa') + """ \
110+
docker run -td -p 3333:3333/udp -p 4201:4201 --net=host --name zilliqa-""" + VERSIONS.get('zilliqa') + """ \
111111
--log-driver json-file --log-opt max-size=1g --log-opt max-file=30 \
112112
-e RUST_LOG="zilliqa=trace" -e RUST_BACKTRACE=1 \
113113
-v /config.toml:/config.toml -v /zilliqa.log:/zilliqa.log -v /data:/data \

z2/resources/start_node.tera.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ start() {
7878
MOUNT_OPTION=""
7979
fi
8080
DOCKER_COMMAND="docker run -td \
81-
-p 3333:3333 \
81+
-p 3333:3333/udp \
8282
-p 4201:4201 \
8383
--net=host \
8484
--name zilliqa-${ZQ_VERSION} \

zilliqa/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ hyper = "1.4.1"
4545
itertools = "0.13.0"
4646
jsonrpsee = { version = "0.24.3", features = ["jsonrpsee-http-client", "server"] }
4747
k256 = {version = "0.13.4", features = ["serde", "pem"] }
48-
libp2p = { version = "0.54.0", features = ["cbor", "dns", "gossipsub", "macros", "tcp", "tokio", "noise", "mdns", "request-response", "kad", "identify", "serde", "yamux", "autonat"] }
48+
libp2p = { version = "0.54.0", features = ["cbor", "dns", "gossipsub", "macros", "tokio", "noise", "mdns", "request-response", "kad", "identify", "serde", "yamux", "autonat", "quic"] }
4949
lru = "0.12"
5050
once_cell = "1.20.1"
5151
opentelemetry = { version = "0.26.0", features = ["metrics"] }

zilliqa/src/p2p_node.rs

+48-54
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ use anyhow::{anyhow, Result};
1111
use cfg_if::cfg_if;
1212
use libp2p::{
1313
autonat,
14-
core::upgrade,
15-
dns,
1614
futures::StreamExt,
1715
gossipsub::{self, IdentTopic, MessageAuthenticity, TopicHash},
1816
identify,
1917
kad::{self, store::MemoryStore},
2018
mdns,
2119
multiaddr::{Multiaddr, Protocol},
22-
noise,
2320
request_response::{self, OutboundFailure, ProtocolSupport},
24-
swarm::{self, dial_opts::DialOpts, NetworkBehaviour, SwarmEvent},
25-
tcp, yamux, PeerId, StreamProtocol, Swarm, Transport,
21+
swarm::{dial_opts::DialOpts, NetworkBehaviour, SwarmEvent},
22+
PeerId, StreamProtocol, Swarm,
2623
};
2724
use tokio::{
2825
select,
@@ -101,51 +98,45 @@ impl P2pNode {
10198
let peer_id = PeerId::from(key_pair.public());
10299
info!(%peer_id);
103100

104-
let transport = tcp::tokio::Transport::new(tcp::Config::default())
105-
.upgrade(upgrade::Version::V1)
106-
.authenticate(noise::Config::new(&key_pair)?)
107-
.multiplex(yamux::Config::default())
108-
.boxed();
109-
let transport = dns::tokio::Transport::system(transport)?.boxed();
110-
111-
let behaviour = Behaviour {
112-
request_response: request_response::cbor::Behaviour::new(
113-
iter::once((StreamProtocol::new("/zq2-message/1"), ProtocolSupport::Full)),
114-
Default::default(),
115-
),
116-
gossipsub: gossipsub::Behaviour::new(
117-
MessageAuthenticity::Signed(key_pair.clone()),
118-
gossipsub::ConfigBuilder::default()
119-
.max_transmit_size(524288)
120-
.build()
101+
let swarm = libp2p::SwarmBuilder::with_existing_identity(key_pair)
102+
.with_tokio()
103+
.with_quic()
104+
.with_dns()?
105+
.with_behaviour(|key_pair| {
106+
Ok(Behaviour {
107+
request_response: request_response::cbor::Behaviour::new(
108+
iter::once((StreamProtocol::new("/zq2-message/1"), ProtocolSupport::Full)),
109+
Default::default(),
110+
),
111+
gossipsub: gossipsub::Behaviour::new(
112+
MessageAuthenticity::Signed(key_pair.clone()),
113+
gossipsub::ConfigBuilder::default()
114+
.max_transmit_size(524288)
115+
.build()
116+
.map_err(|e| anyhow!(e))?,
117+
)
121118
.map_err(|e| anyhow!(e))?,
122-
)
123-
.map_err(|e| anyhow!(e))?,
124-
mdns: mdns::Behaviour::new(Default::default(), peer_id)?,
125-
autonat: autonat::Behaviour::new(
126-
peer_id,
127-
autonat::Config {
128-
// Config changes to speed up autonat initialization. Set back to default if too aggressive.
129-
retry_interval: Duration::from_secs(10),
130-
refresh_interval: Duration::from_secs(30),
131-
boot_delay: Duration::from_secs(5),
132-
throttle_server_period: Duration::ZERO,
133-
..Default::default()
134-
},
135-
),
136-
identify: identify::Behaviour::new(identify::Config::new(
137-
"/ipfs/id/1.0.0".to_owned(),
138-
key_pair.public(),
139-
)),
140-
kademlia: kad::Behaviour::new(peer_id, MemoryStore::new(peer_id)),
141-
};
142-
143-
let swarm = Swarm::new(
144-
transport,
145-
behaviour,
146-
peer_id,
147-
swarm::Config::with_tokio_executor(),
148-
);
119+
mdns: mdns::Behaviour::new(Default::default(), peer_id)?,
120+
autonat: autonat::Behaviour::new(
121+
peer_id,
122+
autonat::Config {
123+
// Config changes to speed up autonat initialization.
124+
// Set back to default if too aggressive.
125+
retry_interval: Duration::from_secs(10),
126+
refresh_interval: Duration::from_secs(30),
127+
boot_delay: Duration::from_secs(5),
128+
throttle_server_period: Duration::ZERO,
129+
..Default::default()
130+
},
131+
),
132+
identify: identify::Behaviour::new(identify::Config::new(
133+
"/ipfs/id/1.0.0".to_owned(),
134+
key_pair.public(),
135+
)),
136+
kademlia: kad::Behaviour::new(peer_id, MemoryStore::new(peer_id)),
137+
})
138+
})?
139+
.build();
149140

150141
Ok(Self {
151142
shard_nodes: HashMap::new(),
@@ -223,10 +214,12 @@ impl P2pNode {
223214
}
224215

225216
pub async fn start(&mut self) -> Result<()> {
226-
let mut addr: Multiaddr = "/ip4/0.0.0.0".parse().unwrap();
227-
addr.push(Protocol::Tcp(self.config.p2p_port));
228-
229-
self.swarm.listen_on(addr)?;
217+
self.swarm.listen_on(
218+
Multiaddr::empty()
219+
.with(Protocol::Ip4(std::net::Ipv4Addr::UNSPECIFIED))
220+
.with(Protocol::Udp(self.config.p2p_port))
221+
.with(Protocol::QuicV1),
222+
)?;
230223

231224
if let Some(bootstrap_address) = &self.config.bootstrap_address {
232225
self.swarm
@@ -242,6 +235,7 @@ impl P2pNode {
242235
if let Some((peer, address)) = &self.config.bootstrap_address {
243236
self.swarm.dial(
244237
DialOpts::peer_id(*peer)
238+
.override_role() // hole-punch
245239
.addresses(vec![address.clone()])
246240
.build(),
247241
)?;
@@ -263,7 +257,7 @@ impl P2pNode {
263257
debug!(?event, "swarm event");
264258
match event {
265259
SwarmEvent::NewListenAddr { address, .. } => {
266-
info!(%address, "started listening");
260+
info!(%address, "P2P swarm listening on");
267261
}
268262
SwarmEvent::Behaviour(BehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
269263
for (peer_id, addr) in list {

0 commit comments

Comments
 (0)