Skip to content

Commit

Permalink
Allow operational call in EVM (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest authored Sep 1, 2023
1 parent edad973 commit 2f0941d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 23 deletions.
30 changes: 27 additions & 3 deletions runtime/common/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
});
}
}
};
}
Expand Down
18 changes: 13 additions & 5 deletions runtime/crab/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -182,9 +184,11 @@ fn addr(a: u64) -> sp_core::H160 {
pub struct DarwiniaDispatchValidator;
impl DispatchValidateT<AccountId, RuntimeCall> for DarwiniaDispatchValidator {
fn validate_before_dispatch(
origin: &AccountId,
_origin: &AccountId,
call: &RuntimeCall,
) -> Option<fp_evm::PrecompileFailure> {
let info = call.get_dispatch_info();

if matches!(
call,
RuntimeCall::Assets(..)
Expand All @@ -194,12 +198,16 @@ impl DispatchValidateT<AccountId, RuntimeCall> 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 if info.pays_fee == Pays::No || info.class == DispatchClass::Mandatory {
Some(fp_evm::PrecompileFailure::Error {
exit_status: ExitError::Other("Permission denied calls".into()),
})
} else {
<() as DispatchValidateT<AccountId, RuntimeCall>>::validate_before_dispatch(
origin, call,
)
None
}
}
}
18 changes: 13 additions & 5 deletions runtime/darwinia/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,9 +183,11 @@ fn addr(a: u64) -> sp_core::H160 {
pub struct DarwiniaDispatchValidator;
impl DispatchValidateT<AccountId, RuntimeCall> for DarwiniaDispatchValidator {
fn validate_before_dispatch(
origin: &AccountId,
_origin: &AccountId,
call: &RuntimeCall,
) -> Option<fp_evm::PrecompileFailure> {
let info = call.get_dispatch_info();

if matches!(
call,
RuntimeCall::Assets(..)
Expand All @@ -193,12 +197,16 @@ impl DispatchValidateT<AccountId, RuntimeCall> 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 if info.pays_fee == Pays::No || info.class == DispatchClass::Mandatory {
Some(fp_evm::PrecompileFailure::Error {
exit_status: ExitError::Other("Permission denied calls".into()),
})
} else {
<() as DispatchValidateT<AccountId, RuntimeCall>>::validate_before_dispatch(
origin, call,
)
None
}
}
}
18 changes: 13 additions & 5 deletions runtime/pangolin/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -184,9 +186,11 @@ fn addr(a: u64) -> sp_core::H160 {
pub struct DarwiniaDispatchValidator;
impl DispatchValidateT<AccountId, RuntimeCall> for DarwiniaDispatchValidator {
fn validate_before_dispatch(
origin: &AccountId,
_origin: &AccountId,
call: &RuntimeCall,
) -> Option<fp_evm::PrecompileFailure> {
let info = call.get_dispatch_info();

if matches!(
call,
RuntimeCall::Assets(..)
Expand All @@ -195,12 +199,16 @@ impl DispatchValidateT<AccountId, RuntimeCall> 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 if info.pays_fee == Pays::No || info.class == DispatchClass::Mandatory {
Some(fp_evm::PrecompileFailure::Error {
exit_status: ExitError::Other("Permission denied calls".into()),
})
} else {
<() as DispatchValidateT<AccountId, RuntimeCall>>::validate_before_dispatch(
origin, call,
)
None
}
}
}
18 changes: 13 additions & 5 deletions runtime/pangoro/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -182,9 +184,11 @@ fn addr(a: u64) -> sp_core::H160 {
pub struct DarwiniaDispatchValidator;
impl DispatchValidateT<AccountId, RuntimeCall> for DarwiniaDispatchValidator {
fn validate_before_dispatch(
origin: &AccountId,
_origin: &AccountId,
call: &RuntimeCall,
) -> Option<fp_evm::PrecompileFailure> {
let info = call.get_dispatch_info();

if matches!(
call,
RuntimeCall::Assets(..)
Expand All @@ -193,12 +197,16 @@ impl DispatchValidateT<AccountId, RuntimeCall> 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 if info.pays_fee == Pays::No || info.class == DispatchClass::Mandatory {
Some(fp_evm::PrecompileFailure::Error {
exit_status: ExitError::Other("Permission denied calls".into()),
})
} else {
<() as DispatchValidateT<AccountId, RuntimeCall>>::validate_before_dispatch(
origin, call,
)
None
}
}
}

0 comments on commit 2f0941d

Please sign in to comment.