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 : #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 "../../waltz/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 0 : #define FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN 8192
51 0 : #define FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN 8192
52 0 : #define FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT 8192
53 :
54 : static fd_http_server_params_t
55 0 : derive_http_params( fd_topo_tile_t const * tile ) {
56 0 : return (fd_http_server_params_t) {
57 0 : .max_connection_cnt = tile->gui.max_http_connections,
58 0 : .max_ws_connection_cnt = tile->gui.max_websocket_connections,
59 0 : .max_request_len = FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN,
60 0 : .max_ws_recv_frame_len = FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN,
61 0 : .max_ws_send_frame_cnt = FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT,
62 0 : .outgoing_buffer_sz = tile->gui.send_buffer_size_mb * (1UL<<20UL),
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 : 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 0 : scratch_align( void ) {
101 0 : return 128UL;
102 0 : }
103 :
104 : static inline ulong
105 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
106 0 : ulong http_fp = fd_http_server_footprint( derive_http_params( tile ) );
107 0 : if( FD_UNLIKELY( !http_fp ) ) FD_LOG_ERR(( "Invalid [tiles.gui] config parameters" ));
108 :
109 0 : ulong l = FD_LAYOUT_INIT;
110 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
111 0 : l = FD_LAYOUT_APPEND( l, fd_http_server_align(), http_fp );
112 0 : l = FD_LAYOUT_APPEND( l, fd_gui_align(), fd_gui_footprint() );
113 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
114 0 : return FD_LAYOUT_FINI( l, scratch_align() );
115 0 : }
116 :
117 : /* dist_file_sz returns the sum of static asset file sizes */
118 :
119 : FD_FN_CONST static ulong
120 0 : dist_file_sz( void ) {
121 0 : ulong tot_sz = 0UL;
122 0 : for( fd_http_static_file_t * f = STATIC_FILES; f->name; f++ ) {
123 0 : tot_sz += *(f->data_len);
124 0 : }
125 0 : return tot_sz;
126 0 : }
127 :
128 : FD_FN_PURE static inline ulong
129 0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
130 : /* Reserve total size of files for compression buffers */
131 0 : return fd_spad_footprint( dist_file_sz() ) +
132 0 : # if FD_HAS_ZSTD
133 0 : fd_ulong_align_up( ZSTD_estimateCCtxSize( DIST_COMPRESSION_LEVEL ), FD_WKSP_ALIGN_DEFAULT ) +
134 0 : # endif
135 0 : 256UL * (1UL<<20UL); /* 256MiB of heap space for the cJSON allocator */
136 0 : }
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)seq;
214 0 : (void)tsorig;
215 0 : (void)stem;
216 :
217 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PLUGIN ) ) fd_gui_plugin_message( ctx->gui, sig, ctx->buf );
218 0 : else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_POH_PACK ) ) {
219 0 : FD_TEST( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_BECAME_LEADER );
220 0 : fd_became_leader_t * became_leader = (fd_became_leader_t *)ctx->buf;
221 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 );
222 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PACK_BANK ) ) {
223 0 : if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK ) ) {
224 0 : FD_TEST( sz<ULONG_MAX );
225 0 : fd_microblock_bank_trailer_t * trailer = (fd_microblock_bank_trailer_t *)( ctx->buf+sz-sizeof(fd_microblock_bank_trailer_t) );
226 0 : fd_gui_microblock_execution_begin( ctx->gui,
227 0 : fd_frag_meta_ts_decomp( tspub, fd_tickcount() ),
228 0 : fd_disco_poh_sig_slot( sig ),
229 0 : (fd_txn_p_t *)ctx->buf,
230 0 : (sz-sizeof( fd_microblock_bank_trailer_t ))/sizeof( fd_txn_p_t ),
231 0 : (uint)trailer->microblock_idx,
232 0 : trailer->pack_txn_idx );
233 0 : } else if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_DONE_PACKING ) ) {
234 0 : fd_gui_unbecame_leader( ctx->gui, fd_frag_meta_ts_decomp( tspub, fd_tickcount() ), fd_disco_poh_sig_slot( sig ), ((fd_done_packing_t *)ctx->buf)->microblocks_in_slot );
235 0 : } else {
236 0 : FD_LOG_ERR(( "unexpected poh packet type %lu", fd_disco_poh_sig_pkt_type( sig ) ));
237 0 : }
238 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_BANK_POH ) ) {
239 0 : fd_microblock_trailer_t * trailer = (fd_microblock_trailer_t *)( ctx->buf+sz-sizeof( fd_microblock_trailer_t ) );
240 0 : fd_gui_microblock_execution_end( ctx->gui,
241 0 : fd_frag_meta_ts_decomp( tspub, fd_tickcount() ),
242 0 : ctx->in_bank_idx[ in_idx ],
243 0 : fd_disco_bank_sig_slot( sig ),
244 0 : (sz-sizeof( fd_microblock_trailer_t ))/sizeof( fd_txn_p_t ),
245 0 : (fd_txn_p_t *)ctx->buf,
246 0 : trailer->pack_txn_idx,
247 0 : trailer->txn_start_pct,
248 0 : trailer->txn_load_end_pct,
249 0 : trailer->txn_end_pct,
250 0 : trailer->txn_preload_end_pct,
251 0 : trailer->tips );
252 0 : } else {
253 0 : FD_LOG_ERR(( "unexpected in_kind %lu", ctx->in_kind[ in_idx ] ));
254 0 : }
255 0 : }
256 :
257 : static fd_http_server_response_t
258 0 : gui_http_request( fd_http_server_request_t const * request ) {
259 0 : if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
260 0 : return (fd_http_server_response_t){
261 0 : .status = 405,
262 0 : };
263 0 : }
264 :
265 0 : if( FD_LIKELY( !strcmp( request->path, "/websocket" ) ) ) {
266 0 : return (fd_http_server_response_t){
267 0 : .status = 200,
268 0 : .upgrade_websocket = 1,
269 0 : };
270 0 : } else if( FD_LIKELY( !strcmp( request->path, "/favicon.svg" ) ) ) {
271 0 : return (fd_http_server_response_t){
272 0 : .status = 200,
273 0 : .static_body = firedancer_svg,
274 0 : .static_body_len = firedancer_svg_sz,
275 0 : .content_type = "image/svg+xml",
276 0 : .upgrade_websocket = 0,
277 0 : };
278 0 : }
279 :
280 0 : int is_vite_page = !strcmp( request->path, "/" ) ||
281 0 : !strcmp( request->path, "/leaderSchedule" ) ||
282 0 : !strcmp( request->path, "/gossip") ||
283 0 : !strncmp( request->path, "/?", strlen("/?") ) ||
284 0 : !strncmp( request->path, "/leaderSchedule?", strlen("/leaderSchedule?") ) ||
285 0 : !strncmp( request->path, "/gossip?", strlen("/gossip?") );
286 :
287 0 : for( fd_http_static_file_t * f = STATIC_FILES; f->name; f++ ) {
288 0 : if( !strcmp( request->path, f->name ) ||
289 0 : (!strcmp( f->name, "/index.html" ) && is_vite_page) ) {
290 0 : char const * content_type = NULL;
291 :
292 0 : char const * ext = strrchr( f->name, '.' );
293 0 : if( FD_LIKELY( ext ) ) {
294 0 : if( !strcmp( ext, ".html" ) ) content_type = "text/html; charset=utf-8";
295 0 : else if( !strcmp( ext, ".css" ) ) content_type = "text/css";
296 0 : else if( !strcmp( ext, ".js" ) ) content_type = "application/javascript";
297 0 : else if( !strcmp( ext, ".svg" ) ) content_type = "image/svg+xml";
298 0 : else if( !strcmp( ext, ".woff" ) ) content_type = "font/woff";
299 0 : else if( !strcmp( ext, ".woff2" ) ) content_type = "font/woff2";
300 0 : }
301 :
302 0 : char const * cache_control = NULL;
303 0 : if( FD_LIKELY( !strncmp( request->path, "/assets", 7 ) ) ) cache_control = "public, max-age=31536000, immutable";
304 0 : else if( FD_LIKELY( !strcmp( f->name, "/index.html" ) ) ) cache_control = "no-cache";
305 :
306 0 : const uchar * data = f->data;
307 0 : ulong data_len = *(f->data_len);
308 :
309 0 : int accepts_zstd = 0;
310 0 : if( FD_LIKELY( request->headers.accept_encoding ) ) {
311 0 : accepts_zstd = !!strstr( request->headers.accept_encoding, "zstd" );
312 0 : }
313 :
314 0 : char const * content_encoding = NULL;
315 0 : if( FD_LIKELY( accepts_zstd && f->zstd_data ) ) {
316 0 : content_encoding = "zstd";
317 0 : data = f->zstd_data;
318 0 : data_len = f->zstd_data_len;
319 0 : }
320 :
321 0 : return (fd_http_server_response_t){
322 0 : .status = 200,
323 0 : .static_body = data,
324 0 : .static_body_len = data_len,
325 0 : .content_type = content_type,
326 0 : .cache_control = cache_control,
327 0 : .content_encoding = content_encoding,
328 0 : .upgrade_websocket = 0,
329 0 : };
330 0 : }
331 0 : }
332 :
333 0 : return (fd_http_server_response_t){
334 0 : .status = 404,
335 0 : };
336 0 : }
337 :
338 : static void
339 : gui_ws_open( ulong conn_id,
340 0 : void * _ctx ) {
341 0 : fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
342 :
343 0 : fd_gui_ws_open( ctx->gui, conn_id );
344 0 : }
345 :
346 : static void
347 : gui_ws_message( ulong ws_conn_id,
348 : uchar const * data,
349 : ulong data_len,
350 0 : void * _ctx ) {
351 0 : fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
352 :
353 0 : int close = fd_gui_ws_message( ctx->gui, ws_conn_id, data, data_len );
354 0 : if( FD_UNLIKELY( close<0 ) ) fd_http_server_ws_close( ctx->gui_server, ws_conn_id, close );
355 0 : }
356 :
357 : static void
358 : privileged_init( fd_topo_t * topo,
359 0 : fd_topo_tile_t * tile ) {
360 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
361 :
362 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
363 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
364 :
365 0 : fd_http_server_params_t http_param = derive_http_params( tile );
366 0 : fd_http_server_t * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
367 :
368 0 : fd_http_server_callbacks_t gui_callbacks = {
369 0 : .request = gui_http_request,
370 0 : .ws_open = gui_ws_open,
371 0 : .ws_message = gui_ws_message,
372 0 : };
373 0 : ctx->gui_server = fd_http_server_join( fd_http_server_new( _gui, http_param, gui_callbacks, ctx ) );
374 0 : fd_http_server_listen( ctx->gui_server, tile->gui.listen_addr, tile->gui.listen_port );
375 :
376 0 : if( FD_UNLIKELY( !strcmp( tile->gui.identity_key_path, "" ) ) )
377 0 : FD_LOG_ERR(( "identity_key_path not set" ));
378 :
379 0 : ctx->identity_key = fd_keyload_load( tile->gui.identity_key_path, /* pubkey only: */ 1 );
380 0 : }
381 :
382 : #if FD_HAS_ZSTD
383 :
384 : /* pre_compress_files compresses static assets into wksp-provided
385 : buffers using Zstandard. Logs warning and continues if insufficient
386 : wksp space is available. */
387 :
388 : static void
389 0 : pre_compress_files( fd_wksp_t * wksp ) {
390 :
391 : /* Allocate ZSTD compression context. Freed when exiting this
392 : function's scope. */
393 0 : ulong cctx_sz = ZSTD_estimateCCtxSize( DIST_COMPRESSION_LEVEL );
394 0 : void * cctx_mem = fd_wksp_alloc_laddr( wksp, 16UL, cctx_sz, 1UL );
395 0 : if( FD_UNLIKELY( !cctx_mem ) ) {
396 0 : FD_LOG_WARNING(( "Failed to allocate compressor" ));
397 0 : return;
398 0 : }
399 0 : ZSTD_CCtx * cctx = ZSTD_initStaticCCtx( cctx_mem, cctx_sz );
400 0 : if( FD_UNLIKELY( !cctx ) ) {
401 0 : FD_LOG_WARNING(( "Failed to create ZSTD compression context" ));
402 0 : fd_wksp_free_laddr( cctx_mem );
403 0 : return;
404 0 : }
405 :
406 : /* Allocate permanent space for the compressed files. */
407 0 : ulong glo, ghi;
408 0 : if( FD_UNLIKELY( !fd_wksp_alloc_at_least( wksp, FD_SPAD_ALIGN, dist_file_sz(), 1UL, &glo, &ghi ) ) ) {
409 0 : FD_LOG_WARNING(( "Failed to allocate space for compressing assets" ));
410 0 : fd_wksp_free_laddr( cctx_mem );
411 0 : return;
412 0 : }
413 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( fd_wksp_laddr_fast( wksp, glo ), fd_spad_mem_max_max( ghi-glo ) ) );
414 0 : fd_spad_push( spad );
415 :
416 0 : for( fd_http_static_file_t * f=STATIC_FILES; f->name; f++ ) {
417 0 : char const * ext = strrchr( f->name, '.' );
418 0 : if( !ext ) continue;
419 0 : if( !strcmp( ext, ".html" ) ||
420 0 : !strcmp( ext, ".css" ) ||
421 0 : !strcmp( ext, ".js" ) ||
422 0 : !strcmp( ext, ".svg" ) ) {}
423 0 : else continue;
424 :
425 0 : ulong zstd_bufsz = fd_spad_alloc_max( spad, 1UL );
426 0 : uchar * zstd_buf = fd_spad_prepare( spad, 1UL, zstd_bufsz );
427 0 : ulong zstd_sz = ZSTD_compressCCtx( cctx, zstd_buf, zstd_bufsz, f->data, *f->data_len, DIST_COMPRESSION_LEVEL );
428 0 : if( ZSTD_isError( zstd_sz ) ) {
429 0 : fd_spad_cancel( spad );
430 0 : FD_LOG_WARNING(( "ZSTD_compressCCtx(%s) failed (%s)", f->name, ZSTD_getErrorName( zstd_sz ) ));
431 0 : break;
432 0 : }
433 0 : f->zstd_data = zstd_buf;
434 0 : f->zstd_data_len = zstd_sz;
435 0 : fd_spad_publish( spad, zstd_sz );
436 0 : }
437 :
438 0 : ulong uncompressed_sz = 0UL;
439 0 : ulong compressed_sz = 0UL;
440 0 : for( fd_http_static_file_t * f=STATIC_FILES; f->name; f++ ) {
441 0 : uncompressed_sz += *f->data_len;
442 0 : compressed_sz += fd_ulong_if( !!f->zstd_data_len, f->zstd_data_len, *f->data_len );
443 0 : }
444 :
445 0 : fd_wksp_free_laddr( cctx_mem );
446 0 : FD_LOG_INFO(( "Compressed assets (%lu bytes => %lu bytes)", uncompressed_sz, compressed_sz ));
447 0 : }
448 :
449 : #endif /* FD_HAS_ZSTD */
450 :
451 : static FD_TL fd_alloc_t * cjson_alloc_ctx;
452 :
453 : static void *
454 0 : cjson_alloc( ulong sz ) {
455 0 : if( FD_LIKELY( cjson_alloc_ctx ) ) {
456 0 : return fd_alloc_malloc( cjson_alloc_ctx, 8UL, sz );
457 0 : } else {
458 0 : return malloc( sz );
459 0 : }
460 0 : }
461 :
462 : static void
463 0 : cjson_free( void * ptr ) {
464 0 : if( FD_LIKELY( cjson_alloc_ctx ) ) {
465 0 : fd_alloc_free( cjson_alloc_ctx, ptr );
466 0 : } else {
467 0 : free( ptr );
468 0 : }
469 0 : }
470 :
471 : extern char const fdctl_version_string[];
472 :
473 : static void
474 : unprivileged_init( fd_topo_t * topo,
475 0 : fd_topo_tile_t * tile ) {
476 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
477 :
478 0 : # if FD_HAS_ZSTD
479 0 : pre_compress_files( topo->workspaces[ topo->objs[ tile->tile_obj_id ].wksp_id ].wksp );
480 0 : # endif
481 :
482 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
483 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
484 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( derive_http_params( tile ) ) );
485 0 : void * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_align(), fd_gui_footprint() );
486 0 : void * _alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
487 :
488 0 : FD_TEST( fd_cstr_printf_check( ctx->version_string, sizeof( ctx->version_string ), NULL, "%s", fdctl_version_string ) );
489 :
490 0 : ctx->topo = topo;
491 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, tile->gui.schedule_strategy, ctx->topo ) );
492 0 : FD_TEST( ctx->gui );
493 :
494 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
495 0 : FD_TEST( ctx->keyswitch );
496 :
497 0 : cjson_alloc_ctx = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
498 0 : FD_TEST( cjson_alloc_ctx );
499 0 : cJSON_Hooks hooks = {
500 0 : .malloc_fn = cjson_alloc,
501 0 : .free_fn = cjson_free,
502 0 : };
503 0 : cJSON_InitHooks( &hooks );
504 :
505 0 : ctx->next_poll_deadline = fd_tickcount();
506 :
507 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
508 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
509 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
510 :
511 0 : if( FD_LIKELY( !strcmp( link->name, "plugin_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_PLUGIN;
512 0 : else if( FD_LIKELY( !strcmp( link->name, "poh_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH_PACK;
513 0 : else if( FD_LIKELY( !strcmp( link->name, "pack_bank" ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_BANK;
514 0 : else if( FD_LIKELY( !strcmp( link->name, "bank_poh" ) ) ) ctx->in_kind[ i ] = IN_KIND_BANK_POH;
515 0 : else FD_LOG_ERR(( "gui tile has unexpected input link %lu %s", i, link->name ));
516 :
517 0 : if( FD_LIKELY( !strcmp( link->name, "bank_poh" ) ) ) {
518 0 : ulong producer = fd_topo_find_link_producer( topo, &topo->links[ tile->in_link_id[ i ] ] );
519 0 : ctx->in_bank_idx[ i ] = topo->tiles[ producer ].kind_id;
520 0 : }
521 :
522 0 : ctx->in[ i ].mem = link_wksp->wksp;
523 0 : ctx->in[ i ].mtu = link->mtu;
524 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
525 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
526 0 : }
527 :
528 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
529 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
530 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
531 :
532 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 ));
533 0 : }
534 :
535 : static ulong
536 : populate_allowed_seccomp( fd_topo_t const * topo,
537 : fd_topo_tile_t const * tile,
538 : ulong out_cnt,
539 0 : struct sock_filter * out ) {
540 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
541 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
542 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
543 :
544 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 ) );
545 0 : return sock_filter_policy_fd_gui_tile_instr_cnt;
546 0 : }
547 :
548 : static ulong
549 : populate_allowed_fds( fd_topo_t const * topo,
550 : fd_topo_tile_t const * tile,
551 : ulong out_fds_cnt,
552 0 : int * out_fds ) {
553 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
554 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
555 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
556 :
557 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
558 :
559 0 : ulong out_cnt = 0UL;
560 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
561 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
562 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
563 0 : out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->gui_server ); /* gui listen socket */
564 0 : return out_cnt;
565 0 : }
566 :
567 : static ulong
568 : rlimit_file_cnt( fd_topo_t const * topo FD_PARAM_UNUSED,
569 0 : fd_topo_tile_t const * tile ) {
570 : /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
571 0 : ulong base = 5UL;
572 0 : return base + tile->gui.max_http_connections + tile->gui.max_websocket_connections;
573 0 : }
574 :
575 0 : #define STEM_BURST (1UL)
576 :
577 : /* See explanation in fd_pack */
578 0 : #define STEM_LAZY (128L*3000L)
579 :
580 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_gui_ctx_t
581 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gui_ctx_t)
582 :
583 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
584 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
585 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
586 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
587 :
588 : #include "../../disco/stem/fd_stem.c"
589 :
590 : fd_topo_run_tile_t fd_tile_gui = {
591 : .name = "gui",
592 : .rlimit_file_cnt_fn = rlimit_file_cnt,
593 : .populate_allowed_seccomp = populate_allowed_seccomp,
594 : .populate_allowed_fds = populate_allowed_fds,
595 : .scratch_align = scratch_align,
596 : .scratch_footprint = scratch_footprint,
597 : .loose_footprint = loose_footprint,
598 : .privileged_init = privileged_init,
599 : .unprivileged_init = unprivileged_init,
600 : .run = stem_run,
601 : };
|