|
3 | 3 | use super::*; |
4 | 4 |
|
5 | 5 | #[allow(unused)] |
6 | | -use crate::Pallet as Template; |
7 | | -use frame_benchmarking::{benchmarks, whitelisted_caller}; |
8 | 6 | use frame_system::RawOrigin; |
| 7 | +use crate::Pallet as MultiSend; |
| 8 | +use frame_benchmarking::{account, benchmarks}; |
| 9 | +use frame_support::{inherent::Vec, traits::Currency}; |
| 10 | + |
| 11 | +const SEED: u32 = 0; |
| 12 | +const NUMBER_OF_RECEIVING_ACCOUNTS: u32 = 50; |
| 13 | + |
| 14 | +/// Grab a funded user. |
| 15 | +fn create_funded_user<T: Config>( |
| 16 | + string: &'static str, |
| 17 | + n: u32, |
| 18 | + balance_factor: u32, |
| 19 | +) -> T::AccountId { |
| 20 | + let user = account(string, n, SEED); |
| 21 | + let balance = T::Currency::minimum_balance() * balance_factor.into(); |
| 22 | + let _ = T::Currency::make_free_balance_be(&user, balance); |
| 23 | + user |
| 24 | +} |
| 25 | + |
| 26 | +// Loop and do the following an arbitrary number of times (e.g n = 100), each time measuring the execution time |
| 27 | +// Setup |
| 28 | +// -- Create and fund the sending account with (n+1) * 10 tokens, this allows it to send 10 tokens to n accounts and still have some tokens leftover |
| 29 | +// -- Create n number of receiving accounts with an existensial deposit |
| 30 | + |
| 31 | +// Action |
| 32 | +// -- Call the send_tokens_to_multiple_receivers extrinsic from the pallet |
| 33 | + |
| 34 | +// Verify |
| 35 | +// -- Ascertain that the sending account only has 10 tokens left |
| 36 | +// -- Ascertain that the receiving accounts all received their 10 tokens |
9 | 37 |
|
10 | 38 | benchmarks! { |
11 | | - do_something { |
12 | | - let s in 0 .. 100; |
13 | | - let caller: T::AccountId = whitelisted_caller(); |
14 | | - }: _(RawOrigin::Signed(caller), s) |
| 39 | + send_tokens_to_multiple_receivers { |
| 40 | + let sender = create_funded_user::<T>("user", SEED, (NUMBER_OF_RECEIVING_ACCOUNTS + 1) * 100); |
| 41 | + let sender_initial_balance = T::Currency::free_balance(&sender); |
| 42 | + let mut token_transfer_requests = Vec::new(); |
| 43 | + |
| 44 | + for i in 1 .. NUMBER_OF_RECEIVING_ACCOUNTS { |
| 45 | + token_transfer_requests.push( |
| 46 | + TokenTransferRequest { |
| 47 | + receiver_account: create_funded_user::<T>("receiver", i, 1), |
| 48 | + token_amount: T::Currency::minimum_balance() + 10u32.into() } // Send 10 tokens |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + }: _(RawOrigin::Signed(sender.clone()), token_transfer_requests.clone()) |
15 | 53 | verify { |
16 | | - assert_eq!(Something::<T>::get(), Some(s)); |
| 54 | + assert_eq!(true, T::Currency::free_balance(&sender) < sender_initial_balance); |
| 55 | + for request in token_transfer_requests { |
| 56 | + assert_eq!(T::Currency::minimum_balance() + T::Currency::minimum_balance() + 10u32.into(), T::Currency::free_balance(&request.receiver_account)) // expect 11 tokens = 1 minimum_balance + 10 sent |
| 57 | + } |
17 | 58 | } |
18 | 59 |
|
19 | | - impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test); |
| 60 | + impl_benchmark_test_suite!(MultiSend, crate::mock::new_test_ext(), crate::mock::Test); |
20 | 61 | } |
0 commit comments