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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2021-2023 Axiom-Team
//
// This file is part of Duniter-v2S.
//
// Duniter-v2S is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Duniter-v2S 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::{account, v2::*};
use sp_runtime::traits::One;

fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
    frame_system::Pallet::<T>::assert_has_event(generic_event.into());
}

#[benchmarks(
        where
            IdtyId<T>: From<u32>,
            BalanceOf<T>: From<u64>,
            T::AccountId: From<[u8; 32]>,
)]
mod benchmarks {
    use super::*;

    #[benchmark]
    fn queue_refund() {
        let account: T::AccountId = account("Alice", 1, 1);
        let dummy_refund = Refund {
            account: account.clone(),
            identity: 0u32.into(),
            amount: 20u64.into(),
        };
        let refund = Refund {
            account,
            identity: 1u32.into(),
            amount: 10u64.into(),
        };
        // Complexity is bound to MAX_QUEUD_REFUNDS where an insertion is O(n-1)
        for _ in 0..MAX_QUEUED_REFUNDS - 1 {
            Pallet::<T>::queue_refund(dummy_refund.clone())
        }

        #[block]
        {
            Pallet::<T>::queue_refund(refund.clone());
        }

        assert_eq!(RefundQueue::<T>::get().last(), Some(refund).as_ref());
        assert_eq!(RefundQueue::<T>::get().len() as u32, MAX_QUEUED_REFUNDS);
    }

    #[benchmark]
    fn spend_quota() {
        let idty_id: IdtyId<T> = 1u32.into();
        let amount = 2u64;
        let quota_amount = 10u64;
        IdtyQuota::<T>::insert(
            idty_id,
            Quota {
                last_use: BlockNumberFor::<T>::zero(),
                amount: quota_amount.into(),
            },
        );

        #[block]
        {
            Pallet::<T>::spend_quota(idty_id, amount.into());
        }

        let quota_growth =
            sp_runtime::Perbill::from_rational(BlockNumberFor::<T>::one(), T::ReloadRate::get())
                .mul_floor(T::MaxQuota::get());
        assert_eq!(
            IdtyQuota::<T>::get(idty_id).unwrap().amount,
            quota_growth + quota_amount.into() - amount.into()
        );
    }

    #[benchmark]
    fn try_refund() {
        let account: T::AccountId = account("Alice", 1, 1);
        let idty_id: IdtyId<T> = 1u32.into();
        IdtyQuota::<T>::insert(
            idty_id,
            Quota {
                last_use: BlockNumberFor::<T>::zero(),
                amount: 10u64.into(),
            },
        );
        let _ = CurrencyOf::<T>::make_free_balance_be(&T::RefundAccount::get(), u32::MAX.into());
        // The worst-case scenario is when the refund fails
        // and can only be triggered if the account is dead,
        // in this case by having no balance in the account.
        let refund = Refund {
            account: account.clone(),
            identity: 1u32.into(),
            amount: 10u64.into(),
        };

        #[block]
        {
            Pallet::<T>::try_refund(refund);
        }

        assert_has_event::<T>(Event::<T>::RefundFailed(account).into());
    }

    #[benchmark]
    fn do_refund() {
        let account: T::AccountId = account("Alice", 1, 1);
        let _ = CurrencyOf::<T>::make_free_balance_be(&T::RefundAccount::get(), u32::MAX.into());
        // The worst-case scenario is when the refund fails
        // and can only be triggered if the account is dead,
        // in this case by having no balance in the account.
        let refund = Refund {
            account: account.clone(),
            identity: 1u32.into(),
            amount: 10u64.into(),
        };

        #[block]
        {
            Pallet::<T>::try_refund(refund);
        }

        assert_has_event::<T>(Event::<T>::RefundFailed(account).into());
    }

    #[benchmark]
    fn on_process_refund_queue() {
        // The base weight consumed on processing refund queue when empty.
        assert_eq!(RefundQueue::<T>::get().len() as u32, 0);

        #[block]
        {
            Pallet::<T>::process_refund_queue(Weight::MAX);
        }
    }

    #[benchmark]
    fn on_process_refund_queue_elements(i: Linear<1, MAX_QUEUED_REFUNDS>) {
        // The weight consumed on processing refund queue with one element.
        // Can deduce the process_refund_queue overhead by subtracting try_refund weight.
        let account: T::AccountId = account("Alice", 1, 1);
        let idty_id: IdtyId<T> = 1u32.into();
        IdtyQuota::<T>::insert(
            idty_id,
            Quota {
                last_use: BlockNumberFor::<T>::zero(),
                amount: 10u64.into(),
            },
        );
        let _ = CurrencyOf::<T>::make_free_balance_be(&T::RefundAccount::get(), u32::MAX.into());
        // The worst-case scenario is when the refund fails
        // and can only be triggered if the account is dead,
        // in this case by having no balance in the account.
        let refund = Refund {
            account: account.clone(),
            identity: 1u32.into(),
            amount: 10u64.into(),
        };
        for _ in 0..i {
            Pallet::<T>::queue_refund(refund.clone());
        }
        assert_eq!(RefundQueue::<T>::get().len() as u32, i);

        #[block]
        {
            Pallet::<T>::process_refund_queue(Weight::MAX);
        }

        assert_eq!(RefundQueue::<T>::get().len() as u32, 0);
        assert_has_event::<T>(Event::<T>::RefundFailed(account).into());
    }

    impl_benchmark_test_suite!(
        Pallet,
        crate::mock::new_test_ext(crate::mock::QuotaConfig {
            identities: vec![1, 2]
        }),
        crate::mock::Test
    );
}