Skip to content

Commit

Permalink
chore: added more benchmarking.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Sep 27, 2023
1 parent 3491cf9 commit d1e245c
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 67 deletions.
52 changes: 51 additions & 1 deletion crates/btc-relay/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub mod benchmarks {
}

#[benchmark]
pub fn store_block_header_when_updating_chainwork_mandatory() {
pub fn store_block_header_when_adding_chainwork() {
let caller: T::AccountId = whitelisted_caller();

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

#[benchmark]
pub fn store_block_header_reorganize_chains_based_on_chainwork(f: Linear<3, 6>) {
let caller: T::AccountId = whitelisted_caller();
StableBitcoinConfirmations::<T>::put(SECURE_BITCOIN_CONFIRMATIONS);

let init_block = initialize_relay::<T>(caller.clone());
let mut init_block_hash = init_block.header.hash;

migration::v1::migrate_from_v0_to_v1::<T>();

for i in 1..f {
let mut block_hash = init_block_hash;
for _ in 0..SECURE_BITCOIN_CONFIRMATIONS {
let block = add_new_block_to_relay::<T>(caller.clone(), block_hash, i as usize);
block_hash = block.header.hash;
}
}

// new fork up to block before swapping the main chain
for _ in 1..(BestBlockHeight::<T>::get() + SECURE_BITCOIN_CONFIRMATIONS) {
let block = add_new_block_to_relay::<T>(caller.clone(), init_block_hash, f as usize);
init_block_hash = block.header.hash;
}

let prev_best_block_height = BestBlockHeight::<T>::get();
assert_eq!(prev_best_block_height, SECURE_BITCOIN_CONFIRMATIONS);
assert_eq!(ChainsIndex::<T>::iter().collect::<Vec<_>>().len(), f as usize);

let mut last_entered_block = BtcRelay::<T>::get_block_header_from_hash(init_block_hash).unwrap();
last_entered_block.block_header.target = U256::max_value();
BlockHeaders::<T>::insert(last_entered_block.block_header.hash, last_entered_block);

// we can benchmark the worst-case complexity for swapping
// since we know how many blocks are required
let mut block = new_block::<T>(init_block_hash, f as usize);
block.header.target = U256::max_value();
block.header.update_hash().unwrap();

#[extrinsic_call]
store_block_header(RawOrigin::Signed(caller), block.header, u32::MAX);

// make sure reorg occurred
assert_eq!(
BestBlockHeight::<T>::get(),
prev_best_block_height + SECURE_BITCOIN_CONFIRMATIONS
);
let rich_header = BtcRelay::<T>::get_block_header_from_hash(block.header.hash).unwrap();
assert_eq!(rich_header.chain_id, MAIN_CHAIN_ID);
}

impl_benchmark_test_suite!(BtcRelay, crate::mock::ExtBuilder::build(), crate::mock::Test);
}
125 changes: 94 additions & 31 deletions crates/btc-relay/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ pub trait WeightInfo {
fn store_block_header_new_fork_unsorted(f: u32, ) -> Weight;
fn store_block_header_reorganize_chains(f: u32, ) -> Weight;
fn set_chainwork_for_block() -> Weight;
fn store_block_header_when_updating_chainwork_mandatory () -> Weight;
fn store_block_header_when_adding_chainwork () -> Weight;
fn store_block_header_reorganize_chains_based_on_chainwork(f: u32, ) -> Weight;
}

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


/// Storage: `BTCRelay::BestBlock` (r:1 w:1)
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::DisableDifficultyCheck` (r:1 w:0)
Expand All @@ -76,8 +78,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `403`
// Estimated: `3545`
// Minimum execution time: 80_000_000 picoseconds.
Weight::from_parts(80_000_000, 3545)
// Minimum execution time: 44_000_000 picoseconds.
Weight::from_parts(44_000_000, 3545)
.saturating_add(T::DbWeight::get().reads(6_u64))
.saturating_add(T::DbWeight::get().writes(8_u64))
}
Expand All @@ -89,8 +91,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `1049`
// Estimated: `6340`
// Minimum execution time: 39_000_000 picoseconds.
Weight::from_parts(39_000_000, 6340)
// Minimum execution time: 27_000_000 picoseconds.
Weight::from_parts(27_000_000, 6340)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -112,12 +114,12 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::BestBlockHeight` (r:0 w:1)
/// Proof: `BTCRelay::BestBlockHeight` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
fn store_block_header_when_updating_chainwork_mandatory () -> Weight {
fn store_block_header_when_adding_chainwork () -> Weight {
// Proof Size summary in bytes:
// Measured: `979`
// Estimated: `6340`
// Minimum execution time: 83_000_000 picoseconds.
Weight::from_parts(83_000_000, 6340)
// Minimum execution time: 52_000_000 picoseconds.
Weight::from_parts(52_000_000, 6340)
.saturating_add(T::DbWeight::get().reads(8_u64))
.saturating_add(T::DbWeight::get().writes(6_u64))
}
Expand All @@ -143,8 +145,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `874`
// Estimated: `6340`
// Minimum execution time: 73_000_000 picoseconds.
Weight::from_parts(73_000_000, 6340)
// Minimum execution time: 47_000_000 picoseconds.
Weight::from_parts(47_000_000, 6340)
.saturating_add(T::DbWeight::get().reads(8_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand All @@ -171,8 +173,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `796 + f * (110 ±0)`
// Estimated: `18483`
// Minimum execution time: 85_000_000 picoseconds.
Weight::from_parts(140_000_000, 18483)
// Minimum execution time: 71_000_000 picoseconds.
Weight::from_parts(93_000_000, 18483)
.saturating_add(T::DbWeight::get().reads(20_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand All @@ -199,8 +201,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `832 + f * (108 ±0)`
// Estimated: `18483`
// Minimum execution time: 85_000_000 picoseconds.
Weight::from_parts(149_000_000, 18483)
// Minimum execution time: 54_000_000 picoseconds.
Weight::from_parts(94_000_000, 18483)
.saturating_add(T::DbWeight::get().reads(16_u64))
.saturating_add(T::DbWeight::get().writes(10_u64))
}
Expand Down Expand Up @@ -231,16 +233,47 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `4754 + f * (235 ±0)`
// Estimated: `54490`
// Minimum execution time: 509_000_000 picoseconds.
Weight::from_parts(533_000_000, 54490)
// Minimum execution time: 322_000_000 picoseconds.
Weight::from_parts(345_000_000, 54490)
.saturating_add(T::DbWeight::get().reads(49_u64))
.saturating_add(T::DbWeight::get().writes(46_u64))
}
/// Storage: `BTCRelay::ChainCounter` (r:1 w:0)
/// Proof: `BTCRelay::ChainCounter` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::BlockHeaders` (r:20 w:18)
/// Proof: `BTCRelay::BlockHeaders` (`max_values`: None, `max_size`: Some(200), added: 2675, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::ChainsIndex` (r:3 w:2)
/// Proof: `BTCRelay::ChainsIndex` (`max_values`: None, `max_size`: Some(32), added: 2507, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::DisableDifficultyCheck` (r:1 w:0)
/// Proof: `BTCRelay::DisableDifficultyCheck` (`max_values`: Some(1), `max_size`: Some(1), added: 496, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::ChainsHashes` (r:13 w:24)
/// Proof: `BTCRelay::ChainsHashes` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`)
/// Storage: `Security::ActiveBlockCount` (r:1 w:0)
/// Proof: `Security::ActiveBlockCount` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::ChainWork` (r:2 w:1)
/// Proof: `BTCRelay::ChainWork` (`max_values`: None, `max_size`: Some(80), added: 2555, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::Chains` (r:6 w:0)
/// Proof: `BTCRelay::Chains` (`max_values`: None, `max_size`: Some(24), added: 2499, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::BestBlock` (r:0 w:1)
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::BestBlockHeight` (r:0 w:1)
/// Proof: `BTCRelay::BestBlockHeight` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// The range of component `f` is `[3, 6]`.
fn store_block_header_reorganize_chains_based_on_chainwork (_f: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `5196 + f * (298 ±0)`
// Estimated: `54490`
// Minimum execution time: 328_000_000 picoseconds.
Weight::from_parts(358_000_000, 54490)
.saturating_add(T::DbWeight::get().reads(47_u64))
.saturating_add(T::DbWeight::get().writes(47_u64))
}
}

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


/// Storage: `BTCRelay::BestBlock` (r:1 w:1)
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::DisableDifficultyCheck` (r:1 w:0)
Expand All @@ -267,8 +300,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `403`
// Estimated: `3545`
// Minimum execution time: 80_000_000 picoseconds.
Weight::from_parts(80_000_000, 3545)
// Minimum execution time: 44_000_000 picoseconds.
Weight::from_parts(44_000_000, 3545)
.saturating_add(RocksDbWeight::get().reads(6_u64))
.saturating_add(RocksDbWeight::get().writes(8_u64))
}
Expand All @@ -280,8 +313,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `1049`
// Estimated: `6340`
// Minimum execution time: 39_000_000 picoseconds.
Weight::from_parts(39_000_000, 6340)
// Minimum execution time: 27_000_000 picoseconds.
Weight::from_parts(27_000_000, 6340)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -303,12 +336,12 @@ impl WeightInfo for () {
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::BestBlockHeight` (r:0 w:1)
/// Proof: `BTCRelay::BestBlockHeight` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
fn store_block_header_when_updating_chainwork_mandatory () -> Weight {
fn store_block_header_when_adding_chainwork () -> Weight {
// Proof Size summary in bytes:
// Measured: `979`
// Estimated: `6340`
// Minimum execution time: 83_000_000 picoseconds.
Weight::from_parts(83_000_000, 6340)
// Minimum execution time: 52_000_000 picoseconds.
Weight::from_parts(52_000_000, 6340)
.saturating_add(RocksDbWeight::get().reads(8_u64))
.saturating_add(RocksDbWeight::get().writes(6_u64))
}
Expand All @@ -334,8 +367,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `874`
// Estimated: `6340`
// Minimum execution time: 73_000_000 picoseconds.
Weight::from_parts(73_000_000, 6340)
// Minimum execution time: 47_000_000 picoseconds.
Weight::from_parts(47_000_000, 6340)
.saturating_add(RocksDbWeight::get().reads(8_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
Expand All @@ -362,8 +395,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `796 + f * (110 ±0)`
// Estimated: `18483`
// Minimum execution time: 85_000_000 picoseconds.
Weight::from_parts(140_000_000, 18483)
// Minimum execution time: 71_000_000 picoseconds.
Weight::from_parts(93_000_000, 18483)
.saturating_add(RocksDbWeight::get().reads(20_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
Expand All @@ -390,8 +423,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `832 + f * (108 ±0)`
// Estimated: `18483`
// Minimum execution time: 85_000_000 picoseconds.
Weight::from_parts(149_000_000, 18483)
// Minimum execution time: 54_000_000 picoseconds.
Weight::from_parts(94_000_000, 18483)
.saturating_add(RocksDbWeight::get().reads(16_u64))
.saturating_add(RocksDbWeight::get().writes(10_u64))
}
Expand Down Expand Up @@ -422,9 +455,39 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `4754 + f * (235 ±0)`
// Estimated: `54490`
// Minimum execution time: 509_000_000 picoseconds.
Weight::from_parts(533_000_000, 54490)
// Minimum execution time: 322_000_000 picoseconds.
Weight::from_parts(345_000_000, 54490)
.saturating_add(RocksDbWeight::get().reads(49_u64))
.saturating_add(RocksDbWeight::get().writes(46_u64))
}
/// Storage: `BTCRelay::ChainCounter` (r:1 w:0)
/// Proof: `BTCRelay::ChainCounter` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::BlockHeaders` (r:20 w:18)
/// Proof: `BTCRelay::BlockHeaders` (`max_values`: None, `max_size`: Some(200), added: 2675, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::ChainsIndex` (r:3 w:2)
/// Proof: `BTCRelay::ChainsIndex` (`max_values`: None, `max_size`: Some(32), added: 2507, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::DisableDifficultyCheck` (r:1 w:0)
/// Proof: `BTCRelay::DisableDifficultyCheck` (`max_values`: Some(1), `max_size`: Some(1), added: 496, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::ChainsHashes` (r:13 w:24)
/// Proof: `BTCRelay::ChainsHashes` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`)
/// Storage: `Security::ActiveBlockCount` (r:1 w:0)
/// Proof: `Security::ActiveBlockCount` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::ChainWork` (r:2 w:1)
/// Proof: `BTCRelay::ChainWork` (`max_values`: None, `max_size`: Some(80), added: 2555, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::Chains` (r:6 w:0)
/// Proof: `BTCRelay::Chains` (`max_values`: None, `max_size`: Some(24), added: 2499, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::BestBlock` (r:0 w:1)
/// Proof: `BTCRelay::BestBlock` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `BTCRelay::BestBlockHeight` (r:0 w:1)
/// Proof: `BTCRelay::BestBlockHeight` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// The range of component `f` is `[3, 6]`.
fn store_block_header_reorganize_chains_based_on_chainwork (_f: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `5196 + f * (298 ±0)`
// Estimated: `54490`
// Minimum execution time: 328_000_000 picoseconds.
Weight::from_parts(358_000_000, 54490)
.saturating_add(RocksDbWeight::get().reads(47_u64))
.saturating_add(RocksDbWeight::get().writes(47_u64))
}
}
3 changes: 2 additions & 1 deletion crates/btc-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ pub mod pallet {
.max(<T as Config>::WeightInfo::store_block_header_new_fork_sorted(f))
.max(<T as Config>::WeightInfo::store_block_header_new_fork_unsorted(f))
.max(<T as Config>::WeightInfo::store_block_header_reorganize_chains(f))
.max(<T as Config>::WeightInfo::store_block_header_when_updating_chainwork_mandatory())
.max(<T as Config>::WeightInfo::store_block_header_reorganize_chains_based_on_chainwork(f))
.max(<T as Config>::WeightInfo::store_block_header_when_adding_chainwork())
},
DispatchClass::Operational
))]
Expand Down
Loading

0 comments on commit d1e245c

Please sign in to comment.