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
+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,