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 116 81.0 %
Date: 2026-02-13 06:06:24 Functions: 12 14 85.7 %

          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             : fd_multi_epoch_leaders_epoch_msg_init( fd_multi_epoch_leaders_t   * mleaders,
     115           0 :                                        fd_epoch_info_msg_t const  * msg ) {
     116           0 :   if( FD_UNLIKELY( msg->staked_cnt > MAX_STAKED_LEADERS ) )
     117           0 :     FD_LOG_ERR(( "Multi-epoch leaders received a malformed update with %lu stakes in it,"
     118           0 :                  " but the maximum allowed is %lu", msg->staked_cnt, MAX_STAKED_LEADERS ));
     119             : 
     120           0 :   mleaders->scratch->epoch          = msg->epoch;
     121           0 :   mleaders->scratch->start_slot     = msg->start_slot;
     122           0 :   mleaders->scratch->slot_cnt       = msg->slot_cnt;
     123           0 :   mleaders->scratch->staked_cnt     = msg->staked_cnt;
     124           0 :   mleaders->scratch->excluded_stake = msg->excluded_stake;
     125           0 :   mleaders->scratch->vote_keyed_lsched = msg->vote_keyed_lsched;
     126             : 
     127           0 :   fd_memcpy( mleaders->vote_stake_weight, msg->weights, msg->staked_cnt*sizeof(fd_vote_stake_weight_t) );
     128           0 : }
     129             : 
     130             : void
     131          75 : fd_multi_epoch_leaders_stake_msg_fini( fd_multi_epoch_leaders_t * mleaders ) {
     132          75 :   const ulong epoch          = mleaders->scratch->epoch;
     133          75 :   const ulong slot0          = mleaders->scratch->start_slot;
     134          75 :   const ulong slot_cnt       = mleaders->scratch->slot_cnt;
     135          75 :   const ulong pub_cnt        = mleaders->scratch->staked_cnt;
     136          75 :   const ulong excluded_stake = mleaders->scratch->excluded_stake;
     137          75 :   const ulong vote_keyed_lsched = mleaders->scratch->vote_keyed_lsched;
     138          75 :   const ulong epoch_idx      = epoch % MULTI_EPOCH_LEADERS_EPOCH_CNT;
     139             : 
     140          75 :   fd_vote_stake_weight_t * stakes = mleaders->vote_stake_weight;
     141             : 
     142             :   /* Clear old data */
     143          75 :   fd_epoch_leaders_delete( fd_epoch_leaders_leave( mleaders->lsched[epoch_idx] ) );
     144             : 
     145             :   /* Populate new lsched */
     146          75 :   uchar *  lsched_mem        = mleaders->_lsched[epoch_idx];
     147          75 :   mleaders->lsched[epoch_idx] = fd_epoch_leaders_join( fd_epoch_leaders_new(
     148          75 :                                     lsched_mem, epoch, slot0, slot_cnt,
     149          75 :                                     pub_cnt, stakes, excluded_stake, vote_keyed_lsched ) );
     150          75 :   mleaders->init_done[epoch_idx] = 1;
     151          75 : }
     152             : 
     153             : void
     154           0 : fd_multi_epoch_leaders_epoch_msg_fini( fd_multi_epoch_leaders_t * mleaders ) {
     155           0 :   fd_multi_epoch_leaders_stake_msg_fini( mleaders );
     156           0 : }
     157             : 
     158             : fd_pubkey_t const *
     159             : fd_multi_epoch_leaders_get_leader_for_slot( fd_multi_epoch_leaders_t const * mleaders,
     160       69012 :                                             ulong                            slot ) {
     161       69012 :   const ulong epoch_idx = fd_multi_epoch_leaders_get_epoch_idx( mleaders, slot );
     162       69012 :   if( FD_UNLIKELY( epoch_idx==ULONG_MAX ) ) return NULL;
     163       69012 :   return fd_epoch_leaders_get( mleaders->lsched[epoch_idx], slot );
     164       69012 : }
     165             : 
     166             : fd_multi_epoch_leaders_lsched_sorted_t
     167          15 : fd_multi_epoch_leaders_get_sorted_lscheds( fd_multi_epoch_leaders_t const * mleaders ) {
     168          15 :   fd_multi_epoch_leaders_lsched_sorted_t ret = { .lscheds = { NULL, NULL } };
     169          15 :   fd_epoch_leaders_t * even_option = fd_ptr_if( mleaders->init_done[0], mleaders->lsched[0], NULL );
     170          15 :   fd_epoch_leaders_t * odd_option  = fd_ptr_if( mleaders->init_done[1], mleaders->lsched[1], NULL );
     171             : 
     172             :   /* Sort by epoch if both non-null, null comes first */
     173          15 :   if( even_option && odd_option ) {
     174           9 :     ret.lscheds[0] = fd_ptr_if( even_option->epoch < odd_option->epoch, even_option, odd_option );
     175           9 :     ret.lscheds[1] = fd_ptr_if( even_option->epoch < odd_option->epoch, odd_option, even_option );
     176           9 :   } else {
     177             :     /* if one non-null, this will pick it up. Else, both null and this no-ops */
     178           6 :     ret.lscheds[0] = fd_ptr_if( !!even_option, even_option, odd_option );
     179           6 :   }
     180             : 
     181          15 :   return ret;
     182          15 : }

Generated by: LCOV version 1.14