Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_context_fd_exec_instr_ctx_h 2 : #define HEADER_fd_src_flamenco_runtime_context_fd_exec_instr_ctx_h 3 : 4 : #include "../info/fd_instr_info.h" 5 : #include "../fd_executor_err.h" 6 : #include "../sysvar/fd_sysvar_cache.h" 7 : #include "../../fd_flamenco_base.h" 8 : #include "../../../ballet/txn/fd_txn.h" 9 : 10 : /* Avoid circular include dependency with forward declaration */ 11 : struct fd_borrowed_account; 12 : typedef struct fd_borrowed_account fd_borrowed_account_t; 13 : 14 : /* fd_exec_instr_ctx_t is the context needed to execute a single 15 : instruction (program invocation). */ 16 : 17 : struct fd_exec_instr_ctx { 18 : fd_instr_info_t const * instr; /* The instruction info for this instruction */ 19 : fd_runtime_t * runtime; /* The runtime for this instruction */ 20 : fd_txn_in_t const * txn_in; /* The input for this instruction */ 21 : fd_txn_out_t * txn_out; /* The output for this instruction */ 22 : fd_sysvar_cache_t const * sysvar_cache; 23 : fd_bank_t * bank; 24 : 25 : /* Most instructions log the base58 program id multiple times, so it's 26 : convenient to compute it once and reuse it. */ 27 : char program_id_base58[ FD_BASE58_ENCODED_32_SZ ]; 28 : }; 29 : 30 : /* Be careful when using this macro. There may be places where the error 31 : will need to be handled differently. */ 32 70498 : #define FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, idx, acc ) do { \ 33 70498 : int err = fd_exec_instr_ctx_try_borrow_instr_account( ctx, idx, acc ); \ 34 70498 : if( FD_UNLIKELY( err ) ) return err; \ 35 70498 : } while (0) 36 : 37 : FD_PROTOTYPES_BEGIN 38 : 39 : /* Operators */ 40 : 41 : /* Mirrors Agave function solana_sdk::transaction_context::InstructionContext::check_number_of_instruction_accounts 42 : 43 : Assert that enough accounts were supplied to this instruction. Returns 44 : FD_EXECUTOR_INSTR_SUCCESS if the number of accounts is as expected and 45 : FD_EXECUTOR_INSTR_ERR_MISSING_ACC otherwise. 46 : 47 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L490 */ 48 : 49 : static inline int 50 : fd_exec_instr_ctx_check_num_insn_accounts( fd_exec_instr_ctx_t const * ctx, 51 4569 : uint expected_accounts ) { 52 : 53 4569 : if( FD_UNLIKELY( ctx->instr->acct_cnt<expected_accounts ) ) { 54 7 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC; 55 7 : } 56 4562 : return FD_EXECUTOR_INSTR_SUCCESS; 57 4569 : } 58 : 59 : /* fd_exec_instr_ctx_instr_account_map fills map with, for each 60 : transaction account index, the first index of that account in the 61 : instruction, or USHORT_MAX. Transaction keys are unique, so 62 : map[ fd_runtime_find_index_of_account( pubkey ) ] is the index of 63 : pubkey in the instruction. */ 64 : 65 : void 66 : fd_exec_instr_ctx_instr_account_map( fd_exec_instr_ctx_t const * ctx, 67 : ushort map[ MAX_TX_ACCOUNT_LOCKS ] ); 68 : 69 : /* Mirrors Agave function solana_sdk::transaction_context::InstructionContext::get_index_of_instruction_account_in_transaction 70 : 71 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L552 */ 72 : 73 : static inline int 74 : fd_exec_instr_ctx_get_index_of_instr_account_in_transaction( fd_exec_instr_ctx_t const * ctx, 75 : ushort idx_in_instr, 76 247046 : ushort * idx_in_txn ) { 77 247046 : if( FD_UNLIKELY( idx_in_instr==USHORT_MAX ) ) { 78 1 : *idx_in_txn = USHORT_MAX; 79 1 : return FD_EXECUTOR_INSTR_SUCCESS; 80 1 : } 81 : 82 : /* Return a MissingAccount error if the idx is out of bounds. 83 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L559 */ 84 247045 : if( FD_UNLIKELY( idx_in_instr>=ctx->instr->acct_cnt ) ) { 85 173 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC; 86 173 : } 87 : 88 246872 : *idx_in_txn = ctx->instr->accounts[ idx_in_instr ].index_in_transaction; 89 246872 : return FD_EXECUTOR_INSTR_SUCCESS; 90 247045 : } 91 : 92 : /* A helper function to get the pubkey of an account using its instruction context index */ 93 : int 94 : fd_exec_instr_ctx_get_key_of_account_at_index( fd_exec_instr_ctx_t const * ctx, 95 : ushort idx_in_instr, 96 : fd_pubkey_t const * * key ); 97 : 98 : /* Mirrors Agave function solana_sdk::transaction_context::InstructionContext::get_last_program_key. 99 : 100 : There can only be one program per instruction, so this function simply retrieves 101 : that program's pubkey, despite the name implying multiple programs per instruction. 102 : The function exists to match semantics with Agave. 103 : 104 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L582 */ 105 : 106 : int 107 : fd_exec_instr_ctx_get_last_program_key( fd_exec_instr_ctx_t const * ctx, 108 : fd_pubkey_t const * * key ); 109 : 110 : /* Mirrors Agave function solana_sdk::transaction_context::InstructionContext::try_borrow_account. 111 : 112 : Borrows an account from the instruction context with a given account index. 113 : 114 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L594 */ 115 : 116 : int 117 : fd_exec_instr_ctx_try_borrow_instr_account( fd_exec_instr_ctx_t const * ctx, 118 : ushort idx, 119 : fd_borrowed_account_t * account ); 120 : 121 : /* Mirrors Agave function solana_sdk::transaction_context::InstructionContext::try_borrow_last_program_account 122 : 123 : Borrows the instruction's program account. Since there is only one program account per 124 : instruction, this function simply borrows the instruction's only program account, despite the name. 125 : 126 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L616 */ 127 : 128 : int 129 : fd_exec_instr_ctx_try_borrow_last_program_account( fd_exec_instr_ctx_t const * ctx, 130 : fd_borrowed_account_t * account ); 131 : 132 : /* Mirrors Agave function solana_sdk::transaction_context::InstructionContext::get_signers 133 : 134 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L684 */ 135 : 136 : int 137 : fd_exec_instr_ctx_get_signers( fd_exec_instr_ctx_t const * ctx, 138 : fd_pubkey_t const * signers[ static FD_INSTR_SIGNERS_MAX ], 139 : ulong * signers_cnt ); 140 : 141 : /* fd_exec_instr_ctx_any_signed matches 142 : solana_system_program::system_processor::Address::is_signer 143 : 144 : Scans instruction accounts for matching signer. 145 : 146 : Returns 1 if *any* instruction account with the given pubkey is a 147 : signer and 0 otherwise. Note that the same account/pubkey can be 148 : specified as multiple different instruction accounts that might not 149 : all have the signer bit. 150 : 151 : https://github.com/anza-xyz/agave/blob/v2.1.14/programs/system/src/system_processor.rs#L35-L41 */ 152 : 153 : FD_FN_PURE int 154 : fd_exec_instr_ctx_any_signed( fd_exec_instr_ctx_t const * ctx, 155 : fd_pubkey_t const * pubkey ); 156 : 157 : /* Although fd_signers_contains does not take an instruction context, 158 : it is included here for relevance to signer helper functions 159 : 160 : Loop conditions could be optimized to allow for unroll/vectorize */ 161 : 162 : static inline int 163 : fd_signers_contains( fd_pubkey_t const * signers[ static FD_INSTR_SIGNERS_MAX ], 164 : ulong signers_cnt, 165 13471 : fd_pubkey_t const * pubkey ) { 166 25792 : for( ulong i=0; i<signers_cnt; i++ ) 167 15428 : if( 0==memcmp( signers[i], pubkey, sizeof( fd_pubkey_t ) ) ) return 1; 168 10364 : return 0; 169 13471 : } 170 : 171 : FD_PROTOTYPES_END 172 : 173 : #endif /* HEADER_fd_src_flamenco_runtime_context_fd_exec_instr_ctx_h */