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

Fixes the collator reward wrong percentage #832

Merged
merged 3 commits into from
Sep 19, 2021
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
12 changes: 7 additions & 5 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,15 +1715,16 @@ pub mod pallet {
return;
}
let total_staked = <Staked<T>>::get(round_to_payout);
let mut issuance = Self::compute_issuance(total_staked);
let total_issuance = Self::compute_issuance(total_staked);
let mut left_issuance = total_issuance;
// reserve portion of issuance for parachain bond account
let bond_config = <ParachainBondInfo<T>>::get();
let parachain_bond_reserve = bond_config.percent * issuance;
let parachain_bond_reserve = bond_config.percent * total_issuance;
if let Ok(imb) =
T::Currency::deposit_into_existing(&bond_config.account, parachain_bond_reserve)
{
// update round issuance iff transfer succeeds
issuance -= imb.peek();
left_issuance -= imb.peek();
Self::deposit_event(Event::ReservedForParachainBond(
bond_config.account,
imb.peek(),
Expand All @@ -1745,9 +1746,10 @@ pub mod pallet {
}
};
let collator_fee = <CollatorCommission<T>>::get();
let collator_issuance = collator_fee * total_issuance;
for (val, pts) in <AwardedPts<T>>::drain_prefix(round_to_payout) {
let pct_due = Perbill::from_rational(pts, total);
let mut amt_due = pct_due * issuance;
let mut amt_due = pct_due * left_issuance;
// Take the snapshot of block author and nominations
let state = <AtStake<T>>::take(round_to_payout, &val);
if state.nominators.is_empty() {
Expand All @@ -1756,7 +1758,7 @@ pub mod pallet {
} else {
// pay collator first; commission + due_portion
let val_pct = Perbill::from_rational(state.bond, state.total);
let commission = collator_fee * amt_due;
let commission = pct_due * collator_issuance;
amt_due -= commission;
let val_due = (val_pct * amt_due) + commission;
mint(val_due, val.clone());
Expand Down
52 changes: 26 additions & 26 deletions pallets/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2725,10 +2725,10 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::CollatorChosen(3, 5, 10),
Event::NewRound(10, 3, 5, 140),
Event::ReservedForParachainBond(11, 15),
Event::Rewarded(1, 18),
Event::Rewarded(6, 6),
Event::Rewarded(7, 6),
Event::Rewarded(10, 6),
Event::Rewarded(1, 20),
Event::Rewarded(6, 5),
Event::Rewarded(7, 5),
Event::Rewarded(10, 5),
Event::CollatorChosen(4, 1, 50),
Event::CollatorChosen(4, 2, 40),
Event::CollatorChosen(4, 4, 20),
Expand All @@ -2754,18 +2754,18 @@ fn parachain_bond_inflation_reserve_matches_config() {
let mut new2 = vec![
Event::NominatorExitScheduled(4, 6, 6),
Event::ReservedForParachainBond(11, 16),
Event::Rewarded(1, 19),
Event::Rewarded(6, 6),
Event::Rewarded(7, 6),
Event::Rewarded(10, 6),
Event::Rewarded(1, 21),
Event::Rewarded(6, 5),
Event::Rewarded(7, 5),
Event::Rewarded(10, 5),
Event::CollatorChosen(5, 1, 50),
Event::CollatorChosen(5, 2, 40),
Event::CollatorChosen(5, 4, 20),
Event::CollatorChosen(5, 3, 20),
Event::CollatorChosen(5, 5, 10),
Event::NewRound(20, 5, 5, 140),
Event::ReservedForParachainBond(11, 16),
Event::Rewarded(1, 20),
Event::Rewarded(1, 22),
Event::Rewarded(6, 6),
Event::Rewarded(7, 6),
Event::Rewarded(10, 6),
Expand Down Expand Up @@ -2795,10 +2795,10 @@ fn parachain_bond_inflation_reserve_matches_config() {
Percent::from_percent(50),
),
Event::ReservedForParachainBond(11, 29),
Event::Rewarded(1, 15),
Event::Rewarded(6, 5),
Event::Rewarded(7, 5),
Event::Rewarded(10, 5),
Event::Rewarded(1, 19),
Event::Rewarded(6, 3),
Event::Rewarded(7, 3),
Event::Rewarded(10, 3),
Event::CollatorChosen(7, 2, 40),
Event::CollatorChosen(7, 1, 40),
Event::CollatorChosen(7, 4, 20),
Expand All @@ -2814,9 +2814,9 @@ fn parachain_bond_inflation_reserve_matches_config() {
// no more paying 6
let mut new4 = vec![
Event::ReservedForParachainBond(11, 30),
Event::Rewarded(1, 18),
Event::Rewarded(7, 6),
Event::Rewarded(10, 6),
Event::Rewarded(1, 21),
Event::Rewarded(7, 5),
Event::Rewarded(10, 5),
Event::CollatorChosen(8, 2, 40),
Event::CollatorChosen(8, 1, 40),
Event::CollatorChosen(8, 4, 20),
Expand All @@ -2834,9 +2834,9 @@ fn parachain_bond_inflation_reserve_matches_config() {
let mut new5 = vec![
Event::Nomination(8, 10, 1, NominatorAdded::AddedToTop { new_total: 50 }),
Event::ReservedForParachainBond(11, 32),
Event::Rewarded(1, 19),
Event::Rewarded(7, 6),
Event::Rewarded(10, 6),
Event::Rewarded(1, 22),
Event::Rewarded(7, 5),
Event::Rewarded(10, 5),
Event::CollatorChosen(9, 1, 50),
Event::CollatorChosen(9, 2, 40),
Event::CollatorChosen(9, 4, 20),
Expand All @@ -2852,9 +2852,9 @@ fn parachain_bond_inflation_reserve_matches_config() {
// new nomination is still not rewarded yet
let mut new6 = vec![
Event::ReservedForParachainBond(11, 33),
Event::Rewarded(1, 20),
Event::Rewarded(7, 7),
Event::Rewarded(10, 7),
Event::Rewarded(1, 23),
Event::Rewarded(7, 5),
Event::Rewarded(10, 5),
Event::CollatorChosen(10, 1, 50),
Event::CollatorChosen(10, 2, 40),
Event::CollatorChosen(10, 4, 20),
Expand All @@ -2869,10 +2869,10 @@ fn parachain_bond_inflation_reserve_matches_config() {
// new nomination is rewarded, 2 rounds after joining (`RewardPaymentDelay` is 2)
let mut new7 = vec![
Event::ReservedForParachainBond(11, 35),
Event::Rewarded(1, 18),
Event::Rewarded(7, 6),
Event::Rewarded(8, 6),
Event::Rewarded(10, 6),
Event::Rewarded(1, 22),
Event::Rewarded(7, 4),
Event::Rewarded(8, 4),
Event::Rewarded(10, 4),
Event::CollatorChosen(11, 1, 50),
Event::CollatorChosen(11, 2, 40),
Event::CollatorChosen(11, 4, 20),
Expand Down
4 changes: 2 additions & 2 deletions runtime/moonbase/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,11 @@ fn reward_block_authors_with_parachain_bond_reserved() {
// rewards minted and distributed
assert_eq!(
Balances::free_balance(AccountId::from(ALICE)),
1079592333275448000000,
1082693333281650000000,
);
assert_eq!(
Balances::free_balance(AccountId::from(BOB)),
528942666637724000000,
525841666640825000000,
);
// 30% reserved for parachain bond
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions runtime/moonbeam/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,11 @@ fn reward_block_authors_with_parachain_bond_reserved() {
// rewards minted and distributed
assert_eq!(
Balances::free_balance(AccountId::from(ALICE)),
1079592333275448000000,
1082693333281650000000,
);
assert_eq!(
Balances::free_balance(AccountId::from(BOB)),
528942666637724000000,
525841666640825000000,
);
// 30% reserved for parachain bond
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions runtime/moonriver/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ fn reward_block_authors_with_parachain_bond_reserved() {
// rewards minted and distributed
assert_eq!(
Balances::free_balance(AccountId::from(ALICE)),
1079592333275448000000,
1082693333281650000000,
);
assert_eq!(
Balances::free_balance(AccountId::from(BOB)),
528942666637724000000,
525841666640825000000,
);
// 30% reserved for parachain bond
assert_eq!(
Expand Down