LCOV - code coverage report
Current view: top level - disco/gui - fd_gui_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 599 0.0 %
Date: 2025-11-16 04:48:36 Functions: 0 19 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             : /* The list of files used to serve the frontend is set here.  This is a
      12             :    global variable since is is populated at boot based on the
      13             :    `development.gui.frontend_release_channel` option and is accessed in
      14             :    the gui_http_request callback. */
      15             : static fd_http_static_file_t * STATIC_FILES;
      16             : 
      17             : #include <sys/socket.h> /* SOCK_CLOEXEC, SOCK_NONBLOCK needed for seccomp filter */
      18             : #include <stdlib.h>
      19             : 
      20             : #include "generated/fd_gui_tile_seccomp.h"
      21             : 
      22             : #include "../../disco/tiles.h"
      23             : #include "../../disco/keyguard/fd_keyload.h"
      24             : #include "../../disco/keyguard/fd_keyswitch.h"
      25             : #include "../../disco/gui/fd_gui.h"
      26             : #include "../../disco/plugin/fd_plugin.h"
      27             : #include "../../discof/replay/fd_exec.h"
      28             : #include "../../disco/metrics/fd_metrics.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 "../../ballet/json/cJSON_alloc.h"
      33             : #include "../../util/clock/fd_clock.h"
      34             : #include "../../discof/repair/fd_repair.h"
      35             : #include "../../discof/replay/fd_replay_tile.h"
      36             : #include "../../util/pod/fd_pod_format.h"
      37             : 
      38             : #include "../../flamenco/gossip/fd_gossip_private.h"
      39             : #include "../../flamenco/runtime/fd_bank.h"
      40             : 
      41           0 : #define IN_KIND_PLUGIN       ( 0UL)
      42           0 : #define IN_KIND_POH_PACK     ( 1UL)
      43           0 : #define IN_KIND_PACK_BANK    ( 2UL)
      44           0 : #define IN_KIND_PACK_POH     ( 3UL)
      45           0 : #define IN_KIND_BANK_POH     ( 4UL)
      46           0 : #define IN_KIND_SHRED_OUT    ( 5UL) /* firedancer only */
      47           0 : #define IN_KIND_NET_GOSSVF   ( 6UL) /* firedancer only */
      48           0 : #define IN_KIND_GOSSIP_NET   ( 7UL) /* firedancer only */
      49           0 : #define IN_KIND_GOSSIP_OUT   ( 8UL) /* firedancer only */
      50           0 : #define IN_KIND_SNAPCT       ( 9UL) /* firedancer only */
      51           0 : #define IN_KIND_REPAIR_NET   (10UL) /* firedancer only */
      52           0 : #define IN_KIND_TOWER_OUT    (11UL) /* firedancer only */
      53           0 : #define IN_KIND_REPLAY_OUT   (12UL) /* firedancer only */
      54           0 : #define IN_KIND_REPLAY_STAKE (13UL) /* firedancer only */
      55           0 : #define IN_KIND_GENESI_OUT   (14UL) /* firedancer only */
      56           0 : #define IN_KIND_SNAPIN       (15UL) /* firedancer only */
      57           0 : #define IN_KIND_EXEC_REPLAY  (16UL) /* firedancer only */
      58             : 
      59             : FD_IMPORT_BINARY( firedancer_svg, "book/public/fire.svg" );
      60             : 
      61           0 : #define FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN       8192
      62           0 : #define FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN 8192
      63           0 : #define FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT 8192
      64             : 
      65             : static fd_http_server_params_t
      66           0 : derive_http_params( fd_topo_tile_t const * tile ) {
      67           0 :   return (fd_http_server_params_t) {
      68           0 :     .max_connection_cnt    = tile->gui.max_http_connections,
      69           0 :     .max_ws_connection_cnt = tile->gui.max_websocket_connections,
      70           0 :     .max_request_len       = FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN,
      71           0 :     .max_ws_recv_frame_len = FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN,
      72           0 :     .max_ws_send_frame_cnt = FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT,
      73           0 :     .outgoing_buffer_sz    = tile->gui.send_buffer_size_mb * (1UL<<20UL),
      74           0 :     .compress_websocket    = tile->gui.websocket_compression,
      75           0 :   };
      76           0 : }
      77             : 
      78             : struct fd_gui_in_ctx {
      79             :   fd_wksp_t * mem;
      80             :   ulong       mtu;
      81             :   ulong       chunk0;
      82             :   ulong       wmark;
      83             : };
      84             : 
      85             : typedef struct fd_gui_in_ctx fd_gui_in_ctx_t;
      86             : 
      87             : struct fd_gui_out_ctx {
      88             :   ulong       idx;
      89             :   fd_wksp_t * mem;
      90             :   ulong       chunk0;
      91             :   ulong       wmark;
      92             :   ulong       chunk;
      93             : };
      94             : 
      95             : typedef struct fd_gui_out_ctx fd_gui_out_ctx_t;
      96             : 
      97             : typedef struct {
      98             :   fd_topo_t * topo;
      99             :   fd_banks_t * banks;
     100             : 
     101             :   int is_full_client;
     102             :   int snapshots_enabled;
     103             : 
     104             :   fd_gui_t * gui;
     105             :   fd_gui_peers_ctx_t * peers;
     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             :   const double tick_per_ns;
     113             : 
     114             :   fd_clock_t clock[1];
     115             :   long       recal_next; /* next recalibration time (ns) */
     116             : 
     117             :   uchar __attribute__((aligned(FD_CLOCK_ALIGN))) clock_mem[ FD_CLOCK_FOOTPRINT ];
     118             : 
     119             :   ulong chunk;
     120             :   union {
     121             :     struct {
     122             :       ulong slot;
     123             :     } repair_net;
     124             : 
     125             :     uchar net_gossvf[ FD_NET_MTU ];
     126             :     uchar gossip_net[ FD_NET_MTU ];
     127             :   } parsed;
     128             : 
     129             :   fd_http_server_t * gui_server;
     130             : 
     131             :   long next_poll_deadline;
     132             : 
     133             :   char          version_string[ 16UL ];
     134             : 
     135             :   fd_keyswitch_t * keyswitch;
     136             :   uchar const *    identity_key;
     137             : 
     138             :   int               has_vote_key;
     139             :   fd_pubkey_t const vote_key[ 1UL ];
     140             : 
     141             :   ulong           in_kind[ 64UL ];
     142             :   ulong           in_bank_idx[ 64UL ];
     143             :   fd_gui_in_ctx_t in[ 64UL ];
     144             : 
     145             :   fd_gui_out_ctx_t replay_out[ 1 ];
     146             : } fd_gui_ctx_t;
     147             : 
     148             : FD_FN_CONST static inline ulong
     149           0 : scratch_align( void ) {
     150           0 :   return 128UL;
     151           0 : }
     152             : 
     153             : static inline ulong
     154           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     155           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     156           0 :   ulong http_fp = fd_http_server_footprint( http_param );
     157           0 :   if( FD_UNLIKELY( !http_fp ) ) FD_LOG_ERR(( "Invalid [tiles.gui] config parameters" ));
     158             : 
     159           0 :   ulong l = FD_LAYOUT_INIT;
     160           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     161           0 :   l = FD_LAYOUT_APPEND( l, fd_http_server_align(),  http_fp );
     162           0 :   l = FD_LAYOUT_APPEND( l, fd_gui_peers_align(),    fd_gui_peers_footprint( http_param.max_ws_connection_cnt ) );
     163           0 :   l = FD_LAYOUT_APPEND( l, fd_gui_align(),          fd_gui_footprint() );
     164           0 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),        fd_alloc_footprint() );
     165           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     166           0 : }
     167             : 
     168             : FD_FN_PURE static inline ulong
     169           0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
     170           0 :   return 256UL * (1UL<<20UL); /* 256MiB of heap space for the cJSON allocator */
     171           0 : }
     172             : 
     173             : static inline void
     174           0 : during_housekeeping( fd_gui_ctx_t * ctx ) {
     175           0 :   ctx->ref_wallclock = fd_log_wallclock();
     176           0 :   ctx->ref_tickcount = fd_tickcount();
     177             : 
     178           0 :   if( FD_UNLIKELY( fd_clock_now( ctx->clock ) >= ctx->recal_next ) ) {
     179           0 :     ctx->recal_next = fd_clock_default_recal( ctx->clock );
     180           0 :   }
     181             : 
     182           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     183           0 :     fd_gui_set_identity( ctx->gui, ctx->keyswitch->bytes );
     184           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     185           0 :   }
     186           0 : }
     187             : 
     188             : static inline void
     189           0 : metrics_write( fd_gui_ctx_t * ctx ) {
     190           0 :   FD_MGAUGE_SET( GUI, CONNECTION_COUNT, ctx->gui_server->metrics.connection_cnt );
     191           0 :   FD_MGAUGE_SET( GUI, WEBSOCKET_CONNECTION_COUNT, ctx->gui_server->metrics.ws_connection_cnt );
     192             : 
     193           0 :   FD_MCNT_SET( GUI, WEBSOCKET_FRAMES_SENT,     ctx->gui_server->metrics.frames_written );
     194           0 :   FD_MCNT_SET( GUI, WEBSOCKET_FRAMES_RECEIVED, ctx->gui_server->metrics.frames_read );
     195             : 
     196           0 :   FD_MCNT_SET( GUI, BYTES_WRITTEN, ctx->gui_server->metrics.bytes_written );
     197           0 :   FD_MCNT_SET( GUI, BYTES_READ,    ctx->gui_server->metrics.bytes_read );
     198           0 : }
     199             : 
     200             : static void
     201             : before_credit( fd_gui_ctx_t *      ctx,
     202             :                fd_stem_context_t * stem,
     203           0 :                int *               charge_busy ) {
     204           0 :   (void)stem;
     205             : 
     206           0 :   int charge_busy_server = 0;
     207           0 :   long now = fd_tickcount();
     208           0 :   if( FD_UNLIKELY( now>=ctx->next_poll_deadline ) ) {
     209           0 :     charge_busy_server = fd_http_server_poll( ctx->gui_server, 0 );
     210           0 :     ctx->next_poll_deadline = fd_tickcount() + (long)(ctx->tick_per_ns * 128L * 1000L);
     211           0 :   }
     212             : 
     213           0 :   int charge_poll = 0;
     214           0 :   charge_poll |= fd_gui_poll( ctx->gui, fd_clock_now( ctx->clock ) );
     215           0 :   if( FD_UNLIKELY( ctx->is_full_client ) ) charge_poll |= fd_gui_peers_poll( ctx->peers, fd_clock_now( ctx->clock ) );
     216             : 
     217           0 :   *charge_busy = charge_busy_server | charge_poll;
     218           0 : }
     219             : 
     220             : static int
     221             : before_frag( fd_gui_ctx_t * ctx,
     222             :              ulong          in_idx,
     223             :              ulong          seq,
     224           0 :              ulong          sig ) {
     225           0 :   (void)seq;
     226             : 
     227             :   /* Ignore "done draining banks" signal from pack->poh */
     228           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PACK_POH && sig==ULONG_MAX ) ) return 1;
     229           0 :   return 0;
     230           0 : }
     231             : 
     232             : static inline void
     233             : during_frag( fd_gui_ctx_t * ctx,
     234             :              ulong          in_idx,
     235             :              ulong          seq    FD_PARAM_UNUSED,
     236             :              ulong          sig,
     237             :              ulong          chunk,
     238             :              ulong          sz,
     239           0 :              ulong          gui    FD_PARAM_UNUSED ) {
     240             : 
     241           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
     242             : 
     243           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PLUGIN ) ) {
     244             :     /* ... todo... sigh, sz is not correct since it's too big */
     245           0 :     if( FD_LIKELY( sig==FD_PLUGIN_MSG_GOSSIP_UPDATE ) ) {
     246           0 :       ulong peer_cnt = ((ulong *)src)[ 0 ];
     247           0 :       FD_TEST( peer_cnt<=FD_GUI_MAX_PEER_CNT );
     248           0 :       sz = 8UL + peer_cnt*FD_GOSSIP_LINK_MSG_SIZE;
     249           0 :     } else if( FD_LIKELY( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE ) ) {
     250           0 :       ulong peer_cnt = ((ulong *)src)[ 0 ];
     251           0 :       FD_TEST( peer_cnt<=FD_GUI_MAX_PEER_CNT );
     252           0 :       sz = 8UL + peer_cnt*112UL;
     253           0 :     } else if( FD_UNLIKELY( sig==FD_PLUGIN_MSG_LEADER_SCHEDULE ) ) {
     254           0 :       ulong staked_cnt = ((ulong *)src)[ 1 ];
     255           0 :       FD_TEST( staked_cnt<=MAX_STAKED_LEADERS );
     256           0 :       sz = fd_stake_weight_msg_sz( staked_cnt );
     257           0 :     }
     258           0 :   }
     259             : 
     260           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY_STAKE ) ) {
     261           0 :     fd_stake_weight_msg_t * leader_schedule = (fd_stake_weight_msg_t *)src;
     262           0 :     FD_TEST( sz==(ushort)(sizeof(fd_stake_weight_msg_t)+(leader_schedule->staked_cnt*sizeof(fd_vote_stake_weight_t))) );
     263           0 :     sz = fd_stake_weight_msg_sz( leader_schedule->staked_cnt );
     264           0 :   }
     265             : 
     266           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GENESI_OUT ) ) {
     267           0 :     if( FD_LIKELY( sig==GENESI_SIG_BOOTSTRAP_COMPLETED ) ) sz = sizeof(fd_lthash_value_t)+sizeof(fd_hash_t);
     268           0 :   }
     269             : 
     270           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY_OUT ) ) {
     271           0 :     if( FD_LIKELY( sig!=REPLAY_SIG_SLOT_COMPLETED && sig!=REPLAY_SIG_BECAME_LEADER  ) ) return;
     272           0 :   }
     273             : 
     274           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_SHRED_OUT ) ) {
     275             :     /* There are multiple frags types sent on this link, the currently the
     276             :        only way to distinguish them is to check sz.  We dont actually read
     277             :        from the dcache.  */
     278           0 :     return;
     279           0 :   }
     280             : 
     281           0 :   if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
     282           0 :     FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu] or too large (%lu)", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark, ctx->in[ in_idx ].mtu ));
     283             : 
     284           0 :   switch( ctx->in_kind[ in_idx ] ) {
     285           0 :     case IN_KIND_REPAIR_NET: {
     286           0 :       FD_TEST( ctx->is_full_client );
     287           0 :       ctx->parsed.repair_net.slot = ULONG_MAX;
     288           0 :       uchar * payload;
     289           0 :       ulong payload_sz;
     290           0 :       if( FD_LIKELY( fd_ip4_udp_hdr_strip( src, sz, &payload, &payload_sz, NULL, NULL, NULL ) ) ) {
     291           0 :         fd_repair_msg_t const * msg = (fd_repair_msg_t const *)payload;
     292           0 :         switch ( msg->kind ) {
     293           0 :           case FD_REPAIR_KIND_PING:
     294           0 :           case FD_REPAIR_KIND_PONG:
     295           0 :           case FD_REPAIR_KIND_ORPHAN: break;
     296           0 :           case FD_REPAIR_KIND_SHRED: { if( FD_UNLIKELY( msg->shred.slot==0 ) ) { break; } ctx->parsed.repair_net.slot = msg->shred.slot; break; }
     297           0 :           case FD_REPAIR_KIND_HIGHEST_SHRED: { if( FD_UNLIKELY( msg->highest_shred.slot==0 ) ) { break; } ctx->parsed.repair_net.slot = msg->highest_shred.slot; break; }
     298           0 :           default: FD_LOG_ERR(( "unexpected repair msg kind %u", msg->kind ));
     299           0 :         }
     300           0 :       }
     301           0 :       break;
     302           0 :     }
     303           0 :     case IN_KIND_NET_GOSSVF: {
     304           0 :       FD_TEST( ctx->is_full_client );
     305           0 :       FD_TEST( sz<=sizeof(ctx->parsed.net_gossvf) );
     306           0 :       fd_memcpy( ctx->parsed.net_gossvf, src, sz );
     307           0 :       break;
     308           0 :     }
     309           0 :     case IN_KIND_GOSSIP_NET: {
     310           0 :       FD_TEST( ctx->is_full_client );
     311           0 :       FD_TEST( sz<=sizeof(ctx->parsed.gossip_net) );
     312           0 :       fd_memcpy( ctx->parsed.gossip_net, src, sz );
     313           0 :       break;
     314           0 :     }
     315           0 :   }
     316             : 
     317           0 :   ctx->chunk = chunk;
     318           0 : }
     319             : 
     320             : static inline void
     321             : after_frag( fd_gui_ctx_t *      ctx,
     322             :             ulong               in_idx,
     323             :             ulong               seq,
     324             :             ulong               sig,
     325             :             ulong               sz,
     326             :             ulong               tsorig,
     327             :             ulong               tspub,
     328           0 :             fd_stem_context_t * stem ) {
     329           0 :   (void)seq; (void)stem;
     330             : 
     331           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
     332             : 
     333           0 :   switch ( ctx->in_kind[ in_idx ] ) {
     334           0 :     case IN_KIND_PLUGIN: {
     335           0 :       FD_TEST( !ctx->is_full_client );
     336           0 :       fd_gui_plugin_message( ctx->gui, sig, src, fd_clock_now( ctx->clock ) );
     337           0 :       break;
     338           0 :     }
     339           0 :     case IN_KIND_EXEC_REPLAY: {
     340           0 :       FD_TEST( ctx->is_full_client );
     341           0 :       if( FD_LIKELY( sig>>32==FD_EXEC_TT_TXN_EXEC ) ) {
     342           0 :         fd_exec_task_done_msg_t * msg = (fd_exec_task_done_msg_t *)src;
     343             : 
     344           0 :         long tickcount = fd_tickcount();
     345           0 :         long tsorig_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, tickcount ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     346           0 :         long tspub_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, tickcount ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     347           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 );
     348           0 :       }
     349             : 
     350           0 :       break;
     351           0 :     }
     352           0 :     case IN_KIND_REPLAY_OUT: {
     353           0 :       FD_TEST( ctx->is_full_client );
     354           0 :       if( FD_UNLIKELY( sig==REPLAY_SIG_SLOT_COMPLETED ) ) {
     355           0 :         fd_replay_slot_completed_t const * replay =  (fd_replay_slot_completed_t const *)src;
     356             : 
     357           0 :         fd_bank_t * bank = fd_banks_bank_query( ctx->banks, replay->bank_idx );
     358             :         /* bank should already have positive refcnt */
     359           0 :         FD_TEST( bank );
     360           0 :         FD_TEST( bank->refcnt!=0 );
     361             : 
     362           0 :         fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( bank );
     363           0 :         FD_TEST( fd_vote_states_cnt( vote_states )<FD_RUNTIME_MAX_VOTE_ACCOUNTS );
     364             : 
     365           0 :         fd_vote_states_iter_t iter_[1];
     366           0 :         ulong vote_count = 0UL;
     367           0 :         for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states );
     368           0 :             !fd_vote_states_iter_done( iter );
     369           0 :             fd_vote_states_iter_next( iter ) ) {
     370           0 :           fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
     371             : 
     372           0 :           ctx->peers->votes[ vote_count ].vote_account        = vote_state->vote_account;
     373           0 :           ctx->peers->votes[ vote_count ].node_account        = vote_state->node_account;
     374           0 :           ctx->peers->votes[ vote_count ].stake               = vote_state->stake;
     375           0 :           ctx->peers->votes[ vote_count ].last_vote_slot      = vote_state->last_vote_slot;
     376           0 :           ctx->peers->votes[ vote_count ].last_vote_timestamp = vote_state->last_vote_timestamp;
     377           0 :           ctx->peers->votes[ vote_count ].commission          = vote_state->commission;
     378             :           // ctx->peers->votes[ vote_count ].epoch               = fd_ulong_if( !vote_state->credits_cnt, ULONG_MAX, vote_state->epoch[ 0 ]   );
     379             :           // ctx->peers->votes[ vote_count ].epoch_credits       = fd_ulong_if( !vote_state->credits_cnt, ULONG_MAX, vote_state->credits[ 0 ] );
     380             : 
     381           0 :           vote_count++;
     382           0 :         }
     383           0 :         fd_bank_vote_states_end_locking_query( bank );
     384             : 
     385           0 :         fd_gui_slot_completed_t slot_completed;
     386           0 :         if( FD_LIKELY( replay->parent_bank_idx!=ULONG_MAX ) ) {
     387           0 :           fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, replay->parent_bank_idx );
     388           0 :           FD_TEST( parent_bank );
     389             : 
     390           0 :           slot_completed.total_txn_cnt          = (uint)(fd_bank_txn_count_get( bank )                 - fd_bank_txn_count_get( parent_bank ));
     391           0 :           slot_completed.vote_txn_cnt           = slot_completed.total_txn_cnt - (uint)(fd_bank_nonvote_txn_count_get( bank ) - fd_bank_nonvote_txn_count_get( parent_bank ));
     392           0 :           slot_completed.failed_txn_cnt         = (uint)(fd_bank_failed_txn_count_get( bank )          - fd_bank_failed_txn_count_get( parent_bank ));
     393           0 :           slot_completed.nonvote_failed_txn_cnt = (uint)(fd_bank_nonvote_failed_txn_count_get( bank )  - fd_bank_nonvote_failed_txn_count_get( parent_bank ));
     394             : 
     395           0 :           fd_stem_publish( stem, ctx->replay_out->idx, replay->parent_bank_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
     396           0 :         } else {
     397           0 :           slot_completed.total_txn_cnt          = (uint)fd_bank_txn_count_get( bank );
     398           0 :           slot_completed.vote_txn_cnt           = slot_completed.total_txn_cnt - (uint)fd_bank_nonvote_txn_count_get( bank );
     399           0 :           slot_completed.failed_txn_cnt         = (uint)fd_bank_failed_txn_count_get( bank );
     400           0 :           slot_completed.nonvote_failed_txn_cnt = (uint)fd_bank_nonvote_failed_txn_count_get( bank );
     401           0 :         }
     402             : 
     403           0 :         slot_completed.slot              = fd_bank_slot_get( bank );
     404           0 :         slot_completed.completed_time    = replay->completion_time_nanos;
     405           0 :         slot_completed.parent_slot       = fd_bank_parent_slot_get( bank );
     406           0 :         slot_completed.max_compute_units = fd_uint_if( replay->cost_tracker.block_cost_limit==0UL, UINT_MAX, (uint)replay->cost_tracker.block_cost_limit );
     407           0 :         slot_completed.transaction_fee   = fd_bank_execution_fees_get( bank );
     408           0 :         slot_completed.priority_fee      = fd_bank_priority_fees_get( bank );
     409           0 :         slot_completed.tips              = fd_bank_tips_get( bank );
     410           0 :         slot_completed.compute_units     = fd_uint_if( replay->cost_tracker.block_cost==0UL, UINT_MAX, (uint)replay->cost_tracker.block_cost );
     411           0 :         slot_completed.shred_cnt         = (uint)fd_bank_shred_cnt_get( bank );
     412             : 
     413             :         /* release shared ownership of bank and parent_bank */
     414           0 :         fd_stem_publish( stem, ctx->replay_out->idx, replay->bank_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
     415             : 
     416             :         /* update vote info */
     417           0 :         fd_gui_peers_handle_vote_update( ctx->peers, ctx->peers->votes, vote_count, fd_clock_now( ctx->clock ), ctx->gui->ipinfo.country_code );
     418             : 
     419             :         /* update slot data */
     420           0 :         fd_gui_handle_replay_update( ctx->gui, &slot_completed, fd_clock_now( ctx->clock ) );
     421             : 
     422           0 :       } else if( FD_UNLIKELY( sig==REPLAY_SIG_BECAME_LEADER ) ) {
     423           0 :         fd_became_leader_t * became_leader = (fd_became_leader_t *)src;
     424           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 );
     425           0 :       } else {
     426           0 :         return;
     427           0 :       }
     428           0 :       break;
     429           0 :     }
     430           0 :     case IN_KIND_REPLAY_STAKE: {
     431           0 :       FD_TEST( ctx->is_full_client );
     432             : 
     433           0 :       fd_stake_weight_msg_t * leader_schedule = (fd_stake_weight_msg_t *)src;
     434           0 :       fd_gui_handle_leader_schedule( ctx->gui, leader_schedule, fd_clock_now( ctx->clock ) );
     435           0 :       break;
     436           0 :     }
     437           0 :     case IN_KIND_SNAPIN: {
     438           0 :       FD_TEST( ctx->is_full_client );
     439           0 :       fd_gui_peers_handle_config_account( ctx->peers, src, sz );
     440           0 :       break;
     441           0 :     }
     442           0 :     case IN_KIND_GENESI_OUT: {
     443           0 :       FD_TEST( ctx->is_full_client );
     444             : 
     445           0 :       if( FD_LIKELY( sig==GENESI_SIG_BOOTSTRAP_COMPLETED ) ) {
     446           0 :         fd_gui_handle_genesis_hash( ctx->gui, src+sizeof(fd_lthash_value_t) );
     447           0 :       } else {
     448           0 :         fd_gui_handle_genesis_hash( ctx->gui, src );
     449           0 :       }
     450           0 :       break;
     451           0 :     }
     452           0 :     case IN_KIND_TOWER_OUT: {
     453           0 :       FD_TEST( ctx->is_full_client );
     454           0 :       if( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_DONE )) {
     455           0 :         fd_tower_slot_done_t const * tower = (fd_tower_slot_done_t const *)src;
     456           0 :         fd_gui_handle_tower_update( ctx->gui, tower, fd_clock_now( ctx->clock ) );
     457           0 :       }
     458           0 :       break;
     459           0 :     }
     460           0 :     case IN_KIND_SHRED_OUT: {
     461           0 :       FD_TEST( ctx->is_full_client );
     462           0 :       if( FD_LIKELY( sz!=0 && sz!=FD_SHRED_DATA_HEADER_SZ + FD_SHRED_MERKLE_ROOT_SZ * 2 + sizeof(int) ) ) {
     463           0 :         ulong slot = fd_disco_shred_out_shred_sig_slot( sig );
     464           0 :         int is_turbine = fd_disco_shred_out_shred_sig_is_turbine( sig );
     465           0 :         ulong shred_idx  = fd_disco_shred_out_shred_sig_shred_idx( sig );
     466             :         /* tsorig is the timestamp when the shred was received by the shred tile */
     467           0 :         long tsorig_nanos = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     468           0 :         fd_gui_handle_shred( ctx->gui, slot, shred_idx, is_turbine, tsorig_nanos );
     469           0 :       }
     470           0 :       break;
     471           0 :     }
     472           0 :     case IN_KIND_SNAPCT: {
     473           0 :       FD_TEST( ctx->is_full_client );
     474           0 :       fd_gui_handle_snapshot_update( ctx->gui, (fd_snapct_update_t *)src );
     475           0 :       break;
     476           0 :     }
     477           0 :     case IN_KIND_REPAIR_NET: {
     478           0 :       if( FD_UNLIKELY( ctx->parsed.repair_net.slot==ULONG_MAX ) ) break;
     479           0 :       long tsorig_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     480           0 :       fd_gui_handle_repair_slot( ctx->gui, ctx->parsed.repair_net.slot, tsorig_ns );
     481           0 :       break;
     482           0 :     }
     483           0 :     case IN_KIND_NET_GOSSVF: {
     484           0 :       FD_TEST( ctx->is_full_client );
     485           0 :       uchar * payload;
     486           0 :       ulong payload_sz;
     487           0 :       fd_ip4_hdr_t * ip4_hdr;
     488           0 :       fd_udp_hdr_t * udp_hdr;
     489           0 :       if( FD_LIKELY( fd_ip4_udp_hdr_strip( ctx->parsed.net_gossvf, sz, &payload, &payload_sz, NULL, &ip4_hdr, &udp_hdr ) ) ) {
     490           0 :         fd_gui_peers_handle_gossip_message( ctx->peers, payload, payload_sz, &(fd_ip4_port_t){ .addr = ip4_hdr->saddr, .port = udp_hdr->net_sport }, 1 );
     491           0 :       }
     492           0 :       break;
     493           0 :     }
     494           0 :     case IN_KIND_GOSSIP_NET: {
     495           0 :       FD_TEST( ctx->is_full_client );
     496           0 :       uchar * payload;
     497           0 :       ulong payload_sz;
     498           0 :       fd_ip4_hdr_t * ip4_hdr;
     499           0 :       fd_udp_hdr_t * udp_hdr;
     500           0 :       FD_TEST( fd_ip4_udp_hdr_strip( ctx->parsed.gossip_net, sz, &payload, &payload_sz, NULL, &ip4_hdr, &udp_hdr ) );
     501           0 :       fd_gui_peers_handle_gossip_message( ctx->peers, payload, payload_sz, &(fd_ip4_port_t){ .addr = ip4_hdr->daddr, .port = udp_hdr->net_dport }, 0 );
     502           0 :       break;
     503           0 :     }
     504           0 :     case IN_KIND_GOSSIP_OUT: {
     505           0 :       FD_TEST( ctx->is_full_client );
     506           0 :       fd_gossip_update_message_t * update = (fd_gossip_update_message_t *)src;
     507           0 :       switch( update->tag ) {
     508           0 :         case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE: FD_TEST( sz == FD_GOSSIP_UPDATE_SZ_CONTACT_INFO_REMOVE ); break;
     509           0 :         case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO: FD_TEST( sz == FD_GOSSIP_UPDATE_SZ_CONTACT_INFO ); break;
     510           0 :         default: break;
     511           0 :       }
     512           0 :       fd_gui_peers_handle_gossip_update( ctx->peers, ctx->gui->ipinfo.nodes, update, fd_clock_now( ctx-> clock ), ctx->gui->ipinfo.country_code );
     513           0 :       break;
     514           0 :     }
     515           0 :     case IN_KIND_POH_PACK: {
     516           0 :       FD_TEST( !ctx->is_full_client );
     517           0 :       FD_TEST( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_BECAME_LEADER );
     518           0 :       fd_became_leader_t * became_leader = (fd_became_leader_t *)src;
     519           0 :       fd_gui_became_leader( ctx->gui, fd_disco_poh_sig_slot( sig ), became_leader->slot_start_ns, became_leader->slot_end_ns, became_leader->limits.slot_max_cost, became_leader->max_microblocks_in_slot );
     520           0 :       break;
     521           0 :     }
     522           0 :     case IN_KIND_PACK_POH: {
     523           0 :       fd_gui_unbecame_leader( ctx->gui, fd_disco_bank_sig_slot( sig ), (fd_done_packing_t const *)src, fd_clock_now( ctx->clock ) );
     524           0 :       break;
     525           0 :     }
     526           0 :     case IN_KIND_PACK_BANK: {
     527           0 :       if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK ) ) {
     528           0 :         fd_microblock_bank_trailer_t * trailer = (fd_microblock_bank_trailer_t *)( src+sz-sizeof(fd_microblock_bank_trailer_t) );
     529           0 :         long now = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     530           0 :         fd_gui_microblock_execution_begin( ctx->gui,
     531           0 :                                           now,
     532           0 :                                           fd_disco_poh_sig_slot( sig ),
     533           0 :                                           (fd_txn_p_t *)src,
     534           0 :                                           (sz-sizeof( fd_microblock_bank_trailer_t ))/sizeof( fd_txn_p_t ),
     535           0 :                                           (uint)trailer->microblock_idx,
     536           0 :                                           trailer->pack_txn_idx );
     537           0 :       } else {
     538           0 :         FD_LOG_ERR(( "unexpected poh packet type %lu", fd_disco_poh_sig_pkt_type( sig ) ));
     539           0 :       }
     540           0 :       break;
     541           0 :     }
     542           0 :     case IN_KIND_BANK_POH: {
     543           0 :       fd_microblock_trailer_t * trailer = (fd_microblock_trailer_t *)( src+sz-sizeof( fd_microblock_trailer_t ) );
     544           0 :       long now = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     545           0 :       fd_gui_microblock_execution_end( ctx->gui,
     546           0 :                                       now,
     547           0 :                                       ctx->in_bank_idx[ in_idx ],
     548           0 :                                       fd_disco_bank_sig_slot( sig ),
     549           0 :                                       (sz-sizeof( fd_microblock_trailer_t ))/sizeof( fd_txn_p_t ),
     550           0 :                                       (fd_txn_p_t *)src,
     551           0 :                                       trailer->pack_txn_idx,
     552           0 :                                       trailer->txn_start_pct,
     553           0 :                                       trailer->txn_load_end_pct,
     554           0 :                                       trailer->txn_end_pct,
     555           0 :                                       trailer->txn_preload_end_pct,
     556           0 :                                       trailer->tips );
     557           0 :       break;
     558           0 :     }
     559           0 :     default: FD_LOG_ERR(( "unexpected in_kind %lu", ctx->in_kind[ in_idx ] ));
     560           0 :   }
     561           0 : }
     562             : 
     563             : static fd_http_server_response_t
     564           0 : gui_http_request( fd_http_server_request_t const * request ) {
     565           0 :   if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
     566           0 :     return (fd_http_server_response_t){
     567           0 :       .status = 405,
     568           0 :     };
     569           0 :   }
     570             : 
     571           0 :   if( FD_LIKELY( !strcmp( request->path, "/websocket" ) ) ) {
     572           0 :     return (fd_http_server_response_t){
     573           0 :       .status            = 200,
     574           0 :       .upgrade_websocket = 1,
     575           0 : #ifdef FD_HAS_ZSTD
     576           0 :       .compress_websocket = request->headers.compress_websocket,
     577             : #else
     578             :       .compress_websocket = 0,
     579             : #endif
     580           0 :     };
     581           0 :   } else if( FD_LIKELY( !strcmp( request->path, "/favicon.svg" ) ) ) {
     582           0 :     return (fd_http_server_response_t){
     583           0 :       .status            = 200,
     584           0 :       .static_body       = firedancer_svg,
     585           0 :       .static_body_len   = firedancer_svg_sz,
     586           0 :       .content_type      = "image/svg+xml",
     587           0 :       .upgrade_websocket = 0,
     588           0 :     };
     589           0 :   }
     590             : 
     591           0 :   int is_vite_page = !strcmp( request->path, "/" ) ||
     592           0 :                      !strcmp( request->path, "/slotDetails" ) ||
     593           0 :                      !strcmp( request->path, "/leaderSchedule" ) ||
     594           0 :                      !strcmp( request->path, "/gossip") ||
     595           0 :                      !strncmp( request->path, "/?", strlen("/?") ) ||
     596           0 :                      !strncmp( request->path, "/slotDetails?", strlen("/slotDetails?") ) ||
     597           0 :                      !strncmp( request->path, "/leaderSchedule?", strlen("/leaderSchedule?") ) ||
     598           0 :                      !strncmp( request->path, "/gossip?", strlen("/gossip?") );
     599             : 
     600           0 :   FD_TEST( STATIC_FILES );
     601           0 :   for( fd_http_static_file_t const * f = STATIC_FILES; f->name; f++ ) {
     602           0 :     if( !strcmp( request->path, f->name ) ||
     603           0 :         (!strcmp( f->name, "/index.html" ) && is_vite_page) ) {
     604           0 :       char const * content_type = NULL;
     605             : 
     606           0 :       char const * ext = strrchr( f->name, '.' );
     607           0 :       if( FD_LIKELY( ext ) ) {
     608           0 :         if( !strcmp( ext, ".html" ) ) content_type = "text/html; charset=utf-8";
     609           0 :         else if( !strcmp( ext, ".css" ) ) content_type = "text/css";
     610           0 :         else if( !strcmp( ext, ".js" ) ) content_type = "application/javascript";
     611           0 :         else if( !strcmp( ext, ".svg" ) ) content_type = "image/svg+xml";
     612           0 :         else if( !strcmp( ext, ".woff" ) ) content_type = "font/woff";
     613           0 :         else if( !strcmp( ext, ".woff2" ) ) content_type = "font/woff2";
     614           0 :       }
     615             : 
     616           0 :       char const * cache_control = NULL;
     617           0 :       if( FD_LIKELY( !strncmp( request->path, "/assets", 7 ) ) ) cache_control = "public, max-age=31536000, immutable";
     618           0 :       else if( FD_LIKELY( !strcmp( f->name, "/index.html" ) ) )  cache_control = "no-cache";
     619             : 
     620           0 :       const uchar * data = f->data;
     621           0 :       ulong data_len = *(f->data_len);
     622             : 
     623           0 :       int accepts_zstd = 0;
     624           0 :       if( FD_LIKELY( request->headers.accept_encoding ) ) {
     625           0 :         accepts_zstd = !!strstr( request->headers.accept_encoding, "zstd" );
     626           0 :       }
     627             : 
     628           0 :       int accepts_gzip = 0;
     629           0 :       if( FD_LIKELY( request->headers.accept_encoding ) ) {
     630           0 :         accepts_gzip = !!strstr( request->headers.accept_encoding, "gzip" );
     631           0 :       }
     632             : 
     633           0 :       char const * content_encoding = NULL;
     634           0 :       if( FD_LIKELY( accepts_zstd && f->zstd_data ) ) {
     635           0 :         content_encoding = "zstd";
     636           0 :         data = f->zstd_data;
     637           0 :         data_len = *(f->zstd_data_len);
     638           0 :       } else if( FD_LIKELY( accepts_gzip && f->gzip_data ) ) {
     639           0 :         content_encoding = "gzip";
     640           0 :         data = f->gzip_data;
     641           0 :         data_len = *(f->gzip_data_len);
     642           0 :       }
     643             : 
     644           0 :       return (fd_http_server_response_t){
     645           0 :         .status            = 200,
     646           0 :         .static_body       = data,
     647           0 :         .static_body_len   = data_len,
     648           0 :         .content_type      = content_type,
     649           0 :         .cache_control     = cache_control,
     650           0 :         .content_encoding  = content_encoding,
     651           0 :         .upgrade_websocket = 0,
     652           0 :       };
     653           0 :     }
     654           0 :   }
     655             : 
     656           0 :   return (fd_http_server_response_t){
     657           0 :     .status            = 404,
     658           0 :   };
     659           0 : }
     660             : 
     661             : static void
     662             : gui_ws_open( ulong  conn_id,
     663           0 :              void * _ctx ) {
     664           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     665             : 
     666           0 :   fd_gui_ws_open( ctx->gui, conn_id );
     667           0 :   if( FD_UNLIKELY( ctx->is_full_client ) ) fd_gui_peers_ws_open( ctx->peers, conn_id, fd_clock_now( ctx->clock ), ctx->gui->ipinfo.country_code );
     668           0 : }
     669             : 
     670             : static void
     671             : gui_ws_close( ulong  conn_id,
     672             :               int    reason,
     673           0 :               void * _ctx ) {
     674           0 :   (void) reason;
     675           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     676           0 :   if( FD_UNLIKELY( ctx->is_full_client ) ) fd_gui_peers_ws_close( ctx->peers, conn_id );
     677           0 : }
     678             : 
     679             : static void
     680             : gui_ws_message( ulong         ws_conn_id,
     681             :                 uchar const * data,
     682             :                 ulong         data_len,
     683           0 :                 void *        _ctx ) {
     684           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     685             : 
     686           0 :   int reason = fd_gui_ws_message( ctx->gui, ws_conn_id, data, data_len );
     687           0 :   if( FD_UNLIKELY( ctx->is_full_client && reason==FD_HTTP_SERVER_CONNECTION_CLOSE_UNKNOWN_METHOD ) ) reason = fd_gui_peers_ws_message( ctx->peers, ws_conn_id, data, data_len );
     688             : 
     689           0 :   if( FD_UNLIKELY( reason<0 ) ) fd_http_server_ws_close( ctx->gui_server, ws_conn_id, reason );
     690           0 : }
     691             : 
     692             : static void
     693             : privileged_init( fd_topo_t *      topo,
     694           0 :                  fd_topo_tile_t * tile ) {
     695           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     696             : 
     697           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     698           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     699             : 
     700           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     701           0 :   fd_http_server_t * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
     702             : 
     703           0 :   fd_http_server_callbacks_t gui_callbacks = {
     704           0 :     .request    = gui_http_request,
     705           0 :     .ws_open    = gui_ws_open,
     706           0 :     .ws_close   = gui_ws_close,
     707           0 :     .ws_message = gui_ws_message,
     708           0 :   };
     709           0 :   ctx->gui_server = fd_http_server_join( fd_http_server_new( _gui, http_param, gui_callbacks, ctx ) );
     710           0 :   fd_http_server_listen( ctx->gui_server, tile->gui.listen_addr, tile->gui.listen_port );
     711             : 
     712           0 :   FD_LOG_NOTICE(( "gui server listening at http://" FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS( tile->gui.listen_addr ), tile->gui.listen_port ));
     713             : 
     714           0 :   if( FD_UNLIKELY( !strcmp( tile->gui.identity_key_path, "" ) ) )
     715           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     716             : 
     717           0 :   ctx->identity_key = fd_keyload_load( tile->gui.identity_key_path, /* pubkey only: */ 1 );
     718             : 
     719           0 :   if( FD_UNLIKELY( !strcmp( tile->gui.vote_key_path, "" ) ) ) {
     720           0 :     ctx->has_vote_key = 0;
     721           0 :   } else {
     722           0 :     ctx->has_vote_key = 1;
     723           0 :     if( FD_UNLIKELY( !fd_base58_decode_32( tile->gui.vote_key_path, (uchar *)ctx->vote_key->uc ) ) ) {
     724           0 :       const uchar * vote_key = fd_keyload_load( tile->gui.vote_key_path, /* pubkey only: */ 1 );
     725           0 :       fd_memcpy( (uchar *)ctx->vote_key->uc, vote_key, 32UL );
     726           0 :     }
     727           0 :   }
     728           0 : }
     729             : 
     730             : extern char const fdctl_version_string[];
     731             : 
     732             : static void
     733             : unprivileged_init( fd_topo_t *      topo,
     734           0 :                    fd_topo_tile_t * tile ) {
     735           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     736             : 
     737           0 :   fd_topo_tile_t * gui_tile = &topo->tiles[ fd_topo_find_tile( topo, "gui", 0UL ) ];
     738           0 :   switch( gui_tile->gui.frontend_release_channel ) {
     739           0 :     case 0: STATIC_FILES = STATIC_FILES_STABLE; break;
     740           0 :     case 1: STATIC_FILES = STATIC_FILES_ALPHA;  break;
     741           0 :     case 2: STATIC_FILES = STATIC_FILES_DEV;    break;
     742           0 :     default: __builtin_unreachable();
     743           0 :   }
     744             : 
     745           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     746           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     747           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t )                                    );
     748           0 :                        FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(),  fd_http_server_footprint( http_param )                    );
     749           0 :   void * _peers      = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_peers_align(),    fd_gui_peers_footprint( http_param.max_ws_connection_cnt) );
     750           0 :   void * _gui        = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_align(),          fd_gui_footprint()                                        );
     751           0 :   void * _alloc      = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(),        fd_alloc_footprint()                                      );
     752             : 
     753           0 :   ctx->is_full_client = ULONG_MAX!=fd_topo_find_tile( topo, "repair", 0UL );
     754           0 :   ctx->snapshots_enabled = ULONG_MAX!=fd_topo_find_tile( topo, "snapct", 0UL );
     755             : 
     756           0 :   fd_clock_default_init( ctx->clock, ctx->clock_mem );
     757           0 :   ctx->recal_next = fd_clock_recal_next( ctx->clock );
     758             : 
     759           0 :   ctx->ref_wallclock = fd_log_wallclock();
     760           0 :   ctx->ref_tickcount = fd_tickcount();
     761           0 :   *(double *)&ctx->tick_per_ns = fd_tempo_tick_per_ns( NULL );
     762             : 
     763           0 :   FD_TEST( fd_cstr_printf_check( ctx->version_string, sizeof( ctx->version_string ), NULL, "%s", fdctl_version_string ) );
     764             : 
     765           0 :   ctx->topo = topo;
     766           0 :   ctx->peers = fd_gui_peers_join( fd_gui_peers_new( _peers, ctx->gui_server, ctx->topo, http_param.max_ws_connection_cnt, fd_clock_now( ctx->clock) ) );
     767           0 :   ctx->gui  = fd_gui_join(  fd_gui_new( _gui, ctx->gui_server, ctx->version_string, 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, ctx->topo, fd_clock_now( ctx->clock ) ) );
     768           0 :   FD_TEST( ctx->gui );
     769             : 
     770           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
     771           0 :   FD_TEST( ctx->keyswitch );
     772             : 
     773           0 :   fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
     774           0 :   FD_TEST( alloc );
     775           0 :   cJSON_alloc_install( alloc );
     776             : 
     777           0 :   ctx->next_poll_deadline = fd_tickcount();
     778             : 
     779           0 :   ulong banks_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "banks" );
     780             : 
     781           0 :   if( FD_UNLIKELY( ctx->is_full_client ) ) {
     782           0 :     FD_TEST( banks_obj_id!=ULONG_MAX );
     783           0 :     ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) ); FD_TEST( ctx->banks );
     784           0 :   }
     785             : 
     786           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     787           0 :     fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
     788           0 :     fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     789             : 
     790           0 :     if( FD_LIKELY( !strcmp( link->name, "plugin_out"        ) ) ) ctx->in_kind[ i ] = IN_KIND_PLUGIN;
     791           0 :     else if( FD_LIKELY( !strcmp( link->name, "poh_pack"     ) ) ) ctx->in_kind[ i ] = IN_KIND_POH_PACK;
     792           0 :     else if( FD_LIKELY( !strcmp( link->name, "pack_bank"    ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_BANK;
     793           0 :     else if( FD_LIKELY( !strcmp( link->name, "pack_poh"     ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_POH;
     794           0 :     else if( FD_LIKELY( !strcmp( link->name, "bank_poh"     ) ) ) ctx->in_kind[ i ] = IN_KIND_BANK_POH;
     795           0 :     else if( FD_LIKELY( !strcmp( link->name, "shred_out"    ) ) ) ctx->in_kind[ i ] = IN_KIND_SHRED_OUT;    /* full client only */
     796           0 :     else if( FD_LIKELY( !strcmp( link->name, "net_gossvf"   ) ) ) ctx->in_kind[ i ] = IN_KIND_NET_GOSSVF;   /* full client only */
     797           0 :     else if( FD_LIKELY( !strcmp( link->name, "gossip_net"   ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_NET;   /* full client only */
     798           0 :     else if( FD_LIKELY( !strcmp( link->name, "gossip_out"   ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_OUT;   /* full client only */
     799           0 :     else if( FD_LIKELY( !strcmp( link->name, "snapct_gui"   ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPCT;       /* full client only */
     800           0 :     else if( FD_LIKELY( !strcmp( link->name, "repair_net"   ) ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR_NET;   /* full client only */
     801           0 :     else if( FD_LIKELY( !strcmp( link->name, "tower_out"    ) ) ) ctx->in_kind[ i ] = IN_KIND_TOWER_OUT;    /* full client only */
     802           0 :     else if( FD_LIKELY( !strcmp( link->name, "replay_out"   ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY_OUT;   /* full client only */
     803           0 :     else if( FD_LIKELY( !strcmp( link->name, "replay_stake" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY_STAKE; /* full client only */
     804           0 :     else if( FD_LIKELY( !strcmp( link->name, "genesi_out"   ) ) ) ctx->in_kind[ i ] = IN_KIND_GENESI_OUT; /* full client only */
     805           0 :     else if( FD_LIKELY( !strcmp( link->name, "snapin_gui"   ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPIN; /* full client only */
     806           0 :     else if( FD_LIKELY( !strcmp( link->name, "exec_replay"  ) ) ) ctx->in_kind[ i ] = IN_KIND_EXEC_REPLAY;  /* full client only */
     807           0 :     else FD_LOG_ERR(( "gui tile has unexpected input link %lu %s", i, link->name ));
     808             : 
     809           0 :     if( FD_LIKELY( !strcmp( link->name, "bank_poh" ) ) ) {
     810           0 :       ulong producer = fd_topo_find_link_producer( topo, &topo->links[ tile->in_link_id[ i ] ] );
     811           0 :       ctx->in_bank_idx[ i ] = topo->tiles[ producer ].kind_id;
     812           0 :     }
     813             : 
     814           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
     815           0 :     ctx->in[ i ].mtu    = link->mtu;
     816           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
     817           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
     818           0 :   }
     819             : 
     820           0 :   if( FD_UNLIKELY( ctx->is_full_client ) ) {
     821           0 :     ulong idx = fd_topo_find_tile_out_link( topo, tile, "gui_replay", 0UL );
     822           0 :     FD_TEST( idx!=ULONG_MAX );
     823           0 :     fd_gui_out_ctx_t * replay_out = ctx->replay_out;
     824           0 :     replay_out->idx    = idx;
     825           0 :   }
     826             : 
     827           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     828           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     829           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     830           0 : }
     831             : 
     832             : static ulong
     833             : populate_allowed_seccomp( fd_topo_t const *      topo,
     834             :                           fd_topo_tile_t const * tile,
     835             :                           ulong                  out_cnt,
     836           0 :                           struct sock_filter *   out ) {
     837           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     838           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     839           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     840             : 
     841           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 ) );
     842           0 :   return sock_filter_policy_fd_gui_tile_instr_cnt;
     843           0 : }
     844             : 
     845             : static ulong
     846             : populate_allowed_fds( fd_topo_t const *      topo,
     847             :                       fd_topo_tile_t const * tile,
     848             :                       ulong                  out_fds_cnt,
     849           0 :                       int *                  out_fds ) {
     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 :   if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     855             : 
     856           0 :   ulong out_cnt = 0UL;
     857           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     858           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     859           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     860           0 :   out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->gui_server ); /* gui listen socket */
     861           0 :   return out_cnt;
     862           0 : }
     863             : 
     864             : static ulong
     865             : rlimit_file_cnt( fd_topo_t const *      topo FD_PARAM_UNUSED,
     866           0 :                  fd_topo_tile_t const * tile ) {
     867             :   /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
     868           0 :   ulong base = 5UL;
     869           0 :   return base + tile->gui.max_http_connections + tile->gui.max_websocket_connections;
     870           0 : }
     871             : 
     872           0 : #define STEM_BURST (1UL)
     873             : 
     874             : /* See explanation in fd_pack */
     875           0 : #define STEM_LAZY  (128L*3000L)
     876             : 
     877           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_gui_ctx_t
     878           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gui_ctx_t)
     879             : 
     880           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     881           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     882           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     883           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
     884           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     885           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     886             : 
     887             : #include "../../disco/stem/fd_stem.c"
     888             : 
     889             : fd_topo_run_tile_t fd_tile_gui = {
     890             :   .name                     = "gui",
     891             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
     892             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     893             :   .populate_allowed_fds     = populate_allowed_fds,
     894             :   .scratch_align            = scratch_align,
     895             :   .scratch_footprint        = scratch_footprint,
     896             :   .loose_footprint          = loose_footprint,
     897             :   .privileged_init          = privileged_init,
     898             :   .unprivileged_init        = unprivileged_init,
     899             :   .run                      = stem_run,
     900             : };

Generated by: LCOV version 1.14