LCOV - code coverage report
Current view: top level - disco/shred - fd_stake_ci.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 273 0.0 %
Date: 2026-10-22 18:10:43 Functions: 0 22 0.0 %

          Line data    Source code
       1             : #include "fd_stake_ci.h"
       2             : #include "fd_shred_dest.h"
       3             : #include "../../util/net/fd_ip4.h" /* Just for debug */
       4             : 
       5             : #define SORT_NAME sort_pubkey
       6           0 : #define SORT_KEY_T fd_shred_dest_weighted_t
       7           0 : #define SORT_BEFORE(a,b) (memcmp( (a).pubkey.uc, (b).pubkey.uc, 32UL )>0)
       8             : #include "../../util/tmpl/fd_sort.c"
       9             : 
      10             : /* We don't have or need real contact info for the local validator, but
      11             :    we want to be able to distinguish it from staked nodes with no
      12             :    contact info. */
      13           0 : #define SELF_DUMMY_IP 1U
      14             : 
      15             : void *
      16             : fd_stake_ci_new( void *              mem,
      17             :                  fd_pubkey_t const * identity_key,
      18           0 :                  ulong               seed ) {
      19           0 :   fd_stake_ci_t * info = (fd_stake_ci_t *)mem;
      20             : 
      21           0 :   fd_vote_stake_weight_t dummy_stakes[ 1 ] = {{ .vote_key = {{0}}, .id_key = {{0}}, .stake = 1UL }};
      22           0 :   fd_shred_dest_weighted_t dummy_dests[ 1 ] = {{ .pubkey = *identity_key, .ip4 = SELF_DUMMY_IP }};
      23             : 
      24             :   /* Initialize first 2 to satisfy invariants */
      25           0 :   info->vote_stake_weight[ 0 ] = dummy_stakes[ 0 ];
      26           0 :   info->shred_dest  [ 0 ] = dummy_dests [ 0 ];
      27           0 :   for( ulong i=0UL; i<2UL; i++ ) {
      28           0 :     fd_per_epoch_info_t * ei = info->epoch_info + i;
      29           0 :     ei->epoch             = i;
      30           0 :     ei->start_slot        = 0UL;
      31           0 :     ei->slot_cnt          = 0UL;
      32             : 
      33           0 :     ei->lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( ei->_lsched, 0UL, 0UL, 1UL, 1UL, info->vote_stake_weight ) );
      34           0 :     ei->sdest  = fd_shred_dest_join   ( fd_shred_dest_new   ( ei->_sdest,  info->shred_dest, 1UL, ei->lsched, identity_key, seed ) );
      35           0 :   }
      36           0 :   info->identity_key[ 0 ] = *identity_key;
      37           0 :   info->seed              = seed;
      38             : 
      39           0 :   return (void *)info;
      40           0 : }
      41             : 
      42           0 : fd_stake_ci_t * fd_stake_ci_join( void * mem ) { return (fd_stake_ci_t *)mem; }
      43             : 
      44           0 : void * fd_stake_ci_leave ( fd_stake_ci_t * info ) { return (void *)info; }
      45           0 : void * fd_stake_ci_delete( void          * mem  ) { return mem;          }
      46             : 
      47             : 
      48             : void
      49             : fd_stake_ci_stake_msg_init( fd_stake_ci_t               * info,
      50           0 :                             fd_stake_weight_msg_t const * msg ) {
      51           0 :   if( FD_UNLIKELY( msg->staked_vote_cnt > MAX_STAKE_WEIGHTS ) ) {
      52           0 :     FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu stakes in it,"
      53           0 :                  " but the maximum allowed is %lu", msg->staked_vote_cnt, MAX_STAKE_WEIGHTS ));
      54           0 :   }
      55           0 :   if( FD_UNLIKELY( msg->staked_id_cnt > MAX_STAKE_WEIGHTS ) ) {
      56           0 :     FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu id weights in it,"
      57           0 :                  " but the maximum allowed is %lu", msg->staked_id_cnt, MAX_STAKE_WEIGHTS ));
      58           0 :   }
      59           0 :   if( FD_UNLIKELY( msg->slot_cnt > MAX_SLOTS_PER_EPOCH ) ) {
      60           0 :     FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu slots in it,"
      61           0 :                  " but the maximum allowed is %lu", msg->slot_cnt, MAX_SLOTS_PER_EPOCH ));
      62           0 :   }
      63             : 
      64           0 :   info->scratch->epoch             = msg->epoch;
      65           0 :   info->scratch->start_slot        = msg->start_slot;
      66           0 :   info->scratch->slot_cnt          = msg->slot_cnt;
      67           0 :   info->scratch->staked_vote_cnt   = msg->staked_vote_cnt;
      68           0 :   info->scratch->staked_id_cnt     = msg->staked_id_cnt;
      69             : 
      70           0 :   fd_memcpy( info->vote_stake_weight, fd_stake_weight_msg_stake_weights( msg ), msg->staked_vote_cnt*sizeof(fd_vote_stake_weight_t) );
      71           0 :   fd_memcpy( info->stake_weight,      fd_stake_weight_msg_id_weights( msg ),    msg->staked_id_cnt*sizeof(fd_stake_weight_t) );
      72           0 : }
      73             : 
      74             : void
      75             : fd_stake_ci_epoch_msg_init( fd_stake_ci_t *             info,
      76           0 :                             fd_epoch_info_msg_t const * msg ) {
      77           0 :   if( FD_UNLIKELY( msg->staked_vote_cnt > MAX_STAKE_WEIGHTS ) ) {
      78           0 :     FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu stakes in it,"
      79           0 :                  " but the maximum allowed is %lu", msg->staked_vote_cnt, MAX_STAKE_WEIGHTS ));
      80           0 :   }
      81           0 :   if( FD_UNLIKELY( msg->staked_id_cnt > MAX_STAKE_WEIGHTS ) ) {
      82           0 :     FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu id weights in it,"
      83           0 :                  " but the maximum allowed is %lu", msg->staked_id_cnt, MAX_STAKE_WEIGHTS ));
      84           0 :   }
      85           0 :   if( FD_UNLIKELY( msg->slot_cnt > MAX_SLOTS_PER_EPOCH ) ) {
      86           0 :     FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu slots in it,"
      87           0 :                  " but the maximum allowed is %lu", msg->slot_cnt, MAX_SLOTS_PER_EPOCH ));
      88           0 :   }
      89             : 
      90             : 
      91           0 :   info->scratch->epoch               = msg->epoch;
      92           0 :   info->scratch->start_slot          = msg->start_slot;
      93           0 :   info->scratch->slot_cnt            = msg->slot_cnt;
      94           0 :   info->scratch->staked_vote_cnt     = msg->staked_vote_cnt;
      95           0 :   info->scratch->staked_id_cnt       = msg->staked_id_cnt;
      96             : 
      97           0 :   fd_memcpy( info->vote_stake_weight, fd_epoch_info_msg_stake_weights( msg ), msg->staked_vote_cnt*sizeof(fd_vote_stake_weight_t) );
      98           0 :   fd_memcpy( info->stake_weight,      fd_epoch_info_msg_id_weights( msg ),    msg->staked_id_cnt*sizeof(fd_stake_weight_t) );
      99           0 : }
     100             : 
     101             : static inline void
     102           0 : log_summary( char const * msg, fd_stake_ci_t * info ) {
     103             : #if 0
     104             :   fd_per_epoch_info_t const * ei = info->epoch_info;
     105             :   FD_LOG_NOTICE(( "Dumping stake contact information because %s", msg ));
     106             :   for( ulong i=0UL; i<2UL; i++ ) {
     107             :     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 ));
     108             :     fd_shred_dest_t * sdest = ei[i].sdest;
     109             :     for( fd_shred_dest_idx_t j=0; j<(fd_shred_dest_idx_t)fd_shred_dest_cnt_all( sdest ); j++ ) {
     110             :       fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( sdest, j );
     111             :       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 ));
     112             :     }
     113             :   }
     114             : #else
     115           0 :   (void)msg;
     116           0 :   (void)info;
     117           0 : #endif
     118           0 : }
     119             : 
     120             : #define SET_NAME unhit_set
     121           0 : #define SET_MAX  MAX_SHRED_DESTS
     122             : #include "../../util/tmpl/fd_set.c"
     123             : 
     124             : void
     125           0 : fd_stake_ci_stake_msg_fini( fd_stake_ci_t * info ) {
     126             :   /* The grossness here is a sign our abstractions are wrong and need to
     127             :      be fixed instead of just patched.  We need to generate weighted
     128             :      shred destinations using a combination of the new stake information
     129             :      and whatever contact info we previously knew. */
     130           0 :   ulong epoch             = info->scratch->epoch;
     131           0 :   ulong staked_cnt        = info->scratch->staked_id_cnt;
     132             : 
     133             :   /* Just take the first one arbitrarily because they both have the same
     134             :      contact info, other than possibly some staked nodes with no contact
     135             :      info. */
     136           0 :   fd_shred_dest_t * existing_sdest    = info->epoch_info->sdest;
     137           0 :   ulong             existing_dest_cnt = fd_shred_dest_cnt_all( existing_sdest );
     138             : 
     139             :   /* Keep track of the destinations in existing_sdest that are not
     140             :      staked in this new epoch, i.e. the ones we don't hit in the loop
     141             :      below. */
     142           0 :   unhit_set_t _unhit[ unhit_set_word_cnt ];
     143             :   /* This memsets to 0, right before we memset to 1, and is probably
     144             :      unnecessary, but using it without joining seems like a hack. */
     145           0 :   unhit_set_t * unhit = unhit_set_join( unhit_set_new( _unhit ) );
     146           0 :   unhit_set_full( unhit );
     147             : 
     148           0 :   for( ulong i=0UL; i<staked_cnt; i++ ) {
     149           0 :     fd_shred_dest_idx_t old_idx = fd_shred_dest_pubkey_to_idx( existing_sdest, &(info->stake_weight[ i ].key) );
     150           0 :     fd_shred_dest_weighted_t * in_prev = fd_shred_dest_idx_to_dest( existing_sdest, old_idx );
     151           0 :     info->shred_dest[ i ] = *in_prev;
     152           0 :     if( FD_UNLIKELY( old_idx==FD_SHRED_DEST_NO_DEST ) ) {
     153             :       /* We got the generic empty entry, so fixup the pubkey */
     154           0 :       info->shred_dest[ i ].pubkey = info->stake_weight[ i ].key;
     155           0 :     } else {
     156           0 :       unhit_set_remove( unhit, old_idx );
     157           0 :     }
     158           0 :     info->shred_dest[ i ].stake_lamports = info->stake_weight[ i ].stake;
     159           0 :   }
     160             : 
     161           0 :   int any_destaked = 0;
     162           0 :   ulong j = staked_cnt;
     163           0 :   for( ulong idx=unhit_set_iter_init( unhit ); (idx<existing_dest_cnt) & (!unhit_set_iter_done( idx )) & (j<MAX_SHRED_DESTS);
     164           0 :              idx=unhit_set_iter_next( unhit, idx ) ) {
     165           0 :     fd_shred_dest_weighted_t * in_prev = fd_shred_dest_idx_to_dest( existing_sdest, (fd_shred_dest_idx_t)idx );
     166           0 :     if( FD_LIKELY( in_prev->ip4 ) ) {
     167           0 :       info->shred_dest[ j ] = *in_prev;
     168           0 :       any_destaked |= (in_prev->stake_lamports > 0UL);
     169           0 :       info->shred_dest[ j ].stake_lamports = 0UL;
     170           0 :       j++;
     171           0 :     }
     172           0 :   }
     173             : 
     174           0 :   unhit_set_delete( unhit_set_leave( unhit ) );
     175             : 
     176           0 :   if( FD_UNLIKELY( any_destaked ) ) {
     177             :     /* The unstaked list might be a little out of order because the
     178             :        destinations that were previously staked will be at the start of
     179             :        the unstaked list, sorted by their previous stake, instead of
     180             :        where they should be.  If there weren't any destaked, then the
     181             :        only unstaked nodes come from the previous list, which we know
     182             :        was in order, perhaps skipping some, which doesn't ruin the
     183             :        order. */
     184           0 :     sort_pubkey_inplace( info->shred_dest + staked_cnt, j - staked_cnt );
     185           0 :   }
     186             : 
     187             :   /* Now we have a plausible shred_dest list. */
     188             : 
     189             :   /* Clear the existing info */
     190           0 :   fd_per_epoch_info_t * new_ei = info->epoch_info + (epoch % 2UL);
     191           0 :   fd_shred_dest_delete   ( fd_shred_dest_leave   ( new_ei->sdest  ) );
     192           0 :   fd_epoch_leaders_delete( fd_epoch_leaders_leave( new_ei->lsched ) );
     193             : 
     194             :   /* And create the new one */
     195           0 :   new_ei->epoch             = epoch;
     196           0 :   new_ei->start_slot        = info->scratch->start_slot;
     197           0 :   new_ei->slot_cnt          = info->scratch->slot_cnt;
     198             : 
     199           0 :   new_ei->lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( new_ei->_lsched, epoch, new_ei->start_slot, new_ei->slot_cnt,
     200           0 :                                                                 info->scratch->staked_vote_cnt, info->vote_stake_weight ) );
     201           0 :   new_ei->sdest  = fd_shred_dest_join   ( fd_shred_dest_new   ( new_ei->_sdest, info->shred_dest, j,
     202           0 :                                                                 new_ei->lsched, info->identity_key, info->seed ) );
     203           0 :   log_summary( "stake update", info );
     204           0 : }
     205             : 
     206             : void
     207           0 : fd_stake_ci_epoch_msg_fini( fd_stake_ci_t * info ) {
     208           0 :   fd_stake_ci_stake_msg_fini( info );
     209           0 : }
     210             : 
     211           0 : fd_shred_dest_weighted_t * fd_stake_ci_dest_add_init( fd_stake_ci_t * info ) { return info->shred_dest; }
     212             : 
     213             : static inline void
     214             : fd_stake_ci_dest_add_fini_impl( fd_stake_ci_t       * info,
     215             :                                 ulong                 cnt,
     216           0 :                                 fd_per_epoch_info_t * ei ) {
     217             :   /* Initially we start with one list containing S+U staked and unstaked
     218             :      destinations jumbled together.  In order to update sdest, we need
     219             :      to convert the list to S' staked destinations (taken from the
     220             :      existing sdest, though possibly updated) followed by U unstaked
     221             :      destinations.
     222             : 
     223             :      It's possible to do this in place, but at a cost of additional
     224             :      complexity (similar to memcpy vs memmove).  Rather than do that, we
     225             :      build the combined list in shred_dest_temp. */
     226             : 
     227           0 :   ulong found_unstaked_cnt = 0UL;
     228           0 :   int   any_new_unstaked   = 0;
     229             : 
     230           0 :   ulong const staked_cnt = fd_shred_dest_cnt_staked( ei->sdest );
     231           0 :   ulong j = staked_cnt;
     232             : 
     233           0 :   for( ulong i=0UL; i<cnt; i++ ) {
     234           0 :     fd_shred_dest_idx_t idx = fd_shred_dest_pubkey_to_idx( ei->sdest, &(info->shred_dest[ i ].pubkey) );
     235           0 :     fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( ei->sdest, idx );
     236           0 :     if( FD_UNLIKELY( (dest->stake_lamports==0UL)&(j<MAX_SHRED_DESTS) ) ) {
     237             :       /* Copy this destination to the unstaked part of the new list.
     238             :          This also handles the new unstaked case */
     239           0 :       info->shred_dest_temp[ j ] = info->shred_dest[ i ];
     240           0 :       info->shred_dest_temp[ j ].stake_lamports = 0UL;
     241           0 :       j++;
     242           0 :     }
     243             : 
     244           0 :     if( FD_LIKELY( idx!=FD_SHRED_DEST_NO_DEST ) ) {
     245           0 :       dest->ip4  = info->shred_dest[ i ].ip4;
     246           0 :       dest->port = info->shred_dest[ i ].port;
     247           0 :     }
     248             : 
     249           0 :     any_new_unstaked   |= (idx==FD_SHRED_DEST_NO_DEST);
     250           0 :     found_unstaked_cnt += (ulong)((idx!=FD_SHRED_DEST_NO_DEST) & (dest->stake_lamports==0UL));
     251           0 :   }
     252             : 
     253           0 :   if( FD_LIKELY( !any_new_unstaked && found_unstaked_cnt==fd_shred_dest_cnt_unstaked( ei->sdest ) ) ) {
     254             :     /* Because any_new_unstaked==0, the set of unstaked nodes in this
     255             :        update is fully contained in the set of unstaked nodes in the
     256             :        sdest.  Then additionally, because the sets are the same size,
     257             :        they must actually be equal.  In this case, we've already updated
     258             :        the existing shred_dest_weighted with the newest contact info we
     259             :        have, so there's nothing else to do. */
     260           0 :     return;
     261           0 :   }
     262             : 
     263             :   /* Otherwise something more significant changed and we need to
     264             :      regenerate the sdest.  At this point, elements [staked_cnt, j) now
     265             :      contain all the current unstaked destinations. */
     266             : 
     267             :   /* Copy staked nodes to [0, staked_cnt). We've already applied the
     268             :      updated contact info to these. */
     269           0 :   for( ulong i=0UL; i<staked_cnt; i++ )
     270           0 :     info->shred_dest_temp[ i ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)i );
     271             : 
     272             :   /* The staked nodes are sorted properly because we use the index from
     273             :      sdest.  We need to sort the unstaked nodes by pubkey though. */
     274           0 :   sort_pubkey_inplace( info->shred_dest_temp + staked_cnt, j - staked_cnt );
     275             : 
     276           0 :   fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
     277             : 
     278           0 :   ei->sdest  = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, info->shred_dest_temp, j, ei->lsched,
     279           0 :                                                       info->identity_key, info->seed ) );
     280             : 
     281           0 :   if( FD_UNLIKELY( ei->sdest==NULL ) ) {
     282             :     /* The bounded destination table must always retain our identity. */
     283           0 :     FD_LOG_ERR(( "Identity key is missing from the shred destination table.  Cannot continue." ));
     284           0 :   }
     285           0 : }
     286             : 
     287             : 
     288             : void
     289             : fd_stake_ci_dest_add_fini( fd_stake_ci_t * info,
     290           0 :                            ulong           cnt ) {
     291             :   /* The Rust side uses tvu_peers which typically excludes the local
     292             :      validator.  In some cases, after a set-identity, it might still
     293             :      include the local validator though.  If it doesn't include it, we
     294             :      need to add the local validator back. */
     295           0 :   FD_TEST( cnt<MAX_SHRED_DESTS );
     296           0 :   ulong i=0UL;
     297           0 :   for(; i<cnt; i++ ) if( FD_UNLIKELY( 0==memcmp( info->shred_dest[ i ].pubkey.uc, info->identity_key, 32UL ) ) ) break;
     298             : 
     299           0 :   if( FD_LIKELY( i==cnt ) ) {
     300           0 :     fd_shred_dest_weighted_t self_dests = { .pubkey = info->identity_key[ 0 ], .ip4 = SELF_DUMMY_IP };
     301           0 :     info->shred_dest[ cnt++ ] = self_dests;
     302           0 :   } else {
     303           0 :     info->shred_dest[ i ].ip4 = SELF_DUMMY_IP;
     304           0 :   }
     305             : 
     306             :   /* Update both of them */
     307           0 :   fd_stake_ci_dest_add_fini_impl( info, cnt, info->epoch_info + 0UL );
     308           0 :   fd_stake_ci_dest_add_fini_impl( info, cnt, info->epoch_info + 1UL );
     309             : 
     310           0 :   log_summary( "dest update", info );
     311           0 : }
     312             : 
     313             : 
     314             : /* Returns a value in [0, 2) if found, and ULONG_MAX if not */
     315             : static inline ulong
     316             : fd_stake_ci_get_idx_for_slot( fd_stake_ci_t const * info,
     317           0 :                               ulong                 slot ) {
     318           0 :   fd_per_epoch_info_t const * ei = info->epoch_info;
     319           0 :   ulong idx = ULONG_MAX;
     320           0 :   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 );
     321           0 :   return idx;
     322           0 : }
     323             : 
     324             : 
     325             : void
     326             : fd_stake_ci_set_identity( fd_stake_ci_t *     info,
     327           0 :                           fd_pubkey_t const * identity_key ) {
     328             :   /* None of the stakes are changing, so we just need to regenerate the
     329             :      sdests, slightly adjusting the destination IP addresses.  The only
     330             :      corner case is if the new identity is not present. */
     331           0 :   for( ulong i=0UL; i<2UL; i++ ) {
     332           0 :     fd_per_epoch_info_t * ei = info->epoch_info+i;
     333             : 
     334           0 :     fd_shred_dest_idx_t old_idx = fd_shred_dest_pubkey_to_idx( ei->sdest, info->identity_key );
     335           0 :     fd_shred_dest_idx_t new_idx = fd_shred_dest_pubkey_to_idx( ei->sdest, identity_key       );
     336             : 
     337           0 :     FD_TEST( old_idx!=FD_SHRED_DEST_NO_DEST );
     338             : 
     339           0 :     if( FD_LIKELY( new_idx!=FD_SHRED_DEST_NO_DEST ) ) {
     340           0 :       fd_shred_dest_idx_to_dest( ei->sdest, old_idx )->ip4 = 0U;
     341           0 :       fd_shred_dest_idx_to_dest( ei->sdest, new_idx )->ip4 = SELF_DUMMY_IP;
     342             : 
     343           0 :       fd_shred_dest_update_source( ei->sdest, new_idx );
     344           0 :     } else {
     345           0 :       ulong staked_cnt   = fd_shred_dest_cnt_staked  ( ei->sdest );
     346           0 :       ulong unstaked_cnt = fd_shred_dest_cnt_unstaked( ei->sdest );
     347           0 :       if( FD_UNLIKELY( staked_cnt+unstaked_cnt==MAX_SHRED_DESTS ) ) {
     348           0 :         FD_LOG_ERR(( "too many validators in shred table to add a new validator with set-identity" ));
     349           0 :       }
     350             :       /* We'll add identity_key as a new unstaked validator.  First copy
     351             :          all the staked ones, then place the new validator in the spot
     352             :          where it belongs according to lexicographic order. */
     353           0 :       ulong j=0UL;
     354           0 :       for(; j<staked_cnt; j++ ) info->shred_dest_temp[ j ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)j );
     355           0 :       for(; j<staked_cnt+unstaked_cnt; j++ ) {
     356           0 :         fd_shred_dest_weighted_t * wj = fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)j );
     357           0 :         if( FD_UNLIKELY( (memcmp( wj->pubkey.uc, identity_key->uc, 32UL )<=0) ) ) break;
     358           0 :         info->shred_dest_temp[ j ] = *wj;
     359           0 :       }
     360             : 
     361           0 :       info->shred_dest_temp[ j ].pubkey         = *identity_key;
     362           0 :       info->shred_dest_temp[ j ].stake_lamports = 0UL;
     363           0 :       info->shred_dest_temp[ j ].ip4            = SELF_DUMMY_IP;
     364             : 
     365           0 :       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 );
     366             : 
     367           0 :       fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
     368             : 
     369           0 :       ei->sdest  = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, info->shred_dest_temp, j+1UL, ei->lsched,
     370           0 :                                                           identity_key, info->seed ) );
     371           0 :       FD_TEST( ei->sdest );
     372           0 :     }
     373             : 
     374           0 :   }
     375           0 :   *info->identity_key = *identity_key;
     376           0 : }
     377             : 
     378             : static void
     379             : refresh_sdest( fd_stake_ci_t *            info,
     380             :                fd_shred_dest_weighted_t * shred_dest_temp,
     381             :                ulong                      cnt,
     382             :                ulong                      staked_cnt,
     383           0 :                fd_per_epoch_info_t *      ei ) {
     384           0 :   sort_pubkey_inplace( shred_dest_temp + staked_cnt, cnt - staked_cnt );
     385             : 
     386           0 :   fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
     387           0 :   ei->sdest = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, shred_dest_temp, cnt, ei->lsched,
     388           0 :                                                      info->identity_key, info->seed ) );
     389           0 :   if( FD_UNLIKELY( ei->sdest==NULL ) ) {
     390           0 :     FD_LOG_ERR(( "Identity key is missing from the shred destination table.  Cannot continue." ));
     391           0 :   }
     392           0 : }
     393             : 
     394             : static void
     395             : ci_dest_add_one_unstaked( fd_stake_ci_t *            info,
     396             :                           fd_shred_dest_weighted_t * new_entry,
     397           0 :                           fd_per_epoch_info_t *      ei ) {
     398           0 :   if( fd_shred_dest_cnt_all( ei->sdest )>=MAX_SHRED_DESTS ) {
     399           0 :     FD_LOG_WARNING(( "Too many validators in shred table to add a new validator." ));
     400           0 :     return;
     401           0 :   }
     402           0 :   ulong cur_cnt = fd_shred_dest_cnt_all( ei->sdest );
     403           0 :   for( ulong i=0UL; i<cur_cnt; i++ ) {
     404           0 :     info->shred_dest_temp[ i ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)i );
     405           0 :   }
     406             : 
     407             :   /* TODO: Alternative batched copy using memcpy. Check with Philip if safe */
     408             :   // fd_shred_dest_weighted_t * cur_dest = ei->sdest->all_destinations;
     409             :   // fd_memcpy( info->shred_dest_temp, cur_dest, sizeof(fd_shred_dest_weighted_t)*cur_cnt );
     410           0 :   info->shred_dest_temp[ cur_cnt++ ] = *new_entry;
     411           0 :   refresh_sdest( info, info->shred_dest_temp, cur_cnt, fd_shred_dest_cnt_staked( ei->sdest ), ei );
     412           0 : }
     413             : 
     414             : static void
     415             : ci_dest_update_impl( fd_stake_ci_t *       info,
     416             :                      fd_pubkey_t const *   pubkey,
     417             :                      uint                  ip4,
     418             :                      ushort                port,
     419           0 :                      fd_per_epoch_info_t * ei ) {
     420           0 :   fd_shred_dest_idx_t idx = fd_shred_dest_pubkey_to_idx( ei->sdest, pubkey );
     421           0 :   if( idx==FD_SHRED_DEST_NO_DEST ) {
     422           0 :     fd_shred_dest_weighted_t new_entry = { .pubkey = *pubkey, .ip4 = ip4, .port = port, .stake_lamports = 0UL };
     423           0 :     ci_dest_add_one_unstaked( info, &new_entry, ei );
     424           0 :     return;
     425           0 :   }
     426           0 :   fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( ei->sdest, idx );
     427           0 :   dest->ip4                       = ip4;
     428           0 :   dest->port                      = port;
     429           0 : }
     430             : 
     431             : static void
     432             : ci_dest_remove_impl( fd_stake_ci_t *       info,
     433             :                      fd_pubkey_t const *   pubkey,
     434           0 :                      fd_per_epoch_info_t * ei ) {
     435           0 :   fd_shred_dest_idx_t idx = fd_shred_dest_pubkey_to_idx( ei->sdest, pubkey );
     436           0 :   if( FD_UNLIKELY( idx==FD_SHRED_DEST_NO_DEST ) ) return;
     437             : 
     438           0 :   fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( ei->sdest, idx );
     439           0 :   if( FD_UNLIKELY( dest->stake_lamports>0UL ) ) {
     440             :     /* A staked entry is not "removed", instead its "stale" address is
     441             :        retained */
     442           0 :     return;
     443           0 :   }
     444           0 :   ulong cur_cnt = fd_shred_dest_cnt_all( ei->sdest );
     445           0 :   for( ulong i=0UL, j=0UL; i<cur_cnt; i++ ) {
     446           0 :     if( FD_UNLIKELY( i==idx ) ) continue;
     447           0 :     info->shred_dest_temp[ j++ ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t) i );
     448           0 :   }
     449             :   /* TODO: Alternative batched copy using memcpy. Check with Philip if this is safe */
     450             :   // fd_shred_dest_weighted_t * cur_dest = ei->sdest->all_destinations;
     451             :   // fd_memcpy( info->shred_dest_temp, cur_dest, sizeof(fd_shred_dest_weighted_t)*(idx) );
     452             :   // fd_memcpy( info->shred_dest_temp + idx, cur_dest + idx + 1UL, sizeof(fd_shred_dest_weighted_t)*(cur_cnt - idx - 1UL) );
     453           0 :   refresh_sdest( info, info->shred_dest_temp, cur_cnt-1UL, fd_shred_dest_cnt_staked( ei->sdest ), ei );
     454           0 : }
     455             : 
     456             : void
     457             : fd_stake_ci_dest_update( fd_stake_ci_t *       info,
     458             :                          fd_pubkey_t const *   pubkey,
     459             :                          uint                  ip4,
     460           0 :                          ushort                port ) {
     461           0 :   ci_dest_update_impl( info, pubkey, ip4, port, info->epoch_info+0UL );
     462           0 :   ci_dest_update_impl( info, pubkey, ip4, port, info->epoch_info+1UL );
     463           0 : }
     464             : 
     465             : void
     466             : fd_stake_ci_dest_remove( fd_stake_ci_t * info,
     467           0 :                          fd_pubkey_t const * pubkey ) {
     468           0 :   ci_dest_remove_impl( info, pubkey, info->epoch_info+0UL );
     469           0 :   ci_dest_remove_impl( info, pubkey, info->epoch_info+1UL );
     470             : 
     471           0 : }
     472             : 
     473             : 
     474             : fd_shred_dest_t *
     475             : fd_stake_ci_get_sdest_for_slot( fd_stake_ci_t const * info,
     476           0 :                                 ulong                 slot ) {
     477           0 :   ulong idx = fd_stake_ci_get_idx_for_slot( info, slot );
     478           0 :   return idx!=ULONG_MAX ? info->epoch_info[ idx ].sdest : NULL;
     479           0 : }
     480             : 
     481             : fd_epoch_leaders_t *
     482             : fd_stake_ci_get_lsched_for_slot( fd_stake_ci_t const * info,
     483           0 :                                  ulong                 slot ) {
     484           0 :   ulong idx = fd_stake_ci_get_idx_for_slot( info, slot );
     485           0 :   return idx!=ULONG_MAX ? info->epoch_info[ idx ].lsched : NULL;
     486           0 : }

Generated by: LCOV version 1.14