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