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