vote update node_id (#7253)

* vote update node_id

* un-break binary 'contract'
This commit is contained in:
Rob Walker 2019-12-03 22:50:08 -08:00 committed by GitHub
parent e2f07a5220
commit 9077a94dfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 0 deletions

View File

@ -54,6 +54,9 @@ pub enum VoteInstruction {
/// Withdraw some amount of funds /// Withdraw some amount of funds
Withdraw(u64), Withdraw(u64),
/// Update the vote account's node id
UpdateNode(Pubkey),
} }
fn initialize_account(vote_pubkey: &Pubkey, vote_init: &VoteInit) -> Instruction { fn initialize_account(vote_pubkey: &Pubkey, vote_init: &VoteInit) -> Instruction {
@ -96,6 +99,20 @@ pub fn authorize(
) )
} }
pub fn update_node(
vote_pubkey: &Pubkey, // vote account
authorized_pubkey: &Pubkey, // currently authorized
node_pubkey: &Pubkey,
) -> Instruction {
let account_metas = vec![AccountMeta::new(*vote_pubkey, false)].with_signer(authorized_pubkey);
Instruction::new(
id(),
&VoteInstruction::UpdateNode(*node_pubkey),
account_metas,
)
}
pub fn vote(vote_pubkey: &Pubkey, authorized_voter_pubkey: &Pubkey, vote: Vote) -> Instruction { pub fn vote(vote_pubkey: &Pubkey, authorized_voter_pubkey: &Pubkey, vote: Vote) -> Instruction {
let account_metas = vec![ let account_metas = vec![
AccountMeta::new(*vote_pubkey, false), AccountMeta::new(*vote_pubkey, false),
@ -145,6 +162,9 @@ pub fn process_instruction(
VoteInstruction::Authorize(voter_pubkey, vote_authorize) => { VoteInstruction::Authorize(voter_pubkey, vote_authorize) => {
vote_state::authorize(me, &voter_pubkey, vote_authorize, &signers) vote_state::authorize(me, &voter_pubkey, vote_authorize, &signers)
} }
VoteInstruction::UpdateNode(node_pubkey) => {
vote_state::update_node(me, &node_pubkey, &signers)
}
VoteInstruction::Vote(vote) => { VoteInstruction::Vote(vote) => {
datapoint_debug!("vote-native", ("count", 1, i64)); datapoint_debug!("vote-native", ("count", 1, i64));
vote_state::process_vote( vote_state::process_vote(
@ -237,6 +257,15 @@ mod tests {
)), )),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
assert_eq!(
process_instruction(&update_node(
&Pubkey::default(),
&Pubkey::default(),
&Pubkey::default(),
)),
Err(InstructionError::InvalidAccountData),
);
assert_eq!( assert_eq!(
process_instruction(&withdraw( process_instruction(&withdraw(
&Pubkey::default(), &Pubkey::default(),

View File

@ -367,6 +367,22 @@ pub fn authorize(
vote_account.set_state(&vote_state) vote_account.set_state(&vote_state)
} }
/// 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)
}
fn verify_authorized_signer( fn verify_authorized_signer(
authorized: &Pubkey, authorized: &Pubkey,
signers: &HashSet<Pubkey>, signers: &HashSet<Pubkey>,
@ -621,6 +637,27 @@ mod tests {
); );
} }
#[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));
let vote_state: VoteState = vote_account.state().unwrap();
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(()));
let vote_state: VoteState = vote_account.state().unwrap();
assert_eq!(vote_state.node_pubkey, node_pubkey);
}
#[test] #[test]
fn test_vote_signature() { fn test_vote_signature() {
let (vote_pubkey, mut vote_account) = create_test_account(); let (vote_pubkey, mut vote_account) = create_test_account();