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 "../../disco/gui/generated/http_import_dist.h"
10 :
11 : /* The list of files used to serve the frontend is set here. This is a
12 : global variable since is is populated at boot based on the
13 : `development.gui.frontend_release_channel` option and is accessed in
14 : the gui_http_request callback. */
15 : static fd_http_static_file_t * STATIC_FILES;
16 :
17 : #include <sys/socket.h> /* SOCK_CLOEXEC, SOCK_NONBLOCK needed for seccomp filter */
18 : #include <stdlib.h>
19 :
20 : #include "generated/fd_gui_tile_seccomp.h"
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 : #include "../../util/clock/fd_clock.h"
30 : #include "../../discof/repair/fd_repair.h"
31 : #include "../../flamenco/gossip/fd_gossip_private.h"
32 :
33 : #include <sys/types.h>
34 : #include <unistd.h>
35 : #include <string.h>
36 : #include <poll.h>
37 : #include <stdio.h>
38 :
39 : #if FD_HAS_ZSTD
40 : #define ZSTD_STATIC_LINKING_ONLY
41 : #include <zstd.h>
42 : #endif
43 :
44 0 : #define IN_KIND_PLUGIN ( 0UL)
45 0 : #define IN_KIND_POH_PACK ( 1UL)
46 0 : #define IN_KIND_PACK_BANK ( 2UL)
47 0 : #define IN_KIND_PACK_POH ( 3UL)
48 0 : #define IN_KIND_BANK_POH ( 4UL)
49 0 : #define IN_KIND_SHRED_OUT ( 5UL) /* firedancer only */
50 0 : #define IN_KIND_NET_GOSSVF ( 6UL) /* firedancer only */
51 0 : #define IN_KIND_GOSSIP_NET ( 7UL) /* firedancer only */
52 0 : #define IN_KIND_GOSSIP_OUT ( 8UL) /* firedancer only */
53 0 : #define IN_KIND_SNAPRD ( 9UL) /* firedancer only */
54 0 : #define IN_KIND_REPAIR_NET (10UL) /* firedancer only */
55 0 : #define IN_KIND_TOWER_OUT (11UL) /* firedancer only */
56 0 : #define IN_KIND_REPLAY_OUT (12UL) /* firedancer only */
57 :
58 : FD_IMPORT_BINARY( firedancer_svg, "book/public/fire.svg" );
59 :
60 0 : #define FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN 8192
61 0 : #define FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN 8192
62 0 : #define FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT 8192
63 :
64 : static fd_http_server_params_t
65 0 : derive_http_params( fd_topo_tile_t const * tile ) {
66 0 : return (fd_http_server_params_t) {
67 0 : .max_connection_cnt = tile->gui.max_http_connections,
68 0 : .max_ws_connection_cnt = tile->gui.max_websocket_connections,
69 0 : .max_request_len = FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN,
70 0 : .max_ws_recv_frame_len = FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN,
71 0 : .max_ws_send_frame_cnt = FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT,
72 0 : .outgoing_buffer_sz = tile->gui.send_buffer_size_mb * (1UL<<20UL),
73 0 : .compress_websocket = tile->gui.websocket_compression,
74 0 : };
75 0 : }
76 :
77 : struct fd_gui_in_ctx {
78 : fd_wksp_t * mem;
79 : ulong mtu;
80 : ulong chunk0;
81 : ulong wmark;
82 : };
83 :
84 : typedef struct fd_gui_in_ctx fd_gui_in_ctx_t;
85 :
86 : typedef struct {
87 : fd_topo_t * topo;
88 :
89 : int is_full_client;
90 : int snapshots_enabled;
91 :
92 : fd_gui_t * gui;
93 : fd_gui_peers_ctx_t * peers;
94 :
95 : /* Most of the gui tile uses fd_clock for timing, but some stem
96 : timestamps still used tickcounts, so we keep separate timestamps
97 : here to handle those cases until fd_clock is more widely adopted. */
98 : long ref_wallclock;
99 : long ref_tickcount;
100 :
101 : fd_clock_t clock[1];
102 : long recal_next; /* next recalibration time (ns) */
103 :
104 : uchar __attribute__((aligned(FD_CLOCK_ALIGN))) clock_mem[ FD_CLOCK_FOOTPRINT ];
105 :
106 : /* This needs to be max(plugin_msg) across all kinds of messages.
107 : Currently this is just figured out manually, it's a gossip update
108 : message assuming the table is completely full (40200) of peers.
109 :
110 : We also require an alignment of 64 to hold fd_txn_p_t structs. */
111 : uchar buf[ 8UL+FD_GUI_MAX_PEER_CNT*(58UL+12UL*34UL) ] __attribute__((aligned(64)));
112 :
113 : fd_http_server_t * gui_server;
114 :
115 : long next_poll_deadline;
116 :
117 : char version_string[ 16UL ];
118 :
119 : fd_keyswitch_t * keyswitch;
120 : uchar const * identity_key;
121 :
122 : int has_vote_key;
123 : fd_pubkey_t const vote_key[ 1UL ];
124 :
125 : ulong in_kind[ 64UL ];
126 : ulong in_bank_idx[ 64UL ];
127 : fd_gui_in_ctx_t in[ 64UL ];
128 : } fd_gui_ctx_t;
129 :
130 : FD_FN_CONST static inline ulong
131 0 : scratch_align( void ) {
132 0 : return 128UL;
133 0 : }
134 :
135 : static inline ulong
136 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
137 0 : fd_http_server_params_t http_param = derive_http_params( tile );
138 0 : ulong http_fp = fd_http_server_footprint( http_param );
139 0 : if( FD_UNLIKELY( !http_fp ) ) FD_LOG_ERR(( "Invalid [tiles.gui] config parameters" ));
140 :
141 0 : ulong l = FD_LAYOUT_INIT;
142 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
143 0 : l = FD_LAYOUT_APPEND( l, fd_http_server_align(), http_fp );
144 0 : l = FD_LAYOUT_APPEND( l, fd_gui_peers_align(), fd_gui_peers_footprint( http_param.max_ws_connection_cnt ) );
145 0 : l = FD_LAYOUT_APPEND( l, fd_gui_align(), fd_gui_footprint() );
146 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
147 0 : return FD_LAYOUT_FINI( l, scratch_align() );
148 0 : }
149 :
150 : FD_FN_PURE static inline ulong
151 0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
152 0 : return 256UL * (1UL<<20UL); /* 256MiB of heap space for the cJSON allocator */
153 0 : }
154 :
155 : static inline void
156 0 : during_housekeeping( fd_gui_ctx_t * ctx ) {
157 0 : ctx->ref_wallclock = fd_log_wallclock();
158 0 : ctx->ref_tickcount = fd_tickcount();
159 :
160 0 : if( FD_UNLIKELY( fd_clock_now( ctx->clock ) >= ctx->recal_next ) ) {
161 0 : ctx->recal_next = fd_clock_default_recal( ctx->clock );
162 0 : }
163 :
164 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
165 0 : fd_gui_set_identity( ctx->gui, ctx->keyswitch->bytes );
166 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
167 0 : }
168 0 : }
169 :
170 : static void
171 : before_credit( fd_gui_ctx_t * ctx,
172 : fd_stem_context_t * stem,
173 0 : int * charge_busy ) {
174 0 : (void)stem;
175 :
176 0 : int charge_busy_server = 0;
177 0 : long now = fd_tickcount();
178 0 : if( FD_UNLIKELY( now>=ctx->next_poll_deadline ) ) {
179 0 : charge_busy_server = fd_http_server_poll( ctx->gui_server, 0 );
180 0 : ctx->next_poll_deadline = fd_tickcount() + (long)(fd_tempo_tick_per_ns( NULL ) * 128L * 1000L);
181 0 : }
182 :
183 0 : int charge_poll = 0;
184 0 : charge_poll |= fd_gui_poll( ctx->gui, fd_clock_now( ctx->clock ) );
185 0 : if( FD_UNLIKELY( ctx->is_full_client ) ) charge_poll |= fd_gui_peers_poll( ctx->peers, fd_clock_now( ctx->clock ) );
186 :
187 0 : *charge_busy = charge_busy_server | charge_poll;
188 0 : }
189 :
190 : static int
191 : before_frag( fd_gui_ctx_t * ctx,
192 : ulong in_idx,
193 : ulong seq,
194 0 : ulong sig ) {
195 0 : (void)seq;
196 :
197 : /* Ignore "done draining banks" signal from pack->poh */
198 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PACK_POH && sig==ULONG_MAX ) ) return 1;
199 0 : return 0;
200 0 : }
201 :
202 : static inline void
203 : during_frag( fd_gui_ctx_t * ctx,
204 : ulong in_idx,
205 : ulong seq FD_PARAM_UNUSED,
206 : ulong sig,
207 : ulong chunk,
208 : ulong sz,
209 0 : ulong gui FD_PARAM_UNUSED ) {
210 :
211 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
212 :
213 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PLUGIN ) ) {
214 : /* ... todo... sigh, sz is not correct since it's too big */
215 0 : if( FD_LIKELY( sig==FD_PLUGIN_MSG_GOSSIP_UPDATE ) ) {
216 0 : ulong peer_cnt = ((ulong *)src)[ 0 ];
217 0 : FD_TEST( peer_cnt<=FD_GUI_MAX_PEER_CNT );
218 0 : sz = 8UL + peer_cnt*FD_GOSSIP_LINK_MSG_SIZE;
219 0 : } else if( FD_LIKELY( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE ) ) {
220 0 : ulong peer_cnt = ((ulong *)src)[ 0 ];
221 0 : FD_TEST( peer_cnt<=FD_GUI_MAX_PEER_CNT );
222 0 : sz = 8UL + peer_cnt*112UL;
223 0 : } else if( FD_UNLIKELY( sig==FD_PLUGIN_MSG_LEADER_SCHEDULE ) ) {
224 0 : ulong staked_cnt = ((ulong *)src)[ 1 ];
225 0 : FD_TEST( staked_cnt<=MAX_STAKED_LEADERS );
226 0 : sz = fd_stake_weight_msg_sz( staked_cnt );
227 0 : }
228 0 : }
229 :
230 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_SHRED_OUT ) ) {
231 : /* There are multiple frags types sent on this link, the currently the
232 : only way to distinguish them is to check sz. We dont actually read
233 : from the dcache. */
234 0 : return;
235 0 : }
236 :
237 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
238 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu] or too large (%lu)", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark, ctx->in[ in_idx ].mtu ));
239 :
240 0 : fd_memcpy( ctx->buf, src, sz );
241 0 : }
242 :
243 : static inline void
244 : after_frag( fd_gui_ctx_t * ctx,
245 : ulong in_idx,
246 : ulong seq,
247 : ulong sig,
248 : ulong sz,
249 : ulong tsorig,
250 : ulong tspub,
251 0 : fd_stem_context_t * stem ) {
252 0 : (void)seq;
253 0 : (void)stem;
254 :
255 0 : switch ( ctx->in_kind[ in_idx ] ) {
256 0 : case IN_KIND_PLUGIN: {
257 0 : FD_TEST( !ctx->is_full_client );
258 0 : fd_gui_plugin_message( ctx->gui, sig, ctx->buf, fd_clock_now( ctx->clock ) );
259 0 : break;
260 0 : }
261 0 : case IN_KIND_REPLAY_OUT: {
262 0 : if( FD_UNLIKELY( sig==REPLAY_SIG_SLOT_COMPLETED ) ) {
263 0 : fd_replay_slot_completed_t const * replay = (fd_replay_slot_completed_t const *)ctx->buf;
264 0 : fd_gui_handle_replay_update( ctx->gui, replay, fd_clock_now( ctx->clock ) );
265 0 : }
266 :
267 0 : if( FD_UNLIKELY( sig==REPLAY_SIG_BECAME_LEADER ) ) {
268 0 : fd_became_leader_t * became_leader = (fd_became_leader_t *)ctx->buf;
269 0 : fd_gui_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 );
270 0 : } else {
271 0 : return;
272 0 : }
273 0 : break;
274 0 : }
275 0 : case IN_KIND_TOWER_OUT: {
276 0 : FD_TEST( ctx->is_full_client );
277 0 : fd_tower_slot_done_t const * tower = (fd_tower_slot_done_t const *)ctx->buf;
278 0 : fd_gui_handle_tower_update( ctx->gui, tower, fd_clock_now( ctx->clock ) );
279 0 : break;
280 0 : }
281 0 : case IN_KIND_SHRED_OUT: {
282 0 : FD_TEST( ctx->is_full_client );
283 0 : if( FD_LIKELY( sz!=0 && sz!=FD_SHRED_DATA_HEADER_SZ + sizeof(fd_hash_t) + sizeof(fd_hash_t) ) ) {
284 0 : ulong slot = fd_disco_shred_out_shred_sig_slot( sig );
285 0 : int is_turbine = fd_disco_shred_out_shred_sig_is_turbine( sig );
286 0 : ulong shred_idx = fd_disco_shred_out_shred_sig_shred_idx( sig );
287 0 : ulong fec_idx = fd_disco_shred_out_shred_sig_fec_set_idx( sig );
288 : /* tsorig is the timestamp when the shred was received by the shred tile */
289 0 : long tsorig_nanos = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, fd_tickcount() ) - ctx->ref_tickcount) / fd_tempo_tick_per_ns( NULL ));
290 0 : fd_gui_handle_shred( ctx->gui, slot, shred_idx, fec_idx, is_turbine, tsorig_nanos );
291 0 : }
292 0 : break;
293 0 : }
294 0 : case IN_KIND_SNAPRD: {
295 0 : FD_TEST( ctx->is_full_client );
296 0 : fd_gui_handle_snapshot_update( ctx->gui, (fd_snaprd_update_t *)ctx->buf );
297 0 : break;
298 0 : }
299 0 : case IN_KIND_REPAIR_NET: {
300 0 : FD_TEST( ctx->is_full_client );
301 0 : uchar * payload;
302 0 : ulong payload_sz;
303 0 : if( FD_LIKELY( fd_ip4_udp_hdr_strip( ctx->buf, sz, &payload, &payload_sz, NULL, NULL, NULL ) ) ) {
304 0 : fd_repair_msg_t const * msg = (fd_repair_msg_t const *)payload;
305 0 : long tsorig_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, fd_tickcount() ) - ctx->ref_tickcount) / fd_tempo_tick_per_ns( NULL ));
306 0 : switch ( msg->kind ) {
307 0 : case FD_REPAIR_KIND_PING:
308 0 : case FD_REPAIR_KIND_PONG:
309 0 : case FD_REPAIR_KIND_ORPHAN: break;
310 0 : case FD_REPAIR_KIND_SHRED: { if( FD_UNLIKELY( msg->shred.slot==0 ) ) { break; } fd_gui_handle_repair_slot( ctx->gui, msg->shred.slot, tsorig_ns ); break; }
311 0 : case FD_REPAIR_KIND_HIGHEST_SHRED: { if( FD_UNLIKELY( msg->highest_shred.slot==0 ) ) { break; } fd_gui_handle_repair_slot( ctx->gui, msg->highest_shred.slot, tsorig_ns ); break; }
312 0 : default: FD_LOG_ERR(( "unexpected repair msg kind %u", msg->kind ));
313 0 : }
314 0 : }
315 0 : break;
316 0 : }
317 0 : case IN_KIND_NET_GOSSVF: {
318 0 : FD_TEST( ctx->is_full_client );
319 0 : uchar * payload;
320 0 : ulong payload_sz;
321 0 : fd_ip4_hdr_t * ip4_hdr;
322 0 : fd_udp_hdr_t * udp_hdr;
323 0 : if( FD_LIKELY( fd_ip4_udp_hdr_strip( ctx->buf, sz, &payload, &payload_sz, NULL, &ip4_hdr, &udp_hdr ) ) ) {
324 0 : fd_gui_peers_handle_gossip_message( ctx->peers, payload, payload_sz, &(fd_ip4_port_t){ .addr = ip4_hdr->saddr, .port = udp_hdr->net_sport }, 1 );
325 0 : }
326 0 : break;
327 0 : }
328 0 : case IN_KIND_GOSSIP_NET: {
329 0 : FD_TEST( ctx->is_full_client );
330 0 : uchar * payload;
331 0 : ulong payload_sz;
332 0 : fd_ip4_hdr_t * ip4_hdr;
333 0 : fd_udp_hdr_t * udp_hdr;
334 0 : FD_TEST( fd_ip4_udp_hdr_strip( ctx->buf, sz, &payload, &payload_sz, NULL, &ip4_hdr, &udp_hdr ) );
335 0 : fd_gui_peers_handle_gossip_message( ctx->peers, payload, payload_sz, &(fd_ip4_port_t){ .addr = ip4_hdr->daddr, .port = udp_hdr->net_dport }, 0 );
336 0 : break;
337 0 : }
338 0 : case IN_KIND_GOSSIP_OUT: {
339 0 : FD_TEST( ctx->is_full_client );
340 0 : fd_gossip_update_message_t * update = (fd_gossip_update_message_t *)ctx->buf;
341 0 : switch( update->tag ) {
342 0 : case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE: FD_TEST( sz == FD_GOSSIP_UPDATE_SZ_CONTACT_INFO_REMOVE ); break;
343 0 : case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO: FD_TEST( sz == FD_GOSSIP_UPDATE_SZ_CONTACT_INFO ); break;
344 0 : default: break;
345 0 : }
346 0 : fd_gui_peers_handle_gossip_update( ctx->peers, update, fd_clock_now( ctx-> clock ) );
347 0 : break;
348 0 : }
349 0 : case IN_KIND_POH_PACK: {
350 0 : FD_TEST( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_BECAME_LEADER );
351 0 : fd_became_leader_t * became_leader = (fd_became_leader_t *)ctx->buf;
352 0 : fd_gui_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 );
353 0 : break;
354 0 : }
355 0 : case IN_KIND_PACK_POH: {
356 0 : fd_gui_unbecame_leader( ctx->gui, fd_disco_bank_sig_slot( sig ), ((fd_done_packing_t *)ctx->buf)->microblocks_in_slot );
357 0 : break;
358 0 : }
359 0 : case IN_KIND_PACK_BANK: {
360 0 : if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK ) ) {
361 0 : fd_microblock_bank_trailer_t * trailer = (fd_microblock_bank_trailer_t *)( ctx->buf+sz-sizeof(fd_microblock_bank_trailer_t) );
362 0 : long now = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / fd_tempo_tick_per_ns( NULL ));
363 0 : fd_gui_microblock_execution_begin( ctx->gui,
364 0 : now,
365 0 : fd_disco_poh_sig_slot( sig ),
366 0 : (fd_txn_p_t *)ctx->buf,
367 0 : (sz-sizeof( fd_microblock_bank_trailer_t ))/sizeof( fd_txn_p_t ),
368 0 : (uint)trailer->microblock_idx,
369 0 : trailer->pack_txn_idx );
370 0 : } else {
371 0 : FD_LOG_ERR(( "unexpected poh packet type %lu", fd_disco_poh_sig_pkt_type( sig ) ));
372 0 : }
373 0 : break;
374 0 : }
375 0 : case IN_KIND_BANK_POH: {
376 0 : fd_microblock_trailer_t * trailer = (fd_microblock_trailer_t *)( ctx->buf+sz-sizeof( fd_microblock_trailer_t ) );
377 0 : long now = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / fd_tempo_tick_per_ns( NULL ));
378 0 : fd_gui_microblock_execution_end( ctx->gui,
379 0 : now,
380 0 : ctx->in_bank_idx[ in_idx ],
381 0 : fd_disco_bank_sig_slot( sig ),
382 0 : (sz-sizeof( fd_microblock_trailer_t ))/sizeof( fd_txn_p_t ),
383 0 : (fd_txn_p_t *)ctx->buf,
384 0 : trailer->pack_txn_idx,
385 0 : trailer->txn_start_pct,
386 0 : trailer->txn_load_end_pct,
387 0 : trailer->txn_end_pct,
388 0 : trailer->txn_preload_end_pct,
389 0 : trailer->tips );
390 0 : break;
391 0 : }
392 0 : default: FD_LOG_ERR(( "unexpected in_kind %lu", ctx->in_kind[ in_idx ] ));
393 0 : }
394 0 : }
395 :
396 : static fd_http_server_response_t
397 0 : gui_http_request( fd_http_server_request_t const * request ) {
398 0 : if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
399 0 : return (fd_http_server_response_t){
400 0 : .status = 405,
401 0 : };
402 0 : }
403 :
404 0 : if( FD_LIKELY( !strcmp( request->path, "/websocket" ) ) ) {
405 0 : return (fd_http_server_response_t){
406 0 : .status = 200,
407 0 : .upgrade_websocket = 1,
408 0 : #ifdef FD_HAS_ZSTD
409 0 : .compress_websocket = request->headers.compress_websocket,
410 : #else
411 : .compress_websocket = 0,
412 : #endif
413 0 : };
414 0 : } else if( FD_LIKELY( !strcmp( request->path, "/favicon.svg" ) ) ) {
415 0 : return (fd_http_server_response_t){
416 0 : .status = 200,
417 0 : .static_body = firedancer_svg,
418 0 : .static_body_len = firedancer_svg_sz,
419 0 : .content_type = "image/svg+xml",
420 0 : .upgrade_websocket = 0,
421 0 : };
422 0 : }
423 :
424 0 : int is_vite_page = !strcmp( request->path, "/" ) ||
425 0 : !strcmp( request->path, "/leaderSchedule" ) ||
426 0 : !strcmp( request->path, "/gossip") ||
427 0 : !strncmp( request->path, "/?", strlen("/?") ) ||
428 0 : !strncmp( request->path, "/leaderSchedule?", strlen("/leaderSchedule?") ) ||
429 0 : !strncmp( request->path, "/gossip?", strlen("/gossip?") );
430 :
431 0 : FD_TEST( STATIC_FILES );
432 0 : for( fd_http_static_file_t const * f = STATIC_FILES; f->name; f++ ) {
433 0 : if( !strcmp( request->path, f->name ) ||
434 0 : (!strcmp( f->name, "/index.html" ) && is_vite_page) ) {
435 0 : char const * content_type = NULL;
436 :
437 0 : char const * ext = strrchr( f->name, '.' );
438 0 : if( FD_LIKELY( ext ) ) {
439 0 : if( !strcmp( ext, ".html" ) ) content_type = "text/html; charset=utf-8";
440 0 : else if( !strcmp( ext, ".css" ) ) content_type = "text/css";
441 0 : else if( !strcmp( ext, ".js" ) ) content_type = "application/javascript";
442 0 : else if( !strcmp( ext, ".svg" ) ) content_type = "image/svg+xml";
443 0 : else if( !strcmp( ext, ".woff" ) ) content_type = "font/woff";
444 0 : else if( !strcmp( ext, ".woff2" ) ) content_type = "font/woff2";
445 0 : }
446 :
447 0 : char const * cache_control = NULL;
448 0 : if( FD_LIKELY( !strncmp( request->path, "/assets", 7 ) ) ) cache_control = "public, max-age=31536000, immutable";
449 0 : else if( FD_LIKELY( !strcmp( f->name, "/index.html" ) ) ) cache_control = "no-cache";
450 :
451 0 : const uchar * data = f->data;
452 0 : ulong data_len = *(f->data_len);
453 :
454 0 : int accepts_zstd = 0;
455 0 : if( FD_LIKELY( request->headers.accept_encoding ) ) {
456 0 : accepts_zstd = !!strstr( request->headers.accept_encoding, "zstd" );
457 0 : }
458 :
459 0 : int accepts_gzip = 0;
460 0 : if( FD_LIKELY( request->headers.accept_encoding ) ) {
461 0 : accepts_gzip = !!strstr( request->headers.accept_encoding, "gzip" );
462 0 : }
463 :
464 0 : char const * content_encoding = NULL;
465 0 : if( FD_LIKELY( accepts_zstd && f->zstd_data ) ) {
466 0 : content_encoding = "zstd";
467 0 : data = f->zstd_data;
468 0 : data_len = *(f->zstd_data_len);
469 0 : } else if( FD_LIKELY( accepts_gzip && f->gzip_data ) ) {
470 0 : content_encoding = "gzip";
471 0 : data = f->gzip_data;
472 0 : data_len = *(f->gzip_data_len);
473 0 : }
474 :
475 0 : return (fd_http_server_response_t){
476 0 : .status = 200,
477 0 : .static_body = data,
478 0 : .static_body_len = data_len,
479 0 : .content_type = content_type,
480 0 : .cache_control = cache_control,
481 0 : .content_encoding = content_encoding,
482 0 : .upgrade_websocket = 0,
483 0 : };
484 0 : }
485 0 : }
486 :
487 0 : return (fd_http_server_response_t){
488 0 : .status = 404,
489 0 : };
490 0 : }
491 :
492 : static void
493 : gui_ws_open( ulong conn_id,
494 0 : void * _ctx ) {
495 0 : fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
496 :
497 0 : fd_gui_ws_open( ctx->gui, conn_id );
498 0 : if( FD_UNLIKELY( ctx->is_full_client ) ) fd_gui_peers_ws_open( ctx->peers, conn_id, fd_clock_now( ctx->clock ) );
499 0 : }
500 :
501 : static void
502 : gui_ws_close( ulong conn_id,
503 : int reason,
504 0 : void * _ctx ) {
505 0 : (void) reason;
506 0 : fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
507 0 : if( FD_UNLIKELY( ctx->is_full_client ) ) fd_gui_peers_ws_close( ctx->peers, conn_id );
508 0 : }
509 :
510 : static void
511 : gui_ws_message( ulong ws_conn_id,
512 : uchar const * data,
513 : ulong data_len,
514 0 : void * _ctx ) {
515 0 : fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
516 :
517 0 : int reason = fd_gui_ws_message( ctx->gui, ws_conn_id, data, data_len );
518 0 : if( FD_UNLIKELY( ctx->is_full_client && reason==FD_HTTP_SERVER_CONNECTION_CLOSE_UNKNOWN_METHOD ) ) reason = fd_gui_peers_ws_message( ctx->peers, ws_conn_id, data, data_len );
519 :
520 0 : if( FD_UNLIKELY( reason<0 ) ) fd_http_server_ws_close( ctx->gui_server, ws_conn_id, reason );
521 0 : }
522 :
523 : static void
524 : privileged_init( fd_topo_t * topo,
525 0 : fd_topo_tile_t * tile ) {
526 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
527 :
528 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
529 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
530 :
531 0 : fd_http_server_params_t http_param = derive_http_params( tile );
532 0 : fd_http_server_t * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
533 :
534 0 : fd_http_server_callbacks_t gui_callbacks = {
535 0 : .request = gui_http_request,
536 0 : .ws_open = gui_ws_open,
537 0 : .ws_close = gui_ws_close,
538 0 : .ws_message = gui_ws_message,
539 0 : };
540 0 : ctx->gui_server = fd_http_server_join( fd_http_server_new( _gui, http_param, gui_callbacks, ctx ) );
541 0 : fd_http_server_listen( ctx->gui_server, tile->gui.listen_addr, tile->gui.listen_port );
542 :
543 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 ));
544 :
545 0 : if( FD_UNLIKELY( !strcmp( tile->gui.identity_key_path, "" ) ) )
546 0 : FD_LOG_ERR(( "identity_key_path not set" ));
547 :
548 0 : ctx->identity_key = fd_keyload_load( tile->gui.identity_key_path, /* pubkey only: */ 1 );
549 :
550 0 : if( FD_UNLIKELY( !strcmp( tile->gui.vote_key_path, "" ) ) ) {
551 0 : ctx->has_vote_key = 0;
552 0 : } else {
553 0 : ctx->has_vote_key = 1;
554 0 : if( FD_UNLIKELY( !fd_base58_decode_32( tile->gui.vote_key_path, (uchar *)ctx->vote_key->uc ) ) ) {
555 0 : const uchar * vote_key = fd_keyload_load( tile->gui.vote_key_path, /* pubkey only: */ 1 );
556 0 : fd_memcpy( (uchar *)ctx->vote_key->uc, vote_key, 32UL );
557 0 : }
558 0 : }
559 0 : }
560 :
561 : static FD_TL fd_alloc_t * cjson_alloc_ctx;
562 :
563 : static void *
564 0 : cjson_alloc( ulong sz ) {
565 0 : if( FD_LIKELY( cjson_alloc_ctx ) ) {
566 0 : return fd_alloc_malloc( cjson_alloc_ctx, 8UL, sz );
567 0 : } else {
568 0 : return malloc( sz );
569 0 : }
570 0 : }
571 :
572 : static void
573 0 : cjson_free( void * ptr ) {
574 0 : if( FD_LIKELY( cjson_alloc_ctx ) ) {
575 0 : fd_alloc_free( cjson_alloc_ctx, ptr );
576 0 : } else {
577 0 : free( ptr );
578 0 : }
579 0 : }
580 :
581 : extern char const fdctl_version_string[];
582 :
583 : static void
584 : unprivileged_init( fd_topo_t * topo,
585 0 : fd_topo_tile_t * tile ) {
586 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
587 :
588 0 : fd_topo_tile_t * gui_tile = &topo->tiles[ fd_topo_find_tile( topo, "gui", 0UL ) ];
589 0 : STATIC_FILES = fd_ptr_if( gui_tile->gui.frontend_release_channel==0UL, (fd_http_static_file_t *)STATIC_FILES_STABLE, (fd_http_static_file_t *)STATIC_FILES_ALPHA );
590 :
591 0 : fd_http_server_params_t http_param = derive_http_params( tile );
592 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
593 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
594 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
595 0 : void * _peers = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_peers_align(), fd_gui_peers_footprint( http_param.max_ws_connection_cnt) );
596 0 : void * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_align(), fd_gui_footprint() );
597 0 : void * _alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
598 :
599 0 : ctx->is_full_client = ULONG_MAX!=fd_topo_find_tile( topo, "repair", 0UL );
600 0 : ctx->snapshots_enabled = ULONG_MAX!=fd_topo_find_tile( topo, "snaprd", 0UL );
601 :
602 0 : fd_clock_default_init( ctx->clock, ctx->clock_mem );
603 0 : ctx->recal_next = fd_clock_recal_next( ctx->clock );
604 :
605 0 : ctx->ref_wallclock = fd_log_wallclock();
606 0 : ctx->ref_tickcount = fd_tickcount();
607 :
608 0 : FD_TEST( fd_cstr_printf_check( ctx->version_string, sizeof( ctx->version_string ), NULL, "%s", fdctl_version_string ) );
609 :
610 0 : ctx->topo = topo;
611 0 : ctx->peers = fd_gui_peers_join( fd_gui_peers_new( _peers, ctx->gui_server, ctx->topo, http_param.max_ws_connection_cnt, fd_clock_now( ctx->clock) ) );
612 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, ctx->is_full_client, ctx->snapshots_enabled, tile->gui.is_voting, tile->gui.schedule_strategy, ctx->topo, fd_clock_now( ctx->clock ) ) );
613 0 : FD_TEST( ctx->gui );
614 :
615 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
616 0 : FD_TEST( ctx->keyswitch );
617 :
618 0 : cjson_alloc_ctx = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
619 0 : FD_TEST( cjson_alloc_ctx );
620 0 : cJSON_Hooks hooks = {
621 0 : .malloc_fn = cjson_alloc,
622 0 : .free_fn = cjson_free,
623 0 : };
624 0 : cJSON_InitHooks( &hooks );
625 :
626 0 : ctx->next_poll_deadline = fd_tickcount();
627 :
628 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
629 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
630 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
631 :
632 0 : if( FD_LIKELY( !strcmp( link->name, "plugin_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_PLUGIN;
633 0 : else if( FD_LIKELY( !strcmp( link->name, "poh_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH_PACK;
634 0 : else if( FD_LIKELY( !strcmp( link->name, "pack_bank" ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_BANK;
635 0 : else if( FD_LIKELY( !strcmp( link->name, "pack_poh" ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_POH;
636 0 : else if( FD_LIKELY( !strcmp( link->name, "bank_poh" ) ) ) ctx->in_kind[ i ] = IN_KIND_BANK_POH;
637 0 : else if( FD_LIKELY( !strcmp( link->name, "shred_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_SHRED_OUT; /* full client only */
638 0 : else if( FD_LIKELY( !strcmp( link->name, "net_gossvf" ) ) ) ctx->in_kind[ i ] = IN_KIND_NET_GOSSVF; /* full client only */
639 0 : else if( FD_LIKELY( !strcmp( link->name, "gossip_net" ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_NET; /* full client only */
640 0 : else if( FD_LIKELY( !strcmp( link->name, "gossip_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_OUT; /* full client only */
641 0 : else if( FD_LIKELY( !strcmp( link->name, "snaprd_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPRD; /* full client only */
642 0 : else if( FD_LIKELY( !strcmp( link->name, "repair_net" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR_NET; /* full client only */
643 0 : else if( FD_LIKELY( !strcmp( link->name, "tower_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_TOWER_OUT; /* full client only */
644 0 : else if( FD_LIKELY( !strcmp( link->name, "replay_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY_OUT; /* full client only */
645 0 : else FD_LOG_ERR(( "gui tile has unexpected input link %lu %s", i, link->name ));
646 :
647 0 : if( FD_LIKELY( !strcmp( link->name, "bank_poh" ) ) ) {
648 0 : ulong producer = fd_topo_find_link_producer( topo, &topo->links[ tile->in_link_id[ i ] ] );
649 0 : ctx->in_bank_idx[ i ] = topo->tiles[ producer ].kind_id;
650 0 : }
651 :
652 0 : ctx->in[ i ].mem = link_wksp->wksp;
653 0 : ctx->in[ i ].mtu = link->mtu;
654 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
655 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
656 0 : }
657 :
658 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
659 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
660 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
661 0 : }
662 :
663 : static ulong
664 : populate_allowed_seccomp( fd_topo_t const * topo,
665 : fd_topo_tile_t const * tile,
666 : ulong out_cnt,
667 0 : struct sock_filter * out ) {
668 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
669 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
670 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
671 :
672 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 ) );
673 0 : return sock_filter_policy_fd_gui_tile_instr_cnt;
674 0 : }
675 :
676 : static ulong
677 : populate_allowed_fds( fd_topo_t const * topo,
678 : fd_topo_tile_t const * tile,
679 : ulong out_fds_cnt,
680 0 : int * out_fds ) {
681 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
682 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
683 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
684 :
685 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
686 :
687 0 : ulong out_cnt = 0UL;
688 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
689 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
690 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
691 0 : out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->gui_server ); /* gui listen socket */
692 0 : return out_cnt;
693 0 : }
694 :
695 : static ulong
696 : rlimit_file_cnt( fd_topo_t const * topo FD_PARAM_UNUSED,
697 0 : fd_topo_tile_t const * tile ) {
698 : /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
699 0 : ulong base = 5UL;
700 0 : return base + tile->gui.max_http_connections + tile->gui.max_websocket_connections;
701 0 : }
702 :
703 0 : #define STEM_BURST (1UL)
704 :
705 : /* See explanation in fd_pack */
706 0 : #define STEM_LAZY (128L*3000L)
707 :
708 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_gui_ctx_t
709 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gui_ctx_t)
710 :
711 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
712 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
713 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
714 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
715 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
716 :
717 : #include "../../disco/stem/fd_stem.c"
718 :
719 : fd_topo_run_tile_t fd_tile_gui = {
720 : .name = "gui",
721 : .rlimit_file_cnt_fn = rlimit_file_cnt,
722 : .populate_allowed_seccomp = populate_allowed_seccomp,
723 : .populate_allowed_fds = populate_allowed_fds,
724 : .scratch_align = scratch_align,
725 : .scratch_footprint = scratch_footprint,
726 : .loose_footprint = loose_footprint,
727 : .privileged_init = privileged_init,
728 : .unprivileged_init = unprivileged_init,
729 : .run = stem_run,
730 : };
|