Line data Source code
1 : #define _GNU_SOURCE
2 : #include "../tiles.h"
3 :
4 : #if FD_HAS_X86
5 : #include <x86intrin.h>
6 : #endif
7 :
8 : #include <errno.h>
9 : #include <fcntl.h>
10 : #include <unistd.h>
11 :
12 : #include "generated/fd_shred_tile_seccomp.h"
13 : #include "../../util/pod/fd_pod_format.h"
14 : #include "fd_shredder.h"
15 : #include "fd_shred_batch.h"
16 : #include "fd_shred_dest.h"
17 : #include "fd_fec_resolver.h"
18 : #include "fd_stake_ci.h"
19 : #include "fd_rnonce_ss.h"
20 : #include "fd_shred_tile.h"
21 : #include "../store/fd_store.h"
22 : #include "../keyguard/fd_keyload.h"
23 : #include "../keyguard/fd_keyguard.h"
24 : #include "../keyguard/fd_keyguard_client.h"
25 : #include "../keyguard/fd_keyswitch.h"
26 : #include "../fd_disco.h"
27 : #include "../net/fd_net_tile.h"
28 : #include "../../flamenco/leaders/fd_leaders.h"
29 : #include "../../util/net/fd_net_headers.h"
30 : #include "../../flamenco/gossip/fd_gossip_message.h"
31 : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
32 : #include "../../flamenco/runtime/fd_slot_params.h"
33 : #include "../../discof/tower/fd_tower_slot_rooted.h"
34 : #include "../../discof/votor/fd_votor_rooted.h"
35 : #if FD_HAS_AVX
36 : #include "../../util/simd/fd_nt_memcpy.h"
37 : #else
38 : #define fd_memcpy_tn fd_memcpy
39 : #define fd_memcpy_nt_nofence fd_memcpy
40 : #define _mm_sfence() do {} while( 0 )
41 : #endif
42 :
43 : /* The shred tile handles shreds from two data sources: shreds generated
44 : from microblocks from the leader pipeline, and shreds retransmitted
45 : from the network.
46 :
47 : They have rather different semantics, but at the end of the day, they
48 : both result in a bunch of shreds and FEC sets that need to be sent to
49 : the blockstore and on the network, which is why one tile handles
50 : both.
51 :
52 : We segment the memory for the two types of shreds into two halves of
53 : a dcache because they follow somewhat different flow control
54 : patterns. For flow control, the normal guarantee we want to provide
55 : is that the dcache entry is not overwritten unless the mcache entry
56 : has also been overwritten. The normal way to do this when using both
57 : cyclically and with a 1-to-1 mapping is to make the dcache at least
58 : `burst` entries bigger than the mcache.
59 :
60 : The FEC arena uses an exposure window e. Frankendancer publishes
61 : arena pointers to shred_store, so e is that link's depth. Firedancer
62 : copies data out before after_frag returns and so uses a small bound
63 : instead.
64 :
65 : Leader pipeline: Every entry creates up to FD_SHRED_BATCH_FEC_SETS_MAX
66 : FEC sets. The leader section therefore needs
67 : e+FD_SHRED_BATCH_FEC_SETS_MAX entries.
68 :
69 : From the network: The FEC resolver doesn't use a cyclic order, but it
70 : does promise that once it returns an FEC set, it will return at least
71 : complete_depth FEC sets before returning it again. This means we
72 : want at most complete_depth-1 FEC sets exposed, so
73 : complete_depth=e+1 FEC sets. The FEC resolver has the
74 : ability to keep individual shreds for partial_depth calls, but
75 : because in this version of the shred tile, we send each shred to all
76 : its destinations as soon as we get it, we don't need that
77 : functionality, so we set partial_depth=1.
78 :
79 : Adding these up and plugging in the current value of
80 : BATCH_FEC_SETS_MAX, we get 2*e+6+fec_resolver_depth FEC sets. The
81 : Frankendancer topology code cannot specify mcache and dcache depth
82 : independently, so its shred_store link uses a doubled MTU and a
83 : burst of 4+fec_resolver_depth/2. With e=d, that gives
84 : 2*d+2*(4+fec_resolver_depth/2) >= 2*d+6+fec_resolver_depth FEC sets.
85 :
86 : A note on parallelization. From the network, shreds are distributed
87 : to tiles based on a validator-specific seeded hash of (slot, FEC set
88 : index) so all the shreds for a given FEC set (and any equivocating
89 : FEC set) are processed by the same tile. From the leader pipeline,
90 : the original implementation used to parallelize by batch of
91 : microblocks (so within a block, batches were distributed to different
92 : tiles). To support chained merkle shreds, the current implementation
93 : processes all the batches on tile 0 -- this should be a temporary
94 : state while Solana moves to a newer shred format that support better
95 : parallelization. */
96 :
97 0 : #define IN_KIND_CONTACT ( 0UL)
98 0 : #define IN_KIND_EPOCH ( 1UL) /* Firedancer */
99 0 : #define IN_KIND_STAKE ( 2UL) /* Frankendancer */
100 0 : #define IN_KIND_POH ( 3UL)
101 0 : #define IN_KIND_NET ( 4UL)
102 0 : #define IN_KIND_SIGN ( 5UL)
103 : #define IN_KIND_REPAIR ( 6UL)
104 0 : #define IN_KIND_IPECHO ( 7UL)
105 0 : #define IN_KIND_GOSSIP ( 8UL)
106 0 : #define IN_KIND_ROOTED ( 9UL)
107 0 : #define IN_KIND_ROOTEDH (10UL)
108 0 : #define IN_KIND_ROOTEDV (11UL) /* Alpenglow */
109 :
110 0 : #define NET_OUT_IDX 1
111 0 : #define SIGN_OUT_IDX 2
112 : #define IN_LINK_MAX 32UL
113 :
114 : FD_STATIC_ASSERT( sizeof(fd_entry_batch_meta_t)==56UL, poh_shred_mtu );
115 : FD_STATIC_ASSERT( sizeof(fd_fec_set_t)==FD_SHRED_STORE_MTU, shred_store_mtu );
116 :
117 0 : #define FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT 2
118 :
119 : /* Number of entries in the block_ids table. Each entry is 32 byte.
120 : This table is used to keep track of block ids that we create
121 : when we're leader, so that we can access them whenever we need
122 : a *parent* block id for a new block. Larger table allows to
123 : retrieve older parent block ids. Currently it's set for worst
124 : case parent offset of USHORT_MAX (max allowed in a shred),
125 : making the total table 2MiB.
126 : See also comment on chained_merkle_root. */
127 0 : #define BLOCK_IDS_TABLE_CNT USHORT_MAX
128 :
129 : /* See note on parallelization above. Currently we process all batches in tile 0. */
130 : #if 1
131 : #define SHOULD_PROCESS_THESE_SHREDS ( ctx->round_robin_id==0 )
132 : #else
133 : #define SHOULD_PROCESS_THESE_SHREDS ( ctx->batch_cnt%ctx->round_robin_cnt==ctx->round_robin_id )
134 : #endif
135 :
136 : /* The behavior of the shred tile is slightly different for
137 : Frankendancer vs Firedancer. For example, Frankendancer produces
138 : chained merkle shreds, while Firedancer doesn't yet. We can check
139 : at runtime the difference by inspecting the topology. The simplest
140 : way is to test if ctx->store is initialized. */
141 :
142 : typedef union {
143 : struct {
144 : fd_wksp_t * mem;
145 : ulong chunk0;
146 : ulong wmark;
147 : };
148 : fd_net_rx_bounds_t net_rx;
149 : } fd_shred_in_ctx_t;
150 :
151 : typedef struct {
152 : fd_shredder_t * shredder;
153 : fd_fec_resolver_t * resolver;
154 : ulong shred_limit;
155 : fd_pubkey_t identity_key[1]; /* Just the public key */
156 :
157 : ulong round_robin_id;
158 : ulong round_robin_cnt;
159 : /* Number of batches shredded from PoH during the current slot.
160 : This should be the same for all the shred tiles. */
161 : ulong batch_cnt;
162 : /* Slot of the most recent microblock we've seen from PoH,
163 : or 0 if we haven't seen one yet */
164 : ulong slot;
165 :
166 : fd_rnonce_ss_t repair_nonce_ss[1];
167 :
168 : fd_keyswitch_t * keyswitch;
169 : fd_keyguard_client_t keyguard_client[1];
170 :
171 : fd_fec_set_t * fec_sets;
172 :
173 : fd_stake_ci_t * stake_ci;
174 : /* These are used in between during_frag and after_frag */
175 : fd_shred_dest_weighted_t * new_dest_ptr;
176 : ulong new_dest_cnt;
177 : ulong shredded_txn_cnt;
178 : ulong new_root;
179 :
180 : ulong poh_in_expect_seq;
181 :
182 : ushort net_id;
183 :
184 : int skip_frag;
185 :
186 : ulong adtl_dests_leader_cnt;
187 : fd_shred_dest_weighted_t adtl_dests_leader [ FD_TOPO_ADTL_DESTS_MAX ];
188 : ulong adtl_dests_retransmit_cnt;
189 : fd_shred_dest_weighted_t adtl_dests_retransmit[ FD_TOPO_ADTL_DESTS_MAX ];
190 :
191 : fd_ip4_udp_hdrs_t data_shred_net_hdr [1];
192 : fd_ip4_udp_hdrs_t parity_shred_net_hdr[1];
193 :
194 : ulong shredder_fec_set_idx; /* In [0, shredder_max_fec_set_idx) */
195 : ulong shredder_max_fec_set_idx; /* exclusive */
196 :
197 : uchar shredder_merkle_root[32];
198 :
199 : ulong send_fec_set_idx[ FD_SHRED_BATCH_FEC_SETS_MAX ];
200 : ulong send_fec_set_cnt;
201 : ulong tsorig; /* timestamp of the last packet in compressed form */
202 :
203 : /* Includes Ethernet, IP, UDP headers */
204 : ulong shred_buffer_sz;
205 : uchar shred_buffer[ FD_NET_MTU ];
206 :
207 : /* These seeds get generated in privileged_init but used in
208 : unprivileged_init, so we store them here in between. */
209 : ulong resolver_seed;
210 : ulong shred_dest_seed;
211 :
212 : fd_shred_in_ctx_t in[ IN_LINK_MAX ];
213 : int in_kind[ IN_LINK_MAX ];
214 :
215 : fd_wksp_t * net_out_mem;
216 : ulong net_out_chunk0;
217 : ulong net_out_wmark;
218 : ulong net_out_chunk;
219 :
220 : ulong store_out_idx;
221 : fd_wksp_t * store_out_mem;
222 : ulong store_out_chunk0;
223 : ulong store_out_wmark;
224 : ulong store_out_chunk;
225 :
226 : /* This is the output link for shreds that is currently consumed by
227 : the repair and replay tile. */
228 : ulong shred_out_idx;
229 : fd_wksp_t * shred_out_mem;
230 : ulong shred_out_chunk0;
231 : ulong shred_out_wmark;
232 : ulong shred_out_chunk;
233 :
234 : fd_store_t * store;
235 : fd_store_map_t map_join[1];
236 : int disk_fd;
237 : int store_maintenance;
238 :
239 : fd_gossip_update_message_t gossip_upd_buf[1];
240 :
241 : struct {
242 : fd_histf_t contact_info_cnt[ 1 ];
243 : fd_histf_t batch_sz[ 1 ];
244 : fd_histf_t batch_microblock_cnt[ 1 ];
245 : fd_histf_t shredding_timing[ 1 ];
246 : fd_histf_t add_shred_timing[ 1 ];
247 : fd_histf_t fec_fallback_write_timing[ 1 ];
248 : ulong shred_processing_result[ FD_FEC_RESOLVER_ADD_SHRED_RETVAL_CNT+FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT ];
249 : ulong invalid_block_id_cnt;
250 : ulong shred_rejected_unchained_cnt;
251 : ulong repair_rcv_cnt;
252 : ulong repair_rcv_bytes;
253 : ulong turbine_rcv_cnt;
254 : ulong turbine_rcv_bytes;
255 : ulong bad_nonce;
256 : ulong fec_fallback_write_cnt;
257 : ulong fec_fallback_write_bytes;
258 : } metrics[ 1 ];
259 :
260 : struct {
261 : ulong txn_cnt;
262 : ulong pos; /* in payload, range [0, FD_SHRED_BATCH_RAW_BUF_SZ-8UL) */
263 : ulong slot; /* set to 0 when pos==0 */
264 : union {
265 : struct {
266 : ulong microblock_cnt;
267 : uchar payload[ FD_SHRED_BATCH_RAW_BUF_SZ - 8UL ];
268 : };
269 : uchar raw[ FD_SHRED_BATCH_RAW_BUF_SZ ];
270 : };
271 : } pending_batch;
272 :
273 : fd_epoch_schedule_t epoch_schedule[1];
274 : fd_shred_features_activation_t features_activation[1];
275 :
276 : /* max_shred_idx is the exclusive upper bound for shred
277 : indices. We need to reject any shred with an
278 : index >= current_max_shred_idx, but we also want to reject anything
279 : that is part of an FEC set whose highest shred index would reach
280 : the bound.
281 :
282 : Because this bound can change with feature gates, for example the
283 : reduce_slot_time feature gates, we store the bound for the
284 : previous, current, and next regimes, along with the slots at which
285 : the current and next bounds take effect. This lets us apply, to
286 : each shred, the limit effective at that shreds slot.
287 :
288 : For a given shred slot, the max_shred_idx for that shred is:
289 : next_max_shred_idx_start_slot <= slot -> next_max_shred_idx
290 : current_max_shred_idx_start_slot <= slot < next_max_shred_idx_start_slot -> current_max_shred_idx
291 : slot < current_max_shred_idx_start_slot -> prev_max_shred_idx
292 : */
293 : ulong prev_max_shred_idx;
294 : ulong current_max_shred_idx;
295 : ulong next_max_shred_idx;
296 : ulong current_max_shred_idx_start_slot;
297 : ulong next_max_shred_idx_start_slot;
298 : ulong bench_max_shred_idx; /* [development.bench.max_shreds_per_block], floors the chain's limits */
299 : /* too large to be left in the stack */
300 : fd_shred_dest_idx_t scratchpad_dests[ FD_SHRED_DEST_MAX_FANOUT*(FD_REEDSOL_DATA_SHREDS_MAX+FD_REEDSOL_PARITY_SHREDS_MAX) ];
301 :
302 : uchar * chained_merkle_root;
303 : fd_bmtree_node_t out_merkle_roots[ FD_SHRED_BATCH_FEC_SETS_MAX ];
304 : uchar block_ids[ BLOCK_IDS_TABLE_CNT ][ FD_SHRED_MERKLE_ROOT_SZ ];
305 :
306 : /* Bank object that we receive from the PoH tile and pass on to
307 : the store tile for setting the block_id of a slot. */
308 : void const * leader_bank;
309 : } fd_shred_ctx_t;
310 :
311 : /* shred features are generally considered active at the epoch *following*
312 : the epoch in which the feature gate is activated.
313 :
314 : As an optimization, when the activation slot is received, it is converted
315 : into the first slot of the subsequent epoch. This allows for a more
316 : efficient check (shred_slot >= feature_slot) and avoids the overhead of
317 : repeatedly converting slots into epochs for comparison.
318 :
319 : This function is only for Firedancer, while Frankendancer already receives
320 : the final activation slot from POH tile.
321 :
322 : In Agave, this is done with check_feature_activation():
323 : https://github.com/anza-xyz/agave/blob/v3.1.4/turbine/src/cluster_nodes.rs#L771
324 : https://github.com/anza-xyz/agave/blob/v3.1.4/core/src/shred_fetch_stage.rs#L456 */
325 : static inline ulong
326 0 : fd_shred_get_feature_activation_slot0( ulong feature_slot, fd_shred_ctx_t * ctx ) {
327 : /* if the feature does not have an activation slot yet, return ULONG_MAX */
328 0 : if( FD_UNLIKELY( feature_slot==ULONG_MAX ) ) {
329 0 : return ULONG_MAX;
330 0 : }
331 : /* if we don't have an epoch schedule yet, return ULONG_MAX */
332 0 : if( FD_UNLIKELY( ctx->epoch_schedule->slots_per_epoch==0 ) ) {
333 0 : return ULONG_MAX;
334 0 : }
335 : /* compute the activation epoch, add one, return the first slot. */
336 0 : ulong feature_epoch = 1 + fd_slot_to_epoch( ctx->epoch_schedule, feature_slot, NULL );
337 0 : return fd_epoch_slot0( ctx->epoch_schedule, feature_epoch );
338 0 : }
339 :
340 : FD_FN_CONST static inline ulong
341 0 : scratch_align( void ) {
342 0 : return 128UL;
343 0 : }
344 :
345 : FD_FN_PURE static inline ulong
346 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
347 :
348 0 : ulong fec_resolver_footprint = fd_fec_resolver_footprint( tile->shred.fec_resolver_depth, 1UL, tile->shred.fec_exposure+1UL,
349 0 : 128UL * tile->shred.fec_resolver_depth );
350 0 : ulong l = FD_LAYOUT_INIT;
351 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_shred_ctx_t), sizeof(fd_shred_ctx_t) );
352 0 : l = FD_LAYOUT_APPEND( l, fd_stake_ci_align(), fd_stake_ci_footprint() );
353 0 : l = FD_LAYOUT_APPEND( l, fd_fec_resolver_align(), fec_resolver_footprint );
354 0 : l = FD_LAYOUT_APPEND( l, fd_shredder_align(), fd_shredder_footprint() );
355 0 : return FD_LAYOUT_FINI( l, scratch_align() );
356 0 : }
357 :
358 : static inline void
359 0 : during_housekeeping( fd_shred_ctx_t * ctx ) {
360 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
361 0 : ulong seq_must_complete = ctx->keyswitch->param;
362 :
363 0 : if( FD_UNLIKELY( fd_seq_lt( ctx->poh_in_expect_seq, seq_must_complete ) ) ) {
364 : /* See fd_keyswitch.h, we need to flush any in-flight shreds from
365 : the leader pipeline before switching key. */
366 0 : FD_LOG_WARNING(( "Flushing in-flight unpublished shreds, must reach seq %lu, currently at %lu ...", seq_must_complete, ctx->poh_in_expect_seq ));
367 0 : return;
368 0 : }
369 :
370 0 : memcpy( ctx->identity_key->uc, ctx->keyswitch->bytes, 32UL );
371 0 : fd_stake_ci_set_identity( ctx->stake_ci, ctx->identity_key );
372 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
373 0 : }
374 0 : }
375 :
376 : static inline void
377 0 : metrics_write( fd_shred_ctx_t * ctx ) {
378 0 : FD_MHIST_COPY( SHRED, CONTACT_INFO_PER_MESSAGE, ctx->metrics->contact_info_cnt );
379 0 : FD_MHIST_COPY( SHRED, BATCH_SIZE_BYTES, ctx->metrics->batch_sz );
380 0 : FD_MHIST_COPY( SHRED, MICROBLOCK_PER_BATCH, ctx->metrics->batch_microblock_cnt );
381 0 : FD_MHIST_COPY( SHRED, SHREDDING_DURATION_SECONDS, ctx->metrics->shredding_timing );
382 0 : FD_MHIST_COPY( SHRED, ADD_SHRED_DURATION_SECONDS, ctx->metrics->add_shred_timing );
383 0 : FD_MHIST_COPY( SHRED, FEC_FALLBACK_WRITE_SECONDS, ctx->metrics->fec_fallback_write_timing );
384 0 : FD_MCNT_SET ( SHRED, SHRED_REPAIR_RX, ctx->metrics->repair_rcv_cnt );
385 0 : FD_MCNT_SET ( SHRED, SHRED_REPAIR_RX_BYTES, ctx->metrics->repair_rcv_bytes );
386 0 : FD_MCNT_SET ( SHRED, SHRED_TURBINE_RX, ctx->metrics->turbine_rcv_cnt );
387 0 : FD_MCNT_SET ( SHRED, SHRED_TURBINE_RX_BYTES, ctx->metrics->turbine_rcv_bytes );
388 0 : FD_MCNT_SET ( SHRED, NONCE_INVALID, ctx->metrics->bad_nonce );
389 0 : FD_MCNT_SET ( SHRED, FEC_FALLBACK_WRITE, ctx->metrics->fec_fallback_write_cnt );
390 0 : FD_MCNT_SET ( SHRED, FEC_FALLBACK_WRITE_BYTES, ctx->metrics->fec_fallback_write_bytes );
391 :
392 0 : FD_MCNT_SET ( SHRED, BLOCK_ID_INVALID, ctx->metrics->invalid_block_id_cnt );
393 0 : FD_MCNT_SET ( SHRED, SHRED_UNCHAINED_REJECTED, ctx->metrics->shred_rejected_unchained_cnt );
394 :
395 0 : FD_MCNT_ENUM_COPY( SHRED, SHRED_PROCESSED, ctx->metrics->shred_processing_result );
396 0 : }
397 :
398 : static void
399 : after_credit( fd_shred_ctx_t * ctx,
400 : fd_stem_context_t * stem FD_PARAM_UNUSED,
401 : int * opt_poll_in FD_PARAM_UNUSED,
402 0 : int * charge_busy ) {
403 0 : if( FD_UNLIKELY( ctx->store_maintenance && ctx->disk_fd>=0 &&
404 0 : fd_store_disk_maintain( ctx->store, ctx->disk_fd ) ) )
405 0 : *charge_busy = 1;
406 0 : }
407 :
408 : static inline void
409 : handle_new_cluster_contact_info( fd_shred_ctx_t * ctx,
410 0 : uchar const * buf ) {
411 0 : ulong const * header = (ulong const *)fd_type_pun_const( buf );
412 :
413 0 : ulong dest_cnt = header[ 0 ];
414 0 : fd_histf_sample( ctx->metrics->contact_info_cnt, dest_cnt );
415 :
416 0 : if( dest_cnt >= MAX_SHRED_DESTS )
417 0 : FD_LOG_ERR(( "Cluster nodes had %lu destinations, which was more than the max of %lu", dest_cnt, MAX_SHRED_DESTS ));
418 :
419 0 : fd_shred_dest_wire_t const * in_dests = fd_type_pun_const( header+1UL );
420 0 : fd_shred_dest_weighted_t * dests = fd_stake_ci_dest_add_init( ctx->stake_ci );
421 :
422 0 : ctx->new_dest_ptr = dests;
423 0 : ctx->new_dest_cnt = dest_cnt;
424 :
425 0 : for( ulong i=0UL; i<dest_cnt; i++ ) {
426 0 : memcpy( dests[i].pubkey.uc, in_dests[i].pubkey, 32UL );
427 0 : dests[i].ip4 = in_dests[i].ip4_addr;
428 0 : dests[i].port = in_dests[i].udp_port;
429 0 : }
430 0 : }
431 :
432 : static inline void
433 0 : finalize_new_cluster_contact_info( fd_shred_ctx_t * ctx ) {
434 0 : fd_stake_ci_dest_add_fini( ctx->stake_ci, ctx->new_dest_cnt );
435 0 : }
436 :
437 : static inline int
438 : before_frag( fd_shred_ctx_t * ctx,
439 : ulong in_idx,
440 : ulong seq,
441 0 : ulong sig ) {
442 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_IPECHO ) ) {
443 0 : FD_TEST( sig!=0UL && sig<=USHORT_MAX );
444 0 : fd_shredder_set_shred_version ( ctx->shredder, (ushort)sig );
445 0 : fd_fec_resolver_set_shred_version( ctx->resolver, (ushort)sig );
446 0 : return 1;
447 0 : }
448 :
449 0 : if( FD_UNLIKELY( !ctx->shredder->shred_version ) ) return -1;
450 :
451 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH ) ) {
452 0 : ctx->poh_in_expect_seq = seq+1UL;
453 0 : return (int)(fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_MICROBLOCK) &
454 0 : (int)(fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_SHRED_EPOCH_MSG) &
455 0 : (int)(fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_LEADER_BANK);
456 0 : }
457 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
458 0 : return (int)(fd_disco_netmux_sig_proto( sig )!=DST_PROTO_SHRED) & (int)(fd_disco_netmux_sig_proto( sig )!=DST_PROTO_REPAIR);
459 0 : }
460 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ){
461 0 : return sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO &&
462 0 : sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE;
463 0 : }
464 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_ROOTEDH ) ) {
465 0 : return sig!=0UL; /* only care about rooted banks, not completed blockhash */
466 0 : }
467 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_ROOTED ) ) {
468 0 : return sig!=FD_TOWER_SIG_SLOT_ROOTED; /* only care about slot_confirmed messages */
469 0 : }
470 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_ROOTEDV ) ) {
471 0 : return sig!=FD_VOTOR_SIG_ROOTED; /* only care about rooted messages */
472 0 : }
473 0 : return 0;
474 0 : }
475 :
476 : static void
477 : during_frag( fd_shred_ctx_t * ctx,
478 : ulong in_idx,
479 : ulong seq FD_PARAM_UNUSED,
480 : ulong sig,
481 : ulong chunk,
482 : ulong sz,
483 0 : ulong ctl ) {
484 :
485 0 : ctx->skip_frag = 0;
486 :
487 0 : ctx->tsorig = fd_frag_meta_ts_comp( fd_tickcount() );
488 :
489 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPAIR ) ) {
490 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_NET_MTU ) )
491 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
492 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
493 :
494 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
495 0 : fd_memcpy( ctx->shred_buffer, dcache_entry, sz );
496 0 : return;
497 0 : }
498 :
499 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_CONTACT ) ) {
500 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
501 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
502 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
503 :
504 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
505 0 : handle_new_cluster_contact_info( ctx, dcache_entry );
506 0 : return;
507 0 : }
508 :
509 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
510 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>sizeof(fd_gossip_update_message_t) ) )
511 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
512 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
513 0 : uchar const * gossip_upd_msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
514 0 : fd_memcpy( ctx->gossip_upd_buf, gossip_upd_msg, sz );
515 0 : return;
516 0 : }
517 :
518 : /* Firedancer only */
519 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_ROOTED ) ) {
520 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz<sizeof(fd_tower_slot_rooted_t) ) )
521 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
522 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
523 0 : fd_tower_slot_rooted_t const * rooted_msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
524 0 : ctx->new_root = rooted_msg->slot;
525 0 : return;
526 0 : }
527 :
528 : /* Firedancer only (Alpenglow) */
529 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_ROOTEDV ) ) {
530 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz<sizeof(fd_votor_rooted_t) ) )
531 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
532 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
533 0 : fd_votor_rooted_t const * rooted_msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
534 0 : ctx->new_root = rooted_msg->slot;
535 0 : return;
536 0 : }
537 :
538 : /* Frankendancer only */
539 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_ROOTEDH ) ) {
540 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz<sizeof(fd_rooted_bank_t) ) )
541 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
542 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
543 : /* The message format is a pointer to the bank (which is in the
544 : agave address space, so we couldn't access it even if we wanted
545 : to) followed by the rooted slot. */
546 0 : ulong const * replay_msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
547 0 : ctx->new_root = replay_msg[ 1 ];
548 0 : return;
549 0 : }
550 :
551 : /* Firedancer only */
552 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_EPOCH ) ) {
553 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
554 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
555 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
556 :
557 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
558 0 : fd_epoch_info_msg_t const * epoch_msg = fd_type_pun_const( dcache_entry );
559 :
560 0 : FD_TEST( epoch_msg->staked_vote_cnt<=MAX_STAKE_WEIGHTS );
561 0 : FD_TEST( epoch_msg->staked_id_cnt<=MAX_STAKE_WEIGHTS );
562 :
563 0 : fd_stake_ci_epoch_msg_init( ctx->stake_ci, epoch_msg );
564 :
565 0 : *ctx->epoch_schedule = epoch_msg->epoch_schedule;
566 :
567 0 : fd_slot_params_t slot_params = fd_slot_params_lookup( &FD_SLOT_PARAMS_400MS,
568 0 : &epoch_msg->features,
569 0 : &epoch_msg->epoch_schedule,
570 0 : epoch_msg->start_slot );
571 :
572 0 : ctx->current_max_shred_idx = fd_ulong_max( slot_params.max_shred_idx, ctx->bench_max_shred_idx );
573 0 : ctx->current_max_shred_idx_start_slot = fd_slot_params_effective_slot( &slot_params,
574 0 : &epoch_msg->features,
575 0 : &epoch_msg->epoch_schedule );
576 0 : ctx->next_max_shred_idx_start_slot = fd_slot_params_next_effective_slot( &slot_params,
577 0 : &epoch_msg->features,
578 0 : &epoch_msg->epoch_schedule );
579 0 : ctx->prev_max_shred_idx = fd_ulong_max( fd_slot_params_lookup( &FD_SLOT_PARAMS_400MS,
580 0 : &epoch_msg->features,
581 0 : &epoch_msg->epoch_schedule,
582 0 : fd_ulong_sat_sub( ctx->current_max_shred_idx_start_slot, 1UL ) ).max_shred_idx, ctx->bench_max_shred_idx );
583 0 : ctx->next_max_shred_idx = fd_ulong_max( fd_slot_params_lookup( &FD_SLOT_PARAMS_400MS,
584 0 : &epoch_msg->features,
585 0 : &epoch_msg->epoch_schedule,
586 0 : ctx->next_max_shred_idx_start_slot ).max_shred_idx, ctx->bench_max_shred_idx );
587 0 : ctx->features_activation->enforce_fixed_fec_set = fd_shred_get_feature_activation_slot0(
588 0 : epoch_msg->features.enforce_fixed_fec_set, ctx );
589 :
590 0 : return;
591 0 : }
592 :
593 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) {
594 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
595 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
596 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
597 :
598 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
599 0 : fd_stake_ci_stake_msg_init( ctx->stake_ci, fd_type_pun_const( dcache_entry ) );
600 0 : return;
601 0 : }
602 :
603 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH ) ) {
604 0 : ctx->send_fec_set_cnt = 0UL;
605 :
606 0 : if( FD_UNLIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_LEADER_BANK ) ) {
607 : /* Only one tile needs to act on this. Other tiles see the frag but
608 : ignore it. */
609 0 : if( ctx->round_robin_id!=0UL ) return;
610 : /* poh is handing off a leader bank pointer for a slot we may
611 : be leader for. Copy the pointer out of the dcache for
612 : after_frag to attach to the FEC sets for this slot. */
613 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(void const *) ) )
614 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
615 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
616 :
617 0 : void const * const * src = (void const * const *)fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
618 0 : ctx->leader_bank = *src;
619 0 : return;
620 0 : }
621 :
622 0 : if( FD_UNLIKELY( (fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_SHRED_EPOCH_MSG) ) ) {
623 : /* There is a subset of FD_SHRED_FEATURES_ACTIVATION_... slots
624 : that the shred tile needs to be aware of, as well as
625 : shred limits that can change at epoch boundaries. Since these
626 : require the bank, we are forced (so far) to receive them from
627 : the poh tile (as a POH_PKT_TYPE_SHRED_EPOCH_MSG). */
628 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
629 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=(sizeof(fd_shred_epoch_msg_t)) ) )
630 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
631 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
632 :
633 0 : fd_shred_epoch_msg_t const * msg = (fd_shred_epoch_msg_t const *)dcache_entry;
634 :
635 0 : *ctx->features_activation = msg->features_activation;
636 :
637 0 : fd_shred_slot_limits_t const * lim = &msg->slot_limits;
638 0 : ctx->prev_max_shred_idx = fd_ulong_max( lim->prev_max_shred_idx, ctx->bench_max_shred_idx );
639 0 : ctx->current_max_shred_idx = fd_ulong_max( lim->current_max_shred_idx, ctx->bench_max_shred_idx );
640 0 : ctx->next_max_shred_idx = fd_ulong_max( lim->next_max_shred_idx, ctx->bench_max_shred_idx );
641 0 : ctx->current_max_shred_idx_start_slot = lim->current_start_slot;
642 0 : ctx->next_max_shred_idx_start_slot = lim->next_start_slot;
643 0 : }
644 0 : else { /* (fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK) */
645 : /* This is a frag from the PoH tile. We'll copy it to our pending
646 : microblock batch and shred it if necessary (last in block or
647 : above watermark). We just go ahead and shred it here, even
648 : though we may get overrun. If we do end up getting overrun, we
649 : just won't send these shreds out and we'll reuse the FEC set for
650 : the next one. From a higher level though, if we do get overrun,
651 : a bunch of shreds will never be transmitted, and we'll end up
652 : producing a block that never lands on chain. */
653 :
654 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
655 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_POH_SHRED_MTU ||
656 0 : sz<(sizeof(fd_entry_batch_meta_t)+sizeof(fd_entry_batch_header_t)) ) )
657 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
658 0 : ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
659 :
660 0 : fd_entry_batch_meta_t const * entry_meta = (fd_entry_batch_meta_t const *)dcache_entry;
661 0 : uchar const * entry = dcache_entry + sizeof(fd_entry_batch_meta_t);
662 0 : ulong entry_sz = sz - sizeof(fd_entry_batch_meta_t);
663 :
664 0 : fd_entry_batch_header_t const * microblock = (fd_entry_batch_header_t const *)entry;
665 :
666 : /* It should never be possible for this to fail, but we check it
667 : anyway. */
668 0 : FD_TEST( entry_sz + ctx->pending_batch.pos <= sizeof(ctx->pending_batch.payload) );
669 :
670 0 : ulong target_slot = fd_disco_poh_sig_slot( sig );
671 0 : if( FD_UNLIKELY( (ctx->pending_batch.microblock_cnt>0) & (ctx->pending_batch.slot!=target_slot) ) ) {
672 : /* TODO: The Agave client sends a dummy entry batch with only 1
673 : byte and the block-complete bit set. This helps other
674 : validators know that the block is dead and they should not try
675 : to continue building a fork on it. We probably want a similar
676 : approach eventually. */
677 0 : FD_LOG_WARNING(( "Abandoning %lu microblocks for slot %lu and switching to slot %lu",
678 0 : ctx->pending_batch.microblock_cnt, ctx->pending_batch.slot, target_slot ));
679 0 : ctx->pending_batch.slot = 0UL;
680 0 : ctx->pending_batch.pos = 0UL;
681 0 : ctx->pending_batch.microblock_cnt = 0UL;
682 0 : ctx->pending_batch.txn_cnt = 0UL;
683 0 : ctx->batch_cnt = 0UL;
684 :
685 0 : FD_MCNT_INC( SHRED, MICROBLOCK_ABANDONED, 1UL );
686 0 : }
687 :
688 0 : ctx->pending_batch.slot = target_slot;
689 : /* We want to send out some shreds immediately when we start a new
690 : slot to help with leader targeting. */
691 0 : int new_slot = 0;
692 0 : if( FD_UNLIKELY( target_slot!=ctx->slot )) {
693 : /* Reset batch count if we are in a new slot */
694 0 : ctx->batch_cnt = 0UL;
695 0 : ctx->slot = target_slot;
696 0 : new_slot = 1;
697 :
698 : /* At the beginning of a new slot, prepare chained_merkle_root.
699 : chained_merkle_root is initialized at the block_id of the parent
700 : block, there's two cases:
701 :
702 : 1. block_id is passed in by the poh tile:
703 : - it's always passed when parent block had a different leader
704 : - it may be passed when we were leader for parent block (there
705 : are race conditions when it's not passed)
706 :
707 : 2. block_id is taken from block_ids table if we were the leader
708 : for the parent block (when we were NOT the leader, because of
709 : equivocation, we can't store block_id in the table)
710 :
711 : chained_merkle_root is stored in block_ids table at target_slot
712 : and it's progressively updated as more microblocks are received.
713 : As a result, when we move to a new slot, the block_ids table at
714 : the old slot will contain the block_id.
715 :
716 : The block_ids table is designed to protect against the race condition
717 : case in 1., therefore the table may not be set in some cases, e.g. if
718 : a validator (re)starts, but in those cases we don't expect the race
719 : condition to apply. */
720 0 : ctx->chained_merkle_root = ctx->block_ids[ target_slot % BLOCK_IDS_TABLE_CNT ];
721 0 : if( FD_UNLIKELY( SHOULD_PROCESS_THESE_SHREDS ) ) {
722 0 : if( FD_LIKELY( entry_meta->parent_block_id_valid ) ) {
723 : /* 1. Initialize chained_merkle_root sent from poh tile */
724 0 : memcpy( ctx->chained_merkle_root, entry_meta->parent_block_id, FD_SHRED_MERKLE_ROOT_SZ );
725 0 : } else {
726 0 : ulong parent_slot = target_slot - entry_meta->parent_offset;
727 0 : fd_epoch_leaders_t const * lsched = fd_stake_ci_get_lsched_for_slot( ctx->stake_ci, parent_slot );
728 0 : fd_pubkey_t const * slot_leader = fd_epoch_leaders_get( lsched, parent_slot );
729 :
730 0 : if( lsched && slot_leader && fd_memeq( slot_leader, ctx->identity_key, sizeof(fd_pubkey_t) ) ) {
731 : /* 2. Initialize chained_merkle_root from block_ids table, if we were the leader */
732 0 : memcpy( ctx->chained_merkle_root, ctx->block_ids[ parent_slot % BLOCK_IDS_TABLE_CNT ], FD_SHRED_MERKLE_ROOT_SZ );
733 0 : } else {
734 : /* This should never happen, log a metric and set chained_merkle_root to 0 */
735 0 : ctx->metrics->invalid_block_id_cnt++;
736 0 : memset( ctx->chained_merkle_root, 0, FD_SHRED_MERKLE_ROOT_SZ );
737 0 : }
738 0 : }
739 0 : }
740 0 : }
741 :
742 0 : if( FD_LIKELY( !SHOULD_PROCESS_THESE_SHREDS ) ) {
743 : /* If we are not processing this batch, filter in after_frag. */
744 0 : ctx->skip_frag = 1;
745 0 : }
746 :
747 0 : ulong pending_batch_wmark = FD_SHRED_BATCH_WMARK_CHAINED;
748 0 : uchar * chained_merkle_root = ctx->chained_merkle_root;
749 0 : ulong load_for_32_shreds = FD_SHREDDER_CHAINED_FEC_SET_PAYLOAD_SZ;
750 : /* All fec sets in the last batch of a block need to be resigned.
751 : This needs to match Agave's behavior - as a reference, see:
752 : https://github.com/anza-xyz/agave/blob/v2.3/ledger/src/shred/merkle.rs#L1040 */
753 0 : if( FD_UNLIKELY( entry_meta->block_complete==1 ) ) {
754 0 : pending_batch_wmark = FD_SHRED_BATCH_WMARK_RESIGNED;
755 : /* chained_merkle_root also applies to resigned FEC sets. */
756 0 : load_for_32_shreds = FD_SHREDDER_RESIGNED_FEC_SET_PAYLOAD_SZ;
757 0 : }
758 :
759 : /* If this microblock completes the block, the batch is then
760 : finalized here. Otherwise, we check whether the new entry
761 : would exceed the pending_batch_wmark. If true, then the
762 : batch is closed now, shredded, and a new batch is started
763 : with the incoming microblock. If false, no shredding takes
764 : place, and the microblock is added to the current batch.
765 : Pack limits entry bytes so this batching cannot exceed
766 : max_shred_idx. */
767 0 : int is_marker = entry_meta->block_complete==-1;
768 0 : int writing_marker = 0;
769 0 : int forced_end_batch = entry_meta->block_complete | new_slot;
770 0 : int batch_would_exceed_wmark = ( ctx->pending_batch.pos + entry_sz ) > pending_batch_wmark;
771 0 : int include_in_current_batch = !is_marker && ( forced_end_batch | ( !batch_would_exceed_wmark ) );
772 0 : int process_current_batch = is_marker ? !!ctx->pending_batch.pos : ( forced_end_batch | batch_would_exceed_wmark );
773 0 : int init_new_batch = !include_in_current_batch;
774 :
775 0 : if( FD_LIKELY( include_in_current_batch ) ) {
776 0 : if( FD_UNLIKELY( SHOULD_PROCESS_THESE_SHREDS ) ) {
777 : /* Ugh, yet another memcpy */
778 0 : fd_memcpy_tn( ctx->pending_batch.payload + ctx->pending_batch.pos, entry, entry_sz );
779 0 : }
780 0 : ctx->pending_batch.pos += entry_sz;
781 0 : ctx->pending_batch.microblock_cnt += 1UL;
782 0 : ctx->pending_batch.txn_cnt += microblock->txn_cnt;
783 0 : }
784 :
785 0 : alpenglow_marker:
786 0 : if( FD_LIKELY( process_current_batch )) {
787 : /* Batch and padding size calculation. */
788 0 : ulong batch_sz = sizeof(ulong) + ctx->pending_batch.pos; /* without padding */
789 0 : ulong batch_sz_padded = load_for_32_shreds * ( ( batch_sz + load_for_32_shreds - 1UL ) / load_for_32_shreds );
790 0 : ulong padding_sz = batch_sz_padded - batch_sz;
791 :
792 0 : if( FD_UNLIKELY( SHOULD_PROCESS_THESE_SHREDS ) ) {
793 : /* If it's our turn, shred this batch. FD_UNLIKELY because shred
794 : tile cnt generally >= 2 */
795 :
796 0 : long shredding_timing = -fd_tickcount();
797 :
798 0 : fd_memset( ctx->pending_batch.payload + ctx->pending_batch.pos, 0, padding_sz );
799 :
800 0 : if( FD_UNLIKELY( !writing_marker ) ) ctx->send_fec_set_cnt = 0UL; /* verbose */
801 0 : ctx->shredded_txn_cnt = ctx->pending_batch.txn_cnt;
802 :
803 0 : fd_shredder_init_batch( ctx->shredder, ctx->pending_batch.raw, batch_sz_padded, target_slot, entry_meta );
804 :
805 0 : ulong pend_sz = batch_sz_padded;
806 0 : ulong pend_idx = ctx->send_fec_set_cnt;
807 0 : while( pend_sz > 0UL ) {
808 :
809 0 : fd_fec_set_t * out = ctx->fec_sets + ctx->shredder_fec_set_idx;
810 :
811 0 : FD_TEST( fd_shredder_next_fec_set( ctx->shredder, out, chained_merkle_root ) );
812 0 : FD_TEST( pend_idx<FD_SHRED_BATCH_FEC_SETS_MAX );
813 0 : memcpy( ctx->out_merkle_roots[pend_idx].hash, chained_merkle_root, 32UL );
814 :
815 0 : out->data_shred_rcvd = 0U;
816 0 : out->parity_shred_rcvd = 0U;
817 0 : out->turbine_shred_rcvd = 0UL;
818 0 : out->repair_shred_rcvd = 0UL;
819 :
820 0 : FD_TEST( ctx->send_fec_set_cnt<FD_SHRED_BATCH_FEC_SETS_MAX );
821 0 : ctx->send_fec_set_idx[ ctx->send_fec_set_cnt ] = ctx->shredder_fec_set_idx;
822 0 : ctx->send_fec_set_cnt += 1UL;
823 0 : ctx->shredder_fec_set_idx = (ctx->shredder_fec_set_idx+1UL)%ctx->shredder_max_fec_set_idx;
824 :
825 0 : pend_sz -= load_for_32_shreds;
826 0 : pend_idx++;
827 0 : }
828 :
829 0 : fd_shredder_fini_batch( ctx->shredder );
830 0 : shredding_timing += fd_tickcount();
831 :
832 : /* Update metrics */
833 0 : fd_histf_sample( ctx->metrics->batch_sz, batch_sz /* without padding */ );
834 0 : fd_histf_sample( ctx->metrics->batch_microblock_cnt, ctx->pending_batch.microblock_cnt );
835 0 : fd_histf_sample( ctx->metrics->shredding_timing, (ulong)shredding_timing );
836 0 : } else {
837 0 : if( FD_UNLIKELY( !writing_marker ) ) ctx->send_fec_set_cnt = 0UL; /* verbose */
838 :
839 0 : fd_shredder_skip_batch( ctx->shredder, batch_sz_padded, target_slot, entry_meta->block_complete==1 );
840 0 : }
841 :
842 0 : ctx->pending_batch.slot = 0UL;
843 0 : ctx->pending_batch.pos = 0UL;
844 0 : ctx->pending_batch.microblock_cnt = 0UL;
845 0 : ctx->pending_batch.txn_cnt = 0UL;
846 0 : ctx->batch_cnt++;
847 0 : }
848 :
849 0 : if( FD_UNLIKELY( init_new_batch && !writing_marker ) ) {
850 : /* TODO: this assumes that SHOULD_PROCESS_THESE_SHREDS is
851 : constant across batches. Otherwise, the condition may
852 : need to be removed (or adjusted). */
853 0 : if( FD_UNLIKELY( SHOULD_PROCESS_THESE_SHREDS ) ) {
854 : /* Ugh, yet another memcpy */
855 0 : fd_memcpy_tn( is_marker ? ctx->pending_batch.raw : ctx->pending_batch.payload + 0UL /* verbose */, entry, entry_sz );
856 0 : }
857 0 : ctx->pending_batch.slot = target_slot;
858 0 : ctx->pending_batch.pos = fd_ulong_if( is_marker, entry_sz-sizeof(ulong), entry_sz );
859 0 : ctx->pending_batch.microblock_cnt = fd_ulong_if( is_marker, 0UL, 1UL );
860 0 : ctx->pending_batch.txn_cnt = fd_ulong_if( is_marker, 0UL, microblock->txn_cnt );
861 0 : }
862 :
863 : /* The entry opened a batch of its own, so close it now. */
864 0 : if( FD_UNLIKELY( is_marker && !writing_marker ) ) {
865 0 : process_current_batch = 1;
866 0 : writing_marker = 1;
867 0 : goto alpenglow_marker;
868 0 : }
869 0 : }
870 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
871 : /* The common case, from the net tile. The FEC resolver API does
872 : not present a prepare/commit model. If we get overrun between
873 : when the FEC resolver verifies the signature and when it stores
874 : the local copy, we could end up storing and retransmitting
875 : garbage. Instead we copy it locally, sadly, and only give it to
876 : the FEC resolver when we know it won't be overrun anymore. */
877 0 : uchar const * dcache_entry = fd_net_rx_translate_frag( &ctx->in[ in_idx ].net_rx, chunk, ctl, sz );
878 0 : ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
879 0 : FD_TEST( hdr_sz <= sz ); /* Should be ensured by the net tile */
880 : /* Use the looser ctx->shred_limit as the max_shred_idx for this
881 : parse, as we do not have the slot for the shred yet. The
882 : tighter shred-specific max_shred_idx will be applied in the
883 : parse in after_frag. */
884 0 : fd_shred_t const * shred = fd_shred_parse( dcache_entry+hdr_sz, sz-hdr_sz, ctx->shred_limit );
885 0 : if( FD_UNLIKELY( !shred ) ) {
886 0 : ctx->skip_frag = 1;
887 0 : return;
888 0 : };
889 :
890 0 : if( FD_UNLIKELY( fd_disco_netmux_sig_proto( sig )==DST_PROTO_REPAIR ) ) {
891 0 : ctx->metrics->repair_rcv_cnt++;
892 0 : ctx->metrics->repair_rcv_bytes += sz;
893 0 : } else {
894 0 : ctx->metrics->turbine_rcv_cnt++;
895 0 : ctx->metrics->turbine_rcv_bytes += sz;
896 0 : }
897 :
898 : /* Drop unchained merkle shreds */
899 0 : int is_unchained = !fd_shred_is_chained( fd_shred_type( shred->variant ) );
900 0 : if( FD_UNLIKELY( is_unchained ) ) {
901 0 : ctx->metrics->shred_rejected_unchained_cnt++;
902 0 : ctx->skip_frag = 1;
903 0 : return;
904 0 : };
905 :
906 : /* all shreds in the same FEC set will have the same signature
907 : so we can round-robin shreds between the shred tiles based on
908 : just the signature without splitting individual FEC sets. */
909 0 : ulong sig = fd_ulong_load_8( shred->signature );
910 0 : if( FD_LIKELY( sig%ctx->round_robin_cnt!=ctx->round_robin_id ) ) {
911 0 : ctx->skip_frag = 1;
912 0 : return;
913 0 : }
914 0 : fd_memcpy( ctx->shred_buffer, dcache_entry+hdr_sz, sz-hdr_sz );
915 0 : ctx->shred_buffer_sz = sz-hdr_sz;
916 0 : }
917 0 : }
918 :
919 : static inline void
920 : send_shred( fd_shred_ctx_t * ctx,
921 : fd_stem_context_t * stem,
922 : fd_shred_t const * shred,
923 : fd_shred_dest_weighted_t const * dest,
924 0 : ulong tsorig ) {
925 :
926 0 : if( FD_UNLIKELY( !dest->ip4 ) ) return;
927 :
928 0 : uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
929 :
930 0 : int is_data = fd_shred_is_data( fd_shred_type( shred->variant ) );
931 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
932 0 : *hdr = *( is_data ? ctx->data_shred_net_hdr : ctx->parity_shred_net_hdr );
933 :
934 0 : fd_ip4_hdr_t * ip4 = hdr->ip4;
935 0 : ip4->daddr = dest->ip4;
936 0 : ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
937 0 : ip4->check = 0U;
938 0 : ip4->check = fd_ip4_hdr_check_fast( ip4 );
939 :
940 0 : hdr->udp->net_dport = fd_ushort_bswap( dest->port );
941 :
942 0 : ulong shred_sz = fd_ulong_if( is_data, FD_SHRED_MIN_SZ, FD_SHRED_MAX_SZ );
943 0 : #if FD_HAS_AVX
944 : /* We're going to copy this shred potentially a bunch of times without
945 : reading it again, and we'd rather not thrash our cache, so we want
946 : to use non-temporal writes here. We need to make sure we don't
947 : touch the cache line containing the network headers that we just
948 : wrote to though. We know the destination is 64 byte aligned. */
949 0 : fd_memcpy_nt( packet+sizeof(fd_ip4_udp_hdrs_t), shred, shred_sz );
950 : #else
951 : fd_memcpy ( packet+sizeof(fd_ip4_udp_hdrs_t), shred, shred_sz );
952 : #endif
953 :
954 0 : ulong pkt_sz = shred_sz + sizeof(fd_ip4_udp_hdrs_t);
955 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
956 0 : ulong sig = fd_disco_netmux_sig( dest->ip4, dest->port, dest->ip4, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
957 0 : ulong const chunk = ctx->net_out_chunk;
958 0 : fd_stem_publish( stem, NET_OUT_IDX, sig, chunk, pkt_sz, 0UL, tsorig, tspub );
959 0 : ctx->net_out_chunk = fd_dcache_compact_next( chunk, pkt_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
960 0 : }
961 :
962 : static void
963 : after_frag( fd_shred_ctx_t * ctx,
964 : ulong in_idx,
965 : ulong seq,
966 : ulong sig,
967 : ulong sz,
968 : ulong tsorig,
969 : ulong _tspub,
970 0 : fd_stem_context_t * stem ) {
971 0 : (void)seq;
972 0 : (void)sz;
973 0 : (void)tsorig;
974 0 : (void)_tspub;
975 :
976 0 : if( FD_UNLIKELY( ctx->skip_frag ) ) return;
977 :
978 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_CONTACT ) ) {
979 0 : finalize_new_cluster_contact_info( ctx );
980 0 : return;
981 0 : }
982 :
983 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_EPOCH ) ) {
984 0 : fd_stake_ci_epoch_msg_fini( ctx->stake_ci );
985 0 : return;
986 0 : }
987 :
988 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) {
989 0 : fd_stake_ci_stake_msg_fini( ctx->stake_ci );
990 0 : return;
991 0 : }
992 :
993 0 : if( FD_UNLIKELY( (ctx->in_kind[ in_idx ]==IN_KIND_ROOTED) | (ctx->in_kind[ in_idx ]==IN_KIND_ROOTEDH) | (ctx->in_kind[ in_idx ]==IN_KIND_ROOTEDV) ) ) {
994 0 : if( FD_LIKELY( (ctx->new_root > 0UL) & (ctx->new_root<ULONG_MAX) ) ) fd_fec_resolver_advance_slot_old( ctx->resolver, ctx->new_root );
995 0 : return;
996 0 : }
997 :
998 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
999 0 : if( ctx->gossip_upd_buf->tag==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ) {
1000 0 : fd_gossip_contact_info_t const * ci = ctx->gossip_upd_buf->contact_info->value;
1001 0 : fd_ip4_port_t tvu_addr;
1002 0 : tvu_addr.addr = ci->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU ].is_ipv6 ? 0U : ci->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU ].ip4;
1003 0 : tvu_addr.port = ci->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU ].port;
1004 0 : if( !tvu_addr.l ){
1005 0 : fd_stake_ci_dest_remove( ctx->stake_ci, fd_type_pun_const( ctx->gossip_upd_buf->origin ) );
1006 0 : } else {
1007 0 : fd_stake_ci_dest_update( ctx->stake_ci, fd_type_pun_const( ctx->gossip_upd_buf->origin ), tvu_addr.addr, fd_ushort_bswap( tvu_addr.port ) );
1008 0 : }
1009 0 : } else if( ctx->gossip_upd_buf->tag==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE ) {
1010 0 : if( FD_UNLIKELY( !memcmp( ctx->identity_key->uc, ctx->gossip_upd_buf->origin, 32UL ) ) ) {
1011 : /* If our own contact info was dropped, we update with dummy IP
1012 : instead of removing since stake_ci expects our contact info
1013 : in the sdests table all the time. fd_stake_ci_new initializes
1014 : both ei->sdests with our contact info so this should always
1015 : update (and not append). */
1016 0 : fd_stake_ci_dest_update( ctx->stake_ci, fd_type_pun_const( ctx->gossip_upd_buf->origin ), 1U, 0U );
1017 0 : } else {
1018 0 : fd_stake_ci_dest_remove( ctx->stake_ci, fd_type_pun_const( ctx->gossip_upd_buf->origin ) );
1019 0 : }
1020 0 : }
1021 0 : return;
1022 0 : }
1023 :
1024 0 : if( FD_UNLIKELY( (ctx->in_kind[ in_idx ]==IN_KIND_POH) & (ctx->send_fec_set_cnt==0UL) ) ) {
1025 : /* Entry from PoH that didn't trigger a new FEC set to be made */
1026 0 : return;
1027 0 : }
1028 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPAIR ) ) {
1029 0 : return;
1030 0 : }
1031 :
1032 0 : ulong fanout = 200UL; /* Default Agave's DATA_PLANE_FANOUT = 200UL */
1033 :
1034 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
1035 0 : uchar * shred_buffer = ctx->shred_buffer;
1036 0 : ulong shred_buffer_sz = ctx->shred_buffer_sz;
1037 :
1038 : /* Accessing the slot like this is safe because we have already
1039 : parsed the shred in during_frag */
1040 0 : ulong shred_slot = ((fd_shred_t const *)shred_buffer)->slot;
1041 0 : ulong max_shred_idx = ctx->current_max_shred_idx;
1042 0 : if( FD_UNLIKELY( shred_slot< ctx->current_max_shred_idx_start_slot ) ) max_shred_idx = ctx->prev_max_shred_idx;
1043 0 : if( FD_UNLIKELY( shred_slot>=ctx->next_max_shred_idx_start_slot ) ) max_shred_idx = ctx->next_max_shred_idx;
1044 :
1045 0 : fd_shred_t const * shred = fd_shred_parse( shred_buffer, shred_buffer_sz, max_shred_idx );
1046 :
1047 0 : if( FD_UNLIKELY( !shred ) ) { ctx->metrics->shred_processing_result[ 1 ]++; return; }
1048 :
1049 0 : fd_epoch_leaders_t const * lsched = fd_stake_ci_get_lsched_for_slot( ctx->stake_ci, shred->slot );
1050 0 : if( FD_UNLIKELY( !lsched ) ) { ctx->metrics->shred_processing_result[ 0 ]++; return; }
1051 :
1052 0 : fd_pubkey_t const * slot_leader = fd_epoch_leaders_get( lsched, shred->slot );
1053 0 : if( FD_UNLIKELY( !slot_leader ) ) { ctx->metrics->shred_processing_result[ 0 ]++; return; } /* Count this as bad slot too */
1054 :
1055 0 : fd_fec_set_t const * out_fec_set[1];
1056 0 : fd_shred_t const * out_shred[1];
1057 0 : fd_fec_resolver_spilled_t spilled_fec = { 0 };
1058 0 : int from_repair = 0;
1059 :
1060 0 : uint nonce = UINT_MAX;
1061 0 : ulong shred_sz = fd_shred_sz( shred );
1062 0 : if( FD_UNLIKELY( (fd_disco_netmux_sig_proto( sig )==DST_PROTO_REPAIR) & (shred_buffer_sz>=shred_sz+sizeof(uint)) ) ) {
1063 0 : nonce = FD_LOAD(uint, shred_buffer + shred_sz );
1064 0 : long est_now_ns = fd_log_wallclock(); /* TODO: switch to fd_clock for performance */
1065 0 : int slot_complete = fd_shred_is_data( fd_shred_type( shred->variant ) ) && (shred->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE);
1066 0 : int nonce_okay = fd_rnonce_ss_verify( ctx->repair_nonce_ss, nonce, shred->slot, shred->idx, slot_complete, est_now_ns );
1067 0 : ctx->metrics->bad_nonce += (ulong)(!nonce_okay);
1068 0 : from_repair = nonce_okay;
1069 0 : }
1070 :
1071 0 : uint shred_source = fd_disco_netmux_sig_proto( sig )==DST_PROTO_REPAIR
1072 0 : ? fd_uint_if( from_repair, FD_FEC_RESOLVER_SHRED_SRC_REPAIR, FD_FEC_RESOLVER_SHRED_SRC_BAD_REPAIR )
1073 0 : : FD_FEC_RESOLVER_SHRED_SRC_TURBINE;
1074 :
1075 0 : long add_shred_timing = -fd_tickcount();
1076 0 : int rv = fd_fec_resolver_add_shred( ctx->resolver, shred, shred_buffer_sz, max_shred_idx, shred_source, slot_leader->uc, out_fec_set, out_shred, &ctx->out_merkle_roots[0], &spilled_fec );
1077 0 : add_shred_timing += fd_tickcount();
1078 :
1079 0 : fd_histf_sample( ctx->metrics->add_shred_timing, (ulong)add_shred_timing );
1080 0 : ctx->metrics->shred_processing_result[ rv + FD_FEC_RESOLVER_ADD_SHRED_RETVAL_OFF+FD_SHRED_ADD_SHRED_EXTRA_RETVAL_CNT ]++;
1081 :
1082 0 : if( FD_UNLIKELY( ctx->shred_out_idx!=ULONG_MAX && /* Only send to repair in full Firedancer */
1083 0 : spilled_fec.slot!=0 ) ) {
1084 : /* We've spilled an in-progress FEC set in the fec_resolver. We
1085 : need to let repair know to clear out it's cached info for that
1086 : fec set and re-repair those shreds. */
1087 0 : fd_fec_evicted_t * evicted_msg = (fd_fec_evicted_t *)fd_type_pun( fd_chunk_to_laddr( ctx->shred_out_mem, ctx->shred_out_chunk ) );
1088 0 : evicted_msg->slot = spilled_fec.slot;
1089 0 : evicted_msg->fec_set_idx = spilled_fec.fec_set_idx;
1090 0 : fd_memcpy( evicted_msg->merkle_root.uc, spilled_fec.merkle_root->hash, sizeof(fd_hash_t) );
1091 :
1092 0 : fd_stem_publish( stem, ctx->shred_out_idx, SHRED_SIG_FEC_EVICTED, ctx->shred_out_chunk, sizeof(fd_fec_evicted_t), 0, ctx->tsorig, fd_frag_meta_ts_comp( fd_tickcount() ) );
1093 0 : ctx->shred_out_chunk = fd_dcache_compact_next( ctx->shred_out_chunk, sizeof(fd_fec_evicted_t), ctx->shred_out_chunk0, ctx->shred_out_wmark );
1094 0 : }
1095 :
1096 0 : if( FD_LIKELY( ctx->shred_out_idx!=ULONG_MAX /* Only send to repair/replay in full Firedancer */
1097 0 : && ( ( rv==FD_FEC_RESOLVER_SHRED_OKAY )
1098 0 : | ( rv==FD_FEC_RESOLVER_SHRED_COMPLETES )
1099 0 : | ( rv==FD_FEC_RESOLVER_SHRED_DUPLICATE )
1100 0 : | ( rv==FD_FEC_RESOLVER_SHRED_EQUIVOC ) ) ) ) {
1101 :
1102 : /* Construct the sig from fec_resolver result and shred source. */
1103 :
1104 0 : ulong _sig = fd_disco_netmux_sig_proto( sig )==DST_PROTO_REPAIR
1105 0 : ? ( from_repair /*nonce_okay*/ ? SHRED_SIG_SRC_REPAIR : SHRED_SIG_SRC_BAD_REPAIR )
1106 0 : : ( SHRED_SIG_SRC_TURBINE );
1107 0 : _sig = ((ulong)rv << 32UL) | _sig;
1108 :
1109 : /* Copy the full shred into the frag and publish. */
1110 :
1111 0 : fd_shred_base_t * shred_msg = (fd_shred_base_t *)fd_chunk_to_laddr( ctx->shred_out_mem, ctx->shred_out_chunk );
1112 0 : memcpy( shred_msg->shred_, shred, fd_shred_sz( shred ) );
1113 0 : memcpy( &shred_msg->merkle_root, ctx->out_merkle_roots[0].hash, sizeof(fd_hash_t) );
1114 0 : if( FD_UNLIKELY( fd_disco_netmux_sig_proto( sig )==DST_PROTO_REPAIR ) ) { shred_msg->rnonce = nonce; }
1115 :
1116 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
1117 0 : fd_stem_publish( stem, ctx->shred_out_idx, _sig, ctx->shred_out_chunk, sizeof(fd_shred_base_t), 0UL, ctx->tsorig, tspub );
1118 0 : ctx->shred_out_chunk = fd_dcache_compact_next( ctx->shred_out_chunk, sizeof(fd_shred_base_t), ctx->shred_out_chunk0, ctx->shred_out_wmark );
1119 0 : }
1120 :
1121 0 : if( FD_LIKELY( fd_disco_netmux_sig_proto( sig ) != DST_PROTO_REPAIR &&
1122 0 : ( (rv==FD_FEC_RESOLVER_SHRED_OKAY) | (rv==FD_FEC_RESOLVER_SHRED_COMPLETES) ) ) ) {
1123 : /* Relay this shred */
1124 0 : ulong max_dest_cnt[1];
1125 0 : do {
1126 : /* If we've validated the shred and it COMPLETES but we can't
1127 : compute the destination for whatever reason, don't forward
1128 : the shred, but still send it to the blockstore. */
1129 0 : fd_shred_dest_t * sdest = fd_stake_ci_get_sdest_for_slot( ctx->stake_ci, shred->slot );
1130 0 : if( FD_UNLIKELY( !sdest ) ) break;
1131 0 : fd_shred_dest_idx_t * dests = fd_shred_dest_compute_children( sdest, &shred, 1UL, ctx->scratchpad_dests, 1UL, fanout, fanout, max_dest_cnt );
1132 0 : if( FD_UNLIKELY( !dests ) ) break;
1133 :
1134 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 );
1135 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 );
1136 0 : } while( 0 );
1137 0 : }
1138 :
1139 0 : if( FD_LIKELY( rv!=FD_FEC_RESOLVER_SHRED_COMPLETES ) ) return;
1140 :
1141 0 : FD_TEST( ctx->fec_sets <= *out_fec_set );
1142 0 : ctx->send_fec_set_idx[ 0UL ] = (ulong)(*out_fec_set - ctx->fec_sets);
1143 0 : ctx->send_fec_set_cnt = 1UL;
1144 0 : ctx->shredded_txn_cnt = 0UL;
1145 0 : }
1146 :
1147 0 : if( FD_UNLIKELY( ctx->send_fec_set_cnt==0UL ) ) return;
1148 :
1149 : /* Try to distribute shredded txn count across the fec sets.
1150 : This is an approximation, but it is acceptable. */
1151 0 : ulong shredded_txn_cnt_per_fec_set = ctx->shredded_txn_cnt / ctx->send_fec_set_cnt;
1152 0 : ulong shredded_txn_cnt_remain = ctx->shredded_txn_cnt - shredded_txn_cnt_per_fec_set * ctx->send_fec_set_cnt;
1153 0 : ulong shredded_txn_cnt_last_fec_set = shredded_txn_cnt_per_fec_set + shredded_txn_cnt_remain;
1154 :
1155 : /* If this shred completes a FEC set or is part of a microblock from
1156 : pack (ie. we're leader), we now have a full FEC set: so we notify
1157 : repair and insert into the blockstore, as well as retransmit. */
1158 :
1159 0 : for( ulong fset_k=0; fset_k<ctx->send_fec_set_cnt; fset_k++ ) {
1160 :
1161 0 : fd_fec_set_t * set = ctx->fec_sets + ctx->send_fec_set_idx[ fset_k ];
1162 :
1163 0 : fd_shred_t const * last = set->data_shreds[ FD_FEC_SHRED_CNT - 1 ].s;
1164 :
1165 : /* Broadcast locally produced and recovered shreds before any disk
1166 : work. */
1167 0 : do {
1168 0 : fd_shred_t const * new_shreds[ FD_REEDSOL_DATA_SHREDS_MAX+FD_REEDSOL_PARITY_SHREDS_MAX ];
1169 0 : ulong k=0UL;
1170 0 : for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ )
1171 0 : if( !(set->data_shred_rcvd & (1U<<i)) ) new_shreds[ k++ ] = set->data_shreds [ i ].s;
1172 0 : for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ )
1173 0 : if( !(set->parity_shred_rcvd & (1U<<i)) ) new_shreds[ k++ ] = set->parity_shreds[ i ].s;
1174 :
1175 0 : if( FD_UNLIKELY( !k ) ) break;
1176 0 : fd_shred_dest_t * sdest = fd_stake_ci_get_sdest_for_slot( ctx->stake_ci, new_shreds[ 0 ]->slot );
1177 0 : if( FD_UNLIKELY( !sdest ) ) break;
1178 :
1179 0 : ulong out_stride;
1180 0 : ulong max_dest_cnt[1];
1181 0 : fd_shred_dest_idx_t * dests;
1182 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
1183 0 : for( ulong i=0UL; i<k; i++ ) {
1184 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 );
1185 0 : }
1186 0 : out_stride = k;
1187 0 : dests = fd_shred_dest_compute_children( sdest, new_shreds, k, ctx->scratchpad_dests, k, fanout, fanout, max_dest_cnt );
1188 0 : } else {
1189 0 : for( ulong i=0UL; i<k; i++ ) {
1190 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 );
1191 0 : }
1192 0 : out_stride = 1UL;
1193 0 : *max_dest_cnt = 1UL;
1194 0 : dests = fd_shred_dest_compute_first( sdest, new_shreds, k, ctx->scratchpad_dests );
1195 0 : }
1196 0 : if( FD_UNLIKELY( !dests ) ) break;
1197 :
1198 0 : for( ulong i=0UL; i<k; i++ ) {
1199 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 );
1200 0 : }
1201 0 : } while( 0 );
1202 :
1203 : /* Compute merkle root and chained merkle root. */
1204 :
1205 0 : int replay_fwd = 1;
1206 0 : if( FD_LIKELY( ctx->store ) ) { /* firedancer-only */
1207 :
1208 0 : set->leader_bank = NULL; /* un-used by firedancer */
1209 :
1210 0 : fd_hash_t * mr = (fd_hash_t *)fd_type_pun( &ctx->out_merkle_roots[fset_k] );
1211 :
1212 0 : fd_store_fec_t * fec;
1213 0 : int insert_err = fd_store_insert( ctx->store, ctx->map_join, mr, &fec );
1214 0 : if( FD_UNLIKELY( insert_err==FD_MAP_ERR_KEY ) ) {
1215 0 : replay_fwd = 0;
1216 0 : } else {
1217 0 : FD_TEST( !insert_err && fec );
1218 :
1219 0 : fd_store_fec_spill_stats_t spill[1];
1220 0 : uchar * fec_data = fd_store_fec_data_acquire_ex( ctx->store, ctx->disk_fd, fec, spill );
1221 0 : if( FD_UNLIKELY( spill->write_cnt ) ) {
1222 0 : fd_histf_sample( ctx->metrics->fec_fallback_write_timing, spill->write_ticks );
1223 0 : ctx->metrics->fec_fallback_write_cnt += spill->write_cnt;
1224 0 : ctx->metrics->fec_fallback_write_bytes += spill->write_bytes;
1225 0 : }
1226 0 : if( FD_UNLIKELY( !fec_data ) )
1227 0 : FD_LOG_CRIT(( "Store could not allocate a FEC payload" ));
1228 0 : for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) {
1229 0 : fd_shred_t * data_shred = set->data_shreds[i].s;
1230 0 : ulong payload_sz = fd_shred_payload_sz( data_shred );
1231 0 : if( FD_UNLIKELY( fec->data_sz + payload_sz > ctx->store->fec_data_max ) ) {
1232 :
1233 0 : FD_LOG_CRIT(( "Shred tile %lu: completed FEC set %lu %u data_sz: %lu exceeds data_max: %lu", ctx->round_robin_id, data_shred->slot, data_shred->fec_set_idx, fec->data_sz + payload_sz, ctx->store->fec_data_max ));
1234 0 : }
1235 0 : fd_memcpy_nt_nofence( fec_data + fec->data_sz, fd_shred_data_payload( data_shred ), payload_sz );
1236 0 : fec->data_sz += payload_sz;
1237 0 : if( FD_LIKELY( i<32UL ) ) fec->shred_offs[ i ] = (uint)payload_sz + (i==0UL ? 0U : fec->shred_offs[ i-1UL ]);
1238 0 : }
1239 0 : _mm_sfence();
1240 0 : fd_store_fec_data_publish( ctx->store, fec );
1241 0 : }
1242 0 : }
1243 :
1244 0 : if( FD_LIKELY( ctx->shred_out_idx!=ULONG_MAX && replay_fwd ) ) { /* firedancer-only */
1245 :
1246 : /* Send all of the data shreds we recovered (weren't received).
1247 : The chunks are all written before any is published, so the
1248 : link burst must cover them or a chunk could be reused while
1249 : an unreliable consumer still sees its old seq. */
1250 0 : FD_STATIC_ASSERT( FD_SHRED_STEM_BURST>=32UL, shred_out_burst );
1251 0 : ulong missing_chunk[ 32 ];
1252 0 : ulong missing_cnt = 0UL;
1253 0 : for( int i=0; i<32; i++ ) {
1254 0 : if( fd_uint_extract_bit( set->data_shred_rcvd, i )==0 ) {
1255 0 : fd_shred_t * const missing = &set->data_shreds[ i ].s[0];
1256 :
1257 0 : fd_shred_base_t * shred_msg = (fd_shred_base_t *)fd_chunk_to_laddr( ctx->shred_out_mem, ctx->shred_out_chunk );
1258 0 : fd_memcpy_nt_nofence( shred_msg->shred_, missing, fd_shred_sz( missing ) );
1259 0 : memcpy( &shred_msg->merkle_root, ctx->out_merkle_roots[fset_k].hash, sizeof(fd_hash_t) );
1260 :
1261 0 : missing_chunk[ missing_cnt++ ] = ctx->shred_out_chunk;
1262 0 : ctx->shred_out_chunk = fd_dcache_compact_next( ctx->shred_out_chunk, sizeof(fd_shred_base_t), ctx->shred_out_chunk0, ctx->shred_out_wmark );
1263 0 : }
1264 0 : }
1265 :
1266 0 : if( FD_LIKELY( missing_cnt ) ) {
1267 0 : _mm_sfence();
1268 0 : ulong sig = ((ulong)FD_FEC_RESOLVER_SHRED_COMPLETES << 32UL) | SHRED_SIG_SRC_RECONSTRUCTED;
1269 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
1270 0 : for( ulong i=0UL; i<missing_cnt; i++ ) fd_stem_publish( stem, ctx->shred_out_idx, sig, missing_chunk[ i ], sizeof(fd_shred_base_t), 0UL, ctx->tsorig, tspub );
1271 0 : }
1272 :
1273 : /* Replay requires the FEC payload before this notification.
1274 : Wire-shred persistence on rserve is asynchronous.
1275 :
1276 : This message, the shred msg, and the FEC evict msg constitute
1277 : the max 3 possible messages to repair/replay per after_frag.
1278 : In reality, it is only possible to publish all 3 in the case
1279 : where we receive a coding shred first for a FEC set where
1280 : (N=1,K=18), which allows for the FEC set to be instantly
1281 : completed by the singular coding shred, and that also happens
1282 : to evict a FEC set from the curr_map. When fix-32 arrives, the
1283 : link burst value can be lowered to 2. */
1284 0 : ulong sig = ctx->in_kind[ in_idx ]==IN_KIND_POH ? SHRED_SIG_FEC_COMPLETE_LEADER : SHRED_SIG_FEC_COMPLETE;
1285 :
1286 0 : fd_fec_complete_t * complete_msg = fd_chunk_to_laddr( ctx->shred_out_mem, ctx->shred_out_chunk );
1287 0 : complete_msg->last_shred_hdr = *last;
1288 0 : memcpy( &complete_msg->merkle_root, ctx->out_merkle_roots[fset_k].hash, sizeof(fd_hash_t) );
1289 0 : complete_msg->chained_merkle_root = *(fd_hash_t *)fd_type_pun((uchar *)last + fd_shred_chain_off( last->variant ));
1290 0 : if( FD_LIKELY( sig==SHRED_SIG_FEC_COMPLETE ) ) {
1291 0 : complete_msg->turbine_shred_cnt = (ulong)fd_ulong_popcnt( set->turbine_shred_rcvd );
1292 0 : complete_msg->repair_shred_cnt = (ulong)fd_ulong_popcnt( set->repair_shred_rcvd );
1293 0 : complete_msg->reconstructed_shred_cnt = 2U*(uint)FD_FEC_SHRED_CNT-(uint)(fd_uint_popcnt( set->data_shred_rcvd ) + fd_uint_popcnt( set->parity_shred_rcvd ));
1294 0 : } else {
1295 0 : complete_msg->turbine_shred_cnt = 0U;
1296 0 : complete_msg->repair_shred_cnt = 0U;
1297 0 : complete_msg->reconstructed_shred_cnt = 0U;
1298 0 : }
1299 :
1300 0 : fd_stem_publish( stem, ctx->shred_out_idx, sig, ctx->shred_out_chunk, sizeof(fd_fec_complete_t), 0UL, ctx->tsorig, fd_frag_meta_ts_comp( fd_tickcount() ) );
1301 0 : ctx->shred_out_chunk = fd_dcache_compact_next( ctx->shred_out_chunk, sizeof(fd_fec_complete_t), ctx->shred_out_chunk0, ctx->shred_out_wmark );
1302 :
1303 0 : } else if( FD_UNLIKELY( ctx->store_out_idx != ULONG_MAX ) ) { /* frankendancer-only */
1304 :
1305 : /* Send to the blockstore */
1306 :
1307 0 : ulong txn_cnt = fd_ulong_if( fset_k==ctx->send_fec_set_cnt-1UL, shredded_txn_cnt_last_fec_set, shredded_txn_cnt_per_fec_set );
1308 : /* If the low 32 bits of sig are 0, the store tile will do extra
1309 : checks */
1310 0 : ulong new_sig = txn_cnt<<32 | (ulong)(ctx->in_kind[ in_idx ]!=IN_KIND_NET);
1311 :
1312 : /* Attach the leader bank pointer and merkle root so that the
1313 : store tile can set the block_id for the slot. Network
1314 : FEC sets have no leader bank. */
1315 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH ) ) {
1316 0 : set->leader_bank = ctx->leader_bank;
1317 0 : memcpy( set->merkle_root, ctx->out_merkle_roots[fset_k].hash, 32UL );
1318 0 : } else {
1319 0 : set->leader_bank = NULL;
1320 0 : }
1321 :
1322 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
1323 : /* The size is actually slightly larger than USHORT_MAX, but the store tile
1324 : knows to use sizeof(fd_fec_set_t) instead of the sz field. Put
1325 : USHORT_MAX so that monitoring tools are at least close. */
1326 0 : ulong sz = fd_ulong_min( sizeof(fd_fec_set_t), USHORT_MAX );
1327 0 : fd_stem_publish( stem, 0UL, new_sig, fd_laddr_to_chunk( ctx->store_out_mem, set ), sz, 0UL, ctx->tsorig, tspub );
1328 :
1329 : /* Store tile will release the bank pointer when it sees
1330 : SLOT_COMPLETE FEC set. So we reset our tracking. */
1331 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH &&
1332 0 : (set->data_shreds[ FD_FEC_SHRED_CNT-1UL ].s->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE) ) ) {
1333 0 : ctx->leader_bank = NULL;
1334 0 : }
1335 0 : }
1336 0 : }
1337 0 : }
1338 :
1339 : static void
1340 : privileged_init( fd_topo_t const * topo,
1341 0 : fd_topo_tile_t const * tile ) {
1342 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1343 0 : FD_TEST( scratch!=NULL );
1344 :
1345 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1346 0 : fd_shred_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_shred_ctx_t ), sizeof( fd_shred_ctx_t ) );
1347 :
1348 0 : if( FD_UNLIKELY( !strcmp( tile->shred.identity_key_path, "" ) ) )
1349 0 : FD_LOG_ERR(( "identity_key_path not set" ));
1350 :
1351 0 : ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->shred.identity_key_path, /* pubkey only: */ 1 ) );
1352 :
1353 0 : if( FD_UNLIKELY( !fd_rng_secure( &(ctx->resolver_seed), sizeof(ulong) ) ) ) {
1354 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
1355 0 : }
1356 0 : if( FD_UNLIKELY( !fd_rng_secure( &(ctx->shred_dest_seed), sizeof(ulong) ) ) ) {
1357 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
1358 0 : }
1359 : /* This is only needed in frankendancer, but we'll overwrite it with
1360 : the value the repair tile generated in full firedancer. */
1361 0 : if( FD_UNLIKELY( !fd_rng_secure( ctx->repair_nonce_ss->bytes, sizeof(fd_rnonce_ss_t) ) ) ) {
1362 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
1363 0 : }
1364 :
1365 0 : ctx->disk_fd = -1;
1366 0 : ulong store_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "store" );
1367 0 : if( FD_LIKELY( store_obj_id!=ULONG_MAX ) ) {
1368 0 : fd_store_t * store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
1369 0 : FD_TEST( store );
1370 0 : ctx->disk_fd = FD_STORE_FD_RW;
1371 0 : if( FD_UNLIKELY( fcntl( ctx->disk_fd, F_GETFD )<0 ) )
1372 0 : FD_LOG_ERR(( "store file descriptor was not inherited (%i-%s)", errno, fd_io_strerror( errno ) ));
1373 0 : }
1374 :
1375 0 : }
1376 :
1377 : static void
1378 : fd_shred_signer( void * signer_ctx,
1379 : uchar signature[ static 64 ],
1380 0 : uchar const merkle_root[ static 32 ] ) {
1381 0 : fd_keyguard_client_sign( signer_ctx, signature, merkle_root, 32UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
1382 0 : }
1383 :
1384 : static void
1385 : unprivileged_init( fd_topo_t const * topo,
1386 0 : fd_topo_tile_t const * tile ) {
1387 :
1388 0 : FD_TEST( 0==strcmp( topo->links[tile->out_link_id[ NET_OUT_IDX ]].name, "shred_net" ) );
1389 0 : FD_TEST( 0==strcmp( topo->links[tile->out_link_id[ SIGN_OUT_IDX ]].name, "shred_sign" ) );
1390 :
1391 0 : if( FD_UNLIKELY( !tile->out_cnt ) )
1392 0 : FD_LOG_ERR(( "shred tile has no primary output link" ));
1393 :
1394 0 : ulong fec_exposure = tile->shred.fec_exposure;
1395 0 : if( FD_UNLIKELY( !fec_exposure ) ) FD_LOG_ERR(( "fec_exposure not set" ));
1396 :
1397 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1398 0 : FD_TEST( scratch!=NULL );
1399 :
1400 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1401 0 : fd_shred_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_shred_ctx_t ), sizeof( fd_shred_ctx_t ) );
1402 :
1403 0 : FD_CHECK_ERR( tile->in_cnt<=IN_LINK_MAX, "too many input links" );
1404 :
1405 0 : ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
1406 0 : ctx->round_robin_id = tile->kind_id;
1407 0 : ctx->batch_cnt = 0UL;
1408 0 : ctx->slot = ULONG_MAX;
1409 0 : ctx->store = NULL;
1410 :
1411 0 : ulong store_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "store" );
1412 0 : if( FD_LIKELY( store_obj_id!=ULONG_MAX ) ) {
1413 0 : ctx->store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
1414 0 : FD_TEST( ctx->store->magic==FD_STORE_MAGIC );
1415 0 : FD_TEST( fd_store_map_ljoin( ctx->store, ctx->map_join ) );
1416 0 : }
1417 : /* With rserve disabled, shred:0 remains responsible for punching
1418 : reclaimed spill pages, and will charge busy. */
1419 0 : ctx->store_maintenance = !!ctx->store && !ctx->round_robin_id &&
1420 0 : fd_topo_find_tile( topo, "rserve", 0UL )==ULONG_MAX;
1421 :
1422 : /* If the default partial_depth is ever changed, correspondingly
1423 : change the size of the fd_fec_intra_pool in fd_fec_repair. */
1424 0 : ulong fec_resolver_footprint = fd_fec_resolver_footprint( tile->shred.fec_resolver_depth, 1UL, fec_exposure+1UL,
1425 0 : 128UL * tile->shred.fec_resolver_depth );
1426 0 : ulong fec_set_cnt = fd_shred_tile_fec_set_cnt( fec_exposure, tile->shred.fec_resolver_depth );
1427 0 : ulong fec_sets_required_sz = fec_set_cnt*sizeof(fd_fec_set_t);
1428 :
1429 0 : void * fec_sets_shmem = NULL;
1430 0 : ctx->shred_out_idx = fd_topo_find_tile_out_link( topo, tile, "shred_out", ctx->round_robin_id );
1431 0 : ctx->store_out_idx = fd_topo_find_tile_out_link( topo, tile, "shred_store", ctx->round_robin_id );
1432 0 : if( FD_LIKELY( ctx->shred_out_idx!=ULONG_MAX ) ) { /* firedancer-only */
1433 0 : FD_TEST( fec_exposure==FD_SHRED_FIREDANCER_FEC_EXPOSURE );
1434 0 : FD_TEST( ctx->store_out_idx==ULONG_MAX );
1435 0 : fd_topo_link_t const * shred_out = &topo->links[ tile->out_link_id[ ctx->shred_out_idx ] ];
1436 0 : ctx->shred_out_mem = topo->workspaces[ topo->objs[ shred_out->dcache_obj_id ].wksp_id ].wksp;
1437 0 : ctx->shred_out_chunk0 = fd_dcache_compact_chunk0( ctx->shred_out_mem, shred_out->dcache );
1438 0 : ctx->shred_out_wmark = fd_dcache_compact_wmark ( ctx->shred_out_mem, shred_out->dcache, shred_out->mtu );
1439 0 : ctx->shred_out_chunk = ctx->shred_out_chunk0;
1440 0 : FD_TEST( fd_dcache_compact_is_safe( ctx->shred_out_mem, shred_out->dcache, shred_out->mtu, shred_out->depth ) );
1441 0 : ulong fec_sets_required_cnt = fec_set_cnt*ctx->round_robin_cnt;
1442 0 : if( FD_LIKELY( ctx->store ) ) {
1443 0 : FD_TEST( ctx->store->fec_set_cnt>=fec_sets_required_cnt );
1444 0 : fd_fec_set_t * store_fec_sets = fd_store_fec_sets( ctx->store );
1445 0 : FD_TEST( store_fec_sets );
1446 :
1447 0 : fec_sets_shmem = store_fec_sets + ctx->round_robin_id*fec_set_cnt;
1448 0 : } else {
1449 0 : ulong fec_sets_obj_id = fd_pod_query_ulong( topo->props, "fec_sets", ULONG_MAX );
1450 0 : FD_TEST( fec_sets_obj_id!=ULONG_MAX );
1451 :
1452 0 : ulong fec_sets_sz = fd_pod_queryf_ulong( topo->props, 0UL, "obj.%lu.sz", fec_sets_obj_id );
1453 0 : FD_TEST( fec_sets_sz>=fec_sets_required_cnt*sizeof(fd_fec_set_t) );
1454 :
1455 0 : fd_fec_set_t * fec_sets = fd_topo_obj_laddr( topo, fec_sets_obj_id );
1456 0 : FD_TEST( fec_sets );
1457 :
1458 0 : fec_sets_shmem = fec_sets + ctx->round_robin_id*fec_set_cnt;
1459 0 : }
1460 :
1461 0 : ulong rnonce_ss_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "rnonce_ss" );
1462 0 : FD_TEST( rnonce_ss_id!=ULONG_MAX );
1463 0 : memcpy( ctx->repair_nonce_ss, fd_topo_obj_laddr( topo, rnonce_ss_id ), sizeof(fd_rnonce_ss_t) );
1464 :
1465 0 : } else if ( FD_LIKELY( ctx->store_out_idx!=ULONG_MAX ) ) { /* frankendancer-only */
1466 0 : FD_TEST( 0==strcmp( topo->links[tile->out_link_id[ ctx->store_out_idx ]].name, "shred_store" ) );
1467 0 : FD_TEST( fec_exposure==topo->links[ tile->out_link_id[ ctx->store_out_idx ] ].depth );
1468 0 : fec_sets_shmem = topo->links[ tile->out_link_id[ ctx->store_out_idx ] ].dcache;
1469 0 : if( FD_UNLIKELY( fd_dcache_data_sz( fec_sets_shmem )<fec_sets_required_sz ) ) {
1470 0 : FD_LOG_ERR(( "shred_store dcache too small. It is %lu bytes but must be at least %lu bytes. ",
1471 0 : fd_dcache_data_sz( fec_sets_shmem ),
1472 0 : fec_sets_required_sz ));
1473 0 : }
1474 0 : }
1475 0 : if( FD_UNLIKELY( !fec_sets_shmem ) ) FD_LOG_ERR(( "missing FEC-set backing memory" ));
1476 :
1477 0 : if( FD_UNLIKELY( !tile->shred.fec_resolver_depth ) ) FD_LOG_ERR(( "fec_resolver_depth not set" ));
1478 0 : if( FD_UNLIKELY( !tile->shred.shred_listen_port ) ) FD_LOG_ERR(( "shred_listen_port not set" ));
1479 :
1480 0 : void * _stake_ci = FD_SCRATCH_ALLOC_APPEND( l, fd_stake_ci_align(), fd_stake_ci_footprint() );
1481 0 : void * _resolver = FD_SCRATCH_ALLOC_APPEND( l, fd_fec_resolver_align(), fec_resolver_footprint );
1482 0 : void * _shredder = FD_SCRATCH_ALLOC_APPEND( l, fd_shredder_align(), fd_shredder_footprint() );
1483 :
1484 0 : fd_fec_set_t * fec_sets = (fd_fec_set_t *)fec_sets_shmem;
1485 :
1486 0 : #define NONNULL( x ) (__extension__({ \
1487 0 : __typeof__((x)) __x = (x); \
1488 0 : if( FD_UNLIKELY( !__x ) ) FD_LOG_ERR(( #x " was unexpectedly NULL" )); \
1489 0 : __x; }))
1490 :
1491 0 : int has_ipecho_in = fd_topo_find_tile_in_link( topo, tile, "ipecho_out", 0UL )!=ULONG_MAX;
1492 0 : ushort expected_shred_version = tile->shred.expected_shred_version;
1493 0 : if( FD_UNLIKELY( !has_ipecho_in && !expected_shred_version ) ) {
1494 0 : ulong busy_obj_id = fd_pod_query_ulong( topo->props, "pohh_shred", ULONG_MAX );
1495 0 : FD_TEST( busy_obj_id!=ULONG_MAX );
1496 0 : ulong * gossip_shred_version = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
1497 0 : FD_LOG_INFO(( "Waiting for shred version to be determined via gossip." ));
1498 0 : ulong _expected_shred_version = ULONG_MAX;
1499 0 : do {
1500 0 : _expected_shred_version = FD_VOLATILE_CONST( *gossip_shred_version );
1501 0 : } while( _expected_shred_version==ULONG_MAX );
1502 :
1503 0 : if( FD_UNLIKELY( _expected_shred_version>USHORT_MAX ) ) FD_LOG_ERR(( "invalid shred version %lu", _expected_shred_version ));
1504 0 : FD_LOG_INFO(( "Using shred version %hu", (ushort)_expected_shred_version ));
1505 0 : expected_shred_version = (ushort)_expected_shred_version;
1506 0 : }
1507 :
1508 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
1509 0 : FD_TEST( ctx->keyswitch );
1510 :
1511 : /* populate ctx */
1512 0 : ulong sign_in_idx = fd_topo_find_tile_in_link( topo, tile, "sign_shred", tile->kind_id );
1513 0 : FD_TEST( sign_in_idx!=ULONG_MAX );
1514 0 : fd_topo_link_t const * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
1515 0 : fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ SIGN_OUT_IDX ] ];
1516 0 : NONNULL( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
1517 0 : sign_out->mcache,
1518 0 : sign_out->dcache,
1519 0 : sign_in->mcache,
1520 0 : sign_in->dcache,
1521 0 : sign_out->mtu,
1522 0 : sign_in->mtu ) ) );
1523 :
1524 0 : ctx->bench_max_shred_idx = tile->shred.bench_max_shreds_per_block;
1525 0 : ulong shred_limit = tile->shred.max_shreds_per_block;
1526 0 : ctx->shred_limit = shred_limit;
1527 0 : fd_fec_set_t * resolver_sets = fec_sets + fec_exposure + FD_SHRED_BATCH_FEC_SETS_MAX;
1528 0 : ctx->shredder = NONNULL( fd_shredder_join ( fd_shredder_new ( _shredder, fd_shred_signer, ctx->keyguard_client ) ) );
1529 0 : ctx->resolver = NONNULL( fd_fec_resolver_join ( fd_fec_resolver_new ( _resolver,
1530 0 : fd_shred_signer, ctx->keyguard_client,
1531 0 : tile->shred.fec_resolver_depth, 1UL,
1532 0 : fec_exposure+1UL,
1533 0 : 128UL * tile->shred.fec_resolver_depth, resolver_sets,
1534 0 : ctx->resolver_seed ) ) );
1535 :
1536 0 : if( FD_LIKELY( !!expected_shred_version ) ) {
1537 0 : fd_shredder_set_shred_version ( ctx->shredder, expected_shred_version );
1538 0 : fd_fec_resolver_set_shred_version( ctx->resolver, expected_shred_version );
1539 0 : }
1540 :
1541 0 : ctx->fec_sets = fec_sets;
1542 :
1543 0 : ctx->stake_ci = fd_stake_ci_join( fd_stake_ci_new( _stake_ci, ctx->identity_key, ctx->shred_dest_seed ) );
1544 :
1545 0 : ctx->net_id = (ushort)0;
1546 :
1547 0 : fd_ip4_udp_hdr_init( ctx->data_shred_net_hdr, FD_SHRED_MIN_SZ, 0, tile->shred.shred_listen_port );
1548 0 : fd_ip4_udp_hdr_init( ctx->parity_shred_net_hdr, FD_SHRED_MAX_SZ, 0, tile->shred.shred_listen_port );
1549 :
1550 0 : ctx->adtl_dests_retransmit_cnt = tile->shred.adtl_dests_retransmit_cnt;
1551 0 : for( ulong i=0UL; i<ctx->adtl_dests_retransmit_cnt; i++) {
1552 0 : ctx->adtl_dests_retransmit[ i ].ip4 = tile->shred.adtl_dests_retransmit[ i ].ip;
1553 0 : ctx->adtl_dests_retransmit[ i ].port = tile->shred.adtl_dests_retransmit[ i ].port;
1554 0 : }
1555 0 : ctx->adtl_dests_leader_cnt = tile->shred.adtl_dests_leader_cnt;
1556 0 : for( ulong i=0UL; i<ctx->adtl_dests_leader_cnt; i++) {
1557 0 : ctx->adtl_dests_leader[i].ip4 = tile->shred.adtl_dests_leader[i].ip;
1558 0 : ctx->adtl_dests_leader[i].port = tile->shred.adtl_dests_leader[i].port;
1559 0 : }
1560 :
1561 0 : uchar has_contact_info_in = 0;
1562 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
1563 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
1564 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
1565 :
1566 0 : if( FD_LIKELY( !strcmp( link->name, "net_shred" ) ) ) {
1567 0 : ctx->in_kind[ i ] = IN_KIND_NET;
1568 0 : fd_net_rx_bounds_init( &ctx->in[ i ].net_rx, link->dcache );
1569 0 : continue; /* only net_rx needs to be set in this case. */
1570 0 : }
1571 0 : else if( FD_LIKELY( !strcmp( link->name, "poh_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH; /* Firedancer */
1572 0 : else if( FD_LIKELY( !strcmp( link->name, "pohh_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH; /* Frankendancer */
1573 0 : else if( FD_LIKELY( !strcmp( link->name, "stake_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_STAKE; /* Frankendancer */
1574 0 : else if( FD_LIKELY( !strcmp( link->name, "replay_epoch" ) ) ) ctx->in_kind[ i ] = IN_KIND_EPOCH; /* Firedancer */
1575 0 : else if( FD_LIKELY( !strcmp( link->name, "sign_shred" ) ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
1576 0 : else if( FD_LIKELY( !strcmp( link->name, "ipecho_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_IPECHO;
1577 0 : else if( FD_LIKELY( !strcmp( link->name, "tower_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_ROOTED;
1578 0 : else if( FD_LIKELY( !strcmp( link->name, "replay_resol" ) ) ) ctx->in_kind[ i ] = IN_KIND_ROOTEDH;
1579 0 : else if( FD_LIKELY( !strcmp( link->name, "votor_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_ROOTEDV;
1580 0 : else if( FD_LIKELY( !strcmp( link->name, "crds_shred" ) ) ) { ctx->in_kind[ i ] = IN_KIND_CONTACT;
1581 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" ));
1582 0 : has_contact_info_in = 1;
1583 0 : }
1584 0 : else if( FD_LIKELY( !strcmp( link->name, "gossip_out" ) ) ) { ctx->in_kind[ i ] = IN_KIND_GOSSIP;
1585 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" ));
1586 0 : has_contact_info_in = 1;
1587 0 : }
1588 :
1589 0 : else FD_LOG_ERR(( "shred tile has unexpected input link %lu %s", i, link->name ));
1590 :
1591 0 : if( FD_LIKELY( !!link->mtu ) ) {
1592 0 : ctx->in[ i ].mem = link_wksp->wksp;
1593 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
1594 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
1595 0 : }
1596 0 : }
1597 :
1598 0 : fd_topo_link_t const * net_out = &topo->links[ tile->out_link_id[ NET_OUT_IDX ] ];
1599 :
1600 0 : ctx->net_out_chunk0 = fd_dcache_compact_chunk0( fd_wksp_containing( net_out->dcache ), net_out->dcache );
1601 0 : ctx->net_out_mem = topo->workspaces[ topo->objs[ net_out->dcache_obj_id ].wksp_id ].wksp;
1602 0 : ctx->net_out_wmark = fd_dcache_compact_wmark ( ctx->net_out_mem, net_out->dcache, net_out->mtu );
1603 0 : ctx->net_out_chunk = ctx->net_out_chunk0;
1604 :
1605 0 : if( FD_LIKELY( ctx->shred_out_idx!=ULONG_MAX ) ) { /* firedancer-only */
1606 0 : fd_topo_link_t const * shred_out = &topo->links[ tile->out_link_id[ ctx->shred_out_idx ] ];
1607 0 : ctx->shred_out_mem = topo->workspaces[ topo->objs[ shred_out->dcache_obj_id ].wksp_id ].wksp;
1608 0 : ctx->shred_out_chunk0 = fd_dcache_compact_chunk0( ctx->shred_out_mem, shred_out->dcache );
1609 0 : ctx->shred_out_wmark = fd_dcache_compact_wmark ( ctx->shred_out_mem, shred_out->dcache, shred_out->mtu );
1610 0 : ctx->shred_out_chunk = ctx->shred_out_chunk0;
1611 0 : FD_TEST( fd_dcache_compact_is_safe( ctx->shred_out_mem, shred_out->dcache, shred_out->mtu, shred_out->depth ) );
1612 : /* An unreliable consumer ignores credits, so the dcache must hold
1613 : a full STEM_BURST beyond the mcache depth. */
1614 0 : FD_TEST( shred_out->burst>=FD_SHRED_STEM_BURST );
1615 0 : }
1616 :
1617 0 : if( FD_LIKELY( ctx->store_out_idx!=ULONG_MAX ) ) { /* frankendancer-only */
1618 0 : fd_topo_link_t const * store_out = &topo->links[ tile->out_link_id[ ctx->store_out_idx ] ];
1619 0 : ctx->store_out_mem = topo->workspaces[ topo->objs[ store_out->dcache_obj_id ].wksp_id ].wksp;
1620 0 : ctx->store_out_chunk0 = fd_dcache_compact_chunk0( ctx->store_out_mem, store_out->dcache );
1621 0 : ctx->store_out_wmark = fd_dcache_compact_wmark ( ctx->store_out_mem, store_out->dcache, store_out->mtu );
1622 0 : ctx->store_out_chunk = ctx->store_out_chunk0;
1623 0 : FD_TEST( fd_dcache_compact_is_safe( ctx->store_out_mem, store_out->dcache, store_out->mtu, store_out->depth ) );
1624 0 : }
1625 :
1626 0 : ctx->poh_in_expect_seq = 0UL;
1627 :
1628 0 : ctx->shredder_fec_set_idx = 0UL;
1629 0 : ctx->shredder_max_fec_set_idx = fec_exposure + FD_SHRED_BATCH_FEC_SETS_MAX;
1630 :
1631 0 : ctx->chained_merkle_root = NULL;
1632 0 : memset( ctx->out_merkle_roots, 0, sizeof(ctx->out_merkle_roots) );
1633 :
1634 0 : for( ulong i=0UL; i<FD_SHRED_BATCH_FEC_SETS_MAX; i++ ) { ctx->send_fec_set_idx[ i ] = ULONG_MAX; }
1635 0 : ctx->send_fec_set_cnt = 0UL;
1636 :
1637 0 : ctx->shred_buffer_sz = 0UL;
1638 0 : memset( ctx->shred_buffer, 0xFF, FD_NET_MTU );
1639 :
1640 0 : ctx->leader_bank = NULL;
1641 :
1642 0 : fd_histf_join( fd_histf_new( ctx->metrics->contact_info_cnt, FD_MHIST_MIN( SHRED, CONTACT_INFO_PER_MESSAGE ),
1643 0 : FD_MHIST_MAX( SHRED, CONTACT_INFO_PER_MESSAGE ) ) );
1644 0 : fd_histf_join( fd_histf_new( ctx->metrics->batch_sz, FD_MHIST_MIN( SHRED, BATCH_SIZE_BYTES ),
1645 0 : FD_MHIST_MAX( SHRED, BATCH_SIZE_BYTES ) ) );
1646 0 : fd_histf_join( fd_histf_new( ctx->metrics->batch_microblock_cnt, FD_MHIST_MIN( SHRED, MICROBLOCK_PER_BATCH ),
1647 0 : FD_MHIST_MAX( SHRED, MICROBLOCK_PER_BATCH ) ) );
1648 0 : fd_histf_join( fd_histf_new( ctx->metrics->shredding_timing, FD_MHIST_SECONDS_MIN( SHRED, SHREDDING_DURATION_SECONDS ),
1649 0 : FD_MHIST_SECONDS_MAX( SHRED, SHREDDING_DURATION_SECONDS ) ) );
1650 0 : fd_histf_join( fd_histf_new( ctx->metrics->add_shred_timing, FD_MHIST_SECONDS_MIN( SHRED, ADD_SHRED_DURATION_SECONDS ),
1651 0 : FD_MHIST_SECONDS_MAX( SHRED, ADD_SHRED_DURATION_SECONDS ) ) );
1652 0 : fd_histf_join( fd_histf_new( ctx->metrics->fec_fallback_write_timing,
1653 0 : FD_MHIST_SECONDS_MIN( SHRED, FEC_FALLBACK_WRITE_SECONDS ),
1654 0 : FD_MHIST_SECONDS_MAX( SHRED, FEC_FALLBACK_WRITE_SECONDS ) ) );
1655 0 : memset( ctx->metrics->shred_processing_result, '\0', sizeof(ctx->metrics->shred_processing_result) );
1656 0 : ctx->metrics->invalid_block_id_cnt = 0UL;
1657 0 : ctx->metrics->shred_rejected_unchained_cnt = 0UL;
1658 0 : ctx->metrics->repair_rcv_cnt = 0UL;
1659 0 : ctx->metrics->repair_rcv_bytes = 0UL;
1660 0 : ctx->metrics->turbine_rcv_cnt = 0UL;
1661 0 : ctx->metrics->turbine_rcv_bytes = 0UL;
1662 0 : ctx->metrics->bad_nonce = 0UL;
1663 0 : ctx->metrics->fec_fallback_write_cnt = 0UL;
1664 0 : ctx->metrics->fec_fallback_write_bytes = 0UL;
1665 :
1666 0 : ctx->pending_batch.microblock_cnt = 0UL;
1667 0 : ctx->pending_batch.txn_cnt = 0UL;
1668 0 : ctx->pending_batch.pos = 0UL;
1669 0 : ctx->pending_batch.slot = 0UL;
1670 0 : memset( ctx->pending_batch.payload, 0, sizeof(ctx->pending_batch.payload) );
1671 :
1672 0 : memset( ctx->epoch_schedule, 0, sizeof(ctx->epoch_schedule) );
1673 0 : for( ulong i=0UL; i<FD_SHRED_FEATURES_ACTIVATION_SLOT_CNT; i++ ) {
1674 0 : ctx->features_activation->slots[i] = FD_SHRED_FEATURES_ACTIVATION_SLOT_DISABLED;
1675 0 : }
1676 0 : ctx->prev_max_shred_idx = ctx->shred_limit;
1677 0 : ctx->current_max_shred_idx = ctx->shred_limit;
1678 0 : ctx->next_max_shred_idx = ctx->shred_limit;
1679 0 : ctx->current_max_shred_idx_start_slot = 0UL;
1680 0 : ctx->next_max_shred_idx_start_slot = ULONG_MAX;
1681 :
1682 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
1683 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
1684 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
1685 :
1686 0 : memset( ctx->block_ids, 0, sizeof(ctx->block_ids) );
1687 0 : }
1688 :
1689 : static ulong
1690 : populate_allowed_seccomp( fd_topo_t const * topo,
1691 : fd_topo_tile_t const * tile,
1692 : ulong out_cnt,
1693 0 : struct sock_filter * out ) {
1694 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1695 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1696 0 : fd_shred_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_shred_ctx_t ), sizeof( fd_shred_ctx_t ) );
1697 :
1698 0 : populate_sock_filter_policy_fd_shred_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)ctx->disk_fd );
1699 0 : return sock_filter_policy_fd_shred_tile_instr_cnt;
1700 0 : }
1701 :
1702 : static ulong
1703 : populate_allowed_fds( fd_topo_t const * topo,
1704 : fd_topo_tile_t const * tile,
1705 : ulong out_fds_cnt,
1706 0 : int * out_fds ) {
1707 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1708 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1709 0 : fd_shred_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_shred_ctx_t ), sizeof( fd_shred_ctx_t ) );
1710 :
1711 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
1712 :
1713 0 : ulong out_cnt = 0UL;
1714 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
1715 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
1716 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
1717 0 : if( FD_LIKELY( ctx->disk_fd>=0 ) )
1718 0 : out_fds[ out_cnt++ ] = ctx->disk_fd;
1719 0 : return out_cnt;
1720 0 : }
1721 :
1722 : /* A leader batch can publish 32 data-shred headers and one completion
1723 : per FEC set to shred_out. Network input publishes at most one such
1724 : FEC set, and Frankendancer publishes fewer reliable fragments. */
1725 0 : #define STEM_BURST FD_SHRED_STEM_BURST
1726 :
1727 : /* See explanation in fd_pack */
1728 0 : #define STEM_LAZY (128L*3000L)
1729 :
1730 : /* When leader, poh_shred carries a stream of one microblock per frag
1731 : and the other links are near empty; keep draining it. */
1732 0 : #define STEM_STICKY_POLL_MAX (16UL)
1733 :
1734 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_shred_ctx_t
1735 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_shred_ctx_t)
1736 :
1737 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
1738 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
1739 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
1740 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
1741 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
1742 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
1743 :
1744 : #include "../stem/fd_stem.c"
1745 :
1746 : fd_topo_run_tile_t fd_tile_shred = {
1747 : .name = "shred",
1748 : .populate_allowed_seccomp = populate_allowed_seccomp,
1749 : .populate_allowed_fds = populate_allowed_fds,
1750 : .scratch_align = scratch_align,
1751 : .scratch_footprint = scratch_footprint,
1752 : .privileged_init = privileged_init,
1753 : .unprivileged_init = unprivileged_init,
1754 : .run = stem_run,
1755 : };
|