From 03ea05f902c8e000709473e7397d5cb3551714bf Mon Sep 17 00:00:00 2001 From: bear Date: Thu, 31 Aug 2023 13:38:13 +0800 Subject: [PATCH 1/5] Update the error message --- runtime/crab/src/pallets/evm.rs | 4 +++- runtime/darwinia/src/pallets/evm.rs | 4 +++- runtime/pangolin/src/pallets/evm.rs | 4 +++- runtime/pangoro/src/pallets/evm.rs | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/runtime/crab/src/pallets/evm.rs b/runtime/crab/src/pallets/evm.rs index c64188c8a..2310d1673 100644 --- a/runtime/crab/src/pallets/evm.rs +++ b/runtime/crab/src/pallets/evm.rs @@ -194,7 +194,9 @@ impl DispatchValidateT for DarwiniaDispatchValidator { | RuntimeCall::MessageTransact(..) ) { Some(fp_evm::PrecompileFailure::Error { - exit_status: ExitError::Other("These pallet's calls are forbidden".into()), + exit_status: ExitError::Other( + "These pallet's calls are not allowed to be called from precompile.".into(), + ), }) } else { <() as DispatchValidateT>::validate_before_dispatch( diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 8c53decc7..f51508082 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -193,7 +193,9 @@ impl DispatchValidateT for DarwiniaDispatchValidator { | RuntimeCall::MessageTransact(..) ) { Some(fp_evm::PrecompileFailure::Error { - exit_status: ExitError::Other("These pallet's calls are forbidden".into()), + exit_status: ExitError::Other( + "These pallet's calls are not allowed to be called from precompile.".into(), + ), }) } else { <() as DispatchValidateT>::validate_before_dispatch( diff --git a/runtime/pangolin/src/pallets/evm.rs b/runtime/pangolin/src/pallets/evm.rs index 80e56606d..28c2c209c 100644 --- a/runtime/pangolin/src/pallets/evm.rs +++ b/runtime/pangolin/src/pallets/evm.rs @@ -195,7 +195,9 @@ impl DispatchValidateT for DarwiniaDispatchValidator { | RuntimeCall::MessageTransact(..) ) { Some(fp_evm::PrecompileFailure::Error { - exit_status: ExitError::Other("These pallet's calls are forbidden".into()), + exit_status: ExitError::Other( + "These pallet's calls are not allowed to be called from precompile.".into(), + ), }) } else { <() as DispatchValidateT>::validate_before_dispatch( diff --git a/runtime/pangoro/src/pallets/evm.rs b/runtime/pangoro/src/pallets/evm.rs index 5664306e0..4a767d59e 100644 --- a/runtime/pangoro/src/pallets/evm.rs +++ b/runtime/pangoro/src/pallets/evm.rs @@ -193,7 +193,9 @@ impl DispatchValidateT for DarwiniaDispatchValidator { | RuntimeCall::MessageTransact(..) ) { Some(fp_evm::PrecompileFailure::Error { - exit_status: ExitError::Other("These pallet's calls are forbidden".into()), + exit_status: ExitError::Other( + "These pallet's calls are not allowed to be called from precompile.".into(), + ), }) } else { <() as DispatchValidateT>::validate_before_dispatch( From 3002e444e970741c52b8ccb0dc0e0794f182033e Mon Sep 17 00:00:00 2001 From: bear Date: Thu, 31 Aug 2023 15:36:14 +0800 Subject: [PATCH 2/5] Update filter and test --- runtime/common/src/test.rs | 30 ++++++++++++++++++++++++++--- runtime/crab/src/pallets/evm.rs | 15 ++++++++++----- runtime/darwinia/src/pallets/evm.rs | 15 ++++++++++----- runtime/pangolin/src/pallets/evm.rs | 14 ++++++++++---- runtime/pangoro/src/pallets/evm.rs | 14 ++++++++++---- 5 files changed, 67 insertions(+), 21 deletions(-) diff --git a/runtime/common/src/test.rs b/runtime/common/src/test.rs index 106bc562e..31ea6f09f 100644 --- a/runtime/common/src/test.rs +++ b/runtime/common/src/test.rs @@ -486,17 +486,15 @@ macro_rules! impl_evm_tests { } #[test] - fn dispatch_validator_works() { + fn dispatch_validator_filter_runtime_calls() { ExtBuilder::default().build().execute_with(|| { assert!(DarwiniaDispatchValidator::validate_before_dispatch( - // normal account &H160::default().into(), &RuntimeCall::System(frame_system::Call::remark { remark: vec![] }) ) .is_none()); assert!(DarwiniaDispatchValidator::validate_before_dispatch( - // normal account &H160::default().into(), // forbidden call &RuntimeCall::EVM(pallet_evm::Call::call { @@ -514,6 +512,32 @@ macro_rules! impl_evm_tests { .is_some()); }); } + + #[test] + fn dispatch_validator_filter_dispatch_class() { + ExtBuilder::default().build().execute_with(|| { + // Default class + assert!(DarwiniaDispatchValidator::validate_before_dispatch( + &H160::default().into(), + &RuntimeCall::System(frame_system::Call::remark { remark: vec![] }) + ) + .is_none()); + + // Operational class + assert!(DarwiniaDispatchValidator::validate_before_dispatch( + &H160::default().into(), + &RuntimeCall::System(frame_system::Call::set_heap_pages { pages: 20 }) + ) + .is_none()); + + // Mandatory class + assert!(DarwiniaDispatchValidator::validate_before_dispatch( + &H160::default().into(), + &RuntimeCall::Timestamp(pallet_timestamp::Call::set { now: 100 }) + ) + .is_some()); + }); + } } }; } diff --git a/runtime/crab/src/pallets/evm.rs b/runtime/crab/src/pallets/evm.rs index 2310d1673..f86d4ce5e 100644 --- a/runtime/crab/src/pallets/evm.rs +++ b/runtime/crab/src/pallets/evm.rs @@ -18,6 +18,8 @@ // darwinia use crate::*; +// substrate +use frame_support::dispatch::{DispatchClass, GetDispatchInfo, Pays}; // frontier use pallet_evm::{ExitError, IsPrecompileResult, Precompile}; use pallet_evm_precompile_dispatch::DispatchValidateT; @@ -182,13 +184,14 @@ fn addr(a: u64) -> sp_core::H160 { pub struct DarwiniaDispatchValidator; impl DispatchValidateT for DarwiniaDispatchValidator { fn validate_before_dispatch( - origin: &AccountId, + _origin: &AccountId, call: &RuntimeCall, ) -> Option { + let info = call.get_dispatch_info(); + if matches!( call, RuntimeCall::Assets(..) - | RuntimeCall::Vesting(..) | RuntimeCall::Ethereum(..) | RuntimeCall::EVM(..) | RuntimeCall::MessageTransact(..) @@ -198,10 +201,12 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) + } else if info.pays_fee != Pays::Yes || info.class == DispatchClass::Mandatory { + Some(fp_evm::PrecompileFailure::Error { + exit_status: ExitError::Other("Permission denied calls".into()), + }) } else { - <() as DispatchValidateT>::validate_before_dispatch( - origin, call, - ) + None } } } diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index f51508082..3feb797d0 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -18,6 +18,8 @@ // darwinia use crate::*; +// substrate +use frame_support::dispatch::{DispatchClass, GetDispatchInfo, Pays}; // frontier use pallet_evm::{ExitError, IsPrecompileResult, Precompile}; use pallet_evm_precompile_dispatch::DispatchValidateT; @@ -181,13 +183,14 @@ fn addr(a: u64) -> sp_core::H160 { pub struct DarwiniaDispatchValidator; impl DispatchValidateT for DarwiniaDispatchValidator { fn validate_before_dispatch( - origin: &AccountId, + _origin: &AccountId, call: &RuntimeCall, ) -> Option { + let info = call.get_dispatch_info(); + if matches!( call, RuntimeCall::Assets(..) - | RuntimeCall::Vesting(..) | RuntimeCall::Ethereum(..) | RuntimeCall::EVM(..) | RuntimeCall::MessageTransact(..) @@ -197,10 +200,12 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) + } else if info.pays_fee != Pays::Yes || info.class == DispatchClass::Mandatory { + Some(fp_evm::PrecompileFailure::Error { + exit_status: ExitError::Other("Permission denied calls".into()), + }) } else { - <() as DispatchValidateT>::validate_before_dispatch( - origin, call, - ) + None } } } diff --git a/runtime/pangolin/src/pallets/evm.rs b/runtime/pangolin/src/pallets/evm.rs index 28c2c209c..f56c3525c 100644 --- a/runtime/pangolin/src/pallets/evm.rs +++ b/runtime/pangolin/src/pallets/evm.rs @@ -18,6 +18,8 @@ // darwinia use crate::*; +// substrate +use frame_support::dispatch::{DispatchClass, GetDispatchInfo, Pays}; // frontier use pallet_evm::{ExitError, IsPrecompileResult, Precompile}; use pallet_evm_precompile_dispatch::DispatchValidateT; @@ -184,9 +186,11 @@ fn addr(a: u64) -> sp_core::H160 { pub struct DarwiniaDispatchValidator; impl DispatchValidateT for DarwiniaDispatchValidator { fn validate_before_dispatch( - origin: &AccountId, + _origin: &AccountId, call: &RuntimeCall, ) -> Option { + let info = call.get_dispatch_info(); + if matches!( call, RuntimeCall::Assets(..) @@ -199,10 +203,12 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) + } else if info.pays_fee != Pays::Yes || info.class == DispatchClass::Mandatory { + Some(fp_evm::PrecompileFailure::Error { + exit_status: ExitError::Other("Permission denied calls".into()), + }) } else { - <() as DispatchValidateT>::validate_before_dispatch( - origin, call, - ) + None } } } diff --git a/runtime/pangoro/src/pallets/evm.rs b/runtime/pangoro/src/pallets/evm.rs index 4a767d59e..2df92cf78 100644 --- a/runtime/pangoro/src/pallets/evm.rs +++ b/runtime/pangoro/src/pallets/evm.rs @@ -18,6 +18,8 @@ // darwinia use crate::*; +// substrate +use frame_support::dispatch::{DispatchClass, GetDispatchInfo, Pays}; // frontier use pallet_evm::{ExitError, IsPrecompileResult, Precompile}; use pallet_evm_precompile_dispatch::DispatchValidateT; @@ -182,9 +184,11 @@ fn addr(a: u64) -> sp_core::H160 { pub struct DarwiniaDispatchValidator; impl DispatchValidateT for DarwiniaDispatchValidator { fn validate_before_dispatch( - origin: &AccountId, + _origin: &AccountId, call: &RuntimeCall, ) -> Option { + let info = call.get_dispatch_info(); + if matches!( call, RuntimeCall::Assets(..) @@ -197,10 +201,12 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) + } else if info.pays_fee != Pays::Yes || info.class == DispatchClass::Mandatory { + Some(fp_evm::PrecompileFailure::Error { + exit_status: ExitError::Other("Permission denied calls".into()), + }) } else { - <() as DispatchValidateT>::validate_before_dispatch( - origin, call, - ) + None } } } From c499c977ae38ffc30ab1b09aaaa83a8d2138ad6a Mon Sep 17 00:00:00 2001 From: bear Date: Fri, 1 Sep 2023 09:29:12 +0800 Subject: [PATCH 3/5] Add missing runtime call --- runtime/crab/src/pallets/evm.rs | 1 + runtime/darwinia/src/pallets/evm.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/runtime/crab/src/pallets/evm.rs b/runtime/crab/src/pallets/evm.rs index f86d4ce5e..bfda3576b 100644 --- a/runtime/crab/src/pallets/evm.rs +++ b/runtime/crab/src/pallets/evm.rs @@ -192,6 +192,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { if matches!( call, RuntimeCall::Assets(..) + | RuntimeCall::Vesting(..) | RuntimeCall::Ethereum(..) | RuntimeCall::EVM(..) | RuntimeCall::MessageTransact(..) diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 3feb797d0..35f6b586d 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -191,6 +191,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { if matches!( call, RuntimeCall::Assets(..) + | RuntimeCall::Vesting(..) | RuntimeCall::Ethereum(..) | RuntimeCall::EVM(..) | RuntimeCall::MessageTransact(..) From 2b68aee037e852ce406596d19253c0946398d53b Mon Sep 17 00:00:00 2001 From: bear Date: Fri, 1 Sep 2023 11:44:22 +0800 Subject: [PATCH 4/5] Fix review --- runtime/crab/src/pallets/evm.rs | 2 +- runtime/darwinia/src/pallets/evm.rs | 2 +- runtime/pangolin/src/pallets/evm.rs | 2 +- runtime/pangoro/src/pallets/evm.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/crab/src/pallets/evm.rs b/runtime/crab/src/pallets/evm.rs index bfda3576b..4e1e0b7b6 100644 --- a/runtime/crab/src/pallets/evm.rs +++ b/runtime/crab/src/pallets/evm.rs @@ -202,7 +202,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) - } else if info.pays_fee != Pays::Yes || info.class == DispatchClass::Mandatory { + } else if info.pays_fee == Pays::Yes || info.class == DispatchClass::Mandatory { Some(fp_evm::PrecompileFailure::Error { exit_status: ExitError::Other("Permission denied calls".into()), }) diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 35f6b586d..95a548a46 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -201,7 +201,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) - } else if info.pays_fee != Pays::Yes || info.class == DispatchClass::Mandatory { + } else if info.pays_fee == Pays::Yes || info.class == DispatchClass::Mandatory { Some(fp_evm::PrecompileFailure::Error { exit_status: ExitError::Other("Permission denied calls".into()), }) diff --git a/runtime/pangolin/src/pallets/evm.rs b/runtime/pangolin/src/pallets/evm.rs index f56c3525c..7aca4d104 100644 --- a/runtime/pangolin/src/pallets/evm.rs +++ b/runtime/pangolin/src/pallets/evm.rs @@ -203,7 +203,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) - } else if info.pays_fee != Pays::Yes || info.class == DispatchClass::Mandatory { + } else if info.pays_fee == Pays::Yes || info.class == DispatchClass::Mandatory { Some(fp_evm::PrecompileFailure::Error { exit_status: ExitError::Other("Permission denied calls".into()), }) diff --git a/runtime/pangoro/src/pallets/evm.rs b/runtime/pangoro/src/pallets/evm.rs index 2df92cf78..5b6634883 100644 --- a/runtime/pangoro/src/pallets/evm.rs +++ b/runtime/pangoro/src/pallets/evm.rs @@ -201,7 +201,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) - } else if info.pays_fee != Pays::Yes || info.class == DispatchClass::Mandatory { + } else if info.pays_fee == Pays::Yes || info.class == DispatchClass::Mandatory { Some(fp_evm::PrecompileFailure::Error { exit_status: ExitError::Other("Permission denied calls".into()), }) From 7e536cc712806e35a71c1553e80f4b176b2b6128 Mon Sep 17 00:00:00 2001 From: bear Date: Fri, 1 Sep 2023 11:45:32 +0800 Subject: [PATCH 5/5] Another commit --- runtime/crab/src/pallets/evm.rs | 2 +- runtime/darwinia/src/pallets/evm.rs | 2 +- runtime/pangolin/src/pallets/evm.rs | 2 +- runtime/pangoro/src/pallets/evm.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/crab/src/pallets/evm.rs b/runtime/crab/src/pallets/evm.rs index 4e1e0b7b6..15d61a13f 100644 --- a/runtime/crab/src/pallets/evm.rs +++ b/runtime/crab/src/pallets/evm.rs @@ -202,7 +202,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) - } else if info.pays_fee == Pays::Yes || info.class == DispatchClass::Mandatory { + } else if info.pays_fee == Pays::No || info.class == DispatchClass::Mandatory { Some(fp_evm::PrecompileFailure::Error { exit_status: ExitError::Other("Permission denied calls".into()), }) diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index 95a548a46..66ba7634f 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -201,7 +201,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) - } else if info.pays_fee == Pays::Yes || info.class == DispatchClass::Mandatory { + } else if info.pays_fee == Pays::No || info.class == DispatchClass::Mandatory { Some(fp_evm::PrecompileFailure::Error { exit_status: ExitError::Other("Permission denied calls".into()), }) diff --git a/runtime/pangolin/src/pallets/evm.rs b/runtime/pangolin/src/pallets/evm.rs index 7aca4d104..80b776499 100644 --- a/runtime/pangolin/src/pallets/evm.rs +++ b/runtime/pangolin/src/pallets/evm.rs @@ -203,7 +203,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) - } else if info.pays_fee == Pays::Yes || info.class == DispatchClass::Mandatory { + } else if info.pays_fee == Pays::No || info.class == DispatchClass::Mandatory { Some(fp_evm::PrecompileFailure::Error { exit_status: ExitError::Other("Permission denied calls".into()), }) diff --git a/runtime/pangoro/src/pallets/evm.rs b/runtime/pangoro/src/pallets/evm.rs index 5b6634883..e5f4c5cf8 100644 --- a/runtime/pangoro/src/pallets/evm.rs +++ b/runtime/pangoro/src/pallets/evm.rs @@ -201,7 +201,7 @@ impl DispatchValidateT for DarwiniaDispatchValidator { "These pallet's calls are not allowed to be called from precompile.".into(), ), }) - } else if info.pays_fee == Pays::Yes || info.class == DispatchClass::Mandatory { + } else if info.pays_fee == Pays::No || info.class == DispatchClass::Mandatory { Some(fp_evm::PrecompileFailure::Error { exit_status: ExitError::Other("Permission denied calls".into()), })