Line data Source code
1 : #include "fd_repair_serde.h"
2 : #include "../../ballet/bmtree/fd_bmtree.h"
3 : #include "../../ballet/sha256/fd_sha256.h"
4 :
5 : ulong
6 : fd_repair_pong_ser( fd_repair_pong_t const * self,
7 : fd_ed25519_sig_t const signature,
8 : fd_pubkey_t const * from,
9 0 : uchar buf[ static FD_REPAIR_PONG_SER_SZ ] ) {
10 0 : ulong off = 0UL;
11 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_PONG ); off += sizeof(uint);
12 0 : memcpy( buf+off, from->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
13 0 : memcpy( buf+off, self->hash.uc, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
14 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
15 0 : return off;
16 0 : }
17 :
18 : ulong
19 : fd_repair_req_sig_ser( uchar const * buf,
20 : ulong buf_sz,
21 0 : uchar out[ static FD_REPAIR_SIG_SER_MAX ] ) {
22 0 : memcpy( out, buf, sizeof(uint) );
23 0 : memcpy( out+sizeof(uint), buf+sizeof(uint)+FD_ED25519_SIG_SZ, buf_sz-sizeof(uint)-FD_ED25519_SIG_SZ );
24 0 : return buf_sz-FD_ED25519_SIG_SZ;
25 0 : }
26 :
27 : ulong
28 : fd_repair_req_window_index_ser( fd_repair_shred_t const * self,
29 : fd_ed25519_sig_t const signature,
30 : fd_pubkey_t const * sender,
31 : fd_pubkey_t const * recipient,
32 : ulong timestamp,
33 : uint nonce,
34 0 : uchar buf[ static FD_REPAIR_WINDOW_INDEX_SER_SZ ] ) {
35 0 : ulong off = 0UL;
36 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_WINDOW_INDEX ); off += sizeof(uint);
37 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
38 0 : memcpy( buf+off, sender->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
39 0 : memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
40 0 : FD_STORE( ulong, buf+off, timestamp ); off += sizeof(ulong);
41 0 : FD_STORE( uint, buf+off, nonce ); off += sizeof(uint);
42 0 : FD_STORE( ulong, buf+off, self->slot ); off += sizeof(ulong);
43 0 : FD_STORE( ulong, buf+off, self->shred_idx ); off += sizeof(ulong);
44 0 : return off;
45 0 : }
46 :
47 : ulong
48 : fd_repair_req_highest_window_index_ser( fd_repair_highest_shred_t const * self,
49 : fd_ed25519_sig_t const signature,
50 : fd_pubkey_t const * sender,
51 : fd_pubkey_t const * recipient,
52 : ulong timestamp,
53 : uint nonce,
54 0 : uchar buf[ static FD_REPAIR_HIGHEST_WINDOW_INDEX_SER_SZ ] ) {
55 0 : ulong off = 0UL;
56 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_HIGHEST_WINDOW_INDEX ); off += sizeof(uint);
57 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
58 0 : memcpy( buf+off, sender->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
59 0 : memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
60 0 : FD_STORE( ulong, buf+off, timestamp ); off += sizeof(ulong);
61 0 : FD_STORE( uint, buf+off, nonce ); off += sizeof(uint);
62 0 : FD_STORE( ulong, buf+off, self->slot ); off += sizeof(ulong);
63 0 : FD_STORE( ulong, buf+off, self->shred_idx ); off += sizeof(ulong);
64 0 : return off;
65 0 : }
66 :
67 : ulong
68 : fd_repair_req_orphan_ser( fd_repair_orphan_t const * self,
69 : fd_ed25519_sig_t const signature,
70 : fd_pubkey_t const * sender,
71 : fd_pubkey_t const * recipient,
72 : ulong timestamp,
73 : uint nonce,
74 0 : uchar buf[ static FD_REPAIR_ORPHAN_SER_SZ ] ) {
75 0 : ulong off = 0UL;
76 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_ORPHAN ); off += sizeof(uint);
77 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
78 0 : memcpy( buf+off, sender->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
79 0 : memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
80 0 : FD_STORE( ulong, buf+off, timestamp ); off += sizeof(ulong);
81 0 : FD_STORE( uint, buf+off, nonce ); off += sizeof(uint);
82 0 : FD_STORE( ulong, buf+off, self->slot ); off += sizeof(ulong);
83 0 : return off;
84 0 : }
85 :
86 : ulong
87 : fd_repair_req_parent_and_fec_set_count_ser( fd_repair_parent_fec_set_count_t const * self,
88 : fd_ed25519_sig_t const signature,
89 : fd_pubkey_t const * sender,
90 : fd_pubkey_t const * recipient,
91 : ulong timestamp,
92 : uint nonce,
93 0 : uchar buf[ static FD_REPAIR_PARENT_AND_FEC_SET_COUNT_SER_SZ ] ) {
94 0 : ulong off = 0UL;
95 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_PARENT_AND_FEC_SET_COUNT ); off += sizeof(uint);
96 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
97 0 : memcpy( buf+off, sender->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
98 0 : memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
99 0 : FD_STORE( ulong, buf+off, timestamp ); off += sizeof(ulong);
100 0 : FD_STORE( uint, buf+off, nonce ); off += sizeof(uint);
101 0 : FD_STORE( ulong, buf+off, self->slot ); off += sizeof(ulong);
102 0 : memcpy( buf+off, self->block_id.uc, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
103 0 : return off;
104 0 : }
105 :
106 : ulong
107 : fd_repair_req_fec_set_root_ser( fd_repair_fec_set_root_t const * self,
108 : fd_ed25519_sig_t const signature,
109 : fd_pubkey_t const * sender,
110 : fd_pubkey_t const * recipient,
111 : ulong timestamp,
112 : uint nonce,
113 0 : uchar buf[ static FD_REPAIR_FEC_SET_ROOT_SER_SZ ] ) {
114 0 : ulong off = 0UL;
115 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_FEC_SET_ROOT ); off += sizeof(uint);
116 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
117 0 : memcpy( buf+off, sender->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
118 0 : memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
119 0 : FD_STORE( ulong, buf+off, timestamp ); off += sizeof(ulong);
120 0 : FD_STORE( uint, buf+off, nonce ); off += sizeof(uint);
121 0 : FD_STORE( ulong, buf+off, self->slot ); off += sizeof(ulong);
122 0 : memcpy( buf+off, self->block_id.uc, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
123 0 : FD_STORE( uint, buf+off, self->fec_set_idx ); off += sizeof(uint);
124 0 : return off;
125 0 : }
126 :
127 : ulong
128 : fd_repair_req_window_index_for_block_id_ser( fd_repair_shred_for_block_id_t const * self,
129 : fd_ed25519_sig_t const signature,
130 : fd_pubkey_t const * sender,
131 : fd_pubkey_t const * recipient,
132 : ulong timestamp,
133 : uint nonce,
134 0 : uchar buf[ static FD_REPAIR_WINDOW_INDEX_FOR_BLOCK_ID_SER_SZ ] ) {
135 0 : ulong off = 0UL;
136 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_WINDOW_INDEX_FOR_BLOCK_ID ); off += sizeof(uint);
137 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
138 0 : memcpy( buf+off, sender->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
139 0 : memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
140 0 : FD_STORE( ulong, buf+off, timestamp ); off += sizeof(ulong);
141 0 : FD_STORE( uint, buf+off, nonce ); off += sizeof(uint);
142 0 : FD_STORE( ulong, buf+off, self->slot ); off += sizeof(ulong);
143 0 : FD_STORE( uint, buf+off, self->shred_idx ); off += sizeof(uint);
144 0 : memcpy( buf+off, self->block_id.uc, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
145 0 : return off;
146 0 : }
147 :
148 : ulong
149 : fd_repair_req_window_index_de( uchar const * buf,
150 : ulong buf_sz,
151 : fd_repair_shred_t * self,
152 : fd_ed25519_sig_t signature,
153 : fd_pubkey_t * sender,
154 : fd_pubkey_t * recipient,
155 : ulong * timestamp,
156 0 : uint * nonce ) {
157 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_WINDOW_INDEX_SER_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_WINDOW_INDEX ) ) return 0UL;
158 0 : ulong off = sizeof(uint);
159 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
160 0 : memcpy( sender->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
161 0 : memcpy( recipient->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
162 0 : *timestamp = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
163 0 : *nonce = FD_LOAD( uint, buf+off ); off += sizeof(uint);
164 0 : self->slot = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
165 0 : self->shred_idx = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
166 0 : return off;
167 0 : }
168 :
169 : ulong
170 : fd_repair_req_highest_window_index_de( uchar const * buf,
171 : ulong buf_sz,
172 : fd_repair_highest_shred_t * self,
173 : fd_ed25519_sig_t signature,
174 : fd_pubkey_t * sender,
175 : fd_pubkey_t * recipient,
176 : ulong * timestamp,
177 0 : uint * nonce ) {
178 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_HIGHEST_WINDOW_INDEX_SER_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_HIGHEST_WINDOW_INDEX ) ) return 0UL;
179 0 : ulong off = sizeof(uint);
180 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
181 0 : memcpy( sender->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
182 0 : memcpy( recipient->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
183 0 : *timestamp = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
184 0 : *nonce = FD_LOAD( uint, buf+off ); off += sizeof(uint);
185 0 : self->slot = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
186 0 : self->shred_idx = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
187 0 : return off;
188 0 : }
189 :
190 : ulong
191 : fd_repair_req_orphan_de( uchar const * buf,
192 : ulong buf_sz,
193 : fd_repair_orphan_t * self,
194 : fd_ed25519_sig_t signature,
195 : fd_pubkey_t * sender,
196 : fd_pubkey_t * recipient,
197 : ulong * timestamp,
198 0 : uint * nonce ) {
199 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_ORPHAN_SER_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_ORPHAN ) ) return 0UL;
200 0 : ulong off = sizeof(uint);
201 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
202 0 : memcpy( sender->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
203 0 : memcpy( recipient->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
204 0 : *timestamp = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
205 0 : *nonce = FD_LOAD( uint, buf+off ); off += sizeof(uint);
206 0 : self->slot = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
207 0 : return off;
208 0 : }
209 :
210 : ulong
211 : fd_repair_req_parent_and_fec_set_count_de( uchar const * buf,
212 : ulong buf_sz,
213 : fd_repair_parent_fec_set_count_t * self,
214 : fd_ed25519_sig_t signature,
215 : fd_pubkey_t * sender,
216 : fd_pubkey_t * recipient,
217 : ulong * timestamp,
218 0 : uint * nonce ) {
219 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_PARENT_AND_FEC_SET_COUNT_SER_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_PARENT_AND_FEC_SET_COUNT ) ) return 0UL;
220 0 : ulong off = sizeof(uint);
221 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
222 0 : memcpy( sender->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
223 0 : memcpy( recipient->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
224 0 : *timestamp = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
225 0 : *nonce = FD_LOAD( uint, buf+off ); off += sizeof(uint);
226 0 : self->slot = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
227 0 : memcpy( self->block_id.uc, buf+off, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
228 0 : return off;
229 0 : }
230 :
231 : ulong
232 : fd_repair_req_fec_set_root_de( uchar const * buf,
233 : ulong buf_sz,
234 : fd_repair_fec_set_root_t * self,
235 : fd_ed25519_sig_t signature,
236 : fd_pubkey_t * sender,
237 : fd_pubkey_t * recipient,
238 : ulong * timestamp,
239 0 : uint * nonce ) {
240 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_FEC_SET_ROOT_SER_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_FEC_SET_ROOT ) ) return 0UL;
241 0 : ulong off = sizeof(uint);
242 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
243 0 : memcpy( sender->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
244 0 : memcpy( recipient->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
245 0 : *timestamp = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
246 0 : *nonce = FD_LOAD( uint, buf+off ); off += sizeof(uint);
247 0 : self->slot = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
248 0 : memcpy( self->block_id.uc, buf+off, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
249 0 : self->fec_set_idx = FD_LOAD( uint, buf+off ); off += sizeof(uint);
250 0 : return off;
251 0 : }
252 :
253 : ulong
254 : fd_repair_req_window_index_for_block_id_de( uchar const * buf,
255 : ulong buf_sz,
256 : fd_repair_shred_for_block_id_t * self,
257 : fd_ed25519_sig_t signature,
258 : fd_pubkey_t * sender,
259 : fd_pubkey_t * recipient,
260 : ulong * timestamp,
261 0 : uint * nonce ) {
262 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_WINDOW_INDEX_FOR_BLOCK_ID_SER_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_WINDOW_INDEX_FOR_BLOCK_ID ) ) return 0UL;
263 0 : ulong off = sizeof(uint);
264 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
265 0 : memcpy( sender->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
266 0 : memcpy( recipient->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
267 0 : *timestamp = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
268 0 : *nonce = FD_LOAD( uint, buf+off ); off += sizeof(uint);
269 0 : self->slot = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
270 0 : self->shred_idx = FD_LOAD( uint, buf+off ); off += sizeof(uint);
271 0 : memcpy( self->block_id.uc, buf+off, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
272 0 : return off;
273 0 : }
274 :
275 : ulong
276 : fd_repair_pong_de( uchar const * buf,
277 : ulong buf_sz,
278 : fd_repair_pong_t * self,
279 : fd_ed25519_sig_t signature,
280 0 : fd_pubkey_t * from ) {
281 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_PONG_SER_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_PONG ) ) return 0UL;
282 0 : ulong off = sizeof(uint);
283 0 : memcpy( from->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
284 0 : memcpy( self->hash.uc, buf+off, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
285 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
286 0 : return off;
287 0 : }
288 :
289 : ulong
290 : fd_repair_ping_de( uchar const * buf,
291 : ulong buf_sz,
292 : fd_pubkey_t * from,
293 : fd_hash_t * token,
294 0 : fd_ed25519_sig_t signature ) {
295 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_PING_DE_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_PING ) ) return 0UL;
296 0 : ulong off = sizeof(uint);
297 0 : memcpy( from->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
298 0 : memcpy( token->uc, buf+off, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
299 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
300 0 : return off;
301 0 : }
302 :
303 : ulong
304 : fd_repair_block_id_ping_de( uchar const * buf,
305 : ulong buf_sz,
306 : fd_pubkey_t * from,
307 : fd_hash_t * token,
308 0 : fd_ed25519_sig_t signature ) {
309 0 : if( FD_UNLIKELY( buf_sz<FD_REPAIR_PING_DE_SZ || FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_BLOCK_ID_PING ) ) return 0UL;
310 0 : ulong off = sizeof(uint);
311 0 : memcpy( from->uc, buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
312 0 : memcpy( token->uc, buf+off, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
313 0 : memcpy( signature, buf+off, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
314 0 : return off;
315 0 : }
316 :
317 : ulong
318 : fd_repair_ping_ser( fd_pubkey_t const * from,
319 : fd_hash_t const * token,
320 : fd_ed25519_sig_t const signature,
321 0 : uchar buf[ static FD_REPAIR_PING_DE_SZ ] ) {
322 0 : ulong off = 0UL;
323 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_PING ); off += sizeof(uint);
324 0 : memcpy( buf+off, from->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
325 0 : memcpy( buf+off, token->uc, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
326 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
327 0 : return off;
328 0 : }
329 :
330 : ulong
331 : fd_repair_block_id_ping_ser( fd_pubkey_t const * from,
332 : fd_hash_t const * token,
333 : fd_ed25519_sig_t const signature,
334 0 : uchar buf[ static FD_REPAIR_PING_DE_SZ ] ) {
335 0 : ulong off = 0UL;
336 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_BLOCK_ID_PING ); off += sizeof(uint);
337 0 : memcpy( buf+off, from->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
338 0 : memcpy( buf+off, token->uc, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
339 0 : memcpy( buf+off, signature, FD_ED25519_SIG_SZ ); off += FD_ED25519_SIG_SZ;
340 0 : return off;
341 0 : }
342 :
343 : ulong
344 : fd_repair_res_parent_fec_set_count_de( uchar const * buf,
345 : ulong buf_sz,
346 : uint * fec_set_cnt,
347 : ulong * parent_slot,
348 : fd_hash_t * parent_block_id,
349 : uchar proof[ static FD_REPAIR_PROOF_MAX ],
350 : ulong * proof_sz,
351 0 : uint * nonce ) {
352 0 : if( FD_UNLIKELY( buf_sz<sizeof(uint)+sizeof(uint)+sizeof(ulong)+sizeof(fd_hash_t)+sizeof(ulong) ) ) return 0UL;
353 0 : if( FD_UNLIKELY( FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_PARENT_FEC_SET_COUNT_RES ) ) return 0UL;
354 0 : ulong off = sizeof(uint);
355 0 : *fec_set_cnt = FD_LOAD( uint, buf+off ); off += sizeof(uint);
356 0 : *parent_slot = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
357 0 : memcpy( parent_block_id->uc, buf+off, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
358 0 : *proof_sz = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
359 0 : if( FD_UNLIKELY( *proof_sz>FD_REPAIR_PROOF_MAX || *proof_sz+sizeof(uint)>buf_sz-off ) ) return 0UL;
360 0 : memcpy( proof, buf+off, *proof_sz ); off += *proof_sz;
361 0 : *nonce = FD_LOAD( uint, buf+off ); off += sizeof(uint);
362 0 : return off;
363 0 : }
364 :
365 : ulong
366 : fd_repair_res_fec_set_root_de( uchar const * buf,
367 : ulong buf_sz,
368 : uchar fec_set_root[ static FD_SHRED_MERKLE_NODE_SZ ],
369 : uchar proof[ static FD_REPAIR_PROOF_MAX ],
370 : ulong * proof_sz,
371 0 : uint * nonce ) {
372 0 : if( FD_UNLIKELY( buf_sz<sizeof(uint)+FD_SHRED_MERKLE_NODE_SZ+sizeof(ulong) ) ) return 0UL;
373 0 : if( FD_UNLIKELY( FD_LOAD( uint, buf )!=FD_REPAIR_SERDE_TAG_FEC_SET_ROOT_RES ) ) return 0UL;
374 0 : ulong off = sizeof(uint);
375 0 : memcpy( fec_set_root, buf+off, FD_SHRED_MERKLE_NODE_SZ ); off += FD_SHRED_MERKLE_NODE_SZ;
376 0 : *proof_sz = FD_LOAD( ulong, buf+off ); off += sizeof(ulong);
377 0 : if( FD_UNLIKELY( *proof_sz>FD_REPAIR_PROOF_MAX || *proof_sz+sizeof(uint)>buf_sz-off ) ) return 0UL;
378 0 : memcpy( proof, buf+off, *proof_sz ); off += *proof_sz;
379 0 : *nonce = FD_LOAD( uint, buf+off ); off += sizeof(uint);
380 0 : return off;
381 0 : }
382 :
383 : ulong
384 : fd_repair_res_parent_fec_set_count_ser( uint fec_set_cnt,
385 : ulong parent_slot,
386 : fd_hash_t const * parent_block_id,
387 : uchar const * proof,
388 : ulong proof_sz,
389 : uint nonce,
390 : uchar * buf,
391 0 : ulong buf_sz ) {
392 0 : if( FD_UNLIKELY( proof_sz>FD_REPAIR_PROOF_MAX ) ) return 0UL;
393 0 : if( FD_UNLIKELY( buf_sz<sizeof(uint)+sizeof(uint)+sizeof(ulong)+sizeof(fd_hash_t)+sizeof(ulong)+proof_sz+sizeof(uint) ) ) return 0UL;
394 0 : ulong off = 0UL;
395 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_PARENT_FEC_SET_COUNT_RES ); off += sizeof(uint);
396 0 : FD_STORE( uint, buf+off, fec_set_cnt ); off += sizeof(uint);
397 0 : FD_STORE( ulong, buf+off, parent_slot ); off += sizeof(ulong);
398 0 : memcpy( buf+off, parent_block_id->uc, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
399 0 : FD_STORE( ulong, buf+off, proof_sz ); off += sizeof(ulong);
400 0 : memcpy( buf+off, proof, proof_sz ); off += proof_sz;
401 0 : FD_STORE( uint, buf+off, nonce ); off += sizeof(uint);
402 0 : return off;
403 0 : }
404 :
405 : ulong
406 : fd_repair_res_fec_set_root_ser( uchar const fec_set_root[ static FD_SHRED_MERKLE_NODE_SZ ],
407 : uchar const * proof,
408 : ulong proof_sz,
409 : uint nonce,
410 : uchar * buf,
411 0 : ulong buf_sz ) {
412 0 : if( FD_UNLIKELY( proof_sz>FD_REPAIR_PROOF_MAX ) ) return 0UL;
413 0 : if( FD_UNLIKELY( buf_sz<sizeof(uint)+FD_SHRED_MERKLE_NODE_SZ+sizeof(ulong)+proof_sz+sizeof(uint) ) ) return 0UL;
414 0 : ulong off = 0UL;
415 0 : FD_STORE( uint, buf+off, FD_REPAIR_SERDE_TAG_FEC_SET_ROOT_RES ); off += sizeof(uint);
416 0 : memcpy( buf+off, fec_set_root, FD_SHRED_MERKLE_NODE_SZ ); off += FD_SHRED_MERKLE_NODE_SZ;
417 0 : FD_STORE( ulong, buf+off, proof_sz ); off += sizeof(ulong);
418 0 : memcpy( buf+off, proof, proof_sz ); off += proof_sz;
419 0 : FD_STORE( uint, buf+off, nonce ); off += sizeof(uint);
420 0 : return off;
421 0 : }
422 :
423 : int
424 : fd_repair_res_parent_fec_set_count_verify( uint fec_set_cnt,
425 : ulong parent_slot,
426 : fd_hash_t const * parent_block_id,
427 : uchar const * proof,
428 : ulong proof_sz,
429 0 : fd_hash_t const * block_id ) {
430 0 : if( FD_UNLIKELY( !fec_set_cnt || fec_set_cnt>FD_FEC_BLK_MAX ) ) return -1;
431 0 : if( FD_UNLIKELY( proof_sz!=( fd_bmtree_depth( fec_set_cnt+1UL )-1UL )*FD_SHRED_MERKLE_NODE_SZ ) ) return -1;
432 :
433 0 : fd_bmtree_node_t leaf[1];
434 0 : fd_sha256_t sha[1];
435 0 : fd_sha256_init ( sha );
436 0 : fd_sha256_append( sha, &parent_slot, sizeof(ulong) );
437 0 : fd_sha256_append( sha, parent_block_id->uc, sizeof(fd_hash_t) );
438 0 : fd_sha256_append( sha, &fec_set_cnt, sizeof(uint) );
439 0 : fd_sha256_fini ( sha, leaf->hash );
440 :
441 0 : fd_bmtree_node_t root[1];
442 0 : if( FD_UNLIKELY( !fd_bmtree_from_proof( leaf, fec_set_cnt, root, proof, proof_sz/FD_SHRED_MERKLE_NODE_SZ, FD_SHRED_MERKLE_NODE_SZ, FD_BMTREE_LONG_PREFIX_SZ ) ) ) return -1;
443 0 : if( FD_UNLIKELY( memcmp( root->hash, block_id->uc, sizeof(fd_hash_t) ) ) ) return -1;
444 0 : return 0;
445 0 : }
446 :
447 : int
448 : fd_repair_res_fec_set_root_verify( uchar const fec_set_root[ static FD_SHRED_MERKLE_NODE_SZ ],
449 : uchar const * proof,
450 : ulong proof_sz,
451 : fd_hash_t const * block_id,
452 : uint fec_set_idx,
453 0 : uint fec_set_cnt ) {
454 0 : if( FD_UNLIKELY( !fec_set_cnt || fec_set_cnt>FD_FEC_BLK_MAX ) ) return -1;
455 0 : if( FD_UNLIKELY( fec_set_idx%FD_FEC_SHRED_CNT || fec_set_idx/FD_FEC_SHRED_CNT>=fec_set_cnt ) ) return -1;
456 0 : if( FD_UNLIKELY( proof_sz!=( fd_bmtree_depth( fec_set_cnt+1UL )-1UL )*FD_SHRED_MERKLE_NODE_SZ ) ) return -1;
457 :
458 0 : fd_bmtree_node_t leaf[1] = {0};
459 0 : memcpy( leaf->hash, fec_set_root, FD_SHRED_MERKLE_NODE_SZ );
460 :
461 0 : fd_bmtree_node_t root[1];
462 0 : if( FD_UNLIKELY( !fd_bmtree_from_proof( leaf, fec_set_idx/FD_FEC_SHRED_CNT, root, proof, proof_sz/FD_SHRED_MERKLE_NODE_SZ, FD_SHRED_MERKLE_NODE_SZ, FD_BMTREE_LONG_PREFIX_SZ ) ) ) return -1;
463 0 : if( FD_UNLIKELY( memcmp( root->hash, block_id->uc, sizeof(fd_hash_t) ) ) ) return -1;
464 0 : return 0;
465 0 : }
|