mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 13:24:44 +00:00
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 <Age@AgeManning.com> Co-authored-by: Pawan Dhananjay <pawandhananjay@gmail.com> Co-authored-by: antondlr <anton@delaruelle.net> Co-authored-by: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
//! Helper functions and an extension trait for Ethereum 2 ENRs.
|
||||
|
||||
pub use discv5::enr::{CombinedKey, EnrBuilder};
|
||||
pub use discv5::enr::CombinedKey;
|
||||
|
||||
use super::enr_ext::CombinedKeyExt;
|
||||
use super::ENR_FILENAME;
|
||||
use crate::types::{Enr, EnrAttestationBitfield, EnrSyncCommitteeBitfield};
|
||||
use crate::NetworkConfig;
|
||||
use discv5::enr::EnrKey;
|
||||
use libp2p::identity::Keypair;
|
||||
use slog::{debug, warn};
|
||||
use ssz::{Decode, Encode};
|
||||
@@ -142,11 +141,13 @@ pub fn build_or_load_enr<T: EthSpec>(
|
||||
Ok(local_enr)
|
||||
}
|
||||
|
||||
pub fn create_enr_builder_from_config<T: EnrKey>(
|
||||
/// Builds a lighthouse ENR given a `NetworkConfig`.
|
||||
pub fn build_enr<T: EthSpec>(
|
||||
enr_key: &CombinedKey,
|
||||
config: &NetworkConfig,
|
||||
enable_libp2p: bool,
|
||||
) -> EnrBuilder<T> {
|
||||
let mut builder = EnrBuilder::new("v4");
|
||||
enr_fork_id: &EnrForkId,
|
||||
) -> Result<Enr, String> {
|
||||
let mut builder = discv5::enr::Enr::builder();
|
||||
let (maybe_ipv4_address, maybe_ipv6_address) = &config.enr_address;
|
||||
|
||||
if let Some(ip) = maybe_ipv4_address {
|
||||
@@ -165,63 +166,51 @@ pub fn create_enr_builder_from_config<T: EnrKey>(
|
||||
builder.udp6(udp6_port.get());
|
||||
}
|
||||
|
||||
if enable_libp2p {
|
||||
// Add QUIC fields to the ENR.
|
||||
// Since QUIC is used as an alternative transport for the libp2p protocols,
|
||||
// the related fields should only be added when both QUIC and libp2p are enabled
|
||||
if !config.disable_quic_support {
|
||||
// If we are listening on ipv4, add the quic ipv4 port.
|
||||
if let Some(quic4_port) = config.enr_quic4_port.or_else(|| {
|
||||
config
|
||||
.listen_addrs()
|
||||
.v4()
|
||||
.and_then(|v4_addr| v4_addr.quic_port.try_into().ok())
|
||||
}) {
|
||||
builder.add_value(QUIC_ENR_KEY, &quic4_port.get());
|
||||
}
|
||||
|
||||
// If we are listening on ipv6, add the quic ipv6 port.
|
||||
if let Some(quic6_port) = config.enr_quic6_port.or_else(|| {
|
||||
config
|
||||
.listen_addrs()
|
||||
.v6()
|
||||
.and_then(|v6_addr| v6_addr.quic_port.try_into().ok())
|
||||
}) {
|
||||
builder.add_value(QUIC6_ENR_KEY, &quic6_port.get());
|
||||
}
|
||||
}
|
||||
|
||||
// If the ENR port is not set, and we are listening over that ip version, use the listening port instead.
|
||||
let tcp4_port = config.enr_tcp4_port.or_else(|| {
|
||||
// Add QUIC fields to the ENR.
|
||||
// Since QUIC is used as an alternative transport for the libp2p protocols,
|
||||
// the related fields should only be added when both QUIC and libp2p are enabled
|
||||
if !config.disable_quic_support {
|
||||
// If we are listening on ipv4, add the quic ipv4 port.
|
||||
if let Some(quic4_port) = config.enr_quic4_port.or_else(|| {
|
||||
config
|
||||
.listen_addrs()
|
||||
.v4()
|
||||
.and_then(|v4_addr| v4_addr.tcp_port.try_into().ok())
|
||||
});
|
||||
if let Some(tcp4_port) = tcp4_port {
|
||||
builder.tcp4(tcp4_port.get());
|
||||
.and_then(|v4_addr| v4_addr.quic_port.try_into().ok())
|
||||
}) {
|
||||
builder.add_value(QUIC_ENR_KEY, &quic4_port.get());
|
||||
}
|
||||
|
||||
let tcp6_port = config.enr_tcp6_port.or_else(|| {
|
||||
// If we are listening on ipv6, add the quic ipv6 port.
|
||||
if let Some(quic6_port) = config.enr_quic6_port.or_else(|| {
|
||||
config
|
||||
.listen_addrs()
|
||||
.v6()
|
||||
.and_then(|v6_addr| v6_addr.tcp_port.try_into().ok())
|
||||
});
|
||||
if let Some(tcp6_port) = tcp6_port {
|
||||
builder.tcp6(tcp6_port.get());
|
||||
.and_then(|v6_addr| v6_addr.quic_port.try_into().ok())
|
||||
}) {
|
||||
builder.add_value(QUIC6_ENR_KEY, &quic6_port.get());
|
||||
}
|
||||
}
|
||||
builder
|
||||
}
|
||||
|
||||
/// Builds a lighthouse ENR given a `NetworkConfig`.
|
||||
pub fn build_enr<T: EthSpec>(
|
||||
enr_key: &CombinedKey,
|
||||
config: &NetworkConfig,
|
||||
enr_fork_id: &EnrForkId,
|
||||
) -> Result<Enr, String> {
|
||||
let mut builder = create_enr_builder_from_config(config, true);
|
||||
// If the ENR port is not set, and we are listening over that ip version, use the listening port instead.
|
||||
let tcp4_port = config.enr_tcp4_port.or_else(|| {
|
||||
config
|
||||
.listen_addrs()
|
||||
.v4()
|
||||
.and_then(|v4_addr| v4_addr.tcp_port.try_into().ok())
|
||||
});
|
||||
if let Some(tcp4_port) = tcp4_port {
|
||||
builder.tcp4(tcp4_port.get());
|
||||
}
|
||||
|
||||
let tcp6_port = config.enr_tcp6_port.or_else(|| {
|
||||
config
|
||||
.listen_addrs()
|
||||
.v6()
|
||||
.and_then(|v6_addr| v6_addr.tcp_port.try_into().ok())
|
||||
});
|
||||
if let Some(tcp6_port) = tcp6_port {
|
||||
builder.tcp6(tcp6_port.get());
|
||||
}
|
||||
|
||||
// set the `eth2` field on our ENR
|
||||
builder.add_value(ETH2_ENR_KEY, &enr_fork_id.as_ssz_bytes());
|
||||
|
||||
@@ -366,9 +366,7 @@ mod tests {
|
||||
let libp2p_kp: Keypair = secp256k1_kp.into();
|
||||
let peer_id = libp2p_kp.public().to_peer_id();
|
||||
|
||||
let enr = discv5::enr::EnrBuilder::new("v4")
|
||||
.build(&secret_key)
|
||||
.unwrap();
|
||||
let enr = discv5::enr::Enr::builder().build(&secret_key).unwrap();
|
||||
let node_id = peer_id_to_node_id(&peer_id).unwrap();
|
||||
|
||||
assert_eq!(enr.node_id(), node_id);
|
||||
@@ -387,9 +385,7 @@ mod tests {
|
||||
let libp2p_kp: Keypair = secp256k1_kp.into();
|
||||
let peer_id = libp2p_kp.public().to_peer_id();
|
||||
|
||||
let enr = discv5::enr::EnrBuilder::new("v4")
|
||||
.build(&secret_key)
|
||||
.unwrap();
|
||||
let enr = discv5::enr::Enr::builder().build(&secret_key).unwrap();
|
||||
let node_id = peer_id_to_node_id(&peer_id).unwrap();
|
||||
|
||||
assert_eq!(enr.node_id(), node_id);
|
||||
|
||||
@@ -10,11 +10,8 @@ pub mod enr_ext;
|
||||
use crate::service::TARGET_SUBNET_PEERS;
|
||||
use crate::{error, Enr, NetworkConfig, NetworkGlobals, Subnet, SubnetDiscovery};
|
||||
use crate::{metrics, ClearDialError};
|
||||
use discv5::{enr::NodeId, Discv5, Discv5Event};
|
||||
pub use enr::{
|
||||
build_enr, create_enr_builder_from_config, load_enr_from_disk, use_or_load_enr, CombinedKey,
|
||||
Eth2Enr,
|
||||
};
|
||||
use discv5::{enr::NodeId, Discv5};
|
||||
pub use enr::{build_enr, load_enr_from_disk, use_or_load_enr, CombinedKey, Eth2Enr};
|
||||
pub use enr_ext::{peer_id_to_node_id, CombinedKeyExt, EnrExt};
|
||||
pub use libp2p::identity::{Keypair, PublicKey};
|
||||
|
||||
@@ -147,15 +144,10 @@ enum EventStream {
|
||||
/// Awaiting an event stream to be generated. This is required due to the poll nature of
|
||||
/// `Discovery`
|
||||
Awaiting(
|
||||
Pin<
|
||||
Box<
|
||||
dyn Future<Output = Result<mpsc::Receiver<Discv5Event>, discv5::Discv5Error>>
|
||||
+ Send,
|
||||
>,
|
||||
>,
|
||||
Pin<Box<dyn Future<Output = Result<mpsc::Receiver<discv5::Event>, discv5::Error>> + Send>>,
|
||||
),
|
||||
/// The future has completed.
|
||||
Present(mpsc::Receiver<Discv5Event>),
|
||||
Present(mpsc::Receiver<discv5::Event>),
|
||||
// The future has failed or discv5 has been disabled. There are no events from discv5.
|
||||
InActive,
|
||||
}
|
||||
@@ -996,7 +988,7 @@ impl<TSpec: EthSpec> NetworkBehaviour for Discovery<TSpec> {
|
||||
match event {
|
||||
// We filter out unwanted discv5 events here and only propagate useful results to
|
||||
// the peer manager.
|
||||
Discv5Event::Discovered(_enr) => {
|
||||
discv5::Event::Discovered(_enr) => {
|
||||
// Peers that get discovered during a query but are not contactable or
|
||||
// don't match a predicate can end up here. For debugging purposes we
|
||||
// log these to see if we are unnecessarily dropping discovered peers
|
||||
@@ -1009,7 +1001,7 @@ impl<TSpec: EthSpec> NetworkBehaviour for Discovery<TSpec> {
|
||||
}
|
||||
*/
|
||||
}
|
||||
Discv5Event::SocketUpdated(socket_addr) => {
|
||||
discv5::Event::SocketUpdated(socket_addr) => {
|
||||
info!(self.log, "Address updated"; "ip" => %socket_addr.ip(), "udp_port" => %socket_addr.port());
|
||||
metrics::inc_counter(&metrics::ADDRESS_UPDATE_COUNT);
|
||||
metrics::check_nat();
|
||||
@@ -1030,10 +1022,10 @@ impl<TSpec: EthSpec> NetworkBehaviour for Discovery<TSpec> {
|
||||
// NOTE: We assume libp2p itself can keep track of IP changes and we do
|
||||
// not inform it about IP changes found via discovery.
|
||||
}
|
||||
Discv5Event::EnrAdded { .. }
|
||||
| Discv5Event::TalkRequest(_)
|
||||
| Discv5Event::NodeInserted { .. }
|
||||
| Discv5Event::SessionEstablished { .. } => {} // Ignore all other discv5 server events
|
||||
discv5::Event::EnrAdded { .. }
|
||||
| discv5::Event::TalkRequest(_)
|
||||
| discv5::Event::NodeInserted { .. }
|
||||
| discv5::Event::SessionEstablished { .. } => {} // Ignore all other discv5 server events
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1144,7 +1136,6 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::rpc::methods::{MetaData, MetaDataV2};
|
||||
use enr::EnrBuilder;
|
||||
use libp2p::identity::secp256k1;
|
||||
use slog::{o, Drain};
|
||||
use types::{BitVector, MinimalEthSpec, SubnetId};
|
||||
@@ -1227,7 +1218,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn make_enr(subnet_ids: Vec<usize>) -> Enr {
|
||||
let mut builder = EnrBuilder::new("v4");
|
||||
let mut builder = Enr::builder();
|
||||
let keypair = secp256k1::Keypair::generate();
|
||||
let enr_key: CombinedKey = CombinedKey::from_secp256k1(&keypair);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user