LCOV - code coverage report
Current view: top level - disco/shred - fd_shred_dest.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 239 249 96.0 %
Date: 2024-11-13 11:58:15 Functions: 12 12 100.0 %

          Line data    Source code
       1             : #include "fd_shred_dest.h"
       2             : 
       3             : struct pubkey_to_idx {
       4             :   fd_pubkey_t key;
       5             :   ulong       idx;
       6             : };
       7             : typedef struct pubkey_to_idx pubkey_to_idx_t;
       8             : 
       9             : const fd_pubkey_t null_pubkey = {{ 0 }};
      10             : 
      11             : #define MAP_NAME              pubkey_to_idx
      12   243213687 : #define MAP_T                 pubkey_to_idx_t
      13  2960112495 : #define MAP_KEY_T             fd_pubkey_t
      14   828687780 : #define MAP_KEY_NULL          null_pubkey
      15             : #define MAP_KEY_EQUAL_IS_SLOW 1
      16             : #define MAP_MEMOIZE           0
      17  2960112495 : #define MAP_KEY_INVAL(k)      MAP_KEY_EQUAL((k),MAP_KEY_NULL)
      18  5686081302 : #define MAP_KEY_EQUAL(k0,k1)  (!memcmp( (k0).key, (k1).key, 32UL ))
      19   242587869 : #define MAP_KEY_HASH(key)     ((MAP_HASH_T)( (key).ul[1] ))
      20             : 
      21             : #include "../../util/tmpl/fd_map_dynamic.c"
      22             : 
      23             : 
      24             : /* This 45 byte struct gets hashed to compute the seed for Chacha20 to
      25             :    compute the shred destinations. */
      26             : struct __attribute__((packed)) shred_dest_input {
      27             :   ulong slot;
      28             :   uchar type; /*     Data = 0b1010_0101, Code = 0b0101_1010 */
      29             :   uint  idx;
      30             :   uchar leader_pubkey[32];
      31             : };
      32             : typedef struct shred_dest_input shred_dest_input_t;
      33             : 
      34             : ulong
      35      120612 : fd_shred_dest_footprint( ulong staked_cnt, ulong unstaked_cnt ) {
      36      120612 :   ulong cnt = staked_cnt+unstaked_cnt;
      37      120612 :   int lg_cnt = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*fd_ulong_max( cnt, 1UL ) ) );
      38      120612 :   return FD_LAYOUT_FINI( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND(
      39      120612 :                 FD_LAYOUT_INIT,
      40      120612 :                 fd_shred_dest_align(),             sizeof(fd_shred_dest_t)              ),
      41      120612 :                 pubkey_to_idx_align(),             pubkey_to_idx_footprint( lg_cnt )    ),
      42      120612 :                 alignof(fd_shred_dest_weighted_t), sizeof(fd_shred_dest_weighted_t)*cnt ),
      43      120612 :                 fd_wsample_align(),                fd_wsample_footprint( staked_cnt, 1 )),
      44      120612 :                 alignof(ulong),                    sizeof(ulong)*unstaked_cnt           ),
      45      120612 :       FD_SHRED_DEST_ALIGN );
      46      120612 : }
      47             : 
      48             : 
      49             : void *
      50             : fd_shred_dest_new( void                           * mem,
      51             :                    fd_shred_dest_weighted_t const * info,
      52             :                    ulong                            cnt,
      53             :                    fd_epoch_leaders_t const       * lsched,
      54             :                    fd_pubkey_t const              * source,
      55      312915 :                    ulong                            excluded_stake ) {
      56             : 
      57      312915 :   if( FD_UNLIKELY( !mem ) ) {
      58           3 :     FD_LOG_WARNING(( "NULL mem" ));
      59           3 :     return NULL;
      60           3 :   }
      61             : 
      62      312912 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, fd_shred_dest_align() ) ) ) {
      63           3 :     FD_LOG_WARNING(( "misaligned mem" ));
      64           3 :     return NULL;
      65           3 :   }
      66             : 
      67      312909 :   int lg_cnt = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*fd_ulong_max( cnt, 1UL ) ) );
      68      312909 :   FD_SCRATCH_ALLOC_INIT( footprint, mem );
      69      312909 :   fd_shred_dest_t * sdest;
      70      312909 :   /* */  sdest     = FD_SCRATCH_ALLOC_APPEND( footprint, fd_shred_dest_align(),             sizeof(fd_shred_dest_t)              );
      71      312909 :   void * _map      = FD_SCRATCH_ALLOC_APPEND( footprint, pubkey_to_idx_align(),             pubkey_to_idx_footprint( lg_cnt )    );
      72      312909 :   void * _info     = FD_SCRATCH_ALLOC_APPEND( footprint, alignof(fd_shred_dest_weighted_t), sizeof(fd_shred_dest_weighted_t)*cnt );
      73             : 
      74           0 :   ulong cnts[2] = { 0UL, 0UL }; /* cnts[0] = staked, cnts[1] = unstaked */
      75             : 
      76      312909 :   fd_shred_dest_weighted_t * copy = (fd_shred_dest_weighted_t *)_info;
      77   234456597 :   for( ulong i=0UL; i<cnt; i++ ) {
      78   234143688 :     copy[i] = info[i];
      79   234143688 :     ulong stake = info[i].stake_lamports;
      80             :     /* Check to make we never have a staked node following an unstaked
      81             :        node, which would mean info is not sorted properly. */
      82   234143688 :     if( FD_UNLIKELY( (stake>0UL) & (cnts[1]>0UL) ) ) {
      83           0 :       FD_LOG_WARNING(( "info was not sorted properly. info[%lu] has non-zero stake %lu but follows an unstaked node", i, stake ));
      84           0 :       return NULL;
      85           0 :     }
      86   234143688 :     cnts[ stake==0UL ]++;
      87   234143688 :   }
      88             : 
      89      312909 :   ulong staked_cnt   = cnts[0];
      90      312909 :   ulong unstaked_cnt = cnts[1];
      91             : 
      92      312909 :   if( FD_UNLIKELY( (excluded_stake>0UL) & (unstaked_cnt>0UL) ) ) {
      93             :     /* If excluded stake > 0, then the list must be filled with staked nodes. */
      94           0 :     FD_LOG_WARNING(( "cannot have excluded stake and unstaked validators" ));
      95           0 :     return NULL;
      96           0 :   }
      97             : 
      98      312909 :   void * _wsample  = FD_SCRATCH_ALLOC_APPEND( footprint, fd_wsample_align(),                fd_wsample_footprint( staked_cnt, 1 ));
      99      312909 :   void * _unstaked = FD_SCRATCH_ALLOC_APPEND( footprint, alignof(ulong),                    sizeof(ulong)*unstaked_cnt           );
     100             : 
     101             : 
     102      312909 :   fd_chacha20rng_t * rng = fd_chacha20rng_join( fd_chacha20rng_new( sdest->rng, FD_CHACHA20RNG_MODE_SHIFT ) );
     103             : 
     104      312909 :   void  *  _staked   = fd_wsample_new_init( _wsample,  rng, staked_cnt,   1, FD_WSAMPLE_HINT_POWERLAW_REMOVE );
     105             : 
     106   201238467 :   for( ulong i=0UL; i<staked_cnt;   i++ ) _staked   = fd_wsample_new_add( _staked,   info[i].stake_lamports );
     107      312909 :   _staked   = fd_wsample_new_fini( _staked, excluded_stake );
     108             : 
     109      312909 :   pubkey_to_idx_t * pubkey_to_idx_map = pubkey_to_idx_join( pubkey_to_idx_new( _map, lg_cnt ) );
     110   234456597 :   for( ulong i=0UL; i<cnt; i++ ) {
     111   234143688 :     pubkey_to_idx_insert( pubkey_to_idx_map, info[i].pubkey )->idx = i;
     112   234143688 :   }
     113      312909 :   pubkey_to_idx_t * query = pubkey_to_idx_query( pubkey_to_idx_map, *source, NULL );
     114      312909 :   if( FD_UNLIKELY( !query ) ) {
     115          12 :     FD_LOG_WARNING(( "source pubkey not found" ));
     116          12 :     return NULL;
     117          12 :   }
     118             : 
     119      312897 :   memset( sdest->null_dest, 0, sizeof(fd_shred_dest_weighted_t) );
     120      312897 :   sdest->lsched                     = lsched;
     121      312897 :   sdest->cnt                        = cnt;
     122      312897 :   sdest->all_destinations           = copy;
     123      312897 :   sdest->staked                     = fd_wsample_join( _staked );
     124      312897 :   sdest->unstaked                   = _unstaked;
     125      312897 :   sdest->unstaked_unremoved_cnt     = 0UL; /* unstaked doesn't get initialized until it's needed */
     126      312897 :   sdest->staked_cnt                 = staked_cnt;
     127      312897 :   sdest->unstaked_cnt               = unstaked_cnt;
     128      312897 :   sdest->excluded_stake             = excluded_stake;
     129      312897 :   sdest->pubkey_to_idx_map          = pubkey_to_idx_map;
     130      312897 :   sdest->source_validator_orig_idx  = query->idx;
     131             : 
     132      312897 :   return (void *)sdest;
     133      312909 : }
     134             : 
     135      312909 : fd_shred_dest_t * fd_shred_dest_join( void * mem  ) { return (fd_shred_dest_t *)mem; }
     136      312819 : void * fd_shred_dest_leave( fd_shred_dest_t * sdest ) { return (void *)sdest;          }
     137             : 
     138      312819 : void * fd_shred_dest_delete( void * mem ) {
     139      312819 :   fd_shred_dest_t * sdest = (fd_shred_dest_t *)mem;
     140             : 
     141      312819 :   fd_chacha20rng_delete( fd_chacha20rng_leave( sdest->rng               ) );
     142      312819 :   fd_wsample_delete    ( fd_wsample_leave    ( sdest->staked            ) );
     143      312819 :   pubkey_to_idx_delete ( pubkey_to_idx_leave ( sdest->pubkey_to_idx_map ) );
     144      312819 :   return mem;
     145      312819 : }
     146             : 
     147             : /* sample_unstaked, sample_unstaked_noprepare, and
     148             :    prepare_unstaked_sampling are used to perform the specific form of
     149             :    unweighted random sampling that Solana uses for unstaked validators.
     150             :    In essence, you:
     151             :     1. construct a list of all the unstaked validators,
     152             :     2. delete the leader (if present)
     153             :     then repeatedly:
     154             :     3. choose the chacha20rng_roll( |unstaked| )th element.
     155             :     4. swap the last element in unstaked with the chosen element
     156             :     5. return and remove the chosen element (which is now in the last
     157             :     position, so remove is O(1)).
     158             :    Steps 1 and 2 are both O(|unstaked|), but they can be combined
     159             :    relatively easily if we wait to construct the list until we know
     160             :    which element we need to delete.  prepare_unstaked_sampling performs
     161             :    steps 1 and 2. sample_unstaked performs steps 3-5.  Thus, you must
     162             :    call prepare_unstaked_sampling prior to calling sample_unstaked.
     163             : 
     164             :    When only sampling a single element from the list, forming the whole
     165             :    array is wasteful; we just need to know whether the element we choose
     166             :    comes before or after the element we deleted.
     167             :    sample_unstaked_noprepare returns the same result as
     168             :    prepare_unstaked_sampling followed by one call to sample_unstaked.
     169             :    sample_unstaked_noprepare does not read or modify the unstaked array,
     170             :    so it can be called without calling prepare_unstaked_sampling.
     171             : 
     172             :    remove_idx is the index of the element to remove in step 2.  If it is
     173             :    not in [sdest->staked_cnt, sdest->staked_cnt+sdest->unstaked_cnt),
     174             :    then it will be ignored.  sample_unstaked and
     175             :    sample_unstaked_noprepare return the index into
     176             :    sdest->all_destinations of the selected sample.  The returned value
     177             :    will be in [sdest->staked_cnt, sdest->staked_cnt+sdest->unstaked_cnt)
     178             :    or FD_WSAMPLE_EMPTY. */
     179             : static inline ulong
     180             : sample_unstaked_noprepare( fd_shred_dest_t  * sdest,
     181          99 :                            ulong              remove_idx ) {
     182             :   /* is remove_idx in
     183             :      [sdest->staked_cnt, sdest->staked_cnt+sdest->unstaked_cnt) ? */
     184          99 :   int remove_in_interval = (sdest->staked_cnt <= remove_idx) & (remove_idx < (sdest->staked_cnt+sdest->unstaked_cnt) );
     185          99 :   ulong unstaked_cnt = sdest->unstaked_cnt - (ulong)remove_in_interval;
     186          99 :   if( FD_UNLIKELY( unstaked_cnt==0UL ) ) return FD_WSAMPLE_EMPTY;
     187             : 
     188          99 :   ulong sample = sdest->staked_cnt + fd_chacha20rng_ulong_roll( sdest->rng, unstaked_cnt );
     189          99 :   return fd_ulong_if( (!remove_in_interval) | (sample<remove_idx), sample, sample+1UL );
     190          99 : }
     191             : 
     192             : /* It's cheaper to initialize unstaked without the element we want to
     193             :    delete than to delete it after initializing, so we defer the
     194             :    initialization until we know who the leader is. */
     195             : static inline void
     196             : prepare_unstaked_sampling( fd_shred_dest_t  * sdest,
     197      136032 :                            ulong              remove_idx ) {
     198      136032 :   int remove_in_interval = (sdest->staked_cnt <= remove_idx) & (remove_idx < (sdest->staked_cnt+sdest->unstaked_cnt) );
     199      136032 :   ulong unstaked_cnt = sdest->unstaked_cnt - (ulong)remove_in_interval;
     200      136032 :   sdest->unstaked_unremoved_cnt = unstaked_cnt;
     201      136032 :   if( FD_UNLIKELY( unstaked_cnt==0UL ) ) return;
     202             : 
     203             :   /* If we had to remove something in the interval, we want to make sure
     204             :      it doesn't occur in the list of indices.  Otherwise just take them
     205             :      all. */
     206      130227 :   ulong direct_index_up_to = fd_ulong_if( remove_in_interval, remove_idx - sdest->staked_cnt, unstaked_cnt );
     207      130227 :   ulong i=0UL;
     208    11153415 :   for( ; i<direct_index_up_to; i++ ) sdest->unstaked[i] = i+sdest->staked_cnt;
     209      624822 :   for( ; i<unstaked_cnt;       i++ ) sdest->unstaked[i] = i+sdest->staked_cnt + 1UL;
     210      130227 : }
     211             : 
     212             : static inline ulong
     213    11376087 : sample_unstaked( fd_shred_dest_t * sdest ) {
     214    11376087 :   if( FD_UNLIKELY( sdest->unstaked_unremoved_cnt==0UL ) ) return FD_WSAMPLE_EMPTY;
     215             : 
     216    11258799 :   ulong sample = fd_chacha20rng_ulong_roll( sdest->rng, sdest->unstaked_unremoved_cnt );
     217    11258799 :   ulong to_return = sdest->unstaked[sample];
     218    11258799 :   sdest->unstaked[sample] = sdest->unstaked[--sdest->unstaked_unremoved_cnt];
     219    11258799 :   return to_return;
     220    11376087 : }
     221             : 
     222             : 
     223             : /* Returns 0 on success */
     224             : static inline int
     225             : compute_seeds( fd_shred_dest_t           * sdest,
     226             :                fd_shred_t  const * const * input_shreds,
     227             :                ulong                       shred_cnt,
     228             :                fd_pubkey_t       const   * leader,
     229             :                ulong                       slot,
     230     3756717 :                uchar                       dest_hash_output[ FD_SHRED_DEST_MAX_SHRED_CNT ][ 32 ] ) {
     231             : 
     232     3756717 :   shred_dest_input_t dest_hash_inputs [ FD_SHRED_DEST_MAX_SHRED_CNT ];
     233     3756717 :   fd_sha256_batch_t * sha256 = fd_sha256_batch_init( sdest->_sha256_batch );
     234             : 
     235     8263434 :   for( ulong i=0UL; i<shred_cnt; i++ ) {
     236     4506717 :     shred_dest_input_t * h_in  = dest_hash_inputs+i;
     237     4506717 :     fd_shred_t const   * shred = input_shreds[i];
     238     4506717 :     if( FD_UNLIKELY( shred->slot != slot ) ) return -1;
     239             : 
     240     4506717 :     uchar shred_type = fd_shred_type( shred->variant );
     241     4506717 :     h_in->slot = slot;
     242     4506717 :     h_in->type = fd_uchar_if( (shred_type==FD_SHRED_TYPE_LEGACY_DATA) | (shred_type==FD_SHRED_TYPE_MERKLE_DATA), 0xA5, 0x5A );
     243     4506717 :     h_in->idx  = shred->idx;
     244     4506717 :     memcpy( h_in->leader_pubkey, leader, 32UL );
     245             : 
     246     4506717 :     fd_sha256_batch_add( sha256, dest_hash_inputs+i,   sizeof(shred_dest_input_t), dest_hash_output[ i ] );
     247     4506717 :   }
     248     3756717 :   fd_sha256_batch_fini( sha256 );
     249     3756717 :   return 0;
     250     3756717 : }
     251             : 
     252             : 
     253             : fd_shred_dest_idx_t *
     254             : fd_shred_dest_compute_first( fd_shred_dest_t          * sdest,
     255             :                              fd_shred_t const * const * input_shreds,
     256             :                              ulong                      shred_cnt,
     257        5820 :                              fd_shred_dest_idx_t      * out ) {
     258             : 
     259        5820 :   if( FD_UNLIKELY( shred_cnt==0UL ) ) return out;
     260             : 
     261        5820 :   if( FD_UNLIKELY( sdest->cnt<=1UL ) ) {
     262             :     /* We are the only validator that we know about, and we can't send
     263             :        it to ourself, so there's nobody we can send the shred to. */
     264           0 :     for( ulong i=0UL; i<shred_cnt; i++ ) out[ i ] = FD_SHRED_DEST_NO_DEST;
     265           0 :     return out;
     266           0 :   }
     267             : 
     268        5820 :   uchar dest_hash_outputs[ FD_SHRED_DEST_MAX_SHRED_CNT ][ 32 ];
     269             : 
     270        5820 :   ulong slot = input_shreds[0]->slot;
     271        5820 :   fd_pubkey_t const * leader = fd_epoch_leaders_get( sdest->lsched, slot );
     272        5820 :   if( FD_UNLIKELY( !leader ) ) return NULL;
     273             : 
     274        5820 :   if( FD_UNLIKELY( compute_seeds( sdest, input_shreds, shred_cnt, leader, slot, dest_hash_outputs ) ) ) return NULL;
     275             : 
     276             :   /* If we're calling this, we must be the leader.  That means we had
     277             :      some stake when the leader schedule was created, but maybe not
     278             :      anymore?  This version of the code is safe either way, but I should
     279             :      probably confirm this can happen. */
     280        5820 :   int source_validator_is_staked = sdest->source_validator_orig_idx<sdest->staked_cnt;
     281        5820 :   if( FD_LIKELY( source_validator_is_staked ) )
     282        4158 :     fd_wsample_remove_idx( sdest->staked, sdest->source_validator_orig_idx );
     283             : 
     284        5820 :   int any_staked_candidates = sdest->staked_cnt > (ulong)source_validator_is_staked;
     285       11640 :   for( ulong i=0UL; i<shred_cnt; i++ ) {
     286        5820 :     fd_wsample_seed_rng( fd_wsample_get_rng( sdest->staked ), dest_hash_outputs[ i ] );
     287             :     /* Map FD_WSAMPLE_INDETERMINATE to FD_SHRED_DEST_NO_DEST */
     288        5820 :     if( FD_LIKELY( any_staked_candidates ) ) out[i] = (ushort)fd_ulong_min( fd_wsample_sample( sdest->staked ), FD_SHRED_DEST_NO_DEST );
     289          99 :     else                                     out[i] = (ushort)sample_unstaked_noprepare( sdest, sdest->source_validator_orig_idx );
     290        5820 :   }
     291        5820 :   fd_wsample_restore_all( sdest->staked );
     292             : 
     293        5820 :   return out;
     294        5820 : }
     295             : 
     296             : fd_shred_dest_idx_t *
     297             : fd_shred_dest_compute_children( fd_shred_dest_t          * sdest,
     298             :                                 fd_shred_t const * const * input_shreds,
     299             :                                 ulong                      shred_cnt,
     300             :                                 fd_shred_dest_idx_t      * out,
     301             :                                 ulong                      out_stride,
     302             :                                 ulong                      fanout,
     303             :                                 ulong                      dest_cnt,
     304     3789198 :                                 ulong                    * opt_max_dest_cnt ) {
     305             : 
     306             :   /* The logic here is a little tricky since we are keeping track of
     307             :      staked and unstaked separately and only logically concatenating
     308             :      them [staked, unstaked] , but that does allow us to skip some
     309             :      samples sometimes.  We're operating from the source validator's
     310             :      perspective here, so everything in the first person singular refers
     311             :      to the source validator. */
     312             : 
     313     3789198 :   ulong my_orig_idx = sdest->source_validator_orig_idx;
     314     3789198 :   int   i_am_staked = my_orig_idx<sdest->staked_cnt;
     315             : 
     316     3789198 :   fd_ulong_store_if( !!opt_max_dest_cnt, opt_max_dest_cnt, 0UL );
     317             : 
     318     3789198 :   if( FD_UNLIKELY( (shred_cnt==0UL) | (dest_cnt==0UL) ) ) return out; /* Nothing to do */
     319             : 
     320     3789198 :   ulong               slot   = input_shreds[0]->slot;
     321     3789198 :   fd_pubkey_t const * leader = fd_epoch_leaders_get   ( sdest->lsched, slot );
     322     3789198 :   pubkey_to_idx_t *   query  = pubkey_to_idx_query( sdest->pubkey_to_idx_map, *leader, NULL );
     323     3789198 :   int                 leader_is_staked = query ? (query->idx<sdest->staked_cnt): 0;
     324     3789198 :   ulong               leader_idx       = query ?  query->idx                   : ULONG_MAX;
     325     3789198 :   if( FD_UNLIKELY( !leader                 ) ) return NULL; /* Unknown slot */
     326     3789198 :   if( FD_UNLIKELY( leader_idx==my_orig_idx ) ) return NULL; /* I am the leader. Use compute_first */
     327             : 
     328     3789198 :   if( FD_UNLIKELY( (sdest->cnt<=1UL) |                    /* We don't know about a single destination, so we can't send
     329             :                                                              anything. */
     330     3789198 :         ( (!i_am_staked) & (sdest->staked_cnt-(ulong)leader_is_staked>fanout) ) ) ) {
     331             :     /* My position is somewhere after all the staked nodes, which means
     332             :        my shuffled index is always greater than fanout.  That means I'm
     333             :        always at the bottom of the Turbine tree so I don't have to send
     334             :        any shreds to anyone. */
     335    19622568 :     for( ulong j=0UL; j<dest_cnt; j++ ) for( ulong i=0UL; i<shred_cnt; i++ ) out[ j*out_stride + i ] = FD_SHRED_DEST_NO_DEST;
     336       38301 :     return out;
     337       38301 :   }
     338             : 
     339     3750897 :   uchar dest_hash_outputs[ FD_SHRED_DEST_MAX_SHRED_CNT ][ 32 ];
     340             : 
     341             : 
     342     3750897 :   if( FD_UNLIKELY( compute_seeds( sdest, input_shreds, shred_cnt, leader, slot, dest_hash_outputs ) ) ) return NULL;
     343             : 
     344     3750897 :   ulong max_dest_cnt = 0UL;
     345             : 
     346     3750897 :   ulong staked_shuffle[ sdest->staked_cnt+1UL ];
     347     3750897 :   ulong staked_shuffle_populated_cnt = 0UL;
     348             : 
     349     8251794 :   for( ulong i=0UL; i<shred_cnt; i++ ) {
     350             :     /* Remove the leader. */
     351     4500897 :     if( FD_LIKELY( query && leader_is_staked ) ) fd_wsample_remove_idx( sdest->staked, leader_idx );
     352             : 
     353     4500897 :     ulong my_idx         = 0UL;
     354     4500897 :     fd_wsample_seed_rng( fd_wsample_get_rng( sdest->staked ), dest_hash_outputs[ i ] ); /* Seeds both samplers since the rng is shared */
     355             : 
     356     4500897 :     if( FD_UNLIKELY( !i_am_staked ) ) {
     357             :       /* If there's excluded stake, we don't know about any unstaked
     358             :          validators, so if I am unstaked, then I'm in the excluded
     359             :          region, and we have no hope of computing my position in the
     360             :          shuffle. */
     361       37503 :       if( FD_UNLIKELY( sdest->excluded_stake>0UL ) ) return NULL;
     362             : 
     363             :       /* Quickly burn through all the staked nodes since I'll be in the
     364             :          unstaked portion.  We don't care about the values, but we need
     365             :          to advance the RNG the right number of times, and sadly there's
     366             :          no other way to do it than this. There can't be too many of
     367             :          them since otherwise we would have taken the quick exit at the
     368             :          start of the function. */
     369       37503 :       staked_shuffle_populated_cnt = sdest->staked_cnt + 1UL;
     370       37503 :       fd_wsample_sample_and_remove_many( sdest->staked, staked_shuffle, staked_shuffle_populated_cnt );
     371       37503 :       my_idx += sdest->staked_cnt - (ulong)(query && leader_is_staked);
     372             : 
     373       37503 :       prepare_unstaked_sampling( sdest, leader_idx );
     374      342495 :       while( my_idx <= fanout ) {
     375      325584 :         ulong sample = sample_unstaked( sdest );
     376      325584 :         if( FD_UNLIKELY( sample==my_orig_idx      ) ) break; /* Found me! */
     377      304992 :         if( FD_UNLIKELY( sample==FD_WSAMPLE_EMPTY ) ) return NULL; /* I couldn't find myself.  This should be impossible. */
     378      304992 :         my_idx++;
     379      304992 :       }
     380     4463394 :     } else {
     381     4463394 :       staked_shuffle_populated_cnt = fd_ulong_min( fanout+1UL, sdest->staked_cnt+1UL );
     382     4463394 :       fd_wsample_sample_and_remove_many( sdest->staked, staked_shuffle, staked_shuffle_populated_cnt );
     383   749728224 :       while( my_idx <= fanout ) {
     384             :         /* my_idx < fanout+1UL because of the while loop condition.
     385             :            my_idx < staked_cnt+1UL because my_idx==staked_cnt will
     386             :            trigger sample==FD_WSAMPLE_EMPTY below.  Thus, this access is
     387             :            safe. */
     388   746113935 :         ulong sample = staked_shuffle[ my_idx ];
     389   746113935 :         if( FD_UNLIKELY( sample==my_orig_idx              ) ) break; /* Found me! */
     390   745264830 :         if( FD_UNLIKELY( sample==FD_WSAMPLE_EMPTY         ) ) return NULL; /* I couldn't find myself.  This should be impossible. */
     391   745264830 :         if( FD_UNLIKELY( sample==FD_WSAMPLE_INDETERMINATE ) ) my_idx=ULONG_MAX-1UL; /* Hit poisoned region before myself. No
     392             :                                                                                        clue about position. Break immediately and
     393             :                                                                                        fill with NO_DEST below. */
     394   745264830 :         my_idx++;
     395   745264830 :       }
     396     4463394 :     }
     397             : 
     398     4500897 :     if( FD_LIKELY( my_idx > fanout ) ) {
     399             :       /* I'm at the bottom of Turbine tree for this shred.  Fill in all
     400             :          the destinations with NO_DEST. */
     401   732081666 :       for( ulong j=0UL; j<dest_cnt; j++ ) out[ j*out_stride + i ] = FD_SHRED_DEST_NO_DEST;
     402             : 
     403     3631200 :       fd_wsample_restore_all( sdest->staked   );
     404     3631200 :       continue; /* Next shred */
     405     3631200 :     }
     406             :     /* If my index is    |  Send to indices
     407             :        ------------------------------------
     408             :         Leader (no idx)  |  0             (just for reference)
     409             :         0                |  1, 2, ..., F
     410             :         j in [1, F]      |  j + l*F for l in [1,F]
     411             :         [F+1, F^2+F]     |  Nobody
     412             :         [F^2+F+1, inf)   |  Not yet implemented in Agave code
     413             :      */
     414      869697 :     ulong last_dest_idx = fd_ulong_if( my_idx==0UL, fanout, my_idx+fanout*fanout ); /* inclusive */
     415      869697 :     ulong stride        = fd_ulong_if( my_idx==0UL, 1UL,    fanout               );
     416             : 
     417      869697 :     last_dest_idx = fd_ulong_min( last_dest_idx, my_idx + stride*dest_cnt );
     418             : 
     419      869697 :     ulong cursor     = my_idx+1UL;
     420      869697 :     ulong stored_cnt = 0UL;
     421             : 
     422      869697 :     if( FD_LIKELY( (last_dest_idx>=staked_shuffle_populated_cnt) & (staked_shuffle_populated_cnt<sdest->staked_cnt+1UL ) ) ) {
     423      404337 :       ulong adtl = fd_ulong_min( last_dest_idx+1UL, sdest->staked_cnt+1UL ) - staked_shuffle_populated_cnt;
     424             : 
     425      404337 :       fd_wsample_sample_and_remove_many( sdest->staked, staked_shuffle+staked_shuffle_populated_cnt, adtl );
     426      404337 :       staked_shuffle_populated_cnt += adtl;
     427      404337 :     }
     428             : 
     429    55823223 :     while( cursor<=fd_ulong_min( last_dest_idx, sdest->staked_cnt ) ) {
     430    55052592 :       ulong sample = staked_shuffle[ cursor ];
     431    55052592 :       if( FD_UNLIKELY( sample==FD_WSAMPLE_EMPTY         ) ) break;
     432    54955941 :       if( FD_UNLIKELY( sample==FD_WSAMPLE_INDETERMINATE ) ) break;
     433             : 
     434    54953526 :       if( FD_UNLIKELY( cursor == my_idx + stride*(stored_cnt+1UL) ) ) {
     435     3971526 :         out[ stored_cnt*out_stride + i ] = (ushort)sample;
     436     3971526 :         stored_cnt++;
     437     3971526 :       }
     438    54953526 :       cursor++;
     439    54953526 :     }
     440             :     /* If we broke from the above loop because of indeterminate, then
     441             :        unstaked_cnt==0, so the next loop will also be a no-op, and we'll
     442             :        fill the remaining array with NO_DEST, as desired. */
     443             : 
     444             :     /* Next set of samples (if any) come from the unstaked portion. If I
     445             :        am not staked, then prepare_unstaked_sampling was already called
     446             :        earlier. */
     447      869697 :     if( FD_LIKELY( (cursor<=last_dest_idx) & !!i_am_staked ) ) prepare_unstaked_sampling( sdest, leader_idx );
     448    11802912 :     while( cursor<=last_dest_idx ) {
     449    11050503 :       ulong sample = sample_unstaked( sdest );
     450    11050503 :       if( FD_UNLIKELY( sample==FD_WSAMPLE_EMPTY ) ) break;
     451             : 
     452    10933215 :       if( FD_UNLIKELY( cursor == my_idx + stride*(stored_cnt+1UL) ) ) {
     453       78399 :         out[ stored_cnt*out_stride + i ] = (ushort)sample;
     454       78399 :         stored_cnt++;
     455       78399 :       }
     456    10933215 :       cursor++;
     457    10933215 :     }
     458      869697 :     max_dest_cnt = fd_ulong_max( max_dest_cnt, stored_cnt );
     459             : 
     460             :     /* The rest of my destinations are past the end of the tree */
     461    27410088 :     for( ulong j=stored_cnt; j<dest_cnt; j++ ) out[ j*out_stride + i ] = FD_SHRED_DEST_NO_DEST;
     462             : 
     463      869697 :     fd_wsample_restore_all( sdest->staked );
     464             : 
     465      869697 :   }
     466     3750897 :   fd_ulong_store_if( !!opt_max_dest_cnt, opt_max_dest_cnt, max_dest_cnt );
     467     3750897 :   return out;
     468     3750897 : }
     469             : 
     470             : fd_shred_dest_idx_t
     471             : fd_shred_dest_pubkey_to_idx( fd_shred_dest_t   * sdest,
     472     4342074 :                              fd_pubkey_t const * pubkey     ) {
     473     4342074 :   if( FD_UNLIKELY( !memcmp( pubkey, null_pubkey.uc, 32UL ) ) ) return FD_SHRED_DEST_NO_DEST;
     474             : 
     475     4342074 :   pubkey_to_idx_t default_res[ 1 ] = {{ .idx = FD_SHRED_DEST_NO_DEST }};
     476     4342074 :   pubkey_to_idx_t * query = pubkey_to_idx_query( sdest->pubkey_to_idx_map, *pubkey, default_res );
     477             : 
     478     4342074 :   return (fd_shred_dest_idx_t)query->idx;
     479     4342074 : }
     480             : 

Generated by: LCOV version 1.14