-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathbenchmarking.rs
153 lines (128 loc) · 4.67 KB
/
benchmarking.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// This file is part of Astar.
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.
use super::{Pallet as Migration, *};
use frame_benchmarking::{account as benchmark_account, v2::*};
use frame_support::{assert_ok, traits::Currency};
use sp_std::prelude::*;
/// Generate an unique smart contract using the provided index as a sort-of indetifier
fn smart_contract<T: pallet_dapps_staking::Config>(index: u8) -> T::SmartContract {
// This is a hacky approach to provide different smart contracts without touching the smart contract trait.
let mut encoded_smart_contract = T::SmartContract::default().encode();
*encoded_smart_contract.last_mut().unwrap() = index;
Decode::decode(&mut TrailingZeroInput::new(encoded_smart_contract.as_ref()))
.expect("Shouldn't occur as long as EVM is the default type.")
}
/// Initialize the old dApp staking pallet with some storage.
pub(super) fn initial_config<T: Config>()
where
BlockNumberFor<T>: IsType<BlockNumber>,
{
let dapps_number = <T as pallet_dapp_staking_v3::Config>::MaxNumberOfContracts::get();
let dapps_number = (dapps_number as u8).min(100);
// Add some dummy dApps to the old pallet.
for idx in 0..dapps_number {
let developer: T::AccountId = benchmark_account("developer", idx.into(), 123);
<T as pallet_dapps_staking::Config>::Currency::make_free_balance_be(
&developer,
<T as pallet_dapps_staking::Config>::RegisterDeposit::get() * 2,
);
let smart_contract = smart_contract::<T>(idx);
assert_ok!(pallet_dapps_staking::Pallet::<T>::register(
RawOrigin::Root.into(),
developer,
smart_contract.clone(),
));
let staker: T::AccountId = benchmark_account("staker", idx.into(), 123);
let lock_amount = <T as pallet_dapps_staking::Config>::MinimumStakingAmount::get()
.max(<T as pallet_dapp_staking_v3::Config>::MinimumLockedAmount::get());
<T as pallet_dapps_staking::Config>::Currency::make_free_balance_be(
&staker,
lock_amount * 100,
);
assert_ok!(pallet_dapps_staking::Pallet::<T>::bond_and_stake(
RawOrigin::Signed(staker.clone()).into(),
smart_contract,
lock_amount,
));
}
}
#[benchmarks(
where
BlockNumberFor<T>: IsType<BlockNumber>,
)]
mod benchmarks {
use super::*;
#[benchmark]
fn migrate_dapps_success() {
initial_config::<T>();
#[block]
{
assert!(Migration::<T>::migrate_dapps().is_ok());
}
}
#[benchmark]
fn migrate_dapps_noop() {
#[block]
{
assert!(Migration::<T>::migrate_dapps().is_err());
}
}
#[benchmark]
fn migrate_ledger_success() {
initial_config::<T>();
#[block]
{
assert!(Migration::<T>::migrate_ledger().is_ok());
}
}
#[benchmark]
fn migrate_ledger_noop() {
#[block]
{
assert!(Migration::<T>::migrate_ledger().is_err());
}
}
#[benchmark]
fn cleanup_old_storage_success(x: Linear<1, 5>) {
initial_config::<T>();
#[block]
{
// TODO: for some reason, tests always fail here, nothing gets removed from storage.
// When tested against real runtime, it works just fine.
let _ = Migration::<T>::cleanup_old_storage(x.into());
}
}
#[benchmark]
fn cleanup_old_storage_noop() {
let hashed_prefix = twox_128(pallet_dapps_staking::Pallet::<T>::name().as_bytes());
let _ = clear_prefix(&hashed_prefix, None);
#[block]
{
assert!(Migration::<T>::cleanup_old_storage(1).is_err());
}
}
impl_benchmark_test_suite!(
Pallet,
crate::benchmarking::tests::new_test_ext(),
crate::mock::Test,
);
}
#[cfg(test)]
mod tests {
use crate::mock;
use sp_io::TestExternalities;
pub fn new_test_ext() -> TestExternalities {
mock::ExtBuilder::build()
}
}