forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shred.rs
263 lines (243 loc) · 9.5 KB
/
shred.rs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#![allow(clippy::arithmetic_side_effects)]
use {
solana_entry::entry::Entry,
solana_ledger::shred::{
max_entries_per_n_shred, verify_test_data_shred, ProcessShredsStats, ReedSolomonCache,
Shred, Shredder, DATA_SHREDS_PER_FEC_BLOCK, LEGACY_SHRED_DATA_CAPACITY,
},
solana_sdk::{
clock::Slot,
hash::Hash,
signature::{Keypair, Signer},
system_transaction,
},
std::{
collections::{BTreeMap, HashSet},
convert::TryInto,
sync::Arc,
},
};
type IndexShredsMap = BTreeMap<u32, Vec<Shred>>;
#[test]
fn test_multi_fec_block_coding() {
let keypair = Arc::new(Keypair::new());
let slot = 0x1234_5678_9abc_def0;
let shredder = Shredder::new(slot, slot - 5, 0, 0).unwrap();
let num_fec_sets = 100;
let num_data_shreds = DATA_SHREDS_PER_FEC_BLOCK * num_fec_sets;
let keypair0 = Keypair::new();
let keypair1 = Keypair::new();
let tx0 = system_transaction::transfer(&keypair0, &keypair1.pubkey(), 1, Hash::default());
let entry = Entry::new(&Hash::default(), 1, vec![tx0]);
let num_entries = max_entries_per_n_shred(
&entry,
num_data_shreds as u64,
Some(LEGACY_SHRED_DATA_CAPACITY),
);
let entries: Vec<_> = (0..num_entries)
.map(|_| {
let keypair0 = Keypair::new();
let keypair1 = Keypair::new();
let tx0 =
system_transaction::transfer(&keypair0, &keypair1.pubkey(), 1, Hash::default());
Entry::new(&Hash::default(), 1, vec![tx0])
})
.collect();
let reed_solomon_cache = ReedSolomonCache::default();
let serialized_entries = bincode::serialize(&entries).unwrap();
let (data_shreds, coding_shreds) = shredder.entries_to_shreds(
&keypair,
&entries,
true, // is_last_in_slot
None, // chained_merkle_root
0, // next_shred_index
0, // next_code_index
false, // merkle_variant
&reed_solomon_cache,
&mut ProcessShredsStats::default(),
);
let next_index = data_shreds.last().unwrap().index() + 1;
assert_eq!(next_index as usize, num_data_shreds);
assert_eq!(data_shreds.len(), num_data_shreds);
assert_eq!(coding_shreds.len(), num_data_shreds);
for c in &coding_shreds {
assert!(!c.is_data());
}
let mut all_shreds = vec![];
for i in 0..num_fec_sets {
let shred_start_index = DATA_SHREDS_PER_FEC_BLOCK * i;
let end_index = shred_start_index + DATA_SHREDS_PER_FEC_BLOCK - 1;
let fec_set_shreds = data_shreds[shred_start_index..=end_index]
.iter()
.cloned()
.chain(coding_shreds[shred_start_index..=end_index].iter().cloned())
.collect::<Vec<_>>();
let mut shred_info: Vec<Shred> = fec_set_shreds
.iter()
.enumerate()
.filter_map(|(i, b)| if i % 2 != 0 { Some(b.clone()) } else { None })
.collect();
let recovered_data =
Shredder::try_recovery(shred_info.clone(), &reed_solomon_cache).unwrap();
for (i, recovered_shred) in recovered_data.into_iter().enumerate() {
let index = shred_start_index + (i * 2);
verify_test_data_shred(
&recovered_shred,
index.try_into().unwrap(),
slot,
slot - 5,
&keypair.pubkey(),
true,
index == end_index,
index == end_index,
);
shred_info.insert(i * 2, recovered_shred);
}
all_shreds.extend(shred_info.into_iter().take(DATA_SHREDS_PER_FEC_BLOCK));
}
let result = Shredder::deshred(&all_shreds[..]).unwrap();
assert_eq!(serialized_entries[..], result[..serialized_entries.len()]);
}
#[test]
fn test_multi_fec_block_different_size_coding() {
let slot = 0x1234_5678_9abc_def0;
let parent_slot = slot - 5;
let keypair = Arc::new(Keypair::new());
let (fec_data, fec_coding, num_shreds_per_iter) =
setup_different_sized_fec_blocks(slot, parent_slot, keypair.clone());
let total_num_data_shreds: usize = fec_data.values().map(|x| x.len()).sum();
let reed_solomon_cache = ReedSolomonCache::default();
// Test recovery
for (fec_data_shreds, fec_coding_shreds) in fec_data.values().zip(fec_coding.values()) {
let first_data_index = fec_data_shreds.first().unwrap().index() as usize;
let all_shreds: Vec<Shred> = fec_data_shreds
.iter()
.step_by(2)
.chain(fec_coding_shreds.iter().step_by(2))
.cloned()
.collect();
let recovered_data = Shredder::try_recovery(all_shreds, &reed_solomon_cache).unwrap();
// Necessary in order to ensure the last shred in the slot
// is part of the recovered set, and that the below `index`
// calculation in the loop is correct
assert!(fec_data_shreds.len() % 2 == 0);
for (i, recovered_shred) in recovered_data.into_iter().enumerate() {
let index = first_data_index + (i * 2) + 1;
verify_test_data_shred(
&recovered_shred,
index.try_into().unwrap(),
slot,
parent_slot,
&keypair.pubkey(),
true,
index == total_num_data_shreds - 1,
index % num_shreds_per_iter == num_shreds_per_iter - 1,
);
}
}
}
fn sort_data_coding_into_fec_sets(
data_shreds: Vec<Shred>,
coding_shreds: Vec<Shred>,
fec_data: &mut IndexShredsMap,
fec_coding: &mut IndexShredsMap,
data_slot_and_index: &mut HashSet<(Slot, u32)>,
coding_slot_and_index: &mut HashSet<(Slot, u32)>,
) {
for shred in data_shreds {
assert!(shred.is_data());
let key = (shred.slot(), shred.index());
// Make sure there are no duplicates for same key
assert!(!data_slot_and_index.contains(&key));
data_slot_and_index.insert(key);
let fec_entry = fec_data.entry(shred.fec_set_index()).or_default();
fec_entry.push(shred);
}
for shred in coding_shreds {
assert!(!shred.is_data());
let key = (shred.slot(), shred.index());
// Make sure there are no duplicates for same key
assert!(!coding_slot_and_index.contains(&key));
coding_slot_and_index.insert(key);
let fec_entry = fec_coding.entry(shred.fec_set_index()).or_default();
fec_entry.push(shred);
}
}
#[allow(clippy::assertions_on_constants)]
fn setup_different_sized_fec_blocks(
slot: Slot,
parent_slot: Slot,
keypair: Arc<Keypair>,
) -> (IndexShredsMap, IndexShredsMap, usize) {
let shredder = Shredder::new(slot, parent_slot, 0, 0).unwrap();
let keypair0 = Keypair::new();
let keypair1 = Keypair::new();
let tx0 = system_transaction::transfer(&keypair0, &keypair1.pubkey(), 1, Hash::default());
let entry = Entry::new(&Hash::default(), 1, vec![tx0]);
// Make enough entries for `DATA_SHREDS_PER_FEC_BLOCK + 2` shreds so one
// fec set will have `DATA_SHREDS_PER_FEC_BLOCK` shreds and the next
// will have 2 shreds.
assert!(DATA_SHREDS_PER_FEC_BLOCK > 2);
let num_shreds_per_iter = DATA_SHREDS_PER_FEC_BLOCK + 2;
let num_entries = max_entries_per_n_shred(
&entry,
num_shreds_per_iter as u64,
Some(LEGACY_SHRED_DATA_CAPACITY),
);
let entries: Vec<_> = (0..num_entries)
.map(|_| {
let keypair0 = Keypair::new();
let keypair1 = Keypair::new();
let tx0 =
system_transaction::transfer(&keypair0, &keypair1.pubkey(), 1, Hash::default());
Entry::new(&Hash::default(), 1, vec![tx0])
})
.collect();
// Run the shredder twice, generate data and coding shreds
let mut next_shred_index = 0;
let mut next_code_index = 0;
let mut fec_data = BTreeMap::new();
let mut fec_coding = BTreeMap::new();
let mut data_slot_and_index = HashSet::new();
let mut coding_slot_and_index = HashSet::new();
let total_num_data_shreds: usize = 2 * num_shreds_per_iter;
let reed_solomon_cache = ReedSolomonCache::default();
for i in 0..2 {
let is_last = i == 1;
let (data_shreds, coding_shreds) = shredder.entries_to_shreds(
&keypair,
&entries,
is_last,
None, // chained_merkle_root
next_shred_index,
next_code_index,
false, // merkle_variant
&reed_solomon_cache,
&mut ProcessShredsStats::default(),
);
for shred in &data_shreds {
if (shred.index() as usize) == total_num_data_shreds - 1 {
assert!(shred.data_complete());
assert!(shred.last_in_slot());
} else if (shred.index() as usize) % num_shreds_per_iter == num_shreds_per_iter - 1 {
assert!(shred.data_complete());
} else {
assert!(!shred.data_complete());
assert!(!shred.last_in_slot());
}
}
assert_eq!(data_shreds.len(), num_shreds_per_iter);
next_shred_index = data_shreds.last().unwrap().index() + 1;
next_code_index = coding_shreds.last().unwrap().index() + 1;
sort_data_coding_into_fec_sets(
data_shreds,
coding_shreds,
&mut fec_data,
&mut fec_coding,
&mut data_slot_and_index,
&mut coding_slot_and_index,
);
}
assert_eq!(fec_data.len(), fec_coding.len());
(fec_data, fec_coding, num_shreds_per_iter)
}