Stop using VoteTransaction in Vote processor
This commit is contained in:
parent
a28f7db950
commit
5f41909098
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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> {
|
|
||||||
bank: &'a Bank,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> VoteBank<'a> {
|
|
||||||
fn new(bank: &'a Bank) -> Self {
|
|
||||||
Self { bank }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_vote_account(
|
fn create_vote_account(
|
||||||
&self,
|
bank_client: &BankClient,
|
||||||
from_keypair: &Keypair,
|
|
||||||
vote_id: &Pubkey,
|
vote_id: &Pubkey,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let blockhash = self.bank.last_blockhash();
|
let script = VoteScript::new_account(&bank_client.pubkey(), vote_id, lamports);
|
||||||
let tx = VoteTransaction::new_account(from_keypair, vote_id, blockhash, lamports, 0);
|
bank_client.process_script(script)
|
||||||
self.bank.process_transaction(&tx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_vote_account_with_delegate(
|
fn create_vote_account_with_delegate(
|
||||||
&self,
|
bank_client: &BankClient,
|
||||||
from_keypair: &Keypair,
|
|
||||||
vote_keypair: &Keypair,
|
|
||||||
delegate_id: &Pubkey,
|
delegate_id: &Pubkey,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let blockhash = self.bank.last_blockhash();
|
let vote_id = bank_client.pubkeys()[1];
|
||||||
let tx = VoteTransaction::new_account_with_delegate(
|
let mut script = VoteScript::new_account(&bank_client.pubkey(), &vote_id, lamports);
|
||||||
from_keypair,
|
let delegate_ix = VoteInstruction::new_delegate_stake(&vote_id, delegate_id);
|
||||||
vote_keypair,
|
script.push(delegate_ix);
|
||||||
delegate_id,
|
bank_client.process_script(script)
|
||||||
blockhash,
|
|
||||||
lamports,
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
self.bank.process_transaction(&tx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn submit_vote(
|
fn submit_vote(
|
||||||
&self,
|
bank_client: &BankClient,
|
||||||
staking_account: &Pubkey,
|
staking_account: &Pubkey,
|
||||||
vote_keypair: &Keypair,
|
|
||||||
tick_height: u64,
|
tick_height: u64,
|
||||||
) -> Result<VoteState> {
|
) -> Result<()> {
|
||||||
let blockhash = self.bank.last_blockhash();
|
let vote_ix = VoteInstruction::new_vote(staking_account, Vote::new(tick_height));
|
||||||
let tx =
|
bank_client.process_instruction(vote_ix)
|
||||||
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();
|
||||||
|
|
|
@ -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])
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
|
@ -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>) {
|
||||||
|
|
Loading…
Reference in New Issue