Complete v13 type definitions, support metadata decoding and serde (#4)

* Add String type param for Decode impl, fix serde serialization

* Remove Deserialize derives and fix FormString impl for no_std

* Add SignedExtensionMetadata type, name is possibly be different than type name

* Add Extrinsic type

* Fix SignedExtenstionMetadata compilation issues

* Fmt

* Use PhantomData for V0 to fix no features check

* Restore ModuleMetadata index field

* Add module constants metadata

* Fmt
This commit is contained in:
Andrew Jones
2021-01-15 09:26:17 +00:00
committed by GitHub
parent 1ac42f8153
commit aa716b2bd6
4 changed files with 173 additions and 30 deletions
+25 -7
View File
@@ -22,7 +22,10 @@
cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
use codec::{Decode, Error, Input};
use serde::Serialize;
use serde::{
Deserialize,
Serialize,
};
} else {
extern crate alloc;
use alloc::vec::Vec;
@@ -37,14 +40,29 @@ pub mod v12;
#[cfg(feature = "v13")]
pub mod v13;
cfg_if::cfg_if! {
if #[cfg(not(feature = "v13"))] {
/// Dummy trait in place of `scale_info::form::FormString`.
/// Since the `scale-info` crate is only imported for the `v13` feature.
pub trait FormString {}
impl FormString for &'static str {}
#[cfg(feature = "std")]
impl FormString for String {}
} else {
pub(crate) use scale_info::form::FormString;
}
}
/// The metadata of a runtime.
/// The version ID encoded/decoded through
/// the enum nature of `RuntimeMetadata`.
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
pub enum RuntimeMetadata {
#[cfg_attr(feature = "std", serde(bound(serialize = "S: Serialize")))]
pub enum RuntimeMetadata<S: FormString = &'static str> {
/// Unused; enum filler.
V0(RuntimeMetadataDeprecated),
V0(core::marker::PhantomData<S>),
/// Version 1 for runtime metadata. No longer used.
V1(RuntimeMetadataDeprecated),
/// Version 2 for runtime metadata. No longer used.
@@ -69,13 +87,13 @@ pub enum RuntimeMetadata {
V11(RuntimeMetadataDeprecated),
/// Version 12 for runtime metadata
#[cfg(feature = "v12")]
V12(v12::RuntimeMetadataV12),
V12(v12::RuntimeMetadataV12<S>),
/// Version 12 for runtime metadata, as raw encoded bytes.
#[cfg(not(feature = "v12"))]
V12(OpaqueMetadata),
/// Version 13 for runtime metadata.
#[cfg(feature = "v13")]
V13(v13::RuntimeMetadataV13),
V13(v13::RuntimeMetadataV13<S>),
/// Version 13 for runtime metadata, as raw encoded bytes.
#[cfg(not(feature = "v13"))]
V13(OpaqueMetadata),
@@ -83,12 +101,12 @@ pub enum RuntimeMetadata {
/// Stores the encoded `RuntimeMetadata` as raw bytes.
#[derive(Encode, Eq, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Deserialize, Debug))]
pub struct OpaqueMetadata(pub Vec<u8>);
/// Enum that should fail.
#[derive(Eq, PartialEq)]
#[cfg_attr(feature = "std", derive(Serialize, Debug))]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub enum RuntimeMetadataDeprecated {}
impl Encode for RuntimeMetadataDeprecated {
+11 -2
View File
@@ -35,6 +35,9 @@ cfg_if::cfg_if! {
}
}
use super::FormString;
use core::marker::PhantomData;
/// Current prefix of metadata
pub const META_RESERVED: u32 = 0x6174656d; // 'meta' warn endianness
@@ -347,7 +350,10 @@ pub struct StorageMetadata {
/// Metadata prefixed by a u32 for reserved usage
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
pub struct RuntimeMetadataPrefixed(pub u32, pub super::RuntimeMetadata);
pub struct RuntimeMetadataPrefixed<S: FormString = &'static str>(
pub u32,
pub super::RuntimeMetadata<S>,
);
/// Metadata of the extrinsic used by the runtime.
#[derive(Eq, Encode, PartialEq)]
@@ -362,11 +368,14 @@ pub struct ExtrinsicMetadata {
/// The metadata of a runtime.
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
pub struct RuntimeMetadataV12 {
#[cfg_attr(feature = "std", serde(bound(serialize = "S: Serialize")))]
pub struct RuntimeMetadataV12<S = ()> {
/// Metadata of all the modules.
pub modules: DecodeDifferentArray<ModuleMetadata>,
/// Metadata of the extrinsic.
pub extrinsic: ExtrinsicMetadata,
/// Marker for dummy type parameter required by v13 but not used here.
pub marker: PhantomData<S>,
}
/// The latest version of the metadata.
+136 -21
View File
@@ -17,7 +17,7 @@
cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
use codec::{Decode, Error, Input};
use codec::Decode;
use serde::Serialize;
}
}
@@ -38,7 +38,11 @@ pub type ByteGetter = Vec<u8>;
/// Metadata prefixed by a u32 for reserved usage
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
pub struct RuntimeMetadataPrefixed(pub u32, pub super::RuntimeMetadata);
#[cfg_attr(feature = "std", serde(bound(serialize = "S: Serialize")))]
pub struct RuntimeMetadataPrefixed<S: FormString = &'static str>(
pub u32,
pub super::RuntimeMetadata<S>,
);
pub type RuntimeMetadataLastVersion = RuntimeMetadataV13;
@@ -52,12 +56,13 @@ impl From<RuntimeMetadataLastVersion> for RuntimeMetadataPrefixed {
// todo: [AJ] add back clone derive if required (requires PortableRegistry to implement clone)
#[derive(PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "std", serde(bound(serialize = "S: Serialize")))]
pub struct RuntimeMetadataV13<S: FormString = &'static str> {
pub types: PortableRegistry<S>,
/// Metadata of all the modules.
pub modules: Vec<ModuleMetadata<PortableForm>>,
pub modules: Vec<ModuleMetadata<PortableForm<S>>>,
/// Metadata of the extrinsic.
pub extrinsic: ExtrinsicMetadata<PortableForm>,
pub extrinsic: ExtrinsicMetadata<PortableForm<S>>,
}
impl RuntimeMetadataV13 {
@@ -74,13 +79,19 @@ impl RuntimeMetadataV13 {
}
/// Metadata of the extrinsic used by the runtime.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode))]
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct ExtrinsicMetadata<T: Form = MetaForm> {
/// The type of the extrinsic.
pub ty: T::Type,
/// Extrinsic version.
pub version: u8,
/// The signed extensions in the order they appear in the extrinsic.
pub signed_extensions: Vec<T::Type>,
pub signed_extensions: Vec<SignedExtensionMetadata<T>>,
}
impl IntoPortable for ExtrinsicMetadata {
@@ -88,8 +99,34 @@ impl IntoPortable for ExtrinsicMetadata {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
ExtrinsicMetadata {
ty: registry.register_type(&self.ty),
version: self.version,
signed_extensions: registry.register_types(self.signed_extensions),
signed_extensions: registry.map_into_portable(self.signed_extensions),
}
}
}
/// Metadata of an extrinsic's signed extension.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct SignedExtensionMetadata<T: Form = MetaForm> {
/// The unique signed extension identifier, which may be different from the type name.
pub identifier: T::String,
/// The signed extensions in the order they appear in the extrinsic.
pub ty: T::Type,
}
impl IntoPortable for SignedExtensionMetadata {
type Output = SignedExtensionMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
SignedExtensionMetadata {
identifier: self.identifier.into_portable(registry),
ty: registry.register_type(&self.ty),
}
}
}
@@ -97,13 +134,20 @@ impl IntoPortable for ExtrinsicMetadata {
/// All metadata about an runtime module.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct ModuleMetadata<T: Form = MetaForm> {
pub name: T::String,
pub storage: Option<Vec<StorageMetadata<T>>>,
pub calls: Option<Vec<FunctionMetadata<T>>>,
pub event: Option<Vec<EventMetadata<T>>>,
// pub constants: DFnA<ModuleConstantMetadata>,
pub constants: Option<Vec<ModuleConstantMetadata<T>>>,
pub errors: Vec<ErrorMetadata<T>>,
/// Define the index of the module, this index will be used for the encoding of module event,
/// call and origin variants.
pub index: u8,
}
impl IntoPortable for ModuleMetadata {
@@ -117,7 +161,11 @@ impl IntoPortable for ModuleMetadata {
.map(|storage| registry.map_into_portable(storage)),
calls: self.calls.map(|calls| registry.map_into_portable(calls)),
event: self.event.map(|event| registry.map_into_portable(event)),
constants: self
.constants
.map(|constant| registry.map_into_portable(constant)),
errors: registry.map_into_portable(self.errors),
index: self.index,
}
}
}
@@ -125,6 +173,10 @@ impl IntoPortable for ModuleMetadata {
/// All metadata of the storage.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct StorageMetadata<T: Form = MetaForm> {
/// The common prefix used by all storage entries.
pub prefix: T::String,
@@ -145,6 +197,10 @@ impl IntoPortable for StorageMetadata {
/// All the metadata about one storage entry.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct StorageEntryMetadata<T: Form = MetaForm> {
pub name: T::String,
pub modifier: StorageEntryModifier,
@@ -191,6 +247,10 @@ pub enum StorageHasher {
/// A storage entry type.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub enum StorageEntryType<T: Form = MetaForm> {
Plain(T::String),
Map {
@@ -244,8 +304,12 @@ impl IntoPortable for StorageEntryType {
}
/// All the metadata about a function.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode))]
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct FunctionMetadata<T: Form = MetaForm> {
pub name: T::String,
pub arguments: Vec<FunctionArgumentMetadata<T>>,
@@ -265,8 +329,12 @@ impl IntoPortable for FunctionMetadata {
}
/// All the metadata about a function argument.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode))]
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct FunctionArgumentMetadata<T: Form = MetaForm> {
pub name: T::String,
pub ty: T::Type,
@@ -286,8 +354,12 @@ impl IntoPortable for FunctionArgumentMetadata {
}
/// All the metadata about an outer event.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode))]
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct OuterEventMetadata<T: Form = MetaForm> {
pub name: T::String,
pub events: Vec<ModuleEventMetadata<T>>,
@@ -305,8 +377,12 @@ impl IntoPortable for OuterEventMetadata {
}
/// Metadata about a module event.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode))]
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct ModuleEventMetadata<T: Form = MetaForm> {
pub name: T::String,
pub events: Vec<EventMetadata<T>>,
@@ -324,8 +400,12 @@ impl IntoPortable for ModuleEventMetadata {
}
/// All the metadata about an event.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode))]
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct EventMetadata<T: Form = MetaForm> {
pub name: T::String,
pub arguments: Vec<TypeSpec<T>>,
@@ -344,9 +424,40 @@ impl IntoPortable for EventMetadata {
}
}
/// All the metadata about one module constant.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct ModuleConstantMetadata<T: Form = MetaForm> {
pub name: T::String,
pub ty: T::Type,
pub value: ByteGetter,
pub documentation: Vec<T::String>,
}
impl IntoPortable for ModuleConstantMetadata {
type Output = ModuleConstantMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
ModuleConstantMetadata {
name: self.name.into_portable(registry),
ty: registry.register_type(&self.ty),
value: self.value,
documentation: registry.map_into_portable(self.documentation),
}
}
}
/// All the metadata about a module error.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct ErrorMetadata<T: Form = MetaForm> {
pub name: T::String,
pub documentation: Vec<T::String>,
@@ -380,8 +491,12 @@ impl IntoPortable for ErrorMetadata {
/// `pred`'s display name is `Predicate` and the display name of
/// the return type is simply `bool`. Note that `Predicate` could
/// simply be a type alias to `fn(i32, i32) -> Ordering`.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode))]
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct TypeSpec<T: Form = MetaForm> {
/// The actual type.
pub ty: T::Type,