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