LCOV - code coverage report
Current view: top level - flamenco/leaders - fd_multi_epoch_leaders.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 94 101 93.1 %
Date: 2025-08-07 04:53:16 Functions: 12 12 100.0 %

          Line data    Source code
       1             : #include "fd_multi_epoch_leaders.h"
       2             : 
       3             : void *
       4          24 : fd_multi_epoch_leaders_new( void * shmem ) {
       5          24 :   if( FD_UNLIKELY( !shmem ) ) {
       6           0 :     FD_LOG_WARNING(( "NULL shmem" ));
       7           0 :     return NULL;
       8           0 :   }
       9             : 
      10          24 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_multi_epoch_leaders_align() ) ) ) {
      11           0 :     FD_LOG_WARNING(( "misaligned shmem" ));
      12           0 :     return NULL;
      13           0 :   }
      14             : 
      15          24 :   fd_multi_epoch_leaders_t * leaders = (fd_multi_epoch_leaders_t *)shmem;
      16          24 :   leaders->scratch->vote_keyed_lsched = 0;
      17             : 
      18             :   /* Initialize all epochs to satisfy invariants */
      19          24 :   fd_vote_stake_weight_t dummy_stakes[ 1 ] = {{ .vote_key = {{0}}, .id_key = {{0}}, .stake = 1UL }};
      20          72 :   for( ulong i=0UL; i<MULTI_EPOCH_LEADERS_EPOCH_CNT; i++ ) {
      21          48 :     leaders->lsched[i] = fd_epoch_leaders_join( fd_epoch_leaders_new( leaders->_lsched[i], i, 0UL, 1UL, 1UL, dummy_stakes, 0UL, leaders->scratch->vote_keyed_lsched ) );
      22          48 :     FD_TEST( leaders->lsched[i] );
      23          48 :     leaders->init_done[i] = 0;
      24          48 :   }
      25             : 
      26          24 :   return shmem;
      27          24 : }
      28             : 
      29             : fd_multi_epoch_leaders_t *
      30          24 : fd_multi_epoch_leaders_join( void * shleaders ) { return shleaders; }
      31             : 
      32             : void *
      33          24 : fd_multi_epoch_leaders_leave( fd_multi_epoch_leaders_t * mleaders ) { return mleaders; }
      34             : 
      35             : void *
      36          24 : fd_multi_epoch_leaders_delete( void * shleaders ) { return shleaders; }
      37             : 
      38             : fd_epoch_leaders_t const *
      39             : fd_multi_epoch_leaders_get_lsched_for_epoch( fd_multi_epoch_leaders_t const * mleaders,
      40           6 :                                              ulong                            epoch ) {
      41           6 :   fd_epoch_leaders_t const * even_lsched = fd_ptr_if( mleaders->init_done[0] & !!(mleaders->lsched[0]->epoch==epoch), mleaders->lsched[0], NULL );
      42           6 :   fd_epoch_leaders_t const * odd_lsched  = fd_ptr_if( mleaders->init_done[1] & !!(mleaders->lsched[1]->epoch==epoch), mleaders->lsched[1], NULL );
      43           6 :   return fd_ptr_if( !!even_lsched, even_lsched, odd_lsched );
      44           6 : }
      45             : 
      46             : static inline ulong
      47             : fd_multi_epoch_leaders_get_epoch_idx( fd_multi_epoch_leaders_t const * mleaders,
      48       69186 :                                       ulong                            slot ) {
      49       69186 :   fd_epoch_leaders_t const * even_lsched = mleaders->lsched[0];
      50       69186 :   fd_epoch_leaders_t const * odd_lsched  = mleaders->lsched[1];
      51             : 
      52       69186 :   ulong even_match = fd_ulong_if( mleaders->init_done[0] & !!(even_lsched->slot0<=slot) & !!(slot<even_lsched->slot0+even_lsched->slot_cnt), 0UL, ULONG_MAX );
      53       69186 :   ulong odd_match  = fd_ulong_if( mleaders->init_done[1] & !!(odd_lsched->slot0<=slot) & !!(slot<odd_lsched->slot0+odd_lsched->slot_cnt), 1UL, ULONG_MAX );
      54             : 
      55       69186 :   return fd_ulong_if( even_match!=ULONG_MAX, even_match, odd_match );
      56       69186 : }
      57             : 
      58             : fd_epoch_leaders_t const *
      59             : fd_multi_epoch_leaders_get_lsched_for_slot( fd_multi_epoch_leaders_t const *  mleaders,
      60         159 :                                              ulong                            slot ) {
      61         159 :   const ulong epoch_idx = fd_multi_epoch_leaders_get_epoch_idx( mleaders, slot );
      62         159 :   if( FD_UNLIKELY( epoch_idx==ULONG_MAX ) ) return NULL;
      63         153 :   return mleaders->lsched[epoch_idx];
      64         159 : }
      65             : 
      66             : ulong
      67             : fd_multi_epoch_leaders_get_next_slot( fd_multi_epoch_leaders_t const * mleaders,
      68             :                                       ulong                            start_slot,
      69          15 :                                       fd_pubkey_t              const * leader_q ) {
      70             : 
      71             :   /* Find epoch containing start_slot */
      72          15 :   ulong epoch_idx = fd_multi_epoch_leaders_get_epoch_idx( mleaders, start_slot );
      73          15 :   if( FD_UNLIKELY( epoch_idx==ULONG_MAX ) ) return ULONG_MAX;
      74             : 
      75             :   /* Start at epoch_idx and seek next slot (across epochs)  */
      76          24 :   for( ulong i=0; i<MULTI_EPOCH_LEADERS_EPOCH_CNT; i++ ) {
      77          21 :     ulong epoch_i = (epoch_idx + i) % MULTI_EPOCH_LEADERS_EPOCH_CNT;
      78             : 
      79          21 :     fd_epoch_leaders_t const * epoch_lsched  = mleaders->lsched[ epoch_i ];
      80          21 :     ulong                      slot0         = epoch_lsched->slot0;
      81          21 :     ulong                      slot_end      = slot0 + epoch_lsched->slot_cnt;
      82             : 
      83             :     /* skip older epochs */
      84          21 :     if( FD_UNLIKELY( !mleaders->init_done[epoch_i] ) ) continue;
      85             : 
      86          21 :     ulong start_slot_it = fd_ulong_max( start_slot, slot0 );
      87        9165 :     for( ulong slot=start_slot_it; slot<slot_end; slot++ ) {
      88        9156 :       fd_pubkey_t const * leader = fd_epoch_leaders_get( epoch_lsched, slot );
      89        9156 :       if( FD_UNLIKELY( !memcmp( leader->key, leader_q->key, 32UL ) ) ) return slot;
      90        9156 :     }
      91          21 :   }
      92             : 
      93           3 :   return ULONG_MAX;
      94          15 : }
      95             : 
      96             : void
      97             : fd_multi_epoch_leaders_stake_msg_init( fd_multi_epoch_leaders_t    * mleaders,
      98          78 :                                        fd_stake_weight_msg_t const * msg ) {
      99          78 :   if( FD_UNLIKELY( msg->staked_cnt > MAX_STAKED_LEADERS ) )
     100           0 :     FD_LOG_ERR(( "Multi-epoch leaders received a malformed update with %lu stakes in it,"
     101          78 :                  " but the maximum allowed is %lu", msg->staked_cnt, MAX_STAKED_LEADERS ));
     102             : 
     103          78 :   mleaders->scratch->epoch          = msg->epoch;
     104          78 :   mleaders->scratch->start_slot     = msg->start_slot;
     105          78 :   mleaders->scratch->slot_cnt       = msg->slot_cnt;
     106          78 :   mleaders->scratch->staked_cnt     = msg->staked_cnt;
     107          78 :   mleaders->scratch->excluded_stake = msg->excluded_stake;
     108          78 :   mleaders->scratch->vote_keyed_lsched = msg->vote_keyed_lsched;
     109             : 
     110          78 :   fd_memcpy( mleaders->vote_stake_weight, msg->weights, msg->staked_cnt*sizeof(fd_vote_stake_weight_t) );
     111          78 : }
     112             : 
     113             : void
     114          75 : fd_multi_epoch_leaders_stake_msg_fini( fd_multi_epoch_leaders_t * mleaders ) {
     115          75 :   const ulong epoch          = mleaders->scratch->epoch;
     116          75 :   const ulong slot0          = mleaders->scratch->start_slot;
     117          75 :   const ulong slot_cnt       = mleaders->scratch->slot_cnt;
     118          75 :   const ulong pub_cnt        = mleaders->scratch->staked_cnt;
     119          75 :   const ulong excluded_stake = mleaders->scratch->excluded_stake;
     120          75 :   const ulong vote_keyed_lsched = mleaders->scratch->vote_keyed_lsched;
     121          75 :   const ulong epoch_idx      = epoch % MULTI_EPOCH_LEADERS_EPOCH_CNT;
     122             : 
     123          75 :   fd_vote_stake_weight_t * stakes = mleaders->vote_stake_weight;
     124             : 
     125             :   /* Clear old data */
     126          75 :   fd_epoch_leaders_delete( fd_epoch_leaders_leave( mleaders->lsched[epoch_idx] ) );
     127             : 
     128             :   /* Populate new lsched */
     129          75 :   uchar *  lsched_mem        = mleaders->_lsched[epoch_idx];
     130          75 :   mleaders->lsched[epoch_idx] = fd_epoch_leaders_join( fd_epoch_leaders_new(
     131          75 :                                     lsched_mem, epoch, slot0, slot_cnt,
     132          75 :                                     pub_cnt, stakes, excluded_stake, vote_keyed_lsched ) );
     133          75 :   mleaders->init_done[epoch_idx] = 1;
     134          75 : }
     135             : 
     136             : fd_pubkey_t const *
     137             : fd_multi_epoch_leaders_get_leader_for_slot( fd_multi_epoch_leaders_t const * mleaders,
     138       69012 :                                             ulong                            slot ) {
     139       69012 :   const ulong epoch_idx = fd_multi_epoch_leaders_get_epoch_idx( mleaders, slot );
     140       69012 :   if( FD_UNLIKELY( epoch_idx==ULONG_MAX ) ) return NULL;
     141       69012 :   return fd_epoch_leaders_get( mleaders->lsched[epoch_idx], slot );
     142       69012 : }
     143             : 
     144             : fd_multi_epoch_leaders_lsched_sorted_t
     145          15 : fd_multi_epoch_leaders_get_sorted_lscheds( fd_multi_epoch_leaders_t const * mleaders ) {
     146          15 :   fd_multi_epoch_leaders_lsched_sorted_t ret = { .lscheds = { NULL, NULL } };
     147          15 :   fd_epoch_leaders_t * even_option = fd_ptr_if( mleaders->init_done[0], mleaders->lsched[0], NULL );
     148          15 :   fd_epoch_leaders_t * odd_option  = fd_ptr_if( mleaders->init_done[1], mleaders->lsched[1], NULL );
     149             : 
     150             :   /* Sort by epoch if both non-null, null comes first */
     151          15 :   if( even_option && odd_option ) {
     152           9 :     ret.lscheds[0] = fd_ptr_if( even_option->epoch < odd_option->epoch, even_option, odd_option );
     153           9 :     ret.lscheds[1] = fd_ptr_if( even_option->epoch < odd_option->epoch, odd_option, even_option );
     154           9 :   } else {
     155             :     /* if one non-null, this will pick it up. Else, both null and this no-ops */
     156           6 :     ret.lscheds[0] = fd_ptr_if( !!even_option, even_option, odd_option );
     157           6 :   }
     158             : 
     159          15 :   return ret;
     160          15 : }

Generated by: LCOV version 1.14