Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add WeightToFee and LengthToFee impls to transaction-payment Runtime API #13110

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ impl_runtime_apis! {
) -> pallet_transaction_payment::FeeDetails<Balance> {
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<Block, Balance, RuntimeCall>
Expand All @@ -494,6 +500,12 @@ impl_runtime_apis! {
) -> pallet_transaction_payment::FeeDetails<Balance> {
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")]
Expand Down
12 changes: 12 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,12 @@ impl_runtime_apis! {
fn query_fee_details(uxt: <Block as BlockT>::Extrinsic, len: u32) -> FeeDetails<Balance> {
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<Block, Balance, RuntimeCall>
Expand All @@ -2130,6 +2136,12 @@ impl_runtime_apis! {
fn query_call_fee_details(call: RuntimeCall, len: u32) -> FeeDetails<Balance> {
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<
Expand Down
12 changes: 10 additions & 2 deletions frame/transaction-payment/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ 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<Balance> where
Balance: Codec + MaybeDisplay,
{
#[changed_in(2)]
fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance, sp_weights::OldWeight>;
fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance>;
fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails<Balance>;
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<Balance, Call>
where
Balance: Codec + MaybeDisplay,
Expand All @@ -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<Balance>;

/// 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;
notlesh marked this conversation as resolved.
Show resolved Hide resolved
}
}
68 changes: 68 additions & 0 deletions frame/transaction-payment/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ pub trait TransactionPaymentApi<BlockHash, ResponseType> {
encoded_xt: Bytes,
at: Option<BlockHash>,
) -> RpcResult<FeeDetails<NumberOrHex>>;

#[method(name = "payment_weightToFee")]
fn query_weight_to_fee(
&self,
weight: sp_weights::Weight,
at: Option<BlockHash>,
) -> RpcResult<NumberOrHex>;

#[method(name = "payment_lengthToFee")]
fn query_length_to_fee(
&self,
length: u32,
at: Option<BlockHash>,
) -> RpcResult<NumberOrHex>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New RPCs are discouraged. Having the runtime api is enough. People can call the state_call rpc to call the api.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, RPC seems unnecessary. Removed in ee6f399.

}

/// Provides RPC methods to query a dispatchable's class, weight and fee.
Expand Down Expand Up @@ -194,4 +208,58 @@ where
tip: Default::default(),
})
}

fn query_weight_to_fee(
&self,
weight: sp_weights::Weight,
at: Option<Block::Hash>,
) -> RpcResult<NumberOrHex> {
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<Block::Hash>,
) -> RpcResult<NumberOrHex> {
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)
}
}
4 changes: 2 additions & 2 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ where
}
}

fn length_to_fee(length: u32) -> BalanceOf<T> {
pub fn length_to_fee(length: u32) -> BalanceOf<T> {
T::LengthToFee::weight_to_fee(&Weight::from_ref_time(length as u64))
}

fn weight_to_fee(weight: Weight) -> BalanceOf<T> {
pub fn weight_to_fee(weight: Weight) -> BalanceOf<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add some docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 9767e6b

// 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);
Expand Down