fix: use enrforkid in dns (#7900)

Co-authored-by: Emilia Hane <elsaemiliaevahane@gmail.com>
This commit is contained in:
Matthias Seitz
2024-04-26 11:24:26 +02:00
committed by GitHub
parent 57d09e84be
commit 6425064d07
6 changed files with 45 additions and 33 deletions

View File

@ -28,7 +28,6 @@ use crate::{
error::{DecodePacketError, Discv4Error},
proto::{FindNode, Message, Neighbours, Packet, Ping, Pong},
};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use discv5::{
kbucket,
kbucket::{
@ -2174,33 +2173,13 @@ pub enum DiscoveryUpdate {
Batch(Vec<DiscoveryUpdate>),
}
/// Represents a forward-compatible ENR entry for including the forkid in a node record via
/// EIP-868. Forward compatibility is achieved by allowing trailing fields.
///
/// See:
/// <https://github.com/ethereum/go-ethereum/blob/9244d5cd61f3ea5a7645fdf2a1a96d53421e412f/eth/protocols/eth/discovery.go#L27-L38>
///
/// for how geth implements ForkId values and forward compatibility.
#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[rlp(trailing)]
pub struct EnrForkIdEntry {
/// The inner forkid
pub fork_id: ForkId,
}
impl From<ForkId> for EnrForkIdEntry {
fn from(fork_id: ForkId) -> Self {
Self { fork_id }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{create_discv4, create_discv4_with_config, rng_endpoint, rng_record};
use alloy_rlp::{Decodable, Encodable};
use rand::{thread_rng, Rng};
use reth_primitives::{hex, mainnet_nodes, ForkHash};
use reth_primitives::{hex, mainnet_nodes, EnrForkIdEntry, ForkHash};
use std::future::poll_fn;
#[tokio::test]

View File

@ -1,11 +1,11 @@
//! Discovery v4 protocol implementation.
use crate::{error::DecodePacketError, EnrForkIdEntry, PeerId, MAX_PACKET_SIZE, MIN_PACKET_SIZE};
use crate::{error::DecodePacketError, PeerId, MAX_PACKET_SIZE, MIN_PACKET_SIZE};
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header, RlpDecodable, RlpEncodable};
use enr::Enr;
use reth_primitives::{
bytes::{Buf, BufMut, Bytes, BytesMut},
keccak256, pk2id, ForkId, NodeRecord, B256,
keccak256, pk2id, EnrForkIdEntry, ForkId, NodeRecord, B256,
};
use secp256k1::{
ecdsa::{RecoverableSignature, RecoveryId},
@ -261,7 +261,7 @@ impl EnrResponse {
/// See also <https://github.com/ethereum/go-ethereum/blob/9244d5cd61f3ea5a7645fdf2a1a96d53421e412f/eth/protocols/eth/discovery.go#L36>
pub fn eth_fork_id(&self) -> Option<ForkId> {
let mut maybe_fork_id = self.enr.get_raw_rlp(b"eth")?;
EnrForkIdEntry::decode(&mut maybe_fork_id).ok().map(|entry| entry.fork_id)
EnrForkIdEntry::decode(&mut maybe_fork_id).ok().map(Into::into)
}
}