1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use frame_support_procedural_tools::{syn_ext as ext, Parse, ToTokens};
use syn::{spanned::Spanned, Ident, Token};
mod keyword {
syn::custom_keyword!(generate_storage_info);
syn::custom_keyword!(hiddencrate);
syn::custom_keyword!(add_extra_genesis);
syn::custom_keyword!(extra_genesis_skip_phantom_data_field);
syn::custom_keyword!(config);
syn::custom_keyword!(max_values);
syn::custom_keyword!(build);
syn::custom_keyword!(get);
syn::custom_keyword!(map);
syn::custom_keyword!(double_map);
syn::custom_keyword!(nmap);
syn::custom_keyword!(opaque_blake2_256);
syn::custom_keyword!(opaque_blake2_128);
syn::custom_keyword!(blake2_128_concat);
syn::custom_keyword!(opaque_twox_256);
syn::custom_keyword!(opaque_twox_128);
syn::custom_keyword!(twox_64_concat);
syn::custom_keyword!(identity);
syn::custom_keyword!(hasher);
syn::custom_keyword!(tainted);
syn::custom_keyword!(natural);
syn::custom_keyword!(prehashed);
}
#[derive(Debug, Clone)]
pub struct Opt<P> {
pub inner: Option<P>,
}
impl<P: quote::ToTokens> quote::ToTokens for Opt<P> {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
if let Some(ref p) = self.inner {
p.to_tokens(tokens);
}
}
}
macro_rules! impl_parse_for_opt {
($struct:ident => $token:path) => {
impl syn::parse::Parse for Opt<$struct> {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
if input.peek($token) {
input.parse().map(|p| Opt { inner: Some(p) })
} else {
Ok(Opt { inner: None })
}
}
}
};
}
#[derive(Parse, ToTokens, Debug)]
struct StorageDefinition {
pub generate_storage_info: Opt<GenerateStorageInfo>,
pub hidden_crate: Opt<SpecificHiddenCrate>,
pub visibility: syn::Visibility,
pub trait_token: Token![trait],
pub ident: Ident,
pub for_token: Token![for],
pub module_ident: Ident,
pub mod_lt_token: Token![<],
pub mod_param_generic: syn::Ident,
pub mod_param_bound_token: Option<Token![:]>,
pub mod_param_bound: syn::Path,
pub mod_instance_param_token: Option<Token![,]>,
pub mod_instance: Option<syn::Ident>,
pub mod_instantiable_token: Option<Token![:]>,
pub mod_instantiable: Option<syn::Ident>,
pub mod_default_instance_token: Option<Token![=]>,
pub mod_default_instance: Option<syn::Ident>,
pub mod_gt_token: Token![>],
pub as_token: Token![as],
pub crate_ident: Ident,
pub where_clause: Option<syn::WhereClause>,
pub content: ext::Braces<ext::Punctuated<DeclStorageLine, Token![;]>>,
pub extra_genesis: Opt<AddExtraGenesis>,
}
#[derive(Parse, ToTokens, Debug)]
struct GenerateStorageInfo {
pub keyword: keyword::generate_storage_info,
}
impl_parse_for_opt!(GenerateStorageInfo => keyword::generate_storage_info);
#[derive(Parse, ToTokens, Debug)]
struct SpecificHiddenCrate {
pub keyword: keyword::hiddencrate,
pub ident: ext::Parens<Ident>,
}
impl_parse_for_opt!(SpecificHiddenCrate => keyword::hiddencrate);
#[derive(Parse, ToTokens, Debug)]
struct AddExtraGenesis {
pub extragenesis_keyword: keyword::add_extra_genesis,
pub content: ext::Braces<AddExtraGenesisContent>,
}
impl_parse_for_opt!(AddExtraGenesis => keyword::add_extra_genesis);
#[derive(Parse, ToTokens, Debug)]
struct AddExtraGenesisContent {
pub lines: ext::Punctuated<AddExtraGenesisLineEnum, Token![;]>,
}
#[derive(ToTokens, Debug)]
enum AddExtraGenesisLineEnum {
AddExtraGenesisLine(AddExtraGenesisLine),
AddExtraGenesisBuild(DeclStorageBuild),
}
impl syn::parse::Parse for AddExtraGenesisLineEnum {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
let input_fork = input.fork();
let _: ext::OuterAttributes = input_fork.parse()?;
let lookahead = input_fork.lookahead1();
if lookahead.peek(keyword::build) {
Ok(Self::AddExtraGenesisBuild(input.parse()?))
} else if lookahead.peek(keyword::config) {
Ok(Self::AddExtraGenesisLine(input.parse()?))
} else {
Err(lookahead.error())
}
}
}
#[derive(Parse, ToTokens, Debug)]
struct AddExtraGenesisLine {
pub attrs: ext::OuterAttributes,
pub config_keyword: keyword::config,
pub extra_field: ext::Parens<Ident>,
pub coldot_token: Token![:],
pub extra_type: syn::Type,
pub default_value: Opt<DeclStorageDefault>,
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageLine {
pub attrs: ext::OuterAttributes,
pub visibility: syn::Visibility,
pub name: Ident,
pub getter: Opt<DeclStorageGetter>,
pub config: Opt<DeclStorageConfig>,
pub max_values: Opt<DeclStorageMaxValues>,
pub build: Opt<DeclStorageBuild>,
pub coldot_token: Token![:],
pub storage_type: DeclStorageType,
pub default_value: Opt<DeclStorageDefault>,
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageGetterBody {
fn_keyword: Token![fn],
ident: Ident,
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageGetter {
pub getter_keyword: keyword::get,
pub getfn: ext::Parens<DeclStorageGetterBody>,
}
impl_parse_for_opt!(DeclStorageGetter => keyword::get);
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageConfig {
pub config_keyword: keyword::config,
pub expr: ext::Parens<Option<syn::Ident>>,
}
impl_parse_for_opt!(DeclStorageConfig => keyword::config);
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageMaxValues {
pub max_values_keyword: keyword::max_values,
pub expr: ext::Parens<syn::Expr>,
}
impl_parse_for_opt!(DeclStorageMaxValues => keyword::max_values);
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageBuild {
pub build_keyword: keyword::build,
pub expr: ext::Parens<syn::Expr>,
}
impl_parse_for_opt!(DeclStorageBuild => keyword::build);
#[derive(ToTokens, Debug)]
enum DeclStorageType {
Map(DeclStorageMap),
DoubleMap(Box<DeclStorageDoubleMap>),
NMap(DeclStorageNMap),
Simple(syn::Type),
}
impl syn::parse::Parse for DeclStorageType {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
if input.peek(keyword::map) {
Ok(Self::Map(input.parse()?))
} else if input.peek(keyword::double_map) {
Ok(Self::DoubleMap(input.parse()?))
} else if input.peek(keyword::nmap) {
Ok(Self::NMap(input.parse()?))
} else {
Ok(Self::Simple(input.parse()?))
}
}
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageMap {
pub map_keyword: keyword::map,
pub hasher: Opt<SetHasher>,
pub key: syn::Type,
pub ass_keyword: Token![=>],
pub value: syn::Type,
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageDoubleMap {
pub map_keyword: keyword::double_map,
pub hasher1: Opt<SetHasher>,
pub key1: syn::Type,
pub comma_keyword: Token![,],
pub hasher2: Opt<SetHasher>,
pub key2: syn::Type,
pub ass_keyword: Token![=>],
pub value: syn::Type,
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageKey {
pub hasher: Opt<SetHasher>,
pub key: syn::Type,
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageNMap {
pub map_keyword: keyword::nmap,
pub storage_keys: ext::PunctuatedTrailing<DeclStorageKey, Token![,]>,
pub ass_keyword: Token![=>],
pub value: syn::Type,
}
#[derive(Clone, ToTokens, Debug)]
enum Hasher {
Blake2_256(keyword::opaque_blake2_256),
Blake2_128(keyword::opaque_blake2_128),
Blake2_128Concat(keyword::blake2_128_concat),
Twox256(keyword::opaque_twox_256),
Twox128(keyword::opaque_twox_128),
Twox64Concat(keyword::twox_64_concat),
Identity(keyword::identity),
}
impl syn::parse::Parse for Hasher {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(keyword::opaque_blake2_256) {
Ok(Self::Blake2_256(input.parse()?))
} else if lookahead.peek(keyword::opaque_blake2_128) {
Ok(Self::Blake2_128(input.parse()?))
} else if lookahead.peek(keyword::blake2_128_concat) {
Ok(Self::Blake2_128Concat(input.parse()?))
} else if lookahead.peek(keyword::opaque_twox_256) {
Ok(Self::Twox256(input.parse()?))
} else if lookahead.peek(keyword::opaque_twox_128) {
Ok(Self::Twox128(input.parse()?))
} else if lookahead.peek(keyword::twox_64_concat) {
Ok(Self::Twox64Concat(input.parse()?))
} else if lookahead.peek(keyword::identity) {
Ok(Self::Identity(input.parse()?))
} else if lookahead.peek(keyword::tainted) {
Ok(Self::Blake2_128Concat(input.parse()?))
} else if lookahead.peek(keyword::natural) {
Ok(Self::Twox64Concat(input.parse()?))
} else if lookahead.peek(keyword::prehashed) {
Ok(Self::Identity(input.parse()?))
} else {
Err(lookahead.error())
}
}
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageDefault {
pub equal_token: Token![=],
pub expr: syn::Expr,
}
impl syn::parse::Parse for Opt<DeclStorageDefault> {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
if input.peek(Token![=]) {
input.parse().map(|p| Opt { inner: Some(p) })
} else {
Ok(Opt { inner: None })
}
}
}
#[derive(Clone, Parse, ToTokens, Debug)]
struct SetHasher {
pub hasher_keyword: keyword::hasher,
pub inner: ext::Parens<Hasher>,
}
impl_parse_for_opt!(SetHasher => keyword::hasher);
impl From<SetHasher> for super::HasherKind {
fn from(set_hasher: SetHasher) -> Self {
set_hasher.inner.content.into()
}
}
impl From<Hasher> for super::HasherKind {
fn from(hasher: Hasher) -> Self {
match hasher {
Hasher::Blake2_256(_) => super::HasherKind::Blake2_256,
Hasher::Blake2_128(_) => super::HasherKind::Blake2_128,
Hasher::Blake2_128Concat(_) => super::HasherKind::Blake2_128Concat,
Hasher::Twox256(_) => super::HasherKind::Twox256,
Hasher::Twox128(_) => super::HasherKind::Twox128,
Hasher::Twox64Concat(_) => super::HasherKind::Twox64Concat,
Hasher::Identity(_) => super::HasherKind::Identity,
}
}
}
fn get_module_instance(
instance: Option<syn::Ident>,
instantiable: Option<syn::Ident>,
default_instance: Option<syn::Ident>,
) -> syn::Result<Option<super::ModuleInstanceDef>> {
let right_syntax = "Should be $I: $Instance = $DefaultInstance";
if instantiable.as_ref().map_or(false, |i| i != "Instance") {
let msg = format!(
"Instance trait must be named `Instance`, other names are no longer supported, because \
it is now defined at frame_support::traits::Instance. Expect `Instance` found `{}`",
instantiable.as_ref().unwrap(),
);
return Err(syn::Error::new(instantiable.span(), msg))
}
match (instance, instantiable, default_instance) {
(Some(instance), Some(instantiable), default_instance) =>
Ok(Some(super::ModuleInstanceDef {
instance_generic: instance,
instance_trait: instantiable,
instance_default: default_instance,
})),
(None, None, None) => Ok(None),
(Some(instance), None, _) => Err(syn::Error::new(
instance.span(),
format!("Expect instantiable trait bound for instance: {}. {}", instance, right_syntax),
)),
(None, Some(instantiable), _) => Err(syn::Error::new(
instantiable.span(),
format!(
"Expect instance generic for bound instantiable: {}. {}",
instantiable, right_syntax,
),
)),
(None, _, Some(default_instance)) => Err(syn::Error::new(
default_instance.span(),
format!(
"Expect instance generic for default instance: {}. {}",
default_instance, right_syntax,
),
)),
}
}
pub fn parse(input: syn::parse::ParseStream) -> syn::Result<super::DeclStorageDef> {
use syn::parse::Parse;
let def = StorageDefinition::parse(input)?;
let module_instance =
get_module_instance(def.mod_instance, def.mod_instantiable, def.mod_default_instance)?;
let mut extra_genesis_config_lines = vec![];
let mut extra_genesis_build = None;
for line in def
.extra_genesis
.inner
.into_iter()
.flat_map(|o| o.content.content.lines.inner.into_iter())
{
match line {
AddExtraGenesisLineEnum::AddExtraGenesisLine(def) => {
extra_genesis_config_lines.push(super::ExtraGenesisLineDef {
attrs: def.attrs.inner,
name: def.extra_field.content,
typ: def.extra_type,
default: def.default_value.inner.map(|o| o.expr),
});
},
AddExtraGenesisLineEnum::AddExtraGenesisBuild(def) => {
if extra_genesis_build.is_some() {
return Err(syn::Error::new(
def.span(),
"Only one build expression allowed for extra genesis",
))
}
extra_genesis_build = Some(def.expr.content);
},
}
}
let storage_lines = parse_storage_line_defs(def.content.content.inner.into_iter())?;
Ok(super::DeclStorageDef {
generate_storage_info: def.generate_storage_info.inner.is_some(),
hidden_crate: def.hidden_crate.inner.map(|i| i.ident.content),
visibility: def.visibility,
module_name: def.module_ident,
store_trait: def.ident,
module_runtime_generic: def.mod_param_generic,
module_runtime_trait: def.mod_param_bound,
where_clause: def.where_clause,
crate_name: def.crate_ident,
module_instance,
extra_genesis_build,
extra_genesis_config_lines,
storage_lines,
})
}
fn parse_storage_line_defs(
defs: impl Iterator<Item = DeclStorageLine>,
) -> syn::Result<Vec<super::StorageLineDef>> {
let mut storage_lines = Vec::<super::StorageLineDef>::new();
for line in defs {
let getter = line.getter.inner.map(|o| o.getfn.content.ident);
let config = if let Some(config) = line.config.inner {
if let Some(ident) = config.expr.content {
Some(ident)
} else if let Some(ref ident) = getter {
Some(ident.clone())
} else {
return Err(syn::Error::new(
config.span(),
"Invalid storage definition, couldn't find config identifier: storage must \
either have a get identifier `get(fn ident)` or a defined config identifier \
`config(ident)`",
))
}
} else {
None
};
if let Some(ref config) = config {
storage_lines.iter().filter_map(|sl| sl.config.as_ref()).try_for_each(
|other_config| {
if other_config == config {
Err(syn::Error::new(
config.span(),
"`config()`/`get()` with the same name already defined.",
))
} else {
Ok(())
}
},
)?;
}
let max_values = match &line.storage_type {
DeclStorageType::Map(_) | DeclStorageType::DoubleMap(_) | DeclStorageType::NMap(_) =>
line.max_values.inner.map(|i| i.expr.content),
DeclStorageType::Simple(_) =>
if let Some(max_values) = line.max_values.inner {
let msg = "unexpected max_values attribute for storage value.";
let span = max_values.max_values_keyword.span();
return Err(syn::Error::new(span, msg))
} else {
Some(syn::parse_quote!(1u32))
},
};
let span = line.storage_type.span();
let no_hasher_error = || {
syn::Error::new(
span,
"Default hasher has been removed, use explicit hasher(blake2_128_concat) instead.",
)
};
let storage_type = match line.storage_type {
DeclStorageType::Map(map) => super::StorageLineTypeDef::Map(super::MapDef {
hasher: map.hasher.inner.ok_or_else(no_hasher_error)?.into(),
key: map.key,
value: map.value,
}),
DeclStorageType::DoubleMap(map) =>
super::StorageLineTypeDef::DoubleMap(Box::new(super::DoubleMapDef {
hasher1: map.hasher1.inner.ok_or_else(no_hasher_error)?.into(),
hasher2: map.hasher2.inner.ok_or_else(no_hasher_error)?.into(),
key1: map.key1,
key2: map.key2,
value: map.value,
})),
DeclStorageType::NMap(map) => super::StorageLineTypeDef::NMap(super::NMapDef {
hashers: map
.storage_keys
.inner
.iter()
.map(|pair| Ok(pair.hasher.inner.clone().ok_or_else(no_hasher_error)?.into()))
.collect::<Result<Vec<_>, syn::Error>>()?,
keys: map.storage_keys.inner.iter().map(|pair| pair.key.clone()).collect(),
value: map.value,
}),
DeclStorageType::Simple(expr) => super::StorageLineTypeDef::Simple(expr),
};
storage_lines.push(super::StorageLineDef {
attrs: line.attrs.inner,
visibility: line.visibility,
name: line.name,
getter,
config,
max_values,
build: line.build.inner.map(|o| o.expr.content),
default_value: line.default_value.inner.map(|o| o.expr),
storage_type,
})
}
Ok(storage_lines)
}