From c8a0d2d12eb989a210ed3833010e464fcbe76852 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 9 Jan 2023 15:54:44 -0700 Subject: [PATCH 1/4] Add WeightToFee and LengthToFee impls to RPC --- bin/node-template/runtime/src/lib.rs | 12 ++++ bin/node/runtime/src/lib.rs | 12 ++++ .../rpc/runtime-api/src/lib.rs | 12 +++- frame/transaction-payment/rpc/src/lib.rs | 68 +++++++++++++++++++ frame/transaction-payment/src/lib.rs | 4 +- 5 files changed, 104 insertions(+), 4 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index f4372af525da5..baba5d9b05e59 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -477,6 +477,12 @@ impl_runtime_apis! { ) -> pallet_transaction_payment::FeeDetails { TransactionPayment::query_fee_details(uxt, len) } + fn query_weight_to_fee(weight: Weight) -> Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> Balance { + TransactionPayment::length_to_fee(length) + } } impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi @@ -494,6 +500,12 @@ impl_runtime_apis! { ) -> pallet_transaction_payment::FeeDetails { TransactionPayment::query_call_fee_details(call, len) } + fn query_weight_to_fee(weight: Weight) -> Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> Balance { + TransactionPayment::length_to_fee(length) + } } #[cfg(feature = "runtime-benchmarks")] diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index a9e93a16f0713..0250e7126ce7a 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -2119,6 +2119,12 @@ impl_runtime_apis! { fn query_fee_details(uxt: ::Extrinsic, len: u32) -> FeeDetails { TransactionPayment::query_fee_details(uxt, len) } + fn query_weight_to_fee(weight: Weight) -> Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> Balance { + TransactionPayment::length_to_fee(length) + } } impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi @@ -2130,6 +2136,12 @@ impl_runtime_apis! { fn query_call_fee_details(call: RuntimeCall, len: u32) -> FeeDetails { TransactionPayment::query_call_fee_details(call, len) } + fn query_weight_to_fee(weight: Weight) -> Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> Balance { + TransactionPayment::length_to_fee(length) + } } 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 10fd2a9e61fc1..5ea6b7c19d038 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -25,7 +25,7 @@ use sp_runtime::traits::MaybeDisplay; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; sp_api::decl_runtime_apis! { - #[api_version(2)] + #[api_version(3)] pub trait TransactionPaymentApi where Balance: Codec + MaybeDisplay, { @@ -33,9 +33,11 @@ sp_api::decl_runtime_apis! { fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; + fn query_weight_to_fee(weight: sp_weights::Weight) -> Balance; + fn query_length_to_fee(length: u32) -> Balance; } - #[api_version(2)] + #[api_version(3)] pub trait TransactionPaymentCallApi where Balance: Codec + MaybeDisplay, @@ -46,5 +48,11 @@ sp_api::decl_runtime_apis! { /// Query fee details of a given encoded `Call`. fn query_call_fee_details(call: Call, len: u32) -> FeeDetails; + + /// Query the output of the current WeightToFee given some input + fn query_weight_to_fee(weight: sp_weights::Weight) -> Balance; + + /// Query the output of the current LengthToFee given some input + fn query_length_to_fee(length: u32) -> Balance; } } diff --git a/frame/transaction-payment/rpc/src/lib.rs b/frame/transaction-payment/rpc/src/lib.rs index 19007d37963ec..b8a0fe92c5538 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_weightToFee")] + fn query_weight_to_fee( + &self, + weight: sp_weights::Weight, + at: Option, + ) -> RpcResult; + + #[method(name = "payment_lengthToFee")] + fn query_length_to_fee( + &self, + length: u32, + at: Option, + ) -> RpcResult; } /// Provides RPC methods to query a dispatchable's class, weight and fee. @@ -194,4 +208,58 @@ where tip: Default::default(), }) } + + fn query_weight_to_fee( + &self, + weight: sp_weights::Weight, + at: Option, + ) -> RpcResult { + let api = self.client.runtime_api(); + let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + + let fee = api.query_weight_to_fee(&at, weight).map_err(|e| { + CallError::Custom(ErrorObject::owned( + Error::RuntimeError.into(), + "Unable to query weight_to_fee.", + Some(e.to_string()), + )) + })?; + + let fee = fee.try_into().map_err(|_| { + CallError::Custom(ErrorObject::owned( + Error::RuntimeError.into(), + "Balance not convertible", + Some("Balance could not be converted to NumberOrHex"), + )) + })?; + + Ok(fee) + } + + fn query_length_to_fee( + &self, + length: u32, + at: Option, + ) -> RpcResult { + let api = self.client.runtime_api(); + let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + + let fee = api.query_length_to_fee(&at, length).map_err(|e| { + CallError::Custom(ErrorObject::owned( + Error::RuntimeError.into(), + "Unable to query length_to_fee.", + Some(e.to_string()), + )) + })?; + + let fee = fee.try_into().map_err(|_| { + CallError::Custom(ErrorObject::owned( + Error::RuntimeError.into(), + "Balance not convertible", + Some("Balance could not be converted to NumberOrHex"), + )) + })?; + + Ok(fee) + } } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index ce747fa6bd85c..86c64370c9ed6 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -607,11 +607,11 @@ where } } - fn length_to_fee(length: u32) -> BalanceOf { + pub fn length_to_fee(length: u32) -> BalanceOf { T::LengthToFee::weight_to_fee(&Weight::from_ref_time(length as u64)) } - fn weight_to_fee(weight: Weight) -> BalanceOf { + pub fn weight_to_fee(weight: Weight) -> BalanceOf { // cap the weight to the maximum defined in runtime, otherwise it will be the // `Bounded` maximum of its data type, which is not desired. let capped_weight = weight.min(T::BlockWeights::get().max_block); From ee6f3996e63e1d59bdcda7e524219781ab18f6fb Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Tue, 10 Jan 2023 09:36:00 -0700 Subject: [PATCH 2/4] Remove RPC additions --- frame/transaction-payment/rpc/src/lib.rs | 68 ------------------------ 1 file changed, 68 deletions(-) diff --git a/frame/transaction-payment/rpc/src/lib.rs b/frame/transaction-payment/rpc/src/lib.rs index b8a0fe92c5538..19007d37963ec 100644 --- a/frame/transaction-payment/rpc/src/lib.rs +++ b/frame/transaction-payment/rpc/src/lib.rs @@ -48,20 +48,6 @@ pub trait TransactionPaymentApi { encoded_xt: Bytes, at: Option, ) -> RpcResult>; - - #[method(name = "payment_weightToFee")] - fn query_weight_to_fee( - &self, - weight: sp_weights::Weight, - at: Option, - ) -> RpcResult; - - #[method(name = "payment_lengthToFee")] - fn query_length_to_fee( - &self, - length: u32, - at: Option, - ) -> RpcResult; } /// Provides RPC methods to query a dispatchable's class, weight and fee. @@ -208,58 +194,4 @@ where tip: Default::default(), }) } - - fn query_weight_to_fee( - &self, - weight: sp_weights::Weight, - at: Option, - ) -> RpcResult { - let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); - - let fee = api.query_weight_to_fee(&at, weight).map_err(|e| { - CallError::Custom(ErrorObject::owned( - Error::RuntimeError.into(), - "Unable to query weight_to_fee.", - Some(e.to_string()), - )) - })?; - - let fee = fee.try_into().map_err(|_| { - CallError::Custom(ErrorObject::owned( - Error::RuntimeError.into(), - "Balance not convertible", - Some("Balance could not be converted to NumberOrHex"), - )) - })?; - - Ok(fee) - } - - fn query_length_to_fee( - &self, - length: u32, - at: Option, - ) -> RpcResult { - let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); - - let fee = api.query_length_to_fee(&at, length).map_err(|e| { - CallError::Custom(ErrorObject::owned( - Error::RuntimeError.into(), - "Unable to query length_to_fee.", - Some(e.to_string()), - )) - })?; - - let fee = fee.try_into().map_err(|_| { - CallError::Custom(ErrorObject::owned( - Error::RuntimeError.into(), - "Balance not convertible", - Some("Balance could not be converted to NumberOrHex"), - )) - })?; - - Ok(fee) - } } From 0ae8033e08ce3a31eebd1c959872c60760fc439d Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Tue, 10 Jan 2023 13:18:23 -0700 Subject: [PATCH 3/4] Update frame/transaction-payment/rpc/runtime-api/src/lib.rs Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> --- frame/transaction-payment/rpc/runtime-api/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/frame/transaction-payment/rpc/runtime-api/src/lib.rs index 5ea6b7c19d038..760f9e3693417 100644 --- a/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -49,10 +49,10 @@ sp_api::decl_runtime_apis! { /// Query fee details of a given encoded `Call`. fn query_call_fee_details(call: Call, len: u32) -> FeeDetails; - /// Query the output of the current WeightToFee given some input + /// Query the output of the current `WeightToFee` given some input. fn query_weight_to_fee(weight: sp_weights::Weight) -> Balance; - /// Query the output of the current LengthToFee given some input + /// Query the output of the current `LengthToFee` given some input. fn query_length_to_fee(length: u32) -> Balance; } } From 9767e6be8c5e88c3a29db23134115036c88da28a Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Wed, 11 Jan 2023 14:28:50 -0700 Subject: [PATCH 4/4] Add comments to length_to_fee and weight_to_fee --- frame/transaction-payment/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 86c64370c9ed6..09e1e1fd32c10 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -607,10 +607,13 @@ where } } + /// Compute the length portion of a fee by invoking the configured `LengthToFee` impl. pub fn length_to_fee(length: u32) -> BalanceOf { T::LengthToFee::weight_to_fee(&Weight::from_ref_time(length as u64)) } + /// Compute the unadjusted portion of the weight fee by invoking the configured `WeightToFee` + /// impl. Note that the input `weight` is capped by the maximum block weight before computation. pub fn weight_to_fee(weight: Weight) -> BalanceOf { // cap the weight to the maximum defined in runtime, otherwise it will be the // `Bounded` maximum of its data type, which is not desired.