Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions anchor/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ libp2p = { workspace = true, default-features = false, features = [
message_receiver = { workspace = true }
metrics = { workspace = true }
network_utils = { workspace = true }
parking_lot = { workspace = true }
peer-store = { package = "libp2p-peer-store", git = "https://github.com/libp2p/rust-libp2p.git", rev = "d63dab1" }
prometheus-client = { workspace = true }
quick-protobuf = "0.8.1"
Expand Down
24 changes: 11 additions & 13 deletions anchor/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use types::{ChainSpec, EthSpec};
use version::version_with_platform;

use crate::{
Config,
Config, SharedDomainType,
behaviour::BehaviourError::Gossipsub,
discovery::{Discovery, FIND_NODE_QUERY_CLOSEST_PEERS},
handshake,
Expand Down Expand Up @@ -92,6 +92,7 @@ impl AnchorBehaviour {
network_config: &Config,
metrics_registry: &mut Registry,
spec: &ChainSpec,
domain_type: SharedDomainType,
) -> Result<Self, BehaviourError> {
let identify = {
let local_public_key = local_keypair.public();
Expand Down Expand Up @@ -155,7 +156,8 @@ impl AnchorBehaviour {

let discovery = {
// Build and start the discovery sub-behaviour
let mut discovery = Discovery::new(local_keypair.clone(), network_config).await?;
let mut discovery =
Discovery::new(local_keypair.clone(), network_config, domain_type.clone()).await?;
// start searching for peers
discovery.discover_peers(FIND_NODE_QUERY_CLOSEST_PEERS);
discovery
Expand All @@ -169,17 +171,13 @@ impl AnchorBehaviour {
};

let handshake = {
let domain_type: String = network_config.domain_type.into();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see! so before, we were passing around copies of DomainType derived from initial config. now, the source of truth SharedDomainType is passed around and is updated at the fork epoch, which propagates to discovery and handshake behavior, nice!

let node_info = handshake::node_info::NodeInfo::new(
domain_type,
Some(handshake::node_info::NodeMetadata {
node_version: version_with_platform(),
execution_node: "geth/v1.10.8".to_string(),
consensus_node: "lighthouse/v1.5.0".to_string(),
subnets: "00000000000000000000000000000000".to_string(),
}),
);
handshake::Behaviour::new(local_keypair, node_info)
let metadata = handshake::node_info::NodeMetadata {
node_version: version_with_platform(),
execution_node: "geth/v1.10.8".to_string(),
consensus_node: "lighthouse/v1.5.0".to_string(),
subnets: "00000000000000000000000000000000".to_string(),
};
handshake::Behaviour::new(local_keypair, domain_type, metadata)
};

let upnp = Toggle::from(
Expand Down
34 changes: 11 additions & 23 deletions anchor/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use tracing::{debug, error, info, trace, warn};
use typenum::U128;

use crate::{
Config,
Config, SharedDomainType,
discovery::DiscoveryError::{Discv5Init, Discv5Start, EnrKey},
};

Expand Down Expand Up @@ -152,7 +152,7 @@ pub struct Discovery {
/// been started
update_ports: UpdatePorts,

domain_type: DomainType,
domain_type: SharedDomainType,

enr_file_path: PathBuf,
}
Expand All @@ -161,6 +161,7 @@ impl Discovery {
pub async fn new(
local_keypair: Keypair,
network_config: &Config,
domain_type: SharedDomainType,
) -> Result<Self, DiscoveryError> {
let protocol_identity = ProtocolIdentity {
protocol_id: *b"ssvdv5",
Expand Down Expand Up @@ -306,7 +307,7 @@ impl Discovery {
discv5,
event_stream,
started: !network_config.disable_discovery,
domain_type: network_config.domain_type,
domain_type,
update_ports,
enr_file_path,
})
Expand Down Expand Up @@ -412,24 +413,11 @@ impl Discovery {
Ok(true)
}

/// Update the domain type in both the local state and ENR.
/// Update the ENR domain type so other nodes can discover us with the new fork's domain.
///
/// Called when a fork activates to ensure this node can be discovered by
/// nodes using the new fork's domain type filter.
///
/// # Arguments
///
/// * `new_domain_type` - The domain type for the newly activated fork
///
/// # Returns
///
/// * `Ok(())` - Domain type was updated successfully
/// * `Err(String)` - Update failed with the given error message
pub fn update_domain_type(&mut self, new_domain_type: DomainType) -> Result<(), String> {
// Update local state used for query filtering
self.domain_type = new_domain_type;

// Update ENR so other nodes can discover us
/// Called when a fork activates. The shared domain type is updated separately;
/// this method only handles the ENR update and disk persistence.
pub fn update_enr_domain_type(&mut self, new_domain_type: DomainType) -> Result<(), String> {
self.discv5
.enr_insert("domaintype", &new_domain_type.0)
.map_err(|e| format!("Failed to update ENR domain type: {e:?}"))?;
Expand Down Expand Up @@ -459,12 +447,12 @@ impl Discovery {
// predicate for finding nodes with a valid tcp port
let tcp_predicate = move |enr: &Enr| enr.tcp4().is_some() || enr.tcp6().is_some();

// Capture a copy of the domain type so the closure no longer references `self`.
let local_domain_type = self.domain_type;
// Clone the shared domain type so the closure can read the current value at query time.
let shared_domain_type = self.domain_type.clone();

let domain_type_predicate = move |enr: &Enr| {
if let Some(Ok(domain_type)) = enr.get_decodable::<[u8; 4]>("domaintype") {
local_domain_type.0 == domain_type
shared_domain_type.get().0 == domain_type
} else {
trace!(?enr, "Rejecting ENR with missing domaintype");
false
Expand Down
Loading
Loading