1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::{
cluster_info_vote_listener::VerifiedLabelVotePacketsReceiver, crds_value::CrdsValueLabel,
result::Result,
};
use solana_perf::packet::Packets;
use solana_sdk::clock::Slot;
use std::{
collections::{hash_map::Entry, HashMap},
time::Duration,
};
#[derive(Default)]
pub struct VerifiedVotePackets(HashMap<CrdsValueLabel, (u64, Slot, Packets)>);
impl VerifiedVotePackets {
pub fn receive_and_process_vote_packets(
&mut self,
vote_packets_receiver: &VerifiedLabelVotePacketsReceiver,
last_update_version: &mut u64,
) -> Result<()> {
let timer = Duration::from_millis(200);
let vote_packets = vote_packets_receiver.recv_timeout(timer)?;
*last_update_version += 1;
for (label, slot, packet) in vote_packets {
self.0.insert(label, (*last_update_version, slot, packet));
}
while let Ok(vote_packets) = vote_packets_receiver.try_recv() {
for (label, slot, packet) in vote_packets {
self.0.insert(label, (*last_update_version, slot, packet));
}
}
Ok(())
}
#[cfg(test)]
fn get_vote_packets(&self, key: &CrdsValueLabel) -> Option<&(u64, Slot, Packets)> {
self.0.get(key)
}
pub fn get_latest_votes(&self, last_update_version: u64) -> (u64, Packets) {
let mut new_update_version = last_update_version;
let mut votes = HashMap::new();
for (label, (version, slot, packets)) in &self.0 {
new_update_version = std::cmp::max(*version, new_update_version);
if *version <= last_update_version {
continue;
}
match votes.entry(label.pubkey()) {
Entry::Vacant(entry) => {
entry.insert((slot, packets));
}
Entry::Occupied(mut entry) => {
let (entry_slot, _) = entry.get();
if *entry_slot < slot {
*entry.get_mut() = (slot, packets);
}
}
}
}
let packets = votes
.into_iter()
.flat_map(|(_, (_, packets))| packets.packets.clone())
.collect();
(new_update_version, Packets::new(packets))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::result::Error;
use crossbeam_channel::{unbounded, RecvTimeoutError};
use solana_perf::packet::{Meta, Packet};
#[test]
fn test_get_latest_votes() {
let pubkey = solana_sdk::pubkey::new_rand();
let label1 = CrdsValueLabel::Vote(0, pubkey);
let label2 = CrdsValueLabel::Vote(1, pubkey);
let mut verified_vote_packets = VerifiedVotePackets(HashMap::new());
let data = Packet {
meta: Meta {
repair: true,
..Meta::default()
},
..Packet::default()
};
let none_empty_packets = Packets::new(vec![data, Packet::default()]);
verified_vote_packets
.0
.insert(label1, (2, 42, none_empty_packets));
verified_vote_packets
.0
.insert(label2, (1, 23, Packets::default()));
let (new_update_version, updates) = verified_vote_packets.get_latest_votes(0);
assert_eq!(new_update_version, 2);
assert_eq!(updates.packets.len(), 2);
let (new_update_version, updates) = verified_vote_packets.get_latest_votes(1);
assert_eq!(new_update_version, 2);
assert_eq!(updates.packets.is_empty(), false);
let (new_update_version, updates) = verified_vote_packets.get_latest_votes(3);
assert_eq!(new_update_version, 3);
assert!(updates.is_empty());
}
#[test]
fn test_get_and_process_vote_packets() {
let (s, r) = unbounded();
let pubkey = solana_sdk::pubkey::new_rand();
let label1 = CrdsValueLabel::Vote(0, pubkey);
let label2 = CrdsValueLabel::Vote(1, pubkey);
let mut update_version = 0;
s.send(vec![(label1.clone(), 17, Packets::default())])
.unwrap();
s.send(vec![(label2.clone(), 23, Packets::default())])
.unwrap();
let data = Packet {
meta: Meta {
repair: true,
..Meta::default()
},
..Packet::default()
};
let later_packets = Packets::new(vec![data, Packet::default()]);
s.send(vec![(label1.clone(), 42, later_packets)]).unwrap();
let mut verified_vote_packets = VerifiedVotePackets(HashMap::new());
verified_vote_packets
.receive_and_process_vote_packets(&r, &mut update_version)
.unwrap();
let update_version1 = verified_vote_packets.get_vote_packets(&label1).unwrap().0;
assert_eq!(
update_version1,
verified_vote_packets.get_vote_packets(&label2).unwrap().0
);
assert!(
verified_vote_packets
.get_vote_packets(&label1)
.unwrap()
.2
.packets
.len()
> 1
);
assert_eq!(
verified_vote_packets
.get_vote_packets(&label2)
.unwrap()
.2
.packets
.len(),
0
);
s.send(vec![(label2.clone(), 51, Packets::default())])
.unwrap();
verified_vote_packets
.receive_and_process_vote_packets(&r, &mut update_version)
.unwrap();
let update_version2 = verified_vote_packets.get_vote_packets(&label2).unwrap().0;
assert!(update_version2 > update_version1);
let before = update_version;
assert_matches!(
verified_vote_packets.receive_and_process_vote_packets(&r, &mut update_version),
Err(Error::CrossbeamRecvTimeoutError(RecvTimeoutError::Timeout))
);
assert_eq!(before, update_version);
}
}