LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_dump_pb.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 765 0.0 %
Date: 2025-10-14 04:31:44 Functions: 0 20 0.0 %

          Line data    Source code
       1             : #include "fd_dump_pb.h"
       2             : #include "generated/block.pb.h"
       3             : #include "generated/invoke.pb.h"
       4             : #include "generated/txn.pb.h"
       5             : #include "generated/vm.pb.h"
       6             : #include "../fd_system_ids.h"
       7             : #include "../fd_bank.h"
       8             : #include "../program/fd_address_lookup_table_program.h"
       9             : #include "../../../ballet/nanopb/pb_encode.h"
      10             : #include "../program/fd_program_cache.h"
      11             : 
      12             : 
      13             : #include <stdio.h> /* fopen */
      14             : #include <sys/mman.h> /* mmap */
      15             : #include <unistd.h> /* ftruncate */
      16             : 
      17             : #define SORT_NAME        sort_uint64_t
      18           0 : #define SORT_KEY_T       uint64_t
      19           0 : #define SORT_BEFORE(a,b) (a)<(b)
      20             : #include "../../../util/tmpl/fd_sort.c"
      21             : 
      22             : /***** UTILITY FUNCTIONS *****/
      23             : 
      24             : /** GENERAL UTILITY FUNCTIONS AND MACROS **/
      25             : 
      26             : static int
      27             : is_builtin_account( fd_pubkey_t const * loaded_builtins,
      28             :                     ulong               num_loaded_builtins,
      29           0 :                     fd_pubkey_t const * account_key ) {
      30           0 :   for( ulong j = 0; j < num_loaded_builtins; ++j ) {
      31           0 :     if( !memcmp( account_key, &loaded_builtins[j], sizeof(fd_pubkey_t) ) ) {
      32           0 :       return 1;
      33           0 :     }
      34           0 :   }
      35           0 :   return 0;
      36           0 : }
      37             : 
      38             : /** FEATURE DUMPING **/
      39             : static void
      40             : dump_sorted_features( fd_features_t const * features,
      41             :                       fd_exec_test_feature_set_t * output_feature_set,
      42           0 :                       fd_spad_t * spad ) {
      43             :   /* NOTE: Caller must have a spad frame prepared */
      44           0 :   uint64_t * unsorted_features = fd_spad_alloc( spad, alignof(uint64_t), FD_FEATURE_ID_CNT * sizeof(uint64_t) );
      45           0 :   ulong num_features = 0;
      46           0 :   for( const fd_feature_id_t * current_feature = fd_feature_iter_init(); !fd_feature_iter_done( current_feature ); current_feature = fd_feature_iter_next( current_feature ) ) {
      47           0 :     if (features->f[current_feature->index] != FD_FEATURE_DISABLED) {
      48           0 :       unsorted_features[num_features++] = (uint64_t) current_feature->id.ul[0];
      49           0 :     }
      50           0 :   }
      51             :   // Sort the features
      52           0 :   void * scratch = fd_spad_alloc( spad, sort_uint64_t_stable_scratch_align(), sort_uint64_t_stable_scratch_footprint(num_features) );
      53           0 :   uint64_t * sorted_features = sort_uint64_t_stable_fast( unsorted_features, num_features, scratch );
      54             : 
      55             :   // Set feature set in message
      56           0 :   output_feature_set->features_count = (pb_size_t) num_features;
      57           0 :   output_feature_set->features       = sorted_features;
      58           0 : }
      59             : 
      60             : /** ACCOUNT DUMPING **/
      61             : static void
      62             : dump_account_state( fd_txn_account_t const *    txn_account,
      63             :                     fd_exec_test_acct_state_t * output_account,
      64           0 :                     fd_spad_t *                 spad ) {
      65             :     // Address
      66           0 :     fd_memcpy(output_account->address, txn_account->pubkey, sizeof(fd_pubkey_t));
      67             : 
      68             :     // Lamports
      69           0 :     output_account->lamports = (uint64_t)fd_txn_account_get_lamports( txn_account );
      70             : 
      71             :     // Data
      72           0 :     output_account->data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( fd_txn_account_get_data_len( txn_account ) ) );
      73           0 :     output_account->data->size = (pb_size_t) fd_txn_account_get_data_len( txn_account );
      74           0 :     fd_memcpy(output_account->data->bytes, fd_txn_account_get_data( txn_account ), fd_txn_account_get_data_len( txn_account ) );
      75             : 
      76             :     // Executable
      77           0 :     output_account->executable = (bool)fd_txn_account_is_executable( txn_account );
      78             : 
      79             :     // Owner
      80           0 :     fd_memcpy(output_account->owner, fd_txn_account_get_owner( txn_account ), sizeof(fd_pubkey_t));
      81             : 
      82             :     // Seed address (not present)
      83           0 :     output_account->has_seed_addr = false;
      84           0 : }
      85             : 
      86             : static uchar
      87             : account_already_dumped( fd_exec_test_acct_state_t const * dumped_accounts,
      88             :                         ulong                             dumped_cnt,
      89           0 :                         fd_pubkey_t const *               account_key ) {
      90           0 :   for( ulong i=0UL; i<dumped_cnt; i++ ) {
      91           0 :     if( !memcmp( account_key, dumped_accounts[i].address, sizeof(fd_pubkey_t) ) ) {
      92           0 :       return 1;
      93           0 :     }
      94           0 :   }
      95           0 :   return 0;
      96           0 : }
      97             : 
      98             : /* Dumps a borrowed account if it exists and has not been dumped yet. Sets up the output borrowed
      99             :    account if it exists. Returns 0 if the account exists, 1 otherwise. */
     100             : static uchar
     101             : dump_account_if_not_already_dumped( fd_funk_t const *           funk,
     102             :                                     fd_funk_txn_xid_t const *   xid,
     103             :                                     fd_pubkey_t const *         account_key,
     104             :                                     fd_spad_t *                 spad,
     105             :                                     fd_exec_test_acct_state_t * out_acct_states,
     106             :                                     pb_size_t *                 out_acct_states_cnt,
     107           0 :                                     fd_txn_account_t *          opt_out_borrowed_account ) {
     108           0 :   FD_TXN_ACCOUNT_DECL( account );
     109           0 :   if( fd_txn_account_init_from_funk_readonly( account, account_key, funk, xid ) ) {
     110           0 :     return 1;
     111           0 :   }
     112             : 
     113           0 :   if( !account_already_dumped( out_acct_states, *out_acct_states_cnt, account_key ) ) {
     114           0 :     dump_account_state( account, &out_acct_states[*out_acct_states_cnt], spad );
     115           0 :     (*out_acct_states_cnt)++;
     116           0 :   }
     117             : 
     118           0 :   if( opt_out_borrowed_account ) {
     119           0 :     *opt_out_borrowed_account = *account;
     120           0 :   }
     121           0 :   return 0;
     122           0 : }
     123             : 
     124             : /* TODO: This can be made slightly more efficient by dumping only the referenced ALUT accounts instead of all accounts */
     125             : static void
     126             : dump_lut_account_and_contained_accounts( fd_funk_t const *              funk,
     127             :                                          fd_funk_txn_xid_t const *      xid,
     128             :                                          uchar const *                  txn_payload,
     129             :                                          fd_txn_acct_addr_lut_t const * lookup_table,
     130             :                                          fd_spad_t *                    spad,
     131             :                                          fd_exec_test_acct_state_t *    out_account_states,
     132           0 :                                          pb_size_t *                    out_account_states_count ) {
     133           0 :   FD_TXN_ACCOUNT_DECL( alut_account );
     134           0 :   fd_pubkey_t const * alut_pubkey = (fd_pubkey_t const *)((uchar *)txn_payload + lookup_table->addr_off);
     135           0 :   uchar account_exists = dump_account_if_not_already_dumped( funk, xid, alut_pubkey, spad, out_account_states, out_account_states_count, alut_account );
     136           0 :   if( !account_exists || fd_txn_account_get_data_len( alut_account )<FD_LOOKUP_TABLE_META_SIZE ) {
     137           0 :     return;
     138           0 :   }
     139             : 
     140             :   /* Decode the ALUT account and find its referenced writable and readonly indices */
     141           0 :   if( fd_txn_account_get_data_len( alut_account ) & 0x1fUL ) {
     142           0 :     return;
     143           0 :   }
     144             : 
     145           0 :   fd_pubkey_t * lookup_addrs = (fd_pubkey_t *)&fd_txn_account_get_data( alut_account )[FD_LOOKUP_TABLE_META_SIZE];
     146           0 :   ulong lookup_addrs_cnt     = ( fd_txn_account_get_data_len( alut_account ) - FD_LOOKUP_TABLE_META_SIZE ) >> 5UL; // = (dlen - 56) / 32
     147           0 :   for( ulong i=0UL; i<lookup_addrs_cnt; i++ ) {
     148           0 :     fd_pubkey_t const * referenced_pubkey = &lookup_addrs[i];
     149           0 :     dump_account_if_not_already_dumped( funk, xid, referenced_pubkey, spad, out_account_states, out_account_states_count, NULL );
     150           0 :   }
     151           0 : }
     152             : 
     153             : static void
     154             : dump_executable_account_if_exists( fd_funk_t const *                 funk,
     155             :                                    fd_funk_txn_xid_t const *         xid,
     156             :                                    fd_exec_test_acct_state_t const * program_account,
     157             :                                    fd_spad_t *                       spad,
     158             :                                    fd_exec_test_acct_state_t *       out_account_states,
     159           0 :                                    pb_size_t *                       out_account_states_count ) {
     160           0 :   if( FD_LIKELY( memcmp( program_account->owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
     161           0 :     return;
     162           0 :   }
     163             : 
     164           0 :   int err;
     165           0 :   fd_bpf_upgradeable_loader_state_t * program_loader_state = fd_bincode_decode_spad(
     166           0 :       bpf_upgradeable_loader_state,
     167           0 :       spad,
     168           0 :       program_account->data->bytes,
     169           0 :       program_account->data->size,
     170           0 :       &err );
     171           0 :   if( FD_UNLIKELY( err ) ) return;
     172             : 
     173           0 :   if( !fd_bpf_upgradeable_loader_state_is_program( program_loader_state ) ) {
     174           0 :     return;
     175           0 :   }
     176             : 
     177           0 :   fd_pubkey_t * programdata_acc = &program_loader_state->inner.program.programdata_address;
     178           0 :   dump_account_if_not_already_dumped( funk, xid, programdata_acc, spad, out_account_states, out_account_states_count, NULL );
     179           0 : }
     180             : 
     181             : static void
     182             : dump_vote_accounts( fd_funk_t *                 funk,
     183             :                     fd_funk_txn_xid_t const *   xid,
     184             :                     fd_vote_states_t const *    vote_states,
     185             :                     fd_spad_t *                 spad,
     186             :                     fd_exec_test_acct_state_t * out_acct_states,
     187           0 :                     pb_size_t *                 out_acct_states_cnt ) {
     188             : 
     189           0 :   fd_vote_states_iter_t iter_[1];
     190           0 :   for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states ); !fd_vote_states_iter_done( iter ); fd_vote_states_iter_next( iter ) ) {
     191           0 :     fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
     192           0 :     dump_account_if_not_already_dumped( funk, xid, &vote_state->vote_account, spad, out_acct_states, out_acct_states_cnt, NULL );
     193           0 :   }
     194           0 : }
     195             : 
     196             : static void
     197             : dump_sanitized_transaction( fd_funk_t *                            funk,
     198             :                             fd_funk_txn_xid_t const *              xid,
     199             :                             fd_txn_t const *                       txn_descriptor,
     200             :                             uchar const *                          txn_payload,
     201             :                             fd_spad_t *                            spad,
     202           0 :                             fd_exec_test_sanitized_transaction_t * sanitized_transaction ) {
     203           0 :   fd_txn_acct_addr_lut_t const * address_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
     204             : 
     205             :   /* Transaction Context -> tx -> message */
     206           0 :   sanitized_transaction->has_message = true;
     207           0 :   fd_exec_test_transaction_message_t * message = &sanitized_transaction->message;
     208             : 
     209             :   /* Transaction Context -> tx -> message -> is_legacy */
     210           0 :   message->is_legacy = txn_descriptor->transaction_version == FD_TXN_VLEGACY;
     211             : 
     212             :   /* Transaction Context -> tx -> message -> header */
     213           0 :   message->has_header = true;
     214           0 :   fd_exec_test_message_header_t * header = &message->header;
     215             : 
     216             :   /* Transaction Context -> tx -> message -> header -> num_required_signatures */
     217           0 :   header->num_required_signatures = txn_descriptor->signature_cnt;
     218             : 
     219             :   /* Transaction Context -> tx -> message -> header -> num_readonly_signed_accounts */
     220           0 :   header->num_readonly_signed_accounts = txn_descriptor->readonly_signed_cnt;
     221             : 
     222             :   /* Transaction Context -> tx -> message -> header -> num_readonly_unsigned_accounts */
     223           0 :   header->num_readonly_unsigned_accounts = txn_descriptor->readonly_unsigned_cnt;
     224             : 
     225             :   /* Transaction Context -> tx -> message -> account_keys */
     226           0 :   message->account_keys_count = txn_descriptor->acct_addr_cnt;
     227           0 :   message->account_keys = fd_spad_alloc( spad, alignof(pb_bytes_array_t *), PB_BYTES_ARRAY_T_ALLOCSIZE(txn_descriptor->acct_addr_cnt * sizeof(pb_bytes_array_t *)) );
     228           0 :   fd_acct_addr_t const * account_keys = fd_txn_get_acct_addrs( txn_descriptor, txn_payload );
     229           0 :   for( ulong i = 0; i < txn_descriptor->acct_addr_cnt; i++ ) {
     230           0 :     pb_bytes_array_t * account_key = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_pubkey_t)) );
     231           0 :     account_key->size = sizeof(fd_pubkey_t);
     232           0 :     memcpy( account_key->bytes, &account_keys[i], sizeof(fd_pubkey_t) );
     233           0 :     message->account_keys[i] = account_key;
     234           0 :   }
     235             : 
     236             :   /* Transaction Context -> tx -> message -> recent_blockhash */
     237           0 :   uchar const * recent_blockhash = fd_txn_get_recent_blockhash( txn_descriptor, txn_payload );
     238           0 :   message->recent_blockhash = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_hash_t)) );
     239           0 :   message->recent_blockhash->size = sizeof(fd_hash_t);
     240           0 :   memcpy( message->recent_blockhash->bytes, recent_blockhash, sizeof(fd_hash_t) );
     241             : 
     242             :   /* Transaction Context -> tx -> message -> instructions */
     243           0 :   message->instructions_count = txn_descriptor->instr_cnt;
     244           0 :   message->instructions = fd_spad_alloc( spad, alignof(fd_exec_test_compiled_instruction_t), txn_descriptor->instr_cnt * sizeof(fd_exec_test_compiled_instruction_t) );
     245           0 :   for( ulong i = 0; i < txn_descriptor->instr_cnt; ++i ) {
     246           0 :     fd_txn_instr_t instr = txn_descriptor->instr[i];
     247           0 :     fd_exec_test_compiled_instruction_t * compiled_instruction = &message->instructions[i];
     248             : 
     249             :     // compiled instruction -> program_id_index
     250           0 :     compiled_instruction->program_id_index = instr.program_id;
     251             : 
     252             :     // compiled instruction -> accounts
     253           0 :     compiled_instruction->accounts_count = instr.acct_cnt;
     254           0 :     compiled_instruction->accounts = fd_spad_alloc( spad, alignof(uint32_t), instr.acct_cnt * sizeof(uint32_t) );
     255           0 :     uchar const * instr_accounts = fd_txn_get_instr_accts( &instr, txn_payload );
     256           0 :     for( ulong j = 0; j < instr.acct_cnt; ++j ) {
     257           0 :       uchar instr_acct_index = instr_accounts[j];
     258           0 :       compiled_instruction->accounts[j] = instr_acct_index;
     259           0 :     }
     260             : 
     261             :     // compiled instruction -> data
     262           0 :     uchar const * instr_data = fd_txn_get_instr_data( &instr, txn_payload );
     263           0 :     compiled_instruction->data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(instr.data_sz) );
     264           0 :     compiled_instruction->data->size = instr.data_sz;
     265           0 :     memcpy( compiled_instruction->data->bytes, instr_data, instr.data_sz );
     266           0 :   }
     267             : 
     268             :   /* ALUT stuff (non-legacy) */
     269           0 :   message->address_table_lookups_count = 0;
     270           0 :   if( !message->is_legacy ) {
     271             :     /* Transaction Context -> tx -> message -> address_table_lookups */
     272           0 :     message->address_table_lookups_count = txn_descriptor->addr_table_lookup_cnt;
     273           0 :     message->address_table_lookups = fd_spad_alloc( spad,
     274           0 :                                                     alignof(fd_exec_test_message_address_table_lookup_t),
     275           0 :                                                     txn_descriptor->addr_table_lookup_cnt * sizeof(fd_exec_test_message_address_table_lookup_t) );
     276           0 :     for( ulong i = 0; i < txn_descriptor->addr_table_lookup_cnt; ++i ) {
     277             :       // alut -> account_key
     278           0 :       fd_pubkey_t * alut_key = (fd_pubkey_t *) (txn_payload + address_lookup_tables[i].addr_off);
     279           0 :       memcpy( message->address_table_lookups[i].account_key, alut_key, sizeof(fd_pubkey_t) );
     280             : 
     281             :       // Access ALUT account data to access its keys
     282           0 :       FD_TXN_ACCOUNT_DECL(addr_lut_rec);
     283           0 :       int err = fd_txn_account_init_from_funk_readonly( addr_lut_rec, alut_key, funk, xid );
     284           0 :       if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) ) {
     285           0 :         FD_LOG_ERR(( "addr lut not found" ));
     286           0 :       }
     287             : 
     288             :       // alut -> writable_indexes
     289           0 :       message->address_table_lookups[i].writable_indexes_count = address_lookup_tables[i].writable_cnt;
     290           0 :       message->address_table_lookups[i].writable_indexes = fd_spad_alloc( spad, alignof(uint32_t), address_lookup_tables[i].writable_cnt * sizeof(uint32_t) );
     291           0 :       uchar * writable_indexes = (uchar *) (txn_payload + address_lookup_tables[i].writable_off);
     292           0 :       for( ulong j = 0; j < address_lookup_tables[i].writable_cnt; ++j ) {
     293           0 :         message->address_table_lookups[i].writable_indexes[j] = writable_indexes[j];
     294           0 :       }
     295             : 
     296             :       // alut -> readonly_indexes
     297           0 :       message->address_table_lookups[i].readonly_indexes_count = address_lookup_tables[i].readonly_cnt;
     298           0 :       message->address_table_lookups[i].readonly_indexes = fd_spad_alloc( spad, alignof(uint32_t), address_lookup_tables[i].readonly_cnt * sizeof(uint32_t) );
     299           0 :       uchar * readonly_indexes = (uchar *) (txn_payload + address_lookup_tables[i].readonly_off);
     300           0 :       for( ulong j = 0; j < address_lookup_tables[i].readonly_cnt; ++j ) {
     301           0 :         message->address_table_lookups[i].readonly_indexes[j] = readonly_indexes[j];
     302           0 :       }
     303           0 :     }
     304           0 :   }
     305             : 
     306             :   /* Transaction Context -> tx -> message_hash */
     307             :   // Skip because it does not matter what's in here
     308             : 
     309             :   /* Transaction Context -> tx -> signatures */
     310           0 :   sanitized_transaction->signatures_count = txn_descriptor->signature_cnt;
     311           0 :   sanitized_transaction->signatures = fd_spad_alloc( spad, alignof(pb_bytes_array_t *), PB_BYTES_ARRAY_T_ALLOCSIZE(txn_descriptor->signature_cnt * sizeof(pb_bytes_array_t *)) );
     312           0 :   fd_ed25519_sig_t const * signatures = fd_txn_get_signatures( txn_descriptor, txn_payload );
     313           0 :   for( uchar i = 0; i < txn_descriptor->signature_cnt; ++i ) {
     314           0 :     pb_bytes_array_t * signature = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_ed25519_sig_t)) );
     315           0 :     signature->size = sizeof(fd_ed25519_sig_t);
     316           0 :     memcpy( signature->bytes, &signatures[i], sizeof(fd_ed25519_sig_t) );
     317           0 :     sanitized_transaction->signatures[i] = signature;
     318           0 :   }
     319           0 : }
     320             : 
     321             : /** BLOCKHASH QUEUE DUMPING **/
     322             : 
     323             : static void
     324             : dump_blockhash_queue( fd_blockhashes_t const * queue,
     325             :                       fd_spad_t *              spad,
     326             :                       pb_bytes_array_t **      output_blockhash_queue,
     327           0 :                       pb_size_t *              output_blockhash_queue_count ) {
     328           0 :   pb_size_t cnt = 0;
     329             : 
     330             :   // Iterate over all block hashes in the queue and save them in the output
     331           0 :   for( fd_blockhash_deq_iter_t iter=fd_blockhash_deq_iter_init_rev( queue->d.deque );
     332           0 :        !fd_blockhash_deq_iter_done_rev( queue->d.deque, iter );
     333           0 :        iter=fd_blockhash_deq_iter_prev( queue->d.deque, iter ) ) {
     334           0 :     fd_blockhash_info_t const * ele = fd_blockhash_deq_iter_ele_const( queue->d.deque, iter );
     335           0 :     pb_bytes_array_t * output_blockhash = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_hash_t)) );
     336           0 :     output_blockhash->size = sizeof(fd_hash_t);
     337           0 :     fd_memcpy( output_blockhash->bytes, &ele->hash, sizeof(fd_hash_t) );
     338           0 :     output_blockhash_queue[ cnt ] = output_blockhash;
     339           0 :     cnt++;
     340           0 :   }
     341             : 
     342           0 :   *output_blockhash_queue_count = cnt;
     343           0 : }
     344             : 
     345             : /** SECONDARY FUNCTIONS **/
     346             : 
     347             : static void
     348             : create_block_context_protobuf_from_block( fd_exec_test_block_context_t * block_context,
     349             :                                           fd_banks_t *                   banks,
     350             :                                           fd_bank_t *                    bank,
     351             :                                           fd_funk_t *                    funk,
     352             :                                           fd_funk_txn_xid_t const *      xid,
     353           0 :                                           fd_spad_t *                    spad ) {
     354             : 
     355             :   /* BlockContext -> acct_states */
     356             :   // Dump sysvars + builtins
     357           0 :   fd_pubkey_t const fd_relevant_sysvar_ids[] = {
     358           0 :     fd_sysvar_recent_block_hashes_id,
     359           0 :     fd_sysvar_clock_id,
     360           0 :     fd_sysvar_slot_history_id,
     361           0 :     fd_sysvar_slot_hashes_id,
     362           0 :     fd_sysvar_epoch_schedule_id,
     363           0 :     fd_sysvar_epoch_rewards_id,
     364           0 :     fd_sysvar_fees_id,
     365           0 :     fd_sysvar_rent_id,
     366           0 :     fd_sysvar_stake_history_id,
     367           0 :     fd_sysvar_last_restart_slot_id,
     368           0 :   };
     369             : 
     370           0 :   fd_pubkey_t const loaded_builtins[] = {
     371           0 :     fd_solana_system_program_id,
     372           0 :     fd_solana_vote_program_id,
     373           0 :     fd_solana_stake_program_id,
     374           0 :     fd_solana_config_program_id,
     375           0 :     fd_solana_zk_token_proof_program_id,
     376           0 :     fd_solana_bpf_loader_v4_program_id,
     377           0 :     fd_solana_address_lookup_table_program_id,
     378           0 :     fd_solana_bpf_loader_deprecated_program_id,
     379           0 :     fd_solana_bpf_loader_program_id,
     380           0 :     fd_solana_bpf_loader_upgradeable_program_id,
     381           0 :     fd_solana_compute_budget_program_id,
     382           0 :     fd_solana_keccak_secp_256k_program_id,
     383           0 :     fd_solana_secp256r1_program_id,
     384           0 :     fd_solana_zk_elgamal_proof_program_id,
     385           0 :     fd_solana_ed25519_sig_verify_program_id,
     386           0 :   };
     387           0 :   ulong num_sysvar_entries    = (sizeof(fd_relevant_sysvar_ids) / sizeof(fd_pubkey_t));
     388           0 :   ulong num_loaded_builtins   = (sizeof(loaded_builtins) / sizeof(fd_pubkey_t));
     389             : 
     390           0 :   fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_frontier_query( banks, bank );
     391           0 :   ulong stake_account_cnt     = fd_stake_delegations_cnt( stake_delegations );
     392             : 
     393           0 :   fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( bank );
     394           0 :   ulong vote_account_t_cnt    = fd_vote_states_cnt( vote_states );
     395           0 :   fd_bank_vote_states_end_locking_query( bank );
     396             : 
     397           0 :   fd_vote_states_t const * vote_states_prev = fd_bank_vote_states_prev_locking_query( bank );
     398           0 :   ulong vote_account_t_1_cnt  = fd_vote_states_cnt( vote_states_prev );
     399           0 :   fd_bank_vote_states_prev_end_locking_query( bank );
     400             : 
     401           0 :   fd_vote_states_t const * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_query( bank );
     402           0 :   ulong vote_account_t_2_cnt = fd_vote_states_cnt( vote_states_prev_prev );
     403           0 :   fd_bank_vote_states_prev_prev_end_locking_query( bank );
     404             : 
     405           0 :   ulong total_num_accounts    = num_sysvar_entries +
     406           0 :                                 num_loaded_builtins +
     407           0 :                                 stake_account_cnt +
     408           0 :                                 vote_account_t_cnt +
     409           0 :                                 vote_account_t_1_cnt +
     410           0 :                                 vote_account_t_2_cnt;
     411             : 
     412           0 :   block_context->acct_states_count = 0;
     413           0 :   block_context->acct_states       = fd_spad_alloc( spad,
     414           0 :                                                     alignof(fd_exec_test_acct_state_t),
     415           0 :                                                     total_num_accounts * sizeof(fd_exec_test_acct_state_t) );
     416             : 
     417             : 
     418           0 :   for( ulong i=0UL; i<num_sysvar_entries; i++ ) {
     419           0 :     dump_account_if_not_already_dumped( funk, xid, &fd_relevant_sysvar_ids[i], spad, block_context->acct_states, &block_context->acct_states_count, NULL );
     420           0 :   }
     421             : 
     422           0 :   for( ulong i=0UL; i<num_loaded_builtins; i++ ) {
     423           0 :     dump_account_if_not_already_dumped( funk, xid, &loaded_builtins[i], spad, block_context->acct_states, &block_context->acct_states_count, NULL );
     424           0 :   }
     425             : 
     426             :   /* BlockContext -> blockhash_queue */
     427           0 :   pb_bytes_array_t ** output_blockhash_queue = fd_spad_alloc( spad,
     428           0 :                                                               alignof(pb_bytes_array_t *),
     429           0 :                                                               PB_BYTES_ARRAY_T_ALLOCSIZE((FD_BLOCKHASHES_MAX) * sizeof(pb_bytes_array_t *)) );
     430           0 :   block_context->blockhash_queue = output_blockhash_queue;
     431             : 
     432           0 :   fd_blockhashes_t const * bhq = fd_bank_block_hash_queue_query( bank );
     433           0 :   dump_blockhash_queue( bhq, spad, block_context->blockhash_queue, &block_context->blockhash_queue_count );
     434             : 
     435             :   /* BlockContext -> SlotContext */
     436           0 :   block_context->has_slot_ctx                       = true;
     437           0 :   block_context->slot_ctx.slot                      = fd_bank_slot_get( bank );
     438             :   // HACK FOR NOW: block height gets incremented in process_new_epoch, so we should dump block height + 1
     439           0 :   block_context->slot_ctx.block_height              = fd_bank_block_height_get( bank ) + 1UL;
     440             :   // fd_memcpy( block_context->slot_ctx.poh, &slot_ctx->slot_bank.poh, sizeof(fd_pubkey_t) ); // TODO: dump here when process epoch happens after poh verification
     441           0 :   fd_memcpy( block_context->slot_ctx.parent_bank_hash, fd_bank_bank_hash_query( bank ), sizeof(fd_pubkey_t) );
     442           0 :   block_context->slot_ctx.prev_slot                 = fd_bank_parent_slot_get( bank );
     443           0 :   block_context->slot_ctx.prev_lps                  = fd_bank_prev_lamports_per_signature_get( bank );
     444           0 :   block_context->slot_ctx.prev_epoch_capitalization = fd_bank_capitalization_get( bank );
     445             : 
     446             :   /* BlockContext -> EpochContext */
     447           0 :   block_context->has_epoch_ctx                        = true;
     448           0 :   block_context->epoch_ctx.has_features               = true;
     449           0 :   dump_sorted_features( fd_bank_features_query( bank ), &block_context->epoch_ctx.features, spad );
     450           0 :   block_context->epoch_ctx.hashes_per_tick            = fd_bank_hashes_per_tick_get( bank );
     451           0 :   block_context->epoch_ctx.ticks_per_slot             = fd_bank_ticks_per_slot_get( bank );
     452           0 :   block_context->epoch_ctx.slots_per_year             = fd_bank_slots_per_year_get( bank );
     453           0 :   block_context->epoch_ctx.has_inflation              = true;
     454             : 
     455           0 :   fd_inflation_t const * inflation = fd_bank_inflation_query( bank );
     456           0 :   block_context->epoch_ctx.inflation                  = (fd_exec_test_inflation_t) {
     457           0 :     .initial         = inflation->initial,
     458           0 :     .terminal        = inflation->terminal,
     459           0 :     .taper           = inflation->taper,
     460           0 :     .foundation      = inflation->foundation,
     461           0 :     .foundation_term = inflation->foundation_term,
     462           0 :   };
     463           0 :   block_context->epoch_ctx.genesis_creation_time      = fd_bank_genesis_creation_time_get( bank );
     464             : 
     465             :   /* Dumping stake accounts for this epoch */
     466             : 
     467           0 :   fd_stake_delegations_iter_t iter_[1];
     468           0 :   for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
     469           0 :        !fd_stake_delegations_iter_done( iter );
     470           0 :        fd_stake_delegations_iter_next( iter ) ) {
     471           0 :     fd_stake_delegation_t * stake_delegation = fd_stake_delegations_iter_ele( iter );
     472           0 :     dump_account_if_not_already_dumped( funk, xid, &stake_delegation->stake_account, spad, block_context->acct_states, &block_context->acct_states_count, NULL );
     473           0 :   }
     474             : 
     475             :   /* Dumping vote accounts for this epoch */
     476             : 
     477           0 :   vote_states = fd_bank_vote_states_locking_query( bank );
     478           0 :   fd_vote_states_iter_t vote_iter_[1];
     479           0 :   for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( vote_iter_, vote_states ); !fd_vote_states_iter_done( iter ); fd_vote_states_iter_next( iter ) ) {
     480           0 :     fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
     481           0 :     dump_account_if_not_already_dumped( funk, xid, &vote_state->vote_account, spad, block_context->acct_states, &block_context->acct_states_count, NULL );
     482           0 :   }
     483             : 
     484           0 :   fd_bank_vote_states_end_locking_query( bank );
     485             : 
     486             :   // BlockContext -> EpochContext -> vote_accounts_t_1 (vote accounts at epoch T-1)
     487           0 :   vote_states_prev = fd_bank_vote_states_prev_locking_query( bank );
     488           0 :   dump_vote_accounts( funk,
     489           0 :                       xid,
     490           0 :                       vote_states_prev,
     491           0 :                       spad,
     492           0 :                       block_context->acct_states,
     493           0 :                       &block_context->acct_states_count );
     494           0 :   fd_bank_vote_states_prev_end_locking_query( bank );
     495             : 
     496             :   // BlockContext -> EpochContext -> vote_accounts_t_2 (vote accounts at epoch T-2)
     497           0 :   vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_query( bank );
     498           0 :   dump_vote_accounts( funk,
     499           0 :                       xid,
     500           0 :                       vote_states,
     501           0 :                       spad,
     502           0 :                       block_context->acct_states,
     503           0 :                       &block_context->acct_states_count );
     504           0 :   fd_bank_vote_states_prev_prev_end_locking_query( bank );
     505           0 : }
     506             : 
     507             : static void
     508             : create_block_context_protobuf_from_block_tx_only( fd_exec_test_block_context_t *  block_context,
     509             :                                                   fd_runtime_block_info_t const * block_info,
     510             :                                                   fd_bank_t *                     bank,
     511             :                                                   fd_funk_t *                     funk,
     512             :                                                   fd_funk_txn_xid_t const *       xid,
     513           0 :                                                   fd_spad_t *                     spad ) {
     514             :   /* BlockContext -> txns */
     515           0 :   block_context->txns_count = 0U;
     516           0 :   block_context->txns       = fd_spad_alloc( spad, alignof(fd_exec_test_sanitized_transaction_t), block_info->txn_cnt * sizeof(fd_exec_test_sanitized_transaction_t) );
     517           0 :   fd_memset( block_context->txns, 0, block_info->txn_cnt * sizeof(fd_exec_test_sanitized_transaction_t) );
     518             : 
     519             :   /* BlockContext -> acct_states
     520             :      Allocate additional space for the remaining accounts */
     521           0 :   fd_exec_test_acct_state_t * current_accounts = block_context->acct_states;
     522           0 :   block_context->acct_states                   = fd_spad_alloc( spad,
     523           0 :                                                                 alignof(fd_exec_test_acct_state_t),
     524           0 :                                                                 ( ( block_info->txn_cnt * 128UL ) + (ulong)block_context->acct_states_count ) *
     525           0 :                                                                 sizeof(fd_exec_test_acct_state_t) );
     526           0 :   fd_memcpy( block_context->acct_states, current_accounts, block_context->acct_states_count * sizeof(fd_exec_test_acct_state_t) );
     527             : 
     528             :   /* BlockContext -> slot_ctx -> poh
     529             :      This currently needs to be done because POH verification is done after epoch boundary processing. That should probably be changed */
     530           0 :   fd_memcpy( block_context->slot_ctx.poh, fd_bank_poh_query( bank )->hash, sizeof(fd_pubkey_t) );
     531             : 
     532             :   /* When iterating over microblocks batches and microblocks, we flatten the batches for the output block context (essentially just one big batch with several microblocks) */
     533           0 :   for( ulong i=0UL; i<block_info->microblock_batch_cnt; i++ ) {
     534           0 :     fd_microblock_batch_info_t const * microblock_batch = &block_info->microblock_batch_infos[i];
     535             : 
     536           0 :     for( ulong j=0UL; j<microblock_batch->microblock_cnt; j++ ) {
     537           0 :       fd_microblock_info_t const * microblock_info = &microblock_batch->microblock_infos[j];
     538           0 :       ulong                        txn_cnt         = microblock_info->microblock.hdr->txn_cnt;
     539           0 :       if (txn_cnt==0UL) continue;
     540             : 
     541             :       /* BlockContext -> txns */
     542           0 :       for( ulong k=0UL; k<txn_cnt; k++ ) {
     543           0 :         fd_txn_p_t const * txn_ptr      = &microblock_info->txns[k];
     544           0 :         fd_txn_t const * txn_descriptor = TXN( txn_ptr );
     545           0 :         dump_sanitized_transaction( funk, xid, txn_descriptor, txn_ptr->payload, spad, &block_context->txns[block_context->txns_count++] );
     546             : 
     547             :         /* BlockContext -> acct_states */
     548             :         /* Dump account + alut + programdata accounts (if applicable). There's a lot more brute force work since none of the borrowed accounts are set up yet. We have to:
     549             :            1. Dump the raw txn account keys
     550             :            2. Dump the ALUT accounts
     551             :            3. Dump all referenced accounts in the ALUTs
     552             :            4. Dump any executable accounts
     553             :            5. Dump any sysvars + builtin accounts (occurs outside of this loop) */
     554             : 
     555             :         // 1. Dump any account keys that are referenced by transactions
     556           0 :         fd_acct_addr_t const * account_keys = fd_txn_get_acct_addrs( txn_descriptor, txn_ptr->payload );
     557           0 :         for( ushort l=0; l<txn_descriptor->acct_addr_cnt; l++ ) {
     558           0 :           fd_pubkey_t const * account_key = fd_type_pun_const( &account_keys[l] );
     559           0 :           dump_account_if_not_already_dumped( funk, xid, account_key, spad, block_context->acct_states, &block_context->acct_states_count, NULL );
     560           0 :         }
     561             : 
     562             :         // 2 + 3. Dump any ALUT accounts + any accounts referenced in the ALUTs
     563           0 :         fd_txn_acct_addr_lut_t const * txn_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
     564           0 :         for( ushort l=0; l<txn_descriptor->addr_table_lookup_cnt; l++ ) {
     565           0 :           fd_txn_acct_addr_lut_t const * lookup_table = &txn_lookup_tables[l];
     566           0 :           dump_lut_account_and_contained_accounts( funk, xid, txn_ptr->payload, lookup_table, spad, block_context->acct_states, &block_context->acct_states_count );
     567           0 :         }
     568             : 
     569             :         // 4. Go through all dumped accounts and dump any executable accounts
     570           0 :         ulong dumped_accounts = block_context->acct_states_count;
     571           0 :         for( ulong l=0; l<dumped_accounts; l++ ) {
     572           0 :           fd_exec_test_acct_state_t const * maybe_program_account = &block_context->acct_states[l];
     573           0 :           dump_executable_account_if_exists( funk, xid, maybe_program_account, spad, block_context->acct_states, &block_context->acct_states_count );
     574           0 :         }
     575           0 :       }
     576           0 :     }
     577           0 :   }
     578           0 : }
     579             : 
     580             : static void
     581             : create_txn_context_protobuf_from_txn( fd_exec_test_txn_context_t * txn_context_msg,
     582             :                                       fd_exec_txn_ctx_t *          txn_ctx,
     583           0 :                                       fd_spad_t *                  spad ) {
     584           0 :   fd_txn_t const * txn_descriptor = TXN( &txn_ctx->txn );
     585           0 :   uchar const *    txn_payload    = (uchar const *) txn_ctx->txn.payload;
     586             : 
     587             :   /* We don't want to store builtins in account shared data */
     588           0 :   fd_pubkey_t const loaded_builtins[] = {
     589           0 :     fd_solana_system_program_id,
     590           0 :     fd_solana_vote_program_id,
     591           0 :     fd_solana_stake_program_id,
     592             :     // fd_solana_config_program_id, // migrated to BPF, so we should dump it
     593             :     // fd_solana_zk_token_proof_program_id,
     594           0 :     fd_solana_bpf_loader_v4_program_id,
     595             :     // fd_solana_address_lookup_table_program_id, // migrated to BPF, so we should dump it
     596           0 :     fd_solana_bpf_loader_deprecated_program_id,
     597           0 :     fd_solana_bpf_loader_program_id,
     598           0 :     fd_solana_bpf_loader_upgradeable_program_id,
     599           0 :     fd_solana_compute_budget_program_id,
     600           0 :     fd_solana_keccak_secp_256k_program_id,
     601           0 :     fd_solana_secp256r1_program_id,
     602           0 :     fd_solana_zk_elgamal_proof_program_id,
     603           0 :     fd_solana_ed25519_sig_verify_program_id,
     604           0 :   };
     605           0 :   const ulong num_loaded_builtins = (sizeof(loaded_builtins) / sizeof(fd_pubkey_t));
     606             : 
     607             :   /* Prepare sysvar cache accounts */
     608           0 :   fd_pubkey_t const fd_relevant_sysvar_ids[] = {
     609           0 :     fd_sysvar_recent_block_hashes_id,
     610           0 :     fd_sysvar_clock_id,
     611           0 :     fd_sysvar_slot_history_id,
     612           0 :     fd_sysvar_slot_hashes_id,
     613           0 :     fd_sysvar_epoch_schedule_id,
     614           0 :     fd_sysvar_epoch_rewards_id,
     615           0 :     fd_sysvar_fees_id,
     616           0 :     fd_sysvar_rent_id,
     617           0 :     fd_sysvar_stake_history_id,
     618           0 :     fd_sysvar_last_restart_slot_id,
     619           0 :   };
     620           0 :   const ulong num_sysvar_entries = (sizeof(fd_relevant_sysvar_ids) / sizeof(fd_pubkey_t));
     621             : 
     622             :   /* Transaction Context -> account_shared_data
     623             :      Contains:
     624             :       - Account data for regular accounts
     625             :       - Account data for LUT accounts
     626             :       - Account data for executable accounts
     627             :       - Account data for (almost) all sysvars
     628             :   */
     629             :   // Dump regular accounts first
     630           0 :   txn_context_msg->account_shared_data_count = 0;
     631           0 :   txn_context_msg->account_shared_data = fd_spad_alloc( spad,
     632           0 :                                                         alignof(fd_exec_test_acct_state_t),
     633           0 :                                                         (256UL*2UL + txn_descriptor->addr_table_lookup_cnt + num_sysvar_entries) * sizeof(fd_exec_test_acct_state_t) );
     634           0 :   for( ulong i = 0; i < txn_ctx->accounts_cnt; ++i ) {
     635           0 :     FD_TXN_ACCOUNT_DECL( txn_account );
     636           0 :     int ret = fd_txn_account_init_from_funk_readonly( txn_account, &txn_ctx->account_keys[i], txn_ctx->funk, txn_ctx->xid );
     637           0 :     if( FD_UNLIKELY( ret ) ) {
     638           0 :       continue;
     639           0 :     }
     640             : 
     641             :     // Make sure account is not a builtin
     642           0 :     if( !is_builtin_account( loaded_builtins, num_loaded_builtins, &txn_ctx->account_keys[i] ) ) {
     643           0 :       dump_account_state( txn_account, &txn_context_msg->account_shared_data[txn_context_msg->account_shared_data_count++], spad );
     644             : 
     645           0 :     }
     646           0 :   }
     647             : 
     648             :   // Dump LUT accounts
     649           0 :   fd_txn_acct_addr_lut_t const * address_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
     650           0 :   for( ulong i = 0; i < txn_descriptor->addr_table_lookup_cnt; ++i ) {
     651           0 :     FD_TXN_ACCOUNT_DECL( txn_account );
     652           0 :     fd_txn_acct_addr_lut_t const * addr_lut  = &address_lookup_tables[i];
     653           0 :     fd_pubkey_t * alut_key = (fd_pubkey_t *) (txn_payload + addr_lut[i].addr_off);
     654           0 :     int ret = fd_txn_account_init_from_funk_readonly( txn_account, alut_key, txn_ctx->funk, txn_ctx->xid );
     655           0 :     if( FD_UNLIKELY( ret ) ) continue;
     656             : 
     657           0 :     dump_account_state( txn_account, &txn_context_msg->account_shared_data[txn_context_msg->account_shared_data_count++], spad );
     658             : 
     659           0 :     fd_acct_addr_t * lookup_addrs  = (fd_acct_addr_t *)&fd_txn_account_get_data( txn_account )[FD_LOOKUP_TABLE_META_SIZE];
     660           0 :     ulong lookup_addrs_cnt         = (fd_txn_account_get_data_len( txn_account ) - FD_LOOKUP_TABLE_META_SIZE) >> 5UL; // = (dlen - 56) / 32
     661             : 
     662             :     /* Dump any account state refererenced in ALUTs */
     663           0 :     uchar const * writable_lut_idxs = txn_payload + addr_lut->writable_off;
     664           0 :     for( ulong j=0; j<addr_lut->writable_cnt; j++ ) {
     665           0 :       if( writable_lut_idxs[j] >= lookup_addrs_cnt ) {
     666           0 :         continue;
     667           0 :       }
     668           0 :       fd_pubkey_t const * referenced_addr = fd_type_pun( &lookup_addrs[writable_lut_idxs[j]] );
     669           0 :       if( is_builtin_account( loaded_builtins, num_loaded_builtins, referenced_addr ) ) continue;
     670             : 
     671           0 :       FD_TXN_ACCOUNT_DECL( referenced_account );
     672           0 :       ret = fd_txn_account_init_from_funk_readonly( referenced_account, referenced_addr, txn_ctx->funk, txn_ctx->xid );
     673           0 :       if( FD_UNLIKELY( ret ) ) continue;
     674           0 :       dump_account_state( referenced_account, &txn_context_msg->account_shared_data[txn_context_msg->account_shared_data_count++], spad );
     675           0 :     }
     676             : 
     677           0 :     uchar const * readonly_lut_idxs = txn_payload + addr_lut->readonly_off;
     678           0 :     for( ulong j = 0; j < addr_lut->readonly_cnt; j++ ) {
     679           0 :       if( readonly_lut_idxs[j] >= lookup_addrs_cnt ) {
     680           0 :         continue;
     681           0 :       }
     682           0 :       fd_pubkey_t const * referenced_addr = fd_type_pun( &lookup_addrs[readonly_lut_idxs[j]] );
     683           0 :       if( is_builtin_account( loaded_builtins, num_loaded_builtins, referenced_addr ) ) continue;
     684             : 
     685           0 :       FD_TXN_ACCOUNT_DECL( referenced_account );
     686           0 :       ret = fd_txn_account_init_from_funk_readonly( referenced_account, referenced_addr, txn_ctx->funk, txn_ctx->xid );
     687           0 :       if( FD_UNLIKELY( ret ) ) continue;
     688           0 :       dump_account_state( referenced_account, &txn_context_msg->account_shared_data[txn_context_msg->account_shared_data_count++], spad );
     689           0 :     }
     690           0 :   }
     691             : 
     692             :   /* Dump the programdata accounts for any potential v3-owned program accounts */
     693           0 :   uint accounts_dumped_so_far = txn_context_msg->account_shared_data_count;
     694           0 :   for( uint i=0U; i<accounts_dumped_so_far; i++ ) {
     695           0 :     fd_exec_test_acct_state_t const * maybe_program_account = &txn_context_msg->account_shared_data[i];
     696           0 :     dump_executable_account_if_exists( txn_ctx->funk, txn_ctx->xid, maybe_program_account, spad, txn_context_msg->account_shared_data, &txn_context_msg->account_shared_data_count );
     697           0 :   }
     698             : 
     699             :   /* Dump sysvars */
     700           0 :   for( ulong i = 0; i < num_sysvar_entries; i++ ) {
     701           0 :     FD_TXN_ACCOUNT_DECL( txn_account );
     702           0 :     int ret = fd_txn_account_init_from_funk_readonly( txn_account, &fd_relevant_sysvar_ids[i], txn_ctx->funk, txn_ctx->xid );
     703           0 :     if( ret != FD_ACC_MGR_SUCCESS ) {
     704           0 :       continue;
     705           0 :     }
     706             : 
     707             :     // Make sure the account doesn't exist in the output accounts yet
     708           0 :     int account_exists = 0;
     709           0 :     for( ulong j = 0; j < txn_ctx->accounts_cnt; j++ ) {
     710           0 :       if ( 0 == memcmp( txn_ctx->account_keys[j].key, fd_relevant_sysvar_ids[i].uc, sizeof(fd_pubkey_t) ) ) {
     711           0 :         account_exists = true;
     712           0 :         break;
     713           0 :       }
     714           0 :     }
     715             :     // Copy it into output
     716           0 :     if (!account_exists) {
     717           0 :       dump_account_state( txn_account, &txn_context_msg->account_shared_data[txn_context_msg->account_shared_data_count++], spad );
     718           0 :     }
     719           0 :   }
     720             : 
     721             :   /* Transaction Context -> tx */
     722           0 :   txn_context_msg->has_tx = true;
     723           0 :   fd_exec_test_sanitized_transaction_t * sanitized_transaction = &txn_context_msg->tx;
     724           0 :   dump_sanitized_transaction( txn_ctx->funk, txn_ctx->xid, txn_descriptor, txn_payload, spad, sanitized_transaction );
     725             : 
     726             :   /* Transaction Context -> blockhash_queue
     727             :      NOTE: Agave's implementation of register_hash incorrectly allows the blockhash queue to hold max_age + 1 (max 301)
     728             :      entries. We have this incorrect logic implemented in fd_sysvar_recent_hashes:register_blockhash and it's not a
     729             :      huge issue, but something to keep in mind. */
     730           0 :   pb_bytes_array_t ** output_blockhash_queue = fd_spad_alloc(
     731           0 :                                                       spad,
     732           0 :                                                       alignof(pb_bytes_array_t *),
     733           0 :                                                       PB_BYTES_ARRAY_T_ALLOCSIZE((FD_BLOCKHASHES_MAX) * sizeof(pb_bytes_array_t *)) );
     734           0 :   txn_context_msg->blockhash_queue = output_blockhash_queue;
     735           0 :   fd_blockhashes_t const * block_hash_queue = fd_bank_block_hash_queue_query( txn_ctx->bank );
     736           0 :   dump_blockhash_queue( block_hash_queue, spad, output_blockhash_queue, &txn_context_msg->blockhash_queue_count );
     737             : 
     738             :   /* Transaction Context -> epoch_ctx */
     739           0 :   txn_context_msg->has_epoch_ctx = true;
     740           0 :   txn_context_msg->epoch_ctx.has_features = true;
     741           0 :   dump_sorted_features( &txn_ctx->features, &txn_context_msg->epoch_ctx.features, spad );
     742             : 
     743             :   /* Transaction Context -> slot_ctx */
     744           0 :   txn_context_msg->has_slot_ctx  = true;
     745           0 :   txn_context_msg->slot_ctx.slot = txn_ctx->slot;
     746           0 : }
     747             : 
     748             : static void
     749             : create_instr_context_protobuf_from_instructions( fd_exec_test_instr_context_t * instr_context,
     750             :                                                  fd_exec_txn_ctx_t const *      txn_ctx,
     751           0 :                                                  fd_instr_info_t const *        instr ) {
     752             :   /* Prepare sysvar cache accounts */
     753           0 :   fd_pubkey_t const fd_relevant_sysvar_ids[] = {
     754           0 :     fd_sysvar_recent_block_hashes_id,
     755           0 :     fd_sysvar_clock_id,
     756           0 :     fd_sysvar_slot_history_id,
     757           0 :     fd_sysvar_slot_hashes_id,
     758           0 :     fd_sysvar_epoch_schedule_id,
     759           0 :     fd_sysvar_epoch_rewards_id,
     760           0 :     fd_sysvar_fees_id,
     761           0 :     fd_sysvar_rent_id,
     762           0 :     fd_sysvar_stake_history_id,
     763           0 :     fd_sysvar_last_restart_slot_id,
     764           0 :     fd_sysvar_instructions_id,
     765           0 :   };
     766           0 :   const ulong num_sysvar_entries = (sizeof(fd_relevant_sysvar_ids) / sizeof(fd_pubkey_t));
     767             : 
     768             :   /* Program ID */
     769           0 :   fd_memcpy( instr_context->program_id, txn_ctx->account_keys[ instr->program_id ].uc, sizeof(fd_pubkey_t) );
     770             : 
     771             :   /* Accounts */
     772           0 :   instr_context->accounts_count = (pb_size_t) txn_ctx->accounts_cnt;
     773           0 :   instr_context->accounts = fd_spad_alloc( txn_ctx->spad, alignof(fd_exec_test_acct_state_t), (instr_context->accounts_count + num_sysvar_entries + txn_ctx->executable_cnt) * sizeof(fd_exec_test_acct_state_t));
     774           0 :   for( ulong i = 0; i < txn_ctx->accounts_cnt; i++ ) {
     775             :     // Copy account information over
     776           0 :     fd_txn_account_t const *    txn_account    = &txn_ctx->accounts[i];
     777           0 :     fd_exec_test_acct_state_t * output_account = &instr_context->accounts[i];
     778           0 :     dump_account_state( txn_account, output_account, txn_ctx->spad );
     779           0 :   }
     780             : 
     781             :   /* Add sysvar cache variables */
     782           0 :   for( ulong i = 0; i < num_sysvar_entries; i++ ) {
     783           0 :     FD_TXN_ACCOUNT_DECL( txn_account );
     784           0 :     int ret = fd_txn_account_init_from_funk_readonly( txn_account, &fd_relevant_sysvar_ids[i], txn_ctx->funk, txn_ctx->xid );
     785           0 :     if( ret != FD_ACC_MGR_SUCCESS ) {
     786           0 :       continue;
     787           0 :     }
     788             :     // Make sure the account doesn't exist in the output accounts yet
     789           0 :     int account_exists = 0;
     790           0 :     for( ulong j = 0; j < txn_ctx->accounts_cnt; j++ ) {
     791           0 :       if ( 0 == memcmp( txn_ctx->account_keys[j].key, fd_relevant_sysvar_ids[i].uc, sizeof(fd_pubkey_t) ) ) {
     792           0 :         account_exists = true;
     793           0 :         break;
     794           0 :       }
     795           0 :     }
     796             : 
     797             :     // Copy it into output
     798           0 :     if (!account_exists) {
     799           0 :       fd_exec_test_acct_state_t * output_account = &instr_context->accounts[instr_context->accounts_count++];
     800           0 :       dump_account_state( txn_account, output_account, txn_ctx->spad );
     801           0 :     }
     802           0 :   }
     803             : 
     804             :   /* Add executable accounts */
     805           0 :   for( ulong i = 0; i < txn_ctx->executable_cnt; i++ ) {
     806           0 :     FD_TXN_ACCOUNT_DECL( txn_account );
     807           0 :     int ret = fd_txn_account_init_from_funk_readonly( txn_account, txn_ctx->executable_accounts[i].pubkey, txn_ctx->funk, txn_ctx->xid );
     808           0 :     if( ret != FD_ACC_MGR_SUCCESS ) {
     809           0 :       continue;
     810           0 :     }
     811             :     // Make sure the account doesn't exist in the output accounts yet
     812           0 :     bool account_exists = false;
     813           0 :     for( ulong j = 0; j < instr_context->accounts_count; j++ ) {
     814           0 :       if( 0 == memcmp( instr_context->accounts[j].address, txn_ctx->executable_accounts[i].pubkey->uc, sizeof(fd_pubkey_t) ) ) {
     815           0 :         account_exists = true;
     816           0 :         break;
     817           0 :       }
     818           0 :     }
     819             :     // Copy it into output
     820           0 :     if( !account_exists ) {
     821           0 :       fd_exec_test_acct_state_t * output_account = &instr_context->accounts[instr_context->accounts_count++];
     822           0 :       dump_account_state( txn_account, output_account, txn_ctx->spad );
     823           0 :     }
     824           0 :   }
     825             : 
     826             :   /* Instruction Accounts */
     827           0 :   instr_context->instr_accounts_count = (pb_size_t) instr->acct_cnt;
     828           0 :   instr_context->instr_accounts = fd_spad_alloc( txn_ctx->spad, alignof(fd_exec_test_instr_acct_t), instr_context->instr_accounts_count * sizeof(fd_exec_test_instr_acct_t) );
     829           0 :   for( ushort i = 0; i < instr->acct_cnt; i++ ) {
     830           0 :     fd_exec_test_instr_acct_t * output_instr_account = &instr_context->instr_accounts[i];
     831             : 
     832           0 :     output_instr_account->index       = instr->accounts[i].index_in_transaction;
     833           0 :     output_instr_account->is_writable = instr->accounts[i].is_writable;
     834           0 :     output_instr_account->is_signer   = instr->accounts[i].is_signer;
     835           0 :   }
     836             : 
     837             :   /* Data */
     838           0 :   instr_context->data = fd_spad_alloc( txn_ctx->spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( instr->data_sz ) );
     839           0 :   instr_context->data->size = (pb_size_t) instr->data_sz;
     840           0 :   fd_memcpy( instr_context->data->bytes, instr->data, instr->data_sz );
     841             : 
     842             :   /* Compute Units */
     843           0 :   instr_context->cu_avail = txn_ctx->compute_budget_details.compute_meter;
     844             : 
     845             :   /* Slot Context */
     846           0 :   instr_context->has_slot_context = true;
     847             : 
     848             :   /* Epoch Context */
     849           0 :   instr_context->has_epoch_context = true;
     850           0 :   instr_context->epoch_context.has_features = true;
     851           0 :   dump_sorted_features( &txn_ctx->features, &instr_context->epoch_context.features, txn_ctx->spad );
     852           0 : }
     853             : 
     854             : /***** PUBLIC APIs *****/
     855             : 
     856             : void
     857             : fd_dump_instr_to_protobuf( fd_exec_txn_ctx_t * txn_ctx,
     858             :                            fd_instr_info_t *   instr,
     859           0 :                            ushort              instruction_idx ) {
     860           0 :   FD_SPAD_FRAME_BEGIN( txn_ctx->spad ) {
     861             :     // Get base58-encoded tx signature
     862           0 :     const fd_ed25519_sig_t * signatures = fd_txn_get_signatures( TXN( &txn_ctx->txn ), txn_ctx->txn.payload );
     863           0 :     fd_ed25519_sig_t signature; fd_memcpy( signature, signatures[0], sizeof(fd_ed25519_sig_t) );
     864           0 :     char encoded_signature[FD_BASE58_ENCODED_64_SZ];
     865           0 :     ulong out_size;
     866           0 :     fd_base58_encode_64( signature, &out_size, encoded_signature );
     867             : 
     868           0 :     if (txn_ctx->capture_ctx->dump_proto_sig_filter) {
     869           0 :       ulong filter_strlen = (ulong) strlen(txn_ctx->capture_ctx->dump_proto_sig_filter);
     870             : 
     871             :       // Terminate early if the signature does not match
     872           0 :       if( memcmp( txn_ctx->capture_ctx->dump_proto_sig_filter, encoded_signature, filter_strlen < out_size ? filter_strlen : out_size ) ) {
     873           0 :         return;
     874           0 :       }
     875           0 :     }
     876             : 
     877           0 :     fd_exec_test_instr_context_t instr_context = FD_EXEC_TEST_INSTR_CONTEXT_INIT_DEFAULT;
     878           0 :     create_instr_context_protobuf_from_instructions( &instr_context, txn_ctx, instr );
     879             : 
     880             :     /* Output to file */
     881           0 :     ulong out_buf_size = 100 * 1024 * 1024;
     882           0 :     uint8_t * out = fd_spad_alloc( txn_ctx->spad, alignof(uchar) , out_buf_size );
     883           0 :     pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
     884           0 :     if (pb_encode(&stream, FD_EXEC_TEST_INSTR_CONTEXT_FIELDS, &instr_context)) {
     885           0 :       char output_filepath[256]; fd_memset(output_filepath, 0, sizeof(output_filepath));
     886           0 :       char * position = fd_cstr_init(output_filepath);
     887           0 :       position = fd_cstr_append_cstr(position, txn_ctx->capture_ctx->dump_proto_output_dir);
     888           0 :       position = fd_cstr_append_cstr(position, "/instr-");
     889           0 :       position = fd_cstr_append_cstr(position, encoded_signature);
     890           0 :       position = fd_cstr_append_cstr(position, "-");
     891           0 :       position = fd_cstr_append_ushort_as_text(position, '0', 0, instruction_idx, 3); // Assume max 3 digits
     892           0 :       position = fd_cstr_append_cstr(position, ".instrctx");
     893           0 :       fd_cstr_fini(position);
     894             : 
     895           0 :       FILE * file = fopen(output_filepath, "wb");
     896           0 :       if( file ) {
     897           0 :         fwrite( out, 1, stream.bytes_written, file );
     898           0 :         fclose( file );
     899           0 :       }
     900           0 :     }
     901           0 :   } FD_SPAD_FRAME_END;
     902           0 : }
     903             : 
     904             : void
     905           0 : fd_dump_txn_to_protobuf( fd_exec_txn_ctx_t * txn_ctx, fd_spad_t * spad ) {
     906           0 :   FD_SPAD_FRAME_BEGIN( spad ) {
     907             :     // Get base58-encoded tx signature
     908           0 :     const fd_ed25519_sig_t * signatures = fd_txn_get_signatures( TXN( &txn_ctx->txn ), txn_ctx->txn.payload );
     909           0 :     fd_ed25519_sig_t signature; fd_memcpy( signature, signatures[0], sizeof(fd_ed25519_sig_t) );
     910           0 :     char encoded_signature[FD_BASE58_ENCODED_64_SZ];
     911           0 :     ulong out_size;
     912           0 :     fd_base58_encode_64( signature, &out_size, encoded_signature );
     913             : 
     914           0 :     if( txn_ctx->capture_ctx->dump_proto_sig_filter ) {
     915             :       // Terminate early if the signature does not match
     916           0 :       if( strcmp( txn_ctx->capture_ctx->dump_proto_sig_filter, encoded_signature ) ) {
     917           0 :         return;
     918           0 :       }
     919           0 :     }
     920             : 
     921           0 :     fd_exec_test_txn_context_t txn_context_msg = FD_EXEC_TEST_TXN_CONTEXT_INIT_DEFAULT;
     922           0 :     create_txn_context_protobuf_from_txn( &txn_context_msg, txn_ctx, spad );
     923             : 
     924             :     /* Output to file */
     925           0 :     ulong out_buf_size = 100 * 1024 * 1024;
     926           0 :     uint8_t * out = fd_spad_alloc( spad, alignof(uint8_t), out_buf_size );
     927           0 :     pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
     928           0 :     if( pb_encode( &stream, FD_EXEC_TEST_TXN_CONTEXT_FIELDS, &txn_context_msg ) ) {
     929           0 :       char output_filepath[256]; fd_memset( output_filepath, 0, sizeof(output_filepath) );
     930           0 :       char * position = fd_cstr_init( output_filepath );
     931           0 :       position = fd_cstr_append_cstr( position, txn_ctx->capture_ctx->dump_proto_output_dir );
     932           0 :       position = fd_cstr_append_cstr( position, "/txn-" );
     933           0 :       position = fd_cstr_append_cstr( position, encoded_signature );
     934           0 :       position = fd_cstr_append_cstr(position, ".txnctx");
     935           0 :       fd_cstr_fini(position);
     936             : 
     937           0 :       FILE * file = fopen(output_filepath, "wb");
     938           0 :       if( file ) {
     939           0 :         fwrite( out, 1, stream.bytes_written, file );
     940           0 :         fclose( file );
     941           0 :       }
     942           0 :     }
     943           0 :   } FD_SPAD_FRAME_END;
     944           0 : }
     945             : 
     946             : void
     947             : fd_dump_block_to_protobuf( fd_banks_t *                   banks,
     948             :                            fd_bank_t *                    bank,
     949             :                            fd_funk_t *                    funk,
     950             :                            fd_funk_txn_xid_t const *      xid,
     951             :                            fd_capture_ctx_t const *       capture_ctx,
     952             :                            fd_spad_t *                    spad,
     953           0 :                            fd_exec_test_block_context_t * block_context_msg /* output */ ) {
     954             :   /* No spad frame because these allocations must persist beyond the lifetime of this function call */
     955           0 :   if( FD_UNLIKELY( capture_ctx==NULL ) ) {
     956           0 :     FD_LOG_WARNING(( "Capture context may not be NULL when dumping blocks." ));
     957           0 :     return;
     958           0 :   }
     959           0 :   create_block_context_protobuf_from_block( block_context_msg, banks, bank, funk, xid, spad );
     960           0 : }
     961             : 
     962             : void
     963             : fd_dump_block_to_protobuf_tx_only( fd_runtime_block_info_t const * block_info,
     964             :                                    fd_bank_t *                     bank,
     965             :                                    fd_funk_t *                     funk,
     966             :                                    fd_funk_txn_xid_t const *       xid,
     967             :                                    fd_capture_ctx_t const *        capture_ctx,
     968             :                                    fd_spad_t *                     spad,
     969           0 :                                    fd_exec_test_block_context_t *  block_context_msg ) {
     970           0 :   FD_SPAD_FRAME_BEGIN( spad ) {
     971           0 :     if( FD_UNLIKELY( capture_ctx==NULL ) ) {
     972           0 :       FD_LOG_WARNING(( "Capture context may not be NULL when dumping blocks." ));
     973           0 :       return;
     974           0 :     }
     975             : 
     976           0 :     if( FD_UNLIKELY( block_info==NULL ) ) {
     977           0 :       FD_LOG_WARNING(( "Block info may not be NULL when dumping blocks." ));
     978           0 :       return;
     979           0 :     }
     980             : 
     981           0 :     create_block_context_protobuf_from_block_tx_only( block_context_msg, block_info, bank, funk, xid, spad );
     982             : 
     983             :     /* Output to file */
     984           0 :     ulong out_buf_size = 5UL<<30UL; /* 5 GB */
     985           0 :     uint8_t * out = fd_spad_alloc( spad, alignof(uint8_t), out_buf_size );
     986           0 :     pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
     987           0 :     if( pb_encode( &stream, FD_EXEC_TEST_BLOCK_CONTEXT_FIELDS, block_context_msg ) ) {
     988           0 :       char output_filepath[256]; fd_memset( output_filepath, 0, sizeof(output_filepath) );
     989           0 :       char * position = fd_cstr_init( output_filepath );
     990           0 :       position = fd_cstr_append_printf( position, "%s/block-%lu.blockctx", capture_ctx->dump_proto_output_dir, fd_bank_slot_get( bank ) );
     991           0 :       fd_cstr_fini( position );
     992             : 
     993           0 :       FILE * file = fopen(output_filepath, "wb");
     994           0 :       if( file ) {
     995           0 :         fwrite( out, 1, stream.bytes_written, file );
     996           0 :         fclose( file );
     997           0 :       }
     998           0 :     }
     999           0 :   } FD_SPAD_FRAME_END;
    1000           0 : }
    1001             : 
    1002             : void
    1003             : fd_dump_vm_syscall_to_protobuf( fd_vm_t const * vm,
    1004           0 :                                 char const *    fn_name ) {
    1005           0 : FD_SPAD_FRAME_BEGIN( vm->instr_ctx->txn_ctx->spad ) {
    1006             : 
    1007           0 :   fd_ed25519_sig_t signature;
    1008           0 :   memcpy( signature, (uchar const *)vm->instr_ctx->txn_ctx->txn.payload + TXN( &vm->instr_ctx->txn_ctx->txn )->signature_off, sizeof(fd_ed25519_sig_t) );
    1009           0 :   char encoded_signature[FD_BASE58_ENCODED_64_SZ];
    1010           0 :   fd_base58_encode_64( signature, NULL, encoded_signature );
    1011             : 
    1012           0 :   char filename[256];
    1013           0 :   sprintf( filename,
    1014           0 :           "%s/syscall-%s-%s-%d-%hhu-%lu.sysctx",
    1015           0 :           vm->instr_ctx->txn_ctx->capture_ctx->dump_proto_output_dir,
    1016           0 :           fn_name,
    1017           0 :           encoded_signature,
    1018           0 :           vm->instr_ctx->txn_ctx->current_instr_idx,
    1019           0 :           vm->instr_ctx->txn_ctx->instr_stack_sz,
    1020           0 :           vm->cu );
    1021             : 
    1022             :   /* The generated filename should be unique for every call. Silently return otherwise. */
    1023           0 :   if( FD_UNLIKELY( access( filename, F_OK )!=-1 ) ) {
    1024           0 :     return;
    1025           0 :   }
    1026             : 
    1027           0 :   fd_exec_test_syscall_context_t sys_ctx = FD_EXEC_TEST_SYSCALL_CONTEXT_INIT_ZERO;
    1028             : 
    1029             :   /* SyscallContext -> vm_ctx */
    1030           0 :   sys_ctx.has_vm_ctx = 1;
    1031             : 
    1032             :   /* SyscallContext -> vm_ctx -> heap_max */
    1033           0 :   sys_ctx.vm_ctx.heap_max = vm->heap_max; /* should be equiv. to txn_ctx->heap_sz */
    1034             : 
    1035             :   /* SyscallContext -> vm_ctx -> rodata */
    1036           0 :   sys_ctx.vm_ctx.rodata = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->rodata_sz ) );
    1037           0 :   sys_ctx.vm_ctx.rodata->size = (pb_size_t) vm->rodata_sz;
    1038           0 :   fd_memcpy( sys_ctx.vm_ctx.rodata->bytes, vm->rodata, vm->rodata_sz );
    1039             : 
    1040             :   /* SyscallContext -> vm_ctx -> rodata_text_section_offset */
    1041           0 :   sys_ctx.vm_ctx.rodata_text_section_offset = vm->text_off;
    1042             : 
    1043             :   /* SyscallContext -> vm_ctx -> rodata_text_section_length */
    1044           0 :   sys_ctx.vm_ctx.rodata_text_section_length = vm->text_sz;
    1045             : 
    1046             :   /* SyscallContext -> vm_ctx -> r0-11 */
    1047           0 :   sys_ctx.vm_ctx.r0  = vm->reg[0];
    1048           0 :   sys_ctx.vm_ctx.r1  = vm->reg[1];
    1049           0 :   sys_ctx.vm_ctx.r2  = vm->reg[2];
    1050           0 :   sys_ctx.vm_ctx.r3  = vm->reg[3];
    1051           0 :   sys_ctx.vm_ctx.r4  = vm->reg[4];
    1052           0 :   sys_ctx.vm_ctx.r5  = vm->reg[5];
    1053           0 :   sys_ctx.vm_ctx.r6  = vm->reg[6];
    1054           0 :   sys_ctx.vm_ctx.r7  = vm->reg[7];
    1055           0 :   sys_ctx.vm_ctx.r8  = vm->reg[8];
    1056           0 :   sys_ctx.vm_ctx.r9  = vm->reg[9];
    1057           0 :   sys_ctx.vm_ctx.r10 = vm->reg[10];
    1058           0 :   sys_ctx.vm_ctx.r11 = vm->reg[11];
    1059             : 
    1060             :   /* SyscallContext -> vm_ctx -> entry_pc */
    1061           0 :   sys_ctx.vm_ctx.entry_pc = vm->entry_pc;
    1062             : 
    1063             :   /* SyscallContext -> vm_ctx -> return_data */
    1064           0 :   sys_ctx.vm_ctx.has_return_data = 1;
    1065             : 
    1066             :   /* SyscallContext -> vm_ctx -> return_data -> data */
    1067           0 :   sys_ctx.vm_ctx.return_data.data = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->instr_ctx->txn_ctx->return_data.len ) );
    1068           0 :   sys_ctx.vm_ctx.return_data.data->size = (pb_size_t)vm->instr_ctx->txn_ctx->return_data.len;
    1069           0 :   fd_memcpy( sys_ctx.vm_ctx.return_data.data->bytes, vm->instr_ctx->txn_ctx->return_data.data, vm->instr_ctx->txn_ctx->return_data.len );
    1070             : 
    1071             :   /* SyscallContext -> vm_ctx -> return_data -> program_id */
    1072           0 :   sys_ctx.vm_ctx.return_data.program_id = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, alignof(pb_bytes_array_t), sizeof(fd_pubkey_t) );
    1073           0 :   sys_ctx.vm_ctx.return_data.program_id->size = sizeof(fd_pubkey_t);
    1074           0 :   fd_memcpy( sys_ctx.vm_ctx.return_data.program_id->bytes, vm->instr_ctx->txn_ctx->return_data.program_id.key, sizeof(fd_pubkey_t) );
    1075             : 
    1076             :   /* SyscallContext -> vm_ctx -> sbpf_version */
    1077           0 :   sys_ctx.vm_ctx.sbpf_version = (uint)vm->sbpf_version;
    1078             : 
    1079             :   /* SyscallContext -> instr_ctx */
    1080           0 :   sys_ctx.has_instr_ctx = 1;
    1081           0 :   create_instr_context_protobuf_from_instructions( &sys_ctx.instr_ctx,
    1082           0 :                                                     vm->instr_ctx->txn_ctx,
    1083           0 :                                                     vm->instr_ctx->instr );
    1084             : 
    1085             :   /* SyscallContext -> syscall_invocation */
    1086           0 :   sys_ctx.has_syscall_invocation = 1;
    1087             : 
    1088             :   /* SyscallContext -> syscall_invocation -> function_name */
    1089           0 :   sys_ctx.syscall_invocation.function_name.size = fd_uint_min( (uint) strlen(fn_name), sizeof(sys_ctx.syscall_invocation.function_name.bytes) );
    1090           0 :   fd_memcpy( sys_ctx.syscall_invocation.function_name.bytes,
    1091           0 :              fn_name,
    1092           0 :              sys_ctx.syscall_invocation.function_name.size );
    1093             : 
    1094             :   /* SyscallContext -> syscall_invocation -> heap_prefix */
    1095           0 :   sys_ctx.syscall_invocation.heap_prefix = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, 8UL, PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
    1096           0 :   sys_ctx.syscall_invocation.heap_prefix->size = (pb_size_t) vm->instr_ctx->txn_ctx->compute_budget_details.heap_size;
    1097           0 :   fd_memcpy( sys_ctx.syscall_invocation.heap_prefix->bytes, vm->heap, vm->instr_ctx->txn_ctx->compute_budget_details.heap_size );
    1098             : 
    1099             :   /* SyscallContext -> syscall_invocation -> stack_prefix */
    1100           0 :   pb_size_t stack_sz = (pb_size_t)FD_VM_STACK_MAX;
    1101           0 :   sys_ctx.syscall_invocation.stack_prefix = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, 8UL, PB_BYTES_ARRAY_T_ALLOCSIZE( stack_sz ) );
    1102           0 :   sys_ctx.syscall_invocation.stack_prefix->size = stack_sz;
    1103           0 :   fd_memcpy( sys_ctx.syscall_invocation.stack_prefix->bytes, vm->stack, stack_sz );
    1104             : 
    1105             :   /* Output to file */
    1106           0 :   ulong out_buf_size = 1UL<<29UL; /* 128 MB */
    1107           0 :   uint8_t * out = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, alignof(uint8_t), out_buf_size );
    1108           0 :   pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
    1109           0 :   if( pb_encode( &stream, FD_EXEC_TEST_SYSCALL_CONTEXT_FIELDS, &sys_ctx ) ) {
    1110           0 :     FILE * file = fopen(filename, "wb");
    1111           0 :     if( file ) {
    1112           0 :       fwrite( out, 1, stream.bytes_written, file );
    1113           0 :       fclose( file );
    1114           0 :     }
    1115           0 :   }
    1116           0 : } FD_SPAD_FRAME_END;
    1117           0 : }
    1118             : 
    1119             : void
    1120             : fd_dump_elf_to_protobuf( fd_exec_txn_ctx_t * txn_ctx,
    1121           0 :                          fd_txn_account_t *  program_acc ) {
    1122           0 : FD_SPAD_FRAME_BEGIN( txn_ctx->spad ) {
    1123             : 
    1124             :   /* Get the programdata for the account */
    1125           0 :   ulong         program_data_len = 0UL;
    1126           0 :   uchar const * program_data     = fd_program_cache_get_account_programdata( txn_ctx->funk,
    1127           0 :                                                                              txn_ctx->xid,
    1128           0 :                                                                              program_acc,
    1129           0 :                                                                              &program_data_len );
    1130           0 :   if( program_data==NULL ) {
    1131           0 :     return;
    1132           0 :   }
    1133             : 
    1134             :   /* Serialize the ELF to protobuf */
    1135           0 :   fd_ed25519_sig_t signature;
    1136           0 :   memcpy( signature, (uchar const *)txn_ctx->txn.payload + TXN( &txn_ctx->txn )->signature_off, sizeof(fd_ed25519_sig_t) );
    1137           0 :   char encoded_signature[FD_BASE58_ENCODED_64_SZ];
    1138           0 :   fd_base58_encode_64( signature, NULL, encoded_signature );
    1139             : 
    1140           0 :   char filename[256];
    1141           0 :   sprintf( filename,
    1142           0 :           "%s/elf-%s-%s-%lu.elfctx",
    1143           0 :           txn_ctx->capture_ctx->dump_proto_output_dir,
    1144           0 :           encoded_signature,
    1145           0 :           FD_BASE58_ENC_32_ALLOCA( program_acc->pubkey ),
    1146           0 :           txn_ctx->slot );
    1147             : 
    1148             :   /* The generated filename should be unique for every call. Silently return otherwise. */
    1149           0 :   if( FD_UNLIKELY( access( filename, F_OK )!=-1 ) ) {
    1150           0 :     return;
    1151           0 :   }
    1152             : 
    1153           0 :   fd_exec_test_elf_loader_ctx_t elf_ctx = FD_EXEC_TEST_ELF_LOADER_CTX_INIT_ZERO;
    1154             : 
    1155             :   /* ElfLoaderCtx -> elf */
    1156           0 :   elf_ctx.has_elf = true;
    1157           0 :   elf_ctx.elf.data = fd_spad_alloc( txn_ctx->spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( program_data_len ) );
    1158           0 :   elf_ctx.elf.data->size = (pb_size_t)program_data_len;
    1159           0 :   fd_memcpy( elf_ctx.elf.data->bytes, program_data, program_data_len );
    1160             : 
    1161             :   /* ElfLoaderCtx -> features */
    1162           0 :   elf_ctx.has_features = true;
    1163           0 :   dump_sorted_features( &txn_ctx->features, &elf_ctx.features, txn_ctx->spad );
    1164             : 
    1165             :   /* ElfLoaderCtx -> deploy_checks
    1166             :      We hardcode this to true and rely the fuzzer to toggle this as it pleases */
    1167           0 :   elf_ctx.deploy_checks = true;
    1168             : 
    1169             :   /* Output to file */
    1170           0 :   ulong out_buf_size = 1UL<<29UL; /* 128 MB */
    1171           0 :   uint8_t * out = fd_spad_alloc( txn_ctx->spad, alignof(uint8_t), out_buf_size );
    1172           0 :   pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
    1173           0 :   if( pb_encode( &stream, FD_EXEC_TEST_ELF_LOADER_CTX_FIELDS, &elf_ctx ) ) {
    1174           0 :     FILE * file = fopen(filename, "wb");
    1175           0 :     if( file ) {
    1176           0 :       fwrite( out, 1, stream.bytes_written, file );
    1177           0 :       fclose( file );
    1178           0 :     }
    1179           0 :   }
    1180           0 : } FD_SPAD_FRAME_END;
    1181           0 : }

Generated by: LCOV version 1.14