2019-04-10 17:52:47 -07:00
|
|
|
//! Vote program
|
|
|
|
//! Receive and processes votes from validators
|
|
|
|
|
2019-09-09 18:17:32 -07:00
|
|
|
use crate::{
|
|
|
|
id,
|
2019-09-25 13:53:49 -07:00
|
|
|
vote_state::{self, Vote, VoteAuthorize, VoteInit, VoteState},
|
2019-09-09 18:17:32 -07:00
|
|
|
};
|
2019-04-10 17:52:47 -07:00
|
|
|
use log::*;
|
2019-09-09 18:17:32 -07:00
|
|
|
use num_derive::{FromPrimitive, ToPrimitive};
|
2019-03-02 13:51:26 -08:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-10-04 16:25:22 -07:00
|
|
|
use solana_metrics::datapoint_debug;
|
2019-09-09 18:17:32 -07:00
|
|
|
use solana_sdk::{
|
2019-11-15 14:16:24 -08:00
|
|
|
account::{get_signers, KeyedAccount},
|
2019-11-18 12:43:47 -08:00
|
|
|
instruction::{AccountMeta, Instruction, InstructionError, WithSigner},
|
2019-11-15 14:16:24 -08:00
|
|
|
instruction_processor_utils::{limited_deserialize, next_keyed_account, DecodeError},
|
2019-09-09 18:17:32 -07:00
|
|
|
pubkey::Pubkey,
|
2019-10-03 14:22:48 -07:00
|
|
|
system_instruction,
|
2019-11-04 12:31:24 -08:00
|
|
|
sysvar::{self, clock::Clock, slot_hashes::SlotHashes, Sysvar},
|
2019-09-09 18:17:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Reasons the stake might have had an error
|
2019-11-29 12:50:32 -08:00
|
|
|
#[derive(Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
|
2019-09-09 18:17:32 -07:00
|
|
|
pub enum VoteError {
|
|
|
|
VoteTooOld,
|
|
|
|
SlotsMismatch,
|
|
|
|
SlotHashMismatch,
|
|
|
|
EmptySlots,
|
|
|
|
}
|
|
|
|
impl<E> DecodeError<E> for VoteError {
|
|
|
|
fn type_of() -> &'static str {
|
|
|
|
"VoteError"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for VoteError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
match self {
|
|
|
|
VoteError::VoteTooOld => "vote already recorded or not in slot hashes history",
|
|
|
|
VoteError::SlotsMismatch => "vote slots do not match bank history",
|
|
|
|
VoteError::SlotHashMismatch => "vote hash does not match bank hash",
|
|
|
|
VoteError::EmptySlots => "vote has no slots, invalid",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::error::Error for VoteError {}
|
2019-03-02 13:51:26 -08:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum VoteInstruction {
|
|
|
|
/// Initialize the VoteState for this `vote account`
|
2019-09-25 13:53:49 -07:00
|
|
|
InitializeAccount(VoteInit),
|
2019-03-25 19:57:25 -07:00
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
/// Authorize a voter to send signed votes or a withdrawer
|
|
|
|
/// to withdraw
|
|
|
|
Authorize(Pubkey, VoteAuthorize),
|
2019-03-25 19:57:25 -07:00
|
|
|
|
2019-04-11 14:48:36 -07:00
|
|
|
/// A Vote instruction with recent votes
|
2019-09-02 12:01:09 -07:00
|
|
|
Vote(Vote),
|
2019-07-25 23:20:47 -07:00
|
|
|
|
|
|
|
/// Withdraw some amount of funds
|
|
|
|
Withdraw(u64),
|
2019-03-02 13:51:26 -08:00
|
|
|
}
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
fn initialize_account(vote_pubkey: &Pubkey, vote_init: &VoteInit) -> Instruction {
|
2019-10-03 14:22:48 -07:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
|
|
|
AccountMeta::new(sysvar::rent::id(), false),
|
|
|
|
];
|
2019-04-03 08:45:57 -07:00
|
|
|
Instruction::new(
|
|
|
|
id(),
|
2019-09-25 13:53:49 -07:00
|
|
|
&VoteInstruction::InitializeAccount(*vote_init),
|
2019-04-03 08:45:57 -07:00
|
|
|
account_metas,
|
|
|
|
)
|
|
|
|
}
|
2019-03-25 19:57:25 -07:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
pub fn create_account(
|
2019-05-23 23:20:04 -07:00
|
|
|
from_pubkey: &Pubkey,
|
|
|
|
vote_pubkey: &Pubkey,
|
2019-09-25 13:53:49 -07:00
|
|
|
vote_init: &VoteInit,
|
2019-04-10 17:52:47 -07:00
|
|
|
lamports: u64,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
let space = VoteState::size_of() as u64;
|
2019-09-20 14:10:39 -07:00
|
|
|
let create_ix =
|
|
|
|
system_instruction::create_account(from_pubkey, vote_pubkey, lamports, space, &id());
|
2019-09-25 13:53:49 -07:00
|
|
|
let init_ix = initialize_account(vote_pubkey, vote_init);
|
2019-04-10 17:52:47 -07:00
|
|
|
vec![create_ix, init_ix]
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
pub fn authorize(
|
2019-05-23 23:20:04 -07:00
|
|
|
vote_pubkey: &Pubkey,
|
2019-09-25 13:53:49 -07:00
|
|
|
authorized_pubkey: &Pubkey, // currently authorized
|
|
|
|
new_authorized_pubkey: &Pubkey,
|
|
|
|
vote_authorize: VoteAuthorize,
|
2019-05-20 13:32:32 -07:00
|
|
|
) -> Instruction {
|
2019-11-18 12:43:47 -08:00
|
|
|
let account_metas = vec![AccountMeta::new(*vote_pubkey, false)].with_signer(authorized_pubkey);
|
2019-05-20 13:32:32 -07:00
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
Instruction::new(
|
|
|
|
id(),
|
2019-09-25 13:53:49 -07:00
|
|
|
&VoteInstruction::Authorize(*new_authorized_pubkey, vote_authorize),
|
2019-04-03 08:45:57 -07:00
|
|
|
account_metas,
|
|
|
|
)
|
|
|
|
}
|
2019-03-25 19:57:25 -07:00
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
pub fn vote(vote_pubkey: &Pubkey, authorized_voter_pubkey: &Pubkey, vote: Vote) -> Instruction {
|
2019-11-18 12:43:47 -08:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(sysvar::slot_hashes::id(), false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
|
|
|
]
|
|
|
|
.with_signer(authorized_voter_pubkey);
|
2019-05-20 13:32:32 -07:00
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
Instruction::new(id(), &VoteInstruction::Vote(vote), account_metas)
|
2019-03-02 13:51:26 -08:00
|
|
|
}
|
2019-04-10 17:52:47 -07:00
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
pub fn withdraw(
|
|
|
|
vote_pubkey: &Pubkey,
|
|
|
|
withdrawer_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
to_pubkey: &Pubkey,
|
|
|
|
) -> Instruction {
|
2019-11-18 12:43:47 -08:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
|
|
|
AccountMeta::new(*to_pubkey, false),
|
|
|
|
]
|
|
|
|
.with_signer(withdrawer_pubkey);
|
2019-07-25 23:20:47 -07:00
|
|
|
|
|
|
|
Instruction::new(id(), &VoteInstruction::Withdraw(lamports), account_metas)
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
pub fn process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<(), InstructionError> {
|
2019-09-23 18:09:20 -07:00
|
|
|
solana_logger::setup_with_filter("solana=info");
|
2019-04-10 17:52:47 -07:00
|
|
|
|
|
|
|
trace!("process_instruction: {:?}", data);
|
|
|
|
trace!("keyed_accounts: {:?}", keyed_accounts);
|
|
|
|
|
2019-11-15 14:16:24 -08:00
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-04-30 15:11:08 -07:00
|
|
|
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut keyed_accounts.iter_mut();
|
|
|
|
let me = &mut next_keyed_account(keyed_accounts)?;
|
2019-05-20 13:32:32 -07:00
|
|
|
|
2019-10-23 19:56:07 -07:00
|
|
|
match limited_deserialize(data)? {
|
2019-09-25 13:53:49 -07:00
|
|
|
VoteInstruction::InitializeAccount(vote_init) => {
|
2019-11-15 14:16:24 -08:00
|
|
|
sysvar::rent::verify_rent_exemption(me, next_keyed_account(keyed_accounts)?)?;
|
2019-09-25 13:53:49 -07:00
|
|
|
vote_state::initialize_account(me, &vote_init)
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
2019-09-25 13:53:49 -07:00
|
|
|
VoteInstruction::Authorize(voter_pubkey, vote_authorize) => {
|
2019-11-15 14:16:24 -08:00
|
|
|
vote_state::authorize(me, &voter_pubkey, vote_authorize, &signers)
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
2019-09-02 12:01:09 -07:00
|
|
|
VoteInstruction::Vote(vote) => {
|
2019-10-04 16:25:22 -07:00
|
|
|
datapoint_debug!("vote-native", ("count", 1, i64));
|
2019-09-02 12:01:09 -07:00
|
|
|
vote_state::process_vote(
|
2019-06-21 20:43:24 -07:00
|
|
|
me,
|
2019-11-15 14:16:24 -08:00
|
|
|
&SlotHashes::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
|
|
|
|
&Clock::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
|
2019-09-02 12:01:09 -07:00
|
|
|
&vote,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-06-21 20:43:24 -07:00
|
|
|
)
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
2019-07-25 23:20:47 -07:00
|
|
|
VoteInstruction::Withdraw(lamports) => {
|
2019-11-15 14:16:24 -08:00
|
|
|
let to = next_keyed_account(keyed_accounts)?;
|
|
|
|
vote_state::withdraw(me, lamports, to, &signers)
|
2019-07-25 23:20:47 -07:00
|
|
|
}
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-04-30 15:11:08 -07:00
|
|
|
use solana_sdk::account::Account;
|
2019-10-30 16:25:12 -07:00
|
|
|
use solana_sdk::rent::Rent;
|
2019-04-10 17:52:47 -07:00
|
|
|
|
2019-05-20 13:32:32 -07:00
|
|
|
// these are for 100% coverage in this file
|
2019-04-10 17:52:47 -07:00
|
|
|
#[test]
|
2019-04-30 15:11:08 -07:00
|
|
|
fn test_vote_process_instruction_decode_bail() {
|
|
|
|
assert_eq!(
|
2019-05-31 15:29:21 -07:00
|
|
|
super::process_instruction(&Pubkey::default(), &mut [], &[],),
|
2019-11-15 14:16:24 -08:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-04-30 15:11:08 -07:00
|
|
|
);
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:11:08 -07:00
|
|
|
fn process_instruction(instruction: &Instruction) -> Result<(), InstructionError> {
|
2019-06-21 20:43:24 -07:00
|
|
|
let mut accounts: Vec<_> = instruction
|
|
|
|
.accounts
|
|
|
|
.iter()
|
|
|
|
.map(|meta| {
|
2019-07-12 16:38:15 -07:00
|
|
|
if sysvar::clock::check_id(&meta.pubkey) {
|
2019-11-04 12:31:24 -08:00
|
|
|
sysvar::clock::create_account(1, 0, 0, 0, 0)
|
2019-07-12 16:38:15 -07:00
|
|
|
} else if sysvar::slot_hashes::check_id(&meta.pubkey) {
|
|
|
|
sysvar::slot_hashes::create_account(1, &[])
|
2019-10-03 14:22:48 -07:00
|
|
|
} else if sysvar::rent::check_id(&meta.pubkey) {
|
2019-11-04 12:31:24 -08:00
|
|
|
Rent::default().create_account(1)
|
2019-06-21 20:43:24 -07:00
|
|
|
} else {
|
|
|
|
Account::default()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2019-04-30 15:11:08 -07:00
|
|
|
for _ in 0..instruction.accounts.len() {
|
|
|
|
accounts.push(Account::default());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut keyed_accounts: Vec<_> = instruction
|
|
|
|
.accounts
|
|
|
|
.iter()
|
|
|
|
.zip(accounts.iter_mut())
|
|
|
|
.map(|(meta, account)| KeyedAccount::new(&meta.pubkey, meta.is_signer, account))
|
|
|
|
.collect();
|
2019-05-31 15:29:21 -07:00
|
|
|
super::process_instruction(&Pubkey::default(), &mut keyed_accounts, &instruction.data)
|
2019-04-30 15:11:08 -07:00
|
|
|
}
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-30 15:11:08 -07:00
|
|
|
fn test_vote_process_instruction() {
|
|
|
|
let instructions = create_account(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
2019-09-25 13:53:49 -07:00
|
|
|
&VoteInit::default(),
|
2019-04-30 15:11:08 -07:00
|
|
|
100,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&instructions[1]),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-05-20 13:32:32 -07:00
|
|
|
process_instruction(&vote(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
2019-09-02 12:01:09 -07:00
|
|
|
Vote::default(),
|
2019-05-20 13:32:32 -07:00
|
|
|
)),
|
2019-04-30 15:11:08 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2019-04-10 17:52:47 -07:00
|
|
|
assert_eq!(
|
2019-09-25 13:53:49 -07:00
|
|
|
process_instruction(&authorize(
|
2019-05-20 13:32:32 -07:00
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
2019-09-25 13:53:49 -07:00
|
|
|
VoteAuthorize::Voter,
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&withdraw(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
0,
|
|
|
|
&Pubkey::default()
|
2019-05-20 13:32:32 -07:00
|
|
|
)),
|
2019-04-30 15:11:08 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
2019-04-10 17:52:47 -07:00
|
|
|
);
|
|
|
|
}
|
2019-04-30 15:11:08 -07:00
|
|
|
|
2019-07-20 16:28:17 -07:00
|
|
|
#[test]
|
|
|
|
fn test_minimum_balance() {
|
2019-10-30 16:25:12 -07:00
|
|
|
let rent = solana_sdk::rent::Rent::default();
|
|
|
|
let minimum_balance = rent.minimum_balance(VoteState::size_of());
|
2019-07-20 16:28:17 -07:00
|
|
|
// vote state cheaper than "my $0.02" ;)
|
2019-10-30 14:59:44 -07:00
|
|
|
assert!(minimum_balance as f64 / 10f64.powf(9.0) < 0.02)
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
#[test]
|
|
|
|
fn test_custom_error_decode() {
|
|
|
|
use num_traits::FromPrimitive;
|
|
|
|
fn pretty_err<T>(err: InstructionError) -> String
|
|
|
|
where
|
|
|
|
T: 'static + std::error::Error + DecodeError<T> + FromPrimitive,
|
|
|
|
{
|
|
|
|
if let InstructionError::CustomError(code) = err {
|
|
|
|
let specific_error: T = T::decode_custom_error_to_enum(code).unwrap();
|
|
|
|
format!(
|
|
|
|
"{:?}: {}::{:?} - {}",
|
|
|
|
err,
|
|
|
|
T::type_of(),
|
|
|
|
specific_error,
|
|
|
|
specific_error,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
"CustomError(0): VoteError::VoteTooOld - vote already recorded or not in slot hashes history",
|
|
|
|
pretty_err::<VoteError>(VoteError::VoteTooOld.into())
|
|
|
|
)
|
|
|
|
}
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|