Line data Source code
1 : #include "fd_gossip_tile.h"
2 : #include "../../disco/metrics/fd_metrics.h"
3 : #include <linux/futex.h>
4 : #include "generated/fd_gossip_tile_seccomp.h"
5 :
6 : #include "../../flamenco/gossip/fd_gossip_out.h"
7 : #include "../../flamenco/gossip/fd_active_set.h"
8 : #include "../../flamenco/features/fd_features.h"
9 : #include "../../disco/keyguard/fd_keyload.h"
10 : #include "../../disco/shred/fd_stake_ci.h"
11 : #include "../../flamenco/stakes/fd_stake_weight_sort.h"
12 : #include "../../disco/topo/fd_dns_resolve.h"
13 : #include "../../disco/fd_txn_m.h"
14 : #include "../restore/utils/fd_ssmsg.h"
15 : #include "../restore/utils/fd_wfs.h"
16 :
17 0 : #define IN_KIND_GOSSVF (0)
18 0 : #define IN_KIND_SHRED_VERSION (1)
19 0 : #define IN_KIND_SIGN (2)
20 0 : #define IN_KIND_EPOCH (4)
21 0 : #define IN_KIND_SNAPIN_MANIF (6)
22 :
23 : FD_FN_CONST static inline ulong
24 0 : scratch_align( void ) {
25 0 : return 128UL;
26 0 : }
27 :
28 : FD_FN_PURE static inline ulong
29 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
30 0 : ulong l = FD_LAYOUT_INIT;
31 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
32 0 : l = FD_LAYOUT_APPEND( l, fd_gossip_align(), fd_gossip_footprint( tile->gossip.max_entries, tile->gossip.entrypoints_cnt ) );
33 0 : l = FD_LAYOUT_APPEND( l, fd_gui_gossip_bw_align(), fd_gui_gossip_bw_footprint() );
34 0 : return FD_LAYOUT_FINI( l, scratch_align() );
35 0 : }
36 :
37 : static void
38 : gui_bw_flush( fd_gossip_tile_ctx_t * ctx,
39 0 : fd_stem_context_t * stem ) {
40 0 : fd_gui_gossip_bw_rec_t * dst = fd_chunk_to_laddr( ctx->gui_out->mem, ctx->gui_out->chunk );
41 0 : ulong cnt = fd_gui_gossip_bw_flush( ctx->gui_bw, dst );
42 0 : ulong sz = cnt*sizeof(fd_gui_gossip_bw_rec_t);
43 0 : fd_stem_publish( stem, ctx->gui_out->idx, cnt, ctx->gui_out->chunk, sz, 0UL, 0UL, 0UL );
44 0 : ctx->gui_out->chunk = fd_dcache_compact_next( ctx->gui_out->chunk, sz, ctx->gui_out->chunk0, ctx->gui_out->wmark );
45 0 : }
46 :
47 : static void
48 : gossip_send_fn( void * ctx,
49 : fd_stem_context_t * stem,
50 : uchar const * payload,
51 : ulong payload_sz,
52 : fd_ip4_port_t const * peer_address,
53 0 : ulong tsorig ) {
54 0 : fd_gossip_tile_ctx_t * gossip_ctx = (fd_gossip_tile_ctx_t *)ctx;
55 :
56 0 : uchar * packet = (uchar *)fd_chunk_to_laddr( gossip_ctx->net_out->mem, gossip_ctx->net_out->chunk );
57 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
58 0 : *hdr = *gossip_ctx->net_out_hdr;
59 :
60 0 : fd_ip4_hdr_t * ip4 = hdr->ip4;
61 0 : fd_udp_hdr_t * udp = hdr->udp;
62 :
63 0 : ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t) + sizeof(fd_ip4_hdr_t)) );
64 0 : udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
65 0 : ip4->daddr = peer_address->addr;
66 0 : udp->net_dport = peer_address->port;
67 0 : ip4->net_id = fd_ushort_bswap( gossip_ctx->net_id++ );
68 0 : ip4->check = fd_ip4_hdr_check_fast( ip4 );
69 0 : udp->check = 0;
70 :
71 0 : fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), payload, payload_sz );
72 :
73 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
74 0 : ulong sig = fd_disco_netmux_sig( peer_address->addr, peer_address->port, peer_address->addr, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
75 0 : ulong packet_sz = payload_sz + sizeof(fd_ip4_udp_hdrs_t);
76 :
77 0 : fd_stem_publish( stem, gossip_ctx->net_out->idx, sig, gossip_ctx->net_out->chunk, packet_sz, 0UL, tsorig, tspub );
78 0 : gossip_ctx->net_out->chunk = fd_dcache_compact_next( gossip_ctx->net_out->chunk, packet_sz, gossip_ctx->net_out->chunk0, gossip_ctx->net_out->wmark );
79 :
80 0 : if( FD_LIKELY( gossip_ctx->has_gui ) ) {
81 0 : if( FD_UNLIKELY( fd_gui_gossip_bw_add( gossip_ctx->gui_bw, peer_address->addr, peer_address->port, payload, payload_sz, stem->now ) ) ) gui_bw_flush( gossip_ctx, stem );
82 0 : }
83 0 : }
84 :
85 : static void
86 : gossip_sign_fn( void * ctx,
87 : uchar const * data,
88 : ulong data_sz,
89 0 : int sign_type ) {
90 0 : fd_gossip_tile_ctx_t * gossip_ctx = (fd_gossip_tile_ctx_t *)ctx;
91 0 : FD_TEST( data_sz<=gossip_ctx->sign_out_mtu );
92 0 : fd_memcpy( fd_chunk_to_laddr( gossip_ctx->sign_out->mem, gossip_ctx->sign_out->chunk ), data, data_sz );
93 0 : fd_stem_publish( gossip_ctx->stem, gossip_ctx->sign_out->idx, (ulong)(uint)sign_type, gossip_ctx->sign_out->chunk, data_sz, 0UL, 0UL, 0UL );
94 0 : gossip_ctx->sign_out->chunk = fd_dcache_compact_next( gossip_ctx->sign_out->chunk, data_sz, gossip_ctx->sign_out->chunk0, gossip_ctx->sign_out->wmark );
95 0 : }
96 :
97 : static void
98 : gossip_ping_tracker_change_fn( void * _ctx,
99 : uchar const * peer_pubkey,
100 : fd_ip4_port_t peer_address,
101 : long now,
102 0 : int change_type ) {
103 0 : (void)now;
104 :
105 0 : fd_gossip_tile_ctx_t * ctx = (fd_gossip_tile_ctx_t *)_ctx;
106 :
107 0 : fd_gossip_ping_update_t * ping_update = (fd_gossip_ping_update_t *)fd_chunk_to_laddr( ctx->gossvf_out->mem, ctx->gossvf_out->chunk );
108 0 : fd_memcpy( ping_update->pubkey.uc, peer_pubkey, 32UL );
109 0 : ping_update->gossip_addr.l = peer_address.l;
110 0 : ping_update->remove = change_type!=FD_PING_TRACKER_CHANGE_TYPE_ACTIVE;
111 :
112 0 : fd_stem_publish( ctx->stem, ctx->gossvf_out->idx, 0UL, ctx->gossvf_out->chunk, sizeof(fd_gossip_ping_update_t), 0UL, 0UL, 0UL );
113 0 : ctx->gossvf_out->chunk = fd_dcache_compact_next( ctx->gossvf_out->chunk, sizeof(fd_gossip_ping_update_t), ctx->gossvf_out->chunk0, ctx->gossvf_out->wmark );
114 0 : }
115 :
116 : static void
117 : gossip_activity_update_fn( void * _ctx,
118 : fd_pubkey_t const * identity,
119 : fd_gossip_contact_info_t const * ci,
120 0 : int change_type ) {
121 0 : fd_gossip_tile_ctx_t * ctx = (fd_gossip_tile_ctx_t *)_ctx;
122 :
123 : /* We won't start tracking updates until after the manifest is loaded.
124 : This is okay since this callback is triggered by all contact info
125 : updates, including refreshes, so any updates we missed at boot will
126 : show up shortly after. */
127 0 : if( FD_LIKELY( !ctx->my_contact_info->shred_version || ctx->wfs_state!=FD_GOSSIP_WFS_STATE_WAIT ) ) return;
128 :
129 : /* gossvf should filter out messages with mismatching shred version */
130 0 : FD_TEST( ci->shred_version==ctx->my_contact_info->shred_version );
131 :
132 : /* To match Agave's tvu_peers() filter, require a valid TVU UDP
133 : socket for the ACTIVE path. */
134 0 : if( FD_LIKELY( change_type==FD_GOSSIP_ACTIVITY_CHANGE_TYPE_ACTIVE ) ) {
135 0 : fd_ip4_port_t tvu_addr;
136 0 : tvu_addr.addr = ci->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU ].is_ipv6 ? 0U : ci->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU ].ip4;
137 0 : tvu_addr.port = ci->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU ].port;
138 0 : if( FD_UNLIKELY( !tvu_addr.l ) ) return;
139 0 : }
140 :
141 : /* If identity is not found in ctx->wfs_stakes the peer is likely
142 : unstaked and can be ignored. */
143 0 : ulong stake_idx = fd_stake_weight_key_sort_split( ctx->wfs_stakes, ctx->wfs_stakes_cnt, (fd_stake_weight_t){ .key = *identity } );
144 0 : if( FD_UNLIKELY( stake_idx>=ctx->wfs_stakes_cnt || memcmp( identity->uc, ctx->wfs_stakes[ stake_idx ].key.uc, sizeof(fd_pubkey_t) ) ) ) return;
145 :
146 0 : if( FD_LIKELY( change_type==FD_GOSSIP_ACTIVITY_CHANGE_TYPE_ACTIVE ) ) {
147 0 : if( FD_UNLIKELY( !ctx->wfs_active[ stake_idx ] ) ) {
148 0 : ctx->wfs_stake.online += ctx->wfs_stakes[ stake_idx ].stake;
149 0 : ctx->wfs_peers.online++;
150 0 : }
151 0 : ctx->wfs_active[ stake_idx ] = 1;
152 0 : }
153 0 : if( FD_LIKELY( change_type==FD_GOSSIP_ACTIVITY_CHANGE_TYPE_INACTIVE ) ) {
154 0 : if( FD_UNLIKELY( ctx->wfs_active[ stake_idx ] ) ) {
155 0 : ctx->wfs_stake.online -= ctx->wfs_stakes[ stake_idx ].stake;
156 0 : ctx->wfs_peers.online--;
157 0 : }
158 0 : ctx->wfs_active[ stake_idx ] = 0;
159 0 : }
160 :
161 0 : if( FD_UNLIKELY( ctx->wfs_stake.total>0UL && (ulong)( ((double)ctx->wfs_stake.online / (double)ctx->wfs_stake.total) * 100.0 ) >= 80UL ) ) {
162 0 : ctx->wfs_state = FD_GOSSIP_WFS_STATE_PUBLISH;
163 0 : }
164 0 : }
165 :
166 : static inline void
167 0 : during_housekeeping( fd_gossip_tile_ctx_t * ctx ) {
168 0 : if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) {
169 0 : fd_clock_tile_recal( ctx->clock );
170 0 : }
171 :
172 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_UNHALT_PENDING ) ) {
173 0 : FD_LOG_DEBUG(( "keyswitch: unhalting" ));
174 0 : FD_CHECK_CRIT( ctx->is_halting_signing, "state machine corruption" );
175 : /* Defer the actual set_identity call to after_credit, because it
176 : may incur a stem frag publish. */
177 0 : ctx->is_pending_set_identity = 1;
178 0 : }
179 :
180 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
181 0 : ctx->is_halting_signing = 1;
182 0 : if( FD_LIKELY( !fd_gossip_sign_pend_cnt( ctx->gossip ) ) ) fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
183 0 : }
184 0 : }
185 :
186 : static inline long
187 0 : next_deadline( fd_gossip_tile_ctx_t const * ctx ) {
188 0 : long deadline = fd_gui_gossip_bw_next_deadline( ctx->gui_bw );
189 0 : if( FD_UNLIKELY( !ctx->my_contact_info->shred_version || ctx->is_halting_signing ) ) return deadline; /* advance not run */
190 0 : return fd_long_min( deadline, fd_clock_tile_wallclock_to_tickcount( ctx->clock, fd_gossip_next_deadline( ctx->gossip ) ) );
191 0 : }
192 :
193 : static inline void
194 : before_credit( fd_gossip_tile_ctx_t * ctx,
195 : fd_stem_context_t * stem,
196 0 : int * charge_busy ) {
197 0 : if( FD_UNLIKELY( fd_gui_gossip_bw_due( ctx->gui_bw, stem->now ) ) ) {
198 0 : gui_bw_flush( ctx, stem );
199 0 : *charge_busy = 1;
200 0 : }
201 0 : }
202 :
203 : static inline void
204 0 : metrics_write( fd_gossip_tile_ctx_t * ctx ) {
205 0 : fd_ping_tracker_metrics_t const * ping_tracker_metrics = fd_gossip_ping_tracker_metrics( ctx->gossip );
206 :
207 0 : FD_MGAUGE_SET( GOSSIP, PING_PEER_COUNT_UNPINGED, ping_tracker_metrics->unpinged_cnt );
208 0 : FD_MGAUGE_SET( GOSSIP, PING_PEER_COUNT_AWAITING_PONG, ping_tracker_metrics->invalid_cnt );
209 0 : FD_MGAUGE_SET( GOSSIP, PING_PEER_COUNT_VALID, ping_tracker_metrics->valid_cnt );
210 0 : FD_MGAUGE_SET( GOSSIP, PING_PEER_COUNT_REFRESHING, ping_tracker_metrics->refreshing_cnt );
211 :
212 0 : FD_MCNT_ENUM_COPY( GOSSIP, PONG_PROCESSED, ping_tracker_metrics->pong_result );
213 0 : FD_MCNT_ENUM_COPY( GOSSIP, PING_PEER_REMOVED, ping_tracker_metrics->removed );
214 :
215 0 : FD_MGAUGE_SET( GOSSIP, CONTACT_INFO_CLIENT_ID, ctx->my_contact_info->version.client );
216 :
217 0 : FD_MCNT_SET( GOSSIP, PING_PEER_ADDED, ping_tracker_metrics->tracked_cnt );
218 0 : FD_MCNT_SET( GOSSIP, PING_PEER_ADDRESS_CHANGED, ping_tracker_metrics->address_changed_cnt );
219 :
220 0 : fd_gossip_purged_metrics_t const * purged_metrics = fd_gossip_purged_metrics2( ctx->gossip );
221 :
222 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PURGED_USED, purged_metrics->purged_cnt );
223 0 : FD_MCNT_SET( GOSSIP, CRDS_PURGED_EVICTED, purged_metrics->purged_evicted_cnt );
224 0 : FD_MCNT_SET( GOSSIP, CRDS_PURGED_EXPIRED, purged_metrics->purged_expired_cnt );
225 :
226 0 : fd_crds_metrics_t const * crds_metrics = fd_gossip_crds_metrics( ctx->gossip );
227 :
228 0 : FD_MGAUGE_ENUM_COPY( GOSSIP, CRDS_USED, crds_metrics->count );
229 0 : FD_MCNT_SET( GOSSIP, CRDS_EXPIRED, crds_metrics->expired_cnt );
230 0 : FD_MCNT_SET( GOSSIP, CRDS_EVICTED, crds_metrics->evicted_cnt );
231 0 : FD_MCNT_SET( GOSSIP, CRDS_RELAY_COALESCED, crds_metrics->relay_coalesced_cnt );
232 :
233 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_COUNT_STAKED, crds_metrics->peer_staked_cnt );
234 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_COUNT_UNSTAKED, crds_metrics->peer_unstaked_cnt );
235 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_STAKE_LAMPORTS, crds_metrics->peer_visible_stake );
236 0 : FD_MCNT_SET( GOSSIP, CRDS_PEER_EVICTED, crds_metrics->peer_evicted_cnt );
237 :
238 0 : fd_gossip_metrics_t const * metrics = fd_gossip_metrics( ctx->gossip );
239 0 : fd_active_set_metrics_t const * active_set_metrics = fd_gossip_active_set_metrics2( ctx->gossip );
240 :
241 0 : ulong total_message_tx[ FD_METRICS_ENUM_GOSSIP_MESSAGE_TYPE_CNT ] = {0};
242 0 : ulong total_message_tx_bytes[ FD_METRICS_ENUM_GOSSIP_MESSAGE_TYPE_CNT ] = {0};
243 0 : for( ulong i=0UL; i<FD_METRICS_ENUM_GOSSIP_MESSAGE_TYPE_CNT; i++ ) {
244 0 : total_message_tx[ i ] = metrics->message_tx[ i ] + active_set_metrics->message_tx[ i ];
245 0 : total_message_tx_bytes[ i ] = metrics->message_tx_bytes[ i ] + active_set_metrics->message_tx_bytes[ i ];
246 0 : }
247 0 : FD_MCNT_ENUM_COPY( GOSSIP, MESSAGE_TX, total_message_tx );
248 0 : FD_MCNT_ENUM_COPY( GOSSIP, MESSAGE_TX_BYTES, total_message_tx_bytes );
249 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_PUSH_TX, active_set_metrics->crds_tx_push );
250 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_PUSH_TX_BYTES, active_set_metrics->crds_tx_push_bytes );
251 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_PULL_RESPONSE_TX, metrics->crds_tx_pull_response );
252 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_PULL_RESPONSE_TX_BYTES, metrics->crds_tx_pull_response_bytes );
253 :
254 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_UPSERTED, metrics->crds_rx_count );
255 :
256 0 : FD_MCNT_SET( GOSSIP, SIGN_QUEUE_FULL_DROPPED, metrics->sign_pend_full_cnt );
257 :
258 0 : FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STAKED_PEER_ONLINE, ctx->wfs_peers.online );
259 0 : FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STAKE_ONLINE_LAMPORTS, ctx->wfs_stake.online );
260 0 : FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STATE, (ulong)ctx->wfs_state );
261 0 : }
262 :
263 : /* Minimum quiet period (no new peers discovered) before we declare
264 : the gossip peer table saturated. Pull requests fire every ~1.6ms,
265 : so 500ms of silence means ~300 pulls returned no new contact
266 : infos — a strong convergence signal. */
267 : #define FD_GOSSIP_PEER_SAT_QUIET_NS (500L*1000L*1000L)
268 :
269 : static void
270 : after_credit( fd_gossip_tile_ctx_t * ctx,
271 : fd_stem_context_t * stem,
272 : int * opt_poll_in,
273 0 : int * charge_busy ) {
274 0 : ctx->stem = stem;
275 :
276 0 : if( FD_UNLIKELY( ctx->is_pending_set_identity ) ) {
277 : /* the identity key is swapped after the sign tile has been swapped
278 : because the below function directly sends a sign request. */
279 0 : FD_BASE58_ENCODE_32_BYTES( ctx->keyswitch->bytes, _new_id_b58 );
280 0 : fd_gossip_set_identity( ctx->gossip,
281 0 : ctx->keyswitch->bytes,
282 0 : fd_clock_tile_now( ctx->clock ),
283 0 : (ulong)FD_NANOSEC_TO_MICRO( ctx->keyswitch->param ) );
284 0 : ctx->is_halting_signing = 0;
285 0 : ctx->is_pending_set_identity = 0;
286 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
287 0 : *charge_busy = 1;
288 0 : return;
289 0 : }
290 :
291 0 : if( FD_UNLIKELY( !ctx->my_contact_info->shred_version ) ) return;
292 :
293 0 : if( FD_UNLIKELY( ctx->wfs_state==FD_GOSSIP_WFS_STATE_PUBLISH ) ) {
294 0 : fd_gossip_out_ctx_t const * out = ctx->update_out + fd_gossip_update_link( FD_GOSSIP_UPDATE_TAG_WFS_DONE );
295 0 : fd_stem_publish( ctx->stem, out->idx, FD_GOSSIP_UPDATE_TAG_WFS_DONE, out->chunk, 0UL, 0UL, 0UL, 0UL );
296 0 : ctx->wfs_state = FD_GOSSIP_WFS_STATE_DONE;
297 0 : *opt_poll_in = 0;
298 0 : *charge_busy = 1;
299 0 : return;
300 0 : }
301 :
302 : /* Prevent attempts to sign with our old identity while switching to
303 : the new one. */
304 0 : if( FD_UNLIKELY( ctx->is_halting_signing ) ) return;
305 :
306 : /* The stem's tick, at most one loop iteration stale, saves an rdtsc
307 : per iteration. */
308 0 : long now = fd_clock_tile_tickcount_to_wallclock( ctx->clock, stem->now );
309 0 : fd_gossip_advance( ctx->gossip, now, stem, charge_busy );
310 :
311 : /* Peer table saturation detection. After fd_gossip_advance updates
312 : the CRDS, check if the peer count has grown. If it hasn't grown
313 : for FD_GOSSIP_PEER_SAT_QUIET_NS and there is at least one other
314 : peer, publish a one-shot PEER_SATURATED notification. */
315 0 : if( FD_LIKELY( !ctx->peer_sat_published ) ) {
316 0 : fd_crds_metrics_t const * crds_metrics = fd_gossip_crds_metrics( ctx->gossip );
317 0 : ulong peer_cnt = crds_metrics->peer_staked_cnt + crds_metrics->peer_unstaked_cnt;
318 0 : if( FD_UNLIKELY( peer_cnt>ctx->peer_sat_hwm ) ) {
319 0 : ctx->peer_sat_hwm = peer_cnt;
320 0 : ctx->peer_sat_hwm_nanos = now;
321 0 : } else if( FD_UNLIKELY( peer_cnt>1UL && ctx->peer_sat_hwm_nanos!=0L &&
322 0 : (now-ctx->peer_sat_hwm_nanos)>FD_GOSSIP_PEER_SAT_QUIET_NS ) ) {
323 0 : FD_LOG_INFO(( "gossip peer table saturated (%lu peers, quiet for %ld ms)",
324 0 : peer_cnt, (now-ctx->peer_sat_hwm_nanos)/(1000L*1000L) ));
325 0 : fd_gossip_out_ctx_t const * out = ctx->update_out + fd_gossip_update_link( FD_GOSSIP_UPDATE_TAG_PEER_SATURATED );
326 0 : fd_stem_publish( ctx->stem, out->idx, FD_GOSSIP_UPDATE_TAG_PEER_SATURATED, out->chunk, 0UL, 0UL, 0UL, 0UL );
327 0 : ctx->peer_sat_published = 1;
328 0 : *opt_poll_in = 0;
329 0 : *charge_busy = 1;
330 0 : }
331 0 : }
332 0 : }
333 :
334 : static void
335 : handle_shred_version( fd_gossip_tile_ctx_t * ctx,
336 0 : ulong sig ) {
337 0 : ctx->my_contact_info->shred_version = (ushort)sig;
338 0 : fd_gossip_set_shred_version( ctx->gossip, (ushort)sig );
339 0 : }
340 :
341 : static void
342 : handle_epoch( fd_gossip_tile_ctx_t * ctx,
343 0 : fd_epoch_info_msg_t const * msg ) {
344 0 : if( FD_UNLIKELY( msg->staked_vote_cnt>MAX_STAKE_WEIGHTS ) )
345 0 : FD_LOG_ERR(( "epoch stakes exceed MAX_STAKE_WEIGHTS=%lu", MAX_STAKE_WEIGHTS ));
346 0 : if( FD_UNLIKELY( msg->staked_id_cnt>MAX_STAKE_WEIGHTS ) )
347 0 : FD_LOG_ERR(( "epoch id weights exceed MAX_STAKE_WEIGHTS=%lu", MAX_STAKE_WEIGHTS ));
348 :
349 0 : fd_stake_weight_t const * weights = fd_epoch_info_msg_id_weights( msg );
350 0 : fd_gossip_stakes_update( ctx->gossip, weights, msg->staked_id_cnt );
351 :
352 : /* Wait for supermajority uses the stakes of the epoch being
353 : resumed, which is the first epoch message replay sends at boot.
354 : The list is filled once; the manifest handler owns wfs_state. */
355 0 : if( FD_LIKELY( ctx->wfs_state==FD_GOSSIP_WFS_STATE_DONE || ctx->wfs_stakes_cnt ) ) return;
356 :
357 0 : fd_memcpy( ctx->wfs_stakes, weights, msg->staked_id_cnt*sizeof(fd_stake_weight_t) );
358 0 : ctx->wfs_stakes_cnt = msg->staked_id_cnt;
359 0 : for( ulong i=0UL; i<ctx->wfs_stakes_cnt; i++ ) {
360 0 : ctx->wfs_stake.total += ctx->wfs_stakes[ i ].stake;
361 0 : }
362 :
363 : /* sort for quick lookup */
364 0 : fd_stake_weight_key_sort_inplace( ctx->wfs_stakes, ctx->wfs_stakes_cnt );
365 :
366 0 : ctx->wfs_peers.total = ctx->wfs_stakes_cnt;
367 0 : FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STAKED_PEER_COUNT, ctx->wfs_peers.total );
368 0 : FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STAKE_TRACKED_LAMPORTS, ctx->wfs_stake.total );
369 :
370 0 : FD_LOG_NOTICE(( "wait for supermajority: tracking epoch %lu, %lu staked peers, %lu total stake",
371 0 : msg->epoch, ctx->wfs_peers.total, ctx->wfs_stake.total ));
372 0 : }
373 :
374 : static void
375 : handle_packet( fd_gossip_tile_ctx_t * ctx,
376 : ulong sig,
377 : uchar const * payload,
378 : ulong payload_sz,
379 0 : fd_stem_context_t * stem ) {
380 0 : long now = fd_clock_tile_now( ctx->clock );
381 :
382 0 : fd_ip4_port_t peer = (fd_ip4_port_t){
383 0 : .addr = fd_gossvf_sig_addr( sig ),
384 0 : .port = fd_gossvf_sig_port( sig )
385 0 : };
386 :
387 0 : switch( fd_gossvf_sig_kind( sig ) ) {
388 0 : case 0: {
389 0 : fd_gossip_rx( ctx->gossip, peer, payload, payload_sz, now, stem );
390 0 : fd_gossip_advance( ctx->gossip, now, stem, NULL );
391 0 : break;
392 0 : }
393 0 : case 1: {
394 0 : fd_gossip_pingreq_t * pingreq = (fd_gossip_pingreq_t *)payload;
395 0 : fd_gossip_ping_tracker_track( ctx->gossip, pingreq->pubkey.uc, peer, now );
396 0 : }
397 0 : }
398 0 : }
399 :
400 : static inline int
401 : before_frag( fd_gossip_tile_ctx_t * ctx,
402 : ulong in_idx,
403 : ulong seq FD_PARAM_UNUSED,
404 0 : ulong sig FD_PARAM_UNUSED ) {
405 0 : if( FD_UNLIKELY( ctx->in[ in_idx ].kind==IN_KIND_SIGN ) ) return 0;
406 :
407 : /* Defer frag processing while switching identity or learning shred
408 : version */
409 0 : if( FD_UNLIKELY( ctx->is_halting_signing || ( !ctx->my_contact_info->shred_version && ctx->in[ in_idx ].kind!=IN_KIND_SHRED_VERSION ) ) ) return -1;
410 :
411 0 : return 0;
412 0 : }
413 :
414 : static inline void
415 : during_frag( fd_gossip_tile_ctx_t * ctx,
416 : ulong in_idx,
417 : ulong seq FD_PARAM_UNUSED,
418 : ulong sig FD_PARAM_UNUSED,
419 : ulong chunk,
420 : ulong sz,
421 0 : ulong ctl FD_PARAM_UNUSED ) {
422 0 : switch( ctx->in[ in_idx ].kind ) {
423 0 : case IN_KIND_GOSSVF: {
424 0 : if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
425 0 : FD_LOG_ERR(( "chunk %lu %lu from in %d corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].kind, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
426 :
427 0 : fd_memcpy( ctx->gossvf_staged, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ), sz );
428 0 : break;
429 0 : }
430 0 : case IN_KIND_SIGN: {
431 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=64UL ) )
432 0 : FD_LOG_ERR(( "chunk %lu %lu from in %d corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].kind, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
433 :
434 0 : fd_memcpy( ctx->sign_staged, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ), 64UL );
435 0 : break;
436 0 : }
437 0 : }
438 0 : }
439 :
440 : static inline void
441 : after_frag( fd_gossip_tile_ctx_t * ctx,
442 : ulong in_idx,
443 : ulong seq FD_PARAM_UNUSED,
444 : ulong sig,
445 : ulong sz,
446 : ulong tsorig FD_PARAM_UNUSED,
447 : ulong tspub FD_PARAM_UNUSED,
448 0 : fd_stem_context_t * stem ) {
449 0 : ctx->stem = stem;
450 :
451 0 : switch( ctx->in[ in_idx ].kind ) {
452 0 : case IN_KIND_GOSSVF: {
453 :
454 0 : FD_TEST( sz<=sizeof(ctx->gossvf_staged) );
455 :
456 0 : handle_packet( ctx, sig, ctx->gossvf_staged, sz, stem );
457 0 : break;
458 0 : }
459 0 : case IN_KIND_SIGN: {
460 0 : fd_gossip_sign_response( ctx->gossip, ctx->sign_staged, stem, fd_clock_tile_now( ctx->clock ) );
461 0 : if( FD_UNLIKELY( ctx->is_halting_signing && !fd_gossip_sign_pend_cnt( ctx->gossip ) &&
462 0 : fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
463 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
464 0 : }
465 0 : break;
466 0 : }
467 0 : }
468 0 : }
469 :
470 : static inline int
471 : returnable_frag( fd_gossip_tile_ctx_t * ctx,
472 : ulong in_idx,
473 : ulong seq FD_PARAM_UNUSED,
474 : ulong sig,
475 : ulong chunk,
476 : ulong sz,
477 : ulong ctl FD_PARAM_UNUSED,
478 : ulong tsorig FD_PARAM_UNUSED,
479 : ulong tspub FD_PARAM_UNUSED,
480 0 : fd_stem_context_t * stem ) {
481 :
482 : /* Return early for unreliable links. */
483 0 : if( FD_UNLIKELY( ctx->in[ in_idx ].kind==IN_KIND_GOSSVF || ctx->in[ in_idx ].kind==IN_KIND_SIGN ) ) return 0;
484 :
485 0 : ctx->stem = stem;
486 :
487 0 : if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
488 0 : FD_LOG_ERR(( "chunk %lu %lu from in %d corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].kind, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
489 :
490 0 : switch( ctx->in[ in_idx ].kind ) {
491 0 : case IN_KIND_SHRED_VERSION: handle_shred_version( ctx, sig ); break;
492 0 : case IN_KIND_EPOCH: handle_epoch( ctx, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ) ); break;
493 0 : case IN_KIND_SNAPIN_MANIF: {
494 0 : if( FD_LIKELY( ctx->wfs_state==FD_GOSSIP_WFS_STATE_DONE ) ) break;
495 :
496 0 : if( FD_UNLIKELY( fd_ssmsg_sig_message( sig )==FD_SSMSG_DONE ) ) {
497 : /* Classify boot against WFS (see fd_wfs.h):
498 : MODE_ERROR -> keep state, never signal WFS_DONE
499 : MODE_UNRESOLVED -> error condition
500 : MODE_MATCH -> STATE_WAIT
501 : MODE_DISABLED -> STATE_DONE
502 : MODE_NOOP -> STATE_DONE
503 :
504 : ERROR is fatal, but replay owns that decision. */
505 0 : int wfs_mode = fd_wfs_mode( ctx->wfs_slot, ctx->wfs_hash_is_zero, (ulong)ctx->wfs_shred_version, ctx->wfs_boot_slot );
506 0 : if( FD_UNLIKELY( wfs_mode==FD_WFS_MODE_ERROR ) ) {
507 0 : FD_LOG_WARNING(( "boot slot %lu is behind [consensus.wait_for_supermajority_at_slot] %lu; "
508 0 : "the supermajority wait will not complete",
509 0 : ctx->wfs_boot_slot, ctx->wfs_slot ));
510 0 : break;
511 0 : }
512 0 : if( FD_UNLIKELY( wfs_mode==FD_WFS_MODE_UNRESOLVED ) ) {
513 0 : FD_LOG_CRIT(( "snapshot DONE received before any manifest" ));
514 0 : }
515 0 : ctx->wfs_state = fd_int_if( wfs_mode==FD_WFS_MODE_MATCH,
516 0 : FD_GOSSIP_WFS_STATE_WAIT, FD_GOSSIP_WFS_STATE_DONE );
517 0 : break;
518 0 : }
519 :
520 0 : fd_snapshot_manifest_t const * manifest = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
521 :
522 : /* Latest manifest, not a running max (see fd_wfs.h): a stale max
523 : would classify NOOP here against replay's MATCH, and deadlock. */
524 0 : ctx->wfs_boot_slot = manifest->slot;
525 0 : break;
526 0 : }
527 0 : default: FD_LOG_ERR(( "unreachable" ));
528 0 : }
529 :
530 0 : return 0;
531 0 : }
532 :
533 : static void
534 : privileged_init( fd_topo_t const * topo,
535 0 : fd_topo_tile_t const * tile ) {
536 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
537 :
538 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
539 0 : fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
540 0 : fd_memset( ctx, 0, sizeof(fd_gossip_tile_ctx_t) );
541 :
542 0 : if( FD_UNLIKELY( !strcmp( tile->gossip.identity_key_path, "" ) ) )
543 0 : FD_LOG_ERR(( "identity_key_path not set" ));
544 :
545 0 : ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->gossip.identity_key_path, /* pubkey only: */ 1 ) );
546 0 : FD_TEST( fd_rng_secure( &ctx->rng_seed, 4UL ) );
547 0 : FD_TEST( fd_rng_secure( &ctx->rng_idx, 8UL ) );
548 0 : FD_TEST( fd_rng_secure( ctx->ping_seed, 32UL ) );
549 0 : FD_TEST( fd_rng_secure( &ctx->gui_bw_seed, 8UL ) );
550 :
551 0 : FD_TEST( tile->gossip.entrypoints_cnt<=FD_TOPO_GOSSIP_ENTRYPOINTS_MAX );
552 0 : ctx->entrypoints_cnt = tile->gossip.entrypoints_cnt;
553 0 : fd_dns_resolve_peers( tile->gossip.entrypoints[ 0 ], sizeof(tile->gossip.entrypoints[ 0 ]), tile->gossip.entrypoints_cnt, "gossip.entrypoints", ctx->entrypoints );
554 :
555 0 : if( FD_LIKELY( tile->gossip.gossip_host[ 0 ]!='\0' ) ) {
556 0 : if( FD_UNLIKELY( !fd_dns_resolve_address( tile->gossip.gossip_host, &ctx->gossip_ip_addr ) ) ) FD_LOG_ERR(( "failed to resolve gossip host `%s`", tile->gossip.gossip_host ));
557 0 : } else {
558 0 : ctx->gossip_ip_addr = tile->gossip.net_ip_addr;
559 0 : }
560 0 : }
561 :
562 : static inline fd_gossip_out_ctx_t
563 : out1( fd_topo_t const * topo,
564 : fd_topo_tile_t const * tile,
565 0 : char const * name ) {
566 0 : ulong idx = ULONG_MAX;
567 :
568 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
569 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
570 0 : if( !strcmp( link->name, name ) ) {
571 0 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had multiple output links named %s but expected one", tile->name, tile->kind_id, name ));
572 0 : idx = i;
573 0 : }
574 0 : }
575 :
576 0 : if( FD_UNLIKELY( idx==ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had no output link named %s", tile->name, tile->kind_id, name ));
577 :
578 0 : ulong mtu = topo->links[ tile->out_link_id[ idx ] ].mtu;
579 0 : if( FD_UNLIKELY( mtu==0UL ) ) return (fd_gossip_out_ctx_t){ .idx = idx, .mem = NULL, .chunk0 = ULONG_MAX, .wmark = ULONG_MAX, .chunk = ULONG_MAX };
580 :
581 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
582 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
583 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
584 :
585 0 : return (fd_gossip_out_ctx_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
586 0 : }
587 :
588 : static void
589 : unprivileged_init( fd_topo_t const * topo,
590 0 : fd_topo_tile_t const * tile ) {
591 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
592 :
593 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
594 0 : fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
595 0 : void * _gossip = FD_SCRATCH_ALLOC_APPEND( l, fd_gossip_align(), fd_gossip_footprint( tile->gossip.max_entries, tile->gossip.entrypoints_cnt ) );
596 0 : void * _gui_bw = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_gossip_bw_align(), fd_gui_gossip_bw_footprint() );
597 :
598 0 : FD_TEST( fd_rng_join( fd_rng_new( ctx->rng, ctx->rng_seed, ctx->rng_idx ) ) );
599 :
600 : /* If WFS is enabled (see fd_wfs.h), start in INIT and let the
601 : manifest slot resolve the mode; otherwise immediately DONE. */
602 0 : ctx->wfs_slot = tile->gossip.wait_for_supermajority_at_slot;
603 0 : ctx->wfs_shred_version = tile->gossip.expected_shred_version;
604 0 : ctx->wfs_boot_slot = ULONG_MAX;
605 0 : ctx->wfs_hash_is_zero = tile->gossip.wait_for_supermajority_hash_is_zero;
606 0 : ctx->wfs_state = fd_int_if( fd_wfs_configured( ctx->wfs_slot, ctx->wfs_hash_is_zero, (ulong)ctx->wfs_shred_version ),
607 0 : FD_GOSSIP_WFS_STATE_INIT, FD_GOSSIP_WFS_STATE_DONE );
608 :
609 0 : FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
610 0 : ulong sign_in_tile_idx = ULONG_MAX;
611 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
612 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
613 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
614 :
615 0 : ctx->in[ i ].mem = link_wksp->wksp;
616 0 : if( FD_LIKELY( link->mtu ) ) {
617 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
618 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
619 0 : }
620 0 : ctx->in[ i ].mtu = link->mtu;
621 :
622 0 : if( FD_UNLIKELY( !strcmp( link->name, "ipecho_out" ) ) ) {
623 0 : ctx->in[ i ].kind = IN_KIND_SHRED_VERSION;
624 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "gossvf_gossip" ) ) ) {
625 0 : ctx->in[ i ].kind = IN_KIND_GOSSVF;
626 0 : FD_TEST( link->mtu<=sizeof(ctx->gossvf_staged) );
627 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "sign_gossip" ) ) ) {
628 0 : ctx->in[ i ].kind = IN_KIND_SIGN;
629 0 : sign_in_tile_idx = i;
630 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "replay_epoch" ) ) ) {
631 0 : ctx->in[ i ].kind = IN_KIND_EPOCH;
632 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "snapin_manif" ) ) ) {
633 0 : ctx->in[ i ].kind = IN_KIND_SNAPIN_MANIF;
634 0 : } else {
635 0 : FD_LOG_ERR(( "unexpected input link name %s", link->name ));
636 0 : }
637 0 : }
638 :
639 0 : if( FD_UNLIKELY( sign_in_tile_idx==ULONG_MAX ) )
640 0 : FD_LOG_ERR(( "tile %s:%lu had no input link named sign_gossip", tile->name, tile->kind_id ));
641 :
642 0 : *ctx->net_out = out1( topo, tile, "gossip_net" );
643 0 : *ctx->sign_out = out1( topo, tile, "gossip_sign" );
644 0 : ctx->update_out[ FD_GOSSIP_UPDATE_LINK_CI_ADDR ] = out1( topo, tile, "gossip_ciaddr" );
645 0 : ctx->update_out[ FD_GOSSIP_UPDATE_LINK_CI_SEEN ] = fd_topo_find_tile_out_link( topo, tile, "gossip_ciseen", 0UL )!=ULONG_MAX
646 0 : ? out1( topo, tile, "gossip_ciseen" )
647 0 : : (fd_gossip_out_ctx_t){ .idx = ULONG_MAX }; /* no gui */
648 0 : ctx->update_out[ FD_GOSSIP_UPDATE_LINK_MISC ] = out1( topo, tile, "gossip_misc" );
649 0 : *ctx->gossvf_out = out1( topo, tile, "gossip_gossvf" );
650 :
651 0 : ctx->has_gui = fd_topo_find_tile_out_link( topo, tile, "gossip_gui", 0UL )!=ULONG_MAX;
652 0 : if( FD_LIKELY( ctx->has_gui ) ) *ctx->gui_out = out1( topo, tile, "gossip_gui" );
653 0 : ctx->gui_bw = fd_gui_gossip_bw_join( fd_gui_gossip_bw_new( _gui_bw, ctx->gui_bw_seed ) );
654 0 : FD_TEST( ctx->gui_bw );
655 :
656 0 : fd_topo_link_t const * sign_in = &topo->links[ tile->in_link_id [ sign_in_tile_idx ] ];
657 0 : fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ ctx->sign_out->idx ] ];
658 0 : FD_TEST( sign_in->mtu==64UL );
659 0 : FD_TEST( sign_in->depth>=FD_GOSSIP_SIGN_PEND_MAX && sign_out->depth>=FD_GOSSIP_SIGN_PEND_MAX );
660 0 : ctx->sign_out_mtu = sign_out->mtu;
661 :
662 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
663 0 : FD_TEST( ctx->keyswitch );
664 :
665 0 : fd_clock_tile_init( ctx->clock );
666 :
667 0 : ctx->my_contact_info->shred_version = tile->gossip.expected_shred_version;
668 :
669 0 : ctx->my_contact_info->outset = (ulong)FD_NANOSEC_TO_MICRO( tile->gossip.boot_timestamp_nanos );
670 :
671 0 : ctx->my_contact_info->version.client = FD_GOSSIP_CONTACT_INFO_CLIENT_FIREDANCER;
672 0 : ctx->my_contact_info->version.major = (ushort)fd_major_version;
673 0 : ctx->my_contact_info->version.minor = (ushort)fd_minor_version;
674 0 : ctx->my_contact_info->version.patch = (ushort)fd_patch_version;
675 0 : ctx->my_contact_info->version.commit = fd_commit_ref_u32;
676 0 : ctx->my_contact_info->version.feature_set = FD_FEATURE_SET_ID;
677 :
678 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_GOSSIP ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.gossip ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.gossip ) };
679 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.tvu ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tvu ) };
680 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TPU ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.tpu ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
681 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_FORWARDS ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.tpu ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
682 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_QUIC ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.tpu_quic ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
683 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_VOTE_QUIC ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.tpu_quic ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
684 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_FORWARDS_QUIC ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.tpu_quic ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
685 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_VOTE ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.tpu ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
686 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_SERVE_REPAIR ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.rserve ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.rserve ) };
687 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_SERVE_REPAIR_QUIC ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.rserve ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.rserve ) };
688 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_ALPENGLOW ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = tile->gossip.ports.votor ? ctx->gossip_ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.votor ) };
689 :
690 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU_QUIC ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = 0, .port = 0 };
691 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = 0, .port = 0 };
692 0 : ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC_PUBSUB ] = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = 0, .port = 0 };
693 :
694 0 : ctx->gossip = fd_gossip_join( fd_gossip_new( _gossip,
695 0 : ctx->rng,
696 0 : ctx->ping_seed,
697 0 : tile->gossip.max_entries,
698 0 : ctx->entrypoints_cnt,
699 0 : ctx->entrypoints,
700 0 : ctx->identity_key->uc,
701 0 : ctx->my_contact_info,
702 0 : fd_clock_tile_now( ctx->clock ),
703 0 : gossip_send_fn,
704 0 : ctx,
705 0 : gossip_sign_fn,
706 0 : ctx,
707 0 : gossip_ping_tracker_change_fn,
708 0 : ctx,
709 0 : gossip_activity_update_fn,
710 0 : ctx,
711 0 : ctx->update_out,
712 0 : ctx->net_out ) );
713 0 : FD_TEST( ctx->gossip );
714 :
715 0 : FD_MGAUGE_SET( GOSSIP, CRDS_CAPACITY, tile->gossip.max_entries );
716 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_CAPACITY, FD_CONTACT_INFO_TABLE_SIZE );
717 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PURGED_CAPACITY, tile->gossip.max_entries );
718 :
719 0 : fd_ip4_udp_hdr_init( ctx->net_out_hdr, FD_GOSSIP_MTU, tile->gossip.bind_ip_addr, tile->gossip.ports.gossip );
720 :
721 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
722 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
723 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
724 0 : }
725 :
726 : static ulong
727 : populate_allowed_seccomp( fd_topo_t const * topo,
728 : fd_topo_tile_t const * tile,
729 : ulong out_cnt,
730 0 : struct sock_filter * out ) {
731 0 : (void)topo;
732 0 : (void)tile;
733 :
734 0 : populate_sock_filter_policy_fd_gossip_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
735 0 : return sock_filter_policy_fd_gossip_tile_instr_cnt;
736 0 : }
737 :
738 : static ulong
739 : populate_allowed_fds( fd_topo_t const * topo,
740 : fd_topo_tile_t const * tile,
741 : ulong out_fds_cnt,
742 0 : int * out_fds ) {
743 0 : (void)topo;
744 0 : (void)tile;
745 :
746 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
747 :
748 0 : ulong out_cnt = 0UL;
749 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
750 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
751 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
752 0 : return out_cnt;
753 0 : }
754 :
755 : /* STEM_BURST must bound the maximum number of fd_stem_publish
756 : calls on any single output link between two consecutive
757 : credit checks in the stem run loop. One iteration consists
758 : of after_credit (which calls fd_gossip_advance) followed by
759 : processing one input fragment (returnable_frag). A gossip packet
760 : handler calls fd_gossip_advance again after fd_gossip_rx.
761 :
762 : The reliable output links and their per-iteration worst cases:
763 :
764 : gossvf_out (via gossip_ping_tracker_change_fn):
765 : tx_ping evictions + expiries FD_PING_TRACKER_MAX
766 : fd_ping_tracker_track from rx_values 2*FD_GOSSIP_MESSAGE_MAX_CRDS
767 : Total: FD_PING_TRACKER_MAX + 2*FD_GOSSIP_MESSAGE_MAX_CRDS
768 :
769 : gossip_ciaddr and gossip_ciseen (via fd_gossip_tx_publish_chunk),
770 : each at most one frag per contact info event:
771 : fd_crds_advance expire (ContactInfos) FD_CONTACT_INFO_TABLE_SIZE
772 : fd_crds_insert publish + evictions 2*FD_GOSSIP_MESSAGE_MAX_CRDS
773 : Total: FD_CONTACT_INFO_TABLE_SIZE + 2*FD_GOSSIP_MESSAGE_MAX_CRDS
774 :
775 : gossip_misc carries a subset of the fd_crds_insert publishes
776 : (at most FD_GOSSIP_MESSAGE_MAX_CRDS, plus local pushes), so the
777 : contact info links bound it.
778 :
779 : Among the reliable output links, gossvf_out dominates. Every
780 : reliable out link must be at least STEM_BURST deep. */
781 : FD_STATIC_ASSERT( FD_PING_TRACKER_MAX+2UL*FD_GOSSIP_MESSAGE_MAX_CRDS>=FD_CONTACT_INFO_TABLE_SIZE+2UL*FD_GOSSIP_MESSAGE_MAX_CRDS, "STEM_BURST does not account for worst case output link" );
782 :
783 : /* Raw state values exported by the metrics (states= enums) */
784 : FD_STATIC_ASSERT( FD_GOSSIP_WFS_STATE_INIT==FD_METRICS_ENUM_WAIT_FOR_SUPERMAJORITY_STATE_V_LOADING_STAKES_IDX, metrics_state );
785 : FD_STATIC_ASSERT( FD_GOSSIP_WFS_STATE_WAIT==FD_METRICS_ENUM_WAIT_FOR_SUPERMAJORITY_STATE_V_WAITING_FOR_STAKE_IDX, metrics_state );
786 : FD_STATIC_ASSERT( FD_GOSSIP_WFS_STATE_PUBLISH==FD_METRICS_ENUM_WAIT_FOR_SUPERMAJORITY_STATE_V_PUBLISHING_IDX, metrics_state );
787 : FD_STATIC_ASSERT( FD_GOSSIP_WFS_STATE_DONE==FD_METRICS_ENUM_WAIT_FOR_SUPERMAJORITY_STATE_V_DONE_OR_DISABLED_IDX, metrics_state );
788 0 : #define STEM_BURST (FD_PING_TRACKER_MAX+2UL*FD_GOSSIP_MESSAGE_MAX_CRDS)
789 :
790 0 : #define STEM_LAZY (128L*3000L)
791 :
792 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_gossip_tile_ctx_t
793 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gossip_tile_ctx_t)
794 :
795 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
796 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
797 0 : #define STEM_CALLBACK_NEXT_DEADLINE next_deadline
798 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
799 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
800 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
801 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
802 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
803 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
804 :
805 : #include "../../disco/stem/fd_stem.c"
806 :
807 : fd_topo_run_tile_t fd_tile_gossip = {
808 : .name = "gossip",
809 : .populate_allowed_seccomp = populate_allowed_seccomp,
810 : .populate_allowed_fds = populate_allowed_fds,
811 : .scratch_align = scratch_align,
812 : .scratch_footprint = scratch_footprint,
813 : .privileged_init = privileged_init,
814 : .unprivileged_init = unprivileged_init,
815 : .run = stem_run,
816 : };
|