2018-12-04 07:45:32 -08:00
|
|
|
//! Vote program
|
|
|
|
//! Receive and processes votes from validators
|
|
|
|
|
2019-02-15 07:42:09 -08:00
|
|
|
use crate::account::{Account, KeyedAccount};
|
2018-12-14 20:39:10 -08:00
|
|
|
use crate::native_program::ProgramError;
|
|
|
|
use crate::pubkey::Pubkey;
|
2019-02-28 10:34:32 -08:00
|
|
|
use crate::transaction_builder::BuilderInstruction;
|
2019-01-10 16:15:27 -08:00
|
|
|
use bincode::{deserialize, serialize_into, serialized_size, ErrorKind};
|
2019-02-15 07:42:09 -08:00
|
|
|
use log::*;
|
2018-12-04 07:45:32 -08:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
pub const VOTE_PROGRAM_ID: [u8; 32] = [
|
|
|
|
132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0,
|
|
|
|
];
|
|
|
|
|
|
|
|
pub fn check_id(program_id: &Pubkey) -> bool {
|
|
|
|
program_id.as_ref() == VOTE_PROGRAM_ID
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id() -> Pubkey {
|
|
|
|
Pubkey::new(&VOTE_PROGRAM_ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct Vote {
|
|
|
|
// TODO: add signature of the state here as well
|
2019-02-21 00:44:37 -08:00
|
|
|
/// A vote for height slot_height
|
|
|
|
pub slot_height: u64,
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-01-21 15:45:30 -08:00
|
|
|
impl Vote {
|
2019-02-21 00:44:37 -08:00
|
|
|
pub fn new(slot_height: u64) -> Self {
|
|
|
|
Self { slot_height }
|
2019-01-21 15:45:30 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 21:19:31 -08:00
|
|
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct Lockout {
|
|
|
|
pub slot_height: u64,
|
|
|
|
pub confirmation_count: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lockout {
|
|
|
|
pub fn new(vote: &Vote) -> Self {
|
|
|
|
Self {
|
|
|
|
slot_height: vote.slot_height,
|
|
|
|
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)
|
|
|
|
pub fn expiration_slot_height(&self) -> u64 {
|
|
|
|
self.slot_height + self.lockout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 07:45:32 -08:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum VoteInstruction {
|
2019-02-28 17:08:45 -08:00
|
|
|
/// Initialize the VoteState for this `vote account`
|
|
|
|
/// * Transaction::keys[0] - the staker id that is also assumed to be the delegate by default
|
|
|
|
/// * Transaction::keys[1] - the new "vote account" to be associated with the delegate
|
|
|
|
InitializeAccount,
|
|
|
|
/// `Delegate` or `Assign` A staking account to a particular node
|
|
|
|
DelegateStake(Pubkey),
|
2019-02-01 14:50:11 -08:00
|
|
|
Vote(Vote),
|
2019-02-15 12:20:34 -08:00
|
|
|
/// Clear the credits in the vote account
|
|
|
|
/// * Transaction::keys[0] - the "vote account"
|
|
|
|
ClearCredits,
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-02-28 10:34:32 -08:00
|
|
|
impl VoteInstruction {
|
|
|
|
pub fn new_clear_credits(vote_id: Pubkey) -> BuilderInstruction {
|
|
|
|
BuilderInstruction::new(id(), &VoteInstruction::ClearCredits, vec![(vote_id, true)])
|
|
|
|
}
|
2019-02-28 17:08:45 -08:00
|
|
|
pub fn new_delegate_stake(vote_id: Pubkey, delegate_id: Pubkey) -> BuilderInstruction {
|
|
|
|
BuilderInstruction::new(
|
|
|
|
id(),
|
|
|
|
&VoteInstruction::DelegateStake(delegate_id),
|
|
|
|
vec![(vote_id, true)],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
pub fn new_initialize_account(vote_id: Pubkey, staker_id: Pubkey) -> BuilderInstruction {
|
|
|
|
BuilderInstruction::new(
|
|
|
|
id(),
|
|
|
|
&VoteInstruction::InitializeAccount,
|
|
|
|
vec![(staker_id, true), (vote_id, false)],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
pub fn new_vote(vote_id: Pubkey, vote: Vote) -> BuilderInstruction {
|
|
|
|
BuilderInstruction::new(id(), &VoteInstruction::Vote(vote), vec![(vote_id, true)])
|
|
|
|
}
|
2019-02-28 10:34:32 -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-02-28 17:08:45 -08:00
|
|
|
pub delegate_id: Pubkey,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_max_size() -> usize {
|
|
|
|
// Upper limit on the size of the Vote State. Equal to
|
2019-02-26 21:19:31 -08:00
|
|
|
// sizeof(VoteState) when votes.len() is MAX_LOCKOUT_HISTORY
|
2019-02-15 07:42:09 -08:00
|
|
|
let mut vote_state = VoteState::default();
|
2019-02-26 21:19:31 -08:00
|
|
|
vote_state.votes = VecDeque::from(vec![Lockout::default(); MAX_LOCKOUT_HISTORY]);
|
|
|
|
vote_state.root_slot = Some(std::u64::MAX);
|
2019-02-15 07:42:09 -08:00
|
|
|
serialized_size(&vote_state).unwrap() as usize
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:50:11 -08:00
|
|
|
impl VoteState {
|
2019-02-28 17:08:45 -08:00
|
|
|
pub fn new(delegate_id: Pubkey) -> 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-02-28 17:08:45 -08:00
|
|
|
delegate_id,
|
2019-02-05 09:25:59 -08:00
|
|
|
credits,
|
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-02-01 14:50:11 -08:00
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, ProgramError> {
|
2019-01-10 16:15:27 -08:00
|
|
|
deserialize(input).map_err(|_| ProgramError::InvalidUserdata)
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:50:11 -08:00
|
|
|
pub fn serialize(&self, output: &mut [u8]) -> Result<(), ProgramError> {
|
2019-01-10 16:15:27 -08:00
|
|
|
serialize_into(output, self).map_err(|err| match *err {
|
|
|
|
ErrorKind::SizeLimit => ProgramError::UserdataTooSmall,
|
|
|
|
_ => ProgramError::GenericError,
|
|
|
|
})
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
2019-02-05 09:25:59 -08: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()
|
|
|
|
.map_or(false, |old_vote| old_vote.slot_height >= vote.slot_height)
|
|
|
|
{
|
|
|
|
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-02-26 21:19:31 -08:00
|
|
|
self.pop_expired_votes(vote.slot_height);
|
|
|
|
// 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();
|
|
|
|
self.root_slot = Some(vote.slot_height);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear any credits.
|
|
|
|
pub fn clear_credits(&mut self) {
|
|
|
|
self.credits = 0;
|
|
|
|
}
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
fn pop_expired_votes(&mut self, slot_height: u64) {
|
|
|
|
loop {
|
|
|
|
if self
|
|
|
|
.votes
|
|
|
|
.back()
|
|
|
|
.map_or(false, |v| v.expiration_slot_height() < slot_height)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-02-28 17:08:45 -08:00
|
|
|
pub fn delegate_stake(
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
node_id: Pubkey,
|
|
|
|
) -> Result<(), ProgramError> {
|
|
|
|
if !check_id(&keyed_accounts[0].account.owner) {
|
|
|
|
error!("account[0] is not assigned to the VOTE_PROGRAM");
|
|
|
|
Err(ProgramError::InvalidArgument)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyed_accounts[0].signer_key().is_none() {
|
|
|
|
error!("account[0] should sign the transaction");
|
|
|
|
Err(ProgramError::InvalidArgument)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata);
|
|
|
|
if let Ok(mut vote_state) = vote_state {
|
|
|
|
vote_state.delegate_id = node_id;
|
|
|
|
vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
|
|
|
|
} else {
|
|
|
|
error!("account[0] does not valid userdata");
|
|
|
|
Err(ProgramError::InvalidUserdata)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize the vote_state for a staking account
|
|
|
|
/// 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
|
|
|
|
pub fn initialize_account(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
|
2019-02-15 07:42:09 -08:00
|
|
|
if !check_id(&keyed_accounts[1].account.owner) {
|
|
|
|
error!("account[1] is not assigned to the VOTE_PROGRAM");
|
|
|
|
Err(ProgramError::InvalidArgument)?;
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:08:45 -08:00
|
|
|
if keyed_accounts[0].signer_key().is_none() {
|
|
|
|
error!("account[0] should sign the transaction");
|
|
|
|
Err(ProgramError::InvalidArgument)?;
|
|
|
|
}
|
2019-02-15 07:42:09 -08:00
|
|
|
|
2019-02-28 17:08:45 -08:00
|
|
|
let staker_id = keyed_accounts[0].signer_key().unwrap();
|
|
|
|
let vote_state = VoteState::deserialize(&keyed_accounts[1].account.userdata);
|
|
|
|
if let Ok(vote_state) = vote_state {
|
|
|
|
if vote_state.delegate_id == Pubkey::default() {
|
|
|
|
let vote_state = VoteState::new(*staker_id);
|
|
|
|
vote_state.serialize(&mut keyed_accounts[1].account.userdata)?;
|
|
|
|
} else {
|
|
|
|
error!("account[1] userdata already initialized");
|
|
|
|
Err(ProgramError::InvalidUserdata)?;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error!("account[1] does not have valid userdata");
|
|
|
|
Err(ProgramError::InvalidUserdata)?;
|
|
|
|
}
|
2019-02-15 07:42:09 -08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_vote(keyed_accounts: &mut [KeyedAccount], vote: Vote) -> Result<(), ProgramError> {
|
|
|
|
if !check_id(&keyed_accounts[0].account.owner) {
|
|
|
|
error!("account[0] is not assigned to the VOTE_PROGRAM");
|
|
|
|
Err(ProgramError::InvalidArgument)?;
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:21:16 -08:00
|
|
|
if keyed_accounts[0].signer_key().is_none() {
|
|
|
|
error!("account[0] should sign the transaction");
|
|
|
|
Err(ProgramError::InvalidArgument)?;
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:42:09 -08:00
|
|
|
let mut vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata)?;
|
|
|
|
vote_state.process_vote(vote);
|
|
|
|
vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-15 12:20:34 -08:00
|
|
|
pub fn clear_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
|
|
|
|
if !check_id(&keyed_accounts[0].account.owner) {
|
|
|
|
error!("account[0] is not assigned to the VOTE_PROGRAM");
|
|
|
|
Err(ProgramError::InvalidArgument)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata)?;
|
|
|
|
vote_state.clear_credits();
|
|
|
|
vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:42:09 -08:00
|
|
|
pub fn create_vote_account(tokens: u64) -> Account {
|
|
|
|
let space = get_max_size();
|
|
|
|
Account::new(tokens, space, id())
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:08:45 -08:00
|
|
|
pub fn initialize_and_deserialize(
|
2019-02-15 07:42:09 -08:00
|
|
|
from_id: &Pubkey,
|
|
|
|
from_account: &mut Account,
|
|
|
|
vote_id: &Pubkey,
|
|
|
|
vote_account: &mut Account,
|
|
|
|
) -> Result<VoteState, ProgramError> {
|
|
|
|
let mut keyed_accounts = [
|
|
|
|
KeyedAccount::new(from_id, true, from_account),
|
|
|
|
KeyedAccount::new(vote_id, false, vote_account),
|
|
|
|
];
|
2019-02-28 17:08:45 -08:00
|
|
|
initialize_account(&mut keyed_accounts)?;
|
2019-02-15 07:42:09 -08:00
|
|
|
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
|
|
|
|
Ok(vote_state)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn vote_and_deserialize(
|
|
|
|
vote_id: &Pubkey,
|
|
|
|
vote_account: &mut Account,
|
|
|
|
vote: Vote,
|
|
|
|
) -> Result<VoteState, ProgramError> {
|
|
|
|
let mut keyed_accounts = [KeyedAccount::new(vote_id, true, vote_account)];
|
|
|
|
process_vote(&mut keyed_accounts, vote)?;
|
|
|
|
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
|
|
|
|
Ok(vote_state)
|
|
|
|
}
|
|
|
|
|
2018-12-04 07:45:32 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-02-15 07:42:09 -08:00
|
|
|
use crate::signature::{Keypair, KeypairUtil};
|
2018-12-04 07:45:32 -08:00
|
|
|
|
2019-02-28 17:08:45 -08:00
|
|
|
#[test]
|
|
|
|
fn test_initialize_staking_account() {
|
|
|
|
let staker_id = Keypair::new().pubkey();
|
|
|
|
let mut staker_account = Account::new(100, 0, Pubkey::default());
|
|
|
|
let mut bogus_staker_account = Account::new(100, 0, Pubkey::default());
|
|
|
|
|
|
|
|
let staking_account_id = Keypair::new().pubkey();
|
|
|
|
let mut staking_account = create_vote_account(100);
|
|
|
|
|
|
|
|
let bogus_account_id = Keypair::new().pubkey();
|
|
|
|
let mut bogus_account = Account::new(100, 0, id());
|
|
|
|
|
|
|
|
let mut keyed_accounts = [
|
|
|
|
KeyedAccount::new(&staker_id, false, &mut bogus_staker_account),
|
|
|
|
KeyedAccount::new(&bogus_account_id, false, &mut bogus_account),
|
|
|
|
];
|
|
|
|
|
|
|
|
//staker is not signer
|
|
|
|
let res = initialize_account(&mut keyed_accounts);
|
|
|
|
assert_eq!(res, Err(ProgramError::InvalidArgument));
|
|
|
|
|
|
|
|
//space was never initialized, deserialize will fail
|
|
|
|
keyed_accounts[0] = KeyedAccount::new(&staker_id, true, &mut staker_account);
|
|
|
|
let res = initialize_account(&mut keyed_accounts);
|
|
|
|
assert_eq!(res, Err(ProgramError::InvalidUserdata));
|
|
|
|
|
|
|
|
//init should pass
|
|
|
|
keyed_accounts[1] = KeyedAccount::new(&staking_account_id, false, &mut staking_account);
|
|
|
|
let res = initialize_account(&mut keyed_accounts);
|
|
|
|
assert_eq!(res, Ok(()));
|
|
|
|
|
|
|
|
// reinit should fail
|
|
|
|
let res = initialize_account(&mut keyed_accounts);
|
|
|
|
assert_eq!(res, Err(ProgramError::InvalidUserdata));
|
|
|
|
}
|
|
|
|
|
2018-12-04 07:45:32 -08:00
|
|
|
#[test]
|
2019-02-05 09:25:59 -08:00
|
|
|
fn test_vote_serialize() {
|
2018-12-04 07:45:32 -08:00
|
|
|
let mut buffer: Vec<u8> = vec![0; get_max_size()];
|
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-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() {
|
|
|
|
let from_id = Keypair::new().pubkey();
|
|
|
|
let mut from_account = Account::new(100, 0, Pubkey::default());
|
|
|
|
|
|
|
|
let vote_id = Keypair::new().pubkey();
|
|
|
|
let mut vote_account = create_vote_account(100);
|
|
|
|
|
|
|
|
let vote_state =
|
2019-02-28 17:08:45 -08:00
|
|
|
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account)
|
2019-02-15 07:42:09 -08:00
|
|
|
.unwrap();
|
2019-02-28 17:08:45 -08:00
|
|
|
assert_eq!(vote_state.delegate_id, from_id);
|
2019-02-15 07:42:09 -08:00
|
|
|
assert!(vote_state.votes.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote() {
|
|
|
|
let from_id = Keypair::new().pubkey();
|
|
|
|
let mut from_account = Account::new(100, 0, Pubkey::default());
|
|
|
|
let vote_id = Keypair::new().pubkey();
|
|
|
|
let mut vote_account = create_vote_account(100);
|
2019-02-28 17:08:45 -08:00
|
|
|
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account)
|
|
|
|
.unwrap();
|
2019-02-15 07:42:09 -08:00
|
|
|
|
|
|
|
let vote = Vote::new(1);
|
|
|
|
let vote_state = vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).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() {
|
|
|
|
let from_id = Keypair::new().pubkey();
|
|
|
|
let mut from_account = Account::new(100, 0, Pubkey::default());
|
|
|
|
let vote_id = Keypair::new().pubkey();
|
|
|
|
let mut vote_account = create_vote_account(100);
|
|
|
|
initialize_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let vote = Vote::new(1);
|
|
|
|
let mut keyed_accounts = [KeyedAccount::new(&vote_id, false, &mut vote_account)];
|
|
|
|
let res = process_vote(&mut keyed_accounts, vote);
|
|
|
|
assert_eq!(res, Err(ProgramError::InvalidArgument));
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:42:09 -08:00
|
|
|
#[test]
|
2019-02-28 17:08:45 -08:00
|
|
|
fn test_vote_without_initialization() {
|
2019-02-15 07:42:09 -08:00
|
|
|
let vote_id = Keypair::new().pubkey();
|
|
|
|
let mut vote_account = create_vote_account(100);
|
|
|
|
|
|
|
|
let vote = Vote::new(1);
|
|
|
|
let vote_state = vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap();
|
2019-02-28 17:08:45 -08:00
|
|
|
assert_eq!(vote_state.delegate_id, Pubkey::default());
|
2019-02-26 21:19:31 -08:00
|
|
|
assert_eq!(vote_state.votes, vec![Lockout::new(&vote)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote_lockout() {
|
|
|
|
let voter_id = Keypair::new().pubkey();
|
2019-02-28 17:08:45 -08:00
|
|
|
let mut vote_state = VoteState::new(voter_id);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..(MAX_LOCKOUT_HISTORY + 1) {
|
|
|
|
vote_state.process_vote(Vote::new((INITIAL_LOCKOUT as usize * i) as u64));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
let top_vote = vote_state.votes.front().unwrap().slot_height;
|
|
|
|
vote_state.process_vote(Vote::new(
|
|
|
|
vote_state.votes.back().unwrap().expiration_slot_height(),
|
|
|
|
));
|
|
|
|
assert_eq!(Some(top_vote), vote_state.root_slot);
|
|
|
|
|
|
|
|
// Expire everything except the first vote
|
|
|
|
let vote = Vote::new(vote_state.votes.front().unwrap().expiration_slot_height());
|
|
|
|
vote_state.process_vote(vote);
|
|
|
|
// 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() {
|
|
|
|
let voter_id = Keypair::new().pubkey();
|
2019-02-28 17:08:45 -08:00
|
|
|
let mut vote_state = VoteState::new(voter_id);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..3 {
|
|
|
|
let vote = Vote::new(i as u64);
|
|
|
|
vote_state.process_vote(vote);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the lockouts for first and second votes. Lockouts should be
|
|
|
|
// INITIAL_LOCKOUT^3 and INITIAL_LOCKOUT^2 respectively
|
|
|
|
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
|
|
|
|
vote_state.process_vote(Vote::new((2 + INITIAL_LOCKOUT + 1) as u64));
|
|
|
|
check_lockouts(&vote_state);
|
|
|
|
|
|
|
|
// Vote again, this time the vote stack depth increases, so the lockouts should
|
|
|
|
// double for everybody
|
|
|
|
vote_state.process_vote(Vote::new((2 + INITIAL_LOCKOUT + 2) as u64));
|
|
|
|
check_lockouts(&vote_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vote_credits() {
|
|
|
|
let voter_id = Keypair::new().pubkey();
|
2019-02-28 17:08:45 -08:00
|
|
|
let mut vote_state = VoteState::new(voter_id);
|
2019-02-26 21:19:31 -08:00
|
|
|
|
|
|
|
for i in 0..MAX_LOCKOUT_HISTORY {
|
|
|
|
vote_state.process_vote(Vote::new(i as u64));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(vote_state.credits, 0);
|
|
|
|
|
|
|
|
vote_state.process_vote(Vote::new(MAX_LOCKOUT_HISTORY as u64 + 1));
|
|
|
|
assert_eq!(vote_state.credits, 1);
|
|
|
|
vote_state.process_vote(Vote::new(MAX_LOCKOUT_HISTORY as u64 + 2));
|
|
|
|
assert_eq!(vote_state.credits(), 2);
|
|
|
|
vote_state.process_vote(Vote::new(MAX_LOCKOUT_HISTORY as u64 + 3));
|
|
|
|
assert_eq!(vote_state.credits(), 3);
|
|
|
|
vote_state.clear_credits();
|
|
|
|
assert_eq!(vote_state.credits(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|