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

Generated by: LCOV version 1.14