Skip to content

Commit

Permalink
Add DispatchInfo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitofdeepanshu committed Sep 25, 2023
1 parent 23c8a46 commit 6ca2376
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions primitives/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use frame_support::{
};
use pallet_evm_precompile_dispatch::DispatchValidateT;

/// Struct that allows only whitelisted runtime calls to pass through dispatch precompile,
/// Whitelisted calls are defined in runtime
/// Struct that allows only calls based on `Filter` to pass through.
pub struct DispatchFilterValidate<RuntimeCall, Filter: Contains<RuntimeCall>>(
PhantomData<(RuntimeCall, Filter)>,
);
Expand Down
1 change: 1 addition & 0 deletions tests/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sp-runtime = { workspace = true }
# astar dependencies
pallet-ethereum-checked = { workspace = true }
pallet-evm-precompile-assets-erc20 = { workspace = true }
pallet-evm-precompile-dispatch = { workspace = true }
precompile-utils = { workspace = true }

astar-primitives = { workspace = true }
Expand Down
95 changes: 94 additions & 1 deletion tests/integration/src/dispatch_precompile_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
#![cfg(test)]

use crate::setup::*;
use frame_support::traits::Contains;
use astar_primitives::precompiles::DispatchFilterValidate;
use fp_evm::{ExitError, PrecompileFailure};
use frame_support::{
dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays},
traits::Contains,
};
use pallet_evm_precompile_dispatch::DispatchValidateT;

/// Whitelisted Calls are defined in the runtime
#[test]
Expand Down Expand Up @@ -106,3 +112,90 @@ fn filter_accepts_whitelisted_batch_all_calls() {
assert!(WhitelistedCalls::contains(&call));
});
}

#[test]
fn test_correct_dispatch_info_works() {
ExtBuilder::default().build().execute_with(|| {
// Mock implementation
struct Filter;
struct AccountId;
enum RuntimeCall {
System,
DappsStaking,
}
impl GetDispatchInfo for RuntimeCall {
fn get_dispatch_info(&self) -> DispatchInfo {
// Default is Pays::Yes and DispatchCall::Normal
DispatchInfo::default()
}
}
impl Contains<RuntimeCall> for Filter {
fn contains(t: &RuntimeCall) -> bool {
match t {
RuntimeCall::DappsStaking => true,
_ => false,
}
}
}
// Case 1: Whitelisted Call with correct Dispatch info
assert_eq!(
DispatchFilterValidate::<RuntimeCall, Filter>::validate_before_dispatch(
&AccountId,
&RuntimeCall::DappsStaking
),
Option::None
);
// Case 2: Non-Whitelisted Call with correct Dispatch Info
assert_eq!(
DispatchFilterValidate::<RuntimeCall, Filter>::validate_before_dispatch(
&AccountId,
&RuntimeCall::System
),
Option::Some(PrecompileFailure::Error {
exit_status: ExitError::Other("call filtered out".into()),
})
);
});
}

#[test]
fn test_incorrect_dispatch_info_fails() {
ExtBuilder::default().build().execute_with(|| {
// Mock implementation
struct Filter;
struct AccountId;
enum RuntimeCall {
System,
DappsStaking,
}
impl GetDispatchInfo for RuntimeCall {
fn get_dispatch_info(&self) -> DispatchInfo {
DispatchInfo {
weight: Weight::default(),
class: DispatchClass::Normal,
// Should have been Pays::Yes for call to pass
pays_fee: Pays::No,
}
}
}
impl Contains<RuntimeCall> for Filter {
fn contains(t: &RuntimeCall) -> bool {
match t {
RuntimeCall::DappsStaking => true,
_ => false,
}
}
}

// WhiteListed Call fails because of incorrect DispatchInfo
assert_eq!(
DispatchFilterValidate::<RuntimeCall, Filter>::validate_before_dispatch(
&AccountId,
&RuntimeCall::DappsStaking
),
Option::Some(PrecompileFailure::Error {
exit_status: ExitError::Other("invalid call".into()),
})
);
})
}

0 comments on commit 6ca2376

Please sign in to comment.