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: 2 20 10.0 %
Date: 2025-10-17 04:46:03 Functions: 0 105 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 "../../../funk/fd_funk.h"
      11             : #include "../../progcache/fd_progcache_user.h"
      12             : #include "../fd_compute_budget_details.h"
      13             : #include "../../../disco/pack/fd_microblock.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          48 : #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             :   /* All pointers starting here are valid local joins in txn execution. */
      54             :   fd_features_t                        features;
      55             :   fd_txncache_t *                      status_cache;
      56             :   int                                  enable_exec_recording;
      57             :   fd_bank_hash_cmp_t *                 bank_hash_cmp;
      58             :   fd_funk_t                            funk[1];
      59             :   fd_progcache_t *                     progcache;
      60             :   fd_progcache_t                       _progcache[1];
      61             :   fd_funk_txn_xid_t                    xid[1];
      62             :   ulong                                slot;
      63             :   ulong                                bank_idx;
      64             :   fd_txn_p_t                           txn;
      65             : 
      66             :   fd_spad_t *                          spad;                                        /* Sized out to handle the worst case footprint of single transaction execution. */
      67             :   fd_wksp_t *                          spad_wksp;                                   /* Workspace for the spad. */
      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             :   ulong                           collected_rent;                              /* Rent collected from accounts in this transaction */
     123             : 
     124             :   fd_capture_ctx_t * capture_ctx;
     125             : 
     126             :   /* The instr_infos for the entire transaction are allocated at the start of
     127             :      the transaction. However, this must preserve a different counter because
     128             :      the top level instructions must get set up at once. The instruction
     129             :      error check on a maximum instruction size can be done on the
     130             :      instr_info_cnt instead of the instr_trace_length because it is a proxy
     131             :      for the trace_length: the instr_info_cnt gets incremented faster than
     132             :      the instr_trace_length because it counts all of the top level instructions
     133             :      first. */
     134             :   fd_instr_info_t             instr_infos[FD_MAX_INSTRUCTION_TRACE_LENGTH];
     135             :   ulong                       instr_info_cnt;
     136             : 
     137             :   /* These instr infos are statically allocated at the beginning of a transaction
     138             :      and are only written to / referred to within the VM. It's kept
     139             :      at the transaction level because syscalls like `GetProcessedSiblingInstruction()`
     140             :      may refer to instructions processed earlier in the transaction. */
     141             :   fd_instr_info_t             cpi_instr_infos[FD_MAX_INSTRUCTION_TRACE_LENGTH];
     142             :   ulong                       cpi_instr_info_cnt;
     143             : 
     144             :   /* Each instr info within `instr_trace` may refer to an `instr_infos` or `cpi_instr_infos`
     145             :      entry. */
     146             :   fd_exec_instr_trace_entry_t instr_trace[FD_MAX_INSTRUCTION_TRACE_LENGTH]; /* Instruction trace */
     147             :   ulong                       instr_trace_length;                           /* Number of instructions in the trace */
     148             : 
     149             :   fd_log_collector_t          log_collector;             /* Log collector instance */
     150             : 
     151             :   /* Execution error and type, to match Agave. */
     152             :   int exec_err;
     153             :   int exec_err_kind;
     154             : 
     155             :    /* The current instruction index being executed */
     156             :   int current_instr_idx;
     157             : 
     158             :   /* fuzzing options */
     159             :   struct {
     160             :     int enable_vm_tracing;
     161             :   } fuzz_config;
     162             : };
     163             : 
     164           0 : #define FD_EXEC_TXN_CTX_ALIGN     (alignof(fd_exec_txn_ctx_t))
     165          24 : #define FD_EXEC_TXN_CTX_FOOTPRINT ( sizeof(fd_exec_txn_ctx_t))
     166           0 : #define FD_EXEC_TXN_CTX_MAGIC     (0x9AD93EE71469F4D7UL      ) /* random */
     167             : 
     168             : FD_PROTOTYPES_BEGIN
     169             : 
     170             : /* Error logging handholding assertions */
     171             : 
     172             : #ifdef FD_RUNTIME_ERR_HANDHOLDING
     173             : 
     174             : /* Asserts that the error and error kind are not populated (zero) */
     175             : #define FD_TXN_TEST_ERR_OVERWRITE( txn_ctx ) \
     176             :    FD_TEST( !txn_ctx->exec_err );            \
     177             :    FD_TEST( !txn_ctx->exec_err_kind )
     178             : 
     179             : /* Used prior to a FD_TXN_ERR_FOR_LOG_INSTR call to deliberately
     180             :    bypass overwrite handholding checks.
     181             :    Only use this if you know what you're doing. */
     182             : #define FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx ) \
     183             :    txn_ctx->exec_err = 0;                          \
     184             :    txn_ctx->exec_err_kind = 0
     185             : 
     186             : #else
     187             : 
     188           0 : #define FD_TXN_TEST_ERR_OVERWRITE( txn_ctx ) ( ( void )0 )
     189           0 : #define FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx ) ( ( void )0 )
     190             : 
     191             : #endif
     192             : 
     193           0 : #define FD_TXN_ERR_FOR_LOG_INSTR( txn_ctx, err, idx ) (__extension__({ \
     194           0 :     FD_TXN_TEST_ERR_OVERWRITE( txn_ctx );                              \
     195           0 :     txn_ctx->exec_err = err;                                           \
     196           0 :     txn_ctx->exec_err_kind = FD_EXECUTOR_ERR_KIND_INSTR;               \
     197           0 :     txn_ctx->instr_err_idx = idx;                                      \
     198           0 :   }))
     199             : 
     200             : void *
     201             : fd_exec_txn_ctx_new( void * mem );
     202             : 
     203             : fd_exec_txn_ctx_t *
     204             : fd_exec_txn_ctx_join( void * mem, fd_spad_t * spad, fd_wksp_t * spad_wksp );
     205             : 
     206             : void *
     207             : fd_exec_txn_ctx_leave( fd_exec_txn_ctx_t * ctx );
     208             : 
     209             : void *
     210             : fd_exec_txn_ctx_delete( void * mem );
     211             : 
     212             : /* Sets up a basic transaction ctx without a txn descriptor or txn raw. Useful
     213             :    for mocking transaction context objects for instructions. */
     214             : void
     215             : fd_exec_txn_ctx_setup_basic( fd_exec_txn_ctx_t * ctx );
     216             : 
     217             : void
     218             : fd_exec_txn_ctx_teardown( fd_exec_txn_ctx_t * txn_ctx );
     219             : 
     220             : /* Mirrors Agave function solana_sdk::transaction_context::find_index_of_account
     221             : 
     222             :    Backward scan over transaction accounts.
     223             :    Returns -1 if not found.
     224             : 
     225             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L233-L238 */
     226             : 
     227             : static inline int
     228             : fd_exec_txn_ctx_find_index_of_account( fd_exec_txn_ctx_t const * ctx,
     229           0 :                                        fd_pubkey_t const *       pubkey ) {
     230           0 :   for( ulong i=ctx->accounts_cnt; i>0UL; i-- ) {
     231           0 :     if( 0==memcmp( pubkey, &ctx->account_keys[ i-1UL ], sizeof(fd_pubkey_t) ) ) {
     232           0 :       return (int)(i-1UL);
     233           0 :     }
     234           0 :   }
     235           0 :   return -1;
     236           0 : }
     237             : 
     238             : typedef int fd_txn_account_condition_fn_t ( fd_txn_account_t *        acc,
     239             :                                             fd_exec_txn_ctx_t const * ctx,
     240             :                                             ushort                    idx );
     241             : 
     242             : /* Mirrors Agave function solana_sdk::transaction_context::get_account_at_index
     243             : 
     244             :    Takes a function pointer to a condition function to check pre-conditions on the
     245             :    obtained account. If the condition function is NULL, the account is returned without
     246             :    any pre-condition checks.
     247             : 
     248             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L223-L230 */
     249             : 
     250             : int
     251             : fd_exec_txn_ctx_get_account_at_index( fd_exec_txn_ctx_t *             ctx,
     252             :                                       ushort                          idx,
     253             :                                       fd_txn_account_t * *            account,
     254             :                                       fd_txn_account_condition_fn_t * condition );
     255             : 
     256             : /* A wrapper around fd_exec_txn_ctx_get_account_at_index that obtains an
     257             :    account from the transaction context by its pubkey. */
     258             : 
     259             : int
     260             : fd_exec_txn_ctx_get_account_with_key( fd_exec_txn_ctx_t *             ctx,
     261             :                                       fd_pubkey_t const *             pubkey,
     262             :                                       fd_txn_account_t * *            account,
     263             :                                       fd_txn_account_condition_fn_t * condition );
     264             : 
     265             : /* Gets an executable (program data) account via its pubkey. */
     266             : 
     267             : int
     268             : fd_exec_txn_ctx_get_executable_account( fd_exec_txn_ctx_t *             ctx,
     269             :                                         fd_pubkey_t const *             pubkey,
     270             :                                         fd_txn_account_t * *            account,
     271             :                                         fd_txn_account_condition_fn_t * condition );
     272             : 
     273             : /* Mirrors Agave function solana_sdk::transaction_context::get_key_of_account_at_index
     274             : 
     275             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L212 */
     276             : 
     277             : int
     278             : fd_exec_txn_ctx_get_key_of_account_at_index( fd_exec_txn_ctx_t *  ctx,
     279             :                                              ushort               idx,
     280             :                                              fd_pubkey_t const * * key );
     281             : 
     282             : void
     283             : fd_exec_txn_ctx_reset_return_data( fd_exec_txn_ctx_t * ctx );
     284             : 
     285             : /* In agave, the writable accounts cache is populated by this below function.
     286             :    This cache is then referenced to determine if a transaction account is
     287             :    writable or not.
     288             : 
     289             :    The overall logic is as follows: an account can be passed
     290             :    in as writable based on the signature and readonly signature as they are
     291             :    passed in by the transaction message. However, the account's writable
     292             :    status will be demoted if either of the two conditions are met:
     293             :    1. If the account is in the set of reserved pubkeys
     294             :    2. If the account is the program id AND the upgradeable loader account is in
     295             :       the set of transaction accounts. */
     296             : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L137-L150 */
     297             : 
     298             : int
     299             : fd_exec_txn_ctx_account_is_writable_idx( fd_exec_txn_ctx_t const * txn_ctx, ushort idx );
     300             : 
     301             : /* This flat function does the same as the function above, but uses the
     302             :    exact arguments needed instead of the full fd_exec_txn_ctx_t */
     303             : 
     304             : int
     305             : fd_exec_txn_account_is_writable_idx_flat( const ulong           slot,
     306             :                                           const ushort          idx,
     307             :                                           const fd_pubkey_t *   addr_at_idx,
     308             :                                           const fd_txn_t *      txn_descriptor,
     309             :                                           const fd_features_t * features,
     310             :                                           const uint            bpf_upgradeable_in_txn );
     311             : 
     312             : /* The bpf_upgradeable_in_txn argument of the above function can be
     313             :    obtained by the function below */
     314             : uint
     315             : fd_txn_account_has_bpf_loader_upgradeable( const fd_pubkey_t * account_keys,
     316             :                                            const ulong         accounts_cnt );
     317             : 
     318             : 
     319             : /* Account pre-condition filtering functions
     320             : 
     321             :    Used to filter accounts based on pre-conditions such as existence, is_writable, etc.
     322             :    when obtaining accounts from the transaction context. Passed as a function pointer. */
     323             : 
     324             : int
     325             : fd_txn_account_check_exists( fd_txn_account_t *        acc,
     326             :                              fd_exec_txn_ctx_t const * ctx,
     327             :                              ushort                    idx );
     328             : 
     329             : int
     330             : fd_txn_account_check_is_writable( fd_txn_account_t *        acc,
     331             :                                   fd_exec_txn_ctx_t const * ctx,
     332             :                                   ushort                    idx );
     333             : 
     334             : /* The fee payer is a valid modifiable account if it is passed in as writable
     335             :    in the message via a valid signature. We ignore if the account has been
     336             :    demoted or not (see fd_exec_txn_ctx_account_is_writable_idx) for more details.
     337             :    Agave and Firedancer will reject the fee payer if the transaction message
     338             :    doesn't have a writable signature. */
     339             : 
     340             : int
     341             : fd_txn_account_check_fee_payer_writable( fd_txn_account_t *        acc,
     342             :                                          fd_exec_txn_ctx_t const * ctx,
     343             :                                          ushort                    idx );
     344             : 
     345             : /* Checks if the account is mutable and borrows the account mutably.
     346             : 
     347             :    The borrow is an acquired write on the account object.
     348             :    The caller is responsible for releasing the write via
     349             :    fd_txn_account_release_write.
     350             : 
     351             :    TODO: Agave doesn't need to check if the account is mutable
     352             :    because it uses Writable/Readable traits for accounts. We
     353             :    should have a similar concept to abstract away fd_txn_account_t's
     354             :    const_meta and meta fields. */
     355             : 
     356             : int
     357             : fd_txn_account_check_borrow_mut( fd_txn_account_t *        acc,
     358             :                                  fd_exec_txn_ctx_t const * ctx,
     359             :                                  ushort                    idx );
     360             : 
     361             : 
     362             : FD_PROTOTYPES_END
     363             : 
     364             : #endif /* HEADER_fd_src_flamenco_runtime_context_fd_exec_txn_ctx_h */

Generated by: LCOV version 1.14