diff --git a/frame-metadata/src/common.rs b/frame-metadata/src/common.rs index 9eece64..79239c2 100644 --- a/frame-metadata/src/common.rs +++ b/frame-metadata/src/common.rs @@ -20,12 +20,11 @@ use codec::Decode; #[cfg(feature = "serde_full")] use serde::Serialize; -use super::{RuntimeMetadataPrefixed, META_RESERVED}; use codec::Encode; use scale_info::{ form::{Form, MetaForm, PortableForm}, - prelude::{collections::BTreeMap, vec::Vec}, - IntoPortable, MetaType, PortableRegistry, Registry, + prelude::vec::Vec, + IntoPortable, MetaType, Registry, }; /// Metadata of an extrinsic's signed extension. diff --git a/frame-metadata/src/lib.rs b/frame-metadata/src/lib.rs index a49d1c2..b8945cb 100644 --- a/frame-metadata/src/lib.rs +++ b/frame-metadata/src/lib.rs @@ -97,6 +97,9 @@ pub mod v15; #[cfg(feature = "v14")] pub use self::v14::*; +#[cfg(any(feature = "v14", feature = "v15-unstable"))] +mod common; + /// Metadata prefix. pub const META_RESERVED: u32 = 0x6174656d; // 'meta' warning for endianness. diff --git a/frame-metadata/src/v14.rs b/frame-metadata/src/v14.rs index d4ef844..de4248b 100644 --- a/frame-metadata/src/v14.rs +++ b/frame-metadata/src/v14.rs @@ -26,6 +26,12 @@ use scale_info::{ IntoPortable, MetaType, PortableRegistry, Registry, }; +pub use crate::common::{ + PalletCallMetadata, PalletConstantMetadata, PalletErrorMetadata, PalletEventMetadata, + PalletStorageMetadata, SignedExtensionMetadata, StorageEntryMetadata, StorageEntryModifier, + StorageEntryType, StorageHasher, +}; + /// Current prefix of metadata pub const META_RESERVED: u32 = 0x6174656d; // 'meta' warn endianness @@ -102,35 +108,6 @@ impl IntoPortable for ExtrinsicMetadata { } } -/// Metadata of an extrinsic's signed extension. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct SignedExtensionMetadata { - /// The unique signed extension identifier, which may be different from the type name. - pub identifier: T::String, - /// The type of the signed extension, with the data to be included in the extrinsic. - pub ty: T::Type, - /// The type of the additional signed data, with the data to be included in the signed payload - pub additional_signed: T::Type, -} - -impl IntoPortable for SignedExtensionMetadata { - type Output = SignedExtensionMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - SignedExtensionMetadata { - identifier: self.identifier.into_portable(registry), - ty: registry.register_type(&self.ty), - additional_signed: registry.register_type(&self.additional_signed), - } - } -} - /// All metadata about an runtime pallet. #[derive(Clone, PartialEq, Eq, Encode, Debug)] #[cfg_attr(feature = "decode", derive(Decode))] @@ -172,253 +149,3 @@ impl IntoPortable for PalletMetadata { } } } - -/// All metadata of the pallet's storage. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct PalletStorageMetadata { - /// The common prefix used by all storage entries. - pub prefix: T::String, - /// Metadata for all storage entries. - pub entries: Vec>, -} - -impl IntoPortable for PalletStorageMetadata { - type Output = PalletStorageMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletStorageMetadata { - prefix: self.prefix.into_portable(registry), - entries: registry.map_into_portable(self.entries), - } - } -} - -/// Metadata about one storage entry. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct StorageEntryMetadata { - /// Variable name of the storage entry. - pub name: T::String, - /// An `Option` modifier of that storage entry. - pub modifier: StorageEntryModifier, - /// Type of the value stored in the entry. - pub ty: StorageEntryType, - /// Default value (SCALE encoded). - pub default: Vec, - /// Storage entry documentation. - pub docs: Vec, -} - -impl IntoPortable for StorageEntryMetadata { - type Output = StorageEntryMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - StorageEntryMetadata { - name: self.name.into_portable(registry), - modifier: self.modifier, - ty: self.ty.into_portable(registry), - default: self.default, - docs: registry.map_into_portable(self.docs), - } - } -} - -/// A storage entry modifier indicates how a storage entry is returned when fetched and what the value will be if the key is not present. -/// Specifically this refers to the "return type" when fetching a storage entry, and what the value will be if the key is not present. -/// -/// `Optional` means you should expect an `Option`, with `None` returned if the key is not present. -/// `Default` means you should expect a `T` with the default value of default if the key is not present. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -pub enum StorageEntryModifier { - /// The storage entry returns an `Option`, with `None` if the key is not present. - Optional, - /// The storage entry returns `T::Default` if the key is not present. - Default, -} - -/// Hasher used by storage maps -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -pub enum StorageHasher { - /// 128-bit Blake2 hash. - Blake2_128, - /// 256-bit Blake2 hash. - Blake2_256, - /// Multiple 128-bit Blake2 hashes concatenated. - Blake2_128Concat, - /// 128-bit XX hash. - Twox128, - /// 256-bit XX hash. - Twox256, - /// Multiple 64-bit XX hashes concatenated. - Twox64Concat, - /// Identity hashing (no hashing). - Identity, -} - -/// A type of storage value. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub enum StorageEntryType { - /// Plain storage entry (just the value). - Plain(T::Type), - /// A storage map. - Map { - /// One or more hashers, should be one hasher per key element. - hashers: Vec, - /// The type of the key, can be a tuple with elements for each of the hashers. - key: T::Type, - /// The type of the value. - value: T::Type, - }, -} - -impl IntoPortable for StorageEntryType { - type Output = StorageEntryType; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - match self { - Self::Plain(plain) => StorageEntryType::Plain(registry.register_type(&plain)), - Self::Map { - hashers, - key, - value, - } => StorageEntryType::Map { - hashers, - key: registry.register_type(&key), - value: registry.register_type(&value), - }, - } - } -} - -/// Metadata for all calls in a pallet -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct PalletCallMetadata { - /// The corresponding enum type for the pallet call. - pub ty: T::Type, -} - -impl IntoPortable for PalletCallMetadata { - type Output = PalletCallMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletCallMetadata { - ty: registry.register_type(&self.ty), - } - } -} - -impl From for PalletCallMetadata { - fn from(ty: MetaType) -> Self { - Self { ty } - } -} - -/// Metadata about the pallet Event type. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -pub struct PalletEventMetadata { - /// The Event type. - pub ty: T::Type, -} - -impl IntoPortable for PalletEventMetadata { - type Output = PalletEventMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletEventMetadata { - ty: registry.register_type(&self.ty), - } - } -} - -impl From for PalletEventMetadata { - fn from(ty: MetaType) -> Self { - Self { ty } - } -} - -/// Metadata about one pallet constant. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct PalletConstantMetadata { - /// Name of the pallet constant. - pub name: T::String, - /// Type of the pallet constant. - pub ty: T::Type, - /// Value stored in the constant (SCALE encoded). - pub value: Vec, - /// Documentation of the constant. - pub docs: Vec, -} - -impl IntoPortable for PalletConstantMetadata { - type Output = PalletConstantMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletConstantMetadata { - name: self.name.into_portable(registry), - ty: registry.register_type(&self.ty), - value: self.value, - docs: registry.map_into_portable(self.docs), - } - } -} - -/// Metadata about a pallet error. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr(feature = "serde_full", serde(bound(serialize = "T::Type: Serialize")))] -pub struct PalletErrorMetadata { - /// The error type information. - pub ty: T::Type, -} - -impl IntoPortable for PalletErrorMetadata { - type Output = PalletErrorMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletErrorMetadata { - ty: registry.register_type(&self.ty), - } - } -} - -impl From for PalletErrorMetadata { - fn from(ty: MetaType) -> Self { - Self { ty } - } -} diff --git a/frame-metadata/src/v15.rs b/frame-metadata/src/v15.rs index e113545..e56ef18 100644 --- a/frame-metadata/src/v15.rs +++ b/frame-metadata/src/v15.rs @@ -26,6 +26,12 @@ use scale_info::{ IntoPortable, MetaType, PortableRegistry, Registry, }; +pub use crate::common::{ + PalletCallMetadata, PalletConstantMetadata, PalletErrorMetadata, PalletEventMetadata, + PalletStorageMetadata, SignedExtensionMetadata, StorageEntryMetadata, StorageEntryModifier, + StorageEntryType, StorageHasher, +}; + /// Latest runtime metadata pub type RuntimeMetadataLastVersion = RuntimeMetadataV15; @@ -86,6 +92,51 @@ impl RuntimeMetadataV15 { } } +/// All metadata about an runtime pallet. +#[derive(Clone, PartialEq, Eq, Encode, Debug)] +#[cfg_attr(feature = "decode", derive(Decode))] +#[cfg_attr(feature = "serde_full", derive(Serialize))] +#[cfg_attr( + feature = "serde_full", + serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) +)] +pub struct PalletMetadata { + /// Pallet name. + pub name: T::String, + /// Pallet storage metadata. + pub storage: Option>, + /// Pallet calls metadata. + pub calls: Option>, + /// Pallet event metadata. + pub event: Option>, + /// Pallet constants metadata. + pub constants: Vec>, + /// Pallet error metadata. + pub error: Option>, + /// Define the index of the pallet, this index will be used for the encoding of pallet event, + /// call and origin variants. + pub index: u8, + /// Pallet documentation. + pub docs: Vec, +} + +impl IntoPortable for PalletMetadata { + type Output = PalletMetadata; + + fn into_portable(self, registry: &mut Registry) -> Self::Output { + PalletMetadata { + name: self.name.into_portable(registry), + storage: self.storage.map(|storage| storage.into_portable(registry)), + calls: self.calls.map(|calls| calls.into_portable(registry)), + event: self.event.map(|event| event.into_portable(registry)), + constants: registry.map_into_portable(self.constants), + error: self.error.map(|error| error.into_portable(registry)), + index: self.index, + docs: registry.map_into_portable(self.docs), + } + } +} + /// Metadata of a runtime trait. #[derive(Clone, PartialEq, Eq, Encode, Debug)] #[cfg_attr(feature = "decode", derive(Decode))] @@ -211,330 +262,6 @@ impl IntoPortable for ExtrinsicMetadata { } } -/// Metadata of an extrinsic's signed extension. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct SignedExtensionMetadata { - /// The unique signed extension identifier, which may be different from the type name. - pub identifier: T::String, - /// The type of the signed extension, with the data to be included in the extrinsic. - pub ty: T::Type, - /// The type of the additional signed data, with the data to be included in the signed payload - pub additional_signed: T::Type, -} - -impl IntoPortable for SignedExtensionMetadata { - type Output = SignedExtensionMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - SignedExtensionMetadata { - identifier: self.identifier.into_portable(registry), - ty: registry.register_type(&self.ty), - additional_signed: registry.register_type(&self.additional_signed), - } - } -} - -/// All metadata about an runtime pallet. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct PalletMetadata { - /// Pallet name. - pub name: T::String, - /// Pallet storage metadata. - pub storage: Option>, - /// Pallet calls metadata. - pub calls: Option>, - /// Pallet event metadata. - pub event: Option>, - /// Pallet constants metadata. - pub constants: Vec>, - /// Pallet error metadata. - pub error: Option>, - /// Define the index of the pallet, this index will be used for the encoding of pallet event, - /// call and origin variants. - pub index: u8, - /// Pallet documentation. - pub docs: Vec, -} - -impl IntoPortable for PalletMetadata { - type Output = PalletMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletMetadata { - name: self.name.into_portable(registry), - storage: self.storage.map(|storage| storage.into_portable(registry)), - calls: self.calls.map(|calls| calls.into_portable(registry)), - event: self.event.map(|event| event.into_portable(registry)), - constants: registry.map_into_portable(self.constants), - error: self.error.map(|error| error.into_portable(registry)), - index: self.index, - docs: registry.map_into_portable(self.docs), - } - } -} - -/// All metadata of the pallet's storage. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct PalletStorageMetadata { - /// The common prefix used by all storage entries. - pub prefix: T::String, - /// Metadata for all storage entries. - pub entries: Vec>, -} - -impl IntoPortable for PalletStorageMetadata { - type Output = PalletStorageMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletStorageMetadata { - prefix: self.prefix.into_portable(registry), - entries: registry.map_into_portable(self.entries), - } - } -} - -/// Metadata about one storage entry. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct StorageEntryMetadata { - /// Variable name of the storage entry. - pub name: T::String, - /// An `Option` modifier of that storage entry. - pub modifier: StorageEntryModifier, - /// Type of the value stored in the entry. - pub ty: StorageEntryType, - /// Default value (SCALE encoded). - pub default: Vec, - /// Storage entry documentation. - pub docs: Vec, -} - -impl IntoPortable for StorageEntryMetadata { - type Output = StorageEntryMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - StorageEntryMetadata { - name: self.name.into_portable(registry), - modifier: self.modifier, - ty: self.ty.into_portable(registry), - default: self.default, - docs: registry.map_into_portable(self.docs), - } - } -} - -/// A storage entry modifier indicates how a storage entry is returned when fetched and what the value will be if the key is not present. -/// Specifically this refers to the "return type" when fetching a storage entry, and what the value will be if the key is not present. -/// -/// `Optional` means you should expect an `Option`, with `None` returned if the key is not present. -/// `Default` means you should expect a `T` with the default value of default if the key is not present. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -pub enum StorageEntryModifier { - /// The storage entry returns an `Option`, with `None` if the key is not present. - Optional, - /// The storage entry returns `T::Default` if the key is not present. - Default, -} - -/// Hasher used by storage maps -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -pub enum StorageHasher { - /// 128-bit Blake2 hash. - Blake2_128, - /// 256-bit Blake2 hash. - Blake2_256, - /// Multiple 128-bit Blake2 hashes concatenated. - Blake2_128Concat, - /// 128-bit XX hash. - Twox128, - /// 256-bit XX hash. - Twox256, - /// Multiple 64-bit XX hashes concatenated. - Twox64Concat, - /// Identity hashing (no hashing). - Identity, -} - -/// A type of storage value. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub enum StorageEntryType { - /// Plain storage entry (just the value). - Plain(T::Type), - /// A storage map. - Map { - /// One or more hashers, should be one hasher per key element. - hashers: Vec, - /// The type of the key, can be a tuple with elements for each of the hashers. - key: T::Type, - /// The type of the value. - value: T::Type, - }, -} - -impl IntoPortable for StorageEntryType { - type Output = StorageEntryType; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - match self { - Self::Plain(plain) => StorageEntryType::Plain(registry.register_type(&plain)), - Self::Map { - hashers, - key, - value, - } => StorageEntryType::Map { - hashers, - key: registry.register_type(&key), - value: registry.register_type(&value), - }, - } - } -} - -/// Metadata for all calls in a pallet -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct PalletCallMetadata { - /// The corresponding enum type for the pallet call. - pub ty: T::Type, -} - -impl IntoPortable for PalletCallMetadata { - type Output = PalletCallMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletCallMetadata { - ty: registry.register_type(&self.ty), - } - } -} - -impl From for PalletCallMetadata { - fn from(ty: MetaType) -> Self { - Self { ty } - } -} - -/// Metadata about the pallet Event type. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -pub struct PalletEventMetadata { - /// The Event type. - pub ty: T::Type, -} - -impl IntoPortable for PalletEventMetadata { - type Output = PalletEventMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletEventMetadata { - ty: registry.register_type(&self.ty), - } - } -} - -impl From for PalletEventMetadata { - fn from(ty: MetaType) -> Self { - Self { ty } - } -} - -/// Metadata about one pallet constant. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr( - feature = "serde_full", - serde(bound(serialize = "T::Type: Serialize, T::String: Serialize")) -)] -pub struct PalletConstantMetadata { - /// Name of the pallet constant. - pub name: T::String, - /// Type of the pallet constant. - pub ty: T::Type, - /// Value stored in the constant (SCALE encoded). - pub value: Vec, - /// Documentation of the constant. - pub docs: Vec, -} - -impl IntoPortable for PalletConstantMetadata { - type Output = PalletConstantMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletConstantMetadata { - name: self.name.into_portable(registry), - ty: registry.register_type(&self.ty), - value: self.value, - docs: registry.map_into_portable(self.docs), - } - } -} - -/// Metadata about a pallet error. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] -#[cfg_attr(feature = "decode", derive(Decode))] -#[cfg_attr(feature = "serde_full", derive(Serialize))] -#[cfg_attr(feature = "serde_full", serde(bound(serialize = "T::Type: Serialize")))] -pub struct PalletErrorMetadata { - /// The error type information. - pub ty: T::Type, -} - -impl IntoPortable for PalletErrorMetadata { - type Output = PalletErrorMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - PalletErrorMetadata { - ty: registry.register_type(&self.ty), - } - } -} - -impl From for PalletErrorMetadata { - fn from(ty: MetaType) -> Self { - Self { ty } - } -} - /// Metadata for custom types. /// /// This map associates a string key to a `CustomValueMetadata`.