Record vote account commission with voting/staking rewards and surface in RPC

This commit is contained in:
Michael Vines
2021-07-10 23:18:42 -07:00
parent e322bc90cc
commit 4098af3b5b
17 changed files with 90 additions and 26 deletions

View File

@ -71,6 +71,7 @@ use solana_sdk::{
create_account_shared_data_with_fields as create_account, from_account, Account,
AccountSharedData, InheritableAccountFields, ReadableAccount, WritableAccount,
},
account_utils::StateMut,
clock::{
BankId, Epoch, Slot, SlotCount, SlotIndex, UnixTimestamp, DEFAULT_TICKS_PER_SECOND,
INITIAL_RENT_EPOCH, MAX_PROCESSING_AGE, MAX_RECENT_BLOCKHASHES,
@ -107,7 +108,10 @@ use solana_sdk::{
transaction::{self, Result, Transaction, TransactionError},
};
use solana_stake_program::stake_state::{self, InflationPointCalculationEvent, PointValue};
use solana_vote_program::vote_instruction::VoteInstruction;
use solana_vote_program::{
vote_instruction::VoteInstruction,
vote_state::{VoteState, VoteStateVersions},
};
use std::{
borrow::Cow,
cell::RefCell,
@ -142,6 +146,7 @@ impl RentDebits {
reward_type: RewardType::Rent,
lamports: rent_debit,
post_balance,
commission: None, // Not applicable
};
self.0.push((*account, reward_info));
} else {
@ -830,8 +835,9 @@ pub trait DropCallback: fmt::Debug {
#[derive(Debug, PartialEq, Serialize, Deserialize, AbiExample, Clone, Copy)]
pub struct RewardInfo {
pub reward_type: RewardType,
pub lamports: i64, // Reward amount
pub post_balance: u64, // Account balance in lamports after `lamports` was applied
pub lamports: i64, // Reward amount
pub post_balance: u64, // Account balance in lamports after `lamports` was applied
pub commission: Option<u8>, // Vote account commission when the reward was credited, only present for voting and staking rewards
}
#[derive(Debug, Default)]
@ -2044,6 +2050,17 @@ impl Bank {
for (vote_pubkey, (stake_group, vote_account)) in stake_delegation_accounts.iter_mut() {
let mut vote_account_changed = false;
let voters_account_pre_balance = vote_account.lamports();
let vote_state: VoteState = match StateMut::<VoteStateVersions>::state(vote_account) {
Ok(vote_state) => vote_state.convert_to_current(),
Err(err) => {
debug!(
"failed to deserialize vote account {}: {}",
vote_pubkey, err
);
continue;
}
};
let commission = Some(vote_state.commission);
for (stake_pubkey, stake_account) in stake_group.iter_mut() {
// curry closure to add the contextual stake_pubkey
@ -2058,6 +2075,7 @@ impl Bank {
rewarded_epoch,
stake_account,
vote_account,
&vote_state,
&point_value,
Some(&stake_history),
&mut reward_calc_tracer.as_mut(),
@ -2074,6 +2092,7 @@ impl Bank {
reward_type: RewardType::Staking,
lamports: stakers_reward as i64,
post_balance: stake_account.lamports(),
commission,
},
));
}
@ -2095,6 +2114,7 @@ impl Bank {
reward_type: RewardType::Voting,
lamports,
post_balance,
commission,
},
));
}
@ -2206,6 +2226,7 @@ impl Bank {
reward_type: RewardType::Fee,
lamports: deposit as i64,
post_balance,
commission: None,
},
));
}
@ -3589,6 +3610,7 @@ impl Bank {
reward_type: RewardType::Rent,
lamports: rent_to_be_paid as i64,
post_balance: account.lamports(),
commission: None,
},
));
}
@ -5452,7 +5474,6 @@ pub(crate) mod tests {
use crossbeam_channel::{bounded, unbounded};
use solana_sdk::{
account::Account,
account_utils::StateMut,
clock::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_TICKS_PER_SLOT},
epoch_schedule::MINIMUM_SLOTS_PER_EPOCH,
feature::Feature,
@ -7479,6 +7500,7 @@ pub(crate) mod tests {
reward_type: RewardType::Staking,
lamports: (rewards.validator_point_value * validator_points as f64) as i64,
post_balance: bank1.get_balance(&stake_id),
commission: Some(0),
}
)]
);
@ -7980,6 +8002,7 @@ pub(crate) mod tests {
reward_type: RewardType::Fee,
lamports: expected_fee_collected as i64,
post_balance: initial_balance + expected_fee_collected,
commission: None,
}
)]
);
@ -8015,6 +8038,7 @@ pub(crate) mod tests {
reward_type: RewardType::Fee,
lamports: expected_fee_collected as i64,
post_balance: initial_balance + 2 * expected_fee_collected,
commission: None,
}
)]
);