LCOV - code coverage report
Current view: top level - flamenco/runtime/program - fd_system_program_nonce.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 594 0.0 %
Date: 2025-10-13 04:42:14 Functions: 0 16 0.0 %

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

Generated by: LCOV version 1.14