1use {
2 serde::{Deserialize, Serialize},
3 solana_clock::Slot,
4 solana_commitment_config::CommitmentLevel,
5 solana_vote_program::vote_state::MAX_LOCKOUT_HISTORY,
6 std::collections::HashMap,
7};
8
9pub const VOTE_THRESHOLD_SIZE: f64 = 2f64 / 3f64;
10
11pub type BlockCommitmentArray = [u64; MAX_LOCKOUT_HISTORY + 1];
12
13#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
14pub struct BlockCommitment {
15 pub commitment: BlockCommitmentArray,
16}
17
18impl BlockCommitment {
19 pub fn increase_confirmation_stake(&mut self, confirmation_count: usize, stake: u64) {
20 assert!(confirmation_count > 0 && confirmation_count <= MAX_LOCKOUT_HISTORY);
21 self.commitment[confirmation_count - 1] += stake;
22 }
23
24 pub fn get_confirmation_stake(&mut self, confirmation_count: usize) -> u64 {
25 assert!(confirmation_count > 0 && confirmation_count <= MAX_LOCKOUT_HISTORY);
26 self.commitment[confirmation_count - 1]
27 }
28
29 pub fn increase_rooted_stake(&mut self, stake: u64) {
30 self.commitment[MAX_LOCKOUT_HISTORY] += stake;
31 }
32
33 pub fn get_rooted_stake(&self) -> u64 {
34 self.commitment[MAX_LOCKOUT_HISTORY]
35 }
36
37 pub fn new(commitment: BlockCommitmentArray) -> Self {
38 Self { commitment }
39 }
40}
41
42#[derive(Default)]
44pub struct BlockCommitmentCache {
45 block_commitment: HashMap<Slot, BlockCommitment>,
48 commitment_slots: CommitmentSlots,
51 total_stake: u64,
53}
54
55impl std::fmt::Debug for BlockCommitmentCache {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 f.debug_struct("BlockCommitmentCache")
58 .field("block_commitment", &self.block_commitment)
59 .field("total_stake", &self.total_stake)
60 .field(
61 "bank",
62 &format_args!("Bank({{current_slot: {:?}}})", self.commitment_slots.slot),
63 )
64 .field("root", &self.commitment_slots.root)
65 .finish()
66 }
67}
68
69impl BlockCommitmentCache {
70 pub fn new(
71 block_commitment: HashMap<Slot, BlockCommitment>,
72 total_stake: u64,
73 commitment_slots: CommitmentSlots,
74 ) -> Self {
75 Self {
76 block_commitment,
77 commitment_slots,
78 total_stake,
79 }
80 }
81
82 pub fn get_block_commitment(&self, slot: Slot) -> Option<&BlockCommitment> {
83 self.block_commitment.get(&slot)
84 }
85
86 pub fn total_stake(&self) -> u64 {
87 self.total_stake
88 }
89
90 pub fn slot(&self) -> Slot {
91 self.commitment_slots.slot
92 }
93
94 pub fn root(&self) -> Slot {
95 self.commitment_slots.root
96 }
97
98 pub fn highest_confirmed_slot(&self) -> Slot {
99 self.commitment_slots.highest_confirmed_slot
100 }
101
102 pub fn highest_super_majority_root(&self) -> Slot {
103 self.commitment_slots.highest_super_majority_root
104 }
105
106 pub fn commitment_slots(&self) -> CommitmentSlots {
107 self.commitment_slots
108 }
109
110 pub fn highest_gossip_confirmed_slot(&self) -> Slot {
111 self.highest_confirmed_slot()
114 }
115
116 pub fn slot_with_commitment(&self, commitment_level: CommitmentLevel) -> Slot {
117 match commitment_level {
118 CommitmentLevel::Processed => self.slot(),
119 CommitmentLevel::Confirmed => self.highest_gossip_confirmed_slot(),
120 CommitmentLevel::Finalized => self.highest_super_majority_root(),
121 }
122 }
123
124 fn highest_slot_with_confirmation_count(&self, confirmation_count: usize) -> Slot {
125 assert!(confirmation_count > 0 && confirmation_count <= MAX_LOCKOUT_HISTORY);
126 for slot in (self.root()..self.slot()).rev() {
127 if let Some(count) = self.get_confirmation_count(slot) {
128 if count >= confirmation_count {
129 return slot;
130 }
131 }
132 }
133 self.commitment_slots.root
134 }
135
136 pub fn calculate_highest_confirmed_slot(&self) -> Slot {
137 self.highest_slot_with_confirmation_count(1)
138 }
139
140 pub fn get_confirmation_count(&self, slot: Slot) -> Option<usize> {
141 self.get_lockout_count(slot, VOTE_THRESHOLD_SIZE)
142 }
143
144 fn get_lockout_count(&self, slot: Slot, minimum_stake_percentage: f64) -> Option<usize> {
147 self.get_block_commitment(slot).map(|block_commitment| {
148 let iterator = block_commitment.commitment.iter().enumerate().rev();
149 let mut sum = 0;
150 for (i, stake) in iterator {
151 sum += stake;
152 if (sum as f64 / self.total_stake as f64) > minimum_stake_percentage {
153 return i + 1;
154 }
155 }
156 0
157 })
158 }
159
160 pub fn new_for_tests() -> Self {
161 let mut block_commitment: HashMap<Slot, BlockCommitment> = HashMap::new();
162 block_commitment.insert(0, BlockCommitment::default());
163 Self {
164 block_commitment,
165 total_stake: 42,
166 ..Self::default()
167 }
168 }
169
170 pub fn new_for_tests_with_slots(slot: Slot, root: Slot) -> Self {
171 let mut block_commitment: HashMap<Slot, BlockCommitment> = HashMap::new();
172 block_commitment.insert(0, BlockCommitment::default());
173 Self {
174 block_commitment,
175 total_stake: 42,
176 commitment_slots: CommitmentSlots {
177 slot,
178 root,
179 highest_confirmed_slot: root,
180 highest_super_majority_root: root,
181 },
182 }
183 }
184
185 pub fn set_highest_confirmed_slot(&mut self, slot: Slot) {
186 self.commitment_slots.highest_confirmed_slot = slot;
187 }
188
189 pub fn set_highest_super_majority_root(&mut self, root: Slot) {
190 self.commitment_slots.highest_super_majority_root = root;
191 }
192
193 pub fn initialize_slots(&mut self, slot: Slot, root: Slot) {
194 self.commitment_slots.slot = slot;
195 self.commitment_slots.root = root;
196 }
197
198 pub fn set_all_slots(&mut self, slot: Slot, root: Slot) {
199 self.commitment_slots.slot = slot;
200 self.commitment_slots.highest_confirmed_slot = slot;
201 self.commitment_slots.root = root;
202 self.commitment_slots.highest_super_majority_root = root;
203 }
204}
205
206#[derive(Default, Clone, Copy)]
207pub struct CommitmentSlots {
208 pub slot: Slot,
210 pub root: Slot,
212 pub highest_confirmed_slot: Slot,
214 pub highest_super_majority_root: Slot,
216}
217
218impl CommitmentSlots {
219 pub fn new_from_slot(slot: Slot) -> Self {
220 Self {
221 slot,
222 ..Self::default()
223 }
224 }
225}
226
227#[cfg(test)]
228mod tests {
229 use super::*;
230
231 #[test]
232 fn test_block_commitment() {
233 let mut cache = BlockCommitment::default();
234 assert_eq!(cache.get_confirmation_stake(1), 0);
235 cache.increase_confirmation_stake(1, 10);
236 assert_eq!(cache.get_confirmation_stake(1), 10);
237 cache.increase_confirmation_stake(1, 20);
238 assert_eq!(cache.get_confirmation_stake(1), 30);
239 }
240
241 #[test]
242 fn test_get_confirmations() {
243 let mut cache0 = BlockCommitment::default();
245 cache0.increase_confirmation_stake(1, 5);
246 cache0.increase_confirmation_stake(2, 40);
247
248 let mut cache1 = BlockCommitment::default();
249 cache1.increase_confirmation_stake(1, 40);
250 cache1.increase_confirmation_stake(2, 5);
251
252 let mut cache2 = BlockCommitment::default();
253 cache2.increase_confirmation_stake(1, 20);
254 cache2.increase_confirmation_stake(2, 5);
255
256 let mut block_commitment = HashMap::new();
257 block_commitment.entry(0).or_insert(cache0);
258 block_commitment.entry(1).or_insert(cache1);
259 block_commitment.entry(2).or_insert(cache2);
260 let block_commitment_cache = BlockCommitmentCache {
261 block_commitment,
262 total_stake: 50,
263 ..BlockCommitmentCache::default()
264 };
265
266 assert_eq!(block_commitment_cache.get_confirmation_count(0), Some(2));
267 assert_eq!(block_commitment_cache.get_confirmation_count(1), Some(1));
268 assert_eq!(block_commitment_cache.get_confirmation_count(2), Some(0),);
269 assert_eq!(block_commitment_cache.get_confirmation_count(3), None,);
270 }
271
272 #[test]
273 fn test_highest_confirmed_slot() {
274 let bank_slot_5 = 5;
275 let total_stake = 50;
276
277 let mut cache0 = BlockCommitment::default();
279 cache0.increase_confirmation_stake(1, 5);
280 cache0.increase_confirmation_stake(2, 40);
281
282 let mut cache1 = BlockCommitment::default();
284 cache1.increase_confirmation_stake(1, 40);
285 cache1.increase_confirmation_stake(2, 5);
286
287 let mut cache2 = BlockCommitment::default();
289 cache2.increase_confirmation_stake(1, 20);
290 cache2.increase_confirmation_stake(2, 5);
291
292 let mut block_commitment = HashMap::new();
293 block_commitment.entry(1).or_insert_with(|| cache0.clone()); block_commitment.entry(2).or_insert_with(|| cache1.clone()); block_commitment.entry(3).or_insert_with(|| cache2.clone()); let commitment_slots = CommitmentSlots::new_from_slot(bank_slot_5);
297 let block_commitment_cache =
298 BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
299
300 assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 2);
301
302 let mut block_commitment = HashMap::new();
304 block_commitment.entry(1).or_insert_with(|| cache1.clone()); block_commitment.entry(2).or_insert_with(|| cache1.clone()); block_commitment.entry(3).or_insert_with(|| cache2.clone()); let block_commitment_cache =
308 BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
309
310 assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 2);
311
312 let mut block_commitment = HashMap::new();
314 block_commitment.entry(1).or_insert_with(|| cache1.clone()); block_commitment.entry(3).or_insert(cache1); block_commitment.entry(5).or_insert_with(|| cache2.clone()); let block_commitment_cache =
318 BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
319
320 assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 3);
321
322 let mut block_commitment = HashMap::new();
324 block_commitment.entry(1).or_insert(cache0); block_commitment.entry(2).or_insert_with(|| cache2.clone()); block_commitment.entry(3).or_insert_with(|| cache2.clone()); let block_commitment_cache =
328 BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
329
330 assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 1);
331
332 let mut block_commitment = HashMap::new();
334 block_commitment.entry(1).or_insert_with(|| cache2.clone()); block_commitment.entry(2).or_insert_with(|| cache2.clone()); block_commitment.entry(3).or_insert(cache2); let block_commitment_cache =
338 BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
339
340 assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 0);
341 }
342}