LCOV - code coverage report
Current view: top level - disco/shred - fd_fec_resolver.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 406 470 86.4 %
Date: 2025-08-05 05:04:49 Functions: 9 12 75.0 %

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

Generated by: LCOV version 1.14