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_runtime.h"
7 : #include "../sysvar/fd_sysvar_cache.h"
8 : #include "../sysvar/fd_sysvar_epoch_schedule.h"
9 : #include "../../accdb/fd_accdb_admin_v1.h"
10 : #include "../../accdb/fd_accdb_impl_v1.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 : if( runner->bank->new_votes_fork_id!=USHORT_MAX ) {
18 0 : fd_new_votes_evict_fork( fd_bank_new_votes( runner->bank ), runner->bank->new_votes_fork_id );
19 0 : runner->bank->new_votes_fork_id = USHORT_MAX;
20 0 : }
21 0 : fd_banks_stake_delegations_evict_bank_fork( runner->banks, runner->bank );
22 :
23 0 : fd_accdb_v1_clear( runner->accdb_admin );
24 0 : fd_progcache_reset( runner->progcache->join );
25 :
26 : /* Keep the runner reusable across many bundle inputs. */
27 0 : fd_alloc_compact( fd_accdb_user_v1_funk( runner->accdb )->alloc );
28 0 : fd_alloc_compact( runner->progcache->join->alloc );
29 0 : }
30 :
31 : static fd_txn_p_t *
32 : fd_solfuzz_pb_bundle_ctx_create( fd_solfuzz_runner_t * runner,
33 : fd_exec_test_bundle_context_t const * test_ctx,
34 0 : ulong * out_txn_cnt ) {
35 0 : ulong txn_cnt = (ulong)test_ctx->txns_count;
36 0 : FD_TEST( txn_cnt<=FD_PACK_MAX_TXN_PER_BUNDLE );
37 :
38 0 : fd_accdb_user_t * accdb = runner->accdb;
39 :
40 0 : fd_banks_clear_bank( runner->banks, runner->bank, 64UL );
41 0 : ulong slot = fd_solfuzz_pb_get_slot( test_ctx->account_shared_data, test_ctx->account_shared_data_count );
42 0 : runner->bank->f.slot = slot;
43 :
44 0 : fd_funk_txn_xid_t xid = fd_bank_xid( runner->bank );
45 0 : fd_funk_txn_xid_t parent_xid; fd_funk_txn_xid_set_root( &parent_xid );
46 0 : fd_accdb_attach_child( runner->accdb_admin, &parent_xid, &xid );
47 0 : runner->bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, fd_progcache_fork_id_initial() );
48 :
49 0 : FD_TEST( test_ctx->has_bank );
50 0 : fd_exec_test_txn_bank_t const * txn_bank = &test_ctx->bank;
51 :
52 0 : fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( runner->banks );
53 0 : runner->bank->stake_delegations_fork_id = fd_stake_delegations_new_fork( stake_delegations );
54 0 : runner->bank->new_votes_fork_id = fd_new_votes_new_fork( fd_bank_new_votes( runner->bank ) );
55 :
56 0 : fd_solfuzz_pb_restore_blockhash_queue( runner->bank, txn_bank->blockhash_queue, txn_bank->blockhash_queue_count );
57 0 : runner->bank->f.rbh_lamports_per_sig = txn_bank->rbh_lamports_per_signature;
58 :
59 0 : FD_TEST( txn_bank->has_fee_rate_governor );
60 0 : fd_solfuzz_pb_restore_fee_rate_governor( runner->bank, &txn_bank->fee_rate_governor );
61 :
62 0 : runner->bank->f.parent_slot = slot-1UL;
63 0 : runner->bank->f.total_epoch_stake = txn_bank->total_epoch_stake;
64 :
65 0 : FD_TEST( txn_bank->has_epoch_schedule );
66 0 : fd_solfuzz_pb_restore_epoch_schedule( runner->bank, &txn_bank->epoch_schedule );
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 : runner->bank->f.epoch = fd_slot_to_epoch( &runner->bank->f.epoch_schedule, slot, NULL );
72 :
73 0 : for( ulong i=0UL; i<test_ctx->account_shared_data_count; i++ ) {
74 0 : fd_solfuzz_pb_load_account( runner->runtime, accdb, &xid, &test_ctx->account_shared_data[i], i );
75 0 : }
76 :
77 0 : runner->bank->f.ticks_per_slot = 64;
78 0 : runner->bank->f.slots_per_year = SECONDS_PER_YEAR * (1000000000.0 / (double)6250000) / (double)(runner->bank->f.ticks_per_slot);
79 :
80 0 : fd_sysvar_cache_restore_fuzz( runner->bank, runner->accdb, &xid );
81 0 : FD_TEST( fd_sysvar_cache_rent_read( &runner->bank->f.sysvar_cache, &runner->bank->f.rent ) );
82 :
83 0 : fd_txn_p_t * txns = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), txn_cnt*sizeof(fd_txn_p_t) );
84 0 : fd_memset( txns, 0, txn_cnt*sizeof(fd_txn_p_t) );
85 :
86 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
87 0 : ulong msg_sz = fd_solfuzz_pb_txn_serialize( txns[i].payload, &test_ctx->txns[i] );
88 0 : if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) return NULL;
89 0 : if( FD_UNLIKELY( !fd_txn_parse( txns[i].payload, msg_sz, TXN( &txns[i] ), NULL ) ) ) return NULL;
90 0 : txns[i].payload_sz = msg_sz;
91 0 : }
92 :
93 0 : *out_txn_cnt = txn_cnt;
94 0 : return txns;
95 0 : }
96 :
97 : static void
98 : fd_solfuzz_bundle_cancel_txns( fd_runtime_t * runtime,
99 : fd_txn_out_t * txn_outs,
100 0 : ulong txn_cnt ) {
101 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
102 0 : txn_outs[i].err.is_committable = 0;
103 0 : fd_runtime_cancel_txn( runtime, &txn_outs[i] );
104 0 : }
105 0 : }
106 :
107 : static int
108 : fd_solfuzz_bundle_execute( fd_solfuzz_runner_t * runner,
109 : fd_exec_test_bundle_context_t const * input,
110 : int is_bundle,
111 : void * output_buf,
112 : ulong output_bufsz,
113 : fd_exec_test_bundle_effects_t ** effects_out,
114 0 : ulong * output_used ) {
115 0 : ulong txn_cnt = 0UL;
116 0 : fd_txn_p_t * txns = fd_solfuzz_pb_bundle_ctx_create( runner, input, &txn_cnt );
117 0 : if( FD_UNLIKELY( !txns ) ) {
118 0 : fd_solfuzz_bundle_ctx_destroy( runner );
119 0 : return 0;
120 0 : }
121 :
122 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
123 0 : ulong output_end = (ulong)output_buf + output_bufsz;
124 :
125 0 : fd_exec_test_bundle_effects_t * effects =
126 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_bundle_effects_t),
127 0 : sizeof(fd_exec_test_bundle_effects_t) );
128 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
129 0 : fd_memset( effects, 0, sizeof(fd_exec_test_bundle_effects_t) );
130 :
131 0 : effects->txn_results_count = (pb_size_t)txn_cnt;
132 0 : effects->txn_results = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_txn_result_t),
133 0 : txn_cnt*sizeof(fd_exec_test_txn_result_t) );
134 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
135 0 : fd_memset( effects->txn_results, 0, txn_cnt*sizeof(fd_exec_test_txn_result_t) );
136 :
137 0 : ulong update_max = txn_cnt*MAX_TX_ACCOUNT_LOCKS;
138 0 : effects->stake_deltas = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_stake_delta_t),
139 0 : update_max*sizeof(fd_exec_test_stake_delta_t) );
140 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
141 0 : fd_memset( effects->stake_deltas, 0, update_max*sizeof(fd_exec_test_stake_delta_t) );
142 0 : effects->stake_deltas_count = 0UL;
143 :
144 0 : effects->vote_updates = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_vote_update_t),
145 0 : update_max*sizeof(fd_exec_test_vote_update_t) );
146 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
147 0 : fd_memset( effects->vote_updates, 0, update_max*sizeof(fd_exec_test_vote_update_t) );
148 0 : effects->vote_updates_count = 0UL;
149 :
150 0 : effects->new_votes = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_new_vote_t),
151 0 : update_max*sizeof(fd_exec_test_new_vote_t) );
152 0 : if( FD_UNLIKELY( _l>output_end ) ) abort();
153 0 : fd_memset( effects->new_votes, 0, update_max*sizeof(fd_exec_test_new_vote_t) );
154 0 : effects->new_votes_count = 0UL;
155 :
156 0 : fd_runtime_t * runtime = runner->runtime;
157 0 : fd_txn_out_t * txn_outs = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), txn_cnt*sizeof(fd_txn_out_t) );
158 0 : fd_log_collector_t * logs = fd_spad_alloc( runner->spad, alignof(fd_log_collector_t), txn_cnt*sizeof(fd_log_collector_t) );
159 0 : ulong ran_cnt = 0UL;
160 0 : int saw_exec_err = 0;
161 :
162 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
163 0 : fd_txn_in_t txn_in = {0};
164 0 : txn_in.txn = &txns[i];
165 0 : txn_in.bundle.is_bundle = is_bundle;
166 0 : txn_in.bundle.prev_txn_cnt = is_bundle ? i : 0UL;
167 0 : for( ulong j=0UL; is_bundle && j<i; j++ ) txn_in.bundle.prev_txn_outs[j] = &txn_outs[j];
168 :
169 0 : int exec_res = 0;
170 0 : runtime->log.log_collector = &logs[i];
171 0 : runtime->acc_pool = runner->acc_pool;
172 0 : fd_solfuzz_txn_ctx_exec( runner, runtime, &txn_in, &exec_res, &txn_outs[i], 1 );
173 0 : ran_cnt = i+1UL;
174 :
175 0 : if( exec_res!=FD_RUNTIME_EXECUTE_SUCCESS ) {
176 0 : saw_exec_err = 1;
177 0 : if( is_bundle ) fd_solfuzz_bundle_cancel_txns( runtime, txn_outs, ran_cnt );
178 0 : else fd_solfuzz_bundle_cancel_txns( runtime, &txn_outs[i], 1UL );
179 0 : break;
180 0 : }
181 :
182 0 : fd_exec_test_txn_result_t * txn_result = NULL;
183 0 : ulong txn_result_sz = create_txn_result_protobuf_from_txn(
184 0 : &txn_result,
185 0 : (void *)_l,
186 0 : output_end - _l,
187 0 : &txn_in,
188 0 : &txn_outs[i],
189 0 : exec_res );
190 0 : FD_TEST( txn_result_sz );
191 0 : FD_TEST( txn_result );
192 0 : effects->txn_results[i] = *txn_result;
193 0 : _l += txn_result_sz;
194 :
195 0 : for( ulong j=0UL; j<txn_outs[i].accounts.cnt; j++ ) {
196 0 : if( txn_outs[i].accounts.stake_update[j] ) {
197 0 : fd_exec_test_stake_delta_t * stake_delta = &effects->stake_deltas[effects->stake_deltas_count++];
198 0 : fd_memcpy( stake_delta->address, &txn_outs[i].accounts.keys[j], sizeof(fd_pubkey_t) );
199 0 : stake_delta->delta = 0UL;
200 :
201 0 : fd_stake_state_t const * stake_state = fd_stakes_get_state( txn_outs[i].accounts.account[j].meta );
202 0 : if( stake_state && stake_state->stake_type==FD_STAKE_STATE_STAKE ) {
203 0 : stake_delta->delta = stake_state->stake.stake.delegation.stake;
204 0 : }
205 0 : }
206 :
207 0 : if( txn_outs[i].accounts.vote_update[j] ) {
208 0 : fd_vote_block_timestamp_t last_vote;
209 0 : if( !fd_vote_account_last_timestamp( fd_account_data( txn_outs[i].accounts.account[j].meta ),
210 0 : txn_outs[i].accounts.account[j].meta->dlen,
211 0 : &last_vote ) ) {
212 0 : fd_exec_test_vote_update_t * vote_update = &effects->vote_updates[effects->vote_updates_count++];
213 0 : fd_memcpy( vote_update->address, &txn_outs[i].accounts.keys[j], sizeof(fd_pubkey_t) );
214 0 : vote_update->last_vote_slot = last_vote.slot;
215 0 : vote_update->last_vote_timestamp = (ulong)last_vote.timestamp;
216 0 : }
217 0 : }
218 0 : }
219 :
220 0 : if( !is_bundle ) fd_runtime_commit_txn( runtime, runner->bank, &txn_outs[i] );
221 0 : }
222 :
223 0 : if( is_bundle && !saw_exec_err ) {
224 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
225 0 : fd_runtime_commit_txn( runtime, runner->bank, &txn_outs[i] );
226 0 : }
227 0 : }
228 :
229 0 : effects->has_error = saw_exec_err;
230 0 : if( saw_exec_err ) {
231 0 : effects->txn_results_count = 0UL;
232 0 : effects->txn_results = NULL;
233 0 : effects->stake_deltas_count = 0UL;
234 0 : effects->stake_deltas = NULL;
235 0 : effects->vote_updates_count = 0UL;
236 0 : effects->vote_updates = NULL;
237 0 : effects->new_votes_count = 0UL;
238 0 : effects->new_votes = NULL;
239 0 : } else {
240 0 : ushort fork_idx = runner->bank->new_votes_fork_id;
241 0 : uchar iter_mem[ FD_NEW_VOTES_ITER_FOOTPRINT ] __attribute__((aligned(FD_NEW_VOTES_ITER_ALIGN)));
242 0 : fd_new_votes_iter_t * iter = fd_new_votes_iter_init( fd_bank_new_votes( runner->bank ), &fork_idx, 1UL, iter_mem );
243 0 : for( ; !fd_new_votes_iter_done( iter ); fd_new_votes_iter_next( iter ) ) {
244 0 : FD_TEST( effects->new_votes_count<update_max );
245 :
246 0 : fd_exec_test_new_vote_t * new_vote = &effects->new_votes[effects->new_votes_count++];
247 0 : int is_tombstone;
248 0 : fd_pubkey_t const * pubkey = fd_new_votes_iter_ele( iter, &is_tombstone );
249 0 : new_vote->is_tombstone = !!is_tombstone;
250 0 : fd_memcpy( new_vote->address, pubkey, sizeof(fd_pubkey_t) );
251 0 : }
252 0 : fd_new_votes_iter_fini( iter );
253 0 : }
254 :
255 0 : *effects_out = effects;
256 0 : *output_used = FD_SCRATCH_ALLOC_FINI( l, 1UL ) - (ulong)output_buf;
257 :
258 0 : fd_solfuzz_bundle_ctx_destroy( runner );
259 0 : return 1;
260 0 : }
261 :
262 : static void
263 : fd_solfuzz_bundle_assert_same_success( fd_solfuzz_runner_t * runner,
264 : fd_exec_test_bundle_effects_t * regular,
265 0 : fd_exec_test_bundle_effects_t * bundle ) {
266 0 : ulong buf_sz = 100000000UL;
267 0 : uchar * regular_buf = fd_spad_alloc( runner->spad, 1UL, buf_sz );
268 0 : uchar * bundle_buf = fd_spad_alloc( runner->spad, 1UL, buf_sz );
269 :
270 0 : ulong regular_sz = buf_sz;
271 0 : ulong bundle_sz = buf_sz;
272 0 : FD_TEST( sol_compat_encode( regular_buf, ®ular_sz, regular, &fd_exec_test_bundle_effects_t_msg ) );
273 0 : FD_TEST( sol_compat_encode( bundle_buf, &bundle_sz, bundle, &fd_exec_test_bundle_effects_t_msg ) );
274 :
275 0 : if( FD_UNLIKELY( regular_sz!=bundle_sz || !fd_memeq( regular_buf, bundle_buf, regular_sz ) ) ) {
276 0 : FD_LOG_ERR(( "bundle transaction effects mismatch" ));
277 0 : }
278 0 : }
279 :
280 : ulong
281 : fd_solfuzz_pb_bundle_run( fd_solfuzz_runner_t * runner,
282 : void const * input_,
283 : void ** output_,
284 : void * output_buf,
285 0 : ulong output_bufsz ) {
286 0 : fd_exec_test_bundle_context_t const * input = fd_type_pun_const( input_ );
287 0 : fd_exec_test_bundle_effects_t ** output = fd_type_pun( output_ );
288 :
289 0 : FD_SPAD_FRAME_BEGIN( runner->spad ) {
290 0 : void * regular_buf = fd_spad_alloc( runner->spad, 1UL, output_bufsz );
291 :
292 0 : fd_exec_test_bundle_effects_t * regular_effects = NULL;
293 0 : fd_exec_test_bundle_effects_t * bundle_effects = NULL;
294 0 : ulong regular_sz = 0UL;
295 0 : ulong bundle_sz = 0UL;
296 :
297 0 : int regular_ok = fd_solfuzz_bundle_execute( runner, input, 0, regular_buf, output_bufsz, ®ular_effects, ®ular_sz );
298 0 : int bundle_ok = fd_solfuzz_bundle_execute( runner, input, 1, output_buf, output_bufsz, &bundle_effects, &bundle_sz );
299 :
300 0 : if( FD_UNLIKELY( regular_ok!=bundle_ok ) ) {
301 0 : FD_LOG_ERR(( "bundle harness setup parity mismatch: regular_ok=%d bundle_ok=%d", regular_ok, bundle_ok ));
302 0 : }
303 0 : if( FD_UNLIKELY( !regular_ok ) ) {
304 0 : return 0UL;
305 0 : }
306 :
307 0 : (void)regular_sz;
308 0 : if( FD_UNLIKELY( regular_effects->has_error!=bundle_effects->has_error ) ) {
309 0 : FD_LOG_ERR(( "bundle success parity mismatch: regular_has_error=%d bundle_has_error=%d",
310 0 : regular_effects->has_error, bundle_effects->has_error ));
311 0 : }
312 :
313 0 : if( !regular_effects->has_error ) {
314 0 : fd_solfuzz_bundle_assert_same_success( runner, regular_effects, bundle_effects );
315 0 : }
316 :
317 0 : *output = bundle_effects;
318 0 : return bundle_sz;
319 0 : } FD_SPAD_FRAME_END;
320 0 : }
|