Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_fd_runtime_h 2 : #define HEADER_fd_src_flamenco_runtime_fd_runtime_h 3 : 4 : #include "fd_runtime_helpers.h" 5 : 6 : /* The general structure for executing transactions in Firedancer can 7 : be thought as a state maching where transaction execution is a 8 : deterministic state transition over various data structures. 9 : 10 : The starting and ending state before a transaction is executed is 11 : represented by the bank, accounts database, and status cache. The 12 : bank holds Solana state not represented by accounts (see fd_bank.c/h 13 : for more details) and each bank is per-slot. The latter two data 14 : structures are contained by the runtime. 15 : 16 : The runtime also owns valid joins to important data structures which 17 : are non-deterministically transitioned through execution such as the 18 : program cache which is a pure cache on top of the accounts database. 19 : The runtime also owns bounded out temporary memory regions used for 20 : transaction execution and valid joins to other scratch memory regions 21 : (e.g. acc_pool). 22 : 23 : So we expect the state of the runtime and the bank to change as a 24 : result of execution. 25 : 26 : The transaction, or the input to said state machine is represented by 27 : a fd_txn_in_t. The fd_txn_in_t is just a parsed transaction message 28 : and any state that may have accrued as a result of bundle execution. 29 : 30 : Executing a transaction produces a set of results. This is 31 : represented by a fd_txn_out_t. The fd_txn_out_t consists of any 32 : information that needs to be applied to the bank and runtime. 33 : 34 : We can execute a fd_txn_in_t against a given fd_runtime_t and a 35 : fd_bank_t and expect to produce a fd_txn_out_t. Then a fd_txn_out_t 36 : can be applied/committed on top of a fd_runtime_t and fd_bank_t. 37 : Execution is done via fd_runtime_prepare_and_execute_txn. If a 38 : transaction is committable, it should be committed via 39 : fd_runtime_commit_txn. If a transaction is not committable, it 40 : should be canceled via fd_runtime_cancel_txn. */ 41 : 42 : struct fd_runtime { 43 : fd_accdb_user_t * accdb; 44 : fd_txncache_t * status_cache; 45 : fd_progcache_t * progcache; 46 : fd_acc_pool_t * acc_pool; 47 : 48 : struct { 49 : uchar stack_sz; /* Current depth of the instruction execution stack. */ 50 : fd_exec_instr_ctx_t stack[ FD_MAX_INSTRUCTION_STACK_DEPTH ]; /* Instruction execution stack. */ 51 : /* The memory for all of the instructions in the transaction 52 : (including CPI instructions) are preallocated. However, the 53 : order in which the instructions are executed does not match the 54 : order in which they are allocated. The instr_trace will instead 55 : be used to track the order in which the instructions are 56 : executed. We add a +1 to allow any instructions past the max 57 : instr trace limit to be safely allocated, so that we can fail 58 : out like Agave does later at the stack push step within 59 : fd_execute_instr. 60 : 61 : The caller is responsible for updating the trace_length for the 62 : callee. For CPI, the trace length is updated when preparing a 63 : new instruction within cpi_common. For top-level instructions, 64 : the trace length is updated within fd_execute_txn when preparing 65 : an instruction for execution. */ 66 : fd_instr_info_t trace[ FD_MAX_INSTRUCTION_TRACE_LENGTH+1UL ]; 67 : ulong trace_length; 68 : /* The current instruction index being executed */ 69 : int current_idx; 70 : } instr; 71 : 72 : struct { 73 : /* The sysvar instructions account is a special account that is 74 : modified through the course of transaction execution, but its 75 : results are not committed to the bank or accounts database. */ 76 : uchar sysvar_instructions_mem[ FD_ACC_TOT_SZ_MAX ] __attribute__((aligned(FD_ACCOUNT_REC_ALIGN))); 77 : 78 : /* The executable accounts are derived from the accounts in the 79 : transaction and are used by the bpf loader program to validate 80 : the program data account. */ 81 : ulong executable_cnt; /* Number of BPF upgradeable loader accounts. */ 82 : fd_accdb_ro_t executable[ MAX_TX_ACCOUNT_LOCKS ]; /* Array of BPF upgradeable loader program data accounts */ 83 : 84 : ulong starting_lamports[ MAX_TX_ACCOUNT_LOCKS ]; /* Starting lamports for each account */ 85 : ulong starting_dlen[ MAX_TX_ACCOUNT_LOCKS ]; /* Starting data length for each account */ 86 : ulong refcnt[ MAX_TX_ACCOUNT_LOCKS ]; /* Reference count for each account */ 87 : } accounts; 88 : 89 : struct { 90 : int enable_log_collector; 91 : fd_log_collector_t * log_collector; /* Log collector instance */ 92 : fd_capture_ctx_t * capture_ctx; 93 : /* Pointer to buffer used for dumping instructions and transactions 94 : into protobuf files. */ 95 : uchar * dumping_mem; 96 : /* Pointer to buffer used for tracing instructions and transactions 97 : into protobuf files. */ 98 : int enable_vm_tracing; 99 : uchar * tracing_mem; 100 : } log; 101 : 102 : struct { 103 : uchar serialization_mem[ FD_MAX_INSTRUCTION_STACK_DEPTH ][ BPF_LOADER_SERIALIZATION_FOOTPRINT ] __attribute__((aligned(FD_RUNTIME_EBPF_HOST_ALIGN))); 104 : } bpf_loader_serialization; 105 : 106 : struct { 107 : uchar rodata [ FD_RUNTIME_ACC_SZ_MAX ] __attribute__((aligned(FD_SBPF_PROG_RODATA_ALIGN))); 108 : uchar sbpf_footprint[ FD_SBPF_PROGRAM_FOOTPRINT ] __attribute__((aligned(alignof(fd_sbpf_program_t)))); 109 : uchar programdata [ FD_RUNTIME_ACC_SZ_MAX ] __attribute__((aligned(FD_ACCOUNT_REC_ALIGN))); 110 : } bpf_loader_program; 111 : 112 : union { 113 : struct { 114 : uchar vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 115 : uchar authorized_voters_mem[ FD_AUTHORIZED_VOTERS_FOOTPRINT ] __attribute__((aligned(FD_AUTHORIZED_VOTERS_ALIGN))); 116 : uchar landed_votes_mem [ FD_LANDED_VOTES_FOOTPRINT ] __attribute__((aligned(FD_LANDED_VOTES_ALIGN))); 117 : uchar vote_lockout_mem [ FD_VOTE_LOCKOUTS_FOOTPRINT ] __attribute__((aligned(FD_VOTE_LOCKOUTS_ALIGN))); 118 : } authorize; 119 : 120 : struct { 121 : uchar vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 122 : uchar authorized_voters_mem[ FD_AUTHORIZED_VOTERS_FOOTPRINT ] __attribute__((aligned(FD_AUTHORIZED_VOTERS_ALIGN))); 123 : uchar landed_votes_mem [ FD_LANDED_VOTES_FOOTPRINT ] __attribute__((aligned(FD_LANDED_VOTES_ALIGN))); 124 : uchar vote_lockout_mem [ FD_VOTE_LOCKOUTS_FOOTPRINT ] __attribute__((aligned(FD_VOTE_LOCKOUTS_ALIGN))); 125 : } update_validator_identity; 126 : 127 : struct { 128 : uchar vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 129 : uchar authorized_voters_mem[ FD_AUTHORIZED_VOTERS_FOOTPRINT ] __attribute__((aligned(FD_AUTHORIZED_VOTERS_ALIGN))); 130 : uchar landed_votes_mem [ FD_LANDED_VOTES_FOOTPRINT ] __attribute__((aligned(FD_LANDED_VOTES_ALIGN))); 131 : uchar vote_lockout_mem [ FD_VOTE_LOCKOUTS_FOOTPRINT ] __attribute__((aligned(FD_VOTE_LOCKOUTS_ALIGN))); 132 : } update_commission; 133 : 134 : struct { 135 : uchar vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 136 : uchar authorized_voters_mem[ FD_AUTHORIZED_VOTERS_FOOTPRINT ] __attribute__((aligned(FD_AUTHORIZED_VOTERS_ALIGN))); 137 : uchar landed_votes_mem [ FD_LANDED_VOTES_FOOTPRINT ] __attribute__((aligned(FD_LANDED_VOTES_ALIGN))); 138 : uchar vote_lockout_mem [ FD_VOTE_LOCKOUTS_FOOTPRINT ] __attribute__((aligned(FD_VOTE_LOCKOUTS_ALIGN))); 139 : } withdraw; 140 : 141 : struct { 142 : uchar vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 143 : uchar authorized_voters_mem[ FD_AUTHORIZED_VOTERS_FOOTPRINT ] __attribute__((aligned(FD_AUTHORIZED_VOTERS_ALIGN))); 144 : uchar vote_lockout_mem [ FD_VOTE_LOCKOUTS_FOOTPRINT ] __attribute__((aligned(FD_VOTE_LOCKOUTS_ALIGN))); 145 : } init_account; 146 : 147 : struct { 148 : uchar vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 149 : uchar authorized_voters_mem [ FD_AUTHORIZED_VOTERS_FOOTPRINT ] __attribute__((aligned(FD_AUTHORIZED_VOTERS_ALIGN))); 150 : uchar vote_state_landed_votes_mem[ FD_LANDED_VOTES_FOOTPRINT ] __attribute__((aligned(FD_LANDED_VOTES_ALIGN))); 151 : uchar tower_sync_landed_votes_mem[ FD_LANDED_VOTES_FOOTPRINT ] __attribute__((aligned(FD_LANDED_VOTES_ALIGN))); 152 : uchar vote_lockout_mem [ FD_VOTE_LOCKOUTS_FOOTPRINT ] __attribute__((aligned(FD_VOTE_LOCKOUTS_ALIGN))); 153 : } tower_sync; 154 : 155 : struct { 156 : /* Deprecated instructions */ 157 : uchar vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 158 : uchar authorized_voters_mem [ FD_AUTHORIZED_VOTERS_FOOTPRINT ] __attribute__((aligned(FD_AUTHORIZED_VOTERS_ALIGN))); 159 : uchar landed_votes_mem [ FD_LANDED_VOTES_FOOTPRINT ] __attribute__((aligned(FD_LANDED_VOTES_ALIGN))); 160 : uchar vote_lockout_mem [ FD_VOTE_LOCKOUTS_FOOTPRINT ] __attribute__((aligned(FD_VOTE_LOCKOUTS_ALIGN))); 161 : uchar compact_vs_lockout_mem [ FD_VOTE_LOCKOUTS_FOOTPRINT ] __attribute__((aligned(FD_VOTE_LOCKOUTS_ALIGN))); 162 : uchar vs_update_landed_votes_mem[ FD_LANDED_VOTES_FOOTPRINT ] __attribute__((aligned(FD_LANDED_VOTES_ALIGN))); 163 : } process_vote; 164 : 165 : } vote_program; 166 : 167 : union { 168 : struct { 169 : uchar vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 170 : uchar authorized_voters_mem[ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(128UL))); 171 : uchar landed_votes_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(128UL))); 172 : } delegate; 173 : struct { 174 : uchar delinquent_vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 175 : uchar delinquent_authorized_voters_mem[ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(128UL))); 176 : uchar delinquent_landed_votes_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(128UL))); 177 : 178 : uchar reference_vote_state_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(FD_VOTE_STATE_VERSIONED_ALIGN))); 179 : uchar reference_authorized_voters_mem[ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(128UL))); 180 : uchar reference_landed_votes_mem [ FD_VOTE_STATE_VERSIONED_FOOTPRINT ] __attribute__((aligned(128UL))); 181 : } deactivate_delinquent; 182 : } stake_program; 183 : 184 : struct { 185 : 186 : /* Ticks spent spent preparing a txn-level VM (zeroing memory, 187 : copying account data, etc) */ 188 : ulong vm_setup_cum_ticks; 189 : 190 : /* Ticks spent committing txn-level VM results (copying account 191 : data, etc) */ 192 : ulong vm_commit_cum_ticks; 193 : 194 : /* Ticks spent in top-levl VM interpreter (includes CPI setup/commit 195 : ticks) */ 196 : ulong vm_exec_cum_ticks; 197 : 198 : /* Ticks spent preparing/committing a cross-program invocation) */ 199 : ulong cpi_setup_cum_ticks; 200 : ulong cpi_commit_cum_ticks; 201 : 202 : /* Number of user txn account transitions */ 203 : 204 : # define FD_RUNTIME_SAVE_UNCHANGED_NONEXIST 0 /* non-existent account not modified */ 205 : # define FD_RUNTIME_SAVE_CREATE 1 /* account previously non-existent, non-zero balance after txn */ 206 : # define FD_RUNTIME_SAVE_DELETE 2 /* account previously existed, non-existent after txn */ 207 0 : # define FD_RUNTIME_SAVE_MODIFY 3 /* existing account modified */ 208 0 : # define FD_RUNTIME_SAVE_UNCHANGED 4 /* existing account not modified */ 209 : # define FD_RUNTIME_SAVE_MAX 5 /* enum variant count */ 210 : ulong txn_account_save[ FD_RUNTIME_SAVE_MAX ]; 211 : 212 : } metrics; 213 : }; 214 : typedef struct fd_runtime fd_runtime_t; 215 : 216 : struct fd_txn_in { 217 : fd_txn_p_t const * txn; 218 : 219 : struct { 220 : int is_bundle; 221 : fd_txn_out_t * prev_txn_outs[ FD_PACK_MAX_TXN_PER_BUNDLE ]; 222 : ulong prev_txn_cnt; 223 : } bundle; 224 : }; 225 : typedef struct fd_txn_in fd_txn_in_t; 226 : 227 : struct fd_txn_out { 228 : struct { 229 : int is_committable; 230 : int is_fees_only; 231 : int txn_err; 232 : /* These are error fields produced by instruction execution 233 : when txn_err == FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR (-9). */ 234 : int exec_err; 235 : int exec_err_kind; 236 : int exec_err_idx; 237 : uint custom_err; 238 : } err; 239 : 240 : struct { 241 : long prep_start_timestamp; 242 : long load_start_timestamp; 243 : long exec_start_timestamp; 244 : long commit_start_timestamp; 245 : 246 : fd_compute_budget_details_t compute_budget; /* Compute budget details */ 247 : fd_transaction_cost_t txn_cost; /* Transaction cost */ 248 : ulong loaded_accounts_data_size; /* The actual transaction loaded data size */ 249 : ulong accounts_resize_delta; /* Transaction level tracking for account resizing */ 250 : 251 : fd_txn_return_data_t return_data; /* Data returned from `return_data` syscalls */ 252 : 253 : fd_hash_t blake_txn_msg_hash; /* Hash of raw transaction message used by the status cache */ 254 : fd_hash_t blockhash; /* Blockhash of the block that the transaction is being executed in */ 255 : 256 : ulong execution_fee; /* Execution fee paid by the fee payer in the transaction */ 257 : ulong priority_fee; /* Priority fee paid by the fee payer in the transaction */ 258 : ulong tips; /* Jito tips paid during execution */ 259 : 260 : ulong signature_count; /* Number of signatures in the transaction */ 261 : int is_simple_vote; /* Whether the transaction is a simple vote */ 262 : /* When a program is deployed or upgraded, we must queue it to be 263 : updated in the program cache (if it exists already) so that 264 : the cache entry's ELF / sBPF information can be updated for 265 : future executions. We keep an array of pubkeys for the 266 : transaction to track which programs need to be reverified. The 267 : actual queueing for reverification is done in the transaction 268 : finalization step. */ 269 : uchar programs_to_reverify_cnt; 270 : fd_pubkey_t programs_to_reverify[ MAX_TX_ACCOUNT_LOCKS ]; 271 : } details; 272 : 273 : /* During sanitization, v0 transactions are allowed to have up to 256 accounts: 274 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/sdk/program/src/message/versions/v0/mod.rs#L139 275 : Nonetheless, when Agave prepares a sanitized batch for execution and tries to lock accounts, a lower limit is enforced: 276 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 277 : That is the limit we are going to use here. */ 278 : struct { 279 : /* is_setup is set to 1 if account data buffer resources have been 280 : acquired for the transaction and 0 if they have not. If the flag 281 : has been set, memory resources must be released. */ 282 : int is_setup; 283 : ulong cnt; 284 : fd_pubkey_t keys [ MAX_TX_ACCOUNT_LOCKS ]; 285 : fd_accdb_rw_t account [ MAX_TX_ACCOUNT_LOCKS ]; /* FIXME use accdb_ref_t here for safety - some accounts are readonly */ 286 : uchar is_writable[ MAX_TX_ACCOUNT_LOCKS ]; 287 : 288 : /* The fee payer and nonce accounts are treated differently than 289 : other accounts: if an on-transaction fails they are still 290 : committed to the accounts database. However, they are saved at 291 : the point right after a fee is debited or the nonce is advanced 292 : respectively. The rollback accounts store this state because a 293 : failed transaction could have potentially modified the state of 294 : these two accounts. 295 : 296 : The memory for the nonce and fee payer is always provisioned when 297 : the transaction is prepared, but isn't necessarily used. */ 298 : uchar * rollback_fee_payer_mem; 299 : uchar * rollback_nonce_mem; 300 : 301 : ulong nonce_idx_in_txn; /* !=ULONG_MAX if exists */ 302 : fd_account_meta_t * rollback_nonce; 303 : fd_account_meta_t * rollback_fee_payer; 304 : } accounts; 305 : }; 306 : typedef struct fd_txn_out fd_txn_out_t; 307 : 308 : FD_PROTOTYPES_BEGIN 309 : 310 : /* fd_runtime_block_execute_prepare kicks off the execution of a block. 311 : After this function is called, transactions can be executed and 312 : committed against the block. This function handles epoch boundary 313 : and rewards updates if needed and updates sysvars. It assumes that 314 : the bank and accounts database have been setup to execute against 315 : the bank: the bank has already been cloned from the parent bank and 316 : that the database has a transaction that is linked to the parent 317 : block's xid. */ 318 : 319 : void 320 : fd_runtime_block_execute_prepare( fd_banks_t * banks, 321 : fd_bank_t * bank, 322 : fd_accdb_user_t * accdb, 323 : fd_runtime_stack_t * runtime_stack, 324 : fd_capture_ctx_t * capture_ctx, 325 : int * is_epoch_boundary ); 326 : 327 : /* fd_runtime_block_execute_finalize finishes the execution of the block 328 : by paying a fee out to the block leader, updating any sysvars, and 329 : updating the bank hash. The required updates are made to the bank 330 : and the accounts database. */ 331 : 332 : void 333 : fd_runtime_block_execute_finalize( fd_bank_t * bank, 334 : fd_accdb_user_t * accdb, 335 : fd_capture_ctx_t * capture_ctx ); 336 : 337 : /* fd_runtime_prepare_and_execute_txn is responsible for executing a 338 : fd_txn_in_t against a fd_runtime_t and a fd_bank_t. The results of 339 : the transaction execution are set in the fd_txn_out_t. The caller 340 : is responisble for correctly setting up the fd_txn_in_t and the 341 : fd_runtime_t handles. 342 : 343 : TODO: fd_runtime_t and fd_bank_t should be const here. */ 344 : 345 : void 346 : fd_runtime_prepare_and_execute_txn( fd_runtime_t * runtime, 347 : fd_bank_t * bank, 348 : fd_txn_in_t const * txn_in, 349 : fd_txn_out_t * txn_out ); 350 : 351 : /* fd_runtime_commit_txn commits the results of a transaction execution 352 : as represented by the fd_txn_out_t to the bank and the accounts 353 : database. */ 354 : 355 : void 356 : fd_runtime_commit_txn( fd_runtime_t * runtime, 357 : fd_bank_t * bank, 358 : fd_txn_out_t * txn_out ); 359 : 360 : /* fd_runtime_cancel_txn cancels the result of a transaction execution 361 : and frees any resources that may have been acquired. A transaction 362 : should only be canceled when the transaction is not committable. 363 : 1. An invalid transaction that causes a block to be rejected/ 364 : considered invalid/'bad'. 365 : 2. All transactions in a bundle with a failed transaction should be 366 : canceled as they will not be included in the block. */ 367 : 368 : void 369 : fd_runtime_cancel_txn( fd_runtime_t * runtime, 370 : fd_txn_out_t * txn_out ); 371 : 372 : FD_PROTOTYPES_END 373 : 374 : #endif /* HEADER_fd_src_flamenco_runtime_fd_runtime_h */