Metadata V15: Add Runtime API metadata (#13302)

* impl_runtime_apis: Generate getters for `metadata_at` functions

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* runtime: Implement new `Metadata` runtime trait

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* runtime: Move `metadata_at` functions to construct_runtime macro

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* contruct_runtime: Use `OpaqueMetadata` from hidden imports

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Adjust testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/tests: Add tests for the new API

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Helper to extract documentation literals

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Helper to filter all `cfg` attributes

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Generate documentation getters for metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Avoid trait collision with snake case methods

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* proc-macro/tests: Check doc getters

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Generate metadata for runtime methods

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/api: Export scale-info and frame-metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Generate metadata for runtime traits

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/runtime: Expose metadata v15 internally

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* test: Use metadata v15 from `lexnv/md_v15_test` branch

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Generate crate access one module up

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Implement `runtime_metadata` for mocks and tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Fix warnings

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Add no-docs flag

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Adjust more tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/tests: Check runtime metadata correctness

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/benchmarking: Adjust benchmarks

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/benchmarks: Adjust more benchmarks

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/api: Fix clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/proc-macro: Generate runtime metadata on the `decl_runtime_apis`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Abuse Deref to resolve `runtime_metadata`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "frame: Implement `runtime_metadata` for mocks and tests"

This reverts commit b7f41aa189218589392a6e713ea9488e93c4db45.

Revert "frame: Adjust more tests"

This reverts commit 3cba5982c7f45552e76335e96c430aecbc42d8c6.

Revert "frame/benchmarking: Adjust benchmarks"

This reverts commit 60b382ada486c791ffceeb65da587e949b90ec5d.

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

Revert "frame/benchmarks: Adjust more benchmarks"

This reverts commit eb75c477179b1a27347a5554c5732ef26a00d7e8.

* primitives/proc-macro: Remove unused imports and function

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/support: Adjust runtime metadata test

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/tests: Remove doc getter test

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/support: Enable `no-metadata-docs` feature from `sp-api`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/tests: Add `TypeInfo` for test::extrinsic

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/api: Expose scale-info and frame-metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update frame-metadata to include v15

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix merge conflicts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/metadata_ir: Add IR for runtime API metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/metadata_ir: Convert IR to V15

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/api: Collect IR metadata for runtime API

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/api: Move `metadata_ir` from frame/support

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/tests: Adjust testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/tests: Adjust `metadata_versions` test

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/runtime_metadata: Exclude default type parameters from methods

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update primitives/api/proc-macro/src/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/src/metadata_ir/types.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/src/metadata_ir/mod.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/proc-macro/src/utils.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/proc-macro/src/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/proc-macro/src/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/proc-macro/src/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/proc-macro/src/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/proc-macro/src/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/proc-macro/src/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* primitives: Fix build

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/metadata-ir: Move IR to dedicated crate

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives: Reexport metadata-ir and frame-metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Use apis field instead of runtime

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Better documentation for the `Deref` abstraction

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ui-tests: Check empty `impl_runtime_apis`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives: Remove unneeded bounds on generic params

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives: Rename `collect_where_bounds` to `get_argument_type_param`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives: Generate crate access per fn call

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "primitives: Remove unneeded bounds on generic params"

This reverts commit 5178e38cf21cfb481156eefd628d62989201d59a.

* metadata-ir: Add no-std

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives: Adjust where bounds

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Change `frame-metadata` branch to "origin/main"

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update to `main` from origin

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update frame-metadata to crates.io v15.1

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "ui-tests: Check empty `impl_runtime_apis`"

This reverts commit cf78a7190ad9cba3c3bb2e78dc3d0dc382b2fea9.

* Move ui test to primitives/ui

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update frame/support/test/tests/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/api/proc-macro/src/runtime_metadata.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Test already covered by `empty_impl_runtime_apis_call.stderr`

This reverts commit 3bafb294cbe9745569bf5e5a1a2e6b4a4c1aadc5.

* Retriger CI

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Import `TokenStream` as `TokenStream2`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: parity-processbot <>
Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Alexandru Vasile
2023-04-11 14:07:52 +03:00
committed by GitHub
parent 17765733f0
commit 29ea9e0ad9
25 changed files with 993 additions and 31 deletions
+1 -1
View File
@@ -50,6 +50,7 @@ pub use paste;
pub use scale_info;
#[cfg(feature = "std")]
pub use serde;
pub use sp_api::metadata_ir;
pub use sp_core::{OpaqueMetadata, Void};
#[doc(hidden)]
pub use sp_core_hashing_proc_macro;
@@ -80,7 +81,6 @@ pub mod error;
pub mod crypto;
pub mod dispatch_context;
pub mod instances;
pub mod metadata_ir;
pub mod migrations;
pub mod traits;
pub mod weights;
@@ -1,82 +0,0 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Intermediate representation of the runtime metadata.
mod types;
use frame_metadata::{RuntimeMetadataPrefixed, RuntimeMetadataV14};
pub use types::*;
mod v14;
/// Metadata V14.
const V14: u32 = 14;
/// Transform the IR to the specified version.
///
/// Use [`supported_versions`] to find supported versions.
pub fn into_version(metadata: MetadataIR, version: u32) -> Option<RuntimeMetadataPrefixed> {
match version {
// Latest stable version.
V14 => {
let v14: frame_metadata::v14::RuntimeMetadataV14 = metadata.into();
Some(v14.into())
},
_ => None,
}
}
/// Returns the supported metadata versions.
pub fn supported_versions() -> sp_std::vec::Vec<u32> {
sp_std::vec![V14,]
}
/// Transform the IR to the latest stable metadata version.
pub fn into_latest(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
let latest: RuntimeMetadataV14 = metadata.into();
latest.into()
}
#[cfg(test)]
mod test {
use super::*;
use crate::metadata_ir::ExtrinsicMetadataIR;
use frame_metadata::{v14::META_RESERVED, RuntimeMetadata};
use scale_info::meta_type;
fn ir_metadata() -> MetadataIR {
MetadataIR {
pallets: vec![],
extrinsic: ExtrinsicMetadataIR {
ty: meta_type::<()>(),
version: 0,
signed_extensions: vec![],
},
ty: meta_type::<()>(),
}
}
#[test]
fn into_version_14() {
let ir = ir_metadata();
let metadata = into_version(ir, V14).expect("Should return prefixed metadata");
assert_eq!(metadata.0, META_RESERVED);
assert!(matches!(metadata.1, RuntimeMetadata::V14(_)));
}
}
@@ -1,329 +0,0 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use codec::Encode;
use scale_info::{
form::{Form, MetaForm, PortableForm},
prelude::vec::Vec,
IntoPortable, MetaType, Registry,
};
/// The intermediate representation for the runtime metadata.
/// Contains the needed context that allows conversion to multiple metadata versions.
///
/// # Note
///
/// Further fields could be added or removed to ensure proper conversion.
/// When the IR does not contain enough information to generate a specific version
/// of the runtime metadata an appropriate default value is used (ie, empty vector).
pub struct MetadataIR<T: Form = MetaForm> {
/// Pallet metadata.
pub pallets: Vec<PalletMetadataIR<T>>,
/// Metadata of the extrinsic.
pub extrinsic: ExtrinsicMetadataIR<T>,
/// The type of the `Runtime`.
pub ty: T::Type,
}
/// The intermediate representation for a pallet metadata.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletMetadataIR<T: Form = MetaForm> {
/// Pallet name.
pub name: T::String,
/// Pallet storage metadata.
pub storage: Option<PalletStorageMetadataIR<T>>,
/// Pallet calls metadata.
pub calls: Option<PalletCallMetadataIR<T>>,
/// Pallet event metadata.
pub event: Option<PalletEventMetadataIR<T>>,
/// Pallet constants metadata.
pub constants: Vec<PalletConstantMetadataIR<T>>,
/// Pallet error metadata.
pub error: Option<PalletErrorMetadataIR<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,
/// Pallet documentation.
pub docs: Vec<T::String>,
}
impl IntoPortable for PalletMetadataIR {
type Output = PalletMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletMetadataIR {
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 the extrinsic used by the runtime.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct ExtrinsicMetadataIR<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<SignedExtensionMetadataIR<T>>,
}
impl IntoPortable for ExtrinsicMetadataIR {
type Output = ExtrinsicMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
ExtrinsicMetadataIR {
ty: registry.register_type(&self.ty),
version: self.version,
signed_extensions: registry.map_into_portable(self.signed_extensions),
}
}
}
/// Metadata of an extrinsic's signed extension.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct SignedExtensionMetadataIR<T: Form = MetaForm> {
/// 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 SignedExtensionMetadataIR {
type Output = SignedExtensionMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
SignedExtensionMetadataIR {
identifier: self.identifier.into_portable(registry),
ty: registry.register_type(&self.ty),
additional_signed: registry.register_type(&self.additional_signed),
}
}
}
/// All metadata of the pallet's storage.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
/// The common prefix used by all storage entries.
pub struct PalletStorageMetadataIR<T: Form = MetaForm> {
/// The common prefix used by all storage entries.
pub prefix: T::String,
/// Metadata for all storage entries.
pub entries: Vec<StorageEntryMetadataIR<T>>,
}
impl IntoPortable for PalletStorageMetadataIR {
type Output = PalletStorageMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletStorageMetadataIR {
prefix: self.prefix.into_portable(registry),
entries: registry.map_into_portable(self.entries),
}
}
}
/// Metadata about one storage entry.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct StorageEntryMetadataIR<T: Form = MetaForm> {
/// Variable name of the storage entry.
pub name: T::String,
/// An `Option` modifier of that storage entry.
pub modifier: StorageEntryModifierIR,
/// Type of the value stored in the entry.
pub ty: StorageEntryTypeIR<T>,
/// Default value (SCALE encoded).
pub default: Vec<u8>,
/// Storage entry documentation.
pub docs: Vec<T::String>,
}
impl IntoPortable for StorageEntryMetadataIR {
type Output = StorageEntryMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
StorageEntryMetadataIR {
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<T>`, 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)]
pub enum StorageEntryModifierIR {
/// The storage entry returns an `Option<T>`, 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)]
pub enum StorageHasherIR {
/// 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)]
pub enum StorageEntryTypeIR<T: Form = MetaForm> {
/// 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<StorageHasherIR>,
/// 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 StorageEntryTypeIR {
type Output = StorageEntryTypeIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
match self {
Self::Plain(plain) => StorageEntryTypeIR::Plain(registry.register_type(&plain)),
Self::Map { hashers, key, value } => StorageEntryTypeIR::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)]
pub struct PalletCallMetadataIR<T: Form = MetaForm> {
/// The corresponding enum type for the pallet call.
pub ty: T::Type,
}
impl IntoPortable for PalletCallMetadataIR {
type Output = PalletCallMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletCallMetadataIR { ty: registry.register_type(&self.ty) }
}
}
impl From<MetaType> for PalletCallMetadataIR {
fn from(ty: MetaType) -> Self {
Self { ty }
}
}
/// Metadata about the pallet Event type.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletEventMetadataIR<T: Form = MetaForm> {
/// The Event type.
pub ty: T::Type,
}
impl IntoPortable for PalletEventMetadataIR {
type Output = PalletEventMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletEventMetadataIR { ty: registry.register_type(&self.ty) }
}
}
impl From<MetaType> for PalletEventMetadataIR {
fn from(ty: MetaType) -> Self {
Self { ty }
}
}
/// Metadata about one pallet constant.
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletConstantMetadataIR<T: Form = MetaForm> {
/// 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<u8>,
/// Documentation of the constant.
pub docs: Vec<T::String>,
}
impl IntoPortable for PalletConstantMetadataIR {
type Output = PalletConstantMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletConstantMetadataIR {
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)]
pub struct PalletErrorMetadataIR<T: Form = MetaForm> {
/// The error type information.
pub ty: T::Type,
}
impl IntoPortable for PalletErrorMetadataIR {
type Output = PalletErrorMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletErrorMetadataIR { ty: registry.register_type(&self.ty) }
}
}
impl From<MetaType> for PalletErrorMetadataIR {
fn from(ty: MetaType) -> Self {
Self { ty }
}
}
@@ -1,158 +0,0 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Convert the IR to V14 metadata.
use super::types::{
ExtrinsicMetadataIR, MetadataIR, PalletCallMetadataIR, PalletConstantMetadataIR,
PalletErrorMetadataIR, PalletEventMetadataIR, PalletMetadataIR, PalletStorageMetadataIR,
SignedExtensionMetadataIR, StorageEntryMetadataIR, StorageEntryModifierIR, StorageEntryTypeIR,
StorageHasherIR,
};
use frame_metadata::v14::{
ExtrinsicMetadata, PalletCallMetadata, PalletConstantMetadata, PalletErrorMetadata,
PalletEventMetadata, PalletMetadata, PalletStorageMetadata, RuntimeMetadataV14,
SignedExtensionMetadata, StorageEntryMetadata, StorageEntryModifier, StorageEntryType,
StorageHasher,
};
impl From<MetadataIR> for RuntimeMetadataV14 {
fn from(ir: MetadataIR) -> Self {
RuntimeMetadataV14::new(
ir.pallets.into_iter().map(Into::into).collect(),
ir.extrinsic.into(),
ir.ty,
)
}
}
impl From<PalletMetadataIR> for PalletMetadata {
fn from(ir: PalletMetadataIR) -> Self {
PalletMetadata {
name: ir.name,
storage: ir.storage.map(Into::into),
calls: ir.calls.map(Into::into),
event: ir.event.map(Into::into),
constants: ir.constants.into_iter().map(Into::into).collect(),
error: ir.error.map(Into::into),
index: ir.index,
// Note: ir.docs not part of v14.
}
}
}
impl From<StorageEntryModifierIR> for StorageEntryModifier {
fn from(ir: StorageEntryModifierIR) -> Self {
match ir {
StorageEntryModifierIR::Optional => StorageEntryModifier::Optional,
StorageEntryModifierIR::Default => StorageEntryModifier::Default,
}
}
}
impl From<StorageHasherIR> for StorageHasher {
fn from(ir: StorageHasherIR) -> Self {
match ir {
StorageHasherIR::Blake2_128 => StorageHasher::Blake2_128,
StorageHasherIR::Blake2_256 => StorageHasher::Blake2_256,
StorageHasherIR::Blake2_128Concat => StorageHasher::Blake2_128Concat,
StorageHasherIR::Twox128 => StorageHasher::Twox128,
StorageHasherIR::Twox256 => StorageHasher::Twox256,
StorageHasherIR::Twox64Concat => StorageHasher::Twox64Concat,
StorageHasherIR::Identity => StorageHasher::Identity,
}
}
}
impl From<StorageEntryTypeIR> for StorageEntryType {
fn from(ir: StorageEntryTypeIR) -> Self {
match ir {
StorageEntryTypeIR::Plain(ty) => StorageEntryType::Plain(ty),
StorageEntryTypeIR::Map { hashers, key, value } => StorageEntryType::Map {
hashers: hashers.into_iter().map(Into::into).collect(),
key,
value,
},
}
}
}
impl From<StorageEntryMetadataIR> for StorageEntryMetadata {
fn from(ir: StorageEntryMetadataIR) -> Self {
StorageEntryMetadata {
name: ir.name,
modifier: ir.modifier.into(),
ty: ir.ty.into(),
default: ir.default,
docs: ir.docs,
}
}
}
impl From<PalletStorageMetadataIR> for PalletStorageMetadata {
fn from(ir: PalletStorageMetadataIR) -> Self {
PalletStorageMetadata {
prefix: ir.prefix,
entries: ir.entries.into_iter().map(Into::into).collect(),
}
}
}
impl From<PalletCallMetadataIR> for PalletCallMetadata {
fn from(ir: PalletCallMetadataIR) -> Self {
PalletCallMetadata { ty: ir.ty }
}
}
impl From<PalletEventMetadataIR> for PalletEventMetadata {
fn from(ir: PalletEventMetadataIR) -> Self {
PalletEventMetadata { ty: ir.ty }
}
}
impl From<PalletConstantMetadataIR> for PalletConstantMetadata {
fn from(ir: PalletConstantMetadataIR) -> Self {
PalletConstantMetadata { name: ir.name, ty: ir.ty, value: ir.value, docs: ir.docs }
}
}
impl From<PalletErrorMetadataIR> for PalletErrorMetadata {
fn from(ir: PalletErrorMetadataIR) -> Self {
PalletErrorMetadata { ty: ir.ty }
}
}
impl From<SignedExtensionMetadataIR> for SignedExtensionMetadata {
fn from(ir: SignedExtensionMetadataIR) -> Self {
SignedExtensionMetadata {
identifier: ir.identifier,
ty: ir.ty,
additional_signed: ir.additional_signed,
}
}
}
impl From<ExtrinsicMetadataIR> for ExtrinsicMetadata {
fn from(ir: ExtrinsicMetadataIR) -> Self {
ExtrinsicMetadata {
ty: ir.ty,
version: ir.version,
signed_extensions: ir.signed_extensions.into_iter().map(Into::into).collect(),
}
}
}