Skip to content

Commit 904b947

Browse files
runtime: sysvar cache lifetimes and misc cleanup/fixes
1 parent 54bd073 commit 904b947

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2849
-664
lines changed

src/app/ledger/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ initial_setup( int argc, char ** argv, fd_ledger_args_t * args ) {
14141414
/* We've got full snapshots that contain all 0s for the account
14151415
hash in account meta. Running hash verify allows us to
14161416
populate the hash in account meta with real values. */
1417-
FD_LOG_WARNING(( "verify-acc-hash needs to be 1" ));
1417+
FD_LOG_WARNING(( "verify-acc-hash should be 1" ));
14181418
}
14191419

14201420
// TODO: Add argument validation. Make sure that we aren't including any arguments that aren't parsed for

src/discof/replay/fd_replay_tile.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,10 @@ prepare_new_block_execution( fd_replay_tile_ctx_t * ctx,
15891589
fork->slot_ctx->funk_txn = fd_funk_txn_prepare(ctx->funk, fork->slot_ctx->funk_txn, &xid, 1);
15901590
fd_funk_txn_end_write( ctx->funk );
15911591

1592+
/* We must invalidate all of the sysvar cache entries in the case that
1593+
their memory is no longer valid/the cache contains stale data. */
1594+
fd_sysvar_cache_invalidate( fork->slot_ctx->sysvar_cache );
1595+
15921596
int is_epoch_boundary = 0;
15931597
/* TODO: Currently all of the epoch boundary/rewards logic is not
15941598
multhreaded at the epoch boundary. */
@@ -1623,7 +1627,8 @@ prepare_new_block_execution( fd_replay_tile_ctx_t * ctx,
16231627
}
16241628

16251629
/* Read slot history into slot ctx */
1626-
fork->slot_ctx->slot_history = fd_sysvar_slot_history_read( fork->slot_ctx->funk, fork->slot_ctx->funk_txn, ctx->runtime_spad );
1630+
fork->slot_ctx->slot_history = fd_sysvar_cache_slot_history( fork->slot_ctx->sysvar_cache,
1631+
fork->slot_ctx->runtime_wksp );
16271632

16281633
if( is_new_epoch_in_new_block ) {
16291634
publish_stake_weights( ctx, stem, fork->slot_ctx );
@@ -2658,9 +2663,6 @@ after_credit( fd_replay_tile_ctx_t * ctx,
26582663
/* Call fd_runtime_block_execute_finalize_tpool which updates sysvar and cleanup some other stuff */
26592664
/**************************************************************************************************/
26602665

2661-
/* Destroy the slot history */
2662-
fd_slot_history_destroy( fork->slot_ctx->slot_history );
2663-
26642666
fd_runtime_block_info_t runtime_block_info[1];
26652667
runtime_block_info->signature_cnt = fork->slot_ctx->signature_cnt;
26662668

src/flamenco/rewards/fd_rewards.c

+11-9
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ calculate_reward_points_partitioned( fd_exec_slot_ctx_t * slot_ctx,
389389
int is_some = fd_new_warmup_cooldown_rate_epoch( slot_ctx->slot_bank.slot,
390390
slot_ctx->sysvar_cache,
391391
&slot_ctx->epoch_ctx->features,
392+
slot_ctx->runtime_wksp,
392393
new_warmup_cooldown_rate_epoch,
393394
_err );
394395
if( FD_UNLIKELY( !is_some ) ) {
@@ -604,7 +605,8 @@ calculate_stake_vote_rewards( fd_exec_slot_ctx_t * slot_ct
604605
ulong * new_warmup_cooldown_rate_epoch = &new_warmup_cooldown_rate_epoch_val;
605606
int is_some = fd_new_warmup_cooldown_rate_epoch( slot_ctx->slot_bank.slot,
606607
slot_ctx->sysvar_cache,
607-
&slot_ctx->epoch_ctx->features,
608+
&slot_ctx->epoch_ctx->features,
609+
slot_ctx->runtime_wksp,
608610
new_warmup_cooldown_rate_epoch,
609611
_err );
610612
if( FD_UNLIKELY( !is_some ) ) {
@@ -695,7 +697,8 @@ calculate_validator_rewards( fd_exec_slot_ctx_t * slot_ctx,
695697
ulong exec_spad_cnt,
696698
fd_spad_t * runtime_spad ) {
697699
/* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/runtime/src/bank.rs#L2759-L2786 */
698-
fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history( slot_ctx->sysvar_cache );
700+
701+
fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
699702
if( FD_UNLIKELY( !stake_history ) ) {
700703
FD_LOG_ERR(( "StakeHistory sysvar is missing from sysvar cache" ));
701704
}
@@ -1176,11 +1179,8 @@ fd_rewards_recalculate_partitioned_rewards( fd_exec_slot_ctx_t * slot_ctx,
11761179
fd_spad_t * * exec_spads,
11771180
ulong exec_spad_cnt,
11781181
fd_spad_t * runtime_spad ) {
1179-
fd_sysvar_epoch_rewards_t epoch_rewards[1];
1180-
if( FD_UNLIKELY( fd_sysvar_epoch_rewards_read( epoch_rewards,
1181-
slot_ctx->sysvar_cache,
1182-
slot_ctx->funk,
1183-
slot_ctx->funk_txn )==NULL ) ) {
1182+
fd_sysvar_epoch_rewards_t * epoch_rewards = fd_sysvar_cache_epoch_rewards( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
1183+
if( FD_UNLIKELY( epoch_rewards == NULL ) ) {
11841184
FD_LOG_NOTICE(( "failed to read sysvar epoch rewards - the sysvar may not have been created yet" ));
11851185
set_epoch_reward_status_inactive( slot_ctx, runtime_spad );
11861186
return;
@@ -1211,12 +1211,14 @@ fd_rewards_recalculate_partitioned_rewards( fd_exec_slot_ctx_t * slot_ctx,
12111211
int is_some = fd_new_warmup_cooldown_rate_epoch( slot_ctx->slot_bank.slot,
12121212
slot_ctx->sysvar_cache,
12131213
&slot_ctx->epoch_ctx->features,
1214-
new_warmup_cooldown_rate_epoch, _err );
1214+
slot_ctx->runtime_wksp,
1215+
new_warmup_cooldown_rate_epoch,
1216+
_err );
12151217
if( FD_UNLIKELY( !is_some ) ) {
12161218
new_warmup_cooldown_rate_epoch = NULL;
12171219
}
12181220

1219-
fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history( slot_ctx->sysvar_cache );
1221+
fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
12201222
if( FD_UNLIKELY( !stake_history ) ) {
12211223
FD_LOG_ERR(( "StakeHistory sysvar is missing from sysvar cache" ));
12221224
}

src/flamenco/runtime/context/fd_exec_slot_ctx.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ fd_exec_slot_ctx_new( void * mem,
2525
fd_exec_slot_ctx_t * self = (fd_exec_slot_ctx_t *)mem;
2626
fd_slot_bank_new( &self->slot_bank );
2727

28-
self->sysvar_cache = fd_sysvar_cache_new( fd_spad_alloc( runtime_spad, fd_sysvar_cache_align(), fd_sysvar_cache_footprint() ) );
28+
uchar * sysvar_cache_mem = fd_spad_alloc( runtime_spad, fd_sysvar_cache_align(), fd_sysvar_cache_footprint() );
29+
if( FD_UNLIKELY( !sysvar_cache_mem ) ) {
30+
FD_LOG_WARNING(( "failed to allocate sysvar cache" ));
31+
}
32+
self->sysvar_cache = fd_sysvar_cache_new( sysvar_cache_mem );
2933

3034
FD_COMPILER_MFENCE();
3135
self->magic = FD_EXEC_SLOT_CTX_MAGIC;

src/flamenco/runtime/context/fd_exec_slot_ctx.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct __attribute__((aligned(8UL))) fd_exec_slot_ctx {
4747
fd_sysvar_cache_t * sysvar_cache;
4848

4949
fd_txncache_t * status_cache;
50-
fd_slot_history_t * slot_history;
50+
fd_slot_history_global_t * slot_history;
5151

5252
int enable_exec_recording; /* Enable/disable execution metadata
5353
recording, e.g. txn logs. Analogue

src/flamenco/runtime/fd_executor.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,14 @@ status_check_tower( ulong slot, void * _ctx ) {
278278
return 1;
279279
}
280280

281-
fd_slot_history_t * slot_history = fd_sysvar_slot_history_read( ctx->funk,
282-
ctx->funk_txn,
283-
ctx->spad );
284-
285-
if( fd_sysvar_slot_history_find_slot( slot_history, slot ) == FD_SLOT_HISTORY_SLOT_FOUND ) {
281+
fd_slot_history_global_t * slot_history = fd_sysvar_slot_history_read( ctx->funk,
282+
ctx->funk_txn,
283+
ctx->spad,
284+
ctx->spad_wksp );
285+
286+
if( fd_sysvar_slot_history_find_slot( slot_history,
287+
slot,
288+
ctx->runtime_pub_wksp ) == FD_SLOT_HISTORY_SLOT_FOUND ) {
286289
return 1;
287290
}
288291

@@ -453,7 +456,7 @@ load_transaction_account( fd_exec_txn_ctx_t * txn_ctx,
453456
int
454457
fd_executor_load_transaction_accounts( fd_exec_txn_ctx_t * txn_ctx ) {
455458
ulong requested_loaded_accounts_data_size = txn_ctx->loaded_accounts_data_size_limit;
456-
fd_epoch_schedule_t const * schedule = fd_sysvar_cache_epoch_schedule( txn_ctx->sysvar_cache );
459+
fd_epoch_schedule_t const * schedule = fd_sysvar_cache_epoch_schedule( txn_ctx->sysvar_cache, txn_ctx->runtime_pub_wksp );
457460
ulong epoch = fd_slot_to_epoch( schedule, txn_ctx->slot, NULL );
458461

459462
/* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L429-L443 */
@@ -823,7 +826,7 @@ fd_executor_validate_transaction_fee_payer( fd_exec_txn_ctx_t * txn_ctx ) {
823826

824827
/* Collect rent from the fee payer and set the starting lamports (to avoid unbalanced lamports issues in instruction execution)
825828
https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/svm/src/transaction_processor.rs#L438-L445 */
826-
fd_epoch_schedule_t const * schedule = fd_sysvar_cache_epoch_schedule( txn_ctx->sysvar_cache );
829+
fd_epoch_schedule_t const * schedule = fd_sysvar_cache_epoch_schedule( txn_ctx->sysvar_cache, txn_ctx->runtime_pub_wksp );
827830
ulong epoch = fd_slot_to_epoch( schedule, txn_ctx->slot, NULL );
828831
txn_ctx->collected_rent += fd_runtime_collect_rent_from_account( txn_ctx->slot,
829832
&txn_ctx->schedule,
@@ -861,7 +864,7 @@ fd_executor_setup_accessed_accounts_for_txn( fd_exec_txn_ctx_t * txn_ctx ) {
861864

862865
if( txn_ctx->txn_descriptor->transaction_version == FD_TXN_V0 ) {
863866
/* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/runtime/src/bank/address_lookup_table.rs#L44-L48 */
864-
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( txn_ctx->sysvar_cache );
867+
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( txn_ctx->sysvar_cache, txn_ctx->runtime_pub_wksp );
865868
if( FD_UNLIKELY( !slot_hashes_global ) ) {
866869
return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
867870
}
@@ -1438,7 +1441,7 @@ fd_execute_txn( fd_execute_txn_task_info_t * task_info ) {
14381441

14391442
int
14401443
fd_executor_txn_check( fd_exec_txn_ctx_t * txn_ctx ) {
1441-
fd_rent_t const * rent = fd_sysvar_cache_rent( txn_ctx->sysvar_cache );
1444+
fd_rent_t const * rent = fd_sysvar_cache_rent( txn_ctx->sysvar_cache, txn_ctx->runtime_pub_wksp );
14421445

14431446
ulong starting_lamports_l = 0;
14441447
ulong starting_lamports_h = 0;

src/flamenco/runtime/fd_runtime.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ fd_runtime_validate_fee_collector( fd_exec_slot_ctx_t const * slot_ctx,
273273
We already know that the post deposit balance is >0 because we are paying a >0 amount.
274274
So TLDR we just check if the account is rent exempt.
275275
*/
276-
ulong minbal = fd_rent_exempt_minimum_balance( fd_sysvar_cache_rent( slot_ctx->sysvar_cache ), collector->vt->get_data_len( collector ) );
276+
ulong minbal = fd_rent_exempt_minimum_balance( fd_sysvar_cache_rent( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp ),
277+
collector->vt->get_data_len( collector ) );
277278
if( FD_UNLIKELY( collector->vt->get_lamports( collector ) + fee < minbal ) ) {
278279
FD_BASE58_ENCODE_32_BYTES( collector->pubkey->key, _out_key );
279280
FD_LOG_WARNING(("cannot pay a rent paying account (%s)", _out_key ));
@@ -467,7 +468,7 @@ fd_runtime_freeze( fd_exec_slot_ctx_t * slot_ctx, fd_spad_t * runtime_spad ) {
467468
fd_sysvar_recent_hashes_update( slot_ctx, runtime_spad );
468469

469470
if( !FD_FEATURE_ACTIVE( slot_ctx->slot_bank.slot, slot_ctx->epoch_ctx->features, disable_fees_sysvar) )
470-
fd_sysvar_fees_update(slot_ctx);
471+
fd_sysvar_fees_update( slot_ctx, runtime_spad );
471472

472473
ulong fees = 0UL;
473474
ulong burn = 0UL;
@@ -531,6 +532,9 @@ fd_runtime_freeze( fd_exec_slot_ctx_t * slot_ctx, fd_spad_t * runtime_spad ) {
531532

532533
FD_LOG_DEBUG(( "fd_runtime_freeze: capitalization %lu ", slot_ctx->slot_bank.capitalization));
533534
slot_ctx->slot_bank.collected_rent = 0;
535+
536+
/* At this point we want to invalidate the sysvar cache entries. */
537+
fd_sysvar_cache_invalidate( slot_ctx->sysvar_cache );
534538
}
535539

536540
#define FD_RENT_EXEMPT (-1L)
@@ -990,13 +994,13 @@ fd_runtime_block_sysvar_update_pre_execute( fd_exec_slot_ctx_t * slot_ctx,
990994
double clock_update_time_ms = (double)clock_update_time * 1e-6;
991995
FD_LOG_INFO(( "clock updated - slot: %lu, elapsed: %6.6f ms", slot_ctx->slot_bank.slot, clock_update_time_ms ));
992996
if( !FD_FEATURE_ACTIVE( slot_ctx->slot_bank.slot, slot_ctx->epoch_ctx->features, disable_fees_sysvar ) ) {
993-
fd_sysvar_fees_update(slot_ctx);
997+
fd_sysvar_fees_update( slot_ctx, runtime_spad );
994998
}
995999
// It has to go into the current txn previous info but is not in slot 0
9961000
if( slot_ctx->slot_bank.slot != 0 ) {
9971001
fd_sysvar_slot_hashes_update( slot_ctx, runtime_spad );
9981002
}
999-
fd_sysvar_last_restart_slot_update( slot_ctx );
1003+
fd_sysvar_last_restart_slot_update( slot_ctx, runtime_spad );
10001004

10011005
return 0;
10021006
}
@@ -2321,7 +2325,7 @@ fd_new_target_program_account( fd_exec_slot_ctx_t * slot_ctx,
23212325
};
23222326

23232327
/* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/core_bpf_migration/mod.rs#L89-L90 */
2324-
fd_rent_t const * rent = fd_sysvar_cache_rent( slot_ctx->sysvar_cache );
2328+
fd_rent_t const * rent = fd_sysvar_cache_rent( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
23252329
if( FD_UNLIKELY( rent==NULL ) ) {
23262330
return -1;
23272331
}
@@ -2394,7 +2398,7 @@ fd_new_target_program_data_account( fd_exec_slot_ctx_t * slot_ctx,
23942398
}
23952399

23962400
/* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/core_bpf_migration/mod.rs#L127-L132 */
2397-
const fd_rent_t * rent = fd_sysvar_cache_rent( slot_ctx->sysvar_cache );
2401+
const fd_rent_t * rent = fd_sysvar_cache_rent( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
23982402
if( FD_UNLIKELY( rent==NULL ) ) {
23992403
return -1;
24002404
}
@@ -2839,6 +2843,7 @@ fd_runtime_process_new_epoch( fd_exec_slot_ctx_t * slot_ctx,
28392843
int is_some = fd_new_warmup_cooldown_rate_epoch( slot_ctx->slot_bank.slot,
28402844
slot_ctx->sysvar_cache,
28412845
&slot_ctx->epoch_ctx->features,
2846+
slot_ctx->runtime_wksp,
28422847
new_rate_activation_epoch,
28432848
_err );
28442849
if( FD_UNLIKELY( !is_some ) ) {
@@ -2871,7 +2876,7 @@ fd_runtime_process_new_epoch( fd_exec_slot_ctx_t * slot_ctx,
28712876

28722877
/* Refresh vote accounts in stakes cache using updated stake weights, and merges slot bank vote accounts with the epoch bank vote accounts.
28732878
https://github.com/anza-xyz/agave/blob/v2.1.6/runtime/src/stakes.rs#L363-L370 */
2874-
fd_stake_history_t const * history = fd_sysvar_cache_stake_history( slot_ctx->sysvar_cache );
2879+
fd_stake_history_t const * history = fd_sysvar_cache_stake_history( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
28752880
if( FD_UNLIKELY( !history ) ) {
28762881
FD_LOG_ERR(( "StakeHistory sysvar is missing from sysvar cache" ));
28772882
}

src/flamenco/runtime/program/fd_address_lookup_table_program.c

+12-17
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ create_lookup_table( fd_exec_instr_ctx_t * ctx,
259259
ulong derivation_slot = 1UL;
260260

261261
do {
262-
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( ctx->txn_ctx->sysvar_cache );
262+
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( ctx->txn_ctx->sysvar_cache, ctx->txn_ctx->runtime_pub_wksp );
263263

264264
if( FD_UNLIKELY( !slot_hashes_global ) ) {
265265
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
@@ -678,12 +678,11 @@ extend_lookup_table( fd_exec_instr_ctx_t * ctx,
678678
}
679679

680680
/* https://github.com/solana-labs/solana/blob/v1.17.4/programs/address-lookup-table/src/processor.rs#L290 */
681-
fd_sol_sysvar_clock_t clock[1];
682-
if( FD_UNLIKELY( !fd_sysvar_clock_read( clock,
683-
ctx->txn_ctx->sysvar_cache,
684-
ctx->txn_ctx->funk,
685-
ctx->txn_ctx->funk_txn ) ) )
681+
fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock( ctx->txn_ctx->sysvar_cache,
682+
ctx->txn_ctx->runtime_pub_wksp );
683+
if( FD_UNLIKELY( !clock ) ) {
686684
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
685+
}
687686

688687
/* https://github.com/solana-labs/solana/blob/v1.17.4/programs/address-lookup-table/src/processor.rs#L291-L299 */
689688
if( clock->slot!=state->meta.last_extended_slot ) {
@@ -874,11 +873,9 @@ deactivate_lookup_table( fd_exec_instr_ctx_t * ctx ) {
874873
}
875874

876875
/* https://github.com/solana-labs/solana/blob/v1.17.4/programs/address-lookup-table/src/processor.rs#L380 */
877-
fd_sol_sysvar_clock_t clock[1];
878-
if( FD_UNLIKELY( !fd_sysvar_clock_read( clock,
879-
ctx->txn_ctx->sysvar_cache,
880-
ctx->txn_ctx->funk,
881-
ctx->txn_ctx->funk_txn ) ) ) {
876+
fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock( ctx->txn_ctx->sysvar_cache,
877+
ctx->txn_ctx->runtime_pub_wksp );
878+
if( FD_UNLIKELY( !clock ) ) {
882879
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
883880
}
884881

@@ -996,16 +993,14 @@ close_lookup_table( fd_exec_instr_ctx_t * ctx ) {
996993
}
997994

998995
/* https://github.com/solana-labs/solana/blob/v1.17.4/programs/address-lookup-table/src/processor.rs#L437 */
999-
fd_sol_sysvar_clock_t clock[1];
1000-
if( FD_UNLIKELY( !fd_sysvar_clock_read( clock,
1001-
ctx->txn_ctx->sysvar_cache,
1002-
ctx->txn_ctx->funk,
1003-
ctx->txn_ctx->funk_txn ) ) ) {
996+
fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock( ctx->txn_ctx->sysvar_cache,
997+
ctx->txn_ctx->runtime_pub_wksp );
998+
if( FD_UNLIKELY( !clock ) ) {
1004999
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
10051000
}
10061001

10071002
/* https://github.com/solana-labs/solana/blob/v1.17.4/programs/address-lookup-table/src/processor.rs#L438 */
1008-
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( ctx->txn_ctx->sysvar_cache );
1003+
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( ctx->txn_ctx->sysvar_cache, ctx->txn_ctx->runtime_pub_wksp );
10091004
if( FD_UNLIKELY( !slot_hashes_global ) ) {
10101005
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
10111006
}

0 commit comments

Comments
 (0)