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