VoteProgram -> VoteState
This commit is contained in:
@ -43,34 +43,34 @@ pub enum VoteInstruction {
|
||||
/// * Transaction::keys[1] - the new "vote account" to be associated with the validator
|
||||
/// identified by keys[0] for voting
|
||||
RegisterAccount,
|
||||
NewVote(Vote),
|
||||
Vote(Vote),
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct VoteProgram {
|
||||
pub struct VoteState {
|
||||
pub votes: VecDeque<Vote>,
|
||||
pub node_id: Pubkey,
|
||||
}
|
||||
|
||||
pub fn get_max_size() -> usize {
|
||||
// Upper limit on the size of the Vote State. Equal to
|
||||
// sizeof(VoteProgram) when votes.len() is MAX_VOTE_HISTORY
|
||||
let mut vote_program = VoteProgram::default();
|
||||
// sizeof(VoteState) when votes.len() is MAX_VOTE_HISTORY
|
||||
let mut vote_program = VoteState::default();
|
||||
vote_program.votes = VecDeque::from(vec![Vote::default(); MAX_VOTE_HISTORY]);
|
||||
serialized_size(&vote_program).unwrap() as usize
|
||||
}
|
||||
|
||||
impl VoteProgram {
|
||||
impl VoteState {
|
||||
pub fn new(node_id: Pubkey) -> Self {
|
||||
let votes = VecDeque::new();
|
||||
Self { votes, node_id }
|
||||
}
|
||||
|
||||
pub fn deserialize(input: &[u8]) -> Result<VoteProgram, ProgramError> {
|
||||
pub fn deserialize(input: &[u8]) -> Result<Self, ProgramError> {
|
||||
deserialize(input).map_err(|_| ProgramError::InvalidUserdata)
|
||||
}
|
||||
|
||||
pub fn serialize(self: &VoteProgram, output: &mut [u8]) -> Result<(), ProgramError> {
|
||||
pub fn serialize(&self, output: &mut [u8]) -> Result<(), ProgramError> {
|
||||
serialize_into(output, self).map_err(|err| match *err {
|
||||
ErrorKind::SizeLimit => ProgramError::UserdataTooSmall,
|
||||
_ => ProgramError::GenericError,
|
||||
@ -85,9 +85,9 @@ mod tests {
|
||||
#[test]
|
||||
fn test_serde() {
|
||||
let mut buffer: Vec<u8> = vec![0; get_max_size()];
|
||||
let mut vote_program = VoteProgram::default();
|
||||
let mut vote_program = VoteState::default();
|
||||
vote_program.votes = (0..MAX_VOTE_HISTORY).map(|_| Vote::default()).collect();
|
||||
vote_program.serialize(&mut buffer).unwrap();
|
||||
assert_eq!(VoteProgram::deserialize(&buffer).unwrap(), vote_program);
|
||||
assert_eq!(VoteState::deserialize(&buffer).unwrap(), vote_program);
|
||||
}
|
||||
}
|
||||
|
@ -13,15 +13,15 @@ pub struct VoteTransaction {}
|
||||
|
||||
impl VoteTransaction {
|
||||
pub fn new_vote<T: KeypairUtil>(
|
||||
vote_account: &T,
|
||||
voting_keypair: &T,
|
||||
tick_height: u64,
|
||||
last_id: Hash,
|
||||
fee: u64,
|
||||
) -> Transaction {
|
||||
let vote = Vote { tick_height };
|
||||
let instruction = VoteInstruction::NewVote(vote);
|
||||
let instruction = VoteInstruction::Vote(vote);
|
||||
Transaction::new(
|
||||
vote_account,
|
||||
voting_keypair,
|
||||
&[],
|
||||
vote_program::id(),
|
||||
&instruction,
|
||||
@ -31,28 +31,25 @@ impl VoteTransaction {
|
||||
}
|
||||
|
||||
pub fn new_account(
|
||||
validator_id: &Keypair,
|
||||
from_keypair: &Keypair,
|
||||
vote_account_id: Pubkey,
|
||||
last_id: Hash,
|
||||
num_tokens: u64,
|
||||
fee: u64,
|
||||
) -> Transaction {
|
||||
let create_tx = SystemInstruction::CreateAccount {
|
||||
tokens: num_tokens,
|
||||
space: vote_program::get_max_size() as u64,
|
||||
program_id: vote_program::id(),
|
||||
};
|
||||
Transaction::new_with_instructions(
|
||||
&[validator_id],
|
||||
&[from_keypair],
|
||||
&[vote_account_id],
|
||||
last_id,
|
||||
fee,
|
||||
vec![system_program::id(), vote_program::id()],
|
||||
vec![
|
||||
Instruction::new(
|
||||
0,
|
||||
&SystemInstruction::CreateAccount {
|
||||
tokens: num_tokens,
|
||||
space: vote_program::get_max_size() as u64,
|
||||
program_id: vote_program::id(),
|
||||
},
|
||||
vec![0, 1],
|
||||
),
|
||||
Instruction::new(0, &create_tx, vec![0, 1]),
|
||||
Instruction::new(1, &VoteInstruction::RegisterAccount, vec![0, 1]),
|
||||
],
|
||||
)
|
||||
@ -63,7 +60,7 @@ impl VoteTransaction {
|
||||
for i in 0..tx.instructions.len() {
|
||||
let tx_program_id = tx.program_id(i);
|
||||
if vote_program::check_id(&tx_program_id) {
|
||||
if let Ok(Some(VoteInstruction::NewVote(vote))) = deserialize(&tx.userdata(i)) {
|
||||
if let Ok(Some(VoteInstruction::Vote(vote))) = deserialize(&tx.userdata(i)) {
|
||||
votes.push((tx.account_keys[0], vote, tx.last_id))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user