Line data Source code
1 : #include "fd_exec_instr_ctx.h"
2 : #include "../fd_runtime.h"
3 : #include "../fd_borrowed_account.h"
4 :
5 : int
6 : fd_exec_instr_ctx_find_idx_of_instr_account( fd_exec_instr_ctx_t const * ctx,
7 108 : fd_pubkey_t const * pubkey ) {
8 216 : for( int i=0; i<ctx->instr->acct_cnt; i++ ) {
9 216 : ushort idx_in_txn = ctx->instr->accounts[ i ].index_in_transaction;
10 216 : if( memcmp( pubkey->uc, ctx->txn_out->accounts.keys[ idx_in_txn ].uc, sizeof(fd_pubkey_t) )==0 ) {
11 108 : return i;
12 108 : }
13 216 : }
14 0 : return -1;
15 108 : }
16 :
17 : int
18 : fd_exec_instr_ctx_get_key_of_account_at_index( fd_exec_instr_ctx_t const * ctx,
19 : ushort idx_in_instr,
20 0 : fd_pubkey_t const * * key ) {
21 0 : ushort idx_in_txn;
22 0 : int err = fd_exec_instr_ctx_get_index_of_instr_account_in_transaction( ctx,
23 0 : idx_in_instr,
24 0 : &idx_in_txn );
25 0 : if( FD_UNLIKELY( err ) ) {
26 0 : return err;
27 0 : }
28 :
29 0 : return fd_runtime_get_key_of_account_at_index( ctx->txn_out,
30 0 : idx_in_txn,
31 0 : key );
32 0 : }
33 :
34 : int
35 : fd_exec_instr_ctx_get_last_program_key( fd_exec_instr_ctx_t const * ctx,
36 240 : fd_pubkey_t const * * key ) {
37 240 : return fd_runtime_get_key_of_account_at_index( ctx->txn_out,
38 240 : ctx->instr->program_id,
39 240 : key );
40 240 : }
41 :
42 : int
43 : fd_exec_instr_ctx_try_borrow_account( fd_exec_instr_ctx_t const * ctx,
44 : ushort idx_in_instr,
45 : ushort idx_in_txn,
46 300 : fd_borrowed_account_t * account ) {
47 : /* Get the account from the transaction context using idx_in_txn.
48 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L600-L602 */
49 300 : int err = fd_runtime_get_account_at_index( ctx->txn_in,
50 300 : ctx->txn_out,
51 300 : idx_in_txn,
52 300 : NULL );
53 300 : if( FD_UNLIKELY( err ) ) {
54 : /* Return a MissingAccount error if the account is not found.
55 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L603 */
56 0 : FD_TXN_ERR_FOR_LOG_INSTR( ctx->txn_out, FD_EXECUTOR_INSTR_ERR_MISSING_ACC, ctx->txn_out->err.exec_err_idx );
57 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
58 0 : }
59 :
60 300 : fd_account_meta_t * meta = ctx->txn_out->accounts.account[idx_in_txn].meta;
61 :
62 : /* Return an AccountBorrowFailed error if the write is not acquirable.
63 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L605 */
64 300 : if( FD_UNLIKELY( ctx->runtime->accounts.refcnt[idx_in_txn]!=0UL ) ) {
65 0 : return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
66 0 : }
67 300 : ctx->runtime->accounts.refcnt[idx_in_txn]++;
68 :
69 : /* Create a BorrowedAccount upon success.
70 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L606 */
71 300 : fd_borrowed_account_init( account,
72 300 : &ctx->txn_out->accounts.keys[idx_in_txn],
73 300 : meta,
74 300 : ctx,
75 300 : idx_in_instr,
76 300 : &ctx->runtime->accounts.refcnt[idx_in_txn] );
77 300 : return FD_EXECUTOR_INSTR_SUCCESS;
78 300 : }
79 :
80 : int
81 : fd_exec_instr_ctx_try_borrow_instr_account( fd_exec_instr_ctx_t const * ctx,
82 : ushort idx,
83 300 : fd_borrowed_account_t * account ) {
84 : /* Find the index of the account in the transaction context.
85 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L649-L650 */
86 300 : ushort idx_in_txn;
87 300 : int err = fd_exec_instr_ctx_get_index_of_instr_account_in_transaction( ctx,
88 300 : idx,
89 300 : &idx_in_txn );
90 300 : if( FD_UNLIKELY( err ) ) {
91 0 : return err;
92 0 : }
93 :
94 300 : return fd_exec_instr_ctx_try_borrow_account( ctx,
95 300 : idx,
96 300 : idx_in_txn,
97 300 : account );
98 300 : }
99 :
100 : int
101 : fd_exec_instr_ctx_try_borrow_instr_account_with_key( fd_exec_instr_ctx_t const * ctx,
102 : fd_pubkey_t const * pubkey,
103 48 : fd_borrowed_account_t * account ) {
104 120 : for( ushort i=0; i<ctx->instr->acct_cnt; i++ ) {
105 120 : ushort idx_in_txn = ctx->instr->accounts[ i ].index_in_transaction;
106 120 : if( memcmp( pubkey->uc, ctx->txn_out->accounts.keys[ idx_in_txn ].uc, sizeof(fd_pubkey_t) )==0 ) {
107 48 : return fd_exec_instr_ctx_try_borrow_instr_account( ctx, i, account );
108 48 : }
109 120 : }
110 :
111 : /* Return a NotEnoughAccountKeys error if the account is not found
112 : in the instruction context to match the error code returned by
113 : fd_exec_instr_ctx_try_borrow_instr_account. */
114 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
115 48 : }
116 :
117 : int
118 : fd_exec_instr_ctx_try_borrow_last_program_account( fd_exec_instr_ctx_t const * ctx,
119 0 : fd_borrowed_account_t * account ) {
120 : /* The index_in_instruction for a borrowed program account is invalid,
121 : so it is set to a sentinel value of USHORT_MAX. */
122 0 : return fd_exec_instr_ctx_try_borrow_account( ctx,
123 0 : USHORT_MAX,
124 0 : ctx->instr->program_id,
125 0 : account );
126 0 : }
127 :
128 : int
129 : fd_exec_instr_ctx_get_signers( fd_exec_instr_ctx_t const * ctx,
130 0 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX] ) {
131 0 : ulong j = 0UL;
132 0 : for( ushort i=0; i<ctx->instr->acct_cnt && j<FD_TXN_SIG_MAX; i++ )
133 0 : if( fd_instr_acc_is_signer_idx( ctx->instr, i, NULL ) ) {
134 0 : ushort idx_in_txn = ctx->instr->accounts[i].index_in_transaction;
135 0 : int err = fd_runtime_get_key_of_account_at_index( ctx->txn_out,
136 0 : idx_in_txn,
137 0 : &signers[j++] );
138 0 : if( FD_UNLIKELY( err ) ) {
139 0 : return err;
140 0 : }
141 0 : }
142 0 : return FD_EXECUTOR_INSTR_SUCCESS;
143 0 : }
144 :
145 : int
146 : fd_exec_instr_ctx_any_signed( fd_exec_instr_ctx_t const * ctx,
147 0 : fd_pubkey_t const * pubkey ) {
148 0 : int is_signer = 0;
149 0 : for( ushort j=0; j<ctx->instr->acct_cnt; j++ ) {
150 0 : ushort idx_in_txn = ctx->instr->accounts[ j ].index_in_transaction;
151 0 : is_signer |=
152 : ( ( !!fd_instr_acc_is_signer_idx( ctx->instr, j, NULL ) ) &
153 0 : ( 0==memcmp( pubkey->key, ctx->txn_out->accounts.keys[ idx_in_txn ].key, sizeof(fd_pubkey_t) ) ) );
154 0 : }
155 0 : return is_signer;
156 0 : }
|