Skip to content

Commit 156958f

Browse files
lexnvbkchr
authored andcommitted
Metadata V15: Add Runtime API metadata (paritytech#13302)
* impl_runtime_apis: Generate getters for `metadata_at` functions Signed-off-by: Alexandru Vasile <[email protected]> * runtime: Implement new `Metadata` runtime trait Signed-off-by: Alexandru Vasile <[email protected]> * runtime: Move `metadata_at` functions to construct_runtime macro Signed-off-by: Alexandru Vasile <[email protected]> * contruct_runtime: Use `OpaqueMetadata` from hidden imports Signed-off-by: Alexandru Vasile <[email protected]> * Adjust testing Signed-off-by: Alexandru Vasile <[email protected]> * frame/tests: Add tests for the new API Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Helper to extract documentation literals Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Helper to filter all `cfg` attributes Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Generate documentation getters for metadata Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Avoid trait collision with snake case methods Signed-off-by: Alexandru Vasile <[email protected]> * proc-macro/tests: Check doc getters Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Generate metadata for runtime methods Signed-off-by: Alexandru Vasile <[email protected]> * primitives/api: Export scale-info and frame-metadata Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Generate metadata for runtime traits Signed-off-by: Alexandru Vasile <[email protected]> * frame/runtime: Expose metadata v15 internally Signed-off-by: Alexandru Vasile <[email protected]> * test: Use metadata v15 from `lexnv/md_v15_test` branch Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Generate crate access one module up Signed-off-by: Alexandru Vasile <[email protected]> * frame: Implement `runtime_metadata` for mocks and tests Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Fix warnings Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Add no-docs flag Signed-off-by: Alexandru Vasile <[email protected]> * frame: Adjust more tests Signed-off-by: Alexandru Vasile <[email protected]> * frame/tests: Check runtime metadata correctness Signed-off-by: Alexandru Vasile <[email protected]> * frame/benchmarking: Adjust benchmarks Signed-off-by: Alexandru Vasile <[email protected]> * frame/benchmarks: Adjust more benchmarks Signed-off-by: Alexandru Vasile <[email protected]> * primitives/api: Fix clippy Signed-off-by: Alexandru Vasile <[email protected]> * primitives/proc-macro: Generate runtime metadata on the `decl_runtime_apis` Signed-off-by: Alexandru Vasile <[email protected]> * frame: Abuse Deref to resolve `runtime_metadata` Signed-off-by: Alexandru Vasile <[email protected]> * Revert "frame: Implement `runtime_metadata` for mocks and tests" This reverts commit e4782de. Revert "frame: Adjust more tests" This reverts commit de1352c. Revert "frame/benchmarking: Adjust benchmarks" This reverts commit ae85bbe. Signed-off-by: Alexandru Vasile <[email protected]> Revert "frame/benchmarks: Adjust more benchmarks" This reverts commit d37aa22. * primitives/proc-macro: Remove unused imports and function Signed-off-by: Alexandru Vasile <[email protected]> * frame/support: Adjust runtime metadata test Signed-off-by: Alexandru Vasile <[email protected]> * primitives/tests: Remove doc getter test Signed-off-by: Alexandru Vasile <[email protected]> * frame/support: Enable `no-metadata-docs` feature from `sp-api` Signed-off-by: Alexandru Vasile <[email protected]> * primitives/tests: Add `TypeInfo` for test::extrinsic Signed-off-by: Alexandru Vasile <[email protected]> * primitives/api: Expose scale-info and frame-metadata Signed-off-by: Alexandru Vasile <[email protected]> * Update frame-metadata to include v15 Signed-off-by: Alexandru Vasile <[email protected]> * Fix merge conflicts Signed-off-by: Alexandru Vasile <[email protected]> * frame/metadata_ir: Add IR for runtime API metadata Signed-off-by: Alexandru Vasile <[email protected]> * frame/metadata_ir: Convert IR to V15 Signed-off-by: Alexandru Vasile <[email protected]> * primitives/api: Collect IR metadata for runtime API Signed-off-by: Alexandru Vasile <[email protected]> * primitives/api: Move `metadata_ir` from frame/support Signed-off-by: Alexandru Vasile <[email protected]> * frame/tests: Adjust testing Signed-off-by: Alexandru Vasile <[email protected]> * frame/tests: Adjust `metadata_versions` test Signed-off-by: Alexandru Vasile <[email protected]> * primitives/runtime_metadata: Exclude default type parameters from methods Signed-off-by: Alexandru Vasile <[email protected]> * Update primitives/api/proc-macro/src/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/src/metadata_ir/types.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/src/metadata_ir/mod.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/proc-macro/src/utils.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/proc-macro/src/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/proc-macro/src/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/proc-macro/src/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/proc-macro/src/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/proc-macro/src/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/proc-macro/src/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * primitives: Fix build Signed-off-by: Alexandru Vasile <[email protected]> * primitives/metadata-ir: Move IR to dedicated crate Signed-off-by: Alexandru Vasile <[email protected]> * primitives: Reexport metadata-ir and frame-metadata Signed-off-by: Alexandru Vasile <[email protected]> * frame: Use apis field instead of runtime Signed-off-by: Alexandru Vasile <[email protected]> * Better documentation for the `Deref` abstraction Signed-off-by: Alexandru Vasile <[email protected]> * ui-tests: Check empty `impl_runtime_apis` Signed-off-by: Alexandru Vasile <[email protected]> * primitives: Remove unneeded bounds on generic params Signed-off-by: Alexandru Vasile <[email protected]> * primitives: Rename `collect_where_bounds` to `get_argument_type_param` Signed-off-by: Alexandru Vasile <[email protected]> * primitives: Generate crate access per fn call Signed-off-by: Alexandru Vasile <[email protected]> * Revert "primitives: Remove unneeded bounds on generic params" This reverts commit 5178e38. * metadata-ir: Add no-std Signed-off-by: Alexandru Vasile <[email protected]> * primitives: Adjust where bounds Signed-off-by: Alexandru Vasile <[email protected]> * Change `frame-metadata` branch to "origin/main" Signed-off-by: Alexandru Vasile <[email protected]> * Update to `main` from origin Signed-off-by: Alexandru Vasile <[email protected]> * Update frame-metadata to crates.io v15.1 Signed-off-by: Alexandru Vasile <[email protected]> * Revert "ui-tests: Check empty `impl_runtime_apis`" This reverts commit cf78a71. * Move ui test to primitives/ui Signed-off-by: Alexandru Vasile <[email protected]> * Update frame/support/test/tests/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * Update primitives/api/proc-macro/src/runtime_metadata.rs Co-authored-by: Bastian Köcher <[email protected]> * Test already covered by `empty_impl_runtime_apis_call.stderr` This reverts commit 3bafb29. * Retriger CI Signed-off-by: Alexandru Vasile <[email protected]> * Import `TokenStream` as `TokenStream2` Signed-off-by: Alexandru Vasile <[email protected]> --------- Signed-off-by: Alexandru Vasile <[email protected]> Co-authored-by: parity-processbot <> Co-authored-by: Bastian Köcher <[email protected]>
1 parent c82e142 commit 156958f

File tree

25 files changed

+993
-31
lines changed

25 files changed

+993
-31
lines changed

Cargo.lock

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ members = [
202202
"primitives/keystore",
203203
"primitives/maybe-compressed-blob",
204204
"primitives/merkle-mountain-range",
205+
"primitives/metadata-ir",
205206
"primitives/npos-elections",
206207
"primitives/npos-elections/fuzzer",
207208
"primitives/offchain",

frame/support/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1616
serde = { version = "1.0.136", optional = true, features = ["derive"] }
1717
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive", "max-encoded-len"] }
1818
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
19-
frame-metadata = { version = "15.0.0", default-features = false, features = ["v14"] }
19+
frame-metadata = { version = "15.1.0", default-features = false, features = ["v14", "v15-unstable"] }
2020
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" }
2121
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }
2222
sp-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" }
@@ -74,7 +74,7 @@ runtime-benchmarks = []
7474
try-runtime = []
7575
# By default some types have documentation, `no-metadata-docs` allows to reduce the documentation
7676
# in the metadata.
77-
no-metadata-docs = ["frame-support-procedural/no-metadata-docs"]
77+
no-metadata-docs = ["frame-support-procedural/no-metadata-docs", "sp-api/no-metadata-docs"]
7878
# By default some types have documentation, `full-metadata-docs` allows to add documentation to
7979
# more types in the metadata.
8080
full-metadata-docs = ["scale-info/docs"]

frame/support/procedural/src/construct_runtime/expand/metadata.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ pub fn expand_runtime_metadata(
7777
quote! {
7878
impl #runtime {
7979
fn metadata_ir() -> #scrate::metadata_ir::MetadataIR {
80+
// Each runtime must expose the `runtime_metadata()` to fetch the runtime API metadata.
81+
// The function is implemented by calling `impl_runtime_apis!`.
82+
//
83+
// However, the `construct_runtime!` may be called without calling `impl_runtime_apis!`.
84+
// Rely on the `Deref` trait to differentiate between a runtime that implements
85+
// APIs (by macro impl_runtime_apis!) and a runtime that is simply created (by macro construct_runtime!).
86+
//
87+
// Both `InternalConstructRuntime` and `InternalImplRuntimeApis` expose a `runtime_metadata()` function.
88+
// `InternalConstructRuntime` is implemented by the `construct_runtime!` for Runtime references (`& Runtime`),
89+
// while `InternalImplRuntimeApis` is implemented by the `impl_runtime_apis!` for Runtime (`Runtime`).
90+
//
91+
// Therefore, the `Deref` trait will resolve the `runtime_metadata` from `impl_runtime_apis!`
92+
// when both macros are called; and will resolve an empty `runtime_metadata` when only the `construct_runtime!`
93+
// is called.
94+
//
95+
// `Deref` needs a reference for resolving the function call.
96+
let rt = #runtime;
97+
8098
#scrate::metadata_ir::MetadataIR {
8199
pallets: #scrate::sp_std::vec![ #(#pallets),* ],
82100
extrinsic: #scrate::metadata_ir::ExtrinsicMetadataIR {
@@ -95,7 +113,8 @@ pub fn expand_runtime_metadata(
95113
})
96114
.collect(),
97115
},
98-
ty: #scrate::scale_info::meta_type::<#runtime>()
116+
ty: #scrate::scale_info::meta_type::<#runtime>(),
117+
apis: (&rt).runtime_metadata(),
99118
}
100119
}
101120

frame/support/procedural/src/construct_runtime/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,31 @@ fn construct_runtime_final_expansion(
296296
type RuntimeBlock = #block;
297297
}
298298

299+
// Each runtime must expose the `runtime_metadata()` to fetch the runtime API metadata.
300+
// The function is implemented by calling `impl_runtime_apis!`.
301+
//
302+
// However, the `construct_runtime!` may be called without calling `impl_runtime_apis!`.
303+
// Rely on the `Deref` trait to differentiate between a runtime that implements
304+
// APIs (by macro impl_runtime_apis!) and a runtime that is simply created (by macro construct_runtime!).
305+
//
306+
// Both `InternalConstructRuntime` and `InternalImplRuntimeApis` expose a `runtime_metadata()` function.
307+
// `InternalConstructRuntime` is implemented by the `construct_runtime!` for Runtime references (`& Runtime`),
308+
// while `InternalImplRuntimeApis` is implemented by the `impl_runtime_apis!` for Runtime (`Runtime`).
309+
//
310+
// Therefore, the `Deref` trait will resolve the `runtime_metadata` from `impl_runtime_apis!`
311+
// when both macros are called; and will resolve an empty `runtime_metadata` when only the `construct_runtime!`
312+
// is called.
313+
314+
#[doc(hidden)]
315+
trait InternalConstructRuntime {
316+
#[inline(always)]
317+
fn runtime_metadata(&self) -> #scrate::sp_std::vec::Vec<#scrate::metadata_ir::RuntimeApiMetadataIR> {
318+
Default::default()
319+
}
320+
}
321+
#[doc(hidden)]
322+
impl InternalConstructRuntime for &#name {}
323+
299324
#outer_event
300325

301326
#outer_origin

frame/support/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub use paste;
5050
pub use scale_info;
5151
#[cfg(feature = "std")]
5252
pub use serde;
53+
pub use sp_api::metadata_ir;
5354
pub use sp_core::{OpaqueMetadata, Void};
5455
#[doc(hidden)]
5556
pub use sp_core_hashing_proc_macro;
@@ -80,7 +81,6 @@ pub mod error;
8081
pub mod crypto;
8182
pub mod dispatch_context;
8283
pub mod instances;
83-
pub mod metadata_ir;
8484
pub mod migrations;
8585
pub mod traits;
8686
pub mod weights;

frame/support/test/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1515
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
1616
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
1717
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
18+
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/api" }
1819
sp-arithmetic = { version = "6.0.0", default-features = false, path = "../../../primitives/arithmetic" }
1920
sp-io = { version = "7.0.0", path = "../../../primitives/io", default-features = false }
2021
sp-state-machine = { version = "0.13.0", optional = true, path = "../../../primitives/state-machine" }
@@ -47,6 +48,7 @@ std = [
4748
"sp-state-machine",
4849
"sp-arithmetic/std",
4950
"sp-version/std",
51+
"sp-api/std",
5052
]
5153
try-runtime = ["frame-support/try-runtime"]
5254
# WARNING:

frame/support/test/tests/pallet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ fn metadata_at_version() {
16711671

16721672
#[test]
16731673
fn metadata_versions() {
1674-
assert_eq!(vec![LATEST_METADATA_VERSION], Runtime::metadata_versions());
1674+
assert_eq!(vec![LATEST_METADATA_VERSION, u32::MAX], Runtime::metadata_versions());
16751675
}
16761676

16771677
#[test]

0 commit comments

Comments
 (0)