2018-12-04 07:45:32 -08:00
|
|
|
//! Vote program
|
|
|
|
//! Receive and processes votes from validators
|
|
|
|
|
2018-12-14 20:39:10 -08:00
|
|
|
use crate::native_program::ProgramError;
|
|
|
|
use crate::pubkey::Pubkey;
|
2019-01-10 16:15:27 -08:00
|
|
|
use bincode::{deserialize, serialize_into, serialized_size, ErrorKind};
|
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
|
|
|
|
pub const MAX_VOTE_HISTORY: usize = 32;
|
|
|
|
|
|
|
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct Vote {
|
|
|
|
// TODO: add signature of the state here as well
|
|
|
|
/// A vote for height tick_height
|
|
|
|
pub tick_height: u64,
|
|
|
|
}
|
|
|
|
|
2019-01-21 15:45:30 -08:00
|
|
|
impl Vote {
|
|
|
|
pub fn new(tick_height: u64) -> Self {
|
|
|
|
Self { tick_height }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 07:45:32 -08:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum VoteInstruction {
|
|
|
|
/// Register a new "vote account" to represent a particular validator in the Vote Contract,
|
|
|
|
/// and initialize the VoteState for this "vote account"
|
|
|
|
/// * Transaction::keys[0] - the validator id
|
|
|
|
/// * Transaction::keys[1] - the new "vote account" to be associated with the validator
|
|
|
|
/// identified by keys[0] for voting
|
|
|
|
RegisterAccount,
|
2019-02-01 14:50:11 -08:00
|
|
|
Vote(Vote),
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
|
2019-02-01 14:50:11 -08:00
|
|
|
pub struct VoteState {
|
2018-12-04 07:45:32 -08:00
|
|
|
pub votes: VecDeque<Vote>,
|
|
|
|
pub node_id: Pubkey,
|
2019-02-01 15:35:50 -08:00
|
|
|
pub staker_id: Pubkey,
|
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-01 14:50:11 -08:00
|
|
|
// sizeof(VoteState) when votes.len() is MAX_VOTE_HISTORY
|
|
|
|
let mut vote_program = VoteState::default();
|
2019-01-10 15:47:59 -08:00
|
|
|
vote_program.votes = VecDeque::from(vec![Vote::default(); MAX_VOTE_HISTORY]);
|
2019-01-10 16:15:27 -08:00
|
|
|
serialized_size(&vote_program).unwrap() as usize
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:50:11 -08:00
|
|
|
impl VoteState {
|
2019-02-01 15:35:50 -08:00
|
|
|
pub fn new(node_id: Pubkey, staker_id: Pubkey) -> Self {
|
2019-01-21 15:45:30 -08:00
|
|
|
let votes = VecDeque::new();
|
2019-02-01 15:35:50 -08:00
|
|
|
Self {
|
|
|
|
votes,
|
|
|
|
node_id,
|
|
|
|
staker_id,
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serde() {
|
|
|
|
let mut buffer: Vec<u8> = vec![0; get_max_size()];
|
2019-02-01 14:50:11 -08:00
|
|
|
let mut vote_program = VoteState::default();
|
2018-12-04 07:45:32 -08:00
|
|
|
vote_program.votes = (0..MAX_VOTE_HISTORY).map(|_| Vote::default()).collect();
|
|
|
|
vote_program.serialize(&mut buffer).unwrap();
|
2019-02-01 14:50:11 -08:00
|
|
|
assert_eq!(VoteState::deserialize(&buffer).unwrap(), vote_program);
|
2018-12-04 07:45:32 -08:00
|
|
|
}
|
|
|
|
}
|