LCOV - code coverage report
Current view: top level - flamenco/runtime/context - fd_exec_txn_ctx.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 20 0.0 %
Date: 2025-11-12 04:46:17 Functions: 0 104 0.0 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_flamenco_runtime_context_fd_exec_txn_ctx_h
       2             : #define HEADER_fd_src_flamenco_runtime_context_fd_exec_txn_ctx_h
       3             : 
       4             : #include "fd_exec_instr_ctx.h"
       5             : #include "../../log_collector/fd_log_collector_base.h"
       6             : #include "../../../ballet/txn/fd_txn.h"
       7             : #include "../../features/fd_features.h"
       8             : #include "../fd_txncache.h"
       9             : #include "../fd_bank_hash_cmp.h"
      10             : #include "../../progcache/fd_progcache_user.h"
      11             : #include "../fd_compute_budget_details.h"
      12             : #include "../../../disco/pack/fd_microblock.h"
      13             : #include "../../../disco/pack/fd_pack.h"
      14             : 
      15             : /* Return data for syscalls */
      16             : 
      17             : struct fd_txn_return_data {
      18             :   fd_pubkey_t program_id;
      19             :   ulong       len;
      20             :   uchar       data[1024];
      21             : };
      22             : 
      23             : typedef struct fd_txn_return_data fd_txn_return_data_t;
      24             : 
      25             : /* fd_exec_txn_ctx_t is the context needed to execute a transaction. */
      26             : 
      27             : /* An entry in the instruction trace */
      28             : struct fd_exec_instr_trace_entry {
      29             :   /* Metadata about the instruction */
      30             :   fd_instr_info_t * instr_info;
      31             :   /* Stack height when this instruction was pushed onto the stack (including itself)
      32             :      https://github.com/anza-xyz/agave/blob/d87e23d8d91c32d5f2be2bb3557c730bee1e9434/sdk/src/transaction_context.rs#L475-L480 */
      33             :   ulong stack_height;
      34             : };
      35             : typedef struct fd_exec_instr_trace_entry fd_exec_instr_trace_entry_t;
      36             : 
      37             : /* https://github.com/anza-xyz/agave/blob/0d34a1a160129c4293dac248e14231e9e773b4ce/program-runtime/src/compute_budget.rs#L139 */
      38             : #define FD_MAX_INSTRUCTION_TRACE_LENGTH (64UL)
      39             : /* https://github.com/anza-xyz/agave/blob/f70ab5598ccd86b216c3928e4397bf4a5b58d723/compute-budget/src/compute_budget.rs#L13 */
      40           0 : #define FD_MAX_INSTRUCTION_STACK_DEPTH  (5UL)
      41             : 
      42             : struct fd_exec_txn_ctx {
      43             :   ulong magic; /* ==FD_EXEC_TXN_CTX_MAGIC */
      44             : 
      45             :   /* TODO: These are fields borrowed from the slot and epoch ctx. This
      46             :      could be refactored even further. Currently these fields are not
      47             :      all valid local joins within the scope of txn execution. */
      48             : 
      49             :   uint flags;
      50             : 
      51             :   fd_bank_t * bank;
      52             : 
      53             :   fd_exec_stack_t *    exec_stack;
      54             :   fd_exec_accounts_t * exec_accounts;
      55             : 
      56             :   /* All pointers starting here are valid local joins in txn execution. */
      57             :   fd_features_t                        features;
      58             :   fd_txncache_t *                      status_cache;
      59             :   int                                  enable_exec_recording;
      60             :   fd_bank_hash_cmp_t *                 bank_hash_cmp;
      61             :   fd_funk_t                            funk[1];
      62             :   fd_progcache_t *                     progcache;
      63             :   fd_progcache_t                       _progcache[1];
      64             :   fd_funk_txn_xid_t                    xid[1];
      65             :   ulong                                slot;
      66             :   ulong                                bank_idx;
      67             :   fd_txn_p_t                           txn;
      68             : 
      69             :   fd_compute_budget_details_t          compute_budget_details;                      /* Compute budget details */
      70             : 
      71             :   /* Fields below here are not guaranteed to be local joins in txn execution. */
      72             : 
      73             :   ulong                                paid_fees;
      74             :   ulong                                loaded_accounts_data_size;                   /* The actual transaction loaded data size */
      75             :   ulong                                loaded_accounts_data_size_cost;              /* The cost of the loaded accounts data size in CUs */
      76             :   uint                                 custom_err;                                  /* When a custom error is returned, this is where the numeric value gets stashed */
      77             :   uchar                                instr_stack_sz;                              /* Current depth of the instruction execution stack. */
      78             :   fd_exec_instr_ctx_t                  instr_stack[FD_MAX_INSTRUCTION_STACK_DEPTH]; /* Instruction execution stack. */
      79             :   fd_exec_instr_ctx_t *                failed_instr;
      80             :   int                                  instr_err_idx;
      81             :   /* During sanitization, v0 transactions are allowed to have up to 256 accounts:
      82             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/sdk/program/src/message/versions/v0/mod.rs#L139
      83             :      Nonetheless, when Agave prepares a sanitized batch for execution and tries to lock accounts, a lower limit is enforced:
      84             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118
      85             :      That is the limit we are going to use here. */
      86             :   ulong                           accounts_cnt;                                /* Number of account pubkeys accessed by this transaction. */
      87             :   fd_pubkey_t                     account_keys[ MAX_TX_ACCOUNT_LOCKS ];        /* Array of account pubkeys accessed by this transaction. */
      88             :   ulong                           executable_cnt;                              /* Number of BPF upgradeable loader accounts. */
      89             :   fd_txn_account_t                executable_accounts[ MAX_TX_ACCOUNT_LOCKS ]; /* Array of BPF upgradeable loader program data accounts */
      90             :   fd_txn_account_t                accounts[ MAX_TX_ACCOUNT_LOCKS ];            /* Array of borrowed accounts accessed by this transaction. */
      91             : 
      92             :   /* When a program is deployed or upgraded, we must queue it to be
      93             :      updated in the program cache (if it exists already) so that
      94             :      the cache entry's ELF / sBPF information can be updated for future
      95             :      executions. We keep an array of pubkeys for the transaction to
      96             :      track which programs need to be reverified. The actual queueing
      97             :      for reverification is done in the transaction finalization step. */
      98             :   uchar                           programs_to_reverify_cnt;
      99             :   fd_pubkey_t                     programs_to_reverify[ MAX_TX_ACCOUNT_LOCKS ];
     100             : 
     101             :   /* The next three fields describe Agave's "rollback" accounts, which
     102             :      are copies of the fee payer and (if applicable) nonce account. If the
     103             :      transaction fails to load, the fee payer is still debited the transaction fee,
     104             :      and the nonce account is advanced. The fee payer must also be rolled back to it's
     105             :      state pre-transaction, plus debited any transaction fees.
     106             : 
     107             :      This is a bit of a misnomer but Agave calls it "rollback".
     108             :      This is the account state that the nonce account should be in when
     109             :      the txn fails.
     110             :      It will advance the nonce account, rather than "roll back".
     111             :    */
     112             :   fd_txn_account_t                rollback_nonce_account[ 1 ];
     113             :   ulong                           nonce_account_idx_in_txn;                    /* If the transaction has a nonce account that must be advanced, this would be !=ULONG_MAX. */
     114             :   fd_txn_account_t                rollback_fee_payer_account[ 1 ];
     115             : 
     116             :   uint                            num_instructions;                            /* Counter for number of instructions in txn */
     117             :   fd_txn_return_data_t            return_data;                                 /* Data returned from `return_data` syscalls */
     118             :   ulong                           accounts_resize_delta;                       /* Transaction level tracking for account resizing */
     119             :   fd_hash_t                       blake_txn_msg_hash;                          /* Hash of raw transaction message used by the status cache */
     120             :   ulong                           execution_fee;                               /* Execution fee paid by the fee payer in the transaction */
     121             :   ulong                           priority_fee;                                /* Priority fee paid by the fee payer in the transaction */
     122             : 
     123             :   fd_capture_ctx_t * capture_ctx;
     124             : 
     125             :   /* The instr_infos for the entire transaction are allocated at the start of
     126             :      the transaction. However, this must preserve a different counter because
     127             :      the top level instructions must get set up at once. The instruction
     128             :      error check on a maximum instruction size can be done on the
     129             :      instr_info_cnt instead of the instr_trace_length because it is a proxy
     130             :      for the trace_length: the instr_info_cnt gets incremented faster than
     131             :      the instr_trace_length because it counts all of the top level instructions
     132             :      first. */
     133             :   fd_instr_info_t             instr_infos[FD_MAX_INSTRUCTION_TRACE_LENGTH];
     134             :   ulong                       instr_info_cnt;
     135             : 
     136             :   /* These instr infos are statically allocated at the beginning of a transaction
     137             :      and are only written to / referred to within the VM. It's kept
     138             :      at the transaction level because syscalls like `GetProcessedSiblingInstruction()`
     139             :      may refer to instructions processed earlier in the transaction. */
     140             :   fd_instr_info_t             cpi_instr_infos[FD_MAX_INSTRUCTION_TRACE_LENGTH];
     141             :   ulong                       cpi_instr_info_cnt;
     142             : 
     143             :   /* Each instr info within `instr_trace` may refer to an `instr_infos` or `cpi_instr_infos`
     144             :      entry. */
     145             :   fd_exec_instr_trace_entry_t instr_trace[FD_MAX_INSTRUCTION_TRACE_LENGTH]; /* Instruction trace */
     146             :   ulong                       instr_trace_length;                           /* Number of instructions in the trace */
     147             : 
     148             :   fd_log_collector_t          log_collector;             /* Log collector instance */
     149             : 
     150             :   /* Execution error and type, to match Agave. */
     151             :   int exec_err;
     152             :   int exec_err_kind;
     153             : 
     154             :    /* The current instruction index being executed */
     155             :   int current_instr_idx;
     156             : 
     157             :   struct {
     158             :     int                 is_bundle;
     159             :     fd_exec_txn_ctx_t * prev_txn_ctxs[ FD_PACK_MAX_TXN_PER_BUNDLE ];
     160             :     ulong               prev_txn_ctxs_cnt;
     161             :   } bundle;
     162             : 
     163             :   /* debugging options */
     164             : 
     165             :   /* Pointer to buffer used for dumping instructions and transactions
     166             :      into protobuf files. */
     167             :   uchar * dumping_mem;
     168             : 
     169             :   /* Pointer to buffer used for tracing instructions and transactions
     170             :      into protobuf files. */
     171             :   int enable_vm_tracing;
     172             :   uchar * tracing_mem;
     173             : };
     174             : 
     175           0 : #define FD_EXEC_TXN_CTX_ALIGN     (alignof(fd_exec_txn_ctx_t))
     176           0 : #define FD_EXEC_TXN_CTX_FOOTPRINT ( sizeof(fd_exec_txn_ctx_t))
     177           0 : #define FD_EXEC_TXN_CTX_MAGIC     (0x9AD93EE71469F4D7UL      ) /* random */
     178             : 
     179             : FD_PROTOTYPES_BEGIN
     180             : 
     181             : /* Error logging handholding assertions */
     182             : 
     183             : #ifdef FD_RUNTIME_ERR_HANDHOLDING
     184             : 
     185             : /* Asserts that the error and error kind are not populated (zero) */
     186             : #define FD_TXN_TEST_ERR_OVERWRITE( txn_ctx ) \
     187             :    FD_TEST( !txn_ctx->exec_err );            \
     188             :    FD_TEST( !txn_ctx->exec_err_kind )
     189             : 
     190             : /* Used prior to a FD_TXN_ERR_FOR_LOG_INSTR call to deliberately
     191             :    bypass overwrite handholding checks.
     192             :    Only use this if you know what you're doing. */
     193             : #define FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx ) \
     194             :    txn_ctx->exec_err = 0;                          \
     195             :    txn_ctx->exec_err_kind = 0
     196             : 
     197             : #else
     198             : 
     199           0 : #define FD_TXN_TEST_ERR_OVERWRITE( txn_ctx ) ( ( void )0 )
     200           0 : #define FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx ) ( ( void )0 )
     201             : 
     202             : #endif
     203             : 
     204           0 : #define FD_TXN_ERR_FOR_LOG_INSTR( txn_ctx, err, idx ) (__extension__({ \
     205           0 :     FD_TXN_TEST_ERR_OVERWRITE( txn_ctx );                              \
     206           0 :     txn_ctx->exec_err = err;                                           \
     207           0 :     txn_ctx->exec_err_kind = FD_EXECUTOR_ERR_KIND_INSTR;               \
     208           0 :     txn_ctx->instr_err_idx = idx;                                      \
     209           0 :   }))
     210             : 
     211             : void *
     212             : fd_exec_txn_ctx_new( void * mem );
     213             : 
     214             : fd_exec_txn_ctx_t *
     215             : fd_exec_txn_ctx_join( void * mem );
     216             : 
     217             : void *
     218             : fd_exec_txn_ctx_leave( fd_exec_txn_ctx_t * ctx );
     219             : 
     220             : void *
     221             : fd_exec_txn_ctx_delete( void * mem );
     222             : 
     223             : /* Sets up a basic transaction ctx without a txn descriptor or txn raw. Useful
     224             :    for mocking transaction context objects for instructions. */
     225             : void
     226             : fd_exec_txn_ctx_setup_basic( fd_exec_txn_ctx_t * ctx );
     227             : 
     228             : void
     229             : fd_exec_txn_ctx_teardown( fd_exec_txn_ctx_t * txn_ctx );
     230             : 
     231             : /* Mirrors Agave function solana_sdk::transaction_context::find_index_of_account
     232             : 
     233             :    Backward scan over transaction accounts.
     234             :    Returns -1 if not found.
     235             : 
     236             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L233-L238 */
     237             : 
     238             : static inline int
     239             : fd_exec_txn_ctx_find_index_of_account( fd_exec_txn_ctx_t const * ctx,
     240           0 :                                        fd_pubkey_t const *       pubkey ) {
     241           0 :   for( ulong i=ctx->accounts_cnt; i>0UL; i-- ) {
     242           0 :     if( 0==memcmp( pubkey, &ctx->account_keys[ i-1UL ], sizeof(fd_pubkey_t) ) ) {
     243           0 :       return (int)(i-1UL);
     244           0 :     }
     245           0 :   }
     246           0 :   return -1;
     247           0 : }
     248             : 
     249             : typedef int fd_txn_account_condition_fn_t ( fd_txn_account_t *        acc,
     250             :                                             fd_exec_txn_ctx_t const * ctx,
     251             :                                             ushort                    idx );
     252             : 
     253             : /* Mirrors Agave function solana_sdk::transaction_context::get_account_at_index
     254             : 
     255             :    Takes a function pointer to a condition function to check pre-conditions on the
     256             :    obtained account. If the condition function is NULL, the account is returned without
     257             :    any pre-condition checks.
     258             : 
     259             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L223-L230 */
     260             : 
     261             : int
     262             : fd_exec_txn_ctx_get_account_at_index( fd_exec_txn_ctx_t *             ctx,
     263             :                                       ushort                          idx,
     264             :                                       fd_txn_account_t * *            account,
     265             :                                       fd_txn_account_condition_fn_t * condition );
     266             : 
     267             : /* A wrapper around fd_exec_txn_ctx_get_account_at_index that obtains an
     268             :    account from the transaction context by its pubkey. */
     269             : 
     270             : int
     271             : fd_exec_txn_ctx_get_account_with_key( fd_exec_txn_ctx_t *             ctx,
     272             :                                       fd_pubkey_t const *             pubkey,
     273             :                                       fd_txn_account_t * *            account,
     274             :                                       fd_txn_account_condition_fn_t * condition );
     275             : 
     276             : /* Gets an executable (program data) account via its pubkey. */
     277             : 
     278             : int
     279             : fd_exec_txn_ctx_get_executable_account( fd_exec_txn_ctx_t *             ctx,
     280             :                                         fd_pubkey_t const *             pubkey,
     281             :                                         fd_txn_account_t * *            account,
     282             :                                         fd_txn_account_condition_fn_t * condition );
     283             : 
     284             : /* Mirrors Agave function solana_sdk::transaction_context::get_key_of_account_at_index
     285             : 
     286             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L212 */
     287             : 
     288             : int
     289             : fd_exec_txn_ctx_get_key_of_account_at_index( fd_exec_txn_ctx_t *  ctx,
     290             :                                              ushort               idx,
     291             :                                              fd_pubkey_t const * * key );
     292             : 
     293             : void
     294             : fd_exec_txn_ctx_reset_return_data( fd_exec_txn_ctx_t * ctx );
     295             : 
     296             : /* In agave, the writable accounts cache is populated by this below function.
     297             :    This cache is then referenced to determine if a transaction account is
     298             :    writable or not.
     299             : 
     300             :    The overall logic is as follows: an account can be passed
     301             :    in as writable based on the signature and readonly signature as they are
     302             :    passed in by the transaction message. However, the account's writable
     303             :    status will be demoted if either of the two conditions are met:
     304             :    1. If the account is in the set of reserved pubkeys
     305             :    2. If the account is the program id AND the upgradeable loader account is in
     306             :       the set of transaction accounts. */
     307             : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L137-L150 */
     308             : 
     309             : int
     310             : fd_exec_txn_ctx_account_is_writable_idx( fd_exec_txn_ctx_t const * txn_ctx, ushort idx );
     311             : 
     312             : /* This flat function does the same as the function above, but uses the
     313             :    exact arguments needed instead of the full fd_exec_txn_ctx_t */
     314             : 
     315             : int
     316             : fd_exec_txn_account_is_writable_idx_flat( const ulong           slot,
     317             :                                           const ushort          idx,
     318             :                                           const fd_pubkey_t *   addr_at_idx,
     319             :                                           const fd_txn_t *      txn_descriptor,
     320             :                                           const fd_features_t * features,
     321             :                                           const uint            bpf_upgradeable_in_txn );
     322             : 
     323             : /* The bpf_upgradeable_in_txn argument of the above function can be
     324             :    obtained by the function below */
     325             : uint
     326             : fd_txn_account_has_bpf_loader_upgradeable( const fd_pubkey_t * account_keys,
     327             :                                            const ulong         accounts_cnt );
     328             : 
     329             : 
     330             : /* Account pre-condition filtering functions
     331             : 
     332             :    Used to filter accounts based on pre-conditions such as existence, is_writable, etc.
     333             :    when obtaining accounts from the transaction context. Passed as a function pointer. */
     334             : 
     335             : int
     336             : fd_txn_account_check_exists( fd_txn_account_t *        acc,
     337             :                              fd_exec_txn_ctx_t const * ctx,
     338             :                              ushort                    idx );
     339             : 
     340             : int
     341             : fd_txn_account_check_is_writable( fd_txn_account_t *        acc,
     342             :                                   fd_exec_txn_ctx_t const * ctx,
     343             :                                   ushort                    idx );
     344             : 
     345             : /* The fee payer is a valid modifiable account if it is passed in as writable
     346             :    in the message via a valid signature. We ignore if the account has been
     347             :    demoted or not (see fd_exec_txn_ctx_account_is_writable_idx) for more details.
     348             :    Agave and Firedancer will reject the fee payer if the transaction message
     349             :    doesn't have a writable signature. */
     350             : 
     351             : int
     352             : fd_txn_account_check_fee_payer_writable( fd_txn_account_t *        acc,
     353             :                                          fd_exec_txn_ctx_t const * ctx,
     354             :                                          ushort                    idx );
     355             : 
     356             : /* Checks if the account is mutable and borrows the account mutably.
     357             : 
     358             :    The borrow is an acquired write on the account object.
     359             :    The caller is responsible for releasing the write via
     360             :    fd_txn_account_release_write.
     361             : 
     362             :    TODO: Agave doesn't need to check if the account is mutable
     363             :    because it uses Writable/Readable traits for accounts. We
     364             :    should have a similar concept to abstract away fd_txn_account_t's
     365             :    const_meta and meta fields. */
     366             : 
     367             : int
     368             : fd_txn_account_check_borrow_mut( fd_txn_account_t *        acc,
     369             :                                  fd_exec_txn_ctx_t const * ctx,
     370             :                                  ushort                    idx );
     371             : 
     372             : 
     373             : FD_PROTOTYPES_END
     374             : 
     375             : #endif /* HEADER_fd_src_flamenco_runtime_context_fd_exec_txn_ctx_h */

Generated by: LCOV version 1.14