Support V16 metadata and refactor metadata code (#1967)

* WIP integrate unstable v16 metadata into Subxt

* first pass moving retain to the CLI tool

* Remove otuer enum variant stripping and move now simpler strip_metadata to new crate. test it

* tidyup to use stripmetadata package etc

* Fix / comment out tests

* fmt

* clippy

* Fix wasm example

* wasm-example fix

* wasm-example fix

* Maske sure to move IDs around after types.retain()

* fmt

* Tweak comment

* Find dispatch error separately to avoid issues during mapping

* Expose associated type information in pallet metadata

* Hopefully fix flaky archive RPC

* remove unwanted temp file

* Address nits

* Add back commented-otu tests and address review comments

* use either, and simplify for_each
This commit is contained in:
James Wilson
2025-03-28 15:35:55 +00:00
committed by GitHub
parent 06396f8b1a
commit 72ac18491c
32 changed files with 2355 additions and 2274 deletions
+32 -5
View File
@@ -17,11 +17,32 @@ mod storage;
mod utils;
use crate::utils::MetadataTestRunner;
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
use subxt_utils_stripmetadata::StripMetadata;
// Each of these tests leads to some rust code being compiled and
// executed to test that compilation is successful (or errors in the
// way that we'd expect).
fn strip_metadata<Pallets, Apis>(
metadata: &mut RuntimeMetadataPrefixed,
pallets: Pallets,
apis: Apis,
) where
Pallets: Fn(&str) -> bool,
Apis: Fn(&str) -> bool,
{
match &mut metadata.1 {
RuntimeMetadata::V14(m) => m.strip_metadata(pallets, apis),
RuntimeMetadata::V15(m) => m.strip_metadata(pallets, apis),
RuntimeMetadata::V16(m) => m.strip_metadata(pallets, apis),
m => panic!(
"Metadata should be V14, V15 or V16, but is V{}",
m.version()
),
}
}
#[test]
fn ui_tests() {
let mut m = MetadataTestRunner::default();
@@ -62,7 +83,7 @@ fn ui_tests() {
// Test retaining only specific pallets and ensure that works.
for pallet in ["Babe", "Claims", "Grandpa", "Balances"] {
let mut metadata = MetadataTestRunner::load_metadata();
metadata.retain(|p| p == pallet, |_| true);
strip_metadata(&mut metadata, |p| p == pallet, |_| true);
t.pass(
m.new_test_case()
@@ -74,7 +95,7 @@ fn ui_tests() {
// Test retaining only specific runtime APIs to ensure that works.
for runtime_api in ["Core", "Metadata"] {
let mut metadata = MetadataTestRunner::load_metadata();
metadata.retain(|_| true, |r| r == runtime_api);
strip_metadata(&mut metadata, |_| true, |r| r == runtime_api);
t.pass(
m.new_test_case()
@@ -87,7 +108,8 @@ fn ui_tests() {
// client state is full:
{
let mut metadata = MetadataTestRunner::load_metadata();
metadata.retain(
strip_metadata(
&mut metadata,
|p| ["Babe", "Claims"].contains(&p),
|r| ["Core", "Metadata"].contains(&r),
);
@@ -104,12 +126,17 @@ fn ui_tests() {
// _not_ compare valid against client with differently stripped metadata.
{
let mut codegen_metadata = MetadataTestRunner::load_metadata();
codegen_metadata.retain(
strip_metadata(
&mut codegen_metadata,
|p| ["Babe", "Claims"].contains(&p),
|r| ["Core", "Metadata"].contains(&r),
);
let mut validation_metadata = MetadataTestRunner::load_metadata();
validation_metadata.retain(|p| p != "Claims", |r| r != "Metadata");
strip_metadata(
&mut validation_metadata,
|p| p != "Claims",
|r| r != "Metadata",
);
t.pass(
m.new_test_case()
@@ -3,8 +3,8 @@
// see LICENSE for license details.
use codec::{Decode, Encode};
use frame_metadata::RuntimeMetadataPrefixed;
use std::io::Read;
use subxt_metadata::Metadata;
static TEST_DIR_PREFIX: &str = "subxt_generated_ui_tests_";
static METADATA_FILE: &str = "../../artifacts/polkadot_metadata_full.scale";
@@ -17,7 +17,7 @@ pub struct MetadataTestRunner {
impl MetadataTestRunner {
/// Loads metadata that we can use in our tests. Panics if
/// there is some issue decoding the metadata.
pub fn load_metadata() -> Metadata {
pub fn load_metadata() -> RuntimeMetadataPrefixed {
let mut file =
std::fs::File::open(METADATA_FILE).expect("Cannot open metadata.scale artifact");
@@ -25,7 +25,7 @@ impl MetadataTestRunner {
file.read_to_end(&mut bytes)
.expect("Failed to read metadata.scale file");
Metadata::decode(&mut &*bytes).expect("Cannot decode metadata bytes")
RuntimeMetadataPrefixed::decode(&mut &*bytes).expect("Cannot decode metadata bytes")
}
/// Create a new test case.
@@ -54,7 +54,7 @@ impl Drop for MetadataTestRunner {
pub struct MetadataTestRunnerCaseBuilder {
index: usize,
name: String,
validation_metadata: Option<Metadata>,
validation_metadata: Option<RuntimeMetadataPrefixed>,
should_be_valid: bool,
}
@@ -76,7 +76,7 @@ impl MetadataTestRunnerCaseBuilder {
/// Set metadata to be validated against the generated code.
/// By default, we'll validate the same metadata used to generate the code.
pub fn validation_metadata(mut self, md: impl Into<Metadata>) -> Self {
pub fn validation_metadata(mut self, md: impl Into<RuntimeMetadataPrefixed>) -> Self {
self.validation_metadata = Some(md.into());
self
}
@@ -100,15 +100,12 @@ impl MetadataTestRunnerCaseBuilder {
///
/// The generated code will be tidied up when the `MetadataTestRunner` that
/// this was handed out from is dropped.
pub fn build<M>(self, macro_metadata: M) -> String
where
M: TryInto<Metadata>,
M::Error: std::fmt::Debug,
{
let macro_metadata = macro_metadata.try_into().expect("can into Metadata");
let validation_metadata = self
.validation_metadata
.unwrap_or_else(|| macro_metadata.clone());
pub fn build(self, macro_metadata: frame_metadata::RuntimeMetadataPrefixed) -> String {
let validation_metadata = self.validation_metadata.unwrap_or_else(|| {
// RuntimeMetadataPrefixed doesn't implement Clone for some reason (we should prob fix that).
// until then, this hack clones it by encoding and then decoding it again from bytes..
clone_via_encode(&macro_metadata)
});
let index = self.index;
let mut tmp_dir = std::env::temp_dir();
@@ -178,3 +175,8 @@ impl MetadataTestRunnerCaseBuilder {
tmp_rust_path
}
}
fn clone_via_encode<T: codec::Encode + codec::Decode>(item: &T) -> T {
let bytes = item.encode();
T::decode(&mut &*bytes).unwrap()
}