Skip to content

Commit 9d5b903

Browse files
runtime: removing stake account keys from bank
1 parent 13a65bf commit 9d5b903

File tree

15 files changed

+279
-319
lines changed

15 files changed

+279
-319
lines changed

src/discof/replay/fd_replay_tile.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,13 @@ init_after_snapshot( fd_replay_tile_ctx_t * ctx,
640640
fd_stem_context_t * stem ) {
641641
/* Do not modify order! */
642642

643+
/* Now that the snapshot has been loaded in, we have to refresh the
644+
stake delegations since the manifest does not contain the full set
645+
of data required for the stake delegations. See
646+
fd_stake_delegations.h for why this is required. */
647+
648+
fd_refresh_stake_delegations( ctx->slot_ctx );
649+
643650
/* After both snapshots have been loaded in, we can determine if we should
644651
start distributing rewards. */
645652

src/flamenco/rewards/fd_rewards.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,21 @@ distribute_epoch_reward_to_stake_acc( fd_exec_slot_ctx_t * slot_ctx,
927927
stake_state->inner.stake.stake.delegation.stake = fd_ulong_sat_add( stake_state->inner.stake.stake.delegation.stake,
928928
reward_lamports );
929929

930+
/* The stake account has just been updated, so we need to update the
931+
stake delegations stored in the bank. */
932+
933+
fd_stake_delegations_t * stake_delegations = fd_bank_stake_delegations_locking_modify( slot_ctx->bank );
934+
fd_stake_delegations_update(
935+
stake_delegations,
936+
stake_pubkey,
937+
&stake_state->inner.stake.stake.delegation.voter_pubkey,
938+
stake_state->inner.stake.stake.delegation.stake,
939+
stake_state->inner.stake.stake.delegation.activation_epoch,
940+
stake_state->inner.stake.stake.delegation.deactivation_epoch,
941+
stake_state->inner.stake.stake.credits_observed,
942+
stake_state->inner.stake.stake.delegation.warmup_cooldown_rate );
943+
fd_bank_stake_delegations_end_locking_modify( slot_ctx->bank );
944+
930945
if( capture_ctx ) {
931946
fd_solcap_write_stake_account_payout( capture_ctx->capture,
932947
stake_pubkey,

src/flamenco/runtime/context/fd_exec_slot_ctx.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,14 @@ fd_exec_slot_ctx_recover( fd_exec_slot_ctx_t * slot_ctx,
318318
n = fd_delegation_pair_t_map_successor( stake_pool, n ) ) {
319319

320320
fd_stake_delegations_update(
321-
stake_delegations,
322-
&n->elem.account,
323-
&n->elem.delegation.voter_pubkey,
324-
n->elem.delegation.stake,
325-
n->elem.delegation.activation_epoch,
326-
n->elem.delegation.deactivation_epoch,
327-
n->elem.delegation.warmup_cooldown_rate );
321+
stake_delegations,
322+
&n->elem.account,
323+
&n->elem.delegation.voter_pubkey,
324+
n->elem.delegation.stake,
325+
n->elem.delegation.activation_epoch,
326+
n->elem.delegation.deactivation_epoch,
327+
0UL,
328+
n->elem.delegation.warmup_cooldown_rate );
328329
}
329330

330331
fd_bank_stake_delegations_end_locking_modify( slot_ctx->bank );

src/flamenco/runtime/fd_bank.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,9 @@ FD_PROTOTYPES_BEGIN
140140
/* Define additional fields to the bank struct here. If trying to add
141141
a CoW field to the bank, define a pool for it as done below. */
142142

143-
#define FD_BANKS_ITER(X) \
143+
#define FD_BANKS_ITER(X) \
144144
/* type, name, footprint, align, CoW, limit fork width, has lock */ \
145145
X(fd_clock_timestamp_votes_global_t, clock_timestamp_votes, 5000000UL, 128UL, 1, 0, 1 ) /* TODO: This needs to get sized out */ \
146-
X(fd_account_keys_global_t, stake_account_keys, 100000000UL, 128UL, 1, 0, 1 ) /* Supports roughly 3M stake accounts */ \
147146
X(fd_account_keys_global_t, vote_account_keys, 3200000UL, 128UL, 1, 0, 1 ) /* Supports roughly 100k vote accounts */ \
148147
X(fd_blockhashes_t, block_hash_queue, sizeof(fd_blockhashes_t), alignof(fd_blockhashes_t), 0, 0, 0 ) /* Block hash queue */ \
149148
X(fd_fee_rate_governor_t, fee_rate_governor, sizeof(fd_fee_rate_governor_t), alignof(fd_fee_rate_governor_t), 0, 0, 0 ) /* Fee rate governor */ \
@@ -245,12 +244,6 @@ FD_PROTOTYPES_BEGIN
245244
#undef POOL_NAME
246245
#undef POOL_T
247246

248-
#define POOL_NAME fd_bank_stake_account_keys_pool
249-
#define POOL_T fd_bank_stake_account_keys_t
250-
#include "../../util/tmpl/fd_pool.c"
251-
#undef POOL_NAME
252-
#undef POOL_T
253-
254247
#define POOL_NAME fd_bank_vote_account_keys_pool
255248
#define POOL_T fd_bank_vote_account_keys_t
256249
#include "../../util/tmpl/fd_pool.c"

src/flamenco/runtime/fd_runtime.c

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ fd_runtime_finalize_txn( fd_funk_t * funk,
14471447

14481448
if( dirty_stake_acc && 0==memcmp( fd_txn_account_get_owner( acc_rec ), &fd_solana_stake_program_id, sizeof(fd_pubkey_t) ) ) {
14491449
// TODO: does this correctly handle stake account close?
1450-
fd_store_stake_delegation( acc_rec, bank );
1450+
fd_update_stake_delegation( acc_rec, bank );
14511451
}
14521452

14531453
fd_runtime_save_account( funk, funk_txn, &txn_ctx->accounts[i], bank, txn_ctx->spad_wksp, capture_ctx );
@@ -1607,25 +1607,12 @@ fd_update_stake_delegations( fd_exec_slot_ctx_t * slot_ctx,
16071607
temp_info->stake_infos[idx].stake.delegation.stake,
16081608
temp_info->stake_infos[idx].stake.delegation.activation_epoch,
16091609
temp_info->stake_infos[idx].stake.delegation.deactivation_epoch,
1610+
temp_info->stake_infos[idx].stake.credits_observed,
16101611
temp_info->stake_infos[idx].stake.delegation.warmup_cooldown_rate
16111612
);
16121613
}
16131614

16141615
fd_bank_stake_delegations_end_locking_modify( slot_ctx->bank );
1615-
1616-
/* At the epoch boundary, release all of the stake account keys
1617-
because at this point all of the changes have been applied to the
1618-
stakes. */
1619-
fd_account_keys_global_t * stake_account_keys = fd_bank_stake_account_keys_locking_modify( slot_ctx->bank );
1620-
fd_account_keys_pair_t_mapnode_t * account_keys_pool = fd_account_keys_account_keys_pool_join( stake_account_keys );
1621-
fd_account_keys_pair_t_mapnode_t * account_keys_root = fd_account_keys_account_keys_root_join( stake_account_keys );
1622-
1623-
fd_account_keys_pair_t_map_release_tree( account_keys_pool, account_keys_root );
1624-
account_keys_root = NULL;
1625-
1626-
fd_account_keys_account_keys_pool_update( stake_account_keys, account_keys_pool );
1627-
fd_account_keys_account_keys_root_update( stake_account_keys, account_keys_root );
1628-
fd_bank_stake_account_keys_end_locking_modify( slot_ctx->bank );
16291616
}
16301617

16311618
/* Replace the stakes in T-2 (epoch_stakes) by the stakes at T-1 (next_epoch_stakes) */
@@ -1877,7 +1864,7 @@ fd_migrate_builtin_to_core_bpf( fd_exec_slot_ctx_t * slot_ctx,
18771864
FD_TXN_ACCOUNT_DECL( source_buffer_account );
18781865
fd_funk_rec_prepare_t source_buffer_prepare = {0};
18791866
if( FD_UNLIKELY( fd_txn_account_init_from_funk_mutable( source_buffer_account, source_buffer_address, slot_ctx->funk, slot_ctx->funk_txn, 0, 0UL, &source_buffer_prepare )!=FD_ACC_MGR_SUCCESS ) ) {
1880-
FD_LOG_WARNING(( "Buffer account %s does not exist, skipping migration...", FD_BASE58_ENC_32_ALLOCA( source_buffer_address ) ));
1867+
FD_LOG_NOTICE(( "Buffer account %s does not exist, skipping migration...", FD_BASE58_ENC_32_ALLOCA( source_buffer_address ) ));
18811868
return;
18821869
}
18831870

@@ -2559,6 +2546,7 @@ fd_runtime_init_bank_from_genesis( fd_exec_slot_ctx_t * slot_ctx,
25592546
stake_state.inner.stake.stake.delegation.stake,
25602547
stake_state.inner.stake.stake.delegation.activation_epoch,
25612548
stake_state.inner.stake.stake.delegation.deactivation_epoch,
2549+
stake_state.inner.stake.stake.credits_observed,
25622550
stake_state.inner.stake.stake.delegation.warmup_cooldown_rate );
25632551

25642552
} FD_SPAD_FRAME_END;
@@ -2855,18 +2843,8 @@ fd_runtime_read_genesis( fd_exec_slot_ctx_t * slot_ctx,
28552843
}
28562844
}
28572845

2858-
2859-
fd_account_keys_global_t * stake_account_keys = fd_bank_stake_account_keys_locking_modify( slot_ctx->bank );
2860-
uchar * pool_mem = (uchar *)fd_ulong_align_up( (ulong)stake_account_keys + sizeof(fd_account_keys_global_t), fd_account_keys_pair_t_map_align() );
2861-
fd_account_keys_pair_t_mapnode_t * stake_account_keys_pool = fd_account_keys_pair_t_map_join( fd_account_keys_pair_t_map_new( pool_mem, 100000UL ) );
2862-
fd_account_keys_pair_t_mapnode_t * stake_account_keys_root = NULL;
2863-
2864-
fd_account_keys_account_keys_pool_update( stake_account_keys, stake_account_keys_pool );
2865-
fd_account_keys_account_keys_root_update( stake_account_keys, stake_account_keys_root );
2866-
fd_bank_stake_account_keys_end_locking_modify( slot_ctx->bank );
2867-
28682846
fd_account_keys_global_t * vote_account_keys = fd_bank_vote_account_keys_locking_modify( slot_ctx->bank );
2869-
pool_mem = (uchar *)fd_ulong_align_up( (ulong)vote_account_keys + sizeof(fd_account_keys_global_t), fd_account_keys_pair_t_map_align() );
2847+
uchar * pool_mem = (uchar *)fd_ulong_align_up( (ulong)vote_account_keys + sizeof(fd_account_keys_global_t), fd_account_keys_pair_t_map_align() );
28702848
fd_account_keys_pair_t_mapnode_t * vote_account_keys_pool = fd_account_keys_pair_t_map_join( fd_account_keys_pair_t_map_new( pool_mem, 100000UL ) );
28712849
fd_account_keys_pair_t_mapnode_t * vote_account_keys_root = NULL;
28722850

src/flamenco/runtime/program/fd_stake_program.c

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -3289,114 +3289,3 @@ fd_stake_activating_and_deactivating( fd_delegation_t const * self,
32893289
return stake_activating_and_deactivating(
32903290
self, target_epoch, stake_history, new_rate_activation_epoch );
32913291
}
3292-
3293-
/* Removes stake delegation from epoch stakes and updates vote account */
3294-
static void
3295-
fd_stakes_remove_stake_delegation( fd_txn_account_t * stake_account,
3296-
fd_bank_t * bank ) {
3297-
3298-
fd_account_keys_global_t * stake_account_keys = fd_bank_stake_account_keys_locking_modify( bank );
3299-
fd_account_keys_pair_t_mapnode_t * account_keys_pool = fd_account_keys_account_keys_pool_join( stake_account_keys );
3300-
fd_account_keys_pair_t_mapnode_t * account_keys_root = fd_account_keys_account_keys_root_join( stake_account_keys );
3301-
3302-
fd_account_keys_pair_t_mapnode_t key;
3303-
fd_memcpy( key.elem.key.uc, stake_account->pubkey->uc, sizeof(fd_pubkey_t) );
3304-
if( FD_UNLIKELY( account_keys_pool==NULL ) ) {
3305-
/* TODO: Should this be a LOG_ERR/LOG_CRIT? */
3306-
fd_bank_stake_account_keys_end_locking_modify( bank );
3307-
FD_LOG_DEBUG(("Stake accounts pool does not exist"));
3308-
return;
3309-
}
3310-
fd_account_keys_pair_t_mapnode_t * entry = fd_account_keys_pair_t_map_find( account_keys_pool, account_keys_root, &key );
3311-
if( FD_UNLIKELY( entry ) ) {
3312-
fd_account_keys_pair_t_map_remove( account_keys_pool, &account_keys_root, entry );
3313-
// TODO: do we need a release here?
3314-
}
3315-
3316-
fd_account_keys_account_keys_pool_update( stake_account_keys, account_keys_pool );
3317-
fd_account_keys_account_keys_root_update( stake_account_keys, account_keys_root );
3318-
3319-
fd_bank_stake_account_keys_end_locking_modify( bank );
3320-
}
3321-
3322-
/* Updates stake delegation in epoch stakes */
3323-
static void
3324-
fd_stakes_upsert_stake_delegation( fd_txn_account_t * stake_account,
3325-
fd_bank_t * bank ) {
3326-
FD_TEST( fd_txn_account_get_lamports( stake_account )!=0 );
3327-
3328-
fd_delegation_pair_t_mapnode_t key;
3329-
fd_memcpy(&key.elem.account, stake_account->pubkey->uc, sizeof(fd_pubkey_t));
3330-
3331-
fd_account_keys_global_t * stake_account_keys = fd_bank_stake_account_keys_locking_modify( bank );
3332-
3333-
fd_account_keys_pair_t_mapnode_t * account_keys_pool = NULL;
3334-
fd_account_keys_pair_t_mapnode_t * account_keys_root = NULL;
3335-
if( stake_account_keys->account_keys_pool_offset==0 ) {
3336-
uchar * pool_mem = (uchar *)fd_ulong_align_up( (ulong)stake_account_keys + sizeof(fd_account_keys_global_t), fd_account_keys_pair_t_map_align() );
3337-
account_keys_pool = fd_account_keys_pair_t_map_join( fd_account_keys_pair_t_map_new( pool_mem, 100000UL ) );
3338-
account_keys_root = NULL;
3339-
} else {
3340-
account_keys_pool = fd_account_keys_account_keys_pool_join( stake_account_keys );
3341-
account_keys_root = fd_account_keys_account_keys_root_join( stake_account_keys );
3342-
}
3343-
3344-
/* TODO: When stake delegations are combined with account keys,
3345-
actually create the stake delegation map here. */
3346-
fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_locking_query( bank );
3347-
fd_stake_delegation_t const * entry = !!stake_delegations ? fd_stake_delegations_query( stake_delegations, stake_account->pubkey ) : NULL;
3348-
3349-
if( FD_UNLIKELY( !entry ) ) {
3350-
fd_account_keys_pair_t_mapnode_t key;
3351-
fd_memcpy( key.elem.key.uc, stake_account->pubkey->uc, sizeof(fd_pubkey_t) );
3352-
if( account_keys_pool==NULL ) {
3353-
FD_LOG_DEBUG(( "Stake accounts pool does not exist" ));
3354-
fd_bank_stake_account_keys_end_locking_query( bank );
3355-
fd_bank_stake_delegations_end_locking_query( bank );
3356-
return;
3357-
}
3358-
fd_account_keys_pair_t_mapnode_t * stake_entry = fd_account_keys_pair_t_map_find( account_keys_pool, account_keys_root, &key );
3359-
if( stake_entry ) {
3360-
stake_entry->elem.exists = 1;
3361-
} else {
3362-
fd_account_keys_pair_t_mapnode_t * new_node = fd_account_keys_pair_t_map_acquire( account_keys_pool );
3363-
ulong size = fd_account_keys_pair_t_map_size( account_keys_pool, account_keys_root );
3364-
if( new_node==NULL ) {
3365-
FD_LOG_ERR(("Stake accounts keys map full %lu", size));
3366-
}
3367-
new_node->elem.exists = 1;
3368-
fd_memcpy( new_node->elem.key.uc, stake_account->pubkey->uc, sizeof(fd_pubkey_t) );
3369-
fd_account_keys_pair_t_map_insert( account_keys_pool, &account_keys_root, new_node );
3370-
}
3371-
}
3372-
3373-
fd_account_keys_account_keys_pool_update( stake_account_keys, account_keys_pool );
3374-
fd_account_keys_account_keys_root_update( stake_account_keys, account_keys_root );
3375-
3376-
fd_bank_stake_account_keys_end_locking_modify( bank );
3377-
3378-
fd_bank_stake_delegations_end_locking_query( bank );
3379-
}
3380-
3381-
void
3382-
fd_store_stake_delegation( fd_txn_account_t * stake_account,
3383-
fd_bank_t * bank ) {
3384-
fd_pubkey_t const * owner = fd_txn_account_get_owner( stake_account );
3385-
3386-
if( memcmp( owner->uc, fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) {
3387-
return;
3388-
}
3389-
3390-
int is_empty = fd_txn_account_get_lamports( stake_account )==0;
3391-
int is_uninit = 1;
3392-
if( fd_txn_account_get_data_len( stake_account )>=4UL ) {
3393-
uint prefix = FD_LOAD( uint, fd_txn_account_get_data( stake_account ) );
3394-
is_uninit = ( prefix==fd_stake_state_v2_enum_uninitialized );
3395-
}
3396-
3397-
if( is_empty || is_uninit ) {
3398-
fd_stakes_remove_stake_delegation( stake_account, bank );
3399-
} else {
3400-
fd_stakes_upsert_stake_delegation( stake_account, bank );
3401-
}
3402-
}

src/flamenco/runtime/program/fd_stake_program.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ fd_stake_activating_and_deactivating( fd_delegation_t const * self,
4848
fd_stake_history_t const * stake_history,
4949
ulong * new_rate_activation_epoch );
5050

51-
void
52-
fd_store_stake_delegation( fd_txn_account_t * stake_account,
53-
fd_bank_t * bank );
54-
5551
FD_PROTOTYPES_END
5652

5753
#endif /* HEADER_fd_src_flamenco_runtime_program_fd_stake_program_h */

src/flamenco/runtime/test_bank.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ main( int argc, char ** argv ) {
124124

125125
FD_TEST( fd_bank_vote_account_keys_pool_free( fd_bank_get_vote_account_keys_pool( bank9 ) ) == 15UL );
126126

127-
fd_account_keys_global_t * keys2 = fd_bank_stake_account_keys_locking_modify( bank9 );
128-
keys2->account_keys_pool_offset = 101UL;
129-
keys2->account_keys_root_offset = 101UL;
130-
fd_bank_stake_account_keys_end_locking_modify( bank9 );
127+
fd_stake_delegations_t * keys2 = fd_bank_stake_delegations_locking_modify( bank9 );
128+
keys2->magic = 101UL;
129+
fd_bank_stake_delegations_end_locking_modify( bank9 );
131130

132131
/* Verify that the bank is published and that it is indeed bank7 */
133132

@@ -176,10 +175,9 @@ main( int argc, char ** argv ) {
176175
FD_TEST( keys3->account_keys_root_offset == 100UL );
177176
fd_bank_vote_account_keys_end_locking_query( bank11 );
178177

179-
fd_account_keys_global_t const * keys4 = fd_bank_stake_account_keys_locking_query( bank11 );
180-
FD_TEST( keys4->account_keys_pool_offset == 101UL );
181-
FD_TEST( keys4->account_keys_root_offset == 101UL );
182-
fd_bank_stake_account_keys_end_locking_query( bank11 );
178+
fd_stake_delegations_t const * keys4 = fd_bank_stake_delegations_locking_query( bank11 );
179+
FD_TEST( keys4->magic == 101UL );
180+
fd_bank_stake_delegations_end_locking_query( bank11 );
183181

184182
keys = fd_bank_vote_account_keys_locking_modify( bank11 );
185183
keys->account_keys_pool_offset = 200UL;
@@ -219,10 +217,9 @@ main( int argc, char ** argv ) {
219217
FD_TEST( keys3->account_keys_root_offset == 200UL );
220218
fd_bank_vote_account_keys_end_locking_query( bank11 );
221219

222-
keys4 = fd_bank_stake_account_keys_locking_query( bank11 );
223-
FD_TEST( keys4->account_keys_pool_offset == 101UL );
224-
FD_TEST( keys4->account_keys_root_offset == 101UL );
225-
fd_bank_stake_account_keys_end_locking_query( bank11 );
220+
keys4 = fd_bank_stake_delegations_locking_query( bank11 );
221+
FD_TEST( keys4->magic == 101UL );
222+
fd_bank_stake_delegations_end_locking_query( bank11 );
226223

227224
votes_const = fd_bank_clock_timestamp_votes_locking_query( bank11 );
228225
FD_TEST( votes->votes_pool_offset == 102UL );
@@ -247,10 +244,9 @@ main( int argc, char ** argv ) {
247244
FD_TEST( keys3->account_keys_root_offset == 100UL );
248245
fd_bank_vote_account_keys_end_locking_query( bank11 );
249246

250-
keys4 = fd_bank_stake_account_keys_locking_query( bank11 );
251-
FD_TEST( keys4->account_keys_pool_offset == 101UL );
252-
FD_TEST( keys4->account_keys_root_offset == 101UL );
253-
fd_bank_stake_account_keys_end_locking_query( bank11 );
247+
keys4 = fd_bank_stake_delegations_locking_query( bank11 );
248+
FD_TEST( keys4->magic == 101UL );
249+
fd_bank_stake_delegations_end_locking_query( bank11 );
254250

255251
votes_const = fd_bank_clock_timestamp_votes_locking_query( bank11 );
256252
FD_TEST( !votes_const );

0 commit comments

Comments
 (0)