Skip to content

Commit 0957aa0

Browse files
muharemltfschoen
authored andcommitted
Whitelist pallet preimage provider upgrade (paritytech#12834)
* whitelist preimage provider upgrade * rustdocs unresolved link error fix * ".git/.scripts/bench-bot.sh" pallet dev pallet_whitelist * PreimageHash alias, remove type annotation Co-authored-by: command-bot <>
1 parent 703eb6e commit 0957aa0

File tree

6 files changed

+153
-104
lines changed

6 files changed

+153
-104
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ impl pallet_whitelist::Config for Runtime {
15441544
type RuntimeCall = RuntimeCall;
15451545
type WhitelistOrigin = EnsureRoot<AccountId>;
15461546
type DispatchWhitelistedOrigin = EnsureRoot<AccountId>;
1547-
type PreimageProvider = Preimage;
1547+
type Preimages = Preimage;
15481548
type WeightInfo = pallet_whitelist::weights::SubstrateWeight<Runtime>;
15491549
}
15501550

frame/whitelist/src/benchmarking.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121

2222
use super::*;
2323
use frame_benchmarking::benchmarks;
24-
use frame_support::{
25-
ensure,
26-
traits::{EnsureOrigin, Get, PreimageRecipient},
27-
};
24+
use frame_support::{ensure, traits::EnsureOrigin};
2825

2926
#[cfg(test)]
3027
use crate::Pallet as Whitelist;
@@ -40,7 +37,7 @@ benchmarks! {
4037
"call not whitelisted"
4138
);
4239
ensure!(
43-
T::PreimageProvider::preimage_requested(&call_hash),
40+
T::Preimages::is_requested(&call_hash),
4441
"preimage not requested"
4542
);
4643
}
@@ -57,7 +54,7 @@ benchmarks! {
5754
"whitelist not removed"
5855
);
5956
ensure!(
60-
!T::PreimageProvider::preimage_requested(&call_hash),
57+
!T::Preimages::is_requested(&call_hash),
6158
"preimage still requested"
6259
);
6360
}
@@ -66,30 +63,30 @@ benchmarks! {
6663
// If the resulting weight is too big, maybe it worth having a weight which depends
6764
// on the size of the call, with a new witness in parameter.
6865
dispatch_whitelisted_call {
69-
let origin = T::DispatchWhitelistedOrigin::successful_origin();
7066
// NOTE: we remove `10` because we need some bytes to encode the variants and vec length
71-
let remark_len = <T::PreimageProvider as PreimageRecipient<_>>::MaxSize::get() - 10;
72-
let remark = sp_std::vec![1u8; remark_len as usize];
67+
let n in 1 .. T::Preimages::MAX_LENGTH as u32 - 10;
7368

69+
let origin = T::DispatchWhitelistedOrigin::successful_origin();
70+
let remark = sp_std::vec![1u8; n as usize];
7471
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark }.into();
7572
let call_weight = call.get_dispatch_info().weight;
7673
let encoded_call = call.encode();
77-
let call_hash = T::Hashing::hash(&encoded_call[..]);
74+
let call_encoded_len = encoded_call.len() as u32;
75+
let call_hash = call.blake2_256().into();
7876

7977
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
8078
.expect("whitelisting call must be successful");
8179

82-
let encoded_call = encoded_call.try_into().expect("encoded_call must be small enough");
83-
T::PreimageProvider::note_preimage(encoded_call);
80+
T::Preimages::note(encoded_call.into()).unwrap();
8481

85-
}: _<T::RuntimeOrigin>(origin, call_hash, call_weight)
82+
}: _<T::RuntimeOrigin>(origin, call_hash, call_encoded_len, call_weight)
8683
verify {
8784
ensure!(
8885
!WhitelistedCall::<T>::contains_key(call_hash),
8986
"whitelist not removed"
9087
);
9188
ensure!(
92-
!T::PreimageProvider::preimage_requested(&call_hash),
89+
!T::Preimages::is_requested(&call_hash),
9390
"preimage still requested"
9491
);
9592
}
@@ -101,7 +98,7 @@ benchmarks! {
10198
let remark = sp_std::vec![1u8; n as usize];
10299

103100
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark }.into();
104-
let call_hash = T::Hashing::hash_of(&call);
101+
let call_hash = call.blake2_256().into();
105102

106103
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
107104
.expect("whitelisting call must be successful");
@@ -112,7 +109,7 @@ benchmarks! {
112109
"whitelist not removed"
113110
);
114111
ensure!(
115-
!T::PreimageProvider::preimage_requested(&call_hash),
112+
!T::Preimages::is_requested(&call_hash),
116113
"preimage still requested"
117114
);
118115
}

frame/whitelist/src/lib.rs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//! with the root origin.
2828
//!
2929
//! In the meantime the call corresponding to the hash must have been submitted to the pre-image
30-
//! handler [`PreimageProvider`].
30+
//! handler [`pallet::Config::Preimages`].
3131
3232
#![cfg_attr(not(feature = "std"), no_std)]
3333

@@ -44,11 +44,12 @@ use codec::{DecodeLimit, Encode, FullCodec};
4444
use frame_support::{
4545
dispatch::{GetDispatchInfo, PostDispatchInfo},
4646
ensure,
47-
traits::{PreimageProvider, PreimageRecipient},
47+
traits::{Hash as PreimageHash, QueryPreimage, StorePreimage},
4848
weights::Weight,
49+
Hashable,
4950
};
5051
use scale_info::TypeInfo;
51-
use sp_runtime::traits::{Dispatchable, Hash};
52+
use sp_runtime::traits::Dispatchable;
5253
use sp_std::prelude::*;
5354

5455
pub use pallet::*;
@@ -80,8 +81,7 @@ pub mod pallet {
8081
type DispatchWhitelistedOrigin: EnsureOrigin<Self::RuntimeOrigin>;
8182

8283
/// The handler of pre-images.
83-
// NOTE: recipient is only needed for benchmarks.
84-
type PreimageProvider: PreimageProvider<Self::Hash> + PreimageRecipient<Self::Hash>;
84+
type Preimages: QueryPreimage + StorePreimage;
8585

8686
/// The weight information for this pallet.
8787
type WeightInfo: WeightInfo;
@@ -94,9 +94,9 @@ pub mod pallet {
9494
#[pallet::event]
9595
#[pallet::generate_deposit(pub(super) fn deposit_event)]
9696
pub enum Event<T: Config> {
97-
CallWhitelisted { call_hash: T::Hash },
98-
WhitelistedCallRemoved { call_hash: T::Hash },
99-
WhitelistedCallDispatched { call_hash: T::Hash, result: DispatchResultWithPostInfo },
97+
CallWhitelisted { call_hash: PreimageHash },
98+
WhitelistedCallRemoved { call_hash: PreimageHash },
99+
WhitelistedCallDispatched { call_hash: PreimageHash, result: DispatchResultWithPostInfo },
100100
}
101101

102102
#[pallet::error]
@@ -114,12 +114,13 @@ pub mod pallet {
114114
}
115115

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

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

125126
ensure!(
@@ -128,32 +129,37 @@ pub mod pallet {
128129
);
129130

130131
WhitelistedCall::<T>::insert(call_hash, ());
131-
T::PreimageProvider::request_preimage(&call_hash);
132+
T::Preimages::request(&call_hash);
132133

133134
Self::deposit_event(Event::<T>::CallWhitelisted { call_hash });
134135

135136
Ok(())
136137
}
137138

138139
#[pallet::weight(T::WeightInfo::remove_whitelisted_call())]
139-
pub fn remove_whitelisted_call(origin: OriginFor<T>, call_hash: T::Hash) -> DispatchResult {
140+
pub fn remove_whitelisted_call(
141+
origin: OriginFor<T>,
142+
call_hash: PreimageHash,
143+
) -> DispatchResult {
140144
T::WhitelistOrigin::ensure_origin(origin)?;
141145

142146
WhitelistedCall::<T>::take(call_hash).ok_or(Error::<T>::CallIsNotWhitelisted)?;
143147

144-
T::PreimageProvider::unrequest_preimage(&call_hash);
148+
T::Preimages::unrequest(&call_hash);
145149

146150
Self::deposit_event(Event::<T>::WhitelistedCallRemoved { call_hash });
147151

148152
Ok(())
149153
}
150154

151155
#[pallet::weight(
152-
T::WeightInfo::dispatch_whitelisted_call().saturating_add(*call_weight_witness)
156+
T::WeightInfo::dispatch_whitelisted_call(*call_encoded_len)
157+
.saturating_add(*call_weight_witness)
153158
)]
154159
pub fn dispatch_whitelisted_call(
155160
origin: OriginFor<T>,
156-
call_hash: T::Hash,
161+
call_hash: PreimageHash,
162+
call_encoded_len: u32,
157163
call_weight_witness: Weight,
158164
) -> DispatchResultWithPostInfo {
159165
T::DispatchWhitelistedOrigin::ensure_origin(origin)?;
@@ -163,8 +169,8 @@ pub mod pallet {
163169
Error::<T>::CallIsNotWhitelisted,
164170
);
165171

166-
let call = T::PreimageProvider::get_preimage(&call_hash)
167-
.ok_or(Error::<T>::UnavailablePreImage)?;
172+
let call = T::Preimages::fetch(&call_hash, Some(call_encoded_len))
173+
.map_err(|_| Error::<T>::UnavailablePreImage)?;
168174

169175
let call = <T as Config>::RuntimeCall::decode_all_with_depth_limit(
170176
sp_api::MAX_EXTRINSIC_DEPTH,
@@ -177,8 +183,9 @@ pub mod pallet {
177183
Error::<T>::InvalidCallWeightWitness
178184
);
179185

180-
let actual_weight = Self::clean_and_dispatch(call_hash, call)
181-
.map(|w| w.saturating_add(T::WeightInfo::dispatch_whitelisted_call()));
186+
let actual_weight = Self::clean_and_dispatch(call_hash, call).map(|w| {
187+
w.saturating_add(T::WeightInfo::dispatch_whitelisted_call(call_encoded_len))
188+
});
182189

183190
Ok(actual_weight.into())
184191
}
@@ -196,7 +203,7 @@ pub mod pallet {
196203
) -> DispatchResultWithPostInfo {
197204
T::DispatchWhitelistedOrigin::ensure_origin(origin)?;
198205

199-
let call_hash = <T as frame_system::Config>::Hashing::hash_of(&call);
206+
let call_hash = call.blake2_256().into();
200207

201208
ensure!(
202209
WhitelistedCall::<T>::contains_key(call_hash),
@@ -217,10 +224,13 @@ impl<T: Config> Pallet<T> {
217224
/// Clean whitelisting/preimage and dispatch call.
218225
///
219226
/// Return the call actual weight of the dispatched call if there is some.
220-
fn clean_and_dispatch(call_hash: T::Hash, call: <T as Config>::RuntimeCall) -> Option<Weight> {
227+
fn clean_and_dispatch(
228+
call_hash: PreimageHash,
229+
call: <T as Config>::RuntimeCall,
230+
) -> Option<Weight> {
221231
WhitelistedCall::<T>::remove(call_hash);
222232

223-
T::PreimageProvider::unrequest_preimage(&call_hash);
233+
T::Preimages::unrequest(&call_hash);
224234

225235
let result = call.dispatch(frame_system::Origin::<T>::Root.into());
226236

frame/whitelist/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl pallet_whitelist::Config for Test {
106106
type RuntimeCall = RuntimeCall;
107107
type WhitelistOrigin = EnsureRoot<Self::AccountId>;
108108
type DispatchWhitelistedOrigin = EnsureRoot<Self::AccountId>;
109-
type PreimageProvider = Preimage;
109+
type Preimages = Preimage;
110110
type WeightInfo = ();
111111
}
112112

0 commit comments

Comments
 (0)