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

Generated by: LCOV version 1.14