20
20
#![ recursion_limit = "512" ]
21
21
22
22
mod benchmark;
23
- mod clone_no_bound;
24
23
mod construct_runtime;
25
24
mod crate_version;
26
- mod debug_no_bound;
27
- mod default_no_bound;
28
25
mod derive_impl;
29
26
mod dummy_part_checker;
30
27
mod key_prefix;
31
28
mod match_and_insert;
29
+ mod no_bound;
32
30
mod pallet;
33
31
mod pallet_error;
34
- mod partial_eq_no_bound;
35
- mod storage;
36
32
mod storage_alias;
37
33
mod transactional;
38
34
mod tt_macro;
@@ -42,9 +38,10 @@ use macro_magic::import_tokens_attr;
42
38
use proc_macro:: TokenStream ;
43
39
use quote:: { quote, ToTokens } ;
44
40
use std:: { cell:: RefCell , str:: FromStr } ;
45
- pub ( crate ) use storage:: INHERENT_INSTANCE_NAME ;
46
41
use syn:: { parse_macro_input, Error , ItemImpl , ItemMod } ;
47
42
43
+ pub ( crate ) const INHERENT_INSTANCE_NAME : & str = "__InherentHiddenInstance" ;
44
+
48
45
thread_local ! {
49
46
/// A global counter, can be used to generate a relatively unique identifier.
50
47
static COUNTER : RefCell <Counter > = RefCell :: new( Counter ( 0 ) ) ;
@@ -79,233 +76,6 @@ fn counter_prefix(prefix: &str) -> String {
79
76
format ! ( "CounterFor{}" , prefix)
80
77
}
81
78
82
- /// Declares strongly-typed wrappers around codec-compatible types in storage.
83
- ///
84
- /// ## Example
85
- ///
86
- /// ```nocompile
87
- /// decl_storage! {
88
- /// trait Store for Module<T: Config> as Example {
89
- /// Foo get(fn foo) config(): u32=12;
90
- /// Bar: map hasher(identity) u32 => u32;
91
- /// pub Zed build(|config| vec![(0, 0)]): map hasher(identity) u32 => u32;
92
- /// }
93
- /// }
94
- /// ```
95
- ///
96
- /// Declaration is set with the header `(pub) trait Store for Module<T: Config> as Example`,
97
- /// with `Store` a (pub) trait generated associating each storage item to the `Module` and
98
- /// `as Example` setting the prefix used for storage items of this module. `Example` must be unique:
99
- /// another module with the same name and the same inner storage item name will conflict.
100
- /// `Example` is called the module prefix.
101
- ///
102
- /// note: For instantiable modules the module prefix is prepended with instance
103
- /// prefix. Instance prefix is "" for default instance and "Instance$n" for instance number $n.
104
- /// Thus, instance 3 of module Example has a module prefix of `Instance3Example`
105
- ///
106
- /// Basic storage consists of a name and a type; supported types are:
107
- ///
108
- /// * Value: `Foo: type`: Implements the
109
- /// [`StorageValue`](../frame_support/storage/trait.StorageValue.html) trait using the
110
- /// [`StorageValue generator`](../frame_support/storage/generator/trait.StorageValue.html).
111
- ///
112
- /// The generator is implemented with:
113
- /// * `module_prefix`: module_prefix
114
- /// * `storage_prefix`: storage_name
115
- ///
116
- /// Thus the storage value is finally stored at:
117
- /// ```nocompile
118
- /// Twox128(module_prefix) ++ Twox128(storage_prefix)
119
- /// ```
120
- ///
121
- /// * Map: `Foo: map hasher($hash) type => type`: Implements the
122
- /// [`StorageMap`](../frame_support/storage/trait.StorageMap.html) trait using the [`StorageMap
123
- /// generator`](../frame_support/storage/generator/trait.StorageMap.html). And
124
- /// [`StoragePrefixedMap`](../frame_support/storage/trait.StoragePrefixedMap.html).
125
- ///
126
- /// `$hash` representing a choice of hashing algorithms available in the
127
- /// [`Hashable`](../frame_support/trait.Hashable.html) trait. You will generally want to use one
128
- /// of three hashers:
129
- /// * `blake2_128_concat`: The default, safe choice. Use if you are unsure or don't care. It is
130
- /// secure against user-tainted keys, fairly fast and memory-efficient and supports iteration
131
- /// over its keys and values. This must be used if the keys of your map can be selected *en
132
- /// masse* by untrusted users.
133
- /// * `twox_64_concat`: This is an insecure hasher and can only be used safely if you know that
134
- /// the preimages cannot be chosen at will by untrusted users. It is memory-efficient, extremely
135
- /// performant and supports iteration over its keys and values. You can safely use this is the
136
- /// key is:
137
- /// - A (slowly) incrementing index.
138
- /// - Known to be the result of a cryptographic hash (though `identity` is a better choice
139
- /// here).
140
- /// - Known to be the public key of a cryptographic key pair in existence.
141
- /// * `identity`: This is not a hasher at all, and just uses the key material directly. Since it
142
- /// does no hashing or appending, it's the fastest possible hasher, however, it's also the least
143
- /// secure. It can be used only if you know that the key will be cryptographically/securely
144
- /// randomly distributed over the binary encoding space. In most cases this will not be true.
145
- /// One case where it is true, however, if where the key is itself the result of a cryptographic
146
- /// hash of some existent data.
147
- ///
148
- /// Other hashers will tend to be "opaque" and not support iteration over the keys in the
149
- /// map. It is not recommended to use these.
150
- ///
151
- /// The generator is implemented with:
152
- /// * `module_prefix`: $module_prefix
153
- /// * `storage_prefix`: storage_name
154
- /// * `Hasher`: $hash
155
- ///
156
- /// Thus the keys are stored at:
157
- /// ```nocompile
158
- /// twox128(module_prefix) ++ twox128(storage_prefix) ++ hasher(encode(key))
159
- /// ```
160
- ///
161
- /// * Double map: `Foo: double_map hasher($hash1) u32, hasher($hash2) u32 => u32`: Implements the
162
- /// [`StorageDoubleMap`](../frame_support/storage/trait.StorageDoubleMap.html) trait using the
163
- /// [`StorageDoubleMap
164
- /// generator`](../frame_support/storage/generator/trait.StorageDoubleMap.html). And
165
- /// [`StoragePrefixedMap`](../frame_support/storage/trait.StoragePrefixedMap.html).
166
- ///
167
- /// `$hash1` and `$hash2` representing choices of hashing algorithms available in the
168
- /// [`Hashable`](../frame_support/trait.Hashable.html) trait. They must be chosen with care, see
169
- /// generator documentation.
170
- ///
171
- /// The generator is implemented with:
172
- /// * `module_prefix`: $module_prefix
173
- /// * `storage_prefix`: storage_name
174
- /// * `Hasher1`: $hash1
175
- /// * `Hasher2`: $hash2
176
- ///
177
- /// Thus keys are stored at:
178
- /// ```nocompile
179
- /// Twox128(module_prefix) ++ Twox128(storage_prefix) ++ Hasher1(encode(key1)) ++
180
- /// Hasher2(encode(key2)) ```
181
- ///
182
- /// Supported hashers (ordered from least to best security):
183
- ///
184
- /// * `identity` - Just the unrefined key material. Use only when it is known to be a secure hash
185
- /// already. The most efficient and iterable over keys.
186
- /// * `twox_64_concat` - TwoX with 64bit + key concatenated. Use only when an untrusted source
187
- /// cannot select and insert key values. Very efficient and iterable over keys.
188
- /// * `blake2_128_concat` - Blake2 with 128bit + key concatenated. Slower but safe to use in all
189
- /// circumstances. Iterable over keys.
190
- ///
191
- /// Deprecated hashers, which do not support iteration over keys include:
192
- /// * `twox_128` - TwoX with 128bit.
193
- /// * `twox_256` - TwoX with with 256bit.
194
- /// * `blake2_128` - Blake2 with 128bit.
195
- /// * `blake2_256` - Blake2 with 256bit.
196
- ///
197
- /// Basic storage can be extended as such:
198
- ///
199
- /// `#vis #name get(fn #getter) config(#field_name) build(#closure): #type = #default;`
200
- ///
201
- /// * `#vis`: Set the visibility of the structure. `pub` or nothing.
202
- /// * `#name`: Name of the storage item, used as a prefix in storage.
203
- /// * \[optional\] `get(fn #getter)`: Implements the function #getter to `Module`.
204
- /// * \[optional\] `config(#field_name)`: `field_name` is optional if get is set.
205
- /// Will include the item in `GenesisConfig`.
206
- /// * \[optional\] `build(#closure)`: Closure called with storage overlays.
207
- /// * \[optional\] `max_values(#expr)`: `expr` is an expression returning a `u32`. It is used to
208
- /// implement `StorageInfoTrait`. Note this attribute is not available for storage value as the
209
- /// maximum number of values is 1.
210
- /// * `#type`: Storage type.
211
- /// * \[optional\] `#default`: Value returned when none.
212
- ///
213
- /// Storage items are accessible in multiple ways:
214
- ///
215
- /// * The structure: `Foo` or `Foo::<T>` depending if the value type is generic or not.
216
- /// * The `Store` trait structure: `<Module<T> as Store>::Foo`
217
- /// * The getter on the module that calls get on the structure: `Module::<T>::foo()`
218
- ///
219
- /// ## GenesisConfig
220
- ///
221
- /// An optional `GenesisConfig` struct for storage initialization can be defined, either
222
- /// when at least one storage field requires default initialization
223
- /// (both `get` and `config` or `build`), or specifically as in:
224
- ///
225
- /// ```nocompile
226
- /// decl_storage! {
227
- /// trait Store for Module<T: Config> as Example {
228
- ///
229
- /// // Your storage items
230
- /// }
231
- /// add_extra_genesis {
232
- /// config(genesis_field): GenesisFieldType;
233
- /// config(genesis_field2): GenesisFieldType;
234
- /// ...
235
- /// build(|_: &Self| {
236
- /// // Modification of storage
237
- /// })
238
- /// }
239
- /// }
240
- /// ```
241
- ///
242
- /// This struct can be exposed as `ExampleConfig` by the `construct_runtime!` macro like follows:
243
- ///
244
- /// ```nocompile
245
- /// construct_runtime!(
246
- /// pub enum Runtime with ... {
247
- /// ...,
248
- /// Example: example::{Pallet, Storage, ..., Config<T>},
249
- /// ...,
250
- /// }
251
- /// );
252
- /// ```
253
- ///
254
- /// ### Module with Instances
255
- ///
256
- /// The `decl_storage!` macro supports building modules with instances with the following syntax
257
- /// (`DefaultInstance` type is optional):
258
- ///
259
- /// ```nocompile
260
- /// trait Store for Module<T: Config<I>, I: Instance=DefaultInstance> as Example {}
261
- /// ```
262
- ///
263
- /// Accessing the structure no requires the instance as generic parameter:
264
- /// * `Foo::<I>` if the value type is not generic
265
- /// * `Foo::<T, I>` if the value type is generic
266
- ///
267
- /// ## Where clause
268
- ///
269
- /// This macro supports a where clause which will be replicated to all generated types.
270
- ///
271
- /// ```nocompile
272
- /// trait Store for Module<T: Config> as Example where T::AccountId: std::fmt::Display {}
273
- /// ```
274
- ///
275
- /// ## Limitations
276
- ///
277
- /// # Instancing and generic `GenesisConfig`
278
- ///
279
- /// If your module supports instancing and you see an error like `parameter `I` is never used` for
280
- /// your `decl_storage!`, you are hitting a limitation of the current implementation. You probably
281
- /// try to use an associated type of a non-instantiable trait. To solve this, add the following to
282
- /// your macro call:
283
- ///
284
- /// ```nocompile
285
- /// add_extra_genesis {
286
- /// config(phantom): std::marker::PhantomData<I>,
287
- /// }
288
- /// ```
289
- ///
290
- /// This adds a field to your `GenesisConfig` with the name `phantom` that you can initialize with
291
- /// `Default::default()`.
292
- ///
293
- /// ## PoV information
294
- ///
295
- /// To implement the trait `StorageInfoTrait` for storages an additional attribute can be used
296
- /// `generate_storage_info`:
297
- /// ```nocompile
298
- /// decl_storage! { generate_storage_info
299
- /// trait Store for ...
300
- /// }
301
- /// ```
302
- #[ proc_macro]
303
- #[ deprecated( note = "Will be removed after July 2023; use the attribute `#[pallet]` macro instead.
304
- For more info, see: <https://github.com/paritytech/substrate/pull/13705>" ) ]
305
- pub fn decl_storage ( input : TokenStream ) -> TokenStream {
306
- storage:: decl_storage_impl ( input)
307
- }
308
-
309
79
/// Construct a runtime, with the given name and the given pallets.
310
80
///
311
81
/// The parameters here are specific types for `Block`, `NodeBlock`, and `UncheckedExtrinsic`
@@ -645,13 +415,13 @@ pub fn require_transactional(attr: TokenStream, input: TokenStream) -> TokenStre
645
415
/// Derive [`Clone`] but do not bound any generic. Docs are at `frame_support::CloneNoBound`.
646
416
#[ proc_macro_derive( CloneNoBound ) ]
647
417
pub fn derive_clone_no_bound ( input : TokenStream ) -> TokenStream {
648
- clone_no_bound :: derive_clone_no_bound ( input)
418
+ no_bound :: clone :: derive_clone_no_bound ( input)
649
419
}
650
420
651
421
/// Derive [`Debug`] but do not bound any generics. Docs are at `frame_support::DebugNoBound`.
652
422
#[ proc_macro_derive( DebugNoBound ) ]
653
423
pub fn derive_debug_no_bound ( input : TokenStream ) -> TokenStream {
654
- debug_no_bound :: derive_debug_no_bound ( input)
424
+ no_bound :: debug :: derive_debug_no_bound ( input)
655
425
}
656
426
657
427
/// Derive [`Debug`], if `std` is enabled it uses `frame_support::DebugNoBound`, if `std` is not
@@ -660,7 +430,7 @@ pub fn derive_debug_no_bound(input: TokenStream) -> TokenStream {
660
430
#[ proc_macro_derive( RuntimeDebugNoBound ) ]
661
431
pub fn derive_runtime_debug_no_bound ( input : TokenStream ) -> TokenStream {
662
432
if cfg ! ( any( feature = "std" , feature = "try-runtime" ) ) {
663
- debug_no_bound :: derive_debug_no_bound ( input)
433
+ no_bound :: debug :: derive_debug_no_bound ( input)
664
434
} else {
665
435
let input: syn:: DeriveInput = match syn:: parse ( input) {
666
436
Ok ( input) => input,
@@ -687,7 +457,7 @@ pub fn derive_runtime_debug_no_bound(input: TokenStream) -> TokenStream {
687
457
/// `frame_support::PartialEqNoBound`.
688
458
#[ proc_macro_derive( PartialEqNoBound ) ]
689
459
pub fn derive_partial_eq_no_bound ( input : TokenStream ) -> TokenStream {
690
- partial_eq_no_bound :: derive_partial_eq_no_bound ( input)
460
+ no_bound :: partial_eq :: derive_partial_eq_no_bound ( input)
691
461
}
692
462
693
463
/// derive Eq but do no bound any generic. Docs are at `frame_support::EqNoBound`.
@@ -712,7 +482,7 @@ pub fn derive_eq_no_bound(input: TokenStream) -> TokenStream {
712
482
/// derive `Default` but do no bound any generic. Docs are at `frame_support::DefaultNoBound`.
713
483
#[ proc_macro_derive( DefaultNoBound , attributes( default ) ) ]
714
484
pub fn derive_default_no_bound ( input : TokenStream ) -> TokenStream {
715
- default_no_bound :: derive_default_no_bound ( input)
485
+ no_bound :: default :: derive_default_no_bound ( input)
716
486
}
717
487
718
488
#[ proc_macro]
0 commit comments