Skip to content

Commit

Permalink
remove BlockAll and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
gitofdeepanshu committed Sep 21, 2023
1 parent 74651c1 commit 69b6fc0
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 135 deletions.
31 changes: 0 additions & 31 deletions primitives/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@ use frame_support::{
};
use pallet_evm_precompile_dispatch::DispatchValidateT;

/// Struct that blocks all runtime calls to Dispatch Precompile
pub struct BlockAllDispatchValidate;

/// The default implementation of `DispatchValidateT`.
impl<AccountId, RuntimeCall> DispatchValidateT<AccountId, RuntimeCall>
for BlockAllDispatchValidate
{
fn validate_before_dispatch(
_origin: &AccountId,
_call: &RuntimeCall,
) -> Option<PrecompileFailure> {
Some(PrecompileFailure::Error {
exit_status: ExitError::Other("invalid call".into()),
})
}
}

/// Struct that allows only whitelisted runtime calls to pass through dispatch precompile,
/// Whitelisted calls are defined in runtime
pub struct DispatchFilterValidate<RuntimeCall, Filter: InstanceFilter<RuntimeCall> + Default>(
Expand Down Expand Up @@ -71,17 +54,3 @@ impl<AccountId, RuntimeCall: GetDispatchInfo, Filter: InstanceFilter<RuntimeCall
}
}
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn all_non_whitelisted_call_should_fail() {
assert_eq!(
BlockAllDispatchValidate::validate_before_dispatch(&(), &()).unwrap(),
PrecompileFailure::Error {
exit_status: ExitError::Other("invalid call".into()),
}
)
}
}
26 changes: 1 addition & 25 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]

pub use crate::precompiles::DispatchPrecompileFilter;
pub use astar_primitives::{
evm::EvmRevertCodeHandler, xcm::AssetLocationIdConverter, AccountId, Address, AssetId, Balance,
BlockNumber, Hash, Header, Index, Signature,
Expand Down Expand Up @@ -844,31 +845,6 @@ impl pallet_xc_asset_config::Config for Runtime {
type WeightInfo = pallet_xc_asset_config::weights::SubstrateWeight<Self>;
}

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
#[derive(Default)]
pub struct DispatchPrecompileFilter;

impl InstanceFilter<RuntimeCall> for DispatchPrecompileFilter {
fn filter(&self, c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
for call in calls {
if !DispatchPrecompileFilter::default().filter(call) {
return false;
}
}
true
}
RuntimeCall::DappsStaking(_) => true,
_ => false,
}
}
fn is_superset(&self, _o: &Self) -> bool {
false
}
}

/// The type used to represent the kinds of proxying allowed.
#[derive(
Copy,
Expand Down
27 changes: 26 additions & 1 deletion runtime/astar/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

//! The Astar Network EVM precompiles. This can be compiled with ``#[no_std]`, ready for Wasm.
use crate::{DispatchPrecompileFilter, RuntimeCall};
use crate::RuntimeCall;
use astar_primitives::precompiles::DispatchFilterValidate;
use frame_support::traits::InstanceFilter;
use pallet_evm::{
ExitRevert, IsPrecompileResult, Precompile, PrecompileFailure, PrecompileHandle,
PrecompileResult, PrecompileSet,
Expand Down Expand Up @@ -47,6 +48,30 @@ use xcm::latest::prelude::MultiLocation;
/// to Erc20AssetsPrecompileSet
pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
#[derive(Default)]
pub struct DispatchPrecompileFilter;

impl InstanceFilter<RuntimeCall> for DispatchPrecompileFilter {
fn filter(&self, c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
for call in calls {
if !DispatchPrecompileFilter::default().filter(call) {
return false;
}
}
true
}
RuntimeCall::DappsStaking(_) => true,
_ => false,
}
}
fn is_superset(&self, _o: &Self) -> bool {
false
}
}
/// The PrecompileSet installed in the Astar runtime.
#[derive(Debug, Default, Clone, Copy)]
pub struct AstarNetworkPrecompiles<R, C>(PhantomData<(R, C)>);
Expand Down
26 changes: 1 addition & 25 deletions runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub use astar_primitives::{
Index, Signature,
};

pub use crate::precompiles::DispatchPrecompileFilter;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
Expand Down Expand Up @@ -845,31 +846,6 @@ impl pallet_sudo::Config for Runtime {
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
}

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
#[derive(Default)]
pub struct DispatchPrecompileFilter;

impl InstanceFilter<RuntimeCall> for DispatchPrecompileFilter {
fn filter(&self, c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
for call in calls {
if !DispatchPrecompileFilter::default().filter(call) {
return false;
}
}
true
}
RuntimeCall::DappsStaking(_) => true,
_ => false,
}
}
fn is_superset(&self, _o: &Self) -> bool {
false
}
}

/// The type used to represent the kinds of proxying allowed.
#[derive(
Copy,
Expand Down
28 changes: 27 additions & 1 deletion runtime/local/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

//! The Local EVM precompiles. This can be compiled with ``#[no_std]`, ready for Wasm.
use crate::{DispatchPrecompileFilter, RuntimeCall};
use crate::RuntimeCall;
use astar_primitives::precompiles::DispatchFilterValidate;
use frame_support::traits::InstanceFilter;
use pallet_evm::{
ExitRevert, IsPrecompileResult, Precompile, PrecompileFailure, PrecompileHandle,
PrecompileResult, PrecompileSet,
Expand All @@ -45,6 +46,31 @@ use sp_std::marker::PhantomData;
/// to Erc20AssetsPrecompileSet
pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
#[derive(Default)]
pub struct DispatchPrecompileFilter;

impl InstanceFilter<RuntimeCall> for DispatchPrecompileFilter {
fn filter(&self, c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
for call in calls {
if !DispatchPrecompileFilter::default().filter(call) {
return false;
}
}
true
}
RuntimeCall::DappsStaking(_) => true,
_ => false,
}
}
fn is_superset(&self, _o: &Self) -> bool {
false
}
}

/// The PrecompileSet installed in the Local runtime.
#[derive(Debug, Default, Clone, Copy)]
pub struct LocalNetworkPrecompiles<R>(PhantomData<R>);
Expand Down
27 changes: 2 additions & 25 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub use astar_primitives::{
Index, Signature,
};

pub use crate::precompiles::DispatchPrecompileFilter;

use pallet_evm_precompile_assets_erc20::AddressToAssetId;

#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -1053,31 +1055,6 @@ impl pallet_sudo::Config for Runtime {
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
}

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
#[derive(Default)]
pub struct DispatchPrecompileFilter;

impl InstanceFilter<RuntimeCall> for DispatchPrecompileFilter {
fn filter(&self, c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
for call in calls {
if !DispatchPrecompileFilter::default().filter(call) {
return false;
}
}
true
}
RuntimeCall::DappsStaking(_) => true,
_ => false,
}
}
fn is_superset(&self, _o: &Self) -> bool {
false
}
}

parameter_types! {
// One storage item; key size 32, value size 8.
pub const ProxyDepositBase: Balance = deposit(1, 8);
Expand Down
28 changes: 27 additions & 1 deletion runtime/shibuya/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

//! The Shibuya Network EVM precompiles. This can be compiled with ``#[no_std]`, ready for Wasm.
use crate::{DispatchPrecompileFilter, RuntimeCall};
use crate::RuntimeCall;
use astar_primitives::precompiles::DispatchFilterValidate;
use frame_support::traits::InstanceFilter;
use pallet_evm::{
ExitRevert, IsPrecompileResult, Precompile, PrecompileFailure, PrecompileHandle,
PrecompileResult, PrecompileSet,
Expand Down Expand Up @@ -48,6 +49,31 @@ use xcm::latest::prelude::MultiLocation;
/// to Erc20AssetsPrecompileSet
pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
#[derive(Default)]
pub struct DispatchPrecompileFilter;

impl InstanceFilter<RuntimeCall> for DispatchPrecompileFilter {
fn filter(&self, c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
for call in calls {
if !DispatchPrecompileFilter::default().filter(call) {
return false;
}
}
true
}
RuntimeCall::DappsStaking(_) => true,
_ => false,
}
}
fn is_superset(&self, _o: &Self) -> bool {
false
}
}

/// The PrecompileSet installed in the Shiden runtime.
#[derive(Debug, Default, Clone, Copy)]
pub struct ShibuyaNetworkPrecompiles<R, C>(PhantomData<(R, C)>);
Expand Down
26 changes: 1 addition & 25 deletions runtime/shiden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use sp_runtime::{
};
use sp_std::prelude::*;

pub use crate::precompiles::DispatchPrecompileFilter;
pub use astar_primitives::{
evm::EvmRevertCodeHandler, xcm::AssetLocationIdConverter, AccountId, Address, AssetId, Balance,
BlockNumber, Hash, Header, Index, Signature,
Expand Down Expand Up @@ -832,31 +833,6 @@ impl pallet_xc_asset_config::Config for Runtime {
type WeightInfo = pallet_xc_asset_config::weights::SubstrateWeight<Self>;
}

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
#[derive(Default)]
pub struct DispatchPrecompileFilter;

impl InstanceFilter<RuntimeCall> for DispatchPrecompileFilter {
fn filter(&self, c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
for call in calls {
if !DispatchPrecompileFilter::default().filter(call) {
return false;
}
}
true
}
RuntimeCall::DappsStaking(_) => true,
_ => false,
}
}
fn is_superset(&self, _o: &Self) -> bool {
false
}
}

/// The type used to represent the kinds of proxying allowed.
#[derive(
Copy,
Expand Down
28 changes: 27 additions & 1 deletion runtime/shiden/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

//! The Shiden Network EVM precompiles. This can be compiled with ``#[no_std]`, ready for Wasm.
use crate::{DispatchPrecompileFilter, RuntimeCall};
use crate::RuntimeCall;
use astar_primitives::precompiles::DispatchFilterValidate;
use frame_support::traits::InstanceFilter;
use pallet_evm::{
ExitRevert, IsPrecompileResult, Precompile, PrecompileFailure, PrecompileHandle,
PrecompileResult, PrecompileSet,
Expand Down Expand Up @@ -47,6 +48,31 @@ use xcm::latest::prelude::MultiLocation;
/// to Erc20AssetsPrecompileSet
pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
#[derive(Default)]
pub struct DispatchPrecompileFilter;

impl InstanceFilter<RuntimeCall> for DispatchPrecompileFilter {
fn filter(&self, c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
for call in calls {
if !DispatchPrecompileFilter::default().filter(call) {
return false;
}
}
true
}
RuntimeCall::DappsStaking(_) => true,
_ => false,
}
}
fn is_superset(&self, _o: &Self) -> bool {
false
}
}

/// The PrecompileSet installed in the Shiden runtime.
#[derive(Debug, Default, Clone, Copy)]
pub struct ShidenNetworkPrecompiles<R, C>(PhantomData<(R, C)>);
Expand Down

0 comments on commit 69b6fc0

Please sign in to comment.