Line data Source code
1 : #include "fd_gossip_tile.h"
2 : #include "../../disco/metrics/fd_metrics.h"
3 : #include "generated/fd_gossip_tile_seccomp.h"
4 :
5 : #include "../../flamenco/gossip/crds/fd_crds.h"
6 : #include "../../flamenco/gossip/fd_gossip_out.h"
7 : #include "../../disco/keyguard/fd_keyload.h"
8 : #include "../../disco/shred/fd_stake_ci.h"
9 : #include "../../disco/fd_txn_m.h"
10 :
11 0 : #define IN_KIND_GOSSVF (0)
12 0 : #define IN_KIND_SHRED_VERSION (1)
13 0 : #define IN_KIND_SIGN (2)
14 0 : #define IN_KIND_SEND (3)
15 0 : #define IN_KIND_STAKE (4)
16 :
17 : /* Symbols exported by version.c */
18 : extern ulong const firedancer_major_version;
19 : extern ulong const firedancer_minor_version;
20 : extern ulong const firedancer_patch_version;
21 : extern uint const firedancer_commit_ref;
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, alignof(fd_stake_weight_t), MAX_STAKED_LEADERS*sizeof(fd_stake_weight_t) );
34 0 : return FD_LAYOUT_FINI( l, scratch_align() );
35 0 : }
36 :
37 : static void
38 : gossip_send_fn( void * ctx,
39 : fd_stem_context_t * stem,
40 : uchar const * payload,
41 : ulong payload_sz,
42 : fd_ip4_port_t const * peer_address,
43 0 : ulong tsorig ) {
44 0 : fd_gossip_tile_ctx_t * gossip_ctx = (fd_gossip_tile_ctx_t *)ctx;
45 :
46 0 : uchar * packet = (uchar *)fd_chunk_to_laddr( gossip_ctx->net_out->mem, gossip_ctx->net_out->chunk );
47 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
48 0 : *hdr = *gossip_ctx->net_out_hdr;
49 :
50 0 : fd_ip4_hdr_t * ip4 = hdr->ip4;
51 0 : fd_udp_hdr_t * udp = hdr->udp;
52 :
53 0 : ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t) + sizeof(fd_ip4_hdr_t)) );
54 0 : udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
55 0 : ip4->daddr = peer_address->addr;
56 0 : udp->net_dport = peer_address->port;
57 0 : ip4->net_id = fd_ushort_bswap( gossip_ctx->net_id++ );
58 0 : ip4->check = fd_ip4_hdr_check_fast( ip4 );
59 0 : udp->check = 0;
60 :
61 : /* TODO: Construct payload in place to avoid memcpy here. */
62 0 : fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), payload, payload_sz );
63 :
64 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
65 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) );
66 0 : ulong packet_sz = payload_sz + sizeof(fd_ip4_udp_hdrs_t);
67 :
68 0 : fd_stem_publish( stem, gossip_ctx->net_out->idx, sig, gossip_ctx->net_out->chunk, packet_sz, 0UL, tspub, tsorig );
69 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 );
70 0 : }
71 :
72 : static void
73 : gossip_sign_fn( void * ctx,
74 : uchar const * data,
75 : ulong data_sz,
76 : int sign_type,
77 0 : uchar * out_signature ) {
78 0 : fd_gossip_tile_ctx_t * gossip_ctx = (fd_gossip_tile_ctx_t *)ctx;
79 0 : fd_keyguard_client_sign( gossip_ctx->keyguard_client, out_signature, data, data_sz, sign_type );
80 0 : }
81 :
82 : static void
83 : gossip_ping_tracker_change_fn( void * _ctx,
84 : uchar const * peer_pubkey,
85 : fd_ip4_port_t peer_address,
86 : long now,
87 0 : int change_type ) {
88 0 : (void)now;
89 :
90 0 : fd_gossip_tile_ctx_t * ctx = (fd_gossip_tile_ctx_t *)_ctx;
91 :
92 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 );
93 0 : fd_memcpy( ping_update->pubkey.uc, peer_pubkey, 32UL );
94 0 : ping_update->gossip_addr.l = peer_address.l;
95 0 : ping_update->remove = change_type!=FD_PING_TRACKER_CHANGE_TYPE_ACTIVE;
96 :
97 0 : fd_stem_publish( ctx->stem, ctx->gossvf_out->idx, 0UL, ctx->gossvf_out->chunk, sizeof(fd_gossip_ping_update_t), 0UL, 0UL, 0UL );
98 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 );
99 0 : }
100 :
101 : static inline void
102 0 : during_housekeeping( fd_gossip_tile_ctx_t * ctx ) {
103 0 : ctx->last_wallclock = fd_log_wallclock();
104 0 : ctx->last_tickcount = fd_tickcount();
105 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
106 : /* TODO: Need some kind of state machine here, to ensure we switch
107 : in sync with the signing tile. Currently, we might send out a
108 : badly signed message before the signing tile has switched. */
109 0 : fd_memcpy( ctx->my_contact_info->pubkey.uc, ctx->keyswitch->bytes, 32UL );
110 0 : fd_gossip_set_my_contact_info( ctx->gossip, ctx->my_contact_info, ctx->last_wallclock );
111 :
112 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
113 0 : }
114 0 : }
115 :
116 : static inline void
117 0 : metrics_write( fd_gossip_tile_ctx_t * ctx ) {
118 0 : fd_ping_tracker_metrics_t const * ping_tracker_metrics = fd_gossip_ping_tracker_metrics( ctx->gossip );
119 :
120 0 : FD_MGAUGE_SET( GOSSIP, PING_TRACKER_COUNT_UNPINGED, ping_tracker_metrics->unpinged_cnt );
121 0 : FD_MGAUGE_SET( GOSSIP, PING_TRACKER_COUNT_INVALID, ping_tracker_metrics->invalid_cnt );
122 0 : FD_MGAUGE_SET( GOSSIP, PING_TRACKER_COUNT_VALID, ping_tracker_metrics->valid_cnt );
123 0 : FD_MGAUGE_SET( GOSSIP, PING_TRACKER_COUNT_VALID_REFRESHING, ping_tracker_metrics->refreshing_cnt );
124 :
125 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_STAKED, ping_tracker_metrics->pong_result[ 0UL ] );
126 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_ENTRYPOINT, ping_tracker_metrics->pong_result[ 1UL ] );
127 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_UNTRACKED, ping_tracker_metrics->pong_result[ 2UL ] );
128 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_ADDRESS, ping_tracker_metrics->pong_result[ 3UL ] );
129 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_TOKEN, ping_tracker_metrics->pong_result[ 4UL ] );
130 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_SUCCESS, ping_tracker_metrics->pong_result[ 5UL ] );
131 :
132 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_EVICTED_COUNT, ping_tracker_metrics->peers_evicted );
133 0 : FD_MCNT_SET( GOSSIP, PING_TRACKED_COUNT, ping_tracker_metrics->tracked_cnt );
134 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_STAKE_CHANGED_COUNT, ping_tracker_metrics->stake_changed_cnt );
135 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_ADDRESS_CHANGED_COUNT, ping_tracker_metrics->address_changed_cnt );
136 :
137 0 : fd_crds_metrics_t const * crds_metrics = fd_gossip_crds_metrics( ctx->gossip );
138 :
139 0 : FD_MGAUGE_ENUM_COPY( GOSSIP, CRDS_COUNT, crds_metrics->count );
140 0 : FD_MCNT_SET( GOSSIP, CRDS_EXPIRED_COUNT, crds_metrics->expired_cnt );
141 0 : FD_MCNT_SET( GOSSIP, CRDS_EVICTED_COUNT, crds_metrics->evicted_cnt );
142 :
143 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_STAKED_COUNT, crds_metrics->peer_staked_cnt );
144 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_UNSTAKED_COUNT, crds_metrics->peer_unstaked_cnt );
145 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_TOTAL_STAKE, crds_metrics->peer_visible_stake );
146 0 : FD_MCNT_SET( GOSSIP, CRDS_PEER_EVICTED_COUNT, crds_metrics->peer_evicted_cnt );
147 :
148 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PURGED_COUNT, crds_metrics->purged_cnt );
149 0 : FD_MCNT_SET( GOSSIP, CRDS_PURGED_EVICTED_COUNT, crds_metrics->purged_evicted_cnt );
150 0 : FD_MCNT_SET( GOSSIP, CRDS_PURGED_EXPIRED_COUNT, crds_metrics->purged_expired_cnt );
151 :
152 0 : fd_gossip_metrics_t const * metrics = fd_gossip_metrics( ctx->gossip );
153 :
154 0 : FD_MCNT_ENUM_COPY( GOSSIP, MESSAGE_TX_COUNT, metrics->message_tx );
155 0 : FD_MCNT_ENUM_COPY( GOSSIP, MESSAGE_TX_BYTES, metrics->message_tx_bytes );
156 :
157 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_TX_PUSH_COUNT, metrics->crds_tx_push );
158 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_TX_PUSH_BYTES, metrics->crds_tx_push_bytes );
159 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_TX_PULL_RESPONSE_COUNT, metrics->crds_tx_pull_response );
160 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_TX_PULL_RESPONSE_BYTES, metrics->crds_tx_pull_response_bytes );
161 :
162 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_RX_COUNT, metrics->crds_rx_count );
163 :
164 0 : FD_MCNT_SET( GOSSIP, CONTACT_INFO_UNRECOGNIZED_SOCKET_TAGS, metrics->ci_rx_unrecognized_socket_tag_cnt );
165 0 : FD_MCNT_SET( GOSSIP, CONTACT_INFO_IPV6, metrics->ci_rx_ipv6_address_cnt );
166 0 : }
167 :
168 : void
169 : after_credit( fd_gossip_tile_ctx_t * ctx,
170 : fd_stem_context_t * stem,
171 : int * opt_poll_in FD_PARAM_UNUSED,
172 0 : int * charge_busy FD_PARAM_UNUSED ) {
173 0 : ctx->stem = stem;
174 :
175 0 : if( FD_UNLIKELY( !ctx->my_contact_info->shred_version ) ) return;
176 :
177 0 : long now = ctx->last_wallclock + (long)((double)(fd_tickcount()-ctx->last_tickcount)/ctx->ticks_per_ns);
178 0 : fd_gossip_advance( ctx->gossip, now, stem );
179 0 : }
180 :
181 : static void
182 : handle_shred_version( fd_gossip_tile_ctx_t * ctx,
183 0 : ulong sig ) {
184 0 : long now = ctx->last_wallclock + (long)((double)(fd_tickcount()-ctx->last_tickcount)/ctx->ticks_per_ns);
185 0 : ctx->my_contact_info->shred_version = (ushort)sig;
186 0 : fd_gossip_set_my_contact_info( ctx->gossip, ctx->my_contact_info, now );
187 0 : }
188 :
189 : static void
190 : handle_local_vote( fd_gossip_tile_ctx_t * ctx,
191 : fd_txn_m_t const * txn_m,
192 0 : fd_stem_context_t * stem ) {
193 0 : long now = ctx->last_wallclock + (long)((double)(fd_tickcount()-ctx->last_tickcount)/ctx->ticks_per_ns);
194 0 : fd_gossip_push_vote( ctx->gossip, fd_txn_m_payload_const( txn_m ), txn_m->payload_sz, stem, now );
195 0 : }
196 :
197 : static void
198 : handle_stakes( fd_gossip_tile_ctx_t * ctx,
199 0 : fd_stake_weight_msg_t const * msg ) {
200 0 : ulong stakes_cnt = compute_id_weights_from_vote_weights( ctx->stake_weights_converted, msg->weights, msg->staked_cnt );
201 0 : fd_gossip_stakes_update( ctx->gossip, ctx->stake_weights_converted, stakes_cnt );
202 0 : }
203 :
204 : static void
205 : handle_packet( fd_gossip_tile_ctx_t * ctx,
206 : ulong sig,
207 : uchar const * payload,
208 : ulong payload_sz,
209 0 : fd_stem_context_t * stem ) {
210 0 : long now = ctx->last_wallclock + (long)((double)(fd_tickcount()-ctx->last_tickcount)/ctx->ticks_per_ns);
211 :
212 0 : fd_ip4_port_t peer = (fd_ip4_port_t){
213 0 : .addr = fd_gossvf_sig_addr( sig ),
214 0 : .port = fd_gossvf_sig_port( sig )
215 0 : };
216 :
217 0 : switch( fd_gossvf_sig_kind( sig ) ) {
218 0 : case 0: {
219 0 : fd_gossip_rx( ctx->gossip, peer, payload, payload_sz, now, stem );
220 0 : fd_gossip_advance( ctx->gossip, now, stem );
221 0 : break;
222 0 : }
223 0 : case 1: {
224 0 : fd_gossip_pingreq_t * pingreq = (fd_gossip_pingreq_t *)payload;
225 0 : fd_gossip_ping_tracker_track( ctx->gossip, pingreq->pubkey.uc, peer, now );
226 0 : }
227 0 : }
228 0 : }
229 :
230 : static inline int
231 : returnable_frag( fd_gossip_tile_ctx_t * ctx,
232 : ulong in_idx,
233 : ulong seq,
234 : ulong sig,
235 : ulong chunk,
236 : ulong sz,
237 : ulong ctl,
238 : ulong tsorig,
239 : ulong tspub,
240 0 : fd_stem_context_t * stem ) {
241 0 : (void)seq;
242 0 : (void)ctl;
243 0 : (void)tsorig;
244 0 : (void)tspub;
245 :
246 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
247 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
248 :
249 0 : if( FD_UNLIKELY( !ctx->my_contact_info->shred_version && ctx->in[ in_idx ].kind!=IN_KIND_SHRED_VERSION ) ) return 1;
250 :
251 0 : switch( ctx->in[ in_idx ].kind ) {
252 0 : case IN_KIND_SHRED_VERSION: handle_shred_version( ctx, sig ); break;
253 0 : case IN_KIND_SEND: handle_local_vote( ctx, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ), stem ); break;
254 0 : case IN_KIND_STAKE: handle_stakes( ctx, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ) ); break;
255 0 : case IN_KIND_GOSSVF: handle_packet( ctx, sig, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ), sz, stem ); break;
256 0 : }
257 :
258 0 : return 0;
259 0 : }
260 :
261 : static void
262 : privileged_init( fd_topo_t * topo,
263 0 : fd_topo_tile_t * tile ) {
264 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
265 :
266 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
267 0 : fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
268 :
269 0 : if( FD_UNLIKELY( !strcmp( tile->gossip.identity_key_path, "" ) ) )
270 0 : FD_LOG_ERR(( "identity_key_path not set" ));
271 :
272 0 : fd_memcpy( ctx->my_contact_info->pubkey.uc, fd_type_pun_const( fd_keyload_load( tile->gossip.identity_key_path, /* pubkey only: */ 1 ) ), 32UL );
273 0 : FD_TEST( fd_rng_secure( &ctx->rng_seed, 4UL ) );
274 0 : FD_TEST( fd_rng_secure( &ctx->rng_idx, 8UL ) );
275 0 : }
276 :
277 : static inline fd_gossip_out_ctx_t
278 : out1( fd_topo_t const * topo,
279 : fd_topo_tile_t const * tile,
280 0 : char const * name ) {
281 0 : ulong idx = ULONG_MAX;
282 :
283 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
284 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
285 0 : if( !strcmp( link->name, name ) ) {
286 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 ));
287 0 : idx = i;
288 0 : }
289 0 : }
290 :
291 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 ));
292 :
293 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
294 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
295 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
296 :
297 0 : return (fd_gossip_out_ctx_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
298 0 : }
299 :
300 : static void
301 : unprivileged_init( fd_topo_t * topo,
302 0 : fd_topo_tile_t * tile ) {
303 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
304 :
305 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
306 0 : fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
307 0 : void * _gossip = FD_SCRATCH_ALLOC_APPEND( l, fd_gossip_align(), fd_gossip_footprint( tile->gossip.max_entries, tile->gossip.entrypoints_cnt ) );
308 0 : void * _stake_weights = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_stake_weight_t), MAX_STAKED_LEADERS*sizeof(fd_stake_weight_t) );
309 :
310 0 : ctx->stake_weights_converted = (fd_stake_weight_t *)_stake_weights;
311 :
312 0 : FD_TEST( fd_rng_join( fd_rng_new( ctx->rng, ctx->rng_seed, ctx->rng_idx ) ) );
313 :
314 0 : FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
315 0 : ulong sign_in_tile_idx = ULONG_MAX;
316 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
317 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
318 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
319 :
320 0 : ctx->in[ i ].mem = link_wksp->wksp;
321 0 : if( FD_LIKELY( link->mtu ) ) {
322 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
323 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
324 0 : } else {
325 0 : ctx->in[ i ].chunk0 = 0UL;
326 0 : ctx->in[ i ].wmark = 0UL;
327 0 : }
328 0 : ctx->in[ i ].mtu = link->mtu;
329 :
330 0 : if( FD_UNLIKELY( !strcmp( link->name, "ipecho_out" ) ) ) {
331 0 : ctx->in[ i ].kind = IN_KIND_SHRED_VERSION;
332 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "gossvf_gossi" ) ) ) {
333 0 : ctx->in[ i ].kind = IN_KIND_GOSSVF;
334 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "sign_gossip" ) ) ) {
335 0 : ctx->in[ i ].kind = IN_KIND_SIGN;
336 0 : sign_in_tile_idx = i;
337 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "send_txns" ) ) ) {
338 0 : ctx->in[ i ].kind = IN_KIND_SEND;
339 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "replay_stake" ) ) ) {
340 0 : ctx->in[ i ].kind = IN_KIND_STAKE;
341 0 : } else {
342 0 : FD_LOG_ERR(( "unexpected input link name %s", link->name ));
343 0 : }
344 0 : }
345 :
346 0 : if( FD_UNLIKELY( sign_in_tile_idx==ULONG_MAX ) )
347 0 : FD_LOG_ERR(( "tile %s:%lu had no input link named sign_gossip", tile->name, tile->kind_id ));
348 :
349 0 : *ctx->net_out = out1( topo, tile, "gossip_net" );
350 0 : *ctx->sign_out = out1( topo, tile, "gossip_sign" );
351 0 : *ctx->gossip_out = out1( topo, tile, "gossip_out" );
352 0 : *ctx->gossvf_out = out1( topo, tile, "gossip_gossv" );
353 :
354 0 : fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id [ sign_in_tile_idx ] ];
355 0 : fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ ctx->sign_out->idx ] ];
356 :
357 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
358 0 : FD_TEST( ctx->keyswitch );
359 :
360 0 : if( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
361 0 : sign_out->mcache,
362 0 : sign_out->dcache,
363 0 : sign_in->mcache,
364 0 : sign_in->dcache,
365 0 : sign_out->mtu ) )==NULL ) {
366 0 : FD_LOG_ERR(( "failed to join keyguard client" ));
367 0 : }
368 :
369 0 : ctx->ticks_per_ns = fd_tempo_tick_per_ns( NULL );
370 0 : ctx->last_wallclock = fd_log_wallclock();
371 0 : ctx->last_tickcount = fd_tickcount();
372 :
373 0 : ctx->my_contact_info->shred_version = tile->gossip.shred_version;
374 :
375 0 : ctx->my_contact_info->wallclock_nanos = ctx->last_wallclock;
376 0 : ctx->my_contact_info->instance_creation_wallclock_nanos = tile->gossip.boot_timestamp_nanos;
377 :
378 0 : ctx->my_contact_info->version.client = FD_CONTACT_INFO_VERSION_CLIENT_FIREDANCER;
379 0 : ctx->my_contact_info->version.major = (ushort)firedancer_major_version;
380 0 : ctx->my_contact_info->version.minor = (ushort)firedancer_minor_version;
381 0 : ctx->my_contact_info->version.patch = (ushort)firedancer_patch_version;
382 0 : ctx->my_contact_info->version.commit = firedancer_commit_ref;
383 0 : ctx->my_contact_info->version.feature_set = UINT_MAX; /* TODO ... */
384 :
385 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_GOSSIP ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.gossip ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.gossip ) };
386 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TVU ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tvu ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tvu ) };
387 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
388 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_FORWARDS ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
389 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_QUIC ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu_quic ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
390 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_VOTE_QUIC ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu_quic ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
391 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_FORWARDS_QUIC ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu_quic ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
392 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_VOTE ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
393 :
394 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TVU_QUIC ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
395 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_SERVE_REPAIR ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
396 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_SERVE_REPAIR_QUIC ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
397 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_RPC ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
398 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_RPC_PUBSUB ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
399 :
400 0 : ctx->gossip = fd_gossip_join( fd_gossip_new( _gossip,
401 0 : ctx->rng,
402 0 : tile->gossip.max_entries,
403 0 : tile->gossip.entrypoints_cnt,
404 0 : tile->gossip.entrypoints,
405 0 : ctx->my_contact_info,
406 0 : ctx->last_wallclock,
407 0 : gossip_send_fn,
408 0 : ctx,
409 0 : gossip_sign_fn,
410 0 : ctx,
411 0 : gossip_ping_tracker_change_fn,
412 0 : ctx,
413 0 : ctx->gossip_out,
414 0 : ctx->net_out ) );
415 0 : FD_TEST( ctx->gossip );
416 :
417 0 : FD_MGAUGE_SET( GOSSIP, CRDS_CAPACITY, tile->gossip.max_entries );
418 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_CAPACITY, FD_CONTACT_INFO_TABLE_SIZE );
419 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PURGED_CAPACITY, 4UL*tile->gossip.max_entries );
420 :
421 0 : fd_ip4_udp_hdr_init( ctx->net_out_hdr, FD_GOSSIP_MTU, tile->gossip.ip_addr, tile->gossip.ports.gossip );
422 :
423 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
424 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
425 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
426 0 : }
427 :
428 : static ulong
429 : populate_allowed_seccomp( fd_topo_t const * topo,
430 : fd_topo_tile_t const * tile,
431 : ulong out_cnt,
432 0 : struct sock_filter * out ) {
433 0 : (void)topo;
434 0 : (void)tile;
435 :
436 0 : populate_sock_filter_policy_fd_gossip_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
437 0 : return sock_filter_policy_fd_gossip_tile_instr_cnt;
438 0 : }
439 :
440 : static ulong
441 : populate_allowed_fds( fd_topo_t const * topo,
442 : fd_topo_tile_t const * tile,
443 : ulong out_fds_cnt,
444 0 : int * out_fds ) {
445 0 : (void)topo;
446 0 : (void)tile;
447 :
448 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
449 :
450 0 : ulong out_cnt = 0UL;
451 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
452 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
453 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
454 0 : return out_cnt;
455 0 : }
456 :
457 : /* Account for worst case in fd_gossip_rx and fd_gossip_advance, which
458 : are both called in returnable_frag.
459 :
460 : fd_gossip_rx: Gossip updates are sent out via the gossip_out link for
461 : specific CRDS messages received, and when a contact info is dropped.
462 : Worst case is when:
463 : - all incoming CRDS messages are broadcasted as updates, and
464 : - CRDS table is full, and all entries dropped to make way for new
465 : ones are contact infos
466 :
467 : Ping tracker track also publishes a status change on the
468 : gossip_gossv link if an incoming pong changes an inactive or
469 : unpinged peer to active. There is only one pong processed per
470 : after_frag loop.
471 :
472 : This leaves us with a worst case of FD_GOSSIP_MSG_MAX_CRDS*2 on
473 : gossip_out, and 1 on gossip_gossv.
474 :
475 : fd_gossip_advance: two links we need to look at: the gossip_gossv
476 : link that publishes fd_ping_tracker changes and the gossip_out link
477 : for when contact infos are dropped during expiry.
478 :
479 : fd_ping_tracker publishes a ping status change message when a peer
480 : becomes inactive. In the worst case, all peers can become inactive
481 : in one loop. So there would be FD_PING_TRACKER_MAX ping status
482 : changes.
483 :
484 : During the expire loop, all contact infos might be dropped in one
485 : iteration, which would result in CRDS_MAX_CONTACT_INFO gossip
486 : updates
487 :
488 : We find the worst case burst by taking the maximum burst of the two
489 : links in fd_gossip_rx and fd_gossip_advance. That would be:
490 : gossip_out link gossip_gossv link
491 : max( FD_GOSSIP_MSG_CRDS_MAX*2+CRDS_MAX_CONTACT_INFO, 1+FD_PING_TRACKER_MAX)
492 :
493 : */
494 :
495 : FD_STATIC_ASSERT( CRDS_MAX_CONTACT_INFO+FD_GOSSIP_MSG_MAX_CRDS*2UL<=FD_PING_TRACKER_MAX+1UL,
496 : "Gossip stem burst needs recalculating" );
497 0 : #define STEM_BURST ( FD_PING_TRACKER_MAX+1UL )
498 :
499 0 : #define STEM_LAZY (128L*3000L)
500 :
501 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_gossip_tile_ctx_t
502 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gossip_tile_ctx_t)
503 :
504 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
505 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
506 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
507 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
508 :
509 : #include "../../disco/stem/fd_stem.c"
510 :
511 : fd_topo_run_tile_t fd_tile_gossip = {
512 : .name = "gossip",
513 : .populate_allowed_seccomp = populate_allowed_seccomp,
514 : .populate_allowed_fds = populate_allowed_fds,
515 : .scratch_align = scratch_align,
516 : .scratch_footprint = scratch_footprint,
517 : .privileged_init = privileged_init,
518 : .unprivileged_init = unprivileged_init,
519 : .run = stem_run,
520 : };
|