Add conditional compilation support for iml_runtime_apis! (#14709)

* Handle `cfg_attr` in `decl_runtime_api`

* Integration tests

* UI tests

* Enable staging api tests on CI

* docs

* Comments and minor style fixes

* Fix doc comments

* Apply suggestions from code review

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

* Apply suggestions from code review

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

* Fix formatting and a compilation error

* Fix a doc comment

* Combine the errors from `ApiImplItem`

* Fix a typo in UI test

* Use attribute span when reporting multiple conditional `api_version` error

* Apply suggestions from code review

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

* Don't use `cfg_attr` for methods. Use simple feature gate instead

* Remove a stale comment

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

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

* Revert "Update primitives/api/proc-macro/src/impl_runtime_apis.rs"

This reverts commit 4da20a79bd762580ebf502e9f807c2d7e5876106.

* Code review feedback

* Style improvements

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: parity-processbot <>
This commit is contained in:
Tsvetomir Dimitrov
2023-08-24 11:06:07 +03:00
committed by GitHub
parent cd464f9cfd
commit dbb48a5ba2
5 changed files with 301 additions and 18 deletions
@@ -50,6 +50,29 @@ decl_runtime_apis! {
#[api_version(4)]
fn glory_one();
}
pub trait ApiWithStagingMethod {
fn stable_one(data: u64);
#[api_version(99)]
fn staging_one();
}
pub trait ApiWithStagingAndVersionedMethods {
fn stable_one(data: u64);
#[api_version(2)]
fn new_one();
#[api_version(99)]
fn staging_one();
}
#[api_version(2)]
pub trait ApiWithStagingAndChangedBase {
fn stable_one(data: u64);
fn new_one();
#[api_version(99)]
fn staging_one();
}
}
impl_runtime_apis! {
@@ -82,6 +105,33 @@ impl_runtime_apis! {
fn new_one() {}
}
#[cfg_attr(feature = "enable-staging-api", api_version(99))]
impl self::ApiWithStagingMethod<Block> for Runtime {
fn stable_one(_: u64) {}
#[cfg(feature = "enable-staging-api")]
fn staging_one() { }
}
#[cfg_attr(feature = "enable-staging-api", api_version(99))]
#[api_version(2)]
impl self::ApiWithStagingAndVersionedMethods<Block> for Runtime {
fn stable_one(_: u64) {}
fn new_one() {}
#[cfg(feature = "enable-staging-api")]
fn staging_one() {}
}
#[cfg_attr(feature = "enable-staging-api", api_version(99))]
impl self::ApiWithStagingAndChangedBase<Block> for Runtime {
fn stable_one(_: u64) {}
fn new_one() {}
#[cfg(feature = "enable-staging-api")]
fn staging_one() {}
}
impl sp_api::Core<Block> for Runtime {
fn version() -> sp_version::RuntimeVersion {
unimplemented!()
@@ -179,12 +229,38 @@ fn check_runtime_api_info() {
// The stable version of the API
assert_eq!(<dyn ApiWithMultipleVersions::<Block>>::VERSION, 2);
assert_eq!(<dyn ApiWithStagingMethod::<Block>>::VERSION, 1);
assert_eq!(<dyn ApiWithStagingAndVersionedMethods::<Block>>::VERSION, 1);
assert_eq!(<dyn ApiWithStagingAndChangedBase::<Block>>::VERSION, 2);
}
fn check_runtime_api_versions_contains<T: RuntimeApiInfo + ?Sized>() {
assert!(RUNTIME_API_VERSIONS.iter().any(|v| v == &(T::ID, T::VERSION)));
}
fn check_staging_runtime_api_versions<T: RuntimeApiInfo + ?Sized>(_staging_ver: u32) {
// Staging APIs should contain staging version if the feature is set...
#[cfg(feature = "enable-staging-api")]
assert!(RUNTIME_API_VERSIONS.iter().any(|v| v == &(T::ID, _staging_ver)));
//... otherwise the base version should be set
#[cfg(not(feature = "enable-staging-api"))]
check_runtime_api_versions_contains::<dyn ApiWithStagingMethod<Block>>();
}
#[allow(unused_assignments)]
fn check_staging_multiver_runtime_api_versions<T: RuntimeApiInfo + ?Sized>(
_staging_ver: u32,
_stable_ver: u32,
) {
// Staging APIs should contain staging version if the feature is set...
#[cfg(feature = "enable-staging-api")]
assert!(RUNTIME_API_VERSIONS.iter().any(|v| v == &(T::ID, _staging_ver)));
//... otherwise the base version should be set
#[cfg(not(feature = "enable-staging-api"))]
assert!(RUNTIME_API_VERSIONS.iter().any(|v| v == &(T::ID, _stable_ver)));
}
#[test]
fn check_runtime_api_versions() {
check_runtime_api_versions_contains::<dyn Api<Block>>();
@@ -192,6 +268,13 @@ fn check_runtime_api_versions() {
assert!(RUNTIME_API_VERSIONS
.iter()
.any(|v| v == &(<dyn ApiWithMultipleVersions<Block>>::ID, 3)));
check_staging_runtime_api_versions::<dyn ApiWithStagingMethod<Block>>(99);
check_staging_multiver_runtime_api_versions::<dyn ApiWithStagingAndVersionedMethods<Block>>(
99, 2,
);
check_staging_runtime_api_versions::<dyn ApiWithStagingAndChangedBase<Block>>(99);
check_runtime_api_versions_contains::<dyn sp_api::Core<Block>>();
}