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