VoteProgram -> VoteState
This commit is contained in:
@ -7,7 +7,7 @@ use solana_sdk::account::KeyedAccount;
|
||||
use solana_sdk::native_program::ProgramError;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::solana_entrypoint;
|
||||
use solana_sdk::vote_program::{self, Vote, VoteInstruction, VoteProgram};
|
||||
use solana_sdk::vote_program::{self, Vote, VoteInstruction, VoteState};
|
||||
|
||||
fn register(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
|
||||
if !vote_program::check_id(&keyed_accounts[1].account.owner) {
|
||||
@ -15,9 +15,7 @@ fn register(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
|
||||
Err(ProgramError::InvalidArgument)?;
|
||||
}
|
||||
|
||||
// TODO: a single validator could register multiple "vote accounts"
|
||||
// which would clutter the "accounts" structure. See github issue 1654.
|
||||
let vote_state = VoteProgram::new(*keyed_accounts[0].signer_key().unwrap());
|
||||
let vote_state = VoteState::new(*keyed_accounts[0].signer_key().unwrap());
|
||||
vote_state.serialize(&mut keyed_accounts[1].account.userdata)?;
|
||||
|
||||
Ok(())
|
||||
@ -29,7 +27,7 @@ fn process_vote(keyed_accounts: &mut [KeyedAccount], vote: Vote) -> Result<(), P
|
||||
Err(ProgramError::InvalidArgument)?;
|
||||
}
|
||||
|
||||
let mut vote_state = VoteProgram::deserialize(&keyed_accounts[0].account.userdata)?;
|
||||
let mut vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata)?;
|
||||
|
||||
// TODO: Integrity checks
|
||||
// a) Verify the vote's bank hash matches what is expected
|
||||
@ -66,7 +64,7 @@ fn entrypoint(
|
||||
|
||||
match deserialize(data).map_err(|_| ProgramError::InvalidUserdata)? {
|
||||
VoteInstruction::RegisterAccount => register(keyed_accounts),
|
||||
VoteInstruction::NewVote(vote) => {
|
||||
VoteInstruction::Vote(vote) => {
|
||||
debug!("{:?} by {}", vote, keyed_accounts[0].signer_key().unwrap());
|
||||
solana_metrics::submit(
|
||||
solana_metrics::influxdb::Point::new("vote-native")
|
||||
@ -85,85 +83,79 @@ mod tests {
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::vote_program;
|
||||
|
||||
fn create_vote_program(tokens: u64) -> Account {
|
||||
fn create_vote_account(tokens: u64) -> Account {
|
||||
let space = vote_program::get_max_size();
|
||||
Account::new(tokens, space, vote_program::id())
|
||||
}
|
||||
|
||||
fn register_and_deserialize(
|
||||
voter_id: &Pubkey,
|
||||
voter_account: &mut Account,
|
||||
vote_state_id: &Pubkey,
|
||||
vote_state_account: &mut Account,
|
||||
) -> Result<VoteProgram, ProgramError> {
|
||||
from_id: &Pubkey,
|
||||
from_account: &mut Account,
|
||||
vote_id: &Pubkey,
|
||||
vote_account: &mut Account,
|
||||
) -> Result<VoteState, ProgramError> {
|
||||
let mut keyed_accounts = [
|
||||
KeyedAccount::new(voter_id, true, voter_account),
|
||||
KeyedAccount::new(vote_state_id, false, vote_state_account),
|
||||
KeyedAccount::new(from_id, true, from_account),
|
||||
KeyedAccount::new(vote_id, false, vote_account),
|
||||
];
|
||||
register(&mut keyed_accounts)?;
|
||||
let vote_state = VoteProgram::deserialize(&vote_state_account.userdata).unwrap();
|
||||
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
|
||||
Ok(vote_state)
|
||||
}
|
||||
|
||||
fn vote_and_deserialize(
|
||||
vote_state_id: &Pubkey,
|
||||
vote_state_account: &mut Account,
|
||||
vote_id: &Pubkey,
|
||||
vote_account: &mut Account,
|
||||
vote: Vote,
|
||||
) -> Result<VoteProgram, ProgramError> {
|
||||
let mut keyed_accounts = [KeyedAccount::new(vote_state_id, true, vote_state_account)];
|
||||
) -> Result<VoteState, ProgramError> {
|
||||
let mut keyed_accounts = [KeyedAccount::new(vote_id, true, vote_account)];
|
||||
process_vote(&mut keyed_accounts, vote)?;
|
||||
let vote_state = VoteProgram::deserialize(&vote_state_account.userdata).unwrap();
|
||||
let vote_state = VoteState::deserialize(&vote_account.userdata).unwrap();
|
||||
Ok(vote_state)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_voter_registration() {
|
||||
let voter_id = Keypair::new().pubkey();
|
||||
let mut voter_account = Account::new(100, 0, Pubkey::default());
|
||||
let from_id = Keypair::new().pubkey();
|
||||
let mut from_account = Account::new(100, 0, Pubkey::default());
|
||||
|
||||
let vote_state_id = Keypair::new().pubkey();
|
||||
let mut vote_state_account = create_vote_program(100);
|
||||
let vote_id = Keypair::new().pubkey();
|
||||
let mut vote_account = create_vote_account(100);
|
||||
|
||||
let vote_state = register_and_deserialize(
|
||||
&voter_id,
|
||||
&mut voter_account,
|
||||
&vote_state_id,
|
||||
&mut vote_state_account,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(vote_state.node_id, voter_id);
|
||||
let vote_state =
|
||||
register_and_deserialize(&from_id, &mut from_account, &vote_id, &mut vote_account)
|
||||
.unwrap();
|
||||
assert_eq!(vote_state.node_id, from_id);
|
||||
assert!(vote_state.votes.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vote() {
|
||||
let voter_id = Keypair::new().pubkey();
|
||||
let mut voter_account = Account::new(100, 0, Pubkey::default());
|
||||
let from_id = Keypair::new().pubkey();
|
||||
let mut from_account = Account::new(100, 0, Pubkey::default());
|
||||
|
||||
let vote_state_id = Keypair::new().pubkey();
|
||||
let mut vote_state_account = create_vote_program(100);
|
||||
let vote_id = Keypair::new().pubkey();
|
||||
let mut vote_account = create_vote_account(100);
|
||||
|
||||
let mut keyed_accounts = [
|
||||
KeyedAccount::new(&voter_id, true, &mut voter_account),
|
||||
KeyedAccount::new(&vote_state_id, false, &mut vote_state_account),
|
||||
KeyedAccount::new(&from_id, true, &mut from_account),
|
||||
KeyedAccount::new(&vote_id, false, &mut vote_account),
|
||||
];
|
||||
register(&mut keyed_accounts).unwrap();
|
||||
|
||||
let vote = Vote::new(1);
|
||||
let vote_state =
|
||||
vote_and_deserialize(&vote_state_id, &mut vote_state_account, vote.clone()).unwrap();
|
||||
let vote_state = vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap();
|
||||
assert_eq!(vote_state.votes, vec![vote]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
// TODO: Should this work?
|
||||
fn test_vote_without_registration() {
|
||||
let vote_state_id = Keypair::new().pubkey();
|
||||
let mut vote_state_account = create_vote_program(100);
|
||||
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_state_id, &mut vote_state_account, vote.clone()).unwrap();
|
||||
let vote_state = vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap();
|
||||
assert_eq!(vote_state.node_id, Pubkey::default());
|
||||
assert_eq!(vote_state.votes, vec![vote]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user