-
Notifications
You must be signed in to change notification settings - Fork 154
feat(l1): enr fork id filtering #5500
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
535be82
enr forkid filtering
edg-l 8d4cadc
dont return invalid contact, simply filter
edg-l 4ecb240
fix
edg-l 26ae9b0
Merge remote-tracking branch 'origin/main' into fork_filtering
edg-l 96a9dab
better logging
edg-l f532d69
refactor
edg-l f41211e
Merge branch 'main' into fork_filtering
edg-l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,8 @@ use crate::{ | |
| }, | ||
| }; | ||
| use bytes::BytesMut; | ||
| use ethrex_common::{H256, H512}; | ||
| use ethrex_storage::Store; | ||
| use ethrex_common::{H256, H512, types::ForkId}; | ||
| use ethrex_storage::{Store, error::StoreError}; | ||
| use futures::StreamExt; | ||
| use rand::rngs::OsRng; | ||
| use secp256k1::SecretKey; | ||
|
|
@@ -59,6 +59,8 @@ pub enum DiscoveryServerError { | |
| InvalidContact, | ||
| #[error(transparent)] | ||
| PeerTable(#[from] PeerTableError), | ||
| #[error(transparent)] | ||
| Store(#[from] StoreError), | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
|
|
@@ -83,6 +85,7 @@ pub struct DiscoveryServer { | |
| local_node_record: NodeRecord, | ||
| signer: SecretKey, | ||
| udp_socket: Arc<UdpSocket>, | ||
| store: Store, | ||
| peer_table: PeerTable, | ||
| /// The last `FindNode` message sent, cached due to message | ||
| /// signatures being expensive. | ||
|
|
@@ -113,6 +116,7 @@ impl DiscoveryServer { | |
| local_node_record, | ||
| signer, | ||
| udp_socket, | ||
| store: storage.clone(), | ||
| peer_table: peer_table.clone(), | ||
| find_node_message: Self::random_message(&signer), | ||
| }; | ||
|
|
@@ -342,7 +346,7 @@ impl DiscoveryServer { | |
| .expect("first 32 bytes are the message hash"); | ||
| // We do not use self.send() here, as we already encoded the message to calculate hash. | ||
| self.udp_socket.send_to(&buf, node.udp_addr()).await?; | ||
| debug!(sent = "Ping", to = %format!("{:#x}", node.public_key)); | ||
| trace!(sent = "Ping", to = %format!("{:#x}", node.public_key)); | ||
| Ok(H256::from(ping_hash)) | ||
| } | ||
|
|
||
|
|
@@ -362,7 +366,7 @@ impl DiscoveryServer { | |
|
|
||
| self.send(pong, node.udp_addr()).await?; | ||
|
|
||
| debug!(sent = "Pong", to = %format!("{:#x}", node.public_key)); | ||
| trace!(sent = "Pong", to = %format!("{:#x}", node.public_key)); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
@@ -501,10 +505,68 @@ impl DiscoveryServer { | |
| .record_enr_response_received( | ||
| &node_id, | ||
| enr_response_message.request_hash, | ||
| enr_response_message.node_record, | ||
| enr_response_message.node_record.clone(), | ||
| ) | ||
| .await?; | ||
|
|
||
| self.validate_enr_fork_id(node_id, sender_public_key, enr_response_message.node_record) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Validates the fork id of the given ENR is valid, saving it to the peer_table. | ||
| async fn validate_enr_fork_id( | ||
| &mut self, | ||
| node_id: H256, | ||
| sender_public_key: H512, | ||
| node_record: NodeRecord, | ||
| ) -> Result<(), DiscoveryServerError> { | ||
| let pairs = node_record.decode_pairs(); | ||
|
|
||
| let Some(remote_fork_id) = pairs.eth else { | ||
| self.peer_table | ||
| .set_is_fork_id_valid(&node_id, false) | ||
| .await?; | ||
| debug!(received = "ENRResponse", from = %format!("{sender_public_key:#x}"), "missing fork id in ENR response, skipping"); | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let chain_config = self.store.get_chain_config(); | ||
| let genesis_header = self | ||
| .store | ||
| .get_block_header(0)? | ||
| .ok_or(DiscoveryServerError::InvalidContact)?; | ||
| let latest_block_number = self.store.get_latest_block_number().await?; | ||
| let latest_block_header = self | ||
| .store | ||
| .get_block_header(latest_block_number)? | ||
| .ok_or(DiscoveryServerError::InvalidContact)?; | ||
|
|
||
| let local_fork_id = ForkId::new( | ||
| chain_config, | ||
| genesis_header.clone(), | ||
| latest_block_header.timestamp, | ||
| latest_block_number, | ||
| ); | ||
|
Comment on lines
+546
to
+551
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably cache this: #3660 (in another PR) |
||
|
|
||
| if !local_fork_id.is_valid( | ||
| remote_fork_id.clone(), | ||
| latest_block_number, | ||
| latest_block_header.timestamp, | ||
| chain_config, | ||
| genesis_header, | ||
| ) { | ||
| self.peer_table | ||
| .set_is_fork_id_valid(&node_id, false) | ||
| .await?; | ||
| debug!(received = "ENRResponse", from = %format!("{sender_public_key:#x}"), local_fork_id=%local_fork_id, remote_fork_id=%remote_fork_id, "fork id mismatch in ENR response, skipping"); | ||
| return Ok(()); | ||
| } | ||
|
|
||
| debug!(received = "ENRResponse", from = %format!("{sender_public_key:#x}"), local_fork_id=%local_fork_id, remote_fork_id=%remote_fork_id, "valid fork id in ENR found"); | ||
| self.peer_table.set_is_fork_id_valid(&node_id, true).await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.