Line data Source code
1 : #include "fd_runtime.h"
2 : #include "context/fd_capture_ctx.h"
3 : #include "fd_acc_mgr.h"
4 : #include "fd_alut_interp.h"
5 : #include "fd_bank.h"
6 : #include "fd_executor_err.h"
7 : #include "fd_hashes.h"
8 : #include "fd_runtime_err.h"
9 : #include "fd_runtime_stack.h"
10 :
11 : #include "fd_executor.h"
12 : #include "sysvar/fd_sysvar_cache.h"
13 : #include "sysvar/fd_sysvar_clock.h"
14 : #include "sysvar/fd_sysvar_epoch_schedule.h"
15 : #include "sysvar/fd_sysvar_recent_hashes.h"
16 : #include "sysvar/fd_sysvar_stake_history.h"
17 :
18 : #include "../stakes/fd_stakes.h"
19 : #include "../rewards/fd_rewards.h"
20 : #include "../accdb/fd_accdb_impl_v1.h"
21 : #include "../progcache/fd_progcache_user.h"
22 :
23 : #include "program/fd_stake_program.h"
24 : #include "program/fd_builtin_programs.h"
25 :
26 : #include "sysvar/fd_sysvar_clock.h"
27 : #include "sysvar/fd_sysvar_last_restart_slot.h"
28 : #include "sysvar/fd_sysvar_recent_hashes.h"
29 : #include "sysvar/fd_sysvar_rent.h"
30 : #include "sysvar/fd_sysvar_slot_hashes.h"
31 : #include "sysvar/fd_sysvar_slot_history.h"
32 :
33 : #include "tests/fd_dump_pb.h"
34 :
35 : #include "fd_system_ids.h"
36 :
37 : #include "../../disco/pack/fd_pack.h"
38 : #include "../../disco/pack/fd_pack_tip_prog_blacklist.h"
39 : #include "../../disco/genesis/fd_genesis_cluster.h"
40 :
41 : #include <unistd.h>
42 : #include <sys/stat.h>
43 : #include <sys/types.h>
44 : #include <fcntl.h>
45 :
46 : /******************************************************************************/
47 : /* Public Runtime Helpers */
48 : /******************************************************************************/
49 :
50 :
51 : /*
52 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1254-L1258
53 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1749
54 : */
55 : int
56 : fd_runtime_compute_max_tick_height( ulong ticks_per_slot,
57 : ulong slot,
58 0 : ulong * out_max_tick_height /* out */ ) {
59 0 : ulong max_tick_height = 0UL;
60 0 : if( FD_LIKELY( ticks_per_slot > 0UL ) ) {
61 0 : ulong next_slot = fd_ulong_sat_add( slot, 1UL );
62 0 : if( FD_UNLIKELY( next_slot == slot ) ) {
63 0 : FD_LOG_WARNING(( "max tick height addition overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
64 0 : return -1;
65 0 : }
66 0 : if( FD_UNLIKELY( ULONG_MAX / ticks_per_slot < next_slot ) ) {
67 0 : FD_LOG_WARNING(( "max tick height multiplication overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
68 0 : return -1;
69 0 : }
70 0 : max_tick_height = fd_ulong_sat_mul( next_slot, ticks_per_slot );
71 0 : }
72 0 : *out_max_tick_height = max_tick_height;
73 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
74 0 : }
75 :
76 : /* Returns whether the specified epoch should use the new vote account
77 : keyed leader schedule (returns 1) or the old validator identity keyed
78 : leader schedule (returns 0). See SIMD-0180. This is the analogous to
79 : Agave's Bank::should_use_vote_keyed_leader_schedule():
80 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6148 */
81 :
82 : static int
83 0 : fd_runtime_should_use_vote_keyed_leader_schedule( fd_bank_t * bank ) {
84 : /* Agave uses an option type for their effective_epoch value. We
85 : represent None as ULONG_MAX and Some(value) as the value.
86 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6149-L6165 */
87 0 : if( FD_FEATURE_ACTIVE_BANK( bank, enable_vote_address_leader_schedule ) ) {
88 : /* Return the first epoch if activated at genesis
89 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6153-L6157 */
90 0 : ulong activation_slot = fd_bank_features_query( bank )->enable_vote_address_leader_schedule;
91 0 : if( activation_slot==0UL ) return 1; /* effective_epoch=0, current_epoch >= effective_epoch always true */
92 :
93 : /* Calculate the epoch that the feature became activated in
94 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6159-L6160 */
95 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
96 0 : ulong activation_epoch = fd_slot_to_epoch( epoch_schedule, activation_slot, NULL );
97 :
98 : /* The effective epoch is the epoch immediately after the activation
99 : epoch.
100 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6162-L6164 */
101 0 : ulong effective_epoch = activation_epoch + 1UL;
102 0 : ulong current_epoch = fd_bank_epoch_get( bank );
103 :
104 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6167-L6170 */
105 0 : return !!( current_epoch >= effective_epoch );
106 0 : }
107 :
108 : /* ...The rest of the logic in this function either returns None or
109 : Some(false) so we will just return 0 by default. */
110 0 : return 0;
111 0 : }
112 :
113 : void
114 : fd_runtime_update_leaders( fd_bank_t * bank,
115 0 : fd_runtime_stack_t * runtime_stack ) {
116 :
117 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
118 :
119 0 : ulong epoch = fd_slot_to_epoch ( epoch_schedule, fd_bank_slot_get( bank ), NULL );
120 0 : ulong slot0 = fd_epoch_slot0 ( epoch_schedule, epoch );
121 0 : ulong slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
122 :
123 0 : fd_vote_states_t const * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_query( bank );
124 0 : fd_vote_stake_weight_t * epoch_weights = runtime_stack->stakes.stake_weights;
125 0 : ulong stake_weight_cnt = fd_stake_weights_by_node( vote_states_prev_prev, epoch_weights );
126 0 : fd_bank_vote_states_prev_prev_end_locking_query( bank );
127 :
128 : /* Derive leader schedule */
129 :
130 0 : ulong epoch_leaders_footprint = fd_epoch_leaders_footprint( stake_weight_cnt, slot_cnt );
131 0 : if( FD_LIKELY( epoch_leaders_footprint ) ) {
132 0 : if( FD_UNLIKELY( stake_weight_cnt>MAX_PUB_CNT ) ) {
133 0 : FD_LOG_ERR(( "Stake weight count exceeded max" ));
134 0 : }
135 0 : if( FD_UNLIKELY( slot_cnt>MAX_SLOTS_PER_EPOCH ) ) {
136 0 : FD_LOG_ERR(( "Slot count exceeeded max" ));
137 0 : }
138 :
139 0 : ulong vote_keyed_lsched = (ulong)fd_runtime_should_use_vote_keyed_leader_schedule( bank );
140 0 : void * epoch_leaders_mem = fd_bank_epoch_leaders_locking_modify( bank );
141 0 : fd_epoch_leaders_t * leaders = fd_epoch_leaders_join( fd_epoch_leaders_new(
142 0 : epoch_leaders_mem,
143 0 : epoch,
144 0 : slot0,
145 0 : slot_cnt,
146 0 : stake_weight_cnt,
147 0 : epoch_weights,
148 0 : 0UL,
149 0 : vote_keyed_lsched ) );
150 0 : if( FD_UNLIKELY( !leaders ) ) {
151 0 : FD_LOG_ERR(( "Unable to init and join fd_epoch_leaders" ));
152 0 : }
153 0 : fd_bank_epoch_leaders_end_locking_modify( bank );
154 0 : }
155 0 : }
156 :
157 : /******************************************************************************/
158 : /* Various Private Runtime Helpers */
159 : /******************************************************************************/
160 :
161 : /* fee to be deposited should be > 0
162 : Returns 0 if validation succeeds
163 : Returns the amount to burn(==fee) on failure */
164 : static ulong
165 : fd_runtime_validate_fee_collector( fd_bank_t * bank,
166 : fd_txn_account_t const * collector,
167 0 : ulong fee ) {
168 0 : if( FD_UNLIKELY( fee<=0UL ) ) {
169 0 : FD_LOG_ERR(( "expected fee(%lu) to be >0UL", fee ));
170 0 : }
171 :
172 0 : if( FD_UNLIKELY( memcmp( fd_txn_account_get_owner( collector ), fd_solana_system_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
173 0 : FD_BASE58_ENCODE_32_BYTES( collector->pubkey->key, _out_key );
174 0 : FD_LOG_WARNING(( "cannot pay a non-system-program owned account (%s)", _out_key ));
175 0 : return fee;
176 0 : }
177 :
178 : /* https://github.com/anza-xyz/agave/blob/v1.18.23/runtime/src/bank/fee_distribution.rs#L111
179 : https://github.com/anza-xyz/agave/blob/v1.18.23/runtime/src/accounts/account_rent_state.rs#L39
180 : In agave's fee deposit code, rent state transition check logic is as follows:
181 : The transition is NOT allowed iff
182 : === BEGIN
183 : the post deposit account is rent paying AND the pre deposit account is not rent paying
184 : OR
185 : the post deposit account is rent paying AND the pre deposit account is rent paying AND !(post_data_size == pre_data_size && post_lamports <= pre_lamports)
186 : === END
187 : post_data_size == pre_data_size is always true during fee deposit.
188 : However, post_lamports > pre_lamports because we are paying a >0 amount.
189 : So, the above reduces down to
190 : === BEGIN
191 : the post deposit account is rent paying AND the pre deposit account is not rent paying
192 : OR
193 : the post deposit account is rent paying AND the pre deposit account is rent paying AND TRUE
194 : === END
195 : This is equivalent to checking that the post deposit account is rent paying.
196 : An account is rent paying if the post deposit balance is >0 AND it's not rent exempt.
197 : We already know that the post deposit balance is >0 because we are paying a >0 amount.
198 : So TLDR we just check if the account is rent exempt.
199 : */
200 0 : fd_rent_t const * rent = fd_bank_rent_query( bank );
201 0 : ulong minbal = fd_rent_exempt_minimum_balance( rent, fd_txn_account_get_data_len( collector ) );
202 0 : if( FD_UNLIKELY( fd_txn_account_get_lamports( collector )+fee<minbal ) ) {
203 0 : FD_BASE58_ENCODE_32_BYTES( collector->pubkey->key, _out_key );
204 0 : FD_LOG_WARNING(("cannot pay a rent paying account (%s)", _out_key ));
205 0 : return fee;
206 0 : }
207 :
208 0 : return 0UL;
209 0 : }
210 :
211 : static int
212 : fd_runtime_run_incinerator( fd_bank_t * bank,
213 : fd_accdb_user_t * accdb,
214 : fd_funk_txn_xid_t const * xid,
215 0 : fd_capture_ctx_t * capture_ctx ) {
216 0 : fd_txn_account_t rec[1];
217 0 : fd_funk_rec_prepare_t prepare = {0};
218 :
219 0 : int ok = !!fd_txn_account_init_from_funk_mutable(
220 0 : rec,
221 0 : &fd_sysvar_incinerator_id,
222 0 : accdb,
223 0 : xid,
224 0 : 0,
225 0 : 0UL,
226 0 : &prepare );
227 0 : if( FD_UNLIKELY( !ok ) ) {
228 : // TODO: not really an error! This is fine!
229 0 : return -1;
230 0 : }
231 :
232 0 : fd_lthash_value_t prev_hash[1];
233 0 : fd_hashes_account_lthash( rec->pubkey, fd_txn_account_get_meta( rec ), fd_txn_account_get_data( rec ), prev_hash );
234 :
235 0 : ulong new_capitalization = fd_ulong_sat_sub( fd_bank_capitalization_get( bank ), fd_txn_account_get_lamports( rec ) );
236 0 : fd_bank_capitalization_set( bank, new_capitalization );
237 :
238 0 : fd_txn_account_set_lamports( rec, 0UL );
239 0 : fd_hashes_update_lthash( rec, prev_hash, bank, capture_ctx );
240 0 : fd_txn_account_mutable_fini( rec, accdb, &prepare );
241 :
242 0 : return 0;
243 0 : }
244 :
245 : static void
246 : fd_runtime_freeze( fd_bank_t * bank,
247 : fd_accdb_user_t * accdb,
248 0 : fd_capture_ctx_t * capture_ctx ) {
249 :
250 0 : fd_funk_txn_xid_t const xid = { .ul = { fd_bank_slot_get( bank ), bank->idx } };
251 :
252 0 : if( FD_LIKELY( fd_bank_slot_get( bank ) != 0UL ) ) {
253 0 : fd_sysvar_recent_hashes_update( bank, accdb, &xid, capture_ctx );
254 0 : }
255 :
256 0 : fd_sysvar_slot_history_update( bank, accdb, &xid, capture_ctx );
257 :
258 0 : ulong execution_fees = fd_bank_execution_fees_get( bank );
259 0 : ulong priority_fees = fd_bank_priority_fees_get( bank );
260 :
261 0 : ulong burn = execution_fees / 2;
262 0 : ulong fees = fd_ulong_sat_add( priority_fees, execution_fees - burn );
263 :
264 0 : if( FD_LIKELY( fees ) ) {
265 : // Look at collect_fees... I think this was where I saw the fee payout..
266 0 : fd_txn_account_t rec[1];
267 :
268 0 : do {
269 : /* do_create=1 because we might wanna pay fees to a leader
270 : account that we've purged due to 0 balance. */
271 :
272 0 : fd_epoch_leaders_t const * leaders = fd_bank_epoch_leaders_locking_query( bank );
273 0 : if( FD_UNLIKELY( !leaders ) ) {
274 0 : FD_LOG_CRIT(( "fd_runtime_freeze: leaders not found" ));
275 0 : fd_bank_epoch_leaders_end_locking_query( bank );
276 0 : break;
277 0 : }
278 :
279 0 : fd_pubkey_t const * leader = fd_epoch_leaders_get( leaders, fd_bank_slot_get( bank ) );
280 0 : if( FD_UNLIKELY( !leader ) ) {
281 0 : FD_LOG_CRIT(( "fd_runtime_freeze: leader not found" ));
282 0 : fd_bank_epoch_leaders_end_locking_query( bank );
283 0 : break;
284 0 : }
285 :
286 0 : fd_funk_rec_prepare_t prepare = {0};
287 0 : int ok = !!fd_txn_account_init_from_funk_mutable(
288 0 : rec,
289 0 : leader,
290 0 : accdb,
291 0 : &xid,
292 0 : 1,
293 0 : 0UL,
294 0 : &prepare );
295 0 : if( FD_UNLIKELY( !ok ) ) {
296 0 : FD_LOG_WARNING(( "fd_runtime_freeze: fd_txn_account_init_from_funk_mutable for leader (%s) failed", FD_BASE58_ENC_32_ALLOCA( leader ) ));
297 0 : burn = fd_ulong_sat_add( burn, fees );
298 0 : fd_bank_epoch_leaders_end_locking_query( bank );
299 0 : break;
300 0 : }
301 :
302 0 : fd_lthash_value_t prev_hash[1];
303 0 : fd_hashes_account_lthash( leader, fd_txn_account_get_meta( rec ), fd_txn_account_get_data( rec ), prev_hash );
304 :
305 0 : fd_bank_epoch_leaders_end_locking_query( bank );
306 :
307 0 : if ( FD_LIKELY( FD_FEATURE_ACTIVE_BANK( bank, validate_fee_collector_account ) ) ) {
308 0 : ulong _burn;
309 0 : if( FD_UNLIKELY( _burn=fd_runtime_validate_fee_collector( bank, rec, fees ) ) ) {
310 0 : if( FD_UNLIKELY( _burn!=fees ) ) {
311 0 : FD_LOG_ERR(( "expected _burn(%lu)==fees(%lu)", _burn, fees ));
312 0 : }
313 0 : burn = fd_ulong_sat_add( burn, fees );
314 0 : FD_LOG_WARNING(("fd_runtime_freeze: burned %lu", fees ));
315 0 : break;
316 0 : }
317 0 : }
318 :
319 : /* TODO: is it ok to not check the overflow error here? */
320 0 : fd_txn_account_checked_add_lamports( rec, fees );
321 0 : fd_txn_account_set_slot( rec, fd_bank_slot_get( bank ) );
322 :
323 0 : fd_hashes_update_lthash( rec, prev_hash, bank, capture_ctx );
324 0 : fd_txn_account_mutable_fini( rec, accdb, &prepare );
325 :
326 0 : } while(0);
327 :
328 0 : ulong old = fd_bank_capitalization_get( bank );
329 0 : fd_bank_capitalization_set( bank, fd_ulong_sat_sub( old, burn ) );
330 0 : FD_LOG_DEBUG(( "fd_runtime_freeze: burn %lu, capitalization %lu->%lu ", burn, old, fd_bank_capitalization_get( bank ) ));
331 0 : }
332 :
333 : /* jito collects a 3% fee at the end of the block + 3% fee at
334 : distribution time. */
335 0 : fd_bank_tips_set( bank, (fd_bank_tips_get( bank ) * 6UL / 100UL) );
336 :
337 0 : fd_runtime_run_incinerator( bank, accdb, &xid, capture_ctx );
338 :
339 0 : }
340 :
341 : /******************************************************************************/
342 : /* Block-Level Execution Preparation/Finalization */
343 : /******************************************************************************/
344 : void
345 : fd_runtime_new_fee_rate_governor_derived( fd_bank_t * bank,
346 0 : ulong latest_signatures_per_slot ) {
347 :
348 0 : fd_fee_rate_governor_t const * base_fee_rate_governor = fd_bank_fee_rate_governor_query( bank );
349 :
350 0 : ulong old_lamports_per_signature = fd_bank_rbh_lamports_per_sig_get( bank );
351 :
352 0 : fd_fee_rate_governor_t me = {
353 0 : .target_signatures_per_slot = base_fee_rate_governor->target_signatures_per_slot,
354 0 : .target_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature,
355 0 : .max_lamports_per_signature = base_fee_rate_governor->max_lamports_per_signature,
356 0 : .min_lamports_per_signature = base_fee_rate_governor->min_lamports_per_signature,
357 0 : .burn_percent = base_fee_rate_governor->burn_percent
358 0 : };
359 :
360 0 : ulong new_lamports_per_signature = 0;
361 0 : if( me.target_signatures_per_slot > 0 ) {
362 0 : me.min_lamports_per_signature = fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 2) );
363 0 : me.max_lamports_per_signature = me.target_lamports_per_signature * 10;
364 0 : ulong desired_lamports_per_signature = fd_ulong_min(
365 0 : me.max_lamports_per_signature,
366 0 : fd_ulong_max(
367 0 : me.min_lamports_per_signature,
368 0 : me.target_lamports_per_signature
369 0 : * fd_ulong_min(latest_signatures_per_slot, (ulong)UINT_MAX)
370 0 : / me.target_signatures_per_slot
371 0 : )
372 0 : );
373 0 : long gap = (long)desired_lamports_per_signature - (long)old_lamports_per_signature;
374 0 : if ( gap == 0 ) {
375 0 : new_lamports_per_signature = desired_lamports_per_signature;
376 0 : } else {
377 0 : long gap_adjust = (long)(fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 20) ))
378 0 : * (gap != 0)
379 0 : * (gap > 0 ? 1 : -1);
380 0 : new_lamports_per_signature = fd_ulong_min(
381 0 : me.max_lamports_per_signature,
382 0 : fd_ulong_max(
383 0 : me.min_lamports_per_signature,
384 0 : (ulong)((long)old_lamports_per_signature + gap_adjust)
385 0 : )
386 0 : );
387 0 : }
388 0 : } else {
389 0 : new_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature;
390 0 : me.min_lamports_per_signature = me.target_lamports_per_signature;
391 0 : me.max_lamports_per_signature = me.target_lamports_per_signature;
392 0 : }
393 0 : fd_bank_fee_rate_governor_set( bank, me );
394 0 : fd_bank_rbh_lamports_per_sig_set( bank, new_lamports_per_signature );
395 0 : }
396 :
397 : /******************************************************************************/
398 : /* Epoch Boundary */
399 : /******************************************************************************/
400 :
401 : static void
402 0 : fd_runtime_refresh_previous_stake_values( fd_bank_t * bank ) {
403 0 : fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
404 0 : fd_vote_states_iter_t iter_[1];
405 0 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states );
406 0 : !fd_vote_states_iter_done( iter );
407 0 : fd_vote_states_iter_next( iter ) ) {
408 0 : fd_vote_state_ele_t * vote_state = fd_vote_states_iter_ele( iter );
409 0 : vote_state->stake_t_2 = vote_state->stake_t_1;
410 0 : vote_state->stake_t_1 = vote_state->stake;
411 0 : }
412 0 : fd_bank_vote_states_end_locking_modify( bank );
413 0 : }
414 :
415 : /* Replace the vote states for T-2 (vote_states_prev_prev) with the vote
416 : states for T-1 (vote_states_prev) */
417 :
418 : static void
419 0 : fd_runtime_update_vote_states_prev_prev( fd_bank_t * bank ) {
420 :
421 0 : fd_vote_states_t * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_modify( bank );
422 0 : fd_vote_states_t const * vote_states_prev = fd_bank_vote_states_prev_locking_query( bank );
423 0 : fd_memcpy( vote_states_prev_prev, vote_states_prev, fd_bank_vote_states_footprint );
424 0 : fd_bank_vote_states_prev_prev_end_locking_modify( bank );
425 0 : fd_bank_vote_states_prev_end_locking_query( bank );
426 0 : }
427 :
428 : /* Replace the vote states for T-1 (vote_states_prev) with the vote
429 : states for T-1 (vote_states) */
430 :
431 : static void
432 0 : fd_runtime_update_vote_states_prev( fd_bank_t * bank ) {
433 0 : fd_vote_states_t * vote_states_prev = fd_bank_vote_states_prev_locking_modify( bank );
434 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( bank );
435 0 : fd_memcpy( vote_states_prev, vote_states, fd_bank_vote_states_footprint );
436 0 : fd_bank_vote_states_prev_end_locking_modify( bank );
437 0 : fd_bank_vote_states_end_locking_query( bank );
438 0 : }
439 :
440 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6704 */
441 : static void
442 : fd_apply_builtin_program_feature_transitions( fd_bank_t * bank,
443 : fd_accdb_user_t * accdb,
444 : fd_funk_txn_xid_t const * xid,
445 : fd_runtime_stack_t * runtime_stack,
446 0 : fd_capture_ctx_t * capture_ctx ) {
447 : /* TODO: Set the upgrade authority properly from the core bpf migration config. Right now it's set to None.
448 :
449 : Migrate any necessary stateless builtins to core BPF. So far,
450 : the only "stateless" builtin is the Feature program. Beginning
451 : checks in the migrate_builtin_to_core_bpf function will fail if the
452 : program has already been migrated to BPF. */
453 :
454 0 : fd_builtin_program_t const * builtins = fd_builtins();
455 0 : for( ulong i=0UL; i<fd_num_builtins(); i++ ) {
456 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6732-L6751 */
457 0 : if( builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_query( bank ), builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
458 0 : FD_LOG_DEBUG(( "Migrating builtin program %s to core BPF", FD_BASE58_ENC_32_ALLOCA( builtins[i].pubkey->key ) ));
459 0 : fd_migrate_builtin_to_core_bpf( bank, accdb, xid, runtime_stack, builtins[i].core_bpf_migration_config, capture_ctx );
460 0 : }
461 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6753-L6774 */
462 0 : if( builtins[i].enable_feature_offset!=NO_ENABLE_FEATURE_ID && FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, builtins[i].enable_feature_offset ) ) {
463 0 : FD_LOG_DEBUG(( "Enabling builtin program %s", FD_BASE58_ENC_32_ALLOCA( builtins[i].pubkey->key ) ));
464 0 : fd_write_builtin_account( bank, accdb, xid, capture_ctx, *builtins[i].pubkey, builtins[i].data,strlen(builtins[i].data) );
465 0 : }
466 0 : }
467 :
468 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6776-L6793 */
469 0 : fd_stateless_builtin_program_t const * stateless_builtins = fd_stateless_builtins();
470 0 : for( ulong i=0UL; i<fd_num_stateless_builtins(); i++ ) {
471 0 : if( stateless_builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_query( bank ), stateless_builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
472 0 : FD_LOG_DEBUG(( "Migrating stateless builtin program %s to core BPF", FD_BASE58_ENC_32_ALLOCA( stateless_builtins[i].pubkey->key ) ));
473 0 : fd_migrate_builtin_to_core_bpf( bank, accdb, xid, runtime_stack, stateless_builtins[i].core_bpf_migration_config, capture_ctx );
474 0 : }
475 0 : }
476 :
477 : /* https://github.com/anza-xyz/agave/blob/c1080de464cfb578c301e975f498964b5d5313db/runtime/src/bank.rs#L6795-L6805 */
478 0 : fd_precompile_program_t const * precompiles = fd_precompiles();
479 0 : for( ulong i=0UL; i<fd_num_precompiles(); i++ ) {
480 0 : if( precompiles[i].feature_offset != NO_ENABLE_FEATURE_ID && FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, precompiles[i].feature_offset ) ) {
481 0 : fd_write_builtin_account( bank, accdb, xid, capture_ctx, *precompiles[i].pubkey, "", 0 );
482 0 : }
483 0 : }
484 0 : }
485 :
486 : static void
487 : fd_feature_activate( fd_bank_t * bank,
488 : fd_accdb_user_t * accdb,
489 : fd_funk_txn_xid_t const * xid,
490 : fd_capture_ctx_t * capture_ctx,
491 : fd_feature_id_t const * id,
492 0 : fd_pubkey_t const * addr ) {
493 0 : fd_features_t * features = fd_bank_features_modify( bank );
494 :
495 0 : if( id->reverted==1 ) return;
496 :
497 0 : fd_funk_t * funk = fd_accdb_user_v1_funk( accdb );
498 0 : fd_txn_account_t acct_rec[1];
499 0 : int err = fd_txn_account_init_from_funk_readonly( acct_rec, addr, funk, xid );
500 0 : if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) ) {
501 0 : return;
502 0 : }
503 :
504 0 : FD_BASE58_ENCODE_32_BYTES( addr->uc, addr_b58 );
505 0 : fd_feature_t feature[1];
506 0 : int decode_err = 0;
507 0 : if( FD_UNLIKELY( !fd_bincode_decode_static( feature, feature, fd_txn_account_get_data( acct_rec ), fd_txn_account_get_data_len( acct_rec ), &decode_err ) ) ) {
508 0 : FD_LOG_WARNING(( "Failed to decode feature account %s (%d)", addr_b58, decode_err ));
509 0 : return;
510 0 : }
511 :
512 0 : if( feature->has_activated_at ) {
513 0 : FD_LOG_DEBUG(( "feature already activated - acc: %s, slot: %lu", addr_b58, feature->activated_at ));
514 0 : fd_features_set( features, id, feature->activated_at);
515 0 : } else {
516 0 : FD_LOG_DEBUG(( "Feature %s not activated at %lu, activating", addr_b58, feature->activated_at ));
517 :
518 0 : fd_txn_account_t modify_acct_rec[1];
519 0 : fd_funk_rec_prepare_t modify_acct_prepare = {0};
520 0 : int ok = !!fd_txn_account_init_from_funk_mutable( modify_acct_rec, addr, accdb, xid, 0, 0UL, &modify_acct_prepare );
521 0 : if( FD_UNLIKELY( !ok ) ) return;
522 :
523 0 : fd_lthash_value_t prev_hash[1];
524 0 : fd_hashes_account_lthash(
525 0 : addr,
526 0 : fd_txn_account_get_meta( modify_acct_rec ),
527 0 : fd_txn_account_get_data( modify_acct_rec ),
528 0 : prev_hash );
529 :
530 0 : feature->has_activated_at = 1;
531 0 : feature->activated_at = fd_bank_slot_get( bank );
532 0 : fd_bincode_encode_ctx_t encode_ctx = {
533 0 : .data = fd_txn_account_get_data_mut( modify_acct_rec ),
534 0 : .dataend = fd_txn_account_get_data_mut( modify_acct_rec ) + fd_txn_account_get_data_len( modify_acct_rec ),
535 0 : };
536 0 : int encode_err = fd_feature_encode( feature, &encode_ctx );
537 0 : if( FD_UNLIKELY( encode_err != FD_BINCODE_SUCCESS ) ) {
538 0 : FD_LOG_ERR(( "Failed to encode feature account %s (%d)", addr_b58, decode_err ));
539 0 : }
540 :
541 0 : fd_hashes_update_lthash( modify_acct_rec, prev_hash, bank, capture_ctx );
542 0 : fd_txn_account_mutable_fini( modify_acct_rec, accdb, &modify_acct_prepare );
543 0 : }
544 0 : }
545 :
546 : static void
547 : fd_features_activate( fd_bank_t * bank,
548 : fd_accdb_user_t * accdb,
549 : fd_funk_txn_xid_t const * xid,
550 0 : fd_capture_ctx_t * capture_ctx ) {
551 0 : for( fd_feature_id_t const * id = fd_feature_iter_init();
552 0 : !fd_feature_iter_done( id );
553 0 : id = fd_feature_iter_next( id ) ) {
554 0 : fd_feature_activate( bank, accdb, xid, capture_ctx, id, &id->id );
555 0 : }
556 0 : }
557 :
558 : /* Starting a new epoch.
559 : New epoch: T
560 : Just ended epoch: T-1
561 : Epoch before: T-2
562 :
563 : In this function:
564 : - stakes in T-2 (vote_states_prev_prev) should be replaced by T-1 (vote_states_prev)
565 : - stakes at T-1 (vote_states_prev) should be replaced by updated stakes at T (vote_states)
566 : - leader schedule should be calculated using new T-2 stakes (vote_states_prev_prev)
567 :
568 : Invariant during an epoch T:
569 : vote_states_prev holds the stakes at T-1
570 : vote_states_prev_prev holds the stakes at T-2
571 : */
572 : /* process for the start of a new epoch */
573 : static void
574 : fd_runtime_process_new_epoch( fd_banks_t * banks,
575 : fd_bank_t * bank,
576 : fd_accdb_user_t * accdb,
577 : fd_funk_txn_xid_t const * xid,
578 : fd_capture_ctx_t * capture_ctx,
579 : ulong parent_epoch,
580 0 : fd_runtime_stack_t * runtime_stack ) {
581 :
582 0 : FD_LOG_NOTICE(( "fd_process_new_epoch start, epoch: %lu, slot: %lu", fd_bank_epoch_get( bank ), fd_bank_slot_get( bank ) ));
583 :
584 0 : runtime_stack->stakes.prev_vote_credits_used = 0;
585 :
586 0 : fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_frontier_query( banks, bank );
587 0 : if( FD_UNLIKELY( !stake_delegations ) ) {
588 0 : FD_LOG_CRIT(( "stake_delegations is NULL" ));
589 0 : }
590 :
591 0 : long start = fd_log_wallclock();
592 :
593 0 : ulong const slot = fd_bank_slot_get( bank );
594 :
595 : /* Activate new features
596 : https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6587-L6598 */
597 :
598 0 : fd_funk_t * funk = fd_accdb_user_v1_funk( accdb );
599 0 : fd_features_activate( bank, accdb, xid, capture_ctx );
600 0 : fd_features_restore( bank, funk, xid );
601 :
602 : /* Apply builtin program feature transitions
603 : https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6621-L6624 */
604 :
605 0 : fd_apply_builtin_program_feature_transitions( bank, accdb, xid, runtime_stack, capture_ctx );
606 :
607 : /* Get the new rate activation epoch */
608 0 : int _err[1];
609 0 : ulong new_rate_activation_epoch_val = 0UL;
610 0 : ulong * new_rate_activation_epoch = &new_rate_activation_epoch_val;
611 0 : int is_some = fd_new_warmup_cooldown_rate_epoch(
612 0 : fd_bank_epoch_schedule_query( bank ),
613 0 : fd_bank_features_query( bank ),
614 0 : slot,
615 0 : new_rate_activation_epoch,
616 0 : _err );
617 0 : if( FD_UNLIKELY( !is_some ) ) {
618 0 : new_rate_activation_epoch = NULL;
619 0 : }
620 :
621 : /* Updates stake history sysvar accumulated values and recomputes
622 : stake delegations for vote accounts. */
623 :
624 0 : fd_stakes_activate_epoch( bank, accdb, xid, capture_ctx, stake_delegations, new_rate_activation_epoch );
625 :
626 : /* Distribute rewards. This involves calculating the rewards for
627 : every vote and stake account. */
628 :
629 0 : fd_hash_t const * parent_blockhash = fd_blockhashes_peek_last_hash( fd_bank_block_hash_queue_query( bank ) );
630 0 : fd_begin_partitioned_rewards( bank,
631 0 : accdb,
632 0 : xid,
633 0 : runtime_stack,
634 0 : capture_ctx,
635 0 : stake_delegations,
636 0 : parent_blockhash,
637 0 : parent_epoch );
638 :
639 : /* The Agave client handles updating their stakes cache with a call to
640 : update_epoch_stakes() which keys stakes by the leader schedule
641 : epochs and retains up to 6 epochs of stakes. However, to correctly
642 : calculate the leader schedule, we just need to maintain the vote
643 : states for the current epoch, the previous epoch, and the one
644 : before that.
645 : https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/bank.rs#L2175
646 : */
647 :
648 : /* We want to cache the stake values for T-1 and T-2 in the forward
649 : looking vote states. This is done as an optimization for tower
650 : calculations (T-1 stake) and clock calculation (T-2 stake).
651 : We use the current stake to populate the T-1 stake and the T-1
652 : stake to populate the T-2 stake. */
653 0 : fd_runtime_refresh_previous_stake_values( bank );
654 :
655 : /* Update vote_states_prev_prev with vote_states_prev */
656 :
657 0 : fd_runtime_update_vote_states_prev_prev( bank );
658 :
659 : /* Update vote_states_prev with vote_states */
660 :
661 0 : fd_runtime_update_vote_states_prev( bank );
662 :
663 : /* Now that our stakes caches have been updated, we can calculate the
664 : leader schedule for the upcoming epoch epoch using our new
665 : vote_states_prev_prev (stakes for T-2). */
666 :
667 0 : fd_runtime_update_leaders( bank, runtime_stack );
668 :
669 0 : long end = fd_log_wallclock();
670 0 : FD_LOG_NOTICE(("fd_process_new_epoch took %ld ns", end - start));
671 :
672 0 : }
673 :
674 : static void
675 : fd_runtime_block_pre_execute_process_new_epoch( fd_banks_t * banks,
676 : fd_bank_t * bank,
677 : fd_accdb_user_t * accdb,
678 : fd_funk_txn_xid_t const * xid,
679 : fd_capture_ctx_t * capture_ctx,
680 : fd_runtime_stack_t * runtime_stack,
681 0 : int * is_epoch_boundary ) {
682 :
683 0 : ulong const slot = fd_bank_slot_get( bank );
684 0 : if( slot != 0UL ) {
685 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
686 :
687 0 : ulong prev_epoch = fd_slot_to_epoch( epoch_schedule, fd_bank_parent_slot_get( bank ), NULL );
688 0 : ulong slot_idx;
689 0 : ulong new_epoch = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
690 0 : if( FD_UNLIKELY( slot_idx==1UL && new_epoch==0UL ) ) {
691 : /* The block after genesis has a height of 1. */
692 0 : fd_bank_block_height_set( bank, 1UL );
693 0 : }
694 :
695 0 : if( FD_UNLIKELY( prev_epoch<new_epoch || !slot_idx ) ) {
696 0 : FD_LOG_DEBUG(( "Epoch boundary starting" ));
697 0 : fd_runtime_process_new_epoch( banks, bank, accdb, xid, capture_ctx, prev_epoch, runtime_stack );
698 0 : *is_epoch_boundary = 1;
699 0 : }
700 0 : } else {
701 0 : *is_epoch_boundary = 0;
702 0 : }
703 :
704 0 : if( FD_LIKELY( fd_bank_slot_get( bank )!=0UL ) ) {
705 0 : fd_distribute_partitioned_epoch_rewards( bank, accdb, xid, capture_ctx );
706 0 : }
707 0 : }
708 :
709 :
710 : static void
711 : fd_runtime_block_sysvar_update_pre_execute( fd_bank_t * bank,
712 : fd_accdb_user_t * accdb,
713 : fd_funk_txn_xid_t const * xid,
714 : fd_runtime_stack_t * runtime_stack,
715 0 : fd_capture_ctx_t * capture_ctx ) {
716 : // let (fee_rate_governor, fee_components_time_us) = measure_us!(
717 : // FeeRateGovernor::new_derived(&parent.fee_rate_governor, parent.signature_count())
718 : // );
719 : /* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/runtime/src/bank.rs#L1312-L1314 */
720 :
721 0 : fd_runtime_new_fee_rate_governor_derived( bank, fd_bank_parent_signature_cnt_get( bank ) );
722 :
723 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
724 0 : ulong parent_epoch = fd_slot_to_epoch( epoch_schedule, fd_bank_parent_slot_get( bank ), NULL );
725 0 : fd_sysvar_clock_update( bank, accdb, xid, capture_ctx, runtime_stack, &parent_epoch );
726 :
727 : // It has to go into the current txn previous info but is not in slot 0
728 0 : if( fd_bank_slot_get( bank ) != 0 ) {
729 0 : fd_sysvar_slot_hashes_update( bank, accdb, xid, capture_ctx );
730 0 : }
731 0 : fd_sysvar_last_restart_slot_update( bank, accdb, xid, capture_ctx, fd_bank_last_restart_slot_get( bank ).slot );
732 0 : }
733 :
734 : int
735 : fd_runtime_load_txn_address_lookup_tables(
736 : fd_txn_t const * txn,
737 : uchar const * payload,
738 : fd_funk_t * funk,
739 : fd_funk_txn_xid_t const * xid,
740 : ulong slot,
741 : fd_slot_hash_t const * hashes, /* deque */
742 0 : fd_acct_addr_t * out_accts_alt ) {
743 :
744 0 : if( FD_LIKELY( txn->transaction_version!=FD_TXN_V0 ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
745 :
746 0 : fd_alut_interp_t interp[1];
747 0 : fd_alut_interp_new(
748 0 : interp,
749 0 : out_accts_alt,
750 0 : txn,
751 0 : payload,
752 0 : hashes,
753 0 : slot );
754 :
755 0 : fd_txn_acct_addr_lut_t const * addr_luts = fd_txn_get_address_tables_const( txn );
756 0 : for( ulong i=0UL; i<txn->addr_table_lookup_cnt; i++ ) {
757 0 : fd_txn_acct_addr_lut_t const * addr_lut = &addr_luts[i];
758 0 : fd_pubkey_t addr_lut_acc = FD_LOAD( fd_pubkey_t, payload+addr_lut->addr_off );
759 :
760 : /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/accounts-db/src/accounts.rs#L90-L94 */
761 0 : fd_txn_account_t addr_lut_rec[1];
762 0 : int db_err = fd_txn_account_init_from_funk_readonly(
763 0 : addr_lut_rec, &addr_lut_acc, funk, xid );
764 0 : if( FD_UNLIKELY( db_err!=FD_ACC_MGR_SUCCESS ) ) {
765 0 : return FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND;
766 0 : }
767 :
768 0 : int err = fd_alut_interp_next(
769 0 : interp,
770 0 : &addr_lut_acc,
771 0 : fd_txn_account_get_owner ( addr_lut_rec ),
772 0 : fd_txn_account_get_data ( addr_lut_rec ),
773 0 : fd_txn_account_get_data_len( addr_lut_rec ) );
774 0 : if( FD_UNLIKELY( err ) ) return err;
775 0 : }
776 :
777 0 : fd_alut_interp_delete( interp );
778 :
779 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
780 0 : }
781 :
782 : void
783 : fd_runtime_block_execute_prepare( fd_banks_t * banks,
784 : fd_bank_t * bank,
785 : fd_accdb_user_t * accdb,
786 : fd_runtime_stack_t * runtime_stack,
787 : fd_capture_ctx_t * capture_ctx,
788 0 : int * is_epoch_boundary ) {
789 :
790 0 : fd_funk_txn_xid_t const xid = { .ul = { fd_bank_slot_get( bank ), bank->idx } };
791 :
792 0 : fd_runtime_block_pre_execute_process_new_epoch( banks, bank, accdb, &xid, capture_ctx, runtime_stack, is_epoch_boundary );
793 :
794 0 : fd_bank_execution_fees_set( bank, 0UL );
795 0 : fd_bank_priority_fees_set( bank, 0UL );
796 0 : fd_bank_signature_count_set( bank, 0UL );
797 0 : fd_bank_total_compute_units_used_set( bank, 0UL );
798 :
799 0 : if( FD_LIKELY( fd_bank_slot_get( bank ) ) ) {
800 0 : fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_locking_modify( bank );
801 0 : FD_TEST( cost_tracker );
802 0 : fd_cost_tracker_init( cost_tracker, fd_bank_features_query( bank ), fd_bank_slot_get( bank ) );
803 0 : fd_bank_cost_tracker_end_locking_modify( bank );
804 0 : }
805 :
806 0 : fd_runtime_block_sysvar_update_pre_execute( bank, accdb, &xid, runtime_stack, capture_ctx );
807 :
808 0 : fd_funk_t * funk = fd_accdb_user_v1_funk( accdb );
809 0 : if( FD_UNLIKELY( !fd_sysvar_cache_restore( bank, funk, &xid ) ) ) {
810 0 : FD_LOG_ERR(( "Failed to restore sysvar cache" ));
811 0 : }
812 0 : }
813 :
814 : static void
815 : fd_runtime_update_bank_hash( fd_bank_t * bank,
816 0 : fd_capture_ctx_t * capture_ctx ) {
817 : /* Save the previous bank hash, and the parents signature count */
818 0 : fd_hash_t const * prev_bank_hash = NULL;
819 0 : if( FD_LIKELY( fd_bank_slot_get( bank )!=0UL ) ) {
820 0 : prev_bank_hash = fd_bank_bank_hash_query( bank );
821 0 : fd_bank_prev_bank_hash_set( bank, *prev_bank_hash );
822 0 : } else {
823 0 : prev_bank_hash = fd_bank_prev_bank_hash_query( bank );
824 0 : }
825 :
826 0 : fd_bank_parent_signature_cnt_set( bank, fd_bank_signature_count_get( bank ) );
827 :
828 : /* Compute the new bank hash */
829 0 : fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
830 0 : fd_hash_t new_bank_hash[1] = { 0 };
831 0 : fd_hashes_hash_bank(
832 0 : lthash,
833 0 : prev_bank_hash,
834 0 : (fd_hash_t *)fd_bank_poh_query( bank )->hash,
835 0 : fd_bank_signature_count_get( bank ),
836 0 : new_bank_hash );
837 :
838 : /* Update the bank hash */
839 0 : fd_bank_bank_hash_set( bank, *new_bank_hash );
840 :
841 0 : if( capture_ctx != NULL && capture_ctx->capture != NULL &&
842 0 : fd_bank_slot_get( bank )>=capture_ctx->solcap_start_slot ) {
843 :
844 0 : uchar lthash_hash[FD_HASH_FOOTPRINT];
845 0 : fd_blake3_hash(lthash->bytes, FD_LTHASH_LEN_BYTES, lthash_hash );
846 :
847 0 : fd_solcap_write_bank_preimage(
848 0 : capture_ctx->capture,
849 0 : new_bank_hash->hash,
850 0 : fd_bank_prev_bank_hash_query( bank ),
851 0 : NULL,
852 0 : lthash_hash,
853 0 : fd_bank_poh_query( bank )->hash,
854 0 : fd_bank_signature_count_get( bank ) );
855 0 : }
856 :
857 0 : fd_bank_lthash_end_locking_query( bank );
858 0 : }
859 :
860 : /******************************************************************************/
861 : /* Transaction Level Execution Management */
862 : /******************************************************************************/
863 :
864 : /* fd_runtime_pre_execute_check is responsible for conducting many of the
865 : transaction sanitization checks. */
866 :
867 : static inline int
868 : fd_runtime_pre_execute_check( fd_runtime_t * runtime,
869 : fd_bank_t * bank,
870 : fd_txn_in_t const * txn_in,
871 0 : fd_txn_out_t * txn_out ) {
872 :
873 : /* Set up the core account keys. These are the account keys directly
874 : passed in via the serialized transaction, represented as an array.
875 : Note that this does not include additional keys referenced in
876 : address lookup tables. */
877 0 : fd_executor_setup_txn_account_keys( txn_in, txn_out );
878 :
879 0 : int err;
880 :
881 : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/sdk/src/transaction/sanitized.rs#L263-L275
882 : TODO: Agave's precompile verification is done at the slot level, before batching and executing transactions. This logic should probably
883 : be moved in the future. The Agave call heirarchy looks something like this:
884 : process_single_slot
885 : v
886 : confirm_full_slot
887 : v
888 : confirm_slot_entries --------------------------------------------------->
889 : v v v
890 : verify_transaction ComputeBudget::process_instruction process_entries
891 : v v
892 : verify_precompiles process_batches
893 : v
894 : ...
895 : v
896 : load_and_execute_transactions
897 : v
898 : ...
899 : v
900 : load_accounts --> load_transaction_accounts
901 : v
902 : general transaction execution
903 :
904 : */
905 :
906 0 : # if FD_HAS_FLATCC
907 0 : uchar dump_txn = !!( runtime->log.capture_ctx &&
908 0 : fd_bank_slot_get( bank ) >= runtime->log.capture_ctx->dump_proto_start_slot &&
909 0 : runtime->log.capture_ctx->dump_txn_to_pb );
910 0 : if( FD_UNLIKELY( dump_txn ) ) {
911 0 : fd_dump_txn_to_protobuf( runtime, bank, txn_in, txn_out );
912 0 : }
913 0 : # endif
914 :
915 : /* Verify the transaction. For now, this step only involves processing
916 : the compute budget instructions. */
917 0 : err = fd_executor_verify_transaction( bank, txn_in, txn_out );
918 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
919 0 : txn_out->err.is_committable = 0;
920 0 : return err;
921 0 : }
922 :
923 : /* Resolve and verify ALUT-referenced account keys, if applicable */
924 0 : err = fd_executor_setup_txn_alut_account_keys( runtime, bank, txn_in, txn_out );
925 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
926 0 : txn_out->err.is_committable = 0;
927 0 : return err;
928 0 : }
929 :
930 : /* Set up the transaction accounts and other txn ctx metadata */
931 0 : fd_executor_setup_accounts_for_txn( runtime, bank, txn_in, txn_out );
932 :
933 : /* Post-sanitization checks. Called from prepare_sanitized_batch()
934 : which, for now, only is used to lock the accounts and perform a
935 : couple basic validations.
936 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 */
937 0 : err = fd_executor_validate_account_locks( bank, txn_out );
938 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
939 0 : txn_out->err.is_committable = 0;
940 0 : return err;
941 0 : }
942 :
943 : /* load_and_execute_transactions() -> check_transactions()
944 : https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3667-L3672 */
945 0 : err = fd_executor_check_transactions( runtime, bank, txn_in, txn_out );
946 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
947 0 : txn_out->err.is_committable = 0;
948 0 : return err;
949 0 : }
950 :
951 : /* load_and_execute_sanitized_transactions() -> validate_fees() ->
952 : validate_transaction_fee_payer()
953 : https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L236-L249 */
954 0 : err = fd_executor_validate_transaction_fee_payer( runtime, bank, txn_in, txn_out );
955 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
956 0 : txn_out->err.is_committable = 0;
957 0 : return err;
958 0 : }
959 :
960 0 : txn_out->details.exec_start_timestamp = fd_tickcount();
961 :
962 : /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L284-L296 */
963 0 : err = fd_executor_load_transaction_accounts( runtime, bank, txn_in, txn_out );
964 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
965 : /* Regardless of whether transaction accounts were loaded successfully, the transaction is
966 : included in the block and transaction fees are collected.
967 : https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/transaction_processor.rs#L341-L357 */
968 0 : txn_out->err.is_fees_only = 1;
969 :
970 : /* If the transaction fails to load, the "rollback" accounts will include one of the following:
971 : 1. Nonce account only
972 : 2. Fee payer only
973 : 3. Nonce account + fee payer
974 :
975 : Because the cost tracker uses the loaded account data size in block cost calculations, we need to
976 : make sure our calculated loaded accounts data size is conformant with Agave's.
977 : https://github.com/anza-xyz/agave/blob/v2.1.14/runtime/src/bank.rs#L4116
978 :
979 : In any case, we should always add the dlen of the fee payer. */
980 0 : txn_out->details.loaded_accounts_data_size = fd_txn_account_get_data_len( &txn_out->accounts.accounts[FD_FEE_PAYER_TXN_IDX] );
981 :
982 : /* Special case handling for if a nonce account is present in the transaction. */
983 0 : if( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) {
984 : /* If the nonce account is not the fee payer, then we separately add the dlen of the nonce account. Otherwise, we would
985 : be double counting the dlen of the fee payer. */
986 0 : if( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) {
987 0 : txn_out->details.loaded_accounts_data_size += fd_txn_account_get_data_len( txn_out->accounts.rollback_nonce );
988 0 : }
989 0 : }
990 0 : }
991 :
992 : /*
993 : The fee payer and the nonce account will be stored and hashed so
994 : long as the transaction landed on chain, or, in Agave terminology,
995 : the transaction was processed.
996 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/account_saver.rs#L72
997 :
998 : A transaction lands on chain in one of two ways:
999 : (1) Passed fee validation and loaded accounts.
1000 : (2) Passed fee validation and failed to load accounts and the enable_transaction_loading_failure_fees feature is enabled as per
1001 : SIMD-0082 https://github.com/anza-xyz/feature-gate-tracker/issues/52
1002 :
1003 : So, at this point, the transaction is committable.
1004 : */
1005 :
1006 0 : return err;
1007 0 : }
1008 :
1009 : /* fd_runtime_finalize_account is a helper used to commit the data from
1010 : a writable transaction account back into the accountsdb. */
1011 :
1012 : static void
1013 : fd_runtime_finalize_account( fd_funk_t * funk,
1014 : fd_funk_txn_xid_t const * xid,
1015 : fd_txn_account_t * acc,
1016 0 : fd_funk_rec_t * prev_rec ) {
1017 0 : if( FD_UNLIKELY( !fd_txn_account_is_mutable( acc ) ) ) {
1018 0 : FD_LOG_CRIT(( "fd_runtime_finalize_account: account is not mutable" ));
1019 0 : }
1020 :
1021 0 : fd_pubkey_t const * key = acc->pubkey;
1022 0 : uchar const * record_data = (uchar *)fd_txn_account_get_meta( acc );
1023 0 : ulong record_sz = fd_account_meta_get_record_sz( acc->meta );
1024 :
1025 0 : int err = FD_FUNK_SUCCESS;
1026 :
1027 0 : if( !prev_rec || !fd_funk_txn_xid_eq( prev_rec->pair.xid, xid ) ) {
1028 :
1029 0 : fd_funk_rec_key_t funk_key = fd_funk_acc_key( key );
1030 0 : fd_funk_rec_prepare_t prepare[1];
1031 0 : fd_funk_rec_t * rec = fd_funk_rec_prepare( funk, xid, &funk_key, prepare, &err );
1032 0 : if( FD_UNLIKELY( !rec || err!=FD_FUNK_SUCCESS ) ) {
1033 0 : FD_LOG_ERR(( "fd_runtime_finalize_account: failed to prepare record (%i-%s)", err, fd_funk_strerror( err ) ));
1034 0 : }
1035 :
1036 0 : if( FD_UNLIKELY( !fd_funk_val_truncate(
1037 0 : rec,
1038 0 : fd_funk_alloc( funk ),
1039 0 : fd_funk_wksp( funk ),
1040 0 : 0UL,
1041 0 : record_sz,
1042 0 : &err ) ) ) {
1043 0 : FD_LOG_ERR(( "fd_funk_val_truncate(sz=%lu) for account failed (%i-%s)", record_sz, err, fd_funk_strerror( err ) ));
1044 0 : }
1045 :
1046 0 : fd_memcpy( fd_funk_val( rec, fd_funk_wksp( funk ) ), record_data, record_sz );
1047 :
1048 0 : fd_funk_rec_publish( funk, prepare );
1049 :
1050 0 : } else {
1051 :
1052 0 : if( FD_UNLIKELY( !fd_funk_val_truncate(
1053 0 : prev_rec,
1054 0 : fd_funk_alloc( funk ),
1055 0 : fd_funk_wksp( funk ),
1056 0 : 0UL,
1057 0 : record_sz,
1058 0 : &err ) ) ) {
1059 0 : FD_LOG_ERR(( "fd_funk_val_truncate(sz=%lu) for account failed (%i-%s)", record_sz, err, fd_funk_strerror( err ) ));
1060 0 : }
1061 :
1062 0 : fd_memcpy( fd_funk_val( prev_rec, fd_funk_wksp( funk ) ), record_data, record_sz );
1063 :
1064 0 : }
1065 :
1066 0 : }
1067 :
1068 : /* fd_runtime_buffer_solcap_account_update buffers an account
1069 : update event message in the capture context, which will be
1070 : sent to the replay tile via the exec_replay link.
1071 : This buffering is done to avoid passing stem down into the runtime.
1072 :
1073 : TODO: remove this when solcap v2 is here. */
1074 : static void
1075 : fd_runtime_buffer_solcap_account_update( fd_txn_account_t * account,
1076 : fd_bank_t * bank,
1077 0 : fd_capture_ctx_t * capture_ctx ) {
1078 :
1079 : /* Check if we should publish the update */
1080 0 : if( FD_UNLIKELY( !capture_ctx || fd_bank_slot_get( bank )<capture_ctx->solcap_start_slot ) ) {
1081 0 : return;
1082 0 : }
1083 :
1084 : /* Get account data */
1085 0 : fd_account_meta_t const * meta = fd_txn_account_get_meta( account );
1086 0 : void const * data = fd_txn_account_get_data( account );
1087 :
1088 : /* Calculate account hash using lthash */
1089 0 : fd_lthash_value_t lthash[1];
1090 0 : fd_hashes_account_lthash( account->pubkey, meta, data, lthash );
1091 :
1092 : /* Calculate message size */
1093 0 : if( FD_UNLIKELY( capture_ctx->account_updates_len >= FD_CAPTURE_CTX_MAX_ACCOUNT_UPDATES ) ) {
1094 0 : FD_LOG_CRIT(( "cannot buffer solcap account update. this should never happen" ));
1095 0 : return;
1096 0 : }
1097 :
1098 : /* Write the message to the buffer */
1099 0 : fd_capture_ctx_account_update_msg_t * account_update_msg = (fd_capture_ctx_account_update_msg_t *)(capture_ctx->account_updates_buffer_ptr);
1100 0 : account_update_msg->pubkey = *account->pubkey;
1101 0 : account_update_msg->data_sz = meta->dlen;
1102 0 : account_update_msg->bank_idx = bank->idx;
1103 0 : fd_solana_account_meta_init(
1104 0 : &account_update_msg->info,
1105 0 : fd_txn_account_get_lamports ( account ),
1106 0 : fd_txn_account_get_owner ( account ),
1107 0 : fd_txn_account_is_executable( account )
1108 0 : );
1109 0 : memcpy( account_update_msg->hash.uc, lthash->bytes, sizeof(fd_hash_t) );
1110 0 : capture_ctx->account_updates_buffer_ptr += sizeof(fd_capture_ctx_account_update_msg_t);
1111 :
1112 : /* Write the account data to the buffer */
1113 0 : memcpy( capture_ctx->account_updates_buffer_ptr, data, meta->dlen );
1114 0 : capture_ctx->account_updates_buffer_ptr += meta->dlen;
1115 :
1116 0 : capture_ctx->account_updates_len++;
1117 0 : }
1118 :
1119 : /* fd_runtime_save_account is a convenience wrapper that looks
1120 : up the previous account state from funk before updating the lthash
1121 : and saving the new version of the account to funk.
1122 :
1123 : TODO: We have to make a read request to the DB, so that we can calculate
1124 : the previous version of the accounts hash, to mix-out from the accumulated
1125 : lthash. In future we should likely cache the previous version of the account
1126 : in transaction setup, so that we don't have to issue a read request here.
1127 :
1128 : funk is the funk database handle. funk_txn is the transaction
1129 : context to query (NULL for root context). account is the modified
1130 : account. bank and capture_ctx are passed to fd_hashes_update_lthash.
1131 :
1132 : This function:
1133 : - Queries funk for the previous account version
1134 : - Computes the hash of the previous version (or uses zero for new)
1135 : - Calls fd_hashes_update_lthash with the computed previous hash
1136 : - Saves the new version of the account to Funk
1137 : - Notifies the replay tile that an account update has occurred, so it
1138 : can write the account to the solcap file.
1139 :
1140 : The function handles FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT gracefully (uses
1141 : zero hash). On other funk errors, the function will FD_LOG_ERR.
1142 : All non-optional pointers must be valid. */
1143 :
1144 : static void
1145 : fd_runtime_save_account( fd_funk_t * funk,
1146 : fd_funk_txn_xid_t const * xid,
1147 : fd_txn_account_t * account,
1148 : fd_bank_t * bank,
1149 0 : fd_capture_ctx_t * capture_ctx ) {
1150 : /* Look up the previous version of the account from Funk */
1151 0 : int err = FD_ACC_MGR_SUCCESS;
1152 0 : fd_funk_rec_t * funk_prev_rec = NULL;
1153 0 : fd_account_meta_t const * prev_meta = fd_funk_get_acc_meta_readonly(
1154 0 : funk,
1155 0 : xid,
1156 0 : account->pubkey,
1157 0 : fd_type_pun( &funk_prev_rec ),
1158 0 : &err,
1159 0 : NULL );
1160 0 : uchar const * prev_data = (void const *)( prev_meta+1 );
1161 :
1162 : /* Hash the old version of the account */
1163 0 : fd_lthash_value_t prev_hash[1];
1164 0 : fd_lthash_zero( prev_hash );
1165 0 : if( err != FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) {
1166 0 : fd_hashes_account_lthash(
1167 0 : account->pubkey,
1168 0 : prev_meta,
1169 0 : prev_data,
1170 0 : prev_hash );
1171 0 : }
1172 :
1173 : /* Mix in the account hash into the bank hash */
1174 0 : fd_hashes_update_lthash( account, prev_hash, bank, NULL );
1175 :
1176 : /* Publish account update to replay tile for solcap writing
1177 : TODO: write in the exec tile with solcap v2 */
1178 0 : fd_runtime_buffer_solcap_account_update( account, bank, capture_ctx );
1179 :
1180 : /* Save the new version of the account to Funk */
1181 0 : fd_runtime_finalize_account( funk, xid, account, funk_prev_rec );
1182 0 : }
1183 :
1184 : /* fd_runtime_commit_txn is a helper used by the non-tpool transaction
1185 : executor to finalize borrowed account changes back into funk. It also
1186 : handles txncache insertion and updates to the vote/stake cache.
1187 : TODO: This function should probably be moved to fd_executor.c. */
1188 :
1189 : void
1190 : fd_runtime_commit_txn( fd_runtime_t * runtime,
1191 : fd_bank_t * bank,
1192 : fd_txn_in_t const * txn_in,
1193 0 : fd_txn_out_t * txn_out ) {
1194 :
1195 0 : txn_out->details.commit_start_timestamp = fd_tickcount();
1196 :
1197 0 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->idx } };
1198 :
1199 0 : if( FD_UNLIKELY( txn_out->err.txn_err ) ) {
1200 :
1201 : /* Save the fee_payer. Everything but the fee balance should be reset.
1202 : TODO: an optimization here could be to use a dirty flag in the
1203 : borrowed account. If the borrowed account data has been changed in
1204 : any way, then the full account can be rolled back as it is done now.
1205 : However, most of the time the account data is not changed, and only
1206 : the lamport balance has to change. */
1207 :
1208 : /* With nonce account rollbacks, there are three cases:
1209 : 1. No nonce account in the transaction
1210 : 2. Nonce account is the fee payer
1211 : 3. Nonce account is not the fee payer
1212 :
1213 : We should always rollback the nonce account first. Note that the nonce account may be the fee payer (case 2). */
1214 0 : if( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) {
1215 0 : fd_runtime_save_account( runtime->funk, &xid, txn_out->accounts.rollback_nonce, bank, runtime->log.capture_ctx );
1216 0 : }
1217 :
1218 : /* Now, we must only save the fee payer if the nonce account was not the fee payer (because that was already saved above) */
1219 0 : if( FD_LIKELY( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
1220 0 : fd_runtime_save_account( runtime->funk, &xid, txn_out->accounts.rollback_fee_payer, bank, runtime->log.capture_ctx );
1221 0 : }
1222 0 : } else {
1223 :
1224 0 : for( ushort i=0; i<txn_out->accounts.accounts_cnt; i++ ) {
1225 : /* We are only interested in saving writable accounts and the fee
1226 : payer account. */
1227 0 : if( !fd_runtime_account_is_writable_idx( txn_in, txn_out, bank, i ) && i!=FD_FEE_PAYER_TXN_IDX ) {
1228 0 : continue;
1229 0 : }
1230 :
1231 0 : fd_txn_account_t * acc_rec = fd_txn_account_join( &txn_out->accounts.accounts[i] );
1232 0 : if( FD_UNLIKELY( !acc_rec ) ) {
1233 0 : FD_LOG_CRIT(( "fd_runtime_commit_txn: failed to join account at idx %u", i ));
1234 0 : }
1235 :
1236 : /* Tips for bundles are collected in the bank: a user submitting a
1237 : bundle must include a instruction that transfers lamports to
1238 : a specific tip account. Tips accumulated through the slot. */
1239 0 : if( fd_pack_tip_is_tip_account( fd_type_pun( acc_rec->pubkey->uc ) ) ) {
1240 0 : txn_out->details.tips += fd_ulong_sat_sub( acc_rec->meta->lamports, acc_rec->starting_lamports );
1241 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_tips_modify( bank ), txn_out->details.tips );
1242 0 : }
1243 :
1244 0 : if( 0==memcmp( fd_txn_account_get_owner( acc_rec ), &fd_solana_vote_program_id, sizeof(fd_pubkey_t) ) ) {
1245 0 : fd_stakes_update_vote_state( acc_rec, bank );
1246 0 : }
1247 :
1248 0 : if( 0==memcmp( fd_txn_account_get_owner( acc_rec ), &fd_solana_stake_program_id, sizeof(fd_pubkey_t) ) ) {
1249 0 : fd_stakes_update_stake_delegation( acc_rec, bank );
1250 0 : }
1251 :
1252 : /* Reclaim any accounts that have 0-lamports, now that any related
1253 : cache updates have been applied. */
1254 0 : fd_executor_reclaim_account( &txn_out->accounts.accounts[i], fd_bank_slot_get( bank ) );
1255 :
1256 0 : fd_runtime_save_account( runtime->funk, &xid, &txn_out->accounts.accounts[i], bank, runtime->log.capture_ctx );
1257 0 : }
1258 :
1259 : /* We need to queue any existing program accounts that may have
1260 : been deployed / upgraded for reverification in the program
1261 : cache since their programdata may have changed. ELF / sBPF
1262 : metadata will need to be updated. */
1263 0 : ulong current_slot = fd_bank_slot_get( bank );
1264 0 : for( uchar i=0; i<txn_out->details.programs_to_reverify_cnt; i++ ) {
1265 0 : fd_pubkey_t const * program_key = &txn_out->details.programs_to_reverify[i];
1266 0 : fd_progcache_invalidate( runtime->progcache, &xid, program_key, current_slot );
1267 0 : }
1268 0 : }
1269 :
1270 : /* Accumulate block-level information to the bank. */
1271 :
1272 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_txn_count_modify( bank ), 1UL );
1273 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_execution_fees_modify( bank ), txn_out->details.execution_fee );
1274 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_priority_fees_modify( bank ), txn_out->details.priority_fee );
1275 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_signature_count_modify( bank ), txn_out->details.signature_count );
1276 :
1277 0 : int is_vote = fd_txn_is_simple_vote_transaction( TXN( txn_in->txn ), txn_in->txn->payload );
1278 0 : if( !is_vote ){
1279 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_nonvote_txn_count_modify( bank ), 1 );
1280 0 : if( FD_UNLIKELY( txn_out->err.exec_err ) ) {
1281 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_nonvote_failed_txn_count_modify( bank ), 1 );
1282 0 : }
1283 0 : }
1284 :
1285 0 : if( FD_UNLIKELY( txn_out->err.exec_err ) ) {
1286 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_failed_txn_count_modify( bank ), 1 );
1287 0 : }
1288 :
1289 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_total_compute_units_used_modify( bank ), txn_out->details.compute_budget.compute_unit_limit - txn_out->details.compute_budget.compute_meter );
1290 :
1291 : /* Update the cost tracker. */
1292 :
1293 0 : fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_locking_modify( bank );
1294 0 : int res = fd_cost_tracker_calculate_cost_and_add( cost_tracker, bank, txn_in, txn_out );
1295 0 : if( FD_UNLIKELY( res!=FD_COST_TRACKER_SUCCESS ) ) {
1296 0 : FD_LOG_DEBUG(( "fd_runtime_commit_txn: transaction failed to fit into block %d", res ));
1297 0 : txn_out->err.is_committable = 0;
1298 0 : }
1299 0 : fd_bank_cost_tracker_end_locking_modify( bank );
1300 :
1301 0 : txn_out->details.loaded_accounts_data_size_cost = fd_cost_tracker_calculate_loaded_accounts_data_size_cost( txn_out );
1302 :
1303 : /* Finally, update the status cache. */
1304 :
1305 0 : if( FD_LIKELY( runtime->status_cache && txn_out->accounts.nonce_idx_in_txn==ULONG_MAX ) ) {
1306 : /* In Agave, durable nonce transactions are inserted to the status
1307 : cache the same as any others, but this is only to serve RPC
1308 : requests, they do not need to be in there for correctness as the
1309 : nonce mechanism itself prevents double spend. We skip this logic
1310 : entirely to simplify and improve performance of the txn cache. */
1311 :
1312 0 : fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->recent_blockhash_off);
1313 0 : fd_txncache_insert( runtime->status_cache, bank->txncache_fork_id, blockhash->uc, txn_out->details.blake_txn_msg_hash.uc );
1314 0 : }
1315 0 : }
1316 :
1317 : void
1318 : fd_runtime_prepare_and_execute_txn( fd_runtime_t * runtime,
1319 : fd_bank_t * bank,
1320 : fd_txn_in_t const * txn_in,
1321 0 : fd_txn_out_t * txn_out ) {
1322 :
1323 0 : txn_out->details.prep_start_timestamp = fd_tickcount();
1324 0 : txn_out->details.load_start_timestamp = LONG_MAX;
1325 0 : txn_out->details.exec_start_timestamp = LONG_MAX;
1326 0 : txn_out->details.commit_start_timestamp = LONG_MAX;
1327 :
1328 0 : txn_out->accounts.accounts_cnt = 0UL;
1329 :
1330 0 : txn_out->details.programs_to_reverify_cnt = 0UL;
1331 0 : txn_out->details.loaded_accounts_data_size = 0UL;
1332 0 : txn_out->details.loaded_accounts_data_size_cost = 0UL;
1333 0 : txn_out->details.accounts_resize_delta = 0UL;
1334 0 : txn_out->details.return_data.len = 0UL;
1335 0 : txn_out->details.tips = 0UL;
1336 0 : txn_out->details.execution_fee = 0UL;
1337 0 : txn_out->details.priority_fee = 0UL;
1338 0 : txn_out->details.signature_count = TXN( txn_in->txn )->signature_cnt;
1339 0 : memset( txn_out->details.return_data.program_id.key, 0, sizeof(fd_pubkey_t) );
1340 0 : fd_compute_budget_details_new( &txn_out->details.compute_budget );
1341 :
1342 0 : runtime->executable.cnt = 0UL;
1343 0 : runtime->log.enable_log_collector = 0;
1344 0 : runtime->instr.info_cnt = 0UL;
1345 0 : runtime->instr.trace_length = 0UL;
1346 0 : runtime->instr.current_idx = 0;
1347 0 : runtime->instr.stack_sz = 0;
1348 :
1349 0 : txn_out->err.is_committable = 1;
1350 0 : txn_out->err.is_fees_only = 0;
1351 0 : txn_out->err.txn_err = FD_RUNTIME_EXECUTE_SUCCESS;
1352 0 : txn_out->err.exec_err = FD_EXECUTOR_INSTR_SUCCESS;
1353 0 : txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_NONE;
1354 0 : txn_out->err.exec_err_idx = INT_MAX;
1355 :
1356 : /* Transaction sanitization. If a transaction can't be commited or is
1357 : fees-only, we return early. */
1358 0 : txn_out->err.txn_err = fd_runtime_pre_execute_check( runtime, bank, txn_in, txn_out );
1359 :
1360 0 : txn_out->details.exec_start_timestamp = fd_tickcount();
1361 :
1362 : /* Execute the transaction. */
1363 0 : if( FD_LIKELY( txn_out->err.is_committable && !txn_out->err.is_fees_only ) ) {
1364 0 : txn_out->err.txn_err = fd_execute_txn( runtime, bank, txn_in, txn_out );
1365 0 : }
1366 0 : }
1367 :
1368 : /* fd_executor_txn_verify and fd_runtime_pre_execute_check are responisble
1369 : for the bulk of the pre-transaction execution checks in the runtime.
1370 : They aim to preserve the ordering present in the Agave client to match
1371 : parity in terms of error codes. Sigverify is kept separate from the rest
1372 : of the transaction checks for fuzzing convenience.
1373 :
1374 : For reference this is the general code path which contains all relevant
1375 : pre-transactions checks in the v2.0.x Agave client from upstream
1376 : to downstream is as follows:
1377 :
1378 : confirm_slot_entries() which calls verify_ticks() and
1379 : verify_transaction(). verify_transaction() calls verify_and_hash_message()
1380 : and verify_precompiles() which parallels fd_executor_txn_verify() and
1381 : fd_executor_verify_transaction().
1382 :
1383 : process_entries() contains a duplicate account check which is part of
1384 : agave account lock acquiring. This is checked inline in
1385 : fd_runtime_pre_execute_check().
1386 :
1387 : load_and_execute_transactions() contains the function check_transactions().
1388 : This contains check_age() and check_status_cache() which is paralleled by
1389 : fd_executor_check_transaction_age_and_compute_budget_limits() and
1390 : fd_executor_check_status_cache() respectively.
1391 :
1392 : load_and_execute_sanitized_transactions() contains validate_fees()
1393 : which is responsible for executing the compute budget instructions,
1394 : validating the fee payer and collecting the fee. This is mirrored in
1395 : firedancer with fd_executor_compute_budget_program_execute_instructions()
1396 : and fd_executor_collect_fees(). load_and_execute_sanitized_transactions()
1397 : also checks the total data size of the accounts in load_accounts() and
1398 : validates the program accounts in load_transaction_accounts(). This
1399 : is paralled by fd_executor_load_transaction_accounts(). */
1400 :
1401 :
1402 : /******************************************************************************/
1403 : /* Genesis */
1404 : /*******************************************************************************/
1405 :
1406 : static void
1407 : fd_runtime_genesis_init_program( fd_bank_t * bank,
1408 : fd_accdb_user_t * accdb,
1409 : fd_funk_txn_xid_t const * xid,
1410 0 : fd_capture_ctx_t * capture_ctx ) {
1411 :
1412 0 : fd_sysvar_clock_init( bank, accdb, xid, capture_ctx );
1413 0 : fd_sysvar_rent_init( bank, accdb, xid, capture_ctx );
1414 :
1415 0 : fd_sysvar_slot_history_init( bank, accdb, xid, capture_ctx );
1416 0 : fd_sysvar_epoch_schedule_init( bank, accdb, xid, capture_ctx );
1417 0 : fd_sysvar_recent_hashes_init( bank, accdb, xid, capture_ctx );
1418 0 : fd_sysvar_stake_history_init( bank, accdb, xid, capture_ctx );
1419 0 : fd_sysvar_last_restart_slot_init( bank, accdb, xid, capture_ctx );
1420 :
1421 0 : fd_builtin_programs_init( bank, accdb, xid, capture_ctx );
1422 0 : fd_stake_program_config_init( accdb, xid );
1423 0 : }
1424 :
1425 : static void
1426 : fd_runtime_init_bank_from_genesis( fd_banks_t * banks,
1427 : fd_bank_t * bank,
1428 : fd_funk_t * funk,
1429 : fd_funk_txn_xid_t const * xid,
1430 : fd_genesis_solana_global_t const * genesis_block,
1431 0 : fd_hash_t const * genesis_hash ) {
1432 :
1433 0 : fd_bank_poh_set( bank, *genesis_hash );
1434 :
1435 0 : fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
1436 0 : memset( bank_hash->hash, 0, FD_SHA256_HASH_SZ );
1437 :
1438 0 : fd_poh_config_global_t const * poh = &genesis_block->poh_config;
1439 0 : uint128 target_tick_duration = ((uint128)poh->target_tick_duration.seconds * 1000000000UL + (uint128)poh->target_tick_duration.nanoseconds);
1440 :
1441 0 : fd_bank_epoch_schedule_set( bank, genesis_block->epoch_schedule );
1442 :
1443 0 : fd_bank_rent_set( bank, genesis_block->rent );
1444 :
1445 0 : fd_bank_block_height_set( bank, 0UL );
1446 :
1447 0 : fd_bank_inflation_set( bank, genesis_block->inflation );
1448 :
1449 0 : {
1450 : /* FIXME Why is there a previous blockhash at genesis? Why is the
1451 : last_hash field an option type in Agave, if even the first
1452 : real block has a previous blockhash? */
1453 : /* TODO: Use a real seed here. */
1454 0 : fd_blockhashes_t * bhq = fd_blockhashes_init( fd_bank_block_hash_queue_modify( bank ), 0UL );
1455 0 : fd_blockhash_info_t * info = fd_blockhashes_push_new( bhq, genesis_hash );
1456 0 : info->fee_calculator.lamports_per_signature = 0UL;
1457 0 : }
1458 :
1459 0 : fd_bank_fee_rate_governor_set( bank, genesis_block->fee_rate_governor );
1460 :
1461 0 : fd_bank_max_tick_height_set( bank, genesis_block->ticks_per_slot * (fd_bank_slot_get( bank ) + 1) );
1462 :
1463 0 : fd_bank_hashes_per_tick_set( bank, !!poh->hashes_per_tick ? poh->hashes_per_tick : 0UL );
1464 :
1465 0 : fd_bank_ns_per_slot_set( bank, (fd_w_u128_t) { .ud=target_tick_duration * genesis_block->ticks_per_slot } );
1466 :
1467 0 : fd_bank_ticks_per_slot_set( bank, genesis_block->ticks_per_slot );
1468 :
1469 0 : fd_bank_genesis_creation_time_set( bank, genesis_block->creation_time );
1470 :
1471 0 : fd_bank_slots_per_year_set( bank, SECONDS_PER_YEAR * (1000000000.0 / (double)target_tick_duration) / (double)genesis_block->ticks_per_slot );
1472 :
1473 0 : fd_bank_signature_count_set( bank, 0UL );
1474 :
1475 : /* Derive epoch stakes */
1476 :
1477 0 : fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
1478 0 : if( FD_UNLIKELY( !stake_delegations ) ) {
1479 0 : FD_LOG_CRIT(( "Failed to join and new a stake delegations" ));
1480 0 : }
1481 :
1482 0 : fd_vote_states_t * vote_states = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_locking_modify( bank ), FD_RUNTIME_MAX_VOTE_ACCOUNTS, 999UL ) );
1483 0 : if( FD_UNLIKELY( !vote_states ) ) {
1484 0 : FD_LOG_CRIT(( "Failed to join and new a vote states" ));
1485 0 : }
1486 :
1487 0 : ulong capitalization = 0UL;
1488 :
1489 0 : fd_pubkey_account_pair_global_t const * accounts = fd_genesis_solana_accounts_join( genesis_block );
1490 :
1491 0 : for( ulong i=0UL; i<genesis_block->accounts_len; i++ ) {
1492 0 : fd_pubkey_account_pair_global_t const * acc = &accounts[ i ];
1493 0 : capitalization = fd_ulong_sat_add( capitalization, acc->account.lamports );
1494 :
1495 0 : uchar const * acc_data = fd_solana_account_data_join( &acc->account );
1496 :
1497 0 : if( !memcmp( acc->account.owner.key, fd_solana_vote_program_id.key, sizeof(fd_pubkey_t) ) ) {
1498 : /* This means that there is a vote account which should be
1499 : inserted into the vote states. Even after the vote account is
1500 : inserted, we still don't know the total amount of stake that is
1501 : delegated to the vote account. This must be calculated later. */
1502 0 : fd_vote_states_update_from_account( vote_states, &acc->key, acc_data, acc->account.data_len );
1503 0 : } else if( !memcmp( acc->account.owner.key, fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) {
1504 : /* If an account is a stake account, then it must be added to the
1505 : stake delegations cache. We should only add stake accounts that
1506 : have a valid non-zero stake. */
1507 0 : fd_stake_state_v2_t stake_state = {0};
1508 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
1509 0 : stake_state_v2, &stake_state,
1510 0 : acc_data, acc->account.data_len,
1511 0 : NULL ) ) ) {
1512 0 : FD_BASE58_ENCODE_32_BYTES( acc->key.key, stake_b58 );
1513 0 : FD_LOG_ERR(( "Failed to deserialize genesis stake account %s", stake_b58 ));
1514 0 : }
1515 0 : if( !fd_stake_state_v2_is_stake( &stake_state ) ) continue;
1516 0 : if( !stake_state.inner.stake.stake.delegation.stake ) continue;
1517 :
1518 0 : fd_stake_delegations_update(
1519 0 : stake_delegations,
1520 0 : (fd_pubkey_t *)acc->key.key,
1521 0 : &stake_state.inner.stake.stake.delegation.voter_pubkey,
1522 0 : stake_state.inner.stake.stake.delegation.stake,
1523 0 : stake_state.inner.stake.stake.delegation.activation_epoch,
1524 0 : stake_state.inner.stake.stake.delegation.deactivation_epoch,
1525 0 : stake_state.inner.stake.stake.credits_observed,
1526 0 : stake_state.inner.stake.stake.delegation.warmup_cooldown_rate );
1527 :
1528 0 : } else if( !memcmp( acc->account.owner.key, fd_solana_feature_program_id.key, sizeof(fd_pubkey_t) ) ) {
1529 : /* Feature Account */
1530 :
1531 : /* Scan list of feature IDs to resolve address=>feature offset */
1532 0 : fd_feature_id_t const *found = NULL;
1533 0 : for( fd_feature_id_t const * id = fd_feature_iter_init();
1534 0 : !fd_feature_iter_done( id );
1535 0 : id = fd_feature_iter_next( id ) ) {
1536 0 : if( !memcmp( acc->key.key, id->id.key, sizeof(fd_pubkey_t) ) ) {
1537 0 : found = id;
1538 0 : break;
1539 0 : }
1540 0 : }
1541 :
1542 0 : if( found ) {
1543 : /* Load feature activation */
1544 0 : fd_feature_t feature[1];
1545 0 : FD_TEST( fd_bincode_decode_static( feature, feature, acc_data, acc->account.data_len, NULL ) );
1546 :
1547 0 : fd_features_t * features = fd_bank_features_modify( bank );
1548 0 : if( feature->has_activated_at ) {
1549 0 : FD_LOG_DEBUG(( "Feature %s activated at %lu (genesis)", FD_BASE58_ENC_32_ALLOCA( acc->key.key ), feature->activated_at ));
1550 0 : fd_features_set( features, found, feature->activated_at );
1551 0 : } else {
1552 0 : FD_LOG_DEBUG(( "Feature %s not activated (genesis)", FD_BASE58_ENC_32_ALLOCA( acc->key.key ) ));
1553 0 : fd_features_set( features, found, ULONG_MAX );
1554 0 : }
1555 0 : }
1556 0 : }
1557 0 : }
1558 0 : fd_bank_vote_states_end_locking_modify( bank );
1559 :
1560 : /* fd_refresh_vote_accounts is responsible for updating the vote
1561 : states with the total amount of active delegated stake. It does
1562 : this by iterating over all active stake delegations and summing up
1563 : the amount of stake that is delegated to each vote account. */
1564 :
1565 0 : ulong new_rate_activation_epoch = 0UL;
1566 :
1567 0 : fd_stake_history_t stake_history[1];
1568 0 : fd_sysvar_stake_history_read( funk, xid, stake_history );
1569 :
1570 0 : fd_refresh_vote_accounts(
1571 0 : bank,
1572 0 : stake_delegations,
1573 0 : stake_history,
1574 0 : &new_rate_activation_epoch );
1575 :
1576 : /* Now that the stake and vote delegations are updated correctly, we
1577 : will propagate the vote states to the vote states for the previous
1578 : epoch and the epoch before that.
1579 :
1580 : This is despite the fact we are booting off of genesis which means
1581 : that there is no previous or previous-previous epoch. This is done
1582 : to simplify edge cases around leader schedule and rewards
1583 : calculation.
1584 :
1585 : TODO: Each of the edge cases around this needs to be documented
1586 : much better where each case is clearly enumerated and explained. */
1587 :
1588 0 : vote_states = fd_bank_vote_states_locking_modify( bank );
1589 0 : for( ulong i=0UL; i<genesis_block->accounts_len; i++ ) {
1590 0 : fd_pubkey_account_pair_global_t const * acc = &accounts[ i ];
1591 :
1592 0 : if( !memcmp( acc->account.owner.key, fd_solana_vote_program_id.key, sizeof(fd_pubkey_t) ) ) {
1593 0 : fd_vote_state_ele_t * vote_state = fd_vote_states_query( vote_states, &acc->key );
1594 :
1595 0 : vote_state->stake_t_1 = vote_state->stake;
1596 0 : vote_state->stake_t_2 = vote_state->stake;
1597 0 : }
1598 0 : }
1599 :
1600 0 : fd_vote_states_t * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_modify( bank );
1601 0 : fd_memcpy( vote_states_prev_prev, vote_states, FD_VOTE_STATES_FOOTPRINT );
1602 0 : fd_bank_vote_states_prev_prev_end_locking_modify( bank );
1603 :
1604 0 : fd_vote_states_t * vote_states_prev = fd_bank_vote_states_prev_locking_modify( bank );
1605 0 : fd_memcpy( vote_states_prev, vote_states, FD_VOTE_STATES_FOOTPRINT );
1606 0 : fd_bank_vote_states_prev_end_locking_modify( bank );
1607 :
1608 0 : fd_bank_vote_states_end_locking_modify( bank );
1609 :
1610 0 : fd_bank_epoch_set( bank, 0UL );
1611 :
1612 0 : fd_bank_capitalization_set( bank, capitalization );
1613 0 : }
1614 :
1615 : static int
1616 : fd_runtime_process_genesis_block( fd_bank_t * bank,
1617 : fd_accdb_user_t * accdb,
1618 : fd_funk_txn_xid_t const * xid,
1619 : fd_capture_ctx_t * capture_ctx,
1620 0 : fd_runtime_stack_t * runtime_stack ) {
1621 :
1622 0 : fd_hash_t * poh = fd_bank_poh_modify( bank );
1623 0 : ulong hashcnt_per_slot = fd_bank_hashes_per_tick_get( bank ) * fd_bank_ticks_per_slot_get( bank );
1624 0 : while( hashcnt_per_slot-- ) {
1625 0 : fd_sha256_hash( poh->hash, sizeof(fd_hash_t), poh->hash );
1626 0 : }
1627 :
1628 0 : fd_bank_execution_fees_set( bank, 0UL );
1629 :
1630 0 : fd_bank_priority_fees_set( bank, 0UL );
1631 :
1632 0 : fd_bank_signature_count_set( bank, 0UL );
1633 :
1634 0 : fd_bank_txn_count_set( bank, 0UL );
1635 :
1636 0 : fd_bank_failed_txn_count_set( bank, 0UL );
1637 :
1638 0 : fd_bank_nonvote_failed_txn_count_set( bank, 0UL );
1639 :
1640 0 : fd_bank_total_compute_units_used_set( bank, 0UL );
1641 :
1642 0 : fd_runtime_genesis_init_program( bank, accdb, xid, capture_ctx );
1643 :
1644 0 : fd_sysvar_slot_history_update( bank, accdb, xid, capture_ctx );
1645 :
1646 0 : fd_runtime_update_leaders( bank, runtime_stack );
1647 :
1648 0 : fd_runtime_freeze( bank, accdb, capture_ctx );
1649 :
1650 0 : fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
1651 :
1652 0 : fd_hash_t const * prev_bank_hash = fd_bank_bank_hash_query( bank );
1653 :
1654 0 : fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
1655 0 : fd_hashes_hash_bank(
1656 0 : lthash,
1657 0 : prev_bank_hash,
1658 0 : (fd_hash_t *)fd_bank_poh_query( bank )->hash,
1659 0 : 0UL,
1660 0 : bank_hash );
1661 :
1662 0 : fd_bank_lthash_end_locking_query( bank );
1663 :
1664 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
1665 0 : }
1666 :
1667 : void
1668 : fd_runtime_read_genesis( fd_banks_t * banks,
1669 : fd_bank_t * bank,
1670 : fd_accdb_user_t * accdb,
1671 : fd_funk_txn_xid_t const * xid,
1672 : fd_capture_ctx_t * capture_ctx,
1673 : fd_hash_t const * genesis_hash,
1674 : fd_lthash_value_t const * genesis_lthash,
1675 : fd_genesis_solana_global_t const * genesis_block,
1676 0 : fd_runtime_stack_t * runtime_stack ) {
1677 :
1678 0 : fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
1679 0 : *lthash = *genesis_lthash;
1680 0 : fd_bank_lthash_end_locking_modify( bank );
1681 :
1682 : /* Once the accounts have been loaded from the genesis config into
1683 : the accounts db, we can initialize the bank state. This involves
1684 : setting some fields, and notably setting up the vote and stake
1685 : caches which are used for leader scheduling/rewards. */
1686 :
1687 0 : fd_funk_t * funk = fd_accdb_user_v1_funk( accdb );
1688 0 : fd_runtime_init_bank_from_genesis( banks, bank, funk, xid, genesis_block, genesis_hash );
1689 :
1690 : /* Write the native programs to the accounts db. */
1691 :
1692 0 : fd_string_pubkey_pair_global_t * nips = fd_genesis_solana_native_instruction_processors_join( genesis_block );
1693 :
1694 0 : for( ulong i=0UL; i<genesis_block->native_instruction_processors_len; i++ ) {
1695 0 : fd_string_pubkey_pair_global_t const * a = &nips[ i ];
1696 :
1697 0 : uchar const * string = fd_string_pubkey_pair_string_join( a );
1698 0 : fd_write_builtin_account( bank, accdb, xid, capture_ctx, a->pubkey, (const char *)string, a->string_len );
1699 0 : }
1700 :
1701 0 : fd_features_restore( bank, funk, xid );
1702 :
1703 : /* At this point, state related to the bank and the accounts db
1704 : have been initialized and we are free to finish executing the
1705 : block. In practice, this updates some bank fields (notably the
1706 : poh and bank hash). */
1707 :
1708 0 : int err = fd_runtime_process_genesis_block( bank, accdb, xid, capture_ctx, runtime_stack );
1709 0 : if( FD_UNLIKELY( err ) ) FD_LOG_CRIT(( "genesis slot 0 execute failed with error %d", err ));
1710 0 : }
1711 :
1712 : void
1713 : fd_runtime_block_execute_finalize( fd_bank_t * bank,
1714 : fd_accdb_user_t * accdb,
1715 0 : fd_capture_ctx_t * capture_ctx ) {
1716 :
1717 : /* This slot is now "frozen" and can't be changed anymore. */
1718 0 : fd_runtime_freeze( bank, accdb, capture_ctx );
1719 :
1720 0 : fd_runtime_update_bank_hash( bank, capture_ctx );
1721 0 : }
1722 :
1723 :
1724 : /* Mirrors Agave function solana_sdk::transaction_context::find_index_of_account
1725 :
1726 : Backward scan over transaction accounts.
1727 : Returns -1 if not found.
1728 :
1729 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L233-L238 */
1730 :
1731 : int
1732 : fd_runtime_find_index_of_account( fd_txn_out_t const * txn_out,
1733 0 : fd_pubkey_t const * pubkey ) {
1734 0 : for( ulong i=txn_out->accounts.accounts_cnt; i>0UL; i-- ) {
1735 0 : if( 0==memcmp( pubkey, &txn_out->accounts.account_keys[ i-1UL ], sizeof(fd_pubkey_t) ) ) {
1736 0 : return (int)(i-1UL);
1737 0 : }
1738 0 : }
1739 0 : return -1;
1740 0 : }
1741 :
1742 : int
1743 : fd_runtime_get_account_at_index( fd_txn_in_t const * txn_in,
1744 : fd_txn_out_t * txn_out,
1745 : ushort idx,
1746 : fd_txn_account_t * * account,
1747 0 : fd_txn_account_condition_fn_t * condition ) {
1748 0 : if( FD_UNLIKELY( idx>=txn_out->accounts.accounts_cnt ) ) {
1749 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
1750 0 : }
1751 :
1752 0 : fd_txn_account_t * txn_account = &txn_out->accounts.accounts[idx];
1753 0 : *account = txn_account;
1754 :
1755 0 : if( FD_LIKELY( condition != NULL ) ) {
1756 0 : if( FD_UNLIKELY( !condition( *account, txn_in, txn_out, idx ) ) ) {
1757 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
1758 0 : }
1759 0 : }
1760 :
1761 0 : return FD_ACC_MGR_SUCCESS;
1762 0 : }
1763 :
1764 : int
1765 : fd_runtime_get_account_with_key( fd_txn_in_t const * txn_in,
1766 : fd_txn_out_t * txn_out,
1767 : fd_pubkey_t const * pubkey,
1768 : fd_txn_account_t * * account,
1769 0 : fd_txn_account_condition_fn_t * condition ) {
1770 0 : int index = fd_runtime_find_index_of_account( txn_out, pubkey );
1771 0 : if( FD_UNLIKELY( index==-1 ) ) {
1772 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
1773 0 : }
1774 :
1775 0 : return fd_runtime_get_account_at_index( txn_in,
1776 0 : txn_out,
1777 0 : (uchar)index,
1778 0 : account,
1779 0 : condition );
1780 0 : }
1781 :
1782 : int
1783 : fd_runtime_get_executable_account( fd_runtime_t * runtime,
1784 : fd_txn_in_t const * txn_in,
1785 : fd_txn_out_t * txn_out,
1786 : fd_pubkey_t const * pubkey,
1787 : fd_txn_account_t * * account,
1788 0 : fd_txn_account_condition_fn_t * condition ) {
1789 : /* First try to fetch the executable account from the existing borrowed accounts.
1790 : If the pubkey is in the account keys, then we want to re-use that
1791 : borrowed account since it reflects changes from prior instructions. Referencing the
1792 : read-only executable accounts list is incorrect behavior when the program
1793 : data account is written to in a prior instruction (e.g. program upgrade + invoke within the same txn) */
1794 0 : int err = fd_runtime_get_account_with_key( txn_in,
1795 0 : txn_out,
1796 0 : pubkey,
1797 0 : account,
1798 0 : condition );
1799 0 : if( FD_UNLIKELY( err==FD_ACC_MGR_SUCCESS ) ) {
1800 0 : return FD_ACC_MGR_SUCCESS;
1801 0 : }
1802 :
1803 0 : for( ushort i=0; i<runtime->executable.cnt; i++ ) {
1804 0 : if( memcmp( pubkey->uc, runtime->executable.accounts[i].pubkey->uc, sizeof(fd_pubkey_t) )==0 ) {
1805 0 : fd_txn_account_t * txn_account = &runtime->executable.accounts[i];
1806 0 : *account = txn_account;
1807 :
1808 0 : if( FD_LIKELY( condition != NULL ) ) {
1809 0 : if( FD_UNLIKELY( !condition( *account, txn_in, txn_out, i ) ) ) {
1810 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
1811 0 : }
1812 0 : }
1813 :
1814 0 : return FD_ACC_MGR_SUCCESS;
1815 0 : }
1816 0 : }
1817 :
1818 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
1819 0 : }
1820 :
1821 : int
1822 : fd_runtime_get_key_of_account_at_index( fd_txn_out_t * txn_out,
1823 : ushort idx,
1824 0 : fd_pubkey_t const * * key ) {
1825 : /* Return a NotEnoughAccountKeys error if idx is out of bounds.
1826 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L218 */
1827 0 : if( FD_UNLIKELY( idx>=txn_out->accounts.accounts_cnt ) ) {
1828 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1829 0 : }
1830 :
1831 0 : *key = &txn_out->accounts.account_keys[ idx ];
1832 0 : return FD_EXECUTOR_INSTR_SUCCESS;
1833 0 : }
1834 :
1835 : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L162 */
1836 : int
1837 : fd_txn_account_is_demotion( const int idx,
1838 : const fd_txn_t * txn_descriptor,
1839 0 : const uint bpf_upgradeable_in_txn ) {
1840 0 : uint is_program = 0U;
1841 0 : for( ulong j=0UL; j<txn_descriptor->instr_cnt; j++ ) {
1842 0 : if( txn_descriptor->instr[j].program_id == idx ) {
1843 0 : is_program = 1U;
1844 0 : break;
1845 0 : }
1846 0 : }
1847 :
1848 0 : return (is_program && !bpf_upgradeable_in_txn);
1849 0 : }
1850 :
1851 : uint
1852 : fd_txn_account_has_bpf_loader_upgradeable( const fd_pubkey_t * account_keys,
1853 0 : const ulong accounts_cnt ) {
1854 0 : for( ulong j=0; j<accounts_cnt; j++ ) {
1855 0 : const fd_pubkey_t * acc = &account_keys[j];
1856 0 : if ( memcmp( acc->uc, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) == 0 ) {
1857 0 : return 1U;
1858 0 : }
1859 0 : }
1860 0 : return 0U;
1861 0 : }
1862 :
1863 : static inline int
1864 : fd_runtime_account_is_writable_idx_flat( const ulong slot,
1865 : const ushort idx,
1866 : const fd_pubkey_t * addr_at_idx,
1867 : const fd_txn_t * txn_descriptor,
1868 : const fd_features_t * features,
1869 0 : const uint bpf_upgradeable_in_txn ) {
1870 : /* https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L43 */
1871 0 : if( !fd_txn_is_writable( txn_descriptor, idx ) ) {
1872 0 : return 0;
1873 0 : }
1874 :
1875 : /* See comments in fd_system_ids.h.
1876 : https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L44 */
1877 0 : if( fd_pubkey_is_active_reserved_key( addr_at_idx ) ||
1878 0 : fd_pubkey_is_pending_reserved_key( addr_at_idx ) ||
1879 0 : ( FD_FEATURE_ACTIVE( slot, features, enable_secp256r1_precompile ) &&
1880 0 : fd_pubkey_is_secp256r1_key( addr_at_idx ) ) ) {
1881 :
1882 0 : return 0;
1883 0 : }
1884 :
1885 0 : if( fd_txn_account_is_demotion( idx, txn_descriptor, bpf_upgradeable_in_txn ) ) {
1886 0 : return 0;
1887 0 : }
1888 :
1889 0 : return 1;
1890 0 : }
1891 :
1892 :
1893 : /* This function aims to mimic the writable accounts check to populate the writable accounts cache, used
1894 : to determine if accounts are writable or not.
1895 :
1896 : https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L38-L47 */
1897 : int
1898 : fd_runtime_account_is_writable_idx( fd_txn_in_t const * txn_in,
1899 : fd_txn_out_t const * txn_out,
1900 : fd_bank_t * bank,
1901 0 : ushort idx ) {
1902 0 : uint bpf_upgradeable = fd_txn_account_has_bpf_loader_upgradeable( txn_out->accounts.account_keys, txn_out->accounts.accounts_cnt );
1903 0 : return fd_runtime_account_is_writable_idx_flat( fd_bank_slot_get( bank ),
1904 0 : idx,
1905 0 : &txn_out->accounts.account_keys[idx],
1906 0 : TXN( txn_in->txn ),
1907 0 : fd_bank_features_query( bank ),
1908 0 : bpf_upgradeable );
1909 0 : }
1910 :
1911 : /* Account pre-condition filtering functions */
1912 :
1913 : int
1914 : fd_runtime_account_check_exists( fd_txn_account_t * acc,
1915 : fd_txn_in_t const * txn_in,
1916 : fd_txn_out_t * txn_out,
1917 0 : ushort idx ) {
1918 0 : (void) txn_in;
1919 0 : (void) txn_out;
1920 0 : (void) idx;
1921 0 : return fd_account_meta_exists( fd_txn_account_get_meta( acc ) );
1922 0 : }
1923 :
1924 : int
1925 : fd_runtime_account_check_fee_payer_writable( fd_txn_account_t * acc,
1926 : fd_txn_in_t const * txn_in,
1927 : fd_txn_out_t * txn_out,
1928 0 : ushort idx ) {
1929 0 : (void) txn_out;
1930 0 : (void) acc;
1931 0 : return fd_txn_is_writable( TXN( txn_in->txn ), idx );
1932 0 : }
|