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