1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use bincode::deserialize;
use log::*;
use solana_rewards_api::rewards_instruction::RewardsInstruction;
use solana_sdk::account::KeyedAccount;
use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::solana_entrypoint;
use solana_vote_api::vote_state::VoteState;
const INTEREST_PER_CREDIT_DIVISOR: u64 = 100;
const MINIMUM_CREDITS_PER_REDEMPTION: u64 = 1;
fn calc_vote_reward(credits: u64, stake: u64) -> Result<u64, ProgramError> {
if credits < MINIMUM_CREDITS_PER_REDEMPTION {
error!("Credit redemption too early");
Err(ProgramError::GenericError)?;
}
Ok(credits * (stake / INTEREST_PER_CREDIT_DIVISOR))
}
fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
if keyed_accounts[0].signer_key().is_none() {
error!("account[0] is unsigned");
Err(ProgramError::InvalidArgument)?;
}
if !solana_vote_api::check_id(&keyed_accounts[0].account.owner) {
error!("account[0] is not assigned to the VOTE_PROGRAM");
Err(ProgramError::InvalidArgument)?;
}
let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata)?;
let stake = keyed_accounts[0].account.lamports;
if stake == 0 {
error!("staking account has no stake");
Err(ProgramError::InvalidArgument)?;
}
let lamports = calc_vote_reward(vote_state.credits(), stake)?;
keyed_accounts[1].account.lamports -= lamports;
keyed_accounts[0].account.lamports += lamports;
Ok(())
}
solana_entrypoint!(entrypoint);
fn entrypoint(
_program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
_tick_height: u64,
) -> Result<(), ProgramError> {
solana_logger::setup();
trace!("process_instruction: {:?}", data);
trace!("keyed_accounts: {:?}", keyed_accounts);
match deserialize(data).map_err(|_| ProgramError::InvalidUserdata)? {
RewardsInstruction::RedeemVoteCredits => redeem_vote_credits(keyed_accounts),
}
}
#[cfg(test)]
mod tests {
use super::*;
use solana_rewards_api;
use solana_rewards_api::rewards_state::RewardsState;
use solana_sdk::account::Account;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_vote_api::vote_instruction::Vote;
use solana_vote_api::vote_state;
fn create_rewards_account(lamports: u64) -> Account {
let space = RewardsState::max_size();
Account::new(lamports, space, &solana_rewards_api::id())
}
fn redeem_vote_credits_(
rewards_id: &Pubkey,
rewards_account: &mut Account,
vote_id: &Pubkey,
vote_account: &mut Account,
) -> Result<(), ProgramError> {
let mut keyed_accounts = [
KeyedAccount::new(vote_id, true, vote_account),
KeyedAccount::new(rewards_id, false, rewards_account),
];
redeem_vote_credits(&mut keyed_accounts)
}
#[test]
fn test_redeem_vote_credits_via_program() {
let vote_id = Keypair::new().pubkey();
let mut vote_account = vote_state::create_vote_account(100);
vote_state::initialize_and_deserialize(&vote_id, &mut vote_account).unwrap();
for i in 0..vote_state::MAX_LOCKOUT_HISTORY {
let vote = Vote::new(i as u64);
let vote_state =
vote_state::vote_and_deserialize(&vote_id, &mut vote_account, vote.clone())
.unwrap();
assert_eq!(vote_state.credits(), 0);
}
let vote = Vote::new(vote_state::MAX_LOCKOUT_HISTORY as u64 + 1);
let vote_state =
vote_state::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);
let lamports_before = vote_account.lamports;
redeem_vote_credits_(
&rewards_id,
&mut rewards_account,
&vote_id,
&mut vote_account,
)
.unwrap();
assert!(vote_account.lamports > lamports_before);
}
}