diff --git a/Cargo.lock b/Cargo.lock index cce52a2469..26f4771264 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4322,6 +4322,7 @@ dependencies = [ "sp-rpc", "sp-runtime", "sp-std", + "sp-weights", ] [[package]] @@ -4342,6 +4343,7 @@ dependencies = [ "sp-api", "sp-runtime", "sp-std", + "sp-weights", ] [[package]] @@ -10619,7 +10621,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "digest 0.10.5", "rand 0.8.5", "static_assertions", diff --git a/Cargo.toml b/Cargo.toml index 57199d9509..dc1788a5d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/pallets/transaction-payment/src/types.rs b/pallets/transaction-payment/src/types.rs index 5cf04c0ba4..06be0cb9d0 100644 --- a/pallets/transaction-payment/src/types.rs +++ b/pallets/transaction-payment/src/types.rs @@ -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)] @@ -95,14 +95,14 @@ impl FeeDetails { #[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 { +pub struct RuntimeDispatchInfo { /// Weight of this dispatch. pub weight: Weight, /// Class of this dispatch. @@ -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() { diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 97d1f4f379..d391dfda93 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -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 } @@ -49,4 +50,5 @@ std = [ "sp-core/std", "sp-runtime/std", "sp-std/std", + "sp-weights/std", ] diff --git a/rpc/runtime-api/Cargo.toml b/rpc/runtime-api/Cargo.toml index 3f841a525b..37850c7ca9 100644 --- a/rpc/runtime-api/Cargo.toml +++ b/rpc/runtime-api/Cargo.toml @@ -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 } @@ -45,4 +46,5 @@ std = [ "sp-api/std", "sp-runtime/std", "sp-std/std", + "sp-weights/std", ] diff --git a/rpc/runtime-api/src/transaction_payment.rs b/rpc/runtime-api/src/transaction_payment.rs index a326857890..a14fe1a244 100644 --- a/rpc/runtime-api/src/transaction_payment.rs +++ b/rpc/runtime-api/src/transaction_payment.rs @@ -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; fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails; } + #[api_version(2)] pub trait TransactionPaymentCallApi where Call: Codec, diff --git a/rpc/src/transaction_payment.rs b/rpc/src/transaction_payment.rs index bb02bfb8cc..6d15672920 100644 --- a/rpc/src/transaction_payment.rs +++ b/rpc/src/transaction_payment.rs @@ -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; @@ -66,7 +66,7 @@ impl TransactionPayment { } } -impl TransactionPaymentApiServer<::Hash, RuntimeDispatchInfo> +impl TransactionPaymentApiServer<::Hash, RuntimeDispatchInfo> for TransactionPayment where Block: BlockT, @@ -77,7 +77,7 @@ where &self, encoded_xt: Bytes, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult> { 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. @@ -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::>(&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::, + )) + })?; + + 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(