Skip to content

Conversation

JereSalo
Copy link
Contributor

@JereSalo JereSalo commented Feb 21, 2025

Motivation

Description

  • Move methods like add_block to Blockchain struct.
  • I use Blockchain::default_with_store(store) in the case of tests. Default VM is REVM. The Store that I use for tests is the InMemory Engine Type.
  • Blockchain has attributes: VM and Store

Note: I don't know if I should test these changes in some other way rather than just running the tests that we have.

Closes #2018

@JereSalo JereSalo self-assigned this Feb 21, 2025
@JereSalo JereSalo changed the title refactor(l1): transform blockchain into singleton struct refactor(l1): transform crate blockchain into singleton struct Feb 21, 2025
Copy link

github-actions bot commented Feb 21, 2025

| File                                                                    | Lines | Diff |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/cmd/ef_tests/blockchain/test_runner.rs  | 138   | +1   |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/cmd/ethrex/ethrex.rs                    | 388   | -42  |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/cmd/ethrex_l2/src/commands/wallet.rs    | 617   | -1   |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/blockchain/blockchain.rs         | 280   | +68  |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/blockchain/payload.rs            | 569   | +8   |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/blockchain/smoke_test.rs         | 205   | +24  |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/l2/utils/prover/save_state.rs    | 438   | +1   |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/l2/utils/test_data_io.rs         | 89    | +1   |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/sync.rs           | 433   | +8   |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/rpc/engine/payload.rs | 567   | +16  |
+-------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/vm/vm.rs                         | 129   | -6   |
+-------------------------------------------------------------------------+-------+------+

Total lines added: +127
Total lines removed: 49
Total lines changed: 176

@JereSalo JereSalo changed the title refactor(l1): transform crate blockchain into singleton struct refactor(l1): transform crate blockchain into struct Feb 21, 2025

// Attempt to add the block as the head of the chain
let chain_result = add_block(block, &store);
let chain_result = Blockchain::default().add_block(block, &store);
Copy link
Collaborator

@mpaulucci mpaulucci Feb 24, 2025

Choose a reason for hiding this comment

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

The idea is that the blockchain will be stateful,

let blockchain = Blockchain:new(evm, store, ... etc);
for block in blocks {
blockchain.add_block(block)
}

Otherwise a blockchain struct wouldn't make much sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right! I haven't thought about that.


Ok(())
impl Blockchain {
pub fn new(evm: EVM) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we add store?

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 in a follow up.

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! I will try to implement it and see if I can make it work, so that we don't need to send store as parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pub use execution_result::*;
pub use revm::primitives::{Address as RevmAddress, SpecId, U256 as RevmU256};

use std::sync::OnceLock;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice!

finalize_payload(&mut context)?;
Ok((context.blobs_bundle, context.block_value))
}
impl Blockchain {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why did we need to move this methods to Blockchain in the first iteration?

Copy link
Contributor Author

@JereSalo JereSalo Feb 24, 2025

Choose a reason for hiding this comment

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

I moved them to Blockchain because these methods were using get_evm_backend_or_default() and I wanted them to use the Blockchain EVM that we previously set.

store.add_initial_state(genesis)?;
for block in chain {
add_block(&block, &store)?;
Blockchain::default().add_block(&block, &store)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

create blockchain above the adding of blocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although it has changed a little bit I fixed that here. The difference with the current code is that now we also have Store inside of Blockchain and we don't need to send it as parameter in some Blockchain methods.

let block: &CoreBlock = &block_fixture.block().unwrap().clone().into();
let hash = block.hash();

let blockchain = Blockchain::default();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Once you move store, you won't be able to use default. Maybe the default should be on the evm.

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, I was thinking on using something like default_with_store(store) so that we use the default VM and the provided store. Is there a better alternative to that?

block.header.number, hash, error
);
}
if store
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a TODO comment stating that the forkchoice update should not be part of this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

JereSalo and others added 6 commits February 24, 2025 13:03
**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

**Description**

<!-- A clear and concise general description of the changes this PR
introduces -->

<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes #issue_number
@JereSalo JereSalo marked this pull request as ready for review February 24, 2025 18:35
@JereSalo JereSalo requested a review from a team as a code owner February 24, 2025 18:35
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.

🎉

@ilitteri
Copy link
Contributor

L2 Integration Test will probably fail until #1705 is merged

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.

LGTM, just a small comment for something that we might need to check later.

Comment on lines +515 to +524
let add_block_result = {
let lock = context.syncer.try_lock();
if let Ok(syncer) = lock {
syncer.blockchain.add_block(block)
} else {
Err(ChainError::Custom(
"Error when trying to lock syncer".to_string(),
))
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if the new locks to the syncer might have some impact or if we want to maintain the state of the blockchain in it, but we can check this later if needed.

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. I created an issue just for not forgetting about it.
#2063

@JereSalo JereSalo enabled auto-merge February 24, 2025 21:36
@JereSalo JereSalo disabled auto-merge February 24, 2025 21:37
@JereSalo JereSalo enabled auto-merge February 24, 2025 21:45
@JereSalo JereSalo added this pull request to the merge queue Feb 24, 2025
Merged via the queue into main with commit 71b70b1 Feb 24, 2025
23 checks passed
@JereSalo JereSalo deleted the l1/refactor_blockchain_vms branch February 24, 2025 22:26
JereSalo added a commit that referenced this pull request Feb 28, 2025
**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

**Description**

<!-- A clear and concise general description of the changes this PR
introduces -->
- Move methods like `add_block` to `Blockchain` struct.
- I use `Blockchain::default_with_store(store)` in the case of tests.
Default VM is REVM. The Store that I use for tests is the `InMemory`
Engine Type.
- Blockchain has attributes: VM and Store

Note: I don't know if I should test these changes in some other way
rather than just running the tests that we have.
<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes #2018

---------

Co-authored-by: Ivan Litteri <[email protected]>
fkrause98 pushed a commit that referenced this pull request Mar 5, 2025
**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

**Description**

<!-- A clear and concise general description of the changes this PR
introduces -->
- Move methods like `add_block` to `Blockchain` struct.
- I use `Blockchain::default_with_store(store)` in the case of tests.
Default VM is REVM. The Store that I use for tests is the `InMemory`
Engine Type.
- Blockchain has attributes: VM and Store

Note: I don't know if I should test these changes in some other way
rather than just running the tests that we have.
<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes #2018

---------

Co-authored-by: Ivan Litteri <[email protected]>
fkrause98 pushed a commit that referenced this pull request Mar 5, 2025
**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

**Description**

<!-- A clear and concise general description of the changes this PR
introduces -->
- Move methods like `add_block` to `Blockchain` struct.
- I use `Blockchain::default_with_store(store)` in the case of tests.
Default VM is REVM. The Store that I use for tests is the `InMemory`
Engine Type.
- Blockchain has attributes: VM and Store

Note: I don't know if I should test these changes in some other way
rather than just running the tests that we have.
<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes #2018

---------

Co-authored-by: Ivan Litteri <[email protected]>
pedrobergamini pushed a commit to pedrobergamini/ethrex that referenced this pull request Aug 24, 2025
**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

**Description**

<!-- A clear and concise general description of the changes this PR
introduces -->
- Move methods like `add_block` to `Blockchain` struct.
- I use `Blockchain::default_with_store(store)` in the case of tests.
Default VM is REVM. The Store that I use for tests is the `InMemory`
Engine Type.
- Blockchain has attributes: VM and Store

Note: I don't know if I should test these changes in some other way
rather than just running the tests that we have.
<!-- Link to issues: Resolves lambdaclass#111, Resolves lambdaclass#222 -->

Closes lambdaclass#2018

---------

Co-authored-by: Ivan Litteri <[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.

Refactor: create blockchain struct that contains a vm.

4 participants