LCOV - code coverage report
Current view: top level - disco/shred - fd_fec_resolver.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 392 613 63.9 %
Date: 2026-11-26 02:16:43 Functions: 10 13 76.9 %

          Line data    Source code
       1             : #include "../../ballet/shred/fd_shred.h"
       2             : #include "fd_fec_set.h"
       3             : #include "../../ballet/sha256/fd_sha256.h"
       4             : #include "../../ballet/ed25519/fd_ed25519_verifier.h"
       5             : #include "../../ballet/reedsol/fd_reedsol.h"
       6             : #include "../metrics/fd_metrics.h"
       7             : #include "fd_fec_resolver.h"
       8             : #include "fd_fec_samp.h"
       9             : 
      10             : 
      11             : typedef struct __attribute__((packed)) {
      12             :   ulong slot;
      13             :   uint fec_idx;
      14             : } slot_fec_pair_t;
      15             : 
      16             : struct __attribute__((aligned(32UL))) set_ctx {
      17             :   /* The Merkle root.  This is the only unique unforgeable identifier of
      18             :      the FEC set. */
      19             :   fd_bmtree_node_t      root;
      20             : 
      21             :   /* The leader's signature of the root of the Merkle tree of the shreds
      22             :      in this FEC set.  If the leader has sent multiple signatures, this
      23             :      is the first valid one we received. */
      24             :   fd_ed25519_sig_t         sig;
      25             : 
      26             :   union {
      27             :     /* When allocated, it's in a map_chain by the root and a treap
      28             :        by (shred, FEC set idx).  When it's not allocated, it is either
      29             :        in the free list or the completed list.  Both of those slists use
      30             :        free_next. */
      31             :     struct {
      32             :       uint              map_next;
      33             :       uint              map_prev;
      34             :       uint              treap_parent;
      35             :       uint              treap_left;
      36             :       uint              treap_right;
      37             :       uint              treap_prio;
      38             :     };
      39             :     struct {
      40             :       uint              free_next;
      41             :     };
      42             :   };
      43             : 
      44             :   ulong                 slot;
      45             :   uint                  fec_set_idx;
      46             : 
      47             :   uchar                 data_variant;
      48             :   uchar                 parity_variant;
      49             : 
      50             :   /* equivoc is set to 1 if at any moment there is a context in the
      51             :      map/treap with the same (slot, fec_set_idx) as this one */
      52             :   uchar                 equivoc;
      53             : 
      54             :   ulong                 total_rx_shred_cnt;
      55             : 
      56             :   fd_fec_set_t *        set;
      57             : 
      58             :   union {
      59             :     fd_bmtree_commit_t  tree[1];
      60             :     uchar               _footprint[ FD_BMTREE_COMMIT_FOOTPRINT( FD_SHRED_MERKLE_LAYER_CNT ) ] __attribute__((aligned(FD_BMTREE_COMMIT_ALIGN)));
      61             :   };
      62             : };
      63             : typedef struct set_ctx set_ctx_t;
      64             : 
      65             : #define MAP_NAME              ctx_map
      66        9145 : #define MAP_KEY               root
      67             : #define MAP_KEY_T             fd_bmtree_node_t
      68        5580 : #define MAP_IDX_T             uint
      69        9145 : #define MAP_NEXT              map_next
      70        4573 : #define MAP_PREV              map_prev
      71        4572 : #define MAP_ELE_T             set_ctx_t
      72           0 : #define MAP_KEY_EQ(k0,k1)    (!memcmp( (k0)->hash, (k1)->hash, 32UL ))
      73        9145 : #define MAP_KEY_HASH(key,s)  (fd_hash( s, key->hash, 32UL ))
      74             : #define MAP_OPTIMIZE_RANDOM_ACCESS_REMOVAL 1
      75             : #include "../../util/tmpl/fd_map_chain.c"
      76             : 
      77             : 
      78             : #define SLIST_NAME  ctx_list
      79             : #define SLIST_ELE_T set_ctx_t
      80        9145 : #define SLIST_IDX_T uint
      81       66348 : #define SLIST_NEXT  free_next
      82             : #include "../../util/tmpl/fd_slist.c"
      83             : 
      84             : 
      85             : static inline int
      86             : slot_fec_pair_compare( slot_fec_pair_t const * q,
      87      141756 :                        set_ctx_t       const * e ) {
      88             :   /* It seems like
      89             :      return (int)( q->slot!=e->slot ?
      90             :                    q->slot    - e->slot :
      91             :                    q->fec_idx - e->fec_set_idx );
      92             :      should work, but I am concerned about overflow since this is all
      93             :      attacker controlled input. */
      94      141756 :   if( FD_LIKELY( q->slot   !=e->slot        ) ) return fd_int_if( q->slot   <e->slot,        -1, 1 );
      95      141756 :   if( FD_LIKELY( q->fec_idx!=e->fec_set_idx ) ) return fd_int_if( q->fec_idx<e->fec_set_idx, -1, 1 );
      96      141754 :   return 0;
      97      141756 : }
      98             : 
      99             : #define TREAP_NAME       ctx_treap
     100             : #define TREAP_T          set_ctx_t
     101       18292 : #define TREAP_IDX_T      uint
     102        9148 : #define TREAP_PARENT     treap_parent
     103      150902 : #define TREAP_LEFT       treap_left
     104      150901 : #define TREAP_RIGHT      treap_right
     105       52665 : #define TREAP_PRIO       treap_prio
     106           1 : #define TREAP_LT(e0,e1)  (((e0)->slot < (e1)->slot) | ( ((e0)->slot==(e1)->slot) & ((e0)->fec_set_idx < (e1)->fec_set_idx)))
     107             : #define TREAP_QUERY_T    slot_fec_pair_t const *
     108      141756 : #define TREAP_CMP(q,e)   slot_fec_pair_compare( (q), (e) )
     109             : #include "../../util/tmpl/fd_treap.c"
     110             : 
     111             : 
     112             : 
     113             : /* Once we're done with a FEC set, it goes into a map_chain and heap,
     114             :    both keyed by (slot, FEC set idx). */
     115             : 
     116             : struct done_ele {
     117             :   slot_fec_pair_t key;
     118             :   uint            heap_left; /* also used by pool when not allocated */
     119             :   uint            heap_right;
     120             :   uint            map_next;
     121             :   uint            map_prev;
     122             :   /* Ideally here we'd store the Merkle root of the one we completed,
     123             :      but there are two problems with that.  First of all, the shreds
     124             :      don't contain the Merkle root, which means we have to derive it
     125             :      with a bunch of mildly expensive hashing (about 1us per shred).
     126             :      Second of all, the Merkle root is relatively large (32 bytes).
     127             : 
     128             :      Thankfully, we have a key advantage that we can leverage.
     129             :      Equivocation detection can be probabilistic.  It's not that big of
     130             :      a deal to reject a shred with IGNORED instead of EQUIVOC as long as
     131             :      some firedancer node picks up that it is equivoc and produces the
     132             :      proof.  This suggests some kind of validator-specific sampling
     133             :      solution.  The code is factored nicely to be able to iterate on the
     134             :      sampling in the future. */
     135             :   fd_fec_samp_t   fec_sample[1];
     136             :   /* Low 56 bits of fd_tickcount() at which this FEC set completed.
     137             :      Elapsed ticks are computed modulo 2^56, retaining full tick
     138             :      precision for intervals shorter than 2^56 ticks (about 278 days at
     139             :      3 GHz). */
     140             :   ulong complete_ts : 56;
     141             :   uchar post_done_turbine_rem; /* decremented after done, but initally set to the number of repair shreds that
     142             :                                   contributed to the FEC set. We receive up to 32 unique repair shreds per FEC,
     143             :                                   so this number begins maximum at 32.  We decrement this as we receive turbine
     144             :                                   shreds post-completion, and by proxy measure how eagerly we repaired for
     145             :                                   this FEC set. */
     146             : };
     147             : typedef struct done_ele done_ele_t;
     148             : FD_STATIC_ASSERT( sizeof(done_ele_t)==40UL, done_ele_t );
     149             : #define SIG_HASH_EQUIVOC UINT_MAX
     150             : 
     151             : #define MAP_NAME              done_map
     152        4572 : #define MAP_KEY               key
     153             : #define MAP_KEY_T             slot_fec_pair_t
     154      307378 : #define MAP_IDX_T             uint
     155        5067 : #define MAP_NEXT              map_next
     156        4587 : #define MAP_PREV              map_prev
     157           0 : #define MAP_ELE_T             done_ele_t
     158         495 : #define MAP_KEY_EQ(k0,k1)     ( ((k0)->slot==(k1)->slot) & ((k0)->fec_idx==(k1)->fec_idx) )
     159      155471 : #define MAP_KEY_HASH(key,s)  ((fd_ulong_hash( (key)->slot ^ (s) ) ^ fd_uint_hash( (key)->fec_idx ^ (uint)(s>>19) )))
     160             : #define MAP_OPTIMIZE_RANDOM_ACCESS_REMOVAL 1
     161             : #include "../../util/tmpl/fd_map_chain.c"
     162             : 
     163             : #define HEAP_NAME             done_heap
     164        4572 : #define HEAP_IDX_T            uint
     165        9763 : #define HEAP_LEFT             heap_left
     166        9763 : #define HEAP_RIGHT            heap_right
     167             : #define HEAP_T                done_ele_t
     168        5191 : #define HEAP_LT(e0,e1)       (((e0)->key.slot < (e1)->key.slot) | \
     169        5191 :                             ( ((e0)->key.slot==(e1)->key.slot) & ((e0)->key.fec_idx < (e1)->key.fec_idx)))
     170             : #include "../../util/tmpl/fd_heap.c"
     171             : 
     172             : #define POOL_NAME             done_pool
     173        2016 : #define POOL_T                done_ele_t
     174             : #define POOL_IDX_T            uint
     175      262365 : #define POOL_NEXT             heap_left
     176             : #include "../../util/tmpl/fd_pool.c"
     177             : 
     178             : struct __attribute__((aligned(FD_FEC_RESOLVER_ALIGN))) fd_fec_resolver {
     179             :   /* depth stores the number of FEC sets this resolver can track
     180             :      simultaneously.  done_depth stores the depth of the done tcache,
     181             :      i.e. the number of done FEC set keys that this resolver remembers.
     182             :      partial_depth stores the minimum size of the free FEC set list.
     183             :      completed_depth stores the size of the completed FEC set list. */
     184             :   ulong depth;
     185             :   ulong partial_depth;
     186             :   ulong complete_depth;
     187             :   ulong done_depth;
     188             : 
     189             :   /* upper bound on the slot range above the root slot */
     190             :   ulong slot_max;
     191             : 
     192             :   /* expected_shred_version: discard all shreds with a shred version
     193             :      other than the specified value */
     194             :   ushort expected_shred_version;
     195             : 
     196             :   /* Test/fuzz mode: bypass Merkle+Ed25519 verification in add_shred. */
     197             :   uchar  bypass_verify;
     198             : 
     199             :   /* ctx_pool: A flat array (not an fd_pool) of the set_ctx_t
     200             :      structures used to back ctx_map, ctx_treap, and the ctx
     201             :      freelists. */
     202             :   set_ctx_t * ctx_pool;
     203             : 
     204             :   /* ctx_map: A map (using fd_map_chain) from Merkle roots to
     205             :      the context object with its relevant data for in progress FEC sets.
     206             :      This map contains at most `depth` elements at any time. */
     207             :   ctx_map_t * ctx_map;
     208             : 
     209             :   /* ctx_treap: A treap (using fd_treap) of the context objects for in
     210             :      progress FEC sets.  They are sorted by (slot, FEC index) from
     211             :      smallest to largest.  In the case of equivocation, multiple
     212             :      elements with the same key may be present, with no particular
     213             :      ordering between them. */
     214             :   ctx_treap_t ctx_treap[1];
     215             : 
     216             :   /* free_list and complete_list are slists (using fd_slist)
     217             :      of FEC set contexts that are not in ctx_map.  See the long comment
     218             :      in the header for why there are two.  In order to satisfy the
     219             :      invariants, technically we only need to store the FEC set memory,
     220             :      not the full context, but it's not that big of a difference
     221             :      (especially if partial_depth and complete_depth are small), and it
     222             :      simplifies memory management.
     223             : 
     224             :      Invariant: at every entry and exit to fd_fec_resolver_add_shred:
     225             :      - free_list has between partial_depth and partial_depth+depth
     226             :        elements.
     227             :      - complete_list has complete_depth elements
     228             :        (all these counts are inclusive). */
     229             :   ctx_list_t  free_list[1];
     230             :   ctx_list_t  complete_list[1];
     231             : 
     232             :   /* free_list_cnt: The number of items in free_list. */
     233             :   ulong free_list_cnt;
     234             : 
     235             :   /* completion_lag_hist: histogram of how much earlier repair made a
     236             :      FEC set complete.  Completion needs any FD_FEC_SHRED_CNT shreds, so
     237             :      a set that completed with R repair shreds would have needed R more
     238             :      turbine arrivals to get there on turbine alone.
     239             : 
     240             :      Best effort approximation.  It counts turbine *arrivals*, not
     241             :      distinct shred indices -- the per-set bitmaps are released at
     242             :      completion. */
     243             :   fd_histf_t completion_lag_hist[1];
     244             : 
     245             :   /* done_pool: A pool (this time using fd_pool) of the done_ele_t
     246             :      elements that back done_map and done_heap.  Invariant: each element
     247             :      is either (i) released and in the pool, or (ii) in both the
     248             :      done_map and done_heap. */
     249             :   done_ele_t * done_pool;
     250             : 
     251             :   /* done_map: A map (using fd_map_chain) mapping (slot, fec_idx) to an
     252             :      element of done_pool.  Even in the presence of equivocation, a
     253             :      specific (slot, fec_idx) tuple occurs at most once in the map,
     254             :      and the FEC sample is likely set to force_match.  In the presence
     255             :      of equivocation, the right shreds are probably being delivered
     256             :      using repair, which will bypass reading the done_map, so it doesn't
     257             :      really matter. */
     258             :   done_map_t * done_map;
     259             : 
     260             :   /* done_heap: A min heap (using fd_heap) based on (slot, fec_idx) used
     261             :      to stop tracking done elements older than slot_old, and for
     262             :      eviction in the unlikely case that we run out of elements in the
     263             :      done_map. */
     264             :   done_heap_t done_heap[1];
     265             : 
     266             :   /* slot_old: slot_old is the lowest slot for which shreds will be
     267             :      accepted.  That is any shred with slot<slot_old is rejected by
     268             :      add_shred with IGNORED.  slot_old can only increase. */
     269             :   ulong slot_old;
     270             : 
     271             :   /* seed: done_map uses seed to compute a hash of the FEC set */
     272             :   ulong seed;
     273             : 
     274             :   /* reedsol is used for calculations while adding a shred.  Its state
     275             :      outside a call to add_shred is indeterminate. */
     276             :   fd_reedsol_t  reedsol[1];
     277             : 
     278             :   /* verifier: verifies the FEC set signatures, with a small per leader
     279             :      pubkey cache.  A leader signs every set of its slots, so the few
     280             :      keys active at once repeat for hundreds of sets. */
     281             :   fd_ed25519_verifier_t * verifier;
     282             : 
     283             :   /* The footprint for the objects follows the struct and is in the same
     284             :      order as the pointers, namely:
     285             :        ctx_pool
     286             :        ctx_map
     287             :        done_pool
     288             :        done_map
     289             :        verifier */
     290             : };
     291             : 
     292             : #define ED25519_CACHE_ENT_CNT (16UL)
     293             : 
     294             : typedef struct fd_fec_resolver fd_fec_resolver_t;
     295             : 
     296             : FD_FN_PURE ulong
     297             : fd_fec_resolver_footprint( ulong depth,
     298             :                            ulong partial_depth,
     299             :                            ulong complete_depth,
     300        1008 :                            ulong done_depth ) {
     301        1008 :   if( FD_UNLIKELY( (depth==0UL) | (partial_depth==0UL) | (complete_depth==0UL) | (done_depth==0UL) ) ) return 0UL;
     302        1008 :   if( FD_UNLIKELY( (depth>UINT_MAX) | (partial_depth>UINT_MAX) | (complete_depth>UINT_MAX)         ) ) return 0UL;
     303             : 
     304        1008 :   ulong depth_sum = depth + partial_depth + complete_depth;
     305        1008 :   if( FD_UNLIKELY( depth_sum>=UINT_MAX ) ) return 0UL;
     306             : 
     307        1008 :   ulong ctx_chain_cnt  = ctx_map_chain_cnt_est ( depth      );
     308        1008 :   ulong done_chain_cnt = done_map_chain_cnt_est( done_depth );
     309             : 
     310        1008 :   ulong layout = FD_LAYOUT_INIT;
     311        1008 :   layout = FD_LAYOUT_APPEND( layout, FD_FEC_RESOLVER_ALIGN,    sizeof(fd_fec_resolver_t)                           );
     312        1008 :   layout = FD_LAYOUT_APPEND( layout, alignof(set_ctx_t),       sizeof(set_ctx_t)*depth_sum                         );
     313        1008 :   layout = FD_LAYOUT_APPEND( layout, ctx_map_align(),          ctx_map_footprint  ( ctx_chain_cnt  )               );
     314        1008 :   layout = FD_LAYOUT_APPEND( layout, done_pool_align(),        done_pool_footprint( done_depth     )               );
     315        1008 :   layout = FD_LAYOUT_APPEND( layout, done_map_align(),         done_map_footprint ( done_chain_cnt )               );
     316        1008 :   layout = FD_LAYOUT_APPEND( layout, fd_ed25519_verifier_align(), fd_ed25519_verifier_footprint( ED25519_CACHE_ENT_CNT ) );
     317             : 
     318        1008 :   return FD_LAYOUT_FINI( layout, FD_FEC_RESOLVER_ALIGN );
     319        1008 : }
     320             : 
     321        1008 : FD_FN_CONST ulong fd_fec_resolver_align( void ) { return FD_FEC_RESOLVER_ALIGN; }
     322             : 
     323             : fd_histf_t const *
     324           0 : fd_fec_resolver_completion_lag_hist( fd_fec_resolver_t const * resolver ) {
     325           0 :   return resolver->completion_lag_hist;
     326           0 : }
     327             : 
     328             : 
     329             : void *
     330             : fd_fec_resolver_new( void                    * shmem,
     331             :                      ulong                     depth,
     332             :                      ulong                     partial_depth,
     333             :                      ulong                     complete_depth,
     334             :                      ulong                     done_depth,
     335             :                      ulong                     slot_max,
     336             :                      fd_fec_set_t            * sets,
     337        1008 :                      ulong                     seed ) {
     338        1008 :   if( FD_UNLIKELY( (depth==0UL) | (partial_depth==0UL) | (complete_depth==0UL) | (done_depth==0UL) ) ) return NULL;
     339        1008 :   if( FD_UNLIKELY( (depth>UINT_MAX) | (partial_depth>UINT_MAX) | (complete_depth>UINT_MAX)         ) ) return NULL;
     340             : 
     341        1008 :   ulong depth_sum = depth + partial_depth + complete_depth;
     342        1008 :   if( FD_UNLIKELY( depth_sum>=UINT_MAX ) ) return NULL;
     343             : 
     344        1008 :   ulong ctx_chain_cnt  = ctx_map_chain_cnt_est ( depth      );
     345        1008 :   ulong done_chain_cnt = done_map_chain_cnt_est( done_depth );
     346             : 
     347             :                                                               /* round( 2^64 * ... */
     348        1008 :   ulong seed0 = fd_ulong_hash( seed +  7640891576956012809UL );  /* sqrt(2)-1 */
     349        1008 :   ulong seed1 = fd_ulong_hash( seed + 13503953896175478587UL );  /* sqrt(3)-1 */
     350        1008 :   ulong seed2 = fd_ulong_hash( seed +  4354685564936845356UL );  /* sqrt(5)-2 */
     351        1008 :   ulong seed3 = fd_ulong_hash( seed + 11912009170470909682UL );  /* sqrt(7)-2 */
     352        1008 :   ulong seed4 = fd_ulong_hash( seed +  5883258537300569243UL );  /* sqrt(11)-3 */
     353             : 
     354        1008 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
     355        1008 :   void * self        = FD_SCRATCH_ALLOC_APPEND( l, FD_FEC_RESOLVER_ALIGN,    sizeof(fd_fec_resolver_t)                           );
     356        1008 :   void * _ctx_pool   = FD_SCRATCH_ALLOC_APPEND( l, alignof(set_ctx_t),       sizeof(set_ctx_t)*depth_sum                         );
     357        1008 :   void * _ctx_map    = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),          ctx_map_footprint  ( ctx_chain_cnt  )               );
     358        1008 :   void * _done_pool  = FD_SCRATCH_ALLOC_APPEND( l, done_pool_align(),        done_pool_footprint( done_depth     )               );
     359        1008 :   void * _done_map   = FD_SCRATCH_ALLOC_APPEND( l, done_map_align(),         done_map_footprint ( done_chain_cnt )               );
     360        1008 :   void * _verifier   = FD_SCRATCH_ALLOC_APPEND( l, fd_ed25519_verifier_align(), fd_ed25519_verifier_footprint( ED25519_CACHE_ENT_CNT ) );
     361        1008 :   FD_SCRATCH_ALLOC_FINI( l, FD_FEC_RESOLVER_ALIGN );
     362             : 
     363        1008 :   fd_fec_resolver_t * resolver = (fd_fec_resolver_t *)self;
     364        1008 :   void * _ctx_treap     = resolver->ctx_treap;
     365        1008 :   void * _free_list     = resolver->free_list;
     366        1008 :   void * _complete_list = resolver->complete_list;
     367        1008 :   void * _done_heap     = resolver->done_heap;
     368             : 
     369        1008 :   if( FD_UNLIKELY( !ctx_map_new         ( _ctx_map, ctx_chain_cnt, seed0          ) ) ) { FD_LOG_WARNING(( "ctx_map_new fail"       )); return NULL; }
     370        1008 :   if( FD_UNLIKELY( !ctx_treap_new       ( _ctx_treap, depth_sum                   ) ) ) { FD_LOG_WARNING(( "ctx_treap_new fail"     )); return NULL; }
     371        1008 :   if( FD_UNLIKELY( !ctx_list_new        ( _free_list                              ) ) ) { FD_LOG_WARNING(( "ctx_list_new fail"      )); return NULL; }
     372        1008 :   if( FD_UNLIKELY( !ctx_list_new        ( _complete_list                          ) ) ) { FD_LOG_WARNING(( "ctx_list_new fail"      )); return NULL; }
     373        1008 :   if( FD_UNLIKELY( !done_pool_new       ( _done_pool, done_depth                  ) ) ) { FD_LOG_WARNING(( "done_pool_new fail"     )); return NULL; }
     374        1008 :   if( FD_UNLIKELY( !done_map_new        ( _done_map, done_chain_cnt, seed1        ) ) ) { FD_LOG_WARNING(( "done_map_new fail"      )); return NULL; }
     375        1008 :   if( FD_UNLIKELY( !done_heap_new       ( _done_heap, done_depth                  ) ) ) { FD_LOG_WARNING(( "done_heap_new fail"     )); return NULL; }
     376        1008 :   if( FD_UNLIKELY( !fd_ed25519_verifier_new( _verifier, ED25519_CACHE_ENT_CNT, seed4 ) ) ) { FD_LOG_WARNING(( "ed25519_verifier_new fail" )); return NULL; }
     377             : 
     378        1008 :   fd_histf_join( fd_metrics_histf_new_ns( resolver->completion_lag_hist, FD_MHIST_EDGES_NS( SHRED, REPAIR_LEAD_DELAY_NANOS ) ) );
     379             : 
     380        1008 :   set_ctx_t * ctx_pool = (set_ctx_t *)_ctx_pool;
     381        1008 :   fd_memset( ctx_pool, '\0', sizeof(set_ctx_t)*depth_sum );
     382       49113 :   for( ulong i=0UL; i<depth_sum; i++ ) ctx_pool[i].set = sets + i;
     383        1008 :   ctx_treap_seed( ctx_pool, depth_sum, seed2 );
     384             : 
     385             :   /* Initialize all the lists */
     386        1008 :   ctx_list_t * free_list     = ctx_list_join( _free_list     );    FD_TEST( free_list    ==resolver->free_list     );
     387        1008 :   ctx_list_t * complete_list = ctx_list_join( _complete_list );    FD_TEST( complete_list==resolver->complete_list );
     388             : 
     389       41039 :   for( ulong i=0UL;                 i<depth+partial_depth; i++ ) { ctx_list_idx_push_tail( free_list,     i, ctx_pool ); }
     390        9051 :   for( ulong i=depth+partial_depth; i<depth_sum;           i++ ) { ctx_list_idx_push_tail( complete_list, i, ctx_pool ); }
     391        1008 :   ctx_list_leave( complete_list );
     392        1008 :   ctx_list_leave( free_list     );
     393             : 
     394        1008 :   resolver->depth                  = depth;
     395        1008 :   resolver->partial_depth          = partial_depth;
     396        1008 :   resolver->complete_depth         = complete_depth;
     397        1008 :   resolver->done_depth             = done_depth;
     398        1008 :   resolver->slot_max               = slot_max;
     399        1008 :   resolver->expected_shred_version = 0;
     400        1008 :   resolver->bypass_verify          = 0;
     401        1008 :   resolver->free_list_cnt          = depth+partial_depth;
     402        1008 :   resolver->slot_old               = 0UL;
     403        1008 :   resolver->seed                   = seed3;
     404        1008 :   return shmem;
     405        1008 : }
     406             : 
     407             : fd_fec_resolver_t *
     408        1007 : fd_fec_resolver_join( void * shmem ) {
     409        1007 :   fd_fec_resolver_t * resolver = (fd_fec_resolver_t *)shmem;
     410        1007 :   ulong depth          = resolver->depth;
     411        1007 :   ulong partial_depth  = resolver->partial_depth;
     412        1007 :   ulong complete_depth = resolver->complete_depth;
     413        1007 :   ulong done_depth     = resolver->done_depth;
     414             : 
     415        1007 :   ulong depth_sum = depth + partial_depth + complete_depth;
     416        1007 :   if( FD_UNLIKELY( depth_sum>=UINT_MAX ) ) return NULL;
     417             : 
     418        1007 :   ulong ctx_chain_cnt  = ctx_map_chain_cnt_est ( depth      );
     419        1007 :   ulong done_chain_cnt = done_map_chain_cnt_est( done_depth );
     420             : 
     421        1007 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
     422        1007 :   /*     self     */   FD_SCRATCH_ALLOC_APPEND( l, FD_FEC_RESOLVER_ALIGN,    sizeof(fd_fec_resolver_t)                           );
     423        1007 :   void * _ctx_pool   = FD_SCRATCH_ALLOC_APPEND( l, alignof(set_ctx_t),       sizeof(set_ctx_t)*depth_sum                         );
     424        1007 :   void * _ctx_map    = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),          ctx_map_footprint  ( ctx_chain_cnt  )               );
     425        1007 :   void * _done_pool  = FD_SCRATCH_ALLOC_APPEND( l, done_pool_align(),        done_pool_footprint( done_depth     )               );
     426        1007 :   void * _done_map   = FD_SCRATCH_ALLOC_APPEND( l, done_map_align(),         done_map_footprint ( done_chain_cnt )               );
     427        1007 :   void * _verifier   = FD_SCRATCH_ALLOC_APPEND( l, fd_ed25519_verifier_align(), fd_ed25519_verifier_footprint( ED25519_CACHE_ENT_CNT ) );
     428        1007 :   FD_SCRATCH_ALLOC_FINI( l, FD_FEC_RESOLVER_ALIGN );
     429             : 
     430        1007 :   resolver->ctx_pool      = (set_ctx_t *)_ctx_pool;
     431        1007 :   resolver->ctx_map       = ctx_map_join         ( _ctx_map   );  if( FD_UNLIKELY( !resolver->ctx_map       ) ) return NULL;
     432        1007 :   resolver->done_pool     = done_pool_join       ( _done_pool );  if( FD_UNLIKELY( !resolver->done_pool     ) ) return NULL;
     433        1007 :   resolver->done_map      = done_map_join        ( _done_map  );  if( FD_UNLIKELY( !resolver->done_map      ) ) return NULL;
     434        1007 :   resolver->verifier      = fd_ed25519_verifier_join( _verifier );  if( FD_UNLIKELY( !resolver->verifier   ) ) return NULL;
     435        1007 :   if( FD_UNLIKELY(      ctx_treap_join( resolver->ctx_treap     )!=      resolver->ctx_treap     ) ) return NULL;
     436        1007 :   if( FD_UNLIKELY(      ctx_list_join ( resolver->free_list     )!=      resolver->free_list     ) ) return NULL;
     437        1007 :   if( FD_UNLIKELY(      ctx_list_join ( resolver->complete_list )!=      resolver->complete_list ) ) return NULL;
     438        1007 :   if( FD_UNLIKELY(      done_heap_join( resolver->done_heap     )!=      resolver->done_heap     ) ) return NULL;
     439             : 
     440        1007 :   return resolver;
     441        1007 : }
     442             : 
     443             : void
     444             : fd_fec_resolver_set_shred_version( fd_fec_resolver_t * resolver,
     445        1007 :                                    ushort              expected_shred_version ) {
     446        1007 :   resolver->expected_shred_version = expected_shred_version;
     447        1007 : }
     448             : 
     449             : void
     450             : fd_fec_resolver_set_bypass_verify( fd_fec_resolver_t * resolver,
     451        1007 :                                    int                 bypass_verify ) {
     452        1007 :   resolver->bypass_verify = (uchar)!!bypass_verify;
     453        1007 : }
     454             : 
     455             : void
     456             : fd_fec_resolver_advance_slot_old( fd_fec_resolver_t * resolver,
     457        1007 :                                   ulong               slot_old ) {
     458        1007 :   if( FD_UNLIKELY( slot_old <= resolver->slot_old ) ) return;
     459        1007 :   resolver->slot_old = slot_old;
     460             : 
     461             :   /* Remove from done map */
     462        1007 :   done_heap_t * done_heap = resolver->done_heap;
     463        1007 :   done_map_t  * done_map  = resolver->done_map;
     464        1007 :   done_ele_t  * done_pool = resolver->done_pool;
     465             : 
     466        1007 :   while( done_heap_ele_cnt( done_heap ) ) {
     467           0 :     done_ele_t * min_ele = done_heap_ele_peek_min( done_heap, done_pool );
     468           0 :     if( FD_UNLIKELY( min_ele->key.slot>=slot_old ) ) break;
     469           0 :     done_map_ele_remove_fast( done_map,  min_ele, done_pool );
     470           0 :     done_heap_idx_remove_min( done_heap,          done_pool );
     471           0 :     done_pool_ele_release   ( done_pool, min_ele            );
     472           0 :   }
     473             : 
     474             :   /* Remove from in progress map */
     475        1007 :   ctx_map_t   * ctx_map       = resolver->ctx_map;
     476        1007 :   ctx_treap_t * ctx_treap     = resolver->ctx_treap;
     477        1007 :   set_ctx_t   * ctx_pool      = resolver->ctx_pool;
     478        1007 :   ctx_list_t  * free_list     = resolver->free_list;
     479             : 
     480        1007 :   ctx_treap_fwd_iter_t next;
     481        1007 :   for( ctx_treap_fwd_iter_t iter=ctx_treap_fwd_iter_init( ctx_treap, ctx_pool ); !ctx_treap_fwd_iter_done( iter ); iter=next ) {
     482           0 :     next = ctx_treap_fwd_iter_next( iter, ctx_pool );
     483           0 :     set_ctx_t * min_ele = ctx_treap_fwd_iter_ele( iter, ctx_pool );
     484           0 :     if( FD_UNLIKELY( min_ele->slot>=slot_old ) ) break;
     485             : 
     486           0 :     ctx_treap_ele_remove   ( ctx_treap, min_ele, ctx_pool );
     487           0 :     ctx_map_ele_remove_fast( ctx_map,   min_ele, ctx_pool );
     488           0 :     ctx_list_ele_push_head ( free_list, min_ele, ctx_pool );
     489           0 :     resolver->free_list_cnt++;
     490           0 :   }
     491        1007 : }
     492             : 
     493             : static inline void
     494             : ensure_done_pool_free( done_ele_t  * done_pool,
     495             :                        done_heap_t * done_heap,
     496        4572 :                        done_map_t  * done_map ) {
     497        4572 :   if( FD_UNLIKELY( !done_pool_free( done_pool ) ) ) {
     498             :     /* Done map is full, so we'll forget about the oldest slot */
     499           0 :     ulong worst_idx = done_heap_idx_peek_min( done_heap );
     500           0 :     FD_TEST( worst_idx!=done_heap_idx_null() ); /* Done pool can't be empty and full at the same time */
     501           0 :     done_heap_idx_remove_min( done_heap,            done_pool );
     502           0 :     done_map_idx_remove_fast( done_map,  worst_idx, done_pool );
     503           0 :     done_pool_idx_release( done_pool, worst_idx );
     504             :     /* Now it's not empty */
     505           0 :   }
     506        4572 : }
     507             : 
     508             : 
     509             : int
     510             : fd_fec_resolver_add_shred( fd_fec_resolver_t         * resolver,
     511             :                            fd_shred_t const          * shred,
     512             :                            ulong                       shred_sz,
     513             :                            ulong                       max_shred_idx,
     514             :                            uint                        source,
     515             :                            uchar const               * leader_pubkey,
     516             :                            fd_fec_set_t const      * * out_fec_set,
     517             :                            fd_shred_t const        * * out_shred,
     518             :                            fd_bmtree_node_t          * out_merkle_root,
     519      146328 :                            fd_fec_resolver_spilled_t * out_spilled      ) {
     520      146328 :   FD_TEST( source<=FD_FEC_RESOLVER_SHRED_SRC_BAD_REPAIR );
     521             : 
     522             :   /* Unpack variables */
     523      146328 :   ulong partial_depth = resolver->partial_depth;
     524             : 
     525      146328 :   ctx_list_t  * free_list     = resolver->free_list;
     526      146328 :   ctx_list_t  * complete_list = resolver->complete_list;
     527      146328 :   ctx_map_t   * ctx_map       = resolver->ctx_map;
     528      146328 :   ctx_treap_t * ctx_treap     = resolver->ctx_treap;
     529      146328 :   set_ctx_t   * ctx_pool      = resolver->ctx_pool;
     530      146328 :   done_map_t  * done_map      = resolver->done_map;
     531      146328 :   done_ele_t  * done_pool     = resolver->done_pool;
     532      146328 :   done_heap_t * done_heap     = resolver->done_heap;
     533             : 
     534      146328 :   fd_reedsol_t * reedsol       = resolver->reedsol;
     535             : 
     536             :   /* Invariants:
     537             :       * each set_ctx_t is in exactly one of ctx_map, freelist, or
     538             :         complete_list */
     539             : 
     540             :   /* Is this shred for a slot we've already rooted or otherwise don't
     541             :      care about? */
     542      146328 :   if( FD_UNLIKELY( shred->slot<resolver->slot_old ) ) return FD_FEC_RESOLVER_SHRED_IGNORED;
     543      146328 :   if( FD_UNLIKELY( resolver->slot_max && shred->slot>=resolver->slot_old+resolver->slot_max ) ) return FD_FEC_RESOLVER_SHRED_IGNORED;
     544             : 
     545             :   /* Do a bunch of quick validity checks */
     546      146328 :   if( FD_UNLIKELY( shred->version!=resolver->expected_shred_version  ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     547      146328 :   if( FD_UNLIKELY( shred_sz<fd_shred_sz( shred )                     ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     548      146328 :   if( FD_UNLIKELY( shred->idx>=max_shred_idx                         ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     549      146328 :   if( FD_UNLIKELY( shred->fec_set_idx>max_shred_idx-FD_FEC_SHRED_CNT ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     550      146328 :   if( FD_UNLIKELY( shred->idx-shred->fec_set_idx>=FD_FEC_SHRED_CNT   ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     551      146328 :   if( FD_UNLIKELY( shred->fec_set_idx%FD_FEC_SHRED_CNT!=0UL          ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     552             : 
     553      146328 :   uchar variant    = shred->variant;
     554      146328 :   uchar shred_type = fd_shred_type( variant );
     555             : 
     556      146328 :   int is_data_shred = fd_shred_is_data( shred_type );
     557             : 
     558      146328 :   if( !is_data_shred ) { /* Roughly 50/50 branch */
     559           0 :     if( FD_UNLIKELY( (shred->code.data_cnt!=FD_FEC_SHRED_CNT) | (shred->code.code_cnt!=FD_FEC_SHRED_CNT) ) )
     560           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     561           0 :     if( FD_UNLIKELY( shred->code.idx>=FD_FEC_SHRED_CNT            ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     562           0 :     if( FD_UNLIKELY( shred->code.idx!=shred->idx%FD_FEC_SHRED_CNT ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     563             :     /* We want to ignore parity shreds corresponding to data shreds for
     564             :        which the parent slot is something we've already pruned (see
     565             :        below), but the parent offset is only a field in data shreds.  We
     566             :        could do it at reconstruction time, but then there's not a good
     567             :        return code.  It would be like a FEC_FATAL_REJECT, but returning
     568             :        IGNORED.  So instead we just snoop into the parity data and pick
     569             :        out the parent_off.  If all data shreds have the same value for
     570             :        parent_off, then all parity shreds will have that same value at
     571             :        the corresponding bytes.  If the data shreds have different
     572             :        values, this comparison is non-sensical, but we'll reject the FEC
     573             :        set later. */
     574           0 :     ushort parent_off = FD_LOAD( ushort, fd_shred_code_payload( shred )+0x53UL-0x40UL );
     575           0 :     if( FD_UNLIKELY( shred->slot-parent_off<resolver->slot_old ) ) return FD_FEC_RESOLVER_SHRED_IGNORED;
     576      146328 :   } else {
     577             :     /* if the shred's parent is for a slot we've already pruned, ignore it. */
     578      146328 :     if( FD_UNLIKELY( shred->slot-shred->data.parent_off<resolver->slot_old ) ) return FD_FEC_RESOLVER_SHRED_IGNORED;
     579             : 
     580             :     /* if it has slot complete, it must be the last one in the FEC. */
     581      146328 :     if( FD_UNLIKELY( (shred->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE) && ((1UL+shred->idx) % FD_FEC_SHRED_CNT) ) ) {
     582           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     583           0 :     }
     584             : 
     585             :     /* if it has data complete, it must be the last data shred in the FEC set
     586             :        https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/ledger/src/shred/filter.rs#L346-L354 */
     587      146328 :     if( FD_UNLIKELY( ( shred->data.flags & FD_SHRED_DATA_FLAG_DATA_COMPLETE ) &&
     588      146328 :                      ( shred->idx != ( shred->fec_set_idx + ( FD_FEC_SHRED_CNT - 1UL ) ) ) ) ) {
     589           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     590           0 :     }
     591      146328 :   }
     592             : 
     593      146328 :   if( FD_UNLIKELY( !fd_shred_is_chained( shred_type ) ) ) {
     594             :     /* Reject any legacy and unchained shreds */
     595           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
     596           0 :   }
     597             : 
     598             :   /* For the purposes of the shred header, tree_depth means the number
     599             :      of nodes, counting the leaf but excluding the root.  For bmtree,
     600             :      depth means the number of layers, which counts both. */
     601      146328 :   ulong tree_depth           = fd_shred_merkle_cnt( variant ); /* In [0, 15] */
     602             : 
     603             :   /* in_type_idx is between [0, code.data_cnt) or [0, code.code_cnt),
     604             :      where data_cnt <= FD_FEC_SHRED_CNT and code_cnt <= FD_FEC_SHRED_CNT
     605             :      On the other hand, shred_idx, goes from [0, code.data_cnt +
     606             :      code.code_cnt), with all the data shreds having
     607             :      shred_idx < code.data_cnt and all the parity shreds having
     608             :      shred_idx >= code.data_cnt. */
     609      146328 :   ulong in_type_idx = fd_ulong_if( is_data_shred, shred->idx - shred->fec_set_idx, shred->code.idx );
     610      146328 :   ulong shred_idx   = fd_ulong_if( is_data_shred, in_type_idx, in_type_idx + shred->code.data_cnt  );
     611             : 
     612      146328 :   if( FD_UNLIKELY( ( shred->fec_set_idx % FD_FEC_SHRED_CNT ) != 0UL ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     613      146328 :   if( FD_UNLIKELY( in_type_idx >= FD_FEC_SHRED_CNT ) )                  return FD_FEC_RESOLVER_SHRED_REJECTED;
     614             : 
     615             :   /* This, combined with the check on shred->code.data_cnt implies that
     616             :      shred_idx is in [0, 2*FD_FEC_SHRED_CNT). */
     617             : 
     618      146328 :   if( FD_UNLIKELY( tree_depth!=FD_SHRED_MERKLE_LAYER_CNT-1UL ) ) return FD_FEC_RESOLVER_SHRED_REJECTED;
     619             : 
     620             :   /* Now we're done with all the simple, quick checks */
     621      146328 :   int is_repair = source==FD_FEC_RESOLVER_SHRED_SRC_REPAIR && is_data_shred;
     622             : 
     623      146328 :   set_ctx_t * ctx = NULL;
     624             : 
     625             :   /* If we detect a different Merkle root for the same (slot, FEC set
     626             :      idx), it means either the shred is invalid, or the leader is
     627             :      equivocating.  We can't tell which without verifying the shred
     628             :      though. */
     629      146328 :   int equivoc_or_invalid = 0;
     630             : 
     631      146328 :   slot_fec_pair_t const slot_fec_pair[1] = {{ .slot = shred->slot, .fec_idx = shred->fec_set_idx }};
     632             : 
     633             :   /* If it's from Turbine, we'll first check if we've already completed
     634             :      the FEC set (likely case) or another FEC set for the same (slot,
     635             :      FEC set idx) pair (unlikely case).  Either case will lead to
     636             :      rejecting the shred, but possibly later via the equivoc_or_invalid
     637             :      if we need to validate it. */
     638      146328 :   if( FD_LIKELY( !is_repair ) ) {
     639             :     /* Most likely, it's just done. */
     640      146326 :     done_ele_t * done_ele = done_map_ele_query( done_map, slot_fec_pair, NULL, done_pool );
     641      146326 :     if( FD_LIKELY( done_ele ) ) {
     642           0 :       if( FD_LIKELY( fd_fec_samp_matches( done_ele->fec_sample, resolver->seed, shred ) ) ) {
     643           0 :         if( FD_UNLIKELY( (source==FD_FEC_RESOLVER_SHRED_SRC_TURBINE) & (done_ele->post_done_turbine_rem>0L) ) ) {
     644             :           /* Turbine has now supplied as many shreds for this set as
     645             :              repair contributed, so this is approx. when the set would
     646             :              have completed on turbine alone. */
     647           0 :           done_ele->post_done_turbine_rem--;
     648           0 :           if( FD_UNLIKELY( done_ele->post_done_turbine_rem==0 ) ) {
     649           0 :             ulong lag = ((ulong)fd_tickcount() - (ulong)done_ele->complete_ts) & ((1UL<<56)-1UL);
     650           0 :             fd_histf_sample( resolver->completion_lag_hist, lag );
     651           0 :           }
     652           0 :         }
     653           0 :         return FD_FEC_RESOLVER_SHRED_IGNORED;
     654           0 :       }
     655             : 
     656           0 :       equivoc_or_invalid = 1;
     657           0 :     }
     658             :     /* There could be 0, 1, or >1 contexts in the treap with this (slot,
     659             :        FEC set index).  If there are >1, this sets ctx to an arbitrary
     660             :        one, which may or may not have the Merkle root that matches this
     661             :        shred.  Regardless, we'll reject it with equivoc_or_invalid. */
     662      146326 :     ctx = ctx_treap_ele_query( ctx_treap, slot_fec_pair, ctx_pool );
     663      146326 :     if( FD_LIKELY( ctx ) ) equivoc_or_invalid |= ctx->equivoc;
     664      146326 :   }
     665             : 
     666             : 
     667      146328 :   ulong reedsol_protected_sz = 1115UL + FD_SHRED_DATA_HEADER_SZ - FD_SHRED_SIGNATURE_SZ - FD_SHRED_MERKLE_NODE_SZ*tree_depth
     668      146328 :                                       - FD_SHRED_MERKLE_ROOT_SZ*fd_shred_is_chained ( shred_type )
     669      146328 :                                       - FD_SHRED_SIGNATURE_SZ  *fd_shred_is_resigned( shred_type); /* In [743, 1139] conservatively*/
     670      146328 :   ulong data_merkle_protected_sz   = reedsol_protected_sz + FD_SHRED_MERKLE_ROOT_SZ*fd_shred_is_chained( shred_type );
     671      146328 :   ulong parity_merkle_protected_sz = reedsol_protected_sz + FD_SHRED_MERKLE_ROOT_SZ*fd_shred_is_chained( shred_type )
     672      146328 :                                                           + FD_SHRED_CODE_HEADER_SZ - FD_ED25519_SIG_SZ;
     673      146328 :   ulong merkle_protected_sz  = fd_ulong_if( is_data_shred, data_merkle_protected_sz, parity_merkle_protected_sz );
     674             : 
     675      146328 :   fd_bmtree_node_t leaf[1];
     676             : 
     677      146328 :   fd_bmtree_hash_leaf( leaf, (uchar const *)shred + sizeof(fd_ed25519_sig_t), merkle_protected_sz, FD_BMTREE_LONG_PREFIX_SZ );
     678             : 
     679             : 
     680      146328 :   if( FD_UNLIKELY( is_repair ) ) {
     681             :     /* If it's coming from repair, we're willing to do the work to
     682             :        compute the Merkle root and lookup the ctx by Merkle root. */
     683           0 :     fd_bmtree_node_t _root[1];
     684           0 :     fd_bmtree_node_t * root = fd_bmtree_from_proof( leaf, shred_idx, _root, (uchar const *)fd_shred_merkle_nodes( shred ),
     685           0 :                                                     tree_depth, FD_SHRED_MERKLE_NODE_SZ, FD_BMTREE_LONG_PREFIX_SZ );
     686             : 
     687           0 :     ctx = ctx_map_ele_query( ctx_map, root, NULL, ctx_pool );
     688           0 :   }
     689             : 
     690      146328 :   if( FD_UNLIKELY( !ctx ) ) { /* This is the first shred in the FEC set */
     691             : 
     692        4573 :     if( FD_UNLIKELY( resolver->free_list_cnt<=partial_depth ) ) {
     693             :       /* Packet loss is really high and we have a lot of in-progress FEC
     694             :          sets that we haven't been able to finish.  Evict the context
     695             :          with the highest (slot, FEC idx).  This is the one that is the
     696             :          farthest away from what we're currently replaying, which means
     697             :          we have the longest time to request it via repair if we
     698             :          actually need it.  This also handles the case where a leader
     699             :          sends some shreds from their slots that are far in the future
     700             :          in this epoch. */
     701           0 :       set_ctx_t * victim_ctx = ctx_treap_rev_iter_ele( ctx_treap_rev_iter_init( ctx_treap, ctx_pool ), ctx_pool );
     702             : 
     703           0 :       if( FD_LIKELY( out_spilled ) ) {
     704           0 :         out_spilled->slot         = victim_ctx->slot;
     705           0 :         out_spilled->fec_set_idx  = victim_ctx->fec_set_idx;
     706           0 :         *out_spilled->merkle_root = victim_ctx->root;
     707           0 :       }
     708             : 
     709             :       /* Remove from treap and map, then add to free_list */
     710           0 :       ctx_treap_ele_remove   ( ctx_treap, victim_ctx, ctx_pool );
     711           0 :       ctx_map_ele_remove_fast( ctx_map,   victim_ctx, ctx_pool );
     712             : 
     713           0 :       ctx_list_ele_push_tail ( free_list, victim_ctx, ctx_pool );
     714           0 :       resolver->free_list_cnt++;
     715             : 
     716           0 :       FD_MCNT_INC( SHRED, FEC_INCOMPLETE_EVICTED, 1UL );
     717           0 :     }
     718             :     /* Now we know |free_list|>partial_depth */
     719             : 
     720        4573 :     ctx = ctx_list_ele_pop_head( free_list, ctx_pool );
     721        4573 :     resolver->free_list_cnt--;
     722             : 
     723             :     /* Now we need to derive the root of the Merkle tree and verify the
     724             :        signature to prevent a DOS attack just by sending lots of invalid
     725             :        shreds.  If this shred came from repair, we're redoing
     726             :        computation, but the code is gross otherwise. */
     727        4573 :     fd_bmtree_commit_t * tree;
     728        4573 :     tree = fd_bmtree_commit_init( ctx->_footprint, FD_SHRED_MERKLE_NODE_SZ, FD_BMTREE_LONG_PREFIX_SZ, FD_SHRED_MERKLE_LAYER_CNT );
     729        4573 :     FD_TEST( tree==ctx->tree );
     730             : 
     731        4573 :     fd_bmtree_node_t _root[1] = {0};
     732             : 
     733        4573 :     fd_shred_merkle_t const * proof = fd_shred_merkle_nodes( shred );
     734        4573 :     int rv = fd_bmtree_commitp_insert_with_proof( tree, shred_idx, leaf, (uchar const *)proof, tree_depth, _root );
     735        4573 :     if( FD_UNLIKELY( !rv ) ) {
     736           0 :       ctx_list_ele_push_head( free_list, ctx, ctx_pool );
     737           0 :       resolver->free_list_cnt++;
     738           0 :       FD_MCNT_INC( SHRED, INITIAL_REJECTED, 1UL );
     739           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     740           0 :     }
     741             : 
     742        4573 :     if( FD_LIKELY( !resolver->bypass_verify ) ) {
     743           0 :       if( FD_UNLIKELY( FD_ED25519_SUCCESS != fd_ed25519_verifier_verify( resolver->verifier, _root->hash, 32UL, shred->signature, leader_pubkey ) ) ) {
     744           0 :         ctx_list_ele_push_head( free_list, ctx, ctx_pool );
     745           0 :         resolver->free_list_cnt++;
     746           0 :         FD_MCNT_INC( SHRED, INITIAL_REJECTED, 1UL );
     747           0 :         return FD_FEC_RESOLVER_SHRED_REJECTED;
     748           0 :       }
     749           0 :     }
     750             : 
     751             :     /* Copy the merkle root into the output arg. */
     752        4573 :     if( FD_LIKELY( out_merkle_root ) ) memcpy( out_merkle_root, _root, sizeof(fd_bmtree_node_t) );
     753             : 
     754        4573 :     if( FD_UNLIKELY( equivoc_or_invalid ) ) {
     755             :       /* It wasn't invalid, so it must be equivoc.  As a reminder, this
     756             :          is the case where is_repair==0, we had the (slot, FEC set idx)
     757             :          in the done_map, and this shred didn't match the FEC sample. */
     758           0 :       ctx_list_ele_push_head( free_list, ctx, ctx_pool );
     759           0 :       resolver->free_list_cnt++;
     760             :       /* We want to record that we've sigverified the shred somewhere so
     761             :          that if an attacker sends it to us again, we don't have to
     762             :          verify it again.  We do that by inserting it into the done_map
     763             :          with force_match. */
     764           0 :       done_ele_t * done = done_map_ele_query( done_map, slot_fec_pair, NULL, done_pool );
     765           0 :       if( FD_LIKELY( done ) ) fd_fec_samp_set_force_match( done->fec_sample );
     766           0 :       else {
     767             :         /* This case can't happen right now, but keep it in case some
     768             :            code gets added that could evict from the done_map between
     769             :            the !is_repair query and here. */
     770           0 :         ensure_done_pool_free( done_pool, done_heap, done_map );
     771             : 
     772           0 :         done = done_pool_ele_acquire( done_pool );
     773             : 
     774           0 :         done->key.slot              = shred->slot;
     775           0 :         done->key.fec_idx           = shred->fec_set_idx;
     776           0 :         done->complete_ts           = 0UL;
     777           0 :         done->post_done_turbine_rem = 0U;
     778           0 :         fd_fec_samp_set_force_match( done->fec_sample );
     779             : 
     780           0 :         done_heap_ele_insert( done_heap, done, done_pool );
     781           0 :         done_map_ele_insert ( done_map,  done, done_pool );
     782           0 :       }
     783             : 
     784           0 :       return FD_FEC_RESOLVER_SHRED_EQUIVOC;
     785           0 :     }
     786             :     /* Do we already have something in the treap for the same (slot, FEC
     787             :        idx) of this shred?  If so, mark this one and the old one as
     788             :        equivocating.  If we have multiple, marking the old one will be a
     789             :        no-op, which means we don't care which one query returns. */
     790        4573 :     uchar equivoc = 0;
     791        4573 :     set_ctx_t * equivoc_ctx = ctx_treap_ele_query( ctx_treap, slot_fec_pair, ctx_pool );
     792        4573 :     if( FD_UNLIKELY( equivoc_ctx ) ) {
     793           0 :       equivoc_ctx->equivoc = 1;
     794           0 :       equivoc              = 1;
     795           0 :     }
     796             : 
     797             :     /* This seems like a legitimate FEC set, so we populate the rest of
     798             :        the fields, then add it to the map and treap. */
     799        4573 :     memcpy( ctx->sig, shred->signature, 64UL );
     800        4573 :     ctx->slot               = shred->slot;
     801        4573 :     ctx->fec_set_idx        = shred->fec_set_idx;
     802        4573 :     ctx->data_variant       = fd_uchar_if(  is_data_shred, variant, fd_shred_variant( fd_shred_swap_type( shred_type ), (uchar)tree_depth ) );
     803        4573 :     ctx->parity_variant     = fd_uchar_if( !is_data_shred, variant, fd_shred_variant( fd_shred_swap_type( shred_type ), (uchar)tree_depth ) );
     804        4573 :     ctx->equivoc            = equivoc;
     805        4573 :     ctx->total_rx_shred_cnt = 0UL;
     806        4573 :     ctx->root               = *_root;
     807             : 
     808             :     /* Reset the FEC set */
     809        4573 :     ctx->set->data_shred_rcvd    = 0U;
     810        4573 :     ctx->set->parity_shred_rcvd  = 0U;
     811        4573 :     ctx->set->turbine_shred_rcvd = 0UL;
     812        4573 :     ctx->set->repair_shred_rcvd  = 0UL;
     813             : 
     814        4573 :     ctx_map_ele_insert  ( ctx_map,   ctx, ctx_pool );
     815        4573 :     ctx_treap_ele_insert( ctx_treap, ctx, ctx_pool );
     816             : 
     817      141755 :   } else {
     818             :     /* This is not the first shred in the set */
     819             : 
     820             :     /* Verify that the shred's slot and fec_set_idx match the context
     821             :        established by the first shred. */
     822      141755 :     if( FD_UNLIKELY( shred->slot!=ctx->slot || shred->fec_set_idx!=ctx->fec_set_idx ) ) {
     823           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     824           0 :     }
     825             : 
     826             :     /* First ensure that all the shreds in the FEC set have consistent
     827             :        variants.  They all must have the same tree_depth and the same
     828             :        chained/not chained, resigned/not resigned bits. */
     829      141755 :     if( FD_UNLIKELY( variant!=fd_uchar_if( is_data_shred, ctx->data_variant, ctx->parity_variant ) ) ) {
     830           0 :       return FD_FEC_RESOLVER_SHRED_REJECTED;
     831           0 :     }
     832             : 
     833      141755 :     int matching_root = 1;
     834      141755 :     fd_bmtree_node_t _root[1];
     835      141755 :     int matching_sig  = fd_memeq( ctx->sig, shred->signature, 64UL );
     836      141758 :     if( FD_UNLIKELY( resolver->bypass_verify ) ) {
     837      141758 :       *_root = ctx->root;
     838      141758 :       matching_sig = 1;
     839 >1844*10^16 :     } else {
     840 >1844*10^16 :       fd_shred_merkle_t const * proof = fd_shred_merkle_nodes( shred );
     841 >1844*10^16 :       int rv = fd_bmtree_commitp_insert_with_proof( ctx->tree, shred_idx, leaf, (uchar const *)proof, tree_depth, _root );
     842 >1844*10^16 :       if( FD_UNLIKELY( !rv ) ) {
     843             :         /* This means we've gotten a shred with a different Merkle root
     844             :            than ctx.  This can only happen in the Turbine case, because
     845             :            otherwise ctx comes from the Merkle-root keyed map.  We need
     846             :            to check the signature before we know if it's EQUIVOC or
     847             :            REJECTED. */
     848           0 :         fd_bmtree_node_t * root = fd_bmtree_from_proof( leaf, shred_idx, _root, (uchar const *)fd_shred_merkle_nodes( shred ),
     849           0 :                                                         tree_depth, FD_SHRED_MERKLE_NODE_SZ, FD_BMTREE_LONG_PREFIX_SZ );
     850           0 :         (void)root; /* it has to be ==_root.  If it were going to fail,
     851             :                        it would have happened earlier. */
     852           0 :         matching_root = 0;
     853           0 :       }
     854 >1844*10^16 :     }
     855             : 
     856      141755 :     if( FD_UNLIKELY( !(matching_root & matching_sig ) ) ) {
     857             :       /* There are a few things that could be going on here.
     858             :          1. A malicious validator could be producing an alternative
     859             :             signature for the same Merkle root
     860             :          2. A malicious validator could be equivocating (valid
     861             :             signature, different Merkle root)
     862             :          3. Someone could be sending us a shred from a FEC set the
     863             :             leader previously signed, but with a clobbered signature
     864             :          4. Someone could be sending us total junk.
     865             :          Note: if bypass_verify is true, this branch can't be taken. */
     866           0 :       if( FD_UNLIKELY( FD_ED25519_SUCCESS != fd_ed25519_verifier_verify( resolver->verifier, _root->hash, 32UL, shred->signature, leader_pubkey ) ) ) {
     867             :         /* Case 3 or 4, reject it */
     868           0 :         return FD_FEC_RESOLVER_SHRED_REJECTED;
     869           0 :       }
     870           0 :     }
     871             : 
     872      141758 :     if( FD_LIKELY( out_merkle_root ) ) *out_merkle_root = *_root;
     873             : 
     874      141755 :     if( FD_UNLIKELY( (!matching_root) | equivoc_or_invalid ) ) {
     875             :       /* Case 2, or other equivocation. Store it as equivocating and then reject it. */
     876           0 :       done_ele_t * done = done_map_ele_query( done_map, slot_fec_pair, NULL, done_pool );
     877           0 :       if( FD_LIKELY( done ) ) fd_fec_samp_set_force_match( done->fec_sample );
     878           0 :       else {
     879           0 :         ensure_done_pool_free( done_pool, done_heap, done_map );
     880             : 
     881           0 :         done = done_pool_ele_acquire( done_pool );
     882             : 
     883           0 :         done->key.slot              = shred->slot;
     884           0 :         done->key.fec_idx           = shred->fec_set_idx;
     885           0 :         done->complete_ts           = 0UL;
     886           0 :         done->post_done_turbine_rem = 0U;
     887           0 :         fd_fec_samp_set_force_match( done->fec_sample );
     888             : 
     889           0 :         done_heap_ele_insert( done_heap, done, done_pool );
     890           0 :         done_map_ele_insert ( done_map,  done, done_pool );
     891           0 :       }
     892           0 :       return FD_FEC_RESOLVER_SHRED_EQUIVOC;
     893           0 :     }
     894             :     /* Otherwise, this must be case 1, which is fine */
     895             : 
     896             :     /* Check to make sure this is not a duplicate */
     897             :     /* We need to make sure that if repair evicted this set and is
     898             :        re-repairing it, we aren't ignoring its shreds, which would keep
     899             :        the set from ever completing.  In the old days, we would return
     900             :        IGNORED earlier for non-repair shreds, but in order to determine
     901             :        if we already have the shred (and so should IGNORE it), we need
     902             :        to make sure we have the right ctx, and we don't have a way to
     903             :        map shred ctx precisely without deriving the Merkle root, so that
     904             :        doesn't make sense anymore. */
     905      141755 :     int shred_dup = !!(fd_uint_if( is_data_shred, ctx->set->data_shred_rcvd, ctx->set->parity_shred_rcvd ) & (1U << in_type_idx));
     906             : 
     907      141755 :     if( FD_UNLIKELY( shred_dup ) ) {
     908           0 :       *out_shred = is_data_shred ? ctx->set->data_shreds[ in_type_idx ].s : ctx->set->parity_shreds[ in_type_idx ].s;
     909           0 :       return fd_int_if( is_repair, FD_FEC_RESOLVER_SHRED_DUPLICATE, FD_FEC_RESOLVER_SHRED_IGNORED );
     910           0 :     }
     911      141755 :   }
     912             : 
     913             :   /* At this point, the shred has passed Merkle validation and is new.
     914             :      We also know that ctx is a pointer to the set_ctx_t where this
     915             :      shred belongs. */
     916             : 
     917             :   /* Copy the shred to memory the FEC resolver owns */
     918 >1844*10^16 :   uchar * dst = is_data_shred ? ctx->set->data_shreds[ in_type_idx ].b : ctx->set->parity_shreds[ in_type_idx ].b;
     919      146328 :   fd_memcpy( dst, shred, fd_shred_sz( shred ) );
     920             : 
     921             :   /* Resigned shreds are relayed with a zero retransmitter signature:
     922             :      nothing verifies it (verify_retransmitter_signature never activated
     923             :      and is superseded by Alpenglow) */
     924      146328 :   if( FD_UNLIKELY( fd_shred_is_resigned( shred_type ) ) ) {
     925           0 :     memset( dst + fd_shred_retransmitter_sig_off( (fd_shred_t *)dst ), 0, 64UL );
     926           0 :   }
     927             : 
     928      146328 :   ctx->set->data_shred_rcvd   |= (uint)(!!is_data_shred)<<in_type_idx;
     929      146328 :   ctx->set->parity_shred_rcvd |= (uint)( !is_data_shred)<<in_type_idx;
     930      146328 :   ulong shred_bit = 1UL<<shred_idx;
     931      146328 :   if( source==FD_FEC_RESOLVER_SHRED_SRC_TURBINE ) ctx->set->turbine_shred_rcvd |= shred_bit;
     932           1 :   else                                            ctx->set->repair_shred_rcvd  |= shred_bit;
     933      146328 :   ctx->total_rx_shred_cnt++;
     934             : 
     935             :   /* Do we have enough to begin reconstruction? */
     936      146328 :   if( FD_LIKELY( ctx->total_rx_shred_cnt < FD_FEC_SHRED_CNT ) ) {
     937      141755 :     *out_shred = (fd_shred_t const *)dst;
     938      141755 :     return FD_FEC_RESOLVER_SHRED_OKAY;
     939      141755 :   }
     940             : 
     941             :   /* At this point, the FEC set is either valid or permanently invalid,
     942             :      so we can consider it done either way. */
     943             : 
     944        4573 :   done_ele_t * done = NULL;
     945             : 
     946             :   /* If it's already in the done map, we don't need to re-insert it.
     947             :      It's not very clear what we should do if it's for a different FEC
     948             :      set, but this can only happen the second insert was a repair shred,
     949             :      and in that case, it gets bypassed anyway, so it doesn't really
     950             :      matter.  We'll just keep the existing value in that case. */
     951        4573 :   slot_fec_pair_t done_key[1] = {{ .slot = ctx->slot, .fec_idx = ctx->fec_set_idx }};
     952        4573 :   if( FD_LIKELY( !done_map_ele_query( done_map, done_key, NULL, done_pool ) ) ) {
     953        4572 :     ensure_done_pool_free( done_pool, done_heap, done_map );
     954             : 
     955        4572 :     done = done_pool_ele_acquire( done_pool );
     956             : 
     957        4572 :     done->key.slot              = ctx->slot;
     958        4572 :     done->key.fec_idx           = ctx->fec_set_idx;
     959        4572 :     done->complete_ts           = (ulong)fd_tickcount() & ((1UL<<56)-1UL);
     960        4572 :     done->post_done_turbine_rem = (uchar)fd_ulong_popcnt( ctx->set->repair_shred_rcvd );
     961             : 
     962        4572 :     if( FD_LIKELY( !!ctx->set->repair_shred_rcvd ) ) {
     963           0 :       FD_MCNT_INC( SHRED, REPAIR_COMPLETION_ASSISTED, 1UL );
     964           0 :     }
     965             :     /* We need to finish the reedsol_recover and inserting all the
     966             :        missing shreds into the tree before we can properly
     967             :        derive this, so for now, we'll set it as force match.  If we hit
     968             :        one of the FEC_FATAL_REJECTED cases below, which is extremely
     969             :        rare, we'll end up rejecting all Turbine shreds for this (slot,
     970             :        FEC set idx).  That's not ideal, but the block is going to get
     971             :        skipped unless the leader equivocates. */
     972        4572 :     fd_fec_samp_set_force_match( done->fec_sample );
     973             : 
     974        4572 :     done_heap_ele_insert( done_heap, done, done_pool );
     975        4572 :     done_map_ele_insert ( done_map,  done, done_pool );
     976        4572 :   }
     977             : 
     978             : 
     979        4573 :   ctx_map_ele_remove_fast( ctx_map,   ctx, ctx_pool );
     980        4573 :   ctx_treap_ele_remove   ( ctx_treap, ctx, ctx_pool );
     981             :   /* At this point, ctx is not in any of the data structures, so we need
     982             :      to be sure to add it to one of the lists before exiting. */
     983             : 
     984        4573 :   fd_fec_set_t       * set  = ctx->set;
     985        4573 :   fd_bmtree_commit_t * tree = ctx->tree;
     986             : 
     987        4573 :   reedsol = fd_reedsol_recover_init( (void*)reedsol, reedsol_protected_sz );
     988      150877 :   for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) {
     989      146304 :     uchar * rs_payload = set->data_shreds[ i ].b + sizeof(fd_ed25519_sig_t);
     990      146304 :     if( set->data_shred_rcvd&(1U<<i) ) fd_reedsol_recover_add_rcvd_shred  ( reedsol, 1, rs_payload );
     991           0 :     else                               fd_reedsol_recover_add_erased_shred( reedsol, 1, rs_payload );
     992      146304 :   }
     993      150877 :   for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) {
     994      146304 :     uchar * rs_payload = set->parity_shreds[ i ].b + FD_SHRED_CODE_HEADER_SZ;
     995      146304 :     if( set->parity_shred_rcvd&(1U<<i) ) fd_reedsol_recover_add_rcvd_shred  ( reedsol, 0, rs_payload );
     996      146304 :     else                                 fd_reedsol_recover_add_erased_shred( reedsol, 0, rs_payload );
     997      146304 :   }
     998             : 
     999        4573 :   if( FD_UNLIKELY( FD_REEDSOL_SUCCESS != fd_reedsol_recover_fini( reedsol ) ) ) {
    1000             :     /* Exactly FD_FEC_SHRED_CNT shreds are present.  Any such subset
    1001             :        defines a codeword, so recovery cannot detect an inconsistent
    1002             :        FEC set here.  A malicious leader's inconsistent shreds are caught
    1003             :        by checking the recovered shreds against the signed Merkle root
    1004             :        below. */
    1005           0 :     ctx_list_ele_push_tail( free_list, ctx, ctx_pool );
    1006           0 :     resolver->free_list_cnt++;
    1007           0 :     FD_MCNT_INC( SHRED, FEC_FATAL_REJECTED, 1UL );
    1008           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
    1009           0 :   }
    1010             : 
    1011        4573 :   uchar const * chained_root = fd_ptr_if( fd_shred_is_chained( shred_type ), (uchar *)shred+fd_shred_chain_off( variant ), NULL );
    1012             : 
    1013             :   /* Populate the recovered shreds' headers and hash their leaves in
    1014             :      sha256 batches.  As in the shredder, the leaf prefix sits in the
    1015             :      unwritten signature tail; the signature is copied in afterwards. */
    1016        4573 :   ulong const prefix_off = sizeof(fd_ed25519_sig_t)-FD_BMTREE_LONG_PREFIX_SZ;
    1017        4573 :   fd_bmtree_node_t leaves[ 2UL*FD_FEC_SHRED_CNT ];
    1018        4573 :   uchar batch_mem[ FD_SHA256_BATCH_FOOTPRINT ] __attribute__((aligned(FD_SHA256_BATCH_ALIGN)));
    1019        4573 :   fd_sha256_batch_t * batch = fd_sha256_batch_init( batch_mem );
    1020             : 
    1021      150877 :   for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) {
    1022      146304 :     if( !(set->data_shred_rcvd&(1U<<i)) ) {
    1023           0 :       if( FD_LIKELY( fd_shred_is_chained( shred_type ) ) ) {
    1024           0 :         fd_memcpy( set->data_shreds[i].b+fd_shred_chain_off( ctx->data_variant ), chained_root, FD_SHRED_MERKLE_ROOT_SZ );
    1025           0 :       }
    1026           0 :       if( FD_LIKELY( !resolver->bypass_verify ) ) {
    1027           0 :         fd_memcpy( set->data_shreds[i].b+prefix_off, fd_bmtree_leaf_prefix, FD_BMTREE_LONG_PREFIX_SZ );
    1028           0 :         fd_sha256_batch_add( batch, set->data_shreds[i].b+prefix_off, data_merkle_protected_sz+FD_BMTREE_LONG_PREFIX_SZ, leaves[ i ].hash );
    1029           0 :       }
    1030           0 :     }
    1031      146304 :   }
    1032             : 
    1033      150877 :   for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) {
    1034      146304 :     if( !(set->parity_shred_rcvd&(1U<<i)) ) {
    1035      146304 :       fd_shred_t * p_shred = set->parity_shreds[i].s; /* We can't parse because we haven't populated the header */
    1036      146304 :       p_shred->variant       = ctx->parity_variant;
    1037      146304 :       p_shred->slot          = shred->slot;
    1038      146304 :       p_shred->idx           = (uint)(i + ctx->fec_set_idx);
    1039      146304 :       p_shred->version       = shred->version;
    1040      146304 :       p_shred->fec_set_idx   = (uint)ctx->fec_set_idx;
    1041      146304 :       p_shred->code.data_cnt = (ushort)FD_FEC_SHRED_CNT;
    1042      146304 :       p_shred->code.code_cnt = (ushort)FD_FEC_SHRED_CNT;
    1043      146304 :       p_shred->code.idx      = (ushort)i;
    1044             : 
    1045      146304 :       if( FD_LIKELY( fd_shred_is_chained( shred_type ) ) ) {
    1046      146304 :         fd_memcpy( set->parity_shreds[i].b+fd_shred_chain_off( ctx->parity_variant ), chained_root, FD_SHRED_MERKLE_ROOT_SZ );
    1047      146304 :       }
    1048      146304 :       if( FD_LIKELY( !resolver->bypass_verify ) ) {
    1049           0 :         fd_memcpy( set->parity_shreds[i].b+prefix_off, fd_bmtree_leaf_prefix, FD_BMTREE_LONG_PREFIX_SZ );
    1050           0 :         fd_sha256_batch_add( batch, set->parity_shreds[i].b+prefix_off, parity_merkle_protected_sz+FD_BMTREE_LONG_PREFIX_SZ, leaves[ FD_FEC_SHRED_CNT+i ].hash );
    1051           0 :       }
    1052      146304 :     }
    1053      146304 :   }
    1054        4573 :   fd_sha256_batch_fini( batch );
    1055             : 
    1056             :   /* Copy the signatures in, then insert the leaves in the same order */
    1057      150877 :   for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) {
    1058      146304 :     if( !(set->data_shred_rcvd&(1U<<i)) ) {
    1059           0 :       fd_memcpy( set->data_shreds[i].b, shred, sizeof(fd_ed25519_sig_t) );
    1060           0 :       if( FD_LIKELY( !resolver->bypass_verify ) ) {
    1061           0 :         if( FD_UNLIKELY( !fd_bmtree_commitp_insert_with_proof( tree, i, leaves+i, NULL, 0, NULL ) ) ) {
    1062           0 :           ctx_list_ele_push_tail( free_list, ctx, ctx_pool );
    1063           0 :           resolver->free_list_cnt++;
    1064           0 :           FD_MCNT_INC( SHRED, FEC_FATAL_REJECTED, 1UL );
    1065           0 :           return FD_FEC_RESOLVER_SHRED_REJECTED;
    1066           0 :         }
    1067           0 :       }
    1068           0 :     }
    1069      146304 :   }
    1070             : 
    1071      150877 :   for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) {
    1072      146304 :     if( !(set->parity_shred_rcvd&(1U<<i)) ) {
    1073      146304 :       fd_memcpy( set->parity_shreds[i].s->signature, shred->signature, sizeof(fd_ed25519_sig_t) );
    1074      146304 :       if( FD_LIKELY( !resolver->bypass_verify ) ) {
    1075           0 :         if( FD_UNLIKELY( !fd_bmtree_commitp_insert_with_proof( tree, FD_FEC_SHRED_CNT + i, leaves+FD_FEC_SHRED_CNT+i, NULL, 0, NULL ) ) ) {
    1076           0 :           ctx_list_ele_push_tail( free_list, ctx, ctx_pool );
    1077           0 :           resolver->free_list_cnt++;
    1078           0 :           FD_MCNT_INC( SHRED, FEC_FATAL_REJECTED, 1UL );
    1079           0 :           return FD_FEC_RESOLVER_SHRED_REJECTED;
    1080           0 :         }
    1081           0 :       }
    1082      146304 :     }
    1083      146304 :   }
    1084             : 
    1085             :   /* Check that the whole Merkle tree is consistent. */
    1086        4573 :   if( FD_UNLIKELY( !resolver->bypass_verify && !fd_bmtree_commitp_fini( tree, FD_FEC_SHRED_CNT + FD_FEC_SHRED_CNT ) ) ) {
    1087           0 :     ctx_list_ele_push_tail( free_list, ctx, ctx_pool );
    1088           0 :     resolver->free_list_cnt++;
    1089           0 :     FD_MCNT_INC( SHRED, FEC_FATAL_REJECTED, 1UL );
    1090           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
    1091           0 :   }
    1092             : 
    1093             :   /* We now have fully populated the bmtree, so we can derive the
    1094             :      fec_sample. */
    1095        4573 :   if( FD_LIKELY( done && !resolver->bypass_verify ) ) fd_fec_samp_derive( done->fec_sample, resolver->seed, ctx->slot, ctx->fec_set_idx, ctx->sig, &ctx->root, tree );
    1096             : 
    1097             :   /* Check that all the fields that are supposed to be consistent across
    1098             :      an FEC set actually are. */
    1099        4573 :   fd_shred_t const * base_data_shred   = fd_shred_parse( set->data_shreds  [ 0 ].b, FD_SHRED_MIN_SZ, max_shred_idx );
    1100        4573 :   fd_shred_t const * base_parity_shred = fd_shred_parse( set->parity_shreds[ 0 ].b, FD_SHRED_MAX_SZ, max_shred_idx );
    1101        4573 :   int reject = (!base_data_shred) | (!base_parity_shred);
    1102             : 
    1103             :   /* Check idx of base shreds */
    1104        4573 :   reject = reject || ((base_data_shred->idx!=ctx->fec_set_idx) | (base_parity_shred->idx!=ctx->fec_set_idx) |
    1105        4572 :                       (base_data_shred->data.flags & FD_SHRED_DATA_FLAG_DATA_COMPLETE));
    1106             : 
    1107      146305 :   for( ulong i=1UL; (!reject) & (i<FD_FEC_SHRED_CNT); i++ ) {
    1108             :     /* Technically, we only need to re-parse the ones we recovered with
    1109             :        Reedsol, but parsing is pretty cheap and the rest of the
    1110             :        validation we need to do on all of them. */
    1111      141732 :     fd_shred_t const * parsed = fd_shred_parse( set->data_shreds[ i ].b, FD_SHRED_MIN_SZ, max_shred_idx );
    1112      141732 :     if( FD_UNLIKELY( !parsed ) ) { reject = 1; break; }
    1113      141732 :     reject |= parsed->variant         != base_data_shred->variant;
    1114      141732 :     reject |= parsed->slot            != base_data_shred->slot;
    1115      141732 :     reject |= parsed->version         != base_data_shred->version;
    1116      141732 :     reject |= parsed->fec_set_idx     != base_data_shred->fec_set_idx;
    1117      141732 :     reject |= parsed->data.parent_off != base_data_shred->data.parent_off;
    1118      141732 :     reject |= parsed->idx             != (uint)(ctx->fec_set_idx+i);
    1119      141732 :     reject |= (i!=FD_FEC_SHRED_CNT-1UL) && (parsed->data.flags & FD_SHRED_DATA_FLAG_DATA_COMPLETE);
    1120             : 
    1121      141732 :     reject |= fd_shred_is_chained( fd_shred_type( parsed->variant ) ) &&
    1122      141732 :                 !fd_memeq( (uchar *)parsed         +fd_shred_chain_off( parsed->variant          ),
    1123      141732 :                            (uchar *)base_data_shred+fd_shred_chain_off( base_data_shred->variant ), FD_SHRED_MERKLE_ROOT_SZ );
    1124      141732 :   }
    1125             : 
    1126      150877 :   for( ulong i=0UL; (!reject) & (i<FD_FEC_SHRED_CNT); i++ ) {
    1127      146304 :     fd_shred_t const * parsed = fd_shred_parse( set->parity_shreds[ i ].b, FD_SHRED_MAX_SZ, max_shred_idx );
    1128      146304 :     if( FD_UNLIKELY( !parsed ) ) { reject = 1; break; }
    1129      146304 :     reject |= fd_shred_type( parsed->variant )       != fd_shred_swap_type( fd_shred_type( base_data_shred->variant ) );
    1130      146304 :     reject |= fd_shred_merkle_cnt( parsed->variant ) != fd_shred_merkle_cnt( base_data_shred->variant );
    1131      146304 :     reject |= parsed->slot                           != base_data_shred->slot;
    1132      146304 :     reject |= parsed->version                        != base_data_shred->version;
    1133      146304 :     reject |= parsed->fec_set_idx                    != base_data_shred->fec_set_idx;
    1134      146304 :     reject |= parsed->idx                            != (uint)(ctx->fec_set_idx+i);
    1135      146304 :     reject |= parsed->code.data_cnt                  != base_parity_shred->code.data_cnt;
    1136      146304 :     reject |= parsed->code.code_cnt                  != base_parity_shred->code.code_cnt;
    1137      146304 :     reject |= parsed->code.idx                       != (ushort)i;
    1138             : 
    1139      146304 :     reject |= fd_shred_is_chained( fd_shred_type( parsed->variant ) ) &&
    1140      146304 :                 !fd_memeq( (uchar *)parsed         +fd_shred_chain_off( parsed->variant          ),
    1141      146304 :                            (uchar *)base_data_shred+fd_shred_chain_off( base_data_shred->variant ), FD_SHRED_MERKLE_ROOT_SZ );
    1142      146304 :   }
    1143        4573 :   if( FD_UNLIKELY( reject ) ) {
    1144           0 :     ctx_list_ele_push_tail( free_list, ctx, ctx_pool );
    1145           0 :     resolver->free_list_cnt++;
    1146           0 :     FD_MCNT_INC( SHRED, FEC_FATAL_REJECTED, 1UL );
    1147           0 :     return FD_FEC_RESOLVER_SHRED_REJECTED;
    1148           0 :   }
    1149             : 
    1150             : 
    1151             :   /* Populate missing Merkle proofs */
    1152      150877 :   for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++   ) if( !( set->data_shred_rcvd&(1U<<i) ) )
    1153           0 :     fd_bmtree_get_proof( tree, set->data_shreds[i].b   + fd_shred_merkle_off( set->data_shreds[i].s   ), i );
    1154             : 
    1155      150877 :   for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) if( !( set->parity_shred_rcvd&(1U<<i) ) )
    1156      146304 :     fd_bmtree_get_proof( tree, set->parity_shreds[i].b + fd_shred_merkle_off( set->parity_shreds[i].s ), FD_FEC_SHRED_CNT+i );
    1157             : 
    1158             :   /* Zero retransmitter signature on the recovered shreds too */
    1159        4573 :   if( FD_UNLIKELY( fd_shred_is_resigned( shred_type ) ) ) {
    1160           0 :     for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++   ) if( !( set->data_shred_rcvd&(1U<<i) ) )
    1161           0 :       memset( set->data_shreds[i].b   + fd_shred_retransmitter_sig_off( set->data_shreds[i].s   ), 0, 64UL );
    1162             : 
    1163           0 :     for( ulong i=0UL; i<FD_FEC_SHRED_CNT; i++ ) if( !( set->parity_shred_rcvd&(1U<<i) ) )
    1164           0 :       memset( set->parity_shreds[i].b + fd_shred_retransmitter_sig_off( set->parity_shreds[i].s ), 0, 64UL );
    1165           0 :   }
    1166             : 
    1167             :   /* Finally... A valid FEC set.  Forward it along. */
    1168        4573 :   ctx_list_ele_push_tail( complete_list, ctx, ctx_pool );
    1169        4573 :   ctx_list_idx_push_tail( free_list, ctx_list_idx_pop_head( complete_list, ctx_pool ), ctx_pool );
    1170        4573 :   resolver->free_list_cnt++;
    1171             : 
    1172        4573 :   *out_fec_set = set;
    1173        4573 :   *out_shred   = (fd_shred_t const *)dst;
    1174             : 
    1175        4573 :   return FD_FEC_RESOLVER_SHRED_COMPLETES;
    1176        4573 : }
    1177             : 
    1178             : 
    1179           0 : void * fd_fec_resolver_leave( fd_fec_resolver_t * resolver ) {
    1180           0 :   fd_ed25519_verifier_leave( resolver->verifier );
    1181           0 :   done_heap_leave       ( resolver->done_heap     );
    1182           0 :   ctx_list_leave        ( resolver->complete_list );
    1183           0 :   ctx_list_leave        ( resolver->free_list     );
    1184           0 :   ctx_treap_leave       ( resolver->ctx_treap     );
    1185           0 :   done_map_leave        ( resolver->done_map      );
    1186           0 :   done_pool_leave       ( resolver->done_pool     );
    1187           0 :   ctx_map_leave         ( resolver->ctx_map       );
    1188             : 
    1189           0 :   return (void *)resolver;
    1190           0 : }
    1191             : 
    1192           0 : void * fd_fec_resolver_delete( void * shmem ) {
    1193           0 :   fd_fec_resolver_t * resolver = (fd_fec_resolver_t *)shmem;
    1194           0 :   ulong depth          = resolver->depth;
    1195           0 :   ulong partial_depth  = resolver->partial_depth;
    1196           0 :   ulong complete_depth = resolver->complete_depth;
    1197           0 :   ulong done_depth     = resolver->done_depth;
    1198             : 
    1199           0 :   ulong depth_sum      = depth + partial_depth + complete_depth;
    1200           0 :   ulong ctx_chain_cnt  = ctx_map_chain_cnt_est ( depth      );
    1201           0 :   ulong done_chain_cnt = done_map_chain_cnt_est( done_depth );
    1202             : 
    1203           0 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
    1204           0 :   /*     self      */  FD_SCRATCH_ALLOC_APPEND( l, FD_FEC_RESOLVER_ALIGN,    sizeof(fd_fec_resolver_t)                           );
    1205           0 :   /*     _ctx_pool */  FD_SCRATCH_ALLOC_APPEND( l, alignof(set_ctx_t),       sizeof(set_ctx_t)*depth_sum                         );
    1206           0 :   void * _ctx_map    = FD_SCRATCH_ALLOC_APPEND( l, ctx_map_align(),          ctx_map_footprint  ( ctx_chain_cnt  )               );
    1207           0 :   void * _done_pool  = FD_SCRATCH_ALLOC_APPEND( l, done_pool_align(),        done_pool_footprint( done_depth     )               );
    1208           0 :   void * _done_map   = FD_SCRATCH_ALLOC_APPEND( l, done_map_align(),         done_map_footprint ( done_chain_cnt )               );
    1209           0 :   void * _verifier   = FD_SCRATCH_ALLOC_APPEND( l, fd_ed25519_verifier_align(), fd_ed25519_verifier_footprint( ED25519_CACHE_ENT_CNT ) );
    1210           0 :   FD_SCRATCH_ALLOC_FINI( l, FD_FEC_RESOLVER_ALIGN );
    1211             : 
    1212           0 :   fd_ed25519_verifier_delete( _verifier );
    1213           0 :   done_heap_delete       ( resolver->done_heap     );
    1214           0 :   done_map_delete        ( _done_map               );
    1215           0 :   done_pool_delete       ( _done_pool              );
    1216           0 :   ctx_list_delete        ( resolver->complete_list );
    1217           0 :   ctx_list_delete        ( resolver->free_list     );
    1218           0 :   ctx_treap_delete       ( resolver->ctx_treap     );
    1219           0 :   ctx_map_delete         ( _ctx_map                );
    1220             : 
    1221           0 :   return shmem;
    1222           0 : }

Generated by: LCOV version 1.14