Skip to content
Prev Previous commit
Next Next commit
[revm] gas refactor
  • Loading branch information
rakita committed Feb 19, 2022
commit 73617606ebac5df221c863725707bea6547ad8e9
1 change: 0 additions & 1 deletion crates/revm/src/gas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod calc;
mod constants;
mod utils;

pub use calc::*;
pub use constants::*;
Expand Down
22 changes: 21 additions & 1 deletion crates/revm/src/gas/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ pub fn create2_cost(len: usize) -> Option<u64> {
Some(gas)
}


fn log2floor(value: U256) -> u64 {
assert!(!value.is_zero());
let mut l: u64 = 256;
for i in 0..4 {
let i = 3 - i;
if value.0[i] == 0u64 {
l -= 64;
} else {
l -= value.0[i].leading_zeros() as u64;
if l == 0 {
return l;
} else {
return l - 1;
}
}
}
l
}

pub fn exp_cost<SPEC: Spec>(power: U256) -> Option<u64> {
if power.is_zero() {
Some(EXP)
Expand All @@ -73,7 +93,7 @@ pub fn exp_cost<SPEC: Spec>(power: U256) -> Option<u64> {
10
}); // EIP-160: EXP cost increase
let gas = U256::from(EXP).checked_add(
gas_byte.checked_mul(U256::from(super::utils::log2floor(power) / 8 + 1))?,
gas_byte.checked_mul(U256::from(log2floor(power) / 8 + 1))?,
)?;

if gas > U256::from(u64::MAX) {
Expand Down
20 changes: 0 additions & 20 deletions crates/revm/src/gas/utils.rs

This file was deleted.

11 changes: 0 additions & 11 deletions crates/revm/src/interpreter/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,5 @@ mod tests {
let next_multiple = x + 32 - (x % 32);
assert_eq!(Some(next_multiple), next_multiple_of_32(x.into()));
}

// // next_multiple_of_32 returns None when the next multiple of 32 is too big
// let last_multiple_of_32 = U256::MAX & !U256::from(31);
// for i in 0..63 {
// let x = U256::MAX - U256::from(i);
// if x > last_multiple_of_32 {
// assert_eq!(None, next_multiple_of_32(x));
// } else {
// assert_eq!(Some(last_multiple_of_32), next_multiple_of_32(x));
// }
// }
}
}