LCOV - code coverage report
Current view: top level - disco/shred - fd_fec_resolver.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 354 398 88.9 %
Date: 2024-11-13 11:58:15 Functions: 9 9 100.0 %

          Line data    Source code
       1             : #include "../../ballet/shred/fd_shred.h"
       2             : #include "../../ballet/shred/fd_fec_set.h"
       3             : #include "../../ballet/bmtree/fd_bmtree.h"
       4             : #include "../../ballet/sha512/fd_sha512.h"
       5             : #include "../../ballet/ed25519/fd_ed25519.h"
       6             : #include "../../ballet/reedsol/fd_reedsol.h"
       7             : #include "../metrics/fd_metrics.h"
       8             : #include "fd_fec_resolver.h"
       9             : 
      10         258 : #define SHRED_CNT_NOT_SET      (UINT_MAX/2U)
      11             : 
      12             : typedef union {
      13             :   fd_ed25519_sig_t u;
      14             :   ulong            l;
      15             : } wrapped_sig_t;
      16             : 
      17             : struct set_ctx;
      18             : typedef struct set_ctx set_ctx_t;
      19             : 
      20             : struct __attribute__((aligned(32UL))) set_ctx {
      21             :   wrapped_sig_t         sig;
      22             :   fd_fec_set_t *        set;
      23             :   fd_bmtree_commit_t  * tree;
      24             :   set_ctx_t *           prev;
      25             :   set_ctx_t *           next;
      26             :   ulong                 total_rx_shred_cnt;
      27             :   ulong                 fec_set_idx;
      28             :   /* The shred index of the first parity shred in this FEC set */
      29             :   ulong                 parity_idx0;
      30             :   uchar                 data_variant;
      31             :   uchar                 parity_variant;
      32             :   /* If this FEC set has resigned shreds, this is our signature of the
      33             :      root of the Merkle tree */
      34             :   wrapped_sig_t         retransmitter_sig;
      35             : };
      36             : typedef struct set_ctx set_ctx_t;
      37             : 
      38             : #define DEQUE_NAME freelist
      39         219 : #define DEQUE_T    fd_fec_set_t *
      40             : #include "../../util/tmpl/fd_deque_dynamic.c"
      41             : 
      42             : #define DEQUE_NAME bmtrlist
      43         129 : #define DEQUE_T    void *
      44             : #include "../../util/tmpl/fd_deque_dynamic.c"
      45             : 
      46             : static const wrapped_sig_t null_signature = {{0}};
      47             : 
      48        7902 : #define MAP_KEY               sig
      49        7161 : #define MAP_KEY_T             wrapped_sig_t
      50         741 : #define MAP_KEY_NULL          null_signature
      51       17364 : #define MAP_KEY_EQUAL(k0,k1)  (!memcmp( (k0).u, (k1).u, FD_ED25519_SIG_SZ ))
      52       10671 : #define MAP_KEY_INVAL(k)      MAP_KEY_EQUAL( k, MAP_KEY_NULL )
      53             : #define MAP_KEY_EQUAL_IS_SLOW 1
      54        6930 : #define MAP_KEY_HASH(key)     ((MAP_HASH_T)fd_ulong_hash( key.l ))
      55             : #define MAP_MEMOIZE           0
      56             : #define MAP_NAME              ctx_map
      57        7017 : #define MAP_T                 set_ctx_t
      58             : /* The prev and next fields of set_ctx_t thread a linked list through
      59             :    the map.  The map can move elements around during a deletion though,
      60             :    so we need to update the links when it does.  Thankfully it gives a
      61             :    perfect hook for doing so. */
      62           6 : #define MAP_MOVE(d,s)   do { \
      63           6 :                           set_ctx_t * _d = &(d); \
      64           6 :                           set_ctx_t * _s = &(s); \
      65           6 :                           _s->prev->next = _d;   \
      66           6 :                           _s->next->prev = _d;   \
      67           6 :                           *_d = *_s;             \
      68           6 :                         } while( 0 )
      69             : #include "../../util/tmpl/fd_map_dynamic.c"
      70             : 
      71             : 
      72             : struct __attribute__((aligned(FD_FEC_RESOLVER_ALIGN))) fd_fec_resolver {
      73             :   /* depth stores the number of FEC sets this resolver can track
      74             :      simultaneously.  done_depth stores the depth of the done tcache,
      75             :      i.e. the number of done FEC set keys that this resolver remembers.
      76             :      partial_depth stores the minimum size of the free FEC set list.
      77             :      completed_depth stores the size of the completed FEC set list. */
      78             :   ulong depth;
      79             :   ulong partial_depth;
      80             :   ulong complete_depth;
      81             :   ulong done_depth;
      82             : 
      83             :   /* expected_shred_version: discard all shreds with a shred version
      84             :      other than the specified value */
      85             :   ushort expected_shred_version;
      86             : 
      87             :   /* curr_map: A map (using fd_map_dynamic) from tags of signatures to
      88             :      the context object with its relevant data.  This map contains at
      89             :      most `depth` elements at any time, but to improve query performance,
      90             :      we size it at 2*depth. */
      91             :   set_ctx_t * curr_map;
      92             : 
      93             :   /* curr_ll_sentinel: The elements of curr_map also make
      94             :      essentially a circular doubly linked list using the next and prev
      95             :      fields.  To simplify the logic, we use a sentinel node that's
      96             :      stored here instead of in the map.  Thus, the head (newest) and the
      97             :      tail (oldest) of the linked list are the next and prev pointers of
      98             :      this context (respectively).  The other fields aren't used. */
      99             :   set_ctx_t curr_ll_sentinel[1];
     100             : 
     101             :   /* done: stores signatures of FEC sets that have recently been
     102             :      completed.  This is like a tcache, but with a non-ulong key and
     103             :      using a linked list instead of a ring buffer. Any new packets
     104             :      matching tags in this set can be ignored.  Since the data structure
     105             :      we need (map with linked list) is very similar to for curr_map, we
     106             :      just use the same fd_map_dynamic instantiation.  Only fields sig,
     107             :      prev, and next are used. */
     108             :   set_ctx_t * done_map;
     109             : 
     110             :   /* done_ll_sentinel: Analogous to curr_ll_sentinel, but for the done
     111             :      map instead of the current map. */
     112             :   set_ctx_t   done_ll_sentinel[1];
     113             : 
     114             :   /* free_list and complete_list are deques (using fd_deque_dynamic)
     115             :      that FEC sets that are not in contexts in curr_map.  Similarly,
     116             :      bmtree_free_list stores footprints for the bmtree objects that are
     117             :      not in contexts in curr_map.  These lists point to objects of
     118             :      indeterminate state and need to be cleared/reset when popped off.
     119             :      Invariant: at every entry and exit to fd_fec_resolver_add_shred:
     120             :      - free_list has between partial_depth and partial_depth+depth
     121             :        elements.
     122             :      - complete_list has complete_depth elements
     123             :      - bmtree_free_list has between 0 and depth elements
     124             :      (all these counts are inclusive). */
     125             :   fd_fec_set_t * * free_list;
     126             :   fd_fec_set_t * * complete_list;
     127             :   void         * * bmtree_free_list;
     128             : 
     129             :   /* signer is used to sign shreds that require a retransmitter
     130             :      signature.  sign_ctx is provided as the first argument to the
     131             :      function. */
     132             :   fd_fec_resolver_sign_fn * signer;
     133             :   void                    * sign_ctx;
     134             : 
     135             :   /* max_shred_idx is the exclusive upper bound for shred indices.  We
     136             :      need to reject any shred with an index >= max_shred_idx, but we
     137             :      also want to reject anything that is part of an FEC set where the
     138             :      highest index of a shred in the FEC set will be >= max_shred_idx.
     139             :      */
     140             :   ulong max_shred_idx;
     141             : 
     142             :   /* sha512 and reedsol are used for calculations while adding a shred.
     143             :      Their state outside a call to add_shred is indeterminate. */
     144             :   fd_sha512_t   sha512[1];
     145             :   fd_reedsol_t  reedsol[1];
     146             : 
     147             :   /* The footprint for the objects follows the struct and is in the same
     148             :      order as the pointers, namely:
     149             :        curr_map map
     150             :        done_map map
     151             :        free_list deque
     152             :        complete_list deque
     153             :        bmtree_free_list deque
     154             :        Actual footprint for bmtrees */
     155             : };
     156             : 
     157             : typedef struct fd_fec_resolver fd_fec_resolver_t;
     158             : 
     159             : ulong
     160             : fd_fec_resolver_footprint( ulong depth,
     161             :                            ulong partial_depth,
     162             :                            ulong complete_depth,
     163           6 :                            ulong done_depth ) {
     164           6 :   if( FD_UNLIKELY( (depth==0UL) | (partial_depth==0UL) | (complete_depth==0UL) | (done_depth==0UL) ) ) return 0UL;
     165           6 :   if( FD_UNLIKELY( (depth>=(1UL<<62)-1UL) | (done_depth>=(1UL<<62)-1UL ) ) ) return 0UL; /* prevent overflow */
     166             : 
     167           6 :   int lg_curr_map_cnt = fd_ulong_find_msb( depth      + 1UL ) + 2; /* See fd_tcache.h for the logic */
     168           6 :   int lg_done_map_cnt = fd_ulong_find_msb( done_depth + 1UL ) + 2; /*  ... behind the + 2. */
     169             : 
     170           6 :   ulong footprint_per_bmtree = fd_bmtree_commit_footprint( FD_SHRED_MERKLE_LAYER_CNT );
     171             : 
     172           6 :   ulong layout = FD_LAYOUT_INIT;
     173           6 :   layout = FD_LAYOUT_APPEND( layout, FD_FEC_RESOLVER_ALIGN,  sizeof(fd_fec_resolver_t)                      );
     174           6 :   layout = FD_LAYOUT_APPEND( layout, ctx_map_align(),        ctx_map_footprint( lg_curr_map_cnt )           );
     175           6 :   layout = FD_LAYOUT_APPEND( layout, ctx_map_align(),        ctx_map_footprint( lg_done_map_cnt )           );
     176           6 :   layout = FD_LAYOUT_APPEND( layout, freelist_align(),       freelist_footprint( depth+partial_depth+1UL )  );
     177           6 :   layout = FD_LAYOUT_APPEND( layout, freelist_align(),       freelist_footprint( complete_depth+1UL  )      );
     178           6 :   layout = FD_LAYOUT_APPEND( layout, bmtrlist_align(),       bmtrlist_footprint( depth+1UL )                );
     179           6 :   layout = FD_LAYOUT_APPEND( layout, FD_BMTREE_COMMIT_ALIGN, depth*footprint_per_bmtree                     );
     180             : 
     181           6 :   return FD_LAYOUT_FINI( layout, FD_FEC_RESOLVER_ALIGN );
     182           6 : }
     183             : 
     184           6 : ulong fd_fec_resolver_align( void ) { return FD_FEC_RESOLVER_ALIGN; }
     185             : 
     186             : 
     187             : void *
     188             : fd_fec_resolver_new( void                    * shmem,
     189             :                      fd_fec_resolver_sign_fn * signer,
     190             :                      void                    * sign_ctx,
     191             :                      ulong                     depth,
     192             :                      ulong                     partial_depth,
     193             :                      ulong                     complete_depth,
     194             :                      ulong                     done_depth,
     195             :                      fd_fec_set_t            * sets,
     196             :                      ushort                    expected_shred_version,
     197          24 :                      ulong                     max_shred_idx ) {
     198          24 :   if( FD_UNLIKELY( (depth==0UL) | (partial_depth==0UL) | (complete_depth==0UL) | (done_depth==0UL) ) ) return NULL;
     199          24 :   if( FD_UNLIKELY( (depth>=(1UL<<62)-1UL) | (done_depth>=(1UL<<62)-1UL ) ) ) return NULL;
     200             : 
     201          24 :   int lg_curr_map_cnt = fd_ulong_find_msb( depth      + 1UL ) + 2;
     202          24 :   int lg_done_map_cnt = fd_ulong_find_msb( done_depth + 1UL ) + 2;
     203             : 
     204          24 :   ulong footprint_per_bmtree = fd_bmtree_commit_footprint( FD_SHRED_MERKLE_LAYER_CNT );
     205             : 
     206          24 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
     207          24 :   void * self        = FD_SCRATCH_ALLOC_APPEND( l, FD_FEC_RESOLVER_ALIGN,  sizeof(fd_fec_resolver_t)                       );
     208          24 :   void * curr        = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),        ctx_map_footprint( lg_curr_map_cnt )            );
     209          24 :   void * done        = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),        ctx_map_footprint( lg_done_map_cnt )            );
     210          24 :   void * free        = FD_SCRATCH_ALLOC_APPEND( l, freelist_align(),       freelist_footprint( depth+partial_depth+1UL )   );
     211          24 :   void * cmplst      = FD_SCRATCH_ALLOC_APPEND( l, freelist_align(),       freelist_footprint( complete_depth+1UL  )       );
     212          24 :   void * bmfree      = FD_SCRATCH_ALLOC_APPEND( l, bmtrlist_align(),       bmtrlist_footprint( depth+1UL )                 );
     213          24 :   void * bmfootprint = FD_SCRATCH_ALLOC_APPEND( l, FD_BMTREE_COMMIT_ALIGN, depth*footprint_per_bmtree                      );
     214          24 :   FD_SCRATCH_ALLOC_FINI( l, FD_FEC_RESOLVER_ALIGN );
     215             : 
     216          24 :   fd_fec_resolver_t * resolver = (fd_fec_resolver_t *)self;
     217             : 
     218          24 :   if( FD_UNLIKELY( !ctx_map_new  ( curr,   lg_curr_map_cnt         )) ) { FD_LOG_WARNING(( "curr map_new failed" )); return NULL; }
     219          24 :   if( FD_UNLIKELY( !ctx_map_new  ( done,   lg_done_map_cnt         )) ) { FD_LOG_WARNING(( "done map_new failed" )); return NULL; }
     220          24 :   if( FD_UNLIKELY( !freelist_new ( free,   depth+partial_depth+1UL )) ) { FD_LOG_WARNING(( "freelist_new failed" )); return NULL; }
     221          24 :   if( FD_UNLIKELY( !freelist_new ( cmplst, complete_depth+1UL      )) ) { FD_LOG_WARNING(( "freelist_new failed" )); return NULL; }
     222          24 :   if( FD_UNLIKELY( !bmtrlist_new ( bmfree, depth                   )) ) { FD_LOG_WARNING(( "bmtrlist_new failed" )); return NULL; }
     223          24 :   if( FD_UNLIKELY( !fd_sha512_new( (void *)resolver->sha512        )) ) { FD_LOG_WARNING(( "sha512_new failed"   )); return NULL; }
     224             : 
     225             :   /* Initialize all the lists */
     226          24 :   fd_fec_set_t * * free_list     = freelist_join( free   );
     227          24 :   fd_fec_set_t * * complete_list = freelist_join( cmplst );
     228          96 :   for( ulong i=0UL;                 i<depth+partial_depth;                i++ ) { freelist_push_tail( free_list,     sets+i ); }
     229          48 :   for( ulong i=depth+partial_depth; i<depth+partial_depth+complete_depth; i++ ) { freelist_push_tail( complete_list, sets+i ); }
     230          24 :   freelist_leave( complete_list );
     231          24 :   freelist_leave( free_list     );
     232             : 
     233          24 :   void * * bmtree_list = bmtrlist_join( bmfree );
     234          72 :   for( ulong i=0UL; i<depth; i++ ) { bmtrlist_push_tail( bmtree_list, (uchar *)bmfootprint + i*footprint_per_bmtree ); }
     235          24 :   bmtrlist_leave( bmtree_list );
     236             : 
     237          24 :   if( FD_UNLIKELY( expected_shred_version==(ushort)0 ) ) { FD_LOG_WARNING(( "expected shred version cannot be 0" )); return NULL; }
     238             : 
     239          24 :   resolver->curr_ll_sentinel->prev = resolver->curr_ll_sentinel;
     240          24 :   resolver->curr_ll_sentinel->next = resolver->curr_ll_sentinel;
     241          24 :   resolver->done_ll_sentinel->prev = resolver->done_ll_sentinel;
     242          24 :   resolver->done_ll_sentinel->next = resolver->done_ll_sentinel;
     243             : 
     244          24 :   resolver->depth                  = depth;
     245          24 :   resolver->partial_depth          = partial_depth;
     246          24 :   resolver->complete_depth         = complete_depth;
     247          24 :   resolver->done_depth             = done_depth;
     248          24 :   resolver->expected_shred_version = expected_shred_version;
     249          24 :   resolver->signer                 = signer;
     250          24 :   resolver->sign_ctx               = sign_ctx;
     251          24 :   resolver->max_shred_idx          = max_shred_idx;
     252          24 :   return shmem;
     253          24 : }
     254             : 
     255             : fd_fec_resolver_t *
     256          24 : fd_fec_resolver_join( void * shmem ) {
     257          24 :   fd_fec_resolver_t * resolver = (fd_fec_resolver_t *)shmem;
     258          24 :   ulong depth          = resolver->depth;
     259          24 :   ulong partial_depth  = resolver->partial_depth;
     260          24 :   ulong complete_depth = resolver->complete_depth;
     261          24 :   ulong done_depth     = resolver->done_depth;
     262             : 
     263          24 :   int lg_curr_map_cnt = fd_ulong_find_msb( depth      + 1UL ) + 2;
     264          24 :   int lg_done_map_cnt = fd_ulong_find_msb( done_depth + 1UL ) + 2;
     265             : 
     266          24 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
     267          24 :   /*     self       */ FD_SCRATCH_ALLOC_APPEND( l, FD_FEC_RESOLVER_ALIGN,  sizeof(fd_fec_resolver_t)                     );
     268          24 :   void * curr        = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),        ctx_map_footprint( lg_curr_map_cnt )          );
     269          24 :   void * done        = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),        ctx_map_footprint( lg_done_map_cnt )          );
     270          24 :   void * free        = FD_SCRATCH_ALLOC_APPEND( l, freelist_align(),       freelist_footprint( depth+partial_depth+1UL ) );
     271          24 :   void * cmplst      = FD_SCRATCH_ALLOC_APPEND( l, freelist_align(),       freelist_footprint( complete_depth+1UL  )     );
     272          24 :   void * bmfree      = FD_SCRATCH_ALLOC_APPEND( l, bmtrlist_align(),       bmtrlist_footprint( depth+1UL )               );
     273          24 :   FD_SCRATCH_ALLOC_FINI( l, FD_FEC_RESOLVER_ALIGN );
     274             : 
     275          24 :   resolver->curr_map         = ctx_map_join  ( curr   ); if( FD_UNLIKELY( !resolver->curr_map         ) ) return NULL;
     276          24 :   resolver->done_map         = ctx_map_join  ( done   ); if( FD_UNLIKELY( !resolver->done_map         ) ) return NULL;
     277          24 :   resolver->free_list        = freelist_join ( free   ); if( FD_UNLIKELY( !resolver->free_list        ) ) return NULL;
     278          24 :   resolver->complete_list    = freelist_join ( cmplst ); if( FD_UNLIKELY( !resolver->complete_list    ) ) return NULL;
     279          24 :   resolver->bmtree_free_list = bmtrlist_join ( bmfree ); if( FD_UNLIKELY( !resolver->bmtree_free_list ) ) return NULL;
     280          24 :   if( FD_UNLIKELY( !fd_sha512_join( resolver->sha512 ) ) ) return NULL;
     281             : 
     282          24 :   return resolver;
     283          24 : }
     284             : 
     285             : /* Two helper functions for working with the linked lists that are
     286             :    threaded through maps.  Use them as follows:
     287             :       ctx_ll_insert( <sentinel corresponding to map>, ctx_map_insert( <map>, key ) );
     288             :       ctx_map_remove( <map>, ctx_ll_remove( <node to remove> ) );
     289             : 
     290             :   */
     291             : /* Removes r from the linked list */
     292             : static set_ctx_t *
     293         207 : ctx_ll_remove( set_ctx_t * r ) {
     294         207 :   r->next->prev = r->prev;
     295         207 :   r->prev->next = r->next;
     296         207 :   r->next = NULL;
     297         207 :   r->prev = NULL;
     298         207 :   return r;
     299         207 : }
     300             : 
     301             : /* Inserts c immediately after p.  Returns c. */
     302             : static set_ctx_t *
     303         252 : ctx_ll_insert( set_ctx_t * p, set_ctx_t * c ) {
     304         252 :   c->next = p->next;
     305         252 :   c->prev = p;
     306         252 :   p->next->prev = c;
     307         252 :   p->next       = c;
     308         252 :   return c;
     309         252 : }
     310             : 
     311             : 
     312             : int fd_fec_resolver_add_shred( fd_fec_resolver_t    * resolver,
     313             :                                fd_shred_t   const   * shred,
     314             :                                ulong                  shred_sz,
     315             :                                uchar        const   * leader_pubkey,
     316             :                                fd_fec_set_t const * * out_fec_set,
     317        3510 :                                fd_shred_t   const * * out_shred ) {
     318             :   /* Unpack variables */
     319        3510 :   ulong partial_depth = resolver->partial_depth;
     320        3510 :   ulong done_depth    = resolver->done_depth;
     321             : 
     322        3510 :   fd_fec_set_t * * free_list        = resolver->free_list;
     323        3510 :   fd_fec_set_t * * complete_list    = resolver->complete_list;
     324        3510 :   void         * * bmtree_free_list = resolver->bmtree_free_list;
     325        3510 :   set_ctx_t    *   curr_map         = resolver->curr_map;
     326        3510 :   set_ctx_t    *   done_map         = resolver->done_map;
     327             : 
     328        3510 :   fd_reedsol_t * reedsol       = resolver->reedsol;
     329        3510 :   fd_sha512_t  * sha512        = resolver->sha512;
     330             : 
     331        3510 :   set_ctx_t    * curr_ll_sentinel = resolver->curr_ll_sentinel;
     332        3510 :   set_ctx_t    * done_ll_sentinel = resolver->done_ll_sentinel;
     333             : 
     334             :   /* Invariants:
     335             :       * no key is in both the done map and the current map
     336             :       * each set pointer provided to the new function is in exactly one
     337             :           of curr_map, freelist, or complete_list
     338             :       * bmtree_free_list has exactly partial_depth fewer elements than
     339             :           freelist
     340             :    */
     341        3510 :   wrapped_sig_t * w_sig = (wrapped_sig_t *)shred->signature;
     342             : 
     343             :   /* Immediately reject any shred with a 0 signature. */
     344        3510 :   if( FD_UNLIKELY( ctx_map_key_inval( *w_sig ) ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     345             : 
     346             :   /* Are we already done with this FEC set? */
     347        3510 :   int found = !!ctx_map_query( done_map, *w_sig, NULL );
     348             : 
     349        3510 :   if( found )  return FD_FEC_RESOLVER_SHRED_IGNORED; /* With no packet loss, we expect found==1 about 50% of the time */
     350             : 
     351        3159 :   set_ctx_t * ctx = ctx_map_query( curr_map, *w_sig, NULL );
     352             : 
     353        3159 :   fd_bmtree_node_t leaf[1];
     354        3159 :   uchar variant    = shred->variant;
     355        3159 :   uchar shred_type = fd_shred_type( variant );
     356             : 
     357        3159 :   if( FD_UNLIKELY( (shred_type==FD_SHRED_TYPE_LEGACY_DATA) | (shred_type==FD_SHRED_TYPE_LEGACY_CODE) ) ) {
     358             :     /* Reject any legacy shreds */
     359           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
     360           0 :   }
     361             : 
     362        3159 :   if( FD_UNLIKELY( shred->version!=resolver->expected_shred_version ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     363        3156 :   if( FD_UNLIKELY( shred_sz<fd_shred_sz( shred )                    ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     364        3156 :   if( FD_UNLIKELY( shred->idx>=resolver->max_shred_idx              ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     365             : 
     366        3144 :   int is_data_shred = fd_shred_is_data( shred_type );
     367             : 
     368        3144 :   if( !is_data_shred ) { /* Roughly 50/50 branch */
     369        1704 :     if( FD_UNLIKELY( (shred->code.data_cnt>FD_REEDSOL_DATA_SHREDS_MAX) | (shred->code.code_cnt>FD_REEDSOL_PARITY_SHREDS_MAX) ) )
     370           3 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     371        1701 :     if( FD_UNLIKELY( (shred->code.data_cnt==0UL) | (shred->code.code_cnt==0UL)                                               ) )
     372           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     373        1701 :     if( FD_UNLIKELY( (ulong)shred->fec_set_idx+(ulong)shred->code.data_cnt>=resolver->max_shred_idx                          ) )
     374           3 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     375        1698 :     if( FD_UNLIKELY( (ulong)shred->idx + (ulong)shred->code.code_cnt - (ulong)shred->code.idx>=resolver->max_shred_idx       ) )
     376           3 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     377        1698 :   }
     378             : 
     379             : 
     380             :   /* For the purposes of the shred header, tree_depth means the number
     381             :      of nodes, counting the leaf but excluding the root.  For bmtree,
     382             :      depth means the number of layers, which counts both. */
     383        3135 :   ulong tree_depth           = fd_shred_merkle_cnt( variant ); /* In [0, 15] */
     384        3135 :   ulong reedsol_protected_sz = 1115UL + FD_SHRED_DATA_HEADER_SZ - FD_SHRED_SIGNATURE_SZ - FD_SHRED_MERKLE_NODE_SZ*tree_depth
     385        3135 :                                       - FD_SHRED_MERKLE_ROOT_SZ*fd_shred_is_chained ( shred_type )
     386        3135 :                                       - FD_SHRED_SIGNATURE_SZ  *fd_shred_is_resigned( shred_type); /* In [743, 1139] conservatively*/
     387        3135 :   ulong data_merkle_protected_sz   = reedsol_protected_sz + FD_SHRED_MERKLE_ROOT_SZ*fd_shred_is_chained ( shred_type );
     388        3135 :   ulong parity_merkle_protected_sz = reedsol_protected_sz + FD_SHRED_MERKLE_ROOT_SZ*fd_shred_is_chained ( shred_type )+FD_SHRED_CODE_HEADER_SZ-FD_ED25519_SIG_SZ;
     389        3135 :   ulong merkle_protected_sz  = fd_ulong_if( is_data_shred, data_merkle_protected_sz, parity_merkle_protected_sz );
     390             : 
     391        3135 :   fd_bmtree_hash_leaf( leaf, (uchar const *)shred + sizeof(fd_ed25519_sig_t), merkle_protected_sz, FD_BMTREE_LONG_PREFIX_SZ );
     392             : 
     393             :   /* in_type_idx is between [0, code.data_cnt) or [0, code.code_cnt),
     394             :      where data_cnt <= FD_REEDSOL_DATA_SHREDS_MAX and code_cnt <=
     395             :      FD_REEDSOL_PARITY_SHREDS_MAX.
     396             :      On the other hand, shred_idx, goes from [0, code.data_cnt +
     397             :      code.code_cnt), with all the data shreds having
     398             :      shred_idx < code.data_cnt and all the parity shreds having
     399             :      shred_idx >= code.data_cnt. */
     400        3135 :   ulong in_type_idx = fd_ulong_if( is_data_shred, shred->idx - shred->fec_set_idx, shred->code.idx );
     401        3135 :   ulong shred_idx   = fd_ulong_if( is_data_shred, in_type_idx, in_type_idx + shred->code.data_cnt  );
     402             : 
     403        3135 :   if( FD_UNLIKELY( in_type_idx >= fd_ulong_if( is_data_shred, FD_REEDSOL_DATA_SHREDS_MAX, FD_REEDSOL_PARITY_SHREDS_MAX ) ) )
     404           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
     405             :   /* This, combined with the check on shred->code.data_cnt implies that
     406             :      shred_idx is in [0, DATA_SHREDS_MAX+PARITY_SHREDS_MAX). */
     407             : 
     408        3135 :   if( FD_UNLIKELY( tree_depth>FD_SHRED_MERKLE_LAYER_CNT-1UL             ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     409        3135 :   if( FD_UNLIKELY( fd_bmtree_depth( shred_idx+1UL ) > tree_depth+1UL ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     410             : 
     411        3135 :   if( FD_UNLIKELY( !ctx ) ) {
     412             :     /* This is the first shred in the FEC set */
     413         129 :     if( FD_UNLIKELY( freelist_cnt( free_list )<=partial_depth ) ) {
     414             :       /* Packet loss is really high and we have a lot of in-progress FEC
     415             :          sets that we haven't been able to finish.  Take the resources
     416             :          (FEC set and bmtree) from the oldest, and send the oldest FEC
     417             :          set to the back of the free list. */
     418          33 :       set_ctx_t * victim_ctx = resolver->curr_ll_sentinel->prev;
     419             : 
     420             :       /* Add this one that we're sacrificing to the done map to
     421             :          prevent the possibility of thrashing. */
     422          33 :       ctx_ll_insert( done_ll_sentinel, ctx_map_insert( done_map, victim_ctx->sig ) );
     423          33 :       if( FD_UNLIKELY( ctx_map_key_cnt( done_map ) > done_depth ) ) ctx_map_remove( done_map, ctx_ll_remove( done_ll_sentinel->prev ) );
     424             : 
     425          33 :       freelist_push_tail( free_list,        victim_ctx->set  );
     426          33 :       bmtrlist_push_tail( bmtree_free_list, victim_ctx->tree );
     427             : 
     428             :       /* Remove from linked list and then from the map */
     429          33 :       ctx_map_remove( curr_map, ctx_ll_remove( victim_ctx ) );
     430             : 
     431          33 :       FD_MCNT_INC( SHRED, FEC_SET_SPILLED, 1UL );
     432          33 :     }
     433             :     /* Now we know |free_list|>partial_depth and |bmtree_free_list|>1 */
     434             : 
     435         129 :     fd_fec_set_t * set_to_use = freelist_pop_head( free_list        );
     436         129 :     void         * bmtree_mem = bmtrlist_pop_head( bmtree_free_list );
     437             : 
     438             :     /* Now we need to derive the root of the Merkle tree and verify the
     439             :        signature to prevent a DOS attack just by sending lots of invalid
     440             :        shreds. */
     441         129 :     fd_bmtree_commit_t * tree;
     442         129 :     tree = fd_bmtree_commit_init( bmtree_mem, FD_SHRED_MERKLE_NODE_SZ, FD_BMTREE_LONG_PREFIX_SZ, FD_SHRED_MERKLE_LAYER_CNT );
     443             : 
     444         129 :     fd_bmtree_node_t _root[1];
     445         129 :     fd_shred_merkle_t const * proof = fd_shred_merkle_nodes( shred );
     446         129 :     int rv = fd_bmtree_commitp_insert_with_proof( tree, shred_idx, leaf, (uchar const *)proof, tree_depth, _root );
     447         129 :     if( FD_UNLIKELY( !rv ) ) {
     448           0 :       freelist_push_head( free_list,        set_to_use );
     449           0 :       bmtrlist_push_head( bmtree_free_list, bmtree_mem );
     450           0 :       FD_MCNT_INC( SHRED, SHRED_REJECTED_INITIAL, 1UL );
     451           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     452           0 :     }
     453             : 
     454         129 :     if( FD_UNLIKELY( FD_ED25519_SUCCESS != fd_ed25519_verify( _root->hash, 32UL, shred->signature, leader_pubkey, sha512 ) ) ) {
     455           0 :       freelist_push_head( free_list,        set_to_use );
     456           0 :       bmtrlist_push_head( bmtree_free_list, bmtree_mem );
     457           0 :       FD_MCNT_INC( SHRED, SHRED_REJECTED_INITIAL, 1UL );
     458           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     459           0 :     }
     460             : 
     461             :     /* This seems like a legitimate FEC set, so we can reserve some
     462             :        resources for it. */
     463         129 :     ctx = ctx_ll_insert( curr_ll_sentinel, ctx_map_insert( curr_map, *w_sig ) );
     464         129 :     ctx->set  = set_to_use;
     465         129 :     ctx->tree = tree;
     466         129 :     ctx->total_rx_shred_cnt = 0UL;
     467         129 :     ctx->data_variant   = fd_uchar_if(  is_data_shred, variant, fd_shred_variant( fd_shred_swap_type( shred_type ), (uchar)tree_depth ) );
     468         129 :     ctx->parity_variant = fd_uchar_if( !is_data_shred, variant, fd_shred_variant( fd_shred_swap_type( shred_type ), (uchar)tree_depth ) );
     469             : 
     470         129 :     if( FD_UNLIKELY( fd_shred_is_resigned( shred_type ) & !!(resolver->signer) ) ) {
     471           3 :       resolver->signer( resolver->sign_ctx, ctx->retransmitter_sig.u, _root->hash );
     472         126 :     } else {
     473         126 :       fd_memset( ctx->retransmitter_sig.u, 0, 64UL );
     474         126 :     }
     475             : 
     476             :     /* Reset the FEC set */
     477         129 :     ctx->set->data_shred_cnt   = SHRED_CNT_NOT_SET;
     478         129 :     ctx->set->parity_shred_cnt = SHRED_CNT_NOT_SET;
     479         129 :     d_rcvd_join( d_rcvd_new( d_rcvd_delete( d_rcvd_leave( ctx->set->data_shred_rcvd   ) ) ) );
     480         129 :     p_rcvd_join( p_rcvd_new( p_rcvd_delete( p_rcvd_leave( ctx->set->parity_shred_rcvd ) ) ) );
     481             : 
     482        3006 :   } else {
     483             :     /* This is not the first shred in the set */
     484             :     /* First, check to make sure this is not a duplicate */
     485        3006 :     int shred_dup = fd_int_if( is_data_shred, d_rcvd_test( ctx->set->data_shred_rcvd,   in_type_idx ),
     486        3006 :                                               p_rcvd_test( ctx->set->parity_shred_rcvd, in_type_idx ) );
     487             : 
     488        3006 :     if( FD_UNLIKELY( shred_dup ) ) return FD_FEC_RESOLVER_SHRED_IGNORED;
     489             : 
     490             :     /* Ensure that all the shreds in the FEC set have consistent
     491             :        variants.  They all must have the same tree_depth and the same
     492             :        chained/not chained, resigned/not resigned bits. */
     493        2796 :     if( FD_UNLIKELY( variant!=fd_uchar_if( is_data_shred, ctx->data_variant, ctx->parity_variant ) ) ) {
     494           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     495           0 :     }
     496             : 
     497        2796 :     fd_shred_merkle_t const * proof = fd_shred_merkle_nodes( shred );
     498        2796 :     int rv = fd_bmtree_commitp_insert_with_proof( ctx->tree, shred_idx, leaf, (uchar const *)proof, tree_depth, NULL );
     499        2796 :     if( !rv ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     500        2796 :   }
     501             : 
     502        2922 :   if( FD_UNLIKELY( (ctx->set->data_shred_cnt==SHRED_CNT_NOT_SET) & (!is_data_shred) ) ) {
     503         108 :     ctx->set->data_shred_cnt   = shred->code.data_cnt;
     504         108 :     ctx->set->parity_shred_cnt = shred->code.code_cnt;
     505         108 :     ctx->parity_idx0           = shred->idx - in_type_idx;
     506         108 :     ctx->fec_set_idx           = shred->fec_set_idx;
     507         108 :   }
     508             : 
     509             :   /* At this point, the shred has passed Merkle validation and is new.
     510             :      We also know that ctx is a pointer to the slot for signature in the
     511             :      current map. */
     512             : 
     513             :   /* Copy the shred to memory the FEC resolver owns */
     514        2922 :   uchar * dst = fd_ptr_if( is_data_shred, ctx->set->data_shreds[ in_type_idx ], ctx->set->parity_shreds[ in_type_idx ] );
     515        2922 :   fd_memcpy( dst, shred, fd_shred_sz( shred ) );
     516             : 
     517             :   /* If the shred needs a retransmitter signature, set it */
     518        2922 :   if( FD_UNLIKELY( fd_shred_is_resigned( shred_type ) ) ) {
     519          99 :     memcpy( dst + fd_shred_retransmitter_sig_off( (fd_shred_t *)dst ), ctx->retransmitter_sig.u, 64UL );
     520          99 :   }
     521             : 
     522        2922 :   d_rcvd_insert_if( ctx->set->data_shred_rcvd,    is_data_shred, in_type_idx );
     523        2922 :   p_rcvd_insert_if( ctx->set->parity_shred_rcvd, !is_data_shred, in_type_idx );
     524        2922 :   ctx->total_rx_shred_cnt++;
     525             : 
     526        2922 :   *out_shred = (fd_shred_t const *)dst;
     527             : 
     528             :   /* Do we have enough to begin reconstruction? */
     529        2922 :   if( FD_LIKELY( ctx->total_rx_shred_cnt < ctx->set->data_shred_cnt ) ) return FD_FEC_RESOLVER_SHRED_OKAY;
     530             : 
     531             :   /* At this point, the FEC set is either valid or permanently invalid,
     532             :      so we can consider it done either way.  First though, since ctx_map_remove
     533             :      can change what's at *ctx, so unpack the values before we do that */
     534          90 :   fd_fec_set_t        * set            = ctx->set;
     535          90 :   fd_bmtree_commit_t  * tree           = ctx->tree;
     536          90 :   ulong                 fec_set_idx    = ctx->fec_set_idx;
     537          90 :   ulong                 parity_idx0    = ctx->parity_idx0;
     538          90 :   wrapped_sig_t         retran_sig     = ctx->retransmitter_sig;
     539          90 :   uchar                 parity_variant = ctx->parity_variant;
     540          90 :   uchar                 data_variant   = ctx->data_variant;
     541             : 
     542          90 :   ctx_ll_insert( done_ll_sentinel, ctx_map_insert( done_map, ctx->sig ) );
     543          90 :   if( FD_UNLIKELY( ctx_map_key_cnt( done_map ) > done_depth ) ) ctx_map_remove( done_map, ctx_ll_remove( done_ll_sentinel->prev ) );
     544             : 
     545          90 :   ctx_map_remove( curr_map, ctx_ll_remove( ctx ) );
     546             : 
     547          90 :   reedsol = fd_reedsol_recover_init( (void*)reedsol, reedsol_protected_sz );
     548        2832 :   for( ulong i=0UL; i<set->data_shred_cnt; i++ ) {
     549        2742 :     uchar * rs_payload = set->data_shreds[ i ] + sizeof(fd_ed25519_sig_t);
     550        2742 :     if( d_rcvd_test( set->data_shred_rcvd, i ) ) fd_reedsol_recover_add_rcvd_shred  ( reedsol, 1, rs_payload );
     551        1419 :     else                                         fd_reedsol_recover_add_erased_shred( reedsol, 1, rs_payload );
     552        2742 :   }
     553        2994 :   for( ulong i=0UL; i<set->parity_shred_cnt; i++ ) {
     554        2904 :     uchar * rs_payload = set->parity_shreds[ i ] + FD_SHRED_CODE_HEADER_SZ;
     555        2904 :     if( p_rcvd_test( set->parity_shred_rcvd, i ) ) fd_reedsol_recover_add_rcvd_shred  ( reedsol, 0, rs_payload );
     556        1437 :     else                                           fd_reedsol_recover_add_erased_shred( reedsol, 0, rs_payload );
     557        2904 :   }
     558             : 
     559          90 :   if( FD_UNLIKELY( FD_REEDSOL_SUCCESS != fd_reedsol_recover_fini( reedsol ) ) ) {
     560             :     /* A few lines up, we already checked to make sure it wasn't the
     561             :        insufficient case, so it must be the inconsistent case.  That
     562             :        means the leader signed a shred with invalid Reed-Solomon FEC
     563             :        set.  This shouldn't happen in practice, but we need to handle it
     564             :        for the malicious leader case.  This should probably be a
     565             :        slash-able offense. */
     566           0 :     freelist_push_tail( free_list,        set  );
     567           0 :     bmtrlist_push_tail( bmtree_free_list, tree );
     568           0 :     FD_MCNT_INC( SHRED, FEC_REJECTED_FATAL, 1UL );
     569           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
     570           0 :   }
     571             : 
     572          90 :   uchar const * chained_root = fd_ptr_if( fd_shred_is_chained( shred_type ), (uchar *)shred+fd_shred_chain_offset( variant ), NULL );
     573             : 
     574             :   /* Iterate over recovered shreds, add them to the Merkle tree,
     575             :      populate headers and signatures. */
     576        2832 :   for( ulong i=0UL; i<set->data_shred_cnt; i++ ) {
     577        2742 :     if( !d_rcvd_test( set->data_shred_rcvd, i ) ) {
     578        1419 :       fd_memcpy( set->data_shreds[i], shred, sizeof(fd_ed25519_sig_t) );
     579        1419 :       if( FD_UNLIKELY( fd_shred_is_chained( shred_type ) ) ) {
     580           0 :         fd_memcpy( set->data_shreds[i]+fd_shred_chain_offset( data_variant ), chained_root, FD_SHRED_MERKLE_ROOT_SZ );
     581           0 :       }
     582        1419 :       fd_bmtree_hash_leaf( leaf, set->data_shreds[i]+sizeof(fd_ed25519_sig_t), data_merkle_protected_sz, FD_BMTREE_LONG_PREFIX_SZ );
     583        1419 :       if( FD_UNLIKELY( !fd_bmtree_commitp_insert_with_proof( tree, i, leaf, NULL, 0, NULL ) ) ) {
     584           0 :         freelist_push_tail( free_list,        set  );
     585           0 :         bmtrlist_push_tail( bmtree_free_list, tree );
     586           0 :         FD_MCNT_INC( SHRED, FEC_REJECTED_FATAL, 1UL );
     587           0 :         return FD_FEC_RESOLVER_SHRED_REJECTED;
     588           0 :       }
     589             : 
     590        1419 :     }
     591        2742 :   }
     592             : 
     593        2994 :   for( ulong i=0UL; i<set->parity_shred_cnt; i++ ) {
     594        2904 :     if( !p_rcvd_test( set->parity_shred_rcvd, i ) ) {
     595        1437 :       fd_shred_t * p_shred = (fd_shred_t *)set->parity_shreds[i]; /* We can't parse because we haven't populated the header */
     596        1437 :       fd_memcpy( p_shred->signature, shred->signature, sizeof(fd_ed25519_sig_t) );
     597        1437 :       p_shred->variant       = parity_variant;
     598        1437 :       p_shred->slot          = shred->slot;
     599        1437 :       p_shred->idx           = (uint)(i + parity_idx0);
     600        1437 :       p_shred->version       = shred->version;
     601        1437 :       p_shred->fec_set_idx   = (uint)fec_set_idx;
     602        1437 :       p_shred->code.data_cnt = (ushort)set->data_shred_cnt;
     603        1437 :       p_shred->code.code_cnt = (ushort)set->parity_shred_cnt;
     604        1437 :       p_shred->code.idx      = (ushort)i;
     605             : 
     606        1437 :       if( FD_UNLIKELY( fd_shred_is_chained( shred_type ) ) ) {
     607         345 :         fd_memcpy( set->parity_shreds[i]+fd_shred_chain_offset( parity_variant ), chained_root, FD_SHRED_MERKLE_ROOT_SZ );
     608         345 :       }
     609             : 
     610        1437 :       fd_bmtree_hash_leaf( leaf, set->parity_shreds[i]+ sizeof(fd_ed25519_sig_t), parity_merkle_protected_sz, FD_BMTREE_LONG_PREFIX_SZ );
     611        1437 :       if( FD_UNLIKELY( !fd_bmtree_commitp_insert_with_proof( tree, set->data_shred_cnt + i, leaf, NULL, 0, NULL ) ) ) {
     612           0 :         freelist_push_tail( free_list,        set  );
     613           0 :         bmtrlist_push_tail( bmtree_free_list, tree );
     614           0 :         FD_MCNT_INC( SHRED, FEC_REJECTED_FATAL, 1UL );
     615           0 :         return FD_FEC_RESOLVER_SHRED_REJECTED;
     616           0 :       }
     617        1437 :     }
     618        2904 :   }
     619             : 
     620             :   /* Check that the whole Merkle tree is consistent. */
     621          90 :   if( FD_UNLIKELY( !fd_bmtree_commitp_fini( tree, set->data_shred_cnt + set->parity_shred_cnt ) ) ) {
     622           0 :     freelist_push_tail( free_list,        set  );
     623           0 :     bmtrlist_push_tail( bmtree_free_list, tree );
     624           0 :     FD_MCNT_INC( SHRED, FEC_REJECTED_FATAL, 1UL );
     625           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
     626           0 :   }
     627             : 
     628             :   /* Check that all the fields that are supposed to be consistent across
     629             :      an FEC set actually are. */
     630          90 :   fd_shred_t const * base_data_shred   = fd_shred_parse( set->data_shreds  [ 0 ], FD_SHRED_MIN_SZ );
     631          90 :   fd_shred_t const * base_parity_shred = fd_shred_parse( set->parity_shreds[ 0 ], FD_SHRED_MAX_SZ );
     632          90 :   int reject = (!base_data_shred) | (!base_parity_shred);
     633             : 
     634        2742 :   for( ulong i=1UL; (!reject) & (i<set->data_shred_cnt); i++ ) {
     635             :     /* Technically, we only need to re-parse the ones we recovered with
     636             :        Reedsol, but parsing is pretty cheap and the rest of the
     637             :        validation we need to do on all of them. */
     638        2652 :     fd_shred_t const * parsed = fd_shred_parse( set->data_shreds[ i ], FD_SHRED_MIN_SZ );
     639        2652 :     if( FD_UNLIKELY( !parsed ) ) { reject = 1; break; }
     640        2652 :     reject |= parsed->variant         != base_data_shred->variant;
     641        2652 :     reject |= parsed->slot            != base_data_shred->slot;
     642        2652 :     reject |= parsed->version         != base_data_shred->version;
     643        2652 :     reject |= parsed->fec_set_idx     != base_data_shred->fec_set_idx;
     644        2652 :     reject |= parsed->data.parent_off != base_data_shred->data.parent_off;
     645             : 
     646        2652 :     reject |= fd_shred_is_chained( fd_shred_type( parsed->variant ) ) &&
     647        2652 :                 !fd_memeq( (uchar *)parsed         +fd_shred_chain_offset( parsed->variant          ),
     648         183 :                            (uchar *)base_data_shred+fd_shred_chain_offset( base_data_shred->variant ), FD_SHRED_MERKLE_ROOT_SZ );
     649        2652 :   }
     650        2994 :   for( ulong i=0UL; (!reject) & (i<set->parity_shred_cnt); i++ ) {
     651        2904 :     fd_shred_t const * parsed = fd_shred_parse( set->parity_shreds[ i ], FD_SHRED_MAX_SZ );
     652        2904 :     if( FD_UNLIKELY( !parsed ) ) { reject = 1; break; }
     653        2904 :     reject |= fd_shred_type( parsed->variant )       != fd_shred_swap_type( fd_shred_type( base_data_shred->variant ) );
     654        2904 :     reject |= fd_shred_merkle_cnt( parsed->variant ) != fd_shred_merkle_cnt( base_data_shred->variant );
     655        2904 :     reject |= parsed->slot                           != base_data_shred->slot;
     656        2904 :     reject |= parsed->version                        != base_data_shred->version;
     657        2904 :     reject |= parsed->fec_set_idx                    != base_data_shred->fec_set_idx;
     658        2904 :     reject |= parsed->code.data_cnt                  != base_parity_shred->code.data_cnt;
     659        2904 :     reject |= parsed->code.code_cnt                  != base_parity_shred->code.code_cnt;
     660        2904 :     reject |= parsed->code.idx                       != (ushort)i;
     661             : 
     662        2904 :     reject |= fd_shred_is_chained( fd_shred_type( parsed->variant ) ) &&
     663        2904 :                 !fd_memeq( (uchar *)parsed         +fd_shred_chain_offset( parsed->variant          ),
     664         360 :                            (uchar *)base_data_shred+fd_shred_chain_offset( base_data_shred->variant ), FD_SHRED_MERKLE_ROOT_SZ );
     665        2904 :   }
     666          90 :   if( FD_UNLIKELY( reject ) ) {
     667           0 :     freelist_push_tail( free_list,        set  );
     668           0 :     bmtrlist_push_tail( bmtree_free_list, tree );
     669           0 :     FD_MCNT_INC( SHRED, FEC_REJECTED_FATAL, 1UL );
     670           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
     671           0 :   }
     672             : 
     673             :   /* Populate missing Merkle proofs */
     674        2832 :   for( ulong i=0UL; i<set->data_shred_cnt; i++ ) if( !d_rcvd_test( set->data_shred_rcvd, i ) )
     675        1419 :     fd_bmtree_get_proof( tree, set->data_shreds[i]   + fd_shred_merkle_off( (fd_shred_t *)set->data_shreds[i] ), i );
     676             : 
     677        2994 :   for( ulong i=0UL; i<set->parity_shred_cnt; i++ ) if( !p_rcvd_test( set->parity_shred_rcvd, i ) )
     678        1437 :     fd_bmtree_get_proof( tree, set->parity_shreds[i] + fd_shred_merkle_off( (fd_shred_t *)set->parity_shreds[i] ), set->data_shred_cnt+i );
     679             : 
     680             :   /* Set the retransmitter signature for shreds that need one */
     681          90 :   if( FD_UNLIKELY( fd_shred_is_resigned( shred_type ) ) ) {
     682          99 :     for( ulong i=0UL; i<set->data_shred_cnt; i++ ) if( !d_rcvd_test( set->data_shred_rcvd, i ) )
     683           0 :       memcpy( set->data_shreds[i]   + fd_shred_retransmitter_sig_off( (fd_shred_t *)set->data_shreds[i]   ), retran_sig.u, 64UL );
     684             : 
     685          99 :     for( ulong i=0UL; i<set->parity_shred_cnt; i++ ) if( !p_rcvd_test( set->parity_shred_rcvd, i ) )
     686          93 :       memcpy( set->parity_shreds[i] + fd_shred_retransmitter_sig_off( (fd_shred_t *)set->parity_shreds[i] ), retran_sig.u, 64UL );
     687           3 :   }
     688             : 
     689             :   /* Finally... A valid FEC set.  Forward it along. */
     690          90 :   bmtrlist_push_tail( bmtree_free_list, tree );
     691          90 :   freelist_push_tail( complete_list, set );
     692          90 :   freelist_push_tail( free_list, freelist_pop_head( complete_list ) );
     693             : 
     694          90 :   *out_fec_set = set;
     695             : 
     696          90 :   return FD_FEC_RESOLVER_SHRED_COMPLETES;
     697          90 : }
     698             : 
     699          21 : void * fd_fec_resolver_leave( fd_fec_resolver_t * resolver ) {
     700          21 :   fd_sha512_leave( resolver->sha512           );
     701          21 :   bmtrlist_leave ( resolver->bmtree_free_list );
     702          21 :   freelist_leave ( resolver->complete_list    );
     703          21 :   freelist_leave ( resolver->free_list        );
     704          21 :   ctx_map_leave  ( resolver->done_map         );
     705          21 :   ctx_map_leave  ( resolver->curr_map         );
     706             : 
     707          21 :   return (void *)resolver;
     708          21 : }
     709             : 
     710          21 : void * fd_fec_resolver_delete( void * shmem ) {
     711          21 :   fd_fec_resolver_t * resolver = (fd_fec_resolver_t *)shmem;
     712          21 :   ulong depth          = resolver->depth;
     713          21 :   ulong partial_depth  = resolver->partial_depth;
     714          21 :   ulong complete_depth = resolver->complete_depth;
     715          21 :   ulong done_depth     = resolver->done_depth;
     716             : 
     717          21 :   int lg_curr_map_cnt = fd_ulong_find_msb( depth      + 1UL ) + 2;
     718          21 :   int lg_done_map_cnt = fd_ulong_find_msb( done_depth + 1UL ) + 2;
     719             : 
     720          21 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
     721          21 :   /*     self       */ FD_SCRATCH_ALLOC_APPEND( l, FD_FEC_RESOLVER_ALIGN,  sizeof(fd_fec_resolver_t)                     );
     722          21 :   void * curr        = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),        ctx_map_footprint( lg_curr_map_cnt )          );
     723          21 :   void * done        = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),        ctx_map_footprint( lg_done_map_cnt )          );
     724          21 :   void * free        = FD_SCRATCH_ALLOC_APPEND( l, freelist_align(),       freelist_footprint( depth+partial_depth+1UL ) );
     725          21 :   void * cmplst      = FD_SCRATCH_ALLOC_APPEND( l, freelist_align(),       freelist_footprint( complete_depth+1UL  )     );
     726          21 :   void * bmfree      = FD_SCRATCH_ALLOC_APPEND( l, bmtrlist_align(),       bmtrlist_footprint( depth+1UL )               );
     727          21 :   FD_SCRATCH_ALLOC_FINI( l, FD_FEC_RESOLVER_ALIGN );
     728             : 
     729          21 :   fd_sha512_delete( resolver->sha512 );
     730          21 :   bmtrlist_delete ( bmfree           );
     731          21 :   freelist_delete ( cmplst           );
     732          21 :   freelist_delete ( free             );
     733          21 :   ctx_map_delete  ( done             );
     734          21 :   ctx_map_delete  ( curr             );
     735             : 
     736          21 :   return shmem;
     737          21 : }

Generated by: LCOV version 1.14