Skip to content

Commit d1e245c

Browse files
committed
chore: added more benchmarking.
1 parent 3491cf9 commit d1e245c

5 files changed

Lines changed: 241 additions & 67 deletions

File tree

crates/btc-relay/src/benchmarking.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub mod benchmarks {
8181
}
8282

8383
#[benchmark]
84-
pub fn store_block_header_when_updating_chainwork_mandatory() {
84+
pub fn store_block_header_when_adding_chainwork() {
8585
let caller: T::AccountId = whitelisted_caller();
8686

8787
let init_block = initialize_relay::<T>(caller.clone());
@@ -206,5 +206,55 @@ pub mod benchmarks {
206206
assert_eq!(rich_header.chain_id, MAIN_CHAIN_ID);
207207
}
208208

209+
#[benchmark]
210+
pub fn store_block_header_reorganize_chains_based_on_chainwork(f: Linear<3, 6>) {
211+
let caller: T::AccountId = whitelisted_caller();
212+
StableBitcoinConfirmations::<T>::put(SECURE_BITCOIN_CONFIRMATIONS);
213+
214+
let init_block = initialize_relay::<T>(caller.clone());
215+
let mut init_block_hash = init_block.header.hash;
216+
217+
migration::v1::migrate_from_v0_to_v1::<T>();
218+
219+
for i in 1..f {
220+
let mut block_hash = init_block_hash;
221+
for _ in 0..SECURE_BITCOIN_CONFIRMATIONS {
222+
let block = add_new_block_to_relay::<T>(caller.clone(), block_hash, i as usize);
223+
block_hash = block.header.hash;
224+
}
225+
}
226+
227+
// new fork up to block before swapping the main chain
228+
for _ in 1..(BestBlockHeight::<T>::get() + SECURE_BITCOIN_CONFIRMATIONS) {
229+
let block = add_new_block_to_relay::<T>(caller.clone(), init_block_hash, f as usize);
230+
init_block_hash = block.header.hash;
231+
}
232+
233+
let prev_best_block_height = BestBlockHeight::<T>::get();
234+
assert_eq!(prev_best_block_height, SECURE_BITCOIN_CONFIRMATIONS);
235+
assert_eq!(ChainsIndex::<T>::iter().collect::<Vec<_>>().len(), f as usize);
236+
237+
let mut last_entered_block = BtcRelay::<T>::get_block_header_from_hash(init_block_hash).unwrap();
238+
last_entered_block.block_header.target = U256::max_value();
239+
BlockHeaders::<T>::insert(last_entered_block.block_header.hash, last_entered_block);
240+
241+
// we can benchmark the worst-case complexity for swapping
242+
// since we know how many blocks are required
243+
let mut block = new_block::<T>(init_block_hash, f as usize);
244+
block.header.target = U256::max_value();
245+
block.header.update_hash().unwrap();
246+
247+
#[extrinsic_call]
248+
store_block_header(RawOrigin::Signed(caller), block.header, u32::MAX);
249+
250+
// make sure reorg occurred
251+
assert_eq!(
252+
BestBlockHeight::<T>::get(),
253+
prev_best_block_height + SECURE_BITCOIN_CONFIRMATIONS
254+
);
255+
let rich_header = BtcRelay::<T>::get_block_header_from_hash(block.header.hash).unwrap();
256+
assert_eq!(rich_header.chain_id, MAIN_CHAIN_ID);
257+
}
258+
209259
impl_benchmark_test_suite!(BtcRelay, crate::mock::ExtBuilder::build(), crate::mock::Test);
210260
}

crates/btc-relay/src/default_weights.rs

Lines changed: 94 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ pub trait WeightInfo {
4343
fn store_block_header_new_fork_unsorted(f: u32, ) -> Weight;
4444
fn store_block_header_reorganize_chains(f: u32, ) -> Weight;
4545
fn set_chainwork_for_block() -> Weight;
46-
fn store_block_header_when_updating_chainwork_mandatory () -> Weight;
46+
fn store_block_header_when_adding_chainwork () -> Weight;
47+
fn store_block_header_reorganize_chains_based_on_chainwork(f: u32, ) -> Weight;
4748
}
4849

4950
/// Weights for btc_relay using the Substrate node and recommended hardware.
5051
pub struct SubstrateWeight<T>(PhantomData<T>);
5152
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
5253

54+
5355
/// Storage: `BTCRelay::BestBlock` (r:1 w:1)
5456
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
5557
/// Storage: `BTCRelay::DisableDifficultyCheck` (r:1 w:0)
@@ -76,8 +78,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
7678
// Proof Size summary in bytes:
7779
// Measured: `403`
7880
// Estimated: `3545`
79-
// Minimum execution time: 80_000_000 picoseconds.
80-
Weight::from_parts(80_000_000, 3545)
81+
// Minimum execution time: 44_000_000 picoseconds.
82+
Weight::from_parts(44_000_000, 3545)
8183
.saturating_add(T::DbWeight::get().reads(6_u64))
8284
.saturating_add(T::DbWeight::get().writes(8_u64))
8385
}
@@ -89,8 +91,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
8991
// Proof Size summary in bytes:
9092
// Measured: `1049`
9193
// Estimated: `6340`
92-
// Minimum execution time: 39_000_000 picoseconds.
93-
Weight::from_parts(39_000_000, 6340)
94+
// Minimum execution time: 27_000_000 picoseconds.
95+
Weight::from_parts(27_000_000, 6340)
9496
.saturating_add(T::DbWeight::get().reads(3_u64))
9597
.saturating_add(T::DbWeight::get().writes(1_u64))
9698
}
@@ -112,12 +114,12 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
112114
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
113115
/// Storage: `BTCRelay::BestBlockHeight` (r:0 w:1)
114116
/// Proof: `BTCRelay::BestBlockHeight` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
115-
fn store_block_header_when_updating_chainwork_mandatory () -> Weight {
117+
fn store_block_header_when_adding_chainwork () -> Weight {
116118
// Proof Size summary in bytes:
117119
// Measured: `979`
118120
// Estimated: `6340`
119-
// Minimum execution time: 83_000_000 picoseconds.
120-
Weight::from_parts(83_000_000, 6340)
121+
// Minimum execution time: 52_000_000 picoseconds.
122+
Weight::from_parts(52_000_000, 6340)
121123
.saturating_add(T::DbWeight::get().reads(8_u64))
122124
.saturating_add(T::DbWeight::get().writes(6_u64))
123125
}
@@ -143,8 +145,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
143145
// Proof Size summary in bytes:
144146
// Measured: `874`
145147
// Estimated: `6340`
146-
// Minimum execution time: 73_000_000 picoseconds.
147-
Weight::from_parts(73_000_000, 6340)
148+
// Minimum execution time: 47_000_000 picoseconds.
149+
Weight::from_parts(47_000_000, 6340)
148150
.saturating_add(T::DbWeight::get().reads(8_u64))
149151
.saturating_add(T::DbWeight::get().writes(5_u64))
150152
}
@@ -171,8 +173,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
171173
// Proof Size summary in bytes:
172174
// Measured: `796 + f * (110 ±0)`
173175
// Estimated: `18483`
174-
// Minimum execution time: 85_000_000 picoseconds.
175-
Weight::from_parts(140_000_000, 18483)
176+
// Minimum execution time: 71_000_000 picoseconds.
177+
Weight::from_parts(93_000_000, 18483)
176178
.saturating_add(T::DbWeight::get().reads(20_u64))
177179
.saturating_add(T::DbWeight::get().writes(5_u64))
178180
}
@@ -199,8 +201,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
199201
// Proof Size summary in bytes:
200202
// Measured: `832 + f * (108 ±0)`
201203
// Estimated: `18483`
202-
// Minimum execution time: 85_000_000 picoseconds.
203-
Weight::from_parts(149_000_000, 18483)
204+
// Minimum execution time: 54_000_000 picoseconds.
205+
Weight::from_parts(94_000_000, 18483)
204206
.saturating_add(T::DbWeight::get().reads(16_u64))
205207
.saturating_add(T::DbWeight::get().writes(10_u64))
206208
}
@@ -231,16 +233,47 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
231233
// Proof Size summary in bytes:
232234
// Measured: `4754 + f * (235 ±0)`
233235
// Estimated: `54490`
234-
// Minimum execution time: 509_000_000 picoseconds.
235-
Weight::from_parts(533_000_000, 54490)
236+
// Minimum execution time: 322_000_000 picoseconds.
237+
Weight::from_parts(345_000_000, 54490)
236238
.saturating_add(T::DbWeight::get().reads(49_u64))
237239
.saturating_add(T::DbWeight::get().writes(46_u64))
238240
}
241+
/// Storage: `BTCRelay::ChainCounter` (r:1 w:0)
242+
/// Proof: `BTCRelay::ChainCounter` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
243+
/// Storage: `BTCRelay::BlockHeaders` (r:20 w:18)
244+
/// Proof: `BTCRelay::BlockHeaders` (`max_values`: None, `max_size`: Some(200), added: 2675, mode: `MaxEncodedLen`)
245+
/// Storage: `BTCRelay::ChainsIndex` (r:3 w:2)
246+
/// Proof: `BTCRelay::ChainsIndex` (`max_values`: None, `max_size`: Some(32), added: 2507, mode: `MaxEncodedLen`)
247+
/// Storage: `BTCRelay::DisableDifficultyCheck` (r:1 w:0)
248+
/// Proof: `BTCRelay::DisableDifficultyCheck` (`max_values`: Some(1), `max_size`: Some(1), added: 496, mode: `MaxEncodedLen`)
249+
/// Storage: `BTCRelay::ChainsHashes` (r:13 w:24)
250+
/// Proof: `BTCRelay::ChainsHashes` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`)
251+
/// Storage: `Security::ActiveBlockCount` (r:1 w:0)
252+
/// Proof: `Security::ActiveBlockCount` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
253+
/// Storage: `BTCRelay::ChainWork` (r:2 w:1)
254+
/// Proof: `BTCRelay::ChainWork` (`max_values`: None, `max_size`: Some(80), added: 2555, mode: `MaxEncodedLen`)
255+
/// Storage: `BTCRelay::Chains` (r:6 w:0)
256+
/// Proof: `BTCRelay::Chains` (`max_values`: None, `max_size`: Some(24), added: 2499, mode: `MaxEncodedLen`)
257+
/// Storage: `BTCRelay::BestBlock` (r:0 w:1)
258+
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
259+
/// Storage: `BTCRelay::BestBlockHeight` (r:0 w:1)
260+
/// Proof: `BTCRelay::BestBlockHeight` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
261+
/// The range of component `f` is `[3, 6]`.
262+
fn store_block_header_reorganize_chains_based_on_chainwork (_f: u32, ) -> Weight {
263+
// Proof Size summary in bytes:
264+
// Measured: `5196 + f * (298 ±0)`
265+
// Estimated: `54490`
266+
// Minimum execution time: 328_000_000 picoseconds.
267+
Weight::from_parts(358_000_000, 54490)
268+
.saturating_add(T::DbWeight::get().reads(47_u64))
269+
.saturating_add(T::DbWeight::get().writes(47_u64))
270+
}
239271
}
240272

241273
// For backwards compatibility and tests
242274
impl WeightInfo for () {
243275

276+
244277
/// Storage: `BTCRelay::BestBlock` (r:1 w:1)
245278
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
246279
/// Storage: `BTCRelay::DisableDifficultyCheck` (r:1 w:0)
@@ -267,8 +300,8 @@ impl WeightInfo for () {
267300
// Proof Size summary in bytes:
268301
// Measured: `403`
269302
// Estimated: `3545`
270-
// Minimum execution time: 80_000_000 picoseconds.
271-
Weight::from_parts(80_000_000, 3545)
303+
// Minimum execution time: 44_000_000 picoseconds.
304+
Weight::from_parts(44_000_000, 3545)
272305
.saturating_add(RocksDbWeight::get().reads(6_u64))
273306
.saturating_add(RocksDbWeight::get().writes(8_u64))
274307
}
@@ -280,8 +313,8 @@ impl WeightInfo for () {
280313
// Proof Size summary in bytes:
281314
// Measured: `1049`
282315
// Estimated: `6340`
283-
// Minimum execution time: 39_000_000 picoseconds.
284-
Weight::from_parts(39_000_000, 6340)
316+
// Minimum execution time: 27_000_000 picoseconds.
317+
Weight::from_parts(27_000_000, 6340)
285318
.saturating_add(RocksDbWeight::get().reads(3_u64))
286319
.saturating_add(RocksDbWeight::get().writes(1_u64))
287320
}
@@ -303,12 +336,12 @@ impl WeightInfo for () {
303336
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
304337
/// Storage: `BTCRelay::BestBlockHeight` (r:0 w:1)
305338
/// Proof: `BTCRelay::BestBlockHeight` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
306-
fn store_block_header_when_updating_chainwork_mandatory () -> Weight {
339+
fn store_block_header_when_adding_chainwork () -> Weight {
307340
// Proof Size summary in bytes:
308341
// Measured: `979`
309342
// Estimated: `6340`
310-
// Minimum execution time: 83_000_000 picoseconds.
311-
Weight::from_parts(83_000_000, 6340)
343+
// Minimum execution time: 52_000_000 picoseconds.
344+
Weight::from_parts(52_000_000, 6340)
312345
.saturating_add(RocksDbWeight::get().reads(8_u64))
313346
.saturating_add(RocksDbWeight::get().writes(6_u64))
314347
}
@@ -334,8 +367,8 @@ impl WeightInfo for () {
334367
// Proof Size summary in bytes:
335368
// Measured: `874`
336369
// Estimated: `6340`
337-
// Minimum execution time: 73_000_000 picoseconds.
338-
Weight::from_parts(73_000_000, 6340)
370+
// Minimum execution time: 47_000_000 picoseconds.
371+
Weight::from_parts(47_000_000, 6340)
339372
.saturating_add(RocksDbWeight::get().reads(8_u64))
340373
.saturating_add(RocksDbWeight::get().writes(5_u64))
341374
}
@@ -362,8 +395,8 @@ impl WeightInfo for () {
362395
// Proof Size summary in bytes:
363396
// Measured: `796 + f * (110 ±0)`
364397
// Estimated: `18483`
365-
// Minimum execution time: 85_000_000 picoseconds.
366-
Weight::from_parts(140_000_000, 18483)
398+
// Minimum execution time: 71_000_000 picoseconds.
399+
Weight::from_parts(93_000_000, 18483)
367400
.saturating_add(RocksDbWeight::get().reads(20_u64))
368401
.saturating_add(RocksDbWeight::get().writes(5_u64))
369402
}
@@ -390,8 +423,8 @@ impl WeightInfo for () {
390423
// Proof Size summary in bytes:
391424
// Measured: `832 + f * (108 ±0)`
392425
// Estimated: `18483`
393-
// Minimum execution time: 85_000_000 picoseconds.
394-
Weight::from_parts(149_000_000, 18483)
426+
// Minimum execution time: 54_000_000 picoseconds.
427+
Weight::from_parts(94_000_000, 18483)
395428
.saturating_add(RocksDbWeight::get().reads(16_u64))
396429
.saturating_add(RocksDbWeight::get().writes(10_u64))
397430
}
@@ -422,9 +455,39 @@ impl WeightInfo for () {
422455
// Proof Size summary in bytes:
423456
// Measured: `4754 + f * (235 ±0)`
424457
// Estimated: `54490`
425-
// Minimum execution time: 509_000_000 picoseconds.
426-
Weight::from_parts(533_000_000, 54490)
458+
// Minimum execution time: 322_000_000 picoseconds.
459+
Weight::from_parts(345_000_000, 54490)
427460
.saturating_add(RocksDbWeight::get().reads(49_u64))
428461
.saturating_add(RocksDbWeight::get().writes(46_u64))
429462
}
463+
/// Storage: `BTCRelay::ChainCounter` (r:1 w:0)
464+
/// Proof: `BTCRelay::ChainCounter` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
465+
/// Storage: `BTCRelay::BlockHeaders` (r:20 w:18)
466+
/// Proof: `BTCRelay::BlockHeaders` (`max_values`: None, `max_size`: Some(200), added: 2675, mode: `MaxEncodedLen`)
467+
/// Storage: `BTCRelay::ChainsIndex` (r:3 w:2)
468+
/// Proof: `BTCRelay::ChainsIndex` (`max_values`: None, `max_size`: Some(32), added: 2507, mode: `MaxEncodedLen`)
469+
/// Storage: `BTCRelay::DisableDifficultyCheck` (r:1 w:0)
470+
/// Proof: `BTCRelay::DisableDifficultyCheck` (`max_values`: Some(1), `max_size`: Some(1), added: 496, mode: `MaxEncodedLen`)
471+
/// Storage: `BTCRelay::ChainsHashes` (r:13 w:24)
472+
/// Proof: `BTCRelay::ChainsHashes` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`)
473+
/// Storage: `Security::ActiveBlockCount` (r:1 w:0)
474+
/// Proof: `Security::ActiveBlockCount` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
475+
/// Storage: `BTCRelay::ChainWork` (r:2 w:1)
476+
/// Proof: `BTCRelay::ChainWork` (`max_values`: None, `max_size`: Some(80), added: 2555, mode: `MaxEncodedLen`)
477+
/// Storage: `BTCRelay::Chains` (r:6 w:0)
478+
/// Proof: `BTCRelay::Chains` (`max_values`: None, `max_size`: Some(24), added: 2499, mode: `MaxEncodedLen`)
479+
/// Storage: `BTCRelay::BestBlock` (r:0 w:1)
480+
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
481+
/// Storage: `BTCRelay::BestBlockHeight` (r:0 w:1)
482+
/// Proof: `BTCRelay::BestBlockHeight` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
483+
/// The range of component `f` is `[3, 6]`.
484+
fn store_block_header_reorganize_chains_based_on_chainwork (_f: u32, ) -> Weight {
485+
// Proof Size summary in bytes:
486+
// Measured: `5196 + f * (298 ±0)`
487+
// Estimated: `54490`
488+
// Minimum execution time: 328_000_000 picoseconds.
489+
Weight::from_parts(358_000_000, 54490)
490+
.saturating_add(RocksDbWeight::get().reads(47_u64))
491+
.saturating_add(RocksDbWeight::get().writes(47_u64))
492+
}
430493
}

crates/btc-relay/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ pub mod pallet {
162162
.max(<T as Config>::WeightInfo::store_block_header_new_fork_sorted(f))
163163
.max(<T as Config>::WeightInfo::store_block_header_new_fork_unsorted(f))
164164
.max(<T as Config>::WeightInfo::store_block_header_reorganize_chains(f))
165-
.max(<T as Config>::WeightInfo::store_block_header_when_updating_chainwork_mandatory())
165+
.max(<T as Config>::WeightInfo::store_block_header_reorganize_chains_based_on_chainwork(f))
166+
.max(<T as Config>::WeightInfo::store_block_header_when_adding_chainwork())
166167
},
167168
DispatchClass::Operational
168169
))]

0 commit comments

Comments
 (0)