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

Generated by: LCOV version 1.14