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

Generated by: LCOV version 1.14