LCOV - code coverage report
Current view: top level - flamenco/leaders - fd_leaders.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 32 33 97.0 %
Date: 2026-12-01 04:32:29 Functions: 13 376 3.5 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_flamenco_leaders_fd_leaders_h
       2             : #define HEADER_fd_src_flamenco_leaders_fd_leaders_h
       3             : 
       4             : /* fd_leaders provides APIs for the Solana leader schedule.
       5             : 
       6             :    Every slot is assigned a leader (identified by a "node identity"
       7             :    public key).  The sequence of leaders for all slots in an epoch is
       8             :    called the "leader schedule".  The main responsibility of the leader
       9             :    is to produce a block for the slot.
      10             : 
      11             :    The leader schedule is divided into sched_cnt rotations.  Each
      12             :    rotation spans one or more slots, such that
      13             : 
      14             :      slots_per_epoch = sched_cnt * slots_per_rotation
      15             : 
      16             :    The leader can only change between rotations.  An example leader
      17             :    schedule looks as follows (where A, B, C are node identities and each
      18             :    column is a slot, with 4 slots per rotation)
      19             : 
      20             :      A  A  A  A  B  B  B  B  B  B  B  B  C  C  C  C  A  A  A  A
      21             :      ^           ^           ^           ^           ^
      22             :      rotation    rotation    rotation    rotation    rotation
      23             : 
      24             :    The mainnet epoch duration is quite long (currently 432000 slots, for
      25             :    more information see fd_sysvar_epoch_schedule.h).  To save space, we
      26             :    store node identities in a lookup table and only store an index
      27             :    into it for each rotation. */
      28             : 
      29             : #include "../fd_flamenco_base.h"
      30             : #include "fd_leaders_base.h"
      31             : #include "../stakes/fd_stake_weight.h"
      32             : #include "../../ballet/wsample/fd_wsample.h"
      33             : 
      34         156 : #define FD_ULONG_MAX(  a, b ) (__builtin_choose_expr( __builtin_constant_p( a ) & __builtin_constant_p( b ),        \
      35         156 :                                                       ((ulong )(a))>=((ulong )(b)) ? ((ulong )(a)) : ((ulong )(b)), \
      36         156 :                                                       fd_ulong_max( (a), (b) ) ))
      37             : 
      38             : /* FD_EPOCH_LEADERS_GEO_RUN_LENGTH is the maximum number of leaders in
      39             :    a single cluster. Defined by SIMD-0675.
      40             : 
      41             :    N.b: SIMD-0675 originally had this value as 3, but this is being
      42             :    changed, likely to 7. This constant will be updated when SIMD-0675
      43             :    is merged. */
      44    16012893 : #define FD_EPOCH_LEADERS_GEO_RUN_LENGTH (7UL)
      45             : 
      46             : /* FD_EPOCH_LEADERS_GEO_SCRATCH_FOOTPRINT is the scratch space needed
      47             :    for the calculations for the geo-aware leader schedule re-ordering.
      48             : 
      49             :    For each validator (pub_cnt), we need:
      50             :    - its radius: sizeof(ulong)
      51             :    - its cluster - the member count plus up to RUN_LENGTH members:
      52             :      (1+RUN_LENGTH)*sizeof(uint)
      53             :    - its entry in the open clusters and eligible clusters lists:
      54             :      2*sizeof(uint) */
      55             : 
      56             : #define FD_EPOCH_LEADERS_GEO_SCRATCH_FOOTPRINT( pub_cnt ) \
      57             :   ((pub_cnt)*(sizeof(ulong)+(1UL+FD_EPOCH_LEADERS_GEO_RUN_LENGTH)*sizeof(uint)+2UL*sizeof(uint)))
      58             : 
      59             : /* FD_EPOCH_LEADERS_{ALIGN,FOOTPRINT} are compile-time-friendly versions
      60             :    of the fd_epoch_leaders_{align,footprint} functions. */
      61             : 
      62           0 : #define FD_EPOCH_LEADERS_ALIGN (64UL)
      63             : #define FD_EPOCH_LEADERS_FOOTPRINT( pub_cnt, slot_cnt )                                                     \
      64       12639 :   ( FD_LAYOUT_FINI( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND(                                   \
      65       12639 :     FD_LAYOUT_INIT,                                                                                         \
      66       12639 :       alignof(fd_epoch_leaders_t), sizeof(fd_epoch_leaders_t)                            ),                 \
      67       12639 :       alignof(uint),               (                                                                        \
      68       12639 :         (slot_cnt+FD_EPOCH_SLOTS_PER_ROTATION-1UL)/FD_EPOCH_SLOTS_PER_ROTATION*sizeof(uint)                 \
      69       12639 :         )                                                                                ),                 \
      70       12639 :       32UL,                        (                                                                        \
      71       12639 :         (slot_cnt+FD_EPOCH_SLOTS_PER_ROTATION-1UL)/FD_EPOCH_SLOTS_PER_ROTATION*32UL                         \
      72       12639 :         )                                                                                ),                 \
      73       12639 :       FD_EPOCH_LEADERS_ALIGN                                                             )  +               \
      74       12639 :       FD_ULONG_ALIGN_UP( FD_ULONG_MAX( FD_ULONG_MAX( 32UL*(pub_cnt),                                        \
      75       12639 :                                                      FD_WSAMPLE_FOOTPRINT( pub_cnt, 0 ) ),                  \
      76       12639 :                                        FD_EPOCH_LEADERS_GEO_SCRATCH_FOOTPRINT( pub_cnt ) ), 64UL ) )
      77             : 
      78     4447221 : #define FD_EPOCH_SLOTS_PER_ROTATION (4UL)
      79             : 
      80             : /* fd_epoch_leaders_t contains the leader schedule of a Solana epoch. */
      81             : 
      82             : struct fd_epoch_leaders {
      83             :   /* This struct contains the schedule for epoch `epoch` which spans
      84             :      slots [slot0, slot0+slot_cnt). */
      85             :   ulong epoch;
      86             :   ulong slot0;
      87             :   ulong slot_cnt;
      88             : 
      89             :   /* pub is a lookup table of node public keys, one per vote-account
      90             :      stake entry, with length pub_cnt.  Node public keys may repeat. */
      91             :   fd_pubkey_t * pub;
      92             :   ulong         pub_cnt;
      93             : 
      94             :   /* sched contains the leader schedule in the form of indexes into
      95             :      the pub array.  For sched_cnt, refer to below. */
      96             :   uint *        sched;
      97             :   ulong         sched_cnt;
      98             : 
      99             :   /* vote_addr is the leader vote account address per rotation
     100             :      (sched_cnt entries).  Needed by SIMD-0232 fee collection: multiple
     101             :      vote accounts can share one node identity, so the vote address
     102             :      cannot be recovered from pub. */
     103             :   fd_pubkey_t * vote_addr;
     104             : };
     105             : typedef struct fd_epoch_leaders fd_epoch_leaders_t;
     106             : 
     107             : FD_PROTOTYPES_BEGIN
     108             : 
     109             : /* fd_epoch_leaders_{align,footprint} describe the required footprint
     110             :    and alignment of the leader schedule object.  pub_cnt is the number
     111             :    of vote-account stake entries.  slot_cnt is the number of slots in
     112             :    the epoch. */
     113             : 
     114             : FD_FN_CONST ulong
     115             : fd_epoch_leaders_align( void );
     116             : 
     117             : FD_FN_CONST ulong
     118             : fd_epoch_leaders_footprint( ulong pub_cnt,
     119             :                             ulong slot_cnt );
     120             : 
     121             : /* fd_epoch_leaders_new formats a memory region for use as a leader
     122             :    schedule object.  shmem points to the first byte of a memory region
     123             :    with matching alignment and footprint requirements.  The leader
     124             :    schedule object will contain the leader schedule for epoch `epoch`
     125             :    which spans slots [slot0, slot0+slot_cnt).  `slot0` must be the first
     126             :    slot in the epoch, but slot_cnt can be less than the length of the
     127             :    epoch to derive only the first portion of the leader schedule.
     128             :    slot_cnt must be at most MAX_SLOTS_PER_EPOCH (FD_LOG_ERR otherwise).
     129             :    pub_cnt is the number of entries in stakes.
     130             :    `stakes` points to the first entry of pub_cnt entries of stake
     131             :    weights sorted by tuple (stake, pubkey) in descending order.
     132             : 
     133             :    If geo_aware is 1, the schedule is reordered so that nearby
     134             :    leaders tend to follow each other as in SIMD-0675.  Every leader
     135             :    still gets the same number of rotations, the schedule just goes
     136             :    through an extra re-ordering step at the end.  Every entry in stakes
     137             :    needs a valid location, otherwise this will FD_LOG_ERR.
     138             : 
     139             :    Deriving only the first portion of the leader schedule is not
     140             :    supported with the geo-aware leader schedule (geo_aware non-zero).
     141             : 
     142             :    Does NOT retain a read interest in stakes upon return.
     143             :    The caller is not joined to the object on return. */
     144             : 
     145             : void *
     146             : fd_epoch_leaders_new( void  *                  shmem,
     147             :                       ulong                    epoch,
     148             :                       ulong                    slot0,
     149             :                       ulong                    slot_cnt,
     150             :                       ulong                    pub_cnt,
     151             :                       fd_vote_stake_weight_t * stakes, /* indexed [0, pub_cnt) */
     152             :                       int                      geo_aware );
     153             : 
     154             : /* fd_epoch_leaders_use_geo_aware returns 1 if the leader schedule for
     155             :    schedule_epoch should be geo-aware, and 0 otherwise. */
     156             : int
     157             : fd_epoch_leaders_use_geo_aware( fd_features_t const *       features,
     158             :                                 fd_epoch_schedule_t const * epoch_schedule,
     159             :                                 ulong                       schedule_epoch );
     160             : 
     161             : /* fd_epoch_leaders_join joins the caller to the leader schedule object.
     162             :    fd_epoch_leaders_leave undoes an existing join. */
     163             : 
     164             : fd_epoch_leaders_t *
     165             : fd_epoch_leaders_join( void * shleaders );
     166             : 
     167             : void *
     168             : fd_epoch_leaders_leave( fd_epoch_leaders_t * leaders );
     169             : 
     170             : /* fd_epoch_leaders_delete unformats a memory region and returns owner-
     171             :    ship back to the caller. */
     172             : 
     173             : void *
     174             : fd_epoch_leaders_delete( void * shleaders );
     175             : 
     176             : /* fd_epoch_leaders_get returns a pointer to the selected public key
     177             :    given a slot.  Returns NULL if slot is not in [slot0, slot0+slot_cnt)
     178             :    given the values supplied in fd_epoch_leaders_new. */
     179             : 
     180             : FD_FN_PURE static inline fd_pubkey_t const *
     181             : fd_epoch_leaders_get( fd_epoch_leaders_t const * leaders,
     182     3112722 :                       ulong                      slot ) {
     183     3112722 :   if( FD_UNLIKELY( leaders==NULL ) ) return NULL;
     184     3112722 :   ulong slot_delta = slot - leaders->slot0;
     185     3112722 :   if( FD_UNLIKELY( slot      < leaders->slot0    ) ) return NULL;
     186     3112494 :   if( FD_UNLIKELY( slot_delta>=leaders->slot_cnt ) ) return NULL;
     187     3111786 :   return (fd_pubkey_t const *)( leaders->pub + leaders->sched[ slot_delta/FD_EPOCH_SLOTS_PER_ROTATION ] );
     188     3112494 : }
     189             : 
     190             : /* fd_epoch_leaders_get_vote returns a pointer to the vote account
     191             :    address of the leader for the given slot.  Same lookup semantics as
     192             :    fd_epoch_leaders_get. */
     193             : 
     194             : FD_FN_PURE static inline fd_pubkey_t const *
     195             : fd_epoch_leaders_get_vote( fd_epoch_leaders_t const * leaders,
     196     1308237 :                            ulong                      slot ) {
     197     1308237 :   if( FD_UNLIKELY( leaders==NULL ) ) return NULL;
     198     1308237 :   ulong slot_delta = slot - leaders->slot0;
     199     1308237 :   if( FD_UNLIKELY( slot      < leaders->slot0    ) ) return NULL;
     200     1308237 :   if( FD_UNLIKELY( slot_delta>=leaders->slot_cnt ) ) return NULL;
     201     1308234 :   return (fd_pubkey_t const *)( leaders->vote_addr + slot_delta/FD_EPOCH_SLOTS_PER_ROTATION );
     202     1308237 : }
     203             : 
     204             : FD_PROTOTYPES_END
     205             : 
     206             : #endif /* HEADER_fd_src_flamenco_leaders_fd_leaders_h */

Generated by: LCOV version 1.14