Line data Source code
1 : #include "fd_system_program.h"
2 : #include "../fd_borrowed_account.h"
3 : #include "../fd_system_ids.h"
4 : #include "../sysvar/fd_sysvar_rent.h"
5 : #include "../sysvar/fd_sysvar_recent_hashes.h"
6 : #include "../../accdb/fd_accdb_sync.h"
7 : #include "../../log_collector/fd_log_collector.h"
8 :
9 : static int
10 : require_acct( fd_exec_instr_ctx_t * ctx,
11 : ushort idx,
12 0 : fd_pubkey_t const * pubkey ) {
13 :
14 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/program-runtime/src/sysvar_cache.rs#L290-L294 */
15 0 : fd_pubkey_t const * acc_key = NULL;
16 0 : int err = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, idx, &acc_key );
17 0 : if( FD_UNLIKELY( err ) ) return err;
18 :
19 0 : if( FD_UNLIKELY( 0!=memcmp( acc_key, pubkey->uc, sizeof(fd_pubkey_t) ) ) )
20 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
21 :
22 0 : return FD_EXECUTOR_INSTR_SUCCESS;
23 0 : }
24 :
25 : static int
26 : require_acct_rent( fd_exec_instr_ctx_t * ctx,
27 : ushort idx,
28 0 : fd_rent_t * rent ) {
29 :
30 0 : do {
31 0 : int err = require_acct( ctx, idx, &fd_sysvar_rent_id );
32 0 : if( FD_UNLIKELY( err ) ) return err;
33 0 : } while(0);
34 :
35 0 : if( FD_UNLIKELY( !fd_sysvar_cache_rent_read( ctx->sysvar_cache, rent ) ) )
36 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
37 :
38 0 : return FD_EXECUTOR_INSTR_SUCCESS;
39 0 : }
40 :
41 : static int
42 : require_acct_recent_blockhashes( fd_exec_instr_ctx_t * ctx,
43 0 : ushort idx ) {
44 0 : int err = require_acct( ctx, idx, &fd_sysvar_recent_block_hashes_id );
45 0 : if( FD_UNLIKELY( err ) ) return err;
46 :
47 0 : if( FD_UNLIKELY( !fd_sysvar_cache_recent_hashes_is_valid( ctx->sysvar_cache ) ) ) {
48 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
49 0 : }
50 :
51 0 : return FD_EXECUTOR_INSTR_SUCCESS;
52 0 : }
53 :
54 : /* most_recent_block_hash mirrors
55 : solana_runtime::bank::Bank::last_blockhash_and_lamports_per_signature
56 :
57 : https://github.com/solana-labs/solana/blob/v1.17.23/runtime/src/bank.rs#L4033-L4040 */
58 :
59 : static int
60 : most_recent_block_hash( fd_exec_instr_ctx_t * ctx,
61 0 : fd_blockhash_info_t * out ) {
62 : /* The environment config blockhash comes from `bank.last_blockhash_and_lamports_per_signature()`,
63 : which takes the top element from the blockhash queue.
64 : https://github.com/anza-xyz/agave/blob/v2.1.6/programs/system/src/system_instruction.rs#L47 */
65 0 : fd_blockhashes_t const * blockhashes = &ctx->bank->f.block_hash_queue;
66 0 : fd_blockhash_info_t const * last_bhash_info = fd_blockhashes_peek_last( blockhashes );
67 0 : if( FD_UNLIKELY( last_bhash_info==NULL ) ) {
68 : // Agave panics if this blockhash was never set at the start of the txn batch
69 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_NONCE_NO_RECENT_BLOCKHASHES;
70 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
71 0 : }
72 :
73 0 : *out = *last_bhash_info;
74 0 : return FD_EXECUTOR_INSTR_SUCCESS;
75 0 : }
76 :
77 : static void
78 : fd_durable_nonce_from_blockhash( fd_hash_t * out,
79 153 : fd_hash_t const * blockhash ) {
80 153 : uchar buf[45];
81 153 : memcpy( buf, "DURABLE_NONCE", 13UL );
82 153 : memcpy( buf+13, blockhash, sizeof(fd_hash_t) );
83 153 : fd_sha256_hash( buf, sizeof(buf), out );
84 153 : }
85 :
86 : /* fd_system_program_set_nonce_state is a helper for updating the
87 : contents of a nonce account.
88 :
89 : Matches solana_sdk::transaction_context::BorrowedAccount::set_state
90 : https://github.com/solana-labs/solana/blob/v1.17.23/sdk/src/transaction_context.rs#L1020-L1029 */
91 :
92 : static int
93 : fd_system_program_set_nonce_state( fd_borrowed_account_t * account,
94 0 : fd_nonce_state_versions_t const * new_state ) {
95 :
96 : /* https://github.com/solana-labs/solana/blob/v1.17.23/sdk/src/transaction_context.rs#L1021
97 : => https://github.com/solana-labs/solana/blob/v1.17.23/sdk/src/transaction_context.rs#L868 */
98 :
99 0 : uchar * data = NULL;
100 0 : ulong dlen = 0UL;
101 0 : int err = fd_borrowed_account_get_data_mut( account, &data, &dlen );
102 0 : if( FD_UNLIKELY( err ) ) return err;
103 :
104 : /* https://github.com/solana-labs/solana/blob/v1.17.23/sdk/src/transaction_context.rs#L1024-L1026 */
105 :
106 0 : if( FD_UNLIKELY( fd_nonce_state_versions_size( new_state ) > fd_borrowed_account_get_data_len( account ) ) )
107 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
108 :
109 : /* https://github.com/solana-labs/solana/blob/v1.17.23/sdk/src/transaction_context.rs#L1027 */
110 :
111 0 : do {
112 0 : fd_bincode_encode_ctx_t encode =
113 0 : { .data = data,
114 0 : .dataend = data + dlen };
115 0 : int err = fd_nonce_state_versions_encode( new_state, &encode );
116 0 : if( FD_UNLIKELY( err ) ) {
117 0 : return FD_EXECUTOR_INSTR_ERR_GENERIC_ERR;
118 0 : }
119 0 : } while(0);
120 :
121 0 : return FD_EXECUTOR_INSTR_SUCCESS;
122 0 : }
123 :
124 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L20-L70
125 :
126 : Matches Solana Labs system_instruction::advance_nonce_account */
127 :
128 : static int
129 : fd_system_program_advance_nonce_account( fd_exec_instr_ctx_t * ctx,
130 : fd_borrowed_account_t * account,
131 0 : ushort instr_acc_idx ) {
132 :
133 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L25-L32 */
134 :
135 0 : if( FD_UNLIKELY( !fd_instr_acc_is_writable_idx( ctx->instr, instr_acc_idx ) ) ) {
136 : /* Max msg_sz: 50 - 2 + 45 = 93 < 127 => we can use printf */
137 0 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, pubkey_b58 );
138 0 : fd_log_collector_printf_dangerous_max_127( ctx,
139 0 : "Advance nonce account: Account %s must be writeable", pubkey_b58 );
140 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
141 0 : }
142 :
143 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L34 */
144 :
145 0 : fd_nonce_state_versions_t versions[1];
146 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
147 0 : nonce_state_versions, versions,
148 0 : fd_borrowed_account_get_data( account ),
149 0 : fd_borrowed_account_get_data_len( account ) ) ) ) {
150 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
151 0 : }
152 :
153 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L35 */
154 :
155 0 : fd_nonce_state_t * state = NULL;
156 0 : switch( versions->discriminant ) {
157 0 : case fd_nonce_state_versions_enum_legacy:
158 0 : state = &versions->inner.legacy;
159 0 : break;
160 0 : case fd_nonce_state_versions_enum_current:
161 0 : state = &versions->inner.current;
162 0 : break;
163 0 : default:
164 0 : FD_LOG_CRIT(( "invalid nonce state version %u", versions->discriminant ));
165 0 : }
166 :
167 0 : switch( state->discriminant ) {
168 :
169 0 : case fd_nonce_state_enum_initialized: {
170 0 : fd_nonce_data_t * data = &state->inner.initialized;
171 :
172 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L37-L44 */
173 :
174 0 : if( FD_UNLIKELY( !fd_exec_instr_ctx_any_signed( ctx, &data->authority ) ) ) {
175 : /* Max msg_sz: 50 - 2 + 45 = 93 < 127 => we can use printf */
176 0 : FD_BASE58_ENCODE_32_BYTES( data->authority.key, authority_b58 );
177 0 : fd_log_collector_printf_dangerous_max_127( ctx,
178 0 : "Advance nonce account: Account %s must be a signer", authority_b58 );
179 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
180 0 : }
181 :
182 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L45 */
183 :
184 0 : fd_blockhash_info_t blockhash[1];
185 0 : do {
186 0 : int err = most_recent_block_hash( ctx, blockhash );
187 0 : if( FD_UNLIKELY( err ) ) return err;
188 0 : } while(0);
189 :
190 0 : fd_hash_t next_durable_nonce;
191 0 : fd_durable_nonce_from_blockhash( &next_durable_nonce, &blockhash->hash );
192 :
193 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L46-L52 */
194 :
195 0 : if( FD_UNLIKELY( 0==memcmp( data->durable_nonce.hash, next_durable_nonce.hash, sizeof(fd_hash_t) ) ) ) {
196 0 : fd_log_collector_msg_literal( ctx, "Advance nonce account: nonce can only advance once per slot" );
197 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_NONCE_BLOCKHASH_NOT_EXPIRED;
198 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
199 0 : }
200 :
201 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/programs/system/src/system_instruction.rs#L57-L63 */
202 :
203 0 : fd_nonce_state_versions_t new_state = {
204 0 : .discriminant = fd_nonce_state_versions_enum_current,
205 0 : .inner = { .current = {
206 0 : .discriminant = fd_nonce_state_enum_initialized,
207 0 : .inner = { .initialized = {
208 0 : .authority = data->authority,
209 0 : .durable_nonce = next_durable_nonce,
210 0 : .fee_calculator = {
211 0 : .lamports_per_signature = blockhash->fee_calculator.lamports_per_signature
212 0 : }
213 0 : } }
214 0 : } }
215 0 : };
216 :
217 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L59 */
218 :
219 0 : do {
220 0 : int err = fd_system_program_set_nonce_state( account, &new_state );
221 0 : if( FD_UNLIKELY( err ) ) return err;
222 0 : } while(0);
223 :
224 0 : break;
225 0 : }
226 :
227 0 : case fd_nonce_state_enum_uninitialized: {
228 : /* Max msg_sz: 50 - 2 + 45 = 93 < 127 => we can use printf */
229 0 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, pubkey_b58 );
230 0 : fd_log_collector_printf_dangerous_max_127( ctx,
231 0 : "Advance nonce account: Account %s state is invalid", pubkey_b58 );
232 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
233 0 : }
234 :
235 0 : } /* switch */
236 :
237 0 : return FD_EXECUTOR_INSTR_SUCCESS;
238 0 : }
239 :
240 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L423-L441
241 :
242 : Matches Solana Labs system_processor SystemInstruction::AdvanceNonceAccount => { ... } */
243 :
244 : int
245 0 : fd_system_program_exec_advance_nonce_account( fd_exec_instr_ctx_t * ctx ) {
246 0 : int err;
247 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L423-L441 */
248 :
249 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) )
250 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
251 :
252 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L425-L426 */
253 :
254 0 : uchar const instr_acc_idx = 0;
255 :
256 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/system/src/system_processor.rs#L409-L410 */
257 :
258 0 : fd_guarded_borrowed_account_t account = {0};
259 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, instr_acc_idx, &account );
260 :
261 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L427-L432 */
262 :
263 0 : err = require_acct_recent_blockhashes( ctx, 1UL );
264 0 : if( FD_UNLIKELY( err ) ) return err;
265 :
266 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L433-L439 */
267 :
268 0 : int bhq_empty;
269 0 : do {
270 0 : fd_block_block_hash_entry_t const * hashes = fd_sysvar_cache_recent_hashes_join_const( ctx->sysvar_cache );
271 0 : FD_TEST( hashes ); /* validated above */
272 0 : bhq_empty = deq_fd_block_block_hash_entry_t_empty( hashes );
273 0 : fd_sysvar_cache_recent_hashes_leave_const( ctx->sysvar_cache, hashes );
274 0 : } while(0);
275 0 : if( FD_UNLIKELY( bhq_empty ) ) {
276 0 : fd_log_collector_msg_literal( ctx, "Advance nonce account: recent blockhash list is empty" );
277 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_NONCE_NO_RECENT_BLOCKHASHES;
278 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
279 0 : }
280 :
281 0 : err = fd_system_program_advance_nonce_account( ctx, &account, instr_acc_idx );
282 :
283 : /* Implicit drop */
284 :
285 0 : return err;
286 0 : }
287 :
288 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L72-L151
289 :
290 : Matches Solana Labs system_instruction::withdraw_nonce_account */
291 :
292 : static int
293 : fd_system_program_withdraw_nonce_account( fd_exec_instr_ctx_t * ctx,
294 : ulong requested_lamports,
295 0 : fd_rent_t const * rent ) {
296 0 : int err;
297 0 : ushort const from_acct_idx = 0UL;
298 0 : ushort const to_acct_idx = 1UL;
299 :
300 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L82-L83 */
301 :
302 0 : fd_guarded_borrowed_account_t from = {0};
303 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, from_acct_idx, &from );
304 :
305 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L84-L91 */
306 :
307 0 : if( FD_UNLIKELY( !fd_instr_acc_is_writable_idx( ctx->instr, from_acct_idx ) ) ) {
308 : /* Max msg_sz: 51 - 2 + 45 = 94 < 127 => we can use printf */
309 0 : FD_BASE58_ENCODE_32_BYTES( from.pubkey->key, pubkey_b58 );
310 0 : fd_log_collector_printf_dangerous_max_127( ctx,
311 0 : "Withdraw nonce account: Account %s must be writeable", pubkey_b58 );
312 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
313 0 : }
314 :
315 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L93 */
316 :
317 0 : fd_nonce_state_versions_t versions[1];
318 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
319 0 : nonce_state_versions, versions,
320 0 : fd_borrowed_account_get_data( &from ),
321 0 : fd_borrowed_account_get_data_len( &from ) ) ) ) {
322 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
323 0 : }
324 :
325 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L94 */
326 :
327 0 : fd_nonce_state_t * state = NULL;
328 0 : switch( versions->discriminant ) {
329 0 : case fd_nonce_state_versions_enum_legacy:
330 0 : state = &versions->inner.legacy;
331 0 : break;
332 0 : case fd_nonce_state_versions_enum_current:
333 0 : state = &versions->inner.current;
334 0 : break;
335 0 : default:
336 0 : FD_LOG_CRIT(( "invalid nonce state version %u", versions->discriminant ));
337 0 : }
338 :
339 0 : fd_pubkey_t signer[1] = {0};
340 0 : switch( state->discriminant ) {
341 :
342 0 : case fd_nonce_state_enum_uninitialized: {
343 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L95-L106 */
344 :
345 0 : if( FD_UNLIKELY( requested_lamports > fd_borrowed_account_get_lamports( &from ) ) ) {
346 : /* Max msg_sz: 59 - 6 + 20 + 20 = 93 < 127 => we can use printf */
347 0 : fd_log_collector_printf_dangerous_max_127( ctx,
348 0 : "Withdraw nonce account: insufficient lamports %lu, need %lu", fd_borrowed_account_get_lamports( &from ), requested_lamports );
349 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
350 0 : }
351 :
352 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L105 */
353 :
354 0 : *signer = *from.pubkey;
355 :
356 0 : break;
357 0 : }
358 :
359 0 : case fd_nonce_state_enum_initialized: {
360 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L107-L132 */
361 0 : fd_nonce_data_t * data = &state->inner.initialized;
362 :
363 0 : if( requested_lamports == fd_borrowed_account_get_lamports( &from ) ) {
364 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L108-L117 */
365 :
366 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L109 */
367 :
368 0 : fd_blockhash_info_t blockhash[1];
369 0 : do {
370 0 : int err = most_recent_block_hash( ctx, blockhash );
371 0 : if( FD_UNLIKELY( err ) ) return err;
372 0 : } while(0);
373 :
374 0 : fd_hash_t next_durable_nonce;
375 0 : fd_durable_nonce_from_blockhash( &next_durable_nonce, &blockhash->hash );
376 :
377 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L110-L116 */
378 :
379 0 : if( FD_UNLIKELY( 0==memcmp( data->durable_nonce.hash, next_durable_nonce.hash, sizeof(fd_hash_t) ) ) ) {
380 0 : fd_log_collector_msg_literal( ctx, "Withdraw nonce account: nonce can only advance once per slot" );
381 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_NONCE_BLOCKHASH_NOT_EXPIRED;
382 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
383 0 : }
384 :
385 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L117 */
386 :
387 0 : fd_nonce_state_versions_t new_state[1] = {{
388 0 : .discriminant = fd_nonce_state_versions_enum_current,
389 0 : .inner = { .current = {
390 0 : .discriminant = fd_nonce_state_enum_uninitialized
391 0 : } }
392 0 : }};
393 :
394 0 : do {
395 0 : int err = fd_system_program_set_nonce_state( &from, new_state );
396 0 : if( FD_UNLIKELY( err ) ) return err;
397 0 : } while(0);
398 :
399 0 : } else {
400 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L118-L130 */
401 :
402 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L120 */
403 :
404 0 : ulong min_balance = fd_rent_exempt_minimum_balance( rent, fd_borrowed_account_get_data_len( &from ) );
405 :
406 0 : ulong amount;
407 0 : if( FD_UNLIKELY( __builtin_uaddl_overflow( requested_lamports, min_balance, &amount ) ) )
408 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
409 :
410 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L121-L129 */
411 :
412 0 : if( FD_UNLIKELY( amount > fd_borrowed_account_get_lamports( &from ) ) ) {
413 : /* Max msg_sz: 59 - 6 + 20 + 20 = 93 < 127 => we can use printf */
414 0 : fd_log_collector_printf_dangerous_max_127( ctx,
415 0 : "Withdraw nonce account: insufficient lamports %lu, need %lu", fd_borrowed_account_get_lamports( &from ), amount );
416 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
417 0 : }
418 :
419 0 : }
420 :
421 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L131 */
422 :
423 0 : *signer = data->authority;
424 :
425 0 : break;
426 0 : }
427 :
428 0 : } /* switch */
429 :
430 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L135-L142 */
431 :
432 0 : if( FD_UNLIKELY( !fd_exec_instr_ctx_any_signed( ctx, signer ) ) ) {
433 : /* Max msg_sz: 44 - 2 + 45 = 87 < 127 => we can use printf */
434 0 : FD_BASE58_ENCODE_32_BYTES( signer->key, signer_b58 );
435 0 : fd_log_collector_printf_dangerous_max_127( ctx,
436 0 : "Withdraw nonce account: Account %s must sign", signer_b58 );
437 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
438 0 : }
439 :
440 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L144 */
441 :
442 0 : err = fd_borrowed_account_checked_sub_lamports( &from, requested_lamports );
443 0 : if( FD_UNLIKELY( err ) ) return err;
444 :
445 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L145 */
446 :
447 0 : fd_borrowed_account_drop( &from );
448 :
449 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L146-L147 */
450 :
451 0 : fd_guarded_borrowed_account_t to = {0};
452 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, to_acct_idx, &to );
453 :
454 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L148 */
455 :
456 0 : err = fd_borrowed_account_checked_add_lamports( &to, requested_lamports );
457 0 : if( FD_UNLIKELY( err ) ) return err;
458 :
459 : /* Implicit drop */
460 :
461 0 : return FD_EXECUTOR_INSTR_SUCCESS;
462 0 : }
463 :
464 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L442-L461
465 :
466 : Matches Solana Labs system_processor SystemInstruction::WithdrawNonceAccount { ... } => { ... } */
467 :
468 : int
469 : fd_system_program_exec_withdraw_nonce_account( fd_exec_instr_ctx_t * ctx,
470 0 : ulong requested_lamports ) {
471 :
472 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L443 */
473 :
474 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 2 ) )
475 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
476 :
477 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L445-L449 */
478 :
479 0 : do {
480 0 : int err = require_acct_recent_blockhashes( ctx, 2UL );
481 0 : if( FD_UNLIKELY( err ) ) return err;
482 0 : } while(0);
483 :
484 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L450 */
485 :
486 0 : fd_rent_t rent[1];
487 0 : do {
488 0 : int err = require_acct_rent( ctx, 3UL, rent );
489 0 : if( FD_UNLIKELY( err ) ) return err;
490 0 : } while(0);
491 :
492 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L451-L460 */
493 :
494 0 : return fd_system_program_withdraw_nonce_account( ctx, requested_lamports, rent );
495 0 : }
496 :
497 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L153-L198
498 :
499 : Matches Solana Labs system_instruction::initialize_nonce_account */
500 :
501 : static int
502 : fd_system_program_initialize_nonce_account( fd_exec_instr_ctx_t * ctx,
503 : fd_borrowed_account_t * account,
504 : fd_pubkey_t const * authorized,
505 0 : fd_rent_t const * rent ) {
506 :
507 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/system/src/system_instruction.rs#L167-L174 */
508 :
509 0 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( account ) ) ) {
510 : /* Max msg_sz: 53 - 2 + 45 = 96 < 127 => we can use printf */
511 0 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, pubkey_b58 );
512 0 : fd_log_collector_printf_dangerous_max_127( ctx,
513 0 : "Initialize nonce account: Account %s must be writeable", pubkey_b58 );
514 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
515 0 : }
516 :
517 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L168 */
518 :
519 0 : fd_nonce_state_versions_t versions[1];
520 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
521 0 : nonce_state_versions, versions,
522 0 : fd_borrowed_account_get_data( account ),
523 0 : fd_borrowed_account_get_data_len( account ) ) ) ) {
524 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
525 0 : }
526 :
527 0 : fd_nonce_state_t * state = NULL;
528 0 : switch( versions->discriminant ) {
529 0 : case fd_nonce_state_versions_enum_legacy:
530 0 : state = &versions->inner.legacy;
531 0 : break;
532 0 : case fd_nonce_state_versions_enum_current:
533 0 : state = &versions->inner.current;
534 0 : break;
535 0 : default:
536 0 : FD_LOG_CRIT(( "invalid nonce state version %u", versions->discriminant ));
537 0 : }
538 :
539 0 : switch( state->discriminant ) {
540 :
541 0 : case fd_nonce_state_enum_uninitialized: {
542 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L169-L188 */
543 :
544 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L170 */
545 :
546 0 : ulong min_balance = fd_rent_exempt_minimum_balance( rent, fd_borrowed_account_get_data_len( account ) );
547 :
548 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L171-L179 */
549 :
550 0 : if( FD_UNLIKELY( fd_borrowed_account_get_lamports( account ) < min_balance ) ) {
551 : /* Max msg_sz: 61 - 6 + 20 + 20 = 95 < 127 => we can use printf */
552 0 : fd_log_collector_printf_dangerous_max_127( ctx,
553 0 : "Initialize nonce account: insufficient lamports %lu, need %lu", fd_borrowed_account_get_lamports( account ), min_balance );
554 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
555 0 : }
556 :
557 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L180 */
558 :
559 0 : fd_blockhash_info_t blockhash[1];
560 0 : do {
561 0 : int err = most_recent_block_hash( ctx, blockhash );
562 0 : if( FD_UNLIKELY( err ) ) return err;
563 0 : } while(0);
564 :
565 0 : fd_hash_t durable_nonce;
566 0 : fd_durable_nonce_from_blockhash( &durable_nonce, &blockhash->hash );
567 :
568 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/programs/system/src/system_instruction.rs#L185-L191 */
569 :
570 0 : fd_nonce_state_versions_t new_state = {
571 0 : .discriminant = fd_nonce_state_versions_enum_current,
572 0 : .inner = { .current = {
573 0 : .discriminant = fd_nonce_state_enum_initialized,
574 0 : .inner = { .initialized = {
575 0 : .authority = *authorized,
576 0 : .durable_nonce = durable_nonce,
577 0 : .fee_calculator = {
578 0 : .lamports_per_signature = blockhash->fee_calculator.lamports_per_signature
579 0 : }
580 0 : } }
581 0 : } }
582 0 : };
583 :
584 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L187 */
585 :
586 0 : do {
587 0 : int err = fd_system_program_set_nonce_state( account, &new_state );
588 0 : if( FD_UNLIKELY( err ) ) return err;
589 0 : } while(0);
590 :
591 0 : break;
592 0 : }
593 :
594 0 : case fd_nonce_state_enum_initialized: {
595 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L189-L196 */
596 :
597 : /* Max msg_sz: 53 - 2 + 45 = 96 < 127 => we can use printf */
598 0 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, pubkey_b58 );
599 0 : fd_log_collector_printf_dangerous_max_127( ctx,
600 0 : "Initialize nonce account: Account %s state is invalid", pubkey_b58 );
601 :
602 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
603 0 : }
604 :
605 0 : } /* switch */
606 :
607 0 : return FD_EXECUTOR_INSTR_SUCCESS;
608 0 : }
609 :
610 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L462-L481
611 :
612 : Matches Solana Labs system_processor SystemInstruction::InitializeNonceAccount { ... } => { ... } */
613 :
614 : int
615 : fd_system_program_exec_initialize_nonce_account( fd_exec_instr_ctx_t * ctx,
616 0 : fd_pubkey_t const * authorized ) {
617 0 : int err;
618 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L463 */
619 :
620 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) )
621 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
622 :
623 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L464-L465 */
624 :
625 0 : uchar const instr_acc_idx = 0;
626 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/system/src/system_processor.rs#L448-L449 */
627 0 : fd_guarded_borrowed_account_t account = {0};
628 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, instr_acc_idx, &account );
629 :
630 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L466-L471 */
631 :
632 0 : do {
633 0 : err = require_acct_recent_blockhashes( ctx, 1UL );
634 0 : if( FD_UNLIKELY( err ) ) return err;
635 0 : } while(0);
636 :
637 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L472-L478 */
638 :
639 0 : int bhq_empty;
640 0 : do {
641 0 : fd_block_block_hash_entry_t const * hashes = fd_sysvar_cache_recent_hashes_join_const( ctx->sysvar_cache );
642 0 : FD_TEST( hashes ); /* validated above */
643 0 : bhq_empty = deq_fd_block_block_hash_entry_t_empty( hashes );
644 0 : fd_sysvar_cache_recent_hashes_leave_const( ctx->sysvar_cache, hashes );
645 0 : } while(0);
646 0 : if( FD_UNLIKELY( bhq_empty ) ) {
647 0 : fd_log_collector_msg_literal( ctx, "Initialize nonce account: recent blockhash list is empty" );
648 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_NONCE_NO_RECENT_BLOCKHASHES;
649 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
650 0 : }
651 :
652 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L479 */
653 :
654 0 : fd_rent_t rent[1];
655 0 : do {
656 0 : err = require_acct_rent( ctx, 2UL, rent );
657 0 : if( FD_UNLIKELY( err ) ) return err;
658 0 : } while(0);
659 :
660 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L480 */
661 :
662 0 : err = fd_system_program_initialize_nonce_account( ctx, &account, authorized, rent );
663 :
664 : /* Implicit drop */
665 :
666 0 : return err;
667 0 : }
668 :
669 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L200-L236
670 :
671 : Matches Solana Labs system_instruction::authorize_nonce_account */
672 :
673 : static int
674 : fd_system_program_authorize_nonce_account( fd_exec_instr_ctx_t * ctx,
675 : fd_borrowed_account_t * account,
676 : ushort instr_acc_idx,
677 0 : fd_pubkey_t const * nonce_authority ) {
678 :
679 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L206-L213 */
680 :
681 0 : if( FD_UNLIKELY( !fd_instr_acc_is_writable_idx( ctx->instr, instr_acc_idx ) ) ) {
682 : /* Max msg_sz: 52 - 2 + 45 = 95 < 127 => we can use printf */
683 0 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, pubkey_b58 );
684 0 : fd_log_collector_printf_dangerous_max_127( ctx,
685 0 : "Authorize nonce account: Account %s must be writeable", pubkey_b58 );
686 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
687 0 : }
688 :
689 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L214-L215 */
690 :
691 0 : fd_nonce_state_versions_t versions[1];
692 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
693 0 : nonce_state_versions, versions,
694 0 : fd_borrowed_account_get_data( account ),
695 0 : fd_borrowed_account_get_data_len( account ) ) ) ) {
696 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
697 0 : }
698 :
699 : /* Inlining solana_program::nonce::state::Versions::authorize
700 : https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/nonce/state/mod.rs#L76-L102 */
701 :
702 : /* https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/nonce/state/mod.rs#L81 */
703 :
704 0 : fd_nonce_state_t * state = NULL;
705 0 : switch( versions->discriminant ) {
706 0 : case fd_nonce_state_versions_enum_legacy:
707 0 : state = &versions->inner.legacy;
708 0 : break;
709 0 : case fd_nonce_state_versions_enum_current:
710 0 : state = &versions->inner.current;
711 0 : break;
712 0 : default:
713 0 : FD_LOG_CRIT(( "invalid nonce state version %u", versions->discriminant ));
714 0 : }
715 :
716 : /* https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/nonce/state/mod.rs#L81-L84 */
717 :
718 0 : if( FD_UNLIKELY( state->discriminant != fd_nonce_state_enum_initialized ) ) {
719 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L219-L226 */
720 :
721 : /* Max msg_sz: 52 - 2 + 45 = 95 < 127 => we can use printf */
722 0 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, pubkey_b58 );
723 0 : fd_log_collector_printf_dangerous_max_127( ctx,
724 0 : "Authorize nonce account: Account %s state is invalid", pubkey_b58 );
725 :
726 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
727 0 : }
728 :
729 0 : fd_nonce_data_t * data = &state->inner.initialized;
730 :
731 : /* https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/nonce/state/mod.rs#L85-L89 */
732 :
733 0 : if( FD_UNLIKELY( !fd_exec_instr_ctx_any_signed( ctx, &data->authority ) ) ) {
734 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L227-L234 */
735 : /* Max msg_sz: 45 - 2 + 45 = 88 < 127 => we can use printf */
736 0 : FD_BASE58_ENCODE_32_BYTES( data->authority.key, authority_b58 );
737 0 : fd_log_collector_printf_dangerous_max_127( ctx,
738 0 : "Authorize nonce account: Account %s must sign", authority_b58 );
739 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
740 0 : }
741 :
742 : /* https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/nonce/state/mod.rs#L90-L95 */
743 :
744 0 : fd_nonce_state_t new_state[1] = {{
745 0 : .discriminant = fd_nonce_state_enum_initialized,
746 0 : .inner = { .initialized = {
747 0 : .authority = *nonce_authority,
748 0 : .durable_nonce = data->durable_nonce,
749 0 : .fee_calculator = data->fee_calculator
750 0 : } }
751 0 : }};
752 :
753 : /* https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/nonce/state/mod.rs#L96-L101 */
754 :
755 0 : fd_nonce_state_versions_t new_versioned[1] = {{0}};
756 0 : new_versioned->discriminant = versions->discriminant;
757 0 : switch( versions->discriminant ) {
758 0 : case fd_nonce_state_versions_enum_legacy:
759 0 : new_versioned->inner.legacy = *new_state;
760 0 : break;
761 0 : case fd_nonce_state_versions_enum_current:
762 0 : new_versioned->inner.current = *new_state;
763 0 : break;
764 0 : default:
765 0 : FD_LOG_CRIT(( "invalid nonce state version %u", versions->discriminant ));
766 0 : }
767 :
768 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_instruction.rs#L218 */
769 :
770 0 : do {
771 0 : int err = fd_system_program_set_nonce_state( account, new_versioned );
772 0 : if( FD_UNLIKELY( err ) ) return err;
773 0 : } while(0);
774 :
775 0 : return FD_EXECUTOR_INSTR_SUCCESS;
776 0 : }
777 :
778 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L482-L487
779 :
780 : Matches Solana Labs system_processor SystemInstruction::AuthorizeNonceAccount { ... } => { ... } */
781 :
782 : int
783 : fd_system_program_exec_authorize_nonce_account( fd_exec_instr_ctx_t * ctx,
784 0 : fd_pubkey_t const * nonce_authority ) {
785 0 : int err;
786 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L483 */
787 :
788 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) )
789 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
790 :
791 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L484-L485 */
792 :
793 0 : fd_guarded_borrowed_account_t account = {0};
794 0 : err = fd_exec_instr_ctx_try_borrow_instr_account( ctx, 0, &account );
795 0 : if( FD_UNLIKELY( err ) ) {
796 0 : return err;
797 0 : }
798 :
799 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L486 */
800 :
801 0 : err = fd_system_program_authorize_nonce_account( ctx, &account, 0UL, nonce_authority );
802 :
803 : /* Implicit drop */
804 :
805 0 : return err;
806 0 : }
807 :
808 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L488-L503
809 :
810 : Matches Solana Labs system_processor SystemInstruction::UpgradeNonceAccount { ... } => { ... } */
811 :
812 : int
813 0 : fd_system_program_exec_upgrade_nonce_account( fd_exec_instr_ctx_t * ctx ) {
814 0 : int err;
815 0 : ushort const nonce_acct_idx = 0UL;
816 :
817 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L489 */
818 :
819 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) )
820 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
821 :
822 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/system/src/system_processor.rs#L474-475 */
823 :
824 0 : fd_guarded_borrowed_account_t account = {0};
825 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, nonce_acct_idx, &account );
826 :
827 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L492-L494 */
828 :
829 0 : if( FD_UNLIKELY( 0!=memcmp( fd_borrowed_account_get_owner( &account ), fd_solana_system_program_id.key, sizeof(fd_pubkey_t) ) ) )
830 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
831 :
832 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L495-L497 */
833 :
834 0 : if( FD_UNLIKELY( !fd_instr_acc_is_writable_idx( ctx->instr, 0 ) ) )
835 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
836 :
837 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L498 */
838 :
839 0 : fd_nonce_state_versions_t versions[1];
840 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
841 0 : nonce_state_versions, versions,
842 0 : fd_borrowed_account_get_data( &account ),
843 0 : fd_borrowed_account_get_data_len( &account ) ) ) ) {
844 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
845 0 : }
846 :
847 : /* Inlining solana_program::nonce::state::Versions::upgrade
848 : https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/nonce/state/mod.rs#L55-L73 */
849 :
850 0 : if( FD_UNLIKELY( versions->discriminant != fd_nonce_state_versions_enum_legacy ) )
851 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
852 :
853 0 : fd_nonce_state_t * state = &versions->inner.legacy;
854 0 : if( FD_UNLIKELY( state->discriminant != fd_nonce_state_enum_initialized ) )
855 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
856 :
857 0 : fd_durable_nonce_from_blockhash( &state->inner.initialized.durable_nonce, &state->inner.initialized.durable_nonce );
858 :
859 : /* https://github.com/solana-labs/solana/blob/v1.17.23/programs/system/src/system_processor.rs#L501 */
860 :
861 0 : fd_nonce_state_versions_t new_state[1] = {{
862 0 : .discriminant = fd_nonce_state_versions_enum_current,
863 0 : .inner = { .current = *state }
864 0 : }};
865 :
866 0 : err = fd_system_program_set_nonce_state( &account, new_state );
867 0 : if( FD_UNLIKELY( err ) ) return err;
868 :
869 : /* Implicit drop */
870 :
871 0 : return FD_EXECUTOR_INSTR_SUCCESS;
872 0 : }
873 :
874 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/runtime/src/bank/check_transactions.rs#L166-L200 */
875 : /* The age of a transaction is valid under two conditions. The first is that
876 : the transactions blockhash is a recent blockhash (within 151) in the block
877 : hash queue. The other condition is that the transaction contains a valid
878 : nonce account. This is the case under several conditions. If neither
879 : condition is met then the transaction is invalid.
880 : Note: We check 151 and not 150 due to a known bug in agave. */
881 : int
882 : fd_check_transaction_age( fd_runtime_t * runtime,
883 : fd_bank_t * bank,
884 : fd_txn_in_t const * txn_in,
885 153 : fd_txn_out_t * txn_out ) {
886 153 : fd_blockhashes_t const * block_hash_queue = &bank->f.block_hash_queue;
887 153 : fd_hash_t const * last_blockhash = fd_blockhashes_peek_last_hash( block_hash_queue );
888 153 : if( FD_UNLIKELY( !last_blockhash ) ) {
889 0 : FD_LOG_CRIT(( "blockhash queue is empty" ));
890 0 : }
891 :
892 : /* check_transaction_age */
893 153 : fd_hash_t next_durable_nonce = {0};
894 153 : fd_durable_nonce_from_blockhash( &next_durable_nonce, last_blockhash );
895 153 : ushort recent_blockhash_off = TXN( txn_in->txn )->recent_blockhash_off;
896 153 : fd_hash_t * recent_blockhash = (fd_hash_t *)((uchar *)txn_in->txn->payload + recent_blockhash_off);
897 :
898 : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/runtime/src/bank.rs#L3538-L3542 */
899 : /* get_hash_info_if_valid. Check 151 hashes from the block hash queue and its
900 : age to see if it is valid. */
901 :
902 153 : if( fd_blockhashes_check_age( block_hash_queue, recent_blockhash, FD_SYSVAR_RECENT_HASHES_CAP ) ) {
903 153 : return FD_RUNTIME_EXECUTE_SUCCESS;
904 153 : }
905 :
906 : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/runtime/src/bank.rs#L3622-L3633 */
907 : /* check_and_load_message_nonce_account */
908 0 : if( FD_UNLIKELY( !memcmp( &next_durable_nonce, recent_blockhash, sizeof(fd_hash_t) ) ) ) { /* nonce_is_advanceable == false */
909 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_NONCE_ALREADY_ADVANCED;
910 0 : }
911 :
912 : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/runtime/src/bank.rs#L3603-L3620*/
913 : /* load_message_nonce_account */
914 :
915 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm-transaction/src/svm_message.rs#L87-L119 */
916 : /* get_durable_nonce */
917 0 : if( FD_UNLIKELY( !TXN( txn_in->txn )->instr_cnt ) ) {
918 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_NOT_FOUND;
919 0 : }
920 : /* Check the first instruction (nonce instruction) to see if the
921 : program id is the system program. Also make sure that it is an
922 : advance nonce account instruction. Finally make sure that the
923 : first instruction account is writable; if it is, then that account
924 : is a durable nonce account. */
925 0 : fd_txn_instr_t const * txn_instr = &TXN( txn_in->txn )->instr[0];
926 0 : fd_acct_addr_t const * tx_accs = fd_txn_get_acct_addrs( TXN( txn_in->txn ), txn_in->txn->payload );
927 0 : fd_acct_addr_t const * prog_id = tx_accs + txn_instr->program_id;
928 0 : if( FD_UNLIKELY( memcmp( prog_id->b, fd_solana_system_program_id.key, sizeof( fd_pubkey_t ) ) ) ) {
929 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_NOT_FOUND;
930 0 : }
931 0 : uchar const * instr_data = fd_txn_get_instr_data( txn_instr, txn_in->txn->payload );
932 0 : uchar const * instr_accts = fd_txn_get_instr_accts( txn_instr, txn_in->txn->payload );
933 0 : uchar nonce_idx = instr_accts[0];
934 :
935 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm-transaction/src/svm_message.rs#L99-L105 */
936 0 : if( FD_UNLIKELY( txn_instr->data_sz<4UL || FD_LOAD( uint, instr_data ) !=
937 0 : (uint)fd_system_program_instruction_enum_advance_nonce_account ) ) {
938 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_NOT_FOUND;
939 0 : }
940 :
941 : /* Nonce account must be...
942 : - writable
943 : - statically included in the transaction account keys (if SIMD-242
944 : is active)
945 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm-transaction/src/svm_message.rs#L110-L111 */
946 0 : if( FD_UNLIKELY( !fd_runtime_account_is_writable_idx( txn_in, txn_out, nonce_idx ) ) ) {
947 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
948 0 : }
949 0 : if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( bank, require_static_nonce_account ) &&
950 0 : nonce_idx>=TXN( txn_in->txn )->acct_addr_cnt ) ) {
951 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
952 0 : }
953 :
954 0 : fd_accdb_ro_t ro[1];
955 0 : fd_funk_txn_xid_t xid = { .ul = { bank->f.slot, bank->idx } };
956 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( runtime->accdb, ro, &xid, &txn_out->accounts.keys[ nonce_idx ] ) ) ) {
957 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
958 0 : }
959 :
960 : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/sdk/src/nonce_account.rs#L28-L42 */
961 : /* verify_nonce_account */
962 0 : fd_pubkey_t const * owner_pubkey = fd_accdb_ref_owner( ro );
963 0 : if( FD_UNLIKELY( !fd_pubkey_eq( owner_pubkey, &fd_solana_system_program_id ) ) ) {
964 0 : fd_accdb_close_ro( runtime->accdb, ro );
965 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
966 0 : }
967 :
968 0 : fd_nonce_state_versions_t state[1];
969 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
970 0 : nonce_state_versions, state,
971 0 : fd_accdb_ref_data_const( ro ),
972 0 : fd_accdb_ref_data_sz ( ro ) ) ) ) {
973 0 : fd_accdb_close_ro( runtime->accdb, ro );
974 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
975 0 : }
976 0 : fd_accdb_close_ro( runtime->accdb, ro );
977 :
978 : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/sdk/program/src/nonce/state/mod.rs#L36-L53 */
979 : /* verify_recent_blockhash. This checks that the decoded nonce record is
980 : not a legacy nonce nor uninitialized. If this is the case, then we can
981 : verify by comparing the decoded durable nonce to the recent blockhash */
982 0 : if( FD_UNLIKELY( fd_nonce_state_versions_is_legacy( state ) ) ) {
983 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
984 0 : }
985 :
986 0 : fd_nonce_state_t nonce_state = state->inner.current;
987 0 : if( FD_UNLIKELY( fd_nonce_state_is_uninitialized( &nonce_state ) ) ) {
988 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
989 0 : }
990 :
991 0 : if( FD_UNLIKELY( memcmp( &nonce_state.inner.initialized.durable_nonce, recent_blockhash, sizeof(fd_hash_t) ) ) ) {
992 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_WRONG_NONCE;
993 0 : }
994 :
995 : /* Finally check that the nonce is authorized by seeing if any accounts in
996 : the nonce instruction are signers. This is a successful exit case. */
997 0 : for( ushort i=0; i<txn_instr->acct_cnt; ++i ) {
998 0 : if( fd_txn_is_signer( TXN( txn_in->txn ), (int)instr_accts[i] ) ) {
999 0 : if( fd_pubkey_eq( &txn_out->accounts.keys[ instr_accts[i] ], &state->inner.current.inner.initialized.authority ) ) {
1000 : /*
1001 : Mark nonce account to make sure that we modify and hash the
1002 : account even if the transaction failed to execute
1003 : successfully.
1004 : */
1005 0 : txn_out->accounts.nonce_idx_in_txn = instr_accts[ 0 ];
1006 : /*
1007 : Now figure out the state that the nonce account should
1008 : advance to.
1009 : */
1010 0 : fd_accdb_ro_t nonce_ro[1];
1011 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( runtime->accdb, nonce_ro, &xid, &txn_out->accounts.keys[ instr_accts[ 0UL ] ] ) ) ) {
1012 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
1013 0 : }
1014 :
1015 0 : fd_blockhashes_t const * blockhashes = &bank->f.block_hash_queue;
1016 0 : fd_blockhash_info_t const * last_bhash_info = fd_blockhashes_peek_last( blockhashes );
1017 0 : FD_TEST( last_bhash_info ); /* Agave panics here if the blockhash queue is empty */
1018 :
1019 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/runtime/src/bank/check_transactions.rs#L217-L221*/
1020 0 : fd_nonce_state_versions_t new_state = {
1021 0 : .discriminant = fd_nonce_state_versions_enum_current,
1022 0 : .inner = { .current = {
1023 0 : .discriminant = fd_nonce_state_enum_initialized,
1024 0 : .inner = { .initialized = {
1025 0 : .authority = state->inner.current.inner.initialized.authority,
1026 0 : .durable_nonce = next_durable_nonce,
1027 0 : .fee_calculator = {
1028 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/runtime/src/bank/check_transactions.rs#L88-L90 */
1029 0 : .lamports_per_signature = last_bhash_info->fee_calculator.lamports_per_signature
1030 0 : }
1031 0 : } }
1032 0 : } }
1033 0 : };
1034 0 : if( FD_UNLIKELY( fd_nonce_state_versions_size( &new_state ) > FD_SYSTEM_PROGRAM_NONCE_DLEN ) ) {
1035 0 : FD_LOG_CRIT(( "fd_nonce_state_versions_size( &new_state ) %lu > FD_SYSTEM_PROGRAM_NONCE_DLEN %lu", fd_nonce_state_versions_size( &new_state ), FD_SYSTEM_PROGRAM_NONCE_DLEN ));
1036 0 : }
1037 : /* make_modifiable uses the old length for the data copy */
1038 0 : uchar * borrowed_account_data = txn_out->accounts.rollback_nonce_mem;
1039 0 : if( FD_UNLIKELY( !borrowed_account_data ) ) {
1040 0 : FD_LOG_CRIT(( "Failed to allocate memory for nonce account" ));
1041 0 : }
1042 0 : fd_memcpy( borrowed_account_data,
1043 0 : nonce_ro->meta,
1044 0 : sizeof(fd_account_meta_t) );
1045 0 : fd_memcpy( borrowed_account_data+sizeof(fd_account_meta_t),
1046 0 : fd_accdb_ref_data_const( nonce_ro ),
1047 0 : fd_accdb_ref_data_sz ( nonce_ro ) );
1048 :
1049 0 : txn_out->accounts.rollback_nonce = (fd_account_meta_t *)borrowed_account_data;
1050 0 : fd_accdb_close_ro( runtime->accdb, nonce_ro );
1051 :
1052 0 : if( FD_UNLIKELY( fd_nonce_state_versions_size( &new_state )>txn_out->accounts.rollback_nonce->dlen ) ) {
1053 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
1054 0 : }
1055 0 : do {
1056 0 : fd_bincode_encode_ctx_t encode_ctx =
1057 0 : { .data = fd_account_data( txn_out->accounts.rollback_nonce ),
1058 0 : .dataend = fd_account_data( txn_out->accounts.rollback_nonce ) + txn_out->accounts.rollback_nonce->dlen };
1059 0 : int err = fd_nonce_state_versions_encode( &new_state, &encode_ctx );
1060 0 : if( FD_UNLIKELY( err ) ) {
1061 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
1062 0 : }
1063 0 : } while(0);
1064 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
1065 0 : }
1066 0 : }
1067 0 : }
1068 : /* This means that the blockhash was not found */
1069 0 : return FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR;
1070 :
1071 0 : }
|