MrBoec/pallets/slow-clap/src/benchmarking.rs
Uncle Stinky 0bb46482b2
offchain worker restructure and block commitments added
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-11-20 03:06:48 +03:00

100 lines
3.6 KiB
Rust

#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::v1::*;
use frame_support::traits::fungible::Inspect;
use frame_system::RawOrigin;
pub fn create_account<T: Config>() -> T::AccountId {
let account_bytes = Vec::from([1u8; 32]);
T::AccountId::decode(&mut &account_bytes[0..32])
.expect("32 bytes always construct an AccountId32")
}
benchmarks! {
slow_clap {
let minimum_balance = <<T as pallet::Config>::Currency>::minimum_balance();
let receiver = create_account::<T>();
let amount = minimum_balance + minimum_balance;
let network_id = NetworkIdOf::<T>::default();
let session_index = T::ValidatorSet::session_index();
let transaction_hash = H256::repeat_byte(1u8);
let args_hash = Pallet::<T>::generate_unique_hash(&receiver, &amount, &network_id);
let authorities = vec![T::AuthorityId::generate_pair(None)];
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.clone())
.map_err(|()| "more than the maximum number of keys provided")?;
Authorities::<T>::set(&session_index, bounded_authorities);
let authority_index = 0u32;
let clap = Clap {
session_index,
authority_index,
transaction_hash,
block_number: 69,
removed: false,
network_id,
receiver: receiver.clone(),
amount,
};
let authority_id = authorities
.get(authority_index as usize)
.expect("first authority should exist");
let signature = authority_id.sign(&clap.encode())
.ok_or("couldn't make signature")?;
}: _(RawOrigin::None, clap, signature)
verify {
let clap_key = (session_index, transaction_hash, args_hash);
assert_eq!(ReceivedClaps::<T>::get(&clap_key).get(&authority_index).is_some(), true);
assert_eq!(<<T as pallet::Config>::Currency>::total_balance(&receiver), amount);
}
commit_block {
let session_index = T::ValidatorSet::session_index();
let network_id = NetworkIdOf::<T>::default();
let authorities = vec![T::AuthorityId::generate_pair(None)];
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.clone())
.map_err(|()| "more than the maximum number of keys provided")?;
Authorities::<T>::set(&session_index, bounded_authorities);
let authority_index = 0u32;
let block_commitment = BlockCommitment {
session_index,
authority_index,
network_id,
commitment: CommitmentDetails {
last_registered_block: 69,
last_seen_block: 420,
last_updated: 1337,
}
};
let authority_id = authorities
.get(authority_index as usize)
.expect("first authority should exist");
let signature = authority_id.sign(&block_commitment.encode())
.ok_or("couldn't make signature")?;
}: _(RawOrigin::None, block_commitment, signature)
verify {
let stored_commitment = BlockCommitments::<T>::get(&network_id)
.get(&authority_index)
.cloned()
.unwrap_or_default();
assert_eq!(stored_commitment.last_registered_block, 69);
assert_eq!(stored_commitment.last_seen_block, 420);
assert_eq!(stored_commitment.last_updated, 1337);
}
impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(),
crate::mock::Runtime,
);
}