Line data Source code
1 : #include "fd_bundle_harness.h"
2 : #include "fd_solfuzz_private.h"
3 : #include "fd_txn_harness.h"
4 : #include "fd_dump_pb.h"
5 : #include "generated/bundle.pb.h"
6 : #include "../fd_cost_tracker.h"
7 : #include "../fd_slot_params.h"
8 : #include "../fd_runtime.h"
9 : #include "../sysvar/fd_sysvar_cache.h"
10 : #include "../sysvar/fd_sysvar_epoch_schedule.h"
11 : #include "../../progcache/fd_progcache_admin.h"
12 : #include "../../log_collector/fd_log_collector.h" /* IWYU pragma: keep */
13 : #include "../../stakes/fd_stakes.h"
14 :
15 : static void
16 0 : fd_solfuzz_bundle_ctx_destroy( fd_solfuzz_runner_t * runner ) {
17 0 : fd_banks_stake_delegations_evict_bank_fork( runner->banks, runner->bank );
18 :
19 0 : fd_progcache_reset( runner->progcache->join );
20 :
21 : /* Purge the fork attached in ctx_create so the accdb fork pool slot
22 : is released back for reuse. Without this, repeated harness
23 : invocations (e.g. under a fuzzer) exhaust max_live_slots. */
24 0 : fd_accdb_purge( runner->accdb, runner->bank->accdb_fork_id );
25 0 : int charge_busy = 0;
26 0 : fd_accdb_background( runner->accdb, &charge_busy );
27 :
28 : /* Keep the runner reusable across many bundle inputs. */
29 0 : fd_alloc_compact( runner->progcache->join->alloc );
30 0 : }
31 :
32 : static fd_txn_p_t *
33 : fd_solfuzz_pb_bundle_ctx_create( fd_solfuzz_runner_t * runner,
34 : fd_exec_test_bundle_context_t const * test_ctx,
35 0 : ulong * out_txn_cnt ) {
36 0 : ulong txn_cnt = (ulong)test_ctx->txns_count;
37 0 : FD_TEST( txn_cnt<=FD_PACK_MAX_TXN_PER_BUNDLE );
38 :
39 0 : fd_accdb_t * accdb = runner->accdb;
40 :
41 0 : ulong slot = fd_solfuzz_pb_get_slot( test_ctx->account_shared_data, test_ctx->account_shared_data_count );
42 :
43 : /* Initialize bank from input txn bank */
44 0 : fd_banks_clear_bank( runner->banks, runner->bank );
45 :
46 0 : runner->bank->f.slot = slot;
47 0 : runner->bank->bank_seq = runner->bank->idx;
48 :
49 0 : runner->bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, fd_progcache_fork_id_initial() );
50 0 : runner->bank->accdb_fork_id = fd_accdb_attach_child( accdb, runner->root_fork_id );
51 0 : runner->bank->parent_accdb_fork_id = runner->bank->accdb_fork_id;
52 :
53 0 : FD_TEST( test_ctx->has_bank );
54 0 : fd_exec_test_txn_bank_t const * txn_bank = &test_ctx->bank;
55 :
56 0 : fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( runner->banks );
57 0 : runner->bank->stake_delegations_fork_id = fd_stake_delegations_new_fork( stake_delegations );
58 :
59 0 : fd_solfuzz_pb_restore_blockhash_queue( runner->bank, txn_bank->blockhash_queue, txn_bank->blockhash_queue_count );
60 0 : runner->bank->f.rbh_lamports_per_sig = txn_bank->rbh_lamports_per_signature;
61 :
62 0 : FD_TEST( txn_bank->has_fee_rate_governor );
63 0 : fd_solfuzz_pb_restore_fee_rate_governor( runner->bank, &txn_bank->fee_rate_governor );
64 :
65 0 : runner->bank->f.parent_slot = slot-1UL;
66 0 : runner->bank->f.total_epoch_stake = txn_bank->total_epoch_stake;
67 :
68 0 : FD_TEST( txn_bank->has_features );
69 0 : FD_TEST( fd_solfuzz_pb_restore_features( &runner->bank->f.features, &txn_bank->features ) );
70 :
71 0 : for( ulong i=0UL; i<test_ctx->account_shared_data_count; i++ ) {
72 0 : fd_solfuzz_pb_load_account( runner->runtime, accdb, runner->bank->accdb_fork_id, &test_ctx->account_shared_data[i], i );
73 0 : }
74 :
75 0 : runner->bank->f.ticks_per_slot = 64;
76 0 : runner->bank->f.slot_params = FD_SLOT_PARAMS_400MS;
77 0 : runner->bank->f.slot_params.slots_per_year = SECONDS_PER_YEAR * (1000000000.0 / (double)6250000) / (double)(runner->bank->f.ticks_per_slot);
78 0 : runner->bank->f.slot_params_default = runner->bank->f.slot_params;
79 :
80 0 : fd_sysvar_cache_restore_fuzz( runner->bank, runner->accdb );
81 :
82 0 : FD_TEST( fd_sysvar_cache_epoch_schedule_read( &runner->bank->f.sysvar_cache, &runner->bank->f.epoch_schedule ) );
83 0 : runner->bank->f.epoch = fd_slot_to_epoch( &runner->bank->f.epoch_schedule, slot, NULL );
84 :
85 0 : FD_TEST( fd_sysvar_cache_rent_read( &runner->bank->f.sysvar_cache, &runner->bank->f.rent ) );
86 :
87 : /* Initialize cost tracker */
88 0 : fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_modify( runner->bank );
89 0 : fd_cost_tracker_init( cost_tracker, &runner->bank->f.features, &runner->bank->f.slot_params, slot );
90 :
91 0 : fd_txn_p_t * txns = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), txn_cnt*sizeof(fd_txn_p_t) );
92 0 : fd_memset( txns, 0, txn_cnt*sizeof(fd_txn_p_t) );
93 :
94 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
95 0 : ulong msg_sz = fd_solfuzz_pb_txn_serialize( txns[i].payload, &test_ctx->txns[i] );
96 0 : if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) return NULL;
97 0 : if( FD_UNLIKELY( !fd_txn_parse( txns[i].payload, msg_sz, TXN( &txns[i] ), NULL ) ) ) return NULL;
98 0 : txns[i].payload_sz = msg_sz;
99 0 : }
100 :
101 0 : *out_txn_cnt = txn_cnt;
102 0 : return txns;
103 0 : }
104 :
105 : static void
106 : fd_solfuzz_bundle_mark_uncommittable( fd_txn_out_t * txn_outs,
107 0 : ulong txn_cnt ) {
108 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
109 0 : txn_outs[i].err.is_committable = 0;
110 0 : }
111 0 : }
112 :
113 : /* Record the latest stake and vote account updates only. */
114 :
115 : static fd_exec_test_stake_delta_t *
116 : fd_solfuzz_bundle_stake_delta_upsert( fd_exec_test_bundle_effects_t * effects,
117 0 : fd_pubkey_t const * key ) {
118 0 : for( ulong i=0UL; i<effects->stake_deltas_count; i++ ) {
119 0 : if( fd_memeq( effects->stake_deltas[i].address, key, sizeof(fd_pubkey_t) ) ) return &effects->stake_deltas[i];
120 0 : }
121 0 : fd_exec_test_stake_delta_t * stake_delta = &effects->stake_deltas[effects->stake_deltas_count++];
122 0 : fd_memcpy( stake_delta->address, key, sizeof(fd_pubkey_t) );
123 0 : return stake_delta;
124 0 : }
125 :
126 : static fd_exec_test_vote_update_t *
127 : fd_solfuzz_bundle_vote_update_upsert( fd_exec_test_bundle_effects_t * effects,
128 0 : fd_pubkey_t const * key ) {
129 0 : for( ulong i=0UL; i<effects->vote_updates_count; i++ ) {
130 0 : if( fd_memeq( effects->vote_updates[i].address, key, sizeof(fd_pubkey_t) ) ) return &effects->vote_updates[i];
131 0 : }
132 0 : fd_exec_test_vote_update_t * vote_update = &effects->vote_updates[effects->vote_updates_count++];
133 0 : fd_memcpy( vote_update->address, key, sizeof(fd_pubkey_t) );
134 0 : return vote_update;
135 0 : }
136 :
137 : static int
138 : fd_solfuzz_bundle_execute( fd_solfuzz_runner_t * runner,
139 : fd_exec_test_bundle_context_t const * input,
140 : int is_bundle,
141 : void * output_buf,
142 : ulong output_bufsz,
143 : fd_exec_test_bundle_effects_t ** effects_out,
144 0 : ulong * output_used ) {
145 0 : ulong txn_cnt = 0UL;
146 0 : fd_txn_p_t * txns = fd_solfuzz_pb_bundle_ctx_create( runner, input, &txn_cnt );
147 0 : if( FD_UNLIKELY( !txns ) ) {
148 0 : fd_solfuzz_bundle_ctx_destroy( runner );
149 0 : return 0;
150 0 : }
151 :
152 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
153 0 : ulong output_end = (ulong)output_buf + output_bufsz;
154 :
155 0 : fd_exec_test_bundle_effects_t * effects =
156 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_bundle_effects_t),
157 0 : sizeof(fd_exec_test_bundle_effects_t) );
158 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
159 0 : fd_memset( effects, 0, sizeof(fd_exec_test_bundle_effects_t) );
160 :
161 0 : effects->txn_results_count = (pb_size_t)txn_cnt;
162 0 : effects->txn_results = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_txn_result_t),
163 0 : txn_cnt*sizeof(fd_exec_test_txn_result_t) );
164 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
165 0 : fd_memset( effects->txn_results, 0, txn_cnt*sizeof(fd_exec_test_txn_result_t) );
166 :
167 0 : ulong update_max = txn_cnt*MAX_TX_ACCOUNT_LOCKS;
168 0 : effects->stake_deltas = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_stake_delta_t),
169 0 : update_max*sizeof(fd_exec_test_stake_delta_t) );
170 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
171 0 : fd_memset( effects->stake_deltas, 0, update_max*sizeof(fd_exec_test_stake_delta_t) );
172 0 : effects->stake_deltas_count = 0UL;
173 :
174 0 : effects->vote_updates = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_vote_update_t),
175 0 : update_max*sizeof(fd_exec_test_vote_update_t) );
176 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
177 0 : fd_memset( effects->vote_updates, 0, update_max*sizeof(fd_exec_test_vote_update_t) );
178 0 : effects->vote_updates_count = 0UL;
179 :
180 0 : effects->new_votes = NULL;
181 0 : effects->new_votes_count = 0UL;
182 :
183 0 : fd_runtime_t * runtime = runner->runtime;
184 0 : fd_txn_out_t * txn_outs = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), txn_cnt*sizeof(fd_txn_out_t) );
185 0 : fd_txn_in_t * txn_ins = fd_spad_alloc( runner->spad, alignof(fd_txn_in_t), txn_cnt*sizeof(fd_txn_in_t) );
186 0 : fd_log_collector_t * logs = fd_spad_alloc( runner->spad, alignof(fd_log_collector_t), txn_cnt*sizeof(fd_log_collector_t) );
187 0 : ulong ran_cnt = 0UL;
188 0 : int saw_exec_err = 0;
189 :
190 : /* Set up every txn_in up-front. For bundles, acquire all of the
191 : bundle's accounts in a single acquire_a/acquire_b pair before any
192 : txn executes; each txn then binds to this shared pool, which is
193 : released once after the bundle is committed or cancelled. */
194 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
195 0 : txn_ins[i] = (fd_txn_in_t){0};
196 0 : txn_ins[i].txn = &txns[i];
197 0 : txn_ins[i].bundle.is_bundle = is_bundle;
198 0 : txn_ins[i].bundle.prev_txn_cnt = is_bundle ? i : 0UL;
199 0 : for( ulong j=0UL; is_bundle && j<i; j++ ) txn_ins[i].bundle.prev_txn_outs[j] = &txn_outs[j];
200 0 : }
201 : /* Mirror fd_execle_tile: a failed bundle prepare acquires nothing, so
202 : every txn fails and fd_runtime_fini_bundle must be skipped. An
203 : empty bundle likewise acquires nothing and must skip fini. */
204 0 : int bundle_prep_ok = 1;
205 0 : if( is_bundle ) {
206 0 : runtime->accdb = runner->accdb;
207 0 : if( FD_LIKELY( txn_cnt ) ) {
208 0 : int prepare_err = fd_runtime_prepare_bundle_accounts( runtime, runner->bank, txn_ins, txn_outs, txn_cnt );
209 0 : if( FD_UNLIKELY( prepare_err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
210 0 : bundle_prep_ok = 0;
211 0 : saw_exec_err = 1;
212 0 : }
213 0 : }
214 0 : }
215 :
216 0 : for( ulong i=0UL; bundle_prep_ok && i<txn_cnt; i++ ) {
217 0 : fd_txn_in_t * txn_in = &txn_ins[i];
218 :
219 0 : int exec_res = 0;
220 0 : runtime->log.log_collector = &logs[i];
221 0 : fd_solfuzz_txn_ctx_exec( runner, runtime, txn_in, &exec_res, &txn_outs[i], 1 );
222 0 : ran_cnt = i+1UL;
223 :
224 0 : if( exec_res!=FD_RUNTIME_EXECUTE_SUCCESS ) {
225 0 : saw_exec_err = 1;
226 0 : if( is_bundle ) {
227 0 : fd_solfuzz_bundle_mark_uncommittable( txn_outs, ran_cnt );
228 0 : } else {
229 0 : txn_outs[i].err.is_committable = 0;
230 0 : fd_runtime_cancel_txn( runtime, NULL, NULL, &txn_outs[i], 0 );
231 0 : }
232 0 : break;
233 0 : }
234 :
235 0 : fd_exec_test_txn_result_t * txn_result = NULL;
236 0 : ulong txn_result_sz = create_txn_result_protobuf_from_txn(
237 0 : &txn_result,
238 0 : (void *)_l,
239 0 : output_end - _l,
240 0 : txn_in,
241 0 : &txn_outs[i],
242 0 : exec_res );
243 0 : FD_TEST( txn_result_sz );
244 0 : FD_TEST( txn_result );
245 0 : effects->txn_results[i] = *txn_result;
246 0 : _l += txn_result_sz;
247 :
248 0 : for( ulong j=0UL; j<txn_outs[i].accounts.cnt; j++ ) {
249 0 : if( txn_outs[i].accounts.stake_update[j] ) {
250 0 : fd_exec_test_stake_delta_t * stake_delta = fd_solfuzz_bundle_stake_delta_upsert( effects, &txn_outs[i].accounts.keys[j] );
251 0 : stake_delta->new_stake = 0UL;
252 :
253 0 : fd_stake_state_t const * stake_state = fd_stakes_get_state( txn_outs[i].accounts.account[j] );
254 0 : if( stake_state && stake_state->stake_type==FD_STAKE_STATE_STAKE ) {
255 0 : stake_delta->new_stake = stake_state->stake.stake.delegation.stake;
256 0 : }
257 0 : }
258 :
259 0 : if( txn_outs[i].accounts.vote_update[j] ) {
260 0 : fd_vote_block_timestamp_t last_vote;
261 0 : if( !fd_vote_account_last_timestamp( txn_outs[i].accounts.account[j]->data,
262 0 : txn_outs[i].accounts.account[j]->data_len,
263 0 : &last_vote ) ) {
264 0 : fd_exec_test_vote_update_t * vote_update = fd_solfuzz_bundle_vote_update_upsert( effects, &txn_outs[i].accounts.keys[j] );
265 0 : vote_update->last_vote_slot = last_vote.slot;
266 0 : vote_update->last_vote_timestamp = (ulong)last_vote.timestamp;
267 0 : }
268 0 : }
269 0 : }
270 :
271 0 : if( !is_bundle ) fd_runtime_commit_txn( runtime, runner->bank, NULL, &txn_outs[i], 0 );
272 0 : }
273 :
274 0 : if( is_bundle && !saw_exec_err ) {
275 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
276 0 : fd_runtime_commit_txn( runtime, runner->bank, NULL, &txn_outs[i], 0 );
277 0 : }
278 0 : }
279 :
280 : /* Release the bundle's shared account pool exactly once now that all of
281 : its txns have been committed or cancelled. */
282 0 : if( is_bundle && bundle_prep_ok && txn_cnt ) fd_runtime_fini_bundle( runtime );
283 :
284 0 : effects->has_error = saw_exec_err;
285 0 : if( saw_exec_err ) {
286 0 : effects->txn_results_count = 0UL;
287 0 : effects->txn_results = NULL;
288 0 : effects->stake_deltas_count = 0UL;
289 0 : effects->stake_deltas = NULL;
290 0 : effects->vote_updates_count = 0UL;
291 0 : effects->vote_updates = NULL;
292 0 : effects->new_votes_count = 0UL;
293 0 : effects->new_votes = NULL;
294 0 : }
295 :
296 0 : *effects_out = effects;
297 0 : *output_used = FD_SCRATCH_ALLOC_FINI( l, 1UL ) - (ulong)output_buf;
298 :
299 0 : fd_solfuzz_bundle_ctx_destroy( runner );
300 0 : return 1;
301 0 : }
302 :
303 : static void
304 : fd_solfuzz_bundle_assert_same_success( fd_solfuzz_runner_t * runner,
305 : fd_exec_test_bundle_effects_t * regular,
306 0 : fd_exec_test_bundle_effects_t * bundle ) {
307 0 : ulong buf_sz = 100000000UL;
308 0 : uchar * regular_buf = fd_spad_alloc( runner->spad, 1UL, buf_sz );
309 0 : uchar * bundle_buf = fd_spad_alloc( runner->spad, 1UL, buf_sz );
310 :
311 0 : ulong regular_sz = buf_sz;
312 0 : ulong bundle_sz = buf_sz;
313 0 : FD_TEST( sol_compat_encode( regular_buf, ®ular_sz, regular, &fd_exec_test_bundle_effects_t_msg ) );
314 0 : FD_TEST( sol_compat_encode( bundle_buf, &bundle_sz, bundle, &fd_exec_test_bundle_effects_t_msg ) );
315 :
316 0 : if( FD_UNLIKELY( regular_sz!=bundle_sz || !fd_memeq( regular_buf, bundle_buf, regular_sz ) ) ) {
317 0 : FD_LOG_ERR(( "bundle transaction effects mismatch" ));
318 0 : }
319 0 : }
320 :
321 : ulong
322 : fd_solfuzz_pb_bundle_run( fd_solfuzz_runner_t * runner,
323 : void const * input_,
324 : void ** output_,
325 : void * output_buf,
326 0 : ulong output_bufsz ) {
327 0 : fd_exec_test_bundle_context_t const * input = fd_type_pun_const( input_ );
328 0 : fd_exec_test_bundle_effects_t ** output = fd_type_pun( output_ );
329 :
330 0 : FD_SPAD_FRAME_BEGIN( runner->spad ) {
331 0 : void * regular_buf = fd_spad_alloc( runner->spad, 1UL, output_bufsz );
332 :
333 0 : fd_exec_test_bundle_effects_t * regular_effects = NULL;
334 0 : fd_exec_test_bundle_effects_t * bundle_effects = NULL;
335 0 : ulong regular_sz = 0UL;
336 0 : ulong bundle_sz = 0UL;
337 :
338 0 : int regular_ok = fd_solfuzz_bundle_execute( runner, input, 0, regular_buf, output_bufsz, ®ular_effects, ®ular_sz );
339 0 : int bundle_ok = fd_solfuzz_bundle_execute( runner, input, 1, output_buf, output_bufsz, &bundle_effects, &bundle_sz );
340 :
341 0 : if( FD_UNLIKELY( regular_ok!=bundle_ok ) ) {
342 0 : FD_LOG_ERR(( "bundle harness setup parity mismatch: regular_ok=%d bundle_ok=%d", regular_ok, bundle_ok ));
343 0 : }
344 0 : if( FD_UNLIKELY( !regular_ok ) ) {
345 0 : return 0UL;
346 0 : }
347 :
348 0 : (void)regular_sz;
349 0 : if( FD_UNLIKELY( regular_effects->has_error!=bundle_effects->has_error ) ) {
350 0 : FD_LOG_ERR(( "bundle success parity mismatch: regular_has_error=%d bundle_has_error=%d",
351 0 : regular_effects->has_error, bundle_effects->has_error ));
352 0 : }
353 :
354 0 : if( !regular_effects->has_error ) {
355 0 : fd_solfuzz_bundle_assert_same_success( runner, regular_effects, bundle_effects );
356 0 : }
357 :
358 0 : *output = bundle_effects;
359 0 : return bundle_sz;
360 0 : } FD_SPAD_FRAME_END;
361 0 : }
|