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