Skip to content

Commit 38df87c

Browse files
jxsAgeManningpawanjay176antondlrmichaelsproul
authored
Switch libp2p sigp gossipsub fork (#4999)
* switch libp2p source to sigp fork * Shift the connection closing inside RPC behaviour * Tag specific commits * Add slow peer scoring * Fix test * Use default yamux config * Pin discv5 to our libp2p fork and cargo update * Upgrade libp2p to enable yamux gains * Add a comment specifying the branch being used * cleanup build output from within container (prevents CI warnings related to fs permissions) * Remove revision tags add branches for testing, will revert back once we're happy * Update to latest rust-libp2p version * Pin forks * Update cargo.lock * Re-pin to panic-free rust --------- Co-authored-by: Age Manning <[email protected]> Co-authored-by: Pawan Dhananjay <[email protected]> Co-authored-by: antondlr <[email protected]> Co-authored-by: Michael Sproul <[email protected]>
1 parent be79f74 commit 38df87c

File tree

17 files changed

+1046
-976
lines changed

17 files changed

+1046
-976
lines changed

Cargo.lock

Lines changed: 854 additions & 814 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ criterion = "0.3"
105105
delay_map = "0.3"
106106
derivative = "2"
107107
dirs = "3"
108-
discv5 = { version = "0.3", features = ["libp2p"] }
108+
discv5 = { git="https://github.com/sigp/discv5", rev="dbb4a718cd32eaed8127c3c8241bfd0fde9eb908", features = ["libp2p"] }
109109
env_logger = "0.9"
110110
error-chain = "0.12"
111111
ethereum-types = "0.14"
@@ -149,7 +149,7 @@ slog = { version = "2", features = ["max_level_trace", "release_max_level_trace"
149149
slog-async = "2"
150150
slog-term = "2"
151151
sloggers = { version = "2", features = ["json"] }
152-
smallvec = "1"
152+
smallvec = "1.11.2"
153153
snap = "1"
154154
ssz_types = "0.5"
155155
strum = { version = "0.24", features = ["derive"] }

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ cli:
210210
# `cargo`.
211211
cli-local:
212212
make && ./scripts/cli.sh
213-
213+
214214
# Runs the entire test suite, downloading test vectors if required.
215215
test-full: cargo-fmt test-release test-debug test-ef test-exec-engine
216216

beacon_node/http_api/src/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use beacon_processor::{BeaconProcessor, BeaconProcessorChannels, BeaconProcessor
77
use directory::DEFAULT_ROOT_DIR;
88
use eth2::{BeaconNodeHttpClient, Timeouts};
99
use lighthouse_network::{
10-
discv5::enr::{CombinedKey, EnrBuilder},
10+
discv5::enr::CombinedKey,
1111
libp2p::swarm::{
1212
behaviour::{ConnectionEstablished, FromSwarm},
1313
ConnectionId, NetworkBehaviour,
@@ -138,7 +138,7 @@ pub async fn create_api_server<T: BeaconChainTypes>(
138138
syncnets: EnrSyncCommitteeBitfield::<T::EthSpec>::default(),
139139
});
140140
let enr_key = CombinedKey::generate_secp256k1();
141-
let enr = EnrBuilder::new("v4").build(&enr_key).unwrap();
141+
let enr = Enr::builder().build(&enr_key).unwrap();
142142
let network_globals = Arc::new(NetworkGlobals::new(
143143
enr.clone(),
144144
meta_data,

beacon_node/lighthouse_network/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ prometheus-client = "0.22.0"
4343
unused_port = { workspace = true }
4444
delay_map = { workspace = true }
4545
void = "1"
46-
libp2p-mplex = "0.41.0"
46+
libp2p-mplex = { git = "https://github.com/sigp/rust-libp2p/", rev = "b96b90894faab0a1eed78e1c82c6452138a3538a" }
4747

4848
[dependencies.libp2p]
49-
version = "0.53"
49+
git = "https://github.com/sigp/rust-libp2p/"
50+
rev = "b96b90894faab0a1eed78e1c82c6452138a3538a"
5051
default-features = false
5152
features = ["identify", "yamux", "noise", "gossipsub", "dns", "tcp", "tokio", "plaintext", "secp256k1", "macros", "ecdsa", "metrics", "quic"]
5253

beacon_node/lighthouse_network/src/config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::{Enr, PeerIdSerialized};
55
use directory::{
66
DEFAULT_BEACON_NODE_DIR, DEFAULT_HARDCODED_NETWORK, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR,
77
};
8-
use discv5::{Discv5Config, Discv5ConfigBuilder};
98
use libp2p::gossipsub;
109
use libp2p::Multiaddr;
1110
use serde::{Deserialize, Serialize};
@@ -91,7 +90,7 @@ pub struct Config {
9190

9291
/// Discv5 configuration parameters.
9392
#[serde(skip)]
94-
pub discv5_config: Discv5Config,
93+
pub discv5_config: discv5::Config,
9594

9695
/// List of nodes to initially connect to.
9796
pub boot_nodes_enr: Vec<Enr>,
@@ -324,7 +323,7 @@ impl Default for Config {
324323
discv5::ListenConfig::from_ip(Ipv4Addr::UNSPECIFIED.into(), 9000);
325324

326325
// discv5 configuration
327-
let discv5_config = Discv5ConfigBuilder::new(discv5_listen_config)
326+
let discv5_config = discv5::ConfigBuilder::new(discv5_listen_config)
328327
.enable_packet_filter()
329328
.session_cache_capacity(5000)
330329
.request_timeout(Duration::from_secs(1))

beacon_node/lighthouse_network/src/discovery/enr.rs

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//! Helper functions and an extension trait for Ethereum 2 ENRs.
22
3-
pub use discv5::enr::{CombinedKey, EnrBuilder};
3+
pub use discv5::enr::CombinedKey;
44

55
use super::enr_ext::CombinedKeyExt;
66
use super::ENR_FILENAME;
77
use crate::types::{Enr, EnrAttestationBitfield, EnrSyncCommitteeBitfield};
88
use crate::NetworkConfig;
9-
use discv5::enr::EnrKey;
109
use libp2p::identity::Keypair;
1110
use slog::{debug, warn};
1211
use ssz::{Decode, Encode};
@@ -142,11 +141,13 @@ pub fn build_or_load_enr<T: EthSpec>(
142141
Ok(local_enr)
143142
}
144143

145-
pub fn create_enr_builder_from_config<T: EnrKey>(
144+
/// Builds a lighthouse ENR given a `NetworkConfig`.
145+
pub fn build_enr<T: EthSpec>(
146+
enr_key: &CombinedKey,
146147
config: &NetworkConfig,
147-
enable_libp2p: bool,
148-
) -> EnrBuilder<T> {
149-
let mut builder = EnrBuilder::new("v4");
148+
enr_fork_id: &EnrForkId,
149+
) -> Result<Enr, String> {
150+
let mut builder = discv5::enr::Enr::builder();
150151
let (maybe_ipv4_address, maybe_ipv6_address) = &config.enr_address;
151152

152153
if let Some(ip) = maybe_ipv4_address {
@@ -165,63 +166,51 @@ pub fn create_enr_builder_from_config<T: EnrKey>(
165166
builder.udp6(udp6_port.get());
166167
}
167168

168-
if enable_libp2p {
169-
// Add QUIC fields to the ENR.
170-
// Since QUIC is used as an alternative transport for the libp2p protocols,
171-
// the related fields should only be added when both QUIC and libp2p are enabled
172-
if !config.disable_quic_support {
173-
// If we are listening on ipv4, add the quic ipv4 port.
174-
if let Some(quic4_port) = config.enr_quic4_port.or_else(|| {
175-
config
176-
.listen_addrs()
177-
.v4()
178-
.and_then(|v4_addr| v4_addr.quic_port.try_into().ok())
179-
}) {
180-
builder.add_value(QUIC_ENR_KEY, &quic4_port.get());
181-
}
182-
183-
// If we are listening on ipv6, add the quic ipv6 port.
184-
if let Some(quic6_port) = config.enr_quic6_port.or_else(|| {
185-
config
186-
.listen_addrs()
187-
.v6()
188-
.and_then(|v6_addr| v6_addr.quic_port.try_into().ok())
189-
}) {
190-
builder.add_value(QUIC6_ENR_KEY, &quic6_port.get());
191-
}
192-
}
193-
194-
// If the ENR port is not set, and we are listening over that ip version, use the listening port instead.
195-
let tcp4_port = config.enr_tcp4_port.or_else(|| {
169+
// Add QUIC fields to the ENR.
170+
// Since QUIC is used as an alternative transport for the libp2p protocols,
171+
// the related fields should only be added when both QUIC and libp2p are enabled
172+
if !config.disable_quic_support {
173+
// If we are listening on ipv4, add the quic ipv4 port.
174+
if let Some(quic4_port) = config.enr_quic4_port.or_else(|| {
196175
config
197176
.listen_addrs()
198177
.v4()
199-
.and_then(|v4_addr| v4_addr.tcp_port.try_into().ok())
200-
});
201-
if let Some(tcp4_port) = tcp4_port {
202-
builder.tcp4(tcp4_port.get());
178+
.and_then(|v4_addr| v4_addr.quic_port.try_into().ok())
179+
}) {
180+
builder.add_value(QUIC_ENR_KEY, &quic4_port.get());
203181
}
204182

205-
let tcp6_port = config.enr_tcp6_port.or_else(|| {
183+
// If we are listening on ipv6, add the quic ipv6 port.
184+
if let Some(quic6_port) = config.enr_quic6_port.or_else(|| {
206185
config
207186
.listen_addrs()
208187
.v6()
209-
.and_then(|v6_addr| v6_addr.tcp_port.try_into().ok())
210-
});
211-
if let Some(tcp6_port) = tcp6_port {
212-
builder.tcp6(tcp6_port.get());
188+
.and_then(|v6_addr| v6_addr.quic_port.try_into().ok())
189+
}) {
190+
builder.add_value(QUIC6_ENR_KEY, &quic6_port.get());
213191
}
214192
}
215-
builder
216-
}
217193

218-
/// Builds a lighthouse ENR given a `NetworkConfig`.
219-
pub fn build_enr<T: EthSpec>(
220-
enr_key: &CombinedKey,
221-
config: &NetworkConfig,
222-
enr_fork_id: &EnrForkId,
223-
) -> Result<Enr, String> {
224-
let mut builder = create_enr_builder_from_config(config, true);
194+
// If the ENR port is not set, and we are listening over that ip version, use the listening port instead.
195+
let tcp4_port = config.enr_tcp4_port.or_else(|| {
196+
config
197+
.listen_addrs()
198+
.v4()
199+
.and_then(|v4_addr| v4_addr.tcp_port.try_into().ok())
200+
});
201+
if let Some(tcp4_port) = tcp4_port {
202+
builder.tcp4(tcp4_port.get());
203+
}
204+
205+
let tcp6_port = config.enr_tcp6_port.or_else(|| {
206+
config
207+
.listen_addrs()
208+
.v6()
209+
.and_then(|v6_addr| v6_addr.tcp_port.try_into().ok())
210+
});
211+
if let Some(tcp6_port) = tcp6_port {
212+
builder.tcp6(tcp6_port.get());
213+
}
225214

226215
// set the `eth2` field on our ENR
227216
builder.add_value(ETH2_ENR_KEY, &enr_fork_id.as_ssz_bytes());

beacon_node/lighthouse_network/src/discovery/enr_ext.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,7 @@ mod tests {
366366
let libp2p_kp: Keypair = secp256k1_kp.into();
367367
let peer_id = libp2p_kp.public().to_peer_id();
368368

369-
let enr = discv5::enr::EnrBuilder::new("v4")
370-
.build(&secret_key)
371-
.unwrap();
369+
let enr = discv5::enr::Enr::builder().build(&secret_key).unwrap();
372370
let node_id = peer_id_to_node_id(&peer_id).unwrap();
373371

374372
assert_eq!(enr.node_id(), node_id);
@@ -387,9 +385,7 @@ mod tests {
387385
let libp2p_kp: Keypair = secp256k1_kp.into();
388386
let peer_id = libp2p_kp.public().to_peer_id();
389387

390-
let enr = discv5::enr::EnrBuilder::new("v4")
391-
.build(&secret_key)
392-
.unwrap();
388+
let enr = discv5::enr::Enr::builder().build(&secret_key).unwrap();
393389
let node_id = peer_id_to_node_id(&peer_id).unwrap();
394390

395391
assert_eq!(enr.node_id(), node_id);

beacon_node/lighthouse_network/src/discovery/mod.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ pub mod enr_ext;
1010
use crate::service::TARGET_SUBNET_PEERS;
1111
use crate::{error, Enr, NetworkConfig, NetworkGlobals, Subnet, SubnetDiscovery};
1212
use crate::{metrics, ClearDialError};
13-
use discv5::{enr::NodeId, Discv5, Discv5Event};
14-
pub use enr::{
15-
build_enr, create_enr_builder_from_config, load_enr_from_disk, use_or_load_enr, CombinedKey,
16-
Eth2Enr,
17-
};
13+
use discv5::{enr::NodeId, Discv5};
14+
pub use enr::{build_enr, load_enr_from_disk, use_or_load_enr, CombinedKey, Eth2Enr};
1815
pub use enr_ext::{peer_id_to_node_id, CombinedKeyExt, EnrExt};
1916
pub use libp2p::identity::{Keypair, PublicKey};
2017

@@ -147,15 +144,10 @@ enum EventStream {
147144
/// Awaiting an event stream to be generated. This is required due to the poll nature of
148145
/// `Discovery`
149146
Awaiting(
150-
Pin<
151-
Box<
152-
dyn Future<Output = Result<mpsc::Receiver<Discv5Event>, discv5::Discv5Error>>
153-
+ Send,
154-
>,
155-
>,
147+
Pin<Box<dyn Future<Output = Result<mpsc::Receiver<discv5::Event>, discv5::Error>> + Send>>,
156148
),
157149
/// The future has completed.
158-
Present(mpsc::Receiver<Discv5Event>),
150+
Present(mpsc::Receiver<discv5::Event>),
159151
// The future has failed or discv5 has been disabled. There are no events from discv5.
160152
InActive,
161153
}
@@ -996,7 +988,7 @@ impl<TSpec: EthSpec> NetworkBehaviour for Discovery<TSpec> {
996988
match event {
997989
// We filter out unwanted discv5 events here and only propagate useful results to
998990
// the peer manager.
999-
Discv5Event::Discovered(_enr) => {
991+
discv5::Event::Discovered(_enr) => {
1000992
// Peers that get discovered during a query but are not contactable or
1001993
// don't match a predicate can end up here. For debugging purposes we
1002994
// log these to see if we are unnecessarily dropping discovered peers
@@ -1009,7 +1001,7 @@ impl<TSpec: EthSpec> NetworkBehaviour for Discovery<TSpec> {
10091001
}
10101002
*/
10111003
}
1012-
Discv5Event::SocketUpdated(socket_addr) => {
1004+
discv5::Event::SocketUpdated(socket_addr) => {
10131005
info!(self.log, "Address updated"; "ip" => %socket_addr.ip(), "udp_port" => %socket_addr.port());
10141006
metrics::inc_counter(&metrics::ADDRESS_UPDATE_COUNT);
10151007
metrics::check_nat();
@@ -1030,10 +1022,10 @@ impl<TSpec: EthSpec> NetworkBehaviour for Discovery<TSpec> {
10301022
// NOTE: We assume libp2p itself can keep track of IP changes and we do
10311023
// not inform it about IP changes found via discovery.
10321024
}
1033-
Discv5Event::EnrAdded { .. }
1034-
| Discv5Event::TalkRequest(_)
1035-
| Discv5Event::NodeInserted { .. }
1036-
| Discv5Event::SessionEstablished { .. } => {} // Ignore all other discv5 server events
1025+
discv5::Event::EnrAdded { .. }
1026+
| discv5::Event::TalkRequest(_)
1027+
| discv5::Event::NodeInserted { .. }
1028+
| discv5::Event::SessionEstablished { .. } => {} // Ignore all other discv5 server events
10371029
}
10381030
}
10391031
}
@@ -1144,7 +1136,6 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
11441136
mod tests {
11451137
use super::*;
11461138
use crate::rpc::methods::{MetaData, MetaDataV2};
1147-
use enr::EnrBuilder;
11481139
use libp2p::identity::secp256k1;
11491140
use slog::{o, Drain};
11501141
use types::{BitVector, MinimalEthSpec, SubnetId};
@@ -1227,7 +1218,7 @@ mod tests {
12271218
}
12281219

12291220
fn make_enr(subnet_ids: Vec<usize>) -> Enr {
1230-
let mut builder = EnrBuilder::new("v4");
1221+
let mut builder = Enr::builder();
12311222
let keypair = secp256k1::Keypair::generate();
12321223
let enr_key: CombinedKey = CombinedKey::from_secp256k1(&keypair);
12331224

0 commit comments

Comments
 (0)