Skip to content

Commit

Permalink
Implementation of dynamic extrinsic filter (#288)
Browse files Browse the repository at this point in the history
* initial commit of extrinsic-filter impl

* adjustment to TOML and runtime

* more comments and error types

* add mock.rs

* add tests

* add benchmarking and weights

* spaces -> tabs (why cargo fmt didn't work?)

* update comment

* add testcase set_mode_should_not_clear_blocked_extrinsics
  • Loading branch information
Kailai-Wang committed Jan 10, 2022
1 parent e38998a commit 2fc9f42
Show file tree
Hide file tree
Showing 10 changed files with 1,174 additions and 96 deletions.
180 changes: 99 additions & 81 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ panic = 'unwind'
[workspace]
members = [
'node',
'primitives',
'runtime',
'pallets/bridge',
'pallets/bridge-transfer',
'pallets/drop3',
]
'pallets/extrinsic-filter',
'primitives',
'runtime',
]
46 changes: 46 additions & 0 deletions pallets/extrinsic-filter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = 'pallet-extrinsic-filter'
authors = ['Litentry Dev']
description = 'Pallet for dynamically filtering extrinsics'
edition = '2021'
homepage = 'https://litentry.com/'
license = 'GPL-3.0'
repository = 'https://github.com/litentry/litentry-parachain'
version = '0.1.0'

[dependencies]
codec = { package = "parity-scale-codec", version = "2.2", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }

sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }

frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false, optional = true }

[dev-dependencies]
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.13" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }

[features]
default = ["std"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
std = [
"codec/std",
"sp-std/std",
"sp-runtime/std",
"sp-io/std",
"sp-core/std",
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
"pallet-balances/std",
"pallet-timestamp/std",
]
77 changes: 77 additions & 0 deletions pallets/extrinsic-filter/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2020-2022 Litentry Technologies GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

//! Benchmarking setup for pallet-extrinsic-filter

use super::*;

#[allow(unused)]
use crate::Pallet as ExtrinsicFilter;
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;
use sp_std::vec;

const MAX_BYTES: u32 = 1_024;

fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

benchmarks! {
block_extrinsics {
let p in 1 .. MAX_BYTES;
let f in 1 .. MAX_BYTES;

let pallet_name_bytes = vec![0u8; p as usize];
let function_name_bytes = vec![0u8; f as usize];
}: _(RawOrigin::Root, pallet_name_bytes.clone(), Some(function_name_bytes.clone()))
verify {
assert_eq!(
ExtrinsicFilter::<T>::blocked_extrinsics((pallet_name_bytes.clone(), function_name_bytes.clone())),
Some(())
);
assert_last_event::<T>(Event::ExtrinsicsBlocked {
pallet_name_bytes,
function_name_bytes: Some(function_name_bytes)
}.into());
}

unblock_extrinsics {
let p in 1 .. MAX_BYTES;
let f in 1 .. MAX_BYTES;

let pallet_name_bytes = vec![0u8; p as usize];
let function_name_bytes = vec![0u8; f as usize];
// block them
assert!(ExtrinsicFilter::<T>::block_extrinsics(
RawOrigin::Root.into(),
pallet_name_bytes.clone(),
Some(function_name_bytes.clone())
).is_ok());
}: _(RawOrigin::Root, pallet_name_bytes.clone(), Some(function_name_bytes.clone()))
verify {
assert_eq!(
ExtrinsicFilter::<T>::blocked_extrinsics((pallet_name_bytes.clone(), function_name_bytes.clone())),
None
);
assert_last_event::<T>(Event::ExtrinsicsUnblocked {
pallet_name_bytes,
function_name_bytes: Some(function_name_bytes)
}.into());
}
}

impl_benchmark_test_suite!(ExtrinsicFilter, crate::mock::new_test_ext(), crate::mock::Test,);

0 comments on commit 2fc9f42

Please sign in to comment.