Add redeem_vote_credits to runtime (#7910)

* Move redeem_vote_credits into runtime

* fixup

* test

* move stake manipulation to stake program

* chugga for less indentation
This commit is contained in:
Rob Walker
2020-01-22 12:21:31 -08:00
committed by GitHub
parent 3a0d13aa77
commit ce70d6eedc
4 changed files with 151 additions and 34 deletions

View File

@ -48,7 +48,7 @@ use solana_sdk::{
timing::years_as_slots,
transaction::{Result, Transaction, TransactionError},
};
use solana_stake_program::stake_state::Delegation;
use solana_stake_program::stake_state::{self, Delegation};
use solana_vote_program::vote_state::VoteState;
use std::{
cell::RefCell,
@ -630,12 +630,50 @@ impl Bank {
&sysvar::rewards::create_account(1, validator_point_value, storage_point_value),
);
let validator_rewards = self.pay_validator_rewards(validator_point_value);
self.capitalization.fetch_add(
(validator_rewards + storage_rewards) as u64,
validator_rewards + storage_rewards as u64,
Ordering::Relaxed,
);
}
/// iterate over all stakes, redeem vote credits for each stake we can
/// successfully load and parse, return total payout
fn pay_validator_rewards(&self, point_value: f64) -> u64 {
let stake_history = self.stakes.read().unwrap().history().clone();
self.stake_delegations()
.iter()
.map(|(stake_pubkey, delegation)| {
match (
self.get_account(&stake_pubkey),
self.get_account(&delegation.voter_pubkey),
) {
(Some(mut stake_account), Some(mut vote_account)) => {
let rewards = stake_state::redeem_rewards(
&mut stake_account,
&mut vote_account,
point_value,
Some(&stake_history),
);
if let Ok(rewards) = rewards {
self.store_account(&stake_pubkey, &stake_account);
self.store_account(&delegation.voter_pubkey, &vote_account);
rewards
} else {
debug!(
"stake_state::redeem_rewards() failed for {}: {:?}",
stake_pubkey, rewards
);
0
}
}
(_, _) => 0,
}
})
.sum()
}
pub fn update_recent_blockhashes(&self) {
let blockhash_queue = self.blockhash_queue.read().unwrap();
let recent_blockhash_iter = blockhash_queue.get_recent_blockhashes();
@ -2920,14 +2958,14 @@ mod tests {
}));
assert_eq!(bank.capitalization(), 42 * 1_000_000_000);
let ((vote_id, mut vote_account), stake) =
let ((vote_id, mut vote_account), (stake_id, stake_account)) =
crate::stakes::tests::create_staked_node_accounts(1_0000);
let ((validator_id, validator_account), (archiver_id, archiver_account)) =
crate::storage_utils::tests::create_storage_accounts_with_credits(100);
// set up stakes, vote, and storage accounts
bank.store_account(&stake.0, &stake.1);
bank.store_account(&stake_id, &stake_account);
bank.store_account(&validator_id, &validator_account.borrow());
bank.store_account(&archiver_id, &archiver_account.borrow());
@ -2960,6 +2998,16 @@ mod tests {
.map(|account| Rewards::from_account(&account).unwrap())
.unwrap();
// verify the stake and vote accounts are the right size
assert!(
((bank1.get_balance(&stake_id) - stake_account.lamports + bank1.get_balance(&vote_id)
- vote_account.lamports) as f64
- rewards.validator_point_value * validator_points as f64)
.abs()
< 1.0
);
// verify the rewards are the right size
assert!(
((rewards.validator_point_value * validator_points as f64
+ rewards.storage_point_value * storage_points as f64)

View File

@ -1,4 +1,3 @@
use assert_matches::assert_matches;
use solana_runtime::{
bank::Bank,
bank_client::BankClient,
@ -11,7 +10,7 @@ use solana_sdk::{
pubkey::Pubkey,
signature::{Keypair, KeypairUtil},
system_instruction::create_address_with_seed,
sysvar::{self, rewards::Rewards, stake_history::StakeHistory, Sysvar},
sysvar::{self, stake_history::StakeHistory, Sysvar},
};
use solana_stake_program::{
stake_instruction::{self},
@ -241,6 +240,14 @@ fn test_stake_account_lifetime() {
assert!(false, "wrong account type found")
}
loop {
if warmed_up(&bank, &stake_pubkey) {
break;
}
// Cycle thru banks until we're fully warmed up
bank = next_epoch(&bank);
}
// Reward redemption
// Submit enough votes to generate rewards
bank = fill_epoch_with_votes(&bank, &vote_keypair, &mint_keypair);
@ -251,35 +258,14 @@ fn test_stake_account_lifetime() {
// 1 less vote, as the first vote should have cleared the lockout
assert_eq!(vote_state.votes.len(), 31);
assert_eq!(vote_state.credits(), 1);
// one vote per slot, might be more slots than 32 in the epoch
assert!(vote_state.credits() >= 1);
bank = fill_epoch_with_votes(&bank, &vote_keypair, &mint_keypair);
loop {
if warmed_up(&bank, &stake_pubkey) {
break;
}
// Cycle thru banks until we're fully warmed up
bank = next_epoch(&bank);
}
// Test that rewards are there
let rewards_account = bank
.get_account(&sysvar::rewards::id())
.expect("account not found");
assert_matches!(Rewards::from_account(&rewards_account), Some(_));
let pre_staked = get_staked(&bank, &stake_pubkey);
// Redeem the credit
let bank_client = BankClient::new_shared(&bank);
let message = Message::new_with_payer(
vec![stake_instruction::redeem_vote_credits(
&stake_pubkey,
&vote_pubkey,
)],
Some(&mint_pubkey),
);
assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
// next epoch bank should pay rewards
bank = next_epoch(&bank);
// Test that balance increased, and that the balance got staked
let staked = get_staked(&bank, &stake_pubkey);
@ -290,6 +276,8 @@ fn test_stake_account_lifetime() {
// split the stake
let split_stake_keypair = Keypair::new();
let split_stake_pubkey = split_stake_keypair.pubkey();
let bank_client = BankClient::new_shared(&bank);
// Test split
let message = Message::new_with_payer(
stake_instruction::split(