Remove Instruction wrapper structs and name functions after enum fields
This commit is contained in:
@ -3,7 +3,7 @@ use crate::vote_state::VoteState;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use solana_sdk::instruction::{AccountMeta, Instruction};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
use solana_sdk::system_instruction;
|
||||
|
||||
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
pub struct Vote {
|
||||
@ -37,44 +37,42 @@ pub enum VoteInstruction {
|
||||
ClearCredits,
|
||||
}
|
||||
|
||||
impl VoteInstruction {
|
||||
fn new_initialize_account(vote_id: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, false)];
|
||||
Instruction::new(id(), &VoteInstruction::InitializeAccount, account_metas)
|
||||
}
|
||||
|
||||
pub fn new_account(from_id: &Pubkey, staker_id: &Pubkey, lamports: u64) -> Vec<Instruction> {
|
||||
let space = VoteState::max_size() as u64;
|
||||
let create_ix = SystemInstruction::new_account(&from_id, staker_id, lamports, space, &id());
|
||||
let init_ix = VoteInstruction::new_initialize_account(staker_id);
|
||||
vec![create_ix, init_ix]
|
||||
}
|
||||
|
||||
pub fn new_clear_credits(vote_id: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
||||
Instruction::new(id(), &VoteInstruction::ClearCredits, account_metas)
|
||||
}
|
||||
|
||||
pub fn new_delegate_stake(vote_id: &Pubkey, delegate_id: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
||||
Instruction::new(
|
||||
id(),
|
||||
&VoteInstruction::DelegateStake(*delegate_id),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_authorize_voter(vote_id: &Pubkey, authorized_voter_id: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
||||
Instruction::new(
|
||||
id(),
|
||||
&VoteInstruction::AuthorizeVoter(*authorized_voter_id),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
||||
Instruction::new(id(), &VoteInstruction::Vote(vote), account_metas)
|
||||
}
|
||||
fn initialize_account(vote_id: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, false)];
|
||||
Instruction::new(id(), &VoteInstruction::InitializeAccount, account_metas)
|
||||
}
|
||||
|
||||
pub fn create_account(from_id: &Pubkey, staker_id: &Pubkey, lamports: u64) -> Vec<Instruction> {
|
||||
let space = VoteState::max_size() as u64;
|
||||
let create_ix = system_instruction::create_account(&from_id, staker_id, lamports, space, &id());
|
||||
let init_ix = initialize_account(staker_id);
|
||||
vec![create_ix, init_ix]
|
||||
}
|
||||
|
||||
pub fn clear_credits(vote_id: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
||||
Instruction::new(id(), &VoteInstruction::ClearCredits, account_metas)
|
||||
}
|
||||
|
||||
pub fn delegate_stake(vote_id: &Pubkey, delegate_id: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
||||
Instruction::new(
|
||||
id(),
|
||||
&VoteInstruction::DelegateStake(*delegate_id),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn authorize_voter(vote_id: &Pubkey, authorized_voter_id: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
||||
Instruction::new(
|
||||
id(),
|
||||
&VoteInstruction::AuthorizeVoter(*authorized_voter_id),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn vote(vote_id: &Pubkey, vote: Vote) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
||||
Instruction::new(id(), &VoteInstruction::Vote(vote), account_metas)
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ pub fn process_instruction(
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::id;
|
||||
use crate::vote_instruction::{Vote, VoteInstruction};
|
||||
use crate::vote_instruction::{self, Vote};
|
||||
use crate::vote_state::VoteState;
|
||||
use solana_runtime::bank::{Bank, Result};
|
||||
use solana_runtime::bank_client::BankClient;
|
||||
@ -54,7 +54,7 @@ mod tests {
|
||||
use solana_sdk::message::Message;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
use solana_sdk::system_instruction;
|
||||
use solana_sdk::transaction::TransactionError;
|
||||
|
||||
fn create_bank(lamports: u64) -> (Bank, Keypair) {
|
||||
@ -70,7 +70,7 @@ mod tests {
|
||||
vote_id: &Pubkey,
|
||||
lamports: u64,
|
||||
) -> Result<()> {
|
||||
let ixs = VoteInstruction::new_account(&from_keypair.pubkey(), vote_id, lamports);
|
||||
let ixs = vote_instruction::create_account(&from_keypair.pubkey(), vote_id, lamports);
|
||||
let message = Message::new(ixs);
|
||||
bank_client.process_message(&[from_keypair], message)
|
||||
}
|
||||
@ -83,8 +83,8 @@ mod tests {
|
||||
lamports: u64,
|
||||
) -> Result<()> {
|
||||
let vote_id = vote_keypair.pubkey();
|
||||
let mut ixs = VoteInstruction::new_account(&from_keypair.pubkey(), &vote_id, lamports);
|
||||
let delegate_ix = VoteInstruction::new_delegate_stake(&vote_id, delegate_id);
|
||||
let mut ixs = vote_instruction::create_account(&from_keypair.pubkey(), &vote_id, lamports);
|
||||
let delegate_ix = vote_instruction::delegate_stake(&vote_id, delegate_id);
|
||||
ixs.push(delegate_ix);
|
||||
let message = Message::new(ixs);
|
||||
bank_client.process_message(&[&from_keypair, vote_keypair], message)
|
||||
@ -95,7 +95,7 @@ mod tests {
|
||||
vote_keypair: &Keypair,
|
||||
tick_height: u64,
|
||||
) -> Result<()> {
|
||||
let vote_ix = VoteInstruction::new_vote(&vote_keypair.pubkey(), Vote::new(tick_height));
|
||||
let vote_ix = vote_instruction::vote(&vote_keypair.pubkey(), Vote::new(tick_height));
|
||||
bank_client.process_instruction(&vote_keypair, vote_ix)
|
||||
}
|
||||
|
||||
@ -142,13 +142,13 @@ mod tests {
|
||||
create_vote_account(&bank_client, &mallory_keypair, &vote_id, 100).unwrap();
|
||||
|
||||
let mallory_id = mallory_keypair.pubkey();
|
||||
let mut vote_ix = VoteInstruction::new_vote(&vote_id, Vote::new(0));
|
||||
let mut vote_ix = vote_instruction::vote(&vote_id, Vote::new(0));
|
||||
vote_ix.accounts[0].is_signer = false; // <--- attack!! No signer required.
|
||||
|
||||
// Sneak in an instruction so that the transaction is signed but
|
||||
// the 0th account in the second instruction is not! The program
|
||||
// needs to check that it's signed.
|
||||
let move_ix = SystemInstruction::new_transfer(&mallory_id, &vote_id, 1);
|
||||
let move_ix = system_instruction::transfer(&mallory_id, &vote_id, 1);
|
||||
let message = Message::new(vec![move_ix, vote_ix]);
|
||||
let result = bank_client.process_message(&[&mallory_keypair], message);
|
||||
|
||||
|
Reference in New Issue
Block a user