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

Generated by: LCOV version 1.14