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