Skip to content

Commit cfd879f

Browse files
authored
Merge v4.1.1 (#620)
* Fix slashe exception * Rewards: 20% for validator and 80% for the reward pot. * Fix xstaking tests Co-authored-by: icodezjb <[email protected]>
1 parent 3a9a4af commit cfd879f

File tree

6 files changed

+41
-29
lines changed

6 files changed

+41
-29
lines changed

runtime/chainx/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl frame_support::traits::ValidatorSet<AccountId> for Runtime {
461461
}
462462

463463
fn validators() -> Vec<Self::ValidatorId> {
464-
XStaking::active_validator_set().collect()
464+
XStaking::active_candidates()
465465
}
466466
}
467467

runtime/dev/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl frame_support::traits::ValidatorSet<AccountId> for Runtime {
460460
}
461461

462462
fn validators() -> Vec<Self::ValidatorId> {
463-
XStaking::active_validator_set().collect()
463+
XStaking::active_candidates()
464464
}
465465
}
466466

runtime/malan/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl frame_support::traits::ValidatorSet<AccountId> for Runtime {
460460
}
461461

462462
fn validators() -> Vec<Self::ValidatorId> {
463-
XStaking::active_validator_set().collect()
463+
XStaking::active_candidates()
464464
}
465465
}
466466

xpallets/mining/staking/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,18 @@ impl<T: Config> Pallet<T> {
879879
Self::validator_set().filter(Self::is_active)
880880
}
881881

882+
/// Returns all the validators and candidates by order
883+
#[inline]
884+
pub fn active_candidates() -> Vec<T::AccountId> {
885+
let mut candidates = Self::active_validator_set()
886+
.map(|v| (Self::total_votes_of(&v), v))
887+
.collect::<Vec<_>>();
888+
889+
candidates.sort_by(|&(ref b1, _), &(ref b2, _)| b2.cmp(b1));
890+
891+
candidates.into_iter().map(|(_, v)| v).collect()
892+
}
893+
882894
/// Returns the sum of total active staked PCX, i.e., total staking power.
883895
///
884896
/// * One (indivisible) PCX one power.

xpallets/mining/staking/src/reward/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ impl<T: Config> Pallet<T> {
3535
///
3636
/// Add the reward to their balance, and their reward pot, pro-rata.
3737
fn apply_reward_validator(who: &T::AccountId, reward: BalanceOf<T>) {
38-
// Validator themselves can only directly gain 10%, the rest 90% is for the reward pot.
39-
let off_the_table = reward.saturated_into::<BalanceOf<T>>() / 10u32.saturated_into();
38+
// Validator themselves can only directly gain 20%, the rest 80% is for the reward pot.
39+
let off_the_table = reward.saturated_into::<BalanceOf<T>>() / 5u32.saturated_into();
4040
Self::mint(who, off_the_table);
4141
frame_support::log::debug!(
4242
target: "runtime::mining::staking",
@@ -45,7 +45,7 @@ impl<T: Config> Pallet<T> {
4545
off_the_table
4646
);
4747

48-
// Issue the rest 90% to validator's reward pot.
48+
// Issue the rest 80% to validator's reward pot.
4949
let to_reward_pot = reward - off_the_table;
5050
let reward_pot = T::DetermineRewardPotAccount::reward_pot_account_for(who);
5151
Self::mint(&reward_pot, to_reward_pot);

xpallets/mining/staking/src/tests.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,17 @@ fn staking_reward_should_work() {
518518
staked: Balance,
519519
session_index: SessionIndex| {
520520
let val_total_reward = staking_mining_reward * staked / total_staked;
521-
// 10% -> validator
522-
// 90% -> validator's reward pot
521+
// 20% -> validator
522+
// 80% -> validator's reward pot
523523
assert_eq!(
524524
Balances::free_balance(&validator),
525-
initial_free + val_total_reward * session_index as u128 / 10
525+
initial_free + val_total_reward * session_index as u128 / 5
526526
);
527527
assert_eq!(
528528
Balances::free_balance(
529529
&DummyStakingRewardPotAccountDeterminer::reward_pot_account_for(&validator)
530530
),
531-
(val_total_reward - val_total_reward / 10) * session_index as u128
531+
(val_total_reward - val_total_reward / 5) * session_index as u128
532532
);
533533
};
534534

@@ -625,22 +625,22 @@ fn staker_reward_should_work() {
625625
let calc_reward_for_pot =
626626
|validator_votes: Balance, total_staked: Balance, total_reward: Balance| {
627627
let total_reward_for_validator = validator_votes * total_reward / total_staked;
628-
let to_validator = total_reward_for_validator / 10;
628+
let to_validator = total_reward_for_validator / 5;
629629
total_reward_for_validator - to_validator
630630
};
631631

632632
// Block 1
633633
// total_staked = val(10+10) + val2(20) + val(30) + val(40) = 110
634634
// reward pot:
635-
// 1: 1_980_000_000 * 20/110 * 90% = 324_000_000
636-
// 2: 1_980_000_000 * 20/110 * 90% = 324_000_000
637-
// 3: 1_980_000_000 * 30/110 * 90% = 486_000_000
638-
// 4: 1_980_000_000 * 40/110 * 90% = 648_000_000
635+
// 1: 1_980_000_000 * 20/110 * 90% = 288_000_000
636+
// 2: 1_980_000_000 * 20/110 * 90% = 288_000_000
637+
// 3: 1_980_000_000 * 30/110 * 90% = 432_000_000
638+
// 4: 1_980_000_000 * 40/110 * 90% = 576_000_000
639639
t_start_session(1);
640-
assert_eq!(t_reward_pot_balance(1), 324_000_000);
641-
assert_eq!(t_reward_pot_balance(2), 324_000_000);
642-
assert_eq!(t_reward_pot_balance(3), 486_000_000);
643-
assert_eq!(t_reward_pot_balance(4), 648_000_000);
640+
assert_eq!(t_reward_pot_balance(1), 288_000_000);
641+
assert_eq!(t_reward_pot_balance(2), 288_000_000);
642+
assert_eq!(t_reward_pot_balance(3), 432_000_000);
643+
assert_eq!(t_reward_pot_balance(4), 576_000_000);
644644

645645
assert_eq!(
646646
<ValidatorLedgers<Test>>::get(2),
@@ -663,36 +663,36 @@ fn staker_reward_should_work() {
663663
// Block 2
664664
// total_staked = val(10+10) + val2(20+20) + val(30) + val(40) = 130
665665
// reward pot:
666-
// There might be a calculation loss using 90% directly, the actual
666+
// There might be a calculation loss using 80% directly, the actual
667667
// calculation is:
668668
// validator 3: 1_980_000_000 * 30/130 = 456_923_076
669-
// |_ validator 3: 456_923_076 / 10 = 45_692_307
669+
// |_ validator 3: 456_923_076 / 5 = 91_384_615
670670
// |_ validator 3's reward pot: 731_076_923 - 73_107_692
671671

672672
t_start_session(2);
673673
// The order is [3, 4, 1, 2] when calculating.
674674
assert_eq!(
675675
t_reward_pot_balance(3),
676-
486_000_000 + 456_923_076 - 45_692_307
676+
432_000_000 + 456_923_076 - 91_384_615
677677
);
678678
assert_eq!(
679679
t_reward_pot_balance(3),
680-
486_000_000 + calc_reward_for_pot(30, 130, TOTAL_STAKING_REWARD)
680+
432_000_000 + calc_reward_for_pot(30, 130, TOTAL_STAKING_REWARD)
681681
);
682-
assert_eq!(t_reward_pot_balance(4), 1_196_307_693);
683-
assert_eq!(t_reward_pot_balance(1), 598_153_847);
684-
assert_eq!(t_reward_pot_balance(2), 872_307_693);
682+
assert_eq!(t_reward_pot_balance(4), 1_063_384_616);
683+
assert_eq!(t_reward_pot_balance(1), 531_692_308);
684+
assert_eq!(t_reward_pot_balance(2), 775_384_616);
685685

686686
// validator 1: vote weight = 10 + 20 * 1 = 30
687687
// t_1 vote weight: 10 * 1 = 10
688688
assert_ok!(XStaking::claim(Origin::signed(t_1), 1));
689689
// t_1 = reward_pot_balance * 10 / 30
690-
assert_eq!(XStaking::free_balance(&t_1), 100 + 598_153_847 / 3);
690+
assert_eq!(XStaking::free_balance(&t_1), 100 + 531_692_308 / 3);
691691

692692
// validator 2: vote weight = 40 * 1 + 20 = 60
693693
// t_2 vote weight = 20 * 1 = 20
694694
assert_ok!(XStaking::claim(Origin::signed(t_2), 2));
695-
assert_eq!(XStaking::free_balance(&t_2), 100 + 872_307_693 * 20 / 60);
695+
assert_eq!(XStaking::free_balance(&t_2), 100 + 775_384_616 * 20 / 60);
696696

697697
assert_ok!(XStaking::set_minimum_validator_count(Origin::root(), 3));
698698
assert_ok!(XStaking::chill(Origin::signed(3)));
@@ -702,7 +702,7 @@ fn staker_reward_should_work() {
702702
// validator 3 is chilled now, not rewards then.
703703
assert_eq!(
704704
t_reward_pot_balance(3),
705-
486_000_000 + calc_reward_for_pot(30, 130, TOTAL_STAKING_REWARD)
705+
432_000_000 + calc_reward_for_pot(30, 130, TOTAL_STAKING_REWARD)
706706
);
707707
});
708708
}

0 commit comments

Comments
 (0)