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

Generated by: LCOV version 1.14