Line data Source code
1 : #include "fd_dump_pb.h"
2 : #include "generated/block.pb.h"
3 : #include "generated/instr.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 "../fd_runtime.h"
9 : #include "../fd_alut.h"
10 : #include "../program/fd_precompiles.h"
11 : #include "../../../ballet/nanopb/pb_encode.h"
12 : #include "../../accdb/fd_accdb_sync.h"
13 : #include "../fd_runtime_stack.h"
14 :
15 : #include <stdio.h> /* fopen */
16 : #include <sys/mman.h> /* mmap */
17 : #include <unistd.h> /* ftruncate */
18 :
19 :
20 : struct fd_dump_account_key {
21 : fd_pubkey_t key;
22 : };
23 : typedef struct fd_dump_account_key fd_dump_account_t;
24 :
25 0 : #define FD_DUMP_ACCOUNT_KEY_MAP_LG_SLOT_CNT (20)
26 :
27 : #define MAP_NAME fd_dump_account_key_map
28 0 : #define MAP_T fd_dump_account_t
29 0 : #define MAP_LG_SLOT_CNT FD_DUMP_ACCOUNT_KEY_MAP_LG_SLOT_CNT
30 0 : #define MAP_KEY_T fd_pubkey_t
31 0 : #define MAP_KEY_NULL fd_solana_system_program_id
32 0 : #define MAP_KEY_INVAL(k) fd_pubkey_eq( &(k), &fd_solana_system_program_id )
33 0 : #define MAP_KEY_EQUAL(k0,k1) fd_pubkey_eq( &(k0), &(k1) )
34 : #define MAP_KEY_EQUAL_IS_SLOW (0)
35 : #define MAP_MEMOIZE (0)
36 0 : #define MAP_KEY_HASH(key) ((uint)fd_hash( 0UL, (key).uc, sizeof(fd_pubkey_t) ))
37 : #include "../../../util/tmpl/fd_map.c"
38 :
39 : struct fd_dump_account_key_set {
40 : fd_dump_account_t * map;
41 : ulong cnt;
42 : uchar system_program_dumped;
43 : };
44 : typedef struct fd_dump_account_key_set fd_dump_account_key_set_t;
45 :
46 : struct fd_dump_account_key_iter {
47 : fd_dump_account_key_set_t const * set;
48 : ulong slot_idx;
49 : uchar system_program;
50 : };
51 : typedef struct fd_dump_account_key_iter fd_dump_account_key_iter_t;
52 :
53 : static void
54 0 : fd_dump_account_key_iter_advance_map( fd_dump_account_key_iter_t * iter ) {
55 0 : while( iter->slot_idx<fd_dump_account_key_map_slot_cnt() &&
56 0 : fd_dump_account_key_map_key_inval( iter->set->map[iter->slot_idx].key ) ) {
57 0 : iter->slot_idx++;
58 0 : }
59 0 : }
60 :
61 : static fd_dump_account_key_iter_t *
62 : fd_dump_account_key_iter_init( fd_dump_account_key_iter_t * iter,
63 0 : fd_dump_account_key_set_t const * set ) {
64 0 : iter->set = set;
65 0 : iter->slot_idx = 0UL;
66 0 : iter->system_program = set->system_program_dumped;
67 0 : if( !iter->system_program ) fd_dump_account_key_iter_advance_map( iter );
68 0 : return iter;
69 0 : }
70 :
71 : static int
72 0 : fd_dump_account_key_iter_done( fd_dump_account_key_iter_t const * iter ) {
73 0 : return (!iter->system_program) & (iter->slot_idx>=fd_dump_account_key_map_slot_cnt());
74 0 : }
75 :
76 : static fd_pubkey_t const *
77 0 : fd_dump_account_key_iter_ele( fd_dump_account_key_iter_t const * iter ) {
78 0 : return iter->system_program ? &fd_solana_system_program_id : &iter->set->map[iter->slot_idx].key;
79 0 : }
80 :
81 : static void
82 0 : fd_dump_account_key_iter_next( fd_dump_account_key_iter_t * iter ) {
83 0 : if( iter->system_program ) {
84 0 : iter->system_program = 0;
85 0 : } else {
86 0 : iter->slot_idx++;
87 0 : }
88 0 : fd_dump_account_key_iter_advance_map( iter );
89 0 : }
90 :
91 : /***** CONSTANTS *****/
92 : static fd_pubkey_t const * fd_dump_sysvar_ids[] = {
93 : &fd_sysvar_recent_block_hashes_id,
94 : &fd_sysvar_clock_id,
95 : &fd_sysvar_slot_history_id,
96 : &fd_sysvar_slot_hashes_id,
97 : &fd_sysvar_epoch_schedule_id,
98 : &fd_sysvar_epoch_rewards_id,
99 : &fd_sysvar_fees_id,
100 : &fd_sysvar_rent_id,
101 : &fd_sysvar_stake_history_id,
102 : &fd_sysvar_last_restart_slot_id,
103 : &fd_sysvar_instructions_id,
104 : };
105 : static ulong const num_sysvar_entries = (sizeof(fd_dump_sysvar_ids) / sizeof(fd_pubkey_t *));
106 :
107 : static fd_pubkey_t const * fd_dump_builtin_ids[] = {
108 : &fd_solana_system_program_id,
109 : &fd_solana_vote_program_id,
110 : &fd_solana_stake_program_id,
111 : &fd_solana_bpf_loader_v4_program_id,
112 : &fd_solana_bpf_loader_deprecated_program_id,
113 : &fd_solana_bpf_loader_program_id,
114 : &fd_solana_bpf_loader_upgradeable_program_id,
115 : &fd_solana_compute_budget_program_id,
116 : &fd_solana_keccak_secp_256k_program_id,
117 : &fd_solana_secp256r1_program_id,
118 : &fd_solana_zk_elgamal_proof_program_id,
119 : &fd_solana_ed25519_sig_verify_program_id,
120 : };
121 : static ulong const num_loaded_builtins = (sizeof(fd_dump_builtin_ids) / sizeof(fd_pubkey_t *));
122 :
123 : /***** UTILITY FUNCTIONS *****/
124 :
125 : /** FEATURE DUMPING **/
126 : static void
127 : dump_sorted_features( fd_features_t const * features,
128 : fd_exec_test_feature_set_t * output_feature_set,
129 0 : fd_spad_t * spad ) {
130 : /* NOTE: Caller must have a spad frame prepared */
131 0 : uint64_t * unsorted_features = fd_spad_alloc( spad, alignof(uint64_t), FD_FEATURE_ID_CNT * sizeof(uint64_t) );
132 0 : ulong num_features = 0;
133 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 ) ) {
134 0 : if (features->f[current_feature->index] != FD_FEATURE_DISABLED) {
135 0 : unsorted_features[num_features++] = (uint64_t) current_feature->id.ul[0];
136 0 : }
137 0 : }
138 :
139 : // Set feature set in message
140 0 : output_feature_set->features_count = (pb_size_t)num_features;
141 0 : output_feature_set->features = unsorted_features;
142 0 : }
143 :
144 : /** ACCOUNT DUMPING **/
145 : static void
146 : dump_account_state( fd_pubkey_t const * account_key,
147 : fd_account_meta_t const * account_meta,
148 : fd_exec_test_acct_state_t * output_account,
149 0 : fd_spad_t * spad ) {
150 : // Address
151 0 : fd_memcpy(output_account->address, account_key, sizeof(fd_pubkey_t));
152 :
153 : // Lamports
154 0 : output_account->lamports = (uint64_t)account_meta->lamports;
155 :
156 : // Data
157 0 : output_account->data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( account_meta->dlen ) );
158 0 : output_account->data->size = (pb_size_t) account_meta->dlen;
159 0 : fd_memcpy(output_account->data->bytes, fd_account_data( account_meta ), account_meta->dlen );
160 :
161 : // Executable
162 0 : output_account->executable = (bool)account_meta->executable;
163 :
164 : // Owner
165 0 : fd_memcpy(output_account->owner, account_meta->owner, sizeof(fd_pubkey_t));
166 0 : }
167 :
168 : static uchar
169 : account_already_dumped( fd_exec_test_acct_state_t const * dumped_accounts,
170 : ulong dumped_cnt,
171 0 : fd_pubkey_t const * account_key ) {
172 0 : for( ulong i=0UL; i<dumped_cnt; i++ ) {
173 0 : if( !memcmp( account_key, dumped_accounts[i].address, sizeof(fd_pubkey_t) ) ) {
174 0 : return 1;
175 0 : }
176 0 : }
177 0 : return 0;
178 0 : }
179 :
180 : /* Dumps a borrowed account if it exists and has not been dumped yet.
181 : Sets up the output borrowed account if it exists. Returns 0 if the
182 : account exists, 1 otherwise.
183 : TODO: This can be optimized by using a set. */
184 : static uchar
185 : dump_account_if_not_already_dumped( fd_accdb_user_t * accdb,
186 : fd_funk_txn_xid_t const * xid,
187 : fd_pubkey_t const * account_key,
188 : fd_spad_t * spad,
189 : fd_exec_test_acct_state_t * out_acct_states,
190 : pb_size_t * out_acct_states_cnt,
191 0 : fd_accdb_ro_t * out_ro ) {
192 0 : fd_accdb_ro_t ro[1];
193 0 : if( !fd_accdb_open_ro( accdb, ro, xid, account_key ) ) {
194 0 : return 1;
195 0 : }
196 :
197 0 : if( !account_already_dumped( out_acct_states, *out_acct_states_cnt, account_key ) ) {
198 0 : dump_account_state( account_key, ro->meta, &out_acct_states[*out_acct_states_cnt], spad );
199 0 : (*out_acct_states_cnt)++;
200 0 : }
201 :
202 0 : if( out_ro ) {
203 0 : *out_ro = *ro;
204 0 : } else {
205 0 : fd_accdb_close_ro( accdb, ro );
206 0 : }
207 0 : return 0;
208 0 : }
209 :
210 : static void
211 : dump_executable_account_if_exists( fd_accdb_user_t * accdb,
212 : fd_funk_txn_xid_t const * xid,
213 : fd_exec_test_acct_state_t const * program_account,
214 : fd_spad_t * spad,
215 : fd_exec_test_acct_state_t * out_account_states,
216 0 : pb_size_t * out_account_states_count ) {
217 0 : if( FD_LIKELY( memcmp( program_account->owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
218 0 : return;
219 0 : }
220 :
221 0 : fd_bpf_state_t program_loader_state[1];
222 0 : if( FD_UNLIKELY( fd_bpf_state_decode(
223 0 : program_loader_state,
224 0 : program_account->data->bytes,
225 0 : program_account->data->size ) ) ) {
226 0 : return;
227 0 : }
228 0 : if( program_loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) {
229 0 : return;
230 0 : }
231 :
232 0 : fd_pubkey_t * programdata_acc = &program_loader_state->inner.program.programdata_address;
233 0 : dump_account_if_not_already_dumped( accdb, xid, programdata_acc, spad, out_account_states, out_account_states_count, NULL );
234 0 : }
235 :
236 : static void
237 : dump_sanitized_transaction( fd_accdb_user_t * accdb,
238 : fd_funk_txn_xid_t const * xid,
239 : fd_txn_t const * txn_descriptor,
240 : uchar const * txn_payload,
241 : fd_spad_t * spad,
242 0 : fd_exec_test_sanitized_transaction_t * sanitized_transaction ) {
243 0 : fd_txn_acct_addr_lut_t const * address_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
244 :
245 : /* Transaction Context -> tx -> message */
246 0 : sanitized_transaction->has_message = true;
247 0 : fd_exec_test_transaction_message_t * message = &sanitized_transaction->message;
248 :
249 : /* Transaction Context -> tx -> message -> is_legacy */
250 0 : message->is_legacy = txn_descriptor->transaction_version == FD_TXN_VLEGACY;
251 :
252 : /* Transaction Context -> tx -> message -> header */
253 0 : message->has_header = true;
254 0 : fd_exec_test_message_header_t * header = &message->header;
255 :
256 : /* Transaction Context -> tx -> message -> header -> num_required_signatures */
257 0 : header->num_required_signatures = txn_descriptor->signature_cnt;
258 :
259 : /* Transaction Context -> tx -> message -> header -> num_readonly_signed_accounts */
260 0 : header->num_readonly_signed_accounts = txn_descriptor->readonly_signed_cnt;
261 :
262 : /* Transaction Context -> tx -> message -> header -> num_readonly_unsigned_accounts */
263 0 : header->num_readonly_unsigned_accounts = txn_descriptor->readonly_unsigned_cnt;
264 :
265 : /* Transaction Context -> tx -> message -> account_keys */
266 0 : message->account_keys_count = txn_descriptor->acct_addr_cnt;
267 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 *)) );
268 0 : fd_acct_addr_t const * account_keys = fd_txn_get_acct_addrs( txn_descriptor, txn_payload );
269 0 : for( ulong i = 0; i < txn_descriptor->acct_addr_cnt; i++ ) {
270 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)) );
271 0 : account_key->size = sizeof(fd_pubkey_t);
272 0 : memcpy( account_key->bytes, &account_keys[i], sizeof(fd_pubkey_t) );
273 0 : message->account_keys[i] = account_key;
274 0 : }
275 :
276 : /* Transaction Context -> tx -> message -> recent_blockhash */
277 0 : uchar const * recent_blockhash = fd_txn_get_recent_blockhash( txn_descriptor, txn_payload );
278 0 : memcpy( message->recent_blockhash, recent_blockhash, sizeof(fd_hash_t) );
279 :
280 : /* Transaction Context -> tx -> message -> instructions */
281 0 : message->instructions_count = txn_descriptor->instr_cnt;
282 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) );
283 0 : for( ulong i = 0; i < txn_descriptor->instr_cnt; ++i ) {
284 0 : fd_txn_instr_t instr = txn_descriptor->instr[i];
285 0 : fd_exec_test_compiled_instruction_t * compiled_instruction = &message->instructions[i];
286 :
287 : // compiled instruction -> program_id_index
288 0 : compiled_instruction->program_id_index = instr.program_id;
289 :
290 : // compiled instruction -> accounts
291 0 : compiled_instruction->accounts_count = instr.acct_cnt;
292 0 : compiled_instruction->accounts = fd_spad_alloc( spad, alignof(uint32_t), instr.acct_cnt * sizeof(uint32_t) );
293 0 : uchar const * instr_accounts = fd_txn_get_instr_accts( &instr, txn_payload );
294 0 : for( ulong j = 0; j < instr.acct_cnt; ++j ) {
295 0 : uchar instr_acct_index = instr_accounts[j];
296 0 : compiled_instruction->accounts[j] = instr_acct_index;
297 0 : }
298 :
299 : // compiled instruction -> data
300 0 : uchar const * instr_data = fd_txn_get_instr_data( &instr, txn_payload );
301 0 : compiled_instruction->data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(instr.data_sz) );
302 0 : compiled_instruction->data->size = instr.data_sz;
303 0 : memcpy( compiled_instruction->data->bytes, instr_data, instr.data_sz );
304 0 : }
305 :
306 : /* ALUT stuff (non-legacy) */
307 0 : message->address_table_lookups_count = 0;
308 0 : if( !message->is_legacy ) {
309 : /* Transaction Context -> tx -> message -> address_table_lookups */
310 0 : message->address_table_lookups_count = txn_descriptor->addr_table_lookup_cnt;
311 0 : message->address_table_lookups = fd_spad_alloc( spad,
312 0 : alignof(fd_exec_test_message_address_table_lookup_t),
313 0 : txn_descriptor->addr_table_lookup_cnt * sizeof(fd_exec_test_message_address_table_lookup_t) );
314 0 : for( ulong i = 0; i < txn_descriptor->addr_table_lookup_cnt; ++i ) {
315 : // alut -> account_key
316 0 : fd_pubkey_t * alut_key = (fd_pubkey_t *) (txn_payload + address_lookup_tables[i].addr_off);
317 0 : memcpy( message->address_table_lookups[i].account_key, alut_key, sizeof(fd_pubkey_t) );
318 :
319 : // Access ALUT account data to access its keys
320 0 : fd_accdb_ro_t addr_lut_ro[1];
321 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, addr_lut_ro, xid, alut_key ) ) ) {
322 0 : FD_LOG_ERR(( "addr lut not found" ));
323 0 : }
324 :
325 : // alut -> writable_indexes
326 0 : message->address_table_lookups[i].writable_indexes_count = address_lookup_tables[i].writable_cnt;
327 0 : message->address_table_lookups[i].writable_indexes = fd_spad_alloc( spad, alignof(uint32_t), address_lookup_tables[i].writable_cnt * sizeof(uint32_t) );
328 0 : uchar * writable_indexes = (uchar *) (txn_payload + address_lookup_tables[i].writable_off);
329 0 : for( ulong j = 0; j < address_lookup_tables[i].writable_cnt; ++j ) {
330 0 : message->address_table_lookups[i].writable_indexes[j] = writable_indexes[j];
331 0 : }
332 :
333 : // alut -> readonly_indexes
334 0 : message->address_table_lookups[i].readonly_indexes_count = address_lookup_tables[i].readonly_cnt;
335 0 : message->address_table_lookups[i].readonly_indexes = fd_spad_alloc( spad, alignof(uint32_t), address_lookup_tables[i].readonly_cnt * sizeof(uint32_t) );
336 0 : uchar * readonly_indexes = (uchar *) (txn_payload + address_lookup_tables[i].readonly_off);
337 0 : for( ulong j = 0; j < address_lookup_tables[i].readonly_cnt; ++j ) {
338 0 : message->address_table_lookups[i].readonly_indexes[j] = readonly_indexes[j];
339 0 : }
340 :
341 0 : fd_accdb_close_ro( accdb, addr_lut_ro );
342 0 : }
343 0 : }
344 :
345 : /* Transaction Context -> tx -> message_hash */
346 : // Skip because it does not matter what's in here
347 :
348 : /* Transaction Context -> tx -> signatures */
349 0 : sanitized_transaction->signatures_count = txn_descriptor->signature_cnt;
350 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 *)) );
351 0 : fd_ed25519_sig_t const * signatures = fd_txn_get_signatures( txn_descriptor, txn_payload );
352 0 : for( uchar i = 0; i < txn_descriptor->signature_cnt; ++i ) {
353 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)) );
354 0 : signature->size = sizeof(fd_ed25519_sig_t);
355 0 : memcpy( signature->bytes, &signatures[i], sizeof(fd_ed25519_sig_t) );
356 0 : sanitized_transaction->signatures[i] = signature;
357 0 : }
358 0 : }
359 :
360 : static void
361 : dump_fee_rate_governor( fd_bank_t * bank,
362 0 : fd_exec_test_fee_rate_governor_t * out ) {
363 0 : fd_fee_rate_governor_t const * frg = &bank->f.fee_rate_governor;
364 0 : *out = (fd_exec_test_fee_rate_governor_t){
365 0 : .target_lamports_per_signature = frg->target_lamports_per_signature,
366 0 : .target_signatures_per_slot = frg->target_signatures_per_slot,
367 0 : .min_lamports_per_signature = frg->min_lamports_per_signature,
368 0 : .max_lamports_per_signature = frg->max_lamports_per_signature,
369 0 : .burn_percent = frg->burn_percent,
370 0 : };
371 0 : }
372 :
373 : static void
374 : dump_epoch_schedule( fd_bank_t * bank,
375 0 : fd_exec_test_epoch_schedule_t * out ) {
376 0 : fd_epoch_schedule_t const * es = &bank->f.epoch_schedule;
377 0 : *out = (fd_exec_test_epoch_schedule_t){
378 0 : .slots_per_epoch = es->slots_per_epoch,
379 0 : .leader_schedule_slot_offset = es->leader_schedule_slot_offset,
380 0 : .warmup = es->warmup,
381 0 : .first_normal_epoch = es->first_normal_epoch,
382 0 : .first_normal_slot = es->first_normal_slot,
383 0 : };
384 0 : }
385 :
386 : static void
387 : dump_blockhash_queue( fd_bank_t * bank,
388 : fd_spad_t * spad,
389 : fd_exec_test_blockhash_queue_entry_t ** entries_out,
390 0 : pb_size_t * count_out ) {
391 0 : fd_blockhashes_t const * bhq = &bank->f.block_hash_queue;
392 0 : ulong bhq_size = fd_ulong_min( FD_BLOCKHASHES_MAX, fd_blockhash_deq_cnt( bhq->d.deque ) );
393 :
394 0 : fd_exec_test_blockhash_queue_entry_t * entries = fd_spad_alloc( spad,
395 0 : alignof(fd_exec_test_blockhash_queue_entry_t),
396 0 : bhq_size * sizeof(fd_exec_test_blockhash_queue_entry_t) );
397 :
398 0 : ulong cnt = 0UL;
399 0 : for( fd_blockhash_deq_iter_t iter=fd_blockhash_deq_iter_init_rev( bhq->d.deque );
400 0 : !fd_blockhash_deq_iter_done_rev( bhq->d.deque, iter ) && cnt<bhq_size;
401 0 : iter=fd_blockhash_deq_iter_prev( bhq->d.deque, iter ), cnt++ ) {
402 0 : fd_blockhash_info_t const * ele = fd_blockhash_deq_iter_ele_const( bhq->d.deque, iter );
403 0 : fd_exec_test_blockhash_queue_entry_t * entry = &entries[bhq_size-cnt-1UL];
404 0 : fd_memcpy( entry->blockhash, ele->hash.uc, sizeof(fd_hash_t) );
405 0 : entry->lamports_per_signature = ele->lamports_per_signature;
406 0 : }
407 :
408 0 : *entries_out = entries;
409 0 : *count_out = (pb_size_t)bhq_size;
410 0 : }
411 :
412 : static void
413 : dump_txn_bank( fd_bank_t * bank,
414 : fd_spad_t * spad,
415 0 : fd_exec_test_txn_context_t * txn_context ) {
416 0 : txn_context->has_bank = true;
417 0 : fd_exec_test_txn_bank_t * txn_bank = &txn_context->bank;
418 :
419 : /* TxnBank -> blockhash_queue */
420 0 : dump_blockhash_queue( bank, spad, &txn_bank->blockhash_queue, &txn_bank->blockhash_queue_count );
421 :
422 : /* TxnBank -> rbh_lamports_per_signature */
423 0 : txn_bank->rbh_lamports_per_signature = (uint)bank->f.rbh_lamports_per_sig;
424 :
425 : /* TxnBank -> fee_rate_governor */
426 0 : txn_bank->has_fee_rate_governor = true;
427 0 : dump_fee_rate_governor( bank, &txn_bank->fee_rate_governor );
428 :
429 : /* TxnBank -> total_epoch_stake */
430 0 : txn_bank->total_epoch_stake = bank->f.total_epoch_stake;
431 :
432 : /* TxnBank -> epoch_schedule */
433 0 : txn_bank->has_epoch_schedule = true;
434 0 : dump_epoch_schedule( bank, &txn_bank->epoch_schedule );
435 :
436 : /* TxnBank -> features */
437 0 : txn_bank->has_features = true;
438 0 : dump_sorted_features( &bank->f.features, &txn_bank->features, spad );
439 0 : }
440 :
441 : /** SECONDARY FUNCTIONS **/
442 :
443 : /* add_account_to_dumped_accounts adds an account to the dumped accounts
444 : set if it does not exist already. Returns 0 if the account already
445 : exists, and 1 if the account was added successfully.
446 :
447 : TODO: Txn dumping should be optimized to use these functions. */
448 : static uchar
449 : add_account_to_dumped_accounts( fd_dump_account_key_set_t * dumped_accounts,
450 0 : fd_pubkey_t const * pubkey ) {
451 0 : if( fd_pubkey_eq( pubkey, &fd_solana_system_program_id ) ) {
452 0 : if( dumped_accounts->system_program_dumped ) return 0;
453 0 : dumped_accounts->system_program_dumped = 1;
454 0 : dumped_accounts->cnt++;
455 0 : return 1;
456 0 : }
457 :
458 : /* If the key already exists, return early. */
459 0 : if( fd_dump_account_key_map_query( dumped_accounts->map, *pubkey, NULL ) ) {
460 0 : return 0;
461 0 : }
462 :
463 0 : if( FD_UNLIKELY( dumped_accounts->cnt-dumped_accounts->system_program_dumped>=fd_dump_account_key_map_key_max() ) ) {
464 0 : FD_LOG_CRIT(( "too many dumped accounts for fd_dump_account_key_map" ));
465 0 : }
466 0 : fd_dump_account_t * new_ele = fd_dump_account_key_map_insert( dumped_accounts->map, *pubkey );
467 0 : if( FD_UNLIKELY( !new_ele ) ) FD_LOG_CRIT(( "fd_dump_account_key_map_insert failed" ));
468 0 : dumped_accounts->cnt++;
469 0 : return 1;
470 0 : }
471 :
472 : /* add_account_and_programdata_to_dumped_accounts adds an account and
473 : its programdata account (if the account is a v3 program) to the
474 : dumped accounts set if they do not exist already. */
475 : static void
476 : add_account_and_programdata_to_dumped_accounts( fd_accdb_user_t * accdb,
477 : fd_funk_txn_xid_t const * xid,
478 : fd_dump_account_key_set_t * dumped_accounts,
479 0 : fd_pubkey_t const * pubkey ) {
480 : /* Add the current account to the dumped accounts set. We can save
481 : some time by enforcing an invariant that "if current account was
482 : dumped, then programdata account was also dumped," so we save
483 : ourselves a call to Funk. */
484 0 : uchar ret = add_account_to_dumped_accounts( dumped_accounts, pubkey );
485 0 : if( ret==0 ) return;
486 :
487 : /* Read the account from Funk to see if its a program account and if
488 : it needs to be dumped. */
489 0 : fd_accdb_ro_t program_account[1];
490 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, program_account, xid, pubkey ) ) ) {
491 0 : return;
492 0 : }
493 :
494 : /* Return if its not owned by the v3 loader */
495 0 : if( FD_LIKELY( !fd_pubkey_eq( fd_accdb_ref_owner( program_account ), &fd_solana_bpf_loader_upgradeable_program_id ) ) ) {
496 0 : fd_accdb_close_ro( accdb, program_account );
497 0 : return;
498 0 : }
499 :
500 : /* Get the program account state */
501 0 : fd_bpf_state_t program_account_state[1];
502 0 : if( FD_UNLIKELY( fd_bpf_state_decode(
503 0 : program_account_state,
504 0 : fd_accdb_ref_data_const( program_account ),
505 0 : fd_accdb_ref_data_sz ( program_account ) ) ) ) {
506 0 : fd_accdb_close_ro( accdb, program_account );
507 0 : return;
508 0 : }
509 0 : if( program_account_state->discriminant!=FD_BPF_STATE_PROGRAM ) {
510 0 : fd_accdb_close_ro( accdb, program_account );
511 0 : return;
512 0 : }
513 :
514 : /* Dump the programdata address */
515 0 : add_account_to_dumped_accounts( dumped_accounts, &program_account_state->inner.program.programdata_address );
516 0 : fd_accdb_close_ro( accdb, program_account );
517 0 : }
518 :
519 : /* add_lut_account_to_dumped_accounts adds an address lookup table
520 : account AND all pubkeys in the lookup table to the dumped accounts
521 : set if they do not exist already. */
522 : static void
523 : add_lut_accounts_to_dumped_accounts( fd_accdb_user_t * accdb,
524 : fd_funk_txn_xid_t const * xid,
525 : fd_dump_account_key_set_t * dumped_accounts,
526 0 : fd_pubkey_t const * pubkey ) {
527 : /* Add the current account to the dumped accounts set. */
528 0 : add_account_to_dumped_accounts( dumped_accounts, pubkey );
529 :
530 : /* Read the account and dump all pubkeys within the lookup table. */
531 0 : fd_accdb_ro_t lut_account[1];
532 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, lut_account, xid, pubkey ) ) ) {
533 0 : return;
534 0 : }
535 :
536 0 : uchar const * data = fd_accdb_ref_data_const( lut_account );
537 0 : ulong data_len = fd_accdb_ref_data_sz ( lut_account );
538 :
539 : /* Decode the ALUT account and dump all pubkeys within the lookup
540 : table. */
541 0 : if( data_len<FD_LOOKUP_TABLE_META_SIZE || (data_len&0x1fUL) ) {
542 0 : fd_accdb_close_ro( accdb, lut_account );
543 0 : return;
544 0 : }
545 0 : fd_pubkey_t const * lookup_addrs = fd_type_pun_const( data+FD_LOOKUP_TABLE_META_SIZE );
546 0 : ulong lookup_addrs_cnt = ( data_len-FD_LOOKUP_TABLE_META_SIZE)>>5UL; // = (dlen - 56) / 32
547 0 : for( ulong i=0UL; i<lookup_addrs_cnt; i++ ) {
548 0 : fd_pubkey_t const * referenced_pubkey = &lookup_addrs[i];
549 0 : add_account_and_programdata_to_dumped_accounts( accdb, xid, dumped_accounts, referenced_pubkey );
550 0 : }
551 0 : fd_accdb_close_ro( accdb, lut_account );
552 0 : }
553 :
554 : static void
555 : create_block_context_protobuf_from_block( fd_block_dump_ctx_t * dump_ctx,
556 : fd_banks_t * banks,
557 : fd_bank_t * bank,
558 : fd_accdb_user_t * accdb,
559 0 : fd_runtime_stack_t * runtime_stack ) {
560 : /* We should use the bank fields and funk txn from the parent slot in
561 : order to capture the block context from before the current block
562 : was executed, since dumping is happening in the block finalize
563 : step. */
564 0 : fd_bank_t * parent_bank = fd_banks_get_parent( banks, bank );
565 0 : fd_funk_txn_xid_t parent_xid = fd_bank_xid( parent_bank );
566 0 : fd_exec_test_block_context_t * block_context = &dump_ctx->block_context;
567 0 : ulong dump_txn_count = dump_ctx->txns_to_dump_cnt;
568 0 : fd_spad_t * spad = dump_ctx->spad;
569 :
570 : /* Get vote and stake delegation infos */
571 0 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( parent_bank );
572 0 : ulong vote_account_t_cnt = fd_vote_stakes_ele_cnt( vote_stakes, parent_bank->vote_stakes_fork_id );
573 :
574 0 : fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_frontier_query( banks, parent_bank );
575 :
576 : /* Collect account states in a temporary set before iterating over
577 : them and dumping them out. */
578 0 : void * dumped_accounts_mem = fd_spad_alloc( spad, fd_dump_account_key_map_align(), fd_dump_account_key_map_footprint() );
579 0 : fd_dump_account_t * dumped_accounts_map = fd_dump_account_key_map_join( fd_dump_account_key_map_new( dumped_accounts_mem ) );
580 0 : fd_dump_account_key_set_t dumped_accounts[1] = {{ dumped_accounts_map, 0UL, 0 }};
581 :
582 : /* BlockContext -> txns */
583 0 : block_context->txns_count = (pb_size_t)dump_txn_count;
584 0 : block_context->txns = fd_spad_alloc( spad, alignof(fd_exec_test_sanitized_transaction_t), dump_ctx->txns_to_dump_cnt * sizeof(fd_exec_test_sanitized_transaction_t) );
585 0 : fd_memset( block_context->txns, 0, dump_ctx->txns_to_dump_cnt * sizeof(fd_exec_test_sanitized_transaction_t) );
586 :
587 : /* Dump sanitized transactions from the transaction descriptors */
588 0 : for( ulong i=0UL; i<dump_ctx->txns_to_dump_cnt; i++ ) {
589 0 : fd_txn_p_t const * txn_ptr = &dump_ctx->txns_to_dump[i];
590 0 : fd_txn_t const * txn_descriptor = TXN( txn_ptr );
591 0 : dump_sanitized_transaction( accdb, &parent_xid, txn_descriptor, txn_ptr->payload, spad, &block_context->txns[i] );
592 :
593 : /* Dump account + alut + programdata accounts (if applicable).
594 : 1. Dump the raw txn account keys
595 : 2. Dump the ALUT accounts
596 : 3. Dump all referenced accounts in the ALUTs
597 : 4. Dump any executable accounts */
598 :
599 : // 1 + 4. Dump any account keys that are referenced by transactions
600 : // + any programdata accounts (if applicable).
601 0 : fd_acct_addr_t const * account_keys = fd_txn_get_acct_addrs( txn_descriptor, txn_ptr->payload );
602 0 : for( ushort l=0; l<txn_descriptor->acct_addr_cnt; l++ ) {
603 0 : fd_pubkey_t const * account_key = fd_type_pun_const( &account_keys[l] );
604 0 : add_account_and_programdata_to_dumped_accounts( accdb, &parent_xid, dumped_accounts, account_key );
605 0 : }
606 :
607 : // 2 + 3 + 4. Dump any ALUT accounts + any accounts referenced in
608 : // the ALUTs + any programdata accounts (if applicable).
609 0 : fd_txn_acct_addr_lut_t const * txn_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
610 0 : for( ushort l=0; l<txn_descriptor->addr_table_lookup_cnt; l++ ) {
611 0 : fd_txn_acct_addr_lut_t const * lookup_table = &txn_lookup_tables[l];
612 0 : fd_pubkey_t const * lut_key = fd_type_pun_const( txn_ptr->payload+lookup_table->addr_off );
613 0 : add_lut_accounts_to_dumped_accounts( accdb, &parent_xid, dumped_accounts, lut_key );
614 0 : }
615 0 : }
616 :
617 : /* Dump sysvars */
618 0 : for( ulong i=0UL; i<num_sysvar_entries; i++ ) {
619 0 : add_account_to_dumped_accounts( dumped_accounts, fd_dump_sysvar_ids[i] );
620 0 : }
621 :
622 : /* Dump builtins */
623 0 : for( ulong i=0UL; i<num_loaded_builtins; i++ ) {
624 0 : add_account_to_dumped_accounts( dumped_accounts, fd_dump_builtin_ids[i] );
625 0 : }
626 :
627 : /* Dump stake accounts for this epoch */
628 0 : fd_stake_delegations_iter_t iter_[1];
629 0 : for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
630 0 : !fd_stake_delegations_iter_done( iter );
631 0 : fd_stake_delegations_iter_next( iter ) ) {
632 0 : fd_stake_delegation_t const * stake_delegation = fd_stake_delegations_iter_ele( iter );
633 0 : add_account_to_dumped_accounts( dumped_accounts, &stake_delegation->stake_account );
634 0 : }
635 0 : fd_bank_stake_delegations_end_frontier_query( banks, parent_bank );
636 :
637 :
638 0 : ushort fork_idx = parent_bank->vote_stakes_fork_id;
639 0 : uchar __attribute__((aligned(FD_VOTE_STAKES_ITER_ALIGN))) iter_mem[ FD_VOTE_STAKES_ITER_FOOTPRINT ];
640 0 : for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_fork_iter_init( vote_stakes, fork_idx, iter_mem );
641 0 : !fd_vote_stakes_fork_iter_done( vote_stakes, fork_idx, iter );
642 0 : fd_vote_stakes_fork_iter_next( vote_stakes, fork_idx, iter ) ) {
643 0 : fd_pubkey_t pubkey;
644 0 : fd_vote_stakes_fork_iter_ele( vote_stakes, fork_idx, iter, &pubkey, NULL, NULL, NULL, NULL, NULL, NULL );
645 0 : add_account_to_dumped_accounts( dumped_accounts, &pubkey );
646 0 : }
647 0 : fd_vote_stakes_fork_iter_fini( vote_stakes );
648 :
649 : /* BlockBank -> vote_accounts_t_1 and vote_accounts_t_2 */
650 0 : fd_exec_test_prev_vote_account_t * va_t1 = fd_spad_alloc( spad,
651 0 : alignof(fd_exec_test_prev_vote_account_t),
652 0 : vote_account_t_cnt * sizeof(fd_exec_test_prev_vote_account_t) );
653 0 : fd_exec_test_prev_vote_account_t * va_t2 = fd_spad_alloc( spad,
654 0 : alignof(fd_exec_test_prev_vote_account_t),
655 0 : vote_account_t_cnt * sizeof(fd_exec_test_prev_vote_account_t) );
656 0 : pb_size_t va_t1_cnt = 0U;
657 0 : pb_size_t va_t2_cnt = 0U;
658 :
659 0 : for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_fork_iter_init( vote_stakes, fork_idx, iter_mem );
660 0 : !fd_vote_stakes_fork_iter_done( vote_stakes, fork_idx, iter );
661 0 : fd_vote_stakes_fork_iter_next( vote_stakes, fork_idx, iter ) ) {
662 0 : fd_pubkey_t pubkey;
663 0 : ulong stake_t_1;
664 0 : ulong stake_t_2;
665 0 : fd_pubkey_t node_t_1;
666 0 : fd_pubkey_t node_t_2;
667 0 : ushort commission_t_1;
668 0 : ushort commission_t_2;
669 0 : fd_vote_stakes_fork_iter_ele( vote_stakes, fork_idx, iter, &pubkey, &stake_t_1, &stake_t_2, &node_t_1, &node_t_2, &commission_t_1, &commission_t_2 );
670 :
671 0 : if( stake_t_1 ) {
672 0 : fd_exec_test_prev_vote_account_t * entry = &va_t1[ va_t1_cnt++ ];
673 0 : fd_memcpy( entry->address, &pubkey, sizeof(fd_pubkey_t) );
674 0 : fd_memcpy( entry->node_pubkey, &node_t_1, sizeof(fd_pubkey_t) );
675 0 : entry->stake = stake_t_1;
676 0 : entry->commission_bps = commission_t_1;
677 0 : entry->version = FD_EXEC_TEST_VOTE_ACCOUNT_VERSION_V3;
678 0 : entry->epoch_credits_count = 0U;
679 0 : }
680 :
681 0 : if( stake_t_2 ) {
682 0 : fd_exec_test_prev_vote_account_t * entry = &va_t2[ va_t2_cnt++ ];
683 0 : fd_memcpy( entry->address, &pubkey, sizeof(fd_pubkey_t) );
684 0 : fd_memcpy( entry->node_pubkey, &node_t_2, sizeof(fd_pubkey_t) );
685 0 : entry->stake = stake_t_2;
686 0 : entry->commission_bps = commission_t_2;
687 0 : entry->version = FD_EXEC_TEST_VOTE_ACCOUNT_VERSION_V3;
688 0 : entry->epoch_credits_count = 0U;
689 0 : }
690 0 : }
691 0 : fd_vote_stakes_fork_iter_fini( vote_stakes );
692 :
693 : /* Dump epoch_credits from runtime_stack->stakes.vote_ele if the
694 : vote_ele_map has been populated (happens after epoch boundary
695 : reward calculation). Needed for the harness to correctly
696 : recalculate partitioned epoch rewards. */
697 0 : fd_vote_rewards_map_t * vote_ele_map = runtime_stack->stakes.vote_map;
698 0 : for( pb_size_t i=0U; i<va_t1_cnt; i++ ) {
699 0 : fd_pubkey_t va_pubkey = FD_LOAD( fd_pubkey_t, va_t1[i].address );
700 0 : uint idx = (uint)fd_vote_rewards_map_idx_query( vote_ele_map, &va_pubkey, UINT_MAX, runtime_stack->stakes.vote_ele );
701 0 : if( idx==UINT_MAX ) continue;
702 0 : fd_epoch_credits_t * ec = &fd_bank_epoch_credits( parent_bank )[idx];
703 0 : ulong cnt = ec->cnt;
704 0 : ulong base = ec->base_credits;
705 0 : va_t1[i].epoch_credits_count = (pb_size_t)cnt;
706 0 : va_t1[i].epoch_credits = fd_spad_alloc( spad, alignof(fd_exec_test_epoch_credit_t), cnt * sizeof(fd_exec_test_epoch_credit_t) );
707 0 : for( ulong j=0; j<cnt; j++ ) {
708 0 : va_t1[i].epoch_credits[j].epoch = ec->epoch[j];
709 0 : va_t1[i].epoch_credits[j].credits = base + ec->credits_delta[j];
710 0 : va_t1[i].epoch_credits[j].prev_credits = base + ec->prev_credits_delta[j];
711 0 : }
712 0 : }
713 :
714 : /* BlockContext -> acct_states
715 : Iterate over the set and dump all the account keys in one pass. */
716 0 : block_context->acct_states_count = 0U;
717 0 : block_context->acct_states = fd_spad_alloc(
718 0 : spad,
719 0 : alignof(fd_exec_test_acct_state_t),
720 0 : dumped_accounts->cnt*sizeof(fd_exec_test_acct_state_t) );
721 0 : fd_dump_account_key_iter_t dumped_accounts_iter_[1];
722 0 : for( fd_dump_account_key_iter_t * iter = fd_dump_account_key_iter_init( dumped_accounts_iter_, dumped_accounts );
723 0 : !fd_dump_account_key_iter_done( iter );
724 0 : fd_dump_account_key_iter_next( iter ) ) {
725 0 : fd_pubkey_t const * pubkey = fd_dump_account_key_iter_ele( iter );
726 0 : fd_accdb_ro_t ro[1];
727 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, ro, &parent_xid, pubkey ) ) ) {
728 0 : continue;
729 0 : }
730 0 : dump_account_state(
731 0 : fd_accdb_ref_address( ro ),
732 0 : ro->meta,
733 0 : &block_context->acct_states[block_context->acct_states_count++],
734 0 : spad );
735 0 : fd_accdb_close_ro( accdb, ro );
736 0 : }
737 :
738 : /* BlockContext -> bank */
739 0 : block_context->has_bank = true;
740 0 : fd_exec_test_block_bank_t * block_bank = &block_context->bank;
741 :
742 : /* BlockBank -> blockhash_queue */
743 0 : dump_blockhash_queue( parent_bank, spad, &block_bank->blockhash_queue, &block_bank->blockhash_queue_count );
744 :
745 : /* BlockBank -> rbh_lamports_per_signature */
746 0 : block_bank->rbh_lamports_per_signature = (uint)parent_bank->f.rbh_lamports_per_sig;
747 :
748 : /* BlockBank -> fee_rate_governor */
749 0 : block_bank->has_fee_rate_governor = true;
750 0 : dump_fee_rate_governor( parent_bank, &block_bank->fee_rate_governor );
751 :
752 : /* BlockBank -> slot */
753 0 : block_bank->slot = bank->f.slot;
754 :
755 : /* BlockBank -> parent_slot */
756 0 : block_bank->parent_slot = bank->f.parent_slot;
757 :
758 : /* BlockBank -> capitalization */
759 0 : block_bank->capitalization = parent_bank->f.capitalization;
760 :
761 : /* BlockBank -> ns_per_slot */
762 0 : fd_w_u128_t ns_per_slot = bank->f.ns_per_slot;
763 0 : fd_memcpy( block_bank->ns_per_slot, &ns_per_slot.ud, sizeof(uint128) );
764 :
765 : /* BlockBank -> inflation */
766 0 : block_bank->has_inflation = true;
767 0 : fd_inflation_t const * inflation = &parent_bank->f.inflation;
768 0 : block_bank->inflation = (fd_exec_test_inflation_t){
769 0 : .initial = inflation->initial,
770 0 : .terminal = inflation->terminal,
771 0 : .taper = inflation->taper,
772 0 : .foundation = inflation->foundation,
773 0 : .foundation_term = inflation->foundation_term,
774 0 : };
775 :
776 : /* BlockBank -> block_height */
777 0 : block_bank->block_height = bank->f.block_height;
778 :
779 : /* BlockBank -> poh */
780 0 : fd_memcpy( block_bank->poh, &bank->f.poh, sizeof(fd_hash_t) );
781 :
782 : /* BlockBank -> parent_bank_hash */
783 0 : fd_memcpy( block_bank->parent_bank_hash, &parent_bank->f.bank_hash, sizeof(fd_hash_t) );
784 :
785 : /* BlockBank -> parent_lt_hash */
786 0 : fd_lthash_value_t const * parent_lthash = fd_bank_lthash_locking_query( parent_bank );
787 0 : fd_memcpy( block_bank->parent_lt_hash, parent_lthash, sizeof(fd_lthash_value_t) );
788 0 : fd_bank_lthash_end_locking_query( parent_bank );
789 :
790 : /* BlockBank -> parent_signature_count */
791 0 : block_bank->parent_signature_count = parent_bank->f.parent_signature_cnt;
792 :
793 : /* BlockBank -> epoch_schedule */
794 0 : block_bank->has_epoch_schedule = true;
795 0 : dump_epoch_schedule( parent_bank, &block_bank->epoch_schedule );
796 :
797 : /* BlockBank -> features */
798 0 : block_bank->has_features = true;
799 0 : dump_sorted_features( &parent_bank->f.features, &block_bank->features, spad );
800 :
801 : /* BlockBank -> vote_accounts_t_1 / vote_accounts_t_2 */
802 0 : block_bank->vote_accounts_t_1 = va_t1;
803 0 : block_bank->vote_accounts_t_1_count = va_t1_cnt;
804 0 : block_bank->vote_accounts_t_2 = va_t2;
805 0 : block_bank->vote_accounts_t_2_count = va_t2_cnt;
806 0 : }
807 :
808 : static void
809 : create_txn_context_protobuf_from_txn( fd_exec_test_txn_context_t * txn_context_msg,
810 : fd_runtime_t * runtime,
811 : fd_bank_t * bank,
812 : fd_txn_in_t const * txn_in,
813 : fd_txn_out_t * txn_out,
814 0 : fd_spad_t * spad ) {
815 0 : fd_txn_t const * txn_descriptor = TXN( txn_in->txn );
816 0 : uchar const * txn_payload = (uchar const *) txn_in->txn->payload;
817 :
818 : /* Transaction Context -> account_shared_data
819 : Contains:
820 : - Account data for regular accounts
821 : - Account data for LUT accounts
822 : - Account data for executable accounts
823 : - Account data for (almost) all sysvars */
824 0 : txn_context_msg->account_shared_data_count = 0;
825 0 : txn_context_msg->account_shared_data = fd_spad_alloc( spad,
826 0 : alignof(fd_exec_test_acct_state_t),
827 0 : (256UL*2UL + txn_descriptor->addr_table_lookup_cnt + num_sysvar_entries) * sizeof(fd_exec_test_acct_state_t) );
828 0 : fd_xid_t xid = fd_bank_xid( bank );
829 :
830 : /* Dump regular accounts first */
831 0 : for( ulong i = 0; i < txn_out->accounts.cnt; ++i ) {
832 0 : dump_account_if_not_already_dumped(
833 0 : runtime->accdb,
834 0 : &xid,
835 0 : &txn_out->accounts.keys[i],
836 0 : spad,
837 0 : txn_context_msg->account_shared_data,
838 0 : &txn_context_msg->account_shared_data_count,
839 0 : NULL
840 0 : );
841 0 : }
842 :
843 : // Dump LUT accounts
844 0 : fd_txn_acct_addr_lut_t const * address_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
845 0 : for( ulong i = 0; i < txn_descriptor->addr_table_lookup_cnt; ++i ) {
846 0 : fd_txn_acct_addr_lut_t const * addr_lut = &address_lookup_tables[i];
847 0 : fd_pubkey_t * alut_key = (fd_pubkey_t *) (txn_payload + addr_lut->addr_off);
848 :
849 : // Dump the LUT account itself if not already dumped
850 0 : fd_accdb_ro_t ro[1];
851 0 : int ret = dump_account_if_not_already_dumped(
852 0 : runtime->accdb,
853 0 : &xid,
854 0 : alut_key,
855 0 : spad,
856 0 : txn_context_msg->account_shared_data,
857 0 : &txn_context_msg->account_shared_data_count,
858 0 : ro
859 0 : );
860 0 : if( FD_UNLIKELY( ret ) ) continue;
861 :
862 0 : uchar const * alut_data = fd_accdb_ref_data_const( ro );
863 0 : ulong alut_sz = fd_accdb_ref_data_sz ( ro );
864 :
865 0 : if( FD_UNLIKELY( alut_sz<FD_LOOKUP_TABLE_META_SIZE ) ) {
866 : /* Skip over invalid address lookup tables */
867 0 : fd_accdb_close_ro( runtime->accdb, ro );
868 0 : continue;
869 0 : }
870 :
871 0 : fd_pubkey_t const * lookup_addrs = fd_type_pun_const( alut_data+FD_LOOKUP_TABLE_META_SIZE );
872 0 : ulong lookup_addrs_cnt = (alut_sz - FD_LOOKUP_TABLE_META_SIZE) / sizeof(fd_pubkey_t);
873 :
874 : /* Dump any account state refererenced in ALUTs */
875 0 : uchar const * writable_lut_idxs = txn_payload + addr_lut->writable_off;
876 0 : for( ulong j=0; j<addr_lut->writable_cnt; j++ ) {
877 0 : if( writable_lut_idxs[j] >= lookup_addrs_cnt ) {
878 0 : continue;
879 0 : }
880 0 : fd_pubkey_t const * referenced_addr = lookup_addrs + writable_lut_idxs[j];
881 0 : dump_account_if_not_already_dumped(
882 0 : runtime->accdb,
883 0 : &xid,
884 0 : referenced_addr,
885 0 : spad,
886 0 : txn_context_msg->account_shared_data,
887 0 : &txn_context_msg->account_shared_data_count,
888 0 : NULL
889 0 : );
890 0 : }
891 :
892 0 : uchar const * readonly_lut_idxs = txn_payload + addr_lut->readonly_off;
893 0 : for( ulong j = 0; j < addr_lut->readonly_cnt; j++ ) {
894 0 : if( readonly_lut_idxs[j] >= lookup_addrs_cnt ) {
895 0 : continue;
896 0 : }
897 0 : fd_pubkey_t const * referenced_addr = lookup_addrs + readonly_lut_idxs[j];
898 0 : dump_account_if_not_already_dumped(
899 0 : runtime->accdb,
900 0 : &xid,
901 0 : referenced_addr,
902 0 : spad,
903 0 : txn_context_msg->account_shared_data,
904 0 : &txn_context_msg->account_shared_data_count,
905 0 : NULL
906 0 : );
907 0 : }
908 :
909 0 : fd_accdb_close_ro( runtime->accdb, ro );
910 0 : }
911 :
912 : /* Dump the programdata accounts for any potential v3-owned program accounts */
913 0 : uint accounts_dumped_so_far = txn_context_msg->account_shared_data_count;
914 0 : for( uint i=0U; i<accounts_dumped_so_far; i++ ) {
915 0 : fd_exec_test_acct_state_t const * maybe_program_account = &txn_context_msg->account_shared_data[i];
916 0 : dump_executable_account_if_exists( runtime->accdb, &xid, maybe_program_account, spad, txn_context_msg->account_shared_data, &txn_context_msg->account_shared_data_count );
917 0 : }
918 :
919 : /* Dump sysvars */
920 0 : for( ulong i = 0; i < num_sysvar_entries; i++ ) {
921 0 : dump_account_if_not_already_dumped(
922 0 : runtime->accdb,
923 0 : &xid,
924 0 : fd_dump_sysvar_ids[i],
925 0 : spad,
926 0 : txn_context_msg->account_shared_data,
927 0 : &txn_context_msg->account_shared_data_count,
928 0 : NULL
929 0 : );
930 0 : }
931 :
932 : /* Transaction Context -> tx */
933 0 : txn_context_msg->has_tx = true;
934 0 : fd_exec_test_sanitized_transaction_t * sanitized_transaction = &txn_context_msg->tx;
935 0 : dump_sanitized_transaction( runtime->accdb, &xid, txn_descriptor, txn_payload, spad, sanitized_transaction );
936 :
937 : /* Transaction Context -> bank */
938 0 : dump_txn_bank( bank, spad, txn_context_msg );
939 0 : }
940 :
941 : static void
942 : create_instr_context_protobuf_from_instructions( fd_exec_test_instr_context_t * instr_context,
943 : fd_runtime_t * runtime,
944 : fd_bank_t * bank,
945 : fd_txn_out_t * txn_out,
946 : fd_instr_info_t const * instr,
947 0 : fd_spad_t * spad ) {
948 : /* Program ID */
949 0 : fd_memcpy( instr_context->program_id, txn_out->accounts.keys[ instr->program_id ].uc, sizeof(fd_pubkey_t) );
950 :
951 0 : fd_xid_t xid = fd_bank_xid( bank );
952 :
953 : /* Accounts */
954 0 : instr_context->accounts_count = (pb_size_t) txn_out->accounts.cnt;
955 0 : instr_context->accounts = fd_spad_alloc( spad, alignof(fd_exec_test_acct_state_t), (instr_context->accounts_count + num_sysvar_entries + runtime->accounts.executable_cnt) * sizeof(fd_exec_test_acct_state_t));
956 0 : for( ulong i = 0; i < txn_out->accounts.cnt; i++ ) {
957 : // Copy account information over
958 0 : fd_account_meta_t * account_meta = txn_out->accounts.account[i].meta;
959 0 : fd_exec_test_acct_state_t * output_account = &instr_context->accounts[i];
960 0 : dump_account_state( &txn_out->accounts.keys[i], account_meta, output_account, spad );
961 0 : }
962 :
963 : /* Add sysvar cache variables */
964 0 : for( ulong i = 0; i < num_sysvar_entries; i++ ) {
965 0 : fd_accdb_ro_t ro[1];
966 0 : if( !fd_accdb_open_ro( runtime->accdb, ro, &xid, fd_dump_sysvar_ids[i] ) ) {
967 0 : continue;
968 0 : }
969 : // Make sure the account doesn't exist in the output accounts yet
970 0 : int account_exists = 0;
971 0 : for( ulong j = 0; j < txn_out->accounts.cnt; j++ ) {
972 0 : if( fd_pubkey_eq( &txn_out->accounts.keys[j], fd_dump_sysvar_ids[i] ) ) {
973 0 : account_exists = true;
974 0 : break;
975 0 : }
976 0 : }
977 :
978 : // Copy it into output
979 0 : if( !account_exists ) {
980 0 : fd_exec_test_acct_state_t * output_account = &instr_context->accounts[instr_context->accounts_count++];
981 0 : dump_account_state( fd_accdb_ref_address( ro ), ro->meta, output_account, spad );
982 0 : }
983 0 : fd_accdb_close_ro( runtime->accdb, ro );
984 0 : }
985 :
986 : /* Add executable accounts */
987 0 : for( ulong i = 0; i < runtime->accounts.executable_cnt; i++ ) {
988 : // Make sure the account doesn't exist in the output accounts yet
989 0 : fd_accdb_ro_t const * ro = &runtime->accounts.executable[i];
990 0 : bool account_exists = false;
991 0 : for( ulong j = 0; j < instr_context->accounts_count; j++ ) {
992 0 : if( 0 == memcmp( instr_context->accounts[j].address, fd_accdb_ref_address( ro ), sizeof(fd_pubkey_t) ) ) {
993 0 : account_exists = true;
994 0 : break;
995 0 : }
996 0 : }
997 : // Copy it into output
998 0 : if( !account_exists ) {
999 0 : fd_exec_test_acct_state_t * output_account = &instr_context->accounts[instr_context->accounts_count++];
1000 0 : dump_account_state( fd_accdb_ref_address( ro ), ro->meta, output_account, spad );
1001 0 : }
1002 0 : }
1003 :
1004 : /* Instruction Accounts */
1005 0 : instr_context->instr_accounts_count = (pb_size_t) instr->acct_cnt;
1006 0 : instr_context->instr_accounts = fd_spad_alloc( spad, alignof(fd_exec_test_instr_acct_t), instr_context->instr_accounts_count * sizeof(fd_exec_test_instr_acct_t) );
1007 0 : for( ushort i = 0; i < instr->acct_cnt; i++ ) {
1008 0 : fd_exec_test_instr_acct_t * output_instr_account = &instr_context->instr_accounts[i];
1009 :
1010 0 : output_instr_account->index = instr->accounts[i].index_in_transaction;
1011 0 : output_instr_account->is_writable = instr->accounts[i].is_writable;
1012 0 : output_instr_account->is_signer = instr->accounts[i].is_signer;
1013 0 : }
1014 :
1015 : /* Data */
1016 0 : instr_context->data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( instr->data_sz ) );
1017 0 : instr_context->data->size = (pb_size_t) instr->data_sz;
1018 0 : fd_memcpy( instr_context->data->bytes, instr->data, instr->data_sz );
1019 :
1020 : /* Compute Units */
1021 0 : instr_context->cu_avail = txn_out->details.compute_budget.compute_meter;
1022 :
1023 : /* Feature set */
1024 0 : instr_context->has_features = true;
1025 0 : dump_sorted_features( &bank->f.features, &instr_context->features, spad );
1026 0 : }
1027 :
1028 : /***** PUBLIC APIs *****/
1029 :
1030 : void
1031 : fd_dump_instr_to_protobuf( fd_runtime_t * runtime,
1032 : fd_bank_t * bank,
1033 : fd_txn_in_t const * txn_in,
1034 : fd_txn_out_t * txn_out,
1035 : fd_instr_info_t * instr,
1036 0 : ushort instruction_idx ) {
1037 : /* Check program ID filter, if it exists */
1038 0 : if( runtime->log.dump_proto_ctx->has_dump_instr_program_id_filter &&
1039 0 : memcmp( txn_out->accounts.keys[ instr->program_id ].uc, runtime->log.dump_proto_ctx->dump_instr_program_id_filter, sizeof(fd_pubkey_t) ) ) {
1040 0 : return;
1041 0 : }
1042 :
1043 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( runtime->log.dumping_mem, 1UL<<28UL ) );
1044 :
1045 0 : FD_SPAD_FRAME_BEGIN( spad ) {
1046 : // Get base58-encoded tx signature
1047 0 : const fd_ed25519_sig_t * signatures = fd_txn_get_signatures( TXN( txn_in->txn ), txn_in->txn->payload );
1048 0 : char encoded_signature[FD_BASE58_ENCODED_64_SZ];
1049 0 : fd_base58_encode_64( signatures[0], NULL, encoded_signature );
1050 :
1051 0 : fd_exec_test_instr_context_t instr_context = FD_EXEC_TEST_INSTR_CONTEXT_INIT_DEFAULT;
1052 0 : create_instr_context_protobuf_from_instructions( &instr_context, runtime, bank, txn_out, instr, spad );
1053 :
1054 : /* Output to file */
1055 0 : ulong out_buf_size = 100 * 1024 * 1024;
1056 0 : uint8_t * out = fd_spad_alloc( spad, alignof(uchar) , out_buf_size );
1057 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1058 0 : if (pb_encode(&stream, FD_EXEC_TEST_INSTR_CONTEXT_FIELDS, &instr_context)) {
1059 0 : char output_filepath[ PATH_MAX ];
1060 0 : snprintf( output_filepath, PATH_MAX, "%s/instr-%s-%hu.instrctx", runtime->log.dump_proto_ctx->dump_proto_output_dir, encoded_signature, instruction_idx );
1061 0 : FILE * file = fopen(output_filepath, "wb");
1062 0 : if( file ) {
1063 0 : fwrite( out, 1, stream.bytes_written, file );
1064 0 : fclose( file );
1065 0 : }
1066 0 : }
1067 0 : } FD_SPAD_FRAME_END;
1068 0 : }
1069 :
1070 : /* Writes a single account state into the resulting_state field of a
1071 : TxnResult protobuf. Sub-allocations for account data are bump-
1072 : allocated from the caller's scratch region via _l. */
1073 : static void
1074 : write_account_to_result( fd_pubkey_t const * pubkey,
1075 : fd_account_meta_t const * meta,
1076 : fd_exec_test_acct_state_t * out_accounts,
1077 : pb_size_t * out_accounts_cnt,
1078 : ulong * scratch_cur,
1079 0 : ulong scratch_end ) {
1080 0 : fd_exec_test_acct_state_t * out_acct = &out_accounts[ *out_accounts_cnt ];
1081 0 : (*out_accounts_cnt)++;
1082 :
1083 0 : memset( out_acct, 0, sizeof(fd_exec_test_acct_state_t) );
1084 0 : memcpy( out_acct->address, pubkey, sizeof(fd_pubkey_t) );
1085 0 : out_acct->lamports = meta->lamports;
1086 :
1087 0 : if( meta->dlen>0UL ) {
1088 0 : pb_bytes_array_t * data = (pb_bytes_array_t *)fd_ulong_align_up( *scratch_cur, alignof(pb_bytes_array_t) );
1089 0 : *scratch_cur = (ulong)data + PB_BYTES_ARRAY_T_ALLOCSIZE( meta->dlen );
1090 0 : if( FD_UNLIKELY( *scratch_cur > scratch_end ) ) abort();
1091 0 : data->size = (pb_size_t)meta->dlen;
1092 0 : fd_memcpy( data->bytes, fd_account_data( meta ), meta->dlen );
1093 0 : out_acct->data = data;
1094 0 : }
1095 :
1096 0 : out_acct->executable = meta->executable;
1097 0 : memcpy( out_acct->owner, meta->owner, sizeof(fd_pubkey_t) );
1098 0 : }
1099 :
1100 : ulong
1101 : create_txn_result_protobuf_from_txn( fd_exec_test_txn_result_t ** txn_result_out,
1102 : void * out_buf,
1103 : ulong out_bufsz,
1104 : fd_txn_in_t const * txn_in,
1105 : fd_txn_out_t * txn_out,
1106 0 : int exec_res ) {
1107 0 : FD_SCRATCH_ALLOC_INIT( l, out_buf );
1108 0 : ulong out_end = (ulong)out_buf + out_bufsz;
1109 :
1110 0 : fd_exec_test_txn_result_t * txn_result =
1111 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_txn_result_t),
1112 0 : sizeof(fd_exec_test_txn_result_t) );
1113 0 : if( FD_UNLIKELY( _l > out_end ) ) abort();
1114 0 : fd_memset( txn_result, 0, sizeof(fd_exec_test_txn_result_t) );
1115 :
1116 : /* Map nonce errors into the agave expected ones. */
1117 0 : if( FD_UNLIKELY( exec_res==FD_RUNTIME_TXN_ERR_BLOCKHASH_NONCE_ALREADY_ADVANCED ||
1118 0 : exec_res==FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR ||
1119 0 : exec_res==FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_WRONG_NONCE )) {
1120 0 : exec_res = FD_RUNTIME_TXN_ERR_BLOCKHASH_NOT_FOUND;
1121 0 : }
1122 :
1123 : /* Basic result fields */
1124 0 : txn_result->executed = txn_out->err.is_committable;
1125 0 : txn_result->sanitization_error = !txn_out->err.is_committable;
1126 0 : txn_result->modified_accounts_count = 0;
1127 0 : txn_result->rollback_accounts_count = 0;
1128 0 : txn_result->is_ok = !exec_res;
1129 0 : txn_result->status = (uint32_t) -exec_res;
1130 0 : txn_result->instruction_error = 0;
1131 0 : txn_result->instruction_error_index = 0;
1132 0 : txn_result->custom_error = 0;
1133 0 : txn_result->has_fee_details = false;
1134 0 : txn_result->loaded_accounts_data_size = txn_out->details.loaded_accounts_data_size;
1135 :
1136 0 : if( txn_result->sanitization_error ) {
1137 0 : if( txn_out->err.is_fees_only ) {
1138 0 : txn_result->has_fee_details = true;
1139 0 : txn_result->fee_details.prioritization_fee = txn_out->details.priority_fee;
1140 0 : txn_result->fee_details.transaction_fee = txn_out->details.execution_fee;
1141 0 : }
1142 :
1143 0 : if( exec_res==FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR ) {
1144 0 : txn_result->instruction_error = (uint32_t) -txn_out->err.exec_err;
1145 0 : txn_result->instruction_error_index = (uint32_t) txn_out->err.exec_err_idx;
1146 0 : if( txn_out->err.exec_err==FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR ) {
1147 0 : txn_result->custom_error = txn_out->err.custom_err;
1148 0 : }
1149 0 : }
1150 :
1151 0 : *txn_result_out = txn_result;
1152 0 : return FD_SCRATCH_ALLOC_FINI( l, 1UL ) - (ulong)out_buf;
1153 0 : }
1154 :
1155 : /* Capture instruction error code for executed transactions */
1156 0 : if( exec_res==FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR ) {
1157 0 : fd_txn_t const * txn = TXN( txn_in->txn );
1158 0 : int instr_err_idx = txn_out->err.exec_err_idx;
1159 0 : int program_id_idx = txn->instr[instr_err_idx].program_id;
1160 :
1161 0 : txn_result->instruction_error = (uint32_t) -txn_out->err.exec_err;
1162 0 : txn_result->instruction_error_index = (uint32_t) instr_err_idx;
1163 :
1164 0 : if( txn_out->err.exec_err==FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR &&
1165 0 : fd_executor_lookup_native_precompile_program( &txn_out->accounts.keys[ program_id_idx ] )==NULL ) {
1166 0 : txn_result->custom_error = txn_out->err.custom_err;
1167 0 : }
1168 0 : }
1169 :
1170 0 : txn_result->has_fee_details = true;
1171 0 : txn_result->fee_details.transaction_fee = txn_out->details.execution_fee;
1172 0 : txn_result->fee_details.prioritization_fee = txn_out->details.priority_fee;
1173 0 : txn_result->executed_units = txn_out->details.compute_budget.compute_unit_limit - txn_out->details.compute_budget.compute_meter;
1174 :
1175 : /* Return data */
1176 0 : if( txn_out->details.return_data.len>0 ) {
1177 0 : txn_result->return_data = FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
1178 0 : PB_BYTES_ARRAY_T_ALLOCSIZE( txn_out->details.return_data.len ) );
1179 0 : if( FD_UNLIKELY( _l > out_end ) ) abort();
1180 0 : txn_result->return_data->size = (pb_size_t)txn_out->details.return_data.len;
1181 0 : fd_memcpy( txn_result->return_data->bytes, txn_out->details.return_data.data, txn_out->details.return_data.len );
1182 0 : }
1183 :
1184 : /* Modified accounts */
1185 0 : txn_result->modified_accounts = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_acct_state_t), sizeof(fd_exec_test_acct_state_t) * txn_out->accounts.cnt );
1186 0 : txn_result->rollback_accounts = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_acct_state_t), sizeof(fd_exec_test_acct_state_t) * 2UL );
1187 0 : if( FD_UNLIKELY( _l > out_end ) ) abort();
1188 :
1189 0 : if( txn_out->err.is_fees_only || exec_res!=FD_RUNTIME_EXECUTE_SUCCESS ) {
1190 : /* If the transaction errored, capture the rollback accounts (fee payer and nonce). */
1191 0 : if( FD_LIKELY( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
1192 0 : write_account_to_result(
1193 0 : &txn_out->accounts.keys[FD_FEE_PAYER_TXN_IDX],
1194 0 : txn_out->accounts.rollback_fee_payer,
1195 0 : txn_result->rollback_accounts,
1196 0 : &txn_result->rollback_accounts_count,
1197 0 : &_l,
1198 0 : out_end
1199 0 : );
1200 0 : }
1201 :
1202 0 : if( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) {
1203 0 : write_account_to_result(
1204 0 : &txn_out->accounts.keys[txn_out->accounts.nonce_idx_in_txn],
1205 0 : txn_out->accounts.rollback_nonce,
1206 0 : txn_result->rollback_accounts,
1207 0 : &txn_result->rollback_accounts_count,
1208 0 : &_l,
1209 0 : out_end
1210 0 : );
1211 0 : }
1212 0 : }
1213 :
1214 0 : if( !txn_out->err.is_fees_only ) {
1215 : /* Executed: capture fee payer and writable accounts. */
1216 0 : for( ulong j=0UL; j<txn_out->accounts.cnt; j++ ) {
1217 0 : if( !( fd_runtime_account_is_writable_idx( txn_in, txn_out, (ushort)j ) ||
1218 0 : j==FD_FEE_PAYER_TXN_IDX ) ) {
1219 0 : continue;
1220 0 : }
1221 :
1222 0 : write_account_to_result(
1223 0 : &txn_out->accounts.keys[j],
1224 0 : txn_out->accounts.account[j].meta,
1225 0 : txn_result->modified_accounts,
1226 0 : &txn_result->modified_accounts_count,
1227 0 : &_l,
1228 0 : out_end
1229 0 : );
1230 0 : }
1231 0 : }
1232 :
1233 0 : *txn_result_out = txn_result;
1234 0 : return FD_SCRATCH_ALLOC_FINI( l, 1UL ) - (ulong)out_buf;
1235 0 : }
1236 :
1237 : void
1238 : fd_dump_txn_to_protobuf( fd_runtime_t * runtime,
1239 : fd_bank_t * bank,
1240 : fd_txn_in_t const * txn_in,
1241 0 : fd_txn_out_t * txn_out ) {
1242 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( runtime->log.dumping_mem, 1UL<<28UL ) );
1243 :
1244 0 : FD_SPAD_FRAME_BEGIN( spad ) {
1245 : // Get base58-encoded tx signature
1246 0 : const fd_ed25519_sig_t * signatures = fd_txn_get_signatures( TXN( txn_in->txn ), txn_in->txn->payload );
1247 0 : char encoded_signature[FD_BASE58_ENCODED_64_SZ];
1248 0 : fd_base58_encode_64( signatures[0], NULL, encoded_signature );
1249 :
1250 0 : fd_exec_test_txn_context_t txn_context_msg = FD_EXEC_TEST_TXN_CONTEXT_INIT_DEFAULT;
1251 0 : create_txn_context_protobuf_from_txn( &txn_context_msg, runtime, bank, txn_in, txn_out, spad );
1252 :
1253 : /* Output to file */
1254 0 : ulong out_buf_size = 100UL<<20UL; // 100 MB
1255 0 : uchar * out = fd_spad_alloc( spad, alignof(uchar), out_buf_size );
1256 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1257 0 : if( pb_encode( &stream, FD_EXEC_TEST_TXN_CONTEXT_FIELDS, &txn_context_msg ) ) {
1258 0 : char output_filepath[ PATH_MAX ];
1259 0 : snprintf( output_filepath, PATH_MAX, "%s/txn-%s.txnctx", runtime->log.dump_proto_ctx->dump_proto_output_dir, encoded_signature );
1260 0 : FILE * file = fopen(output_filepath, "wb");
1261 0 : if( file ) {
1262 0 : fwrite( out, 1, stream.bytes_written, file );
1263 0 : fclose( file );
1264 0 : }
1265 0 : }
1266 0 : } FD_SPAD_FRAME_END;
1267 0 : }
1268 :
1269 : void
1270 : fd_dump_txn_context_to_protobuf( fd_txn_dump_ctx_t * txn_dump_ctx,
1271 : fd_runtime_t * runtime,
1272 : fd_bank_t * bank,
1273 : fd_txn_in_t const * txn_in,
1274 0 : fd_txn_out_t * txn_out ) {
1275 0 : fd_txn_dump_context_reset( txn_dump_ctx );
1276 :
1277 0 : txn_dump_ctx->fixture.has_metadata = true;
1278 0 : strncpy(
1279 0 : txn_dump_ctx->fixture.metadata.fn_entrypoint,
1280 0 : "sol_compat_txn_execute_v1",
1281 0 : sizeof(txn_dump_ctx->fixture.metadata.fn_entrypoint)-1UL
1282 0 : );
1283 :
1284 0 : txn_dump_ctx->fixture.has_input = true;
1285 0 : create_txn_context_protobuf_from_txn( &txn_dump_ctx->fixture.input,
1286 0 : runtime, bank, txn_in, txn_out,
1287 0 : txn_dump_ctx->spad );
1288 0 : }
1289 :
1290 : void
1291 : fd_dump_txn_result_to_protobuf( fd_txn_dump_ctx_t * txn_dump_ctx,
1292 : fd_txn_in_t const * txn_in,
1293 : fd_txn_out_t * txn_out,
1294 0 : int exec_res ) {
1295 0 : txn_dump_ctx->fixture.has_output = true;
1296 :
1297 0 : ulong buf_sz = 100UL<<20UL;
1298 0 : void * buf = fd_spad_alloc( txn_dump_ctx->spad, alignof(fd_exec_test_txn_result_t), buf_sz );
1299 0 : fd_exec_test_txn_result_t * result = NULL;
1300 0 : create_txn_result_protobuf_from_txn( &result, buf, buf_sz, txn_in, txn_out, exec_res );
1301 0 : txn_dump_ctx->fixture.output = *result;
1302 0 : }
1303 :
1304 : void
1305 : fd_dump_txn_fixture_to_file( fd_txn_dump_ctx_t * txn_dump_ctx,
1306 : fd_dump_proto_ctx_t const * dump_proto_ctx,
1307 0 : fd_txn_in_t const * txn_in ) {
1308 0 : const fd_ed25519_sig_t * signatures = fd_txn_get_signatures( TXN( txn_in->txn ), txn_in->txn->payload );
1309 0 : char encoded_signature[FD_BASE58_ENCODED_64_SZ];
1310 0 : fd_base58_encode_64( signatures[0], NULL, encoded_signature );
1311 :
1312 0 : FD_SPAD_FRAME_BEGIN( txn_dump_ctx->spad ) {
1313 0 : ulong out_buf_size = 100UL<<20UL;
1314 0 : uchar * out = fd_spad_alloc( txn_dump_ctx->spad, alignof(uchar), out_buf_size );
1315 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1316 :
1317 0 : char output_filepath[ PATH_MAX ];
1318 :
1319 0 : if( dump_proto_ctx->dump_txn_as_fixture ) {
1320 0 : if( pb_encode( &stream, FD_EXEC_TEST_TXN_FIXTURE_FIELDS, &txn_dump_ctx->fixture ) ) {
1321 0 : snprintf( output_filepath, PATH_MAX, "%s/txn-%s.fix", dump_proto_ctx->dump_proto_output_dir, encoded_signature );
1322 0 : FILE * file = fopen( output_filepath, "wb" );
1323 0 : if( file ) {
1324 0 : fwrite( out, 1, stream.bytes_written, file );
1325 0 : fclose( file );
1326 0 : }
1327 0 : }
1328 0 : } else {
1329 0 : if( pb_encode( &stream, FD_EXEC_TEST_TXN_CONTEXT_FIELDS, &txn_dump_ctx->fixture.input ) ) {
1330 0 : snprintf( output_filepath, PATH_MAX, "%s/txn-%s.txnctx", dump_proto_ctx->dump_proto_output_dir, encoded_signature );
1331 0 : FILE * file = fopen( output_filepath, "wb" );
1332 0 : if( file ) {
1333 0 : fwrite( out, 1, stream.bytes_written, file );
1334 0 : fclose( file );
1335 0 : }
1336 0 : }
1337 0 : }
1338 0 : } FD_SPAD_FRAME_END;
1339 0 : }
1340 :
1341 : void
1342 : fd_dump_block_to_protobuf_collect_tx( fd_block_dump_ctx_t * dump_block_ctx,
1343 0 : fd_txn_p_t const * txn ) {
1344 0 : if( FD_UNLIKELY( dump_block_ctx->txns_to_dump_cnt>=FD_BLOCK_DUMP_CTX_MAX_TXN_CNT ) ) {
1345 0 : FD_LOG_ERR(( "Please increase FD_BLOCK_DUMP_CTX_MAX_TXN_CNT to dump more than %lu transactions.", FD_BLOCK_DUMP_CTX_MAX_TXN_CNT ));
1346 0 : return;
1347 0 : }
1348 0 : fd_memcpy( &dump_block_ctx->txns_to_dump[dump_block_ctx->txns_to_dump_cnt++], txn, sizeof(fd_txn_p_t) );
1349 0 : }
1350 :
1351 : void
1352 : fd_dump_block_to_protobuf( fd_block_dump_ctx_t * dump_block_ctx,
1353 : fd_banks_t * banks,
1354 : fd_bank_t * bank,
1355 : fd_accdb_user_t * accdb,
1356 : fd_dump_proto_ctx_t const * dump_proto_ctx,
1357 0 : fd_runtime_stack_t * runtime_stack ) {
1358 0 : if( FD_UNLIKELY( dump_block_ctx==NULL ) ) {
1359 0 : FD_LOG_WARNING(( "Block dumping context may not be NULL when dumping blocks." ));
1360 0 : return;
1361 0 : }
1362 :
1363 0 : FD_SPAD_FRAME_BEGIN( dump_block_ctx->spad ) {
1364 0 : if( FD_UNLIKELY( dump_proto_ctx==NULL ) ) {
1365 0 : FD_LOG_WARNING(( "Protobuf dumping context may not be NULL when dumping blocks." ));
1366 0 : return;
1367 0 : }
1368 :
1369 : /* Dump the block context */
1370 0 : create_block_context_protobuf_from_block( dump_block_ctx, banks, bank, accdb, runtime_stack );
1371 :
1372 : /* Output to file */
1373 0 : ulong out_buf_size = 1UL<<30UL; /* 1 GB */
1374 0 : uint8_t * out = fd_spad_alloc( dump_block_ctx->spad, alignof(uint8_t), out_buf_size );
1375 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1376 0 : if( pb_encode( &stream, FD_EXEC_TEST_BLOCK_CONTEXT_FIELDS, &dump_block_ctx->block_context ) ) {
1377 0 : char output_filepath[ PATH_MAX ];
1378 0 : snprintf( output_filepath, PATH_MAX, "%s/block-%lu.blockctx", dump_proto_ctx->dump_proto_output_dir, bank->f.slot );
1379 0 : FILE * file = fopen(output_filepath, "wb");
1380 0 : if( file ) {
1381 0 : fwrite( out, 1, stream.bytes_written, file );
1382 0 : fclose( file );
1383 0 : }
1384 0 : }
1385 0 : } FD_SPAD_FRAME_END;
1386 0 : }
1387 :
1388 : void
1389 : fd_dump_vm_syscall_to_protobuf( fd_vm_t const * vm,
1390 0 : char const * fn_name ) {
1391 0 : char const * syscall_name_filter = vm->instr_ctx->runtime->log.dump_proto_ctx->dump_syscall_name_filter;
1392 0 : if( syscall_name_filter && strlen( syscall_name_filter ) && strcmp( syscall_name_filter, fn_name ) ) {
1393 0 : return;
1394 0 : }
1395 :
1396 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( vm->instr_ctx->runtime->log.dumping_mem, 1UL<<28UL ) );
1397 :
1398 0 : FD_SPAD_FRAME_BEGIN( spad ) {
1399 :
1400 0 : fd_ed25519_sig_t signature;
1401 0 : memcpy( signature, (uchar const *)vm->instr_ctx->txn_in->txn->payload + TXN( vm->instr_ctx->txn_in->txn )->signature_off, sizeof(fd_ed25519_sig_t) );
1402 0 : char encoded_signature[FD_BASE58_ENCODED_64_SZ];
1403 0 : fd_base58_encode_64( signature, NULL, encoded_signature );
1404 :
1405 0 : char filename[ PATH_MAX ];
1406 0 : snprintf( filename,
1407 0 : PATH_MAX,
1408 0 : "%s/syscall-%s-%s-%d-%hhu-%lu.sysctx",
1409 0 : vm->instr_ctx->runtime->log.dump_proto_ctx->dump_proto_output_dir,
1410 0 : fn_name,
1411 0 : encoded_signature,
1412 0 : vm->instr_ctx->runtime->instr.current_idx,
1413 0 : vm->instr_ctx->runtime->instr.stack_sz,
1414 0 : vm->cu );
1415 :
1416 : /* The generated filename should be unique for every call. Silently return otherwise. */
1417 0 : if( FD_UNLIKELY( access( filename, F_OK )!=-1 ) ) {
1418 0 : return;
1419 0 : }
1420 :
1421 0 : fd_exec_test_syscall_context_t sys_ctx = FD_EXEC_TEST_SYSCALL_CONTEXT_INIT_ZERO;
1422 :
1423 : /* SyscallContext -> vm_ctx */
1424 0 : sys_ctx.has_vm_ctx = 1;
1425 :
1426 : /* SyscallContext -> vm_ctx -> heap_max */
1427 0 : sys_ctx.vm_ctx.heap_max = vm->heap_max; /* should be equiv. to txn_ctx->heap_sz */
1428 :
1429 : /* SyscallContext -> vm_ctx -> rodata */
1430 0 : sys_ctx.vm_ctx.rodata = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->rodata_sz ) );
1431 0 : sys_ctx.vm_ctx.rodata->size = (pb_size_t) vm->rodata_sz;
1432 0 : fd_memcpy( sys_ctx.vm_ctx.rodata->bytes, vm->rodata, vm->rodata_sz );
1433 :
1434 : /* SyscallContext -> vm_ctx -> r0-11 */
1435 0 : sys_ctx.vm_ctx.r0 = vm->reg[0];
1436 0 : sys_ctx.vm_ctx.r1 = vm->reg[1];
1437 0 : sys_ctx.vm_ctx.r2 = vm->reg[2];
1438 0 : sys_ctx.vm_ctx.r3 = vm->reg[3];
1439 0 : sys_ctx.vm_ctx.r4 = vm->reg[4];
1440 0 : sys_ctx.vm_ctx.r5 = vm->reg[5];
1441 0 : sys_ctx.vm_ctx.r6 = vm->reg[6];
1442 0 : sys_ctx.vm_ctx.r7 = vm->reg[7];
1443 0 : sys_ctx.vm_ctx.r8 = vm->reg[8];
1444 0 : sys_ctx.vm_ctx.r9 = vm->reg[9];
1445 0 : sys_ctx.vm_ctx.r10 = vm->reg[10];
1446 0 : sys_ctx.vm_ctx.r11 = vm->reg[11];
1447 :
1448 : /* SyscallContext -> vm_ctx -> entry_pc */
1449 0 : sys_ctx.vm_ctx.entry_pc = vm->entry_pc;
1450 :
1451 : /* SyscallContext -> vm_ctx -> return_data */
1452 0 : sys_ctx.vm_ctx.has_return_data = 1;
1453 :
1454 : /* SyscallContext -> vm_ctx -> return_data -> data */
1455 0 : sys_ctx.vm_ctx.return_data.data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->instr_ctx->txn_out->details.return_data.len ) );
1456 0 : sys_ctx.vm_ctx.return_data.data->size = (pb_size_t)vm->instr_ctx->txn_out->details.return_data.len;
1457 0 : fd_memcpy( sys_ctx.vm_ctx.return_data.data->bytes, vm->instr_ctx->txn_out->details.return_data.data, vm->instr_ctx->txn_out->details.return_data.len );
1458 :
1459 : /* SyscallContext -> vm_ctx -> return_data -> program_id */
1460 0 : sys_ctx.vm_ctx.return_data.program_id = fd_spad_alloc( spad, alignof(pb_bytes_array_t), sizeof(fd_pubkey_t) );
1461 0 : sys_ctx.vm_ctx.return_data.program_id->size = sizeof(fd_pubkey_t);
1462 0 : fd_memcpy( sys_ctx.vm_ctx.return_data.program_id->bytes, vm->instr_ctx->txn_out->details.return_data.program_id.key, sizeof(fd_pubkey_t) );
1463 :
1464 : /* SyscallContext -> vm_ctx -> sbpf_version */
1465 0 : sys_ctx.vm_ctx.sbpf_version = (uint)vm->sbpf_version;
1466 :
1467 : /* SyscallContext -> instr_ctx */
1468 0 : sys_ctx.has_instr_ctx = 1;
1469 0 : create_instr_context_protobuf_from_instructions( &sys_ctx.instr_ctx,
1470 0 : vm->instr_ctx->runtime,
1471 0 : vm->instr_ctx->bank,
1472 0 : vm->instr_ctx->txn_out,
1473 0 : vm->instr_ctx->instr,
1474 0 : spad );
1475 :
1476 : /* SyscallContext -> syscall_invocation */
1477 0 : sys_ctx.has_syscall_invocation = 1;
1478 :
1479 : /* SyscallContext -> syscall_invocation -> function_name */
1480 0 : sys_ctx.syscall_invocation.function_name.size = fd_uint_min( (uint) strlen(fn_name), sizeof(sys_ctx.syscall_invocation.function_name.bytes) );
1481 0 : fd_memcpy( sys_ctx.syscall_invocation.function_name.bytes,
1482 0 : fn_name,
1483 0 : sys_ctx.syscall_invocation.function_name.size );
1484 :
1485 : /* SyscallContext -> syscall_invocation -> heap_prefix */
1486 0 : sys_ctx.syscall_invocation.heap_prefix = fd_spad_alloc( spad, 8UL, PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
1487 0 : sys_ctx.syscall_invocation.heap_prefix->size = (pb_size_t) vm->instr_ctx->txn_out->details.compute_budget.heap_size;
1488 0 : fd_memcpy( sys_ctx.syscall_invocation.heap_prefix->bytes, vm->heap, vm->instr_ctx->txn_out->details.compute_budget.heap_size );
1489 :
1490 : /* SyscallContext -> syscall_invocation -> stack_prefix */
1491 0 : pb_size_t stack_sz = (pb_size_t)FD_VM_STACK_MAX;
1492 0 : sys_ctx.syscall_invocation.stack_prefix = fd_spad_alloc( spad, 8UL, PB_BYTES_ARRAY_T_ALLOCSIZE( stack_sz ) );
1493 0 : sys_ctx.syscall_invocation.stack_prefix->size = stack_sz;
1494 0 : fd_memcpy( sys_ctx.syscall_invocation.stack_prefix->bytes, vm->stack, stack_sz );
1495 :
1496 : /* Output to file */
1497 0 : ulong out_buf_size = 1UL<<29UL; /* 128 MB */
1498 0 : uint8_t * out = fd_spad_alloc( spad, alignof(uint8_t), out_buf_size );
1499 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1500 0 : if( pb_encode( &stream, FD_EXEC_TEST_SYSCALL_CONTEXT_FIELDS, &sys_ctx ) ) {
1501 0 : FILE * file = fopen(filename, "wb");
1502 0 : if( file ) {
1503 0 : fwrite( out, 1, stream.bytes_written, file );
1504 0 : fclose( file );
1505 0 : }
1506 0 : }
1507 0 : } FD_SPAD_FRAME_END;
1508 0 : }
|