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