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

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_bundle_tile_private.h"
       3             : #include "fd_bundle_tile.h"
       4             : #include "../fd_txn_m.h"
       5             : #include "../metrics/fd_metrics.h"
       6             : #include "../topo/fd_topo.h"
       7             : #include "../keyguard/fd_keyload.h"
       8             : #include "../waker/fd_waker.h"
       9             : #include "../../waltz/http/fd_url.h"
      10             : #include "../../ballet/hex/fd_hex.h"
      11             : #include <errno.h>
      12             : 
      13             : #include <dirent.h> /* opendir */
      14             : #include <stdio.h> /* snprintf */
      15             : #include <fcntl.h> /* F_SETFL */
      16             : #include <unistd.h> /* close */
      17             : #include <sys/mman.h> /* PROT_READ (seccomp) */
      18             : #include <sys/uio.h> /* writev */
      19             : #include <netinet/in.h> /* AF_INET */
      20             : #include <netinet/tcp.h> /* TCP_FASTOPEN_CONNECT (seccomp) */
      21             : #include "../../waltz/resolv/fd_netdb.h"
      22             : #include "../../discof/replay/fd_replay_tile.h"
      23             : 
      24             : #include <linux/futex.h>
      25             : #include "generated/fd_bundle_tile_seccomp.h"
      26             : 
      27           0 : #define IN_KIND_REPLAY_OUT (1)
      28             : 
      29           0 : #define STEM_BURST (5UL)
      30             : 
      31             : /* Raw state values exported by the metrics (states= enums) */
      32             : FD_STATIC_ASSERT( FD_BUNDLE_STATE_DISCONNECTED==FD_METRICS_ENUM_BUNDLE_CONN_STATE_V_DISCONNECTED_IDX, metrics_state );
      33             : FD_STATIC_ASSERT( FD_BUNDLE_STATE_CONNECTING==FD_METRICS_ENUM_BUNDLE_CONN_STATE_V_CONNECTING_IDX, metrics_state );
      34             : FD_STATIC_ASSERT( FD_BUNDLE_STATE_CONNECTED==FD_METRICS_ENUM_BUNDLE_CONN_STATE_V_CONNECTED_IDX, metrics_state );
      35             : FD_STATIC_ASSERT( FD_BUNDLE_STATE_SLEEPING==FD_METRICS_ENUM_BUNDLE_CONN_STATE_V_SLEEPING_IDX, metrics_state );
      36             : 
      37             : /* hysteresis thresholds to avoid bouncing (e.g. during forks) */
      38           0 : #define FD_BUNDLE_SLEEP_THRESHOLD_SLOTS   (450UL)
      39           0 : #define FD_BUNDLE_WAKE_THRESHOLD_SLOTS    (400UL)
      40           0 : #define FD_BUNDLE_SLEEP_CHECK_INTERVAL_NS ((long)5e9)
      41             : 
      42             : FD_FN_CONST static ulong
      43           0 : scratch_align( void ) {
      44           0 :   return fd_ulong_max( alignof(fd_bundle_tile_t), fd_grpc_client_align() );
      45           0 : }
      46             : 
      47             : FD_FN_CONST static ulong
      48           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      49           0 :   ulong l = FD_LAYOUT_INIT;
      50           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_bundle_tile_t), sizeof(fd_bundle_tile_t)                        );
      51           0 :   l = FD_LAYOUT_APPEND( l, fd_grpc_client_align(),    fd_grpc_client_footprint( tile->bundle.buf_sz ) );
      52           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      53           0 : }
      54             : 
      55             : static void
      56           0 : fd_bundle_tile_maybe_sleep( fd_bundle_tile_t * ctx, long now_ns ) {
      57           0 :   if( FD_UNLIKELY( !ctx->replay_in.mem ) ) return;
      58           0 :   if( FD_LIKELY( now_ns < ctx->sleep_check_ns ) ) return;
      59           0 :   ctx->sleep_check_ns = now_ns + FD_BUNDLE_SLEEP_CHECK_INTERVAL_NS;
      60             : 
      61           0 :   ulong next_leader_slot = ctx->next_leader_slot;
      62           0 :   ulong reset_slot       = ctx->reset_slot;
      63             : 
      64             :   /* Either don't know the leader schedule yet or have no upcoming
      65             :      leader slots.  Sleep. */
      66           0 :   if( FD_UNLIKELY( next_leader_slot==ULONG_MAX || reset_slot==ULONG_MAX ) ) {
      67           0 :     if( !ctx->sleep_mode ) {
      68           0 :       ctx->sleep_mode = 1;
      69           0 :       FD_LOG_INFO(( "Bundle tile entering sleep mode: no upcoming leader slots" ));
      70           0 :     }
      71           0 :     return;
      72           0 :   }
      73             : 
      74           0 :   ulong slots_until_leader = fd_ulong_sat_sub( next_leader_slot, reset_slot );
      75             : 
      76           0 :   if( ctx->sleep_mode ) {
      77           0 :     if( slots_until_leader <= FD_BUNDLE_WAKE_THRESHOLD_SLOTS ) {
      78           0 :       ctx->sleep_mode = 0;
      79           0 :       ctx->next_step_deadline = 0L;
      80           0 :       ctx->last_bundle_status_log_nanos = now_ns;
      81           0 :       FD_LOG_INFO(( "Bundle tile waking up: next leader slot %lu (~%lu slots away)", next_leader_slot, slots_until_leader ));
      82           0 :     }
      83           0 :   } else {
      84             :     /* reset_slot stays at/near next_leader_slot throughout a leader
      85             :        rotation and only jumps to the next rotation after leadership
      86             :        ends, so this cannot trigger mid-rotation. */
      87           0 :     if( slots_until_leader > FD_BUNDLE_SLEEP_THRESHOLD_SLOTS ) {
      88           0 :       ctx->sleep_mode = 1;
      89           0 :       FD_LOG_INFO(( "Bundle tile entering sleep mode: next leader slot %lu (~%lu slots away)", next_leader_slot, slots_until_leader ));
      90           0 :     }
      91           0 :   }
      92           0 : }
      93             : 
      94             : static inline void
      95           0 : metrics_write( fd_bundle_tile_t * ctx ) {
      96           0 :   FD_MCNT_SET  ( BUNDLE, TXN_RX,                    ctx->metrics.txn_received_cnt              );
      97           0 :   FD_MCNT_SET  ( BUNDLE, BUNDLE_RX,                 ctx->metrics.bundle_received_cnt           );
      98           0 :   FD_MCNT_SET  ( BUNDLE, STANDALONE_TXN_RX,         ctx->metrics.packet_received_cnt           );
      99           0 :   FD_MCNT_SET  ( BUNDLE, PROTOBUF_RX_BYTES,         ctx->metrics.proto_received_bytes          );
     100           0 :   FD_MCNT_SET  ( BUNDLE, PING_ACKED,                ctx->metrics.ping_ack_cnt                  );
     101           0 :   FD_MCNT_SET  ( BUNDLE, CLIENT_FAILED_DECODE,      ctx->metrics.decode_fail_cnt               );
     102           0 :   FD_MCNT_SET  ( BUNDLE, CLIENT_FAILED_TRANSPORT,   ctx->metrics.transport_fail_cnt            );
     103           0 :   FD_MCNT_SET  ( BUNDLE, CLIENT_FAILED_NO_FEE_INFO, ctx->metrics.missing_builder_info_fail_cnt );
     104           0 :   FD_MCNT_SET  ( BUNDLE, TXN_BUFFER_FULL_DROPPED,   ctx->metrics.backpressure_drop_cnt         );
     105             : 
     106           0 :   FD_MGAUGE_SET( BUNDLE, LAST_RTT_NANOS,      (ulong)ctx->rtt->latest_rtt   );
     107           0 :   FD_MGAUGE_SET( BUNDLE, RTT_SMOOTHED_NANOS,  (ulong)ctx->rtt->smoothed_rtt );
     108           0 :   FD_MGAUGE_SET( BUNDLE, RTT_DEVIATION_NANOS, (ulong)ctx->rtt->var_rtt      );
     109             : 
     110           0 :   FD_MHIST_COPY( BUNDLE, MESSAGE_RX_DELAY_NANOS, ctx->metrics.msg_rx_delay );
     111             : 
     112           0 :   fd_wksp_t * wksp = fd_wksp_containing( ctx );
     113           0 :   fd_wksp_usage_t usage[1];
     114           0 :   ulong const free_tag = 0UL;
     115           0 :   if( FD_UNLIKELY( !fd_wksp_usage( wksp, &free_tag, 1UL, usage ) ) ) {
     116           0 :     FD_LOG_ERR(( "fd_wksp_usage failed" )); /* unreachable */
     117           0 :   }
     118           0 :   FD_MGAUGE_SET( BUNDLE, HEAP_CAPACITY_BYTES, usage->total_sz );
     119           0 :   FD_MGAUGE_SET( BUNDLE, HEAP_FREE_BYTES,     usage->free_sz  );
     120             : 
     121           0 :   int status = fd_bundle_client_status( ctx );
     122           0 :   ulong state = (ulong)status;
     123           0 :   if( FD_UNLIKELY( ctx->sleep_mode ) ) state = FD_BUNDLE_STATE_SLEEPING;
     124             : 
     125           0 :   FD_MGAUGE_SET( BUNDLE, CONN_STATE, state );
     126           0 :   ctx->bundle_status_recent = (uchar)state;
     127           0 : }
     128             : 
     129             : void
     130           0 : fd_bundle_tile_housekeeping( fd_bundle_tile_t * ctx ) {
     131           0 :   long log_interval_ns = (long)30e9;
     132           0 :   int  status          = fd_bundle_client_status( ctx );
     133           0 :   long log_next_ns     = ctx->last_bundle_status_log_nanos + log_interval_ns;
     134           0 :   long now_ns          = fd_log_wallclock();
     135             : 
     136           0 :   if( FD_UNLIKELY( !ctx->sleep_mode && status!=FD_BUNDLE_STATE_CONNECTED && now_ns>log_next_ns ) ) {
     137           0 :     FD_LOG_WARNING(( "No bundle server connection in the last %ld seconds", log_interval_ns/(long)1e9 ) );
     138           0 :     ctx->last_bundle_status_log_nanos = now_ns;
     139           0 :   }
     140             : 
     141           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     142           0 :     if( ctx->tcp_sock>=0 ) fd_bundle_client_reset( ctx );
     143           0 :     ctx->halt_signing = 1;
     144           0 :     fd_memcpy( ctx->auther.pubkey, ctx->keyswitch->bytes, 32UL );
     145           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     146           0 :   }
     147             : 
     148           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_UNHALT_PENDING ) ) {
     149           0 :     ctx->defer_reset        = 1;
     150           0 :     ctx->sleep_check_ns     = 0;
     151           0 :     ctx->halt_signing       = 0;
     152           0 :     ctx->next_step_deadline = 0L; /* the socket was closed outside a step: step now */
     153           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     154           0 :   }
     155             : 
     156           0 :   fd_bundle_tile_maybe_sleep( ctx, now_ns );
     157           0 : }
     158             : 
     159             : static void
     160             : fd_bundle_tile_publish_block_engine_update(
     161             :     fd_bundle_tile_t *  ctx,
     162             :     fd_stem_context_t * stem
     163           0 : ) {
     164           0 :   fd_bundle_block_engine_update_t * update =
     165           0 :       fd_chunk_to_laddr( ctx->plugin_out.mem, ctx->plugin_out.chunk );
     166           0 :   memset( update, 0, sizeof(fd_bundle_block_engine_update_t) );
     167             : 
     168           0 :   strncpy( update->name, "jito", sizeof(update->name) );
     169             : 
     170           0 :   FD_TEST( fd_cstr_printf_check( update->url, sizeof(update->url), NULL,
     171           0 :                                 "%s://%.*s:%u",
     172           0 :                                 ctx->is_ssl ? "https" : "http",
     173           0 :                                 (int)ctx->server_fqdn_len,
     174           0 :                                 ctx->server_fqdn,
     175           0 :                                 ctx->server_tcp_port ) );
     176             : 
     177             :   /* Format IPv4 string */
     178           0 :   snprintf( update->ip_cstr, sizeof(update->ip_cstr),
     179           0 :             FD_IP4_ADDR_FMT,
     180           0 :             FD_IP4_ADDR_FMT_ARGS( ctx->server_ip4_addr ) );
     181             : 
     182           0 :   ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_bundle_now( ctx ) );
     183           0 :   fd_stem_publish(
     184           0 :       stem,
     185           0 :       ctx->plugin_out.idx,
     186           0 :       (ulong)ctx->bundle_status_recent,
     187           0 :       ctx->plugin_out.chunk,
     188           0 :       sizeof(fd_bundle_block_engine_update_t),
     189           0 :       0UL, /* ctl */
     190           0 :       0UL, /* seq */
     191           0 :       tspub
     192           0 :   );
     193           0 :   ctx->plugin_out.chunk = fd_dcache_compact_next( ctx->plugin_out.chunk, sizeof(fd_bundle_block_engine_update_t), ctx->plugin_out.chunk0, ctx->plugin_out.wmark );
     194           0 : }
     195             : 
     196             : static void
     197             : during_frag( fd_bundle_tile_t * ctx,
     198             :              ulong              in_idx,
     199             :              ulong              seq    FD_PARAM_UNUSED,
     200             :              ulong              sig,
     201             :              ulong              chunk,
     202             :              ulong              sz     FD_PARAM_UNUSED,
     203           0 :              ulong              ctl    FD_PARAM_UNUSED ) {
     204             : 
     205           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY_OUT ) ) {
     206           0 :     if( FD_LIKELY( sig!=REPLAY_SIG_BECAME_LEADER && sig!=REPLAY_SIG_NEXT_LEADER ) ) return;
     207           0 :     ulong want_sz = sig==REPLAY_SIG_BECAME_LEADER ? sizeof(fd_became_leader_t) : sizeof(fd_replay_next_leader_t);
     208           0 :     if( FD_UNLIKELY( chunk<ctx->replay_in.chunk0 || chunk>ctx->replay_in.wmark || sz!=want_sz ) )
     209           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->replay_in.chunk0, ctx->replay_in.wmark ));
     210             : 
     211           0 :     if( sig==REPLAY_SIG_BECAME_LEADER ) {
     212             :       /* Becoming leader resets to the parent of the leader slot. */
     213           0 :       fd_became_leader_t const * became_leader = fd_chunk_to_laddr_const( ctx->replay_in.mem, chunk );
     214           0 :       ctx->next_leader_slot_staged = became_leader->slot;
     215           0 :       ctx->reset_slot_staged       = became_leader->parent_slot;
     216           0 :     } else {
     217           0 :       fd_replay_next_leader_t const * next_leader = fd_chunk_to_laddr_const( ctx->replay_in.mem, chunk );
     218           0 :       ctx->next_leader_slot_staged = next_leader->next_leader_slot;
     219           0 :       ctx->reset_slot_staged       = next_leader->slot;
     220           0 :     }
     221           0 :   }
     222           0 : }
     223             : 
     224             : static void
     225             : after_frag( fd_bundle_tile_t *  ctx,
     226             :             ulong               in_idx,
     227             :             ulong               seq    FD_PARAM_UNUSED,
     228             :             ulong               sig,
     229             :             ulong               sz     FD_PARAM_UNUSED,
     230             :             ulong               tsorig FD_PARAM_UNUSED,
     231             :             ulong               tspub  FD_PARAM_UNUSED,
     232           0 :             fd_stem_context_t * stem   FD_PARAM_UNUSED ) {
     233             : 
     234           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY_OUT ) ) {
     235           0 :     if( FD_LIKELY( sig!=REPLAY_SIG_BECAME_LEADER && sig!=REPLAY_SIG_NEXT_LEADER ) ) return;
     236           0 :     ctx->next_leader_slot = ctx->next_leader_slot_staged;
     237           0 :     ctx->reset_slot       = ctx->reset_slot_staged;
     238           0 :   }
     239           0 : }
     240             : 
     241             : static long
     242           0 : next_deadline( fd_bundle_tile_t * ctx ) {
     243           0 :   if( FD_UNLIKELY( ctx->halt_signing || ctx->sleep_mode || ctx->next_step_deadline==LONG_MAX ) ) return LONG_MAX;
     244           0 :   return fd_clock_tile_wallclock_to_tickcount( ctx->clock, ctx->next_step_deadline );
     245           0 : }
     246             : 
     247             : static void
     248             : before_credit( fd_bundle_tile_t *  ctx,
     249             :                fd_stem_context_t * stem,
     250           0 :                int *               charge_busy ) {
     251           0 :   if( FD_UNLIKELY( !ctx->stem ) ) {
     252           0 :     ctx->stem = stem;
     253           0 :   }
     254             : 
     255           0 :   if( FD_UNLIKELY( ctx->halt_signing || ctx->sleep_mode ) ) {
     256           0 :     if( ctx->sleep_mode && ctx->tcp_sock>=0 ) {
     257           0 :       fd_bundle_client_reset( ctx );
     258             :       /* Override backoff so we don't treat this as an error */
     259           0 :       ctx->backoff_until = 0;
     260           0 :       ctx->backoff_iter  = 0;
     261           0 :     }
     262             :     /* The socket is closed so any wake (even one that raced the close)
     263             :        is stale: drain it or the stem never parks, and rearm so the next
     264             :        connection can wake us. */
     265           0 :     if( FD_UNLIKELY( fd_fseq_query( ctx->waker_fseq )==1UL ) ) {
     266           0 :       fd_fseq_update( ctx->waker_fseq, 0UL );
     267           0 :       fd_waker_client_rearm( ctx->waker_client_idx );
     268           0 :     }
     269           0 :     return;
     270           0 :   }
     271             : 
     272           0 :   int  fired = fd_fseq_query( ctx->waker_fseq )==1UL;
     273           0 :   long now   = fd_bundle_now( ctx );
     274           0 :   if( FD_UNLIKELY( fired || now>=ctx->next_step_deadline ) ) {
     275           0 :     if( FD_LIKELY( fired ) ) fd_fseq_update( ctx->waker_fseq, 0UL );
     276           0 :     int busy = 0;
     277           0 :     fd_bundle_client_step( ctx, &busy );
     278           0 :     if( FD_LIKELY( fired ) ) fd_waker_client_rearm( ctx->waker_client_idx );
     279           0 :     *charge_busy = busy;
     280             :     /* A step that made progress may have more buffered work: re-step
     281             :        immediately. */
     282           0 :     ctx->next_step_deadline = busy ? 0L : fd_bundle_client_next_deadline( ctx, fd_bundle_now( ctx ) );
     283           0 :   }
     284           0 : }
     285             : 
     286             : static void
     287             : after_credit( fd_bundle_tile_t *  ctx,
     288             :               fd_stem_context_t * stem,
     289             :               int *               opt_poll_in FD_PARAM_UNUSED,
     290           0 :               int *               charge_busy ) {
     291           0 :   if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) fd_clock_tile_recal( ctx->clock );
     292             : 
     293           0 :   if( ctx->plugin_out.mem ) {
     294           0 :     if( FD_UNLIKELY( ctx->bundle_status_recent != ctx->bundle_status_plugin ) ) {
     295           0 :       fd_bundle_tile_publish_block_engine_update( ctx, stem );
     296           0 :       ctx->bundle_status_plugin = (uchar)ctx->bundle_status_recent;
     297           0 :       *charge_busy = 1;
     298           0 :     }
     299           0 :   }
     300           0 : }
     301             : 
     302             : static void
     303             : fd_bundle_tls_keylog_line( fd_bundle_tile_t * ctx,
     304             :                            char const *       label,
     305             :                            uchar const        client_random[ static 32 ],
     306             :                            uchar const *      secret,
     307           0 :                            ulong              secret_sz ) {
     308           0 :   char line[ 256 ];
     309           0 :   char * p = fd_cstr_init( line );
     310           0 :   p = fd_cstr_append_cstr( p, label );
     311           0 :   p = fd_hex_encode( p, client_random, 32UL );
     312           0 :   p = fd_cstr_append_char( p, ' ' );
     313           0 :   p = fd_hex_encode( p, secret, secret_sz );
     314           0 :   ulong line_sz = (ulong)(p-line);
     315           0 :   fd_cstr_fini( p );
     316             : 
     317           0 :   struct iovec iovs[2] = {
     318           0 :     { .iov_base=line,         .iov_len=line_sz },
     319           0 :     { .iov_base=(void *)"\n", .iov_len=1UL     },
     320           0 :   };
     321           0 :   if( FD_UNLIKELY( writev( ctx->keylog_fd, iovs, 2 )!=(long)(line_sz+1UL) ) ) {
     322           0 :     FD_LOG_WARNING(( "write(keylog) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     323           0 :   }
     324           0 : }
     325             : 
     326             : static void
     327             : fd_bundle_tls_keylog( void const * handshake,
     328             :                       void const * recv_secret,
     329             :                       void const * send_secret,
     330           0 :                       uint         encryption_level ) {
     331           0 :   fd_tlsrec_conn_t const * tls_conn = (fd_tlsrec_conn_t const *)(
     332           0 :       (ulong)handshake - offsetof(fd_tlsrec_conn_t, hs) );
     333           0 :   fd_bundle_tile_t * ctx = (fd_bundle_tile_t *)(
     334           0 :       (ulong)tls_conn - offsetof(fd_bundle_tile_t, tls_conn) );
     335           0 :   fd_tls_estate_t const * hs = handshake;
     336             : 
     337           0 :   char const * client_label;
     338           0 :   char const * server_label;
     339           0 :   switch( encryption_level ) {
     340           0 :   case FD_TLS_LEVEL_HANDSHAKE:
     341           0 :     client_label = "CLIENT_HANDSHAKE_TRAFFIC_SECRET ";
     342           0 :     server_label = "SERVER_HANDSHAKE_TRAFFIC_SECRET ";
     343           0 :     break;
     344           0 :   case FD_TLS_LEVEL_APPLICATION:
     345           0 :     client_label = "CLIENT_TRAFFIC_SECRET_0 ";
     346           0 :     server_label = "SERVER_TRAFFIC_SECRET_0 ";
     347           0 :     break;
     348           0 :   default:
     349           0 :     return;
     350           0 :   }
     351             : 
     352           0 :   uchar const * client_secret = hs->base.server ? recv_secret : send_secret;
     353           0 :   uchar const * server_secret = hs->base.server ? send_secret : recv_secret;
     354           0 :   fd_bundle_tls_keylog_line( ctx, client_label, hs->base.client_random,
     355           0 :                              client_secret, 32UL );
     356           0 :   fd_bundle_tls_keylog_line( ctx, server_label, hs->base.client_random,
     357           0 :                              server_secret, 32UL );
     358           0 : }
     359             : 
     360             : #ifndef FD_TILE_TEST
     361             : static void
     362             : fd_bundle_tile_parse_endpoint( fd_bundle_tile_t *     ctx,
     363           0 :                                fd_topo_tile_t const * tile ) {
     364           0 :   fd_url_t url[1];
     365           0 :   _Bool is_ssl = 0;
     366           0 :   if( FD_UNLIKELY( fd_url_parse_endpoint( url,
     367           0 :                                           tile->bundle.url,
     368           0 :                                           tile->bundle.url_len,
     369           0 :                                           &ctx->server_tcp_port,
     370           0 :                                           &is_ssl,
     371           0 :                                           "[tiles.bundle.url]" ) ) ) {
     372           0 :     FD_LOG_ERR(( "Could not parse [tiles.bundle.url]" ));
     373           0 :   }
     374           0 :   if( FD_UNLIKELY( url->host_len>=FD_FQDN_BUF_MAX ) ) {
     375           0 :     FD_LOG_CRIT(( "Invalid url->host_len" )); /* unreachable */
     376           0 :   }
     377           0 :   fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_fqdn ), url->host, url->host_len ) );
     378           0 :   ctx->server_fqdn_len = url->host_len;
     379             : 
     380           0 :   if( FD_UNLIKELY( tile->bundle.sni_len ) ) {
     381           0 :     fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_sni ), tile->bundle.sni, tile->bundle.sni_len ) );
     382           0 :     ctx->server_sni_len = tile->bundle.sni_len;
     383           0 :   } else {
     384           0 :     fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_sni ), url->host, url->host_len ) );
     385           0 :     ctx->server_sni_len = url->host_len;
     386           0 :   }
     387             : 
     388           0 :   if( FD_UNLIKELY( ctx->server_sni_len>=sizeof(ctx->tls->server_name) ) ) {
     389           0 :     FD_LOG_ERR(( "Server name is %lu bytes, longer than the %lu byte maximum: "
     390           0 :                  "check [tiles.bundle.url] and [tiles.bundle.tls_domain_name]",
     391           0 :                  ctx->server_sni_len, sizeof(ctx->tls->server_name)-1UL ));
     392           0 :   }
     393             : 
     394           0 :   ctx->is_ssl = !!is_ssl;
     395           0 : }
     396             : 
     397             : 
     398             : static void
     399             : fd_bundle_tile_init_tls( fd_bundle_tile_t *     ctx,
     400           0 :                          fd_topo_tile_t const * tile ) {
     401             :   /* Initialize native TLS */
     402           0 :   fd_tls_t * tls = fd_tls_join( fd_tls_new( ctx->tls ) );
     403             : 
     404           0 :   uchar rng_key[ FD_CHACHA_KEY_SZ ];
     405           0 :   if( FD_UNLIKELY( !fd_rng_secure( rng_key, sizeof(rng_key) ) ) ) FD_LOG_CRIT(( "fd_rng_secure failed" ));
     406           0 :   fd_chacha_rng_init( ctx->tls_rng, rng_key, FD_CHACHA_RNG_ALGO_CHACHA8 );
     407           0 :   fd_memzero_explicit( rng_key, sizeof(rng_key) );
     408           0 :   tls->rng = ctx->tls_rng;
     409           0 :   if( FD_LIKELY( ctx->keylog_fd>=0 ) ) tls->secrets_fn = fd_bundle_tls_keylog;
     410             : 
     411             :   /* ALPN: h2 */
     412           0 :   static uchar const alpn[] = { 2, 'h', '2' };
     413           0 :   fd_memcpy( tls->alpn, alpn, sizeof(alpn) );
     414           0 :   tls->alpn_sz = sizeof(alpn);
     415             : 
     416           0 :   if( FD_UNLIKELY( !ctx->is_ssl ) ) return; /* plaintext, nothing to verify */
     417             : 
     418           0 :   if( FD_UNLIKELY( !tile->bundle.tls_cert_verify ) ) {
     419           0 :     FD_LOG_WARNING(( "[tiles.bundle.tls_cert_verify] is disabled.  The block engine "
     420           0 :                      "certificate will not be verified.  This is insecure." ));
     421           0 :     return;
     422           0 :   }
     423             : 
     424             :   /* Load system CA certificates */
     425           0 :   if( FD_UNLIKELY( fd_x509_ca_store_load_system( ctx->ca_store )<0L ) ) {
     426           0 :     FD_LOG_ERR(( "No CA certificate bundle found, cannot verify the block engine "
     427           0 :                  "certificate.  Install the system CA certificates, or set "
     428           0 :                  "`[tiles.bundle] tls_cert_verify = false` to disable verification "
     429           0 :                  "(insecure)." ));
     430           0 :   }
     431             : 
     432           0 :   tls->ca_store = ctx->ca_store;
     433           0 : }
     434             : 
     435             : static void
     436             : privileged_init( fd_topo_t const *      topo,
     437           0 :                  fd_topo_tile_t const * tile ) {
     438           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     439             : 
     440           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     441           0 :   fd_bundle_tile_t * ctx         = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_bundle_tile_t), sizeof(fd_bundle_tile_t)                        );
     442           0 :   void *             grpc_mem    = FD_SCRATCH_ALLOC_APPEND( l, fd_grpc_client_align(),    fd_grpc_client_footprint( tile->bundle.buf_sz ) );
     443             : 
     444           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     445           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     446           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     447             : 
     448           0 :   memset( ctx, 0, sizeof(fd_bundle_tile_t) );
     449           0 :   ctx->grpc_client_mem  = grpc_mem;
     450           0 :   ctx->grpc_buf_max     = tile->bundle.buf_sz;
     451           0 :   ctx->tcp_sock         = -1;
     452           0 :   ctx->waker_client_idx = tile->waker_client_idx;
     453             : 
     454           0 :   fd_bundle_auther_init( &ctx->auther );
     455           0 :   uchar const * public_key = fd_keyload_load( tile->bundle.identity_key_path, 1 /* public key only */ );
     456           0 :   fd_memcpy( ctx->auther.pubkey, public_key, 32UL );
     457             : 
     458           0 :   ctx->keylog_fd = -1;
     459             : 
     460           0 :   if( FD_UNLIKELY( tile->bundle.key_log_path[0] ) ) {
     461           0 :     ctx->keylog_fd = open( tile->bundle.key_log_path, O_WRONLY|O_APPEND|O_CREAT, 0600 );
     462           0 :     if( FD_UNLIKELY( ctx->keylog_fd < 0 ) ) {
     463           0 :       FD_LOG_ERR(( "open(%s) failed (%i-%s)", tile->bundle.key_log_path, errno, fd_io_strerror( errno ) ));
     464           0 :     }
     465           0 :   }
     466             : 
     467           0 :   fd_bundle_tile_parse_endpoint( ctx, tile );
     468             : 
     469             :   /* Initialize native TLS before seccomp (reads CA certs from disk) */
     470           0 :   fd_bundle_tile_init_tls( ctx, tile );
     471             : 
     472             :   /* Init resolver */
     473           0 :   if( FD_UNLIKELY( !fd_netdb_open_fds( ctx->netdb_fds ) ) ) {
     474           0 :     FD_LOG_ERR(( "fd_netdb_open_fds failed" ));
     475           0 :   }
     476             : 
     477             :   /* Random seed for header hashmap */
     478           0 :   if( FD_UNLIKELY( !fd_rng_secure( &ctx->map_seed, sizeof(ulong) ) ) ) {
     479           0 :     FD_LOG_CRIT(( "fd_rng_secure failed" ));
     480           0 :   }
     481             : 
     482             :   /* Random seed for timing RNG */
     483           0 :   uint rng_seed;
     484           0 :   if( FD_UNLIKELY( !fd_rng_secure( &rng_seed, sizeof(uint) ) ) ) {
     485           0 :     FD_LOG_CRIT(( "fd_rng_secure failed" ));
     486           0 :   }
     487           0 :   if( FD_UNLIKELY( !fd_rng_join( fd_rng_new( &ctx->rng, rng_seed, 0UL ) ) ) ) {
     488           0 :     FD_LOG_CRIT(( "fd_rng_join failed" )); /* unreachable */
     489           0 :   }
     490           0 : }
     491             : 
     492             : static fd_bundle_out_ctx_t
     493             : bundle_out_link( fd_topo_t const *      topo,
     494             :                  fd_topo_link_t const * link,
     495           0 :                  ulong                  out_link_idx ) {
     496           0 :   fd_bundle_out_ctx_t out = {0};
     497           0 :   out.idx    = out_link_idx;
     498           0 :   out.mem    = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
     499           0 :   out.chunk0 = fd_dcache_compact_chunk0( out.mem, link->dcache );
     500           0 :   out.wmark  = fd_dcache_compact_wmark ( out.mem, link->dcache, link->mtu );
     501           0 :   out.chunk  = out.chunk0;
     502           0 :   return out;
     503           0 : }
     504             : 
     505             : static void
     506             : unprivileged_init( fd_topo_t const *      topo,
     507           0 :                    fd_topo_tile_t const * tile ) {
     508           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     509           0 :   if( FD_UNLIKELY( tile->kind_id!=0 ) ) {
     510           0 :     FD_LOG_ERR(( "There can only be one bundle tile" ));
     511           0 :   }
     512             : 
     513           0 :   fd_clock_tile_init( ctx->clock );
     514             : 
     515           0 :   ulong sign_in_idx = fd_topo_find_tile_in_link( topo, tile, "sign_bundle", tile->kind_id );
     516           0 :   if( FD_UNLIKELY( sign_in_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing sign_bundle link" ));
     517           0 :   fd_topo_link_t const * sign_in  = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
     518             : 
     519           0 :   ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_sign", tile->kind_id );
     520           0 :   if( FD_UNLIKELY( sign_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing bundle_sign link" ));
     521           0 :   fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
     522             : 
     523           0 :   fd_sleep_t * sleep = NULL;
     524           0 :   if( FD_UNLIKELY( topo->sleep_obj_id!=ULONG_MAX ) ) {
     525           0 :     sleep = fd_sleep_join( fd_topo_obj_laddr( topo, topo->sleep_obj_id ) );
     526           0 :     FD_TEST( sleep );
     527           0 :   }
     528             : 
     529           0 :   if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new(
     530           0 :       ctx->keyguard_client,
     531           0 :       sign_out->mcache,
     532           0 :       sign_out->dcache,
     533           0 :       sign_in->mcache,
     534           0 :       sign_in->dcache,
     535           0 :       sign_out->mtu,
     536           0 :       sign_in->mtu,
     537           0 :       sleep,
     538           0 :       sign_out->id,
     539           0 :       fd_topo_find_link_consumer( topo, sign_out )
     540           0 :   ) ) ) ) {
     541           0 :     FD_LOG_ERR(( "fd_keyguard_client_join failed" )); /* unreachable */
     542           0 :   }
     543             : 
     544           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
     545           0 :   FD_TEST( ctx->keyswitch );
     546             : 
     547           0 :   FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
     548           0 :   ctx->waker_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->waker_fseq_obj_id ) );
     549           0 :   FD_TEST( ctx->waker_fseq );
     550             : 
     551           0 :   ulong verify_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_verif", tile->kind_id );
     552           0 :   if( FD_UNLIKELY( verify_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing bundle_verif link" ));
     553           0 :   ctx->verify_out = bundle_out_link( topo, &topo->links[ tile->out_link_id[ verify_out_idx ] ], verify_out_idx );
     554             : 
     555           0 :   ulong plugin_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_status", tile->kind_id );
     556           0 :   if( plugin_out_idx!=ULONG_MAX ) {
     557           0 :     ctx->plugin_out = bundle_out_link( topo, &topo->links[ tile->out_link_id[ plugin_out_idx ] ], plugin_out_idx );
     558           0 :   } else {
     559           0 :     ctx->plugin_out = (fd_bundle_out_ctx_t){ .idx=ULONG_MAX };
     560           0 :   }
     561             : 
     562             :   /* Set socket receive buffer size */
     563           0 :   ulong so_rcvbuf = tile->bundle.buf_sz;
     564           0 :   if( FD_UNLIKELY( so_rcvbuf < 2048UL  ) ) FD_LOG_ERR(( "Invalid [development.bundle.buffer_size_kib]: too small" ));
     565           0 :   if( FD_UNLIKELY( so_rcvbuf > INT_MAX ) ) FD_LOG_ERR(( "Invalid [development.bundle.buffer_size_kib]: too large" ));
     566           0 :   ctx->so_rcvbuf = (int)so_rcvbuf;
     567             : 
     568             :   /* Set idle ping timer */
     569           0 :   ctx->keepalive_interval = (long)tile->bundle.keepalive_interval_nanos;
     570             : 
     571           0 :   ctx->bundle_status_plugin = 127;
     572           0 :   ctx->bundle_status_recent = (uchar)FD_BUNDLE_STATE_DISCONNECTED;
     573           0 :   ctx->last_bundle_status_log_nanos = fd_log_wallclock();
     574             : 
     575           0 :   FD_TEST( tile->in_cnt<=sizeof(ctx->in_kind)/sizeof(ctx->in_kind[0]) );
     576           0 :   int has_replay_in = 0;
     577           0 :   ulong polled_in_idx = 0UL;
     578           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     579           0 :     if( FD_UNLIKELY( !tile->in_link_poll[ i ] ) ) continue;
     580             : 
     581           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     582           0 :     if( !strcmp( link->name, "replay_slot" ) ) {
     583           0 :       ctx->in_kind[ polled_in_idx ] = IN_KIND_REPLAY_OUT;
     584           0 :       fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     585           0 :       ctx->replay_in.mem    = link_wksp->wksp;
     586           0 :       ctx->replay_in.chunk0 = fd_dcache_compact_chunk0( ctx->replay_in.mem, link->dcache );
     587           0 :       ctx->replay_in.wmark  = fd_dcache_compact_wmark ( ctx->replay_in.mem, link->dcache, link->mtu );
     588           0 :       has_replay_in = 1;
     589           0 :     }
     590           0 :     polled_in_idx++;
     591           0 :   }
     592             : 
     593           0 :   ctx->next_leader_slot = ULONG_MAX;
     594           0 :   ctx->reset_slot       = ULONG_MAX;
     595           0 :   ctx->sleep_mode       = has_replay_in; /* start asleep until we learn leader schedule */
     596           0 :   ctx->sleep_check_ns   = 0;
     597           0 :   ctx->halt_signing     = 0;
     598           0 :   if( !has_replay_in ) memset( &ctx->replay_in, 0, sizeof(ctx->replay_in) );
     599             : 
     600           0 :   ctx->grpc_client = fd_grpc_client_new( ctx->grpc_client_mem, &fd_bundle_client_grpc_callbacks, ctx->grpc_metrics, ctx, ctx->grpc_buf_max, ctx->map_seed );
     601           0 :   if( FD_UNLIKELY( !ctx->grpc_client ) ) {
     602           0 :     FD_LOG_CRIT(( "fd_grpc_client_new failed" )); /* unreachable */
     603           0 :   }
     604           0 :   fd_grpc_client_set_version( ctx->grpc_client, fd_version_cstr, strlen( fd_version_cstr ) );
     605           0 :   fd_grpc_client_set_authority( ctx->grpc_client, ctx->server_sni, ctx->server_sni_len, ctx->server_tcp_port );
     606           0 :   fd_grpc_client_set_https( ctx->grpc_client, ctx->is_ssl );
     607             : 
     608           0 :   fd_histf_new( ctx->metrics.msg_rx_delay,
     609           0 :       FD_MHIST_MIN( BUNDLE, MESSAGE_RX_DELAY_NANOS ),
     610           0 :       FD_MHIST_MAX( BUNDLE, MESSAGE_RX_DELAY_NANOS ) );
     611           0 : }
     612             : 
     613             : static ulong
     614             : populate_allowed_seccomp( fd_topo_t const *      topo,
     615             :                           fd_topo_tile_t const * tile,
     616             :                           ulong                  out_cnt,
     617           0 :                           struct sock_filter *   out ) {
     618           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     619             : 
     620           0 :   uint epoll_inner_fd = (uint)FD_WAKER_INNER_FD( tile->waker_client_idx );
     621           0 :   uint epoll_outer_fd = (uint)FD_WAKER_OUTER_FD;
     622             : 
     623           0 :   populate_sock_filter_policy_fd_bundle_tile(
     624           0 :       out_cnt, out,
     625           0 :       (uint)fd_log_private_logfile_fd(),
     626           0 :       (uint)ctx->keylog_fd,
     627           0 :       (uint)ctx->netdb_fds->etc_hosts,
     628           0 :       (uint)ctx->netdb_fds->etc_resolv_conf,
     629           0 :       epoll_inner_fd,
     630           0 :       epoll_outer_fd
     631           0 :   );
     632           0 :   return sock_filter_policy_fd_bundle_tile_instr_cnt;
     633           0 : }
     634             : 
     635             : static ulong
     636             : populate_allowed_fds( fd_topo_t const *      topo,
     637             :                       fd_topo_tile_t const * tile,
     638             :                       ulong                  out_fds_cnt,
     639           0 :                       int *                  out_fds ) {
     640           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     641             : 
     642           0 :   if( FD_UNLIKELY( out_fds_cnt<7UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     643             : 
     644           0 :   ulong out_cnt = 0UL;
     645           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     646           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     647           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     648           0 :   if( FD_LIKELY( ctx->netdb_fds->etc_hosts >= 0 ) )
     649           0 :     out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_hosts;
     650           0 :   out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_resolv_conf;
     651           0 :   if( FD_UNLIKELY( ctx->keylog_fd>=0 ) )
     652           0 :     out_fds[ out_cnt++ ] = ctx->keylog_fd;
     653           0 :   out_fds[ out_cnt++ ] = FD_WAKER_OUTER_FD;
     654           0 :   out_fds[ out_cnt++ ] = FD_WAKER_INNER_FD( tile->waker_client_idx );
     655           0 :   return out_cnt;
     656           0 : }
     657             : 
     658           0 : #define STEM_LAZY ((long)10e6)
     659             : 
     660           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_bundle_tile_t
     661           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_bundle_tile_t)
     662             : 
     663           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING fd_bundle_tile_housekeeping
     664           0 : #define STEM_CALLBACK_NEXT_DEADLINE       next_deadline
     665           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     666           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     667           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     668           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     669           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
     670             : 
     671             : #include "../stem/fd_stem.c"
     672             : 
     673             : fd_topo_run_tile_t fd_tile_bundle = {
     674             :   .name                     = "bundle",
     675             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     676             :   .populate_allowed_fds     = populate_allowed_fds,
     677             :   .scratch_align            = scratch_align,
     678             :   .scratch_footprint        = scratch_footprint,
     679             :   .privileged_init          = privileged_init,
     680             :   .unprivileged_init        = unprivileged_init,
     681             :   .run                      = stem_run,
     682             :   .rlimit_file_cnt          = 64,
     683             :   .keep_host_networking     = 1,
     684             :   .allow_connect            = 1
     685             : };
     686             : #endif

Generated by: LCOV version 1.14