RPC to query transaction fee + weight + info (#3876)

* initial version for testing

* New version that compiles

* optional at block parameter

* Fix some more view grumbles.

* Update srml/transaction-payment/src/lib.rs
This commit is contained in:
Kian Paimani
2019-10-28 16:04:45 +01:00
committed by Gavin Wood
parent 9b1dd268bf
commit 7e87dfdc07
15 changed files with 359 additions and 21 deletions
@@ -0,0 +1,17 @@
[package]
name = "srml-transaction-payment-rpc"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
client = { package = "substrate-client", path = "../../../core/client" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
jsonrpc-core = "13.2.0"
jsonrpc-core-client = "13.2.0"
jsonrpc-derive = "13.2.0"
primitives = { package = "substrate-primitives", path = "../../../core/primitives" }
rpc-primitives = { package = "substrate-rpc-primitives", path = "../../../core/rpc/primitives" }
serde = { version = "1.0.101", features = ["derive"] }
sr-primitives = { path = "../../../core/sr-primitives" }
srml-transaction-payment-rpc-runtime-api = { path = "./runtime-api" }
@@ -0,0 +1,22 @@
[package]
name = "srml-transaction-payment-rpc-runtime-api"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
serde = { version = "1.0.101", optional = true, features = ["derive"] }
client = { package = "substrate-client", path = "../../../../core/client", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.6", default-features = false, features = ["derive"] }
rstd = { package = "sr-std", path = "../../../../core/sr-std", default-features = false }
sr-primitives = { path = "../../../../core/sr-primitives", default-features = false }
[features]
default = ["std"]
std = [
"serde",
"client/std",
"codec/std",
"rstd/std",
"sr-primitives/std",
]
@@ -0,0 +1,47 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Runtime API definition for transaction payment module.
#![cfg_attr(not(feature = "std"), no_std)]
use rstd::prelude::*;
use sr_primitives::weights::{Weight, DispatchClass};
use codec::{Encode, Codec, Decode};
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
/// Some information related to a dispatchable that can be queried from the runtime.
#[derive(Eq, PartialEq, Encode, Decode, Default)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct RuntimeDispatchInfo<Balance> {
/// Weight of this dispatch.
pub weight: Weight,
/// Class of this dispatch.
pub class: DispatchClass,
/// The partial inclusion fee of this dispatch. This does not include tip or anything else which
/// is dependent on the signature (aka. depends on a `SignedExtension`).
pub partial_fee: Balance,
}
client::decl_runtime_apis! {
pub trait TransactionPaymentApi<Balance, Extrinsic> where
Balance: Codec,
Extrinsic: Codec,
{
fn query_info(uxt: Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance>;
}
}
@@ -0,0 +1,108 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! RPC interface for the transaction payment module.
use std::sync::Arc;
use codec::{Codec, Decode};
use client::blockchain::HeaderBackend;
use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
use jsonrpc_derive::rpc;
use sr_primitives::{
generic::BlockId,
traits::{Block as BlockT, ProvideRuntimeApi},
};
use primitives::Bytes;
use srml_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
pub use srml_transaction_payment_rpc_runtime_api::TransactionPaymentApi as TransactionPaymentRuntimeApi;
pub use self::gen_client::Client as TransactionPaymentClient;
#[rpc]
pub trait TransactionPaymentApi<BlockHash, Balance> {
#[rpc(name = "payment_queryInfo")]
fn query_info(
&self,
encoded_xt: Bytes,
at: Option<BlockHash>
) -> Result<RuntimeDispatchInfo<Balance>>;
}
/// A struct that implements the [`TransactionPaymentApi`].
pub struct TransactionPayment<C, P> {
client: Arc<C>,
_marker: std::marker::PhantomData<P>,
}
impl<C, P> TransactionPayment<C, P> {
/// Create new `TransactionPayment` with the given reference to the client.
pub fn new(client: Arc<C>) -> Self {
TransactionPayment { client, _marker: Default::default() }
}
}
/// Error type of this RPC api.
pub enum Error {
/// The transaction was not decodable.
DecodeError,
/// The call to runtime failed.
RuntimeError,
}
impl From<Error> for i64 {
fn from(e: Error) -> i64 {
match e {
Error::RuntimeError => 1,
Error::DecodeError => 2,
}
}
}
impl<C, Block, Balance, Extrinsic> TransactionPaymentApi<<Block as BlockT>::Hash, Balance>
for TransactionPayment<C, (Block, Extrinsic)>
where
Block: BlockT,
C: Send + Sync + 'static,
C: ProvideRuntimeApi,
C: HeaderBackend<Block>,
C::Api: TransactionPaymentRuntimeApi<Block, Balance, Extrinsic>,
Balance: Codec,
Extrinsic: Codec + Send + Sync + 'static,
{
fn query_info(
&self,
encoded_xt: Bytes,
at: Option<<Block as BlockT>::Hash>
) -> Result<RuntimeDispatchInfo<Balance>> {
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.
self.client.info().best_hash
));
let encoded_len = encoded_xt.len() as u32;
let uxt: Extrinsic = Decode::decode(&mut &*encoded_xt).map_err(|e| RpcError {
code: ErrorCode::ServerError(Error::DecodeError.into()),
message: "Unable to query dispatch info.".into(),
data: Some(format!("{:?}", e).into()),
})?;
api.query_info(&at, uxt, encoded_len).map_err(|e| RpcError {
code: ErrorCode::ServerError(Error::RuntimeError.into()),
message: "Unable to query dispatch info.".into(),
data: Some(format!("{:?}", e).into()),
})
}
}