-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathmetadata.rs
584 lines (529 loc) · 17.5 KB
/
metadata.rs
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Copyright 2019-2021 Parity Technologies (UK) Ltd. and Supercomputing Systems AG
// and Integritee AG.
// This file is part of subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
//! Handle substrate chain metadata
//!
//! This file is mostly subxt.
use crate::{storage::GetStorage, Encoded};
use codec::{Encode, Error as CodecError};
use frame_metadata::{
PalletConstantMetadata, RuntimeMetadata, RuntimeMetadataLastVersion, RuntimeMetadataPrefixed,
StorageEntryMetadata, META_RESERVED,
};
use frame_support::serde::Serialize;
use scale_info::{form::PortableForm, Type, Variant};
use sp_core::storage::StorageKey;
use std::{collections::HashMap, convert::TryFrom};
/// Metadata error.
#[derive(Debug, thiserror::Error)]
pub enum MetadataError {
/// Module is not in metadata.
#[error("Pallet {0} not found")]
PalletNotFound(String),
/// Pallet is not in metadata.
#[error("Pallet index {0} not found")]
PalletIndexNotFound(u8),
/// Call is not in metadata.
#[error("Call {0} not found")]
CallNotFound(&'static str),
/// Event is not in metadata.
#[error("Pallet {0}, Event {0} not found")]
EventNotFound(u8, u8),
/// Event is not in metadata.
#[error("Pallet {0}, Error {0} not found")]
ErrorNotFound(u8, u8),
/// Storage is not in metadata.
#[error("Storage {0} not found")]
StorageNotFound(&'static str),
/// Storage type does not match requested type.
#[error("Storage type error")]
StorageTypeError,
#[error("Map value type error")]
MapValueTypeError,
/// Default error.
#[error("Failed to decode default: {0}")]
DefaultError(CodecError),
/// Failure to decode constant value.
#[error("Failed to decode constant value: {0}")]
ConstantValueError(CodecError),
/// Constant is not in metadata.
#[error("Constant {0} not found")]
ConstantNotFound(&'static str),
#[error("Type {0} missing from type registry")]
TypeNotFound(u32),
}
/// Runtime metadata.
#[derive(Clone, Debug)]
pub struct Metadata {
metadata: RuntimeMetadataLastVersion,
pallets: HashMap<String, PalletMetadata>,
events: HashMap<(u8, u8), EventMetadata>,
errors: HashMap<(u8, u8), ErrorMetadata>,
}
impl Metadata {
/// Returns a reference to [`PalletMetadata`].
pub fn pallet(&self, name: &'static str) -> Result<&PalletMetadata, MetadataError> {
self.pallets
.get(name)
.ok_or_else(|| MetadataError::PalletNotFound(name.to_string()))
}
/// Returns the metadata for the event at the given pallet and event indices.
pub fn event(
&self,
pallet_index: u8,
event_index: u8,
) -> Result<&EventMetadata, MetadataError> {
let event = self
.events
.get(&(pallet_index, event_index))
.ok_or(MetadataError::EventNotFound(pallet_index, event_index))?;
Ok(event)
}
/// Returns the metadata for all events of a given pallet
pub fn events(&self, pallet_index: u8) -> Vec<EventMetadata> {
self.events
.clone()
.into_iter()
.filter(|(k, _v)| k.0 == pallet_index)
.map(|(_k, v)| v)
.collect()
}
/// Returns the metadata for the error at the given pallet and error indices.
pub fn error(
&self,
pallet_index: u8,
error_index: u8,
) -> Result<&ErrorMetadata, MetadataError> {
let error = self
.errors
.get(&(pallet_index, error_index))
.ok_or(MetadataError::ErrorNotFound(pallet_index, error_index))?;
Ok(error)
}
/// Returns the metadata for all errors of a given pallet
pub fn errors(&self, pallet_index: u8) -> Vec<ErrorMetadata> {
self.errors
.clone()
.into_iter()
.filter(|(k, _v)| k.0 == pallet_index)
.map(|(_k, v)| v)
.collect()
}
/// Resolve a type definition.
pub fn resolve_type(&self, id: u32) -> Option<&Type<PortableForm>> {
self.metadata.types.resolve(id)
}
/// Return the runtime metadata.
pub fn runtime_metadata(&self) -> &RuntimeMetadataLastVersion {
&self.metadata
}
pub fn pretty_format(metadata: &RuntimeMetadataPrefixed) -> Option<String> {
let buf = Vec::new();
let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
let mut ser = serde_json::Serializer::with_formatter(buf, formatter);
metadata.serialize(&mut ser).unwrap();
String::from_utf8(ser.into_inner()).ok()
}
pub fn print_overview(&self) {
let mut string = String::new();
for (name, pallet) in &self.pallets {
string.push_str(name.as_str());
string.push('\n');
for storage in pallet.storage.keys() {
string.push_str(" s ");
string.push_str(storage.as_str());
string.push('\n');
}
for call in pallet.calls.keys() {
string.push_str(" c ");
string.push_str(call.as_str());
string.push('\n');
}
for constant in pallet.constants.keys() {
string.push_str(" cst ");
string.push_str(constant.as_str());
string.push('\n');
}
for event in self.events(pallet.index) {
string.push_str(" e ");
string.push_str(&event.event);
string.push('\n');
}
for error in self.errors(pallet.index) {
string.push_str(" err ");
string.push_str(&error.error);
string.push('\n');
}
}
println!("{}", string);
}
pub fn print_pallets(&self) {
for m in self.pallets.values() {
m.print()
}
}
pub fn print_pallets_with_calls(&self) {
for m in self.pallets.values() {
if !m.calls.is_empty() {
m.print_calls();
}
}
}
pub fn print_pallets_with_constants(&self) {
for m in self.pallets.values() {
if !m.constants.is_empty() {
m.print_constants();
}
}
}
pub fn print_pallet_with_storages(&self) {
for m in self.pallets.values() {
if !m.storage.is_empty() {
m.print_storages();
}
}
}
pub fn print_pallets_with_events(&self) {
for pallet in self.pallets.values() {
println!(
"----------------- Events for Pallet: {} -----------------\n",
pallet.name
);
for m in self.events(pallet.index) {
m.print();
}
println!();
}
}
pub fn print_pallets_with_errors(&self) {
for pallet in self.pallets.values() {
println!(
"----------------- Errors for Pallet: {} -----------------\n",
pallet.name
);
for m in self.errors(pallet.index) {
m.print();
}
println!();
}
}
}
#[derive(Clone, Debug)]
pub struct PalletMetadata {
pub index: u8,
pub name: String,
pub calls: HashMap<String, u8>,
pub storage: HashMap<String, StorageEntryMetadata<PortableForm>>,
pub constants: HashMap<String, PalletConstantMetadata<PortableForm>>,
}
impl PalletMetadata {
pub fn encode_call<C>(&self, call_name: &'static str, args: C) -> Result<Encoded, MetadataError>
where
C: Encode,
{
let fn_index = self
.calls
.get(call_name)
.ok_or(MetadataError::CallNotFound(call_name))?;
let mut bytes = vec![self.index, *fn_index];
bytes.extend(args.encode());
Ok(Encoded(bytes))
}
pub fn storage(
&self,
key: &'static str,
) -> Result<&StorageEntryMetadata<PortableForm>, MetadataError> {
self.storage
.get(key)
.ok_or(MetadataError::StorageNotFound(key))
}
/// Get a constant's metadata by name
pub fn constant(
&self,
key: &'static str,
) -> Result<&PalletConstantMetadata<PortableForm>, MetadataError> {
self.constants
.get(key)
.ok_or(MetadataError::ConstantNotFound(key))
}
pub fn print(&self) {
println!(
"----------------- Pallet: '{}' -----------------\n",
self.name
);
println!("Pallet id: {}", self.index);
//self.print_calls();
}
pub fn print_calls(&self) {
println!(
"----------------- Calls for Pallet: {} -----------------\n",
self.name
);
for (name, index) in &self.calls {
println!("Name: {}, index {}", name, index);
}
println!();
}
pub fn print_constants(&self) {
println!(
"----------------- Constants for Pallet: {} -----------------\n",
self.name
);
for (name, constant) in &self.constants {
println!(
"Name: {}, Type {:?}, Value {:?}",
name, constant.ty, constant.value
);
}
println!();
}
pub fn print_storages(&self) {
println!(
"----------------- Storages for Pallet: {} -----------------\n",
self.name
);
for (name, storage) in &self.storage {
println!(
"Name: {}, Modifier: {:?}, Type {:?}, Default {:?}",
name, storage.modifier, storage.ty, storage.default
);
}
println!();
}
}
#[derive(Clone, Debug)]
pub struct EventMetadata {
pallet: String,
event: String,
variant: Variant<PortableForm>,
}
impl EventMetadata {
/// Get the name of the pallet from which the event was emitted.
pub fn pallet(&self) -> &str {
&self.pallet
}
/// Get the name of the pallet event which was emitted.
pub fn event(&self) -> &str {
&self.event
}
/// Get the type def variant for the pallet event.
pub fn variant(&self) -> &Variant<PortableForm> {
&self.variant
}
pub fn print(&self) {
println!("Name: {}", self.event());
println!("Variant: {:?}", self.variant());
println!()
}
}
#[derive(Clone, Debug)]
pub struct ErrorMetadata {
pallet: String,
error: String,
variant: Variant<PortableForm>,
}
impl ErrorMetadata {
/// Get the name of the pallet from which the error originates.
pub fn pallet(&self) -> &str {
&self.pallet
}
/// Get the name of the specific pallet error.
pub fn error(&self) -> &str {
&self.error
}
/// Get the description of the specific pallet error.
pub fn description(&self) -> &[String] {
self.variant.docs()
}
pub fn print(&self) {
println!("Name: {}", self.error());
println!("Description: {:?}", self.description());
println!()
}
}
#[derive(Debug, thiserror::Error)]
pub enum InvalidMetadataError {
#[error("Invalid prefix")]
InvalidPrefix,
#[error("Invalid version")]
InvalidVersion,
#[error("Type {0} missing from type registry")]
MissingType(u32),
#[error("Type {0} was not a variant/enum type")]
TypeDefNotVariant(u32),
}
impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
type Error = InvalidMetadataError;
fn try_from(metadata: RuntimeMetadataPrefixed) -> Result<Self, Self::Error> {
if metadata.0 != META_RESERVED {
return Err(InvalidMetadataError::InvalidPrefix);
}
let metadata = match metadata.1 {
RuntimeMetadata::V14(meta) => meta,
_ => return Err(InvalidMetadataError::InvalidVersion),
};
let get_type_def_variant = |type_id: u32| {
let ty = metadata
.types
.resolve(type_id)
.ok_or(InvalidMetadataError::MissingType(type_id))?;
if let scale_info::TypeDef::Variant(var) = ty.type_def() {
Ok(var)
} else {
Err(InvalidMetadataError::TypeDefNotVariant(type_id))
}
};
let pallets = metadata
.pallets
.iter()
.map(|pallet| {
let calls = pallet.calls.as_ref().map_or(Ok(HashMap::new()), |call| {
let type_def_variant = get_type_def_variant(call.ty.id())?;
let calls = type_def_variant
.variants()
.iter()
.map(|v| (v.name().clone(), v.index()))
.collect();
Ok(calls)
})?;
let storage = pallet.storage.as_ref().map_or(HashMap::new(), |storage| {
storage
.entries
.iter()
.map(|entry| (entry.name.clone(), entry.clone()))
.collect()
});
let constants = pallet
.constants
.iter()
.map(|constant| (constant.name.clone(), constant.clone()))
.collect();
let pallet_metadata = PalletMetadata {
index: pallet.index,
name: pallet.name.to_string(),
calls,
storage,
constants,
};
Ok((pallet.name.to_string(), pallet_metadata))
})
.collect::<Result<_, _>>()?;
let pallet_events = metadata
.pallets
.iter()
.filter_map(|pallet| {
pallet.event.as_ref().map(|event| {
let type_def_variant = get_type_def_variant(event.ty.id())?;
Ok((pallet, type_def_variant))
})
})
.collect::<Result<Vec<_>, _>>()?;
let events = pallet_events
.iter()
.flat_map(|(pallet, type_def_variant)| {
type_def_variant.variants().iter().map(move |var| {
let key = (pallet.index, var.index());
let value = EventMetadata {
pallet: pallet.name.clone(),
event: var.name().clone(),
variant: var.clone(),
};
(key, value)
})
})
.collect();
let pallet_errors = metadata
.pallets
.iter()
.filter_map(|pallet| {
pallet.error.as_ref().map(|error| {
let type_def_variant = get_type_def_variant(error.ty.id())?;
Ok((pallet, type_def_variant))
})
})
.collect::<Result<Vec<_>, _>>()?;
let errors = pallet_errors
.iter()
.flat_map(|(pallet, type_def_variant)| {
type_def_variant.variants().iter().map(move |var| {
let key = (pallet.index, var.index());
let value = ErrorMetadata {
pallet: pallet.name.clone(),
error: var.name().clone(),
variant: var.clone(),
};
(key, value)
})
})
.collect();
Ok(Self {
metadata,
pallets,
events,
errors,
})
}
}
/// Get the storage keys corresponding to a storage
///
/// This is **not** part of subxt.
impl Metadata {
pub fn storage_value_key(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
) -> Result<StorageKey, MetadataError> {
Ok(self
.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_value(storage_prefix)?
.key())
}
pub fn storage_map_key<K: Encode>(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
map_key: K,
) -> Result<StorageKey, MetadataError> {
Ok(self
.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_map::<K>(storage_prefix)?
.key(map_key))
}
pub fn storage_map_key_prefix(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
) -> Result<StorageKey, MetadataError> {
self.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_map_prefix(storage_prefix)
}
pub fn storage_double_map_key<K: Encode, Q: Encode>(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
first: K,
second: Q,
) -> Result<StorageKey, MetadataError> {
Ok(self
.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_double_map::<K, Q>(storage_prefix)?
.key(first, second))
}
}