Skip to content

Commit 59fc48a

Browse files
runtime: replacing fd_stakes_t with handrolled stake delegation type
1 parent daaed25 commit 59fc48a

17 files changed

+888
-254
lines changed

config/extra/with-handholding.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ CPPFLAGS+=-DFD_TXN_HANDHOLDING=1
99
CPPFLAGS+=-DFD_FUNK_HANDHOLDING=1
1010
CPPFLAGS+=-DFD_RUNTIME_ERR_HANDHOLDING=1
1111
CPPFLAGS+=-DFD_FOREST_USE_HANDHOLDING=1
12+
CPPFLAGS+=-DFD_STAKES_USE_HANDHOLDING=1

src/discof/replay/fd_replay_tile.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,7 @@ init_after_snapshot( fd_replay_tile_ctx_t * ctx,
689689
memset( block_id, 0, sizeof(fd_hash_t) );
690690
block_id->key[0] = UCHAR_MAX; /* TODO: would be good to have the actual block id of the snapshot slot */
691691

692-
fd_stakes_global_t const * stakes = fd_bank_stakes_locking_query( ctx->slot_ctx->bank );
693-
fd_vote_accounts_global_t const * vote_accounts = &stakes->vote_accounts;
692+
fd_vote_accounts_global_t const * vote_accounts = fd_bank_curr_epoch_stakes_locking_query( ctx->slot_ctx->bank );
694693

695694
fd_vote_accounts_pair_global_t_mapnode_t * vote_accounts_pool = fd_vote_accounts_vote_accounts_pool_join( vote_accounts );
696695
fd_vote_accounts_pair_global_t_mapnode_t * vote_accounts_root = fd_vote_accounts_vote_accounts_root_join( vote_accounts );
@@ -726,7 +725,7 @@ init_after_snapshot( fd_replay_tile_ctx_t * ctx,
726725
}
727726
bank_hash_cmp->watermark = snapshot_slot;
728727

729-
fd_bank_stakes_end_locking_query( ctx->slot_ctx->bank );
728+
fd_bank_curr_epoch_stakes_end_locking_query( ctx->slot_ctx->bank );
730729

731730
ulong root = snapshot_slot;
732731
if( FD_LIKELY( root > fd_fseq_query( ctx->published_wmark ) ) ) {

src/flamenco/rewards/fd_epoch_rewards.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ FD_STATIC_ASSERT( FD_REWARDS_MAX_PARTITIONS <= FD_RUNTIME_SLOTS_PER_EPOCH / MAX_
5757
total footprint: 4224 bytes + 384 MB + 12360 bytes = 384016584 bytes
5858
*/
5959
#define FD_EPOCH_REWARDS_FOOTPRINT (384016584UL)
60-
/* TODO: Add some static asserts to validate this calculation*/
60+
/* TODO: Add some static asserts to validate this calculation */
6161

6262
#define FD_EPOCH_REWARDS_ALIGN (128UL)
6363

src/flamenco/rewards/fd_rewards.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,15 +1158,12 @@ fd_rewards_recalculate_partitioned_rewards( fd_exec_slot_ctx_t * slot_ctx,
11581158
fd_point_value_t point_value = { .points = epoch_rewards->total_points,
11591159
.rewards = epoch_rewards->total_rewards };
11601160

1161-
/* Populate vote and stake state info from vote and stakes cache for the stake vote rewards calculation */
1162-
fd_stakes_global_t const * stakes = fd_bank_stakes_locking_query( slot_ctx->bank );
1163-
fd_delegation_pair_t_mapnode_t * stake_delegations_pool = fd_stakes_stake_delegations_pool_join( stakes );
1164-
fd_delegation_pair_t_mapnode_t * stake_delegations_root = fd_stakes_stake_delegations_root_join( stakes );
1161+
fd_stake_delegations_t * stake_delegations = fd_stake_delegations_join( fd_bank_stake_delegations_locking_modify( slot_ctx->bank ) );
11651162

11661163
fd_epoch_info_t epoch_info = {0};
11671164
fd_epoch_info_new( &epoch_info );
11681165

1169-
ulong stake_delegation_sz = fd_delegation_pair_t_map_size( stake_delegations_pool, stake_delegations_root );
1166+
ulong stake_delegation_sz = fd_stake_delegations_cnt( stake_delegations );
11701167
epoch_info.stake_infos_len = 0UL;
11711168
epoch_info.stake_infos = fd_spad_alloc( runtime_spad, FD_EPOCH_INFO_PAIR_ALIGN, sizeof(fd_epoch_info_pair_t)*stake_delegation_sz );
11721169

@@ -1176,15 +1173,16 @@ fd_rewards_recalculate_partitioned_rewards( fd_exec_slot_ctx_t * slot_ctx,
11761173
.deactivating = 0UL
11771174
};
11781175

1179-
fd_accumulate_stake_infos( slot_ctx,
1180-
stakes,
1181-
stake_history,
1182-
new_warmup_cooldown_rate_epoch,
1183-
&_accumulator,
1184-
&epoch_info,
1185-
runtime_spad );
1176+
fd_accumulate_stake_infos(
1177+
slot_ctx,
1178+
stake_delegations,
1179+
stake_history,
1180+
new_warmup_cooldown_rate_epoch,
1181+
&_accumulator,
1182+
&epoch_info,
1183+
runtime_spad );
11861184

1187-
fd_bank_stakes_end_locking_query( slot_ctx->bank );
1185+
fd_bank_stake_delegations_end_locking_modify( slot_ctx->bank );
11881186

11891187
/* NOTE: this is just a workaround for now to correctly populate epoch_info. */
11901188
fd_populate_vote_accounts( slot_ctx,

src/flamenco/runtime/context/fd_exec_slot_ctx.c

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,53 @@ fd_exec_slot_ctx_delete( void * mem ) {
8888
fd_exec_slot_ctx_t *
8989
fd_exec_slot_ctx_recover( fd_exec_slot_ctx_t * slot_ctx,
9090
fd_solana_manifest_global_t const * manifest ) {
91-
ulong stakes_sz = fd_stakes_size_global( &manifest->bank.stakes );
92-
fd_stakes_global_t * stakes = fd_bank_stakes_locking_modify( slot_ctx->bank );
93-
fd_memcpy( stakes, &manifest->bank.stakes, stakes_sz );
94-
/* Verify stakes */
9591

96-
fd_bank_stakes_end_locking_modify( slot_ctx->bank );
92+
fd_stakes_global_t const * manifest_stakes = &manifest->bank.stakes;
93+
fd_vote_accounts_global_t const * manifest_vote_accounts = &manifest_stakes->vote_accounts;
94+
fd_vote_accounts_pair_global_t_mapnode_t * manifest_vote_accounts_pool = fd_vote_accounts_vote_accounts_pool_join( manifest_vote_accounts );
95+
fd_vote_accounts_pair_global_t_mapnode_t * manifest_vote_accounts_root = fd_vote_accounts_vote_accounts_root_join( manifest_vote_accounts );
96+
97+
fd_vote_accounts_global_t * curr_epoch_stakes = fd_bank_curr_epoch_stakes_locking_modify( slot_ctx->bank );
98+
uchar * curr_epoch_stakes_pool_mem = (uchar *)fd_ulong_align_up( (ulong)curr_epoch_stakes + sizeof(fd_vote_accounts_global_t), fd_vote_accounts_pair_global_t_map_align() );
99+
fd_vote_accounts_pair_global_t_mapnode_t * curr_epoch_stakes_pool = fd_vote_accounts_pair_global_t_map_join( fd_vote_accounts_pair_global_t_map_new( curr_epoch_stakes_pool_mem, 50000UL ) );
100+
fd_vote_accounts_pair_global_t_mapnode_t * curr_epoch_stakes_root = NULL;
101+
uchar * acc_region_curr = (uchar *)fd_ulong_align_up( (ulong)curr_epoch_stakes_pool + fd_vote_accounts_pair_global_t_map_footprint( 50000UL ), 8UL );
102+
103+
for( fd_vote_accounts_pair_global_t_mapnode_t * n = fd_vote_accounts_pair_global_t_map_minimum( manifest_vote_accounts_pool, manifest_vote_accounts_root );
104+
n;
105+
n = fd_vote_accounts_pair_global_t_map_successor( manifest_vote_accounts_pool, n ) ) {
106+
107+
fd_vote_accounts_pair_global_t_mapnode_t * elem = fd_vote_accounts_pair_global_t_map_acquire( curr_epoch_stakes_pool );
108+
FD_TEST( elem );
109+
110+
elem->elem.stake = n->elem.stake;
111+
elem->elem.key = n->elem.key;
112+
113+
elem->elem.value.lamports = n->elem.value.lamports;
114+
elem->elem.value.data_len = 0UL;
115+
elem->elem.value.data_offset = 0UL;
116+
elem->elem.value.owner = n->elem.value.owner;
117+
elem->elem.value.executable = n->elem.value.executable;
118+
elem->elem.value.rent_epoch = n->elem.value.rent_epoch;
119+
120+
elem->elem.value.data_offset = (ulong)(acc_region_curr - (uchar *)&elem->elem.value);;
121+
elem->elem.value.data_len = n->elem.value.data_len;
122+
123+
uchar * manifest_data = fd_solana_account_data_join( &n->elem.value );
124+
memcpy( acc_region_curr, manifest_data, n->elem.value.data_len );
125+
acc_region_curr += n->elem.value.data_len;
126+
127+
fd_vote_accounts_pair_global_t_map_insert(
128+
curr_epoch_stakes_pool,
129+
&curr_epoch_stakes_root,
130+
elem );
131+
}
132+
133+
fd_vote_accounts_vote_accounts_pool_update( curr_epoch_stakes, curr_epoch_stakes_pool );
134+
fd_vote_accounts_vote_accounts_root_update( curr_epoch_stakes, curr_epoch_stakes_root );
135+
fd_bank_curr_epoch_stakes_end_locking_modify( slot_ctx->bank );
136+
137+
fd_bank_epoch_set( slot_ctx->bank, manifest->bank.epoch );
97138

98139
/* Move EpochStakes */
99140
do {
@@ -174,7 +215,7 @@ fd_exec_slot_ctx_recover( fd_exec_slot_ctx_t * slot_ctx,
174215
fd_vote_accounts_pair_global_t_mapnode_t * epoch_stakes_pool = fd_vote_accounts_pair_global_t_map_join( fd_vote_accounts_pair_global_t_map_new( epoch_stakes_pool_mem, 50000UL ) );
175216
fd_vote_accounts_pair_global_t_mapnode_t * epoch_stakes_root = NULL;
176217

177-
uchar * acc_region_curr = (uchar *)fd_ulong_align_up( (ulong)epoch_stakes_pool + fd_vote_accounts_pair_global_t_map_footprint( 50000UL ), 8UL );
218+
acc_region_curr = (uchar *)fd_ulong_align_up( (ulong)epoch_stakes_pool + fd_vote_accounts_pair_global_t_map_footprint( 50000UL ), 8UL );
178219

179220
for( fd_vote_accounts_pair_global_t_mapnode_t * n = fd_vote_accounts_pair_global_t_map_minimum(
180221
vote_accounts_curr_stakes_pool,
@@ -261,6 +302,33 @@ fd_exec_slot_ctx_recover( fd_exec_slot_ctx_t * slot_ctx,
261302

262303
} while(0);
263304

305+
306+
/* Copy the stakes delegations */
307+
308+
fd_stake_delegations_t * stake_delegations = fd_bank_stake_delegations_locking_modify( slot_ctx->bank );
309+
stake_delegations = fd_stake_delegations_join( fd_stake_delegations_new( stake_delegations, FD_RUNTIME_MAX_STAKE_ACCOUNTS ) );
310+
311+
fd_stakes_global_t const * stakes_global = &manifest->bank.stakes;
312+
313+
fd_delegation_pair_t_mapnode_t * stake_pool = fd_stakes_stake_delegations_pool_join( stakes_global );
314+
fd_delegation_pair_t_mapnode_t * stake_root = fd_stakes_stake_delegations_root_join( stakes_global );
315+
316+
for( fd_delegation_pair_t_mapnode_t * n = fd_delegation_pair_t_map_minimum( stake_pool, stake_root );
317+
n;
318+
n = fd_delegation_pair_t_map_successor( stake_pool, n ) ) {
319+
320+
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 );
328+
}
329+
330+
fd_bank_stake_delegations_end_locking_modify( slot_ctx->bank );
331+
264332
return slot_ctx;
265333
}
266334

@@ -345,9 +413,3 @@ fd_exec_slot_ctx_recover_status_cache( fd_exec_slot_ctx_t * ctx,
345413
} FD_SPAD_FRAME_END;
346414
return ctx;
347415
}
348-
349-
ulong
350-
fd_bank_epoch_get( fd_bank_t const * bank ) {
351-
fd_epoch_schedule_t epoch_schedule = fd_bank_epoch_schedule_get( bank );
352-
return fd_slot_to_epoch( &epoch_schedule, fd_bank_slot_get( bank ), NULL );
353-
}

src/flamenco/runtime/fd_bank.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../leaders/fd_leaders.h"
66
#include "../features/fd_features.h"
77
#include "../rewards/fd_epoch_rewards.h"
8+
#include "../stakes/fd_stake_delegations.h"
89
#include "../fd_rwlock.h"
910
#include "fd_runtime_const.h"
1011
#include "fd_blockhashes.h"
@@ -187,11 +188,11 @@ FD_PROTOTYPES_BEGIN
187188
/* E-1 and they determined the leader schedule for epoch */ \
188189
/* E+1. */ \
189190
X(fd_vote_accounts_global_t, epoch_stakes, 200000000UL, 128UL, 1, 0, 1 ) /* Epoch stakes ~4K per account * 50k vote accounts */ \
191+
X(fd_vote_accounts_global_t, curr_epoch_stakes, 200000000UL, 128UL, 1, 0, 1 ) /* Stakes being accumulated in current epoch */ \
190192
X(fd_epoch_rewards_t, epoch_rewards, FD_EPOCH_REWARDS_FOOTPRINT, FD_EPOCH_REWARDS_ALIGN, 1, 1, 1 ) /* Epoch rewards */ \
191193
X(fd_epoch_leaders_t, epoch_leaders, FD_RUNTIME_MAX_EPOCH_LEADERS, FD_EPOCH_LEADERS_ALIGN, 1, 1, 1 ) /* Epoch leaders. If our system supports 100k vote accs, */ \
192194
/* then there can be 100k unique leaders in the worst */ \
193195
/* case. We also can assume 432k slots per epoch. */ \
194-
X(fd_stakes_global_t, stakes, 400000000UL, 128UL, 1, 0, 1 ) /* Stakes */ \
195196
X(fd_features_t, features, sizeof(fd_features_t), alignof(fd_features_t), 0, 0, 0 ) /* Features */ \
196197
X(ulong, txn_count, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Transaction count */ \
197198
X(ulong, nonvote_txn_count, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Nonvote transaction count */ \
@@ -201,7 +202,9 @@ FD_PROTOTYPES_BEGIN
201202
X(ulong, part_width, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Part width */ \
202203
X(ulong, slots_per_epoch, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Slots per epoch */ \
203204
X(ulong, shred_cnt, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Shred count */ \
204-
X(int, enable_exec_recording, sizeof(int), alignof(int), 0, 0, 0 ) /* Enable exec recording */
205+
X(int, enable_exec_recording, sizeof(int), alignof(int), 0, 0, 0 ) /* Enable exec recording */ \
206+
X(fd_stake_delegations_t, stake_delegations, FD_STAKE_DELEGATIONS_FOOTPRINT, FD_STAKE_DELEGATIONS_ALIGN, 1, 0, 1 ) /* Stake delegations */ \
207+
X(ulong, epoch, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Epoch */
205208

206209
/* Invariant Every CoW field must have a rw-lock */
207210
#define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
@@ -272,14 +275,20 @@ FD_PROTOTYPES_BEGIN
272275
#undef POOL_NAME
273276
#undef POOL_T
274277

275-
#define POOL_NAME fd_bank_stakes_pool
276-
#define POOL_T fd_bank_stakes_t
278+
#define POOL_NAME fd_bank_epoch_rewards_pool
279+
#define POOL_T fd_bank_epoch_rewards_t
277280
#include "../../util/tmpl/fd_pool.c"
278281
#undef POOL_NAME
279282
#undef POOL_T
280283

281-
#define POOL_NAME fd_bank_epoch_rewards_pool
282-
#define POOL_T fd_bank_epoch_rewards_t
284+
#define POOL_NAME fd_bank_stake_delegations_pool
285+
#define POOL_T fd_bank_stake_delegations_t
286+
#include "../../util/tmpl/fd_pool.c"
287+
#undef POOL_NAME
288+
#undef POOL_T
289+
290+
#define POOL_NAME fd_bank_curr_epoch_stakes_pool
291+
#define POOL_T fd_bank_curr_epoch_stakes_t
283292
#include "../../util/tmpl/fd_pool.c"
284293
#undef POOL_NAME
285294
#undef POOL_T
@@ -471,9 +480,6 @@ fd_bank_slot_get( fd_bank_t const * bank ) {
471480
return bank->slot_;
472481
}
473482

474-
ulong
475-
fd_bank_epoch_get( fd_bank_t const * bank );
476-
477483
/* Simple getters and setters for members of fd_banks_t.*/
478484

479485
static inline fd_bank_t *

0 commit comments

Comments
 (0)