Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
pallet whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Dec 13, 2021
1 parent 914f6e5 commit 4826e1e
Show file tree
Hide file tree
Showing 10 changed files with 722 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ members = [
"frame/uniques",
"frame/utility",
"frame/vesting",
"frame/whitelist",
"frame/bags-list",
"frame/bags-list/remote-tests",
"frame/bags-list/fuzzer",
Expand Down
5 changes: 4 additions & 1 deletion bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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" }
Expand Down Expand Up @@ -176,7 +177,7 @@ std = [
"log/std",
"frame-try-runtime/std",
"sp-npos-elections/std",
"sp-io/std",
"sp-io/std",
"pallet-child-bounties/std",
]
runtime-benchmarks = [
Expand Down Expand Up @@ -216,6 +217,7 @@ runtime-benchmarks = [
"pallet-utility/runtime-benchmarks",
"pallet-uniques/runtime-benchmarks",
"pallet-vesting/runtime-benchmarks",
"pallet-whitelist/runtime-benchmarks",
"pallet-offences-benchmarking",
"pallet-session-benchmarking",
"frame-system-benchmarking",
Expand Down Expand Up @@ -263,6 +265,7 @@ try-runtime = [
"pallet-uniques/try-runtime",
"pallet-vesting/try-runtime",
"pallet-gilt/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.
Expand Down
12 changes: 12 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,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,
Expand Down Expand Up @@ -1339,6 +1348,7 @@ construct_runtime!(
TransactionStorage: pallet_transaction_storage,
BagsList: pallet_bags_list,
ChildBounties: pallet_child_bounties,
Whitelist: pallet_whitelist,
}
);

Expand Down Expand Up @@ -1719,6 +1729,7 @@ impl_runtime_apis! {
list_benchmark!(list, extra, pallet_uniques, Uniques);
list_benchmark!(list, extra, pallet_utility, Utility);
list_benchmark!(list, extra, pallet_vesting, Vesting);
list_benchmark!(list, extra, pallet_whitelist, Whitelist);

let storage_info = AllPalletsWithSystem::storage_info();

Expand Down Expand Up @@ -1798,6 +1809,7 @@ impl_runtime_apis! {
add_benchmark!(params, batches, pallet_uniques, Uniques);
add_benchmark!(params, batches, pallet_utility, Utility);
add_benchmark!(params, batches, pallet_vesting, Vesting);
add_benchmark!(params, batches, pallet_whitelist, Whitelist);

Ok(batches)
}
Expand Down
46 changes: 46 additions & 0 deletions frame/whitelist/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "pallet-whitelist"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
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 = "2.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
sp-std = { version = "4.0.0-dev", default-features = false, path = "../../primitives/std" }
sp-io = { version = "4.0.0-dev", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "4.0.0-dev", 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 = "4.1.0-dev", 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"]
98 changes: 98 additions & 0 deletions frame/whitelist/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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 frame_benchmarking::benchmarks;
use frame_support::{ensure, traits::PreimageRecipient};
use sp_runtime::traits::Hash;
use core::convert::TryInto;

#[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) == true,
"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) == false,
"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 = (0 .. <T::PreimageProvider as PreimageRecipient<_>>::MaxSize::get() - 10)
.map(|_| 1u8)
.collect();

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) == false,
"preimage still requested"
);
}

impl_benchmark_test_suite!(Whitelist, crate::mock::new_test_ext(), crate::mock::Test);
}
Loading

0 comments on commit 4826e1e

Please # to comment.