Skip to content

Commit 4148afb

Browse files
authored
fix: runtime API implementations not exported (#809)
Fixes KILTprotocol/ticket#3688. Due to how the `impl_runtime_apis` macro works, it is expected to be included in a runtime's `lib.rs` file. Since we split up the runtimes, the runtime APIs were not included anymore. I found a related issue in the subxt repo: paritytech/subxt#1873. Because the trait definition generated by the macro is local, we can't import it in the `lib.rs` module for the `construct_runtime` macro to pick up the right implementation, so we need a workaround that basically introduces a new marker trait, and we import _that_ in the `lib.rs` file so that the right implementation of `Runtime::runtime_metadata()` is picked up. More details about the issue are presented in the subxt ticket. ## How to test Spin up a chopsticks deployment after building peregrine and spiritnet runtime, and verify that the runtime APIs are there now.
1 parent 4f5429d commit 4148afb

File tree

8 files changed

+43
-0
lines changed

8 files changed

+43
-0
lines changed

Cargo.lock

Lines changed: 2 additions & 0 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
@@ -169,6 +169,7 @@ sp-core = { git = "https://github.com/parityt
169169
sp-genesis-builder = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
170170
sp-inherents = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
171171
sp-io = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
172+
sp-metadata-ir = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
172173
sp-offchain = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
173174
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
174175
sp-session = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }

runtimes/peregrine/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ sp-block-builder = { workspace = true }
5959
sp-consensus-aura = { workspace = true }
6060
sp-core = { workspace = true }
6161
sp-inherents = { workspace = true }
62+
sp-metadata-ir = { workspace = true }
6263
sp-offchain = { workspace = true }
6364
sp-runtime = { workspace = true }
6465
sp-session = { workspace = true }
@@ -241,6 +242,7 @@ std = [
241242
"sp-core/std",
242243
"sp-genesis-builder/std",
243244
"sp-inherents/std",
245+
"sp-metadata-ir/std",
244246
"sp-offchain/std",
245247
"sp-runtime/std",
246248
"sp-session/std",

runtimes/peregrine/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ mod migrations;
5454
pub use migrations::RuntimeMigrations;
5555
mod parachain;
5656
mod runtime_apis;
57+
use runtime_apis::_InternalImplRuntimeApis;
5758
pub use runtime_apis::{api, RuntimeApi};
5859
mod system;
5960
use sp_version::RuntimeVersion;

runtimes/peregrine/src/runtime_apis.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
1616
use sp_api::impl_runtime_apis;
1717
use sp_core::OpaqueMetadata;
1818
use sp_inherents::{CheckInherentsResult, InherentData};
19+
use sp_metadata_ir::RuntimeApiMetadataIR;
1920
use sp_runtime::{
2021
traits::{Block as BlockT, TryConvert},
2122
ApplyExtrinsicResult, KeyTypeId,
@@ -53,6 +54,22 @@ use crate::{
5354
// `impl_runtime_apis` is private.
5455
pub(crate) const RUNTIME_API_VERSION: ApisVec = RUNTIME_API_VERSIONS;
5556

57+
// Workaround for runtime API impls not exposed in metadata if implemented in a
58+
// different file than the runtime's `lib.rs`. Related issue (subxt) -> https://github.com/paritytech/subxt/issues/1873.
59+
pub(crate) trait _InternalImplRuntimeApis {
60+
fn runtime_metadata(&self) -> Vec<RuntimeApiMetadataIR>;
61+
}
62+
63+
impl<T> _InternalImplRuntimeApis for T
64+
where
65+
T: InternalImplRuntimeApis,
66+
{
67+
#[inline(always)]
68+
fn runtime_metadata(&self) -> Vec<RuntimeApiMetadataIR> {
69+
<T as InternalImplRuntimeApis>::runtime_metadata(self)
70+
}
71+
}
72+
5673
impl_runtime_apis! {
5774
impl sp_api::Core<Block> for Runtime {
5875
fn version() -> RuntimeVersion {

runtimes/spiritnet/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ sp-block-builder = { workspace = true }
5959
sp-consensus-aura = { workspace = true }
6060
sp-core = { workspace = true }
6161
sp-inherents = { workspace = true }
62+
sp-metadata-ir = { workspace = true }
6263
sp-offchain = { workspace = true }
6364
sp-runtime = { workspace = true }
6465
sp-session = { workspace = true }
@@ -240,6 +241,7 @@ std = [
240241
"sp-core/std",
241242
"sp-genesis-builder/std",
242243
"sp-inherents/std",
244+
"sp-metadata-ir/std",
243245
"sp-offchain/std",
244246
"sp-runtime/std",
245247
"sp-session/std",

runtimes/spiritnet/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ mod migrations;
5454
pub use migrations::RuntimeMigrations;
5555
mod parachain;
5656
mod runtime_apis;
57+
use runtime_apis::_InternalImplRuntimeApis;
5758
pub use runtime_apis::{api, RuntimeApi};
5859
mod system;
5960
use sp_version::RuntimeVersion;

runtimes/spiritnet/src/runtime_apis.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
1616
use sp_api::impl_runtime_apis;
1717
use sp_core::OpaqueMetadata;
1818
use sp_inherents::{CheckInherentsResult, InherentData};
19+
use sp_metadata_ir::RuntimeApiMetadataIR;
1920
use sp_runtime::{
2021
traits::{Block as BlockT, TryConvert},
2122
ApplyExtrinsicResult, KeyTypeId,
@@ -53,6 +54,22 @@ use crate::{
5354
// `impl_runtime_apis` is private.
5455
pub(crate) const RUNTIME_API_VERSION: ApisVec = RUNTIME_API_VERSIONS;
5556

57+
// Workaround for runtime API impls not exposed in metadata if implemented in a
58+
// different file than the runtime's `lib.rs`. Related issue (subxt) -> https://github.com/paritytech/subxt/issues/1873.
59+
pub(crate) trait _InternalImplRuntimeApis {
60+
fn runtime_metadata(&self) -> Vec<RuntimeApiMetadataIR>;
61+
}
62+
63+
impl<T> _InternalImplRuntimeApis for T
64+
where
65+
T: InternalImplRuntimeApis,
66+
{
67+
#[inline(always)]
68+
fn runtime_metadata(&self) -> Vec<RuntimeApiMetadataIR> {
69+
<T as InternalImplRuntimeApis>::runtime_metadata(self)
70+
}
71+
}
72+
5673
impl_runtime_apis! {
5774
impl sp_api::Core<Block> for Runtime {
5875
fn version() -> RuntimeVersion {

0 commit comments

Comments
 (0)