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): move mempool from Store to Blockchain #2089

Merged
merged 26 commits into from
Feb 27, 2025

Conversation

LeanSerra
Copy link
Contributor

@LeanSerra LeanSerra commented Feb 25, 2025

Motivation

The mempool is currently stored in the Store struct, it makes more sense to have it in the Blockchain struct

Description

  • Move all the functions related to mempool to a new struct called Mempool that implements all the functions related to the mempool.
  • To add new transactions to the mempool we need to do some validations, for this we need access to the store so some mempool functions are exposed by the blockchain struct
    • Blockchain::add_transaction_to_pool,
    • Blockchain::add_blob_transaction_to_pool
    • Blockchain::remove_transaction_from_pool

Closes #2072

@LeanSerra LeanSerra added tech debt Refactors, cleanups, etc L1 Ethereum client labels Feb 25, 2025
@LeanSerra LeanSerra self-assigned this Feb 25, 2025
@LeanSerra LeanSerra changed the title refactor(l1,l2): move mempool from Store to Blockchain refactor(l1): move mempool from Store to Blockchain Feb 25, 2025
Copy link

github-actions bot commented Feb 25, 2025

| File                                                                           | Lines | Diff |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/cmd/ethrex/ethrex.rs                           | 433   | +3   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/blockchain/blockchain.rs                | 370   | +90  |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/blockchain/mempool.rs                   | 571   | +66  |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/blockchain/payload.rs                   | 552   | -17  |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/l2/proposer/l1_watcher.rs               | 245   | +4   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/l2/proposer/mod.rs                      | 103   | +4   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/discv4/server.rs         | 680   | +3   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/network.rs               | 173   | +4   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/rlpx/connection.rs       | 505   | +4   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/rlpx/eth/transactions.rs | 306   | +9   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/rlpx/handshake.rs        | 424   | +2   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/rpc/engine/fork_choice.rs    | 359   | +2   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/rpc/eth/account.rs           | 221   | -1   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/rpc/eth/filter.rs            | 584   | +12  |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/rpc/eth/gas_price.rs         | 133   | +4   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/rpc/eth/max_priority_fee.rs  | 118   | +4   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/rpc/rpc.rs                   | 635   | +13  |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/rpc/utils.rs                 | 292   | +3   |
+--------------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/storage/store.rs                        | 1116  | -153 |
+--------------------------------------------------------------------------------+-------+------+

Total lines added: +227
Total lines removed: 171
Total lines changed: 398

Ok(hash)
}

/// Add a transaction to the mempool
pub fn add_transaction(transaction: Transaction, store: &Store) -> Result<H256, MempoolError> {
pub fn add_transaction(
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is the difference between this object and add_transaction_to_pool ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

seems like this should be a method of blockchain struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function add_transaction was calling add_transaction_to_pool at the end, but add_transaction also included transaction validations, for these validations you need to access the store, so I decided to leave a minimal interface with the mempool from the blockchain struct:

  • add_transaction_to_pool
  • add_blob_transaction_to_pool
  • remove_transaction_from_pool
  • validate_transaction -> this one needs access to the storage

Then the mempool struct implements functions to lock the mempool Mutex then insert the transaction/blob without doing any validity checks leaving a comment indicating this.

@@ -261,7 +261,7 @@ async fn main() {
peer_table.clone(),
sync_mode,
cancel_token.clone(),
blockchain,
blockchain.clone(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't we pass a blockchain reference instead of cloning? I would disable clone for a blockchain, since it's a singleton.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would mean adding an Arc and then cloning that Arc right? What we are doing right now is technically the same as the only field we are really cloning is the EVM both the store and mempool are internally arcs so when we clone them we are only cloning the references. I will still change it to Arc if thats what makes more sense.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok, leave it as is then.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This will be tackled in a separate PR, moving blockchain to an Arc

@jrchatruc jrchatruc marked this pull request as ready for review February 26, 2025 20:44
@jrchatruc jrchatruc requested a review from a team as a code owner February 26, 2025 20:44

*/

pub fn validate_transaction(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can be tackled in a follow up, but it seems this should call a function inside mempool

@@ -57,7 +57,7 @@ where
node,
stream,
codec,
context.storage,
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's pass both storage and blockchain since these are different entities and shouldn't be coupled together.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@chaja This should be for all places where the store was replaced by blockchain? Specially on the initial layers like rpc start on cmd/ethrex/ethrex.rs right? The idea is to distinguish the Blockchain having a reference to the store vs being the actual owner of the mempool at an arq level?

I'm a bit confused on when would be better to use the store separated from the blockchain struct and when to use the store reference inside the blockchain struct, as far as I understand most of this PR leaned to use the store ref inside blockchain replacing the previous isolated ones.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My comment is about being more future proof. A store might be divided into different stores, like p2p store, state store, history store, etc.. Then relying in the store from blockchain wont be the solution inside every rpc/p2p call.

Copy link
Collaborator

@mpaulucci mpaulucci left a comment

Choose a reason for hiding this comment

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

When you remove the remaining context.blockchain.storage from both rpc and p2p

@rodrigo-o
Copy link
Collaborator

When you remove the remaining context.blockchain.storage from both rpc and p2p

We can create an issue to make the storage in blockchain private following this PR to avoid this kind of ambiguity in the future.

@LeanSerra
Copy link
Contributor Author

When you remove the remaining context.blockchain.storage from both rpc and p2p

We can create an issue to make the storage in blockchain private following this PR to avoid this kind of ambiguity in the future.

Left an issue here #2105

Copy link
Collaborator

@rodrigo-o rodrigo-o left a comment

Choose a reason for hiding this comment

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

Great work, it's almost there, i have a couple of comments in line with previous request from @chaja, still reviewing the mempool and blockchain file, will finish ASAP

return Ok(None);
};
let result = match tx {
Transaction::LegacyTransaction(itx) => P2PTransaction::LegacyTransaction(itx),
Transaction::EIP2930Transaction(itx) => P2PTransaction::EIP2930Transaction(itx),
Transaction::EIP1559Transaction(itx) => P2PTransaction::EIP1559Transaction(itx),
Transaction::EIP4844Transaction(itx) => {
let Some(bundle) = store.get_blobs_bundle_from_pool(*hash)? else {
let Some(bundle) = mempool.get_blobs_bundle(*hash)? else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the future we'll need a consistent interface through blockchain/mempool, right now in the following changes we useblockchain.add_blob_transaction_to_pool or blockchain.add_transaction_to_pool instead of using internal mempool functions as done here. We might want to go one way or the other in a future PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, the only reason why I left some functions that access the mempool as members of Blockchain is because to add transactions we perform some validations that use the Store. That was done before me knowing that the Store and Blockchain should be completely separate entities.
Knowing that I think they should be removed from blockchain and be functions of mempool that receive a store like add_transaction_to_pool(self, tx, store)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I think both ways have their pros and cons but clearly something we could tackle in another PR, so we could go ahead with it for now and discuss the next step later!

Copy link
Collaborator

@rodrigo-o rodrigo-o left a comment

Choose a reason for hiding this comment

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

Great work @LeanSerra 🚀. Left some minor las comment (all pointing to the same thing)

@LeanSerra LeanSerra added this pull request to the merge queue Feb 27, 2025
Merged via the queue into main with commit 1c62317 Feb 27, 2025
24 checks passed
@LeanSerra LeanSerra deleted the l1/move_mempool_from_store branch February 27, 2025 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L1 Ethereum client tech debt Refactors, cleanups, etc
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Refactor: move mempool to blockchain struct
3 participants