Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
PreimageHash alias, remove type annotation
  • Loading branch information
muharem committed Dec 6, 2022
commit d894da1463e3773a46d66e678a744bb495ec3dee
12 changes: 6 additions & 6 deletions frame/whitelist/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ benchmarks! {
"call not whitelisted"
);
ensure!(
<T::Preimages as QueryPreimage>::is_requested(&call_hash),
T::Preimages::is_requested(&call_hash),
"preimage not requested"
);
}
Expand All @@ -54,7 +54,7 @@ benchmarks! {
"whitelist not removed"
);
ensure!(
!<T::Preimages as QueryPreimage>::is_requested(&call_hash),
!T::Preimages::is_requested(&call_hash),
"preimage still requested"
);
}
Expand All @@ -64,7 +64,7 @@ benchmarks! {
// on the size of the call, with a new witness in parameter.
dispatch_whitelisted_call {
// NOTE: we remove `10` because we need some bytes to encode the variants and vec length
let n in 1 .. <T::Preimages as StorePreimage>::MAX_LENGTH as u32 - 10;
let n in 1 .. T::Preimages::MAX_LENGTH as u32 - 10;

let origin = T::DispatchWhitelistedOrigin::successful_origin();
let remark = sp_std::vec![1u8; n as usize];
Expand All @@ -77,7 +77,7 @@ benchmarks! {
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");

<T::Preimages as StorePreimage>::note(encoded_call.into()).unwrap();
T::Preimages::note(encoded_call.into()).unwrap();

}: _<T::RuntimeOrigin>(origin, call_hash, call_encoded_len, call_weight)
verify {
Expand All @@ -86,7 +86,7 @@ benchmarks! {
"whitelist not removed"
);
ensure!(
!<T::Preimages as QueryPreimage>::is_requested(&call_hash),
!T::Preimages::is_requested(&call_hash),
"preimage still requested"
);
}
Expand All @@ -109,7 +109,7 @@ benchmarks! {
"whitelist not removed"
);
ensure!(
!<T::Preimages as QueryPreimage>::is_requested(&call_hash),
!T::Preimages::is_requested(&call_hash),
"preimage still requested"
);
}
Expand Down
25 changes: 16 additions & 9 deletions frame/whitelist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use codec::{DecodeLimit, Encode, FullCodec};
use frame_support::{
dispatch::{GetDispatchInfo, PostDispatchInfo},
ensure,
traits::{Hash, QueryPreimage, StorePreimage},
traits::{Hash as PreimageHash, QueryPreimage, StorePreimage},
weights::Weight,
Hashable,
};
Expand Down Expand Up @@ -94,9 +94,9 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
CallWhitelisted { call_hash: Hash },
WhitelistedCallRemoved { call_hash: Hash },
WhitelistedCallDispatched { call_hash: Hash, result: DispatchResultWithPostInfo },
CallWhitelisted { call_hash: PreimageHash },
WhitelistedCallRemoved { call_hash: PreimageHash },
WhitelistedCallDispatched { call_hash: PreimageHash, result: DispatchResultWithPostInfo },
}

#[pallet::error]
Expand All @@ -114,12 +114,13 @@ pub mod pallet {
}

#[pallet::storage]
pub type WhitelistedCall<T: Config> = StorageMap<_, Twox64Concat, Hash, (), OptionQuery>;
pub type WhitelistedCall<T: Config> =
StorageMap<_, Twox64Concat, PreimageHash, (), OptionQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::WeightInfo::whitelist_call())]
pub fn whitelist_call(origin: OriginFor<T>, call_hash: Hash) -> DispatchResult {
pub fn whitelist_call(origin: OriginFor<T>, call_hash: PreimageHash) -> DispatchResult {
T::WhitelistOrigin::ensure_origin(origin)?;

ensure!(
Expand All @@ -136,7 +137,10 @@ pub mod pallet {
}

#[pallet::weight(T::WeightInfo::remove_whitelisted_call())]
pub fn remove_whitelisted_call(origin: OriginFor<T>, call_hash: Hash) -> DispatchResult {
pub fn remove_whitelisted_call(
origin: OriginFor<T>,
call_hash: PreimageHash,
) -> DispatchResult {
T::WhitelistOrigin::ensure_origin(origin)?;

WhitelistedCall::<T>::take(call_hash).ok_or(Error::<T>::CallIsNotWhitelisted)?;
Expand All @@ -154,7 +158,7 @@ pub mod pallet {
)]
pub fn dispatch_whitelisted_call(
origin: OriginFor<T>,
call_hash: Hash,
call_hash: PreimageHash,
call_encoded_len: u32,
call_weight_witness: Weight,
) -> DispatchResultWithPostInfo {
Expand Down Expand Up @@ -220,7 +224,10 @@ 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: Hash, call: <T as Config>::RuntimeCall) -> Option<Weight> {
fn clean_and_dispatch(
call_hash: PreimageHash,
call: <T as Config>::RuntimeCall,
) -> Option<Weight> {
WhitelistedCall::<T>::remove(call_hash);

T::Preimages::unrequest(&call_hash);
Expand Down
22 changes: 11 additions & 11 deletions frame/whitelist/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn test_whitelist_call_and_remove() {

assert_ok!(Whitelist::whitelist_call(RuntimeOrigin::root(), call_hash));

assert!(<Preimage as QueryPreimage>::is_requested(&call_hash));
assert!(Preimage::is_requested(&call_hash));

assert_noop!(
Whitelist::whitelist_call(RuntimeOrigin::root(), call_hash),
Expand All @@ -60,7 +60,7 @@ fn test_whitelist_call_and_remove() {

assert_ok!(Whitelist::remove_whitelisted_call(RuntimeOrigin::root(), call_hash));

assert!(!<Preimage as QueryPreimage>::is_requested(&call_hash));
assert!(!Preimage::is_requested(&call_hash));

assert_noop!(
Whitelist::remove_whitelisted_call(RuntimeOrigin::root(), call_hash),
Expand Down Expand Up @@ -110,9 +110,9 @@ fn test_whitelist_call_and_execute() {
crate::Error::<Test>::UnavailablePreImage,
);

assert_ok!(<Preimage as StorePreimage>::note(encoded_call.into()));
assert_ok!(Preimage::note(encoded_call.into()));

assert!(<Preimage as QueryPreimage>::is_requested(&call_hash));
assert!(Preimage::is_requested(&call_hash));

assert_noop!(
Whitelist::dispatch_whitelisted_call(
Expand All @@ -131,7 +131,7 @@ fn test_whitelist_call_and_execute() {
call_weight
));

assert!(!<Preimage as QueryPreimage>::is_requested(&call_hash));
assert!(!Preimage::is_requested(&call_hash));

assert_noop!(
Whitelist::dispatch_whitelisted_call(
Expand Down Expand Up @@ -159,15 +159,15 @@ fn test_whitelist_call_and_execute_failing_call() {
let call_hash = <Test as frame_system::Config>::Hashing::hash(&encoded_call[..]);

assert_ok!(Whitelist::whitelist_call(RuntimeOrigin::root(), call_hash));
assert_ok!(<Preimage as StorePreimage>::note(encoded_call.into()));
assert!(<Preimage as QueryPreimage>::is_requested(&call_hash));
assert_ok!(Preimage::note(encoded_call.into()));
assert!(Preimage::is_requested(&call_hash));
assert_ok!(Whitelist::dispatch_whitelisted_call(
RuntimeOrigin::root(),
call_hash,
call_encoded_len,
call_weight
));
assert!(!<Preimage as QueryPreimage>::is_requested(&call_hash));
assert!(!Preimage::is_requested(&call_hash));
});
}

Expand All @@ -180,14 +180,14 @@ fn test_whitelist_call_and_execute_without_note_preimage() {
let call_hash = <Test as frame_system::Config>::Hashing::hash_of(&call);

assert_ok!(Whitelist::whitelist_call(RuntimeOrigin::root(), call_hash));
assert!(<Preimage as QueryPreimage>::is_requested(&call_hash));
assert!(Preimage::is_requested(&call_hash));

assert_ok!(Whitelist::dispatch_whitelisted_call_with_preimage(
RuntimeOrigin::root(),
call.clone()
));

assert!(!<Preimage as QueryPreimage>::is_requested(&call_hash));
assert!(!Preimage::is_requested(&call_hash));

assert_noop!(
Whitelist::dispatch_whitelisted_call_with_preimage(RuntimeOrigin::root(), call),
Expand All @@ -209,7 +209,7 @@ fn test_whitelist_call_and_execute_decode_consumes_all() {

let call_hash = <Test as frame_system::Config>::Hashing::hash(&call[..]);

assert_ok!(<Preimage as StorePreimage>::note(call.into()));
assert_ok!(Preimage::note(call.into()));
assert_ok!(Whitelist::whitelist_call(RuntimeOrigin::root(), call_hash));

assert_noop!(
Expand Down