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 :
19 : #include "generated/fd_gui_tile_seccomp.h"
20 :
21 : #include "../../disco/tiles.h"
22 : #include "../../disco/keyguard/fd_keyload.h"
23 : #include "../../disco/keyguard/fd_keyswitch.h"
24 : #include "../../disco/gui/fd_gui.h"
25 : #include "../../disco/plugin/fd_plugin.h"
26 : #include "../../discof/replay/fd_execrp.h"
27 : #include "../../disco/metrics/fd_metrics.h"
28 : #include "../../disco/net/fd_net_tile.h"
29 : #include "../../discof/genesis/fd_genesi_tile.h" // TODO: Layering violation
30 : #include "../../waltz/http/fd_http_server.h"
31 : #include "../../waltz/http/fd_http_server_private.h"
32 : #include "../../ballet/json/cJSON_alloc.h"
33 : #include "../../util/clock/fd_clock.h"
34 : #include "../../discof/repair/fd_repair.h"
35 : #include "../../discof/replay/fd_replay_tile.h"
36 : #include "../../disco/shred/fd_shred_tile.h"
37 :
38 0 : #define IN_KIND_PLUGIN ( 0UL)
39 0 : #define IN_KIND_POH_PACK ( 1UL)
40 0 : #define IN_KIND_PACK_EXECLE ( 2UL)
41 0 : #define IN_KIND_PACK_POH ( 3UL)
42 0 : #define IN_KIND_EXECLE_POH ( 4UL)
43 0 : #define IN_KIND_SHRED_OUT ( 5UL) /* firedancer only */
44 0 : #define IN_KIND_NET_GOSSVF ( 6UL) /* firedancer only */
45 0 : #define IN_KIND_GOSSIP_NET ( 7UL) /* firedancer only */
46 0 : #define IN_KIND_GOSSIP_OUT ( 8UL) /* firedancer only */
47 0 : #define IN_KIND_SNAPCT ( 9UL) /* firedancer only */
48 0 : #define IN_KIND_REPAIR_NET (10UL) /* firedancer only */
49 0 : #define IN_KIND_TOWER_OUT (11UL) /* firedancer only */
50 0 : #define IN_KIND_REPLAY_OUT (12UL) /* firedancer only */
51 0 : #define IN_KIND_EPOCH (13UL) /* firedancer only */
52 0 : #define IN_KIND_GENESI_OUT (14UL) /* firedancer only */
53 0 : #define IN_KIND_SNAPIN (15UL) /* firedancer only */
54 0 : #define IN_KIND_EXECRP_REPLAY (16UL) /* firedancer only */
55 0 : #define IN_KIND_BUNDLE (17UL)
56 0 : #define IN_KIND_SNAPIN_MANIF (18UL) /* firedancer only */
57 :
58 : FD_IMPORT_BINARY( firedancer_svg, "book/public/fire.svg" );
59 :
60 0 : #define FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN 65536
61 0 : #define FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN 65536
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 : struct fd_gui_out_ctx {
87 : ulong idx;
88 : fd_wksp_t * mem;
89 : ulong chunk0;
90 : ulong wmark;
91 : ulong chunk;
92 : };
93 :
94 : typedef struct fd_gui_out_ctx fd_gui_out_ctx_t;
95 :
96 : typedef struct {
97 : fd_topo_t * topo;
98 :
99 : int is_full_client;
100 : int snapshots_enabled;
101 :
102 : fd_gui_t * gui;
103 : fd_gui_peers_ctx_t * peers;
104 :
105 : ulong in_cnt;
106 : ulong idle_cnt;
107 :
108 : /* Most of the gui tile uses fd_clock for timing, but some stem
109 : timestamps still used tickcounts, so we keep separate timestamps
110 : here to handle those cases until fd_clock is more widely adopted. */
111 : long ref_wallclock;
112 : long ref_tickcount;
113 : const double tick_per_ns;
114 :
115 : fd_clock_t clock[1];
116 : long recal_next; /* next recalibration time (ns) */
117 :
118 : uchar __attribute__((aligned(FD_CLOCK_ALIGN))) clock_mem[ FD_CLOCK_FOOTPRINT ];
119 :
120 : ulong chunk;
121 : union {
122 : struct {
123 : ulong slot;
124 : ulong shred_idx;
125 : } repair_net;
126 :
127 : uchar net_gossvf[ FD_NET_MTU ];
128 : uchar gossip_net[ FD_NET_MTU ];
129 : } parsed;
130 :
131 : fd_http_server_t * gui_server;
132 :
133 : long next_poll_deadline;
134 :
135 : char version_string[ 64UL ];
136 :
137 : fd_keyswitch_t * keyswitch;
138 : uchar const * identity_key;
139 :
140 : int has_vote_key;
141 : fd_pubkey_t const vote_key[ 1UL ];
142 :
143 : ulong in_kind[ 64UL ];
144 : int in_reliable[ 64UL ];
145 : ulong in_bank_idx[ 64UL ];
146 : fd_gui_in_ctx_t in[ 64UL ];
147 :
148 : fd_net_rx_bounds_t net_in_bounds[ 64UL ];
149 : } fd_gui_ctx_t;
150 :
151 : FD_FN_CONST static inline ulong
152 0 : scratch_align( void ) {
153 0 : return 128UL;
154 0 : }
155 :
156 : static inline ulong
157 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
158 0 : fd_http_server_params_t http_param = derive_http_params( tile );
159 0 : ulong http_fp = fd_http_server_footprint( http_param );
160 0 : if( FD_UNLIKELY( !http_fp ) ) FD_LOG_ERR(( "Invalid [tiles.gui] config parameters" ));
161 :
162 0 : ulong l = FD_LAYOUT_INIT;
163 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
164 0 : l = FD_LAYOUT_APPEND( l, fd_http_server_align(), http_fp );
165 0 : l = FD_LAYOUT_APPEND( l, fd_gui_peers_align(), fd_gui_peers_footprint( http_param.max_ws_connection_cnt ) );
166 0 : l = FD_LAYOUT_APPEND( l, fd_gui_align(), fd_gui_footprint( tile->gui.tile_cnt ) );
167 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
168 0 : return FD_LAYOUT_FINI( l, scratch_align() );
169 0 : }
170 :
171 : FD_FN_PURE static inline ulong
172 0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
173 0 : return 256UL * (1UL<<20UL); /* 256MiB of heap space for the cJSON allocator */
174 0 : }
175 :
176 : static inline void
177 0 : during_housekeeping( fd_gui_ctx_t * ctx ) {
178 0 : ctx->ref_wallclock = fd_log_wallclock();
179 0 : ctx->ref_tickcount = fd_tickcount();
180 :
181 0 : if( FD_UNLIKELY( fd_clock_now( ctx->clock ) >= ctx->recal_next ) ) {
182 0 : ctx->recal_next = fd_clock_default_recal( ctx->clock );
183 0 : }
184 :
185 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
186 0 : fd_gui_set_identity( ctx->gui, ctx->keyswitch->bytes );
187 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
188 0 : }
189 0 : }
190 :
191 : static inline void
192 0 : metrics_write( fd_gui_ctx_t * ctx ) {
193 0 : FD_MGAUGE_SET( GUI, CONNECTION_COUNT, ctx->gui_server->metrics.connection_cnt );
194 0 : FD_MGAUGE_SET( GUI, WEBSOCKET_CONNECTION_COUNT, ctx->gui_server->metrics.ws_connection_cnt );
195 :
196 0 : FD_MCNT_SET( GUI, WEBSOCKET_FRAMES_SENT, ctx->gui_server->metrics.frames_written );
197 0 : FD_MCNT_SET( GUI, WEBSOCKET_FRAMES_RECEIVED, ctx->gui_server->metrics.frames_read );
198 :
199 0 : FD_MCNT_SET( GUI, BYTES_WRITTEN, ctx->gui_server->metrics.bytes_written );
200 0 : FD_MCNT_SET( GUI, BYTES_READ, ctx->gui_server->metrics.bytes_read );
201 0 : }
202 :
203 : static void
204 : before_credit( fd_gui_ctx_t * ctx,
205 : fd_stem_context_t * stem,
206 0 : int * charge_busy ) {
207 0 : (void)stem;
208 :
209 0 : ctx->idle_cnt++;
210 0 : if( FD_LIKELY( ctx->idle_cnt<2UL*ctx->in_cnt ) ) return;
211 0 : ctx->idle_cnt = 0UL;
212 :
213 0 : int charge_busy_server = 0;
214 0 : long now = fd_tickcount();
215 0 : if( FD_UNLIKELY( now>=ctx->next_poll_deadline ) ) {
216 0 : charge_busy_server = fd_http_server_poll( ctx->gui_server, 0 );
217 0 : ctx->next_poll_deadline = fd_tickcount() + (long)(ctx->tick_per_ns * 128L * 1000L);
218 0 : }
219 :
220 0 : int charge_poll = 0;
221 0 : charge_poll |= fd_gui_poll( ctx->gui, fd_clock_now( ctx->clock ) );
222 0 : if( FD_UNLIKELY( ctx->is_full_client ) ) charge_poll |= fd_gui_peers_poll( ctx->peers, fd_clock_now( ctx->clock ) );
223 :
224 0 : *charge_busy = charge_busy_server | charge_poll;
225 0 : }
226 :
227 : static int
228 : before_frag( fd_gui_ctx_t * ctx,
229 : ulong in_idx,
230 : ulong seq,
231 0 : ulong sig ) {
232 0 : (void)seq;
233 :
234 : /* Ignore "done draining banks" and "reduce microblock bound" signals from pack->poh */
235 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PACK_POH && (sig==FD_PACK_MSG_DONE_DRAINING || sig==FD_PACK_MSG_REDUCE_MB_BOUND) ) ) return 1;
236 :
237 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP_OUT &&
238 0 : (sig==FD_GOSSIP_UPDATE_TAG_WFS_DONE || sig==FD_GOSSIP_UPDATE_TAG_PEER_SATURATED) ) ) return 1;
239 0 : return 0;
240 0 : }
241 :
242 : static inline void
243 : during_frag( fd_gui_ctx_t * ctx,
244 : ulong in_idx,
245 : ulong seq FD_PARAM_UNUSED,
246 : ulong sig,
247 : ulong chunk,
248 : ulong sz,
249 0 : ulong ctl ) {
250 :
251 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
252 :
253 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_PLUGIN ) ) {
254 : /* ... todo... sigh, sz is not correct since it's too big */
255 0 : if( FD_LIKELY( sig==FD_PLUGIN_MSG_GOSSIP_UPDATE ) ) {
256 0 : ulong peer_cnt = FD_LOAD( ulong, src );
257 0 : FD_TEST( peer_cnt<=FD_GUI_MAX_PEER_CNT );
258 0 : sz = 8UL + peer_cnt*FD_GOSSIP_LINK_MSG_SIZE;
259 0 : } else if( FD_LIKELY( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE ) ) {
260 0 : ulong peer_cnt = FD_LOAD( ulong, src );
261 0 : FD_TEST( peer_cnt<=FD_GUI_MAX_PEER_CNT );
262 0 : sz = 8UL + peer_cnt*112UL;
263 0 : } else if( FD_UNLIKELY( sig==FD_PLUGIN_MSG_LEADER_SCHEDULE ) ) {
264 0 : ulong staked_vote_cnt = FD_LOAD( ulong, src+8UL );
265 0 : ulong staked_id_cnt = FD_LOAD( ulong, src+16UL );
266 0 : FD_TEST( staked_vote_cnt<=MAX_COMPRESSED_STAKE_WEIGHTS );
267 0 : FD_TEST( staked_id_cnt<=MAX_SHRED_DESTS );
268 0 : sz = fd_stake_weight_msg_sz( staked_vote_cnt, staked_id_cnt );
269 0 : }
270 0 : }
271 :
272 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_EPOCH ) ) {
273 0 : fd_epoch_info_msg_t * epoch_info = (fd_epoch_info_msg_t *)src;
274 0 : FD_TEST( epoch_info->staked_vote_cnt<=MAX_COMPRESSED_STAKE_WEIGHTS );
275 0 : FD_TEST( epoch_info->staked_id_cnt<=MAX_SHRED_DESTS );
276 0 : sz = fd_epoch_info_msg_sz( epoch_info->staked_vote_cnt, epoch_info->staked_id_cnt );
277 0 : }
278 :
279 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GENESI_OUT ) ) {
280 0 : sz = sig;
281 0 : }
282 :
283 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY_OUT ) ) {
284 0 : if( FD_LIKELY( sig!=REPLAY_SIG_SLOT_COMPLETED && sig!=REPLAY_SIG_BECAME_LEADER ) ) return;
285 0 : }
286 :
287 0 : if( FD_UNLIKELY( (sz>0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark)) || sz>ctx->in[ in_idx ].mtu ) )
288 0 : FD_LOG_ERR(( "in_kind %lu chunk %lu %lu corrupt, not in range [%lu,%lu] or too large (%lu)", ctx->in_kind[ in_idx ], chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark, ctx->in[ in_idx ].mtu ));
289 :
290 0 : switch( ctx->in_kind[ in_idx ] ) {
291 0 : case IN_KIND_REPAIR_NET: {
292 0 : FD_TEST( ctx->is_full_client );
293 0 : ctx->parsed.repair_net.slot = ULONG_MAX;
294 0 : uchar * payload;
295 0 : ulong payload_sz;
296 0 : if( FD_LIKELY( fd_ip4_udp_hdr_strip( src, sz, &payload, &payload_sz, NULL, NULL, NULL ) ) ) {
297 0 : fd_repair_msg_t const * msg = (fd_repair_msg_t const *)payload;
298 0 : if( FD_LIKELY( msg->kind==FD_REPAIR_KIND_SHRED ) ) {
299 0 : if( FD_UNLIKELY( msg->shred.slot==0 ) ) break;
300 0 : ctx->parsed.repair_net.slot = msg->shred.slot;
301 0 : ctx->parsed.repair_net.shred_idx = msg->shred.shred_idx;
302 0 : }
303 0 : }
304 0 : break;
305 0 : }
306 0 : case IN_KIND_NET_GOSSVF: {
307 0 : FD_TEST( ctx->is_full_client );
308 0 : FD_TEST( sz<=sizeof(ctx->parsed.net_gossvf) );
309 0 : uchar const * net_src = fd_net_rx_translate_frag( &ctx->net_in_bounds[ in_idx ], chunk, ctl, sz );
310 0 : fd_memcpy( ctx->parsed.net_gossvf, net_src, sz );
311 0 : break;
312 0 : }
313 0 : case IN_KIND_GOSSIP_NET: {
314 0 : FD_TEST( ctx->is_full_client );
315 0 : FD_TEST( sz<=sizeof(ctx->parsed.gossip_net) );
316 0 : fd_memcpy( ctx->parsed.gossip_net, src, sz );
317 0 : break;
318 0 : }
319 0 : }
320 :
321 0 : ctx->chunk = chunk;
322 0 : }
323 :
324 : static inline void
325 : after_frag( fd_gui_ctx_t * ctx,
326 : ulong in_idx,
327 : ulong seq,
328 : ulong sig,
329 : ulong sz,
330 : ulong tsorig,
331 : ulong tspub,
332 0 : fd_stem_context_t * stem ) {
333 0 : (void)seq; (void)stem;
334 :
335 0 : if( FD_LIKELY( ctx->in_reliable[ in_idx ] ) ) ctx->idle_cnt = 0UL;
336 :
337 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
338 :
339 0 : switch ( ctx->in_kind[ in_idx ] ) {
340 0 : case IN_KIND_PLUGIN: {
341 0 : FD_TEST( !ctx->is_full_client );
342 0 : fd_gui_plugin_message( ctx->gui, sig, src, fd_clock_now( ctx->clock ) );
343 0 : break;
344 0 : }
345 0 : case IN_KIND_EXECRP_REPLAY: {
346 0 : FD_TEST( ctx->is_full_client );
347 0 : if( FD_LIKELY( sig>>32==FD_EXECRP_TT_TXN_EXEC ) ) {
348 0 : fd_execrp_task_done_msg_t * msg = (fd_execrp_task_done_msg_t *)src;
349 :
350 0 : long tickcount = fd_tickcount();
351 0 : long tsorig_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, tickcount ) - ctx->ref_tickcount) / ctx->tick_per_ns);
352 0 : long tspub_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, tickcount ) - ctx->ref_tickcount) / ctx->tick_per_ns);
353 0 : fd_gui_handle_exec_txn_done( ctx->gui, msg->txn_exec->slot, msg->txn_exec->start_shred_idx, msg->txn_exec->end_shred_idx, tsorig_ns, tspub_ns );
354 :
355 0 : int txn_succeeded = msg->txn_exec->is_committable && !msg->txn_exec->is_fees_only && !msg->txn_exec->txn_err;
356 0 : if( FD_UNLIKELY( msg->txn_exec->vote.slot!=ULONG_MAX && txn_succeeded ) ) {
357 0 : fd_gui_peers_handle_vote( ctx->peers,
358 0 : msg->txn_exec->vote.vote_acct,
359 0 : msg->txn_exec->vote.slot,
360 0 : !memcmp(ctx->gui->summary.identity_key->uc, msg->txn_exec->vote.identity->uc, sizeof(fd_pubkey_t) ) );
361 0 : }
362 0 : }
363 :
364 0 : break;
365 0 : }
366 0 : case IN_KIND_REPLAY_OUT: {
367 0 : FD_TEST( ctx->is_full_client );
368 0 : if( FD_UNLIKELY( sig==REPLAY_SIG_SLOT_COMPLETED ) ) {
369 0 : fd_replay_slot_completed_t const * slot_completed = (fd_replay_slot_completed_t const *)src;
370 :
371 0 : fd_gui_peers_update_delinquency( ctx->peers, fd_clock_now( ctx->clock ) );
372 :
373 0 : fd_gui_handle_replay_update( ctx->gui, slot_completed, ctx->peers->slot_voted, fd_clock_now( ctx->clock ) );
374 0 : } else if( FD_UNLIKELY( sig==REPLAY_SIG_BECAME_LEADER ) ) {
375 0 : fd_became_leader_t * became_leader = (fd_became_leader_t *)src;
376 0 : fd_gui_became_leader( ctx->gui, became_leader->slot, became_leader->slot_start_ns, became_leader->slot_end_ns, became_leader->limits.slot_max_cost, became_leader->max_microblocks_in_slot );
377 0 : } else {
378 0 : return;
379 0 : }
380 0 : break;
381 0 : }
382 0 : case IN_KIND_EPOCH: {
383 0 : FD_TEST( ctx->is_full_client );
384 :
385 0 : fd_epoch_info_msg_t * epoch_info = (fd_epoch_info_msg_t *)src;
386 0 : fd_gui_handle_epoch_info( ctx->gui, epoch_info, fd_clock_now( ctx->clock ) );
387 0 : fd_gui_peers_handle_epoch_info( ctx->peers, epoch_info, fd_clock_now( ctx->clock ) );
388 0 : break;
389 0 : }
390 0 : case IN_KIND_SNAPIN: {
391 0 : FD_TEST( ctx->is_full_client );
392 0 : fd_gui_peers_handle_config_account( ctx->peers, src, sz );
393 0 : break;
394 0 : }
395 0 : case IN_KIND_SNAPIN_MANIF: {
396 0 : FD_TEST( ctx->is_full_client );
397 :
398 0 : if( fd_ssmsg_sig_message( sig )==FD_SSMSG_DONE ) {
399 0 : fd_gui_peers_commit_snapshot_manifest( ctx->peers );
400 0 : } else {
401 0 : fd_gui_stage_snapshot_manifest( ctx->gui, (fd_snapshot_manifest_t const *)src );
402 0 : fd_gui_peers_stage_snapshot_manifest( ctx->peers, (fd_snapshot_manifest_t const *)src, fd_clock_now( ctx->clock ) );
403 0 : }
404 0 : break;
405 0 : }
406 0 : case IN_KIND_GENESI_OUT: {
407 0 : FD_TEST( ctx->is_full_client );
408 0 : fd_genesis_meta_t const * meta = (fd_genesis_meta_t const *)src;
409 0 : fd_gui_handle_genesis_hash( ctx->gui, &meta->genesis_hash );
410 0 : break;
411 0 : }
412 0 : case IN_KIND_TOWER_OUT: {
413 0 : FD_TEST( ctx->is_full_client );
414 0 : if( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_DONE )) {
415 0 : fd_tower_slot_done_t const * tower = (fd_tower_slot_done_t const *)src;
416 0 : fd_gui_handle_tower_update( ctx->gui, tower, fd_clock_now( ctx->clock ) );
417 0 : }
418 0 : if( FD_UNLIKELY( sig==FD_TOWER_SIG_SLOT_CONFIRMED ) ) {
419 0 : fd_gui_handle_votes_update( ctx->gui, (fd_tower_slot_confirmed_t const *)src );
420 0 : }
421 0 : break;
422 0 : }
423 0 : case IN_KIND_SHRED_OUT: {
424 0 : FD_TEST( ctx->is_full_client );
425 0 : long tsorig_nanos = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
426 0 : uint sig_src = fd_shred_sig_src( sig );
427 0 : if( FD_LIKELY( sig_src==SHRED_SIG_SRC_TURBINE || sig_src==SHRED_SIG_SRC_REPAIR || sig_src==SHRED_SIG_SRC_BAD_REPAIR ) ) {
428 0 : fd_shred_base_t const * msg = (fd_shred_base_t const *)fd_type_pun_const( src );
429 0 : ulong slot = msg->shred.slot;
430 0 : ulong shred_idx = msg->shred.idx;
431 0 : int is_turbine = sig_src==SHRED_SIG_SRC_TURBINE;
432 : /* tsorig is the timestamp when the shred was received by the shred tile */
433 0 : fd_gui_handle_shred( ctx->gui, slot, shred_idx, is_turbine, tsorig_nanos );
434 0 : }
435 0 : if( FD_UNLIKELY( sig==SHRED_SIG_FEC_COMPLETE_LEADER ) ) {
436 0 : fd_fec_complete_t const * complete_msg = (fd_fec_complete_t const *)fd_type_pun_const( src );
437 0 : fd_gui_handle_leader_fec( ctx->gui, complete_msg->last_shred_hdr.slot, FD_FEC_SHRED_CNT, complete_msg->last_shred_hdr.data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE, tsorig_nanos );
438 0 : }
439 0 : break;
440 0 : }
441 0 : case IN_KIND_SNAPCT: {
442 0 : FD_TEST( ctx->is_full_client );
443 0 : fd_gui_handle_snapshot_update( ctx->gui, (fd_snapct_update_t *)src );
444 0 : break;
445 0 : }
446 0 : case IN_KIND_REPAIR_NET: {
447 0 : if( FD_UNLIKELY( ctx->parsed.repair_net.slot==ULONG_MAX ) ) break;
448 0 : long tsorig_ns = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tsorig, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
449 0 : fd_gui_handle_repair_request( ctx->gui, ctx->parsed.repair_net.slot, ctx->parsed.repair_net.shred_idx, tsorig_ns );
450 0 : break;
451 0 : }
452 0 : case IN_KIND_NET_GOSSVF: {
453 0 : FD_TEST( ctx->is_full_client );
454 0 : uchar * payload;
455 0 : ulong payload_sz;
456 0 : fd_ip4_hdr_t * ip4_hdr;
457 0 : fd_udp_hdr_t * udp_hdr;
458 0 : if( FD_LIKELY( fd_ip4_udp_hdr_strip( ctx->parsed.net_gossvf, sz, &payload, &payload_sz, NULL, &ip4_hdr, &udp_hdr ) ) ) {
459 0 : fd_gossip_socket_t socket = {
460 0 : .is_ipv6 = 0,
461 0 : .ip4 = ip4_hdr->saddr,
462 0 : .port = udp_hdr->net_sport,
463 0 : };
464 0 : fd_gui_peers_handle_gossip_message( ctx->peers, payload, payload_sz, &socket, 1 );
465 0 : }
466 0 : break;
467 0 : }
468 0 : case IN_KIND_GOSSIP_NET: {
469 0 : FD_TEST( ctx->is_full_client );
470 0 : uchar * payload;
471 0 : ulong payload_sz;
472 0 : fd_ip4_hdr_t * ip4_hdr;
473 0 : fd_udp_hdr_t * udp_hdr;
474 0 : FD_TEST( fd_ip4_udp_hdr_strip( ctx->parsed.gossip_net, sz, &payload, &payload_sz, NULL, &ip4_hdr, &udp_hdr ) );
475 0 : fd_gossip_socket_t socket = {
476 0 : .is_ipv6 = 0,
477 0 : .ip4 = ip4_hdr->daddr,
478 0 : .port = udp_hdr->net_dport,
479 0 : };
480 0 : fd_gui_peers_handle_gossip_message( ctx->peers, payload, payload_sz, &socket, 0 );
481 0 : break;
482 0 : }
483 0 : case IN_KIND_GOSSIP_OUT: {
484 0 : FD_TEST( ctx->is_full_client );
485 0 : fd_gui_peers_handle_gossip_update( ctx->peers, (fd_gossip_update_message_t *)src, fd_clock_now( ctx-> clock ) );
486 0 : break;
487 0 : }
488 0 : case IN_KIND_POH_PACK: {
489 0 : FD_TEST( !ctx->is_full_client );
490 0 : FD_TEST( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_BECAME_LEADER );
491 0 : fd_became_leader_t * became_leader = (fd_became_leader_t *)src;
492 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 );
493 0 : break;
494 0 : }
495 0 : case IN_KIND_PACK_POH: {
496 0 : fd_gui_unbecame_leader( ctx->gui, fd_disco_execle_sig_slot( sig ), (fd_done_packing_t const *)src, fd_clock_now( ctx->clock ) );
497 0 : break;
498 0 : }
499 0 : case IN_KIND_PACK_EXECLE: {
500 0 : FD_TEST( sz>=sizeof(fd_microblock_execle_trailer_t) );
501 0 : FD_TEST( (sz-sizeof(fd_microblock_execle_trailer_t))%sizeof(fd_txn_e_t)==0UL );
502 :
503 0 : if( FD_LIKELY( fd_disco_poh_sig_pkt_type( sig )==POH_PKT_TYPE_MICROBLOCK ) ) {
504 0 : fd_microblock_execle_trailer_t trailer[1];
505 0 : fd_memcpy( trailer, src+sz-sizeof(fd_microblock_execle_trailer_t), sizeof(fd_microblock_execle_trailer_t) );
506 0 : long now = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
507 0 : fd_gui_microblock_execution_begin( ctx->gui,
508 0 : now,
509 0 : fd_disco_poh_sig_slot( sig ),
510 0 : (fd_txn_e_t *)src,
511 0 : (sz-sizeof( fd_microblock_execle_trailer_t ))/sizeof( fd_txn_e_t ),
512 0 : (uint)trailer->microblock_idx,
513 0 : trailer->pack_txn_idx );
514 0 : } else {
515 0 : FD_LOG_ERR(( "unexpected poh packet type %lu", fd_disco_poh_sig_pkt_type( sig ) ));
516 0 : }
517 0 : break;
518 0 : }
519 0 : case IN_KIND_EXECLE_POH: {
520 0 : FD_TEST( sz>=sizeof(fd_microblock_trailer_t) );
521 0 : FD_TEST( (sz-sizeof(fd_microblock_trailer_t))%sizeof(fd_txn_p_t)==0UL );
522 :
523 0 : fd_microblock_trailer_t trailer[1];
524 0 : fd_memcpy( trailer, src+sz-sizeof(fd_microblock_trailer_t), sizeof(fd_microblock_trailer_t) );
525 0 : long now = ctx->ref_wallclock + (long)((double)(fd_frag_meta_ts_decomp( tspub, fd_tickcount() ) - ctx->ref_tickcount) / ctx->tick_per_ns);
526 0 : ulong txn_cnt = (sz-sizeof( fd_microblock_trailer_t ))/sizeof(fd_txn_p_t);
527 0 : fd_gui_microblock_execution_end( ctx->gui,
528 0 : now,
529 0 : ctx->in_bank_idx[ in_idx ],
530 0 : fd_disco_execle_sig_slot( sig ),
531 0 : txn_cnt,
532 0 : (fd_txn_p_t *)src,
533 0 : trailer->pack_txn_idx,
534 0 : trailer->txn_start_pct,
535 0 : trailer->txn_load_end_pct,
536 0 : trailer->txn_end_pct,
537 0 : trailer->txn_preload_end_pct,
538 0 : trailer->tips );
539 0 : break;
540 0 : }
541 0 : case IN_KIND_BUNDLE: {
542 0 : fd_gui_handle_block_engine_update( ctx->gui, (fd_bundle_block_engine_update_t *)src );
543 0 : break;
544 0 : }
545 0 : default: FD_LOG_ERR(( "unexpected in_kind %lu", ctx->in_kind[ in_idx ] ));
546 0 : }
547 0 : }
548 :
549 : static fd_http_server_response_t
550 0 : gui_http_request( fd_http_server_request_t const * request ) {
551 0 : if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
552 0 : return (fd_http_server_response_t){
553 0 : .status = 405,
554 0 : };
555 0 : }
556 :
557 0 : if( FD_LIKELY( !strcmp( request->path, "/websocket" ) ) ) {
558 0 : return (fd_http_server_response_t){
559 0 : .status = 200,
560 0 : .upgrade_websocket = 1,
561 0 : #ifdef FD_HAS_ZSTD
562 0 : .compress_websocket = request->headers.compress_websocket,
563 : #else
564 : .compress_websocket = 0,
565 : #endif
566 0 : };
567 0 : } else if( FD_LIKELY( !strcmp( request->path, "/favicon.svg" ) ) ) {
568 0 : return (fd_http_server_response_t){
569 0 : .status = 200,
570 0 : .static_body = firedancer_svg,
571 0 : .static_body_len = firedancer_svg_sz,
572 0 : .content_type = "image/svg+xml",
573 0 : .upgrade_websocket = 0,
574 0 : };
575 0 : }
576 :
577 0 : int is_vite_page = !strcmp( request->path, "/" ) ||
578 0 : !strcmp( request->path, "/slotDetails" ) ||
579 0 : !strcmp( request->path, "/leaderSchedule" ) ||
580 0 : !strcmp( request->path, "/gossip") ||
581 0 : !strncmp( request->path, "/?", strlen("/?") ) ||
582 0 : !strncmp( request->path, "/slotDetails?", strlen("/slotDetails?") ) ||
583 0 : !strncmp( request->path, "/leaderSchedule?", strlen("/leaderSchedule?") ) ||
584 0 : !strncmp( request->path, "/gossip?", strlen("/gossip?") );
585 :
586 0 : FD_TEST( STATIC_FILES );
587 0 : for( fd_http_static_file_t const * f = STATIC_FILES; f->name; f++ ) {
588 0 : if( !strcmp( request->path, f->name ) ||
589 0 : (!strcmp( f->name, "/index.html" ) && is_vite_page) ) {
590 0 : char const * content_type = NULL;
591 :
592 0 : char const * ext = strrchr( f->name, '.' );
593 0 : if( FD_LIKELY( ext ) ) {
594 0 : if( !strcmp( ext, ".html" ) ) content_type = "text/html; charset=utf-8";
595 0 : else if( !strcmp( ext, ".css" ) ) content_type = "text/css";
596 0 : else if( !strcmp( ext, ".js" ) ) content_type = "application/javascript";
597 0 : else if( !strcmp( ext, ".svg" ) ) content_type = "image/svg+xml";
598 0 : else if( !strcmp( ext, ".woff" ) ) content_type = "font/woff";
599 0 : else if( !strcmp( ext, ".woff2" ) ) content_type = "font/woff2";
600 0 : }
601 :
602 0 : char const * cache_control = NULL;
603 0 : if( FD_LIKELY( !strncmp( request->path, "/assets", 7 ) ) ) cache_control = "public, max-age=31536000, immutable";
604 0 : else if( FD_LIKELY( !strcmp( f->name, "/index.html" ) ) ) cache_control = "no-cache";
605 :
606 0 : const uchar * data = f->data;
607 0 : ulong data_len = *(f->data_len);
608 :
609 0 : int accepts_zstd = 0;
610 0 : if( FD_LIKELY( request->headers.accept_encoding ) ) {
611 0 : accepts_zstd = !!strstr( request->headers.accept_encoding, "zstd" );
612 0 : }
613 :
614 0 : int accepts_gzip = 0;
615 0 : if( FD_LIKELY( request->headers.accept_encoding ) ) {
616 0 : accepts_gzip = !!strstr( request->headers.accept_encoding, "gzip" );
617 0 : }
618 :
619 0 : char const * content_encoding = NULL;
620 0 : if( FD_LIKELY( accepts_zstd && f->zstd_data ) ) {
621 0 : content_encoding = "zstd";
622 0 : data = f->zstd_data;
623 0 : data_len = *(f->zstd_data_len);
624 0 : } else if( FD_LIKELY( accepts_gzip && f->gzip_data ) ) {
625 0 : content_encoding = "gzip";
626 0 : data = f->gzip_data;
627 0 : data_len = *(f->gzip_data_len);
628 0 : }
629 :
630 0 : return (fd_http_server_response_t){
631 0 : .status = 200,
632 0 : .static_body = data,
633 0 : .static_body_len = data_len,
634 0 : .content_type = content_type,
635 0 : .cache_control = cache_control,
636 0 : .content_encoding = content_encoding,
637 0 : .upgrade_websocket = 0,
638 0 : };
639 0 : }
640 0 : }
641 :
642 0 : return (fd_http_server_response_t){
643 0 : .status = 404,
644 0 : };
645 0 : }
646 :
647 : static void
648 : gui_ws_open( ulong conn_id,
649 0 : void * _ctx ) {
650 0 : fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
651 :
652 0 : fd_gui_ws_open( ctx->gui, conn_id, fd_clock_now( ctx->clock ) );
653 0 : if( FD_UNLIKELY( ctx->is_full_client ) ) fd_gui_peers_ws_open( ctx->peers, conn_id, fd_clock_now( ctx->clock ) );
654 0 : }
655 :
656 : static void
657 : gui_ws_close( ulong conn_id,
658 : int reason,
659 0 : void * _ctx ) {
660 0 : (void) reason;
661 0 : fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
662 0 : if( FD_UNLIKELY( ctx->is_full_client ) ) fd_gui_peers_ws_close( ctx->peers, conn_id );
663 0 : }
664 :
665 : static void
666 : gui_ws_message( ulong ws_conn_id,
667 : uchar const * data,
668 : ulong data_len,
669 0 : void * _ctx ) {
670 0 : fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
671 :
672 0 : int reason = fd_gui_ws_message( ctx->gui, ws_conn_id, data, data_len );
673 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 );
674 :
675 0 : if( FD_UNLIKELY( reason<0 ) ) fd_http_server_ws_close( ctx->gui_server, ws_conn_id, reason );
676 0 : }
677 :
678 : static void
679 : privileged_init( fd_topo_t * topo,
680 0 : fd_topo_tile_t * tile ) {
681 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
682 :
683 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
684 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
685 :
686 0 : fd_http_server_params_t http_param = derive_http_params( tile );
687 0 : fd_http_server_t * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
688 :
689 0 : fd_http_server_callbacks_t gui_callbacks = {
690 0 : .request = gui_http_request,
691 0 : .ws_open = gui_ws_open,
692 0 : .ws_close = gui_ws_close,
693 0 : .ws_message = gui_ws_message,
694 0 : };
695 0 : ctx->gui_server = fd_http_server_join( fd_http_server_new( _gui, http_param, gui_callbacks, ctx ) );
696 0 : fd_http_server_listen( ctx->gui_server, tile->gui.listen_addr, tile->gui.listen_port );
697 :
698 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 ));
699 :
700 0 : if( FD_UNLIKELY( !strcmp( tile->gui.identity_key_path, "" ) ) )
701 0 : FD_LOG_ERR(( "identity_key_path not set" ));
702 :
703 0 : ctx->identity_key = fd_keyload_load( tile->gui.identity_key_path, /* pubkey only: */ 1 );
704 :
705 0 : if( FD_UNLIKELY( !strcmp( tile->gui.vote_key_path, "" ) ) ) {
706 0 : ctx->has_vote_key = 0;
707 0 : } else {
708 0 : ctx->has_vote_key = 1;
709 0 : if( FD_UNLIKELY( !fd_base58_decode_32( tile->gui.vote_key_path, (uchar *)ctx->vote_key->uc ) ) ) {
710 0 : const uchar * vote_key = fd_keyload_load( tile->gui.vote_key_path, /* pubkey only: */ 1 );
711 0 : fd_memcpy( (uchar *)ctx->vote_key->uc, vote_key, 32UL );
712 0 : }
713 0 : }
714 0 : }
715 :
716 : extern char const fdctl_version_string[];
717 :
718 : static void
719 : unprivileged_init( fd_topo_t * topo,
720 0 : fd_topo_tile_t * tile ) {
721 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
722 :
723 0 : fd_topo_tile_t * gui_tile = &topo->tiles[ fd_topo_find_tile( topo, "gui", 0UL ) ];
724 0 : switch( gui_tile->gui.frontend_release_channel ) {
725 0 : case 0: STATIC_FILES = STATIC_FILES_STABLE; break;
726 0 : case 1: STATIC_FILES = STATIC_FILES_ALPHA; break;
727 0 : case 2: STATIC_FILES = STATIC_FILES_DEV; break;
728 0 : default: FD_LOG_CRIT(( "invalid frontend_release_channel %d", gui_tile->gui.frontend_release_channel ));
729 0 : }
730 :
731 0 : fd_http_server_params_t http_param = derive_http_params( tile );
732 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
733 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
734 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_param ) );
735 0 : void * _peers = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_peers_align(), fd_gui_peers_footprint( http_param.max_ws_connection_cnt) );
736 0 : void * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_align(), fd_gui_footprint( tile->gui.tile_cnt ) );
737 0 : void * _alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
738 :
739 0 : ctx->is_full_client = ULONG_MAX!=fd_topo_find_tile( topo, "repair", 0UL );
740 0 : ctx->snapshots_enabled = ULONG_MAX!=fd_topo_find_tile( topo, "snapct", 0UL );
741 :
742 0 : fd_clock_default_init( ctx->clock, ctx->clock_mem );
743 0 : ctx->recal_next = fd_clock_recal_next( ctx->clock );
744 :
745 0 : ctx->ref_wallclock = fd_log_wallclock();
746 0 : ctx->ref_tickcount = fd_tickcount();
747 0 : *(double *)&ctx->tick_per_ns = fd_tempo_tick_per_ns( NULL );
748 :
749 0 : FD_TEST( fd_cstr_printf_check( ctx->version_string, sizeof( ctx->version_string ), NULL, "%s", fdctl_version_string ) );
750 :
751 0 : ctx->topo = topo;
752 0 : ctx->peers = fd_gui_peers_join( fd_gui_peers_new( _peers, ctx->gui_server, ctx->topo, http_param.max_ws_connection_cnt, tile->gui.wfs_bank_hash, fd_clock_now( ctx->clock ) ) );
753 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, tile->gui.wfs_bank_hash, tile->gui.expected_shred_version, ctx->topo, fd_clock_now( ctx->clock ) ) );
754 0 : FD_TEST( ctx->gui );
755 :
756 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
757 0 : FD_TEST( ctx->keyswitch );
758 :
759 0 : fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
760 0 : FD_TEST( alloc );
761 0 : cJSON_alloc_install( alloc );
762 :
763 0 : ctx->next_poll_deadline = fd_tickcount();
764 :
765 0 : ctx->idle_cnt = 0UL;
766 0 : ctx->in_cnt = tile->in_cnt;
767 :
768 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
769 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
770 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
771 :
772 0 : if( FD_LIKELY( !strcmp( link->name, "plugin_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_PLUGIN;
773 0 : else if( FD_LIKELY( !strcmp( link->name, "poh_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH_PACK; /* full client only */
774 0 : else if( FD_LIKELY( !strcmp( link->name, "pohh_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH_PACK; /* frank only */
775 0 : else if( FD_LIKELY( !strcmp( link->name, "pack_bank" ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_EXECLE;
776 0 : else if( FD_LIKELY( !strcmp( link->name, "pack_execle" ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_EXECLE;
777 0 : else if( FD_LIKELY( !strcmp( link->name, "pack_poh" ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_POH; /* full client only */
778 0 : else if( FD_LIKELY( !strcmp( link->name, "pack_pohh" ) ) ) ctx->in_kind[ i ] = IN_KIND_PACK_POH; /* frank only */
779 0 : else if( FD_LIKELY( !strcmp( link->name, "execle_poh" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE_POH; /* full client only */
780 0 : else if( FD_LIKELY( !strcmp( link->name, "bank_pohh" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE_POH; /* frank only */
781 0 : else if( FD_LIKELY( !strcmp( link->name, "shred_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_SHRED_OUT; /* full client only */
782 0 : else if( FD_LIKELY( !strcmp( link->name, "net_gossvf" ) ) ) {
783 0 : ctx->in_kind[ i ] = IN_KIND_NET_GOSSVF;
784 0 : fd_net_rx_bounds_init( &ctx->net_in_bounds[ i ], link->dcache );
785 0 : }
786 0 : else if( FD_LIKELY( !strcmp( link->name, "gossip_net" ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_NET; /* full client only */
787 0 : else if( FD_LIKELY( !strcmp( link->name, "gossip_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_OUT; /* full client only */
788 0 : else if( FD_LIKELY( !strcmp( link->name, "snapct_gui" ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPCT; /* full client only */
789 0 : else if( FD_LIKELY( !strcmp( link->name, "repair_net" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR_NET; /* full client only */
790 0 : else if( FD_LIKELY( !strcmp( link->name, "tower_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_TOWER_OUT; /* full client only */
791 0 : else if( FD_LIKELY( !strcmp( link->name, "replay_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY_OUT; /* full client only */
792 0 : else if( FD_LIKELY( !strcmp( link->name, "replay_epoch" ) ) ) ctx->in_kind[ i ] = IN_KIND_EPOCH; /* full client only */
793 0 : else if( FD_LIKELY( !strcmp( link->name, "genesi_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_GENESI_OUT; /* full client only */
794 0 : else if( FD_LIKELY( !strcmp( link->name, "snapin_gui" ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPIN; /* full client only */
795 0 : else if( FD_LIKELY( !strcmp( link->name, "snapin_manif" ) ) ) ctx->in_kind[ i ] = IN_KIND_SNAPIN_MANIF; /* full client only */
796 0 : else if( FD_LIKELY( !strcmp( link->name, "execrp_replay" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECRP_REPLAY; /* full client only */
797 0 : else if( FD_LIKELY( !strcmp( link->name, "bundle_status" ) ) ) ctx->in_kind[ i ] = IN_KIND_BUNDLE; /* full client only */
798 0 : else FD_LOG_ERR(( "gui tile has unexpected input link %lu %s", i, link->name ));
799 :
800 0 : if( FD_LIKELY( !strcmp( link->name, "bank_pohh" ) || !strcmp( link->name, "execle_poh" ) ) ) {
801 0 : ulong producer = fd_topo_find_link_producer( topo, &topo->links[ tile->in_link_id[ i ] ] );
802 0 : ctx->in_bank_idx[ i ] = topo->tiles[ producer ].kind_id;
803 0 : }
804 :
805 0 : ctx->in_reliable[ i ] = tile->in_link_reliable[ i ];
806 0 : ctx->in[ i ].mem = link_wksp->wksp;
807 0 : ctx->in[ i ].mtu = link->mtu;
808 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
809 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
810 0 : }
811 :
812 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
813 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
814 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
815 0 : }
816 :
817 : static ulong
818 : populate_allowed_seccomp( fd_topo_t const * topo,
819 : fd_topo_tile_t const * tile,
820 : ulong out_cnt,
821 0 : struct sock_filter * out ) {
822 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
823 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
824 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
825 :
826 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 ) );
827 0 : return sock_filter_policy_fd_gui_tile_instr_cnt;
828 0 : }
829 :
830 : static ulong
831 : populate_allowed_fds( fd_topo_t const * topo,
832 : fd_topo_tile_t const * tile,
833 : ulong out_fds_cnt,
834 0 : int * out_fds ) {
835 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
836 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
837 0 : fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
838 :
839 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
840 :
841 0 : ulong out_cnt = 0UL;
842 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
843 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
844 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
845 0 : out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->gui_server ); /* gui listen socket */
846 0 : return out_cnt;
847 0 : }
848 :
849 : static ulong
850 : rlimit_file_cnt( fd_topo_t const * topo FD_PARAM_UNUSED,
851 0 : fd_topo_tile_t const * tile ) {
852 : /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
853 0 : ulong base = 5UL;
854 0 : return base + tile->gui.max_http_connections + tile->gui.max_websocket_connections;
855 0 : }
856 :
857 0 : #define STEM_BURST (2UL)
858 :
859 : /* See explanation in fd_pack */
860 0 : #define STEM_LAZY (128L*3000L)
861 :
862 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_gui_ctx_t
863 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gui_ctx_t)
864 :
865 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
866 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
867 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
868 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
869 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
870 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
871 :
872 : #include "../../disco/stem/fd_stem.c"
873 :
874 : fd_topo_run_tile_t fd_tile_gui = {
875 : .name = "gui",
876 : .rlimit_file_cnt_fn = rlimit_file_cnt,
877 : .populate_allowed_seccomp = populate_allowed_seccomp,
878 : .populate_allowed_fds = populate_allowed_fds,
879 : .scratch_align = scratch_align,
880 : .scratch_footprint = scratch_footprint,
881 : .loose_footprint = loose_footprint,
882 : .privileged_init = privileged_init,
883 : .unprivileged_init = unprivileged_init,
884 : .run = stem_run,
885 : };
|