From f4b1727ec933545f3d34f30f4537c0820671c52c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 10 May 2021 12:00:19 +0100 Subject: [PATCH] Refactor v13 metadata types (#11) * Encapsulate call and event metadata types, remove OuterEventMetadata * Rename some types, use pallet instead of module * Remove some serde bounds * Fmt * Rename ErrorMetadata to PalletErrorMetadata * Rename errors field to error and make optional * Fix error * Rename StorageMetadata with Pallet prefix for consistency, fix comment. --- frame-metadata/src/v13.rs | 176 +++++++++++++++----------------------- 1 file changed, 69 insertions(+), 107 deletions(-) diff --git a/frame-metadata/src/v13.rs b/frame-metadata/src/v13.rs index f3ced21..8f8a1a0 100644 --- a/frame-metadata/src/v13.rs +++ b/frame-metadata/src/v13.rs @@ -50,20 +50,20 @@ impl From for super::RuntimeMetadataPrefixed { #[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))] pub struct RuntimeMetadataV13 { pub types: PortableRegistry, - /// Metadata of all the modules. - pub modules: Vec>, + /// Metadata of all the pallets. + pub pallets: Vec>, /// Metadata of the extrinsic. pub extrinsic: ExtrinsicMetadata, } impl RuntimeMetadataV13 { - pub fn new(modules: Vec, extrinsic: ExtrinsicMetadata) -> Self { + pub fn new(pallets: Vec, extrinsic: ExtrinsicMetadata) -> Self { let mut registry = Registry::new(); - let modules = registry.map_into_portable(modules); + let pallets = registry.map_into_portable(pallets); let extrinsic = extrinsic.into_portable(&mut registry); Self { types: registry.into(), - modules, + pallets, extrinsic, } } @@ -122,59 +122,59 @@ impl IntoPortable for SignedExtensionMetadata { } } -/// All metadata about an runtime module. +/// All metadata about an runtime pallet. #[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 { +pub struct PalletMetadata { pub name: T::String, - pub storage: Option>, - pub calls: Option>>, - pub event: Option>>, - pub constants: Vec>, - pub errors: Vec>, - /// Define the index of the module, this index will be used for the encoding of module event, + pub storage: Option>, + pub calls: Option>, + pub event: Option>, + pub constants: Vec>, + 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, } -impl IntoPortable for ModuleMetadata { - type Output = ModuleMetadata; +impl IntoPortable for PalletMetadata { + type Output = PalletMetadata; fn into_portable(self, registry: &mut Registry) -> Self::Output { - ModuleMetadata { + PalletMetadata { name: self.name.into_portable(registry), storage: self.storage.map(|storage| storage.into_portable(registry)), - calls: self.calls.map(|calls| registry.map_into_portable(calls)), - event: self.event.map(|event| registry.map_into_portable(event)), + 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), - errors: registry.map_into_portable(self.errors), + error: self.error.map(|error| error.into_portable(registry)), index: self.index, } } } -/// All metadata of the storage. +/// All metadata of the pallet's 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 { +pub struct PalletStorageMetadata { /// The common prefix used by all storage entries. pub prefix: T::String, pub entries: Vec>, } -impl IntoPortable for StorageMetadata { - type Output = StorageMetadata; +impl IntoPortable for PalletStorageMetadata { + type Output = PalletStorageMetadata; fn into_portable(self, registry: &mut Registry) -> Self::Output { - StorageMetadata { + PalletStorageMetadata { prefix: self.prefix.into_portable(registry), entries: registry.map_into_portable(self.entries), } @@ -290,6 +290,30 @@ impl IntoPortable for StorageEntryType { } } +/// Metadata for all calls in a pallet +#[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 PalletCallMetadata { + /// The corresponding enum type for the pallet call. + pub ty: T::Type, + pub calls: Vec>, +} + +impl IntoPortable for PalletCallMetadata { + type Output = PalletCallMetadata; + + fn into_portable(self, registry: &mut Registry) -> Self::Output { + PalletCallMetadata { + ty: registry.register_type(&self.ty), + calls: registry.map_into_portable(self.calls), + } + } +} + /// All the metadata about a function. #[derive(Clone, PartialEq, Eq, Encode)] #[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))] @@ -318,10 +342,6 @@ impl IntoPortable for FunctionMetadata { /// All the metadata about a function argument. #[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 { pub name: T::String, pub ty: T::Type, @@ -338,96 +358,42 @@ impl IntoPortable for FunctionArgumentMetadata { } } -/// All the metadata about an outer event. +/// Metadata about the pallet event 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 struct OuterEventMetadata { - pub name: T::String, - pub events: Vec>, +pub struct PalletEventMetadata { + pub ty: T::Type, } -impl IntoPortable for OuterEventMetadata { - type Output = OuterEventMetadata; +impl IntoPortable for PalletEventMetadata { + type Output = PalletEventMetadata; fn into_portable(self, registry: &mut Registry) -> Self::Output { - OuterEventMetadata { - name: self.name.into_portable(registry), - events: registry.map_into_portable(self.events), + PalletEventMetadata { + ty: registry.register_type(&self.ty), } } } -/// Metadata about a module event. +/// All the metadata about one pallet 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 ModuleEventMetadata { - pub name: T::String, - pub events: Vec>, -} - -impl IntoPortable for ModuleEventMetadata { - type Output = ModuleEventMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - ModuleEventMetadata { - name: self.name.into_portable(registry), - events: registry.map_into_portable(self.events), - } - } -} - -/// All the metadata about an event. -#[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 { - pub name: T::String, - pub arguments: Vec>, - pub documentation: Vec, -} - -impl IntoPortable for EventMetadata { - type Output = EventMetadata; - - fn into_portable(self, registry: &mut Registry) -> Self::Output { - EventMetadata { - name: self.name.into_portable(registry), - arguments: registry.map_into_portable(self.arguments), - documentation: registry.map_into_portable(self.documentation), - } - } -} - -/// 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 { +pub struct PalletConstantMetadata { pub name: T::String, pub ty: T::Type, pub value: ByteGetter, pub documentation: Vec, } -impl IntoPortable for ModuleConstantMetadata { - type Output = ModuleConstantMetadata; +impl IntoPortable for PalletConstantMetadata { + type Output = PalletConstantMetadata; fn into_portable(self, registry: &mut Registry) -> Self::Output { - ModuleConstantMetadata { + PalletConstantMetadata { name: self.name.into_portable(registry), ty: registry.register_type(&self.ty), value: self.value, @@ -436,25 +402,21 @@ impl IntoPortable for ModuleConstantMetadata { } } -/// All the metadata about a module error. +/// Metadata about a pallet 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 { - pub name: T::String, - pub documentation: Vec, +#[cfg_attr(feature = "std", serde(bound(serialize = "T::Type: Serialize")))] +pub struct PalletErrorMetadata { + /// The error type information. + pub ty: T::Type, } -impl IntoPortable for ErrorMetadata { - type Output = ErrorMetadata; +impl IntoPortable for PalletErrorMetadata { + type Output = PalletErrorMetadata; fn into_portable(self, registry: &mut Registry) -> Self::Output { - ErrorMetadata { - name: self.name.into_portable(registry), - documentation: registry.map_into_portable(self.documentation), + PalletErrorMetadata { + ty: registry.register_type(&self.ty), } } }