2019-11-15 14:16:24 -08:00
|
|
|
#![allow(clippy::implicit_hasher)]
|
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-09-09 18:17:32 -07:00
|
|
|
use crate::{id, vote_instruction::VoteError};
|
2019-01-10 16:15:27 -08:00
|
|
|
use bincode::{deserialize, serialize_into, serialized_size, ErrorKind};
|
2019-05-21 21:45:38 -07:00
|
|
|
use log::*;
|
2019-03-02 13:51:26 -08:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-09-06 10:55:03 -07:00
|
|
|
use solana_sdk::{
|
|
|
|
account::{Account, KeyedAccount},
|
|
|
|
account_utils::State,
|
2019-12-06 13:38:49 -08:00
|
|
|
clock::{Epoch, Slot, UnixTimestamp},
|
2019-09-06 10:55:03 -07:00
|
|
|
hash::Hash,
|
|
|
|
instruction::InstructionError,
|
|
|
|
pubkey::Pubkey,
|
2019-12-03 20:44:02 -08:00
|
|
|
rent::Rent,
|
2019-10-08 22:34:26 -07:00
|
|
|
slot_hashes::SlotHash,
|
2019-09-06 10:55:03 -07:00
|
|
|
sysvar::clock::Clock,
|
|
|
|
};
|
2019-11-15 14:16:24 -08:00
|
|
|
use std::collections::{HashSet, VecDeque};
|
2018-12-04 07:45:32 -08:00
|
|
|
|
2019-10-08 22:34:26 -07:00
|
|
|
// Maximum number of votes to keep around, tightly coupled with epoch_schedule::MIN_SLOTS_PER_EPOCH
|
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-06-21 20:43:24 -07:00
|
|
|
// Maximum number of credits history to keep around
|
|
|
|
// smaller numbers makes
|
|
|
|
pub const MAX_EPOCH_CREDITS_HISTORY: usize = 64;
|
|
|
|
|
2019-12-14 11:23:02 -08:00
|
|
|
// Frequency of timestamp Votes. In v0.22.0, this is approximately 30min with cluster clock
|
2019-12-06 13:38:49 -08:00
|
|
|
// defaults, intended to limit block time drift to < 1hr
|
|
|
|
pub const TIMESTAMP_SLOT_INTERVAL: u64 = 4500;
|
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
2019-04-10 17:52:47 -07:00
|
|
|
pub struct Vote {
|
2019-09-02 12:01:09 -07:00
|
|
|
/// A stack of votes starting with the oldest vote
|
|
|
|
pub slots: Vec<Slot>,
|
|
|
|
/// signature of the bank's state at the last slot
|
2019-05-21 21:45:38 -07:00
|
|
|
pub hash: Hash,
|
2019-12-06 13:38:49 -08:00
|
|
|
/// processing timestamp of last slot
|
|
|
|
pub timestamp: Option<UnixTimestamp>,
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vote {
|
2019-09-02 12:01:09 -07:00
|
|
|
pub fn new(slots: Vec<Slot>, hash: Hash) -> Self {
|
2019-12-06 13:38:49 -08:00
|
|
|
Self {
|
|
|
|
slots,
|
|
|
|
hash,
|
|
|
|
timestamp: None,
|
|
|
|
}
|
2019-04-10 17:52:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 21:19:31 -08:00
|
|
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct Lockout {
|
2019-06-21 20:43:24 -07:00
|
|
|
pub slot: Slot,
|
2019-02-26 21:19:31 -08:00
|
|
|
pub confirmation_count: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lockout {
|
2019-09-02 12:01:09 -07:00
|
|
|
pub fn new(slot: Slot) -> Self {
|
2019-02-26 21:19:31 -08:00
|
|
|
Self {
|
2019-09-02 12:01:09 -07:00
|
|
|
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-06-21 20:43:24 -07:00
|
|
|
pub fn expiration_slot(&self) -> Slot {
|
2019-03-05 14:18:29 -08:00
|
|
|
self.slot + self.lockout()
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
2019-06-21 20:43:24 -07:00
|
|
|
pub fn is_expired(&self, slot: Slot) -> bool {
|
2019-03-18 12:12:33 -07:00
|
|
|
self.expiration_slot() < slot
|
|
|
|
}
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
#[derive(Default, Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub struct VoteInit {
|
|
|
|
pub node_pubkey: Pubkey,
|
|
|
|
pub authorized_voter: Pubkey,
|
|
|
|
pub authorized_withdrawer: Pubkey,
|
|
|
|
pub commission: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub enum VoteAuthorize {
|
|
|
|
Voter,
|
|
|
|
Withdrawer,
|
|
|
|
}
|
|
|
|
|
2019-12-06 13:38:49 -08:00
|
|
|
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
|
|
|
pub struct BlockTimestamp {
|
|
|
|
pub slot: Slot,
|
|
|
|
pub timestamp: UnixTimestamp,
|
|
|
|
}
|
|
|
|
|
2019-12-30 19:57:53 -08:00
|
|
|
// this is how many epochs a voter can be remembered for slashing
|
|
|
|
const MAX_ITEMS: usize = 32;
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
|
|
|
pub struct CircBuf<I> {
|
|
|
|
pub buf: [I; MAX_ITEMS],
|
|
|
|
/// next pointer
|
|
|
|
pub idx: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Default + Copy> Default for CircBuf<I> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
buf: [I::default(); MAX_ITEMS],
|
|
|
|
idx: MAX_ITEMS - 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I> CircBuf<I> {
|
|
|
|
pub fn append(&mut self, item: I) {
|
|
|
|
// remember prior delegate and when we switched, to support later slashing
|
|
|
|
self.idx += 1;
|
|
|
|
self.idx %= MAX_ITEMS;
|
|
|
|
|
|
|
|
self.buf[self.idx] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-25 13:53:49 -07:00
|
|
|
/// the node that votes in this account
|
2019-05-23 23:20:04 -07:00
|
|
|
pub node_pubkey: Pubkey,
|
2019-12-30 19:57:53 -08:00
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
/// the signer for vote transactions
|
|
|
|
pub authorized_voter: Pubkey,
|
2019-12-30 19:57:53 -08:00
|
|
|
/// when the authorized voter was set/initialized
|
|
|
|
pub authorized_voter_epoch: Epoch,
|
|
|
|
|
|
|
|
/// history of prior authorized voters and the epoch ranges for which
|
|
|
|
/// they were set
|
|
|
|
pub prior_voters: CircBuf<(Pubkey, Epoch, Epoch, Slot)>,
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
/// the signer for withdrawals
|
|
|
|
pub authorized_withdrawer: Pubkey,
|
2019-12-03 20:55:04 -08:00
|
|
|
/// percentage (0-100) that represents what part of a rewards
|
2019-04-07 21:45:28 -07:00
|
|
|
/// payout should be given to this VoteAccount
|
2019-07-02 14:18:11 -07:00
|
|
|
pub commission: u8,
|
2019-09-25 13:53:49 -07:00
|
|
|
|
|
|
|
pub votes: VecDeque<Lockout>,
|
2019-02-26 21:19:31 -08:00
|
|
|
pub root_slot: Option<u64>,
|
2019-06-21 20:43:24 -07:00
|
|
|
|
|
|
|
/// history of how many credits earned by the end of each epoch
|
|
|
|
/// each tuple is (Epoch, credits, prev_credits)
|
|
|
|
epoch_credits: Vec<(Epoch, u64, u64)>,
|
2019-12-06 13:38:49 -08:00
|
|
|
|
|
|
|
/// most recent timestamp submitted with a vote
|
|
|
|
pub last_timestamp: BlockTimestamp,
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:50:11 -08:00
|
|
|
impl VoteState {
|
2019-12-30 19:57:53 -08:00
|
|
|
pub fn new(vote_init: &VoteInit, clock: &Clock) -> Self {
|
2019-02-01 15:35:50 -08:00
|
|
|
Self {
|
2019-09-25 13:53:49 -07:00
|
|
|
node_pubkey: vote_init.node_pubkey,
|
|
|
|
authorized_voter: vote_init.authorized_voter,
|
2019-12-30 19:57:53 -08:00
|
|
|
authorized_voter_epoch: clock.epoch,
|
2019-09-25 13:53:49 -07:00
|
|
|
authorized_withdrawer: vote_init.authorized_withdrawer,
|
|
|
|
commission: vote_init.commission,
|
2019-06-21 20:43:24 -07:00
|
|
|
..VoteState::default()
|
2019-02-01 15:35:50 -08:00
|
|
|
}
|
2019-01-21 15:45:30 -08:00
|
|
|
}
|
|
|
|
|
2019-12-03 20:44:02 -08:00
|
|
|
pub fn get_rent_exempt_reserve(rent: &Rent) -> u64 {
|
|
|
|
rent.minimum_balance(VoteState::size_of())
|
|
|
|
}
|
|
|
|
|
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);
|
2019-06-21 20:43:24 -07:00
|
|
|
vote_state.epoch_credits = vec![(0, 0, 0); MAX_EPOCH_CREDITS_HISTORY];
|
2019-03-02 13:51:26 -08:00
|
|
|
serialized_size(&vote_state).unwrap() as usize
|
|
|
|
}
|
|
|
|
|
2019-05-20 22:21:13 -07:00
|
|
|
// utility function, used by Stakes, tests
|
|
|
|
pub fn from(account: &Account) -> Option<VoteState> {
|
2019-06-14 11:38:37 -07:00
|
|
|
Self::deserialize(&account.data).ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
// utility function, used by Stakes, tests
|
|
|
|
pub fn to(&self, account: &mut Account) -> Option<()> {
|
|
|
|
Self::serialize(self, &mut account.data).ok()
|
2019-05-20 22:21:13 -07:00
|
|
|
}
|
|
|
|
|
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-06-14 11:38:37 -07:00
|
|
|
// utility function, used by Stakes, tests
|
|
|
|
pub fn credits_from(account: &Account) -> Option<u64> {
|
|
|
|
Self::from(account).map(|state| state.credits())
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:11:08 -07:00
|
|
|
/// returns commission split as (voter_portion, staker_portion, was_split) tuple
|
2019-04-07 21:45:28 -07:00
|
|
|
///
|
|
|
|
/// if commission calculation is 100% one way or other,
|
2019-04-30 15:11:08 -07:00
|
|
|
/// indicate with false for was_split
|
2019-04-07 21:45:28 -07:00
|
|
|
pub fn commission_split(&self, on: f64) -> (f64, f64, bool) {
|
2019-12-03 20:55:04 -08:00
|
|
|
match self.commission.min(100) {
|
2019-04-07 21:45:28 -07:00
|
|
|
0 => (0.0, on, false),
|
2019-12-03 20:55:04 -08:00
|
|
|
100 => (on, 0.0, false),
|
2019-04-07 21:45:28 -07:00
|
|
|
split => {
|
2019-12-03 20:55:04 -08:00
|
|
|
let mine = on * f64::from(split) / f64::from(100);
|
2019-04-07 21:45:28 -07:00
|
|
|
(mine, on - mine, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 10:55:03 -07:00
|
|
|
fn check_slots_are_valid(
|
|
|
|
&self,
|
|
|
|
vote: &Vote,
|
|
|
|
slot_hashes: &[(Slot, Hash)],
|
|
|
|
) -> Result<(), VoteError> {
|
|
|
|
let mut i = 0; // index into the vote's slots
|
|
|
|
let mut j = slot_hashes.len(); // index into the slot_hashes
|
2019-09-02 12:01:09 -07:00
|
|
|
while i < vote.slots.len() && j > 0 {
|
2019-09-06 10:55:03 -07:00
|
|
|
// find the most recent "new" slot in the vote
|
2019-09-02 12:01:09 -07:00
|
|
|
if self
|
|
|
|
.votes
|
|
|
|
.back()
|
|
|
|
.map_or(false, |old_vote| old_vote.slot >= vote.slots[i])
|
|
|
|
{
|
|
|
|
i += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if vote.slots[i] != slot_hashes[j - 1].0 {
|
|
|
|
j -= 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
j -= 1;
|
|
|
|
}
|
|
|
|
if j == slot_hashes.len() {
|
2019-11-04 16:19:54 -08:00
|
|
|
debug!(
|
2019-09-06 10:55:03 -07:00
|
|
|
"{} dropped vote {:?} too old: {:?} ",
|
2019-09-02 12:01:09 -07:00
|
|
|
self.node_pubkey, vote, slot_hashes
|
|
|
|
);
|
2019-09-06 10:55:03 -07:00
|
|
|
return Err(VoteError::VoteTooOld);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
if i != vote.slots.len() {
|
|
|
|
warn!(
|
|
|
|
"{} dropped vote {:?} failed to match slot: {:?}",
|
|
|
|
self.node_pubkey, vote, slot_hashes,
|
|
|
|
);
|
2019-09-06 10:55:03 -07:00
|
|
|
return Err(VoteError::SlotsMismatch);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
if slot_hashes[j].1 != vote.hash {
|
|
|
|
warn!(
|
|
|
|
"{} dropped vote {:?} failed to match hash {} {}",
|
|
|
|
self.node_pubkey, vote, vote.hash, slot_hashes[j].1
|
|
|
|
);
|
2019-09-06 10:55:03 -07:00
|
|
|
return Err(VoteError::SlotHashMismatch);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
2019-09-06 10:55:03 -07:00
|
|
|
Ok(())
|
2019-04-11 14:48:36 -07:00
|
|
|
}
|
2019-09-06 10:55:03 -07:00
|
|
|
pub fn process_vote(
|
|
|
|
&mut self,
|
|
|
|
vote: &Vote,
|
2019-09-08 11:13:59 -07:00
|
|
|
slot_hashes: &[SlotHash],
|
2019-09-06 10:55:03 -07:00
|
|
|
epoch: Epoch,
|
|
|
|
) -> Result<(), VoteError> {
|
2019-09-02 12:01:09 -07:00
|
|
|
if vote.slots.is_empty() {
|
2019-09-06 10:55:03 -07:00
|
|
|
return Err(VoteError::EmptySlots);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
2019-09-06 10:55:03 -07:00
|
|
|
self.check_slots_are_valid(vote, slot_hashes)?;
|
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
vote.slots.iter().for_each(|s| self.process_slot(*s, epoch));
|
2019-09-06 10:55:03 -07:00
|
|
|
Ok(())
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_slot(&mut self, slot: Slot, epoch: Epoch) {
|
2019-02-26 21:19:31 -08:00
|
|
|
// Ignore votes for slots earlier than we already have votes for
|
|
|
|
if self
|
|
|
|
.votes
|
|
|
|
.back()
|
2019-09-02 12:01:09 -07:00
|
|
|
.map_or(false, |old_vote| old_vote.slot >= slot)
|
2019-02-26 21:19:31 -08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
let vote = Lockout::new(slot);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
self.pop_expired_votes(slot);
|
2019-06-21 20:43:24 -07:00
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
// Once the stack is full, pop the oldest lockout and distribute rewards
|
2019-02-26 21:19:31 -08:00
|
|
|
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-06-21 20:43:24 -07:00
|
|
|
|
|
|
|
self.increment_credits(epoch);
|
2019-02-05 09:25:59 -08:00
|
|
|
}
|
|
|
|
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-06-21 20:43:24 -07:00
|
|
|
/// increment credits, record credits for last epoch if new epoch
|
|
|
|
pub fn increment_credits(&mut self, epoch: Epoch) {
|
2020-01-21 19:08:40 -08:00
|
|
|
// increment credits, record by epoch
|
|
|
|
|
|
|
|
// never seen a credit
|
|
|
|
if self.epoch_credits.is_empty() {
|
|
|
|
self.epoch_credits.push((epoch, 0, 0));
|
|
|
|
} else if epoch != self.epoch_credits.last().unwrap().0 {
|
|
|
|
let (_, credits, prev_credits) = *self.epoch_credits.last().unwrap();
|
|
|
|
|
|
|
|
if credits != prev_credits {
|
|
|
|
// if credits were earned previous epoch
|
|
|
|
// append entry at end of list for the new epoch
|
|
|
|
self.epoch_credits.push((epoch, credits, credits));
|
|
|
|
} else {
|
|
|
|
// else just move the current epoch
|
|
|
|
self.epoch_credits.last_mut().unwrap().0 = epoch;
|
2019-09-26 20:57:35 -07:00
|
|
|
}
|
2020-01-21 19:08:40 -08:00
|
|
|
|
2019-06-21 20:43:24 -07:00
|
|
|
// if stakers do not claim before the epoch goes away they lose the
|
|
|
|
// credits...
|
|
|
|
if self.epoch_credits.len() > MAX_EPOCH_CREDITS_HISTORY {
|
|
|
|
self.epoch_credits.remove(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 19:08:40 -08:00
|
|
|
self.epoch_credits.last_mut().unwrap().1 += 1;
|
2019-06-21 20:43:24 -07:00
|
|
|
}
|
|
|
|
|
2019-07-13 00:24:15 -07:00
|
|
|
/// "unchecked" functions used by tests and Tower
|
2019-05-21 21:45:38 -07:00
|
|
|
pub fn process_vote_unchecked(&mut self, vote: &Vote) {
|
2019-09-02 12:01:09 -07:00
|
|
|
let slot_hashes: Vec<_> = vote.slots.iter().rev().map(|x| (*x, vote.hash)).collect();
|
2020-01-21 19:08:40 -08:00
|
|
|
let _ignored = self.process_vote(vote, &slot_hashes, self.current_epoch());
|
2019-05-21 21:45:38 -07:00
|
|
|
}
|
2019-06-21 20:43:24 -07:00
|
|
|
pub fn process_slot_vote_unchecked(&mut self, slot: Slot) {
|
2019-09-02 12:01:09 -07:00
|
|
|
self.process_vote_unchecked(&Vote::new(vec![slot], Hash::default()));
|
2019-05-21 21:45:38 -07: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 19:08:40 -08:00
|
|
|
fn current_epoch(&self) -> Epoch {
|
|
|
|
if self.epoch_credits.is_empty() {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
self.epoch_credits.last().unwrap().0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-21 19:08:40 -08:00
|
|
|
if self.epoch_credits.is_empty() {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
self.epoch_credits.last().unwrap().1
|
|
|
|
}
|
2019-02-05 09:25:59 -08:00
|
|
|
}
|
|
|
|
|
2019-06-21 20:43:24 -07:00
|
|
|
/// Number of "credits" owed to this account from the mining pool on a per-epoch basis,
|
|
|
|
/// starting from credits observed.
|
2019-09-06 10:55:03 -07:00
|
|
|
/// Each tuple of (Epoch, u64, u64) is read as (epoch, credits, prev_credits), where
|
|
|
|
/// credits for each epoch is credits - prev_credits; while redundant this makes
|
|
|
|
/// calculating rewards over partial epochs nice and simple
|
2019-12-11 22:04:54 -08:00
|
|
|
pub fn epoch_credits(&self) -> &Vec<(Epoch, u64, u64)> {
|
|
|
|
&self.epoch_credits
|
2019-06-21 20:43:24 -07:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:38:30 -07:00
|
|
|
fn pop_expired_votes(&mut self, slot: Slot) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 13:38:49 -08:00
|
|
|
|
|
|
|
pub fn process_timestamp(
|
|
|
|
&mut self,
|
|
|
|
slot: Slot,
|
|
|
|
timestamp: UnixTimestamp,
|
|
|
|
) -> Result<(), VoteError> {
|
|
|
|
if (slot < self.last_timestamp.slot || timestamp < self.last_timestamp.timestamp)
|
|
|
|
|| ((slot == self.last_timestamp.slot || timestamp == self.last_timestamp.timestamp)
|
2020-01-20 16:54:44 -08:00
|
|
|
&& BlockTimestamp { slot, timestamp } != self.last_timestamp
|
|
|
|
&& self.last_timestamp.slot != 0)
|
2019-12-06 13:38:49 -08:00
|
|
|
{
|
|
|
|
return Err(VoteError::TimestampTooOld);
|
|
|
|
}
|
|
|
|
self.last_timestamp = BlockTimestamp { slot, timestamp };
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
/// Authorize the given pubkey to withdraw or sign votes. This may be called multiple times,
|
2019-03-06 13:28:21 -08:00
|
|
|
/// but will implicitly withdraw authorization from the previously authorized
|
2019-09-25 13:53:49 -07:00
|
|
|
/// key
|
|
|
|
pub fn authorize(
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_account: &mut KeyedAccount,
|
2019-09-25 13:53:49 -07:00
|
|
|
authorized: &Pubkey,
|
|
|
|
vote_authorize: VoteAuthorize,
|
2019-11-15 14:16:24 -08:00
|
|
|
signers: &HashSet<Pubkey>,
|
2019-12-30 19:57:53 -08:00
|
|
|
clock: &Clock,
|
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-09-25 13:53:49 -07:00
|
|
|
// current authorized signer must say "yay"
|
|
|
|
match vote_authorize {
|
|
|
|
VoteAuthorize::Voter => {
|
2019-11-15 14:16:24 -08:00
|
|
|
verify_authorized_signer(&vote_state.authorized_voter, signers)?;
|
2019-12-30 19:57:53 -08:00
|
|
|
// only one re-authorization supported per epoch
|
|
|
|
if vote_state.authorized_voter_epoch == clock.epoch {
|
|
|
|
return Err(VoteError::TooSoonToReauthorize.into());
|
|
|
|
}
|
|
|
|
// remember prior
|
|
|
|
vote_state.prior_voters.append((
|
|
|
|
vote_state.authorized_voter,
|
|
|
|
vote_state.authorized_voter_epoch,
|
|
|
|
clock.epoch,
|
|
|
|
clock.slot,
|
|
|
|
));
|
2019-09-25 13:53:49 -07:00
|
|
|
vote_state.authorized_voter = *authorized;
|
2019-12-30 19:57:53 -08:00
|
|
|
vote_state.authorized_voter_epoch = clock.epoch;
|
2019-09-25 13:53:49 -07:00
|
|
|
}
|
|
|
|
VoteAuthorize::Withdrawer => {
|
2019-11-15 14:16:24 -08:00
|
|
|
verify_authorized_signer(&vote_state.authorized_withdrawer, signers)?;
|
2019-09-25 13:53:49 -07:00
|
|
|
vote_state.authorized_withdrawer = *authorized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vote_account.set_state(&vote_state)
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:50:08 -08:00
|
|
|
/// Update the node_pubkey, requires signature of the authorized voter
|
|
|
|
pub fn update_node(
|
|
|
|
vote_account: &mut KeyedAccount,
|
|
|
|
node_pubkey: &Pubkey,
|
|
|
|
signers: &HashSet<Pubkey>,
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
let mut vote_state: VoteState = vote_account.state()?;
|
|
|
|
|
|
|
|
// current authorized voter must say "yay"
|
|
|
|
verify_authorized_signer(&vote_state.authorized_voter, signers)?;
|
|
|
|
|
|
|
|
vote_state.node_pubkey = *node_pubkey;
|
|
|
|
|
|
|
|
vote_account.set_state(&vote_state)
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
fn verify_authorized_signer(
|
|
|
|
authorized: &Pubkey,
|
2019-11-15 14:16:24 -08:00
|
|
|
signers: &HashSet<Pubkey>,
|
2019-09-25 13:53:49 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2019-11-15 14:16:24 -08:00
|
|
|
if signers.contains(authorized) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::MissingRequiredSignature)
|
2019-03-06 13:28:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:20:47 -07:00
|
|
|
/// Withdraw funds from the vote account
|
|
|
|
pub fn withdraw(
|
|
|
|
vote_account: &mut KeyedAccount,
|
|
|
|
lamports: u64,
|
|
|
|
to_account: &mut KeyedAccount,
|
2019-11-15 14:16:24 -08:00
|
|
|
signers: &HashSet<Pubkey>,
|
2019-07-25 23:20:47 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2019-09-25 13:53:49 -07:00
|
|
|
let vote_state: VoteState = vote_account.state()?;
|
|
|
|
|
2019-11-15 14:16:24 -08:00
|
|
|
verify_authorized_signer(&vote_state.authorized_withdrawer, signers)?;
|
2019-09-25 13:53:49 -07:00
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
if vote_account.lamports()? < lamports {
|
2019-07-25 23:20:47 -07:00
|
|
|
return Err(InstructionError::InsufficientFunds);
|
|
|
|
}
|
2020-01-22 09:11:56 -08:00
|
|
|
vote_account.try_account_ref_mut()?.lamports -= lamports;
|
|
|
|
to_account.try_account_ref_mut()?.lamports += lamports;
|
2019-07-25 23:20:47 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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,
|
2019-09-25 13:53:49 -07:00
|
|
|
vote_init: &VoteInit,
|
2019-12-30 19:57:53 -08:00
|
|
|
clock: &Clock,
|
2019-04-10 17:52:47 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
let vote_state: VoteState = vote_account.state()?;
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
if vote_state.authorized_voter != Pubkey::default() {
|
2019-04-10 17:52:47 -07:00
|
|
|
return Err(InstructionError::AccountAlreadyInitialized);
|
|
|
|
}
|
2019-12-30 19:57:53 -08:00
|
|
|
vote_account.set_state(&VoteState::new(vote_init, clock))
|
2019-02-15 07:42:09 -08:00
|
|
|
}
|
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
pub fn process_vote(
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_account: &mut KeyedAccount,
|
2019-09-08 11:13:59 -07:00
|
|
|
slot_hashes: &[SlotHash],
|
2019-07-12 16:38:15 -07:00
|
|
|
clock: &Clock,
|
2019-09-02 12:01:09 -07:00
|
|
|
vote: &Vote,
|
2019-11-15 14:16:24 -08:00
|
|
|
signers: &HashSet<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-02-15 07:42:09 -08:00
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
if vote_state.authorized_voter == Pubkey::default() {
|
2019-04-10 17:52:47 -07:00
|
|
|
return Err(InstructionError::UninitializedAccount);
|
2019-03-06 13:28:21 -08:00
|
|
|
}
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-11-15 14:16:24 -08:00
|
|
|
verify_authorized_signer(&vote_state.authorized_voter, signers)?;
|
2019-02-15 12:20:34 -08:00
|
|
|
|
2019-09-06 10:55:03 -07:00
|
|
|
vote_state.process_vote(vote, slot_hashes, clock.epoch)?;
|
2019-12-06 13:38:49 -08:00
|
|
|
if let Some(timestamp) = vote.timestamp {
|
|
|
|
vote.slots
|
|
|
|
.iter()
|
|
|
|
.max()
|
|
|
|
.ok_or_else(|| VoteError::EmptySlots)
|
2019-12-30 19:57:53 -08:00
|
|
|
.and_then(|slot| vote_state.process_timestamp(*slot, timestamp))?;
|
2019-12-06 13:38:49 -08:00
|
|
|
}
|
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-05-23 23:20:04 -07:00
|
|
|
vote_pubkey: &Pubkey,
|
|
|
|
node_pubkey: &Pubkey,
|
2019-07-02 14:18:11 -07:00
|
|
|
commission: u8,
|
2019-04-10 17:52:47 -07:00
|
|
|
lamports: u64,
|
|
|
|
) -> Account {
|
|
|
|
let mut vote_account = Account::new(lamports, VoteState::size_of(), &id());
|
|
|
|
|
2019-12-30 19:57:53 -08:00
|
|
|
VoteState::new(
|
|
|
|
&VoteInit {
|
|
|
|
node_pubkey: *node_pubkey,
|
|
|
|
authorized_voter: *vote_pubkey,
|
|
|
|
authorized_withdrawer: *vote_pubkey,
|
|
|
|
commission,
|
|
|
|
},
|
|
|
|
&Clock::default(),
|
|
|
|
)
|
2019-09-25 13:53:49 -07:00
|
|
|
.to(&mut vote_account)
|
|
|
|
.unwrap();
|
2019-06-21 20:43:24 -07:00
|
|
|
|
2019-04-10 17:52:47 -07:00
|
|
|
vote_account
|
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;
|
2019-11-15 14:16:24 -08:00
|
|
|
use solana_sdk::{
|
|
|
|
account::{get_signers, Account},
|
|
|
|
account_utils::State,
|
|
|
|
hash::hash,
|
|
|
|
instruction_processor_utils::next_keyed_account,
|
|
|
|
};
|
2020-01-22 09:11:56 -08:00
|
|
|
use std::cell::RefCell;
|
2018-12-04 07:45:32 -08:00
|
|
|
|
2019-04-11 14:48:36 -07:00
|
|
|
const MAX_RECENT_VOTES: usize = 16;
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
impl VoteState {
|
|
|
|
pub fn new_for_test(auth_pubkey: &Pubkey) -> Self {
|
2019-12-30 19:57:53 -08:00
|
|
|
Self::new(
|
|
|
|
&VoteInit {
|
|
|
|
node_pubkey: Pubkey::new_rand(),
|
|
|
|
authorized_voter: *auth_pubkey,
|
|
|
|
authorized_withdrawer: *auth_pubkey,
|
|
|
|
commission: 0,
|
|
|
|
},
|
|
|
|
&Clock::default(),
|
|
|
|
)
|
2019-09-25 13:53:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:08:45 -08:00
|
|
|
#[test]
|
2019-03-04 17:50:19 -08:00
|
|
|
fn test_initialize_vote_account() {
|
2019-05-23 23:20:04 -07:00
|
|
|
let vote_account_pubkey = Pubkey::new_rand();
|
2020-01-22 09:11:56 -08:00
|
|
|
let mut vote_account = Account::new_ref(100, VoteState::size_of(), &id());
|
2019-02-28 17:08:45 -08:00
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let node_pubkey = Pubkey::new_rand();
|
2019-02-28 17:08:45 -08:00
|
|
|
|
|
|
|
//init should pass
|
2019-05-23 23:20:04 -07:00
|
|
|
let mut vote_account = KeyedAccount::new(&vote_account_pubkey, false, &mut vote_account);
|
2019-09-25 13:53:49 -07:00
|
|
|
let res = initialize_account(
|
|
|
|
&mut vote_account,
|
|
|
|
&VoteInit {
|
|
|
|
node_pubkey,
|
|
|
|
authorized_voter: vote_account_pubkey,
|
|
|
|
authorized_withdrawer: vote_account_pubkey,
|
|
|
|
commission: 0,
|
|
|
|
},
|
2019-12-30 19:57:53 -08:00
|
|
|
&Clock::default(),
|
2019-09-25 13:53:49 -07:00
|
|
|
);
|
2019-02-28 17:08:45 -08:00
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// reinit should fail
|
2019-09-25 13:53:49 -07:00
|
|
|
let res = initialize_account(
|
|
|
|
&mut vote_account,
|
|
|
|
&VoteInit {
|
|
|
|
node_pubkey,
|
|
|
|
authorized_voter: vote_account_pubkey,
|
|
|
|
authorized_withdrawer: vote_account_pubkey,
|
|
|
|
commission: 0,
|
|
|
|
},
|
2019-12-30 19:57:53 -08:00
|
|
|
&Clock::default(),
|
2019-09-25 13:53:49 -07:00
|
|
|
);
|
2019-04-10 17:52:47 -07:00
|
|
|
assert_eq!(res, Err(InstructionError::AccountAlreadyInitialized));
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
fn create_test_account() -> (Pubkey, RefCell<Account>) {
|
2019-05-23 23:20:04 -07:00
|
|
|
let vote_pubkey = Pubkey::new_rand();
|
2019-04-10 17:52:47 -07:00
|
|
|
(
|
2019-05-23 23:20:04 -07:00
|
|
|
vote_pubkey,
|
2020-01-22 09:11:56 -08:00
|
|
|
RefCell::new(vote_state::create_account(
|
|
|
|
&vote_pubkey,
|
|
|
|
&Pubkey::new_rand(),
|
|
|
|
0,
|
|
|
|
100,
|
|
|
|
)),
|
2019-04-10 17:52:47 -07:00
|
|
|
)
|
2019-02-28 17:08:45 -08:00
|
|
|
}
|
|
|
|
|
2019-05-21 21:45:38 -07:00
|
|
|
fn simulate_process_vote(
|
2019-05-23 23:20:04 -07:00
|
|
|
vote_pubkey: &Pubkey,
|
2020-01-22 09:11:56 -08:00
|
|
|
vote_account: &mut RefCell<Account>,
|
2019-05-21 21:45:38 -07:00
|
|
|
vote: &Vote,
|
2019-09-08 11:13:59 -07:00
|
|
|
slot_hashes: &[SlotHash],
|
2019-11-02 00:38:30 -07:00
|
|
|
epoch: Epoch,
|
2019-05-21 21:45:38 -07:00
|
|
|
) -> Result<VoteState, InstructionError> {
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-09-02 12:01:09 -07:00
|
|
|
process_vote(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-06-21 20:43:24 -07:00
|
|
|
slot_hashes,
|
2019-07-12 16:38:15 -07:00
|
|
|
&Clock {
|
2019-06-21 20:43:24 -07:00
|
|
|
epoch,
|
2019-07-12 16:38:15 -07:00
|
|
|
..Clock::default()
|
2019-06-21 20:43:24 -07:00
|
|
|
},
|
2019-09-02 12:01:09 -07:00
|
|
|
&vote.clone(),
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-05-21 21:45:38 -07:00
|
|
|
)?;
|
2020-01-22 09:11:56 -08:00
|
|
|
vote_account.borrow().state()
|
2019-05-21 21:45:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// exercises all the keyed accounts stuff
|
|
|
|
fn simulate_process_vote_unchecked(
|
2019-05-23 23:20:04 -07:00
|
|
|
vote_pubkey: &Pubkey,
|
2020-01-22 09:11:56 -08:00
|
|
|
vote_account: &mut RefCell<Account>,
|
2019-05-21 21:45:38 -07:00
|
|
|
vote: &Vote,
|
|
|
|
) -> Result<VoteState, InstructionError> {
|
2019-06-21 20:43:24 -07:00
|
|
|
simulate_process_vote(
|
|
|
|
vote_pubkey,
|
|
|
|
vote_account,
|
|
|
|
vote,
|
2019-09-02 12:01:09 -07:00
|
|
|
&[(*vote.slots.last().unwrap(), vote.hash)],
|
2019-06-21 20:43:24 -07:00
|
|
|
0,
|
|
|
|
)
|
2019-05-21 21:45:38 -07: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-05-23 23:20:04 -07:00
|
|
|
let (vote_pubkey, vote_account) = create_test_account();
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
let vote_state: VoteState = vote_account.borrow().state().unwrap();
|
2019-09-25 13:53:49 -07:00
|
|
|
assert_eq!(vote_state.authorized_voter, vote_pubkey);
|
2019-02-15 07:42:09 -08:00
|
|
|
assert!(vote_state.votes.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote() {
|
2019-05-23 23:20:04 -07:00
|
|
|
let (vote_pubkey, mut vote_account) = create_test_account();
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
let vote = Vote::new(vec![1], Hash::default());
|
2019-05-21 21:45:38 -07:00
|
|
|
let vote_state =
|
2019-05-23 23:20:04 -07:00
|
|
|
simulate_process_vote_unchecked(&vote_pubkey, &mut vote_account, &vote).unwrap();
|
2019-09-02 12:01:09 -07:00
|
|
|
assert_eq!(
|
|
|
|
vote_state.votes,
|
|
|
|
vec![Lockout::new(*vote.slots.last().unwrap())]
|
|
|
|
);
|
2019-02-15 07:42:09 -08:00
|
|
|
assert_eq!(vote_state.credits(), 0);
|
|
|
|
}
|
|
|
|
|
2019-05-21 21:45:38 -07:00
|
|
|
#[test]
|
|
|
|
fn test_vote_slot_hashes() {
|
2019-05-23 23:20:04 -07:00
|
|
|
let (vote_pubkey, mut vote_account) = create_test_account();
|
2019-05-21 21:45:38 -07:00
|
|
|
|
|
|
|
let hash = hash(&[0u8]);
|
2019-09-02 12:01:09 -07:00
|
|
|
let vote = Vote::new(vec![0], hash);
|
2019-05-21 21:45:38 -07:00
|
|
|
|
|
|
|
// wrong hash
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
simulate_process_vote(
|
|
|
|
&vote_pubkey,
|
|
|
|
&mut vote_account,
|
|
|
|
&vote,
|
|
|
|
&[(0, Hash::default())],
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
Err(VoteError::SlotHashMismatch.into())
|
|
|
|
);
|
2019-05-21 21:45:38 -07:00
|
|
|
|
|
|
|
// wrong slot
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
simulate_process_vote(&vote_pubkey, &mut vote_account, &vote, &[(1, hash)], 0),
|
|
|
|
Err(VoteError::SlotsMismatch.into())
|
|
|
|
);
|
2019-05-21 21:45:38 -07:00
|
|
|
|
|
|
|
// empty slot_hashes
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
simulate_process_vote(&vote_pubkey, &mut vote_account, &vote, &[], 0),
|
|
|
|
Err(VoteError::VoteTooOld.into())
|
|
|
|
);
|
2019-05-21 21:45:38 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 22:50:08 -08:00
|
|
|
#[test]
|
|
|
|
fn test_vote_update_node_id() {
|
|
|
|
let (vote_pubkey, mut vote_account) = create_test_account();
|
|
|
|
|
|
|
|
let node_pubkey = Pubkey::new_rand();
|
|
|
|
|
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, false, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
|
|
|
let res = update_node(&mut keyed_accounts[0], &node_pubkey, &signers);
|
|
|
|
assert_eq!(res, Err(InstructionError::MissingRequiredSignature));
|
2020-01-22 09:11:56 -08:00
|
|
|
let vote_state: VoteState = vote_account.borrow().state().unwrap();
|
2019-12-03 22:50:08 -08:00
|
|
|
assert!(vote_state.node_pubkey != node_pubkey);
|
|
|
|
|
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
|
|
|
let res = update_node(&mut keyed_accounts[0], &node_pubkey, &signers);
|
|
|
|
assert_eq!(res, Ok(()));
|
2020-01-22 09:11:56 -08:00
|
|
|
let vote_state: VoteState = vote_account.borrow().state().unwrap();
|
2019-12-03 22:50:08 -08:00
|
|
|
assert_eq!(vote_state.node_pubkey, node_pubkey);
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:21:16 -08:00
|
|
|
#[test]
|
|
|
|
fn test_vote_signature() {
|
2019-05-23 23:20:04 -07:00
|
|
|
let (vote_pubkey, mut vote_account) = create_test_account();
|
2019-09-02 12:01:09 -07:00
|
|
|
let vote = Vote::new(vec![1], Hash::default());
|
2019-04-10 17:52:47 -07:00
|
|
|
|
|
|
|
// unsigned
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, false, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-09-02 12:01:09 -07:00
|
|
|
let res = process_vote(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-09-02 12:01:09 -07:00
|
|
|
&[(*vote.slots.last().unwrap(), vote.hash)],
|
2019-07-12 16:38:15 -07:00
|
|
|
&Clock::default(),
|
2019-09-02 12:01:09 -07:00
|
|
|
&vote,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-04-10 17:52:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Err(InstructionError::MissingRequiredSignature));
|
|
|
|
|
2019-11-15 14:16:24 -08:00
|
|
|
// signed
|
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-09-02 12:01:09 -07:00
|
|
|
let res = process_vote(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-09-02 12:01:09 -07:00
|
|
|
&[(*vote.slots.last().unwrap(), vote.hash)],
|
2019-07-12 16:38:15 -07:00
|
|
|
&Clock::default(),
|
2019-09-02 12:01:09 -07:00
|
|
|
&vote,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-04-10 17:52:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
2019-11-15 14:16:24 -08:00
|
|
|
// another voter, unsigned
|
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, false, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-05-23 23:20:04 -07:00
|
|
|
let authorized_voter_pubkey = Pubkey::new_rand();
|
2019-09-25 13:53:49 -07:00
|
|
|
let res = authorize(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-05-23 23:20:04 -07:00
|
|
|
&authorized_voter_pubkey,
|
2019-09-25 13:53:49 -07:00
|
|
|
VoteAuthorize::Voter,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-12-30 19:57:53 -08:00
|
|
|
&Clock {
|
|
|
|
epoch: 1,
|
|
|
|
..Clock::default()
|
|
|
|
},
|
2019-04-10 17:52:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Err(InstructionError::MissingRequiredSignature));
|
|
|
|
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-09-25 13:53:49 -07:00
|
|
|
let res = authorize(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-05-23 23:20:04 -07:00
|
|
|
&authorized_voter_pubkey,
|
2019-09-25 13:53:49 -07:00
|
|
|
VoteAuthorize::Voter,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-12-30 19:57:53 -08:00
|
|
|
&Clock::default(),
|
|
|
|
);
|
|
|
|
assert_eq!(res, Err(VoteError::TooSoonToReauthorize.into()));
|
|
|
|
|
|
|
|
let res = authorize(
|
|
|
|
&mut keyed_accounts[0],
|
|
|
|
&authorized_voter_pubkey,
|
|
|
|
VoteAuthorize::Voter,
|
|
|
|
&signers,
|
|
|
|
&Clock {
|
|
|
|
epoch: 1,
|
|
|
|
..Clock::default()
|
|
|
|
},
|
2019-04-10 17:52:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
2019-11-15 14:16:24 -08:00
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
// verify authorized_voter_pubkey can authorize authorized_voter_pubkey ;)
|
2020-01-22 09:11:56 -08:00
|
|
|
let mut authorized_voter_account = RefCell::new(Account::default());
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [
|
|
|
|
KeyedAccount::new(&vote_pubkey, false, &mut vote_account),
|
|
|
|
KeyedAccount::new(
|
2019-05-23 23:20:04 -07:00
|
|
|
&authorized_voter_pubkey,
|
2019-04-10 17:52:47 -07:00
|
|
|
true,
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut authorized_voter_account,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
|
|
|
let res = authorize(
|
|
|
|
&mut keyed_accounts[0],
|
2019-05-23 23:20:04 -07:00
|
|
|
&authorized_voter_pubkey,
|
2019-09-25 13:53:49 -07:00
|
|
|
VoteAuthorize::Voter,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-12-30 19:57:53 -08:00
|
|
|
&Clock::default(),
|
2019-09-25 13:53:49 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// authorize another withdrawer
|
|
|
|
// another voter
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-09-25 13:53:49 -07:00
|
|
|
let authorized_withdrawer_pubkey = Pubkey::new_rand();
|
|
|
|
let res = authorize(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-09-25 13:53:49 -07:00
|
|
|
&authorized_withdrawer_pubkey,
|
|
|
|
VoteAuthorize::Withdrawer,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-12-30 19:57:53 -08:00
|
|
|
&Clock::default(),
|
2019-09-25 13:53:49 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// verify authorized_withdrawer can authorize authorized_withdrawer ;)
|
2020-01-22 09:11:56 -08:00
|
|
|
let mut withdrawer_account = RefCell::new(Account::default());
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [
|
|
|
|
KeyedAccount::new(&vote_pubkey, false, &mut vote_account),
|
|
|
|
KeyedAccount::new(&authorized_withdrawer_pubkey, true, &mut withdrawer_account),
|
|
|
|
];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-09-25 13:53:49 -07:00
|
|
|
let res = authorize(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-09-25 13:53:49 -07:00
|
|
|
&authorized_withdrawer_pubkey,
|
|
|
|
VoteAuthorize::Withdrawer,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-12-30 19:57:53 -08:00
|
|
|
&Clock::default(),
|
2019-04-10 17:52:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// not signed by authorized voter
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-09-02 12:01:09 -07:00
|
|
|
let vote = Vote::new(vec![2], Hash::default());
|
|
|
|
let res = process_vote(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-09-02 12:01:09 -07:00
|
|
|
&[(*vote.slots.last().unwrap(), vote.hash)],
|
2019-07-12 16:38:15 -07:00
|
|
|
&Clock::default(),
|
2019-09-02 12:01:09 -07:00
|
|
|
&vote,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-04-10 17:52:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Err(InstructionError::MissingRequiredSignature));
|
|
|
|
|
|
|
|
// signed by authorized voter
|
2020-01-22 09:11:56 -08:00
|
|
|
let mut authorized_voter_account = RefCell::new(Account::default());
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [
|
|
|
|
KeyedAccount::new(&vote_pubkey, false, &mut vote_account),
|
|
|
|
KeyedAccount::new(
|
|
|
|
&authorized_voter_pubkey,
|
|
|
|
true,
|
|
|
|
&mut authorized_voter_account,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-09-02 12:01:09 -07:00
|
|
|
let vote = Vote::new(vec![2], Hash::default());
|
|
|
|
let res = process_vote(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-09-02 12:01:09 -07:00
|
|
|
&[(*vote.slots.last().unwrap(), vote.hash)],
|
2019-07-12 16:38:15 -07:00
|
|
|
&Clock::default(),
|
2019-09-02 12:01:09 -07:00
|
|
|
&vote,
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-04-10 17:52:47 -07:00
|
|
|
);
|
|
|
|
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-05-23 23:20:04 -07:00
|
|
|
let vote_pubkey = Pubkey::new_rand();
|
2020-01-22 09:11:56 -08:00
|
|
|
let mut vote_account = RefCell::new(Account::new(100, VoteState::size_of(), &id()));
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-05-21 21:45:38 -07:00
|
|
|
let res = simulate_process_vote_unchecked(
|
2019-05-23 23:20:04 -07:00
|
|
|
&vote_pubkey,
|
2019-05-21 21:45:38 -07:00
|
|
|
&mut vote_account,
|
2019-09-02 12:01:09 -07:00
|
|
|
&Vote::new(vec![1], Hash::default()),
|
2019-05-21 21:45:38 -07:00
|
|
|
);
|
2019-04-10 17:52:47 -07:00
|
|
|
assert_eq!(res, Err(InstructionError::UninitializedAccount));
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote_lockout() {
|
2019-05-23 23:20:04 -07:00
|
|
|
let (_vote_pubkey, vote_account) = create_test_account();
|
2019-04-10 17:52:47 -07:00
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
let mut vote_state: VoteState = vote_account.borrow().state().unwrap();
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..(MAX_LOCKOUT_HISTORY + 1) {
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked((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-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(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-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(vote_state.votes.front().unwrap().expiration_slot());
|
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-05-23 23:20:04 -07:00
|
|
|
let voter_pubkey = Pubkey::new_rand();
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::new_for_test(&voter_pubkey);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..3 {
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(i as u64);
|
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-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked((2 + INITIAL_LOCKOUT + 1) as u64);
|
2019-02-26 21:19:31 -08:00
|
|
|
check_lockouts(&vote_state);
|
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
// Vote again, this time the vote stack depth increases, so the votes should
|
2019-02-26 21:19:31 -08:00
|
|
|
// double for everybody
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked((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
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
// Vote again, this time the vote stack depth increases, so the votes should
|
2019-04-05 22:11:40 -07:00
|
|
|
// double for everybody
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked((2 + INITIAL_LOCKOUT + 3) as u64);
|
2019-04-05 22:11:40 -07:00
|
|
|
check_lockouts(&vote_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_expire_multiple_votes() {
|
2019-05-23 23:20:04 -07:00
|
|
|
let voter_pubkey = Pubkey::new_rand();
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::new_for_test(&voter_pubkey);
|
2019-04-05 22:11:40 -07:00
|
|
|
|
|
|
|
for i in 0..3 {
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(i as u64);
|
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-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(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-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(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-05-23 23:20:04 -07:00
|
|
|
let voter_pubkey = Pubkey::new_rand();
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::new_for_test(&voter_pubkey);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..MAX_LOCKOUT_HISTORY {
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(i as u64);
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
|
|
|
|
2020-01-21 19:08:40 -08:00
|
|
|
assert_eq!(vote_state.credits(), 0);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(MAX_LOCKOUT_HISTORY as u64 + 1);
|
2020-01-21 19:08:40 -08:00
|
|
|
assert_eq!(vote_state.credits(), 1);
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(MAX_LOCKOUT_HISTORY as u64 + 2);
|
2019-02-26 21:19:31 -08:00
|
|
|
assert_eq!(vote_state.credits(), 2);
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(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-05-23 23:20:04 -07:00
|
|
|
let voter_pubkey = Pubkey::new_rand();
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::new_for_test(&voter_pubkey);
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(0);
|
|
|
|
vote_state.process_slot_vote_unchecked(1);
|
|
|
|
vote_state.process_slot_vote_unchecked(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-05-23 23:20:04 -07:00
|
|
|
let voter_pubkey = Pubkey::new_rand();
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::new_for_test(&voter_pubkey);
|
2019-03-18 12:12:33 -07:00
|
|
|
for i in 0..MAX_LOCKOUT_HISTORY {
|
2019-05-21 21:45:38 -07:00
|
|
|
vote_state.process_slot_vote_unchecked(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() {
|
2019-09-02 12:01:09 -07:00
|
|
|
let num_votes = vote_state.votes.len() - i;
|
|
|
|
assert_eq!(vote.lockout(), INITIAL_LOCKOUT.pow(num_votes as u32) as u64);
|
2019-02-26 21:19:31 -08:00
|
|
|
}
|
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())
|
2019-09-02 12:01:09 -07:00
|
|
|
.map(|i| Vote::new(vec![vote_state.votes.get(i).unwrap().slot], Hash::default()))
|
2019-04-11 14:48:36 -07:00
|
|
|
.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();
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state_a = VoteState::new_for_test(&account_a);
|
2019-04-11 14:48:36 -07:00
|
|
|
let account_b = Pubkey::new_rand();
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state_b = VoteState::new_for_test(&account_b);
|
2019-04-11 14:48:36 -07:00
|
|
|
|
|
|
|
// process some votes on account a
|
2019-05-21 21:45:38 -07:00
|
|
|
(0..5)
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|i| vote_state_a.process_slot_vote_unchecked(i as u64));
|
2019-04-11 14:48:36 -07:00
|
|
|
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
|
2019-09-02 12:01:09 -07:00
|
|
|
let slots = (0u64..MAX_RECENT_VOTES as u64).into_iter().collect();
|
|
|
|
let vote = Vote::new(slots, Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vote.slots.iter().rev().map(|x| (*x, vote.hash)).collect();
|
2019-05-21 21:45:38 -07:00
|
|
|
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(vote_state_a.process_vote(&vote, &slot_hashes, 0), Ok(()));
|
|
|
|
assert_eq!(vote_state_b.process_vote(&vote, &slot_hashes, 0), Ok(()));
|
2019-04-11 14:48:36 -07:00
|
|
|
assert_eq!(recent_votes(&vote_state_a), recent_votes(&vote_state_b));
|
|
|
|
}
|
2019-04-30 15:11:08 -07:00
|
|
|
|
2019-09-02 12:01:09 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_vote_skips_old_vote() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::default();
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![0], Hash::default());
|
2019-09-06 10:55:03 -07:00
|
|
|
let slot_hashes: Vec<_> = vec![(0, vote.hash)];
|
|
|
|
assert_eq!(vote_state.process_vote(&vote, &slot_hashes, 0), Ok(()));
|
|
|
|
let recent = recent_votes(&vote_state);
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.process_vote(&vote, &slot_hashes, 0),
|
|
|
|
Err(VoteError::VoteTooOld)
|
|
|
|
);
|
|
|
|
assert_eq!(recent, recent_votes(&vote_state));
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_slots_are_valid_vote_empty_slot_hashes() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let vote_state = VoteState::default();
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![0], Hash::default());
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
vote_state.check_slots_are_valid(&vote, &vec![]),
|
|
|
|
Err(VoteError::VoteTooOld)
|
|
|
|
);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_slots_are_valid_new_vote() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let vote_state = VoteState::default();
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![0], Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), vote.hash)];
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
vote_state.check_slots_are_valid(&vote, &slot_hashes),
|
|
|
|
Ok(())
|
|
|
|
);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_slots_are_valid_bad_hash() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let vote_state = VoteState::default();
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![0], Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), hash(vote.hash.as_ref()))];
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
vote_state.check_slots_are_valid(&vote, &slot_hashes),
|
|
|
|
Err(VoteError::SlotHashMismatch)
|
|
|
|
);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_slots_are_valid_bad_slot() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let vote_state = VoteState::default();
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![1], Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vec![(0, vote.hash)];
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
vote_state.check_slots_are_valid(&vote, &slot_hashes),
|
|
|
|
Err(VoteError::SlotsMismatch)
|
|
|
|
);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_slots_are_valid_duplicate_vote() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::default();
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![0], Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), vote.hash)];
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(vote_state.process_vote(&vote, &slot_hashes, 0), Ok(()));
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.check_slots_are_valid(&vote, &slot_hashes),
|
|
|
|
Err(VoteError::VoteTooOld)
|
|
|
|
);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_slots_are_valid_next_vote() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::default();
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![0], Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), vote.hash)];
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(vote_state.process_vote(&vote, &slot_hashes, 0), Ok(()));
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![0, 1], Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vec![(1, vote.hash), (0, vote.hash)];
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
vote_state.check_slots_are_valid(&vote, &slot_hashes),
|
|
|
|
Ok(())
|
|
|
|
);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_slots_are_valid_next_vote_only() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::default();
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![0], Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), vote.hash)];
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(vote_state.process_vote(&vote, &slot_hashes, 0), Ok(()));
|
2019-09-02 12:01:09 -07:00
|
|
|
|
|
|
|
let vote = Vote::new(vec![1], Hash::default());
|
|
|
|
let slot_hashes: Vec<_> = vec![(1, vote.hash), (0, vote.hash)];
|
2019-09-06 10:55:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
vote_state.check_slots_are_valid(&vote, &slot_hashes),
|
|
|
|
Ok(())
|
|
|
|
);
|
2019-09-02 12:01:09 -07:00
|
|
|
}
|
2019-09-25 13:53:49 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_vote_empty_slots() {
|
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
|
|
|
|
let vote = Vote::new(vec![], Hash::default());
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.process_vote(&vote, &[], 0),
|
|
|
|
Err(VoteError::EmptySlots)
|
|
|
|
);
|
|
|
|
}
|
2019-09-02 12:01:09 -07:00
|
|
|
|
2019-04-30 15:11:08 -07:00
|
|
|
#[test]
|
|
|
|
fn test_vote_state_commission_split() {
|
2019-09-25 13:53:49 -07:00
|
|
|
let vote_state = VoteState::default();
|
2019-04-30 15:11:08 -07:00
|
|
|
|
|
|
|
assert_eq!(vote_state.commission_split(1.0), (0.0, 1.0, false));
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
vote_state.commission = std::u8::MAX;
|
2019-04-30 15:11:08 -07:00
|
|
|
assert_eq!(vote_state.commission_split(1.0), (1.0, 0.0, false));
|
|
|
|
|
2019-12-03 20:55:04 -08:00
|
|
|
vote_state.commission = 50;
|
2019-04-30 15:11:08 -07:00
|
|
|
let (voter_portion, staker_portion, was_split) = vote_state.commission_split(10.0);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
(voter_portion.round(), staker_portion.round(), was_split),
|
|
|
|
(5.0, 5.0, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:20:47 -07:00
|
|
|
#[test]
|
|
|
|
fn test_vote_state_withdraw() {
|
|
|
|
let (vote_pubkey, mut vote_account) = create_test_account();
|
|
|
|
|
2019-09-25 13:53:49 -07:00
|
|
|
// unsigned request
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, false, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-07-25 23:20:47 -07:00
|
|
|
let res = withdraw(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-07-25 23:20:47 -07:00
|
|
|
0,
|
2020-01-22 09:11:56 -08:00
|
|
|
&mut KeyedAccount::new(
|
|
|
|
&Pubkey::new_rand(),
|
|
|
|
false,
|
|
|
|
&mut RefCell::new(Account::default()),
|
|
|
|
),
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-07-25 23:20:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Err(InstructionError::MissingRequiredSignature));
|
|
|
|
|
|
|
|
// insufficient funds
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-07-25 23:20:47 -07:00
|
|
|
let res = withdraw(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-07-25 23:20:47 -07:00
|
|
|
101,
|
2020-01-22 09:11:56 -08:00
|
|
|
&mut KeyedAccount::new(
|
|
|
|
&Pubkey::new_rand(),
|
|
|
|
false,
|
|
|
|
&mut RefCell::new(Account::default()),
|
|
|
|
),
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-07-25 23:20:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Err(InstructionError::InsufficientFunds));
|
|
|
|
|
|
|
|
// all good
|
2020-01-22 09:11:56 -08:00
|
|
|
let mut to_account = RefCell::new(Account::default());
|
|
|
|
let lamports = vote_account.borrow().lamports;
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-07-25 23:20:47 -07:00
|
|
|
let res = withdraw(
|
2019-11-15 14:16:24 -08:00
|
|
|
&mut keyed_accounts[0],
|
2019-09-25 13:53:49 -07:00
|
|
|
lamports,
|
|
|
|
&mut KeyedAccount::new(&Pubkey::new_rand(), false, &mut to_account),
|
2019-11-15 14:16:24 -08:00
|
|
|
&signers,
|
2019-09-25 13:53:49 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(vote_account.borrow().lamports, 0);
|
|
|
|
assert_eq!(to_account.borrow().lamports, lamports);
|
2019-09-25 13:53:49 -07:00
|
|
|
|
|
|
|
// reset balance, verify that authorized_withdrawer works
|
2020-01-22 09:11:56 -08:00
|
|
|
vote_account.borrow_mut().lamports = lamports;
|
2019-11-15 14:16:24 -08:00
|
|
|
|
|
|
|
// authorize authorized_withdrawer
|
|
|
|
let authorized_withdrawer_pubkey = Pubkey::new_rand();
|
|
|
|
let keyed_accounts = &mut [KeyedAccount::new(&vote_pubkey, true, &mut vote_account)];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
|
|
|
let res = authorize(
|
|
|
|
&mut keyed_accounts[0],
|
|
|
|
&authorized_withdrawer_pubkey,
|
|
|
|
VoteAuthorize::Withdrawer,
|
|
|
|
&signers,
|
2019-12-30 19:57:53 -08:00
|
|
|
&Clock::default(),
|
2019-11-15 14:16:24 -08:00
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// withdraw using authorized_withdrawer to authorized_withdrawer's account
|
2020-01-22 09:11:56 -08:00
|
|
|
let mut withdrawer_account = RefCell::new(Account::default());
|
2019-11-15 14:16:24 -08:00
|
|
|
let keyed_accounts = &mut [
|
|
|
|
KeyedAccount::new(&vote_pubkey, false, &mut vote_account),
|
|
|
|
KeyedAccount::new(&authorized_withdrawer_pubkey, true, &mut withdrawer_account),
|
|
|
|
];
|
|
|
|
let signers = get_signers(keyed_accounts);
|
|
|
|
let keyed_accounts = &mut keyed_accounts.iter_mut();
|
|
|
|
let vote_keyed_account = next_keyed_account(keyed_accounts).unwrap();
|
|
|
|
let withdrawer_keyed_account = next_keyed_account(keyed_accounts).unwrap();
|
2019-09-25 13:53:49 -07:00
|
|
|
let res = withdraw(
|
2019-11-15 14:16:24 -08:00
|
|
|
vote_keyed_account,
|
2019-07-25 23:20:47 -07:00
|
|
|
lamports,
|
2019-11-15 14:16:24 -08:00
|
|
|
withdrawer_keyed_account,
|
|
|
|
&signers,
|
2019-07-25 23:20:47 -07:00
|
|
|
);
|
|
|
|
assert_eq!(res, Ok(()));
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(vote_account.borrow().lamports, 0);
|
|
|
|
assert_eq!(withdrawer_account.borrow().lamports, lamports);
|
2019-07-25 23:20:47 -07:00
|
|
|
}
|
|
|
|
|
2019-06-21 20:43:24 -07:00
|
|
|
#[test]
|
|
|
|
fn test_vote_state_epoch_credits() {
|
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
|
|
|
|
assert_eq!(vote_state.credits(), 0);
|
2019-12-11 22:04:54 -08:00
|
|
|
assert_eq!(vote_state.epoch_credits().clone(), vec![]);
|
2019-06-21 20:43:24 -07:00
|
|
|
|
|
|
|
let mut expected = vec![];
|
|
|
|
let mut credits = 0;
|
|
|
|
let epochs = (MAX_EPOCH_CREDITS_HISTORY + 2) as u64;
|
|
|
|
for epoch in 0..epochs {
|
|
|
|
for _j in 0..epoch {
|
|
|
|
vote_state.increment_credits(epoch);
|
|
|
|
credits += 1;
|
|
|
|
}
|
|
|
|
expected.push((epoch, credits, credits - epoch));
|
|
|
|
}
|
2020-01-21 19:08:40 -08:00
|
|
|
|
2019-06-21 20:43:24 -07:00
|
|
|
while expected.len() > MAX_EPOCH_CREDITS_HISTORY {
|
|
|
|
expected.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(vote_state.credits(), credits);
|
2019-12-11 22:04:54 -08:00
|
|
|
assert_eq!(vote_state.epoch_credits().clone(), expected);
|
2019-06-21 20:43:24 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 20:57:35 -07:00
|
|
|
#[test]
|
|
|
|
fn test_vote_state_epoch0_no_credits() {
|
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
|
2019-12-11 22:04:54 -08:00
|
|
|
assert_eq!(vote_state.epoch_credits().len(), 0);
|
2019-09-26 20:57:35 -07:00
|
|
|
vote_state.increment_credits(1);
|
2020-01-21 19:08:40 -08:00
|
|
|
assert_eq!(vote_state.epoch_credits().len(), 1);
|
2019-09-26 20:57:35 -07:00
|
|
|
|
|
|
|
vote_state.increment_credits(2);
|
2020-01-21 19:08:40 -08:00
|
|
|
assert_eq!(vote_state.epoch_credits().len(), 2);
|
2019-09-26 20:57:35 -07:00
|
|
|
}
|
2019-12-06 13:38:49 -08:00
|
|
|
|
2019-12-30 19:57:53 -08:00
|
|
|
#[test]
|
|
|
|
fn test_vote_state_increment_credits() {
|
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
|
|
|
|
let credits = (MAX_EPOCH_CREDITS_HISTORY + 2) as u64;
|
|
|
|
for i in 0..credits {
|
|
|
|
vote_state.increment_credits(i as u64);
|
|
|
|
}
|
|
|
|
assert_eq!(vote_state.credits(), credits);
|
|
|
|
assert!(vote_state.epoch_credits().len() <= MAX_EPOCH_CREDITS_HISTORY);
|
|
|
|
}
|
|
|
|
|
2019-12-06 13:38:49 -08:00
|
|
|
#[test]
|
|
|
|
fn test_vote_process_timestamp() {
|
|
|
|
let (slot, timestamp) = (15, 1575412285);
|
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
vote_state.last_timestamp = BlockTimestamp { slot, timestamp };
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.process_timestamp(slot - 1, timestamp + 1),
|
|
|
|
Err(VoteError::TimestampTooOld)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.last_timestamp,
|
|
|
|
BlockTimestamp { slot, timestamp }
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.process_timestamp(slot + 1, timestamp - 1),
|
|
|
|
Err(VoteError::TimestampTooOld)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.process_timestamp(slot + 1, timestamp),
|
|
|
|
Err(VoteError::TimestampTooOld)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.process_timestamp(slot, timestamp + 1),
|
|
|
|
Err(VoteError::TimestampTooOld)
|
|
|
|
);
|
|
|
|
assert_eq!(vote_state.process_timestamp(slot, timestamp), Ok(()));
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.last_timestamp,
|
|
|
|
BlockTimestamp { slot, timestamp }
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.process_timestamp(slot + 1, timestamp + 1),
|
|
|
|
Ok(())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
vote_state.last_timestamp,
|
|
|
|
BlockTimestamp {
|
|
|
|
slot: slot + 1,
|
|
|
|
timestamp: timestamp + 1
|
|
|
|
}
|
|
|
|
);
|
2020-01-20 16:54:44 -08:00
|
|
|
|
|
|
|
// Test initial vote
|
|
|
|
vote_state.last_timestamp = BlockTimestamp::default();
|
|
|
|
assert_eq!(vote_state.process_timestamp(0, timestamp), Ok(()));
|
2019-12-06 13:38:49 -08:00
|
|
|
}
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|