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

feat(fevm): L3 testing #1084

Draft
wants to merge 68 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
3492657
db, warm addresses and cache scaffold
JereSalo Oct 26, 2024
13d7d47
ongoing changes on db
JereSalo Oct 28, 2024
5fd1345
Add `Database` api
juanimedone Oct 28, 2024
c0f8cf8
some changes in Database and Db
JereSalo Oct 28, 2024
2e9cf34
add method get_from_db_then_cache()
JereSalo Oct 28, 2024
1ddefdf
fix some tests
JereSalo Oct 28, 2024
b5eebd4
Fix environment opcodes
juanimedone Oct 28, 2024
99b4fc3
make changes to vm testing function
JereSalo Oct 28, 2024
6b381a8
Merge branch 'levm/refactor/db' of github.com:lambdaclass/lambda_ethe…
JereSalo Oct 28, 2024
594ef74
fix more tests
JereSalo Oct 28, 2024
cdd786d
fix some issues in vm
JereSalo Oct 28, 2024
0b8614b
Add `get_block_hash`
juanimedone Oct 28, 2024
5c380f1
fix more tests i guess..
JereSalo Oct 28, 2024
db12190
Merge branch 'levm/refactor/db' of github.com:lambdaclass/lambda_ethe…
JereSalo Oct 28, 2024
ffad514
'fix' errors in call opcode
JereSalo Oct 28, 2024
61d36c8
change to create
JereSalo Oct 28, 2024
eaf4616
Fix `SLOAD` and `SSTORE`
juanimedone Oct 28, 2024
f34b493
changes in caching from db
JereSalo Oct 28, 2024
1fa59e0
Merge branch 'levm/refactor/db' of github.com:lambdaclass/lambda_ethe…
JereSalo Oct 28, 2024
c3b21ff
Fix lint
juanimedone Oct 28, 2024
b2fe679
fix errors in tests
JereSalo Oct 28, 2024
b1c9be0
Merge branch 'levm/refactor/db' of github.com:lambdaclass/lambda_ethe…
JereSalo Oct 28, 2024
96e66cc
cache all data added to db in tests
JereSalo Oct 28, 2024
e66aa3e
fix one test...
JereSalo Oct 28, 2024
741a543
fix call tests
JereSalo Oct 28, 2024
91ab731
fix sstore
JereSalo Oct 28, 2024
7c7ada9
fix create tests
JereSalo Oct 28, 2024
842a530
Fix `extcodehash_account_with_empty_code`
juanimedone Oct 28, 2024
965bc4c
Fix compiling warning
juanimedone Oct 28, 2024
439c2ac
change variables names for clarity
JereSalo Oct 29, 2024
abd5789
fix call opcodes, remove delegate and change opcodes that used it
JereSalo Oct 29, 2024
96a83c9
comment delegatecall tests for being messy :)
JereSalo Oct 29, 2024
63d67bc
add comments to callframe attributes
JereSalo Oct 29, 2024
08cc426
run cargo fmt
JereSalo Oct 29, 2024
dd64d70
remove unused object
JereSalo Oct 29, 2024
b463d30
remove commented methods from Db implementation of Database trait
JereSalo Oct 29, 2024
5233c57
delete debug trait for database and vm, for integration to work
JereSalo Oct 29, 2024
e37a47e
change storage key from U256 to H256
JereSalo Oct 29, 2024
9d7e700
change block_number from U256 to u64
JereSalo Oct 29, 2024
c42f9f0
run cargo fmt
JereSalo Oct 29, 2024
18868f6
Merge branch 'main' into levm/refactor/db
juanimedone Oct 29, 2024
84330a6
Merge fixes
juanimedone Oct 30, 2024
97a6e11
Improve call/create transactions handling
maximopalopoli Oct 30, 2024
8a49f1f
Move validations in new to transact
maximopalopoli Oct 30, 2024
f0440fc
Delete commented code
maximopalopoli Oct 30, 2024
55ab9eb
starting changes, doesnt work
maximopalopoli Oct 30, 2024
3cee7e5
environment changes
JereSalo Oct 30, 2024
c04a00f
Fix tests
jrchatruc Oct 30, 2024
fedefff
Fix mutable variable
jrchatruc Oct 30, 2024
4b5f0fc
[WIP] Levm ethereum Rust integration
jrchatruc Oct 30, 2024
167a04e
fix tests and lints
JereSalo Oct 30, 2024
b85bff9
change name of package ethereum_rust_levm
JereSalo Oct 31, 2024
b5dae80
change storewrapper implementation of database methods
JereSalo Oct 31, 2024
5cc1adc
[WIP] More progress
jrchatruc Oct 31, 2024
a979da3
add gas consumed by sstore (without gas refunds)
JereSalo Oct 31, 2024
376248f
fix gas cost issue, now call to a contract works properly
JereSalo Oct 31, 2024
63550e1
fix sstore test
JereSalo Nov 1, 2024
bce322a
Fix clippy alerts and fmt
maximopalopoli Nov 1, 2024
557e57a
Change address obtaining to prevent overflow + fmt
maximopalopoli Nov 1, 2024
d73f18f
add some gas comnsumption comments
JereSalo Nov 4, 2024
5bd6be8
feat(levm): merge `main` into `levm-ethereum-rust-integration` (#1062)
ilitteri Nov 4, 2024
7993506
Merge branch 'main' of github.com:lambdaclass/lambda_ethereum_rust in…
ilitteri Nov 4, 2024
30d447d
fix(levm): fix generic_call (#1063)
JereSalo Nov 4, 2024
ee18468
Pops the succes value of the generic_call in create
maximopalopoli Nov 5, 2024
7e375e1
Add a debug for printing bytes
maximopalopoli Nov 5, 2024
ae31d27
effectively decreases sender's balance
maximopalopoli Nov 5, 2024
88eadc5
Add code of the acounts that creates the contracts
maximopalopoli Nov 6, 2024
098882c
cargo fmt
maximopalopoli Nov 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use ethereum_rust_core::H256;

use ethereum_rust_storage::error::StoreError;
use ethereum_rust_storage::Store;
use ethereum_rust_vm::{
evm_state, execute_block, get_state_transitions, spec_id, EvmState, SpecId,
};
use ethereum_rust_vm::{evm_state, execute_block, spec_id, EvmState, SpecId};

//TODO: Implement a struct Chain or BlockChain to encapsulate
//functionality and canonical chain state and config
Expand All @@ -41,11 +39,11 @@ pub fn add_block(block: &Block, storage: &Store) -> Result<(), ChainError> {
// Validate the block pre-execution
validate_block(block, &parent_header, &state)?;

let receipts = execute_block(block, &mut state)?;
let (receipts, account_updates) = execute_block(block, &mut state)?;

validate_gas_used(&receipts, &block.header)?;
dbg!(&account_updates);

let account_updates = get_state_transitions(&mut state);
validate_gas_used(&receipts, &block.header)?;

// Apply the account updates over the last block's state and compute the new state root
let new_state_root = state
Expand Down Expand Up @@ -166,6 +164,8 @@ pub fn validate_gas_used(
block_header: &BlockHeader,
) -> Result<(), ChainError> {
if let Some(last) = receipts.last() {
dbg!(last.cumulative_gas_used);
dbg!(block_header.gas_used);
if last.cumulative_gas_used != block_header.gas_used {
return Err(ChainError::InvalidBlock(InvalidBlockError::GasUsedMismatch));
}
Expand Down
1 change: 1 addition & 0 deletions crates/blockchain/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ fn apply_plain_transaction(

fn finalize_payload(context: &mut PayloadBuildContext) -> Result<(), StoreError> {
let account_updates = get_state_transitions(context.evm_state);
dbg!(&account_updates);
context.payload.header.state_root = context
.store()
.ok_or(StoreError::MissingStore)?
Expand Down
2 changes: 1 addition & 1 deletion crates/common/types/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ mod serde_impl {
pub gas: Option<u64>,
#[serde(default)]
pub value: U256,
#[serde(default, with = "crate::serde_utils::bytes", alias = "data")]
#[serde(default, with = "crate::serde_utils::bytes")]
pub input: Bytes,
#[serde(default, with = "crate::serde_utils::u64::hex_str")]
pub gas_price: u64,
Expand Down
1 change: 1 addition & 0 deletions crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
ethereum_rust-core = { path = "../common", default-features = false }
ethereum_rust-storage = { path = "../storage/store", default-features = false }
ethereum_rust_levm = { path = "./levm" }
revm = { version = "14.0.3", features = [
"serde",
"std",
Expand Down
38 changes: 37 additions & 1 deletion crates/vm/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ethereum_rust_core::{types::BlockHash, Address as CoreAddress, H256 as CoreH256};
use ethereum_rust_core::{types::BlockHash, Address as CoreAddress, H256 as CoreH256, U256};
use ethereum_rust_levm::db::Database as LevmDatabase;
use ethereum_rust_storage::{error::StoreError, Store};
use revm::primitives::{
AccountInfo as RevmAccountInfo, Address as RevmAddress, Bytecode as RevmBytecode,
Expand All @@ -10,6 +11,41 @@ pub struct StoreWrapper {
pub block_hash: BlockHash,
}

impl LevmDatabase for StoreWrapper {
fn get_account_info(&self, address: CoreAddress) -> ethereum_rust_levm::account::AccountInfo {
let acc_info = self
.store
.get_account_info_by_hash(self.block_hash, address)
.unwrap()
.unwrap_or_default();

let acc_code = self
.store
.get_account_code(acc_info.code_hash)
.unwrap()
.unwrap();

ethereum_rust_levm::account::AccountInfo {
balance: acc_info.balance,
nonce: acc_info.nonce,
bytecode: acc_code,
}
}

fn get_storage_slot(&self, address: CoreAddress, key: CoreH256) -> U256 {
self.store
.get_storage_at_hash(self.block_hash, address, key)
.unwrap()
.unwrap_or_default()
}

fn get_block_hash(&self, block_number: u64) -> Option<CoreH256> {
let a = self.store.get_block_header(block_number).unwrap();

a.map(|a| CoreH256::from(a.compute_block_hash().0))
}
}

impl revm::Database for StoreWrapper {
type Error = StoreError;

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/levm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "ethereum_rust-levm"
name = "ethereum_rust_levm"
version.workspace = true
edition.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/levm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ help: ## 📚 Show help for each of the Makefile recipes
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

test: ## 🧪 Runs all tests except Ethereum tests
cargo test -p ethereum_rust-levm
cargo test -p ethereum_rust_levm

lint: ## 🧹 Linter check
cargo clippy --all-targets --all-features -- -D warnings
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/levm/bench/revm_comparison/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "revm_comparison"
path = "src/lib.rs"

[dependencies]
ethereum_rust-levm = { path = "../../" }
ethereum_rust_levm = { path = "../../" }
hex = "0.4.3"
revm = "9.0.0"
bytes = "1.8.0"
Expand Down
6 changes: 1 addition & 5 deletions crates/vm/levm/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ impl Cache {
}

pub fn get_storage_slot(&self, address: Address, key: H256) -> Option<StorageSlot> {
self.get_account(address)
.expect("Account should have been cached")
.storage
.get(&key)
.cloned()
self.get_account(address)?.storage.get(&key).cloned()
}

pub fn add_account(&mut self, address: &Address, account: &Account) {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/levm/src/opcode_handlers/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl VM {
.stack
.push(U256::from(current_call_frame.bytecode.len()))?;

self.env.consumed_gas += gas_cost::CODESIZE;
self.increase_consumed_gas(current_call_frame, gas_cost::CODESIZE)?;

Ok(OpcodeSuccess::Continue)
}
Expand Down
3 changes: 1 addition & 2 deletions crates/vm/levm/src/opcode_handlers/push.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use ethereum_rust_core::U256;

use crate::{
call_frame::CallFrame,
constants::gas_cost,
errors::{OpcodeSuccess, VMError},
opcodes::Opcode,
vm::VM,
};
use ethereum_rust_core::U256;

// Push Operations
// Opcodes: PUSH0, PUSH1 ... PUSH32
Expand Down
33 changes: 24 additions & 9 deletions crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl VM {
}

// SSTORE operation
// TODO: add gas consumption
// TODO: add gas REFUNDS
pub fn op_sstore(
&mut self,
current_call_frame: &mut CallFrame,
Expand All @@ -165,21 +165,36 @@ impl VM {

let address = current_call_frame.to;

let original_value = if self.cache.is_slot_cached(&address, key) {
self.cache
.get_storage_slot(address, key)
.expect("Storage slot should have been cached")
.original_value
let mut base_dynamic_gas: U256 = U256::zero();

let storage_slot = if self.cache.is_slot_cached(&address, key) {
self.cache.get_storage_slot(address, key).unwrap()
} else {
self.cache_from_db(&address);
self.db.get_storage_slot(address, key)
// If slot is cold 2100 is added to base_dynamic_gas
base_dynamic_gas += U256::from(2100);

self.get_storage_slot(&address, key) // it is not in cache because of previous if
};

base_dynamic_gas += if value == storage_slot.current_value {
U256::from(100)
} else if storage_slot.current_value == storage_slot.original_value {
if storage_slot.original_value == U256::zero() {
U256::from(20000)
} else {
U256::from(2900)
}
} else {
U256::from(100)
};

self.increase_consumed_gas(current_call_frame, base_dynamic_gas)?;

self.cache.write_account_storage(
&address,
key,
StorageSlot {
original_value,
original_value: storage_slot.original_value,
current_value: value,
},
);
Expand Down
15 changes: 10 additions & 5 deletions crates/vm/levm/src/opcode_handlers/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
errors::{OpcodeSuccess, ResultReason, VMError},
vm::{word_to_address, VM},
};
use ethereum_rust_core::{types::TxKind, Address, U256};
use ethereum_rust_core::{types::TxKind, U256};

// System Operations (10)
// Opcodes: CREATE, CALL, CALLCODE, RETURN, DELEGATECALL, CREATE2, STATICCALL, REVERT, INVALID, SELFDESTRUCT
Expand All @@ -16,7 +16,7 @@ impl VM {
current_call_frame: &mut CallFrame,
) -> Result<OpcodeSuccess, VMError> {
let gas = current_call_frame.stack.pop()?;
let code_address = Address::from_low_u64_be(current_call_frame.stack.pop()?.low_u64());
let code_address = word_to_address(current_call_frame.stack.pop()?);
let value = current_call_frame.stack.pop()?;
let args_offset = current_call_frame
.stack
Expand Down Expand Up @@ -94,12 +94,13 @@ impl VM {
}

// CALLCODE operation
// TODO: Add gas consumption
pub fn op_callcode(
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeSuccess, VMError> {
let gas = current_call_frame.stack.pop()?;
let code_address = Address::from_low_u64_be(current_call_frame.stack.pop()?.low_u64());
let code_address = word_to_address(current_call_frame.stack.pop()?);
let value = current_call_frame.stack.pop()?;
let args_offset = current_call_frame.stack.pop()?.try_into().unwrap();
let args_size = current_call_frame.stack.pop()?.try_into().unwrap();
Expand Down Expand Up @@ -157,12 +158,13 @@ impl VM {
}

// DELEGATECALL operation
// TODO: Add gas consumption
pub fn op_delegatecall(
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeSuccess, VMError> {
let gas = current_call_frame.stack.pop()?;
let code_address = Address::from_low_u64_be(current_call_frame.stack.pop()?.low_u64());
let code_address = word_to_address(current_call_frame.stack.pop()?);
let args_offset = current_call_frame.stack.pop()?.try_into().unwrap();
let args_size = current_call_frame.stack.pop()?.try_into().unwrap();
let ret_offset = current_call_frame.stack.pop()?.try_into().unwrap();
Expand Down Expand Up @@ -190,12 +192,13 @@ impl VM {
}

// STATICCALL operation
// TODO: Add gas consumption
pub fn op_staticcall(
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeSuccess, VMError> {
let gas = current_call_frame.stack.pop()?;
let code_address = Address::from_low_u64_be(current_call_frame.stack.pop()?.low_u64());
let code_address = word_to_address(current_call_frame.stack.pop()?);
let args_offset = current_call_frame.stack.pop()?.try_into().unwrap();
let args_size = current_call_frame.stack.pop()?.try_into().unwrap();
let ret_offset = current_call_frame.stack.pop()?.try_into().unwrap();
Expand All @@ -222,6 +225,7 @@ impl VM {
}

// CREATE operation
// TODO: Add gas consumption
pub fn op_create(
&mut self,
current_call_frame: &mut CallFrame,
Expand All @@ -240,6 +244,7 @@ impl VM {
}

// CREATE2 operation
// TODO: Add gas consumption
pub fn op_create2(
&mut self,
current_call_frame: &mut CallFrame,
Expand Down
6 changes: 2 additions & 4 deletions crates/vm/levm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use bytes::Bytes;
use ethereum_rust_core::{types::TxKind, Address, U256};
use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};

pub fn ops_to_bytecode(operations: &[Operation]) -> Bytes {
operations
Expand Down Expand Up @@ -96,9 +96,7 @@ pub fn new_vm_with_ops_addr_bal_db(
env,
Default::default(),
Default::default(),
Box::new(db),
Arc::new(db),
cache,
Default::default(),
None,
)
}
Loading
Loading