Line data Source code
1 : #include "fd_stake_ci.h"
2 : #include "fd_shred_dest.h"
3 : #include "../../util/net/fd_ip4.h" /* Just for debug */
4 :
5 : #define SORT_NAME sort_pubkey
6 0 : #define SORT_KEY_T fd_shred_dest_weighted_t
7 0 : #define SORT_BEFORE(a,b) (memcmp( (a).pubkey.uc, (b).pubkey.uc, 32UL )>0)
8 : #include "../../util/tmpl/fd_sort.c"
9 :
10 : #define SORT_NAME sort_weights_by_stake_id
11 0 : #define SORT_KEY_T fd_stake_weight_t
12 0 : #define SORT_BEFORE(a,b) ((a).stake > (b).stake ? 1 : ((a).stake < (b).stake ? 0 : memcmp( (a).key.uc, (b).key.uc, 32UL )>0))
13 : #include "../../util/tmpl/fd_sort.c"
14 :
15 : #define SORT_NAME sort_weights_by_id
16 0 : #define SORT_KEY_T fd_stake_weight_t
17 0 : #define SORT_BEFORE(a,b) (memcmp( (a).key.uc, (b).key.uc, 32UL )>0)
18 : #include "../../util/tmpl/fd_sort.c"
19 :
20 : /* We don't have or need real contact info for the local validator, but
21 : we want to be able to distinguish it from staked nodes with no
22 : contact info. */
23 0 : #define SELF_DUMMY_IP 1U
24 :
25 : void *
26 : fd_stake_ci_new( void * mem,
27 0 : fd_pubkey_t const * identity_key ) {
28 0 : fd_stake_ci_t * info = (fd_stake_ci_t *)mem;
29 :
30 0 : fd_vote_stake_weight_t dummy_stakes[ 1 ] = {{ .vote_key = {{0}}, .id_key = {{0}}, .stake = 1UL }};
31 0 : fd_shred_dest_weighted_t dummy_dests[ 1 ] = {{ .pubkey = *identity_key, .ip4 = SELF_DUMMY_IP }};
32 :
33 : /* Initialize first 2 to satisfy invariants */
34 0 : info->vote_stake_weight[ 0 ] = dummy_stakes[ 0 ];
35 0 : info->shred_dest [ 0 ] = dummy_dests [ 0 ];
36 0 : for( ulong i=0UL; i<2UL; i++ ) {
37 0 : fd_per_epoch_info_t * ei = info->epoch_info + i;
38 0 : ei->epoch = i;
39 0 : ei->start_slot = 0UL;
40 0 : ei->slot_cnt = 0UL;
41 0 : ei->excluded_stake = 0UL;
42 0 : ei->vote_keyed_lsched = 0UL;
43 :
44 0 : ei->lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( ei->_lsched, 0UL, 0UL, 1UL, 1UL, info->vote_stake_weight, 0UL, ei->vote_keyed_lsched ) );
45 0 : ei->sdest = fd_shred_dest_join ( fd_shred_dest_new ( ei->_sdest, info->shred_dest, 1UL, ei->lsched, identity_key, 0UL ) );
46 0 : }
47 0 : info->identity_key[ 0 ] = *identity_key;
48 :
49 0 : return (void *)info;
50 0 : }
51 :
52 0 : fd_stake_ci_t * fd_stake_ci_join( void * mem ) { return (fd_stake_ci_t *)mem; }
53 :
54 0 : void * fd_stake_ci_leave ( fd_stake_ci_t * info ) { return (void *)info; }
55 0 : void * fd_stake_ci_delete( void * mem ) { return mem; }
56 :
57 :
58 : void
59 : fd_stake_ci_stake_msg_init( fd_stake_ci_t * info,
60 0 : fd_stake_weight_msg_t const * msg ) {
61 0 : if( FD_UNLIKELY( msg->staked_cnt > MAX_SHRED_DESTS ) )
62 0 : FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu stakes in it,"
63 0 : " but the maximum allowed is %lu", msg->staked_cnt, MAX_SHRED_DESTS ));
64 :
65 0 : info->scratch->epoch = msg->epoch;
66 0 : info->scratch->start_slot = msg->start_slot;
67 0 : info->scratch->slot_cnt = msg->slot_cnt;
68 0 : info->scratch->staked_cnt = msg->staked_cnt;
69 0 : info->scratch->excluded_stake = msg->excluded_stake;
70 0 : info->scratch->vote_keyed_lsched = msg->vote_keyed_lsched;
71 :
72 0 : fd_memcpy( info->vote_stake_weight, msg->weights, msg->staked_cnt*sizeof(fd_vote_stake_weight_t) );
73 0 : }
74 :
75 : void
76 : fd_stake_ci_epoch_msg_init( fd_stake_ci_t * info,
77 0 : fd_epoch_info_msg_t const * msg ) {
78 0 : if( FD_UNLIKELY( msg->staked_cnt > MAX_COMPRESSED_STAKE_WEIGHTS ) )
79 0 : FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu stakes in it,"
80 0 : " but the maximum allowed is %lu", msg->staked_cnt, MAX_COMPRESSED_STAKE_WEIGHTS ));
81 :
82 0 : info->scratch->epoch = msg->epoch;
83 0 : info->scratch->start_slot = msg->start_slot;
84 0 : info->scratch->slot_cnt = msg->slot_cnt;
85 0 : info->scratch->staked_cnt = msg->staked_cnt;
86 0 : info->scratch->excluded_stake = msg->excluded_stake;
87 0 : info->scratch->vote_keyed_lsched = msg->vote_keyed_lsched;
88 :
89 0 : fd_memcpy( info->vote_stake_weight, msg->weights, msg->staked_cnt*sizeof(fd_vote_stake_weight_t) );
90 0 : }
91 :
92 : static inline void
93 0 : log_summary( char const * msg, fd_stake_ci_t * info ) {
94 : #if 0
95 : fd_per_epoch_info_t const * ei = info->epoch_info;
96 : FD_LOG_NOTICE(( "Dumping stake contact information because %s", msg ));
97 : for( ulong i=0UL; i<2UL; i++ ) {
98 : FD_LOG_NOTICE(( " Dumping shred destination details for epoch %lu, slots [%lu, %lu)", ei[i].epoch, ei[i].start_slot, ei[i].start_slot+ei[i].slot_cnt ));
99 : fd_shred_dest_t * sdest = ei[i].sdest;
100 : for( fd_shred_dest_idx_t j=0; j<(fd_shred_dest_idx_t)fd_shred_dest_cnt_all( sdest ); j++ ) {
101 : fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( sdest, j );
102 : FD_LOG_NOTICE(( " %16lx %20lu " FD_IP4_ADDR_FMT " %hu ", *(ulong *)dest->pubkey.uc, dest->stake_lamports, FD_IP4_ADDR_FMT_ARGS( dest->ip4 ), dest->port ));
103 : }
104 : }
105 : #else
106 0 : (void)msg;
107 0 : (void)info;
108 0 : #endif
109 0 : }
110 :
111 : ulong
112 : compute_id_weights_from_vote_weights( fd_stake_weight_t * stake_weight,
113 : fd_vote_stake_weight_t const * vote_stake_weight,
114 0 : ulong staked_cnt ) {
115 :
116 0 : if( FD_UNLIKELY( staked_cnt > MAX_COMPRESSED_STAKE_WEIGHTS ) )
117 0 : FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu compressed stakes in it,"
118 0 : " but the maximum allowed is %lu", staked_cnt, MAX_COMPRESSED_STAKE_WEIGHTS ));
119 : /* Copy from input message [(vote, id, stake)] into old format [(id, stake)]. */
120 0 : ulong idx = 0UL;
121 0 : for( ulong i=0UL; i<staked_cnt; i++ ) {
122 0 : if( FD_UNLIKELY( fd_pubkey_eq( &vote_stake_weight[ i ].id_key, &FD_DUMMY_ACCOUNT_PUBKEY ) ) ) continue;
123 0 : memcpy( stake_weight[ idx ].key.uc, vote_stake_weight[ i ].id_key.uc, sizeof(fd_pubkey_t) );
124 0 : stake_weight[ idx ].stake = vote_stake_weight[ i ].stake;
125 0 : idx++;
126 0 : }
127 :
128 : /* Sort [(id, stake)] by id, so we can dedup */
129 0 : sort_weights_by_id_inplace( stake_weight, idx );
130 :
131 : /* Dedup entries, aggregating stake */
132 0 : ulong j=0UL;
133 0 : for( ulong i=1UL; i<idx; i++ ) {
134 0 : fd_pubkey_t * pre = &stake_weight[ j ].key;
135 0 : fd_pubkey_t * cur = &stake_weight[ i ].key;
136 0 : if( 0==memcmp( pre, cur, sizeof(fd_pubkey_t) ) ) {
137 0 : stake_weight[ j ].stake += stake_weight[ i ].stake;
138 0 : } else {
139 0 : ++j;
140 0 : stake_weight[ j ].stake = stake_weight[ i ].stake;
141 0 : memcpy( stake_weight[ j ].key.uc, stake_weight[ i ].key.uc, sizeof(fd_pubkey_t) );
142 0 : }
143 0 : }
144 0 : ulong staked_cnt_by_id = fd_ulong_min( idx, j+1 );
145 :
146 : /* Sort [(id, stake)] by stake then id, as expected */
147 0 : sort_weights_by_stake_id_inplace( stake_weight, staked_cnt_by_id );
148 :
149 0 : return staked_cnt_by_id;
150 0 : }
151 :
152 : #define SET_NAME unhit_set
153 0 : #define SET_MAX MAX_SHRED_DESTS
154 : #include "../../util/tmpl/fd_set.c"
155 :
156 : void
157 0 : fd_stake_ci_stake_msg_fini( fd_stake_ci_t * info ) {
158 : /* The grossness here is a sign our abstractions are wrong and need to
159 : be fixed instead of just patched. We need to generate weighted
160 : shred destinations using a combination of the new stake information
161 : and whatever contact info we previously knew. */
162 0 : ulong epoch = info->scratch->epoch;
163 0 : ulong staked_cnt = info->scratch->staked_cnt;
164 0 : ulong unchanged_staked_cnt = info->scratch->staked_cnt;
165 0 : ulong vote_keyed_lsched = info->scratch->vote_keyed_lsched;
166 :
167 : /* Just take the first one arbitrarily because they both have the same
168 : contact info, other than possibly some staked nodes with no contact
169 : info. */
170 0 : fd_shred_dest_t * existing_sdest = info->epoch_info->sdest;
171 0 : ulong existing_dest_cnt = fd_shred_dest_cnt_all( existing_sdest );
172 :
173 : /* Keep track of the destinations in existing_sdest that are not
174 : staked in this new epoch, i.e. the ones we don't hit in the loop
175 : below. */
176 0 : unhit_set_t _unhit[ unhit_set_word_cnt ];
177 : /* This memsets to 0, right before we memset to 1, and is probably
178 : unnecessary, but using it without joining seems like a hack. */
179 0 : unhit_set_t * unhit = unhit_set_join( unhit_set_new( _unhit ) );
180 0 : unhit_set_full( unhit );
181 :
182 : /* This function will remove any dummy stakes from the list. After
183 : this point, there will only be actual stakes that correspond to
184 : selected leader nodes. */
185 0 : staked_cnt = compute_id_weights_from_vote_weights( info->stake_weight, info->vote_stake_weight, staked_cnt );
186 0 : if( FD_UNLIKELY( staked_cnt>MAX_SHRED_DESTS ) ) {
187 0 : FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu stakes in it,"
188 0 : " but the maximum allowed is %lu", staked_cnt, MAX_SHRED_DESTS ));
189 0 : }
190 :
191 0 : for( ulong i=0UL; i<staked_cnt; i++ ) {
192 0 : fd_shred_dest_idx_t old_idx = fd_shred_dest_pubkey_to_idx( existing_sdest, &(info->stake_weight[ i ].key) );
193 0 : fd_shred_dest_weighted_t * in_prev = fd_shred_dest_idx_to_dest( existing_sdest, old_idx );
194 0 : info->shred_dest[ i ] = *in_prev;
195 0 : if( FD_UNLIKELY( old_idx==FD_SHRED_DEST_NO_DEST ) ) {
196 : /* We got the generic empty entry, so fixup the pubkey */
197 0 : info->shred_dest[ i ].pubkey = info->stake_weight[ i ].key;
198 0 : } else {
199 0 : unhit_set_remove( unhit, old_idx );
200 0 : }
201 0 : info->shred_dest[ i ].stake_lamports = info->stake_weight[ i ].stake;
202 0 : }
203 :
204 0 : int any_destaked = 0;
205 0 : ulong j = staked_cnt;
206 0 : for( ulong idx=unhit_set_iter_init( unhit ); (idx<existing_dest_cnt) & (!unhit_set_iter_done( idx )) & (j<MAX_SHRED_DESTS);
207 0 : idx=unhit_set_iter_next( unhit, idx ) ) {
208 0 : fd_shred_dest_weighted_t * in_prev = fd_shred_dest_idx_to_dest( existing_sdest, (fd_shred_dest_idx_t)idx );
209 0 : if( FD_LIKELY( in_prev->ip4 ) ) {
210 0 : info->shred_dest[ j ] = *in_prev;
211 0 : any_destaked |= (in_prev->stake_lamports > 0UL);
212 0 : info->shred_dest[ j ].stake_lamports = 0UL;
213 0 : j++;
214 0 : }
215 0 : }
216 :
217 0 : unhit_set_delete( unhit_set_leave( unhit ) );
218 :
219 0 : if( FD_UNLIKELY( any_destaked ) ) {
220 : /* The unstaked list might be a little out of order because the
221 : destinations that were previously staked will be at the start of
222 : the unstaked list, sorted by their previous stake, instead of
223 : where they should be. If there weren't any destaked, then the
224 : only unstaked nodes come from the previous list, which we know
225 : was in order, perhaps skipping some, which doesn't ruin the
226 : order. */
227 0 : sort_pubkey_inplace( info->shred_dest + staked_cnt, j - staked_cnt );
228 0 : }
229 :
230 : /* Now we have a plausible shred_dest list. */
231 :
232 : /* Clear the existing info */
233 0 : fd_per_epoch_info_t * new_ei = info->epoch_info + (epoch % 2UL);
234 0 : fd_shred_dest_delete ( fd_shred_dest_leave ( new_ei->sdest ) );
235 0 : fd_epoch_leaders_delete( fd_epoch_leaders_leave( new_ei->lsched ) );
236 :
237 : /* And create the new one */
238 0 : ulong excluded_stake = info->scratch->excluded_stake;
239 :
240 0 : new_ei->epoch = epoch;
241 0 : new_ei->start_slot = info->scratch->start_slot;
242 0 : new_ei->slot_cnt = info->scratch->slot_cnt;
243 0 : new_ei->excluded_stake = excluded_stake;
244 0 : new_ei->vote_keyed_lsched = vote_keyed_lsched;
245 :
246 0 : new_ei->lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( new_ei->_lsched, epoch, new_ei->start_slot, new_ei->slot_cnt,
247 0 : unchanged_staked_cnt, info->vote_stake_weight, excluded_stake, vote_keyed_lsched ) );
248 0 : new_ei->sdest = fd_shred_dest_join ( fd_shred_dest_new ( new_ei->_sdest, info->shred_dest, j,
249 0 : new_ei->lsched, info->identity_key, excluded_stake ) );
250 0 : log_summary( "stake update", info );
251 0 : }
252 :
253 : void
254 0 : fd_stake_ci_epoch_msg_fini( fd_stake_ci_t * info ) {
255 0 : fd_stake_ci_stake_msg_fini( info );
256 0 : }
257 :
258 0 : fd_shred_dest_weighted_t * fd_stake_ci_dest_add_init( fd_stake_ci_t * info ) { return info->shred_dest; }
259 :
260 : static inline void
261 : fd_stake_ci_dest_add_fini_impl( fd_stake_ci_t * info,
262 : ulong cnt,
263 0 : fd_per_epoch_info_t * ei ) {
264 : /* Initially we start with one list containing S+U staked and unstaked
265 : destinations jumbled together. In order to update sdest, we need
266 : to convert the list to S' staked destinations (taken from the
267 : existing sdest, though possibly updated) followed by U unstaked
268 : destinations.
269 :
270 : It's possible to do this in place, but at a cost of additional
271 : complexity (similar to memcpy vs memmove). Rather than do that, we
272 : build the combined list in shred_dest_temp. */
273 :
274 0 : ulong found_unstaked_cnt = 0UL;
275 0 : int any_new_unstaked = 0;
276 :
277 0 : ulong const staked_cnt = fd_shred_dest_cnt_staked( ei->sdest );
278 0 : ulong j = staked_cnt;
279 :
280 0 : for( ulong i=0UL; i<cnt; i++ ) {
281 0 : fd_shred_dest_idx_t idx = fd_shred_dest_pubkey_to_idx( ei->sdest, &(info->shred_dest[ i ].pubkey) );
282 0 : fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( ei->sdest, idx );
283 0 : if( FD_UNLIKELY( (dest->stake_lamports==0UL)&(j<MAX_SHRED_DESTS) ) ) {
284 : /* Copy this destination to the unstaked part of the new list.
285 : This also handles the new unstaked case */
286 0 : info->shred_dest_temp[ j ] = info->shred_dest[ i ];
287 0 : info->shred_dest_temp[ j ].stake_lamports = 0UL;
288 0 : j++;
289 0 : }
290 :
291 0 : if( FD_LIKELY( idx!=FD_SHRED_DEST_NO_DEST ) ) {
292 0 : dest->ip4 = info->shred_dest[ i ].ip4;
293 0 : dest->port = info->shred_dest[ i ].port;
294 0 : }
295 :
296 0 : any_new_unstaked |= (idx==FD_SHRED_DEST_NO_DEST);
297 0 : found_unstaked_cnt += (ulong)((idx!=FD_SHRED_DEST_NO_DEST) & (dest->stake_lamports==0UL));
298 0 : }
299 :
300 0 : if( FD_LIKELY( !any_new_unstaked && found_unstaked_cnt==fd_shred_dest_cnt_unstaked( ei->sdest ) ) ) {
301 : /* Because any_new_unstaked==0, the set of unstaked nodes in this
302 : update is fully contained in the set of unstaked nodes in the
303 : sdest. Then additionally, because the sets are the same size,
304 : they must actually be equal. In this case, we've already updated
305 : the existing shred_dest_weighted with the newest contact info we
306 : have, so there's nothing else to do. */
307 0 : return;
308 0 : }
309 :
310 : /* Otherwise something more significant changed and we need to
311 : regenerate the sdest. At this point, elements [staked_cnt, j) now
312 : contain all the current unstaked destinations. */
313 :
314 : /* Copy staked nodes to [0, staked_cnt). We've already applied the
315 : updated contact info to these. */
316 0 : for( ulong i=0UL; i<staked_cnt; i++ )
317 0 : info->shred_dest_temp[ i ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)i );
318 :
319 : /* The staked nodes are sorted properly because we use the index from
320 : sdest. We need to sort the unstaked nodes by pubkey though. */
321 0 : sort_pubkey_inplace( info->shred_dest_temp + staked_cnt, j - staked_cnt );
322 :
323 0 : fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
324 :
325 0 : ei->sdest = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, info->shred_dest_temp, j, ei->lsched, info->identity_key,
326 0 : ei->excluded_stake ) );
327 :
328 0 : if( FD_UNLIKELY( ei->sdest==NULL ) ) {
329 : /* Happens if the identity key is not present, which can only happen
330 : if the current validator's stake is not in the top 40,200. We
331 : could initialize ei->sdest to a dummy value, but having the wrong
332 : stake weights could lead to potentially slashable issues
333 : elsewhere (e.g. we might product a block when we're not actually
334 : leader). We're just going to terminate in this case. */
335 0 : FD_LOG_ERR(( "Too many validators have higher stake than this validator. Cannot continue." ));
336 0 : }
337 0 : }
338 :
339 :
340 : void
341 : fd_stake_ci_dest_add_fini( fd_stake_ci_t * info,
342 0 : ulong cnt ) {
343 : /* The Rust side uses tvu_peers which typically excludes the local
344 : validator. In some cases, after a set-identity, it might still
345 : include the local validator though. If it doesn't include it, we
346 : need to add the local validator back. */
347 0 : FD_TEST( cnt<MAX_SHRED_DESTS );
348 0 : ulong i=0UL;
349 0 : for(; i<cnt; i++ ) if( FD_UNLIKELY( 0==memcmp( info->shred_dest[ i ].pubkey.uc, info->identity_key, 32UL ) ) ) break;
350 :
351 0 : if( FD_LIKELY( i==cnt ) ) {
352 0 : fd_shred_dest_weighted_t self_dests = { .pubkey = info->identity_key[ 0 ], .ip4 = SELF_DUMMY_IP };
353 0 : info->shred_dest[ cnt++ ] = self_dests;
354 0 : } else {
355 0 : info->shred_dest[ i ].ip4 = SELF_DUMMY_IP;
356 0 : }
357 :
358 : /* Update both of them */
359 0 : fd_stake_ci_dest_add_fini_impl( info, cnt, info->epoch_info + 0UL );
360 0 : fd_stake_ci_dest_add_fini_impl( info, cnt, info->epoch_info + 1UL );
361 :
362 0 : log_summary( "dest update", info );
363 0 : }
364 :
365 :
366 : /* Returns a value in [0, 2) if found, and ULONG_MAX if not */
367 : static inline ulong
368 : fd_stake_ci_get_idx_for_slot( fd_stake_ci_t const * info,
369 0 : ulong slot ) {
370 0 : fd_per_epoch_info_t const * ei = info->epoch_info;
371 0 : ulong idx = ULONG_MAX;
372 0 : for( ulong i=0UL; i<2UL; i++ ) idx = fd_ulong_if( (ei[i].start_slot<=slot) & (slot-ei[i].start_slot<ei[i].slot_cnt), i, idx );
373 0 : return idx;
374 0 : }
375 :
376 :
377 : void
378 : fd_stake_ci_set_identity( fd_stake_ci_t * info,
379 0 : fd_pubkey_t const * identity_key ) {
380 : /* None of the stakes are changing, so we just need to regenerate the
381 : sdests, sightly adjusting the destination IP addresses. The only
382 : corner case is if the new identity is not present. */
383 0 : for( ulong i=0UL; i<2UL; i++ ) {
384 0 : fd_per_epoch_info_t * ei = info->epoch_info+i;
385 :
386 0 : fd_shred_dest_idx_t old_idx = fd_shred_dest_pubkey_to_idx( ei->sdest, info->identity_key );
387 0 : fd_shred_dest_idx_t new_idx = fd_shred_dest_pubkey_to_idx( ei->sdest, identity_key );
388 :
389 0 : FD_TEST( old_idx!=FD_SHRED_DEST_NO_DEST );
390 :
391 0 : if( FD_LIKELY( new_idx!=FD_SHRED_DEST_NO_DEST ) ) {
392 0 : fd_shred_dest_idx_to_dest( ei->sdest, old_idx )->ip4 = 0U;
393 0 : fd_shred_dest_idx_to_dest( ei->sdest, new_idx )->ip4 = SELF_DUMMY_IP;
394 :
395 0 : fd_shred_dest_update_source( ei->sdest, new_idx );
396 0 : } else {
397 0 : ulong staked_cnt = fd_shred_dest_cnt_staked ( ei->sdest );
398 0 : ulong unstaked_cnt = fd_shred_dest_cnt_unstaked( ei->sdest );
399 0 : if( FD_UNLIKELY( staked_cnt+unstaked_cnt==MAX_SHRED_DESTS ) ) {
400 0 : FD_LOG_ERR(( "too many validators in shred table to add a new validator with set-identity" ));
401 0 : }
402 : /* We'll add identity_key as a new unstaked validator. First copy
403 : all the staked ones, then place the new validator in the spot
404 : where it belongs according to lexicographic order. */
405 0 : ulong j=0UL;
406 0 : for(; j<staked_cnt; j++ ) info->shred_dest_temp[ j ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)j );
407 0 : for(; j<staked_cnt+unstaked_cnt; j++ ) {
408 0 : fd_shred_dest_weighted_t * wj = fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)j );
409 0 : if( FD_UNLIKELY( (memcmp( wj->pubkey.uc, identity_key->uc, 32UL )<=0) ) ) break;
410 0 : info->shred_dest_temp[ j ] = *wj;
411 0 : }
412 :
413 0 : info->shred_dest_temp[ j ].pubkey = *identity_key;
414 0 : info->shred_dest_temp[ j ].stake_lamports = 0UL;
415 0 : info->shred_dest_temp[ j ].ip4 = SELF_DUMMY_IP;
416 :
417 0 : for(; j<staked_cnt+unstaked_cnt; j++ ) info->shred_dest_temp[ j+1UL ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)j );
418 :
419 0 : fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
420 :
421 0 : ei->sdest = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, info->shred_dest_temp, j+1UL, ei->lsched, identity_key,
422 0 : ei->excluded_stake ) );
423 0 : FD_TEST( ei->sdest );
424 0 : }
425 :
426 0 : }
427 0 : *info->identity_key = *identity_key;
428 0 : }
429 :
430 : void
431 : refresh_sdest( fd_stake_ci_t * info,
432 : fd_shred_dest_weighted_t * shred_dest_temp,
433 : ulong cnt,
434 : ulong staked_cnt,
435 0 : fd_per_epoch_info_t * ei ) {
436 0 : sort_pubkey_inplace( shred_dest_temp + staked_cnt, cnt - staked_cnt );
437 :
438 0 : fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
439 0 : ei->sdest = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, shred_dest_temp, cnt, ei->lsched, info->identity_key, ei->excluded_stake ) );
440 0 : if( FD_UNLIKELY( ei->sdest==NULL ) ) {
441 0 : FD_LOG_ERR(( "Too many validators have higher stake than this validator. Cannot continue." ));
442 0 : }
443 0 : }
444 :
445 : void
446 : ci_dest_add_one_unstaked( fd_stake_ci_t * info,
447 : fd_shred_dest_weighted_t * new_entry,
448 0 : fd_per_epoch_info_t * ei ) {
449 0 : if( fd_shred_dest_cnt_all( ei->sdest )>=MAX_SHRED_DESTS ) {
450 0 : FD_LOG_WARNING(( "Too many validators in shred table to add a new validator." ));
451 0 : return;
452 0 : }
453 0 : ulong cur_cnt = fd_shred_dest_cnt_all( ei->sdest );
454 0 : for( ulong i=0UL; i<cur_cnt; i++ ) {
455 0 : info->shred_dest_temp[ i ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)i );
456 0 : }
457 :
458 : /* TODO: Alternative batched copy using memcpy. Check with Philip if safe */
459 : // fd_shred_dest_weighted_t * cur_dest = ei->sdest->all_destinations;
460 : // fd_memcpy( info->shred_dest_temp, cur_dest, sizeof(fd_shred_dest_weighted_t)*cur_cnt );
461 0 : info->shred_dest_temp[ cur_cnt++ ] = *new_entry;
462 0 : refresh_sdest( info, info->shred_dest_temp, cur_cnt, fd_shred_dest_cnt_staked( ei->sdest ), ei );
463 0 : }
464 :
465 : void
466 : ci_dest_update_impl( fd_stake_ci_t * info,
467 : fd_pubkey_t const * pubkey,
468 : uint ip4,
469 : ushort port,
470 0 : fd_per_epoch_info_t * ei ) {
471 0 : fd_shred_dest_idx_t idx = fd_shred_dest_pubkey_to_idx( ei->sdest, pubkey );
472 0 : if( idx==FD_SHRED_DEST_NO_DEST ) {
473 0 : fd_shred_dest_weighted_t new_entry = { .pubkey = *pubkey, .ip4 = ip4, .port = port, .stake_lamports = 0UL };
474 0 : ci_dest_add_one_unstaked( info, &new_entry, ei );
475 0 : return;
476 0 : }
477 0 : fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( ei->sdest, idx );
478 0 : dest->ip4 = ip4;
479 0 : dest->port = port;
480 0 : }
481 :
482 : void
483 : ci_dest_remove_impl( fd_stake_ci_t * info,
484 : fd_pubkey_t const * pubkey,
485 0 : fd_per_epoch_info_t * ei ) {
486 0 : fd_shred_dest_idx_t idx = fd_shred_dest_pubkey_to_idx( ei->sdest, pubkey );
487 0 : if( FD_UNLIKELY( idx==FD_SHRED_DEST_NO_DEST ) ) return;
488 :
489 0 : fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( ei->sdest, idx );
490 0 : if( FD_UNLIKELY( dest->stake_lamports>0UL ) ) {
491 : /* A staked entry is not "removed", instead its "stale" address is
492 : retained */
493 0 : return;
494 0 : }
495 0 : ulong cur_cnt = fd_shred_dest_cnt_all( ei->sdest );
496 0 : for( ulong i=0UL, j=0UL; i<cur_cnt; i++ ) {
497 0 : if( FD_UNLIKELY( i==idx ) ) continue;
498 0 : info->shred_dest_temp[ j++ ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t) i );
499 0 : }
500 : /* TODO: Alternative batched copy using memcpy. Check with Philip if this is safe */
501 : // fd_shred_dest_weighted_t * cur_dest = ei->sdest->all_destinations;
502 : // fd_memcpy( info->shred_dest_temp, cur_dest, sizeof(fd_shred_dest_weighted_t)*(idx) );
503 : // fd_memcpy( info->shred_dest_temp + idx, cur_dest + idx + 1UL, sizeof(fd_shred_dest_weighted_t)*(cur_cnt - idx - 1UL) );
504 0 : refresh_sdest( info, info->shred_dest_temp, cur_cnt-1UL, fd_shred_dest_cnt_staked( ei->sdest ), ei );
505 0 : }
506 :
507 : void
508 : fd_stake_ci_dest_update( fd_stake_ci_t * info,
509 : fd_pubkey_t const * pubkey,
510 : uint ip4,
511 0 : ushort port ) {
512 0 : ci_dest_update_impl( info, pubkey, ip4, port, info->epoch_info+0UL );
513 0 : ci_dest_update_impl( info, pubkey, ip4, port, info->epoch_info+1UL );
514 0 : }
515 :
516 : void
517 : fd_stake_ci_dest_remove( fd_stake_ci_t * info,
518 0 : fd_pubkey_t const * pubkey ) {
519 0 : ci_dest_remove_impl( info, pubkey, info->epoch_info+0UL );
520 0 : ci_dest_remove_impl( info, pubkey, info->epoch_info+1UL );
521 :
522 0 : }
523 :
524 :
525 : fd_shred_dest_t *
526 : fd_stake_ci_get_sdest_for_slot( fd_stake_ci_t const * info,
527 0 : ulong slot ) {
528 0 : ulong idx = fd_stake_ci_get_idx_for_slot( info, slot );
529 0 : return idx!=ULONG_MAX ? info->epoch_info[ idx ].sdest : NULL;
530 0 : }
531 :
532 : fd_epoch_leaders_t *
533 : fd_stake_ci_get_lsched_for_slot( fd_stake_ci_t const * info,
534 0 : ulong slot ) {
535 0 : ulong idx = fd_stake_ci_get_idx_for_slot( info, slot );
536 0 : return idx!=ULONG_MAX ? info->epoch_info[ idx ].lsched : NULL;
537 0 : }
|