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