referrerpolicy=no-referrer-when-downgrade

polkadot_runtime_common/auctions/
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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Benchmarking for auctions pallet

#![cfg(feature = "runtime-benchmarks")]
use super::{Pallet as Auctions, *};
use frame_support::{
	assert_ok,
	traits::{EnsureOrigin, OnInitialize},
};
use frame_system::RawOrigin;
use polkadot_runtime_parachains::paras;
use sp_runtime::{traits::Bounded, SaturatedConversion};

use frame_benchmarking::v2::*;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
	let events = frame_system::Pallet::<T>::events();
	let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
	// compare to the last event record
	let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
	assert_eq!(event, &system_event);
}

fn fill_winners<T: Config + paras::Config>(lease_period_index: LeasePeriodOf<T>) {
	let auction_index = AuctionCounter::<T>::get();
	let minimum_balance = CurrencyOf::<T>::minimum_balance();

	for n in 1..=SlotRange::SLOT_RANGE_COUNT as u32 {
		let owner = account("owner", n, 0);
		let worst_validation_code = T::Registrar::worst_validation_code();
		let worst_head_data = T::Registrar::worst_head_data();
		CurrencyOf::<T>::make_free_balance_be(&owner, BalanceOf::<T>::max_value());

		assert!(T::Registrar::register(
			owner,
			ParaId::from(n),
			worst_head_data,
			worst_validation_code
		)
		.is_ok());
	}
	assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
		frame_system::Origin::<T>::Root.into(),
		T::Registrar::worst_validation_code(),
	));

	T::Registrar::execute_pending_transitions();

	for n in 1..=SlotRange::SLOT_RANGE_COUNT as u32 {
		let bidder = account("bidder", n, 0);
		CurrencyOf::<T>::make_free_balance_be(&bidder, BalanceOf::<T>::max_value());

		let slot_range = SlotRange::n((n - 1) as u8).unwrap();
		let (start, end) = slot_range.as_pair();

		assert!(Auctions::<T>::bid(
			RawOrigin::Signed(bidder).into(),
			ParaId::from(n),
			auction_index,
			lease_period_index + start.into(),        // First Slot
			lease_period_index + end.into(),          // Last slot
			minimum_balance.saturating_mul(n.into()), // Amount
		)
		.is_ok());
	}
}

#[benchmarks(
		where T: pallet_babe::Config + paras::Config,
	)]
mod benchmarks {
	use super::*;

	#[benchmark]
	fn new_auction() -> Result<(), BenchmarkError> {
		let duration = BlockNumberFor::<T>::max_value();
		let lease_period_index = LeasePeriodOf::<T>::max_value();
		let origin =
			T::InitiateOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

		#[extrinsic_call]
		_(origin as T::RuntimeOrigin, duration, lease_period_index);

		assert_last_event::<T>(
			Event::<T>::AuctionStarted {
				auction_index: AuctionCounter::<T>::get(),
				lease_period: LeasePeriodOf::<T>::max_value(),
				ending: BlockNumberFor::<T>::max_value(),
			}
			.into(),
		);

		Ok(())
	}

	// Worst case scenario a new bid comes in which kicks out an existing bid for the same slot.
	#[benchmark]
	fn bid() -> Result<(), BenchmarkError> {
		// If there is an offset, we need to be on that block to be able to do lease things.
		let (_, offset) = T::Leaser::lease_period_length();
		frame_system::Pallet::<T>::set_block_number(offset + One::one());

		// Create a new auction
		let duration = BlockNumberFor::<T>::max_value();
		let lease_period_index = LeasePeriodOf::<T>::zero();
		let origin = T::InitiateOrigin::try_successful_origin()
			.expect("InitiateOrigin has no successful origin required for the benchmark");
		Auctions::<T>::new_auction(origin, duration, lease_period_index)?;

		let para = ParaId::from(0);
		let new_para = ParaId::from(1_u32);

		// Register the paras
		let owner = account("owner", 0, 0);
		CurrencyOf::<T>::make_free_balance_be(&owner, BalanceOf::<T>::max_value());
		let worst_head_data = T::Registrar::worst_head_data();
		let worst_validation_code = T::Registrar::worst_validation_code();
		T::Registrar::register(
			owner.clone(),
			para,
			worst_head_data.clone(),
			worst_validation_code.clone(),
		)?;
		T::Registrar::register(owner, new_para, worst_head_data, worst_validation_code.clone())?;
		assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
			frame_system::Origin::<T>::Root.into(),
			worst_validation_code,
		));

		T::Registrar::execute_pending_transitions();

		// Make an existing bid
		let auction_index = AuctionCounter::<T>::get();
		let first_slot = AuctionInfo::<T>::get().unwrap().0;
		let last_slot = first_slot + 3u32.into();
		let first_amount = CurrencyOf::<T>::minimum_balance();
		let first_bidder: T::AccountId = account("first_bidder", 0, 0);
		CurrencyOf::<T>::make_free_balance_be(&first_bidder, BalanceOf::<T>::max_value());
		Auctions::<T>::bid(
			RawOrigin::Signed(first_bidder.clone()).into(),
			para,
			auction_index,
			first_slot,
			last_slot,
			first_amount,
		)?;

		let caller: T::AccountId = whitelisted_caller();
		CurrencyOf::<T>::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
		let bigger_amount = CurrencyOf::<T>::minimum_balance().saturating_mul(10u32.into());
		assert_eq!(CurrencyOf::<T>::reserved_balance(&first_bidder), first_amount);

		#[extrinsic_call]
		_(
			RawOrigin::Signed(caller.clone()),
			new_para,
			auction_index,
			first_slot,
			last_slot,
			bigger_amount,
		);

		// Confirms that we unreserved funds from a previous bidder, which is worst case
		// scenario.
		assert_eq!(CurrencyOf::<T>::reserved_balance(&caller), bigger_amount);

		Ok(())
	}

	// Worst case: 10 bidders taking all wining spots, and we need to calculate the winner for
	// auction end. Entire winner map should be full and removed at the end of the benchmark.
	#[benchmark]
	fn on_initialize() -> Result<(), BenchmarkError> {
		// If there is an offset, we need to be on that block to be able to do lease things.
		let (lease_length, offset) = T::Leaser::lease_period_length();
		frame_system::Pallet::<T>::set_block_number(offset + One::one());

		// Create a new auction
		let duration: BlockNumberFor<T> = lease_length / 2u32.into();
		let lease_period_index = LeasePeriodOf::<T>::zero();
		let now = frame_system::Pallet::<T>::block_number();
		let origin = T::InitiateOrigin::try_successful_origin()
			.expect("InitiateOrigin has no successful origin required for the benchmark");
		Auctions::<T>::new_auction(origin, duration, lease_period_index)?;

		fill_winners::<T>(lease_period_index);

		for winner in Winning::<T>::get(BlockNumberFor::<T>::from(0u32)).unwrap().iter() {
			assert!(winner.is_some());
		}

		let winning_data = Winning::<T>::get(BlockNumberFor::<T>::from(0u32)).unwrap();
		// Make winning map full
		for i in 0u32..(T::EndingPeriod::get() / T::SampleLength::get()).saturated_into() {
			Winning::<T>::insert(BlockNumberFor::<T>::from(i), winning_data.clone());
		}

		// Move ahead to the block we want to initialize
		frame_system::Pallet::<T>::set_block_number(duration + now + T::EndingPeriod::get());

		// Trigger epoch change for new random number value:
		{
			pallet_babe::EpochStart::<T>::set((Zero::zero(), u32::MAX.into()));
			pallet_babe::Pallet::<T>::on_initialize(duration + now + T::EndingPeriod::get());
			let authorities = pallet_babe::Pallet::<T>::authorities();
			// Check for non empty authority set since it otherwise emits a No-OP warning.
			if !authorities.is_empty() {
				pallet_babe::Pallet::<T>::enact_epoch_change(
					authorities.clone(),
					authorities,
					None,
				);
			}
		}

		#[block]
		{
			Auctions::<T>::on_initialize(duration + now + T::EndingPeriod::get());
		}

		let auction_index = AuctionCounter::<T>::get();
		assert_last_event::<T>(Event::<T>::AuctionClosed { auction_index }.into());
		assert!(Winning::<T>::iter().count().is_zero());

		Ok(())
	}

	// Worst case: 10 bidders taking all wining spots, and winning data is full.
	#[benchmark]
	fn cancel_auction() -> Result<(), BenchmarkError> {
		// If there is an offset, we need to be on that block to be able to do lease things.
		let (lease_length, offset) = T::Leaser::lease_period_length();
		frame_system::Pallet::<T>::set_block_number(offset + One::one());

		// Create a new auction
		let duration: BlockNumberFor<T> = lease_length / 2u32.into();
		let lease_period_index = LeasePeriodOf::<T>::zero();
		let origin = T::InitiateOrigin::try_successful_origin()
			.expect("InitiateOrigin has no successful origin required for the benchmark");
		Auctions::<T>::new_auction(origin, duration, lease_period_index)?;

		fill_winners::<T>(lease_period_index);

		let winning_data = Winning::<T>::get(BlockNumberFor::<T>::from(0u32)).unwrap();
		for winner in winning_data.iter() {
			assert!(winner.is_some());
		}

		// Make winning map full
		for i in 0u32..(T::EndingPeriod::get() / T::SampleLength::get()).saturated_into() {
			Winning::<T>::insert(BlockNumberFor::<T>::from(i), winning_data.clone());
		}
		assert!(AuctionInfo::<T>::get().is_some());

		#[extrinsic_call]
		_(RawOrigin::Root);

		assert!(AuctionInfo::<T>::get().is_none());
		Ok(())
	}

	impl_benchmark_test_suite!(
		Auctions,
		crate::integration_tests::new_test_ext(),
		crate::integration_tests::Test,
	);
}