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

Generated by: LCOV version 1.14