LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_cost_tracker.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 6 33 18.2 %
Date: 2025-11-27 21:31:50 Functions: 0 73 0.0 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_flamenco_runtime_fd_cost_tracker_h
       2             : #define HEADER_fd_src_flamenco_runtime_fd_cost_tracker_h
       3             : 
       4             : /* fd_cost_tracker_t is a block-level tracker for various limits
       5             :    including CU consumption, writable account usage, and account data
       6             :    size.  A cost is calculated per-transaction and is accumulated to the
       7             :    block.  If a block's limits are exceeded, then the block is marked as
       8             :    dead. */
       9             : 
      10             : #include "fd_executor.h"
      11             : #include "fd_runtime_err.h"
      12             : #include "../vm/fd_vm_base.h"
      13             : #include "../../disco/pack/fd_pack.h" /* TODO: Layering violation */
      14             : #include "../../disco/pack/fd_pack_cost.h"
      15             : 
      16             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L62-L79 */
      17             : 
      18          39 : #define FD_WRITE_LOCK_UNITS                   (      300UL) /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/block_cost_limits.rs#L20 */
      19             : #define FD_MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA (100000000UL) /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/block_cost_limits.rs#L42 */
      20           0 : #define FD_MAX_WRITABLE_ACCOUNT_UNITS         ( 12000000UL) /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/block_cost_limits.rs#L34 */
      21           0 : #define FD_MAX_BLOCK_UNITS_SIMD_0207          ( 50000000UL) /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/block_cost_limits.rs#L50-L56 */
      22           0 : #define FD_MAX_BLOCK_UNITS_SIMD_0256          ( 60000000UL) /* https://github.com/anza-xyz/agave/blob/v2.3.0/cost-model/src/block_cost_limits.rs#L50-L56 */
      23          39 : #define FD_MAX_BLOCK_UNITS_SIMD_0286          (100000000UL) /* https://github.com/anza-xyz/agave/blob/v3.0.0/cost-model/src/block_cost_limits.rs#L30 */
      24           0 : #define FD_MAX_VOTE_UNITS                     ( 36000000UL) /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/block_cost_limits.rs#L38 */
      25             : 
      26             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L18-L33 */
      27           0 : #define FD_COST_TRACKER_SUCCESS                                     (0)
      28           0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_BLOCK_MAX_LIMIT          (1)
      29           0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_VOTE_MAX_LIMIT           (2)
      30           0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_MAX_LIMIT        (3)
      31           0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT (4)
      32             : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_TOTAL_LIMIT (5)
      33             : 
      34             : /* A reasonably tight bound can be derived based on CUs.  The most
      35             :    optimal use of CUs is to pack as many writable accounts as possible
      36             :    for as cheaply as possible.  This means we should try to pack as many
      37             :    writable accounts as possible into each transaction.  Each
      38             :    transaction requires at least one signature.  We will assume that all
      39             :    of these accounts have no account data.
      40             : 
      41             :    64 - Max number of accounts per transaction.  In this case we will
      42             :    assume that all of these accounts are writable and have no data.
      43             :    100000000 - CUs per slot
      44             :    720 - Cost of a signature
      45             :    300 - Cost of a writable account write lock
      46             : 
      47             :    We can have (100000000 / (720 + 64 * 300)) = 5020 transactions per
      48             :    slot with maximum writable account utilization.
      49             : 
      50             :    So, 5020 transactions per slot * 64 accounts per transaction =
      51             :    321280 writable accounts per slot.
      52             : 
      53             :    NOTE: A slightly tighter bound can probably be derived. */
      54             : 
      55          24 : #define FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT ( \
      56          24 :   FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_TRANSACTION * (FD_MAX_BLOCK_UNITS_SIMD_0286 / ( FD_WRITE_LOCK_UNITS * FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_TRANSACTION + FD_PACK_COST_PER_SIGNATURE)) )
      57             : FD_STATIC_ASSERT( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT==321280UL, "Incorrect FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT" );
      58             : 
      59             : /* TODO: Extremely gross.  Used because these are in a pool which needs
      60             :    to be compile time sized T. */
      61             : #define FD_COST_TRACKER_CHAIN_CNT_EST (262144UL)
      62             : #define FD_COST_TRACKER_FOOTPRINT                                                                   \
      63             :   ( FD_LAYOUT_FINI( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND(         \
      64             :     FD_LAYOUT_INIT,                                                                                 \
      65             :       128UL /* alignof(fd_cost_tracker_t) */,  128UL /* sizeof(fd_cost_tracker_t) */          ),    \
      66             :       128UL /* alignof(cost_tracker_out_t )*/, 128UL /* sizeof(cost_tracker_out_t ) */        ),    \
      67             :       8UL   /* alignof(account_cost_map_t) */, FD_COST_TRACKER_CHAIN_CNT_EST*8UL /*sizeof(ulong)*/ +24UL /* sizeof(account_cost_map_t) */ ), \
      68             :       8UL   /* alignof(account_cost_t) */,     FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT*48UL /*sizeof(account_cost_t)*/ ), \
      69             :       128UL ) )                                               \
      70             : 
      71          15 : #define FD_COST_TRACKER_MAGIC (0xF17EDA2CE7C05170UL) /* FIREDANCER COST V0 */
      72             : 
      73          93 : #define FD_COST_TRACKER_ALIGN (128UL)
      74             : 
      75             : struct __attribute__((aligned(FD_COST_TRACKER_ALIGN))) fd_cost_tracker {
      76             :   ulong block_cost;
      77             :   ulong vote_cost;
      78             :   ulong allocated_accounts_data_size;
      79             : 
      80             :   ulong block_cost_limit;
      81             :   ulong vote_cost_limit;
      82             :   ulong account_cost_limit;
      83             : 
      84             :   int larger_max_cost_per_block;
      85             : };
      86             : 
      87             : typedef struct fd_cost_tracker fd_cost_tracker_t;
      88             : 
      89             : FD_PROTOTYPES_BEGIN
      90             : 
      91             : static inline int
      92           0 : fd_cost_tracker_err_to_runtime_err( int err ) {
      93           0 :   switch( err ) {
      94           0 :     case FD_COST_TRACKER_SUCCESS:
      95           0 :       return FD_RUNTIME_EXECUTE_SUCCESS;
      96           0 :     case FD_COST_TRACKER_ERROR_WOULD_EXCEED_BLOCK_MAX_LIMIT:
      97           0 :       return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_BLOCK_COST_LIMIT;
      98           0 :     case FD_COST_TRACKER_ERROR_WOULD_EXCEED_VOTE_MAX_LIMIT:
      99           0 :       return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_VOTE_COST_LIMIT;
     100           0 :     case FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_MAX_LIMIT:
     101           0 :       return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_ACCOUNT_COST_LIMIT;
     102           0 :     case FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT:
     103           0 :       return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT;
     104           0 :     case FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_TOTAL_LIMIT:
     105           0 :       return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_ACCOUNT_DATA_TOTAL_LIMIT;
     106           0 :     default:
     107           0 :       __builtin_unreachable();
     108           0 :   }
     109           0 : }
     110             : 
     111             : FD_FN_CONST ulong
     112             : fd_cost_tracker_align( void );
     113             : 
     114             : FD_FN_CONST ulong
     115             : fd_cost_tracker_footprint( void );
     116             : 
     117             : void *
     118             : fd_cost_tracker_new( void * shmem,
     119             :                      int    larger_max_cost_per_block,
     120             :                      ulong  seed );
     121             : 
     122             : fd_cost_tracker_t *
     123             : fd_cost_tracker_join( void * shct );
     124             : 
     125             : void
     126             : fd_cost_tracker_init( fd_cost_tracker_t *   cost_tracker,
     127             :                       fd_features_t const * features,
     128             :                       ulong                 slot );
     129             : 
     130             : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L323-L328 */
     131             : FD_FN_PURE ulong
     132             : fd_cost_tracker_calculate_loaded_accounts_data_size_cost( fd_txn_out_t const * txn_out );
     133             : 
     134             : /* fd_cost_tracker_calculate_cost_and_add takes a transaction,
     135             :    calculates the cost of the transaction in terms of various block
     136             :    level limits and adds it to the cost tracker.  If the incremental
     137             :    transaction fits in the block, then the cost tracking is updated and
     138             :    FD_COST_TRACKER_SUCCESS is returned.  If the transaction does not
     139             :    fit then FD_COST_TRACKER_ERROR_{*} is returned depending on what
     140             :    limit is violated.
     141             : 
     142             :    This function assumes that the caller is responsible for managing
     143             :    concurrent callers.
     144             : 
     145             :    This function represents the combination of Agave client functions:
     146             :    `CostModel::calculate_cost_for_executed_transaction()` and
     147             :    `CostTracker::try_add()`.
     148             : 
     149             :     https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L69-L95
     150             :     https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L163-L173 */
     151             : 
     152             : int
     153             : fd_cost_tracker_calculate_cost_and_add( fd_cost_tracker_t * cost_tracker,
     154             :                                         fd_bank_t *         bank,
     155             :                                         fd_txn_in_t const * txn_in,
     156             :                                         fd_txn_out_t *      txn_out );
     157             : 
     158             : FD_PROTOTYPES_END
     159             : 
     160             : #endif /* HEADER_fd_src_flamenco_runtime_fd_cost_tracker_h */

Generated by: LCOV version 1.14