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};
4444use 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} ;
5051use scale_info:: TypeInfo ;
51- use sp_runtime:: traits:: { Dispatchable , Hash } ;
52+ use sp_runtime:: traits:: Dispatchable ;
5253use sp_std:: prelude:: * ;
5354
5455pub 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
0 commit comments