LCOV - code coverage report
Current view: top level - disco/gui - fd_gui_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 41 347 11.8 %
Date: 2025-03-20 12:08:36 Functions: 5 20 25.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 "generated/http_import_dist.h"
      10           3 : #define DIST_COMPRESSION_LEVEL (19)
      11             : 
      12             : #include <sys/socket.h> /* SOCK_CLOEXEC, SOCK_NONBLOCK needed for seccomp filter */
      13             : #if defined(__aarch64__)
      14             : #include "generated/fd_gui_tile.arm64_seccomp.h"
      15             : #else
      16             : #include "generated/fd_gui_tile_seccomp.h"
      17             : #endif
      18             : 
      19             : extern ulong const fdctl_major_version;
      20             : extern ulong const fdctl_minor_version;
      21             : extern ulong const fdctl_patch_version;
      22             : extern uint  const fdctl_commit_ref;
      23             : 
      24             : #include "../../disco/tiles.h"
      25             : #include "../../disco/keyguard/fd_keyload.h"
      26             : #include "../../disco/keyguard/fd_keyswitch.h"
      27             : #include "../../disco/gui/fd_gui.h"
      28             : #include "../../disco/plugin/fd_plugin.h"
      29             : #include "../../ballet/http/fd_http_server.h"
      30             : #include "../../ballet/json/cJSON.h"
      31             : 
      32             : #include <sys/types.h>
      33             : #include <unistd.h>
      34             : #include <string.h>
      35             : #include <poll.h>
      36             : #include <stdio.h>
      37             : 
      38             : #if FD_HAS_ZSTD
      39             : #define ZSTD_STATIC_LINKING_ONLY
      40             : #include <zstd.h>
      41             : #endif
      42             : 
      43           0 : #define IN_KIND_PLUGIN    (0UL)
      44           0 : #define IN_KIND_POH_PACK  (1UL)
      45           0 : #define IN_KIND_PACK_BANK (2UL)
      46           0 : #define IN_KIND_BANK_POH  (3UL)
      47             : 
      48             : FD_IMPORT_BINARY( firedancer_svg, "book/public/fire.svg" );
      49             : 
      50           3 : #define FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN       8192
      51           3 : #define FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN 8192
      52           3 : #define FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT 8192
      53             : 
      54             : fd_http_server_params_t
      55           3 : derive_http_params( fd_topo_tile_t const * tile ) {
      56           3 :   return (fd_http_server_params_t) {
      57           3 :     .max_connection_cnt    = tile->gui.max_http_connections,
      58           3 :     .max_ws_connection_cnt = tile->gui.max_websocket_connections,
      59           3 :     .max_request_len       = FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN,
      60           3 :     .max_ws_recv_frame_len = FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN,
      61           3 :     .max_ws_send_frame_cnt = FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT,
      62           3 :     .outgoing_buffer_sz    = tile->gui.send_buffer_size_mb * (1UL<<20UL),
      63           3 :   };
      64           3 : }
      65             : 
      66             : struct fd_gui_in_ctx {
      67             :   fd_wksp_t * mem;
      68             :   ulong       mtu;
      69             :   ulong       chunk0;
      70             :   ulong       wmark;
      71             : };
      72             : 
      73             : typedef struct fd_gui_in_ctx fd_gui_in_ctx_t;
      74             : 
      75             : typedef struct {
      76             :   fd_topo_t * topo;
      77             : 
      78             :   fd_gui_t * gui;
      79             : 
      80             :   /* This needs to be max(plugin_msg) across all kinds of messages.
      81             :      Currently this is just figured out manually, it's a gossip update
      82             :      message assuming the table is completely full (40200) of peers. */
      83             :   uchar      buf[ 8UL+40200UL*(58UL+12UL*34UL) ] __attribute__((aligned(8)));
      84             : 
      85             :   fd_http_server_t * gui_server;
      86             : 
      87             :   long next_poll_deadline;
      88             : 
      89             :   char          version_string[ 16UL ];
      90             : 
      91             :   fd_keyswitch_t * keyswitch;
      92             :   uchar const *    identity_key;
      93             : 
      94             :   ulong           in_kind[ 64UL ];
      95             :   ulong           in_bank_idx[ 64UL ];
      96             :   fd_gui_in_ctx_t in[ 64UL ];
      97             : } fd_gui_ctx_t;
      98             : 
      99             : FD_FN_CONST static inline ulong
     100           3 : scratch_align( void ) {
     101           3 :   return 128UL;
     102           3 : }
     103             : 
     104             : FD_FN_PURE static inline ulong
     105           3 : scratch_footprint( fd_topo_tile_t const * tile ) {
     106           3 :   ulong http_fp = fd_http_server_footprint( derive_http_params( tile ) );
     107           3 :   if( FD_UNLIKELY( !http_fp ) ) FD_LOG_ERR(( "Invalid [tiles.gui] config parameters" ));
     108             : 
     109           3 :   ulong l = FD_LAYOUT_INIT;
     110           3 :   l = FD_LAYOUT_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     111           3 :   l = FD_LAYOUT_APPEND( l, fd_http_server_align(), http_fp );
     112           3 :   l = FD_LAYOUT_APPEND( l, fd_gui_align(),         fd_gui_footprint() );
     113           3 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),       fd_alloc_footprint() );
     114           3 :   return FD_LAYOUT_FINI( l, scratch_align() );
     115           3 : }
     116             : 
     117             : /* dist_file_sz returns the sum of static asset file sizes */
     118             : 
     119             : FD_FN_CONST static ulong
     120           3 : dist_file_sz( void ) {
     121           3 :   ulong tot_sz = 0UL;
     122          36 :   for( fd_http_static_file_t * f = STATIC_FILES; f->name; f++ ) {
     123          33 :     tot_sz += *(f->data_len);
     124          33 :   }
     125           3 :   return tot_sz;
     126           3 : }
     127             : 
     128             : FD_FN_PURE static inline ulong
     129           3 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
     130             :   /* Reserve total size of files for compression buffers */
     131           3 :   return fd_spad_footprint( dist_file_sz() ) +
     132           3 : #   if FD_HAS_ZSTD
     133           3 :     fd_ulong_align_up( ZSTD_estimateCCtxSize( DIST_COMPRESSION_LEVEL ), FD_WKSP_ALIGN_DEFAULT ) +
     134           3 : #   endif
     135           3 :     256UL * (1UL<<20UL); /* 256MiB of heap space for the cJSON allocator */
     136           3 : }
     137             : 
     138             : static inline void
     139           0 : during_housekeeping( fd_gui_ctx_t * ctx ) {
     140           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     141           0 :     fd_gui_set_identity( ctx->gui, ctx->keyswitch->bytes );
     142           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     143           0 :   }
     144           0 : }
     145             : 
     146             : static void
     147             : before_credit( fd_gui_ctx_t *      ctx,
     148             :                fd_stem_context_t * stem,
     149           0 :                int *               charge_busy ) {
     150           0 :   (void)stem;
     151             : 
     152           0 :   int charge_busy_server = 0;
     153           0 :   long now = fd_tickcount();
     154           0 :   if( FD_UNLIKELY( now>=ctx->next_poll_deadline ) ) {
     155           0 :     charge_busy_server = fd_http_server_poll( ctx->gui_server, 0 );
     156           0 :     ctx->next_poll_deadline = fd_tickcount() + (long)(fd_tempo_tick_per_ns( NULL ) * 128L * 1000L);
     157           0 :   }
     158             : 
     159           0 :   int charge_poll = fd_gui_poll( ctx->gui );
     160             : 
     161           0 :   *charge_busy = charge_busy_server | charge_poll;
     162           0 : }
     163             : 
     164             : static inline void
     165             : during_frag( fd_gui_ctx_t * ctx,
     166             :              ulong          in_idx,
     167             :              ulong          seq    FD_PARAM_UNUSED,
     168             :              ulong          sig,
     169             :              ulong          chunk,
     170             :              ulong          sz,
     171           0 :              ulong          gui    FD_PARAM_UNUSED ) {
     172             : 
     173           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
     174             : 
     175           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PLUGIN ) ) {
     176             :     /* ... todo... sigh, sz is not correct since it's too big */
     177           0 :     if( FD_LIKELY( sig==FD_PLUGIN_MSG_GOSSIP_UPDATE ) ) {
     178           0 :       ulong peer_cnt = ((ulong *)src)[ 0 ];
     179           0 :       FD_TEST( peer_cnt<=40200 );
     180           0 :       sz = 8UL + peer_cnt*FD_GOSSIP_LINK_MSG_SIZE;
     181           0 :     } else if( FD_LIKELY( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE ) ) {
     182           0 :       ulong peer_cnt = ((ulong *)src)[ 0 ];
     183           0 :       FD_TEST( peer_cnt<=40200 );
     184           0 :       sz = 8UL + peer_cnt*112UL;
     185           0 :     } else if( FD_UNLIKELY( sig==FD_PLUGIN_MSG_LEADER_SCHEDULE ) ) {
     186           0 :       ulong staked_cnt = ((ulong *)src)[ 1 ];
     187           0 :       FD_TEST( staked_cnt<=50000UL );
     188           0 :       sz = 40UL + staked_cnt*40UL;
     189           0 :     }
     190             : 
     191           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>sizeof( ctx->buf ) ) )
     192           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     193             : 
     194           0 :     fd_memcpy( ctx->buf, src, sz );
     195           0 :     return;
     196           0 :   }
     197             : 
     198           0 :   if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
     199           0 :     FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     200             : 
     201           0 :   fd_memcpy( ctx->buf, src, sz );
     202           0 : }
     203             : 
     204             : static inline void
     205             : after_frag( fd_gui_ctx_t *      ctx,
     206             :             ulong               in_idx,
     207             :             ulong               seq,
     208             :             ulong               sig,
     209             :             ulong               sz,
     210             :             ulong               tsorig,
     211             :             ulong               tspub,
     212           0 :             fd_stem_context_t * stem ) {
     213           0 :   (void)in_idx;
     214           0 :   (void)seq;
     215           0 :   (void)sz;
     216           0 :   (void)tsorig;
     217           0 :   (void)tspub;
     218           0 :   (void)stem;
     219             : 
     220           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PLUGIN ) ) fd_gui_plugin_message( ctx->gui, sig, ctx->buf );
     221           0 :   else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH_PACK ) ) {
     222           0 :     FD_TEST( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_BECAME_LEADER );
     223           0 :     fd_became_leader_t * became_leader = (fd_became_leader_t *)ctx->buf;
     224           0 :     fd_gui_became_leader( ctx->gui, fd_disco_poh_sig_slot( sig ), became_leader->slot_start_ns, became_leader->slot_end_ns, FD_PACK_MAX_COST_PER_BLOCK, became_leader->max_microblocks_in_slot );
     225           0 :   } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PACK_BANK ) ) {
     226           0 :     if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK ) ) {
     227           0 :       FD_TEST( sz<ULONG_MAX );
     228           0 :       fd_gui_execution_begin( ctx->gui, fd_frag_meta_ts_decomp( tspub, fd_tickcount() ), fd_disco_poh_sig_slot( sig ), fd_disco_poh_sig_bank_tile( sig ), (sz-sizeof( fd_microblock_bank_trailer_t ))/sizeof( fd_txn_p_t ), (fd_txn_p_t *)ctx->buf );
     229           0 :     } else if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_DONE_PACKING ) ) {
     230           0 :       fd_gui_unbecame_leader( ctx->gui, fd_disco_poh_sig_slot( sig ), ((fd_done_packing_t *)ctx->buf)->microblocks_in_slot );
     231           0 :     } else {
     232           0 :       FD_LOG_ERR(( "unexpected poh packet type %lu", fd_disco_poh_sig_pkt_type( sig ) ));
     233           0 :     }
     234           0 :   } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_BANK_POH ) ) {
     235           0 :     fd_microblock_trailer_t * trailer = (fd_microblock_trailer_t *)( ctx->buf+sz-sizeof( fd_microblock_trailer_t ) );
     236           0 :     fd_gui_execution_end( ctx->gui, fd_frag_meta_ts_decomp( tspub, fd_tickcount() ), ctx->in_bank_idx[ in_idx ], trailer->is_last_in_bundle, fd_disco_bank_sig_slot( sig ), (sz-sizeof( fd_microblock_trailer_t ))/sizeof( fd_txn_p_t ), (fd_txn_p_t *)ctx->buf );
     237           0 :   } else {
     238           0 :     FD_LOG_ERR(( "unexpected in_kind %lu", ctx->in_kind[ in_idx ] ));
     239           0 :   }
     240           0 : }
     241             : 
     242             : static fd_http_server_response_t
     243           0 : gui_http_request( fd_http_server_request_t const * request ) {
     244           0 :   if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
     245           0 :     return (fd_http_server_response_t){
     246           0 :       .status = 405,
     247           0 :     };
     248           0 :   }
     249             : 
     250           0 :   if( FD_LIKELY( !strcmp( request->path, "/websocket" ) ) ) {
     251           0 :     return (fd_http_server_response_t){
     252           0 :       .status            = 200,
     253           0 :       .upgrade_websocket = 1,
     254           0 :     };
     255           0 :   } else if( FD_LIKELY( !strcmp( request->path, "/favicon.svg" ) ) ) {
     256           0 :     return (fd_http_server_response_t){
     257           0 :       .status            = 200,
     258           0 :       .static_body       = firedancer_svg,
     259           0 :       .static_body_len   = firedancer_svg_sz,
     260           0 :       .content_type      = "image/svg+xml",
     261           0 :       .upgrade_websocket = 0,
     262           0 :     };
     263           0 :   }
     264             : 
     265           0 :   int is_vite_page = !strcmp( request->path, "/" ) ||
     266           0 :                      !strcmp( request->path, "/leaderSchedule" ) ||
     267           0 :                      !strcmp( request->path, "/gossip" );
     268             : 
     269           0 :   for( fd_http_static_file_t * f = STATIC_FILES; f->name; f++ ) {
     270           0 :     if( !strcmp( request->path, f->name ) ||
     271           0 :         (!strcmp( f->name, "/index.html" ) && is_vite_page) ) {
     272           0 :       char const * content_type = NULL;
     273             : 
     274           0 :       char const * ext = strrchr( f->name, '.' );
     275           0 :       if( FD_LIKELY( ext ) ) {
     276           0 :         if( !strcmp( ext, ".html" ) ) content_type = "text/html; charset=utf-8";
     277           0 :         else if( !strcmp( ext, ".css" ) ) content_type = "text/css";
     278           0 :         else if( !strcmp( ext, ".js" ) ) content_type = "application/javascript";
     279           0 :         else if( !strcmp( ext, ".svg" ) ) content_type = "image/svg+xml";
     280           0 :         else if( !strcmp( ext, ".woff" ) ) content_type = "font/woff";
     281           0 :         else if( !strcmp( ext, ".woff2" ) ) content_type = "font/woff2";
     282           0 :       }
     283             : 
     284           0 :       char const * cache_control = NULL;
     285           0 :       if( FD_LIKELY( !strncmp( request->path, "/assets", 7 ) ) ) cache_control = "public, max-age=31536000, immutable";
     286             : 
     287           0 :       const uchar * data = f->data;
     288           0 :       ulong data_len = *(f->data_len);
     289             : 
     290           0 :       int accepts_zstd = 0;
     291           0 :       if( FD_LIKELY( request->headers.accept_encoding ) ) {
     292           0 :         accepts_zstd = !!strstr( request->headers.accept_encoding, "zstd" );
     293           0 :       }
     294             : 
     295           0 :       char const * content_encoding = NULL;
     296           0 :       if( FD_LIKELY( accepts_zstd && f->zstd_data ) ) {
     297           0 :         content_encoding = "zstd";
     298           0 :         data = f->zstd_data;
     299           0 :         data_len = f->zstd_data_len;
     300           0 :       }
     301             : 
     302           0 :       return (fd_http_server_response_t){
     303           0 :         .status            = 200,
     304           0 :         .static_body       = data,
     305           0 :         .static_body_len   = data_len,
     306           0 :         .content_type      = content_type,
     307           0 :         .cache_control     = cache_control,
     308           0 :         .content_encoding  = content_encoding,
     309           0 :         .upgrade_websocket = 0,
     310           0 :       };
     311           0 :     }
     312           0 :   }
     313             : 
     314           0 :   return (fd_http_server_response_t){
     315           0 :     .status            = 404,
     316           0 :   };
     317           0 : }
     318             : 
     319             : static void
     320             : gui_ws_open( ulong  conn_id,
     321           0 :              void * _ctx ) {
     322           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     323             : 
     324           0 :   fd_gui_ws_open( ctx->gui, conn_id );
     325           0 : }
     326             : 
     327             : static void
     328             : gui_ws_message( ulong         ws_conn_id,
     329             :                 uchar const * data,
     330             :                 ulong         data_len,
     331           0 :                 void *        _ctx ) {
     332           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     333             : 
     334           0 :   int close = fd_gui_ws_message( ctx->gui, ws_conn_id, data, data_len );
     335           0 :   if( FD_UNLIKELY( close<0 ) ) fd_http_server_ws_close( ctx->gui_server, ws_conn_id, close );
     336           0 : }
     337             : 
     338             : static void
     339             : privileged_init( fd_topo_t *      topo,
     340           0 :                  fd_topo_tile_t * tile ) {
     341           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     342             : 
     343           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     344           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     345             : 
     346           0 :   fd_http_server_params_t http_param = derive_http_params( tile );
     347           0 :   fd_http_server_t * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
     348             : 
     349           0 :   fd_http_server_callbacks_t gui_callbacks = {
     350           0 :     .request    = gui_http_request,
     351           0 :     .ws_open    = gui_ws_open,
     352           0 :     .ws_message = gui_ws_message,
     353           0 :   };
     354           0 :   ctx->gui_server = fd_http_server_join( fd_http_server_new( _gui, http_param, gui_callbacks, ctx ) );
     355           0 :   fd_http_server_listen( ctx->gui_server, tile->gui.listen_addr, tile->gui.listen_port );
     356             : 
     357           0 :   if( FD_UNLIKELY( !strcmp( tile->gui.identity_key_path, "" ) ) )
     358           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     359             : 
     360           0 :   ctx->identity_key = fd_keyload_load( tile->gui.identity_key_path, /* pubkey only: */ 1 );
     361           0 : }
     362             : 
     363             : #if FD_HAS_ZSTD
     364             : 
     365             : /* pre_compress_files compresses static assets into wksp-provided
     366             :    buffers using Zstandard.  Logs warning and continues if insufficient
     367             :    wksp space is available. */
     368             : 
     369             : static void
     370           0 : pre_compress_files( fd_wksp_t * wksp ) {
     371             : 
     372             :   /* Allocate ZSTD compression context.  Freed when exiting this
     373             :      function's scope. */
     374           0 :   ulong  cctx_sz  = ZSTD_estimateCCtxSize( DIST_COMPRESSION_LEVEL );
     375           0 :   void * cctx_mem = fd_wksp_alloc_laddr( wksp, 16UL, cctx_sz, 1UL );
     376           0 :   if( FD_UNLIKELY( !cctx_mem ) ) {
     377           0 :     FD_LOG_WARNING(( "Failed to allocate compressor" ));
     378           0 :     return;
     379           0 :   }
     380           0 :   ZSTD_CCtx * cctx = ZSTD_initStaticCCtx( cctx_mem, cctx_sz );
     381           0 :   if( FD_UNLIKELY( !cctx ) ) {
     382           0 :     FD_LOG_WARNING(( "Failed to create ZSTD compression context" ));
     383           0 :     fd_wksp_free_laddr( cctx_mem );
     384           0 :     return;
     385           0 :   }
     386             : 
     387             :   /* Allocate permanent space for the compressed files. */
     388           0 :   ulong glo, ghi;
     389           0 :   if( FD_UNLIKELY( !fd_wksp_alloc_at_least( wksp, FD_SPAD_ALIGN, loose_footprint( NULL ), 1UL, &glo, &ghi ) ) ) {
     390           0 :     FD_LOG_WARNING(( "Failed to allocate space for compressing assets" ));
     391           0 :     fd_wksp_free_laddr( cctx_mem );
     392           0 :     return;
     393           0 :   }
     394           0 :   fd_spad_t * spad = fd_spad_join( fd_spad_new( fd_wksp_laddr_fast( wksp, glo ), fd_spad_mem_max_max( ghi-glo ) ) );
     395           0 :   fd_spad_push( spad );
     396             : 
     397           0 :   for( fd_http_static_file_t * f=STATIC_FILES; f->name; f++ ) {
     398           0 :     char const * ext = strrchr( f->name, '.' );
     399           0 :     if( !ext ) continue;
     400           0 :     if( !strcmp( ext, ".html" ) ||
     401           0 :         !strcmp( ext, ".css"  ) ||
     402           0 :         !strcmp( ext, ".js"   ) ||
     403           0 :         !strcmp( ext, ".svg"  ) ) {}
     404           0 :     else continue;
     405             : 
     406           0 :     ulong   zstd_bufsz = fd_spad_alloc_max( spad, 1UL );
     407           0 :     uchar * zstd_buf   = fd_spad_prepare( spad, 1UL, zstd_bufsz );
     408           0 :     ulong zstd_sz = ZSTD_compressCCtx( cctx, zstd_buf, zstd_bufsz, f->data, *f->data_len, DIST_COMPRESSION_LEVEL );
     409           0 :     if( ZSTD_isError( zstd_sz ) ) {
     410           0 :       fd_spad_cancel( spad );
     411           0 :       FD_LOG_WARNING(( "ZSTD_compressCCtx(%s) failed (%s)", f->name, ZSTD_getErrorName( zstd_sz ) ));
     412           0 :       break;
     413           0 :     }
     414           0 :     f->zstd_data     = zstd_buf;
     415           0 :     f->zstd_data_len = zstd_sz;
     416           0 :     fd_spad_publish( spad, zstd_sz );
     417           0 :   }
     418             : 
     419           0 :   ulong uncompressed_sz = 0UL;
     420           0 :   ulong compressed_sz   = 0UL;
     421           0 :   for( fd_http_static_file_t * f=STATIC_FILES; f->name; f++ ) {
     422           0 :     uncompressed_sz += *f->data_len;
     423           0 :     compressed_sz   += fd_ulong_if( !!f->zstd_data_len, f->zstd_data_len, *f->data_len );
     424           0 :   }
     425             : 
     426           0 :   fd_wksp_free_laddr( cctx_mem );
     427           0 :   FD_LOG_INFO(( "Compressed assets (%lu bytes => %lu bytes)", uncompressed_sz, compressed_sz ));
     428           0 : }
     429             : 
     430             : #endif /* FD_HAS_ZSTD */
     431             : 
     432             : static FD_TL fd_alloc_t * cjson_alloc_ctx;
     433             : 
     434             : static void *
     435           0 : cjson_alloc( ulong sz ) {
     436           0 :   if( FD_LIKELY( cjson_alloc_ctx ) ) {
     437           0 :     return fd_alloc_malloc( cjson_alloc_ctx, 8UL, sz );
     438           0 :   } else {
     439           0 :     return malloc( sz );
     440           0 :   }
     441           0 : }
     442             : 
     443             : static void
     444           0 : cjson_free( void * ptr ) {
     445           0 :   if( FD_LIKELY( cjson_alloc_ctx ) ) {
     446           0 :     fd_alloc_free( cjson_alloc_ctx, ptr );
     447           0 :   } else {
     448           0 :     free( ptr );
     449           0 :   }
     450           0 : }
     451             : 
     452             : extern char const fdctl_version_string[];
     453             : 
     454             : static void
     455             : unprivileged_init( fd_topo_t *      topo,
     456           0 :                    fd_topo_tile_t * tile ) {
     457           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     458             : 
     459           0 : # if FD_HAS_ZSTD
     460           0 :   pre_compress_files( topo->workspaces[ topo->objs[ tile->tile_obj_id ].wksp_id ].wksp );
     461           0 : # endif
     462             : 
     463           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     464           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     465           0 :                        FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( derive_http_params( tile ) ) );
     466           0 :   void * _gui        = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_align(),         fd_gui_footprint() );
     467           0 :   void * _alloc      = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(),       fd_alloc_footprint() );
     468             : 
     469           0 :   FD_TEST( fd_cstr_printf_check( ctx->version_string, sizeof( ctx->version_string ), NULL, "%s", fdctl_version_string ) );
     470             : 
     471           0 :   ctx->topo = topo;
     472           0 :   ctx->gui  = fd_gui_join( fd_gui_new( _gui, ctx->gui_server, ctx->version_string, tile->gui.cluster, ctx->identity_key, tile->gui.is_voting, ctx->topo ) );
     473           0 :   FD_TEST( ctx->gui );
     474             : 
     475           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
     476           0 :   FD_TEST( ctx->keyswitch );
     477             : 
     478           0 :   cjson_alloc_ctx = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
     479           0 :   FD_TEST( cjson_alloc_ctx );
     480           0 :   cJSON_Hooks hooks = {
     481           0 :     .malloc_fn = cjson_alloc,
     482           0 :     .free_fn   = cjson_free,
     483           0 :   };
     484           0 :   cJSON_InitHooks( &hooks );
     485             : 
     486           0 :   ctx->next_poll_deadline = fd_tickcount();
     487             : 
     488           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     489           0 :     fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
     490           0 :     fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     491             : 
     492           0 :     if( FD_LIKELY( !strcmp( link->name, "plugin_out"     ) ) ) ctx->in_kind[ i ] = IN_KIND_PLUGIN;
     493           0 :     else if( FD_LIKELY( !strcmp( link->name, "poh_pack"  ) ) ) ctx->in_kind[ i ] = IN_KIND_POH_PACK;
     494           0 :     else if( FD_LIKELY( !strcmp( link->name, "pack_bank" ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_BANK;
     495           0 :     else if( FD_LIKELY( !strcmp( link->name, "bank_poh"  ) ) ) ctx->in_kind[ i ] = IN_KIND_BANK_POH;
     496           0 :     else FD_LOG_ERR(( "gui tile has unexpected input link %lu %s", i, link->name ));
     497             : 
     498           0 :     if( FD_LIKELY( !strcmp( link->name, "bank_poh" ) ) ) {
     499           0 :       ulong producer = fd_topo_find_link_producer( topo, &topo->links[ tile->in_link_id[ i ] ] );
     500           0 :       ctx->in_bank_idx[ i ] = topo->tiles[ producer ].kind_id;
     501           0 :     }
     502             : 
     503           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
     504           0 :     ctx->in[ i ].mtu    = link->mtu;
     505           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
     506           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
     507           0 :   }
     508             : 
     509           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     510           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     511           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     512             : 
     513           0 :   FD_LOG_WARNING(( "GUI server listening at http://" FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS( tile->gui.listen_addr ), tile->gui.listen_port ));
     514           0 : }
     515             : 
     516             : static ulong
     517             : populate_allowed_seccomp( fd_topo_t const *      topo,
     518             :                           fd_topo_tile_t const * tile,
     519             :                           ulong                  out_cnt,
     520           0 :                           struct sock_filter *   out ) {
     521           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     522           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     523           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     524             : 
     525           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 ) );
     526           0 :   return sock_filter_policy_fd_gui_tile_instr_cnt;
     527           0 : }
     528             : 
     529             : static ulong
     530             : populate_allowed_fds( fd_topo_t const *      topo,
     531             :                       fd_topo_tile_t const * tile,
     532             :                       ulong                  out_fds_cnt,
     533           0 :                       int *                  out_fds ) {
     534           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     535           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     536           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     537             : 
     538           0 :   if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     539             : 
     540           0 :   ulong out_cnt = 0UL;
     541           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     542           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     543           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     544           0 :   out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->gui_server ); /* gui listen socket */
     545           0 :   return out_cnt;
     546           0 : }
     547             : 
     548             : static ulong
     549             : rlimit_file_cnt( fd_topo_t const *      topo FD_PARAM_UNUSED,
     550           0 :                  fd_topo_tile_t const * tile ) {
     551             :   /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
     552           0 :   ulong base = 5UL;
     553           0 :   return base + tile->gui.max_http_connections + tile->gui.max_websocket_connections;
     554           0 : }
     555             : 
     556           0 : #define STEM_BURST (1UL)
     557             : 
     558             : /* See explanation in fd_pack */
     559           0 : #define STEM_LAZY  (128L*3000L)
     560             : 
     561           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_gui_ctx_t
     562           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gui_ctx_t)
     563             : 
     564           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     565           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     566           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     567           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     568             : 
     569             : #include "../../disco/stem/fd_stem.c"
     570             : 
     571             : fd_topo_run_tile_t fd_tile_gui = {
     572             :   .name                     = "gui",
     573             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
     574             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     575             :   .populate_allowed_fds     = populate_allowed_fds,
     576             :   .scratch_align            = scratch_align,
     577             :   .scratch_footprint        = scratch_footprint,
     578             :   .loose_footprint          = loose_footprint,
     579             :   .privileged_init          = privileged_init,
     580             :   .unprivileged_init        = unprivileged_init,
     581             :   .run                      = stem_run,
     582             : };

Generated by: LCOV version 1.14