Stop using VoteTransaction in Vote processor

This commit is contained in:
Greg Fitzgerald 2019-03-22 13:31:58 -06:00
parent a28f7db950
commit 5f41909098
5 changed files with 76 additions and 85 deletions

View File

@ -1,5 +1,6 @@
pub mod vote_instruction; pub mod vote_instruction;
pub mod vote_processor; pub mod vote_processor;
pub mod vote_script;
pub mod vote_state; pub mod vote_state;
pub mod vote_transaction; pub mod vote_transaction;

View File

@ -46,17 +46,15 @@ mod tests {
use super::*; use super::*;
use crate::id; use crate::id;
use crate::vote_instruction::{Vote, VoteInstruction}; use crate::vote_instruction::{Vote, VoteInstruction};
use crate::vote_script::VoteScript;
use crate::vote_state::VoteState; use crate::vote_state::VoteState;
use crate::vote_transaction::VoteTransaction;
use solana_runtime::bank::{Bank, Result}; use solana_runtime::bank::{Bank, Result};
use solana_runtime::bank_client::BankClient;
use solana_sdk::genesis_block::GenesisBlock; use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::hash::hash;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::transaction::{ use solana_sdk::transaction::{AccountMeta, Instruction, InstructionError, TransactionError};
AccountMeta, Instruction, InstructionError, Transaction, TransactionError,
};
fn create_bank(lamports: u64) -> (Bank, Keypair) { fn create_bank(lamports: u64) -> (Bank, Keypair) {
let (genesis_block, mint_keypair) = GenesisBlock::new(lamports); let (genesis_block, mint_keypair) = GenesisBlock::new(lamports);
@ -65,102 +63,75 @@ mod tests {
(bank, mint_keypair) (bank, mint_keypair)
} }
struct VoteBank<'a> { fn create_vote_account(
bank: &'a Bank, bank_client: &BankClient,
vote_id: &Pubkey,
lamports: u64,
) -> Result<()> {
let script = VoteScript::new_account(&bank_client.pubkey(), vote_id, lamports);
bank_client.process_script(script)
} }
impl<'a> VoteBank<'a> { fn create_vote_account_with_delegate(
fn new(bank: &'a Bank) -> Self { bank_client: &BankClient,
Self { bank } delegate_id: &Pubkey,
} lamports: u64,
) -> Result<()> {
let vote_id = bank_client.pubkeys()[1];
let mut script = VoteScript::new_account(&bank_client.pubkey(), &vote_id, lamports);
let delegate_ix = VoteInstruction::new_delegate_stake(&vote_id, delegate_id);
script.push(delegate_ix);
bank_client.process_script(script)
}
fn create_vote_account( fn submit_vote(
&self, bank_client: &BankClient,
from_keypair: &Keypair, staking_account: &Pubkey,
vote_id: &Pubkey, tick_height: u64,
lamports: u64, ) -> Result<()> {
) -> Result<()> { let vote_ix = VoteInstruction::new_vote(staking_account, Vote::new(tick_height));
let blockhash = self.bank.last_blockhash(); bank_client.process_instruction(vote_ix)
let tx = VoteTransaction::new_account(from_keypair, vote_id, blockhash, lamports, 0);
self.bank.process_transaction(&tx)
}
fn create_vote_account_with_delegate(
&self,
from_keypair: &Keypair,
vote_keypair: &Keypair,
delegate_id: &Pubkey,
lamports: u64,
) -> Result<()> {
let blockhash = self.bank.last_blockhash();
let tx = VoteTransaction::new_account_with_delegate(
from_keypair,
vote_keypair,
delegate_id,
blockhash,
lamports,
0,
);
self.bank.process_transaction(&tx)
}
fn submit_vote(
&self,
staking_account: &Pubkey,
vote_keypair: &Keypair,
tick_height: u64,
) -> Result<VoteState> {
let blockhash = self.bank.last_blockhash();
let tx =
VoteTransaction::new_vote(staking_account, vote_keypair, tick_height, blockhash, 0);
self.bank.process_transaction(&tx)?;
self.bank.register_tick(&hash(blockhash.as_ref()));
let vote_account = self.bank.get_account(&vote_keypair.pubkey()).unwrap();
Ok(VoteState::deserialize(&vote_account.data).unwrap())
}
} }
#[test] #[test]
fn test_vote_bank_basic() { fn test_vote_bank_basic() {
let (bank, from_keypair) = create_bank(10_000); let (bank, from_keypair) = create_bank(10_000);
let vote_bank = VoteBank::new(&bank); let alice_client = BankClient::new(&bank, from_keypair);
let vote_keypair = Keypair::new(); let vote_keypair = Keypair::new();
let vote_id = vote_keypair.pubkey(); let vote_client = BankClient::new(&bank, vote_keypair);
vote_bank let vote_id = vote_client.pubkey();
.create_vote_account(&from_keypair, &vote_id, 100)
.unwrap();
let vote_state = vote_bank.submit_vote(&vote_id, &vote_keypair, 0).unwrap(); create_vote_account(&alice_client, &vote_id, 100).unwrap();
submit_vote(&vote_client, &vote_id, 0).unwrap();
let vote_account = bank.get_account(&vote_client.pubkey()).unwrap();
let vote_state = VoteState::deserialize(&vote_account.data).unwrap();
assert_eq!(vote_state.votes.len(), 1); assert_eq!(vote_state.votes.len(), 1);
} }
#[test] #[test]
fn test_vote_bank_delegate() { fn test_vote_bank_delegate() {
let (bank, from_keypair) = create_bank(10_000); let (bank, from_keypair) = create_bank(10_000);
let vote_bank = VoteBank::new(&bank);
let vote_keypair = Keypair::new(); let vote_keypair = Keypair::new();
let delegate_keypair = Keypair::new(); let alice_and_vote_client =
let delegate_id = delegate_keypair.pubkey(); BankClient::new_with_keypairs(&bank, vec![from_keypair, vote_keypair]);
vote_bank let delegate_id = Keypair::new().pubkey();
.create_vote_account_with_delegate(&from_keypair, &vote_keypair, &delegate_id, 100) create_vote_account_with_delegate(&alice_and_vote_client, &delegate_id, 100).unwrap();
.unwrap();
} }
#[test] #[test]
fn test_vote_via_bank_with_no_signature() { fn test_vote_via_bank_with_no_signature() {
let (bank, mallory_keypair) = create_bank(10_000); let (bank, from_keypair) = create_bank(10_000);
let vote_bank = VoteBank::new(&bank); let mallory_client = BankClient::new(&bank, from_keypair);
let vote_keypair = Keypair::new(); let vote_keypair = Keypair::new();
let vote_id = vote_keypair.pubkey(); let vote_client = BankClient::new(&bank, vote_keypair);
vote_bank let vote_id = vote_client.pubkey();
.create_vote_account(&mallory_keypair, &vote_id, 100)
.unwrap();
let mallory_id = mallory_keypair.pubkey(); create_vote_account(&mallory_client, &vote_id, 100).unwrap();
let blockhash = bank.last_blockhash();
let mallory_id = mallory_client.pubkey();
let vote_ix = Instruction::new( let vote_ix = Instruction::new(
id(), id(),
&VoteInstruction::Vote(Vote::new(0)), &VoteInstruction::Vote(Vote::new(0)),
@ -171,10 +142,7 @@ mod tests {
// the 0th account in the second instruction is not! The program // the 0th account in the second instruction is not! The program
// needs to check that it's signed. // needs to check that it's signed.
let move_ix = SystemInstruction::new_move(&mallory_id, &vote_id, 1); let move_ix = SystemInstruction::new_move(&mallory_id, &vote_id, 1);
let mut tx = Transaction::new(vec![move_ix, vote_ix]); let result = mallory_client.process_instructions(vec![move_ix, vote_ix]);
tx.sign(&[&mallory_keypair], blockhash);
let result = bank.process_transaction(&tx);
// And ensure there's no vote. // And ensure there's no vote.
let vote_account = bank.get_account(&vote_id).unwrap(); let vote_account = bank.get_account(&vote_id).unwrap();

View File

@ -0,0 +1,21 @@
//! The `vote_script` module provides functionality for creating vote scripts.
use crate::id;
use crate::vote_instruction::VoteInstruction;
use crate::vote_state::VoteState;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::script::Script;
use solana_sdk::system_instruction::SystemInstruction;
pub struct VoteScript;
impl VoteScript {
/// Fund or create the staking account with lamports
pub fn new_account(from_id: &Pubkey, staker_id: &Pubkey, lamports: u64) -> Script {
let space = VoteState::max_size() as u64;
let create_ix =
SystemInstruction::new_program_account(&from_id, staker_id, lamports, space, &id());
let init_ix = VoteInstruction::new_initialize_account(staker_id);
Script::new(vec![create_ix, init_ix])
}
}

View File

@ -1,6 +1,7 @@
//! The `vote_transaction` module provides functionality for creating vote transactions. //! The `vote_transaction` module provides functionality for creating vote transactions.
use crate::vote_instruction::{Vote, VoteInstruction}; use crate::vote_instruction::{Vote, VoteInstruction};
use crate::vote_script::VoteScript;
use crate::vote_state::VoteState; use crate::vote_state::VoteState;
use crate::{check_id, id}; use crate::{check_id, id};
use bincode::deserialize; use bincode::deserialize;
@ -37,11 +38,7 @@ impl VoteTransaction {
fee: u64, fee: u64,
) -> Transaction { ) -> Transaction {
let from_id = from_keypair.pubkey(); let from_id = from_keypair.pubkey();
let space = VoteState::max_size() as u64; let mut tx = VoteScript::new_account(&from_id, staker_id, lamports).compile();
let create_ix =
SystemInstruction::new_program_account(&from_id, staker_id, lamports, space, &id());
let init_ix = VoteInstruction::new_initialize_account(staker_id);
let mut tx = Transaction::new(vec![create_ix, init_ix]);
tx.fee = fee; tx.fee = fee;
tx.sign(&[from_keypair], recent_blockhash); tx.sign(&[from_keypair], recent_blockhash);
tx tx

View File

@ -48,6 +48,10 @@ impl Script {
Self { instructions } Self { instructions }
} }
pub fn push(&mut self, instruction: Instruction) {
self.instructions.push(instruction);
}
/// Return pubkeys referenced by all instructions, with the ones needing signatures first. /// Return pubkeys referenced by all instructions, with the ones needing signatures first.
/// No duplicates and order is preserved. /// No duplicates and order is preserved.
fn keys(&self) -> (Vec<Pubkey>, Vec<Pubkey>) { fn keys(&self) -> (Vec<Pubkey>, Vec<Pubkey>) {