Skip to content

Commit

Permalink
fix runtime test
Browse files Browse the repository at this point in the history
  • Loading branch information
magecnion committed Jun 17, 2024
1 parent c81298c commit 6f0705b
Showing 1 changed file with 17 additions and 105 deletions.
122 changes: 17 additions & 105 deletions runtime/laos/src/tests/precompile_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::Runtime;
use core::str::FromStr;
use evm::Context;
use frame_support::assert_noop;
use pallet_evm::{ExitRevert, IsPrecompileResult, PrecompileFailure, PrecompileSet};
use pallet_evm::{ExitRevert, PrecompileFailure, PrecompileSet};
use precompile_utils::testing::MockHandle;
use sp_core::H160;

Expand All @@ -30,116 +30,28 @@ fn hash(a: u64) -> H160 {
H160::from_low_u64_be(a)
}

// Check if a given address corresponds to a precompile.
fn is_precompile(address: H160) -> Result<bool, &'static str> {
let p = LaosPrecompiles::<Runtime>::new();
match p.is_precompile(address, 0) {
IsPrecompileResult::Answer { is_precompile, .. } => Ok(is_precompile),
_ => Err("Unexpected result variant"),
}
}

/// Check if custom precompiled addresses are recognized.
#[test]
fn check_custom_precompiled_addresses() {
// Test specific custom precompiled addresses
assert!(is_precompile(hash(1027)).unwrap());
assert!(is_precompile(hash(1029)).unwrap());
assert!(is_precompile(H160::from_str("0xfffffffffffffffffffffffe0000000000000005").unwrap())
.unwrap());
}

#[test]
/// Ensure the null address is not considered a precompile.
fn null_address_is_not_precompile() {
assert!(!is_precompile(H160::zero()).unwrap());
}

/// Check if standard Ethereum precompiled addresses are recognized.
#[test]
fn check_ethereum_precompiled_addresses() {
// Test Ethereum precompiled addresses from 1 to 9
for i in 1..=9 {
assert!(is_precompile(hash(i)).unwrap(), "Address {} should be a precompile", i);
}
}

/// Test to ensure that delegate calls to addresses that are not precompiles are recognized
/// correctly.
#[test]
fn delegatecall_to_non_precompile_is_recognized() {
let precompiles = LaosPrecompiles::<Runtime>::new();

// Address outside the range of standard precompiles
let code_address = hash(11);
let context_address = hash(12);

// Verify that the code address is not a precompile and that it's not treated as a custom
// precompile delegate call
assert!(!is_precompile(code_address).unwrap());
// assert!(!precompiles.is_delegatecall_to_custom_precompile(code_address, context_address)); //
// TODO remove?
}

/// Test to ensure that delegate calls to non-standard Ethereum precompile addresses are recognized.
#[test]
fn delegatecall_to_custom_precompile_is_recognized() {
let precompiles = LaosPrecompiles::<Runtime>::new();

// Address representing a non-standard precompile
let code_address = hash(1027);
let context_address = hash(123456);

// Verify that the code address is a precompile and is recognized as a custom precompile
// delegate call
assert!(is_precompile(code_address).unwrap());
// assert!(precompiles.is_delegatecall_to_custom_precompile(code_address, context_address)); //
// TODO remove?
}

/// Test to ensure that delegate calls to standard Ethereum precompile addresses are not recognized
/// as custom precompiles.
#[test]
fn delegatecall_to_standard_precompile_not_recognized_as_custom() {
let precompiles = LaosPrecompiles::<Runtime>::new();

let context_address = hash(123456);

// Iterate over standard Ethereum precompile addresses (1 to 9)
for i in 1..=9 {
let code_address = hash(i);

// Verify each standard precompile address is not recognized as a custom precompile delegate
// call
assert!(is_precompile(code_address).unwrap());
// assert!(!precompiles.is_delegatecall_to_custom_precompile(code_address,
// context_address)); // TODO remove?
}
}

#[test]
fn execute_delegate_call_on_custom_precompile_should_fail() {
let p = LaosPrecompiles::<Runtime>::new();

let code_address = hash(1027);
let context_address = hash(123456);
ExtBuilder::default().build().execute_with(|| {
let p = LaosPrecompiles::<Runtime>::new();

// Verify that the code address is a precompile
assert!(is_precompile(code_address).unwrap());
let code_address = hash(1027);
let context_address = hash(123456);

// Setup the mock handle for the delegate call
let mut handle = MockHandle::new(
code_address,
Context { address: context_address, caller: H160::zero(), apparent_value: 0.into() },
);
// Setup the mock handle for the delegate call
let mut handle = MockHandle::new(
code_address,
Context { address: context_address, caller: H160::zero(), apparent_value: 0.into() },
);

// Execute the precompile with the delegate call
let result = p.execute(&mut handle);
// Execute the precompile with the delegate call
let result = p.execute(&mut handle);

// Verify that the execution failed due to a delegate call to a custom precompile
assert!(
matches!(result, Some(Err(PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output })) if output == b"cannot be called with DELEGATECALL or CALLCODE")
);
// Verify that the execution failed due to a delegate call to a custom precompile
assert!(
matches!(result, Some(Err(PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output })) if String::from_utf8_lossy(&output).contains("Cannot be called with DELEGATECALL or CALLCODE"))
);
});
}

#[test]
Expand Down

0 comments on commit 6f0705b

Please sign in to comment.