Line data Source code
1 : /* Gossip tile runs the gossip networking protcol for a Firedancer node. */
2 : #define _GNU_SOURCE
3 :
4 : #include "../../disco/topo/fd_topo.h"
5 : #include "generated/fd_gossip_tile_seccomp.h"
6 :
7 : #include "../../disco/fd_disco.h"
8 : #include "../../disco/keyguard/fd_keyload.h"
9 : #include "../../disco/keyguard/fd_keyguard_client.h"
10 : #include "../../disco/net/fd_net_tile.h"
11 : #include "../../flamenco/gossip/fd_gossip.h"
12 : #include "../../util/pod/fd_pod.h"
13 : #include "../../util/net/fd_ip4.h"
14 : #include "../../util/net/fd_udp.h"
15 : #include "../../util/net/fd_net_headers.h"
16 :
17 : #include <unistd.h>
18 : #include <arpa/inet.h>
19 : #include <linux/unistd.h>
20 : #include <sys/random.h>
21 : #include <netdb.h>
22 : #include <netinet/in.h>
23 : #include <sys/socket.h>
24 :
25 0 : #define CONTACT_INFO_PUBLISH_TIME_NS ((long)5e9)
26 : #define PLUGIN_PUBLISH_TIME_NS ((long)30e9)
27 :
28 0 : #define IN_KIND_NET (1)
29 0 : #define IN_KIND_SEND (2)
30 0 : #define IN_KIND_SIGN (4)
31 : #define MAX_IN_LINKS (8)
32 :
33 : static volatile ulong * fd_shred_version;
34 :
35 : static ulong
36 0 : fd_pubkey_hash( fd_pubkey_t const * key, ulong seed ) {
37 0 : return fd_hash( seed, key->key, sizeof(fd_pubkey_t) );
38 0 : }
39 :
40 : struct fd_contact_info_elem {
41 : fd_pubkey_t key;
42 : ulong next;
43 : fd_contact_info_t contact_info;
44 : };
45 : typedef struct fd_contact_info_elem fd_contact_info_elem_t;
46 :
47 : /* Contact info table */
48 : #define MAP_NAME fd_contact_info_table
49 : #define MAP_KEY_T fd_pubkey_t
50 0 : #define MAP_KEY_EQ fd_pubkey_eq
51 0 : #define MAP_KEY_HASH fd_pubkey_hash
52 0 : #define MAP_T fd_contact_info_elem_t
53 : #include "../../util/tmpl/fd_map_giant.c"
54 :
55 : struct fd_gossip_tile_metrics {
56 : ulong last_crds_push_contact_info_publish_ts;
57 : ulong mismatched_contact_info_shred_version;
58 :
59 : /* Below metrics are segmented by TVU, Repair, Send */
60 : ulong ipv6_contact_info[FD_METRICS_COUNTER_GOSSIP_IPV6_CONTACT_INFO_CNT];
61 : ulong zero_ipv4_contact_info[FD_METRICS_COUNTER_GOSSIP_ZERO_IPV4_CONTACT_INFO_CNT];
62 : ulong peer_counts[FD_METRICS_GAUGE_GOSSIP_PEER_COUNTS_CNT];
63 :
64 : ulong shred_version_zero;
65 : };
66 : typedef struct fd_gossip_tile_metrics fd_gossip_tile_metrics_t;
67 0 : #define FD_GOSSIP_TILE_METRICS_FOOTPRINT ( sizeof( fd_gossip_tile_metrics_t ) )
68 :
69 : typedef union {
70 : struct {
71 : fd_wksp_t * mem;
72 : ulong chunk0;
73 : ulong wmark;
74 : };
75 : fd_net_rx_bounds_t net_rx;
76 : } fd_gossip_in_ctx_t;
77 :
78 : struct fd_gossip_tile_ctx {
79 : fd_gossip_t * gossip;
80 : fd_gossip_config_t gossip_config;
81 : long last_shred_dest_push_time;
82 : long last_plugin_push_time;
83 :
84 : ulong gossip_seed;
85 :
86 : uchar in_kind[ MAX_IN_LINKS ];
87 : fd_gossip_in_ctx_t in_links[ MAX_IN_LINKS ];
88 :
89 : fd_contact_info_elem_t * contact_info_table;
90 :
91 : fd_frag_meta_t * shred_contact_out_mcache;
92 : ulong * shred_contact_out_sync;
93 : ulong shred_contact_out_depth;
94 : ulong shred_contact_out_seq;
95 :
96 : fd_wksp_t * shred_contact_out_mem;
97 : ulong shred_contact_out_chunk0;
98 : ulong shred_contact_out_wmark;
99 : ulong shred_contact_out_chunk;
100 :
101 : fd_frag_meta_t * repair_contact_out_mcache;
102 : ulong * repair_contact_out_sync;
103 : ulong repair_contact_out_depth;
104 : ulong repair_contact_out_seq;
105 :
106 : fd_wksp_t * repair_contact_out_mem;
107 : ulong repair_contact_out_chunk0;
108 : ulong repair_contact_out_wmark;
109 : ulong repair_contact_out_chunk;
110 :
111 : fd_frag_meta_t * send_contact_out_mcache;
112 : ulong * send_contact_out_sync;
113 : ulong send_contact_out_depth;
114 : ulong send_contact_out_seq;
115 :
116 : fd_wksp_t * send_contact_out_mem;
117 : ulong send_contact_out_chunk0;
118 : ulong send_contact_out_wmark;
119 : ulong send_contact_out_chunk;
120 :
121 : fd_frag_meta_t * verify_out_mcache;
122 : ulong * verify_out_sync;
123 : ulong verify_out_depth;
124 : ulong verify_out_seq;
125 :
126 : fd_wksp_t * verify_out_mem;
127 : ulong verify_out_chunk0;
128 : ulong verify_out_wmark;
129 : ulong verify_out_chunk;
130 :
131 : ulong tower_out_idx;
132 : fd_wksp_t * tower_out_mem;
133 : ulong tower_out_chunk0;
134 : ulong tower_out_wmark;
135 : ulong tower_out_chunk;
136 :
137 : fd_wksp_t * wksp;
138 : fd_gossip_peer_addr_t gossip_my_addr;
139 : fd_gossip_peer_addr_t tvu_my_addr;
140 : fd_gossip_peer_addr_t tpu_my_addr;
141 : fd_gossip_peer_addr_t tpu_quic_my_addr;
142 : fd_gossip_peer_addr_t tpu_vote_my_addr;
143 : fd_gossip_peer_addr_t repair_serve_addr;
144 : ushort gossip_listen_port;
145 :
146 : fd_frag_meta_t * net_out_mcache;
147 : ulong * net_out_sync;
148 : ulong net_out_depth;
149 : ulong net_out_seq;
150 :
151 : fd_wksp_t * net_out_mem;
152 : ulong net_out_chunk0;
153 : ulong net_out_wmark;
154 : ulong net_out_chunk;
155 :
156 : // Inputs to plugin/gui
157 :
158 : fd_wksp_t * gossip_plugin_out_mem;
159 : ulong gossip_plugin_out_chunk0;
160 : ulong gossip_plugin_out_wmark;
161 : ulong gossip_plugin_out_chunk;
162 : ulong gossip_plugin_out_idx;
163 :
164 : uchar identity_private_key[32];
165 : fd_pubkey_t identity_public_key;
166 :
167 : /* Includes Ethernet, IP, UDP headers */
168 : uchar gossip_buffer[ FD_NET_MTU ];
169 :
170 : ushort net_id;
171 : fd_ip4_udp_hdrs_t hdr[1];
172 :
173 : fd_keyguard_client_t keyguard_client[1];
174 :
175 : fd_stem_context_t * stem;
176 :
177 : ulong replay_vote_txn_sz;
178 : uchar replay_vote_txn [ FD_TXN_MTU ];
179 :
180 : /* Metrics */
181 : fd_gossip_tile_metrics_t metrics;
182 : };
183 : typedef struct fd_gossip_tile_ctx fd_gossip_tile_ctx_t;
184 :
185 : FD_FN_CONST static inline ulong
186 0 : scratch_align( void ) {
187 0 : return 128UL;
188 0 : }
189 :
190 : FD_FN_PURE static inline ulong
191 0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
192 0 : return 1UL * FD_SHMEM_GIGANTIC_PAGE_SZ;
193 0 : }
194 :
195 : FD_FN_PURE static inline ulong
196 0 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
197 0 : ulong l = FD_LAYOUT_INIT;
198 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
199 0 : l = FD_LAYOUT_APPEND( l, fd_gossip_align(), fd_gossip_footprint() );
200 0 : l = FD_LAYOUT_APPEND( l, fd_contact_info_table_align(), fd_contact_info_table_footprint( FD_PEER_KEY_MAX ) );
201 0 : return FD_LAYOUT_FINI( l, scratch_align() );
202 0 : }
203 :
204 : static void
205 : send_packet( fd_gossip_tile_ctx_t * ctx,
206 : uint dst_ip_addr,
207 : ushort dst_port,
208 : uchar const * payload,
209 : ulong payload_sz,
210 0 : ulong tsorig ) {
211 0 : uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
212 :
213 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
214 0 : *hdr = *ctx->hdr;
215 :
216 0 : fd_ip4_hdr_t * ip4 = hdr->ip4;
217 0 : ip4->daddr = dst_ip_addr;
218 0 : ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
219 0 : ip4->check = 0U;
220 0 : ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
221 0 : ip4->check = fd_ip4_hdr_check_fast( ip4 );
222 :
223 0 : fd_udp_hdr_t * udp = hdr->udp;
224 0 : udp->net_dport = dst_port;
225 0 : udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
226 0 : fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), payload, payload_sz );
227 0 : udp->check = 0U;
228 :
229 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
230 0 : ulong sig = fd_disco_netmux_sig( dst_ip_addr, dst_port, dst_ip_addr, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
231 0 : ulong packet_sz = payload_sz + sizeof(fd_ip4_udp_hdrs_t);
232 0 : fd_stem_publish( ctx->stem, 0UL, sig, ctx->net_out_chunk, packet_sz, 0UL, tsorig, tspub );
233 0 : ctx->net_out_chunk = fd_dcache_compact_next( ctx->net_out_chunk, packet_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
234 0 : }
235 :
236 : static void
237 : gossip_send_packet( uchar const * msg,
238 : size_t msglen,
239 : fd_gossip_peer_addr_t const * addr,
240 0 : void * arg ) {
241 0 : ulong tsorig = fd_frag_meta_ts_comp( fd_tickcount() );
242 0 : send_packet( arg, addr->addr, addr->port, msg, msglen, tsorig );
243 0 : }
244 :
245 : static void
246 : gossip_deliver_fun( fd_crds_data_t * data,
247 0 : void * arg ) {
248 0 : fd_gossip_tile_ctx_t * ctx = (fd_gossip_tile_ctx_t *)arg;
249 :
250 0 : if( fd_crds_data_is_vote( data ) ) {
251 0 : if( FD_UNLIKELY( !ctx->verify_out_mcache ) ) return;
252 :
253 0 : fd_gossip_vote_t const * gossip_vote = &data->inner.vote;
254 :
255 0 : uchar * vote_txn_msg = fd_chunk_to_laddr( ctx->verify_out_mem, ctx->verify_out_chunk );
256 0 : ulong vote_txn_sz = gossip_vote->txn.raw_sz;
257 0 : memcpy( vote_txn_msg, gossip_vote->txn.raw, vote_txn_sz );
258 :
259 0 : ulong sig = 1UL;
260 0 : fd_mcache_publish( ctx->verify_out_mcache, ctx->verify_out_depth, ctx->verify_out_seq, sig, ctx->verify_out_chunk,
261 0 : vote_txn_sz, 0UL, 0, 0 );
262 0 : ctx->verify_out_seq = fd_seq_inc( ctx->verify_out_seq, 1UL );
263 0 : ctx->verify_out_chunk = fd_dcache_compact_next( ctx->verify_out_chunk, vote_txn_sz, ctx->verify_out_chunk0, ctx->verify_out_wmark );
264 :
265 0 : } else if( fd_crds_data_is_contact_info_v2( data ) ) {
266 0 : fd_gossip_contact_info_v2_t const * contact_info_v2 = &data->inner.contact_info_v2;
267 :
268 0 : fd_contact_info_elem_t * ele = fd_contact_info_table_query( ctx->contact_info_table, &contact_info_v2->from, NULL );
269 :
270 0 : if( FD_UNLIKELY( !ele &&
271 0 : !fd_contact_info_table_is_full( ctx->contact_info_table ) ) ) {
272 0 : ele = fd_contact_info_table_insert( ctx->contact_info_table, &contact_info_v2->from);
273 0 : fd_contact_info_init( &ele->contact_info );
274 0 : }
275 :
276 0 : if( FD_LIKELY( ele ) ) {
277 0 : fd_contact_info_from_ci_v2( contact_info_v2, &ele->contact_info );
278 0 : }
279 :
280 0 : } else if( fd_crds_data_is_duplicate_shred( data ) ) {
281 :
282 0 : fd_gossip_duplicate_shred_t const * duplicate_shred = &data->inner.duplicate_shred;
283 0 : uchar * chunk_laddr = fd_chunk_to_laddr( ctx->tower_out_mem, ctx->tower_out_chunk );
284 0 : memcpy( chunk_laddr, duplicate_shred, sizeof(fd_gossip_duplicate_shred_t) );
285 0 : memcpy( chunk_laddr + sizeof(fd_gossip_duplicate_shred_t), duplicate_shred->chunk, duplicate_shred->chunk_len );
286 0 : fd_stem_publish( ctx->stem, ctx->tower_out_idx, data->discriminant, ctx->tower_out_chunk, sizeof(fd_gossip_duplicate_shred_t) + duplicate_shred->chunk_len, 0UL, 0, 0 /* FIXME gossip tile needs to plumb through ts. this callback API is not ideal. */ );
287 :
288 0 : }
289 0 : }
290 :
291 : static void
292 : gossip_signer( void * signer_ctx,
293 : uchar signature[ static 64 ],
294 : uchar const * buffer,
295 : ulong len,
296 0 : int sign_type ) {
297 0 : fd_gossip_tile_ctx_t * ctx = (fd_gossip_tile_ctx_t *)signer_ctx;
298 0 : fd_keyguard_client_sign( ctx->keyguard_client, signature, buffer, len, sign_type );
299 0 : }
300 :
301 : static void
302 0 : during_housekeeping( fd_gossip_tile_ctx_t * ctx ) {
303 0 : fd_gossip_settime( ctx->gossip, fd_log_wallclock() );
304 0 : }
305 :
306 : static inline int
307 : before_frag( fd_gossip_tile_ctx_t * ctx,
308 : ulong in_idx,
309 : ulong seq FD_PARAM_UNUSED,
310 0 : ulong sig ) {
311 0 : uint in_kind = ctx->in_kind[ in_idx ];
312 0 : return in_kind != IN_KIND_SEND && fd_disco_netmux_sig_proto( sig ) != DST_PROTO_GOSSIP;
313 0 : }
314 :
315 : static inline void
316 : during_frag( fd_gossip_tile_ctx_t * ctx,
317 : ulong in_idx,
318 : ulong seq FD_PARAM_UNUSED,
319 : ulong sig FD_PARAM_UNUSED,
320 : ulong chunk,
321 : ulong sz,
322 0 : ulong ctl ) {
323 :
324 0 : uint in_kind = ctx->in_kind[ in_idx ];
325 0 : fd_gossip_in_ctx_t const * in_ctx = &ctx->in_links[ in_idx ];
326 :
327 0 : if( in_kind == IN_KIND_SEND ) {
328 0 : if( FD_UNLIKELY( chunk<in_ctx->chunk0 || chunk>in_ctx->wmark || sz>FD_TXN_MTU ) ) {
329 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, in_ctx->chunk0, in_ctx->wmark ));
330 0 : }
331 :
332 0 : ctx->replay_vote_txn_sz = sz;
333 0 : memcpy( ctx->replay_vote_txn, fd_chunk_to_laddr( in_ctx->mem, chunk ), sz );
334 0 : return;
335 0 : }
336 :
337 0 : if( in_kind!=IN_KIND_NET ) return;
338 :
339 0 : void const * src = fd_net_rx_translate_frag( &ctx->in_links[ in_idx ].net_rx, chunk, ctl, sz );
340 0 : fd_memcpy( ctx->gossip_buffer, src, sz );
341 0 : }
342 :
343 : static void
344 : after_frag( fd_gossip_tile_ctx_t * ctx,
345 : ulong in_idx,
346 : ulong seq FD_PARAM_UNUSED,
347 : ulong sig FD_PARAM_UNUSED,
348 : ulong sz,
349 : ulong tsorig FD_PARAM_UNUSED,
350 : ulong tspub FD_PARAM_UNUSED,
351 0 : fd_stem_context_t * stem ) {
352 0 : uint in_kind = ctx->in_kind[ in_idx ];
353 :
354 0 : if( in_kind==IN_KIND_SEND ) {
355 0 : fd_crds_data_t vote_txn_crds;
356 0 : vote_txn_crds.discriminant = fd_crds_data_enum_vote;
357 0 : vote_txn_crds.inner.vote.txn.raw_sz = ctx->replay_vote_txn_sz;
358 0 : memcpy( vote_txn_crds.inner.vote.txn.raw, ctx->replay_vote_txn, ctx->replay_vote_txn_sz );
359 0 : fd_txn_parse( vote_txn_crds.inner.vote.txn.raw, ctx->replay_vote_txn_sz, vote_txn_crds.inner.vote.txn.txn_buf, NULL );
360 :
361 0 : fd_gossip_push_value( ctx->gossip, &vote_txn_crds, NULL );
362 :
363 0 : static ulong sent_vote_cnt = 0;
364 0 : if ( ( ++sent_vote_cnt % 50 ) == 0 )
365 0 : FD_LOG_NOTICE(( "Gossip tile has sent %lu vote txns", sent_vote_cnt ));
366 :
367 0 : return;
368 0 : }
369 :
370 0 : if( in_kind!=IN_KIND_NET ) return;
371 :
372 0 : if( FD_UNLIKELY( sz<42 ) ) return;
373 :
374 0 : ctx->stem = stem;
375 0 : fd_eth_hdr_t const * eth = (fd_eth_hdr_t const *)ctx->gossip_buffer;
376 0 : fd_ip4_hdr_t const * ip4 = (fd_ip4_hdr_t const *)( (ulong)eth + sizeof(fd_eth_hdr_t) );
377 0 : fd_udp_hdr_t const * udp = (fd_udp_hdr_t const *)( (ulong)ip4 + FD_IP4_GET_LEN( *ip4 ) );
378 0 : uchar const * data = (uchar const *)( (ulong)udp + sizeof(fd_udp_hdr_t) );
379 0 : if( FD_UNLIKELY( (ulong)udp+sizeof(fd_udp_hdr_t) > (ulong)eth+sz ) ) return;
380 0 : ulong udp_sz = fd_ushort_bswap( udp->net_len );
381 0 : if( FD_UNLIKELY( udp_sz<sizeof(fd_udp_hdr_t) ) ) return;
382 0 : ulong data_sz = udp_sz-sizeof(fd_udp_hdr_t);
383 0 : if( FD_UNLIKELY( (ulong)data+data_sz > (ulong)eth+sz ) ) return;
384 :
385 0 : fd_gossip_peer_addr_t peer_addr = { .addr=ip4->saddr, .port=udp->net_sport };
386 0 : fd_gossip_recv_packet( ctx->gossip, data, data_sz, &peer_addr );
387 0 : }
388 :
389 : static void
390 : publish_peers_to_plugin( fd_gossip_tile_ctx_t * ctx,
391 0 : fd_stem_context_t * stem ) {
392 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->gossip_plugin_out_mem, ctx->gossip_plugin_out_chunk );
393 :
394 0 : ulong i = 0;
395 0 : for( fd_contact_info_table_iter_t iter = fd_contact_info_table_iter_init( ctx->contact_info_table );
396 0 : !fd_contact_info_table_iter_done( ctx->contact_info_table, iter ) && i < FD_CLUSTER_NODE_CNT;
397 0 : iter = fd_contact_info_table_iter_next( ctx->contact_info_table, iter ), ++i ) {
398 0 : fd_contact_info_elem_t const * ele = fd_contact_info_table_iter_ele_const( ctx->contact_info_table, iter );
399 0 : fd_gossip_update_msg_t * msg = (fd_gossip_update_msg_t *)(dst + sizeof(ulong) + i*FD_GOSSIP_LINK_MSG_SIZE);
400 0 : fd_contact_info_to_update_msg( &ele->contact_info, msg );
401 0 : }
402 :
403 0 : *(ulong *)dst = i;
404 :
405 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
406 0 : fd_stem_publish( stem, ctx->gossip_plugin_out_idx, FD_PLUGIN_MSG_GOSSIP_UPDATE, ctx->gossip_plugin_out_chunk, 0, 0UL, 0UL, tspub );
407 0 : ctx->gossip_plugin_out_chunk = fd_dcache_compact_next( ctx->gossip_plugin_out_chunk, 8UL + 40200UL*(58UL+12UL*34UL), ctx->gossip_plugin_out_chunk0, ctx->gossip_plugin_out_wmark );
408 0 : }
409 :
410 : static void
411 : after_credit( fd_gossip_tile_ctx_t * ctx,
412 : fd_stem_context_t * stem,
413 : int * opt_poll_in,
414 0 : int * charge_busy ) {
415 0 : (void)opt_poll_in;
416 :
417 : /* TODO: Don't charge the tile as busy if after_credit isn't actually
418 : doing any work. */
419 0 : *charge_busy = 1;
420 :
421 0 : ctx->stem = stem;
422 0 : ulong tsorig = fd_frag_meta_ts_comp( fd_tickcount() );
423 :
424 0 : if( FD_LIKELY( ctx->shred_contact_out_sync ) ) fd_mcache_seq_update( ctx->shred_contact_out_sync, ctx->shred_contact_out_seq );
425 0 : if( FD_LIKELY( ctx->repair_contact_out_sync ) ) fd_mcache_seq_update( ctx->repair_contact_out_sync, ctx->repair_contact_out_seq );
426 :
427 0 : long now = fd_gossip_gettime( ctx->gossip );
428 0 : if( ( now - ctx->last_shred_dest_push_time )>CONTACT_INFO_PUBLISH_TIME_NS &&
429 0 : ctx->shred_contact_out_mcache ) {
430 :
431 0 : ctx->metrics.last_crds_push_contact_info_publish_ts = (ulong)(ctx->last_shred_dest_push_time);
432 :
433 0 : ctx->last_shred_dest_push_time = now;
434 :
435 0 : ulong tvu_peer_cnt = 0;
436 0 : ulong repair_peers_cnt = 0;
437 0 : ulong send_peers_cnt = 0;
438 :
439 0 : ulong * shred_dest_msg = fd_chunk_to_laddr( ctx->shred_contact_out_mem, ctx->shred_contact_out_chunk );
440 0 : fd_shred_dest_wire_t * tvu_peers = (fd_shred_dest_wire_t *)(shred_dest_msg+1);
441 0 : fd_shred_dest_wire_t * repair_peers = fd_chunk_to_laddr( ctx->repair_contact_out_mem, ctx->repair_contact_out_chunk );
442 0 : fd_shred_dest_wire_t * send_peers = fd_chunk_to_laddr( ctx->send_contact_out_mem, ctx->send_contact_out_chunk );
443 0 : for( fd_contact_info_table_iter_t iter = fd_contact_info_table_iter_init( ctx->contact_info_table );
444 0 : !fd_contact_info_table_iter_done( ctx->contact_info_table, iter );
445 0 : iter = fd_contact_info_table_iter_next( ctx->contact_info_table, iter ) ) {
446 0 : fd_contact_info_elem_t const * ele = fd_contact_info_table_iter_ele_const( ctx->contact_info_table, iter );
447 0 : fd_contact_info_t const * ci = &ele->contact_info;
448 :
449 0 : if( fd_contact_info_get_shred_version( ci )!=fd_gossip_get_shred_version( ctx->gossip ) ) {
450 0 : ctx->metrics.mismatched_contact_info_shred_version += 1UL;
451 0 : continue;
452 0 : }
453 :
454 0 : {
455 0 : ushort tvu_socket_idx = ci->socket_tag_idx[ FD_GOSSIP_SOCKET_TAG_TVU ];
456 0 : if( tvu_socket_idx == FD_CONTACT_INFO_SOCKET_TAG_NULL ) {
457 0 : ctx->metrics.zero_ipv4_contact_info[ FD_METRICS_ENUM_PEER_TYPES_V_TVU_IDX ] += 1UL;
458 0 : continue;
459 0 : }
460 0 : if( !fd_gossip_ip_addr_is_ip4( &ci->addrs[ ci->sockets[ tvu_socket_idx].index ] )) {
461 0 : continue;
462 0 : }
463 :
464 :
465 0 : tvu_peers[tvu_peer_cnt].ip4_addr = ci->addrs[ ci->sockets[ tvu_socket_idx].index ].inner.ip4;
466 0 : tvu_peers[tvu_peer_cnt].udp_port = ci->ports[ tvu_socket_idx ]; /* NOT converted to net order */
467 0 : memcpy( tvu_peers[tvu_peer_cnt].pubkey, &ci->ci_crd.from, sizeof(fd_pubkey_t) );
468 :
469 0 : tvu_peer_cnt++;
470 0 : }
471 :
472 0 : {
473 0 : ushort repair_socket_idx = ci->socket_tag_idx[ FD_GOSSIP_SOCKET_TAG_SERVE_REPAIR ];
474 0 : if( repair_socket_idx == FD_CONTACT_INFO_SOCKET_TAG_NULL ) {
475 0 : ctx->metrics.zero_ipv4_contact_info[ FD_METRICS_ENUM_PEER_TYPES_V_REPAIR_IDX ] += 1UL;
476 0 : continue;
477 0 : }
478 0 : if( !fd_gossip_ip_addr_is_ip4( &ci->addrs[ ci->sockets[ repair_socket_idx].index ] )) {
479 0 : continue;
480 0 : }
481 :
482 0 : repair_peers[repair_peers_cnt].ip4_addr = ci->addrs[ ci->sockets[ repair_socket_idx].index ].inner.ip4;
483 0 : repair_peers[repair_peers_cnt].udp_port = ci->ports[ repair_socket_idx ]; /* NOT converted to net order */
484 0 : memcpy( repair_peers[repair_peers_cnt].pubkey, &ci->ci_crd.from, sizeof(fd_pubkey_t) );
485 :
486 0 : repair_peers_cnt++;
487 0 : }
488 :
489 0 : {
490 0 : ushort sender_socket_idx = ci->socket_tag_idx[ FD_GOSSIP_SOCKET_TAG_TPU_VOTE_QUIC ];
491 0 : if( sender_socket_idx == FD_CONTACT_INFO_SOCKET_TAG_NULL ) {
492 0 : ctx->metrics.zero_ipv4_contact_info[ FD_METRICS_ENUM_PEER_TYPES_V_SEND_IDX ] += 1UL;
493 0 : continue;
494 0 : }
495 0 : if( !fd_gossip_ip_addr_is_ip4( &ci->addrs[ ci->sockets[ sender_socket_idx].index ] )) {
496 0 : continue;
497 0 : }
498 :
499 0 : send_peers[send_peers_cnt].ip4_addr = ci->addrs[ ci->sockets[ sender_socket_idx ].index ].inner.ip4;
500 0 : send_peers[send_peers_cnt].udp_port = ci->ports[ sender_socket_idx ]; /* NOT converted to net order */
501 0 : memcpy( send_peers[send_peers_cnt].pubkey, &ci->ci_crd.from, sizeof(fd_pubkey_t) );
502 :
503 0 : send_peers_cnt++;
504 0 : }
505 0 : }
506 :
507 0 : #define UPDATE_PEER_CNTS( _peer_cnt_, _peer_type_ ) \
508 0 : ctx->metrics.peer_counts[ FD_METRICS_ENUM_PEER_TYPES_V_ ##_peer_type_ ##_IDX ] = _peer_cnt_;
509 :
510 0 : UPDATE_PEER_CNTS( tvu_peer_cnt, TVU );
511 0 : UPDATE_PEER_CNTS( repair_peers_cnt, REPAIR );
512 0 : UPDATE_PEER_CNTS( send_peers_cnt, SEND );
513 :
514 0 : #undef UPDATE_PEER_CNTS
515 :
516 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
517 :
518 0 : FD_LOG_INFO(( "publishing peers - tvu: %lu, repair: %lu, tpu_vote: %lu", tvu_peer_cnt, repair_peers_cnt, send_peers_cnt ));
519 0 : if( tvu_peer_cnt>0 && ctx->shred_contact_out_mcache ) {
520 0 : *shred_dest_msg = tvu_peer_cnt;
521 0 : ulong shred_contact_sz = sizeof(ulong) + (tvu_peer_cnt * sizeof(fd_shred_dest_wire_t));
522 0 : ulong shred_contact_sig = 2UL;
523 0 : fd_mcache_publish( ctx->shred_contact_out_mcache, ctx->shred_contact_out_depth, ctx->shred_contact_out_seq, shred_contact_sig, ctx->shred_contact_out_chunk,
524 0 : shred_contact_sz, 0UL, tsorig, tspub );
525 0 : ctx->shred_contact_out_seq = fd_seq_inc( ctx->shred_contact_out_seq, 1UL );
526 0 : ctx->shred_contact_out_chunk = fd_dcache_compact_next( ctx->shred_contact_out_chunk, shred_contact_sz, ctx->shred_contact_out_chunk0, ctx->shred_contact_out_wmark );
527 0 : }
528 :
529 0 : if( repair_peers_cnt>0 && ctx->repair_contact_out_mcache ) {
530 0 : ulong repair_contact_sz = (repair_peers_cnt * sizeof(fd_shred_dest_wire_t));
531 0 : ulong repair_contact_sig = 3UL;
532 0 : fd_mcache_publish( ctx->repair_contact_out_mcache, ctx->repair_contact_out_depth, ctx->repair_contact_out_seq, repair_contact_sig, ctx->repair_contact_out_chunk,
533 0 : repair_peers_cnt, 0UL, tsorig, tspub );
534 0 : ctx->repair_contact_out_seq = fd_seq_inc( ctx->repair_contact_out_seq, 1UL );
535 0 : ctx->repair_contact_out_chunk = fd_dcache_compact_next( ctx->repair_contact_out_chunk, repair_contact_sz, ctx->repair_contact_out_chunk0, ctx->repair_contact_out_wmark );
536 0 : }
537 :
538 0 : if( send_peers_cnt>0 && ctx->send_contact_out_mcache ) {
539 0 : ulong send_contact_sz = (send_peers_cnt * sizeof(fd_shred_dest_wire_t));
540 0 : ulong send_contact_sig = 4UL;
541 0 : fd_mcache_publish( ctx->send_contact_out_mcache, ctx->send_contact_out_depth, ctx->send_contact_out_seq, send_contact_sig, ctx->send_contact_out_chunk,
542 0 : send_contact_sz, 0UL, tsorig, tspub );
543 0 : ctx->send_contact_out_seq = fd_seq_inc( ctx->send_contact_out_seq, 1UL );
544 0 : ctx->send_contact_out_chunk = fd_dcache_compact_next( ctx->send_contact_out_chunk, send_contact_sz, ctx->send_contact_out_chunk0, ctx->send_contact_out_wmark );
545 0 : }
546 0 : }
547 :
548 0 : if( ctx->gossip_plugin_out_mem && FD_UNLIKELY( ( now - ctx->last_plugin_push_time )>PLUGIN_PUBLISH_TIME_NS ) ) {
549 0 : ctx->last_plugin_push_time = now;
550 0 : publish_peers_to_plugin( ctx, stem );
551 0 : }
552 :
553 0 : ushort shred_version = fd_gossip_get_shred_version( ctx->gossip );
554 0 : if( shred_version!=0U ) {
555 0 : *fd_shred_version = shred_version;
556 0 : } else {
557 0 : ctx->metrics.shred_version_zero += 1UL;
558 0 : }
559 0 : fd_gossip_continue( ctx->gossip );
560 0 : }
561 :
562 : static void
563 : privileged_init( fd_topo_t * topo,
564 0 : fd_topo_tile_t * tile ) {
565 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
566 :
567 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
568 0 : fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
569 0 : fd_memset( ctx, 0, sizeof(fd_gossip_tile_ctx_t) );
570 :
571 0 : uchar const * identity_key = fd_keyload_load( tile->gossip.identity_key_path, /* pubkey only: */ 1 );
572 0 : fd_memcpy( ctx->identity_public_key.uc, identity_key, sizeof(fd_pubkey_t) );
573 :
574 0 : FD_TEST( sizeof(ulong) == getrandom( &ctx->gossip_seed, sizeof(ulong), 0 ) );
575 0 : }
576 :
577 : static void
578 : unprivileged_init( fd_topo_t * topo,
579 0 : fd_topo_tile_t * tile ) {
580 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
581 :
582 0 : if( FD_UNLIKELY( !tile->out_cnt ) ) FD_LOG_ERR(( "gossip tile has no primary output link" ));
583 :
584 0 : if( FD_UNLIKELY( !tile->gossip.ip_addr ) ) FD_LOG_ERR(( "gossip ip address not set" ));
585 0 : if( FD_UNLIKELY( !tile->gossip.gossip_listen_port ) ) FD_LOG_ERR(( "gossip listen port not set" ));
586 :
587 : /* Scratch mem setup */
588 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
589 0 : fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
590 0 : ctx->gossip = FD_SCRATCH_ALLOC_APPEND( l, fd_gossip_align(), fd_gossip_footprint() );
591 0 : ctx->contact_info_table = fd_contact_info_table_join( fd_contact_info_table_new( FD_SCRATCH_ALLOC_APPEND( l, fd_contact_info_table_align(), fd_contact_info_table_footprint( FD_PEER_KEY_MAX ) ), FD_PEER_KEY_MAX, 0 ) );
592 :
593 0 : if( FD_UNLIKELY( tile->in_cnt > MAX_IN_LINKS ) ) FD_LOG_ERR(( "gossip tile has too many input links" ));
594 :
595 0 : uint sign_link_in_idx = UINT_MAX;
596 0 : memset( ctx->in_kind, 0, sizeof(ctx->in_kind) );
597 0 : for( uint in_idx=0U; in_idx<(tile->in_cnt); in_idx++ ) {
598 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ in_idx ] ];
599 0 : if( 0==strcmp( link->name, "net_gossip" ) ) {
600 0 : ctx->in_kind[ in_idx ] = IN_KIND_NET;
601 0 : fd_net_rx_bounds_init( &ctx->in_links[ in_idx ].net_rx, link->dcache );
602 0 : continue;
603 0 : } else if( 0==strcmp( link->name, "send_txns" ) ) {
604 0 : ctx->in_kind[ in_idx ] = IN_KIND_SEND;
605 0 : } else if( 0==strcmp( link->name, "sign_gossip" ) ) {
606 0 : ctx->in_kind[ in_idx ] = IN_KIND_SIGN;
607 0 : sign_link_in_idx = in_idx;
608 0 : } else {
609 0 : FD_LOG_ERR(( "gossip tile has unexpected input link %s", link->name ));
610 0 : }
611 :
612 0 : ctx->in_links[ in_idx ].mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
613 0 : ctx->in_links[ in_idx ].chunk0 = fd_dcache_compact_chunk0( ctx->in_links[ in_idx ].mem, link->dcache );
614 0 : ctx->in_links[ in_idx ].wmark = fd_dcache_compact_wmark( ctx->in_links[ in_idx ].mem, link->dcache, link->mtu );
615 0 : }
616 0 : if( FD_UNLIKELY( sign_link_in_idx==UINT_MAX ) ) FD_LOG_ERR(( "Missing sign_gossip link" ));
617 :
618 0 : uint sign_link_out_idx = UINT_MAX;
619 0 : for( uint out_idx=0U; out_idx<(tile->out_cnt); out_idx++ ) {
620 0 : fd_topo_link_t * link = &topo->links[ tile->out_link_id[ out_idx ] ];
621 :
622 0 : if( 0==strcmp( link->name, "gossip_net" ) ) {
623 :
624 0 : if( FD_UNLIKELY( ctx->net_out_mcache ) ) FD_LOG_ERR(( "gossip tile has multiple gossip_net out links" ));
625 0 : ctx->net_out_mcache = link->mcache;
626 0 : ctx->net_out_sync = fd_mcache_seq_laddr( ctx->net_out_mcache );
627 0 : ctx->net_out_depth = fd_mcache_depth( ctx->net_out_mcache );
628 0 : ctx->net_out_seq = fd_mcache_seq_query( ctx->net_out_sync );
629 0 : ctx->net_out_chunk0 = fd_dcache_compact_chunk0( fd_wksp_containing( link->dcache ), link->dcache );
630 0 : ctx->net_out_mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
631 0 : ctx->net_out_wmark = fd_dcache_compact_wmark( ctx->net_out_mem, link->dcache, link->mtu );
632 0 : ctx->net_out_chunk = ctx->net_out_chunk0;
633 :
634 0 : } else if( 0==strcmp( link->name, "crds_shred" ) ) {
635 :
636 :
637 0 : if( FD_UNLIKELY( ctx->shred_contact_out_mcache ) ) FD_LOG_ERR(( "gossip tile has multiple crds_shred out links" ));
638 0 : ctx->shred_contact_out_mcache = link->mcache;
639 0 : ctx->shred_contact_out_sync = fd_mcache_seq_laddr( ctx->shred_contact_out_mcache );
640 0 : ctx->shred_contact_out_depth = fd_mcache_depth( ctx->shred_contact_out_mcache );
641 0 : ctx->shred_contact_out_seq = fd_mcache_seq_query( ctx->shred_contact_out_sync );
642 0 : ctx->shred_contact_out_mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
643 0 : ctx->shred_contact_out_chunk0 = fd_dcache_compact_chunk0( ctx->shred_contact_out_mem, link->dcache );
644 0 : ctx->shred_contact_out_wmark = fd_dcache_compact_wmark ( ctx->shred_contact_out_mem, link->dcache, link->mtu );
645 0 : ctx->shred_contact_out_chunk = ctx->shred_contact_out_chunk0;
646 :
647 0 : } else if( 0==strcmp( link->name, "gossip_repai" ) ) {
648 :
649 0 : if( FD_UNLIKELY( ctx->repair_contact_out_mcache ) ) FD_LOG_ERR(( "gossip tile has multiple gossip_repair out links" ));
650 0 : ctx->repair_contact_out_mcache = link->mcache;
651 0 : ctx->repair_contact_out_sync = fd_mcache_seq_laddr( ctx->repair_contact_out_mcache );
652 0 : ctx->repair_contact_out_depth = fd_mcache_depth( ctx->repair_contact_out_mcache );
653 0 : ctx->repair_contact_out_seq = fd_mcache_seq_query( ctx->repair_contact_out_sync );
654 0 : ctx->repair_contact_out_mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
655 0 : ctx->repair_contact_out_chunk0 = fd_dcache_compact_chunk0( ctx->repair_contact_out_mem, link->dcache );
656 0 : ctx->repair_contact_out_wmark = fd_dcache_compact_wmark ( ctx->repair_contact_out_mem, link->dcache, link->mtu );
657 0 : ctx->repair_contact_out_chunk = ctx->repair_contact_out_chunk0;
658 :
659 0 : } else if( 0==strcmp( link->name, "gossip_verif" ) ) {
660 :
661 0 : if( FD_UNLIKELY( ctx->verify_out_mcache ) ) FD_LOG_ERR(( "gossip tile has multiple gossip_verif out links" ));
662 0 : ctx->verify_out_mcache = link->mcache;
663 0 : ctx->verify_out_sync = fd_mcache_seq_laddr( ctx->verify_out_mcache );
664 0 : ctx->verify_out_depth = fd_mcache_depth( ctx->verify_out_mcache );
665 0 : ctx->verify_out_seq = fd_mcache_seq_query( ctx->verify_out_sync );
666 0 : ctx->verify_out_mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
667 0 : ctx->verify_out_chunk0 = fd_dcache_compact_chunk0( ctx->verify_out_mem, link->dcache );
668 0 : ctx->verify_out_wmark = fd_dcache_compact_wmark ( ctx->verify_out_mem, link->dcache, link->mtu );
669 0 : ctx->verify_out_chunk = ctx->verify_out_chunk0;
670 :
671 0 : } else if( 0==strcmp( link->name, "gossip_sign" ) ) {
672 :
673 0 : sign_link_out_idx = out_idx;
674 :
675 0 : } else if( 0==strcmp( link->name, "gossip_send" ) ) {
676 :
677 0 : if( FD_UNLIKELY( ctx->send_contact_out_mcache ) ) FD_LOG_ERR(( "gossip tile has multiple gossip_send out links" ));
678 0 : ctx->send_contact_out_mcache = link->mcache;
679 0 : ctx->send_contact_out_sync = fd_mcache_seq_laddr( ctx->send_contact_out_mcache );
680 0 : ctx->send_contact_out_depth = fd_mcache_depth( ctx->send_contact_out_mcache );
681 0 : ctx->send_contact_out_seq = fd_mcache_seq_query( ctx->send_contact_out_sync );
682 0 : ctx->send_contact_out_mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
683 0 : ctx->send_contact_out_chunk0 = fd_dcache_compact_chunk0( ctx->send_contact_out_mem, link->dcache );
684 0 : ctx->send_contact_out_wmark = fd_dcache_compact_wmark ( ctx->send_contact_out_mem, link->dcache, link->mtu );
685 0 : ctx->send_contact_out_chunk = ctx->send_contact_out_chunk0;
686 :
687 0 : } else if( 0==strcmp( link->name, "gossip_tower" ) ) {
688 :
689 0 : ctx->tower_out_idx = fd_topo_find_tile_out_link( topo, tile, "gossip_tower", 0 );
690 0 : ctx->tower_out_mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
691 0 : ctx->tower_out_chunk0 = fd_dcache_compact_chunk0( ctx->tower_out_mem, link->dcache );
692 0 : ctx->tower_out_wmark = fd_dcache_compact_wmark ( ctx->tower_out_mem, link->dcache, link->mtu );
693 0 : ctx->tower_out_chunk = ctx->tower_out_chunk0;
694 :
695 0 : FD_TEST( ctx->tower_out_idx!=ULONG_MAX );
696 0 : FD_TEST( ctx->tower_out_mem );
697 0 : FD_TEST( fd_dcache_compact_is_safe( ctx->tower_out_mem, link->dcache, link->mtu, link->depth ) );
698 :
699 0 : } else if( 0==strcmp( link->name, "gossip_plugi" ) ) {
700 :
701 0 : if( FD_UNLIKELY( ctx->gossip_plugin_out_mem ) ) FD_LOG_ERR(( "gossip tile has multiple gossip_plugi out links" ));
702 0 : ctx->gossip_plugin_out_mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
703 0 : ctx->gossip_plugin_out_chunk0 = fd_dcache_compact_chunk0( ctx->gossip_plugin_out_mem, link->dcache );
704 0 : ctx->gossip_plugin_out_wmark = fd_dcache_compact_wmark ( ctx->gossip_plugin_out_mem, link->dcache, link->mtu );
705 0 : ctx->gossip_plugin_out_chunk = ctx->gossip_plugin_out_chunk0;
706 0 : ctx->gossip_plugin_out_idx = out_idx;
707 :
708 0 : } else {
709 0 : FD_LOG_ERR(( "gossip tile has unexpected output link %s", link->name ));
710 0 : }
711 :
712 0 : }
713 0 : if( FD_UNLIKELY( sign_link_out_idx==UINT_MAX ) ) FD_LOG_ERR(( "Missing gossip_sign link" ));
714 :
715 0 : ctx->wksp = topo->workspaces[ topo->objs[ tile->tile_obj_id ].wksp_id ].wksp;
716 :
717 0 : ctx->gossip_my_addr.addr = tile->gossip.ip_addr;
718 0 : ctx->gossip_my_addr.port = fd_ushort_bswap( tile->gossip.gossip_listen_port );
719 :
720 0 : ctx->gossip_listen_port = tile->gossip.gossip_listen_port;
721 :
722 0 : FD_TEST( ctx->gossip_listen_port!=0 );
723 :
724 0 : ctx->net_id = (ushort)0;
725 :
726 0 : fd_ip4_udp_hdr_init( ctx->hdr, FD_NET_MTU, ctx->gossip_my_addr.addr, ctx->gossip_listen_port );
727 :
728 0 : ctx->last_shred_dest_push_time = 0;
729 :
730 0 : fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id [ sign_link_in_idx ] ];
731 0 : fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ sign_link_out_idx ] ];
732 0 : if ( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
733 0 : sign_out->mcache,
734 0 : sign_out->dcache,
735 0 : sign_in->mcache,
736 0 : sign_in->dcache ) )==NULL ) {
737 0 : FD_LOG_ERR(( "Keyguard join failed" ));
738 0 : }
739 :
740 : /* Gossip set up */
741 0 : ctx->gossip = fd_gossip_join( fd_gossip_new( ctx->gossip, ctx->gossip_seed ) );
742 :
743 0 : FD_LOG_NOTICE(( "gossip my addr - addr: " FD_IP4_ADDR_FMT ":%u",
744 0 : FD_IP4_ADDR_FMT_ARGS( ctx->gossip_my_addr.addr ), fd_ushort_bswap( ctx->gossip_my_addr.port ) ));
745 0 : ctx->gossip_config.my_addr = ctx->gossip_my_addr;
746 0 : ctx->gossip_config.my_version = (fd_gossip_version_v3_t){
747 0 : .major = 42U,
748 0 : .minor = 42U,
749 0 : .patch = 42U,
750 0 : .commit = 0U,
751 0 : .feature_set = 0U,
752 0 : .client = 2U
753 0 : };
754 0 : ctx->gossip_config.node_outset = fd_log_wallclock() / 1000000; /* in ms */
755 0 : ctx->gossip_config.public_key = &ctx->identity_public_key;
756 0 : ctx->gossip_config.deliver_fun = gossip_deliver_fun;
757 0 : ctx->gossip_config.deliver_arg = ctx;
758 0 : ctx->gossip_config.send_fun = gossip_send_packet;
759 0 : ctx->gossip_config.send_arg = ctx;
760 0 : ctx->gossip_config.sign_fun = gossip_signer;
761 0 : ctx->gossip_config.sign_arg = ctx;
762 0 : ctx->gossip_config.shred_version = (ushort)tile->gossip.expected_shred_version;
763 :
764 0 : if( fd_gossip_set_config( ctx->gossip, &ctx->gossip_config ) ) {
765 0 : FD_LOG_ERR( ( "error setting gossip config" ) );
766 0 : }
767 :
768 0 : fd_gossip_set_entrypoints( ctx->gossip, tile->gossip.entrypoints, tile->gossip.entrypoints_cnt );
769 :
770 0 : fd_gossip_update_addr( ctx->gossip, &ctx->gossip_config.my_addr );
771 :
772 0 : ctx->tvu_my_addr.addr = tile->gossip.ip_addr;
773 0 : ctx->tvu_my_addr.port = fd_ushort_bswap( tile->gossip.tvu_port );
774 0 : ctx->tpu_my_addr.addr = tile->gossip.ip_addr;
775 0 : ctx->tpu_my_addr.port = fd_ushort_bswap( tile->gossip.tpu_port );
776 0 : ctx->tpu_quic_my_addr.addr = tile->gossip.ip_addr;
777 0 : ctx->tpu_quic_my_addr.port = fd_ushort_bswap( tile->gossip.tpu_quic_port );
778 0 : ctx->tpu_vote_my_addr.addr = tile->gossip.ip_addr;
779 0 : ctx->tpu_vote_my_addr.port = fd_ushort_bswap( tile->gossip.tpu_vote_port );
780 0 : ctx->repair_serve_addr.addr = tile->gossip.ip_addr;
781 0 : ctx->repair_serve_addr.port = fd_ushort_bswap( tile->gossip.repair_serve_port );
782 :
783 0 : fd_gossip_update_tvu_addr( ctx->gossip, &ctx->tvu_my_addr );
784 0 : fd_gossip_update_tpu_addr( ctx->gossip, &ctx->tpu_my_addr, &ctx->tpu_quic_my_addr );
785 0 : fd_gossip_update_tpu_vote_addr( ctx->gossip, &ctx->tpu_vote_my_addr );
786 0 : fd_gossip_update_repair_addr( ctx->gossip, &ctx->repair_serve_addr );
787 0 : fd_gossip_settime( ctx->gossip, fd_log_wallclock() );
788 0 : fd_gossip_start( ctx->gossip );
789 :
790 0 : FD_LOG_NOTICE(( "gossip listening on port %u", tile->gossip.gossip_listen_port ));
791 :
792 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
793 0 : if( FD_UNLIKELY( scratch_top>( (ulong)scratch + scratch_footprint( tile ) ) ) )
794 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
795 :
796 0 : ulong poh_shred_obj_id = fd_pod_query_ulong( topo->props, "poh_shred", ULONG_MAX );
797 0 : FD_TEST( poh_shred_obj_id!=ULONG_MAX );
798 :
799 0 : fd_shred_version = fd_fseq_join( fd_topo_obj_laddr( topo, poh_shred_obj_id ) );
800 0 : FD_TEST( fd_shred_version );
801 :
802 : /* Initialize metrics to zero */
803 0 : memset( &ctx->metrics, 0, FD_GOSSIP_TILE_METRICS_FOOTPRINT );
804 0 : }
805 :
806 : static ulong
807 : populate_allowed_seccomp( fd_topo_t const * topo,
808 : fd_topo_tile_t const * tile,
809 : ulong out_cnt,
810 0 : struct sock_filter * out ) {
811 0 : (void)topo;
812 0 : (void)tile;
813 :
814 0 : populate_sock_filter_policy_fd_gossip_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
815 0 : return sock_filter_policy_fd_gossip_tile_instr_cnt;
816 0 : }
817 :
818 : static ulong
819 : populate_allowed_fds( fd_topo_t const * topo,
820 : fd_topo_tile_t const * tile,
821 : ulong out_fds_cnt,
822 0 : int * out_fds ) {
823 0 : (void)topo;
824 0 : (void)tile;
825 :
826 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
827 :
828 0 : ulong out_cnt = 0UL;
829 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
830 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
831 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
832 0 : return out_cnt;
833 0 : }
834 :
835 : static inline void
836 0 : fd_gossip_update_gossip_metrics( fd_gossip_metrics_t * metrics ) {
837 0 : FD_MCNT_SET( GOSSIP, RECEIVED_PACKETS, metrics->recv_pkt_cnt );
838 0 : FD_MCNT_SET( GOSSIP, CORRUPTED_MESSAGES, metrics->recv_pkt_corrupted_msg );
839 :
840 0 : FD_MCNT_ENUM_COPY( GOSSIP, RECEIVED_GOSSIP_MESSAGES, metrics->recv_message );
841 0 : FD_MCNT_SET( GOSSIP, RECEIVED_UNKNOWN_MESSAGE, metrics->recv_unknown_message );
842 :
843 0 : FD_MCNT_ENUM_COPY( GOSSIP, RECEIVED_CRDS_PUSH, metrics->recv_crds[ FD_GOSSIP_CRDS_ROUTE_PUSH ] );
844 0 : FD_MCNT_ENUM_COPY( GOSSIP, RECEIVED_CRDS_PULL, metrics->recv_crds[ FD_GOSSIP_CRDS_ROUTE_PULL_RESP ] );
845 0 : FD_MCNT_ENUM_COPY( GOSSIP, RECEIVED_CRDS_DUPLICATE_MESSAGE_PUSH, metrics->recv_crds_duplicate_message[ FD_GOSSIP_CRDS_ROUTE_PUSH ] );
846 0 : FD_MCNT_ENUM_COPY( GOSSIP, RECEIVED_CRDS_DUPLICATE_MESSAGE_PULL, metrics->recv_crds_duplicate_message[ FD_GOSSIP_CRDS_ROUTE_PULL_RESP ] );
847 0 : FD_MCNT_ENUM_COPY( GOSSIP, RECEIVED_CRDS_DROP, metrics->recv_crds_drop_reason );
848 :
849 0 : FD_MCNT_ENUM_COPY( GOSSIP, PUSH_CRDS, metrics->push_crds );
850 0 : FD_MCNT_ENUM_COPY( GOSSIP, PUSH_CRDS_DUPLICATE_MESSAGE, metrics->push_crds_duplicate );
851 0 : FD_MCNT_ENUM_COPY( GOSSIP, PUSH_CRDS_DROP, metrics->push_crds_drop_reason );
852 0 : FD_MGAUGE_SET( GOSSIP, PUSH_CRDS_QUEUE_COUNT, metrics->push_crds_queue_cnt );
853 :
854 0 : FD_MGAUGE_SET( GOSSIP, VALUE_META_SIZE, metrics->value_meta_cnt );
855 0 : FD_MGAUGE_SET( GOSSIP, VALUE_VEC_SIZE, metrics->value_vec_cnt );
856 :
857 0 : FD_MGAUGE_SET( GOSSIP, ACTIVE_PUSH_DESTINATIONS, metrics->active_push_destinations );
858 0 : FD_MCNT_SET( GOSSIP, REFRESH_PUSH_STATES_FAIL_COUNT, metrics->refresh_push_states_failcnt );
859 :
860 0 : FD_MCNT_ENUM_COPY( GOSSIP, PULL_REQ_FAIL, metrics->handle_pull_req_fails );
861 :
862 0 : FD_MCNT_ENUM_COPY( GOSSIP, PULL_REQ_BLOOM_FILTER, metrics->handle_pull_req_bloom_filter_result);
863 0 : FD_MGAUGE_SET( GOSSIP, PULL_REQ_RESP_PACKETS, metrics->handle_pull_req_npackets );
864 :
865 0 : FD_MCNT_ENUM_COPY( GOSSIP, PRUNE_FAIL_COUNT, metrics->handle_prune_fails );
866 :
867 0 : FD_MCNT_SET( GOSSIP, MAKE_PRUNE_STALE_ENTRY, metrics->make_prune_stale_entry );
868 0 : FD_MCNT_SET( GOSSIP, MAKE_PRUNE_HIGH_DUPLICATES, metrics->make_prune_high_duplicates );
869 0 : FD_MGAUGE_SET( GOSSIP, MAKE_PRUNE_REQUESTED_ORIGINS, metrics->make_prune_requested_origins );
870 0 : FD_MCNT_SET( GOSSIP, MAKE_PRUNE_SIGN_DATA_ENCODE_FAILED, metrics->make_prune_sign_data_encode_failed );
871 :
872 0 : FD_MCNT_ENUM_COPY( GOSSIP, SENT_GOSSIP_MESSAGES, metrics->send_message );
873 :
874 0 : FD_MCNT_SET( GOSSIP, SENT_PACKETS, metrics->send_packet_cnt );
875 :
876 0 : FD_MCNT_ENUM_COPY( GOSSIP, SEND_PING_EVENT, metrics->send_ping_events );
877 0 : FD_MCNT_SET( GOSSIP, RECV_PING_INVALID_SIGNATURE, metrics->recv_ping_invalid_signature );
878 :
879 0 : FD_MCNT_ENUM_COPY( GOSSIP, RECV_PONG_EVENT, metrics->recv_pong_events );
880 :
881 0 : FD_MGAUGE_ENUM_COPY( GOSSIP, GOSSIP_PEER_COUNTS, metrics->gossip_peer_cnt );
882 0 : }
883 :
884 : static inline void
885 0 : metrics_write( fd_gossip_tile_ctx_t * ctx ) {
886 : /* Tile-specific metrics */
887 0 : FD_MGAUGE_SET( GOSSIP, LAST_CRDS_PUSH_CONTACT_INFO_PUBLISH_TIMESTAMP_NANOS, ctx->metrics.last_crds_push_contact_info_publish_ts );
888 0 : FD_MCNT_SET( GOSSIP, MISMATCHED_CONTACT_INFO_SHRED_VERSION, ctx->metrics.mismatched_contact_info_shred_version );
889 0 : FD_MCNT_ENUM_COPY( GOSSIP, IPV6_CONTACT_INFO, ctx->metrics.ipv6_contact_info );
890 0 : FD_MCNT_ENUM_COPY( GOSSIP, ZERO_IPV4_CONTACT_INFO, ctx->metrics.zero_ipv4_contact_info );
891 0 : FD_MGAUGE_ENUM_COPY( GOSSIP, PEER_COUNTS, ctx->metrics.peer_counts );
892 0 : FD_MCNT_SET( GOSSIP, SHRED_VERSION_ZERO, ctx->metrics.shred_version_zero );
893 :
894 : /* Gossip-protocol-specific metrics */
895 0 : fd_gossip_update_gossip_metrics( fd_gossip_get_metrics( ctx->gossip ) );
896 0 : }
897 :
898 0 : #define STEM_BURST (1UL)
899 :
900 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_gossip_tile_ctx_t
901 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gossip_tile_ctx_t)
902 :
903 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
904 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
905 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
906 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
907 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
908 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
909 :
910 : #include "../../disco/stem/fd_stem.c"
911 :
912 : fd_topo_run_tile_t fd_tile_gossip = {
913 : .name = "gossip",
914 : .loose_footprint = loose_footprint,
915 : .populate_allowed_seccomp = populate_allowed_seccomp,
916 : .populate_allowed_fds = populate_allowed_fds,
917 : .scratch_align = scratch_align,
918 : .scratch_footprint = scratch_footprint,
919 : .privileged_init = privileged_init,
920 : .unprivileged_init = unprivileged_init,
921 : .run = stem_run,
922 : };
|