From f8a476fb03ad59400d4a88a8c5ea63bbcea0fcba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jerem=C3=ADas=20Salom=C3=B3n?= <48994069+JereSalo@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:33:41 -0300 Subject: [PATCH] fix(levm): remove consumed gas from environment (#1487) **Motivation** **Description** - We only want to keep track of the consumed gas in the current callframe. We don't need to keep track of it in the environment. - This change will allow us to implement accurate behavior related to gas consumption in XCALL and CREATE opcodes. Note that the gas consumed by the first callframe will indeed be the gas consumed by total execution, because the gas consume by child CallFrames will be added to the "father". (It is not as straightforward but that's the idea). This change doesn't fix nor break any test, but will be useful for following changes Closes #issue_number --- cmd/ef_tests/levm/runner/levm_runner.rs | 1 - crates/vm/levm/src/environment.rs | 2 - crates/vm/levm/src/vm.rs | 6 - crates/vm/levm/tests/tests.rs | 228 ++++++++++++------------ crates/vm/vm.rs | 1 - 5 files changed, 114 insertions(+), 124 deletions(-) diff --git a/cmd/ef_tests/levm/runner/levm_runner.rs b/cmd/ef_tests/levm/runner/levm_runner.rs index 7d45565c3..8036dbe00 100644 --- a/cmd/ef_tests/levm/runner/levm_runner.rs +++ b/cmd/ef_tests/levm/runner/levm_runner.rs @@ -94,7 +94,6 @@ pub fn prepare_vm_for_tx(vector: &TestVector, test: &EFTest) -> Result Self { Self { origin, - consumed_gas: U256::zero(), refunded_gas: U256::default(), gas_limit: U256::MAX, block_number: Default::default(), diff --git a/crates/vm/levm/src/vm.rs b/crates/vm/levm/src/vm.rs index 9acdc29e5..355d658aa 100644 --- a/crates/vm/levm/src/vm.rs +++ b/crates/vm/levm/src/vm.rs @@ -396,7 +396,6 @@ impl VM { .saturating_sub(current_call_frame.gas_used); current_call_frame.gas_used = current_call_frame.gas_used.saturating_add(left_gas); - self.env.consumed_gas = self.env.consumed_gas.saturating_add(left_gas); } self.restore_state(backup_db, backup_substate, backup_refunded_gas); @@ -1115,11 +1114,6 @@ impl VM { } current_call_frame.gas_used = potential_consumed_gas; - self.env.consumed_gas = self - .env - .consumed_gas - .checked_add(gas) - .ok_or(OutOfGasError::ConsumedGasOverflow)?; Ok(()) } diff --git a/crates/vm/levm/tests/tests.rs b/crates/vm/levm/tests/tests.rs index e397b2503..a9b132da2 100644 --- a/crates/vm/levm/tests/tests.rs +++ b/crates/vm/levm/tests/tests.rs @@ -436,7 +436,7 @@ fn and_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0b1000)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -455,7 +455,7 @@ fn and_binary_with_zero() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, expected_consumed_gas); + assert_eq!(current_call_frame.gas_used, expected_consumed_gas); } #[test] @@ -473,7 +473,7 @@ fn and_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xF0F0)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xF000))), @@ -488,7 +488,7 @@ fn and_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xF000)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xB020))), @@ -503,7 +503,7 @@ fn and_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0b1000000000000)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -521,7 +521,7 @@ fn or_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0b1110)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0b1010))), @@ -536,7 +536,7 @@ fn or_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0b1010)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(u64::MAX))), @@ -551,7 +551,7 @@ fn or_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xFFFFFFFFFFFFFFFF_u64)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -569,7 +569,7 @@ fn or_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xFFFF)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xF000))), @@ -584,7 +584,7 @@ fn or_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xF0F0)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xB020))), @@ -599,7 +599,7 @@ fn or_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0b1011111100101111)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -617,7 +617,7 @@ fn xor_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0b110)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0b1010))), @@ -632,7 +632,7 @@ fn xor_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0b1010)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(u64::MAX))), @@ -647,7 +647,7 @@ fn xor_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(u64::MAX)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(u64::MAX))), @@ -662,7 +662,7 @@ fn xor_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -680,7 +680,7 @@ fn xor_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xFF)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xFF))), @@ -695,7 +695,7 @@ fn xor_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xFFFF))), @@ -710,7 +710,7 @@ fn xor_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xF0F)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xF000))), @@ -725,7 +725,7 @@ fn xor_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xF0)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0x4C0F))), @@ -740,7 +740,7 @@ fn xor_with_hex_numbers() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0b111011001000100)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -758,7 +758,7 @@ fn not() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); let expected = !U256::from(0b1010); assert_eq!(result, expected); - assert_eq!(vm.env.consumed_gas, U256::from(6)); + assert_eq!(current_call_frame.gas_used, U256::from(6)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::MAX)), @@ -772,7 +772,7 @@ fn not() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(6)); + assert_eq!(current_call_frame.gas_used, U256::from(6)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::zero())), @@ -786,7 +786,7 @@ fn not() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::MAX); - assert_eq!(vm.env.consumed_gas, U256::from(6)); + assert_eq!(current_call_frame.gas_used, U256::from(6)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(1))), @@ -800,7 +800,7 @@ fn not() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::MAX - 1); - assert_eq!(vm.env.consumed_gas, U256::from(6)); + assert_eq!(current_call_frame.gas_used, U256::from(6)); } #[test] @@ -818,7 +818,7 @@ fn byte_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xF1)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0x33ED))), @@ -833,7 +833,7 @@ fn byte_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x33)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -851,7 +851,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xFF)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::MAX)), @@ -866,7 +866,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xFF)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0x00E0D0000))), @@ -881,7 +881,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x0D)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xFDEA179))), @@ -896,7 +896,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xFDEA179))), @@ -911,7 +911,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::zero())), @@ -926,7 +926,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let word = U256::from_big_endian(&[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x57, 0x08, 0x09, 0x90, 0x0B, 0x0C, 0x0D, 0x0E, @@ -947,7 +947,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x90)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, word)), @@ -962,7 +962,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x57)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, word)), @@ -977,7 +977,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xDD)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, word)), @@ -992,7 +992,7 @@ fn byte_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x40)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1010,7 +1010,7 @@ fn shl_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xDDDD)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0x12345678))), @@ -1025,7 +1025,7 @@ fn shl_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x2468acf0)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0x12345678))), @@ -1040,7 +1040,7 @@ fn shl_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(4886718336_u64)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xFF))), @@ -1055,7 +1055,7 @@ fn shl_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xFF << 4)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1073,7 +1073,7 @@ fn shl_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::zero())), @@ -1088,7 +1088,7 @@ fn shl_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::MAX)), @@ -1103,7 +1103,7 @@ fn shl_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::MAX - 1); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1121,7 +1121,7 @@ fn shr_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xDDDD)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0x12345678))), @@ -1136,7 +1136,7 @@ fn shr_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x91a2b3c)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0x12345678))), @@ -1151,7 +1151,7 @@ fn shr_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x1234567)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::from(0xFF))), @@ -1166,7 +1166,7 @@ fn shr_basic() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0xF)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1184,7 +1184,7 @@ fn shr_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::zero())), @@ -1199,7 +1199,7 @@ fn shr_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::zero()); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); let mut vm = new_vm_with_ops(&[ Operation::Push((32, U256::MAX)), @@ -1214,7 +1214,7 @@ fn shr_edge_cases() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::MAX >> 1); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1232,7 +1232,7 @@ fn sar_shift_by_0() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x12345678)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1261,7 +1261,7 @@ fn sar_shifting_large_value_with_all_bits_set() { 0xff, 0xff, ]); assert_eq!(result, expected); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1290,7 +1290,7 @@ fn sar_shifting_negative_value_and_small_shift() { 0x00, 0x00, ]); assert_eq!(result, expected); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1308,7 +1308,7 @@ fn sar_shift_positive_value() { let result = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(result, U256::from(0x07FFFF)); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1338,7 +1338,7 @@ fn sar_shift_negative_value() { ]); // change 0x8f to 0xf8 assert_eq!(result, expected); - assert_eq!(vm.env.consumed_gas, U256::from(9)); + assert_eq!(current_call_frame.gas_used, U256::from(9)); } #[test] @@ -1368,7 +1368,7 @@ fn keccak256_zero_offset_size_four() { U256::from("0x29045a592007d0c246ef02c2223570da9522d0cf0f73282c79a1bc8f0bb2c238") ); assert_eq!(vm.current_call_frame_mut().unwrap().pc(), 40); - assert_eq!(vm.env.consumed_gas, U256::from(52)); + assert_eq!(current_call_frame.gas_used, U256::from(52)); } #[test] @@ -1398,7 +1398,7 @@ fn keccak256_zero_offset_size_bigger_than_actual_memory() { == U256::from("0xae75624a7d0413029c1e0facdd38cc8e177d9225892e2490a69c2f1f89512061") ); assert_eq!(vm.current_call_frame_mut().unwrap().pc(), 40); - assert_eq!(vm.env.consumed_gas, U256::from(61)); + assert_eq!(current_call_frame.gas_used, U256::from(61)); } #[test] @@ -1420,7 +1420,7 @@ fn keccak256_zero_offset_zero_size() { U256::from("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") ); assert_eq!(vm.current_call_frame_mut().unwrap().pc(), 4); - assert_eq!(vm.env.consumed_gas, U256::from(34)); + assert_eq!(current_call_frame.gas_used, U256::from(34)); } #[test] @@ -1450,7 +1450,7 @@ fn keccak256_offset_four_size_four() { U256::from("0xe8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c") ); assert_eq!(vm.current_call_frame_mut().unwrap().pc(), 41); - assert_eq!(vm.env.consumed_gas, U256::from(53)); + assert_eq!(current_call_frame.gas_used, U256::from(53)); } #[test] @@ -1472,7 +1472,7 @@ fn mstore() { U256::from(32) ); assert_eq!(vm.current_call_frame_mut().unwrap().pc(), 69); - assert_eq!(vm.env.consumed_gas, U256::from(14)); + assert_eq!(current_call_frame.gas_used, U256::from(14)); } #[test] @@ -1499,7 +1499,7 @@ fn mstore_saves_correct_value() { let memory_size = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(memory_size, U256::from(32)); - assert_eq!(vm.env.consumed_gas, U256::from(14)); + assert_eq!(current_call_frame.gas_used, U256::from(14)); } #[test] @@ -1526,7 +1526,7 @@ fn mstore8() { stored_value.to_big_endian(&mut value_bytes); assert_eq!(value_bytes[0..1], [0xAB]); - assert_eq!(vm.env.consumed_gas, U256::from(12)); + assert_eq!(current_call_frame.gas_used, U256::from(12)); } #[test] @@ -1557,7 +1557,7 @@ fn mcopy() { let memory_size = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(memory_size, U256::from(96)); - assert_eq!(vm.env.consumed_gas, U256::from(35)); + assert_eq!(current_call_frame.gas_used, U256::from(35)); } #[test] @@ -1578,7 +1578,7 @@ fn mload() { let loaded_value = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(loaded_value, U256::from(0x33333)); - assert_eq!(vm.env.consumed_gas, U256::from(18)); + assert_eq!(current_call_frame.gas_used, U256::from(18)); } #[test] @@ -1592,7 +1592,7 @@ fn msize() { let initial_size = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(initial_size, U256::from(0)); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); let operations = [ Operation::Push((32, U256::from(0x33333))), // value @@ -1609,7 +1609,7 @@ fn msize() { let after_store_size = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(after_store_size, U256::from(32)); - assert_eq!(vm.env.consumed_gas, U256::from(14)); + assert_eq!(current_call_frame.gas_used, U256::from(14)); let operations = [ Operation::Push((32, U256::from(0x55555))), // value @@ -1626,7 +1626,7 @@ fn msize() { let final_size = vm.current_call_frame_mut().unwrap().stack.pop().unwrap(); assert_eq!(final_size, U256::from(96)); - assert_eq!(vm.env.consumed_gas, U256::from(20)); + assert_eq!(current_call_frame.gas_used, U256::from(20)); } #[test] @@ -1651,7 +1651,7 @@ fn mstore_mload_offset_not_multiple_of_32() { assert_eq!(loaded_value, U256::from(0xabcdef)); assert_eq!(memory_size, U256::from(64)); - assert_eq!(vm.env.consumed_gas, U256::from(23)); + assert_eq!(current_call_frame.gas_used, U256::from(23)); // check with big offset @@ -1675,7 +1675,7 @@ fn mstore_mload_offset_not_multiple_of_32() { assert_eq!(loaded_value, U256::from(0x123456)); assert_eq!(memory_size, U256::from(2048)); - assert_eq!(vm.env.consumed_gas, U256::from(217)); + assert_eq!(current_call_frame.gas_used, U256::from(217)); } #[test] @@ -1697,7 +1697,7 @@ fn mload_uninitialized_memory() { assert_eq!(loaded_value, U256::zero()); assert_eq!(memory_size, U256::from(96)); - assert_eq!(vm.env.consumed_gas, U256::from(17)); + assert_eq!(current_call_frame.gas_used, U256::from(17)); } #[test] @@ -1998,7 +1998,7 @@ fn pc_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(0) ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2018,7 +2018,7 @@ fn pc_op_with_push_offset() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(33) ); - assert_eq!(vm.env.consumed_gas, U256::from(5)); + assert_eq!(current_call_frame.gas_used, U256::from(5)); } // #[test] @@ -2316,7 +2316,7 @@ fn jumpi_not_zero() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(10) ); - assert_eq!(vm.env.consumed_gas, U256::from(20)); + assert_eq!(current_call_frame.gas_used, U256::from(20)); } #[test] @@ -2341,7 +2341,7 @@ fn jumpi_for_zero() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(100) ); - assert_eq!(vm.env.consumed_gas, U256::from(19)); + assert_eq!(current_call_frame.gas_used, U256::from(19)); } // This test is just for trying things out, not a real test. But it is useful to have this as an example for conversions between bytes and u256. @@ -2419,7 +2419,7 @@ fn calldataload() { 0x00, 0x00, 0x00, 0x00 ]) ); - assert_eq!(vm.env.consumed_gas, U256::from(6)); + assert_eq!(current_call_frame.gas_used, U256::from(6)); } #[test] @@ -2515,7 +2515,7 @@ fn calldatasize() { let current_call_frame = vm.current_call_frame_mut().unwrap(); let top_of_stack = current_call_frame.stack.pop().unwrap(); assert_eq!(top_of_stack, U256::from(3)); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2538,7 +2538,7 @@ fn calldatacopy() { let current_call_frame = vm.current_call_frame_mut().unwrap(); let memory = memory::load_range(&mut current_call_frame.memory, U256::zero(), 2).unwrap(); assert_eq!(memory, vec![0x22, 0x33]); - assert_eq!(vm.env.consumed_gas, U256::from(18)); + assert_eq!(current_call_frame.gas_used, U256::from(18)); } #[test] @@ -2555,7 +2555,7 @@ fn returndatasize() { let current_call_frame = vm.current_call_frame_mut().unwrap(); let top_of_stack = current_call_frame.stack.pop().unwrap(); assert_eq!(top_of_stack, U256::from(3)); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2578,7 +2578,7 @@ fn returndatacopy() { let current_call_frame = vm.current_call_frame_mut().unwrap(); let memory = memory::load_range(&mut current_call_frame.memory, U256::zero(), 2).unwrap(); assert_eq!(memory, vec![0xBB, 0xCC]); - assert_eq!(vm.env.consumed_gas, U256::from(18)); + assert_eq!(current_call_frame.gas_used, U256::from(18)); } #[test] @@ -2665,7 +2665,7 @@ fn blockhash_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), expected_block_hash ); - assert_eq!(vm.env.consumed_gas, U256::from(23)); + assert_eq!(current_call_frame.gas_used, U256::from(23)); } #[test] @@ -2697,7 +2697,7 @@ fn blockhash_same_block_number() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), expected_block_hash ); - assert_eq!(vm.env.consumed_gas, U256::from(23)); + assert_eq!(current_call_frame.gas_used, U256::from(23)); } #[test] @@ -2733,7 +2733,7 @@ fn blockhash_block_number_not_from_recent_256() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), expected_block_hash ); - assert_eq!(vm.env.consumed_gas, U256::from(23)); + assert_eq!(current_call_frame.gas_used, U256::from(23)); } #[test] @@ -2752,7 +2752,7 @@ fn coinbase_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(coinbase_address) ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2771,7 +2771,7 @@ fn timestamp_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), timestamp ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2790,7 +2790,7 @@ fn number_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), block_number ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2809,7 +2809,7 @@ fn prevrandao_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from_big_endian(&prevrandao.0) ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2828,7 +2828,7 @@ fn gaslimit_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), gas_limit ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2873,7 +2873,7 @@ fn chain_id_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), chain_id ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2892,7 +2892,7 @@ fn basefee_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), base_fee_per_gas ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } // TODO: Add excess_blob_gas and blob_gas_used to env @@ -2911,7 +2911,7 @@ fn blobbasefee_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(2) ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } // TODO: Add excess_blob_gas and blob_gas_used to env @@ -2930,7 +2930,7 @@ fn blobbasefee_minimum_cost() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::one() ); - assert_eq!(vm.env.consumed_gas, U256::from(2)); + assert_eq!(current_call_frame.gas_used, U256::from(2)); } #[test] @@ -2951,7 +2951,7 @@ fn pop_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::one() ); - assert_eq!(vm.env.consumed_gas, U256::from(8)); + assert_eq!(current_call_frame.gas_used, U256::from(8)); } #[test] @@ -2975,7 +2975,7 @@ fn jump_op() { U256::from(10) ); assert_eq!(vm.current_call_frame_mut().unwrap().pc(), 70); - assert_eq!(vm.env.consumed_gas, U256::from(15)); + assert_eq!(current_call_frame.gas_used, U256::from(15)); } #[test] @@ -3144,7 +3144,7 @@ fn log0() { assert_eq!(logs.len(), 1); assert_eq!(logs[0].data, data.to_vec()); assert_eq!(logs[0].topics.len(), 0); - assert_eq!(vm.env.consumed_gas, U256::from(649)); + assert_eq!(current_call_frame.gas_used, U256::from(649)); } #[test] @@ -3174,7 +3174,7 @@ fn log1() { assert_eq!(logs.len(), 1); assert_eq!(logs[0].data, data.to_vec()); assert_eq!(logs[0].topics, vec![H256::from_slice(&topic1)]); - assert_eq!(vm.env.consumed_gas, U256::from(1027)); + assert_eq!(current_call_frame.gas_used, U256::from(1027)); } #[test] @@ -3210,7 +3210,7 @@ fn log2() { logs[0].topics, vec![H256::from_slice(&topic1), H256::from_slice(&topic2)] ); - assert_eq!(vm.env.consumed_gas, U256::from(1405)); + assert_eq!(current_call_frame.gas_used, U256::from(1405)); } #[test] @@ -3253,7 +3253,7 @@ fn log3() { H256::from_slice(&topic3) ] ); - assert_eq!(vm.env.consumed_gas, U256::from(1783)); + assert_eq!(current_call_frame.gas_used, U256::from(1783)); } #[test] @@ -3300,7 +3300,7 @@ fn log4() { H256::from_slice(&topic4) ] ); - assert_eq!(vm.env.consumed_gas, U256::from(2161)); + assert_eq!(current_call_frame.gas_used, U256::from(2161)); } #[test] @@ -3325,7 +3325,7 @@ fn log_with_0_data_size() { assert_eq!(logs.len(), 1); assert_eq!(logs[0].data, Vec::new()); assert_eq!(logs[0].topics.len(), 0); - assert_eq!(vm.env.consumed_gas, U256::from(393)); + assert_eq!(current_call_frame.gas_used, U256::from(393)); } #[test] @@ -3378,7 +3378,7 @@ fn log_with_data_in_memory_smaller_than_size() { assert_eq!(logs.len(), 1); assert_eq!(logs[0].data, data); assert_eq!(logs[0].topics.len(), 0); - assert_eq!(vm.env.consumed_gas, U256::from(649)); + assert_eq!(current_call_frame.gas_used, U256::from(649)); } #[test] @@ -4184,7 +4184,7 @@ fn caller_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(caller.as_bytes()) ); - assert_eq!(vm.env.consumed_gas, gas_cost::CALLER); + assert_eq!(current_call_frame.gas_used, gas_cost::CALLER); } #[test] @@ -4227,7 +4227,7 @@ fn origin_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(msg_sender.as_bytes()) ); - assert_eq!(vm.env.consumed_gas, gas_cost::ORIGIN); + assert_eq!(current_call_frame.gas_used, gas_cost::ORIGIN); } #[test] @@ -4297,7 +4297,7 @@ fn address_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(address_that_has_the_code.as_bytes()) ); - assert_eq!(vm.env.consumed_gas, gas_cost::ADDRESS); + assert_eq!(current_call_frame.gas_used, gas_cost::ADDRESS); } #[test] @@ -4344,7 +4344,7 @@ fn selfbalance_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), balance ); - assert_eq!(vm.env.consumed_gas, gas_cost::SELFBALANCE); + assert_eq!(current_call_frame.gas_used, gas_cost::SELFBALANCE); } #[test] @@ -4388,7 +4388,7 @@ fn callvalue_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), value ); - assert_eq!(vm.env.consumed_gas, gas_cost::CALLVALUE); + assert_eq!(current_call_frame.gas_used, gas_cost::CALLVALUE); } #[test] @@ -4430,7 +4430,7 @@ fn codesize_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(2) ); - assert_eq!(vm.env.consumed_gas, gas_cost::CODESIZE); + assert_eq!(current_call_frame.gas_used, gas_cost::CODESIZE); } #[test] @@ -4472,7 +4472,7 @@ fn gasprice_op() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), U256::from(0x9876) ); - assert_eq!(vm.env.consumed_gas, gas_cost::GASPRICE); + assert_eq!(current_call_frame.gas_used, gas_cost::GASPRICE); } #[test] @@ -4536,7 +4536,7 @@ fn codecopy_op() { expected_memory ); assert_eq!( - vm.env.consumed_gas, + current_call_frame.gas_used, U256::from(9) + U256::from(3) * gas_cost::PUSHN ); } @@ -4564,7 +4564,7 @@ fn extcodesize_existing_account() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), 23.into() ); - assert_eq!(vm.env.consumed_gas, 2603.into()); + assert_eq!(current_call_frame.gas_used, 2603.into()); } #[test] @@ -4584,7 +4584,7 @@ fn extcodesize_non_existing_account() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), 0.into() ); - assert_eq!(vm.env.consumed_gas, 2603.into()); + assert_eq!(current_call_frame.gas_used, 2603.into()); } #[test] @@ -4620,7 +4620,7 @@ fn extcodecopy_existing_account() { .unwrap(), vec![0x60] ); - assert_eq!(vm.env.consumed_gas, 2616.into()); + assert_eq!(current_call_frame.gas_used, 2616.into()); } #[test] @@ -4650,7 +4650,7 @@ fn extcodecopy_non_existing_account() { .unwrap(), vec![0; size] ); - assert_eq!(vm.env.consumed_gas, 2616.into()); + assert_eq!(current_call_frame.gas_used, 2616.into()); } #[test] @@ -4673,7 +4673,7 @@ fn extcodehash_account_with_empty_code() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".into() ); - assert_eq!(vm.env.consumed_gas, 2603.into()); + assert_eq!(current_call_frame.gas_used, 2603.into()); } #[test] @@ -4693,7 +4693,7 @@ fn extcodehash_non_existing_account() { vm.current_call_frame_mut().unwrap().stack.pop().unwrap(), "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".into() ); - assert_eq!(vm.env.consumed_gas, 2603.into()); + assert_eq!(current_call_frame.gas_used, 2603.into()); } #[test] diff --git a/crates/vm/vm.rs b/crates/vm/vm.rs index 5aa8e9c81..ee0d06c79 100644 --- a/crates/vm/vm.rs +++ b/crates/vm/vm.rs @@ -169,7 +169,6 @@ cfg_if::cfg_if! { let env = Environment { origin: tx.sender(), - consumed_gas: U256::zero(), refunded_gas: U256::zero(), gas_limit: tx.gas_limit().into(), block_number: block_header.number.into(),