LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_block_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 449 0.0 %
Date: 2025-12-16 05:08:26 Functions: 0 10 0.0 %

          Line data    Source code
       1             : #include "fd_solfuzz_private.h"
       2             : #include "../fd_cost_tracker.h"
       3             : #include "fd_txn_harness.h"
       4             : #include "../fd_runtime.h"
       5             : #include "../fd_system_ids.h"
       6             : #include "../fd_txn_account.h"
       7             : #include "../fd_runtime_stack.h"
       8             : #include "../program/fd_stake_program.h"
       9             : #include "../program/fd_vote_program.h"
      10             : #include "../sysvar/fd_sysvar_epoch_schedule.h"
      11             : #include "../sysvar/fd_sysvar_rent.h"
      12             : #include "../sysvar/fd_sysvar_recent_hashes.h"
      13             : #include "../../accdb/fd_accdb_impl_v1.h"
      14             : #include "../../log_collector/fd_log_collector.h"
      15             : #include "../../rewards/fd_rewards.h"
      16             : #include "../../stakes/fd_stakes.h"
      17             : #include "../../types/fd_types.h"
      18             : #include "../../../disco/pack/fd_pack.h"
      19             : #include "generated/block.pb.h"
      20             : #include "../../capture/fd_capture_ctx.h"
      21             : #include "../../capture/fd_solcap_writer.h"
      22             : 
      23             : /* Templatized leader schedule sort helper functions */
      24             : typedef struct {
      25             :   fd_pubkey_t pk;
      26             :   ulong       sched_pos; /* track original position in sched[] */
      27             : } pk_with_pos_t;
      28             : 
      29             : #define SORT_NAME        sort_pkpos
      30           0 : #define SORT_KEY_T       pk_with_pos_t
      31           0 : #define SORT_BEFORE(a,b) (memcmp(&(a).pk, &(b).pk, sizeof(fd_pubkey_t))<0)
      32             : #include "../../../util/tmpl/fd_sort.c"  /* generates templatized sort_pkpos_*() APIs */
      33             : 
      34             : /* Fixed leader schedule hash seed (consistent with solfuzz-agave) */
      35           0 : #define LEADER_SCHEDULE_HASH_SEED 0xDEADFACEUL
      36             : 
      37             : /* Stripped down version of fd_refresh_vote_accounts that simply
      38             :    refreshes the stake delegation amount for each of the vote accounts
      39             :    using the stake delegations cache. */
      40             : static void
      41             : fd_solfuzz_block_refresh_vote_accounts( fd_vote_states_t *       vote_states,
      42             :                                         fd_vote_states_t *       vote_states_prev,
      43             :                                         fd_vote_states_t *       vote_states_prev_prev,
      44             :                                         fd_stake_delegations_t * stake_delegations,
      45           0 :                                         ulong                    epoch ) {
      46           0 :   fd_stake_delegations_iter_t iter_[1];
      47           0 :   for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
      48           0 :        !fd_stake_delegations_iter_done( iter );
      49           0 :        fd_stake_delegations_iter_next( iter ) ) {
      50           0 :     fd_stake_delegation_t * node = fd_stake_delegations_iter_ele( iter );
      51             : 
      52           0 :     fd_pubkey_t * voter_pubkey = &node->vote_account;
      53           0 :     ulong         stake        = node->stake;
      54             : 
      55             :     /* Find the voter in the vote accounts cache and update their
      56             :        delegation amount */
      57           0 :     fd_vote_state_ele_t * vote_state = fd_vote_states_query( vote_states, voter_pubkey );
      58           0 :     if( !vote_state ) continue;
      59             : 
      60           0 :     vote_state->stake     += stake;
      61           0 :     vote_state->stake_t_1 += stake;
      62           0 :     vote_state->stake_t_2 += stake;
      63           0 :   }
      64             : 
      65             :   /* We need to set the stake_t_2 for the vote accounts in the vote
      66             :      states cache.  An important edge case to handle is if the current
      67             :      epoch is less than 2, that means we should use the current stakes
      68             :      because the stake_t_2 field is not yet populated. */
      69           0 :   fd_vote_states_iter_t vs_iter_[1];
      70           0 :   for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( vs_iter_, vote_states_prev_prev );
      71           0 :        !fd_vote_states_iter_done( iter );
      72           0 :        fd_vote_states_iter_next( iter ) ) {
      73           0 :     fd_vote_state_ele_t * vote_state = fd_vote_states_iter_ele( iter );
      74           0 :     fd_vote_state_ele_t * vote_state_prev_prev = fd_vote_states_query( vote_states_prev_prev, &vote_state->vote_account );
      75           0 :     ulong t_2_stake = !!vote_state_prev_prev ? vote_state_prev_prev->stake : 0UL;
      76           0 :     vote_state->stake_t_2 = epoch>=2UL ? t_2_stake : vote_state->stake;
      77           0 :     vote_state->stake_t_2 = vote_state->stake;
      78           0 :   }
      79             : 
      80             :   /* Set stake_t_1 for the vote accounts in the vote states cache. */
      81           0 :   for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( vs_iter_, vote_states_prev );
      82           0 :        !fd_vote_states_iter_done( iter );
      83           0 :        fd_vote_states_iter_next( iter ) ) {
      84           0 :     fd_vote_state_ele_t * vote_state = fd_vote_states_iter_ele( iter );
      85           0 :     fd_vote_state_ele_t * vote_state_prev = fd_vote_states_query( vote_states_prev, &vote_state->vote_account );
      86           0 :     ulong t_1_stake = !!vote_state_prev ? vote_state_prev->stake : 0UL;
      87           0 :     vote_state->stake_t_1 = epoch>=1UL ? t_1_stake : vote_state->stake;
      88           0 :     vote_state->stake_t_1 = vote_state->stake;
      89           0 :   }
      90           0 : }
      91             : 
      92             : /* Registers a single vote account into the current votes cache.  The
      93             :    entry is derived from the current present account state.  This
      94             :    function also registers a vote timestamp for the vote account. */
      95             : static void
      96             : fd_solfuzz_block_register_vote_account( fd_funk_t  *              funk,
      97             :                                         fd_funk_txn_xid_t const * xid,
      98             :                                         fd_vote_states_t *        vote_states,
      99           0 :                                         fd_pubkey_t *             pubkey ) {
     100           0 :   fd_txn_account_t acc[1];
     101           0 :   if( FD_UNLIKELY( fd_txn_account_init_from_funk_readonly( acc, pubkey, funk, xid ) ) ) {
     102           0 :     return;
     103           0 :   }
     104             : 
     105             :   /* Account must be owned by the vote program */
     106           0 :   if( memcmp( fd_txn_account_get_owner( acc ), fd_solana_vote_program_id.key, sizeof(fd_pubkey_t) ) ) {
     107           0 :     return;
     108           0 :   }
     109             : 
     110             :   /* Account must have > 0 lamports */
     111           0 :   if( fd_txn_account_get_lamports( acc )==0UL ) {
     112           0 :     return;
     113           0 :   }
     114             : 
     115             :   /* Account must be initialized correctly */
     116           0 :   if( FD_UNLIKELY( !fd_vote_state_versions_is_correct_and_initialized( acc ) ) ) {
     117           0 :     return;
     118           0 :   }
     119             : 
     120           0 :   fd_vote_states_update_from_account(
     121           0 :       vote_states,
     122           0 :       acc->pubkey,
     123           0 :       fd_txn_account_get_data( acc ),
     124           0 :       fd_txn_account_get_data_len( acc ) );
     125           0 : }
     126             : 
     127             : /* Stores an entry in the stake delegations cache for the given vote
     128             :    account.  Deserializes and uses the present account state to derive
     129             :    delegation information. */
     130             : static void
     131             : fd_solfuzz_block_register_stake_delegation( fd_funk_t *               funk,
     132             :                                             fd_funk_txn_xid_t const * xid,
     133             :                                             fd_stake_delegations_t *  stake_delegations,
     134           0 :                                             fd_pubkey_t *             pubkey ) {
     135           0 :  fd_txn_account_t acc[1];
     136           0 :   if( FD_UNLIKELY( fd_txn_account_init_from_funk_readonly( acc, pubkey, funk, xid ) ) ) {
     137           0 :     return;
     138           0 :   }
     139             : 
     140             :   /* Account must be owned by the stake program */
     141           0 :   if( memcmp( fd_txn_account_get_owner( acc ), fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) {
     142           0 :     return;
     143           0 :   }
     144             : 
     145             :   /* Account must have > 0 lamports */
     146           0 :   if( fd_txn_account_get_lamports( acc )==0UL ) {
     147           0 :     return;
     148           0 :   }
     149             : 
     150             :   /* Stake state must exist and be initialized correctly */
     151           0 :   fd_stake_state_v2_t stake_state;
     152           0 :   if( FD_UNLIKELY( fd_stake_get_state( acc, &stake_state ) || !fd_stake_state_v2_is_stake( &stake_state ) ) ) {
     153           0 :     return;
     154           0 :   }
     155             : 
     156             :   /* Skip 0-stake accounts */
     157           0 :   if( FD_UNLIKELY( stake_state.inner.stake.stake.delegation.stake==0UL ) ) {
     158           0 :     return;
     159           0 :   }
     160             : 
     161             :   /* Nothing to do if the account already exists in the cache */
     162           0 :   fd_stake_delegations_update(
     163           0 :       stake_delegations,
     164           0 :       pubkey,
     165           0 :       &stake_state.inner.stake.stake.delegation.voter_pubkey,
     166           0 :       stake_state.inner.stake.stake.delegation.stake,
     167           0 :       stake_state.inner.stake.stake.delegation.activation_epoch,
     168           0 :       stake_state.inner.stake.stake.delegation.deactivation_epoch,
     169           0 :       stake_state.inner.stake.stake.credits_observed,
     170           0 :       stake_state.inner.stake.stake.delegation.warmup_cooldown_rate );
     171           0 : }
     172             : 
     173             : /* Common helper method for populating a previous epoch's vote cache. */
     174             : static void
     175             : fd_solfuzz_pb_block_update_prev_epoch_votes_cache( fd_vote_states_t *            vote_states,
     176             :                                                    fd_exec_test_vote_account_t * vote_accounts,
     177             :                                                    pb_size_t                     vote_accounts_cnt,
     178             :                                                    fd_runtime_stack_t *          runtime_stack,
     179             :                                                    fd_spad_t *                   spad,
     180           0 :                                                    uchar                         is_t_1 ) {
     181           0 :   FD_SPAD_FRAME_BEGIN( spad ) {
     182           0 :     for( uint i=0U; i<vote_accounts_cnt; i++ ) {
     183           0 :       fd_exec_test_acct_state_t * vote_account  = &vote_accounts[i].vote_account;
     184           0 :       ulong                       stake         = vote_accounts[i].stake;
     185           0 :       uchar *                     vote_data     = vote_account->data->bytes;
     186           0 :       ulong                       vote_data_len = vote_account->data->size;
     187           0 :       fd_pubkey_t                 vote_address  = {0};
     188           0 :       fd_memcpy( &vote_address, vote_account->address, sizeof(fd_pubkey_t) );
     189             : 
     190             :       /* Try decoding the vote state from the account data. If it isn't
     191             :          decodable, don't try inserting it into the cache. */
     192           0 :       fd_vote_state_versioned_t * res = fd_bincode_decode_spad(
     193           0 :           vote_state_versioned, spad,
     194           0 :           vote_data,
     195           0 :           vote_data_len,
     196           0 :           NULL );
     197           0 :       if( res==NULL ) continue;
     198           0 :       if( res->discriminant==fd_vote_state_versioned_enum_v0_23_5 ) continue;
     199             : 
     200           0 :       fd_vote_states_update_from_account( vote_states, &vote_address, vote_data, vote_data_len );
     201           0 :       fd_vote_state_ele_t * vote_state = fd_vote_states_query( vote_states, &vote_address );
     202           0 :       vote_state->stake     += stake;
     203           0 :       vote_state->stake_t_1 += stake;
     204           0 :       vote_state->stake_t_2 += stake;
     205             : 
     206           0 :       if( !is_t_1 ) continue;
     207             : 
     208             :       /* Update vote credits for T-1 */
     209           0 :       fd_vote_epoch_credits_t * epoch_credits = NULL;
     210           0 :       switch( res->discriminant ) {
     211           0 :         case fd_vote_state_versioned_enum_v0_23_5:
     212           0 :           epoch_credits = res->inner.v0_23_5.epoch_credits;
     213           0 :           break;
     214           0 :         case fd_vote_state_versioned_enum_v1_14_11:
     215           0 :           epoch_credits = res->inner.v1_14_11.epoch_credits;
     216           0 :           break;
     217           0 :         case fd_vote_state_versioned_enum_current:
     218           0 :           epoch_credits = res->inner.current.epoch_credits;
     219           0 :           break;
     220           0 :         default:
     221           0 :           __builtin_unreachable();
     222           0 :       }
     223             : 
     224           0 :       fd_vote_state_credits_t * vote_credits = &runtime_stack->stakes.vote_credits[ vote_state->idx ];
     225           0 :       vote_credits->credits_cnt = 0UL;
     226           0 :       for( deq_fd_vote_epoch_credits_t_iter_t iter = deq_fd_vote_epoch_credits_t_iter_init( epoch_credits );
     227           0 :            !deq_fd_vote_epoch_credits_t_iter_done( epoch_credits, iter );
     228           0 :            iter = deq_fd_vote_epoch_credits_t_iter_next( epoch_credits, iter ) ) {
     229           0 :         fd_vote_epoch_credits_t const * credit_ele = deq_fd_vote_epoch_credits_t_iter_ele_const( epoch_credits, iter );
     230           0 :         vote_credits->epoch[ vote_credits->credits_cnt ]        = (ushort)credit_ele->epoch;
     231           0 :         vote_credits->credits[ vote_credits->credits_cnt ]      = credit_ele->credits;
     232           0 :         vote_credits->prev_credits[ vote_credits->credits_cnt ] = credit_ele->prev_credits;
     233           0 :         vote_credits->credits_cnt++;
     234           0 :       }
     235           0 :     }
     236           0 :   } FD_SPAD_FRAME_END;
     237           0 : }
     238             : 
     239             : static void
     240           0 : fd_solfuzz_pb_block_ctx_destroy( fd_solfuzz_runner_t * runner ) {
     241           0 :   fd_accdb_clear( runner->accdb_admin );
     242           0 :   fd_progcache_clear( runner->progcache_admin );
     243             : 
     244             :   /* In order to check for leaks in the workspace, we need to compact the
     245             :      allocators. Without doing this, empty superblocks may be retained
     246             :      by the fd_alloc instance, which mean we cannot check for leaks. */
     247           0 :   fd_alloc_compact( runner->accdb_admin->funk->alloc );
     248           0 :   fd_alloc_compact( runner->progcache_admin->funk->alloc );
     249           0 : }
     250             : 
     251             : /* Sets up block execution context from an input test case to execute
     252             :    against the runtime.  Returns block_info on success and NULL on
     253             :    failure. */
     254             : static fd_txn_p_t *
     255             : fd_solfuzz_pb_block_ctx_create( fd_solfuzz_runner_t *                runner,
     256             :                                 fd_exec_test_block_context_t const * test_ctx,
     257             :                                 ulong *                              out_txn_cnt,
     258           0 :                                 fd_hash_t *                          poh ) {
     259           0 :   fd_accdb_user_t * accdb = runner->accdb;
     260           0 :   fd_funk_t *       funk  = fd_accdb_user_v1_funk( runner->accdb );
     261           0 :   fd_bank_t *       bank  = runner->bank;
     262           0 :   fd_banks_t *      banks = runner->banks;
     263             : 
     264           0 :   fd_runtime_stack_t * runtime_stack = runner->runtime_stack;
     265             : 
     266           0 :   fd_banks_clear_bank( banks, bank );
     267             : 
     268             :   /* Generate unique ID for funk txn */
     269           0 :   fd_funk_txn_xid_t xid[1] = {{ .ul={ LONG_MAX,LONG_MAX } }};
     270             : 
     271             :   /* Create temporary funk transaction and slot / epoch contexts */
     272           0 :   fd_funk_txn_xid_t parent_xid; fd_funk_txn_xid_set_root( &parent_xid );
     273           0 :   fd_accdb_attach_child( runner->accdb_admin, &parent_xid, xid );
     274           0 :   fd_progcache_txn_attach_child( runner->progcache_admin, &parent_xid, xid );
     275             : 
     276             :   /* Restore feature flags */
     277           0 :   fd_features_t features = {0};
     278           0 :   if( !fd_solfuzz_pb_restore_features( &features, &test_ctx->epoch_ctx.features ) ) {
     279           0 :     return NULL;
     280           0 :   }
     281           0 :   fd_bank_features_set( bank, features );
     282             : 
     283             :   /* Set up slot context */
     284           0 :   ulong slot        = test_ctx->slot_ctx.slot;
     285           0 :   ulong parent_slot = test_ctx->slot_ctx.prev_slot;
     286             : 
     287           0 :   fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
     288           0 :   fd_memcpy( bank_hash, test_ctx->slot_ctx.parent_bank_hash, sizeof(fd_hash_t) );
     289             : 
     290             :   /* All bank mgr stuff here. */
     291             : 
     292           0 :   fd_bank_slot_set( bank, slot );
     293             : 
     294           0 :   fd_bank_parent_slot_set( bank, parent_slot );
     295             : 
     296           0 :   fd_bank_block_height_set( bank, test_ctx->slot_ctx.block_height );
     297             : 
     298           0 :   fd_bank_capitalization_set( bank, test_ctx->slot_ctx.prev_epoch_capitalization );
     299             : 
     300             :   // self.max_tick_height = (self.slot + 1) * self.ticks_per_slot;
     301           0 :   fd_bank_hashes_per_tick_set( bank, test_ctx->epoch_ctx.hashes_per_tick );
     302             : 
     303           0 :   fd_bank_ticks_per_slot_set( bank, test_ctx->epoch_ctx.ticks_per_slot );
     304             : 
     305           0 :   fd_bank_ns_per_slot_set( bank, (fd_w_u128_t) { .ul={ 400000000,0 } } ); // TODO: restore from input
     306             : 
     307           0 :   fd_bank_genesis_creation_time_set( bank, test_ctx->epoch_ctx.genesis_creation_time );
     308             : 
     309           0 :   fd_bank_slots_per_year_set( bank, test_ctx->epoch_ctx.slots_per_year );
     310             : 
     311           0 :   fd_bank_parent_signature_cnt_set( bank, test_ctx->slot_ctx.parent_signature_count );
     312             : 
     313           0 :   fd_fee_rate_governor_t * fee_rate_governor = fd_bank_fee_rate_governor_modify( bank );
     314           0 :   *fee_rate_governor = (fd_fee_rate_governor_t){
     315           0 :     .target_lamports_per_signature = test_ctx->slot_ctx.fee_rate_governor.target_lamports_per_signature,
     316           0 :     .target_signatures_per_slot    = test_ctx->slot_ctx.fee_rate_governor.target_signatures_per_slot,
     317           0 :     .min_lamports_per_signature    = test_ctx->slot_ctx.fee_rate_governor.min_lamports_per_signature,
     318           0 :     .max_lamports_per_signature    = test_ctx->slot_ctx.fee_rate_governor.max_lamports_per_signature,
     319           0 :     .burn_percent                  = (uchar)test_ctx->slot_ctx.fee_rate_governor.burn_percent
     320           0 :   };
     321             :   /* https://github.com/firedancer-io/solfuzz-agave/blob/agave-v3.0.3/src/block.rs#L393-L396 */
     322           0 :   fd_bank_rbh_lamports_per_sig_set( bank, FD_RUNTIME_FEE_STRUCTURE_LAMPORTS_PER_SIGNATURE );
     323             : 
     324           0 :   fd_inflation_t * inflation = fd_bank_inflation_modify( bank );
     325           0 :   *inflation = (fd_inflation_t){
     326           0 :     .initial         = test_ctx->epoch_ctx.inflation.initial,
     327           0 :     .terminal        = test_ctx->epoch_ctx.inflation.terminal,
     328           0 :     .taper           = test_ctx->epoch_ctx.inflation.taper,
     329           0 :     .foundation      = test_ctx->epoch_ctx.inflation.foundation,
     330           0 :     .foundation_term = test_ctx->epoch_ctx.inflation.foundation_term
     331           0 :   };
     332             : 
     333           0 :   fd_bank_block_height_set( bank, test_ctx->slot_ctx.block_height );
     334             : 
     335             :   /* Initialize the current running epoch stake and vote accounts */
     336             : 
     337           0 :   fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
     338           0 :   vote_states = fd_vote_states_join( fd_vote_states_new( vote_states, FD_RUNTIME_MAX_VOTE_ACCOUNTS, 999UL ) );
     339           0 :   fd_bank_vote_states_end_locking_modify( bank );
     340             : 
     341           0 :   fd_vote_states_t * vote_states_prev = fd_bank_vote_states_prev_locking_modify( bank );
     342           0 :   vote_states_prev = fd_vote_states_join( fd_vote_states_new( vote_states_prev, FD_RUNTIME_MAX_VOTE_ACCOUNTS, 999UL ) );
     343           0 :   fd_bank_vote_states_prev_end_locking_modify( bank );
     344             : 
     345           0 :   fd_vote_states_t * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_modify( bank );
     346           0 :   vote_states_prev_prev = fd_vote_states_join( fd_vote_states_new( vote_states_prev_prev, FD_RUNTIME_MAX_VOTE_ACCOUNTS, 999UL ) );
     347           0 :   fd_bank_vote_states_prev_prev_end_locking_modify( bank );
     348             : 
     349           0 :   fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
     350           0 :   stake_delegations = fd_stake_delegations_join( fd_stake_delegations_new( stake_delegations, FD_RUNTIME_MAX_STAKE_ACCOUNTS, 0 ) );
     351             : 
     352             :   /* Load in all accounts with > 0 lamports provided in the context. The input expects unique account pubkeys. */
     353           0 :   vote_states = fd_bank_vote_states_locking_modify( bank );
     354           0 :   for( ushort i=0; i<test_ctx->acct_states_count; i++ ) {
     355           0 :     fd_txn_account_t acc[1];
     356           0 :     fd_solfuzz_pb_load_account( acc, accdb, xid, &test_ctx->acct_states[i], 1 );
     357             : 
     358             :     /* Update vote accounts cache for epoch T */
     359           0 :     fd_pubkey_t pubkey;
     360           0 :     memcpy( &pubkey, test_ctx->acct_states[i].address, sizeof(fd_pubkey_t) );
     361           0 :     fd_solfuzz_block_register_vote_account(
     362           0 :         funk,
     363           0 :         xid,
     364           0 :         vote_states,
     365           0 :         &pubkey );
     366             : 
     367             :     /* Update the stake delegations cache for epoch T */
     368           0 :     fd_solfuzz_block_register_stake_delegation( funk, xid, stake_delegations, &pubkey );
     369           0 :   }
     370             : 
     371             :   /* Zero out vote stakes to avoid leakage across tests */
     372           0 :   fd_vote_states_reset_stakes( vote_states );
     373             : 
     374             :   /* Finish init epoch bank sysvars */
     375           0 :   fd_epoch_schedule_t epoch_schedule_[1];
     376           0 :   fd_epoch_schedule_t * epoch_schedule = fd_sysvar_epoch_schedule_read( funk, xid, epoch_schedule_ );
     377           0 :   FD_TEST( epoch_schedule );
     378           0 :   fd_bank_epoch_schedule_set( bank, *epoch_schedule );
     379             : 
     380           0 :   fd_rent_t rent[1];
     381           0 :   FD_TEST( fd_sysvar_rent_read( funk, xid, rent ) );
     382           0 :   fd_bank_rent_set( bank, *rent );
     383             : 
     384             :   /* Current epoch gets updated in process_new_epoch, so use the epoch
     385             :      from the parent slot */
     386           0 :   fd_bank_epoch_set( bank, fd_slot_to_epoch( epoch_schedule, parent_slot, NULL ) );
     387             : 
     388             :   /* Update vote cache for epoch T-1 */
     389           0 :   vote_states_prev = fd_bank_vote_states_prev_locking_modify( bank );
     390           0 :   fd_solfuzz_pb_block_update_prev_epoch_votes_cache(
     391           0 :       vote_states_prev,
     392           0 :       test_ctx->epoch_ctx.vote_accounts_t_1,
     393           0 :       test_ctx->epoch_ctx.vote_accounts_t_1_count,
     394           0 :       runtime_stack,
     395           0 :       runner->spad,
     396           0 :       1 );
     397           0 :   fd_bank_vote_states_prev_end_locking_modify( bank );
     398             : 
     399             :   /* Update vote cache for epoch T-2 */
     400           0 :   vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_modify( bank );
     401           0 :   fd_solfuzz_pb_block_update_prev_epoch_votes_cache(
     402           0 :       vote_states_prev_prev,
     403           0 :       test_ctx->epoch_ctx.vote_accounts_t_2,
     404           0 :       test_ctx->epoch_ctx.vote_accounts_t_2_count,
     405           0 :       runtime_stack,
     406           0 :       runner->spad,
     407           0 :       0 );
     408             : 
     409             :   /* Refresh vote accounts to calculate stake delegations */
     410           0 :   fd_solfuzz_block_refresh_vote_accounts(
     411           0 :     vote_states,
     412           0 :     vote_states_prev,
     413           0 :     vote_states_prev_prev,
     414           0 :     stake_delegations,
     415           0 :     fd_bank_epoch_get( bank ) );
     416           0 :   fd_bank_vote_states_end_locking_modify( bank );
     417             : 
     418           0 :   fd_bank_vote_states_prev_prev_end_locking_modify( bank );
     419             : 
     420             :   /* Update leader schedule */
     421           0 :   fd_runtime_update_leaders( bank, runtime_stack );
     422             : 
     423             :   /* Initialize the blockhash queue and recent blockhashes sysvar from the input blockhash queue */
     424           0 :   ulong blockhash_seed; FD_TEST( fd_rng_secure( &blockhash_seed, sizeof(ulong) ) );
     425           0 :   fd_blockhashes_init( fd_bank_block_hash_queue_modify( bank ), blockhash_seed );
     426             : 
     427             :   /* TODO: We might need to load this in from the input. We also need to
     428             :      size this out for worst case, but this also blows up the memory
     429             :      requirement. */
     430             :   /* Allocate all the memory for the rent fresh accounts list */
     431             : 
     432             :   // Set genesis hash to {0}
     433           0 :   fd_hash_t * genesis_hash = fd_bank_genesis_hash_modify( bank );
     434           0 :   fd_memset( genesis_hash->hash, 0, sizeof(fd_hash_t) );
     435             : 
     436             :   // Use the latest lamports per signature
     437           0 :   uchar __attribute__((aligned(FD_SYSVAR_RECENT_HASHES_ALIGN))) rbh_mem[FD_SYSVAR_RECENT_HASHES_FOOTPRINT];
     438           0 :   fd_recent_block_hashes_t const * rbh = fd_sysvar_recent_hashes_read( funk, xid, rbh_mem );
     439           0 :   if( rbh && !deq_fd_block_block_hash_entry_t_empty( rbh->hashes ) ) {
     440           0 :     fd_block_block_hash_entry_t const * last = deq_fd_block_block_hash_entry_t_peek_head_const( rbh->hashes );
     441           0 :     if( last && last->fee_calculator.lamports_per_signature!=0UL ) {
     442           0 :       fd_bank_rbh_lamports_per_sig_set( bank, last->fee_calculator.lamports_per_signature );
     443           0 :     }
     444           0 :   }
     445             : 
     446             :   /* Make a new funk transaction since we're done loading in accounts for context */
     447           0 :   fd_funk_txn_xid_t fork_xid = { .ul = { slot, 0UL } };
     448           0 :   fd_accdb_attach_child        ( runner->accdb_admin,     xid, &fork_xid );
     449           0 :   fd_progcache_txn_attach_child( runner->progcache_admin, xid, &fork_xid );
     450           0 :   xid[0] = fork_xid;
     451             : 
     452             :   /* Set the initial lthash from the input since we're in a new Funk txn */
     453           0 :   fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
     454           0 :   fd_memcpy( lthash, test_ctx->slot_ctx.parent_lthash, sizeof(fd_lthash_value_t) );
     455           0 :   fd_bank_lthash_end_locking_modify( bank );
     456             : 
     457             :   // Populate blockhash queue and recent blockhashes sysvar
     458           0 :   for( ushort i=0; i<test_ctx->blockhash_queue_count; ++i ) {
     459           0 :     fd_hash_t hash;
     460           0 :     memcpy( &hash, test_ctx->blockhash_queue[i]->bytes, sizeof(fd_hash_t) );
     461           0 :     fd_bank_poh_set( bank, hash );
     462           0 :     fd_sysvar_recent_hashes_update( bank, accdb, xid, NULL ); /* appends an entry */
     463           0 :   }
     464             : 
     465             :   /* Set the poh from the input.  This is the blockhash that will get
     466             :      inserted after. */
     467           0 :   memcpy( poh, test_ctx->slot_ctx.poh, sizeof(fd_hash_t) );
     468             : 
     469             :   /* Restore sysvar cache */
     470           0 :   fd_sysvar_cache_restore_fuzz( bank, funk, xid );
     471             : 
     472             :   /* Prepare raw transaction pointers and block / microblock infos */
     473           0 :   ulong        txn_cnt  = test_ctx->txns_count;
     474           0 :   fd_txn_p_t * txn_ptrs = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), txn_cnt * sizeof(fd_txn_p_t) );
     475           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
     476           0 :     fd_txn_p_t * txn    = &txn_ptrs[i];
     477           0 :     ulong        msg_sz = fd_solfuzz_pb_txn_serialize( txn->payload, &test_ctx->txns[i] );
     478             : 
     479             :     // Reject any transactions over 1232 bytes
     480           0 :     if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) {
     481           0 :       return NULL;
     482           0 :     }
     483           0 :     txn->payload_sz = msg_sz;
     484             : 
     485             :     // Reject any transactions that cannot be parsed
     486           0 :     if( FD_UNLIKELY( !fd_txn_parse( txn->payload, msg_sz, TXN( txn ), NULL ) ) ) {
     487           0 :       return NULL;
     488           0 :     }
     489           0 :   }
     490             : 
     491           0 :   *out_txn_cnt = txn_cnt;
     492           0 :   return txn_ptrs;
     493           0 : }
     494             : 
     495             : /* Takes in a list of txn_p_t created from
     496             :    fd_runtime_fuzz_block_ctx_create and executes it against the runtime.
     497             :    Returns the execution result. */
     498             : static int
     499             : fd_solfuzz_block_ctx_exec( fd_solfuzz_runner_t * runner,
     500             :                            fd_txn_p_t *          txn_ptrs,
     501             :                            ulong                 txn_cnt,
     502           0 :                            fd_hash_t *           poh ) {
     503           0 :   int res = 0;
     504             : 
     505             :   // Prepare. Execute. Finalize.
     506           0 :   FD_SPAD_FRAME_BEGIN( runner->spad ) {
     507           0 :     fd_capture_ctx_t * capture_ctx = NULL;
     508             : 
     509           0 :     if( runner->solcap ) {
     510           0 :       void * capture_ctx_mem = fd_spad_alloc( runner->spad, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
     511           0 :       capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( capture_ctx_mem ) );
     512           0 :       if( FD_UNLIKELY( !capture_ctx ) ) {
     513           0 :         FD_LOG_ERR(( "Failed to initialize capture_ctx" ));
     514           0 :       }
     515             : 
     516           0 :       fd_capture_link_file_t * capture_link_file =
     517           0 :         fd_spad_alloc( runner->spad, alignof(fd_capture_link_file_t), sizeof(fd_capture_link_file_t) );
     518           0 :       if( FD_UNLIKELY( !capture_link_file ) ) {
     519           0 :         FD_LOG_ERR(( "Failed to allocate capture_link_file" ));
     520           0 :       }
     521             : 
     522           0 :       capture_link_file->base.vt = &fd_capture_link_file_vt;
     523             : 
     524           0 :       int solcap_fd = (int)(ulong)runner->solcap_file;
     525           0 :       capture_link_file->fd         = solcap_fd;
     526           0 :       capture_ctx->capture_link      = &capture_link_file->base;
     527           0 :       capture_ctx->capctx_type.file   = capture_link_file;
     528           0 :       capture_ctx->solcap_start_slot = fd_bank_slot_get( runner->bank );
     529             : 
     530           0 :       fd_solcap_writer_init( capture_ctx->capture, solcap_fd );
     531           0 :     }
     532             : 
     533           0 :     fd_funk_t * funk = fd_accdb_user_v1_funk( runner->accdb );
     534           0 :     fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( runner->bank ), runner->bank->idx } };
     535             : 
     536           0 :     fd_rewards_recalculate_partitioned_rewards( runner->banks, runner->bank, funk, &xid, runner->runtime_stack, capture_ctx );
     537             : 
     538             :     /* Process new epoch may push a new spad frame onto the runtime spad. We should make sure this frame gets
     539             :        cleared (if it was allocated) before executing the block. */
     540           0 :     int is_epoch_boundary = 0;
     541           0 :     fd_runtime_block_execute_prepare( runner->banks, runner->bank, runner->accdb, runner->runtime_stack, capture_ctx, &is_epoch_boundary );
     542             : 
     543             :     /* Sequential transaction execution */
     544           0 :     for( ulong i=0UL; i<txn_cnt; i++ ) {
     545           0 :       fd_txn_p_t * txn = &txn_ptrs[i];
     546             : 
     547             :       /* Execute the transaction against the runtime */
     548           0 :       res = FD_RUNTIME_EXECUTE_SUCCESS;
     549           0 :       fd_txn_in_t  txn_in = { .txn = txn, .exec_accounts = runner->exec_accounts, .bundle.is_bundle = 0 };
     550           0 :       fd_txn_out_t txn_out;
     551           0 :       fd_runtime_t * runtime = runner->runtime;
     552           0 :       fd_log_collector_t log[1];
     553           0 :       runtime->log.log_collector = log;
     554           0 :       fd_solfuzz_txn_ctx_exec( runner, runtime, &txn_in, &res, &txn_out );
     555           0 :       txn_out.err.exec_err = res;
     556             : 
     557           0 :       if( FD_UNLIKELY( !txn_out.err.is_committable ) ) {
     558           0 :         return 0;
     559           0 :       }
     560             : 
     561             :       /* Finalize the transaction */
     562           0 :       fd_runtime_commit_txn( runtime, runner->bank, &txn_in, &txn_out );
     563             : 
     564           0 :       if( FD_UNLIKELY( !txn_out.err.is_committable ) ) {
     565           0 :         return 0;
     566           0 :       }
     567             : 
     568           0 :     }
     569             : 
     570             :     /* At this point we want to set the poh.  This is what will get
     571             :        updated in the blockhash queue. */
     572           0 :     fd_bank_poh_set( runner->bank, *poh );
     573             :     /* Finalize the block */
     574           0 :     fd_runtime_block_execute_finalize( runner->bank, runner->accdb, capture_ctx );
     575           0 :   } FD_SPAD_FRAME_END;
     576             : 
     577           0 :   return 1;
     578           0 : }
     579             : 
     580             : /* Canonical (Agave-aligned) schedule hash
     581             :    Unique pubkeys referenced by sched, sorted deterministically
     582             :    Per-rotation indices mapped into sorted-uniq array */
     583             : ulong
     584             : fd_solfuzz_block_hash_epoch_leaders( fd_solfuzz_runner_t *      runner,
     585             :                                      fd_epoch_leaders_t const * leaders,
     586             :                                      ulong                      seed,
     587           0 :                                      uchar                      out[16] ) {
     588             :   /* Single contiguous spad allocation for uniq[] and sched_mapped[] */
     589           0 :   void *buf = fd_spad_alloc(
     590           0 :     runner->spad,
     591           0 :     alignof(pk_with_pos_t),
     592           0 :     leaders->sched_cnt*sizeof(pk_with_pos_t) +
     593           0 :     leaders->sched_cnt*sizeof(uint) );
     594             : 
     595           0 :   pk_with_pos_t * tmp          = (pk_with_pos_t *)buf;
     596           0 :   uint          * sched_mapped = (uint *)( tmp + leaders->sched_cnt );
     597             : 
     598             :   /* Gather all pubkeys and original positions from sched[] (skip invalid) */
     599           0 :   ulong gather_cnt = 0UL;
     600           0 :   for( ulong i=0UL; i<leaders->sched_cnt; i++ ) {
     601           0 :     uint idx = leaders->sched[i];
     602           0 :     if( idx>=leaders->pub_cnt ) { /* invalid slot leader */
     603           0 :       sched_mapped[i] = 0U;       /* prefill invalid mapping */
     604           0 :       continue;
     605           0 :     }
     606           0 :     fd_memcpy( &tmp[gather_cnt].pk, &leaders->pub[idx], sizeof(fd_pubkey_t) );
     607           0 :     tmp[gather_cnt].sched_pos = i;
     608           0 :     gather_cnt++;
     609           0 :   }
     610             : 
     611           0 :   if( gather_cnt==0UL ) {
     612             :     /* No leaders => hash:=0, count:=0 */
     613           0 :     fd_memset( out, 0, sizeof(ulong)*2 );
     614           0 :     return 0UL;
     615           0 :   }
     616             : 
     617             :   /* Sort tmp[] by pubkey, note: comparator relies on first struct member */
     618           0 :   sort_pkpos_inplace( tmp, (ulong)gather_cnt );
     619             : 
     620             :   /* Dedupe and assign indices into sched_mapped[] during single pass */
     621           0 :   ulong uniq_cnt = 0UL;
     622           0 :   for( ulong i=0UL; i<gather_cnt; i++ ) {
     623           0 :     if( i==0UL || memcmp( &tmp[i].pk, &tmp[i-1].pk, sizeof(fd_pubkey_t) )!=0 )
     624           0 :       uniq_cnt++;
     625             :     /* uniq_cnt-1 is index in uniq set */
     626           0 :     sched_mapped[tmp[i].sched_pos] = (uint)(uniq_cnt-1UL);
     627           0 :   }
     628             : 
     629             :   /* Reconstruct contiguous uniq[] for hashing */
     630           0 :   fd_pubkey_t *uniq = fd_spad_alloc( runner->spad,
     631           0 :                                      alignof(fd_pubkey_t),
     632           0 :                                      uniq_cnt*sizeof(fd_pubkey_t) );
     633           0 :   {
     634           0 :     ulong write_pos = 0UL;
     635           0 :     for( ulong i=0UL; i<gather_cnt; i++ ) {
     636           0 :       if( i==0UL || memcmp( &tmp[i].pk, &tmp[i-1].pk, sizeof(fd_pubkey_t) )!=0 )
     637           0 :       fd_memcpy( &uniq[write_pos++], &tmp[i].pk, sizeof(fd_pubkey_t) );
     638           0 :     }
     639           0 :   }
     640             : 
     641             :   /* Hash sorted unique pubkeys */
     642           0 :   ulong h1 = fd_hash( seed, uniq, uniq_cnt * sizeof(fd_pubkey_t) );
     643           0 :   fd_memcpy( out, &h1, sizeof(ulong) );
     644             : 
     645             :   /* Hash mapped indices */
     646           0 :   ulong h2 = fd_hash( seed, sched_mapped, leaders->sched_cnt * sizeof(uint) );
     647           0 :   fd_memcpy( out + sizeof(ulong), &h2, sizeof(ulong) );
     648             : 
     649           0 :   return uniq_cnt;
     650           0 : }
     651             : 
     652             : static void
     653             : fd_solfuzz_pb_build_leader_schedule_effects( fd_solfuzz_runner_t *          runner,
     654             :                                              fd_funk_txn_xid_t const *      xid,
     655           0 :                                              fd_exec_test_block_effects_t * effects ) {
     656             :   /* Read epoch schedule sysvar */
     657           0 :   fd_funk_t * funk = fd_accdb_user_v1_funk( runner->accdb );
     658           0 :   fd_epoch_schedule_t es_;
     659           0 :   fd_epoch_schedule_t *sched = fd_sysvar_epoch_schedule_read( funk, xid, &es_ );
     660           0 :   FD_TEST( sched!=NULL );
     661             : 
     662             :   /* We will capture the leader schedule for the current epoch that we
     663             :      are in.  This will capture the leader schedule generated by an
     664             :      epoch boundary if one was crossed. */
     665           0 :   ulong epoch          = fd_bank_epoch_get( runner->bank );
     666           0 :   ulong ls_slot0       = fd_epoch_slot0( sched, epoch );
     667           0 :   ulong slots_in_epoch = fd_epoch_slot_cnt( sched, epoch );
     668             : 
     669           0 :   fd_epoch_leaders_t const * effects_leaders = fd_bank_epoch_leaders_locking_query( runner->bank );
     670             : 
     671             :   /* Fill out effects struct from the Agave epoch info */
     672           0 :   effects->has_leader_schedule               = 1;
     673           0 :   effects->leader_schedule.leaders_epoch     = epoch;
     674           0 :   effects->leader_schedule.leaders_slot0     = ls_slot0;
     675           0 :   effects->leader_schedule.leaders_slot_cnt  = slots_in_epoch;
     676           0 :   effects->leader_schedule.leaders_sched_cnt = slots_in_epoch;
     677           0 :   effects->leader_schedule.leader_pub_cnt    = fd_solfuzz_block_hash_epoch_leaders(
     678           0 :       runner, effects_leaders,
     679           0 :       LEADER_SCHEDULE_HASH_SEED,
     680           0 :       effects->leader_schedule.leader_schedule_hash
     681           0 :   );
     682           0 :   fd_bank_epoch_leaders_end_locking_query( runner->bank );
     683           0 : }
     684             : 
     685             : ulong
     686             : fd_solfuzz_pb_block_run( fd_solfuzz_runner_t * runner,
     687             :                           void const *         input_,
     688             :                           void **              output_,
     689             :                           void *               output_buf,
     690           0 :                           ulong                output_bufsz ) {
     691           0 :   fd_exec_test_block_context_t const * input  = fd_type_pun_const( input_ );
     692           0 :   fd_exec_test_block_effects_t **      output = fd_type_pun( output_ );
     693             : 
     694           0 :   FD_SPAD_FRAME_BEGIN( runner->spad ) {
     695           0 :     ulong txn_cnt;
     696           0 :     fd_hash_t poh = {0};
     697           0 :     fd_txn_p_t * txn_ptrs = fd_solfuzz_pb_block_ctx_create( runner, input, &txn_cnt, &poh );
     698           0 :     if( txn_ptrs==NULL ) {
     699           0 :       fd_solfuzz_pb_block_ctx_destroy( runner );
     700           0 :       return 0;
     701           0 :     }
     702             : 
     703           0 :     fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( runner->bank ), runner->bank->idx } };
     704             : 
     705             :     /* Execute the constructed block against the runtime. */
     706           0 :     int is_committable = fd_solfuzz_block_ctx_exec( runner, txn_ptrs, txn_cnt, &poh );
     707             : 
     708             :     /* Start saving block exec results */
     709           0 :     FD_SCRATCH_ALLOC_INIT( l, output_buf );
     710           0 :     ulong output_end = (ulong)output_buf + output_bufsz;
     711             : 
     712           0 :     fd_exec_test_block_effects_t * effects =
     713           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_block_effects_t),
     714           0 :                                 sizeof(fd_exec_test_block_effects_t) );
     715           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     716           0 :       abort();
     717           0 :     }
     718           0 :     fd_memset( effects, 0, sizeof(fd_exec_test_block_effects_t) );
     719             : 
     720             :     /* Capture error status */
     721           0 :     effects->has_error = !is_committable;
     722             : 
     723             :     /* Capture capitalization */
     724           0 :     effects->slot_capitalization = !effects->has_error ? fd_bank_capitalization_get( runner->bank ) : 0UL;
     725             : 
     726             :     /* Capture hashes */
     727           0 :     fd_hash_t bank_hash = !effects->has_error ? fd_bank_bank_hash_get( runner->bank ) : (fd_hash_t){0};
     728           0 :     fd_memcpy( effects->bank_hash, bank_hash.hash, sizeof(fd_hash_t) );
     729             : 
     730             :     /* Capture cost tracker */
     731           0 :     fd_cost_tracker_t const * cost_tracker = fd_bank_cost_tracker_locking_query( runner->bank );
     732           0 :     effects->has_cost_tracker = 1;
     733           0 :     effects->cost_tracker = (fd_exec_test_cost_tracker_t) {
     734           0 :       .block_cost = cost_tracker ? cost_tracker->block_cost : 0UL,
     735           0 :       .vote_cost  = cost_tracker ? cost_tracker->vote_cost  : 0UL,
     736           0 :     };
     737           0 :     fd_bank_cost_tracker_end_locking_query( runner->bank );
     738             : 
     739             :     /* Effects: build T-epoch (bank epoch), T-stakes ephemeral leaders and report */
     740           0 :     fd_solfuzz_pb_build_leader_schedule_effects( runner, &xid, effects );
     741             : 
     742           0 :     ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     743           0 :     fd_solfuzz_pb_block_ctx_destroy( runner );
     744             : 
     745           0 :     *output = effects;
     746           0 :     return actual_end - (ulong)output_buf;
     747           0 :   } FD_SPAD_FRAME_END;
     748           0 : }

Generated by: LCOV version 1.14