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