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

refactor(l1): discovery service #1768

Merged
merged 38 commits into from
Jan 28, 2025
Merged

refactor(l1): discovery service #1768

merged 38 commits into from
Jan 28, 2025

Conversation

MarcosNicolau
Copy link
Member

@MarcosNicolau MarcosNicolau commented Jan 21, 2025

Motivation
Discovery service was becoming too big and unmanageable to be inside net.rs.

Description
This refactor splits the discovery service into its own structs and files, now the flow is:

  • Discv4 struct: handles the core logic of the discv4 protocol
    • Loading bootnodes
    • Listening for incoming connections and handling messages
    • Managing revalidations
    • Initiating lookups
  • Discv4LookupHandler: the complexity of the lookups is encapsulated in this struct.

The following improvements have also been made:

  • All Tokio tasks are now spawned using the tracker.
  • When receiving neighbors' messages, now we check we do not add ourselves to the table (which could occur occasionally).
  • Error handling has been improved, and the verification logic is now more readable, especially within message handling.

Closes None

MarcosNicolau added a commit that referenced this pull request Jan 22, 2025
allow many arguments is a temprarily fix, this will be soled in #1768
Copy link

github-actions bot commented Jan 23, 2025

| File                                                                     | Lines | Diff |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/cmd/ethrex/ethrex.rs                     | 351   | -3   |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/discv4/helpers.rs  | 23    | +23  |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/discv4/lookup.rs   | 306   | +306 |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/discv4/messages.rs | 903   | +903 |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/discv4/mod.rs      | 4     | +4   |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/discv4/server.rs   | 683   | +683 |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/kademlia.rs        | 468   | -22  |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/net.rs             | 160   | -956 |
+--------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/rlpx/connection.rs | 543   | -8   |
+--------------------------------------------------------------------------+-------+------+

Total lines added: +1919
Total lines removed: 989
Total lines changed: 2908

Comment on lines -146 to -154
let digest = Keccak256::digest(msg.get(65..).ok_or(RLPxError::InvalidMessageLength())?);
let signature = &Signature::from_bytes(
msg.get(..64)
.ok_or(RLPxError::InvalidMessageLength())?
.into(),
)?;
let rid = RecoveryId::from_byte(*msg.get(64).ok_or(RLPxError::InvalidMessageLength())?)
.ok_or(RLPxError::InvalidRecoveryId())?;
let peer_pk = VerifyingKey::recover_from_prehash(&digest, signature, rid)?;
Copy link
Member Author

Choose a reason for hiding this comment

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

I remove this because it is recovering the pubkey points bytes (node_id) from the msg, something we already do when decoding the packet (see https://github.com/lambdaclass/ethrex/blob/main/crates/networking/p2p/discv4.rs#L87-L97).

Copy link
Contributor

Choose a reason for hiding this comment

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

This is great, missed to comment this on previous review!

pub(crate) type RLPxConnBroadcastSender = broadcast::Sender<(tokio::task::Id, Arc<Message>)>;

// https://github.com/ethereum/go-ethereum/blob/master/p2p/peer.go#L44
pub const P2P_MAX_MESSAGE_SIZE: usize = 2048;
Copy link
Member Author

@MarcosNicolau MarcosNicolau Jan 23, 2025

Choose a reason for hiding this comment

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

The spec doesn't mention a max size but we were using the max size of the discovery protocol, and reading geth code they define 2048 as the max packet size in rlpx, see here.

Comment on lines -278 to +275
tracker.spawn(networking);
.await.expect("Network starts");
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that we don't need to spawn the networking as a task. This allows us to panic if there is an err during the network startup.

@MarcosNicolau MarcosNicolau marked this pull request as ready for review January 23, 2025 22:21
@MarcosNicolau MarcosNicolau requested a review from a team as a code owner January 23, 2025 22:21
Copy link
Contributor

@ElFantasma ElFantasma left a comment

Choose a reason for hiding this comment

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

Most comments are minor issues. And some may be wrong 😬
Good work overall!

Copy link
Contributor

@ElFantasma ElFantasma left a comment

Choose a reason for hiding this comment

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

Nice work!

@MarcosNicolau MarcosNicolau added this pull request to the merge queue Jan 28, 2025
Merged via the queue into main with commit 0e8cf77 Jan 28, 2025
19 checks passed
@MarcosNicolau MarcosNicolau deleted the refactor/discovery branch January 28, 2025 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants