LCOV - code coverage report
Current view: top level - discoh/guih - fd_guih_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 369 0.0 %
Date: 2026-06-30 05:50:37 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 FRONTEND_CLIENT=Frankendancer
       6             : 
       7             :    from the repository root. */
       8             : 
       9             : #include "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_guih_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 "fd_guih.h"
      23             : #include "../../discoh/plugin/fd_plugin.h"
      24             : #include "../../discof/replay/fd_execrp.h"
      25             : #include "../../disco/metrics/fd_metrics.h"
      26             : #include "../../disco/net/fd_net_tile.h"
      27             : #include "../../disco/fd_clock_tile.h"
      28             : #include "../../discof/genesis/fd_genesi_tile.h" // TODO: Layering violation
      29             : #include "../../waltz/http/fd_http_server.h"
      30             : #include "../../waltz/http/fd_http_server_private.h"
      31             : #include "../../ballet/json/cJSON_alloc.h"
      32             : #include "../../discof/repair/fd_repair.h"
      33             : #include "../../discof/replay/fd_replay_tile.h"
      34             : #include "../../disco/shred/fd_shred_tile.h"
      35             : 
      36           0 : #define IN_KIND_PLUGIN        ( 0UL)
      37           0 : #define IN_KIND_POH_PACK      ( 1UL)
      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_BUNDLE        (17UL)
      42             : 
      43             : FD_IMPORT_BINARY( firedancer_svg, "book/public/fire.svg" );
      44             : 
      45           0 : #define FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN       65536
      46           0 : #define FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN 65536
      47           0 : #define FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT 8192
      48             : 
      49             : static fd_http_server_params_t
      50           0 : derive_http_params( fd_topo_tile_t const * tile ) {
      51           0 :   return (fd_http_server_params_t) {
      52           0 :     .max_connection_cnt    = tile->gui.max_http_connections,
      53           0 :     .max_ws_connection_cnt = tile->gui.max_websocket_connections,
      54           0 :     .max_request_len       = FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN,
      55           0 :     .max_ws_recv_frame_len = FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN,
      56           0 :     .max_ws_send_frame_cnt = FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT,
      57           0 :     .outgoing_buffer_sz    = tile->gui.send_buffer_size_mb * (1UL<<20UL),
      58           0 :     .compress_websocket    = tile->gui.websocket_compression,
      59           0 :   };
      60           0 : }
      61             : 
      62             : struct fd_guih_in_ctx {
      63             :   fd_wksp_t * mem;
      64             :   ulong       mtu;
      65             :   ulong       chunk0;
      66             :   ulong       wmark;
      67             : };
      68             : 
      69             : typedef struct fd_guih_in_ctx fd_guih_in_ctx_t;
      70             : 
      71             : struct fd_guih_out_ctx {
      72             :   ulong       idx;
      73             :   fd_wksp_t * mem;
      74             :   ulong       chunk0;
      75             :   ulong       wmark;
      76             :   ulong       chunk;
      77             : };
      78             : 
      79             : typedef struct fd_guih_out_ctx fd_guih_out_ctx_t;
      80             : 
      81             : typedef struct {
      82             :   fd_topo_t const * topo;
      83             : 
      84             :   fd_guih_t * gui;
      85             : 
      86             :   ulong in_cnt;
      87             :   ulong idle_cnt;
      88             : 
      89             :   /* Most of the gui tile uses fd_clock for timing, but some stem
      90             :      timestamps still used tickcounts, so we keep separate timestamps
      91             :      here to handle those cases until fd_clock is more widely adopted. */
      92             :   long ref_wallclock;
      93             :   long ref_tickcount;
      94             :   double tick_per_ns;
      95             : 
      96             :   fd_clock_tile_t clock[1];
      97             : 
      98             :   ulong chunk;
      99             : 
     100             :   fd_http_server_t * gui_server;
     101             : 
     102             :   long next_poll_deadline;
     103             : 
     104             :   fd_keyswitch_t * keyswitch;
     105             :   uchar const *    identity_key;
     106             : 
     107             :   int               has_vote_key;
     108             :   fd_pubkey_t const vote_key[ 1UL ];
     109             : 
     110             :   ulong           in_kind[ 64UL ];
     111             :   int             in_reliable[ 64UL ];
     112             :   ulong           in_bank_idx[ 64UL ];
     113             :   fd_guih_in_ctx_t in[ 64UL ];
     114             : } fd_guih_ctx_t;
     115             : 
     116             : FD_FN_CONST static inline ulong
     117           0 : scratch_align( void ) {
     118           0 :   ulong a = alignof( fd_guih_ctx_t );
     119           0 :   a = fd_ulong_max( a, fd_http_server_align() );
     120           0 :   a = fd_ulong_max( a, fd_guih_align() );
     121           0 :   a = fd_ulong_max( a, fd_alloc_align() );
     122           0 :   return a;
     123           0 : }
     124             : 
     125             : static inline ulong
     126           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     127           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     128           0 :   ulong http_fp = fd_http_server_footprint( http_param );
     129           0 :   if( FD_UNLIKELY( !http_fp ) ) FD_LOG_ERR(( "Invalid [tiles.gui] config parameters" ));
     130             : 
     131           0 :   ulong l = FD_LAYOUT_INIT;
     132           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_guih_ctx_t ), sizeof( fd_guih_ctx_t ) );
     133           0 :   l = FD_LAYOUT_APPEND( l, fd_http_server_align(),  http_fp );
     134           0 :   l = FD_LAYOUT_APPEND( l, fd_guih_align(),          fd_guih_footprint( tile->gui.tile_cnt ) );
     135           0 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),        fd_alloc_footprint() );
     136           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     137           0 : }
     138             : 
     139             : FD_FN_PURE static inline ulong
     140           0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
     141           0 :   return 256UL * (1UL<<20UL); /* 256MiB of heap space for the cJSON allocator */
     142           0 : }
     143             : 
     144             : static inline void
     145           0 : during_housekeeping( fd_guih_ctx_t * ctx ) {
     146           0 :   ctx->ref_wallclock = fd_log_wallclock();
     147           0 :   ctx->ref_tickcount = fd_tickcount();
     148             : 
     149           0 :   if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) {
     150           0 :     fd_clock_tile_recal( ctx->clock );
     151           0 :   }
     152             : 
     153           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     154           0 :     fd_guih_set_identity( ctx->gui, ctx->keyswitch->bytes );
     155           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     156           0 :   }
     157           0 : }
     158             : 
     159             : static inline void
     160           0 : metrics_write( fd_guih_ctx_t * ctx ) {
     161           0 :   FD_MGAUGE_SET( GUIH, CONN_ACTIVE, ctx->gui_server->metrics.connection_cnt );
     162           0 :   FD_MGAUGE_SET( GUIH, WEBSOCKET_CONN_ACTIVE, ctx->gui_server->metrics.ws_connection_cnt );
     163             : 
     164           0 :   FD_MCNT_SET( GUIH, WEBSOCKET_FRAME_TX, ctx->gui_server->metrics.frames_written );
     165           0 :   FD_MCNT_SET( GUIH, WEBSOCKET_FRAME_RX, ctx->gui_server->metrics.frames_read );
     166             : 
     167           0 :   FD_MCNT_SET( GUIH, BYTES_WRITTEN, ctx->gui_server->metrics.bytes_written );
     168           0 :   FD_MCNT_SET( GUIH, BYTES_READ,    ctx->gui_server->metrics.bytes_read );
     169           0 : }
     170             : 
     171             : static void
     172             : before_credit( fd_guih_ctx_t *      ctx,
     173             :                fd_stem_context_t * stem,
     174           0 :                int *               charge_busy ) {
     175           0 :   (void)stem;
     176             : 
     177           0 :   ctx->idle_cnt++;
     178           0 :   if( FD_LIKELY( ctx->idle_cnt<2UL*ctx->in_cnt ) ) return;
     179           0 :   ctx->idle_cnt = 0UL;
     180             : 
     181           0 :   int charge_busy_server = 0;
     182           0 :   long now = fd_tickcount();
     183           0 :   if( FD_UNLIKELY( now>=ctx->next_poll_deadline ) ) {
     184           0 :     charge_busy_server = fd_http_server_poll( ctx->gui_server, 0 );
     185           0 :     ctx->next_poll_deadline = fd_tickcount() + (long)(ctx->tick_per_ns * 128L * 1000L);
     186           0 :   }
     187             : 
     188           0 :   int charge_poll = 0;
     189           0 :   charge_poll |= fd_guih_poll( ctx->gui, fd_clock_tile_now( ctx->clock ) );
     190             : 
     191           0 :   *charge_busy = charge_busy_server | charge_poll;
     192           0 : }
     193             : 
     194             : static int
     195             : before_frag( fd_guih_ctx_t * ctx,
     196             :              ulong          in_idx,
     197             :              ulong          seq,
     198           0 :              ulong          sig ) {
     199           0 :   (void)seq;
     200             : 
     201             :   /* Ignore "done draining banks" and "reduce microblock bound" signals from pack->poh */
     202           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;
     203             : 
     204           0 :   return 0;
     205           0 : }
     206             : 
     207             : static inline void
     208             : during_frag( fd_guih_ctx_t * ctx,
     209             :              ulong          in_idx,
     210             :              ulong          seq FD_PARAM_UNUSED,
     211             :              ulong          sig,
     212             :              ulong          chunk,
     213             :              ulong          sz,
     214           0 :              ulong          ctl FD_PARAM_UNUSED ) {
     215             : 
     216           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
     217             : 
     218           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PLUGIN ) ) {
     219             :     /* ... todo... sigh, sz is not correct since it's too big */
     220           0 :     if( FD_LIKELY( sig==FD_PLUGIN_MSG_GOSSIP_UPDATE ) ) {
     221           0 :       ulong peer_cnt = FD_LOAD( ulong, src );
     222           0 :       FD_TEST( peer_cnt<=FD_GUIH_MAX_PEER_CNT );
     223           0 :       sz = 8UL + peer_cnt*FD_GOSSIP_LINK_MSG_SIZE;
     224           0 :     } else if( FD_LIKELY( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE ) ) {
     225           0 :       ulong peer_cnt = FD_LOAD( ulong, src );
     226           0 :       FD_TEST( peer_cnt<=FD_GUIH_MAX_PEER_CNT );
     227           0 :       sz = 8UL + peer_cnt*112UL;
     228           0 :     } else if( FD_UNLIKELY( sig==FD_PLUGIN_MSG_LEADER_SCHEDULE ) ) {
     229           0 :       ulong staked_vote_cnt = FD_LOAD( ulong, src+8UL );
     230           0 :       ulong staked_id_cnt   = FD_LOAD( ulong, src+16UL );
     231           0 :       FD_TEST( staked_vote_cnt<=MAX_COMPRESSED_STAKE_WEIGHTS );
     232           0 :       FD_TEST( staked_id_cnt<=MAX_SHRED_DESTS );
     233           0 :       sz = fd_stake_weight_msg_sz( staked_vote_cnt, staked_id_cnt );
     234           0 :     }
     235           0 :   }
     236             : 
     237           0 :   if( FD_UNLIKELY( (sz>0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark)) || sz>ctx->in[ in_idx ].mtu ) )
     238           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 ));
     239             : 
     240           0 :   ctx->chunk = chunk;
     241           0 : }
     242             : 
     243             : static inline void
     244             : after_frag( fd_guih_ctx_t *      ctx,
     245             :             ulong               in_idx,
     246             :             ulong               seq,
     247             :             ulong               sig,
     248             :             ulong               sz,
     249             :             ulong               tsorig FD_PARAM_UNUSED,
     250             :             ulong               tspub,
     251           0 :             fd_stem_context_t * stem ) {
     252           0 :   (void)seq; (void)stem;
     253             : 
     254           0 :   if( FD_LIKELY( ctx->in_reliable[ in_idx ] ) ) ctx->idle_cnt = 0UL;
     255             : 
     256           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
     257             : 
     258           0 :   switch( ctx->in_kind[ in_idx ] ) {
     259           0 :     case IN_KIND_PLUGIN: {
     260           0 :       fd_guih_plugin_message( ctx->gui, sig, src, fd_clock_tile_now( ctx->clock ) );
     261           0 :       break;
     262           0 :     }
     263           0 :     case IN_KIND_POH_PACK: {
     264           0 :       FD_TEST( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_BECAME_LEADER );
     265           0 :       fd_became_leader_t * became_leader = (fd_became_leader_t *)src;
     266           0 :       fd_guih_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 );
     267           0 :       break;
     268           0 :     }
     269           0 :     case IN_KIND_PACK_POH: {
     270           0 :       fd_guih_unbecame_leader( ctx->gui, fd_disco_execle_sig_slot( sig ), (fd_done_packing_t const *)src, fd_clock_tile_now( ctx->clock ) );
     271           0 :       break;
     272           0 :     }
     273           0 :     case IN_KIND_PACK_EXECLE: {
     274           0 :       FD_TEST( sz>=sizeof(fd_microblock_execle_trailer_t) );
     275           0 :       FD_TEST( (sz-sizeof(fd_microblock_execle_trailer_t))%sizeof(fd_txn_e_t)==0UL );
     276             : 
     277           0 :       if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK ) ) {
     278           0 :         fd_microblock_execle_trailer_t trailer[1];
     279           0 :         fd_memcpy( trailer, src+sz-sizeof(fd_microblock_execle_trailer_t), sizeof(fd_microblock_execle_trailer_t) );
     280           0 :         long tspub_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     281           0 :         fd_guih_microblock_execution_begin( ctx->gui,
     282           0 :                                           tspub_ns,
     283           0 :                                           fd_disco_poh_sig_slot( sig ),
     284           0 :                                           (fd_txn_e_t *)src,
     285           0 :                                           (sz-sizeof( fd_microblock_execle_trailer_t ))/sizeof( fd_txn_e_t ),
     286           0 :                                           (uint)trailer->microblock_idx,
     287           0 :                                           trailer->pack_txn_idx );
     288           0 :       } else {
     289           0 :         FD_LOG_ERR(( "unexpected poh packet type %lu", fd_disco_poh_sig_pkt_type( sig ) ));
     290           0 :       }
     291           0 :       break;
     292           0 :     }
     293           0 :     case IN_KIND_EXECLE_POH: {
     294           0 :       FD_TEST( sz>=sizeof(fd_microblock_trailer_t) );
     295           0 :       FD_TEST( (sz-sizeof(fd_microblock_trailer_t))%sizeof(fd_txn_p_t)==0UL );
     296             : 
     297           0 :       fd_microblock_trailer_t trailer[1];
     298           0 :       fd_memcpy( trailer, src+sz-sizeof(fd_microblock_trailer_t), sizeof(fd_microblock_trailer_t) );
     299           0 :       long tspub_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
     300           0 :       ulong txn_cnt = (sz-sizeof( fd_microblock_trailer_t ))/sizeof(fd_txn_p_t);
     301           0 :       fd_guih_microblock_execution_end( ctx->gui,
     302           0 :                                       tspub_ns,
     303           0 :                                       ctx->in_bank_idx[ in_idx ],
     304           0 :                                       fd_disco_execle_sig_slot( sig ),
     305           0 :                                       txn_cnt,
     306           0 :                                       (fd_txn_p_t *)src,
     307           0 :                                       trailer->pack_txn_idx,
     308           0 :                                       trailer->txn_ns_dt,
     309           0 :                                       trailer->tips );
     310           0 :       break;
     311           0 :     }
     312           0 :     case IN_KIND_BUNDLE: {
     313           0 :       fd_guih_handle_block_engine_update( ctx->gui, (fd_bundle_block_engine_update_t *)src );
     314           0 :       break;
     315           0 :     }
     316           0 :     default: FD_LOG_ERR(( "unexpected in_kind %lu", ctx->in_kind[ in_idx ] ));
     317           0 :   }
     318           0 : }
     319             : 
     320             : static fd_http_server_response_t
     321           0 : gui_http_request( fd_http_server_request_t const * request ) {
     322           0 :   if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
     323           0 :     return (fd_http_server_response_t){
     324           0 :       .status = 405,
     325           0 :     };
     326           0 :   }
     327             : 
     328           0 :   if( FD_LIKELY( !strcmp( request->path, "/websocket" ) ) ) {
     329           0 :     return (fd_http_server_response_t){
     330           0 :       .status            = 200,
     331           0 :       .upgrade_websocket = 1,
     332           0 : #ifdef FD_HAS_ZSTD
     333           0 :       .compress_websocket = request->headers.compress_websocket,
     334             : #else
     335             :       .compress_websocket = 0,
     336             : #endif
     337           0 :     };
     338           0 :   } else if( FD_LIKELY( !strcmp( request->path, "/favicon.svg" ) ) ) {
     339           0 :     return (fd_http_server_response_t){
     340           0 :       .status            = 200,
     341           0 :       .static_body       = firedancer_svg,
     342           0 :       .static_body_len   = firedancer_svg_sz,
     343           0 :       .content_type      = "image/svg+xml",
     344           0 :       .upgrade_websocket = 0,
     345           0 :     };
     346           0 :   }
     347             : 
     348           0 :   int is_vite_page = !strcmp( request->path, "/" ) ||
     349           0 :                      !strcmp( request->path, "/slotDetails" ) ||
     350           0 :                      !strcmp( request->path, "/leaderSchedule" ) ||
     351           0 :                      !strcmp( request->path, "/gossip") ||
     352           0 :                      !strncmp( request->path, "/?", strlen("/?") ) ||
     353           0 :                      !strncmp( request->path, "/slotDetails?", strlen("/slotDetails?") ) ||
     354           0 :                      !strncmp( request->path, "/leaderSchedule?", strlen("/leaderSchedule?") ) ||
     355           0 :                      !strncmp( request->path, "/gossip?", strlen("/gossip?") );
     356             : 
     357           0 :   for( fd_http_static_file_t const * f = STATIC_FILES; f->name; f++ ) {
     358           0 :     if( !strcmp( request->path, f->name ) ||
     359           0 :         (!strcmp( f->name, "/index.html" ) && is_vite_page) ) {
     360           0 :       char const * content_type = NULL;
     361             : 
     362           0 :       char const * ext = strrchr( f->name, '.' );
     363           0 :       if( FD_LIKELY( ext ) ) {
     364           0 :         if( !strcmp( ext, ".html" ) ) content_type = "text/html; charset=utf-8";
     365           0 :         else if( !strcmp( ext, ".css" ) ) content_type = "text/css";
     366           0 :         else if( !strcmp( ext, ".js" ) ) content_type = "application/javascript";
     367           0 :         else if( !strcmp( ext, ".svg" ) ) content_type = "image/svg+xml";
     368           0 :         else if( !strcmp( ext, ".woff" ) ) content_type = "font/woff";
     369           0 :         else if( !strcmp( ext, ".woff2" ) ) content_type = "font/woff2";
     370           0 :       }
     371             : 
     372           0 :       char const * cache_control = NULL;
     373           0 :       if( FD_LIKELY( !strncmp( request->path, "/assets", 7 ) ) ) cache_control = "public, max-age=31536000, immutable";
     374           0 :       else if( FD_LIKELY( !strcmp( f->name, "/index.html" ) ) )  cache_control = "no-cache";
     375             : 
     376           0 :       const uchar * data = f->data;
     377           0 :       ulong data_len = *(f->data_len);
     378             : 
     379           0 :       int accepts_zstd = 0;
     380           0 :       if( FD_LIKELY( request->headers.accept_encoding ) ) {
     381           0 :         accepts_zstd = !!strstr( request->headers.accept_encoding, "zstd" );
     382           0 :       }
     383             : 
     384           0 :       int accepts_gzip = 0;
     385           0 :       if( FD_LIKELY( request->headers.accept_encoding ) ) {
     386           0 :         accepts_gzip = !!strstr( request->headers.accept_encoding, "gzip" );
     387           0 :       }
     388             : 
     389           0 :       char const * content_encoding = NULL;
     390           0 :       if( FD_LIKELY( accepts_zstd && f->zstd_data ) ) {
     391           0 :         content_encoding = "zstd";
     392           0 :         data = f->zstd_data;
     393           0 :         data_len = *(f->zstd_data_len);
     394           0 :       } else if( FD_LIKELY( accepts_gzip && f->gzip_data ) ) {
     395           0 :         content_encoding = "gzip";
     396           0 :         data = f->gzip_data;
     397           0 :         data_len = *(f->gzip_data_len);
     398           0 :       }
     399             : 
     400           0 :       return (fd_http_server_response_t){
     401           0 :         .status            = 200,
     402           0 :         .static_body       = data,
     403           0 :         .static_body_len   = data_len,
     404           0 :         .content_type      = content_type,
     405           0 :         .cache_control     = cache_control,
     406           0 :         .content_encoding  = content_encoding,
     407           0 :         .upgrade_websocket = 0,
     408           0 :       };
     409           0 :     }
     410           0 :   }
     411             : 
     412           0 :   return (fd_http_server_response_t){
     413           0 :     .status            = 404,
     414           0 :   };
     415           0 : }
     416             : 
     417             : static void
     418             : gui_ws_open( ulong  conn_id,
     419           0 :              void * _ctx ) {
     420           0 :   fd_guih_ctx_t * ctx = (fd_guih_ctx_t *)_ctx;
     421             : 
     422           0 :   fd_guih_ws_open( ctx->gui, conn_id, fd_clock_tile_now( ctx->clock ) );
     423           0 : }
     424             : 
     425             : static void
     426             : gui_ws_close( ulong  conn_id,
     427             :               int    reason,
     428           0 :               void * _ctx ) {
     429           0 :   (void) reason;
     430           0 :   (void) conn_id;
     431           0 :   (void) _ctx;
     432           0 : }
     433             : 
     434             : static void
     435             : gui_ws_message( ulong         ws_conn_id,
     436             :                 uchar const * data,
     437             :                 ulong         data_len,
     438           0 :                 void *        _ctx ) {
     439           0 :   fd_guih_ctx_t * ctx = (fd_guih_ctx_t *)_ctx;
     440             : 
     441           0 :   int reason = fd_guih_ws_message( ctx->gui, ws_conn_id, data, data_len );
     442             : 
     443           0 :   if( FD_UNLIKELY( reason<0 ) ) fd_http_server_ws_close( ctx->gui_server, ws_conn_id, reason );
     444           0 : }
     445             : 
     446             : static void
     447             : privileged_init( fd_topo_t const *      topo,
     448           0 :                  fd_topo_tile_t const * tile ) {
     449           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     450             : 
     451           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     452           0 :   fd_guih_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_guih_ctx_t ), sizeof( fd_guih_ctx_t ) );
     453             : 
     454           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     455           0 :   fd_http_server_t * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
     456             : 
     457           0 :   fd_http_server_callbacks_t gui_callbacks = {
     458           0 :     .request    = gui_http_request,
     459           0 :     .ws_open    = gui_ws_open,
     460           0 :     .ws_close   = gui_ws_close,
     461           0 :     .ws_message = gui_ws_message,
     462           0 :   };
     463           0 :   ctx->gui_server = fd_http_server_join( fd_http_server_new( _gui, http_param, gui_callbacks, ctx ) );
     464           0 :   fd_http_server_listen( ctx->gui_server, tile->gui.listen_addr, tile->gui.listen_port );
     465             : 
     466           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 ));
     467             : 
     468           0 :   if( FD_UNLIKELY( !strcmp( tile->gui.identity_key_path, "" ) ) )
     469           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     470             : 
     471           0 :   ctx->identity_key = fd_keyload_load( tile->gui.identity_key_path, /* pubkey only: */ 1 );
     472             : 
     473           0 :   if( FD_UNLIKELY( !strcmp( tile->gui.vote_key_path, "" ) ) ) {
     474           0 :     ctx->has_vote_key = 0;
     475           0 :   } else {
     476           0 :     ctx->has_vote_key = 1;
     477           0 :     if( FD_UNLIKELY( !fd_base58_decode_32( tile->gui.vote_key_path, (uchar *)ctx->vote_key->uc ) ) ) {
     478           0 :       const uchar * vote_key = fd_keyload_load( tile->gui.vote_key_path, /* pubkey only: */ 1 );
     479           0 :       fd_memcpy( (uchar *)ctx->vote_key->uc, vote_key, 32UL );
     480           0 :     }
     481           0 :   }
     482           0 : }
     483             : 
     484             : static void
     485             : unprivileged_init( fd_topo_t const *      topo,
     486           0 :                    fd_topo_tile_t const * tile ) {
     487           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     488             : 
     489           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     490           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     491           0 :   fd_guih_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_guih_ctx_t ), sizeof( fd_guih_ctx_t )                                    );
     492           0 :                        FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(),  fd_http_server_footprint( http_param )                    );
     493           0 :   void * _gui        = FD_SCRATCH_ALLOC_APPEND( l, fd_guih_align(),          fd_guih_footprint( tile->gui.tile_cnt )                    );
     494           0 :   void * _alloc      = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(),        fd_alloc_footprint()                                      );
     495             : 
     496           0 :   fd_clock_tile_init( ctx->clock );
     497             : 
     498           0 :   ctx->ref_wallclock = fd_log_wallclock();
     499           0 :   ctx->ref_tickcount = fd_tickcount();
     500           0 :   ctx->tick_per_ns = fd_tempo_tick_per_ns( NULL );
     501             : 
     502           0 :   ctx->topo = topo;
     503           0 :   ctx->gui   = fd_guih_join( fd_guih_new( _gui, ctx->gui_server, fd_version_cstr, tile->gui.cluster, ctx->identity_key, ctx->has_vote_key, ctx->vote_key->uc, 0, 0, tile->gui.is_voting, tile->gui.schedule_strategy, tile->gui.wfs_bank_hash, tile->gui.expected_shred_version, ctx->topo, fd_clock_tile_now( ctx->clock ) ) );
     504           0 :   FD_TEST( ctx->gui );
     505             : 
     506           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
     507           0 :   FD_TEST( ctx->keyswitch );
     508             : 
     509           0 :   fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
     510           0 :   FD_TEST( alloc );
     511           0 :   cJSON_alloc_install( alloc );
     512             : 
     513           0 :   ctx->next_poll_deadline = fd_tickcount();
     514             : 
     515           0 :   ctx->idle_cnt = 0UL;
     516           0 :   ctx->in_cnt = tile->in_cnt;
     517             : 
     518           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     519           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     520           0 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     521             : 
     522           0 :     if( FD_LIKELY( !strcmp( link->name, "plugin_out"        ) ) ) ctx->in_kind[ i ] = IN_KIND_PLUGIN;
     523           0 :     else if( FD_LIKELY( !strcmp( link->name, "pohh_pack"    ) ) ) ctx->in_kind[ i ] = IN_KIND_POH_PACK;
     524           0 :     else if( FD_LIKELY( !strcmp( link->name, "pack_bank"    ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_EXECLE;
     525           0 :     else if( FD_LIKELY( !strcmp( link->name, "pack_pohh"    ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_POH;
     526           0 :     else if( FD_LIKELY( !strcmp( link->name, "bank_pohh"    ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE_POH;
     527           0 :     else if( FD_LIKELY( !strcmp( link->name, "bundle_status" ) ) ) ctx->in_kind[ i ] = IN_KIND_BUNDLE;
     528           0 :     else FD_LOG_ERR(( "gui tile has unexpected input link %lu %s", i, link->name ));
     529             : 
     530           0 :     if( FD_LIKELY( !strcmp( link->name, "bank_pohh" ) ) ) {
     531           0 :       ulong producer = fd_topo_find_link_producer( topo, &topo->links[ tile->in_link_id[ i ] ] );
     532           0 :       ctx->in_bank_idx[ i ] = topo->tiles[ producer ].kind_id;
     533           0 :     }
     534             : 
     535           0 :     ctx->in_reliable[ i ] = tile->in_link_reliable[ i ];
     536           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
     537           0 :     ctx->in[ i ].mtu    = link->mtu;
     538           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
     539           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
     540           0 :   }
     541             : 
     542           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     543           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     544           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     545           0 : }
     546             : 
     547             : static ulong
     548             : populate_allowed_seccomp( fd_topo_t const *      topo,
     549             :                           fd_topo_tile_t const * tile,
     550             :                           ulong                  out_cnt,
     551           0 :                           struct sock_filter *   out ) {
     552           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     553           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     554           0 :   fd_guih_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_guih_ctx_t ), sizeof( fd_guih_ctx_t ) );
     555             : 
     556           0 :   populate_sock_filter_policy_fd_guih_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)fd_http_server_fd( ctx->gui_server ) );
     557           0 :   return sock_filter_policy_fd_guih_tile_instr_cnt;
     558           0 : }
     559             : 
     560             : static ulong
     561             : populate_allowed_fds( fd_topo_t const *      topo,
     562             :                       fd_topo_tile_t const * tile,
     563             :                       ulong                  out_fds_cnt,
     564           0 :                       int *                  out_fds ) {
     565           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     566           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     567           0 :   fd_guih_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_guih_ctx_t ), sizeof( fd_guih_ctx_t ) );
     568             : 
     569           0 :   if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     570             : 
     571           0 :   ulong out_cnt = 0UL;
     572           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     573           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     574           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     575           0 :   out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->gui_server ); /* gui listen socket */
     576           0 :   return out_cnt;
     577           0 : }
     578             : 
     579             : static ulong
     580             : rlimit_file_cnt( fd_topo_t const *      topo FD_PARAM_UNUSED,
     581           0 :                  fd_topo_tile_t const * tile ) {
     582             :   /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
     583           0 :   ulong base = 5UL;
     584           0 :   return base + tile->gui.max_http_connections + tile->gui.max_websocket_connections;
     585           0 : }
     586             : 
     587           0 : #define STEM_BURST (2UL)
     588             : 
     589             : /* See explanation in fd_pack */
     590           0 : #define STEM_LAZY  (128L*3000L)
     591             : 
     592           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_guih_ctx_t
     593           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_guih_ctx_t)
     594             : 
     595           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     596           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     597           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     598           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
     599           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     600           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     601             : 
     602             : #include "../../disco/stem/fd_stem.c"
     603             : 
     604             : fd_topo_run_tile_t fd_tile_guih = {
     605             :   .name                     = "guih",
     606             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
     607             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     608             :   .populate_allowed_fds     = populate_allowed_fds,
     609             :   .scratch_align            = scratch_align,
     610             :   .scratch_footprint        = scratch_footprint,
     611             :   .loose_footprint          = loose_footprint,
     612             :   .privileged_init          = privileged_init,
     613             :   .unprivileged_init        = unprivileged_init,
     614             :   .run                      = stem_run,
     615             : };

Generated by: LCOV version 1.14