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(levm): error handling ef_tests (Part 1) #1319

Merged
merged 32 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
49a1967
add errors
JereSalo Nov 27, 2024
8c0088a
add is_internal function to vmerror
JereSalo Nov 27, 2024
cfeee50
remove previous validate_transaction
JereSalo Nov 27, 2024
3167f2c
change execute, mainly for propagating internal errors
JereSalo Nov 27, 2024
74e4923
add validate_transaction (not working yet) and gas_price_or_max_fee_p…
JereSalo Nov 27, 2024
1e0449d
change return type of has_code in Account
JereSalo Nov 27, 2024
c62cfad
add intrinsic gas function
JereSalo Nov 27, 2024
f4566c9
add necessary constants
JereSalo Nov 27, 2024
d24ec11
add some fields to environment and think of gas_price as effective ga…
JereSalo Nov 27, 2024
6ac5c20
add unwraps to execute
JereSalo Nov 28, 2024
b01ec97
change blob hashes type and add other paremeters in vm.rs
JereSalo Nov 28, 2024
e5196ec
make small changes in type 3 validations
JereSalo Nov 28, 2024
99a1e21
fix behavior for op_blobhash
JereSalo Nov 28, 2024
88c50c3
Merge branch 'main' of github.com:lambdaclass/lambda_ethereum_rust in…
JereSalo Nov 28, 2024
40b6b8a
fix ef_tests environment for levm
JereSalo Nov 28, 2024
8d62d29
stop using effective gas price in levm
JereSalo Nov 28, 2024
85b7dd6
comment some stuff in validate transaction
JereSalo Nov 28, 2024
d94a98a
delete add_intrinsic_gas (out of scope), and change some things in va…
JereSalo Nov 28, 2024
2f71d26
last commit was wrong, this one is the real one :)
JereSalo Nov 28, 2024
d13cf3b
remove import gas price
JereSalo Nov 28, 2024
0f2f7a1
Merge branch 'main' into levm/ef-tests-error-handling
JereSalo Nov 28, 2024
68a3191
add unwrap() to execute in edge cases tests
JereSalo Nov 28, 2024
79b14d6
merge main into branch
JereSalo Nov 28, 2024
8e882bc
return error when not finding transaction levm_runner
JereSalo Nov 28, 2024
8981d83
remove storage constants that were not used
JereSalo Nov 28, 2024
c9b747f
remove comment from revert create
JereSalo Nov 28, 2024
fc1c17d
Merge branch 'main' into levm/ef-tests-error-handling
JereSalo Nov 28, 2024
ba57edf
fix clippy lint in execute
JereSalo Nov 28, 2024
793d6ad
Merge branch 'main' into levm/ef-tests-error-handling
JereSalo Nov 28, 2024
f80c900
merge
JereSalo Nov 28, 2024
7455a1d
change rust version levm workflow
JereSalo Nov 28, 2024
4897293
Merge branch 'main' into levm/ef-tests-error-handling
JereSalo Nov 28, 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
26 changes: 14 additions & 12 deletions cmd/ef_tests/levm/runner/levm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,33 @@ pub fn prepare_vm_for_tx(vector: &TestVector, test: &EFTest) -> Result<VM, EFTes
store: initial_state.database().unwrap().clone(),
block_hash,
});

let tx = test.transactions.get(vector).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Here instead of using .unwrap() you can return an EFTestRunnerError

Copy link
Contributor Author

Choose a reason for hiding this comment

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


VM::new(
test.transactions.get(vector).unwrap().to.clone(),
tx.to.clone(),
Environment {
origin: test.transactions.get(vector).unwrap().sender,
origin: tx.sender,
consumed_gas: U256::default(),
refunded_gas: U256::default(),
gas_limit: test.env.current_gas_limit, //this should be tx gas limit
gas_limit: tx.gas_limit,
block_number: test.env.current_number,
coinbase: test.env.current_coinbase,
timestamp: test.env.current_timestamp,
prev_randao: test.env.current_random,
chain_id: U256::from(1729),
base_fee_per_gas: test.env.current_base_fee.unwrap_or_default(),
gas_price: test
.transactions
.get(vector)
.unwrap()
.gas_price
.unwrap_or_default(), // or max_fee_per_gas?
gas_price: tx.gas_price.unwrap_or_default(),
block_excess_blob_gas: test.env.current_excess_blob_gas,
block_blob_gas_used: None,
tx_blob_hashes: None,
tx_blob_hashes: tx.blob_versioned_hashes.clone(),
tx_max_priority_fee_per_gas: tx.max_priority_fee_per_gas,
tx_max_fee_per_gas: tx.max_fee_per_gas,
tx_max_fee_per_blob_gas: tx.max_fee_per_blob_gas,
block_gas_limit: test.env.current_gas_limit,
},
test.transactions.get(vector).unwrap().value,
test.transactions.get(vector).unwrap().data.clone(),
tx.value,
tx.data.clone(),
db,
CacheDB::default(),
)
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/levm/bench/revm_comparison/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ pub fn run_with_levm(program: &str, runs: usize, number_of_iterations: u32) {
let mut vm = new_vm_with_bytecode(Bytes::new()).unwrap();
*vm.current_call_frame_mut().unwrap() = call_frame.clone();
let mut current_call_frame = vm.call_frames.pop().unwrap();
let tx_report = black_box(vm.execute(&mut current_call_frame));
let tx_report = black_box(vm.execute(&mut current_call_frame).unwrap());
assert!(tx_report.result == TxResult::Success);
}
let mut vm = new_vm_with_bytecode(Bytes::new()).unwrap();
*vm.current_call_frame_mut().unwrap() = call_frame.clone();
let mut current_call_frame = vm.call_frames.pop().unwrap();
let tx_report = black_box(vm.execute(&mut current_call_frame));
let tx_report = black_box(vm.execute(&mut current_call_frame).unwrap());
assert!(tx_report.result == TxResult::Success);

match tx_report.result {
Expand Down
7 changes: 7 additions & 0 deletions crates/vm/levm/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub const MEMORY_EXPANSION_QUOTIENT: usize = 512;
pub const TX_BASE_COST: U256 = U256([21000, 0, 0, 0]);

pub const MAX_CODE_SIZE: usize = 0x6000;
pub const INIT_CODE_MAX_SIZE: usize = 49152;
pub const MAX_CREATE_CODE_SIZE: usize = 2 * MAX_CODE_SIZE;

pub const INVALID_CONTRACT_PREFIX: u8 = 0xef;
Expand All @@ -44,6 +45,12 @@ pub const MAX_BLOB_NUMBER_PER_BLOCK: usize = 6;
pub const TARGET_BLOB_GAS_PER_BLOCK: U256 = U256([393216, 0, 0, 0]); // TARGET_BLOB_NUMBER_PER_BLOCK * GAS_PER_BLOB
pub const MIN_BASE_FEE_PER_BLOB_GAS: U256 = U256([1, 0, 0, 0]);
pub const BLOB_BASE_FEE_UPDATE_FRACTION: U256 = U256([3338477, 0, 0, 0]);
pub const MAX_BLOB_COUNT: usize = 6;
pub const VALID_BLOB_PREFIXES: [u8; 2] = [0x01, 0x02];
// Storage constants
pub const COLD_STORAGE_ACCESS_COST: U256 = U256([2100, 0, 0, 0]);
pub const WARM_ADDRESS_ACCESS_COST: U256 = U256([100, 0, 0, 0]);
pub const BALANCE_COLD_ADDRESS_ACCESS_COST: U256 = U256([2600, 0, 0, 0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

This cost constants should be in gas_cost.rs, since they are mainly used in that file

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 ended up removing them cause they were not used.
8981d83


// Block constants
pub const LAST_AVAILABLE_BLOCK_LIMIT: U256 = U256([256, 0, 0, 0]);
Expand Down
12 changes: 10 additions & 2 deletions crates/vm/levm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ pub struct Environment {
pub prev_randao: Option<H256>,
pub chain_id: U256,
pub base_fee_per_gas: U256,
pub gas_price: U256,
pub gas_price: U256, // Effective gas price
pub block_excess_blob_gas: Option<U256>,
pub block_blob_gas_used: Option<U256>,
pub tx_blob_hashes: Option<Vec<H256>>,
pub tx_blob_hashes: Vec<H256>,
pub tx_max_priority_fee_per_gas: Option<U256>,
pub tx_max_fee_per_gas: Option<U256>,
pub tx_max_fee_per_blob_gas: Option<U256>,
pub block_gas_limit: U256,
}

impl Environment {
Expand All @@ -38,6 +42,10 @@ impl Environment {
block_excess_blob_gas: Default::default(),
block_blob_gas_used: Default::default(),
tx_blob_hashes: Default::default(),
tx_max_priority_fee_per_gas: Default::default(),
tx_max_fee_per_gas: Default::default(),
tx_max_fee_per_blob_gas: Default::default(),
block_gas_limit: Default::default(),
}
}
}
42 changes: 42 additions & 0 deletions crates/vm/levm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,48 @@ pub enum VMError {
// Internal
#[error("Internal error: {0}")]
Internal(#[from] InternalError),
#[error("Transaction validation error: {0}")]
TxValidation(#[from] TxValidationError),
}

impl VMError {
pub fn is_internal(&self) -> bool {
matches!(self, VMError::Internal(_))
}
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, Serialize, Deserialize)]
pub enum TxValidationError {
#[error("Sender account should not have bytecode")]
SenderNotEOA,
#[error("Insufficient account founds")]
InsufficientAccountFunds,
#[error("Nonce is max (overflow)")]
NonceIsMax,
#[error("Initcode size exceeded")]
InitcodeSizeExceeded,
#[error("Priority fee greater than max fee per gas")]
PriorityGreaterThanMaxFeePerGas,
#[error("Intrinsic gas too low")]
IntrinsicGasTooLow,
#[error("Gas allowance exceeded")]
GasAllowanceExceeded,
#[error("Insufficient max fee per gas")]
InsufficientMaxFeePerGas,
#[error("Insufficient max fee per blob gas")]
InsufficientMaxFeePerBlobGas,
#[error("Type3TxZeroBlobs")]
Type3TxZeroBlobs,
#[error("Type3TxInvalidBlobVersionedHash")]
Type3TxInvalidBlobVersionedHash,
#[error("Type3TxBlobCountExceeded")]
Type3TxBlobCountExceeded,
#[error("Type3TxContractCreation")]
Type3TxContractCreation,
#[error("Undefined state")]
UndefinedState(i32), // This error is temporarily for things that cause an undefined state.
#[error("Gas limit price product overflow")]
GasLimitPriceProductOverflow,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error, Serialize, Deserialize)]
Expand Down
28 changes: 11 additions & 17 deletions crates/vm/levm/src/opcode_handlers/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use ethrex_core::{
types::{BLOB_BASE_FEE_UPDATE_FRACTION, MIN_BASE_FEE_PER_BLOB_GAS},
H256, U256,
U256,
};

// Block Information (11)
Expand Down Expand Up @@ -170,22 +170,16 @@ impl VM {
.try_into()
.map_err(|_err| VMError::VeryLargeNumber)?;

let blob_hash: H256 = match &self.env.tx_blob_hashes {
Some(vec) => match vec.get(index) {
Some(el) => *el,
None => {
return Err(VMError::BlobHashIndexOutOfBounds);
}
},
None => {
return Err(VMError::MissingBlobHashes);
}
};

// Could not find a better way to translate from H256 to U256
let u256_blob = U256::from(blob_hash.as_bytes());

current_call_frame.stack.push(u256_blob)?;
let blob_hashes = &self.env.tx_blob_hashes;

blob_hashes
.get(index)
.map(|el| {
current_call_frame
.stack
.push(U256::from_big_endian(el.as_bytes()))
})
.unwrap_or_else(|| current_call_frame.stack.push(U256::zero()))?;

Ok(OpcodeSuccess::Continue)
}
Expand Down
Loading
Loading