LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_bundle_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 245 0.0 %
Date: 2026-10-18 04:43:39 Functions: 0 8 0.0 %

          Line data    Source code
       1             : #include "fd_bundle_harness.h"
       2             : #include "fd_solfuzz_private.h"
       3             : #include "fd_txn_harness.h"
       4             : #include "fd_dump_pb.h"
       5             : #include "generated/bundle.pb.h"
       6             : #include "../fd_cost_tracker.h"
       7             : #include "../fd_slot_params.h"
       8             : #include "../fd_runtime.h"
       9             : #include "../../alpenglow/fd_alpenglow.h"
      10             : #include "../sysvar/fd_sysvar_cache.h"
      11             : #include "../sysvar/fd_sysvar_epoch_schedule.h"
      12             : #include "../../progcache/fd_progcache_admin.h"
      13             : #include "../../log_collector/fd_log_collector.h"  /* IWYU pragma: keep */
      14             : #include "../../stakes/fd_stakes.h"
      15             : 
      16             : static void
      17           0 : fd_solfuzz_bundle_ctx_destroy( fd_solfuzz_runner_t * runner ) {
      18           0 :   fd_banks_stake_delegations_evict_bank_fork( runner->banks, runner->bank );
      19             : 
      20           0 :   fd_progcache_reset( runner->progcache->join );
      21             : 
      22             :   /* Purge the fork attached in ctx_create so the accdb fork pool slot
      23             :      is released back for reuse.  Without this, repeated harness
      24             :      invocations (e.g. under a fuzzer) exhaust max_live_slots. */
      25           0 :   fd_accdb_purge( runner->accdb, runner->bank->accdb_fork_id );
      26           0 :   int charge_busy = 0;
      27           0 :   fd_accdb_background( runner->accdb, &charge_busy );
      28             : 
      29           0 : }
      30             : 
      31             : static fd_txn_p_t *
      32             : fd_solfuzz_pb_bundle_ctx_create( fd_solfuzz_runner_t *                 runner,
      33             :                                  fd_exec_test_bundle_context_t const * test_ctx,
      34           0 :                                  ulong *                               out_txn_cnt ) {
      35           0 :   ulong txn_cnt = (ulong)test_ctx->txns_count;
      36           0 :   FD_TEST( txn_cnt<=FD_PACK_MAX_TXN_PER_BUNDLE );
      37             : 
      38           0 :   fd_accdb_t * accdb = runner->accdb;
      39             : 
      40           0 :   ulong slot = fd_solfuzz_pb_get_slot( test_ctx->account_shared_data, test_ctx->account_shared_data_count );
      41             : 
      42             :   /* Initialize bank from input txn bank */
      43           0 :   fd_banks_clear_bank( runner->banks, runner->bank );
      44             : 
      45           0 :   runner->bank->f.slot = slot;
      46           0 :   runner->bank->bank_seq = runner->bank->idx;
      47             : 
      48           0 :   runner->bank->progcache_fork_id    = fd_progcache_attach_child( runner->progcache->join, fd_progcache_fork_id_initial() );
      49           0 :   runner->bank->accdb_fork_id        = fd_accdb_attach_child( accdb, runner->root_fork_id );
      50           0 :   runner->bank->parent_accdb_fork_id = runner->bank->accdb_fork_id;
      51             : 
      52           0 :   FD_TEST( test_ctx->has_bank );
      53           0 :   fd_exec_test_txn_bank_t const * txn_bank = &test_ctx->bank;
      54             : 
      55           0 :   fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( runner->banks );
      56           0 :   runner->bank->stake_delegations_fork_id = fd_stake_delegations_new_fork( stake_delegations, USHORT_MAX );
      57             : 
      58           0 :   fd_solfuzz_pb_restore_blockhash_queue( runner->bank, txn_bank->blockhash_queue, txn_bank->blockhash_queue_count );
      59           0 :   runner->bank->f.rbh_lamports_per_sig = txn_bank->rbh_lamports_per_signature;
      60             : 
      61           0 :   FD_TEST( txn_bank->has_fee_rate_governor );
      62           0 :   fd_solfuzz_pb_restore_fee_rate_governor( runner->bank, &txn_bank->fee_rate_governor );
      63             : 
      64           0 :   runner->bank->f.parent_slot       = slot-1UL;
      65           0 :   runner->bank->f.total_epoch_stake = txn_bank->total_epoch_stake;
      66             : 
      67           0 :   FD_TEST( txn_bank->has_features );
      68           0 :   FD_TEST( fd_solfuzz_pb_restore_features( &runner->bank->f.features, &txn_bank->features ) );
      69             : 
      70           0 :   for( ulong i=0UL; i<test_ctx->account_shared_data_count; i++ ) {
      71           0 :     fd_solfuzz_pb_load_account( runner->runtime, accdb, runner->bank->accdb_fork_id, &test_ctx->account_shared_data[i], i );
      72           0 :   }
      73             : 
      74           0 :   runner->bank->f.ticks_per_slot             = 64;
      75           0 :   runner->bank->f.slot_params                = FD_SLOT_PARAMS_400MS;
      76           0 :   runner->bank->f.slot_params.slots_per_year = SECONDS_PER_YEAR * (1000000000.0 / (double)6250000) / (double)(runner->bank->f.ticks_per_slot);
      77           0 :   runner->bank->f.slot_params_default        = runner->bank->f.slot_params;
      78             : 
      79           0 :   fd_sysvar_cache_restore_fuzz( runner->bank, runner->accdb );
      80             : 
      81           0 :   FD_TEST( fd_sysvar_cache_epoch_schedule_read( &runner->bank->f.sysvar_cache, &runner->bank->f.epoch_schedule ) );
      82           0 :   runner->bank->f.epoch = fd_slot_to_epoch( &runner->bank->f.epoch_schedule, slot, NULL );
      83             : 
      84           0 :   FD_TEST( fd_sysvar_cache_rent_read( &runner->bank->f.sysvar_cache, &runner->bank->f.rent ) );
      85             : 
      86           0 :   runner->bank->f.alpenglow_migration_slot = fd_alpenglow_migration_slot( runner->bank, accdb );
      87             : 
      88             :   /* Initialize cost tracker */
      89           0 :   fd_cost_tracker_t * cost_tracker = fd_cost_tracker_store_peek( fd_bank_cost_tracker( runner->bank ), runner->bank->cost_tracker_fork_id );
      90           0 :   fd_cost_tracker_init( cost_tracker, &runner->bank->f.features, &runner->bank->f.slot_params, slot );
      91             : 
      92           0 :   fd_txn_p_t * txns = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), txn_cnt*sizeof(fd_txn_p_t) );
      93           0 :   fd_memset( txns, 0, txn_cnt*sizeof(fd_txn_p_t) );
      94             : 
      95           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
      96           0 :     ulong msg_sz = fd_solfuzz_pb_txn_serialize( txns[i].payload, &test_ctx->txns[i] );
      97           0 :     if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) return NULL;
      98           0 :     if( FD_UNLIKELY( !fd_txn_parse( txns[i].payload, msg_sz, TXN( &txns[i] ), NULL ) ) ) return NULL;
      99           0 :     txns[i].payload_sz = (ushort)msg_sz;
     100           0 :   }
     101             : 
     102           0 :   *out_txn_cnt = txn_cnt;
     103           0 :   return txns;
     104           0 : }
     105             : 
     106             : static void
     107             : fd_solfuzz_bundle_mark_uncommittable( fd_txn_out_t * txn_outs,
     108           0 :                                       ulong          txn_cnt ) {
     109           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
     110           0 :     txn_outs[i].err.is_committable = 0;
     111           0 :   }
     112           0 : }
     113             : 
     114             : /* Record the latest stake and vote account updates only. */
     115             : 
     116             : static fd_exec_test_stake_delta_t *
     117             : fd_solfuzz_bundle_stake_delta_upsert( fd_exec_test_bundle_effects_t * effects,
     118           0 :                                       fd_pubkey_t const *             key ) {
     119           0 :   for( ulong i=0UL; i<effects->stake_deltas_count; i++ ) {
     120           0 :     if( fd_memeq( effects->stake_deltas[i].address, key, sizeof(fd_pubkey_t) ) ) return &effects->stake_deltas[i];
     121           0 :   }
     122           0 :   fd_exec_test_stake_delta_t * stake_delta = &effects->stake_deltas[effects->stake_deltas_count++];
     123           0 :   fd_memcpy( stake_delta->address, key, sizeof(fd_pubkey_t) );
     124           0 :   return stake_delta;
     125           0 : }
     126             : 
     127             : static fd_exec_test_vote_update_t *
     128             : fd_solfuzz_bundle_vote_update_upsert( fd_exec_test_bundle_effects_t * effects,
     129           0 :                                       fd_pubkey_t const *             key ) {
     130           0 :   for( ulong i=0UL; i<effects->vote_updates_count; i++ ) {
     131           0 :     if( fd_memeq( effects->vote_updates[i].address, key, sizeof(fd_pubkey_t) ) ) return &effects->vote_updates[i];
     132           0 :   }
     133           0 :   fd_exec_test_vote_update_t * vote_update = &effects->vote_updates[effects->vote_updates_count++];
     134           0 :   fd_memcpy( vote_update->address, key, sizeof(fd_pubkey_t) );
     135           0 :   return vote_update;
     136           0 : }
     137             : 
     138             : static int
     139             : fd_solfuzz_bundle_execute( fd_solfuzz_runner_t *                 runner,
     140             :                            fd_exec_test_bundle_context_t const * input,
     141             :                            int                                   is_bundle,
     142             :                            void *                                output_buf,
     143             :                            ulong                                 output_bufsz,
     144             :                            fd_exec_test_bundle_effects_t **      effects_out,
     145           0 :                            ulong *                               output_used ) {
     146           0 :   ulong txn_cnt = 0UL;
     147           0 :   fd_txn_p_t * txns = fd_solfuzz_pb_bundle_ctx_create( runner, input, &txn_cnt );
     148           0 :   if( FD_UNLIKELY( !txns ) ) {
     149           0 :     fd_solfuzz_bundle_ctx_destroy( runner );
     150           0 :     return 0;
     151           0 :   }
     152             : 
     153           0 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
     154           0 :   ulong output_end = (ulong)output_buf + output_bufsz;
     155             : 
     156           0 :   fd_exec_test_bundle_effects_t * effects =
     157           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_bundle_effects_t),
     158           0 :                                 sizeof(fd_exec_test_bundle_effects_t) );
     159           0 :   if( FD_UNLIKELY( _l>output_end ) ) abort();
     160           0 :   fd_memset( effects, 0, sizeof(fd_exec_test_bundle_effects_t) );
     161             : 
     162           0 :   effects->txn_results_count = (pb_size_t)txn_cnt;
     163           0 :   effects->txn_results = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_txn_result_t),
     164           0 :                                                   txn_cnt*sizeof(fd_exec_test_txn_result_t) );
     165           0 :   if( FD_UNLIKELY( _l>output_end ) ) abort();
     166           0 :   fd_memset( effects->txn_results, 0, txn_cnt*sizeof(fd_exec_test_txn_result_t) );
     167             : 
     168           0 :   ulong update_max = txn_cnt*MAX_TX_ACCOUNT_LOCKS;
     169           0 :   effects->stake_deltas = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_stake_delta_t),
     170           0 :                                                    update_max*sizeof(fd_exec_test_stake_delta_t) );
     171           0 :   if( FD_UNLIKELY( _l>output_end ) ) abort();
     172           0 :   fd_memset( effects->stake_deltas, 0, update_max*sizeof(fd_exec_test_stake_delta_t) );
     173           0 :   effects->stake_deltas_count = 0UL;
     174             : 
     175           0 :   effects->vote_updates = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_vote_update_t),
     176           0 :                                                    update_max*sizeof(fd_exec_test_vote_update_t) );
     177           0 :   if( FD_UNLIKELY( _l>output_end ) ) abort();
     178           0 :   fd_memset( effects->vote_updates, 0, update_max*sizeof(fd_exec_test_vote_update_t) );
     179           0 :   effects->vote_updates_count = 0UL;
     180             : 
     181           0 :   effects->new_votes       = NULL;
     182           0 :   effects->new_votes_count = 0UL;
     183             : 
     184           0 :   fd_runtime_t *       runtime      = runner->runtime;
     185           0 :   fd_txn_out_t *       txn_outs     = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), txn_cnt*sizeof(fd_txn_out_t) );
     186           0 :   fd_txn_in_t  *       txn_ins      = fd_spad_alloc( runner->spad, alignof(fd_txn_in_t),  txn_cnt*sizeof(fd_txn_in_t)  );
     187           0 :   fd_log_collector_t * logs         = fd_spad_alloc( runner->spad, alignof(fd_log_collector_t), txn_cnt*sizeof(fd_log_collector_t) );
     188           0 :   ulong                ran_cnt      = 0UL;
     189           0 :   int                  saw_exec_err = 0;
     190             : 
     191             :   /* Set up every txn_in up-front.  For bundles, acquire all of the
     192             :      bundle's accounts in a single acquire_a/acquire_b pair before any
     193             :      txn executes; each txn then binds to this shared pool, which is
     194             :      released once after the bundle is committed or cancelled. */
     195           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
     196           0 :     txn_ins[i]                     = (fd_txn_in_t){0};
     197           0 :     txn_ins[i].txn                 = &txns[i];
     198           0 :     txn_ins[i].bundle.is_bundle    = is_bundle;
     199           0 :     txn_ins[i].bundle.prev_txn_cnt = is_bundle ? i : 0UL;
     200           0 :     for( ulong j=0UL; is_bundle && j<i; j++ ) txn_ins[i].bundle.prev_txn_outs[j] = &txn_outs[j];
     201           0 :   }
     202             :   /* Mirror fd_execle_tile: a failed bundle prepare acquires nothing, so
     203             :      every txn fails and fd_runtime_fini_bundle must be skipped.  An
     204             :      empty bundle likewise acquires nothing and must skip fini. */
     205           0 :   int bundle_prep_ok = 1;
     206           0 :   if( is_bundle ) {
     207           0 :     runtime->accdb = runner->accdb;
     208           0 :     if( FD_LIKELY( txn_cnt ) ) {
     209           0 :       int prepare_err = fd_runtime_prepare_bundle_accounts( runtime, runner->bank, txn_ins, txn_outs, txn_cnt );
     210           0 :       if( FD_UNLIKELY( prepare_err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     211           0 :         bundle_prep_ok = 0;
     212           0 :         saw_exec_err   = 1;
     213           0 :       }
     214           0 :     }
     215           0 :   }
     216             : 
     217           0 :   for( ulong i=0UL; bundle_prep_ok && i<txn_cnt; i++ ) {
     218           0 :     fd_txn_in_t * txn_in = &txn_ins[i];
     219             : 
     220           0 :     int exec_res = 0;
     221           0 :     runtime->log.log_collector = &logs[i];
     222           0 :     fd_solfuzz_txn_ctx_exec( runner, runtime, txn_in, &exec_res, &txn_outs[i], 1 );
     223           0 :     ran_cnt = i+1UL;
     224             : 
     225           0 :     if( exec_res!=FD_RUNTIME_EXECUTE_SUCCESS ) {
     226           0 :       saw_exec_err = 1;
     227           0 :       if( is_bundle ) {
     228           0 :         fd_solfuzz_bundle_mark_uncommittable( txn_outs, ran_cnt );
     229           0 :       } else {
     230           0 :         txn_outs[i].err.is_committable = 0;
     231           0 :         fd_runtime_cancel_txn( runtime, NULL, NULL, &txn_outs[i] );
     232           0 :       }
     233           0 :       break;
     234           0 :     }
     235             : 
     236           0 :     fd_exec_test_txn_result_t * txn_result = NULL;
     237           0 :     ulong txn_result_sz = create_txn_result_protobuf_from_txn(
     238           0 :         &txn_result,
     239           0 :         (void *)_l,
     240           0 :         output_end - _l,
     241           0 :         txn_in,
     242           0 :         &txn_outs[i],
     243           0 :         exec_res );
     244           0 :     FD_TEST( txn_result_sz );
     245           0 :     FD_TEST( txn_result );
     246           0 :     effects->txn_results[i] = *txn_result;
     247           0 :     _l += txn_result_sz;
     248             : 
     249           0 :     for( ulong j=0UL; j<txn_outs[i].accounts.cnt; j++ ) {
     250           0 :       if( txn_outs[i].accounts.stake_update[j] ) {
     251           0 :         fd_exec_test_stake_delta_t * stake_delta = fd_solfuzz_bundle_stake_delta_upsert( effects, &txn_outs[i].accounts.keys[j] );
     252           0 :         stake_delta->new_stake = 0UL;
     253             : 
     254           0 :         fd_stake_state_t const * stake_state = fd_stakes_get_state( txn_outs[i].accounts.account[j] );
     255           0 :         if( stake_state && stake_state->stake_type==FD_STAKE_STATE_STAKE ) {
     256           0 :           stake_delta->new_stake = stake_state->stake.stake.delegation.stake;
     257           0 :         }
     258           0 :       }
     259             : 
     260           0 :       if( txn_outs[i].accounts.vote_update[j] ) {
     261           0 :         fd_vote_block_timestamp_t last_vote;
     262           0 :         if( !fd_vote_account_last_timestamp( txn_outs[i].accounts.account[j]->data,
     263           0 :                                              txn_outs[i].accounts.account[j]->data_len,
     264           0 :                                              &last_vote ) ) {
     265           0 :           fd_exec_test_vote_update_t * vote_update = fd_solfuzz_bundle_vote_update_upsert( effects, &txn_outs[i].accounts.keys[j] );
     266           0 :           vote_update->last_vote_slot      = last_vote.slot;
     267           0 :           vote_update->last_vote_timestamp = (ulong)last_vote.timestamp;
     268           0 :         }
     269           0 :       }
     270           0 :     }
     271             : 
     272           0 :     if( !is_bundle ) fd_runtime_commit_txn( runtime, runner->bank, NULL, &txn_outs[i] );
     273           0 :   }
     274             : 
     275           0 :   if( is_bundle && !saw_exec_err ) {
     276           0 :     for( ulong i=0UL; i<txn_cnt; i++ ) {
     277           0 :       fd_runtime_commit_txn( runtime, runner->bank, NULL, &txn_outs[i] );
     278           0 :     }
     279           0 :   }
     280             : 
     281             :   /* Release the bundle's shared account pool exactly once now that all of
     282             :      its txns have been committed or cancelled. */
     283           0 :   if( is_bundle && bundle_prep_ok && txn_cnt ) fd_runtime_fini_bundle( runtime );
     284             : 
     285           0 :   effects->has_error = saw_exec_err;
     286           0 :   if( saw_exec_err ) {
     287           0 :     effects->txn_results_count  = 0UL;
     288           0 :     effects->txn_results        = NULL;
     289           0 :     effects->stake_deltas_count = 0UL;
     290           0 :     effects->stake_deltas       = NULL;
     291           0 :     effects->vote_updates_count = 0UL;
     292           0 :     effects->vote_updates       = NULL;
     293           0 :     effects->new_votes_count    = 0UL;
     294           0 :     effects->new_votes          = NULL;
     295           0 :   }
     296             : 
     297           0 :   *effects_out = effects;
     298           0 :   *output_used = FD_SCRATCH_ALLOC_FINI( l, 1UL ) - (ulong)output_buf;
     299             : 
     300           0 :   fd_solfuzz_bundle_ctx_destroy( runner );
     301           0 :   return 1;
     302           0 : }
     303             : 
     304             : static void
     305             : fd_solfuzz_bundle_assert_same_success( fd_solfuzz_runner_t *          runner,
     306             :                                        fd_exec_test_bundle_effects_t * regular,
     307           0 :                                        fd_exec_test_bundle_effects_t * bundle ) {
     308           0 :   ulong   buf_sz      = 100000000UL;
     309           0 :   uchar * regular_buf = fd_spad_alloc( runner->spad, 1UL, buf_sz );
     310           0 :   uchar * bundle_buf  = fd_spad_alloc( runner->spad, 1UL, buf_sz );
     311             : 
     312           0 :   ulong regular_sz = buf_sz;
     313           0 :   ulong bundle_sz  = buf_sz;
     314           0 :   FD_TEST( sol_compat_encode( regular_buf, &regular_sz, regular, &fd_exec_test_bundle_effects_t_msg ) );
     315           0 :   FD_TEST( sol_compat_encode( bundle_buf,  &bundle_sz,  bundle,  &fd_exec_test_bundle_effects_t_msg ) );
     316             : 
     317           0 :   if( FD_UNLIKELY( regular_sz!=bundle_sz || !fd_memeq( regular_buf, bundle_buf, regular_sz ) ) ) {
     318           0 :     FD_LOG_ERR(( "bundle transaction effects mismatch" ));
     319           0 :   }
     320           0 : }
     321             : 
     322             : ulong
     323             : fd_solfuzz_pb_bundle_run( fd_solfuzz_runner_t * runner,
     324             :                           void const *          input_,
     325             :                           void **               output_,
     326             :                           void *                output_buf,
     327           0 :                           ulong                 output_bufsz ) {
     328           0 :   fd_exec_test_bundle_context_t const * input  = fd_type_pun_const( input_ );
     329           0 :   fd_exec_test_bundle_effects_t **      output = fd_type_pun( output_ );
     330             : 
     331           0 :   FD_SPAD_FRAME_BEGIN( runner->spad ) {
     332           0 :     void * regular_buf = fd_spad_alloc( runner->spad, 1UL, output_bufsz );
     333             : 
     334           0 :     fd_exec_test_bundle_effects_t * regular_effects = NULL;
     335           0 :     fd_exec_test_bundle_effects_t * bundle_effects  = NULL;
     336           0 :     ulong                           regular_sz      = 0UL;
     337           0 :     ulong                           bundle_sz       = 0UL;
     338             : 
     339           0 :     int regular_ok = fd_solfuzz_bundle_execute( runner, input, 0, regular_buf, output_bufsz, &regular_effects, &regular_sz );
     340           0 :     int bundle_ok  = fd_solfuzz_bundle_execute( runner, input, 1, output_buf,  output_bufsz, &bundle_effects,  &bundle_sz  );
     341             : 
     342           0 :     if( FD_UNLIKELY( regular_ok!=bundle_ok ) ) {
     343           0 :       FD_LOG_ERR(( "bundle harness setup parity mismatch: regular_ok=%d bundle_ok=%d", regular_ok, bundle_ok ));
     344           0 :     }
     345           0 :     if( FD_UNLIKELY( !regular_ok ) ) {
     346           0 :       return 0UL;
     347           0 :     }
     348             : 
     349           0 :     (void)regular_sz;
     350           0 :     if( FD_UNLIKELY( regular_effects->has_error!=bundle_effects->has_error ) ) {
     351           0 :       FD_LOG_ERR(( "bundle success parity mismatch: regular_has_error=%d bundle_has_error=%d",
     352           0 :                    regular_effects->has_error, bundle_effects->has_error ));
     353           0 :     }
     354             : 
     355           0 :     if( !regular_effects->has_error ) {
     356           0 :       fd_solfuzz_bundle_assert_same_success( runner, regular_effects, bundle_effects );
     357           0 :     }
     358             : 
     359           0 :     *output = bundle_effects;
     360           0 :     return bundle_sz;
     361           0 :   } FD_SPAD_FRAME_END;
     362           0 : }

Generated by: LCOV version 1.14