Skip to content

Conversation

ElFantasma
Copy link
Contributor

P2p connection processes are complex and error prone. Using spawned to clean it up and properly separate concurrency logic from business logic.

Description

Replaces the main_loop from RLPxConnection with a spawned process that handles all the messages from and to the remote peer, as well as the backend.

@ElFantasma ElFantasma marked this pull request as ready for review June 25, 2025 17:20
@ElFantasma ElFantasma requested a review from a team as a code owner June 25, 2025 17:20
Copy link

github-actions bot commented Jun 25, 2025

Lines of code report

Total lines added: 1450
Total lines removed: 67
Total lines changed: 1517

Detailed view
+-----------------------------------------------------------+-------+------+
| File                                                      | Lines | Diff |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/l2/based/block_fetcher.rs                   | 495   | -3   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/l2/based/state_updater.rs                   | 228   | -3   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/block_producer.rs              | 189   | -5   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/l1_committer.rs                | 553   | -5   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/l1_proof_sender.rs             | 314   | -5   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/l1_watcher.rs                  | 352   | +2   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/metrics.rs                     | 152   | +3   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/proof_coordinator.rs           | 521   | -2   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/discv4/server.rs             | 845   | -5   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/network.rs                   | 153   | -37  |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/peer_handler.rs              | 707   | +37  |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/rlpx.rs                      | 7     | -2   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/rlpx/connection/codec.rs     | 201   | +201 |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/rlpx/connection/handshake.rs | 490   | +490 |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/rlpx/connection/mod.rs       | 3     | +3   |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/rlpx/connection/server.rs    | 712   | +712 |
+-----------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/rlpx/error.rs                | 91    | +2   |
+-----------------------------------------------------------+-------+------+

pub(crate) init_message: Vec<u8>,
}

pub(crate) async fn perform(
Copy link
Contributor

Choose a reason for hiding this comment

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

I find the name "perform" a bit confusing, can we add some comments on what this function does?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see, "perform" as in "perform handshake", a little bit confusing without context still

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah... is to be used in the form handshake::perform but will think if there's a more expressive way

pub(crate) stream: Arc<TcpStream>,
}

#[derive(Clone)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this struct really need Clone? The original RLPxConnection didn't need to be cloned, also if we're clonning an 'established' connection somewhere, I'm guessing we're doing something wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, as it is part of the State of a GenServer. We may be cloning the state, but not the connection itself (which is a single spawned task)

Copy link
Collaborator

@Arkenan Arkenan left a comment

Choose a reason for hiding this comment

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

LGTM with a couple of questions

}

impl GenServer for RLPxConnection {
type CallMsg = Unused;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: If we have unused here, do we need to define the empty call message struct above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, good find. Don't know why rust forgot to complain about an unused struct.

@ElFantasma ElFantasma added this pull request to the merge queue Jul 3, 2025
Merged via the queue into main with commit c6ad97c Jul 3, 2025
34 checks passed
@ElFantasma ElFantasma deleted the spawned_p2p branch July 3, 2025 13:28
d-roak pushed a commit to 1sixtech/ethrex that referenced this pull request Jul 17, 2025
P2p connection processes are complex and error prone. Using `spawned` to
clean it up and properly separate concurrency logic from business logic.

**Description**

Replaces the main_loop from `RLPxConnection` with a `spawned` process
that handles all the messages from and to the remote peer, as well as
the backend.

---------

Co-authored-by: Lucas Fiegl <[email protected]>
Co-authored-by: Tomás Grüner <[email protected]>
Co-authored-by: Manuel Iñaki Bilbao <[email protected]>
Co-authored-by: Avila Gastón <[email protected]>
Co-authored-by: Ivan Litteri <[email protected]>
Co-authored-by: Jeremías Salomón <[email protected]>
Co-authored-by: Mario Rugiero <[email protected]>
Co-authored-by: MrAzteca <[email protected]>
Co-authored-by: Edgar <[email protected]>
Co-authored-by: LeanSerra <[email protected]>
pedrobergamini pushed a commit to pedrobergamini/ethrex that referenced this pull request Aug 24, 2025
P2p connection processes are complex and error prone. Using `spawned` to
clean it up and properly separate concurrency logic from business logic.

**Description**

Replaces the main_loop from `RLPxConnection` with a `spawned` process
that handles all the messages from and to the remote peer, as well as
the backend.

---------

Co-authored-by: Lucas Fiegl <[email protected]>
Co-authored-by: Tomás Grüner <[email protected]>
Co-authored-by: Manuel Iñaki Bilbao <[email protected]>
Co-authored-by: Avila Gastón <[email protected]>
Co-authored-by: Ivan Litteri <[email protected]>
Co-authored-by: Jeremías Salomón <[email protected]>
Co-authored-by: Mario Rugiero <[email protected]>
Co-authored-by: MrAzteca <[email protected]>
Co-authored-by: Edgar <[email protected]>
Co-authored-by: LeanSerra <[email protected]>
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.