solana_vote_program/
vote_transaction.rs

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