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