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
#[macro_export]
macro_rules! impl_bits_conversion {
($ident: ident from $type: ty) => {
impl From<$ident> for $type {
fn from(value: $ident) -> Self {
value.bits()
}
}
impl TryFrom<$type> for $ident {
type Error = $type;
fn try_from(value: $type) -> Result<Self, $type> {
Self::from_bits(value).ok_or(value)
}
}
impl Encode for $ident {
fn encode(&self) -> Vec<u8> {
<$type>::encode(&self.clone().into())
}
}
impl Decode for $ident {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
let decoded = <$type>::decode(input)?;
Self::from_bits(decoded).ok_or("Invalid value".into())
}
}
impl MaxEncodedLen for $ident {
fn max_encoded_len() -> usize {
<$type>::max_encoded_len()
}
}
};
}
#[macro_export]
macro_rules! pub_for_test {
($(#[$meta:meta])* $vis: vis $ident: ident $($val: tt)*) => {
#[cfg(test)]
$(#[$meta])*
pub $ident $($val)*
#[cfg(not(test))]
$(#[$meta])*
$vis $ident $($val)*
}
}
#[macro_export]
macro_rules! field_accessor {
($self: ident, () with $($rest: tt)*) => {
() $($rest)*
};
($self: ident, $lit: literal with $($rest: tt)*) => {
$lit $($rest)*
};
($self: ident, { $expr: expr } with $($rest: tt)*) => {
$expr($self) $($rest)*
};
($self: ident, $($ident: tt $(($($call: tt),*))?).+ with $($rest: tt)*) => {
$self.$($ident $(($($call)*))*).+ $($rest)*
};
}
#[macro_export]
macro_rules! impl_to_state_change {
($type: ident) => {
impl<T: $crate::common::TypesAndLimits> $crate::common::ToStateChange<T> for $type<T> {
fn to_state_change(&self) -> $crate::common::StateChange<'_, T> {
$crate::common::StateChange::$type(sp_std::borrow::Cow::Borrowed(self))
}
}
};
}
#[macro_export]
macro_rules! impl_action {
(
$type: ident for $target: ty: with
$($len_field: tt $(($($len_call: tt),*))?).+ as len,
$($target_field: tt $(($($target_call: tt),*))?).+ as target
) => {
$crate::impl_to_state_change! { $type }
impl<T: $crate::common::TypesAndLimits> $crate::util::Action for $type<T> {
type Target = $target;
fn target(&self) -> $target {
$crate::field_accessor!(self, $($target_field $(($($target_call)*))*).+ with)
}
fn len(&self) -> u32 {
$crate::field_accessor!(self, $($len_field $(($($len_call)*))*).+ with as u32)
}
}
};
(
$type: ident for $target: ty: with
$($len_field: tt $(($($len_call: tt),*))?).+ as len,
$($target_field: tt $(($($target_call: tt),*))?).+ as target
no_state_change
) => {
impl<T: $crate::common::TypesAndLimits> $crate::util::Action for $type<T> {
type Target = $target;
fn target(&self) -> $target {
$crate::field_accessor!(self, $($target_field $(($($target_call)*))*).+ with)
}
fn len(&self) -> u32 {
$crate::field_accessor!(self, $($len_field $(($($len_call)*))*).+ with as u32)
}
}
};
(
for $target: ty:
$(
$type: ident with
$($len_field: tt $(($($len_call: tt),*))?).+ as len,
$($target_field: tt $(($($target_call: tt),*))?).+ as target
$($rest: tt)?
),+
) => {
$(
$crate::impl_action! {
$type for $target:
with $($len_field $(($($len_call),*))*).+ as len,
$($target_field $(($($target_call)*))*).+ as target
$($rest)?
}
)+
};
}
#[macro_export]
macro_rules! impl_action_with_nonce {
($type: ident for $($token: tt)*) => {
$crate::impl_action! { $type for $($token)* }
impl<T: $crate::common::TypesAndLimits> $crate::util::ActionWithNonce<T> for $type<T> {
fn nonce(&self) -> T::BlockNumber {
self.nonce
}
}
};
(for $target: ty:
$(
$type: ident with $($len: tt $(($($len_call: tt),*))?).+ as len,
$($target_field: tt $(($($target_call: tt),*))?).+ as target
),+
) => {
$(
$crate::impl_action_with_nonce! {
$type for $target:
with $($len $(($($len_call),*))*).+ as len,
$($target_field $(($($target_call)*))*).+ as target
}
)+
};
}
#[macro_export]
macro_rules! impl_authorize_target {
(for $target: ty: $actor: ident $(with Value=$target_value_ty: ty =>)? fn($self: ident, $key_var: pat, $action_var: pat, $target_value_var: pat) $body: block) => {
impl<T> AuthorizeTarget<T, $target, DidKey> for $actor where T: Config, $target: crate::util::Associated<T$(, Value=$target_value_ty)?> {
fn ensure_authorizes_target<A>(
&$self,
$key_var: &DidKey,
$action_var: &A,
$target_value_var: Option<&<$target as crate::util::Associated<T>>::Value>,
) -> DispatchResult
where
A: crate::util::Action<Target = $target> {
$body
Ok(())
}
}
impl<T> AuthorizeTarget<T, $target, DidMethodKey> for $actor where T: Config, $target: crate::util::Associated<T$(, Value=$target_value_ty)?> {
fn ensure_authorizes_target<A>(
&$self,
$key_var: &DidMethodKey,
$action_var: &A,
$target_value_var: Option<&<$target as crate::util::Associated<T>>::Value>,
) -> DispatchResult
where
A: crate::util::Action<Target = $target> {
$body
Ok(())
}
}
};
}
#[macro_export]
macro_rules! impl_tuple {
(
@ $method: ident$(<$($gen: ident),+>)?
($($arg: ident: $arg_ty: ty),*) => using $combine: ident for $main: ident)
=> { $main::$method::<$($($gen),+)*>($($arg),*)
};
(
@ $method: ident$(<$($gen: ident),+>)?
($($arg: ident: $arg_ty: ty),*) => using $combine: ident for $main: ident $($ty: ident)+) => {
$main::$method::<$($($gen),+)*>($($arg),*).$combine($crate::impl_tuple!(@ $method$(<$($gen),+>)*($($arg: $arg_ty),*) => using $combine for $($ty)*))
};
(
$trait: ident$(<$($trait_gen: ident),+>)?
::$method: ident$(<$($gen: ident),+>)?
($($arg: ident: $arg_ty: ty),*) -> $ret_ty: ty => using $combine: ident for $($ty: ident)*
$(=> where $($where_tt: tt)+)?
) => {
impl<$($ty),+ $(,$($trait_gen),+)*> $trait$(<$($trait_gen),+>)* for ($($ty),+)
where $($ty: $trait),+
{
fn $method<$($($gen),+)*>($($arg: $arg_ty),*) -> $ret_ty $(where $($where_tt)+)* {
$crate::impl_tuple!(@ $method($($arg: $arg_ty),*) => using $combine for $($ty)*)
}
}
}
}
#[macro_export]
macro_rules! hex_debug {
($ty: ty) => {
impl core::fmt::Debug for $ty {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "0x{}", ::hex::encode(&self.0[..]))
}
}
};
}
#[macro_export]
macro_rules! deposit_indexed_event {
($event: ident($($value: expr),+) over $($index: expr),+) => {{
<frame_system::Pallet<T>>::deposit_event_indexed(
&[$(<<T as frame_system::Config>::Hashing as sp_core::Hasher>::hash(&$index[..])),+],
<T as Config>::Event::from(Event::$event($($value),+)).into()
);
}};
($event: ident($($value: expr),+)) => {
$crate::deposit_indexed_event!($event($($value),+) over $($value),+)
}
}
#[macro_export]
macro_rules! impl_wrapper {
(no_wrapper_from_type $wrapper: ident$(<$($gen: ident),+>)? $(where $($where_ty: ident: $bound: path),+ =>)? ($type: ty) $(,$($rest: tt)*)?) => {
$($crate::impl_encode_decode_wrapper_tests! { $wrapper($type), $($rest)* })?
impl$(<$($gen),+>)* From<$wrapper$(<$($gen),+>)*> for $type $(where $($where_ty: $bound),+)* {
fn from(wrapper: $wrapper$(<$($gen),+>)*) -> $type {
wrapper.0
}
}
impl$(<$($gen),+>)* core::ops::Deref for $wrapper$(<$($gen),+>)* $(where $($where_ty: $bound),+)* {
type Target = $type;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl$(<$($gen),+>)* core::ops::DerefMut for $wrapper$(<$($gen),+>)* $(where $($where_ty: $bound),+)* {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
};
($wrapper: ident$(<$($gen: ident),+>)? $(where $($where_ty: ident: $bound: path),+ =>)? ($type: ty) $(,$($rest: tt)*)?) => {
$crate::impl_wrapper! { no_wrapper_from_type $wrapper$(<$($gen),+>)* $(where $($where_ty: $bound),+ =>)* ($type) $(,$($rest)*)? }
$crate::impl_wrapper_from_type_conversion! { from $type => $wrapper$(<$($gen),+>)* $(where $($where_ty: $bound),+)* }
};
}
#[macro_export]
macro_rules! impl_wrapper_from_type_conversion {
(from $type: ty => $wrapper: ident$(<$($gen: ident),+>)? $(where $($where_ty: ident: $bound: path),+)? ) => {
impl$(<$($gen),+>)* From<$type> for $wrapper$(<$($gen),+>)* $(where $($where_ty: $bound),+)* {
fn from(value: $type) -> $wrapper$(<$($gen),+>)* {
$wrapper(value.into())
}
}
}
}
#[macro_export]
macro_rules! impl_wrapper_type_info {
($wrapper: ident($type: ty)) => {
impl scale_info::TypeInfo for $wrapper {
type Identity = Self;
fn type_info() -> scale_info::Type {
scale_info::Type::builder()
.path(scale_info::Path::new(
core::stringify!($wrapper),
core::stringify!($wrapper),
))
.composite(scale_info::build::Fields::unnamed().field(|f| f.ty::<$type>()))
}
}
};
}
#[macro_export]
macro_rules! def_state_change {
($(#[$meta:meta])* $name: ident: $($mod: ident::$type: ident),+) => {
$(#[$meta])*
#[
derive(
scale_info_derive::TypeInfo,
codec::Encode,
codec::Decode,
frame_support::DebugNoBound,
frame_support::CloneNoBound,
frame_support::PartialEqNoBound
)
]
#[scale_info(skip_type_params(T))]
#[scale_info(omit_prefix)]
pub enum $name<'a, T: $crate::common::TypesAndLimits> {
$($type(sp_std::borrow::Cow<'a, $crate::modules::$mod::$type<T>>)),+
}
}
}
#[macro_export]
macro_rules! impl_encode_decode_wrapper_tests {
($wrapper: ident($type: ty)) => {};
($wrapper: ident($type: ty), with tests as $mod: ident) => {
$crate::impl_encode_decode_wrapper_tests!(
$wrapper($type), for rand use rand::random(), with tests as $mod
);
};
($wrapper: ident($type: ty), for rand use $rand: expr, with tests as $mod: ident) => {
#[cfg(test)]
pub mod $mod {
use super::*;
use codec::{Decode, Encode};
#[test]
fn encode_decode() {
let type_value: $type = $rand;
let wrapper_value = $wrapper(type_value.clone());
let encoded_wrapper = Encode::encode(&wrapper_value);
let encoded_type = Encode::encode(&type_value);
assert_eq!(encoded_type, encoded_wrapper);
let decoded_wrapper: $wrapper = Decode::decode(&mut &encoded_wrapper[..]).unwrap();
assert_eq!(decoded_wrapper, wrapper_value);
let decoded_wrapper: $wrapper = Decode::decode(&mut &encoded_type[..]).unwrap();
assert_eq!(decoded_wrapper, wrapper_value);
let decoded_type: $type = Decode::decode(&mut &encoded_type[..]).unwrap();
assert_eq!(decoded_type, type_value);
let decoded_type: $type = Decode::decode(&mut &encoded_wrapper[..]).unwrap();
assert_eq!(decoded_type, type_value);
}
#[test]
fn deref() {
let type_value: $type = $rand;
let wrapper_value = $wrapper(type_value.clone());
assert_eq!(*wrapper_value, type_value);
}
#[test]
fn deref_mut() {
let type_value: $type = $rand;
let mut wrapper_value = $wrapper($rand);
*wrapper_value = type_value.clone();
assert_eq!(wrapper_value.0, type_value);
}
#[test]
fn from_into() {
let type_value: $type = $rand;
let wrapper_value = $wrapper(type_value.clone());
assert_eq!(<$wrapper>::from(type_value.clone()), wrapper_value);
assert_eq!(<$type>::from(wrapper_value), type_value);
}
}
};
}
#[cfg(feature = "runtime-benchmarks")]
#[macro_export]
macro_rules! def_test_pair {
(sr25519, $seed: expr) => {{
use sp_core::Pair;
let pair = sp_core::sr25519::Pair::from_seed($seed);
struct TestSr25519Pair {
pair: sp_core::sr25519::Pair,
}
impl TestSr25519Pair {
fn sign(&self, msg: &[u8]) -> sp_core::sr25519::Signature {
use rand_chacha::rand_core::SeedableRng;
use schnorrkel::{context::attach_rng, *};
let mut transcript = merlin::Transcript::new(b"SigningContext");
transcript.append_message(b"", b"substrate");
transcript.append_message(b"sign-bytes", msg);
let context = attach_rng(transcript, rand_chacha::ChaChaRng::from_seed([10u8; 32]));
let sk = SecretKey::from_bytes(&self.pair.to_raw_vec()[..]).unwrap();
sk.sign(
context,
&PublicKey::from_bytes(&self.pair.public()[..]).unwrap(),
)
.into()
}
fn public(&self) -> sp_core::sr25519::Public {
self.pair.public()
}
}
TestSr25519Pair { pair }
}};
(ed25519, $seed: expr) => {{
use sp_core::Pair;
sp_core::ed25519::Pair::from_seed($seed)
}};
(secp256k1, $seed: expr) => {
$crate::common::get_secp256k1_keypair_struct($seed)
};
}
#[cfg(feature = "runtime-benchmarks")]
#[macro_export]
macro_rules! bench_with_all_pairs {
(
with_pairs:
$(
$bench_name_sr25519: ident for sr25519,
$bench_name_ed25519: ident for ed25519,
$bench_name_secp256k1: ident for secp256k1
{
$({ $($init: tt)* })?
let $pair: ident as Pair;
$($body: tt)+
}:
$($call: block)?
$($dispatch: ident($($dispatch_arg: expr),+))?
verify { $($verification: tt)* }
)+
$(;
standard:
$($other: tt)*
)?
) => {
benchmarks! {
where_clause { where T: core::fmt::Debug }
$(
$bench_name_sr25519 {
$($($init)*)*
#[allow(unused_imports)]
use sp_core::Pair;
let $pair = $crate::def_test_pair!(sr25519, &[4; 32]);
$($body)+
}: $({ $call })* $($dispatch($($dispatch_arg),+))* verify { $($verification)* }
$bench_name_ed25519 {
$($($init)*)*
#[allow(unused_imports)]
use sp_core::Pair;
let $pair = $crate::def_test_pair!(ed25519, &[3; 32]);
$($body)+
}: $({ $call })* $($dispatch($($dispatch_arg),+))* verify { $($verification)* }
$bench_name_secp256k1 {
$($($init)*)*
#[allow(unused_imports)]
use sp_core::Pair;
let $pair = $crate::def_test_pair!(secp256k1, &[2; 32]);
$($body)+
}: $({ $call })* $($dispatch($($dispatch_arg),+))* verify { $($verification)* }
)+
$($($other)*)?
}
};
}