Skip to content

Commit

Permalink
fix evm-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zjb0807 committed Jan 24, 2024
1 parent 2c7f562 commit f0013a4
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 51 deletions.
60 changes: 13 additions & 47 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion evm-tests
13 changes: 13 additions & 0 deletions modules/evm/src/precompiles/blake2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ impl Precompile for Blake2F {
output: output_buf.to_vec(),
})
}

#[cfg(feature = "evm-tests")]
fn execute_ext(
input: &[u8],
target_gas: Option<u64>,
context: &crate::Context,
is_static: bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure> {
let mut handle = crate::precompiles::tests::MockPrecompileHandle::new(&input, target_gas, context, is_static);
let output = Self::execute(&mut handle)?;

Ok((output, handle.gas_used))
}
}

#[cfg(test)]
Expand Down
39 changes: 39 additions & 0 deletions modules/evm/src/precompiles/bn128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ impl Precompile for Bn128Add {
output: buf.to_vec(),
})
}

#[cfg(feature = "evm-tests")]
fn execute_ext(
input: &[u8],
target_gas: Option<u64>,
context: &crate::Context,
is_static: bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure> {
let mut handle = crate::precompiles::tests::MockPrecompileHandle::new(&input, target_gas, context, is_static);
let output = Self::execute(&mut handle)?;

Ok((output, handle.gas_used))
}
}

/// The Bn128Mul builtin
Expand Down Expand Up @@ -137,6 +150,19 @@ impl Precompile for Bn128Mul {
output: buf.to_vec(),
})
}

#[cfg(feature = "evm-tests")]
fn execute_ext(
input: &[u8],
target_gas: Option<u64>,
context: &crate::Context,
is_static: bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure> {
let mut handle = crate::precompiles::tests::MockPrecompileHandle::new(&input, target_gas, context, is_static);
let output = Self::execute(&mut handle)?;

Ok((output, handle.gas_used))
}
}

/// The Bn128Pairing builtin
Expand Down Expand Up @@ -236,6 +262,19 @@ impl Precompile for Bn128Pairing {
output: buf.to_vec(),
})
}

#[cfg(feature = "evm-tests")]
fn execute_ext(
input: &[u8],
target_gas: Option<u64>,
context: &crate::Context,
is_static: bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure> {
let mut handle = crate::precompiles::tests::MockPrecompileHandle::new(&input, target_gas, context, is_static);
let output = Self::execute(&mut handle)?;

Ok((output, handle.gas_used))
}
}

#[cfg(test)]
Expand Down
20 changes: 20 additions & 0 deletions modules/evm/src/precompiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ pub trait Precompile {
/// `target_gas`. Return `Ok(status, output, gas_used)` if the execution is
/// successful. Otherwise return `Err(_)`.
fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult;

#[cfg(feature = "evm-tests")]
fn execute_ext(
input: &[u8],
target_gas: Option<u64>,
context: &crate::Context,
is_static: bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure>;
}

pub trait LinearCostPrecompile {
Expand All @@ -66,6 +74,18 @@ impl<T: LinearCostPrecompile> Precompile for T {
let (exit_status, output) = T::execute(handle.input(), cost)?;
Ok(PrecompileOutput { exit_status, output })
}

#[cfg(feature = "evm-tests")]
fn execute_ext(
input: &[u8],
target_gas: Option<u64>,
context: &crate::Context,
is_static: bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure> {
let cost = ensure_linear_cost(target_gas, input.len() as u64, T::BASE, T::WORD)?;
let (exit_status, output) = T::execute(input, cost)?;
Ok((PrecompileOutput { exit_status, output }, cost))
}
}

/// Linear gas cost
Expand Down
26 changes: 26 additions & 0 deletions modules/evm/src/precompiles/modexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ impl Precompile for IstanbulModexp {
output,
})
}

#[cfg(feature = "evm-tests")]
fn execute_ext(
input: &[u8],
target_gas: Option<u64>,
context: &crate::Context,
is_static: bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure> {
let mut handle = crate::precompiles::tests::MockPrecompileHandle::new(&input, target_gas, context, is_static);
let output = Self::execute(&mut handle)?;

Ok((output, handle.gas_used))
}
}

impl Precompile for Modexp {
Expand Down Expand Up @@ -335,6 +348,19 @@ impl Precompile for Modexp {
output,
})
}

#[cfg(feature = "evm-tests")]
fn execute_ext(
input: &[u8],
target_gas: Option<u64>,
context: &crate::Context,
is_static: bool,
) -> Result<(PrecompileOutput, u64), PrecompileFailure> {
let mut handle = crate::precompiles::tests::MockPrecompileHandle::new(&input, target_gas, context, is_static);
let output = Self::execute(&mut handle)?;

Ok((output, handle.gas_used))
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion modules/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ impl<'vicinity, 'config, T: Config> BackendT for SubstrateStackState<'vicinity,
}

fn block_randomness(&self) -> Option<H256> {
None
self.vicinity.block_randomness
}

fn block_hash(&self, number: U256) -> H256 {
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub struct Vicinity {
pub block_difficulty: Option<U256>,
/// Environmental base fee per gas.
pub block_base_fee_per_gas: Option<U256>,
/// Environmental randomness.
pub block_randomness: Option<H256>,
}

#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
Expand Down
4 changes: 2 additions & 2 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ impl<I: From<Balance>> frame_support::traits::Get<I> for TxFeePerGasV2 {
}

#[cfg(feature = "with-ethereum-compatibility")]
static LONDON_CONFIG: module_evm_utility::evm::Config = module_evm_utility::evm::Config::london();
static SHANGHAI_CONFIG: module_evm_utility::evm::Config = module_evm_utility::evm::Config::shanghai();

impl module_evm::Config for Runtime {
type AddressMapping = EvmAddressMapping<Runtime>;
Expand Down Expand Up @@ -1673,7 +1673,7 @@ impl module_evm::Config for Runtime {

#[cfg(feature = "with-ethereum-compatibility")]
fn config() -> &'static module_evm_utility::evm::Config {
&LONDON_CONFIG
&SHANGHAI_CONFIG
}
}

Expand Down

0 comments on commit f0013a4

Please sign in to comment.