LCOV - code coverage report
Current view: top level - disco/shred - fd_stake_ci.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 180 187 96.3 %
Date: 2025-07-01 05:00:49 Functions: 14 14 100.0 %

          Line data    Source code
       1             : #include "fd_stake_ci.h"
       2             : #include "../../util/net/fd_ip4.h" /* Just for debug */
       3             : 
       4             : #define SORT_NAME sort_pubkey
       5     3472326 : #define SORT_KEY_T fd_shred_dest_weighted_t
       6     5510922 : #define SORT_BEFORE(a,b) (memcmp( (a).pubkey.uc, (b).pubkey.uc, 32UL )<0)
       7             : #include "../../util/tmpl/fd_sort.c"
       8             : 
       9             : /* We don't have or need real contact info for the local validator, but
      10             :    we want to be able to distinguish it from staked nodes with no
      11             :    contact info. */
      12         147 : #define SELF_DUMMY_IP 1U
      13             : 
      14             : void *
      15             : fd_stake_ci_new( void             * mem,
      16          36 :                 fd_pubkey_t const * identity_key ) {
      17          36 :   fd_stake_ci_t * info = (fd_stake_ci_t *)mem;
      18             : 
      19          36 :   fd_stake_weight_t dummy_stakes[ 1 ] = {{ .key = {{0}}, .stake = 1UL }};
      20          36 :   fd_shred_dest_weighted_t dummy_dests[ 1 ] = {{ .pubkey = *identity_key, .ip4 = SELF_DUMMY_IP }};
      21             : 
      22             :   /* Initialize first 2 to satisfy invariants */
      23          36 :   info->stake_weight[ 0 ] = dummy_stakes[ 0 ];
      24          36 :   info->shred_dest  [ 0 ] = dummy_dests [ 0 ];
      25         108 :   for( ulong i=0UL; i<2UL; i++ ) {
      26          72 :     fd_per_epoch_info_t * ei = info->epoch_info + i;
      27          72 :     ei->epoch          = i;
      28          72 :     ei->start_slot     = 0UL;
      29          72 :     ei->slot_cnt       = 0UL;
      30          72 :     ei->excluded_stake = 0UL;
      31             : 
      32          72 :     ei->lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( ei->_lsched, 0UL, 0UL, 1UL, 1UL,    info->stake_weight,       0UL ) );
      33          72 :     ei->sdest  = fd_shred_dest_join   ( fd_shred_dest_new   ( ei->_sdest,  info->shred_dest, 1UL, ei->lsched, identity_key, 0UL ) );
      34          72 :   }
      35          36 :   info->identity_key[ 0 ] = *identity_key;
      36             : 
      37          36 :   return (void *)info;
      38          36 : }
      39             : 
      40          36 : fd_stake_ci_t * fd_stake_ci_join( void * mem ) { return (fd_stake_ci_t *)mem; }
      41             : 
      42          33 : void * fd_stake_ci_leave ( fd_stake_ci_t * info ) { return (void *)info; }
      43          33 : void * fd_stake_ci_delete( void          * mem  ) { return mem;          }
      44             : 
      45             : 
      46             : void
      47             : fd_stake_ci_stake_msg_init( fd_stake_ci_t               * info,
      48          93 :                             fd_stake_weight_msg_t const * msg ) {
      49          93 :   if( FD_UNLIKELY( msg->staked_cnt > MAX_SHRED_DESTS ) )
      50           0 :     FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu stakes in it,"
      51          93 :                  " but the maximum allowed is %lu", msg->staked_cnt, MAX_SHRED_DESTS ));
      52             : 
      53          93 :   info->scratch->epoch          = msg->epoch;
      54          93 :   info->scratch->start_slot     = msg->start_slot;
      55          93 :   info->scratch->slot_cnt       = msg->slot_cnt;
      56          93 :   info->scratch->staked_cnt     = msg->staked_cnt;
      57          93 :   info->scratch->excluded_stake = msg->excluded_stake;
      58             : 
      59          93 :   fd_memcpy( info->stake_weight, msg->weights, msg->staked_cnt*sizeof(fd_stake_weight_t) );
      60          93 : }
      61             : 
      62             : static inline void
      63         177 : log_summary( char const * msg, fd_stake_ci_t * info ) {
      64             : #if 0
      65             :   fd_per_epoch_info_t const * ei = info->epoch_info;
      66             :   FD_LOG_NOTICE(( "Dumping stake contact information because %s", msg ));
      67             :   for( ulong i=0UL; i<2UL; i++ ) {
      68             :     FD_LOG_NOTICE(( "  Dumping shred destination details for epoch %lu, slots [%lu, %lu)", ei[i].epoch, ei[i].start_slot, ei[i].start_slot+ei[i].slot_cnt ));
      69             :     fd_shred_dest_t * sdest = ei[i].sdest;
      70             :     for( fd_shred_dest_idx_t j=0; j<(fd_shred_dest_idx_t)fd_shred_dest_cnt_all( sdest ); j++ ) {
      71             :       fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( sdest, j );
      72             :       FD_LOG_NOTICE(( "    %16lx  %20lu " FD_IP4_ADDR_FMT " %hu ", *(ulong *)dest->pubkey.uc, dest->stake_lamports, FD_IP4_ADDR_FMT_ARGS( dest->ip4 ), dest->port ));
      73             :     }
      74             :   }
      75             : #else
      76         177 :   (void)msg;
      77         177 :   (void)info;
      78         177 : #endif
      79         177 : }
      80             : 
      81             : #define SET_NAME unhit_set
      82          90 : #define SET_MAX  MAX_SHRED_DESTS
      83             : #include "../../util/tmpl/fd_set.c"
      84             : 
      85             : void
      86          90 : fd_stake_ci_stake_msg_fini( fd_stake_ci_t * info ) {
      87             :   /* The grossness here is a sign our abstractions are wrong and need to
      88             :      be fixed instead of just patched.  We need to generate weighted
      89             :      shred destinations using a combination of the new stake information
      90             :      and whatever contact info we previously knew. */
      91          90 :   ulong epoch                  = info->scratch->epoch;
      92          90 :   ulong staked_cnt             = info->scratch->staked_cnt;
      93             : 
      94             :   /* Just take the first one arbitrarily because they both have the same
      95             :      contact info, other than possibly some staked nodes with no contact
      96             :      info. */
      97          90 :   fd_shred_dest_t * existing_sdest    = info->epoch_info->sdest;
      98          90 :   ulong             existing_dest_cnt = fd_shred_dest_cnt_all( existing_sdest );
      99             : 
     100             :   /* Keep track of the destinations in existing_sdest that are not
     101             :      staked in this new epoch, i.e. the ones we don't hit in the loop
     102             :      below. */
     103          90 :   unhit_set_t _unhit[ unhit_set_word_cnt ];
     104             :   /* This memsets to 0, right before we memset to 1, and is probably
     105             :      unnecessary, but using it without joining seems like a hack. */
     106          90 :   unhit_set_t * unhit = unhit_set_join( unhit_set_new( _unhit ) );
     107          90 :   unhit_set_full( unhit );
     108             : 
     109      482715 :   for( ulong i=0UL; i<staked_cnt; i++ ) {
     110      482625 :     fd_shred_dest_idx_t old_idx = fd_shred_dest_pubkey_to_idx( existing_sdest, &(info->stake_weight[ i ].key) );
     111      482625 :     fd_shred_dest_weighted_t * in_prev = fd_shred_dest_idx_to_dest( existing_sdest, old_idx );
     112      482625 :     info->shred_dest[ i ] = *in_prev;
     113      482625 :     if( FD_UNLIKELY( old_idx==FD_SHRED_DEST_NO_DEST ) ) {
     114             :       /* We got the generic empty entry, so fixup the pubkey */
     115      120711 :       info->shred_dest[ i ].pubkey = info->stake_weight[ i ].key;
     116      361914 :     } else {
     117      361914 :       unhit_set_remove( unhit, old_idx );
     118      361914 :     }
     119      482625 :     info->shred_dest[ i ].stake_lamports = info->stake_weight[ i ].stake;
     120      482625 :   }
     121             : 
     122          90 :   int any_destaked = 0;
     123          90 :   ulong j = staked_cnt;
     124         279 :   for( ulong idx=unhit_set_iter_init( unhit ); (idx<existing_dest_cnt) & (!unhit_set_iter_done( idx )) & (j<MAX_SHRED_DESTS);
     125         189 :              idx=unhit_set_iter_next( unhit, idx ) ) {
     126         189 :     fd_shred_dest_weighted_t * in_prev = fd_shred_dest_idx_to_dest( existing_sdest, (fd_shred_dest_idx_t)idx );
     127         189 :     if( FD_LIKELY( in_prev->ip4 ) ) {
     128         147 :       info->shred_dest[ j ] = *in_prev;
     129         147 :       any_destaked |= (in_prev->stake_lamports > 0UL);
     130         147 :       info->shred_dest[ j ].stake_lamports = 0UL;
     131         147 :       j++;
     132         147 :     }
     133         189 :   }
     134             : 
     135          90 :   unhit_set_delete( unhit_set_leave( unhit ) );
     136             : 
     137          90 :   if( FD_UNLIKELY( any_destaked ) ) {
     138             :     /* The unstaked list might be a little out of order because the
     139             :        destinations that were previously staked will be at the start of
     140             :        the unstaked list, sorted by their previous stake, instead of
     141             :        where they should be.  If there weren't any destaked, then the
     142             :        only unstaked nodes come from the previous list, which we know
     143             :        was in order, perhaps skipping some, which doesn't ruin the
     144             :        order. */
     145          18 :     sort_pubkey_inplace( info->shred_dest + staked_cnt, j - staked_cnt );
     146          18 :   }
     147             : 
     148             :   /* Now we have a plausible shred_dest list. */
     149             : 
     150             :   /* Clear the existing info */
     151          90 :   fd_per_epoch_info_t * new_ei = info->epoch_info + (epoch % 2UL);
     152          90 :   fd_shred_dest_delete   ( fd_shred_dest_leave   ( new_ei->sdest  ) );
     153          90 :   fd_epoch_leaders_delete( fd_epoch_leaders_leave( new_ei->lsched ) );
     154             : 
     155             :   /* And create the new one */
     156          90 :   ulong excluded_stake = info->scratch->excluded_stake;
     157             : 
     158          90 :   new_ei->epoch          = epoch;
     159          90 :   new_ei->start_slot     = info->scratch->start_slot;
     160          90 :   new_ei->slot_cnt       = info->scratch->slot_cnt;
     161          90 :   new_ei->excluded_stake = excluded_stake;
     162             : 
     163          90 :   new_ei->lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( new_ei->_lsched, epoch, new_ei->start_slot, new_ei->slot_cnt,
     164          90 :                                                                 staked_cnt, info->stake_weight,     excluded_stake ) );
     165          90 :   new_ei->sdest  = fd_shred_dest_join   ( fd_shred_dest_new   ( new_ei->_sdest, info->shred_dest, j,
     166          90 :                                                                 new_ei->lsched, info->identity_key, excluded_stake ) );
     167          90 :   log_summary( "stake update", info );
     168          90 : }
     169             : 
     170          90 : fd_shred_dest_weighted_t * fd_stake_ci_dest_add_init( fd_stake_ci_t * info ) { return info->shred_dest; }
     171             : 
     172             : static inline void
     173             : fd_stake_ci_dest_add_fini_impl( fd_stake_ci_t       * info,
     174             :                                 ulong                 cnt,
     175         174 :                                 fd_per_epoch_info_t * ei ) {
     176             :   /* Initially we start with one list containing S+U staked and unstaked
     177             :      destinations jumbled together.  In order to update sdest, we need
     178             :      to convert the list to S' staked destinations (taken from the
     179             :      existing sdest, though possibly updated) followed by U unstaked
     180             :      destinations.
     181             : 
     182             :      It's possible to do this in place, but at a cost of additional
     183             :      complexity (similar to memcpy vs memmove).  Rather than do that, we
     184             :      build the combined list in shred_dest_temp. */
     185             : 
     186         174 :   ulong found_unstaked_cnt = 0UL;
     187         174 :   int   any_new_unstaked   = 0;
     188             : 
     189         174 :   ulong const staked_cnt = fd_shred_dest_cnt_staked( ei->sdest );
     190         174 :   ulong j = staked_cnt;
     191             : 
     192     3859698 :   for( ulong i=0UL; i<cnt; i++ ) {
     193     3859524 :     fd_shred_dest_idx_t idx = fd_shred_dest_pubkey_to_idx( ei->sdest, &(info->shred_dest[ i ].pubkey) );
     194     3859524 :     fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( ei->sdest, idx );
     195     3859524 :     if( FD_UNLIKELY( (dest->stake_lamports==0UL)&(j<MAX_SHRED_DESTS) ) ) {
     196             :       /* Copy this destination to the unstaked part of the new list.
     197             :          This also handles the new unstaked case */
     198      482760 :       info->shred_dest_temp[ j ] = info->shred_dest[ i ];
     199      482760 :       info->shred_dest_temp[ j ].stake_lamports = 0UL;
     200      482760 :       j++;
     201      482760 :     }
     202             : 
     203     3859524 :     if( FD_LIKELY( idx!=FD_SHRED_DEST_NO_DEST ) ) {
     204     3738693 :       dest->ip4  = info->shred_dest[ i ].ip4;
     205     3738693 :       dest->port = info->shred_dest[ i ].port;
     206     3738693 :     }
     207             : 
     208     3859524 :     any_new_unstaked   |= (idx==FD_SHRED_DEST_NO_DEST);
     209     3859524 :     found_unstaked_cnt += (ulong)((idx!=FD_SHRED_DEST_NO_DEST) & (dest->stake_lamports==0UL));
     210     3859524 :   }
     211             : 
     212         174 :   if( FD_LIKELY( !any_new_unstaked && found_unstaked_cnt==fd_shred_dest_cnt_unstaked( ei->sdest ) ) ) {
     213             :     /* Because any_new_unstaked==0, the set of unstaked nodes in this
     214             :        update is fully contained in the set of unstaked nodes in the
     215             :        sdest.  Then additionally, because the sets are the same size,
     216             :        they must actually be equal.  In this case, we've already updated
     217             :        the existing shred_dest_weighted with the newest contact info we
     218             :        have, so there's nothing else to do. */
     219          48 :     return;
     220          48 :   }
     221             : 
     222             :   /* Otherwise something more significant changed and we need to
     223             :      regenerate the sdest.  At this point, elements [staked_cnt, j) now
     224             :      contain all the current unstaked destinations. */
     225             : 
     226             :   /* Copy staked nodes to [0, staked_cnt). We've already applied the
     227             :      updated contact info to these. */
     228     1929831 :   for( ulong i=0UL; i<staked_cnt; i++ )
     229     1929705 :     info->shred_dest_temp[ i ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)i );
     230             : 
     231             :   /* The staked nodes are sorted properly because we use the index from
     232             :      sdest.  We need to sort the unstaked nodes by pubkey though. */
     233         126 :   sort_pubkey_inplace( info->shred_dest_temp + staked_cnt, j - staked_cnt );
     234             : 
     235         126 :   fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
     236             : 
     237         126 :   ei->sdest  = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, info->shred_dest_temp, j, ei->lsched, info->identity_key,
     238         126 :                                                       ei->excluded_stake ) );
     239             : 
     240         126 :   if( FD_UNLIKELY( ei->sdest==NULL ) ) {
     241             :     /* Happens if the identity key is not present, which can only happen
     242             :        if the current validator's stake is not in the top 40,200.  We
     243             :        could initialize ei->sdest to a dummy value, but having the wrong
     244             :        stake weights could lead to potentially slashable issues
     245             :        elsewhere (e.g. we might product a block when we're not actually
     246             :        leader).  We're just going to terminate in this case. */
     247           0 :     FD_LOG_ERR(( "Too many validators have higher stake than this validator.  Cannot continue." ));
     248           0 :   }
     249         126 : }
     250             : 
     251             : 
     252             : void
     253             : fd_stake_ci_dest_add_fini( fd_stake_ci_t * info,
     254          87 :                            ulong           cnt ) {
     255             :   /* The Rust side uses tvu_peers which typically excludes the local
     256             :      validator.  In some cases, after a set-identity, it might still
     257             :      include the local validator though.  If it doesn't include it, we
     258             :      need to add the local validator back. */
     259          87 :   FD_TEST( cnt<MAX_SHRED_DESTS );
     260          87 :   ulong i=0UL;
     261     1929762 :   for(; i<cnt; i++ ) if( FD_UNLIKELY( 0==memcmp( info->shred_dest[ i ].pubkey.uc, info->identity_key, 32UL ) ) ) break;
     262             : 
     263          87 :   if( FD_LIKELY( i==cnt ) ) {
     264          87 :     fd_shred_dest_weighted_t self_dests = { .pubkey = info->identity_key[ 0 ], .ip4 = SELF_DUMMY_IP };
     265          87 :     info->shred_dest[ cnt++ ] = self_dests;
     266          87 :   } else {
     267           0 :     info->shred_dest[ i ].ip4 = SELF_DUMMY_IP;
     268           0 :   }
     269             : 
     270             :   /* Update both of them */
     271          87 :   fd_stake_ci_dest_add_fini_impl( info, cnt, info->epoch_info + 0UL );
     272          87 :   fd_stake_ci_dest_add_fini_impl( info, cnt, info->epoch_info + 1UL );
     273             : 
     274          87 :   log_summary( "dest update", info );
     275          87 : }
     276             : 
     277             : 
     278             : /* Returns a value in [0, 2) if found, and ULONG_MAX if not */
     279             : static inline ulong
     280             : fd_stake_ci_get_idx_for_slot( fd_stake_ci_t const * info,
     281        1179 :                               ulong                 slot ) {
     282        1179 :   fd_per_epoch_info_t const * ei = info->epoch_info;
     283        1179 :   ulong idx = ULONG_MAX;
     284        3537 :   for( ulong i=0UL; i<2UL; i++ ) idx = fd_ulong_if( (ei[i].start_slot<=slot) & (slot-ei[i].start_slot<ei[i].slot_cnt), i, idx );
     285        1179 :   return idx;
     286        1179 : }
     287             : 
     288             : 
     289             : void
     290             : fd_stake_ci_set_identity( fd_stake_ci_t *     info,
     291          12 :                           fd_pubkey_t const * identity_key ) {
     292             :   /* None of the stakes are changing, so we just need to regenerate the
     293             :      sdests, sightly adjusting the destination IP addresses.  The only
     294             :      corner case is if the new identity is not present. */
     295          36 :   for( ulong i=0UL; i<2UL; i++ ) {
     296          24 :     fd_per_epoch_info_t * ei = info->epoch_info+i;
     297             : 
     298          24 :     fd_shred_dest_idx_t old_idx = fd_shred_dest_pubkey_to_idx( ei->sdest, info->identity_key );
     299          24 :     fd_shred_dest_idx_t new_idx = fd_shred_dest_pubkey_to_idx( ei->sdest, identity_key       );
     300             : 
     301          24 :     FD_TEST( old_idx!=FD_SHRED_DEST_NO_DEST );
     302             : 
     303          24 :     if( FD_LIKELY( new_idx!=FD_SHRED_DEST_NO_DEST ) ) {
     304          18 :       fd_shred_dest_idx_to_dest( ei->sdest, old_idx )->ip4 = 0U;
     305          18 :       fd_shred_dest_idx_to_dest( ei->sdest, new_idx )->ip4 = SELF_DUMMY_IP;
     306             : 
     307          18 :       fd_shred_dest_update_source( ei->sdest, new_idx );
     308          18 :     } else {
     309           6 :       ulong staked_cnt   = fd_shred_dest_cnt_staked  ( ei->sdest );
     310           6 :       ulong unstaked_cnt = fd_shred_dest_cnt_unstaked( ei->sdest );
     311           6 :       if( FD_UNLIKELY( staked_cnt+unstaked_cnt==MAX_SHRED_DESTS ) ) {
     312           0 :         FD_LOG_ERR(( "too many validators in shred table to add a new validator with set-identity" ));
     313           0 :       }
     314             :       /* We'll add identity_key as a new unstaked validator.  First copy
     315             :          all the staked ones, then place the new validator in the spot
     316             :          where it belongs according to lexicographic order. */
     317           6 :       ulong j=0UL;
     318          24 :       for(; j<staked_cnt; j++ ) info->shred_dest_temp[ j ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)j );
     319          33 :       for(; j<staked_cnt+unstaked_cnt; j++ ) {
     320          33 :         fd_shred_dest_weighted_t * wj = fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)j );
     321          33 :         if( FD_UNLIKELY( (memcmp( wj->pubkey.uc, identity_key->uc, 32UL )>=0) ) ) break;
     322          27 :         info->shred_dest_temp[ j ] = *wj;
     323          27 :       }
     324             : 
     325           6 :       info->shred_dest_temp[ j ].pubkey         = *identity_key;
     326           6 :       info->shred_dest_temp[ j ].stake_lamports = 0UL;
     327           6 :       info->shred_dest_temp[ j ].ip4            = SELF_DUMMY_IP;
     328             : 
     329          12 :       for(; j<staked_cnt+unstaked_cnt; j++ ) info->shred_dest_temp[ j+1UL ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)j );
     330             : 
     331           6 :       fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
     332             : 
     333           6 :       ei->sdest  = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, info->shred_dest_temp, j+1UL, ei->lsched, identity_key,
     334           6 :                                                           ei->excluded_stake ) );
     335           6 :       FD_TEST( ei->sdest );
     336           6 :     }
     337             : 
     338          24 :   }
     339          12 :   *info->identity_key = *identity_key;
     340          12 : }
     341             : 
     342             : 
     343             : fd_shred_dest_t *
     344             : fd_stake_ci_get_sdest_for_slot( fd_stake_ci_t const * info,
     345         591 :                                 ulong                 slot ) {
     346         591 :   ulong idx = fd_stake_ci_get_idx_for_slot( info, slot );
     347         591 :   return idx!=ULONG_MAX ? info->epoch_info[ idx ].sdest : NULL;
     348         591 : }
     349             : 
     350             : fd_epoch_leaders_t *
     351             : fd_stake_ci_get_lsched_for_slot( fd_stake_ci_t const * info,
     352         588 :                                  ulong                 slot ) {
     353         588 :   ulong idx = fd_stake_ci_get_idx_for_slot( info, slot );
     354         588 :   return idx!=ULONG_MAX ? info->epoch_info[ idx ].lsched : NULL;
     355         588 : }

Generated by: LCOV version 1.14