Line data Source code
1 : #include "fd_native_cpi.h" 2 : #include "../fd_borrowed_account.h" 3 : #include "../fd_executor.h" 4 : #include "../../vm/syscall/fd_vm_syscall.h" 5 : #include "../../../util/bits/fd_uwide.h" 6 : 7 : int 8 : fd_native_cpi_native_invoke( fd_exec_instr_ctx_t * ctx, 9 : fd_pubkey_t const * native_program_id, 10 : uchar * instr_data, 11 : ulong instr_data_len, 12 : fd_vm_rust_account_meta_t const * acct_metas, 13 : ulong acct_metas_len, 14 : fd_pubkey_t const * signers, 15 0 : ulong signers_cnt ) { 16 : /* Set up the instr info */ 17 0 : fd_instr_info_t * instr_info = &ctx->runtime->instr.trace[ ctx->runtime->instr.trace_length++ ]; 18 0 : fd_instruction_account_t instruction_accounts[ FD_VM_CPI_MAX_INSTRUCTION_ACCOUNTS ]; 19 0 : ulong instruction_accounts_cnt; 20 : 21 : /* Set the stack size */ 22 0 : instr_info->stack_height = ctx->runtime->instr.stack_sz+1; 23 : 24 : /* fd_vm_prepare_instruction will handle missing/invalid account case */ 25 0 : instr_info->program_id = UCHAR_MAX; 26 0 : int program_id = fd_runtime_find_index_of_account( ctx->txn_out, native_program_id ); 27 0 : if( FD_LIKELY( program_id!=-1 ) ) { 28 0 : instr_info->program_id = (uchar)program_id; 29 0 : } 30 : 31 0 : fd_pubkey_t instr_acct_keys[ FD_VM_CPI_MAX_INSTRUCTION_ACCOUNTS ]; 32 0 : uchar acc_idx_seen[ FD_TXN_ACCT_ADDR_MAX ] = {0}; 33 : 34 0 : instr_info->acct_cnt = (ushort)acct_metas_len; 35 0 : for( ushort j=0U; j<acct_metas_len; j++ ) { 36 0 : fd_vm_rust_account_meta_t const * acct_meta = &acct_metas[j]; 37 0 : fd_pubkey_t const * acct_key = fd_type_pun_const( acct_meta->pubkey ); 38 0 : instr_acct_keys[j] = *acct_key; 39 : 40 0 : int idx_in_txn = fd_runtime_find_index_of_account( ctx->txn_out, acct_key ); 41 0 : int idx_in_caller = fd_exec_instr_ctx_find_idx_of_instr_account( ctx, acct_key ); 42 : 43 0 : fd_instr_info_setup_instr_account( instr_info, 44 0 : acc_idx_seen, 45 0 : idx_in_txn!=-1 ? (ushort)idx_in_txn : USHORT_MAX, 46 0 : idx_in_caller!=-1 ? (ushort)idx_in_caller : USHORT_MAX, 47 0 : j, 48 0 : acct_meta->is_writable, 49 0 : acct_meta->is_signer ); 50 0 : } 51 : 52 0 : fd_memcpy( instr_info->data, instr_data, instr_data_len ); 53 0 : instr_info->data_sz = (ushort)instr_data_len; 54 : 55 : /* https://github.com/anza-xyz/agave/blob/v2.2.6/program-runtime/src/invoke_context.rs#L312-L313 */ 56 0 : int exec_err = fd_vm_prepare_instruction( instr_info, 57 0 : ctx, 58 0 : native_program_id, 59 0 : instr_acct_keys, 60 0 : instruction_accounts, 61 0 : &instruction_accounts_cnt, 62 0 : signers, 63 0 : signers_cnt ); 64 0 : if( FD_UNLIKELY( exec_err!=FD_EXECUTOR_INSTR_SUCCESS ) ) { 65 0 : return exec_err; 66 0 : } 67 : 68 : /* https://github.com/anza-xyz/agave/blob/v2.2.6/program-runtime/src/invoke_context.rs#L315-L321 */ 69 0 : return fd_execute_instr( ctx->runtime, ctx->bank, ctx->txn_in, ctx->txn_out, instr_info ); 70 0 : } 71 : 72 : void 73 : fd_native_cpi_create_account_meta( fd_pubkey_t const * key, uchar is_signer, 74 0 : uchar is_writable, fd_vm_rust_account_meta_t * meta ) { 75 0 : meta->is_signer = is_signer; 76 0 : meta->is_writable = is_writable; 77 0 : fd_memcpy( meta->pubkey, key->key, sizeof(fd_pubkey_t) ); 78 0 : }