Go object-oriented
Easy to imagine a trait here that's implemented using a Bank or a testnet.
This commit is contained in:
parent
64dcc31ac7
commit
b9bb92099e
|
@ -8,73 +8,89 @@ use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
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(
|
struct RewardsBank<'a> {
|
||||||
bank: &Bank,
|
bank: &'a 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_vote_account(
|
impl<'a> RewardsBank<'a> {
|
||||||
bank: &Bank,
|
fn new(bank: &'a Bank) -> Self {
|
||||||
from_keypair: &Keypair,
|
bank.add_native_program("solana_rewards_program", &rewards_program::id());
|
||||||
vote_id: Pubkey,
|
Self { bank }
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn submit_vote(bank: &Bank, vote_keypair: &Keypair, tick_height: u64) -> Result<VoteState> {
|
fn create_rewards_account(
|
||||||
let last_id = bank.last_id();
|
&self,
|
||||||
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, last_id, 0);
|
from_keypair: &Keypair,
|
||||||
bank.process_transaction(&tx)?;
|
rewards_id: Pubkey,
|
||||||
bank.register_tick(&hash(last_id.as_ref()));
|
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();
|
fn create_vote_account(
|
||||||
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
|
&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(
|
fn submit_vote(&self, vote_keypair: &Keypair, tick_height: u64) -> Result<VoteState> {
|
||||||
bank: &Bank,
|
let last_id = self.bank.last_id();
|
||||||
rewards_id: Pubkey,
|
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, last_id, 0);
|
||||||
vote_keypair: &Keypair,
|
self.bank.process_transaction(&tx)?;
|
||||||
to_id: Pubkey,
|
self.bank.register_tick(&hash(last_id.as_ref()));
|
||||||
) -> Result<VoteState> {
|
|
||||||
let last_id = bank.last_id();
|
let vote_account = self.bank.get_account(&vote_keypair.pubkey()).unwrap();
|
||||||
let tx = RewardsTransaction::new_redeem_credits(&vote_keypair, rewards_id, to_id, last_id, 0);
|
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
|
||||||
bank.process_transaction(&tx)?;
|
}
|
||||||
let vote_account = 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]
|
#[test]
|
||||||
fn test_redeem_vote_credits_via_bank() {
|
fn test_redeem_vote_credits_via_bank() {
|
||||||
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
|
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
|
||||||
let bank = Bank::new(&genesis_block);
|
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.
|
// Create a rewards account to hold all rewards pool tokens.
|
||||||
let rewards_keypair = Keypair::new();
|
let rewards_keypair = Keypair::new();
|
||||||
let rewards_id = rewards_keypair.pubkey();
|
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.
|
// 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();
|
||||||
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.
|
// 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(&bank, &vote_keypair, 1).unwrap();
|
let vote_state = rewards_bank.submit_vote(&vote_keypair, 1).unwrap();
|
||||||
assert_eq!(vote_state.credits(), 0);
|
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);
|
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"
|
||||||
|
@ -84,7 +100,9 @@ fn test_redeem_vote_credits_via_bank() {
|
||||||
|
|
||||||
// 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(&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!(bank.get_balance(&to_id) > to_tokens);
|
||||||
assert_eq!(vote_state.credits(), 0);
|
assert_eq!(vote_state.credits(), 0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue