LCOV - code coverage report
Current view: top level - choreo/tower - fd_tower_serdes.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 192 214 89.7 %
Date: 2026-10-22 04:28:21 Functions: 9 9 100.0 %

          Line data    Source code
       1             : #include "fd_tower_serdes.h"
       2             : #include "fd_tower.h"
       3             : 
       4        2268 : #define DE( T, name ) do {                                \
       5        2268 :     if( FD_UNLIKELY( buf_sz<sizeof(T) ) ) return -1;      \
       6        2268 :     serde->name = FD_LOAD( T, buf );                      \
       7        1791 :     buf    += sizeof(T);                                  \
       8        1791 :     buf_sz -= sizeof(T);                                  \
       9        1791 : } while(0)
      10             : 
      11         900 : #define SER( T, name ) do {                               \
      12         900 :     if( FD_UNLIKELY( off+sizeof(T)>buf_max ) ) return -1; \
      13         900 :     FD_STORE( T, buf+off, serde->name );                  \
      14         900 :     off += sizeof(T);                                     \
      15         900 : } while(0)
      16             : 
      17         111 : #define SER_SHORT_U16( name ) do {                        \
      18         111 :     uchar tmp[3];                                         \
      19         111 :     ulong n = ser_short_u16( tmp, serde->name );          \
      20         111 :     if( FD_UNLIKELY( off+n>buf_max ) ) return -1;         \
      21         111 :     fd_memcpy( buf+off, tmp, n );                         \
      22         111 :     off += n;                                             \
      23         111 : } while(0)
      24             : 
      25         402 : #define SER_VAR_INT( name ) do {                          \
      26         402 :     uchar tmp[10];                                        \
      27         402 :     ulong n = ser_var_int( tmp, serde->name );            \
      28         402 :     if( FD_UNLIKELY( off+n>buf_max ) ) return -1;         \
      29         402 :     fd_memcpy( buf+off, tmp, n );                         \
      30         402 :     off += n;                                             \
      31         402 : } while(0)
      32             : 
      33             : static int
      34         516 : de_short_u16( ushort * dst, uchar const ** src, ulong * src_sz ) {
      35         516 :   uchar const * s = *src;
      36         516 :   if( FD_UNLIKELY( *src_sz<1 ) ) return -1;
      37         507 :   if( FD_LIKELY( !( 0x80U & s[0] ) ) ) {
      38         501 :     *dst = (ushort)s[0];
      39         501 :     *src += 1;
      40         501 :     *src_sz -= 1;
      41         501 :     return 0;
      42         501 :   }
      43           6 :   if( FD_UNLIKELY( *src_sz<2 ) ) return -1;
      44           3 :   if( FD_LIKELY( !( 0x80U & s[1] ) ) ) {
      45           0 :     if( FD_UNLIKELY( !s[1] ) ) return -1; /* non-canonical: value fits in 1 byte */
      46           0 :     *dst = (ushort)( (ulong)(s[0]&0x7FUL) + (((ulong)s[1])<<7) );
      47           0 :     *src += 2;
      48           0 :     *src_sz -= 2;
      49           0 :     return 0;
      50           0 :   }
      51           3 :   if( FD_UNLIKELY( *src_sz<3    ) ) return -1;
      52           3 :   if( FD_UNLIKELY( 0x80U & s[2] ) ) return -1; /* 3rd byte is final; continuation bit is invalid */
      53           3 :   if( FD_UNLIKELY( !s[2]        ) ) return -1; /* non-canonical: value fits in 2 bytes */
      54           3 :   ulong val = (ulong)(s[0]&0x7FUL) + (((ulong)(s[1]&0x7FUL))<<7) + (((ulong)s[2])<<14);
      55           3 :   if( FD_UNLIKELY( val>USHORT_MAX ) ) return -1;
      56           3 :   *dst = (ushort)val;
      57           3 :   *src += 3;
      58           3 :   *src_sz -= 3;
      59           3 :   return 0;
      60           3 : }
      61             : 
      62             : static int
      63         531 : de_var_int( ulong * dst, uchar const ** src, ulong * src_sz ) {
      64         531 :   *dst = 0;
      65         531 :   ulong bit = 0;
      66         594 :   while( FD_LIKELY( bit < 64 ) ) {
      67         591 :     if( FD_UNLIKELY( !*src_sz ) ) return -1;
      68         582 :     uchar byte = **src;
      69         582 :     (*src)++; (*src_sz)--;
      70         582 :     *dst |= (ulong)(byte & 0x7FUL) << bit;
      71         582 :     if( FD_LIKELY( (byte & 0x80U) == 0U ) ) {
      72         519 :       if( FD_UNLIKELY( (*dst>>bit) != byte                ) ) return -1;
      73         519 :       if( FD_UNLIKELY( byte==0U && (bit!=0U || *dst!=0UL) ) ) return -1;
      74         516 :       return 0;
      75         519 :     }
      76          63 :     bit += 7;
      77          63 :   }
      78           3 :   return -1;
      79         531 : }
      80             : 
      81             : static ulong
      82         111 : ser_short_u16( uchar * dst, ushort val ) {
      83         111 :   if     ( FD_LIKELY( val < 0x80U ) ) {
      84         111 :     dst[0] = (uchar)val;
      85         111 :     return 1;
      86         111 :   }
      87           0 :   else if( FD_LIKELY( val < 0x4000U ) ) {
      88           0 :     dst[0] = (uchar)((val & 0x7FUL) | 0x80U);
      89           0 :     dst[1] = (uchar)(val >> 7);
      90           0 :     return 2;
      91           0 :   }
      92           0 :   else {
      93           0 :     dst[0] = (uchar)((val & 0x7FUL) | 0x80U);
      94           0 :     dst[1] = (uchar)(((val >> 7) & 0x7FUL) | 0x80U);
      95           0 :     dst[2] = (uchar)(val >> 14);
      96           0 :     return 3;
      97           0 :   }
      98         111 : }
      99             : 
     100             : static ulong
     101         402 : ser_var_int( uchar * dst, ulong val ) {
     102         402 :   ulong off = 0;
     103         429 :   while( FD_LIKELY( val >= 0x80UL ) ) {
     104          27 :     dst[off] = (uchar)((val & 0x7FUL) | 0x80U);
     105          27 :     val >>= 7;
     106          27 :     off  += 1;
     107          27 :   }
     108         402 :   dst[off] = (uchar)val;
     109         402 :   return off + 1;
     110         402 : }
     111             : 
     112             : int
     113             : fd_compact_tower_sync_de( fd_compact_tower_sync_serde_t * serde,
     114             :                           uchar const *                   buf,
     115         570 :                           ulong                           buf_sz ) {
     116         570 :   DE( ulong, root );
     117         516 :   if( FD_UNLIKELY( de_short_u16( &serde->lockouts_cnt, &buf, &buf_sz ) ) ) return -1;
     118         504 :   if( FD_UNLIKELY( serde->lockouts_cnt > FD_TOWER_VOTE_MAX ) ) return -1;
     119        1011 :   for( ulong i = 0; i < serde->lockouts_cnt; i++ ) {
     120         531 :     if( FD_UNLIKELY( de_var_int( &serde->lockouts[i].offset, &buf, &buf_sz ) ) ) return -1;
     121         516 :     DE( uchar, lockouts[i].confirmation_count );
     122         516 :   }
     123         480 :   DE( fd_hash_t, hash             );
     124         288 :   DE( uchar,     timestamp_option );
     125         282 :   if( FD_UNLIKELY( serde->timestamp_option!=1 && serde->timestamp_option!=0 ) ) return -1;
     126         276 :   if( FD_LIKELY( serde->timestamp_option ) ) {
     127         165 :     DE( long, timestamp );
     128         165 :   }
     129         249 :   DE( fd_hash_t, block_id );
     130          54 :   return 0;
     131         249 : }
     132             : 
     133             : int
     134             : fd_compact_tower_sync_ser( fd_compact_tower_sync_serde_t const * serde,
     135             :                            uchar *                               buf,
     136             :                            ulong                                 buf_max,
     137         111 :                            ulong *                               buf_sz ) {
     138         111 :   if( FD_UNLIKELY( serde->lockouts_cnt > FD_TOWER_VOTE_MAX ) ) return -1;
     139         111 :   ulong off = 0;
     140         111 :   SER( ulong, root );
     141         111 :   SER_SHORT_U16( lockouts_cnt );
     142         513 :   for( ulong i = 0; i < serde->lockouts_cnt; i++ ) {
     143         402 :     SER_VAR_INT( lockouts[i].offset );
     144         402 :     SER( uchar, lockouts[i].confirmation_count );
     145         402 :   }
     146         111 :   SER( fd_hash_t, hash             );
     147         111 :   SER( uchar,     timestamp_option );
     148         111 :   if( FD_LIKELY( serde->timestamp_option ) ) {
     149          54 :     SER( long, timestamp );
     150          54 :   }
     151         111 :   SER( fd_hash_t, block_id );
     152         111 :   if( FD_LIKELY( buf_sz ) ) *buf_sz = off;
     153         111 :   return 0;
     154         111 : }
     155             : 
     156             : fd_vote_acc_desc_t *
     157             : fd_vote_acc_desc( fd_vote_acc_desc_t * desc,
     158             :                   uchar const *        data,
     159       33174 :                   ulong                data_sz ) {
     160       33174 :   if( FD_UNLIKELY( data_sz < sizeof(uint) ) ) return NULL;
     161       33174 :   uint kind = FD_LOAD( uint, data );
     162       33174 :   fd_vote_acc_t const * voter = fd_type_pun_const( data );
     163       33174 :   void const *  vote_cnt_p;
     164       33174 :   ulong         vote_stride;
     165       33174 :   switch( kind ) {
     166           6 :   case FD_VOTE_ACC_V2:
     167           6 :     vote_cnt_p  = &voter->v2.votes_cnt;
     168           6 :     vote_stride = sizeof(fd_vote_acc_vote_v2_t);
     169           6 :     break;
     170        2145 :   case FD_VOTE_ACC_V3:
     171        2145 :     vote_cnt_p  = &voter->v3.votes_cnt;
     172        2145 :     vote_stride = sizeof(fd_vote_acc_vote_t);
     173        2145 :     break;
     174       31023 :   case FD_VOTE_ACC_V4:
     175       31023 :     if( FD_UNLIKELY( (ulong)&voter->v4.has_bls_pubkey_compressed >= (ulong)data+data_sz ) ) return NULL;
     176       31023 :     vote_cnt_p  = voter->v4.bls_pubkey_compressed + (!!voter->v4.has_bls_pubkey_compressed * sizeof(voter->v4.bls_pubkey_compressed));
     177       31023 :     vote_stride = sizeof(fd_vote_acc_vote_t);
     178       31023 :     break;
     179           0 :   default:
     180           0 :     return NULL;
     181       33174 :   }
     182       33174 :   if( FD_UNLIKELY( (ulong)vote_cnt_p+sizeof(ulong) > (ulong)data+data_sz ) ) return NULL;
     183       33174 :   ulong vote_cnt = FD_LOAD( ulong, vote_cnt_p );
     184             :   /* FIXME silent truncation is questionable behavior */
     185       33174 :   vote_cnt = fd_ulong_min( vote_cnt, FD_TOWER_VOTE_MAX );
     186       33174 :   ulong vote_hi_p = (ulong)vote_cnt_p + sizeof(ulong) + vote_cnt*vote_stride;
     187       33174 :   if( FD_UNLIKELY( vote_hi_p > (ulong)data+data_sz ) ) return NULL;
     188             :   /* Option<ulong> "root_slot" follows vote array */
     189       33174 :   ulong root_slot = ULONG_MAX;
     190       33174 :   if( FD_UNLIKELY( vote_hi_p+1UL > (ulong)data+data_sz ) ) return NULL;
     191       33174 :   int root_opt = FD_LOAD( uchar, (void const *)vote_hi_p );
     192       33174 :   if( root_opt==1 ) {
     193       32685 :     if( FD_UNLIKELY( vote_hi_p+9UL > (ulong)data+data_sz ) ) return NULL;
     194       32685 :     root_slot = FD_LOAD( ulong, (void const *)(vote_hi_p+1UL) );
     195       32685 :   } else if( FD_UNLIKELY( root_opt!=0 ) ) {
     196           0 :     return NULL;
     197           0 :   }
     198       33174 :   *desc = (fd_vote_acc_desc_t){
     199       33174 :     .kind        = (int)kind,
     200       33174 :     .votes_off   = (ushort)( (ulong)vote_cnt_p + sizeof(ulong) - (ulong)data ),
     201       33174 :     .vote_cnt    = (uchar)vote_cnt,
     202       33174 :     .vote_stride = (uchar)vote_stride,
     203       33174 :     .root_slot   = root_slot
     204       33174 :   };
     205       33174 :   return desc;
     206       33174 : }
     207             : 
     208             : int
     209             : fd_txn_parse_simple_vote( fd_txn_t const *                txn,
     210             :                           uchar    const *                payload,
     211           3 :                           fd_compact_tower_sync_serde_t * opt_tower_sync ) {
     212           3 :   fd_txn_instr_t const * instr      = &txn->instr[ 0 ];
     213           3 :   ulong required_accts = txn->signature_cnt==1 ? 2UL : 3UL;
     214           3 :   if( FD_UNLIKELY( !fd_txn_is_simple_vote_transaction( txn, payload ) || instr->data_sz < sizeof(uint) || txn->acct_addr_cnt < required_accts ) ) return 0;
     215             : 
     216           3 :   uchar const *          instr_data = payload + instr->data_off;
     217           3 :   uint                   kind       = fd_uint_load_4_fast( instr_data );
     218             :   /* Older vote instruction kinds are deprecated / ignored */
     219           3 :   if( FD_UNLIKELY( kind == FD_VOTE_IX_KIND_TOWER_SYNC || kind == FD_VOTE_IX_KIND_TOWER_SYNC_SWITCH ) ) {
     220           3 :     fd_compact_tower_sync_serde_t compact_tower_sync_serde[ 1 ];
     221           3 :     int err = fd_compact_tower_sync_de( compact_tower_sync_serde, instr_data + sizeof(uint), instr->data_sz - sizeof(uint) );
     222           3 :     if( FD_LIKELY( !err ) ) {
     223           3 :       if( !!opt_tower_sync ) *opt_tower_sync = *compact_tower_sync_serde;
     224           3 :       return 1;
     225           3 :     }
     226           3 :   }
     227           0 :   return 0;
     228           3 : }
     229             : 
     230             : int
     231             : fd_compact_tower_sync_to_votes( fd_compact_tower_sync_serde_t const * serde,
     232             :                                 fd_tower_vote_t *                     out,
     233             :                                 ulong *                               out_cnt,
     234          57 :                                 ulong *                               out_root ) {
     235          57 :   if( FD_UNLIKELY( serde->lockouts_cnt>FD_TOWER_VOTE_MAX ) ) return -1;
     236             : 
     237          57 :   fd_tower_vote_t votes[ FD_TOWER_VOTE_MAX ];
     238          57 :   ulong slot      = fd_ulong_if( serde->root==ULONG_MAX, 0UL, serde->root );
     239          57 :   ulong prev_conf = FD_TOWER_VOTE_MAX+1UL;
     240             :   /* Only the vote below is checked, Agave pops expired votes from the
     241             :      top only, so a deeper expired vote is a normal state. */
     242         144 :   for( ulong i=0UL; i<serde->lockouts_cnt; i++ ) {
     243         114 :     ulong offset = serde->lockouts[ i ].offset;
     244         114 :     ulong conf   = serde->lockouts[ i ].confirmation_count;
     245         114 :     int repeats_slot = !offset && (i || serde->root!=ULONG_MAX);
     246         114 :     if( FD_UNLIKELY( repeats_slot || offset>ULONG_MAX-slot || !conf ||
     247         114 :                      conf>=prev_conf || conf>FD_TOWER_VOTE_MAX ) ) return -1;
     248          93 :     if( FD_UNLIKELY( i && offset>(1UL<<prev_conf) ) ) return -1;
     249          87 :     slot += offset;
     250          87 :     votes[ i ] = (fd_tower_vote_t){ .slot=slot, .conf=conf };
     251          87 :     prev_conf = conf;
     252          87 :   }
     253          30 :   fd_memcpy( out, votes, serde->lockouts_cnt*sizeof(fd_tower_vote_t) );
     254          30 :   *out_cnt  = serde->lockouts_cnt;
     255          30 :   *out_root = serde->root;
     256          30 :   return 0;
     257          57 : }
     258             : 
     259             : #undef DE
     260             : #undef SER

Generated by: LCOV version 1.14