referrerpolicy=no-referrer-when-downgrade

staging_node_cli/
benchmarking.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Setup code for [`super::command`] which would otherwise bloat that module.
20//!
21//! Should only be used for benchmarking as it may break in other contexts.
22
23use crate::service::{create_extrinsic, FullClient};
24
25use polkadot_sdk::*;
26
27use kitchensink_runtime::{BalancesCall, SystemCall};
28use node_primitives::{AccountId, Balance};
29use sc_cli::Result;
30use sp_inherents::{InherentData, InherentDataProvider};
31use sp_keyring::Sr25519Keyring;
32use sp_runtime::OpaqueExtrinsic;
33
34use std::{sync::Arc, time::Duration};
35
36/// Generates `System::Remark` extrinsics for the benchmarks.
37///
38/// Note: Should only be used for benchmarking.
39pub struct RemarkBuilder {
40	client: Arc<FullClient>,
41}
42
43impl RemarkBuilder {
44	/// Creates a new [`Self`] from the given client.
45	pub fn new(client: Arc<FullClient>) -> Self {
46		Self { client }
47	}
48}
49
50impl frame_benchmarking_cli::ExtrinsicBuilder for RemarkBuilder {
51	fn pallet(&self) -> &str {
52		"system"
53	}
54
55	fn extrinsic(&self) -> &str {
56		"remark"
57	}
58
59	fn build(&self, nonce: u32) -> std::result::Result<OpaqueExtrinsic, &'static str> {
60		let acc = Sr25519Keyring::Bob.pair();
61		let extrinsic: OpaqueExtrinsic = create_extrinsic(
62			self.client.as_ref(),
63			acc,
64			SystemCall::remark { remark: vec![] },
65			Some(nonce),
66		)
67		.into();
68
69		Ok(extrinsic)
70	}
71}
72
73/// Generates `Balances::TransferKeepAlive` extrinsics for the benchmarks.
74///
75/// Note: Should only be used for benchmarking.
76pub struct TransferKeepAliveBuilder {
77	client: Arc<FullClient>,
78	dest: AccountId,
79	value: Balance,
80}
81
82impl TransferKeepAliveBuilder {
83	/// Creates a new [`Self`] from the given client.
84	pub fn new(client: Arc<FullClient>, dest: AccountId, value: Balance) -> Self {
85		Self { client, dest, value }
86	}
87}
88
89impl frame_benchmarking_cli::ExtrinsicBuilder for TransferKeepAliveBuilder {
90	fn pallet(&self) -> &str {
91		"balances"
92	}
93
94	fn extrinsic(&self) -> &str {
95		"transfer_keep_alive"
96	}
97
98	fn build(&self, nonce: u32) -> std::result::Result<OpaqueExtrinsic, &'static str> {
99		let acc = Sr25519Keyring::Bob.pair();
100		let extrinsic: OpaqueExtrinsic = create_extrinsic(
101			self.client.as_ref(),
102			acc,
103			BalancesCall::transfer_keep_alive {
104				dest: self.dest.clone().into(),
105				value: self.value.into(),
106			},
107			Some(nonce),
108		)
109		.into();
110
111		Ok(extrinsic)
112	}
113}
114
115/// Generates inherent data for the `benchmark overhead` command.
116pub fn inherent_benchmark_data() -> Result<InherentData> {
117	let mut inherent_data = InherentData::new();
118	let d = Duration::from_millis(0);
119	let timestamp = sp_timestamp::InherentDataProvider::new(d.into());
120
121	futures::executor::block_on(timestamp.provide_inherent_data(&mut inherent_data))
122		.map_err(|e| format!("creating inherent data: {:?}", e))?;
123	Ok(inherent_data)
124}