Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dispatch filter #1252

Merged
merged 6 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}
Loading