Migrate Rewards test from runtime to Bank
This commit is contained in:
parent
36546b4c4c
commit
64dcc31ac7
|
@ -1,152 +1,90 @@
|
||||||
use solana_rewards_api::rewards_program;
|
use solana_rewards_api::rewards_program;
|
||||||
use solana_rewards_api::rewards_transaction::RewardsTransaction;
|
use solana_rewards_api::rewards_transaction::RewardsTransaction;
|
||||||
use solana_runtime::runtime::{execute_transaction, RuntimeError};
|
use solana_runtime::bank::{Bank, Result};
|
||||||
use solana_sdk::account::Account;
|
use solana_sdk::genesis_block::GenesisBlock;
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::hash::hash;
|
||||||
use solana_sdk::native_loader::create_program_account;
|
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::system_program;
|
|
||||||
use solana_sdk::vote_program::{self, VoteState};
|
use solana_sdk::vote_program::{self, VoteState};
|
||||||
use solana_sdk::vote_transaction::VoteTransaction;
|
use solana_sdk::vote_transaction::VoteTransaction;
|
||||||
|
|
||||||
fn create_rewards_account(
|
fn create_rewards_account(
|
||||||
|
bank: &Bank,
|
||||||
from_keypair: &Keypair,
|
from_keypair: &Keypair,
|
||||||
from_account: &mut Account,
|
|
||||||
rewards_id: Pubkey,
|
rewards_id: Pubkey,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
) -> Result<Account, RuntimeError> {
|
) -> Result<()> {
|
||||||
let system_program_account = create_program_account("solana_system_program");
|
let last_id = bank.last_id();
|
||||||
let loaders = &mut [vec![(system_program::id(), system_program_account)]];
|
|
||||||
let last_id = Hash::default();
|
|
||||||
let tx = RewardsTransaction::new_account(from_keypair, rewards_id, last_id, lamports, 0);
|
let tx = RewardsTransaction::new_account(from_keypair, rewards_id, last_id, lamports, 0);
|
||||||
let accounts = &mut [from_account.clone(), Account::new(0, 0, Pubkey::default())];
|
bank.process_transaction(&tx)
|
||||||
execute_transaction(&tx, loaders, accounts, 0)?;
|
|
||||||
|
|
||||||
// Simulate committing account after successful transaction
|
|
||||||
from_account.tokens = accounts[0].tokens;
|
|
||||||
|
|
||||||
Ok(accounts[1].clone())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_vote_account(
|
fn create_vote_account(
|
||||||
|
bank: &Bank,
|
||||||
from_keypair: &Keypair,
|
from_keypair: &Keypair,
|
||||||
from_account: &mut Account,
|
|
||||||
vote_id: Pubkey,
|
vote_id: Pubkey,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
) -> Result<Account, RuntimeError> {
|
) -> Result<()> {
|
||||||
let system_program_account = create_program_account("solana_system_program");
|
let last_id = bank.last_id();
|
||||||
let vote_program_account = create_program_account("solana_vote_program");
|
|
||||||
let loaders = &mut [
|
|
||||||
vec![(system_program::id(), system_program_account)],
|
|
||||||
vec![(vote_program::id(), vote_program_account)],
|
|
||||||
];
|
|
||||||
let last_id = Hash::default();
|
|
||||||
let tx = VoteTransaction::new_account(from_keypair, vote_id, last_id, lamports, 0);
|
let tx = VoteTransaction::new_account(from_keypair, vote_id, last_id, lamports, 0);
|
||||||
let accounts = &mut [from_account.clone(), Account::new(0, 0, Pubkey::default())];
|
bank.process_transaction(&tx)
|
||||||
execute_transaction(&tx, loaders, accounts, 0)?;
|
|
||||||
|
|
||||||
// Simulate committing account after successful transaction
|
|
||||||
from_account.tokens = accounts[1].tokens;
|
|
||||||
|
|
||||||
Ok(accounts[1].clone())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn submit_vote(
|
fn submit_vote(bank: &Bank, vote_keypair: &Keypair, tick_height: u64) -> Result<VoteState> {
|
||||||
vote_keypair: &Keypair,
|
let last_id = bank.last_id();
|
||||||
vote_account: &mut Account,
|
|
||||||
tick_height: u64,
|
|
||||||
) -> Result<VoteState, RuntimeError> {
|
|
||||||
let vote_program_account = create_program_account("solana_vote_program");
|
|
||||||
let loaders = &mut [vec![(vote_program::id(), vote_program_account)]];
|
|
||||||
let last_id = Hash::default();
|
|
||||||
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, last_id, 0);
|
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, last_id, 0);
|
||||||
let accounts = &mut [vote_account.clone()];
|
bank.process_transaction(&tx)?;
|
||||||
execute_transaction(&tx, loaders, accounts, 0)?;
|
bank.register_tick(&hash(last_id.as_ref()));
|
||||||
|
|
||||||
// Simulate committing account after successful transaction
|
|
||||||
vote_account
|
|
||||||
.userdata
|
|
||||||
.clone_from_slice(&accounts[0].userdata);
|
|
||||||
|
|
||||||
|
let vote_account = bank.get_account(&vote_keypair.pubkey()).unwrap();
|
||||||
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
|
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redeem_credits(
|
fn redeem_credits(
|
||||||
|
bank: &Bank,
|
||||||
rewards_id: Pubkey,
|
rewards_id: Pubkey,
|
||||||
rewards_account: &mut Account,
|
|
||||||
vote_keypair: &Keypair,
|
vote_keypair: &Keypair,
|
||||||
vote_account: &mut Account,
|
|
||||||
to_id: Pubkey,
|
to_id: Pubkey,
|
||||||
to_account: &mut Account,
|
) -> Result<VoteState> {
|
||||||
) -> Result<VoteState, RuntimeError> {
|
let last_id = bank.last_id();
|
||||||
let last_id = Hash::default();
|
|
||||||
let rewards_program_account = create_program_account("solana_rewards_program");
|
|
||||||
let vote_program_account = create_program_account("solana_vote_program");
|
|
||||||
let loaders = &mut [
|
|
||||||
vec![(rewards_program::id(), rewards_program_account)],
|
|
||||||
vec![(vote_program::id(), vote_program_account)],
|
|
||||||
];
|
|
||||||
|
|
||||||
let accounts = &mut [
|
|
||||||
vote_account.clone(),
|
|
||||||
rewards_account.clone(),
|
|
||||||
to_account.clone(),
|
|
||||||
];
|
|
||||||
let tx = RewardsTransaction::new_redeem_credits(&vote_keypair, rewards_id, to_id, last_id, 0);
|
let tx = RewardsTransaction::new_redeem_credits(&vote_keypair, rewards_id, to_id, last_id, 0);
|
||||||
execute_transaction(&tx, loaders, accounts, 0)?;
|
bank.process_transaction(&tx)?;
|
||||||
|
let vote_account = bank.get_account(&vote_keypair.pubkey()).unwrap();
|
||||||
// Simulate committing account after successful transaction
|
|
||||||
vote_account
|
|
||||||
.userdata
|
|
||||||
.clone_from_slice(&accounts[0].userdata);
|
|
||||||
rewards_account.tokens = accounts[1].tokens;
|
|
||||||
to_account.tokens = accounts[2].tokens;
|
|
||||||
|
|
||||||
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
|
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_redeem_vote_credits_via_runtime() {
|
fn test_redeem_vote_credits_via_bank() {
|
||||||
|
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
|
||||||
|
let bank = Bank::new(&genesis_block);
|
||||||
|
bank.add_native_program("solana_rewards_program", &rewards_program::id());
|
||||||
|
|
||||||
// Create a rewards account to hold all rewards pool tokens.
|
// Create a rewards account to hold all rewards pool tokens.
|
||||||
let from_keypair = Keypair::new();
|
|
||||||
let mut from_account = Account::new(10_000, 0, Pubkey::default());
|
|
||||||
let rewards_keypair = Keypair::new();
|
let rewards_keypair = Keypair::new();
|
||||||
let rewards_id = rewards_keypair.pubkey();
|
let rewards_id = rewards_keypair.pubkey();
|
||||||
let mut rewards_account =
|
create_rewards_account(&bank, &from_keypair, rewards_id, 100).unwrap();
|
||||||
create_rewards_account(&from_keypair, &mut from_account, rewards_id, 100).unwrap();
|
|
||||||
|
|
||||||
// A staker create a vote account account and delegates a validator to vote on its behalf.
|
// A staker create a vote account account and delegates a validator to vote on its behalf.
|
||||||
let vote_keypair = Keypair::new();
|
let vote_keypair = Keypair::new();
|
||||||
let vote_id = vote_keypair.pubkey();
|
let vote_id = vote_keypair.pubkey();
|
||||||
let mut vote_account =
|
create_vote_account(&bank, &from_keypair, vote_id, 100).unwrap();
|
||||||
create_vote_account(&from_keypair, &mut from_account, vote_id, 100).unwrap();
|
|
||||||
|
|
||||||
// The validator submits votes to accumulate credits.
|
// The validator submits votes to accumulate credits.
|
||||||
for _ in 0..vote_program::MAX_VOTE_HISTORY {
|
for _ in 0..vote_program::MAX_VOTE_HISTORY {
|
||||||
let vote_state = submit_vote(&vote_keypair, &mut vote_account, 1).unwrap();
|
let vote_state = submit_vote(&bank, &vote_keypair, 1).unwrap();
|
||||||
assert_eq!(vote_state.credits(), 0);
|
assert_eq!(vote_state.credits(), 0);
|
||||||
}
|
}
|
||||||
let vote_state = submit_vote(&vote_keypair, &mut vote_account, 1).unwrap();
|
let vote_state = submit_vote(&bank, &vote_keypair, 1).unwrap();
|
||||||
assert_eq!(vote_state.credits(), 1);
|
assert_eq!(vote_state.credits(), 1);
|
||||||
|
|
||||||
// TODO: Add VoteInstruction::RegisterStakerId so that we don't need to point the "to"
|
// TODO: Add VoteInstruction::RegisterStakerId so that we don't need to point the "to"
|
||||||
// account to the "from" account.
|
// account to the "from" account.
|
||||||
let to_id = from_keypair.pubkey();
|
let to_id = from_keypair.pubkey();
|
||||||
let mut to_account = from_account;
|
let to_tokens = bank.get_balance(&to_id);
|
||||||
let to_tokens = to_account.tokens;
|
|
||||||
|
|
||||||
// Periodically, the staker sumbits its vote account to the rewards pool
|
// Periodically, the staker sumbits its vote account to the rewards pool
|
||||||
// to exchange its credits for lamports.
|
// to exchange its credits for lamports.
|
||||||
let vote_state = redeem_credits(
|
let vote_state = redeem_credits(&bank, rewards_id, &vote_keypair, to_id).unwrap();
|
||||||
rewards_id,
|
assert!(bank.get_balance(&to_id) > to_tokens);
|
||||||
&mut rewards_account,
|
|
||||||
&vote_keypair,
|
|
||||||
&mut vote_account,
|
|
||||||
to_id,
|
|
||||||
&mut to_account,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
assert!(to_account.tokens > to_tokens);
|
|
||||||
assert_eq!(vote_state.credits(), 0);
|
assert_eq!(vote_state.credits(), 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ mod accounts;
|
||||||
pub mod bank;
|
pub mod bank;
|
||||||
pub mod bloom;
|
pub mod bloom;
|
||||||
mod last_id_queue;
|
mod last_id_queue;
|
||||||
pub mod runtime;
|
mod runtime;
|
||||||
mod status_cache;
|
mod status_cache;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
|
Loading…
Reference in New Issue