Skip to content

Commit

Permalink
chore: added benchmarking.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Sep 26, 2023
1 parent 1745f62 commit 3491cf9
Show file tree
Hide file tree
Showing 7 changed files with 750 additions and 552 deletions.
40 changes: 40 additions & 0 deletions crates/btc-relay/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,46 @@ pub mod benchmarks {
_(RawOrigin::Signed(caller), block.header, height);
}

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

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

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

let block = add_new_block_to_relay::<T>(caller.clone(), init_block_hash, 0);

#[extrinsic_call]
_(RawOrigin::Signed(caller), block.header.hash);

// make sure chain work is stored
assert_eq!(BtcRelay::<T>::contains_chainwork(block.header.hash), true);
}

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

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

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

let block = new_block::<T>(init_block_hash, 0);

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

// make sure block is stored
let rich_header = BtcRelay::<T>::get_block_header_from_hash(block.header.hash).unwrap();
assert_eq!(rich_header.chain_id, MAIN_CHAIN_ID);

// make sure chain work is stored
assert_eq!(BtcRelay::<T>::contains_chainwork(block.header.hash), true);
}

#[benchmark]
pub fn store_block_header() {
let caller: T::AccountId = whitelisted_caller();
Expand Down
606 changes: 342 additions & 264 deletions crates/btc-relay/src/default_weights.rs

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions crates/btc-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ pub mod pallet {
Ok(Pays::No.into())
}

// ToDo: Add benchmarking for `store_block_header_reorganize_based_on_work`
/// Stores a single new block header
///
/// # Arguments
Expand All @@ -163,6 +162,7 @@ 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())
},
DispatchClass::Operational
))]
Expand Down Expand Up @@ -192,10 +192,9 @@ pub mod pallet {
}

/// Stores a chainwork for block header
// ToDo: Benchmarking needs to be performed
#[pallet::call_index(2)]
#[pallet::weight((
<T as Config>::WeightInfo::initialize(),
<T as Config>::WeightInfo::set_chainwork_for_block(),
DispatchClass::Operational
))]
#[transactional]
Expand Down Expand Up @@ -813,7 +812,7 @@ impl<T: Config> Pallet<T> {
ChainCounter::<T>::get()
}

fn contains_chainwork(block_hash: H256Le) -> bool {
pub fn contains_chainwork(block_hash: H256Le) -> bool {
if let None = ChainWork::<T>::get(block_hash) {
false
} else {
Expand Down
10 changes: 8 additions & 2 deletions crates/btc-relay/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ pub mod v1 {
let current_storage_version = Pallet::<T>::current_storage_version();
let _expected_storage_version = StorageVersion::new(0);
if matches!(current_storage_version, _expected_storage_version) {
//Fixme: insert latest btc block as well as the calculated chain work. But for testnet the
// Fixme: insert latest btc block as well as the calculated chain work. But for testnet the
// block header should be different.
ChainWork::<T>::insert(H256Le::zero(), U256::zero());
ChainWork::<T>::insert(
H256Le::from_bytes_le(&[
177, 89, 206, 70, 83, 47, 12, 29, 30, 21, 192, 96, 38, 114, 155, 10, 5, 77, 59, 247, 14, 99, 150,
79, 228, 250, 72, 71, 124, 92, 197, 19,
]),
U256::zero(),
);
}
StorageVersion::new(1).put::<Pallet<T>>();
weight
Expand Down
7 changes: 5 additions & 2 deletions crates/btc-relay/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ mod chainwork {

let mut genesis_block = sample_block_header();
genesis_block.nonce = 11;
genesis_block.hash = H256Le::zero();
genesis_block.hash = H256Le::from_bytes_le(&[
177, 89, 206, 70, 83, 47, 12, 29, 30, 21, 192, 96, 38, 114, 155, 10, 5, 77, 59, 247, 14, 99, 150, 79,
228, 250, 72, 71, 124, 92, 197, 19,
]);

let block_height = 0;
assert_ok!(BTCRelay::_initialize(3, genesis_block, block_height));
Expand Down Expand Up @@ -352,7 +355,7 @@ mod chainwork {

// Step 6: Perform a reorg based on chainwork
let mut block_14 = from_prev(31 + 12, prev_hash);
// update target so that the next block chain work is more that the best block.
// update target so that the next block chain work calculation is more that the best block.
block_14.target = U256::max_value();
prev_hash = block_14.hash;
assert_ok!(BTCRelay::_store_block_header(&3, block_14));
Expand Down
Loading

0 comments on commit 3491cf9

Please sign in to comment.