No longer modify external userdata
This commit is contained in:
@ -40,7 +40,11 @@ fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), Progra
|
|||||||
Err(ProgramError::InvalidArgument)?;
|
Err(ProgramError::InvalidArgument)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata)?;
|
// TODO: Verify the next instruction in the transaction being processed is
|
||||||
|
// VoteInstruction::ClearCredits and that it points to the same vote account
|
||||||
|
// as keyed_accounts[0].
|
||||||
|
|
||||||
|
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
|
//// 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.
|
//// at the time of voting, not at credit redemption.
|
||||||
@ -63,11 +67,6 @@ fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), Progra
|
|||||||
keyed_accounts[1].account.tokens -= lamports;
|
keyed_accounts[1].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.serialize(&mut keyed_accounts[0].account.userdata)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,22 +100,20 @@ mod tests {
|
|||||||
Account::new(tokens, space, rewards_program::id())
|
Account::new(tokens, space, rewards_program::id())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redeem_vote_credits_and_deserialize(
|
fn redeem_vote_credits_(
|
||||||
rewards_id: &Pubkey,
|
rewards_id: &Pubkey,
|
||||||
rewards_account: &mut Account,
|
rewards_account: &mut Account,
|
||||||
vote_id: &Pubkey,
|
vote_id: &Pubkey,
|
||||||
vote_account: &mut Account,
|
vote_account: &mut Account,
|
||||||
to_id: &Pubkey,
|
to_id: &Pubkey,
|
||||||
to_account: &mut Account,
|
to_account: &mut Account,
|
||||||
) -> Result<VoteState, ProgramError> {
|
) -> Result<(), ProgramError> {
|
||||||
let mut keyed_accounts = [
|
let mut keyed_accounts = [
|
||||||
KeyedAccount::new(vote_id, true, vote_account),
|
KeyedAccount::new(vote_id, true, vote_account),
|
||||||
KeyedAccount::new(rewards_id, false, rewards_account),
|
KeyedAccount::new(rewards_id, false, rewards_account),
|
||||||
KeyedAccount::new(to_id, false, to_account),
|
KeyedAccount::new(to_id, false, to_account),
|
||||||
];
|
];
|
||||||
redeem_vote_credits(&mut keyed_accounts)?;
|
redeem_vote_credits(&mut keyed_accounts)
|
||||||
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
|
|
||||||
Ok(vote_state)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -156,7 +153,7 @@ mod tests {
|
|||||||
let mut to_account = from_account;
|
let mut to_account = from_account;
|
||||||
let to_tokens = to_account.tokens;
|
let to_tokens = to_account.tokens;
|
||||||
|
|
||||||
let vote_state = redeem_vote_credits_and_deserialize(
|
redeem_vote_credits_(
|
||||||
&rewards_id,
|
&rewards_id,
|
||||||
&mut rewards_account,
|
&mut rewards_account,
|
||||||
&vote_id,
|
&vote_id,
|
||||||
@ -165,7 +162,6 @@ mod tests {
|
|||||||
&mut to_account,
|
&mut to_account,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(vote_state.credits(), 0);
|
|
||||||
assert!(to_account.tokens > to_tokens);
|
assert!(to_account.tokens > to_tokens);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,11 @@ fn redeem_credits(
|
|||||||
) -> Result<VoteState, RuntimeError> {
|
) -> Result<VoteState, RuntimeError> {
|
||||||
let last_id = Hash::default();
|
let last_id = Hash::default();
|
||||||
let rewards_program_account = create_program_account("solana_rewards_program");
|
let rewards_program_account = create_program_account("solana_rewards_program");
|
||||||
let loaders = &mut [vec![(rewards_program::id(), rewards_program_account)]];
|
let vote_program_account = create_program_account("solana_vote_program");
|
||||||
|
let loaders = &mut [
|
||||||
|
vec![(rewards_program::id(), rewards_program_account)],
|
||||||
|
vec![(vote_program::id(), vote_program_account)],
|
||||||
|
];
|
||||||
|
|
||||||
let accounts = &mut [
|
let accounts = &mut [
|
||||||
vote_account.clone(),
|
vote_account.clone(),
|
||||||
|
@ -7,7 +7,8 @@ use solana_sdk::hash::Hash;
|
|||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::signature::Keypair;
|
use solana_sdk::signature::Keypair;
|
||||||
use solana_sdk::system_transaction::SystemTransaction;
|
use solana_sdk::system_transaction::SystemTransaction;
|
||||||
use solana_sdk::transaction::Transaction;
|
use solana_sdk::transaction::{Instruction, Transaction};
|
||||||
|
use solana_sdk::vote_program::{self, VoteInstruction};
|
||||||
|
|
||||||
pub struct RewardsTransaction {}
|
pub struct RewardsTransaction {}
|
||||||
|
|
||||||
@ -37,14 +38,16 @@ impl RewardsTransaction {
|
|||||||
last_id: Hash,
|
last_id: Hash,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
) -> Transaction {
|
) -> Transaction {
|
||||||
let instruction = RewardsInstruction::RedeemVoteCredits;
|
Transaction::new_with_instructions(
|
||||||
Transaction::new(
|
&[vote_keypair],
|
||||||
vote_keypair,
|
|
||||||
&[rewards_id, to_id],
|
&[rewards_id, to_id],
|
||||||
rewards_program::id(),
|
|
||||||
&instruction,
|
|
||||||
last_id,
|
last_id,
|
||||||
fee,
|
fee,
|
||||||
|
vec![rewards_program::id(), vote_program::id()],
|
||||||
|
vec![
|
||||||
|
Instruction::new(0, &RewardsInstruction::RedeemVoteCredits, vec![0, 1, 2]),
|
||||||
|
Instruction::new(1, &VoteInstruction::ClearCredits, vec![0]),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,5 +38,6 @@ fn entrypoint(
|
|||||||
);
|
);
|
||||||
vote_program::process_vote(keyed_accounts, vote)
|
vote_program::process_vote(keyed_accounts, vote)
|
||||||
}
|
}
|
||||||
|
VoteInstruction::ClearCredits => vote_program::clear_credits(keyed_accounts),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,9 @@ pub enum VoteInstruction {
|
|||||||
/// identified by keys[0] for voting
|
/// identified by keys[0] for voting
|
||||||
RegisterAccount,
|
RegisterAccount,
|
||||||
Vote(Vote),
|
Vote(Vote),
|
||||||
|
/// Clear the credits in the vote account
|
||||||
|
/// * Transaction::keys[0] - the "vote account"
|
||||||
|
ClearCredits,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
@ -148,6 +151,18 @@ pub fn process_vote(keyed_accounts: &mut [KeyedAccount], vote: Vote) -> Result<(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clear_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
|
||||||
|
if !check_id(&keyed_accounts[0].account.owner) {
|
||||||
|
error!("account[0] is not assigned to the VOTE_PROGRAM");
|
||||||
|
Err(ProgramError::InvalidArgument)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata)?;
|
||||||
|
vote_state.clear_credits();
|
||||||
|
vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_vote_account(tokens: u64) -> Account {
|
pub fn create_vote_account(tokens: u64) -> Account {
|
||||||
let space = get_max_size();
|
let space = get_max_size();
|
||||||
Account::new(tokens, space, id())
|
Account::new(tokens, space, id())
|
||||||
|
Reference in New Issue
Block a user