Move vote_program out of src/

This commit is contained in:
Michael Vines
2018-12-04 07:45:32 -08:00
parent a594f56c02
commit ea6e042a6f
20 changed files with 241 additions and 193 deletions

View File

@ -0,0 +1,78 @@
//! Vote program
//! Receive and processes votes from validators
extern crate bincode;
#[macro_use]
extern crate log;
#[macro_use]
extern crate solana_sdk;
use bincode::deserialize;
use solana_sdk::account::KeyedAccount;
use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::vote_program::*;
use std::collections::VecDeque;
solana_entrypoint!(entrypoint);
fn entrypoint(
_program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
_tick_height: u64,
) -> Result<(), ProgramError> {
trace!("process_instruction: {:?}", data);
trace!("keyed_accounts: {:?}", keyed_accounts);
// all vote instructions require that accounts_keys[0] be a signer
if keyed_accounts[0].signer_key().is_none() {
error!("account[0] is unsigned");
Err(ProgramError::InvalidArgument)?;
}
match deserialize(data) {
Ok(VoteInstruction::RegisterAccount) => {
if !check_id(&keyed_accounts[1].account.owner) {
error!("account[1] is not assigned to the VOTE_PROGRAM");
Err(ProgramError::InvalidArgument)?;
}
// TODO: a single validator could register multiple "vote accounts"
// which would clutter the "accounts" structure. See github issue 1654.
let mut vote_state = VoteProgram {
votes: VecDeque::new(),
node_id: *keyed_accounts[0].signer_key().unwrap(),
};
vote_state.serialize(&mut keyed_accounts[1].account.userdata)?;
Ok(())
}
Ok(VoteInstruction::NewVote(vote)) => {
if !check_id(&keyed_accounts[0].account.owner) {
error!("account[0] is not assigned to the VOTE_PROGRAM");
Err(ProgramError::InvalidArgument)?;
}
let mut vote_state = VoteProgram::deserialize(&keyed_accounts[0].account.userdata)?;
// TODO: Integrity checks
// a) Verify the vote's bank hash matches what is expected
// b) Verify vote is older than previous votes
// Only keep around the most recent MAX_VOTE_HISTORY votes
if vote_state.votes.len() == MAX_VOTE_HISTORY {
vote_state.votes.pop_front();
}
vote_state.votes.push_back(vote);
vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
Ok(())
}
Err(_) => {
info!("Invalid transaction instruction userdata: {:?}", data);
Err(ProgramError::InvalidUserdata)
}
}
}