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