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