Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Jan 20, 2023
1 parent d8fd0cb commit 18dd63e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ sp-npos-elections = { git = "https://github.com/PolymeshAssociation/substrate",
sp-offchain = { git = "https://github.com/PolymeshAssociation/substrate", branch = "polymesh-monthly-2022-11" }
sp-rpc = { git = "https://github.com/PolymeshAssociation/substrate", branch = "polymesh-monthly-2022-11" }
sp-runtime = { git = "https://github.com/PolymeshAssociation/substrate", branch = "polymesh-monthly-2022-11" }
sp-weights = { git = "https://github.com/PolymeshAssociation/substrate", branch = "polymesh-monthly-2022-11" }
sp-runtime-interface = { git = "https://github.com/PolymeshAssociation/substrate", branch = "polymesh-monthly-2022-11" }
sp-session = { git = "https://github.com/PolymeshAssociation/substrate", branch = "polymesh-monthly-2022-11" }
sp-staking = { git = "https://github.com/PolymeshAssociation/substrate", branch = "polymesh-monthly-2022-11" }
Expand Down
13 changes: 7 additions & 6 deletions pallets/transaction-payment/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
use sp_runtime::traits::{AtLeast32BitUnsigned, Zero};
use sp_std::prelude::*;

use frame_support::{dispatch::DispatchClass, weights::Weight};
use frame_support::dispatch::DispatchClass;

/// The base fee and adjusted weight and length fees constitute the _inclusion fee_.
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -95,14 +95,14 @@ impl<Balance: AtLeast32BitUnsigned + Copy> FeeDetails<Balance> {
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "std",
serde(bound(serialize = "Balance: std::fmt::Display"))
feature = "std",
serde(bound(serialize = "Balance: std::fmt::Display, Weight: Serialize"))
)]
#[cfg_attr(
feature = "std",
serde(bound(deserialize = "Balance: std::str::FromStr"))
feature = "std",
serde(bound(deserialize = "Balance: std::str::FromStr, Weight: Deserialize<'de>"))
)]
pub struct RuntimeDispatchInfo<Balance> {
pub struct RuntimeDispatchInfo<Balance, Weight = frame_support::weights::Weight> {
/// Weight of this dispatch.
pub weight: Weight,
/// Class of this dispatch.
Expand Down Expand Up @@ -138,6 +138,7 @@ mod serde_balance {
#[cfg(test)]
mod tests {
use super::*;
use frame_support::weights::Weight;

#[test]
fn should_serialize_and_deserialize_properly_with_string() {
Expand Down
2 changes: 2 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sp-core = { version = "6.0.0", default_features = false }
sp-rpc = { version = "6.0.0" }
sp-runtime = { version = "6.0.0", default_features = false }
sp-std = {version = "4.0.0", default_features = false }
sp-weights = { version = "4.0.0", default_features = false }
frame-support = { version = "4.0.0-dev", default-features = false }
frame-system = { version = "4.0.0-dev", default-features = false }

Expand Down Expand Up @@ -49,4 +50,5 @@ std = [
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"sp-weights/std",
]
2 changes: 2 additions & 0 deletions rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
sp-api = { version = "4.0.0-dev", default-features = false }
sp-runtime = { version = "6.0.0", default-features = false }
sp-std = { version = "4.0.0", default_features = false }
sp-weights = { version = "4.0.0", default_features = false }
frame-support = { version = "4.0.0-dev", default-features = false }
frame-system = { version = "4.0.0-dev", default-features = false }

Expand Down Expand Up @@ -45,4 +46,5 @@ std = [
"sp-api/std",
"sp-runtime/std",
"sp-std/std",
"sp-weights/std",
]
4 changes: 4 additions & 0 deletions rpc/runtime-api/src/transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchIn
use polymesh_primitives::Balance;

sp_api::decl_runtime_apis! {
#[api_version(2)]
pub trait TransactionPaymentApi
{
#[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>;
}

#[api_version(2)]
pub trait TransactionPaymentCallApi<Call>
where
Call: Codec,
Expand Down
43 changes: 35 additions & 8 deletions rpc/src/transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use node_rpc_runtime_api::transaction_payment::{
TransactionPaymentApi as TransactionPaymentRuntimeApi,
};
use polymesh_primitives::Balance;
use sp_api::ProvideRuntimeApi;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_rpc::number::NumberOrHex;
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<C, P> TransactionPayment<C, P> {
}
}

impl<C, Block> TransactionPaymentApiServer<<Block as BlockT>::Hash, RuntimeDispatchInfo<Balance>>
impl<C, Block> TransactionPaymentApiServer<<Block as BlockT>::Hash, RuntimeDispatchInfo<Balance, sp_weights::OldWeight>>
for TransactionPayment<C, Block>
where
Block: BlockT,
Expand All @@ -77,7 +77,7 @@ where
&self,
encoded_xt: Bytes,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<RuntimeDispatchInfo<Balance>> {
) -> RpcResult<RuntimeDispatchInfo<Balance, sp_weights::OldWeight>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| {
// If the block hash is not supplied assume the best block.
Expand All @@ -93,14 +93,41 @@ where
Some(format!("{:?}", e)),
))
})?;
api.query_info(&at, uxt, encoded_len).map_err(|e| {

fn map_err(error: impl ToString, desc: &'static str) -> CallError {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to query dispatch info.",
Some(e.to_string()),
desc,
Some(error.to_string()),
))
.into()
})
}

let api_version = api
.api_version::<dyn TransactionPaymentRuntimeApi<Block>>(&at)
.map_err(|e| map_err(e, "Failed to get transaction payment runtime api version"))?
.ok_or_else(|| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Transaction payment runtime api wasn't found in the runtime",
None::<String>,
))
})?;

if api_version < 2 {
#[allow(deprecated)]
api.query_info_before_version_2(&at, uxt, encoded_len)
.map_err(|e| map_err(e, "Unable to query dispatch info.").into())
} else {
let res = api
.query_info(&at, uxt, encoded_len)
.map_err(|e| map_err(e, "Unable to query dispatch info."))?;

Ok(RuntimeDispatchInfo {
weight: sp_weights::OldWeight(res.weight.ref_time()),
class: res.class,
partial_fee: res.partial_fee,
})
}
}

fn query_fee_details(
Expand Down

0 comments on commit 18dd63e

Please sign in to comment.