From 9bce3c888889cddb9395e1d0a5dde30fb740780d Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 3 May 2023 21:31:26 +0800 Subject: [PATCH] Update generic params and add tests (#1047) --- frame/evm/precompile/dispatch/src/lib.rs | 17 ++-- frame/evm/precompile/dispatch/src/tests.rs | 95 ++++++++++------------ 2 files changed, 52 insertions(+), 60 deletions(-) diff --git a/frame/evm/precompile/dispatch/src/lib.rs b/frame/evm/precompile/dispatch/src/lib.rs index 790e95809c..6a4d3d46fd 100644 --- a/frame/evm/precompile/dispatch/src/lib.rs +++ b/frame/evm/precompile/dispatch/src/lib.rs @@ -51,7 +51,7 @@ where T: pallet_evm::Config, T::RuntimeCall: Dispatchable + GetDispatchInfo + Decode, ::RuntimeOrigin: From>, - DispatchValidator: DispatchValidateT, + DispatchValidator: DispatchValidateT, DecodeLimit: Get, { fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult { @@ -106,22 +106,21 @@ where } /// Dispatch validation trait. -pub trait DispatchValidateT { +pub trait DispatchValidateT { fn validate_before_dispatch( - origin: &T::AccountId, - call: &T::RuntimeCall, + origin: &AccountId, + call: &RuntimeCall, ) -> Option; } /// The default implementation of `DispatchValidateT`. -impl DispatchValidateT for () +impl DispatchValidateT for () where - T: pallet_evm::Config, - T::RuntimeCall: GetDispatchInfo, + RuntimeCall: GetDispatchInfo, { fn validate_before_dispatch( - _origin: &T::AccountId, - call: &T::RuntimeCall, + _origin: &AccountId, + call: &RuntimeCall, ) -> Option { let info = call.get_dispatch_info(); if !(info.pays_fee == Pays::Yes && info.class == DispatchClass::Normal) { diff --git a/frame/evm/precompile/dispatch/src/tests.rs b/frame/evm/precompile/dispatch/src/tests.rs index f5fca870e1..55acb68141 100644 --- a/frame/evm/precompile/dispatch/src/tests.rs +++ b/frame/evm/precompile/dispatch/src/tests.rs @@ -20,62 +20,16 @@ use super::*; use crate::mock::*; -use fp_evm::{Context, GenesisAccount}; -use frame_support::{assert_ok, traits::GenesisBuild}; +use fp_evm::Context; +use frame_support::{assert_err, assert_ok}; use scale_codec::Encode; use sp_core::{H160, U256}; -use std::{collections::BTreeMap, str::FromStr}; pub fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::default() + frame_system::GenesisConfig::default() .build_storage::() - .unwrap(); - - let mut accounts = BTreeMap::new(); - accounts.insert( - H160::from_str("1000000000000000000000000000000000000001").unwrap(), - GenesisAccount { - nonce: U256::from(1), - balance: U256::from(1000000), - storage: Default::default(), - code: vec![ - 0x00, // STOP - ], - }, - ); - accounts.insert( - H160::from_str("1000000000000000000000000000000000000002").unwrap(), - GenesisAccount { - nonce: U256::from(1), - balance: U256::from(1000000), - storage: Default::default(), - code: vec![ - 0xff, // INVALID - ], - }, - ); - accounts.insert( - H160::default(), // root - GenesisAccount { - nonce: U256::from(1), - balance: U256::max_value(), - storage: Default::default(), - code: vec![], - }, - ); - - pallet_balances::GenesisConfig:: { - // Create the block author account with some balance. - balances: vec![( - H160::from_str("0x1234500000000000000000000000000000000000").unwrap(), - 12345, - )], - } - .assimilate_storage(&mut t) - .expect("Pallet balances storage can be assimilated"); - GenesisBuild::::assimilate_storage(&pallet_evm::GenesisConfig { accounts }, &mut t) - .unwrap(); - t.into() + .unwrap() + .into() } #[test] @@ -135,3 +89,42 @@ fn decode_limit_ok() { assert_ok!(Dispatch::::execute(&mut handle)); }); } + +#[test] +fn dispatch_validator_works_well() { + new_test_ext().execute_with(|| { + let call = RuntimeCall::System(frame_system::Call::remark { remark: Vec::new() }); + let mut handle = MockHandle { + input: call.encode(), + context: Context { + address: H160::default(), + caller: H160::default(), + apparent_value: U256::default(), + }, + }; + assert_ok!(Dispatch::::execute(&mut handle)); + + pub struct MockValidator; + impl DispatchValidateT for MockValidator { + fn validate_before_dispatch( + _origin: &H160, + call: &RuntimeCall, + ) -> Option { + match call { + RuntimeCall::System(frame_system::Call::remark { remark: _ }) => { + return Some(PrecompileFailure::Error { + exit_status: ExitError::Other("This call is not allowed".into()), + }) + } + _ => None, + } + } + } + assert_err!( + Dispatch::::execute(&mut handle), + PrecompileFailure::Error { + exit_status: ExitError::Other("This call is not allowed".into()), + } + ); + }); +}