Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor v13 metadata types #11

Merged
merged 9 commits into from
May 10, 2021
176 changes: 69 additions & 107 deletions frame-metadata/src/v13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ impl From<RuntimeMetadataLastVersion> 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<ModuleMetadata<PortableForm>>,
/// Metadata of all the pallets.
pub pallets: Vec<PalletMetadata<PortableForm>>,
/// Metadata of the extrinsic.
pub extrinsic: ExtrinsicMetadata<PortableForm>,
}

impl RuntimeMetadataV13 {
pub fn new(modules: Vec<ModuleMetadata>, extrinsic: ExtrinsicMetadata) -> Self {
pub fn new(pallets: Vec<PalletMetadata>, 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,
}
}
Expand Down Expand Up @@ -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<T: Form = MetaForm> {
pub struct PalletMetadata<T: Form = MetaForm> {
pub name: T::String,
pub storage: Option<StorageMetadata<T>>,
pub calls: Option<Vec<FunctionMetadata<T>>>,
pub event: Option<Vec<EventMetadata<T>>>,
pub constants: 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,
pub storage: Option<PalletStorageMetadata<T>>,
pub calls: Option<PalletCallMetadata<T>>,
pub event: Option<PalletEventMetadata<T>>,
pub constants: Vec<PalletConstantMetadata<T>>,
pub error: Option<PalletErrorMetadata<T>>,
/// 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<PortableForm>;
impl IntoPortable for PalletMetadata {
type Output = PalletMetadata<PortableForm>;

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<T: Form = MetaForm> {
pub struct PalletStorageMetadata<T: Form = MetaForm> {
/// The common prefix used by all storage entries.
pub prefix: T::String,
pub entries: Vec<StorageEntryMetadata<T>>,
}

impl IntoPortable for StorageMetadata {
type Output = StorageMetadata<PortableForm>;
impl IntoPortable for PalletStorageMetadata {
type Output = PalletStorageMetadata<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
StorageMetadata {
PalletStorageMetadata {
prefix: self.prefix.into_portable(registry),
entries: registry.map_into_portable(self.entries),
}
Expand Down Expand Up @@ -290,144 +290,110 @@ impl IntoPortable for StorageEntryType {
}
}

/// All the metadata about a function.
/// 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 FunctionMetadata<T: Form = MetaForm> {
pub name: T::String,
pub arguments: Vec<FunctionArgumentMetadata<T>>,
pub documentation: Vec<T::String>,
}

impl IntoPortable for FunctionMetadata {
type Output = FunctionMetadata<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
FunctionMetadata {
name: self.name.into_portable(registry),
arguments: registry.map_into_portable(self.arguments),
documentation: registry.map_into_portable(self.documentation),
}
}
}

/// 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<T: Form = MetaForm> {
pub name: T::String,
pub struct PalletCallMetadata<T: Form = MetaForm> {
/// The corresponding enum type for the pallet call.
pub ty: T::Type,
pub calls: Vec<FunctionMetadata<T>>,
}

impl IntoPortable for FunctionArgumentMetadata {
type Output = FunctionArgumentMetadata<PortableForm>;
impl IntoPortable for PalletCallMetadata {
type Output = PalletCallMetadata<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
FunctionArgumentMetadata {
name: self.name.into_portable(registry),
PalletCallMetadata {
ty: registry.register_type(&self.ty),
calls: registry.map_into_portable(self.calls),
}
}
}

/// All the metadata about an outer event.
/// All the metadata about a function.
#[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 struct FunctionMetadata<T: Form = MetaForm> {
pub name: T::String,
pub events: Vec<ModuleEventMetadata<T>>,
pub arguments: Vec<FunctionArgumentMetadata<T>>,
pub documentation: Vec<T::String>,
}

impl IntoPortable for OuterEventMetadata {
type Output = OuterEventMetadata<PortableForm>;
impl IntoPortable for FunctionMetadata {
type Output = FunctionMetadata<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
OuterEventMetadata {
FunctionMetadata {
name: self.name.into_portable(registry),
events: registry.map_into_portable(self.events),
arguments: registry.map_into_portable(self.arguments),
documentation: registry.map_into_portable(self.documentation),
}
}
}

/// Metadata about a module event.
/// 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 ModuleEventMetadata<T: Form = MetaForm> {
pub struct FunctionArgumentMetadata<T: Form = MetaForm> {
pub name: T::String,
pub events: Vec<EventMetadata<T>>,
pub ty: T::Type,
}

impl IntoPortable for ModuleEventMetadata {
type Output = ModuleEventMetadata<PortableForm>;
impl IntoPortable for FunctionArgumentMetadata {
type Output = FunctionArgumentMetadata<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
ModuleEventMetadata {
FunctionArgumentMetadata {
name: self.name.into_portable(registry),
events: registry.map_into_portable(self.events),
ty: registry.register_type(&self.ty),
}
}
}

/// All the metadata about an 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 EventMetadata<T: Form = MetaForm> {
pub name: T::String,
pub arguments: Vec<TypeSpec<T>>,
pub documentation: Vec<T::String>,
pub struct PalletEventMetadata<T: Form = MetaForm> {
pub ty: T::Type,
}

impl IntoPortable for EventMetadata {
type Output = EventMetadata<PortableForm>;
impl IntoPortable for PalletEventMetadata {
type Output = PalletEventMetadata<PortableForm>;

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),
PalletEventMetadata {
ty: registry.register_type(&self.ty),
}
}
}

/// All the metadata about one module constant.
/// 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 ModuleConstantMetadata<T: Form = MetaForm> {
pub struct PalletConstantMetadata<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>;
impl IntoPortable for PalletConstantMetadata {
type Output = PalletConstantMetadata<PortableForm>;

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,
Expand All @@ -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<T: Form = MetaForm> {
pub name: T::String,
pub documentation: Vec<T::String>,
#[cfg_attr(feature = "std", serde(bound(serialize = "T::Type: Serialize")))]
pub struct PalletErrorMetadata<T: Form = MetaForm> {
/// The error type information.
pub ty: T::Type,
}

impl IntoPortable for ErrorMetadata {
type Output = ErrorMetadata<PortableForm>;
impl IntoPortable for PalletErrorMetadata {
type Output = PalletErrorMetadata<PortableForm>;

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),
}
}
}
Expand Down