|
5 | 5 | use crate::service::FullClient; |
6 | 6 |
|
7 | 7 | use node_subtensor_runtime as runtime; |
| 8 | +use node_subtensor_runtime::pallet_subtensor; |
8 | 9 | use runtime::{AccountId, Balance, BalancesCall, SystemCall}; |
9 | 10 | use sc_cli::Result; |
10 | 11 | use sc_client_api::BlockBackend; |
11 | 12 | use sp_core::{Encode, Pair}; |
12 | 13 | use sp_inherents::{InherentData, InherentDataProvider}; |
13 | 14 | use sp_keyring::Sr25519Keyring; |
14 | 15 | use sp_runtime::{OpaqueExtrinsic, SaturatedConversion}; |
15 | | -use node_subtensor_runtime::pallet_subtensor; |
16 | 16 |
|
17 | 17 | use std::{sync::Arc, time::Duration}; |
18 | 18 |
|
19 | 19 | // Generates extrinsics for the `benchmark overhead` command. |
20 | 20 | // |
21 | 21 | // Note: Should only be used for benchmarking. |
22 | 22 | pub struct RemarkBuilder { |
23 | | - client: Arc<FullClient>, |
| 23 | + client: Arc<FullClient>, |
24 | 24 | } |
25 | 25 |
|
26 | 26 | impl RemarkBuilder { |
27 | | - // Creates a new [`Self`] from the given client. |
28 | | - pub fn new(client: Arc<FullClient>) -> Self { |
29 | | - Self { client } |
30 | | - } |
| 27 | + // Creates a new [`Self`] from the given client. |
| 28 | + pub fn new(client: Arc<FullClient>) -> Self { |
| 29 | + Self { client } |
| 30 | + } |
31 | 31 | } |
32 | 32 |
|
33 | 33 | impl frame_benchmarking_cli::ExtrinsicBuilder for RemarkBuilder { |
34 | | - fn pallet(&self) -> &str { |
35 | | - "system" |
36 | | - } |
37 | | - |
38 | | - fn extrinsic(&self) -> &str { |
39 | | - "remark" |
40 | | - } |
41 | | - |
42 | | - fn build(&self, nonce: u32) -> std::result::Result<OpaqueExtrinsic, &'static str> { |
43 | | - let acc = Sr25519Keyring::Bob.pair(); |
44 | | - let extrinsic: OpaqueExtrinsic = create_benchmark_extrinsic( |
45 | | - self.client.as_ref(), |
46 | | - acc, |
47 | | - SystemCall::remark { remark: vec![] }.into(), |
48 | | - nonce, |
49 | | - ) |
50 | | - .into(); |
51 | | - |
52 | | - Ok(extrinsic) |
53 | | - } |
| 34 | + fn pallet(&self) -> &str { |
| 35 | + "system" |
| 36 | + } |
| 37 | + |
| 38 | + fn extrinsic(&self) -> &str { |
| 39 | + "remark" |
| 40 | + } |
| 41 | + |
| 42 | + fn build(&self, nonce: u32) -> std::result::Result<OpaqueExtrinsic, &'static str> { |
| 43 | + let acc = Sr25519Keyring::Bob.pair(); |
| 44 | + let extrinsic: OpaqueExtrinsic = create_benchmark_extrinsic( |
| 45 | + self.client.as_ref(), |
| 46 | + acc, |
| 47 | + SystemCall::remark { remark: vec![] }.into(), |
| 48 | + nonce, |
| 49 | + ) |
| 50 | + .into(); |
| 51 | + |
| 52 | + Ok(extrinsic) |
| 53 | + } |
54 | 54 | } |
55 | 55 |
|
56 | 56 | // Generates `Balances::TransferKeepAlive` extrinsics for the benchmarks. |
57 | 57 | // |
58 | 58 | // Note: Should only be used for benchmarking. |
59 | 59 | pub struct TransferKeepAliveBuilder { |
60 | | - client: Arc<FullClient>, |
61 | | - dest: AccountId, |
62 | | - value: Balance, |
| 60 | + client: Arc<FullClient>, |
| 61 | + dest: AccountId, |
| 62 | + value: Balance, |
63 | 63 | } |
64 | 64 |
|
65 | 65 | impl TransferKeepAliveBuilder { |
66 | | - // Creates a new [`Self`] from the given client. |
67 | | - pub fn new(client: Arc<FullClient>, dest: AccountId, value: Balance) -> Self { |
68 | | - Self { client, dest, value } |
69 | | - } |
| 66 | + // Creates a new [`Self`] from the given client. |
| 67 | + pub fn new(client: Arc<FullClient>, dest: AccountId, value: Balance) -> Self { |
| 68 | + Self { |
| 69 | + client, |
| 70 | + dest, |
| 71 | + value, |
| 72 | + } |
| 73 | + } |
70 | 74 | } |
71 | 75 |
|
72 | 76 | impl frame_benchmarking_cli::ExtrinsicBuilder for TransferKeepAliveBuilder { |
73 | | - fn pallet(&self) -> &str { |
74 | | - "balances" |
75 | | - } |
76 | | - |
77 | | - fn extrinsic(&self) -> &str { |
78 | | - "transfer_keep_alive" |
79 | | - } |
80 | | - |
81 | | - fn build(&self, nonce: u32) -> std::result::Result<OpaqueExtrinsic, &'static str> { |
82 | | - let acc = Sr25519Keyring::Bob.pair(); |
83 | | - let extrinsic: OpaqueExtrinsic = create_benchmark_extrinsic( |
84 | | - self.client.as_ref(), |
85 | | - acc, |
86 | | - BalancesCall::transfer_keep_alive { |
87 | | - dest: self.dest.clone().into(), |
88 | | - value: self.value.into(), |
89 | | - } |
90 | | - .into(), |
91 | | - nonce, |
92 | | - ) |
93 | | - .into(); |
94 | | - |
95 | | - Ok(extrinsic) |
96 | | - } |
| 77 | + fn pallet(&self) -> &str { |
| 78 | + "balances" |
| 79 | + } |
| 80 | + |
| 81 | + fn extrinsic(&self) -> &str { |
| 82 | + "transfer_keep_alive" |
| 83 | + } |
| 84 | + |
| 85 | + fn build(&self, nonce: u32) -> std::result::Result<OpaqueExtrinsic, &'static str> { |
| 86 | + let acc = Sr25519Keyring::Bob.pair(); |
| 87 | + let extrinsic: OpaqueExtrinsic = create_benchmark_extrinsic( |
| 88 | + self.client.as_ref(), |
| 89 | + acc, |
| 90 | + BalancesCall::transfer_keep_alive { |
| 91 | + dest: self.dest.clone().into(), |
| 92 | + value: self.value.into(), |
| 93 | + } |
| 94 | + .into(), |
| 95 | + nonce, |
| 96 | + ) |
| 97 | + .into(); |
| 98 | + |
| 99 | + Ok(extrinsic) |
| 100 | + } |
97 | 101 | } |
98 | 102 |
|
99 | 103 | // Create a transaction using the given `call`. |
100 | 104 | // |
101 | 105 | // Note: Should only be used for benchmarking. |
102 | 106 | pub fn create_benchmark_extrinsic( |
103 | | - client: &FullClient, |
104 | | - sender: sp_core::sr25519::Pair, |
105 | | - call: runtime::RuntimeCall, |
106 | | - nonce: u32, |
| 107 | + client: &FullClient, |
| 108 | + sender: sp_core::sr25519::Pair, |
| 109 | + call: runtime::RuntimeCall, |
| 110 | + nonce: u32, |
107 | 111 | ) -> runtime::UncheckedExtrinsic { |
108 | | - let genesis_hash = client.block_hash(0).ok().flatten().expect("Genesis block exists; qed"); |
109 | | - let best_hash = client.chain_info().best_hash; |
110 | | - let best_block = client.chain_info().best_number; |
111 | | - |
112 | | - let period = runtime::BlockHashCount::get() |
113 | | - .checked_next_power_of_two() |
114 | | - .map(|c| c / 2) |
115 | | - .unwrap_or(2) as u64; |
116 | | - let extra: runtime::SignedExtra = ( |
117 | | - frame_system::CheckNonZeroSender::<runtime::Runtime>::new(), |
118 | | - frame_system::CheckSpecVersion::<runtime::Runtime>::new(), |
119 | | - frame_system::CheckTxVersion::<runtime::Runtime>::new(), |
120 | | - frame_system::CheckGenesis::<runtime::Runtime>::new(), |
121 | | - frame_system::CheckEra::<runtime::Runtime>::from(sp_runtime::generic::Era::mortal( |
122 | | - period, |
123 | | - best_block.saturated_into(), |
124 | | - )), |
125 | | - frame_system::CheckNonce::<runtime::Runtime>::from(nonce), |
126 | | - frame_system::CheckWeight::<runtime::Runtime>::new(), |
127 | | - pallet_transaction_payment::ChargeTransactionPayment::<runtime::Runtime>::from(0), |
128 | | - pallet_subtensor::SubtensorSignedExtension::<runtime::Runtime>::new(), |
129 | | - pallet_commitments::CommitmentsSignedExtension::<runtime::Runtime>::new() |
130 | | - ); |
131 | | - |
132 | | - let raw_payload = runtime::SignedPayload::from_raw( |
133 | | - call.clone(), |
134 | | - extra.clone(), |
135 | | - ( |
136 | | - (), |
137 | | - runtime::VERSION.spec_version, |
138 | | - runtime::VERSION.transaction_version, |
139 | | - genesis_hash, |
140 | | - best_hash, |
141 | | - (), |
142 | | - (), |
143 | | - (), |
144 | | - (), |
145 | | - () |
146 | | - ), |
147 | | - ); |
148 | | - let signature = raw_payload.using_encoded(|e| sender.sign(e)); |
149 | | - |
150 | | - runtime::UncheckedExtrinsic::new_signed( |
151 | | - call.clone(), |
152 | | - sp_runtime::AccountId32::from(sender.public()).into(), |
153 | | - runtime::Signature::Sr25519(signature.clone()), |
154 | | - extra.clone(), |
155 | | - ) |
| 112 | + let genesis_hash = client |
| 113 | + .block_hash(0) |
| 114 | + .ok() |
| 115 | + .flatten() |
| 116 | + .expect("Genesis block exists; qed"); |
| 117 | + let best_hash = client.chain_info().best_hash; |
| 118 | + let best_block = client.chain_info().best_number; |
| 119 | + |
| 120 | + let period = runtime::BlockHashCount::get() |
| 121 | + .checked_next_power_of_two() |
| 122 | + .map(|c| c / 2) |
| 123 | + .unwrap_or(2) as u64; |
| 124 | + let extra: runtime::SignedExtra = ( |
| 125 | + frame_system::CheckNonZeroSender::<runtime::Runtime>::new(), |
| 126 | + frame_system::CheckSpecVersion::<runtime::Runtime>::new(), |
| 127 | + frame_system::CheckTxVersion::<runtime::Runtime>::new(), |
| 128 | + frame_system::CheckGenesis::<runtime::Runtime>::new(), |
| 129 | + frame_system::CheckEra::<runtime::Runtime>::from(sp_runtime::generic::Era::mortal( |
| 130 | + period, |
| 131 | + best_block.saturated_into(), |
| 132 | + )), |
| 133 | + frame_system::CheckNonce::<runtime::Runtime>::from(nonce), |
| 134 | + frame_system::CheckWeight::<runtime::Runtime>::new(), |
| 135 | + pallet_transaction_payment::ChargeTransactionPayment::<runtime::Runtime>::from(0), |
| 136 | + pallet_subtensor::SubtensorSignedExtension::<runtime::Runtime>::new(), |
| 137 | + pallet_commitments::CommitmentsSignedExtension::<runtime::Runtime>::new(), |
| 138 | + ); |
| 139 | + |
| 140 | + let raw_payload = runtime::SignedPayload::from_raw( |
| 141 | + call.clone(), |
| 142 | + extra.clone(), |
| 143 | + ( |
| 144 | + (), |
| 145 | + runtime::VERSION.spec_version, |
| 146 | + runtime::VERSION.transaction_version, |
| 147 | + genesis_hash, |
| 148 | + best_hash, |
| 149 | + (), |
| 150 | + (), |
| 151 | + (), |
| 152 | + (), |
| 153 | + (), |
| 154 | + ), |
| 155 | + ); |
| 156 | + let signature = raw_payload.using_encoded(|e| sender.sign(e)); |
| 157 | + |
| 158 | + runtime::UncheckedExtrinsic::new_signed( |
| 159 | + call.clone(), |
| 160 | + sp_runtime::AccountId32::from(sender.public()).into(), |
| 161 | + runtime::Signature::Sr25519(signature.clone()), |
| 162 | + extra.clone(), |
| 163 | + ) |
156 | 164 | } |
157 | 165 |
|
158 | 166 | // Generates inherent data for the `benchmark overhead` command. |
159 | 167 | // |
160 | 168 | // Note: Should only be used for benchmarking. |
161 | 169 | pub fn inherent_benchmark_data() -> Result<InherentData> { |
162 | | - let mut inherent_data = InherentData::new(); |
163 | | - let d = Duration::from_millis(0); |
164 | | - let timestamp = sp_timestamp::InherentDataProvider::new(d.into()); |
| 170 | + let mut inherent_data = InherentData::new(); |
| 171 | + let d = Duration::from_millis(0); |
| 172 | + let timestamp = sp_timestamp::InherentDataProvider::new(d.into()); |
165 | 173 |
|
166 | | - futures::executor::block_on(timestamp.provide_inherent_data(&mut inherent_data)) |
167 | | - .map_err(|e| format!("creating inherent data: {:?}", e))?; |
168 | | - Ok(inherent_data) |
| 174 | + futures::executor::block_on(timestamp.provide_inherent_data(&mut inherent_data)) |
| 175 | + .map_err(|e| format!("creating inherent data: {:?}", e))?; |
| 176 | + Ok(inherent_data) |
169 | 177 | } |
0 commit comments