Files
solana/programs/rewards/tests/rewards.rs

116 lines
4.0 KiB
Rust
Raw Normal View History

2019-02-15 08:45:55 -07:00
use solana_rewards_api::rewards_transaction::RewardsTransaction;
use solana_runtime::bank::{Bank, Result};
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::hash::hash;
2019-02-15 08:45:55 -07:00
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
2019-03-02 14:51:26 -07:00
use solana_vote_api::vote_state::{self, VoteState};
use solana_vote_api::vote_transaction::VoteTransaction;
2019-02-15 08:45:55 -07:00
struct RewardsBank<'a> {
bank: &'a Bank,
2019-02-15 08:45:55 -07:00
}
impl<'a> RewardsBank<'a> {
fn new(bank: &'a Bank) -> Self {
2019-03-02 08:10:27 -07:00
bank.add_native_program("solana_rewards_program", &solana_rewards_api::id());
Self { bank }
}
2019-02-15 08:45:55 -07:00
fn create_rewards_account(
&self,
from_keypair: &Keypair,
rewards_id: &Pubkey,
lamports: u64,
) -> Result<()> {
2019-03-02 10:25:16 -08:00
let blockhash = self.bank.last_blockhash();
let tx = RewardsTransaction::new_account(from_keypair, rewards_id, blockhash, lamports, 0);
self.bank.process_transaction(&tx)
}
2019-02-15 08:45:55 -07:00
fn create_vote_account(
&self,
from_keypair: &Keypair,
vote_id: &Pubkey,
lamports: u64,
) -> Result<()> {
2019-03-02 10:25:16 -08:00
let blockhash = self.bank.last_blockhash();
let tx = VoteTransaction::new_account(from_keypair, vote_id, blockhash, lamports, 0);
self.bank.process_transaction(&tx)
}
2019-02-15 08:45:55 -07:00
fn submit_vote(
&self,
staking_account: &Pubkey,
vote_keypair: &Keypair,
tick_height: u64,
) -> Result<VoteState> {
2019-03-02 10:25:16 -08:00
let blockhash = self.bank.last_blockhash();
let tx =
VoteTransaction::new_vote(staking_account, vote_keypair, tick_height, blockhash, 0);
self.bank.process_transaction(&tx)?;
2019-03-02 10:25:16 -08:00
self.bank.register_tick(&hash(blockhash.as_ref()));
let vote_account = self.bank.get_account(&vote_keypair.pubkey()).unwrap();
Ok(VoteState::deserialize(&vote_account.data).unwrap())
}
fn redeem_credits(&self, rewards_id: &Pubkey, vote_keypair: &Keypair) -> Result<VoteState> {
2019-03-02 10:25:16 -08:00
let blockhash = self.bank.last_blockhash();
let tx = RewardsTransaction::new_redeem_credits(&vote_keypair, rewards_id, blockhash, 0);
self.bank.process_transaction(&tx)?;
let vote_account = self.bank.get_account(&vote_keypair.pubkey()).unwrap();
Ok(VoteState::deserialize(&vote_account.data).unwrap())
}
2019-02-15 08:45:55 -07:00
}
#[test]
fn test_redeem_vote_credits_via_bank() {
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
let bank = Bank::new(&genesis_block);
let rewards_bank = RewardsBank::new(&bank);
2019-03-05 17:27:25 -08:00
// Create a rewards account to hold all rewards pool lamports.
2019-02-15 08:45:55 -07:00
let rewards_keypair = Keypair::new();
let rewards_id = rewards_keypair.pubkey();
rewards_bank
.create_rewards_account(&from_keypair, &rewards_id, 100)
.unwrap();
2019-02-15 08:45:55 -07:00
// A staker create a vote account account and delegates a validator to vote on its behalf.
let vote_keypair = Keypair::new();
let vote_id = vote_keypair.pubkey();
rewards_bank
.create_vote_account(&from_keypair, &vote_id, 100)
.unwrap();
2019-02-15 08:45:55 -07:00
// The validator submits votes to accumulate credits.
2019-03-02 14:51:26 -07:00
for i in 0..vote_state::MAX_LOCKOUT_HISTORY {
let vote_state = rewards_bank
.submit_vote(&vote_id, &vote_keypair, i as u64)
.unwrap();
2019-02-15 08:45:55 -07:00
assert_eq!(vote_state.credits(), 0);
}
let vote_state = rewards_bank
.submit_vote(
&vote_id,
&vote_keypair,
vote_state::MAX_LOCKOUT_HISTORY as u64 + 1,
)
.unwrap();
2019-02-15 08:45:55 -07:00
assert_eq!(vote_state.credits(), 1);
// TODO: Add VoteInstruction::RegisterStakerId so that we don't need to point the "to"
// account to the "from" account.
let to_id = vote_id;
2019-03-05 17:27:25 -08:00
let to_lamports = bank.get_balance(&vote_id);
2019-02-15 08:45:55 -07:00
// Periodically, the staker sumbits its vote account to the rewards pool
// to exchange its credits for lamports.
let vote_state = rewards_bank
.redeem_credits(&rewards_id, &vote_keypair)
.unwrap();
2019-03-05 17:27:25 -08:00
assert!(bank.get_balance(&to_id) > to_lamports);
2019-02-15 08:45:55 -07:00
assert_eq!(vote_state.credits(), 0);
}