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