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
15 changes: 4 additions & 11 deletions crates/networking/p2p/discovery/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use crate::{
server::{Discv5Message, Discv5State, update_local_ip},
},
peer_table::{DiscoveryProtocol, PeerTable, PeerTableServerProtocol as _},
types::{INITIAL_ENR_SEQ, Node, NodeRecord},
types::{Node, NodeRecord},
};
use bytes::BytesMut;
use ethrex_common::utils::keccak;
use ethrex_storage::Store;
use futures::StreamExt;
use secp256k1::SecretKey;
use spawned_concurrency::{
Expand Down Expand Up @@ -105,9 +104,11 @@ impl std::fmt::Debug for DiscoveryServer {

#[actor(protocol = DiscoveryServerProtocol)]
impl DiscoveryServer {
/// Starts the discovery actor, advertising `local_node_record` as this
/// node's ENR.
pub async fn spawn(
storage: Store,
local_node: Node,
local_node_record: NodeRecord,
Comment on lines 110 to +111

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

These should be deduped in the future

signer: SecretKey,
udp_socket: Arc<UdpSocket>,
peer_table: PeerTable,
Expand All @@ -116,14 +117,6 @@ impl DiscoveryServer {
) -> Result<(), DiscoveryServerError> {
debug!("Starting discovery server");

let mut local_node_record = NodeRecord::from_node(&local_node, INITIAL_ENR_SEQ, &signer)
.expect("Failed to create local node record");
if let Ok(fork_id) = storage.get_fork_id().await {
local_node_record
.set_fork_id(fork_id, &signer)
.expect("Failed to set fork_id on local node record");
}

let discv4 = if config.discv4_enabled {
info!(
protocol = "discv4",
Expand Down
26 changes: 24 additions & 2 deletions crates/networking/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
p2p::SUPPORTED_SNAP_CAPABILITIES,
},
tx_broadcaster::{TxBroadcaster, TxBroadcasterError},
types::{NetworkConfig, Node},
types::{INITIAL_ENR_SEQ, NetworkConfig, Node, NodeError, NodeRecord},
};
use ethrex_blockchain::Blockchain;
use ethrex_common::H256;
Expand Down Expand Up @@ -117,6 +117,26 @@ pub enum NetworkError {
TxBroadcasterError(#[from] TxBroadcasterError),
#[error("Failed to bind UDP socket: {0}")]
UdpSocketError(std::io::Error),
#[error("Failed to build the local node record: {0}")]
LocalNodeRecordError(#[from] NodeError),
}

/// The ENR this node publishes: whatever `local_node` advertises, plus our own
/// EIP-2124 fork id.
///
/// Built here rather than inside discovery, which has no say in what this node
/// announces about itself.
///
/// A chain that cannot supply a fork id leaves the entry out instead of failing
/// startup. The record is still usable for discovery, and a peer that requires
/// `eth` simply passes us over until we republish with it. A record we cannot
/// sign is different, and is reported.
async fn build_local_node_record(context: &P2PContext) -> Result<NodeRecord, NodeError> {
let mut record = NodeRecord::from_node(&context.local_node, INITIAL_ENR_SEQ, &context.signer)?;
if let Ok(fork_id) = context.storage.get_fork_id().await {
record.set_fork_id(fork_id, &context.signer)?;
}
Ok(record)
}

pub async fn start_network(
Expand All @@ -130,9 +150,11 @@ pub async fn start_network(
.map_err(NetworkError::UdpSocketError)?,
);

let local_node_record = build_local_node_record(&context).await?;

DiscoveryServer::spawn(
context.storage.clone(),
context.local_node.clone(),
local_node_record,
context.signer,
udp_socket,
context.table.clone(),
Expand Down
Loading