LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_cost_tracker.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 44 294 15.0 %
Date: 2025-11-08 04:42:58 Functions: 4 16 25.0 %

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

Generated by: LCOV version 1.14