Line data Source code
1 : #include "fd_runtime.h"
2 : #include "context/fd_capture_ctx.h"
3 : #include "fd_acc_mgr.h"
4 : #include "fd_alut_interp.h"
5 : #include "fd_bank.h"
6 : #include "fd_hashes.h"
7 : #include "fd_runtime_err.h"
8 : #include "fd_runtime_init.h"
9 : #include "fd_runtime_stack.h"
10 :
11 : #include "fd_executor.h"
12 : #include "sysvar/fd_sysvar_cache.h"
13 : #include "sysvar/fd_sysvar_clock.h"
14 : #include "sysvar/fd_sysvar_epoch_schedule.h"
15 : #include "sysvar/fd_sysvar_recent_hashes.h"
16 : #include "sysvar/fd_sysvar_stake_history.h"
17 :
18 : #include "../stakes/fd_stakes.h"
19 : #include "../rewards/fd_rewards.h"
20 : #include "../progcache/fd_progcache_user.h"
21 :
22 : #include "context/fd_exec_txn_ctx.h"
23 :
24 : #include "program/fd_stake_program.h"
25 : #include "program/fd_builtin_programs.h"
26 :
27 : #include "sysvar/fd_sysvar_clock.h"
28 : #include "sysvar/fd_sysvar_last_restart_slot.h"
29 : #include "sysvar/fd_sysvar_recent_hashes.h"
30 : #include "sysvar/fd_sysvar_rent.h"
31 : #include "sysvar/fd_sysvar_slot_hashes.h"
32 : #include "sysvar/fd_sysvar_slot_history.h"
33 :
34 : #include "tests/fd_dump_pb.h"
35 :
36 : #include "fd_system_ids.h"
37 :
38 : #include "../../disco/pack/fd_pack.h"
39 : #include "../../disco/genesis/fd_genesis_cluster.h"
40 :
41 : #include <unistd.h>
42 : #include <sys/stat.h>
43 : #include <sys/types.h>
44 : #include <fcntl.h>
45 :
46 : /******************************************************************************/
47 : /* Public Runtime Helpers */
48 : /******************************************************************************/
49 :
50 : int
51 0 : fd_runtime_should_use_vote_keyed_leader_schedule( fd_bank_t * bank ) {
52 : /* Agave uses an option type for their effective_epoch value. We
53 : represent None as ULONG_MAX and Some(value) as the value.
54 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6149-L6165 */
55 0 : if( FD_FEATURE_ACTIVE_BANK( bank, enable_vote_address_leader_schedule ) ) {
56 : /* Return the first epoch if activated at genesis
57 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6153-L6157 */
58 0 : ulong activation_slot = fd_bank_features_query( bank )->enable_vote_address_leader_schedule;
59 0 : if( activation_slot==0UL ) return 1; /* effective_epoch=0, current_epoch >= effective_epoch always true */
60 :
61 : /* Calculate the epoch that the feature became activated in
62 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6159-L6160 */
63 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
64 0 : ulong activation_epoch = fd_slot_to_epoch( epoch_schedule, activation_slot, NULL );
65 :
66 : /* The effective epoch is the epoch immediately after the activation
67 : epoch.
68 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6162-L6164 */
69 0 : ulong effective_epoch = activation_epoch + 1UL;
70 0 : ulong current_epoch = fd_bank_epoch_get( bank );
71 :
72 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6167-L6170 */
73 0 : return !!( current_epoch >= effective_epoch );
74 0 : }
75 :
76 : /* ...The rest of the logic in this function either returns None or
77 : Some(false) so we will just return 0 by default. */
78 0 : return 0;
79 0 : }
80 :
81 : /*
82 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1254-L1258
83 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1749
84 : */
85 : int
86 : fd_runtime_compute_max_tick_height( ulong ticks_per_slot,
87 : ulong slot,
88 0 : ulong * out_max_tick_height /* out */ ) {
89 0 : ulong max_tick_height = 0UL;
90 0 : if( FD_LIKELY( ticks_per_slot > 0UL ) ) {
91 0 : ulong next_slot = fd_ulong_sat_add( slot, 1UL );
92 0 : if( FD_UNLIKELY( next_slot == slot ) ) {
93 0 : FD_LOG_WARNING(( "max tick height addition overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
94 0 : return -1;
95 0 : }
96 0 : if( FD_UNLIKELY( ULONG_MAX / ticks_per_slot < next_slot ) ) {
97 0 : FD_LOG_WARNING(( "max tick height multiplication overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
98 0 : return -1;
99 0 : }
100 0 : max_tick_height = fd_ulong_sat_mul( next_slot, ticks_per_slot );
101 0 : }
102 0 : *out_max_tick_height = max_tick_height;
103 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
104 0 : }
105 :
106 : void
107 : fd_runtime_update_leaders( fd_bank_t * bank,
108 0 : fd_runtime_stack_t * runtime_stack ) {
109 :
110 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
111 :
112 0 : ulong epoch = fd_slot_to_epoch ( epoch_schedule, fd_bank_slot_get( bank ), NULL );
113 0 : ulong slot0 = fd_epoch_slot0 ( epoch_schedule, epoch );
114 0 : ulong slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
115 :
116 0 : fd_vote_states_t const * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_query( bank );
117 0 : fd_vote_stake_weight_t * epoch_weights = runtime_stack->stakes.stake_weights;
118 0 : ulong stake_weight_cnt = fd_stake_weights_by_node( vote_states_prev_prev, epoch_weights );
119 0 : fd_bank_vote_states_prev_prev_end_locking_query( bank );
120 :
121 : /* Derive leader schedule */
122 :
123 0 : ulong epoch_leaders_footprint = fd_epoch_leaders_footprint( stake_weight_cnt, slot_cnt );
124 0 : if( FD_LIKELY( epoch_leaders_footprint ) ) {
125 0 : if( FD_UNLIKELY( stake_weight_cnt>MAX_PUB_CNT ) ) {
126 0 : FD_LOG_ERR(( "Stake weight count exceeded max" ));
127 0 : }
128 0 : if( FD_UNLIKELY( slot_cnt>MAX_SLOTS_PER_EPOCH ) ) {
129 0 : FD_LOG_ERR(( "Slot count exceeeded max" ));
130 0 : }
131 :
132 0 : ulong vote_keyed_lsched = (ulong)fd_runtime_should_use_vote_keyed_leader_schedule( bank );
133 0 : void * epoch_leaders_mem = fd_bank_epoch_leaders_locking_modify( bank );
134 0 : fd_epoch_leaders_t * leaders = fd_epoch_leaders_join( fd_epoch_leaders_new(
135 0 : epoch_leaders_mem,
136 0 : epoch,
137 0 : slot0,
138 0 : slot_cnt,
139 0 : stake_weight_cnt,
140 0 : epoch_weights,
141 0 : 0UL,
142 0 : vote_keyed_lsched ) );
143 0 : if( FD_UNLIKELY( !leaders ) ) {
144 0 : FD_LOG_ERR(( "Unable to init and join fd_epoch_leaders" ));
145 0 : }
146 0 : fd_bank_epoch_leaders_end_locking_modify( bank );
147 0 : }
148 0 : }
149 :
150 : /******************************************************************************/
151 : /* Various Private Runtime Helpers */
152 : /******************************************************************************/
153 :
154 : /* fee to be deposited should be > 0
155 : Returns 0 if validation succeeds
156 : Returns the amount to burn(==fee) on failure */
157 : static ulong
158 : fd_runtime_validate_fee_collector( fd_bank_t * bank,
159 : fd_txn_account_t const * collector,
160 0 : ulong fee ) {
161 0 : if( FD_UNLIKELY( fee<=0UL ) ) {
162 0 : FD_LOG_ERR(( "expected fee(%lu) to be >0UL", fee ));
163 0 : }
164 :
165 0 : if( FD_UNLIKELY( memcmp( fd_txn_account_get_owner( collector ), fd_solana_system_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
166 0 : FD_BASE58_ENCODE_32_BYTES( collector->pubkey->key, _out_key );
167 0 : FD_LOG_WARNING(( "cannot pay a non-system-program owned account (%s)", _out_key ));
168 0 : return fee;
169 0 : }
170 :
171 : /* https://github.com/anza-xyz/agave/blob/v1.18.23/runtime/src/bank/fee_distribution.rs#L111
172 : https://github.com/anza-xyz/agave/blob/v1.18.23/runtime/src/accounts/account_rent_state.rs#L39
173 : In agave's fee deposit code, rent state transition check logic is as follows:
174 : The transition is NOT allowed iff
175 : === BEGIN
176 : the post deposit account is rent paying AND the pre deposit account is not rent paying
177 : OR
178 : 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)
179 : === END
180 : post_data_size == pre_data_size is always true during fee deposit.
181 : However, post_lamports > pre_lamports because we are paying a >0 amount.
182 : So, the above reduces down to
183 : === BEGIN
184 : the post deposit account is rent paying AND the pre deposit account is not rent paying
185 : OR
186 : the post deposit account is rent paying AND the pre deposit account is rent paying AND TRUE
187 : === END
188 : This is equivalent to checking that the post deposit account is rent paying.
189 : An account is rent paying if the post deposit balance is >0 AND it's not rent exempt.
190 : We already know that the post deposit balance is >0 because we are paying a >0 amount.
191 : So TLDR we just check if the account is rent exempt.
192 : */
193 0 : fd_rent_t const * rent = fd_bank_rent_query( bank );
194 0 : ulong minbal = fd_rent_exempt_minimum_balance( rent, fd_txn_account_get_data_len( collector ) );
195 0 : if( FD_UNLIKELY( fd_txn_account_get_lamports( collector )+fee<minbal ) ) {
196 0 : FD_BASE58_ENCODE_32_BYTES( collector->pubkey->key, _out_key );
197 0 : FD_LOG_WARNING(("cannot pay a rent paying account (%s)", _out_key ));
198 0 : return fee;
199 0 : }
200 :
201 0 : return 0UL;
202 0 : }
203 :
204 : static int
205 : fd_runtime_run_incinerator( fd_bank_t * bank,
206 : fd_accdb_user_t * accdb,
207 : fd_funk_txn_xid_t const * xid,
208 0 : fd_capture_ctx_t * capture_ctx ) {
209 0 : fd_txn_account_t rec[1];
210 0 : fd_funk_rec_prepare_t prepare = {0};
211 :
212 0 : int ok = !!fd_txn_account_init_from_funk_mutable(
213 0 : rec,
214 0 : &fd_sysvar_incinerator_id,
215 0 : accdb,
216 0 : xid,
217 0 : 0,
218 0 : 0UL,
219 0 : &prepare );
220 0 : if( FD_UNLIKELY( !ok ) ) {
221 : // TODO: not really an error! This is fine!
222 0 : return -1;
223 0 : }
224 :
225 0 : fd_lthash_value_t prev_hash[1];
226 0 : fd_hashes_account_lthash( rec->pubkey, fd_txn_account_get_meta( rec ), fd_txn_account_get_data( rec ), prev_hash );
227 :
228 0 : ulong new_capitalization = fd_ulong_sat_sub( fd_bank_capitalization_get( bank ), fd_txn_account_get_lamports( rec ) );
229 0 : fd_bank_capitalization_set( bank, new_capitalization );
230 :
231 0 : fd_txn_account_set_lamports( rec, 0UL );
232 0 : fd_hashes_update_lthash( rec, prev_hash, bank, capture_ctx );
233 0 : fd_txn_account_mutable_fini( rec, accdb, &prepare );
234 :
235 0 : return 0;
236 0 : }
237 :
238 : static void
239 : fd_runtime_freeze( fd_bank_t * bank,
240 : fd_accdb_user_t * accdb,
241 : fd_funk_txn_xid_t const * xid,
242 0 : fd_capture_ctx_t * capture_ctx ) {
243 :
244 0 : if( FD_LIKELY( fd_bank_slot_get( bank ) != 0UL ) ) {
245 0 : fd_sysvar_recent_hashes_update( bank, accdb, xid, capture_ctx );
246 0 : }
247 :
248 0 : fd_sysvar_slot_history_update( bank, accdb, xid, capture_ctx );
249 :
250 0 : ulong execution_fees = fd_bank_execution_fees_get( bank );
251 0 : ulong priority_fees = fd_bank_priority_fees_get( bank );
252 :
253 0 : ulong burn = execution_fees / 2;
254 0 : ulong fees = fd_ulong_sat_add( priority_fees, execution_fees - burn );
255 :
256 0 : if( FD_LIKELY( fees ) ) {
257 : // Look at collect_fees... I think this was where I saw the fee payout..
258 0 : fd_txn_account_t rec[1];
259 :
260 0 : do {
261 : /* do_create=1 because we might wanna pay fees to a leader
262 : account that we've purged due to 0 balance. */
263 :
264 0 : fd_epoch_leaders_t const * leaders = fd_bank_epoch_leaders_locking_query( bank );
265 0 : if( FD_UNLIKELY( !leaders ) ) {
266 0 : FD_LOG_CRIT(( "fd_runtime_freeze: leaders not found" ));
267 0 : fd_bank_epoch_leaders_end_locking_query( bank );
268 0 : break;
269 0 : }
270 :
271 0 : fd_pubkey_t const * leader = fd_epoch_leaders_get( leaders, fd_bank_slot_get( bank ) );
272 0 : if( FD_UNLIKELY( !leader ) ) {
273 0 : FD_LOG_CRIT(( "fd_runtime_freeze: leader not found" ));
274 0 : fd_bank_epoch_leaders_end_locking_query( bank );
275 0 : break;
276 0 : }
277 :
278 0 : fd_funk_rec_prepare_t prepare = {0};
279 0 : int ok = !!fd_txn_account_init_from_funk_mutable(
280 0 : rec,
281 0 : leader,
282 0 : accdb,
283 0 : xid,
284 0 : 1,
285 0 : 0UL,
286 0 : &prepare );
287 0 : if( FD_UNLIKELY( !ok ) ) {
288 0 : FD_LOG_WARNING(( "fd_runtime_freeze: fd_txn_account_init_from_funk_mutable for leader (%s) failed", FD_BASE58_ENC_32_ALLOCA( leader ) ));
289 0 : burn = fd_ulong_sat_add( burn, fees );
290 0 : fd_bank_epoch_leaders_end_locking_query( bank );
291 0 : break;
292 0 : }
293 :
294 0 : fd_lthash_value_t prev_hash[1];
295 0 : fd_hashes_account_lthash( leader, fd_txn_account_get_meta( rec ), fd_txn_account_get_data( rec ), prev_hash );
296 :
297 0 : fd_bank_epoch_leaders_end_locking_query( bank );
298 :
299 0 : if ( FD_LIKELY( FD_FEATURE_ACTIVE_BANK( bank, validate_fee_collector_account ) ) ) {
300 0 : ulong _burn;
301 0 : if( FD_UNLIKELY( _burn=fd_runtime_validate_fee_collector( bank, rec, fees ) ) ) {
302 0 : if( FD_UNLIKELY( _burn!=fees ) ) {
303 0 : FD_LOG_ERR(( "expected _burn(%lu)==fees(%lu)", _burn, fees ));
304 0 : }
305 0 : burn = fd_ulong_sat_add( burn, fees );
306 0 : FD_LOG_WARNING(("fd_runtime_freeze: burned %lu", fees ));
307 0 : break;
308 0 : }
309 0 : }
310 :
311 : /* TODO: is it ok to not check the overflow error here? */
312 0 : fd_txn_account_checked_add_lamports( rec, fees );
313 0 : fd_txn_account_set_slot( rec, fd_bank_slot_get( bank ) );
314 :
315 0 : fd_hashes_update_lthash( rec, prev_hash, bank, capture_ctx );
316 0 : fd_txn_account_mutable_fini( rec, accdb, &prepare );
317 :
318 0 : } while(0);
319 :
320 0 : ulong old = fd_bank_capitalization_get( bank );
321 0 : fd_bank_capitalization_set( bank, fd_ulong_sat_sub( old, burn ) );
322 0 : FD_LOG_DEBUG(( "fd_runtime_freeze: burn %lu, capitalization %lu->%lu ", burn, old, fd_bank_capitalization_get( bank ) ));
323 0 : }
324 :
325 : /* jito collects a 3% fee at the end of the block + 3% fee at
326 : distribution time. */
327 0 : ulong tip = fd_bank_tips_get( bank );
328 0 : tip -= (tip * 6UL / 100UL);
329 0 : fd_bank_tips_set( bank, tip );
330 :
331 0 : fd_runtime_run_incinerator( bank, accdb, xid, capture_ctx );
332 :
333 0 : }
334 :
335 : /******************************************************************************/
336 : /* Block-Level Execution Preparation/Finalization */
337 : /******************************************************************************/
338 :
339 : /*
340 : https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/sdk/program/src/fee_calculator.rs#L105-L165
341 : */
342 : static void
343 : fd_runtime_new_fee_rate_governor_derived( fd_bank_t * bank,
344 0 : ulong latest_signatures_per_slot ) {
345 :
346 0 : fd_fee_rate_governor_t const * base_fee_rate_governor = fd_bank_fee_rate_governor_query( bank );
347 :
348 0 : ulong old_lamports_per_signature = fd_bank_lamports_per_signature_get( bank );
349 :
350 0 : fd_fee_rate_governor_t me = {
351 0 : .target_signatures_per_slot = base_fee_rate_governor->target_signatures_per_slot,
352 0 : .target_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature,
353 0 : .max_lamports_per_signature = base_fee_rate_governor->max_lamports_per_signature,
354 0 : .min_lamports_per_signature = base_fee_rate_governor->min_lamports_per_signature,
355 0 : .burn_percent = base_fee_rate_governor->burn_percent
356 0 : };
357 :
358 0 : ulong new_lamports_per_signature = 0;
359 0 : if( me.target_signatures_per_slot > 0 ) {
360 0 : me.min_lamports_per_signature = fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 2) );
361 0 : me.max_lamports_per_signature = me.target_lamports_per_signature * 10;
362 0 : ulong desired_lamports_per_signature = fd_ulong_min(
363 0 : me.max_lamports_per_signature,
364 0 : fd_ulong_max(
365 0 : me.min_lamports_per_signature,
366 0 : me.target_lamports_per_signature
367 0 : * fd_ulong_min(latest_signatures_per_slot, (ulong)UINT_MAX)
368 0 : / me.target_signatures_per_slot
369 0 : )
370 0 : );
371 0 : long gap = (long)desired_lamports_per_signature - (long)old_lamports_per_signature;
372 0 : if ( gap == 0 ) {
373 0 : new_lamports_per_signature = desired_lamports_per_signature;
374 0 : } else {
375 0 : long gap_adjust = (long)(fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 20) ))
376 0 : * (gap != 0)
377 0 : * (gap > 0 ? 1 : -1);
378 0 : new_lamports_per_signature = fd_ulong_min(
379 0 : me.max_lamports_per_signature,
380 0 : fd_ulong_max(
381 0 : me.min_lamports_per_signature,
382 0 : (ulong)((long)old_lamports_per_signature + gap_adjust)
383 0 : )
384 0 : );
385 0 : }
386 0 : } else {
387 0 : new_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature;
388 0 : me.min_lamports_per_signature = me.target_lamports_per_signature;
389 0 : me.max_lamports_per_signature = me.target_lamports_per_signature;
390 0 : }
391 :
392 0 : if( FD_UNLIKELY( old_lamports_per_signature==0UL ) ) {
393 0 : fd_bank_prev_lamports_per_signature_set( bank, new_lamports_per_signature );
394 0 : } else {
395 0 : fd_bank_prev_lamports_per_signature_set( bank, old_lamports_per_signature );
396 0 : }
397 :
398 0 : fd_bank_fee_rate_governor_set( bank, me );
399 :
400 0 : fd_bank_lamports_per_signature_set( bank, new_lamports_per_signature );
401 0 : }
402 :
403 : static int
404 : fd_runtime_block_sysvar_update_pre_execute( fd_bank_t * bank,
405 : fd_accdb_user_t * accdb,
406 : fd_funk_txn_xid_t const * xid,
407 : fd_runtime_stack_t * runtime_stack,
408 0 : fd_capture_ctx_t * capture_ctx ) {
409 : // let (fee_rate_governor, fee_components_time_us) = measure_us!(
410 : // FeeRateGovernor::new_derived(&parent.fee_rate_governor, parent.signature_count())
411 : // );
412 : /* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/runtime/src/bank.rs#L1312-L1314 */
413 :
414 0 : fd_runtime_new_fee_rate_governor_derived( bank, fd_bank_parent_signature_cnt_get( bank ) );
415 :
416 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
417 0 : ulong parent_epoch = fd_slot_to_epoch( epoch_schedule, fd_bank_parent_slot_get( bank ), NULL );
418 0 : fd_sysvar_clock_update( bank, accdb, xid, capture_ctx, runtime_stack, &parent_epoch );
419 :
420 : // It has to go into the current txn previous info but is not in slot 0
421 0 : if( fd_bank_slot_get( bank ) != 0 ) {
422 0 : fd_sysvar_slot_hashes_update( bank, accdb, xid, capture_ctx );
423 0 : }
424 0 : fd_sysvar_last_restart_slot_update( bank, accdb, xid, capture_ctx, fd_bank_last_restart_slot_get( bank ).slot );
425 :
426 0 : return 0;
427 0 : }
428 :
429 : int
430 : fd_runtime_load_txn_address_lookup_tables(
431 : fd_txn_t const * txn,
432 : uchar const * payload,
433 : fd_funk_t * funk,
434 : fd_funk_txn_xid_t const * xid,
435 : ulong slot,
436 : fd_slot_hash_t const * hashes, /* deque */
437 0 : fd_acct_addr_t * out_accts_alt ) {
438 :
439 0 : if( FD_LIKELY( txn->transaction_version!=FD_TXN_V0 ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
440 :
441 0 : fd_alut_interp_t interp[1];
442 0 : fd_alut_interp_new(
443 0 : interp,
444 0 : out_accts_alt,
445 0 : txn,
446 0 : payload,
447 0 : hashes,
448 0 : slot );
449 :
450 0 : fd_txn_acct_addr_lut_t const * addr_luts = fd_txn_get_address_tables_const( txn );
451 0 : for( ulong i=0UL; i<txn->addr_table_lookup_cnt; i++ ) {
452 0 : fd_txn_acct_addr_lut_t const * addr_lut = &addr_luts[i];
453 0 : fd_pubkey_t addr_lut_acc = FD_LOAD( fd_pubkey_t, payload+addr_lut->addr_off );
454 :
455 : /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/accounts-db/src/accounts.rs#L90-L94 */
456 0 : fd_txn_account_t addr_lut_rec[1];
457 0 : int db_err = fd_txn_account_init_from_funk_readonly(
458 0 : addr_lut_rec, &addr_lut_acc, funk, xid );
459 0 : if( FD_UNLIKELY( db_err!=FD_ACC_MGR_SUCCESS ) ) {
460 0 : return FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND;
461 0 : }
462 :
463 0 : int err = fd_alut_interp_next(
464 0 : interp,
465 0 : &addr_lut_acc,
466 0 : fd_txn_account_get_owner ( addr_lut_rec ),
467 0 : fd_txn_account_get_data ( addr_lut_rec ),
468 0 : fd_txn_account_get_data_len( addr_lut_rec ) );
469 0 : if( FD_UNLIKELY( err ) ) return err;
470 0 : }
471 :
472 0 : fd_alut_interp_delete( interp );
473 :
474 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
475 0 : }
476 :
477 : int
478 : fd_runtime_block_execute_prepare( fd_bank_t * bank,
479 : fd_accdb_user_t * accdb,
480 : fd_funk_txn_xid_t const * xid,
481 : fd_runtime_stack_t * runtime_stack,
482 0 : fd_capture_ctx_t * capture_ctx ) {
483 0 : fd_bank_execution_fees_set( bank, 0UL );
484 0 : fd_bank_priority_fees_set( bank, 0UL );
485 0 : fd_bank_signature_count_set( bank, 0UL );
486 0 : fd_bank_total_compute_units_used_set( bank, 0UL );
487 :
488 0 : if( FD_LIKELY( fd_bank_slot_get( bank ) ) ) {
489 0 : fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_locking_modify( bank );
490 0 : FD_TEST( cost_tracker );
491 0 : fd_cost_tracker_init( cost_tracker, fd_bank_features_query( bank ), fd_bank_slot_get( bank ) );
492 0 : fd_bank_cost_tracker_end_locking_modify( bank );
493 0 : }
494 :
495 0 : int result = fd_runtime_block_sysvar_update_pre_execute( bank, accdb, xid, runtime_stack, capture_ctx );
496 0 : if( FD_UNLIKELY( result != 0 ) ) {
497 0 : FD_LOG_WARNING(("updating sysvars failed"));
498 0 : return result;
499 0 : }
500 :
501 0 : if( FD_UNLIKELY( !fd_sysvar_cache_restore( bank, accdb->funk, xid ) ) ) {
502 0 : FD_LOG_ERR(( "Failed to restore sysvar cache" ));
503 0 : }
504 :
505 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
506 0 : }
507 :
508 : static void
509 : fd_runtime_update_bank_hash( fd_bank_t * bank,
510 : fd_capture_ctx_t * capture_ctx,
511 0 : int silent ) {
512 : /* Save the previous bank hash, and the parents signature count */
513 0 : fd_hash_t const * prev_bank_hash = NULL;
514 0 : if( FD_LIKELY( fd_bank_slot_get( bank )!=0UL ) ) {
515 0 : prev_bank_hash = fd_bank_bank_hash_query( bank );
516 0 : fd_bank_prev_bank_hash_set( bank, *prev_bank_hash );
517 0 : } else {
518 0 : prev_bank_hash = fd_bank_prev_bank_hash_query( bank );
519 0 : }
520 :
521 0 : fd_bank_parent_signature_cnt_set( bank, fd_bank_signature_count_get( bank ) );
522 :
523 : /* Compute the new bank hash */
524 0 : fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
525 0 : fd_hash_t new_bank_hash[1] = { 0 };
526 0 : fd_hashes_hash_bank(
527 0 : lthash,
528 0 : prev_bank_hash,
529 0 : (fd_hash_t *)fd_bank_poh_query( bank )->hash,
530 0 : fd_bank_signature_count_get( bank ),
531 0 : new_bank_hash );
532 :
533 : /* Update the bank hash */
534 0 : fd_bank_bank_hash_set( bank, *new_bank_hash );
535 :
536 0 : if( !silent ) {
537 0 : FD_LOG_NOTICE(( "\n\n[Runtime]\n"
538 0 : "slot: %lu\n"
539 0 : "bank hash: %s\n"
540 0 : "parent bank hash: %s\n"
541 0 : "lthash: %s\n"
542 0 : "signature_count: %lu\n"
543 0 : "last_blockhash: %s\n",
544 0 : fd_bank_slot_get( bank ),
545 0 : FD_BASE58_ENC_32_ALLOCA( new_bank_hash->hash ),
546 0 : FD_BASE58_ENC_32_ALLOCA( fd_bank_prev_bank_hash_query( bank ) ),
547 0 : FD_LTHASH_ENC_32_ALLOCA( lthash->bytes ),
548 0 : fd_bank_signature_count_get( bank ),
549 0 : FD_BASE58_ENC_32_ALLOCA( fd_bank_poh_query( bank )->hash ) ));
550 0 : }
551 :
552 0 : if( capture_ctx != NULL && capture_ctx->capture != NULL &&
553 0 : fd_bank_slot_get( bank )>=capture_ctx->solcap_start_slot ) {
554 :
555 0 : uchar lthash_hash[FD_HASH_FOOTPRINT];
556 0 : fd_blake3_hash(lthash->bytes, FD_LTHASH_LEN_BYTES, lthash_hash );
557 :
558 0 : fd_solcap_write_bank_preimage(
559 0 : capture_ctx->capture,
560 0 : new_bank_hash->hash,
561 0 : fd_bank_prev_bank_hash_query( bank ),
562 0 : NULL,
563 0 : lthash_hash,
564 0 : fd_bank_poh_query( bank )->hash,
565 0 : fd_bank_signature_count_get( bank ) );
566 0 : }
567 :
568 0 : fd_bank_lthash_end_locking_query( bank );
569 0 : }
570 :
571 : /******************************************************************************/
572 : /* Transaction Level Execution Management */
573 : /******************************************************************************/
574 :
575 : /* fd_runtime_pre_execute_check is responsible for conducting many of the
576 : transaction sanitization checks. */
577 :
578 : int
579 0 : fd_runtime_pre_execute_check( fd_exec_txn_ctx_t * txn_ctx ) {
580 :
581 0 : int err;
582 :
583 : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/sdk/src/transaction/sanitized.rs#L263-L275
584 : TODO: Agave's precompile verification is done at the slot level, before batching and executing transactions. This logic should probably
585 : be moved in the future. The Agave call heirarchy looks something like this:
586 : process_single_slot
587 : v
588 : confirm_full_slot
589 : v
590 : confirm_slot_entries --------------------------------------------------->
591 : v v v
592 : verify_transaction ComputeBudget::process_instruction process_entries
593 : v v
594 : verify_precompiles process_batches
595 : v
596 : ...
597 : v
598 : load_and_execute_transactions
599 : v
600 : ...
601 : v
602 : load_accounts --> load_transaction_accounts
603 : v
604 : general transaction execution
605 :
606 : */
607 :
608 0 : uchar dump_txn = !!( txn_ctx->capture_ctx &&
609 0 : txn_ctx->slot >= txn_ctx->capture_ctx->dump_proto_start_slot &&
610 0 : txn_ctx->capture_ctx->dump_txn_to_pb );
611 0 : if( FD_UNLIKELY( dump_txn ) ) {
612 0 : fd_dump_txn_to_protobuf( txn_ctx );
613 0 : }
614 :
615 : /* Verify the transaction. For now, this step only involves processing
616 : the compute budget instructions. */
617 0 : err = fd_executor_verify_transaction( txn_ctx );
618 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
619 0 : txn_ctx->flags = 0U;
620 0 : return err;
621 0 : }
622 :
623 : /* Resolve and verify ALUT-referenced account keys, if applicable */
624 0 : err = fd_executor_setup_txn_alut_account_keys( txn_ctx );
625 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
626 0 : txn_ctx->flags = 0U;
627 0 : return err;
628 0 : }
629 :
630 : /* Set up the transaction accounts and other txn ctx metadata */
631 0 : fd_executor_setup_accounts_for_txn( txn_ctx );
632 :
633 : /* Post-sanitization checks. Called from prepare_sanitized_batch()
634 : which, for now, only is used to lock the accounts and perform a
635 : couple basic validations.
636 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 */
637 0 : err = fd_executor_validate_account_locks( txn_ctx );
638 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
639 0 : txn_ctx->flags = 0U;
640 0 : return err;
641 0 : }
642 :
643 : /* load_and_execute_transactions() -> check_transactions()
644 : https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3667-L3672 */
645 0 : err = fd_executor_check_transactions( txn_ctx );
646 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
647 0 : txn_ctx->flags = 0U;
648 0 : return err;
649 0 : }
650 :
651 : /* load_and_execute_sanitized_transactions() -> validate_fees() ->
652 : validate_transaction_fee_payer()
653 : https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L236-L249 */
654 0 : err = fd_executor_validate_transaction_fee_payer( txn_ctx );
655 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
656 0 : txn_ctx->flags = 0U;
657 0 : return err;
658 0 : }
659 :
660 : /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L284-L296 */
661 0 : err = fd_executor_load_transaction_accounts( txn_ctx );
662 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
663 : /* Regardless of whether transaction accounts were loaded successfully, the transaction is
664 : included in the block and transaction fees are collected.
665 : https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/transaction_processor.rs#L341-L357 */
666 0 : txn_ctx->flags |= FD_TXN_P_FLAGS_FEES_ONLY;
667 :
668 : /* If the transaction fails to load, the "rollback" accounts will include one of the following:
669 : 1. Nonce account only
670 : 2. Fee payer only
671 : 3. Nonce account + fee payer
672 :
673 : Because the cost tracker uses the loaded account data size in block cost calculations, we need to
674 : make sure our calculated loaded accounts data size is conformant with Agave's.
675 : https://github.com/anza-xyz/agave/blob/v2.1.14/runtime/src/bank.rs#L4116
676 :
677 : In any case, we should always add the dlen of the fee payer. */
678 0 : txn_ctx->loaded_accounts_data_size = fd_txn_account_get_data_len( &txn_ctx->accounts[FD_FEE_PAYER_TXN_IDX] );
679 :
680 : /* Special case handling for if a nonce account is present in the transaction. */
681 0 : if( txn_ctx->nonce_account_idx_in_txn!=ULONG_MAX ) {
682 : /* If the nonce account is not the fee payer, then we separately add the dlen of the nonce account. Otherwise, we would
683 : be double counting the dlen of the fee payer. */
684 0 : if( txn_ctx->nonce_account_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) {
685 0 : txn_ctx->loaded_accounts_data_size += fd_txn_account_get_data_len( txn_ctx->rollback_nonce_account );
686 0 : }
687 0 : }
688 0 : }
689 :
690 : /*
691 : The fee payer and the nonce account will be stored and hashed so
692 : long as the transaction landed on chain, or, in Agave terminology,
693 : the transaction was processed.
694 : https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/account_saver.rs#L72
695 :
696 : A transaction lands on chain in one of two ways:
697 : (1) Passed fee validation and loaded accounts.
698 : (2) Passed fee validation and failed to load accounts and the enable_transaction_loading_failure_fees feature is enabled as per
699 : SIMD-0082 https://github.com/anza-xyz/feature-gate-tracker/issues/52
700 :
701 : So, at this point, the transaction is committable.
702 : */
703 :
704 0 : return err;
705 0 : }
706 :
707 : /* fd_runtime_finalize_account is a helper used to commit the data from
708 : a writable transaction account back into the accountsdb. */
709 :
710 : static void
711 : fd_runtime_finalize_account( fd_funk_t * funk,
712 : fd_funk_txn_xid_t const * xid,
713 : fd_txn_account_t * acc,
714 0 : fd_funk_rec_t * prev_rec ) {
715 0 : if( FD_UNLIKELY( !fd_txn_account_is_mutable( acc ) ) ) {
716 0 : FD_LOG_CRIT(( "fd_runtime_finalize_account: account is not mutable" ));
717 0 : }
718 :
719 0 : fd_pubkey_t const * key = acc->pubkey;
720 0 : uchar const * record_data = (uchar *)fd_txn_account_get_meta( acc );
721 0 : ulong record_sz = fd_account_meta_get_record_sz( acc->meta );
722 :
723 0 : int err = FD_FUNK_SUCCESS;
724 :
725 0 : if( !prev_rec || !fd_funk_txn_xid_eq( prev_rec->pair.xid, xid ) ) {
726 :
727 0 : fd_funk_rec_key_t funk_key = fd_funk_acc_key( key );
728 0 : fd_funk_rec_prepare_t prepare[1];
729 0 : fd_funk_rec_t * rec = fd_funk_rec_prepare( funk, xid, &funk_key, prepare, &err );
730 0 : if( FD_UNLIKELY( !rec || err!=FD_FUNK_SUCCESS ) ) {
731 0 : FD_LOG_ERR(( "fd_runtime_finalize_account: failed to prepare record (%i-%s)", err, fd_funk_strerror( err ) ));
732 0 : }
733 :
734 0 : if( FD_UNLIKELY( !fd_funk_val_truncate(
735 0 : rec,
736 0 : fd_funk_alloc( funk ),
737 0 : fd_funk_wksp( funk ),
738 0 : 0UL,
739 0 : record_sz,
740 0 : &err ) ) ) {
741 0 : FD_LOG_ERR(( "fd_funk_val_truncate(sz=%lu) for account failed (%i-%s)", record_sz, err, fd_funk_strerror( err ) ));
742 0 : }
743 :
744 0 : fd_memcpy( fd_funk_val( rec, fd_funk_wksp( funk ) ), record_data, record_sz );
745 :
746 0 : fd_funk_rec_publish( funk, prepare );
747 :
748 0 : } else {
749 :
750 0 : if( FD_UNLIKELY( !fd_funk_val_truncate(
751 0 : prev_rec,
752 0 : fd_funk_alloc( funk ),
753 0 : fd_funk_wksp( funk ),
754 0 : 0UL,
755 0 : record_sz,
756 0 : &err ) ) ) {
757 0 : FD_LOG_ERR(( "fd_funk_val_truncate(sz=%lu) for account failed (%i-%s)", record_sz, err, fd_funk_strerror( err ) ));
758 0 : }
759 :
760 0 : fd_memcpy( fd_funk_val( prev_rec, fd_funk_wksp( funk ) ), record_data, record_sz );
761 :
762 0 : }
763 :
764 0 : }
765 :
766 : /* fd_runtime_buffer_solcap_account_update buffers an account
767 : update event message in the capture context, which will be
768 : sent to the replay tile via the exec_replay link.
769 : This buffering is done to avoid passing stem down into the runtime.
770 :
771 : TODO: remove this when solcap v2 is here. */
772 : static void
773 : fd_runtime_buffer_solcap_account_update( fd_txn_account_t * account,
774 : fd_bank_t * bank,
775 0 : fd_capture_ctx_t * capture_ctx ) {
776 :
777 : /* Check if we should publish the update */
778 0 : if( FD_UNLIKELY( !capture_ctx || fd_bank_slot_get( bank )<capture_ctx->solcap_start_slot ) ) {
779 0 : return;
780 0 : }
781 :
782 : /* Get account data */
783 0 : fd_account_meta_t const * meta = fd_txn_account_get_meta( account );
784 0 : void const * data = fd_txn_account_get_data( account );
785 :
786 : /* Calculate account hash using lthash */
787 0 : fd_lthash_value_t lthash[1];
788 0 : fd_hashes_account_lthash( account->pubkey, meta, data, lthash );
789 :
790 : /* Calculate message size */
791 0 : if( FD_UNLIKELY( capture_ctx->account_updates_len >= FD_CAPTURE_CTX_MAX_ACCOUNT_UPDATES ) ) {
792 0 : FD_LOG_CRIT(( "cannot buffer solcap account update. this should never happen" ));
793 0 : return;
794 0 : }
795 :
796 : /* Write the message to the buffer */
797 0 : fd_capture_ctx_account_update_msg_t * account_update_msg = (fd_capture_ctx_account_update_msg_t *)(capture_ctx->account_updates_buffer_ptr);
798 0 : account_update_msg->pubkey = *account->pubkey;
799 0 : account_update_msg->info = fd_txn_account_get_solana_meta( account );
800 0 : account_update_msg->data_sz = meta->dlen;
801 0 : account_update_msg->bank_idx = bank->idx;
802 0 : memcpy( account_update_msg->hash.uc, lthash->bytes, sizeof(fd_hash_t) );
803 0 : capture_ctx->account_updates_buffer_ptr += sizeof(fd_capture_ctx_account_update_msg_t);
804 :
805 : /* Write the account data to the buffer */
806 0 : memcpy( capture_ctx->account_updates_buffer_ptr, data, meta->dlen );
807 0 : capture_ctx->account_updates_buffer_ptr += meta->dlen;
808 :
809 0 : capture_ctx->account_updates_len++;
810 0 : }
811 :
812 : /* fd_runtime_save_account is a convenience wrapper that looks
813 : up the previous account state from funk before updating the lthash
814 : and saving the new version of the account to funk.
815 :
816 : TODO: We have to make a read request to the DB, so that we can calculate
817 : the previous version of the accounts hash, to mix-out from the accumulated
818 : lthash. In future we should likely cache the previous version of the account
819 : in transaction setup, so that we don't have to issue a read request here.
820 :
821 : funk is the funk database handle. funk_txn is the transaction
822 : context to query (NULL for root context). account is the modified
823 : account. bank and capture_ctx are passed to fd_hashes_update_lthash.
824 :
825 : This function:
826 : - Queries funk for the previous account version
827 : - Computes the hash of the previous version (or uses zero for new)
828 : - Calls fd_hashes_update_lthash with the computed previous hash
829 : - Saves the new version of the account to Funk
830 : - Notifies the replay tile that an account update has occurred, so it
831 : can write the account to the solcap file.
832 :
833 : The function handles FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT gracefully (uses
834 : zero hash). On other funk errors, the function will FD_LOG_ERR.
835 : All non-optional pointers must be valid. */
836 :
837 : static void
838 : fd_runtime_save_account( fd_funk_t * funk,
839 : fd_funk_txn_xid_t const * xid,
840 : fd_txn_account_t * account,
841 : fd_bank_t * bank,
842 0 : fd_capture_ctx_t * capture_ctx ) {
843 : /* Join the transaction account */
844 0 : if( FD_UNLIKELY( !fd_txn_account_join( account ) ) ) {
845 0 : FD_LOG_CRIT(( "fd_runtime_save_account: failed to join account" ));
846 0 : }
847 :
848 : /* Look up the previous version of the account from Funk */
849 0 : int err = FD_ACC_MGR_SUCCESS;
850 0 : fd_funk_rec_t * funk_prev_rec = NULL;
851 0 : fd_account_meta_t const * prev_meta = fd_funk_get_acc_meta_readonly(
852 0 : funk,
853 0 : xid,
854 0 : account->pubkey,
855 0 : fd_type_pun( &funk_prev_rec ),
856 0 : &err,
857 0 : NULL );
858 0 : uchar const * prev_data = (void const *)( prev_meta+1 );
859 :
860 : /* Hash the old version of the account */
861 0 : fd_lthash_value_t prev_hash[1];
862 0 : fd_lthash_zero( prev_hash );
863 0 : if( err != FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) {
864 0 : fd_hashes_account_lthash(
865 0 : account->pubkey,
866 0 : prev_meta,
867 0 : prev_data,
868 0 : prev_hash );
869 0 : }
870 :
871 : /* Mix in the account hash into the bank hash */
872 0 : fd_hashes_update_lthash( account, prev_hash, bank, NULL );
873 :
874 : /* Publish account update to replay tile for solcap writing
875 : TODO: write in the exec tile with solcap v2 */
876 0 : fd_runtime_buffer_solcap_account_update( account, bank, capture_ctx );
877 :
878 : /* Save the new version of the account to Funk */
879 0 : fd_runtime_finalize_account( funk, xid, account, funk_prev_rec );
880 0 : }
881 :
882 : /* fd_runtime_finalize_txn is a helper used by the non-tpool transaction
883 : executor to finalize borrowed account changes back into funk. It also
884 : handles txncache insertion and updates to the vote/stake cache.
885 : TODO: This function should probably be moved to fd_executor.c. */
886 :
887 : void
888 : fd_runtime_finalize_txn( fd_funk_t * funk,
889 : fd_progcache_t * progcache,
890 : fd_txncache_t * txncache,
891 : fd_funk_txn_xid_t const * xid,
892 : fd_exec_txn_ctx_t * txn_ctx,
893 : fd_bank_t * bank,
894 : fd_capture_ctx_t * capture_ctx,
895 0 : ulong * tips_out_opt ) {
896 :
897 : /* Collect fees */
898 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_txn_count_modify( bank ), 1UL );
899 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_execution_fees_modify( bank ), txn_ctx->execution_fee );
900 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_priority_fees_modify( bank ), txn_ctx->priority_fee );
901 :
902 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_signature_count_modify( bank ), TXN( &txn_ctx->txn )->signature_cnt );
903 :
904 0 : if( FD_UNLIKELY( txn_ctx->exec_err ) ) {
905 :
906 : /* Save the fee_payer. Everything but the fee balance should be reset.
907 : TODO: an optimization here could be to use a dirty flag in the
908 : borrowed account. If the borrowed account data has been changed in
909 : any way, then the full account can be rolled back as it is done now.
910 : However, most of the time the account data is not changed, and only
911 : the lamport balance has to change. */
912 :
913 : /* With nonce account rollbacks, there are three cases:
914 : 1. No nonce account in the transaction
915 : 2. Nonce account is the fee payer
916 : 3. Nonce account is not the fee payer
917 :
918 : We should always rollback the nonce account first. Note that the nonce account may be the fee payer (case 2). */
919 0 : if( txn_ctx->nonce_account_idx_in_txn!=ULONG_MAX ) {
920 0 : fd_runtime_save_account( funk, xid, txn_ctx->rollback_nonce_account, bank, capture_ctx );
921 0 : }
922 :
923 : /* Now, we must only save the fee payer if the nonce account was not the fee payer (because that was already saved above) */
924 0 : if( FD_LIKELY( txn_ctx->nonce_account_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
925 0 : fd_runtime_save_account( funk, xid, txn_ctx->rollback_fee_payer_account, bank, capture_ctx );
926 0 : }
927 0 : } else {
928 :
929 0 : fd_pubkey_t const * tip_accounts = NULL;
930 0 : if( fd_bank_cluster_type_get( bank )==FD_CLUSTER_TESTNET ) {
931 0 : tip_accounts = TESTNET_TIP_ACCOUNTS;
932 0 : } else if( fd_bank_cluster_type_get( bank )==FD_CLUSTER_MAINNET_BETA ) {
933 0 : tip_accounts = MAINNET_TIP_ACCOUNTS;
934 0 : }
935 :
936 0 : for( ushort i=0; i<txn_ctx->accounts_cnt; i++ ) {
937 : /* We are only interested in saving writable accounts and the fee
938 : payer account. */
939 0 : if( !fd_exec_txn_ctx_account_is_writable_idx( txn_ctx, i ) && i!=FD_FEE_PAYER_TXN_IDX ) {
940 0 : continue;
941 0 : }
942 :
943 0 : fd_txn_account_t * acc_rec = fd_txn_account_join( &txn_ctx->accounts[i] );
944 0 : if( FD_UNLIKELY( !acc_rec ) ) {
945 0 : FD_LOG_CRIT(( "fd_runtime_finalize_txn: failed to join account at idx %u", i ));
946 0 : }
947 :
948 : /* Tips for bundles are collected in the bank: a user submitting a
949 : bundle must include a instruction that transfers lamports to
950 : a specific tip account. Tips accumulated through the slot. */
951 0 : if( tip_accounts ) {
952 0 : for( ulong j=0UL; j<FD_TIP_ACCOUNTS_CNT; j++ ) {
953 0 : if( 0==memcmp( acc_rec->pubkey, &tip_accounts[j], sizeof(fd_pubkey_t) ) ) {
954 0 : ulong tip = fd_ulong_sat_sub( acc_rec->meta->lamports, acc_rec->starting_lamports );
955 0 : if( !!tips_out_opt ) *tips_out_opt += tip;
956 0 : FD_ATOMIC_FETCH_AND_ADD( fd_bank_tips_modify( bank ), tip );
957 0 : break;
958 0 : }
959 0 : }
960 0 : }
961 :
962 0 : if( 0==memcmp( fd_txn_account_get_owner( acc_rec ), &fd_solana_vote_program_id, sizeof(fd_pubkey_t) ) ) {
963 0 : fd_stakes_update_vote_state( acc_rec, bank );
964 0 : }
965 :
966 0 : if( 0==memcmp( fd_txn_account_get_owner( acc_rec ), &fd_solana_stake_program_id, sizeof(fd_pubkey_t) ) ) {
967 0 : fd_stakes_update_stake_delegation( acc_rec, bank );
968 0 : }
969 :
970 : /* Reclaim any accounts that have 0-lamports, now that any related
971 : cache updates have been applied. */
972 0 : fd_executor_reclaim_account( txn_ctx, &txn_ctx->accounts[i] );
973 :
974 0 : fd_runtime_save_account( funk, xid, &txn_ctx->accounts[i], bank, capture_ctx );
975 0 : }
976 :
977 : /* We need to queue any existing program accounts that may have
978 : been deployed / upgraded for reverification in the program
979 : cache since their programdata may have changed. ELF / sBPF
980 : metadata will need to be updated. */
981 0 : ulong current_slot = fd_bank_slot_get( bank );
982 0 : for( uchar i=0; i<txn_ctx->programs_to_reverify_cnt; i++ ) {
983 0 : fd_pubkey_t const * program_key = &txn_ctx->programs_to_reverify[i];
984 0 : fd_progcache_invalidate( progcache, xid, program_key, current_slot );
985 0 : }
986 0 : }
987 :
988 0 : int is_vote = fd_txn_is_simple_vote_transaction( TXN( &txn_ctx->txn ), txn_ctx->txn.payload );
989 0 : if( !is_vote ){
990 0 : ulong * nonvote_txn_count = fd_bank_nonvote_txn_count_modify( bank );
991 0 : FD_ATOMIC_FETCH_AND_ADD(nonvote_txn_count, 1);
992 :
993 0 : if( FD_UNLIKELY( txn_ctx->exec_err ) ){
994 0 : ulong * nonvote_failed_txn_count = fd_bank_nonvote_failed_txn_count_modify( bank );
995 0 : FD_ATOMIC_FETCH_AND_ADD( nonvote_failed_txn_count, 1 );
996 0 : }
997 0 : }
998 :
999 0 : if( FD_UNLIKELY( txn_ctx->exec_err ) ){
1000 0 : ulong * failed_txn_count = fd_bank_failed_txn_count_modify( bank );
1001 0 : FD_ATOMIC_FETCH_AND_ADD( failed_txn_count, 1 );
1002 0 : }
1003 :
1004 0 : ulong * total_compute_units_used = fd_bank_total_compute_units_used_modify( bank );
1005 0 : FD_ATOMIC_FETCH_AND_ADD( total_compute_units_used, txn_ctx->compute_budget_details.compute_unit_limit - txn_ctx->compute_budget_details.compute_meter );
1006 :
1007 : /* Update the cost tracker */
1008 0 : fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_locking_modify( bank );
1009 0 : int res = fd_cost_tracker_calculate_cost_and_add( cost_tracker, txn_ctx );
1010 0 : if( FD_UNLIKELY( res!=FD_COST_TRACKER_SUCCESS ) ) {
1011 0 : txn_ctx->flags = 0U;
1012 0 : }
1013 0 : fd_bank_cost_tracker_end_locking_modify( bank );
1014 :
1015 0 : txn_ctx->loaded_accounts_data_size_cost = fd_cost_tracker_calculate_loaded_accounts_data_size_cost( txn_ctx );
1016 :
1017 0 : if( FD_LIKELY( txncache && txn_ctx->nonce_account_idx_in_txn==ULONG_MAX ) ) {
1018 : /* In Agave, durable nonce transactions are inserted to the status
1019 : cache the same as any others, but this is only to serve RPC
1020 : requests, they do not need to be in there for correctness as the
1021 : nonce mechanism itself prevents double spend. We skip this logic
1022 : entirely to simplify and improve performance of the txn cache. */
1023 :
1024 0 : fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_ctx->txn.payload + TXN( &txn_ctx->txn )->recent_blockhash_off);
1025 0 : fd_txncache_insert( txncache, bank->txncache_fork_id, blockhash->uc, txn_ctx->blake_txn_msg_hash.uc );
1026 0 : }
1027 0 : }
1028 :
1029 : int
1030 : fd_runtime_prepare_and_execute_txn( fd_banks_t * banks,
1031 : ulong bank_idx,
1032 : fd_exec_txn_ctx_t * txn_ctx,
1033 : fd_txn_p_t * txn,
1034 : fd_capture_ctx_t * capture_ctx,
1035 : fd_exec_stack_t * exec_stack,
1036 : fd_exec_accounts_t * exec_accounts,
1037 : uchar * dumping_mem,
1038 0 : uchar * tracing_mem ) {
1039 0 : int exec_res = 0;
1040 :
1041 0 : fd_bank_t * bank = fd_banks_bank_query( banks, bank_idx );
1042 0 : if( FD_UNLIKELY( !bank ) ) {
1043 0 : FD_LOG_CRIT(( "Could not get bank at pool idx %lu", bank_idx ));
1044 0 : }
1045 :
1046 0 : ulong slot = fd_bank_slot_get( bank );
1047 :
1048 : /* Setup and execute the transaction. */
1049 0 : txn_ctx->bank = bank;
1050 0 : txn_ctx->slot = fd_bank_slot_get( bank );
1051 0 : txn_ctx->bank_idx = bank_idx;
1052 0 : txn_ctx->features = fd_bank_features_get( bank );
1053 0 : txn_ctx->enable_exec_recording = !!( bank->flags & FD_BANK_FLAGS_EXEC_RECORDING );
1054 0 : txn_ctx->xid[0] = (fd_funk_txn_xid_t){ .ul = { slot, bank_idx } };
1055 0 : txn_ctx->txn = *txn;
1056 0 : txn_ctx->exec_stack = exec_stack;
1057 0 : txn_ctx->exec_accounts = exec_accounts;
1058 0 : txn_ctx->dumping_mem = dumping_mem;
1059 0 : txn_ctx->tracing_mem = tracing_mem;
1060 0 : txn_ctx->flags = FD_TXN_P_FLAGS_SANITIZE_SUCCESS;
1061 0 : fd_exec_txn_ctx_setup_basic( txn_ctx );
1062 0 : txn_ctx->capture_ctx = capture_ctx;
1063 :
1064 : /* Set up the core account keys. These are the account keys directly
1065 : passed in via the serialized transaction, represented as an array.
1066 : Note that this does not include additional keys referenced in
1067 : address lookup tables. */
1068 0 : fd_executor_setup_txn_account_keys( txn_ctx );
1069 :
1070 : /* Pre-execution checks */
1071 0 : exec_res = fd_runtime_pre_execute_check( txn_ctx );
1072 0 : if( FD_UNLIKELY( !( txn_ctx->flags & FD_TXN_P_FLAGS_SANITIZE_SUCCESS ) ) ) {
1073 0 : return exec_res;
1074 0 : }
1075 :
1076 : /* Execute the transaction. Note that fees-only transactions are still
1077 : marked as "executed". */
1078 0 : txn_ctx->flags |= FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
1079 0 : if( FD_LIKELY( !( txn_ctx->flags & FD_TXN_P_FLAGS_FEES_ONLY ) ) ) {
1080 0 : exec_res = fd_execute_txn( txn_ctx );
1081 0 : }
1082 :
1083 0 : return exec_res;
1084 0 : }
1085 :
1086 : /* fd_executor_txn_verify and fd_runtime_pre_execute_check are responisble
1087 : for the bulk of the pre-transaction execution checks in the runtime.
1088 : They aim to preserve the ordering present in the Agave client to match
1089 : parity in terms of error codes. Sigverify is kept separate from the rest
1090 : of the transaction checks for fuzzing convenience.
1091 :
1092 : For reference this is the general code path which contains all relevant
1093 : pre-transactions checks in the v2.0.x Agave client from upstream
1094 : to downstream is as follows:
1095 :
1096 : confirm_slot_entries() which calls verify_ticks() and
1097 : verify_transaction(). verify_transaction() calls verify_and_hash_message()
1098 : and verify_precompiles() which parallels fd_executor_txn_verify() and
1099 : fd_executor_verify_transaction().
1100 :
1101 : process_entries() contains a duplicate account check which is part of
1102 : agave account lock acquiring. This is checked inline in
1103 : fd_runtime_pre_execute_check().
1104 :
1105 : load_and_execute_transactions() contains the function check_transactions().
1106 : This contains check_age() and check_status_cache() which is paralleled by
1107 : fd_executor_check_transaction_age_and_compute_budget_limits() and
1108 : fd_executor_check_status_cache() respectively.
1109 :
1110 : load_and_execute_sanitized_transactions() contains validate_fees()
1111 : which is responsible for executing the compute budget instructions,
1112 : validating the fee payer and collecting the fee. This is mirrored in
1113 : firedancer with fd_executor_compute_budget_program_execute_instructions()
1114 : and fd_executor_collect_fees(). load_and_execute_sanitized_transactions()
1115 : also checks the total data size of the accounts in load_accounts() and
1116 : validates the program accounts in load_transaction_accounts(). This
1117 : is paralled by fd_executor_load_transaction_accounts(). */
1118 :
1119 : /******************************************************************************/
1120 : /* Epoch Boundary */
1121 : /******************************************************************************/
1122 :
1123 : /* Replace the vote states for T-2 (vote_states_prev_prev) with the vote
1124 : states for T-1 (vote_states_prev) */
1125 :
1126 : static void
1127 0 : fd_update_vote_states_prev_prev( fd_bank_t * bank ) {
1128 :
1129 0 : fd_vote_states_t * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_modify( bank );
1130 0 : fd_vote_states_t const * vote_states_prev = fd_bank_vote_states_prev_locking_query( bank );
1131 0 : fd_memcpy( vote_states_prev_prev, vote_states_prev, fd_bank_vote_states_footprint );
1132 0 : fd_bank_vote_states_prev_prev_end_locking_modify( bank );
1133 0 : fd_bank_vote_states_prev_end_locking_query( bank );
1134 0 : }
1135 :
1136 : /* Replace the vote states for T-1 (vote_states_prev) with the vote
1137 : states for T-1 (vote_states) */
1138 :
1139 : static void
1140 0 : fd_update_vote_states_prev( fd_bank_t * bank ) {
1141 0 : fd_vote_states_t * vote_states_prev = fd_bank_vote_states_prev_locking_modify( bank );
1142 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( bank );
1143 0 : fd_memcpy( vote_states_prev, vote_states, fd_bank_vote_states_footprint );
1144 0 : fd_bank_vote_states_prev_end_locking_modify( bank );
1145 0 : fd_bank_vote_states_end_locking_query( bank );
1146 0 : }
1147 :
1148 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6704 */
1149 : static void
1150 : fd_apply_builtin_program_feature_transitions( fd_bank_t * bank,
1151 : fd_accdb_user_t * accdb,
1152 : fd_funk_txn_xid_t const * xid,
1153 : fd_runtime_stack_t * runtime_stack,
1154 0 : fd_capture_ctx_t * capture_ctx ) {
1155 : /* TODO: Set the upgrade authority properly from the core bpf migration config. Right now it's set to None.
1156 :
1157 : Migrate any necessary stateless builtins to core BPF. So far,
1158 : the only "stateless" builtin is the Feature program. Beginning
1159 : checks in the migrate_builtin_to_core_bpf function will fail if the
1160 : program has already been migrated to BPF. */
1161 :
1162 0 : fd_builtin_program_t const * builtins = fd_builtins();
1163 0 : for( ulong i=0UL; i<fd_num_builtins(); i++ ) {
1164 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6732-L6751 */
1165 0 : if( builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_get( bank ), builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
1166 0 : FD_LOG_DEBUG(( "Migrating builtin program %s to core BPF", FD_BASE58_ENC_32_ALLOCA( builtins[i].pubkey->key ) ));
1167 0 : fd_migrate_builtin_to_core_bpf( bank, accdb, xid, runtime_stack, builtins[i].core_bpf_migration_config, capture_ctx );
1168 0 : }
1169 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6753-L6774 */
1170 0 : if( builtins[i].enable_feature_offset!=NO_ENABLE_FEATURE_ID && FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, builtins[i].enable_feature_offset ) ) {
1171 0 : FD_LOG_DEBUG(( "Enabling builtin program %s", FD_BASE58_ENC_32_ALLOCA( builtins[i].pubkey->key ) ));
1172 0 : fd_write_builtin_account( bank, accdb, xid, capture_ctx, *builtins[i].pubkey, builtins[i].data,strlen(builtins[i].data) );
1173 0 : }
1174 0 : }
1175 :
1176 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6776-L6793 */
1177 0 : fd_stateless_builtin_program_t const * stateless_builtins = fd_stateless_builtins();
1178 0 : for( ulong i=0UL; i<fd_num_stateless_builtins(); i++ ) {
1179 0 : if( stateless_builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_get( bank ), stateless_builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
1180 0 : FD_LOG_DEBUG(( "Migrating stateless builtin program %s to core BPF", FD_BASE58_ENC_32_ALLOCA( stateless_builtins[i].pubkey->key ) ));
1181 0 : fd_migrate_builtin_to_core_bpf( bank, accdb, xid, runtime_stack, stateless_builtins[i].core_bpf_migration_config, capture_ctx );
1182 0 : }
1183 0 : }
1184 :
1185 : /* https://github.com/anza-xyz/agave/blob/c1080de464cfb578c301e975f498964b5d5313db/runtime/src/bank.rs#L6795-L6805 */
1186 0 : fd_precompile_program_t const * precompiles = fd_precompiles();
1187 0 : for( ulong i=0UL; i<fd_num_precompiles(); i++ ) {
1188 0 : if( precompiles[i].feature_offset != NO_ENABLE_FEATURE_ID && FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, precompiles[i].feature_offset ) ) {
1189 0 : fd_write_builtin_account( bank, accdb, xid, capture_ctx, *precompiles[i].pubkey, "", 0 );
1190 0 : }
1191 0 : }
1192 0 : }
1193 :
1194 : static void
1195 : fd_feature_activate( fd_bank_t * bank,
1196 : fd_accdb_user_t * accdb,
1197 : fd_funk_txn_xid_t const * xid,
1198 : fd_capture_ctx_t * capture_ctx,
1199 : fd_feature_id_t const * id,
1200 0 : fd_pubkey_t const * addr ) {
1201 0 : fd_features_t * features = fd_bank_features_modify( bank );
1202 :
1203 0 : if( id->reverted==1 ) return;
1204 :
1205 0 : fd_txn_account_t acct_rec[1];
1206 0 : int err = fd_txn_account_init_from_funk_readonly( acct_rec, addr, accdb->funk, xid );
1207 0 : if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) ) {
1208 0 : return;
1209 0 : }
1210 :
1211 0 : FD_BASE58_ENCODE_32_BYTES( addr->uc, addr_b58 );
1212 0 : fd_feature_t feature[1];
1213 0 : int decode_err = 0;
1214 0 : if( FD_UNLIKELY( !fd_bincode_decode_static( feature, feature, fd_txn_account_get_data( acct_rec ), fd_txn_account_get_data_len( acct_rec ), &decode_err ) ) ) {
1215 0 : FD_LOG_WARNING(( "Failed to decode feature account %s (%d)", addr_b58, decode_err ));
1216 0 : return;
1217 0 : }
1218 :
1219 0 : if( feature->has_activated_at ) {
1220 0 : FD_LOG_DEBUG(( "feature already activated - acc: %s, slot: %lu", addr_b58, feature->activated_at ));
1221 0 : fd_features_set( features, id, feature->activated_at);
1222 0 : } else {
1223 0 : FD_LOG_DEBUG(( "Feature %s not activated at %lu, activating", addr_b58, feature->activated_at ));
1224 :
1225 0 : fd_txn_account_t modify_acct_rec[1];
1226 0 : fd_funk_rec_prepare_t modify_acct_prepare = {0};
1227 0 : int ok = !!fd_txn_account_init_from_funk_mutable( modify_acct_rec, addr, accdb, xid, 0, 0UL, &modify_acct_prepare );
1228 0 : if( FD_UNLIKELY( !ok ) ) return;
1229 :
1230 0 : fd_lthash_value_t prev_hash[1];
1231 0 : fd_hashes_account_lthash(
1232 0 : addr,
1233 0 : fd_txn_account_get_meta( modify_acct_rec ),
1234 0 : fd_txn_account_get_data( modify_acct_rec ),
1235 0 : prev_hash );
1236 :
1237 0 : feature->has_activated_at = 1;
1238 0 : feature->activated_at = fd_bank_slot_get( bank );
1239 0 : fd_bincode_encode_ctx_t encode_ctx = {
1240 0 : .data = fd_txn_account_get_data_mut( modify_acct_rec ),
1241 0 : .dataend = fd_txn_account_get_data_mut( modify_acct_rec ) + fd_txn_account_get_data_len( modify_acct_rec ),
1242 0 : };
1243 0 : int encode_err = fd_feature_encode( feature, &encode_ctx );
1244 0 : if( FD_UNLIKELY( encode_err != FD_BINCODE_SUCCESS ) ) {
1245 0 : FD_LOG_ERR(( "Failed to encode feature account %s (%d)", addr_b58, decode_err ));
1246 0 : }
1247 :
1248 0 : fd_hashes_update_lthash( modify_acct_rec, prev_hash, bank, capture_ctx );
1249 0 : fd_txn_account_mutable_fini( modify_acct_rec, accdb, &modify_acct_prepare );
1250 0 : }
1251 0 : }
1252 :
1253 : static void
1254 : fd_features_activate( fd_bank_t * bank,
1255 : fd_accdb_user_t * accdb,
1256 : fd_funk_txn_xid_t const * xid,
1257 0 : fd_capture_ctx_t * capture_ctx ) {
1258 0 : for( fd_feature_id_t const * id = fd_feature_iter_init();
1259 0 : !fd_feature_iter_done( id );
1260 0 : id = fd_feature_iter_next( id ) ) {
1261 0 : fd_feature_activate( bank, accdb, xid, capture_ctx, id, &id->id );
1262 0 : }
1263 0 : }
1264 :
1265 : /* Starting a new epoch.
1266 : New epoch: T
1267 : Just ended epoch: T-1
1268 : Epoch before: T-2
1269 :
1270 : In this function:
1271 : - stakes in T-2 (vote_states_prev_prev) should be replaced by T-1 (vote_states_prev)
1272 : - stakes at T-1 (vote_states_prev) should be replaced by updated stakes at T (vote_states)
1273 : - leader schedule should be calculated using new T-2 stakes (vote_states_prev_prev)
1274 :
1275 : Invariant during an epoch T:
1276 : vote_states_prev holds the stakes at T-1
1277 : vote_states_prev_prev holds the stakes at T-2
1278 : */
1279 : /* process for the start of a new epoch */
1280 : static void
1281 : fd_runtime_process_new_epoch( fd_banks_t * banks,
1282 : fd_bank_t * bank,
1283 : fd_accdb_user_t * accdb,
1284 : fd_funk_txn_xid_t const * xid,
1285 : fd_capture_ctx_t * capture_ctx,
1286 : ulong parent_epoch,
1287 0 : fd_runtime_stack_t * runtime_stack ) {
1288 :
1289 0 : FD_LOG_NOTICE(( "fd_process_new_epoch start, epoch: %lu, slot: %lu", fd_bank_epoch_get( bank ), fd_bank_slot_get( bank ) ));
1290 :
1291 0 : runtime_stack->stakes.prev_vote_credits_used = 0;
1292 :
1293 0 : fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_frontier_query( banks, bank );
1294 0 : if( FD_UNLIKELY( !stake_delegations ) ) {
1295 0 : FD_LOG_CRIT(( "stake_delegations is NULL" ));
1296 0 : }
1297 :
1298 0 : long start = fd_log_wallclock();
1299 :
1300 0 : ulong const slot = fd_bank_slot_get( bank );
1301 :
1302 : /* Activate new features
1303 : https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6587-L6598 */
1304 :
1305 0 : fd_features_activate( bank, accdb, xid, capture_ctx );
1306 0 : fd_features_restore( bank, accdb->funk, xid );
1307 :
1308 : /* Apply builtin program feature transitions
1309 : https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6621-L6624 */
1310 :
1311 0 : fd_apply_builtin_program_feature_transitions( bank, accdb, xid, runtime_stack, capture_ctx );
1312 :
1313 : /* Get the new rate activation epoch */
1314 0 : int _err[1];
1315 0 : ulong new_rate_activation_epoch_val = 0UL;
1316 0 : ulong * new_rate_activation_epoch = &new_rate_activation_epoch_val;
1317 0 : int is_some = fd_new_warmup_cooldown_rate_epoch(
1318 0 : fd_bank_epoch_schedule_query( bank ),
1319 0 : fd_bank_features_query( bank ),
1320 0 : slot,
1321 0 : new_rate_activation_epoch,
1322 0 : _err );
1323 0 : if( FD_UNLIKELY( !is_some ) ) {
1324 0 : new_rate_activation_epoch = NULL;
1325 0 : }
1326 :
1327 : /* Updates stake history sysvar accumulated values and recomputes
1328 : stake delegations for vote accounts. */
1329 :
1330 0 : fd_stakes_activate_epoch( bank, accdb, xid, capture_ctx, stake_delegations, new_rate_activation_epoch );
1331 :
1332 : /* Distribute rewards. This involves calculating the rewards for
1333 : every vote and stake account. */
1334 :
1335 0 : fd_hash_t const * parent_blockhash = fd_blockhashes_peek_last( fd_bank_block_hash_queue_query( bank ) );
1336 0 : fd_begin_partitioned_rewards( bank,
1337 0 : accdb,
1338 0 : xid,
1339 0 : runtime_stack,
1340 0 : capture_ctx,
1341 0 : stake_delegations,
1342 0 : parent_blockhash,
1343 0 : parent_epoch );
1344 :
1345 : /* The Agave client handles updating their stakes cache with a call to
1346 : update_epoch_stakes() which keys stakes by the leader schedule
1347 : epochs and retains up to 6 epochs of stakes. However, to correctly
1348 : calculate the leader schedule, we just need to maintain the vote
1349 : states for the current epoch, the previous epoch, and the one
1350 : before that.
1351 : https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/bank.rs#L2175
1352 : */
1353 :
1354 : /* Update vote_states_prev_prev with vote_states_prev */
1355 :
1356 0 : fd_update_vote_states_prev_prev( bank );
1357 :
1358 : /* Update vote_states_prev with vote_states */
1359 :
1360 0 : fd_update_vote_states_prev( bank );
1361 :
1362 : /* Now that our stakes caches have been updated, we can calculate the
1363 : leader schedule for the upcoming epoch epoch using our new
1364 : vote_states_prev_prev (stakes for T-2). */
1365 :
1366 0 : fd_runtime_update_leaders( bank, runtime_stack );
1367 :
1368 0 : long end = fd_log_wallclock();
1369 0 : FD_LOG_NOTICE(("fd_process_new_epoch took %ld ns", end - start));
1370 :
1371 0 : }
1372 :
1373 : /******************************************************************************/
1374 : /* Genesis */
1375 : /*******************************************************************************/
1376 :
1377 : static void
1378 : fd_runtime_genesis_init_program( fd_bank_t * bank,
1379 : fd_accdb_user_t * accdb,
1380 : fd_funk_txn_xid_t const * xid,
1381 0 : fd_capture_ctx_t * capture_ctx ) {
1382 :
1383 0 : fd_sysvar_clock_init( bank, accdb, xid, capture_ctx );
1384 0 : fd_sysvar_rent_init( bank, accdb, xid, capture_ctx );
1385 :
1386 0 : fd_sysvar_slot_history_init( bank, accdb, xid, capture_ctx );
1387 0 : fd_sysvar_epoch_schedule_init( bank, accdb, xid, capture_ctx );
1388 0 : fd_sysvar_recent_hashes_init( bank, accdb, xid, capture_ctx );
1389 0 : fd_sysvar_stake_history_init( bank, accdb, xid, capture_ctx );
1390 0 : fd_sysvar_last_restart_slot_init( bank, accdb, xid, capture_ctx );
1391 :
1392 0 : fd_builtin_programs_init( bank, accdb, xid, capture_ctx );
1393 0 : fd_stake_program_config_init( accdb, xid );
1394 0 : }
1395 :
1396 : static void
1397 : fd_runtime_init_bank_from_genesis( fd_banks_t * banks,
1398 : fd_bank_t * bank,
1399 : fd_funk_t * funk,
1400 : fd_funk_txn_xid_t const * xid,
1401 : fd_genesis_solana_global_t const * genesis_block,
1402 0 : fd_hash_t const * genesis_hash ) {
1403 :
1404 0 : fd_bank_poh_set( bank, *genesis_hash );
1405 :
1406 0 : fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
1407 0 : memset( bank_hash->hash, 0, FD_SHA256_HASH_SZ );
1408 :
1409 0 : fd_poh_config_global_t const * poh = &genesis_block->poh_config;
1410 0 : uint128 target_tick_duration = ((uint128)poh->target_tick_duration.seconds * 1000000000UL + (uint128)poh->target_tick_duration.nanoseconds);
1411 :
1412 0 : fd_bank_epoch_schedule_set( bank, genesis_block->epoch_schedule );
1413 :
1414 0 : fd_bank_rent_set( bank, genesis_block->rent );
1415 :
1416 0 : fd_bank_block_height_set( bank, 0UL );
1417 :
1418 0 : fd_bank_inflation_set( bank, genesis_block->inflation );
1419 :
1420 0 : {
1421 : /* FIXME Why is there a previous blockhash at genesis? Why is the
1422 : last_hash field an option type in Agave, if even the first
1423 : real block has a previous blockhash? */
1424 : /* TODO: Use a real seed here. */
1425 0 : fd_blockhashes_t * bhq = fd_blockhashes_init( fd_bank_block_hash_queue_modify( bank ), 0UL );
1426 0 : fd_blockhash_info_t * info = fd_blockhashes_push_new( bhq, genesis_hash );
1427 0 : info->fee_calculator.lamports_per_signature = 0UL;
1428 0 : }
1429 :
1430 0 : fd_bank_fee_rate_governor_set( bank, genesis_block->fee_rate_governor );
1431 :
1432 0 : fd_bank_lamports_per_signature_set( bank, 0UL );
1433 :
1434 0 : fd_bank_prev_lamports_per_signature_set( bank, 0UL );
1435 :
1436 0 : fd_bank_max_tick_height_set( bank, genesis_block->ticks_per_slot * (fd_bank_slot_get( bank ) + 1) );
1437 :
1438 0 : fd_bank_hashes_per_tick_set( bank, !!poh->hashes_per_tick ? poh->hashes_per_tick : 0UL );
1439 :
1440 0 : fd_bank_ns_per_slot_set( bank, target_tick_duration * genesis_block->ticks_per_slot );
1441 :
1442 0 : fd_bank_ticks_per_slot_set( bank, genesis_block->ticks_per_slot );
1443 :
1444 0 : fd_bank_genesis_creation_time_set( bank, genesis_block->creation_time );
1445 :
1446 0 : fd_bank_slots_per_year_set( bank, SECONDS_PER_YEAR * (1000000000.0 / (double)target_tick_duration) / (double)genesis_block->ticks_per_slot );
1447 :
1448 0 : fd_bank_signature_count_set( bank, 0UL );
1449 :
1450 : /* Derive epoch stakes */
1451 :
1452 0 : fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
1453 0 : if( FD_UNLIKELY( !stake_delegations ) ) {
1454 0 : FD_LOG_CRIT(( "Failed to join and new a stake delegations" ));
1455 0 : }
1456 :
1457 0 : fd_vote_states_t * vote_states = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_locking_modify( bank ), FD_RUNTIME_MAX_VOTE_ACCOUNTS, 999UL ) );
1458 0 : if( FD_UNLIKELY( !vote_states ) ) {
1459 0 : FD_LOG_CRIT(( "Failed to join and new a vote states" ));
1460 0 : }
1461 :
1462 0 : ulong capitalization = 0UL;
1463 :
1464 0 : fd_pubkey_account_pair_global_t const * accounts = fd_genesis_solana_accounts_join( genesis_block );
1465 :
1466 0 : for( ulong i=0UL; i<genesis_block->accounts_len; i++ ) {
1467 0 : fd_pubkey_account_pair_global_t const * acc = &accounts[ i ];
1468 0 : capitalization = fd_ulong_sat_add( capitalization, acc->account.lamports );
1469 :
1470 0 : uchar const * acc_data = fd_solana_account_data_join( &acc->account );
1471 :
1472 0 : if( !memcmp( acc->account.owner.key, fd_solana_vote_program_id.key, sizeof(fd_pubkey_t) ) ) {
1473 : /* This means that there is a vote account which should be
1474 : inserted into the vote states. Even after the vote account is
1475 : inserted, we still don't know the total amount of stake that is
1476 : delegated to the vote account. This must be calculated later. */
1477 0 : fd_vote_states_update_from_account( vote_states, &acc->key, acc_data, acc->account.data_len );
1478 0 : } else if( !memcmp( acc->account.owner.key, fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) {
1479 : /* If an account is a stake account, then it must be added to the
1480 : stake delegations cache. We should only add stake accounts that
1481 : have a valid non-zero stake. */
1482 0 : fd_stake_state_v2_t stake_state = {0};
1483 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
1484 0 : stake_state_v2, &stake_state,
1485 0 : acc_data, acc->account.data_len,
1486 0 : NULL ) ) ) {
1487 0 : FD_BASE58_ENCODE_32_BYTES( acc->key.key, stake_b58 );
1488 0 : FD_LOG_ERR(( "Failed to deserialize genesis stake account %s", stake_b58 ));
1489 0 : }
1490 0 : if( !fd_stake_state_v2_is_stake( &stake_state ) ) continue;
1491 0 : if( !stake_state.inner.stake.stake.delegation.stake ) continue;
1492 :
1493 0 : fd_stake_delegations_update(
1494 0 : stake_delegations,
1495 0 : (fd_pubkey_t *)acc->key.key,
1496 0 : &stake_state.inner.stake.stake.delegation.voter_pubkey,
1497 0 : stake_state.inner.stake.stake.delegation.stake,
1498 0 : stake_state.inner.stake.stake.delegation.activation_epoch,
1499 0 : stake_state.inner.stake.stake.delegation.deactivation_epoch,
1500 0 : stake_state.inner.stake.stake.credits_observed,
1501 0 : stake_state.inner.stake.stake.delegation.warmup_cooldown_rate );
1502 :
1503 0 : } else if( !memcmp( acc->account.owner.key, fd_solana_feature_program_id.key, sizeof(fd_pubkey_t) ) ) {
1504 : /* Feature Account */
1505 :
1506 : /* Scan list of feature IDs to resolve address=>feature offset */
1507 0 : fd_feature_id_t const *found = NULL;
1508 0 : for( fd_feature_id_t const * id = fd_feature_iter_init();
1509 0 : !fd_feature_iter_done( id );
1510 0 : id = fd_feature_iter_next( id ) ) {
1511 0 : if( !memcmp( acc->key.key, id->id.key, sizeof(fd_pubkey_t) ) ) {
1512 0 : found = id;
1513 0 : break;
1514 0 : }
1515 0 : }
1516 :
1517 0 : if( found ) {
1518 : /* Load feature activation */
1519 0 : fd_feature_t feature[1];
1520 0 : FD_TEST( fd_bincode_decode_static( feature, feature, acc_data, acc->account.data_len, NULL ) );
1521 :
1522 0 : fd_features_t * features = fd_bank_features_modify( bank );
1523 0 : if( feature->has_activated_at ) {
1524 0 : FD_LOG_DEBUG(( "Feature %s activated at %lu (genesis)", FD_BASE58_ENC_32_ALLOCA( acc->key.key ), feature->activated_at ));
1525 0 : fd_features_set( features, found, feature->activated_at );
1526 0 : } else {
1527 0 : FD_LOG_DEBUG(( "Feature %s not activated (genesis)", FD_BASE58_ENC_32_ALLOCA( acc->key.key ) ));
1528 0 : fd_features_set( features, found, ULONG_MAX );
1529 0 : }
1530 0 : }
1531 0 : }
1532 0 : }
1533 0 : fd_bank_vote_states_end_locking_modify( bank );
1534 :
1535 : /* fd_refresh_vote_accounts is responsible for updating the vote
1536 : states with the total amount of active delegated stake. It does
1537 : this by iterating over all active stake delegations and summing up
1538 : the amount of stake that is delegated to each vote account. */
1539 :
1540 0 : ulong new_rate_activation_epoch = 0UL;
1541 :
1542 0 : fd_stake_history_t stake_history[1];
1543 0 : fd_sysvar_stake_history_read( funk, xid, stake_history );
1544 :
1545 0 : fd_refresh_vote_accounts(
1546 0 : bank,
1547 0 : stake_delegations,
1548 0 : stake_history,
1549 0 : &new_rate_activation_epoch );
1550 :
1551 : /* Now that the stake and vote delegations are updated correctly, we
1552 : will propagate the vote states to the vote states for the previous
1553 : epoch and the epoch before that.
1554 :
1555 : This is despite the fact we are booting off of genesis which means
1556 : that there is no previous or previous-previous epoch. This is done
1557 : to simplify edge cases around leader schedule and rewards
1558 : calculation.
1559 :
1560 : TODO: Each of the edge cases around this needs to be documented
1561 : much better where each case is clearly enumerated and explained. */
1562 :
1563 0 : vote_states = fd_bank_vote_states_locking_modify( bank );
1564 0 : for( ulong i=0UL; i<genesis_block->accounts_len; i++ ) {
1565 0 : fd_pubkey_account_pair_global_t const * acc = &accounts[ i ];
1566 :
1567 0 : if( !memcmp( acc->account.owner.key, fd_solana_vote_program_id.key, sizeof(fd_pubkey_t) ) ) {
1568 0 : fd_vote_state_ele_t * vote_state = fd_vote_states_query( vote_states, &acc->key );
1569 :
1570 0 : vote_state->stake_t_1 = vote_state->stake;
1571 0 : vote_state->stake_t_2 = vote_state->stake;
1572 0 : }
1573 0 : }
1574 :
1575 0 : fd_vote_states_t * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_modify( bank );
1576 0 : fd_memcpy( vote_states_prev_prev, vote_states, FD_VOTE_STATES_FOOTPRINT );
1577 0 : fd_bank_vote_states_prev_prev_end_locking_modify( bank );
1578 :
1579 0 : fd_vote_states_t * vote_states_prev = fd_bank_vote_states_prev_locking_modify( bank );
1580 0 : fd_memcpy( vote_states_prev, vote_states, FD_VOTE_STATES_FOOTPRINT );
1581 0 : fd_bank_vote_states_prev_end_locking_modify( bank );
1582 :
1583 0 : fd_bank_vote_states_end_locking_modify( bank );
1584 :
1585 0 : fd_bank_epoch_set( bank, 0UL );
1586 :
1587 0 : fd_bank_capitalization_set( bank, capitalization );
1588 0 : }
1589 :
1590 : static int
1591 : fd_runtime_process_genesis_block( fd_bank_t * bank,
1592 : fd_accdb_user_t * accdb,
1593 : fd_funk_txn_xid_t const * xid,
1594 : fd_capture_ctx_t * capture_ctx,
1595 0 : fd_runtime_stack_t * runtime_stack ) {
1596 :
1597 0 : fd_hash_t * poh = fd_bank_poh_modify( bank );
1598 0 : ulong hashcnt_per_slot = fd_bank_hashes_per_tick_get( bank ) * fd_bank_ticks_per_slot_get( bank );
1599 0 : while( hashcnt_per_slot-- ) {
1600 0 : fd_sha256_hash( poh->hash, sizeof(fd_hash_t), poh->hash );
1601 0 : }
1602 :
1603 0 : fd_bank_execution_fees_set( bank, 0UL );
1604 :
1605 0 : fd_bank_priority_fees_set( bank, 0UL );
1606 :
1607 0 : fd_bank_signature_count_set( bank, 0UL );
1608 :
1609 0 : fd_bank_txn_count_set( bank, 0UL );
1610 :
1611 0 : fd_bank_failed_txn_count_set( bank, 0UL );
1612 :
1613 0 : fd_bank_nonvote_failed_txn_count_set( bank, 0UL );
1614 :
1615 0 : fd_bank_total_compute_units_used_set( bank, 0UL );
1616 :
1617 0 : fd_runtime_genesis_init_program( bank, accdb, xid, capture_ctx );
1618 :
1619 0 : fd_sysvar_slot_history_update( bank, accdb, xid, capture_ctx );
1620 :
1621 0 : fd_runtime_update_leaders( bank, runtime_stack );
1622 :
1623 0 : fd_runtime_freeze( bank, accdb, xid, capture_ctx );
1624 :
1625 0 : fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
1626 :
1627 0 : fd_hash_t const * prev_bank_hash = fd_bank_bank_hash_query( bank );
1628 :
1629 0 : fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
1630 0 : fd_hashes_hash_bank(
1631 0 : lthash,
1632 0 : prev_bank_hash,
1633 0 : (fd_hash_t *)fd_bank_poh_query( bank )->hash,
1634 0 : 0UL,
1635 0 : bank_hash );
1636 :
1637 0 : fd_bank_lthash_end_locking_query( bank );
1638 :
1639 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
1640 0 : }
1641 :
1642 : void
1643 : fd_runtime_read_genesis( fd_banks_t * banks,
1644 : fd_bank_t * bank,
1645 : fd_accdb_user_t * accdb,
1646 : fd_funk_txn_xid_t const * xid,
1647 : fd_capture_ctx_t * capture_ctx,
1648 : fd_hash_t const * genesis_hash,
1649 : fd_lthash_value_t const * genesis_lthash,
1650 : fd_genesis_solana_global_t const * genesis_block,
1651 0 : fd_runtime_stack_t * runtime_stack ) {
1652 :
1653 0 : fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
1654 0 : *lthash = *genesis_lthash;
1655 0 : fd_bank_lthash_end_locking_modify( bank );
1656 :
1657 : /* Once the accounts have been loaded from the genesis config into
1658 : the accounts db, we can initialize the bank state. This involves
1659 : setting some fields, and notably setting up the vote and stake
1660 : caches which are used for leader scheduling/rewards. */
1661 :
1662 0 : fd_runtime_init_bank_from_genesis( banks, bank, accdb->funk, xid, genesis_block, genesis_hash );
1663 :
1664 : /* Write the native programs to the accounts db. */
1665 :
1666 0 : fd_string_pubkey_pair_global_t * nips = fd_genesis_solana_native_instruction_processors_join( genesis_block );
1667 :
1668 0 : for( ulong i=0UL; i<genesis_block->native_instruction_processors_len; i++ ) {
1669 0 : fd_string_pubkey_pair_global_t const * a = &nips[ i ];
1670 :
1671 0 : uchar const * string = fd_string_pubkey_pair_string_join( a );
1672 0 : fd_write_builtin_account( bank, accdb, xid, capture_ctx, a->pubkey, (const char *)string, a->string_len );
1673 0 : }
1674 :
1675 0 : fd_features_restore( bank, accdb->funk, xid );
1676 :
1677 : /* At this point, state related to the bank and the accounts db
1678 : have been initialized and we are free to finish executing the
1679 : block. In practice, this updates some bank fields (notably the
1680 : poh and bank hash). */
1681 :
1682 0 : int err = fd_runtime_process_genesis_block( bank, accdb, xid, capture_ctx, runtime_stack );
1683 0 : if( FD_UNLIKELY( err ) ) FD_LOG_CRIT(( "genesis slot 0 execute failed with error %d", err ));
1684 0 : }
1685 :
1686 : /******************************************************************************/
1687 : /* Offline Replay */
1688 : /******************************************************************************/
1689 :
1690 : /* As a note, currently offline and live replay of transactions has differences
1691 : with regards to how the execution environment is setup. These are helpers
1692 : used to emulate this behavior */
1693 :
1694 : void
1695 : fd_runtime_block_pre_execute_process_new_epoch( fd_banks_t * banks,
1696 : fd_bank_t * bank,
1697 : fd_accdb_user_t * accdb,
1698 : fd_funk_txn_xid_t const * xid,
1699 : fd_capture_ctx_t * capture_ctx,
1700 : fd_runtime_stack_t * runtime_stack,
1701 0 : int * is_epoch_boundary ) {
1702 :
1703 0 : ulong const slot = fd_bank_slot_get( bank );
1704 0 : if( slot != 0UL ) {
1705 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
1706 :
1707 0 : ulong prev_epoch = fd_slot_to_epoch( epoch_schedule, fd_bank_parent_slot_get( bank ), NULL );
1708 0 : ulong slot_idx;
1709 0 : ulong new_epoch = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
1710 0 : if( FD_UNLIKELY( slot_idx==1UL && new_epoch==0UL ) ) {
1711 : /* The block after genesis has a height of 1. */
1712 0 : fd_bank_block_height_set( bank, 1UL );
1713 0 : }
1714 :
1715 0 : if( FD_UNLIKELY( prev_epoch<new_epoch || !slot_idx ) ) {
1716 0 : FD_LOG_DEBUG(( "Epoch boundary starting" ));
1717 0 : fd_runtime_process_new_epoch( banks, bank, accdb, xid, capture_ctx, prev_epoch, runtime_stack );
1718 0 : *is_epoch_boundary = 1;
1719 0 : }
1720 0 : } else {
1721 0 : *is_epoch_boundary = 0;
1722 0 : }
1723 :
1724 0 : if( FD_LIKELY( fd_bank_slot_get( bank )!=0UL ) ) {
1725 0 : fd_distribute_partitioned_epoch_rewards( bank, accdb, xid, capture_ctx );
1726 0 : }
1727 0 : }
1728 :
1729 : void
1730 : fd_runtime_block_execute_finalize( fd_bank_t * bank,
1731 : fd_accdb_user_t * accdb,
1732 : fd_funk_txn_xid_t const * xid,
1733 : fd_capture_ctx_t * capture_ctx,
1734 0 : int silent ) {
1735 :
1736 : /* This slot is now "frozen" and can't be changed anymore. */
1737 0 : fd_runtime_freeze( bank, accdb, xid, capture_ctx );
1738 :
1739 0 : fd_runtime_update_bank_hash( bank, capture_ctx, silent );
1740 0 : }
|