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_runtime_err.h" 11 : #include "fd_runtime_const.h" 12 : #include "../../disco/pack/fd_pack_cost.h" 13 : 14 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L62-L79 */ 15 : 16 300 : #define FD_WRITE_LOCK_UNITS ( 300U) /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/block_cost_limits.rs#L20 */ 17 4557 : #define FD_MAX_VOTE_UNITS ( 36000000U) /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/block_cost_limits.rs#L38 */ 18 150 : #define FD_SIMPLE_VOTE_USAGE_COST ( 3428U) /* https://github.com/anza-xyz/agave/blob/v3.1.8/cost-model/src/transaction_cost.rs#L21 */ 19 : 20 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L18-L33 */ 21 420 : #define FD_COST_TRACKER_SUCCESS (0) 22 0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_BLOCK_MAX_LIMIT (1) 23 0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_VOTE_MAX_LIMIT (2) 24 0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_MAX_LIMIT (3) 25 0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT (4) 26 0 : #define FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_TOTAL_LIMIT (5) 27 : 28 : /* A reasonably tight bound of the number of writable accounts per slot 29 : can be derived from worst-case CU limits. For the cost tracker, 30 : there are two main codepaths: 31 : - Simple vote transactions: legacy transactions that invoke the vote 32 : program. These can have unused writable accounts. 33 : - Everything else: these transactions are charged based on CUs per 34 : signature and write lock. 35 : 36 : For the simple vote transactions, the worst-case transaction has 35 37 : writable accounts. These transactions can not use ALTs and are 38 : subject to fitting within the transaction size limit (1232B). 39 : 40 : 36000000 vote CUs per slot 41 : 3428 CUs per simple vote transaction 42 : 35 writable accounts per simple vote transaction 43 : Therefore, the number of simple vote transactions per slot is: 44 : (36000000 / 3428) = 10501 vote txns per slot 45 : Therefore, the number of writable accounts per slot is: 46 : (10501 * 35) = 367535 writable accounts per slot 47 : 48 : Now consider the general case. This means we should try to pack as 49 : many writable accounts as possible into each transaction. Each 50 : transaction requires at least one signature. We will assume that all 51 : of these accounts have no account data. 52 : 53 : 64 - Max number of accounts per transaction. In this case we will 54 : assume that all of these accounts are writable and have no data. 55 : 100000000 - CUs per slot 56 : 720 - Cost of a signature 57 : 300 - Cost of a writable account write lock 58 : 59 : We can have (100000000 / (720 + 64 * 300)) = 5020 transactions per 60 : slot with maximum writable account utilization. 61 : 62 : So, 5020 transactions per slot * 64 accounts per transaction = 63 : 321280 writable accounts per slot. 64 : 65 : We should use the bound derived from simple vote transactions. 66 : 67 : TODO: After remove_simple_vote_from_cost_model is activated, the 68 : smaller bound can be used. */ 69 : 70 0 : #define FD_RUNTIME_MAX_VOTE_TXNS_IN_SLOT (FD_MAX_VOTE_UNITS / FD_SIMPLE_VOTE_USAGE_COST) 71 0 : #define FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_VOTE_TXNS (35UL) /* see FD_TXN_ACCT_ADDR_MAX */ 72 0 : #define FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT (FD_RUNTIME_MAX_VOTE_TXNS_IN_SLOT * FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_VOTE_TXNS) 73 : FD_STATIC_ASSERT( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT==367535UL, "Incorrect FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT" ); 74 : 75 : /* TODO: Extremely gross. Used because these are in a pool which needs 76 : to be compile time sized T. */ 77 : 78 : /* The average mainnet block uses around 4200 distinct writable 79 : accounts. We size the cost tracker's account map at 8192 chains 80 : to give ~2x headroom over the observed average load. */ 81 822 : #define FD_COST_TRACKER_CHAIN_CNT_EST (8192UL) 82 : #define FD_COST_TRACKER_FOOTPRINT \ 83 : ( FD_LAYOUT_FINI( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( \ 84 : FD_LAYOUT_INIT, \ 85 : 128UL /* alignof(fd_cost_tracker_t) */, 128UL /* sizeof(fd_cost_tracker_t) */ ), \ 86 : 128UL /* alignof(cost_tracker_out_t )*/, 128UL /* sizeof(cost_tracker_out_t ) */ ), \ 87 : 8UL /* alignof(account_cost_map_t) */, FD_COST_TRACKER_CHAIN_CNT_EST*4UL /*sizeof(uint)*/ +24UL /* sizeof(account_cost_map_t) */ ), \ 88 : 4UL /* alignof(account_cost_t) */, FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT*40UL /*sizeof(account_cost_t)*/ ), \ 89 : 128UL ) ) \ 90 : 91 813 : #define FD_COST_TRACKER_MAGIC (0xF17EDA2CE7C05170UL) /* FIREDANCER COST V0 */ 92 : 93 2487 : #define FD_COST_TRACKER_ALIGN (128UL) 94 : 95 : struct __attribute__((aligned(FD_COST_TRACKER_ALIGN))) fd_cost_tracker { 96 : ulong block_cost; 97 : ulong vote_cost; 98 : ulong allocated_accounts_data_size; 99 : 100 : ulong block_cost_limit; 101 : ulong vote_cost_limit; 102 : ulong account_cost_limit; 103 : ulong data_size_limit; 104 : 105 : int larger_max_cost_per_block; 106 : int remove_simple_vote_from_cost_model; 107 : }; 108 : 109 : typedef struct fd_cost_tracker fd_cost_tracker_t; 110 : 111 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/transaction_cost.rs#L153-L161 */ 112 : 113 : struct fd_usage_cost_details { 114 : uint signature_cost; 115 : uint write_lock_cost; 116 : uint data_bytes_cost; 117 : uint programs_execution_cost; 118 : uint loaded_accounts_data_size_cost; 119 : ulong allocated_accounts_data_size; 120 : }; 121 : typedef struct fd_usage_cost_details fd_usage_cost_details_t; 122 : 123 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/transaction_cost.rs#L20-L23 */ 124 : 125 : struct fd_transaction_cost { 126 : uint type; /* FD_TXN_COST_TYPE_* */ 127 : union { 128 : fd_usage_cost_details_t transaction; 129 : }; 130 : }; 131 : 132 : typedef struct fd_transaction_cost fd_transaction_cost_t; 133 : 134 384 : #define FD_TXN_COST_TYPE_SIMPLE_VOTE 0U 135 840 : #define FD_TXN_COST_TYPE_TRANSACTION 1U 136 : 137 : FD_PROTOTYPES_BEGIN 138 : 139 : static inline int 140 0 : fd_cost_tracker_err_to_runtime_err( int err ) { 141 0 : switch( err ) { 142 0 : case FD_COST_TRACKER_SUCCESS: 143 0 : return FD_RUNTIME_EXECUTE_SUCCESS; 144 0 : case FD_COST_TRACKER_ERROR_WOULD_EXCEED_BLOCK_MAX_LIMIT: 145 0 : return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_BLOCK_COST_LIMIT; 146 0 : case FD_COST_TRACKER_ERROR_WOULD_EXCEED_VOTE_MAX_LIMIT: 147 0 : return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_VOTE_COST_LIMIT; 148 0 : case FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_MAX_LIMIT: 149 0 : return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_ACCOUNT_COST_LIMIT; 150 0 : case FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT: 151 0 : return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT; 152 0 : case FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_TOTAL_LIMIT: 153 0 : return FD_RUNTIME_TXN_ERR_WOULD_EXCEED_ACCOUNT_DATA_TOTAL_LIMIT; 154 0 : default: 155 0 : FD_LOG_CRIT(( "unexpected cost tracker error %d", err )); 156 0 : } 157 0 : } 158 : 159 : FD_FN_CONST ulong 160 : fd_cost_tracker_align( void ); 161 : 162 : FD_FN_CONST ulong 163 : fd_cost_tracker_footprint( void ); 164 : 165 : void * 166 : fd_cost_tracker_new( void * shmem, 167 : int larger_max_cost_per_block, 168 : ulong seed ); 169 : 170 : fd_cost_tracker_t * 171 : fd_cost_tracker_join( void * shct ); 172 : 173 : void 174 : fd_cost_tracker_init( fd_cost_tracker_t * cost_tracker, 175 : fd_features_t const * features, 176 : fd_slot_params_t const * slot_params, 177 : ulong slot ); 178 : 179 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L323-L328 */ 180 : FD_FN_PURE uint 181 : fd_cost_tracker_calculate_loaded_accounts_data_size_cost( fd_txn_out_t const * txn_out ); 182 : 183 : /* fd_cost_tracker_calculate_cost_and_add takes a transaction, 184 : calculates the cost of the transaction in terms of various block 185 : level limits and adds it to the cost tracker. If the incremental 186 : transaction fits in the block, then the cost tracking is updated and 187 : FD_COST_TRACKER_SUCCESS is returned. If the transaction does not 188 : fit then FD_COST_TRACKER_ERROR_{*} is returned depending on what 189 : limit is violated. 190 : 191 : This function assumes that the caller is responsible for managing 192 : concurrent callers. 193 : 194 : This function represents the Agave client function: 195 : `CostModel::calculate_cost_for_executed_transaction()` 196 : 197 : https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L69-L95 198 : */ 199 : 200 : void 201 : fd_cost_tracker_calculate_cost( fd_bank_t * bank, 202 : fd_txn_in_t const * txn_in, 203 : fd_txn_out_t * txn_out ); 204 : 205 : /* fd_cost_tracker_try_add_cost takes the cost as calculated by 206 : fd_cost_tracker_calculate_cost and tries to accumulate it into the 207 : cost tracker. If the cost fits, the cost is added and 208 : FD_COST_TRACKER_SUCCESS is returned. If the cost does not fit, the 209 : cost will not be updated and FD_COST_TRACKER_ERROR_* is returned 210 : depending on what limit is violated. 211 : 212 : This function is the Agave client function: 'CostTracker::try_add()' 213 : https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L163-L173 */ 214 : 215 : int 216 : fd_cost_tracker_try_add_cost( fd_cost_tracker_t * cost_tracker, 217 : fd_txn_out_t * txn_out ); 218 : 219 : FD_PROTOTYPES_END 220 : 221 : #endif /* HEADER_fd_src_flamenco_runtime_fd_cost_tracker_h */