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

Generated by: LCOV version 1.14