Only require voting account to be signed
This commit is contained in:
parent
d35b3754a2
commit
a2cb18bfe9
@ -30,20 +30,20 @@ fn calc_vote_reward(credits: u64, stake: u64) -> Result<u64, ProgramError> {
|
|||||||
|
|
||||||
fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
|
fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
|
||||||
// The owner of the vote account needs to authorize having its credits cleared.
|
// The owner of the vote account needs to authorize having its credits cleared.
|
||||||
if keyed_accounts[1].signer_key().is_none() {
|
if keyed_accounts[0].signer_key().is_none() {
|
||||||
error!("account[1] is unsigned");
|
error!("account[0] is unsigned");
|
||||||
Err(ProgramError::InvalidArgument)?;
|
Err(ProgramError::InvalidArgument)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !vote_program::check_id(&keyed_accounts[1].account.owner) {
|
if !vote_program::check_id(&keyed_accounts[0].account.owner) {
|
||||||
error!("account[1] is not assigned to the VOTE_PROGRAM");
|
error!("account[0] is not assigned to the VOTE_PROGRAM");
|
||||||
Err(ProgramError::InvalidArgument)?;
|
Err(ProgramError::InvalidArgument)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut vote_state = VoteState::deserialize(&keyed_accounts[1].account.userdata)?;
|
let mut 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.
|
||||||
if vote_state.staker_id != *keyed_accounts[2].unsigned_key() {
|
if vote_state.staker_id != *keyed_accounts[2].unsigned_key() {
|
||||||
error!("account[2] was not the VOTE_PROGRAM's staking account");
|
error!("account[2] was not the VOTE_PROGRAM's staking account");
|
||||||
Err(ProgramError::InvalidArgument)?;
|
Err(ProgramError::InvalidArgument)?;
|
||||||
@ -60,13 +60,13 @@ fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), Progra
|
|||||||
let lamports = calc_vote_reward(vote_state.credits(), stake)?;
|
let lamports = calc_vote_reward(vote_state.credits(), stake)?;
|
||||||
|
|
||||||
// Transfer rewards from the rewards pool to the staking account.
|
// Transfer rewards from the rewards pool to the staking account.
|
||||||
keyed_accounts[0].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
|
// TODO: The runtime should reject this, because this program
|
||||||
// is not the owner of the VoteState account.
|
// 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[0].account.userdata)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -111,8 +111,8 @@ mod tests {
|
|||||||
to_account: &mut Account,
|
to_account: &mut Account,
|
||||||
) -> Result<VoteState, ProgramError> {
|
) -> Result<VoteState, ProgramError> {
|
||||||
let mut keyed_accounts = [
|
let mut keyed_accounts = [
|
||||||
KeyedAccount::new(rewards_id, true, rewards_account),
|
|
||||||
KeyedAccount::new(vote_id, true, vote_account),
|
KeyedAccount::new(vote_id, true, vote_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)?;
|
||||||
|
@ -31,16 +31,16 @@ impl RewardsTransaction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_redeem_credits(
|
pub fn new_redeem_credits(
|
||||||
keypair: &Keypair,
|
vote_keypair: &Keypair,
|
||||||
vote_id: Pubkey,
|
rewards_id: Pubkey,
|
||||||
to_id: Pubkey,
|
to_id: Pubkey,
|
||||||
last_id: Hash,
|
last_id: Hash,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
) -> Transaction {
|
) -> Transaction {
|
||||||
let instruction = RewardsInstruction::RedeemVoteCredits;
|
let instruction = RewardsInstruction::RedeemVoteCredits;
|
||||||
Transaction::new(
|
Transaction::new(
|
||||||
keypair,
|
vote_keypair,
|
||||||
&[vote_id, to_id],
|
&[rewards_id, to_id],
|
||||||
rewards_program::id(),
|
rewards_program::id(),
|
||||||
&instruction,
|
&instruction,
|
||||||
last_id,
|
last_id,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user