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