Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

[WIP] Allow metadata to be filtered to contain only the pallet information asked for #12369

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ impl_runtime_apis! {
}

impl sp_api::Metadata<Block> for Runtime {
fn metadata_for_pallets(pallets_to_keep: Vec<Vec<u8>>) -> OpaqueMetadata {
OpaqueMetadata::new(Runtime::metadata_for_pallets(pallets_to_keep).into())
}
fn metadata() -> OpaqueMetadata {
OpaqueMetadata::new(Runtime::metadata().into())
}
Expand Down
3 changes: 3 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,9 @@ impl_runtime_apis! {
}

impl sp_api::Metadata<Block> for Runtime {
fn metadata_for_pallets(pallets_to_keep: Vec<Vec<u8>>) -> OpaqueMetadata {
OpaqueMetadata::new(Runtime::metadata_for_pallets(pallets_to_keep).into())
}
fn metadata() -> OpaqueMetadata {
OpaqueMetadata::new(Runtime::metadata().into())
}
Expand Down
2 changes: 1 addition & 1 deletion client/rpc-api/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub trait StateApi<Hash> {

/// Returns the runtime metadata as an opaque blob.
#[method(name = "state_getMetadata", blocking)]
fn metadata(&self, hash: Option<Hash>) -> RpcResult<Bytes>;
fn metadata(&self, pallets: Option<Vec<String>>, hash: Option<Hash>) -> RpcResult<Bytes>;

/// Get the runtime version.
#[method(name = "state_getRuntimeVersion", aliases = ["chain_getRuntimeVersion"], blocking)]
Expand Down
8 changes: 5 additions & 3 deletions client/rpc/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ where
) -> Result<Option<u64>, Error>;

/// Returns the runtime metadata as an opaque blob.
fn metadata(&self, block: Option<Block::Hash>) -> Result<Bytes, Error>;
///
/// If a list of pallet names is provided, only metadata for those pallets will be returned.
fn metadata(&self, pallets: Option<Vec<String>>, block: Option<Block::Hash>) -> Result<Bytes, Error>;

/// Get the runtime version.
fn runtime_version(&self, block: Option<Block::Hash>) -> Result<RuntimeVersion, Error>;
Expand Down Expand Up @@ -267,8 +269,8 @@ where
self.backend.storage_size(block, key).map_err(Into::into)
}

fn metadata(&self, block: Option<Block::Hash>) -> RpcResult<Bytes> {
self.backend.metadata(block).map_err(Into::into)
fn metadata(&self, pallets: Option<Vec<String>>, block: Option<Block::Hash>) -> RpcResult<Bytes> {
self.backend.metadata(pallets, block).map_err(Into::into)
}

fn runtime_version(&self, at: Option<Block::Hash>) -> RpcResult<RuntimeVersion> {
Expand Down
16 changes: 12 additions & 4 deletions client/rpc/src/state/state_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,19 @@ where
.map_err(client_err)
}

fn metadata(&self, block: Option<Block::Hash>) -> std::result::Result<Bytes, Error> {
fn metadata(&self, pallets: Option<Vec<String>>, block: Option<Block::Hash>) -> std::result::Result<Bytes, Error> {
self.block_or_best(block).map_err(client_err).and_then(|block| {
self.client
.runtime_api()
.metadata(&BlockId::Hash(block))
let api = self.client.runtime_api();

let metadata = match pallets {
Some(pallet_names) => {
let name_bytes = pallet_names.into_iter().map(|n| n.into_bytes()).collect();
api.metadata_for_pallets(&BlockId::Hash(block), name_bytes)
},
None => api.metadata(&BlockId::Hash(block))
};

metadata
.map(Into::into)
.map_err(|e| Error::Client(Box::new(e)))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,25 @@ pub fn expand_runtime_metadata(

quote! {
impl #runtime {
pub fn metadata_for_pallets(pallets_to_keep: #scrate::sp_std::vec::Vec<#scrate::sp_std::vec::Vec<u8>>) -> #scrate::metadata::RuntimeMetadataPrefixed {
Self::metadata_inner(Some(pallets_to_keep))
}
pub fn metadata() -> #scrate::metadata::RuntimeMetadataPrefixed {
Self::metadata_inner(None)
}
fn metadata_inner(pallets_to_keep: Option<#scrate::sp_std::vec::Vec<#scrate::sp_std::vec::Vec<u8>>>) -> #scrate::metadata::RuntimeMetadataPrefixed {
let mut pallets = #scrate::sp_std::vec![ #(#pallets),* ];

// Filter pallet list to only include those asked for, if provided.
if let Some(pallet_names_to_keep) = pallets_to_keep {
pallets = pallets
.into_iter()
.filter(|p| pallet_names_to_keep.iter().any(|n| n == p.name.as_bytes()))
.collect();
}

#scrate::metadata::RuntimeMetadataLastVersion::new(
#scrate::sp_std::vec![ #(#pallets),* ],
pallets,
#scrate::metadata::ExtrinsicMetadata {
ty: #scrate::scale_info::meta_type::<#extrinsic>(),
version: <#extrinsic as #scrate::sp_runtime::traits::ExtrinsicMetadata>::VERSION,
Expand Down
3 changes: 3 additions & 0 deletions primitives/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,9 @@ decl_runtime_apis! {

/// The `Metadata` api trait that returns metadata for the runtime.
pub trait Metadata {
/// Returns the metadata of a runtime filtered to contain only
/// details about the list of pallets provided.
fn metadata_for_pallets(pallets_to_keep: sp_std::vec::Vec<sp_std::vec::Vec<u8>>) -> OpaqueMetadata;
/// Returns the metadata of a runtime.
fn metadata() -> OpaqueMetadata;
}
Expand Down
6 changes: 6 additions & 0 deletions test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@ cfg_if! {
}

impl sp_api::Metadata<Block> for Runtime {
fn metadata_for_pallets(_pallet_names: Vec<Vec<u8>>) -> OpaqueMetadata {
unimplemented!()
}
fn metadata() -> OpaqueMetadata {
unimplemented!()
}
Expand Down Expand Up @@ -980,6 +983,9 @@ cfg_if! {
}

impl sp_api::Metadata<Block> for Runtime {
fn metadata_for_pallets(pallet_names_to_keep: Vec<Vec<u8>>) -> OpaqueMetadata {
unimplemented!()
}
fn metadata() -> OpaqueMetadata {
unimplemented!()
}
Expand Down