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