Line data Source code
1 : #include "fd_solfuzz_private.h"
2 : #include "../fd_cost_tracker.h"
3 : #include "../fd_slot_params.h"
4 : #include "fd_txn_harness.h"
5 : #include "../fd_runtime.h"
6 : #include "../fd_runtime_helpers.h"
7 : #include "../fd_system_ids.h"
8 : #include "../fd_runtime_stack_tmpl.h"
9 : #include "../../stakes/fd_stake_types.h"
10 : #include "../program/vote/fd_vote_codec.h"
11 : #include "../sysvar/fd_sysvar_epoch_schedule.h"
12 : #include "../sysvar/fd_sysvar_stake_history.h"
13 : #include "../../progcache/fd_progcache_admin.h"
14 : #include "../../log_collector/fd_log_collector.h"
15 : #include "../../rewards/fd_rewards.h"
16 : #include "../../rewards/fd_stake_rewards.h"
17 : #include "generated/block.pb.h"
18 : #include "../../capture/fd_capture_ctx.h"
19 : #include "../../capture/fd_solcap_writer.h"
20 : #include <stdlib.h>
21 :
22 : /* Templatized leader schedule sort helper functions */
23 : typedef struct {
24 : fd_pubkey_t pk;
25 : ulong sched_pos; /* track original position in sched[] */
26 : } pk_with_pos_t;
27 :
28 : #define SORT_NAME sort_pkpos
29 0 : #define SORT_KEY_T pk_with_pos_t
30 0 : #define SORT_BEFORE(a,b) (memcmp(&(a).pk, &(b).pk, sizeof(fd_pubkey_t))<0)
31 : #include "../../../util/tmpl/fd_sort.c" /* generates templatized sort_pkpos_*() APIs */
32 :
33 : #define SORT_NAME sort_stake_delegation
34 0 : #define SORT_KEY_T fd_exec_test_stake_delegation_t
35 0 : #define SORT_BEFORE(a,b) (memcmp((a).stake_account, (b).stake_account, sizeof(fd_pubkey_t))<0)
36 : #include "../../../util/tmpl/fd_sort.c"
37 :
38 : /* Fixed leader schedule hash seed (consistent with solfuzz-agave) */
39 0 : #define LEADER_SCHEDULE_HASH_SEED 0xDEADFACEUL
40 :
41 : /* fd_solfuzz_epoch_credit_is_alpenglow_marker returns 1 if ec is the
42 : tower->Alpenglow migration sentinel, 0 otherwise. Protobuf mirror of
43 : fd_vote_epoch_credits_is_alpenglow_marker(). */
44 :
45 : FD_FN_PURE static inline int
46 0 : fd_solfuzz_epoch_credit_is_alpenglow_marker( fd_exec_test_epoch_credit_t const * ec ) {
47 0 : return ec->epoch==ULONG_MAX && ec->credits==ULONG_MAX && ec->prev_credits==ULONG_MAX;
48 0 : }
49 :
50 : static void
51 : fd_solfuzz_block_update_prev_epoch_stakes( fd_vote_stakes_t * vote_stakes,
52 : ushort vote_stakes_fork_id,
53 : int use_t_1,
54 : fd_exec_test_prev_vote_account_t * vote_accounts,
55 0 : uint vote_accounts_cnt ) {
56 0 : if( FD_UNLIKELY( !vote_accounts ) ) return;
57 :
58 0 : for( uint i=0U; i<vote_accounts_cnt; i++ ) {
59 0 : fd_pubkey_t vote_pubkey = FD_LOAD( fd_pubkey_t, &vote_accounts[i].address );
60 0 : fd_pubkey_t node_pubkey = FD_LOAD( fd_pubkey_t, &vote_accounts[i].node_pubkey );
61 0 : ulong stake = vote_accounts[i].stake;
62 :
63 : /* v4 stores the rate in basis points; older versions only have a
64 : percentage byte. */
65 0 : ushort commission;
66 0 : if( vote_accounts[i].version == FD_EXEC_TEST_VOTE_ACCOUNT_VERSION_V4 ) {
67 0 : commission = (ushort)vote_accounts[i].commission_bps;
68 0 : } else {
69 0 : commission = (ushort)( (uchar)( vote_accounts[i].commission_bps / 100U ) * 100U );
70 0 : }
71 :
72 : /* SIMD-0123 fields exist in v4 state only; older versions keep the
73 : 100% default and no pending rewards. */
74 0 : ushort block_revenue_commission_bps = FD_VOTE_DEFAULT_BLOCK_REVENUE_COMMISSION_BPS;
75 0 : ulong pending_delegator_rewards = 0UL;
76 0 : fd_pubkey_t const * inflation_collector = NULL;
77 0 : fd_pubkey_t const * block_collector = NULL;
78 0 : if( vote_accounts[i].version == FD_EXEC_TEST_VOTE_ACCOUNT_VERSION_V4 ) {
79 0 : block_revenue_commission_bps = (ushort)vote_accounts[i].block_revenue_commission_bps;
80 0 : pending_delegator_rewards = vote_accounts[i].pending_delegator_rewards;
81 : /* SIMD-0232 collectors; empty fields keep the vote/node defaults. */
82 0 : if( vote_accounts[i].inflation_rewards_collector.size==32U ) inflation_collector = (fd_pubkey_t const *)vote_accounts[i].inflation_rewards_collector.bytes;
83 0 : if( vote_accounts[i].block_revenue_collector.size ==32U ) block_collector = (fd_pubkey_t const *)vote_accounts[i].block_revenue_collector.bytes;
84 0 : }
85 :
86 0 : uchar const no_bls[ FD_BLS_PUBKEY_COMPRESSED_SZ ] = {0};
87 0 : fd_validator_location_t const no_loc = {0};
88 0 : if( use_t_1 ) fd_vote_stakes_snap_insert_t_1( vote_stakes, vote_stakes_fork_id, &vote_pubkey, &node_pubkey, stake, commission, no_bls,
89 0 : block_revenue_commission_bps, pending_delegator_rewards, inflation_collector, block_collector, &no_loc );
90 0 : else fd_vote_stakes_snap_insert_t_2( vote_stakes, vote_stakes_fork_id, &vote_pubkey, &node_pubkey, stake, commission, no_bls,
91 0 : block_revenue_commission_bps, pending_delegator_rewards, inflation_collector, block_collector, &no_loc );
92 0 : }
93 0 : }
94 :
95 : /* Stores an acc in the stake delegations cache for the given vote
96 : account. Deserializes and uses the present account state to derive
97 : delegation information. */
98 : static void
99 : fd_solfuzz_block_register_stake_delegation( fd_accdb_t * accdb,
100 : fd_accdb_fork_id_t fork_id,
101 : fd_stake_delegations_t * stake_delegations,
102 0 : fd_pubkey_t * pubkey ) {
103 0 : fd_acc_t acc = fd_accdb_read_one( accdb, fork_id, pubkey->key );
104 0 : if( FD_UNLIKELY( !acc.lamports ) ) {
105 0 : fd_accdb_unread_one( accdb, &acc );
106 0 : return;
107 0 : }
108 :
109 0 : fd_stake_state_t const * stake_state = NULL;
110 0 : if( memcmp( acc.owner, fd_solana_stake_program_id.key, 32UL )!=0 ||
111 0 : !( stake_state = fd_stake_state_view( acc.data, acc.data_len ) ) ||
112 0 : stake_state->stake_type!=FD_STAKE_STATE_STAKE ) {
113 0 : fd_accdb_unread_one( accdb, &acc );
114 0 : return;
115 0 : }
116 :
117 0 : fd_stake_delegations_root_update(
118 0 : stake_delegations,
119 0 : pubkey,
120 0 : &stake_state->stake.stake.delegation.voter_pubkey,
121 0 : stake_state->stake.stake.delegation.stake,
122 0 : stake_state->stake.stake.delegation.activation_epoch,
123 0 : stake_state->stake.stake.delegation.deactivation_epoch,
124 0 : stake_state->stake.stake.credits_observed,
125 0 : acc.lamports,
126 0 : (uint)acc.data_len );
127 0 : fd_accdb_unread_one( accdb, &acc );
128 0 : }
129 :
130 : static void
131 0 : fd_solfuzz_pb_block_ctx_destroy( fd_solfuzz_runner_t * runner ) {
132 0 : fd_banks_stake_delegations_evict_bank_fork( runner->banks, runner->bank );
133 :
134 0 : runner->bank->stake_rewards_fork_id = USHORT_MAX;
135 0 : fd_stake_rewards_clear( fd_bank_stake_rewards_modify( runner->bank ) );
136 :
137 0 : fd_progcache_reset( runner->progcache->join );
138 :
139 : /* Purge the fork attached in ctx_create so the accdb fork pool slot
140 : is released back for reuse. Without this, repeated harness
141 : invocations (e.g. under a fuzzer) exhaust max_live_slots. */
142 0 : fd_accdb_purge( runner->accdb, runner->bank->accdb_fork_id );
143 0 : int charge_busy = 0;
144 0 : fd_accdb_background( runner->accdb, &charge_busy );
145 :
146 0 : }
147 :
148 : /* Sets up block execution context from an input test case to execute
149 : against the runtime. Returns block_info on success and NULL on
150 : failure. */
151 : static fd_txn_p_t *
152 : fd_solfuzz_pb_block_ctx_create( fd_solfuzz_runner_t * runner,
153 : fd_exec_test_block_context_t const * test_ctx,
154 : ulong * out_txn_cnt,
155 0 : fd_hash_t * poh ) {
156 0 : fd_accdb_t * accdb = runner->accdb;
157 0 : fd_bank_t * bank = runner->bank;
158 0 : fd_banks_t * banks = runner->banks;
159 :
160 0 : fd_runtime_stack_t * runtime_stack = runner->runtime_stack;
161 :
162 0 : fd_banks_clear_bank( banks, bank );
163 :
164 0 : runner->bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, fd_progcache_fork_id_initial() );
165 :
166 : /* Attach a fork off the runner's root for context loading */
167 0 : fd_accdb_fork_id_t fork_id = fd_accdb_attach_child( accdb, runner->root_fork_id );
168 0 : bank->accdb_fork_id = fork_id;
169 0 : bank->parent_accdb_fork_id = bank->accdb_fork_id;
170 :
171 : /* Initialize bank from input block bank */
172 0 : FD_TEST( test_ctx->has_bank );
173 0 : fd_exec_test_block_bank_t const * block_bank = &test_ctx->bank;
174 :
175 : /* Slot */
176 0 : ulong slot = block_bank->slot;
177 0 : bank->f.slot = slot;
178 :
179 : /* Register a non-root progcache transaction at the bank's xid so the
180 : BPF loader can insert program cache entries during execution. */
181 0 : bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, bank->progcache_fork_id );
182 :
183 : /* Blockhash queue */
184 0 : fd_solfuzz_pb_restore_blockhash_queue( bank, block_bank->blockhash_queue, block_bank->blockhash_queue_count );
185 :
186 : /* RBH lamports per signature. In the Agave harness this is set inside
187 : the fee rate governor itself. */
188 0 : runner->bank->f.rbh_lamports_per_sig = block_bank->rbh_lamports_per_signature;
189 :
190 : /* Fee rate governor */
191 0 : FD_TEST( block_bank->has_fee_rate_governor );
192 0 : fd_solfuzz_pb_restore_fee_rate_governor( bank, &block_bank->fee_rate_governor );
193 :
194 : /* Parent slot */
195 0 : ulong parent_slot = block_bank->parent_slot;
196 0 : bank->f.parent_slot = parent_slot;
197 :
198 : /* Capitalization */
199 0 : bank->f.capitalization = block_bank->capitalization;
200 :
201 : /* Inflation */
202 0 : FD_TEST( block_bank->has_inflation );
203 0 : fd_inflation_t inflation = {
204 0 : .initial = block_bank->inflation.initial,
205 0 : .terminal = block_bank->inflation.terminal,
206 0 : .taper = block_bank->inflation.taper,
207 0 : .foundation = block_bank->inflation.foundation,
208 0 : .foundation_term = block_bank->inflation.foundation_term,
209 0 : };
210 0 : bank->f.inflation = inflation;
211 :
212 : /* Block height */
213 0 : bank->f.block_height = block_bank->block_height;
214 :
215 : /* POH (set right before finalize since we don't fuzz POH calculation) */
216 0 : fd_memcpy( poh, block_bank->poh, sizeof(fd_hash_t) );
217 :
218 : /* Bank hash (parent bank hash because current bank hash gets computed
219 : after the block executes) */
220 0 : fd_hash_t * bank_hash = &bank->f.bank_hash;
221 0 : fd_memcpy( bank_hash, block_bank->parent_bank_hash, sizeof(fd_hash_t) );
222 :
223 : /* Previous bank hash (used as input to the bank hash computation).
224 : In production this is set by fd_banks_clone_from_parent. */
225 0 : bank->f.prev_bank_hash = *(fd_hash_t const *)block_bank->parent_bank_hash;
226 :
227 : /* Parent signature count */
228 0 : bank->f.parent_signature_cnt = block_bank->parent_signature_count;
229 :
230 : /* Feature set */
231 0 : FD_TEST( block_bank->has_features );
232 0 : fd_exec_test_feature_set_t const * feature_set = &block_bank->features;
233 0 : fd_features_t * features_bm = &bank->f.features;
234 0 : fd_solfuzz_pb_create_feature_accounts( accdb, fork_id, feature_set, test_ctx->acct_states, test_ctx->acct_states_count );
235 0 : FD_TEST( fd_solfuzz_pb_restore_features( features_bm, feature_set ) );
236 :
237 : /* Total epoch stake (derived from T-1 vote accounts) */
238 0 : ulong total_epoch_stake = 0UL;
239 0 : for( uint i=0U; i<block_bank->vote_accounts_t_1_count; i++ ) {
240 0 : total_epoch_stake += block_bank->vote_accounts_t_1[i].stake;
241 0 : }
242 0 : bank->f.total_epoch_stake = total_epoch_stake;
243 :
244 : /* Using default configuration of 64 ticks per slot
245 : https://github.com/anza-xyz/solana-sdk/blob/time-utils%40v3.0.0/time-utils/src/lib.rs#L18-L27 */
246 0 : uint128 ns_per_slot_128 = FD_LOAD(uint128, block_bank->ns_per_slot );
247 0 : FD_TEST( ns_per_slot_128<=(uint128)ULONG_MAX );
248 0 : ulong ns_per_slot = (ulong)ns_per_slot_128;
249 0 : bank->f.slot_params = FD_SLOT_PARAMS_400MS;
250 0 : bank->f.slot_params.ns_per_slot = ns_per_slot;
251 0 : bank->f.ticks_per_slot = 64UL;
252 0 : runner->bank->f.slot_params.slots_per_year = (double)SECONDS_PER_YEAR * 1e9 / (double)ns_per_slot;
253 0 : bank->f.slot_params.hashes_per_tick = (slot+1UL)*64UL;
254 0 : bank->f.slot_params_default = bank->f.slot_params;
255 :
256 : /* Load in accounts, populate stake delegations and vote accounts */
257 0 : fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
258 0 : fd_stake_delegations_reset( stake_delegations );
259 :
260 0 : bank->stake_delegations_fork_id = fd_stake_delegations_new_fork( stake_delegations, USHORT_MAX );
261 :
262 0 : FD_TEST( block_bank->vote_accounts_t_1_count<=FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS );
263 0 : FD_TEST( block_bank->vote_accounts_t_2_count<=FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS );
264 :
265 0 : for( ushort i=0; i<test_ctx->acct_states_count; i++ ) {
266 0 : fd_solfuzz_pb_load_account( runner->runtime, accdb, fork_id, &test_ctx->acct_states[i], i );
267 :
268 : /* Update the stake delegations cache for epoch T */
269 0 : fd_pubkey_t pubkey;
270 0 : memcpy( &pubkey, test_ctx->acct_states[i].address, sizeof(fd_pubkey_t) );
271 0 : fd_solfuzz_block_register_stake_delegation( accdb, fork_id, stake_delegations, &pubkey );
272 0 : }
273 :
274 : /* reduce_stake_warmup_cooldown is activated on all clusters, so the
275 : new warmup/cooldown rate (0.09) applies from epoch 0 onwards. */
276 0 : bank->f.warmup_cooldown_rate_epoch = 0UL;
277 :
278 : /* Restore sysvar cache */
279 0 : fd_sysvar_cache_t * sysvar_cache = runner->runtime->sysvar_cache;
280 0 : fd_sysvar_cache_restore_fuzz( sysvar_cache, accdb, bank->accdb_fork_id );
281 0 : runner->runtime->sysvar_bank_seq = 0UL; /* refreshed again after block_execute_prepare */
282 :
283 : /* Epoch schedule */
284 0 : FD_TEST( fd_sysvar_cache_epoch_schedule_read( sysvar_cache, &bank->f.epoch_schedule ) );
285 :
286 : /* Current epoch gets updated in process_new_epoch, so use the epoch
287 : from the parent slot */
288 0 : bank->f.epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, parent_slot, NULL );
289 :
290 0 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
291 0 : fd_bank_t * parent = fd_banks_get_parent( banks, bank );
292 0 : FD_TEST( parent );
293 :
294 : /* Restore the snapshot on the parent, then fork the execution bank
295 : from it just as replay does. */
296 0 : fd_vote_stakes_reset( vote_stakes );
297 0 : parent->vote_stakes_fork_id = fd_vote_stakes_init( vote_stakes, bank->f.epoch );
298 0 : fd_solfuzz_block_update_prev_epoch_stakes( vote_stakes, parent->vote_stakes_fork_id, 1, block_bank->vote_accounts_t_1, block_bank->vote_accounts_t_1_count );
299 0 : fd_solfuzz_block_update_prev_epoch_stakes( vote_stakes, parent->vote_stakes_fork_id, 0, block_bank->vote_accounts_t_2, block_bank->vote_accounts_t_2_count );
300 0 : fd_vote_stakes_refresh( vote_stakes, parent->vote_stakes_fork_id, accdb, fork_id );
301 0 : bank->vote_stakes_fork_id = fd_vote_stakes_new_fork( vote_stakes, parent->vote_stakes_fork_id, bank->f.epoch );
302 :
303 : /* Initialize total_effective/activating/deactivating_stake from the
304 : loaded stake delegations. These are read by fd_stakes_activate_epoch
305 : at epoch boundary instead of re-scanning all delegations. */
306 0 : uchar stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
307 0 : fd_stake_history_t stake_history[1];
308 0 : if( FD_UNLIKELY( !fd_sysvar_stake_history_read( accdb, bank->accdb_fork_id, stake_history_data, stake_history ) ) ) {
309 0 : FD_LOG_ERR(( "StakeHistory sysvar missing or invalid" ));
310 0 : }
311 0 : fd_stake_delegations_refresh( stake_delegations, bank->f.epoch, stake_history, &bank->f.warmup_cooldown_rate_epoch, FD_FEATURE_ACTIVE_BANK( bank, remove_inactive_stakes ) );
312 :
313 0 : ulong chain_cnt = fd_vote_rewards_map_chain_cnt_est( runtime_stack->max_vote_accounts );
314 0 : FD_TEST( fd_vote_rewards_map_join( fd_vote_rewards_map_new( runtime_stack->stakes.vote_map, chain_cnt, 999 ) ) );
315 :
316 : /* Use epoch_credits from the proto if available (captured at epoch
317 : boundary time), otherwise fall back to the vote account in accdb. */
318 0 : ulong epoch_credits_len = 0UL;
319 0 : fd_epoch_credits_view_t epoch_credits_view[1];
320 0 : FD_TEST( fd_epoch_credits_view_init( epoch_credits_view, fd_bank_epoch_credits( bank ), bank->epoch_credits_fork_id ) );
321 0 : for( uint i=0U; i<block_bank->vote_accounts_t_1_count; i++ ) {
322 0 : fd_exec_test_prev_vote_account_t const * prev_vote_accs = &block_bank->vote_accounts_t_1[i];
323 :
324 0 : if( FD_UNLIKELY( !fd_vote_stakes_query_t_1( vote_stakes, bank->vote_stakes_fork_id, (fd_pubkey_t const *)prev_vote_accs->address, NULL ) ) ) continue;
325 :
326 0 : FD_TEST( prev_vote_accs->epoch_credits_count<=FD_EPOCH_CREDITS_MAX );
327 0 : fd_epoch_credits_t * ec = &epoch_credits_view->credits[epoch_credits_len++];
328 0 : fd_memcpy( ec->pubkey, prev_vote_accs->address, sizeof(fd_pubkey_t) );
329 :
330 : /* Alpenglow migration markers are not credits records: skip them so
331 : base_credits comes from the first real entry (subtracting a
332 : ULONG_MAX base would underflow every delta) and cnt counts only
333 : real entries. Mirrors fd_ssload.c / get_vote_credits(). */
334 0 : ulong cnt = 0UL;
335 0 : ec->base_credits = 0UL;
336 0 : for( ulong j=0UL; j<prev_vote_accs->epoch_credits_count; j++ ) {
337 0 : fd_exec_test_epoch_credit_t const * epc = &prev_vote_accs->epoch_credits[j];
338 0 : if( FD_UNLIKELY( fd_solfuzz_epoch_credit_is_alpenglow_marker( epc ) ) ) continue;
339 0 : if( FD_UNLIKELY( !cnt ) ) ec->base_credits = epc->prev_credits;
340 0 : ec->epoch[ cnt ] = (ushort)epc->epoch;
341 0 : ec->credits_delta[ cnt ] = epc->credits - ec->base_credits;
342 0 : ec->prev_credits_delta[ cnt ] = epc->prev_credits - ec->base_credits;
343 0 : cnt++;
344 0 : }
345 0 : ec->cnt = (uchar)cnt; /* <=FD_EPOCH_CREDITS_MAX tested above */
346 0 : }
347 0 : epoch_credits_view->len = epoch_credits_len;
348 0 : fd_epoch_credits_view_fini( epoch_credits_view );
349 :
350 : /* Update leader schedule */
351 0 : fd_runtime_update_leaders( bank, runtime_stack );
352 :
353 : /* Set the initial lthash from the input. */
354 0 : fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
355 0 : fd_memcpy( lthash, block_bank->parent_lt_hash, sizeof(fd_lthash_value_t) );
356 0 : fd_bank_lthash_end_locking_modify( bank );
357 :
358 : /* Rent */
359 0 : FD_TEST( fd_sysvar_cache_rent_read( sysvar_cache, &runner->bank->f.rent ) );
360 :
361 : /* Prepare raw transaction pointers and block / microblock infos */
362 0 : ulong txn_cnt = test_ctx->txns_count;
363 0 : fd_txn_p_t * txn_ptrs = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), txn_cnt * sizeof(fd_txn_p_t) );
364 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
365 0 : fd_txn_p_t * txn = &txn_ptrs[i];
366 0 : ulong msg_sz = fd_solfuzz_pb_txn_serialize( txn->payload, &test_ctx->txns[i] );
367 :
368 : // Reject transactions that cannot be serialized within FD_TXN_MTU
369 0 : if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) {
370 0 : return NULL;
371 0 : }
372 0 : txn->payload_sz = (ushort)msg_sz;
373 :
374 : // Reject any transactions that cannot be parsed
375 0 : if( FD_UNLIKELY( !fd_txn_parse( txn->payload, msg_sz, TXN( txn ), NULL ) ) ) {
376 0 : return NULL;
377 0 : }
378 0 : }
379 :
380 0 : *out_txn_cnt = txn_cnt;
381 0 : return txn_ptrs;
382 0 : }
383 :
384 : /* Takes in a list of txn_p_t created from
385 : fd_runtime_fuzz_block_ctx_create and executes it against the runtime.
386 : Returns the execution result. */
387 : static int
388 : fd_solfuzz_block_ctx_exec( fd_solfuzz_runner_t * runner,
389 : fd_txn_p_t * txn_ptrs,
390 : ulong txn_cnt,
391 0 : fd_hash_t * poh ) {
392 0 : int res = 0;
393 :
394 : // Prepare. Execute. Finalize.
395 0 : FD_SPAD_FRAME_BEGIN( runner->spad ) {
396 0 : fd_capture_ctx_t * capture_ctx = NULL;
397 :
398 0 : if( runner->solcap ) {
399 0 : void * capture_ctx_mem = fd_spad_alloc( runner->spad, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
400 0 : capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( capture_ctx_mem ) );
401 0 : if( FD_UNLIKELY( !capture_ctx ) ) {
402 0 : FD_LOG_ERR(( "Failed to initialize capture_ctx" ));
403 0 : }
404 :
405 0 : fd_capture_link_file_t * capture_link_file =
406 0 : fd_spad_alloc( runner->spad, alignof(fd_capture_link_file_t), sizeof(fd_capture_link_file_t) );
407 0 : if( FD_UNLIKELY( !capture_link_file ) ) {
408 0 : FD_LOG_ERR(( "Failed to allocate capture_link_file" ));
409 0 : }
410 :
411 0 : capture_link_file->base.vt = &fd_capture_link_file_vt;
412 :
413 0 : int solcap_fd = (int)(ulong)runner->solcap_file;
414 0 : capture_link_file->fd = solcap_fd;
415 0 : capture_ctx->capture_link = &capture_link_file->base;
416 0 : capture_ctx->capctx_type.file = capture_link_file;
417 0 : capture_ctx->solcap_start_slot = runner->bank->f.slot;
418 0 : capture_ctx->capture_solcap = 1;
419 :
420 0 : fd_solcap_writer_init( capture_ctx->capture, solcap_fd );
421 0 : }
422 :
423 0 : fd_rewards_recalculate_partitioned_rewards( runner->bank, runner->accdb, runner->runtime_stack, capture_ctx );
424 :
425 : /* Process new epoch may push a new spad frame onto the runtime spad. We should make sure this frame gets
426 : cleared (if it was allocated) before executing the block. */
427 0 : int is_epoch_boundary = 0;
428 0 : fd_runtime_block_execute_prepare( runner->banks, runner->bank, runner->accdb, runner->runtime_stack, capture_ctx, &is_epoch_boundary );
429 :
430 : /* Sequential transaction execution. Continue processing
431 : transactions even if a prior one was uncommitable. */
432 0 : int has_err = 0;
433 : /* fd_txn_out_t is ~10 MiB (inlined rollback/sysvar buffers), which
434 : overflows a tile's 8 MiB stack (FD_TILE_PRIVATE_STACK_SZ) when run
435 : under the multi-tile sol_compat harness. Allocate it from the
436 : per-runner spad instead (one buffer, reused each iteration). Must
437 : be the spad, not static, since runner tiles execute concurrently. */
438 0 : fd_txn_out_t * txn_out = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), sizeof(fd_txn_out_t) );
439 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
440 0 : fd_txn_p_t * txn = &txn_ptrs[i];
441 :
442 : /* Execute the transaction against the runtime */
443 0 : res = FD_RUNTIME_EXECUTE_SUCCESS;
444 0 : fd_txn_in_t txn_in = { .txn = txn, .bundle.is_bundle = 0 };
445 0 : fd_runtime_t * runtime = runner->runtime;
446 0 : fd_log_collector_t log[1];
447 0 : runtime->log.log_collector = log;
448 0 : fd_solfuzz_txn_ctx_exec( runner, runtime, &txn_in, &res, txn_out, 1 );
449 0 : txn_out->err.exec_err = res;
450 :
451 0 : if( FD_UNLIKELY( !txn_out->err.is_committable ) ) {
452 0 : fd_runtime_cancel_txn( runtime, NULL, NULL, txn_out );
453 0 : has_err = 1;
454 0 : continue;
455 0 : }
456 :
457 : /* Finalize the transaction */
458 0 : fd_runtime_commit_txn( runtime, runner->bank, NULL, txn_out );
459 :
460 0 : if( FD_UNLIKELY( !txn_out->err.is_committable ) ) {
461 0 : has_err = 1;
462 0 : continue;
463 0 : }
464 0 : }
465 :
466 : /* At this point we want to set the poh. This is what will get
467 : updated in the blockhash queue. */
468 0 : runner->bank->f.poh = *poh;
469 : /* Finalize the block */
470 0 : fd_runtime_block_execute_finalize( runner->bank, runner->accdb, capture_ctx, NULL, (ushort)0, NULL );
471 :
472 0 : return !has_err;
473 0 : } FD_SPAD_FRAME_END;
474 0 : }
475 :
476 : /* Canonical (Agave-aligned) schedule hash
477 : Unique pubkeys referenced by sched, sorted deterministically
478 : Per-rotation indices mapped into sorted-uniq array */
479 : ulong
480 : fd_solfuzz_block_hash_epoch_leaders( fd_solfuzz_runner_t * runner,
481 : fd_epoch_leaders_t const * leaders,
482 : ulong seed,
483 0 : uchar out[16] ) {
484 : /* Single contiguous spad allocation for uniq[] and sched_mapped[] */
485 0 : void *buf = fd_spad_alloc(
486 0 : runner->spad,
487 0 : alignof(pk_with_pos_t),
488 0 : leaders->sched_cnt*sizeof(pk_with_pos_t) +
489 0 : leaders->sched_cnt*sizeof(uint) );
490 :
491 0 : pk_with_pos_t * tmp = (pk_with_pos_t *)buf;
492 0 : uint * sched_mapped = (uint *)( tmp + leaders->sched_cnt );
493 :
494 : /* Gather all pubkeys and original positions from sched[] (skip invalid) */
495 0 : ulong gather_cnt = 0UL;
496 0 : for( ulong i=0UL; i<leaders->sched_cnt; i++ ) {
497 0 : uint idx = leaders->sched[i];
498 0 : if( idx>=leaders->pub_cnt ) { /* invalid slot leader */
499 0 : sched_mapped[i] = 0U; /* prefill invalid mapping */
500 0 : continue;
501 0 : }
502 0 : fd_memcpy( &tmp[gather_cnt].pk, &leaders->pub[idx], sizeof(fd_pubkey_t) );
503 0 : tmp[gather_cnt].sched_pos = i;
504 0 : gather_cnt++;
505 0 : }
506 :
507 0 : if( gather_cnt==0UL ) {
508 : /* No leaders => hash:=0, count:=0 */
509 0 : fd_memset( out, 0, sizeof(ulong)*2 );
510 0 : return 0UL;
511 0 : }
512 :
513 : /* Sort tmp[] by pubkey, note: comparator relies on first struct member */
514 0 : sort_pkpos_inplace( tmp, (ulong)gather_cnt );
515 :
516 : /* Dedupe and assign indices into sched_mapped[] during single pass */
517 0 : ulong uniq_cnt = 0UL;
518 0 : for( ulong i=0UL; i<gather_cnt; i++ ) {
519 0 : if( i==0UL || memcmp( &tmp[i].pk, &tmp[i-1].pk, sizeof(fd_pubkey_t) )!=0 )
520 0 : uniq_cnt++;
521 : /* uniq_cnt-1 is index in uniq set */
522 0 : sched_mapped[tmp[i].sched_pos] = (uint)(uniq_cnt-1UL);
523 0 : }
524 :
525 : /* Reconstruct contiguous uniq[] for hashing */
526 0 : fd_pubkey_t *uniq = fd_spad_alloc( runner->spad,
527 0 : alignof(fd_pubkey_t),
528 0 : uniq_cnt*sizeof(fd_pubkey_t) );
529 0 : {
530 0 : ulong write_pos = 0UL;
531 0 : for( ulong i=0UL; i<gather_cnt; i++ ) {
532 0 : if( i==0UL || memcmp( &tmp[i].pk, &tmp[i-1].pk, sizeof(fd_pubkey_t) )!=0 )
533 0 : fd_memcpy( &uniq[write_pos++], &tmp[i].pk, sizeof(fd_pubkey_t) );
534 0 : }
535 0 : }
536 :
537 : /* Hash sorted unique pubkeys */
538 0 : ulong h1 = fd_hash( seed, uniq, uniq_cnt * sizeof(fd_pubkey_t) );
539 0 : fd_memcpy( out, &h1, sizeof(ulong) );
540 :
541 : /* Hash mapped indices */
542 0 : ulong h2 = fd_hash( seed, sched_mapped, leaders->sched_cnt * sizeof(uint) );
543 0 : fd_memcpy( out + sizeof(ulong), &h2, sizeof(ulong) );
544 :
545 0 : return uniq_cnt;
546 0 : }
547 :
548 : static void
549 : fd_solfuzz_pb_build_leader_schedule_effects( fd_solfuzz_runner_t * runner,
550 0 : fd_exec_test_block_effects_t * effects ) {
551 : /* Read epoch schedule sysvar */
552 0 : fd_epoch_schedule_t const * epoch_schedule = &runner->bank->f.epoch_schedule;
553 0 : FD_TEST( epoch_schedule );
554 :
555 : /* We will capture the leader schedule for the current epoch that we
556 : are in. This will capture the leader schedule generated by an
557 : epoch boundary if one was crossed. */
558 0 : ulong epoch = runner->bank->f.epoch;
559 0 : ulong ls_slot0 = fd_epoch_slot0( epoch_schedule, epoch );
560 0 : ulong slots_in_epoch = fd_epoch_slot_cnt( epoch_schedule, epoch );
561 :
562 0 : fd_epoch_leaders_t const * effects_leaders = fd_bank_epoch_leaders_query( runner->bank, epoch );
563 :
564 : /* Fill out effects struct from the Agave epoch info */
565 0 : effects->has_leader_schedule = 1;
566 0 : effects->leader_schedule.leaders_epoch = epoch;
567 0 : effects->leader_schedule.leaders_slot0 = ls_slot0;
568 0 : effects->leader_schedule.leaders_slot_cnt = slots_in_epoch;
569 0 : effects->leader_schedule.leaders_sched_cnt = slots_in_epoch;
570 0 : effects->leader_schedule.leader_pub_cnt = fd_solfuzz_block_hash_epoch_leaders(
571 0 : runner, effects_leaders,
572 0 : LEADER_SCHEDULE_HASH_SEED,
573 0 : effects->leader_schedule.leader_schedule_hash
574 0 : );
575 0 : }
576 :
577 : static ulong
578 : fd_solfuzz_pb_collect_stake_delegations( fd_solfuzz_runner_t * runner,
579 : fd_exec_test_stake_delegation_t * out,
580 0 : ulong out_max ) {
581 0 : fd_bank_t * bank = runner->bank;
582 :
583 0 : uchar stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
584 0 : fd_stake_history_t stake_history_[1];
585 0 : fd_stake_history_t * stake_history = fd_sysvar_stake_history_read( runner->accdb, bank->accdb_fork_id, stake_history_data, stake_history_ );
586 0 : ulong * warmup_cooldown_rate_epoch = &bank->f.warmup_cooldown_rate_epoch;
587 :
588 0 : fd_stake_delegations_t * stake_delegations = fd_bank_stake_delegations_modify( bank );
589 0 : fd_stake_delegations_view_begin( stake_delegations,
590 0 : bank->f.epoch,
591 0 : stake_history,
592 0 : warmup_cooldown_rate_epoch,
593 0 : bank->stake_delegations_fork_id );
594 :
595 0 : ulong cnt = 0UL;
596 0 : fd_stake_delegations_iter_t iter_[1];
597 0 : for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
598 0 : !fd_stake_delegations_iter_done( iter );
599 0 : fd_stake_delegations_iter_next( iter ) ) {
600 0 : if( FD_UNLIKELY( cnt>=out_max ) ) abort();
601 0 : fd_stake_delegation_t const * d = fd_stake_delegations_iter_ele( iter );
602 0 : fd_exec_test_stake_delegation_t * o = &out[ cnt++ ];
603 0 : fd_memcpy( o->stake_account, d->stake_account.uc, sizeof(fd_pubkey_t) );
604 0 : fd_memcpy( o->vote_account, d->vote_account.uc, sizeof(fd_pubkey_t) );
605 0 : o->stake = d->stake;
606 0 : o->activation_epoch = d->activation_epoch ==USHORT_MAX ? ULONG_MAX : (ulong)d->activation_epoch;
607 0 : o->deactivation_epoch = d->deactivation_epoch==USHORT_MAX ? ULONG_MAX : (ulong)d->deactivation_epoch;
608 0 : o->credits_observed = d->credits_observed;
609 0 : o->lamports = d->lamports;
610 0 : o->data_len = d->acc_dlen;
611 0 : }
612 :
613 0 : fd_stake_delegations_view_end( stake_delegations, stake_history, warmup_cooldown_rate_epoch );
614 :
615 0 : sort_stake_delegation_inplace( out, cnt );
616 0 : return cnt;
617 0 : }
618 :
619 : ulong
620 : fd_solfuzz_pb_block_run( fd_solfuzz_runner_t * runner,
621 : void const * input_,
622 : void ** output_,
623 : void * output_buf,
624 0 : ulong output_bufsz ) {
625 0 : fd_exec_test_block_context_t const * input = fd_type_pun_const( input_ );
626 0 : fd_exec_test_block_effects_t ** output = fd_type_pun( output_ );
627 :
628 0 : FD_SPAD_FRAME_BEGIN( runner->spad ) {
629 0 : ulong txn_cnt;
630 0 : fd_hash_t poh = {0};
631 0 : fd_txn_p_t * txn_ptrs = fd_solfuzz_pb_block_ctx_create( runner, input, &txn_cnt, &poh );
632 0 : if( txn_ptrs==NULL ) {
633 0 : fd_solfuzz_pb_block_ctx_destroy( runner );
634 0 : return 0;
635 0 : }
636 :
637 : /* Execute the constructed block against the runtime. */
638 0 : int is_committable = fd_solfuzz_block_ctx_exec( runner, txn_ptrs, txn_cnt, &poh );
639 :
640 : /* Start saving block exec results */
641 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
642 0 : ulong output_end = (ulong)output_buf + output_bufsz;
643 :
644 0 : fd_exec_test_block_effects_t * effects =
645 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_block_effects_t),
646 0 : sizeof(fd_exec_test_block_effects_t) );
647 0 : if( FD_UNLIKELY( _l > output_end ) ) {
648 0 : abort();
649 0 : }
650 0 : fd_memset( effects, 0, sizeof(fd_exec_test_block_effects_t) );
651 :
652 : /* Capture error status */
653 0 : effects->has_error = !is_committable;
654 :
655 : /* Capture capitalization */
656 0 : effects->slot_capitalization = !effects->has_error ? runner->bank->f.capitalization : 0UL;
657 :
658 : /* Capture hashes */
659 0 : fd_hash_t bank_hash = !effects->has_error ? runner->bank->f.bank_hash : (fd_hash_t){0};
660 0 : fd_memcpy( effects->bank_hash, bank_hash.hash, sizeof(fd_hash_t) );
661 :
662 : /* Capture cost tracker */
663 0 : fd_cost_tracker_t const * cost_tracker = fd_cost_tracker_store_peek( fd_bank_cost_tracker( runner->bank ), runner->bank->cost_tracker_fork_id );
664 0 : effects->has_cost_tracker = 1;
665 0 : effects->cost_tracker = (fd_exec_test_cost_tracker_t) {
666 0 : .block_cost = cost_tracker ? cost_tracker->block_cost : 0UL,
667 0 : };
668 :
669 : /* Effects: build T-epoch (bank epoch), T-stakes ephemeral leaders and report */
670 0 : fd_solfuzz_pb_build_leader_schedule_effects( runner, effects );
671 :
672 0 : if( !effects->has_error ) {
673 0 : fd_exec_test_stake_delegation_t * out = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_stake_delegation_t), 0UL );
674 0 : ulong cnt = fd_solfuzz_pb_collect_stake_delegations( runner, out, (output_end-_l)/sizeof(fd_exec_test_stake_delegation_t) );
675 0 : FD_SCRATCH_ALLOC_APPEND( l, 1UL, cnt*sizeof(fd_exec_test_stake_delegation_t) );
676 0 : effects->stake_delegations = out;
677 0 : effects->stake_delegations_count = (uint)cnt;
678 0 : }
679 :
680 0 : ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
681 0 : fd_solfuzz_pb_block_ctx_destroy( runner );
682 :
683 0 : *output = effects;
684 0 : return actual_end - (ulong)output_buf;
685 0 : } FD_SPAD_FRAME_END;
686 0 : }
|