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