Reduce remaining program crates to boilerplate crates

This commit is contained in:
Greg Fitzgerald
2019-03-22 06:47:05 -06:00
committed by Grimes
parent 0a5b6154e8
commit 38fdbbba3f
20 changed files with 765 additions and 758 deletions

View File

@ -13,8 +13,13 @@ bincode = "1.1.2"
log = "0.4.2"
serde = "1.0.89"
serde_derive = "1.0.89"
solana-logger = { path = "../../logger", version = "0.13.0" }
solana-metrics = { path = "../../metrics", version = "0.13.0" }
solana-sdk = { path = "../../sdk", version = "0.13.0" }
[dev-dependencies]
solana-runtime = { path = "../../runtime", version = "0.13.0" }
[lib]
name = "solana_vote_api"
crate-type = ["lib"]

View File

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

View File

@ -0,0 +1,192 @@
//! Vote program
//! Receive and processes votes from validators
use crate::vote_instruction::VoteInstruction;
use crate::vote_state;
use bincode::deserialize;
use log::*;
use solana_sdk::account::KeyedAccount;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction::InstructionError;
pub fn process_instruction(
_program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
_tick_height: u64,
) -> Result<(), InstructionError> {
solana_logger::setup();
trace!("process_instruction: {:?}", data);
trace!("keyed_accounts: {:?}", keyed_accounts);
match deserialize(data).map_err(|_| InstructionError::InvalidInstructionData)? {
VoteInstruction::InitializeAccount => vote_state::initialize_account(keyed_accounts),
VoteInstruction::DelegateStake(delegate_id) => {
vote_state::delegate_stake(keyed_accounts, &delegate_id)
}
VoteInstruction::AuthorizeVoter(voter_id) => {
vote_state::authorize_voter(keyed_accounts, &voter_id)
}
VoteInstruction::Vote(vote) => {
debug!("{:?} by {}", vote, keyed_accounts[0].signer_key().unwrap());
solana_metrics::submit(
solana_metrics::influxdb::Point::new("vote-native")
.add_field("count", solana_metrics::influxdb::Value::Integer(1))
.to_owned(),
);
vote_state::process_vote(keyed_accounts, vote)
}
VoteInstruction::ClearCredits => vote_state::clear_credits(keyed_accounts),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::id;
use crate::vote_instruction::{Vote, VoteInstruction};
use crate::vote_state::VoteState;
use crate::vote_transaction::VoteTransaction;
use solana_runtime::bank::{Bank, Result};
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::hash::hash;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::transaction::{
AccountMeta, Instruction, InstructionError, Transaction, TransactionError,
};
fn create_bank(lamports: u64) -> (Bank, Keypair) {
let (genesis_block, mint_keypair) = GenesisBlock::new(lamports);
let mut bank = Bank::new(&genesis_block);
bank.add_instruction_processor(id(), process_instruction);
(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(
&self,
from_keypair: &Keypair,
vote_id: &Pubkey,
lamports: u64,
) -> Result<()> {
let blockhash = self.bank.last_blockhash();
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]
fn test_vote_bank_basic() {
let (bank, from_keypair) = create_bank(10_000);
let vote_bank = VoteBank::new(&bank);
let vote_keypair = Keypair::new();
let vote_id = vote_keypair.pubkey();
vote_bank
.create_vote_account(&from_keypair, &vote_id, 100)
.unwrap();
let vote_state = vote_bank.submit_vote(&vote_id, &vote_keypair, 0).unwrap();
assert_eq!(vote_state.votes.len(), 1);
}
#[test]
fn test_vote_bank_delegate() {
let (bank, from_keypair) = create_bank(10_000);
let vote_bank = VoteBank::new(&bank);
let vote_keypair = Keypair::new();
let delegate_keypair = Keypair::new();
let delegate_id = delegate_keypair.pubkey();
vote_bank
.create_vote_account_with_delegate(&from_keypair, &vote_keypair, &delegate_id, 100)
.unwrap();
}
#[test]
fn test_vote_via_bank_with_no_signature() {
let (bank, mallory_keypair) = create_bank(10_000);
let vote_bank = VoteBank::new(&bank);
let vote_keypair = Keypair::new();
let vote_id = vote_keypair.pubkey();
vote_bank
.create_vote_account(&mallory_keypair, &vote_id, 100)
.unwrap();
let mallory_id = mallory_keypair.pubkey();
let blockhash = bank.last_blockhash();
let vote_ix = Instruction::new(
id(),
&VoteInstruction::Vote(Vote::new(0)),
vec![AccountMeta::new(vote_id, 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_move(&mallory_id, &vote_id, 1);
let mut tx = Transaction::new(vec![move_ix, vote_ix]);
tx.sign(&[&mallory_keypair], blockhash);
let result = bank.process_transaction(&tx);
// And ensure there's no vote.
let vote_account = bank.get_account(&vote_id).unwrap();
let vote_state = VoteState::deserialize(&vote_account.data).unwrap();
assert_eq!(vote_state.votes.len(), 0);
assert_eq!(
result,
Err(TransactionError::InstructionError(
1,
InstructionError::InvalidArgument
))
);
}
}