diff --git a/crates/vm/levm/src/gas_cost.rs b/crates/vm/levm/src/gas_cost.rs index a0b1d7faf..1e1f87c20 100644 --- a/crates/vm/levm/src/gas_cost.rs +++ b/crates/vm/levm/src/gas_cost.rs @@ -267,9 +267,6 @@ fn copy_behavior( .map_err(|_| VMError::VeryLargeNumber)?; let memory_expansion_cost = memory::expansion_cost(new_memory_size, current_memory_size)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::VeryLargeNumber)?; let minimum_word_size_cost = dynamic_base .checked_mul(minimum_word_size) @@ -312,10 +309,6 @@ pub fn log( .checked_mul(size) .ok_or(OutOfGasError::GasCostOverflow)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::VeryLargeNumber)?; - Ok(topics_cost .checked_add(LOGN_STATIC) .ok_or(OutOfGasError::GasCostOverflow)? @@ -343,9 +336,6 @@ fn mem_expansion_behavior( static_cost: u64, ) -> Result { let memory_expansion_cost = memory::expansion_cost(new_memory_size, current_memory_size)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::RevertOpcode)?; Ok(static_cost .checked_add(memory_expansion_cost) @@ -408,9 +398,6 @@ pub fn mcopy( / WORD_SIZE; let memory_expansion_cost = memory::expansion_cost(new_memory_size, current_memory_size)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::VeryLargeNumber)?; let words_copied: u64 = words_copied .try_into() @@ -474,9 +461,6 @@ fn compute_gas_create( .ok_or(OutOfGasError::GasCostOverflow)?; // will not panic since it's 2 let memory_expansion_cost = memory::expansion_cost(new_memory_size, current_memory_size)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::VeryLargeNumber)?; let hash_cost = if is_create_2 { minimum_word_size @@ -635,9 +619,6 @@ pub fn call( gas_left: u64, ) -> Result<(u64, u64), VMError> { let memory_expansion_cost = memory::expansion_cost(new_memory_size, current_memory_size)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::VeryLargeNumber)?; let address_access_cost = address_access_cost( address_was_cold, @@ -681,9 +662,6 @@ pub fn callcode( gas_left: u64, ) -> Result<(u64, u64), VMError> { let memory_expansion_cost = memory::expansion_cost(new_memory_size, current_memory_size)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::VeryLargeNumber)?; let address_access_cost = address_access_cost( address_was_cold, @@ -719,9 +697,6 @@ pub fn delegatecall( gas_left: u64, ) -> Result<(u64, u64), VMError> { let memory_expansion_cost = memory::expansion_cost(new_memory_size, current_memory_size)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::VeryLargeNumber)?; let address_access_cost = address_access_cost( address_was_cold, @@ -744,9 +719,6 @@ pub fn staticcall( gas_left: u64, ) -> Result<(u64, u64), VMError> { let memory_expansion_cost = memory::expansion_cost(new_memory_size, current_memory_size)?; - let memory_expansion_cost: u64 = memory_expansion_cost - .try_into() - .map_err(|_| VMError::VeryLargeNumber)?; let address_access_cost = address_access_cost( address_was_cold, diff --git a/crates/vm/levm/src/memory.rs b/crates/vm/levm/src/memory.rs index e20de3db2..90d5c8d35 100644 --- a/crates/vm/levm/src/memory.rs +++ b/crates/vm/levm/src/memory.rs @@ -167,10 +167,7 @@ pub fn try_copy_within( /// When a memory expansion is triggered, only the additional bytes of memory /// must be paid for. -pub fn expansion_cost( - new_memory_size: usize, - current_memory_size: usize, -) -> Result { +pub fn expansion_cost(new_memory_size: usize, current_memory_size: usize) -> Result { let cost = if new_memory_size <= current_memory_size { 0 } else { @@ -182,7 +179,7 @@ pub fn expansion_cost( } /// The total cost for a given memory size. -fn cost(memory_size: usize) -> Result { +fn cost(memory_size: usize) -> Result { let memory_size_word = memory_size .checked_add( WORD_SIZE_IN_BYTES_USIZE @@ -192,7 +189,7 @@ fn cost(memory_size: usize) -> Result { .ok_or(OutOfGasError::MemoryExpansionCostOverflow)? / WORD_SIZE_IN_BYTES_USIZE; - Ok(memory_size_word + let gas_cost = memory_size_word .checked_pow(2) .ok_or(OutOfGasError::MemoryExpansionCostOverflow)? .checked_div(MEMORY_EXPANSION_QUOTIENT) @@ -202,7 +199,9 @@ fn cost(memory_size: usize) -> Result { .checked_mul(memory_size_word) .ok_or(OutOfGasError::MemoryExpansionCostOverflow)?, ) - .ok_or(OutOfGasError::MemoryExpansionCostOverflow)?) + .ok_or(OutOfGasError::MemoryExpansionCostOverflow)?; + + gas_cost.try_into().map_err(|_| VMError::VeryLargeNumber) } pub fn calculate_memory_size(offset: U256, size: usize) -> Result { diff --git a/crates/vm/levm/src/opcode_handlers/system.rs b/crates/vm/levm/src/opcode_handlers/system.rs index 07b5f5528..bfc44adc7 100644 --- a/crates/vm/levm/src/opcode_handlers/system.rs +++ b/crates/vm/levm/src/opcode_handlers/system.rs @@ -173,10 +173,8 @@ impl VM { let new_memory_size = calculate_memory_size(offset, size)?; - let memory_expansion_cost: u64 = - memory::expansion_cost(new_memory_size, current_call_frame.memory.len())? - .try_into() - .map_err(|_err| VMError::Internal(InternalError::ConversionError))?; + let memory_expansion_cost = + memory::expansion_cost(new_memory_size, current_call_frame.memory.len())?; self.increase_consumed_gas(current_call_frame, memory_expansion_cost)?; @@ -409,9 +407,7 @@ impl VM { let new_memory_size = calculate_memory_size(offset, size)?; let memory_expansion_cost: u64 = - memory::expansion_cost(new_memory_size, current_call_frame.memory.len())? - .try_into() - .map_err(|_err| VMError::Internal(InternalError::ConversionError))?; + memory::expansion_cost(new_memory_size, current_call_frame.memory.len())?; self.increase_consumed_gas(current_call_frame, memory_expansion_cost)?;