Add unit-test for Rewards program

This commit is contained in:
Greg Fitzgerald 2019-02-13 11:33:39 -07:00
parent 134cd7ab04
commit 2c5cbaff25
8 changed files with 151 additions and 98 deletions

1
Cargo.lock generated
View File

@ -2213,6 +2213,7 @@ dependencies = [
"solana-logger 0.12.0", "solana-logger 0.12.0",
"solana-metrics 0.12.0", "solana-metrics 0.12.0",
"solana-sdk 0.12.0", "solana-sdk 0.12.0",
"solana-vote-program 0.12.0",
] ]
[[package]] [[package]]

View File

@ -17,6 +17,9 @@ solana-logger = { path = "../../../logger", version = "0.12.0" }
solana-metrics = { path = "../../../metrics", version = "0.12.0" } solana-metrics = { path = "../../../metrics", version = "0.12.0" }
solana-sdk = { path = "../../../sdk", version = "0.12.0" } solana-sdk = { path = "../../../sdk", version = "0.12.0" }
[dev-dependencies]
solana-vote-program = { path = "../vote", version = "0.12.0" }
[lib] [lib]
name = "solana_rewards_program" name = "solana_rewards_program"
crate-type = ["cdylib"] crate-type = ["cdylib"]

View File

@ -63,6 +63,8 @@ fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), Progra
keyed_accounts[0].account.tokens -= lamports; keyed_accounts[0].account.tokens -= lamports;
keyed_accounts[2].account.tokens += lamports; keyed_accounts[2].account.tokens += lamports;
// TODO: The runtime should reject this, because this program
// is not the owner of the VoteState account.
vote_state.clear_credits(); vote_state.clear_credits();
vote_state.serialize(&mut keyed_accounts[1].account.userdata)?; vote_state.serialize(&mut keyed_accounts[1].account.userdata)?;
@ -91,65 +93,89 @@ fn entrypoint(
} }
} }
//#[cfg(test)] #[cfg(test)]
//mod tests { mod tests {
// use super::*; use super::*;
// use solana_sdk::account::Account; use solana_sdk::account::Account;
// use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::rewards_program;
// use solana_sdk::vote_program; use solana_sdk::signature::{Keypair, KeypairUtil};
// use solana_sdk::vote_program::{self, Vote};
// fn create_vote_account(tokens: u64) -> Account { use solana_vote_program;
// let space = vote_program::get_max_size();
// Account::new(tokens, space, vote_program::id()) fn create_rewards_account(tokens: u64) -> Account {
// } let space = rewards_program::get_max_size();
// Account::new(tokens, space, rewards_program::id())
// fn register_and_deserialize( }
// from_id: &Pubkey,
// from_account: &mut Account, fn redeem_vote_credits_and_deserialize(
// vote_id: &Pubkey, rewards_id: &Pubkey,
// vote_account: &mut Account, rewards_account: &mut Account,
// ) -> Result<VoteState, ProgramError> { vote_id: &Pubkey,
// let mut keyed_accounts = [ vote_account: &mut Account,
// KeyedAccount::new(from_id, true, from_account), to_id: &Pubkey,
// KeyedAccount::new(vote_id, false, vote_account), to_account: &mut Account,
// ]; ) -> Result<VoteState, ProgramError> {
// register(&mut keyed_accounts)?; let mut keyed_accounts = [
// let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap(); KeyedAccount::new(rewards_id, true, rewards_account),
// Ok(vote_state) KeyedAccount::new(vote_id, true, vote_account),
// } KeyedAccount::new(to_id, false, to_account),
// ];
// fn vote_and_deserialize( redeem_vote_credits(&mut keyed_accounts)?;
// vote_id: &Pubkey, let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
// vote_account: &mut Account, Ok(vote_state)
// vote: Vote, }
// ) -> Result<VoteState, ProgramError> {
// let mut keyed_accounts = [KeyedAccount::new(vote_id, true, vote_account)]; #[test]
// process_vote(&mut keyed_accounts, vote)?; fn test_redeem_vote_credits() {
// let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap(); let from_id = Keypair::new().pubkey();
// Ok(vote_state) let mut from_account = Account::new(100, 0, Pubkey::default());
// }
// let vote_id = Keypair::new().pubkey();
// #[test] let mut vote_account = solana_vote_program::create_vote_account(100);
// fn test_redeem_vote_credits() { solana_vote_program::register_and_deserialize(
// let from_id = Keypair::new().pubkey(); &from_id,
// let mut from_account = Account::new(100, 0, Pubkey::default()); &mut from_account,
// &vote_id,
// let vote_id = Keypair::new().pubkey(); &mut vote_account,
// let mut vote_account = create_vote_account(100); )
// register_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account).unwrap(); .unwrap();
//
// for _ in 0..vote_program::MAX_VOTE_HISTORY { for _ in 0..vote_program::MAX_VOTE_HISTORY {
// let vote = Vote::new(1); let vote = Vote::new(1);
// let vote_state = let vote_state = solana_vote_program::vote_and_deserialize(
// vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap(); &vote_id,
// assert_eq!(vote_state.credits(), 0); &mut vote_account,
// } vote.clone(),
// )
// let vote = Vote::new(1); .unwrap();
// let vote_state = vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap(); assert_eq!(vote_state.credits(), 0);
// assert_eq!(vote_state.credits(), 1); }
//
// let vote_state = redeem_vote_credits_and_deserialize() let vote = Vote::new(1);
// assert_eq!(vote_state.credits(), 0); let vote_state =
// } solana_vote_program::vote_and_deserialize(&vote_id, &mut vote_account, vote.clone())
//} .unwrap();
assert_eq!(vote_state.credits(), 1);
let rewards_id = Keypair::new().pubkey();
let mut rewards_account = create_rewards_account(100);
// TODO: Add VoteInstruction::RegisterStakerId so that we don't need to point the "to"
// account to the "from" account.
let to_id = from_id;
let mut to_account = from_account;
let to_tokens = to_account.tokens;
let vote_state = redeem_vote_credits_and_deserialize(
&rewards_id,
&mut rewards_account,
&vote_id,
&mut vote_account,
&to_id,
&mut to_account,
)
.unwrap();
assert_eq!(vote_state.credits(), 0);
assert!(to_account.tokens > to_tokens);
}
}

View File

@ -22,5 +22,5 @@ solana-runtime = { path = "../../../runtime", version = "0.12.0" }
[lib] [lib]
name = "solana_vote_program" name = "solana_vote_program"
crate-type = ["cdylib"] crate-type = ["cdylib", "lib"]

View File

@ -3,7 +3,7 @@
use bincode::deserialize; use bincode::deserialize;
use log::*; use log::*;
use solana_sdk::account::KeyedAccount; use solana_sdk::account::{Account, KeyedAccount};
use solana_sdk::native_program::ProgramError; use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::solana_entrypoint; use solana_sdk::solana_entrypoint;
@ -76,43 +76,41 @@ fn entrypoint(
} }
} }
pub fn create_vote_account(tokens: u64) -> Account {
let space = vote_program::get_max_size();
Account::new(tokens, space, vote_program::id())
}
pub fn register_and_deserialize(
from_id: &Pubkey,
from_account: &mut Account,
vote_id: &Pubkey,
vote_account: &mut Account,
) -> Result<VoteState, ProgramError> {
let mut keyed_accounts = [
KeyedAccount::new(from_id, true, from_account),
KeyedAccount::new(vote_id, false, vote_account),
];
register(&mut keyed_accounts)?;
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
Ok(vote_state)
}
pub fn vote_and_deserialize(
vote_id: &Pubkey,
vote_account: &mut Account,
vote: Vote,
) -> Result<VoteState, ProgramError> {
let mut keyed_accounts = [KeyedAccount::new(vote_id, true, vote_account)];
process_vote(&mut keyed_accounts, vote)?;
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
Ok(vote_state)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use solana_sdk::account::Account;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::vote_program;
fn create_vote_account(tokens: u64) -> Account {
let space = vote_program::get_max_size();
Account::new(tokens, space, vote_program::id())
}
fn register_and_deserialize(
from_id: &Pubkey,
from_account: &mut Account,
vote_id: &Pubkey,
vote_account: &mut Account,
) -> Result<VoteState, ProgramError> {
let mut keyed_accounts = [
KeyedAccount::new(from_id, true, from_account),
KeyedAccount::new(vote_id, false, vote_account),
];
register(&mut keyed_accounts)?;
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
Ok(vote_state)
}
fn vote_and_deserialize(
vote_id: &Pubkey,
vote_account: &mut Account,
vote: Vote,
) -> Result<VoteState, ProgramError> {
let mut keyed_accounts = [KeyedAccount::new(vote_id, true, vote_account)];
process_vote(&mut keyed_accounts, vote)?;
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
Ok(vote_state)
}
#[test] #[test]
fn test_voter_registration() { fn test_voter_registration() {

View File

@ -12,6 +12,7 @@ pub mod native_program;
pub mod packet; pub mod packet;
pub mod payment_plan; pub mod payment_plan;
pub mod pubkey; pub mod pubkey;
pub mod rewards_program;
pub mod shortvec; pub mod shortvec;
pub mod signature; pub mod signature;
pub mod storage_program; pub mod storage_program;

View File

@ -0,0 +1,24 @@
use crate::pubkey::Pubkey;
use bincode::serialized_size;
pub const REWARDS_PROGRAM_ID: [u8; 32] = [
133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
];
pub fn check_id(program_id: &Pubkey) -> bool {
program_id.as_ref() == REWARDS_PROGRAM_ID
}
pub fn id() -> Pubkey {
Pubkey::new(&REWARDS_PROGRAM_ID)
}
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
struct RewardsState {}
/// Upper limit on the size of the Rewards State.
pub fn get_max_size() -> usize {
let rewards_state = RewardsState::default();
serialized_size(&rewards_state).unwrap() as usize
}

View File

@ -20,7 +20,7 @@ pub fn id() -> Pubkey {
} }
// Maximum number of votes to keep around // Maximum number of votes to keep around
const MAX_VOTE_HISTORY: usize = 32; pub const MAX_VOTE_HISTORY: usize = 32;
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Vote { pub struct Vote {