LCOV - code coverage report
Current view: top level - discof/gossip - fd_gossip_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 459 0.0 %
Date: 2026-08-13 04:56:22 Functions: 0 23 0.0 %

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

Generated by: LCOV version 1.14