Skip to content

Commit 1e78244

Browse files
committed
feat: add benchmarks for withdraw_tokens extrinsic
1 parent b57132e commit 1e78244

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

pallets/msa/src/benchmarking.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::types::EMPTY_FUNCTION;
55
#[allow(unused)]
66
use crate::Pallet as Msa;
77
use frame_benchmarking::{account, v2::*};
8-
use frame_support::assert_ok;
8+
use frame_support::{assert_ok, traits::fungible::Inspect};
99
use frame_system::RawOrigin;
1010
use sp_core::{crypto::KeyTypeId, Encode};
1111
use sp_runtime::RuntimeAppPublic;
@@ -369,6 +369,44 @@ mod benchmarks {
369369
Ok(())
370370
}
371371

372+
#[benchmark]
373+
fn withdraw_tokens() -> Result<(), BenchmarkError> {
374+
prep_signature_registry::<T>();
375+
376+
let (msa_public_key, msa_key_pair, msa_id) =
377+
create_msa_account_and_keys::<T>();
378+
379+
let eth_account_id: H160 = Msa::<T>::msa_id_to_eth_address(msa_id);
380+
let mut bytes = &EthereumAddressMapper::to_bytes32(&eth_account_id.0)[..];
381+
let msa_account_id = <T as frame_system::Config>::AccountId::decode(&mut bytes).unwrap();
382+
383+
// Fund MSA
384+
// let balance = <<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance.from(10_000_000u128);
385+
let balance = <T as Config>::Currency::minimum_balance();
386+
T::Currency::set_balance(&msa_account_id, balance);
387+
assert_eq!(T::Currency::balance(&msa_account_id), balance);
388+
389+
let (add_key_payload, _, new_account_id) =
390+
add_key_payload_and_signature::<T>(msa_id);
391+
392+
let encoded_add_key_payload = wrap_binary_data(add_key_payload.encode());
393+
let owner_signature = MultiSignature::Sr25519(
394+
msa_key_pair.sign(&encoded_add_key_payload).unwrap().into(),
395+
);
396+
397+
#[extrinsic_call]
398+
_(
399+
RawOrigin::Signed(new_account_id.clone()),
400+
msa_public_key.clone(),
401+
owner_signature,
402+
add_key_payload,
403+
);
404+
405+
assert_eq!(T::Currency::balance(&msa_account_id), Zero::zero());
406+
Ok(())
407+
}
408+
409+
372410
impl_benchmark_test_suite!(
373411
Msa,
374412
crate::tests::mock::new_test_ext_keystore(),

pallets/msa/src/tests/msa_token_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ fn it_succeeds_when_balance_is_sufficient() {
190190
let (msa_id, owner_key_pair) = create_account();
191191
let (origin_key_pair, _) = sr25519::Pair::generate();
192192
let eth_account_id: H160 = Msa::msa_id_to_eth_address(msa_id);
193-
let bytes = EthereumAddressMapper::to_bytes32(&eth_account_id.0);
194-
let msa_account_id = <Test as frame_system::Config>::AccountId::from(bytes.clone());
193+
let bytes: [u8; 32] = EthereumAddressMapper::to_bytes32(&eth_account_id.0);
194+
let msa_account_id = <Test as frame_system::Config>::AccountId::from(bytes);
195195

196196
let (payload, _, msa_signature) = generate_payload(msa_id, &owner_key_pair, &origin_key_pair, None);
197197

pallets/msa/src/weights.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,22 @@ impl WeightInfo for () {
438438
// Minimum execution time: 4_000_000 picoseconds.
439439
Weight::from_parts(8_000_000, 0)
440440
}
441+
/// Storage: `Msa::PayloadSignatureRegistryList` (r:2 w:2)
442+
/// Proof: `Msa::PayloadSignatureRegistryList` (`max_values`: Some(50000), `max_size`: Some(144), added: 2124, mode: `MaxEncodedLen`)
443+
/// Storage: `Msa::PayloadSignatureRegistryPointer` (r:1 w:1)
444+
/// Proof: `Msa::PayloadSignatureRegistryPointer` (`max_values`: Some(1), `max_size`: Some(140), added: 635, mode: `MaxEncodedLen`)
445+
/// Storage: `Msa::PublicKeyToMsaId` (r:1 w:0)
446+
/// Proof: `Msa::PublicKeyToMsaId` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
447+
/// Storage: `System::Account` (r:2 w:2)
448+
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
441449
fn withdraw_tokens() -> Weight {
442450
// Proof Size summary in bytes:
443-
// Measured: `0`
444-
// Estimated: `0`
445-
// Minimum execution time: 4_000_000 picoseconds.
446-
Weight::from_parts(8_000_000, 0)
451+
// Measured: `1270`
452+
// Estimated: `6691`
453+
// Minimum execution time: 151_000_000 picoseconds.
454+
Weight::from_parts(162_000_000, 6691)
455+
.saturating_add(RocksDbWeight::get().reads(6_u64))
456+
.saturating_add(RocksDbWeight::get().writes(5_u64))
447457
}
448458
}
449459

@@ -602,4 +612,16 @@ mod tests {
602612
> 4107
603613
);
604614
}
615+
#[test]
616+
fn test_withdraw_tokens() {
617+
assert!(
618+
BlockWeights::get()
619+
.per_class
620+
.get(frame_support::dispatch::DispatchClass::Normal)
621+
.max_extrinsic
622+
.unwrap_or_else(<Weight as sp_runtime::traits::Bounded>::max_value)
623+
.proof_size()
624+
> 6691
625+
);
626+
}
605627
}

0 commit comments

Comments
 (0)