LCOV - code coverage report
Current view: top level - disco/bundle - fd_bundle_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 347 0.0 %
Date: 2026-03-19 18:19:27 Functions: 0 16 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 "../../waltz/http/fd_url.h"
       9             : #include "../../waltz/openssl/fd_openssl_tile.h"
      10             : 
      11             : #if FD_HAS_OPENSSL
      12             : #include <errno.h>
      13             : #endif
      14             : 
      15             : #include <dirent.h> /* opendir */
      16             : #include <stdio.h> /* snprintf */
      17             : #include <fcntl.h> /* F_SETFL */
      18             : #include <unistd.h> /* close */
      19             : #include <sys/mman.h> /* PROT_READ (seccomp) */
      20             : #include <sys/uio.h> /* writev */
      21             : #include <netinet/in.h> /* AF_INET */
      22             : #include <netinet/tcp.h> /* TCP_FASTOPEN_CONNECT (seccomp) */
      23             : #include "../../waltz/resolv/fd_netdb.h"
      24             : 
      25             : #include "generated/fd_bundle_tile_seccomp.h"
      26             : 
      27             : /* Provided by fdctl/firedancer version.c */
      28             : extern char const fdctl_version_string[];
      29             : 
      30           0 : #define STEM_BURST (5UL)
      31             : FD_STATIC_ASSERT( FD_BUNDLE_CLIENT_MAX_TXN_PER_BUNDLE<=STEM_BURST, stem_burst );
      32             : 
      33             : FD_FN_CONST static ulong
      34           0 : scratch_align( void ) {
      35           0 :   return fd_ulong_max( fd_ulong_max( fd_ulong_max( alignof(fd_bundle_tile_t), fd_grpc_client_align() ), fd_alloc_align() ), pending_txn_align() );
      36           0 : }
      37             : 
      38             : FD_FN_CONST static ulong
      39           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      40           0 :   ulong pending_max = tile->bundle.out_depth;
      41           0 :   ulong l = FD_LAYOUT_INIT;
      42           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_bundle_tile_t), sizeof(fd_bundle_tile_t)                        );
      43           0 :   l = FD_LAYOUT_APPEND( l, fd_grpc_client_align(),    fd_grpc_client_footprint( tile->bundle.buf_sz ) );
      44           0 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),          fd_alloc_footprint()                            );
      45           0 :   l = FD_LAYOUT_APPEND( l, pending_txn_align(),       pending_txn_footprint( pending_max )            );
      46           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      47           0 : }
      48             : 
      49             : FD_FN_CONST static inline ulong
      50           0 : loose_footprint( fd_topo_tile_t const * tile ) {
      51           0 :   (void)tile;
      52             :   /* Leftover space for OpenSSL allocations */
      53           0 :   return 1UL<<26; /* 64 MiB */
      54           0 : }
      55             : 
      56             : static inline void
      57           0 : metrics_write( fd_bundle_tile_t * ctx ) {
      58           0 :   FD_MCNT_SET( BUNDLE, TRANSACTION_RECEIVED,   ctx->metrics.txn_received_cnt          );
      59           0 :   FD_MCNT_SET( BUNDLE, BUNDLE_RECEIVED,        ctx->metrics.bundle_received_cnt       );
      60           0 :   FD_MCNT_SET( BUNDLE, PACKET_RECEIVED,        ctx->metrics.packet_received_cnt       );
      61           0 :   FD_MCNT_SET( BUNDLE, PROTO_RECEIVED_BYTES,   ctx->metrics.proto_received_bytes      );
      62           0 :   FD_MCNT_SET( BUNDLE, SHREDSTREAM_HEARTBEATS, ctx->metrics.shredstream_heartbeat_cnt );
      63           0 :   FD_MCNT_SET( BUNDLE, KEEPALIVES,             ctx->metrics.ping_ack_cnt              );
      64           0 :   FD_MCNT_SET( BUNDLE, ERRORS_PROTOBUF,        ctx->metrics.decode_fail_cnt           );
      65           0 :   FD_MCNT_SET( BUNDLE, ERRORS_TRANSPORT,       ctx->metrics.transport_fail_cnt        );
      66           0 :   FD_MCNT_SET( BUNDLE, ERRORS_NO_FEE_INFO,     ctx->metrics.missing_builder_info_fail_cnt );
      67           0 :   FD_MGAUGE_SET( BUNDLE, PENDING_TRANSACTIONS, pending_txn_cnt( ctx->pending_txns )   );
      68           0 :   FD_MCNT_SET  ( BUNDLE, TRANSACTION_DROPPED_BACKPRESSURE, ctx->metrics.backpressure_drop_cnt );
      69           0 : #if FD_HAS_OPENSSL
      70           0 :   FD_MCNT_SET( BUNDLE, ERRORS_SSL_ALLOC,       fd_ossl_alloc_errors                 );
      71           0 : #endif
      72             : 
      73           0 :   FD_MGAUGE_SET( BUNDLE, RTT_SAMPLE,   (ulong)ctx->rtt->latest_rtt   );
      74           0 :   FD_MGAUGE_SET( BUNDLE, RTT_SMOOTHED, (ulong)ctx->rtt->smoothed_rtt );
      75           0 :   FD_MGAUGE_SET( BUNDLE, RTT_VAR,      (ulong)ctx->rtt->var_rtt      );
      76             : 
      77           0 :   FD_MHIST_COPY( BUNDLE, MESSAGE_RX_DELAY_NANOS, ctx->metrics.msg_rx_delay );
      78             : 
      79           0 :   fd_wksp_t * wksp = fd_wksp_containing( ctx );
      80           0 :   fd_wksp_usage_t usage[1];
      81           0 :   ulong const free_tag = 0UL;
      82           0 :   if( FD_UNLIKELY( !fd_wksp_usage( wksp, &free_tag, 1UL, usage ) ) ) {
      83           0 :     FD_LOG_ERR(( "fd_wksp_usage failed" )); /* unreachable */
      84           0 :   }
      85           0 :   FD_MGAUGE_SET( BUNDLE, HEAP_SIZE,       usage->total_sz );
      86           0 :   FD_MGAUGE_SET( BUNDLE, HEAP_FREE_BYTES, usage->free_sz  );
      87             : 
      88           0 :   int bundle_status = fd_bundle_client_status( ctx );
      89           0 :   FD_MGAUGE_SET( BUNDLE, CONNECTED, bundle_status==FD_BUNDLE_BLOCK_ENGINE_STATUS_CONNECTED );
      90           0 :   ctx->bundle_status_recent = (uchar)bundle_status;
      91           0 : }
      92             : 
      93             : void
      94           0 : fd_bundle_tile_housekeeping( fd_bundle_tile_t * ctx ) {
      95           0 :   long log_interval_ns = (long)30e9;
      96           0 :   int  status          = fd_bundle_client_status( ctx );
      97           0 :   long log_next_ns     = ctx->last_bundle_status_log_nanos + log_interval_ns;
      98           0 :   long now_ns          = fd_log_wallclock();
      99           0 :   if( FD_UNLIKELY( status!=FD_BUNDLE_BLOCK_ENGINE_STATUS_CONNECTED && now_ns>log_next_ns ) ) {
     100           0 :     FD_LOG_WARNING(( "No bundle server connection in the last %ld seconds", log_interval_ns/(long)1e9 ) );
     101           0 :     ctx->last_bundle_status_log_nanos = now_ns;
     102           0 :   }
     103             : 
     104           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     105           0 :     fd_memcpy( ctx->auther.pubkey, ctx->keyswitch->bytes, 32UL );
     106           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     107           0 :     ctx->defer_reset = 1;
     108           0 :   }
     109           0 : }
     110             : 
     111             : static void
     112             : fd_bundle_tile_publish_block_engine_update(
     113             :     fd_bundle_tile_t *  ctx,
     114             :     fd_stem_context_t * stem
     115           0 : ) {
     116           0 :   fd_bundle_block_engine_update_t * update =
     117           0 :       fd_chunk_to_laddr( ctx->plugin_out.mem, ctx->plugin_out.chunk );
     118           0 :   memset( update, 0, sizeof(fd_bundle_block_engine_update_t) );
     119             : 
     120           0 :   strncpy( update->name, "jito", sizeof(update->name) );
     121             : 
     122             :   /* Deliberately silently truncates */
     123           0 :   snprintf( update->url, sizeof(update->url), "%s://%.*s:%u",
     124           0 :             ctx->is_ssl ? "https" : "http",
     125           0 :             (int)ctx->server_fqdn_len,
     126           0 :             ctx->server_fqdn,
     127           0 :             ctx->server_tcp_port );
     128             : 
     129             :   /* Format IPv4 string */
     130           0 :   snprintf( update->ip_cstr, sizeof(update->ip_cstr),
     131           0 :             FD_IP4_ADDR_FMT,
     132           0 :             FD_IP4_ADDR_FMT_ARGS( ctx->server_ip4_addr ) );
     133             : 
     134           0 :   update->status = (uchar)ctx->bundle_status_recent;
     135             : 
     136           0 :   ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_bundle_now() );
     137           0 :   fd_stem_publish(
     138           0 :       stem,
     139           0 :       ctx->plugin_out.idx,
     140           0 :       (ulong)ctx->bundle_status_recent,
     141           0 :       ctx->plugin_out.chunk,
     142           0 :       sizeof(fd_bundle_block_engine_update_t),
     143           0 :       0UL, /* ctl */
     144           0 :       0UL, /* seq */
     145           0 :       tspub
     146           0 :   );
     147           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 );
     148           0 : }
     149             : 
     150             : static void
     151             : before_credit( fd_bundle_tile_t *  ctx,
     152             :                fd_stem_context_t * stem,
     153           0 :                int *               charge_busy ) {
     154           0 :   if( FD_UNLIKELY( !ctx->stem ) ) {
     155           0 :     ctx->stem = stem;
     156           0 :   }
     157             : 
     158           0 :   if( pending_txn_empty( ctx->pending_txns ) ) {
     159           0 :     fd_bundle_client_step( ctx, charge_busy );
     160           0 :   }
     161           0 : }
     162             : 
     163             : static void
     164             : after_credit( fd_bundle_tile_t *  ctx,
     165             :               fd_stem_context_t * stem,
     166             :               int *               opt_poll_in,
     167           0 :               int *               charge_busy ) {
     168           0 :   if( !pending_txn_empty( ctx->pending_txns ) ) {
     169           0 :     fd_bundle_pending_txn_t * head = pending_txn_peek_head( ctx->pending_txns );
     170           0 :     ulong drain_seq = head->bundle_seq;
     171           0 :     ulong drain_sig = head->sig;
     172           0 :     ulong drain_cnt = 0UL;
     173             : 
     174           0 :     do {
     175           0 :       fd_bundle_pending_txn_t const * txn = pending_txn_peek_head( ctx->pending_txns );
     176             : 
     177           0 :       fd_txn_m_t * txnm = fd_chunk_to_laddr( ctx->verify_out.mem, ctx->verify_out.chunk );
     178           0 :       *txnm = (fd_txn_m_t) {
     179           0 :         .reference_slot = 0UL,
     180           0 :         .payload_sz     = txn->payload_sz,
     181           0 :         .txn_t_sz       = 0U,
     182           0 :         .source_ipv4    = txn->source_ipv4,
     183           0 :         .source_tpu     = FD_TXN_M_TPU_SOURCE_BUNDLE,
     184           0 :         .block_engine   = {
     185           0 :           .bundle_id      = txn->bundle_seq,
     186           0 :           .bundle_txn_cnt = txn->bundle_txn_cnt,
     187           0 :           .commission     = txn->commission,
     188           0 :         },
     189           0 :       };
     190           0 :       fd_memcpy( txnm->block_engine.commission_pubkey, txn->commission_pubkey, 32UL );
     191           0 :       fd_memcpy( fd_txn_m_payload( txnm ), txn->payload, txn->payload_sz );
     192             : 
     193           0 :       ulong sz    = fd_txn_m_realized_footprint( txnm, 0, 0 );
     194           0 :       ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_bundle_now() );
     195           0 :       fd_stem_publish( stem, ctx->verify_out.idx, txn->sig, ctx->verify_out.chunk, sz, 0UL, 0UL, tspub );
     196           0 :       ctx->verify_out.chunk = fd_dcache_compact_next( ctx->verify_out.chunk, sz, ctx->verify_out.chunk0, ctx->verify_out.wmark );
     197             : 
     198           0 :       pending_txn_remove_head( ctx->pending_txns );
     199           0 :       drain_cnt++;
     200           0 :     } while( fd_bundle_drain_continue( ctx->pending_txns, drain_sig, drain_seq, drain_cnt, STEM_BURST ) );
     201             : 
     202           0 :     *charge_busy = 1;
     203           0 :     *opt_poll_in = 0;
     204           0 :   }
     205             : 
     206           0 :   if( ctx->plugin_out.mem ) {
     207           0 :     if( FD_UNLIKELY( ctx->bundle_status_recent != ctx->bundle_status_plugin ) ) {
     208           0 :       fd_bundle_tile_publish_block_engine_update( ctx, stem );
     209           0 :       ctx->bundle_status_plugin = (uchar)ctx->bundle_status_recent;
     210           0 :       *charge_busy = 1;
     211           0 :     }
     212           0 :   }
     213           0 : }
     214             : 
     215             : static void
     216             : fd_bundle_tile_parse_endpoint( fd_bundle_tile_t *     ctx,
     217           0 :                                fd_topo_tile_t const * tile ) {
     218           0 :   fd_url_t url[1];
     219           0 :   _Bool is_ssl = 0;
     220           0 :   if( FD_UNLIKELY( fd_url_parse_endpoint( url,
     221           0 :                                           tile->bundle.url,
     222           0 :                                           tile->bundle.url_len,
     223           0 :                                           &ctx->server_tcp_port,
     224           0 :                                           &is_ssl,
     225           0 :                                           "[tiles.bundle.url]" ) ) ) {
     226           0 :     FD_LOG_ERR(( "Could not parse [tiles.bundle.url]" ));
     227           0 :   }
     228           0 :   if( FD_UNLIKELY( url->host_len > 255 ) ) {
     229           0 :     FD_LOG_CRIT(( "Invalid url->host_len" )); /* unreachable */
     230           0 :   }
     231           0 :   fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_fqdn ), url->host, url->host_len ) );
     232           0 :   ctx->server_fqdn_len = url->host_len;
     233             : 
     234           0 :   if( FD_UNLIKELY( tile->bundle.sni_len ) ) {
     235           0 :     fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_sni ), tile->bundle.sni, tile->bundle.sni_len ) );
     236           0 :     ctx->server_sni_len = tile->bundle.sni_len;
     237           0 :   } else {
     238           0 :     fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_sni ), url->host, url->host_len ) );
     239           0 :     ctx->server_sni_len = url->host_len;
     240           0 :   }
     241             : 
     242           0 :   ctx->is_ssl = !!is_ssl;
     243             : #if !FD_HAS_OPENSSL
     244             :   if( FD_UNLIKELY( is_ssl ) ) {
     245             :     FD_LOG_ERR(( "This build does not include OpenSSL. To install OpenSSL, re-run ./deps.sh and do a clean re build." ));
     246             :   }
     247             : #endif
     248           0 : }
     249             : 
     250             : #if FD_HAS_OPENSSL
     251             : 
     252             : static void
     253             : fd_ossl_keylog_callback( SSL const *  ssl,
     254           0 :                          char const * line ) {
     255           0 :   SSL_CTX * ssl_ctx = SSL_get_SSL_CTX( ssl );
     256           0 :   fd_bundle_tile_t * ctx = SSL_CTX_get_ex_data( ssl_ctx, 0 );
     257           0 :   ulong line_len = strlen( line );
     258           0 :   struct iovec iovs[2] = {
     259           0 :     { .iov_base=(void *)line, .iov_len=line_len },
     260           0 :     { .iov_base=(void *)"\n", .iov_len=1UL }
     261           0 :   };
     262           0 :   if( FD_UNLIKELY( writev( ctx->keylog_fd, iovs, 2 )!=(long)line_len+1 ) ) {
     263           0 :     FD_LOG_WARNING(( "write(keylog) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     264           0 :   }
     265           0 : }
     266             : 
     267             : static void
     268             : fd_bundle_tile_init_openssl( fd_bundle_tile_t * ctx,
     269             :                              void *             alloc_mem,
     270           0 :                              int                tls_cert_verify ) {
     271           0 :   fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( alloc_mem, 1UL ), 1UL );
     272           0 :   if( FD_UNLIKELY( !alloc ) ) {
     273           0 :     FD_LOG_ERR(( "fd_alloc_new failed" ));
     274           0 :   }
     275           0 :   ctx->ssl_alloc = alloc;
     276           0 :   fd_ossl_tile_init( alloc );
     277             : 
     278           0 :   SSL_CTX * ssl_ctx = SSL_CTX_new( TLS_client_method() );
     279           0 :   if( FD_UNLIKELY( !ssl_ctx ) ) {
     280           0 :     FD_LOG_ERR(( "SSL_CTX_new failed" ));
     281           0 :   }
     282             : 
     283           0 :   if( FD_UNLIKELY( !SSL_CTX_set_ex_data( ssl_ctx, 0, ctx ) ) ) {
     284           0 :     FD_LOG_ERR(( "SSL_CTX_set_ex_data failed" ));
     285           0 :   }
     286             : 
     287           0 :   if( FD_UNLIKELY( !SSL_CTX_set_mode( ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_AUTO_RETRY ) ) ) {
     288           0 :     FD_LOG_ERR(( "SSL_CTX_set_mode failed" ));
     289           0 :   }
     290             : 
     291           0 :   if( FD_UNLIKELY( !SSL_CTX_set_min_proto_version( ssl_ctx, TLS1_3_VERSION ) ) ) {
     292           0 :     FD_LOG_ERR(( "SSL_CTX_set_min_proto_version(ssl_ctx,TLS1_3_VERSION) failed" ));
     293           0 :   }
     294             : 
     295           0 :   if( FD_UNLIKELY( 0!=SSL_CTX_set_alpn_protos( ssl_ctx, (const unsigned char *)"\x02h2", 3 ) ) ) {
     296           0 :     FD_LOG_ERR(( "SSL_CTX_set_alpn_protos failed" ));
     297           0 :   }
     298             : 
     299           0 :   if( tls_cert_verify ) {
     300           0 :     fd_ossl_load_certs( ssl_ctx );
     301           0 :   }
     302             : 
     303           0 :   if( FD_LIKELY( ctx->keylog_fd >= 0 ) ) {
     304           0 :     SSL_CTX_set_keylog_callback( ssl_ctx, fd_ossl_keylog_callback );
     305           0 :   }
     306             : 
     307           0 :   ctx->ssl_ctx = ssl_ctx;
     308           0 : }
     309             : 
     310             : #endif /* FD_HAS_OPENSSL */
     311             : 
     312             : static void
     313             : privileged_init( fd_topo_t *      topo,
     314           0 :                  fd_topo_tile_t * tile ) {
     315           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     316             : 
     317           0 :   ulong const pending_max = tile->bundle.out_depth;
     318             : 
     319           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     320           0 :   fd_bundle_tile_t * ctx         = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_bundle_tile_t), sizeof(fd_bundle_tile_t)                        );
     321           0 :   void *             grpc_mem    = FD_SCRATCH_ALLOC_APPEND( l, fd_grpc_client_align(),    fd_grpc_client_footprint( tile->bundle.buf_sz ) );
     322           0 :   void *             alloc_mem   = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(),          fd_alloc_footprint()                            );
     323           0 :   void *             deque_mem   = FD_SCRATCH_ALLOC_APPEND( l, pending_txn_align(),        pending_txn_footprint( pending_max )            );
     324           0 :   ulong              scratch_end = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     325           0 :   (void)alloc_mem; /* potentially unused */
     326             : 
     327           0 :   if( FD_UNLIKELY( (ulong)ctx != (ulong)scratch ) ) {
     328           0 :     FD_LOG_CRIT(( "Invalid bundle tile scratch alignment" )); /* unreachable */
     329           0 :   }
     330           0 :   if( FD_UNLIKELY( scratch_end - (ulong)scratch > scratch_footprint( tile ) ) ) {
     331           0 :     FD_LOG_CRIT(( "Bundle tile scratch overflow" )); /* unreachable */
     332           0 :   }
     333             : 
     334           0 :   memset( ctx, 0, sizeof(fd_bundle_tile_t) );
     335           0 :   ctx->grpc_client_mem = grpc_mem;
     336           0 :   ctx->grpc_buf_max    = tile->bundle.buf_sz;
     337           0 :   ctx->tcp_sock        = -1;
     338           0 :   ctx->pending_txns    = pending_txn_join( pending_txn_new( deque_mem, pending_max ) );
     339             : 
     340           0 :   fd_bundle_auther_init( &ctx->auther );
     341           0 :   uchar const * public_key = fd_keyload_load( tile->bundle.identity_key_path, 1 /* public key only */ );
     342           0 :   fd_memcpy( ctx->auther.pubkey, public_key, 32UL );
     343             : 
     344           0 :   ctx->keylog_fd = -1;
     345             : 
     346           0 : # if FD_HAS_OPENSSL
     347             : 
     348           0 :   if( FD_UNLIKELY( tile->bundle.key_log_path[0] ) ) {
     349           0 :     ctx->keylog_fd = open( tile->bundle.key_log_path, O_WRONLY|O_APPEND|O_CREAT, 0644 );
     350           0 :     if( FD_UNLIKELY( ctx->keylog_fd < 0 ) ) {
     351           0 :       FD_LOG_ERR(( "open(%s) failed (%i-%s)", tile->bundle.key_log_path, errno, fd_io_strerror( errno ) ));
     352           0 :     }
     353           0 :   }
     354             : 
     355             :   /* OpenSSL goes and tries to read files and allocate memory and
     356             :      other dumb things on a thread local basis, so we need a special
     357             :      initializer to do it before seccomp happens in the process. */
     358           0 :   fd_bundle_tile_init_openssl( ctx, alloc_mem, tile->bundle.tls_cert_verify );
     359             : 
     360           0 : # endif /* FD_HAS_OPENSSL */
     361             : 
     362             :   /* Init resolver */
     363           0 :   if( FD_UNLIKELY( !fd_netdb_open_fds( ctx->netdb_fds ) ) ) {
     364           0 :     FD_LOG_ERR(( "fd_netdb_open_fds failed" ));
     365           0 :   }
     366             : 
     367             :   /* Random seed for header hashmap */
     368           0 :   if( FD_UNLIKELY( !fd_rng_secure( &ctx->map_seed, sizeof(ulong) ) ) ) {
     369           0 :     FD_LOG_CRIT(( "fd_rng_secure failed" ));
     370           0 :   }
     371             : 
     372             :   /* Random seed for timing RNG */
     373           0 :   uint rng_seed;
     374           0 :   if( FD_UNLIKELY( !fd_rng_secure( &rng_seed, sizeof(uint) ) ) ) {
     375           0 :     FD_LOG_CRIT(( "fd_rng_secure failed" ));
     376           0 :   }
     377           0 :   if( FD_UNLIKELY( !fd_rng_join( fd_rng_new( &ctx->rng, rng_seed, 0UL ) ) ) ) {
     378           0 :     FD_LOG_CRIT(( "fd_rng_join failed" )); /* unreachable */
     379           0 :   }
     380           0 : }
     381             : 
     382             : static fd_bundle_out_ctx_t
     383             : bundle_out_link( fd_topo_t const *      topo,
     384             :                  fd_topo_link_t const * link,
     385           0 :                  ulong                  out_link_idx ) {
     386           0 :   fd_bundle_out_ctx_t out = {0};
     387           0 :   out.idx    = out_link_idx;
     388           0 :   out.mem    = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
     389           0 :   out.chunk0 = fd_dcache_compact_chunk0( out.mem, link->dcache );
     390           0 :   out.wmark  = fd_dcache_compact_wmark ( out.mem, link->dcache, link->mtu );
     391           0 :   out.chunk  = out.chunk0;
     392           0 :   return out;
     393           0 : }
     394             : 
     395             : static void
     396             : unprivileged_init( fd_topo_t *      topo,
     397           0 :                    fd_topo_tile_t * tile ) {
     398           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     399           0 :   if( FD_UNLIKELY( tile->kind_id!=0 ) ) {
     400           0 :     FD_LOG_ERR(( "There can only be one bundle tile" ));
     401           0 :   }
     402             : 
     403           0 :   ulong sign_in_idx = fd_topo_find_tile_in_link( topo, tile, "sign_bundle", tile->kind_id );
     404           0 :   if( FD_UNLIKELY( sign_in_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing sign_bundle link" ));
     405           0 :   fd_topo_link_t const * sign_in  = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
     406             : 
     407           0 :   ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_sign", tile->kind_id );
     408           0 :   if( FD_UNLIKELY( sign_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing bundle_sign link" ));
     409           0 :   fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
     410             : 
     411           0 :   if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new(
     412           0 :       ctx->keyguard_client,
     413           0 :       sign_out->mcache,
     414           0 :       sign_out->dcache,
     415           0 :       sign_in->mcache,
     416           0 :       sign_in->dcache,
     417           0 :       sign_out->mtu
     418           0 :   ) ) ) ) {
     419           0 :     FD_LOG_ERR(( "fd_keyguard_client_join failed" )); /* unreachable */
     420           0 :   }
     421             : 
     422           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
     423           0 :   FD_TEST( ctx->keyswitch );
     424             : 
     425           0 :   ulong verify_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_verif", tile->kind_id );
     426           0 :   if( FD_UNLIKELY( verify_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing bundle_verif link" ));
     427           0 :   ctx->verify_out = bundle_out_link( topo, &topo->links[ tile->out_link_id[ verify_out_idx ] ], verify_out_idx );
     428             : 
     429           0 :   ulong plugin_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_status", tile->kind_id );
     430           0 :   if( plugin_out_idx!=ULONG_MAX ) {
     431           0 :     ctx->plugin_out = bundle_out_link( topo, &topo->links[ tile->out_link_id[ plugin_out_idx ] ], plugin_out_idx );
     432           0 :   } else {
     433           0 :     ctx->plugin_out = (fd_bundle_out_ctx_t){ .idx=ULONG_MAX };
     434           0 :   }
     435             : 
     436             :   /* Set socket receive buffer size */
     437           0 :   ulong so_rcvbuf = tile->bundle.buf_sz;
     438           0 :   if( FD_UNLIKELY( so_rcvbuf < 2048UL  ) ) FD_LOG_ERR(( "Invalid [development.bundle.buffer_size_kib]: too small" ));
     439           0 :   if( FD_UNLIKELY( so_rcvbuf > INT_MAX ) ) FD_LOG_ERR(( "Invalid [development.bundle.buffer_size_kib]: too large" ));
     440           0 :   ctx->so_rcvbuf = (int)so_rcvbuf;
     441             : 
     442             :   /* Set idle ping timer */
     443           0 :   ctx->keepalive_interval = (long)tile->bundle.keepalive_interval_nanos;
     444             : 
     445           0 :   ctx->bundle_status_plugin = 127;
     446           0 :   ctx->bundle_status_recent = FD_BUNDLE_BLOCK_ENGINE_STATUS_DISCONNECTED;
     447           0 :   ctx->last_bundle_status_log_nanos = fd_log_wallclock();
     448             : 
     449           0 :   fd_bundle_tile_parse_endpoint( ctx, tile );
     450             : 
     451           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 );
     452           0 :   if( FD_UNLIKELY( !ctx->grpc_client ) ) {
     453           0 :     FD_LOG_CRIT(( "fd_grpc_client_new failed" )); /* unreachable */
     454           0 :   }
     455           0 :   fd_grpc_client_set_version( ctx->grpc_client, fdctl_version_string, strlen( fdctl_version_string ) );
     456           0 :   fd_grpc_client_set_authority( ctx->grpc_client, ctx->server_sni, ctx->server_sni_len, ctx->server_tcp_port );
     457             : 
     458           0 :   fd_histf_new( ctx->metrics.msg_rx_delay,
     459           0 :       FD_MHIST_MIN( BUNDLE, MESSAGE_RX_DELAY_NANOS ),
     460           0 :       FD_MHIST_MAX( BUNDLE, MESSAGE_RX_DELAY_NANOS ) );
     461           0 : }
     462             : 
     463             : static ulong
     464             : populate_allowed_seccomp( fd_topo_t const *      topo,
     465             :                           fd_topo_tile_t const * tile,
     466             :                           ulong                  out_cnt,
     467           0 :                           struct sock_filter *   out ) {
     468           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     469             : 
     470           0 :   populate_sock_filter_policy_fd_bundle_tile(
     471           0 :       out_cnt, out,
     472           0 :       (uint)fd_log_private_logfile_fd(),
     473           0 :       (uint)ctx->keylog_fd,
     474           0 :       (uint)ctx->netdb_fds->etc_hosts,
     475           0 :       (uint)ctx->netdb_fds->etc_resolv_conf
     476           0 :   );
     477           0 :   return sock_filter_policy_fd_bundle_tile_instr_cnt;
     478           0 : }
     479             : 
     480             : static ulong
     481             : populate_allowed_fds( fd_topo_t const *      topo,
     482             :                       fd_topo_tile_t const * tile,
     483             :                       ulong                  out_fds_cnt,
     484           0 :                       int *                  out_fds ) {
     485           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     486             : 
     487           0 :   if( FD_UNLIKELY( out_fds_cnt<5UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     488             : 
     489           0 :   ulong out_cnt = 0UL;
     490           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     491           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     492           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     493           0 :   if( FD_LIKELY( ctx->netdb_fds->etc_hosts >= 0 ) )
     494           0 :     out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_hosts;
     495           0 :   out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_resolv_conf;
     496           0 :   if( FD_UNLIKELY( ctx->keylog_fd>=0 ) )
     497           0 :     out_fds[ out_cnt++ ] = ctx->keylog_fd;
     498           0 :   return out_cnt;
     499           0 : }
     500             : 
     501           0 : #define STEM_LAZY ((long)10e6)
     502             : 
     503           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_bundle_tile_t
     504           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_bundle_tile_t)
     505             : 
     506           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING fd_bundle_tile_housekeeping
     507           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     508           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     509           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
     510             : 
     511             : #include "../stem/fd_stem.c"
     512             : 
     513             : fd_topo_run_tile_t fd_tile_bundle = {
     514             :   .name                     = "bundle",
     515             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     516             :   .populate_allowed_fds     = populate_allowed_fds,
     517             :   .scratch_align            = scratch_align,
     518             :   .scratch_footprint        = scratch_footprint,
     519             :   .loose_footprint          = loose_footprint,
     520             :   .privileged_init          = privileged_init,
     521             :   .unprivileged_init        = unprivileged_init,
     522             :   .run                      = stem_run,
     523             :   .rlimit_file_cnt          = 64,
     524             :   .keep_host_networking     = 1,
     525             :   .allow_connect            = 1
     526             : };

Generated by: LCOV version 1.14