Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(discv5): open dns for discv5 #7328

Merged
merged 18 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 17 additions & 8 deletions crates/net/dns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

pub use crate::resolver::{DnsResolver, MapResolver, Resolver};
Expand All @@ -22,12 +21,11 @@ use crate::{
pub use config::DnsDiscoveryConfig;
use enr::Enr;
use error::ParseDnsEntryError;
use reth_primitives::{ForkId, NodeRecord, PeerId};
use reth_primitives::{get_fork_id, ForkId, NodeRecord, NodeRecordParseError};
use schnellru::{ByLength, LruMap};
use secp256k1::SecretKey;
use std::{
collections::{hash_map::Entry, HashMap, HashSet, VecDeque},
net::IpAddr,
pin::Pin,
sync::Arc,
task::{ready, Context, Poll},
Expand Down Expand Up @@ -392,22 +390,33 @@ pub enum DnsDiscoveryEvent {

/// Converts an [Enr] into a [NodeRecord]
fn convert_enr_node_record(enr: Enr<SecretKey>) -> Option<DnsNodeRecordUpdate> {
use alloy_rlp::Decodable;

let node_record = match NodeRecord::try_from(&enr) {
Ok(node_record) => node_record,
Err(err) => {
trace!(target: "disc::dns",
%err,
"can't convert enr to node_record"
"can't convert enr to dns update"
);

return None
}
};

let mut maybe_fork_id = enr.get(b"eth")?;
let fork_id = ForkId::decode(&mut maybe_fork_id).ok();
let fork_id = match get_fork_id(&enr) {
Ok(fork_id) => Some(fork_id),
Err(err) => {
if matches!(err, NodeRecordParseError::EthForkIdMissing) {
trace!(target: "disc::dns",
%err,
"can't convert enr to dns update"
);

return None
}

None // enr fork could be badly configured in node record, peer could still be useful
}
emhane marked this conversation as resolved.
Show resolved Hide resolved
};

Some(DnsNodeRecordUpdate { node_record, fork_id, enr })
}
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ pub use header::{Header, HeaderValidationError, HeadersDirection, SealedHeader};
pub use integer_list::IntegerList;
pub use log::{logs_bloom, Log};
pub use net::{
goerli_nodes, holesky_nodes, mainnet_nodes, parse_nodes, pk_to_id, sepolia_nodes, NodeRecord,
NodeRecordParseError, GOERLI_BOOTNODES, HOLESKY_BOOTNODES, MAINNET_BOOTNODES,
get_fork_id, goerli_nodes, holesky_nodes, mainnet_nodes, parse_nodes, pk_to_id, sepolia_nodes,
NodeRecord, NodeRecordParseError, GOERLI_BOOTNODES, HOLESKY_BOOTNODES, MAINNET_BOOTNODES,
SEPOLIA_BOOTNODES,
};
pub use peer::{AnyNode, PeerId, WithPeerId};
Expand Down
18 changes: 18 additions & 0 deletions crates/primitives/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use alloy_rlp::Decodable;
pub use reth_rpc_types::{pk_to_id, NodeRecord, NodeRecordParseError};

emhane marked this conversation as resolved.
Show resolved Hide resolved
use enr::Enr;

emhane marked this conversation as resolved.
Show resolved Hide resolved
use crate::ForkId;

// <https://github.com/ledgerwatch/erigon/blob/610e648dc43ec8cd6563313e28f06f534a9091b3/params/bootnodes.go>

/// Ethereum Foundation Go Bootnodes
Expand Down Expand Up @@ -66,6 +71,19 @@ pub fn parse_nodes(nodes: impl IntoIterator<Item = impl AsRef<str>>) -> Vec<Node
nodes.into_iter().map(|s| s.as_ref().parse().unwrap()).collect()
}

/// Tries to read the [`ForkId`] from given [`Enr`].
pub fn get_fork_id(enr: &Enr<secp256k1::SecretKey>) -> Result<ForkId, NodeRecordParseError> {
let Some(mut maybe_fork_id) = enr.get(b"eth") else {
return Err(NodeRecordParseError::EthForkIdMissing)
};

let Ok(fork_id) = ForkId::decode(&mut maybe_fork_id) else {
return Err(NodeRecordParseError::ForkIdDecodeError(maybe_fork_id.to_vec()))
};

Ok(fork_id)
}

#[cfg(test)]
mod tests {
use std::{
Expand Down
6 changes: 6 additions & 0 deletions crates/rpc/rpc-types/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ pub enum NodeRecordParseError {
/// Conversion from type [`Enr<SecretKey>`] failed.
#[error("failed to convert enr into node record")]
ConversionFromEnrFailed,
emhane marked this conversation as resolved.
Show resolved Hide resolved
/// Missing key used to identify an execution layer enr on Ethereum network.
#[error("fork id missing on enr, 'eth' key missing")]
EthForkIdMissing,
/// Failed to decode fork ID rlp value.
#[error("failed to decode fork id, 'eth': {0:?}")]
ForkIdDecodeError(Vec<u8>),
emhane marked this conversation as resolved.
Show resolved Hide resolved
}

impl FromStr for NodeRecord {
Expand Down
Loading