Line data Source code
1 : #include "fd_solfuzz_private.h"
2 : #include "../fd_cost_tracker.h"
3 : #include "fd_txn_harness.h"
4 : #include "../fd_runtime.h"
5 : #include "../fd_system_ids.h"
6 : #include "../fd_runtime_stack.h"
7 : #include "../../stakes/fd_stake_types.h"
8 : #include "../sysvar/fd_sysvar_epoch_schedule.h"
9 : #include "../sysvar/fd_sysvar_cache.h"
10 : #include "../../accdb/fd_accdb_admin_v1.h"
11 : #include "../../accdb/fd_accdb_impl_v1.h"
12 : #include "../../accdb/fd_accdb_sync.h"
13 : #include "../../progcache/fd_progcache_admin.h"
14 : #include "../../log_collector/fd_log_collector.h"
15 : #include "../../rewards/fd_rewards.h"
16 : #include "generated/block.pb.h"
17 : #include "../../capture/fd_capture_ctx.h"
18 : #include "../../capture/fd_solcap_writer.h"
19 :
20 : /* Templatized leader schedule sort helper functions */
21 : typedef struct {
22 : fd_pubkey_t pk;
23 : ulong sched_pos; /* track original position in sched[] */
24 : } pk_with_pos_t;
25 :
26 : #define SORT_NAME sort_pkpos
27 0 : #define SORT_KEY_T pk_with_pos_t
28 0 : #define SORT_BEFORE(a,b) (memcmp(&(a).pk, &(b).pk, sizeof(fd_pubkey_t))<0)
29 : #include "../../../util/tmpl/fd_sort.c" /* generates templatized sort_pkpos_*() APIs */
30 :
31 : /* Fixed leader schedule hash seed (consistent with solfuzz-agave) */
32 0 : #define LEADER_SCHEDULE_HASH_SEED 0xDEADFACEUL
33 :
34 : static void
35 : fd_solfuzz_block_update_prev_epoch_stakes( fd_bank_t const * bank,
36 : fd_top_votes_t * top_votes,
37 : fd_vote_stakes_t * vote_stakes,
38 : fd_exec_test_prev_vote_account_t * vote_accounts,
39 : pb_size_t vote_accounts_cnt,
40 0 : uchar is_t_1 ) {
41 0 : if( FD_UNLIKELY( !vote_accounts ) ) return;
42 :
43 0 : for( uint i=0U; i<vote_accounts_cnt; i++ ) {
44 0 : fd_pubkey_t vote_pubkey = FD_LOAD( fd_pubkey_t, &vote_accounts[i].address );
45 0 : fd_pubkey_t node_pubkey = FD_LOAD( fd_pubkey_t, &vote_accounts[i].node_pubkey );
46 0 : ulong stake = vote_accounts[i].stake;
47 :
48 0 : ushort commission;
49 0 : if( FD_FEATURE_ACTIVE_BANK( bank, commission_rate_in_basis_points ) && vote_accounts[i].version == FD_EXEC_TEST_VOTE_ACCOUNT_VERSION_V4 ) {
50 0 : commission = (ushort)vote_accounts[i].commission_bps;
51 0 : } else if( vote_accounts[i].version == FD_EXEC_TEST_VOTE_ACCOUNT_VERSION_V4 ) {
52 0 : commission = (ushort)( ( vote_accounts[i].commission_bps / 100U ) * 100U );
53 0 : } else {
54 0 : commission = (ushort)( (uchar)( vote_accounts[i].commission_bps / 100U ) * 100U );
55 0 : }
56 :
57 0 : if( is_t_1 ) {
58 0 : fd_vote_stakes_root_insert_key( vote_stakes, &vote_pubkey, &node_pubkey, stake, commission, 0 );
59 0 : } else {
60 0 : fd_vote_stakes_root_update_meta( vote_stakes, &vote_pubkey, &node_pubkey, stake, commission, 0 );
61 0 : }
62 0 : fd_top_votes_insert( top_votes, &vote_pubkey, &node_pubkey, stake, commission );
63 0 : }
64 0 : }
65 :
66 : /* Stores an entry in the stake delegations cache for the given vote
67 : account. Deserializes and uses the present account state to derive
68 : delegation information. */
69 : static void
70 : fd_solfuzz_block_register_stake_delegation( fd_accdb_user_t * accdb,
71 : fd_funk_txn_xid_t const * xid,
72 : fd_stake_delegations_t * stake_delegations,
73 0 : fd_pubkey_t * pubkey ) {
74 0 : fd_accdb_ro_t ro[1];
75 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, ro, xid, pubkey ) ) ) return;
76 :
77 0 : fd_stake_state_t const * stake_state = NULL;
78 0 : if( !fd_pubkey_eq( fd_accdb_ref_owner( ro ), &fd_solana_stake_program_id ) ||
79 0 : fd_accdb_ref_lamports( ro )==0UL ||
80 0 : !( stake_state = fd_stake_state_view( fd_accdb_ref_data_const( ro ), fd_accdb_ref_data_sz( ro ) ) ) ||
81 0 : stake_state->stake_type!=FD_STAKE_STATE_STAKE ||
82 0 : stake_state->stake.stake.delegation.stake==0UL ) {
83 0 : fd_accdb_close_ro( accdb, ro );
84 0 : return;
85 0 : }
86 :
87 0 : fd_stake_delegations_root_update(
88 0 : stake_delegations,
89 0 : pubkey,
90 0 : &stake_state->stake.stake.delegation.voter_pubkey,
91 0 : stake_state->stake.stake.delegation.stake,
92 0 : stake_state->stake.stake.delegation.activation_epoch,
93 0 : stake_state->stake.stake.delegation.deactivation_epoch,
94 0 : stake_state->stake.stake.credits_observed,
95 0 : FD_STAKE_DELEGATIONS_WARMUP_COOLDOWN_RATE_ENUM_025 );
96 0 : fd_accdb_close_ro( accdb, ro );
97 0 : }
98 :
99 : static void
100 0 : fd_solfuzz_pb_block_ctx_destroy( fd_solfuzz_runner_t * runner ) {
101 0 : fd_banks_stake_delegations_evict_bank_fork( runner->banks, runner->bank );
102 :
103 0 : runner->bank->stake_rewards_fork_id = UCHAR_MAX;
104 :
105 0 : fd_accdb_v1_clear( runner->accdb_admin );
106 0 : fd_progcache_reset( runner->progcache->join );
107 :
108 : /* In order to check for leaks in the workspace, we need to compact the
109 : allocators. Without doing this, empty superblocks may be retained
110 : by the fd_alloc instance, which mean we cannot check for leaks. */
111 0 : fd_alloc_compact( fd_accdb_user_v1_funk( runner->accdb )->alloc );
112 0 : fd_alloc_compact( runner->progcache->join->alloc );
113 0 : }
114 :
115 : /* Sets up block execution context from an input test case to execute
116 : against the runtime. Returns block_info on success and NULL on
117 : failure. */
118 : static fd_txn_p_t *
119 : fd_solfuzz_pb_block_ctx_create( fd_solfuzz_runner_t * runner,
120 : fd_exec_test_block_context_t const * test_ctx,
121 : ulong * out_txn_cnt,
122 0 : fd_hash_t * poh ) {
123 0 : fd_accdb_user_t * accdb = runner->accdb;
124 0 : fd_bank_t * bank = runner->bank;
125 0 : fd_banks_t * banks = runner->banks;
126 :
127 0 : fd_runtime_stack_t * runtime_stack = runner->runtime_stack;
128 :
129 : /* Must match fd_banks_footprint max_vote_accounts (2048) to avoid buffer overrun
130 : when fd_vote_stakes_new reinitializes and epoch boundary inserts from vote_ele_map */
131 0 : fd_banks_clear_bank( banks, bank, 2048UL );
132 :
133 : /* Generate unique ID for funk txn */
134 0 : fd_funk_txn_xid_t xid[1] = {{ .ul={ 0UL, 0UL } }};
135 :
136 : /* Create temporary funk transaction and slot / epoch contexts */
137 0 : fd_funk_txn_xid_t parent_xid; fd_funk_txn_xid_set_root( &parent_xid );
138 0 : fd_accdb_attach_child( runner->accdb_admin, &parent_xid, xid );
139 0 : runner->bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, fd_progcache_fork_id_initial() );
140 :
141 : /* Initialize bank from input block bank */
142 0 : FD_TEST( test_ctx->has_bank );
143 0 : fd_exec_test_block_bank_t const * block_bank = &test_ctx->bank;
144 :
145 : /* Slot */
146 0 : ulong slot = block_bank->slot;
147 0 : bank->f.slot = slot;
148 :
149 : /* Blockhash queue */
150 0 : fd_solfuzz_pb_restore_blockhash_queue( bank, block_bank->blockhash_queue, block_bank->blockhash_queue_count );
151 :
152 : /* RBH lamports per signature. In the Agave harness this is set inside
153 : the fee rate governor itself. */
154 0 : runner->bank->f.rbh_lamports_per_sig = block_bank->rbh_lamports_per_signature;
155 :
156 : /* Fee rate governor */
157 0 : FD_TEST( block_bank->has_fee_rate_governor );
158 0 : fd_solfuzz_pb_restore_fee_rate_governor( bank, &block_bank->fee_rate_governor );
159 :
160 : /* Parent slot */
161 0 : ulong parent_slot = block_bank->parent_slot;
162 0 : bank->f.parent_slot = parent_slot;
163 :
164 : /* Capitalization */
165 0 : bank->f.capitalization = block_bank->capitalization;
166 :
167 : /* Inflation */
168 0 : FD_TEST( block_bank->has_inflation );
169 0 : fd_inflation_t inflation = {
170 0 : .initial = block_bank->inflation.initial,
171 0 : .terminal = block_bank->inflation.terminal,
172 0 : .taper = block_bank->inflation.taper,
173 0 : .foundation = block_bank->inflation.foundation,
174 0 : .foundation_term = block_bank->inflation.foundation_term,
175 0 : };
176 0 : bank->f.inflation = inflation;
177 :
178 : /* Block height */
179 0 : bank->f.block_height = block_bank->block_height;
180 :
181 : /* POH (set right before finalize since we don't fuzz POH calculation) */
182 0 : fd_memcpy( poh, block_bank->poh, sizeof(fd_hash_t) );
183 :
184 : /* Bank hash (parent bank hash because current bank hash gets computed
185 : after the block executes) */
186 0 : fd_hash_t * bank_hash = &bank->f.bank_hash;
187 0 : fd_memcpy( bank_hash, block_bank->parent_bank_hash, sizeof(fd_hash_t) );
188 :
189 : /* Previous bank hash (used as input to the bank hash computation).
190 : In production this is set by fd_banks_clone_from_parent. */
191 0 : bank->f.prev_bank_hash = *(fd_hash_t const *)block_bank->parent_bank_hash;
192 :
193 : /* Parent signature count */
194 0 : bank->f.parent_signature_cnt = block_bank->parent_signature_count;
195 :
196 : /* Epoch schedule */
197 0 : FD_TEST( block_bank->has_epoch_schedule );
198 0 : fd_solfuzz_pb_restore_epoch_schedule( bank, &block_bank->epoch_schedule );
199 :
200 : /* Feature set */
201 0 : FD_TEST( block_bank->has_features );
202 0 : fd_exec_test_feature_set_t const * feature_set = &block_bank->features;
203 0 : fd_features_t * features_bm = &bank->f.features;
204 0 : fd_solfuzz_pb_create_feature_accounts( accdb, xid, feature_set, test_ctx->acct_states, test_ctx->acct_states_count );
205 0 : FD_TEST( fd_solfuzz_pb_restore_features( features_bm, feature_set ) );
206 :
207 : /* Total epoch stake (derived from T-1 vote accounts) */
208 0 : ulong total_epoch_stake = 0UL;
209 0 : for( uint i=0U; i<block_bank->vote_accounts_t_1_count; i++ ) {
210 0 : total_epoch_stake += block_bank->vote_accounts_t_1[i].stake;
211 0 : }
212 0 : bank->f.total_epoch_stake = total_epoch_stake;
213 :
214 : /* Using default configuration of 64 ticks per slot
215 : https://github.com/anza-xyz/solana-sdk/blob/time-utils%40v3.0.0/time-utils/src/lib.rs#L18-L27 */
216 0 : uint128 ns_per_slot = FD_LOAD(uint128, block_bank->ns_per_slot );
217 0 : bank->f.ns_per_slot = (fd_w_u128_t){ .ud = ns_per_slot };
218 0 : bank->f.ticks_per_slot = 64UL;
219 0 : runner->bank->f.slots_per_year = (double)SECONDS_PER_YEAR * 1e9 / (double)ns_per_slot;
220 0 : bank->f.hashes_per_tick = (slot+1UL)*64UL;
221 :
222 : /* Load in accounts, populate stake delegations and vote accounts */
223 0 : fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
224 0 : fd_stake_delegations_reset( stake_delegations );
225 :
226 0 : bank->stake_delegations_fork_id = fd_stake_delegations_new_fork( stake_delegations );
227 :
228 0 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
229 0 : bank->vote_stakes_fork_id = fd_vote_stakes_get_root_idx( vote_stakes );
230 :
231 0 : fd_top_votes_t * top_votes_t_1 = fd_bank_top_votes_t_1_modify( bank );
232 0 : fd_top_votes_init( top_votes_t_1 );
233 :
234 0 : fd_top_votes_t * top_votes_t_2 = fd_bank_top_votes_t_2_modify( bank );
235 0 : fd_top_votes_init( top_votes_t_2 );
236 :
237 : /* Cap number of vote accounts at FD_RUNTIME_EXPECTED_VOTE_ACCOUNTS */
238 0 : FD_TEST( block_bank->vote_accounts_t_1_count<=FD_RUNTIME_EXPECTED_VOTE_ACCOUNTS );
239 0 : FD_TEST( block_bank->vote_accounts_t_2_count<=FD_RUNTIME_EXPECTED_VOTE_ACCOUNTS );
240 :
241 0 : fd_solfuzz_block_update_prev_epoch_stakes( bank, top_votes_t_1, vote_stakes, block_bank->vote_accounts_t_1, block_bank->vote_accounts_t_1_count, 1 );
242 0 : fd_solfuzz_block_update_prev_epoch_stakes( bank, top_votes_t_2, vote_stakes, block_bank->vote_accounts_t_2, block_bank->vote_accounts_t_2_count, 0 );
243 :
244 0 : for( ushort i=0; i<test_ctx->acct_states_count; i++ ) {
245 0 : fd_solfuzz_pb_load_account( runner->runtime, accdb, xid, &test_ctx->acct_states[i], i );
246 :
247 : /* Update the stake delegations cache for epoch T */
248 0 : fd_pubkey_t pubkey;
249 0 : memcpy( &pubkey, test_ctx->acct_states[i].address, sizeof(fd_pubkey_t) );
250 0 : fd_solfuzz_block_register_stake_delegation( accdb, xid, stake_delegations, &pubkey );
251 0 : }
252 :
253 : /* Refresh top votes after loading accdb. */
254 0 : fd_top_votes_refresh( top_votes_t_2, accdb, xid );
255 :
256 : /* Current epoch gets updated in process_new_epoch, so use the epoch
257 : from the parent slot */
258 0 : bank->f.epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, parent_slot, NULL );
259 :
260 : /* reduce_stake_warmup_cooldown is activated on all clusters, so the
261 : new warmup/cooldown rate (0.09) applies from epoch 0 onwards. */
262 0 : bank->f.warmup_cooldown_rate_epoch = 0UL;
263 :
264 : /* Restore sysvar cache */
265 0 : fd_sysvar_cache_restore_fuzz( bank, accdb, xid );
266 :
267 : /* Initialize total_effective/activating/deactivating_stake from the
268 : loaded stake delegations. These are read by fd_stakes_activate_epoch
269 : at epoch boundary instead of re-scanning all delegations. */
270 0 : fd_stake_history_t stake_history[1];
271 0 : if( FD_UNLIKELY( !fd_sysvar_cache_stake_history_view( &bank->f.sysvar_cache, stake_history ) ) ) {
272 0 : FD_LOG_ERR(( "StakeHistory sysvar missing or invalid" ));
273 0 : }
274 0 : fd_stake_delegations_refresh( stake_delegations, bank->f.epoch, stake_history, &bank->f.warmup_cooldown_rate_epoch, accdb, xid );
275 :
276 : /* Finalize root fork. Required before epoch boundary processing which
277 : may call fd_vote_stakes_advance_root. See fd_vote_stakes.h. */
278 :
279 0 : ulong chain_cnt = fd_vote_rewards_map_chain_cnt_est( runtime_stack->expected_vote_accounts );
280 0 : FD_TEST( fd_vote_rewards_map_join( fd_vote_rewards_map_new( runtime_stack->stakes.vote_map, chain_cnt, 999 ) ) );
281 :
282 : /* Use epoch_credits from the proto if available (captured at epoch
283 : boundary time), otherwise fall back to the vote account in funk. */
284 0 : for( uint i=0U; i<block_bank->vote_accounts_t_1_count; i++ ) {
285 0 : fd_exec_test_prev_vote_account_t const * prev_vote_accs = &block_bank->vote_accounts_t_1[i];
286 :
287 0 : FD_TEST( prev_vote_accs->epoch_credits_count<=FD_EPOCH_CREDITS_MAX );
288 0 : fd_epoch_credits_t * ec = &fd_bank_epoch_credits( bank )[i];
289 0 : fd_memcpy( ec->pubkey, prev_vote_accs->address, sizeof(fd_pubkey_t) );
290 0 : ec->cnt = prev_vote_accs->epoch_credits_count;
291 0 : ec->base_credits = ec->cnt > 0UL ? prev_vote_accs->epoch_credits[0].prev_credits : 0UL;
292 0 : for( ulong j=0UL; j<prev_vote_accs->epoch_credits_count; j++ ) {
293 0 : ec->epoch[j] = (ushort)prev_vote_accs->epoch_credits[j].epoch;
294 0 : ec->credits_delta[j] = (uint)( prev_vote_accs->epoch_credits[j].credits - ec->base_credits );
295 0 : ec->prev_credits_delta[j] = (uint)( prev_vote_accs->epoch_credits[j].prev_credits - ec->base_credits );
296 0 : }
297 0 : }
298 0 : *fd_bank_epoch_credits_len( bank ) = block_bank->vote_accounts_t_1_count;
299 :
300 : /* Update leader schedule */
301 0 : fd_runtime_update_leaders( bank, runtime_stack );
302 :
303 : /* Make a new funk transaction since we're done loading in accounts for context */
304 0 : fd_funk_txn_xid_t fork_xid = fd_bank_xid( bank );
305 0 : fd_accdb_attach_child( runner->accdb_admin, xid, &fork_xid );
306 0 : bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, bank->progcache_fork_id );
307 0 : xid[0] = fork_xid;
308 :
309 : /* Set the initial lthash from the input since we're in a new Funk txn */
310 0 : fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
311 0 : fd_memcpy( lthash, block_bank->parent_lt_hash, sizeof(fd_lthash_value_t) );
312 0 : fd_bank_lthash_end_locking_modify( bank );
313 :
314 : /* Rent */
315 0 : FD_TEST( fd_sysvar_cache_rent_read( &runner->bank->f.sysvar_cache, &runner->bank->f.rent ) );
316 :
317 : /* Prepare raw transaction pointers and block / microblock infos */
318 0 : ulong txn_cnt = test_ctx->txns_count;
319 0 : fd_txn_p_t * txn_ptrs = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), txn_cnt * sizeof(fd_txn_p_t) );
320 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
321 0 : fd_txn_p_t * txn = &txn_ptrs[i];
322 0 : ulong msg_sz = fd_solfuzz_pb_txn_serialize( txn->payload, &test_ctx->txns[i] );
323 :
324 : // Reject any transactions over 1232 bytes
325 0 : if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) {
326 0 : return NULL;
327 0 : }
328 0 : txn->payload_sz = msg_sz;
329 :
330 : // Reject any transactions that cannot be parsed
331 0 : if( FD_UNLIKELY( !fd_txn_parse( txn->payload, msg_sz, TXN( txn ), NULL ) ) ) {
332 0 : return NULL;
333 0 : }
334 0 : }
335 :
336 0 : *out_txn_cnt = txn_cnt;
337 0 : return txn_ptrs;
338 0 : }
339 :
340 : /* Takes in a list of txn_p_t created from
341 : fd_runtime_fuzz_block_ctx_create and executes it against the runtime.
342 : Returns the execution result. */
343 : static int
344 : fd_solfuzz_block_ctx_exec( fd_solfuzz_runner_t * runner,
345 : fd_txn_p_t * txn_ptrs,
346 : ulong txn_cnt,
347 0 : fd_hash_t * poh ) {
348 0 : int res = 0;
349 :
350 : // Prepare. Execute. Finalize.
351 0 : FD_SPAD_FRAME_BEGIN( runner->spad ) {
352 0 : fd_capture_ctx_t * capture_ctx = NULL;
353 :
354 0 : if( runner->solcap ) {
355 0 : void * capture_ctx_mem = fd_spad_alloc( runner->spad, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
356 0 : capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( capture_ctx_mem ) );
357 0 : if( FD_UNLIKELY( !capture_ctx ) ) {
358 0 : FD_LOG_ERR(( "Failed to initialize capture_ctx" ));
359 0 : }
360 :
361 0 : fd_capture_link_file_t * capture_link_file =
362 0 : fd_spad_alloc( runner->spad, alignof(fd_capture_link_file_t), sizeof(fd_capture_link_file_t) );
363 0 : if( FD_UNLIKELY( !capture_link_file ) ) {
364 0 : FD_LOG_ERR(( "Failed to allocate capture_link_file" ));
365 0 : }
366 :
367 0 : capture_link_file->base.vt = &fd_capture_link_file_vt;
368 :
369 0 : int solcap_fd = (int)(ulong)runner->solcap_file;
370 0 : capture_link_file->fd = solcap_fd;
371 0 : capture_ctx->capture_link = &capture_link_file->base;
372 0 : capture_ctx->capctx_type.file = capture_link_file;
373 0 : capture_ctx->solcap_start_slot = runner->bank->f.slot;
374 0 : capture_ctx->capture_solcap = 1;
375 :
376 0 : fd_solcap_writer_init( capture_ctx->capture, solcap_fd );
377 0 : }
378 :
379 : /* TODO: Make sure this is able to work with booting up inside
380 : the partitioned epoch rewards distribution phase. */
381 0 : fd_funk_txn_xid_t xid = fd_bank_xid( runner->bank );
382 0 : fd_rewards_recalculate_partitioned_rewards( runner->banks, runner->bank, runner->accdb, &xid, runner->runtime_stack, capture_ctx );
383 :
384 : /* Process new epoch may push a new spad frame onto the runtime spad. We should make sure this frame gets
385 : cleared (if it was allocated) before executing the block. */
386 0 : int is_epoch_boundary = 0;
387 0 : fd_runtime_block_execute_prepare( runner->banks, runner->bank, runner->accdb, runner->runtime_stack, capture_ctx, &is_epoch_boundary );
388 :
389 : /* Sequential transaction execution. Continue processing
390 : transactions even if a prior one was uncommitable. */
391 0 : int has_err = 0;
392 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
393 0 : fd_txn_p_t * txn = &txn_ptrs[i];
394 :
395 : /* Execute the transaction against the runtime */
396 0 : res = FD_RUNTIME_EXECUTE_SUCCESS;
397 0 : fd_txn_in_t txn_in = { .txn = txn, .bundle.is_bundle = 0 };
398 0 : fd_txn_out_t txn_out;
399 0 : fd_runtime_t * runtime = runner->runtime;
400 0 : fd_log_collector_t log[1];
401 0 : runtime->log.log_collector = log;
402 0 : runtime->acc_pool = runner->acc_pool;
403 0 : fd_solfuzz_txn_ctx_exec( runner, runtime, &txn_in, &res, &txn_out, 1 );
404 0 : txn_out.err.exec_err = res;
405 :
406 0 : if( FD_UNLIKELY( !txn_out.err.is_committable ) ) {
407 0 : fd_runtime_cancel_txn( runtime, &txn_out );
408 0 : has_err = 1;
409 0 : continue;
410 0 : }
411 :
412 : /* Finalize the transaction */
413 0 : fd_runtime_commit_txn( runtime, runner->bank, &txn_out );
414 :
415 0 : if( FD_UNLIKELY( !txn_out.err.is_committable ) ) {
416 0 : has_err = 1;
417 0 : continue;
418 0 : }
419 0 : }
420 :
421 : /* At this point we want to set the poh. This is what will get
422 : updated in the blockhash queue. */
423 0 : runner->bank->f.poh = *poh;
424 : /* Finalize the block */
425 0 : fd_runtime_block_execute_finalize( runner->bank, runner->accdb, capture_ctx );
426 :
427 0 : return !has_err;
428 0 : } FD_SPAD_FRAME_END;
429 0 : }
430 :
431 : /* Canonical (Agave-aligned) schedule hash
432 : Unique pubkeys referenced by sched, sorted deterministically
433 : Per-rotation indices mapped into sorted-uniq array */
434 : ulong
435 : fd_solfuzz_block_hash_epoch_leaders( fd_solfuzz_runner_t * runner,
436 : fd_epoch_leaders_t const * leaders,
437 : ulong seed,
438 0 : uchar out[16] ) {
439 : /* Single contiguous spad allocation for uniq[] and sched_mapped[] */
440 0 : void *buf = fd_spad_alloc(
441 0 : runner->spad,
442 0 : alignof(pk_with_pos_t),
443 0 : leaders->sched_cnt*sizeof(pk_with_pos_t) +
444 0 : leaders->sched_cnt*sizeof(uint) );
445 :
446 0 : pk_with_pos_t * tmp = (pk_with_pos_t *)buf;
447 0 : uint * sched_mapped = (uint *)( tmp + leaders->sched_cnt );
448 :
449 : /* Gather all pubkeys and original positions from sched[] (skip invalid) */
450 0 : ulong gather_cnt = 0UL;
451 0 : for( ulong i=0UL; i<leaders->sched_cnt; i++ ) {
452 0 : uint idx = leaders->sched[i];
453 0 : if( idx>=leaders->pub_cnt ) { /* invalid slot leader */
454 0 : sched_mapped[i] = 0U; /* prefill invalid mapping */
455 0 : continue;
456 0 : }
457 0 : fd_memcpy( &tmp[gather_cnt].pk, &leaders->pub[idx], sizeof(fd_pubkey_t) );
458 0 : tmp[gather_cnt].sched_pos = i;
459 0 : gather_cnt++;
460 0 : }
461 :
462 0 : if( gather_cnt==0UL ) {
463 : /* No leaders => hash:=0, count:=0 */
464 0 : fd_memset( out, 0, sizeof(ulong)*2 );
465 0 : return 0UL;
466 0 : }
467 :
468 : /* Sort tmp[] by pubkey, note: comparator relies on first struct member */
469 0 : sort_pkpos_inplace( tmp, (ulong)gather_cnt );
470 :
471 : /* Dedupe and assign indices into sched_mapped[] during single pass */
472 0 : ulong uniq_cnt = 0UL;
473 0 : for( ulong i=0UL; i<gather_cnt; i++ ) {
474 0 : if( i==0UL || memcmp( &tmp[i].pk, &tmp[i-1].pk, sizeof(fd_pubkey_t) )!=0 )
475 0 : uniq_cnt++;
476 : /* uniq_cnt-1 is index in uniq set */
477 0 : sched_mapped[tmp[i].sched_pos] = (uint)(uniq_cnt-1UL);
478 0 : }
479 :
480 : /* Reconstruct contiguous uniq[] for hashing */
481 0 : fd_pubkey_t *uniq = fd_spad_alloc( runner->spad,
482 0 : alignof(fd_pubkey_t),
483 0 : uniq_cnt*sizeof(fd_pubkey_t) );
484 0 : {
485 0 : ulong write_pos = 0UL;
486 0 : for( ulong i=0UL; i<gather_cnt; i++ ) {
487 0 : if( i==0UL || memcmp( &tmp[i].pk, &tmp[i-1].pk, sizeof(fd_pubkey_t) )!=0 )
488 0 : fd_memcpy( &uniq[write_pos++], &tmp[i].pk, sizeof(fd_pubkey_t) );
489 0 : }
490 0 : }
491 :
492 : /* Hash sorted unique pubkeys */
493 0 : ulong h1 = fd_hash( seed, uniq, uniq_cnt * sizeof(fd_pubkey_t) );
494 0 : fd_memcpy( out, &h1, sizeof(ulong) );
495 :
496 : /* Hash mapped indices */
497 0 : ulong h2 = fd_hash( seed, sched_mapped, leaders->sched_cnt * sizeof(uint) );
498 0 : fd_memcpy( out + sizeof(ulong), &h2, sizeof(ulong) );
499 :
500 0 : return uniq_cnt;
501 0 : }
502 :
503 : static void
504 : fd_solfuzz_pb_build_leader_schedule_effects( fd_solfuzz_runner_t * runner,
505 0 : fd_exec_test_block_effects_t * effects ) {
506 : /* Read epoch schedule sysvar */
507 0 : fd_epoch_schedule_t const * epoch_schedule = &runner->bank->f.epoch_schedule;
508 0 : FD_TEST( epoch_schedule );
509 :
510 : /* We will capture the leader schedule for the current epoch that we
511 : are in. This will capture the leader schedule generated by an
512 : epoch boundary if one was crossed. */
513 0 : ulong epoch = runner->bank->f.epoch;
514 0 : ulong ls_slot0 = fd_epoch_slot0( epoch_schedule, epoch );
515 0 : ulong slots_in_epoch = fd_epoch_slot_cnt( epoch_schedule, epoch );
516 :
517 0 : fd_epoch_leaders_t const * effects_leaders = fd_bank_epoch_leaders_query( runner->bank, epoch );
518 :
519 : /* Fill out effects struct from the Agave epoch info */
520 0 : effects->has_leader_schedule = 1;
521 0 : effects->leader_schedule.leaders_epoch = epoch;
522 0 : effects->leader_schedule.leaders_slot0 = ls_slot0;
523 0 : effects->leader_schedule.leaders_slot_cnt = slots_in_epoch;
524 0 : effects->leader_schedule.leaders_sched_cnt = slots_in_epoch;
525 0 : effects->leader_schedule.leader_pub_cnt = fd_solfuzz_block_hash_epoch_leaders(
526 0 : runner, effects_leaders,
527 0 : LEADER_SCHEDULE_HASH_SEED,
528 0 : effects->leader_schedule.leader_schedule_hash
529 0 : );
530 0 : }
531 :
532 : ulong
533 : fd_solfuzz_pb_block_run( fd_solfuzz_runner_t * runner,
534 : void const * input_,
535 : void ** output_,
536 : void * output_buf,
537 0 : ulong output_bufsz ) {
538 0 : fd_exec_test_block_context_t const * input = fd_type_pun_const( input_ );
539 0 : fd_exec_test_block_effects_t ** output = fd_type_pun( output_ );
540 :
541 0 : FD_SPAD_FRAME_BEGIN( runner->spad ) {
542 0 : ulong txn_cnt;
543 0 : fd_hash_t poh = {0};
544 0 : fd_txn_p_t * txn_ptrs = fd_solfuzz_pb_block_ctx_create( runner, input, &txn_cnt, &poh );
545 0 : if( txn_ptrs==NULL ) {
546 0 : fd_solfuzz_pb_block_ctx_destroy( runner );
547 0 : return 0;
548 0 : }
549 :
550 : /* Execute the constructed block against the runtime. */
551 0 : int is_committable = fd_solfuzz_block_ctx_exec( runner, txn_ptrs, txn_cnt, &poh );
552 :
553 : /* Start saving block exec results */
554 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
555 0 : ulong output_end = (ulong)output_buf + output_bufsz;
556 :
557 0 : fd_exec_test_block_effects_t * effects =
558 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_block_effects_t),
559 0 : sizeof(fd_exec_test_block_effects_t) );
560 0 : if( FD_UNLIKELY( _l > output_end ) ) {
561 0 : abort();
562 0 : }
563 0 : fd_memset( effects, 0, sizeof(fd_exec_test_block_effects_t) );
564 :
565 : /* Capture error status */
566 0 : effects->has_error = !is_committable;
567 :
568 : /* Capture capitalization */
569 0 : effects->slot_capitalization = !effects->has_error ? runner->bank->f.capitalization : 0UL;
570 :
571 : /* Capture hashes */
572 0 : fd_hash_t bank_hash = !effects->has_error ? runner->bank->f.bank_hash : (fd_hash_t){0};
573 0 : fd_memcpy( effects->bank_hash, bank_hash.hash, sizeof(fd_hash_t) );
574 :
575 : /* Capture cost tracker */
576 0 : fd_cost_tracker_t const * cost_tracker = fd_bank_cost_tracker_query( runner->bank );
577 0 : effects->has_cost_tracker = 1;
578 0 : effects->cost_tracker = (fd_exec_test_cost_tracker_t) {
579 0 : .block_cost = cost_tracker ? cost_tracker->block_cost : 0UL,
580 0 : .vote_cost = cost_tracker ? cost_tracker->vote_cost : 0UL,
581 0 : };
582 :
583 : /* Effects: build T-epoch (bank epoch), T-stakes ephemeral leaders and report */
584 0 : fd_solfuzz_pb_build_leader_schedule_effects( runner, effects );
585 :
586 0 : ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
587 0 : fd_solfuzz_pb_block_ctx_destroy( runner );
588 :
589 0 : *output = effects;
590 0 : return actual_end - (ulong)output_buf;
591 0 : } FD_SPAD_FRAME_END;
592 0 : }
|