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

Generated by: LCOV version 1.14