LCOV - code coverage report
Current view: top level - discof/gossip - fd_gossip_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 59 526 11.2 %
Date: 2026-10-18 04:43:39 Functions: 3 52 5.8 %

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

Generated by: LCOV version 1.14