Line data Source code
1 : #include "../tiles.h"
2 :
3 : #include "generated/fd_shred_tile_seccomp.h"
4 : #include "../../util/pod/fd_pod_format.h"
5 : #include "../shred/fd_shredder.h"
6 : #include "../shred/fd_shred_batch.h"
7 : #include "../shred/fd_shred_dest.h"
8 : #include "../shred/fd_fec_resolver.h"
9 : #include "../shred/fd_stake_ci.h"
10 : #include "../store/fd_store.h"
11 : #include "../keyguard/fd_keyload.h"
12 : #include "../keyguard/fd_keyguard.h"
13 : #include "../keyguard/fd_keyswitch.h"
14 : #include "../fd_disco.h"
15 : #include "../net/fd_net_tile.h"
16 : #include "../../flamenco/leaders/fd_leaders.h"
17 : #include "../../util/net/fd_net_headers.h"
18 : #include "../../flamenco/gossip/fd_gossip_types.h"
19 : #include "../../flamenco/types/fd_types.h"
20 : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
21 :
22 : /* The shred tile handles shreds from two data sources: shreds generated
23 : from microblocks from the banking tile, and shreds retransmitted from
24 : the network.
25 :
26 : They have rather different semantics, but at the end of the day, they
27 : both result in a bunch of shreds and FEC sets that need to be sent to
28 : the blockstore and on the network, which is why one tile handles
29 : both.
30 :
31 : We segment the memory for the two types of shreds into two halves of
32 : a dcache because they follow somewhat different flow control
33 : patterns. For flow control, the normal guarantee we want to provide
34 : is that the dcache entry is not overwritten unless the mcache entry
35 : has also been overwritten. The normal way to do this when using both
36 : cyclically and with a 1-to-1 mapping is to make the dcache at least
37 : `burst` entries bigger than the mcache.
38 :
39 : In this tile, we use one output mcache with one output dcache (which
40 : is logically partitioned into two) for the two sources of data. The
41 : worst case for flow control is when we're only sending with one of
42 : the dcache partitions at a time though, so we can consider them
43 : separately.
44 :
45 : From bank: Every FEC set triggers at least two mcache entries (one
46 : for parity and one for data), so at most, we have ceil(mcache
47 : depth/2) FEC sets exposed. This means we need to decompose dcache
48 : into at least ceil(mcache depth/2)+1 FEC sets.
49 :
50 : From the network: The FEC resolver doesn't use a cyclic order, but it
51 : does promise that once it returns an FEC set, it will return at least
52 : complete_depth FEC sets before returning it again. This means we
53 : want at most complete_depth-1 FEC sets exposed, so
54 : complete_depth=ceil(mcache depth/2)+1 FEC sets as above. The FEC
55 : resolver has the ability to keep individual shreds for partial_depth
56 : calls, but because in this version of the shred tile, we send each
57 : shred to all its destinations as soon as we get it, we don't need
58 : that functionality, so we set partial_depth=1.
59 :
60 : Adding these up, we get 2*ceil(mcache_depth/2)+3+fec_resolver_depth
61 : FEC sets, which is no more than mcache_depth+4+fec_resolver_depth.
62 : Each FEC is paired with 4 fd_shred34_t structs, so that means we need
63 : to decompose the dcache into 4*mcache_depth + 4*fec_resolver_depth +
64 : 16 fd_shred34_t structs.
65 :
66 : A note on parallelization. From the network, shreds are distributed
67 : to tiles by their signature, so all the shreds for a given FEC set
68 : are processed by the same tile. From bank, the original
69 : implementation used to parallelize by batch of microblocks (so within
70 : a block, batches were distributed to different tiles). To support
71 : chained merkle shreds, the current implementation processes all the
72 : batches on tile 0 -- this should be a temporary state while Solana
73 : moves to a newer shred format that support better parallelization. */
74 :
75 : /* The memory this tile uses is a bit complicated and has some logical
76 : aliasing to facilitate zero-copy use. We have a dcache containing
77 : fd_shred34_t objects, which are basically 34 fd_shred_t objects
78 : padded to their max size, where 34 is set so that the size of the
79 : fd_shred34_t object (including some metadata) is less than
80 : USHORT_MAX, which facilitates sending it using Tango. Then, for each
81 : set of 4 consecutive fd_shred34_t objects, we have an fd_fec_set_t.
82 : The first 34 data shreds point to the payload section of the payload
83 : section of each of the packets in the first fd_shred34_t. The other
84 : 33 data shreds point into the second fd_shred34_t. Similar for the
85 : parity shreds pointing into the third and fourth fd_shred34_t. */
86 :
87 : #define FD_SHRED_TILE_SCRATCH_ALIGN 128UL
88 :
89 0 : #define IN_KIND_CONTACT (0UL)
90 0 : #define IN_KIND_STAKE (1UL)
91 0 : #define IN_KIND_POH (2UL)
92 0 : #define IN_KIND_NET (3UL)
93 0 : #define IN_KIND_SIGN (4UL)
94 0 : #define IN_KIND_REPAIR (5UL)
95 0 : #define IN_KIND_IPECHO (6UL)
96 0 : #define IN_KIND_GOSSIP (7UL)
97 :
98 0 : #define NET_OUT_IDX 1
99 0 : #define SIGN_OUT_IDX 2
100 :
101 0 : #define DCACHE_ENTRIES_PER_FEC_SET (4UL)
102 : FD_STATIC_ASSERT( sizeof(fd_shred34_t) < USHORT_MAX, shred_34 );
103 : FD_STATIC_ASSERT( 34*DCACHE_ENTRIES_PER_FEC_SET >= FD_REEDSOL_DATA_SHREDS_MAX+FD_REEDSOL_PARITY_SHREDS_MAX, shred_34 );
104 : FD_STATIC_ASSERT( sizeof(fd_shred34_t) == FD_SHRED_STORE_MTU, shred_34 );
105 :
106 : FD_STATIC_ASSERT( sizeof(fd_entry_batch_meta_t)==56UL, poh_shred_mtu );
107 :
108 0 : #define FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT 2
109 :
110 : /* Number of entries in the block_ids table. Each entry is 32 byte.
111 : This table is used to keep track of block ids that we create
112 : when we're leader, so that we can access them whenever we need
113 : a *parent* block id for a new block. Larger table allows to
114 : retrieve older parent block ids. Currently it's set for worst
115 : case parent offset of USHORT_MAX (max allowed in a shred),
116 : making the total table 2MiB.
117 : See also comment on chained_merkle_root. */
118 0 : #define BLOCK_IDS_TABLE_CNT USHORT_MAX
119 :
120 : /* See note on parallelization above. Currently we process all batches in tile 0. */
121 : #if 1
122 : #define SHOULD_PROCESS_THESE_SHREDS ( ctx->round_robin_id==0 )
123 : #else
124 : #define SHOULD_PROCESS_THESE_SHREDS ( ctx->batch_cnt%ctx->round_robin_cnt==ctx->round_robin_id )
125 : #endif
126 :
127 : /* The behavior of the shred tile is slightly different for
128 : Frankendancer vs Firedancer. For example, Frankendancer produces
129 : chained merkle shreds, while Firedancer doesn't yet. We can check
130 : at runtime the difference by inspecting the topology. The simplest
131 : way is to test if ctx->store is initialized.
132 :
133 : FIXME don't assume only frank vs. fire */
134 : #define IS_FIREDANCER ( ctx->store!=NULL )
135 :
136 : typedef union {
137 : struct {
138 : fd_wksp_t * mem;
139 : ulong chunk0;
140 : ulong wmark;
141 : };
142 : fd_net_rx_bounds_t net_rx;
143 : } fd_shred_in_ctx_t;
144 :
145 : typedef struct {
146 : fd_shredder_t * shredder;
147 : fd_fec_resolver_t * resolver;
148 : fd_pubkey_t identity_key[1]; /* Just the public key */
149 :
150 : ulong round_robin_id;
151 : ulong round_robin_cnt;
152 : /* Number of batches shredded from PoH during the current slot.
153 : This should be the same for all the shred tiles. */
154 : ulong batch_cnt;
155 : /* Slot of the most recent microblock we've seen from PoH,
156 : or 0 if we haven't seen one yet */
157 : ulong slot;
158 :
159 : fd_keyswitch_t * keyswitch;
160 : fd_keyguard_client_t keyguard_client[1];
161 :
162 : /* shred34 and fec_sets are very related: fec_sets[i] has pointers
163 : to the shreds in shred34[4*i + k] for k=0,1,2,3. */
164 : fd_shred34_t * shred34;
165 : fd_fec_set_t * fec_sets;
166 :
167 : fd_stake_ci_t * stake_ci;
168 : /* These are used in between during_frag and after_frag */
169 : fd_shred_dest_weighted_t * new_dest_ptr;
170 : ulong new_dest_cnt;
171 : ulong shredded_txn_cnt;
172 :
173 : ulong poh_in_expect_seq;
174 :
175 : ushort net_id;
176 :
177 : int skip_frag;
178 :
179 : ulong adtl_dests_leader_cnt;
180 : fd_shred_dest_weighted_t adtl_dests_leader [ FD_TOPO_ADTL_DESTS_MAX ];
181 : ulong adtl_dests_retransmit_cnt;
182 : fd_shred_dest_weighted_t adtl_dests_retransmit[ FD_TOPO_ADTL_DESTS_MAX ];
183 :
184 : fd_ip4_udp_hdrs_t data_shred_net_hdr [1];
185 : fd_ip4_udp_hdrs_t parity_shred_net_hdr[1];
186 :
187 : ulong shredder_fec_set_idx; /* In [0, shredder_max_fec_set_idx) */
188 : ulong shredder_max_fec_set_idx; /* exclusive */
189 :
190 : uchar shredder_merkle_root[32];
191 :
192 : ulong send_fec_set_idx[ FD_SHRED_BATCH_FEC_SETS_MAX ];
193 : ulong send_fec_set_cnt;
194 : ulong tsorig; /* timestamp of the last packet in compressed form */
195 :
196 : /* Includes Ethernet, IP, UDP headers */
197 : ulong shred_buffer_sz;
198 : uchar shred_buffer[ FD_NET_MTU ];
199 :
200 : fd_shred_in_ctx_t in[ 32 ];
201 : int in_kind[ 32 ];
202 :
203 : fd_wksp_t * net_out_mem;
204 : ulong net_out_chunk0;
205 : ulong net_out_wmark;
206 : ulong net_out_chunk;
207 :
208 : ulong store_out_idx;
209 : fd_wksp_t * store_out_mem;
210 : ulong store_out_chunk0;
211 : ulong store_out_wmark;
212 : ulong store_out_chunk;
213 :
214 : /* This is the output link for shreds that is currently consumed by
215 : the repair and replay tile. */
216 : ulong shred_out_idx;
217 : fd_wksp_t * shred_out_mem;
218 : ulong shred_out_chunk0;
219 : ulong shred_out_wmark;
220 : ulong shred_out_chunk;
221 :
222 : fd_store_t * store;
223 :
224 : fd_gossip_update_message_t gossip_upd_buf[1];
225 :
226 : struct {
227 : fd_histf_t contact_info_cnt[ 1 ];
228 : fd_histf_t batch_sz[ 1 ];
229 : fd_histf_t batch_microblock_cnt[ 1 ];
230 : fd_histf_t shredding_timing[ 1 ];
231 : fd_histf_t add_shred_timing[ 1 ];
232 : ulong shred_processing_result[ FD_FEC_RESOLVER_ADD_SHRED_RETVAL_CNT+FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT ];
233 : ulong invalid_block_id_cnt;
234 : ulong shred_rejected_unchained_cnt;
235 : ulong repair_rcv_cnt;
236 : ulong repair_rcv_bytes;
237 : ulong turbine_rcv_cnt;
238 : ulong turbine_rcv_bytes;
239 : fd_histf_t store_insert_wait[ 1 ];
240 : fd_histf_t store_insert_work[ 1 ];
241 : } metrics[ 1 ];
242 :
243 : struct {
244 : ulong txn_cnt;
245 : ulong pos; /* in payload, range [0, FD_SHRED_BATCH_RAW_BUF_SZ-8UL) */
246 : ulong slot; /* set to 0 when pos==0 */
247 : union {
248 : struct {
249 : ulong microblock_cnt;
250 : uchar payload[ FD_SHRED_BATCH_RAW_BUF_SZ - 8UL ];
251 : };
252 : uchar raw[ FD_SHRED_BATCH_RAW_BUF_SZ ];
253 : };
254 : } pending_batch;
255 :
256 : fd_shred_features_activation_t features_activation[1];
257 : /* too large to be left in the stack */
258 : fd_shred_dest_idx_t scratchpad_dests[ FD_SHRED_DEST_MAX_FANOUT*(FD_REEDSOL_DATA_SHREDS_MAX+FD_REEDSOL_PARITY_SHREDS_MAX) ];
259 :
260 : uchar * chained_merkle_root;
261 : fd_bmtree_node_t out_merkle_roots[ FD_SHRED_BATCH_FEC_SETS_MAX ];
262 : uchar block_ids[ BLOCK_IDS_TABLE_CNT ][ FD_SHRED_MERKLE_ROOT_SZ ];
263 : } fd_shred_ctx_t;
264 :
265 : /* shred features are generally considered active at the epoch *following*
266 : the epoch in which the feature gate is activated.
267 :
268 : As an optimization, when the activation slot is received, it is converted
269 : into the first slot of the subsequent epoch. This allows for a more
270 : efficient check (shred_slot >= feature_slot) and avoids the overhead of
271 : repeatedly converting slots into epochs for comparison.
272 :
273 : In Agave, this is done with check_feature_activation():
274 : https://github.com/anza-xyz/agave/blob/v3.1.4/turbine/src/cluster_nodes.rs#L771
275 : https://github.com/anza-xyz/agave/blob/v3.1.4/core/src/shred_fetch_stage.rs#L456
276 :
277 : Note that this function does not currently handle warmup epochs (i.e.,
278 : local clusters). This limitation is acceptable for now, as it only
279 : affects thetransition period during which a feature is being
280 : implemented and activated. */
281 : static inline ulong
282 0 : fd_shred_get_feature_activation_slot0( ulong feature_slot, fd_shred_ctx_t * ctx ) {
283 : /* we need info about the epoch schedule (specifically slot_cnt).
284 : if we don't know any schedule (yet), we return ULONG_MAX, i.e. feature inactive. */
285 0 : fd_epoch_leaders_t * lsched = ctx->stake_ci->epoch_info[ 0 ].lsched
286 0 : ? ctx->stake_ci->epoch_info[ 0 ].lsched
287 0 : : ctx->stake_ci->epoch_info[ 1 ].lsched;
288 0 : if( lsched==NULL ) {
289 0 : return ULONG_MAX;
290 0 : }
291 : /* compute the activation epoch, add one, return the first slot. */
292 0 : fd_epoch_schedule_t default_schedule[1] = {{ lsched->slot_cnt, lsched->slot_cnt, 0, 0, 0 }};
293 0 : ulong feature_epoch = 1 + fd_slot_to_epoch( default_schedule, feature_slot, NULL );
294 0 : return fd_epoch_slot0( default_schedule, feature_epoch );
295 0 : }
296 :
297 : FD_FN_CONST static inline ulong
298 0 : scratch_align( void ) {
299 0 : return 128UL;
300 0 : }
301 :
302 : FD_FN_PURE static inline ulong
303 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
304 :
305 0 : ulong fec_resolver_footprint = fd_fec_resolver_footprint( tile->shred.fec_resolver_depth, 1UL, tile->shred.depth,
306 0 : 128UL * tile->shred.fec_resolver_depth );
307 0 : ulong fec_set_cnt = tile->shred.depth + tile->shred.fec_resolver_depth + 4UL;
308 :
309 0 : ulong l = FD_LAYOUT_INIT;
310 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_shred_ctx_t), sizeof(fd_shred_ctx_t) );
311 0 : l = FD_LAYOUT_APPEND( l, fd_stake_ci_align(), fd_stake_ci_footprint() );
312 0 : l = FD_LAYOUT_APPEND( l, fd_fec_resolver_align(), fec_resolver_footprint );
313 0 : l = FD_LAYOUT_APPEND( l, fd_shredder_align(), fd_shredder_footprint() );
314 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_fec_set_t), sizeof(fd_fec_set_t)*fec_set_cnt );
315 0 : return FD_LAYOUT_FINI( l, scratch_align() );
316 0 : }
317 :
318 : static inline void
319 0 : during_housekeeping( fd_shred_ctx_t * ctx ) {
320 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
321 0 : ulong seq_must_complete = ctx->keyswitch->param;
322 :
323 0 : if( FD_UNLIKELY( fd_seq_lt( ctx->poh_in_expect_seq, seq_must_complete ) ) ) {
324 : /* See fd_keyswitch.h, we need to flush any in-flight shreds from
325 : the leader pipeline before switching key. */
326 0 : FD_LOG_WARNING(( "Flushing in-flight unpublished shreds, must reach seq %lu, currently at %lu ...", seq_must_complete, ctx->poh_in_expect_seq ));
327 0 : return;
328 0 : }
329 :
330 0 : memcpy( ctx->identity_key->uc, ctx->keyswitch->bytes, 32UL );
331 0 : fd_stake_ci_set_identity( ctx->stake_ci, ctx->identity_key );
332 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
333 0 : }
334 0 : }
335 :
336 : static inline void
337 0 : metrics_write( fd_shred_ctx_t * ctx ) {
338 0 : FD_MHIST_COPY( SHRED, CLUSTER_CONTACT_INFO_CNT, ctx->metrics->contact_info_cnt );
339 0 : FD_MHIST_COPY( SHRED, BATCH_SZ, ctx->metrics->batch_sz );
340 0 : FD_MHIST_COPY( SHRED, BATCH_MICROBLOCK_CNT, ctx->metrics->batch_microblock_cnt );
341 0 : FD_MHIST_COPY( SHRED, SHREDDING_DURATION_SECONDS, ctx->metrics->shredding_timing );
342 0 : FD_MHIST_COPY( SHRED, ADD_SHRED_DURATION_SECONDS, ctx->metrics->add_shred_timing );
343 0 : FD_MCNT_SET ( SHRED, SHRED_REPAIR_RCV, ctx->metrics->repair_rcv_cnt );
344 0 : FD_MCNT_SET ( SHRED, SHRED_REPAIR_RCV_BYTES, ctx->metrics->repair_rcv_bytes );
345 0 : FD_MCNT_SET ( SHRED, SHRED_TURBINE_RCV, ctx->metrics->turbine_rcv_cnt );
346 0 : FD_MCNT_SET ( SHRED, SHRED_TURBINE_RCV_BYTES, ctx->metrics->turbine_rcv_bytes );
347 :
348 0 : FD_MCNT_SET ( SHRED, INVALID_BLOCK_ID, ctx->metrics->invalid_block_id_cnt );
349 0 : FD_MCNT_SET ( SHRED, SHRED_REJECTED_UNCHAINED, ctx->metrics->shred_rejected_unchained_cnt );
350 0 : FD_MHIST_COPY( SHRED, STORE_INSERT_WAIT, ctx->metrics->store_insert_wait );
351 0 : FD_MHIST_COPY( SHRED, STORE_INSERT_WORK, ctx->metrics->store_insert_work );
352 :
353 0 : FD_MCNT_ENUM_COPY( SHRED, SHRED_PROCESSED, ctx->metrics->shred_processing_result );
354 0 : }
355 :
356 : static inline void
357 : handle_new_cluster_contact_info( fd_shred_ctx_t * ctx,
358 0 : uchar const * buf ) {
359 0 : ulong const * header = (ulong const *)fd_type_pun_const( buf );
360 :
361 0 : ulong dest_cnt = header[ 0 ];
362 0 : fd_histf_sample( ctx->metrics->contact_info_cnt, dest_cnt );
363 :
364 0 : if( dest_cnt >= MAX_SHRED_DESTS )
365 0 : FD_LOG_ERR(( "Cluster nodes had %lu destinations, which was more than the max of %lu", dest_cnt, MAX_SHRED_DESTS ));
366 :
367 0 : fd_shred_dest_wire_t const * in_dests = fd_type_pun_const( header+1UL );
368 0 : fd_shred_dest_weighted_t * dests = fd_stake_ci_dest_add_init( ctx->stake_ci );
369 :
370 0 : ctx->new_dest_ptr = dests;
371 0 : ctx->new_dest_cnt = dest_cnt;
372 :
373 0 : for( ulong i=0UL; i<dest_cnt; i++ ) {
374 0 : memcpy( dests[i].pubkey.uc, in_dests[i].pubkey, 32UL );
375 0 : dests[i].ip4 = in_dests[i].ip4_addr;
376 0 : dests[i].port = in_dests[i].udp_port;
377 0 : }
378 0 : }
379 :
380 : static inline void
381 0 : finalize_new_cluster_contact_info( fd_shred_ctx_t * ctx ) {
382 0 : fd_stake_ci_dest_add_fini( ctx->stake_ci, ctx->new_dest_cnt );
383 0 : }
384 :
385 : static inline int
386 : before_frag( fd_shred_ctx_t * ctx,
387 : ulong in_idx,
388 : ulong seq,
389 0 : ulong sig ) {
390 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_IPECHO ) ) {
391 0 : FD_TEST( sig!=0UL && sig<=USHORT_MAX );
392 0 : fd_shredder_set_shred_version ( ctx->shredder, (ushort)sig );
393 0 : fd_fec_resolver_set_shred_version( ctx->resolver, (ushort)sig );
394 0 : return 1;
395 0 : }
396 :
397 0 : if( FD_UNLIKELY( !ctx->shredder->shred_version ) ) return -1;
398 :
399 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH ) ) {
400 0 : ctx->poh_in_expect_seq = seq+1UL;
401 0 : return (int)(fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_MICROBLOCK) & (int)(fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_FEAT_ACT_SLOT);
402 0 : }
403 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
404 0 : return (int)(fd_disco_netmux_sig_proto( sig )!=DST_PROTO_SHRED) & (int)(fd_disco_netmux_sig_proto( sig )!=DST_PROTO_REPAIR);
405 0 : }
406 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ){
407 0 : return sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO &&
408 0 : sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE;
409 0 : }
410 0 : return 0;
411 0 : }
412 :
413 : static void
414 : during_frag( fd_shred_ctx_t * ctx,
415 : ulong in_idx,
416 : ulong seq FD_PARAM_UNUSED,
417 : ulong sig,
418 : ulong chunk,
419 : ulong sz,
420 0 : ulong ctl ) {
421 :
422 0 : ctx->skip_frag = 0;
423 :
424 0 : ctx->tsorig = fd_frag_meta_ts_comp( fd_tickcount() );
425 :
426 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPAIR ) ) {
427 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
428 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
429 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
430 :
431 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
432 0 : fd_memcpy( ctx->shred_buffer, dcache_entry, sz );
433 0 : return;
434 0 : }
435 :
436 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_CONTACT ) ) {
437 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
438 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
439 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
440 :
441 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
442 0 : handle_new_cluster_contact_info( ctx, dcache_entry );
443 0 : return;
444 0 : }
445 :
446 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
447 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
448 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
449 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
450 0 : uchar const * gossip_upd_msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
451 0 : fd_memcpy( ctx->gossip_upd_buf, gossip_upd_msg, sz );
452 0 : return;
453 0 : }
454 :
455 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) {
456 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
457 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
458 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
459 :
460 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
461 0 : fd_stake_ci_stake_msg_init( ctx->stake_ci, fd_type_pun_const( dcache_entry ) );
462 0 : return;
463 0 : }
464 :
465 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH ) ) {
466 0 : ctx->send_fec_set_cnt = 0UL;
467 :
468 0 : if( FD_UNLIKELY( (fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_FEAT_ACT_SLOT) ) ) {
469 : /* There is a subset of FD_SHRED_FEATURES_ACTIVATION_... slots that
470 : the shred tile needs to be aware of. Since this requires the
471 : bank, we are forced (so far) to receive them from the poh tile
472 : (as a POH_PKT_TYPE_FEAT_ACT_SLOT). This is not elegant, and it
473 : should be revised in the future (TODO), but it provides a
474 : "temporary" working solution to handle features activation. */
475 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
476 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=(sizeof(fd_shred_features_activation_t)) ) )
477 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
478 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
479 :
480 0 : fd_shred_features_activation_t const * act_data = (fd_shred_features_activation_t const *)dcache_entry;
481 0 : memcpy( ctx->features_activation, act_data, sizeof(fd_shred_features_activation_t) );
482 :
483 0 : for( ulong i=0; i<FD_SHRED_FEATURES_ACTIVATION_SLOT_CNT; i++ ) {
484 0 : ctx->features_activation->slots[i] = fd_shred_get_feature_activation_slot0( ctx->features_activation->slots[i], ctx );
485 0 : }
486 0 : }
487 0 : else { /* (fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK) */
488 : /* This is a frag from the PoH tile. We'll copy it to our pending
489 : microblock batch and shred it if necessary (last in block or
490 : above watermark). We just go ahead and shred it here, even
491 : though we may get overrun. If we do end up getting overrun, we
492 : just won't send these shreds out and we'll reuse the FEC set for
493 : the next one. From a higher level though, if we do get overrun,
494 : a bunch of shreds will never be transmitted, and we'll end up
495 : producing a block that never lands on chain. */
496 :
497 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
498 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_POH_SHRED_MTU ||
499 0 : sz<(sizeof(fd_entry_batch_meta_t)+sizeof(fd_entry_batch_header_t)) ) )
500 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
501 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
502 :
503 0 : fd_entry_batch_meta_t const * entry_meta = (fd_entry_batch_meta_t const *)dcache_entry;
504 0 : uchar const * entry = dcache_entry + sizeof(fd_entry_batch_meta_t);
505 0 : ulong entry_sz = sz - sizeof(fd_entry_batch_meta_t);
506 :
507 0 : fd_entry_batch_header_t const * microblock = (fd_entry_batch_header_t const *)entry;
508 :
509 : /* It should never be possible for this to fail, but we check it
510 : anyway. */
511 0 : FD_TEST( entry_sz + ctx->pending_batch.pos <= sizeof(ctx->pending_batch.payload) );
512 :
513 0 : ulong target_slot = fd_disco_poh_sig_slot( sig );
514 0 : if( FD_UNLIKELY( (ctx->pending_batch.microblock_cnt>0) & (ctx->pending_batch.slot!=target_slot) ) ) {
515 : /* TODO: The Agave client sends a dummy entry batch with only 1
516 : byte and the block-complete bit set. This helps other
517 : validators know that the block is dead and they should not try
518 : to continue building a fork on it. We probably want a similar
519 : approach eventually. */
520 0 : FD_LOG_WARNING(( "Abandoning %lu microblocks for slot %lu and switching to slot %lu",
521 0 : ctx->pending_batch.microblock_cnt, ctx->pending_batch.slot, target_slot ));
522 0 : ctx->pending_batch.slot = 0UL;
523 0 : ctx->pending_batch.pos = 0UL;
524 0 : ctx->pending_batch.microblock_cnt = 0UL;
525 0 : ctx->pending_batch.txn_cnt = 0UL;
526 0 : ctx->batch_cnt = 0UL;
527 :
528 0 : FD_MCNT_INC( SHRED, MICROBLOCKS_ABANDONED, 1UL );
529 0 : }
530 :
531 0 : ctx->pending_batch.slot = target_slot;
532 0 : if( FD_UNLIKELY( target_slot!=ctx->slot )) {
533 : /* Reset batch count if we are in a new slot */
534 0 : ctx->batch_cnt = 0UL;
535 0 : ctx->slot = target_slot;
536 :
537 : /* At the beginning of a new slot, prepare chained_merkle_root.
538 : chained_merkle_root is initialized at the block_id of the parent
539 : block, there's two cases:
540 :
541 : 1. block_id is passed in by the poh tile:
542 : - it's always passed when parent block had a different leader
543 : - it may be passed when we were leader for parent block (there
544 : are race conditions when it's not passed)
545 :
546 : 2. block_id is taken from block_ids table if we were the leader
547 : for the parent block (when we were NOT the leader, because of
548 : equivocation, we can't store block_id in the table)
549 :
550 : chained_merkle_root is stored in block_ids table at target_slot
551 : and it's progressively updated as more microblocks are received.
552 : As a result, when we move to a new slot, the block_ids table at
553 : the old slot will contain the block_id.
554 :
555 : The block_ids table is designed to protect against the race condition
556 : case in 1., therefore the table may not be set in some cases, e.g. if
557 : a validator (re)starts, but in those cases we don't expect the race
558 : condition to apply. */
559 0 : ctx->chained_merkle_root = ctx->block_ids[ target_slot % BLOCK_IDS_TABLE_CNT ];
560 0 : if( FD_UNLIKELY( SHOULD_PROCESS_THESE_SHREDS ) ) {
561 0 : if( FD_LIKELY( entry_meta->parent_block_id_valid ) ) {
562 : /* 1. Initialize chained_merkle_root sent from poh tile */
563 0 : memcpy( ctx->chained_merkle_root, entry_meta->parent_block_id, FD_SHRED_MERKLE_ROOT_SZ );
564 0 : } else {
565 0 : ulong parent_slot = target_slot - entry_meta->parent_offset;
566 0 : fd_epoch_leaders_t const * lsched = fd_stake_ci_get_lsched_for_slot( ctx->stake_ci, parent_slot );
567 0 : fd_pubkey_t const * slot_leader = fd_epoch_leaders_get( lsched, parent_slot );
568 :
569 0 : if( lsched && slot_leader && fd_memeq( slot_leader, ctx->identity_key, sizeof(fd_pubkey_t) ) ) {
570 : /* 2. Initialize chained_merkle_root from block_ids table, if we were the leader */
571 0 : memcpy( ctx->chained_merkle_root, ctx->block_ids[ parent_slot % BLOCK_IDS_TABLE_CNT ], FD_SHRED_MERKLE_ROOT_SZ );
572 0 : } else {
573 : /* This should never happen, log a metric and set chained_merkle_root to 0 */
574 0 : ctx->metrics->invalid_block_id_cnt++;
575 0 : memset( ctx->chained_merkle_root, 0, FD_SHRED_MERKLE_ROOT_SZ );
576 0 : }
577 0 : }
578 0 : }
579 0 : }
580 :
581 0 : if( FD_LIKELY( !SHOULD_PROCESS_THESE_SHREDS ) ) {
582 : /* If we are not processing this batch, filter in after_frag. */
583 0 : ctx->skip_frag = 1;
584 0 : }
585 :
586 0 : ulong pending_batch_wmark = FD_SHRED_BATCH_WMARK_CHAINED;
587 0 : uchar * chained_merkle_root = ctx->chained_merkle_root;
588 0 : ulong load_for_32_shreds = FD_SHREDDER_CHAINED_FEC_SET_PAYLOAD_SZ;
589 : /* All fec sets in the last batch of a block need to be resigned.
590 : This needs to match Agave's behavior - as a reference, see:
591 : https://github.com/anza-xyz/agave/blob/v2.3/ledger/src/shred/merkle.rs#L1040 */
592 0 : if( FD_UNLIKELY( entry_meta->block_complete ) ) {
593 0 : pending_batch_wmark = FD_SHRED_BATCH_WMARK_RESIGNED;
594 : /* chained_merkle_root also applies to resigned FEC sets. */
595 0 : load_for_32_shreds = FD_SHREDDER_RESIGNED_FEC_SET_PAYLOAD_SZ;
596 0 : }
597 :
598 : /* If this microblock completes the block, the batch is then
599 : finalized here. Otherwise, we check whether the new entry
600 : would exceed the pending_batch_wmark. If true, then the
601 : batch is closed now, shredded, and a new batch is started
602 : with the incoming microblock. If false, no shredding takes
603 : place, and the microblock is added to the current batch. */
604 0 : int batch_would_exceed_wmark = ( ctx->pending_batch.pos + entry_sz ) > pending_batch_wmark;
605 0 : int include_in_current_batch = entry_meta->block_complete | ( !batch_would_exceed_wmark );
606 0 : int process_current_batch = entry_meta->block_complete | batch_would_exceed_wmark;
607 0 : int init_new_batch = !include_in_current_batch;
608 :
609 0 : if( FD_LIKELY( include_in_current_batch ) ) {
610 0 : if( FD_UNLIKELY( SHOULD_PROCESS_THESE_SHREDS ) ) {
611 : /* Ugh, yet another memcpy */
612 0 : fd_memcpy( ctx->pending_batch.payload + ctx->pending_batch.pos, entry, entry_sz );
613 0 : }
614 0 : ctx->pending_batch.pos += entry_sz;
615 0 : ctx->pending_batch.microblock_cnt += 1UL;
616 0 : ctx->pending_batch.txn_cnt += microblock->txn_cnt;
617 0 : }
618 :
619 0 : if( FD_LIKELY( process_current_batch )) {
620 : /* Batch and padding size calculation. */
621 0 : ulong batch_sz = sizeof(ulong) + ctx->pending_batch.pos; /* without padding */
622 0 : ulong batch_sz_padded = load_for_32_shreds * ( ( batch_sz + load_for_32_shreds - 1UL ) / load_for_32_shreds );
623 0 : ulong padding_sz = batch_sz_padded - batch_sz;
624 :
625 0 : if( FD_UNLIKELY( SHOULD_PROCESS_THESE_SHREDS ) ) {
626 : /* If it's our turn, shred this batch. FD_UNLIKELY because shred
627 : tile cnt generally >= 2 */
628 :
629 0 : long shredding_timing = -fd_tickcount();
630 :
631 0 : fd_memset( ctx->pending_batch.payload + ctx->pending_batch.pos, 0, padding_sz );
632 :
633 0 : ctx->send_fec_set_cnt = 0UL; /* verbose */
634 0 : ctx->shredded_txn_cnt = ctx->pending_batch.txn_cnt;
635 :
636 0 : fd_shredder_init_batch( ctx->shredder, ctx->pending_batch.raw, batch_sz_padded, target_slot, entry_meta );
637 :
638 0 : ulong pend_sz = batch_sz_padded;
639 0 : ulong pend_idx = 0;
640 0 : while( pend_sz > 0UL ) {
641 :
642 0 : fd_fec_set_t * out = ctx->fec_sets + ctx->shredder_fec_set_idx;
643 :
644 0 : FD_TEST( fd_shredder_next_fec_set( ctx->shredder, out, chained_merkle_root, ctx->out_merkle_roots[pend_idx].hash ) );
645 :
646 0 : d_rcvd_join( d_rcvd_new( d_rcvd_delete( d_rcvd_leave( out->data_shred_rcvd ) ) ) );
647 0 : p_rcvd_join( p_rcvd_new( p_rcvd_delete( p_rcvd_leave( out->parity_shred_rcvd ) ) ) );
648 :
649 0 : ctx->send_fec_set_idx[ ctx->send_fec_set_cnt ] = ctx->shredder_fec_set_idx;
650 0 : ctx->send_fec_set_cnt += 1UL;
651 0 : ctx->shredder_fec_set_idx = (ctx->shredder_fec_set_idx+1UL)%ctx->shredder_max_fec_set_idx;
652 :
653 0 : pend_sz -= load_for_32_shreds;
654 0 : pend_idx++;
655 0 : }
656 :
657 0 : fd_shredder_fini_batch( ctx->shredder );
658 0 : shredding_timing += fd_tickcount();
659 :
660 : /* Update metrics */
661 0 : fd_histf_sample( ctx->metrics->batch_sz, batch_sz /* without padding */ );
662 0 : fd_histf_sample( ctx->metrics->batch_microblock_cnt, ctx->pending_batch.microblock_cnt );
663 0 : fd_histf_sample( ctx->metrics->shredding_timing, (ulong)shredding_timing );
664 0 : } else {
665 0 : ctx->send_fec_set_cnt = 0UL; /* verbose */
666 :
667 0 : ulong shred_type = FD_SHRED_TYPE_MERKLE_DATA_CHAINED;
668 0 : if( FD_UNLIKELY( entry_meta->block_complete ) ) {
669 0 : shred_type = FD_SHRED_TYPE_MERKLE_DATA_CHAINED_RESIGNED;
670 0 : }
671 0 : if( FD_LIKELY( IS_FIREDANCER ) ) {
672 0 : shred_type = FD_SHRED_TYPE_MERKLE_DATA;
673 0 : }
674 0 : fd_shredder_skip_batch( ctx->shredder, batch_sz_padded, target_slot, shred_type );
675 0 : }
676 :
677 0 : ctx->pending_batch.slot = 0UL;
678 0 : ctx->pending_batch.pos = 0UL;
679 0 : ctx->pending_batch.microblock_cnt = 0UL;
680 0 : ctx->pending_batch.txn_cnt = 0UL;
681 0 : ctx->batch_cnt++;
682 0 : }
683 :
684 0 : if( FD_UNLIKELY( init_new_batch ) ) {
685 : /* TODO: this assumes that SHOULD_PROCESS_THESE_SHREDS is
686 : constant across batches. Otherwise, the condition may
687 : need to be removed (or adjusted). */
688 0 : if( FD_UNLIKELY( SHOULD_PROCESS_THESE_SHREDS ) ) {
689 : /* Ugh, yet another memcpy */
690 0 : fd_memcpy( ctx->pending_batch.payload + 0UL /* verbose */, entry, entry_sz );
691 0 : }
692 0 : ctx->pending_batch.slot = target_slot;
693 0 : ctx->pending_batch.pos = entry_sz;
694 0 : ctx->pending_batch.microblock_cnt = 1UL;
695 0 : ctx->pending_batch.txn_cnt = microblock->txn_cnt;
696 0 : }
697 0 : }
698 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
699 : /* The common case, from the net tile. The FEC resolver API does
700 : not present a prepare/commit model. If we get overrun between
701 : when the FEC resolver verifies the signature and when it stores
702 : the local copy, we could end up storing and retransmitting
703 : garbage. Instead we copy it locally, sadly, and only give it to
704 : the FEC resolver when we know it won't be overrun anymore. */
705 0 : uchar const * dcache_entry = fd_net_rx_translate_frag( &ctx->in[ in_idx ].net_rx, chunk, ctl, sz );
706 0 : ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
707 0 : FD_TEST( hdr_sz <= sz ); /* Should be ensured by the net tile */
708 0 : fd_shred_t const * shred = fd_shred_parse( dcache_entry+hdr_sz, sz-hdr_sz );
709 0 : if( FD_UNLIKELY( !shred ) ) {
710 0 : ctx->skip_frag = 1;
711 0 : return;
712 0 : };
713 :
714 0 : if( FD_UNLIKELY( fd_disco_netmux_sig_proto( sig )==DST_PROTO_REPAIR ) ) {
715 0 : ctx->metrics->repair_rcv_cnt++;
716 0 : ctx->metrics->repair_rcv_bytes += sz;
717 0 : } else {
718 0 : ctx->metrics->turbine_rcv_cnt++;
719 0 : ctx->metrics->turbine_rcv_bytes += sz;
720 0 : }
721 :
722 : /* Drop unchained merkle shreds */
723 0 : int is_unchained = !fd_shred_is_chained( fd_shred_type( shred->variant ) );
724 0 : if( FD_UNLIKELY( is_unchained ) ) {
725 0 : ctx->metrics->shred_rejected_unchained_cnt++;
726 0 : ctx->skip_frag = 1;
727 0 : return;
728 0 : };
729 :
730 : /* all shreds in the same FEC set will have the same signature
731 : so we can round-robin shreds between the shred tiles based on
732 : just the signature without splitting individual FEC sets. */
733 0 : ulong sig = fd_ulong_load_8( shred->signature );
734 0 : if( FD_LIKELY( sig%ctx->round_robin_cnt!=ctx->round_robin_id ) ) {
735 0 : ctx->skip_frag = 1;
736 0 : return;
737 0 : }
738 0 : fd_memcpy( ctx->shred_buffer, dcache_entry+hdr_sz, sz-hdr_sz );
739 0 : ctx->shred_buffer_sz = sz-hdr_sz;
740 0 : }
741 0 : }
742 :
743 : static inline void
744 : send_shred( fd_shred_ctx_t * ctx,
745 : fd_stem_context_t * stem,
746 : fd_shred_t const * shred,
747 : fd_shred_dest_weighted_t const * dest,
748 0 : ulong tsorig ) {
749 :
750 0 : if( FD_UNLIKELY( !dest->ip4 ) ) return;
751 :
752 0 : uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
753 :
754 0 : int is_data = fd_shred_is_data( fd_shred_type( shred->variant ) );
755 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
756 0 : *hdr = *( is_data ? ctx->data_shred_net_hdr : ctx->parity_shred_net_hdr );
757 :
758 0 : fd_ip4_hdr_t * ip4 = hdr->ip4;
759 0 : ip4->daddr = dest->ip4;
760 0 : ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
761 0 : ip4->check = 0U;
762 0 : ip4->check = fd_ip4_hdr_check_fast( ip4 );
763 :
764 0 : hdr->udp->net_dport = fd_ushort_bswap( dest->port );
765 :
766 0 : ulong shred_sz = fd_ulong_if( is_data, FD_SHRED_MIN_SZ, FD_SHRED_MAX_SZ );
767 0 : #if FD_HAS_AVX
768 : /* We're going to copy this shred potentially a bunch of times without
769 : reading it again, and we'd rather not thrash our cache, so we want
770 : to use non-temporal writes here. We need to make sure we don't
771 : touch the cache line containing the network headers that we just
772 : wrote to though. We know the destination is 64 byte aligned. */
773 0 : FD_STATIC_ASSERT( sizeof(*hdr)<64UL, non_temporal );
774 : /* src[0:sizeof(hdrs)] is invalid, but now we want to copy
775 : dest[i]=src[i] for i>=sizeof(hdrs), so it simplifies the code. */
776 0 : uchar const * src = (uchar const *)((ulong)shred - sizeof(fd_ip4_udp_hdrs_t));
777 0 : memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), src+sizeof(fd_ip4_udp_hdrs_t), 64UL-sizeof(fd_ip4_udp_hdrs_t) );
778 :
779 0 : ulong end_offset = shred_sz + sizeof(fd_ip4_udp_hdrs_t);
780 0 : ulong i;
781 0 : for( i=64UL; end_offset-i<64UL; i+=64UL ) {
782 0 : # if FD_HAS_AVX512
783 0 : _mm512_stream_si512( (void *)(packet+i ), _mm512_loadu_si512( (void const *)(src+i ) ) );
784 : # else
785 0 : _mm256_stream_si256( (void *)(packet+i ), _mm256_loadu_si256( (void const *)(src+i ) ) );
786 0 : _mm256_stream_si256( (void *)(packet+i+32UL), _mm256_loadu_si256( (void const *)(src+i+32UL) ) );
787 0 : # endif
788 0 : }
789 0 : _mm_sfence();
790 0 : fd_memcpy( packet+i, src+i, end_offset-i ); /* Copy the last partial cache line */
791 :
792 : #else
793 : fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), shred, shred_sz );
794 : #endif
795 :
796 0 : ulong pkt_sz = shred_sz + sizeof(fd_ip4_udp_hdrs_t);
797 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
798 0 : ulong sig = fd_disco_netmux_sig( dest->ip4, dest->port, dest->ip4, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
799 0 : ulong const chunk = ctx->net_out_chunk;
800 0 : fd_stem_publish( stem, NET_OUT_IDX, sig, chunk, pkt_sz, 0UL, tsorig, tspub );
801 0 : ctx->net_out_chunk = fd_dcache_compact_next( chunk, pkt_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
802 0 : }
803 :
804 : static void
805 : after_frag( fd_shred_ctx_t * ctx,
806 : ulong in_idx,
807 : ulong seq,
808 : ulong sig,
809 : ulong sz,
810 : ulong tsorig,
811 : ulong _tspub,
812 0 : fd_stem_context_t * stem ) {
813 0 : (void)seq;
814 0 : (void)sz;
815 0 : (void)tsorig;
816 0 : (void)_tspub;
817 :
818 0 : if( FD_UNLIKELY( ctx->skip_frag ) ) return;
819 :
820 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_CONTACT ) ) {
821 0 : finalize_new_cluster_contact_info( ctx );
822 0 : return;
823 0 : }
824 :
825 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) {
826 0 : fd_stake_ci_stake_msg_fini( ctx->stake_ci );
827 0 : return;
828 0 : }
829 :
830 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
831 0 : if( ctx->gossip_upd_buf->tag==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ) {
832 0 : fd_contact_info_t const * ci = ctx->gossip_upd_buf->contact_info.contact_info;
833 0 : fd_ip4_port_t tvu_addr = ci->sockets[ FD_CONTACT_INFO_SOCKET_TVU ];
834 0 : if( !tvu_addr.l ){
835 0 : fd_stake_ci_dest_remove( ctx->stake_ci, &ci->pubkey );
836 0 : } else {
837 0 : fd_stake_ci_dest_update( ctx->stake_ci, &ci->pubkey, tvu_addr.addr, fd_ushort_bswap( tvu_addr.port ) );
838 0 : }
839 0 : } else if( ctx->gossip_upd_buf->tag==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE ) {
840 0 : if( FD_UNLIKELY( !memcmp( ctx->identity_key->uc, ctx->gossip_upd_buf->origin_pubkey, 32UL ) ) ) {
841 : /* If our own contact info was dropped, we update with dummy IP
842 : instead of removing since stake_ci expects our contact info
843 : in the sdests table all the time. fd_stake_ci_new initializes
844 : both ei->sdests with our contact info so this should always
845 : update (and not append). */
846 0 : fd_stake_ci_dest_update( ctx->stake_ci, (fd_pubkey_t *)ctx->gossip_upd_buf->origin_pubkey, 1U, 0U );
847 0 : } else {
848 0 : fd_stake_ci_dest_remove( ctx->stake_ci, (fd_pubkey_t *)ctx->gossip_upd_buf->origin_pubkey );
849 0 : }
850 0 : }
851 0 : return;
852 0 : }
853 :
854 0 : if( FD_UNLIKELY( (ctx->in_kind[ in_idx ]==IN_KIND_POH) & (ctx->send_fec_set_cnt==0UL) ) ) {
855 : /* Entry from PoH that didn't trigger a new FEC set to be made */
856 0 : return;
857 0 : }
858 :
859 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPAIR ) ) {
860 0 : FD_MCNT_INC( SHRED, FORCE_COMPLETE_REQUEST, 1UL );
861 0 : fd_ed25519_sig_t const * shred_sig = (fd_ed25519_sig_t const *)fd_type_pun( ctx->shred_buffer );
862 0 : if( FD_UNLIKELY( fd_fec_resolver_done_contains( ctx->resolver, shred_sig ) ) ) {
863 : /* This is a FEC completion message from the repair tile. We need
864 : to make sure that we don't force complete something that's just
865 : been completed. */
866 0 : FD_MCNT_INC( SHRED, FORCE_COMPLETE_FAILURE, 1UL );
867 0 : return;
868 0 : }
869 :
870 0 : uint last_idx = fd_disco_repair_shred_sig_last_shred_idx( sig );
871 0 : uchar buf_last_shred[FD_SHRED_MIN_SZ];
872 0 : int rv = fd_fec_resolver_shred_query( ctx->resolver, shred_sig, last_idx, buf_last_shred );
873 0 : if( FD_UNLIKELY( rv != FD_FEC_RESOLVER_SHRED_OKAY ) ) {
874 :
875 : /* We will hit this case if FEC is no longer in curr_map, or if
876 : the shred signature is invalid, which is okay.
877 :
878 : There's something of a race condition here. It's possible (but
879 : very unlikely) that between when the repair tile observed the
880 : FEC set needed to be force completed and now, the FEC set was
881 : completed, and then so many additional FEC sets were completed
882 : that it fell off the end of the done list. In that case
883 : fd_fec_resolver_done_contains would have returned false, but
884 : fd_fec_resolver_shred_query will not return OKAY, which means
885 : we'll end up in this block of code. If the FEC set was
886 : completed, then there's nothing we need to do. If it was
887 : spilled, then we'll need to re-repair all the shreds in the FEC
888 : set, but it's not fatal. */
889 :
890 0 : FD_MCNT_INC( SHRED, FORCE_COMPLETE_FAILURE, 1UL );
891 0 : return;
892 0 : }
893 0 : fd_shred_t * out_last_shred = (fd_shred_t *)fd_type_pun( buf_last_shred );
894 :
895 0 : fd_fec_set_t const * out_fec_set[1];
896 0 : rv = fd_fec_resolver_force_complete( ctx->resolver, out_last_shred, out_fec_set, &ctx->out_merkle_roots[0] );
897 0 : if( FD_UNLIKELY( rv != FD_FEC_RESOLVER_SHRED_COMPLETES ) ) {
898 0 : FD_BASE58_ENCODE_32_BYTES( *shred_sig, shred_sig_b58 );
899 0 : FD_LOG_WARNING(( "Shred tile %lu cannot force complete the slot %lu fec_set_idx %u last_idx %u %s", ctx->round_robin_id, out_last_shred->slot, out_last_shred->fec_set_idx, last_idx, shred_sig_b58 ));
900 0 : FD_MCNT_INC( SHRED, FORCE_COMPLETE_FAILURE, 1UL );
901 0 : return;
902 0 : }
903 0 : FD_MCNT_INC( SHRED, FORCE_COMPLETE_SUCCESS, 1UL );
904 0 : FD_TEST( ctx->fec_sets <= *out_fec_set );
905 0 : ctx->send_fec_set_idx[ 0UL ] = (ulong)(*out_fec_set - ctx->fec_sets);
906 0 : ctx->send_fec_set_cnt = 1UL;
907 0 : ctx->shredded_txn_cnt = 0UL;
908 0 : }
909 :
910 0 : ulong fanout = 200UL; /* Default Agave's DATA_PLANE_FANOUT = 200UL */
911 :
912 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
913 0 : uchar * shred_buffer = ctx->shred_buffer;
914 0 : ulong shred_buffer_sz = ctx->shred_buffer_sz;
915 :
916 0 : fd_shred_t const * shred = fd_shred_parse( shred_buffer, shred_buffer_sz );
917 :
918 0 : if( FD_UNLIKELY( !shred ) ) { ctx->metrics->shred_processing_result[ 1 ]++; return; }
919 :
920 0 : fd_epoch_leaders_t const * lsched = fd_stake_ci_get_lsched_for_slot( ctx->stake_ci, shred->slot );
921 0 : if( FD_UNLIKELY( !lsched ) ) { ctx->metrics->shred_processing_result[ 0 ]++; return; }
922 :
923 0 : fd_pubkey_t const * slot_leader = fd_epoch_leaders_get( lsched, shred->slot );
924 0 : if( FD_UNLIKELY( !slot_leader ) ) { ctx->metrics->shred_processing_result[ 0 ]++; return; } /* Count this as bad slot too */
925 :
926 0 : uint nonce = fd_disco_netmux_sig_proto( sig ) == DST_PROTO_SHRED ? UINT_MAX : FD_LOAD(uint, shred_buffer + fd_shred_sz( shred ) );
927 :
928 0 : fd_fec_set_t const * out_fec_set[1];
929 0 : fd_shred_t const * out_shred[1];
930 0 : fd_fec_resolver_spilled_t spilled_fec = { 0 };
931 :
932 0 : long add_shred_timing = -fd_tickcount();
933 0 : int rv = fd_fec_resolver_add_shred( ctx->resolver, shred, shred_buffer_sz, slot_leader->uc, out_fec_set, out_shred, &ctx->out_merkle_roots[0], &spilled_fec );
934 0 : add_shred_timing += fd_tickcount();
935 :
936 0 : fd_histf_sample( ctx->metrics->add_shred_timing, (ulong)add_shred_timing );
937 0 : ctx->metrics->shred_processing_result[ rv + FD_FEC_RESOLVER_ADD_SHRED_RETVAL_OFF+FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT ]++;
938 :
939 0 : if( FD_UNLIKELY( ctx->shred_out_idx!=ULONG_MAX && /* Only send to repair in full Firedancer */
940 0 : spilled_fec.slot!=0 && spilled_fec.max_dshred_idx!=FD_SHRED_BLK_MAX ) ) {
941 : /* We've spilled an in-progress FEC set in the fec_resolver. We
942 : need to let repair know to clear out it's cached info for that
943 : fec set and re-repair those shreds. */
944 0 : ulong sig_ = fd_disco_shred_out_shred_sig( 0, spilled_fec.slot, spilled_fec.fec_set_idx, 0, spilled_fec.max_dshred_idx );
945 0 : fd_stem_publish( stem, ctx->shred_out_idx, sig_, ctx->shred_out_chunk, 0, 0, ctx->tsorig, ctx->tsorig );
946 0 : }
947 :
948 0 : if( (rv==FD_FEC_RESOLVER_SHRED_OKAY) | (rv==FD_FEC_RESOLVER_SHRED_COMPLETES) ) {
949 0 : if( FD_LIKELY( fd_disco_netmux_sig_proto( sig ) != DST_PROTO_REPAIR ) ) {
950 : /* Relay this shred */
951 0 : ulong max_dest_cnt[1];
952 0 : do {
953 : /* If we've validated the shred and it COMPLETES but we can't
954 : compute the destination for whatever reason, don't forward
955 : the shred, but still send it to the blockstore. */
956 0 : fd_shred_dest_t * sdest = fd_stake_ci_get_sdest_for_slot( ctx->stake_ci, shred->slot );
957 0 : if( FD_UNLIKELY( !sdest ) ) break;
958 0 : int use_chacha8 = ( shred->slot >= ctx->features_activation->switch_to_chacha8_turbine );
959 0 : fd_shred_dest_idx_t * dests = fd_shred_dest_compute_children( sdest, &shred, 1UL, ctx->scratchpad_dests, 1UL, fanout, fanout, max_dest_cnt, use_chacha8 );
960 0 : if( FD_UNLIKELY( !dests ) ) break;
961 :
962 0 : for( ulong i=0UL; i<ctx->adtl_dests_retransmit_cnt; i++ ) send_shred( ctx, stem, *out_shred, ctx->adtl_dests_retransmit+i, ctx->tsorig );
963 0 : for( ulong j=0UL; j<*max_dest_cnt; j++ ) send_shred( ctx, stem, *out_shred, fd_shred_dest_idx_to_dest( sdest, dests[ j ] ), ctx->tsorig );
964 0 : } while( 0 );
965 0 : }
966 :
967 0 : if( FD_LIKELY( ctx->shred_out_idx!=ULONG_MAX ) ) { /* Only send to repair/replay in full Firedancer */
968 :
969 : /* Construct the sig from the shred. */
970 :
971 0 : int is_code = fd_shred_is_code( fd_shred_type( shred->variant ) );
972 0 : uint shred_idx_or_data_cnt = shred->idx;
973 0 : if( FD_LIKELY( is_code ) ) shred_idx_or_data_cnt = shred->code.data_cnt; /* optimize for code_cnt >= data_cnt */
974 0 : ulong _sig = fd_disco_shred_out_shred_sig( fd_disco_netmux_sig_proto(sig)==DST_PROTO_SHRED, shred->slot, shred->fec_set_idx, is_code, shred_idx_or_data_cnt );
975 :
976 : /* Copy the shred header into the frag and publish. */
977 :
978 0 : ulong sz = fd_shred_header_sz( shred->variant );
979 0 : fd_memcpy( fd_chunk_to_laddr( ctx->shred_out_mem, ctx->shred_out_chunk ), shred, sz );
980 0 : FD_STORE(uint, fd_chunk_to_laddr( ctx->shred_out_mem, ctx->shred_out_chunk ) + sz, nonce );
981 0 : sz += 4UL;
982 :
983 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
984 0 : fd_stem_publish( stem, ctx->shred_out_idx, _sig, ctx->shred_out_chunk, sz, 0UL, ctx->tsorig, tspub );
985 0 : ctx->shred_out_chunk = fd_dcache_compact_next( ctx->shred_out_chunk, sz, ctx->shred_out_chunk0, ctx->shred_out_wmark );
986 0 : }
987 0 : }
988 0 : if( FD_LIKELY( rv!=FD_FEC_RESOLVER_SHRED_COMPLETES ) ) return;
989 :
990 0 : FD_TEST( ctx->fec_sets <= *out_fec_set );
991 0 : ctx->send_fec_set_idx[ 0UL ] = (ulong)(*out_fec_set - ctx->fec_sets);
992 0 : ctx->send_fec_set_cnt = 1UL;
993 0 : ctx->shredded_txn_cnt = 0UL;
994 0 : }
995 :
996 0 : if( FD_UNLIKELY( ctx->send_fec_set_cnt==0UL ) ) return;
997 :
998 : /* Try to distribute shredded txn count across the fec sets.
999 : This is an approximation, but it is acceptable. */
1000 0 : ulong shredded_txn_cnt_per_fec_set = ctx->shredded_txn_cnt / ctx->send_fec_set_cnt;
1001 0 : ulong shredded_txn_cnt_remain = ctx->shredded_txn_cnt - shredded_txn_cnt_per_fec_set * ctx->send_fec_set_cnt;
1002 0 : ulong shredded_txn_cnt_last_fec_set = shredded_txn_cnt_per_fec_set + shredded_txn_cnt_remain;
1003 :
1004 : /* If this shred completes a FEC set or is part of a microblock from
1005 : pack (ie. we're leader), we now have a full FEC set: so we notify
1006 : repair and insert into the blockstore, as well as retransmit. */
1007 :
1008 0 : for( ulong fset_k=0; fset_k<ctx->send_fec_set_cnt; fset_k++ ) {
1009 :
1010 0 : fd_fec_set_t * set = ctx->fec_sets + ctx->send_fec_set_idx[ fset_k ];
1011 0 : fd_shred34_t * s34 = ctx->shred34 + 4UL*ctx->send_fec_set_idx[ fset_k ];
1012 :
1013 0 : s34[ 0 ].shred_cnt = fd_ulong_min( set->data_shred_cnt, 34UL );
1014 0 : s34[ 1 ].shred_cnt = set->data_shred_cnt - fd_ulong_min( set->data_shred_cnt, 34UL );
1015 0 : s34[ 2 ].shred_cnt = fd_ulong_min( set->parity_shred_cnt, 34UL );
1016 0 : s34[ 3 ].shred_cnt = set->parity_shred_cnt - fd_ulong_min( set->parity_shred_cnt, 34UL );
1017 :
1018 0 : ulong s34_cnt = 2UL + !!(s34[ 1 ].shred_cnt) + !!(s34[ 3 ].shred_cnt);
1019 0 : ulong txn_per_s34 = fd_ulong_if( fset_k<( ctx->send_fec_set_cnt - 1UL ), shredded_txn_cnt_per_fec_set, shredded_txn_cnt_last_fec_set ) / s34_cnt;
1020 :
1021 : /* Attribute the transactions evenly to the non-empty shred34s */
1022 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 );
1023 :
1024 : /* Add whatever is left to the last shred34 */
1025 0 : s34[ fd_ulong_if( s34[ 3 ].shred_cnt>0UL, 3, 2 ) ].est_txn_cnt += ctx->shredded_txn_cnt - txn_per_s34*s34_cnt;
1026 :
1027 : /* Set the sz field so that metrics are more accurate. */
1028 0 : ulong sz0 = sizeof(fd_shred34_t) - (34UL - s34[ 0 ].shred_cnt)*FD_SHRED_MAX_SZ;
1029 0 : ulong sz1 = sizeof(fd_shred34_t) - (34UL - s34[ 1 ].shred_cnt)*FD_SHRED_MAX_SZ;
1030 0 : ulong sz2 = sizeof(fd_shred34_t) - (34UL - s34[ 2 ].shred_cnt)*FD_SHRED_MAX_SZ;
1031 0 : ulong sz3 = sizeof(fd_shred34_t) - (34UL - s34[ 3 ].shred_cnt)*FD_SHRED_MAX_SZ;
1032 :
1033 0 : fd_shred_t const * last = (fd_shred_t const *)fd_type_pun_const( set->data_shreds[ set->data_shred_cnt - 1 ] );
1034 :
1035 : /* Compute merkle root and chained merkle root. */
1036 :
1037 0 : if( FD_LIKELY( ctx->store ) ) { /* firedancer-only */
1038 :
1039 : /* Insert shreds into the store. We do this regardless of whether
1040 : we are leader. */
1041 :
1042 : /* See top-level documentation in fd_store.h under CONCURRENCY to
1043 : understand why it is safe to use a Store read vs. write lock in
1044 : Shred tile. */
1045 :
1046 0 : long shacq_start, shacq_end, shrel_end;
1047 0 : fd_store_fec_t * fec = NULL;
1048 0 : FD_STORE_SHARED_LOCK( ctx->store, shacq_start, shacq_end, shrel_end ) {
1049 0 : fec = fd_store_insert( ctx->store, ctx->round_robin_id, (fd_hash_t *)fd_type_pun( &ctx->out_merkle_roots[fset_k] ) );
1050 0 : } FD_STORE_SHARED_LOCK_END;
1051 :
1052 0 : if( FD_UNLIKELY( !fec ) ) {
1053 : /* fec can be null for several reasons, but the most likely case
1054 : that Firedancer can run into during regular operation is when
1055 : it is our leader slot and someone is sending us back our own
1056 : FEC set shreds. We could end up trying to insert our own FEC
1057 : set twice. In development, this can also occur if you run
1058 : with a staked key and switch to another staked key without
1059 : changing the turbine receive port. */
1060 0 : return;
1061 0 : }
1062 :
1063 0 : for( ulong i=0UL; i<set->data_shred_cnt; i++ ) {
1064 0 : fd_shred_t * data_shred = (fd_shred_t *)fd_type_pun( set->data_shreds[i] );
1065 0 : ulong payload_sz = fd_shred_payload_sz( data_shred );
1066 0 : if( FD_UNLIKELY( fec->data_sz + payload_sz > FD_STORE_DATA_MAX ) ) {
1067 :
1068 : /* This code is only reachable if shred tile has completed the
1069 : FEC set, which implies it was able to validate it, yet
1070 : somehow the total payload sz of this FEC set exceeds the
1071 : maximum payload sz. This indicates either a serious bug or
1072 : shred tile is compromised so log_crit. */
1073 :
1074 0 : FD_LOG_CRIT(( "Shred tile %lu: completed FEC set %lu %u data_sz: %lu exceeds FD_STORE_DATA_MAX: %lu. Ignoring FEC set.", ctx->round_robin_id, data_shred->slot, data_shred->fec_set_idx, fec->data_sz + payload_sz, FD_STORE_DATA_MAX ));
1075 0 : }
1076 0 : fd_memcpy( fec->data + fec->data_sz, fd_shred_data_payload( data_shred ), payload_sz );
1077 0 : fec->data_sz += payload_sz;
1078 0 : if( FD_LIKELY( i<32UL ) ) fec->block_offs[ i ] = (uint)payload_sz + fd_uint_if( i==0UL, 0UL, fec->block_offs[ i-1UL ] );
1079 0 : }
1080 :
1081 : /* It's safe to memcpy the FEC payload outside of the shared-lock,
1082 : because the fec object ptr is guaranteed to be valid. It is
1083 : not possible for a store_publish to free/invalidate the fec
1084 : object during the data memcpy, because the free can only happen
1085 : after the fec is linked to its parent, which happens in the
1086 : repair tile, and crucially, only after we call stem publish in
1087 : this tile. Copying outside the shared lock scope also means
1088 : that we can lower the duration for which the shared lock is
1089 : held, and enables replay to acquire the exclusive lock and
1090 : avoid getting starved. */
1091 :
1092 0 : fd_histf_sample( ctx->metrics->store_insert_wait, (ulong)fd_long_max(shacq_end - shacq_start, 0) );
1093 0 : fd_histf_sample( ctx->metrics->store_insert_work, (ulong)fd_long_max(shrel_end - shacq_end, 0) );
1094 0 : }
1095 :
1096 0 : if( FD_LIKELY( ctx->shred_out_idx!=ULONG_MAX ) ) { /* firedancer-only */
1097 :
1098 : /* Additionally, publish a frag to notify repair and replay that
1099 : the FEC set is complete. Note the ordering wrt store shred
1100 : insertion above is intentional: shreds are inserted into the
1101 : store before notifying repair and replay. This is because the
1102 : replay tile assumes the shreds are already in the store when
1103 : replay gets a notification from the shred tile that the FEC is
1104 : complete. We we don't know whether shred will finish inserting
1105 : into store first or repair will finish validating the FEC set
1106 : first. The header and merkle root of the last shred in the FEC
1107 : set are sent as part of this frag.
1108 :
1109 : This message, the shred msg, and the FEC evict msg constitute
1110 : the max 3 possible messages to repair/replay per after_frag.
1111 : In reality, it is only possible to publish all 3 in the case
1112 : where we receive a coding shred first for a FEC set where
1113 : (N=1,K=18), which allows for the FEC set to be instantly
1114 : completed by the singular coding shred, and that also happens
1115 : to evict a FEC set from the curr_map. When fix-32 arrives, the
1116 : link burst value can be lowered to 2. */
1117 :
1118 0 : int is_leader_fec = ctx->in_kind[ in_idx ]==IN_KIND_POH;
1119 :
1120 0 : ulong sig = fd_disco_shred_out_fec_sig( last->slot, last->fec_set_idx, (uint)set->data_shred_cnt, last->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE, last->data.flags & FD_SHRED_DATA_FLAG_DATA_COMPLETE );
1121 0 : uchar * chunk = fd_chunk_to_laddr( ctx->shred_out_mem, ctx->shred_out_chunk );
1122 0 : memcpy( chunk, last, FD_SHRED_DATA_HEADER_SZ );
1123 0 : memcpy( chunk+FD_SHRED_DATA_HEADER_SZ, ctx->out_merkle_roots[fset_k].hash, FD_SHRED_MERKLE_ROOT_SZ );
1124 0 : memcpy( chunk+FD_SHRED_DATA_HEADER_SZ + FD_SHRED_MERKLE_ROOT_SZ, (uchar *)last + fd_shred_chain_off( last->variant ), FD_SHRED_MERKLE_ROOT_SZ );
1125 0 : memcpy( chunk+FD_SHRED_DATA_HEADER_SZ + (FD_SHRED_MERKLE_ROOT_SZ*2UL), &is_leader_fec, sizeof(int));
1126 :
1127 0 : ulong sz = FD_SHRED_DATA_HEADER_SZ + FD_SHRED_MERKLE_ROOT_SZ * 2 + sizeof(int);
1128 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
1129 0 : fd_stem_publish( stem, ctx->shred_out_idx, sig, ctx->shred_out_chunk, sz, 0UL, ctx->tsorig, tspub );
1130 0 : ctx->shred_out_chunk = fd_dcache_compact_next( ctx->shred_out_chunk, sz, ctx->shred_out_chunk0, ctx->shred_out_wmark );
1131 :
1132 0 : } else if( FD_UNLIKELY( ctx->store_out_idx != ULONG_MAX ) ) { /* frankendancer-only */
1133 :
1134 : /* Send to the blockstore, skipping any empty shred34_t s. */
1135 :
1136 0 : ulong new_sig = ctx->in_kind[ in_idx ]!=IN_KIND_NET; /* sig==0 means the store tile will do extra checks */
1137 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
1138 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, s34+0UL ), sz0, 0UL, ctx->tsorig, tspub );
1139 0 : if( FD_UNLIKELY( s34[ 1 ].shred_cnt ) )
1140 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, s34+1UL ), sz1, 0UL, ctx->tsorig, tspub );
1141 0 : if( FD_UNLIKELY( s34[ 2 ].shred_cnt ) )
1142 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, s34+2UL), sz2, 0UL, ctx->tsorig, tspub );
1143 0 : if( FD_UNLIKELY( s34[ 3 ].shred_cnt ) )
1144 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, s34+3UL ), sz3, 0UL, ctx->tsorig, tspub );
1145 0 : }
1146 :
1147 : /* Compute all the destinations for all the new shreds */
1148 :
1149 0 : fd_shred_t const * new_shreds[ FD_REEDSOL_DATA_SHREDS_MAX+FD_REEDSOL_PARITY_SHREDS_MAX ];
1150 0 : ulong k=0UL;
1151 0 : for( ulong i=0UL; i<set->data_shred_cnt; i++ )
1152 0 : if( !d_rcvd_test( set->data_shred_rcvd, i ) ) new_shreds[ k++ ] = (fd_shred_t const *)set->data_shreds [ i ];
1153 0 : for( ulong i=0UL; i<set->parity_shred_cnt; i++ )
1154 0 : if( !p_rcvd_test( set->parity_shred_rcvd, i ) ) new_shreds[ k++ ] = (fd_shred_t const *)set->parity_shreds[ i ];
1155 :
1156 0 : if( FD_UNLIKELY( !k ) ) return;
1157 0 : fd_shred_dest_t * sdest = fd_stake_ci_get_sdest_for_slot( ctx->stake_ci, new_shreds[ 0 ]->slot );
1158 0 : if( FD_UNLIKELY( !sdest ) ) return;
1159 0 : int use_chacha8 = ( new_shreds[ 0 ]->slot >= ctx->features_activation->switch_to_chacha8_turbine );
1160 :
1161 0 : ulong out_stride;
1162 0 : ulong max_dest_cnt[1];
1163 0 : fd_shred_dest_idx_t * dests;
1164 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
1165 0 : for( ulong i=0UL; i<k; i++ ) {
1166 0 : for( ulong j=0UL; j<ctx->adtl_dests_retransmit_cnt; j++ ) send_shred( ctx, stem, new_shreds[ i ], ctx->adtl_dests_retransmit+j, ctx->tsorig );
1167 0 : }
1168 0 : out_stride = k;
1169 : /* In the case of feature activation, the fanout used below is
1170 : the same as the one calculated/modified previously at the
1171 : beginning of after_frag() for IN_KIND_NET in this slot. */
1172 0 : dests = fd_shred_dest_compute_children( sdest, new_shreds, k, ctx->scratchpad_dests, k, fanout, fanout, max_dest_cnt, use_chacha8 );
1173 0 : } else {
1174 0 : for( ulong i=0UL; i<k; i++ ) {
1175 0 : for( ulong j=0UL; j<ctx->adtl_dests_leader_cnt; j++ ) send_shred( ctx, stem, new_shreds[ i ], ctx->adtl_dests_leader+j, ctx->tsorig );
1176 0 : }
1177 0 : out_stride = 1UL;
1178 0 : *max_dest_cnt = 1UL;
1179 0 : dests = fd_shred_dest_compute_first ( sdest, new_shreds, k, ctx->scratchpad_dests, use_chacha8 );
1180 0 : }
1181 0 : if( FD_UNLIKELY( !dests ) ) return;
1182 :
1183 : /* Send only the ones we didn't receive. */
1184 0 : for( ulong i=0UL; i<k; i++ ) {
1185 0 : for( ulong j=0UL; j<*max_dest_cnt; j++ ) send_shred( ctx, stem, new_shreds[ i ], fd_shred_dest_idx_to_dest( sdest, dests[ j*out_stride+i ]), ctx->tsorig );
1186 0 : }
1187 0 : }
1188 0 : }
1189 :
1190 : static void
1191 : privileged_init( fd_topo_t * topo,
1192 0 : fd_topo_tile_t * tile ) {
1193 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1194 0 : FD_TEST( scratch!=NULL );
1195 :
1196 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1197 0 : fd_shred_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_shred_ctx_t ), sizeof( fd_shred_ctx_t ) );
1198 :
1199 0 : if( FD_UNLIKELY( !strcmp( tile->shred.identity_key_path, "" ) ) )
1200 0 : FD_LOG_ERR(( "identity_key_path not set" ));
1201 :
1202 0 : ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->shred.identity_key_path, /* pubkey only: */ 1 ) );
1203 0 : }
1204 :
1205 : static void
1206 : fd_shred_signer( void * signer_ctx,
1207 : uchar signature[ static 64 ],
1208 0 : uchar const merkle_root[ static 32 ] ) {
1209 0 : fd_keyguard_client_sign( signer_ctx, signature, merkle_root, 32UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
1210 0 : }
1211 :
1212 : static void
1213 : unprivileged_init( fd_topo_t * topo,
1214 0 : fd_topo_tile_t * tile ) {
1215 :
1216 0 : FD_TEST( 0==strcmp( topo->links[tile->out_link_id[ NET_OUT_IDX ]].name, "shred_net" ) );
1217 0 : FD_TEST( 0==strcmp( topo->links[tile->out_link_id[ SIGN_OUT_IDX ]].name, "shred_sign" ) );
1218 :
1219 0 : if( FD_UNLIKELY( !tile->out_cnt ) )
1220 0 : FD_LOG_ERR(( "shred tile has no primary output link" ));
1221 :
1222 0 : ulong shred_store_mcache_depth = tile->shred.depth;
1223 0 : if( topo->links[ tile->out_link_id[ 0 ] ].depth != shred_store_mcache_depth )
1224 0 : FD_LOG_ERR(( "shred tile out depths are not equal %lu %lu",
1225 0 : topo->links[ tile->out_link_id[ 0 ] ].depth, shred_store_mcache_depth ));
1226 :
1227 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1228 0 : FD_TEST( scratch!=NULL );
1229 :
1230 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1231 0 : fd_shred_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_shred_ctx_t ), sizeof( fd_shred_ctx_t ) );
1232 :
1233 0 : ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
1234 0 : ctx->round_robin_id = tile->kind_id;
1235 0 : ctx->batch_cnt = 0UL;
1236 0 : ctx->slot = ULONG_MAX;
1237 :
1238 : /* If the default partial_depth is ever changed, correspondingly
1239 : change the size of the fd_fec_intra_pool in fd_fec_repair. */
1240 0 : ulong fec_resolver_footprint = fd_fec_resolver_footprint( tile->shred.fec_resolver_depth, 1UL, shred_store_mcache_depth,
1241 0 : 128UL * tile->shred.fec_resolver_depth );
1242 0 : ulong fec_set_cnt = shred_store_mcache_depth + tile->shred.fec_resolver_depth + 4UL;
1243 0 : ulong fec_sets_required_sz = fec_set_cnt*DCACHE_ENTRIES_PER_FEC_SET*sizeof(fd_shred34_t);
1244 :
1245 0 : void * fec_sets_shmem = NULL;
1246 0 : ctx->shred_out_idx = fd_topo_find_tile_out_link( topo, tile, "shred_out", ctx->round_robin_id );
1247 0 : ctx->store_out_idx = fd_topo_find_tile_out_link( topo, tile, "shred_store", ctx->round_robin_id );
1248 0 : if( FD_LIKELY( ctx->shred_out_idx!=ULONG_MAX ) ) { /* firedancer-only */
1249 0 : fd_topo_link_t * shred_out = &topo->links[ tile->out_link_id[ ctx->shred_out_idx ] ];
1250 0 : ctx->shred_out_mem = topo->workspaces[ topo->objs[ shred_out->dcache_obj_id ].wksp_id ].wksp;
1251 0 : ctx->shred_out_chunk0 = fd_dcache_compact_chunk0( ctx->shred_out_mem, shred_out->dcache );
1252 0 : ctx->shred_out_wmark = fd_dcache_compact_wmark ( ctx->shred_out_mem, shred_out->dcache, shred_out->mtu );
1253 0 : ctx->shred_out_chunk = ctx->shred_out_chunk0;
1254 0 : FD_TEST( fd_dcache_compact_is_safe( ctx->shred_out_mem, shred_out->dcache, shred_out->mtu, shred_out->depth ) );
1255 0 : ulong fec_sets_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "fec_sets" );
1256 0 : if( FD_UNLIKELY( fec_sets_obj_id == ULONG_MAX ) ) FD_LOG_ERR(( "invalid firedancer topo" ));
1257 0 : fd_topo_obj_t const * obj = &topo->objs[ fec_sets_obj_id ];
1258 0 : if( FD_UNLIKELY( obj->footprint<(fec_sets_required_sz*ctx->round_robin_cnt) ) ) {
1259 0 : FD_LOG_ERR(( "fec_sets wksp obj too small. It is %lu bytes but must be at least %lu bytes. ",
1260 0 : obj->footprint,
1261 0 : fec_sets_required_sz ));
1262 0 : }
1263 0 : fec_sets_shmem = (uchar *)fd_topo_obj_laddr( topo, fec_sets_obj_id ) + (ctx->round_robin_id * fec_sets_required_sz);
1264 0 : } else if ( FD_LIKELY( ctx->store_out_idx!=ULONG_MAX ) ) { /* frankendancer-only */
1265 0 : FD_TEST( 0==strcmp( topo->links[tile->out_link_id[ ctx->store_out_idx ]].name, "shred_store" ) );
1266 0 : fec_sets_shmem = topo->links[ tile->out_link_id[ ctx->store_out_idx ] ].dcache;
1267 0 : if( FD_UNLIKELY( fd_dcache_data_sz( fec_sets_shmem )<fec_sets_required_sz ) ) {
1268 0 : FD_LOG_ERR(( "shred_store dcache too small. It is %lu bytes but must be at least %lu bytes. ",
1269 0 : fd_dcache_data_sz( fec_sets_shmem ),
1270 0 : fec_sets_required_sz ));
1271 0 : }
1272 0 : }
1273 :
1274 0 : if( FD_UNLIKELY( !tile->shred.fec_resolver_depth ) ) FD_LOG_ERR(( "fec_resolver_depth not set" ));
1275 0 : if( FD_UNLIKELY( !tile->shred.shred_listen_port ) ) FD_LOG_ERR(( "shred_listen_port not set" ));
1276 :
1277 0 : void * _stake_ci = FD_SCRATCH_ALLOC_APPEND( l, fd_stake_ci_align(), fd_stake_ci_footprint() );
1278 0 : void * _resolver = FD_SCRATCH_ALLOC_APPEND( l, fd_fec_resolver_align(), fec_resolver_footprint );
1279 0 : void * _shredder = FD_SCRATCH_ALLOC_APPEND( l, fd_shredder_align(), fd_shredder_footprint() );
1280 0 : void * _fec_sets = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_fec_set_t), sizeof(fd_fec_set_t)*fec_set_cnt );
1281 :
1282 0 : fd_fec_set_t * fec_sets = (fd_fec_set_t *)_fec_sets;
1283 0 : fd_shred34_t * shred34 = (fd_shred34_t *)fec_sets_shmem;
1284 :
1285 0 : for( ulong i=0UL; i<fec_set_cnt; i++ ) {
1286 0 : fd_shred34_t * p34_base = shred34 + i*DCACHE_ENTRIES_PER_FEC_SET;
1287 0 : for( ulong k=0UL; k<DCACHE_ENTRIES_PER_FEC_SET; k++ ) {
1288 0 : fd_shred34_t * p34 = p34_base + k;
1289 :
1290 0 : p34->stride = (ulong)p34->pkts[1].buffer - (ulong)p34->pkts[0].buffer;
1291 0 : p34->offset = (ulong)p34->pkts[0].buffer - (ulong)p34;
1292 0 : p34->shred_sz = fd_ulong_if( k<2UL, 1203UL, 1228UL );
1293 0 : }
1294 :
1295 0 : uchar ** data_shred = fec_sets[ i ].data_shreds;
1296 0 : uchar ** parity_shred = fec_sets[ i ].parity_shreds;
1297 0 : for( ulong j=0UL; j<FD_REEDSOL_DATA_SHREDS_MAX; j++ ) data_shred [ j ] = p34_base[ j/34UL ].pkts[ j%34UL ].buffer;
1298 0 : for( ulong j=0UL; j<FD_REEDSOL_PARITY_SHREDS_MAX; j++ ) parity_shred[ j ] = p34_base[ 2UL + j/34UL ].pkts[ j%34UL ].buffer;
1299 0 : }
1300 :
1301 0 : #define NONNULL( x ) (__extension__({ \
1302 0 : __typeof__((x)) __x = (x); \
1303 0 : if( FD_UNLIKELY( !__x ) ) FD_LOG_ERR(( #x " was unexpectedly NULL" )); \
1304 0 : __x; }))
1305 :
1306 0 : int has_ipecho_in = fd_topo_find_tile_in_link( topo, tile, "ipecho_out", 0UL )!=ULONG_MAX;
1307 0 : ushort expected_shred_version = tile->shred.expected_shred_version;
1308 0 : if( FD_UNLIKELY( !has_ipecho_in && !expected_shred_version ) ) {
1309 0 : ulong busy_obj_id = fd_pod_query_ulong( topo->props, "poh_shred", ULONG_MAX );
1310 0 : FD_TEST( busy_obj_id!=ULONG_MAX );
1311 0 : ulong * gossip_shred_version = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
1312 0 : FD_LOG_INFO(( "Waiting for shred version to be determined via gossip." ));
1313 0 : ulong _expected_shred_version = ULONG_MAX;
1314 0 : do {
1315 0 : _expected_shred_version = FD_VOLATILE_CONST( *gossip_shred_version );
1316 0 : } while( _expected_shred_version==ULONG_MAX );
1317 :
1318 0 : if( FD_UNLIKELY( _expected_shred_version>USHORT_MAX ) ) FD_LOG_ERR(( "invalid shred version %lu", _expected_shred_version ));
1319 0 : FD_LOG_INFO(( "Using shred version %hu", (ushort)_expected_shred_version ));
1320 0 : expected_shred_version = (ushort)_expected_shred_version;
1321 0 : }
1322 :
1323 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
1324 0 : FD_TEST( ctx->keyswitch );
1325 :
1326 : /* populate ctx */
1327 0 : ulong sign_in_idx = fd_topo_find_tile_in_link( topo, tile, "sign_shred", tile->kind_id );
1328 0 : FD_TEST( sign_in_idx!=ULONG_MAX );
1329 0 : fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
1330 0 : fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ SIGN_OUT_IDX ] ];
1331 0 : NONNULL( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
1332 0 : sign_out->mcache,
1333 0 : sign_out->dcache,
1334 0 : sign_in->mcache,
1335 0 : sign_in->dcache,
1336 0 : sign_out->mtu ) ) );
1337 :
1338 0 : ulong shred_limit = fd_ulong_if( tile->shred.larger_shred_limits_per_block, 32UL*32UL*1024UL, 32UL*1024UL );
1339 0 : fd_fec_set_t * resolver_sets = fec_sets + (shred_store_mcache_depth+1UL)/2UL + 1UL;
1340 0 : ctx->shredder = NONNULL( fd_shredder_join ( fd_shredder_new ( _shredder, fd_shred_signer, ctx->keyguard_client ) ) );
1341 0 : ctx->resolver = NONNULL( fd_fec_resolver_join ( fd_fec_resolver_new ( _resolver,
1342 0 : fd_shred_signer, ctx->keyguard_client,
1343 0 : tile->shred.fec_resolver_depth, 1UL,
1344 0 : (shred_store_mcache_depth+3UL)/2UL,
1345 0 : 128UL * tile->shred.fec_resolver_depth, resolver_sets,
1346 0 : shred_limit ) ) );
1347 :
1348 0 : if( FD_LIKELY( !!expected_shred_version ) ) {
1349 0 : fd_shredder_set_shred_version ( ctx->shredder, expected_shred_version );
1350 0 : fd_fec_resolver_set_shred_version( ctx->resolver, expected_shred_version );
1351 0 : }
1352 :
1353 0 : ctx->shred34 = shred34;
1354 0 : ctx->fec_sets = fec_sets;
1355 :
1356 0 : ctx->stake_ci = fd_stake_ci_join( fd_stake_ci_new( _stake_ci, ctx->identity_key ) );
1357 :
1358 0 : ctx->net_id = (ushort)0;
1359 :
1360 0 : fd_ip4_udp_hdr_init( ctx->data_shred_net_hdr, FD_SHRED_MIN_SZ, 0, tile->shred.shred_listen_port );
1361 0 : fd_ip4_udp_hdr_init( ctx->parity_shred_net_hdr, FD_SHRED_MAX_SZ, 0, tile->shred.shred_listen_port );
1362 :
1363 0 : ctx->adtl_dests_retransmit_cnt = tile->shred.adtl_dests_retransmit_cnt;
1364 0 : for( ulong i=0UL; i<ctx->adtl_dests_retransmit_cnt; i++) {
1365 0 : ctx->adtl_dests_retransmit[ i ].ip4 = tile->shred.adtl_dests_retransmit[ i ].ip;
1366 0 : ctx->adtl_dests_retransmit[ i ].port = tile->shred.adtl_dests_retransmit[ i ].port;
1367 0 : }
1368 0 : ctx->adtl_dests_leader_cnt = tile->shred.adtl_dests_leader_cnt;
1369 0 : for( ulong i=0UL; i<ctx->adtl_dests_leader_cnt; i++) {
1370 0 : ctx->adtl_dests_leader[i].ip4 = tile->shred.adtl_dests_leader[i].ip;
1371 0 : ctx->adtl_dests_leader[i].port = tile->shred.adtl_dests_leader[i].port;
1372 0 : }
1373 :
1374 0 : uchar has_contact_info_in = 0;
1375 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
1376 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
1377 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
1378 :
1379 0 : if( FD_LIKELY( !strcmp( link->name, "net_shred" ) ) ) {
1380 0 : ctx->in_kind[ i ] = IN_KIND_NET;
1381 0 : fd_net_rx_bounds_init( &ctx->in[ i ].net_rx, link->dcache );
1382 0 : continue; /* only net_rx needs to be set in this case. */
1383 0 : }
1384 0 : else if( FD_LIKELY( !strcmp( link->name, "poh_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
1385 0 : else if( FD_LIKELY( !strcmp( link->name, "stake_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_STAKE;
1386 0 : else if( FD_LIKELY( !strcmp( link->name, "replay_stake" ) ) ) ctx->in_kind[ i ] = IN_KIND_STAKE;
1387 0 : else if( FD_LIKELY( !strcmp( link->name, "sign_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
1388 0 : else if( FD_LIKELY( !strcmp( link->name, "repair_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR;
1389 0 : else if( FD_LIKELY( !strcmp( link->name, "ipecho_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_IPECHO;
1390 0 : else if( FD_LIKELY( !strcmp( link->name, "crds_shred" ) ) ) { ctx->in_kind[ i ] = IN_KIND_CONTACT;
1391 0 : if( FD_UNLIKELY( has_contact_info_in ) ) FD_LOG_ERR(( "shred tile has multiple contact info in link types, can only be either gossip_out or crds_shred" ));
1392 0 : has_contact_info_in = 1;
1393 0 : }
1394 0 : else if( FD_LIKELY( !strcmp( link->name, "gossip_out" ) ) ) { ctx->in_kind[ i ] = IN_KIND_GOSSIP;
1395 0 : if( FD_UNLIKELY( has_contact_info_in ) ) FD_LOG_ERR(( "shred tile has multiple contact info in link types, can only be either gossip_out or crds_shred" ));
1396 0 : has_contact_info_in = 1;
1397 0 : }
1398 :
1399 0 : else FD_LOG_ERR(( "shred tile has unexpected input link %lu %s", i, link->name ));
1400 :
1401 0 : if( FD_LIKELY( !!link->mtu ) ) {
1402 0 : ctx->in[ i ].mem = link_wksp->wksp;
1403 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
1404 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
1405 0 : }
1406 0 : }
1407 :
1408 0 : fd_topo_link_t * net_out = &topo->links[ tile->out_link_id[ NET_OUT_IDX ] ];
1409 :
1410 0 : ctx->net_out_chunk0 = fd_dcache_compact_chunk0( fd_wksp_containing( net_out->dcache ), net_out->dcache );
1411 0 : ctx->net_out_mem = topo->workspaces[ topo->objs[ net_out->dcache_obj_id ].wksp_id ].wksp;
1412 0 : ctx->net_out_wmark = fd_dcache_compact_wmark ( ctx->net_out_mem, net_out->dcache, net_out->mtu );
1413 0 : ctx->net_out_chunk = ctx->net_out_chunk0;
1414 :
1415 0 : ctx->store = NULL;
1416 0 : ulong store_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "store" );
1417 0 : if( FD_LIKELY( store_obj_id!=ULONG_MAX ) ) { /* firedancer-only */
1418 0 : ctx->store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
1419 0 : FD_TEST( ctx->store->magic == FD_STORE_MAGIC );
1420 0 : }
1421 :
1422 0 : if( FD_LIKELY( ctx->shred_out_idx!=ULONG_MAX ) ) { /* firedancer-only */
1423 0 : fd_topo_link_t * shred_out = &topo->links[ tile->out_link_id[ ctx->shred_out_idx ] ];
1424 0 : ctx->shred_out_mem = topo->workspaces[ topo->objs[ shred_out->dcache_obj_id ].wksp_id ].wksp;
1425 0 : ctx->shred_out_chunk0 = fd_dcache_compact_chunk0( ctx->shred_out_mem, shred_out->dcache );
1426 0 : ctx->shred_out_wmark = fd_dcache_compact_wmark ( ctx->shred_out_mem, shred_out->dcache, shred_out->mtu );
1427 0 : ctx->shred_out_chunk = ctx->shred_out_chunk0;
1428 0 : FD_TEST( fd_dcache_compact_is_safe( ctx->shred_out_mem, shred_out->dcache, shred_out->mtu, shred_out->depth ) );
1429 0 : }
1430 :
1431 0 : if( FD_LIKELY( ctx->store_out_idx!=ULONG_MAX ) ) { /* frankendancer-only */
1432 0 : fd_topo_link_t * store_out = &topo->links[ tile->out_link_id[ ctx->store_out_idx ] ];
1433 0 : ctx->store_out_mem = topo->workspaces[ topo->objs[ store_out->dcache_obj_id ].wksp_id ].wksp;
1434 0 : ctx->store_out_chunk0 = fd_dcache_compact_chunk0( ctx->store_out_mem, store_out->dcache );
1435 0 : ctx->store_out_wmark = fd_dcache_compact_wmark ( ctx->store_out_mem, store_out->dcache, store_out->mtu );
1436 0 : ctx->store_out_chunk = ctx->store_out_chunk0;
1437 0 : FD_TEST( fd_dcache_compact_is_safe( ctx->store_out_mem, store_out->dcache, store_out->mtu, store_out->depth ) );
1438 0 : }
1439 :
1440 0 : ctx->poh_in_expect_seq = 0UL;
1441 :
1442 0 : ctx->shredder_fec_set_idx = 0UL;
1443 0 : ctx->shredder_max_fec_set_idx = (shred_store_mcache_depth+1UL)/2UL + 1UL;
1444 :
1445 0 : ctx->chained_merkle_root = NULL;
1446 0 : memset( ctx->out_merkle_roots, 0, sizeof(ctx->out_merkle_roots) );
1447 :
1448 0 : for( ulong i=0UL; i<FD_SHRED_BATCH_FEC_SETS_MAX; i++ ) { ctx->send_fec_set_idx[ i ] = ULONG_MAX; }
1449 0 : ctx->send_fec_set_cnt = 0UL;
1450 :
1451 0 : ctx->shred_buffer_sz = 0UL;
1452 0 : memset( ctx->shred_buffer, 0xFF, FD_NET_MTU );
1453 :
1454 0 : fd_histf_join( fd_histf_new( ctx->metrics->contact_info_cnt, FD_MHIST_MIN( SHRED, CLUSTER_CONTACT_INFO_CNT ),
1455 0 : FD_MHIST_MAX( SHRED, CLUSTER_CONTACT_INFO_CNT ) ) );
1456 0 : fd_histf_join( fd_histf_new( ctx->metrics->batch_sz, FD_MHIST_MIN( SHRED, BATCH_SZ ),
1457 0 : FD_MHIST_MAX( SHRED, BATCH_SZ ) ) );
1458 0 : fd_histf_join( fd_histf_new( ctx->metrics->batch_microblock_cnt, FD_MHIST_MIN( SHRED, BATCH_MICROBLOCK_CNT ),
1459 0 : FD_MHIST_MAX( SHRED, BATCH_MICROBLOCK_CNT ) ) );
1460 0 : fd_histf_join( fd_histf_new( ctx->metrics->shredding_timing, FD_MHIST_SECONDS_MIN( SHRED, SHREDDING_DURATION_SECONDS ),
1461 0 : FD_MHIST_SECONDS_MAX( SHRED, SHREDDING_DURATION_SECONDS ) ) );
1462 0 : fd_histf_join( fd_histf_new( ctx->metrics->add_shred_timing, FD_MHIST_SECONDS_MIN( SHRED, ADD_SHRED_DURATION_SECONDS ),
1463 0 : FD_MHIST_SECONDS_MAX( SHRED, ADD_SHRED_DURATION_SECONDS ) ) );
1464 0 : fd_histf_join( fd_histf_new( ctx->metrics->store_insert_wait, FD_MHIST_SECONDS_MIN( SHRED, STORE_INSERT_WAIT ),
1465 0 : FD_MHIST_SECONDS_MAX( SHRED, STORE_INSERT_WAIT ) ) );
1466 0 : fd_histf_join( fd_histf_new( ctx->metrics->store_insert_work, FD_MHIST_SECONDS_MIN( SHRED, STORE_INSERT_WORK ),
1467 0 : FD_MHIST_SECONDS_MAX( SHRED, STORE_INSERT_WORK ) ) );
1468 0 : memset( ctx->metrics->shred_processing_result, '\0', sizeof(ctx->metrics->shred_processing_result) );
1469 0 : ctx->metrics->invalid_block_id_cnt = 0UL;
1470 0 : ctx->metrics->shred_rejected_unchained_cnt = 0UL;
1471 0 : ctx->metrics->repair_rcv_cnt = 0UL;
1472 0 : ctx->metrics->repair_rcv_bytes = 0UL;
1473 0 : ctx->metrics->turbine_rcv_cnt = 0UL;
1474 0 : ctx->metrics->turbine_rcv_bytes = 0UL;
1475 :
1476 0 : ctx->pending_batch.microblock_cnt = 0UL;
1477 0 : ctx->pending_batch.txn_cnt = 0UL;
1478 0 : ctx->pending_batch.pos = 0UL;
1479 0 : ctx->pending_batch.slot = 0UL;
1480 0 : memset( ctx->pending_batch.payload, 0, sizeof(ctx->pending_batch.payload) );
1481 :
1482 0 : for( ulong i=0UL; i<FD_SHRED_FEATURES_ACTIVATION_SLOT_CNT; i++ )
1483 0 : ctx->features_activation->slots[i] = FD_SHRED_FEATURES_ACTIVATION_SLOT_DISABLED;
1484 :
1485 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
1486 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
1487 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
1488 :
1489 0 : memset( ctx->block_ids, 0, sizeof(ctx->block_ids) );
1490 0 : }
1491 :
1492 : static ulong
1493 : populate_allowed_seccomp( fd_topo_t const * topo,
1494 : fd_topo_tile_t const * tile,
1495 : ulong out_cnt,
1496 0 : struct sock_filter * out ) {
1497 0 : (void)topo;
1498 0 : (void)tile;
1499 :
1500 0 : populate_sock_filter_policy_fd_shred_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
1501 0 : return sock_filter_policy_fd_shred_tile_instr_cnt;
1502 0 : }
1503 :
1504 : static ulong
1505 : populate_allowed_fds( fd_topo_t const * topo,
1506 : fd_topo_tile_t const * tile,
1507 : ulong out_fds_cnt,
1508 0 : int * out_fds ) {
1509 0 : (void)topo;
1510 0 : (void)tile;
1511 :
1512 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
1513 :
1514 0 : ulong out_cnt = 0UL;
1515 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
1516 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
1517 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
1518 0 : return out_cnt;
1519 0 : }
1520 :
1521 : /* Excluding net_out (where the link is unreliable), STEM_BURST needs
1522 : to guarantee enough credits for the worst case. There are 4 cases
1523 : to consider: (IN_KIND_NET/IN_KIND_POH) x (Frankendancer/Firedancer)
1524 : In the IN_KIND_NET case: (Frankendancer) that can be 4 frags to
1525 : store; (Firedancer) that is one frag for the shred to repair, and
1526 : then another frag to repair for the FEC set.
1527 : In the IN_KIND_POH case: (Frankendancer) there might be
1528 : FD_SHRED_BATCH_FEC_SETS_MAX FEC sets, but we know they are 32:32,
1529 : which means only two shred34s per FEC set; (Firedancer) that is
1530 : FD_SHRED_BATCH_FEC_SETS_MAX frags to repair (one per FEC set).
1531 : Therefore, the worst case is IN_KIND_POH for Frankendancer. */
1532 0 : #define STEM_BURST (FD_SHRED_BATCH_FEC_SETS_MAX*2UL)
1533 :
1534 : /* See explanation in fd_pack */
1535 0 : #define STEM_LAZY (128L*3000L)
1536 :
1537 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_shred_ctx_t
1538 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_shred_ctx_t)
1539 :
1540 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
1541 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
1542 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
1543 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
1544 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
1545 :
1546 : #include "../stem/fd_stem.c"
1547 :
1548 : fd_topo_run_tile_t fd_tile_shred = {
1549 : .name = "shred",
1550 : .populate_allowed_seccomp = populate_allowed_seccomp,
1551 : .populate_allowed_fds = populate_allowed_fds,
1552 : .scratch_align = scratch_align,
1553 : .scratch_footprint = scratch_footprint,
1554 : .privileged_init = privileged_init,
1555 : .unprivileged_init = unprivileged_init,
1556 : .run = stem_run,
1557 : };
|