Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto payout #1323

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ pub mod pallet {
if !T::ShouldEndSession::get() {
call_on_exposure!(<Previous<T>>::iter_keys()
// TODO?: make this value adjustable
.drain()
.take(1)
.fold(Zero::zero(), |acc, e| acc
+ Self::payout_inner(e).unwrap_or(Zero::zero())))
Expand Down Expand Up @@ -902,6 +901,7 @@ pub mod pallet {

<PendingRewards<T>>::mutate(&c, |u| *u = u.map(|u| u + r).or(Some(r)));

// TODO: merge into one call
if T::InflationManager::reward(&staking_pot, r).is_ok() {
actual_reward += r;
} else {
Expand All @@ -924,7 +924,7 @@ pub mod pallet {
/// Pay the reward to the collator and its nominators.
pub fn payout_inner(collator: T::AccountId) -> Result<Weight, DispatchError> {
let c_exposure =
call_on_exposure!(<Previous<T>>::get(&collator).ok_or(<Error<T>>::NoReward)?)?;
call_on_exposure!(<Previous<T>>::take(&collator).ok_or(<Error<T>>::NoReward)?)?;
let c_total_payout =
<PendingRewards<T>>::take(&collator).ok_or(<Error<T>>::NoReward)?;
let mut c_payout = c_exposure.commission * c_total_payout;
Expand Down
14 changes: 7 additions & 7 deletions pallet/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,12 @@ impl Efflux {
}

pub fn block(number: BlockNumber) {
for _ in 0..number {
initialize_block(System::block_number() + 1)
}
let now = System::block_number();

(now..now + number).for_each(|n| {
initialize_block(n + 1);
finalize_block(n + 1);
});
}
}

Expand Down Expand Up @@ -498,10 +501,7 @@ pub fn new_session() {
let now = System::block_number();
let target = now + <Period as sp_runtime::traits::Get<BlockNumber>>::get();

(now + 1..=target).for_each(|i| {
initialize_block(i);
finalize_block(i);
});
(now..target).for_each(|_| Efflux::block(1));
}

pub fn payout() {
Expand Down
50 changes: 50 additions & 0 deletions pallet/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,56 @@ fn payout_should_work() {
});
}

#[test]
fn auto_payout_should_work() {
ExtBuilder::default().collator_count(2).build().execute_with(|| {
(1..=2).for_each(|i| {
assert_ok!(Staking::stake(
RuntimeOrigin::signed(i),
0,
i as Balance * UNIT,
Vec::new()
));
assert_ok!(Staking::collect(RuntimeOrigin::signed(i), Perbill::from_percent(i * 10)));
assert_ok!(Staking::nominate(RuntimeOrigin::signed(i), i));
});
(3..=4).for_each(|i| {
assert_ok!(Staking::stake(
RuntimeOrigin::signed(i),
0,
(11 - i as Balance) * UNIT,
Vec::new()
));
assert_ok!(Staking::nominate(RuntimeOrigin::signed(i), i - 2));
});
new_session();
new_session();

Efflux::time(<Period as Get<u64>>::get() as Moment);
Staking::note_authors(&[1, 2]);
new_session();
(1..=4).for_each(|i| assert_eq!(Balances::free_balance(i), 1_000 * UNIT));

Efflux::block(1);
assert_eq!(Balances::free_balance(1), 1000000758879022790250);
assert_eq!(Balances::free_balance(2), 1000000000000000000000);
assert_eq!(Balances::free_balance(3), 1000003035516089643241);
assert_eq!(Balances::free_balance(4), 1000000000000000000000);

Efflux::block(1);
assert_eq!(Balances::free_balance(1), 1000000758879022790250);
assert_eq!(Balances::free_balance(2), 1000001433438163308069);
assert_eq!(Balances::free_balance(3), 1000003035516089643241);
assert_eq!(Balances::free_balance(4), 1000002360956952540378);

Efflux::block(1);
assert_eq!(Balances::free_balance(1), 1000000758879022790250);
assert_eq!(Balances::free_balance(2), 1000001433438163308069);
assert_eq!(Balances::free_balance(3), 1000003035516089643241);
assert_eq!(Balances::free_balance(4), 1000002360956952540378);
});
}

#[test]
fn on_new_session_should_work() {
ExtBuilder::default().collator_count(2).genesis_collator().build().execute_with(|| {
Expand Down