solana_vote/
vote_transaction.rs

1use solana_sdk::{
2    clock::{Slot, UnixTimestamp},
3    hash::Hash,
4    vote::state::{TowerSync, Vote, VoteStateUpdate},
5};
6
7#[derive(Debug, PartialEq, Eq, Clone)]
8pub enum VoteTransaction {
9    Vote(Vote),
10    VoteStateUpdate(VoteStateUpdate),
11    TowerSync(TowerSync),
12}
13
14impl VoteTransaction {
15    pub fn slots(&self) -> Vec<Slot> {
16        match self {
17            VoteTransaction::Vote(vote) => vote.slots.clone(),
18            VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update
19                .lockouts
20                .iter()
21                .map(|lockout| lockout.slot())
22                .collect(),
23            VoteTransaction::TowerSync(tower_sync) => tower_sync.slots(),
24        }
25    }
26
27    pub fn is_empty(&self) -> bool {
28        match self {
29            VoteTransaction::Vote(vote) => vote.slots.is_empty(),
30            VoteTransaction::VoteStateUpdate(vote_state_update) => {
31                vote_state_update.lockouts.is_empty()
32            }
33            VoteTransaction::TowerSync(tower_sync) => tower_sync.lockouts.is_empty(),
34        }
35    }
36
37    pub fn hash(&self) -> Hash {
38        match self {
39            VoteTransaction::Vote(vote) => vote.hash,
40            VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update.hash,
41            VoteTransaction::TowerSync(tower_sync) => tower_sync.hash,
42        }
43    }
44
45    pub fn timestamp(&self) -> Option<UnixTimestamp> {
46        match self {
47            VoteTransaction::Vote(vote) => vote.timestamp,
48            VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update.timestamp,
49            VoteTransaction::TowerSync(tower_sync) => tower_sync.timestamp,
50        }
51    }
52
53    pub fn last_voted_slot(&self) -> Option<Slot> {
54        match self {
55            VoteTransaction::Vote(vote) => vote.slots.last().copied(),
56            VoteTransaction::VoteStateUpdate(vote_state_update) => {
57                Some(vote_state_update.lockouts.back()?.slot())
58            }
59            VoteTransaction::TowerSync(tower_sync) => tower_sync.last_voted_slot(),
60        }
61    }
62
63    pub fn last_voted_slot_hash(&self) -> Option<(Slot, Hash)> {
64        Some((self.last_voted_slot()?, self.hash()))
65    }
66
67    pub fn is_full_tower_vote(&self) -> bool {
68        matches!(
69            self,
70            VoteTransaction::VoteStateUpdate(_) | VoteTransaction::TowerSync(_)
71        )
72    }
73}
74
75impl From<Vote> for VoteTransaction {
76    fn from(vote: Vote) -> Self {
77        VoteTransaction::Vote(vote)
78    }
79}
80
81impl From<VoteStateUpdate> for VoteTransaction {
82    fn from(vote_state_update: VoteStateUpdate) -> Self {
83        VoteTransaction::VoteStateUpdate(vote_state_update)
84    }
85}
86
87impl From<TowerSync> for VoteTransaction {
88    fn from(tower_sync: TowerSync) -> Self {
89        VoteTransaction::TowerSync(tower_sync)
90    }
91}