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 14:51:26 -07:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2020-02-21 13:41:49 -08:00
|
|
|
use solana_metrics::inc_new_counter_info;
|
2019-09-09 18:17:32 -07:00
|
|
|
use solana_sdk::{
|
2020-06-17 10:39:14 -07:00
|
|
|
decode_error::DecodeError,
|
2021-02-24 10:00:48 -07:00
|
|
|
feature_set,
|
2020-05-24 15:38:35 -07:00
|
|
|
hash::Hash,
|
2020-05-30 00:17:44 -06:00
|
|
|
instruction::{AccountMeta, Instruction, InstructionError},
|
2020-10-26 09:14:57 -07:00
|
|
|
keyed_account::{from_keyed_account, get_signers, next_keyed_account, KeyedAccount},
|
2020-10-28 20:21:50 -07:00
|
|
|
process_instruction::InvokeContext,
|
2020-06-17 10:39:14 -07:00
|
|
|
program_utils::limited_deserialize,
|
2019-09-09 18:17:32 -07:00
|
|
|
pubkey::Pubkey,
|
2019-10-04 02:52:48 +05:30
|
|
|
system_instruction,
|
2020-10-26 09:14:57 -07:00
|
|
|
sysvar::{self, clock::Clock, slot_hashes::SlotHashes},
|
2019-09-09 18:17:32 -07:00
|
|
|
};
|
2020-02-25 17:12:01 -08:00
|
|
|
use std::collections::HashSet;
|
2019-12-02 15:42:05 -07:00
|
|
|
use thiserror::Error;
|
2019-09-09 18:17:32 -07:00
|
|
|
|
|
|
|
/// Reasons the stake might have had an error
|
2019-12-02 15:42:05 -07:00
|
|
|
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
|
2019-09-09 18:17:32 -07:00
|
|
|
pub enum VoteError {
|
2019-12-02 15:42:05 -07:00
|
|
|
#[error("vote already recorded or not in slot hashes history")]
|
2019-09-09 18:17:32 -07:00
|
|
|
VoteTooOld,
|
2019-12-02 15:42:05 -07:00
|
|
|
|
|
|
|
#[error("vote slots do not match bank history")]
|
2019-09-09 18:17:32 -07:00
|
|
|
SlotsMismatch,
|
2019-12-02 15:42:05 -07:00
|
|
|
|
|
|
|
#[error("vote hash does not match bank hash")]
|
2019-09-09 18:17:32 -07:00
|
|
|
SlotHashMismatch,
|
2019-12-02 15:42:05 -07:00
|
|
|
|
|
|
|
#[error("vote has no slots, invalid")]
|
2019-09-09 18:17:32 -07:00
|
|
|
EmptySlots,
|
2019-12-06 14:38:49 -07:00
|
|
|
|
|
|
|
#[error("vote timestamp not recent")]
|
|
|
|
TimestampTooOld,
|
2019-12-30 19:57:53 -08:00
|
|
|
|
|
|
|
#[error("authorized voter has already been changed this epoch")]
|
|
|
|
TooSoonToReauthorize,
|
2019-09-09 18:17:32 -07:00
|
|
|
}
|
2019-12-30 19:57:53 -08:00
|
|
|
|
2019-09-09 18:17:32 -07:00
|
|
|
impl<E> DecodeError<E> for VoteError {
|
|
|
|
fn type_of() -> &'static str {
|
|
|
|
"VoteError"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 14:51:26 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum VoteInstruction {
|
2020-05-29 20:29:24 -06:00
|
|
|
/// Initialize a vote account
|
2020-03-18 10:10:13 -07:00
|
|
|
///
|
2020-05-29 20:29:24 -06:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Uninitialized vote account
|
|
|
|
/// 1. [] Rent sysvar
|
|
|
|
/// 2. [] Clock sysvar
|
|
|
|
/// 3. [SIGNER] New validator identity (node_pubkey)
|
2019-09-25 13:53:49 -07:00
|
|
|
InitializeAccount(VoteInit),
|
2019-03-25 20:57:25 -06:00
|
|
|
|
2020-03-18 10:10:13 -07:00
|
|
|
/// Authorize a key to send votes or issue a withdrawal
|
|
|
|
///
|
2020-05-29 20:29:24 -06:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Vote account to be updated with the Pubkey for authorization
|
|
|
|
/// 1. [] Clock sysvar
|
|
|
|
/// 2. [SIGNER] Vote or withdraw authority
|
2019-09-25 13:53:49 -07:00
|
|
|
Authorize(Pubkey, VoteAuthorize),
|
2019-03-25 20:57:25 -06:00
|
|
|
|
2019-04-11 14:48:36 -07:00
|
|
|
/// A Vote instruction with recent votes
|
2020-03-18 10:10:13 -07:00
|
|
|
///
|
2020-05-29 20:29:24 -06:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Vote account to vote with
|
|
|
|
/// 1. [] Slot hashes sysvar
|
|
|
|
/// 2. [] Clock sysvar
|
|
|
|
/// 3. [SIGNER] Vote authority
|
2019-09-02 12:01:09 -07:00
|
|
|
Vote(Vote),
|
2019-07-25 23:20:47 -07:00
|
|
|
|
|
|
|
/// Withdraw some amount of funds
|
2020-03-18 10:10:13 -07:00
|
|
|
///
|
2020-05-29 20:29:24 -06:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Vote account to withdraw from
|
|
|
|
/// 1. [WRITE] Recipient account
|
|
|
|
/// 2. [SIGNER] Withdraw authority
|
2019-07-25 23:20:47 -07:00
|
|
|
Withdraw(u64),
|
2019-12-03 22:50:08 -08:00
|
|
|
|
2020-04-10 14:09:56 -07:00
|
|
|
/// Update the vote account's validator identity (node_pubkey)
|
2020-03-18 10:10:13 -07:00
|
|
|
///
|
2020-05-29 20:29:24 -06:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Vote account to be updated with the given authority public key
|
|
|
|
/// 1. [SIGNER] New validator identity (node_pubkey)
|
|
|
|
/// 2. [SIGNER] Withdraw authority
|
2020-04-10 14:09:56 -07:00
|
|
|
UpdateValidatorIdentity,
|
2020-05-24 15:38:35 -07:00
|
|
|
|
2020-06-09 21:15:54 -07:00
|
|
|
/// Update the commission for the vote account
|
|
|
|
///
|
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Vote account to be updated
|
|
|
|
/// 1. [SIGNER] Withdraw authority
|
|
|
|
UpdateCommission(u8),
|
|
|
|
|
2020-05-24 15:38:35 -07:00
|
|
|
/// A Vote instruction with recent votes
|
|
|
|
///
|
2020-05-29 20:29:24 -06:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Vote account to vote with
|
|
|
|
/// 1. [] Slot hashes sysvar
|
|
|
|
/// 2. [] Clock sysvar
|
|
|
|
/// 3. [SIGNER] Vote authority
|
2020-05-24 15:38:35 -07:00
|
|
|
VoteSwitch(Vote, Hash),
|
2019-03-02 14:51:26 -07:00
|
|
|
}
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
fn initialize_account(vote_pubkey: &Pubkey, vote_init: &VoteInit) -> Instruction {
|
2019-10-04 02:52:48 +05:30
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
2019-12-30 19:57:53 -08:00
|
|
|
AccountMeta::new_readonly(sysvar::rent::id(), false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
2020-05-30 00:17:44 -06:00
|
|
|
AccountMeta::new_readonly(vote_init.node_pubkey, true),
|
|
|
|
];
|
2020-03-19 01:58:52 -07:00
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(
|
2019-04-03 09:45:57 -06:00
|
|
|
id(),
|
2019-09-25 13:53:49 -07:00
|
|
|
&VoteInstruction::InitializeAccount(*vote_init),
|
2019-04-03 09:45:57 -06:00
|
|
|
account_metas,
|
|
|
|
)
|
|
|
|
}
|
2019-03-25 20:57:25 -06: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-12-14 04:38:24 -08:00
|
|
|
pub fn create_account_with_seed(
|
|
|
|
from_pubkey: &Pubkey,
|
|
|
|
vote_pubkey: &Pubkey,
|
2019-12-29 16:42:24 -08:00
|
|
|
base: &Pubkey,
|
2019-12-14 04:38:24 -08:00
|
|
|
seed: &str,
|
|
|
|
vote_init: &VoteInit,
|
|
|
|
lamports: u64,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
let space = VoteState::size_of() as u64;
|
|
|
|
let create_ix = system_instruction::create_account_with_seed(
|
|
|
|
from_pubkey,
|
|
|
|
vote_pubkey,
|
2019-12-29 16:42:24 -08:00
|
|
|
base,
|
2019-12-14 04:38:24 -08:00
|
|
|
seed,
|
|
|
|
lamports,
|
|
|
|
space,
|
|
|
|
&id(),
|
|
|
|
);
|
|
|
|
let init_ix = initialize_account(vote_pubkey, vote_init);
|
|
|
|
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-12-30 19:57:53 -08:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
2020-05-30 00:17:44 -06:00
|
|
|
AccountMeta::new_readonly(*authorized_pubkey, true),
|
|
|
|
];
|
2019-05-20 13:32:32 -07:00
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(
|
2019-04-03 09:45:57 -06:00
|
|
|
id(),
|
2019-09-25 13:53:49 -07:00
|
|
|
&VoteInstruction::Authorize(*new_authorized_pubkey, vote_authorize),
|
2019-04-03 09:45:57 -06:00
|
|
|
account_metas,
|
|
|
|
)
|
|
|
|
}
|
2019-03-25 20:57:25 -06:00
|
|
|
|
2020-04-10 14:09:56 -07:00
|
|
|
pub fn update_validator_identity(
|
2019-12-13 00:49:16 -07:00
|
|
|
vote_pubkey: &Pubkey,
|
2020-03-19 01:58:52 -07:00
|
|
|
authorized_withdrawer_pubkey: &Pubkey,
|
2019-12-03 22:50:08 -08:00
|
|
|
node_pubkey: &Pubkey,
|
|
|
|
) -> Instruction {
|
2020-02-24 19:27:04 -08:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
2020-03-19 01:58:52 -07:00
|
|
|
AccountMeta::new_readonly(*node_pubkey, true),
|
2020-05-30 00:17:44 -06:00
|
|
|
AccountMeta::new_readonly(*authorized_withdrawer_pubkey, true),
|
|
|
|
];
|
2019-12-03 22:50:08 -08:00
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(
|
2020-04-10 14:09:56 -07:00
|
|
|
id(),
|
|
|
|
&VoteInstruction::UpdateValidatorIdentity,
|
|
|
|
account_metas,
|
|
|
|
)
|
2019-12-03 22:50:08 -08:00
|
|
|
}
|
|
|
|
|
2020-06-09 21:15:54 -07:00
|
|
|
pub fn update_commission(
|
|
|
|
vote_pubkey: &Pubkey,
|
|
|
|
authorized_withdrawer_pubkey: &Pubkey,
|
|
|
|
commission: u8,
|
|
|
|
) -> Instruction {
|
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(*authorized_withdrawer_pubkey, true),
|
|
|
|
];
|
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(
|
2020-06-09 21:15:54 -07:00
|
|
|
id(),
|
|
|
|
&VoteInstruction::UpdateCommission(commission),
|
|
|
|
account_metas,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
pub fn vote(vote_pubkey: &Pubkey, authorized_voter_pubkey: &Pubkey, vote: Vote) -> Instruction {
|
2019-11-18 15:43:47 -05: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),
|
2020-05-30 00:17:44 -06:00
|
|
|
AccountMeta::new_readonly(*authorized_voter_pubkey, true),
|
|
|
|
];
|
2019-05-20 13:32:32 -07:00
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(id(), &VoteInstruction::Vote(vote), account_metas)
|
2019-03-02 14:51:26 -07:00
|
|
|
}
|
2019-04-10 17:52:47 -07:00
|
|
|
|
2020-05-24 15:38:35 -07:00
|
|
|
pub fn vote_switch(
|
|
|
|
vote_pubkey: &Pubkey,
|
|
|
|
authorized_voter_pubkey: &Pubkey,
|
|
|
|
vote: Vote,
|
|
|
|
proof_hash: Hash,
|
|
|
|
) -> Instruction {
|
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(sysvar::slot_hashes::id(), false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
2020-05-30 00:17:44 -06:00
|
|
|
AccountMeta::new_readonly(*authorized_voter_pubkey, true),
|
|
|
|
];
|
2020-05-24 15:38:35 -07:00
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(
|
2020-05-24 15:38:35 -07:00
|
|
|
id(),
|
|
|
|
&VoteInstruction::VoteSwitch(vote, proof_hash),
|
|
|
|
account_metas,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
pub fn withdraw(
|
|
|
|
vote_pubkey: &Pubkey,
|
2020-03-19 01:58:52 -07:00
|
|
|
authorized_withdrawer_pubkey: &Pubkey,
|
2019-09-25 13:53:49 -07:00
|
|
|
lamports: u64,
|
|
|
|
to_pubkey: &Pubkey,
|
|
|
|
) -> Instruction {
|
2019-11-18 15:43:47 -05:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*vote_pubkey, false),
|
|
|
|
AccountMeta::new(*to_pubkey, false),
|
2020-05-30 00:17:44 -06:00
|
|
|
AccountMeta::new_readonly(*authorized_withdrawer_pubkey, true),
|
|
|
|
];
|
2019-07-25 23:20:47 -07:00
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(id(), &VoteInstruction::Withdraw(lamports), account_metas)
|
2019-07-25 23:20:47 -07:00
|
|
|
}
|
|
|
|
|
2020-10-26 09:14:57 -07:00
|
|
|
fn verify_rent_exemption(
|
|
|
|
keyed_account: &KeyedAccount,
|
|
|
|
rent_sysvar_account: &KeyedAccount,
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
let rent: sysvar::rent::Rent = from_keyed_account(rent_sysvar_account)?;
|
|
|
|
if !rent.is_exempt(keyed_account.lamports()?, keyed_account.data_len()?) {
|
|
|
|
Err(InstructionError::InsufficientFunds)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
pub fn process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
2020-01-22 17:54:06 -08:00
|
|
|
keyed_accounts: &[KeyedAccount],
|
2019-04-10 17:52:47 -07:00
|
|
|
data: &[u8],
|
2021-02-24 10:00:48 -07:00
|
|
|
invoke_context: &mut dyn InvokeContext,
|
2019-04-10 17:52:47 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
trace!("process_instruction: {:?}", data);
|
|
|
|
trace!("keyed_accounts: {:?}", keyed_accounts);
|
|
|
|
|
2020-02-25 17:12:01 -08:00
|
|
|
let signers: HashSet<Pubkey> = get_signers(keyed_accounts);
|
2019-04-30 15:11:08 -07:00
|
|
|
|
2020-01-22 17:54:06 -08:00
|
|
|
let keyed_accounts = &mut keyed_accounts.iter();
|
2019-11-15 17:16:24 -05:00
|
|
|
let me = &mut next_keyed_account(keyed_accounts)?;
|
2019-05-20 13:32:32 -07:00
|
|
|
|
2021-02-26 14:21:34 -08:00
|
|
|
if invoke_context.is_feature_active(&feature_set::check_program_owner::id())
|
|
|
|
&& me.owner()? != id()
|
|
|
|
{
|
|
|
|
return Err(InstructionError::InvalidAccountOwner);
|
|
|
|
}
|
|
|
|
|
2019-10-23 19:56:07 -07:00
|
|
|
match limited_deserialize(data)? {
|
2019-09-25 13:53:49 -07:00
|
|
|
VoteInstruction::InitializeAccount(vote_init) => {
|
2020-10-26 09:14:57 -07:00
|
|
|
verify_rent_exemption(me, next_keyed_account(keyed_accounts)?)?;
|
2019-12-30 19:57:53 -08:00
|
|
|
vote_state::initialize_account(
|
|
|
|
me,
|
|
|
|
&vote_init,
|
2020-03-19 01:58:52 -07:00
|
|
|
&signers,
|
2020-10-26 09:14:57 -07:00
|
|
|
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
|
2021-02-24 10:00:48 -07:00
|
|
|
invoke_context.is_feature_active(&feature_set::check_init_vote_data::id()),
|
2019-12-30 19:57:53 -08:00
|
|
|
)
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
2019-12-30 19:57:53 -08:00
|
|
|
VoteInstruction::Authorize(voter_pubkey, vote_authorize) => vote_state::authorize(
|
|
|
|
me,
|
|
|
|
&voter_pubkey,
|
|
|
|
vote_authorize,
|
|
|
|
&signers,
|
2020-10-26 09:14:57 -07:00
|
|
|
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
|
2019-12-30 19:57:53 -08:00
|
|
|
),
|
2020-04-10 14:09:56 -07:00
|
|
|
VoteInstruction::UpdateValidatorIdentity => vote_state::update_validator_identity(
|
2020-02-24 19:27:04 -08:00
|
|
|
me,
|
2020-03-19 01:58:52 -07:00
|
|
|
next_keyed_account(keyed_accounts)?.unsigned_key(),
|
2020-02-24 19:27:04 -08:00
|
|
|
&signers,
|
|
|
|
),
|
2020-06-09 21:15:54 -07:00
|
|
|
VoteInstruction::UpdateCommission(commission) => {
|
|
|
|
vote_state::update_commission(me, commission, &signers)
|
|
|
|
}
|
2020-05-24 15:38:35 -07:00
|
|
|
VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => {
|
2020-02-21 13:41:49 -08:00
|
|
|
inc_new_counter_info!("vote-native", 1);
|
2019-09-02 12:01:09 -07:00
|
|
|
vote_state::process_vote(
|
2019-06-21 20:43:24 -07:00
|
|
|
me,
|
2020-10-26 09:14:57 -07:00
|
|
|
&from_keyed_account::<SlotHashes>(next_keyed_account(keyed_accounts)?)?,
|
|
|
|
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
|
2019-09-02 12:01:09 -07:00
|
|
|
&vote,
|
2019-11-15 17:16:24 -05: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 17:16:24 -05: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::*;
|
2020-10-28 22:01:07 -07:00
|
|
|
use solana_sdk::{
|
2021-03-09 15:06:07 -06:00
|
|
|
account::{self, AccountSharedData},
|
2020-10-28 20:21:50 -07:00
|
|
|
process_instruction::MockInvokeContext,
|
2020-10-28 22:01:07 -07:00
|
|
|
rent::Rent,
|
|
|
|
};
|
2020-01-22 09:11:56 -08:00
|
|
|
use std::cell::RefCell;
|
2021-02-26 14:21:34 -08:00
|
|
|
use std::str::FromStr;
|
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!(
|
2020-10-28 20:21:50 -07:00
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&[],
|
|
|
|
&[],
|
|
|
|
&mut MockInvokeContext::default()
|
|
|
|
),
|
2019-11-15 17:16:24 -05:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-04-30 15:11:08 -07:00
|
|
|
);
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
|
|
|
|
2020-08-14 11:43:14 -07:00
|
|
|
#[allow(clippy::same_item_push)]
|
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| {
|
2020-01-22 09:11:56 -08:00
|
|
|
RefCell::new(if sysvar::clock::check_id(&meta.pubkey) {
|
2021-03-09 15:06:07 -06:00
|
|
|
account::create_account_shared_data(&Clock::default(), 1)
|
2019-07-12 16:38:15 -07:00
|
|
|
} else if sysvar::slot_hashes::check_id(&meta.pubkey) {
|
2021-03-09 15:06:07 -06:00
|
|
|
account::create_account_shared_data(&SlotHashes::default(), 1)
|
2019-10-04 02:52:48 +05:30
|
|
|
} else if sysvar::rent::check_id(&meta.pubkey) {
|
2021-03-09 15:06:07 -06:00
|
|
|
account::create_account_shared_data(&Rent::free(), 1)
|
2021-02-26 14:21:34 -08:00
|
|
|
} else if meta.pubkey == invalid_vote_state_pubkey() {
|
2021-03-09 15:06:07 -06:00
|
|
|
AccountSharedData {
|
2021-02-26 14:21:34 -08:00
|
|
|
owner: invalid_vote_state_pubkey(),
|
2021-03-09 15:06:07 -06:00
|
|
|
..AccountSharedData::default()
|
2021-02-26 14:21:34 -08:00
|
|
|
}
|
2019-06-21 20:43:24 -07:00
|
|
|
} else {
|
2021-03-09 15:06:07 -06:00
|
|
|
AccountSharedData {
|
2021-02-26 14:21:34 -08:00
|
|
|
owner: id(),
|
2021-03-09 15:06:07 -06:00
|
|
|
..AccountSharedData::default()
|
2021-02-26 14:21:34 -08:00
|
|
|
}
|
2020-01-22 09:11:56 -08:00
|
|
|
})
|
2019-06-21 20:43:24 -07:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2019-04-30 15:11:08 -07:00
|
|
|
for _ in 0..instruction.accounts.len() {
|
2021-03-09 15:06:07 -06:00
|
|
|
accounts.push(RefCell::new(AccountSharedData::default()));
|
2019-04-30 15:11:08 -07:00
|
|
|
}
|
|
|
|
{
|
2020-01-22 17:54:06 -08:00
|
|
|
let keyed_accounts: Vec<_> = instruction
|
2019-04-30 15:11:08 -07:00
|
|
|
.accounts
|
|
|
|
.iter()
|
2020-01-22 17:54:06 -08:00
|
|
|
.zip(accounts.iter())
|
2019-04-30 15:11:08 -07:00
|
|
|
.map(|(meta, account)| KeyedAccount::new(&meta.pubkey, meta.is_signer, account))
|
|
|
|
.collect();
|
2020-10-28 20:21:50 -07:00
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&keyed_accounts,
|
|
|
|
&instruction.data,
|
|
|
|
&mut MockInvokeContext::default(),
|
|
|
|
)
|
2019-04-30 15:11:08 -07:00
|
|
|
}
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
|
|
|
|
2021-02-26 14:21:34 -08:00
|
|
|
fn invalid_vote_state_pubkey() -> Pubkey {
|
|
|
|
Pubkey::from_str("BadVote111111111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_spoofed_vote() {
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&vote(
|
|
|
|
&invalid_vote_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
Vote::default(),
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
#[test]
|
2019-04-30 15:11:08 -07:00
|
|
|
fn test_vote_process_instruction() {
|
2021-02-26 14:21:34 -08:00
|
|
|
solana_logger::setup();
|
2019-04-30 15:11:08 -07:00
|
|
|
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),
|
2020-05-24 15:38:35 -07:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&vote_switch(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
Vote::default(),
|
|
|
|
Hash::default(),
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
2019-04-30 15:11:08 -07:00
|
|
|
);
|
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),
|
|
|
|
);
|
2019-12-03 22:50:08 -08:00
|
|
|
assert_eq!(
|
2020-04-10 14:09:56 -07:00
|
|
|
process_instruction(&update_validator_identity(
|
2019-12-03 22:50:08 -08:00
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2020-06-09 21:15:54 -07:00
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&update_commission(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
0,
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2019-12-03 22:50:08 -08:00
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
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-12-30 19:57:53 -08:00
|
|
|
// golden, may need updating when vote_state grows
|
|
|
|
assert!(minimum_balance as f64 / 10f64.powf(9.0) < 0.04)
|
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,
|
|
|
|
{
|
2020-04-01 09:01:11 -07:00
|
|
|
if let InstructionError::Custom(code) = err {
|
2019-09-25 13:53:49 -07:00
|
|
|
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!(
|
2020-04-01 09:01:11 -07:00
|
|
|
"Custom(0): VoteError::VoteTooOld - vote already recorded or not in slot hashes history",
|
2019-09-25 13:53:49 -07:00
|
|
|
pretty_err::<VoteError>(VoteError::VoteTooOld.into())
|
|
|
|
)
|
|
|
|
}
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|