LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_hashes.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 49 60 81.7 %
Date: 2026-06-18 08:59:34 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "fd_hashes.h"
       2             : #include "fd_bank.h"
       3             : #include "../capture/fd_capture_ctx.h"
       4             : 
       5             : void
       6             : fd_hashes_account_lthash_simple( uchar const         pubkey[ static FD_HASH_FOOTPRINT ],
       7             :                                  uchar const         owner[ static FD_HASH_FOOTPRINT ],
       8             :                                  ulong               lamports,
       9             :                                  int                 executable,
      10             :                                  uchar const *       data,
      11             :                                  ulong               data_len,
      12       21576 :                                  fd_lthash_value_t * lthash_out ) {
      13       21576 :   fd_lthash_zero( lthash_out );
      14       21576 :   if( FD_UNLIKELY( !lamports ) ) return;
      15             : 
      16       21264 :   uchar executable_flag = !!executable;
      17             : 
      18       21264 :   fd_blake3_t b3[1];
      19       21264 :   fd_blake3_init( b3 );
      20       21264 :   fd_blake3_append( b3, &lamports, sizeof( ulong ) );
      21       21264 :   fd_blake3_append( b3, data, data_len );
      22       21264 :   fd_blake3_append( b3, &executable_flag, sizeof( uchar ) );
      23       21264 :   fd_blake3_append( b3, owner, FD_HASH_FOOTPRINT );
      24       21264 :   fd_blake3_append( b3, pubkey, FD_HASH_FOOTPRINT );
      25       21264 :   fd_blake3_fini_2048( b3, lthash_out->bytes );
      26       21264 : }
      27             : 
      28             : void
      29             : fd_hashes_hash_bank( fd_lthash_value_t const * lthash,
      30             :                      fd_hash_t const *         prev_bank_hash,
      31             :                      fd_hash_t const *         last_blockhash,
      32             :                      ulong                     signature_count,
      33          84 :                      fd_hash_t *               hash_out ) {
      34             : 
      35             :   /* The bank hash for a slot is a sha256 of two sub-hashes:
      36             :      sha256(
      37             :         sha256( previous bank hash, signature count, last PoH blockhash ),
      38             :         lthash of the accounts modified in this slot
      39             :      )
      40             :   */
      41          84 :   fd_sha256_t sha;
      42          84 :   fd_sha256_init( &sha );
      43          84 :   fd_sha256_append( &sha, prev_bank_hash, sizeof( fd_hash_t ) );
      44          84 :   fd_sha256_append( &sha, (uchar const *) &signature_count, sizeof( ulong ) );
      45          84 :   fd_sha256_append( &sha, (uchar const *) last_blockhash, sizeof( fd_hash_t ) );
      46          84 :   fd_sha256_fini( &sha, hash_out->hash );
      47             : 
      48          84 :   fd_sha256_init( &sha );
      49          84 :   fd_sha256_append( &sha, (uchar const *) hash_out->hash, sizeof(fd_hash_t) );
      50          84 :   fd_sha256_append( &sha, (uchar const *) lthash->bytes,  sizeof(fd_lthash_value_t) );
      51          84 :   fd_sha256_fini( &sha, hash_out->hash );
      52          84 : }
      53             : 
      54             : void
      55             : fd_hashes_update_simple( fd_lthash_value_t *       lthash_post, /* out */
      56             :                          fd_lthash_value_t const * lthash_prev, /* in */
      57             :                          uchar const               pubkey[ static FD_HASH_FOOTPRINT ],
      58             :                          uchar const               owner[ static FD_HASH_FOOTPRINT ],
      59             :                          ulong                     lamports,
      60             :                          int                       executable,
      61             :                          uchar const *             data,
      62             :                          ulong                     data_len,
      63             :                          fd_bank_t               * bank,
      64        5091 :                          fd_capture_ctx_t        * capture_ctx ) {
      65             :   /* Compute the new hash of the account */
      66        5091 :   fd_hashes_account_lthash_simple( pubkey, owner, lamports, executable, data, data_len, lthash_post );
      67             : 
      68             :   /* Subtract the old hash of the account from the bank lthash */
      69        5091 :   fd_lthash_value_t * bank_lthash = fd_bank_lthash_locking_modify( bank );
      70        5091 :   fd_lthash_sub( bank_lthash, lthash_prev );
      71             : 
      72             :   /* Add the new hash of the account to the bank lthash */
      73        5091 :   fd_lthash_add( bank_lthash, lthash_post );
      74             : 
      75        5091 :   fd_bank_lthash_end_locking_modify( bank );
      76             : 
      77        5091 :   if( FD_UNLIKELY( capture_ctx &&
      78        5091 :                    capture_ctx->capture_solcap &&
      79        5091 :                    bank->f.slot>=capture_ctx->solcap_start_slot ) ) {
      80           0 :     fd_solana_account_meta_t solana_meta[1];
      81           0 :     fd_solana_account_meta_init( solana_meta, lamports, owner, executable );
      82           0 :     fd_capture_link_write_account_update(
      83           0 :       capture_ctx,
      84           0 :       capture_ctx->current_txn_idx,
      85           0 :       (fd_pubkey_t*)pubkey,
      86           0 :       solana_meta,
      87           0 :       bank->f.slot,
      88           0 :       data,
      89           0 :       data_len );
      90           0 :   }
      91        5091 : }
      92             : 
      93             : void
      94             : fd_hashes_apply_hard_forks( fd_hash_t *            hash,
      95             :                             ulong                  slot,
      96             :                             ulong                  parent_slot,
      97             :                             fd_hard_fork_t const * hard_forks,
      98          15 :                             ulong                  hard_fork_cnt ) {
      99          15 :   ulong sum = 0UL;
     100          39 :   for( ulong i=0UL; i<hard_fork_cnt; i++ ) {
     101          24 :     if( FD_UNLIKELY( parent_slot<hard_forks[ i ].slot && hard_forks[ i ].slot<=slot ) ) sum += hard_forks[ i ].cnt;
     102          24 :   }
     103             : 
     104          15 :   if( FD_UNLIKELY( !sum ) ) return;
     105             : 
     106           9 :   ulong sum_le[ 1 ];
     107           9 :   FD_STORE( ulong, sum_le, sum );
     108             : 
     109           9 :   fd_sha256_t sha;
     110           9 :   fd_sha256_init( &sha );
     111           9 :   fd_sha256_append( &sha, hash->hash, sizeof(fd_hash_t) );
     112           9 :   fd_sha256_append( &sha, sum_le,     sizeof(ulong)     );
     113           9 :   fd_sha256_fini( &sha, hash->hash );
     114           9 : }

Generated by: LCOV version 1.14