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