Skip to content

Commit 8ee32e8

Browse files
runtime: sysvar cache lifetimes and misc cleanup/fixes
1 parent 0b00ac2 commit 8ee32e8

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

+2954
-636
lines changed

src/app/ledger/main.c

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

14161416
// 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

+9-4
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,10 @@ prepare_new_block_execution( fd_replay_tile_ctx_t * ctx,
15581558
fork->slot_ctx->funk_txn = fd_funk_txn_prepare(ctx->funk, fork->slot_ctx->funk_txn, &xid, 1);
15591559
fd_funk_txn_end_write( ctx->funk );
15601560

1561+
/* We must invalidate all of the sysvar cache entries in the case that
1562+
their memory is no longer valid/the cache contains stale data. */
1563+
fd_sysvar_cache_invalidate( fork->slot_ctx->sysvar_cache );
1564+
15611565
int is_epoch_boundary = 0;
15621566
/* TODO: Currently all of the epoch boundary/rewards logic is not
15631567
multhreaded at the epoch boundary. */
@@ -1592,7 +1596,11 @@ prepare_new_block_execution( fd_replay_tile_ctx_t * ctx,
15921596
}
15931597

15941598
/* Read slot history into slot ctx */
1595-
fork->slot_ctx->slot_history = fd_sysvar_slot_history_read( fork->slot_ctx->funk, fork->slot_ctx->funk_txn, ctx->runtime_spad );
1599+
fork->slot_ctx->slot_history = fd_sysvar_slot_history_read( fork->slot_ctx->sysvar_cache,
1600+
fork->slot_ctx->funk,
1601+
fork->slot_ctx->funk_txn,
1602+
ctx->runtime_spad,
1603+
fork->slot_ctx->runtime_wksp );
15961604

15971605
if( is_new_epoch_in_new_block ) {
15981606
publish_stake_weights( ctx, stem, fork->slot_ctx );
@@ -2576,9 +2584,6 @@ after_credit( fd_replay_tile_ctx_t * ctx,
25762584
/* Call fd_runtime_block_execute_finalize_tpool which updates sysvar and cleanup some other stuff */
25772585
/**************************************************************************************************/
25782586

2579-
/* Destroy the slot history */
2580-
fd_slot_history_destroy( fork->slot_ctx->slot_history );
2581-
25822587
fd_runtime_block_info_t runtime_block_info[1];
25832588
runtime_block_info->signature_cnt = fork->slot_ctx->signature_cnt;
25842589

src/flamenco/rewards/fd_rewards.c

+15-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,12 @@ 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_epoch_rewards_read( slot_ctx->sysvar_cache,
1183+
slot_ctx->funk,
1184+
slot_ctx->funk_txn,
1185+
runtime_spad,
1186+
slot_ctx->runtime_wksp );
1187+
if( FD_UNLIKELY( epoch_rewards == NULL ) ) {
11841188
FD_LOG_NOTICE(( "failed to read sysvar epoch rewards - the sysvar may not have been created yet" ));
11851189
set_epoch_reward_status_inactive( slot_ctx, runtime_spad );
11861190
return;
@@ -1211,12 +1215,14 @@ fd_rewards_recalculate_partitioned_rewards( fd_exec_slot_ctx_t * slot_ctx,
12111215
int is_some = fd_new_warmup_cooldown_rate_epoch( slot_ctx->slot_bank.slot,
12121216
slot_ctx->sysvar_cache,
12131217
&slot_ctx->epoch_ctx->features,
1214-
new_warmup_cooldown_rate_epoch, _err );
1218+
slot_ctx->runtime_wksp,
1219+
new_warmup_cooldown_rate_epoch,
1220+
_err );
12151221
if( FD_UNLIKELY( !is_some ) ) {
12161222
new_warmup_cooldown_rate_epoch = NULL;
12171223
}
12181224

1219-
fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history( slot_ctx->sysvar_cache );
1225+
fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
12201226
if( FD_UNLIKELY( !stake_history ) ) {
12211227
FD_LOG_ERR(( "StakeHistory sysvar is missing from sysvar cache" ));
12221228
}

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,15 @@ 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->sysvar_cache,
282+
ctx->funk,
283+
ctx->funk_txn,
284+
ctx->spad,
285+
ctx->runtime_pub_wksp );
286+
287+
if( fd_sysvar_slot_history_find_slot( slot_history,
288+
slot,
289+
ctx->runtime_pub_wksp ) == FD_SLOT_HISTORY_SLOT_FOUND ) {
286290
return 1;
287291
}
288292

@@ -453,7 +457,7 @@ load_transaction_account( fd_exec_txn_ctx_t * txn_ctx,
453457
int
454458
fd_executor_load_transaction_accounts( fd_exec_txn_ctx_t * txn_ctx ) {
455459
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 );
460+
fd_epoch_schedule_t const * schedule = fd_sysvar_cache_epoch_schedule( txn_ctx->sysvar_cache, txn_ctx->runtime_pub_wksp );
457461
ulong epoch = fd_slot_to_epoch( schedule, txn_ctx->slot, NULL );
458462

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

824828
/* Collect rent from the fee payer and set the starting lamports (to avoid unbalanced lamports issues in instruction execution)
825829
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 );
830+
fd_epoch_schedule_t const * schedule = fd_sysvar_cache_epoch_schedule( txn_ctx->sysvar_cache, txn_ctx->runtime_pub_wksp );
827831
ulong epoch = fd_slot_to_epoch( schedule, txn_ctx->slot, NULL );
828832
txn_ctx->collected_rent += fd_runtime_collect_rent_from_account( txn_ctx->slot,
829833
&txn_ctx->schedule,
@@ -861,7 +865,7 @@ fd_executor_setup_accessed_accounts_for_txn( fd_exec_txn_ctx_t * txn_ctx ) {
861865

862866
if( txn_ctx->txn_descriptor->transaction_version == FD_TXN_V0 ) {
863867
/* 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 );
868+
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( txn_ctx->sysvar_cache, txn_ctx->runtime_pub_wksp );
865869
if( FD_UNLIKELY( !slot_hashes_global ) ) {
866870
return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
867871
}
@@ -1438,7 +1442,7 @@ fd_execute_txn( fd_execute_txn_task_info_t * task_info ) {
14381442

14391443
int
14401444
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 );
1445+
fd_rent_t const * rent = fd_sysvar_cache_rent( txn_ctx->sysvar_cache, txn_ctx->runtime_pub_wksp );
14421446

14431447
ulong starting_lamports_l = 0;
14441448
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
}
@@ -2341,7 +2345,7 @@ fd_new_target_program_account( fd_exec_slot_ctx_t * slot_ctx,
23412345
};
23422346

23432347
/* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/core_bpf_migration/mod.rs#L89-L90 */
2344-
fd_rent_t const * rent = fd_sysvar_cache_rent( slot_ctx->sysvar_cache );
2348+
fd_rent_t const * rent = fd_sysvar_cache_rent( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
23452349
if( FD_UNLIKELY( rent==NULL ) ) {
23462350
return -1;
23472351
}
@@ -2414,7 +2418,7 @@ fd_new_target_program_data_account( fd_exec_slot_ctx_t * slot_ctx,
24142418
}
24152419

24162420
/* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/core_bpf_migration/mod.rs#L127-L132 */
2417-
const fd_rent_t * rent = fd_sysvar_cache_rent( slot_ctx->sysvar_cache );
2421+
const fd_rent_t * rent = fd_sysvar_cache_rent( slot_ctx->sysvar_cache, slot_ctx->runtime_wksp );
24182422
if( FD_UNLIKELY( rent==NULL ) ) {
24192423
return -1;
24202424
}
@@ -2859,6 +2863,7 @@ fd_runtime_process_new_epoch( fd_exec_slot_ctx_t * slot_ctx,
28592863
int is_some = fd_new_warmup_cooldown_rate_epoch( slot_ctx->slot_bank.slot,
28602864
slot_ctx->sysvar_cache,
28612865
&slot_ctx->epoch_ctx->features,
2866+
slot_ctx->runtime_wksp,
28622867
new_rate_activation_epoch,
28632868
_err );
28642869
if( FD_UNLIKELY( !is_some ) ) {
@@ -2891,7 +2896,7 @@ fd_runtime_process_new_epoch( fd_exec_slot_ctx_t * slot_ctx,
28912896

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

0 commit comments

Comments
 (0)