pallet-staking-async 0.13.0

FRAME pallet staking async
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{
	reward::EraRewardManager,
	session_rotation::{Eras, Rotator},
	tests::session_mock::{CurrentIndex, Timestamp},
	POT_POOL_SIZE,
};
use codec::Encode;
use frame_support::{
	traits::fungible::{Inspect, Mutate},
	PalletId,
};

use super::*;

#[test]
fn forcing_force_none() {
	ExtBuilder::default().build_and_execute(|| {
		ForceEra::<T>::put(Forcing::ForceNone);

		Session::roll_to_next_session();
		assert_eq!(
			staking_events_since_last_call(),
			vec![Event::SessionRotated { starting_session: 4, active_era: 1, planned_era: 1 }]
		);

		Session::roll_to_next_session();
		assert_eq!(
			staking_events_since_last_call(),
			vec![Event::SessionRotated { starting_session: 5, active_era: 1, planned_era: 1 }]
		);

		Session::roll_to_next_session();
		assert_eq!(
			staking_events_since_last_call(),
			vec![Event::SessionRotated { starting_session: 6, active_era: 1, planned_era: 1 }]
		);

		Session::roll_to_next_session();
		assert_eq!(
			staking_events_since_last_call(),
			vec![Event::SessionRotated { starting_session: 7, active_era: 1, planned_era: 1 }]
		);

		Session::roll_to_next_session();
		assert_eq!(
			staking_events_since_last_call(),
			vec![Event::SessionRotated { starting_session: 8, active_era: 1, planned_era: 1 }]
		);
	});
}

#[test]
fn forcing_no_forcing_default() {
	ExtBuilder::default().build_and_execute(|| {
		// default value, setting it again just for read-ability.
		ForceEra::<T>::put(Forcing::NotForcing);

		Session::roll_until_active_era(2);
		assert_eq!(
			staking_events_since_last_call(),
			vec![
				Event::SessionRotated { starting_session: 4, active_era: 1, planned_era: 2 },
				Event::PagedElectionProceeded { page: 0, result: Ok(2) },
				Event::SessionRotated { starting_session: 5, active_era: 1, planned_era: 2 },
				Event::EraPaid { era_index: 1, validator_payout: 7500, remainder: 0 },
				Event::SessionRotated { starting_session: 6, active_era: 2, planned_era: 2 }
			]
		);
	});
}

#[test]
fn forcing_force_always() {
	ExtBuilder::default()
		.session_per_era(6)
		.no_flush_events()
		.build_and_execute(|| {
			// initial events thus far, without `ForceAlways` set.
			assert_eq!(
				staking_events_since_last_call(),
				vec![
					Event::SessionRotated { starting_session: 1, active_era: 0, planned_era: 0 },
					Event::SessionRotated { starting_session: 2, active_era: 0, planned_era: 0 },
					Event::SessionRotated { starting_session: 3, active_era: 0, planned_era: 0 },
					Event::SessionRotated { starting_session: 4, active_era: 0, planned_era: 1 },
					Event::PagedElectionProceeded { page: 0, result: Ok(2) },
					Event::SessionRotated { starting_session: 5, active_era: 0, planned_era: 1 },
					Event::EraPaid { era_index: 0, validator_payout: 15000, remainder: 0 },
					Event::SessionRotated { starting_session: 6, active_era: 1, planned_era: 1 }
				]
			);

			// but with it set..
			ForceEra::<T>::put(Forcing::ForceAlways);

			Session::roll_until_active_era(2);
			assert_eq!(
				staking_events_since_last_call(),
				vec![
					// we immediately plan a new era as soon as the first session report comes in
					Event::SessionRotated { starting_session: 7, active_era: 1, planned_era: 2 },
					Event::PagedElectionProceeded { page: 0, result: Ok(2) },
					// by now it is given to mock session, and is buffered
					Event::SessionRotated { starting_session: 8, active_era: 1, planned_era: 2 },
					// and by now it is activated. Note how the validator payout is less, since the
					// era duration is less. Note that we immediately plan the next era as well.
					Event::EraPaid { era_index: 1, validator_payout: 7500, remainder: 0 },
					Event::SessionRotated { starting_session: 9, active_era: 2, planned_era: 3 }
				]
			);
		});
}

#[test]
fn forcing_force_new() {
	ExtBuilder::default()
		.session_per_era(6)
		.no_flush_events()
		.build_and_execute(|| {
			// initial events thus far, without `ForceAlways` set.
			assert_eq!(
				staking_events_since_last_call(),
				vec![
					Event::SessionRotated { starting_session: 1, active_era: 0, planned_era: 0 },
					Event::SessionRotated { starting_session: 2, active_era: 0, planned_era: 0 },
					Event::SessionRotated { starting_session: 3, active_era: 0, planned_era: 0 },
					Event::SessionRotated { starting_session: 4, active_era: 0, planned_era: 1 },
					Event::PagedElectionProceeded { page: 0, result: Ok(2) },
					Event::SessionRotated { starting_session: 5, active_era: 0, planned_era: 1 },
					Event::EraPaid { era_index: 0, validator_payout: 15000, remainder: 0 },
					Event::SessionRotated { starting_session: 6, active_era: 1, planned_era: 1 }
				]
			);

			// but with it set..
			ForceEra::<T>::put(Forcing::ForceNew);

			// one era happens quicker
			Session::roll_until_active_era(2);
			assert_eq!(
				staking_events_since_last_call(),
				vec![
					// we immediately plan a new era as soon as the first session report comes in
					Event::SessionRotated { starting_session: 7, active_era: 1, planned_era: 2 },
					Event::PagedElectionProceeded { page: 0, result: Ok(2) },
					// by now it is given to mock session, and is buffered
					Event::SessionRotated { starting_session: 8, active_era: 1, planned_era: 2 },
					// and by now it is activated. Note how the validator payout is less, since the
					// era duration is less.
					Event::EraPaid { era_index: 1, validator_payout: 7500, remainder: 0 },
					Event::SessionRotated { starting_session: 9, active_era: 2, planned_era: 2 }
				]
			);

			// And the next era goes back to normal.
			Session::roll_until_active_era(3);
			assert_eq!(
				staking_events_since_last_call(),
				vec![
					Event::SessionRotated { starting_session: 10, active_era: 2, planned_era: 2 },
					Event::SessionRotated { starting_session: 11, active_era: 2, planned_era: 2 },
					Event::SessionRotated { starting_session: 12, active_era: 2, planned_era: 2 },
					Event::SessionRotated { starting_session: 13, active_era: 2, planned_era: 3 },
					Event::PagedElectionProceeded { page: 0, result: Ok(2) },
					Event::SessionRotated { starting_session: 14, active_era: 2, planned_era: 3 },
					Event::EraPaid { era_index: 2, validator_payout: 15000, remainder: 0 },
					Event::SessionRotated { starting_session: 15, active_era: 3, planned_era: 3 }
				]
			);
		});
}

#[test]
fn activation_timestamp_when_no_planned_era() {
	// maybe not needed, as we have the id check
	ExtBuilder::default().session_per_era(6).build_and_execute(|| {
		Session::roll_until_active_era(2);
		let current_index = CurrentIndex::get();

		// reset events until now.
		let _ = staking_events_since_last_call();

		// GIVEN: no new planned era
		assert_eq!(Rotator::<T>::active_era(), 2);
		assert_eq!(Rotator::<T>::planned_era(), 2);

		// WHEN: send a new activation timestamp (manually).
		<Staking as pallet_staking_async_rc_client::AHStakingInterface>::on_relay_session_report(
			pallet_staking_async_rc_client::SessionReport::new_terminal(
				current_index,
				vec![],
				// sending a timestamp that is in the future with identifier of the next era that
				// is not planned.
				Some((Timestamp::get() + time_per_session(), 3)),
			),
		);

		// THEN: No era rotation should happen, but an error event should be emitted.
		assert_eq!(
			staking_events_since_last_call(),
			vec![
				Event::Unexpected(UnexpectedKind::UnknownValidatorActivation),
				Event::SessionRotated {
					starting_session: current_index + 1,
					active_era: 2,
					planned_era: 2
				}
			]
		);
	});
}

#[test]
#[should_panic]
fn activation_timestamp_when_era_planning_not_complete() {
	// maybe not needed, as we have the id check
	todo!("what if we receive an activation timestamp when the era planning (election) is not complete?");
}

#[test]
fn era_cleanup_history_depth_works_with_prune_era_step_extrinsic() {
	ExtBuilder::default().build_and_execute(|| {
		// Test that era pruning does not happen automatically
		assert_eq!(active_era(), 1);

		Session::roll_until_active_era(HistoryDepth::get() - 1);
		assert!(matches!(
			&staking_events_since_last_call()[..],
			&[
				..,
				Event::SessionRotated { starting_session: 236, active_era: 78, planned_era: 79 },
				Event::EraPaid { era_index: 78, validator_payout: 7500, remainder: 0 },
				Event::SessionRotated { starting_session: 237, active_era: 79, planned_era: 79 }
			]
		));
		// Verify era 78 staker pot has been funded (DAP drips into general pot, staking snapshots).
		let staker_pot_78 = <Test as Config>::RewardPots::pot_account(RewardPot::Era(
			78,
			RewardKind::StakerRewards,
		));
		let ideal_validator_payout = validator_payout_for(time_per_era());
		assert_eq!(Balances::balance(&staker_pot_78), ideal_validator_payout);
		// All eras from 1 to current still present
		assert_ok!(Eras::<T>::era_fully_present(1));
		assert_ok!(Eras::<T>::era_fully_present(2));
		// ..
		assert_ok!(Eras::<T>::era_fully_present(HistoryDepth::get() - 1));

		Session::roll_until_active_era(HistoryDepth::get());
		assert_ok!(Eras::<T>::era_fully_present(1));
		assert_ok!(Eras::<T>::era_fully_present(2));
		// ..
		assert_ok!(Eras::<T>::era_fully_present(HistoryDepth::get()));

		// Eras should NOT be automatically pruned
		Session::roll_until_active_era(HistoryDepth::get() + 1);
		assert_ok!(Eras::<T>::era_fully_present(1));
		assert_ok!(Eras::<T>::era_fully_present(2));
		// ..
		assert_ok!(Eras::<T>::era_fully_present(HistoryDepth::get() + 1));
		assert!(matches!(
			&staking_events_since_last_call()[..],
			&[
				..,
				// NO EraPruned event - pruning is now manual
				Event::EraPaid { era_index: 80, validator_payout: 7500, remainder: 0 },
				Event::SessionRotated { starting_session: 243, active_era: 81, planned_era: 81 }
			]
		));

		// Roll forward more, era 1 is now prunable
		Session::roll_until_active_era(HistoryDepth::get() + 2);
		assert_ok!(Eras::<T>::era_fully_present(1)); // Era 1 still exists!
		assert_ok!(Eras::<T>::era_fully_present(2));
		assert_ok!(Eras::<T>::era_fully_present(3));
		// ..
		assert_ok!(Eras::<T>::era_fully_present(HistoryDepth::get() + 2));
		assert!(matches!(
			&staking_events_since_last_call()[..],
			&[
				..,
				// NO EraPruned event - pruning is now manual
				Event::EraPaid { era_index: 81, validator_payout: 7500, remainder: 0 },
				Event::SessionRotated { starting_session: 246, active_era: 82, planned_era: 82 }
			]
		));
		// Verify eras 79-81 staker pots were funded with expected amount.
		let expected_per_era = validator_payout_for(time_per_era());
		for era in 79..=81 {
			let staker_pot = <Test as Config>::RewardPots::pot_account(RewardPot::Era(
				era,
				RewardKind::StakerRewards,
			));
			assert_eq!(
				Balances::balance(&staker_pot),
				expected_per_era,
				"Era {era} staker pot should have {expected_per_era}"
			);
		}

		// Only old eras (outside pruning window) can be pruned
		// Try to prune era 2 (should fail as it's within the history window)
		assert_noop!(
			Staking::prune_era_step(RuntimeOrigin::signed(99), 2),
			Error::<T>::EraNotPrunable
		);
		// Try to prune the current era
		assert_noop!(
			Staking::prune_era_step(RuntimeOrigin::signed(99), HistoryDepth::get() + 2),
			Error::<T>::EraNotPrunable
		);

		// Verify that we can manually prune era 1 (which is outside history window) and check that
		// we progress through all PruningStep states in the exact order, with storage cleanup
		// verification
		use crate::PruningStep::*;

		// Process each pruning step in the exact order defined by the implementation
		// Each step should clean its specific storage and transition to the next step

		// Process each pruning step, potentially with multiple calls due to item limits
		let steps_order = [
			ErasStakersPaged,
			ErasStakersOverview,
			ErasValidatorPrefs,
			ClaimedRewards,
			ErasValidatorReward,
			ErasRewardPoints,
			SingleEntryCleanups,
			ValidatorSlashInEra,
			ErasValidatorIncentiveWeight,
		];

		let _ = staking_events_since_last_call();
		for expected_step in steps_order.iter() {
			// May need multiple calls for steps with lots of data due to weight limits
			loop {
				let current_state = EraPruningState::<T>::get(1)
					.expect("Era 1 should be marked for pruning at this point");
				assert_eq!(
					current_state, *expected_step,
					"Expected to be in step {:?} but was in {:?}",
					expected_step, current_state
				);

				let result = Staking::prune_era_step(RuntimeOrigin::signed(99), 1);
				assert_ok!(&result);
				let post_info = result.unwrap();

				// When work is actually done (pruning storage), should return Pays::No
				assert_eq!(
					post_info.pays_fee,
					frame_support::dispatch::Pays::No,
					"Should return Pays::No when work is done for step {:?}",
					expected_step
				);

				// Verify weight tracking and limits
				assert!(
					post_info.actual_weight.is_some(),
					"Should report actual weight for {:?}",
					expected_step
				);
				let actual_weight = post_info.actual_weight.unwrap();
				assert!(
					actual_weight.ref_time() > 0,
					"Should report non-zero ref_time for {:?}",
					expected_step
				);
				// No need to validate against limits since we use item-based limiting

				// Check if we've moved to the next step (step completed)
				let new_state = EraPruningState::<T>::get(1).unwrap_or(ErasStakersPaged);
				if new_state != current_state {
					break; // Step completed, move to next
				}
				// Otherwise continue with same step (partial completion due to item limits)
			}

			// Verify the specific storage is cleaned after completing this step
			match expected_step {
				ErasStakersPaged => assert_eq!(
					crate::ErasStakersPaged::<T>::iter_prefix_values((1,)).count(),
					0,
					"{expected_step:?} should be empty after completing step"
				),
				ErasStakersOverview => assert_eq!(
					crate::ErasStakersOverview::<T>::iter_prefix_values(1).count(),
					0,
					"{expected_step:?} should be empty after completing step"
				),
				ErasValidatorPrefs => assert_eq!(
					crate::ErasValidatorPrefs::<T>::iter_prefix_values(1).count(),
					0,
					"{expected_step:?} should be empty after completing step"
				),
				ClaimedRewards => assert_eq!(
					crate::ClaimedRewards::<T>::iter_prefix_values(1).count(),
					0,
					"{expected_step:?} should be empty after completing step"
				),
				ErasValidatorReward => {
					assert!(
						!crate::ErasValidatorReward::<T>::contains_key(1),
						"{expected_step:?} should be empty after completing step"
					);
				},
				ErasRewardPoints => {
					assert!(
						!crate::ErasRewardPoints::<T>::contains_key(1),
						"{expected_step:?} should be empty after completing step"
					);
				},
				SingleEntryCleanups => {
					assert!(
						!crate::ErasTotalStake::<T>::contains_key(1),
						"{expected_step:?} should be empty after completing step"
					);
					assert!(
						!crate::ErasNominatorsSlashable::<T>::contains_key(1),
						"ErasNominatorsSlashable should be empty after completing SingleEntryCleanups step"
					);
					assert!(
						!crate::ErasValidatorIncentiveBudget::<T>::contains_key(1),
						"ErasValidatorIncentiveBudget should be empty after completing SingleEntryCleanups step"
					);
					assert!(
						!crate::ErasSumValidatorIncentiveWeight::<T>::contains_key(1),
						"ErasSumValidatorIncentiveWeight should be empty after completing SingleEntryCleanups step"
					);
				},
				ValidatorSlashInEra => assert_eq!(
					crate::ValidatorSlashInEra::<T>::iter_prefix_values(1).count(),
					0,
					"{expected_step:?} should be empty after completing step"
				),
				ErasValidatorIncentiveWeight => assert_eq!(
					crate::ErasValidatorIncentiveWeight::<T>::iter_prefix_values(1).count(),
					0,
					"{expected_step:?} should be empty after completing step"
				),
			}
		}

		// After final step (ValidatorSlashInEra), the EraPruningState should be removed
		assert!(
			EraPruningState::<T>::get(1).is_none(),
			"EraPruningState should be removed after final step"
		);

		// Should emit exactly one EraPruned event when manual pruning completes
		assert!(matches!(&staking_events_since_last_call()[..], &[Event::EraPruned { index: 1 }]));

		// Attempting to prune again should return an error
		let result = Staking::prune_era_step(RuntimeOrigin::signed(99), 1);
		assert_noop!(result, Error::<T>::EraNotPrunable);

		// Now era 1 should be absent
		assert_ok!(Eras::<T>::era_absent(1));
		// But era 2 should still be present (not automatically pruned)
		assert_ok!(Eras::<T>::era_fully_present(2));

		// Call the extrinsic on an already pruned era (should return error)
		let result = Staking::prune_era_step(RuntimeOrigin::signed(99), 1);
		assert_noop!(result, Error::<T>::EraNotPrunable);
	});
}

#[test]
fn progress_many_eras_with_try_state() {
	// a bit slow, but worthwhile
	ExtBuilder::default().build_and_execute(|| {
		Session::roll_until_active_era_with(
			HistoryDepth::get().max(BondingDuration::get()) + 2,
			|| {
				Staking::do_try_state(System::block_number()).unwrap();
			},
		);
	})
}

mod inflation {
	use super::*;

	#[test]
	fn dap_budget_allocation_determines_staker_rewards() {
		ExtBuilder::default().build_and_execute(|| {
			// 50% of time_per_era() goes to stakers (other half to buffer per
			// mock::default_budget())
			let default_stakers_payout = validator_payout_for(time_per_era());
			assert_eq!(default_stakers_payout, Balance::from(time_per_era()) / 2);

			Session::roll_until_active_era(2);

			assert_eq!(
				staking_events_since_last_call(),
				vec![
					Event::SessionRotated { starting_session: 4, active_era: 1, planned_era: 2 },
					Event::PagedElectionProceeded { page: 0, result: Ok(2) },
					Event::SessionRotated { starting_session: 5, active_era: 1, planned_era: 2 },
					Event::EraPaid { era_index: 1, validator_payout: 7500, remainder: 0 },
					Event::SessionRotated { starting_session: 6, active_era: 2, planned_era: 2 }
				]
			);

			assert_eq!(ErasValidatorReward::<Test>::get(0).unwrap(), default_stakers_payout);
		})
	}
}

#[test]
fn era_pot_drained_after_history_depth() {
	ExtBuilder::default().build_and_execute(|| {
		// GIVEN: Start at era 2
		Session::roll_until_active_era(2);
		let _ = staking_events_since_last_call();

		// Verify era-1 staker pot was funded with expected amount.
		let staker_pot_1 =
			<Test as Config>::RewardPots::pot_account(RewardPot::Era(1, RewardKind::StakerRewards));
		let expected_per_era = validator_payout_for(time_per_era());
		assert_eq!(Balances::balance(&staker_pot_1), expected_per_era);

		// era we expect to be drained
		let drained_era = 1;

		// WHEN: Advance past HistoryDepth so era 1 falls out of the active window.
		let target_era = drained_era + HistoryDepth::get() + 1;
		Session::roll_until_active_era(target_era);
		let _ = staking_events_since_last_call();

		// THEN: era-1's pot account holds zero balance but is kept alive (provider
		// retained) so a future era reusing the same slot can snapshot into it.
		let staker_pot = <Test as Config>::RewardPots::pot_account(RewardPot::Era(
			drained_era,
			RewardKind::StakerRewards,
		));

		assert_eq!(Balances::balance(&staker_pot), 0, "Staker pot should have zero balance");
		assert_eq!(
			System::providers(&staker_pot),
			1,
			"Staker pot is kept alive for slot reuse; provider must be retained"
		);
	});
}

#[test]
fn pot_slot_reuse_drain_then_recreate_is_idempotent() {
	// Drain must keep the slot alive, and a subsequent `create()` on a future
	// era sharing the same slot must not double-increment the provider.
	ExtBuilder::default().build_and_execute(|| {
		let era_a = 5;
		let era_b = era_a + POT_POOL_SIZE;

		// GIVEN: era_a's pot is created and funded.
		let pot = EraRewardManager::<Test>::create(era_a, RewardKind::StakerRewards);
		assert_eq!(System::providers(&pot), 1);
		let funded: Balance = 1_000;
		Balances::set_balance(&pot, funded);
		assert_eq!(Balances::balance(&pot), funded);

		// WHEN: era_a's pot is cleaned up past HistoryDepth.
		EraRewardManager::<Test>::cleanup_era(era_a);

		// THEN: balance drained, provider retained (slot kept alive).
		assert_eq!(Balances::balance(&pot), 0);
		assert_eq!(System::providers(&pot), 1, "drain must not release the provider");

		// WHEN: era_b reuses the same slot.
		EraRewardManager::<Test>::create(era_b, RewardKind::StakerRewards);

		// THEN: provider count unchanged (idempotent create).
		assert_eq!(
			System::providers(&pot),
			1,
			"create must not double-increment provider on slot reuse"
		);

		// AND: a fresh snapshot into the reused slot works as if it were new.
		Balances::set_balance(&pot, 2_000);
		assert_eq!(Balances::balance(&pot), 2_000);
	});
}

#[test]
fn era_pot_slots_collide_every_pool_size_eras() {
	// Verifies the production `Seed` provider derives era pots from `(slot, kind)`
	// rather than `(era, kind)`. Asserted on the encoded seed rather than the
	// resulting `AccountId` because the mock's `AccountId = u64` is too narrow
	// to fit the seed and `into_sub_account_truncating` truncates it down to a
	// constant.
	let seed_for = |era: u32, kind: RewardKind| -> Vec<u8> {
		(
			<PalletId as sp_runtime::TypeId>::TYPE_ID,
			DapPalletId::get(),
			RewardPot::Era(crate::pot_slot(era), kind),
		)
			.encode()
	};

	let base_era = 7u32;

	// distinct seeds within a pool window, collision exactly at distance `POT_POOL_SIZE`.
	for kind in [RewardKind::StakerRewards, RewardKind::ValidatorSelfStake] {
		let base_seed = seed_for(base_era, kind);

		for offset in 1..POT_POOL_SIZE {
			assert_ne!(
				seed_for(base_era + offset, kind),
				base_seed,
				"{:?} pot at era +{} must not collide within the pool window",
				kind,
				offset,
			);
		}

		assert_eq!(
			seed_for(base_era + POT_POOL_SIZE, kind),
			base_seed,
			"{:?} pot at era +POT_POOL_SIZE must reuse the base era's slot",
			kind,
		);
	}

	// Within a single slot, different reward kinds must remain distinct.
	assert_ne!(
		seed_for(base_era, RewardKind::StakerRewards),
		seed_for(base_era, RewardKind::ValidatorSelfStake),
		"staker-rewards and incentive pots within the same slot must be distinct",
	);
}

#[test]
fn disable_legacy_minting_era_updates_correctly() {
	ExtBuilder::default().build_and_execute(|| {
		// GIVEN: DisableMintingGuard is set to 0 in test genesis
		assert_eq!(DisableMintingGuard::<Test>::get(), Some(0));

		// WHEN: Era 1 ends with non-zero reward allocation
		Session::roll_until_active_era(2);
		let _ = staking_events_since_last_call();

		// THEN: DisableMintingGuard remains at 0
		assert_eq!(DisableMintingGuard::<Test>::get(), Some(0));
	});
}

#[test]
fn disable_legacy_minting_era_write_once_semantics() {
	ExtBuilder::default().build_and_execute(|| {
		// GIVEN: Clear DisableMintingGuard to simulate pre-migration state
		DisableMintingGuard::<Test>::kill();
		assert_eq!(DisableMintingGuard::<Test>::get(), None);

		// WHEN: First era ends with rewards
		Session::roll_until_active_era(2);
		let _ = staking_events_since_last_call();

		// THEN: DisableMintingGuard is set to era 1
		assert_eq!(DisableMintingGuard::<Test>::get(), Some(1));

		// WHEN: More eras end
		Session::roll_until_active_era(5);
		let _ = staking_events_since_last_call();

		// THEN: DisableMintingGuard stays at 1 (not updated to higher values)
		assert_eq!(DisableMintingGuard::<Test>::get(), Some(1));
	});
}

#[test]
fn dap_era_with_zero_rewards_still_sets_guard() {
	// A DAP era with nothing to snapshot is still a DAP era. The guard must be set so
	// payout routing for this era uses the DAP path, not legacy minting.
	ExtBuilder::default().build_and_execute(|| {
		// GIVEN: pre-migration state (no guard) and a budget that drips nothing to the
		// staker reward pot.
		DisableMintingGuard::<Test>::kill();
		pallet_dap::BudgetAllocation::<Test>::put(build_budget(&[(buffer_key(), 100)]));
		assert_eq!(DisableMintingGuard::<Test>::get(), None);

		// WHEN: an era ends with zero staker rewards.
		Session::roll_until_active_era(2);
		let _ = staking_events_since_last_call();

		// THEN: guard records this era so future payouts route via DAP.
		assert_eq!(ErasValidatorReward::<Test>::get(1), Some(0));
		assert_eq!(DisableMintingGuard::<Test>::get(), Some(1));
	});
}