solana_vote_program/
vote_transaction.rs

1use {
2    solana_clock::Slot,
3    solana_hash::Hash,
4    solana_keypair::Keypair,
5    solana_signer::Signer,
6    solana_transaction::Transaction,
7    solana_vote_interface::{
8        self as vote,
9        state::{TowerSync, Vote, VoteStateUpdate},
10    },
11};
12
13pub fn new_vote_transaction(
14    slots: Vec<Slot>,
15    bank_hash: Hash,
16    blockhash: Hash,
17    node_keypair: &Keypair,
18    vote_keypair: &Keypair,
19    authorized_voter_keypair: &Keypair,
20    switch_proof_hash: Option<Hash>,
21) -> Transaction {
22    let votes = Vote::new(slots, bank_hash);
23    let vote_ix = if let Some(switch_proof_hash) = switch_proof_hash {
24        vote::instruction::vote_switch(
25            &vote_keypair.pubkey(),
26            &authorized_voter_keypair.pubkey(),
27            votes,
28            switch_proof_hash,
29        )
30    } else {
31        vote::instruction::vote(
32            &vote_keypair.pubkey(),
33            &authorized_voter_keypair.pubkey(),
34            votes,
35        )
36    };
37
38    let mut vote_tx = Transaction::new_with_payer(&[vote_ix], Some(&node_keypair.pubkey()));
39
40    vote_tx.partial_sign(&[node_keypair], blockhash);
41    vote_tx.partial_sign(&[authorized_voter_keypair], blockhash);
42    vote_tx
43}
44
45pub fn new_vote_state_update_transaction(
46    vote_state_update: VoteStateUpdate,
47    blockhash: Hash,
48    node_keypair: &Keypair,
49    vote_keypair: &Keypair,
50    authorized_voter_keypair: &Keypair,
51    switch_proof_hash: Option<Hash>,
52) -> Transaction {
53    let vote_ix = if let Some(switch_proof_hash) = switch_proof_hash {
54        vote::instruction::update_vote_state_switch(
55            &vote_keypair.pubkey(),
56            &authorized_voter_keypair.pubkey(),
57            vote_state_update,
58            switch_proof_hash,
59        )
60    } else {
61        vote::instruction::update_vote_state(
62            &vote_keypair.pubkey(),
63            &authorized_voter_keypair.pubkey(),
64            vote_state_update,
65        )
66    };
67
68    let mut vote_tx = Transaction::new_with_payer(&[vote_ix], Some(&node_keypair.pubkey()));
69
70    vote_tx.partial_sign(&[node_keypair], blockhash);
71    vote_tx.partial_sign(&[authorized_voter_keypair], blockhash);
72    vote_tx
73}
74
75pub fn new_compact_vote_state_update_transaction(
76    vote_state_update: VoteStateUpdate,
77    blockhash: Hash,
78    node_keypair: &Keypair,
79    vote_keypair: &Keypair,
80    authorized_voter_keypair: &Keypair,
81    switch_proof_hash: Option<Hash>,
82) -> Transaction {
83    let vote_ix = if let Some(switch_proof_hash) = switch_proof_hash {
84        vote::instruction::compact_update_vote_state_switch(
85            &vote_keypair.pubkey(),
86            &authorized_voter_keypair.pubkey(),
87            vote_state_update,
88            switch_proof_hash,
89        )
90    } else {
91        vote::instruction::compact_update_vote_state(
92            &vote_keypair.pubkey(),
93            &authorized_voter_keypair.pubkey(),
94            vote_state_update,
95        )
96    };
97
98    let mut vote_tx = Transaction::new_with_payer(&[vote_ix], Some(&node_keypair.pubkey()));
99
100    vote_tx.partial_sign(&[node_keypair], blockhash);
101    vote_tx.partial_sign(&[authorized_voter_keypair], blockhash);
102    vote_tx
103}
104
105#[must_use]
106pub fn new_tower_sync_transaction(
107    tower_sync: TowerSync,
108    blockhash: Hash,
109    node_keypair: &Keypair,
110    vote_keypair: &Keypair,
111    authorized_voter_keypair: &Keypair,
112    switch_proof_hash: Option<Hash>,
113) -> Transaction {
114    let vote_ix = if let Some(switch_proof_hash) = switch_proof_hash {
115        vote::instruction::tower_sync_switch(
116            &vote_keypair.pubkey(),
117            &authorized_voter_keypair.pubkey(),
118            tower_sync,
119            switch_proof_hash,
120        )
121    } else {
122        vote::instruction::tower_sync(
123            &vote_keypair.pubkey(),
124            &authorized_voter_keypair.pubkey(),
125            tower_sync,
126        )
127    };
128
129    let mut vote_tx = Transaction::new_with_payer(&[vote_ix], Some(&node_keypair.pubkey()));
130
131    vote_tx.partial_sign(&[node_keypair], blockhash);
132    vote_tx.partial_sign(&[authorized_voter_keypair], blockhash);
133    vote_tx
134}