mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 19:51:05 +00:00
Generate runtime API from metadata (#294)
* Remove test macro * Remove client crate * Create tests crate and move pallet specific tests there * Extract client, remove metadata and extra, more demolition * Update substrate dependencies to git dependencies * Remove Store stuff for now * Comment out some Call usages * Add back Runtime trait coped from original System trait * Make subxt lib compile * Delete old proc macros and copy over type generation from chameleon * WIP make transfer balance test pass * Change to subxt attribute macro * WIP provide user defined type substitutes * User defined type substitutes compile * WIP submitting transactions * WIP transfer balance test * Fix macro * Cargo fmt * WIP generating storage hashers * WIP add AccountData trait for fetching the nonce * Support single type storage map keys * WIP impl AccountInfo retrieval * Fix up storage struct generation * Implement AccountData triait directly on storage entry * Borrow storage map key and convert account id * Implement storage fetch client methods * Remove legacy metadata storage key construction * Rename CheckEra to CheckMortality * Substitute perthings types for compact impls * Fmt * Downgrade dyn-clone for cargo-contract compat * Scale-fo 1.0 * scale-info 1.0 * Remove special range handling * Restore wildcard type params * Frame metadata 14.0 * WIP decoding events * WIP more dynamically decoding events * Fmt * Decode events, handle errors * Uncomment some tests * Remove unused get_mod function * Fix some warnings * Fix some more warnings * Fix some more warnings * Add tests mod * Rename node-runtime tests mod to frame * Fix some warnings * Fmt * WIP generate storage client with getters * Storage client compiling * Generate storage client api * Fix up system account query account ids * WIP generating tx api fns * Only generate tx api fields when calls available * Fix tx api call fns * Fmt * WIP generate event structs * call functions not async * Derive Eq for comparison on generated types * Generate event structs * Fix call name * Fmt * Update node runtime metadata to substrate c000780db * Download latest substrate release for integration testing * Fix event decoding * Remove unused imports * Fix plain storage access, total_issuance pass * Fmt * Restore contracts tests * Backoff connecting to substrate node * Add required TypeInfo impls for local SignedExtension impls * Remove unnecessary assert formatting * Fix handling of DispatchError * Refactor contracts tests * Troubleshooting contract not found * Remove more client feature stuff * Fix dynamic event variant decoding, write consumed index to output * Fmt * Use substrate branch with heavy dependency removed * Remove sp-rcp dependency, define types locally * Ignore cargo timeing files * Use my branch for substrate test deps * Fix storage key type gen * Comment out fetching contract info * Add key iteration, extract storage client from main client * Debugging key generation * Use substrate master branch * Fix call test * Remove TypeSegmenter and dynclone dependency * Publicly expose Rpc mod * Unused import warnings * Add getter for runtime metadata * Add pallet and event indices for raw events * Add is_call and is_event convenience trait functions * Add missing docs * Refactor tests crate * Restore remaining client tests * Fmt * Fix warnings * Restore get_mod as test helper and fmt * Use client references for api calls * Fix api usages with methods * Use Bytes for RawEvent debug * Update metadata * Restoring some Balances tests * Populate runtime storage metadata * Restore balances lock test * Restore Balances error test * Fmt * Restore transfer subscription API * Staking test * Restore another staking test * Restore another staking test * Restore another staking test * Partially restore chill_works_for_controller_only staking test * Fix fetching Optional storage entries * Restore staking bond test * Restore remaining staking tests * Fmt * Restore sudo tests * Add some system tests * Fmt * Resolve some todos * Remove pass through rpc methods on Client, expose via rpc() getter * Remove more rpc pass through methods * Remove submit tx pass through rpc methods * Add some comments to SubmittableExtrinsic methods * Construct the runtime api from the client * Fmt * Use From trait instead of new for AccountData query * Rename subxt_proc_macro crate to subxt_macro * Fix AccountData From impl * Extract codegen crate from macro crate * Fmt * Replace chameleon hidden field name * Extract StructDef for generating structs * More refactoring of StructDef, moving towards sharing with typegen * Replace explicit tests crate with single implicit integration tests crate * Rename from substrate-subxt to subxt * Fix runtime path relative to root Cargo.toml * Move RpcClient creation to RpcClient * WIP get examples to compile * Rename Runtime to Config trait * WIP implementing default Config * WIP implementing default extrinsic extras * fix metadata constants (#299) * Move DefaultConfig definition and impl to macro * Extract type substitute parsing to ir mod * Extract calls, events and storage from api generation * Add some hardcoded type substitute defaults * Fmt * Add utility pallet tests (#300) * add batch call test example * add pallet utility tests * add utility module * fix warnings * Add polkadot runtime metadata for example * Fix system errors and fmt * Add subxt-cli crate * Add metadata and codegen subcommands * Make subxt-cli codegen command work * Fmt * Add polkadot codegen test * Comment about how to run codegen * Derive AsCompact for structs with single concrete unsigned int field * Fix bitvec codegen, adds as non optional dependency * Regenerate polkadot api with bitvec fix * Edition 2021 * Fix polkadot codegen with bitvec * Polkadot balance transfer is working * Fix fetch remote * Fix transfer_subscribe example * Fix submit_and_watch example * Fmt * Generate storage iter method for iterating over keys * Fmt * Fix existential deposit test * Fix staking tests * Add option for custom generated type derives * Add generated type derives for test runtime api * Fmt * Copy WrapperTypeOpaque from substrate, add Encode/Decode * Fmt * Extract type generator to module, separate & fix tests * Fully qualified primitive and prelude types * Fix up remaining type gen tests * Skip formatting of generated polkadot example code * Remove empty utility test file. * Newline * Update cli/src/main.rs Co-authored-by: David <dvdplm@gmail.com> * Rename subxt-cli executable to subxt * Update src/client.rs Co-authored-by: David <dvdplm@gmail.com> * Add some code docs to TypeGenerator. * Extract TypePath to own file * Extract type def generation to separate file * Renamed ModuleType to TypeDefGen * Fmt * Factor out type parameter from final_key * Fix some type paths * Resolve some todos * Resolve some panic todos in events * Add EventsDecodingError * Decode compact composite types with a single primitive field * Decode compact composite types with a single primitive field * Update src/metadata.rs Co-authored-by: Andrew Plaza <aplaza@liquidthink.net> * Remove Perbill compact substitute types * Remove todos regarding maintaining Rust code items, promoted to follow up issue. * Remove todo regarding overridding default config impl * Remove todo regarding overridding default Extra * Remove todo regarding AccountData storage type defintion * Remove todo regarding borrowing storage key arguments * Remove type substitution tests todo * Remove `Box` field name type hack todo * Remove Compact todo * Remove sudo todos * Remove BitVec implementation todo * Fmt * Add health warning to README * Fix up health warning Co-authored-by: Paulo Martins <paulormart@users.noreply.github.com> Co-authored-by: David <dvdplm@gmail.com> Co-authored-by: Andrew Plaza <aplaza@liquidthink.net>
This commit is contained in:
+232
-552
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// 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
|
||||
@@ -12,55 +12,52 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
convert::TryFrom,
|
||||
marker::PhantomData,
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
Error as CodecError,
|
||||
};
|
||||
use codec::Error as CodecError;
|
||||
|
||||
use frame_metadata::{
|
||||
DecodeDifferent,
|
||||
PalletConstantMetadata,
|
||||
RuntimeMetadata,
|
||||
RuntimeMetadataLastVersion,
|
||||
RuntimeMetadataPrefixed,
|
||||
StorageEntryModifier,
|
||||
StorageEntryType,
|
||||
StorageHasher,
|
||||
StorageEntryMetadata,
|
||||
META_RESERVED,
|
||||
};
|
||||
use sp_core::storage::StorageKey;
|
||||
|
||||
use crate::Encoded;
|
||||
use crate::{
|
||||
Call,
|
||||
Encoded,
|
||||
};
|
||||
use scale_info::{
|
||||
form::PortableForm,
|
||||
Type,
|
||||
Variant,
|
||||
};
|
||||
|
||||
/// Metadata error.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum MetadataError {
|
||||
/// Failed to parse metadata.
|
||||
#[error("Error converting substrate metadata: {0}")]
|
||||
Conversion(#[from] ConversionError),
|
||||
/// Module is not in metadata.
|
||||
#[error("Module {0} not found")]
|
||||
ModuleNotFound(String),
|
||||
/// Module is not in metadata.
|
||||
#[error("Module index {0} not found")]
|
||||
ModuleIndexNotFound(u8),
|
||||
#[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("Event {0} not found")]
|
||||
EventNotFound(u8),
|
||||
#[error("Pallet {0}, Event {0} not found")]
|
||||
EventNotFound(u8, u8),
|
||||
/// Event is not in metadata.
|
||||
#[error("Error {0} not found")]
|
||||
ErrorNotFound(u8),
|
||||
#[error("Pallet {0}, Error {0} not found")]
|
||||
ErrorNotFound(u8, u8),
|
||||
/// Storage is not in metadata.
|
||||
#[error("Storage {0} not found")]
|
||||
StorageNotFound(&'static str),
|
||||
@@ -76,107 +73,91 @@ pub enum MetadataError {
|
||||
/// 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, Default)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Metadata {
|
||||
modules: HashMap<String, ModuleMetadata>,
|
||||
modules_with_calls: HashMap<String, ModuleWithCalls>,
|
||||
modules_with_events: HashMap<String, ModuleWithEvents>,
|
||||
modules_with_errors: HashMap<String, ModuleWithErrors>,
|
||||
metadata: RuntimeMetadataLastVersion,
|
||||
pallets: HashMap<String, PalletMetadata>,
|
||||
events: HashMap<(u8, u8), EventMetadata>,
|
||||
errors: HashMap<(u8, u8), ErrorMetadata>,
|
||||
}
|
||||
|
||||
impl Metadata {
|
||||
/// Returns `ModuleMetadata`.
|
||||
pub fn module<S>(&self, name: S) -> Result<&ModuleMetadata, MetadataError>
|
||||
where
|
||||
S: ToString,
|
||||
{
|
||||
let name = name.to_string();
|
||||
self.modules
|
||||
.get(&name)
|
||||
.ok_or(MetadataError::ModuleNotFound(name))
|
||||
/// Returns a reference to [`PalletMetadata`].
|
||||
pub fn pallet(&self, name: &'static str) -> Result<&PalletMetadata, MetadataError> {
|
||||
self.pallets
|
||||
.get(name)
|
||||
.ok_or(MetadataError::PalletNotFound(name.to_string()))
|
||||
}
|
||||
|
||||
/// Returns `ModuleWithCalls`.
|
||||
pub fn module_with_calls<S>(&self, name: S) -> Result<&ModuleWithCalls, MetadataError>
|
||||
where
|
||||
S: ToString,
|
||||
{
|
||||
let name = name.to_string();
|
||||
self.modules_with_calls
|
||||
.get(&name)
|
||||
.ok_or(MetadataError::ModuleNotFound(name))
|
||||
}
|
||||
|
||||
/// Returns Iterator of `ModuleWithEvents`.
|
||||
pub fn modules_with_events(&self) -> impl Iterator<Item = &ModuleWithEvents> {
|
||||
self.modules_with_events.values()
|
||||
}
|
||||
|
||||
/// Returns `ModuleWithEvents`.
|
||||
pub fn module_with_events(
|
||||
/// Returns the metadata for the event at the given pallet and event indices.
|
||||
pub fn event(
|
||||
&self,
|
||||
module_index: u8,
|
||||
) -> Result<&ModuleWithEvents, MetadataError> {
|
||||
self.modules_with_events
|
||||
.values()
|
||||
.find(|&module| module.index == module_index)
|
||||
.ok_or(MetadataError::ModuleIndexNotFound(module_index))
|
||||
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 `ModuleWithErrors`.
|
||||
pub fn module_with_errors(
|
||||
/// Returns the metadata for the error at the given pallet and error indices.
|
||||
pub fn error(
|
||||
&self,
|
||||
module_index: u8,
|
||||
) -> Result<&ModuleWithErrors, MetadataError> {
|
||||
self.modules_with_errors
|
||||
.values()
|
||||
.find(|&module| module.index == module_index)
|
||||
.ok_or(MetadataError::ModuleIndexNotFound(module_index))
|
||||
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)
|
||||
}
|
||||
|
||||
/// Pretty print metadata.
|
||||
pub fn pretty(&self) -> String {
|
||||
let mut string = String::new();
|
||||
for (name, module) in &self.modules {
|
||||
string.push_str(name.as_str());
|
||||
string.push('\n');
|
||||
for storage in module.storage.keys() {
|
||||
string.push_str(" s ");
|
||||
string.push_str(storage.as_str());
|
||||
string.push('\n');
|
||||
}
|
||||
if let Some(module) = self.modules_with_calls.get(name) {
|
||||
for call in module.calls.keys() {
|
||||
string.push_str(" c ");
|
||||
string.push_str(call.as_str());
|
||||
string.push('\n');
|
||||
}
|
||||
}
|
||||
if let Some(module) = self.modules_with_events.get(name) {
|
||||
for event in module.events.values() {
|
||||
string.push_str(" e ");
|
||||
string.push_str(event.name.as_str());
|
||||
string.push('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
string
|
||||
/// 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
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleMetadata {
|
||||
pub struct PalletMetadata {
|
||||
index: u8,
|
||||
name: String,
|
||||
storage: HashMap<String, StorageMetadata>,
|
||||
constants: HashMap<String, ModuleConstantMetadata>,
|
||||
calls: HashMap<String, u8>,
|
||||
storage: HashMap<String, StorageEntryMetadata<PortableForm>>,
|
||||
constants: HashMap<String, PalletConstantMetadata<PortableForm>>,
|
||||
}
|
||||
|
||||
impl ModuleMetadata {
|
||||
pub fn storage(&self, key: &'static str) -> Result<&StorageMetadata, MetadataError> {
|
||||
impl PalletMetadata {
|
||||
pub fn encode_call<C>(&self, call: &C) -> Result<Encoded, MetadataError>
|
||||
where
|
||||
C: Call,
|
||||
{
|
||||
let fn_index = self
|
||||
.calls
|
||||
.get(C::FUNCTION)
|
||||
.ok_or(MetadataError::CallNotFound(C::FUNCTION))?;
|
||||
let mut bytes = vec![self.index, *fn_index];
|
||||
bytes.extend(call.encode());
|
||||
Ok(Encoded(bytes))
|
||||
}
|
||||
|
||||
pub fn storage(
|
||||
&self,
|
||||
key: &'static str,
|
||||
) -> Result<&StorageEntryMetadata<PortableForm>, MetadataError> {
|
||||
self.storage
|
||||
.get(key)
|
||||
.ok_or(MetadataError::StorageNotFound(key))
|
||||
@@ -186,7 +167,7 @@ impl ModuleMetadata {
|
||||
pub fn constant(
|
||||
&self,
|
||||
key: &'static str,
|
||||
) -> Result<&ModuleConstantMetadata, MetadataError> {
|
||||
) -> Result<&PalletConstantMetadata<PortableForm>, MetadataError> {
|
||||
self.constants
|
||||
.get(key)
|
||||
.ok_or(MetadataError::ConstantNotFound(key))
|
||||
@@ -194,484 +175,183 @@ impl ModuleMetadata {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleWithCalls {
|
||||
index: u8,
|
||||
calls: HashMap<String, u8>,
|
||||
pub struct EventMetadata {
|
||||
pallet: String,
|
||||
event: String,
|
||||
variant: Variant<PortableForm>,
|
||||
}
|
||||
|
||||
impl ModuleWithCalls {
|
||||
pub fn call<T: Encode>(
|
||||
&self,
|
||||
function: &'static str,
|
||||
params: T,
|
||||
) -> Result<Encoded, MetadataError> {
|
||||
let fn_index = self
|
||||
.calls
|
||||
.get(function)
|
||||
.ok_or(MetadataError::CallNotFound(function))?;
|
||||
let mut bytes = vec![self.index, *fn_index];
|
||||
bytes.extend(params.encode());
|
||||
Ok(Encoded(bytes))
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleWithEvents {
|
||||
index: u8,
|
||||
name: String,
|
||||
events: HashMap<u8, ModuleEventMetadata>,
|
||||
pub struct ErrorMetadata {
|
||||
pallet: String,
|
||||
error: String,
|
||||
variant: Variant<PortableForm>,
|
||||
}
|
||||
|
||||
impl ModuleWithEvents {
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
impl ErrorMetadata {
|
||||
/// Get the name of the pallet from which the error originates.
|
||||
pub fn pallet(&self) -> &str {
|
||||
&self.pallet
|
||||
}
|
||||
|
||||
pub fn events(&self) -> impl Iterator<Item = &ModuleEventMetadata> {
|
||||
self.events.values()
|
||||
/// Get the name of the specific pallet error.
|
||||
pub fn error(&self) -> &str {
|
||||
&self.error
|
||||
}
|
||||
|
||||
pub fn event(&self, index: u8) -> Result<&ModuleEventMetadata, MetadataError> {
|
||||
self.events
|
||||
.get(&index)
|
||||
.ok_or(MetadataError::EventNotFound(index))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleWithErrors {
|
||||
index: u8,
|
||||
name: String,
|
||||
errors: HashMap<u8, String>,
|
||||
}
|
||||
|
||||
impl ModuleWithErrors {
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn error(&self, index: u8) -> Result<&String, MetadataError> {
|
||||
self.errors
|
||||
.get(&index)
|
||||
.ok_or(MetadataError::ErrorNotFound(index))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StorageMetadata {
|
||||
module_prefix: String,
|
||||
storage_prefix: String,
|
||||
modifier: StorageEntryModifier,
|
||||
ty: StorageEntryType,
|
||||
default: Vec<u8>,
|
||||
}
|
||||
|
||||
impl StorageMetadata {
|
||||
pub fn prefix(&self) -> StorageKey {
|
||||
let mut bytes = sp_core::twox_128(self.module_prefix.as_bytes()).to_vec();
|
||||
bytes.extend(&sp_core::twox_128(self.storage_prefix.as_bytes())[..]);
|
||||
StorageKey(bytes)
|
||||
}
|
||||
|
||||
pub fn default<V: Decode>(&self) -> Result<V, MetadataError> {
|
||||
Decode::decode(&mut &self.default[..]).map_err(MetadataError::DefaultError)
|
||||
}
|
||||
|
||||
pub fn hash(hasher: &StorageHasher, bytes: &[u8]) -> Vec<u8> {
|
||||
match hasher {
|
||||
StorageHasher::Identity => bytes.to_vec(),
|
||||
StorageHasher::Blake2_128 => sp_core::blake2_128(bytes).to_vec(),
|
||||
StorageHasher::Blake2_128Concat => {
|
||||
// copied from substrate Blake2_128Concat::hash since StorageHasher is not public
|
||||
sp_core::blake2_128(bytes)
|
||||
.iter()
|
||||
.chain(bytes)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
StorageHasher::Blake2_256 => sp_core::blake2_256(bytes).to_vec(),
|
||||
StorageHasher::Twox128 => sp_core::twox_128(bytes).to_vec(),
|
||||
StorageHasher::Twox256 => sp_core::twox_256(bytes).to_vec(),
|
||||
StorageHasher::Twox64Concat => {
|
||||
sp_core::twox_64(bytes)
|
||||
.iter()
|
||||
.chain(bytes)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hash_key<K: Encode>(hasher: &StorageHasher, key: &K) -> Vec<u8> {
|
||||
Self::hash(hasher, &key.encode())
|
||||
}
|
||||
|
||||
pub fn plain(&self) -> Result<StoragePlain, MetadataError> {
|
||||
match &self.ty {
|
||||
StorageEntryType::Plain(_) => {
|
||||
Ok(StoragePlain {
|
||||
prefix: self.prefix().0,
|
||||
})
|
||||
}
|
||||
_ => Err(MetadataError::StorageTypeError),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map<K: Encode>(&self) -> Result<StorageMap<K>, MetadataError> {
|
||||
match &self.ty {
|
||||
StorageEntryType::Map { hasher, .. } => {
|
||||
Ok(StorageMap {
|
||||
_marker: PhantomData,
|
||||
prefix: self.prefix().0,
|
||||
hasher: hasher.clone(),
|
||||
})
|
||||
}
|
||||
_ => Err(MetadataError::StorageTypeError),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn double_map<K1: Encode, K2: Encode>(
|
||||
&self,
|
||||
) -> Result<StorageDoubleMap<K1, K2>, MetadataError> {
|
||||
match &self.ty {
|
||||
StorageEntryType::DoubleMap {
|
||||
hasher,
|
||||
key2_hasher,
|
||||
..
|
||||
} => {
|
||||
Ok(StorageDoubleMap {
|
||||
_marker: PhantomData,
|
||||
prefix: self.prefix().0,
|
||||
hasher1: hasher.clone(),
|
||||
hasher2: key2_hasher.clone(),
|
||||
})
|
||||
}
|
||||
_ => Err(MetadataError::StorageTypeError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StoragePlain {
|
||||
prefix: Vec<u8>,
|
||||
}
|
||||
|
||||
impl StoragePlain {
|
||||
pub fn key(&self) -> StorageKey {
|
||||
StorageKey(self.prefix.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StorageMap<K> {
|
||||
_marker: PhantomData<K>,
|
||||
prefix: Vec<u8>,
|
||||
hasher: StorageHasher,
|
||||
}
|
||||
|
||||
impl<K: Encode> StorageMap<K> {
|
||||
pub fn key(&self, key: &K) -> StorageKey {
|
||||
let mut bytes = self.prefix.clone();
|
||||
bytes.extend(StorageMetadata::hash_key(&self.hasher, key));
|
||||
StorageKey(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StorageDoubleMap<K1, K2> {
|
||||
_marker: PhantomData<(K1, K2)>,
|
||||
prefix: Vec<u8>,
|
||||
hasher1: StorageHasher,
|
||||
hasher2: StorageHasher,
|
||||
}
|
||||
|
||||
impl<K1: Encode, K2: Encode> StorageDoubleMap<K1, K2> {
|
||||
pub fn key(&self, key1: &K1, key2: &K2) -> StorageKey {
|
||||
let mut bytes = self.prefix.clone();
|
||||
bytes.extend(StorageMetadata::hash_key(&self.hasher1, key1));
|
||||
bytes.extend(StorageMetadata::hash_key(&self.hasher2, key2));
|
||||
StorageKey(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleEventMetadata {
|
||||
pub name: String,
|
||||
arguments: Vec<EventArg>,
|
||||
}
|
||||
|
||||
impl ModuleEventMetadata {
|
||||
pub fn arguments(&self) -> Vec<EventArg> {
|
||||
self.arguments.to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
/// Naive representation of event argument types, supports current set of substrate EventArg types.
|
||||
/// If and when Substrate uses `type-metadata`, this can be replaced.
|
||||
///
|
||||
/// Used to calculate the size of a instance of an event variant without having the concrete type,
|
||||
/// so the raw bytes can be extracted from the encoded `Vec<EventRecord<E>>` (without `E` defined).
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub enum EventArg {
|
||||
Primitive(String),
|
||||
Vec(Box<EventArg>),
|
||||
Tuple(Vec<EventArg>),
|
||||
Option(Box<EventArg>),
|
||||
}
|
||||
|
||||
impl FromStr for EventArg {
|
||||
type Err = ConversionError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.starts_with("Vec<") {
|
||||
if s.ends_with('>') {
|
||||
Ok(EventArg::Vec(Box::new(s[4..s.len() - 1].parse()?)))
|
||||
} else {
|
||||
Err(ConversionError::InvalidEventArg(
|
||||
s.to_string(),
|
||||
"Expected closing `>` for `Vec`",
|
||||
))
|
||||
}
|
||||
} else if s.starts_with("Option<") {
|
||||
if s.ends_with('>') {
|
||||
Ok(EventArg::Option(Box::new(s[7..s.len() - 1].parse()?)))
|
||||
} else {
|
||||
Err(ConversionError::InvalidEventArg(
|
||||
s.to_string(),
|
||||
"Expected closing `>` for `Option`",
|
||||
))
|
||||
}
|
||||
} else if s.starts_with('(') {
|
||||
if s.ends_with(')') {
|
||||
let mut args = Vec::new();
|
||||
for arg in s[1..s.len() - 1].split(',') {
|
||||
let arg = arg.trim().parse()?;
|
||||
args.push(arg)
|
||||
}
|
||||
Ok(EventArg::Tuple(args))
|
||||
} else {
|
||||
Err(ConversionError::InvalidEventArg(
|
||||
s.to_string(),
|
||||
"Expecting closing `)` for tuple",
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Ok(EventArg::Primitive(s.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EventArg {
|
||||
/// Returns all primitive types for this EventArg
|
||||
pub fn primitives(&self) -> Vec<String> {
|
||||
match self {
|
||||
EventArg::Primitive(p) => vec![p.clone()],
|
||||
EventArg::Vec(arg) => arg.primitives(),
|
||||
EventArg::Option(arg) => arg.primitives(),
|
||||
EventArg::Tuple(args) => {
|
||||
let mut primitives = Vec::new();
|
||||
for arg in args {
|
||||
primitives.extend(arg.primitives())
|
||||
}
|
||||
primitives
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleConstantMetadata {
|
||||
name: String,
|
||||
ty: String,
|
||||
value: Vec<u8>,
|
||||
documentation: Vec<String>,
|
||||
}
|
||||
|
||||
impl ModuleConstantMetadata {
|
||||
/// Name
|
||||
pub fn name(&self) -> &String {
|
||||
&self.name
|
||||
}
|
||||
|
||||
/// Constant value (decoded)
|
||||
pub fn value<V: Decode>(&self) -> Result<V, MetadataError> {
|
||||
Decode::decode(&mut &self.value[..]).map_err(MetadataError::ConstantValueError)
|
||||
}
|
||||
|
||||
/// Type (as defined in the runtime)
|
||||
pub fn ty(&self) -> &String {
|
||||
&self.ty
|
||||
}
|
||||
|
||||
/// Documentation
|
||||
pub fn documentation(&self) -> &Vec<String> {
|
||||
&self.documentation
|
||||
/// Get the description of the specific pallet error.
|
||||
pub fn description(&self) -> &[String] {
|
||||
self.variant.docs()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ConversionError {
|
||||
pub enum InvalidMetadataError {
|
||||
#[error("Invalid prefix")]
|
||||
InvalidPrefix,
|
||||
#[error("Invalid version")]
|
||||
InvalidVersion,
|
||||
#[error("Expected DecodeDifferent::Decoded")]
|
||||
ExpectedDecoded,
|
||||
#[error("Invalid event arg {0}")]
|
||||
InvalidEventArg(String, &'static str),
|
||||
#[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 = MetadataError;
|
||||
type Error = InvalidMetadataError;
|
||||
|
||||
fn try_from(metadata: RuntimeMetadataPrefixed) -> Result<Self, Self::Error> {
|
||||
if metadata.0 != META_RESERVED {
|
||||
return Err(ConversionError::InvalidPrefix.into())
|
||||
return Err(InvalidMetadataError::InvalidPrefix.into())
|
||||
}
|
||||
let meta = match metadata.1 {
|
||||
RuntimeMetadata::V13(meta) => meta,
|
||||
_ => return Err(ConversionError::InvalidVersion.into()),
|
||||
let metadata = match metadata.1 {
|
||||
RuntimeMetadata::V14(meta) => meta,
|
||||
_ => return Err(InvalidMetadataError::InvalidVersion.into()),
|
||||
};
|
||||
let mut modules = HashMap::new();
|
||||
let mut modules_with_calls = HashMap::new();
|
||||
let mut modules_with_events = HashMap::new();
|
||||
let mut modules_with_errors = HashMap::new();
|
||||
for module in convert(meta.modules)?.into_iter() {
|
||||
let module_name = convert(module.name.clone())?;
|
||||
|
||||
let mut constant_map = HashMap::new();
|
||||
for constant in convert(module.constants)?.into_iter() {
|
||||
let constant_meta = convert_constant(constant)?;
|
||||
constant_map.insert(constant_meta.name.clone(), constant_meta);
|
||||
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 mut storage_map = HashMap::new();
|
||||
if let Some(storage) = module.storage {
|
||||
let storage = convert(storage)?;
|
||||
let module_prefix = convert(storage.prefix)?;
|
||||
for entry in convert(storage.entries)?.into_iter() {
|
||||
let storage_prefix = convert(entry.name.clone())?;
|
||||
let entry = convert_entry(
|
||||
module_prefix.clone(),
|
||||
storage_prefix.clone(),
|
||||
entry,
|
||||
)?;
|
||||
storage_map.insert(storage_prefix, entry);
|
||||
}
|
||||
}
|
||||
modules.insert(
|
||||
module_name.clone(),
|
||||
ModuleMetadata {
|
||||
index: module.index,
|
||||
name: module_name.clone(),
|
||||
storage: storage_map,
|
||||
constants: constant_map,
|
||||
},
|
||||
);
|
||||
let storage = pallet.storage.as_ref().map_or(HashMap::new(), |storage| {
|
||||
storage
|
||||
.entries
|
||||
.iter()
|
||||
.map(|entry| (entry.name.clone(), entry.clone()))
|
||||
.collect()
|
||||
});
|
||||
|
||||
if let Some(calls) = module.calls {
|
||||
let mut call_map = HashMap::new();
|
||||
for (index, call) in convert(calls)?.into_iter().enumerate() {
|
||||
let name = convert(call.name)?;
|
||||
call_map.insert(name, index as u8);
|
||||
}
|
||||
modules_with_calls.insert(
|
||||
module_name.clone(),
|
||||
ModuleWithCalls {
|
||||
index: module.index,
|
||||
calls: call_map,
|
||||
},
|
||||
);
|
||||
}
|
||||
if let Some(events) = module.event {
|
||||
let mut event_map = HashMap::new();
|
||||
for (index, event) in convert(events)?.into_iter().enumerate() {
|
||||
event_map.insert(index as u8, convert_event(event)?);
|
||||
}
|
||||
modules_with_events.insert(
|
||||
module_name.clone(),
|
||||
ModuleWithEvents {
|
||||
index: module.index,
|
||||
name: module_name.clone(),
|
||||
events: event_map,
|
||||
},
|
||||
);
|
||||
}
|
||||
let mut error_map = HashMap::new();
|
||||
for (index, error) in convert(module.errors)?.into_iter().enumerate() {
|
||||
error_map.insert(index as u8, convert_error(error)?);
|
||||
}
|
||||
modules_with_errors.insert(
|
||||
module_name.clone(),
|
||||
ModuleWithErrors {
|
||||
index: module.index,
|
||||
name: module_name.clone(),
|
||||
errors: error_map,
|
||||
},
|
||||
);
|
||||
}
|
||||
Ok(Metadata {
|
||||
modules,
|
||||
modules_with_calls,
|
||||
modules_with_events,
|
||||
modules_with_errors,
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn convert<B: 'static, O: 'static>(
|
||||
dd: DecodeDifferent<B, O>,
|
||||
) -> Result<O, ConversionError> {
|
||||
match dd {
|
||||
DecodeDifferent::Decoded(value) => Ok(value),
|
||||
_ => Err(ConversionError::ExpectedDecoded),
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_event(
|
||||
event: frame_metadata::EventMetadata,
|
||||
) -> Result<ModuleEventMetadata, ConversionError> {
|
||||
let name = convert(event.name)?;
|
||||
let mut arguments = Vec::new();
|
||||
for arg in convert(event.arguments)? {
|
||||
let arg = arg.parse::<EventArg>()?;
|
||||
arguments.push(arg);
|
||||
}
|
||||
Ok(ModuleEventMetadata { name, arguments })
|
||||
}
|
||||
|
||||
fn convert_entry(
|
||||
module_prefix: String,
|
||||
storage_prefix: String,
|
||||
entry: frame_metadata::StorageEntryMetadata,
|
||||
) -> Result<StorageMetadata, ConversionError> {
|
||||
let default = convert(entry.default)?;
|
||||
Ok(StorageMetadata {
|
||||
module_prefix,
|
||||
storage_prefix,
|
||||
modifier: entry.modifier,
|
||||
ty: entry.ty,
|
||||
default,
|
||||
})
|
||||
}
|
||||
|
||||
fn convert_error(
|
||||
error: frame_metadata::ErrorMetadata,
|
||||
) -> Result<String, ConversionError> {
|
||||
convert(error.name)
|
||||
}
|
||||
|
||||
fn convert_constant(
|
||||
constant: frame_metadata::ModuleConstantMetadata,
|
||||
) -> Result<ModuleConstantMetadata, ConversionError> {
|
||||
let name = convert(constant.name)?;
|
||||
let ty = convert(constant.ty)?;
|
||||
let value = convert(constant.value)?;
|
||||
let documentation = convert(constant.documentation)?;
|
||||
Ok(ModuleConstantMetadata {
|
||||
name,
|
||||
ty,
|
||||
value,
|
||||
documentation,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user