2019-04-10 17:52:47 -07:00
|
|
|
//! Vote state, vote program
|
2018-12-04 07:45:32 -08:00
|
|
|
//! Receive and processes votes from validators
|
2019-04-10 17:52:47 -07:00
|
|
|
use crate::id;
|
2019-01-10 16:15:27 -08:00
|
|
|
use bincode::{deserialize, serialize_into, serialized_size, ErrorKind};
|
2019-03-02 13:51:26 -08:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use solana_sdk::account::{Account, KeyedAccount};
|
2019-03-23 20:12:27 -07:00
|
|
|
use solana_sdk::instruction::InstructionError;
|
2019-04-10 17:52:47 -07:00
|
|
|
use solana_sdk::instruction_processor_utils::State;
|
2019-03-02 13:51:26 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-12-04 07:45:32 -08:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
// Maximum number of votes to keep around
|
2019-02-26 21:19:31 -08:00
|
|
|
pub const MAX_LOCKOUT_HISTORY: usize = 31;
|
|
|
|
pub const INITIAL_LOCKOUT: usize = 2;
|
2018-12-04 07:45:32 -08:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct Vote {
|
|
|
|
// TODO: add signature of the state here as well
|
|
|
|
/// A vote for height slot
|
|
|
|
pub slot: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Vote {
|
|
|
|
pub fn new(slot: u64) -> Self {
|
|
|
|
Self { slot }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 21:19:31 -08:00
|
|
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct Lockout {
|
2019-03-05 14:18:29 -08:00
|
|
|
pub slot: u64,
|
2019-02-26 21:19:31 -08:00
|
|
|
pub confirmation_count: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lockout {
|
|
|
|
pub fn new(vote: &Vote) -> Self {
|
|
|
|
Self {
|
2019-03-05 14:18:29 -08:00
|
|
|
slot: vote.slot,
|
2019-02-26 21:19:31 -08:00
|
|
|
confirmation_count: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The number of slots for which this vote is locked
|
|
|
|
pub fn lockout(&self) -> u64 {
|
|
|
|
(INITIAL_LOCKOUT as u64).pow(self.confirmation_count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The slot height at which this vote expires (cannot vote for any slot
|
|
|
|
// less than this)
|
2019-03-05 14:18:29 -08:00
|
|
|
pub fn expiration_slot(&self) -> u64 {
|
|
|
|
self.slot + self.lockout()
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
2019-03-18 12:12:33 -07:00
|
|
|
pub fn is_expired(&self, slot: u64) -> bool {
|
|
|
|
self.expiration_slot() < slot
|
|
|
|
}
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
2019-03-01 11:54:28 -08:00
|
|
|
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
2019-02-01 14:50:11 -08:00
|
|
|
pub struct VoteState {
|
2019-02-26 21:19:31 -08:00
|
|
|
pub votes: VecDeque<Lockout>,
|
2019-04-10 17:52:47 -07:00
|
|
|
pub node_id: Pubkey,
|
2019-03-06 13:28:21 -08:00
|
|
|
pub authorized_voter_id: Pubkey,
|
2019-04-07 21:45:28 -07:00
|
|
|
/// fraction of std::u32::MAX that represents what part of a rewards
|
|
|
|
/// payout should be given to this VoteAccount
|
|
|
|
pub commission: u32,
|
2019-02-26 21:19:31 -08:00
|
|
|
pub root_slot: Option<u64>,
|
2019-02-05 09:25:59 -08:00
|
|
|
credits: u64,
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:50:11 -08:00
|
|
|
impl VoteState {
|
2019-04-10 17:52:47 -07:00
|
|
|
pub fn new(vote_id: &Pubkey, node_id: &Pubkey, commission: u32) -> Self {
|
2019-01-21 15:45:30 -08:00
|
|
|
let votes = VecDeque::new();
|
2019-02-05 09:25:59 -08:00
|
|
|
let credits = 0;
|
2019-02-26 21:19:31 -08:00
|
|
|
let root_slot = None;
|
2019-02-01 15:35:50 -08:00
|
|
|
Self {
|
|
|
|
votes,
|
2019-04-10 17:52:47 -07:00
|
|
|
node_id: *node_id,
|
|
|
|
authorized_voter_id: *vote_id,
|
2019-02-05 09:25:59 -08:00
|
|
|
credits,
|
2019-04-07 21:45:28 -07:00
|
|
|
commission,
|
2019-02-26 21:19:31 -08:00
|
|
|
root_slot,
|
2019-02-01 15:35:50 -08:00
|
|
|
}
|
2019-01-21 15:45:30 -08:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
pub fn size_of() -> usize {
|
2019-03-02 13:51:26 -08:00
|
|
|
// Upper limit on the size of the Vote State. Equal to
|
2019-04-10 17:52:47 -07:00
|
|
|
// size_of(VoteState) when votes.len() is MAX_LOCKOUT_HISTORY
|
2019-03-02 13:51:26 -08:00
|
|
|
let mut vote_state = Self::default();
|
|
|
|
vote_state.votes = VecDeque::from(vec![Lockout::default(); MAX_LOCKOUT_HISTORY]);
|
|
|
|
vote_state.root_slot = Some(std::u64::MAX);
|
|
|
|
serialized_size(&vote_state).unwrap() as usize
|
|
|
|
}
|
|
|
|
|
2019-03-18 09:05:03 -07:00
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, InstructionError> {
|
|
|
|
deserialize(input).map_err(|_| InstructionError::InvalidAccountData)
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-03-18 09:05:03 -07:00
|
|
|
pub fn serialize(&self, output: &mut [u8]) -> Result<(), InstructionError> {
|
2019-01-10 16:15:27 -08:00
|
|
|
serialize_into(output, self).map_err(|err| match *err {
|
2019-03-18 09:05:03 -07:00
|
|
|
ErrorKind::SizeLimit => InstructionError::AccountDataTooSmall,
|
|
|
|
_ => InstructionError::GenericError,
|
2019-01-10 16:15:27 -08:00
|
|
|
})
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
2019-02-05 09:25:59 -08:00
|
|
|
|
2019-04-07 21:45:28 -07:00
|
|
|
/// returns commission split as (voter_portion, staker_portion) tuple
|
|
|
|
///
|
|
|
|
/// if commission calculation is 100% one way or other,
|
|
|
|
/// indicate with None for the 0% side
|
|
|
|
pub fn commission_split(&self, on: f64) -> (f64, f64, bool) {
|
|
|
|
match self.commission {
|
|
|
|
0 => (0.0, on, false),
|
|
|
|
std::u32::MAX => (on, 0.0, false),
|
|
|
|
split => {
|
|
|
|
let mine = on * f64::from(split) / f64::from(std::u32::MAX);
|
|
|
|
(mine, on - mine, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 14:48:36 -07:00
|
|
|
pub fn process_votes(&mut self, votes: &[Vote]) {
|
|
|
|
votes.iter().for_each(|v| self.process_vote(v));;
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
pub fn process_vote(&mut self, vote: &Vote) {
|
2019-02-26 21:19:31 -08:00
|
|
|
// Ignore votes for slots earlier than we already have votes for
|
|
|
|
if self
|
|
|
|
.votes
|
|
|
|
.back()
|
2019-03-05 14:18:29 -08:00
|
|
|
.map_or(false, |old_vote| old_vote.slot >= vote.slot)
|
2019-02-26 21:19:31 -08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let vote = Lockout::new(&vote);
|
|
|
|
|
2019-02-05 09:25:59 -08:00
|
|
|
// TODO: Integrity checks
|
2019-02-26 21:19:31 -08:00
|
|
|
// Verify the vote's bank hash matches what is expected
|
2019-02-05 09:25:59 -08:00
|
|
|
|
2019-03-05 14:18:29 -08:00
|
|
|
self.pop_expired_votes(vote.slot);
|
2019-02-26 21:19:31 -08:00
|
|
|
// Once the stack is full, pop the oldest vote and distribute rewards
|
|
|
|
if self.votes.len() == MAX_LOCKOUT_HISTORY {
|
|
|
|
let vote = self.votes.pop_front().unwrap();
|
2019-03-05 14:18:29 -08:00
|
|
|
self.root_slot = Some(vote.slot);
|
2019-02-05 09:25:59 -08:00
|
|
|
self.credits += 1;
|
|
|
|
}
|
|
|
|
self.votes.push_back(vote);
|
2019-02-26 21:19:31 -08:00
|
|
|
self.double_lockouts();
|
2019-02-05 09:25:59 -08:00
|
|
|
}
|
|
|
|
|
2019-03-18 12:12:33 -07:00
|
|
|
pub fn nth_recent_vote(&self, position: usize) -> Option<&Lockout> {
|
|
|
|
if position < self.votes.len() {
|
|
|
|
let pos = self.votes.len() - 1 - position;
|
|
|
|
self.votes.get(pos)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 09:25:59 -08:00
|
|
|
/// Number of "credits" owed to this account from the mining pool. Submit this
|
|
|
|
/// VoteState to the Rewards program to trade credits for lamports.
|
|
|
|
pub fn credits(&self) -> u64 {
|
|
|
|
self.credits
|
|
|
|
}
|
|
|
|
|
2019-03-05 14:18:29 -08:00
|
|
|
fn pop_expired_votes(&mut self, slot: u64) {
|
2019-02-26 21:19:31 -08:00
|
|
|
loop {
|
2019-03-18 12:12:33 -07:00
|
|
|
if self.votes.back().map_or(false, |v| v.is_expired(slot)) {
|
2019-02-26 21:19:31 -08:00
|
|
|
self.votes.pop_back();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn double_lockouts(&mut self) {
|
|
|
|
let stack_depth = self.votes.len();
|
|
|
|
for (i, v) in self.votes.iter_mut().enumerate() {
|
|
|
|
// Don't increase the lockout for this vote until we get more confirmations
|
|
|
|
// than the max number of confirmations this vote has seen
|
|
|
|
if stack_depth > i + v.confirmation_count as usize {
|
|
|
|
v.confirmation_count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-03-06 13:28:21 -08:00
|
|
|
/// Authorize the given pubkey to sign votes. This may be called multiple times,
|
|
|
|
/// but will implicitly withdraw authorization from the previously authorized
|
|
|
|
/// voter. The default voter is the owner of the vote account's pubkey.
|
|
|
|
pub fn authorize_voter(
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_account: &mut KeyedAccount,
|
|
|
|
other_signers: &[KeyedAccount],
|
|
|
|
authorized_voter_id: &Pubkey,
|
2019-03-18 09:05:03 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_state: VoteState = vote_account.state()?;
|
2019-03-06 13:28:21 -08:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
// current authorized signer must say "yay"
|
|
|
|
let authorized = Some(&vote_state.authorized_voter_id);
|
|
|
|
if vote_account.signer_key() != authorized
|
|
|
|
&& other_signers
|
|
|
|
.iter()
|
|
|
|
.all(|account| account.signer_key() != authorized)
|
|
|
|
{
|
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
2019-03-06 13:28:21 -08:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.authorized_voter_id = *authorized_voter_id;
|
|
|
|
vote_account.set_state(&vote_state)
|
2019-03-06 13:28:21 -08:00
|
|
|
}
|
|
|
|
|
2019-03-04 17:50:19 -08:00
|
|
|
/// Initialize the vote_state for a vote account
|
2019-02-28 17:08:45 -08:00
|
|
|
/// Assumes that the account is being init as part of a account creation or balance transfer and
|
|
|
|
/// that the transaction must be signed by the staker's keys
|
2019-04-10 17:52:47 -07:00
|
|
|
pub fn initialize_account(
|
|
|
|
vote_account: &mut KeyedAccount,
|
|
|
|
node_id: &Pubkey,
|
|
|
|
commission: u32,
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
let vote_state: VoteState = vote_account.state()?;
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
if vote_state.authorized_voter_id != Pubkey::default() {
|
|
|
|
return Err(InstructionError::AccountAlreadyInitialized);
|
|
|
|
}
|
|
|
|
vote_account.set_state(&VoteState::new(
|
|
|
|
vote_account.unsigned_key(),
|
|
|
|
node_id,
|
|
|
|
commission,
|
|
|
|
))
|
2019-02-15 07:42:09 -08:00
|
|
|
}
|
|
|
|
|
2019-03-18 09:05:03 -07:00
|
|
|
pub fn process_vote(
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_account: &mut KeyedAccount,
|
|
|
|
other_signers: &[KeyedAccount],
|
2019-04-11 14:48:36 -07:00
|
|
|
votes: &[Vote],
|
2019-03-18 09:05:03 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_state: VoteState = vote_account.state()?;
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
if vote_state.authorized_voter_id == Pubkey::default() {
|
|
|
|
return Err(InstructionError::UninitializedAccount);
|
2019-03-06 13:28:21 -08:00
|
|
|
}
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
let authorized = Some(&vote_state.authorized_voter_id);
|
|
|
|
// find a signer that matches the authorized_voter_id
|
|
|
|
if vote_account.signer_key() != authorized
|
|
|
|
&& other_signers
|
|
|
|
.iter()
|
|
|
|
.all(|account| account.signer_key() != authorized)
|
|
|
|
{
|
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
2019-02-15 12:20:34 -08:00
|
|
|
}
|
|
|
|
|
2019-04-11 14:48:36 -07:00
|
|
|
vote_state.process_votes(&votes);
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_account.set_state(&vote_state)
|
2019-02-15 07:42:09 -08:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
// utility function, used by Bank, tests
|
|
|
|
pub fn create_account(
|
2019-02-15 07:42:09 -08:00
|
|
|
vote_id: &Pubkey,
|
2019-04-10 17:52:47 -07:00
|
|
|
node_id: &Pubkey,
|
|
|
|
commission: u32,
|
|
|
|
lamports: u64,
|
|
|
|
) -> Account {
|
|
|
|
let mut vote_account = Account::new(lamports, VoteState::size_of(), &id());
|
|
|
|
|
|
|
|
initialize_account(
|
|
|
|
&mut KeyedAccount::new(vote_id, false, &mut vote_account),
|
|
|
|
node_id,
|
|
|
|
commission,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
vote_account
|
2019-02-15 07:42:09 -08:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
// utility function, used by Bank, tests
|
|
|
|
pub fn vote(
|
2019-02-15 07:42:09 -08:00
|
|
|
vote_id: &Pubkey,
|
|
|
|
vote_account: &mut Account,
|
2019-04-10 17:52:47 -07:00
|
|
|
vote: &Vote,
|
2019-03-18 09:05:03 -07:00
|
|
|
) -> Result<VoteState, InstructionError> {
|
2019-04-10 17:52:47 -07:00
|
|
|
process_vote(
|
|
|
|
&mut KeyedAccount::new(vote_id, true, vote_account),
|
|
|
|
&[],
|
2019-04-11 14:48:36 -07:00
|
|
|
&[vote.clone()],
|
2019-04-10 17:52:47 -07:00
|
|
|
)?;
|
|
|
|
vote_account.state()
|
2019-02-15 07:42:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 07:45:32 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-04-10 17:52:47 -07:00
|
|
|
use crate::vote_state;
|
2018-12-04 07:45:32 -08:00
|
|
|
|
2019-04-11 14:48:36 -07:00
|
|
|
const MAX_RECENT_VOTES: usize = 16;
|
|
|
|
|
2019-02-28 17:08:45 -08:00
|
|
|
#[test]
|
2019-03-04 17:50:19 -08:00
|
|
|
fn test_initialize_vote_account() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let vote_account_id = Pubkey::new_rand();
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_account = Account::new(100, VoteState::size_of(), &id());
|
2019-02-28 17:08:45 -08:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
let node_id = Pubkey::new_rand();
|
2019-02-28 17:08:45 -08:00
|
|
|
|
|
|
|
//init should pass
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_account = KeyedAccount::new(&vote_account_id, false, &mut vote_account);
|
|
|
|
let res = initialize_account(&mut vote_account, &node_id, 0);
|
2019-02-28 17:08:45 -08:00
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// reinit should fail
|
2019-04-10 17:52:47 -07:00
|
|
|
let res = initialize_account(&mut vote_account, &node_id, 0);
|
|
|
|
assert_eq!(res, Err(InstructionError::AccountAlreadyInitialized));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_test_account() -> (Pubkey, Account) {
|
|
|
|
let vote_id = Pubkey::new_rand();
|
|
|
|
(
|
|
|
|
vote_id,
|
|
|
|
vote_state::create_account(&vote_id, &Pubkey::new_rand(), 0, 100),
|
|
|
|
)
|
2019-02-28 17:08:45 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 07:45:32 -08:00
|
|
|
#[test]
|
2019-02-05 09:25:59 -08:00
|
|
|
fn test_vote_serialize() {
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut buffer: Vec<u8> = vec![0; VoteState::size_of()];
|
2019-02-05 09:25:59 -08:00
|
|
|
let mut vote_state = VoteState::default();
|
2019-02-26 21:19:31 -08:00
|
|
|
vote_state
|
|
|
|
.votes
|
|
|
|
.resize(MAX_LOCKOUT_HISTORY, Lockout::default());
|
2019-04-10 17:52:47 -07:00
|
|
|
assert!(vote_state.serialize(&mut buffer[0..4]).is_err());
|
2019-02-05 09:25:59 -08:00
|
|
|
vote_state.serialize(&mut buffer).unwrap();
|
|
|
|
assert_eq!(VoteState::deserialize(&buffer).unwrap(), vote_state);
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:42:09 -08:00
|
|
|
#[test]
|
|
|
|
fn test_voter_registration() {
|
2019-04-10 17:52:47 -07:00
|
|
|
let (vote_id, vote_account) = create_test_account();
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
let vote_state: VoteState = vote_account.state().unwrap();
|
|
|
|
assert_eq!(vote_state.authorized_voter_id, vote_id);
|
2019-02-15 07:42:09 -08:00
|
|
|
assert!(vote_state.votes.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote() {
|
2019-04-10 17:52:47 -07:00
|
|
|
let (vote_id, mut vote_account) = create_test_account();
|
2019-02-15 07:42:09 -08:00
|
|
|
|
|
|
|
let vote = Vote::new(1);
|
2019-04-10 17:52:47 -07:00
|
|
|
let vote_state = vote_state::vote(&vote_id, &mut vote_account, &vote).unwrap();
|
2019-02-26 21:19:31 -08:00
|
|
|
assert_eq!(vote_state.votes, vec![Lockout::new(&vote)]);
|
2019-02-15 07:42:09 -08:00
|
|
|
assert_eq!(vote_state.credits(), 0);
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:21:16 -08:00
|
|
|
#[test]
|
|
|
|
fn test_vote_signature() {
|
2019-04-10 17:52:47 -07:00
|
|
|
let (vote_id, mut vote_account) = create_test_account();
|
2019-03-01 17:21:16 -08:00
|
|
|
|
2019-04-11 14:48:36 -07:00
|
|
|
let vote = vec![Vote::new(1)];
|
2019-04-10 17:52:47 -07:00
|
|
|
|
|
|
|
// unsigned
|
|
|
|
let res = process_vote(
|
|
|
|
&mut KeyedAccount::new(&vote_id, false, &mut vote_account),
|
|
|
|
&[],
|
|
|
|
&vote,
|
|
|
|
);
|
|
|
|
assert_eq!(res, Err(InstructionError::MissingRequiredSignature));
|
|
|
|
|
|
|
|
// unsigned
|
|
|
|
let res = process_vote(
|
|
|
|
&mut KeyedAccount::new(&vote_id, true, &mut vote_account),
|
|
|
|
&[],
|
|
|
|
&vote,
|
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// another voter
|
|
|
|
let authorized_voter_id = Pubkey::new_rand();
|
|
|
|
let res = authorize_voter(
|
|
|
|
&mut KeyedAccount::new(&vote_id, false, &mut vote_account),
|
|
|
|
&[],
|
|
|
|
&authorized_voter_id,
|
|
|
|
);
|
|
|
|
assert_eq!(res, Err(InstructionError::MissingRequiredSignature));
|
|
|
|
|
|
|
|
let res = authorize_voter(
|
|
|
|
&mut KeyedAccount::new(&vote_id, true, &mut vote_account),
|
|
|
|
&[],
|
|
|
|
&authorized_voter_id,
|
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
// verify authorized_voter_id can authorize authorized_voter_id ;)
|
|
|
|
let res = authorize_voter(
|
|
|
|
&mut KeyedAccount::new(&vote_id, false, &mut vote_account),
|
|
|
|
&[KeyedAccount::new(
|
|
|
|
&authorized_voter_id,
|
|
|
|
true,
|
|
|
|
&mut Account::default(),
|
|
|
|
)],
|
|
|
|
&authorized_voter_id,
|
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// not signed by authorized voter
|
2019-04-11 14:48:36 -07:00
|
|
|
let vote = vec![Vote::new(2)];
|
2019-04-10 17:52:47 -07:00
|
|
|
let res = process_vote(
|
|
|
|
&mut KeyedAccount::new(&vote_id, true, &mut vote_account),
|
|
|
|
&[],
|
|
|
|
&vote,
|
|
|
|
);
|
|
|
|
assert_eq!(res, Err(InstructionError::MissingRequiredSignature));
|
|
|
|
|
|
|
|
// signed by authorized voter
|
2019-04-11 14:48:36 -07:00
|
|
|
let vote = vec![Vote::new(2)];
|
2019-04-10 17:52:47 -07:00
|
|
|
let res = process_vote(
|
|
|
|
&mut KeyedAccount::new(&vote_id, false, &mut vote_account),
|
|
|
|
&[KeyedAccount::new(
|
|
|
|
&authorized_voter_id,
|
|
|
|
true,
|
|
|
|
&mut Account::default(),
|
|
|
|
)],
|
|
|
|
&vote,
|
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
2019-03-01 17:21:16 -08:00
|
|
|
}
|
|
|
|
|
2019-02-15 07:42:09 -08:00
|
|
|
#[test]
|
2019-02-28 17:08:45 -08:00
|
|
|
fn test_vote_without_initialization() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let vote_id = Pubkey::new_rand();
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_account = Account::new(100, VoteState::size_of(), &id());
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
let res = vote_state::vote(&vote_id, &mut vote_account, &Vote::new(1));
|
|
|
|
assert_eq!(res, Err(InstructionError::UninitializedAccount));
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote_lockout() {
|
2019-04-10 17:52:47 -07:00
|
|
|
let (_vote_id, vote_account) = create_test_account();
|
|
|
|
|
|
|
|
let mut vote_state: VoteState = vote_account.state().unwrap();
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..(MAX_LOCKOUT_HISTORY + 1) {
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new((INITIAL_LOCKOUT as usize * i) as u64));
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The last vote should have been popped b/c it reached a depth of MAX_LOCKOUT_HISTORY
|
|
|
|
assert_eq!(vote_state.votes.len(), MAX_LOCKOUT_HISTORY);
|
|
|
|
assert_eq!(vote_state.root_slot, Some(0));
|
|
|
|
check_lockouts(&vote_state);
|
|
|
|
|
|
|
|
// One more vote that confirms the entire stack,
|
|
|
|
// the root_slot should change to the
|
|
|
|
// second vote
|
2019-03-05 14:18:29 -08:00
|
|
|
let top_vote = vote_state.votes.front().unwrap().slot;
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new(
|
2019-03-05 14:18:29 -08:00
|
|
|
vote_state.votes.back().unwrap().expiration_slot(),
|
2019-02-26 21:19:31 -08:00
|
|
|
));
|
|
|
|
assert_eq!(Some(top_vote), vote_state.root_slot);
|
|
|
|
|
|
|
|
// Expire everything except the first vote
|
2019-03-05 14:18:29 -08:00
|
|
|
let vote = Vote::new(vote_state.votes.front().unwrap().expiration_slot());
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&vote);
|
2019-02-26 21:19:31 -08:00
|
|
|
// First vote and new vote are both stored for a total of 2 votes
|
|
|
|
assert_eq!(vote_state.votes.len(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote_double_lockout_after_expiration() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let voter_id = Pubkey::new_rand();
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_state = VoteState::new(&voter_id, &Pubkey::new_rand(), 0);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..3 {
|
|
|
|
let vote = Vote::new(i as u64);
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&vote);
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
check_lockouts(&vote_state);
|
|
|
|
|
|
|
|
// Expire the third vote (which was a vote for slot 2). The height of the
|
|
|
|
// vote stack is unchanged, so none of the previous votes should have
|
|
|
|
// doubled in lockout
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new((2 + INITIAL_LOCKOUT + 1) as u64));
|
2019-02-26 21:19:31 -08:00
|
|
|
check_lockouts(&vote_state);
|
|
|
|
|
|
|
|
// Vote again, this time the vote stack depth increases, so the lockouts should
|
|
|
|
// double for everybody
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new((2 + INITIAL_LOCKOUT + 2) as u64));
|
2019-02-26 21:19:31 -08:00
|
|
|
check_lockouts(&vote_state);
|
2019-04-05 22:11:40 -07:00
|
|
|
|
|
|
|
// Vote again, this time the vote stack depth increases, so the lockouts should
|
|
|
|
// double for everybody
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new((2 + INITIAL_LOCKOUT + 3) as u64));
|
2019-04-05 22:11:40 -07:00
|
|
|
check_lockouts(&vote_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_expire_multiple_votes() {
|
|
|
|
let voter_id = Pubkey::new_rand();
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_state = VoteState::new(&voter_id, &Pubkey::new_rand(), 0);
|
2019-04-05 22:11:40 -07:00
|
|
|
|
|
|
|
for i in 0..3 {
|
|
|
|
let vote = Vote::new(i as u64);
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&vote);
|
2019-04-05 22:11:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(vote_state.votes[0].confirmation_count, 3);
|
|
|
|
|
|
|
|
// Expire the second and third votes
|
|
|
|
let expire_slot = vote_state.votes[1].slot + vote_state.votes[1].lockout() + 1;
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new(expire_slot));
|
2019-04-05 22:11:40 -07:00
|
|
|
assert_eq!(vote_state.votes.len(), 2);
|
|
|
|
|
|
|
|
// Check that the old votes expired
|
|
|
|
assert_eq!(vote_state.votes[0].slot, 0);
|
|
|
|
assert_eq!(vote_state.votes[1].slot, expire_slot);
|
|
|
|
|
|
|
|
// Process one more vote
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new(expire_slot + 1));
|
2019-04-05 22:11:40 -07:00
|
|
|
|
|
|
|
// Confirmation count for the older first vote should remain unchanged
|
|
|
|
assert_eq!(vote_state.votes[0].confirmation_count, 3);
|
|
|
|
|
|
|
|
// The later votes should still have increasing confirmation counts
|
|
|
|
assert_eq!(vote_state.votes[1].confirmation_count, 2);
|
|
|
|
assert_eq!(vote_state.votes[2].confirmation_count, 1);
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote_credits() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let voter_id = Pubkey::new_rand();
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_state = VoteState::new(&voter_id, &Pubkey::new_rand(), 0);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..MAX_LOCKOUT_HISTORY {
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new(i as u64));
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(vote_state.credits, 0);
|
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new(MAX_LOCKOUT_HISTORY as u64 + 1));
|
2019-02-26 21:19:31 -08:00
|
|
|
assert_eq!(vote_state.credits, 1);
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new(MAX_LOCKOUT_HISTORY as u64 + 2));
|
2019-02-26 21:19:31 -08:00
|
|
|
assert_eq!(vote_state.credits(), 2);
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new(MAX_LOCKOUT_HISTORY as u64 + 3));
|
2019-02-26 21:19:31 -08:00
|
|
|
assert_eq!(vote_state.credits(), 3);
|
|
|
|
}
|
|
|
|
|
2019-03-18 12:12:33 -07:00
|
|
|
#[test]
|
|
|
|
fn test_duplicate_vote() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let voter_id = Pubkey::new_rand();
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_state = VoteState::new(&voter_id, &Pubkey::new_rand(), 0);
|
|
|
|
vote_state.process_vote(&Vote::new(0));
|
|
|
|
vote_state.process_vote(&Vote::new(1));
|
|
|
|
vote_state.process_vote(&Vote::new(0));
|
2019-03-18 12:12:33 -07:00
|
|
|
assert_eq!(vote_state.nth_recent_vote(0).unwrap().slot, 1);
|
|
|
|
assert_eq!(vote_state.nth_recent_vote(1).unwrap().slot, 0);
|
|
|
|
assert!(vote_state.nth_recent_vote(2).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nth_recent_vote() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let voter_id = Pubkey::new_rand();
|
2019-04-10 17:52:47 -07:00
|
|
|
let mut vote_state = VoteState::new(&voter_id, &Pubkey::new_rand(), 0);
|
2019-03-18 12:12:33 -07:00
|
|
|
for i in 0..MAX_LOCKOUT_HISTORY {
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_state.process_vote(&Vote::new(i as u64));
|
2019-03-18 12:12:33 -07:00
|
|
|
}
|
|
|
|
for i in 0..(MAX_LOCKOUT_HISTORY - 1) {
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.nth_recent_vote(i).unwrap().slot as usize,
|
|
|
|
MAX_LOCKOUT_HISTORY - i - 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert!(vote_state.nth_recent_vote(MAX_LOCKOUT_HISTORY).is_none());
|
|
|
|
}
|
|
|
|
|
2019-02-26 21:19:31 -08:00
|
|
|
fn check_lockouts(vote_state: &VoteState) {
|
|
|
|
for (i, vote) in vote_state.votes.iter().enumerate() {
|
|
|
|
let num_lockouts = vote_state.votes.len() - i;
|
|
|
|
assert_eq!(
|
|
|
|
vote.lockout(),
|
|
|
|
INITIAL_LOCKOUT.pow(num_lockouts as u32) as u64
|
|
|
|
);
|
|
|
|
}
|
2019-02-15 07:42:09 -08:00
|
|
|
}
|
2019-04-11 14:48:36 -07:00
|
|
|
|
|
|
|
fn recent_votes(vote_state: &VoteState) -> Vec<Vote> {
|
|
|
|
let start = vote_state.votes.len().saturating_sub(MAX_RECENT_VOTES);
|
|
|
|
(start..vote_state.votes.len())
|
|
|
|
.map(|i| Vote::new(vote_state.votes.get(i).unwrap().slot))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// check that two accounts with different data can be brought to the same state with one vote submission
|
|
|
|
#[test]
|
|
|
|
fn test_process_missed_votes() {
|
|
|
|
let account_a = Pubkey::new_rand();
|
|
|
|
let mut vote_state_a = VoteState::new(&account_a, &Pubkey::new_rand(), 0);
|
|
|
|
let account_b = Pubkey::new_rand();
|
|
|
|
let mut vote_state_b = VoteState::new(&account_b, &Pubkey::new_rand(), 0);
|
|
|
|
|
|
|
|
// process some votes on account a
|
|
|
|
let votes_a: Vec<_> = (0..5).into_iter().map(|i| Vote::new(i)).collect();
|
|
|
|
vote_state_a.process_votes(&votes_a);
|
|
|
|
assert_ne!(recent_votes(&vote_state_a), recent_votes(&vote_state_b));
|
|
|
|
|
|
|
|
// as long as b has missed less than "NUM_RECENT" votes both accounts should be in sync
|
|
|
|
let votes: Vec<_> = (0..MAX_RECENT_VOTES)
|
|
|
|
.into_iter()
|
|
|
|
.map(|i| Vote::new(i as u64))
|
|
|
|
.collect();
|
|
|
|
vote_state_a.process_votes(&votes);
|
|
|
|
vote_state_b.process_votes(&votes);
|
|
|
|
assert_eq!(recent_votes(&vote_state_a), recent_votes(&vote_state_b));
|
|
|
|
}
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|