Skip to content

Commit

Permalink
fix: runtime API implementations not exported (#809)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ntn-x2 authored Nov 27, 2024
1 parent 4f5429d commit 4148afb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ sp-core = { git = "https://github.com/parityt
sp-genesis-builder = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
sp-inherents = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
sp-io = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
sp-metadata-ir = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
sp-offchain = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
sp-session = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
Expand Down
2 changes: 2 additions & 0 deletions runtimes/peregrine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sp-block-builder = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-core = { workspace = true }
sp-inherents = { workspace = true }
sp-metadata-ir = { workspace = true }
sp-offchain = { workspace = true }
sp-runtime = { workspace = true }
sp-session = { workspace = true }
Expand Down Expand Up @@ -241,6 +242,7 @@ std = [
"sp-core/std",
"sp-genesis-builder/std",
"sp-inherents/std",
"sp-metadata-ir/std",
"sp-offchain/std",
"sp-runtime/std",
"sp-session/std",
Expand Down
1 change: 1 addition & 0 deletions runtimes/peregrine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod migrations;
pub use migrations::RuntimeMigrations;
mod parachain;
mod runtime_apis;
use runtime_apis::_InternalImplRuntimeApis;
pub use runtime_apis::{api, RuntimeApi};
mod system;
use sp_version::RuntimeVersion;
Expand Down
17 changes: 17 additions & 0 deletions runtimes/peregrine/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use sp_api::impl_runtime_apis;
use sp_core::OpaqueMetadata;
use sp_inherents::{CheckInherentsResult, InherentData};
use sp_metadata_ir::RuntimeApiMetadataIR;
use sp_runtime::{
traits::{Block as BlockT, TryConvert},
ApplyExtrinsicResult, KeyTypeId,
Expand Down Expand Up @@ -53,6 +54,22 @@ use crate::{
// `impl_runtime_apis` is private.
pub(crate) const RUNTIME_API_VERSION: ApisVec = RUNTIME_API_VERSIONS;

// Workaround for runtime API impls not exposed in metadata if implemented in a
// different file than the runtime's `lib.rs`. Related issue (subxt) -> https://github.com/paritytech/subxt/issues/1873.
pub(crate) trait _InternalImplRuntimeApis {
fn runtime_metadata(&self) -> Vec<RuntimeApiMetadataIR>;
}

impl<T> _InternalImplRuntimeApis for T
where
T: InternalImplRuntimeApis,
{
#[inline(always)]
fn runtime_metadata(&self) -> Vec<RuntimeApiMetadataIR> {
<T as InternalImplRuntimeApis>::runtime_metadata(self)
}
}

impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
Expand Down
2 changes: 2 additions & 0 deletions runtimes/spiritnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sp-block-builder = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-core = { workspace = true }
sp-inherents = { workspace = true }
sp-metadata-ir = { workspace = true }
sp-offchain = { workspace = true }
sp-runtime = { workspace = true }
sp-session = { workspace = true }
Expand Down Expand Up @@ -240,6 +241,7 @@ std = [
"sp-core/std",
"sp-genesis-builder/std",
"sp-inherents/std",
"sp-metadata-ir/std",
"sp-offchain/std",
"sp-runtime/std",
"sp-session/std",
Expand Down
1 change: 1 addition & 0 deletions runtimes/spiritnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod migrations;
pub use migrations::RuntimeMigrations;
mod parachain;
mod runtime_apis;
use runtime_apis::_InternalImplRuntimeApis;
pub use runtime_apis::{api, RuntimeApi};
mod system;
use sp_version::RuntimeVersion;
Expand Down
17 changes: 17 additions & 0 deletions runtimes/spiritnet/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use sp_api::impl_runtime_apis;
use sp_core::OpaqueMetadata;
use sp_inherents::{CheckInherentsResult, InherentData};
use sp_metadata_ir::RuntimeApiMetadataIR;
use sp_runtime::{
traits::{Block as BlockT, TryConvert},
ApplyExtrinsicResult, KeyTypeId,
Expand Down Expand Up @@ -53,6 +54,22 @@ use crate::{
// `impl_runtime_apis` is private.
pub(crate) const RUNTIME_API_VERSION: ApisVec = RUNTIME_API_VERSIONS;

// Workaround for runtime API impls not exposed in metadata if implemented in a
// different file than the runtime's `lib.rs`. Related issue (subxt) -> https://github.com/paritytech/subxt/issues/1873.
pub(crate) trait _InternalImplRuntimeApis {
fn runtime_metadata(&self) -> Vec<RuntimeApiMetadataIR>;
}

impl<T> _InternalImplRuntimeApis for T
where
T: InternalImplRuntimeApis,
{
#[inline(always)]
fn runtime_metadata(&self) -> Vec<RuntimeApiMetadataIR> {
<T as InternalImplRuntimeApis>::runtime_metadata(self)
}
}

impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
Expand Down

0 comments on commit 4148afb

Please sign in to comment.