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

Commit 4f6f830

Browse files
kianenigmagavofyork
authored andcommitted
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
1 parent 33e5652 commit 4f6f830

File tree

15 files changed

+359
-21
lines changed

15 files changed

+359
-21
lines changed

Cargo.lock

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ members = [
8282
"srml/aura",
8383
"srml/balances",
8484
"srml/contracts",
85+
"srml/contracts/rpc",
8586
"srml/collective",
8687
"srml/democracy",
8788
"srml/elections",
@@ -109,6 +110,7 @@ members = [
109110
"srml/timestamp",
110111
"srml/treasury",
111112
"srml/transaction-payment",
113+
"srml/transaction-payment/rpc",
112114
"srml/utility",
113115
"node/cli",
114116
"node/executor",

core/sr-primitives/src/generic/unchecked_extrinsic.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use codec::{Decode, Encode, EncodeLike, Input, Error};
2323
use crate::{
2424
traits::{self, Member, MaybeDisplay, SignedExtension, Checkable, Extrinsic, IdentifyAccount},
2525
generic::CheckedExtrinsic, transaction_validity::{TransactionValidityError, InvalidTransaction},
26+
weights::{GetDispatchInfo, DispatchInfo},
2627
};
2728

2829
const TRANSACTION_VERSION: u8 = 4;
@@ -280,6 +281,17 @@ where
280281
}
281282
}
282283

284+
impl<Address, Call, Signature, Extra> GetDispatchInfo
285+
for UncheckedExtrinsic<Address, Call, Signature, Extra>
286+
where
287+
Call: GetDispatchInfo,
288+
Extra: SignedExtension,
289+
{
290+
fn get_dispatch_info(&self) -> DispatchInfo {
291+
self.function.get_dispatch_info()
292+
}
293+
}
294+
283295
#[cfg(test)]
284296
mod tests {
285297
use super::*;

core/sr-primitives/src/weights.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@
3535
//! Note that the decl_module macro _cannot_ enforce this and will simply fail if an invalid struct
3636
//! (something that does not implement `Weighable`) is passed in.
3737
38+
#[cfg(feature = "std")]
39+
use serde::{Serialize, Deserialize};
40+
use codec::{Encode, Decode};
3841
use arithmetic::traits::Bounded;
3942
use crate::RuntimeDebug;
4043

44+
/// Re-export priority as type
4145
pub use crate::transaction_validity::TransactionPriority;
4246

4347
/// Numeric range of a transaction weight.
@@ -58,10 +62,10 @@ pub trait ClassifyDispatch<T> {
5862
fn classify_dispatch(&self, target: T) -> DispatchClass;
5963
}
6064

61-
/// A generalized group of dispatch types. This is only distinguishing normal, user-triggered
62-
/// transactions (`Normal`) and anything beyond which serves a higher purpose to the system
63-
/// (`Operational`).
64-
#[derive(PartialEq, Eq, Clone, Copy, RuntimeDebug)]
65+
/// A generalized group of dispatch types. This is only distinguishing normal, user-triggered transactions
66+
/// (`Normal`) and anything beyond which serves a higher purpose to the system (`Operational`).
67+
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
68+
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug)]
6569
pub enum DispatchClass {
6670
/// A normal dispatch.
6771
Normal,

node/rpc/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ edition = "2018"
88
client = { package = "substrate-client", path = "../../core/client" }
99
jsonrpc-core = "13.2.0"
1010
node-primitives = { path = "../primitives" }
11+
node-runtime = { path = "../runtime" }
1112
sr-primitives = { path = "../../core/sr-primitives" }
1213
srml-contracts-rpc = { path = "../../srml/contracts/rpc/" }
14+
srml-transaction-payment-rpc = { path = "../../srml/transaction-payment/rpc/" }
1315
srml-system-rpc = { path = "../../srml/system/rpc/" }
1416
transaction_pool = { package = "substrate-transaction-pool", path = "../../core/transaction-pool" }

node/rpc/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use std::sync::Arc;
3333

3434
use node_primitives::{Block, AccountId, Index, Balance};
35+
use node_runtime::UncheckedExtrinsic;
3536
use sr_primitives::traits::ProvideRuntimeApi;
3637
use transaction_pool::txpool::{ChainApi, Pool};
3738

@@ -42,18 +43,23 @@ pub fn create<C, P, M>(client: Arc<C>, pool: Arc<Pool<P>>) -> jsonrpc_core::IoHa
4243
C: Send + Sync + 'static,
4344
C::Api: srml_system_rpc::AccountNonceApi<Block, AccountId, Index>,
4445
C::Api: srml_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance>,
46+
C::Api: srml_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance, UncheckedExtrinsic>,
4547
P: ChainApi + Sync + Send + 'static,
4648
M: jsonrpc_core::Metadata + Default,
4749
{
4850
use srml_system_rpc::{System, SystemApi};
4951
use srml_contracts_rpc::{Contracts, ContractsApi};
52+
use srml_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
5053

5154
let mut io = jsonrpc_core::IoHandler::default();
5255
io.extend_with(
5356
SystemApi::to_delegate(System::new(client.clone(), pool))
5457
);
5558
io.extend_with(
56-
ContractsApi::to_delegate(Contracts::new(client))
59+
ContractsApi::to_delegate(Contracts::new(client.clone()))
60+
);
61+
io.extend_with(
62+
TransactionPaymentApi::to_delegate(TransactionPayment::new(client))
5763
);
5864
io
5965
}

node/runtime/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ timestamp = { package = "srml-timestamp", path = "../../srml/timestamp", default
5252
treasury = { package = "srml-treasury", path = "../../srml/treasury", default-features = false }
5353
utility = { package = "srml-utility", path = "../../srml/utility", default-features = false }
5454
transaction-payment = { package = "srml-transaction-payment", path = "../../srml/transaction-payment", default-features = false }
55+
transaction-payment-rpc-runtime-api = { package = "srml-transaction-payment-rpc-runtime-api", path = "../../srml/transaction-payment/rpc/runtime-api/", default-features = false }
5556

5657
[build-dependencies]
5758
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.4", path = "../../core/utils/wasm-builder-runner" }
@@ -103,5 +104,6 @@ std = [
103104
"treasury/std",
104105
"utility/std",
105106
"transaction-payment/std",
107+
"transaction-payment-rpc-runtime-api/std",
106108
"version/std",
107109
]

node/runtime/src/lib.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ use version::NativeVersion;
5050
use primitives::OpaqueMetadata;
5151
use grandpa::{AuthorityId as GrandpaId, AuthorityWeight as GrandpaWeight};
5252
use im_online::sr25519::{AuthorityId as ImOnlineId};
53+
use transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
54+
use contracts_rpc_runtime_api::ContractExecResult;
5355
use system::offchain::TransactionSubmitter;
5456

5557
#[cfg(any(feature = "std", test))]
@@ -661,9 +663,7 @@ impl_runtime_apis! {
661663
value: Balance,
662664
gas_limit: u64,
663665
input_data: Vec<u8>,
664-
) -> contracts_rpc_runtime_api::ContractExecResult {
665-
use contracts_rpc_runtime_api::ContractExecResult;
666-
666+
) -> ContractExecResult {
667667
let exec_result = Contracts::bare_call(
668668
origin,
669669
dest.into(),
@@ -681,9 +681,20 @@ impl_runtime_apis! {
681681
}
682682
}
683683

684+
impl transaction_payment_rpc_runtime_api::TransactionPaymentApi<
685+
Block,
686+
Balance,
687+
UncheckedExtrinsic,
688+
> for Runtime {
689+
fn query_info(uxt: UncheckedExtrinsic, len: u32) -> RuntimeDispatchInfo<Balance> {
690+
TransactionPayment::query_info(uxt, len)
691+
}
692+
}
693+
684694
impl substrate_session::SessionKeys<Block> for Runtime {
685695
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
686-
let seed = seed.as_ref().map(|s| rstd::str::from_utf8(&s).expect("Seed is an utf8 string"));
696+
let seed = seed.as_ref().map(|s| rstd::str::from_utf8(&s)
697+
.expect("Seed is an utf8 string"));
687698
SessionKeys::generate(seed)
688699
}
689700
}

srml/executive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ mod tests {
571571
assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok());
572572

573573
// default weight for `TestXt` == encoded length.
574-
assert_eq!(<system::Module<Runtime>>::all_extrinsics_weight(), (3 * len).into());
574+
assert_eq!(<system::Module<Runtime>>::all_extrinsics_weight(), (3 * len) as u32);
575575
assert_eq!(<system::Module<Runtime>>::all_extrinsics_len(), 3 * len);
576576

577577
let _ = <system::Module<Runtime>>::finalize();

srml/transaction-payment/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rstd = { package = "sr-std", path = "../../core/sr-std", default-features = fals
1010
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
1111
support = { package = "srml-support", path = "../support", default-features = false }
1212
system = { package = "srml-system", path = "../system", default-features = false }
13+
transaction-payment-rpc-runtime-api = { package = "srml-transaction-payment-rpc-runtime-api", path = "./rpc/runtime-api", default-features = false }
1314

1415
[dev-dependencies]
1516
runtime-io = { package = "sr-io", path = "../../core/sr-io" }
@@ -24,4 +25,5 @@ std = [
2425
"sr-primitives/std",
2526
"support/std",
2627
"system/std",
28+
"transaction-payment-rpc-runtime-api/std"
2729
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "srml-transaction-payment-rpc"
3+
version = "2.0.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
client = { package = "substrate-client", path = "../../../core/client" }
9+
codec = { package = "parity-scale-codec", version = "1.0.0" }
10+
jsonrpc-core = "13.2.0"
11+
jsonrpc-core-client = "13.2.0"
12+
jsonrpc-derive = "13.2.0"
13+
primitives = { package = "substrate-primitives", path = "../../../core/primitives" }
14+
rpc-primitives = { package = "substrate-rpc-primitives", path = "../../../core/rpc/primitives" }
15+
serde = { version = "1.0.101", features = ["derive"] }
16+
sr-primitives = { path = "../../../core/sr-primitives" }
17+
srml-transaction-payment-rpc-runtime-api = { path = "./runtime-api" }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "srml-transaction-payment-rpc-runtime-api"
3+
version = "2.0.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
serde = { version = "1.0.101", optional = true, features = ["derive"] }
9+
client = { package = "substrate-client", path = "../../../../core/client", default-features = false }
10+
codec = { package = "parity-scale-codec", version = "1.0.6", default-features = false, features = ["derive"] }
11+
rstd = { package = "sr-std", path = "../../../../core/sr-std", default-features = false }
12+
sr-primitives = { path = "../../../../core/sr-primitives", default-features = false }
13+
14+
[features]
15+
default = ["std"]
16+
std = [
17+
"serde",
18+
"client/std",
19+
"codec/std",
20+
"rstd/std",
21+
"sr-primitives/std",
22+
]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Runtime API definition for transaction payment module.
18+
19+
#![cfg_attr(not(feature = "std"), no_std)]
20+
21+
use rstd::prelude::*;
22+
use sr_primitives::weights::{Weight, DispatchClass};
23+
use codec::{Encode, Codec, Decode};
24+
#[cfg(feature = "std")]
25+
use serde::{Serialize, Deserialize};
26+
27+
/// Some information related to a dispatchable that can be queried from the runtime.
28+
#[derive(Eq, PartialEq, Encode, Decode, Default)]
29+
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
30+
pub struct RuntimeDispatchInfo<Balance> {
31+
/// Weight of this dispatch.
32+
pub weight: Weight,
33+
/// Class of this dispatch.
34+
pub class: DispatchClass,
35+
/// The partial inclusion fee of this dispatch. This does not include tip or anything else which
36+
/// is dependent on the signature (aka. depends on a `SignedExtension`).
37+
pub partial_fee: Balance,
38+
}
39+
40+
client::decl_runtime_apis! {
41+
pub trait TransactionPaymentApi<Balance, Extrinsic> where
42+
Balance: Codec,
43+
Extrinsic: Codec,
44+
{
45+
fn query_info(uxt: Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance>;
46+
}
47+
}

0 commit comments

Comments
 (0)