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