From ed95f05c10cea5ac929c8581518ed811f3b63d67 Mon Sep 17 00:00:00 2001 From: muharem Date: Tue, 12 Jul 2022 12:06:25 +0200 Subject: [PATCH 01/14] Transaction payment RPC calls: query call info --- Cargo.lock | 2 + .../rpc/runtime-api/Cargo.toml | 2 + .../rpc/runtime-api/src/lib.rs | 36 ++++++++- frame/transaction-payment/rpc/src/lib.rs | 32 ++++++++ frame/transaction-payment/src/lib.rs | 80 +++++++++++++++++++ 5 files changed, 151 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index d68330cfb15c2..b0d5c0b03f049 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6471,7 +6471,9 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "sp-api", + "sp-core", "sp-runtime", + "sp-std", ] [[package]] diff --git a/frame/transaction-payment/rpc/runtime-api/Cargo.toml b/frame/transaction-payment/rpc/runtime-api/Cargo.toml index 5e1cb46753524..b80b2f57053d3 100644 --- a/frame/transaction-payment/rpc/runtime-api/Cargo.toml +++ b/frame/transaction-payment/rpc/runtime-api/Cargo.toml @@ -17,6 +17,8 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features = pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../transaction-payment" } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/api" } sp-runtime = { version = "6.0.0", default-features = false, path = "../../../../primitives/runtime" } +sp-std = { version = "4.0.0", default-features = false, path = "../../../../primitives/std" } +sp-core = { version = "6.0.0", default-features = false, path = "../../../../primitives/core" } [features] default = ["std"] diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index 5a0c70138db24..ead72ff422a44 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -20,7 +20,9 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::Codec; -use sp_runtime::traits::MaybeDisplay; +use sp_core::Bytes; +use sp_runtime::{traits::MaybeDisplay, DispatchError}; +use sp_std::marker::PhantomData; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; @@ -30,5 +32,37 @@ sp_api::decl_runtime_apis! { { fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; + fn query_call_info(encoded_call: Bytes) -> Result, DispatchError>; + fn query_weight_to_fee(encoded_call: Bytes) -> Result; + // fn query_call_info(call: GetDispatchInfo, len: u32) -> RuntimeDispatchInfo; + // fn query_weight_to_fee(call: GetDispatchInfo) -> Balance; + } +} + +/// Helper type to implement runtime api functions not included to the pallet +pub struct TransactionPayment(PhantomData<(T, Balance)>); +impl TransactionPayment +where + T: pallet_transaction_payment::Config, + Balance: Codec + MaybeDisplay, +{ + fn query_call_info(encoded_call: Bytes) -> Result, DispatchError> { + // let encoded_len = encoded_call.len(); + // let call: Call = Call::decode(&mut &*encoded_call).map_err(|e| { + // Error(e) // TODO map decode error to DispatchError::Module to bubble error info + // }); + // Ok (RuntimeDispatchInfo { + // weight, + // class, + // partial_fee: pallet_transaction_payment::Pallet::::query_call_info(len, call) }) + unimplemented!(); + } + + fn query_weight_to_fee(encoded_call: Bytes) -> Result { + // let call: Call = Call::decode(&mut &*encoded_call).map_err(|e| { + // Error(e) // TODO map decode error to DispatchError::Module to bubble error info + // }); + // Ok(pallet_transaction_payment::Pallet::::query_weight_to_fee(call)) + unimplemented!(); } } diff --git a/frame/transaction-payment/rpc/src/lib.rs b/frame/transaction-payment/rpc/src/lib.rs index 75ec42321ef5e..9e9762094dcbf 100644 --- a/frame/transaction-payment/rpc/src/lib.rs +++ b/frame/transaction-payment/rpc/src/lib.rs @@ -48,6 +48,20 @@ pub trait TransactionPaymentApi { encoded_xt: Bytes, at: Option, ) -> RpcResult>; + + #[method(name = "payment_queryCallInfo")] + fn query_call_info( + &self, + encoded_call: Bytes, + at: Option, + ) -> RpcResult>; + + #[method(name = "payment_queryWeightToFee")] + fn query_weight_to_fee( + &self, + encoded_call: Bytes, + at: Option, + ) -> RpcResult; } /// Provides RPC methods to query a dispatchable's class, weight and fee. @@ -166,4 +180,22 @@ where tip: Default::default(), }) } + + fn query_call_info( + &self, + encoded_call: Bytes, + at: Option, + ) -> RpcResult> { + // TODO call api and map runtime api error to Error + unimplemented!(); + } + + fn query_weight_to_fee( + &self, + encoded_call: Bytes, + at: Option, + ) -> RpcResult { + // TODO call api and map runtime api error to Error + unimplemented!(); + } } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 0f5c0321130be..4b1da1680226c 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -445,6 +445,31 @@ where } } + /// Query runtime dispatch info of a given `Call`. + pub fn query_call_info( + call: Call, + len: u32, + ) -> RuntimeDispatchInfo> + where + T::Call: Dispatchable, + { + let dispatch_info = Call::get_dispatch_info(&call); + let DispatchInfo { weight, class, .. } = dispatch_info; + + RuntimeDispatchInfo { + weight, + class, + partial_fee: Self::compute_fee(len, &dispatch_info, 0u32.into()), + } + } + + /// Query weight_to_fee of a given `Call`. + pub fn query_weight_to_fee(call: Call) -> BalanceOf { + let dispatch_info = Call::get_dispatch_info(&call); + + Self::weight_to_fee(dispatch_info.weight) + } + /// Compute the final fee value for a particular transaction. pub fn compute_fee(len: u32, info: &DispatchInfoOf, tip: BalanceOf) -> BalanceOf where @@ -1206,6 +1231,61 @@ mod tests { }); } + #[test] + fn query_call_info_works() { + let call = Call::Balances(BalancesCall::transfer { dest: 2, value: 69 }); + let info = call.get_dispatch_info(); + let ext = call.encode(); + let len = ext.len() as u32; + + ExtBuilder::default().base_weight(5).weight_fee(2).build().execute_with(|| { + // all fees should be x1.5 + >::put(Multiplier::saturating_from_rational(3, 2)); + + assert_eq!( + TransactionPayment::query_call_info(call, len), + RuntimeDispatchInfo { + weight: info.weight, + class: info.class, + partial_fee: 5 * 2 /* base * weight_fee */ + + len as u64 /* len * 1 */ + + info.weight.min(BlockWeights::get().max_block) as u64 * 2 * 3 / 2 /* weight */ + }, + ); + }); + } + + #[test] + fn query_weight_to_fee_works() { + let call = Call::Balances(BalancesCall::transfer { dest: 2, value: 69 }); + let info = call.get_dispatch_info(); + + let origin = 111111; + let extra = (); + let xt = TestXt::new(call.clone(), Some((origin, extra))); + let xt_info = xt.get_dispatch_info(); + + let unsigned_xt = TestXt::<_, ()>::new(call.clone(), None); + let unsigned_xt_info = unsigned_xt.get_dispatch_info(); + + ExtBuilder::default().weight_fee(2).build().execute_with(|| { + assert_eq!( + TransactionPayment::query_weight_to_fee(call), + info.weight.min(BlockWeights::get().max_block) as u64 * 2, + ); + + assert_eq!( + TransactionPayment::query_weight_to_fee(xt), + xt_info.weight.min(BlockWeights::get().max_block) as u64 * 2, + ); + + assert_eq!( + TransactionPayment::query_weight_to_fee(unsigned_xt), + unsigned_xt_info.weight.min(BlockWeights::get().max_block) as u64 * 2, + ); + }); + } + #[test] fn compute_fee_works_without_multiplier() { ExtBuilder::default() From 535b925af9f8c1404a1c0faf13f6bbe3a31c6ee8 Mon Sep 17 00:00:00 2001 From: muharem Date: Sat, 16 Jul 2022 20:59:36 +0200 Subject: [PATCH 02/14] transaction payment pallet - runtime api - add query_call info and fee_details --- bin/node-template/runtime/src/lib.rs | 14 ++++- bin/node/runtime/src/lib.rs | 10 ++++ .../rpc/runtime-api/src/lib.rs | 41 ++----------- frame/transaction-payment/rpc/src/lib.rs | 41 ++----------- frame/transaction-payment/src/lib.rs | 58 ++++++++----------- 5 files changed, 57 insertions(+), 107 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index c514cdf6c25fd..4b60580b61b8f 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -448,7 +448,7 @@ impl_runtime_apis! { } } - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { fn query_info( uxt: ::Extrinsic, len: u32, @@ -461,6 +461,18 @@ impl_runtime_apis! { ) -> pallet_transaction_payment::FeeDetails { TransactionPayment::query_fee_details(uxt, len) } + fn query_call_info( + call: Call, + len: u32, + ) -> pallet_transaction_payment::RuntimeDispatchInfo { + TransactionPayment::query_call_info(call, len) + } + fn query_call_fee_details( + call: Call, + len: u32 + ) -> pallet_transaction_payment::FeeDetails { + TransactionPayment::query_call_fee_details(call, len) + } } #[cfg(feature = "runtime-benchmarks")] diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 2d5981339fab3..74d4637cbd389 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1946,6 +1946,7 @@ impl_runtime_apis! { impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< Block, Balance, + Call, > for Runtime { fn query_info(uxt: ::Extrinsic, len: u32) -> RuntimeDispatchInfo { TransactionPayment::query_info(uxt, len) @@ -1953,6 +1954,15 @@ impl_runtime_apis! { fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { TransactionPayment::query_fee_details(uxt, len) } + fn query_call_info( + call: Call, + len: u32, + ) -> RuntimeDispatchInfo { + TransactionPayment::query_call_info(call, len) + } + fn query_call_fee_details(call: Call, len: u32) -> FeeDetails { + TransactionPayment::query_call_fee_details(call, len) + } } impl pallet_mmr::primitives::MmrApi< diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index ead72ff422a44..aa8987f1f1737 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -20,49 +20,18 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::Codec; -use sp_core::Bytes; -use sp_runtime::{traits::MaybeDisplay, DispatchError}; -use sp_std::marker::PhantomData; +use sp_runtime::traits::MaybeDisplay; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; sp_api::decl_runtime_apis! { - pub trait TransactionPaymentApi where + pub trait TransactionPaymentApi where Balance: Codec + MaybeDisplay, + Call: Codec, { fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; - fn query_call_info(encoded_call: Bytes) -> Result, DispatchError>; - fn query_weight_to_fee(encoded_call: Bytes) -> Result; - // fn query_call_info(call: GetDispatchInfo, len: u32) -> RuntimeDispatchInfo; - // fn query_weight_to_fee(call: GetDispatchInfo) -> Balance; - } -} - -/// Helper type to implement runtime api functions not included to the pallet -pub struct TransactionPayment(PhantomData<(T, Balance)>); -impl TransactionPayment -where - T: pallet_transaction_payment::Config, - Balance: Codec + MaybeDisplay, -{ - fn query_call_info(encoded_call: Bytes) -> Result, DispatchError> { - // let encoded_len = encoded_call.len(); - // let call: Call = Call::decode(&mut &*encoded_call).map_err(|e| { - // Error(e) // TODO map decode error to DispatchError::Module to bubble error info - // }); - // Ok (RuntimeDispatchInfo { - // weight, - // class, - // partial_fee: pallet_transaction_payment::Pallet::::query_call_info(len, call) }) - unimplemented!(); - } - - fn query_weight_to_fee(encoded_call: Bytes) -> Result { - // let call: Call = Call::decode(&mut &*encoded_call).map_err(|e| { - // Error(e) // TODO map decode error to DispatchError::Module to bubble error info - // }); - // Ok(pallet_transaction_payment::Pallet::::query_weight_to_fee(call)) - unimplemented!(); + fn query_call_info(call: Call, len: u32) -> RuntimeDispatchInfo; + fn query_call_fee_details(call: Call, len: u32) -> FeeDetails; } } diff --git a/frame/transaction-payment/rpc/src/lib.rs b/frame/transaction-payment/rpc/src/lib.rs index 9e9762094dcbf..bc1033eb47dae 100644 --- a/frame/transaction-payment/rpc/src/lib.rs +++ b/frame/transaction-payment/rpc/src/lib.rs @@ -38,7 +38,7 @@ use sp_runtime::{ pub use pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi as TransactionPaymentRuntimeApi; #[rpc(client, server)] -pub trait TransactionPaymentApi { +pub trait TransactionPaymentApi { #[method(name = "payment_queryInfo")] fn query_info(&self, encoded_xt: Bytes, at: Option) -> RpcResult; @@ -48,20 +48,6 @@ pub trait TransactionPaymentApi { encoded_xt: Bytes, at: Option, ) -> RpcResult>; - - #[method(name = "payment_queryCallInfo")] - fn query_call_info( - &self, - encoded_call: Bytes, - at: Option, - ) -> RpcResult>; - - #[method(name = "payment_queryWeightToFee")] - fn query_weight_to_fee( - &self, - encoded_call: Bytes, - at: Option, - ) -> RpcResult; } /// Provides RPC methods to query a dispatchable's class, weight and fee. @@ -96,13 +82,14 @@ impl From for i32 { } #[async_trait] -impl - TransactionPaymentApiServer<::Hash, RuntimeDispatchInfo> +impl + TransactionPaymentApiServer<::Hash, RuntimeDispatchInfo, Call> for TransactionPayment where Block: BlockT, C: ProvideRuntimeApi + HeaderBackend + Send + Sync + 'static, - C::Api: TransactionPaymentRuntimeApi, + C::Api: TransactionPaymentRuntimeApi, + Call: Codec + Send + Sync + 'static, Balance: Codec + MaybeDisplay + Copy + TryInto + Send + Sync + 'static, { fn query_info( @@ -180,22 +167,4 @@ where tip: Default::default(), }) } - - fn query_call_info( - &self, - encoded_call: Bytes, - at: Option, - ) -> RpcResult> { - // TODO call api and map runtime api error to Error - unimplemented!(); - } - - fn query_weight_to_fee( - &self, - encoded_call: Bytes, - at: Option, - ) -> RpcResult { - // TODO call api and map runtime api error to Error - unimplemented!(); - } } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 4b1da1680226c..7949f56c00c0f 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -464,10 +464,18 @@ where } /// Query weight_to_fee of a given `Call`. - pub fn query_weight_to_fee(call: Call) -> BalanceOf { + pub fn query_call_fee_details( + call: Call, + len: u32, + ) -> FeeDetails> + where + T::Call: Dispatchable, + { let dispatch_info = Call::get_dispatch_info(&call); - Self::weight_to_fee(dispatch_info.weight) + let tip = 0u32.into(); + + Self::compute_fee_details(len, &dispatch_info, tip) } /// Compute the final fee value for a particular transaction. @@ -1232,18 +1240,18 @@ mod tests { } #[test] - fn query_call_info_works() { + fn query_call_info_and_fee_details_works() { let call = Call::Balances(BalancesCall::transfer { dest: 2, value: 69 }); let info = call.get_dispatch_info(); - let ext = call.encode(); - let len = ext.len() as u32; + let encoded_call = call.encode(); + let len = encoded_call.len() as u32; ExtBuilder::default().base_weight(5).weight_fee(2).build().execute_with(|| { // all fees should be x1.5 >::put(Multiplier::saturating_from_rational(3, 2)); assert_eq!( - TransactionPayment::query_call_info(call, len), + TransactionPayment::query_call_info(call.clone(), len), RuntimeDispatchInfo { weight: info.weight, class: info.class, @@ -1252,36 +1260,18 @@ mod tests { + info.weight.min(BlockWeights::get().max_block) as u64 * 2 * 3 / 2 /* weight */ }, ); - }); - } - - #[test] - fn query_weight_to_fee_works() { - let call = Call::Balances(BalancesCall::transfer { dest: 2, value: 69 }); - let info = call.get_dispatch_info(); - - let origin = 111111; - let extra = (); - let xt = TestXt::new(call.clone(), Some((origin, extra))); - let xt_info = xt.get_dispatch_info(); - - let unsigned_xt = TestXt::<_, ()>::new(call.clone(), None); - let unsigned_xt_info = unsigned_xt.get_dispatch_info(); - - ExtBuilder::default().weight_fee(2).build().execute_with(|| { - assert_eq!( - TransactionPayment::query_weight_to_fee(call), - info.weight.min(BlockWeights::get().max_block) as u64 * 2, - ); - - assert_eq!( - TransactionPayment::query_weight_to_fee(xt), - xt_info.weight.min(BlockWeights::get().max_block) as u64 * 2, - ); assert_eq!( - TransactionPayment::query_weight_to_fee(unsigned_xt), - unsigned_xt_info.weight.min(BlockWeights::get().max_block) as u64 * 2, + TransactionPayment::query_call_fee_details(call, len), + FeeDetails { + inclusion_fee: Some(InclusionFee { + base_fee: 5 * 2, /* base * weight_fee */ + len_fee: len as u64, /* len * 1 */ + adjusted_weight_fee: info.weight.min(BlockWeights::get().max_block) as u64 * + 2 * 3 / 2 /* weight * weight_fee * multipler */ + }), + tip: 0, + }, ); }); } From 8e707c30c9d4ab8b918adffad39f918bbdbacf75 Mon Sep 17 00:00:00 2001 From: muharem Date: Sat, 16 Jul 2022 21:02:58 +0200 Subject: [PATCH 03/14] remove unused deps --- Cargo.lock | 2 -- frame/transaction-payment/rpc/runtime-api/Cargo.toml | 2 -- 2 files changed, 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b0d5c0b03f049..d68330cfb15c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6471,9 +6471,7 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "sp-api", - "sp-core", "sp-runtime", - "sp-std", ] [[package]] diff --git a/frame/transaction-payment/rpc/runtime-api/Cargo.toml b/frame/transaction-payment/rpc/runtime-api/Cargo.toml index b80b2f57053d3..5e1cb46753524 100644 --- a/frame/transaction-payment/rpc/runtime-api/Cargo.toml +++ b/frame/transaction-payment/rpc/runtime-api/Cargo.toml @@ -17,8 +17,6 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features = pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../transaction-payment" } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/api" } sp-runtime = { version = "6.0.0", default-features = false, path = "../../../../primitives/runtime" } -sp-std = { version = "4.0.0", default-features = false, path = "../../../../primitives/std" } -sp-core = { version = "6.0.0", default-features = false, path = "../../../../primitives/core" } [features] default = ["std"] From 083fa771c8097368e601a9ade5136a35eb92b33a Mon Sep 17 00:00:00 2001 From: muharem Date: Sun, 17 Jul 2022 21:04:48 +0200 Subject: [PATCH 04/14] separate call runtime api --- bin/node-template/runtime/src/lib.rs | 10 +++++++++- bin/node/runtime/src/lib.rs | 8 +++++++- frame/transaction-payment/rpc/runtime-api/src/lib.rs | 9 +++++++-- frame/transaction-payment/rpc/src/lib.rs | 9 ++++----- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 4b60580b61b8f..2ba78157698db 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -448,7 +448,9 @@ impl_runtime_apis! { } } - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< + Block, + Balance> for Runtime { fn query_info( uxt: ::Extrinsic, len: u32, @@ -461,6 +463,12 @@ impl_runtime_apis! { ) -> pallet_transaction_payment::FeeDetails { TransactionPayment::query_fee_details(uxt, len) } + } + + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi< + Block, + Balance, + Call> for Runtime { fn query_call_info( call: Call, len: u32, diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 74d4637cbd389..7bf6ad440e85d 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1946,7 +1946,6 @@ impl_runtime_apis! { impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< Block, Balance, - Call, > for Runtime { fn query_info(uxt: ::Extrinsic, len: u32) -> RuntimeDispatchInfo { TransactionPayment::query_info(uxt, len) @@ -1954,6 +1953,13 @@ impl_runtime_apis! { fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { TransactionPayment::query_fee_details(uxt, len) } + } + + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi< + Block, + Balance, + Call, + > for Runtime { fn query_call_info( call: Call, len: u32, diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index aa8987f1f1737..6c125b155e5ce 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -25,12 +25,17 @@ use sp_runtime::traits::MaybeDisplay; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; sp_api::decl_runtime_apis! { - pub trait TransactionPaymentApi where + pub trait TransactionPaymentApi where Balance: Codec + MaybeDisplay, - Call: Codec, { fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; + } + + pub trait TransactionPaymentCallApi where + Balance: Codec + MaybeDisplay, + Call: Codec, + { fn query_call_info(call: Call, len: u32) -> RuntimeDispatchInfo; fn query_call_fee_details(call: Call, len: u32) -> FeeDetails; } diff --git a/frame/transaction-payment/rpc/src/lib.rs b/frame/transaction-payment/rpc/src/lib.rs index bc1033eb47dae..75ec42321ef5e 100644 --- a/frame/transaction-payment/rpc/src/lib.rs +++ b/frame/transaction-payment/rpc/src/lib.rs @@ -38,7 +38,7 @@ use sp_runtime::{ pub use pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi as TransactionPaymentRuntimeApi; #[rpc(client, server)] -pub trait TransactionPaymentApi { +pub trait TransactionPaymentApi { #[method(name = "payment_queryInfo")] fn query_info(&self, encoded_xt: Bytes, at: Option) -> RpcResult; @@ -82,14 +82,13 @@ impl From for i32 { } #[async_trait] -impl - TransactionPaymentApiServer<::Hash, RuntimeDispatchInfo, Call> +impl + TransactionPaymentApiServer<::Hash, RuntimeDispatchInfo> for TransactionPayment where Block: BlockT, C: ProvideRuntimeApi + HeaderBackend + Send + Sync + 'static, - C::Api: TransactionPaymentRuntimeApi, - Call: Codec + Send + Sync + 'static, + C::Api: TransactionPaymentRuntimeApi, Balance: Codec + MaybeDisplay + Copy + TryInto + Send + Sync + 'static, { fn query_info( From a597aeac30a90abc8773f26d73264363e45cfab7 Mon Sep 17 00:00:00 2001 From: muharem Date: Sun, 17 Jul 2022 21:17:58 +0200 Subject: [PATCH 05/14] undo fmt for unchanged code --- bin/node-template/runtime/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 2ba78157698db..1d14a427a0f5c 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -448,9 +448,7 @@ impl_runtime_apis! { } } - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< - Block, - Balance> for Runtime { + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { fn query_info( uxt: ::Extrinsic, len: u32, From 7c6fb38c88f151288b536ddbaf799226ed6a8663 Mon Sep 17 00:00:00 2001 From: muharem Date: Tue, 26 Jul 2022 11:14:59 +0200 Subject: [PATCH 06/14] system config call bounded to GetDispatchInfo, drop Call generic for query call info/fee --- frame/system/src/lib.rs | 6 +++--- frame/transaction-payment/src/lib.rs | 15 ++++----------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 3c6f514808b5d..61c9c5464c60c 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -91,8 +91,8 @@ use frame_support::{ OriginTrait, PalletInfo, SortedMembers, StoredMap, TypedGet, }, weights::{ - extract_actual_weight, DispatchClass, DispatchInfo, PerDispatchClass, RuntimeDbWeight, - Weight, + extract_actual_weight, DispatchClass, DispatchInfo, GetDispatchInfo, PerDispatchClass, + RuntimeDbWeight, Weight, }, Parameter, }; @@ -222,7 +222,7 @@ pub mod pallet { + OriginTrait; /// The aggregated `Call` type. - type Call: Dispatchable + Debug; + type Call: Dispatchable + Debug + GetDispatchInfo; /// Account index (aka nonce) type. This stores the number of previous transactions /// associated with a sender account. diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 7949f56c00c0f..15a2a0e62aed2 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -446,14 +446,11 @@ where } /// Query runtime dispatch info of a given `Call`. - pub fn query_call_info( - call: Call, - len: u32, - ) -> RuntimeDispatchInfo> + pub fn query_call_info(call: T::Call, len: u32) -> RuntimeDispatchInfo> where T::Call: Dispatchable, { - let dispatch_info = Call::get_dispatch_info(&call); + let dispatch_info = ::get_dispatch_info(&call); let DispatchInfo { weight, class, .. } = dispatch_info; RuntimeDispatchInfo { @@ -464,15 +461,11 @@ where } /// Query weight_to_fee of a given `Call`. - pub fn query_call_fee_details( - call: Call, - len: u32, - ) -> FeeDetails> + pub fn query_call_fee_details(call: T::Call, len: u32) -> FeeDetails> where T::Call: Dispatchable, { - let dispatch_info = Call::get_dispatch_info(&call); - + let dispatch_info = ::get_dispatch_info(&call); let tip = 0u32.into(); Self::compute_fee_details(len, &dispatch_info, tip) From 20fb5d05a3df8a9335bad3752abfed827803cd1f Mon Sep 17 00:00:00 2001 From: muharem Date: Tue, 26 Jul 2022 11:44:37 +0200 Subject: [PATCH 07/14] impl GetDispatchInfo for Extrinsics within runtime test-utils --- test-utils/runtime/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index ea62f2ac84f3d..983ab7ea06020 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -241,6 +241,12 @@ impl sp_runtime::traits::Dispatchable for Extrinsic { } } +impl frame_support::weights::GetDispatchInfo for Extrinsic { + fn get_dispatch_info(&self) -> frame_support::weights::DispatchInfo { + panic!("This implementation should not be used for actual get_dispatch_info."); + } +} + impl Extrinsic { /// Convert `&self` into `&Transfer`. /// From a7efefe5b2e138c44ea028d67d6c31a00f45d98f Mon Sep 17 00:00:00 2001 From: muharem Date: Mon, 1 Aug 2022 18:38:29 +0200 Subject: [PATCH 08/14] introduced runtime api methods accept encoded Call instead of Call type --- Cargo.lock | 1 + bin/node-template/runtime/src/lib.rs | 36 ++++-------- bin/node/runtime/src/lib.rs | 28 ++++----- .../rpc/runtime-api/Cargo.toml | 2 + .../rpc/runtime-api/src/lib.rs | 13 ++--- frame/transaction-payment/src/lib.rs | 57 +++++++++++++------ 6 files changed, 71 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 50603c6235a23..72511869d5247 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6378,6 +6378,7 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "sp-api", + "sp-core", "sp-runtime", ] diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index a943199ababf2..4fbdd5f6cd2a2 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -11,12 +11,12 @@ use pallet_grandpa::{ }; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, Bytes, OpaqueMetadata}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, Verify}, transaction_validity::{TransactionSource, TransactionValidity}, - ApplyExtrinsicResult, MultiSignature, + ApplyExtrinsicResult, MultiSignature, RuntimeString, }; use sp_std::prelude::*; #[cfg(feature = "std")] @@ -38,7 +38,7 @@ pub use frame_support::{ pub use frame_system::Call as SystemCall; pub use pallet_balances::Call as BalancesCall; pub use pallet_timestamp::Call as TimestampCall; -use pallet_transaction_payment::CurrencyAdapter; +use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInfo}; #[cfg(any(feature = "std", test))] pub use sp_runtime::BuildStorage; pub use sp_runtime::{Perbill, Permill}; @@ -452,35 +452,23 @@ impl_runtime_apis! { } impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { - fn query_info( - uxt: ::Extrinsic, - len: u32, - ) -> pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo { + fn query_info(uxt: ::Extrinsic, len: u32) -> RuntimeDispatchInfo { TransactionPayment::query_info(uxt, len) } - fn query_fee_details( - uxt: ::Extrinsic, - len: u32, - ) -> pallet_transaction_payment::FeeDetails { + fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { TransactionPayment::query_fee_details(uxt, len) } - } - - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi< - Block, - Balance, - Call> for Runtime { fn query_call_info( - call: Call, + encoded_call: Bytes, len: u32, - ) -> pallet_transaction_payment::RuntimeDispatchInfo { - TransactionPayment::query_call_info(call, len) + ) -> Result, RuntimeString> { + TransactionPayment::query_call_info(encoded_call, len) } fn query_call_fee_details( - call: Call, - len: u32 - ) -> pallet_transaction_payment::FeeDetails { - TransactionPayment::query_call_fee_details(call, len) + encoded_call: Bytes, + len: u32, + ) -> Result, RuntimeString> { + TransactionPayment::query_call_fee_details(encoded_call, len) } } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index b638840c65929..9e6c4758d8d4d 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -57,7 +57,7 @@ pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdj use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; use sp_api::impl_runtime_apis; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, Bytes, OpaqueMetadata}; use sp_inherents::{CheckInherentsResult, InherentData}; use sp_runtime::{ create_runtime_str, @@ -69,6 +69,7 @@ use sp_runtime::{ }, transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity}, ApplyExtrinsicResult, FixedPointNumber, FixedU128, Perbill, Percent, Permill, Perquintill, + RuntimeString, }; use sp_std::prelude::*; #[cfg(any(feature = "std", test))] @@ -1952,31 +1953,24 @@ impl_runtime_apis! { } } - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< - Block, - Balance, - > for Runtime { + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { fn query_info(uxt: ::Extrinsic, len: u32) -> RuntimeDispatchInfo { TransactionPayment::query_info(uxt, len) } fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { TransactionPayment::query_fee_details(uxt, len) } - } - - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi< - Block, - Balance, - Call, - > for Runtime { fn query_call_info( - call: Call, + encoded_call: Bytes, len: u32, - ) -> RuntimeDispatchInfo { - TransactionPayment::query_call_info(call, len) + ) -> Result, RuntimeString> { + TransactionPayment::query_call_info(encoded_call, len) } - fn query_call_fee_details(call: Call, len: u32) -> FeeDetails { - TransactionPayment::query_call_fee_details(call, len) + fn query_call_fee_details( + encoded_call: Bytes, + len: u32, + ) -> Result, RuntimeString> { + TransactionPayment::query_call_fee_details(encoded_call, len) } } diff --git a/frame/transaction-payment/rpc/runtime-api/Cargo.toml b/frame/transaction-payment/rpc/runtime-api/Cargo.toml index 5e1cb46753524..8fbad897a096f 100644 --- a/frame/transaction-payment/rpc/runtime-api/Cargo.toml +++ b/frame/transaction-payment/rpc/runtime-api/Cargo.toml @@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"] codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../transaction-payment" } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/api" } +sp-core = { version = "6.0.0", default-features = false, path = "../../../../primitives/core" } sp-runtime = { version = "6.0.0", default-features = false, path = "../../../../primitives/runtime" } [features] @@ -24,5 +25,6 @@ std = [ "codec/std", "pallet-transaction-payment/std", "sp-api/std", + "sp-core/std", "sp-runtime/std", ] diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index 6c125b155e5ce..ee7311b9f2710 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -20,7 +20,8 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::Codec; -use sp_runtime::traits::MaybeDisplay; +use sp_core::Bytes; +use sp_runtime::{traits::MaybeDisplay, RuntimeString}; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; @@ -30,13 +31,7 @@ sp_api::decl_runtime_apis! { { fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; - } - - pub trait TransactionPaymentCallApi where - Balance: Codec + MaybeDisplay, - Call: Codec, - { - fn query_call_info(call: Call, len: u32) -> RuntimeDispatchInfo; - fn query_call_fee_details(call: Call, len: u32) -> FeeDetails; + fn query_call_info(encoded_call: Bytes, len: u32) -> Result, RuntimeString>; + fn query_call_fee_details(encoded_call: Bytes, len: u32) -> Result, RuntimeString>; } } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 5241a85a1cac1..206e3f0d76bdf 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -50,6 +50,7 @@ use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; +use sp_core::Bytes; use sp_runtime::{ traits::{ Convert, DispatchInfoOf, Dispatchable, One, PostDispatchInfoOf, SaturatedConversion, @@ -58,7 +59,7 @@ use sp_runtime::{ transaction_validity::{ TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransaction, }, - FixedPointNumber, FixedPointOperand, FixedU128, Perquintill, RuntimeDebug, + FixedPointNumber, FixedPointOperand, FixedU128, Perquintill, RuntimeDebug, RuntimeString, }; use sp_std::prelude::*; @@ -445,30 +446,42 @@ where } } - /// Query runtime dispatch info of a given `Call`. - pub fn query_call_info(call: T::Call, len: u32) -> RuntimeDispatchInfo> + /// Query runtime dispatch info of a given encoded `Call`. + pub fn query_call_info( + encoded_call: Bytes, + len: u32, + ) -> Result>, RuntimeString> where - T::Call: Dispatchable, + T::Call: Dispatchable + Decode, { + let call: T::Call = Decode::decode(&mut &*encoded_call) + .map_err(|_| -> RuntimeString { "Failed to decode Call.".into() })?; + let dispatch_info = ::get_dispatch_info(&call); let DispatchInfo { weight, class, .. } = dispatch_info; - RuntimeDispatchInfo { + Ok(RuntimeDispatchInfo { weight, class, partial_fee: Self::compute_fee(len, &dispatch_info, 0u32.into()), - } + }) } - /// Query weight_to_fee of a given `Call`. - pub fn query_call_fee_details(call: T::Call, len: u32) -> FeeDetails> + /// Query weight_to_fee of a given encoded `Call`. + pub fn query_call_fee_details( + encoded_call: Bytes, + len: u32, + ) -> Result>, RuntimeString> where - T::Call: Dispatchable, + T::Call: Dispatchable + Decode, { + let call: T::Call = Decode::decode(&mut &*encoded_call) + .map_err(|_| -> RuntimeString { "Failed to decode Call.".into() })?; + let dispatch_info = ::get_dispatch_info(&call); let tip = 0u32.into(); - Self::compute_fee_details(len, &dispatch_info, tip) + Ok(Self::compute_fee_details(len, &dispatch_info, tip)) } /// Compute the final fee value for a particular transaction. @@ -1239,24 +1252,36 @@ mod tests { let encoded_call = call.encode(); let len = encoded_call.len() as u32; + let invalid_encoded_call: Vec = vec![1, 2, 3]; + ExtBuilder::default().base_weight(5).weight_fee(2).build().execute_with(|| { // all fees should be x1.5 >::put(Multiplier::saturating_from_rational(3, 2)); assert_eq!( - TransactionPayment::query_call_info(call.clone(), len), - RuntimeDispatchInfo { + TransactionPayment::query_call_info(invalid_encoded_call.clone().into(), len), + Err(RuntimeString::Borrowed("Failed to decode Call.")), + ); + + assert_eq!( + TransactionPayment::query_call_info(encoded_call.clone().into(), len), + Ok(RuntimeDispatchInfo { weight: info.weight, class: info.class, partial_fee: 5 * 2 /* base * weight_fee */ + len as u64 /* len * 1 */ + info.weight.min(BlockWeights::get().max_block) as u64 * 2 * 3 / 2 /* weight */ - }, + }), ); assert_eq!( - TransactionPayment::query_call_fee_details(call, len), - FeeDetails { + TransactionPayment::query_call_fee_details(invalid_encoded_call.into(), len), + Err(RuntimeString::Borrowed("Failed to decode Call.")), + ); + + assert_eq!( + TransactionPayment::query_call_fee_details(encoded_call.into(), len), + Ok(FeeDetails { inclusion_fee: Some(InclusionFee { base_fee: 5 * 2, /* base * weight_fee */ len_fee: len as u64, /* len * 1 */ @@ -1264,7 +1289,7 @@ mod tests { 2 * 3 / 2 /* weight * weight_fee * multipler */ }), tip: 0, - }, + }), ); }); } From 7e9a7e21c109ff046c2d2f5bae2749964e692911 Mon Sep 17 00:00:00 2001 From: muharem Date: Thu, 4 Aug 2022 16:51:13 +0200 Subject: [PATCH 09/14] replace Bytes by Vec, docs for for new api, drop len argument, drop GetDispatchInfo bound from system_Config::Call --- Cargo.lock | 1 + bin/node-template/runtime/src/lib.rs | 12 ++++---- bin/node/runtime/src/lib.rs | 12 ++++---- frame/system/src/lib.rs | 6 ++-- .../rpc/runtime-api/Cargo.toml | 2 ++ .../rpc/runtime-api/src/lib.rs | 25 +++++++++++++--- frame/transaction-payment/src/lib.rs | 29 +++++++++---------- 7 files changed, 51 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 72511869d5247..410e7ee1052c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6380,6 +6380,7 @@ dependencies = [ "sp-api", "sp-core", "sp-runtime", + "sp-std", ] [[package]] diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 4fbdd5f6cd2a2..07eba5cf31f7c 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -11,7 +11,7 @@ use pallet_grandpa::{ }; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; -use sp_core::{crypto::KeyTypeId, Bytes, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, Verify}, @@ -459,16 +459,14 @@ impl_runtime_apis! { TransactionPayment::query_fee_details(uxt, len) } fn query_call_info( - encoded_call: Bytes, - len: u32, + encoded_call: Vec, ) -> Result, RuntimeString> { - TransactionPayment::query_call_info(encoded_call, len) + TransactionPayment::query_call_info(encoded_call) } fn query_call_fee_details( - encoded_call: Bytes, - len: u32, + encoded_call: Vec, ) -> Result, RuntimeString> { - TransactionPayment::query_call_fee_details(encoded_call, len) + TransactionPayment::query_call_fee_details(encoded_call) } } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 9e6c4758d8d4d..e55606893348b 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -57,7 +57,7 @@ pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdj use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; use sp_api::impl_runtime_apis; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; -use sp_core::{crypto::KeyTypeId, Bytes, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_inherents::{CheckInherentsResult, InherentData}; use sp_runtime::{ create_runtime_str, @@ -1961,16 +1961,14 @@ impl_runtime_apis! { TransactionPayment::query_fee_details(uxt, len) } fn query_call_info( - encoded_call: Bytes, - len: u32, + encoded_call: Vec, ) -> Result, RuntimeString> { - TransactionPayment::query_call_info(encoded_call, len) + TransactionPayment::query_call_info(encoded_call) } fn query_call_fee_details( - encoded_call: Bytes, - len: u32, + encoded_call: Vec, ) -> Result, RuntimeString> { - TransactionPayment::query_call_fee_details(encoded_call, len) + TransactionPayment::query_call_fee_details(encoded_call) } } diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 391684436252d..94605c2da59bd 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -91,8 +91,8 @@ use frame_support::{ OriginTrait, PalletInfo, SortedMembers, StoredMap, TypedGet, }, weights::{ - extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo, GetDispatchInfo, PerDispatchClass, - RuntimeDbWeight, Weight, + extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo, + PerDispatchClass, RuntimeDbWeight, Weight, }, Parameter, }; @@ -222,7 +222,7 @@ pub mod pallet { + OriginTrait; /// The aggregated `Call` type. - type Call: Dispatchable + Debug + GetDispatchInfo; + type Call: Dispatchable + Debug; /// Account index (aka nonce) type. This stores the number of previous transactions /// associated with a sender account. diff --git a/frame/transaction-payment/rpc/runtime-api/Cargo.toml b/frame/transaction-payment/rpc/runtime-api/Cargo.toml index 8fbad897a096f..91823456cc4ba 100644 --- a/frame/transaction-payment/rpc/runtime-api/Cargo.toml +++ b/frame/transaction-payment/rpc/runtime-api/Cargo.toml @@ -18,6 +18,7 @@ pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/api" } sp-core = { version = "6.0.0", default-features = false, path = "../../../../primitives/core" } sp-runtime = { version = "6.0.0", default-features = false, path = "../../../../primitives/runtime" } +sp-std = { version = "4.0.0", default-features = false, path = "../../../../primitives/std" } [features] default = ["std"] @@ -27,4 +28,5 @@ std = [ "sp-api/std", "sp-core/std", "sp-runtime/std", + "sp-std/std", ] diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index ee7311b9f2710..e4bb76d481547 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -20,18 +20,35 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::Codec; -use sp_core::Bytes; use sp_runtime::{traits::MaybeDisplay, RuntimeString}; +use sp_std::prelude::*; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; sp_api::decl_runtime_apis! { - pub trait TransactionPaymentApi where + pub trait TransactionPaymentApi + where Balance: Codec + MaybeDisplay, { fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; - fn query_call_info(encoded_call: Bytes, len: u32) -> Result, RuntimeString>; - fn query_call_fee_details(encoded_call: Bytes, len: u32) -> Result, RuntimeString>; + + /// Query information of a dispatch class, weight, and fee of a given encoded `Call`. + /// + /// # Panics + /// + /// Panics if the `encoded_call` can not be decoded as runtime `Call`. + fn query_call_info( + encoded_call: Vec, + ) -> Result, RuntimeString>; + + /// Query fee details of a given encoded `Call`. + /// + /// # Panics + /// + /// Panics if the `encoded_call` can not be decoded as runtime `Call`. + fn query_call_fee_details( + encoded_call: Vec, + ) -> Result, RuntimeString>; } } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 206e3f0d76bdf..c1287fb5dbfa5 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -50,7 +50,6 @@ use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; -use sp_core::Bytes; use sp_runtime::{ traits::{ Convert, DispatchInfoOf, Dispatchable, One, PostDispatchInfoOf, SaturatedConversion, @@ -446,42 +445,42 @@ where } } - /// Query runtime dispatch info of a given encoded `Call`. + /// Query information of a dispatch class, weight, and fee of a given encoded `Call`. pub fn query_call_info( - encoded_call: Bytes, - len: u32, + encoded_call: Vec, ) -> Result>, RuntimeString> where - T::Call: Dispatchable + Decode, + T::Call: Dispatchable + Decode + GetDispatchInfo, { let call: T::Call = Decode::decode(&mut &*encoded_call) .map_err(|_| -> RuntimeString { "Failed to decode Call.".into() })?; let dispatch_info = ::get_dispatch_info(&call); let DispatchInfo { weight, class, .. } = dispatch_info; + let encoded_len = encoded_call.len() as u32; Ok(RuntimeDispatchInfo { weight, class, - partial_fee: Self::compute_fee(len, &dispatch_info, 0u32.into()), + partial_fee: Self::compute_fee(encoded_len, &dispatch_info, 0u32.into()), }) } - /// Query weight_to_fee of a given encoded `Call`. + /// Query fee details of a given encoded `Call`. pub fn query_call_fee_details( - encoded_call: Bytes, - len: u32, + encoded_call: Vec, ) -> Result>, RuntimeString> where - T::Call: Dispatchable + Decode, + T::Call: Dispatchable + Decode + GetDispatchInfo, { let call: T::Call = Decode::decode(&mut &*encoded_call) .map_err(|_| -> RuntimeString { "Failed to decode Call.".into() })?; let dispatch_info = ::get_dispatch_info(&call); let tip = 0u32.into(); + let encoded_len = encoded_call.len() as u32; - Ok(Self::compute_fee_details(len, &dispatch_info, tip)) + Ok(Self::compute_fee_details(encoded_len, &dispatch_info, tip)) } /// Compute the final fee value for a particular transaction. @@ -1259,12 +1258,12 @@ mod tests { >::put(Multiplier::saturating_from_rational(3, 2)); assert_eq!( - TransactionPayment::query_call_info(invalid_encoded_call.clone().into(), len), + TransactionPayment::query_call_info(invalid_encoded_call.clone().into()), Err(RuntimeString::Borrowed("Failed to decode Call.")), ); assert_eq!( - TransactionPayment::query_call_info(encoded_call.clone().into(), len), + TransactionPayment::query_call_info(encoded_call.clone().into()), Ok(RuntimeDispatchInfo { weight: info.weight, class: info.class, @@ -1275,12 +1274,12 @@ mod tests { ); assert_eq!( - TransactionPayment::query_call_fee_details(invalid_encoded_call.into(), len), + TransactionPayment::query_call_fee_details(invalid_encoded_call.into()), Err(RuntimeString::Borrowed("Failed to decode Call.")), ); assert_eq!( - TransactionPayment::query_call_fee_details(encoded_call.into(), len), + TransactionPayment::query_call_fee_details(encoded_call.into()), Ok(FeeDetails { inclusion_fee: Some(InclusionFee { base_fee: 5 * 2, /* base * weight_fee */ From 40a99282d83fc0060a4222ce11d1782a51d0c36c Mon Sep 17 00:00:00 2001 From: muharem Date: Thu, 4 Aug 2022 17:00:15 +0200 Subject: [PATCH 10/14] clean up toml and extra impl for dropped bound --- Cargo.lock | 1 - frame/transaction-payment/rpc/runtime-api/Cargo.toml | 2 -- test-utils/runtime/src/lib.rs | 6 ------ 3 files changed, 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 410e7ee1052c3..f296fdb1fc68e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6378,7 +6378,6 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "sp-api", - "sp-core", "sp-runtime", "sp-std", ] diff --git a/frame/transaction-payment/rpc/runtime-api/Cargo.toml b/frame/transaction-payment/rpc/runtime-api/Cargo.toml index 91823456cc4ba..9b1fd07407851 100644 --- a/frame/transaction-payment/rpc/runtime-api/Cargo.toml +++ b/frame/transaction-payment/rpc/runtime-api/Cargo.toml @@ -16,7 +16,6 @@ targets = ["x86_64-unknown-linux-gnu"] codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../transaction-payment" } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/api" } -sp-core = { version = "6.0.0", default-features = false, path = "../../../../primitives/core" } sp-runtime = { version = "6.0.0", default-features = false, path = "../../../../primitives/runtime" } sp-std = { version = "4.0.0", default-features = false, path = "../../../../primitives/std" } @@ -26,7 +25,6 @@ std = [ "codec/std", "pallet-transaction-payment/std", "sp-api/std", - "sp-core/std", "sp-runtime/std", "sp-std/std", ] diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 73b3bdf300883..e5cfae49da56d 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -241,12 +241,6 @@ impl sp_runtime::traits::Dispatchable for Extrinsic { } } -impl frame_support::weights::GetDispatchInfo for Extrinsic { - fn get_dispatch_info(&self) -> frame_support::weights::DispatchInfo { - panic!("This implementation should not be used for actual get_dispatch_info."); - } -} - impl Extrinsic { /// Convert `&self` into `&Transfer`. /// From a4bd61ac06f4f7601c159cd1177126d003a55971 Mon Sep 17 00:00:00 2001 From: muharem Date: Fri, 5 Aug 2022 17:19:32 +0200 Subject: [PATCH 11/14] panic if Call can not be decoded --- bin/node-template/runtime/src/lib.rs | 10 +-- bin/node/runtime/src/lib.rs | 9 +-- .../rpc/runtime-api/src/lib.rs | 10 +-- frame/transaction-payment/src/lib.rs | 64 ++++++++++--------- 4 files changed, 42 insertions(+), 51 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 07eba5cf31f7c..702ffc41c5ef7 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -16,7 +16,7 @@ use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, Verify}, transaction_validity::{TransactionSource, TransactionValidity}, - ApplyExtrinsicResult, MultiSignature, RuntimeString, + ApplyExtrinsicResult, MultiSignature, }; use sp_std::prelude::*; #[cfg(feature = "std")] @@ -458,14 +458,10 @@ impl_runtime_apis! { fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { TransactionPayment::query_fee_details(uxt, len) } - fn query_call_info( - encoded_call: Vec, - ) -> Result, RuntimeString> { + fn query_call_info(encoded_call: Vec) -> RuntimeDispatchInfo { TransactionPayment::query_call_info(encoded_call) } - fn query_call_fee_details( - encoded_call: Vec, - ) -> Result, RuntimeString> { + fn query_call_fee_details(encoded_call: Vec) -> FeeDetails { TransactionPayment::query_call_fee_details(encoded_call) } } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index e55606893348b..17ad0b94e6417 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -69,7 +69,6 @@ use sp_runtime::{ }, transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity}, ApplyExtrinsicResult, FixedPointNumber, FixedU128, Perbill, Percent, Permill, Perquintill, - RuntimeString, }; use sp_std::prelude::*; #[cfg(any(feature = "std", test))] @@ -1960,14 +1959,10 @@ impl_runtime_apis! { fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { TransactionPayment::query_fee_details(uxt, len) } - fn query_call_info( - encoded_call: Vec, - ) -> Result, RuntimeString> { + fn query_call_info(encoded_call: Vec) -> RuntimeDispatchInfo { TransactionPayment::query_call_info(encoded_call) } - fn query_call_fee_details( - encoded_call: Vec, - ) -> Result, RuntimeString> { + fn query_call_fee_details(encoded_call: Vec) -> FeeDetails { TransactionPayment::query_call_fee_details(encoded_call) } } diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index e4bb76d481547..0bf42fdd2695f 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -20,7 +20,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::Codec; -use sp_runtime::{traits::MaybeDisplay, RuntimeString}; +use sp_runtime::traits::MaybeDisplay; use sp_std::prelude::*; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; @@ -38,17 +38,13 @@ sp_api::decl_runtime_apis! { /// # Panics /// /// Panics if the `encoded_call` can not be decoded as runtime `Call`. - fn query_call_info( - encoded_call: Vec, - ) -> Result, RuntimeString>; + fn query_call_info(encoded_call: Vec) -> RuntimeDispatchInfo; /// Query fee details of a given encoded `Call`. /// /// # Panics /// /// Panics if the `encoded_call` can not be decoded as runtime `Call`. - fn query_call_fee_details( - encoded_call: Vec, - ) -> Result, RuntimeString>; + fn query_call_fee_details(encoded_call: Vec) -> FeeDetails; } } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index c1287fb5dbfa5..726b5104a91c2 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -58,7 +58,7 @@ use sp_runtime::{ transaction_validity::{ TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransaction, }, - FixedPointNumber, FixedPointOperand, FixedU128, Perquintill, RuntimeDebug, RuntimeString, + FixedPointNumber, FixedPointOperand, FixedU128, Perquintill, RuntimeDebug, }; use sp_std::prelude::*; @@ -446,41 +446,43 @@ where } /// Query information of a dispatch class, weight, and fee of a given encoded `Call`. - pub fn query_call_info( - encoded_call: Vec, - ) -> Result>, RuntimeString> + /// + /// # Panics + /// + /// Panics if the `encoded_call` can not be decoded as runtime `T::Call`. + pub fn query_call_info(encoded_call: Vec) -> RuntimeDispatchInfo> where T::Call: Dispatchable + Decode + GetDispatchInfo, { - let call: T::Call = Decode::decode(&mut &*encoded_call) - .map_err(|_| -> RuntimeString { "Failed to decode Call.".into() })?; + let call: T::Call = Decode::decode(&mut &*encoded_call).expect("Failed to decode Call."); let dispatch_info = ::get_dispatch_info(&call); let DispatchInfo { weight, class, .. } = dispatch_info; let encoded_len = encoded_call.len() as u32; - Ok(RuntimeDispatchInfo { + RuntimeDispatchInfo { weight, class, partial_fee: Self::compute_fee(encoded_len, &dispatch_info, 0u32.into()), - }) + } } /// Query fee details of a given encoded `Call`. - pub fn query_call_fee_details( - encoded_call: Vec, - ) -> Result>, RuntimeString> + /// + /// # Panics + /// + /// Panics if the `encoded_call` can not be decoded as runtime `T::Call`. + pub fn query_call_fee_details(encoded_call: Vec) -> FeeDetails> where T::Call: Dispatchable + Decode + GetDispatchInfo, { - let call: T::Call = Decode::decode(&mut &*encoded_call) - .map_err(|_| -> RuntimeString { "Failed to decode Call.".into() })?; + let call: T::Call = Decode::decode(&mut &*encoded_call).expect("Failed to decode Call."); let dispatch_info = ::get_dispatch_info(&call); let tip = 0u32.into(); let encoded_len = encoded_call.len() as u32; - Ok(Self::compute_fee_details(encoded_len, &dispatch_info, tip)) + Self::compute_fee_details(encoded_len, &dispatch_info, tip) } /// Compute the final fee value for a particular transaction. @@ -1251,36 +1253,24 @@ mod tests { let encoded_call = call.encode(); let len = encoded_call.len() as u32; - let invalid_encoded_call: Vec = vec![1, 2, 3]; - ExtBuilder::default().base_weight(5).weight_fee(2).build().execute_with(|| { // all fees should be x1.5 >::put(Multiplier::saturating_from_rational(3, 2)); - assert_eq!( - TransactionPayment::query_call_info(invalid_encoded_call.clone().into()), - Err(RuntimeString::Borrowed("Failed to decode Call.")), - ); - assert_eq!( TransactionPayment::query_call_info(encoded_call.clone().into()), - Ok(RuntimeDispatchInfo { + RuntimeDispatchInfo { weight: info.weight, class: info.class, partial_fee: 5 * 2 /* base * weight_fee */ + len as u64 /* len * 1 */ + info.weight.min(BlockWeights::get().max_block) as u64 * 2 * 3 / 2 /* weight */ - }), - ); - - assert_eq!( - TransactionPayment::query_call_fee_details(invalid_encoded_call.into()), - Err(RuntimeString::Borrowed("Failed to decode Call.")), + }, ); assert_eq!( TransactionPayment::query_call_fee_details(encoded_call.into()), - Ok(FeeDetails { + FeeDetails { inclusion_fee: Some(InclusionFee { base_fee: 5 * 2, /* base * weight_fee */ len_fee: len as u64, /* len * 1 */ @@ -1288,11 +1278,25 @@ mod tests { 2 * 3 / 2 /* weight * weight_fee * multipler */ }), tip: 0, - }), + }, ); }); } + #[test] + #[should_panic(expected = "Failed to decode Call.")] + fn query_call_info_panics() { + let invalid_encoded_call: Vec = vec![1, 2, 3]; + TransactionPayment::query_call_info(invalid_encoded_call); + } + + #[test] + #[should_panic(expected = "Failed to decode Call.")] + fn query_call_fee_details_panics() { + let invalid_encoded_call: Vec = vec![1, 2, 3]; + TransactionPayment::query_call_fee_details(invalid_encoded_call); + } + #[test] fn compute_fee_works_without_multiplier() { ExtBuilder::default() From 91dfcf2f460b6542c6055c919a997909b3cb5fa9 Mon Sep 17 00:00:00 2001 From: muharem Date: Wed, 10 Aug 2022 10:55:53 +0200 Subject: [PATCH 12/14] revert to 6d0ca79 --- Cargo.lock | 1 - bin/node-template/runtime/src/lib.rs | 32 ++++++++++--- bin/node/runtime/src/lib.rs | 23 +++++++-- frame/system/src/lib.rs | 6 +-- .../rpc/runtime-api/Cargo.toml | 2 - .../rpc/runtime-api/src/lib.rs | 24 ++++------ frame/transaction-payment/src/lib.rs | 48 ++++--------------- test-utils/runtime/src/lib.rs | 6 +++ 8 files changed, 70 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f296fdb1fc68e..50603c6235a23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6379,7 +6379,6 @@ dependencies = [ "parity-scale-codec", "sp-api", "sp-runtime", - "sp-std", ] [[package]] diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 702ffc41c5ef7..a943199ababf2 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -38,7 +38,7 @@ pub use frame_support::{ pub use frame_system::Call as SystemCall; pub use pallet_balances::Call as BalancesCall; pub use pallet_timestamp::Call as TimestampCall; -use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInfo}; +use pallet_transaction_payment::CurrencyAdapter; #[cfg(any(feature = "std", test))] pub use sp_runtime::BuildStorage; pub use sp_runtime::{Perbill, Permill}; @@ -452,17 +452,35 @@ impl_runtime_apis! { } impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { - fn query_info(uxt: ::Extrinsic, len: u32) -> RuntimeDispatchInfo { + fn query_info( + uxt: ::Extrinsic, + len: u32, + ) -> pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo { TransactionPayment::query_info(uxt, len) } - fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { + fn query_fee_details( + uxt: ::Extrinsic, + len: u32, + ) -> pallet_transaction_payment::FeeDetails { TransactionPayment::query_fee_details(uxt, len) } - fn query_call_info(encoded_call: Vec) -> RuntimeDispatchInfo { - TransactionPayment::query_call_info(encoded_call) + } + + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi< + Block, + Balance, + Call> for Runtime { + fn query_call_info( + call: Call, + len: u32, + ) -> pallet_transaction_payment::RuntimeDispatchInfo { + TransactionPayment::query_call_info(call, len) } - fn query_call_fee_details(encoded_call: Vec) -> FeeDetails { - TransactionPayment::query_call_fee_details(encoded_call) + fn query_call_fee_details( + call: Call, + len: u32 + ) -> pallet_transaction_payment::FeeDetails { + TransactionPayment::query_call_fee_details(call, len) } } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 17ad0b94e6417..b638840c65929 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1952,18 +1952,31 @@ impl_runtime_apis! { } } - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< + Block, + Balance, + > for Runtime { fn query_info(uxt: ::Extrinsic, len: u32) -> RuntimeDispatchInfo { TransactionPayment::query_info(uxt, len) } fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { TransactionPayment::query_fee_details(uxt, len) } - fn query_call_info(encoded_call: Vec) -> RuntimeDispatchInfo { - TransactionPayment::query_call_info(encoded_call) + } + + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi< + Block, + Balance, + Call, + > for Runtime { + fn query_call_info( + call: Call, + len: u32, + ) -> RuntimeDispatchInfo { + TransactionPayment::query_call_info(call, len) } - fn query_call_fee_details(encoded_call: Vec) -> FeeDetails { - TransactionPayment::query_call_fee_details(encoded_call) + fn query_call_fee_details(call: Call, len: u32) -> FeeDetails { + TransactionPayment::query_call_fee_details(call, len) } } diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 94605c2da59bd..391684436252d 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -91,8 +91,8 @@ use frame_support::{ OriginTrait, PalletInfo, SortedMembers, StoredMap, TypedGet, }, weights::{ - extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo, - PerDispatchClass, RuntimeDbWeight, Weight, + extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo, GetDispatchInfo, PerDispatchClass, + RuntimeDbWeight, Weight, }, Parameter, }; @@ -222,7 +222,7 @@ pub mod pallet { + OriginTrait; /// The aggregated `Call` type. - type Call: Dispatchable + Debug; + type Call: Dispatchable + Debug + GetDispatchInfo; /// Account index (aka nonce) type. This stores the number of previous transactions /// associated with a sender account. diff --git a/frame/transaction-payment/rpc/runtime-api/Cargo.toml b/frame/transaction-payment/rpc/runtime-api/Cargo.toml index 9b1fd07407851..5e1cb46753524 100644 --- a/frame/transaction-payment/rpc/runtime-api/Cargo.toml +++ b/frame/transaction-payment/rpc/runtime-api/Cargo.toml @@ -17,7 +17,6 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features = pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../transaction-payment" } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/api" } sp-runtime = { version = "6.0.0", default-features = false, path = "../../../../primitives/runtime" } -sp-std = { version = "4.0.0", default-features = false, path = "../../../../primitives/std" } [features] default = ["std"] @@ -26,5 +25,4 @@ std = [ "pallet-transaction-payment/std", "sp-api/std", "sp-runtime/std", - "sp-std/std", ] diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index 0bf42fdd2695f..6c125b155e5ce 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -21,30 +21,22 @@ use codec::Codec; use sp_runtime::traits::MaybeDisplay; -use sp_std::prelude::*; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; sp_api::decl_runtime_apis! { - pub trait TransactionPaymentApi - where + pub trait TransactionPaymentApi where Balance: Codec + MaybeDisplay, { fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; + } - /// Query information of a dispatch class, weight, and fee of a given encoded `Call`. - /// - /// # Panics - /// - /// Panics if the `encoded_call` can not be decoded as runtime `Call`. - fn query_call_info(encoded_call: Vec) -> RuntimeDispatchInfo; - - /// Query fee details of a given encoded `Call`. - /// - /// # Panics - /// - /// Panics if the `encoded_call` can not be decoded as runtime `Call`. - fn query_call_fee_details(encoded_call: Vec) -> FeeDetails; + pub trait TransactionPaymentCallApi where + Balance: Codec + MaybeDisplay, + Call: Codec, + { + fn query_call_info(call: Call, len: u32) -> RuntimeDispatchInfo; + fn query_call_fee_details(call: Call, len: u32) -> FeeDetails; } } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 726b5104a91c2..5241a85a1cac1 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -445,44 +445,30 @@ where } } - /// Query information of a dispatch class, weight, and fee of a given encoded `Call`. - /// - /// # Panics - /// - /// Panics if the `encoded_call` can not be decoded as runtime `T::Call`. - pub fn query_call_info(encoded_call: Vec) -> RuntimeDispatchInfo> + /// Query runtime dispatch info of a given `Call`. + pub fn query_call_info(call: T::Call, len: u32) -> RuntimeDispatchInfo> where - T::Call: Dispatchable + Decode + GetDispatchInfo, + T::Call: Dispatchable, { - let call: T::Call = Decode::decode(&mut &*encoded_call).expect("Failed to decode Call."); - let dispatch_info = ::get_dispatch_info(&call); let DispatchInfo { weight, class, .. } = dispatch_info; - let encoded_len = encoded_call.len() as u32; RuntimeDispatchInfo { weight, class, - partial_fee: Self::compute_fee(encoded_len, &dispatch_info, 0u32.into()), + partial_fee: Self::compute_fee(len, &dispatch_info, 0u32.into()), } } - /// Query fee details of a given encoded `Call`. - /// - /// # Panics - /// - /// Panics if the `encoded_call` can not be decoded as runtime `T::Call`. - pub fn query_call_fee_details(encoded_call: Vec) -> FeeDetails> + /// Query weight_to_fee of a given `Call`. + pub fn query_call_fee_details(call: T::Call, len: u32) -> FeeDetails> where - T::Call: Dispatchable + Decode + GetDispatchInfo, + T::Call: Dispatchable, { - let call: T::Call = Decode::decode(&mut &*encoded_call).expect("Failed to decode Call."); - let dispatch_info = ::get_dispatch_info(&call); let tip = 0u32.into(); - let encoded_len = encoded_call.len() as u32; - Self::compute_fee_details(encoded_len, &dispatch_info, tip) + Self::compute_fee_details(len, &dispatch_info, tip) } /// Compute the final fee value for a particular transaction. @@ -1258,7 +1244,7 @@ mod tests { >::put(Multiplier::saturating_from_rational(3, 2)); assert_eq!( - TransactionPayment::query_call_info(encoded_call.clone().into()), + TransactionPayment::query_call_info(call.clone(), len), RuntimeDispatchInfo { weight: info.weight, class: info.class, @@ -1269,7 +1255,7 @@ mod tests { ); assert_eq!( - TransactionPayment::query_call_fee_details(encoded_call.into()), + TransactionPayment::query_call_fee_details(call, len), FeeDetails { inclusion_fee: Some(InclusionFee { base_fee: 5 * 2, /* base * weight_fee */ @@ -1283,20 +1269,6 @@ mod tests { }); } - #[test] - #[should_panic(expected = "Failed to decode Call.")] - fn query_call_info_panics() { - let invalid_encoded_call: Vec = vec![1, 2, 3]; - TransactionPayment::query_call_info(invalid_encoded_call); - } - - #[test] - #[should_panic(expected = "Failed to decode Call.")] - fn query_call_fee_details_panics() { - let invalid_encoded_call: Vec = vec![1, 2, 3]; - TransactionPayment::query_call_fee_details(invalid_encoded_call); - } - #[test] fn compute_fee_works_without_multiplier() { ExtBuilder::default() diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index e5cfae49da56d..73b3bdf300883 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -241,6 +241,12 @@ impl sp_runtime::traits::Dispatchable for Extrinsic { } } +impl frame_support::weights::GetDispatchInfo for Extrinsic { + fn get_dispatch_info(&self) -> frame_support::weights::DispatchInfo { + panic!("This implementation should not be used for actual get_dispatch_info."); + } +} + impl Extrinsic { /// Convert `&self` into `&Transfer`. /// From 273cf09a7520a2044573e36872c81613ca6622ab Mon Sep 17 00:00:00 2001 From: muharem Date: Wed, 10 Aug 2022 11:15:51 +0200 Subject: [PATCH 13/14] fmt and docs --- bin/node-template/runtime/src/lib.rs | 9 ++++----- bin/node/runtime/src/lib.rs | 13 ++++--------- frame/system/src/lib.rs | 6 +++--- .../transaction-payment/rpc/runtime-api/src/lib.rs | 6 +++++- frame/transaction-payment/src/lib.rs | 8 ++++---- test-utils/runtime/src/lib.rs | 6 ------ 6 files changed, 20 insertions(+), 28 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index a943199ababf2..f0d87b2a48632 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -466,10 +466,9 @@ impl_runtime_apis! { } } - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi< - Block, - Balance, - Call> for Runtime { + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi + for Runtime + { fn query_call_info( call: Call, len: u32, @@ -478,7 +477,7 @@ impl_runtime_apis! { } fn query_call_fee_details( call: Call, - len: u32 + len: u32, ) -> pallet_transaction_payment::FeeDetails { TransactionPayment::query_call_fee_details(call, len) } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index b638840c65929..867b5e4abe13b 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1964,15 +1964,10 @@ impl_runtime_apis! { } } - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi< - Block, - Balance, - Call, - > for Runtime { - fn query_call_info( - call: Call, - len: u32, - ) -> RuntimeDispatchInfo { + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi + for Runtime + { + fn query_call_info(call: Call, len: u32) -> RuntimeDispatchInfo { TransactionPayment::query_call_info(call, len) } fn query_call_fee_details(call: Call, len: u32) -> FeeDetails { diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 391684436252d..aeb534d194263 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -91,8 +91,8 @@ use frame_support::{ OriginTrait, PalletInfo, SortedMembers, StoredMap, TypedGet, }, weights::{ - extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo, GetDispatchInfo, PerDispatchClass, - RuntimeDbWeight, Weight, + extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo, PerDispatchClass, RuntimeDbWeight, + Weight, }, Parameter, }; @@ -222,7 +222,7 @@ pub mod pallet { + OriginTrait; /// The aggregated `Call` type. - type Call: Dispatchable + Debug + GetDispatchInfo; + type Call: Dispatchable + Debug; /// Account index (aka nonce) type. This stores the number of previous transactions /// associated with a sender account. diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index 6c125b155e5ce..6944593daa57a 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -32,11 +32,15 @@ sp_api::decl_runtime_apis! { fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; } - pub trait TransactionPaymentCallApi where + pub trait TransactionPaymentCallApi + where Balance: Codec + MaybeDisplay, Call: Codec, { + /// Query information of a dispatch class, weight, and fee of a given encoded `Call`. fn query_call_info(call: Call, len: u32) -> RuntimeDispatchInfo; + + /// Query fee details of a given encoded `Call`. fn query_call_fee_details(call: Call, len: u32) -> FeeDetails; } } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 5241a85a1cac1..ff65c0d2735fd 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -445,10 +445,10 @@ where } } - /// Query runtime dispatch info of a given `Call`. + /// Query information of a dispatch class, weight, and fee of a given encoded `Call`. pub fn query_call_info(call: T::Call, len: u32) -> RuntimeDispatchInfo> where - T::Call: Dispatchable, + T::Call: Dispatchable + GetDispatchInfo, { let dispatch_info = ::get_dispatch_info(&call); let DispatchInfo { weight, class, .. } = dispatch_info; @@ -460,10 +460,10 @@ where } } - /// Query weight_to_fee of a given `Call`. + /// Query fee details of a given encoded `Call`. pub fn query_call_fee_details(call: T::Call, len: u32) -> FeeDetails> where - T::Call: Dispatchable, + T::Call: Dispatchable + GetDispatchInfo, { let dispatch_info = ::get_dispatch_info(&call); let tip = 0u32.into(); diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 73b3bdf300883..e5cfae49da56d 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -241,12 +241,6 @@ impl sp_runtime::traits::Dispatchable for Extrinsic { } } -impl frame_support::weights::GetDispatchInfo for Extrinsic { - fn get_dispatch_info(&self) -> frame_support::weights::DispatchInfo { - panic!("This implementation should not be used for actual get_dispatch_info."); - } -} - impl Extrinsic { /// Convert `&self` into `&Transfer`. /// From f8efc1c1dd7196967f5d920688b05531d8beb9de Mon Sep 17 00:00:00 2001 From: muharem Date: Wed, 10 Aug 2022 11:18:59 +0200 Subject: [PATCH 14/14] rustfmt --- frame/system/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index aeb534d194263..94605c2da59bd 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -91,8 +91,8 @@ use frame_support::{ OriginTrait, PalletInfo, SortedMembers, StoredMap, TypedGet, }, weights::{ - extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo, PerDispatchClass, RuntimeDbWeight, - Weight, + extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo, + PerDispatchClass, RuntimeDbWeight, Weight, }, Parameter, };