Skip to content

Commit

Permalink
clippy fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Aug 8, 2024
1 parent 3e87194 commit 9efea8a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 72 deletions.
70 changes: 30 additions & 40 deletions gasometer/src/costs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::consts::*;
use crate::consts;
use crate::Config;
use evm_core::utils::U64_MAX;
use evm_core::ExitError;
Expand All @@ -16,7 +16,7 @@ pub const fn suicide_refund(already_removed: bool) -> i64 {
if already_removed {
0
} else {
R_SUICIDE as i64
consts::R_SUICIDE as i64
}
}

Expand All @@ -40,11 +40,12 @@ pub fn sstore_refund(original: H256, current: H256, new: H256, config: &Config)
}

if original == new {
if original == H256::default() {
refund += (config.gas_sstore_set - config.gas_sload) as i64;
let for_refund = if original == H256::default() {
config.gas_sstore_set - config.gas_sload
} else {
refund += (config.gas_sstore_reset - config.gas_sload) as i64;
}
config.gas_sstore_reset - config.gas_sload
};
refund += i64::try_from(for_refund).unwrap_or(i64::MAX);
}

refund
Expand All @@ -60,15 +61,15 @@ pub fn sstore_refund(original: H256, current: H256, new: H256, config: &Config)
}

pub fn create2_cost(len: U256) -> Result<u64, ExitError> {
let base = U256::from(G_CREATE);
let base = U256::from(consts::G_CREATE);
// ceil(len / 32.0)
let sha_addup_base = len / U256::from(32)
+ if len % U256::from(32) == U256::zero() {
U256::zero()
} else {
U256::one()
};
let sha_addup = U256::from(G_SHA3WORD)
let sha_addup = U256::from(consts::G_SHA3WORD)
.checked_mul(sha_addup_base)
.ok_or(ExitError::OutOfGas)?;
let gas = base.checked_add(sha_addup).ok_or(ExitError::OutOfGas)?;
Expand All @@ -82,9 +83,9 @@ pub fn create2_cost(len: U256) -> Result<u64, ExitError> {

pub fn exp_cost(power: U256, config: &Config) -> Result<u64, ExitError> {
if power == U256::zero() {
Ok(G_EXP as u64)
Ok(u64::from(consts::G_EXP))
} else {
let gas = U256::from(G_EXP)
let gas = U256::from(consts::G_EXP)
.checked_add(
U256::from(config.gas_expbyte)
.checked_mul(U256::from(crate::utils::log2floor(power) / 8 + 1))
Expand All @@ -102,16 +103,12 @@ pub fn exp_cost(power: U256, config: &Config) -> Result<u64, ExitError> {

pub fn verylowcopy_cost(len: U256) -> Result<u64, ExitError> {
let wordd = len / U256::from(32);
let wordr = len % U256::from(32);
let is_wordr = (len % U256::from(32)) == U256::zero();

let gas = U256::from(G_VERYLOW)
let gas = U256::from(consts::G_VERYLOW)
.checked_add(
U256::from(G_COPY)
.checked_mul(if wordr == U256::zero() {
wordd
} else {
wordd + U256::one()
})
U256::from(consts::G_COPY)
.checked_mul(if is_wordr { wordd } else { wordd + U256::one() })
.ok_or(ExitError::OutOfGas)?,
)
.ok_or(ExitError::OutOfGas)?;
Expand All @@ -125,15 +122,11 @@ pub fn verylowcopy_cost(len: U256) -> Result<u64, ExitError> {

pub fn extcodecopy_cost(len: U256, is_cold: bool, config: &Config) -> Result<u64, ExitError> {
let wordd = len / U256::from(32);
let wordr = len % U256::from(32);
let is_wordr = (len % U256::from(32)) == U256::zero();
let gas = U256::from(address_access_cost(is_cold, config.gas_ext_code, config))
.checked_add(
U256::from(G_COPY)
.checked_mul(if wordr == U256::zero() {
wordd
} else {
wordd + U256::one()
})
U256::from(consts::G_COPY)
.checked_mul(if is_wordr { wordd } else { wordd + U256::one() })
.ok_or(ExitError::OutOfGas)?,
)
.ok_or(ExitError::OutOfGas)?;
Expand All @@ -146,14 +139,14 @@ pub fn extcodecopy_cost(len: U256, is_cold: bool, config: &Config) -> Result<u64
}

pub fn log_cost(n: u8, len: U256) -> Result<u64, ExitError> {
let gas = U256::from(G_LOG)
let gas = U256::from(consts::G_LOG)
.checked_add(
U256::from(G_LOGDATA)
U256::from(consts::G_LOGDATA)
.checked_mul(len)
.ok_or(ExitError::OutOfGas)?,
)
.ok_or(ExitError::OutOfGas)?
.checked_add(U256::from(G_LOGTOPIC * n as u32))
.checked_add(U256::from(consts::G_LOGTOPIC * u32::from(n)))
.ok_or(ExitError::OutOfGas)?;

if gas > U64_MAX {
Expand All @@ -165,16 +158,12 @@ pub fn log_cost(n: u8, len: U256) -> Result<u64, ExitError> {

pub fn sha3_cost(len: U256) -> Result<u64, ExitError> {
let wordd = len / U256::from(32);
let wordr = len % U256::from(32);
let is_wordr = (len % U256::from(32)) == U256::zero();

let gas = U256::from(G_SHA3)
let gas = U256::from(consts::G_SHA3)
.checked_add(
U256::from(G_SHA3WORD)
.checked_mul(if wordr == U256::zero() {
wordd
} else {
wordd + U256::one()
})
U256::from(consts::G_SHA3WORD)
.checked_mul(if is_wordr { wordd } else { wordd + U256::one() })
.ok_or(ExitError::OutOfGas)?,
)
.ok_or(ExitError::OutOfGas)?;
Expand Down Expand Up @@ -266,11 +255,12 @@ pub fn suicide_cost(value: U256, is_cold: bool, target_exists: bool, config: &Co

let mut gas = config.gas_suicide + suicide_gas_topup;
if config.increase_state_access_gas && is_cold {
gas += config.gas_account_access_cold
gas += config.gas_account_access_cold;
}
gas
}

#[allow(clippy::fn_params_excessive_bools)]
pub fn call_cost(
value: U256,
is_cold: bool,
Expand Down Expand Up @@ -299,7 +289,7 @@ pub const fn address_access_cost(is_cold: bool, regular_value: u64, config: &Con

const fn xfer_cost(is_call_or_callcode: bool, transfers_value: bool) -> u64 {
if is_call_or_callcode && transfers_value {
G_CALLVALUE as u64
consts::G_CALLVALUE as u64
} else {
0
}
Expand All @@ -315,12 +305,12 @@ const fn new_cost(
if is_call_or_staticcall {
if eip161 {
if transfers_value && new_account {
G_NEWACCOUNT as u64
consts::G_NEWACCOUNT as u64
} else {
0
}
} else if new_account {
G_NEWACCOUNT as u64
consts::G_NEWACCOUNT as u64
} else {
0
}
Expand Down
34 changes: 17 additions & 17 deletions gasometer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![deny(warnings)]
#![forbid(unsafe_code, unused_variables)]
// #![deny(clippy::pedantic, clippy::nursery)]
#![deny(clippy::pedantic, clippy::nursery)]
#![allow(clippy::module_name_repetitions)]
#![cfg_attr(not(feature = "std"), no_std)]

Expand Down Expand Up @@ -83,8 +83,9 @@ pub struct Snapshot {

#[cfg(feature = "tracing")]
impl Snapshot {
fn new<'config>(gas_limit: u64, inner: &'config Inner<'config>) -> Snapshot {
Snapshot {
#[must_use]
const fn new<'config>(gas_limit: u64, inner: &'config Inner<'config>) -> Self {
Self {
gas_limit,
memory_gas: inner.memory_gas,
used_gas: inner.used_gas,
Expand Down Expand Up @@ -224,7 +225,7 @@ impl<'config> Gasometer<'config> {
/// Return `ExitError`
#[inline]
pub fn record_deposit(&mut self, len: usize) -> Result<(), ExitError> {
let cost = len as u64 * (consts::G_CODEDEPOSIT as u64);
let cost = len as u64 * u64::from(consts::G_CODEDEPOSIT);
self.record_cost(cost)
}

Expand Down Expand Up @@ -568,7 +569,8 @@ pub fn static_opcode_cost(opcode: Opcode) -> Option<u32> {
#[allow(
clippy::nonminimal_bool,
clippy::cognitive_complexity,
clippy::too_many_lines
clippy::too_many_lines,
clippy::match_same_arms
)]
pub fn dynamic_opcode_cost<H: Handler>(
address: H160,
Expand Down Expand Up @@ -883,12 +885,10 @@ impl<'config> Inner<'config> {

fn extra_check(&self, cost: GasCost, after_gas: u64) -> Result<(), ExitError> {
match cost {
GasCost::Call { gas, .. } => costs::call_extra_check(gas, after_gas, self.config),
GasCost::CallCode { gas, .. } => costs::call_extra_check(gas, after_gas, self.config),
GasCost::DelegateCall { gas, .. } => {
costs::call_extra_check(gas, after_gas, self.config)
}
GasCost::StaticCall { gas, .. } => costs::call_extra_check(gas, after_gas, self.config),
GasCost::Call { gas, .. }
| GasCost::CallCode { gas, .. }
| GasCost::DelegateCall { gas, .. }
| GasCost::StaticCall { gas, .. } => costs::call_extra_check(gas, after_gas, self.config),
_ => Ok(()),
}
}
Expand Down Expand Up @@ -964,14 +964,14 @@ impl<'config> Inner<'config> {
GasCost::Log { n, len } => costs::log_cost(n, len)?,
GasCost::VeryLowCopy { len } => costs::verylowcopy_cost(len)?,
GasCost::Exp { power } => costs::exp_cost(power, self.config)?,
GasCost::Create => consts::G_CREATE as u64,
GasCost::Create => u64::from(consts::G_CREATE),
GasCost::Create2 { len } => costs::create2_cost(len)?,
GasCost::SLoad { target_is_cold } => costs::sload_cost(target_is_cold, self.config),

GasCost::Zero => consts::G_ZERO as u64,
GasCost::Base => consts::G_BASE as u64,
GasCost::VeryLow => consts::G_VERYLOW as u64,
GasCost::Low => consts::G_LOW as u64,
GasCost::Zero => u64::from(consts::G_ZERO),
GasCost::Base => u64::from(consts::G_BASE),
GasCost::VeryLow => u64::from(consts::G_VERYLOW),
GasCost::Low => u64::from(consts::G_LOW),
GasCost::Invalid(opcode) => return Err(ExitError::InvalidCode(opcode)),

GasCost::ExtCodeSize { target_is_cold } => {
Expand All @@ -984,7 +984,7 @@ impl<'config> Inner<'config> {
GasCost::Balance { target_is_cold } => {
costs::address_access_cost(target_is_cold, self.config.gas_balance, self.config)
}
GasCost::BlockHash => consts::G_BLOCKHASH as u64,
GasCost::BlockHash => u64::from(consts::G_BLOCKHASH),
GasCost::ExtCodeHash { target_is_cold } => costs::address_access_cost(
target_is_cold,
self.config.gas_ext_code_hash,
Expand Down
4 changes: 2 additions & 2 deletions gasometer/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::consts::*;
use crate::consts;
use evm_core::ExitError;

pub fn memory_gas(a: usize) -> Result<u64, ExitError> {
let a = a as u64;
(G_MEMORY as u64)
u64::from(consts::G_MEMORY)
.checked_mul(a)
.ok_or(ExitError::OutOfGas)?
.checked_add(a.checked_mul(a).ok_or(ExitError::OutOfGas)? / 512)
Expand Down
5 changes: 2 additions & 3 deletions gasometer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ pub fn log2floor(value: U256) -> u64 {
if value.0[i] == 0u64 {
l -= 64;
} else {
l -= value.0[i].leading_zeros() as u64;
l -= u64::from(value.0[i].leading_zeros());
if l == 0 {
return l;
} else {
return l - 1;
}
return l - 1;
}
}
l
Expand Down
28 changes: 18 additions & 10 deletions runtime/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,26 @@ pub fn finish_call(

match reason {
ExitReason::Succeed(_) => {
if runtime.machine.memory_mut().copy_large(
out_offset,
U256::zero(),
target_len,
&runtime.return_data_buffer[..],
) == Ok(())
let value_to_push = if runtime
.machine
.memory_mut()
.copy_large(
out_offset,
U256::zero(),
target_len,
&runtime.return_data_buffer[..],
)
.is_ok()
{
runtime.machine.stack_mut().push(U256::one())?;
U256::one()
} else {
runtime.machine.stack_mut().push(U256::zero())?;
}
Ok(())
U256::zero()
};
runtime
.machine
.stack_mut()
.push(value_to_push)
.map_err(Into::into)
}
ExitReason::Revert(_) => {
runtime.machine.stack_mut().push(U256::zero())?;
Expand Down

0 comments on commit 9efea8a

Please sign in to comment.