LCOV - code coverage report
Current view: top level - disco/events - fd_event_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 502 0.0 %
Date: 2026-11-11 12:40:43 Functions: 0 15 0.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_circq.h"
       3             : #include "fd_event_client.h"
       4             : #include "fd_boot_report.h"
       5             : 
       6             : #include "../fd_txn_m.h"
       7             : #include "../fd_clock_tile.h"
       8             : #include "../metrics/fd_metrics.h"
       9             : #include "../metrics/fd_metrics_sample.h"
      10             : #include "../net/fd_net_tile.h"
      11             : #include "../../discof/genesis/fd_genesi_tile.h"
      12             : #include "../repair/fd_repair_serde.h"
      13             : #include "generated/fd_event_gen.h"
      14             : #include "../keyguard/fd_keyload.h"
      15             : #include "../keyguard/fd_keyswitch.h"
      16             : #include "../topo/fd_topo.h"
      17             : #include "../waker/fd_waker.h"
      18             : #include "../../waltz/resolv/fd_netdb.h"
      19             : #include "../../waltz/http/fd_url.h"
      20             : #include "../../ballet/pb/fd_pb_encode.h"
      21             : 
      22             : #include "../../ballet/x509/fd_x509_ca_store.h"
      23             : 
      24             : #include <unistd.h>
      25             : #include <fcntl.h>
      26             : #include <errno.h>
      27             : #include <unistd.h>
      28             : #include <sys/socket.h>
      29             : #include <sys/syscall.h>
      30             : #include <netinet/in.h>
      31             : #include <netinet/tcp.h>
      32             : 
      33             : #include <linux/futex.h>
      34             : #include "generated/fd_event_tile_seccomp.h"
      35             : 
      36           0 : #define GRPC_BUF_MAX (12UL<<20UL) /* 12 MiB */
      37             : 
      38             : /* The largest event, compressed in the worst case, has to fit the
      39             :    gRPC send buffer. */
      40             : FD_STATIC_ASSERT( FD_EVENT_CLIENT_FITS( FD_EVENT_BLOCK_COMPLETED_BUF_MAX, GRPC_BUF_MAX ), event_fits_grpc_tx_buf );
      41             : 
      42             : /* Raw state values exported by the metrics (states= enums) */
      43             : FD_STATIC_ASSERT( FD_EVENT_CLIENT_STATE_DISCONNECTED==FD_METRICS_ENUM_EVENT_CONN_STATE_V_DISCONNECTED_IDX, metrics_state );
      44             : FD_STATIC_ASSERT( FD_EVENT_CLIENT_STATE_CONNECTING==FD_METRICS_ENUM_EVENT_CONN_STATE_V_CONNECTING_IDX, metrics_state );
      45             : FD_STATIC_ASSERT( FD_EVENT_CLIENT_STATE_AUTHENTICATING==FD_METRICS_ENUM_EVENT_CONN_STATE_V_AUTHENTICATING_IDX, metrics_state );
      46             : FD_STATIC_ASSERT( FD_EVENT_CLIENT_STATE_CONNECTED==FD_METRICS_ENUM_EVENT_CONN_STATE_V_CONNECTED_IDX, metrics_state );
      47             : FD_STATIC_ASSERT( FD_EVENT_CLIENT_FITS( FD_EVENT_BOOT_BUF_MAX, GRPC_BUF_MAX ), boot_event_fits_grpc_tx_buf );
      48             : 
      49             : /* Sized so the event workspace (circq + ~144 MiB client/ctx) fits in one
      50             :    gigantic page. */
      51           0 : #define EVENT_CIRCQ_SZ ((1UL<<30UL)-(160UL<<20UL))
      52             : 
      53           0 : #define METRICS_SAMPLE_INTERVAL_SECS (10L)
      54             : 
      55             : /* The worst-case size of a Txn event:
      56             :    - Fixed overhead:
      57             :      - nonce                                    15
      58             :      - event_id                                 15
      59             :      - link_seq                                 15
      60             :      - timestamp                                15
      61             :      - Event protobuf submessage opener         10
      62             :      - Txn protobuf submessage opener           10
      63             :      - source_ip                                14
      64             :      - source_port                              10
      65             :      - protocol                                 10
      66             :      - bundle_id                                15
      67             :      - bundle_txn_count                         10
      68             :      - bundle_commission                        10
      69             :      - bundle_commission_pubkey                 42
      70             :      - payload tag                               5
      71             :      - payload length prefix                     5
      72             :      - PB_ENCODER_SLACK                         32
      73             :     Total:                                     233
      74             : 
      75             :    So the worst-case size is 233 + FD_TPU_MTU.
      76             : 
      77             :    TODO: this needs to be auto-generated as this is fragile,
      78             :    have the schema generator spit out max sizes for messages. */
      79           0 : #define EVENT_TXN_BUF_MAX (FD_TPU_MTU+233UL)
      80             : 
      81           0 : #define IN_KIND_SHRED  (0)
      82           0 : #define IN_KIND_DEDUP  (1)
      83             : #define IN_KIND_SIGN   (2)
      84           0 : #define IN_KIND_GENESI (3)
      85           0 : #define IN_KIND_IPECHO (4)
      86           0 : #define IN_KIND_EVENT  (5)
      87             : 
      88             : union fd_event_tile_in {
      89             :   struct {
      90             :     fd_wksp_t * mem;
      91             :     ulong       mtu;
      92             :     ulong       chunk0;
      93             :     ulong       wmark;
      94             :   };
      95             :   fd_net_rx_bounds_t net_rx;
      96             : };
      97             : 
      98             : typedef union fd_event_tile_in fd_event_tile_in_t;
      99             : 
     100             : struct fd_event_tile {
     101             :   fd_circq_t * circq;
     102             :   fd_event_client_t * client;
     103             : 
     104             :   fd_topo_t const * topo;
     105             : 
     106             :   int tile_shutdown_rendered[ FD_TOPO_MAX_TILES ];
     107             : 
     108             :   fd_keyswitch_t * keyswitch;
     109             : 
     110             :   ulong idle_cnt;
     111             : 
     112             :   ulong   waker_client_idx;
     113             :   ulong * waker_fseq;
     114             :   long    next_poll_deadline;
     115             : 
     116             :   ulong boot_id;
     117             :   ulong machine_id;
     118             :   ulong instance_id;
     119             :   ulong seed;
     120             : 
     121             :   ulong chunk;
     122             : 
     123             :   ushort shred_source_port;
     124             :   int    shred_skip; /* repair ping or metadata response, not a shred */
     125             :   ulong shred_buf_sz;
     126             :   uchar shred_buf[ FD_NET_MTU ];
     127             : 
     128             :   ulong event_type;
     129             :   ulong event_sz;
     130             :   uchar event_buf[ FD_EVENT_GEN_STRUCT_MAX ] __attribute__((aligned(FD_EVENT_GEN_STRUCT_ALIGN)));
     131             : 
     132             :   uchar identity_pubkey[ 32UL ];
     133             : 
     134             :   int use_tls;
     135             : 
     136             :   /* CA trust store, loaded in privileged_init and referenced by the
     137             :      event client for certificate chain verification. */
     138             :   fd_x509_ca_store_t ca_store[1];
     139             : 
     140             :   fd_keyguard_client_t keyguard_client[1];
     141             :   fd_rng_t rng[1];
     142             : 
     143             :   fd_netdb_fds_t netdb_fds[1];
     144             : 
     145             :   fd_clock_tile_t clock[1];
     146             : 
     147             :   fd_boot_report_t boot_report[1];
     148             : 
     149             :   long   metrics_sample_next_nanos;
     150             :   ulong  metrics_sample_id;
     151             :   ulong  metrics_sample_tile_cnt;
     152             : 
     153             :   fd_metrics_sample_schema_t const * sample_schema[ FD_TOPO_MAX_TILES ]; /* NULL: tile not sampled */
     154             : 
     155             :   double ns_per_tick;
     156             : 
     157             :   ulong in_cnt;
     158             :   int in_kind[ FD_TOPO_MAX_TILE_IN_LINKS ];
     159             :   fd_event_tile_in_t in[ FD_TOPO_MAX_TILE_IN_LINKS ];
     160             : };
     161             : 
     162             : typedef struct fd_event_tile fd_event_tile_t;
     163             : 
     164             : FD_FN_CONST static inline ulong
     165           0 : scratch_align( void ) {
     166           0 :   ulong a = alignof( fd_event_tile_t );
     167           0 :   a = fd_ulong_max( a, fd_event_client_align() );
     168           0 :   a = fd_ulong_max( a, fd_circq_align() );
     169           0 :   return a;
     170           0 : }
     171             : 
     172             : FD_FN_PURE static inline ulong
     173           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     174           0 :   (void)tile;
     175             : 
     176           0 :   ulong l = FD_LAYOUT_INIT;
     177           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_event_tile_t), sizeof(fd_event_tile_t)                   );
     178           0 :   l = FD_LAYOUT_APPEND( l, fd_event_client_align(),  fd_event_client_footprint( GRPC_BUF_MAX ) );
     179           0 :   l = FD_LAYOUT_APPEND( l, fd_circq_align(),         fd_circq_footprint( EVENT_CIRCQ_SZ )      );
     180           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     181           0 : }
     182             : 
     183             : static inline void
     184           0 : metrics_write( fd_event_tile_t * ctx ) {
     185           0 :   FD_MGAUGE_SET( EVENT, QUEUE_USED, ctx->circq->cnt );
     186           0 :   FD_MGAUGE_SET( EVENT, QUEUE_UNSENT_COUNT, fd_circq_unsent_cnt( ctx->circq ) );
     187           0 :   FD_MCNT_SET( EVENT, QUEUE_EVICTED, ctx->circq->metrics.drop_cnt );
     188           0 :   FD_MGAUGE_SET( EVENT, QUEUE_USED_BYTES, fd_circq_bytes_used( ctx->circq ) );
     189           0 :   FD_MGAUGE_SET( EVENT, QUEUE_CAPACITY_BYTES, ctx->circq->size );
     190             : 
     191           0 :   fd_event_client_metrics_t const * metrics = fd_event_client_metrics( ctx->client );
     192           0 :   FD_MCNT_SET( EVENT, EVENT_TX,      metrics->events_sent );
     193           0 :   FD_MCNT_SET( EVENT, ACK_RX,        metrics->events_acked );
     194           0 :   FD_MGAUGE_SET( EVENT, LAST_ACKED_NONCE, metrics->last_acked_id );
     195           0 :   FD_MCNT_SET( EVENT, STREAM_TX_BYTES,               metrics->bytes_written );
     196           0 :   FD_MCNT_SET( EVENT, STREAM_RX_BYTES,               metrics->bytes_read );
     197           0 :   FD_MCNT_SET( EVENT, AUTH_FAILED,                   metrics->auth_fail_cnt );
     198           0 :   FD_MCNT_SET( EVENT, RESPONSE_MALFORMED_REJECTED,   metrics->invalid_msg_cnt );
     199           0 :   FD_MCNT_SET( EVENT, CONN_ATTEMPTED,                metrics->connect_attempt_cnt );
     200           0 :   FD_MCNT_SET( EVENT, HANDSHAKE_TIMED_OUT,           metrics->handshake_timeout_cnt );
     201           0 :   FD_MCNT_SET( EVENT, CREDIT_STALLED,                metrics->credit_stall_cnt );
     202             : 
     203           0 :   FD_MGAUGE_SET( EVENT, CONN_STATE,        fd_event_client_state( ctx->client ) );
     204           0 : }
     205             : 
     206             : static void
     207             : metrics_sample_push( fd_event_tile_t *                  ctx,
     208             :                      fd_metrics_sample_schema_t const * schema,
     209             :                      fd_topo_tile_t const *             tile,
     210             :                      ulong                              link_idx,
     211             :                      fd_topo_link_t const *             link,
     212             :                      ulong const volatile *             area,
     213             :                      uint                               reason,
     214             :                      ulong                              slot,
     215             :                      ulong                              snapct_state,
     216             :                      int                                caught_up,
     217           0 :                      long                               now ) {
     218           0 :   ulong   buf_max = schema->buf_max;
     219           0 :   uchar * buffer  = fd_circq_push_back( ctx->circq, 1UL, buf_max );
     220           0 :   FD_TEST( buffer );
     221             : 
     222           0 :   fd_pb_encoder_t encoder[1];
     223           0 :   fd_pb_encoder_init( encoder, buffer, buf_max );
     224             : 
     225           0 :   int ok = 1;
     226           0 :   FD_TEST( ctx->circq->cursor_push_seq );
     227           0 :   ok &= !!fd_pb_push_uint64( encoder, 1U, ctx->circq->cursor_push_seq-1UL );
     228           0 :   ok &= !!fd_pb_push_uint64( encoder, 2U, fd_event_client_id_reserve( ctx->client ) );
     229           0 :   ok &= !!fd_pb_push_uint64( encoder, 3U, 0UL ); /* link_seq */
     230           0 :   ok &= !!fd_pb_push_uint64( encoder, 4U, (ulong)now );
     231             : 
     232           0 :   ok &= !!fd_pb_submsg_open( encoder, 5U ); /* Event */
     233           0 :   ok &= !!fd_pb_submsg_open( encoder, schema->id );
     234           0 :   ok &= !!fd_pb_push_uint64( encoder, FD_METRICS_SAMPLE_F_SAMPLE_ID, ctx->metrics_sample_id );
     235           0 :   ok &= !!fd_pb_push_int32 ( encoder, FD_METRICS_SAMPLE_F_SAMPLE_REASON, (int)reason );
     236           0 :   if( slot ) ok &= !!fd_pb_push_uint64( encoder, FD_METRICS_SAMPLE_F_SAMPLE_SLOT, slot );
     237           0 :   if( snapct_state<FD_METRICS_SAMPLE_SNAPCT_STATE_CNT ) {
     238           0 :     ok &= !!fd_pb_push_int32( encoder, FD_METRICS_SAMPLE_F_SAMPLE_SNAPCT_STATE, (int)fd_metrics_sample_snapct_state[ snapct_state ] );
     239           0 :   }
     240           0 :   if( caught_up ) ok &= !!fd_pb_push_bool( encoder, FD_METRICS_SAMPLE_F_SAMPLE_CAUGHT_UP, 1 );
     241           0 :   ok &= !!fd_pb_push_uint64( encoder, FD_METRICS_SAMPLE_F_SAMPLE_TILE_CNT, ctx->metrics_sample_tile_cnt );
     242           0 :   if( tile->kind_id ) ok &= !!fd_pb_push_uint64( encoder, FD_METRICS_SAMPLE_F_KIND_ID, tile->kind_id );
     243           0 :   if( link_idx!=ULONG_MAX ) {
     244           0 :     ok &= !!fd_pb_push_cstr  ( encoder, FD_METRICS_SAMPLE_F_TILE, tile->name );
     245           0 :     ok &= !!fd_pb_push_cstr  ( encoder, FD_METRICS_SAMPLE_F_LINK, link->name );
     246           0 :     if( link_idx ) ok &= !!fd_pb_push_uint64( encoder, FD_METRICS_SAMPLE_F_LINK_IDX, link_idx );
     247           0 :   }
     248           0 :   ok &= !fd_metrics_sample_encode( encoder, schema, area, ctx->ns_per_tick );
     249           0 :   ok &= !!fd_pb_submsg_close( encoder );
     250           0 :   ok &= !!fd_pb_submsg_close( encoder );
     251           0 :   FD_TEST( ok );
     252           0 :   fd_circq_resize_back( ctx->circq, fd_pb_encoder_out_sz( encoder ) );
     253           0 : }
     254             : 
     255             : static void
     256             : metrics_sample_sweep( fd_event_tile_t * ctx,
     257             :                       uint              reason,
     258             :                       ulong             slot,
     259           0 :                       ulong             snapct_state ) {
     260           0 :   fd_topo_t const * topo = ctx->topo;
     261           0 :   long now = fd_clock_tile_now( ctx->clock );
     262             : 
     263           0 :   int caught_up = 0;
     264           0 :   ulong replay_idx = fd_topo_find_tile( topo, "replay", 0UL );
     265           0 :   if( FD_LIKELY( replay_idx!=ULONG_MAX ) ) {
     266           0 :     caught_up = !!FD_VOLATILE_CONST( fd_metrics_tile( topo->tiles[ replay_idx ].metrics )[ MIDX( GAUGE, REPLAY, IS_CAUGHT_UP ) ] );
     267           0 :   }
     268             : 
     269           0 :   ctx->metrics_sample_tile_cnt = 0UL;
     270           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) ctx->metrics_sample_tile_cnt += ctx->sample_schema[ i ] && !ctx->tile_shutdown_rendered[ i ];
     271             : 
     272           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     273           0 :     fd_metrics_sample_schema_t const * schema = ctx->sample_schema[ i ];
     274             : 
     275           0 :     fd_topo_tile_t const * tile = &topo->tiles[ i ];
     276           0 :     ulong state = FD_VOLATILE_CONST( fd_metrics_tile( tile->metrics )[ MIDX( GAUGE, TILE, STATE ) ] );
     277           0 :     FD_COMPILER_MFENCE();
     278           0 :     if( FD_UNLIKELY( ctx->tile_shutdown_rendered[ i ] ) ) continue;
     279           0 :     if( FD_LIKELY( schema ) ) metrics_sample_push( ctx, schema, tile, ULONG_MAX, NULL, fd_metrics_tile( tile->metrics ), reason, slot, snapct_state, caught_up, now );
     280             : 
     281           0 :     ulong polled_in_idx = 0UL;
     282           0 :     for( ulong k=0UL; k<tile->in_cnt; k++ ) {
     283           0 :       if( FD_UNLIKELY( !tile->in_link_poll[ k ] ) ) continue;
     284           0 :       fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ k ] ];
     285           0 :       metrics_sample_push( ctx, &fd_metrics_sample_link, tile, polled_in_idx, link, fd_metrics_link_in( tile->metrics, polled_in_idx ), reason, slot, snapct_state, caught_up, now );
     286           0 :       polled_in_idx++;
     287           0 :     }
     288             : 
     289           0 :     if( FD_UNLIKELY( state==FD_METRICS_ENUM_TILE_STATE_V_SHUTDOWN_IDX ) ) ctx->tile_shutdown_rendered[ i ] = 1;
     290           0 :   }
     291             : 
     292           0 :   ctx->metrics_sample_id++;
     293           0 :   ctx->next_poll_deadline = 0L;
     294           0 : }
     295             : 
     296             : static long
     297           0 : next_deadline( fd_event_tile_t * ctx ) {
     298           0 :   long due = fd_long_min( ctx->next_poll_deadline, ctx->metrics_sample_next_nanos );
     299           0 :   if( FD_UNLIKELY( due==LONG_MAX ) ) return LONG_MAX;
     300           0 :   return fd_clock_tile_wallclock_to_tickcount( ctx->clock, due );
     301           0 : }
     302             : 
     303             : static void
     304             : before_credit( fd_event_tile_t *   ctx,
     305             :                fd_stem_context_t * stem,
     306           0 :                int *               charge_busy ) {
     307           0 :   (void)stem;
     308             : 
     309           0 :   ctx->idle_cnt++;
     310           0 :   if( FD_LIKELY( ctx->idle_cnt<2UL*ctx->in_cnt ) ) return;
     311           0 :   ctx->idle_cnt = 0UL;
     312             : 
     313           0 :   int  fired = fd_fseq_query( ctx->waker_fseq )==1UL;
     314           0 :   long now   = fd_clock_tile_now( ctx->clock );
     315             : 
     316           0 :   if( FD_UNLIKELY( now>=ctx->metrics_sample_next_nanos ) ) {
     317           0 :     ctx->metrics_sample_next_nanos = now + METRICS_SAMPLE_INTERVAL_SECS*(long)1e9;
     318           0 :     metrics_sample_sweep( ctx, FD_METRICS_SAMPLE_REASON_PERIODIC, 0UL, ULONG_MAX );
     319           0 :   }
     320             : 
     321           0 :   if( FD_UNLIKELY( fired | ( now>=ctx->next_poll_deadline ) ) ) {
     322           0 :     if( FD_LIKELY( fired ) ) fd_fseq_update( ctx->waker_fseq, 0UL );
     323           0 :     int busy = 0;
     324           0 :     fd_event_client_poll( ctx->client, now, &busy );
     325           0 :     if( FD_LIKELY( fired ) ) fd_waker_client_rearm( ctx->waker_client_idx );
     326           0 :     *charge_busy = busy;
     327           0 :     ctx->next_poll_deadline = busy ? 0L : fd_event_client_next_deadline( ctx->client, now );
     328           0 :   }
     329           0 : }
     330             : 
     331             : static void
     332             : during_frag( fd_event_tile_t * ctx,
     333             :              ulong             in_idx,
     334             :              ulong             seq,
     335             :              ulong             sig,
     336             :              ulong             chunk,
     337             :              ulong             sz,
     338           0 :              ulong             ctl ) {
     339           0 :   (void)seq; (void)ctl;
     340             : 
     341           0 :   fd_event_tile_in_t const * in = &ctx->in[ in_idx ];
     342           0 :   switch( ctx->in_kind[ in_idx ] ) {
     343           0 :     case IN_KIND_SHRED: {
     344           0 :       uchar const * dcache_entry = fd_net_rx_translate_frag( &ctx->in[ in_idx ].net_rx, chunk, ctl, sz );
     345           0 :       ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
     346           0 :       FD_TEST( hdr_sz <= sz ); /* Should be ensured by the net tile */
     347           0 :       fd_udp_hdr_t const * udp_hdr = (fd_udp_hdr_t const *)( dcache_entry + hdr_sz - sizeof(fd_udp_hdr_t) );
     348           0 :       ulong dgram_sz = fd_ulong_min( fd_ulong_sat_sub( fd_ushort_bswap( udp_hdr->net_len ), sizeof(fd_udp_hdr_t) ), sz-hdr_sz );
     349           0 :       ctx->shred_skip = fd_disco_netmux_sig_proto( sig )==DST_PROTO_REPAIR && dgram_sz<=FD_REPAIR_RES_DE_MAX;
     350           0 :       if( FD_UNLIKELY( ctx->shred_skip ) ) break;
     351           0 :       ctx->shred_source_port = fd_ushort_bswap( udp_hdr->net_sport );
     352             :       // TODO: SHOULD BE RELIABLE. MAKE XDP TILE RELIABLE FIRST.
     353           0 :       fd_memcpy( ctx->shred_buf, dcache_entry+hdr_sz, sz-hdr_sz );
     354           0 :       ctx->shred_buf_sz = sz-hdr_sz;
     355           0 :       break;
     356           0 :     }
     357           0 :     case IN_KIND_DEDUP:
     358           0 :     case IN_KIND_GENESI:
     359           0 :     case IN_KIND_IPECHO:
     360           0 :       if( FD_UNLIKELY( chunk<in->chunk0 || chunk>in->wmark || sz>in->mtu ) )
     361           0 :         FD_LOG_CRIT(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, in->chunk0, in->wmark ));
     362           0 :       ctx->chunk = chunk;
     363           0 :       break;
     364           0 :     case IN_KIND_EVENT: {
     365           0 :       ulong event_sz = FD_EVENT_SIG_SZ( sig );
     366           0 :       if( FD_UNLIKELY( chunk<in->chunk0 || chunk>in->wmark || event_sz>in->mtu ) )
     367           0 :         FD_LOG_CRIT(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, event_sz, in->chunk0, in->wmark ));
     368           0 :       fd_memcpy( ctx->event_buf, fd_chunk_to_laddr_const( in->mem, chunk ), event_sz );
     369           0 :       ctx->event_type = FD_EVENT_SIG_TYPE( sig );
     370           0 :       ctx->event_sz   = event_sz;
     371           0 :       break;
     372           0 :     }
     373           0 :     default:
     374           0 :       FD_LOG_CRIT(( "unexpected in_kind %d %lu", ctx->in_kind[ in_idx ], in_idx ));
     375           0 :   }
     376           0 : }
     377             : 
     378             : static void
     379             : after_frag( fd_event_tile_t *   ctx,
     380             :             ulong               in_idx,
     381             :             ulong               seq,
     382             :             ulong               sig,
     383             :             ulong               sz,
     384             :             ulong               tsorig,
     385             :             ulong               tspub,
     386           0 :             fd_stem_context_t * stem ) {
     387           0 :   (void)sz; (void)tsorig; (void)stem;
     388             : 
     389           0 :   switch( ctx->in_kind[ in_idx ] ) {
     390           0 :     case IN_KIND_SHRED: {
     391           0 :       if( FD_UNLIKELY( ctx->shred_skip ) ) break;
     392           0 :       FD_TEST( ctx->shred_buf_sz<=FD_NET_MTU );
     393             :       /* TODO: Currently no way to find a tight bound for the buffer
     394             :          size here, but 4096 is guaranteed to fit since sz<=FD_NET_MTU.
     395             :          Need to have the schema generator spit out max sizes for
     396             :          messages. */
     397           0 :       uchar * buffer = fd_circq_push_back( ctx->circq, 1UL, 4096UL );
     398           0 :       FD_TEST( buffer );
     399             : 
     400           0 :       uint ip_addr = fd_disco_netmux_sig_ip( sig );
     401           0 :       uint source_port = ctx->shred_source_port;
     402           0 :       int protocol;
     403           0 :       switch( fd_disco_netmux_sig_proto( sig ) ) {
     404           0 :         case DST_PROTO_SHRED:
     405           0 :           protocol = 1;
     406           0 :           break;
     407           0 :         case DST_PROTO_REPAIR:
     408           0 :           protocol = 2;
     409           0 :           break;
     410           0 :         default:
     411             :           // TODO: Leader shreds?
     412           0 :           FD_LOG_ERR(( "unexpected proto %lu in sig %lu", fd_disco_netmux_sig_proto( sig ), sig ));
     413           0 :       }
     414             : 
     415           0 :       ulong event_id = fd_event_client_id_reserve( ctx->client );
     416           0 :       long timestamp_nanos = fd_clock_tile_tickcomp_to_wallclock( ctx->clock, tspub );
     417             : 
     418           0 :       fd_pb_encoder_t encoder[1];
     419           0 :       fd_pb_encoder_init( encoder, buffer, 4096UL );
     420             : 
     421           0 :       FD_TEST( ctx->circq->cursor_push_seq );
     422           0 :       fd_pb_push_uint64( encoder, 1U, ctx->circq->cursor_push_seq-1UL );
     423           0 :       fd_pb_push_uint64( encoder, 2U, event_id );
     424           0 :       fd_pb_push_uint64( encoder, 3U, seq ); /* link_seq */
     425           0 :       fd_pb_push_uint64( encoder, 4U, (ulong)timestamp_nanos );
     426             : 
     427           0 :       fd_pb_submsg_open( encoder, 5U ); /* Event */
     428           0 :       fd_pb_submsg_open( encoder, 2U ); /* Shred */
     429           0 :       fd_pb_push_bytes( encoder, 1U, &ip_addr, 4UL );
     430           0 :       fd_pb_push_uint32( encoder, 2U, source_port );
     431           0 :       fd_pb_push_int32( encoder, 3U, protocol );
     432           0 :       fd_pb_push_bytes( encoder, 4U, ctx->shred_buf, ctx->shred_buf_sz );
     433           0 :       fd_pb_submsg_close( encoder );
     434           0 :       fd_pb_submsg_close( encoder );
     435           0 :       fd_circq_resize_back( ctx->circq, fd_pb_encoder_out_sz( encoder ) );
     436           0 :       break;
     437           0 :     }
     438           0 :     case IN_KIND_DEDUP:
     439           0 :       FD_TEST( sz<=FD_TPU_PARSED_MTU );
     440           0 :       uchar * buffer = fd_circq_push_back( ctx->circq, 1UL, EVENT_TXN_BUF_MAX );
     441           0 :       FD_TEST( buffer );
     442             : 
     443           0 :       fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
     444           0 :       FD_TEST( txnm->payload_sz<=FD_TPU_MTU );
     445             : 
     446           0 :       int protocol = 0;
     447           0 :       switch( txnm->source_tpu ) {
     448           0 :         case FD_TXN_M_TPU_SOURCE_QUIC:   protocol = 1; break;
     449           0 :         case FD_TXN_M_TPU_SOURCE_UDP:    protocol = 2; break;
     450           0 :         case FD_TXN_M_TPU_SOURCE_GOSSIP: protocol = 3; break;
     451           0 :         case FD_TXN_M_TPU_SOURCE_BUNDLE: protocol = 4; break;
     452           0 :         case FD_TXN_M_TPU_SOURCE_TXSEND: protocol = 5; break;
     453           0 :         default:
     454           0 :           FD_LOG_ERR(( "unexpected source_tpu %u", txnm->source_tpu ));
     455           0 :       }
     456             : 
     457           0 :       ulong event_id = fd_event_client_id_reserve( ctx->client );
     458           0 :       long timestamp_nanos = fd_clock_tile_tickcomp_to_wallclock( ctx->clock, tspub );
     459             : 
     460           0 :       fd_pb_encoder_t encoder[1];
     461           0 :       fd_pb_encoder_init( encoder, buffer, EVENT_TXN_BUF_MAX );
     462             : 
     463           0 :       FD_TEST( ctx->circq->cursor_push_seq );
     464           0 :       fd_pb_push_uint64( encoder, 1U, ctx->circq->cursor_push_seq-1UL );
     465           0 :       fd_pb_push_uint64( encoder, 2U, event_id );
     466           0 :       fd_pb_push_uint64( encoder, 3U, seq ); /* link_seq */
     467           0 :       fd_pb_push_uint64( encoder, 4U, (ulong)timestamp_nanos );
     468             : 
     469           0 :       fd_pb_submsg_open( encoder, 5U ); /* Event */
     470           0 :       fd_pb_submsg_open( encoder, 1U ); /* Txn */
     471           0 :       fd_pb_push_bytes( encoder, 1U, &txnm->source_ipv4, 4UL );
     472           0 :       fd_pb_push_uint32( encoder, 2U, 0U ); /* TODO: source port .. */
     473           0 :       fd_pb_push_int32( encoder, 3U, protocol );
     474           0 :       fd_pb_push_uint64( encoder, 4U, txnm->block_engine.bundle_id );
     475           0 :       if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) {
     476           0 :         fd_pb_push_uint32( encoder, 5U, (uint)txnm->block_engine.bundle_txn_cnt );
     477           0 :         fd_pb_push_uint32( encoder, 6U, txnm->block_engine.commission );
     478           0 :         fd_pb_push_bytes( encoder, 7U, txnm->block_engine.commission_pubkey, 32UL );
     479           0 :       } else {
     480           0 :         fd_pb_push_uint32( encoder, 5U, 0U );
     481           0 :         fd_pb_push_uint32( encoder, 6U, 0U );
     482           0 :         uchar zero_pubkey[32UL] = {0};
     483           0 :         fd_pb_push_bytes( encoder, 7U, zero_pubkey, 32UL );
     484           0 :       }
     485           0 :       fd_pb_push_bytes( encoder, 8U, fd_txn_m_payload( txnm ), txnm->payload_sz );
     486             : 
     487           0 :       fd_pb_submsg_close( encoder );
     488           0 :       fd_pb_submsg_close( encoder );
     489           0 :       fd_circq_resize_back( ctx->circq, fd_pb_encoder_out_sz( encoder ) );
     490             : 
     491           0 :       break;
     492           0 :     case IN_KIND_GENESI: {
     493           0 :       fd_genesis_meta_t const * genesis_meta = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
     494           0 :       fd_event_client_init_genesis( ctx->client, genesis_meta );
     495           0 :       break;
     496           0 :     }
     497           0 :     case IN_KIND_IPECHO:
     498           0 :       FD_TEST( sig && sig<=USHORT_MAX );
     499           0 :       fd_event_client_init_shred_version( ctx->client, (ushort)sig );
     500           0 :       break;
     501           0 :     case IN_KIND_EVENT: {
     502           0 :       if( FD_UNLIKELY( ctx->event_type==FD_EVENT_TYPE_METRICS_TRIGGER ) ) {
     503           0 :         FD_TEST( ctx->event_sz==sizeof(fd_event_metrics_trigger_t) );
     504           0 :         fd_event_metrics_trigger_t const * t = (fd_event_metrics_trigger_t const *)ctx->event_buf;
     505           0 :         metrics_sample_sweep( ctx, t->reason, t->slot, t->snapct_state );
     506           0 :         break;
     507           0 :       }
     508           0 :       long timestamp_nanos = fd_clock_tile_tickcomp_to_wallclock( ctx->clock, tspub );
     509           0 :       fd_event_serialize_by_type( ctx->event_type, ctx->circq, ctx->client, timestamp_nanos, seq, ctx->event_buf, ctx->event_sz );
     510           0 :       break;
     511           0 :     }
     512           0 :     default:
     513           0 :       FD_LOG_ERR(( "unexpected in_kind %d", ctx->in_kind[ in_idx ] ));
     514           0 :   }
     515             : 
     516           0 :   ctx->next_poll_deadline = 0L;
     517           0 : }
     518             : 
     519             : static void
     520             : privileged_init( fd_topo_t const *      topo,
     521           0 :                  fd_topo_tile_t const * tile ) {
     522           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     523             : 
     524           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     525           0 :   fd_event_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_event_tile_t),  sizeof(fd_event_tile_t) );
     526           0 :   FD_SCRATCH_ALLOC_APPEND( l, fd_event_client_align(),  fd_event_client_footprint( GRPC_BUF_MAX ) );
     527           0 :   FD_SCRATCH_ALLOC_APPEND( l, fd_circq_align(),         fd_circq_footprint( EVENT_CIRCQ_SZ )      );
     528             : 
     529           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     530           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     531           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     532             : 
     533           0 :   if( FD_UNLIKELY( !strcmp( tile->event.identity_key_path, "" ) ) ) FD_LOG_ERR(( "identity_key_path not set" ));
     534           0 :   const uchar * identity_key = fd_keyload_load( tile->event.identity_key_path, /* pubkey only: */ 1 );
     535           0 :   fd_memcpy( ctx->identity_pubkey, identity_key, 32UL );
     536             : 
     537           0 :   FD_TEST( fd_rng_secure( &ctx->seed, 8UL ) );
     538           0 :   FD_TEST( fd_rng_secure( &ctx->instance_id, 8UL ) );
     539             : 
     540           0 :   FD_LOG_INFO(( "event instance_id=%lu", ctx->instance_id ));
     541             : 
     542           0 : #define FD_EVENT_ID_SEED 0x812CAFEBABEFEEE0UL
     543             : 
     544           0 :   char _boot_id[ 36 ];
     545           0 :   int boot_id_fd = open( "/proc/sys/kernel/random/boot_id", O_RDONLY );
     546           0 :   if( FD_UNLIKELY( -1==boot_id_fd ) ) FD_LOG_ERR(( "open(/proc/sys/kernel/random/boot_id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     547           0 :   if( FD_UNLIKELY( 36UL!=read( boot_id_fd, _boot_id, 36UL ) ) ) FD_LOG_ERR(( "read(/proc/sys/kernel/random/boot_id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     548           0 :   if( FD_UNLIKELY( -1==close( boot_id_fd ) ) ) FD_LOG_ERR(( "close(/proc/sys/kernel/random/boot_id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     549             : 
     550           0 :   ctx->boot_id = fd_hash( FD_EVENT_ID_SEED, _boot_id, 36UL );
     551             : 
     552           0 :   char _machine_id[ 32 ];
     553           0 :   int machine_id_fd = open( "/etc/machine-id", O_RDONLY );
     554           0 :   if( FD_UNLIKELY( -1==machine_id_fd ) ) FD_LOG_ERR(( "open(/etc/machine-id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     555           0 :   if( FD_UNLIKELY( 32UL!=read( machine_id_fd, _machine_id, 32UL ) ) ) FD_LOG_ERR(( "read(/etc/machine-id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     556           0 :   if( FD_UNLIKELY( -1==close( machine_id_fd ) ) ) FD_LOG_ERR(( "close(/etc/machine-id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     557             : 
     558           0 :   ctx->machine_id = fd_hash( FD_EVENT_ID_SEED, _machine_id, 32UL );
     559             : 
     560           0 :   if( FD_UNLIKELY( !fd_netdb_open_fds( ctx->netdb_fds ) ) ) {
     561           0 :     FD_LOG_ERR(( "fd_netdb_open_fds failed" ));
     562           0 :   }
     563             : 
     564           0 :   fd_boot_report_collect( ctx->boot_report, topo, tile );
     565             : 
     566             :   /* Detect TLS from the URL scheme */
     567           0 :   fd_url_t url[ 1UL ];
     568           0 :   ushort   port;
     569           0 :   _Bool    is_ssl = 0;
     570           0 :   if( FD_UNLIKELY( fd_url_parse_endpoint( url, tile->event.url, strlen( tile->event.url ), &port, &is_ssl, "[tiles.event.url]" ) ) ) {
     571           0 :     FD_LOG_ERR(( "Could not parse [tiles.event.url]" ));
     572           0 :   }
     573           0 :   ctx->use_tls = is_ssl;
     574             : 
     575           0 :   if( ctx->use_tls ) {
     576             :     /* Load system CA certificates.  Must happen here, while the tile can
     577             :        still open files.  The TLS config itself lives in the event
     578             :        client. */
     579           0 :     long ca_cnt = fd_x509_ca_store_load_system( ctx->ca_store );
     580           0 :     if( FD_UNLIKELY( ca_cnt<0L ) ) FD_LOG_ERR(( "No CA certificate bundle found, cannot verify the event server certificate" ));
     581           0 :   }
     582           0 : }
     583             : 
     584             : static int
     585             : link_is_event_report( fd_topo_t const * topo,
     586           0 :                       ulong             link_id ) {
     587           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     588           0 :     if( FD_UNLIKELY( topo->tiles[ i ].event_link_id==link_id ) ) return 1;
     589           0 :   }
     590           0 :   return !strcmp( topo->links[ link_id ].name, "replay_diff" );
     591           0 : }
     592             : 
     593             : static void
     594             : unprivileged_init( fd_topo_t const *      topo,
     595           0 :                    fd_topo_tile_t const * tile ) {
     596           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     597             : 
     598           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     599           0 :   fd_event_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_event_tile_t), sizeof(fd_event_tile_t)                   );
     600           0 :   void * _event_client  = FD_SCRATCH_ALLOC_APPEND( l, fd_event_client_align(),  fd_event_client_footprint( GRPC_BUF_MAX ) );
     601           0 :   void * _circq         = FD_SCRATCH_ALLOC_APPEND( l, fd_circq_align(),         fd_circq_footprint( EVENT_CIRCQ_SZ )      );
     602             : 
     603           0 :   ulong sign_in_idx  = fd_topo_find_tile_in_link ( topo, tile, "sign_event", tile->kind_id );
     604           0 :   ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "event_sign", tile->kind_id );
     605           0 :   FD_TEST( sign_in_idx!=ULONG_MAX );
     606           0 :   fd_topo_link_t const * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
     607           0 :   fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
     608             : 
     609           0 :   fd_sleep_t * sleep = NULL;
     610           0 :   if( FD_UNLIKELY( topo->sleep_obj_id!=ULONG_MAX ) ) {
     611           0 :     sleep = fd_sleep_join( fd_topo_obj_laddr( topo, topo->sleep_obj_id ) );
     612           0 :     FD_TEST( sleep );
     613           0 :   }
     614             : 
     615           0 :   if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
     616           0 :           sign_out->mcache,
     617           0 :           sign_out->dcache,
     618           0 :           sign_in->mcache,
     619           0 :           sign_in->dcache,
     620           0 :           sign_out->mtu,
     621           0 :           sign_in->mtu,
     622           0 :           sleep,
     623           0 :           sign_out->id,
     624           0 :           fd_topo_find_link_consumer( topo, sign_out ) ) ) ) ) {
     625           0 :     FD_LOG_ERR(( "failed to construct keyguard" ));
     626           0 :   }
     627             : 
     628           0 :   FD_TEST( fd_rng_join( fd_rng_new( ctx->rng, 0U, ctx->seed ) ) );
     629             : 
     630           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
     631           0 :   FD_TEST( ctx->keyswitch );
     632             : 
     633           0 :   ctx->circq = fd_circq_join( fd_circq_new( _circq, EVENT_CIRCQ_SZ ) );
     634           0 :   FD_TEST( ctx->circq );
     635             : 
     636           0 :   ctx->waker_client_idx = tile->waker_client_idx;
     637           0 :   FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
     638           0 :   ctx->waker_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->waker_fseq_obj_id ) );
     639           0 :   FD_TEST( ctx->waker_fseq );
     640             : 
     641           0 :   ctx->client = fd_event_client_join( fd_event_client_new( _event_client,
     642           0 :                                                            ctx->keyguard_client,
     643           0 :                                                            ctx->rng,
     644           0 :                                                            ctx->circq,
     645           0 :                                                            FD_WAKER_INNER_FD( ctx->waker_client_idx ),
     646           0 :                                                            2*(1UL<<20UL) /* 2 MiB */,
     647           0 :                                                            tile->event.url,
     648           0 :                                                            ctx->identity_pubkey,
     649           0 :                                                            fd_version_cstr,
     650           0 :                                                            fd_commit_ref_cstr,
     651           0 :                                                            tile->event.action,
     652           0 :                                                            ctx->instance_id,
     653           0 :                                                            ctx->boot_id,
     654           0 :                                                            ctx->machine_id,
     655           0 :                                                            GRPC_BUF_MAX,
     656           0 :                                                            ctx->use_tls,
     657           0 :                                                            ctx->ca_store,
     658           0 :                                                            tile->event.tx_rate_mbps ) );
     659           0 :   FD_TEST( ctx->client );
     660           0 :   ctx->next_poll_deadline = 0L;
     661             : 
     662           0 :   ctx->topo = topo;
     663           0 :   fd_memset( ctx->tile_shutdown_rendered, 0, sizeof(ctx->tile_shutdown_rendered) );
     664             : 
     665           0 :   ctx->metrics_sample_id = 0UL;
     666           0 :   ctx->ns_per_tick = 1.0/fd_tempo_tick_per_ns( NULL );
     667           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     668           0 :     ctx->sample_schema[ i ] = NULL;
     669           0 :     for( fd_metrics_sample_schema_t const * s=fd_metrics_sample_tiles; s->tile; s++ ) {
     670           0 :       if( !strcmp( s->tile, topo->tiles[ i ].name ) ) {
     671           0 :         ctx->sample_schema[ i ] = s;
     672           0 :         break;
     673           0 :       }
     674           0 :     }
     675           0 :     if( FD_LIKELY( ctx->sample_schema[ i ] ) ) continue;
     676             : 
     677           0 :     int skip = 0;
     678           0 :     for( char const * const * t=fd_metrics_sample_skip; *t; t++ ) skip |= !strcmp( *t, topo->tiles[ i ].name );
     679           0 :     if( FD_UNLIKELY( !skip ) ) FD_LOG_WARNING(( "tile %s has no metrics schema, only its in-links are reported", topo->tiles[ i ].name ));
     680           0 :   }
     681             : 
     682           0 :   ctx->idle_cnt = 0UL;
     683             : 
     684           0 :   FD_TEST( tile->in_cnt<=sizeof(ctx->in_kind)/sizeof(ctx->in_kind[0]) );
     685           0 :   int have_genesi = 0;
     686           0 :   int have_ipecho = 0;
     687           0 :   ulong polled_in_idx = 0UL;
     688           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     689           0 :     if( FD_UNLIKELY( !tile->in_link_poll[ i ] ) ) continue;
     690             : 
     691           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     692           0 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     693             : 
     694           0 :     if( FD_LIKELY( !strcmp( link->name, "net_shred"         ) ) ) {
     695           0 :       fd_net_rx_bounds_init( &ctx->in[ polled_in_idx ].net_rx, link->dcache );
     696           0 :       ctx->in_kind[ polled_in_idx ] = IN_KIND_SHRED;
     697           0 :       polled_in_idx++;
     698           0 :       continue; /* only net_rx needs to be set in this case. */
     699           0 :     }
     700           0 :     else if( FD_LIKELY( !strcmp( link->name, "dedup_resolv" ) ) ) ctx->in_kind[ polled_in_idx ] = IN_KIND_DEDUP;
     701           0 :     else if( FD_LIKELY( !strcmp( link->name, "genesi_out"   ) ) ) { ctx->in_kind[ polled_in_idx ] = IN_KIND_GENESI; have_genesi = 1; }
     702           0 :     else if( FD_LIKELY( !strcmp( link->name, "ipecho_out"   ) ) ) { ctx->in_kind[ polled_in_idx ] = IN_KIND_IPECHO; have_ipecho = 1; }
     703           0 :     else if( FD_LIKELY( link_is_event_report( topo, link->id ) ) ) {
     704           0 :       ctx->in_kind[ polled_in_idx ] = IN_KIND_EVENT;
     705           0 :       FD_TEST( link->mtu<=sizeof(ctx->event_buf) );
     706           0 :     }
     707           0 :     else FD_LOG_ERR(( "event tile has unexpected input link %lu %s", i, link->name ));
     708             : 
     709           0 :     ctx->in[ polled_in_idx ].mem = link_wksp->wksp;
     710           0 :     ctx->in[ polled_in_idx ].mtu = link->mtu;
     711           0 :     if( FD_UNLIKELY( ctx->in[ polled_in_idx ].mtu ) ) {
     712           0 :       ctx->in[ polled_in_idx ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ polled_in_idx ].mem, link->dcache );
     713           0 :       ctx->in[ polled_in_idx ].wmark  = fd_dcache_compact_wmark ( ctx->in[ polled_in_idx ].mem, link->dcache, link->mtu );
     714           0 :     } else {
     715           0 :       ctx->in[ polled_in_idx ].chunk0 = 0UL;
     716           0 :       ctx->in[ polled_in_idx ].wmark  = 0UL;
     717           0 :     }
     718           0 :     polled_in_idx++;
     719           0 :   }
     720           0 :   ctx->in_cnt = polled_in_idx;
     721             : 
     722           0 :   fd_boot_report_publish( ctx->boot_report, topo, ctx->circq, ctx->client );
     723             : 
     724           0 :   if( FD_UNLIKELY( !have_genesi ) ) {
     725           0 :     fd_genesis_meta_t meta[1]; fd_memset( meta, 0, sizeof(meta) );
     726           0 :     fd_memcpy( meta->genesis_hash.uc, tile->event.genesis_hash, 32UL );
     727           0 :     fd_event_client_init_genesis( ctx->client, meta );
     728           0 :   }
     729           0 :   if( FD_UNLIKELY( !have_ipecho ) ) {
     730           0 :     fd_event_client_init_shred_version( ctx->client, tile->event.shred_version );
     731           0 :   }
     732             : 
     733           0 :   fd_clock_tile_init( ctx->clock );
     734           0 :   ctx->metrics_sample_next_nanos = fd_clock_tile_now( ctx->clock ) + METRICS_SAMPLE_INTERVAL_SECS*(long)1e9;
     735             : 
     736           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     737           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     738           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     739           0 : }
     740             : 
     741             : static ulong
     742             : populate_allowed_seccomp( fd_topo_t const *      topo,
     743             :                           fd_topo_tile_t const * tile,
     744             :                           ulong                  out_cnt,
     745           0 :                           struct sock_filter *   out ) {
     746           0 :   fd_event_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     747             : 
     748           0 :   uint epoll_inner_fd = (uint)FD_WAKER_INNER_FD( tile->waker_client_idx );
     749           0 :   uint epoll_outer_fd = (uint)FD_WAKER_OUTER_FD;
     750             : 
     751           0 :   populate_sock_filter_policy_fd_event_tile(
     752           0 :       out_cnt, out,
     753           0 :       (uint)fd_log_private_logfile_fd(),
     754           0 :       (uint)ctx->netdb_fds->etc_hosts,
     755           0 :       (uint)ctx->netdb_fds->etc_resolv_conf,
     756           0 :       epoll_inner_fd,
     757           0 :       epoll_outer_fd );
     758           0 :   return sock_filter_policy_fd_event_tile_instr_cnt;
     759           0 : }
     760             : 
     761             : static ulong
     762             : populate_allowed_fds( fd_topo_t const *      topo,
     763             :                       fd_topo_tile_t const * tile,
     764             :                       ulong                  out_fds_cnt,
     765           0 :                       int *                  out_fds ) {
     766           0 :   fd_event_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     767             : 
     768           0 :   if( FD_UNLIKELY( out_fds_cnt<6UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     769             : 
     770           0 :   ulong out_cnt = 0;
     771           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     772           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     773           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     774           0 :   if( FD_LIKELY( ctx->netdb_fds->etc_hosts >= 0 ) )
     775           0 :     out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_hosts;
     776           0 :   out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_resolv_conf;
     777           0 :   out_fds[ out_cnt++ ] = FD_WAKER_OUTER_FD;                           /* waker outer epoll fd (rearm) */
     778           0 :   out_fds[ out_cnt++ ] = FD_WAKER_INNER_FD( tile->waker_client_idx ); /* waker inner epoll fd */
     779           0 :   return out_cnt;
     780           0 : }
     781             : 
     782             : static void
     783           0 : during_housekeeping( fd_event_tile_t * ctx ) {
     784           0 :   if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) {
     785           0 :     fd_clock_tile_recal( ctx->clock );
     786           0 :   }
     787             : 
     788           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     789           0 :     FD_LOG_DEBUG(( "keyswitch: switching identity" ));
     790           0 :     memcpy( ctx->identity_pubkey, ctx->keyswitch->bytes, 32UL );
     791           0 :     fd_event_client_set_identity( ctx->client, ctx->identity_pubkey );
     792           0 :     ctx->next_poll_deadline = 0L;
     793           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     794           0 :   }
     795           0 : }
     796             : 
     797           0 : #define STEM_BURST (1UL)
     798           0 : #define STEM_LAZY ((long)10e6) /* 10ms */
     799             : 
     800           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_event_tile_t
     801           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_event_tile_t)
     802             : 
     803           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     804           0 : #define STEM_CALLBACK_NEXT_DEADLINE       next_deadline
     805           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     806           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     807           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     808           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     809             : 
     810             : #include "../stem/fd_stem.c"
     811             : 
     812             : fd_topo_run_tile_t fd_tile_event = {
     813             :   .name                     = "event",
     814             :   .rlimit_file_cnt          = 5UL, /* stderr, logfile, /etc/hosts, /etc/resolv.conf, and socket to the server */
     815             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     816             :   .populate_allowed_fds     = populate_allowed_fds,
     817             :   .scratch_align            = scratch_align,
     818             :   .scratch_footprint        = scratch_footprint,
     819             :   .privileged_init          = privileged_init,
     820             :   .unprivileged_init        = unprivileged_init,
     821             :   .run                      = stem_run,
     822             :   .keep_host_networking     = 1,
     823             :   .allow_connect            = 1,
     824             : };

Generated by: LCOV version 1.14