LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_cost_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 67 72 93.1 %
Date: 2026-11-26 02:16:43 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "../fd_executor.h"
       2             : #include "fd_solfuzz_private.h"
       3             : #include "fd_txn_harness.h"
       4             : #include "generated/cost.pb.h"
       5             : 
       6             : #include "../fd_cost_tracker.h"
       7             : #include "../program/fd_compute_budget_program.h"
       8             : 
       9             : ulong
      10             : fd_solfuzz_pb_cost_run( fd_solfuzz_runner_t * runner,
      11             :                         void const *          input_,
      12             :                         void **               output_,
      13             :                         void *                output_buf,
      14        1003 :                         ulong                 output_bufsz ) {
      15        1003 :   fd_exec_test_cost_context_t const * input  = fd_type_pun_const( input_ );
      16        1003 :   fd_exec_test_cost_result_t **       output = fd_type_pun( output_ );
      17             : 
      18        1003 :   if( FD_UNLIKELY( !input->has_tx || !input->has_features ) ) return 0UL;
      19             : 
      20        1003 :   fd_banks_clear_bank( runner->banks, runner->bank );
      21        1003 :   runner->bank->f.slot = 1UL;
      22        1003 :   FD_TEST( fd_solfuzz_pb_restore_features( &runner->bank->f.features, &input->features ) );
      23             : 
      24        1003 :   fd_txn_p_t * txn_p = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), sizeof(fd_txn_p_t) );
      25        1003 :   ulong txn_sz = fd_solfuzz_pb_txn_serialize( txn_p->payload, &input->tx );
      26        1003 :   if( FD_UNLIKELY( txn_sz==ULONG_MAX ) ) return 0UL;
      27             : 
      28        1003 :   txn_p->payload_sz = (ushort)txn_sz;
      29        1003 :   if( FD_UNLIKELY( !fd_txn_parse( txn_p->payload, txn_p->payload_sz, TXN( txn_p ), NULL ) ) ) {
      30           0 :     return 0UL;
      31           0 :   }
      32             : 
      33        1003 :   fd_txn_in_t txn_in = {0};
      34        1003 :   txn_in.txn = txn_p;
      35             : 
      36        1003 :   fd_txn_out_t * txn_out = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), sizeof(fd_txn_out_t) );
      37        1003 :   fd_memset( &txn_out->err,     0, sizeof(txn_out->err)     );
      38        1003 :   fd_memset( &txn_out->details, 0, sizeof(txn_out->details) );
      39        1003 :   txn_out->accounts.is_setup       = 0;
      40        1003 :   txn_out->accounts.is_bundle      = 0;
      41        1003 :   txn_out->accounts.cnt            = 0UL;
      42        1003 :   txn_out->accounts.executable_cnt = 0UL;
      43        1003 :   fd_compute_budget_details_new( &txn_out->details.compute_budget );
      44        1003 :   txn_out->details.loaded_accounts_data_size = txn_out->details.compute_budget.loaded_accounts_data_size_limit;
      45             : 
      46        1003 :   int err;
      47        1003 :   if( TXN( txn_p )->transaction_version==FD_TXN_V1 ) {
      48           0 :     err = fd_executor_sanitize_txn_v1_config( &txn_in, txn_out );
      49        1003 :   } else {
      50        1003 :     err = fd_executor_compute_budget_program_execute_instructions( runner->bank, &txn_in, txn_out );
      51        1003 :   }
      52        1003 :   if( FD_LIKELY( !err ) ) err = fd_sanitize_compute_unit_limits( txn_out );
      53        1003 :   if( FD_UNLIKELY( err && input->mode==FD_EXEC_TEST_TXN_COST_MODE_ACTUAL ) ) return 0UL;
      54             : 
      55        1003 :   if( input->mode==FD_EXEC_TEST_TXN_COST_MODE_ACTUAL ) {
      56         487 :     ulong actual_cost = (ulong)input->actual_programs_execution_cost;
      57         487 :     ulong limit = txn_out->details.compute_budget.compute_unit_limit;
      58         487 :     txn_out->details.compute_budget.compute_meter = fd_ulong_sat_sub( limit, fd_ulong_min( actual_cost, limit ) );
      59         487 :     txn_out->details.loaded_accounts_data_size = (ulong)input->actual_loaded_accounts_data_size_bytes;
      60         516 :   } else {
      61             :     /* In ESTIMATE mode, a compute-budget processing failure still produces a
      62             :        cost with zero execution and loaded-accounts-data costs.  Otherwise,
      63             :        programs_execution_cost = compute_unit_limit (see agave
      64             :        cost_model.rs:get_estimated_execution_cost). fd_cost_tracker computes
      65             :        programs_execution_cost as (limit - compute_meter). */
      66         516 :     if( FD_UNLIKELY( err ) ) {
      67           0 :       txn_out->details.compute_budget.compute_unit_limit = 0UL;
      68           0 :       txn_out->details.loaded_accounts_data_size          = 0UL;
      69         516 :     } else {
      70         516 :       txn_out->details.loaded_accounts_data_size = txn_out->details.compute_budget.loaded_accounts_data_size_limit;
      71         516 :     }
      72         516 :     txn_out->details.compute_budget.compute_meter = 0UL;
      73         516 :   }
      74             : 
      75        1003 :   fd_cost_tracker_calculate_cost( runner->bank, &txn_in, txn_out );
      76             : 
      77             :   /* In ACTUAL mode agave passes actual_programs_execution_cost through
      78             :      without clamping to compute_unit_limit (see agave cost_model.rs:
      79             :      calculate_cost_for_executed_transaction). The (limit - meter)
      80             :      formula in fd_cost_tracker inherently clamps, so overwrite with the
      81             :      raw actual value here for parity. */
      82        1003 :   if( input->mode==FD_EXEC_TEST_TXN_COST_MODE_ACTUAL ) {
      83         485 :     txn_out->details.txn_cost.transaction.programs_execution_cost = input->actual_programs_execution_cost;
      84         485 :   }
      85             : 
      86             :   /* Build output into the caller-provided scratch buffer */
      87        1003 :   ulong output_end = (ulong)output_buf + output_bufsz;
      88        1003 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
      89        1003 :   fd_exec_test_cost_result_t * result =
      90        1003 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_cost_result_t), sizeof(fd_exec_test_cost_result_t) );
      91        1003 :   FD_TEST( _l <= output_end );
      92        1003 :   *result = (fd_exec_test_cost_result_t){0};
      93             : 
      94        1003 :   result->has_cost = 1;
      95        1003 :   result->signature_cost                    = txn_out->details.txn_cost.transaction.signature_cost;
      96        1003 :   result->write_lock_cost                   = txn_out->details.txn_cost.transaction.write_lock_cost;
      97        1003 :   result->data_bytes_cost                   = txn_out->details.txn_cost.transaction.data_bytes_cost;
      98        1003 :   result->programs_execution_cost           = txn_out->details.txn_cost.transaction.programs_execution_cost;
      99        1003 :   result->loaded_accounts_data_size_cost    = txn_out->details.txn_cost.transaction.loaded_accounts_data_size_cost;
     100        1003 :   result->allocated_accounts_data_size      = txn_out->details.txn_cost.transaction.allocated_accounts_data_size;
     101             :   /* Saturating add to match agave's UsageCostDetails::sum (transaction_cost.rs:
     102             :      saturating_add chain). Plain + wraps on u64::MAX inputs (e.g. adversarial
     103             :      actual_programs_execution_cost = u64::MAX). */
     104        1003 :   result->total_cost = fd_ulong_sat_add( result->signature_cost, result->write_lock_cost );
     105        1003 :   result->total_cost = fd_ulong_sat_add( result->total_cost, result->data_bytes_cost );
     106        1003 :   result->total_cost = fd_ulong_sat_add( result->total_cost, result->programs_execution_cost );
     107        1003 :   result->total_cost = fd_ulong_sat_add( result->total_cost, result->loaded_accounts_data_size_cost );
     108             : 
     109        1003 :   ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     110        1003 :   *output = result;
     111        1003 :   return actual_end - (ulong)output_buf;
     112        1003 : }

Generated by: LCOV version 1.14