From a491858dddb405148327cd53b791c0e612d19836 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 14 Feb 2023 16:36:45 +0200 Subject: [PATCH] XXX: Include return type for decoding Signed-off-by: Alexandru Vasile --- codegen/src/api/mod.rs | 46 +----------------------- codegen/src/api/runtime_api.rs | 2 +- examples/examples/runtime_calls.rs | 28 --------------- subxt/src/client/online_client.rs | 5 +-- subxt/src/runtime_api/runtime_payload.rs | 14 +++++--- subxt/src/runtime_api/runtime_types.rs | 12 ++++--- 6 files changed, 20 insertions(+), 87 deletions(-) diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index 4312140b02..faacf6e3c2 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -10,7 +10,6 @@ mod events; mod runtime_api; mod storage; -use scale_info::form::PortableForm; use subxt_metadata::get_metadata_per_pallet_hash; use self::runtime_api::generate_runtime_api; @@ -32,10 +31,7 @@ use crate::{ }; use codec::Decode; use frame_metadata::{ - v15::{ - RuntimeMetadataV15, - TraitMetadata, - }, + v15::RuntimeMetadataV15, RuntimeMetadata, RuntimeMetadataPrefixed, }; @@ -158,44 +154,6 @@ pub struct RuntimeGenerator { metadata: RuntimeMetadataV15, } -fn generate_runtime_call_api( - type_gen: &TypeGenerator, - runtime: &Vec>, -) -> TokenStream2 { - let mut result = quote!(); - - for trait_ in runtime { - for method in &trait_.methods { - let trait_name = trait_.name.clone(); - let method_name = method.name.clone(); - - let fn_name = format_ident!("{}_{}", trait_.name, method.name); - - let mut params = Vec::new(); - for input in &method.inputs { - let name = &input.name; - let ty = input.ty; - let ty = type_gen.resolve_type_path(ty.id()); - let param = quote!( #name: #ty ); - params.push(param); - } - - let output = method.output; - let output = type_gen.resolve_type_path(output.id()); - - let fn_ = quote!( - pub fn #fn_name( #( #params, )* ) -> #output { - // call into RPC. - } - ); - - result.extend(fn_); - } - } - - result -} - impl RuntimeGenerator { /// Create a new runtime generator from the provided metadata. /// @@ -233,8 +191,6 @@ impl RuntimeGenerator { derives.clone(), crate_path.clone(), ); - let runtime_api_fns = - generate_runtime_call_api(&type_gen, &self.metadata.runtime); let types_mod = type_gen.generate_types_mod(); let types_mod_ident = types_mod.ident(); diff --git a/codegen/src/api/runtime_api.rs b/codegen/src/api/runtime_api.rs index 8c14ebb3be..71a2b00180 100644 --- a/codegen/src/api/runtime_api.rs +++ b/codegen/src/api/runtime_api.rs @@ -53,7 +53,7 @@ fn generate_trait_api( quote!( #( #[doc = #docs ] )* - pub fn #method_name( #( #params, )* ) -> #crate_path::runtime_api::RuntimeAPIPayload { + pub fn #method_name( #( #params, )* ) -> #crate_path::runtime_api::RuntimeAPIPayload<#output> { let mut #vec_acc = Vec::new(); #( #encoded; )* diff --git a/examples/examples/runtime_calls.rs b/examples/examples/runtime_calls.rs index 9dc470d9e8..f194a3cd8e 100644 --- a/examples/examples/runtime_calls.rs +++ b/examples/examples/runtime_calls.rs @@ -40,26 +40,12 @@ async fn main() -> Result<(), Box> { let bytes = api.runtime_api().at(None).await?.call(api_tx).await?; println!("Result: {:?}", bytes); - let result: polkadot::runtime_api::Core::version_target = - Decode::decode(&mut &bytes[..])?; - println!( - "Result for polkadot::runtime_api::Core::version(): {:?}\n\n", - result - ); - let api_tx = polkadot::runtime_api::Metadata::metadata_versions(); println!("RuntimeApi payload: {:?}", api_tx); let bytes = api.runtime_api().at(None).await?.call(api_tx).await?; println!("Result: {:?}", bytes); - let result: polkadot::runtime_api::Metadata::metadata_versions_target = - Decode::decode(&mut &bytes[..])?; - println!( - "Result for polkadot::runtime_api::Metadata::metadata_versions(): {:?}\n\n", - result - ); - let alice = AccountKeyring::Alice.to_account_id().into(); let api_tx = polkadot::runtime_api::AccountNonceApi::account_nonce(alice); println!("RuntimeApi payload: {:?}", api_tx); @@ -67,13 +53,6 @@ async fn main() -> Result<(), Box> { let bytes = api.runtime_api().at(None).await?.call(api_tx).await?; println!("Result: {:?}", bytes); - let result: polkadot::runtime_api::AccountNonceApi::account_nonce_target = - Decode::decode(&mut &bytes[..])?; - println!( - "Result for polkadot::runtime_api::AccountNonceApi::account_nonce: {:?}\n\n", - result - ); - // Send from Alice to Bob. let signer = PairSigner::new(AccountKeyring::Alice.pair()); let dest = AccountKeyring::Bob.to_account_id().into(); @@ -95,12 +74,5 @@ async fn main() -> Result<(), Box> { let bytes = api.runtime_api().at(None).await?.call(api_tx).await?; println!("Result: {:?}", bytes); - let result: polkadot::runtime_api::AccountNonceApi::account_nonce_target = - Decode::decode(&mut &bytes[..])?; - println!( - "Result for polkadot::runtime_api::AccountNonceApi::account_nonce: {:?}\n\n", - result - ); - Ok(()) } diff --git a/subxt/src/client/online_client.rs b/subxt/src/client/online_client.rs index b7aaf3701a..efa42bc19a 100644 --- a/subxt/src/client/online_client.rs +++ b/subxt/src/client/online_client.rs @@ -25,10 +25,7 @@ use crate::{ Config, Metadata, }; -use codec::{ - Compact, - Decode, -}; +use codec::Decode; use derivative::Derivative; use frame_metadata::{ OpaqueMetadata, diff --git a/subxt/src/runtime_api/runtime_payload.rs b/subxt/src/runtime_api/runtime_payload.rs index af3f8a7fed..c8c493d53f 100644 --- a/subxt/src/runtime_api/runtime_payload.rs +++ b/subxt/src/runtime_api/runtime_payload.rs @@ -2,15 +2,18 @@ // This file is dual-licensed as Apache-2.0 or GPL-3.0. // see LICENSE for license details. +use std::marker::PhantomData; + /// Payload for a runtime API fn. #[derive(Debug)] -pub struct RuntimeAPIPayload { +pub struct RuntimeAPIPayload { func_name: &'static str, data: Vec, - validation_hash: Option<[u8; 32]>, + _validation_hash: Option<[u8; 32]>, + _marker: PhantomData, } -impl RuntimeAPIPayload { +impl RuntimeAPIPayload { /// Create a new [`RuntimeAPIPayload`] from static data. pub fn new( func_name: &'static str, @@ -20,14 +23,15 @@ impl RuntimeAPIPayload { RuntimeAPIPayload { func_name, data, - validation_hash: Some(validation_hash), + _validation_hash: Some(validation_hash), + _marker: PhantomData, } } /// Do not validate this prior to submitting it. pub fn unvalidated(self) -> Self { Self { - validation_hash: None, + _validation_hash: None, ..self } } diff --git a/subxt/src/runtime_api/runtime_types.rs b/subxt/src/runtime_api/runtime_types.rs index fd07d50342..9b3b10f2ff 100644 --- a/subxt/src/runtime_api/runtime_types.rs +++ b/subxt/src/runtime_api/runtime_types.rs @@ -7,6 +7,7 @@ use crate::{ error::Error, Config, }; +use codec::Decode; use derivative::Derivative; use std::{ future::Future, @@ -60,12 +61,13 @@ where } /// Execute a runtime API call for the given payload. - pub fn call( + pub fn call( &self, - payload: RuntimeAPIPayload, - ) -> impl Future, Error>> { + payload: RuntimeAPIPayload, + ) -> impl Future> { let client = self.client.clone(); let block_hash = self.block_hash; + // Ensure that the returned future doesn't have a lifetime tied to api.runtime_api(), // which is a temporary thing we'll be throwing away quickly: async move { @@ -77,7 +79,9 @@ where .rpc() .state_call(function, call_parameters, Some(block_hash)) .await?; - Ok(data.0) + + let result: ReturnTy = Decode::decode(&mut &data.0[..])?; + Ok(result) } } }