LCOV - code coverage report
Current view: top level - disco/gui - fd_gui_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 615 0.0 %
Date: 2026-07-22 05:23:51 Functions: 0 20 0.0 %

          Line data    Source code
       1             : /* The frontend assets are pre-built and statically compiled into the
       2             :    binary here.  To regenerate them, run
       3             : 
       4             :     $ git clone https://github.com/firedancer-io/firedancer-frontend.git frontend
       5             :     $ make frontend
       6             : 
       7             :    from the repository root. */
       8             : 
       9             : #include "../../disco/gui/generated/http_import_dist.h"
      10             : 
      11             : /* STATIC_FILES is the null-terminated list of frontend assets baked into
      12             :    the binary.  It is defined in generated/http_import_dist.c and accessed
      13             :    in the gui_http_request callback. */
      14             : 
      15             : #include <sys/socket.h> /* SOCK_CLOEXEC, SOCK_NONBLOCK needed for seccomp filter */
      16             : 
      17             : #include "generated/fd_gui_tile_seccomp.h"
      18             : 
      19             : #include "../../disco/tiles.h"
      20             : #include "../../disco/keyguard/fd_keyload.h"
      21             : #include "../../disco/keyguard/fd_keyswitch.h"
      22             : #include "../../disco/gui/fd_gui.h"
      23             : #include "../../disco/gui/fd_gui_store.h"
      24             : #include "../../disco/gui/fd_gui_hist.h"
      25             : #include "../../discof/replay/fd_execrp.h"
      26             : #include "../../disco/metrics/fd_metrics.h"
      27             : #include "../../disco/net/fd_net_tile.h"
      28             : #include "../../disco/fd_clock_tile.h"
      29             : #include "../../discof/genesis/fd_genesi_tile.h" // TODO: Layering violation
      30             : #include "../../waltz/http/fd_http_server.h"
      31             : #include "../../waltz/http/fd_http_server_private.h"
      32             : #include "../../third_party/cjson/cJSON_alloc.h"
      33             : #include "../../discof/repair/fd_repair.h"
      34             : #include "../../discof/replay/fd_replay_tile.h"
      35             : #include "../../disco/shred/fd_shred_tile.h"
      36             : #include "../../flamenco/accdb/fd_accdb_shmem.h"
      37             : 
      38           0 : #define IN_KIND_PACK_EXECLE   ( 2UL)
      39           0 : #define IN_KIND_PACK_POH      ( 3UL)
      40           0 : #define IN_KIND_EXECLE_POH    ( 4UL)
      41           0 : #define IN_KIND_SHRED_OUT     ( 5UL) /* firedancer only */
      42           0 : #define IN_KIND_NET_GOSSVF    ( 6UL) /* firedancer only */
      43           0 : #define IN_KIND_GOSSIP_NET    ( 7UL) /* firedancer only */
      44           0 : #define IN_KIND_GOSSIP_OUT    ( 8UL) /* firedancer only */
      45           0 : #define IN_KIND_SNAPCT        ( 9UL) /* firedancer only */
      46           0 : #define IN_KIND_REPAIR_NET    (10UL) /* firedancer only */
      47           0 : #define IN_KIND_TOWER_OUT     (11UL) /* firedancer only */
      48           0 : #define IN_KIND_REPLAY_OUT    (12UL) /* firedancer only */
      49           0 : #define IN_KIND_EPOCH         (13UL) /* firedancer only */
      50           0 : #define IN_KIND_GENESI_OUT    (14UL) /* firedancer only */
      51           0 : #define IN_KIND_SNAPIN        (15UL) /* firedancer only */
      52           0 : #define IN_KIND_EXECRP_REPLAY (16UL) /* firedancer only */
      53           0 : #define IN_KIND_BUNDLE        (17UL)
      54           0 : #define IN_KIND_SNAPIN_MANIF  (18UL) /* firedancer only */
      55             : 
      56             : FD_IMPORT_BINARY( firedancer_svg, "book/public/fire.svg" );
      57             : 
      58           0 : #define FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN       65536
      59           0 : #define FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN 65536
      60           0 : #define FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT 8192
      61             : 
      62             : static fd_http_server_params_t
      63           0 : derive_http_params( fd_topo_tile_t const * tile ) {
      64           0 :   return (fd_http_server_params_t) {
      65           0 :     .max_connection_cnt    = tile->gui.max_http_connections,
      66           0 :     .max_ws_connection_cnt = tile->gui.max_websocket_connections,
      67           0 :     .max_request_len       = FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN,
      68           0 :     .max_ws_recv_frame_len = FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN,
      69           0 :     .max_ws_send_frame_cnt = FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT,
      70           0 :     .outgoing_buffer_sz    = tile->gui.send_buffer_size_mb * (1UL<<20UL),
      71           0 :     .compress_websocket    = tile->gui.websocket_compression,
      72           0 :   };
      73           0 : }
      74             : 
      75             : struct fd_gui_in_ctx {
      76             :   fd_wksp_t * mem;
      77             :   ulong       mtu;
      78             :   ulong       chunk0;
      79             :   ulong       wmark;
      80             : };
      81             : 
      82             : typedef struct fd_gui_in_ctx fd_gui_in_ctx_t;
      83             : 
      84             : struct fd_gui_out_ctx {
      85             :   ulong       idx;
      86             :   fd_wksp_t * mem;
      87             :   ulong       chunk0;
      88             :   ulong       wmark;
      89             :   ulong       chunk;
      90             : };
      91             : 
      92             : typedef struct fd_gui_out_ctx fd_gui_out_ctx_t;
      93             : 
      94             : typedef struct {
      95             :   fd_topo_t const * topo;
      96             : 
      97             :   int is_full_client;
      98             :   int snapshots_enabled;
      99             : 
     100             :   fd_gui_t * gui;
     101             :   fd_gui_store_t * db;
     102             :   fd_gui_peers_ctx_t * peers;
     103             : 
     104             :   ulong in_cnt;
     105             :   ulong idle_cnt;
     106             : 
     107             :   /* Most of the gui tile uses fd_clock for timing, but some stem
     108             :      timestamps still used tickcounts, so we keep separate timestamps
     109             :      here to handle those cases until fd_clock is more widely adopted. */
     110             :   long ref_wallclock;
     111             :   long ref_tickcount;
     112             :   double tick_per_ns;
     113             : 
     114             :   fd_clock_tile_t clock[1];
     115             : 
     116             :   ulong chunk;
     117             :   union {
     118             :     struct {
     119             :       ulong slot;
     120             :       ulong shred_idx;
     121             :     } repair_net;
     122             : 
     123             :     uchar net_gossvf[ FD_NET_MTU ];
     124             :     uchar gossip_net[ FD_NET_MTU ];
     125             :   } parsed;
     126             : 
     127             :   fd_http_server_t * gui_server;
     128             : 
     129             :   long next_poll_deadline;
     130             : 
     131             :   fd_keyswitch_t * keyswitch;
     132             :   uchar const *    identity_key;
     133             : 
     134             :   int               has_vote_key;
     135             :   fd_pubkey_t const vote_key[ 1UL ];
     136             : 
     137             :   ulong           in_kind[ 64UL ];
     138             :   int             in_reliable[ 64UL ];
     139             :   ulong           in_bank_idx[ 64UL ];
     140             :   fd_gui_in_ctx_t in[ 64UL ];
     141             : 
     142             :   fd_net_rx_bounds_t net_in_bounds[ 64UL ];
     143             : } fd_gui_ctx_t;
     144             : 
     145             : FD_FN_CONST static inline ulong
     146           0 : scratch_align( void ) {
     147           0 :   ulong a = alignof( fd_gui_ctx_t );
     148           0 :   a = fd_ulong_max( a, fd_http_server_align() );
     149           0 :   a = fd_ulong_max( a, fd_gui_peers_align() );
     150           0 :   a = fd_ulong_max( a, fd_gui_align() );
     151           0 :   a = fd_ulong_max( a, fd_alloc_align() );
     152           0 :   return a;
     153           0 : }
     154             : 
     155             : static inline ulong
     156           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     157           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     158           0 :   ulong http_fp = fd_http_server_footprint( http_param );
     159           0 :   if( FD_UNLIKELY( !http_fp ) ) FD_LOG_ERR(( "Invalid [tiles.gui] config parameters" ));
     160             : 
     161           0 :   ulong l = FD_LAYOUT_INIT;
     162           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     163           0 :   l = FD_LAYOUT_APPEND( l, fd_http_server_align(),  http_fp );
     164           0 :   l = FD_LAYOUT_APPEND( l, fd_gui_peers_align(),    fd_gui_peers_footprint( http_param.max_ws_connection_cnt ) );
     165           0 :   l = FD_LAYOUT_APPEND( l, fd_gui_align(),          fd_gui_footprint( tile->gui.tile_cnt ) );
     166           0 :   l = FD_LAYOUT_APPEND( l, fd_gui_store_align(),       fd_gui_store_footprint( tile->gui.db_size_gib<<30, fd_gui_hist_db_cnt(), fd_gui_hist_db_descs( tile->gui.db_size_gib<<30 ) ) );
     167           0 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),        fd_alloc_footprint() );
     168           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     169           0 : }
     170             : 
     171             : FD_FN_PURE static inline ulong
     172           0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
     173           0 :   return 256UL * (1UL<<20UL); /* 256MiB of heap space for the cJSON allocator */
     174           0 : }
     175             : 
     176             : static inline void
     177           0 : during_housekeeping( fd_gui_ctx_t * ctx ) {
     178           0 :   ctx->ref_wallclock = fd_log_wallclock();
     179           0 :   ctx->ref_tickcount = fd_tickcount();
     180             : 
     181           0 :   if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) {
     182           0 :     fd_clock_tile_recal( ctx->clock );
     183           0 :   }
     184             : 
     185           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     186           0 :     fd_gui_set_identity( ctx->gui, ctx->keyswitch->bytes );
     187           0 :     fd_gui_peers_handle_identity_change( ctx->peers );
     188           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     189           0 :   }
     190           0 : }
     191             : 
     192             : static inline void
     193           0 : metrics_write( fd_gui_ctx_t * ctx ) {
     194           0 :   FD_MGAUGE_SET( GUI, CONN_ACTIVE, ctx->gui_server->metrics.connection_cnt );
     195           0 :   FD_MGAUGE_SET( GUI, WEBSOCKET_CONN_ACTIVE, ctx->gui_server->metrics.ws_connection_cnt );
     196             : 
     197           0 :   FD_MCNT_SET( GUI, WEBSOCKET_FRAME_TX,     ctx->gui_server->metrics.frames_written );
     198           0 :   FD_MCNT_SET( GUI, WEBSOCKET_FRAME_RX, ctx->gui_server->metrics.frames_read );
     199             : 
     200           0 :   FD_MCNT_SET( GUI, BYTES_WRITTEN, ctx->gui_server->metrics.bytes_written );
     201           0 :   FD_MCNT_SET( GUI, BYTES_READ,    ctx->gui_server->metrics.bytes_read );
     202             : 
     203           0 :   ulong db_cnt = fd_gui_store_cnt( ctx->db );
     204             : 
     205           0 :   ulong used_bytes  [ FD_GUI_HIST_CNT ];
     206           0 :   ulong cap_bytes   [ FD_GUI_HIST_CNT ]; ulong free_bytes  [ FD_GUI_HIST_CNT ];
     207           0 :   ulong used_slots  [ FD_GUI_HIST_CNT ];
     208           0 :   ulong cap_slots   [ FD_GUI_HIST_CNT ]; ulong free_slots  [ FD_GUI_HIST_CNT ];
     209           0 :   memset( used_bytes, 0, sizeof(used_bytes) );
     210           0 :   memset( cap_bytes,  0, sizeof(cap_bytes)  ); memset( free_bytes, 0, sizeof(free_bytes) );
     211           0 :   memset( used_slots, 0, sizeof(used_slots) );
     212           0 :   memset( cap_slots,  0, sizeof(cap_slots)  ); memset( free_slots, 0, sizeof(free_slots) );
     213             : 
     214           0 :   for( ulong i=0UL; i<db_cnt; i++ ) {
     215           0 :     fd_gui_store_ring_stats( ctx->db, i,
     216           0 :                              &used_bytes[ i ],
     217           0 :                              &cap_bytes[ i ],  &free_bytes[ i ],
     218           0 :                              &used_slots[ i ],
     219           0 :                              &cap_slots[ i ],  &free_slots[ i ] );
     220           0 :   }
     221             : 
     222           0 :   FD_MGAUGE_ENUM_COPY( GUI, DB_USED_BYTES,       used_bytes );
     223           0 :   FD_MGAUGE_ENUM_COPY( GUI, DB_CAPACITY_BYTES,   cap_bytes  );
     224           0 :   FD_MGAUGE_ENUM_COPY( GUI, DB_FREE_BYTES,       free_bytes );
     225           0 :   FD_MGAUGE_ENUM_COPY( GUI, DB_RECORD_USED,      used_slots );
     226           0 :   FD_MGAUGE_ENUM_COPY( GUI, DB_RECORD_CAPACITY,  cap_slots  );
     227           0 :   FD_MGAUGE_ENUM_COPY( GUI, DB_RECORD_FREE,      free_slots );
     228             : 
     229           0 :   fd_gui_store_metrics_t const * dm = fd_gui_store_metrics( ctx->db );
     230           0 :   FD_MCNT_ENUM_COPY( GUI, DB_KV_QUERIED,         dm->kv_lookups      );
     231           0 :   FD_MCNT_ENUM_COPY( GUI, DB_TS_RECORD_APPENDED, dm->ts_appends      );
     232           0 :   FD_MCNT_ENUM_COPY( GUI, DB_TS_SCANNED,         dm->ts_reads        );
     233           0 :   FD_MCNT_ENUM_COPY( GUI, DB_TS_RECORD_READ,     dm->ts_read_records );
     234           0 :   FD_MCNT_ENUM_COPY( GUI, DB_RECORD_EVICTED,     dm->evict_records   );
     235           0 :   FD_MCNT_ENUM_COPY( GUI, DB_EVICTION,           dm->evicts          );
     236           0 :   FD_MCNT_ENUM_COPY( GUI, DB_REGION_CLAIMED,     dm->region_grows    );
     237           0 :   fd_gui_hist_metrics_t const * hist = fd_gui_hist_metrics( ctx->gui );
     238           0 :   static ulong const zero_hist_metrics[ FD_GUI_HIST_CNT ] = { 0UL };
     239           0 :   ulong const * hist_map_full = hist ? hist->map_full : zero_hist_metrics;
     240           0 :   ulong const * hist_reserves = hist ? hist->reserves  : zero_hist_metrics;
     241           0 :   FD_MCNT_ENUM_COPY( GUI, DB_REGION_RECLAIMED,   dm->region_reclaims );
     242           0 :   FD_MCNT_ENUM_COPY( GUI, DB_MAP_FULL,           hist_map_full );
     243           0 :   FD_MCNT_ENUM_COPY( GUI, DB_FORCED_EVICTION,    hist_reserves );
     244           0 : }
     245             : 
     246             : static void
     247             : before_credit( fd_gui_ctx_t *      ctx,
     248             :                fd_stem_context_t * stem,
     249           0 :                int *               charge_busy ) {
     250           0 :   (void)stem;
     251             : 
     252           0 :   ctx->idle_cnt++;
     253           0 :   if( FD_LIKELY( ctx->idle_cnt<2UL*ctx->in_cnt ) ) return;
     254           0 :   ctx->idle_cnt = 0UL;
     255             : 
     256           0 :   int charge_busy_server = 0;
     257           0 :   long now = fd_tickcount();
     258           0 :   if( FD_UNLIKELY( now>=ctx->next_poll_deadline ) ) {
     259           0 :     charge_busy_server = fd_http_server_poll( ctx->gui_server, 0 );
     260           0 :     ctx->next_poll_deadline = fd_tickcount() + (long)(ctx->tick_per_ns * 128L * 1000L);
     261           0 :   }
     262             : 
     263           0 :   int charge_poll = 0;
     264           0 :   charge_poll |= fd_gui_poll( ctx->gui, fd_clock_tile_now( ctx->clock ) );
     265           0 :   charge_poll |= fd_gui_peers_poll( ctx->peers, fd_clock_tile_now( ctx->clock ) );
     266             : 
     267           0 :   *charge_busy = charge_busy_server | charge_poll;
     268           0 : }
     269             : 
     270             : static int
     271             : before_frag( fd_gui_ctx_t * ctx,
     272             :              ulong          in_idx,
     273             :              ulong          seq,
     274           0 :              ulong          sig ) {
     275           0 :   (void)seq;
     276             : 
     277             :   /* Ignore "done draining banks" and "reduce microblock bound" signals from pack->poh */
     278           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PACK_POH && (sig==FD_PACK_MSG_DONE_DRAINING || sig==FD_PACK_MSG_REDUCE_MB_BOUND) ) ) return 1;
     279             : 
     280           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP_OUT &&
     281           0 :                  (sig==FD_GOSSIP_UPDATE_TAG_WFS_DONE || sig==FD_GOSSIP_UPDATE_TAG_PEER_SATURATED) ) ) return 1;
     282           0 :   return 0;
     283           0 : }
     284             : 
     285             : static inline void
     286             : during_frag( fd_gui_ctx_t * ctx,
     287             :              ulong          in_idx,
     288             :              ulong          seq FD_PARAM_UNUSED,
     289             :              ulong          sig,
     290             :              ulong          chunk,
     291             :              ulong          sz,
     292           0 :              ulong          ctl ) {
     293           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_TOWER_OUT ) ) return; /* process in returnable_frag */
     294             : 
     295           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
     296             : 
     297           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_EPOCH ) ) {
     298           0 :     fd_epoch_info_msg_t * epoch_info = (fd_epoch_info_msg_t *)src;
     299           0 :     FD_TEST( epoch_info->staked_vote_cnt<=MAX_COMPRESSED_STAKE_WEIGHTS );
     300           0 :     FD_TEST( epoch_info->staked_id_cnt<=MAX_SHRED_DESTS );
     301           0 :     sz = fd_epoch_info_msg_sz( epoch_info->staked_vote_cnt, epoch_info->staked_id_cnt );
     302           0 :   }
     303             : 
     304           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GENESI_OUT ) ) {
     305           0 :     sz = sig;
     306           0 :   }
     307             : 
     308           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY_OUT ) ) {
     309           0 :     if( FD_LIKELY( sig!=REPLAY_SIG_SLOT_COMPLETED &&
     310           0 :                    sig!=REPLAY_SIG_BECAME_LEADER  &&
     311           0 :                    sig!=REPLAY_SIG_ROOT_ADVANCED  &&
     312           0 :                    sig!=REPLAY_SIG_OC_ADVANCED ) ) return;
     313           0 :   }
     314             : 
     315           0 :   if( FD_UNLIKELY( (sz>0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark)) || sz>ctx->in[ in_idx ].mtu ) )
     316           0 :     FD_LOG_ERR(( "in_kind %lu chunk %lu %lu corrupt, not in range [%lu,%lu] or too large (%lu)", ctx->in_kind[ in_idx ], chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark, ctx->in[ in_idx ].mtu ));
     317             : 
     318           0 :   switch( ctx->in_kind[ in_idx ] ) {
     319           0 :     case IN_KIND_REPAIR_NET: {
     320           0 :       ctx->parsed.repair_net.slot = ULONG_MAX;
     321           0 :       uchar * payload;
     322           0 :       ulong payload_sz;
     323           0 :       if( FD_LIKELY( fd_ip4_udp_hdr_strip( src, sz, &payload, &payload_sz, NULL, NULL, NULL ) ) ) {
     324           0 :         fd_repair_msg_t const * msg = (fd_repair_msg_t const *)fd_type_pun_const( payload );
     325           0 :         if( FD_LIKELY( msg->kind==FD_REPAIR_KIND_SHRED ) ) {
     326           0 :           if( FD_UNLIKELY( msg->shred.slot==0 ) ) break;
     327           0 :           ctx->parsed.repair_net.slot = msg->shred.slot;
     328           0 :           ctx->parsed.repair_net.shred_idx = msg->shred.shred_idx;
     329           0 :         }
     330           0 :       }
     331           0 :       break;
     332           0 :     }
     333           0 :     case IN_KIND_NET_GOSSVF: {
     334           0 :       FD_TEST( sz<=sizeof(ctx->parsed.net_gossvf) );
     335           0 :       uchar const * net_src = fd_net_rx_translate_frag( &ctx->net_in_bounds[ in_idx ], chunk, ctl, sz );
     336           0 :       fd_memcpy( ctx->parsed.net_gossvf, net_src, sz );
     337           0 :       break;
     338           0 :     }
     339           0 :     case IN_KIND_GOSSIP_NET: {
     340           0 :       FD_TEST( sz<=sizeof(ctx->parsed.gossip_net) );
     341           0 :       fd_memcpy( ctx->parsed.gossip_net, src, sz );
     342           0 :       break;
     343           0 :     }
     344           0 :   }
     345             : 
     346           0 :   ctx->chunk = chunk;
     347           0 : }
     348             : 
     349             : static inline void
     350             : after_frag( fd_gui_ctx_t *      ctx,
     351             :             ulong               in_idx,
     352             :             ulong               seq,
     353             :             ulong               sig,
     354             :             ulong               sz,
     355             :             ulong               tsorig,
     356             :             ulong               tspub,
     357           0 :             fd_stem_context_t * stem ) {
     358           0 :   (void)seq; (void)stem;
     359             : 
     360           0 :   if( FD_LIKELY( ctx->in_reliable[ in_idx ] ) ) ctx->idle_cnt = 0UL;
     361             : 
     362           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
     363             : 
     364           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_TOWER_OUT ) ) return; /* process in returnable_frag */
     365             : 
     366           0 :   switch( ctx->in_kind[ in_idx ] ) {
     367           0 :     case IN_KIND_EXECRP_REPLAY: {
     368           0 :       if( FD_LIKELY( sig>>32==FD_EXECRP_TT_TXN_EXEC ) ) {
     369           0 :         fd_execrp_task_done_msg_t * msg = (fd_execrp_task_done_msg_t *)src;
     370             : 
     371           0 :         long tickcount = fd_tickcount();
     372           0 :         long tsorig_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, tickcount ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     373           0 :         long tspub_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, tickcount ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     374           0 :         fd_gui_handle_exec_txn_done( ctx->gui, msg->txn_exec->slot, msg->txn_exec->start_shred_idx, msg->txn_exec->end_shred_idx, tsorig_ns, tspub_ns, fd_clock_tile_now( ctx->clock ) );
     375             : 
     376           0 :         int txn_succeeded = msg->txn_exec->is_committable && !msg->txn_exec->is_fees_only && !msg->txn_exec->txn_err;
     377           0 :         if( FD_UNLIKELY( msg->txn_exec->vote.slot!=ULONG_MAX && txn_succeeded ) ) {
     378           0 :           fd_gui_peers_handle_vote( ctx->peers,
     379           0 :                                     msg->txn_exec->vote.vote_acct,
     380           0 :                                     msg->txn_exec->vote.slot,
     381           0 :                                     !memcmp(ctx->gui->summary.identity_key->uc, msg->txn_exec->vote.identity->uc, sizeof(fd_pubkey_t) ) );
     382           0 :         }
     383           0 :       }
     384             : 
     385           0 :       break;
     386           0 :     }
     387           0 :     case IN_KIND_REPLAY_OUT: {
     388           0 :       if( FD_UNLIKELY( sig==REPLAY_SIG_SLOT_COMPLETED ) ) {
     389           0 :         fd_replay_slot_completed_t const * slot_completed =  (fd_replay_slot_completed_t const *)src;
     390             : 
     391           0 :         fd_gui_peers_update_delinquency( ctx->peers, fd_clock_tile_now( ctx->clock ) );
     392             : 
     393           0 :         fd_gui_handle_replay_update( ctx->gui, slot_completed, ctx->peers->slot_voted, fd_clock_tile_now( ctx->clock ) );
     394           0 :       } else if( FD_UNLIKELY( sig==REPLAY_SIG_BECAME_LEADER ) ) {
     395           0 :         fd_became_leader_t * became_leader = (fd_became_leader_t *)src;
     396           0 :         fd_gui_became_leader( ctx->gui, became_leader->slot, became_leader->slot_start_ns, became_leader->slot_end_ns, became_leader->limits.slot_max_cost, became_leader->max_microblocks_in_slot, became_leader->bank_seq );
     397           0 :       } else if( FD_UNLIKELY( sig==REPLAY_SIG_ROOT_ADVANCED ) ) {
     398           0 :         fd_replay_root_advanced_t const * ra = (fd_replay_root_advanced_t const *)src;
     399           0 :         fd_gui_handle_root_advanced( ctx->gui, ra->slot, ra->bank_seq, fd_clock_tile_now( ctx->clock ) );
     400           0 :       } else if( FD_UNLIKELY( sig==REPLAY_SIG_OC_ADVANCED ) ) {
     401           0 :         fd_replay_oc_advanced_t const * oc = (fd_replay_oc_advanced_t const *)src;
     402           0 :         fd_gui_handle_oc_advanced( ctx->gui, oc->slot, oc->bank_seq, fd_clock_tile_now( ctx->clock ) );
     403           0 :       } else {
     404           0 :         return;
     405           0 :       }
     406           0 :       break;
     407           0 :     }
     408           0 :     case IN_KIND_EPOCH: {
     409           0 :       fd_epoch_info_msg_t * epoch_info = (fd_epoch_info_msg_t *)src;
     410           0 :       fd_gui_handle_epoch_info( ctx->gui, epoch_info, fd_clock_tile_now( ctx->clock ) );
     411           0 :       fd_gui_peers_handle_epoch_info( ctx->peers, epoch_info, fd_clock_tile_now( ctx->clock ) );
     412           0 :       break;
     413           0 :     }
     414           0 :     case IN_KIND_SNAPIN: {
     415           0 :       fd_gui_peers_handle_config_account( ctx->peers, src, sz );
     416           0 :       break;
     417           0 :     }
     418           0 :     case IN_KIND_SNAPIN_MANIF: {
     419           0 :       if( fd_ssmsg_sig_message( sig )==FD_SSMSG_DONE ) {
     420           0 :         fd_gui_peers_commit_snapshot_manifest( ctx->peers );
     421           0 :       } else {
     422           0 :         fd_gui_stage_snapshot_manifest( ctx->gui, (fd_snapshot_manifest_t const *)src );
     423           0 :         fd_gui_peers_stage_snapshot_manifest( ctx->peers, (fd_snapshot_manifest_t const *)src, fd_clock_tile_now( ctx->clock ) );
     424           0 :       }
     425           0 :       break;
     426           0 :     }
     427           0 :     case IN_KIND_GENESI_OUT: {
     428           0 :       fd_genesis_meta_t const * meta = (fd_genesis_meta_t const *)src;
     429           0 :       fd_gui_handle_genesis_hash( ctx->gui, &meta->genesis_hash );
     430           0 :       break;
     431           0 :     }
     432           0 :     case IN_KIND_SHRED_OUT: {
     433           0 :       long tsorig_nanos = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     434           0 :       uint sig_src      = fd_shred_sig_src( sig );
     435           0 :       if( FD_LIKELY( sig_src==SHRED_SIG_SRC_TURBINE || sig_src==SHRED_SIG_SRC_REPAIR || sig_src==SHRED_SIG_SRC_BAD_REPAIR ) ) {
     436           0 :         fd_shred_base_t const * msg = (fd_shred_base_t const *)fd_type_pun_const( src );
     437           0 :         ulong slot      = msg->shred.slot;
     438           0 :         ulong shred_idx = msg->shred.idx;
     439           0 :         int is_turbine  = sig_src==SHRED_SIG_SRC_TURBINE;
     440             :         /* tsorig is the timestamp when the shred was received by the shred tile */
     441           0 :         fd_gui_handle_shred( ctx->gui, slot, shred_idx, is_turbine, tsorig_nanos, fd_clock_tile_now( ctx->clock ) );
     442           0 :       }
     443           0 :       if( FD_UNLIKELY( sig==SHRED_SIG_FEC_COMPLETE_LEADER ) ) {
     444           0 :         fd_fec_complete_t const * complete_msg = (fd_fec_complete_t const *)fd_type_pun_const( src );
     445           0 :         fd_gui_handle_leader_fec( ctx->gui, complete_msg->last_shred_hdr.slot, FD_FEC_SHRED_CNT, complete_msg->last_shred_hdr.data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE, tsorig_nanos, fd_clock_tile_now( ctx->clock ) );
     446           0 :       }
     447           0 :       break;
     448           0 :     }
     449           0 :     case IN_KIND_SNAPCT: {
     450           0 :       fd_gui_handle_snapshot_update( ctx->gui, (fd_snapct_update_t *)src );
     451           0 :       break;
     452           0 :     }
     453           0 :     case IN_KIND_REPAIR_NET: {
     454           0 :       if( FD_UNLIKELY( ctx->parsed.repair_net.slot==ULONG_MAX ) ) break;
     455           0 :       long tsorig_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     456           0 :       fd_gui_handle_repair_request( ctx->gui, ctx->parsed.repair_net.slot, ctx->parsed.repair_net.shred_idx, tsorig_ns );
     457           0 :       break;
     458           0 :     }
     459           0 :     case IN_KIND_NET_GOSSVF: {
     460           0 :       uchar * payload;
     461           0 :       ulong payload_sz;
     462           0 :       fd_ip4_hdr_t * ip4_hdr;
     463           0 :       fd_udp_hdr_t * udp_hdr;
     464           0 :       if( FD_LIKELY( fd_ip4_udp_hdr_strip( ctx->parsed.net_gossvf, sz, &payload, &payload_sz, NULL, &ip4_hdr, &udp_hdr ) ) ) {
     465           0 :         fd_gossip_socket_t socket = {
     466           0 :           .is_ipv6 = 0,
     467           0 :           .ip4 = ip4_hdr->saddr,
     468           0 :           .port = udp_hdr->net_sport,
     469           0 :         };
     470           0 :         fd_gui_peers_handle_gossip_message( ctx->peers, payload, payload_sz, &socket, 1 );
     471           0 :       }
     472           0 :       break;
     473           0 :     }
     474           0 :     case IN_KIND_GOSSIP_NET: {
     475           0 :       uchar * payload;
     476           0 :       ulong payload_sz;
     477           0 :       fd_ip4_hdr_t * ip4_hdr;
     478           0 :       fd_udp_hdr_t * udp_hdr;
     479           0 :       FD_TEST( fd_ip4_udp_hdr_strip( ctx->parsed.gossip_net, sz, &payload, &payload_sz, NULL, &ip4_hdr, &udp_hdr ) );
     480           0 :       fd_gossip_socket_t socket = {
     481           0 :         .is_ipv6 = 0,
     482           0 :         .ip4 = ip4_hdr->daddr,
     483           0 :         .port = udp_hdr->net_dport,
     484           0 :       };
     485           0 :       fd_gui_peers_handle_gossip_message( ctx->peers, payload, payload_sz, &socket, 0 );
     486           0 :       break;
     487           0 :     }
     488           0 :     case IN_KIND_GOSSIP_OUT: {
     489           0 :       fd_gui_peers_handle_gossip_update( ctx->peers, (fd_gossip_update_message_t *)src, fd_clock_tile_now( ctx->clock ) );
     490           0 :       break;
     491           0 :     }
     492           0 :     case IN_KIND_PACK_POH: {
     493           0 :       fd_gui_unbecame_leader( ctx->gui, fd_disco_execle_sig_slot( sig ), (fd_done_packing_t const *)src, fd_clock_tile_now( ctx->clock ) );
     494           0 :       break;
     495           0 :     }
     496           0 :     case IN_KIND_PACK_EXECLE: {
     497           0 :       FD_TEST( sz>=sizeof(fd_microblock_execle_trailer_t) );
     498           0 :       FD_TEST( (sz-sizeof(fd_microblock_execle_trailer_t))%sizeof(fd_txn_e_t)==0UL );
     499             : 
     500           0 :       if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK ) ) {
     501           0 :         fd_microblock_execle_trailer_t trailer[1];
     502           0 :         fd_memcpy( trailer, src+sz-sizeof(fd_microblock_execle_trailer_t), sizeof(fd_microblock_execle_trailer_t) );
     503           0 :         long tspub_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     504           0 :         fd_gui_microblock_execution_begin( ctx->gui,
     505           0 :                                           tspub_ns,
     506           0 :                                           fd_disco_poh_sig_slot( sig ),
     507           0 :                                           (fd_txn_e_t *)src,
     508           0 :                                           (sz-sizeof( fd_microblock_execle_trailer_t ))/sizeof( fd_txn_e_t ),
     509           0 :                                           (uint)trailer->microblock_idx,
     510           0 :                                           trailer->pack_txn_idx,
     511           0 :                                           trailer->bank_seq,
     512           0 :                                           fd_clock_tile_now( ctx->clock ) );
     513           0 :       } else {
     514           0 :         FD_LOG_ERR(( "unexpected poh packet type %lu", fd_disco_poh_sig_pkt_type( sig ) ));
     515           0 :       }
     516           0 :       break;
     517           0 :     }
     518           0 :     case IN_KIND_EXECLE_POH: {
     519           0 :       FD_TEST( sz>=sizeof(fd_microblock_trailer_t) );
     520           0 :       FD_TEST( (sz-sizeof(fd_microblock_trailer_t))%sizeof(fd_txn_p_t)==0UL );
     521             : 
     522           0 :       fd_microblock_trailer_t trailer[1];
     523           0 :       fd_memcpy( trailer, src+sz-sizeof(fd_microblock_trailer_t), sizeof(fd_microblock_trailer_t) );
     524           0 :       long tspub_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     525           0 :       ulong txn_cnt = (sz-sizeof( fd_microblock_trailer_t ))/sizeof(fd_txn_p_t);
     526           0 :       fd_gui_microblock_execution_end( ctx->gui,
     527           0 :                                       tspub_ns,
     528           0 :                                       ctx->in_bank_idx[ in_idx ],
     529           0 :                                       fd_disco_execle_sig_slot( sig ),
     530           0 :                                       txn_cnt,
     531           0 :                                       (fd_txn_p_t *)src,
     532           0 :                                       trailer->pack_txn_idx,
     533           0 :                                       trailer->txn_ns_dt,
     534           0 :                                       trailer->tips,
     535           0 :                                       trailer->bank_seq,
     536           0 :                                       fd_clock_tile_now( ctx->clock ) );
     537           0 :       break;
     538           0 :     }
     539           0 :     case IN_KIND_BUNDLE: {
     540           0 :       fd_gui_handle_block_engine_update( ctx->gui, (fd_bundle_block_engine_update_t *)src );
     541           0 :       break;
     542           0 :     }
     543           0 :     default: FD_LOG_ERR(( "unexpected in_kind %lu", ctx->in_kind[ in_idx ] ));
     544           0 :   }
     545           0 : }
     546             : 
     547             : static int
     548             : returnable_frag( fd_gui_ctx_t *      ctx,
     549             :                  ulong               in_idx,
     550             :                  ulong               seq    FD_PARAM_UNUSED,
     551             :                  ulong               sig,
     552             :                  ulong               chunk,
     553             :                  ulong               sz,
     554             :                  ulong               ctl    FD_PARAM_UNUSED,
     555             :                  ulong               tsorig FD_PARAM_UNUSED,
     556             :                  ulong               tspub  FD_PARAM_UNUSED,
     557           0 :                  fd_stem_context_t * stem   FD_PARAM_UNUSED ) {
     558           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]!=IN_KIND_TOWER_OUT ) ) return 0; /* process in {before|during|after}_frag */
     559             : 
     560           0 :   if( FD_UNLIKELY( (sz>0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark)) || sz>ctx->in[ in_idx ].mtu ) )
     561           0 :     FD_LOG_ERR(( "in_kind %lu chunk %lu %lu corrupt, not in range [%lu,%lu] or too large (%lu)", ctx->in_kind[ in_idx ], chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark, ctx->in[ in_idx ].mtu ));
     562             : 
     563           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
     564             : 
     565           0 :   if( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_DONE ) ) {
     566           0 :     FD_TEST( sz>=sizeof(fd_tower_slot_done_t) );
     567           0 :     fd_tower_slot_done_t const * tower = (fd_tower_slot_done_t const *)src;
     568           0 :     if( FD_UNLIKELY( !(tower->reset_slot==ULONG_MAX || !!fd_gui_slot_get( ctx->gui, tower->reset_slot, tower->reset_bank_seq )) ) ) return 1; /* block not replayed yet: defer polling */
     569           0 :     fd_gui_handle_tower_update( ctx->gui, tower, fd_clock_tile_now( ctx->clock ) );
     570           0 :   }
     571             : 
     572           0 :   return 0;
     573           0 : }
     574             : 
     575             : static fd_http_server_response_t
     576           0 : gui_http_request( fd_http_server_request_t const * request ) {
     577           0 :   if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
     578           0 :     return (fd_http_server_response_t){
     579           0 :       .status = 405,
     580           0 :     };
     581           0 :   }
     582             : 
     583           0 :   if( FD_LIKELY( !strcmp( request->path, "/websocket" ) ) ) {
     584           0 :     return (fd_http_server_response_t){
     585           0 :       .status            = 200,
     586           0 :       .upgrade_websocket = 1,
     587           0 : #ifdef FD_HAS_ZSTD
     588           0 :       .compress_websocket = request->headers.compress_websocket,
     589             : #else
     590             :       .compress_websocket = 0,
     591             : #endif
     592           0 :     };
     593           0 :   } else if( FD_LIKELY( !strcmp( request->path, "/favicon.svg" ) ) ) {
     594           0 :     return (fd_http_server_response_t){
     595           0 :       .status            = 200,
     596           0 :       .static_body       = firedancer_svg,
     597           0 :       .static_body_len   = firedancer_svg_sz,
     598           0 :       .content_type      = "image/svg+xml",
     599           0 :       .upgrade_websocket = 0,
     600           0 :     };
     601           0 :   }
     602             : 
     603           0 :   int is_vite_page = !strcmp( request->path, "/" ) ||
     604           0 :                      !strcmp( request->path, "/slotDetails" ) ||
     605           0 :                      !strcmp( request->path, "/leaderSchedule" ) ||
     606           0 :                      !strcmp( request->path, "/gossip") ||
     607           0 :                      !strcmp( request->path, "/accounts") ||
     608           0 :                      !strncmp( request->path, "/?", strlen("/?") ) ||
     609           0 :                      !strncmp( request->path, "/slotDetails?", strlen("/slotDetails?") ) ||
     610           0 :                      !strncmp( request->path, "/leaderSchedule?", strlen("/leaderSchedule?") ) ||
     611           0 :                      !strncmp( request->path, "/gossip?", strlen("/gossip?") ) ||
     612           0 :                      !strncmp( request->path, "/accounts?", strlen("/accounts?") );
     613             : 
     614           0 :   for( fd_http_static_file_t const * f = STATIC_FILES; f->name; f++ ) {
     615           0 :     if( !strcmp( request->path, f->name ) ||
     616           0 :         (!strcmp( f->name, "/index.html" ) && is_vite_page) ) {
     617           0 :       char const * content_type = NULL;
     618             : 
     619           0 :       char const * ext = strrchr( f->name, '.' );
     620           0 :       if( FD_LIKELY( ext ) ) {
     621           0 :         if( !strcmp( ext, ".html" ) ) content_type = "text/html; charset=utf-8";
     622           0 :         else if( !strcmp( ext, ".css" ) ) content_type = "text/css";
     623           0 :         else if( !strcmp( ext, ".js" ) ) content_type = "application/javascript";
     624           0 :         else if( !strcmp( ext, ".svg" ) ) content_type = "image/svg+xml";
     625           0 :         else if( !strcmp( ext, ".woff" ) ) content_type = "font/woff";
     626           0 :         else if( !strcmp( ext, ".woff2" ) ) content_type = "font/woff2";
     627           0 :       }
     628             : 
     629           0 :       char const * cache_control = NULL;
     630           0 :       if( FD_LIKELY( !strncmp( request->path, "/assets", 7 ) ) ) cache_control = "public, max-age=31536000, immutable";
     631           0 :       else if( FD_LIKELY( !strcmp( f->name, "/index.html" ) ) )  cache_control = "no-cache";
     632             : 
     633           0 :       const uchar * data = f->data;
     634           0 :       ulong data_len = *(f->data_len);
     635             : 
     636           0 :       int accepts_zstd = 0;
     637           0 :       if( FD_LIKELY( request->headers.accept_encoding ) ) {
     638           0 :         accepts_zstd = !!strstr( request->headers.accept_encoding, "zstd" );
     639           0 :       }
     640             : 
     641           0 :       int accepts_gzip = 0;
     642           0 :       if( FD_LIKELY( request->headers.accept_encoding ) ) {
     643           0 :         accepts_gzip = !!strstr( request->headers.accept_encoding, "gzip" );
     644           0 :       }
     645             : 
     646           0 :       char const * content_encoding = NULL;
     647           0 :       if( FD_LIKELY( accepts_zstd && f->zstd_data ) ) {
     648           0 :         content_encoding = "zstd";
     649           0 :         data = f->zstd_data;
     650           0 :         data_len = *(f->zstd_data_len);
     651           0 :       } else if( FD_LIKELY( accepts_gzip && f->gzip_data ) ) {
     652           0 :         content_encoding = "gzip";
     653           0 :         data = f->gzip_data;
     654           0 :         data_len = *(f->gzip_data_len);
     655           0 :       }
     656             : 
     657           0 :       return (fd_http_server_response_t){
     658           0 :         .status            = 200,
     659           0 :         .static_body       = data,
     660           0 :         .static_body_len   = data_len,
     661           0 :         .content_type      = content_type,
     662           0 :         .cache_control     = cache_control,
     663           0 :         .content_encoding  = content_encoding,
     664           0 :         .upgrade_websocket = 0,
     665           0 :       };
     666           0 :     }
     667           0 :   }
     668             : 
     669           0 :   return (fd_http_server_response_t){
     670           0 :     .status            = 404,
     671           0 :   };
     672           0 : }
     673             : 
     674             : static void
     675             : gui_ws_open( ulong  conn_id,
     676           0 :              void * _ctx ) {
     677           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     678             : 
     679           0 :   fd_gui_ws_open( ctx->gui, conn_id, fd_clock_tile_now( ctx->clock ) );
     680           0 :   fd_gui_peers_ws_open( ctx->peers, conn_id, fd_clock_tile_now( ctx->clock ) );
     681           0 : }
     682             : 
     683             : static void
     684             : gui_ws_close( ulong  conn_id,
     685             :               int    reason,
     686           0 :               void * _ctx ) {
     687           0 :   (void) reason;
     688           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     689           0 :   fd_gui_peers_ws_close( ctx->peers, conn_id );
     690           0 : }
     691             : 
     692             : static void
     693             : gui_ws_message( ulong         ws_conn_id,
     694             :                 uchar const * data,
     695             :                 ulong         data_len,
     696           0 :                 void *        _ctx ) {
     697           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     698             : 
     699           0 :   int reason = fd_gui_ws_message( ctx->gui, ws_conn_id, data, data_len );
     700           0 :   if( FD_UNLIKELY( reason==FD_HTTP_SERVER_CONNECTION_CLOSE_UNKNOWN_METHOD ) ) reason = fd_gui_peers_ws_message( ctx->peers, ws_conn_id, data, data_len );
     701             : 
     702           0 :   if( FD_UNLIKELY( reason<0 ) ) fd_http_server_ws_close( ctx->gui_server, ws_conn_id, reason );
     703           0 : }
     704             : 
     705             : static void
     706             : privileged_init( fd_topo_t const *      topo,
     707           0 :                  fd_topo_tile_t const * tile ) {
     708           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     709             : 
     710           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     711             : 
     712           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     713           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     714           0 :   fd_http_server_t * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
     715           0 :                      FD_SCRATCH_ALLOC_APPEND( l, fd_gui_peers_align(),    fd_gui_peers_footprint( http_param.max_ws_connection_cnt ) );
     716           0 :                      FD_SCRATCH_ALLOC_APPEND( l, fd_gui_align(),          fd_gui_footprint( tile->gui.tile_cnt )                     );
     717           0 :   void * _db       = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_store_align(),       fd_gui_store_footprint( tile->gui.db_size_gib<<30, fd_gui_hist_db_cnt(), fd_gui_hist_db_descs( tile->gui.db_size_gib<<30 ) ) );
     718             : 
     719           0 :   fd_http_server_callbacks_t gui_callbacks = {
     720           0 :     .request    = gui_http_request,
     721           0 :     .ws_open    = gui_ws_open,
     722           0 :     .ws_close   = gui_ws_close,
     723           0 :     .ws_message = gui_ws_message,
     724           0 :   };
     725           0 :   ctx->gui_server = fd_http_server_join( fd_http_server_new( _gui, http_param, gui_callbacks, ctx ) );
     726           0 :   fd_http_server_listen( ctx->gui_server, tile->gui.listen_addr, tile->gui.listen_port );
     727             : 
     728           0 :   FD_LOG_NOTICE(( "gui server listening at %shttp://" FD_IP4_ADDR_FMT ":%u%s", fd_log_style_bold(), FD_IP4_ADDR_FMT_ARGS( tile->gui.listen_addr ), tile->gui.listen_port, fd_log_style_normal() ));
     729             : 
     730           0 :   ctx->db = fd_gui_store_join( fd_gui_store_new( _db, tile->gui.gui_database_path, tile->gui.db_size_gib<<30, fd_gui_hist_db_cnt(), fd_gui_hist_db_descs( tile->gui.db_size_gib<<30 ) ) );
     731           0 :   if( FD_UNLIKELY( !ctx->db ) ) FD_LOG_ERR(( "fd_gui_store_new(%s) failed; gui tile cannot start", tile->gui.gui_database_path ));
     732             : 
     733           0 :   if( FD_UNLIKELY( !strcmp( tile->gui.identity_key_path, "" ) ) )
     734           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     735             : 
     736           0 :   ctx->identity_key = fd_keyload_load( tile->gui.identity_key_path, /* pubkey only: */ 1 );
     737             : 
     738           0 :   if( FD_UNLIKELY( !strcmp( tile->gui.vote_key_path, "" ) ) ) {
     739           0 :     ctx->has_vote_key = 0;
     740           0 :   } else {
     741           0 :     ctx->has_vote_key = 1;
     742           0 :     if( FD_UNLIKELY( !fd_base58_decode_32( tile->gui.vote_key_path, (uchar *)ctx->vote_key->uc ) ) ) {
     743           0 :       const uchar * vote_key = fd_keyload_load( tile->gui.vote_key_path, /* pubkey only: */ 1 );
     744           0 :       fd_memcpy( (uchar *)ctx->vote_key->uc, vote_key, 32UL );
     745           0 :     }
     746           0 :   }
     747           0 : }
     748             : 
     749             : static void
     750             : unprivileged_init( fd_topo_t const *      topo,
     751           0 :                    fd_topo_tile_t const * tile ) {
     752           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     753             : 
     754           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     755           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     756           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t )                                    );
     757           0 :                        FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(),  fd_http_server_footprint( http_param )                    );
     758           0 :   void * _peers      = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_peers_align(),    fd_gui_peers_footprint( http_param.max_ws_connection_cnt) );
     759           0 :   void * _gui        = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_align(),          fd_gui_footprint( tile->gui.tile_cnt )                     );
     760           0 :                        FD_SCRATCH_ALLOC_APPEND( l, fd_gui_store_align(),       fd_gui_store_footprint( tile->gui.db_size_gib<<30, fd_gui_hist_db_cnt(), fd_gui_hist_db_descs( tile->gui.db_size_gib<<30 ) ) );
     761           0 :   void * _alloc      = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(),        fd_alloc_footprint()                                      );
     762             : 
     763           0 :   ctx->is_full_client = ULONG_MAX!=fd_topo_find_tile( topo, "repair", 0UL );
     764           0 :   ctx->snapshots_enabled = ULONG_MAX!=fd_topo_find_tile( topo, "snapct", 0UL );
     765             : 
     766           0 :   fd_clock_tile_init( ctx->clock );
     767             : 
     768           0 :   ctx->ref_wallclock = fd_log_wallclock();
     769           0 :   ctx->ref_tickcount = fd_tickcount();
     770           0 :   ctx->tick_per_ns = fd_tempo_tick_per_ns( NULL );
     771             : 
     772           0 :   ctx->topo = topo;
     773           0 :   ctx->peers = fd_gui_peers_join( fd_gui_peers_new( _peers, ctx->gui_server, ctx->topo, http_param.max_ws_connection_cnt, tile->gui.wfs_bank_hash, fd_clock_tile_now( ctx->clock ) ) );
     774             :   /* The accounts database is a full-client (Firedancer) feature only.
     775             :      Frankendancer has no accdb, so its topology leaves accdb_obj_id
     776             :      unset (ULONG_MAX) and the gui tile joins no accdb shmem.  The gui
     777             :      only reads partition stats from the shmem; it never reads account
     778             :      data from disk, so it does not join the accdb fd. */
     779           0 :   fd_accdb_shmem_t * accdb_shmem = NULL;
     780           0 :   if( FD_LIKELY( tile->gui.accdb_obj_id!=ULONG_MAX ) ) {
     781           0 :     void * accdb_shmem_raw = fd_topo_obj_laddr( topo, tile->gui.accdb_obj_id );
     782           0 :     FD_TEST( accdb_shmem_raw );
     783           0 :     accdb_shmem = fd_accdb_shmem_join( accdb_shmem_raw );
     784           0 :     FD_TEST( accdb_shmem );
     785           0 :   }
     786           0 :   ctx->gui   = fd_gui_join( fd_gui_new( _gui, ctx->gui_server, fd_version_cstr, tile->gui.cluster, ctx->identity_key, ctx->has_vote_key, ctx->vote_key->uc, ctx->is_full_client, ctx->snapshots_enabled, tile->gui.is_voting, tile->gui.schedule_strategy, tile->gui.wfs_bank_hash, tile->gui.expected_shred_version, tile->gui.accounts_database_path, tile->gui.gui_database_path, ctx->db, ctx->topo, accdb_shmem, fd_clock_tile_now( ctx->clock ) ) );
     787           0 :   FD_TEST( ctx->gui );
     788           0 :   FD_TEST( ctx->db );
     789             : 
     790           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
     791           0 :   FD_TEST( ctx->keyswitch );
     792             : 
     793           0 :   fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
     794           0 :   FD_TEST( alloc );
     795           0 :   cJSON_alloc_install( alloc );
     796             : 
     797           0 :   ctx->next_poll_deadline = fd_tickcount();
     798             : 
     799           0 :   ctx->idle_cnt = 0UL;
     800           0 :   ctx->in_cnt = tile->in_cnt;
     801             : 
     802           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     803           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     804           0 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     805             : 
     806           0 :     if( FD_LIKELY( !strcmp( link->name, "pack_execle"  ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_EXECLE;
     807           0 :     else if( FD_LIKELY( !strcmp( link->name, "pack_poh"     ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_POH;
     808           0 :     else if( FD_LIKELY( !strcmp( link->name, "execle_poh"   ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE_POH;
     809           0 :     else if( FD_LIKELY( !strcmp( link->name, "shred_out"    ) ) ) ctx->in_kind[ i ] = IN_KIND_SHRED_OUT;
     810           0 :     else if( FD_LIKELY( !strcmp( link->name, "net_gossvf"   ) ) ) {
     811           0 :       ctx->in_kind[ i ] = IN_KIND_NET_GOSSVF;
     812           0 :       fd_net_rx_bounds_init( &ctx->net_in_bounds[ i ], link->dcache );
     813           0 :     }
     814           0 :     else if( FD_LIKELY( !strcmp( link->name, "gossip_net"    ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_NET;
     815           0 :     else if( FD_LIKELY( !strcmp( link->name, "gossip_out"    ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_OUT;
     816           0 :     else if( FD_LIKELY( !strcmp( link->name, "snapct_gui"    ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPCT;
     817           0 :     else if( FD_LIKELY( !strcmp( link->name, "repair_net"    ) ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR_NET;
     818           0 :     else if( FD_LIKELY( !strcmp( link->name, "tower_out"     ) ) ) ctx->in_kind[ i ] = IN_KIND_TOWER_OUT;
     819           0 :     else if( FD_LIKELY( !strcmp( link->name, "replay_out"    ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY_OUT;
     820           0 :     else if( FD_LIKELY( !strcmp( link->name, "replay_epoch"  ) ) ) ctx->in_kind[ i ] = IN_KIND_EPOCH;
     821           0 :     else if( FD_LIKELY( !strcmp( link->name, "genesi_out"    ) ) ) ctx->in_kind[ i ] = IN_KIND_GENESI_OUT;
     822           0 :     else if( FD_LIKELY( !strcmp( link->name, "snapin_gui"    ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPIN;
     823           0 :     else if( FD_LIKELY( !strcmp( link->name, "snapin_manif"  ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPIN_MANIF;
     824           0 :     else if( FD_LIKELY( !strcmp( link->name, "execrp_replay" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECRP_REPLAY;
     825           0 :     else if( FD_LIKELY( !strcmp( link->name, "bundle_status"  ) ) ) ctx->in_kind[ i ] = IN_KIND_BUNDLE;
     826           0 :     else FD_LOG_ERR(( "gui tile has unexpected input link %lu %s", i, link->name ));
     827             : 
     828           0 :     if( FD_LIKELY( !strcmp( link->name, "execle_poh" ) ) ) {
     829           0 :       ulong producer = fd_topo_find_link_producer( topo, &topo->links[ tile->in_link_id[ i ] ] );
     830           0 :       ctx->in_bank_idx[ i ] = topo->tiles[ producer ].kind_id;
     831           0 :     }
     832             : 
     833           0 :     ctx->in_reliable[ i ] = tile->in_link_reliable[ i ];
     834           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
     835           0 :     ctx->in[ i ].mtu    = link->mtu;
     836           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
     837           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
     838           0 :   }
     839             : 
     840           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     841           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     842           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     843           0 : }
     844             : 
     845             : static ulong
     846             : populate_allowed_seccomp( fd_topo_t const *      topo,
     847             :                           fd_topo_tile_t const * tile,
     848             :                           ulong                  out_cnt,
     849           0 :                           struct sock_filter *   out ) {
     850           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     851           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     852           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     853             : 
     854           0 :   populate_sock_filter_policy_fd_gui_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)fd_http_server_fd( ctx->gui_server ), (uint)fd_gui_store_fd( ctx->db ) );
     855           0 :   return sock_filter_policy_fd_gui_tile_instr_cnt;
     856           0 : }
     857             : 
     858             : static ulong
     859             : populate_allowed_fds( fd_topo_t const *      topo,
     860             :                       fd_topo_tile_t const * tile,
     861             :                       ulong                  out_fds_cnt,
     862           0 :                       int *                  out_fds ) {
     863           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     864           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     865           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     866             : 
     867           0 :   if( FD_UNLIKELY( out_fds_cnt<4UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     868             : 
     869           0 :   ulong out_cnt = 0UL;
     870           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     871           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     872           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     873           0 :   out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->gui_server ); /* gui listen socket */
     874           0 :   out_fds[ out_cnt++ ] = fd_gui_store_fd( ctx->db ); /* historical store data file */
     875           0 :   return out_cnt;
     876           0 : }
     877             : 
     878             : static ulong
     879             : rlimit_file_cnt( fd_topo_t const *      topo FD_PARAM_UNUSED,
     880           0 :                  fd_topo_tile_t const * tile ) {
     881             :   /* pipefd, socket, stderr, logfile, guidb, and one spare for new accept() connections */
     882           0 :   ulong base = 6UL;
     883           0 :   return base + tile->gui.max_http_connections + tile->gui.max_websocket_connections;
     884           0 : }
     885             : 
     886           0 : #define STEM_BURST (2UL)
     887             : 
     888             : /* See explanation in fd_pack */
     889           0 : #define STEM_LAZY  (128L*3000L)
     890             : 
     891           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_gui_ctx_t
     892           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gui_ctx_t)
     893             : 
     894           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     895           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     896           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     897           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
     898           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     899           0 : #define STEM_CALLBACK_RETURNABLE_FRAG     returnable_frag
     900           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     901             : 
     902             : #include "../../disco/stem/fd_stem.c"
     903             : 
     904             : fd_topo_run_tile_t fd_tile_gui = {
     905             :   .name                     = "gui",
     906             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
     907             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     908             :   .populate_allowed_fds     = populate_allowed_fds,
     909             :   .scratch_align            = scratch_align,
     910             :   .scratch_footprint        = scratch_footprint,
     911             :   .loose_footprint          = loose_footprint,
     912             :   .privileged_init          = privileged_init,
     913             :   .unprivileged_init        = unprivileged_init,
     914             :   .run                      = stem_run,
     915             : };

Generated by: LCOV version 1.14