new pallet: whitelist pallet (#10159)

* pallet whitelist

* refactor a bit

* fmt

* address audit

* improve tests

* return Ok + refund

* add test for dispatching failing

* add dispatch_whitelisted_call_with_preimage

* fmt

* better name

* Consume all data on decode

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add error docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Remove phantom data

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Use rust 2021

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update crate features

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Make compile

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* cargo run --quiet --profile=production  --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_whitelist --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/whitelist/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* Bump Preimage max size

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* cargo run --quiet --profile=production  --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_whitelist --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/whitelist/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* Fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Parity Bot <admin@parity.io>
This commit is contained in:
Guillaume Thiolliere
2022-03-01 14:31:51 +01:00
committed by GitHub
parent 5ae3ed3b95
commit e71c7b259d
13 changed files with 873 additions and 3 deletions
+19
View File
@@ -5019,6 +5019,7 @@ dependencies = [
"pallet-uniques",
"pallet-utility",
"pallet-vesting",
"pallet-whitelist",
"parity-scale-codec",
"scale-info",
"sp-api",
@@ -6615,6 +6616,24 @@ dependencies = [
"sp-std",
]
[[package]]
name = "pallet-whitelist"
version = "4.0.0-dev"
dependencies = [
"frame-benchmarking",
"frame-support",
"frame-system",
"pallet-balances",
"pallet-preimage",
"parity-scale-codec",
"scale-info",
"sp-api",
"sp-core",
"sp-io",
"sp-runtime",
"sp-std",
]
[[package]]
name = "parity-db"
version = "0.3.5"
+1
View File
@@ -138,6 +138,7 @@ members = [
"frame/uniques",
"frame/utility",
"frame/vesting",
"frame/whitelist",
"primitives/api",
"primitives/api/proc-macro",
"primitives/api/test",
+3
View File
@@ -100,6 +100,7 @@ pallet-asset-tx-payment = { version = "4.0.0-dev", default-features = false, pat
pallet-transaction-storage = { version = "4.0.0-dev", default-features = false, path = "../../../frame/transaction-storage" }
pallet-uniques = { version = "4.0.0-dev", default-features = false, path = "../../../frame/uniques" }
pallet-vesting = { version = "4.0.0-dev", default-features = false, path = "../../../frame/vesting" }
pallet-whitelist = { version = "4.0.0-dev", default-features = false, path = "../../../frame/whitelist" }
[build-dependencies]
substrate-wasm-builder = { version = "5.0.0-dev", path = "../../../utils/wasm-builder" }
@@ -220,6 +221,7 @@ runtime-benchmarks = [
"pallet-utility/runtime-benchmarks",
"pallet-uniques/runtime-benchmarks",
"pallet-vesting/runtime-benchmarks",
"pallet-whitelist/runtime-benchmarks",
"frame-system-benchmarking",
"hex-literal",
]
@@ -267,6 +269,7 @@ try-runtime = [
"pallet-uniques/try-runtime",
"pallet-utility/try-runtime",
"pallet-vesting/try-runtime",
"pallet-whitelist/try-runtime",
]
# Make contract callable functions marked as __unstable__ available. Do not enable
# on live chains as those are subject to change.
+11
View File
@@ -1357,6 +1357,15 @@ impl pallet_transaction_storage::Config for Runtime {
type WeightInfo = pallet_transaction_storage::weights::SubstrateWeight<Runtime>;
}
impl pallet_whitelist::Config for Runtime {
type Event = Event;
type Call = Call;
type WhitelistOrigin = EnsureRoot<AccountId>;
type DispatchWhitelistedOrigin = EnsureRoot<AccountId>;
type PreimageProvider = Preimage;
type WeightInfo = pallet_whitelist::weights::SubstrateWeight<Runtime>;
}
construct_runtime!(
pub enum Runtime where
Block = Block,
@@ -1411,6 +1420,7 @@ construct_runtime!(
ChildBounties: pallet_child_bounties,
Referenda: pallet_referenda,
ConvictionVoting: pallet_conviction_voting,
Whitelist: pallet_whitelist,
}
);
@@ -1509,6 +1519,7 @@ mod benches {
[pallet_uniques, Uniques]
[pallet_utility, Utility]
[pallet_vesting, Vesting]
[pallet_whitelist, Whitelist]
);
}
+1 -1
View File
@@ -2,7 +2,7 @@
name = "pallet-conviction-voting"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
+1 -1
View File
@@ -2,7 +2,7 @@
name = "pallet-referenda"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
+47
View File
@@ -0,0 +1,47 @@
[package]
name = "pallet-whitelist"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME pallet for whitelisting call, and dispatch from specific origin"
readme = "README.md"
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.0", default-features = false, features = ["derive"] }
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" }
sp-std = { version = "4.0.0", default-features = false, path = "../../primitives/std" }
sp-io = { version = "5.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "5.0.0", default-features = false, path = "../../primitives/runtime" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../benchmarking", optional = true }
[dev-dependencies]
sp-core = { version = "5.0.0", path = "../../primitives/core" }
pallet-preimage = { version = "4.0.0-dev", path = "../preimage/" }
pallet-balances = { version = "4.0.0-dev", path = "../balances/" }
[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"sp-std/std",
"sp-io/std",
"sp-runtime/std",
"frame-support/std",
"frame-system/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
@@ -0,0 +1,120 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Whitelist pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use core::convert::TryInto;
use frame_benchmarking::benchmarks;
use frame_support::{ensure, traits::PreimageRecipient};
use sp_runtime::traits::Hash;
#[cfg(test)]
use crate::Pallet as Whitelist;
benchmarks! {
whitelist_call {
let origin = T::WhitelistOrigin::successful_origin();
let call_hash = Default::default();
}: _<T::Origin>(origin, call_hash)
verify {
ensure!(
WhitelistedCall::<T>::contains_key(call_hash),
"call not whitelisted"
);
ensure!(
T::PreimageProvider::preimage_requested(&call_hash),
"preimage not requested"
);
}
remove_whitelisted_call {
let origin = T::WhitelistOrigin::successful_origin();
let call_hash = Default::default();
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");
}: _<T::Origin>(origin, call_hash)
verify {
ensure!(
!WhitelistedCall::<T>::contains_key(call_hash),
"whitelist not removed"
);
ensure!(
!T::PreimageProvider::preimage_requested(&call_hash),
"preimage still requested"
);
}
// We benchmark with the maximum possible size for a call.
// If the resulting weight is too big, maybe it worth having a weight which depends
// on the size of the call, with a new witness in parameter.
dispatch_whitelisted_call {
let origin = T::DispatchWhitelistedOrigin::successful_origin();
// NOTE: we remove `10` because we need some bytes to encode the variants and vec length
let remark_len = <T::PreimageProvider as PreimageRecipient<_>>::MaxSize::get() - 10;
let remark = sp_std::vec![1_8; remark_len as usize];
let call: <T as Config>::Call = frame_system::Call::remark { remark }.into();
let call_weight = call.get_dispatch_info().weight;
let encoded_call = call.encode();
let call_hash = T::Hashing::hash(&encoded_call[..]);
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");
let encoded_call = encoded_call.try_into().expect("encoded_call must be small enough");
T::PreimageProvider::note_preimage(encoded_call);
}: _<T::Origin>(origin, call_hash, call_weight)
verify {
ensure!(
!WhitelistedCall::<T>::contains_key(call_hash),
"whitelist not removed"
);
ensure!(
!T::PreimageProvider::preimage_requested(&call_hash),
"preimage still requested"
);
}
dispatch_whitelisted_call_with_preimage {
let n in 1 .. 10_000;
let origin = T::DispatchWhitelistedOrigin::successful_origin();
let remark = sp_std::vec![1u8; n as usize];
let call: <T as Config>::Call = frame_system::Call::remark { remark }.into();
let call_hash = T::Hashing::hash_of(&call);
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");
}: _<T::Origin>(origin, Box::new(call))
verify {
ensure!(
!WhitelistedCall::<T>::contains_key(call_hash),
"whitelist not removed"
);
ensure!(
!T::PreimageProvider::preimage_requested(&call_hash),
"preimage still requested"
);
}
impl_benchmark_test_suite!(Whitelist, crate::mock::new_test_ext(), crate::mock::Test);
}
+244
View File
@@ -0,0 +1,244 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Whitelist Pallet
//!
//! - [`Config`]
//! - [`Call`]
//!
//! ## Overview
//!
//! Allow some configurable origin: [`Config::WhitelistOrigin`] to whitelist some hash of a call,
//! and allow another configurable origin: [`Config::DispatchWhitelistedOrigin`] to dispatch them
//! with the root origin.
//!
//! In the meantime the call corresponding to the hash must have been submitted to the to the
//! pre-image handler [`PreimageProvider`].
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;
use sp_runtime::traits::Dispatchable;
use sp_std::prelude::*;
use codec::{Decode, DecodeLimit, Encode, FullCodec, MaxEncodedLen};
use frame_support::{
ensure,
traits::{PreimageProvider, PreimageRecipient},
weights::{GetDispatchInfo, PostDispatchInfo},
};
use scale_info::TypeInfo;
use sp_api::HashT;
use weights::WeightInfo;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub use pallet::*;
#[derive(Clone, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct Preimage<BoundedVec, Balance, AccountId> {
preimage: BoundedVec,
deposit: Option<(AccountId, Balance)>,
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// The overarching call type.
type Call: IsType<<Self as frame_system::Config>::Call>
+ Dispatchable<Origin = Self::Origin, PostInfo = PostDispatchInfo>
+ GetDispatchInfo
+ FullCodec
+ TypeInfo
+ From<frame_system::Call<Self>>
+ Parameter;
/// Required origin for whitelisting a call.
type WhitelistOrigin: EnsureOrigin<Self::Origin>;
/// Required origin for dispatching whitelisted call with root origin.
type DispatchWhitelistedOrigin: EnsureOrigin<Self::Origin>;
/// The handler of pre-images.
// NOTE: recipient is only needed for benchmarks.
type PreimageProvider: PreimageProvider<Self::Hash> + PreimageRecipient<Self::Hash>;
/// The weight information for this pallet.
type WeightInfo: weights::WeightInfo;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
CallWhitelisted { call_hash: T::Hash },
WhitelistedCallRemoved { call_hash: T::Hash },
WhitelistedCallDispatched { call_hash: T::Hash, result: DispatchResultWithPostInfo },
}
#[pallet::error]
pub enum Error<T> {
/// The preimage of the call hash could not be loaded.
UnavailablePreImage,
/// The call could not be decoded.
UndecodableCall,
/// The weight of the decoded call was higher than the witness.
InvalidCallWeightWitness,
/// The call was not whitelisted.
CallIsNotWhitelisted,
/// The call was already whitelisted; No-Op.
CallAlreadyWhitelisted,
}
#[pallet::storage]
pub type WhitelistedCall<T: Config> = StorageMap<_, Twox64Concat, T::Hash, (), OptionQuery>;
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::WeightInfo::whitelist_call())]
pub fn whitelist_call(origin: OriginFor<T>, call_hash: T::Hash) -> DispatchResult {
T::WhitelistOrigin::ensure_origin(origin)?;
ensure!(
!WhitelistedCall::<T>::contains_key(call_hash),
Error::<T>::CallAlreadyWhitelisted,
);
WhitelistedCall::<T>::insert(call_hash, ());
T::PreimageProvider::request_preimage(&call_hash);
Self::deposit_event(Event::<T>::CallWhitelisted { call_hash });
Ok(())
}
#[pallet::weight(T::WeightInfo::remove_whitelisted_call())]
pub fn remove_whitelisted_call(origin: OriginFor<T>, call_hash: T::Hash) -> DispatchResult {
T::WhitelistOrigin::ensure_origin(origin)?;
WhitelistedCall::<T>::take(call_hash).ok_or(Error::<T>::CallIsNotWhitelisted)?;
T::PreimageProvider::unrequest_preimage(&call_hash);
Self::deposit_event(Event::<T>::WhitelistedCallRemoved { call_hash });
Ok(())
}
#[pallet::weight(
T::WeightInfo::dispatch_whitelisted_call().saturating_add(*call_weight_witness)
)]
pub fn dispatch_whitelisted_call(
origin: OriginFor<T>,
call_hash: T::Hash,
call_weight_witness: Weight,
) -> DispatchResultWithPostInfo {
T::DispatchWhitelistedOrigin::ensure_origin(origin)?;
ensure!(
WhitelistedCall::<T>::contains_key(call_hash),
Error::<T>::CallIsNotWhitelisted,
);
let call = T::PreimageProvider::get_preimage(&call_hash)
.ok_or(Error::<T>::UnavailablePreImage)?;
let call = <T as Config>::Call::decode_all_with_depth_limit(
sp_api::MAX_EXTRINSIC_DEPTH,
&mut &call[..],
)
.map_err(|_| Error::<T>::UndecodableCall)?;
ensure!(
call.get_dispatch_info().weight <= call_weight_witness,
Error::<T>::InvalidCallWeightWitness
);
let actual_weight = Self::clean_and_dispatch(call_hash, call)
.map(|w| w.saturating_add(T::WeightInfo::dispatch_whitelisted_call()));
Ok(actual_weight.into())
}
#[pallet::weight({
let call_weight = call.get_dispatch_info().weight;
let call_len = call.encoded_size() as u32;
T::WeightInfo::dispatch_whitelisted_call_with_preimage(call_len)
.saturating_add(call_weight)
})]
pub fn dispatch_whitelisted_call_with_preimage(
origin: OriginFor<T>,
call: Box<<T as Config>::Call>,
) -> DispatchResultWithPostInfo {
T::DispatchWhitelistedOrigin::ensure_origin(origin)?;
let call_hash = <T as frame_system::Config>::Hashing::hash_of(&call);
ensure!(
WhitelistedCall::<T>::contains_key(call_hash),
Error::<T>::CallIsNotWhitelisted,
);
let call_len = call.encoded_size() as u32;
let actual_weight = Self::clean_and_dispatch(call_hash, *call).map(|w| {
w.saturating_add(T::WeightInfo::dispatch_whitelisted_call_with_preimage(call_len))
});
Ok(actual_weight.into())
}
}
}
impl<T: Config> Pallet<T> {
/// Clean whitelisting/preimage and dispatch call.
///
/// Return the call actual weight of the dispatched call if there is some.
fn clean_and_dispatch(call_hash: T::Hash, call: <T as Config>::Call) -> Option<Weight> {
WhitelistedCall::<T>::remove(call_hash);
T::PreimageProvider::unrequest_preimage(&call_hash);
let result = call.dispatch(frame_system::Origin::<T>::Root.into());
let call_actual_weight = match result {
Ok(call_post_info) => call_post_info.actual_weight,
Err(call_err) => call_err.post_info.actual_weight,
};
Self::deposit_event(Event::<T>::WhitelistedCallDispatched { call_hash, result });
call_actual_weight
}
}
+124
View File
@@ -0,0 +1,124 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Mock for Whitelist Pallet
#![cfg(test)]
use crate as pallet_whitelist;
use frame_support::{
parameter_types,
traits::{ConstU32, ConstU64, Nothing},
};
use frame_system::EnsureRoot;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system,
Balances: pallet_balances,
Whitelist: pallet_whitelist,
Preimage: pallet_preimage,
}
);
frame_support::parameter_types! {
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(1024);
}
impl frame_system::Config for Test {
type BaseCallFilter = Nothing;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Call = Call;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}
impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type Balance = u64;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ConstU64<1>;
type AccountStore = System;
type WeightInfo = ();
}
parameter_types! {
// Taken from Polkadot as reference.
pub const PreimageMaxSize: u32 = 4096 * 1024;
}
impl pallet_preimage::Config for Test {
type Event = Event;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<Self::AccountId>;
type MaxSize = PreimageMaxSize;
type BaseDeposit = ConstU64<1>;
type ByteDeposit = ConstU64<1>;
type WeightInfo = ();
}
impl pallet_whitelist::Config for Test {
type Event = Event;
type Call = Call;
type WhitelistOrigin = EnsureRoot<Self::AccountId>;
type DispatchWhitelistedOrigin = EnsureRoot<Self::AccountId>;
type PreimageProvider = Preimage;
type WeightInfo = ();
}
pub fn new_test_ext() -> sp_io::TestExternalities {
let t = GenesisConfig::default().build_storage().unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
+175
View File
@@ -0,0 +1,175 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Tests for Whitelist Pallet
use crate::mock::*;
use codec::Encode;
use frame_support::{assert_noop, assert_ok, dispatch::GetDispatchInfo, traits::PreimageProvider};
use sp_runtime::{traits::Hash, DispatchError};
#[test]
fn test_whitelist_call_and_remove() {
new_test_ext().execute_with(|| {
let call = Call::System(frame_system::Call::remark { remark: vec![] });
let encoded_call = call.encode();
let call_hash = <Test as frame_system::Config>::Hashing::hash(&encoded_call[..]);
assert_noop!(
Whitelist::remove_whitelisted_call(Origin::root(), call_hash),
crate::Error::<Test>::CallIsNotWhitelisted,
);
assert_noop!(
Whitelist::whitelist_call(Origin::signed(1), call_hash),
DispatchError::BadOrigin,
);
assert_ok!(Whitelist::whitelist_call(Origin::root(), call_hash));
assert!(Preimage::preimage_requested(&call_hash));
assert_noop!(
Whitelist::whitelist_call(Origin::root(), call_hash),
crate::Error::<Test>::CallAlreadyWhitelisted,
);
assert_noop!(
Whitelist::remove_whitelisted_call(Origin::signed(1), call_hash),
DispatchError::BadOrigin,
);
assert_ok!(Whitelist::remove_whitelisted_call(Origin::root(), call_hash));
assert!(!Preimage::preimage_requested(&call_hash));
assert_noop!(
Whitelist::remove_whitelisted_call(Origin::root(), call_hash),
crate::Error::<Test>::CallIsNotWhitelisted,
);
});
}
#[test]
fn test_whitelist_call_and_execute() {
new_test_ext().execute_with(|| {
let call = Call::System(frame_system::Call::remark_with_event { remark: vec![1] });
let call_weight = call.get_dispatch_info().weight;
let encoded_call = call.encode();
let call_hash = <Test as frame_system::Config>::Hashing::hash(&encoded_call[..]);
assert_noop!(
Whitelist::dispatch_whitelisted_call(Origin::root(), call_hash, call_weight),
crate::Error::<Test>::CallIsNotWhitelisted,
);
assert_ok!(Whitelist::whitelist_call(Origin::root(), call_hash));
assert_noop!(
Whitelist::dispatch_whitelisted_call(Origin::signed(1), call_hash, call_weight),
DispatchError::BadOrigin,
);
assert_noop!(
Whitelist::dispatch_whitelisted_call(Origin::root(), call_hash, call_weight),
crate::Error::<Test>::UnavailablePreImage,
);
assert_ok!(Preimage::note_preimage(Origin::root(), encoded_call));
assert!(Preimage::preimage_requested(&call_hash));
assert_noop!(
Whitelist::dispatch_whitelisted_call(Origin::root(), call_hash, call_weight - 1),
crate::Error::<Test>::InvalidCallWeightWitness,
);
assert_ok!(Whitelist::dispatch_whitelisted_call(Origin::root(), call_hash, call_weight));
assert!(!Preimage::preimage_requested(&call_hash));
assert_noop!(
Whitelist::dispatch_whitelisted_call(Origin::root(), call_hash, call_weight),
crate::Error::<Test>::CallIsNotWhitelisted,
);
});
}
#[test]
fn test_whitelist_call_and_execute_failing_call() {
new_test_ext().execute_with(|| {
let call = Call::Whitelist(crate::Call::dispatch_whitelisted_call {
call_hash: Default::default(),
call_weight_witness: 0,
});
let call_weight = call.get_dispatch_info().weight;
let encoded_call = call.encode();
let call_hash = <Test as frame_system::Config>::Hashing::hash(&encoded_call[..]);
assert_ok!(Whitelist::whitelist_call(Origin::root(), call_hash));
assert_ok!(Preimage::note_preimage(Origin::root(), encoded_call));
assert!(Preimage::preimage_requested(&call_hash));
assert_ok!(Whitelist::dispatch_whitelisted_call(Origin::root(), call_hash, call_weight));
assert!(!Preimage::preimage_requested(&call_hash));
});
}
#[test]
fn test_whitelist_call_and_execute_without_note_preimage() {
new_test_ext().execute_with(|| {
let call =
Box::new(Call::System(frame_system::Call::remark_with_event { remark: vec![1] }));
let call_hash = <Test as frame_system::Config>::Hashing::hash_of(&call);
assert_ok!(Whitelist::whitelist_call(Origin::root(), call_hash));
assert!(Preimage::preimage_requested(&call_hash));
assert_ok!(Whitelist::dispatch_whitelisted_call_with_preimage(
Origin::root(),
call.clone()
));
assert!(!Preimage::preimage_requested(&call_hash));
assert_noop!(
Whitelist::dispatch_whitelisted_call_with_preimage(Origin::root(), call),
crate::Error::<Test>::CallIsNotWhitelisted,
);
});
}
#[test]
fn test_whitelist_call_and_execute_decode_consumes_all() {
new_test_ext().execute_with(|| {
let call = Call::System(frame_system::Call::remark_with_event { remark: vec![1] });
let call_weight = call.get_dispatch_info().weight;
let mut call = call.encode();
// Appending something does not make the encoded call invalid.
// This tests that the decode function consumes all data.
call.extend(call.clone());
let call_hash = <Test as frame_system::Config>::Hashing::hash(&call[..]);
assert_ok!(Preimage::note_preimage(Origin::root(), call));
assert_ok!(Whitelist::whitelist_call(Origin::root(), call_hash));
assert_noop!(
Whitelist::dispatch_whitelisted_call(Origin::root(), call_hash, call_weight),
crate::Error::<Test>::UndecodableCall,
);
});
}
+126
View File
@@ -0,0 +1,126 @@
// This file is part of Substrate.
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Autogenerated weights for pallet_whitelist
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-02-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
// Executed Command:
// target/production/substrate
// benchmark
// --chain=dev
// --steps=50
// --repeat=20
// --pallet=pallet_whitelist
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --output=./frame/whitelist/src/weights.rs
// --template=./.maintain/frame-weight-template.hbs
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;
/// Weight functions needed for pallet_whitelist.
pub trait WeightInfo {
fn whitelist_call() -> Weight;
fn remove_whitelisted_call() -> Weight;
fn dispatch_whitelisted_call() -> Weight;
fn dispatch_whitelisted_call_with_preimage(n: u32, ) -> Weight;
}
/// Weights for pallet_whitelist using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Whitelist WhitelistedCall (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
fn whitelist_call() -> Weight {
(16_254_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Whitelist WhitelistedCall (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn remove_whitelisted_call() -> Weight {
(18_348_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: Whitelist WhitelistedCall (r:1 w:1)
// Storage: Preimage PreimageFor (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
fn dispatch_whitelisted_call() -> Weight {
(6_618_241_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: Whitelist WhitelistedCall (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn dispatch_whitelisted_call_with_preimage(n: u32, ) -> Weight {
(20_619_000 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
}
// For backwards compatibility and tests
impl WeightInfo for () {
// Storage: Whitelist WhitelistedCall (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
fn whitelist_call() -> Weight {
(16_254_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Whitelist WhitelistedCall (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn remove_whitelisted_call() -> Weight {
(18_348_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
// Storage: Whitelist WhitelistedCall (r:1 w:1)
// Storage: Preimage PreimageFor (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
fn dispatch_whitelisted_call() -> Weight {
(6_618_241_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
// Storage: Whitelist WhitelistedCall (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn dispatch_whitelisted_call_with_preimage(n: u32, ) -> Weight {
(20_619_000 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(n as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
}
+1 -1
View File
@@ -511,7 +511,7 @@ pub enum DispatchError {
/// Result of a `Dispatchable` which contains the `DispatchResult` and additional information about
/// the `Dispatchable` that is only known post dispatch.
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug)]
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct DispatchErrorWithPostInfo<Info>
where
Info: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable,