LCOV - code coverage report
Current view: top level - discof/gossip - fd_gossip_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 509 0.0 %
Date: 2026-10-22 18:10:43 Functions: 0 24 0.0 %

          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_MCNT_SET( GOSSIP, PING_PEER_ADDED,           ping_tracker_metrics->tracked_cnt         );
     216           0 :   FD_MCNT_SET( GOSSIP, PING_PEER_ADDRESS_CHANGED, ping_tracker_metrics->address_changed_cnt );
     217             : 
     218           0 :   fd_gossip_purged_metrics_t const * purged_metrics = fd_gossip_purged_metrics2( ctx->gossip );
     219             : 
     220           0 :   FD_MGAUGE_SET( GOSSIP, CRDS_PURGED_USED,     purged_metrics->purged_cnt );
     221           0 :   FD_MCNT_SET(   GOSSIP, CRDS_PURGED_EVICTED,  purged_metrics->purged_evicted_cnt );
     222           0 :   FD_MCNT_SET(   GOSSIP, CRDS_PURGED_EXPIRED,  purged_metrics->purged_expired_cnt );
     223             : 
     224           0 :   fd_crds_metrics_t const * crds_metrics = fd_gossip_crds_metrics( ctx->gossip );
     225             : 
     226           0 :   FD_MGAUGE_ENUM_COPY( GOSSIP, CRDS_USED,    crds_metrics->count );
     227           0 :   FD_MCNT_SET(         GOSSIP, CRDS_EXPIRED, crds_metrics->expired_cnt );
     228           0 :   FD_MCNT_SET(         GOSSIP, CRDS_EVICTED, crds_metrics->evicted_cnt );
     229           0 :   FD_MCNT_SET(         GOSSIP, CRDS_RELAY_COALESCED, crds_metrics->relay_coalesced_cnt );
     230             : 
     231           0 :   FD_MGAUGE_SET( GOSSIP, CRDS_PEER_COUNT_STAKED,   crds_metrics->peer_staked_cnt    );
     232           0 :   FD_MGAUGE_SET( GOSSIP, CRDS_PEER_COUNT_UNSTAKED, crds_metrics->peer_unstaked_cnt  );
     233           0 :   FD_MGAUGE_SET( GOSSIP, CRDS_PEER_STAKE_LAMPORTS, crds_metrics->peer_visible_stake );
     234           0 :   FD_MCNT_SET(   GOSSIP, CRDS_PEER_EVICTED,     crds_metrics->peer_evicted_cnt );
     235             : 
     236           0 :   fd_gossip_metrics_t const * metrics = fd_gossip_metrics( ctx->gossip );
     237           0 :   fd_active_set_metrics_t const * active_set_metrics = fd_gossip_active_set_metrics2( ctx->gossip );
     238             : 
     239           0 :   ulong total_message_tx[ FD_METRICS_ENUM_GOSSIP_MESSAGE_TYPE_CNT ] = {0};
     240           0 :   ulong total_message_tx_bytes[ FD_METRICS_ENUM_GOSSIP_MESSAGE_TYPE_CNT ] = {0};
     241           0 :   for( ulong i=0UL; i<FD_METRICS_ENUM_GOSSIP_MESSAGE_TYPE_CNT; i++ ) {
     242           0 :     total_message_tx[ i ] = metrics->message_tx[ i ] + active_set_metrics->message_tx[ i ];
     243           0 :     total_message_tx_bytes[ i ] = metrics->message_tx_bytes[ i ] + active_set_metrics->message_tx_bytes[ i ];
     244           0 :   }
     245           0 :   FD_MCNT_ENUM_COPY( GOSSIP, MESSAGE_TX,              total_message_tx );
     246           0 :   FD_MCNT_ENUM_COPY( GOSSIP, MESSAGE_TX_BYTES,        total_message_tx_bytes );
     247           0 :   FD_MCNT_ENUM_COPY( GOSSIP, CRDS_PUSH_TX,            active_set_metrics->crds_tx_push );
     248           0 :   FD_MCNT_ENUM_COPY( GOSSIP, CRDS_PUSH_TX_BYTES,      active_set_metrics->crds_tx_push_bytes );
     249           0 :   FD_MCNT_ENUM_COPY( GOSSIP, CRDS_PULL_RESPONSE_TX,       metrics->crds_tx_pull_response );
     250           0 :   FD_MCNT_ENUM_COPY( GOSSIP, CRDS_PULL_RESPONSE_TX_BYTES, metrics->crds_tx_pull_response_bytes );
     251             : 
     252           0 :   FD_MCNT_ENUM_COPY( GOSSIP, CRDS_UPSERTED,                 metrics->crds_rx_count );
     253             : 
     254           0 :   FD_MCNT_SET( GOSSIP, SIGN_QUEUE_FULL_DROPPED,               metrics->sign_pend_full_cnt );
     255             : 
     256           0 :   FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STAKED_PEER_ONLINE,    ctx->wfs_peers.online );
     257           0 :   FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STAKE_ONLINE_LAMPORTS, ctx->wfs_stake.online );
     258           0 :   FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STATE, (ulong)ctx->wfs_state );
     259           0 : }
     260             : 
     261             : /* Minimum quiet period (no new peers discovered) before we declare
     262             :    the gossip peer table saturated.  Pull requests fire every ~1.6ms,
     263             :    so 500ms of silence means ~300 pulls returned no new contact
     264             :    infos — a strong convergence signal. */
     265             : #define FD_GOSSIP_PEER_SAT_QUIET_NS (500L*1000L*1000L)
     266             : 
     267             : static void
     268             : after_credit( fd_gossip_tile_ctx_t * ctx,
     269             :               fd_stem_context_t *    stem,
     270             :               int *                  opt_poll_in,
     271           0 :               int *                  charge_busy ) {
     272           0 :   ctx->stem = stem;
     273             : 
     274           0 :   if( FD_UNLIKELY( ctx->is_pending_set_identity ) ) {
     275             :     /* the identity key is swapped after the sign tile has been swapped
     276             :        because the below function directly sends a sign request. */
     277           0 :     FD_BASE58_ENCODE_32_BYTES( ctx->keyswitch->bytes, _new_id_b58 );
     278           0 :     fd_gossip_set_identity( ctx->gossip,
     279           0 :                             ctx->keyswitch->bytes,
     280           0 :                             fd_clock_tile_now( ctx->clock ),
     281           0 :                             (ulong)FD_NANOSEC_TO_MICRO( ctx->keyswitch->param ) );
     282           0 :     ctx->is_halting_signing        = 0;
     283           0 :     ctx->is_pending_set_identity   = 0;
     284           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     285           0 :     *charge_busy = 1;
     286           0 :     return;
     287           0 :   }
     288             : 
     289           0 :   if( FD_UNLIKELY( !ctx->my_contact_info->shred_version ) ) return;
     290             : 
     291           0 :   if( FD_UNLIKELY( ctx->wfs_state==FD_GOSSIP_WFS_STATE_PUBLISH ) ) {
     292           0 :     fd_gossip_out_ctx_t const * out = ctx->update_out + fd_gossip_update_link( FD_GOSSIP_UPDATE_TAG_WFS_DONE );
     293           0 :     fd_stem_publish( ctx->stem, out->idx, FD_GOSSIP_UPDATE_TAG_WFS_DONE, out->chunk, 0UL, 0UL, 0UL, 0UL );
     294           0 :     ctx->wfs_state = FD_GOSSIP_WFS_STATE_DONE;
     295           0 :     *opt_poll_in = 0;
     296           0 :     *charge_busy = 1;
     297           0 :     return;
     298           0 :   }
     299             : 
     300             :   /* Prevent attempts to sign with our old identity while switching to
     301             :      the new one. */
     302           0 :   if( FD_UNLIKELY( ctx->is_halting_signing ) ) return;
     303             : 
     304             :   /* The stem's tick, at most one loop iteration stale, saves an rdtsc
     305             :      per iteration. */
     306           0 :   long now = fd_clock_tile_tickcount_to_wallclock( ctx->clock, stem->now );
     307           0 :   fd_gossip_advance( ctx->gossip, now, stem, charge_busy );
     308             : 
     309             :   /* Peer table saturation detection.  After fd_gossip_advance updates
     310             :      the CRDS, check if the peer count has grown.  If it hasn't grown
     311             :      for FD_GOSSIP_PEER_SAT_QUIET_NS and there is at least one other
     312             :      peer, publish a one-shot PEER_SATURATED notification. */
     313           0 :   if( FD_LIKELY( !ctx->peer_sat_published ) ) {
     314           0 :     fd_crds_metrics_t const * crds_metrics = fd_gossip_crds_metrics( ctx->gossip );
     315           0 :     ulong peer_cnt = crds_metrics->peer_staked_cnt + crds_metrics->peer_unstaked_cnt;
     316           0 :     if( FD_UNLIKELY( peer_cnt>ctx->peer_sat_hwm ) ) {
     317           0 :       ctx->peer_sat_hwm       = peer_cnt;
     318           0 :       ctx->peer_sat_hwm_nanos = now;
     319           0 :     } else if( FD_UNLIKELY( peer_cnt>1UL && ctx->peer_sat_hwm_nanos!=0L &&
     320           0 :                             (now-ctx->peer_sat_hwm_nanos)>FD_GOSSIP_PEER_SAT_QUIET_NS ) ) {
     321           0 :       FD_LOG_INFO(( "gossip peer table saturated (%lu peers, quiet for %ld ms)",
     322           0 :                     peer_cnt, (now-ctx->peer_sat_hwm_nanos)/(1000L*1000L) ));
     323           0 :       fd_gossip_out_ctx_t const * out = ctx->update_out + fd_gossip_update_link( FD_GOSSIP_UPDATE_TAG_PEER_SATURATED );
     324           0 :       fd_stem_publish( ctx->stem, out->idx, FD_GOSSIP_UPDATE_TAG_PEER_SATURATED, out->chunk, 0UL, 0UL, 0UL, 0UL );
     325           0 :       ctx->peer_sat_published = 1;
     326           0 :       *opt_poll_in = 0;
     327           0 :       *charge_busy = 1;
     328           0 :     }
     329           0 :   }
     330           0 : }
     331             : 
     332             : static void
     333             : handle_shred_version( fd_gossip_tile_ctx_t * ctx,
     334           0 :                        ulong                 sig ) {
     335           0 :   ctx->my_contact_info->shred_version = (ushort)sig;
     336           0 :   fd_gossip_set_shred_version( ctx->gossip, (ushort)sig );
     337           0 : }
     338             : 
     339             : static void
     340             : handle_epoch( fd_gossip_tile_ctx_t *      ctx,
     341           0 :               fd_epoch_info_msg_t const * msg ) {
     342           0 :   if( FD_UNLIKELY( msg->staked_vote_cnt>MAX_STAKE_WEIGHTS ) )
     343           0 :     FD_LOG_ERR(( "epoch stakes exceed MAX_STAKE_WEIGHTS=%lu", MAX_STAKE_WEIGHTS ));
     344           0 :   if( FD_UNLIKELY( msg->staked_id_cnt>MAX_STAKE_WEIGHTS ) )
     345           0 :     FD_LOG_ERR(( "epoch id weights exceed MAX_STAKE_WEIGHTS=%lu", MAX_STAKE_WEIGHTS ));
     346             : 
     347           0 :   fd_stake_weight_t const * weights = fd_epoch_info_msg_id_weights( msg );
     348           0 :   fd_gossip_stakes_update( ctx->gossip, weights, msg->staked_id_cnt );
     349           0 : }
     350             : 
     351             : static void
     352             : handle_packet( fd_gossip_tile_ctx_t * ctx,
     353             :                ulong                  sig,
     354             :                uchar const *          payload,
     355             :                ulong                  payload_sz,
     356           0 :                fd_stem_context_t *    stem ) {
     357           0 :   long now = fd_clock_tile_now( ctx->clock );
     358             : 
     359           0 :   fd_ip4_port_t peer = (fd_ip4_port_t){
     360           0 :     .addr = fd_gossvf_sig_addr( sig ),
     361           0 :     .port = fd_gossvf_sig_port( sig )
     362           0 :   };
     363             : 
     364           0 :   switch( fd_gossvf_sig_kind( sig ) ) {
     365           0 :     case 0: {
     366           0 :       fd_gossip_rx( ctx->gossip, peer, payload, payload_sz, now, stem );
     367           0 :       fd_gossip_advance( ctx->gossip, now, stem, NULL );
     368           0 :       break;
     369           0 :     }
     370           0 :     case 1: {
     371           0 :       fd_gossip_pingreq_t * pingreq = (fd_gossip_pingreq_t *)payload;
     372           0 :       fd_gossip_ping_tracker_track( ctx->gossip, pingreq->pubkey.uc, peer, now );
     373           0 :     }
     374           0 :   }
     375           0 : }
     376             : 
     377             : static inline int
     378             : before_frag( fd_gossip_tile_ctx_t * ctx,
     379             :              ulong                  in_idx,
     380             :              ulong                  seq FD_PARAM_UNUSED,
     381           0 :              ulong                  sig FD_PARAM_UNUSED ) {
     382           0 :   if( FD_UNLIKELY( ctx->in[ in_idx ].kind==IN_KIND_SIGN ) ) return 0;
     383             : 
     384             :   /* Defer frag processing while switching identity or learning shred
     385             :      version */
     386           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;
     387             : 
     388           0 :   return 0;
     389           0 : }
     390             : 
     391             : static inline void
     392             : during_frag( fd_gossip_tile_ctx_t * ctx,
     393             :              ulong                  in_idx,
     394             :              ulong                  seq FD_PARAM_UNUSED,
     395             :              ulong                  sig FD_PARAM_UNUSED,
     396             :              ulong                  chunk,
     397             :              ulong                  sz,
     398           0 :              ulong                  ctl FD_PARAM_UNUSED ) {
     399           0 :   switch( ctx->in[ in_idx ].kind ) {
     400           0 :     case IN_KIND_GOSSVF: {
     401           0 :       if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
     402           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 ));
     403             : 
     404           0 :       fd_memcpy( ctx->gossvf_staged, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ), sz );
     405           0 :       break;
     406           0 :     }
     407           0 :     case IN_KIND_SIGN: {
     408           0 :       if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=64UL ) )
     409           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 ));
     410             : 
     411           0 :       fd_memcpy( ctx->sign_staged, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ), 64UL );
     412           0 :       break;
     413           0 :     }
     414           0 :   }
     415           0 : }
     416             : 
     417             : static inline void
     418             : after_frag( fd_gossip_tile_ctx_t * ctx,
     419             :             ulong                  in_idx,
     420             :             ulong                  seq    FD_PARAM_UNUSED,
     421             :             ulong                  sig,
     422             :             ulong                  sz,
     423             :             ulong                  tsorig FD_PARAM_UNUSED,
     424             :             ulong                  tspub  FD_PARAM_UNUSED,
     425           0 :             fd_stem_context_t *    stem ) {
     426           0 :   ctx->stem = stem;
     427             : 
     428           0 :   switch( ctx->in[ in_idx ].kind ) {
     429           0 :     case IN_KIND_GOSSVF: {
     430             : 
     431           0 :       FD_TEST( sz<=sizeof(ctx->gossvf_staged) );
     432             : 
     433           0 :       handle_packet( ctx, sig, ctx->gossvf_staged, sz, stem );
     434           0 :       break;
     435           0 :     }
     436           0 :     case IN_KIND_SIGN: {
     437           0 :       fd_gossip_sign_response( ctx->gossip, ctx->sign_staged, stem, fd_clock_tile_now( ctx->clock ) );
     438           0 :       if( FD_UNLIKELY( ctx->is_halting_signing && !fd_gossip_sign_pend_cnt( ctx->gossip ) &&
     439           0 :                        fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     440           0 :         fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     441           0 :       }
     442           0 :       break;
     443           0 :     }
     444           0 :   }
     445           0 : }
     446             : 
     447             : static inline int
     448             : returnable_frag( fd_gossip_tile_ctx_t * ctx,
     449             :                  ulong                  in_idx,
     450             :                  ulong                  seq FD_PARAM_UNUSED,
     451             :                  ulong                  sig,
     452             :                  ulong                  chunk,
     453             :                  ulong                  sz,
     454             :                  ulong                  ctl FD_PARAM_UNUSED,
     455             :                  ulong                  tsorig FD_PARAM_UNUSED,
     456             :                  ulong                  tspub FD_PARAM_UNUSED,
     457           0 :                  fd_stem_context_t *    stem ) {
     458             : 
     459             :   /* Return early for unreliable links. */
     460           0 :   if( FD_UNLIKELY( ctx->in[ in_idx ].kind==IN_KIND_GOSSVF || ctx->in[ in_idx ].kind==IN_KIND_SIGN ) ) return 0;
     461             : 
     462           0 :   ctx->stem = stem;
     463             : 
     464           0 :   if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
     465           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 ));
     466             : 
     467           0 :   switch( ctx->in[ in_idx ].kind ) {
     468           0 :     case IN_KIND_SHRED_VERSION: handle_shred_version( ctx, sig ); break;
     469           0 :     case IN_KIND_EPOCH:         handle_epoch( ctx, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ) ); break;
     470           0 :     case IN_KIND_SNAPIN_MANIF: {
     471           0 :       if( FD_LIKELY( ctx->wfs_state==FD_GOSSIP_WFS_STATE_DONE ) ) break;
     472             : 
     473           0 :       if( FD_UNLIKELY( fd_ssmsg_sig_message( sig )==FD_SSMSG_DONE ) ) {
     474             :         /* Classify boot against WFS (see fd_wfs.h):
     475             :              MODE_ERROR      -> keep state, never signal WFS_DONE
     476             :              MODE_UNRESOLVED -> error condition
     477             :              MODE_MATCH      -> STATE_WAIT
     478             :              MODE_DISABLED   -> STATE_DONE
     479             :              MODE_NOOP       -> STATE_DONE
     480             : 
     481             :            ERROR is fatal, but replay owns that decision. */
     482           0 :         int wfs_mode = fd_wfs_mode( ctx->wfs_slot, ctx->wfs_hash_is_zero, (ulong)ctx->wfs_shred_version, ctx->wfs_boot_slot );
     483           0 :         if( FD_UNLIKELY( wfs_mode==FD_WFS_MODE_ERROR ) ) {
     484           0 :           FD_LOG_WARNING(( "boot slot %lu is behind [consensus.wait_for_supermajority_at_slot] %lu; "
     485           0 :                            "the supermajority wait will not complete",
     486           0 :                            ctx->wfs_boot_slot, ctx->wfs_slot ));
     487           0 :           break;
     488           0 :         }
     489           0 :         if( FD_UNLIKELY( wfs_mode==FD_WFS_MODE_UNRESOLVED ) ) {
     490           0 :           FD_LOG_CRIT(( "snapshot DONE received before any manifest" ));
     491           0 :         }
     492           0 :         ctx->wfs_state = fd_int_if( wfs_mode==FD_WFS_MODE_MATCH,
     493           0 :                                     FD_GOSSIP_WFS_STATE_WAIT, FD_GOSSIP_WFS_STATE_DONE );
     494           0 :         break;
     495           0 :       }
     496             : 
     497             :       /* FIXME: Replace handling for this when manifest supports larger
     498             :          vote and stake account bounds. */
     499           0 :       fd_snapshot_manifest_t const * manifest = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
     500             : 
     501             :       /* Latest manifest, not a running max (see fd_wfs.h): a stale max
     502             :          would classify NOOP here against replay's MATCH, and deadlock. */
     503           0 :       ctx->wfs_boot_slot = manifest->slot;
     504             : 
     505           0 :       ulong wfs_stakes_unconverted_cnt = 0UL;
     506           0 :       ctx->wfs_stake.online = 0UL;
     507           0 :       ctx->wfs_stake.total  = 0UL;
     508           0 :       ctx->wfs_peers.online = 0UL;
     509           0 :       ctx->wfs_peers.total  = 0UL;
     510           0 :       memset( ctx->wfs_active, 0, sizeof(ctx->wfs_active) );
     511             : 
     512           0 :       FD_TEST( manifest->vote_accounts_len<=FD_RUNTIME_MAX_SNAPSHOT_VOTE_ACCOUNTS );
     513           0 :       for( ulong i=0UL; i<manifest->vote_accounts_len; i++ ) {
     514           0 :           if( FD_UNLIKELY( manifest->vote_accounts[ i ].stake==0UL ) ) continue;
     515           0 :           ctx->wfs_stake.total += manifest->vote_accounts[ i ].stake;
     516             : 
     517           0 :           fd_memcpy( ctx->wfs_stakes_scratch[ wfs_stakes_unconverted_cnt ].id_key.uc, manifest->vote_accounts[ i ].node_account_pubkey, sizeof(fd_pubkey_t) );
     518           0 :           fd_memcpy( ctx->wfs_stakes_scratch[ wfs_stakes_unconverted_cnt ].vote_key.uc, manifest->vote_accounts[ i ].vote_account_pubkey, sizeof(fd_pubkey_t) );
     519           0 :           ctx->wfs_stakes_scratch[ wfs_stakes_unconverted_cnt ].stake = manifest->vote_accounts[ i ].stake;
     520           0 :           wfs_stakes_unconverted_cnt++;
     521           0 :       }
     522           0 :       ctx->wfs_stakes_cnt = compute_id_weights_from_vote_weights( ctx->wfs_stakes, ctx->wfs_stakes_scratch, wfs_stakes_unconverted_cnt );
     523             : 
     524             :       /* sort for quick lookup */
     525           0 :       fd_stake_weight_key_sort_inplace( ctx->wfs_stakes, ctx->wfs_stakes_cnt );
     526             : 
     527           0 :       ctx->wfs_peers.total = ctx->wfs_stakes_cnt;
     528           0 :       FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STAKED_PEER_COUNT,      ctx->wfs_peers.total );
     529           0 :       FD_MGAUGE_SET( GOSSIP, WAIT_FOR_SUPERMAJORITY_STAKE_TRACKED_LAMPORTS, ctx->wfs_stake.total );
     530             : 
     531           0 :       break;
     532           0 :     }
     533           0 :     default: FD_LOG_ERR(( "unreachable" ));
     534           0 :   }
     535             : 
     536           0 :   return 0;
     537           0 : }
     538             : 
     539             : static void
     540             : privileged_init( fd_topo_t const *      topo,
     541           0 :                  fd_topo_tile_t const * tile ) {
     542           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     543             : 
     544           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     545           0 :   fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
     546           0 :   fd_memset( ctx, 0, sizeof(fd_gossip_tile_ctx_t) );
     547             : 
     548           0 :   if( FD_UNLIKELY( !strcmp( tile->gossip.identity_key_path, "" ) ) )
     549           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     550             : 
     551           0 :   ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->gossip.identity_key_path, /* pubkey only: */ 1 ) );
     552           0 :   FD_TEST( fd_rng_secure( &ctx->rng_seed, 4UL ) );
     553           0 :   FD_TEST( fd_rng_secure( &ctx->rng_idx,  8UL ) );
     554           0 :   FD_TEST( fd_rng_secure( ctx->ping_seed, 32UL ) );
     555           0 :   FD_TEST( fd_rng_secure( &ctx->gui_bw_seed, 8UL ) );
     556             : 
     557           0 :   FD_TEST( tile->gossip.entrypoints_cnt<=FD_TOPO_GOSSIP_ENTRYPOINTS_MAX );
     558           0 :   ctx->entrypoints_cnt = tile->gossip.entrypoints_cnt;
     559           0 :   fd_dns_resolve_peers( tile->gossip.entrypoints[ 0 ], sizeof(tile->gossip.entrypoints[ 0 ]), tile->gossip.entrypoints_cnt, "gossip.entrypoints", ctx->entrypoints );
     560             : 
     561           0 :   if( FD_LIKELY( tile->gossip.gossip_host[ 0 ]!='\0' ) ) {
     562           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 ));
     563           0 :   } else {
     564           0 :     ctx->gossip_ip_addr = tile->gossip.net_ip_addr;
     565           0 :   }
     566           0 : }
     567             : 
     568             : static inline fd_gossip_out_ctx_t
     569             : out1( fd_topo_t const *      topo,
     570             :       fd_topo_tile_t const * tile,
     571           0 :       char const *           name ) {
     572           0 :   ulong idx = ULONG_MAX;
     573             : 
     574           0 :   for( ulong i=0UL; i<tile->out_cnt; i++ ) {
     575           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
     576           0 :     if( !strcmp( link->name, name ) ) {
     577           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 ));
     578           0 :       idx = i;
     579           0 :     }
     580           0 :   }
     581             : 
     582           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 ));
     583             : 
     584           0 :   ulong mtu = topo->links[ tile->out_link_id[ idx ] ].mtu;
     585           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 };
     586             : 
     587           0 :   void * mem   = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
     588           0 :   ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
     589           0 :   ulong wmark  = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
     590             : 
     591           0 :   return (fd_gossip_out_ctx_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
     592           0 : }
     593             : 
     594             : static void
     595             : unprivileged_init( fd_topo_t const *      topo,
     596           0 :                    fd_topo_tile_t const * tile ) {
     597           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     598             : 
     599           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     600           0 :   fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
     601           0 :   void * _gossip             = FD_SCRATCH_ALLOC_APPEND( l, fd_gossip_align(),             fd_gossip_footprint( tile->gossip.max_entries, tile->gossip.entrypoints_cnt ) );
     602           0 :   void * _gui_bw             = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_gossip_bw_align(),      fd_gui_gossip_bw_footprint()                                                  );
     603             : 
     604           0 :   FD_TEST( fd_rng_join( fd_rng_new( ctx->rng, ctx->rng_seed, ctx->rng_idx ) ) );
     605             : 
     606             :   /* If WFS is enabled (see fd_wfs.h), start in INIT and let the
     607             :      manifest slot resolve the mode; otherwise immediately DONE. */
     608           0 :   ctx->wfs_slot          = tile->gossip.wait_for_supermajority_at_slot;
     609           0 :   ctx->wfs_shred_version = tile->gossip.expected_shred_version;
     610           0 :   ctx->wfs_boot_slot     = ULONG_MAX;
     611           0 :   ctx->wfs_hash_is_zero  = tile->gossip.wait_for_supermajority_hash_is_zero;
     612           0 :   ctx->wfs_state = fd_int_if( fd_wfs_configured( ctx->wfs_slot, ctx->wfs_hash_is_zero, (ulong)ctx->wfs_shred_version ),
     613           0 :                               FD_GOSSIP_WFS_STATE_INIT, FD_GOSSIP_WFS_STATE_DONE );
     614             : 
     615           0 :   FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
     616           0 :   ulong sign_in_tile_idx = ULONG_MAX;
     617           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     618           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     619           0 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     620             : 
     621           0 :     ctx->in[ i ].mem = link_wksp->wksp;
     622           0 :     if( FD_LIKELY( link->mtu ) ) {
     623           0 :       ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
     624           0 :       ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
     625           0 :     }
     626           0 :     ctx->in[ i ].mtu = link->mtu;
     627             : 
     628           0 :     if( FD_UNLIKELY( !strcmp( link->name, "ipecho_out" ) ) ) {
     629           0 :       ctx->in[ i ].kind = IN_KIND_SHRED_VERSION;
     630           0 :     } else if( FD_UNLIKELY( !strcmp( link->name, "gossvf_gossip" ) ) ) {
     631           0 :       ctx->in[ i ].kind = IN_KIND_GOSSVF;
     632           0 :       FD_TEST( link->mtu<=sizeof(ctx->gossvf_staged) );
     633           0 :     } else if( FD_UNLIKELY( !strcmp( link->name, "sign_gossip" ) ) ) {
     634           0 :       ctx->in[ i ].kind = IN_KIND_SIGN;
     635           0 :       sign_in_tile_idx = i;
     636           0 :     } else if( FD_UNLIKELY( !strcmp( link->name, "replay_epoch" ) ) ) {
     637           0 :       ctx->in[ i ].kind = IN_KIND_EPOCH;
     638           0 :     } else if( FD_UNLIKELY( !strcmp( link->name, "snapin_manif" ) ) ) {
     639           0 :       ctx->in[ i ].kind = IN_KIND_SNAPIN_MANIF;
     640           0 :     } else {
     641           0 :       FD_LOG_ERR(( "unexpected input link name %s", link->name ));
     642           0 :     }
     643           0 :   }
     644             : 
     645           0 :   if( FD_UNLIKELY( sign_in_tile_idx==ULONG_MAX ) )
     646           0 :     FD_LOG_ERR(( "tile %s:%lu had no input link named sign_gossip", tile->name, tile->kind_id ));
     647             : 
     648           0 :   *ctx->net_out    = out1( topo, tile, "gossip_net"    );
     649           0 :   *ctx->sign_out   = out1( topo, tile, "gossip_sign"   );
     650           0 :   ctx->update_out[ FD_GOSSIP_UPDATE_LINK_CI_ADDR ] = out1( topo, tile, "gossip_ciaddr" );
     651           0 :   ctx->update_out[ FD_GOSSIP_UPDATE_LINK_CI_SEEN ] = fd_topo_find_tile_out_link( topo, tile, "gossip_ciseen", 0UL )!=ULONG_MAX
     652           0 :                                                    ? out1( topo, tile, "gossip_ciseen" )
     653           0 :                                                    : (fd_gossip_out_ctx_t){ .idx = ULONG_MAX }; /* no gui */
     654           0 :   ctx->update_out[ FD_GOSSIP_UPDATE_LINK_VOTE    ] = fd_topo_find_tile_out_link( topo, tile, "gossip_vote", 0UL )!=ULONG_MAX
     655           0 :                                                    ? out1( topo, tile, "gossip_vote" )
     656           0 :                                                    : (fd_gossip_out_ctx_t){ .idx = ULONG_MAX }; /* no vote readers */
     657           0 :   ctx->update_out[ FD_GOSSIP_UPDATE_LINK_MISC    ] = out1( topo, tile, "gossip_misc"   );
     658           0 :   *ctx->gossvf_out = out1( topo, tile, "gossip_gossvf" );
     659             : 
     660           0 :   ctx->has_gui = fd_topo_find_tile_out_link( topo, tile, "gossip_gui", 0UL )!=ULONG_MAX;
     661           0 :   if( FD_LIKELY( ctx->has_gui ) ) *ctx->gui_out = out1( topo, tile, "gossip_gui" );
     662           0 :   ctx->gui_bw = fd_gui_gossip_bw_join( fd_gui_gossip_bw_new( _gui_bw, ctx->gui_bw_seed ) );
     663           0 :   FD_TEST( ctx->gui_bw );
     664             : 
     665           0 :   fd_topo_link_t const * sign_in  = &topo->links[ tile->in_link_id [ sign_in_tile_idx  ] ];
     666           0 :   fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ ctx->sign_out->idx ] ];
     667           0 :   FD_TEST( sign_in->mtu==64UL );
     668           0 :   FD_TEST( sign_in->depth>=FD_GOSSIP_SIGN_PEND_MAX && sign_out->depth>=FD_GOSSIP_SIGN_PEND_MAX );
     669           0 :   ctx->sign_out_mtu = sign_out->mtu;
     670             : 
     671           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
     672           0 :   FD_TEST( ctx->keyswitch );
     673             : 
     674           0 :   fd_clock_tile_init( ctx->clock );
     675             : 
     676           0 :   ctx->my_contact_info->shred_version = tile->gossip.expected_shred_version;
     677             : 
     678           0 :   ctx->my_contact_info->outset = (ulong)FD_NANOSEC_TO_MICRO( tile->gossip.boot_timestamp_nanos );
     679             : 
     680           0 :   ctx->my_contact_info->version.client      = FD_GOSSIP_CONTACT_INFO_CLIENT_FIREDANCER;
     681           0 :   ctx->my_contact_info->version.major       = (ushort)fd_major_version;
     682           0 :   ctx->my_contact_info->version.minor       = (ushort)fd_minor_version;
     683           0 :   ctx->my_contact_info->version.patch       = (ushort)fd_patch_version;
     684           0 :   ctx->my_contact_info->version.commit      = fd_commit_ref_u32;
     685           0 :   ctx->my_contact_info->version.feature_set = FD_FEATURE_SET_ID;
     686             : 
     687           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 )   };
     688           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 )      };
     689           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 )      };
     690           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 )      };
     691           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 ) };
     692           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 ) };
     693           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 ) };
     694           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 )      };
     695           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 )   };
     696           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 )   };
     697           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 )    };
     698             : 
     699           0 :   ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_TVU_QUIC ]          = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = 0, .port = 0 };
     700           0 :   ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC ]               = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = 0, .port = 0 };
     701           0 :   ctx->my_contact_info->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC_PUBSUB ]        = (fd_gossip_socket_t){ .is_ipv6 = 0, .ip4 = 0, .port = 0 };
     702             : 
     703           0 :   ctx->gossip = fd_gossip_join( fd_gossip_new( _gossip,
     704           0 :                                                ctx->rng,
     705           0 :                                                ctx->ping_seed,
     706           0 :                                                tile->gossip.max_entries,
     707           0 :                                                ctx->entrypoints_cnt,
     708           0 :                                                ctx->entrypoints,
     709           0 :                                                ctx->identity_key->uc,
     710           0 :                                                ctx->my_contact_info,
     711           0 :                                                fd_clock_tile_now( ctx->clock ),
     712           0 :                                                gossip_send_fn,
     713           0 :                                                ctx,
     714           0 :                                                gossip_sign_fn,
     715           0 :                                                ctx,
     716           0 :                                                gossip_ping_tracker_change_fn,
     717           0 :                                                ctx,
     718           0 :                                                gossip_activity_update_fn,
     719           0 :                                                ctx,
     720           0 :                                                ctx->update_out,
     721           0 :                                                ctx->net_out ) );
     722           0 :   FD_TEST( ctx->gossip );
     723             : 
     724           0 :   FD_MGAUGE_SET( GOSSIP, CRDS_CAPACITY,        tile->gossip.max_entries     );
     725           0 :   FD_MGAUGE_SET( GOSSIP, CRDS_PEER_CAPACITY,   FD_CONTACT_INFO_TABLE_SIZE   );
     726           0 :   FD_MGAUGE_SET( GOSSIP, CRDS_PURGED_CAPACITY, tile->gossip.max_entries     );
     727             : 
     728           0 :   fd_ip4_udp_hdr_init( ctx->net_out_hdr, FD_GOSSIP_MTU, tile->gossip.bind_ip_addr, tile->gossip.ports.gossip );
     729             : 
     730           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     731           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     732           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     733           0 : }
     734             : 
     735             : static ulong
     736             : populate_allowed_seccomp( fd_topo_t const *      topo,
     737             :                           fd_topo_tile_t const * tile,
     738             :                           ulong                  out_cnt,
     739           0 :                           struct sock_filter *   out ) {
     740           0 :   (void)topo;
     741           0 :   (void)tile;
     742             : 
     743           0 :   populate_sock_filter_policy_fd_gossip_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     744           0 :   return sock_filter_policy_fd_gossip_tile_instr_cnt;
     745           0 : }
     746             : 
     747             : static ulong
     748             : populate_allowed_fds( fd_topo_t const *      topo,
     749             :                       fd_topo_tile_t const * tile,
     750             :                       ulong                  out_fds_cnt,
     751           0 :                       int *                  out_fds ) {
     752           0 :   (void)topo;
     753           0 :   (void)tile;
     754             : 
     755           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     756             : 
     757           0 :   ulong out_cnt = 0UL;
     758           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     759           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     760           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     761           0 :   return out_cnt;
     762           0 : }
     763             : 
     764             : /* STEM_BURST must bound the maximum number of fd_stem_publish
     765             :    calls on any single output link between two consecutive
     766             :    credit checks in the stem run loop.  One iteration consists
     767             :    of after_credit (which calls fd_gossip_advance) followed by
     768             :    processing one input fragment (returnable_frag).
     769             : 
     770             :    The reliable output links and their per-iteration worst cases:
     771             : 
     772             :    gossvf_out (via gossip_ping_tracker_change_fn):
     773             :      tx_ping evictions + expiries            FD_PING_TRACKER_MAX
     774             :      fd_ping_tracker_track from rx_values    2*FD_GOSSIP_MESSAGE_MAX_CRDS
     775             :      Total: FD_PING_TRACKER_MAX + 2*FD_GOSSIP_MESSAGE_MAX_CRDS
     776             : 
     777             :    gossip_ciaddr and gossip_ciseen (via fd_gossip_tx_publish_chunk),
     778             :    each at most one frag per contact info event:
     779             :      fd_crds_advance expire (ContactInfos)   FD_CONTACT_INFO_TABLE_SIZE
     780             :      fd_crds_insert publish + evictions      2*FD_GOSSIP_MESSAGE_MAX_CRDS
     781             :      Total: FD_CONTACT_INFO_TABLE_SIZE + 2*FD_GOSSIP_MESSAGE_MAX_CRDS
     782             : 
     783             :    gossip_vote and gossip_misc carry a subset of the fd_crds_insert
     784             :    publishes (at most FD_GOSSIP_MESSAGE_MAX_CRDS each, plus a few local
     785             :    pushes), so the contact info links bound them.
     786             : 
     787             :    Among the reliable output links, gossvf_out dominates.  Every
     788             :    reliable out link must be at least STEM_BURST deep. */
     789             : 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" );
     790             : 
     791             : /* Raw state values exported by the metrics (states= enums) */
     792             : FD_STATIC_ASSERT( FD_GOSSIP_WFS_STATE_INIT==FD_METRICS_ENUM_WAIT_FOR_SUPERMAJORITY_STATE_V_LOADING_STAKES_IDX, metrics_state );
     793             : FD_STATIC_ASSERT( FD_GOSSIP_WFS_STATE_WAIT==FD_METRICS_ENUM_WAIT_FOR_SUPERMAJORITY_STATE_V_WAITING_FOR_STAKE_IDX, metrics_state );
     794             : FD_STATIC_ASSERT( FD_GOSSIP_WFS_STATE_PUBLISH==FD_METRICS_ENUM_WAIT_FOR_SUPERMAJORITY_STATE_V_PUBLISHING_IDX, metrics_state );
     795             : FD_STATIC_ASSERT( FD_GOSSIP_WFS_STATE_DONE==FD_METRICS_ENUM_WAIT_FOR_SUPERMAJORITY_STATE_V_DONE_OR_DISABLED_IDX, metrics_state );
     796           0 : #define STEM_BURST (FD_PING_TRACKER_MAX+2UL*FD_GOSSIP_MESSAGE_MAX_CRDS)
     797             : 
     798           0 : #define STEM_LAZY  (128L*3000L)
     799             : 
     800           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_gossip_tile_ctx_t
     801           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gossip_tile_ctx_t)
     802             : 
     803           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     804           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     805           0 : #define STEM_CALLBACK_NEXT_DEADLINE       next_deadline
     806           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     807           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
     808           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
     809           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     810           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     811           0 : #define STEM_CALLBACK_RETURNABLE_FRAG     returnable_frag
     812             : 
     813             : #include "../../disco/stem/fd_stem.c"
     814             : 
     815             : fd_topo_run_tile_t fd_tile_gossip = {
     816             :   .name                     = "gossip",
     817             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     818             :   .populate_allowed_fds     = populate_allowed_fds,
     819             :   .scratch_align            = scratch_align,
     820             :   .scratch_footprint        = scratch_footprint,
     821             :   .privileged_init          = privileged_init,
     822             :   .unprivileged_init        = unprivileged_init,
     823             :   .run                      = stem_run,
     824             : };

Generated by: LCOV version 1.14