LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_cost_tracker.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 238 293 81.2 %
Date: 2026-02-22 05:44:04 Functions: 17 17 100.0 %

          Line data    Source code
       1             : #include "fd_cost_tracker.h"
       2             : #include "fd_system_ids.h"
       3             : #include "fd_bank.h"
       4             : #include "fd_runtime.h"
       5             : #include "../features/fd_features.h"
       6             : #include "../vm/fd_vm_base.h"
       7             : 
       8             : struct account_cost {
       9             :   fd_pubkey_t account;
      10             :   ulong       cost;
      11             : 
      12             :   struct {
      13             :     ulong next;
      14             :   } map;
      15             : };
      16             : 
      17             : typedef struct account_cost account_cost_t;
      18             : 
      19             : #define MAP_NAME               account_cost_map
      20             : #define MAP_KEY_T              fd_pubkey_t
      21             : #define MAP_ELE_T              account_cost_t
      22           6 : #define MAP_KEY                account
      23          54 : #define MAP_KEY_EQ(k0,k1)      (fd_pubkey_eq( k0, k1 ))
      24          72 : #define MAP_KEY_HASH(key,seed) (fd_hash( seed, key, sizeof(fd_pubkey_t) ))
      25           6 : #define MAP_NEXT               map.next
      26             : #include "../../util/tmpl/fd_map_chain.c"
      27             : 
      28             : struct cost_tracker_outer {
      29             :   fd_cost_tracker_t cost_tracker[1];
      30             :   ulong             pool_offset;
      31             :   ulong             accounts_used;
      32             :   ulong             magic;
      33             : };
      34             : 
      35             : typedef struct cost_tracker_outer cost_tracker_outer_t;
      36             : 
      37             : FD_FN_CONST ulong
      38        1263 : fd_cost_tracker_align( void ) {
      39        1263 :   return FD_COST_TRACKER_ALIGN;
      40        1263 : }
      41             : 
      42             : FD_FN_CONST ulong
      43           9 : fd_cost_tracker_footprint( void ) {
      44           9 :   ulong map_chain_cnt = account_cost_map_chain_cnt_est( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT );
      45             : 
      46           9 :   ulong l = FD_LAYOUT_INIT;
      47           9 :   l = FD_LAYOUT_APPEND( l,  fd_cost_tracker_align(),  sizeof(cost_tracker_outer_t) );
      48           9 :   l = FD_LAYOUT_APPEND( l,  account_cost_map_align(), account_cost_map_footprint( map_chain_cnt ) );
      49           9 :   l = FD_LAYOUT_APPEND( l,  alignof(account_cost_t),  FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT*sizeof(account_cost_t) );
      50           9 :   return FD_LAYOUT_FINI( l, fd_cost_tracker_align() );
      51           9 : }
      52             : 
      53             : void *
      54             : fd_cost_tracker_new( void * shmem,
      55             :                      int    larger_max_cost_per_block,
      56         408 :                      ulong  seed ) {
      57         408 :   if( FD_UNLIKELY( !shmem ) ) {
      58           3 :     FD_LOG_WARNING(( "NULL shmem" ));
      59           3 :     return NULL;
      60           3 :   }
      61             : 
      62         405 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_cost_tracker_align() ) ) ) {
      63           0 :     FD_LOG_WARNING(( "misaligned shmem" ));
      64           0 :     return NULL;
      65           0 :   }
      66             : 
      67         405 :   ulong map_chain_cnt = account_cost_map_chain_cnt_est( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT );
      68             : 
      69         405 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
      70         405 :   cost_tracker_outer_t * cost_tracker = FD_SCRATCH_ALLOC_APPEND( l, fd_cost_tracker_align(),  sizeof(cost_tracker_outer_t) );
      71         405 :   void * _map                         = FD_SCRATCH_ALLOC_APPEND( l, account_cost_map_align(), account_cost_map_footprint( map_chain_cnt ) );
      72         405 :   void * _accounts                    = FD_SCRATCH_ALLOC_APPEND( l, alignof(account_cost_t),  FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT*sizeof(account_cost_t) );
      73             : 
      74           0 :   account_cost_map_t * map = account_cost_map_join( account_cost_map_new( _map, map_chain_cnt, seed ) );
      75         405 :   FD_TEST( map );
      76             : 
      77         405 :   cost_tracker->pool_offset = (ulong)_accounts-(ulong)cost_tracker;
      78             : 
      79         405 :   cost_tracker->cost_tracker->larger_max_cost_per_block = larger_max_cost_per_block;
      80             : 
      81         405 :   (void)_accounts;
      82             : 
      83         405 :   FD_COMPILER_MFENCE();
      84         405 :   FD_VOLATILE( cost_tracker->magic ) = FD_COST_TRACKER_MAGIC;
      85         405 :   FD_COMPILER_MFENCE();
      86             : 
      87         405 :   return shmem;
      88         405 : }
      89             : 
      90             : fd_cost_tracker_t *
      91         411 : fd_cost_tracker_join( void * shct ) {
      92         411 :   if( FD_UNLIKELY( !shct ) ) {
      93           3 :     FD_LOG_WARNING(( "NULL mem" ));
      94           3 :     return NULL;
      95           3 :   }
      96             : 
      97         408 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shct, fd_cost_tracker_align() ) ) ) {
      98           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      99           0 :     return NULL;
     100           0 :   }
     101             : 
     102         408 :   cost_tracker_outer_t * cost_tracker = (cost_tracker_outer_t *)shct;
     103             : 
     104         408 :   if( FD_UNLIKELY( cost_tracker->magic!=FD_COST_TRACKER_MAGIC ) ) {
     105           3 :     FD_LOG_WARNING(( "Invalid cost tracker magic" ));
     106           3 :     return NULL;
     107           3 :   }
     108             : 
     109         405 :   return cost_tracker->cost_tracker;
     110         408 : }
     111             : 
     112             : void
     113             : fd_cost_tracker_init( fd_cost_tracker_t *   cost_tracker,
     114             :                       fd_features_t const * features,
     115          63 :                       ulong                 slot ) {
     116          63 :   if( FD_FEATURE_ACTIVE( slot, features, raise_block_limits_to_100m ) ) {
     117          27 :     cost_tracker->block_cost_limit   = FD_MAX_BLOCK_UNITS_SIMD_0286;
     118          27 :     cost_tracker->vote_cost_limit    = FD_MAX_VOTE_UNITS;
     119          27 :     cost_tracker->account_cost_limit = FD_MAX_WRITABLE_ACCOUNT_UNITS;
     120          36 :   } else if( FD_FEATURE_ACTIVE( slot, features, raise_block_limits_to_60m ) ) {
     121           0 :     cost_tracker->block_cost_limit   = FD_MAX_BLOCK_UNITS_SIMD_0256;
     122           0 :     cost_tracker->vote_cost_limit    = FD_MAX_VOTE_UNITS;
     123           0 :     cost_tracker->account_cost_limit = FD_MAX_WRITABLE_ACCOUNT_UNITS;
     124          36 :   } else {
     125          36 :     cost_tracker->block_cost_limit   = FD_MAX_BLOCK_UNITS_SIMD_0207;
     126          36 :     cost_tracker->vote_cost_limit    = FD_MAX_VOTE_UNITS;
     127          36 :     cost_tracker->account_cost_limit = FD_MAX_WRITABLE_ACCOUNT_UNITS;
     128          36 :   }
     129             : 
     130          63 :   if( FD_UNLIKELY( cost_tracker->larger_max_cost_per_block ) ) cost_tracker->block_cost_limit = LARGER_MAX_COST_PER_BLOCK;
     131             : 
     132             :   /* https://github.com/anza-xyz/agave/blob/v3.0.1/runtime/src/bank.rs#L4059-L4066 */
     133          63 :   if( FD_FEATURE_ACTIVE( slot, features, raise_account_cu_limit ) ) {
     134          27 :     cost_tracker->account_cost_limit = fd_ulong_sat_mul( cost_tracker->block_cost_limit, 40UL ) / 100UL;
     135          27 :   }
     136             : 
     137          63 :   cost_tracker->block_cost                   = 0UL;
     138          63 :   cost_tracker->vote_cost                    = 0UL;
     139          63 :   cost_tracker->allocated_accounts_data_size = 0UL;
     140             : 
     141          63 :   cost_tracker_outer_t * outer = fd_type_pun( cost_tracker );
     142          63 :   outer->accounts_used = 0UL;
     143          63 :   account_cost_map_reset( fd_type_pun( outer+1UL ) );
     144          63 : }
     145             : 
     146             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L313-L321 */
     147             : FD_FN_PURE static inline ulong
     148          81 : get_instructions_data_cost( fd_txn_in_t const * txn_in ) {
     149          81 :   ulong total_instr_data_sz = 0UL;
     150         144 :   for( ushort i=0; i<TXN( txn_in->txn )->instr_cnt; i++ ) {
     151          63 :     total_instr_data_sz += TXN( txn_in->txn )->instr[ i ].data_sz;
     152          63 :   }
     153          81 :   return total_instr_data_sz / FD_PACK_INV_COST_PER_INSTR_DATA_BYTE;
     154          81 : }
     155             : 
     156             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L152-L187 */
     157             : FD_FN_PURE static inline ulong
     158          81 : get_signature_cost( fd_txn_in_t const * txn_in, fd_bank_t * bank ) {
     159          81 :   fd_txn_t const *       txn      = TXN( txn_in->txn );
     160          81 :   void const *           payload  = txn_in->txn->payload;
     161          81 :   fd_acct_addr_t const * accounts = fd_txn_get_acct_addrs( txn, payload );
     162             : 
     163             :   /* Compute signature counts (both normal + precompile)
     164             :      TODO: Factor this logic out into a shared function that can be used
     165             :      both here and in fd_pack_cost.h */
     166          81 :   ulong signature_cost                       = fd_ulong_sat_mul( txn->signature_cnt, FD_PACK_COST_PER_SIGNATURE );
     167          81 :   ulong num_secp256k1_instruction_signatures = 0UL;
     168          81 :   ulong num_ed25519_instruction_signatures   = 0UL;
     169          81 :   ulong num_secp256r1_instruction_signatures = 0UL;
     170             : 
     171         144 :   for( ushort i=0; i<txn->instr_cnt; i++ ) {
     172          63 :     fd_txn_instr_t const * instr = &txn->instr[ i ];
     173          63 :     if( instr->data_sz==0UL ) continue;
     174             : 
     175          63 :     fd_acct_addr_t const * prog_id    = accounts + instr->program_id;
     176          63 :     uchar const *          instr_data = fd_txn_get_instr_data( instr, payload );
     177             : 
     178          63 :     if( fd_memeq( prog_id, fd_solana_ed25519_sig_verify_program_id.key, sizeof(fd_pubkey_t) ) ) {
     179           0 :       num_ed25519_instruction_signatures += (ulong)instr_data[ 0 ];
     180          63 :     } else if( fd_memeq( prog_id, fd_solana_keccak_secp_256k_program_id.key, sizeof(fd_pubkey_t) ) ) {
     181           0 :       num_secp256k1_instruction_signatures += (ulong)instr_data[ 0 ];
     182          63 :     } else if( fd_memeq( prog_id, fd_solana_secp256r1_program_id.key, sizeof(fd_pubkey_t) ) ) {
     183           0 :       num_secp256r1_instruction_signatures += (ulong)instr_data[ 0 ];
     184           0 :     }
     185          63 :   }
     186             : 
     187             :   /* No direct permalink, just factored out for readability */
     188          81 :   ulong secp256k1_verify_cost = fd_ulong_sat_mul( FD_PACK_COST_PER_SECP256K1_SIGNATURE, num_secp256k1_instruction_signatures );
     189             : 
     190             :   /* https://github.com/anza-xyz/agave/blob/master/cost-model/src/cost_model.rs#L151 */
     191          81 :   ulong ed25519_verify_cost = fd_ulong_sat_mul( FD_PACK_COST_PER_ED25519_SIGNATURE, num_ed25519_instruction_signatures );
     192             : 
     193             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L162-L167 */
     194          81 :   ulong secp256r1_verify_cost = 0UL;
     195          81 :   if( FD_FEATURE_ACTIVE_BANK( bank, enable_secp256r1_precompile ) ) {
     196          18 :     secp256r1_verify_cost = fd_ulong_sat_mul( FD_PACK_COST_PER_SECP256R1_SIGNATURE, num_secp256r1_instruction_signatures );
     197          18 :   }
     198             : 
     199             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L169-L186 */
     200          81 :   return fd_ulong_sat_add( signature_cost,
     201          81 :                            fd_ulong_sat_add( secp256k1_verify_cost,
     202          81 :                                              fd_ulong_sat_add( ed25519_verify_cost,
     203          81 :                                                                secp256r1_verify_cost ) ) );
     204          81 : }
     205             : 
     206             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L190-L192 */
     207             : FD_FN_PURE static inline ulong
     208          81 : get_write_lock_cost( ulong num_write_locks ) {
     209          81 :   return fd_ulong_sat_mul( num_write_locks, FD_WRITE_LOCK_UNITS );
     210          81 : }
     211             : 
     212             : /* Loop through all instructions here and deserialize the instruction data to try to determine any
     213             :    system program allocations done.
     214             : 
     215             :    https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L367-L386 */
     216             : static inline ulong
     217          81 : calculate_allocated_accounts_data_size( fd_txn_in_t const * txn_in ) {
     218          81 :   fd_txn_t const * txn     = TXN( txn_in->txn );
     219          81 :   void const *     payload = txn_in->txn->payload;
     220             : 
     221          81 :   ulong allocated_accounts_data_size = 0UL;
     222         138 :   for( ushort i=0; i<txn->instr_cnt; i++ ) {
     223          60 :     fd_txn_instr_t const * instr      = &txn->instr[ i ];
     224          60 :     fd_acct_addr_t const * accounts   = fd_txn_get_acct_addrs( txn, payload );
     225          60 :     fd_acct_addr_t const * prog_id    = accounts + instr->program_id;
     226          60 :     uchar const *          instr_data = fd_txn_get_instr_data( instr, payload );
     227             : 
     228          60 :     if( instr->data_sz==0UL || !fd_memeq( prog_id, &fd_solana_system_program_id, sizeof(fd_pubkey_t) ) ) continue;
     229             : 
     230          24 :     fd_bincode_decode_ctx_t ctx = {
     231          24 :       .data    = instr_data,
     232          24 :       .dataend = instr_data + instr->data_sz,
     233          24 :     };
     234             : 
     235          24 :     ulong total_sz = 0UL;
     236          24 :     int err = fd_system_program_instruction_decode_footprint( &ctx, &total_sz );
     237          24 :     if( FD_UNLIKELY( err ) ) continue;
     238             : 
     239          24 :     uchar buf[total_sz];
     240          24 :     fd_system_program_instruction_t * instruction = fd_system_program_instruction_decode( buf, &ctx );
     241          24 :     if( FD_UNLIKELY( !instruction ) ) continue;
     242             : 
     243             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L330-L346 */
     244          24 :     ulong space = 0UL;
     245             : 
     246          24 :     switch( instruction->discriminant ) {
     247           0 :       case fd_system_program_instruction_enum_create_account: {
     248           0 :         space = instruction->inner.create_account.space;
     249           0 :         break;
     250           0 :       }
     251           0 :       case fd_system_program_instruction_enum_create_account_with_seed: {
     252           0 :         space = instruction->inner.create_account_with_seed.space;
     253           0 :         break;
     254           0 :       }
     255          24 :       case fd_system_program_instruction_enum_allocate: {
     256          24 :         space = instruction->inner.allocate;
     257          24 :         break;
     258           0 :       }
     259           0 :       case fd_system_program_instruction_enum_allocate_with_seed: {
     260           0 :         space = instruction->inner.allocate_with_seed.space;
     261           0 :         break;
     262           0 :       }
     263          24 :     }
     264             : 
     265             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L373-L380 */
     266          24 :     if( FD_UNLIKELY( space>FD_RUNTIME_ACC_SZ_MAX ) ) return 0UL;
     267             : 
     268          21 :     allocated_accounts_data_size = fd_ulong_sat_add( allocated_accounts_data_size, space );
     269          21 :   }
     270             : 
     271             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L396-L397 */
     272          78 :   return fd_ulong_min( 2UL*FD_RUNTIME_ACC_SZ_MAX, allocated_accounts_data_size );
     273          81 : }
     274             : 
     275             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L123-L149 */
     276             : static inline fd_transaction_cost_t
     277             : calculate_non_vote_transaction_cost( fd_bank_t *          bank,
     278             :                                      fd_txn_in_t const *  txn_in,
     279             :                                      fd_txn_out_t const * txn_out,
     280             :                                      ulong                loaded_accounts_data_size_cost,
     281          81 :                                      ulong                data_bytes_cost ) {
     282             : 
     283             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L132 */
     284          81 :   ulong signature_cost = get_signature_cost( txn_in, bank );
     285             : 
     286             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L133 */
     287          81 :   ulong write_lock_cost = get_write_lock_cost( fd_txn_account_cnt( TXN( txn_in->txn ), FD_TXN_ACCT_CAT_WRITABLE ) );
     288             : 
     289             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L135-L136 */
     290          81 :   ulong allocated_accounts_data_size = calculate_allocated_accounts_data_size( txn_in );
     291             : 
     292          81 :   return (fd_transaction_cost_t) {
     293          81 :     .type = FD_TXN_COST_TYPE_TRANSACTION,
     294          81 :     .transaction = {
     295          81 :       .signature_cost                 = signature_cost,
     296          81 :       .write_lock_cost                = write_lock_cost,
     297          81 :       .data_bytes_cost                = data_bytes_cost,
     298          81 :       .programs_execution_cost        = fd_ulong_sat_sub( txn_out->details.compute_budget.compute_unit_limit,
     299          81 :                                                           txn_out->details.compute_budget.compute_meter ),
     300          81 :       .loaded_accounts_data_size_cost = loaded_accounts_data_size_cost,
     301          81 :       .allocated_accounts_data_size   = allocated_accounts_data_size,
     302          81 :     }
     303          81 :   };
     304          81 : }
     305             : 
     306             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/transaction_cost.rs#L26-L42 */
     307             : FD_FN_PURE static inline ulong
     308          36 : transaction_cost_sum( fd_transaction_cost_t const * txn_cost ) {
     309          36 :   switch( txn_cost->type ) {
     310           0 :     case FD_TXN_COST_TYPE_SIMPLE_VOTE: {
     311             :       /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/transaction_cost.rs#L38 */
     312           0 :       return FD_PACK_SIMPLE_VOTE_COST;
     313           0 :     }
     314          36 :     case FD_TXN_COST_TYPE_TRANSACTION: {
     315             :       /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/transaction_cost.rs#L164-L171 */
     316          36 :       fd_usage_cost_details_t const * usage_cost = &txn_cost->transaction;
     317          36 :       ulong                           cost       = 0UL;
     318             : 
     319          36 :       cost = fd_ulong_sat_add( cost, usage_cost->signature_cost );
     320          36 :       cost = fd_ulong_sat_add( cost, usage_cost->write_lock_cost );
     321          36 :       cost = fd_ulong_sat_add( cost, usage_cost->data_bytes_cost );
     322          36 :       cost = fd_ulong_sat_add( cost, usage_cost->programs_execution_cost );
     323          36 :       cost = fd_ulong_sat_add( cost, usage_cost->loaded_accounts_data_size_cost );
     324             : 
     325          36 :       return cost;
     326           0 :     }
     327           0 :     default: {
     328           0 :       __builtin_unreachable();
     329           0 :     }
     330          36 :   }
     331          36 : }
     332             : 
     333             : FD_FN_PURE static inline ulong
     334          36 : get_allocated_accounts_data_size( fd_transaction_cost_t const * txn_cost ) {
     335          36 :   switch( txn_cost->type ) {
     336           0 :   case FD_TXN_COST_TYPE_SIMPLE_VOTE:
     337           0 :     return 0UL;
     338          36 :   case FD_TXN_COST_TYPE_TRANSACTION:
     339          36 :     return txn_cost->transaction.allocated_accounts_data_size;
     340           0 :   default:
     341           0 :     __builtin_unreachable();
     342          36 :   }
     343          36 : }
     344             : 
     345             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L277-L322 */
     346             : static inline int
     347             : would_fit( fd_cost_tracker_t const *     cost_tracker,
     348             :            fd_txn_out_t *                txn_out,
     349          18 :            fd_transaction_cost_t const * tx_cost ) {
     350             : 
     351             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L281 */
     352          18 :   ulong cost = transaction_cost_sum( tx_cost );
     353             : 
     354             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L283-L288 */
     355          18 :   if( tx_cost->type==FD_TXN_COST_TYPE_SIMPLE_VOTE ) {
     356           0 :     if( FD_UNLIKELY( fd_ulong_sat_add( cost_tracker->vote_cost, cost )>cost_tracker->vote_cost_limit ) ) {
     357           0 :       return FD_COST_TRACKER_ERROR_WOULD_EXCEED_VOTE_MAX_LIMIT;
     358           0 :     }
     359           0 :   }
     360             : 
     361             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L290-L293 */
     362          18 :   if( FD_UNLIKELY( fd_ulong_sat_add( cost_tracker->block_cost, cost )>cost_tracker->block_cost_limit ) ) {
     363           0 :     return FD_COST_TRACKER_ERROR_WOULD_EXCEED_BLOCK_MAX_LIMIT;
     364           0 :   }
     365             : 
     366             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L295-L298 */
     367          18 :   if( FD_UNLIKELY( cost>cost_tracker->account_cost_limit ) ) {
     368           0 :     return FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_MAX_LIMIT;
     369           0 :   }
     370             : 
     371             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L300-L301 */
     372          18 :   ulong allocated_accounts_data_size = fd_ulong_sat_add( cost_tracker->allocated_accounts_data_size,
     373          18 :                                                          get_allocated_accounts_data_size( tx_cost ) );
     374             : 
     375             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L303-L304 */
     376          18 :   if( FD_UNLIKELY( allocated_accounts_data_size>FD_MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA ) ) {
     377           0 :     return FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT;
     378           0 :   }
     379             : 
     380             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L308-L319 */
     381             : 
     382          18 :   account_cost_map_t const * map = fd_type_pun_const(((cost_tracker_outer_t const *)cost_tracker)+1UL);
     383          18 :   account_cost_t const * pool = fd_type_pun_const( (void*)((ulong)cost_tracker + ((cost_tracker_outer_t const *)cost_tracker)->pool_offset) );
     384             : 
     385          54 :   for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
     386          36 :     if( txn_out->accounts.is_writable[i]==0 ) continue;
     387             : 
     388          33 :     fd_pubkey_t const * writable_acc = &txn_out->accounts.keys[i];
     389             : 
     390          33 :     account_cost_t const * chained_cost = account_cost_map_ele_query_const( map, writable_acc, NULL, pool );
     391          33 :     if( FD_UNLIKELY( chained_cost && fd_ulong_sat_add( chained_cost->cost, cost )>cost_tracker->account_cost_limit ) ) {
     392           0 :       return FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_MAX_LIMIT;
     393           0 :     }
     394          33 :   }
     395             : 
     396          18 :   return FD_COST_TRACKER_SUCCESS;
     397          18 : }
     398             : 
     399             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L352-L372 */
     400             : static inline void
     401             : add_transaction_execution_cost( fd_cost_tracker_t *           _cost_tracker,
     402             :                                 fd_txn_out_t *                txn_out,
     403             :                                 fd_transaction_cost_t const * tx_cost,
     404          18 :                                 ulong                         adjustment ) {
     405          18 :   cost_tracker_outer_t * cost_tracker = fd_type_pun( _cost_tracker );
     406          18 :   account_cost_map_t * map = fd_type_pun( cost_tracker+1UL );
     407          18 :   account_cost_t * pool = fd_type_pun( (void*)((ulong)cost_tracker+cost_tracker->pool_offset) );
     408             : 
     409          54 :   for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
     410          36 :     if( FD_LIKELY( txn_out->accounts.is_writable[i]==0 ) ) continue;
     411             : 
     412          33 :     fd_pubkey_t const * writable_acc = &txn_out->accounts.keys[i];
     413             : 
     414          33 :     account_cost_t * account_cost = account_cost_map_ele_query( map, writable_acc, NULL, pool );
     415          33 :     if( FD_UNLIKELY( !account_cost ) ) {
     416           6 :       FD_TEST( cost_tracker->accounts_used<FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT );
     417             : 
     418           6 :       account_cost = pool+cost_tracker->accounts_used;
     419           6 :       cost_tracker->accounts_used++;
     420             : 
     421           6 :       account_cost->account = *writable_acc;
     422           6 :       account_cost->cost    = adjustment;
     423             : 
     424           6 :       account_cost_map_ele_insert( map, account_cost, pool );
     425          27 :     } else {
     426          27 :       account_cost->cost = fd_ulong_sat_add( account_cost->cost, adjustment );
     427          27 :     }
     428          33 :   }
     429             : 
     430          18 :   cost_tracker->cost_tracker->block_cost = fd_ulong_sat_add( cost_tracker->cost_tracker->block_cost, adjustment );
     431          18 :   if( FD_UNLIKELY( tx_cost->type==FD_TXN_COST_TYPE_SIMPLE_VOTE ) ) {
     432           0 :     cost_tracker->cost_tracker->vote_cost = fd_ulong_sat_add( cost_tracker->cost_tracker->vote_cost, adjustment );
     433           0 :   }
     434          18 : }
     435             : 
     436             : 
     437             : 
     438             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L323-L328 */
     439             : FD_FN_PURE ulong
     440          81 : fd_cost_tracker_calculate_loaded_accounts_data_size_cost( fd_txn_out_t const * txn_out ) {
     441          81 :   ulong cost = fd_ulong_sat_sub( fd_ulong_sat_add( txn_out->details.loaded_accounts_data_size,
     442          81 :                                                    FD_ACCOUNT_DATA_COST_PAGE_SIZE ),
     443          81 :                                  1UL );
     444          81 :   cost /= FD_ACCOUNT_DATA_COST_PAGE_SIZE;
     445          81 :   return fd_ulong_sat_mul( cost, FD_VM_HEAP_COST );
     446          81 : }
     447             : 
     448             : void
     449             : fd_cost_tracker_calculate_cost( fd_bank_t *         bank,
     450             :                                 fd_txn_in_t const * txn_in,
     451          81 :                                 fd_txn_out_t *      txn_out ) {
     452             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/cost-model/src/cost_model.rs#L83-L85 */
     453          81 :   fd_transaction_cost_t * txn_cost = &txn_out->details.txn_cost;
     454          81 :   if( txn_out->details.is_simple_vote ) {
     455           0 :     txn_cost->type = FD_TXN_COST_TYPE_SIMPLE_VOTE;
     456          81 :   } else {
     457             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L78-L81 */
     458          81 :     ulong loaded_accounts_data_size_cost = fd_cost_tracker_calculate_loaded_accounts_data_size_cost( txn_out );
     459             : 
     460             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L82-L83 */
     461          81 :     ulong instructions_data_cost = get_instructions_data_cost( txn_in );
     462             : 
     463             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L85-L93 */
     464          81 :     *txn_cost = calculate_non_vote_transaction_cost( bank, txn_in, txn_out, loaded_accounts_data_size_cost, instructions_data_cost );
     465          81 :   }
     466          81 : }
     467             : 
     468             : int
     469             : fd_cost_tracker_try_add_cost( fd_cost_tracker_t * cost_tracker,
     470          18 :                               fd_txn_out_t *      txn_out ) {
     471             : 
     472             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L167 */
     473          18 :   int err = would_fit( cost_tracker, txn_out, &txn_out->details.txn_cost );
     474          18 :   if( FD_UNLIKELY( err!=FD_COST_TRACKER_SUCCESS ) ) {
     475           0 :     return err;
     476           0 :   }
     477             : 
     478             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L325-L335 */
     479             : 
     480             :   /* We don't need `updated_costliest_account_cost` since it seems to be
     481             :      for a different use case other than validating block cost limits.
     482             :      https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L168 */
     483             : 
     484             :   /* Note: We purposely omit signature counts updates since they're not relevant to cost calculations right now. */
     485          18 :   cost_tracker->allocated_accounts_data_size += get_allocated_accounts_data_size( &txn_out->details.txn_cost );
     486          18 :   add_transaction_execution_cost( cost_tracker, txn_out, &txn_out->details.txn_cost, transaction_cost_sum( &txn_out->details.txn_cost ) );
     487          18 :   return FD_COST_TRACKER_SUCCESS;
     488          18 : }

Generated by: LCOV version 1.14