LCOV - code coverage report
Current view: top level - app/fdctl/run/tiles - fd_shred.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 15 475 3.2 %
Date: 2024-11-13 11:58:15 Functions: 2 14 14.3 %

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

Generated by: LCOV version 1.14