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