commission as percent (#7239)

* commission_as_percent

* fixup
This commit is contained in:
Rob Walker
2019-12-03 20:55:04 -08:00
committed by GitHub
parent 369f37a0a4
commit ae93d574c2
8 changed files with 21 additions and 25 deletions

View File

@ -1902,9 +1902,9 @@ mod tests {
None, // would be Some((0, 2 * 1 + 1 * 2, 3)),
stake.calculate_rewards(1.0, &vote_state, None)
);
vote_state.commission = std::u8::MAX - 1;
vote_state.commission = 99;
assert_eq!(
None, // would be pSome((0, 2 * 1 + 1 * 2, 3)),
None, // would be Some((0, 2 * 1 + 1 * 2, 3)),
stake.calculate_rewards(1.0, &vote_state, None)
);
}
@ -1993,7 +1993,7 @@ mod tests {
let mut vote_state = VoteState::from(&vote_account).unwrap();
// split credits 3:1 between staker and voter
vote_state.commission = std::u8::MAX / 4;
vote_state.commission = 25;
// put in some credits in epoch 0 for which we should have a non-zero stake
for _i in 0..100 {
vote_state.increment_credits(1);
@ -2033,7 +2033,7 @@ mod tests {
assert!(voter_commission > 0);
assert!(staker_rewards > 0);
assert!(
staker_rewards / 3 > voter_commission,
staker_rewards / 3 >= voter_commission,
"rewards should be split ~3:1"
);
// verify rewards are added to stake

View File

@ -125,7 +125,7 @@ fn test_stake_account_lifetime() {
node_pubkey,
authorized_voter: vote_pubkey,
authorized_withdrawer: vote_pubkey,
commission: std::u8::MAX / 2,
commission: 50,
},
10,
));

View File

@ -91,7 +91,7 @@ pub struct VoteState {
pub authorized_voter: Pubkey,
/// the signer for withdrawals
pub authorized_withdrawer: Pubkey,
/// fraction of std::u8::MAX that represents what part of a rewards
/// percentage (0-100) that represents what part of a rewards
/// payout should be given to this VoteAccount
pub commission: u8,
@ -167,11 +167,11 @@ impl VoteState {
/// if commission calculation is 100% one way or other,
/// indicate with false for was_split
pub fn commission_split(&self, on: f64) -> (f64, f64, bool) {
match self.commission {
match self.commission.min(100) {
0 => (0.0, on, false),
std::u8::MAX => (on, 0.0, false),
100 => (on, 0.0, false),
split => {
let mine = on * f64::from(split) / f64::from(std::u8::MAX);
let mine = on * f64::from(split) / f64::from(100);
(mine, on - mine, true)
}
}
@ -1066,7 +1066,7 @@ mod tests {
vote_state.commission = std::u8::MAX;
assert_eq!(vote_state.commission_split(1.0), (1.0, 0.0, false));
vote_state.commission = std::u8::MAX / 2;
vote_state.commission = 50;
let (voter_portion, staker_portion, was_split) = vote_state.commission_split(10.0);
assert_eq!(