Go object-oriented

Easy to imagine a trait here that's implemented using a Bank or
a testnet.
This commit is contained in:
Greg Fitzgerald 2019-02-19 10:32:21 -07:00
parent 64dcc31ac7
commit b9bb92099e
1 changed files with 62 additions and 44 deletions

View File

@ -8,73 +8,89 @@ use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::vote_program::{self, VoteState};
use solana_sdk::vote_transaction::VoteTransaction;
fn create_rewards_account(
bank: &Bank,
from_keypair: &Keypair,
rewards_id: Pubkey,
lamports: u64,
) -> Result<()> {
let last_id = bank.last_id();
let tx = RewardsTransaction::new_account(from_keypair, rewards_id, last_id, lamports, 0);
bank.process_transaction(&tx)
struct RewardsBank<'a> {
bank: &'a Bank,
}
fn create_vote_account(
bank: &Bank,
from_keypair: &Keypair,
vote_id: Pubkey,
lamports: u64,
) -> Result<()> {
let last_id = bank.last_id();
let tx = VoteTransaction::new_account(from_keypair, vote_id, last_id, lamports, 0);
bank.process_transaction(&tx)
}
impl<'a> RewardsBank<'a> {
fn new(bank: &'a Bank) -> Self {
bank.add_native_program("solana_rewards_program", &rewards_program::id());
Self { bank }
}
fn submit_vote(bank: &Bank, vote_keypair: &Keypair, tick_height: u64) -> Result<VoteState> {
let last_id = bank.last_id();
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, last_id, 0);
bank.process_transaction(&tx)?;
bank.register_tick(&hash(last_id.as_ref()));
fn create_rewards_account(
&self,
from_keypair: &Keypair,
rewards_id: Pubkey,
lamports: u64,
) -> Result<()> {
let last_id = self.bank.last_id();
let tx = RewardsTransaction::new_account(from_keypair, rewards_id, last_id, lamports, 0);
self.bank.process_transaction(&tx)
}
let vote_account = bank.get_account(&vote_keypair.pubkey()).unwrap();
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
}
fn create_vote_account(
&self,
from_keypair: &Keypair,
vote_id: Pubkey,
lamports: u64,
) -> Result<()> {
let last_id = self.bank.last_id();
let tx = VoteTransaction::new_account(from_keypair, vote_id, last_id, lamports, 0);
self.bank.process_transaction(&tx)
}
fn redeem_credits(
bank: &Bank,
rewards_id: Pubkey,
vote_keypair: &Keypair,
to_id: Pubkey,
) -> Result<VoteState> {
let last_id = bank.last_id();
let tx = RewardsTransaction::new_redeem_credits(&vote_keypair, rewards_id, to_id, last_id, 0);
bank.process_transaction(&tx)?;
let vote_account = bank.get_account(&vote_keypair.pubkey()).unwrap();
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
fn submit_vote(&self, vote_keypair: &Keypair, tick_height: u64) -> Result<VoteState> {
let last_id = self.bank.last_id();
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, last_id, 0);
self.bank.process_transaction(&tx)?;
self.bank.register_tick(&hash(last_id.as_ref()));
let vote_account = self.bank.get_account(&vote_keypair.pubkey()).unwrap();
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
}
fn redeem_credits(
&self,
rewards_id: Pubkey,
vote_keypair: &Keypair,
to_id: Pubkey,
) -> Result<VoteState> {
let last_id = self.bank.last_id();
let tx =
RewardsTransaction::new_redeem_credits(&vote_keypair, rewards_id, to_id, last_id, 0);
self.bank.process_transaction(&tx)?;
let vote_account = self.bank.get_account(&vote_keypair.pubkey()).unwrap();
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
}
}
#[test]
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());
let rewards_bank = RewardsBank::new(&bank);
// Create a rewards account to hold all rewards pool tokens.
let rewards_keypair = Keypair::new();
let rewards_id = rewards_keypair.pubkey();
create_rewards_account(&bank, &from_keypair, rewards_id, 100).unwrap();
rewards_bank
.create_rewards_account(&from_keypair, rewards_id, 100)
.unwrap();
// 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();
create_vote_account(&bank, &from_keypair, vote_id, 100).unwrap();
rewards_bank
.create_vote_account(&from_keypair, vote_id, 100)
.unwrap();
// The validator submits votes to accumulate credits.
for _ in 0..vote_program::MAX_VOTE_HISTORY {
let vote_state = submit_vote(&bank, &vote_keypair, 1).unwrap();
let vote_state = rewards_bank.submit_vote(&vote_keypair, 1).unwrap();
assert_eq!(vote_state.credits(), 0);
}
let vote_state = submit_vote(&bank, &vote_keypair, 1).unwrap();
let vote_state = rewards_bank.submit_vote(&vote_keypair, 1).unwrap();
assert_eq!(vote_state.credits(), 1);
// TODO: Add VoteInstruction::RegisterStakerId so that we don't need to point the "to"
@ -84,7 +100,9 @@ fn test_redeem_vote_credits_via_bank() {
// Periodically, the staker sumbits its vote account to the rewards pool
// to exchange its credits for lamports.
let vote_state = redeem_credits(&bank, rewards_id, &vote_keypair, to_id).unwrap();
let vote_state = rewards_bank
.redeem_credits(rewards_id, &vote_keypair, to_id)
.unwrap();
assert!(bank.get_balance(&to_id) > to_tokens);
assert_eq!(vote_state.credits(), 0);
}