Line data Source code
1 : #include "fd_runtime.h"
2 : #include "fd_bank.h"
3 : #include "../events/fd_event_runtime.h"
4 :
5 : #include "../types/fd_cast.h"
6 : #include "fd_alut.h"
7 : #include "fd_executor.h"
8 : #include "fd_hashes.h"
9 : #include "fd_runtime_stack.h"
10 : #include "fd_accdb_svm.h"
11 : #include "../genesis/fd_genesis_parse.h"
12 : #include "fd_txncache.h"
13 : #include "fd_compute_budget_details.h"
14 : #include "tests/fd_dump_pb.h"
15 :
16 : #include "fd_system_ids.h"
17 : #include "sysvar/fd_sysvar.h"
18 : #include "sysvar/fd_sysvar_clock.h"
19 : #include "sysvar/fd_sysvar_epoch_schedule.h"
20 : #include "sysvar/fd_sysvar_recent_hashes.h"
21 : #include "sysvar/fd_sysvar_stake_history.h"
22 : #include "sysvar/fd_sysvar_last_restart_slot.h"
23 : #include "sysvar/fd_sysvar_slot_hashes.h"
24 : #include "sysvar/fd_sysvar_slot_history.h"
25 :
26 : #include "../stakes/fd_stakes.h"
27 : #include "../rewards/fd_rewards.h"
28 :
29 : #include "program/fd_precompiles.h"
30 : #include "program/vote/fd_vote_state_versioned.h"
31 :
32 : FD_STATIC_ASSERT( MAX_STAKE_WEIGHTS==FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS, vat_stake_weights );
33 :
34 : /*
35 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1254-L1258
36 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1749
37 : */
38 : int
39 : fd_runtime_compute_max_tick_height( ulong ticks_per_slot,
40 : ulong slot,
41 0 : ulong * out_max_tick_height /* out */ ) {
42 0 : ulong max_tick_height = 0UL;
43 0 : if( FD_LIKELY( ticks_per_slot > 0UL ) ) {
44 0 : ulong next_slot = fd_ulong_sat_add( slot, 1UL );
45 0 : if( FD_UNLIKELY( next_slot == slot ) ) {
46 0 : FD_LOG_WARNING(( "max tick height addition overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
47 0 : return -1;
48 0 : }
49 0 : if( FD_UNLIKELY( ULONG_MAX / ticks_per_slot < next_slot ) ) {
50 0 : FD_LOG_WARNING(( "max tick height multiplication overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
51 0 : return -1;
52 0 : }
53 0 : max_tick_height = fd_ulong_sat_mul( next_slot, ticks_per_slot );
54 0 : }
55 0 : *out_max_tick_height = max_tick_height;
56 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
57 0 : }
58 :
59 : void
60 : fd_runtime_update_next_leaders( fd_bank_t * bank,
61 0 : fd_runtime_stack_t * runtime_stack ) {
62 :
63 0 : fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
64 :
65 0 : ulong epoch = fd_slot_to_epoch ( epoch_schedule, bank->f.slot, NULL ) + 1UL;
66 0 : ulong slot0 = fd_epoch_slot0 ( epoch_schedule, epoch );
67 0 : ulong slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
68 :
69 0 : fd_vote_stakes_t const * vote_stakes = fd_bank_vote_stakes( bank );
70 0 : fd_vote_stake_weight_t * epoch_weights = runtime_stack->stakes.stake_weights;
71 0 : ulong stake_weight_cnt = fd_stake_weights_by_node( vote_stakes, bank->vote_stakes_fork_id, FD_VOTE_STAKES_ITER_T_1, epoch_weights );
72 0 : FD_TEST( stake_weight_cnt<=MAX_STAKE_WEIGHTS );
73 :
74 0 : void * epoch_leaders_mem = fd_bank_epoch_leaders_modify( bank, epoch );
75 0 : fd_epoch_leaders_t * leaders = fd_epoch_leaders_join( fd_epoch_leaders_new(
76 0 : epoch_leaders_mem,
77 0 : epoch,
78 0 : slot0,
79 0 : slot_cnt,
80 0 : stake_weight_cnt,
81 0 : epoch_weights ) );
82 0 : if( FD_UNLIKELY( !leaders ) ) {
83 0 : FD_LOG_ERR(( "Unable to init and join fd_epoch_leaders" ));
84 0 : }
85 :
86 0 : memcpy( runtime_stack->epoch_weights.next_stake_weights, epoch_weights, stake_weight_cnt*sizeof(fd_vote_stake_weight_t) );
87 0 : runtime_stack->epoch_weights.next_stake_weights_cnt = stake_weight_cnt;
88 :
89 0 : ulong staked_cnt = compute_id_weights_from_vote_weights( runtime_stack->stakes.id_weights, epoch_weights, stake_weight_cnt );
90 0 : FD_TEST( staked_cnt<=MAX_STAKE_WEIGHTS );
91 0 : memcpy( runtime_stack->epoch_weights.next_id_weights, runtime_stack->stakes.id_weights, staked_cnt * sizeof(fd_stake_weight_t) );
92 0 : runtime_stack->epoch_weights.next_id_weights_cnt = staked_cnt;
93 0 : }
94 :
95 : void
96 : fd_runtime_update_leaders( fd_bank_t * bank,
97 282 : fd_runtime_stack_t * runtime_stack ) {
98 :
99 282 : fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
100 :
101 282 : ulong epoch = fd_slot_to_epoch ( epoch_schedule, bank->f.slot, NULL );
102 282 : ulong slot0 = fd_epoch_slot0 ( epoch_schedule, epoch );
103 282 : ulong slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
104 :
105 282 : fd_vote_stakes_t const * vote_stakes = fd_bank_vote_stakes( bank );
106 282 : fd_vote_stake_weight_t * epoch_weights = runtime_stack->stakes.stake_weights;
107 282 : ulong stake_weight_cnt = fd_stake_weights_by_node( vote_stakes, bank->vote_stakes_fork_id, FD_VOTE_STAKES_ITER_T_2, epoch_weights );
108 282 : FD_TEST( stake_weight_cnt<=MAX_STAKE_WEIGHTS );
109 :
110 : /* TODO: Can optimize by avoiding recomputing if another fork has
111 : already computed them for this epoch. */
112 282 : void * epoch_leaders_mem = fd_bank_epoch_leaders_modify( bank, epoch );
113 282 : fd_epoch_leaders_t * leaders = fd_epoch_leaders_join( fd_epoch_leaders_new(
114 282 : epoch_leaders_mem,
115 282 : epoch,
116 282 : slot0,
117 282 : slot_cnt,
118 282 : stake_weight_cnt,
119 282 : epoch_weights ) );
120 282 : if( FD_UNLIKELY( !leaders ) ) {
121 0 : FD_LOG_ERR(( "Unable to init and join fd_epoch_leaders" ));
122 0 : }
123 :
124 282 : memcpy( runtime_stack->epoch_weights.stake_weights, epoch_weights, stake_weight_cnt*sizeof(fd_vote_stake_weight_t) );
125 282 : runtime_stack->epoch_weights.stake_weights_cnt = stake_weight_cnt;
126 :
127 282 : ulong staked_cnt = compute_id_weights_from_vote_weights( runtime_stack->stakes.id_weights, epoch_weights, stake_weight_cnt );
128 282 : FD_TEST( staked_cnt<=MAX_STAKE_WEIGHTS );
129 282 : memcpy( runtime_stack->epoch_weights.id_weights, runtime_stack->stakes.id_weights, staked_cnt * sizeof(fd_stake_weight_t) );
130 282 : runtime_stack->epoch_weights.id_weights_cnt = staked_cnt;
131 282 : }
132 :
133 : /******************************************************************************/
134 : /* Various Private Runtime Helpers */
135 : /******************************************************************************/
136 :
137 : /* Validates the fee collector account before depositing fees. Returns
138 : 0 on success. Returns 1 (fee is burned) if the fee collector is not
139 : owned by the system program, the deposit overflows the collector's
140 : balance, or the rent state transition is invalid.
141 :
142 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.2/runtime/src/bank/fee_distribution.rs#L205-L229 */
143 : static int
144 : fd_runtime_validate_fee_collector( fd_bank_t const * bank,
145 : fd_acc_t const * collector,
146 27 : ulong fee ) {
147 27 : FD_TEST( fee );
148 :
149 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/runtime/src/bank/fee_distribution.rs#L206-L208 */
150 27 : if( FD_UNLIKELY( memcmp( collector->owner, fd_solana_system_program_id.uc, sizeof(fd_pubkey_t) ) ) ) return 1;
151 :
152 : /* Lamport overflow burns the fee.
153 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.2/runtime/src/bank/fee_distribution.rs#L210-L214 */
154 24 : ulong pre_balance = collector->lamports;
155 24 : ulong post_balance;
156 24 : if( FD_UNLIKELY( __builtin_uaddl_overflow( pre_balance, fee, &post_balance ) ) ) return 1;
157 :
158 21 : return !!fd_executor_check_static_account_rent_state_transition(
159 21 : pre_balance,
160 21 : post_balance,
161 21 : collector->data_len,
162 21 : &bank->f.rent,
163 21 : FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check )
164 21 : );
165 24 : }
166 :
167 : /* Validates an external SIMD-0232 block revenue collector after the
168 : fee reward has been added. Returns 0 to deposit, 1 to burn. The
169 : vote account itself is always valid and must not be passed here.
170 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/bank/fee_distribution.rs#L231-L270 */
171 :
172 : static int
173 : fd_runtime_validate_block_revenue_collector( fd_bank_t const * bank,
174 : fd_pubkey_t const * collector_id,
175 : ulong pre_lamports,
176 123 : fd_acc_t const * collector ) {
177 : /* Must be a system program owned account. */
178 123 : if( FD_UNLIKELY( memcmp( collector->owner, fd_solana_system_program_id.uc, sizeof(fd_pubkey_t) ) ) ) return 1;
179 :
180 : /* Must not be a reserved account. */
181 57 : if( FD_UNLIKELY( fd_pubkey_is_active_reserved_key( collector_id ) ||
182 57 : fd_pubkey_is_pending_reserved_key( collector_id ) ) ) return 1;
183 :
184 : /* The incinerator is exempt from the rent check so the deposit
185 : always works. Incinerator funds are burned at the end of the
186 : block. */
187 24 : if( FD_UNLIKELY( fd_pubkey_eq( collector_id, &fd_sysvar_incinerator_id ) ) ) return 0;
188 :
189 : /* Must be rent-exempt after the deposit. With
190 : relax_post_exec_min_balance_check (SIMD-0392) a pre-existing
191 : account may stay rent-paying. */
192 21 : int is_rent_exempt = collector->lamports>=fd_rent_exempt_minimum_balance( &bank->f.rent, collector->data_len );
193 21 : return !is_rent_exempt &&
194 21 : ( !FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check ) || !pre_lamports );
195 24 : }
196 :
197 : /* fd_runtime_settle_fees settles transaction fees accumulated during a
198 : slot. A portion is burnt, another portion is credited to the fee
199 : collector (typically leader). */
200 :
201 : void
202 : fd_runtime_fee_split( ulong execution_fees,
203 : ulong priority_fees,
204 : ulong * burn,
205 357 : ulong * reward ) {
206 357 : *burn = execution_fees/2UL;
207 357 : *reward = fd_ulong_sat_add( priority_fees, execution_fees-*burn );
208 357 : }
209 :
210 : static void
211 : fd_runtime_settle_fees( fd_bank_t * bank,
212 : fd_accdb_t * accdb,
213 357 : fd_capture_ctx_t * capture_ctx ) {
214 357 : ulong slot = bank->f.slot;
215 357 : ulong execution_fees = bank->f.execution_fees;
216 357 : ulong priority_fees = bank->f.priority_fees;
217 357 : ulong total_fees;
218 357 : if( FD_UNLIKELY( __builtin_uaddl_overflow( execution_fees, priority_fees, &total_fees ) ) ) {
219 0 : FD_LOG_EMERG(( "fee overflow detected (slot=%lu execution_fees=%lu priority_fees=%lu)",
220 0 : slot, execution_fees, priority_fees ));
221 0 : }
222 :
223 357 : ulong fee_burn, fee_reward;
224 357 : fd_runtime_fee_split( execution_fees, priority_fees, &fee_burn, &fee_reward );
225 :
226 : /* Remove fee balance from bank (decreasing capitalization).
227 : Allow underflow (wrap) to match Agave's silent fetch_sub behavior. */
228 357 : bank->f.capitalization -= total_fees;
229 357 : bank->f.execution_fees = 0;
230 357 : bank->f.priority_fees = 0;
231 :
232 357 : if( FD_LIKELY( fee_reward ) ) {
233 159 : fd_epoch_leaders_t const * leaders = fd_bank_epoch_leaders_query( bank, bank->f.epoch );
234 159 : fd_pubkey_t const * leader = fd_epoch_leaders_get( leaders, bank->f.slot );
235 159 : if( FD_UNLIKELY( !leader ) ) FD_LOG_CRIT(( "fd_epoch_leaders_get(%lu) returned NULL", bank->f.slot ));
236 :
237 : /* Per SIMD-0232, the fee reward goes to the leader's block revenue
238 : collector from the vote account state the leader schedule was
239 : derived from (captured entering the previous epoch, tag
240 : epoch-1); default is the leader identity.
241 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/bank/fee_distribution.rs#L121-L148 */
242 159 : int custom_commission_collector = FD_FEATURE_ACTIVE_BANK( bank, custom_commission_collector );
243 :
244 159 : fd_pubkey_t const * collector_id = leader;
245 159 : fd_pubkey_t const * leader_vote = NULL;
246 159 : fd_pubkey_t override_collector;
247 159 : if( custom_commission_collector ) {
248 132 : leader_vote = fd_epoch_leaders_get_vote( leaders, bank->f.slot );
249 132 : if( FD_UNLIKELY( !leader_vote ) ) FD_LOG_CRIT(( "fd_epoch_leaders_get_vote(%lu) returned NULL", bank->f.slot ));
250 132 : int flags = fd_collector_overrides_query( fd_bank_collector_overrides( bank ),
251 132 : bank->collector_overrides_fork_id,
252 132 : fd_ulong_sat_sub( bank->f.epoch, 1UL ),
253 132 : leader_vote,
254 132 : NULL,
255 132 : &override_collector );
256 132 : if( FD_UNLIKELY( flags & FD_COLLECTOR_OVERRIDE_BLOCK ) ) collector_id = &override_collector;
257 132 : }
258 :
259 : /* Pay out reward portion of collected fees (increasing capitalization) */
260 159 : fd_accdb_svm_update_t update[1];
261 159 : fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, collector_id, 1 );
262 159 : int burn;
263 159 : if( custom_commission_collector ) {
264 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/bank/fee_distribution.rs#L184-L204 */
265 132 : ulong post_balance;
266 132 : burn = __builtin_uaddl_overflow( acc.lamports, fee_reward, &post_balance );
267 132 : if( FD_LIKELY( !burn ) ) {
268 129 : acc.lamports = post_balance;
269 : /* The vote account itself is always a valid collector. */
270 129 : if( !fd_pubkey_eq( collector_id, leader_vote ) ) {
271 123 : burn = fd_runtime_validate_block_revenue_collector( bank, collector_id, update->lamports_before, &acc );
272 123 : }
273 129 : }
274 132 : if( FD_UNLIKELY( burn ) ) acc.lamports = update->lamports_before;
275 132 : } else {
276 27 : burn = fd_runtime_validate_fee_collector( bank, &acc, fee_reward );
277 27 : if( FD_LIKELY( !burn ) ) {
278 9 : acc.lamports += fee_reward; /* guaranteed to not overflow, checked above */
279 9 : }
280 27 : }
281 159 : if( FD_UNLIKELY( burn ) ) {
282 126 : FD_LOG_INFO(( "slot %lu has an invalid fee collector, burning fee reward (%lu lamports)", bank->f.slot, fee_reward ));
283 126 : }
284 159 : if( FD_LIKELY( !burn ) ) fd_stakes_update_stake_delegation( collector_id, &acc, bank, NULL );
285 159 : fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
286 159 : }
287 :
288 357 : FD_LOG_INFO(( "slot=%lu priority_fees=%lu execution_fees=%lu fee_burn=%lu fee_rewards=%lu",
289 357 : slot,
290 357 : priority_fees, execution_fees, fee_burn, fee_reward ));
291 357 : }
292 :
293 : static void
294 : fd_runtime_freeze( fd_bank_t * bank,
295 : fd_accdb_t * accdb,
296 357 : fd_capture_ctx_t * capture_ctx ) {
297 357 : if( FD_LIKELY( bank->f.slot ) ) fd_sysvar_recent_hashes_update( bank, accdb, capture_ctx );
298 357 : fd_sysvar_slot_history_update( bank, accdb, capture_ctx );
299 357 : fd_runtime_settle_fees( bank, accdb, capture_ctx );
300 :
301 : /* jito collects a 3% fee at the end of the block + 3% fee at
302 : distribution time. */
303 357 : ulong tips_pre_comission = bank->f.tips;
304 357 : bank->f.tips = (tips_pre_comission - (tips_pre_comission * 6UL / 100UL));
305 :
306 357 : fd_accdb_svm_remove( bank, accdb, capture_ctx, &fd_sysvar_incinerator_id );
307 357 : }
308 :
309 : /******************************************************************************/
310 : /* Block-Level Execution Preparation/Finalization */
311 : /******************************************************************************/
312 : void
313 : fd_runtime_new_fee_rate_governor_derived( fd_bank_t * bank,
314 4518 : ulong latest_signatures_per_slot ) {
315 :
316 4518 : fd_fee_rate_governor_t const * base_fee_rate_governor = &bank->f.fee_rate_governor;
317 :
318 4518 : ulong old_lamports_per_signature = bank->f.rbh_lamports_per_sig;
319 :
320 4518 : fd_fee_rate_governor_t me = {
321 4518 : .target_signatures_per_slot = base_fee_rate_governor->target_signatures_per_slot,
322 4518 : .target_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature,
323 4518 : .max_lamports_per_signature = base_fee_rate_governor->max_lamports_per_signature,
324 4518 : .min_lamports_per_signature = base_fee_rate_governor->min_lamports_per_signature,
325 4518 : .burn_percent = base_fee_rate_governor->burn_percent
326 4518 : };
327 :
328 4518 : ulong new_lamports_per_signature = 0;
329 4518 : if( me.target_signatures_per_slot > 0 ) {
330 6 : me.min_lamports_per_signature = fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 2) );
331 6 : me.max_lamports_per_signature = me.target_lamports_per_signature * 10;
332 6 : ulong desired_lamports_per_signature = fd_ulong_min(
333 6 : me.max_lamports_per_signature,
334 6 : fd_ulong_max(
335 6 : me.min_lamports_per_signature,
336 6 : me.target_lamports_per_signature
337 6 : * fd_ulong_min(latest_signatures_per_slot, (ulong)UINT_MAX)
338 6 : / me.target_signatures_per_slot
339 6 : )
340 6 : );
341 6 : long gap = (long)desired_lamports_per_signature - (long)old_lamports_per_signature;
342 6 : if ( gap == 0 ) {
343 0 : new_lamports_per_signature = desired_lamports_per_signature;
344 6 : } else {
345 6 : long gap_adjust = (long)(fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 20) ))
346 6 : * (gap != 0)
347 6 : * (gap > 0 ? 1 : -1);
348 6 : new_lamports_per_signature = fd_ulong_min(
349 6 : me.max_lamports_per_signature,
350 6 : fd_ulong_max(
351 6 : me.min_lamports_per_signature,
352 6 : (ulong)((long)old_lamports_per_signature + gap_adjust)
353 6 : )
354 6 : );
355 6 : }
356 4512 : } else {
357 4512 : new_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature;
358 4512 : me.min_lamports_per_signature = me.target_lamports_per_signature;
359 4512 : me.max_lamports_per_signature = me.target_lamports_per_signature;
360 4512 : }
361 4518 : bank->f.fee_rate_governor = me;
362 4518 : bank->f.rbh_lamports_per_sig = new_lamports_per_signature;
363 4518 : }
364 :
365 : /******************************************************************************/
366 : /* Epoch Boundary */
367 : /******************************************************************************/
368 :
369 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6704 */
370 : static void
371 : fd_apply_builtin_program_feature_transitions( fd_bank_t * bank,
372 : fd_accdb_t * accdb,
373 : fd_runtime_stack_t * runtime_stack,
374 282 : fd_capture_ctx_t * capture_ctx ) {
375 : /* TODO: Set the upgrade authority properly from the core bpf migration config. Right now it's set to None.
376 :
377 : Migrate any necessary stateless builtins to core BPF. So far,
378 : the only "stateless" builtin is the Feature program. Beginning
379 : checks in the migrate_builtin_to_core_bpf function will fail if the
380 : program has already been migrated to BPF. */
381 :
382 282 : fd_builtin_program_t const * builtins = fd_builtins();
383 2820 : for( ulong i=0UL; i<fd_num_builtins(); i++ ) {
384 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6732-L6751 */
385 2538 : if( builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( bank->f.slot, &bank->f.features, builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
386 0 : FD_BASE58_ENCODE_32_BYTES( builtins[i].pubkey->key, pubkey_b58 );
387 0 : FD_LOG_DEBUG(( "Migrating builtin program %s to core BPF", pubkey_b58 ));
388 0 : fd_migrate_builtin_to_core_bpf( bank, accdb, runtime_stack, builtins[i].core_bpf_migration_config, capture_ctx );
389 0 : }
390 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6753-L6774 */
391 2538 : if( builtins[i].enable_feature_offset!=NO_ENABLE_FEATURE_ID && FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, builtins[i].enable_feature_offset ) ) {
392 0 : FD_BASE58_ENCODE_32_BYTES( builtins[i].pubkey->key, pubkey_b58 );
393 0 : FD_LOG_DEBUG(( "Enabling builtin program %s", pubkey_b58 ));
394 0 : fd_write_builtin_account( bank, accdb, capture_ctx, *builtins[i].pubkey, builtins[i].data,strlen(builtins[i].data) );
395 0 : }
396 2538 : }
397 :
398 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6776-L6793 */
399 282 : fd_stateless_builtin_program_t const * stateless_builtins = fd_stateless_builtins();
400 846 : for( ulong i=0UL; i<fd_num_stateless_builtins(); i++ ) {
401 564 : if( stateless_builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( bank->f.slot, &bank->f.features, stateless_builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
402 0 : FD_BASE58_ENCODE_32_BYTES( stateless_builtins[i].pubkey->key, pubkey_b58 );
403 0 : FD_LOG_DEBUG(( "Migrating stateless builtin program %s to core BPF", pubkey_b58 ));
404 0 : fd_migrate_builtin_to_core_bpf( bank, accdb, runtime_stack, stateless_builtins[i].core_bpf_migration_config, capture_ctx );
405 0 : }
406 564 : }
407 :
408 : /* https://github.com/anza-xyz/agave/blob/c1080de464cfb578c301e975f498964b5d5313db/runtime/src/bank.rs#L6795-L6805 */
409 1128 : for( fd_precompile_program_t const * precompiles = fd_precompiles(); precompiles->verify_fn; precompiles++ ) {
410 846 : if( precompiles->feature_offset != NO_ENABLE_FEATURE_ID &&
411 846 : FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, precompiles->feature_offset ) ) {
412 0 : fd_write_builtin_account( bank, accdb, capture_ctx, *precompiles->pubkey, "", 0 );
413 0 : }
414 846 : }
415 282 : }
416 :
417 : static void
418 : fd_feature_activate( fd_bank_t * bank,
419 : fd_accdb_t * accdb,
420 : fd_capture_ctx_t * capture_ctx,
421 : fd_feature_id_t const * id,
422 83472 : fd_pubkey_t const * addr ) {
423 83472 : fd_features_set( &bank->f.features, id, FD_FEATURE_DISABLED );
424 :
425 83472 : if( FD_UNLIKELY( id->reverted==1 ) ) return;
426 :
427 78678 : fd_acc_t acc = fd_accdb_read_one( accdb, bank->accdb_fork_id, addr->uc );
428 78678 : if( FD_UNLIKELY( !acc.lamports || memcmp( acc.owner, fd_solana_feature_program_id.uc, 32UL ) ) ) {
429 78507 : fd_accdb_unread_one( accdb, &acc ); /* Feature account not yet initialized */
430 78507 : return;
431 78507 : }
432 :
433 171 : fd_feature_t feature;
434 171 : if( FD_UNLIKELY( !fd_feature_decode( &feature, acc.data, acc.data_len ) ) ) {
435 3 : FD_BASE58_ENCODE_32_BYTES( addr->uc, addr_b58 );
436 3 : FD_LOG_WARNING(( "cannot activate feature %s, corrupt account data", addr_b58 ));
437 3 : FD_LOG_HEXDUMP_NOTICE(( "corrupt feature account", acc.data, acc.data_len ));
438 3 : fd_accdb_unread_one( accdb, &acc );
439 3 : return;
440 3 : }
441 168 : fd_accdb_unread_one( accdb, &acc );
442 :
443 168 : FD_BASE58_ENCODE_32_BYTES( addr->uc, addr_b58 );
444 168 : if( FD_UNLIKELY( feature.is_active ) ) {
445 129 : FD_LOG_DEBUG(( "feature %s already activated at slot %lu", addr_b58, feature.activation_slot ));
446 129 : fd_features_set( &bank->f.features, id, feature.activation_slot);
447 129 : } else {
448 39 : FD_LOG_DEBUG(( "feature %s not activated at slot %lu, activating", addr_b58, bank->f.slot ));
449 39 : fd_accdb_svm_update_t update[1];
450 39 : fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, addr, 0 );
451 39 : if( FD_UNLIKELY( !acc.lamports ) ) return;
452 39 : FD_TEST( acc.data_len>=sizeof(fd_feature_t) );
453 :
454 39 : feature.is_active = 1;
455 39 : feature.activation_slot = bank->f.slot;
456 39 : FD_STORE( fd_feature_t, acc.data, feature );
457 39 : fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
458 39 : if( FD_UNLIKELY( fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_epoch_feature( addr->uc );
459 39 : }
460 168 : }
461 :
462 : static void
463 : fd_features_activate( fd_bank_t * bank,
464 : fd_accdb_t * accdb,
465 282 : fd_capture_ctx_t * capture_ctx ) {
466 282 : for( fd_feature_id_t const * id = fd_feature_iter_init();
467 83754 : !fd_feature_iter_done( id );
468 83472 : id = fd_feature_iter_next( id ) ) {
469 83472 : fd_feature_activate( bank, accdb, capture_ctx, id, &id->id );
470 83472 : }
471 282 : }
472 :
473 : /* SIMD-0194: deprecate_rent_exemption_threshold
474 : https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5322-L5329 */
475 : static void
476 : deprecate_rent_exemption_threshold( fd_bank_t * bank,
477 : fd_accdb_t * accdb,
478 0 : fd_capture_ctx_t * capture_ctx ) {
479 : /* We use the bank fields here to mirror Agave - in mainnet, devnet
480 : and testnet Agave's bank rent.burn_percent field is different to
481 : the value in the sysvar. When this feature is activated in Agave,
482 : the sysvar inherits the value from the bank. */
483 0 : fd_rent_t rent = bank->f.rent;
484 0 : rent.lamports_per_uint8_year = fd_rust_cast_double_to_ulong(
485 0 : (double)rent.lamports_per_uint8_year * rent.exemption_threshold );
486 0 : rent.exemption_threshold = FD_SIMD_0194_NEW_RENT_EXEMPTION_THRESHOLD;
487 0 : rent.burn_percent = FD_SIMD_0194_NEW_BURN_PERCENT;
488 :
489 : /* We don't refresh the sysvar cache here. The cache is refreshed in
490 : fd_sysvar_cache_restore, which is called at the start of every
491 : block in fd_runtime_block_execute_prepare, after this function. */
492 0 : bank->f.rent = rent;
493 0 : fd_sysvar_rent_write( bank, accdb, capture_ctx, &rent );
494 0 : }
495 :
496 : static void
497 : set_lamports_per_byte( fd_bank_t * bank,
498 : fd_accdb_t * accdb,
499 : fd_capture_ctx_t * capture_ctx,
500 36 : ulong lamports_per_byte ) {
501 36 : fd_rent_t rent = bank->f.rent;
502 36 : rent.lamports_per_uint8_year = lamports_per_byte;
503 :
504 36 : bank->f.rent = rent;
505 36 : fd_sysvar_rent_write( bank, accdb, capture_ctx, &rent );
506 36 : }
507 :
508 : // https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5296-L5391
509 : static void
510 : fd_compute_and_apply_new_feature_activations( fd_bank_t * bank,
511 : fd_accdb_t * accdb,
512 : fd_runtime_stack_t * runtime_stack,
513 282 : fd_capture_ctx_t * capture_ctx ) {
514 : /* Activate new features
515 : https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5296-L5391 */
516 282 : fd_features_activate( bank, accdb, capture_ctx );
517 282 : fd_features_restore( &bank->f.features, accdb, bank->accdb_fork_id, bank->f.slot, &bank->f.epoch_schedule );
518 :
519 : /* SIMD-0194: deprecate_rent_exemption_threshold
520 : https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5322-L5329 */
521 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, deprecate_rent_exemption_threshold ) ) ) {
522 0 : deprecate_rent_exemption_threshold( bank, accdb, capture_ctx );
523 0 : }
524 :
525 : /* SIMD-0437 rent reduction gates.
526 : https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/runtime/src/bank.rs#L5612-L5639 */
527 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_6333 ) ) ) {
528 6 : set_lamports_per_byte( bank, accdb, capture_ctx, 6333UL );
529 6 : }
530 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_5080 ) ) ) {
531 6 : set_lamports_per_byte( bank, accdb, capture_ctx, 5080UL );
532 6 : }
533 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_2575 ) ) ) {
534 6 : set_lamports_per_byte( bank, accdb, capture_ctx, 2575UL );
535 6 : }
536 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_1322 ) ) ) {
537 6 : set_lamports_per_byte( bank, accdb, capture_ctx, 1322UL );
538 6 : }
539 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_696 ) ) ) {
540 6 : set_lamports_per_byte( bank, accdb, capture_ctx, 696UL );
541 6 : }
542 :
543 : /* SIMD-0438 resets rent to the legacy value (in case something goes
544 : wrong with the above).
545 : https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/runtime/src/bank.rs#L5641-L5644 */
546 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_6960 ) ) ) {
547 6 : set_lamports_per_byte( bank, accdb, capture_ctx, 6960UL );
548 6 : }
549 :
550 : /* SIMD-0550: double_disinflation_rate
551 : https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/runtime/src/bank.rs#L6232-L6234 */
552 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, double_disinflation_rate ) ) ) {
553 0 : fd_apply_double_disinflation_rate( bank );
554 0 : }
555 :
556 : /* Apply builtin program feature transitions
557 : https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6621-L6624 */
558 282 : fd_apply_builtin_program_feature_transitions( bank, accdb, runtime_stack, capture_ctx );
559 :
560 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, vote_state_v4 ) ) ) {
561 0 : fd_upgrade_core_bpf_program( bank, accdb, runtime_stack, &fd_solana_stake_program_id, &fd_solana_stake_program_vote_state_v4_buffer_address, capture_ctx );
562 0 : }
563 :
564 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/runtime/src/bank.rs#L5703-L5716 */
565 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, replace_spl_token_with_p_token ) ) ) {
566 0 : fd_upgrade_loader_v2_program_with_loader_v3_program(
567 0 : bank,
568 0 : accdb,
569 0 : runtime_stack,
570 0 : &fd_solana_spl_token_id,
571 0 : &fd_solana_ptoken_program_buffer_address,
572 0 : FD_FEATURE_ACTIVE_BANK( bank, relax_programdata_account_check_migration ),
573 0 : capture_ctx );
574 0 : }
575 :
576 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.4/runtime/src/bank.rs#L5736-L5744 */
577 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, upgrade_bpf_stake_program_to_v5 ) ) ) {
578 0 : fd_upgrade_core_bpf_program(
579 0 : bank,
580 0 : accdb,
581 0 : runtime_stack,
582 0 : &fd_solana_stake_program_id,
583 0 : &fd_solana_stake_program_v5_buffer_address,
584 0 : capture_ctx );
585 0 : }
586 :
587 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/bank.rs#L6182-L6190 */
588 282 : if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ) ) ) {
589 0 : fd_upgrade_core_bpf_program(
590 0 : bank,
591 0 : accdb,
592 0 : runtime_stack,
593 0 : &fd_solana_stake_program_id,
594 0 : &fd_solana_stake_program_v5_1_buffer_address,
595 0 : capture_ctx );
596 0 : }
597 282 : }
598 :
599 : /* Starting a new epoch.
600 : New epoch: T
601 : Just ended epoch: T-1
602 : Epoch before: T-2
603 :
604 : In this function:
605 : - stakes in T-2 (vote_states_prev_prev) should be replaced by T-1 (vote_states_prev)
606 : - stakes at T-1 (vote_states_prev) should be replaced by updated stakes at T (vote_states)
607 : - leader schedule should be calculated using new T-2 stakes (vote_states_prev_prev)
608 :
609 : Invariant during an epoch T:
610 : vote_states_prev holds the stakes at T-1
611 : vote_states_prev_prev holds the stakes at T-2
612 : */
613 : /* process for the start of a new epoch
614 : https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/runtime/src/bank.rs#L1811-L1899 */
615 : static void
616 : fd_runtime_process_new_epoch( fd_banks_t * banks,
617 : fd_bank_t * bank,
618 : fd_accdb_t * accdb,
619 : fd_capture_ctx_t * capture_ctx,
620 : ulong parent_epoch,
621 282 : fd_runtime_stack_t * runtime_stack ) {
622 282 : long start = fd_log_wallclock();
623 282 : ulong epoch_start_capitalization = bank->f.capitalization;
624 :
625 282 : fd_compute_and_apply_new_feature_activations( bank, accdb, runtime_stack, capture_ctx );
626 :
627 282 : bank->f.slot_params = fd_slot_params_at_slot( bank, bank->f.slot );
628 :
629 : /* Update the cached warmup/cooldown rate epoch now that features may
630 : have changed (reduce_stake_warmup_cooldown may have just activated). */
631 282 : bank->f.warmup_cooldown_rate_epoch = fd_slot_to_epoch( &bank->f.epoch_schedule,
632 282 : bank->f.features.reduce_stake_warmup_cooldown,
633 282 : NULL );
634 :
635 : /* Updates stake history sysvar accumulated values and recomputes
636 : stake delegations for vote accounts. */
637 :
638 282 : ushort stake_delegations_fork_ids[ banks->max_total_banks ];
639 282 : ulong stake_delegations_fork_id_cnt = fd_banks_stake_delegations_fork_ids( banks, bank, stake_delegations_fork_ids );
640 282 : fd_stake_history_t stake_delegations_history_[1];
641 282 : fd_stake_history_t * stake_delegations_history = fd_sysvar_cache_stake_history_view( &bank->f.sysvar_cache, stake_delegations_history_ );
642 :
643 282 : fd_stake_delegations_t * stake_delegations = fd_bank_stake_delegations_modify( bank );
644 282 : if( FD_UNLIKELY( !stake_delegations ) ) {
645 0 : FD_LOG_CRIT(( "stake_delegations is NULL" ));
646 0 : }
647 282 : fd_stake_delegations_frontier_query_begin( stake_delegations,
648 282 : bank->f.epoch,
649 282 : stake_delegations_history,
650 282 : &bank->f.warmup_cooldown_rate_epoch,
651 282 : FD_FEATURE_ACTIVE_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ),
652 282 : stake_delegations_fork_ids,
653 282 : stake_delegations_fork_id_cnt );
654 :
655 : /* Wipe WARMED tags awarded under the old floating point math when the
656 : fixed point math activates. This will force all the effective
657 : stakes to be re-computed using the post-activation math.
658 : Unfortunately, one wipe at the activation boundary is not enough.
659 : The consensus root lags the replay frontier. So
660 : post-activation/post-boundary, when the unrooted pre-activation
661 : blocks root and their deltas are applied into the root, their
662 : pre-activation tags will also leak into the root pool. So the
663 : award sites record whether any live WARMED tag might have been
664 : computed with the pre-activation floating point math, and we wipe
665 : whenever that is the case. We expect the invalidation to fire
666 : exactly twice: at the activation boundary and at the first boundary
667 : after it, unless some extremely sparse epochs happen after
668 : activation. Tags are only used at boundaries for now, and this
669 : runs before any use. */
670 282 : if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ) && stake_delegations->fp_warmed_awarded ) ) {
671 0 : fd_stake_delegations_invalidate_warmed( stake_delegations );
672 0 : }
673 :
674 282 : fd_stakes_activate_epoch( bank, runtime_stack, accdb, capture_ctx, stake_delegations,
675 282 : &bank->f.warmup_cooldown_rate_epoch );
676 :
677 : /* Distribute rewards. This involves calculating the rewards for
678 : every vote and stake account. */
679 :
680 282 : fd_hash_t const * parent_blockhash = fd_blockhashes_peek_last_hash( &bank->f.block_hash_queue );
681 282 : fd_begin_partitioned_rewards( bank,
682 282 : accdb,
683 282 : runtime_stack,
684 282 : capture_ctx,
685 282 : stake_delegations,
686 282 : parent_blockhash,
687 282 : parent_epoch,
688 282 : epoch_start_capitalization );
689 :
690 : /* Update stake history balance and capitalization only after epoch
691 : reward partitions have been calculated. */
692 282 : fd_stake_history_ensure_rent_exempt( bank, accdb, capture_ctx );
693 :
694 282 : fd_stake_delegations_frontier_query_end( stake_delegations,
695 282 : stake_delegations_history,
696 282 : &bank->f.warmup_cooldown_rate_epoch,
697 282 : FD_FEATURE_ACTIVE_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ),
698 282 : stake_delegations_fork_ids,
699 282 : stake_delegations_fork_id_cnt );
700 :
701 : /* The Agave client handles updating their stakes cache with a call to
702 : update_epoch_stakes() which keys stakes by the leader schedule
703 : epochs and retains up to 6 epochs of stakes. However, to correctly
704 : calculate the leader schedule, we just need to maintain the vote
705 : states for the current epoch, the previous epoch, and the one
706 : before that.
707 : https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/bank.rs#L2175
708 : */
709 :
710 : /* Now that our stakes caches have been updated, we can calculate the
711 : leader schedule for the upcoming epoch epoch using our new
712 : vote_states_prev_prev (stakes for T-2). */
713 :
714 282 : fd_runtime_update_leaders( bank, runtime_stack );
715 :
716 282 : if( FD_UNLIKELY( fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_epoch_emit( bank );
717 :
718 282 : long end = fd_log_wallclock();
719 282 : FD_LOG_NOTICE(( "starting epoch %s%lu%s at slot %lu %s(took %.6f seconds)%s", fd_log_style_bold(), bank->f.epoch, fd_log_style_normal(), bank->f.slot, fd_log_style_dim(), (double)(end - start) / 1e9, fd_log_style_normal() ));
720 282 : }
721 :
722 : static void
723 : fd_runtime_block_pre_execute_process_new_epoch( fd_banks_t * banks,
724 : fd_bank_t * bank,
725 : fd_accdb_t * accdb,
726 : fd_capture_ctx_t * capture_ctx,
727 : fd_runtime_stack_t * runtime_stack,
728 4518 : int * is_epoch_boundary ) {
729 :
730 4518 : ulong const slot = bank->f.slot;
731 4518 : if( FD_LIKELY( slot != 0UL ) ) {
732 4518 : fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
733 :
734 4518 : ulong prev_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.parent_slot, NULL );
735 4518 : ulong slot_idx;
736 4518 : ulong new_epoch = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
737 4518 : if( FD_UNLIKELY( slot_idx==1UL && new_epoch==0UL ) ) {
738 : /* The block after genesis has a height of 1. */
739 0 : bank->f.block_height = 1UL;
740 0 : }
741 :
742 4518 : if( FD_UNLIKELY( prev_epoch<new_epoch || !slot_idx ) ) {
743 282 : FD_LOG_DEBUG(( "Epoch boundary starting" ));
744 282 : fd_runtime_process_new_epoch( banks, bank, accdb, capture_ctx, prev_epoch, runtime_stack );
745 282 : *is_epoch_boundary = 1;
746 4236 : } else {
747 4236 : *is_epoch_boundary = 0;
748 4236 : }
749 :
750 4518 : fd_distribute_partitioned_epoch_rewards( banks, bank, accdb, runtime_stack, capture_ctx );
751 4518 : } else {
752 0 : *is_epoch_boundary = 0;
753 0 : }
754 4518 : }
755 :
756 :
757 : static void
758 : fd_runtime_block_sysvar_update_pre_execute( fd_bank_t * bank,
759 : fd_accdb_t * accdb,
760 : fd_runtime_stack_t * runtime_stack,
761 4518 : fd_capture_ctx_t * capture_ctx ) {
762 : // let (fee_rate_governor, fee_components_time_us) = measure_us!(
763 : // FeeRateGovernor::new_derived(&parent.fee_rate_governor, parent.signature_count())
764 : // );
765 : /* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/runtime/src/bank.rs#L1312-L1314 */
766 :
767 4518 : fd_runtime_new_fee_rate_governor_derived( bank, bank->f.parent_signature_cnt );
768 :
769 4518 : if( bank->f.alpenglow_migration_slot!=ULONG_MAX ) {
770 6 : fd_sysvar_clock_update_slot_alpenglow( bank, accdb, capture_ctx );
771 4512 : } else {
772 4512 : fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
773 4512 : ulong parent_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.parent_slot, NULL );
774 4512 : fd_sysvar_clock_update( bank, accdb, capture_ctx, runtime_stack, &parent_epoch );
775 4512 : }
776 :
777 : // It has to go into the current txn previous info but is not in slot 0
778 4518 : if( bank->f.slot != 0 ) {
779 4518 : fd_sysvar_slot_hashes_update( bank, accdb, capture_ctx );
780 4518 : }
781 4518 : fd_sysvar_last_restart_slot_update( bank, accdb, capture_ctx );
782 4518 : }
783 :
784 : int
785 : fd_runtime_load_txn_address_lookup_tables( fd_txn_t const * txn,
786 : uchar const * payload,
787 : fd_accdb_t * accdb,
788 : fd_accdb_fork_id_t fork_id,
789 : ulong slot,
790 : fd_slot_hashes_t const * hashes,
791 210 : fd_acct_addr_t * out_accts_alt ) {
792 :
793 210 : if( FD_LIKELY( txn->transaction_version!=FD_TXN_V0 ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
794 :
795 207 : fd_alut_interp_t interp[1];
796 207 : fd_alut_interp_new( interp, out_accts_alt, txn, payload, hashes, slot );
797 :
798 207 : fd_txn_acct_addr_lut_t const * addr_luts = fd_txn_get_address_tables_const( txn );
799 297 : for( ulong i=0UL; i<txn->addr_table_lookup_cnt; i++ ) {
800 129 : fd_txn_acct_addr_lut_t const * addr_lut = &addr_luts[i];
801 129 : fd_pubkey_t addr_lut_acc = FD_LOAD( fd_pubkey_t, payload+addr_lut->addr_off );
802 :
803 129 : fd_acc_t acc = fd_accdb_read_one( accdb, fork_id, addr_lut_acc.uc );
804 129 : if( FD_UNLIKELY( !acc.lamports ) ) {
805 3 : fd_accdb_unread_one( accdb, &acc );
806 3 : return FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND;
807 3 : }
808 126 : int err = fd_alut_interp_next( interp, &addr_lut_acc, acc.owner, acc.data, acc.data_len );
809 126 : fd_accdb_unread_one( accdb, &acc );
810 126 : if( FD_UNLIKELY( err ) ) return err;
811 126 : }
812 :
813 168 : return FD_RUNTIME_EXECUTE_SUCCESS;
814 207 : }
815 :
816 : /* Pre-populate the bank's in-memory feature set with upcoming feature
817 : activations. If the current slot is the last slot before an epoch
818 : boundary, scan all known feature accounts. Otherwise, returns early.
819 :
820 : For any feature that is pending (not yet activated on-chain) but has
821 : an account owned by the feature program, set the in-memory activation
822 : slot within the bank's featureset to the first slot of the next
823 : epoch. This is needed so that deployment verification (which uses
824 : slot+1) can detect features that will activate at the next epoch
825 : boundary.
826 :
827 : In Agave, program deployments use the feature set from the next
828 : slot via DELAY_VISIBILITY_SLOT_OFFSET. The runtime environments
829 : for deployment are selected based on epoch_of(slot+1):
830 : https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/bank.rs#L3280-L3295
831 : https://github.com/anza-xyz/agave/blob/v3.1.8/svm/src/transaction_processor.rs#L339-L345
832 :
833 : This function does NOT write to feature accounts or update the
834 : lthash. It only modifies the bank's in-memory feature set. */
835 : static void
836 : fd_features_prepopulate_upcoming( fd_bank_t * bank,
837 4518 : fd_accdb_t * accdb ) {
838 4518 : ulong slot = bank->f.slot;
839 4518 : fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
840 4518 : ulong curr_epoch = fd_slot_to_epoch( epoch_schedule, slot, NULL );
841 4518 : ulong next_epoch = fd_slot_to_epoch( epoch_schedule, slot+1UL, NULL );
842 4518 : if( FD_LIKELY( curr_epoch==next_epoch ) ) return;
843 :
844 69 : fd_features_restore( &bank->f.features, accdb, bank->accdb_fork_id, slot, epoch_schedule );
845 69 : }
846 :
847 : void
848 : fd_runtime_block_execute_prepare( fd_banks_t * banks,
849 : fd_bank_t * bank,
850 : fd_accdb_t * accdb,
851 : fd_runtime_stack_t * runtime_stack,
852 : fd_capture_ctx_t * capture_ctx,
853 4518 : int * is_epoch_boundary ) {
854 : /* Cache the Alpenglow migration slot inside the bank so that we can
855 : read it inside transaction execution. */
856 4518 : bank->f.alpenglow_migration_slot = fd_alpenglow_migration_slot( bank, accdb );
857 :
858 4518 : if( FD_LIKELY( bank->f.slot ) ) {
859 4518 : fd_bank_t * parent = fd_banks_bank_query( banks, bank->parent_idx );
860 4518 : FD_TEST( parent );
861 4518 : ulong child_epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, bank->f.slot, NULL );
862 4518 : if( FD_UNLIKELY( child_epoch!=fd_vote_stakes_fork_epoch( bank->vote_stakes_fork_id ) ) ) {
863 429 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
864 429 : fd_vote_stakes_purge_fork( vote_stakes, bank->vote_stakes_fork_id );
865 429 : bank->vote_stakes_fork_id = fd_vote_stakes_new_fork( vote_stakes, parent->vote_stakes_fork_id, child_epoch );
866 429 : }
867 4518 : }
868 :
869 4518 : fd_runtime_block_pre_execute_process_new_epoch( banks, bank, accdb, capture_ctx, runtime_stack, is_epoch_boundary );
870 :
871 4518 : if( FD_LIKELY( bank->f.slot ) ) {
872 4518 : fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_modify( bank );
873 4518 : FD_TEST( cost_tracker );
874 4518 : fd_cost_tracker_init( cost_tracker, &bank->f.features, &bank->f.slot_params, bank->f.slot );
875 4518 : }
876 :
877 4518 : fd_features_prepopulate_upcoming( bank, accdb );
878 4518 : fd_runtime_block_sysvar_update_pre_execute( bank, accdb, runtime_stack, capture_ctx );
879 4518 : FD_TEST( fd_sysvar_cache_restore( bank, accdb ) );
880 4518 : }
881 :
882 : static void
883 : fd_runtime_update_bank_hash( fd_bank_t * bank,
884 357 : fd_capture_ctx_t * capture_ctx ) {
885 : /* Compute the new bank hash */
886 357 : fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
887 357 : fd_hash_t new_bank_hash[1] = { 0 };
888 357 : fd_hashes_hash_bank(
889 357 : lthash,
890 357 : &bank->f.prev_bank_hash,
891 357 : (fd_hash_t *)bank->f.poh.hash,
892 357 : bank->f.signature_count,
893 357 : new_bank_hash );
894 :
895 357 : fd_hashes_apply_hard_forks(
896 357 : new_bank_hash,
897 357 : bank->f.slot,
898 357 : bank->f.parent_slot,
899 357 : bank->f.hard_forks,
900 357 : bank->f.hard_fork_cnt );
901 :
902 : /* Update the bank hash */
903 357 : bank->f.bank_hash = *new_bank_hash;
904 :
905 357 : if( capture_ctx && capture_ctx->capture_solcap &&
906 357 : bank->f.slot>=capture_ctx->solcap_start_slot ) {
907 :
908 0 : uchar lthash_hash[FD_HASH_FOOTPRINT];
909 0 : fd_blake3_hash(lthash->bytes, FD_LTHASH_LEN_BYTES, lthash_hash );
910 0 : fd_capture_link_write_bank_preimage(
911 0 : capture_ctx,
912 0 : bank->f.slot,
913 0 : (fd_hash_t *)new_bank_hash->hash,
914 0 : (fd_hash_t *)&bank->f.prev_bank_hash,
915 0 : (fd_hash_t *)lthash_hash,
916 0 : (fd_hash_t *)bank->f.poh.hash,
917 0 : bank->f.signature_count );
918 0 : }
919 :
920 357 : fd_bank_lthash_end_locking_query( bank );
921 357 : }
922 :
923 : /******************************************************************************/
924 : /* Transaction Level Execution Management */
925 : /******************************************************************************/
926 :
927 : /* fd_runtime_pre_execute_check is responsible for conducting many of
928 : the transaction sanitization checks. This is a combination of some
929 : of the work done in Agave's load_and_execute_transactions(), and some
930 : of the work done in Agave's transaction ingestion stage, before the
931 : transaction even hits the scheduler. We do some of the checks also
932 : in our transaction ingestion stage. For example, the duplicate
933 : account check is performed in both the leader and the replay
934 : scheduler. As a result, the duplicate account check below is
935 : essentially redundant, except that our fuzzing harness expects a
936 : single entry point to cover all of these checks. So we keep all of
937 : the checks below for fuzzing purposes. We could in theory hoist some
938 : of the pre-scheduler checks into a public function that is only
939 : invoked by the fuzzer to avoid duplication in the leader and the
940 : replay pipeline. But all the duplicate checks are pretty cheap, and
941 : the order and placement of the checks are also in motion on Agave's
942 : side, and performing all the checks faithfully would require access
943 : to the bank in the scheduler which is kind of gross. So that's all
944 : probably more hassle than worth. */
945 :
946 : static inline int
947 : fd_runtime_pre_execute_check( fd_runtime_t * runtime,
948 : fd_bank_t * bank,
949 : fd_txn_in_t const * txn_in,
950 405 : fd_txn_out_t * txn_out ) {
951 :
952 : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/sdk/src/transaction/sanitized.rs#L263-L275
953 : TODO: Agave's precompile verification is done at the slot level, before batching and executing transactions. This logic should probably
954 : be moved in the future. The Agave call hierarchy looks something like this:
955 : process_single_slot
956 : v
957 : confirm_full_slot
958 : v
959 : confirm_slot_entries --------------------------------------------------->
960 : v v v
961 : verify_transaction ComputeBudget::process_instruction process_entries
962 : v v
963 : verify_precompiles process_batches
964 : v
965 : ...
966 : v
967 : load_and_execute_transactions
968 : v
969 : ...
970 : v
971 : load_accounts --> load_transaction_accounts
972 : v
973 : general transaction execution
974 :
975 : */
976 :
977 : /* Verify the transaction. For now, this step only involves processing
978 : the compute budget instructions. */
979 405 : int err = fd_executor_verify_transaction( bank, txn_in, txn_out );
980 405 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
981 3 : txn_out->err.is_committable = 0;
982 3 : return err;
983 3 : }
984 :
985 : /* Set up the transaction accounts and other txn ctx metadata. This
986 : also resolves ALUT-referenced account keys and validates account
987 : locks before accounts are acquired. Bundle transactions bind to
988 : the pool acquired and validated once for the whole bundle by
989 : fd_runtime_prepare_bundle_accounts() and never acquire on a
990 : per-transaction basis. However, the implicit programdata state
991 : must be re-derived here per-transaction, and not once up front. An
992 : earlier bundle transaction can deploy a program, which changes
993 : whether a pool account parses as a program account, and therefore
994 : which PD account is being implicitly accounted for. */
995 402 : if( FD_UNLIKELY( txn_in->bundle.is_bundle ) ) {
996 105 : fd_executor_setup_accounts_for_txn_bundle( runtime, txn_in, txn_out );
997 105 : err = fd_runtime_setup_bundle_executables( runtime, bank, txn_out );
998 297 : } else {
999 297 : err = fd_executor_setup_accounts_for_txn( runtime, bank, txn_in, txn_out );
1000 297 : }
1001 402 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
1002 0 : txn_out->err.is_committable = 0;
1003 0 : return err;
1004 0 : }
1005 :
1006 402 : txn_out->details.check_start_ticks = fd_tickcount();
1007 :
1008 : /* load_and_execute_transactions() -> check_transactions()
1009 : https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3667-L3672 */
1010 402 : err = fd_executor_check_transactions( runtime, bank, txn_in, txn_out );
1011 402 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
1012 3 : txn_out->err.is_committable = 0;
1013 3 : return err;
1014 3 : }
1015 :
1016 : /* load_and_execute_sanitized_transactions() -> validate_fees() ->
1017 : validate_transaction_fee_payer()
1018 : https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L236-L249 */
1019 399 : err = fd_executor_validate_transaction_fee_payer( bank, txn_in, txn_out );
1020 399 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
1021 : /* As of the relax_fee_payer_constraint feature, a transaction with
1022 : an invalid fee payer can be included in the block. The
1023 : transaction has no effect on account state and charges no fees.
1024 : These are called "no-op" transactions. They consume block space
1025 : (charging the requested CUs) and their loaded accounts data size
1026 : is charged at their loaded accounts data size limit.
1027 :
1028 : Firedancer does not produce blocks containing no-op transactions
1029 : but we need to be able to replay blocks containing these.
1030 :
1031 : The exception is durable nonce transactions - durable nonce
1032 : transactions with invalid fee payers are not allowed in a block,
1033 : and if a block contains any such transactions it is invalid.
1034 :
1035 : https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/svm/src/transaction_processor.rs#L741-L770 */
1036 0 : if( FD_FEATURE_ACTIVE_BANK( bank, relax_fee_payer_constraint ) &&
1037 0 : txn_out->accounts.nonce_idx_in_txn==ULONG_MAX ) {
1038 0 : txn_out->err.is_committable = 1;
1039 0 : txn_out->err.is_noop = 1;
1040 0 : txn_out->err.is_fees_only = 0;
1041 0 : } else {
1042 0 : txn_out->err.is_committable = 0;
1043 0 : txn_out->err.is_noop = 0;
1044 0 : }
1045 0 : return err;
1046 0 : }
1047 :
1048 : /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L284-L296 */
1049 399 : err = fd_executor_load_transaction_accounts( bank, txn_in, txn_out );
1050 399 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
1051 : /* Regardless of whether transaction accounts were loaded successfully, the transaction is
1052 : included in the block and transaction fees are collected.
1053 : https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/transaction_processor.rs#L341-L357 */
1054 21 : txn_out->err.is_fees_only = 1;
1055 :
1056 : /* If the transaction fails to load, the "rollback" accounts will include one of the following:
1057 : 1. Nonce account only
1058 : 2. Fee payer only
1059 : 3. Nonce account + fee payer
1060 :
1061 : Because the cost tracker uses the loaded account data size in block cost calculations, we need to
1062 : make sure our calculated loaded accounts data size is conformant with Agave's.
1063 : https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/svm/src/account_loader.rs#L437-L449
1064 :
1065 : These are different depending on if define_ltds_fee_only_semantics is enabled or not.
1066 :
1067 : If define_ltds_fee_only_semantics is enabled, we use the accumulated load size so far,
1068 : clamped to the compute budget requested loaded_accounts_data_size_limit. */
1069 21 : if( FD_FEATURE_ACTIVE_BANK( bank, define_ltds_fee_only_semantics ) ) {
1070 : /* https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/svm/src/account_loader.rs#L495-L501 */
1071 9 : txn_out->details.loaded_accounts_data_size = fd_ulong_min(
1072 9 : txn_out->details.loaded_accounts_data_size,
1073 9 : txn_out->details.compute_budget.loaded_accounts_data_size_limit );
1074 12 : } else {
1075 : /* If define_ltds_fee_only_semantics is not enabled, initialize
1076 : loaded_accounts_data_size with the dlen of the fee payer. */
1077 12 : txn_out->details.loaded_accounts_data_size = txn_out->accounts.account[ FD_FEE_PAYER_TXN_IDX ]->data_len;
1078 :
1079 : /* Special case handling for if a nonce account is present in the transaction. */
1080 12 : if( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) {
1081 : /* If the nonce account is not the fee payer, then we separately add the dlen of the nonce account. Otherwise, we would
1082 : be double counting the dlen of the fee payer. */
1083 6 : if( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) {
1084 3 : txn_out->details.loaded_accounts_data_size += txn_out->accounts.account[ txn_out->accounts.nonce_idx_in_txn ]->data_len;
1085 3 : }
1086 6 : }
1087 12 : }
1088 21 : }
1089 :
1090 : /*
1091 : The fee payer and the nonce account will be stored and hashed so
1092 : long as the transaction landed on chain, or, in Agave terminology,
1093 : the transaction was processed.
1094 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/account_saver.rs#L72
1095 :
1096 : A transaction lands on chain in one of two ways:
1097 : (1) Passed fee validation and loaded accounts.
1098 : (2) Passed fee validation and failed to load accounts and the enable_transaction_loading_failure_fees feature is enabled as per
1099 : SIMD-0082 https://github.com/anza-xyz/feature-gate-tracker/issues/52
1100 :
1101 : So, at this point, the transaction is committable.
1102 : */
1103 :
1104 399 : return err;
1105 399 : }
1106 :
1107 : /* fd_runtime_lthash_account updates the running lthash of the bank
1108 : given an account that might have been updated. */
1109 :
1110 : static void
1111 : fd_runtime_lthash_account( fd_bank_t * bank,
1112 : fd_pubkey_t const * pubkey,
1113 : fd_acc_t * acc,
1114 399 : fd_capture_ctx_t * capture_ctx ) {
1115 399 : if( FD_UNLIKELY( !acc->lamports ) ) {
1116 24 : acc->data_len = 0UL;
1117 24 : acc->executable = 0;
1118 24 : memset( acc->owner, 0, sizeof(acc->owner) );
1119 24 : }
1120 :
1121 399 : fd_lthash_value_t lthash_prev[1];
1122 399 : if( FD_LIKELY( acc->prior_data ) ) {
1123 384 : fd_hashes_account_lthash_simple( pubkey->uc, acc->prior_owner, acc->prior_lamports, acc->prior_executable, acc->prior_data, acc->prior_data_len, lthash_prev );
1124 384 : } else {
1125 15 : fd_lthash_zero( lthash_prev );
1126 15 : }
1127 :
1128 399 : fd_lthash_value_t lthash_post[1];
1129 399 : if( FD_LIKELY( acc->prior_lamports || acc->lamports ) ) {
1130 399 : fd_hashes_update_simple( lthash_post, lthash_prev, pubkey->uc, acc->owner, acc->lamports, acc->executable, acc->data, acc->data_len, bank, capture_ctx );
1131 399 : }
1132 399 : }
1133 :
1134 : /* fd_runtime_commit_txn is a helper used by the transaction executor to
1135 : finalize account changes back into the database. It also handles
1136 : txncache insertion and updates to the vote/stake cache. TODO: This
1137 : function should probably be moved to fd_executor.c. */
1138 :
1139 : void
1140 : fd_runtime_commit_txn( fd_runtime_t * runtime,
1141 : fd_bank_t * bank,
1142 : fd_txn_in_t const * txn_in,
1143 252 : fd_txn_out_t * txn_out ) {
1144 252 : FD_TEST( txn_out->err.is_committable );
1145 :
1146 252 : txn_out->details.commit_start_ticks = fd_tickcount();
1147 :
1148 252 : if( FD_UNLIKELY( !txn_out->err.txn_err ) ) {
1149 165 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
1150 1137 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1151 : /* We are only interested in saving writable accounts and the fee
1152 : payer account. */
1153 972 : if( FD_UNLIKELY( !txn_out->accounts.is_writable[ i ] ) ) continue;
1154 :
1155 369 : fd_pubkey_t const * pubkey = &txn_out->accounts.keys[ i ];
1156 :
1157 : /* Only the txn that owns the accdb reference commits the account
1158 : state. In a bundle, an account is owned by its last writable
1159 : user (account_acquired==1); every other txn referencing it has
1160 : account_acquired==0 and skips here, so the final shared state is
1161 : lthashed exactly once and not double-counted. stake_update/
1162 : vote_update are recomputed from the final state and were moved
1163 : onto this owner, so they also fire here exactly once. */
1164 369 : if( FD_UNLIKELY( !txn_out->accounts.account_acquired[ i ] ) ) continue;
1165 :
1166 312 : fd_acc_t * account = txn_out->accounts.account[ i ];
1167 312 : account->commit = 1;
1168 :
1169 312 : if( FD_UNLIKELY( txn_out->accounts.stake_update[ i ] ) ) {
1170 6 : fd_stakes_update_stake_delegation( pubkey, account, bank, txn_in );
1171 6 : }
1172 :
1173 312 : if( txn_out->accounts.vote_update[i] ) {
1174 60 : fd_vote_block_timestamp_t last_vote;
1175 60 : if( FD_UNLIKELY( !account->lamports ||
1176 60 : !fd_vsv_is_correct_size_owner_and_init( account->owner, account->data, account->data_len ) ||
1177 60 : fd_vote_account_last_timestamp( account->data, account->data_len, &last_vote ) ) ) {
1178 0 : fd_vote_stakes_update_state( vote_stakes, bank->vote_stakes_fork_id, pubkey, 0UL, 0L, 0 );
1179 60 : } else {
1180 60 : fd_vote_stakes_update_state( vote_stakes, bank->vote_stakes_fork_id, pubkey, last_vote.slot, last_vote.timestamp, 1 );
1181 60 : }
1182 60 : }
1183 :
1184 312 : fd_runtime_lthash_account( bank, pubkey, account, runtime->log.capture_ctx );
1185 312 : }
1186 :
1187 : /* Atomically add all accumulated tips to the bank once after
1188 : processing all accounts. */
1189 165 : if( FD_UNLIKELY( txn_out->details.tips ) ) FD_ATOMIC_FETCH_AND_ADD( &bank->f.tips, txn_out->details.tips );
1190 165 : }
1191 :
1192 252 : txn_out->details.commit_index_in_slot = FD_ATOMIC_FETCH_AND_ADD( &bank->f.txn_count, 1UL );
1193 252 : FD_ATOMIC_FETCH_AND_ADD( &bank->f.execution_fees, txn_out->details.execution_fee );
1194 252 : FD_ATOMIC_FETCH_AND_ADD( &bank->f.priority_fees, txn_out->details.priority_fee );
1195 252 : FD_ATOMIC_FETCH_AND_ADD( &bank->f.signature_count, txn_out->details.signature_count );
1196 :
1197 252 : if( FD_LIKELY( !txn_out->details.is_simple_vote ) ) {
1198 177 : FD_ATOMIC_FETCH_AND_ADD( &bank->f.nonvote_txn_count, 1 );
1199 177 : if( FD_UNLIKELY( txn_out->err.exec_err ) ) FD_ATOMIC_FETCH_AND_ADD( &bank->f.nonvote_failed_txn_count, 1 );
1200 177 : }
1201 :
1202 252 : if( FD_UNLIKELY( txn_out->err.exec_err ) ) FD_ATOMIC_FETCH_AND_ADD( &bank->f.failed_txn_count, 1 );
1203 252 : FD_ATOMIC_FETCH_AND_ADD( &bank->f.total_compute_units_used, txn_out->details.compute_budget.compute_unit_limit-txn_out->details.compute_budget.compute_meter );
1204 :
1205 252 : fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_modify( bank );
1206 252 : int res = fd_cost_tracker_try_add_cost( cost_tracker, txn_out );
1207 252 : if( FD_UNLIKELY( res!=FD_COST_TRACKER_SUCCESS ) ) {
1208 0 : FD_LOG_DEBUG(( "fd_runtime_commit_txn: transaction failed to fit into block %d", res ));
1209 0 : txn_out->err.is_committable = 0;
1210 0 : txn_out->err.txn_err = fd_cost_tracker_err_to_runtime_err( res );
1211 0 : }
1212 :
1213 252 : if( FD_LIKELY( runtime->status_cache && txn_out->accounts.nonce_idx_in_txn==ULONG_MAX && txn_out->err.is_committable ) ) {
1214 : /* In Agave, durable nonce transactions are inserted to the status
1215 : cache the same as any others, but this is only to serve RPC
1216 : requests, they do not need to be in there for correctness as the
1217 : nonce mechanism itself prevents double spend. We skip this logic
1218 : entirely to simplify and improve performance of the txn cache.
1219 :
1220 : Cost tracker rejected transactions are also skipped: the block
1221 : is already dead, and skipping keeps the per slot insert count
1222 : bounded by the cost model, which sizes the txn cache. */
1223 246 : fd_txncache_insert( runtime->status_cache, bank->txncache_fork_id, txn_out->details.blockhash.uc, txn_out->details.blake_txn_msg_hash.uc );
1224 246 : }
1225 :
1226 : /* No-op transactions have no effect on the state so we should skip
1227 : these rollbacks. */
1228 252 : if( FD_UNLIKELY( txn_out->err.txn_err && !txn_out->err.is_noop ) ) {
1229 : /* With nonce account rollbacks, there are three cases:
1230 :
1231 : 1. No nonce account in the transaction
1232 : 2. Nonce account is the fee payer
1233 : 3. Nonce account is not the fee payer
1234 :
1235 : We should always rollback the nonce account first. Note that the
1236 : nonce account may be the fee payer (case 2). */
1237 87 : if( FD_UNLIKELY( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) ) {
1238 0 : fd_acc_t * nonce_account = txn_out->accounts.account[ txn_out->accounts.nonce_idx_in_txn ];
1239 0 : fd_memcpy( nonce_account->data, txn_out->accounts.nonce_rollback_data, txn_out->accounts.nonce_rollback_data_len );
1240 0 : nonce_account->data_len = txn_out->accounts.nonce_rollback_data_len;
1241 0 : fd_memcpy( nonce_account->owner, nonce_account->prior_owner, 32UL );
1242 0 : if( FD_UNLIKELY( txn_out->accounts.nonce_idx_in_txn==FD_FEE_PAYER_TXN_IDX ) ) {
1243 0 : nonce_account->lamports = txn_out->accounts.fee_payer_rollback_lamports;
1244 0 : } else {
1245 0 : nonce_account->lamports = nonce_account->prior_lamports;
1246 0 : }
1247 0 : nonce_account->executable = nonce_account->prior_executable;
1248 0 : nonce_account->commit = 1;
1249 0 : fd_runtime_lthash_account( bank, &txn_out->accounts.keys[ txn_out->accounts.nonce_idx_in_txn ], nonce_account, runtime->log.capture_ctx );
1250 0 : }
1251 :
1252 : /* Now, we must only save the fee payer if the nonce account was not
1253 : the fee payer (because that was already saved above). */
1254 87 : if( FD_LIKELY( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
1255 87 : fd_acc_t * fee_payer_account = txn_out->accounts.account[ FD_FEE_PAYER_TXN_IDX ];
1256 87 : fd_memcpy( fee_payer_account->data, fee_payer_account->prior_data, fee_payer_account->prior_data_len );
1257 87 : fee_payer_account->data_len = fee_payer_account->prior_data_len;
1258 87 : fd_memcpy( fee_payer_account->owner, fee_payer_account->prior_owner, 32UL );
1259 87 : fee_payer_account->lamports = txn_out->accounts.fee_payer_rollback_lamports;
1260 87 : fee_payer_account->executable = fee_payer_account->prior_executable;
1261 :
1262 87 : fee_payer_account->commit = 1;
1263 87 : fd_runtime_lthash_account( bank, &txn_out->accounts.keys[ FD_FEE_PAYER_TXN_IDX ], fee_payer_account, runtime->log.capture_ctx );
1264 87 : }
1265 87 : }
1266 :
1267 252 : if( FD_UNLIKELY( fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_txn_emit( txn_in, txn_out, bank );
1268 :
1269 252 : if( FD_LIKELY( !txn_out->accounts.is_bundle ) ) {
1270 171 : fd_accdb_release_ab( runtime->accdb,
1271 171 : txn_out->accounts.cnt, runtime->accounts.account,
1272 171 : runtime->accounts.executable_cnt, runtime->accounts.executable );
1273 171 : runtime->accounts.executable_cnt = 0UL;
1274 171 : }
1275 252 : }
1276 :
1277 : void
1278 : fd_runtime_cancel_txn( fd_runtime_t * runtime,
1279 : fd_bank_t * bank,
1280 : fd_txn_in_t const * txn_in,
1281 9 : fd_txn_out_t * txn_out ) {
1282 9 : FD_TEST( !txn_out->err.is_committable );
1283 :
1284 9 : if( FD_UNLIKELY( bank && fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_txn_emit( txn_in, txn_out, bank );
1285 :
1286 9 : if( FD_UNLIKELY( !txn_out->accounts.is_setup ) ) return;
1287 :
1288 9 : fd_accdb_release_ab( runtime->accdb,
1289 9 : txn_out->accounts.cnt, runtime->accounts.account,
1290 9 : runtime->accounts.executable_cnt, runtime->accounts.executable );
1291 9 : runtime->accounts.executable_cnt = 0UL;
1292 9 : }
1293 :
1294 : void
1295 51 : fd_runtime_fini_bundle( fd_runtime_t * runtime ) {
1296 51 : fd_accdb_release_ab( runtime->accdb,
1297 51 : runtime->accounts.account_cnt, runtime->accounts.account,
1298 51 : runtime->accounts.executable_cnt, runtime->accounts.executable );
1299 51 : runtime->accounts.account_cnt = 0UL;
1300 51 : runtime->accounts.executable_cnt = 0UL;
1301 51 : }
1302 :
1303 : static inline void
1304 405 : fd_runtime_reset_runtime( fd_runtime_t * runtime ) {
1305 405 : runtime->instr.stack_sz = 0;
1306 405 : runtime->instr.trace_length = 0UL;
1307 405 : }
1308 :
1309 : static inline void
1310 : fd_runtime_new_txn_out( fd_txn_in_t const * txn_in,
1311 405 : fd_txn_out_t * txn_out ) {
1312 405 : txn_out->details.load_start_ticks = fd_tickcount();
1313 405 : txn_out->details.check_start_ticks = LONG_MAX;
1314 405 : txn_out->details.exec_start_ticks = LONG_MAX;
1315 405 : txn_out->details.commit_start_ticks = LONG_MAX;
1316 :
1317 405 : fd_compute_budget_details_new( &txn_out->details.compute_budget );
1318 405 : fd_memset( &txn_out->details.txn_cost, 0, sizeof(txn_out->details.txn_cost) );
1319 :
1320 405 : txn_out->details.loaded_accounts_data_size = 0UL;
1321 405 : txn_out->details.accounts_resize_delta = 0L;
1322 :
1323 405 : txn_out->details.return_data.len = 0UL;
1324 405 : memset( txn_out->details.return_data.program_id.key, 0, sizeof(fd_pubkey_t) );
1325 :
1326 405 : txn_out->details.tips = 0UL;
1327 405 : txn_out->details.execution_fee = 0UL;
1328 405 : txn_out->details.priority_fee = 0UL;
1329 405 : txn_out->details.signature_count = TXN( txn_in->txn )->signature_cnt;
1330 405 : if( FD_LIKELY( txn_out->details.signature_count ) ) {
1331 402 : fd_memcpy( txn_out->details.signature.uc,
1332 402 : (uchar const *)txn_in->txn->payload + TXN( txn_in->txn )->signature_off,
1333 402 : sizeof(fd_signature_t) );
1334 402 : } else {
1335 3 : fd_memset( txn_out->details.signature.uc, 0, sizeof(fd_signature_t) );
1336 3 : }
1337 405 : txn_out->details.is_simple_vote = fd_txn_is_simple_vote_transaction( TXN( txn_in->txn ), txn_in->txn->payload );
1338 :
1339 405 : fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->recent_blockhash_off);
1340 405 : memcpy( txn_out->details.blockhash.uc, blockhash->hash, sizeof(fd_hash_t) );
1341 :
1342 405 : txn_out->accounts.is_setup = 0;
1343 405 : txn_out->accounts.is_bundle = txn_in->bundle.is_bundle;
1344 405 : if( FD_LIKELY( !txn_in->bundle.is_bundle ) ) txn_out->accounts.cnt= 0UL;
1345 :
1346 405 : FD_STATIC_ASSERT( offsetof(fd_txn_out_t, accounts.rm_vote)-offsetof(fd_txn_out_t, accounts.stake_update)==3UL*MAX_TX_ACCOUNT_LOCKS, txn_out_flags_contiguous );
1347 405 : memset( txn_out->accounts.is_writable, 0, sizeof(txn_out->accounts.is_writable) );
1348 405 : memset( txn_out->accounts.stake_update, 0, 4UL*MAX_TX_ACCOUNT_LOCKS );
1349 405 : txn_out->accounts.nonce_idx_in_txn = ULONG_MAX;
1350 :
1351 : /* For bundle transactions the resolved key list is bound once up
1352 : front by fd_runtime_prepare_bundle_accounts() before this runs per
1353 : transaction, so preserve it here. For a non-bundle transaction the
1354 : executable list is built in fd_executor_setup_accounts_for_txn(),
1355 : so reset it here. */
1356 405 : if( FD_LIKELY( !txn_in->bundle.is_bundle ) ) {
1357 300 : txn_out->accounts.executable_cnt = 0UL;
1358 300 : txn_out->accounts.executable_skipped_cnt = 0;
1359 300 : }
1360 405 : txn_out->accounts.nonce_rollback_data_len = 0UL;
1361 405 : txn_out->accounts.fee_payer_rollback_lamports = 0UL;
1362 :
1363 405 : txn_out->err.is_committable = 1;
1364 405 : txn_out->err.is_fees_only = 0;
1365 405 : txn_out->err.is_noop = 0;
1366 405 : txn_out->err.txn_err = FD_RUNTIME_EXECUTE_SUCCESS;
1367 405 : txn_out->err.exec_err = FD_EXECUTOR_INSTR_SUCCESS;
1368 405 : txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_NONE;
1369 405 : txn_out->err.exec_err_idx = UINT_MAX;
1370 405 : txn_out->err.custom_err = 0;
1371 405 : }
1372 :
1373 : void
1374 : fd_runtime_prepare_and_execute_txn( fd_runtime_t * runtime,
1375 : fd_bank_t * bank,
1376 : fd_txn_in_t const * txn_in,
1377 405 : fd_txn_out_t * txn_out ) {
1378 405 : fd_runtime_reset_runtime( runtime );
1379 :
1380 405 : fd_runtime_new_txn_out( txn_in, txn_out );
1381 :
1382 405 : uchar dump_txn = !!(runtime->log.dump_proto_ctx &&
1383 405 : bank->f.slot >= runtime->log.dump_proto_ctx->dump_proto_start_slot &&
1384 405 : runtime->log.dump_proto_ctx->dump_txn_to_pb);
1385 :
1386 : /* Phase 1: Capture TxnContext before execution. */
1387 405 : if( FD_UNLIKELY( dump_txn ) ) {
1388 0 : if( runtime->log.txn_dump_ctx ) {
1389 0 : fd_dump_txn_context_to_protobuf( runtime->log.txn_dump_ctx, runtime, bank, txn_in, txn_out );
1390 0 : } else {
1391 0 : fd_dump_txn_to_protobuf( runtime, bank, txn_in, txn_out );
1392 0 : }
1393 0 : }
1394 :
1395 : /* Transaction sanitization. If a transaction can't be committed, is
1396 : fees-only, or a no-op, we return early. */
1397 405 : txn_out->err.txn_err = fd_runtime_pre_execute_check( runtime, bank, txn_in, txn_out );
1398 405 : ulong cu_before = txn_out->details.compute_budget.compute_meter;
1399 :
1400 : /* Execute the transaction if eligible to do so. */
1401 405 : if( FD_LIKELY( txn_out->err.is_committable ) ) {
1402 399 : if( FD_UNLIKELY( txn_out->err.is_noop ) ) {
1403 : /* No-op transactions charge the requested CUs and loaded
1404 : account data size limit.
1405 : https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/svm/src/transaction_processor.rs#L757-L767
1406 : https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/svm/src/transaction_processing_result.rs#L92-L106 */
1407 0 : txn_out->details.compute_budget.compute_meter = 0UL;
1408 0 : txn_out->details.loaded_accounts_data_size = txn_out->details.compute_budget.loaded_accounts_data_size_limit;
1409 399 : } else if( FD_LIKELY( !txn_out->err.is_fees_only ) ) {
1410 378 : txn_out->details.exec_start_ticks = fd_tickcount();
1411 378 : txn_out->err.txn_err = fd_execute_txn( runtime, bank, txn_in, txn_out );
1412 378 : }
1413 399 : fd_cost_tracker_calculate_cost( bank, txn_in, txn_out );
1414 399 : }
1415 405 : ulong cu_after = txn_out->details.compute_budget.compute_meter;
1416 405 : runtime->metrics.cu_cum += fd_ulong_sat_sub( cu_before, cu_after );
1417 :
1418 : /* Phase 2: Capture TxnResult after execution and write to disk. */
1419 405 : if( FD_UNLIKELY( dump_txn && runtime->log.txn_dump_ctx ) ) {
1420 0 : fd_dump_txn_result_to_protobuf( runtime->log.txn_dump_ctx, txn_in, txn_out, txn_out->err.txn_err );
1421 0 : fd_dump_txn_fixture_to_file( runtime->log.txn_dump_ctx, runtime->log.dump_proto_ctx, txn_in );
1422 0 : }
1423 405 : }
1424 :
1425 : /* fd_executor_txn_verify and fd_runtime_pre_execute_check are responsible
1426 : for the bulk of the pre-transaction execution checks in the runtime.
1427 : They aim to preserve the ordering present in the Agave client to match
1428 : parity in terms of error codes. Sigverify is kept separate from the rest
1429 : of the transaction checks for fuzzing convenience.
1430 :
1431 : For reference this is the general code path which contains all relevant
1432 : pre-transactions checks in the v2.0.x Agave client from upstream
1433 : to downstream is as follows:
1434 :
1435 : confirm_slot_entries() which calls verify_ticks() and
1436 : verify_transaction(). verify_transaction() calls verify_and_hash_message()
1437 : and verify_precompiles() which parallels fd_executor_txn_verify() and
1438 : fd_executor_verify_transaction().
1439 :
1440 : process_entries() contains a duplicate account check which is part of
1441 : agave account lock acquiring. This is checked inline in
1442 : fd_runtime_pre_execute_check().
1443 :
1444 : load_and_execute_transactions() contains the function check_transactions().
1445 : This contains check_age() and check_status_cache() which is paralleled by
1446 : fd_executor_check_transaction_age_and_compute_budget_limits() and
1447 : fd_executor_check_status_cache() respectively.
1448 :
1449 : load_and_execute_sanitized_transactions() contains validate_fees()
1450 : which is responsible for executing the compute budget instructions,
1451 : validating the fee payer and collecting the fee. This is mirrored in
1452 : firedancer with fd_executor_compute_budget_program_execute_instructions()
1453 : and fd_executor_collect_fees(). load_and_execute_sanitized_transactions()
1454 : also checks the total data size of the accounts in load_accounts() and
1455 : validates the program accounts in load_transaction_accounts(). This
1456 : is paralled by fd_executor_load_transaction_accounts(). */
1457 :
1458 :
1459 : /******************************************************************************/
1460 : /* Genesis */
1461 : /*******************************************************************************/
1462 :
1463 : static void
1464 : fd_runtime_genesis_init_program( fd_bank_t * bank,
1465 : fd_accdb_t * accdb,
1466 0 : fd_capture_ctx_t * capture_ctx ) {
1467 :
1468 0 : fd_sysvar_clock_init( bank, accdb, capture_ctx );
1469 0 : fd_sysvar_rent_init( bank, accdb, capture_ctx );
1470 :
1471 0 : fd_sysvar_slot_history_init( bank, accdb, capture_ctx );
1472 0 : fd_sysvar_epoch_schedule_init( bank, accdb, capture_ctx );
1473 0 : fd_sysvar_recent_hashes_init( bank, accdb, capture_ctx );
1474 0 : fd_sysvar_stake_history_init( bank, accdb, capture_ctx );
1475 0 : fd_sysvar_last_restart_slot_init( bank, accdb, capture_ctx );
1476 :
1477 0 : fd_builtin_programs_init( bank, accdb, capture_ctx );
1478 0 : }
1479 :
1480 : static void
1481 : fd_runtime_init_bank_from_genesis( fd_banks_t * banks,
1482 : fd_bank_t * bank,
1483 : fd_runtime_stack_t * runtime_stack,
1484 : fd_accdb_t * accdb,
1485 : fd_genesis_t const * genesis,
1486 : uchar const * genesis_blob,
1487 0 : fd_hash_t const * genesis_hash ) {
1488 :
1489 0 : bank->f.parent_slot = ULONG_MAX;
1490 0 : bank->f.poh = *genesis_hash;
1491 :
1492 0 : fd_hash_t * bank_hash = &bank->f.bank_hash;
1493 0 : memset( bank_hash->hash, 0, FD_SHA256_HASH_SZ );
1494 :
1495 0 : uint128 target_tick_duration = (uint128)genesis->poh.tick_duration_secs * 1000000000UL + (uint128)genesis->poh.tick_duration_ns;
1496 :
1497 0 : fd_epoch_schedule_t * epoch_schedule = &bank->f.epoch_schedule;
1498 0 : epoch_schedule->leader_schedule_slot_offset = genesis->epoch_schedule.leader_schedule_slot_offset;
1499 0 : epoch_schedule->warmup = genesis->epoch_schedule.warmup;
1500 0 : epoch_schedule->first_normal_epoch = genesis->epoch_schedule.first_normal_epoch;
1501 0 : epoch_schedule->first_normal_slot = genesis->epoch_schedule.first_normal_slot;
1502 0 : epoch_schedule->slots_per_epoch = genesis->epoch_schedule.slots_per_epoch;
1503 :
1504 0 : fd_rent_t * rent = &bank->f.rent;
1505 0 : rent->lamports_per_uint8_year = genesis->rent.lamports_per_uint8_year;
1506 0 : rent->exemption_threshold = genesis->rent.exemption_threshold;
1507 0 : rent->burn_percent = genesis->rent.burn_percent;
1508 :
1509 0 : fd_inflation_t * inflation = &bank->f.inflation;
1510 0 : inflation->initial = genesis->inflation.initial;
1511 0 : inflation->terminal = genesis->inflation.terminal;
1512 0 : inflation->taper = genesis->inflation.taper;
1513 0 : inflation->foundation = genesis->inflation.foundation;
1514 0 : inflation->foundation_term = genesis->inflation.foundation_term;
1515 0 : inflation->unused = 0.0;
1516 :
1517 0 : bank->f.block_height = 0UL;
1518 :
1519 0 : {
1520 : /* FIXME Why is there a previous blockhash at genesis? Why is the
1521 : last_hash field an option type in Agave, if even the first
1522 : real block has a previous blockhash? */
1523 0 : fd_blockhashes_t * bhq = fd_blockhashes_init( &bank->f.block_hash_queue, 0UL );
1524 0 : fd_blockhash_info_t * info = fd_blockhashes_push_new( bhq, genesis_hash );
1525 0 : info->lamports_per_signature = 0UL;
1526 0 : }
1527 :
1528 0 : fd_fee_rate_governor_t * fee_rate_governor = &bank->f.fee_rate_governor;
1529 0 : fee_rate_governor->target_lamports_per_signature = genesis->fee_rate_governor.target_lamports_per_signature;
1530 0 : fee_rate_governor->target_signatures_per_slot = genesis->fee_rate_governor.target_signatures_per_slot;
1531 0 : fee_rate_governor->min_lamports_per_signature = genesis->fee_rate_governor.min_lamports_per_signature;
1532 0 : fee_rate_governor->max_lamports_per_signature = genesis->fee_rate_governor.max_lamports_per_signature;
1533 0 : fee_rate_governor->burn_percent = genesis->fee_rate_governor.burn_percent;
1534 :
1535 0 : bank->f.max_tick_height = genesis->poh.ticks_per_slot * (bank->f.slot + 1);
1536 0 : bank->f.slot_params = FD_SLOT_PARAMS_400MS;
1537 0 : bank->f.slot_params.ns_per_slot = (ulong)( target_tick_duration * genesis->poh.ticks_per_slot );
1538 0 : bank->f.slot_params.ns_per_slot_adjusted = fd_ulong_sat_sub( bank->f.slot_params.ns_per_slot, FD_TARGET_SLOT_ADJUSTMENT_NS );
1539 0 : bank->f.slot_params.slots_per_year = SECONDS_PER_YEAR * (1000000000.0 / (double)target_tick_duration) / (double)genesis->poh.ticks_per_slot;
1540 0 : bank->f.slot_params.hashes_per_tick = genesis->poh.hashes_per_tick;
1541 0 : bank->f.slot_params_default = bank->f.slot_params;
1542 :
1543 0 : bank->f.ticks_per_slot = genesis->poh.ticks_per_slot;
1544 0 : bank->f.genesis_creation_time = genesis->creation_time;
1545 :
1546 0 : bank->f.signature_count = 0UL;
1547 :
1548 : /* Derive epoch stakes */
1549 :
1550 0 : fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
1551 0 : if( FD_UNLIKELY( !stake_delegations ) ) {
1552 0 : FD_LOG_CRIT(( "Failed to join and new a stake delegations" ));
1553 0 : }
1554 :
1555 0 : ulong capitalization = 0UL;
1556 :
1557 0 : for( ulong i=0UL; i<genesis->account_cnt; i++ ) {
1558 0 : fd_genesis_account_t account[1];
1559 0 : fd_genesis_account( genesis, genesis_blob, account, i );
1560 :
1561 0 : capitalization = fd_ulong_sat_add( capitalization, account->lamports );
1562 :
1563 0 : if( !memcmp( account->owner.uc, fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) {
1564 : /* If an account is a stake account, then it must be added to the
1565 : stake delegations cache. Like Agave, membership is decided by
1566 : the variant alone: a delegation of zero is still a delegation. */
1567 0 : fd_stake_state_t const * stake_state = fd_stake_state_view( account->data, account->data_len );
1568 0 : if( FD_UNLIKELY( !stake_state ) ) { FD_BASE58_ENCODE_32_BYTES( account->pubkey.uc, stake_b58 ); FD_LOG_ERR(( "invalid stake account %s", stake_b58 )); }
1569 0 : if( stake_state->stake_type!=FD_STAKE_STATE_STAKE ) continue;
1570 :
1571 0 : fd_stake_delegations_root_update(
1572 0 : stake_delegations,
1573 0 : &account->pubkey,
1574 0 : &stake_state->stake.stake.delegation.voter_pubkey,
1575 0 : stake_state->stake.stake.delegation.stake,
1576 0 : stake_state->stake.stake.delegation.activation_epoch,
1577 0 : stake_state->stake.stake.delegation.deactivation_epoch,
1578 0 : stake_state->stake.stake.credits_observed,
1579 0 : account->lamports,
1580 0 : (uint)account->data_len,
1581 0 : FD_STAKE_DELEGATIONS_WARMUP_COOLDOWN_RATE_ENUM_025 /* genesis is epoch 0, always 0.25 */ );
1582 0 : }
1583 0 : }
1584 :
1585 0 : fd_features_restore( &bank->f.features, accdb, bank->accdb_fork_id, bank->f.slot, &bank->f.epoch_schedule );
1586 :
1587 : /* https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/runtime/src/bank.rs#L6101-L6109 */
1588 0 : if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( bank, double_disinflation_rate ) ) ) {
1589 0 : fd_apply_double_disinflation_rate( bank );
1590 0 : }
1591 :
1592 : /* fd_refresh_vote_accounts is responsible for updating the vote
1593 : states with the total amount of active delegated stake. It does
1594 : this by iterating over all active stake delegations and summing up
1595 : the amount of stake that is delegated to each vote account. */
1596 0 : ulong new_rate_activation_epoch = 0UL;
1597 :
1598 0 : {
1599 : /* Snapshot the stake history sysvar into a local buffer and release
1600 : the accdb bracket before calling fd_refresh_vote_accounts, which
1601 : performs its own accdb acquires. fd_sysvar_stake_history_view
1602 : aliases the source bytes, so the bracket cannot be held open
1603 : across an inner acquire. */
1604 0 : uchar stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
1605 0 : fd_stake_history_t stake_history_[1];
1606 0 : fd_stake_history_t * stake_history = NULL;
1607 0 : fd_acc_t ro = fd_accdb_read_one( accdb, bank->accdb_fork_id, fd_sysvar_stake_history_id.uc );
1608 0 : if( FD_LIKELY( ro.lamports ) ) {
1609 0 : ulong copy_sz = fd_ulong_min( ro.data_len, FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ );
1610 0 : fd_memcpy( stake_history_data, ro.data, copy_sz );
1611 0 : fd_accdb_unread_one( accdb, &ro );
1612 0 : stake_history = fd_sysvar_stake_history_view( stake_history_, stake_history_data, copy_sz );
1613 0 : } else {
1614 0 : fd_accdb_unread_one( accdb, &ro );
1615 0 : }
1616 :
1617 0 : fd_refresh_vote_accounts( bank, accdb, runtime_stack, stake_delegations, stake_history, ULONG_MAX, &new_rate_activation_epoch );
1618 0 : }
1619 :
1620 : /* At genesis (epoch 0) there is no previous epoch, so the t-2 set
1621 : should equal the genesis staked set. */
1622 :
1623 0 : {
1624 0 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
1625 0 : ulong fork_id = bank->vote_stakes_fork_id;
1626 0 : uchar __attribute__((aligned(FD_VOTE_STAKES_ITER_ALIGN))) iter_mem[ FD_VOTE_STAKES_ITER_FOOTPRINT ];
1627 0 : for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_iter_init( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_1, iter_mem );
1628 0 : !fd_vote_stakes_iter_done( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_1, iter );
1629 0 : fd_vote_stakes_iter_next( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_1, iter ) ) {
1630 0 : fd_pubkey_t pubkey;
1631 0 : fd_pubkey_t node_account;
1632 0 : ulong stake;
1633 0 : ushort commission;
1634 0 : uchar bls_key[ FD_BLS_PUBKEY_COMPRESSED_SZ ];
1635 :
1636 0 : fd_vote_stakes_iter_ele( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_1, iter, &pubkey, &node_account, &stake,
1637 0 : NULL, NULL, &commission, NULL, NULL, bls_key, NULL );
1638 0 : fd_vote_stakes_snap_insert_t_2( vote_stakes, fork_id, &pubkey, &node_account, stake, commission, bls_key );
1639 0 : }
1640 0 : fd_vote_stakes_refresh( vote_stakes, fork_id, accdb, bank->accdb_fork_id );
1641 0 : }
1642 :
1643 0 : bank->f.epoch = 0UL;
1644 0 : bank->f.capitalization = capitalization;
1645 0 : }
1646 :
1647 : static int
1648 : fd_runtime_process_genesis_block( fd_bank_t * bank,
1649 : fd_accdb_t * accdb,
1650 : fd_capture_ctx_t * capture_ctx,
1651 0 : fd_runtime_stack_t * runtime_stack ) {
1652 0 : fd_sha256_hash_32_repeated( bank->f.poh.hash, bank->f.poh.hash, bank->f.slot_params.hashes_per_tick * bank->f.ticks_per_slot );
1653 :
1654 0 : bank->f.execution_fees = 0UL;
1655 0 : bank->f.priority_fees = 0UL;
1656 0 : bank->f.signature_count = 0UL;
1657 0 : bank->f.txn_count = 0UL;
1658 0 : bank->f.failed_txn_count = 0UL;
1659 0 : bank->f.nonvote_failed_txn_count = 0UL;
1660 0 : bank->f.total_compute_units_used = 0UL;
1661 :
1662 0 : fd_runtime_genesis_init_program( bank, accdb, capture_ctx );
1663 0 : fd_sysvar_slot_history_update( bank, accdb, capture_ctx );
1664 0 : fd_runtime_update_leaders( bank, runtime_stack );
1665 0 : fd_runtime_freeze( bank, accdb, capture_ctx );
1666 :
1667 0 : fd_hash_t const * prev_bank_hash = &bank->f.bank_hash;
1668 :
1669 0 : fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
1670 :
1671 0 : fd_hash_t * bank_hash = &bank->f.bank_hash;
1672 0 : fd_hashes_hash_bank( lthash, prev_bank_hash, (fd_hash_t *)bank->f.poh.hash, 0UL, bank_hash );
1673 :
1674 0 : fd_bank_lthash_end_locking_query( bank );
1675 :
1676 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
1677 0 : }
1678 :
1679 : void
1680 : fd_runtime_read_genesis( fd_banks_t * banks,
1681 : fd_bank_t * bank,
1682 : fd_accdb_t * accdb,
1683 : fd_capture_ctx_t * capture_ctx,
1684 : fd_hash_t const * genesis_hash,
1685 : fd_lthash_value_t const * genesis_lthash,
1686 : fd_genesis_t const * genesis,
1687 : uchar const * genesis_blob,
1688 0 : fd_runtime_stack_t * runtime_stack ) {
1689 0 : fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
1690 0 : *lthash = *genesis_lthash;
1691 0 : fd_bank_lthash_end_locking_modify( bank );
1692 :
1693 : /* Once the accounts have been loaded from the genesis config into
1694 : the accounts db, we can initialize the bank state. This involves
1695 : setting some fields, and notably setting up the vote and stake
1696 : caches which are used for leader scheduling/rewards. */
1697 :
1698 0 : fd_runtime_init_bank_from_genesis( banks, bank, runtime_stack, accdb, genesis, genesis_blob, genesis_hash );
1699 :
1700 : /* Write the native programs to the accounts db. */
1701 :
1702 0 : for( ulong i=0UL; i<genesis->builtin_cnt; i++ ) {
1703 0 : fd_genesis_builtin_t builtin[1];
1704 0 : fd_genesis_builtin( genesis, genesis_blob, builtin, i );
1705 0 : fd_write_builtin_account( bank, accdb, capture_ctx, builtin->pubkey, builtin->data, builtin->data_len );
1706 0 : }
1707 :
1708 : /* At this point, state related to the bank and the accounts db
1709 : have been initialized and we are free to finish executing the
1710 : block. In practice, this updates some bank fields (notably the
1711 : poh and bank hash). */
1712 :
1713 0 : int err = fd_runtime_process_genesis_block( bank, accdb, capture_ctx, runtime_stack );
1714 0 : if( FD_UNLIKELY( err ) ) FD_LOG_CRIT(( "genesis slot 0 execute failed with error %d", err ));
1715 :
1716 : /* Genesis init ran the epoch-boundary refresh paths above; discard
1717 : the accumulated summary so the first real boundary's runtime_epoch
1718 : event only reflects itself. */
1719 0 : if( FD_UNLIKELY( fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_epoch_reset();
1720 0 : }
1721 :
1722 : /* valid_producer_time returns 1 iff an Alpenglow block footer's
1723 : producer_time_nanos is within valid bounds relative to the
1724 : Alpenclock PDA and can be safely represented as a long, and 0 if
1725 : not.
1726 :
1727 : https://github.com/anza-xyz/agave/blob/v4.3.0-beta.3/runtime/src/block_component_processor.rs#L698-L726 */
1728 :
1729 : static inline int
1730 : valid_producer_time( fd_bank_t const * bank,
1731 : fd_accdb_t * accdb,
1732 : fd_pubkey_t const * alpenclock_addr,
1733 : fd_sol_sysvar_clock_t const * clock,
1734 0 : ulong producer_time_nanos ) {
1735 0 : fd_acc_t alpenclock_ac = fd_accdb_read_one( accdb, bank->accdb_fork_id, alpenclock_addr->uc );
1736 :
1737 : /* Fall back to the sysvar clock if the Alpenclock PDA does not exist
1738 : yet (the first block after the Alpenglow migration). */
1739 0 : long parent_nanos = ( alpenclock_ac.lamports && alpenclock_ac.data_len>=sizeof(ulong) )
1740 0 : ? (long)FD_LOAD( ulong, alpenclock_ac.data )
1741 0 : : fd_long_sat_mul( clock->unix_timestamp, 1000000000L );
1742 0 : fd_accdb_unread_one( accdb, &alpenclock_ac );
1743 :
1744 0 : ulong elapsed_nanos = fd_slot_params_slot_range_duration_ns( bank, bank->f.parent_slot+1UL, bank->f.slot+1UL );
1745 0 : long lo = fd_long_sat_add( parent_nanos, 1L );
1746 0 : long hi = fd_long_sat_add( parent_nanos, (long)fd_ulong_min( fd_ulong_sat_mul( elapsed_nanos, 2UL ), (ulong)LONG_MAX ) );
1747 0 : return producer_time_nanos<=(ulong)LONG_MAX && (long)producer_time_nanos>=lo && (long)producer_time_nanos<=hi;
1748 0 : }
1749 :
1750 : static int
1751 : apply_footer( fd_bank_t * bank,
1752 : fd_accdb_t * accdb,
1753 : fd_capture_ctx_t * capture_ctx,
1754 : fd_block_footer_t const * footer,
1755 0 : ushort shred_version ) {
1756 :
1757 : /* leader bank's certs were verified by votor */
1758 0 : if( FD_UNLIKELY( !bank->is_leader && fd_alpenglow_footer_verify( bank, footer, shred_version ) ) ) return -1;
1759 :
1760 : /* Rewrite the clock sysvar and the alpenclock account from the
1761 : footer's producer timestamp (Agave Bank::update_clock_from_footer).
1762 : The clock must be applied before the reward certs. */
1763 :
1764 0 : ulong producer_time_nanos = footer->block_producer_time_nanos;
1765 0 : long unix_timestamp = (long)(producer_time_nanos/1000000000UL);
1766 :
1767 0 : fd_sol_sysvar_clock_t clock_[1];
1768 0 : fd_sol_sysvar_clock_t * clock = fd_sysvar_clock_read( accdb, bank->accdb_fork_id, clock_ );
1769 0 : if( FD_UNLIKELY( !clock ) ) FD_LOG_ERR(( "fd_sysvar_clock_read failed" ));
1770 :
1771 0 : fd_pubkey_t alpenclock_addr;
1772 0 : fd_alpenglow_pda( "alpenclock", &alpenclock_addr );
1773 :
1774 0 : if( FD_UNLIKELY( !valid_producer_time( bank, accdb, &alpenclock_addr, clock, producer_time_nanos ) ) ) return -1;
1775 :
1776 0 : fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
1777 0 : ulong current_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.slot, NULL );
1778 0 : ulong parent_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.parent_slot, NULL );
1779 :
1780 0 : long epoch_start_timestamp = clock->epoch_start_timestamp;
1781 0 : if( FD_UNLIKELY( bank->f.slot && parent_epoch!=current_epoch ) ) epoch_start_timestamp = unix_timestamp;
1782 :
1783 0 : fd_sol_sysvar_clock_t new_clock = {
1784 0 : .slot = bank->f.slot,
1785 0 : .epoch_start_timestamp = epoch_start_timestamp,
1786 0 : .epoch = current_epoch,
1787 0 : .leader_schedule_epoch = fd_slot_to_leader_schedule_epoch( epoch_schedule, bank->f.slot ),
1788 0 : .unix_timestamp = unix_timestamp,
1789 0 : };
1790 0 : fd_sysvar_account_update( bank, accdb, capture_ctx, &fd_sysvar_clock_id, &new_clock, sizeof(fd_sol_sysvar_clock_t) );
1791 :
1792 : /* size the alpenclock balance with exactly the default rent, any
1793 : excess lamports must be burned */
1794 0 : uchar data[ 8UL ];
1795 0 : FD_STORE( ulong, data, producer_time_nanos );
1796 0 : fd_accdb_svm_update_t update[1];
1797 0 : fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, &alpenclock_addr, 1 );
1798 0 : acc.lamports = fd_rent_exempt_minimum_balance( &FD_RENT_DEFAULT_PARAMS, sizeof(data) );
1799 0 : fd_memcpy( acc.owner, fd_solana_system_program_id.uc, sizeof(fd_pubkey_t) );
1800 0 : acc.executable = 0;
1801 0 : acc.data_len = sizeof(data);
1802 0 : fd_memcpy( acc.data, data, sizeof(data) );
1803 0 : fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
1804 :
1805 0 : return fd_alpenglow_rewards_apply( bank, accdb, capture_ctx, footer );
1806 0 : }
1807 :
1808 : int
1809 : fd_runtime_block_execute_finalize( fd_bank_t * bank,
1810 : fd_accdb_t * accdb,
1811 : fd_capture_ctx_t * capture_ctx,
1812 : fd_block_footer_t const * footer,
1813 357 : ushort shred_version ) {
1814 357 : if( FD_UNLIKELY( footer && apply_footer( bank, accdb, capture_ctx, footer, shred_version ) ) ) return -1;
1815 357 : fd_runtime_freeze( bank, accdb, capture_ctx );
1816 357 : fd_runtime_update_bank_hash( bank, capture_ctx );
1817 357 : return 0;
1818 357 : }
1819 :
1820 : /* Mirrors Agave function solana_sdk::transaction_context::find_index_of_account
1821 :
1822 : Backward scan over transaction accounts. Returns ULONG_MAX if not found.
1823 :
1824 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L233-L238 */
1825 :
1826 : ulong
1827 : fd_runtime_find_index_of_account( fd_txn_out_t const * txn_out,
1828 10575 : fd_pubkey_t const * pubkey ) {
1829 25842 : for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
1830 22920 : if( FD_UNLIKELY( !memcmp( pubkey, &txn_out->accounts.keys[ txn_out->accounts.cnt-1UL-i ], sizeof(fd_pubkey_t) ) ) ) return txn_out->accounts.cnt-1UL-i;
1831 22920 : }
1832 2922 : return ULONG_MAX;
1833 10575 : }
1834 :
1835 : fd_acc_t *
1836 : fd_runtime_get_account_at_index( fd_txn_in_t const * txn_in,
1837 : fd_txn_out_t * txn_out,
1838 : ushort idx,
1839 25764 : fd_txn_account_condition_fn_t * condition ) {
1840 25764 : if( FD_UNLIKELY( idx>=txn_out->accounts.cnt ) ) return NULL;
1841 25764 : if( FD_LIKELY( condition && !condition( txn_in, txn_out, idx ) ) ) return NULL;
1842 25623 : return txn_out->accounts.account[ idx ];
1843 25764 : }
1844 :
1845 : fd_acc_t *
1846 : fd_runtime_get_executable_account( fd_txn_out_t * txn_out,
1847 : fd_pubkey_t const * pubkey,
1848 : int * from_parent_copy,
1849 69 : int * pd_write_this_slot ) {
1850 : /* First try to fetch the executable account from the existing
1851 : borrowed accounts. If the pubkey is in the account keys, then we
1852 : want to re-use that borrowed account since it reflects changes from
1853 : prior instructions. Referencing the read-only executable accounts
1854 : list is incorrect behavior when the program data account is written
1855 : to in a prior instruction (e.g. program upgrade + invoke within the
1856 : same txn) */
1857 :
1858 69 : if( FD_UNLIKELY( from_parent_copy ) ) *from_parent_copy = 0;
1859 69 : if( FD_UNLIKELY( pd_write_this_slot ) ) *pd_write_this_slot = 0;
1860 :
1861 69 : ulong account_idx = fd_runtime_find_index_of_account( txn_out, pubkey );
1862 69 : if( FD_LIKELY( account_idx!=ULONG_MAX && txn_out->accounts.account[ account_idx ]->lamports ) ) return txn_out->accounts.account[ account_idx ];
1863 :
1864 66 : for( ushort i=0; i<txn_out->accounts.executable_cnt; i++ ) {
1865 33 : fd_acc_t * ro = txn_out->accounts.executable[ i ];
1866 33 : if( FD_UNLIKELY( !memcmp( pubkey->uc, ro->pubkey, 32UL ) ) ) {
1867 33 : if( FD_UNLIKELY( !ro->lamports ) ) return NULL;
1868 33 : if( FD_UNLIKELY( from_parent_copy ) ) *from_parent_copy = txn_out->accounts.executable_from_parent[ i ];
1869 33 : if( FD_UNLIKELY( pd_write_this_slot ) ) *pd_write_this_slot = txn_out->accounts.executable_pd_write[ i ];
1870 33 : return ro;
1871 33 : }
1872 33 : }
1873 :
1874 33 : return NULL;
1875 66 : }
1876 :
1877 : int
1878 : fd_runtime_get_key_of_account_at_index( fd_txn_out_t * txn_out,
1879 : ushort idx,
1880 23856 : fd_pubkey_t const * * key ) {
1881 : /* Return a MissingAccount error if idx is out of bounds.
1882 : https://github.com/anza-xyz/agave/blob/v3.1.4/transaction-context/src/lib.rs#L187 */
1883 23856 : if( FD_UNLIKELY( idx>=txn_out->accounts.cnt ) ) {
1884 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1885 0 : }
1886 :
1887 23856 : *key = &txn_out->accounts.keys[ idx ];
1888 23856 : return FD_EXECUTOR_INSTR_SUCCESS;
1889 23856 : }
1890 :
1891 : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L162 */
1892 : int
1893 : fd_txn_account_is_demotion( const int idx,
1894 : const fd_txn_t * txn_descriptor,
1895 1350 : const uint bpf_upgradeable_in_txn ) {
1896 1350 : uint is_program = 0U;
1897 2760 : for( ulong j=0UL; j<txn_descriptor->instr_cnt; j++ ) {
1898 1416 : if( txn_descriptor->instr[j].program_id == idx ) {
1899 6 : is_program = 1U;
1900 6 : break;
1901 6 : }
1902 1416 : }
1903 :
1904 1350 : return (is_program && !bpf_upgradeable_in_txn);
1905 1350 : }
1906 :
1907 : uint
1908 : fd_txn_account_has_bpf_loader_upgradeable( fd_pubkey_t const * account_keys,
1909 2547 : ulong accounts_cnt ) {
1910 16920 : for( ulong j=0; j<accounts_cnt; j++ ) {
1911 14679 : const fd_pubkey_t * acc = &account_keys[j];
1912 14679 : if ( memcmp( acc->uc, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) == 0 ) {
1913 306 : return 1U;
1914 306 : }
1915 14679 : }
1916 2241 : return 0U;
1917 2547 : }
1918 :
1919 : static inline int
1920 : fd_runtime_account_is_writable_idx_flat( const ushort idx,
1921 : const fd_pubkey_t * addr_at_idx,
1922 : const fd_txn_t * txn_descriptor,
1923 2547 : const uint bpf_upgradeable_in_txn ) {
1924 : /* https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L43 */
1925 2547 : if( !fd_txn_is_writable( txn_descriptor, idx ) ) {
1926 1191 : return 0;
1927 1191 : }
1928 :
1929 : /* See comments in fd_system_ids.h.
1930 : https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L44 */
1931 1356 : if( fd_pubkey_is_active_reserved_key( addr_at_idx ) ||
1932 1356 : fd_pubkey_is_pending_reserved_key( addr_at_idx ) ) {
1933 :
1934 6 : return 0;
1935 6 : }
1936 :
1937 1350 : if( fd_txn_account_is_demotion( idx, txn_descriptor, bpf_upgradeable_in_txn ) ) {
1938 6 : return 0;
1939 6 : }
1940 :
1941 1344 : return 1;
1942 1350 : }
1943 :
1944 :
1945 : /* This function aims to mimic the writable accounts check to populate the writable accounts cache, used
1946 : to determine if accounts are writable or not.
1947 :
1948 : https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L38-L47 */
1949 : int
1950 : fd_runtime_account_is_writable_idx( fd_txn_in_t const * txn_in,
1951 : fd_txn_out_t const * txn_out,
1952 2547 : ushort idx ) {
1953 2547 : uint bpf_upgradeable = fd_txn_account_has_bpf_loader_upgradeable( txn_out->accounts.keys, txn_out->accounts.cnt );
1954 2547 : return fd_runtime_account_is_writable_idx_flat( idx,
1955 2547 : &txn_out->accounts.keys[idx],
1956 2547 : TXN( txn_in->txn ),
1957 2547 : bpf_upgradeable );
1958 2547 : }
1959 :
1960 : /* Account pre-condition filtering functions */
1961 :
1962 : int
1963 : fd_runtime_account_check_exists( fd_txn_in_t const * txn_in,
1964 : fd_txn_out_t * txn_out,
1965 2178 : ushort idx ) {
1966 2178 : (void) txn_in;
1967 2178 : return txn_out->accounts.account[ idx ]->lamports!=0UL;
1968 2178 : }
1969 :
1970 : int
1971 : fd_runtime_account_check_fee_payer_writable( fd_txn_in_t const * txn_in,
1972 : fd_txn_out_t * txn_out,
1973 399 : ushort idx ) {
1974 399 : (void) txn_out;
1975 399 : return fd_txn_is_writable( TXN( txn_in->txn ), idx );
1976 399 : }
1977 :
1978 :
1979 : int
1980 : fd_account_meta_checked_sub_lamports( fd_acc_t * acc,
1981 399 : ulong lamports ) {
1982 399 : ulong balance_post = 0UL;
1983 399 : int err = fd_ulong_checked_sub( acc->lamports, lamports, &balance_post );
1984 399 : if( FD_UNLIKELY( err ) ) return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
1985 :
1986 399 : acc->lamports = balance_post;
1987 399 : return FD_EXECUTOR_INSTR_SUCCESS;
1988 399 : }
1989 :
1990 : /* fd_executor_reuse_bundle_executable scans the runtime's deduplicated
1991 : account pools for a programdata account matching programdata_key that
1992 : was already opened by an earlier transaction in the bundle (either as
1993 : a regular transaction account or as a programdata account). Returns
1994 : the existing fd_acc_t so it can be reused instead of re-acquiring it,
1995 : or NULL if none is found. Must only be called for bundle txns. */
1996 :
1997 : static fd_acc_t *
1998 : reuse_bundle_executable( fd_runtime_t * runtime,
1999 33 : fd_pubkey_t const * programdata_key ) {
2000 102 : for( ulong j=0UL; j<runtime->accounts.account_cnt; j++ ) {
2001 84 : if( FD_LIKELY( !fd_pubkey_eq( fd_type_pun_const( &runtime->accounts.account[ j ].pubkey ), programdata_key ) ) ) continue;
2002 15 : return &runtime->accounts.account[ j ];
2003 84 : }
2004 18 : for( ulong j=0UL; j<runtime->accounts.executable_cnt; j++ ) {
2005 6 : if( FD_LIKELY( !fd_pubkey_eq( fd_type_pun_const( &runtime->accounts.executable[ j ].pubkey ), programdata_key ) ) ) continue;
2006 6 : return &runtime->accounts.executable[ j ];
2007 6 : }
2008 12 : return NULL;
2009 18 : }
2010 :
2011 : int
2012 : fd_runtime_prepare_bundle_accounts( fd_runtime_t * runtime,
2013 : fd_bank_t * bank,
2014 : fd_txn_in_t const * txn_ins,
2015 : fd_txn_out_t * txn_outs,
2016 54 : ulong txn_cnt ) {
2017 :
2018 54 : # define FD_BUNDLE_ACCT_MAX (FD_PACK_MAX_TXN_PER_BUNDLE*MAX_TX_ACCOUNT_LOCKS)
2019 :
2020 54 : runtime->accounts.account_cnt = 0UL;
2021 54 : runtime->accounts.executable_cnt = 0UL;
2022 :
2023 54 : uchar const * acquire_pubkeys[ FD_BUNDLE_ACCT_MAX ];
2024 54 : int acquire_writable[ FD_BUNDLE_ACCT_MAX ];
2025 54 : ulong acquire_cnt = 0UL;
2026 :
2027 : /* First resolve a deduped set of account keys for all txns in the
2028 : bundle. This includes static account keys as well as ALUT resolved
2029 : addresses. */
2030 :
2031 162 : for( ulong i=0UL; i<txn_cnt; i++ ) {
2032 111 : fd_txn_in_t const * txn_in = &txn_ins [ i ];
2033 111 : fd_txn_out_t * txn_out = &txn_outs[ i ];
2034 :
2035 111 : txn_out->accounts.cnt = (uchar)TXN( txn_in->txn )->acct_addr_cnt;
2036 111 : fd_pubkey_t * tx_accs = (fd_pubkey_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->acct_addr_off);
2037 867 : for( ulong j=0UL; j<TXN( txn_in->txn )->acct_addr_cnt; j++ ) {
2038 756 : txn_out->accounts.keys[ j ] = tx_accs[ j ];
2039 756 : txn_out->accounts.account[ j ] = NULL;
2040 756 : txn_out->accounts.account_acquired[ j ] = 0U;
2041 756 : }
2042 :
2043 111 : int err = fd_executor_setup_txn_alut_account_keys( runtime, bank, txn_in, txn_out );
2044 111 : for( ulong j=TXN( txn_in->txn )->acct_addr_cnt; j<txn_out->accounts.cnt; j++ ) {
2045 0 : txn_out->accounts.account[ j ] = NULL;
2046 0 : txn_out->accounts.account_acquired[ j ] = 0U;
2047 0 : }
2048 111 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
2049 :
2050 : /* Validate account locks before the union acquire below, bounding
2051 : the deduped set within the accdb acquire limit. */
2052 108 : err = fd_executor_validate_account_locks( txn_out );
2053 108 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
2054 :
2055 861 : for( ushort j=0; j<txn_out->accounts.cnt; j++ ) {
2056 753 : fd_pubkey_t const * key = &txn_out->accounts.keys[ j ];
2057 753 : int dup = 0;
2058 3729 : for( ulong k=0UL; k<acquire_cnt; k++ ) if( FD_UNLIKELY( !memcmp( acquire_pubkeys[ k ], key->uc, 32UL ) ) ) { dup = 1; break; }
2059 753 : if( FD_UNLIKELY( dup ) ) continue;
2060 357 : FD_TEST( acquire_cnt<FD_BUNDLE_ACCT_MAX );
2061 357 : acquire_pubkeys [ acquire_cnt ] = key->uc;
2062 : /* Bundle accounts are always acquired writable so that a later
2063 : txn can cleanly upgrade a read permission to a write. */
2064 357 : acquire_writable[ acquire_cnt ] = 1;
2065 357 : acquire_cnt++;
2066 357 : }
2067 108 : }
2068 :
2069 51 : if( FD_LIKELY( acquire_cnt ) ) {
2070 51 : fd_accdb_acquire_a( runtime->accdb, bank->accdb_fork_id, acquire_cnt, acquire_pubkeys, acquire_writable, runtime->accounts.account );
2071 51 : runtime->accounts.account_cnt = acquire_cnt;
2072 51 : }
2073 :
2074 156 : for( ulong i=0UL; i<txn_cnt; i++ ) {
2075 105 : fd_txn_out_t * txn_out = &txn_outs[ i ];
2076 849 : for( ushort j=0; j<txn_out->accounts.cnt; j++ ) {
2077 744 : txn_out->accounts.account[ j ] = NULL;
2078 3711 : for( ulong k=0UL; k<runtime->accounts.account_cnt; k++ ) {
2079 3711 : fd_acc_t * acc = &runtime->accounts.account[ k ];
2080 3711 : if( FD_LIKELY( !fd_pubkey_eq( fd_type_pun_const( &acc->pubkey ), &txn_out->accounts.keys[ j ] ) ) ) continue;
2081 744 : txn_out->accounts.account[ j ] = acc;
2082 744 : txn_out->accounts.starting_lamports[ j ] = acc->prior_lamports;
2083 744 : txn_out->accounts.starting_data_len[ j ] = acc->prior_data_len;
2084 744 : memcpy( &txn_out->accounts.starting_owner[ j ], acc->prior_owner, sizeof(fd_pubkey_t) );
2085 744 : break;
2086 3711 : }
2087 744 : }
2088 105 : }
2089 :
2090 : /* Do the same for executable accounts (programdata accounts). Dedup
2091 : against all other executable-only accounts as well as ones that
2092 : were already included. */
2093 :
2094 51 : fd_pubkey_t programdata_keys[ FD_BUNDLE_ACCT_MAX ];
2095 51 : uchar const * pd_pubkeys [ FD_BUNDLE_ACCT_MAX ];
2096 51 : int pd_writable [ FD_BUNDLE_ACCT_MAX ];
2097 51 : ulong pd_cnt = 0UL;
2098 :
2099 51 : FD_TEST( bank->parent_accdb_fork_id.val!=USHORT_MAX );
2100 :
2101 399 : for( ulong i=0UL; i<runtime->accounts.account_cnt; i++ ) {
2102 348 : fd_acc_t * acc = &runtime->accounts.account[ i ];
2103 348 : if( FD_LIKELY( memcmp( acc->owner, fd_solana_bpf_loader_upgradeable_program_id.key, 32UL ) ) ) continue;
2104 33 : fd_bpf_state_t program_loader_state[1];
2105 33 : if( FD_UNLIKELY( fd_bpf_loader_program_get_state( acc, program_loader_state )!=FD_EXECUTOR_INSTR_SUCCESS ) ) continue;
2106 33 : if( FD_UNLIKELY( program_loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) ) continue;
2107 :
2108 21 : fd_pubkey_t const * programdata_key = &program_loader_state->inner.program.programdata_address;
2109 : /* PD is not live as of the parent, so there is nothing to acquire
2110 : from the parent. We will still need its latest length for loaded
2111 : accounts data size, but that is taken care of per transaction by
2112 : fd_runtime_setup_bundle_executables() without acquiring. */
2113 21 : if( FD_UNLIKELY( !fd_accdb_exists( runtime->accdb, bank->parent_accdb_fork_id, programdata_key->uc ) ) ) continue;
2114 :
2115 : /* Already part of the transaction account pool, or already queued. */
2116 9 : if( reuse_bundle_executable( runtime, programdata_key ) ) continue;
2117 6 : int dup = 0;
2118 6 : for( ulong u=0UL; u<pd_cnt; u++ ) if( FD_UNLIKELY( !memcmp( programdata_keys[ u ].uc, programdata_key->uc, 32UL ) ) ) { dup = 1; break; }
2119 6 : if( dup ) continue;
2120 :
2121 6 : FD_TEST( pd_cnt<FD_BUNDLE_ACCT_MAX );
2122 6 : programdata_keys[ pd_cnt ] = *programdata_key;
2123 6 : pd_pubkeys[ pd_cnt ] = programdata_keys[ pd_cnt ].uc;
2124 6 : pd_writable[ pd_cnt ] = 0;
2125 6 : pd_cnt++;
2126 6 : }
2127 :
2128 : /* acquire_b refunds the per-class reservations acquire_a made for the
2129 : union (reserved_cnt==acquire_cnt) that did not turn out to be
2130 : programdata. Skip it entirely for an empty bundle (nothing was
2131 : reserved and nothing is executable). */
2132 51 : if( FD_LIKELY( acquire_cnt || pd_cnt ) ) {
2133 51 : fd_accdb_acquire_b( runtime->accdb, bank->parent_accdb_fork_id, acquire_cnt, pd_cnt, pd_pubkeys, pd_writable, runtime->accounts.executable );
2134 51 : }
2135 51 : runtime->accounts.executable_cnt = pd_cnt;
2136 :
2137 51 : return FD_RUNTIME_EXECUTE_SUCCESS;
2138 :
2139 51 : # undef FD_BUNDLE_ACCT_MAX
2140 51 : }
2141 :
2142 : int
2143 : fd_runtime_setup_bundle_executables( fd_runtime_t * runtime,
2144 : fd_bank_t * bank,
2145 105 : fd_txn_out_t * txn_out ) {
2146 105 : FD_TEST( bank->parent_accdb_fork_id.val!=USHORT_MAX );
2147 :
2148 105 : ushort exe_cnt = 0;
2149 105 : txn_out->accounts.executable_skipped_cnt = 0;
2150 849 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
2151 : /* The checks below mimic the ones in the non-bundle path. The
2152 : difference is only the source of state as of this transaction.
2153 : Here it's from the live pool when possible, and in the non-bundle
2154 : path it's from the accdb. */
2155 744 : fd_acc_t * acc = txn_out->accounts.account[ i ]; /* This has gone through zero-lamport reset by fd_executor_setup_accounts_for_txn_bundle(). */
2156 744 : if( FD_LIKELY( memcmp( acc->owner, fd_solana_bpf_loader_upgradeable_program_id.key, 32UL ) ) ) continue;
2157 42 : fd_bpf_state_t program_loader_state[1];
2158 42 : if( FD_UNLIKELY( fd_bpf_loader_program_get_state( acc, program_loader_state )!=FD_EXECUTOR_INSTR_SUCCESS ) ) continue;
2159 42 : if( FD_UNLIKELY( program_loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) ) continue;
2160 :
2161 30 : fd_pubkey_t const * programdata_key = &program_loader_state->inner.program.programdata_address;
2162 :
2163 : /* Declared PD is charged as a plain top-level transaction account
2164 : and is preferred by fd_runtime_get_executable_account(). */
2165 30 : if( FD_UNLIKELY( fd_runtime_find_index_of_account( txn_out, programdata_key )!=ULONG_MAX ) ) continue;
2166 :
2167 24 : fd_acc_t * programdata = reuse_bundle_executable( runtime, programdata_key );
2168 :
2169 24 : if( FD_UNLIKELY( !fd_accdb_exists( runtime->accdb, bank->parent_accdb_fork_id, programdata_key->uc ) ) ) {
2170 : /* PD is not live as of the parent. Length only, and no
2171 : executable[] entry, so the loader reports "Program is not
2172 : deployed" exactly as the non-bundle path does. The non-bundle
2173 : path takes the length from the latest accdb view committed on
2174 : this fork. The equivalent here is the live pool copy, which an
2175 : earlier bundle transaction may have created, resized or
2176 : drained. Only a LIVE PD is recorded. Membership in the
2177 : skipped list is the liveness answer, so a dead PD is left out
2178 : entirely rather than recorded with a zero length: a live PD may
2179 : itself have zero-length data, and Agave still charges the
2180 : 64-byte base for that. */
2181 15 : ulong skip_len;
2182 15 : if( FD_LIKELY( programdata ) ) {
2183 : /* A dead PD must contribute nothing to loaded accounts data
2184 : size. Like every other consumer, the lamports need to be
2185 : checked first. The pool copy that programdata points to has
2186 : an up to date lamports, but it is not necessarily otherwise
2187 : normalized: zero length, non-executable, and system-owned.
2188 : The reset in setup_accounts_for_txn_bundle() only covers the
2189 : declared accounts of the transaction. If PD were declared,
2190 : it would have short circuited above. So an in-bundle Close
2191 : leaves PD here at zero lamports with length at
2192 : SIZE_OF_UNINITIALIZED. */
2193 9 : if( FD_UNLIKELY( !programdata->lamports ) ) continue;
2194 6 : skip_len = programdata->data_len;
2195 6 : } else {
2196 : /* At this point, this PD
2197 : - has no pool copy, so no bundle transaction declares PD, and
2198 : - it is not live on the parent.
2199 :
2200 : So PD was created this slot by something outside the bundle,
2201 : and only the latest accdb view has the length we need for
2202 : accounting. It may also have been created and closed within
2203 : this slot, in which case it is dead and contributes nothing. */
2204 6 : int probe_pd = 0;
2205 6 : ulong probe_len = 0UL;
2206 6 : ulong probe_lamports = 0UL;
2207 6 : if( FD_UNLIKELY( !fd_accdb_probe_pd_this_fork( runtime->accdb, bank->accdb_fork_id, programdata_key->uc, &probe_pd, &probe_len, &probe_lamports ) ) ) continue;
2208 6 : if( FD_UNLIKELY( !probe_lamports ) ) continue;
2209 3 : skip_len = probe_len;
2210 3 : }
2211 9 : ushort s = txn_out->accounts.executable_skipped_cnt++;
2212 9 : txn_out->accounts.executable_skipped_key[ s ] = *programdata_key;
2213 9 : txn_out->accounts.executable_skipped_len[ s ] = skip_len;
2214 9 : continue;
2215 15 : }
2216 :
2217 : /* At this point, PD exists in the parent. So prepare() must have
2218 : acquired PD, either in accounts[] if a bundle transaction
2219 : declares it, or in executable[] aka implied load from the parent.
2220 : A miss would imply that a pool account only began parsing as
2221 : Program{PD} after prepare() computed the acquire set, naming a PD
2222 : no bundle transaction declares. This should not be possible,
2223 : because only the DeployWithMaxDataLen instruction writes
2224 : Program{PD}. The instruction declares PD, and its CreateAccount
2225 : CPI cannot succeed against a PD that is already live on the
2226 : parent. We cannot acquire the PD at this point, since a
2227 : mid-bundle acquire of an account the bundle might be holding as
2228 : writable would violate the accdb acquire contract. So fail the
2229 : bundle. */
2230 9 : if( FD_UNLIKELY( !programdata ) ) {
2231 0 : FD_BASE58_ENCODE_32_BYTES( programdata_key->uc, pd_str );
2232 0 : FD_BASE58_ENCODE_64_BYTES( txn_out->details.signature.uc, sig_str );
2233 0 : FD_LOG_INFO(( "dropping bundle: bundle txn %s in slot %lu implies unacquired programdata %s", sig_str, bank->f.slot, pd_str ));
2234 0 : return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
2235 0 : }
2236 :
2237 9 : int from_parent = ( programdata>=runtime->accounts.executable ) &&
2238 9 : ( programdata< runtime->accounts.executable+runtime->accounts.executable_cnt ) &&
2239 9 : ( bank->parent_accdb_fork_id.val!=bank->accdb_fork_id.val );
2240 9 : txn_out->accounts.executable[ exe_cnt ] = programdata;
2241 9 : txn_out->accounts.executable_from_parent[ exe_cnt ] = from_parent;
2242 9 : if( from_parent ) {
2243 : /* A from_parent copy is one no bundle transaction declared, hence
2244 : one no bundle transaction could have modified, because mutating
2245 : PD requires declaring it writable. So the probe from accdb
2246 : reports the latest state that we need. */
2247 6 : int pd = 0;
2248 6 : ulong len = ULONG_MAX;
2249 6 : ulong lamports = 0UL;
2250 6 : fd_accdb_probe_pd_this_fork( runtime->accdb, bank->accdb_fork_id, programdata_key->uc, &pd, &len, &lamports );
2251 6 : txn_out->accounts.executable_pd_write [ exe_cnt ] = pd;
2252 6 : txn_out->accounts.executable_cur_len [ exe_cnt ] = len;
2253 6 : txn_out->accounts.executable_cur_lamports[ exe_cnt ] = lamports;
2254 6 : } else {
2255 3 : txn_out->accounts.executable_pd_write [ exe_cnt ] = 0;
2256 3 : txn_out->accounts.executable_cur_len [ exe_cnt ] = ULONG_MAX;
2257 3 : txn_out->accounts.executable_cur_lamports[ exe_cnt ] = 0UL;
2258 3 : }
2259 9 : exe_cnt++;
2260 9 : }
2261 105 : txn_out->accounts.executable_cnt = exe_cnt;
2262 105 : return FD_RUNTIME_EXECUTE_SUCCESS;
2263 105 : }
|