LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_block_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 356 402 88.6 %
Date: 2026-11-26 02:16:43 Functions: 10 10 100.0 %

          Line data    Source code
       1             : #include "fd_solfuzz_private.h"
       2             : #include "../fd_cost_tracker.h"
       3             : #include "../fd_slot_params.h"
       4             : #include "fd_txn_harness.h"
       5             : #include "../fd_runtime.h"
       6             : #include "../fd_runtime_helpers.h"
       7             : #include "../fd_system_ids.h"
       8             : #include "../fd_runtime_stack_tmpl.h"
       9             : #include "../../stakes/fd_stake_types.h"
      10             : #include "../program/vote/fd_vote_codec.h"
      11             : #include "../sysvar/fd_sysvar_epoch_schedule.h"
      12             : #include "../sysvar/fd_sysvar_stake_history.h"
      13             : #include "../../progcache/fd_progcache_admin.h"
      14             : #include "../../log_collector/fd_log_collector.h"
      15             : #include "../../rewards/fd_rewards.h"
      16             : #include "../../rewards/fd_stake_rewards.h"
      17             : #include "generated/block.pb.h"
      18             : #include "../../capture/fd_capture_ctx.h"
      19             : #include "../../capture/fd_solcap_writer.h"
      20             : #include <stdlib.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    47666207 : #define SORT_KEY_T       pk_with_pos_t
      30    95300742 : #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             : #define SORT_NAME        sort_stake_delegation
      34        1187 : #define SORT_KEY_T       fd_exec_test_stake_delegation_t
      35         782 : #define SORT_BEFORE(a,b) (memcmp((a).stake_account, (b).stake_account, sizeof(fd_pubkey_t))<0)
      36             : #include "../../../util/tmpl/fd_sort.c"
      37             : 
      38             : /* Fixed leader schedule hash seed (consistent with solfuzz-agave) */
      39         611 : #define LEADER_SCHEDULE_HASH_SEED 0xDEADFACEUL
      40             : 
      41             : /* fd_solfuzz_epoch_credit_is_alpenglow_marker returns 1 if ec is the
      42             :    tower->Alpenglow migration sentinel, 0 otherwise.  Protobuf mirror of
      43             :    fd_vote_epoch_credits_is_alpenglow_marker(). */
      44             : 
      45             : FD_FN_PURE static inline int
      46       17093 : fd_solfuzz_epoch_credit_is_alpenglow_marker( fd_exec_test_epoch_credit_t const * ec ) {
      47       17093 :   return ec->epoch==ULONG_MAX && ec->credits==ULONG_MAX && ec->prev_credits==ULONG_MAX;
      48       17093 : }
      49             : 
      50             : static void
      51             : fd_solfuzz_block_update_prev_epoch_stakes( fd_vote_stakes_t *                 vote_stakes,
      52             :                                            ushort                             vote_stakes_fork_id,
      53             :                                            int                                use_t_1,
      54             :                                            fd_exec_test_prev_vote_account_t * vote_accounts,
      55        1222 :                                            uint                          vote_accounts_cnt ) {
      56        1222 :   if( FD_UNLIKELY( !vote_accounts ) ) return;
      57             : 
      58        3238 :   for( uint i=0U; i<vote_accounts_cnt; i++ ) {
      59        2016 :     fd_pubkey_t vote_pubkey = FD_LOAD( fd_pubkey_t, &vote_accounts[i].address );
      60        2016 :     fd_pubkey_t node_pubkey = FD_LOAD( fd_pubkey_t, &vote_accounts[i].node_pubkey );
      61        2016 :     ulong       stake       = vote_accounts[i].stake;
      62             : 
      63             :     /* v4 stores the rate in basis points; older versions only have a
      64             :        percentage byte. */
      65        2016 :     ushort commission;
      66        2016 :     if( vote_accounts[i].version == FD_EXEC_TEST_VOTE_ACCOUNT_VERSION_V4 ) {
      67        1379 :       commission = (ushort)vote_accounts[i].commission_bps;
      68        1379 :     } else {
      69         637 :       commission = (ushort)( (uchar)( vote_accounts[i].commission_bps / 100U ) * 100U );
      70         637 :     }
      71             : 
      72             :     /* SIMD-0123 fields exist in v4 state only; older versions keep the
      73             :        100% default and no pending rewards. */
      74        2016 :     ushort              block_revenue_commission_bps = FD_VOTE_DEFAULT_BLOCK_REVENUE_COMMISSION_BPS;
      75        2016 :     ulong               pending_delegator_rewards    = 0UL;
      76        2016 :     fd_pubkey_t const * inflation_collector          = NULL;
      77        2016 :     fd_pubkey_t const * block_collector              = NULL;
      78        2016 :     if( vote_accounts[i].version == FD_EXEC_TEST_VOTE_ACCOUNT_VERSION_V4 ) {
      79        1379 :       block_revenue_commission_bps = (ushort)vote_accounts[i].block_revenue_commission_bps;
      80        1379 :       pending_delegator_rewards    = vote_accounts[i].pending_delegator_rewards;
      81             :       /* SIMD-0232 collectors; empty fields keep the vote/node defaults. */
      82        1379 :       if( vote_accounts[i].inflation_rewards_collector.size==32U ) inflation_collector = (fd_pubkey_t const *)vote_accounts[i].inflation_rewards_collector.bytes;
      83        1379 :       if( vote_accounts[i].block_revenue_collector.size    ==32U ) block_collector     = (fd_pubkey_t const *)vote_accounts[i].block_revenue_collector.bytes;
      84        1379 :     }
      85             : 
      86        2016 :     uchar const no_bls[ FD_BLS_PUBKEY_COMPRESSED_SZ ] = {0};
      87        2016 :     fd_validator_location_t const no_loc = {0};
      88        2016 :     if( use_t_1 ) fd_vote_stakes_snap_insert_t_1( vote_stakes, vote_stakes_fork_id, &vote_pubkey, &node_pubkey, stake, commission, no_bls,
      89        1023 :                                                   block_revenue_commission_bps, pending_delegator_rewards, inflation_collector, block_collector, &no_loc );
      90         993 :     else          fd_vote_stakes_snap_insert_t_2( vote_stakes, vote_stakes_fork_id, &vote_pubkey, &node_pubkey, stake, commission, no_bls,
      91         993 :                                                   block_revenue_commission_bps, pending_delegator_rewards, inflation_collector, block_collector, &no_loc );
      92        2016 :   }
      93        1222 : }
      94             : 
      95             : /* Stores an acc in the stake delegations cache for the given vote
      96             :    account.  Deserializes and uses the present account state to derive
      97             :    delegation information. */
      98             : static void
      99             : fd_solfuzz_block_register_stake_delegation( fd_accdb_t *             accdb,
     100             :                                             fd_accdb_fork_id_t       fork_id,
     101             :                                             fd_stake_delegations_t * stake_delegations,
     102       13133 :                                             fd_pubkey_t *            pubkey ) {
     103       13133 :   fd_acc_t acc = fd_accdb_read_one( accdb, fork_id, pubkey->key );
     104       13133 :   if( FD_UNLIKELY( !acc.lamports ) ) {
     105           0 :     fd_accdb_unread_one( accdb, &acc );
     106           0 :     return;
     107           0 :   }
     108             : 
     109       13133 :   fd_stake_state_t const * stake_state = NULL;
     110       13133 :   if( memcmp( acc.owner, fd_solana_stake_program_id.key, 32UL )!=0 ||
     111       13133 :       !( stake_state = fd_stake_state_view( acc.data, acc.data_len ) ) ||
     112       13133 :       stake_state->stake_type!=FD_STAKE_STATE_STAKE ) {
     113       12114 :     fd_accdb_unread_one( accdb, &acc );
     114       12114 :     return;
     115       12114 :   }
     116             : 
     117        1019 :   fd_stake_delegations_root_update(
     118        1019 :       stake_delegations,
     119        1019 :       pubkey,
     120        1019 :       &stake_state->stake.stake.delegation.voter_pubkey,
     121        1019 :       stake_state->stake.stake.delegation.stake,
     122        1019 :       stake_state->stake.stake.delegation.activation_epoch,
     123        1019 :       stake_state->stake.stake.delegation.deactivation_epoch,
     124        1019 :       stake_state->stake.stake.credits_observed,
     125        1019 :       acc.lamports,
     126        1019 :       (uint)acc.data_len );
     127        1019 :   fd_accdb_unread_one( accdb, &acc );
     128        1019 : }
     129             : 
     130             : static void
     131         611 : fd_solfuzz_pb_block_ctx_destroy( fd_solfuzz_runner_t * runner ) {
     132         611 :   fd_banks_stake_delegations_evict_bank_fork( runner->banks, runner->bank );
     133             : 
     134         611 :   runner->bank->stake_rewards_fork_id = USHORT_MAX;
     135         611 :   fd_stake_rewards_clear( fd_bank_stake_rewards_modify( runner->bank ) );
     136             : 
     137         611 :   fd_progcache_reset( runner->progcache->join );
     138             : 
     139             :   /* Purge the fork attached in ctx_create so the accdb fork pool slot
     140             :      is released back for reuse.  Without this, repeated harness
     141             :      invocations (e.g. under a fuzzer) exhaust max_live_slots. */
     142         611 :   fd_accdb_purge( runner->accdb, runner->bank->accdb_fork_id );
     143         611 :   int charge_busy = 0;
     144         611 :   fd_accdb_background( runner->accdb, &charge_busy );
     145             : 
     146         611 : }
     147             : 
     148             : /* Sets up block execution context from an input test case to execute
     149             :    against the runtime.  Returns block_info on success and NULL on
     150             :    failure. */
     151             : static fd_txn_p_t *
     152             : fd_solfuzz_pb_block_ctx_create( fd_solfuzz_runner_t *                runner,
     153             :                                 fd_exec_test_block_context_t const * test_ctx,
     154             :                                 ulong *                              out_txn_cnt,
     155         609 :                                 fd_hash_t *                          poh ) {
     156         609 :   fd_accdb_t *  accdb = runner->accdb;
     157         609 :   fd_bank_t *   bank  = runner->bank;
     158         609 :   fd_banks_t *  banks = runner->banks;
     159             : 
     160         609 :   fd_runtime_stack_t * runtime_stack = runner->runtime_stack;
     161             : 
     162         609 :   fd_banks_clear_bank( banks, bank );
     163             : 
     164         609 :   runner->bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, fd_progcache_fork_id_initial() );
     165             : 
     166             :   /* Attach a fork off the runner's root for context loading */
     167         609 :   fd_accdb_fork_id_t fork_id = fd_accdb_attach_child( accdb, runner->root_fork_id );
     168         609 :   bank->accdb_fork_id        = fork_id;
     169         609 :   bank->parent_accdb_fork_id = bank->accdb_fork_id;
     170             : 
     171             :   /* Initialize bank from input block bank */
     172         609 :   FD_TEST( test_ctx->has_bank );
     173         609 :   fd_exec_test_block_bank_t const * block_bank = &test_ctx->bank;
     174             : 
     175             :   /* Slot */
     176         609 :   ulong slot = block_bank->slot;
     177         609 :   bank->f.slot = slot;
     178             : 
     179             :   /* Register a non-root progcache transaction at the bank's xid so the
     180             :      BPF loader can insert program cache entries during execution. */
     181         609 :   bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, bank->progcache_fork_id );
     182             : 
     183             :   /* Blockhash queue */
     184         609 :   fd_solfuzz_pb_restore_blockhash_queue( bank, block_bank->blockhash_queue, block_bank->blockhash_queue_count );
     185             : 
     186             :   /* RBH lamports per signature. In the Agave harness this is set inside
     187             :      the fee rate governor itself. */
     188         609 :   runner->bank->f.rbh_lamports_per_sig = block_bank->rbh_lamports_per_signature;
     189             : 
     190             :   /* Fee rate governor */
     191         609 :   FD_TEST( block_bank->has_fee_rate_governor );
     192         609 :   fd_solfuzz_pb_restore_fee_rate_governor( bank, &block_bank->fee_rate_governor );
     193             : 
     194             :   /* Parent slot */
     195         609 :   ulong parent_slot = block_bank->parent_slot;
     196         609 :   bank->f.parent_slot = parent_slot;
     197             : 
     198             :   /* Capitalization */
     199         609 :   bank->f.capitalization = block_bank->capitalization;
     200             : 
     201             :   /* Inflation */
     202         609 :   FD_TEST( block_bank->has_inflation );
     203         609 :   fd_inflation_t inflation = {
     204         609 :     .initial         = block_bank->inflation.initial,
     205         609 :     .terminal        = block_bank->inflation.terminal,
     206         609 :     .taper           = block_bank->inflation.taper,
     207         609 :     .foundation      = block_bank->inflation.foundation,
     208         609 :     .foundation_term = block_bank->inflation.foundation_term,
     209         609 :   };
     210         609 :   bank->f.inflation = inflation;
     211             : 
     212             :   /* Block height */
     213         609 :   bank->f.block_height = block_bank->block_height;
     214             : 
     215             :   /* POH (set right before finalize since we don't fuzz POH calculation) */
     216         609 :   fd_memcpy( poh, block_bank->poh, sizeof(fd_hash_t) );
     217             : 
     218             :   /* Bank hash (parent bank hash because current bank hash gets computed
     219             :      after the block executes) */
     220         609 :   fd_hash_t * bank_hash = &bank->f.bank_hash;
     221         609 :   fd_memcpy( bank_hash, block_bank->parent_bank_hash, sizeof(fd_hash_t) );
     222             : 
     223             :   /* Previous bank hash (used as input to the bank hash computation).
     224             :      In production this is set by fd_banks_clone_from_parent. */
     225         609 :   bank->f.prev_bank_hash = *(fd_hash_t const *)block_bank->parent_bank_hash;
     226             : 
     227             :   /* Parent signature count */
     228         609 :   bank->f.parent_signature_cnt = block_bank->parent_signature_count;
     229             : 
     230             :   /* Feature set */
     231         609 :   FD_TEST( block_bank->has_features );
     232         609 :   fd_exec_test_feature_set_t const * feature_set = &block_bank->features;
     233         609 :   fd_features_t * features_bm = &bank->f.features;
     234         609 :   fd_solfuzz_pb_create_feature_accounts( accdb, fork_id, feature_set, test_ctx->acct_states, test_ctx->acct_states_count );
     235         609 :   FD_TEST( fd_solfuzz_pb_restore_features( features_bm, feature_set ) );
     236             : 
     237             :   /* Total epoch stake (derived from T-1 vote accounts) */
     238         609 :   ulong total_epoch_stake = 0UL;
     239        1632 :   for( uint i=0U; i<block_bank->vote_accounts_t_1_count; i++ ) {
     240        1023 :     total_epoch_stake += block_bank->vote_accounts_t_1[i].stake;
     241        1023 :   }
     242         609 :   bank->f.total_epoch_stake = total_epoch_stake;
     243             : 
     244             :   /* Using default configuration of 64 ticks per slot
     245             :      https://github.com/anza-xyz/solana-sdk/blob/time-utils%40v3.0.0/time-utils/src/lib.rs#L18-L27 */
     246         609 :   uint128 ns_per_slot_128                    = FD_LOAD(uint128, block_bank->ns_per_slot );
     247         609 :   FD_TEST( ns_per_slot_128<=(uint128)ULONG_MAX );
     248         609 :   ulong   ns_per_slot                        = (ulong)ns_per_slot_128;
     249         609 :   bank->f.slot_params                        = FD_SLOT_PARAMS_400MS;
     250         609 :   bank->f.slot_params.ns_per_slot            = ns_per_slot;
     251         609 :   bank->f.ticks_per_slot                     = 64UL;
     252         609 :   runner->bank->f.slot_params.slots_per_year = (double)SECONDS_PER_YEAR * 1e9 / (double)ns_per_slot;
     253         609 :   bank->f.slot_params.hashes_per_tick        = (slot+1UL)*64UL;
     254         609 :   bank->f.slot_params_default                = bank->f.slot_params;
     255             : 
     256             :   /* Load in accounts, populate stake delegations and vote accounts */
     257         609 :   fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
     258         609 :   fd_stake_delegations_reset( stake_delegations );
     259             : 
     260         609 :   bank->stake_delegations_fork_id = fd_stake_delegations_new_fork( stake_delegations, USHORT_MAX );
     261             : 
     262         609 :   FD_TEST( block_bank->vote_accounts_t_1_count<=FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS );
     263         609 :   FD_TEST( block_bank->vote_accounts_t_2_count<=FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS );
     264             : 
     265       13771 :   for( ushort i=0; i<test_ctx->acct_states_count; i++ ) {
     266       13162 :     fd_solfuzz_pb_load_account( runner->runtime, accdb, fork_id, &test_ctx->acct_states[i], i );
     267             : 
     268             :     /* Update the stake delegations cache for epoch T */
     269       13162 :     fd_pubkey_t pubkey;
     270       13162 :     memcpy( &pubkey, test_ctx->acct_states[i].address, sizeof(fd_pubkey_t) );
     271       13162 :     fd_solfuzz_block_register_stake_delegation( accdb, fork_id, stake_delegations, &pubkey );
     272       13162 :   }
     273             : 
     274             :   /* reduce_stake_warmup_cooldown is activated on all clusters, so the
     275             :      new warmup/cooldown rate (0.09) applies from epoch 0 onwards. */
     276         609 :   bank->f.warmup_cooldown_rate_epoch = 0UL;
     277             : 
     278             :   /* Restore sysvar cache */
     279         609 :   fd_sysvar_cache_t * sysvar_cache = runner->runtime->sysvar_cache;
     280         609 :   fd_sysvar_cache_restore_fuzz( sysvar_cache, accdb, bank->accdb_fork_id );
     281         609 :   runner->runtime->sysvar_bank_seq = 0UL; /* refreshed again after block_execute_prepare */
     282             : 
     283             :   /* Epoch schedule */
     284         609 :   FD_TEST( fd_sysvar_cache_epoch_schedule_read( sysvar_cache, &bank->f.epoch_schedule ) );
     285             : 
     286             :   /* Current epoch gets updated in process_new_epoch, so use the epoch
     287             :      from the parent slot */
     288         609 :   bank->f.epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, parent_slot, NULL );
     289             : 
     290         609 :   fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
     291         609 :   fd_bank_t *        parent      = fd_banks_get_parent( banks, bank );
     292         609 :   FD_TEST( parent );
     293             : 
     294             :   /* Restore the snapshot on the parent, then fork the execution bank
     295             :      from it just as replay does. */
     296         609 :   fd_vote_stakes_reset( vote_stakes );
     297         609 :   parent->vote_stakes_fork_id = fd_vote_stakes_init( vote_stakes, bank->f.epoch );
     298         609 :   fd_solfuzz_block_update_prev_epoch_stakes( vote_stakes, parent->vote_stakes_fork_id, 1, block_bank->vote_accounts_t_1, block_bank->vote_accounts_t_1_count );
     299         609 :   fd_solfuzz_block_update_prev_epoch_stakes( vote_stakes, parent->vote_stakes_fork_id, 0, block_bank->vote_accounts_t_2, block_bank->vote_accounts_t_2_count );
     300         609 :   fd_vote_stakes_refresh( vote_stakes, parent->vote_stakes_fork_id, accdb, fork_id );
     301         609 :   bank->vote_stakes_fork_id = fd_vote_stakes_new_fork( vote_stakes, parent->vote_stakes_fork_id, bank->f.epoch );
     302             : 
     303             :   /* Initialize total_effective/activating/deactivating_stake from the
     304             :      loaded stake delegations.  These are read by fd_stakes_activate_epoch
     305             :      at epoch boundary instead of re-scanning all delegations. */
     306         609 :   uchar              stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
     307         609 :   fd_stake_history_t stake_history[1];
     308         609 :   if( FD_UNLIKELY( !fd_sysvar_stake_history_read( accdb, bank->accdb_fork_id, stake_history_data, stake_history ) ) ) {
     309           0 :     FD_LOG_ERR(( "StakeHistory sysvar missing or invalid" ));
     310           0 :   }
     311         609 :   fd_stake_delegations_refresh( stake_delegations, bank->f.epoch, stake_history, &bank->f.warmup_cooldown_rate_epoch, FD_FEATURE_ACTIVE_BANK( bank, remove_inactive_stakes ) );
     312             : 
     313         609 :   ulong chain_cnt = fd_vote_rewards_map_chain_cnt_est( runtime_stack->max_vote_accounts );
     314         609 :   FD_TEST( fd_vote_rewards_map_join( fd_vote_rewards_map_new( runtime_stack->stakes.vote_map, chain_cnt, 999 ) ) );
     315             : 
     316             :   /* Use epoch_credits from the proto if available (captured at epoch
     317             :      boundary time), otherwise fall back to the vote account in accdb. */
     318         609 :   ulong epoch_credits_len = 0UL;
     319         609 :   fd_epoch_credits_view_t epoch_credits_view[1];
     320         609 :   FD_TEST( fd_epoch_credits_view_init( epoch_credits_view, fd_bank_epoch_credits( bank ), bank->epoch_credits_fork_id ) );
     321        1632 :   for( uint i=0U; i<block_bank->vote_accounts_t_1_count; i++ ) {
     322        1023 :     fd_exec_test_prev_vote_account_t const * prev_vote_accs = &block_bank->vote_accounts_t_1[i];
     323             : 
     324        1023 :     if( FD_UNLIKELY( !fd_vote_stakes_query_t_1( vote_stakes, bank->vote_stakes_fork_id, (fd_pubkey_t const *)prev_vote_accs->address, NULL ) ) ) continue;
     325             : 
     326        1023 :     FD_TEST( prev_vote_accs->epoch_credits_count<=FD_EPOCH_CREDITS_MAX );
     327        1023 :     fd_epoch_credits_t * ec = &epoch_credits_view->credits[epoch_credits_len++];
     328        1023 :     fd_memcpy( ec->pubkey, prev_vote_accs->address, sizeof(fd_pubkey_t) );
     329             : 
     330             :     /* Alpenglow migration markers are not credits records: skip them so
     331             :        base_credits comes from the first real entry (subtracting a
     332             :        ULONG_MAX base would underflow every delta) and cnt counts only
     333             :        real entries.  Mirrors fd_ssload.c / get_vote_credits(). */
     334        1023 :     ulong cnt        = 0UL;
     335        1023 :     ec->base_credits = 0UL;
     336       18116 :     for( ulong j=0UL; j<prev_vote_accs->epoch_credits_count; j++ ) {
     337       17093 :       fd_exec_test_epoch_credit_t const * epc = &prev_vote_accs->epoch_credits[j];
     338       17093 :       if( FD_UNLIKELY( fd_solfuzz_epoch_credit_is_alpenglow_marker( epc ) ) ) continue;
     339       17093 :       if( FD_UNLIKELY( !cnt ) ) ec->base_credits = epc->prev_credits;
     340       17093 :       ec->epoch[ cnt ]              = (ushort)epc->epoch;
     341       17093 :       ec->credits_delta[ cnt ]      = epc->credits      - ec->base_credits;
     342       17093 :       ec->prev_credits_delta[ cnt ] = epc->prev_credits - ec->base_credits;
     343       17093 :       cnt++;
     344       17093 :     }
     345        1023 :     ec->cnt = (uchar)cnt; /* <=FD_EPOCH_CREDITS_MAX tested above */
     346        1023 :   }
     347         609 :   epoch_credits_view->len = epoch_credits_len;
     348         609 :   fd_epoch_credits_view_fini( epoch_credits_view );
     349             : 
     350             :   /* Update leader schedule */
     351         609 :   fd_runtime_update_leaders( bank, runtime_stack );
     352             : 
     353             :   /* Set the initial lthash from the input. */
     354         609 :   fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
     355         609 :   fd_memcpy( lthash, block_bank->parent_lt_hash, sizeof(fd_lthash_value_t) );
     356         609 :   fd_bank_lthash_end_locking_modify( bank );
     357             : 
     358             :   /* Rent */
     359         609 :   FD_TEST( fd_sysvar_cache_rent_read( sysvar_cache, &runner->bank->f.rent ) );
     360             : 
     361             :   /* Prepare raw transaction pointers and block / microblock infos */
     362         609 :   ulong        txn_cnt  = test_ctx->txns_count;
     363         609 :   fd_txn_p_t * txn_ptrs = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), txn_cnt * sizeof(fd_txn_p_t) );
     364        1057 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
     365         448 :     fd_txn_p_t * txn    = &txn_ptrs[i];
     366         448 :     ulong        msg_sz = fd_solfuzz_pb_txn_serialize( txn->payload, &test_ctx->txns[i] );
     367             : 
     368             :     // Reject any transactions over 1232 bytes
     369         448 :     if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) {
     370           0 :       return NULL;
     371           0 :     }
     372         448 :     txn->payload_sz = (ushort)msg_sz;
     373             : 
     374             :     // Reject any transactions that cannot be parsed
     375         448 :     if( FD_UNLIKELY( !fd_txn_parse( txn->payload, msg_sz, TXN( txn ), NULL ) ) ) {
     376           0 :       return NULL;
     377           0 :     }
     378         448 :   }
     379             : 
     380         609 :   *out_txn_cnt = txn_cnt;
     381         609 :   return txn_ptrs;
     382         609 : }
     383             : 
     384             : /* Takes in a list of txn_p_t created from
     385             :    fd_runtime_fuzz_block_ctx_create and executes it against the runtime.
     386             :    Returns the execution result. */
     387             : static int
     388             : fd_solfuzz_block_ctx_exec( fd_solfuzz_runner_t * runner,
     389             :                            fd_txn_p_t *          txn_ptrs,
     390             :                            ulong                 txn_cnt,
     391         611 :                            fd_hash_t *           poh ) {
     392         611 :   int res = 0;
     393             : 
     394             :   // Prepare. Execute. Finalize.
     395         611 :   FD_SPAD_FRAME_BEGIN( runner->spad ) {
     396         611 :     fd_capture_ctx_t * capture_ctx = NULL;
     397             : 
     398         611 :     if( runner->solcap ) {
     399           0 :       void * capture_ctx_mem = fd_spad_alloc( runner->spad, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
     400           0 :       capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( capture_ctx_mem ) );
     401           0 :       if( FD_UNLIKELY( !capture_ctx ) ) {
     402           0 :         FD_LOG_ERR(( "Failed to initialize capture_ctx" ));
     403           0 :       }
     404             : 
     405           0 :       fd_capture_link_file_t * capture_link_file =
     406           0 :         fd_spad_alloc( runner->spad, alignof(fd_capture_link_file_t), sizeof(fd_capture_link_file_t) );
     407           0 :       if( FD_UNLIKELY( !capture_link_file ) ) {
     408           0 :         FD_LOG_ERR(( "Failed to allocate capture_link_file" ));
     409           0 :       }
     410             : 
     411           0 :       capture_link_file->base.vt = &fd_capture_link_file_vt;
     412             : 
     413           0 :       int solcap_fd = (int)(ulong)runner->solcap_file;
     414           0 :       capture_link_file->fd          = solcap_fd;
     415           0 :       capture_ctx->capture_link      = &capture_link_file->base;
     416           0 :       capture_ctx->capctx_type.file  = capture_link_file;
     417           0 :       capture_ctx->solcap_start_slot = runner->bank->f.slot;
     418           0 :       capture_ctx->capture_solcap    = 1;
     419             : 
     420           0 :       fd_solcap_writer_init( capture_ctx->capture, solcap_fd );
     421           0 :     }
     422             : 
     423         611 :     fd_rewards_recalculate_partitioned_rewards( runner->bank, runner->accdb, runner->runtime_stack, capture_ctx );
     424             : 
     425             :     /* Process new epoch may push a new spad frame onto the runtime spad. We should make sure this frame gets
     426             :        cleared (if it was allocated) before executing the block. */
     427         611 :     int is_epoch_boundary = 0;
     428         611 :     fd_runtime_block_execute_prepare( runner->banks, runner->bank, runner->accdb, runner->runtime_stack, capture_ctx, &is_epoch_boundary );
     429             : 
     430             :     /* Sequential transaction execution.  Continue processing
     431             :        transactions even if a prior one was uncommitable. */
     432         611 :     int has_err = 0;
     433             :     /* fd_txn_out_t is ~10 MiB (inlined rollback/sysvar buffers), which
     434             :        overflows a tile's 8 MiB stack (FD_TILE_PRIVATE_STACK_SZ) when run
     435             :        under the multi-tile sol_compat harness.  Allocate it from the
     436             :        per-runner spad instead (one buffer, reused each iteration).  Must
     437             :        be the spad, not static, since runner tiles execute concurrently. */
     438         611 :     fd_txn_out_t * txn_out = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), sizeof(fd_txn_out_t) );
     439        1059 :     for( ulong i=0UL; i<txn_cnt; i++ ) {
     440         448 :       fd_txn_p_t * txn = &txn_ptrs[i];
     441             : 
     442             :       /* Execute the transaction against the runtime */
     443         448 :       res = FD_RUNTIME_EXECUTE_SUCCESS;
     444         448 :       fd_txn_in_t  txn_in = { .txn = txn, .bundle.is_bundle = 0 };
     445         448 :       fd_runtime_t * runtime = runner->runtime;
     446         448 :       fd_log_collector_t log[1];
     447         448 :       runtime->log.log_collector = log;
     448         448 :       fd_solfuzz_txn_ctx_exec( runner, runtime, &txn_in, &res, txn_out, 1 );
     449         448 :       txn_out->err.exec_err = res;
     450             : 
     451         448 :       if( FD_UNLIKELY( !txn_out->err.is_committable ) ) {
     452           1 :         fd_runtime_cancel_txn( runtime, NULL, NULL, txn_out );
     453           1 :         has_err = 1;
     454           1 :         continue;
     455           1 :       }
     456             : 
     457             :       /* Finalize the transaction */
     458         447 :       fd_runtime_commit_txn( runtime, runner->bank, NULL, txn_out );
     459             : 
     460         447 :       if( FD_UNLIKELY( !txn_out->err.is_committable ) ) {
     461           0 :         has_err = 1;
     462           0 :         continue;
     463           0 :       }
     464         447 :     }
     465             : 
     466             :     /* At this point we want to set the poh.  This is what will get
     467             :        updated in the blockhash queue. */
     468         611 :     runner->bank->f.poh = *poh;
     469             :     /* Finalize the block */
     470         611 :     fd_runtime_block_execute_finalize( runner->bank, runner->accdb, capture_ctx, NULL, (ushort)0, NULL );
     471             : 
     472         611 :     return !has_err;
     473         611 :   } FD_SPAD_FRAME_END;
     474           0 : }
     475             : 
     476             : /* Canonical (Agave-aligned) schedule hash
     477             :    Unique pubkeys referenced by sched, sorted deterministically
     478             :    Per-rotation indices mapped into sorted-uniq array */
     479             : ulong
     480             : fd_solfuzz_block_hash_epoch_leaders( fd_solfuzz_runner_t *      runner,
     481             :                                      fd_epoch_leaders_t const * leaders,
     482             :                                      ulong                      seed,
     483         611 :                                      uchar                      out[16] ) {
     484             :   /* Single contiguous spad allocation for uniq[] and sched_mapped[] */
     485         611 :   void *buf = fd_spad_alloc(
     486         611 :     runner->spad,
     487         611 :     alignof(pk_with_pos_t),
     488         611 :     leaders->sched_cnt*sizeof(pk_with_pos_t) +
     489         611 :     leaders->sched_cnt*sizeof(uint) );
     490             : 
     491         611 :   pk_with_pos_t * tmp          = (pk_with_pos_t *)buf;
     492         611 :   uint          * sched_mapped = (uint *)( tmp + leaders->sched_cnt );
     493             : 
     494             :   /* Gather all pubkeys and original positions from sched[] (skip invalid) */
     495         611 :   ulong gather_cnt = 0UL;
     496    31741613 :   for( ulong i=0UL; i<leaders->sched_cnt; i++ ) {
     497    31741002 :     uint idx = leaders->sched[i];
     498    31741002 :     if( idx>=leaders->pub_cnt ) { /* invalid slot leader */
     499           0 :       sched_mapped[i] = 0U;       /* prefill invalid mapping */
     500           0 :       continue;
     501           0 :     }
     502    31741002 :     fd_memcpy( &tmp[gather_cnt].pk, &leaders->pub[idx], sizeof(fd_pubkey_t) );
     503    31741002 :     tmp[gather_cnt].sched_pos = i;
     504    31741002 :     gather_cnt++;
     505    31741002 :   }
     506             : 
     507         611 :   if( gather_cnt==0UL ) {
     508             :     /* No leaders => hash:=0, count:=0 */
     509           0 :     fd_memset( out, 0, sizeof(ulong)*2 );
     510           0 :     return 0UL;
     511           0 :   }
     512             : 
     513             :   /* Sort tmp[] by pubkey, note: comparator relies on first struct member */
     514         611 :   sort_pkpos_inplace( tmp, (ulong)gather_cnt );
     515             : 
     516             :   /* Dedupe and assign indices into sched_mapped[] during single pass */
     517         611 :   ulong uniq_cnt = 0UL;
     518    32526809 :   for( ulong i=0UL; i<gather_cnt; i++ ) {
     519    32526198 :     if( i==0UL || memcmp( &tmp[i].pk, &tmp[i-1].pk, sizeof(fd_pubkey_t) )!=0 )
     520         982 :       uniq_cnt++;
     521             :     /* uniq_cnt-1 is index in uniq set */
     522    32526198 :     sched_mapped[tmp[i].sched_pos] = (uint)(uniq_cnt-1UL);
     523    32526198 :   }
     524             : 
     525             :   /* Reconstruct contiguous uniq[] for hashing */
     526         611 :   fd_pubkey_t *uniq = fd_spad_alloc( runner->spad,
     527         611 :                                      alignof(fd_pubkey_t),
     528         611 :                                      uniq_cnt*sizeof(fd_pubkey_t) );
     529         611 :   {
     530         611 :     ulong write_pos = 0UL;
     531    32566156 :     for( ulong i=0UL; i<gather_cnt; i++ ) {
     532    32578530 :       if( i==0UL || memcmp( &tmp[i].pk, &tmp[i-1].pk, sizeof(fd_pubkey_t) )!=0 )
     533         982 :       fd_memcpy( &uniq[write_pos++], &tmp[i].pk, sizeof(fd_pubkey_t) );
     534    32565545 :     }
     535         611 :   }
     536             : 
     537             :   /* Hash sorted unique pubkeys */
     538         611 :   ulong h1 = fd_hash( seed, uniq, uniq_cnt * sizeof(fd_pubkey_t) );
     539         611 :   fd_memcpy( out, &h1, sizeof(ulong) );
     540             : 
     541             :   /* Hash mapped indices */
     542         611 :   ulong h2 = fd_hash( seed, sched_mapped, leaders->sched_cnt * sizeof(uint) );
     543         611 :   fd_memcpy( out + sizeof(ulong), &h2, sizeof(ulong) );
     544             : 
     545         611 :   return uniq_cnt;
     546         611 : }
     547             : 
     548             : static void
     549             : fd_solfuzz_pb_build_leader_schedule_effects( fd_solfuzz_runner_t *          runner,
     550         611 :                                              fd_exec_test_block_effects_t * effects ) {
     551             :   /* Read epoch schedule sysvar */
     552         611 :   fd_epoch_schedule_t const * epoch_schedule = &runner->bank->f.epoch_schedule;
     553         611 :   FD_TEST( epoch_schedule );
     554             : 
     555             :   /* We will capture the leader schedule for the current epoch that we
     556             :      are in.  This will capture the leader schedule generated by an
     557             :      epoch boundary if one was crossed. */
     558         611 :   ulong epoch          = runner->bank->f.epoch;
     559         611 :   ulong ls_slot0       = fd_epoch_slot0( epoch_schedule, epoch );
     560         611 :   ulong slots_in_epoch = fd_epoch_slot_cnt( epoch_schedule, epoch );
     561             : 
     562         611 :   fd_epoch_leaders_t const * effects_leaders = fd_bank_epoch_leaders_query( runner->bank, epoch );
     563             : 
     564             :   /* Fill out effects struct from the Agave epoch info */
     565         611 :   effects->has_leader_schedule               = 1;
     566         611 :   effects->leader_schedule.leaders_epoch     = epoch;
     567         611 :   effects->leader_schedule.leaders_slot0     = ls_slot0;
     568         611 :   effects->leader_schedule.leaders_slot_cnt  = slots_in_epoch;
     569         611 :   effects->leader_schedule.leaders_sched_cnt = slots_in_epoch;
     570         611 :   effects->leader_schedule.leader_pub_cnt    = fd_solfuzz_block_hash_epoch_leaders(
     571         611 :       runner, effects_leaders,
     572         611 :       LEADER_SCHEDULE_HASH_SEED,
     573         611 :       effects->leader_schedule.leader_schedule_hash
     574         611 :   );
     575         611 : }
     576             : 
     577             : static ulong
     578             : fd_solfuzz_pb_collect_stake_delegations( fd_solfuzz_runner_t *             runner,
     579             :                                          fd_exec_test_stake_delegation_t * out,
     580         610 :                                          ulong                             out_max ) {
     581         610 :   fd_bank_t * bank = runner->bank;
     582             : 
     583         610 :   uchar                stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
     584         610 :   fd_stake_history_t   stake_history_[1];
     585         610 :   fd_stake_history_t * stake_history = fd_sysvar_stake_history_read( runner->accdb, bank->accdb_fork_id, stake_history_data, stake_history_ );
     586         610 :   ulong * warmup_cooldown_rate_epoch = &bank->f.warmup_cooldown_rate_epoch;
     587             : 
     588         610 :   fd_stake_delegations_t * stake_delegations = fd_bank_stake_delegations_modify( bank );
     589         610 :   fd_stake_delegations_view_begin( stake_delegations,
     590         610 :                                    bank->f.epoch,
     591         610 :                                    stake_history,
     592         610 :                                    warmup_cooldown_rate_epoch,
     593         610 :                                    bank->stake_delegations_fork_id );
     594             : 
     595         610 :   ulong cnt = 0UL;
     596         610 :   fd_stake_delegations_iter_t iter_[1];
     597         610 :   for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
     598        1625 :        !fd_stake_delegations_iter_done( iter );
     599        1015 :        fd_stake_delegations_iter_next( iter ) ) {
     600        1015 :     if( FD_UNLIKELY( cnt>=out_max ) ) abort();
     601        1015 :     fd_stake_delegation_t const *     d = fd_stake_delegations_iter_ele( iter );
     602        1015 :     fd_exec_test_stake_delegation_t * o = &out[ cnt++ ];
     603        1015 :     fd_memcpy( o->stake_account, d->stake_account.uc, sizeof(fd_pubkey_t) );
     604        1015 :     fd_memcpy( o->vote_account,  d->vote_account.uc,  sizeof(fd_pubkey_t) );
     605        1015 :     o->stake              = d->stake;
     606        1015 :     o->activation_epoch   = d->activation_epoch  ==USHORT_MAX ? ULONG_MAX : (ulong)d->activation_epoch;
     607        1015 :     o->deactivation_epoch = d->deactivation_epoch==USHORT_MAX ? ULONG_MAX : (ulong)d->deactivation_epoch;
     608        1015 :     o->credits_observed   = d->credits_observed;
     609        1015 :     o->lamports           = d->lamports;
     610        1015 :     o->data_len           = d->acc_dlen;
     611        1015 :   }
     612             : 
     613         610 :   fd_stake_delegations_view_end( stake_delegations, stake_history, warmup_cooldown_rate_epoch );
     614             : 
     615         610 :   sort_stake_delegation_inplace( out, cnt );
     616         610 :   return cnt;
     617         610 : }
     618             : 
     619             : ulong
     620             : fd_solfuzz_pb_block_run( fd_solfuzz_runner_t * runner,
     621             :                           void const *         input_,
     622             :                           void **              output_,
     623             :                           void *               output_buf,
     624         609 :                           ulong                output_bufsz ) {
     625         609 :   fd_exec_test_block_context_t const * input  = fd_type_pun_const( input_ );
     626         609 :   fd_exec_test_block_effects_t **      output = fd_type_pun( output_ );
     627             : 
     628         609 :   FD_SPAD_FRAME_BEGIN( runner->spad ) {
     629         609 :     ulong txn_cnt;
     630         609 :     fd_hash_t poh = {0};
     631         609 :     fd_txn_p_t * txn_ptrs = fd_solfuzz_pb_block_ctx_create( runner, input, &txn_cnt, &poh );
     632         609 :     if( txn_ptrs==NULL ) {
     633           0 :       fd_solfuzz_pb_block_ctx_destroy( runner );
     634           0 :       return 0;
     635           0 :     }
     636             : 
     637             :     /* Execute the constructed block against the runtime. */
     638         609 :     int is_committable = fd_solfuzz_block_ctx_exec( runner, txn_ptrs, txn_cnt, &poh );
     639             : 
     640             :     /* Start saving block exec results */
     641         609 :     FD_SCRATCH_ALLOC_INIT( l, output_buf );
     642         609 :     ulong output_end = (ulong)output_buf + output_bufsz;
     643             : 
     644         609 :     fd_exec_test_block_effects_t * effects =
     645         609 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_block_effects_t),
     646         609 :                                 sizeof(fd_exec_test_block_effects_t) );
     647         609 :     if( FD_UNLIKELY( _l > output_end ) ) {
     648           0 :       abort();
     649           0 :     }
     650         609 :     fd_memset( effects, 0, sizeof(fd_exec_test_block_effects_t) );
     651             : 
     652             :     /* Capture error status */
     653         609 :     effects->has_error = !is_committable;
     654             : 
     655             :     /* Capture capitalization */
     656 >1844*10^16 :     effects->slot_capitalization = !effects->has_error ? runner->bank->f.capitalization : 0UL;
     657             : 
     658             :     /* Capture hashes */
     659 >1844*10^16 :     fd_hash_t bank_hash = !effects->has_error ? runner->bank->f.bank_hash : (fd_hash_t){0};
     660         609 :     fd_memcpy( effects->bank_hash, bank_hash.hash, sizeof(fd_hash_t) );
     661             : 
     662             :     /* Capture cost tracker */
     663         609 :     fd_cost_tracker_t const * cost_tracker = fd_cost_tracker_store_peek( fd_bank_cost_tracker( runner->bank ), runner->bank->cost_tracker_fork_id );
     664         609 :     effects->has_cost_tracker = 1;
     665         609 :     effects->cost_tracker = (fd_exec_test_cost_tracker_t) {
     666 >1844*10^16 :       .block_cost = cost_tracker ? cost_tracker->block_cost : 0UL,
     667         609 :     };
     668             : 
     669             :     /* Effects: build T-epoch (bank epoch), T-stakes ephemeral leaders and report */
     670         609 :     fd_solfuzz_pb_build_leader_schedule_effects( runner, effects );
     671             : 
     672         610 :     if( !effects->has_error ) {
     673         610 :       fd_exec_test_stake_delegation_t * out = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_stake_delegation_t), 0UL );
     674           0 :       ulong cnt = fd_solfuzz_pb_collect_stake_delegations( runner, out, (output_end-_l)/sizeof(fd_exec_test_stake_delegation_t) );
     675         610 :       FD_SCRATCH_ALLOC_APPEND( l, 1UL, cnt*sizeof(fd_exec_test_stake_delegation_t) );
     676           0 :       effects->stake_delegations       = out;
     677         610 :       effects->stake_delegations_count = (uint)cnt;
     678         610 :     }
     679             : 
     680         609 :     ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     681         609 :     fd_solfuzz_pb_block_ctx_destroy( runner );
     682             : 
     683         609 :     *output = effects;
     684         609 :     return actual_end - (ulong)output_buf;
     685         609 :   } FD_SPAD_FRAME_END;
     686           0 : }

Generated by: LCOV version 1.14