LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_recent_hashes.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 36 58 62.1 %
Date: 2025-11-15 04:42:28 Functions: 4 5 80.0 %

          Line data    Source code
       1             : #include "fd_sysvar_recent_hashes.h"
       2             : #include "../fd_acc_mgr.h"
       3             : #include "fd_sysvar.h"
       4             : #include "../fd_system_ids.h"
       5             : 
       6             : /* Skips fd_types encoding preflight checks and directly serializes the
       7             :    blockhash queue into a buffer representing account data for the
       8             :    recent blockhashes sysvar. */
       9             : 
      10             : static void
      11             : encode_rbh_from_blockhash_queue( fd_bank_t * bank,
      12         453 :                                  uchar       out_mem[ static FD_SYSVAR_RECENT_HASHES_BINCODE_SZ ] ) {
      13         453 :   fd_blockhashes_t const * bhq = fd_bank_block_hash_queue_query( bank );
      14             : 
      15         453 :   ulong queue_sz = fd_blockhash_deq_cnt( bhq->d.deque );
      16         453 :   ulong out_max  = fd_ulong_min( queue_sz, FD_SYSVAR_RECENT_HASHES_CAP );
      17             : 
      18         453 :   uchar * enc = out_mem;
      19         453 :   fd_memcpy( enc, &out_max, sizeof(ulong) );
      20             : 
      21         453 :   enc += sizeof(ulong);
      22             : 
      23             :   /* Iterate over blockhash queue and encode the recent blockhashes.
      24             :      We can do direct memcpying and avoid redundant checks from fd_types
      25             :      encoders since the enc buffer is already sized out to the
      26             :      worst-case bound. */
      27         453 :   ulong out_idx = 0UL;
      28         453 :   for( fd_blockhash_deq_iter_t iter = fd_blockhash_deq_iter_init_rev( bhq->d.deque );
      29       34428 :        out_idx<FD_SYSVAR_RECENT_HASHES_CAP &&
      30       34428 :           !fd_blockhash_deq_iter_done_rev( bhq->d.deque, iter );
      31       33975 :        out_idx++,   iter = fd_blockhash_deq_iter_prev( bhq->d.deque, iter ) ) {
      32       33975 :     fd_blockhash_info_t const * n = fd_blockhash_deq_iter_ele_const( bhq->d.deque, iter );
      33       33975 :     fd_memcpy( enc, n->hash.uc, 32 );
      34       33975 :     FD_STORE( ulong, enc+32, n->fee_calculator.lamports_per_signature );
      35       33975 :     enc += 40;
      36       33975 :   }
      37         453 : }
      38             : 
      39             : void
      40             : fd_sysvar_recent_hashes_init( fd_bank_t *               bank,
      41             :                               fd_accdb_user_t *         accdb,
      42             :                               fd_funk_txn_xid_t const * xid,
      43           3 :                               fd_capture_ctx_t *        capture_ctx ) {
      44           3 :   uchar enc[ FD_SYSVAR_RECENT_HASHES_BINCODE_SZ ] = {0};
      45           3 :   encode_rbh_from_blockhash_queue( bank, enc );
      46           3 :   fd_sysvar_account_update( bank, accdb, xid, capture_ctx, &fd_sysvar_recent_block_hashes_id, enc, FD_SYSVAR_RECENT_HASHES_BINCODE_SZ );
      47           3 : }
      48             : 
      49             : // https://github.com/anza-xyz/agave/blob/e8750ba574d9ac7b72e944bc1227dc7372e3a490/accounts-db/src/blockhash_queue.rs#L113
      50             : static void
      51             : register_blockhash( fd_bank_t *       bank,
      52         450 :                     fd_hash_t const * hash ) {
      53         450 :   fd_blockhashes_t * bhq = fd_bank_block_hash_queue_modify( bank );
      54         450 :   fd_blockhash_info_t * bh = fd_blockhashes_push_new( bhq, hash );
      55         450 :   bh->fee_calculator = (fd_fee_calculator_t){
      56         450 :     .lamports_per_signature = fd_bank_rbh_lamports_per_sig_get( bank )
      57         450 :   };
      58         450 : }
      59             : 
      60             : /* This implementation is more consistent with Agave's bank implementation for updating the block hashes sysvar:
      61             :    1. Update the block hash queue with the latest poh
      62             :    2. Take the first 150 blockhashes from the queue (or fewer if there are)
      63             :    3. Manually serialize the recent blockhashes
      64             :    4. Set the sysvar account with the new data */
      65             : void
      66             : fd_sysvar_recent_hashes_update( fd_bank_t *               bank,
      67             :                                 fd_accdb_user_t *         accdb,
      68             :                                 fd_funk_txn_xid_t const * xid,
      69         450 :                                 fd_capture_ctx_t *        capture_ctx ) {
      70         450 :   register_blockhash( bank, fd_bank_poh_query( bank ) );
      71             : 
      72         450 :   uchar enc[ FD_SYSVAR_RECENT_HASHES_BINCODE_SZ ] = {0};
      73         450 :   encode_rbh_from_blockhash_queue( bank, enc );
      74         450 :   fd_sysvar_account_update( bank, accdb, xid, capture_ctx, &fd_sysvar_recent_block_hashes_id, enc, sizeof(enc) );
      75         450 : }
      76             : 
      77             : fd_recent_block_hashes_t *
      78             : fd_sysvar_recent_hashes_read( fd_funk_t *               funk,
      79             :                               fd_funk_txn_xid_t const * xid,
      80           0 :                               uchar                     rbh_mem[ static FD_SYSVAR_RECENT_HASHES_FOOTPRINT ] ) {
      81           0 :   fd_txn_account_t acc[1];
      82           0 :   int err = fd_txn_account_init_from_funk_readonly( acc, &fd_sysvar_recent_block_hashes_id, funk, xid );
      83           0 :   if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) )
      84           0 :     return NULL;
      85             : 
      86           0 :   fd_bincode_decode_ctx_t ctx = {
      87           0 :     .data    = fd_txn_account_get_data( acc ),
      88           0 :     .dataend = fd_txn_account_get_data( acc ) + fd_txn_account_get_data_len( acc ),
      89           0 :   };
      90             : 
      91             :   /* This check is needed as a quirk of the fuzzer. If a sysvar account
      92             :      exists in the accounts database, but doesn't have any lamports,
      93             :      this means that the account does not exist. This wouldn't happen
      94             :      in a real execution environment. */
      95           0 :   if( FD_UNLIKELY( fd_txn_account_get_lamports( acc )==0UL ) ) {
      96           0 :     return NULL;
      97           0 :   }
      98             : 
      99           0 :   ulong total_sz = 0;
     100           0 :   err = fd_recent_block_hashes_decode_footprint( &ctx, &total_sz );
     101           0 :   if( FD_UNLIKELY( err ) ) {
     102           0 :     return NULL;
     103           0 :   }
     104             : 
     105             :   /* This would never happen in a real cluster, this is a workaround
     106             :      for fuzz-generated cases where sysvar accounts are not funded. */
     107           0 :   if( FD_UNLIKELY( fd_txn_account_get_lamports( acc ) == 0 ) ) {
     108           0 :     return NULL;
     109           0 :   }
     110             : 
     111           0 :   return fd_recent_block_hashes_decode( rbh_mem, &ctx );
     112           0 : }

Generated by: LCOV version 1.14