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

@ -771,6 +771,7 @@ pub struct CliEpochReward {
pub post_balance: u64, // lamports
pub percent_change: f64,
pub apr: Option<f64>,
pub commission: Option<u8>,
}
#[derive(Serialize, Deserialize)]
@ -815,23 +816,27 @@ impl fmt::Display for CliKeyedEpochRewards {
writeln!(f, "Epoch Rewards:")?;
writeln!(
f,
" {:<44} {:<18} {:<18} {:>14} {:>14}",
"Address", "Amount", "New Balance", "Percent Change", "APR"
" {:<44} {:<18} {:<18} {:>14} {:>14} {:>10}",
"Address", "Amount", "New Balance", "Percent Change", "APR", "Commission"
)?;
for keyed_reward in &self.rewards {
match &keyed_reward.reward {
Some(reward) => {
writeln!(
f,
" {:<44} ◎{:<17.9} ◎{:<17.9} {:>13.2}% {}",
" {:<44} ◎{:<17.9} ◎{:<17.9} {:>13.9}% {:>14} {:>10}",
keyed_reward.address,
lamports_to_sol(reward.amount),
lamports_to_sol(reward.post_balance),
reward.percent_change,
reward
.apr
.map(|apr| format!("{:>13.2}%", apr))
.map(|apr| format!("{:.2}%", apr))
.unwrap_or_default(),
reward
.commission
.map(|commission| format!("{}%", commission))
.unwrap_or_else(|| "-".to_string())
)?;
}
None => {
@ -948,13 +953,13 @@ fn show_epoch_rewards(
writeln!(f, "Epoch Rewards:")?;
writeln!(
f,
" {:<6} {:<11} {:<18} {:<18} {:>14} {:>14}",
"Epoch", "Reward Slot", "Amount", "New Balance", "Percent Change", "APR"
" {:<6} {:<11} {:<18} {:<18} {:>14} {:>14} {:>10}",
"Epoch", "Reward Slot", "Amount", "New Balance", "Percent Change", "APR", "Commission"
)?;
for reward in epoch_rewards {
writeln!(
f,
" {:<6} {:<11} ◎{:<17.9} ◎{:<17.9} {:>13.2}% {}",
" {:<6} {:<11} ◎{:<17.9} ◎{:<17.9} {:>13.9}% {:>14} {:>10}",
reward.epoch,
reward.effective_slot,
lamports_to_sol(reward.amount),
@ -962,8 +967,12 @@ fn show_epoch_rewards(
reward.percent_change,
reward
.apr
.map(|apr| format!("{:>13.2}%", apr))
.map(|apr| format!("{:.2}%", apr))
.unwrap_or_default(),
reward
.commission
.map(|commission| format!("{}%", commission))
.unwrap_or_else(|| "-".to_string())
)?;
}
}
@ -2178,8 +2187,8 @@ impl fmt::Display for CliBlock {
writeln!(f, "Rewards:")?;
writeln!(
f,
" {:<44} {:^15} {:<15} {:<20} {:>14}",
"Address", "Type", "Amount", "New Balance", "Percent Change"
" {:<44} {:^15} {:<15} {:<20} {:>14} {:>10}",
"Address", "Type", "Amount", "New Balance", "Percent Change", "Commission"
)?;
for reward in rewards {
let sign = if reward.lamports < 0 { "-" } else { "" };
@ -2187,7 +2196,7 @@ impl fmt::Display for CliBlock {
total_rewards += reward.lamports;
writeln!(
f,
" {:<44} {:^15} {:>15} {}",
" {:<44} {:^15} {:>15} {} {}",
reward.pubkey,
if let Some(reward_type) = reward.reward_type {
format!("{}", reward_type)
@ -2209,7 +2218,11 @@ impl fmt::Display for CliBlock {
/ (reward.post_balance as f64 - reward.lamports as f64))
* 100.0
)
}
},
reward
.commission
.map(|commission| format!("{:>9}%", commission))
.unwrap_or_else(|| " -".to_string())
)?;
}

View File

@ -1832,6 +1832,7 @@ pub fn make_cli_reward(
post_balance: reward.post_balance,
percent_change: rate_change * 100.0,
apr: Some(apr * 100.0),
commission: reward.commission,
})
} else {
None

View File

@ -394,8 +394,9 @@ pub struct RpcPerfSample {
pub struct RpcInflationReward {
pub epoch: Epoch,
pub effective_slot: Slot,
pub amount: u64, // lamports
pub post_balance: u64, // lamports
pub amount: u64, // lamports
pub post_balance: u64, // lamports
pub commission: Option<u8>, // Vote account commission when the reward was credited
}
impl From<ConfirmedTransactionStatusWithSignature> for RpcConfirmedTransactionStatusWithSignature {

View File

@ -55,6 +55,7 @@ impl RewardsRecorderService {
lamports: reward_info.lamports,
post_balance: reward_info.post_balance,
reward_type: Some(reward_info.reward_type),
commission: reward_info.commission,
})
.collect();

View File

@ -402,6 +402,7 @@ The result field will be an object with the following fields:
- `lamports: <i64>`- number of reward lamports credited or debited by the account, as a i64
- `postBalance: <u64>` - account balance in lamports after the reward was applied
- `rewardType: <string|undefined>` - type of reward: "fee", "rent", "voting", "staking"
- `commission: <u8|undefined>` - vote account commission when the reward was credited, only present for voting and staking rewards
- `blockTime: <i64 | null>` - estimated production time, as Unix timestamp (seconds since the Unix epoch). null if not available
- `blockHeight: <u64 | null>` - the number of blocks beneath this block
@ -1372,6 +1373,7 @@ The result field will be a JSON array with the following fields:
- `effectiveSlot: <u64>`, the slot in which the rewards are effective
- `amount: <u64>`, reward amount in lamports
- `postBalance: <u64>`, post balance of the account in lamports
- `commission: <u8|undefined>` - vote account commission when the reward was credited
#### Example
@ -2820,6 +2822,7 @@ Returns transaction details for a confirmed transaction
- `lamports: <i64>`- number of reward lamports credited or debited by the account, as a i64
- `postBalance: <u64>` - account balance in lamports after the reward was applied
- `rewardType: <string>` - type of reward: currently only "rent", other types may be added in the future
- `commission: <u8|undefined>` - vote account commission when the reward was credited, only present for voting and staking rewards
#### Example:
@ -4164,6 +4167,7 @@ The result field will be an object with the following fields:
- `lamports: <i64>`- number of reward lamports credited or debited by the account, as a i64
- `postBalance: <u64>` - account balance in lamports after the reward was applied
- `rewardType: <string|undefined>` - type of reward: "fee", "rent", "voting", "staking"
- `commission: <u8|undefined>` - vote account commission when the reward was credited, only present for voting and staking rewards
- `blockTime: <i64 | null>` - estimated production time, as Unix timestamp (seconds since the Unix epoch). null if not available
#### Example:

View File

@ -79,14 +79,14 @@ fn output_slot_rewards(blockstore: &Blockstore, slot: Slot, method: &LedgerOutpu
if !rewards.is_empty() {
println!(" Rewards:");
println!(
" {:<44} {:^15} {:<15} {:<20}",
"Address", "Type", "Amount", "New Balance"
" {:<44} {:^15} {:<15} {:<20} {:>10}",
"Address", "Type", "Amount", "New Balance", "Commission",
);
for reward in rewards {
let sign = if reward.lamports < 0 { "-" } else { "" };
println!(
" {:<44} {:^15} {:<15} {}",
" {:<44} {:^15} {:<15} {} {}",
reward.pubkey,
if let Some(reward_type) = reward.reward_type {
format!("{}", reward_type)
@ -98,7 +98,11 @@ fn output_slot_rewards(blockstore: &Blockstore, slot: Slot, method: &LedgerOutpu
sign,
lamports_to_sol(reward.lamports.abs() as u64)
),
format!("{:<18.9}", lamports_to_sol(reward.post_balance))
format!("{:<18.9}", lamports_to_sol(reward.post_balance)),
reward
.commission
.map(|commission| format!("{:>9}%", commission))
.unwrap_or_else(|| " -".to_string())
);
}
}

View File

@ -21,6 +21,7 @@ fn create_rewards() -> Rewards {
lamports: 42 + i,
post_balance: std::u64::MAX,
reward_type: Some(RewardType::Fee),
commission: None,
})
.collect()
}

View File

@ -8346,6 +8346,7 @@ pub mod tests {
lamports: 42 + i,
post_balance: std::u64::MAX,
reward_type: Some(RewardType::Fee),
commission: None,
})
.collect();
let protobuf_rewards: generated::Rewards = rewards.into();
@ -8412,6 +8413,7 @@ pub mod tests {
lamports: -42,
post_balance: 42,
reward_type: Some(RewardType::Rent),
commission: None,
}]),
};
let deprecated_status: StoredTransactionStatusMeta = status.clone().into();

View File

@ -1029,14 +1029,13 @@ pub fn redeem_rewards(
rewarded_epoch: Epoch,
stake_account: &mut AccountSharedData,
vote_account: &mut AccountSharedData,
vote_state: &VoteState,
point_value: &PointValue,
stake_history: Option<&StakeHistory>,
inflation_point_calc_tracer: &mut Option<impl FnMut(&InflationPointCalculationEvent)>,
fix_stake_deactivate: bool,
) -> Result<(u64, u64), InstructionError> {
if let StakeState::Stake(meta, mut stake) = stake_account.state()? {
let vote_state: VoteState =
StateMut::<VoteStateVersions>::state(vote_account)?.convert_to_current();
if let Some(inflation_point_calc_tracer) = inflation_point_calc_tracer {
inflation_point_calc_tracer(
&InflationPointCalculationEvent::EffectiveStakeAtRewardedEpoch(stake.stake(
@ -1056,7 +1055,7 @@ pub fn redeem_rewards(
if let Some((stakers_reward, voters_reward)) = redeem_stake_rewards(
&mut stake,
point_value,
&vote_state,
vote_state,
stake_history,
inflation_point_calc_tracer,
fix_stake_deactivate,

View File

@ -480,6 +480,7 @@ impl JsonRpcRequestProcessor {
effective_slot: first_confirmed_block_in_epoch,
amount: reward.lamports.abs() as u64,
post_balance: reward.post_balance,
commission: reward.commission,
});
}
None

View File

@ -136,6 +136,7 @@ impl TransactionStatusService {
lamports: reward_info.lamports,
post_balance: reward_info.post_balance,
reward_type: Some(reward_info.reward_type),
commission: reward_info.commission,
})
.collect(),
);

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,
}
)]
);

View File

@ -238,6 +238,7 @@ impl From<StoredConfirmedBlockReward> for Reward {
lamports,
post_balance: 0,
reward_type: None,
commission: None,
}
}
}

View File

@ -88,6 +88,7 @@ message Reward {
int64 lamports = 2;
uint64 post_balance = 3;
RewardType reward_type = 4;
string commission = 5;
}
message Rewards {

View File

@ -89,6 +89,7 @@ impl From<Reward> for generated::Reward {
Some(RewardType::Staking) => generated::RewardType::Staking,
Some(RewardType::Voting) => generated::RewardType::Voting,
} as i32,
commission: reward.commission.map(|c| c.to_string()).unwrap_or_default(),
}
}
}
@ -107,6 +108,7 @@ impl From<generated::Reward> for Reward {
4 => Some(RewardType::Voting),
_ => None,
},
commission: reward.commission.parse::<u8>().ok(),
}
}
}
@ -841,6 +843,7 @@ mod test {
lamports: 123,
post_balance: 321,
reward_type: None,
commission: None,
};
let gen_reward: generated::Reward = reward.clone().into();
assert_eq!(reward, gen_reward.into());

View File

@ -23,6 +23,8 @@ pub struct StoredExtendedReward {
post_balance: u64,
#[serde(deserialize_with = "default_on_eof")]
reward_type: Option<RewardType>,
#[serde(deserialize_with = "default_on_eof")]
commission: Option<u8>,
}
impl From<StoredExtendedReward> for Reward {
@ -32,12 +34,14 @@ impl From<StoredExtendedReward> for Reward {
lamports,
post_balance,
reward_type,
commission,
} = value;
Self {
pubkey,
lamports,
post_balance,
reward_type,
commission,
}
}
}
@ -49,13 +53,14 @@ impl From<Reward> for StoredExtendedReward {
lamports,
post_balance,
reward_type,
..
commission,
} = value;
Self {
pubkey,
lamports,
post_balance,
reward_type,
commission,
}
}
}

View File

@ -339,6 +339,8 @@ pub struct Reward {
pub lamports: i64,
pub post_balance: u64, // Account balance in lamports after `lamports` was applied
pub reward_type: Option<RewardType>,
#[serde(deserialize_with = "default_on_eof")]
pub commission: Option<u8>, // Vote account commission when the reward was credited, only present for voting and staking rewards
}
pub type Rewards = Vec<Reward>;