Skip to content

Commit

Permalink
Codegen test Event, Error and Call for outer enums
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Jun 29, 2023
1 parent 326af9b commit db542dc
Show file tree
Hide file tree
Showing 19 changed files with 49,853 additions and 38,416 deletions.
Binary file modified artifacts/polkadot_metadata_full.scale
Binary file not shown.
Binary file modified artifacts/polkadot_metadata_small.scale
Binary file not shown.
Binary file modified artifacts/polkadot_metadata_tiny.scale
Binary file not shown.
26 changes: 19 additions & 7 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ pub fn generate_runtime_api_from_url(
runtime_types_only: bool,
) -> Result<TokenStream2, CodegenError> {
// Fetch latest unstable version, if that fails fall back to the latest stable.
let bytes = match fetch_metadata_bytes_blocking(url, MetadataVersion::Unstable) {
Ok(bytes) => bytes,
Err(_) => fetch_metadata_bytes_blocking(url, MetadataVersion::Latest)?,
};
// let bytes = match fetch_metadata_bytes_blocking(url, MetadataVersion::Unstable) {
// Ok(bytes) => bytes,
// Err(_) => fetch_metadata_bytes_blocking(url, MetadataVersion::Latest)?,
// };

let bytes = fetch_metadata_bytes_blocking(url, MetadataVersion::Latest)?;

generate_runtime_api_from_bytes(
item_mod,
Expand Down Expand Up @@ -548,10 +550,20 @@ impl RuntimeGenerator {
// Runtime APIs in the metadata by name.
pub static RUNTIME_APIS: [&str; #runtime_api_names_len] = [ #(#runtime_api_names,)* ];

/// Outer most event.
pub type Event = #types_mod_ident::kitchensink_runtime::RuntimeEvent;

/// Outer most event.
pub type Error = #types_mod_ident::kitchensink_runtime::RuntimeError;

/// Outer most event.
// #outer_extrinsic
pub type Call = #types_mod_ident::kitchensink_runtime::RuntimeCall;

/// The error type returned when there is a runtime issue.
pub type DispatchError = #types_mod_ident::sp_runtime::DispatchError;

#outer_event
// #outer_event

impl #crate_path::events::RootEvent for Event {
fn root_event(pallet_bytes: &[u8], pallet_name: &str, pallet_ty: u32, metadata: &#crate_path::Metadata) -> Result<Self, #crate_path::Error> {
Expand All @@ -561,7 +573,7 @@ impl RuntimeGenerator {
}
}

#outer_extrinsic
// #outer_extrinsic

impl #crate_path::blocks::RootExtrinsic for Call {
fn root_extrinsic(pallet_bytes: &[u8], pallet_name: &str, pallet_ty: u32, metadata: &#crate_path::Metadata) -> Result<Self, #crate_path::Error> {
Expand All @@ -571,7 +583,7 @@ impl RuntimeGenerator {
}
}

#outer_error
// #outer_error

impl #crate_path::error::RootError for Error {
fn root_error(pallet_bytes: &[u8], pallet_name: &str, metadata: &#crate_path::Metadata) -> Result<Self, #crate_path::Error> {
Expand Down
5 changes: 3 additions & 2 deletions metadata/src/from_into/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

mod v14;
mod v15;

/// An error emitted if something goes wrong converting [`frame_metadata`]
Expand Down Expand Up @@ -75,7 +74,9 @@ impl TryFrom<frame_metadata::RuntimeMetadataPrefixed> for crate::Metadata {
frame_metadata::RuntimeMetadata::V13(_) => {
Err(TryFromError::UnsupportedMetadataVersion(13))
}
frame_metadata::RuntimeMetadata::V14(m) => m.try_into(),
frame_metadata::RuntimeMetadata::V14(_) => {
Err(TryFromError::UnsupportedMetadataVersion(14))
}
frame_metadata::RuntimeMetadata::V15(m) => m.try_into(),
}
}
Expand Down
194 changes: 0 additions & 194 deletions metadata/src/from_into/v14.rs

This file was deleted.

25 changes: 23 additions & 2 deletions metadata/src/from_into/v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use std::collections::HashMap;

// Converting from V15 metadata into our Subxt repr.
mod from_v15 {
use crate::OuterEnumsMetadata;

use super::*;

impl TryFrom<v15::RuntimeMetadataV15> for Metadata {
Expand Down Expand Up @@ -86,6 +88,11 @@ mod from_v15 {
pallets_by_index,
extrinsic: from_extrinsic_metadata(m.extrinsic),
runtime_ty: m.ty.id,
outer_enums: OuterEnumsMetadata {
call_enum_ty: m.outer_enums.call_enum_ty.id,
event_enum_ty: m.outer_enums.event_enum_ty.id,
error_enum_ty: m.outer_enums.error_enum_ty.id,
},
dispatch_error_ty,
apis: apis.collect(),
})
Expand All @@ -104,13 +111,16 @@ mod from_v15 {

fn from_extrinsic_metadata(value: v15::ExtrinsicMetadata<PortableForm>) -> ExtrinsicMetadata {
ExtrinsicMetadata {
ty: value.ty.id,
version: value.version,
signed_extensions: value
.signed_extensions
.into_iter()
.map(from_signed_extension_metadata)
.collect(),
address_ty: value.address_ty.id,
call_ty: value.call_ty.id,
signature_ty: value.signature_ty.id,
extra_ty: value.extra_ty.id,
}
}

Expand Down Expand Up @@ -268,6 +278,14 @@ mod into_v15 {
.into_iter()
.map(from_runtime_api_metadata)
.collect(),
outer_enums: v15::OuterEnums {
call_enum_ty: m.outer_enums.call_enum_ty.into(),
event_enum_ty: m.outer_enums.event_enum_ty.into(),
error_enum_ty: m.outer_enums.error_enum_ty.into(),
},
custom: v15::CustomMetadata {
map: Default::default(),
},
}
}
}
Expand Down Expand Up @@ -313,13 +331,16 @@ mod into_v15 {

fn from_extrinsic_metadata(e: ExtrinsicMetadata) -> v15::ExtrinsicMetadata<PortableForm> {
v15::ExtrinsicMetadata {
ty: e.ty.into(),
version: e.version,
signed_extensions: e
.signed_extensions
.into_iter()
.map(from_signed_extension_metadata)
.collect(),
address_ty: e.address_ty.into(),
call_ty: e.call_ty.into(),
signature_ty: e.signature_ty.into(),
extra_ty: e.extra_ty.into(),
}
}

Expand Down
1 change: 0 additions & 1 deletion metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@ impl codec::Decode for Metadata {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
let metadata = frame_metadata::RuntimeMetadataPrefixed::decode(input)?;
let metadata = match metadata.1 {
frame_metadata::RuntimeMetadata::V14(md) => md.try_into(),
frame_metadata::RuntimeMetadata::V15(md) => md.try_into(),
_ => return Err("Cannot try_into() to Metadata: unsupported metadata version".into()),
};
Expand Down
Loading

0 comments on commit db542dc

Please sign in to comment.