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

Add validator to dispatch precompile #1042

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 29 additions & 15 deletions frame/evm/precompile/dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,24 @@ use fp_evm::{
};
use frame_support::{
codec::{Decode, DecodeLimit as _},
dispatch::{DispatchClass, Dispatchable, GetDispatchInfo, Pays, PostDispatchInfo},
dispatch::{Dispatchable, GetDispatchInfo, Pays, PostDispatchInfo},
traits::{ConstU32, Get},
};
use pallet_evm::{AddressMapping, GasWeightMapping};

// `DecodeLimit` specifies the max depth a call can use when decoding, as unbounded depth
// can be used to overflow the stack.
// Default value is 8, which is the same as in XCM call decoding.
pub struct Dispatch<T, DecodeLimit = ConstU32<8>> {
_marker: PhantomData<(T, DecodeLimit)>,
pub struct Dispatch<T, DispatchFilter = (), DecodeLimit = ConstU32<8>> {
_marker: PhantomData<(T, DispatchFilter, DecodeLimit)>,
}

impl<T, DecodeLimit> Precompile for Dispatch<T, DecodeLimit>
impl<T, DispatchFilter, DecodeLimit> Precompile for Dispatch<T, DispatchFilter, DecodeLimit>
where
T: pallet_evm::Config,
T::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo + Decode,
<T::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<T::AccountId>>,
DispatchFilter: DispatchFilterT<T>,
DecodeLimit: Get<u32>,
{
fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
Expand All @@ -64,13 +65,6 @@ where
})?;
let info = call.get_dispatch_info();

let valid_call = info.pays_fee == Pays::Yes && info.class == DispatchClass::Normal;
if !valid_call {
return Err(PrecompileFailure::Error {
exit_status: ExitError::Other("invalid call".into()),
});
}

if let Some(gas) = target_gas {
let valid_weight =
info.weight.ref_time() <= T::GasWeightMapping::gas_to_weight(gas, false).ref_time();
Expand All @@ -83,13 +77,21 @@ where

let origin = T::AddressMapping::into_account_id(context.caller);

if !DispatchFilter::allow(&origin, &call) {
return Err(PrecompileFailure::Error {
exit_status: ExitError::Other("call not allowed".into()),
});
}

match call.dispatch(Some(origin).into()) {
Ok(post_info) => {
let cost = T::GasWeightMapping::weight_to_gas(
post_info.actual_weight.unwrap_or(info.weight),
);
if post_info.pays_fee(&info) == Pays::Yes {
let cost = T::GasWeightMapping::weight_to_gas(
post_info.actual_weight.unwrap_or(info.weight),
);

handle.record_cost(cost)?;
handle.record_cost(cost)?;
}

Ok(PrecompileOutput {
exit_status: ExitSucceed::Stopped,
Expand All @@ -104,3 +106,15 @@ where
}
}
}

/// Dispatch filter trait.
pub trait DispatchFilterT<T: pallet_evm::Config> {
fn allow(origin: &T::AccountId, call: &T::RuntimeCall) -> bool;
}

/// The default implementation of `DispatchFilterT`.
impl<T: pallet_evm::Config> DispatchFilterT<T> for () {
fn allow(_origin: &T::AccountId, _call: &T::RuntimeCall) -> bool {
true
boundless-forest marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion primitives/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl std::fmt::Display for AccountId20 {

acc
});
write!(f, "{}", checksum)
write!(f, "{checksum}")
}
}

Expand Down