-
Notifications
You must be signed in to change notification settings - Fork 721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't expose metadata for Runtime APIs that haven't been implemented #6337
Merged
Merged
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3c2cb70
Filter runtime API metadata by version implemented
jsdw bc0b17a
Add test and fix a coupole of issues
jsdw c695171
fmt
jsdw 7423f35
Merge branch 'master' into jsdw-versioned-runtime-api-metadata
jsdw 03da4bf
Add comment
jsdw 9738eb7
Add prdoc
jsdw 8bac882
Merge branch 'master' into jsdw-versioned-runtime-api-metadata
jsdw f99faec
Delete polkadot/Cargo.lock
bkchr a2defb2
Updateframe-support tests
jsdw 36d8ae0
Merge branch 'jsdw-versioned-runtime-api-metadata' of github.com:pari…
jsdw 9cbb5c5
u32 api_version to align with VERSION, and use VERSION instead of add…
jsdw 5d75c2c
Merge branch 'master' into jsdw-versioned-runtime-api-metadata
jsdw 0c48f03
Fix prdoc
jsdw f0073db
Merge branch 'jsdw-versioned-runtime-api-metadata' of github.com:pari…
jsdw b9e7818
Update from jsdw running command 'fmt'
actions-user 7719861
Merge branch 'master' into jsdw-versioned-runtime-api-metadata
jsdw 65bb934
runtime_metadata(impl_version) to filter there
jsdw cabb755
Merge branch 'jsdw-versioned-runtime-api-metadata' of github.com:pari…
jsdw 048103a
Merge branch 'master' into jsdw-versioned-runtime-api-metadata
jsdw e710771
Undo frame-support tests change
jsdw 1275f0d
Merge branch 'jsdw-versioned-runtime-api-metadata' of github.com:pari…
jsdw 5cf3857
Update from jsdw running command 'fmt'
actions-user 78c1716
Merge branch 'master' into jsdw-versioned-runtime-api-metadata
bkchr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,9 @@ use proc_macro2::{Span, TokenStream}; | |
use proc_macro_crate::{crate_name, FoundCrate}; | ||
use quote::{format_ident, quote}; | ||
use syn::{ | ||
parse_quote, punctuated::Punctuated, spanned::Spanned, token::And, Attribute, Error, Expr, | ||
ExprLit, FnArg, GenericArgument, Ident, ItemImpl, Lit, Meta, MetaNameValue, Pat, Path, | ||
PathArguments, Result, ReturnType, Signature, Token, Type, TypePath, | ||
parse_quote, parenthesized, punctuated::Punctuated, spanned::Spanned, token::And, Attribute, Error, Expr, | ||
ExprLit, FnArg, GenericArgument, Ident, ItemImpl, Lit, LitStr, LitInt, Meta, MetaNameValue, | ||
Pat, Path, PathArguments, Result, ReturnType, Signature, Token, Type, TypePath, | ||
}; | ||
|
||
/// Generates the access to the `sc_client` crate. | ||
|
@@ -334,6 +334,88 @@ pub fn get_deprecation(crate_: &TokenStream, attrs: &[syn::Attribute]) -> Result | |
.unwrap_or_else(|| Ok(quote! {#crate_::metadata_ir::DeprecationStatusIR::NotDeprecated})) | ||
} | ||
|
||
/// Represents an API version. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't change any of this code; just moved it to the utils module so that I could use it in runtime_metadata too. |
||
pub struct ApiVersion { | ||
/// Corresponds to `#[api_version(X)]` attribute. | ||
pub custom: Option<u64>, | ||
/// Corresponds to `#[cfg_attr(feature = "enable-staging-api", api_version(99))]` | ||
/// attribute. `String` is the feature name, `u64` the staging api version. | ||
pub feature_gated: Option<(String, u64)>, | ||
} | ||
|
||
/// Extracts the value of `API_VERSION_ATTRIBUTE` and handles errors. | ||
/// Returns: | ||
/// - Err if the version is malformed | ||
/// - `ApiVersion` on success. If a version is set or not is determined by the fields of `ApiVersion` | ||
pub fn extract_api_version(attrs: &[Attribute], span: Span) -> Result<ApiVersion> { | ||
// First fetch all `API_VERSION_ATTRIBUTE` values (should be only one) | ||
let api_ver = attrs | ||
.iter() | ||
.filter(|a| a.path().is_ident(API_VERSION_ATTRIBUTE)) | ||
.collect::<Vec<_>>(); | ||
|
||
if api_ver.len() > 1 { | ||
return Err(Error::new( | ||
span, | ||
format!( | ||
"Found multiple #[{}] attributes for an API implementation. \ | ||
Each runtime API can have only one version.", | ||
API_VERSION_ATTRIBUTE | ||
), | ||
)); | ||
} | ||
|
||
// Parse the runtime version if there exists one. | ||
Ok(ApiVersion { | ||
custom: api_ver.first().map(|v| parse_runtime_api_version(v)).transpose()?, | ||
feature_gated: extract_cfg_api_version(attrs, span)?, | ||
}) | ||
} | ||
|
||
/// Parse feature flagged api_version. | ||
/// E.g. `#[cfg_attr(feature = "enable-staging-api", api_version(99))]` | ||
fn extract_cfg_api_version(attrs: &[Attribute], span: Span) -> Result<Option<(String, u64)>> { | ||
let cfg_attrs = attrs.iter().filter(|a| a.path().is_ident("cfg_attr")).collect::<Vec<_>>(); | ||
|
||
let mut cfg_api_version_attr = Vec::new(); | ||
for cfg_attr in cfg_attrs { | ||
let mut feature_name = None; | ||
let mut api_version = None; | ||
cfg_attr.parse_nested_meta(|m| { | ||
if m.path.is_ident("feature") { | ||
let a = m.value()?; | ||
let b: LitStr = a.parse()?; | ||
feature_name = Some(b.value()); | ||
} else if m.path.is_ident(API_VERSION_ATTRIBUTE) { | ||
let content; | ||
parenthesized!(content in m.input); | ||
let ver: LitInt = content.parse()?; | ||
api_version = Some(ver.base10_parse::<u64>()?); | ||
} | ||
Ok(()) | ||
})?; | ||
|
||
// If there is a cfg attribute containing api_version - save if for processing | ||
if let (Some(feature_name), Some(api_version)) = (feature_name, api_version) { | ||
cfg_api_version_attr.push((feature_name, api_version, cfg_attr.span())); | ||
} | ||
} | ||
|
||
if cfg_api_version_attr.len() > 1 { | ||
let mut err = Error::new(span, format!("Found multiple feature gated api versions (cfg attribute with nested `{}` attribute). This is not supported.", API_VERSION_ATTRIBUTE)); | ||
for (_, _, attr_span) in cfg_api_version_attr { | ||
err.combine(Error::new(attr_span, format!("`{}` found here", API_VERSION_ATTRIBUTE))); | ||
} | ||
|
||
return Err(err); | ||
} | ||
|
||
Ok(cfg_api_version_attr | ||
.into_iter() | ||
.next() | ||
.map(|(feature, name, _)| (feature, name))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use assert_matches::assert_matches; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Committed by accident?