Skip to content

Commit 78a106f

Browse files
lexnvjsdw
andauthored
Utilize Metadata V15 (#1041)
* Update frame-metadata to the latest branch Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Add outer enum types Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Extend the extrinsic with address,call,sign,extra types Signed-off-by: Alexandru Vasile <[email protected]> * Codegen test Event, Error and Call for outer enums Signed-off-by: Alexandru Vasile <[email protected]> * Revert "Codegen test Event, Error and Call for outer enums" This reverts commit db542dc. * Update frame-metadata from the latest release Signed-off-by: Alexandru Vasile <[email protected]> * Update scale-info Signed-off-by: Alexandru Vasile <[email protected]> * codegen/error: Support v15 message Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Convert v14 to v15 Signed-off-by: Alexandru Vasile <[email protected]> * metadata/retain: Adjust to extrinsic type for V15 Signed-off-by: Alexandru Vasile <[email protected]> * metadata/validation: Adjust hashing for extrinsic types V15 Signed-off-by: Alexandru Vasile <[email protected]> * scripts: Fetch V15 and output codegen for full_client only Signed-off-by: Alexandru Vasile <[email protected]> * subxt/blocks: Use extrinsic types directly Signed-off-by: Alexandru Vasile <[email protected]> * testing: Fetch V15 for build script Signed-off-by: Alexandru Vasile <[email protected]> * artifacts: Generate from latest polkadot version Signed-off-by: Alexandru Vasile <[email protected]> * codegen: Fetch legacy with old API for v14 only Signed-off-by: Alexandru Vasile <[email protected]> * rpc: Fetch metadata versions Signed-off-by: Alexandru Vasile <[email protected]> * client: Fetch latest unstable then V15 then V14 Signed-off-by: Alexandru Vasile <[email protected]> * testing: Adjust testing API to latest interface Signed-off-by: Alexandru Vasile <[email protected]> * Adjust clippy Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Generate the `RuntimeError` type for V14 Signed-off-by: Alexandru Vasile <[email protected]> * Remove testing files Signed-off-by: Alexandru Vasile <[email protected]> * testing/staking: Remove controller account from bond Signed-off-by: Alexandru Vasile <[email protected]> * metadata/validation: Use specific variants for hashing RuntimeCall Signed-off-by: Alexandru Vasile <[email protected]> * XXX: Custom Substrate binary: must revert with next release Signed-off-by: Alexandru Vasile <[email protected]> * XXX: To revert: CI use hardcoded substrate Signed-off-by: Alexandru Vasile <[email protected]> * codegen: Use v15 outer enum types Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Retain outer enum types Signed-off-by: Alexandru Vasile <[email protected]> * codegen: Use outer enum types instead of generating them Signed-off-by: Alexandru Vasile <[email protected]> * Update artifacts Signed-off-by: Alexandru Vasile <[email protected]> * Revert "XXX: Custom Substrate binary: must revert with next release" This reverts commit e970529. Revert "XXX: To revert: CI use hardcoded substrate" This reverts commit b18a5a0. * testing: Include env for dummy wat contracts Signed-off-by: Alexandru Vasile <[email protected]> * Adjsut clippy Signed-off-by: Alexandru Vasile <[email protected]> * ci: Use new link for fetching latest substrate binary Signed-off-by: Alexandru Vasile <[email protected]> * tests: Include dummy RuntimeEvent into test metadata Signed-off-by: Alexandru Vasile <[email protected]> * ci: Bump light-client timeout tests to 25min Signed-off-by: Alexandru Vasile <[email protected]> * metadata/validation: Use specific pallets as provided Signed-off-by: Alexandru Vasile <[email protected]> * testing: Rename metadata constant Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Use call_ty instead of signature_ty Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Rename retaining variant function Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Use Option<&[&str]> Signed-off-by: Alexandru Vasile <[email protected]> * online_client: Fetch V15 metadata explicitely Signed-off-by: Alexandru Vasile <[email protected]> * metadata/validation: Include the hash of the outer enum types Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Fix sign typo Signed-off-by: Alexandru Vasile <[email protected]> * artifacts: Update the artifacts Signed-off-by: Alexandru Vasile <[email protected]> * subxt: Remove RootError RootEvent and RootExtrinsic traits Signed-off-by: Alexandru Vasile <[email protected]> * Update polkadot.rs Signed-off-by: Alexandru Vasile <[email protected]> * metadata/tests: Ensure outer enum variants are retained Signed-off-by: Alexandru Vasile <[email protected]> * scripts: Include multiple pallets for our decoding purposes Signed-off-by: Alexandru Vasile <[email protected]> * subxt: Apply clippy Signed-off-by: Alexandru Vasile <[email protected]> * artifacts: Update small metadata Signed-off-by: Alexandru Vasile <[email protected]> * error: Keep raw bytes for the ModuleError representation Signed-off-by: Alexandru Vasile <[email protected]> * error: Modify docs to not include links Signed-off-by: Alexandru Vasile <[email protected]> * subxt/tests: Propagate `RuntimeCall` to outer enums Signed-off-by: Alexandru Vasile <[email protected]> * subxt: Provide proper byte slice for decoding Signed-off-by: Alexandru Vasile <[email protected]> * Update artifacts Signed-off-by: Alexandru Vasile <[email protected]> * cli/tests: Adjust expected pallets message Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Test conversion from v14 to v15 Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Fix typo Signed-off-by: Alexandru Vasile <[email protected]> * Update subxt/src/blocks/extrinsic_types.rs Co-authored-by: James Wilson <[email protected]> * metadata: Simplify type path for RuntimeError Signed-off-by: Alexandru Vasile <[email protected]> * metadata/validation: Use visited ids per outer enum Signed-off-by: Alexandru Vasile <[email protected]> * error: Remove RawModuleError Signed-off-by: Alexandru Vasile <[email protected]> * Fix new clippy error from updated rust version Signed-off-by: Alexandru Vasile <[email protected]> --------- Signed-off-by: Alexandru Vasile <[email protected]> Co-authored-by: James Wilson <[email protected]>
1 parent 4fb051f commit 78a106f

File tree

33 files changed

+3251
-9024
lines changed

33 files changed

+3251
-9024
lines changed

.github/workflows/nightly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
env:
1010
CARGO_TERM_COLOR: always
1111
# Use latest substrate for nightly runs:
12-
SUBSTRATE_URL: https://releases.parity.io/substrate/x86_64-debian:stretch/latest/substrate/substrate
12+
SUBSTRATE_URL: https://releases.parity.io/substrate/x86_64-debian:bullseye/latest/substrate/substrate
1313

1414
jobs:
1515
tests:

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ concurrency:
1919
env:
2020
CARGO_TERM_COLOR: always
2121
# TODO: Currently pointing at latest substrate; is there a suitable binary we can pin to here?
22-
SUBSTRATE_URL: https://releases.parity.io/substrate/x86_64-debian:stretch/latest/substrate/substrate
22+
SUBSTRATE_URL: https://releases.parity.io/substrate/x86_64-debian:bullseye/latest/substrate/substrate
2323

2424
jobs:
2525
build:

Cargo.lock

Lines changed: 23 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ console_error_panic_hook = "0.1.7"
4545
darling = "0.20.0"
4646
derivative = "2.2.0"
4747
either = "1.8.1"
48-
frame-metadata = { version = "15.1.0", features = ["v14", "v15-unstable", "std"] }
48+
frame-metadata = { version = "16.0.0", default-features = false, features = ["current", "std"] }
4949
futures = { version = "0.3.27", default-features = false, features = ["std"] }
5050
getrandom = { version = "0.2", default-features = false }
5151
hex = "0.4.3"
@@ -58,7 +58,7 @@ proc-macro-error = "1.0.4"
5858
proc-macro2 = "1.0.63"
5959
quote = "1.0.29"
6060
regex = "1.9.1"
61-
scale-info = "2.8.0"
61+
scale-info = "2.9.0"
6262
scale-value = "0.10.0"
6363
scale-bits = "0.3"
6464
scale-decode = "0.7.0"
-105 KB
Binary file not shown.
-64.6 KB
Binary file not shown.
1.36 KB
Binary file not shown.

cli/src/commands/explore/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub mod tests {
167167
async fn test_commands() {
168168
// show pallets:
169169
let output = simulate_run("").await;
170-
assert_eq!(output.unwrap(), "Usage:\n subxt explore <PALLET>\n explore a specific pallet\n\nAvailable <PALLET> values are:\n Balances\n Multisig\n Staking\n System\n");
170+
assert_eq!(output.unwrap(), "Usage:\n subxt explore <PALLET>\n explore a specific pallet\n\nAvailable <PALLET> values are:\n Balances\n Multisig\n ParaInherent\n Staking\n System\n Timestamp\n");
171171
// if incorrect pallet, error:
172172
let output = simulate_run("abc123").await;
173173
assert!(output.is_err());

codegen/src/api/mod.rs

Lines changed: 13 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,12 @@ impl RuntimeGenerator {
289289
) -> Result<TokenStream2, CodegenError> {
290290
let item_mod_attrs = item_mod.attrs.clone();
291291
let item_mod_ir = ir::ItemMod::try_from(item_mod)?;
292-
let default_derives = derives.default_derives();
293292

294293
let type_gen = TypeGenerator::new(
295294
self.metadata.types(),
296295
"runtime_types",
297296
type_substitutes,
298-
derives.clone(),
297+
derives,
299298
crate_path.clone(),
300299
should_gen_docs,
301300
);
@@ -382,118 +381,6 @@ impl RuntimeGenerator {
382381
})
383382
.collect::<Result<Vec<_>, CodegenError>>()?;
384383

385-
let outer_event_variants = self.metadata.pallets().filter_map(|p| {
386-
let variant_name = format_ident!("{}", p.name());
387-
let mod_name = format_ident!("{}", p.name().to_string().to_snake_case());
388-
let index = proc_macro2::Literal::u8_unsuffixed(p.index());
389-
390-
p.event_ty_id().map(|_| {
391-
quote! {
392-
#[codec(index = #index)]
393-
#variant_name(#mod_name::Event),
394-
}
395-
})
396-
});
397-
398-
let outer_event = quote! {
399-
#default_derives
400-
pub enum Event {
401-
#( #outer_event_variants )*
402-
}
403-
};
404-
405-
let outer_extrinsic_variants = self.metadata.pallets().filter_map(|p| {
406-
let variant_name = format_ident!("{}", p.name());
407-
let mod_name = format_ident!("{}", p.name().to_string().to_snake_case());
408-
let index = proc_macro2::Literal::u8_unsuffixed(p.index());
409-
410-
p.call_ty_id().map(|_| {
411-
quote! {
412-
#[codec(index = #index)]
413-
#variant_name(#mod_name::Call),
414-
}
415-
})
416-
});
417-
418-
let outer_extrinsic = quote! {
419-
#default_derives
420-
pub enum Call {
421-
#( #outer_extrinsic_variants )*
422-
}
423-
};
424-
425-
let root_event_if_arms = self.metadata.pallets().filter_map(|p| {
426-
let variant_name_str = &p.name();
427-
let variant_name = format_ident!("{}", variant_name_str);
428-
let mod_name = format_ident!("{}", variant_name_str.to_string().to_snake_case());
429-
430-
p.event_ty_id().map(|_| {
431-
// An 'if' arm for the RootEvent impl to match this variant name:
432-
quote! {
433-
if pallet_name == #variant_name_str {
434-
return Ok(Event::#variant_name(#mod_name::Event::decode_with_metadata(
435-
&mut &*pallet_bytes,
436-
pallet_ty,
437-
metadata
438-
)?));
439-
}
440-
}
441-
})
442-
});
443-
444-
let root_extrinsic_if_arms = self.metadata.pallets().filter_map(|p| {
445-
let variant_name_str = p.name();
446-
let variant_name = format_ident!("{}", variant_name_str);
447-
let mod_name = format_ident!("{}", variant_name_str.to_string().to_snake_case());
448-
p.call_ty_id().map(|_| {
449-
// An 'if' arm for the RootExtrinsic impl to match this variant name:
450-
quote! {
451-
if pallet_name == #variant_name_str {
452-
return Ok(Call::#variant_name(#mod_name::Call::decode_with_metadata(
453-
&mut &*pallet_bytes,
454-
pallet_ty,
455-
metadata
456-
)?));
457-
}
458-
}
459-
})
460-
});
461-
462-
let outer_error_variants = self.metadata.pallets().filter_map(|p| {
463-
let variant_name = format_ident!("{}", p.name());
464-
let mod_name = format_ident!("{}", p.name().to_string().to_snake_case());
465-
let index = proc_macro2::Literal::u8_unsuffixed(p.index());
466-
467-
p.error_ty_id().map(|_| {
468-
quote! {
469-
#[codec(index = #index)]
470-
#variant_name(#mod_name::Error),
471-
}
472-
})
473-
});
474-
475-
let outer_error = quote! {
476-
#default_derives
477-
pub enum Error {
478-
#( #outer_error_variants )*
479-
}
480-
};
481-
482-
let root_error_if_arms = self.metadata.pallets().filter_map(|p| {
483-
let variant_name_str = &p.name();
484-
let variant_name = format_ident!("{}", variant_name_str);
485-
let mod_name = format_ident!("{}", variant_name_str.to_string().to_snake_case());
486-
487-
p.error_ty_id().map(|type_id| {
488-
quote! {
489-
if pallet_name == #variant_name_str {
490-
let variant_error = #mod_name::Error::decode_with_metadata(cursor, #type_id, metadata)?;
491-
return Ok(Error::#variant_name(variant_error));
492-
}
493-
}
494-
})
495-
});
496-
497384
let mod_ident = &item_mod_ir.ident;
498385
let pallets_with_constants: Vec<_> = pallets_with_mod_names
499386
.iter()
@@ -526,6 +413,12 @@ impl RuntimeGenerator {
526413
should_gen_docs,
527414
)?;
528415

416+
// Fetch the paths of the outer enums.
417+
// Substrate exposes those under `kitchensink_runtime`, while Polkadot under `polkadot_runtime`.
418+
let call_path = type_gen.resolve_type_path(self.metadata.outer_enums().call_enum_ty());
419+
let event_path = type_gen.resolve_type_path(self.metadata.outer_enums().event_enum_ty());
420+
let error_path = type_gen.resolve_type_path(self.metadata.outer_enums().error_enum_ty());
421+
529422
Ok(quote! {
530423
#( #item_mod_attrs )*
531424
#[allow(dead_code, unused_imports, non_camel_case_types)]
@@ -551,36 +444,14 @@ impl RuntimeGenerator {
551444
/// The error type returned when there is a runtime issue.
552445
pub type DispatchError = #types_mod_ident::sp_runtime::DispatchError;
553446

554-
#outer_event
447+
/// The outer event enum.
448+
pub type Event = #event_path;
555449

556-
impl #crate_path::events::RootEvent for Event {
557-
fn root_event(pallet_bytes: &[u8], pallet_name: &str, pallet_ty: u32, metadata: &#crate_path::Metadata) -> Result<Self, #crate_path::Error> {
558-
use #crate_path::metadata::DecodeWithMetadata;
559-
#( #root_event_if_arms )*
560-
Err(#crate_path::ext::scale_decode::Error::custom(format!("Pallet name '{}' not found in root Event enum", pallet_name)).into())
561-
}
562-
}
563-
564-
#outer_extrinsic
450+
/// The outer extrinsic enum.
451+
pub type Call = #call_path;
565452

566-
impl #crate_path::blocks::RootExtrinsic for Call {
567-
fn root_extrinsic(pallet_bytes: &[u8], pallet_name: &str, pallet_ty: u32, metadata: &#crate_path::Metadata) -> Result<Self, #crate_path::Error> {
568-
use #crate_path::metadata::DecodeWithMetadata;
569-
#( #root_extrinsic_if_arms )*
570-
Err(#crate_path::ext::scale_decode::Error::custom(format!("Pallet name '{}' not found in root Call enum", pallet_name)).into())
571-
}
572-
}
573-
574-
#outer_error
575-
576-
impl #crate_path::error::RootError for Error {
577-
fn root_error(pallet_bytes: &[u8], pallet_name: &str, metadata: &#crate_path::Metadata) -> Result<Self, #crate_path::Error> {
578-
use #crate_path::metadata::DecodeWithMetadata;
579-
let cursor = &mut &pallet_bytes[..];
580-
#( #root_error_if_arms )*
581-
Err(#crate_path::ext::scale_decode::Error::custom(format!("Pallet name '{}' not found in root Error enum", pallet_name)).into())
582-
}
583-
}
453+
/// The outer error enum representing the DispatchError's Module variant.
454+
pub type Error = #error_path;
584455

585456
pub fn constants() -> ConstantsApi {
586457
ConstantsApi

codegen/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum CodegenError {
1919
#[error("Failed IO for {0}, make sure that you are providing the correct file path for metadata: {1}")]
2020
Io(String, std::io::Error),
2121
/// Cannot decode the metadata bytes.
22-
#[error("Could not decode metadata, only V14 metadata is supported: {0}")]
22+
#[error("Could not decode metadata, only V14 and V15 metadata are supported: {0}")]
2323
Decode(#[from] codec::Error),
2424
/// Out of line modules are not supported.
2525
#[error("Out-of-line subxt modules are not supported, make sure you are providing a body to your module: pub mod polkadot {{ ... }}")]

0 commit comments

Comments
 (0)