|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2022 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 | +//! Contains code to setup the command invocations in [`super::command`] which would |
| 20 | +//! otherwise bloat that module. |
| 21 | +
|
| 22 | +use crate::service::FullClient; |
| 23 | + |
| 24 | +use contracts_node_runtime as runtime; |
| 25 | +use runtime::SystemCall; |
| 26 | +use sc_cli::Result; |
| 27 | +use sc_client_api::BlockBackend; |
| 28 | +use sp_core::{Encode, Pair}; |
| 29 | +use sp_inherents::{InherentData, InherentDataProvider}; |
| 30 | +use sp_keyring::Sr25519Keyring; |
| 31 | +use sp_runtime::{OpaqueExtrinsic, SaturatedConversion}; |
| 32 | + |
| 33 | +use std::{sync::Arc, time::Duration}; |
| 34 | + |
| 35 | +/// Generates extrinsics for the `benchmark overhead` command. |
| 36 | +/// |
| 37 | +/// Note: Should only be used for benchmarking. |
| 38 | +pub struct BenchmarkExtrinsicBuilder { |
| 39 | + client: Arc<FullClient>, |
| 40 | +} |
| 41 | + |
| 42 | +impl BenchmarkExtrinsicBuilder { |
| 43 | + /// Creates a new [`Self`] from the given client. |
| 44 | + pub fn new(client: Arc<FullClient>) -> Self { |
| 45 | + Self { client } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl frame_benchmarking_cli::ExtrinsicBuilder for BenchmarkExtrinsicBuilder { |
| 50 | + fn remark(&self, nonce: u32) -> std::result::Result<OpaqueExtrinsic, &'static str> { |
| 51 | + let acc = Sr25519Keyring::Bob.pair(); |
| 52 | + let extrinsic: OpaqueExtrinsic = create_benchmark_extrinsic( |
| 53 | + self.client.as_ref(), |
| 54 | + acc, |
| 55 | + SystemCall::remark { remark: vec![] }.into(), |
| 56 | + nonce, |
| 57 | + ) |
| 58 | + .into(); |
| 59 | + |
| 60 | + Ok(extrinsic) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Create a transaction using the given `call`. |
| 65 | +/// |
| 66 | +/// Note: Should only be used for benchmarking. |
| 67 | +pub fn create_benchmark_extrinsic( |
| 68 | + client: &FullClient, |
| 69 | + sender: sp_core::sr25519::Pair, |
| 70 | + call: runtime::Call, |
| 71 | + nonce: u32, |
| 72 | +) -> runtime::UncheckedExtrinsic { |
| 73 | + let genesis_hash = client.block_hash(0).ok().flatten().expect("Genesis block exists; qed"); |
| 74 | + let best_hash = client.chain_info().best_hash; |
| 75 | + let best_block = client.chain_info().best_number; |
| 76 | + |
| 77 | + let period = runtime::BlockHashCount::get() |
| 78 | + .checked_next_power_of_two() |
| 79 | + .map(|c| c / 2) |
| 80 | + .unwrap_or(2) as u64; |
| 81 | + let extra: runtime::SignedExtra = ( |
| 82 | + frame_system::CheckNonZeroSender::<runtime::Runtime>::new(), |
| 83 | + frame_system::CheckSpecVersion::<runtime::Runtime>::new(), |
| 84 | + frame_system::CheckTxVersion::<runtime::Runtime>::new(), |
| 85 | + frame_system::CheckGenesis::<runtime::Runtime>::new(), |
| 86 | + frame_system::CheckEra::<runtime::Runtime>::from(sp_runtime::generic::Era::mortal( |
| 87 | + period, |
| 88 | + best_block.saturated_into(), |
| 89 | + )), |
| 90 | + frame_system::CheckNonce::<runtime::Runtime>::from(nonce), |
| 91 | + frame_system::CheckWeight::<runtime::Runtime>::new(), |
| 92 | + pallet_transaction_payment::ChargeTransactionPayment::<runtime::Runtime>::from(0), |
| 93 | + ); |
| 94 | + |
| 95 | + let raw_payload = runtime::SignedPayload::from_raw( |
| 96 | + call.clone(), |
| 97 | + extra.clone(), |
| 98 | + ( |
| 99 | + (), |
| 100 | + runtime::VERSION.spec_version, |
| 101 | + runtime::VERSION.transaction_version, |
| 102 | + genesis_hash, |
| 103 | + best_hash, |
| 104 | + (), |
| 105 | + (), |
| 106 | + (), |
| 107 | + ), |
| 108 | + ); |
| 109 | + let signature = raw_payload.using_encoded(|e| sender.sign(e)); |
| 110 | + |
| 111 | + runtime::UncheckedExtrinsic::new_signed( |
| 112 | + call.clone(), |
| 113 | + sp_runtime::AccountId32::from(sender.public()).into(), |
| 114 | + runtime::Signature::Sr25519(signature.clone()), |
| 115 | + extra.clone(), |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +/// Generates inherent data for the `benchmark overhead` command. |
| 120 | +/// |
| 121 | +/// Note: Should only be used for benchmarking. |
| 122 | +pub fn inherent_benchmark_data() -> Result<InherentData> { |
| 123 | + let mut inherent_data = InherentData::new(); |
| 124 | + let d = Duration::from_millis(0); |
| 125 | + let timestamp = sp_timestamp::InherentDataProvider::new(d.into()); |
| 126 | + |
| 127 | + timestamp |
| 128 | + .provide_inherent_data(&mut inherent_data) |
| 129 | + .map_err(|e| format!("creating inherent data: {:?}", e))?; |
| 130 | + Ok(inherent_data) |
| 131 | +} |
0 commit comments