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