Refactor Vote Program Account setup (#2992)

This commit is contained in:
Sagar Dhawan
2019-02-28 17:08:45 -08:00
committed by GitHub
parent d5f0e49535
commit 20e4edec61
19 changed files with 278 additions and 151 deletions

View File

@ -46,16 +46,9 @@ fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), Progra
let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata)?;
//// TODO: This assumes the staker_id is static. If not, it should use the staker_id
//// at the time of voting, not at credit redemption.
if vote_state.staker_id != *keyed_accounts[2].unsigned_key() {
error!("account[2] was not the VOTE_PROGRAM's staking account");
Err(ProgramError::InvalidArgument)?;
}
// TODO: This assumes the stake is static. If not, it should use the account value
// at the time of voting, not at credit redemption.
let stake = keyed_accounts[2].account.tokens;
let stake = keyed_accounts[0].account.tokens;
if stake == 0 {
error!("staking account has no stake");
Err(ProgramError::InvalidArgument)?;
@ -65,7 +58,7 @@ fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), Progra
// Transfer rewards from the rewards pool to the staking account.
keyed_accounts[1].account.tokens -= lamports;
keyed_accounts[2].account.tokens += lamports;
keyed_accounts[0].account.tokens += lamports;
Ok(())
}
@ -105,27 +98,24 @@ mod tests {
rewards_account: &mut Account,
vote_id: &Pubkey,
vote_account: &mut Account,
to_id: &Pubkey,
to_account: &mut Account,
) -> Result<(), ProgramError> {
let mut keyed_accounts = [
KeyedAccount::new(vote_id, true, vote_account),
KeyedAccount::new(rewards_id, false, rewards_account),
KeyedAccount::new(to_id, false, to_account),
];
redeem_vote_credits(&mut keyed_accounts)
}
#[test]
fn test_redeem_vote_credits_via_program() {
let from_id = Keypair::new().pubkey();
let mut from_account = Account::new(100, 0, Pubkey::default());
let staker_id = Keypair::new().pubkey();
let mut staker_account = Account::new(100, 0, Pubkey::default());
let vote_id = Keypair::new().pubkey();
let mut vote_account = vote_program::create_vote_account(100);
vote_program::register_and_deserialize(
&from_id,
&mut from_account,
vote_program::initialize_and_deserialize(
&staker_id,
&mut staker_account,
&vote_id,
&mut vote_account,
)
@ -147,21 +137,15 @@ mod tests {
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 tokens_before = vote_account.tokens;
redeem_vote_credits_(
&rewards_id,
&mut rewards_account,
&vote_id,
&mut vote_account,
&to_id,
&mut to_account,
)
.unwrap();
assert!(to_account.tokens > to_tokens);
assert!(vote_account.tokens > tokens_before);
}
}

View File

@ -36,7 +36,7 @@ impl<'a> RewardsBank<'a> {
lamports: u64,
) -> Result<()> {
let last_id = self.bank.last_id();
let tx = VoteTransaction::new_account(from_keypair, vote_id, last_id, lamports, 0);
let tx = VoteTransaction::fund_staking_account(from_keypair, vote_id, last_id, lamports, 0);
self.bank.process_transaction(&tx)
}
@ -50,15 +50,9 @@ impl<'a> RewardsBank<'a> {
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
}
fn redeem_credits(
&self,
rewards_id: Pubkey,
vote_keypair: &Keypair,
to_id: Pubkey,
) -> Result<VoteState> {
fn redeem_credits(&self, rewards_id: Pubkey, vote_keypair: &Keypair) -> Result<VoteState> {
let last_id = self.bank.last_id();
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, 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())
@ -97,13 +91,13 @@ fn test_redeem_vote_credits_via_bank() {
// TODO: Add VoteInstruction::RegisterStakerId so that we don't need to point the "to"
// account to the "from" account.
let to_id = from_keypair.pubkey();
let to_tokens = bank.get_balance(&to_id);
let to_id = vote_id;
let to_tokens = bank.get_balance(&vote_id);
// 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, to_id)
.redeem_credits(rewards_id, &vote_keypair)
.unwrap();
assert!(bank.get_balance(&to_id) > to_tokens);
assert_eq!(vote_state.credits(), 0);

View File

@ -9,15 +9,11 @@ pub enum RewardsInstruction {
}
impl RewardsInstruction {
pub fn new_redeem_vote_credits(
vote_id: Pubkey,
rewards_id: Pubkey,
to_id: Pubkey,
) -> BuilderInstruction {
pub fn new_redeem_vote_credits(vote_id: Pubkey, rewards_id: Pubkey) -> BuilderInstruction {
BuilderInstruction::new(
rewards_program::id(),
&RewardsInstruction::RedeemVoteCredits,
vec![(vote_id, true), (rewards_id, false), (to_id, false)],
vec![(vote_id, true), (rewards_id, false)],
)
}
}

View File

@ -35,14 +35,13 @@ impl RewardsTransaction {
pub fn new_redeem_credits(
vote_keypair: &Keypair,
rewards_id: Pubkey,
to_id: Pubkey,
last_id: Hash,
fee: u64,
) -> Transaction {
let vote_id = vote_keypair.pubkey();
TransactionBuilder::new(fee)
.push(RewardsInstruction::new_redeem_vote_credits(
vote_id, rewards_id, to_id,
vote_id, rewards_id,
))
.push(VoteInstruction::new_clear_credits(vote_id))
.sign(&[vote_keypair], last_id)

View File

@ -28,7 +28,10 @@ fn entrypoint(
}
match deserialize(data).map_err(|_| ProgramError::InvalidUserdata)? {
VoteInstruction::RegisterAccount => vote_program::register(keyed_accounts),
VoteInstruction::InitializeAccount => vote_program::initialize_account(keyed_accounts),
VoteInstruction::DelegateStake(delegate_id) => {
vote_program::delegate_stake(keyed_accounts, delegate_id)
}
VoteInstruction::Vote(vote) => {
debug!("{:?} by {}", vote, keyed_accounts[0].signer_key().unwrap());
solana_metrics::submit(