Line data Source code
1 : #include "../../../../disco/tiles.h"
2 :
3 : #include "generated/shred_seccomp.h"
4 : #include "../../../../disco/shred/fd_shredder.h"
5 : #include "../../../../disco/shred/fd_shred_dest.h"
6 : #include "../../../../disco/shred/fd_fec_resolver.h"
7 : #include "../../../../disco/shred/fd_stake_ci.h"
8 : #include "../../../../disco/keyguard/fd_keyload.h"
9 : #include "../../../../disco/keyguard/fd_keyguard.h"
10 : #include "../../../../disco/keyguard/fd_keyswitch.h"
11 : #include "../../../../flamenco/leaders/fd_leaders.h"
12 : #include "../../../../disco/fd_disco.h"
13 :
14 : #include "../../../../util/net/fd_net_headers.h"
15 :
16 : #include <linux/unistd.h>
17 :
18 : /* The shred tile handles shreds from two data sources: shreds
19 : generated from microblocks from the banking tile, and shreds
20 : retransmitted from the network.
21 :
22 : They have rather different semantics, but at the end of the day, they
23 : both result in a bunch of shreds and FEC sets that need to be sent to
24 : the blockstore and on the network, which is why one tile handles
25 : both.
26 :
27 : We segment the memory for the two types of shreds into two halves of
28 : a dcache because they follow somewhat different flow control
29 : patterns. For flow control, the normal guarantee we want to provide
30 : is that the dcache entry is not overwritten unless the mcache entry
31 : has also been overwritten. The normal way to do this when using both
32 : cyclically and with a 1-to-1 mapping is to make the dcache at least
33 : `burst` entries bigger than the mcache.
34 :
35 : In this tile, we use one output mcache with one output dcache (which
36 : is logically partitioned into two) for the two sources of data. The
37 : worst case for flow control is when we're only sending with one of
38 : the dcache partitions at a time though, so we can consider them
39 : separately.
40 :
41 : From bank: Every FEC set triggers at least two mcache entries (one
42 : for parity and one for data), so at most, we have ceil(mcache
43 : depth/2) FEC sets exposed. This means we need to decompose dcache
44 : into at least ceil(mcache depth/2)+1 FEC sets.
45 :
46 : From the network: The FEC resolver doesn't use a cyclic order, but it
47 : does promise that once it returns an FEC set, it will return at least
48 : complete_depth FEC sets before returning it again. This means we
49 : want at most complete_depth-1 FEC sets exposed, so
50 : complete_depth=ceil(mcache depth/2)+1 FEC sets as above. The FEC
51 : resolver has the ability to keep individual shreds for partial_depth
52 : calls, but because in this version of the shred tile, we send each
53 : shred to all its destinations as soon as we get it, we don't need
54 : that functionality, so we set partial_depth=1.
55 :
56 : Adding these up, we get 2*ceil(mcache_depth/2)+3+fec_resolver_depth
57 : FEC sets, which is no more than mcache_depth+4+fec_resolver_depth.
58 : Each FEC is paired with 4 fd_shred34_t structs, so that means we need
59 : to decompose the dcache into 4*mcache_depth + 4*fec_resolver_depth +
60 : 16 fd_shred34_t structs. */
61 :
62 :
63 : /* The memory this tile uses is a bit complicated and has some logical
64 : aliasing to facilitate zero-copy use. We have a dcache containing
65 : fd_shred34_t objects, which are basically 34 fd_shred_t objects
66 : padded to their max size, where 34 is set so that the size of the
67 : fd_shred34_t object (including some metadata) is less than
68 : USHORT_MAX, which facilitates sending it using Tango. Then, for each
69 : set of 4 consecutive fd_shred34_t objects, we have an fd_fec_set_t.
70 : The first 34 data shreds point to the payload section of the payload
71 : section of each of the packets in the first fd_shred34_t. The other
72 : 33 data shreds point into the second fd_shred34_t. Similar for the
73 : parity shreds pointing into the third and fourth fd_shred34_t. */
74 :
75 : /* There's nothing deep about this max, but I just find it easier to
76 : have a max and use statically sized arrays than alloca. */
77 : #define MAX_BANK_CNT 64UL
78 :
79 : /* MAX_SHRED_DESTS indicates the maximum number of destinations (i.e. a
80 : pubkey -> ip, port) that the shred tile can keep track of. */
81 0 : #define MAX_SHRED_DESTS 40200UL
82 :
83 : #define FD_SHRED_TILE_SCRATCH_ALIGN 128UL
84 :
85 0 : #define IN_KIND_CONTACT (0UL)
86 0 : #define IN_KIND_STAKE (1UL)
87 0 : #define IN_KIND_POH (2UL)
88 0 : #define IN_KIND_NET (3UL)
89 0 : #define IN_KIND_SIGN (4UL)
90 :
91 0 : #define STORE_OUT_IDX 0
92 0 : #define NET_OUT_IDX 1
93 0 : #define SIGN_OUT_IDX 2
94 :
95 : #define MAX_SLOTS_PER_EPOCH 432000UL
96 :
97 0 : #define DCACHE_ENTRIES_PER_FEC_SET (4UL)
98 : FD_STATIC_ASSERT( sizeof(fd_shred34_t) < USHORT_MAX, shred_34 );
99 : FD_STATIC_ASSERT( 34*DCACHE_ENTRIES_PER_FEC_SET >= FD_REEDSOL_DATA_SHREDS_MAX+FD_REEDSOL_PARITY_SHREDS_MAX, shred_34 );
100 : FD_STATIC_ASSERT( sizeof(fd_shred34_t) == FD_SHRED_STORE_MTU, shred_34 );
101 :
102 : FD_STATIC_ASSERT( sizeof(fd_entry_batch_meta_t)==24UL, poh_shred_mtu );
103 :
104 0 : #define FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT 2
105 :
106 : typedef struct {
107 : fd_wksp_t * mem;
108 : ulong chunk0;
109 : ulong wmark;
110 : } fd_shred_in_ctx_t;
111 :
112 : typedef struct {
113 : fd_shredder_t * shredder;
114 : fd_fec_resolver_t * resolver;
115 : fd_pubkey_t identity_key[1]; /* Just the public key */
116 :
117 : ulong round_robin_id;
118 : ulong round_robin_cnt;
119 : /* Number of batches shredded from PoH during the current slot.
120 : This should be the same for all the shred tiles. */
121 : ulong batch_cnt;
122 : /* Slot of the most recent microblock we've seen from PoH,
123 : or 0 if we haven't seen one yet */
124 : ulong slot;
125 :
126 : fd_keyswitch_t * keyswitch;
127 : fd_keyguard_client_t keyguard_client[1];
128 :
129 : /* shred34 and fec_sets are very related: fec_sets[i] has pointers
130 : to the shreds in shred34[4*i + k] for k=0,1,2,3. */
131 : fd_shred34_t * shred34;
132 : fd_fec_set_t * fec_sets;
133 :
134 : fd_stake_ci_t * stake_ci;
135 : /* These are used in between during_frag and after_frag */
136 : fd_shred_dest_weighted_t * new_dest_ptr;
137 : ulong new_dest_cnt;
138 : ulong shredded_txn_cnt;
139 :
140 : ulong poh_in_expect_seq;
141 :
142 : ushort net_id;
143 :
144 : int skip_frag;
145 :
146 : fd_net_hdrs_t data_shred_net_hdr [1];
147 : fd_net_hdrs_t parity_shred_net_hdr[1];
148 :
149 : fd_wksp_t * shred_store_wksp;
150 :
151 : ulong shredder_fec_set_idx; /* In [0, shredder_max_fec_set_idx) */
152 : ulong shredder_max_fec_set_idx; /* exclusive */
153 :
154 : ulong send_fec_set_idx;
155 : ulong tsorig; /* timestamp of the last packet in compressed form */
156 :
157 : /* Includes Ethernet, IP, UDP headers */
158 : ulong shred_buffer_sz;
159 : uchar shred_buffer[ FD_NET_MTU ];
160 :
161 :
162 : fd_shred_in_ctx_t in[ 32 ];
163 : int in_kind[ 32 ];
164 :
165 : fd_frag_meta_t * net_out_mcache;
166 : ulong * net_out_sync;
167 : ulong net_out_depth;
168 : ulong net_out_seq;
169 :
170 : fd_wksp_t * net_out_mem;
171 : ulong net_out_chunk0;
172 : ulong net_out_wmark;
173 : ulong net_out_chunk;
174 :
175 : fd_wksp_t * store_out_mem;
176 : ulong store_out_chunk0;
177 : ulong store_out_wmark;
178 : ulong store_out_chunk;
179 :
180 : struct {
181 : fd_histf_t contact_info_cnt[ 1 ];
182 : fd_histf_t batch_sz[ 1 ];
183 : fd_histf_t batch_microblock_cnt[ 1 ];
184 : fd_histf_t shredding_timing[ 1 ];
185 : fd_histf_t add_shred_timing[ 1 ];
186 : ulong shred_processing_result[ FD_FEC_RESOLVER_ADD_SHRED_RETVAL_CNT+FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT ];
187 : } metrics[ 1 ];
188 :
189 : struct {
190 : ulong txn_cnt;
191 : ulong pos; /* in payload, so 0<=pos<63671 */
192 : ulong slot; /* set to 0 when pos==0 */
193 : union {
194 : struct {
195 : ulong microblock_cnt;
196 : uchar payload[ 63679UL - 8UL ];
197 : };
198 : uchar raw[ 63679UL ]; /* The largest that fits in 1 FEC set */
199 : };
200 : } pending_batch;
201 : } fd_shred_ctx_t;
202 :
203 : /* PENDING_BATCH_WMARK: Following along the lines of dcache, batch
204 : microblocks until either the slot ends or we excede the watermark.
205 : We know that if we're <= watermark, we can always accept a message of
206 : maximum size. */
207 0 : #define PENDING_BATCH_WMARK (63679UL - 8UL - FD_POH_SHRED_MTU)
208 :
209 : FD_FN_CONST static inline ulong
210 3 : scratch_align( void ) {
211 3 : return 128UL;
212 3 : }
213 :
214 : FD_FN_PURE static inline ulong
215 3 : scratch_footprint( fd_topo_tile_t const * tile ) {
216 :
217 3 : ulong fec_resolver_footprint = fd_fec_resolver_footprint( tile->shred.fec_resolver_depth, 1UL, tile->shred.depth,
218 3 : 128UL * tile->shred.fec_resolver_depth );
219 3 : ulong fec_set_cnt = tile->shred.depth + tile->shred.fec_resolver_depth + 4UL;
220 :
221 3 : ulong l = FD_LAYOUT_INIT;
222 3 : l = FD_LAYOUT_APPEND( l, alignof(fd_shred_ctx_t), sizeof(fd_shred_ctx_t) );
223 3 : l = FD_LAYOUT_APPEND( l, fd_stake_ci_align(), fd_stake_ci_footprint() );
224 3 : l = FD_LAYOUT_APPEND( l, fd_fec_resolver_align(), fec_resolver_footprint );
225 3 : l = FD_LAYOUT_APPEND( l, fd_shredder_align(), fd_shredder_footprint() );
226 3 : l = FD_LAYOUT_APPEND( l, alignof(fd_fec_set_t), sizeof(fd_fec_set_t)*fec_set_cnt );
227 3 : return FD_LAYOUT_FINI( l, scratch_align() );
228 3 : }
229 :
230 : static inline void
231 0 : during_housekeeping( fd_shred_ctx_t * ctx ) {
232 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
233 0 : ulong seq_must_complete = ctx->keyswitch->param;
234 :
235 0 : if( FD_UNLIKELY( fd_seq_lt( ctx->poh_in_expect_seq, seq_must_complete ) ) ) {
236 : /* See fd_keyswitch.h, we need to flush any in-flight shreds from
237 : the leader pipeline before switching key. */
238 0 : FD_LOG_WARNING(( "Flushing in-flight unpublished shreds, must reach seq %lu, currently at %lu ...", seq_must_complete, ctx->poh_in_expect_seq ));
239 0 : return;
240 0 : }
241 :
242 0 : fd_memcpy( ctx->identity_key->uc, ctx->keyswitch->bytes, 32UL );
243 0 : fd_stake_ci_set_identity( ctx->stake_ci, ctx->identity_key );
244 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
245 0 : }
246 0 : }
247 :
248 : static inline void
249 0 : metrics_write( fd_shred_ctx_t * ctx ) {
250 0 : FD_MHIST_COPY( SHRED, CLUSTER_CONTACT_INFO_CNT, ctx->metrics->contact_info_cnt );
251 0 : FD_MHIST_COPY( SHRED, BATCH_SZ, ctx->metrics->batch_sz );
252 0 : FD_MHIST_COPY( SHRED, BATCH_MICROBLOCK_CNT, ctx->metrics->batch_microblock_cnt );
253 0 : FD_MHIST_COPY( SHRED, SHREDDING_DURATION_SECONDS, ctx->metrics->shredding_timing );
254 0 : FD_MHIST_COPY( SHRED, ADD_SHRED_DURATION_SECONDS, ctx->metrics->add_shred_timing );
255 :
256 0 : FD_MCNT_ENUM_COPY( SHRED, SHRED_PROCESSED, ctx->metrics->shred_processing_result );
257 0 : }
258 :
259 : static inline void
260 : handle_new_cluster_contact_info( fd_shred_ctx_t * ctx,
261 0 : uchar const * buf ) {
262 0 : ulong const * header = (ulong const *)fd_type_pun_const( buf );
263 :
264 0 : ulong dest_cnt = header[ 0 ];
265 0 : fd_histf_sample( ctx->metrics->contact_info_cnt, dest_cnt );
266 :
267 0 : if( dest_cnt >= MAX_SHRED_DESTS )
268 0 : FD_LOG_ERR(( "Cluster nodes had %lu destinations, which was more than the max of %lu", dest_cnt, MAX_SHRED_DESTS ));
269 :
270 0 : fd_shred_dest_wire_t const * in_dests = fd_type_pun_const( header+1UL );
271 0 : fd_shred_dest_weighted_t * dests = fd_stake_ci_dest_add_init( ctx->stake_ci );
272 :
273 0 : ctx->new_dest_ptr = dests;
274 0 : ctx->new_dest_cnt = dest_cnt;
275 :
276 0 : for( ulong i=0UL; i<dest_cnt; i++ ) {
277 0 : memcpy( dests[i].pubkey.uc, in_dests[i].pubkey, 32UL );
278 0 : dests[i].ip4 = in_dests[i].ip4_addr;
279 0 : dests[i].port = in_dests[i].udp_port;
280 0 : }
281 0 : }
282 :
283 : static inline void
284 0 : finalize_new_cluster_contact_info( fd_shred_ctx_t * ctx ) {
285 0 : fd_stake_ci_dest_add_fini( ctx->stake_ci, ctx->new_dest_cnt );
286 0 : }
287 :
288 : static inline int
289 : before_frag( fd_shred_ctx_t * ctx,
290 : ulong in_idx,
291 : ulong seq,
292 0 : ulong sig ) {
293 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH ) ) ctx->poh_in_expect_seq = seq+1UL;
294 :
295 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) return fd_disco_netmux_sig_proto( sig )!=DST_PROTO_SHRED;
296 0 : else if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH ) ) return fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_MICROBLOCK;
297 :
298 0 : return 0;
299 0 : }
300 :
301 : static void
302 : during_frag( fd_shred_ctx_t * ctx,
303 : ulong in_idx,
304 : ulong seq FD_PARAM_UNUSED,
305 : ulong sig,
306 : ulong chunk,
307 : ulong sz,
308 0 : ulong ctl ) {
309 :
310 0 : ctx->skip_frag = 0;
311 :
312 0 : ctx->tsorig = fd_frag_meta_ts_comp( fd_tickcount() );
313 :
314 :
315 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_CONTACT ) ) {
316 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
317 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
318 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
319 :
320 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
321 0 : handle_new_cluster_contact_info( ctx, dcache_entry );
322 0 : return;
323 0 : }
324 :
325 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) {
326 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
327 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
328 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
329 :
330 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
331 0 : fd_stake_ci_stake_msg_init( ctx->stake_ci, dcache_entry );
332 0 : return;
333 0 : }
334 :
335 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH ) ) {
336 : /* This is a frag from the PoH tile. We'll copy it to our pending
337 : microblock batch and shred it if necessary (last in block or
338 : above watermark). We just go ahead and shred it here, even
339 : though we may get overrun. If we do end up getting overrun, we
340 : just won't send these shreds out and we'll reuse the FEC set for
341 : the next one. From a higher level though, if we do get overrun,
342 : a bunch of shreds will never be transmitted, and we'll end up
343 : producing a block that never lands on chain. */
344 0 : fd_fec_set_t * out = ctx->fec_sets + ctx->shredder_fec_set_idx;
345 :
346 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
347 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_POH_SHRED_MTU ||
348 0 : sz<(sizeof(fd_entry_batch_meta_t)+sizeof(fd_entry_batch_header_t)) ) )
349 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
350 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
351 :
352 0 : fd_entry_batch_meta_t const * entry_meta = (fd_entry_batch_meta_t const *)dcache_entry;
353 0 : uchar const * entry = dcache_entry + sizeof(fd_entry_batch_meta_t);
354 0 : ulong entry_sz = sz - sizeof(fd_entry_batch_meta_t);
355 :
356 0 : fd_entry_batch_header_t const * microblock = (fd_entry_batch_header_t const *)entry;
357 :
358 : /* It should never be possible for this to fail, but we check it
359 : anyway. */
360 0 : FD_TEST( entry_sz + ctx->pending_batch.pos <= sizeof(ctx->pending_batch.payload) );
361 :
362 0 : ulong target_slot = fd_disco_poh_sig_slot( sig );
363 0 : if( FD_UNLIKELY( (ctx->pending_batch.microblock_cnt>0) & (ctx->pending_batch.slot!=target_slot) ) ) {
364 : /* TODO: The Agave client sends a dummy entry batch with only 1
365 : byte and the block-complete bit set. This helps other
366 : validators know that the block is dead and they should not try
367 : to continue building a fork on it. We probably want a similar
368 : approach eventually. */
369 0 : FD_LOG_WARNING(( "Abandoning %lu microblocks for slot %lu and switching to slot %lu",
370 0 : ctx->pending_batch.microblock_cnt, ctx->pending_batch.slot, target_slot ));
371 0 : ctx->pending_batch.slot = 0UL;
372 0 : ctx->pending_batch.pos = 0UL;
373 0 : ctx->pending_batch.microblock_cnt = 0UL;
374 0 : ctx->pending_batch.txn_cnt = 0UL;
375 0 : ctx->batch_cnt = 0UL;
376 :
377 0 : FD_MCNT_INC( SHRED, MICROBLOCKS_ABANDONED, 1UL );
378 0 : }
379 :
380 0 : ctx->pending_batch.slot = target_slot;
381 0 : if( FD_UNLIKELY( target_slot!=ctx->slot )) {
382 : /* Reset batch count if we are in a new slot */
383 0 : ctx->batch_cnt = 0UL;
384 0 : ctx->slot = target_slot;
385 0 : }
386 0 : if( FD_UNLIKELY( ctx->batch_cnt%ctx->round_robin_cnt==ctx->round_robin_id ) ) {
387 : /* Ugh, yet another memcpy */
388 0 : fd_memcpy( ctx->pending_batch.payload + ctx->pending_batch.pos, entry, entry_sz );
389 0 : } else {
390 : /* If we are not processing this batch, filter */
391 0 : ctx->skip_frag = 1;
392 0 : }
393 0 : ctx->pending_batch.pos += entry_sz;
394 0 : ctx->pending_batch.microblock_cnt += 1UL;
395 0 : ctx->pending_batch.txn_cnt += microblock->txn_cnt;
396 :
397 0 : int last_in_batch = entry_meta->block_complete | (ctx->pending_batch.pos > PENDING_BATCH_WMARK);
398 :
399 0 : ctx->send_fec_set_idx = ULONG_MAX;
400 0 : if( FD_UNLIKELY( last_in_batch )) {
401 0 : if( FD_UNLIKELY( ctx->batch_cnt%ctx->round_robin_cnt==ctx->round_robin_id ) ) {
402 : /* If it's our turn, shred this batch. FD_UNLIKELY because shred tile cnt generally >= 2 */
403 0 : ulong batch_sz = sizeof(ulong)+ctx->pending_batch.pos;
404 :
405 : /* We sized this so it fits in one FEC set */
406 0 : long shredding_timing = -fd_tickcount();
407 :
408 0 : if( FD_UNLIKELY( entry_meta->block_complete && batch_sz < FD_SHREDDER_NORMAL_FEC_SET_PAYLOAD_SZ ) ) {
409 :
410 : /* Ensure the last batch generates >= 32 data shreds by
411 : padding with 0s. Because the last FEC set is "oddly sized"
412 : we only expect this code path to execute for blocks
413 : containing less data than can fill 32 data shred payloads
414 : (hence FD_UNLIKELY).
415 :
416 : See documentation for FD_SHREDDER_NORMAL_FEC_SET_PAYLOAD_SZ
417 : for further context. */
418 :
419 0 : fd_memset( ctx->pending_batch.payload + ctx->pending_batch.pos, 0, FD_SHREDDER_NORMAL_FEC_SET_PAYLOAD_SZ - batch_sz );
420 0 : batch_sz = FD_SHREDDER_NORMAL_FEC_SET_PAYLOAD_SZ;
421 0 : }
422 :
423 0 : fd_shredder_init_batch( ctx->shredder, ctx->pending_batch.raw, batch_sz, target_slot, entry_meta );
424 0 : FD_TEST( fd_shredder_next_fec_set( ctx->shredder, out ) );
425 0 : fd_shredder_fini_batch( ctx->shredder );
426 0 : shredding_timing += fd_tickcount();
427 :
428 0 : d_rcvd_join( d_rcvd_new( d_rcvd_delete( d_rcvd_leave( out->data_shred_rcvd ) ) ) );
429 0 : p_rcvd_join( p_rcvd_new( p_rcvd_delete( p_rcvd_leave( out->parity_shred_rcvd ) ) ) );
430 0 : ctx->shredded_txn_cnt = ctx->pending_batch.txn_cnt;
431 :
432 0 : ctx->send_fec_set_idx = ctx->shredder_fec_set_idx;
433 :
434 : /* Update metrics */
435 0 : fd_histf_sample( ctx->metrics->batch_sz, batch_sz );
436 0 : fd_histf_sample( ctx->metrics->batch_microblock_cnt, ctx->pending_batch.microblock_cnt );
437 0 : fd_histf_sample( ctx->metrics->shredding_timing, (ulong)shredding_timing );
438 0 : } else {
439 : /* If it's not our turn, update the indices for this slot */
440 0 : fd_shredder_skip_batch( ctx->shredder, sizeof(ulong)+ctx->pending_batch.pos, target_slot );
441 0 : }
442 :
443 0 : ctx->pending_batch.slot = 0UL;
444 0 : ctx->pending_batch.pos = 0UL;
445 0 : ctx->pending_batch.microblock_cnt = 0UL;
446 0 : ctx->pending_batch.txn_cnt = 0UL;
447 0 : ctx->batch_cnt++;
448 0 : }
449 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
450 : /* The common case, from the net tile. The FEC resolver API does
451 : not present a prepare/commit model. If we get overrun between
452 : when the FEC resolver verifies the signature and when it stores
453 : the local copy, we could end up storing and retransmitting
454 : garbage. Instead we copy it locally, sadly, and only give it to
455 : the FEC resolver when we know it won't be overrun anymore. */
456 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_NET_MTU ) )
457 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
458 0 : uchar const * dcache_entry = (uchar const *)fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ) + ctl;
459 0 : ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
460 0 : FD_TEST( hdr_sz <= sz ); /* Should be ensured by the net tile */
461 0 : fd_shred_t const * shred = fd_shred_parse( dcache_entry+hdr_sz, sz-hdr_sz );
462 0 : if( FD_UNLIKELY( !shred ) ) {
463 0 : ctx->skip_frag = 1;
464 0 : return;
465 0 : };
466 : /* all shreds in the same FEC set will have the same signature
467 : so we can round-robin shreds between the shred tiles based on
468 : just the signature without splitting individual FEC sets. */
469 0 : ulong sig = fd_ulong_load_8( shred->signature );
470 0 : if( FD_LIKELY( sig%ctx->round_robin_cnt!=ctx->round_robin_id ) ) {
471 0 : ctx->skip_frag = 1;
472 0 : return;
473 0 : }
474 0 : fd_memcpy( ctx->shred_buffer, dcache_entry+hdr_sz, sz-hdr_sz );
475 0 : ctx->shred_buffer_sz = sz-hdr_sz;
476 0 : }
477 0 : }
478 :
479 : static inline void
480 : send_shred( fd_shred_ctx_t * ctx,
481 : fd_shred_t const * shred,
482 : fd_shred_dest_t * sdest,
483 : fd_shred_dest_idx_t dest_idx,
484 0 : ulong tsorig ) {
485 0 : fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( sdest, dest_idx );
486 :
487 0 : if( FD_UNLIKELY( !dest->ip4 ) ) return;
488 :
489 0 : uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
490 :
491 0 : int is_data = fd_shred_type( shred->variant )==FD_SHRED_TYPE_MERKLE_DATA;
492 0 : fd_net_hdrs_t * tmpl = fd_ptr_if( is_data, (fd_net_hdrs_t *)ctx->data_shred_net_hdr,
493 0 : (fd_net_hdrs_t *)ctx->parity_shred_net_hdr );
494 0 : fd_memcpy( packet, tmpl, sizeof(fd_net_hdrs_t) );
495 :
496 0 : fd_net_hdrs_t * hdr = (fd_net_hdrs_t *)packet;
497 :
498 0 : memset( hdr->eth->dst, 0, 6UL );
499 :
500 0 : memcpy( hdr->ip4->daddr_c, &dest->ip4, 4UL );
501 0 : hdr->ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
502 0 : hdr->ip4->check = 0U;
503 0 : hdr->ip4->check = fd_ip4_hdr_check( ( fd_ip4_hdr_t const *) FD_ADDRESS_OF_PACKED_MEMBER( hdr->ip4 ) );
504 :
505 0 : hdr->udp->net_dport = fd_ushort_bswap( dest->port );
506 :
507 0 : ulong shred_sz = fd_ulong_if( is_data, FD_SHRED_MIN_SZ, FD_SHRED_MAX_SZ );
508 0 : fd_memcpy( packet+sizeof(fd_net_hdrs_t), shred, shred_sz );
509 :
510 0 : ulong pkt_sz = shred_sz + sizeof(fd_net_hdrs_t);
511 :
512 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
513 0 : ulong sig = fd_disco_netmux_sig( dest->ip4, dest->port, dest->ip4, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
514 0 : fd_mcache_publish( ctx->net_out_mcache, ctx->net_out_depth, ctx->net_out_seq, sig, ctx->net_out_chunk,
515 0 : pkt_sz, 0UL, tsorig, tspub );
516 0 : ctx->net_out_seq = fd_seq_inc( ctx->net_out_seq, 1UL );
517 0 : ctx->net_out_chunk = fd_dcache_compact_next( ctx->net_out_chunk, pkt_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
518 0 : }
519 :
520 : static void
521 : after_frag( fd_shred_ctx_t * ctx,
522 : ulong in_idx,
523 : ulong seq,
524 : ulong sig,
525 : ulong sz,
526 : ulong tsorig,
527 0 : fd_stem_context_t * stem ) {
528 0 : (void)seq;
529 0 : (void)sig;
530 0 : (void)sz;
531 0 : (void)tsorig;
532 :
533 0 : if( FD_UNLIKELY( ctx->skip_frag ) ) return;
534 :
535 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_CONTACT ) ) {
536 0 : finalize_new_cluster_contact_info( ctx );
537 0 : return;
538 0 : }
539 :
540 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) {
541 0 : fd_stake_ci_stake_msg_fini( ctx->stake_ci );
542 0 : return;
543 0 : }
544 :
545 0 : if( FD_UNLIKELY( (ctx->in_kind[ in_idx ]==IN_KIND_POH) & (ctx->send_fec_set_idx==ULONG_MAX) ) ) {
546 : /* Entry from PoH that didn't trigger a new FEC set to be made */
547 0 : return;
548 0 : }
549 :
550 0 : const ulong fanout = 200UL;
551 0 : fd_shred_dest_idx_t _dests[ 200*(FD_REEDSOL_DATA_SHREDS_MAX+FD_REEDSOL_PARITY_SHREDS_MAX) ];
552 :
553 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
554 0 : uchar * shred_buffer = ctx->shred_buffer;
555 0 : ulong shred_buffer_sz = ctx->shred_buffer_sz;
556 :
557 0 : fd_shred_t const * shred = fd_shred_parse( shred_buffer, shred_buffer_sz );
558 0 : if( FD_UNLIKELY( !shred ) ) { ctx->metrics->shred_processing_result[ 1 ]++; return; }
559 :
560 0 : fd_epoch_leaders_t const * lsched = fd_stake_ci_get_lsched_for_slot( ctx->stake_ci, shred->slot );
561 0 : if( FD_UNLIKELY( !lsched ) ) { ctx->metrics->shred_processing_result[ 0 ]++; return; }
562 :
563 0 : fd_pubkey_t const * slot_leader = fd_epoch_leaders_get( lsched, shred->slot );
564 0 : if( FD_UNLIKELY( !slot_leader ) ) { ctx->metrics->shred_processing_result[ 0 ]++; return; } /* Count this as bad slot too */
565 :
566 0 : fd_fec_set_t const * out_fec_set[ 1 ];
567 0 : fd_shred_t const * out_shred[ 1 ];
568 :
569 0 : long add_shred_timing = -fd_tickcount();
570 0 : int rv = fd_fec_resolver_add_shred( ctx->resolver, shred, shred_buffer_sz, slot_leader->uc, out_fec_set, out_shred );
571 0 : add_shred_timing += fd_tickcount();
572 :
573 0 : fd_histf_sample( ctx->metrics->add_shred_timing, (ulong)add_shred_timing );
574 0 : ctx->metrics->shred_processing_result[ rv + FD_FEC_RESOLVER_ADD_SHRED_RETVAL_OFF+FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT ]++;
575 :
576 0 : if( (rv==FD_FEC_RESOLVER_SHRED_OKAY) | (rv==FD_FEC_RESOLVER_SHRED_COMPLETES) ) {
577 : /* Relay this shred */
578 0 : ulong fanout = 200UL;
579 0 : ulong max_dest_cnt[1];
580 0 : do {
581 : /* If we've validated the shred and it COMPLETES but we can't
582 : compute the destination for whatever reason, don't forward
583 : the shred, but still send it to the blockstore. */
584 0 : fd_shred_dest_t * sdest = fd_stake_ci_get_sdest_for_slot( ctx->stake_ci, shred->slot );
585 0 : if( FD_UNLIKELY( !sdest ) ) break;
586 0 : fd_shred_dest_idx_t * dests = fd_shred_dest_compute_children( sdest, &shred, 1UL, _dests, 1UL, fanout, fanout, max_dest_cnt );
587 0 : if( FD_UNLIKELY( !dests ) ) break;
588 :
589 0 : for( ulong j=0UL; j<*max_dest_cnt; j++ ) send_shred( ctx, *out_shred, sdest, dests[ j ], ctx->tsorig );
590 0 : } while( 0 );
591 0 : }
592 0 : if( FD_LIKELY( rv!=FD_FEC_RESOLVER_SHRED_COMPLETES ) ) return;
593 :
594 0 : FD_TEST( ctx->fec_sets <= *out_fec_set );
595 0 : ctx->send_fec_set_idx = (ulong)(*out_fec_set - ctx->fec_sets);
596 0 : ctx->shredded_txn_cnt = 0UL;
597 0 : } else {
598 : /* We know we didn't get overrun, so advance the index */
599 0 : ctx->shredder_fec_set_idx = (ctx->shredder_fec_set_idx+1UL)%ctx->shredder_max_fec_set_idx;
600 0 : }
601 : /* If this was the shred that completed an FEC set or this was a
602 : microblock we shredded ourself, we now have a full FEC set that we
603 : need to send to the blockstore and on the network (skipping any
604 : shreds we already sent). */
605 :
606 0 : fd_fec_set_t * set = ctx->fec_sets + ctx->send_fec_set_idx;
607 0 : fd_shred34_t * s34 = ctx->shred34 + 4UL*ctx->send_fec_set_idx;
608 :
609 0 : s34[ 0 ].shred_cnt = fd_ulong_min( set->data_shred_cnt, 34UL );
610 0 : s34[ 1 ].shred_cnt = set->data_shred_cnt - fd_ulong_min( set->data_shred_cnt, 34UL );
611 0 : s34[ 2 ].shred_cnt = fd_ulong_min( set->parity_shred_cnt, 34UL );
612 0 : s34[ 3 ].shred_cnt = set->parity_shred_cnt - fd_ulong_min( set->parity_shred_cnt, 34UL );
613 :
614 0 : ulong s34_cnt = 2UL + !!(s34[ 1 ].shred_cnt) + !!(s34[ 3 ].shred_cnt);
615 0 : ulong txn_per_s34 = ctx->shredded_txn_cnt / s34_cnt;
616 :
617 : /* Attribute the transactions evenly to the non-empty shred34s */
618 0 : for( ulong j=0UL; j<4UL; j++ ) s34[ j ].est_txn_cnt = fd_ulong_if( s34[ j ].shred_cnt>0UL, txn_per_s34, 0UL );
619 :
620 : /* Add whatever is left to the last shred34 */
621 0 : s34[ fd_ulong_if( s34[ 3 ].shred_cnt>0UL, 3, 2 ) ].est_txn_cnt += ctx->shredded_txn_cnt - txn_per_s34*s34_cnt;
622 :
623 : /* Set the sz field so that metrics are more accurate. */
624 0 : ulong sz0 = sizeof(fd_shred34_t) - (34UL - s34[ 0 ].shred_cnt)*FD_SHRED_MAX_SZ;
625 0 : ulong sz1 = sizeof(fd_shred34_t) - (34UL - s34[ 1 ].shred_cnt)*FD_SHRED_MAX_SZ;
626 0 : ulong sz2 = sizeof(fd_shred34_t) - (34UL - s34[ 2 ].shred_cnt)*FD_SHRED_MAX_SZ;
627 0 : ulong sz3 = sizeof(fd_shred34_t) - (34UL - s34[ 3 ].shred_cnt)*FD_SHRED_MAX_SZ;
628 :
629 : /* Send to the blockstore, skipping any empty shred34_t s. */
630 0 : ulong new_sig = ctx->in_kind[ in_idx ]!=IN_KIND_NET; /* sig==0 means the store tile will do extra checks */
631 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
632 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, s34+0UL ), sz0, 0UL, ctx->tsorig, tspub );
633 0 : if( FD_UNLIKELY( s34[ 1 ].shred_cnt ) )
634 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, s34+1UL ), sz1, 0UL, ctx->tsorig, tspub );
635 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, s34+2UL), sz2, 0UL, ctx->tsorig, tspub );
636 0 : if( FD_UNLIKELY( s34[ 3 ].shred_cnt ) )
637 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, s34+3UL ), sz3, 0UL, ctx->tsorig, tspub );
638 :
639 :
640 : /* Compute all the destinations for all the new shreds */
641 :
642 0 : fd_shred_t const * new_shreds[ FD_REEDSOL_DATA_SHREDS_MAX+FD_REEDSOL_PARITY_SHREDS_MAX ];
643 0 : ulong k=0UL;
644 0 : for( ulong i=0UL; i<set->data_shred_cnt; i++ )
645 0 : if( !d_rcvd_test( set->data_shred_rcvd, i ) ) new_shreds[ k++ ] = (fd_shred_t const *)set->data_shreds [ i ];
646 0 : for( ulong i=0UL; i<set->parity_shred_cnt; i++ )
647 0 : if( !p_rcvd_test( set->parity_shred_rcvd, i ) ) new_shreds[ k++ ] = (fd_shred_t const *)set->parity_shreds[ i ];
648 :
649 0 : if( FD_UNLIKELY( !k ) ) return;
650 0 : fd_shred_dest_t * sdest = fd_stake_ci_get_sdest_for_slot( ctx->stake_ci, new_shreds[ 0 ]->slot );
651 0 : if( FD_UNLIKELY( !sdest ) ) return;
652 :
653 0 : ulong out_stride;
654 0 : ulong max_dest_cnt[1];
655 0 : fd_shred_dest_idx_t * dests;
656 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
657 0 : out_stride = k;
658 0 : dests = fd_shred_dest_compute_children( sdest, new_shreds, k, _dests, k, fanout, fanout, max_dest_cnt );
659 0 : } else {
660 0 : out_stride = 1UL;
661 0 : *max_dest_cnt = 1UL;
662 0 : dests = fd_shred_dest_compute_first ( sdest, new_shreds, k, _dests );
663 0 : }
664 0 : if( FD_UNLIKELY( !dests ) ) return;
665 :
666 : /* Send only the ones we didn't receive. */
667 0 : for( ulong i=0UL; i<k; i++ ) for( ulong j=0UL; j<*max_dest_cnt; j++ ) send_shred( ctx, new_shreds[ i ], sdest, dests[ j*out_stride+i ], ctx->tsorig );
668 0 : }
669 :
670 : static void
671 : privileged_init( fd_topo_t * topo,
672 0 : fd_topo_tile_t * tile ) {
673 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
674 :
675 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
676 0 : fd_shred_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_shred_ctx_t ), sizeof( fd_shred_ctx_t ) );
677 :
678 0 : if( FD_UNLIKELY( !strcmp( tile->shred.identity_key_path, "" ) ) )
679 0 : FD_LOG_ERR(( "identity_key_path not set" ));
680 :
681 0 : ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->shred.identity_key_path, /* pubkey only: */ 1 ) );
682 0 : }
683 :
684 : static void
685 : fd_shred_signer( void * signer_ctx,
686 : uchar signature[ static 64 ],
687 0 : uchar const merkle_root[ static 32 ] ) {
688 0 : fd_keyguard_client_sign( signer_ctx, signature, merkle_root, 32UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
689 0 : }
690 :
691 : static void
692 : unprivileged_init( fd_topo_t * topo,
693 0 : fd_topo_tile_t * tile ) {
694 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
695 :
696 0 : if( FD_UNLIKELY( tile->out_cnt!=3UL ||
697 0 : (strcmp( topo->links[ tile->out_link_id[ STORE_OUT_IDX ] ].name, "shred_store" ) &&
698 0 : strcmp( topo->links[ tile->out_link_id[ STORE_OUT_IDX ] ].name, "shred_storei" ) ) ||
699 0 : strcmp( topo->links[ tile->out_link_id[ NET_OUT_IDX ] ].name, "shred_net" ) ||
700 0 : strcmp( topo->links[ tile->out_link_id[ SIGN_OUT_IDX ] ].name, "shred_sign" ) ) )
701 0 : FD_LOG_ERR(( "shred tile has none or unexpected output links %lu %s %s",
702 0 : tile->out_cnt, topo->links[ tile->out_link_id[ 0 ] ].name, topo->links[ tile->out_link_id[ 1 ] ].name ));
703 :
704 0 : if( FD_UNLIKELY( !tile->out_cnt ) )
705 0 : FD_LOG_ERR(( "shred tile has no primary output link" ));
706 :
707 0 : ulong shred_store_mcache_depth = tile->shred.depth;
708 0 : if( topo->links[ tile->out_link_id[ 0 ] ].depth != shred_store_mcache_depth )
709 0 : FD_LOG_ERR(( "shred tile out depths are not equal %lu %lu",
710 0 : topo->links[ tile->out_link_id[ 0 ] ].depth, shred_store_mcache_depth ));
711 :
712 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
713 0 : fd_shred_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_shred_ctx_t ), sizeof( fd_shred_ctx_t ) );
714 :
715 0 : ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
716 0 : ctx->round_robin_id = tile->kind_id;
717 0 : ctx->batch_cnt = 0UL;
718 0 : ctx->slot = ULONG_MAX;
719 :
720 0 : ulong fec_resolver_footprint = fd_fec_resolver_footprint( tile->shred.fec_resolver_depth, 1UL, shred_store_mcache_depth,
721 0 : 128UL * tile->shred.fec_resolver_depth );
722 0 : ulong fec_set_cnt = shred_store_mcache_depth + tile->shred.fec_resolver_depth + 4UL;
723 :
724 0 : void * store_out_dcache = topo->links[ tile->out_link_id[ 0 ] ].dcache;
725 :
726 0 : ulong required_dcache_sz = fec_set_cnt*DCACHE_ENTRIES_PER_FEC_SET*sizeof(fd_shred34_t);
727 0 : if( fd_dcache_data_sz( store_out_dcache )<required_dcache_sz ) {
728 0 : FD_LOG_ERR(( "shred->store dcache too small. It is %lu bytes but must be at least %lu bytes.",
729 0 : fd_dcache_data_sz( store_out_dcache ),
730 0 : required_dcache_sz ));
731 0 : }
732 :
733 0 : if( FD_UNLIKELY( !tile->shred.fec_resolver_depth ) ) FD_LOG_ERR(( "fec_resolver_depth not set" ));
734 :
735 0 : uchar zero_mac_addr[6] = {0};
736 0 : if( FD_UNLIKELY( fd_memeq( tile->shred.src_mac_addr, zero_mac_addr, sizeof(zero_mac_addr ) ) ) ) FD_LOG_ERR(( "src_mac_addr not set" ));
737 0 : if( FD_UNLIKELY( !tile->shred.ip_addr ) ) FD_LOG_ERR(( "ip_addr not set" ));
738 0 : if( FD_UNLIKELY( !tile->shred.shred_listen_port ) ) FD_LOG_ERR(( "shred_listen_port not set" ));
739 :
740 0 : ulong bank_cnt = fd_topo_tile_name_cnt( topo, "bank" );
741 0 : ulong replay_cnt = fd_topo_tile_name_cnt( topo, "replay" );
742 :
743 0 : if( FD_UNLIKELY( !bank_cnt && !replay_cnt ) ) FD_LOG_ERR(( "0 bank/replay tiles" ));
744 0 : if( FD_UNLIKELY( bank_cnt>MAX_BANK_CNT ) ) FD_LOG_ERR(( "Too many banks" ));
745 :
746 0 : void * _stake_ci = FD_SCRATCH_ALLOC_APPEND( l, fd_stake_ci_align(), fd_stake_ci_footprint() );
747 0 : void * _resolver = FD_SCRATCH_ALLOC_APPEND( l, fd_fec_resolver_align(), fec_resolver_footprint );
748 0 : void * _shredder = FD_SCRATCH_ALLOC_APPEND( l, fd_shredder_align(), fd_shredder_footprint() );
749 0 : void * _fec_sets = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_fec_set_t), sizeof(fd_fec_set_t)*fec_set_cnt );
750 :
751 0 : fd_fec_set_t * fec_sets = (fd_fec_set_t *)_fec_sets;
752 0 : fd_shred34_t * shred34 = (fd_shred34_t *)store_out_dcache;
753 :
754 0 : for( ulong i=0UL; i<fec_set_cnt; i++ ) {
755 0 : fd_shred34_t * p34_base = shred34 + i*DCACHE_ENTRIES_PER_FEC_SET;
756 0 : for( ulong k=0UL; k<DCACHE_ENTRIES_PER_FEC_SET; k++ ) {
757 0 : fd_shred34_t * p34 = p34_base + k;
758 :
759 0 : p34->stride = (ulong)p34->pkts[1].buffer - (ulong)p34->pkts[0].buffer;
760 0 : p34->offset = (ulong)p34->pkts[0].buffer - (ulong)p34;
761 0 : p34->shred_sz = fd_ulong_if( k<2UL, 1203UL, 1228UL );
762 0 : }
763 :
764 0 : uchar ** data_shred = fec_sets[ i ].data_shreds;
765 0 : uchar ** parity_shred = fec_sets[ i ].parity_shreds;
766 0 : for( ulong j=0UL; j<FD_REEDSOL_DATA_SHREDS_MAX; j++ ) data_shred [ j ] = p34_base[ j/34UL ].pkts[ j%34UL ].buffer;
767 0 : for( ulong j=0UL; j<FD_REEDSOL_PARITY_SHREDS_MAX; j++ ) parity_shred[ j ] = p34_base[ 2UL + j/34UL ].pkts[ j%34UL ].buffer;
768 0 : }
769 :
770 0 : #define NONNULL( x ) (__extension__({ \
771 0 : __typeof__((x)) __x = (x); \
772 0 : if( FD_UNLIKELY( !__x ) ) FD_LOG_ERR(( #x " was unexpectedly NULL" )); \
773 0 : __x; }))
774 :
775 0 : ulong expected_shred_version = tile->shred.expected_shred_version;
776 0 : if( FD_LIKELY( !expected_shred_version ) ) {
777 0 : ulong busy_obj_id = fd_pod_query_ulong( topo->props, "poh_shred", ULONG_MAX );
778 0 : FD_TEST( busy_obj_id!=ULONG_MAX );
779 0 : ulong * gossip_shred_version = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
780 0 : FD_LOG_INFO(( "Waiting for shred version to be determined via gossip." ));
781 0 : do {
782 0 : expected_shred_version = FD_VOLATILE_CONST( *gossip_shred_version );
783 0 : } while( expected_shred_version==ULONG_MAX );
784 0 : }
785 :
786 0 : if( FD_UNLIKELY( expected_shred_version > USHORT_MAX ) ) FD_LOG_ERR(( "invalid shred version %lu", expected_shred_version ));
787 0 : FD_LOG_INFO(( "Using shred version %hu", (ushort)expected_shred_version ));
788 :
789 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
790 0 : FD_TEST( ctx->keyswitch );
791 :
792 : /* populate ctx */
793 0 : ulong sign_in_idx = fd_topo_find_tile_in_link( topo, tile, "sign_shred", tile->kind_id );
794 0 : FD_TEST( sign_in_idx!=ULONG_MAX );
795 0 : fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
796 0 : fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ SIGN_OUT_IDX ] ];
797 0 : NONNULL( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
798 0 : sign_out->mcache,
799 0 : sign_out->dcache,
800 0 : sign_in->mcache,
801 0 : sign_in->dcache ) ) );
802 :
803 0 : ulong shred_limit = fd_ulong_if( tile->shred.larger_shred_limits_per_block, 32UL*32UL*1024UL, 32UL*1024UL );
804 0 : fd_fec_set_t * resolver_sets = fec_sets + (shred_store_mcache_depth+1UL)/2UL + 1UL;
805 0 : ctx->shredder = NONNULL( fd_shredder_join ( fd_shredder_new ( _shredder, fd_shred_signer, ctx->keyguard_client, (ushort)expected_shred_version ) ) );
806 0 : ctx->resolver = NONNULL( fd_fec_resolver_join ( fd_fec_resolver_new ( _resolver,
807 0 : fd_shred_signer, ctx->keyguard_client,
808 0 : tile->shred.fec_resolver_depth, 1UL,
809 0 : (shred_store_mcache_depth+3UL)/2UL,
810 0 : 128UL * tile->shred.fec_resolver_depth, resolver_sets,
811 0 : (ushort)expected_shred_version,
812 0 : shred_limit ) ) );
813 :
814 0 : ctx->shred34 = shred34;
815 0 : ctx->fec_sets = fec_sets;
816 :
817 0 : ctx->stake_ci = fd_stake_ci_join( fd_stake_ci_new( _stake_ci, ctx->identity_key ) );
818 :
819 0 : ctx->net_id = (ushort)0;
820 :
821 0 : fd_net_create_packet_header_template( ctx->data_shred_net_hdr, FD_SHRED_MIN_SZ, tile->shred.ip_addr, tile->shred.src_mac_addr, tile->shred.shred_listen_port );
822 0 : fd_net_create_packet_header_template( ctx->parity_shred_net_hdr, FD_SHRED_MAX_SZ, tile->shred.ip_addr, tile->shred.src_mac_addr, tile->shred.shred_listen_port );
823 :
824 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
825 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
826 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
827 :
828 0 : if( FD_LIKELY( !strcmp( link->name, "net_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_NET;
829 0 : else if( FD_LIKELY( !strcmp( link->name, "poh_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
830 0 : else if( FD_LIKELY( !strcmp( link->name, "stake_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_STAKE;
831 0 : else if( FD_LIKELY( !strcmp( link->name, "crds_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_CONTACT;
832 0 : else if( FD_LIKELY( !strcmp( link->name, "sign_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
833 0 : else FD_LOG_ERR(( "shred tile has unexpected input link %lu %s", i, link->name ));
834 :
835 0 : ctx->in[ i ].mem = link_wksp->wksp;
836 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
837 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
838 0 : }
839 :
840 0 : fd_topo_link_t * net_out = &topo->links[ tile->out_link_id[ NET_OUT_IDX ] ];
841 :
842 0 : ctx->net_out_mcache = net_out->mcache;
843 0 : ctx->net_out_sync = fd_mcache_seq_laddr( ctx->net_out_mcache );
844 0 : ctx->net_out_depth = fd_mcache_depth( ctx->net_out_mcache );
845 0 : ctx->net_out_seq = fd_mcache_seq_query( ctx->net_out_sync );
846 0 : ctx->net_out_chunk0 = fd_dcache_compact_chunk0( fd_wksp_containing( net_out->dcache ), net_out->dcache );
847 0 : ctx->net_out_mem = topo->workspaces[ topo->objs[ net_out->dcache_obj_id ].wksp_id ].wksp;
848 0 : ctx->net_out_wmark = fd_dcache_compact_wmark ( ctx->net_out_mem, net_out->dcache, net_out->mtu );
849 0 : ctx->net_out_chunk = ctx->net_out_chunk0;
850 :
851 0 : fd_topo_link_t * store_out = &topo->links[ tile->out_link_id[ STORE_OUT_IDX ] ];
852 :
853 0 : ctx->store_out_mem = topo->workspaces[ topo->objs[ store_out->dcache_obj_id ].wksp_id ].wksp;
854 0 : ctx->store_out_chunk0 = fd_dcache_compact_chunk0( ctx->store_out_mem, store_out->dcache );
855 0 : ctx->store_out_wmark = fd_dcache_compact_wmark ( ctx->store_out_mem, store_out->dcache, store_out->mtu );
856 0 : ctx->store_out_chunk = ctx->store_out_chunk0;
857 :
858 0 : ctx->poh_in_expect_seq = 0UL;
859 :
860 0 : ctx->shredder_fec_set_idx = 0UL;
861 0 : ctx->shredder_max_fec_set_idx = (shred_store_mcache_depth+1UL)/2UL + 1UL;
862 :
863 0 : ctx->send_fec_set_idx = ULONG_MAX;
864 :
865 0 : ctx->shred_buffer_sz = 0UL;
866 0 : fd_memset( ctx->shred_buffer, 0xFF, FD_NET_MTU );
867 :
868 0 : fd_histf_join( fd_histf_new( ctx->metrics->contact_info_cnt, FD_MHIST_MIN( SHRED, CLUSTER_CONTACT_INFO_CNT ),
869 0 : FD_MHIST_MAX( SHRED, CLUSTER_CONTACT_INFO_CNT ) ) );
870 0 : fd_histf_join( fd_histf_new( ctx->metrics->batch_sz, FD_MHIST_MIN( SHRED, BATCH_SZ ),
871 0 : FD_MHIST_MAX( SHRED, BATCH_SZ ) ) );
872 0 : fd_histf_join( fd_histf_new( ctx->metrics->batch_microblock_cnt, FD_MHIST_MIN( SHRED, BATCH_MICROBLOCK_CNT ),
873 0 : FD_MHIST_MAX( SHRED, BATCH_MICROBLOCK_CNT ) ) );
874 0 : fd_histf_join( fd_histf_new( ctx->metrics->shredding_timing, FD_MHIST_SECONDS_MIN( SHRED, SHREDDING_DURATION_SECONDS ),
875 0 : FD_MHIST_SECONDS_MAX( SHRED, SHREDDING_DURATION_SECONDS ) ) );
876 0 : fd_histf_join( fd_histf_new( ctx->metrics->add_shred_timing, FD_MHIST_SECONDS_MIN( SHRED, ADD_SHRED_DURATION_SECONDS ),
877 0 : FD_MHIST_SECONDS_MAX( SHRED, ADD_SHRED_DURATION_SECONDS ) ) );
878 0 : memset( ctx->metrics->shred_processing_result, '\0', sizeof(ctx->metrics->shred_processing_result) );
879 :
880 0 : ctx->pending_batch.microblock_cnt = 0UL;
881 0 : ctx->pending_batch.txn_cnt = 0UL;
882 0 : ctx->pending_batch.pos = 0UL;
883 0 : ctx->pending_batch.slot = 0UL;
884 0 : fd_memset( ctx->pending_batch.payload, 0, sizeof(ctx->pending_batch.payload) );
885 :
886 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
887 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
888 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
889 0 : }
890 :
891 : static ulong
892 : populate_allowed_seccomp( fd_topo_t const * topo,
893 : fd_topo_tile_t const * tile,
894 : ulong out_cnt,
895 0 : struct sock_filter * out ) {
896 0 : (void)topo;
897 0 : (void)tile;
898 :
899 0 : populate_sock_filter_policy_shred( out_cnt, out, (uint)fd_log_private_logfile_fd() );
900 0 : return sock_filter_policy_shred_instr_cnt;
901 0 : }
902 :
903 : static ulong
904 : populate_allowed_fds( fd_topo_t const * topo,
905 : fd_topo_tile_t const * tile,
906 : ulong out_fds_cnt,
907 0 : int * out_fds ) {
908 0 : (void)topo;
909 0 : (void)tile;
910 :
911 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
912 :
913 0 : ulong out_cnt = 0UL;
914 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
915 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
916 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
917 0 : return out_cnt;
918 0 : }
919 :
920 0 : #define STEM_BURST (4UL)
921 :
922 : /* See explanation in fd_pack */
923 0 : #define STEM_LAZY (128L*3000L)
924 :
925 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_shred_ctx_t
926 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_shred_ctx_t)
927 :
928 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
929 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
930 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
931 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
932 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
933 :
934 : #include "../../../../disco/stem/fd_stem.c"
935 :
936 : fd_topo_run_tile_t fd_tile_shred = {
937 : .name = "shred",
938 : .populate_allowed_seccomp = populate_allowed_seccomp,
939 : .populate_allowed_fds = populate_allowed_fds,
940 : .scratch_align = scratch_align,
941 : .scratch_footprint = scratch_footprint,
942 : .privileged_init = privileged_init,
943 : .unprivileged_init = unprivileged_init,
944 : .run = stem_run,
945 : };
|