Line data Source code
1 : #include "fd_stake_ci.h"
2 : #include "../../util/net/fd_ip4.h" /* Just for debug */
3 :
4 : #define SORT_NAME sort_pubkey
5 3472260 : #define SORT_KEY_T fd_shred_dest_weighted_t
6 5510883 : #define SORT_BEFORE(a,b) (memcmp( (a).pubkey.uc, (b).pubkey.uc, 32UL )<0)
7 : #include "../../util/tmpl/fd_sort.c"
8 :
9 : /* We don't have or need real contact info for the local validator, but
10 : we want to be able to distinguish it from staked nodes with no
11 : contact info. */
12 117 : #define SELF_DUMMY_IP 1U
13 :
14 : void *
15 : fd_stake_ci_new( void * mem,
16 33 : fd_pubkey_t const * identity_key ) {
17 33 : fd_stake_ci_t * info = (fd_stake_ci_t *)mem;
18 :
19 33 : fd_stake_weight_t dummy_stakes[ 1 ] = {{ .key = {{0}}, .stake = 1UL }};
20 33 : fd_shred_dest_weighted_t dummy_dests[ 1 ] = {{ .pubkey = *identity_key, .ip4 = SELF_DUMMY_IP }};
21 :
22 : /* Initialize first 2 to satisfy invariants */
23 33 : info->stake_weight[ 0 ] = dummy_stakes[ 0 ];
24 33 : info->shred_dest [ 0 ] = dummy_dests [ 0 ];
25 99 : for( ulong i=0UL; i<2UL; i++ ) {
26 66 : fd_per_epoch_info_t * ei = info->epoch_info + i;
27 66 : ei->epoch = i;
28 66 : ei->start_slot = 0UL;
29 66 : ei->slot_cnt = 0UL;
30 66 : ei->excluded_stake = 0UL;
31 :
32 66 : ei->lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( ei->_lsched, 0UL, 0UL, 1UL, 1UL, info->stake_weight, 0UL ) );
33 66 : ei->sdest = fd_shred_dest_join ( fd_shred_dest_new ( ei->_sdest, info->shred_dest, 1UL, ei->lsched, identity_key, 0UL ) );
34 66 : }
35 33 : info->identity_key[ 0 ] = *identity_key;
36 :
37 33 : return (void *)info;
38 33 : }
39 :
40 33 : fd_stake_ci_t * fd_stake_ci_join( void * mem ) { return (fd_stake_ci_t *)mem; }
41 :
42 33 : void * fd_stake_ci_leave ( fd_stake_ci_t * info ) { return (void *)info; }
43 33 : void * fd_stake_ci_delete( void * mem ) { return mem; }
44 :
45 :
46 : void
47 : fd_stake_ci_stake_msg_init( fd_stake_ci_t * info,
48 87 : uchar const * new_message ) {
49 87 : ulong const * hdr = fd_type_pun_const( new_message );
50 :
51 87 : ulong epoch = hdr[ 0 ];
52 87 : ulong staked_cnt = hdr[ 1 ];
53 87 : ulong start_slot = hdr[ 2 ];
54 87 : ulong slot_cnt = hdr[ 3 ];
55 87 : ulong excluded_stake = hdr[ 4 ];
56 :
57 87 : if( FD_UNLIKELY( staked_cnt > MAX_SHRED_DESTS ) )
58 0 : FD_LOG_ERR(( "The stakes -> Firedancer splice sent a malformed update with %lu stakes in it,"
59 87 : " but the maximum allowed is %lu", staked_cnt, MAX_SHRED_DESTS ));
60 :
61 87 : info->scratch->epoch = epoch;
62 87 : info->scratch->start_slot = start_slot;
63 87 : info->scratch->slot_cnt = slot_cnt;
64 87 : info->scratch->staked_cnt = staked_cnt;
65 87 : info->scratch->excluded_stake = excluded_stake;
66 :
67 87 : fd_memcpy( info->stake_weight, hdr+5UL, sizeof(fd_stake_weight_t)*staked_cnt );
68 87 : }
69 :
70 : static inline void
71 168 : log_summary( char const * msg, fd_stake_ci_t * info ) {
72 : #if 0
73 : fd_per_epoch_info_t const * ei = info->epoch_info;
74 : FD_LOG_NOTICE(( "Dumping stake contact information because %s", msg ));
75 : for( ulong i=0UL; i<2UL; i++ ) {
76 : 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 ));
77 : fd_shred_dest_t * sdest = ei[i].sdest;
78 : for( fd_shred_dest_idx_t j=0; j<(fd_shred_dest_idx_t)fd_shred_dest_cnt_all( sdest ); j++ ) {
79 : fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( sdest, j );
80 : 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 ));
81 : }
82 : }
83 : #else
84 168 : (void)msg;
85 168 : (void)info;
86 168 : #endif
87 168 : }
88 :
89 : #define SET_NAME unhit_set
90 0 : #define SET_MAX MAX_SHRED_DESTS
91 : #include "../../util/tmpl/fd_set.c"
92 :
93 : void
94 84 : fd_stake_ci_stake_msg_fini( fd_stake_ci_t * info ) {
95 : /* The grossness here is a sign our abstractions are wrong and need to
96 : be fixed instead of just patched. We need to generate weighted
97 : shred destinations using a combination of the new stake information
98 : and whatever contact info we previously knew. */
99 84 : ulong epoch = info->scratch->epoch;
100 84 : ulong staked_cnt = info->scratch->staked_cnt;
101 :
102 : /* Just take the first one arbitrarily because they both have the same
103 : contact info, other than possibly some staked nodes with no contact
104 : info. */
105 84 : fd_shred_dest_t * existing_sdest = info->epoch_info->sdest;
106 84 : ulong existing_dest_cnt = fd_shred_dest_cnt_all( existing_sdest );
107 :
108 : /* Keep track of the destinations in existing_sdest that are not
109 : staked in this new epoch, i.e. the ones we don't hit in the loop
110 : below. */
111 84 : unhit_set_t _unhit[ unhit_set_word_cnt ];
112 : /* This memsets to 0, right before we memset to 1, and is probably
113 : unnecessary, but using it without joining seems like a hack. */
114 84 : unhit_set_t * unhit = unhit_set_join( unhit_set_new( _unhit ) );
115 84 : unhit_set_full( unhit );
116 :
117 482676 : for( ulong i=0UL; i<staked_cnt; i++ ) {
118 482592 : fd_shred_dest_idx_t old_idx = fd_shred_dest_pubkey_to_idx( existing_sdest, &(info->stake_weight[ i ].key) );
119 482592 : fd_shred_dest_weighted_t * in_prev = fd_shred_dest_idx_to_dest( existing_sdest, old_idx );
120 482592 : info->shred_dest[ i ] = *in_prev;
121 482592 : if( FD_UNLIKELY( old_idx==FD_SHRED_DEST_NO_DEST ) ) {
122 : /* We got the generic empty entry, so fixup the pubkey */
123 120693 : info->shred_dest[ i ].pubkey = info->stake_weight[ i ].key;
124 361899 : } else {
125 361899 : unhit_set_remove( unhit, old_idx );
126 361899 : }
127 482592 : info->shred_dest[ i ].stake_lamports = info->stake_weight[ i ].stake;
128 482592 : }
129 :
130 84 : int any_destaked = 0;
131 84 : ulong j = staked_cnt;
132 252 : for( ulong idx=unhit_set_iter_init( unhit ); (idx<existing_dest_cnt) & (!unhit_set_iter_done( idx )) & (j<MAX_SHRED_DESTS);
133 168 : idx=unhit_set_iter_next( unhit, idx ) ) {
134 168 : fd_shred_dest_weighted_t * in_prev = fd_shred_dest_idx_to_dest( existing_sdest, (fd_shred_dest_idx_t)idx );
135 168 : if( FD_LIKELY( in_prev->ip4 ) ) {
136 132 : info->shred_dest[ j ] = *in_prev;
137 132 : any_destaked |= (in_prev->stake_lamports > 0UL);
138 132 : info->shred_dest[ j ].stake_lamports = 0UL;
139 132 : j++;
140 132 : }
141 168 : }
142 :
143 84 : unhit_set_delete( unhit_set_leave( unhit ) );
144 :
145 84 : if( FD_UNLIKELY( any_destaked ) ) {
146 : /* The unstaked list might be a little out of order because the
147 : destinations that were previously staked will be at the start of
148 : the unstaked list, sorted by their previous stake, instead of
149 : where they should be. If there weren't any destaked, then the
150 : only unstaked nodes come from the previous list, which we know
151 : was in order, perhaps skipping some, which doesn't ruin the
152 : order. */
153 18 : sort_pubkey_inplace( info->shred_dest + staked_cnt, j - staked_cnt );
154 18 : }
155 :
156 : /* Now we have a plausible shred_dest list. */
157 :
158 : /* Clear the existing info */
159 84 : fd_per_epoch_info_t * new_ei = info->epoch_info + (epoch % 2UL);
160 84 : fd_shred_dest_delete ( fd_shred_dest_leave ( new_ei->sdest ) );
161 84 : fd_epoch_leaders_delete( fd_epoch_leaders_leave( new_ei->lsched ) );
162 :
163 : /* And create the new one */
164 84 : ulong excluded_stake = info->scratch->excluded_stake;
165 :
166 84 : new_ei->epoch = epoch;
167 84 : new_ei->start_slot = info->scratch->start_slot;
168 84 : new_ei->slot_cnt = info->scratch->slot_cnt;
169 84 : new_ei->excluded_stake = excluded_stake;
170 :
171 84 : new_ei->lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( new_ei->_lsched, epoch, new_ei->start_slot, new_ei->slot_cnt,
172 84 : staked_cnt, info->stake_weight, excluded_stake ) );
173 84 : new_ei->sdest = fd_shred_dest_join ( fd_shred_dest_new ( new_ei->_sdest, info->shred_dest, j,
174 84 : new_ei->lsched, info->identity_key, excluded_stake ) );
175 84 : log_summary( "stake update", info );
176 84 : }
177 :
178 87 : fd_shred_dest_weighted_t * fd_stake_ci_dest_add_init( fd_stake_ci_t * info ) { return info->shred_dest; }
179 :
180 : static inline void
181 : fd_stake_ci_dest_add_fini_impl( fd_stake_ci_t * info,
182 : ulong cnt,
183 168 : fd_per_epoch_info_t * ei ) {
184 : /* Initially we start with one list containing S+U staked and unstaked
185 : destinations jumbled together. In order to update sdest, we need
186 : to convert the list to S' staked destinations (taken from the
187 : existing sdest, though possibly updated) followed by U unstaked
188 : destinations.
189 :
190 : It's possible to do this in place, but at a cost of additional
191 : complexity (similar to memcpy vs memmove). Rather than do that, we
192 : build the combined list in shred_dest_temp. */
193 :
194 168 : ulong found_unstaked_cnt = 0UL;
195 168 : int any_new_unstaked = 0;
196 :
197 168 : ulong const staked_cnt = fd_shred_dest_cnt_staked( ei->sdest );
198 168 : ulong j = staked_cnt;
199 :
200 3859650 : for( ulong i=0UL; i<cnt; i++ ) {
201 3859482 : fd_shred_dest_idx_t idx = fd_shred_dest_pubkey_to_idx( ei->sdest, &(info->shred_dest[ i ].pubkey) );
202 3859482 : fd_shred_dest_weighted_t * dest = fd_shred_dest_idx_to_dest( ei->sdest, idx );
203 3859482 : if( FD_UNLIKELY( (dest->stake_lamports==0UL)&(j<MAX_SHRED_DESTS) ) ) {
204 : /* Copy this destination to the unstaked part of the new list.
205 : This also handles the new unstaked case */
206 482727 : info->shred_dest_temp[ j ] = info->shred_dest[ i ];
207 482727 : info->shred_dest_temp[ j ].stake_lamports = 0UL;
208 482727 : j++;
209 482727 : }
210 :
211 3859482 : if( FD_LIKELY( idx!=FD_SHRED_DEST_NO_DEST ) ) {
212 3738678 : dest->ip4 = info->shred_dest[ i ].ip4;
213 3738678 : dest->port = info->shred_dest[ i ].port;
214 3738678 : memcpy( dest->mac_addr, info->shred_dest[ i ].mac_addr, 6UL );
215 3738678 : }
216 :
217 3859482 : any_new_unstaked |= (idx==FD_SHRED_DEST_NO_DEST);
218 3859482 : found_unstaked_cnt += (ulong)((idx!=FD_SHRED_DEST_NO_DEST) & (dest->stake_lamports==0UL));
219 3859482 : }
220 :
221 168 : if( FD_LIKELY( !any_new_unstaked && found_unstaked_cnt==fd_shred_dest_cnt_unstaked( ei->sdest ) ) ) {
222 : /* Because any_new_unstaked==0, the set of unstaked nodes in this
223 : update is fully contained in the set of unstaked nodes in the
224 : sdest. Then additionally, because the sets are the same size,
225 : they must actually be equal. In this case, we've already updated
226 : the existing shred_dest_weighted with the newest contact info we
227 : have, so there's nothing else to do. */
228 48 : return;
229 48 : }
230 :
231 : /* Otherwise something more significant changed and we need to
232 : regenerate the sdest. At this point, elements [staked_cnt, j) now
233 : contain all the current unstaked destinations. */
234 :
235 : /* Copy staked nodes to [0, staked_cnt). We've already applied the
236 : updated contact info to these. */
237 1929807 : for( ulong i=0UL; i<staked_cnt; i++ )
238 1929687 : info->shred_dest_temp[ i ] = *fd_shred_dest_idx_to_dest( ei->sdest, (fd_shred_dest_idx_t)i );
239 :
240 : /* The staked nodes are sorted properly because we use the index from
241 : sdest. We need to sort the unstaked nodes by pubkey though. */
242 120 : sort_pubkey_inplace( info->shred_dest_temp + staked_cnt, j - staked_cnt );
243 :
244 120 : fd_shred_dest_delete( fd_shred_dest_leave( ei->sdest ) );
245 :
246 120 : ei->sdest = fd_shred_dest_join( fd_shred_dest_new( ei->_sdest, info->shred_dest_temp, j, ei->lsched, info->identity_key,
247 120 : ei->excluded_stake ) );
248 :
249 120 : if( FD_UNLIKELY( ei->sdest==NULL ) ) {
250 : /* Happens if the identity key is not present, which can only happen
251 : if the current validator's stake is not in the top 40,200. We
252 : could initialize ei->sdest to a dummy value, but having the wrong
253 : stake weights could lead to potentially slashable issues
254 : elsewhere (e.g. we might product a block when we're not actually
255 : leader). We're just going to terminate in this case. */
256 0 : FD_LOG_ERR(( "Too many validators have higher stake than this validator. Cannot continue." ));
257 0 : }
258 120 : }
259 :
260 :
261 : void
262 : fd_stake_ci_dest_add_fini( fd_stake_ci_t * info,
263 84 : ulong cnt ) {
264 : /* The Rust side uses tvu_peers which excludes the local validator.
265 : Add the local validator back. */
266 84 : FD_TEST( cnt<MAX_SHRED_DESTS );
267 84 : fd_shred_dest_weighted_t self_dests[ 1 ] = {{ .pubkey = info->identity_key[ 0 ], .ip4 = SELF_DUMMY_IP }};
268 84 : info->shred_dest[ cnt++ ] = self_dests[ 0 ];
269 :
270 : /* Update both of them */
271 84 : fd_stake_ci_dest_add_fini_impl( info, cnt, info->epoch_info + 0UL );
272 84 : fd_stake_ci_dest_add_fini_impl( info, cnt, info->epoch_info + 1UL );
273 :
274 84 : log_summary( "dest update", info );
275 84 : }
276 :
277 :
278 : /* Returns a value in [0, 2) if found, and ULONG_MAX if not */
279 : static inline ulong
280 : fd_stake_ci_get_idx_for_slot( fd_stake_ci_t const * info,
281 1107 : ulong slot ) {
282 1107 : fd_per_epoch_info_t const * ei = info->epoch_info;
283 1107 : ulong idx = ULONG_MAX;
284 3321 : 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 );
285 1107 : return idx;
286 1107 : }
287 :
288 :
289 : fd_shred_dest_t *
290 : fd_stake_ci_get_sdest_for_slot( fd_stake_ci_t const * info,
291 555 : ulong slot ) {
292 555 : ulong idx = fd_stake_ci_get_idx_for_slot( info, slot );
293 555 : return idx!=ULONG_MAX ? info->epoch_info[ idx ].sdest : NULL;
294 555 : }
295 :
296 : fd_epoch_leaders_t *
297 : fd_stake_ci_get_lsched_for_slot( fd_stake_ci_t const * info,
298 552 : ulong slot ) {
299 552 : ulong idx = fd_stake_ci_get_idx_for_slot( info, slot );
300 552 : return idx!=ULONG_MAX ? info->epoch_info[ idx ].lsched : NULL;
301 552 : }
|