Line data Source code
1 : #include "../replay/fd_replay_tile.h"
2 : #include "../genesis/fd_genesi_tile.h"
3 : #include "../votor/fd_votor_tile.h"
4 : #include "../../choreo/votor/fd_cert.h"
5 :
6 : #include "../../ballet/base64/fd_base64.h"
7 : #include "../../ballet/json/fd_jtok.h"
8 : #include "../../disco/topo/fd_topo.h"
9 : #include "../../disco/topo/fd_dns_resolve.h"
10 : #include "../../util/pod/fd_pod_format.h"
11 : #include "../../disco/fd_clock_tile.h"
12 : #include "../../disco/metrics/fd_metrics.h"
13 : #include "../../disco/keyguard/fd_keyload.h"
14 : #include "../../disco/keyguard/fd_keyswitch.h"
15 : #include "../../disco/waker/fd_waker.h"
16 : #include "../../disco/metrics/fd_metrics.h"
17 : #include "../../flamenco/features/fd_features.h"
18 : #include "../../flamenco/leaders/fd_multi_epoch_leaders.h"
19 : #include "../../flamenco/runtime/sysvar/fd_sysvar_rent.h"
20 : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
21 : #include "../../flamenco/runtime/fd_runtime_const.h"
22 : #include "../../flamenco/accdb/fd_accdb.h"
23 : #include "../../flamenco/alpenglow/fd_alpenglow.h"
24 : #include "../../flamenco/accdb/fd_accdb_shmem.h"
25 : #include "../../tango/fseq/fd_fseq.h"
26 : #include "../../flamenco/gossip/fd_gossip_message.h"
27 : #include "../../util/net/fd_ip4.h"
28 : #include "../../waltz/http/fd_http_server.h"
29 : #include "../../waltz/http/fd_http_server_private.h"
30 : #include "../../waltz/http/fd_url.h"
31 :
32 : #include <stddef.h>
33 : #include <poll.h>
34 : #include <sys/socket.h>
35 : #include <string.h>
36 :
37 : #define ZSTD_STATIC_LINKING_ONLY
38 : #include <zstd.h>
39 :
40 : #include "../../util/archive/fd_tar.h"
41 : #include "../../third_party/bzip2/bzlib.h"
42 :
43 : #include <linux/futex.h>
44 : #include "generated/fd_rpc_tile_seccomp.h"
45 :
46 0 : #define FD_RPC_AGAVE_API_VERSION "4.4.0"
47 :
48 0 : #define FD_HTTP_SERVER_RPC_MAX_REQUEST_LEN 8192UL
49 :
50 : /* FD_RPC_ID_MAX_SZ is the longest JSON-RPC request id (as raw JSON
51 : text) that is echoed back in responses. */
52 : #define FD_RPC_ID_MAX_SZ (256UL)
53 0 : #define FD_HTTP_SERVER_RPC_MAX_WS_SEND_FRAME_CNT 128UL
54 :
55 0 : #define IN_KIND_REPLAY (0)
56 0 : #define IN_KIND_GENESI (1)
57 0 : #define IN_KIND_GOSSIP_OUT (2)
58 0 : #define IN_KIND_EPOCH (5)
59 0 : #define IN_KIND_VOTOR (6)
60 :
61 : /* From bzip2 docs:
62 : To guarantee that the compressed data will fit in its buffer,
63 : allocate an output buffer of size 1% larger than the uncompressed
64 : data, plus six hundred extra bytes.
65 : */
66 : FD_FN_CONST static inline ulong
67 0 : fd_rpc_genesis_tar_max_sz( ulong max_message_size ) {
68 0 : return max_message_size + 4UL*FD_TAR_BLOCK_SZ;
69 0 : }
70 :
71 : FD_FN_CONST static inline ulong
72 0 : fd_rpc_genesis_tar_bz_max_sz( ulong max_message_size ) {
73 0 : ulong tar_max_sz = fd_rpc_genesis_tar_max_sz( max_message_size );
74 0 : return tar_max_sz + ((tar_max_sz + 100UL - 1UL) / 100UL) + 600UL;
75 0 : }
76 :
77 0 : #define FD_RPC_BASE58_ENCODED_128_LEN (175UL) /* ceil(128*log58(256)) */
78 0 : #define FD_RPC_ZSTD_LEVEL 1
79 : /* levels 4-15 are compiled out of libfd_zstd (third_party/zstd/Local.mk) */
80 : FD_STATIC_ASSERT( FD_RPC_ZSTD_LEVEL<=3 || FD_RPC_ZSTD_LEVEL>=16, zstd_level );
81 :
82 : #define FD_RPC_COMMITMENT_PROCESSED (0)
83 : #define FD_RPC_COMMITMENT_CONFIRMED (1)
84 : #define FD_RPC_COMMITMENT_FINALIZED (2)
85 :
86 : #define FD_RPC_ENCODING_BASE58 (0)
87 : #define FD_RPC_ENCODING_BASE64 (1)
88 : #define FD_RPC_ENCODING_BASE64_ZSTD (2)
89 : #define FD_RPC_ENCODING_BINARY (3)
90 : #define FD_RPC_ENCODING_JSON_PARSED (4)
91 :
92 0 : #define FD_RPC_HEALTH_STATUS_OK (0)
93 0 : #define FD_RPC_HEALTH_STATUS_BEHIND (1)
94 0 : #define FD_RPC_HEALTH_STATUS_UNKNOWN (2)
95 :
96 : /* Matches Agave's default health_check_slot_distance. */
97 : #define FD_RPC_HEALTH_CHECK_SLOT_DISTANCE (128UL)
98 :
99 : #define FD_RPC_METHOD_GET_ACCOUNT_INFO ( 0)
100 : #define FD_RPC_METHOD_GET_BALANCE ( 1)
101 : #define FD_RPC_METHOD_GET_BLOCK ( 2)
102 : #define FD_RPC_METHOD_GET_BLOCK_COMMITMENT ( 3)
103 : #define FD_RPC_METHOD_GET_BLOCK_HEIGHT ( 4)
104 : #define FD_RPC_METHOD_GET_BLOCK_PRODUCTION ( 5)
105 : #define FD_RPC_METHOD_GET_BLOCKS ( 6)
106 : #define FD_RPC_METHOD_GET_BLOCKS_WITH_LIMIT ( 7)
107 : #define FD_RPC_METHOD_GET_BLOCK_TIME ( 8)
108 : #define FD_RPC_METHOD_GET_CLUSTER_NODES ( 9)
109 : #define FD_RPC_METHOD_GET_EPOCH_INFO (10)
110 : #define FD_RPC_METHOD_GET_EPOCH_SCHEDULE (11)
111 : #define FD_RPC_METHOD_GET_FEE_FOR_MESSAGE (12)
112 : #define FD_RPC_METHOD_GET_FIRST_AVAILABLE_BLOCK (13)
113 : #define FD_RPC_METHOD_GET_GENESIS_HASH (14)
114 : #define FD_RPC_METHOD_GET_HEALTH (15)
115 : #define FD_RPC_METHOD_GET_HIGHEST_SNAPSHOT_SLOT (16)
116 : #define FD_RPC_METHOD_GET_IDENTITY (17)
117 : #define FD_RPC_METHOD_GET_INFLATION_GOVERNOR (18)
118 : #define FD_RPC_METHOD_GET_INFLATION_RATE (19)
119 : #define FD_RPC_METHOD_GET_INFLATION_REWARD (20)
120 : #define FD_RPC_METHOD_GET_LARGEST_ACCOUNTS (21)
121 : #define FD_RPC_METHOD_GET_LATEST_BLOCKHASH (22)
122 : #define FD_RPC_METHOD_GET_LEADER_SCHEDULE (23)
123 : #define FD_RPC_METHOD_GET_MAX_RETRANSMIT_SLOT (24)
124 : #define FD_RPC_METHOD_GET_MAX_SHRED_INSERT_SLOT (25)
125 : #define FD_RPC_METHOD_GET_MINIMUM_BALANCE_FOR_RENT_EXEMPTION (26)
126 : #define FD_RPC_METHOD_GET_MULTIPLE_ACCOUNTS (27)
127 : #define FD_RPC_METHOD_GET_PROGRAM_ACCOUNTS (28)
128 : #define FD_RPC_METHOD_GET_RECENT_PERFORMANCE_SAMPLES (29)
129 : #define FD_RPC_METHOD_GET_RECENT_PRIORITIZATION_FEES (30)
130 : #define FD_RPC_METHOD_GET_SIGNATURES_FOR_ADDRESS (31)
131 : #define FD_RPC_METHOD_GET_SIGNATURE_STATUSES (32)
132 : #define FD_RPC_METHOD_GET_SLOT (33)
133 : #define FD_RPC_METHOD_GET_SLOT_LEADER (34)
134 : #define FD_RPC_METHOD_GET_SLOT_LEADERS (35)
135 : #define FD_RPC_METHOD_GET_STAKE_MINIMUM_DELEGATION (36)
136 : #define FD_RPC_METHOD_GET_SUPPLY (37)
137 : #define FD_RPC_METHOD_GET_TOKEN_ACCOUNT_BALANCE (38)
138 : #define FD_RPC_METHOD_GET_TOKEN_ACCOUNTS_BY_DELEGATE (39)
139 : #define FD_RPC_METHOD_GET_TOKEN_ACCOUNTS_BY_OWNER (40)
140 : #define FD_RPC_METHOD_GET_TOKEN_LARGEST_ACCOUNTS (41)
141 : #define FD_RPC_METHOD_GET_TOKEN_SUPPLY (42)
142 : #define FD_RPC_METHOD_GET_TRANSACTION (43)
143 : #define FD_RPC_METHOD_GET_TRANSACTION_COUNT (44)
144 : #define FD_RPC_METHOD_GET_VERSION (45)
145 : #define FD_RPC_METHOD_GET_VOTE_ACCOUNTS (46)
146 : #define FD_RPC_METHOD_IS_BLOCKHASH_VALID (47)
147 : #define FD_RPC_METHOD_MINIMUM_LEDGER_SLOT (48)
148 : #define FD_RPC_METHOD_REQUEST_AIRDROP (49)
149 : #define FD_RPC_METHOD_SEND_TRANSACTION (50)
150 : #define FD_RPC_METHOD_SIMULATE_TRANSACTION (51)
151 :
152 : // Keep in sync with https://github.com/solana-labs/solana-web3.js/blob/master/src/errors.ts
153 : // and https://github.com/anza-xyz/agave/blob/master/rpc-client-api/src/custom_error.rs
154 : #define FD_RPC_ERROR_BLOCK_CLEANED_UP (-32001)
155 : #define FD_RPC_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE (-32002)
156 : #define FD_RPC_ERROR_TRANSACTION_SIGNATURE_VERIFICATION_FAILURE (-32003)
157 : #define FD_RPC_ERROR_BLOCK_NOT_AVAILABLE (-32004)
158 : #define FD_RPC_ERROR_NODE_UNHEALTHY (-32005)
159 : #define FD_RPC_ERROR_TRANSACTION_PRECOMPILE_VERIFICATION_FAILURE (-32006)
160 : #define FD_RPC_ERROR_SLOT_SKIPPED (-32007)
161 : #define FD_RPC_ERROR_NO_SNAPSHOT (-32008)
162 : #define FD_RPC_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED (-32009)
163 : #define FD_RPC_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX (-32010)
164 : #define FD_RPC_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE (-32011)
165 : #define FD_RPC_ROR (-32012)
166 : #define FD_RPC_ERROR_TRANSACTION_SIGNATURE_LEN_MISMATCH (-32013)
167 : #define FD_RPC_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET (-32014)
168 : #define FD_RPC_ERROR_UNSUPPORTED_TRANSACTION_VERSION (-32015)
169 : #define FD_RPC_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED (-32016)
170 : #define FD_RPC_ERROR_EPOCH_REWARDS_PERIOD_ACTIVE (-32017)
171 : #define FD_RPC_ERROR_SLOT_NOT_EPOCH_BOUNDARY (-32018)
172 : #define FD_RPC_ERROR_LONG_TERM_STORAGE_UNREACHABLE (-32019)
173 :
174 : /* fd_rpc_val_t is a JSON value captured from a request as a raw input
175 : slice plus its kind. Kinds mirror the serde names Agave uses in its
176 : error messages. INT is a number literal that fits in a ulong or a
177 : long, any other number literal is FLOAT. NONE denotes an absent
178 : value (missing member or array element). */
179 :
180 0 : #define FD_RPC_VAL_NONE (0)
181 0 : #define FD_RPC_VAL_STR (1)
182 0 : #define FD_RPC_VAL_MAP (2)
183 0 : #define FD_RPC_VAL_SEQ (3)
184 0 : #define FD_RPC_VAL_BOOL (4)
185 0 : #define FD_RPC_VAL_FLOAT (5)
186 0 : #define FD_RPC_VAL_INT (6)
187 0 : #define FD_RPC_VAL_NULL (7)
188 :
189 : struct fd_rpc_val {
190 : int kind;
191 : char const * raw;
192 : ulong raw_sz;
193 : fd_jtok_str_t str; /* STR only: view of the contents between the quotes */
194 : int neg; /* INT only: value is negative */
195 : ulong u; /* INT only: value if !neg */
196 : };
197 :
198 : typedef struct fd_rpc_val fd_rpc_val_t;
199 :
200 : static void
201 : fd_rpc_val_read( fd_jtok_t * j,
202 0 : fd_rpc_val_t * v ) {
203 0 : *v = (fd_rpc_val_t){ .kind = FD_RPC_VAL_NONE };
204 0 : int kind = fd_jtok_peek( j );
205 0 : char const * raw;
206 0 : ulong raw_sz;
207 0 : fd_jtok_raw( j, &raw, &raw_sz );
208 0 : if( FD_UNLIKELY( fd_jtok_err( j ) ) ) return;
209 0 : v->raw = raw;
210 0 : v->raw_sz = raw_sz;
211 0 : switch( kind ) {
212 0 : case FD_JTOK_OBJ: v->kind = FD_RPC_VAL_MAP; break;
213 0 : case FD_JTOK_ARR: v->kind = FD_RPC_VAL_SEQ; break;
214 0 : case FD_JTOK_BOOL: v->kind = FD_RPC_VAL_BOOL; break;
215 0 : case FD_JTOK_NULL: v->kind = FD_RPC_VAL_NULL; break;
216 0 : case FD_JTOK_NUM: v->kind = FD_RPC_VAL_FLOAT; break;
217 0 : case FD_JTOK_STR:
218 0 : v->kind = FD_RPC_VAL_STR;
219 0 : v->str = (fd_jtok_str_t){ .ptr = raw+1, .sz = raw_sz-2UL }; /* raw is the token including both quotes */
220 0 : break;
221 0 : case FD_JTOK_INT: {
222 0 : fd_jtok_t n[1];
223 0 : ulong u;
224 0 : fd_jtok_ulong( fd_jtok_init( n, raw, raw_sz ), &u );
225 0 : if( FD_LIKELY( !fd_jtok_err( n ) ) ) { v->kind = FD_RPC_VAL_INT; v->u = u; break; }
226 0 : long l;
227 0 : fd_jtok_long( fd_jtok_init( n, raw, raw_sz ), &l );
228 0 : if( FD_LIKELY( !fd_jtok_err( n ) ) ) { v->kind = FD_RPC_VAL_INT; v->neg = l<0L; v->u = l<0L ? 0UL : (ulong)l; break; }
229 0 : v->kind = FD_RPC_VAL_FLOAT;
230 0 : break;
231 0 : }
232 0 : default: break;
233 0 : }
234 0 : }
235 :
236 : /* fd_rpc_val_cstr decodes a STR value into the out_sz byte buffer at
237 : out. Returns 1 on success, 0 if v is not a string or does not fit
238 : (out is then an empty string). */
239 :
240 : static int
241 : fd_rpc_val_cstr( fd_rpc_val_t const * v,
242 : char * out,
243 0 : ulong out_sz ) {
244 0 : out[0] = '\0';
245 0 : if( v->kind!=FD_RPC_VAL_STR ) return 0;
246 0 : fd_jtok_t j[1];
247 0 : fd_jtok_cstr( fd_jtok_init( j, v->raw, v->raw_sz ), out, out_sz );
248 0 : return !fd_jtok_err( j );
249 0 : }
250 :
251 : /* fd_rpc_params_t is the "params" member of a request. kind is the
252 : kind of the params value itself. For a SEQ, cnt is the element
253 : count and v holds the first FD_RPC_PARAM_MAX elements (NONE beyond
254 : cnt). */
255 :
256 0 : #define FD_RPC_PARAM_MAX (2UL)
257 :
258 : struct fd_rpc_params {
259 : int kind;
260 : ulong cnt;
261 : fd_rpc_val_t v[ FD_RPC_PARAM_MAX ];
262 : };
263 :
264 : typedef struct fd_rpc_params fd_rpc_params_t;
265 :
266 : static void
267 : fd_rpc_params_read( fd_rpc_val_t const * params,
268 0 : fd_rpc_params_t * out ) {
269 0 : out->kind = params->kind;
270 0 : out->cnt = 0UL;
271 0 : for( ulong i=0UL; i<FD_RPC_PARAM_MAX; i++ ) out->v[ i ].kind = FD_RPC_VAL_NONE;
272 0 : if( params->kind!=FD_RPC_VAL_SEQ ) return;
273 0 : fd_jtok_t j[1];
274 0 : fd_jtok_arr_enter( fd_jtok_init( j, params->raw, params->raw_sz ) );
275 0 : while( fd_jtok_arr_next( j ) ) {
276 0 : if( out->cnt<FD_RPC_PARAM_MAX ) fd_rpc_val_read( j, &out->v[ out->cnt ] );
277 0 : out->cnt++;
278 0 : }
279 0 : }
280 :
281 : /* fd_rpc_config_t holds the members of a config object that any
282 : handler looks at. For duplicate members the first occurrence wins,
283 : unknown members are skipped. */
284 :
285 : struct fd_rpc_config {
286 : fd_rpc_val_t commitment;
287 : fd_rpc_val_t encoding;
288 : fd_rpc_val_t data_slice;
289 : fd_rpc_val_t min_context_slot;
290 : fd_rpc_val_t identity;
291 : };
292 :
293 : typedef struct fd_rpc_config fd_rpc_config_t;
294 :
295 : static void
296 : fd_rpc_config_read( fd_rpc_val_t const * config,
297 0 : fd_rpc_config_t * out ) {
298 0 : out->commitment.kind = FD_RPC_VAL_NONE;
299 0 : out->encoding.kind = FD_RPC_VAL_NONE;
300 0 : out->data_slice.kind = FD_RPC_VAL_NONE;
301 0 : out->min_context_slot.kind = FD_RPC_VAL_NONE;
302 0 : out->identity.kind = FD_RPC_VAL_NONE;
303 0 : if( config->kind!=FD_RPC_VAL_MAP ) return;
304 0 : fd_jtok_t j[1];
305 0 : fd_jtok_str_t key;
306 0 : fd_jtok_obj_enter( fd_jtok_init( j, config->raw, config->raw_sz ) );
307 0 : while( fd_jtok_obj_next( j, &key ) ) {
308 0 : fd_rpc_val_t * member;
309 0 : if( fd_jtok_str_eq( &key, "commitment" ) ) member = &out->commitment;
310 0 : else if( fd_jtok_str_eq( &key, "encoding" ) ) member = &out->encoding;
311 0 : else if( fd_jtok_str_eq( &key, "dataSlice" ) ) member = &out->data_slice;
312 0 : else if( fd_jtok_str_eq( &key, "minContextSlot" ) ) member = &out->min_context_slot;
313 0 : else if( fd_jtok_str_eq( &key, "identity" ) ) member = &out->identity;
314 0 : else continue;
315 0 : if( member->kind==FD_RPC_VAL_NONE ) fd_rpc_val_read( j, member );
316 0 : }
317 0 : }
318 :
319 : static fd_http_server_params_t
320 0 : derive_http_params( fd_topo_tile_t const * tile ) {
321 0 : return (fd_http_server_params_t) {
322 0 : .max_connection_cnt = tile->rpc.max_http_connections,
323 0 : .max_ws_connection_cnt = tile->rpc.max_websocket_connections,
324 0 : .max_request_len = FD_HTTP_SERVER_RPC_MAX_REQUEST_LEN,
325 0 : .max_ws_recv_frame_len = FD_HTTP_SERVER_RPC_MAX_REQUEST_LEN,
326 0 : .max_ws_send_frame_cnt = FD_HTTP_SERVER_RPC_MAX_WS_SEND_FRAME_CNT,
327 0 : .outgoing_buffer_sz = tile->rpc.send_buffer_size_mb * (1UL<<20UL),
328 0 : .compress_websocket = 0,
329 0 : };
330 0 : }
331 :
332 : struct fd_rpc_in {
333 : fd_wksp_t * mem;
334 : ulong chunk0;
335 : ulong wmark;
336 : ulong mtu;
337 : };
338 :
339 : typedef struct fd_rpc_in fd_rpc_in_t;
340 :
341 : struct fd_rpc_out {
342 : ulong idx;
343 : fd_wksp_t * mem;
344 : ulong chunk0;
345 : ulong wmark;
346 : ulong chunk;
347 : };
348 :
349 : typedef struct fd_rpc_out fd_rpc_out_t;
350 :
351 : struct bank_info {
352 : ulong slot; /* default ULONG_MAX */
353 : ulong bank_idx;
354 : fd_accdb_fork_id_t accdb_fork_id;
355 : ulong epoch;
356 : ulong slot_in_epoch;
357 : ulong slots_per_epoch;
358 :
359 : ulong transaction_count;
360 : uchar block_hash[ 32 ];
361 : ulong block_height;
362 :
363 : struct {
364 : double initial;
365 : double terminal;
366 : double taper;
367 : double foundation;
368 : double foundation_term;
369 : } inflation;
370 :
371 : struct {
372 : ulong lamports_per_uint8_year;
373 : double exemption_threshold;
374 : uchar burn_percent;
375 : } rent;
376 : };
377 :
378 : typedef struct bank_info bank_info_t;
379 :
380 : struct fd_rpc_cluster_node {
381 : int valid;
382 : fd_pubkey_t identity;
383 : fd_gossip_contact_info_t ci[ 1 ];
384 :
385 : struct { ulong prev, next; } dlist;
386 : };
387 :
388 : typedef struct fd_rpc_cluster_node fd_rpc_cluster_node_t;
389 :
390 : #define DLIST_NAME fd_rpc_cluster_node_dlist
391 : #define DLIST_ELE_T fd_rpc_cluster_node_t
392 0 : #define DLIST_PREV dlist.prev
393 0 : #define DLIST_NEXT dlist.next
394 : #include "../../util/tmpl/fd_dlist.c"
395 :
396 : /* fd_rpc_gls_pair_t associates a leader-schedule rotation index with the
397 : identity pubkey that leads it, used to group getLeaderSchedule's
398 : result by identity (see the handler). */
399 : struct fd_rpc_gls_pair {
400 : fd_pubkey_t id_key; /* leader identity */
401 : uint rot; /* rotation index into the epoch's leader schedule */
402 : };
403 : typedef struct fd_rpc_gls_pair fd_rpc_gls_pair_t;
404 :
405 0 : #define FD_RPC_MLEADERS_CNT (5UL)
406 0 : #define FD_RPC_MLEADERS_EPOCH_CNT (FD_RPC_MLEADERS_CNT*MULTI_EPOCH_LEADERS_EPOCH_CNT)
407 :
408 : static inline void
409 0 : fd_rpc_mleaders_init( fd_multi_epoch_leaders_t * mleaders ) {
410 0 : for( ulong i=0UL; i<FD_RPC_MLEADERS_CNT; i++ ) FD_TEST( fd_multi_epoch_leaders_new( &mleaders[ i ] ) );
411 0 : }
412 :
413 : static inline void
414 : fd_rpc_mleaders_ingest( fd_multi_epoch_leaders_t * mleaders,
415 0 : fd_epoch_info_msg_t const * msg ) {
416 0 : ulong idx = (msg->epoch / MULTI_EPOCH_LEADERS_EPOCH_CNT) % FD_RPC_MLEADERS_CNT;
417 0 : fd_multi_epoch_leaders_epoch_msg_init( &mleaders[ idx ], msg );
418 0 : fd_multi_epoch_leaders_epoch_msg_fini( &mleaders[ idx ] );
419 0 : }
420 :
421 : FD_FN_PURE static inline fd_epoch_leaders_t const *
422 : fd_rpc_mleaders_get_lsched_for_slot( fd_multi_epoch_leaders_t const * mleaders,
423 0 : ulong slot ) {
424 0 : for( ulong i=0UL; i<FD_RPC_MLEADERS_CNT; i++ ) {
425 0 : fd_epoch_leaders_t const * lsched = fd_multi_epoch_leaders_get_lsched_for_slot( &mleaders[ i ], slot );
426 0 : if( FD_LIKELY( lsched ) ) return lsched;
427 0 : }
428 0 : return NULL;
429 0 : }
430 :
431 : FD_FN_PURE static inline fd_pubkey_t const *
432 : fd_rpc_mleaders_get_leader_for_slot( fd_multi_epoch_leaders_t const * mleaders,
433 0 : ulong slot ) {
434 0 : fd_epoch_leaders_t const * lsched = fd_rpc_mleaders_get_lsched_for_slot( mleaders, slot );
435 0 : if( FD_UNLIKELY( !lsched ) ) return NULL;
436 0 : return fd_epoch_leaders_get( lsched, slot );
437 0 : }
438 :
439 : struct fd_rpc_tile {
440 : int delay_startup;
441 : int waker_rearm_pending;
442 :
443 : fd_http_server_t * http;
444 :
445 : ulong * ws_subscribers_vote;
446 : ulong ws_subscribers_vote_cnt;
447 : ulong * ws_subscribers_slot;
448 : ulong ws_subscribers_slot_cnt;
449 :
450 : /* Identical subscriptions share one subscription id while any
451 : subscriber remains, and new subscriptions draw ids from a single
452 : incrementing counter. */
453 : ulong ws_sub_id_vote;
454 : ulong ws_sub_id_slot;
455 : ulong ws_sub_id_next;
456 :
457 : fd_rpc_cluster_node_dlist_t * cluster_nodes_dlist;
458 : fd_rpc_cluster_node_t cluster_nodes[ FD_CONTACT_INFO_TABLE_SIZE ];
459 :
460 : bank_info_t * banks;
461 : ulong max_live_slots;
462 :
463 : fd_accdb_t * accdb;
464 :
465 : ulong cluster_confirmed_slot;
466 :
467 : /* Highest slot relayed to turbine (Agave MaxSlots::retransmit),
468 : one fseq per shred tile, written by shred */
469 : ulong shred_rtx_cnt;
470 : ulong const * shred_rtx[ FD_TOPO_MAX_TILES ];
471 :
472 : /* Address of the account holding the alpenglow genesis certificate,
473 : for getAgGenesisCert. */
474 : fd_pubkey_t fd_genesis_cert_addr[ 1 ];
475 :
476 : ulong processed_idx;
477 : ulong confirmed_idx;
478 : ulong finalized_idx;
479 :
480 : int has_genesis_hash;
481 : fd_hash_t genesis_hash[ 1 ];
482 :
483 : ulong genesis_tar_bz_sz;
484 : ulong genesis_max_message_size;
485 : ulong genesis_tar_max_sz;
486 : ulong genesis_tar_bz_max_sz;
487 : uchar * genesis_tar;
488 : uchar * genesis_tar_bz;
489 :
490 : fd_alloc_t * bz2_alloc;
491 :
492 : fd_clock_tile_t clock[1];
493 : ulong in_cnt;
494 : ulong idle_cnt;
495 :
496 : ulong waker_client_idx;
497 : ulong * waker_fseq;
498 :
499 : fd_keyswitch_t * keyswitch;
500 : uchar identity_pubkey[ 32UL ];
501 :
502 : int in_kind[ 64UL ];
503 : fd_rpc_in_t in[ 64UL ];
504 :
505 : fd_rpc_out_t replay_out[1];
506 :
507 : fd_histf_t request_duration[ 1 ];
508 :
509 : fd_multi_epoch_leaders_t mleaders[ FD_RPC_MLEADERS_CNT ];
510 : fd_epoch_schedule_t epoch_schedule;
511 : int has_epoch_schedule;
512 :
513 : ZSTD_CCtx * zstd_cctx;
514 : uchar compress_buf[ ZSTD_COMPRESSBOUND( FD_RUNTIME_ACC_SZ_MAX ) ];
515 :
516 : /* Redirect to snapshot server */
517 : int snapshot_server_enabled;
518 : char snapshot_server_url[ FD_URL_MAX ];
519 :
520 : union {
521 : /* Scratch used by getLeaderSchedule to group the epoch's leader
522 : schedule by identity. */
523 : fd_rpc_gls_pair_t gls_pairs[ (MAX_SLOTS_PER_EPOCH + FD_EPOCH_SLOTS_PER_ROTATION - 1UL) / FD_EPOCH_SLOTS_PER_ROTATION ];
524 :
525 : /* Scratch buffer for fd_accdb_read_one_nocache: holds the account
526 : data bytes returned by the readonly accdb path. Sized to the
527 : runtime account data maximum. Must not be in accdb shmem. */
528 : uchar accdb_data_buf[ FD_RUNTIME_ACC_SZ_MAX ];
529 : } scratch;
530 : };
531 :
532 : typedef struct fd_rpc_tile fd_rpc_tile_t;
533 :
534 : static void
535 : fd_rpc_ws_subscriber_vote_add( fd_rpc_tile_t * ctx,
536 0 : ulong ws_conn_id ) {
537 0 : for( ulong i=0UL; i<ctx->ws_subscribers_vote_cnt; i++ )
538 0 : if( FD_UNLIKELY( ctx->ws_subscribers_vote[ i ]==ws_conn_id ) ) return;
539 :
540 0 : FD_TEST( ctx->ws_subscribers_vote_cnt<ctx->http->max_ws_conns );
541 0 : if( FD_UNLIKELY( !ctx->ws_subscribers_vote_cnt ) ) ctx->ws_sub_id_vote = ctx->ws_sub_id_next++;
542 0 : ctx->ws_subscribers_vote[ ctx->ws_subscribers_vote_cnt++ ] = ws_conn_id;
543 0 : }
544 :
545 : static int
546 : fd_rpc_ws_subscriber_vote_remove( fd_rpc_tile_t * ctx,
547 0 : ulong ws_conn_id ) {
548 0 : for( ulong idx=0UL; idx<ctx->ws_subscribers_vote_cnt; idx++ ) {
549 0 : if( FD_LIKELY( ctx->ws_subscribers_vote[ idx ]!=ws_conn_id ) ) continue;
550 :
551 0 : ctx->ws_subscribers_vote_cnt--;
552 0 : ctx->ws_subscribers_vote[ idx ] = ctx->ws_subscribers_vote[ ctx->ws_subscribers_vote_cnt ];
553 0 : return 1;
554 0 : }
555 :
556 0 : return 0;
557 0 : }
558 :
559 : static void
560 : fd_rpc_ws_subscriber_slot_add( fd_rpc_tile_t * ctx,
561 0 : ulong ws_conn_id ) {
562 0 : for( ulong i=0UL; i<ctx->ws_subscribers_slot_cnt; i++ )
563 0 : if( FD_UNLIKELY( ctx->ws_subscribers_slot[ i ]==ws_conn_id ) ) return;
564 :
565 0 : FD_TEST( ctx->ws_subscribers_slot_cnt<ctx->http->max_ws_conns );
566 0 : if( FD_UNLIKELY( !ctx->ws_subscribers_slot_cnt ) ) ctx->ws_sub_id_slot = ctx->ws_sub_id_next++;
567 0 : ctx->ws_subscribers_slot[ ctx->ws_subscribers_slot_cnt++ ] = ws_conn_id;
568 0 : }
569 :
570 : static int
571 : fd_rpc_ws_subscriber_slot_remove( fd_rpc_tile_t * ctx,
572 0 : ulong ws_conn_id ) {
573 0 : for( ulong idx=0UL; idx<ctx->ws_subscribers_slot_cnt; idx++ ) {
574 0 : if( FD_LIKELY( ctx->ws_subscribers_slot[ idx ]!=ws_conn_id ) ) continue;
575 :
576 0 : ctx->ws_subscribers_slot_cnt--;
577 0 : ctx->ws_subscribers_slot[ idx ] = ctx->ws_subscribers_slot[ ctx->ws_subscribers_slot_cnt ];
578 0 : return 1;
579 0 : }
580 :
581 0 : return 0;
582 0 : }
583 :
584 : /* fd_rpc_ws_unsubscribe removes the subscription with id sub_id from
585 : connection ws_conn_id, whatever its type. Returns 1 if a
586 : subscription was removed, 0 if the connection has no subscription
587 : with that id.
588 :
589 : Agave keeps all subscriptions of a connection in one map keyed by id
590 : and every *Unsubscribe method removes from that map without checking
591 : the subscription type. This is a bug in Agave, we replicate it for
592 : conformance. */
593 :
594 : static int
595 : fd_rpc_ws_unsubscribe( fd_rpc_tile_t * ctx,
596 : ulong sub_id,
597 0 : ulong ws_conn_id ) {
598 0 : if( sub_id==ctx->ws_sub_id_slot && fd_rpc_ws_subscriber_slot_remove( ctx, ws_conn_id ) ) return 1;
599 0 : if( sub_id==ctx->ws_sub_id_vote && fd_rpc_ws_subscriber_vote_remove( ctx, ws_conn_id ) ) return 1;
600 0 : return 0;
601 0 : }
602 :
603 : static void *
604 : bz2_malloc( void * opaque,
605 : int items,
606 0 : int size ) {
607 0 : fd_alloc_t * alloc = (fd_alloc_t *)opaque;
608 :
609 0 : if( FD_UNLIKELY( items<=0 || size<=0 ) ) {
610 0 : FD_LOG_WARNING(( "bz2_malloc: invalid items=%d or size=%d", items, size ));
611 0 : return NULL;
612 0 : }
613 0 : void * result = fd_alloc_malloc( alloc, alignof(max_align_t), (ulong)items*(ulong)size );
614 0 : if( FD_UNLIKELY( !result ) ) return NULL;
615 0 : return result;
616 0 : }
617 :
618 : static void
619 : bz2_free( void * opaque,
620 0 : void * addr ) {
621 0 : fd_alloc_t * alloc = (fd_alloc_t *)opaque;
622 :
623 0 : if( FD_UNLIKELY( !addr ) ) return;
624 0 : fd_alloc_free( alloc, addr );
625 0 : }
626 :
627 : static inline ulong
628 : fd_rpc_file_as_tarball( fd_rpc_tile_t * ctx,
629 : char const * filename_cstr,
630 : uchar const * data,
631 : ulong data_sz,
632 : uchar * scratch,
633 : ulong scratch_sz,
634 : uchar * out,
635 0 : ulong out_sz ) {
636 0 : ulong padding_sz = 2UL*FD_TAR_BLOCK_SZ;
637 0 : if( FD_LIKELY( data_sz % FD_TAR_BLOCK_SZ ) ) padding_sz += FD_TAR_BLOCK_SZ - (data_sz % FD_TAR_BLOCK_SZ);
638 :
639 0 : if( FD_UNLIKELY( data_sz>ctx->genesis_max_message_size ) ) {
640 0 : FD_LOG_ERR(( "Genesis data exceeds maximum size (data_sz=%lu max=%lu)", data_sz, ctx->genesis_max_message_size ));
641 0 : }
642 0 : ulong tar_sz = sizeof(fd_tar_meta_t) + data_sz + padding_sz;
643 0 : if( FD_UNLIKELY( tar_sz>scratch_sz ) ) {
644 0 : FD_LOG_WARNING(( "tar_sz exceeds scratch_sz (tar_sz=%lu scratch_sz=%lu)", tar_sz, scratch_sz ));
645 0 : return ULONG_MAX;
646 0 : }
647 :
648 0 : fd_tar_meta_init_file_default( (fd_tar_meta_t *)scratch, filename_cstr, data_sz, fd_log_wallclock() );
649 0 : fd_memcpy( scratch+sizeof(fd_tar_meta_t), data, data_sz );
650 0 : memset( scratch+sizeof(fd_tar_meta_t)+data_sz, 0, padding_sz );
651 :
652 : /* NOTE: Agave's genesis.tar also contains a `rocksdb` folder */
653 :
654 0 : bz_stream bzstrm = {0};
655 0 : bzstrm.bzalloc = bz2_malloc;
656 0 : bzstrm.bzfree = bz2_free;
657 0 : bzstrm.opaque = ctx->bz2_alloc;
658 0 : int bzerr = BZ2_bzCompressInit( &bzstrm, 1, 0, 0 );
659 0 : if( FD_UNLIKELY( BZ_OK!=bzerr ) ) FD_LOG_ERR(( "BZ2_bzCompressInit() failed (%d)", bzerr ));
660 :
661 0 : ulong tar_bz_sz = out_sz;
662 :
663 0 : bzstrm.next_in = (char *)scratch;
664 0 : bzstrm.avail_in = (uint)tar_sz;
665 0 : bzstrm.next_out = (char *)out;
666 0 : bzstrm.avail_out = (uint)tar_bz_sz;
667 :
668 0 : for(;;) {
669 0 : bzerr = BZ2_bzCompress( &bzstrm, BZ_FINISH );
670 0 : if( FD_LIKELY( bzerr==BZ_STREAM_END ) ) break;
671 0 : if( FD_UNLIKELY( bzerr>=0 ) ) continue;
672 0 : FD_LOG_ERR(( "BZ2_bzCompress(_, BZ_FINISH) failed (%d)", bzerr ));
673 0 : }
674 :
675 0 : tar_bz_sz -= (ulong)bzstrm.avail_out;
676 :
677 0 : bzerr = BZ2_bzCompressEnd( &bzstrm );
678 0 : if( FD_UNLIKELY( BZ_OK!=bzerr ) ) FD_LOG_ERR(( "BZ2_bzCompressEnd() failed (%d)", bzerr ));
679 :
680 0 : return tar_bz_sz;
681 0 : }
682 :
683 : FD_FN_CONST static inline ulong
684 0 : scratch_align( void ) {
685 0 : ulong a = alignof( fd_rpc_tile_t );
686 0 : a = fd_ulong_max( a, fd_http_server_align() );
687 0 : a = fd_ulong_max( a, fd_alloc_align() );
688 0 : a = fd_ulong_max( a, alignof(bank_info_t) );
689 0 : a = fd_ulong_max( a, fd_rpc_cluster_node_dlist_align() );
690 0 : a = fd_ulong_max( a, fd_accdb_align() );
691 0 : return a;
692 0 : }
693 :
694 : FD_FN_CONST static inline ulong
695 0 : loose_footprint( fd_topo_tile_t const * tile ) {
696 0 : (void)tile;
697 : /* Leftover space for bzip2 allocations */
698 0 : return 1UL<<26; /* 64 MiB */
699 0 : }
700 :
701 : static inline ulong
702 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
703 0 : fd_http_server_params_t http_params = derive_http_params( tile );
704 0 : ulong http_fp = fd_http_server_footprint( http_params );
705 0 : if( FD_UNLIKELY( !http_fp ) ) FD_LOG_ERR(( "Invalid [tiles.rpc] config parameters" ));
706 :
707 0 : ulong l = FD_LAYOUT_INIT;
708 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_rpc_tile_t), sizeof(fd_rpc_tile_t) );
709 0 : l = FD_LAYOUT_APPEND( l, fd_http_server_align(), http_fp );
710 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
711 0 : l = FD_LAYOUT_APPEND( l, alignof(bank_info_t), tile->rpc.max_live_slots*sizeof(bank_info_t) );
712 0 : l = FD_LAYOUT_APPEND( l, fd_rpc_cluster_node_dlist_align(), fd_rpc_cluster_node_dlist_footprint() );
713 0 : l = FD_LAYOUT_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->rpc.max_live_slots, 0 ) );
714 0 : l = FD_LAYOUT_APPEND( l, alignof(ulong), http_params.max_ws_connection_cnt*sizeof(ulong) );
715 0 : l = FD_LAYOUT_APPEND( l, alignof(ulong), http_params.max_ws_connection_cnt*sizeof(ulong) );
716 0 : l = FD_LAYOUT_APPEND( l, alignof(uchar), fd_rpc_genesis_tar_max_sz( tile->rpc.genesis_max_message_size ) );
717 0 : l = FD_LAYOUT_APPEND( l, alignof(uchar), fd_rpc_genesis_tar_bz_max_sz( tile->rpc.genesis_max_message_size ) );
718 0 : l = FD_LAYOUT_APPEND( l, 16UL, ZSTD_estimateCCtxSize( FD_RPC_ZSTD_LEVEL ) );
719 0 : return FD_LAYOUT_FINI( l, scratch_align() );
720 0 : }
721 :
722 : static inline void
723 0 : during_housekeeping( fd_rpc_tile_t * ctx ) {
724 0 : if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) {
725 0 : fd_clock_tile_recal( ctx->clock );
726 0 : }
727 :
728 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
729 0 : fd_memcpy( ctx->identity_pubkey, ctx->keyswitch->bytes, 32UL );
730 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
731 0 : }
732 0 : }
733 :
734 : static inline void
735 0 : metrics_write( fd_rpc_tile_t * ctx ) {
736 0 : FD_MHIST_COPY( RPC, REQUEST_DURATION_NANOS, ctx->request_duration );
737 0 : FD_MGAUGE_SET( RPC, CONN_OPEN, ctx->http->metrics.connection_cnt );
738 0 : FD_MGAUGE_SET( RPC, WEBSOCKET_CONN_OPEN, ctx->http->metrics.ws_connection_cnt );
739 0 : FD_MGAUGE_SET( RPC, WEBSOCKET_SUBSCRIBER_COUNT_VOTE, ctx->ws_subscribers_vote_cnt );
740 0 : FD_MGAUGE_SET( RPC, WEBSOCKET_SUBSCRIBER_COUNT_SLOT, ctx->ws_subscribers_slot_cnt );
741 0 : FD_ACCDB_METRICS_WRITE_RO( RPC, fd_accdb_metrics( ctx->accdb ) );
742 0 : }
743 :
744 : static void
745 : before_credit( fd_rpc_tile_t * ctx,
746 : fd_stem_context_t * stem,
747 0 : int * charge_busy ) {
748 0 : (void)stem;
749 :
750 0 : ctx->idle_cnt++;
751 0 : if( FD_LIKELY( ctx->idle_cnt<2UL*ctx->in_cnt ) ) return;
752 0 : ctx->idle_cnt = 0UL;
753 :
754 0 : int replay_ready = ctx->confirmed_idx!=ULONG_MAX && ctx->processed_idx!=ULONG_MAX && ctx->finalized_idx!=ULONG_MAX;
755 0 : if( FD_UNLIKELY( ctx->delay_startup && !replay_ready ) ) {
756 : /* Consume readiness so an early connection does not veto every
757 : park, but do not rearm: the listen fd stays readable and would
758 : refire on every pass. */
759 0 : if( FD_UNLIKELY( fd_fseq_query( ctx->waker_fseq )==1UL ) ) {
760 0 : fd_fseq_update( ctx->waker_fseq, 0UL );
761 0 : ctx->waker_rearm_pending = 1;
762 0 : }
763 0 : return;
764 0 : }
765 :
766 0 : if( FD_UNLIKELY( ctx->waker_rearm_pending ) ) {
767 0 : ctx->waker_rearm_pending = 0;
768 0 : fd_waker_client_rearm( ctx->waker_client_idx );
769 0 : }
770 :
771 0 : if( FD_UNLIKELY( fd_fseq_query( ctx->waker_fseq )==1UL ) ) {
772 0 : fd_fseq_update( ctx->waker_fseq, 0UL );
773 0 : *charge_busy = fd_http_server_epoll_poll( ctx->http, 1UL );
774 0 : fd_waker_client_rearm( ctx->waker_client_idx );
775 0 : }
776 0 : }
777 :
778 : static int
779 : before_frag( fd_rpc_tile_t * ctx,
780 : ulong in_idx,
781 : ulong seq FD_PARAM_UNUSED,
782 0 : ulong sig ) {
783 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP_OUT ) ) {
784 0 : return sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO &&
785 0 : sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE;
786 0 : }
787 :
788 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY ) ) {
789 0 : return sig!=REPLAY_SIG_SLOT_COMPLETED && sig!=REPLAY_SIG_OC_ADVANCED &&
790 0 : sig!=REPLAY_SIG_ROOT_ADVANCED && sig!=REPLAY_SIG_DROP_BANK_REF;
791 0 : }
792 :
793 0 : if( ctx->in_kind[ in_idx ]==IN_KIND_VOTOR ) return sig!=FD_VOTOR_SIG_CERTED;
794 :
795 0 : return 0;
796 0 : }
797 :
798 : static void
799 : fd_rpc_publish_slot_event( fd_rpc_tile_t * ctx,
800 0 : fd_replay_slot_completed_t const * slot_completed ) {
801 0 : if( FD_UNLIKELY( !ctx->ws_subscribers_slot_cnt ) ) return;
802 :
803 0 : ulong sent_cnt = 0UL;
804 0 : for( ulong i=0UL; i<ctx->ws_subscribers_slot_cnt; ) {
805 0 : ulong ws_conn_id = ctx->ws_subscribers_slot[ i ];
806 0 : fd_http_server_printf( ctx->http,
807 0 : "{\"jsonrpc\":\"2.0\",\"method\":\"slotNotification\",\"params\":{\"subscription\":%lu,\"result\":{\"parent\":%lu,\"root\":%lu,\"slot\":%lu}}}\n",
808 0 : ctx->ws_sub_id_slot,
809 0 : slot_completed->parent_slot,
810 0 : slot_completed->root_slot,
811 0 : slot_completed->slot );
812 :
813 0 : if( FD_UNLIKELY( fd_http_server_ws_send( ctx->http, ws_conn_id ) ) ) {
814 0 : fd_rpc_ws_subscriber_slot_remove( ctx, ws_conn_id );
815 0 : fd_http_server_ws_close( ctx->http, ws_conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_TOO_SLOW );
816 0 : continue;
817 0 : }
818 0 : if( FD_UNLIKELY( i>=ctx->ws_subscribers_slot_cnt || ctx->ws_subscribers_slot[ i ]!=ws_conn_id ) ) continue;
819 0 : sent_cnt++;
820 0 : i++;
821 0 : }
822 :
823 0 : FD_MCNT_INC( RPC, WEBSOCKET_NOTIFICATION_PUBLISHED_SLOT, !!sent_cnt );
824 0 : FD_MCNT_INC( RPC, WEBSOCKET_NOTIFICATION_TX_SLOT, sent_cnt );
825 0 : }
826 :
827 : static inline void
828 : fd_rpc_cluster_finalized( fd_rpc_tile_t * ctx,
829 0 : ulong slot ) {
830 0 : ctx->cluster_confirmed_slot = ctx->cluster_confirmed_slot==ULONG_MAX ? slot : fd_ulong_max( ctx->cluster_confirmed_slot, slot );
831 0 : }
832 :
833 : static inline int
834 : returnable_frag( fd_rpc_tile_t * ctx,
835 : ulong in_idx,
836 : ulong seq FD_PARAM_UNUSED,
837 : ulong sig,
838 : ulong chunk,
839 : ulong sz FD_PARAM_UNUSED,
840 : ulong ctl FD_PARAM_UNUSED,
841 : ulong tsorig FD_PARAM_UNUSED,
842 : ulong tspub FD_PARAM_UNUSED,
843 0 : fd_stem_context_t * stem ) {
844 :
845 0 : ctx->idle_cnt = 0UL;
846 :
847 0 : if( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY ) {
848 0 : switch( sig ) {
849 0 : case REPLAY_SIG_SLOT_COMPLETED: {
850 0 : fd_replay_slot_completed_t const * slot_completed = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
851 :
852 0 : FD_TEST( slot_completed->bank_idx<ctx->max_live_slots );
853 0 : bank_info_t * bank = &ctx->banks[ slot_completed->bank_idx ];
854 0 : bank->slot = slot_completed->slot;
855 0 : bank->accdb_fork_id = slot_completed->accdb_fork_id;
856 0 : bank->epoch = slot_completed->epoch;
857 0 : bank->slot_in_epoch = slot_completed->slot_in_epoch;
858 0 : bank->slots_per_epoch = slot_completed->slots_per_epoch;
859 0 : bank->transaction_count = slot_completed->transaction_count;
860 0 : bank->block_height = slot_completed->block_height;
861 0 : fd_memcpy( bank->block_hash, slot_completed->block_hash.uc, 32 );
862 :
863 0 : bank->inflation.initial = slot_completed->inflation.initial;
864 0 : bank->inflation.terminal = slot_completed->inflation.terminal;
865 0 : bank->inflation.taper = slot_completed->inflation.taper;
866 0 : bank->inflation.foundation = slot_completed->inflation.foundation;
867 0 : bank->inflation.foundation_term = slot_completed->inflation.foundation_term;
868 :
869 0 : bank->rent.lamports_per_uint8_year = slot_completed->rent.lamports_per_uint8_year;
870 0 : bank->rent.exemption_threshold = slot_completed->rent.exemption_threshold;
871 0 : bank->rent.burn_percent = slot_completed->rent.burn_percent;
872 :
873 0 : fd_rpc_publish_slot_event( ctx, slot_completed );
874 :
875 : /* In Agave, "processed" confirmation is the bank we've just
876 : voted for (handle_votable_bank), which is also guaranteed to
877 : have been replayed.
878 :
879 : Right now tower is not really built out to replicate this
880 : exactly, so we use the latest replayed slot, which is
881 : slightly more eager than Agave but shouldn't really affect
882 : end-users, since any use-cases that assume "processed" means
883 : "voted-for" would fail in Agave in cases where a cast vote
884 : does not land.
885 :
886 : Due to bank eviction semantics, it is possible that the RPC
887 : can return data about a slot that is not the most recently
888 : replayed slot (since slots can be re-replayed).
889 :
890 : tldr: This isn't strictly conformant with Agave, but doesn't
891 : need to be since Agave doesn't provide any guarantees
892 : anyways. */
893 0 : if( FD_LIKELY( ctx->processed_idx!=ULONG_MAX ) ) fd_stem_publish( stem, ctx->replay_out->idx, ctx->processed_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
894 0 : ctx->processed_idx = slot_completed->bank_idx;
895 0 : break;
896 0 : }
897 0 : case REPLAY_SIG_OC_ADVANCED: {
898 0 : fd_replay_oc_advanced_t const * msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
899 0 : if( FD_LIKELY( ctx->confirmed_idx!=ULONG_MAX ) ) fd_stem_publish( stem, ctx->replay_out->idx, ctx->confirmed_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
900 0 : FD_TEST( msg->bank_idx<ctx->max_live_slots );
901 0 : ctx->confirmed_idx = msg->bank_idx;
902 0 : break;
903 0 : }
904 0 : case REPLAY_SIG_ROOT_ADVANCED: {
905 0 : fd_replay_root_advanced_t const * msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
906 0 : if( FD_LIKELY( ctx->finalized_idx!=ULONG_MAX ) ) fd_stem_publish( stem, ctx->replay_out->idx, ctx->finalized_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
907 0 : FD_TEST( msg->bank_idx<ctx->max_live_slots );
908 0 : ctx->finalized_idx = msg->bank_idx;
909 :
910 0 : if( FD_LIKELY( ctx->confirmed_idx!=ULONG_MAX ) ) fd_stem_publish( stem, ctx->replay_out->idx, ctx->confirmed_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
911 0 : ctx->confirmed_idx = msg->bank_idx;
912 :
913 0 : if( FD_LIKELY( ctx->cluster_confirmed_slot!=ULONG_MAX ) ) fd_rpc_cluster_finalized( ctx, msg->slot );
914 0 : break;
915 0 : }
916 0 : case REPLAY_SIG_DROP_BANK_REF: {
917 0 : fd_replay_drop_bank_ref_t const * msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
918 0 : if( FD_UNLIKELY( ctx->processed_idx==msg->bank_idx ) ) {
919 0 : fd_stem_publish( stem, ctx->replay_out->idx, ctx->processed_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
920 0 : ctx->processed_idx = ULONG_MAX;
921 0 : }
922 0 : if( FD_UNLIKELY( ctx->confirmed_idx==msg->bank_idx ) ) {
923 0 : fd_stem_publish( stem, ctx->replay_out->idx, ctx->confirmed_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
924 0 : ctx->confirmed_idx = ULONG_MAX;
925 0 : }
926 0 : if( FD_UNLIKELY( ctx->finalized_idx==msg->bank_idx ) ) {
927 0 : fd_stem_publish( stem, ctx->replay_out->idx, ctx->finalized_idx, 0UL, 0UL, 0UL, 0UL, 0UL );
928 0 : ctx->finalized_idx = ULONG_MAX;
929 0 : }
930 0 : break;
931 0 : }
932 0 : default: {
933 0 : break;
934 0 : }
935 0 : }
936 0 : } else if( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP_OUT ) {
937 0 : fd_gossip_update_message_t const * update = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
938 0 : switch( update->tag ) {
939 0 : case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO: {
940 0 : if( FD_UNLIKELY( update->contact_info->idx>=FD_CONTACT_INFO_TABLE_SIZE ) ) FD_LOG_ERR(( "unexpected contact_info_idx %lu >= %lu", update->contact_info->idx, FD_CONTACT_INFO_TABLE_SIZE ));
941 0 : fd_rpc_cluster_node_t * node = &ctx->cluster_nodes[ update->contact_info->idx ];
942 0 : if( FD_LIKELY( node->valid ) ) fd_rpc_cluster_node_dlist_idx_remove( ctx->cluster_nodes_dlist, update->contact_info->idx, ctx->cluster_nodes );
943 :
944 0 : node->valid = 1;
945 0 : node->identity = *(fd_pubkey_t *)update->origin;
946 0 : fd_memcpy( node->ci, update->contact_info->value, sizeof(fd_gossip_contact_info_t) );
947 :
948 0 : fd_rpc_cluster_node_dlist_idx_push_tail( ctx->cluster_nodes_dlist, update->contact_info->idx, ctx->cluster_nodes );
949 0 : break;
950 0 : }
951 0 : case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE: {
952 0 : if( FD_UNLIKELY( update->contact_info_remove->idx>=FD_CONTACT_INFO_TABLE_SIZE ) ) FD_LOG_ERR(( "unexpected remove_contact_info_idx %lu >= %lu", update->contact_info_remove->idx, FD_CONTACT_INFO_TABLE_SIZE ));
953 0 : fd_rpc_cluster_node_t * node = &ctx->cluster_nodes[ update->contact_info_remove->idx ];
954 0 : FD_TEST( node->valid );
955 0 : node->valid = 0;
956 0 : fd_rpc_cluster_node_dlist_idx_remove( ctx->cluster_nodes_dlist, update->contact_info_remove->idx, ctx->cluster_nodes );
957 0 : break;
958 0 : }
959 0 : default: break;
960 0 : }
961 0 : } else if( ctx->in_kind[ in_idx ]==IN_KIND_EPOCH ) {
962 0 : fd_epoch_info_msg_t const * msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
963 0 : FD_TEST( msg->staked_vote_cnt<=MAX_STAKE_WEIGHTS );
964 0 : ctx->epoch_schedule = msg->epoch_schedule;
965 0 : ctx->has_epoch_schedule = 1;
966 :
967 0 : fd_rpc_mleaders_ingest( ctx->mleaders, msg );
968 0 : } else if( ctx->in_kind[ in_idx ]==IN_KIND_VOTOR ) {
969 0 : fd_votor_msg_t const * msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
970 0 : if( FD_LIKELY( msg->certed.kind==FD_CERT_KIND_FINAL || msg->certed.kind==FD_CERT_KIND_FAST_FINAL ) ) {
971 0 : fd_rpc_cluster_finalized( ctx, msg->certed.slot );
972 0 : }
973 0 : } else if( ctx->in_kind[ in_idx ]==IN_KIND_GENESI ) {
974 0 : ctx->has_genesis_hash = 1;
975 0 : fd_genesis_meta_t const * genesis_meta = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
976 0 : *ctx->genesis_hash = genesis_meta->genesis_hash;
977 :
978 0 : uchar const * blob = (uchar const *)( genesis_meta+1 );
979 0 : ulong const blob_sz = genesis_meta->blob_sz;
980 0 : FD_TEST( blob_sz<=ctx->genesis_max_message_size );
981 :
982 0 : ctx->genesis_tar_bz_sz = fd_rpc_file_as_tarball(
983 0 : ctx,
984 0 : "genesis.bin",
985 0 : blob, blob_sz,
986 0 : ctx->genesis_tar, ctx->genesis_tar_max_sz,
987 0 : ctx->genesis_tar_bz, ctx->genesis_tar_bz_max_sz );
988 0 : if( FD_UNLIKELY( ctx->genesis_tar_bz_sz==ULONG_MAX ) ) {
989 0 : FD_LOG_ERR(( "failed to create genesis tarball (blob_sz=%lu)", blob_sz ));
990 0 : }
991 0 : }
992 :
993 0 : return 0;
994 0 : }
995 :
996 0 : #define STAGE_JSON(__ctx) (__extension__({ \
997 0 : fd_http_server_response_t __res = (fd_http_server_response_t){ .content_type = "application/json", .status = 200 }; \
998 0 : if( FD_UNLIKELY( fd_http_server_stage_body( __ctx->http, &__res ) ) ) { \
999 0 : __res.status = 500; \
1000 0 : FD_LOG_WARNING(( "Failed to populate RPC response buffer" )); \
1001 0 : FD_LOG_HEXDUMP_WARNING(( "start of message:\n%.*s", __ctx->http->oring+(__ctx->http->stage_off%__ctx->http->oring_sz), fd_ulong_min( 500UL, __ctx->http->oring_sz-(__ctx->http->stage_off%__ctx->http->oring_sz)-1UL ) )); \
1002 0 : FD_LOG_HEXDUMP_WARNING(( "start of buffer:\n%.*s", __ctx->http->oring, fd_ulong_min( 500UL, __ctx->http->oring_sz ) )); \
1003 0 : } \
1004 0 : __res; }))
1005 :
1006 0 : #define PRINTF_JSON(__ctx, ...) (__extension__({ \
1007 0 : fd_http_server_printf( __ctx->http, __VA_ARGS__ ); \
1008 0 : fd_http_server_response_t __res = STAGE_JSON( __ctx ); \
1009 0 : __res; }))
1010 :
1011 :
1012 : static inline char const *
1013 0 : fd_rpc_val_kind_cstr( fd_rpc_val_t const * v ) {
1014 0 : switch( v->kind ) {
1015 0 : case FD_RPC_VAL_STR: return "string";
1016 0 : case FD_RPC_VAL_MAP: return "map";
1017 0 : case FD_RPC_VAL_SEQ: return "sequence";
1018 0 : case FD_RPC_VAL_BOOL: return "boolean";
1019 0 : case FD_RPC_VAL_FLOAT: return "floating point";
1020 0 : case FD_RPC_VAL_INT: return "integer";
1021 0 : case FD_RPC_VAL_NULL: return "null";
1022 0 : default: FD_LOG_ERR(( "unreachable" ));
1023 0 : }
1024 0 : }
1025 :
1026 : /* fd_rpc_str_ws_sz returns the size of the whitespace unit at the
1027 : start of the JSON string body [p,end), or 0 if it does not start
1028 : with one. A unit is a literal space or an escape that decodes to an
1029 : ASCII control character. */
1030 :
1031 : static ulong
1032 : fd_rpc_str_ws_sz( char const * p,
1033 0 : char const * end ) {
1034 0 : if( p[0]==' ' ) return 1UL;
1035 0 : if( p[0]!='\\' || p+1>=end ) return 0UL;
1036 0 : switch( p[1] ) {
1037 0 : case 'b': case 'f': case 'n': case 'r': case 't':
1038 0 : return 2UL;
1039 0 : case 'u':
1040 0 : if( p+6<=end && p[2]=='0' && p[3]=='0' && (p[4]=='0' || p[4]=='1') ) return 6UL;
1041 0 : return 0UL;
1042 0 : default:
1043 0 : return 0UL;
1044 0 : }
1045 0 : }
1046 :
1047 : /* fd_rpc_print_str appends the contents of a request string to the
1048 : response. The bytes between the quotes of a well formed JSON string
1049 : are themselves a well formed JSON string body, so they are copied
1050 : as-is, escapes included. Runs of whitespace (literal spaces and the
1051 : escapes for control characters) are collapsed into a single space to
1052 : keep the error message on one line. */
1053 :
1054 : static void
1055 : fd_rpc_print_str( fd_rpc_tile_t * ctx,
1056 0 : fd_jtok_str_t const * s ) {
1057 0 : char const * p = s->ptr;
1058 0 : char const * end = s->ptr + s->sz;
1059 0 : char const * span = p;
1060 0 : while( p<end ) {
1061 0 : ulong ws_sz = fd_rpc_str_ws_sz( p, end );
1062 0 : if( !ws_sz ) {
1063 0 : p += ( p[0]=='\\' && p+1<end ) ? 2 : 1; /* skip escapes whole */
1064 0 : continue;
1065 0 : }
1066 0 : if( span<p ) fd_http_server_memcpy( ctx->http, (uchar const *)span, (ulong)(p-span) );
1067 0 : fd_http_server_memcpy( ctx->http, (uchar const *)" ", 1UL );
1068 0 : do p += ws_sz; while( p<end && (ws_sz = fd_rpc_str_ws_sz( p, end )) );
1069 0 : span = p;
1070 0 : }
1071 0 : if( span<p ) fd_http_server_memcpy( ctx->http, (uchar const *)span, (ulong)(p-span) );
1072 0 : }
1073 :
1074 : /* fd_rpc_print_float formats a double to match serde_json / zmij. */
1075 :
1076 : static inline void
1077 : fd_rpc_print_float( fd_rpc_tile_t * ctx,
1078 0 : double d ) {
1079 0 : char buf[ 64 ];
1080 0 : double test;
1081 0 : int ok = 0;
1082 0 : if( FD_LIKELY( sprintf( buf, "%1.15g", d )>0 && sscanf( buf, "%lg", &test )==1 && test==d ) ) ok = 1;
1083 0 : else if( FD_LIKELY( sprintf( buf, "%1.16g", d )>0 && sscanf( buf, "%lg", &test )==1 && test==d ) ) ok = 1;
1084 0 : else if( FD_LIKELY( sprintf( buf, "%1.17g", d )>0 ) ) ok = 1;
1085 0 : if( FD_UNLIKELY( !ok ) ) {
1086 0 : fd_http_server_printf( ctx->http, " `0.0`" );
1087 0 : return;
1088 0 : }
1089 0 : if( !strchr( buf, '.' ) && !strchr( buf, 'e' ) ) {
1090 0 : fd_http_server_printf( ctx->http, " `%s.0`", buf );
1091 0 : } else {
1092 0 : fd_http_server_printf( ctx->http, " `%s`", buf );
1093 0 : }
1094 0 : }
1095 :
1096 : /* fd_rpc_err_invalid emits an "invalid type" / "invalid value" error
1097 : for v in the style of serde. what is "type" or "value", expected is
1098 : the name of the expected type. */
1099 :
1100 : static fd_http_server_response_t
1101 : fd_rpc_err_invalid( fd_rpc_tile_t * ctx,
1102 : char const * id_cstr,
1103 : char const * what,
1104 : fd_rpc_val_t const * v,
1105 0 : char const * expected ) {
1106 0 : fd_http_server_printf( ctx->http, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: invalid %s: %s", what, fd_rpc_val_kind_cstr( v ) );
1107 0 : switch( v->kind ) {
1108 0 : case FD_RPC_VAL_STR:
1109 0 : fd_http_server_printf( ctx->http, " \\\"" );
1110 0 : fd_rpc_print_str( ctx, &v->str );
1111 0 : fd_http_server_printf( ctx->http, "\\\"" );
1112 0 : break;
1113 0 : case FD_RPC_VAL_BOOL:
1114 0 : case FD_RPC_VAL_INT:
1115 0 : fd_http_server_printf( ctx->http, " `%.*s`", (int)v->raw_sz, v->raw );
1116 0 : break;
1117 0 : case FD_RPC_VAL_FLOAT: {
1118 0 : fd_jtok_t j[1];
1119 0 : double d;
1120 0 : fd_jtok_double( fd_jtok_init( j, v->raw, v->raw_sz ), &d );
1121 0 : if( FD_LIKELY( !fd_jtok_fini( j ) ) ) fd_rpc_print_float( ctx, d );
1122 0 : else fd_http_server_printf( ctx->http, " `%.*s`", (int)v->raw_sz, v->raw );
1123 0 : break;
1124 0 : }
1125 0 : default:
1126 0 : break;
1127 0 : }
1128 0 : fd_http_server_printf( ctx->http, ", expected %s.\"},\"id\":%s}\n", expected, id_cstr );
1129 0 : return STAGE_JSON( ctx );
1130 0 : }
1131 :
1132 : static fd_http_server_response_t
1133 : fd_rpc_err_unknown_encoding( fd_rpc_tile_t * ctx,
1134 : char const * id_cstr,
1135 0 : fd_jtok_str_t const * variant ) {
1136 0 : fd_http_server_printf( ctx->http, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: unknown variant `" );
1137 0 : fd_rpc_print_str( ctx, variant );
1138 0 : fd_http_server_printf( ctx->http, "`, expected one of `binary`, `base58`, `base64`, `jsonParsed`, `base64+zstd`.\"},\"id\":%s}\n", id_cstr );
1139 0 : return STAGE_JSON( ctx );
1140 0 : }
1141 :
1142 : static inline int
1143 : fd_rpc_validate_params( fd_rpc_tile_t * ctx,
1144 : char const * id_cstr,
1145 : fd_rpc_params_t const * params,
1146 : ulong min_cnt,
1147 : ulong max_cnt,
1148 0 : fd_http_server_response_t * res ) {
1149 0 : FD_TEST( min_cnt <= max_cnt );
1150 : /* Agave also includes a "data" field in some responses with the
1151 : faulty params payload. Instead of printing raw JSON, they print the
1152 : representation which we won't replicate.
1153 :
1154 : e.g. "data" might contain something like
1155 : Array([String(\"\"), Object {}])
1156 :
1157 : instead, we just include the field with an empty string
1158 : */
1159 :
1160 0 : ulong param_cnt = 0UL;
1161 0 : switch( params->kind ) {
1162 0 : case FD_RPC_VAL_NONE:
1163 0 : case FD_RPC_VAL_NULL:
1164 0 : break;
1165 0 : case FD_RPC_VAL_SEQ:
1166 0 : param_cnt = params->cnt;
1167 0 : break;
1168 0 : case FD_RPC_VAL_MAP:
1169 0 : if( max_cnt==0UL ) *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid parameters: No parameters were expected\",\"data\":\"\"},\"id\":%s}\n", id_cstr );
1170 0 : else *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"`params` should be an array\"},\"id\":%s}\n", id_cstr );
1171 0 : return 0;
1172 0 : default:
1173 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"Invalid request\"},\"id\":%s}\n", id_cstr );
1174 0 : return 0;
1175 0 : }
1176 :
1177 0 : if( FD_UNLIKELY( param_cnt>0UL && max_cnt==0UL ) ) {
1178 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid parameters: No parameters were expected\",\"data\":\"\"},\"id\":%s}\n", id_cstr );
1179 0 : return 0;
1180 0 : }
1181 0 : if( FD_UNLIKELY( param_cnt<min_cnt ) ) {
1182 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"`params` should have at least %lu argument(s)\"},\"id\":%s}\n", min_cnt, id_cstr );
1183 0 : return 0;
1184 0 : }
1185 0 : if( param_cnt>max_cnt ) {
1186 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid parameters: Expected from %lu to %lu parameters.\",\"data\":\"\\\"Got: %lu\\\"\"},\"id\":%s}\n", min_cnt, max_cnt, param_cnt, id_cstr );
1187 0 : return 0;
1188 0 : }
1189 :
1190 0 : return 1;
1191 0 : }
1192 :
1193 : /* TODO: use optimized version of this from fd_base58_tmpl.c */
1194 : static const char base58_chars[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1195 :
1196 : static inline int
1197 0 : fd_rpc_cstr_contains_non_base58(const char *str) {
1198 0 : for (; *str; str++) {
1199 0 : if (!strchr(base58_chars, *str)) return 1;
1200 0 : }
1201 0 : return 0;
1202 0 : }
1203 :
1204 : /* adapted from https://salsa.debian.org/debian/libbase58/-/blob/debian/master/base58.c */
1205 : static inline int
1206 0 : fd_rpc_base58_encode_128( char * b58, ulong * b58sz, const void *data, ulong binsz ) {
1207 0 : FD_TEST( binsz <= 128UL );
1208 :
1209 0 : const uchar * bin = data;
1210 0 : ulong carry;
1211 0 : ulong i, j, high, zcount = 0;
1212 0 : ulong size;
1213 :
1214 0 : while( zcount<binsz && !bin[ zcount ] ) zcount++;
1215 :
1216 0 : size = (binsz-zcount)*138/100+1; /* strict overestimate */
1217 0 : size = fd_ulong_min( size, FD_RPC_BASE58_ENCODED_128_LEN ); /* theoretical max */
1218 0 : uchar buf[ FD_RPC_BASE58_ENCODED_128_LEN ] = { 0 };
1219 :
1220 0 : for( i=zcount, high=size-1UL; i<binsz; i++, high=j ) {
1221 0 : for( carry=bin[ i ], j=size-1UL; (j>high) || carry; j-- ) {
1222 0 : carry += 256UL * buf[ j ];
1223 0 : buf[ j ] = (uchar)(carry%58UL);
1224 0 : carry /= 58UL;
1225 0 : if( FD_UNLIKELY( !j ) ) break;
1226 0 : }
1227 0 : }
1228 :
1229 0 : for( j=0; j<size && !buf[ j ]; j++);
1230 :
1231 0 : if( *b58sz<zcount+size-j ) {
1232 0 : *b58sz = zcount+size-j;
1233 0 : return 0;
1234 0 : }
1235 :
1236 0 : if (zcount) memset(b58, '1', zcount);
1237 0 : for( i=zcount; j<size; i++, j++) b58[ i ] = base58_chars[ buf[ j ] ];
1238 0 : *b58sz = i;
1239 :
1240 0 : return 1;
1241 0 : }
1242 :
1243 : /* getInflationGovernor and getMinimumBalanceForRentExemption take a
1244 : bare Option<CommitmentConfig> in Agave, so unlike the flattened
1245 : configs serde rejects a missing or malformed commitment member. */
1246 :
1247 : static int
1248 : fd_rpc_validate_commitment_strict( fd_rpc_tile_t * ctx,
1249 : char const * id_cstr,
1250 : fd_rpc_val_t const * config,
1251 0 : fd_http_server_response_t * res ) {
1252 0 : if( config->kind!=FD_RPC_VAL_MAP ) return 1; /* type errors are fd_rpc_validate_config's */
1253 0 : fd_rpc_config_t cfg[1];
1254 0 : fd_rpc_config_read( config, cfg );
1255 0 : fd_rpc_val_t const * commitment = &cfg->commitment;
1256 0 : if( FD_UNLIKELY( commitment->kind==FD_RPC_VAL_NONE ) ) {
1257 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: missing field `commitment`.\"},\"id\":%s}\n", id_cstr );
1258 0 : return 0;
1259 0 : }
1260 0 : if( FD_UNLIKELY( commitment->kind!=FD_RPC_VAL_STR ) ) {
1261 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", commitment, "enum CommitmentLevel" );
1262 0 : return 0;
1263 0 : }
1264 0 : if( FD_UNLIKELY( !fd_jtok_str_eq( &commitment->str, "processed" ) && !fd_jtok_str_eq( &commitment->str, "confirmed" ) && !fd_jtok_str_eq( &commitment->str, "finalized" ) ) ) {
1265 0 : fd_http_server_printf( ctx->http, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: unknown variant `" );
1266 0 : fd_rpc_print_str( ctx, &commitment->str );
1267 0 : fd_http_server_printf( ctx->http, "`, expected one of `processed`, `confirmed`, `finalized`.\"},\"id\":%s}\n", id_cstr );
1268 0 : *res = STAGE_JSON( ctx );
1269 0 : return 0;
1270 0 : }
1271 0 : return 1;
1272 0 : }
1273 :
1274 : static inline int
1275 : fd_rpc_validate_config( fd_rpc_tile_t * ctx,
1276 : char const * id_cstr,
1277 : fd_rpc_val_t const * config,
1278 : char const * config_rust_type,
1279 : int has_commitment,
1280 : int has_encoding,
1281 : int has_data_slice,
1282 : int has_min_context_slot,
1283 : ulong * bank_idx,
1284 : char const ** opt_encoding_cstr,
1285 : ulong * opt_slice_length,
1286 : ulong * opt_slice_offset,
1287 : fd_rpc_val_t * opt_identity,
1288 0 : fd_http_server_response_t * res ) {
1289 :
1290 0 : switch( config->kind ) {
1291 0 : case FD_RPC_VAL_INT:
1292 0 : case FD_RPC_VAL_FLOAT:
1293 0 : case FD_RPC_VAL_BOOL:
1294 0 : case FD_RPC_VAL_STR:
1295 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", config, config_rust_type );
1296 0 : return 0;
1297 0 : case FD_RPC_VAL_SEQ:
1298 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32065,\"message\":\"Firedancer Error: Positional config params not supported\"},\"id\":%s}\n", id_cstr );
1299 0 : return 0;
1300 0 : default:
1301 0 : break;
1302 0 : }
1303 :
1304 0 : fd_rpc_config_t cfg[1];
1305 0 : fd_rpc_config_read( config, cfg );
1306 0 : if( opt_identity ) *opt_identity = cfg->identity;
1307 :
1308 0 : ulong _bank_idx = ULONG_MAX;
1309 0 : if( FD_LIKELY( has_commitment ) ) {
1310 0 : fd_rpc_val_t const * commitment = &cfg->commitment;
1311 0 : char commitment_cstr[ 16 ];
1312 0 : if( FD_UNLIKELY( !fd_rpc_val_cstr( commitment, commitment_cstr, sizeof(commitment_cstr) ) ) ) _bank_idx = ctx->finalized_idx;
1313 0 : else if( FD_LIKELY( !strcmp( commitment_cstr, "processed" ) ) ) _bank_idx = ctx->processed_idx;
1314 0 : else if( FD_LIKELY( !strcmp( commitment_cstr, "confirmed" ) ) ) _bank_idx = ctx->confirmed_idx;
1315 0 : else if( FD_LIKELY( !strcmp( commitment_cstr, "finalized" ) ) ) _bank_idx = ctx->finalized_idx;
1316 0 : else _bank_idx = ctx->finalized_idx;
1317 0 : } else {
1318 0 : _bank_idx = ctx->finalized_idx;
1319 0 : }
1320 0 : if( FD_UNLIKELY( _bank_idx==ULONG_MAX ) ) {
1321 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32065,\"message\":\"Firedancer Error: banks uninitialized\"},\"id\":%s}\n", id_cstr );
1322 0 : return 0;
1323 0 : }
1324 0 : *bank_idx = _bank_idx;
1325 :
1326 0 : if( FD_LIKELY( has_encoding ) ) {
1327 0 : fd_rpc_val_t const * encoding = &cfg->encoding;
1328 :
1329 0 : if( FD_UNLIKELY( encoding->kind==FD_RPC_VAL_INT || encoding->kind==FD_RPC_VAL_FLOAT || encoding->kind==FD_RPC_VAL_BOOL ) ) {
1330 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", encoding, "string or map" );
1331 0 : return 0;
1332 0 : }
1333 :
1334 : /* An encoding given as a map must be a single key naming the
1335 : variant with a null (unit) value, e.g. {"base64":null}. */
1336 0 : fd_jtok_str_t variant_key = {0};
1337 0 : fd_rpc_val_t variant_val[1] = {{ .kind = FD_RPC_VAL_NONE }};
1338 0 : if( FD_UNLIKELY( encoding->kind==FD_RPC_VAL_MAP ) ) {
1339 0 : fd_jtok_t j[1];
1340 0 : fd_jtok_str_t key;
1341 0 : ulong member_cnt = 0UL;
1342 0 : fd_jtok_obj_enter( fd_jtok_init( j, encoding->raw, encoding->raw_sz ) );
1343 0 : while( fd_jtok_obj_next( j, &key ) ) {
1344 0 : if( !member_cnt ) { variant_key = key; fd_rpc_val_read( j, variant_val ); }
1345 0 : member_cnt++;
1346 0 : }
1347 0 : if( FD_UNLIKELY( member_cnt!=1UL ) ) {
1348 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: invalid value: map, expected map with a single key.\"},\"id\":%s}\n", id_cstr );
1349 0 : return 0;
1350 0 : }
1351 0 : }
1352 0 : if( FD_UNLIKELY( encoding->kind==FD_RPC_VAL_SEQ ) ) {
1353 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", encoding, "string or map" );
1354 0 : return 0;
1355 0 : }
1356 :
1357 0 : char const * encoding_cstr;
1358 0 : if( FD_UNLIKELY( encoding->kind==FD_RPC_VAL_MAP ) ) {
1359 0 : if( fd_jtok_str_eq( &variant_key, "binary" ) ) encoding_cstr = "binary";
1360 0 : else if( fd_jtok_str_eq( &variant_key, "base58" ) ) encoding_cstr = "base58";
1361 0 : else if( fd_jtok_str_eq( &variant_key, "base64" ) ) encoding_cstr = "base64";
1362 0 : else if( fd_jtok_str_eq( &variant_key, "base64+zstd" ) ) encoding_cstr = "base64+zstd";
1363 0 : else if( fd_jtok_str_eq( &variant_key, "jsonParsed" ) ) encoding_cstr = "jsonParsed";
1364 0 : else {
1365 0 : *res = fd_rpc_err_unknown_encoding( ctx, id_cstr, &variant_key );
1366 0 : return 0;
1367 0 : }
1368 0 : if( FD_UNLIKELY( variant_val->kind!=FD_RPC_VAL_NULL ) ) {
1369 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", variant_val, "unit" );
1370 0 : return 0;
1371 0 : }
1372 0 : } else {
1373 0 : char buf[ 16 ];
1374 0 : if( encoding->kind!=FD_RPC_VAL_STR ) encoding_cstr = "binary";
1375 0 : else if( !fd_rpc_val_cstr( encoding, buf, sizeof(buf) ) ) encoding_cstr = "";
1376 0 : else if( !strcmp( buf, "binary" ) ) encoding_cstr = "binary";
1377 0 : else if( !strcmp( buf, "base58" ) ) encoding_cstr = "base58";
1378 0 : else if( !strcmp( buf, "base64" ) ) encoding_cstr = "base64";
1379 0 : else if( !strcmp( buf, "base64+zstd" ) ) encoding_cstr = "base64+zstd";
1380 0 : else if( !strcmp( buf, "jsonParsed" ) ) encoding_cstr = "jsonParsed";
1381 0 : else encoding_cstr = "";
1382 0 : }
1383 :
1384 0 : if( 0==strcmp( encoding_cstr, "jsonParsed" ) ) {
1385 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32065,\"message\":\"Firedancer Error: jsonParsed is unsupported\"},\"id\":%s}\n", id_cstr );
1386 0 : return 0;
1387 0 : } else if( FD_UNLIKELY( !encoding_cstr[0] ) ) {
1388 0 : *res = fd_rpc_err_unknown_encoding( ctx, id_cstr, &encoding->str );
1389 0 : return 0;
1390 0 : }
1391 :
1392 0 : if( FD_LIKELY( opt_encoding_cstr ) ) *opt_encoding_cstr = encoding_cstr;
1393 0 : }
1394 :
1395 0 : if( FD_LIKELY( has_data_slice ) ) {
1396 0 : fd_rpc_val_t const * dataSlice = &cfg->data_slice;
1397 :
1398 0 : if( FD_UNLIKELY( dataSlice->kind==FD_RPC_VAL_INT || dataSlice->kind==FD_RPC_VAL_FLOAT || dataSlice->kind==FD_RPC_VAL_BOOL || dataSlice->kind==FD_RPC_VAL_STR ) ) {
1399 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", dataSlice, "struct UiDataSliceConfig" );
1400 0 : return 0;
1401 0 : }
1402 :
1403 : /* A data slice is either {"offset":..,"length":..} or a two
1404 : element [offset,length] array. */
1405 0 : fd_rpc_val_t _offset[1] = {{ .kind = FD_RPC_VAL_NONE }};
1406 0 : fd_rpc_val_t _length[1] = {{ .kind = FD_RPC_VAL_NONE }};
1407 0 : ulong slice_cnt = 0UL;
1408 0 : if( dataSlice->kind==FD_RPC_VAL_MAP ) {
1409 0 : fd_jtok_t j[1];
1410 0 : fd_jtok_str_t key;
1411 0 : fd_jtok_obj_enter( fd_jtok_init( j, dataSlice->raw, dataSlice->raw_sz ) );
1412 0 : while( fd_jtok_obj_next( j, &key ) ) {
1413 0 : if( fd_jtok_str_eq( &key, "offset" ) ) { if( _offset->kind==FD_RPC_VAL_NONE ) fd_rpc_val_read( j, _offset ); }
1414 0 : else if( fd_jtok_str_eq( &key, "length" ) ) { if( _length->kind==FD_RPC_VAL_NONE ) fd_rpc_val_read( j, _length ); }
1415 0 : }
1416 0 : } else if( FD_UNLIKELY( dataSlice->kind==FD_RPC_VAL_SEQ ) ) {
1417 0 : fd_jtok_t j[1];
1418 0 : fd_jtok_arr_enter( fd_jtok_init( j, dataSlice->raw, dataSlice->raw_sz ) );
1419 0 : while( fd_jtok_arr_next( j ) ) {
1420 0 : if( slice_cnt==0UL ) fd_rpc_val_read( j, _offset );
1421 0 : else if( slice_cnt==1UL ) fd_rpc_val_read( j, _length );
1422 0 : slice_cnt++;
1423 0 : }
1424 0 : }
1425 :
1426 0 : if( FD_UNLIKELY( _offset->kind==FD_RPC_VAL_BOOL || _offset->kind==FD_RPC_VAL_FLOAT ) ) {
1427 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _offset, "usize" );
1428 0 : return 0;
1429 0 : }
1430 0 : if( FD_UNLIKELY( _length->kind==FD_RPC_VAL_BOOL || _length->kind==FD_RPC_VAL_FLOAT ) ) {
1431 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _length, "usize" );
1432 0 : return 0;
1433 0 : }
1434 :
1435 0 : if( FD_UNLIKELY( _offset->kind==FD_RPC_VAL_INT && _offset->neg ) ) {
1436 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "value", _offset, "usize" );
1437 0 : return 0;
1438 0 : }
1439 0 : if( FD_UNLIKELY( _length->kind==FD_RPC_VAL_INT && _length->neg ) ) {
1440 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "value", _length, "usize" );
1441 0 : return 0;
1442 0 : }
1443 :
1444 0 : if( FD_UNLIKELY( _offset->kind==FD_RPC_VAL_STR ) ) {
1445 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _offset, "usize" );
1446 0 : return 0;
1447 0 : }
1448 0 : if( FD_UNLIKELY( _length->kind==FD_RPC_VAL_STR ) ) {
1449 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _length, "usize" );
1450 0 : return 0;
1451 0 : }
1452 :
1453 0 : if( FD_UNLIKELY( _offset->kind!=FD_RPC_VAL_NONE && _offset->kind!=FD_RPC_VAL_INT ) ) {
1454 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _offset, "usize" );
1455 0 : return 0;
1456 0 : }
1457 0 : if( FD_UNLIKELY( _length->kind!=FD_RPC_VAL_NONE && _length->kind!=FD_RPC_VAL_INT ) ) {
1458 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _length, "usize" );
1459 0 : return 0;
1460 0 : }
1461 :
1462 0 : if( FD_UNLIKELY( dataSlice->kind==FD_RPC_VAL_MAP && _offset->kind==FD_RPC_VAL_NONE ) ) {
1463 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: missing field `offset`.\"},\"id\":%s}\n", id_cstr );
1464 0 : return 0;
1465 0 : }
1466 0 : if( FD_UNLIKELY( dataSlice->kind==FD_RPC_VAL_MAP && _length->kind==FD_RPC_VAL_NONE ) ) {
1467 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: missing field `length`.\"},\"id\":%s}\n", id_cstr );
1468 0 : return 0;
1469 0 : }
1470 :
1471 0 : if( FD_UNLIKELY( dataSlice->kind==FD_RPC_VAL_SEQ && slice_cnt!=2UL ) ) {
1472 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: invalid length %lu, expected struct UiDataSliceConfig with 2 elements.\"},\"id\":%s}\n", slice_cnt, id_cstr );
1473 0 : return 0;
1474 0 : }
1475 :
1476 0 : if( dataSlice->kind==FD_RPC_VAL_MAP || dataSlice->kind==FD_RPC_VAL_SEQ ) {
1477 0 : if( FD_LIKELY( opt_slice_offset ) ) *opt_slice_offset = _offset->u;
1478 0 : if( FD_LIKELY( opt_slice_length ) ) *opt_slice_length = _length->u;
1479 0 : } else {
1480 0 : if( FD_LIKELY( opt_slice_offset ) ) *opt_slice_offset = 0UL;
1481 0 : if( FD_LIKELY( opt_slice_length ) ) *opt_slice_length = ULONG_MAX;
1482 0 : }
1483 0 : }
1484 :
1485 0 : if( FD_LIKELY( has_min_context_slot ) ) {
1486 0 : fd_rpc_val_t const * _minContextSlot = &cfg->min_context_slot;
1487 0 : if( FD_UNLIKELY( _minContextSlot->kind==FD_RPC_VAL_BOOL || _minContextSlot->kind==FD_RPC_VAL_FLOAT ) ) {
1488 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _minContextSlot, "u64" );
1489 0 : return 0;
1490 0 : }
1491 :
1492 0 : if( FD_UNLIKELY( _minContextSlot->kind==FD_RPC_VAL_INT && _minContextSlot->neg ) ) {
1493 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "value", _minContextSlot, "u64" );
1494 0 : return 0;
1495 0 : }
1496 :
1497 0 : if( FD_UNLIKELY( _minContextSlot->kind==FD_RPC_VAL_STR ) ) {
1498 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _minContextSlot, "u64" );
1499 0 : return 0;
1500 0 : }
1501 :
1502 0 : if( FD_UNLIKELY( _minContextSlot->kind==FD_RPC_VAL_MAP || _minContextSlot->kind==FD_RPC_VAL_SEQ ) ) {
1503 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", _minContextSlot, "u64" );
1504 0 : return 0;
1505 0 : }
1506 :
1507 0 : ulong minContextSlot = _minContextSlot->kind==FD_RPC_VAL_INT ? _minContextSlot->u : 0UL;
1508 :
1509 0 : if( _bank_idx!=ULONG_MAX && ctx->banks[ _bank_idx ].slot<minContextSlot ) {
1510 0 : *res = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":%d,\"message\":\"Minimum context slot has not been reached\",\"data\":{\"contextSlot\":%lu}},\"id\":%s}\n", FD_RPC_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED, ctx->banks[ _bank_idx ].slot, id_cstr );
1511 0 : return 0;
1512 0 : }
1513 0 : }
1514 :
1515 0 : return 1;
1516 0 : }
1517 :
1518 : static int
1519 : fd_rpc_validate_address( fd_rpc_tile_t * ctx,
1520 : char const * id_cstr,
1521 : fd_rpc_val_t const * address_in,
1522 : fd_pubkey_t * address_out,
1523 0 : fd_http_server_response_t * response ) {
1524 0 : if( FD_UNLIKELY( address_in->kind!=FD_RPC_VAL_STR ) ) {
1525 0 : *response = fd_rpc_err_invalid( ctx, id_cstr, "type", address_in, "a string" );
1526 0 : return 0;
1527 0 : }
1528 0 : char address_cstr[ FD_BASE58_ENCODED_32_SZ ];
1529 0 : fd_jtok_t s[1];
1530 0 : fd_jtok_cstr( fd_jtok_init( s, address_in->raw, address_in->raw_sz ), address_cstr, sizeof(address_cstr) );
1531 0 : int fits = !fd_jtok_err( s );
1532 0 : int invalid_char;
1533 0 : if( FD_LIKELY( fits ) ) invalid_char = fd_rpc_cstr_contains_non_base58( address_cstr );
1534 0 : else {
1535 : /* Too long to be a pubkey. A string of only base58 characters is
1536 : still WrongSize rather than Invalid. */
1537 0 : invalid_char = 0;
1538 0 : for( ulong i=0UL; i<address_in->str.sz; i++ ) {
1539 0 : if( !strchr( base58_chars, address_in->str.ptr[ i ] ) ) { invalid_char = 1; break; }
1540 0 : }
1541 0 : }
1542 0 : if( FD_UNLIKELY( invalid_char ) ) {
1543 0 : *response = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid param: Invalid\"},\"id\":%s}\n", id_cstr );
1544 0 : return 0;
1545 0 : }
1546 0 : int valid = fits && !!fd_base58_decode_32( address_cstr, address_out->uc );
1547 0 : if( FD_UNLIKELY( !valid ) ) {
1548 0 : *response = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid param: WrongSize\"},\"id\":%s}\n", id_cstr );
1549 0 : return 0;
1550 0 : }
1551 :
1552 0 : return 1;
1553 0 : }
1554 :
1555 : static inline int
1556 : fd_rpc_validate_uint_param( fd_rpc_tile_t * ctx,
1557 : char const * id_cstr,
1558 : fd_rpc_val_t const * val,
1559 : char const * type_cstr,
1560 : ulong * out,
1561 0 : fd_http_server_response_t * res ) {
1562 0 : if( FD_UNLIKELY( val->kind==FD_RPC_VAL_BOOL || val->kind==FD_RPC_VAL_FLOAT ) ) {
1563 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", val, type_cstr );
1564 0 : return 0;
1565 0 : }
1566 0 : if( FD_UNLIKELY( val->kind==FD_RPC_VAL_INT && val->neg ) ) {
1567 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "value", val, type_cstr );
1568 0 : return 0;
1569 0 : }
1570 0 : if( FD_UNLIKELY( val->kind!=FD_RPC_VAL_INT ) ) {
1571 0 : *res = fd_rpc_err_invalid( ctx, id_cstr, "type", val, type_cstr );
1572 0 : return 0;
1573 0 : }
1574 :
1575 0 : *out = val->u;
1576 0 : return 1;
1577 0 : }
1578 :
1579 : #define UNIMPLEMENTED(X) \
1580 : static fd_http_server_response_t \
1581 : X( fd_rpc_tile_t * ctx, \
1582 : char const * id_cstr, \
1583 0 : fd_rpc_params_t const * params ) { \
1584 0 : (void)ctx; (void)id_cstr; (void)params; \
1585 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_UNIMPLEMENTED, 1UL ); \
1586 0 : return (fd_http_server_response_t){ .status = 501 }; \
1587 0 : }
1588 :
1589 : UNIMPLEMENTED(getBlock)
1590 : UNIMPLEMENTED(getBlockCommitment)
1591 :
1592 : /* fd_rpc_encode_account_data encodes account data fields
1593 : (executable, lamports, owner, rentEpoch, space, data) into the http
1594 : staging buffer. Returns 1 on success. On failure, calls
1595 : fd_http_server_unstage and writes an error response into
1596 : err_response. */
1597 : static int
1598 : fd_rpc_encode_account_data( fd_rpc_tile_t * ctx,
1599 : uchar const * acct_data,
1600 : ulong acct_data_len,
1601 : uchar const * acct_owner,
1602 : ulong acct_lamports,
1603 : int acct_executable,
1604 : char const * encoding_cstr,
1605 : ulong slice_offset,
1606 : ulong slice_length,
1607 : char const * id_cstr,
1608 0 : fd_http_server_response_t * err_response ) {
1609 :
1610 0 : int is_binary = !strcmp( encoding_cstr, "binary" );
1611 0 : int is_base58 = !strcmp( encoding_cstr, "base58" );
1612 0 : int is_zstd = !strcmp( encoding_cstr, "base64+zstd" );
1613 :
1614 0 : ulong data_sz = acct_data_len;
1615 0 : uchar const * out = acct_data + fd_ulong_if( slice_offset<data_sz, slice_offset, 0UL );
1616 0 : ulong snip_sz = fd_ulong_min( fd_ulong_if( slice_offset<data_sz, data_sz-slice_offset, 0UL ), slice_length );
1617 0 : ulong out_sz = snip_sz;
1618 :
1619 0 : if( FD_UNLIKELY( (is_binary || is_base58) && snip_sz>128UL ) ) {
1620 0 : fd_http_server_unstage( ctx->http );
1621 0 : *err_response = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"Encoded binary (base 58) data should be less than 128 bytes, please use Base64 encoding.\"},\"id\":%s}\n", id_cstr );
1622 0 : return 0;
1623 0 : }
1624 :
1625 0 : if( is_zstd ) {
1626 0 : ulong zstd_res = ZSTD_compressCCtx( ctx->zstd_cctx, ctx->compress_buf, sizeof(ctx->compress_buf), out, snip_sz, FD_RPC_ZSTD_LEVEL );
1627 0 : if( ZSTD_isError( zstd_res ) ) {
1628 0 : fd_http_server_unstage( ctx->http );
1629 0 : *err_response = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32065,\"message\":\"Firedancer Error: zstandard compression failed (%s)\"},\"id\":%s}\n", ZSTD_getErrorName( zstd_res ), id_cstr );
1630 0 : return 0;
1631 0 : }
1632 0 : out = ctx->compress_buf;
1633 0 : out_sz = (ulong)zstd_res;
1634 0 : }
1635 :
1636 0 : FD_BASE58_ENCODE_32_BYTES( acct_owner, owner_b58 );
1637 0 : fd_http_server_printf( ctx->http,
1638 0 : "{\"executable\":%s,\"lamports\":%lu,\"owner\":\"%s\",\"rentEpoch\":18446744073709551615,\"space\":%lu,\"data\":",
1639 0 : acct_executable ? "true" : "false",
1640 0 : acct_lamports,
1641 0 : owner_b58,
1642 0 : data_sz );
1643 :
1644 0 : ulong encoded_sz = fd_ulong_if( is_base58 || is_binary, FD_RPC_BASE58_ENCODED_128_LEN, FD_BASE64_ENC_SZ( out_sz ) );
1645 0 : if( FD_UNLIKELY( is_binary ) ) {
1646 0 : fd_http_server_printf( ctx->http, "\"" );
1647 0 : } else {
1648 0 : fd_http_server_printf( ctx->http, "[\"" );
1649 0 : }
1650 :
1651 0 : uchar * encoded = fd_http_server_append_start( ctx->http, encoded_sz );
1652 0 : if( FD_UNLIKELY( !encoded ) ) {
1653 0 : fd_http_server_unstage( ctx->http );
1654 0 : *err_response = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32065,\"message\":\"Firedancer Error: response encoding buffer overflow (account data too large for response buffer)\"},\"id\":%s}\n", id_cstr );
1655 0 : return 0;
1656 0 : }
1657 :
1658 0 : if( FD_UNLIKELY( is_base58 || is_binary ) ) {
1659 0 : if( FD_UNLIKELY( !fd_rpc_base58_encode_128( (char *)encoded, &encoded_sz, out, out_sz ) ) ) {
1660 0 : fd_http_server_unstage( ctx->http );
1661 0 : FD_LOG_WARNING(( "base58 encode failed out_sz=%lu", out_sz ));
1662 0 : *err_response = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32065,\"message\":\"Firedancer Error: base58 encode failed\"},\"id\":%s}\n", id_cstr );
1663 0 : return 0;
1664 0 : }
1665 0 : } else {
1666 0 : encoded_sz = fd_base64_encode( (char *)encoded, out, out_sz );
1667 0 : }
1668 :
1669 0 : fd_http_server_append_end( ctx->http, encoded_sz );
1670 :
1671 0 : if( FD_UNLIKELY( is_binary ) ) fd_http_server_printf( ctx->http, "\"}" );
1672 0 : else fd_http_server_printf( ctx->http, "\",\"%s\"]}", encoding_cstr );
1673 :
1674 0 : return 1;
1675 0 : }
1676 :
1677 : static fd_http_server_response_t
1678 : getAccountInfo( fd_rpc_tile_t * ctx,
1679 : char const * id_cstr,
1680 0 : fd_rpc_params_t const * params ) {
1681 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_ACCOUNT_INFO, 1UL );
1682 :
1683 0 : fd_http_server_response_t response;
1684 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 1, 2, &response ) ) ) return response;
1685 :
1686 0 : fd_pubkey_t address;
1687 0 : fd_rpc_val_t const * acct_pubkey = ¶ms->v[ 0 ];
1688 0 : if( FD_UNLIKELY( !fd_rpc_validate_address( ctx, id_cstr, acct_pubkey, &address, &response ) ) ) return response;
1689 :
1690 0 : ulong bank_idx = ULONG_MAX;
1691 0 : char const * encoding_cstr = NULL;
1692 0 : ulong slice_length = ULONG_MAX;
1693 0 : ulong slice_offset = 0;
1694 0 : fd_rpc_val_t const * config = ¶ms->v[ 1 ];
1695 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcAccountInfoConfig",
1696 0 : 1, /* has_commitment */
1697 0 : 1, /* has_encoding */
1698 0 : 1, /* has_data_slice */
1699 0 : 1, /* has_min_context_slot */
1700 0 : &bank_idx,
1701 0 : &encoding_cstr,
1702 0 : &slice_length,
1703 0 : &slice_offset,
1704 0 : NULL,
1705 0 : &response );
1706 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
1707 :
1708 0 : bank_info_t * info = &ctx->banks[ bank_idx ];
1709 0 : ulong acct_lamports;
1710 0 : int acct_executable;
1711 0 : uchar acct_owner[ 32UL ];
1712 0 : ulong acct_data_len;
1713 0 : fd_accdb_read_one_nocache( ctx->accdb, info->accdb_fork_id, address.uc,
1714 0 : &acct_lamports, &acct_executable, acct_owner,
1715 0 : ctx->scratch.accdb_data_buf, &acct_data_len );
1716 0 : if( FD_UNLIKELY( !acct_lamports ) ) {
1717 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"%s\",\"slot\":%lu},\"value\":null},\"id\":%s}\n", FD_RPC_AGAVE_API_VERSION, info->slot, id_cstr );
1718 0 : }
1719 :
1720 0 : fd_http_server_printf( ctx->http, "{\"jsonrpc\":\"2.0\",\"id\":%s,\"result\":{\"context\":{\"apiVersion\":\"%s\",\"slot\":%lu},\"value\":", id_cstr, FD_RPC_AGAVE_API_VERSION, info->slot );
1721 :
1722 0 : fd_http_server_response_t err_response;
1723 0 : if( FD_UNLIKELY( !fd_rpc_encode_account_data( ctx, ctx->scratch.accdb_data_buf, acct_data_len, acct_owner, acct_lamports, acct_executable, encoding_cstr, slice_offset, slice_length, id_cstr, &err_response ) ) ) {
1724 0 : return err_response;
1725 0 : }
1726 :
1727 0 : fd_http_server_printf( ctx->http, "}}\n" );
1728 0 : return STAGE_JSON( ctx );
1729 0 : }
1730 :
1731 : static fd_http_server_response_t
1732 : getBalance( fd_rpc_tile_t * ctx,
1733 : char const * id_cstr,
1734 0 : fd_rpc_params_t const * params ) {
1735 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_BALANCE, 1UL );
1736 :
1737 0 : fd_http_server_response_t response;
1738 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 1, 2, &response ) ) ) return response;
1739 :
1740 0 : fd_pubkey_t address;
1741 0 : fd_rpc_val_t const * acct_pubkey = ¶ms->v[ 0 ];
1742 0 : if( FD_UNLIKELY( !fd_rpc_validate_address( ctx, id_cstr, acct_pubkey, &address, &response ) ) ) return response;
1743 :
1744 0 : ulong bank_idx = ULONG_MAX;
1745 0 : fd_rpc_val_t const * config = ¶ms->v[ 1 ];
1746 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcContextConfig",
1747 0 : 1, /* has_commitment */
1748 0 : 0, /* has_encoding */
1749 0 : 0, /* has_data_slice */
1750 0 : 1, /* has_min_context_slot */
1751 0 : &bank_idx,
1752 0 : NULL,
1753 0 : NULL,
1754 0 : NULL,
1755 0 : NULL,
1756 0 : &response );
1757 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
1758 :
1759 0 : ulong balance = fd_accdb_lamports( ctx->accdb, ctx->banks[ bank_idx ].accdb_fork_id, address.uc );
1760 :
1761 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"%s\",\"slot\":%lu},\"value\":%lu},\"id\":%s}\n", FD_RPC_AGAVE_API_VERSION, ctx->banks[ bank_idx ].slot, balance, id_cstr );
1762 0 : }
1763 :
1764 : static fd_http_server_response_t
1765 : getBlockHeight( fd_rpc_tile_t * ctx,
1766 : char const * id_cstr,
1767 0 : fd_rpc_params_t const * params ) {
1768 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_BLOCK_HEIGHT, 1UL );
1769 :
1770 0 : fd_http_server_response_t response;
1771 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 1, &response ) ) ) return response;
1772 :
1773 0 : ulong bank_idx = ULONG_MAX;
1774 0 : fd_rpc_val_t const * config = ¶ms->v[ 0 ];
1775 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcContextConfig",
1776 0 : 1, /* has_commitment */
1777 0 : 0, /* has_encoding */
1778 0 : 0, /* has_data_slice */
1779 0 : 1, /* has_min_context_slot */
1780 0 : &bank_idx,
1781 0 : NULL,
1782 0 : NULL,
1783 0 : NULL,
1784 0 : NULL,
1785 0 : &response );
1786 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
1787 :
1788 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}\n", ctx->banks[ bank_idx ].block_height, id_cstr );
1789 0 : }
1790 :
1791 : UNIMPLEMENTED(getBlockProduction) // TODO: Used by solana-exporter
1792 : UNIMPLEMENTED(getBlocks)
1793 : UNIMPLEMENTED(getBlocksWithLimit)
1794 : UNIMPLEMENTED(getBlockTime)
1795 :
1796 : static fd_http_server_response_t
1797 : getClusterNodes( fd_rpc_tile_t * ctx,
1798 : char const * id_cstr,
1799 0 : fd_rpc_params_t const * params ) {
1800 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_CLUSTER_NODES, 1UL );
1801 :
1802 0 : fd_http_server_response_t response;
1803 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
1804 :
1805 0 : fd_http_server_printf( ctx->http, "{\"jsonrpc\":\"2.0\",\"result\":[" );
1806 :
1807 0 : for( fd_rpc_cluster_node_dlist_iter_t iter = fd_rpc_cluster_node_dlist_iter_rev_init( ctx->cluster_nodes_dlist, ctx->cluster_nodes );
1808 0 : !fd_rpc_cluster_node_dlist_iter_done( iter, ctx->cluster_nodes_dlist, ctx->cluster_nodes );
1809 0 : iter = fd_rpc_cluster_node_dlist_iter_rev_next( iter, ctx->cluster_nodes_dlist, ctx->cluster_nodes ) ) {
1810 0 : fd_rpc_cluster_node_t * ele = fd_rpc_cluster_node_dlist_iter_ele( iter, ctx->cluster_nodes_dlist, ctx->cluster_nodes );
1811 0 : FD_BASE58_ENCODE_32_BYTES( ele->identity.uc, identity_cstr );
1812 0 : int is_last = fd_rpc_cluster_node_dlist_iter_done( fd_rpc_cluster_node_dlist_iter_rev_next( iter, ctx->cluster_nodes_dlist, ctx->cluster_nodes ), ctx->cluster_nodes_dlist, ctx->cluster_nodes );
1813 :
1814 0 : fd_http_server_printf( ctx->http, "{\"featureSet\":%u,", ele->ci->version.feature_set );
1815 :
1816 0 : for( ulong i=0UL; i<FD_GOSSIP_CONTACT_INFO_SOCKET_CNT; i++ ) {
1817 0 : char const * name;
1818 0 : switch( i ) {
1819 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_GOSSIP: name = "gossip"; break;
1820 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_SERVE_REPAIR_QUIC: name = NULL; break;
1821 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_RPC: name = "rpc"; break;
1822 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_RPC_PUBSUB: name = "pubsub"; break;
1823 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_SERVE_REPAIR: name = "serveRepair"; break;
1824 : /* Even though Agave does not support "tpu" and "tpuForwards"
1825 : (hardcoded null), Firedancer still supports
1826 : UDP-TPU. */
1827 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_TPU: name = "tpu"; break;
1828 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_FORWARDS: name = "tpuForwards"; break;
1829 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_FORWARDS_QUIC: name = "tpuForwardsQuic"; break;
1830 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_QUIC: name = "tpuQuic"; break;
1831 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_VOTE: name = "tpuVote"; break;
1832 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_TVU: name = "tvu"; break;
1833 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_TVU_QUIC: name = NULL; break;
1834 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_VOTE_QUIC: name = NULL; break;
1835 0 : case FD_GOSSIP_CONTACT_INFO_SOCKET_ALPENGLOW: name = NULL; break;
1836 0 : default: FD_LOG_ERR(( "unreachable "));
1837 0 : }
1838 0 : if( FD_UNLIKELY( !name ) ) continue;
1839 :
1840 0 : uint ip4 = ele->ci->sockets[ i ].is_ipv6 ? 0U : ele->ci->sockets[ i ].ip4;
1841 0 : if( FD_LIKELY( !!ip4 || !!ele->ci->sockets[ i ].port ) ) fd_http_server_printf( ctx->http, "\"%s\":\"" FD_IP4_ADDR_FMT ":%hu\",", name, FD_IP4_ADDR_FMT_ARGS( ip4 ), fd_ushort_bswap( ele->ci->sockets[ i ].port ) );
1842 0 : else fd_http_server_printf( ctx->http, "\"%s\":null,", name );
1843 0 : }
1844 0 : fd_http_server_printf( ctx->http, "\"pubkey\":\"%s\",", identity_cstr );
1845 0 : fd_http_server_printf( ctx->http, "\"shredVersion\":%u,", ele->ci->shred_version );
1846 :
1847 0 : char version[ 64UL ];
1848 0 : FD_TEST( fd_gossip_version_cstr( ele->ci->version.major, ele->ci->version.minor, ele->ci->version.patch, version, sizeof( version ) ) );
1849 0 : fd_http_server_printf( ctx->http, "\"version\":\"%s\",", version );
1850 :
1851 0 : char const * client_id;
1852 0 : switch( ele->ci->version.client ) {
1853 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_SOLANA_LABS: client_id = "SolanaLabs"; break;
1854 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_JITO_LABS: client_id = "JitoLabs"; break;
1855 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_FRANKENDANCER: client_id = "Frankendancer"; break;
1856 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_AGAVE: client_id = "Agave"; break;
1857 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_AGAVE_PALADIN: client_id = "AgavePaladin"; break;
1858 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_FIREDANCER: client_id = "Firedancer"; break;
1859 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_AGAVE_BAM: client_id = "AgaveBam"; break;
1860 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_SIG: client_id = "Sig"; break;
1861 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_RAKURAI: client_id = "Rakurai"; break;
1862 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_HARMONIC_FIREDANCER: client_id = "HarmonicFiredancer"; break;
1863 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_HARMONIC_AGAVE: client_id = "HarmonicAgave"; break;
1864 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_HARMONIC_FRANKENDANCER: client_id = "HarmonicFrankendancer"; break;
1865 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_FIRE_BAM: client_id = "FireBAM"; break;
1866 0 : case FD_GOSSIP_CONTACT_INFO_CLIENT_RAIKU: client_id = "Raiku"; break;
1867 0 : default: client_id = NULL; break;
1868 0 : }
1869 0 : if( FD_LIKELY( client_id ) ) fd_http_server_printf( ctx->http, "\"clientId\":\"%s\"", client_id );
1870 0 : else fd_http_server_printf( ctx->http, "\"clientId\":\"Unknown(%hu)\"", ele->ci->version.client );
1871 :
1872 0 : if( FD_UNLIKELY( is_last ) ) fd_http_server_printf( ctx->http, "}" );
1873 0 : else fd_http_server_printf( ctx->http, "}," );
1874 0 : }
1875 :
1876 0 : fd_http_server_printf( ctx->http, "],\"id\":%s}\n", id_cstr );
1877 0 : return STAGE_JSON( ctx );
1878 0 : }
1879 :
1880 : static fd_http_server_response_t
1881 : getEpochInfo( fd_rpc_tile_t * ctx,
1882 : char const * id_cstr,
1883 0 : fd_rpc_params_t const * params ) {
1884 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_EPOCH_INFO, 1UL );
1885 :
1886 0 : fd_http_server_response_t response;
1887 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 1, &response ) ) ) return response;
1888 :
1889 0 : ulong bank_idx = ULONG_MAX;
1890 0 : fd_rpc_val_t const * config = ¶ms->v[ 0 ];
1891 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcContextConfig",
1892 0 : 1, /* has_commitment */
1893 0 : 0, /* has_encoding */
1894 0 : 0, /* has_data_slice */
1895 0 : 1, /* has_min_context_slot */
1896 0 : &bank_idx,
1897 0 : NULL,
1898 0 : NULL,
1899 0 : NULL,
1900 0 : NULL,
1901 0 : &response );
1902 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
1903 :
1904 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":{\"absoluteSlot\":%lu,\"blockHeight\":%lu,\"epoch\":%lu,\"slotIndex\":%lu,\"slotsInEpoch\":%lu,\"transactionCount\":%lu},\"id\":%s}\n", ctx->banks[ bank_idx ].slot, ctx->banks[ bank_idx ].block_height, ctx->banks[ bank_idx ].epoch, ctx->banks[ bank_idx ].slot_in_epoch, ctx->banks[ bank_idx ].slots_per_epoch, ctx->banks[ bank_idx ].transaction_count, id_cstr );
1905 0 : }
1906 :
1907 : UNIMPLEMENTED(getEpochSchedule)
1908 : UNIMPLEMENTED(getFeeForMessage)
1909 : UNIMPLEMENTED(getFirstAvailableBlock) // TODO: Used by solana-exporter
1910 :
1911 : /* Get the genesis hash of the cluster. Firedancer deviates slightly
1912 : from Agave here, as the genesis hash is not always known when RPC
1913 : is first booted, it may need to be determined asynchronously in the
1914 : background, fetched from a peer node. If the genesis hash is not yet
1915 : known, we return an error indicating no snapshot is available. */
1916 :
1917 : static fd_http_server_response_t
1918 : getGenesisHash( fd_rpc_tile_t * ctx,
1919 : char const * id_cstr,
1920 0 : fd_rpc_params_t const * params ) {
1921 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_GENESIS_HASH, 1UL );
1922 :
1923 0 : fd_http_server_response_t response;
1924 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
1925 :
1926 0 : if( FD_UNLIKELY( !ctx->has_genesis_hash ) ) {
1927 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":%d,\"message\":\"Firedancer Error: No genesis hash\"},\"id\":%s}\n", FD_RPC_ERROR_NO_SNAPSHOT, id_cstr );
1928 0 : }
1929 :
1930 0 : FD_BASE58_ENCODE_32_BYTES( ctx->genesis_hash->uc, genesis_hash_b58 );
1931 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":\"%s\",\"id\":%s}\n", genesis_hash_b58, id_cstr );
1932 0 : }
1933 :
1934 : /* getAgGenesisCert returns the alpenglow genesis certificate, or null
1935 : if Tower consensus is active still.
1936 :
1937 : The account holds a wincode encoded WireBlockCertMessage,
1938 :
1939 : u64 block.slot
1940 : [u8;32] block.block_id
1941 : [u8;192] signature.signature (uncompressed BLS signature)
1942 : u64 signature.bitmap_len
1943 : [u8] signature.bitmap
1944 :
1945 : and the result is that struct as JSON, with the fixed size byte
1946 : arrays and the bitmap encoded as arrays of numbers, or null if the
1947 : account is empty or does not exist. */
1948 :
1949 0 : #define FD_RPC_AG_GENESIS_CERT_HDR_SZ (8UL+32UL+FD_BLS_SIG_SZ+8UL)
1950 :
1951 : static fd_http_server_response_t
1952 : getAgGenesisCert( fd_rpc_tile_t * ctx,
1953 : char const * id_cstr,
1954 0 : fd_rpc_params_t const * params ) {
1955 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_AG_GENESIS_CERT, 1UL );
1956 :
1957 0 : fd_http_server_response_t response;
1958 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
1959 :
1960 0 : if( FD_UNLIKELY( ctx->finalized_idx==ULONG_MAX ) ) {
1961 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32065,\"message\":\"Firedancer Error: banks uninitialized\"},\"id\":%s}\n", id_cstr );
1962 0 : }
1963 :
1964 0 : bank_info_t * info = &ctx->banks[ ctx->finalized_idx ];
1965 0 : ulong acct_lamports;
1966 0 : int acct_executable;
1967 0 : uchar acct_owner[ 32UL ];
1968 0 : ulong acct_data_len;
1969 0 : fd_accdb_read_one_nocache( ctx->accdb, info->accdb_fork_id, ctx->fd_genesis_cert_addr->uc,
1970 0 : &acct_lamports, &acct_executable, acct_owner,
1971 0 : ctx->scratch.accdb_data_buf, &acct_data_len );
1972 0 : if( FD_UNLIKELY( !acct_lamports || !acct_data_len ) ) {
1973 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":%s}\n", id_cstr );
1974 0 : }
1975 :
1976 0 : uchar const * data = ctx->scratch.accdb_data_buf;
1977 0 : ulong bitmap_sz = 0UL;
1978 0 : int malformed = acct_data_len<FD_RPC_AG_GENESIS_CERT_HDR_SZ;
1979 0 : if( FD_LIKELY( !malformed ) ) {
1980 0 : bitmap_sz = FD_LOAD( ulong, data+FD_RPC_AG_GENESIS_CERT_HDR_SZ-8UL );
1981 0 : malformed = bitmap_sz>acct_data_len-FD_RPC_AG_GENESIS_CERT_HDR_SZ;
1982 0 : }
1983 0 : if( FD_UNLIKELY( malformed ) ) {
1984 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32603,\"message\":\"Firedancer Error: malformed genesis certificate\"},\"id\":%s}\n", id_cstr );
1985 0 : }
1986 :
1987 0 : uchar const * block_id = data+8UL;
1988 0 : uchar const * signature = block_id+32UL;
1989 0 : uchar const * bitmap = data+FD_RPC_AG_GENESIS_CERT_HDR_SZ;
1990 :
1991 0 : fd_http_server_printf( ctx->http, "{\"jsonrpc\":\"2.0\",\"result\":{\"block\":{\"slot\":%lu,\"blockId\":[", FD_LOAD( ulong, data ) );
1992 0 : for( ulong i=0UL; i<32UL; i++ ) fd_http_server_printf( ctx->http, i ? ",%u" : "%u", (uint)block_id [ i ] );
1993 0 : fd_http_server_printf( ctx->http, "]},\"signature\":{\"signature\":[" );
1994 0 : for( ulong i=0UL; i<FD_BLS_SIG_SZ; i++ ) fd_http_server_printf( ctx->http, i ? ",%u" : "%u", (uint)signature[ i ] );
1995 0 : fd_http_server_printf( ctx->http, "],\"bitmap\":[" );
1996 0 : for( ulong i=0UL; i<bitmap_sz; i++ ) fd_http_server_printf( ctx->http, i ? ",%u" : "%u", (uint)bitmap [ i ] );
1997 0 : fd_http_server_printf( ctx->http, "]}},\"id\":%s}\n", id_cstr );
1998 0 : return STAGE_JSON( ctx );
1999 0 : }
2000 :
2001 : /* Determines if the node is healthy. Agave defines this as follows,
2002 :
2003 : - On boot, nodes must go through the entire snapshot slot database
2004 : and hash everything, and once it's done verify the hash matches.
2005 : While this is ongoing, the node is unhealthy with a "slotsBehind"
2006 : value of null.
2007 :
2008 : - On boot, if the cluster is restarting and we are currently waiting
2009 : for a supermajority of stake to join gossip to proceed with
2010 : booting, the node is forcibly marked as healthy, to, per Agave,
2011 :
2012 : > prevent load balancers from removing the node from their list
2013 : > of candidates during a manual restart
2014 :
2015 : - In addition, once booted, there is a period where we do not yet
2016 : know the cluster confirmed slot, because we have not yet observed
2017 : any (or enough) votes arrive from peers in the cluster. During
2018 : this period the node is unhealthy with a "slotsBehind" value of
2019 : null.
2020 :
2021 : - Finally, once the cluster confirmed slot is known, which is the
2022 : highest optimistically confirmed slot observed from both gossip,
2023 : and votes processed in blocks, it is compared to our own
2024 : optimistically confirmed slot, which is just the highest slot down
2025 : the cluster confirmed fork that we have finished replaying
2026 : locally. The difference between these two slots is compared, and
2027 : if it is less than or equal to 128, the node is healthy, otherwise
2028 : it is unhealthy with a "slotsBehind" value equal to the
2029 : difference.
2030 :
2031 : Under alpenglow there are no optimistic confirmations. The cluster
2032 : slot is instead the highest slot for which votor has observed a
2033 : finalization certificate (slow or fast), and the node stays Unknown
2034 : until it has seen one.
2035 :
2036 : Firedancer currently only implements the final two checks, and does
2037 : not forcibly mark the node as healthy while waiting for a
2038 : supermajority, nor does it mark a node as unhealthy while hashing the
2039 : snapshot database on boot. Firedancer hashes snapshots so quickly
2040 : that the node will die on boot if the hash is not valid. */
2041 :
2042 : static inline int
2043 0 : _getHealth( fd_rpc_tile_t * ctx ) {
2044 : /* Requests are not served until RPC has initialized banks */
2045 0 : if( FD_UNLIKELY( ctx->confirmed_idx==ULONG_MAX ) ) return FD_RPC_HEALTH_STATUS_UNKNOWN;
2046 0 : if( FD_UNLIKELY( ctx->cluster_confirmed_slot==ULONG_MAX ) ) return FD_RPC_HEALTH_STATUS_UNKNOWN;
2047 :
2048 0 : ulong slots_behind = fd_ulong_sat_sub( ctx->cluster_confirmed_slot, ctx->banks[ ctx->confirmed_idx ].slot );
2049 0 : if( FD_LIKELY( slots_behind<=FD_RPC_HEALTH_CHECK_SLOT_DISTANCE ) ) return FD_RPC_HEALTH_STATUS_OK;
2050 0 : else return FD_RPC_HEALTH_STATUS_BEHIND;
2051 0 : }
2052 :
2053 : static fd_http_server_response_t
2054 : getHealth( fd_rpc_tile_t * ctx,
2055 : char const * id_cstr,
2056 0 : fd_rpc_params_t const * params ) {
2057 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_HEALTH, 1UL );
2058 :
2059 0 : fd_http_server_response_t response;
2060 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
2061 :
2062 : // TODO: We should probably implement the same waiting_for_supermajority
2063 : // logic to conform with Agave here.
2064 :
2065 0 : int health_status = _getHealth( ctx );
2066 :
2067 0 : switch( health_status ) {
2068 0 : case FD_RPC_HEALTH_STATUS_UNKNOWN: return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":%d,\"message\":\"Node is unhealthy\",\"data\":{\"slotsBehind\":null}},\"id\":%s}\n", FD_RPC_ERROR_NODE_UNHEALTHY, id_cstr );
2069 0 : case FD_RPC_HEALTH_STATUS_BEHIND: return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":%d,\"message\":\"Node is unhealthy\",\"data\":{\"slotsBehind\":%lu}},\"id\":%s}\n", FD_RPC_ERROR_NODE_UNHEALTHY, fd_ulong_sat_sub( ctx->cluster_confirmed_slot, ctx->banks[ ctx->confirmed_idx ].slot ), id_cstr );
2070 0 : case FD_RPC_HEALTH_STATUS_OK: return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":\"ok\",\"id\":%s}\n", id_cstr );
2071 0 : default: FD_LOG_ERR(( "unknown health status" ));
2072 0 : }
2073 0 : }
2074 :
2075 : UNIMPLEMENTED(getHighestSnapshotSlot)
2076 :
2077 : static fd_http_server_response_t
2078 : getIdentity( fd_rpc_tile_t * ctx,
2079 : char const * id_cstr,
2080 0 : fd_rpc_params_t const * params ) {
2081 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_IDENTITY, 1UL );
2082 :
2083 0 : fd_http_server_response_t response;
2084 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
2085 :
2086 0 : FD_BASE58_ENCODE_32_BYTES( ctx->identity_pubkey, identity_pubkey_b58 );
2087 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":{\"identity\":\"%s\"},\"id\":%s}\n", identity_pubkey_b58, id_cstr );
2088 0 : }
2089 :
2090 : static fd_http_server_response_t
2091 : getInflationGovernor( fd_rpc_tile_t * ctx,
2092 : char const * id_cstr,
2093 0 : fd_rpc_params_t const * params ) {
2094 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_INFLATION_GOVERNOR, 1UL );
2095 :
2096 0 : fd_http_server_response_t response;
2097 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 1, &response ) ) ) return response;
2098 :
2099 0 : ulong bank_idx = ULONG_MAX;
2100 0 : fd_rpc_val_t const * config = ¶ms->v[ 0 ];
2101 0 : if( FD_UNLIKELY( !fd_rpc_validate_commitment_strict( ctx, id_cstr, config, &response ) ) ) return response;
2102 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct CommitmentConfig",
2103 0 : 1, /* has_commitment */
2104 0 : 0, /* has_encoding */
2105 0 : 0, /* has_data_slice */
2106 0 : 0, /* has_min_context_slot */
2107 0 : &bank_idx,
2108 0 : NULL,
2109 0 : NULL,
2110 0 : NULL,
2111 0 : NULL,
2112 0 : &response );
2113 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
2114 :
2115 0 : bank_info_t const * bank = &ctx->banks[ bank_idx ];
2116 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":{\"foundation\":%g%s,\"foundationTerm\":%g%s,\"initial\":%g%s,\"taper\":%g%s,\"terminal\":%g%s},\"id\":%s}\n",
2117 0 : bank->inflation.foundation, bank->inflation.foundation==0 ? ".0" : "",
2118 0 : bank->inflation.foundation_term, bank->inflation.foundation_term==0 ? ".0" : "",
2119 0 : bank->inflation.initial, bank->inflation.initial==0 ? ".0" : "",
2120 0 : bank->inflation.taper, bank->inflation.taper==0 ? ".0" : "",
2121 0 : bank->inflation.terminal, bank->inflation.terminal==0 ? ".0" : "",
2122 0 : id_cstr );
2123 0 : }
2124 :
2125 : UNIMPLEMENTED(getInflationRate)
2126 : UNIMPLEMENTED(getInflationReward) // TODO: Used by solana-exporter
2127 : UNIMPLEMENTED(getLargestAccounts)
2128 :
2129 : static fd_http_server_response_t
2130 : getLatestBlockhash( fd_rpc_tile_t * ctx,
2131 : char const * id_cstr,
2132 0 : fd_rpc_params_t const * params ) {
2133 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_LATEST_BLOCKHASH, 1UL );
2134 :
2135 0 : if( FD_UNLIKELY( ctx->processed_idx==ULONG_MAX ) ) {
2136 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32065,\"message\":\"Firedancer Error: banks uninitialized\"},\"id\":%s}\n", id_cstr );
2137 0 : }
2138 :
2139 0 : fd_http_server_response_t response;
2140 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 1, &response ) ) ) return response;
2141 :
2142 0 : ulong bank_idx = ULONG_MAX;
2143 0 : fd_rpc_val_t const * config = ¶ms->v[ 0 ];
2144 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcContextConfig",
2145 0 : 1, /* has_commitment */
2146 0 : 0, /* has_encoding */
2147 0 : 0, /* has_data_slice */
2148 0 : 1, /* has_min_context_slot */
2149 0 : &bank_idx,
2150 0 : NULL,
2151 0 : NULL,
2152 0 : NULL,
2153 0 : NULL,
2154 0 : &response );
2155 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
2156 :
2157 0 : bank_info_t * bank = &ctx->banks[ bank_idx ];
2158 0 : FD_BASE58_ENCODE_32_BYTES( bank->block_hash, block_hash_b58 );
2159 :
2160 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"slot\":%lu,\"apiVersion\":\"%s\"},\"value\":{\"blockhash\":\"%s\",\"lastValidBlockHeight\":%lu}},\"id\":%s}\n", bank->slot, FD_RPC_AGAVE_API_VERSION, block_hash_b58, bank->block_height + 150UL, id_cstr );
2161 0 : }
2162 :
2163 : /* Order getLeaderSchedule pairs by identity, breaking ties by rotation
2164 : so each identity's rotations end up ascending (matching Agave, which
2165 : folds the per-slot leaders into the result in slot order). */
2166 : #define SORT_NAME sort_gls_pairs_by_id
2167 0 : #define SORT_KEY_T fd_rpc_gls_pair_t
2168 0 : #define SORT_BEFORE(a,b) ( memcmp( (a).id_key.uc, (b).id_key.uc, sizeof(fd_pubkey_t) )<0 ? 1 : \
2169 0 : memcmp( (a).id_key.uc, (b).id_key.uc, sizeof(fd_pubkey_t) )>0 ? 0 : \
2170 0 : (a).rot<(b).rot )
2171 : #include "../../util/tmpl/fd_sort.c"
2172 :
2173 : static fd_http_server_response_t
2174 : getLeaderSchedule( fd_rpc_tile_t * ctx,
2175 : char const * id_cstr,
2176 0 : fd_rpc_params_t const * params ) {
2177 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_LEADER_SCHEDULE, 1UL );
2178 :
2179 0 : fd_http_server_response_t response;
2180 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 2, &response ) ) ) return response;
2181 :
2182 0 : fd_rpc_val_t const * p0 = ¶ms->v[ 0 ];
2183 0 : fd_rpc_val_t const * config = ¶ms->v[ 1 ];
2184 0 : ulong req_slot = 0UL;
2185 0 : int have_slot = 0;
2186 0 : int wrapper_config = 0;
2187 0 : if( p0->kind==FD_RPC_VAL_NONE || p0->kind==FD_RPC_VAL_NULL ) {
2188 : /* config stays the second param */
2189 0 : } else if( p0->kind==FD_RPC_VAL_INT && !p0->neg ) {
2190 0 : req_slot = p0->u;
2191 0 : have_slot = 1;
2192 0 : } else if( p0->kind==FD_RPC_VAL_MAP ) {
2193 0 : config = p0;
2194 0 : wrapper_config = 1;
2195 :
2196 : /* Clients can pass a second config object, which also needs
2197 : validation. */
2198 0 : fd_rpc_val_t const * fallback_config = ¶ms->v[ 1 ];
2199 0 : if( fallback_config->kind!=FD_RPC_VAL_NONE ) {
2200 0 : ulong fallback_bank_idx = ULONG_MAX;
2201 0 : fd_rpc_val_t fallback_identity[1];
2202 0 : int fallback_config_valid = fd_rpc_validate_config( ctx, id_cstr, fallback_config, "struct RpcLeaderScheduleConfig",
2203 0 : 1, /* has_commitment */
2204 0 : 0, /* has_encoding */
2205 0 : 0, /* has_data_slice */
2206 0 : 0, /* has_min_context_slot */
2207 0 : &fallback_bank_idx,
2208 0 : NULL,
2209 0 : NULL,
2210 0 : NULL,
2211 0 : fallback_identity,
2212 0 : &response );
2213 0 : if( FD_UNLIKELY( !fallback_config_valid ) ) return response;
2214 :
2215 0 : if( FD_UNLIKELY( fallback_identity->kind!=FD_RPC_VAL_NONE && fallback_identity->kind!=FD_RPC_VAL_NULL && fallback_identity->kind!=FD_RPC_VAL_STR ) ) {
2216 0 : return fd_rpc_err_invalid( ctx, id_cstr, "type", fallback_identity, "a string" );
2217 0 : }
2218 0 : }
2219 0 : } else {
2220 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: data did not match any variant of untagged enum RpcLeaderScheduleConfigWrapper.\"},\"id\":%s}\n", id_cstr );
2221 0 : }
2222 :
2223 0 : ulong bank_idx = ULONG_MAX;
2224 0 : fd_rpc_val_t identity[1];
2225 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcLeaderScheduleConfig",
2226 0 : 1, /* has_commitment */
2227 0 : 0, /* has_encoding */
2228 0 : 0, /* has_data_slice */
2229 0 : 0, /* has_min_context_slot */
2230 0 : &bank_idx,
2231 0 : NULL,
2232 0 : NULL,
2233 0 : NULL,
2234 0 : identity,
2235 0 : &response );
2236 0 : if( FD_UNLIKELY( !config_valid ) ) {
2237 0 : if( !wrapper_config ) return response;
2238 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: data did not match any variant of untagged enum RpcLeaderScheduleConfigWrapper.\"},\"id\":%s}\n", id_cstr );
2239 0 : }
2240 :
2241 : /* Optional identity filter: restrict the result to a single validator. */
2242 0 : int have_identity = 0;
2243 0 : fd_pubkey_t identity_filter[ 1 ];
2244 0 : if( identity->kind!=FD_RPC_VAL_NONE && identity->kind!=FD_RPC_VAL_NULL ) {
2245 0 : if( FD_UNLIKELY( wrapper_config && identity->kind!=FD_RPC_VAL_STR ) ) {
2246 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: data did not match any variant of untagged enum RpcLeaderScheduleConfigWrapper.\"},\"id\":%s}\n", id_cstr );
2247 0 : }
2248 0 : if( FD_UNLIKELY( !fd_rpc_validate_address( ctx, id_cstr, identity, identity_filter, &response ) ) ) return response;
2249 0 : have_identity = 1;
2250 0 : }
2251 :
2252 0 : ulong slot = have_slot ? req_slot : ctx->banks[ bank_idx ].slot;
2253 0 : fd_epoch_leaders_t const * lsched = fd_rpc_mleaders_get_lsched_for_slot( ctx->mleaders, slot );
2254 0 : if( FD_UNLIKELY( !lsched ) ) {
2255 : /* Unlike getSlotLeader(s), Agave returns a null result (not an
2256 : error) when the requested epoch's schedule is unavailable. */
2257 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":%s}\n", id_cstr );
2258 0 : }
2259 :
2260 0 : ulong pub_cnt = lsched->pub_cnt;
2261 0 : ulong sched_cnt = lsched->sched_cnt;
2262 0 : ulong slot_cnt = lsched->slot_cnt;
2263 0 : FD_TEST( pub_cnt<=MAX_STAKE_WEIGHTS );
2264 0 : FD_TEST( sched_cnt<=(sizeof(ctx->scratch.gls_pairs)/sizeof(ctx->scratch.gls_pairs[0])) );
2265 :
2266 0 : fd_rpc_gls_pair_t * pairs = ctx->scratch.gls_pairs;
2267 0 : ulong pair_cnt = 0UL;
2268 0 : for( ulong r=0UL; r<sched_cnt; r++ ) {
2269 0 : ulong idx = lsched->sched[ r ];
2270 0 : if( FD_UNLIKELY( idx>=pub_cnt ) ) continue; /* indeterminate leader; not reported */
2271 0 : if( have_identity && 0!=memcmp( lsched->pub[ idx ].uc, identity_filter->uc, sizeof(fd_pubkey_t) ) ) continue;
2272 0 : pairs[ pair_cnt ].id_key = lsched->pub[ idx ];
2273 0 : pairs[ pair_cnt ].rot = (uint)r;
2274 0 : pair_cnt++;
2275 0 : }
2276 0 : sort_gls_pairs_by_id_inplace( pairs, pair_cnt );
2277 :
2278 0 : fd_http_server_printf( ctx->http, "{\"jsonrpc\":\"2.0\",\"result\":{" );
2279 0 : int first = 1;
2280 0 : for( ulong i=0UL; i<pair_cnt; ) {
2281 : /* [i,j) is the run of pairs sharing pairs[i].id_key (one identity). */
2282 0 : ulong j = i+1UL;
2283 0 : while( j<pair_cnt && 0==memcmp( pairs[ j ].id_key.uc, pairs[ i ].id_key.uc, sizeof(fd_pubkey_t) ) ) j++;
2284 :
2285 0 : FD_BASE58_ENCODE_32_BYTES( pairs[ i ].id_key.uc, pub_b58 );
2286 0 : fd_http_server_printf( ctx->http, "%s\"%s\":[", first ? "" : ",", pub_b58 );
2287 0 : first = 0;
2288 :
2289 0 : int first_slot = 1;
2290 0 : for( ulong b=i; b<j; b++ ) {
2291 0 : ulong r = pairs[ b ].rot;
2292 0 : for( ulong k=0UL; k<FD_EPOCH_SLOTS_PER_ROTATION; k++ ) {
2293 0 : ulong rel = r*FD_EPOCH_SLOTS_PER_ROTATION + k;
2294 0 : if( FD_UNLIKELY( rel>=slot_cnt ) ) break;
2295 0 : fd_http_server_printf( ctx->http, "%s%lu", first_slot ? "" : ",", rel );
2296 0 : first_slot = 0;
2297 0 : }
2298 0 : }
2299 0 : fd_http_server_printf( ctx->http, "]" );
2300 0 : i = j;
2301 0 : }
2302 0 : fd_http_server_printf( ctx->http, "},\"id\":%s}\n", id_cstr );
2303 0 : return STAGE_JSON( ctx );
2304 0 : }
2305 :
2306 : static fd_http_server_response_t
2307 : getMaxRetransmitSlot( fd_rpc_tile_t * ctx,
2308 : char const * id_cstr,
2309 0 : fd_rpc_params_t const * params ) {
2310 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_MAX_RETRANSMIT_SLOT, 1UL );
2311 :
2312 0 : fd_http_server_response_t response;
2313 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
2314 :
2315 0 : ulong max_retransmit_slot = 0UL;
2316 0 : for( ulong i=0UL; i<ctx->shred_rtx_cnt; i++ ) {
2317 0 : ulong slot = fd_fseq_query( ctx->shred_rtx[ i ] );
2318 0 : if( FD_LIKELY( slot!=ULONG_MAX ) ) max_retransmit_slot = fd_ulong_max( max_retransmit_slot, slot );
2319 0 : }
2320 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}\n", max_retransmit_slot, id_cstr );
2321 0 : }
2322 :
2323 : UNIMPLEMENTED(getMaxShredInsertSlot)
2324 :
2325 : static fd_http_server_response_t
2326 : getMinimumBalanceForRentExemption( fd_rpc_tile_t * ctx,
2327 : char const * id_cstr,
2328 0 : fd_rpc_params_t const * params ) {
2329 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_MINIMUM_BALANCE_FOR_RENT_EXEMPTION, 1UL );
2330 :
2331 0 : fd_http_server_response_t response;
2332 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 1, 2, &response ) ) ) return response;
2333 :
2334 0 : ulong bank_idx = ULONG_MAX;
2335 0 : fd_rpc_val_t const * config = ¶ms->v[ 1 ];
2336 0 : if( FD_UNLIKELY( !fd_rpc_validate_commitment_strict( ctx, id_cstr, config, &response ) ) ) return response;
2337 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct CommitmentConfig",
2338 0 : 1, /* has_commitment */
2339 0 : 0, /* has_encoding */
2340 0 : 0, /* has_data_slice */
2341 0 : 0, /* has_min_context_slot */
2342 0 : &bank_idx,
2343 0 : NULL,
2344 0 : NULL,
2345 0 : NULL,
2346 0 : NULL,
2347 0 : &response );
2348 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
2349 :
2350 0 : fd_rpc_val_t const * acct_sz = ¶ms->v[ 0 ];
2351 0 : ulong acct_sz_val;
2352 0 : if( FD_UNLIKELY( !fd_rpc_validate_uint_param( ctx, id_cstr, acct_sz, "usize", &acct_sz_val, &response ) ) ) return response;
2353 0 : if( FD_UNLIKELY( acct_sz_val>FD_RUNTIME_ACC_SZ_MAX ) ) {
2354 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"Invalid request\"},\"id\":%s}\n", id_cstr );
2355 0 : }
2356 :
2357 0 : bank_info_t const * bank = &ctx->banks[ bank_idx ];
2358 :
2359 0 : fd_rent_t rent = {
2360 0 : .lamports_per_uint8_year = bank->rent.lamports_per_uint8_year,
2361 0 : .exemption_threshold = bank->rent.exemption_threshold,
2362 0 : .burn_percent = bank->rent.burn_percent,
2363 0 : };
2364 0 : ulong minimum = fd_rent_exempt_minimum_balance( &rent, acct_sz_val );
2365 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}\n", minimum, id_cstr );
2366 0 : }
2367 :
2368 : static fd_http_server_response_t
2369 : getMultipleAccounts( fd_rpc_tile_t * ctx,
2370 : char const * id_cstr,
2371 0 : fd_rpc_params_t const * params ) {
2372 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_MULTIPLE_ACCOUNTS, 1UL );
2373 :
2374 0 : fd_http_server_response_t response;
2375 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 1, 2, &response ) ) ) return response;
2376 :
2377 0 : fd_rpc_val_t const * keys_arr = ¶ms->v[ 0 ];
2378 0 : if( FD_UNLIKELY( keys_arr->kind!=FD_RPC_VAL_SEQ ) ) {
2379 0 : return fd_rpc_err_invalid( ctx, id_cstr, "type", keys_arr, "a sequence" );
2380 0 : }
2381 :
2382 0 : fd_rpc_val_t keys[ 100UL ];
2383 0 : ulong cnt = 0UL;
2384 0 : fd_jtok_t j[1];
2385 0 : fd_jtok_arr_enter( fd_jtok_init( j, keys_arr->raw, keys_arr->raw_sz ) );
2386 0 : while( fd_jtok_arr_next( j ) ) {
2387 0 : if( cnt<100UL ) fd_rpc_val_read( j, &keys[ cnt ] );
2388 0 : cnt++;
2389 0 : }
2390 0 : if( FD_UNLIKELY( cnt>100UL ) ) {
2391 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Too many inputs provided; max 100\"},\"id\":%s}\n", id_cstr );
2392 0 : }
2393 :
2394 0 : ulong bank_idx = ULONG_MAX;
2395 0 : char const * encoding_cstr = NULL;
2396 0 : ulong slice_length = ULONG_MAX;
2397 0 : ulong slice_offset = 0;
2398 0 : fd_rpc_val_t const * config = ¶ms->v[ 1 ];
2399 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcAccountInfoConfig",
2400 0 : 1, 1, 1, 1,
2401 0 : &bank_idx, &encoding_cstr,
2402 0 : &slice_length, &slice_offset,
2403 0 : NULL,
2404 0 : &response );
2405 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
2406 :
2407 : /* Agave defaults getMultipleAccounts to base64 (getAccountInfo to binary) */
2408 0 : fd_rpc_config_t cfg[1];
2409 0 : fd_rpc_config_read( config, cfg );
2410 0 : if( cfg->encoding.kind==FD_RPC_VAL_NONE || cfg->encoding.kind==FD_RPC_VAL_NULL ) encoding_cstr = "base64";
2411 :
2412 0 : bank_info_t * info = &ctx->banks[ bank_idx ];
2413 :
2414 0 : fd_pubkey_t addresses[ 100UL ];
2415 0 : for( ulong i=0UL; i<cnt; i++ ) {
2416 0 : if( FD_UNLIKELY( !fd_rpc_validate_address( ctx, id_cstr, &keys[ i ], &addresses[ i ], &response ) ) ) return response;
2417 0 : }
2418 :
2419 0 : fd_http_server_printf( ctx->http,
2420 0 : "{\"jsonrpc\":\"2.0\",\"id\":%s,\"result\":{\"context\":{\"apiVersion\":\"%s\",\"slot\":%lu},\"value\":[",
2421 0 : id_cstr, FD_RPC_AGAVE_API_VERSION, info->slot );
2422 :
2423 0 : for( ulong i=0UL; i<cnt; i++ ) {
2424 0 : if( i>0UL ) fd_http_server_printf( ctx->http, "," );
2425 :
2426 0 : ulong acct_lamports;
2427 0 : int acct_executable;
2428 0 : uchar acct_owner[ 32UL ];
2429 0 : ulong acct_data_len;
2430 0 : fd_accdb_read_one_nocache( ctx->accdb, info->accdb_fork_id, addresses[i].uc,
2431 0 : &acct_lamports, &acct_executable, acct_owner,
2432 0 : ctx->scratch.accdb_data_buf, &acct_data_len );
2433 0 : if( FD_UNLIKELY( !acct_lamports ) ) {
2434 0 : fd_http_server_printf( ctx->http, "null" );
2435 0 : continue;
2436 0 : }
2437 :
2438 0 : fd_http_server_response_t err_response;
2439 0 : if( FD_UNLIKELY( !fd_rpc_encode_account_data( ctx, ctx->scratch.accdb_data_buf, acct_data_len, acct_owner, acct_lamports, acct_executable, encoding_cstr, slice_offset, slice_length, id_cstr, &err_response ) ) ) {
2440 0 : return err_response;
2441 0 : }
2442 0 : }
2443 :
2444 0 : fd_http_server_printf( ctx->http, "]}}\n" );
2445 0 : return STAGE_JSON( ctx );
2446 0 : }
2447 :
2448 : UNIMPLEMENTED(getProgramAccounts)
2449 : UNIMPLEMENTED(getRecentPerformanceSamples)
2450 : UNIMPLEMENTED(getRecentPrioritizationFees)
2451 : UNIMPLEMENTED(getSignaturesForAddress)
2452 : UNIMPLEMENTED(getSignatureStatuses)
2453 :
2454 : static fd_http_server_response_t
2455 : getSlot( fd_rpc_tile_t * ctx,
2456 : char const * id_cstr,
2457 0 : fd_rpc_params_t const * params ) {
2458 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_SLOT, 1UL );
2459 :
2460 0 : fd_http_server_response_t response;
2461 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 1, &response ) ) ) return response;
2462 :
2463 0 : ulong bank_idx = ULONG_MAX;
2464 0 : fd_rpc_val_t const * config = ¶ms->v[ 0 ];
2465 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcContextConfig",
2466 0 : 1, /* has_commitment */
2467 0 : 0, /* has_encoding */
2468 0 : 0, /* has_data_slice */
2469 0 : 1, /* has_min_context_slot */
2470 0 : &bank_idx,
2471 0 : NULL,
2472 0 : NULL,
2473 0 : NULL,
2474 0 : NULL,
2475 0 : &response );
2476 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
2477 :
2478 0 : bank_info_t * bank = &ctx->banks[ bank_idx ];
2479 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}\n", bank->slot, id_cstr );
2480 0 : }
2481 :
2482 : static fd_http_server_response_t
2483 : getSlotLeader( fd_rpc_tile_t * ctx,
2484 : char const * id_cstr,
2485 0 : fd_rpc_params_t const * params ) {
2486 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_SLOT_LEADER, 1UL );
2487 :
2488 0 : fd_http_server_response_t response;
2489 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 1, &response ) ) ) return response;
2490 :
2491 0 : ulong bank_idx = ULONG_MAX;
2492 0 : fd_rpc_val_t const * config = ¶ms->v[ 0 ];
2493 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcContextConfig",
2494 0 : 1, /* has_commitment */
2495 0 : 0, /* has_encoding */
2496 0 : 0, /* has_data_slice */
2497 0 : 1, /* has_min_context_slot */
2498 0 : &bank_idx,
2499 0 : NULL,
2500 0 : NULL,
2501 0 : NULL,
2502 0 : NULL,
2503 0 : &response );
2504 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
2505 :
2506 0 : ulong slot = ctx->banks[ bank_idx ].slot;
2507 0 : fd_pubkey_t const * leader = fd_rpc_mleaders_get_leader_for_slot( ctx->mleaders, slot );
2508 0 : if( FD_UNLIKELY( !leader ) ) {
2509 0 : ulong epoch = ctx->has_epoch_schedule ? fd_slot_to_epoch( &ctx->epoch_schedule, slot, NULL ) : 0UL;
2510 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid slot range: leader schedule for epoch %lu is unavailable\"},\"id\":%s}\n", epoch, id_cstr );
2511 0 : }
2512 :
2513 0 : FD_BASE58_ENCODE_32_BYTES( leader->uc, leader_b58 );
2514 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":\"%s\",\"id\":%s}\n", leader_b58, id_cstr );
2515 0 : }
2516 :
2517 : static fd_http_server_response_t
2518 : getSlotLeaders( fd_rpc_tile_t * ctx,
2519 : char const * id_cstr,
2520 0 : fd_rpc_params_t const * params ) {
2521 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_SLOT_LEADERS, 1UL );
2522 :
2523 0 : fd_http_server_response_t response;
2524 0 : if( FD_UNLIKELY( params->kind==FD_RPC_VAL_NULL ) ) {
2525 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: invalid type: null, expected a tuple of size 2.\"},\"id\":%s}\n", id_cstr );
2526 0 : }
2527 0 : if( FD_UNLIKELY( params->kind==FD_RPC_VAL_MAP ) ) {
2528 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: invalid type: map, expected a tuple of size 2.\"},\"id\":%s}\n", id_cstr );
2529 0 : }
2530 0 : fd_rpc_val_t const * p0 = ¶ms->v[ 0 ];
2531 0 : fd_rpc_val_t const * p1 = ¶ms->v[ 1 ];
2532 0 : if( FD_UNLIKELY( params->kind==FD_RPC_VAL_SEQ && params->cnt!=2UL ) ) {
2533 0 : ulong param_cnt = params->cnt;
2534 0 : ulong unused;
2535 0 : if( FD_UNLIKELY( param_cnt>=1UL && !fd_rpc_validate_uint_param( ctx, id_cstr, p0, "u64", &unused, &response ) ) ) return response;
2536 0 : if( FD_UNLIKELY( param_cnt>=2UL && !fd_rpc_validate_uint_param( ctx, id_cstr, p1, "u64", &unused, &response ) ) ) return response;
2537 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: invalid length %lu, expected a tuple of size 2.\"},\"id\":%s}\n", param_cnt, id_cstr );
2538 0 : }
2539 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 2, 2, &response ) ) ) return response;
2540 :
2541 0 : ulong start_slot;
2542 0 : ulong limit;
2543 0 : if( FD_UNLIKELY( !fd_rpc_validate_uint_param( ctx, id_cstr, p0, "u64", &start_slot, &response ) ) ) return response;
2544 0 : if( FD_UNLIKELY( !fd_rpc_validate_uint_param( ctx, id_cstr, p1, "u64", &limit, &response ) ) ) return response;
2545 :
2546 0 : if( FD_UNLIKELY( limit>5000UL ) ) {
2547 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid limit; max 5000\"},\"id\":%s}\n", id_cstr );
2548 0 : }
2549 :
2550 0 : fd_http_server_printf( ctx->http, "{\"jsonrpc\":\"2.0\",\"result\":[" );
2551 0 : for( ulong i=0UL; i<limit; i++ ) {
2552 0 : ulong slot = start_slot + i;
2553 0 : fd_pubkey_t const * leader = FD_LIKELY( slot>=start_slot ) ? fd_rpc_mleaders_get_leader_for_slot( ctx->mleaders, slot ) : NULL;
2554 0 : if( FD_UNLIKELY( !leader ) ) {
2555 0 : fd_http_server_unstage( ctx->http );
2556 0 : ulong epoch = ctx->has_epoch_schedule ? fd_slot_to_epoch( &ctx->epoch_schedule, slot, NULL ) : 0UL;
2557 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid slot range: leader schedule for epoch %lu is unavailable\"},\"id\":%s}\n", epoch, id_cstr );
2558 0 : }
2559 0 : FD_BASE58_ENCODE_32_BYTES( leader->uc, leader_b58 );
2560 0 : fd_http_server_printf( ctx->http, "%s\"%s\"", i ? "," : "", leader_b58 );
2561 0 : }
2562 0 : fd_http_server_printf( ctx->http, "],\"id\":%s}\n", id_cstr );
2563 0 : return STAGE_JSON( ctx );
2564 0 : }
2565 : UNIMPLEMENTED(getStakeMinimumDelegation)
2566 : UNIMPLEMENTED(getSupply)
2567 : UNIMPLEMENTED(getTokenAccountBalance)
2568 : UNIMPLEMENTED(getTokenAccountsByDelegate)
2569 : UNIMPLEMENTED(getTokenAccountsByOwner)
2570 : UNIMPLEMENTED(getTokenLargestAccounts)
2571 : UNIMPLEMENTED(getTokenSupply)
2572 : UNIMPLEMENTED(getTransaction)
2573 :
2574 : static fd_http_server_response_t
2575 : getTransactionCount( fd_rpc_tile_t * ctx,
2576 : char const * id_cstr,
2577 0 : fd_rpc_params_t const * params ) {
2578 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_TRANSACTION_COUNT, 1UL );
2579 :
2580 0 : fd_http_server_response_t response;
2581 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 1, &response ) ) ) return response;
2582 :
2583 0 : ulong bank_idx = ULONG_MAX;
2584 0 : fd_rpc_val_t const * config = ¶ms->v[ 0 ];
2585 0 : int config_valid = fd_rpc_validate_config( ctx, id_cstr, config, "struct RpcContextConfig",
2586 0 : 1, /* has_commitment */
2587 0 : 0, /* has_encoding */
2588 0 : 0, /* has_data_slice */
2589 0 : 1, /* has_min_context_slot */
2590 0 : &bank_idx,
2591 0 : NULL,
2592 0 : NULL,
2593 0 : NULL,
2594 0 : NULL,
2595 0 : &response );
2596 0 : if( FD_UNLIKELY( !config_valid ) ) return response;
2597 :
2598 0 : bank_info_t * bank = &ctx->banks[ bank_idx ];
2599 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}\n", bank->transaction_count, id_cstr );
2600 0 : }
2601 :
2602 : static fd_http_server_response_t
2603 : getVersion( fd_rpc_tile_t * ctx,
2604 : char const * id_cstr,
2605 0 : fd_rpc_params_t const * params ) {
2606 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GET_VERSION, 1UL );
2607 :
2608 0 : fd_http_server_response_t response;
2609 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
2610 :
2611 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":{\"solana-core\":\"%s\",\"feature-set\":%u},\"id\":%s}\n", fd_version_cstr, FD_FEATURE_SET_ID, id_cstr );
2612 0 : }
2613 :
2614 : static fd_http_server_response_t
2615 : voteSubscribe( fd_rpc_tile_t * ctx,
2616 : char const * id_cstr,
2617 : fd_rpc_params_t const * params,
2618 0 : ulong ws_conn_id ) {
2619 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_VOTE_SUBSCRIBE, 1UL );
2620 :
2621 0 : fd_http_server_response_t response;
2622 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
2623 :
2624 0 : if( FD_UNLIKELY( ws_conn_id==ULONG_MAX ) ) {
2625 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32601,\"message\":\"Method not found\"},\"id\":%s}\n", id_cstr );
2626 0 : }
2627 0 : FD_CHECK_CRIT( ws_conn_id < ctx->http->max_ws_conns, "OOB ws_conn_id" );
2628 :
2629 0 : fd_rpc_ws_subscriber_vote_add( ctx, ws_conn_id );
2630 :
2631 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}\n", ctx->ws_sub_id_vote, id_cstr );
2632 0 : }
2633 :
2634 : static fd_http_server_response_t
2635 : slotSubscribe( fd_rpc_tile_t * ctx,
2636 : char const * id_cstr,
2637 : fd_rpc_params_t const * params,
2638 0 : ulong ws_conn_id ) {
2639 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_SLOT_SUBSCRIBE, 1UL );
2640 :
2641 0 : fd_http_server_response_t response;
2642 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 0, 0, &response ) ) ) return response;
2643 :
2644 0 : if( FD_UNLIKELY( ws_conn_id==ULONG_MAX ) ) {
2645 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32601,\"message\":\"Method not found\"},\"id\":%s}\n", id_cstr );
2646 0 : }
2647 0 : FD_CHECK_CRIT( ws_conn_id < ctx->http->max_ws_conns, "OOB ws_conn_id" );
2648 :
2649 0 : fd_rpc_ws_subscriber_slot_add( ctx, ws_conn_id );
2650 :
2651 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}\n", ctx->ws_sub_id_slot, id_cstr );
2652 0 : }
2653 :
2654 : static fd_http_server_response_t
2655 : voteUnsubscribe( fd_rpc_tile_t * ctx,
2656 : char const * id_cstr,
2657 : fd_rpc_params_t const * params,
2658 0 : ulong ws_conn_id ) {
2659 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_VOTE_UNSUBSCRIBE, 1UL );
2660 :
2661 0 : fd_http_server_response_t response;
2662 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 1, 1, &response ) ) ) return response;
2663 :
2664 0 : if( FD_UNLIKELY( ws_conn_id==ULONG_MAX ) ) {
2665 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32601,\"message\":\"Method not found\"},\"id\":%s}\n", id_cstr );
2666 0 : }
2667 0 : FD_CHECK_CRIT( ws_conn_id < ctx->http->max_ws_conns, "OOB ws_conn_id" );
2668 :
2669 0 : fd_rpc_val_t const * subscription = ¶ms->v[ 0 ];
2670 0 : if( FD_UNLIKELY( subscription->kind!=FD_RPC_VAL_INT || subscription->neg ) ) {
2671 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: invalid type: %s, expected usize.\"},\"id\":%s}\n", fd_rpc_val_kind_cstr( subscription ), id_cstr );
2672 0 : }
2673 :
2674 0 : if( FD_UNLIKELY( !fd_rpc_ws_unsubscribe( ctx, subscription->u, ws_conn_id ) ) ) {
2675 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid subscription id.\"},\"id\":%s}\n", id_cstr );
2676 0 : }
2677 :
2678 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":true,\"id\":%s}\n", id_cstr );
2679 0 : }
2680 :
2681 : static fd_http_server_response_t
2682 : slotUnsubscribe( fd_rpc_tile_t * ctx,
2683 : char const * id_cstr,
2684 : fd_rpc_params_t const * params,
2685 0 : ulong ws_conn_id ) {
2686 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_SLOT_UNSUBSCRIBE, 1UL );
2687 :
2688 0 : fd_http_server_response_t response;
2689 0 : if( FD_UNLIKELY( !fd_rpc_validate_params( ctx, id_cstr, params, 1, 1, &response ) ) ) return response;
2690 :
2691 0 : if( FD_UNLIKELY( ws_conn_id==ULONG_MAX ) ) {
2692 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32601,\"message\":\"Method not found\"},\"id\":%s}\n", id_cstr );
2693 0 : }
2694 0 : FD_CHECK_CRIT( ws_conn_id < ctx->http->max_ws_conns, "OOB ws_conn_id" );
2695 :
2696 0 : fd_rpc_val_t const * subscription = ¶ms->v[ 0 ];
2697 0 : if( FD_UNLIKELY( subscription->kind!=FD_RPC_VAL_INT || subscription->neg ) ) {
2698 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid params: invalid type: %s, expected usize.\"},\"id\":%s}\n", fd_rpc_val_kind_cstr( subscription ), id_cstr );
2699 0 : }
2700 :
2701 0 : if( FD_UNLIKELY( !fd_rpc_ws_unsubscribe( ctx, subscription->u, ws_conn_id ) ) ) {
2702 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32602,\"message\":\"Invalid subscription id.\"},\"id\":%s}\n", id_cstr );
2703 0 : }
2704 :
2705 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"result\":true,\"id\":%s}\n", id_cstr );
2706 0 : }
2707 :
2708 : UNIMPLEMENTED(getVoteAccounts) // TODO: Used by solana-exporter
2709 : UNIMPLEMENTED(isBlockhashValid)
2710 : UNIMPLEMENTED(minimumLedgerSlot) // TODO: Used by solana-exporter
2711 : UNIMPLEMENTED(requestAirdrop)
2712 : UNIMPLEMENTED(sendTransaction)
2713 : UNIMPLEMENTED(simulateTransaction)
2714 :
2715 : static fd_http_server_response_t
2716 : rpc_json_request( fd_rpc_tile_t * ctx,
2717 : uchar const * body,
2718 : ulong body_len,
2719 : ulong ws_conn_id );
2720 :
2721 : static fd_http_server_response_t
2722 : rpc_http_request1( fd_rpc_tile_t * ctx,
2723 0 : fd_http_server_request_t const * request ) {
2724 0 : if( FD_UNLIKELY( request->method==FD_HTTP_SERVER_METHOD_GET &&
2725 0 : request->headers.upgrade_websocket &&
2726 0 : (!strcmp( request->path, "/" ) || !strcmp( request->path, "/websocket" )) ) ) {
2727 0 : if( FD_UNLIKELY( !ctx->http->max_ws_conns ) ) return (fd_http_server_response_t){ .status = 404 };
2728 0 : return (fd_http_server_response_t){
2729 0 : .status = 200,
2730 0 : .upgrade_websocket = 1,
2731 0 : };
2732 0 : }
2733 :
2734 0 : if( FD_UNLIKELY( ( request->method==FD_HTTP_SERVER_METHOD_GET || request->method==FD_HTTP_SERVER_METHOD_HEAD ) && !strcmp( request->path, "/health" ) ) ) {
2735 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_HEALTH, 1UL );
2736 0 : int health_status = _getHealth( ctx );
2737 :
2738 0 : switch( health_status ) {
2739 0 : case FD_RPC_HEALTH_STATUS_UNKNOWN: return PRINTF_JSON( ctx, "unknown" );
2740 0 : case FD_RPC_HEALTH_STATUS_BEHIND: return PRINTF_JSON( ctx, "behind" );
2741 0 : case FD_RPC_HEALTH_STATUS_OK: return PRINTF_JSON( ctx, "ok" );
2742 0 : default: FD_LOG_ERR(( "unknown health status" ));
2743 0 : }
2744 0 : }
2745 :
2746 0 : if( FD_UNLIKELY( ( request->method==FD_HTTP_SERVER_METHOD_GET || request->method==FD_HTTP_SERVER_METHOD_HEAD ) && !strcmp( request->path, "/genesis.tar.bz2" ) ) ) {
2747 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_GENESIS, 1UL );
2748 0 : if( FD_UNLIKELY( ctx->genesis_tar_bz_sz==ULONG_MAX ) ) return (fd_http_server_response_t){ .status = 404 };
2749 :
2750 0 : return (fd_http_server_response_t) {
2751 0 : .status = 200,
2752 0 : .static_body = ctx->genesis_tar_bz,
2753 0 : .static_body_len = ctx->genesis_tar_bz_sz,
2754 0 : };
2755 0 : }
2756 :
2757 0 : if( FD_UNLIKELY( ( request->method==FD_HTTP_SERVER_METHOD_GET || request->method==FD_HTTP_SERVER_METHOD_HEAD ) &&
2758 0 : ( !strncmp( request->path, "/snapshot", 9UL ) ||
2759 0 : !strncmp( request->path, "/incremental-snapshot", 21UL ) ) ) ) {
2760 0 : if( FD_UNLIKELY( !ctx->snapshot_server_enabled ) ) {
2761 0 : return (fd_http_server_response_t){ .status = 403 }; /* forbidden */
2762 0 : }
2763 0 : return (fd_http_server_response_t){
2764 0 : .status = 302,
2765 0 : .location = { ctx->snapshot_server_url, request->path_raw },
2766 0 : .location_len = { strlen( ctx->snapshot_server_url ), request->path_len },
2767 0 : };
2768 0 : }
2769 :
2770 :
2771 0 : if( FD_UNLIKELY( request->method==FD_HTTP_SERVER_METHOD_GET || request->method==FD_HTTP_SERVER_METHOD_HEAD ) ) {
2772 0 : return (fd_http_server_response_t){ .status = 404 };
2773 0 : }
2774 :
2775 0 : if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_POST ) ) {
2776 0 : return (fd_http_server_response_t){ .status = 405 };
2777 0 : }
2778 :
2779 0 : return rpc_json_request( ctx, request->post.body, request->post.body_len, ULONG_MAX );
2780 0 : }
2781 :
2782 : static fd_http_server_response_t
2783 : rpc_json_request( fd_rpc_tile_t * ctx,
2784 : uchar const * body,
2785 : ulong body_len,
2786 0 : ulong ws_conn_id ) { /* ULONG_MAX implies HTTP */
2787 0 : fd_jtok_t j[1];
2788 0 : fd_jtok_init( j, body, body_len );
2789 0 : int kind = fd_jtok_peek( j );
2790 :
2791 0 : if( FD_UNLIKELY( kind==FD_JTOK_ARR ) ) {
2792 : /* A bug in Agave ¯\_(ツ)_/¯ */
2793 0 : fd_jtok_arr_enter( j );
2794 0 : if( FD_LIKELY( !fd_jtok_arr_next( j ) && !fd_jtok_fini( j ) ) ) return (fd_http_server_response_t){ .content_type = "application/json", .status = 200 };
2795 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2796 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32700,\"message\":\"Parse error\"},\"id\":null}\n" );
2797 0 : }
2798 :
2799 0 : if( FD_UNLIKELY( kind!=FD_JTOK_OBJ ) ) {
2800 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2801 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32700,\"message\":\"Parse error\"},\"id\":null}\n" );
2802 0 : }
2803 :
2804 : /* For duplicate members the first occurrence wins, later ones are
2805 : skipped. */
2806 0 : fd_rpc_val_t id [1] = {{ .kind = FD_RPC_VAL_NONE }};
2807 0 : fd_rpc_val_t jsonrpc[1] = {{ .kind = FD_RPC_VAL_NONE }};
2808 0 : fd_rpc_val_t _method[1] = {{ .kind = FD_RPC_VAL_NONE }};
2809 0 : fd_rpc_val_t _params[1] = {{ .kind = FD_RPC_VAL_NONE }};
2810 0 : int unknown_member = 0;
2811 :
2812 0 : fd_jtok_str_t key;
2813 0 : fd_jtok_obj_enter( j );
2814 0 : while( fd_jtok_obj_next( j, &key ) ) {
2815 0 : fd_rpc_val_t * member;
2816 0 : if( fd_jtok_str_eq( &key, "jsonrpc" ) ) member = jsonrpc;
2817 0 : else if( fd_jtok_str_eq( &key, "id" ) ) member = id;
2818 0 : else if( fd_jtok_str_eq( &key, "method" ) ) member = _method;
2819 0 : else if( fd_jtok_str_eq( &key, "params" ) ) member = _params;
2820 0 : else { unknown_member = 1; continue; }
2821 0 : if( member->kind==FD_RPC_VAL_NONE ) fd_rpc_val_read( j, member );
2822 0 : }
2823 0 : if( FD_UNLIKELY( fd_jtok_fini( j ) ) ) {
2824 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2825 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32700,\"message\":\"Parse error\"},\"id\":null}\n" );
2826 0 : }
2827 :
2828 : /* Echo the id back verbatim. */
2829 0 : char id_buf[ FD_RPC_ID_MAX_SZ+1UL ];
2830 0 : char const * id_cstr = "null";
2831 0 : if( id->kind!=FD_RPC_VAL_NONE ) {
2832 0 : if( FD_UNLIKELY( id->raw_sz>FD_RPC_ID_MAX_SZ ) ) {
2833 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2834 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32700,\"message\":\"Parse error\"},\"id\":null}\n" );
2835 0 : }
2836 0 : fd_memcpy( id_buf, id->raw, id->raw_sz );
2837 0 : id_buf[ id->raw_sz ] = '\0';
2838 0 : id_cstr = id_buf;
2839 0 : }
2840 :
2841 0 : if( FD_UNLIKELY( unknown_member ) ) {
2842 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2843 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"Invalid request\"},\"id\":%s}\n", id_cstr );
2844 0 : }
2845 :
2846 0 : if( FD_UNLIKELY( _method->kind!=FD_RPC_VAL_NONE && id->kind==FD_RPC_VAL_NONE ) ) {
2847 : /* A bug in Agave ¯\_(ツ)_/¯ */
2848 0 : return (fd_http_server_response_t){ .content_type = "application/json", .status = 200 };
2849 0 : }
2850 :
2851 0 : if( FD_UNLIKELY( jsonrpc->kind==FD_RPC_VAL_NONE && _method->kind!=FD_RPC_VAL_NONE ) ) {
2852 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2853 0 : return PRINTF_JSON( ctx, "{\"error\":{\"code\":-32600,\"message\":\"Unsupported JSON-RPC protocol version\"},\"id\":%s}\n", id_cstr );
2854 0 : }
2855 :
2856 0 : if( FD_UNLIKELY( jsonrpc->kind==FD_RPC_VAL_NONE || _method->kind==FD_RPC_VAL_NONE ) ) {
2857 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2858 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"Invalid request\"},\"id\":%s}\n", id_cstr );
2859 0 : }
2860 :
2861 0 : if( FD_UNLIKELY( !(id->kind==FD_RPC_VAL_INT && !id->neg) && id->kind!=FD_RPC_VAL_STR && id->kind!=FD_RPC_VAL_NULL ) ) {
2862 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2863 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32700,\"message\":\"Parse error\"},\"id\":null}\n" );
2864 0 : }
2865 :
2866 0 : if( FD_UNLIKELY( jsonrpc->kind==FD_RPC_VAL_NULL ) ) {
2867 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2868 0 : return PRINTF_JSON( ctx, "{\"error\":{\"code\":-32600,\"message\":\"Unsupported JSON-RPC protocol version\"},\"id\":%s}\n", id_cstr );
2869 0 : }
2870 :
2871 0 : if( FD_UNLIKELY( jsonrpc->kind!=FD_RPC_VAL_STR || !fd_jtok_str_eq( &jsonrpc->str, "2.0" ) ) ) {
2872 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2873 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"Invalid request\"},\"id\":%s}\n", id_cstr );
2874 0 : }
2875 :
2876 0 : fd_http_server_response_t response;
2877 :
2878 0 : if( FD_UNLIKELY( _method->kind!=FD_RPC_VAL_STR ) ) {
2879 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_INVALID, 1UL );
2880 0 : return PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"Invalid request\"},\"id\":%s}\n", id_cstr );
2881 0 : }
2882 0 : char method[ 64 ];
2883 0 : fd_rpc_val_cstr( _method, method, sizeof(method) ); /* too long or undecodable is an unknown method */
2884 :
2885 0 : fd_rpc_params_t params[1];
2886 0 : fd_rpc_params_read( _params, params );
2887 :
2888 0 : if( FD_LIKELY( !strcmp( method, "getAccountInfo" ) ) ) response = getAccountInfo( ctx, id_cstr, params );
2889 0 : else if( FD_LIKELY( !strcmp( method, "getBalance" ) ) ) response = getBalance( ctx, id_cstr, params );
2890 0 : else if( FD_LIKELY( !strcmp( method, "getBlock" ) ) ) response = getBlock( ctx, id_cstr, params );
2891 0 : else if( FD_LIKELY( !strcmp( method, "getBlockCommitment" ) ) ) response = getBlockCommitment( ctx, id_cstr, params );
2892 0 : else if( FD_LIKELY( !strcmp( method, "getBlockHeight" ) ) ) response = getBlockHeight( ctx, id_cstr, params );
2893 0 : else if( FD_LIKELY( !strcmp( method, "getBlockProduction" ) ) ) response = getBlockProduction( ctx, id_cstr, params );
2894 0 : else if( FD_LIKELY( !strcmp( method, "getBlocks" ) ) ) response = getBlocks( ctx, id_cstr, params );
2895 0 : else if( FD_LIKELY( !strcmp( method, "getBlocksWithLimit" ) ) ) response = getBlocksWithLimit( ctx, id_cstr, params );
2896 0 : else if( FD_LIKELY( !strcmp( method, "getBlockTime" ) ) ) response = getBlockTime( ctx, id_cstr, params );
2897 0 : else if( FD_LIKELY( !strcmp( method, "getClusterNodes" ) ) ) response = getClusterNodes( ctx, id_cstr, params );
2898 0 : else if( FD_LIKELY( !strcmp( method, "getEpochInfo" ) ) ) response = getEpochInfo( ctx, id_cstr, params );
2899 0 : else if( FD_LIKELY( !strcmp( method, "getEpochSchedule" ) ) ) response = getEpochSchedule( ctx, id_cstr, params );
2900 0 : else if( FD_LIKELY( !strcmp( method, "getFeeForMessage" ) ) ) response = getFeeForMessage( ctx, id_cstr, params );
2901 0 : else if( FD_LIKELY( !strcmp( method, "getFirstAvailableBlock" ) ) ) response = getFirstAvailableBlock( ctx, id_cstr, params );
2902 0 : else if( FD_LIKELY( !strcmp( method, "getGenesisHash" ) ) ) response = getGenesisHash( ctx, id_cstr, params );
2903 0 : else if( FD_LIKELY( !strcmp( method, "getHealth" ) ) ) response = getHealth( ctx, id_cstr, params );
2904 0 : else if( FD_LIKELY( !strcmp( method, "getHighestSnapshotSlot" ) ) ) response = getHighestSnapshotSlot( ctx, id_cstr, params );
2905 0 : else if( FD_LIKELY( !strcmp( method, "getIdentity" ) ) ) response = getIdentity( ctx, id_cstr, params );
2906 0 : else if( FD_LIKELY( !strcmp( method, "getInflationGovernor" ) ) ) response = getInflationGovernor( ctx, id_cstr, params );
2907 0 : else if( FD_LIKELY( !strcmp( method, "getInflationRate" ) ) ) response = getInflationRate( ctx, id_cstr, params );
2908 0 : else if( FD_LIKELY( !strcmp( method, "getInflationReward" ) ) ) response = getInflationReward( ctx, id_cstr, params );
2909 0 : else if( FD_LIKELY( !strcmp( method, "getLargestAccounts" ) ) ) response = getLargestAccounts( ctx, id_cstr, params );
2910 0 : else if( FD_LIKELY( !strcmp( method, "getLatestBlockhash" ) ) ) response = getLatestBlockhash( ctx, id_cstr, params );
2911 0 : else if( FD_LIKELY( !strcmp( method, "getLeaderSchedule" ) ) ) response = getLeaderSchedule( ctx, id_cstr, params );
2912 0 : else if( FD_LIKELY( !strcmp( method, "getMaxRetransmitSlot" ) ) ) response = getMaxRetransmitSlot( ctx, id_cstr, params );
2913 0 : else if( FD_LIKELY( !strcmp( method, "getMaxShredInsertSlot" ) ) ) response = getMaxShredInsertSlot( ctx, id_cstr, params );
2914 0 : else if( FD_LIKELY( !strcmp( method, "getMinimumBalanceForRentExemption" ) ) ) response = getMinimumBalanceForRentExemption( ctx, id_cstr, params );
2915 0 : else if( FD_LIKELY( !strcmp( method, "getMultipleAccounts" ) ) ) response = getMultipleAccounts( ctx, id_cstr, params );
2916 0 : else if( FD_LIKELY( !strcmp( method, "getProgramAccounts" ) ) ) response = getProgramAccounts( ctx, id_cstr, params );
2917 0 : else if( FD_LIKELY( !strcmp( method, "getRecentPerformanceSamples" ) ) ) response = getRecentPerformanceSamples( ctx, id_cstr, params );
2918 0 : else if( FD_LIKELY( !strcmp( method, "getRecentPrioritizationFees" ) ) ) response = getRecentPrioritizationFees( ctx, id_cstr, params );
2919 0 : else if( FD_LIKELY( !strcmp( method, "getSignaturesForAddress" ) ) ) response = getSignaturesForAddress( ctx, id_cstr, params );
2920 0 : else if( FD_LIKELY( !strcmp( method, "getSignatureStatuses" ) ) ) response = getSignatureStatuses( ctx, id_cstr, params );
2921 0 : else if( FD_LIKELY( !strcmp( method, "getSlot" ) ) ) response = getSlot( ctx, id_cstr, params );
2922 0 : else if( FD_LIKELY( !strcmp( method, "getSlotLeader" ) ) ) response = getSlotLeader( ctx, id_cstr, params );
2923 0 : else if( FD_LIKELY( !strcmp( method, "getSlotLeaders" ) ) ) response = getSlotLeaders( ctx, id_cstr, params );
2924 0 : else if( FD_LIKELY( !strcmp( method, "getStakeMinimumDelegation" ) ) ) response = getStakeMinimumDelegation( ctx, id_cstr, params );
2925 0 : else if( FD_LIKELY( !strcmp( method, "getSupply" ) ) ) response = getSupply( ctx, id_cstr, params );
2926 0 : else if( FD_LIKELY( !strcmp( method, "getTokenAccountBalance" ) ) ) response = getTokenAccountBalance( ctx, id_cstr, params );
2927 0 : else if( FD_LIKELY( !strcmp( method, "getTokenAccountsByDelegate" ) ) ) response = getTokenAccountsByDelegate( ctx, id_cstr, params );
2928 0 : else if( FD_LIKELY( !strcmp( method, "getTokenAccountsByOwner" ) ) ) response = getTokenAccountsByOwner( ctx, id_cstr, params );
2929 0 : else if( FD_LIKELY( !strcmp( method, "getTokenLargestAccounts" ) ) ) response = getTokenLargestAccounts( ctx, id_cstr, params );
2930 0 : else if( FD_LIKELY( !strcmp( method, "getTokenSupply" ) ) ) response = getTokenSupply( ctx, id_cstr, params );
2931 0 : else if( FD_LIKELY( !strcmp( method, "getTransaction" ) ) ) response = getTransaction( ctx, id_cstr, params );
2932 0 : else if( FD_LIKELY( !strcmp( method, "getTransactionCount" ) ) ) response = getTransactionCount( ctx, id_cstr, params );
2933 0 : else if( FD_LIKELY( !strcmp( method, "getVersion" ) ) ) response = getVersion( ctx, id_cstr, params );
2934 0 : else if( FD_LIKELY( !strcmp( method, "getVoteAccounts" ) ) ) response = getVoteAccounts( ctx, id_cstr, params );
2935 0 : else if( FD_LIKELY( !strcmp( method, "slotSubscribe" ) ) ) response = slotSubscribe( ctx, id_cstr, params, ws_conn_id );
2936 0 : else if( FD_LIKELY( !strcmp( method, "slotUnsubscribe" ) ) ) response = slotUnsubscribe( ctx, id_cstr, params, ws_conn_id );
2937 0 : else if( FD_LIKELY( !strcmp( method, "voteSubscribe" ) ) ) response = voteSubscribe( ctx, id_cstr, params, ws_conn_id );
2938 0 : else if( FD_LIKELY( !strcmp( method, "voteUnsubscribe" ) ) ) response = voteUnsubscribe( ctx, id_cstr, params, ws_conn_id );
2939 0 : else if( FD_LIKELY( !strcmp( method, "isBlockhashValid" ) ) ) response = isBlockhashValid( ctx, id_cstr, params );
2940 0 : else if( FD_LIKELY( !strcmp( method, "minimumLedgerSlot" ) ) ) response = minimumLedgerSlot( ctx, id_cstr, params );
2941 0 : else if( FD_LIKELY( !strcmp( method, "requestAirdrop" ) ) ) response = requestAirdrop( ctx, id_cstr, params );
2942 0 : else if( FD_LIKELY( !strcmp( method, "sendTransaction" ) ) ) response = sendTransaction( ctx, id_cstr, params );
2943 0 : else if( FD_LIKELY( !strcmp( method, "simulateTransaction" ) ) ) response = simulateTransaction( ctx, id_cstr, params );
2944 0 : else if( FD_LIKELY( !strcmp( method, "getAgGenesisCert" ) ) ) response = getAgGenesisCert( ctx, id_cstr, params );
2945 0 : else {
2946 0 : FD_MCNT_INC( RPC, REQUEST_HANDLED_UNKNOWN, 1UL );
2947 0 : response = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32601,\"message\":\"Method not found\"},\"id\":%s}\n", id_cstr );
2948 0 : }
2949 :
2950 : /* Agave pubsub answers non-subscription methods with -32601 and
2951 : keeps the socket; a bare 501 would close it and drop subscriptions */
2952 0 : if( FD_UNLIKELY( ws_conn_id!=ULONG_MAX && response.status==501UL ) ) {
2953 0 : response = PRINTF_JSON( ctx, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32601,\"message\":\"Method not found\"},\"id\":%s}\n", id_cstr );
2954 0 : }
2955 :
2956 0 : return response;
2957 0 : }
2958 :
2959 : static fd_http_server_response_t
2960 0 : rpc_http_request( fd_http_server_request_t const * request ) {
2961 0 : fd_rpc_tile_t * ctx = request->ctx;
2962 0 : long dt = -fd_tickcount();
2963 0 : fd_http_server_response_t response = rpc_http_request1( ctx, request );
2964 0 : dt += fd_tickcount();
2965 0 : fd_histf_sample( ctx->request_duration, (ulong)dt );
2966 0 : return response;
2967 0 : }
2968 :
2969 : static void
2970 : rpc_ws_open( ulong ws_conn_id,
2971 0 : void * ctx ) {
2972 0 : (void)ws_conn_id; (void)ctx;
2973 0 : }
2974 :
2975 : static void
2976 : rpc_ws_close( ulong ws_conn_id,
2977 : int reason FD_PARAM_UNUSED,
2978 0 : void * _ctx ) {
2979 0 : fd_rpc_tile_t * ctx = (fd_rpc_tile_t *)_ctx;
2980 0 : if( FD_UNLIKELY( ws_conn_id>=ctx->http->max_ws_conns ) ) return;
2981 0 : fd_rpc_ws_subscriber_vote_remove( ctx, ws_conn_id );
2982 0 : fd_rpc_ws_subscriber_slot_remove( ctx, ws_conn_id );
2983 0 : }
2984 :
2985 : static void
2986 : rpc_ws_message( ulong ws_conn_id,
2987 : uchar const * data,
2988 : ulong data_len,
2989 0 : void * _ctx ) {
2990 0 : fd_rpc_tile_t * ctx = (fd_rpc_tile_t *)_ctx;
2991 :
2992 0 : fd_http_server_unstage( ctx->http );
2993 :
2994 0 : long dt = -fd_tickcount();
2995 0 : fd_http_server_response_t response = rpc_json_request( ctx, data, data_len, ws_conn_id );
2996 0 : dt += fd_tickcount();
2997 0 : fd_histf_sample( ctx->request_duration, (ulong)dt );
2998 :
2999 : /* fd_http_server_reserve may evict this connection while staging the response. */
3000 0 : if( FD_UNLIKELY( ctx->http->pollfds[ ctx->http->max_conns+ws_conn_id ].fd==-1 ) ) return;
3001 :
3002 0 : if( FD_UNLIKELY( response.status!=200UL ) ) {
3003 0 : fd_http_server_ws_close( ctx->http, ws_conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST );
3004 0 : return;
3005 0 : }
3006 :
3007 0 : if( FD_LIKELY( response._body_len ) ) {
3008 0 : ulong response_body_end = response._body_off + response._body_len;
3009 :
3010 0 : ctx->http->stage_off = response._body_off;
3011 0 : ctx->http->stage_len = response._body_len;
3012 0 : ctx->http->stage_comp_len = 0UL;
3013 :
3014 0 : int err = fd_http_server_ws_send( ctx->http, ws_conn_id );
3015 0 : if( FD_UNLIKELY( ctx->http->stage_off<response_body_end ) ) ctx->http->stage_off = response_body_end;
3016 0 : if( FD_UNLIKELY( err ) )
3017 0 : fd_http_server_ws_close( ctx->http, ws_conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_TOO_SLOW );
3018 0 : }
3019 0 : }
3020 :
3021 : static void
3022 : privileged_init( fd_topo_t const * topo,
3023 0 : fd_topo_tile_t const * tile ) {
3024 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
3025 :
3026 0 : fd_http_server_params_t http_params = derive_http_params( tile );
3027 :
3028 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
3029 0 : fd_rpc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_rpc_tile_t ), sizeof( fd_rpc_tile_t ) );
3030 0 : fd_http_server_t * _http = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_params ) );
3031 :
3032 0 : fd_memset( ctx, 0, sizeof(fd_rpc_tile_t) );
3033 :
3034 0 : if( FD_UNLIKELY( !strcmp( tile->rpc.identity_key_path, "" ) ) )
3035 0 : FD_LOG_ERR(( "identity_key_path not set" ));
3036 :
3037 0 : const uchar * identity_key = fd_keyload_load( tile->rpc.identity_key_path, /* pubkey only: */ 1 );
3038 0 : fd_memcpy( ctx->identity_pubkey, identity_key, 32UL );
3039 :
3040 0 : fd_http_server_callbacks_t callbacks = {
3041 0 : .request = rpc_http_request,
3042 0 : .ws_open = rpc_ws_open,
3043 0 : .ws_close = rpc_ws_close,
3044 0 : .ws_message = rpc_ws_message,
3045 0 : };
3046 0 : ctx->http = fd_http_server_join( fd_http_server_new( _http, http_params, callbacks, ctx ) );
3047 0 : ctx->snapshot_server_enabled = tile->rpc.snapshot_server_enabled;
3048 0 : if( FD_LIKELY( ctx->snapshot_server_enabled ) ) {
3049 0 : uint ip_addr;
3050 0 : if( FD_UNLIKELY( !fd_dns_resolve_address( tile->rpc.snapshot_server_host, &ip_addr ) ) )
3051 0 : FD_LOG_ERR(( "failed to resolve [gossip.host] `%s`", tile->rpc.snapshot_server_host ));
3052 0 : FD_TEST( fd_cstr_printf_check(
3053 0 : ctx->snapshot_server_url, sizeof(ctx->snapshot_server_url), NULL,
3054 0 : "http://" FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS( ip_addr ),
3055 0 : tile->rpc.snapshot_server_port ) );
3056 0 : }
3057 0 : ctx->waker_client_idx = tile->waker_client_idx;
3058 0 : FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
3059 0 : fd_http_server_listen6( ctx->http, FD_WAKER_INNER_FD( ctx->waker_client_idx ), &tile->rpc.listen_addr, tile->rpc.listen_port );
3060 0 : char listen_addr_cstr[ FD_IP6_ADDR_CSTR_MAX ]; fd_ip6_addr_cstr( listen_addr_cstr, &tile->rpc.listen_addr );
3061 0 : FD_LOG_NOTICE(( "rpc server listening at %shttp://%s:%u%s", fd_log_style_bold(), listen_addr_cstr, tile->rpc.listen_port, fd_log_style_normal() ));
3062 0 : }
3063 :
3064 : static inline fd_rpc_out_t
3065 : out1( fd_topo_t const * topo,
3066 : fd_topo_tile_t const * tile,
3067 0 : char const * name ) {
3068 0 : ulong idx = ULONG_MAX;
3069 :
3070 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
3071 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
3072 0 : if( !strcmp( link->name, name ) ) {
3073 0 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had multiple output links named %s but expected one", tile->name, tile->kind_id, name ));
3074 0 : idx = i;
3075 0 : }
3076 0 : }
3077 :
3078 0 : if( FD_UNLIKELY( idx==ULONG_MAX ) ) return (fd_rpc_out_t){ .idx = ULONG_MAX, .mem = NULL, .chunk0 = 0, .wmark = 0, .chunk = 0 };
3079 :
3080 :
3081 0 : ulong mtu = topo->links[ tile->out_link_id[ idx ] ].mtu;
3082 0 : if( FD_UNLIKELY( mtu==0UL ) ) return (fd_rpc_out_t){ .idx = idx, .mem = NULL, .chunk0 = ULONG_MAX, .wmark = ULONG_MAX, .chunk = ULONG_MAX };
3083 :
3084 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
3085 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
3086 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
3087 :
3088 0 : return (fd_rpc_out_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
3089 0 : }
3090 :
3091 : static void
3092 : unprivileged_init( fd_topo_t const * topo,
3093 0 : fd_topo_tile_t const * tile ) {
3094 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
3095 :
3096 0 : fd_http_server_params_t http_params = derive_http_params( tile );
3097 :
3098 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
3099 0 : fd_rpc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_rpc_tile_t), sizeof(fd_rpc_tile_t) );
3100 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( http_params ) );
3101 0 : void * _bz2_alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
3102 0 : void * _banks = FD_SCRATCH_ALLOC_APPEND( l, alignof(bank_info_t), tile->rpc.max_live_slots*sizeof(bank_info_t) );
3103 0 : void * _nodes_dlist = FD_SCRATCH_ALLOC_APPEND( l, fd_rpc_cluster_node_dlist_align(), fd_rpc_cluster_node_dlist_footprint() );
3104 0 : void * _accdb_join = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->rpc.max_live_slots, 0 ) );
3105 0 : void * _ws_sub_vote = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), http_params.max_ws_connection_cnt*sizeof(ulong) );
3106 0 : void * _ws_sub_slot = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), http_params.max_ws_connection_cnt*sizeof(ulong) );
3107 0 : void * _genesis_tar = FD_SCRATCH_ALLOC_APPEND( l, alignof(uchar), fd_rpc_genesis_tar_max_sz( tile->rpc.genesis_max_message_size ) );
3108 0 : void * _genesis_tar_bz = FD_SCRATCH_ALLOC_APPEND( l, alignof(uchar), fd_rpc_genesis_tar_bz_max_sz( tile->rpc.genesis_max_message_size ) );
3109 0 : ulong zstd_wksp_sz = ZSTD_estimateCCtxSize( FD_RPC_ZSTD_LEVEL );
3110 0 : void * _zstd_wksp = FD_SCRATCH_ALLOC_APPEND( l, 16UL, zstd_wksp_sz );
3111 :
3112 0 : ctx->delay_startup = tile->rpc.delay_startup;
3113 0 : fd_alpenglow_pda( "carlgration", ctx->fd_genesis_cert_addr );
3114 0 : ctx->waker_rearm_pending = 0;
3115 0 : ctx->ws_subscribers_vote = _ws_sub_vote;
3116 0 : ctx->ws_subscribers_vote_cnt = 0UL;
3117 0 : ctx->ws_subscribers_slot = _ws_sub_slot;
3118 0 : ctx->ws_subscribers_slot_cnt = 0UL;
3119 0 : ctx->ws_sub_id_vote = 0UL;
3120 0 : ctx->ws_sub_id_slot = 0UL;
3121 0 : ctx->ws_sub_id_next = 0UL;
3122 :
3123 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
3124 0 : FD_TEST( ctx->keyswitch );
3125 :
3126 0 : ctx->bz2_alloc = fd_alloc_join( fd_alloc_new( _bz2_alloc, 1UL ), 1UL );
3127 0 : FD_TEST( ctx->bz2_alloc );
3128 :
3129 0 : ctx->zstd_cctx = ZSTD_initStaticCCtx( _zstd_wksp, zstd_wksp_sz );
3130 0 : FD_CHECK_ERR( ctx->zstd_cctx, "ZSTD_initStaticCCtx failed" );
3131 :
3132 0 : fd_clock_tile_init( ctx->clock );
3133 0 : ctx->in_cnt = tile->in_cnt;
3134 0 : ctx->idle_cnt = 0UL;
3135 :
3136 0 : FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
3137 0 : ctx->waker_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->waker_fseq_obj_id ) );
3138 0 : FD_TEST( ctx->waker_fseq );
3139 :
3140 0 : ctx->cluster_confirmed_slot = ULONG_MAX;
3141 0 : ctx->genesis_max_message_size = tile->rpc.genesis_max_message_size;
3142 0 : ctx->genesis_tar_max_sz = fd_rpc_genesis_tar_max_sz( tile->rpc.genesis_max_message_size );
3143 0 : ctx->genesis_tar_bz_max_sz = fd_rpc_genesis_tar_bz_max_sz( tile->rpc.genesis_max_message_size );
3144 0 : ctx->genesis_tar = _genesis_tar;
3145 0 : ctx->genesis_tar_bz = _genesis_tar_bz;
3146 0 : ctx->genesis_tar_bz_sz = ULONG_MAX;
3147 :
3148 0 : ctx->processed_idx = ULONG_MAX;
3149 0 : ctx->confirmed_idx = ULONG_MAX;
3150 0 : ctx->finalized_idx = ULONG_MAX;
3151 :
3152 0 : ctx->cluster_nodes_dlist = fd_rpc_cluster_node_dlist_join( fd_rpc_cluster_node_dlist_new( _nodes_dlist ) );
3153 0 : ctx->banks = _banks;
3154 0 : ctx->max_live_slots = tile->rpc.max_live_slots;
3155 0 : for( ulong i=0UL; i<ctx->max_live_slots; i++ ) ctx->banks[ i ].slot = ULONG_MAX;
3156 :
3157 0 : fd_rpc_mleaders_init( ctx->mleaders );
3158 0 : ctx->has_epoch_schedule = 0;
3159 :
3160 0 : FD_TEST( tile->in_cnt<=sizeof( ctx->in )/sizeof( ctx->in[ 0 ] ) );
3161 0 : for( ulong i=0; i<tile->in_cnt; i++ ) {
3162 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
3163 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
3164 :
3165 0 : ctx->in[ i ].mem = link_wksp->wksp;
3166 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
3167 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
3168 0 : ctx->in[ i ].mtu = link->mtu;
3169 :
3170 0 : if ( FD_LIKELY( !strcmp( link->name, "replay_slot" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY;
3171 0 : else if( FD_LIKELY( !strcmp( link->name, "genesi_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_GENESI;
3172 0 : else if( FD_LIKELY( !strcmp( link->name, "gossip_ciaddr" ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_OUT;
3173 0 : else if( FD_LIKELY( !strcmp( link->name, "replay_epoch" ) ) ) ctx->in_kind[ i ] = IN_KIND_EPOCH;
3174 0 : else if( FD_UNLIKELY( !strcmp( link->name, "votor_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_VOTOR;
3175 0 : else FD_LOG_ERR(( "unexpected link name %s", link->name ));
3176 0 : }
3177 :
3178 0 : for( ctx->shred_rtx_cnt=0UL; ctx->shred_rtx_cnt<FD_TOPO_MAX_TILES; ctx->shred_rtx_cnt++ ) {
3179 0 : ulong rtx_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "shred_rtx.%lu", ctx->shred_rtx_cnt );
3180 0 : if( FD_UNLIKELY( rtx_obj_id==ULONG_MAX ) ) break;
3181 0 : ctx->shred_rtx[ ctx->shred_rtx_cnt ] = fd_fseq_join( fd_topo_obj_laddr( topo, rtx_obj_id ) );
3182 0 : FD_TEST( ctx->shred_rtx[ ctx->shred_rtx_cnt ] );
3183 0 : }
3184 :
3185 0 : *ctx->replay_out = out1( topo, tile, "rpc_replay" ); FD_TEST( ctx->replay_out->idx!=ULONG_MAX );
3186 :
3187 : /* Read-only join to accdb. The accdb workspace is mapped
3188 : PROT_READ in this tile (see topology); the only writable
3189 : external mapping is our private epoch fseq. fd FD_ACCDB_FD_RO is
3190 : the O_RDONLY dup of the accdb data file. */
3191 0 : void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->rpc.accdb_obj_id );
3192 0 : fd_accdb_shmem_t * accdb_shmem_ro = fd_accdb_shmem_join( _accdb_shmem );
3193 0 : FD_TEST( accdb_shmem_ro );
3194 0 : ulong * epoch_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->rpc.accdb_epoch_fseq_obj_id ) );
3195 0 : FD_TEST( epoch_fseq );
3196 0 : ctx->accdb = fd_accdb_join_readonly( _accdb_join, accdb_shmem_ro, epoch_fseq, FD_ACCDB_FD_RO );
3197 0 : FD_TEST( ctx->accdb );
3198 :
3199 0 : fd_histf_join( fd_metrics_histf_new_ns( ctx->request_duration, FD_MHIST_EDGES_NS( RPC, REQUEST_DURATION_NANOS ) ) );
3200 :
3201 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
3202 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
3203 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
3204 0 : }
3205 :
3206 : static ulong
3207 : populate_allowed_seccomp( fd_topo_t const * topo,
3208 : fd_topo_tile_t const * tile,
3209 : ulong out_cnt,
3210 0 : struct sock_filter * out ) {
3211 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
3212 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
3213 0 : fd_rpc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_rpc_tile_t ), sizeof( fd_rpc_tile_t ) );
3214 :
3215 0 : uint epoll_inner_fd = (uint)FD_WAKER_INNER_FD( tile->waker_client_idx );
3216 0 : uint epoll_outer_fd = (uint)FD_WAKER_OUTER_FD;
3217 :
3218 0 : populate_sock_filter_policy_fd_rpc_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)fd_http_server_fd( ctx->http ), (uint)FD_ACCDB_FD_RO, epoll_inner_fd, epoll_outer_fd );
3219 0 : return sock_filter_policy_fd_rpc_tile_instr_cnt;
3220 0 : }
3221 :
3222 : static ulong
3223 : populate_allowed_fds( fd_topo_t const * topo,
3224 : fd_topo_tile_t const * tile,
3225 : ulong out_fds_cnt,
3226 0 : int * out_fds ) {
3227 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
3228 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
3229 0 : fd_rpc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_rpc_tile_t ), sizeof( fd_rpc_tile_t ) );
3230 :
3231 0 : if( FD_UNLIKELY( out_fds_cnt<6UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
3232 :
3233 0 : ulong out_cnt = 0UL;
3234 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
3235 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
3236 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
3237 0 : out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->http ); /* rpc listen socket */
3238 0 : out_fds[ out_cnt++ ] = FD_ACCDB_FD_RO; /* accounts db readonly fd */
3239 0 : out_fds[ out_cnt++ ] = FD_WAKER_OUTER_FD;
3240 0 : out_fds[ out_cnt++ ] = FD_WAKER_INNER_FD( tile->waker_client_idx );
3241 :
3242 0 : return out_cnt;
3243 0 : }
3244 :
3245 : static ulong
3246 : rlimit_file_cnt( fd_topo_t const * topo FD_PARAM_UNUSED,
3247 0 : fd_topo_tile_t const * tile ) {
3248 : /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
3249 0 : ulong base = 5UL;
3250 0 : return base + tile->rpc.max_http_connections + tile->rpc.max_websocket_connections;
3251 0 : }
3252 :
3253 0 : #define STEM_BURST (3UL)
3254 :
3255 : /* The default STEM_LAZY is based on cr_max, which is the minimum depth
3256 : across all output links that have at least one reliable consumer.
3257 : RPC has one tiny output link used to release banks, with a
3258 : significantly slower line rate than assumed in the formula for the
3259 : default STEM_LAZY value.
3260 :
3261 : Instead, lazy just needs to be frequent enough to relinquish credits
3262 : to upstream producers faster than they are exhausted. 384us is a
3263 : reasonable default used in many other non-critical tiles. */
3264 0 : #define STEM_LAZY (128L*3000L)
3265 :
3266 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_rpc_tile_t
3267 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_rpc_tile_t)
3268 :
3269 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
3270 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
3271 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
3272 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
3273 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
3274 :
3275 : #include "../../disco/stem/fd_stem.c"
3276 :
3277 : #ifndef FD_TILE_TEST
3278 : fd_topo_run_tile_t fd_tile_rpc = {
3279 : .name = "rpc",
3280 : .rlimit_file_cnt_fn = rlimit_file_cnt,
3281 : .populate_allowed_seccomp = populate_allowed_seccomp,
3282 : .populate_allowed_fds = populate_allowed_fds,
3283 : .scratch_align = scratch_align,
3284 : .scratch_footprint = scratch_footprint,
3285 : .loose_footprint = loose_footprint,
3286 : .privileged_init = privileged_init,
3287 : .unprivileged_init = unprivileged_init,
3288 : .run = stem_run,
3289 : };
3290 : #endif
|