Line data Source code
1 : #include "fd_rpc_service.h"
2 : #include "fd_methods.h"
3 : #include "fd_webserver.h"
4 : #include "base_enc.h"
5 : #include "../../flamenco/types/fd_types.h"
6 : #include "../../flamenco/types/fd_solana_block.pb.h"
7 : #include "../../flamenco/runtime/fd_runtime.h"
8 : #include "../../flamenco/runtime/fd_acc_mgr.h"
9 : #include "../../flamenco/runtime/sysvar/fd_sysvar_rent.h"
10 : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
11 : #include "../../ballet/base58/fd_base58.h"
12 : #include "../../ballet/base64/fd_base64.h"
13 : #include "fd_rpc_history.h"
14 : #include "keywords.h"
15 : #include <errno.h>
16 : #include <stdlib.h>
17 : #include <stdio.h> /* snprintf */
18 : #include <sys/socket.h>
19 : #include <netinet/in.h>
20 : #include <stdarg.h>
21 :
22 : #include "../../app/firedancer/version.h"
23 :
24 0 : #define CRLF "\r\n"
25 0 : #define MATCH_STRING(_text_,_text_sz_,_str_) (_text_sz_ == sizeof(_str_)-1 && memcmp(_text_, _str_, sizeof(_str_)-1) == 0)
26 0 : #define EMIT_SIMPLE(_str_) fd_web_reply_append(ws, _str_, sizeof(_str_)-1)
27 :
28 : static const uint PATH_COMMITMENT[4] = { ( JSON_TOKEN_LBRACE << 16 ) | KEYW_JSON_PARAMS,
29 : ( JSON_TOKEN_LBRACKET << 16 ) | 1,
30 : ( JSON_TOKEN_LBRACE << 16 ) | KEYW_JSON_COMMITMENT,
31 : ( JSON_TOKEN_STRING << 16 ) };
32 :
33 : struct fd_ws_subscription {
34 : ulong conn_id;
35 : long meth_id;
36 : char call_id[64];
37 : ulong subsc_id;
38 : union {
39 : struct {
40 : fd_pubkey_t acct;
41 : fd_rpc_encoding_t enc;
42 : long off;
43 : long len;
44 : } acct_subscribe;
45 : };
46 : };
47 :
48 0 : #define FD_WS_MAX_SUBS 1024
49 :
50 : typedef struct fd_stats_snapshot fd_stats_snapshot_t;
51 :
52 : struct fd_perf_sample {
53 : ulong num_slots;
54 : ulong num_transactions;
55 : ulong num_non_vote_transactions;
56 : ulong highest_slot;
57 : };
58 : typedef struct fd_perf_sample fd_perf_sample_t;
59 :
60 : #define DEQUE_NAME fd_perf_sample_deque
61 0 : #define DEQUE_T fd_perf_sample_t
62 0 : #define DEQUE_MAX 720UL /* MAX RPC PERF SAMPLES */
63 : #include "../../util/tmpl/fd_deque.c"
64 :
65 : struct fd_rpc_global_ctx {
66 : fd_spad_t * spad;
67 : fd_webserver_t ws;
68 : fd_funk_t * funk;
69 : fd_blockstore_t blockstore[1];
70 : int blockstore_fd;
71 : struct fd_ws_subscription sub_list[FD_WS_MAX_SUBS];
72 : ulong sub_cnt;
73 : ulong last_subsc_id;
74 : int tpu_socket;
75 : struct sockaddr_in tpu_addr;
76 : fd_perf_sample_t * perf_samples;
77 : fd_perf_sample_t perf_sample_snapshot;
78 : long perf_sample_ts;
79 : fd_multi_epoch_leaders_t * leaders;
80 : ulong acct_age;
81 : fd_rpc_history_t * history;
82 : };
83 : typedef struct fd_rpc_global_ctx fd_rpc_global_ctx_t;
84 :
85 : struct fd_rpc_ctx {
86 : char call_id[64];
87 : fd_rpc_global_ctx_t * global;
88 : };
89 :
90 : static void
91 0 : fd_method_simple_error( fd_rpc_ctx_t * ctx, int errcode, const char* text ) {
92 0 : fd_web_reply_error( &ctx->global->ws, errcode, text, ctx->call_id );
93 0 : }
94 :
95 : static void
96 : fd_method_error( fd_rpc_ctx_t * ctx, int errcode, const char* format, ... )
97 : __attribute__ ((format (printf, 3, 4)));
98 :
99 : static void
100 0 : fd_method_error( fd_rpc_ctx_t * ctx, int errcode, const char* format, ... ) {
101 0 : char text[4096];
102 0 : va_list ap;
103 0 : va_start(ap, format);
104 0 : vsnprintf(text, sizeof(text), format, ap);
105 0 : va_end(ap);
106 0 : fd_method_simple_error(ctx, errcode, text);
107 0 : }
108 :
109 : static const void *
110 0 : read_account_with_xid( fd_rpc_ctx_t * ctx, fd_funk_rec_key_t * recid, fd_funk_txn_xid_t * xid, ulong * result_len ) {
111 0 : fd_funk_txn_map_t * txn_map = fd_funk_txn_map( ctx->global->funk );
112 0 : fd_funk_txn_t * txn = fd_funk_txn_query( xid, txn_map );
113 0 : return fd_funk_rec_query_copy( ctx->global->funk, txn, recid, fd_spad_virtual(ctx->global->spad), result_len );
114 0 : }
115 :
116 : static const void *
117 0 : read_account( fd_rpc_ctx_t * ctx, fd_funk_rec_key_t * recid, ulong * result_len ) {
118 0 : return fd_funk_rec_query_copy( ctx->global->funk, NULL, recid, fd_spad_virtual(ctx->global->spad), result_len );
119 0 : }
120 :
121 : static ulong
122 0 : get_slot_from_commitment_level( struct json_values * values, fd_rpc_ctx_t * ctx ) {
123 0 : ulong commit_str_sz = 0;
124 0 : const void * commit_str = json_get_value( values, PATH_COMMITMENT, 4, &commit_str_sz );
125 0 : if( commit_str == NULL ) {
126 0 : return fd_rpc_history_latest_slot( ctx->global->history );
127 0 : } else if( MATCH_STRING( commit_str, commit_str_sz, "confirmed" ) ) {
128 0 : return ctx->global->blockstore->shmem->hcs;
129 0 : } else if( MATCH_STRING( commit_str, commit_str_sz, "processed" ) ) {
130 0 : return ctx->global->blockstore->shmem->lps;
131 0 : } else if( MATCH_STRING( commit_str, commit_str_sz, "finalized" ) ) {
132 0 : return ctx->global->blockstore->shmem->wmk;
133 0 : } else {
134 0 : fd_method_error( ctx, -1, "invalid commitment %s", (const char *)commit_str );
135 0 : return FD_SLOT_NULL;
136 0 : }
137 0 : }
138 :
139 : /*
140 : static const char *
141 : block_flags_to_confirmation_status( uchar flags ) {
142 : if( flags & (1U << FD_BLOCK_FLAG_FINALIZED) ) return "\"finalized\"";
143 : if( flags & (1U << FD_BLOCK_FLAG_CONFIRMED) ) return "\"confirmed\"";
144 : if( flags & (1U << FD_BLOCK_FLAG_PROCESSED) ) return "\"processed\"";
145 : return "null";
146 : }
147 : */
148 :
149 : // Implementation of the "getAccountInfo" method
150 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "id": 1, "method": "getAccountInfo", "params": [ "21bVZhkqPJRVYDG3YpYtzHLMvkc7sa4KB7fMwGekTquG", { "encoding": "base64" } ] }'
151 :
152 : static int
153 0 : method_getAccountInfo(struct json_values* values, fd_rpc_ctx_t * ctx) {
154 0 : fd_webserver_t * ws = &ctx->global->ws;
155 :
156 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
157 : // Path to argument
158 0 : static const uint PATH[3] = {
159 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
160 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
161 0 : (JSON_TOKEN_STRING<<16)
162 0 : };
163 0 : ulong arg_sz = 0;
164 0 : const void* arg = json_get_value(values, PATH, 3, &arg_sz);
165 0 : if (arg == NULL) {
166 0 : fd_method_error(ctx, -1, "getAccountInfo requires a string as first parameter");
167 0 : return 0;
168 0 : }
169 :
170 0 : fd_pubkey_t acct;
171 0 : if( fd_base58_decode_32((const char *)arg, acct.uc) == NULL ) {
172 0 : fd_method_error(ctx, -1, "invalid base58 encoding");
173 0 : return 0;
174 0 : }
175 0 : static const uint PATH2[4] = {
176 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
177 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
178 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ENCODING,
179 0 : (JSON_TOKEN_STRING<<16)
180 0 : };
181 :
182 0 : ulong enc_str_sz = 0;
183 0 : const void* enc_str = json_get_value(values, PATH2, 4, &enc_str_sz);
184 0 : fd_rpc_encoding_t enc;
185 0 : if (enc_str == NULL || MATCH_STRING(enc_str, enc_str_sz, "base58"))
186 0 : enc = FD_ENC_BASE58;
187 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64"))
188 0 : enc = FD_ENC_BASE64;
189 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64+zstd"))
190 0 : enc = FD_ENC_BASE64_ZSTD;
191 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "jsonParsed"))
192 0 : enc = FD_ENC_JSON;
193 0 : else {
194 0 : fd_method_error(ctx, -1, "invalid data encoding %s", (const char*)enc_str);
195 0 : return 0;
196 0 : }
197 :
198 0 : ulong val_sz;
199 0 : fd_funk_rec_key_t recid = fd_funk_acc_key(&acct);
200 0 : const void * val = read_account(ctx, &recid, &val_sz);
201 0 : if (val == NULL) {
202 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":null},\"id\":%s}" CRLF,
203 0 : fd_rpc_history_latest_slot( ctx->global->history ), ctx->call_id);
204 0 : return 0;
205 0 : }
206 :
207 0 : static const uint PATH3[5] = {
208 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
209 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
210 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_DATASLICE,
211 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_LENGTH,
212 0 : (JSON_TOKEN_INTEGER<<16)
213 0 : };
214 0 : static const uint PATH4[5] = {
215 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
216 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
217 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_DATASLICE,
218 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_OFFSET,
219 0 : (JSON_TOKEN_INTEGER<<16)
220 0 : };
221 0 : ulong len_sz = 0;
222 0 : const void* len_ptr = json_get_value(values, PATH3, 5, &len_sz);
223 0 : ulong off_sz = 0;
224 0 : const void* off_ptr = json_get_value(values, PATH4, 5, &off_sz);
225 0 : long off = (off_ptr ? *(long *)off_ptr : FD_LONG_UNSET);
226 0 : long len = (len_ptr ? *(long *)len_ptr : FD_LONG_UNSET);
227 :
228 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":",
229 0 : fd_rpc_history_latest_slot( ctx->global->history ) );
230 0 : const char * err = fd_account_to_json( ws, acct, enc, val, val_sz, off, len, ctx->global->spad );
231 0 : if( err ) {
232 0 : fd_method_error(ctx, -1, "%s", err);
233 0 : return 0;
234 0 : }
235 0 : fd_web_reply_sprintf(ws, "},\"id\":%s}" CRLF, ctx->call_id);
236 :
237 0 : } FD_SPAD_FRAME_END;
238 :
239 0 : return 0;
240 0 : }
241 :
242 : // Implementation of the "getBalance" method
243 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "id": 1, "method": "getBalance", "params": [ "6s5gDyLyfNXP6WHUEn4YSMQJVcGETpKze7FCPeg9wxYT" ] }'
244 :
245 : static int
246 0 : method_getBalance(struct json_values* values, fd_rpc_ctx_t * ctx) {
247 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
248 : // Path to argument
249 0 : static const uint PATH[3] = {
250 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
251 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
252 0 : (JSON_TOKEN_STRING<<16)
253 0 : };
254 0 : fd_webserver_t * ws = &ctx->global->ws;
255 0 : ulong arg_sz = 0;
256 0 : const void* arg = json_get_value(values, PATH, 3, &arg_sz);
257 0 : if (arg == NULL) {
258 0 : fd_method_error(ctx, -1, "getBalance requires a string as first parameter");
259 0 : return 0;
260 0 : }
261 0 : fd_pubkey_t acct;
262 0 : if( fd_base58_decode_32((const char *)arg, acct.uc) == NULL ) {
263 0 : fd_method_error(ctx, -1, "invalid base58 encoding");
264 0 : return 0;
265 0 : }
266 0 : ulong val_sz;
267 0 : fd_funk_rec_key_t recid = fd_funk_acc_key(&acct);
268 0 : const void * val = read_account(ctx, &recid, &val_sz);
269 0 : if (val == NULL) {
270 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":0},\"id\":%s}" CRLF,
271 0 : fd_rpc_history_latest_slot( ctx->global->history ), ctx->call_id);
272 0 : return 0;
273 0 : }
274 0 : fd_account_meta_t * metadata = (fd_account_meta_t *)val;
275 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":%lu},\"id\":%s}" CRLF,
276 0 : fd_rpc_history_latest_slot( ctx->global->history ), metadata->info.lamports, ctx->call_id);
277 0 : } FD_SPAD_FRAME_END;
278 0 : return 0;
279 0 : }
280 :
281 : // Implementation of the "getBlock" method
282 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc": "2.0","id":1, "method":"getBlock", "params": [270562740, {"encoding": "json", "maxSupportedTransactionVersion":0, "transactionDetails":"full", "rewards":false}]} '
283 :
284 : static int
285 0 : method_getBlock(struct json_values* values, fd_rpc_ctx_t * ctx) {
286 0 : static const uint PATH_SLOT[3] = {
287 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
288 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
289 0 : (JSON_TOKEN_INTEGER<<16)
290 0 : };
291 0 : static const uint PATH_ENCODING[4] = {
292 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
293 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
294 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ENCODING,
295 0 : (JSON_TOKEN_STRING<<16)
296 0 : };
297 0 : static const uint PATH_MAXVERS[4] = {
298 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
299 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
300 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_MAXSUPPORTEDTRANSACTIONVERSION,
301 0 : (JSON_TOKEN_INTEGER<<16)
302 0 : };
303 0 : static const uint PATH_DETAIL[4] = {
304 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
305 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
306 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_TRANSACTIONDETAILS,
307 0 : (JSON_TOKEN_STRING<<16)
308 0 : };
309 : /*
310 : static const uint PATH_REWARDS[4] = {
311 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
312 : (JSON_TOKEN_LBRACKET<<16) | 1,
313 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_REWARDS,
314 : (JSON_TOKEN_BOOL<<16)
315 : };
316 : */
317 :
318 0 : fd_webserver_t * ws = &ctx->global->ws;
319 0 : ulong slot_sz = 0;
320 0 : const void* slot = json_get_value(values, PATH_SLOT, 3, &slot_sz);
321 0 : if (slot == NULL) {
322 0 : fd_method_error(ctx, -1, "getBlock requires a slot number as first parameter");
323 0 : return 0;
324 0 : }
325 0 : ulong slotn = (ulong)(*(long*)slot);
326 :
327 0 : ulong enc_str_sz = 0;
328 0 : const void* enc_str = json_get_value(values, PATH_ENCODING, 4, &enc_str_sz);
329 0 : fd_rpc_encoding_t enc;
330 0 : if (enc_str == NULL || MATCH_STRING(enc_str, enc_str_sz, "json"))
331 0 : enc = FD_ENC_JSON;
332 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base58"))
333 0 : enc = FD_ENC_BASE58;
334 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64"))
335 0 : enc = FD_ENC_BASE64;
336 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "jsonParsed"))
337 0 : enc = FD_ENC_JSON_PARSED;
338 0 : else {
339 0 : fd_method_error(ctx, -1, "invalid data encoding %s", (const char*)enc_str);
340 0 : return 0;
341 0 : }
342 :
343 0 : ulong maxvers_sz = 0;
344 0 : const void* maxvers = json_get_value(values, PATH_MAXVERS, 4, &maxvers_sz);
345 :
346 0 : ulong det_str_sz = 0;
347 0 : const void* det_str = json_get_value(values, PATH_DETAIL, 4, &det_str_sz);
348 0 : enum fd_block_detail det;
349 0 : if (det_str == NULL || MATCH_STRING(det_str, det_str_sz, "full"))
350 0 : det = FD_BLOCK_DETAIL_FULL;
351 0 : else if (MATCH_STRING(det_str, det_str_sz, "accounts"))
352 0 : det = FD_BLOCK_DETAIL_ACCTS;
353 0 : else if (MATCH_STRING(det_str, det_str_sz, "signatures"))
354 0 : det = FD_BLOCK_DETAIL_SIGS;
355 0 : else if (MATCH_STRING(det_str, det_str_sz, "none"))
356 0 : det = FD_BLOCK_DETAIL_NONE;
357 0 : else {
358 0 : fd_method_error(ctx, -1, "invalid block detail %s", (const char*)det_str);
359 0 : return 0;
360 0 : }
361 :
362 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info( ctx->global->history, slotn );
363 0 : if( info == NULL ) {
364 0 : fd_method_error(ctx, -1, "unable to find slot info");
365 0 : return 0;
366 0 : }
367 0 : fd_replay_notif_msg_t * parent_info = fd_rpc_history_get_block_info( ctx->global->history, info->slot_exec.parent );
368 :
369 0 : ulong blk_sz;
370 0 : uchar * blk_data = fd_rpc_history_get_block( ctx->global->history, slotn, &blk_sz );
371 0 : if( blk_data == NULL ) {
372 0 : fd_method_error(ctx, -1, "failed to display block for slot %lu", slotn);
373 0 : return 0;
374 0 : }
375 :
376 0 : const char * err = fd_block_to_json(ws,
377 0 : ctx->call_id,
378 0 : blk_data,
379 0 : blk_sz,
380 0 : info,
381 0 : parent_info,
382 0 : enc,
383 0 : (maxvers == NULL ? 0 : *(const long*)maxvers),
384 0 : det,
385 0 : NULL,
386 0 : ctx->global->spad );
387 0 : if( err ) {
388 0 : fd_method_error(ctx, -1, "%s", err);
389 0 : return 0;
390 0 : }
391 0 : return 0;
392 0 : }
393 :
394 : // Implementation of the "getBlockCommitment" methods
395 : static int
396 0 : method_getBlockCommitment(struct json_values* values, fd_rpc_ctx_t * ctx) {
397 0 : (void)values;
398 0 : (void)ctx;
399 0 : FD_LOG_WARNING(( "getBlockCommitment is not implemented" ));
400 0 : fd_method_error(ctx, -1, "getBlockCommitment is not implemented");
401 0 : return 0;
402 0 : }
403 :
404 : // Implementation of the "getBlockHeight" method
405 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d '{ "jsonrpc":"2.0","id":1, "method":"getBlockHeight" }'
406 : static int
407 0 : method_getBlockHeight(struct json_values* values, fd_rpc_ctx_t * ctx) {
408 0 : fd_webserver_t * ws = &ctx->global->ws;
409 0 : ulong slot = get_slot_from_commitment_level( values, ctx );
410 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info(ctx->global->history, slot);
411 0 : if( info == NULL ) {
412 0 : fd_method_error(ctx, -1, "block info not available");
413 0 : return 0;
414 0 : }
415 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
416 0 : info->slot_exec.height, ctx->call_id);
417 0 : return 0;
418 0 : }
419 :
420 : // Implementation of the "getBlockProduction" methods
421 :
422 : struct product_rb_node {
423 : fd_pubkey_t key;
424 : uint nleader, nproduced;
425 : ulong redblack_parent;
426 : ulong redblack_left;
427 : ulong redblack_right;
428 : int redblack_color;
429 : };
430 : typedef struct product_rb_node product_rb_node_t;
431 0 : #define REDBLK_T product_rb_node_t
432 : #define REDBLK_NAME product_rb
433 0 : FD_FN_PURE static long product_rb_compare(product_rb_node_t* left, product_rb_node_t* right) {
434 0 : for( uint i = 0; i < sizeof(fd_pubkey_t)/sizeof(ulong); ++i ) {
435 0 : ulong a = left->key.ul[i];
436 0 : ulong b = right->key.ul[i];
437 0 : if( a != b ) return (fd_ulong_bswap( a ) < fd_ulong_bswap( b ) ? -1 : 1);
438 0 : }
439 0 : return 0;
440 0 : }
441 : #include "../../util/tmpl/fd_redblack.c"
442 :
443 : static int
444 0 : method_getBlockProduction(struct json_values* values, fd_rpc_ctx_t * ctx) {
445 0 : (void)values;
446 0 : fd_rpc_global_ctx_t * glob = ctx->global;
447 0 : fd_webserver_t * ws = &glob->ws;
448 0 : fd_blockstore_t * blockstore = glob->blockstore;
449 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
450 0 : ulong startslot = blockstore->shmem->wmk;
451 0 : ulong endslot = blockstore->shmem->lps;
452 :
453 0 : fd_multi_epoch_leaders_lsched_sorted_t lscheds = fd_multi_epoch_leaders_get_sorted_lscheds( glob->leaders );
454 :
455 0 : ulong slots_in_leaders = (lscheds.lscheds[0] ? lscheds.lscheds[0]->slot_cnt : 0UL) +
456 0 : (lscheds.lscheds[1] ? lscheds.lscheds[1]->slot_cnt : 0UL);
457 0 : ulong worst_case_n = fd_ulong_min( slots_in_leaders, (endslot - startslot) / 4UL + 1UL );
458 :
459 0 : void * shmem = fd_spad_alloc( glob->spad, product_rb_align(), product_rb_footprint( worst_case_n ) );
460 0 : product_rb_node_t * pool = product_rb_join( product_rb_new( shmem, worst_case_n ) );
461 0 : product_rb_node_t * root = NULL;
462 :
463 0 : for( ulong i=0UL; i<2UL; i++ ) {
464 0 : const fd_epoch_leaders_t * lsched = lscheds.lscheds[i];
465 0 : if( !lsched ) continue;
466 :
467 0 : ulong const start_slot_in_epoch = fd_ulong_max( startslot, lsched->slot0 );
468 0 : ulong const end_slot_in_epoch = fd_ulong_min( endslot+1, lsched->slot0 + lsched->slot_cnt );
469 : /* we're guaranteed start_slot_in_epoch <= end_slot_in_epoch */
470 :
471 0 : for( ulong j=start_slot_in_epoch; j<end_slot_in_epoch; j++ ) {
472 0 : fd_pubkey_t const * slot_leader = fd_epoch_leaders_get( lsched, j );
473 0 : if( slot_leader ) {
474 0 : product_rb_node_t key;
475 0 : fd_memcpy( key.key.uc, slot_leader->uc, sizeof(fd_pubkey_t) );
476 0 : product_rb_node_t * nd = product_rb_find( pool, root, &key );
477 0 : if( !nd ) {
478 0 : nd = product_rb_acquire( pool );
479 0 : fd_memcpy( nd->key.uc, slot_leader->uc, sizeof(fd_pubkey_t) );
480 0 : nd->nproduced = nd->nleader = 0;
481 0 : product_rb_insert( pool, &root, nd );
482 0 : }
483 0 : nd->nleader++;
484 0 : if( fd_rpc_history_get_block_info(ctx->global->history, j) ) {
485 0 : nd->nproduced++;
486 0 : }
487 0 : }
488 0 : }
489 0 : }
490 :
491 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":{\"byIdentity\":{",
492 0 : fd_rpc_history_latest_slot( glob->history ) );
493 0 : int first=1;
494 0 : for ( product_rb_node_t* nd = product_rb_minimum(pool, root); nd; nd = product_rb_successor(pool, nd) ) {
495 0 : char str[50];
496 0 : fd_base58_encode_32(nd->key.uc, 0, str);
497 0 : fd_web_reply_sprintf(ws, "%s\"%s\":[%u,%u]", (first ? "" : ","), str, nd->nleader, nd->nproduced);
498 0 : first=0;
499 0 : }
500 0 : fd_web_reply_sprintf(ws, "},\"range\":{\"firstSlot\":%lu,\"lastSlot\":%lu}}},\"id\":%s}" CRLF,
501 0 : startslot, endslot, ctx->call_id);
502 0 : } FD_SPAD_FRAME_END;
503 0 : return 0;
504 0 : }
505 :
506 : // Implementation of the "getBlocks" method
507 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc": "2.0", "id": 1, "method": "getBlocks", "params": [270562730, 270562740]} '
508 :
509 : static int
510 0 : method_getBlocks(struct json_values* values, fd_rpc_ctx_t * ctx) {
511 0 : static const uint PATH_STARTSLOT[3] = {
512 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
513 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
514 0 : (JSON_TOKEN_INTEGER<<16)
515 0 : };
516 0 : fd_webserver_t * ws = &ctx->global->ws;
517 0 : ulong startslot_sz = 0;
518 0 : const void* startslot = json_get_value(values, PATH_STARTSLOT, 3, &startslot_sz);
519 0 : if (startslot == NULL) {
520 0 : fd_method_error(ctx, -1, "getBlocks requires a start slot number as first parameter");
521 0 : return 0;
522 0 : }
523 0 : ulong startslotn = (ulong)(*(long*)startslot);
524 0 : static const uint PATH_ENDSLOT[3] = {
525 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
526 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
527 0 : (JSON_TOKEN_INTEGER<<16)
528 0 : };
529 0 : ulong endslot_sz = 0;
530 0 : const void* endslot = json_get_value(values, PATH_ENDSLOT, 3, &endslot_sz);
531 0 : ulong endslotn = (endslot == NULL ? ULONG_MAX : (ulong)(*(long*)endslot));
532 :
533 0 : if (startslotn < fd_rpc_history_first_slot( ctx->global->history ))
534 0 : startslotn = fd_rpc_history_first_slot( ctx->global->history );
535 0 : if (endslotn > fd_rpc_history_latest_slot( ctx->global->history ))
536 0 : endslotn = fd_rpc_history_latest_slot( ctx->global->history );
537 :
538 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":[");
539 0 : uint cnt = 0;
540 0 : for ( ulong i = startslotn; i <= endslotn && cnt < 500000U; ++i ) {
541 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info( ctx->global->history, i );
542 0 : if( info == NULL ) continue;
543 0 : fd_web_reply_sprintf(ws, "%s%lu", (cnt==0 ? "" : ","), i);
544 0 : ++cnt;
545 0 : }
546 0 : fd_web_reply_sprintf(ws, "],\"id\":%s}" CRLF, ctx->call_id);
547 :
548 0 : return 0;
549 0 : }
550 :
551 : // Implementation of the "getBlocksWithLimit" method
552 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc": "2.0", "id":1, "method":"getBlocksWithLimit", "params":[270562730, 3]} '
553 :
554 : static int
555 0 : method_getBlocksWithLimit(struct json_values* values, fd_rpc_ctx_t * ctx) {
556 0 : static const uint PATH_SLOT[3] = {
557 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
558 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
559 0 : (JSON_TOKEN_INTEGER<<16)
560 0 : };
561 0 : fd_webserver_t * ws = &ctx->global->ws;
562 0 : ulong startslot_sz = 0;
563 0 : const void* startslot = json_get_value(values, PATH_SLOT, 3, &startslot_sz);
564 0 : if (startslot == NULL) {
565 0 : fd_method_error(ctx, -1, "getBlocksWithLimit requires a start slot number as first parameter");
566 0 : return 0;
567 0 : }
568 0 : ulong startslotn = (ulong)(*(long*)startslot);
569 0 : static const uint PATH_LIMIT[3] = {
570 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
571 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
572 0 : (JSON_TOKEN_INTEGER<<16)
573 0 : };
574 0 : ulong limit_sz = 0;
575 0 : const void* limit = json_get_value(values, PATH_LIMIT, 3, &limit_sz);
576 0 : if (limit == NULL) {
577 0 : fd_method_error(ctx, -1, "getBlocksWithLimit requires a limit as second parameter");
578 0 : return 0;
579 0 : }
580 0 : ulong limitn = (ulong)(*(long*)limit);
581 :
582 0 : if (startslotn < fd_rpc_history_first_slot( ctx->global->history ))
583 0 : startslotn = fd_rpc_history_first_slot( ctx->global->history );
584 0 : if (limitn > 500000)
585 0 : limitn = 500000;
586 :
587 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":[");
588 0 : uint cnt = 0;
589 0 : for ( ulong i = startslotn; i <= fd_rpc_history_latest_slot( ctx->global->history ) && cnt < limitn; ++i ) {
590 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info( ctx->global->history, i );
591 0 : if( info == NULL ) continue;
592 0 : fd_web_reply_sprintf(ws, "%s%lu", (cnt==0 ? "" : ","), i);
593 0 : ++cnt;
594 0 : }
595 0 : fd_web_reply_sprintf(ws, "],\"id\":%s}" CRLF, ctx->call_id);
596 :
597 0 : return 0;
598 0 : }
599 :
600 : // Implementation of the "getBlockTime" methods
601 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"getBlockTime","params":[280687015]}'
602 :
603 : static int
604 0 : method_getBlockTime(struct json_values* values, fd_rpc_ctx_t * ctx) {
605 0 : static const uint PATH_SLOT[3] = {
606 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
607 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
608 0 : (JSON_TOKEN_INTEGER<<16)
609 0 : };
610 0 : fd_webserver_t * ws = &ctx->global->ws;
611 0 : ulong slot_sz = 0;
612 0 : const void* slot = json_get_value(values, PATH_SLOT, 3, &slot_sz);
613 0 : if (slot == NULL) {
614 0 : fd_method_error(ctx, -1, "getBlockTime requires a slot number as first parameter");
615 0 : return 0;
616 0 : }
617 0 : ulong slotn = (ulong)(*(long*)slot);
618 :
619 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info( ctx->global->history, slotn );
620 0 : if( info == NULL ) {
621 0 : fd_method_error(ctx, -1, "invalid slot: %lu", slotn);
622 0 : return 0;
623 0 : }
624 :
625 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%ld,\"id\":%s}" CRLF,
626 0 : (long)info->slot_exec.ts/(long)1e9,
627 0 : ctx->call_id);
628 0 : return 0;
629 0 : }
630 :
631 : // Implementation of the "getClusterNodes" methods
632 : static int
633 0 : method_getClusterNodes(struct json_values* values, fd_rpc_ctx_t * ctx) {
634 0 : (void)values;
635 0 : (void)ctx;
636 0 : FD_LOG_WARNING(( "getClusterNodes is not implemented" ));
637 0 : fd_method_error(ctx, -1, "getClusterNodes is not implemented");
638 0 : return 0;
639 0 : }
640 :
641 : // Implementation of the "getEpochInfo" methods
642 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getEpochInfo"} '
643 :
644 : static int
645 0 : method_getEpochInfo(struct json_values* values, fd_rpc_ctx_t * ctx) {
646 :
647 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
648 :
649 0 : fd_webserver_t * ws = &ctx->global->ws;
650 0 : ulong slot = get_slot_from_commitment_level( values, ctx );
651 :
652 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info( ctx->global->history, slot );
653 0 : if( info == NULL ) {
654 0 : fd_method_error(ctx, -1, "unable to find slot info");
655 0 : return 0;
656 0 : }
657 :
658 0 : ulong slot_index = 0UL;
659 : // ulong epoch = fd_slot_to_epoch( &epoch_bank->epoch_schedule, slot, &slot_index );
660 0 : ulong epoch = 0UL;
661 :
662 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"absoluteSlot\":%lu,\"blockHeight\":%lu,\"epoch\":%lu,\"slotIndex\":%lu,\"slotsInEpoch\":%lu,\"transactionCount\":%lu},\"id\":%s}" CRLF,
663 0 : slot,
664 0 : info->slot_exec.height,
665 0 : epoch,
666 0 : slot_index,
667 0 : 432000UL,
668 0 : info->slot_exec.transaction_count,
669 0 : ctx->call_id);
670 0 : } FD_SPAD_FRAME_END;
671 0 : return 0;
672 0 : }
673 :
674 : // Implementation of the "getEpochSchedule" methods
675 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getEpochSchedule"} '
676 :
677 : static int
678 0 : method_getEpochSchedule(struct json_values* values, fd_rpc_ctx_t * ctx) {
679 0 : (void)values;
680 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
681 0 : fd_webserver_t * ws = &ctx->global->ws;
682 0 : (void)ws;
683 : // fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"firstNormalEpoch\":%lu,\"firstNormalSlot\":%lu,\"leaderScheduleSlotOffset\":%lu,\"slotsPerEpoch\":%lu,\"warmup\":%s},\"id\":%s}" CRLF,
684 : // epoch_bank->epoch_schedule.first_normal_epoch,
685 : // epoch_bank->epoch_schedule.first_normal_slot,
686 : // epoch_bank->epoch_schedule.leader_schedule_slot_offset,
687 : // epoch_bank->epoch_schedule.slots_per_epoch,
688 : // (epoch_bank->epoch_schedule.warmup ? "true" : "false"),
689 : // ctx->call_id);
690 0 : } FD_SPAD_FRAME_END;
691 0 : return 0;
692 0 : }
693 :
694 : // Implementation of the "getFeeForMessage" methods
695 : static int
696 0 : method_getFeeForMessage(struct json_values* values, fd_rpc_ctx_t * ctx) {
697 0 : fd_webserver_t * ws = &ctx->global->ws;
698 0 : static const uint PATH[3] = {
699 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
700 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
701 0 : (JSON_TOKEN_STRING<<16)
702 0 : };
703 0 : ulong arg_sz = 0;
704 0 : const void* arg = json_get_value(values, PATH, 3, &arg_sz);
705 0 : if (arg == NULL) {
706 0 : fd_method_error(ctx, -1, "getFeeForMessage requires a string as first parameter");
707 0 : return 0;
708 0 : }
709 0 : if( FD_BASE64_DEC_SZ(arg_sz) > FD_TXN_MTU ) {
710 0 : fd_method_error(ctx, -1, "message too large");
711 0 : return 0;
712 0 : }
713 0 : uchar data[FD_TXN_MTU];
714 0 : long res = fd_base64_decode( data, (const char*)arg, arg_sz );
715 0 : if( res < 0 ) {
716 0 : fd_method_error(ctx, -1, "failed to decode base64 data");
717 0 : return 0;
718 0 : }
719 0 : ulong data_sz = (ulong)res;
720 : // TODO: implement this
721 0 : (void)data;
722 0 : (void)data_sz;
723 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":5000},\"id\":%s}" CRLF,
724 0 : fd_rpc_history_latest_slot( ctx->global->history ), ctx->call_id);
725 0 : return 0;
726 0 : }
727 :
728 : // Implementation of the "getFirstAvailableBlock" methods
729 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"getFirstAvailableBlock"}'
730 :
731 : static int
732 0 : method_getFirstAvailableBlock(struct json_values* values, fd_rpc_ctx_t * ctx) {
733 0 : (void) values;
734 0 : fd_webserver_t * ws = &ctx->global->ws;
735 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
736 0 : fd_rpc_history_first_slot( ctx->global->history ), ctx->call_id); /* FIXME archival file */
737 0 : return 0;
738 0 : }
739 :
740 : // Implementation of the "getGenesisHash" methods
741 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getGenesisHash"} '
742 :
743 : static int
744 0 : method_getGenesisHash(struct json_values* values, fd_rpc_ctx_t * ctx) {
745 0 : (void)values;
746 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
747 0 : fd_webserver_t * ws = &ctx->global->ws;
748 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":\"");
749 : // fd_web_reply_encode_base58(ws, epoch_bank->genesis_hash.uc, sizeof(fd_pubkey_t));
750 0 : fd_web_reply_sprintf(ws, "\",\"id\":%s}" CRLF, ctx->call_id);
751 0 : } FD_SPAD_FRAME_END;
752 0 : return 0;
753 0 : }
754 :
755 : // Implementation of the "getHealth" methods
756 : static int
757 0 : method_getHealth(struct json_values* values, fd_rpc_ctx_t * ctx) {
758 0 : (void)values;
759 0 : fd_webserver_t * ws = &ctx->global->ws;
760 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":\"ok\",\"id\":%s}" CRLF, ctx->call_id);
761 0 : return 0;
762 0 : }
763 :
764 : // Implementation of the "getHighestSnapshotSlot" methods
765 : static int
766 0 : method_getHighestSnapshotSlot(struct json_values* values, fd_rpc_ctx_t * ctx) {
767 0 : (void)values;
768 0 : (void)ctx;
769 0 : FD_LOG_WARNING(( "getHighestSnapshotSlot is not implemented" ));
770 0 : fd_method_error(ctx, -1, "getHighestSnapshotSlot is not implemented");
771 0 : return 0;
772 0 : }
773 :
774 : // Implementation of the "getIdentity" method
775 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getIdentity"} '
776 :
777 : static int
778 0 : method_getIdentity(struct json_values* values, fd_rpc_ctx_t * ctx) {
779 0 : fd_webserver_t * ws = &ctx->global->ws;
780 0 : ulong slot = get_slot_from_commitment_level( values, ctx );
781 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info(ctx->global->history, slot);
782 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"identity\":\"");
783 0 : fd_web_reply_encode_base58(ws, &info->slot_exec.identity, sizeof(fd_pubkey_t));
784 0 : fd_web_reply_sprintf(ws, "\"},\"id\":%s}" CRLF, ctx->call_id);
785 0 : return 0;
786 0 : }
787 : // Implementation of the "getInflationGovernor" methods
788 : static int
789 0 : method_getInflationGovernor(struct json_values* values, fd_rpc_ctx_t * ctx) {
790 0 : (void)values;
791 0 : (void)ctx;
792 0 : FD_LOG_WARNING(( "getInflationGovernor is not implemented" ));
793 0 : fd_method_error(ctx, -1, "getInflationGovernor is not implemented");
794 0 : return 0;
795 0 : }
796 :
797 : // Implementation of the "getInflationRate" methods
798 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getInflationRate"} '
799 :
800 : static int
801 0 : method_getInflationRate(struct json_values* values, fd_rpc_ctx_t * ctx) {
802 0 : (void) values;
803 0 : (void)ctx;
804 0 : FD_LOG_WARNING(( "getInflationRate is not implemented" ));
805 0 : fd_method_error(ctx, -1, "getInflationRate is not implemented");
806 0 : return 0;
807 : /* FIXME!
808 : fd_webserver_t * ws = &ctx->global->ws;
809 : fd_inflation_rates_t rates;
810 : calculate_inflation_rates( get_slot_ctx(ctx), &rates );
811 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"epoch\":%lu,\"foundation\":%.18f,\"total\":%.18f,\"validator\":%.18f},\"id\":%s}" CRLF,
812 : rates.epoch,
813 : rates.foundation,
814 : rates.total,
815 : rates.validator,
816 : ctx->call_id);
817 : fd_web_replier_done(replier);
818 : return 0;
819 : */
820 0 : }
821 :
822 : // Implementation of the "getInflationReward" methods
823 : static int
824 0 : method_getInflationReward(struct json_values* values, fd_rpc_ctx_t * ctx) {
825 0 : (void)values;
826 0 : (void)ctx;
827 0 : FD_LOG_WARNING(( "getInflationReward is not implemented" ));
828 0 : fd_method_error(ctx, -1, "getInflationReward is not implemented");
829 0 : return 0;
830 0 : }
831 :
832 : // Implementation of the "getLargestAccounts" methods
833 : static int
834 0 : method_getLargestAccounts(struct json_values* values, fd_rpc_ctx_t * ctx) {
835 0 : (void)values;
836 0 : (void)ctx;
837 0 : FD_LOG_WARNING(( "getLargestAccounts is not implemented" ));
838 0 : fd_method_error(ctx, -1, "getLargestAccounts is not implemented");
839 0 : return 0;
840 0 : }
841 :
842 : // Implementation of the "getLatestBlockhash" methods
843 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getLatestBlockhash"} '
844 :
845 : static int
846 0 : method_getLatestBlockhash(struct json_values* values, fd_rpc_ctx_t * ctx) {
847 0 : (void) values;
848 0 : fd_webserver_t * ws = &ctx->global->ws;
849 0 : ulong slot = get_slot_from_commitment_level( values, ctx );
850 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info(ctx->global->history, slot);
851 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":{\"blockhash\":\"",
852 0 : info->slot_exec.slot);
853 0 : fd_web_reply_encode_base58(ws, &info->slot_exec.block_hash, sizeof(fd_hash_t));
854 0 : fd_web_reply_sprintf(ws, "\",\"lastValidBlockHeight\":%lu}},\"id\":%s}" CRLF,
855 0 : info->slot_exec.height, ctx->call_id);
856 0 : return 0;
857 0 : }
858 :
859 : // Implementation of the "getLeaderSchedule" methods
860 :
861 : struct leader_rb_node {
862 : fd_pubkey_t key;
863 : uint first, last;
864 : ulong redblack_parent;
865 : ulong redblack_left;
866 : ulong redblack_right;
867 : int redblack_color;
868 : };
869 : typedef struct leader_rb_node leader_rb_node_t;
870 0 : #define REDBLK_T leader_rb_node_t
871 : #define REDBLK_NAME leader_rb
872 0 : FD_FN_PURE static long leader_rb_compare(leader_rb_node_t* left, leader_rb_node_t* right) {
873 0 : for( uint i = 0; i < sizeof(fd_pubkey_t)/sizeof(ulong); ++i ) {
874 0 : ulong a = left->key.ul[i];
875 0 : ulong b = right->key.ul[i];
876 0 : if( a != b ) return (fd_ulong_bswap( a ) < fd_ulong_bswap( b ) ? -1 : 1);
877 0 : }
878 0 : return 0;
879 0 : }
880 : #include "../../util/tmpl/fd_redblack.c"
881 :
882 : static int
883 0 : method_getLeaderSchedule(struct json_values* values, fd_rpc_ctx_t * ctx) {
884 0 : (void)values;
885 :
886 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
887 0 : fd_webserver_t * ws = &ctx->global->ws;
888 :
889 0 : ulong slot = get_slot_from_commitment_level( values, ctx );
890 : // fd_epoch_bank_t * epoch_bank = read_epoch_bank(ctx);
891 : // if( epoch_bank == NULL ) {
892 : // fd_method_error(ctx, -1, "unable to read epoch_bank");
893 : // return 0;
894 : // }
895 :
896 0 : fd_epoch_leaders_t const * leaders = fd_multi_epoch_leaders_get_lsched_for_slot( ctx->global->leaders, slot );
897 0 : if( FD_UNLIKELY( !leaders ) ) {
898 0 : fd_method_error(ctx, -1, "unable to get leaders for slot %lu", slot);
899 0 : return 0;
900 0 : }
901 :
902 : /* Reorganize the map to index on sorted leader key */
903 0 : void * shmem = fd_spad_alloc( ctx->global->spad, leader_rb_align(), leader_rb_footprint( leaders->pub_cnt ) );
904 0 : leader_rb_node_t * pool = leader_rb_join( leader_rb_new( shmem, leaders->pub_cnt ) );
905 0 : leader_rb_node_t * root = NULL;
906 0 : uint * nexts = (uint*)fd_spad_alloc( ctx->global->spad, alignof(uint), sizeof(uint) * leaders->sched_cnt );
907 0 : for( uint i = 0; i < leaders->sched_cnt; ++i ) {
908 0 : fd_pubkey_t * pk = leaders->pub + leaders->sched[i];
909 0 : leader_rb_node_t key;
910 0 : fd_memcpy( key.key.uc, pk->uc, sizeof(fd_pubkey_t) );
911 0 : leader_rb_node_t * nd = leader_rb_find( pool, root, &key );
912 0 : if( nd ) {
913 0 : nexts[nd->last] = i;
914 0 : nd->last = i;
915 0 : nexts[i] = UINT_MAX;
916 0 : } else {
917 0 : nd = leader_rb_acquire( pool );
918 0 : fd_memcpy( nd->key.uc, pk->uc, sizeof(fd_pubkey_t) );
919 0 : nd->first = nd->last = i;
920 0 : nexts[i] = UINT_MAX;
921 0 : leader_rb_insert( pool, &root, nd );
922 0 : }
923 0 : }
924 :
925 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{");
926 :
927 0 : int first=1;
928 0 : for ( leader_rb_node_t* nd = leader_rb_minimum(pool, root); nd; nd = leader_rb_successor(pool, nd) ) {
929 0 : char str[50];
930 0 : fd_base58_encode_32(nd->key.uc, 0, str);
931 0 : fd_web_reply_sprintf(ws, "%s\"%s\":[", (first ? "" : ","), str);
932 0 : first=0;
933 0 : int first2=1;
934 0 : for( uint i = nd->first; i != UINT_MAX; i = nexts[i] ) {
935 0 : fd_web_reply_sprintf(ws, "%s%u,%u,%u,%u", (first2 ? "" : ","), i*4, i*4+1, i*4+2, i*4+3);
936 0 : first2=0;
937 0 : }
938 0 : fd_web_reply_sprintf(ws, "]");
939 0 : }
940 :
941 0 : fd_web_reply_sprintf(ws, "},\"id\":%s}" CRLF, ctx->call_id);
942 0 : } FD_SPAD_FRAME_END;
943 0 : return 0;
944 0 : }
945 :
946 : // Implementation of the "getMaxRetransmitSlot" methods
947 : static int
948 0 : method_getMaxRetransmitSlot(struct json_values* values, fd_rpc_ctx_t * ctx) {
949 0 : (void)values;
950 0 : (void)ctx;
951 0 : FD_LOG_WARNING(( "getMaxRetransmitSlot is not implemented" ));
952 0 : fd_method_error(ctx, -1, "getMaxRetransmitSlot is not implemented");
953 0 : return 0;
954 0 : }
955 :
956 : // Implementation of the "getMaxShredInsertSlot" methods
957 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getMaxShredInsertSlot"} '
958 :
959 : static int
960 0 : method_getMaxShredInsertSlot(struct json_values* values, fd_rpc_ctx_t * ctx) {
961 0 : (void) values;
962 0 : fd_blockstore_t * blockstore = ctx->global->blockstore;
963 0 : fd_webserver_t * ws = &ctx->global->ws;
964 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
965 0 : blockstore->shmem->wmk, ctx->call_id); /* FIXME archival file */
966 0 : return 0;
967 0 : }
968 :
969 : // Implementation of the "getMinimumBalanceForRentExemption" methods
970 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc": "2.0", "id": 1, "method": "getMinimumBalanceForRentExemption", "params": [50]} '
971 :
972 : static int
973 0 : method_getMinimumBalanceForRentExemption(struct json_values* values, fd_rpc_ctx_t * ctx) {
974 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
975 0 : static const uint PATH_SIZE[3] = {
976 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
977 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
978 0 : (JSON_TOKEN_INTEGER<<16)
979 0 : };
980 0 : ulong size_sz = 0;
981 0 : const void* size = json_get_value(values, PATH_SIZE, 3, &size_sz);
982 0 : ulong sizen = (size == NULL ? 0UL : (ulong)(*(long*)size));
983 0 : (void)sizen;
984 : // ulong min_balance = fd_rent_exempt_minimum_balance( &epoch_bank->rent, sizen );
985 0 : ulong min_balance = 0UL;
986 :
987 0 : fd_webserver_t * ws = &ctx->global->ws;
988 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
989 0 : min_balance, ctx->call_id);
990 0 : } FD_SPAD_FRAME_END;
991 0 : return 0;
992 0 : }
993 :
994 : // Implementation of the "getMultipleAccounts" method
995 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc": "2.0", "id": 1, "method": "getMultipleAccounts", "params": [["Cwg1f6m4m3DGwMEbmsbAfDtUToUf5jRdKrJSGD7GfZCB", "Cwg1f6m4m3DGwMEbmsbAfDtUToUf5jRdKrJSGD7GfZCB", "7935owQYeYk1H6HjzKRYnT1aZpf1uXcpZNYjgTZ8q7VR"], {"encoding": "base64"}]} '
996 :
997 : static int
998 0 : method_getMultipleAccounts(struct json_values* values, fd_rpc_ctx_t * ctx) {
999 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
1000 0 : static const uint ENC_PATH[4] = {
1001 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1002 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
1003 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ENCODING,
1004 0 : (JSON_TOKEN_STRING<<16)
1005 0 : };
1006 0 : fd_webserver_t * ws = &ctx->global->ws;
1007 0 : ulong enc_str_sz = 0;
1008 0 : const void* enc_str = json_get_value(values, ENC_PATH, 4, &enc_str_sz);
1009 0 : fd_rpc_encoding_t enc;
1010 0 : if (enc_str == NULL || MATCH_STRING(enc_str, enc_str_sz, "base58"))
1011 0 : enc = FD_ENC_BASE58;
1012 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64"))
1013 0 : enc = FD_ENC_BASE64;
1014 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64+zstd"))
1015 0 : enc = FD_ENC_BASE64_ZSTD;
1016 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "jsonParsed"))
1017 0 : enc = FD_ENC_JSON;
1018 0 : else {
1019 0 : fd_method_error(ctx, -1, "invalid data encoding %s", (const char*)enc_str);
1020 0 : return 0;
1021 0 : }
1022 :
1023 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":[",
1024 0 : fd_rpc_history_latest_slot( ctx->global->history ));
1025 :
1026 : // Iterate through account ids
1027 0 : for ( ulong i = 0; ; ++i ) {
1028 : // Path to argument
1029 0 : uint path[4];
1030 0 : path[0] = (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS;
1031 0 : path[1] = (JSON_TOKEN_LBRACKET<<16) | 0;
1032 0 : path[2] = (uint) ((JSON_TOKEN_LBRACKET<<16) | i);
1033 0 : path[3] = (JSON_TOKEN_STRING<<16);
1034 0 : ulong arg_sz = 0;
1035 0 : const void* arg = json_get_value(values, path, 4, &arg_sz);
1036 0 : if (arg == NULL)
1037 : // End of list
1038 0 : break;
1039 :
1040 0 : if (i > 0)
1041 0 : fd_web_reply_append(ws, ",", 1);
1042 :
1043 0 : fd_pubkey_t acct;
1044 0 : if( fd_base58_decode_32((const char *)arg, acct.uc) == NULL ) {
1045 0 : fd_method_error(ctx, -1, "invalid base58 encoding");
1046 0 : return 0;
1047 0 : }
1048 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
1049 0 : ulong val_sz;
1050 0 : fd_funk_rec_key_t recid = fd_funk_acc_key(&acct);
1051 0 : const void * val = read_account(ctx, &recid, &val_sz);
1052 0 : if (val == NULL) {
1053 0 : fd_web_reply_sprintf(ws, "null");
1054 0 : continue;
1055 0 : }
1056 :
1057 0 : const char * err = fd_account_to_json( ws, acct, enc, val, val_sz, FD_LONG_UNSET, FD_LONG_UNSET, ctx->global->spad );
1058 0 : if( err ) {
1059 0 : fd_method_error(ctx, -1, "%s", err);
1060 0 : return 0;
1061 0 : }
1062 0 : } FD_SPAD_FRAME_END;
1063 0 : }
1064 :
1065 0 : fd_web_reply_sprintf(ws, "]},\"id\":%s}" CRLF, ctx->call_id);
1066 0 : } FD_SPAD_FRAME_END;
1067 0 : return 0;
1068 0 : }
1069 :
1070 : // Implementation of the "getProgramAccounts" methods
1071 : static int
1072 0 : method_getProgramAccounts(struct json_values* values, fd_rpc_ctx_t * ctx) {
1073 0 : (void)values;
1074 0 : (void)ctx;
1075 0 : FD_LOG_WARNING(( "getProgramAccounts is not implemented" ));
1076 0 : fd_method_error(ctx, -1, "getProgramAccounts is not implemented");
1077 0 : return 0;
1078 0 : }
1079 :
1080 : // Implementation of the "getRecentPerformanceSamples" methods
1081 : static int
1082 0 : method_getRecentPerformanceSamples(struct json_values* values, fd_rpc_ctx_t * ctx) {
1083 0 : (void)values;
1084 0 : (void)ctx;
1085 :
1086 0 : fd_webserver_t * ws = &ctx->global->ws;
1087 :
1088 0 : static const uint PATH_LIMIT[3] = {
1089 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1090 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
1091 0 : (JSON_TOKEN_INTEGER<<16)
1092 0 : };
1093 :
1094 0 : ulong limit_sz = 0;
1095 0 : const void* limit = json_get_value(values, PATH_LIMIT, 3, &limit_sz);
1096 0 : if( FD_UNLIKELY( !limit ) ) {
1097 0 : fd_method_error( ctx, -1, "getRecentPerformanceSamples requires a number as first parameter" );
1098 0 : return 0;
1099 0 : }
1100 0 : ulong limitn = (ulong)(*(long*)limit);
1101 :
1102 0 : ulong cnt = fd_ulong_min( fd_perf_sample_deque_cnt( ctx->global->perf_samples ), limitn );
1103 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":[");
1104 :
1105 0 : for (ulong i = 0; i < cnt; i++) {
1106 0 : fd_perf_sample_t const * perf_sample = fd_perf_sample_deque_peek_index_const( ctx->global->perf_samples, i );
1107 0 : FD_TEST( perf_sample );
1108 0 : fd_web_reply_sprintf(ws, "{\"numSlots\":%lu,\"numTransactions\":%lu,\"numNonVoteTransactions\":%lu,\"samplePeriodSecs\":60,\"slot\":%lu}", perf_sample->num_slots, perf_sample->num_transactions, perf_sample->num_non_vote_transactions, perf_sample->highest_slot );
1109 0 : if ( FD_LIKELY( i < cnt - 1 ) ) {
1110 0 : fd_web_reply_sprintf(ws, ",");
1111 0 : }
1112 0 : }
1113 0 : fd_web_reply_sprintf(ws, "]}");
1114 :
1115 0 : return 0;
1116 0 : }
1117 :
1118 : // Implementation of the "getRecentPrioritizationFees" methods
1119 : static int
1120 0 : method_getRecentPrioritizationFees(struct json_values* values, fd_rpc_ctx_t * ctx) {
1121 0 : (void)values;
1122 0 : (void)ctx;
1123 0 : FD_LOG_WARNING(( "getRecentPrioritizationFees is not implemented" ));
1124 0 : fd_method_error(ctx, -1, "getRecentPrioritizationFees is not implemented");
1125 0 : return 0;
1126 0 : }
1127 :
1128 : // Implementation of the "getSignaturesForAddress" methods
1129 : static int
1130 0 : method_getSignaturesForAddress(struct json_values* values, fd_rpc_ctx_t * ctx) {
1131 0 : fd_webserver_t * ws = &ctx->global->ws;
1132 :
1133 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
1134 : // Path to argument
1135 0 : static const uint PATH[3] = {
1136 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1137 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
1138 0 : (JSON_TOKEN_STRING<<16)
1139 0 : };
1140 0 : ulong arg_sz = 0;
1141 0 : const void* arg = json_get_value(values, PATH, 3, &arg_sz);
1142 0 : if (arg == NULL) {
1143 0 : fd_method_error(ctx, -1, "getSignaturesForAddress requires a string as first parameter");
1144 0 : return 0;
1145 0 : }
1146 0 : fd_pubkey_t acct;
1147 0 : if( fd_base58_decode_32((const char *)arg, acct.uc) == NULL ) {
1148 0 : fd_method_error(ctx, -1, "invalid base58 encoding");
1149 0 : return 0;
1150 0 : }
1151 :
1152 0 : static const uint PATH2[4] = {
1153 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1154 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
1155 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_LIMIT,
1156 0 : (JSON_TOKEN_INTEGER<<16)
1157 0 : };
1158 0 : ulong limit_sz = 0;
1159 0 : const void* limit_ptr = json_get_value(values, PATH2, 4, &limit_sz);
1160 0 : ulong limit = ( limit_ptr ? fd_ulong_min( *(const ulong*)limit_ptr, 1000U ) : 1000U );
1161 :
1162 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":[");
1163 0 : fd_rpc_global_ctx_t * gctx = ctx->global;
1164 0 : fd_rpc_txn_key_t sig;
1165 0 : ulong slot;
1166 0 : const void * iter = fd_rpc_history_first_txn_for_acct( gctx->history, &acct, &sig, &slot );
1167 0 : ulong cnt = 0;
1168 0 : while( iter != NULL && cnt < limit ) {
1169 0 : if( cnt ) EMIT_SIMPLE(",");
1170 :
1171 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info( ctx->global->history, slot );
1172 0 : char buf64[FD_BASE58_ENCODED_64_SZ];
1173 0 : fd_base58_encode_64((uchar const*)&sig, NULL, buf64);
1174 0 : fd_web_reply_sprintf(ws, "{\"blockTime\":%ld,\"confirmationStatus\":%s,\"err\":null,\"memo\":null,\"signature\":\"%s\",\"slot\":%lu}",
1175 0 : (long)info->slot_exec.ts/(long)1e9, "confirmed", buf64, slot);
1176 :
1177 0 : cnt++;
1178 :
1179 0 : iter = fd_rpc_history_next_txn_for_acct( gctx->history, &sig, &slot, iter );
1180 0 : }
1181 0 : fd_web_reply_sprintf(ws, "],\"id\":%s}" CRLF, ctx->call_id);
1182 :
1183 0 : } FD_SPAD_FRAME_END;
1184 :
1185 0 : return 0;
1186 0 : }
1187 :
1188 : // Implementation of the "getSignatureStatuses" methods
1189 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc": "2.0", "id": 1, "method": "getSignatureStatuses", "params": [["4qj8WecUytFE96SFhdiTkc3v2AYLY7795sbSQTnYG7cPL9s6xKNHNyi3wraQc83PsNSgV8yedWbfGa4vRXfzBDzB"], {"searchTransactionHistory": true}]} '
1190 :
1191 : static int
1192 0 : method_getSignatureStatuses(struct json_values* values, fd_rpc_ctx_t * ctx) {
1193 0 : fd_webserver_t * ws = &ctx->global->ws;
1194 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":[",
1195 0 : fd_rpc_history_latest_slot( ctx->global->history ));
1196 :
1197 : // Iterate through account ids
1198 0 : for ( ulong i = 0; ; ++i ) {
1199 : // Path to argument
1200 0 : uint path[4];
1201 0 : path[0] = (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS;
1202 0 : path[1] = (JSON_TOKEN_LBRACKET<<16) | 0;
1203 0 : path[2] = (uint) ((JSON_TOKEN_LBRACKET<<16) | i);
1204 0 : path[3] = (JSON_TOKEN_STRING<<16);
1205 0 : ulong sig_sz = 0;
1206 0 : const void* sig = json_get_value(values, path, 4, &sig_sz);
1207 0 : if (sig == NULL)
1208 : // End of list
1209 0 : break;
1210 :
1211 0 : if (i > 0)
1212 0 : fd_web_reply_append(ws, ",", 1);
1213 :
1214 0 : fd_rpc_txn_key_t key;
1215 0 : if ( fd_base58_decode_64( sig, (uchar*)&key) == NULL ) {
1216 0 : fd_web_reply_sprintf(ws, "null");
1217 0 : continue;
1218 0 : }
1219 :
1220 0 : ulong txn_sz;
1221 0 : ulong slot;
1222 0 : uchar * txn_data_raw = fd_rpc_history_get_txn( ctx->global->history, &key, &txn_sz, &slot );
1223 0 : if( txn_data_raw == NULL ) {
1224 0 : fd_web_reply_sprintf(ws, "null");
1225 0 : continue;
1226 0 : }
1227 :
1228 : // TODO other fields
1229 0 : fd_web_reply_sprintf(ws, "{\"slot\":%lu,\"confirmations\":null,\"err\":null,\"status\":{\"Ok\":null},\"confirmationStatus\":\"%s\"}",
1230 0 : slot, "processed");
1231 0 : }
1232 :
1233 0 : fd_web_reply_sprintf(ws, "]},\"id\":%s}" CRLF, ctx->call_id);
1234 0 : return 0;
1235 0 : }
1236 :
1237 : // Implementation of the "getSlot" method
1238 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getSlot"} '
1239 :
1240 : static int
1241 0 : method_getSlot(struct json_values* values, fd_rpc_ctx_t * ctx) {
1242 0 : fd_webserver_t * ws = &ctx->global->ws;
1243 0 : ulong slot = get_slot_from_commitment_level( values, ctx );
1244 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
1245 0 : slot, ctx->call_id);
1246 0 : return 0;
1247 0 : }
1248 :
1249 : // Implementation of the "getSlotLeader" methods
1250 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getSlotLeader"} '
1251 :
1252 : static int
1253 0 : method_getSlotLeader(struct json_values* values, fd_rpc_ctx_t * ctx) {
1254 0 : fd_webserver_t * ws = &ctx->global->ws;
1255 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":");
1256 0 : ulong slot = get_slot_from_commitment_level( values, ctx );
1257 0 : fd_pubkey_t const * slot_leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->global->leaders, slot );
1258 0 : if( slot_leader ) {
1259 0 : char str[50];
1260 0 : fd_base58_encode_32(slot_leader->uc, 0, str);
1261 0 : fd_web_reply_sprintf(ws, "\"%s\"", str);
1262 0 : } else {
1263 0 : EMIT_SIMPLE("null");
1264 0 : }
1265 0 : fd_web_reply_sprintf(ws, ",\"id\":%s}" CRLF, ctx->call_id);
1266 0 : return 0;
1267 0 : }
1268 :
1269 : // Implementation of the "getSlotLeaders" methods
1270 : static int
1271 0 : method_getSlotLeaders(struct json_values* values, fd_rpc_ctx_t * ctx) {
1272 0 : static const uint PATH_SLOT[3] = {
1273 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1274 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
1275 0 : (JSON_TOKEN_INTEGER<<16)
1276 0 : };
1277 0 : fd_webserver_t * ws = &ctx->global->ws;
1278 0 : ulong startslot_sz = 0;
1279 0 : const void* startslot = json_get_value(values, PATH_SLOT, 3, &startslot_sz);
1280 0 : if (startslot == NULL) {
1281 0 : fd_method_error(ctx, -1, "getSlotLeaders requires a start slot number as first parameter");
1282 0 : return 0;
1283 0 : }
1284 0 : ulong startslotn = (ulong)(*(long*)startslot);
1285 0 : static const uint PATH_LIMIT[3] = {
1286 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1287 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
1288 0 : (JSON_TOKEN_INTEGER<<16)
1289 0 : };
1290 0 : ulong limit_sz = 0;
1291 0 : const void* limit = json_get_value(values, PATH_LIMIT, 3, &limit_sz);
1292 0 : if (limit == NULL) {
1293 0 : fd_method_error(ctx, -1, "getSlotLeaders requires a limit as second parameter");
1294 0 : return 0;
1295 0 : }
1296 0 : ulong limitn = (ulong)(*(long*)limit);
1297 0 : if (limitn > 5000)
1298 0 : limitn = 5000;
1299 :
1300 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":[");
1301 0 : fd_epoch_leaders_t const * lsched = fd_multi_epoch_leaders_get_lsched_for_slot( ctx->global->leaders, startslotn );
1302 0 : if( lsched ) {
1303 0 : for ( ulong i = startslotn; i < startslotn + limitn; ++i ) {
1304 0 : if( i > startslotn ) EMIT_SIMPLE(",");
1305 0 : fd_pubkey_t const * slot_leader = fd_epoch_leaders_get( lsched, i );
1306 0 : if( slot_leader ) {
1307 0 : char str[50];
1308 0 : fd_base58_encode_32(slot_leader->uc, 0, str);
1309 0 : fd_web_reply_sprintf(ws, "\"%s\"", str);
1310 0 : } else {
1311 0 : EMIT_SIMPLE("null");
1312 0 : }
1313 0 : }
1314 0 : }
1315 0 : fd_web_reply_sprintf(ws, "],\"id\":%s}" CRLF, ctx->call_id);
1316 :
1317 0 : return 0;
1318 0 : }
1319 :
1320 : // Implementation of the "getStakeActivation" methods
1321 : static int
1322 0 : method_getStakeActivation(struct json_values* values, fd_rpc_ctx_t * ctx) {
1323 0 : (void)values;
1324 0 : (void)ctx;
1325 0 : FD_LOG_WARNING(( "getStakeActivation is not implemented" ));
1326 0 : fd_method_error(ctx, -1, "getStakeActivation is not implemented");
1327 0 : return 0;
1328 0 : }
1329 :
1330 : // Implementation of the "getStakeMinimumDelegation" methods
1331 : static int
1332 0 : method_getStakeMinimumDelegation(struct json_values* values, fd_rpc_ctx_t * ctx) {
1333 0 : (void)values;
1334 0 : (void)ctx;
1335 0 : FD_LOG_WARNING(( "getStakeMinimumDelegation is not implemented" ));
1336 0 : fd_method_error(ctx, -1, "getStakeMinimumDelegation is not implemented");
1337 0 : return 0;
1338 0 : }
1339 :
1340 : // Implementation of the "getSupply" methods
1341 : // TODO
1342 : static int
1343 0 : method_getSupply(struct json_values* values, fd_rpc_ctx_t * ctx) {
1344 0 : (void)values;
1345 0 : (void)ctx;
1346 0 : FD_LOG_WARNING(( "getSupply is not implemented" ));
1347 0 : fd_method_error(ctx, -1, "getSupply is not implemented");
1348 0 : return 0;
1349 0 : }
1350 :
1351 : // Implementation of the "getTokenAccountBalance" methods
1352 : static int
1353 0 : method_getTokenAccountBalance(struct json_values* values, fd_rpc_ctx_t * ctx) {
1354 0 : (void)values;
1355 0 : (void)ctx;
1356 0 : FD_LOG_WARNING(( "getTokenAccountBalance is not implemented" ));
1357 0 : fd_method_error(ctx, -1, "getTokenAccountBalance is not implemented");
1358 0 : return 0;
1359 0 : }
1360 :
1361 : // Implementation of the "getTokenAccountsByDelegate" methods
1362 : static int
1363 0 : method_getTokenAccountsByDelegate(struct json_values* values, fd_rpc_ctx_t * ctx) {
1364 0 : (void)values;
1365 0 : (void)ctx;
1366 0 : FD_LOG_WARNING(( "getTokenAccountsByDelegate is not implemented" ));
1367 0 : fd_method_error(ctx, -1, "getTokenAccountsByDelegate is not implemented");
1368 0 : return 0;
1369 0 : }
1370 :
1371 : // Implementation of the "getTokenAccountsByOwner" methods
1372 : static int
1373 0 : method_getTokenAccountsByOwner(struct json_values* values, fd_rpc_ctx_t * ctx) {
1374 0 : (void)values;
1375 0 : (void)ctx;
1376 0 : FD_LOG_WARNING(( "getTokenAccountsByOwner is not implemented" ));
1377 0 : fd_method_error(ctx, -1, "getTokenAccountsByOwner is not implemented");
1378 0 : return 0;
1379 0 : }
1380 :
1381 : // Implementation of the "getTokenLargestAccounts" methods
1382 : static int
1383 0 : method_getTokenLargestAccounts(struct json_values* values, fd_rpc_ctx_t * ctx) {
1384 0 : (void)values;
1385 0 : (void)ctx;
1386 0 : FD_LOG_WARNING(( "getTokenLargestAccounts is not implemented" ));
1387 0 : fd_method_error(ctx, -1, "getTokenLargestAccounts is not implemented");
1388 0 : return 0;
1389 0 : }
1390 :
1391 : // Implementation of the "getTokenSupply" methods
1392 : static int
1393 0 : method_getTokenSupply(struct json_values* values, fd_rpc_ctx_t * ctx) {
1394 0 : (void)values;
1395 0 : (void)ctx;
1396 0 : FD_LOG_WARNING(( "getTokenSupply is not implemented" ));
1397 0 : fd_method_error(ctx, -1, "getTokenSupply is not implemented");
1398 0 : return 0;
1399 0 : }
1400 :
1401 : // Implementation of the "getTransaction" method
1402 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc": "2.0", "id": 1, "method": "getTransaction", "params": ["4qj8WecUytFE96SFhdiTkc3v2AYLY7795sbSQTnYG7cPL9s6xKNHNyi3wraQc83PsNSgV8yedWbfGa4vRXfzBDzB", "json"]} '
1403 :
1404 : static int
1405 0 : method_getTransaction(struct json_values* values, fd_rpc_ctx_t * ctx) {
1406 0 : static const uint PATH_SIG[3] = {
1407 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1408 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
1409 0 : (JSON_TOKEN_STRING<<16)
1410 0 : };
1411 0 : static const uint PATH_ENCODING[3] = {
1412 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1413 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
1414 0 : (JSON_TOKEN_STRING<<16)
1415 0 : };
1416 0 : static const uint PATH_ENCODING2[4] = {
1417 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1418 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
1419 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ENCODING,
1420 0 : (JSON_TOKEN_STRING<<16)
1421 0 : };
1422 :
1423 0 : fd_webserver_t * ws = &ctx->global->ws;
1424 0 : ulong sig_sz = 0;
1425 0 : const void* sig = json_get_value(values, PATH_SIG, 3, &sig_sz);
1426 0 : if (sig == NULL) {
1427 0 : fd_method_error(ctx, -1, "getTransaction requires a signature as first parameter");
1428 0 : return 0;
1429 0 : }
1430 :
1431 0 : ulong enc_str_sz = 0;
1432 0 : const void* enc_str = json_get_value(values, PATH_ENCODING, 3, &enc_str_sz);
1433 0 : if (enc_str == NULL) enc_str = json_get_value(values, PATH_ENCODING2, 4, &enc_str_sz);
1434 0 : fd_rpc_encoding_t enc;
1435 0 : if (enc_str == NULL || MATCH_STRING(enc_str, enc_str_sz, "json"))
1436 0 : enc = FD_ENC_JSON;
1437 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base58"))
1438 0 : enc = FD_ENC_BASE58;
1439 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64"))
1440 0 : enc = FD_ENC_BASE64;
1441 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "jsonParsed"))
1442 0 : enc = FD_ENC_JSON_PARSED;
1443 0 : else {
1444 0 : fd_method_error(ctx, -1, "invalid data encoding %s", (const char*)enc_str);
1445 0 : return 0;
1446 0 : }
1447 :
1448 0 : ulong commit_str_sz = 0;
1449 0 : const void* commit_str = json_get_value(values, PATH_COMMITMENT, 4, &commit_str_sz);
1450 0 : uchar need_blk_flags;
1451 0 : if (commit_str == NULL || MATCH_STRING(commit_str, commit_str_sz, "processed"))
1452 0 : need_blk_flags = (uchar)(1U << FD_BLOCK_FLAG_PROCESSED);
1453 0 : else if (MATCH_STRING(commit_str, commit_str_sz, "confirmed"))
1454 0 : need_blk_flags = (uchar)(1U << FD_BLOCK_FLAG_CONFIRMED);
1455 0 : else if (MATCH_STRING(commit_str, commit_str_sz, "finalized"))
1456 0 : need_blk_flags = (uchar)(1U << FD_BLOCK_FLAG_FINALIZED);
1457 0 : else {
1458 0 : fd_method_error(ctx, -1, "invalid commitment %s", (const char*)commit_str);
1459 0 : return 0;
1460 0 : }
1461 0 : (void)need_blk_flags;
1462 :
1463 0 : fd_rpc_txn_key_t key;
1464 0 : if ( fd_base58_decode_64( sig, (uchar*)&key) == NULL ) {
1465 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":%s}" CRLF, ctx->call_id);
1466 0 : return 0;
1467 0 : }
1468 0 : ulong txn_sz;
1469 0 : ulong slot;
1470 0 : uchar * txn_data_raw = fd_rpc_history_get_txn( ctx->global->history, &key, &txn_sz, &slot );
1471 0 : if( txn_data_raw == NULL ) {
1472 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":%s}" CRLF, ctx->call_id);
1473 0 : return 0;
1474 0 : }
1475 0 : uchar txn_out[FD_TXN_MAX_SZ];
1476 0 : ulong pay_sz = 0;
1477 0 : ulong txn_sz2 = fd_txn_parse_core(txn_data_raw, txn_sz, txn_out, NULL, &pay_sz);
1478 0 : if ( txn_sz2 == 0 || txn_sz2 > FD_TXN_MAX_SZ || txn_sz != pay_sz ) {
1479 0 : FD_LOG_ERR(("failed to parse transaction"));
1480 0 : }
1481 :
1482 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info( ctx->global->history, slot );
1483 :
1484 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"blockTime\":%ld,\"slot\":%lu,",
1485 0 : fd_rpc_history_latest_slot( ctx->global->history ), (long)info->slot_exec.ts/(long)1e9, slot);
1486 :
1487 0 : const char * err = fd_txn_to_json( ws, (fd_txn_t *)txn_out, txn_data_raw, pay_sz, enc, 0, FD_BLOCK_DETAIL_FULL, ctx->global->spad );
1488 0 : if( err ) {
1489 0 : fd_method_error(ctx, -1, "%s", err);
1490 0 : return 0;
1491 0 : }
1492 0 : fd_web_reply_sprintf(ws, "},\"id\":%s}" CRLF, ctx->call_id);
1493 :
1494 0 : return 0;
1495 0 : }
1496 :
1497 : // Implementation of the "getTransactionCount" methods
1498 : static int
1499 0 : method_getTransactionCount(struct json_values* values, fd_rpc_ctx_t * ctx) {
1500 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
1501 0 : (void)values;
1502 0 : fd_webserver_t * ws = &ctx->global->ws;
1503 :
1504 0 : ulong slot = get_slot_from_commitment_level( values, ctx );
1505 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info( ctx->global->history, slot );
1506 0 : if( FD_UNLIKELY( !info ) ) {
1507 0 : fd_method_error( ctx, -1, "slot bank %lu not found", slot );
1508 0 : return 0;
1509 0 : }
1510 : /* FIXME: should be the transaction count here */
1511 0 : fd_web_reply_sprintf( ws,
1512 0 : "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
1513 0 : 0UL,
1514 0 : ctx->call_id );
1515 0 : } FD_SPAD_FRAME_END;
1516 0 : return 0;
1517 0 : }
1518 :
1519 : // Implementation of the "getVersion" method
1520 : // curl http://localhost:8123 -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getVersion"} '
1521 :
1522 : static int
1523 0 : method_getVersion(struct json_values* values, fd_rpc_ctx_t * ctx) {
1524 0 : (void) values;
1525 0 : fd_webserver_t * ws = &ctx->global->ws;
1526 : /* TODO Where does feature-set come from? */
1527 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"feature-set\":666,\"solana-core\":\"" FIREDANCER_VERSION "\"},\"id\":%s}" CRLF,
1528 0 : ctx->call_id);
1529 0 : return 0;
1530 0 : }
1531 :
1532 : static int
1533 0 : method_getVoteAccounts(struct json_values* values, fd_rpc_ctx_t * ctx) {
1534 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
1535 0 : fd_webserver_t * ws = &ctx->global->ws;
1536 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"current\":[");
1537 :
1538 0 : uint path[4] = {
1539 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1540 0 : (uint) ((JSON_TOKEN_LBRACKET<<16) | 0),
1541 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_VOTEPUBKEY,
1542 0 : (JSON_TOKEN_STRING<<16)
1543 0 : };
1544 0 : ulong arg_sz = 0;
1545 0 : const void* arg = json_get_value(values, path, 4, &arg_sz);
1546 0 : (void)arg; // Ignore for now
1547 :
1548 : // ulong slot = get_slot_from_commitment_level( values, ctx );
1549 : // fd_slot_bank_t * slot_bank = read_slot_bank( ctx, slot );
1550 : // if( FD_UNLIKELY( !slot_bank ) ) {
1551 : // fd_method_error( ctx, -1, "slot bank %lu not found", slot );
1552 : // return 0;
1553 : // }
1554 :
1555 0 : int needcomma = 0;
1556 :
1557 0 : fd_clock_timestamp_vote_t_mapnode_t * timestamp_votes_root = NULL;
1558 0 : fd_clock_timestamp_vote_t_mapnode_t * timestamp_votes_pool = NULL;
1559 0 : fd_vote_accounts_pair_t_mapnode_t * vote_acc_root = NULL; // slot_bank->epoch_stakes.vote_accounts_root;
1560 0 : fd_vote_accounts_pair_t_mapnode_t * vote_acc_pool = NULL; // slot_bank->epoch_stakes.vote_accounts_pool;
1561 0 : for( fd_vote_accounts_pair_t_mapnode_t* n = fd_vote_accounts_pair_t_map_minimum(vote_acc_pool, vote_acc_root);
1562 0 : n;
1563 0 : n = fd_vote_accounts_pair_t_map_successor( vote_acc_pool, n ) ) {
1564 :
1565 : /* get timestamp */
1566 0 : fd_pubkey_t const * vote_pubkey = &n->elem.key;
1567 :
1568 0 : if( timestamp_votes_pool == NULL ) {
1569 0 : continue;
1570 0 : } else {
1571 0 : fd_clock_timestamp_vote_t_mapnode_t query_vote_acc_node;
1572 0 : query_vote_acc_node.elem.pubkey = *vote_pubkey;
1573 0 : fd_clock_timestamp_vote_t_mapnode_t * vote_acc_node = fd_clock_timestamp_vote_t_map_find(timestamp_votes_pool, timestamp_votes_root, &query_vote_acc_node);
1574 0 : ulong vote_timestamp = 0;
1575 0 : ulong vote_slot = 0;
1576 0 : if( vote_acc_node == NULL ) {
1577 0 : int err;
1578 0 : fd_vote_state_versioned_t * vsv = fd_bincode_decode_spad(
1579 0 : vote_state_versioned, ctx->global->spad,
1580 0 : n->elem.value.data,
1581 0 : n->elem.value.data_len,
1582 0 : &err );
1583 0 : if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
1584 0 : FD_LOG_WARNING(( "Vote state versioned decode failed" ));
1585 0 : continue;
1586 0 : }
1587 :
1588 0 : switch( vsv->discriminant ) {
1589 0 : case fd_vote_state_versioned_enum_v0_23_5:
1590 0 : vote_timestamp = (ulong)vsv->inner.v0_23_5.last_timestamp.timestamp;
1591 0 : vote_slot = vsv->inner.v0_23_5.last_timestamp.slot;
1592 0 : break;
1593 0 : case fd_vote_state_versioned_enum_v1_14_11:
1594 0 : vote_timestamp = (ulong)vsv->inner.v1_14_11.last_timestamp.timestamp;
1595 0 : vote_slot = vsv->inner.v1_14_11.last_timestamp.slot;
1596 0 : break;
1597 0 : case fd_vote_state_versioned_enum_current:
1598 0 : vote_timestamp = (ulong)vsv->inner.current.last_timestamp.timestamp;
1599 0 : vote_slot = vsv->inner.current.last_timestamp.slot;
1600 0 : break;
1601 0 : default:
1602 0 : __builtin_unreachable();
1603 0 : }
1604 :
1605 0 : } else {
1606 0 : vote_timestamp = (ulong)vote_acc_node->elem.timestamp;
1607 0 : vote_slot = vote_acc_node->elem.slot;
1608 0 : }
1609 0 : (void)vote_timestamp;
1610 :
1611 0 : char pubkey[50];
1612 0 : fd_base58_encode_32(vote_pubkey->uc, 0, pubkey);
1613 0 : char key[50];
1614 0 : fd_base58_encode_32(n->elem.key.uc, 0, key);
1615 0 : if( needcomma ) fd_web_reply_sprintf(ws, ",");
1616 0 : fd_web_reply_sprintf(ws, "{\"commission\":0,\"epochVoteAccount\":true,\"epochCredits\":[],\"nodePubkey\":\"%s\",\"lastVote\":%lu,\"activatedStake\":%lu,\"votePubkey\":\"%s\",\"rootSlot\":0}",
1617 0 : pubkey, vote_slot, n->elem.stake, key);
1618 0 : needcomma = 1;
1619 0 : }
1620 0 : }
1621 :
1622 0 : fd_web_reply_sprintf(ws, "]},\"id\":%s}" CRLF, ctx->call_id);
1623 0 : } FD_SPAD_FRAME_END;
1624 0 : return 0;
1625 0 : }
1626 :
1627 : // Implementation of the "isBlockhashValid" methods
1628 : static int
1629 0 : method_isBlockhashValid(struct json_values* values, fd_rpc_ctx_t * ctx) {
1630 0 : fd_rpc_global_ctx_t * glob = ctx->global;
1631 0 : fd_webserver_t * ws = &glob->ws;
1632 :
1633 : // Path to argument
1634 0 : static const uint PATH[3] = {
1635 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1636 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
1637 0 : (JSON_TOKEN_STRING<<16)
1638 0 : };
1639 0 : ulong arg_sz = 0;
1640 0 : const void* arg = json_get_value(values, PATH, 3, &arg_sz);
1641 0 : if (arg == NULL) {
1642 0 : fd_method_error(ctx, -1, "isBlockhashValid requires a string as first parameter");
1643 0 : return 0;
1644 0 : }
1645 :
1646 0 : fd_hash_t h;
1647 0 : if( fd_base58_decode_32((const char *)arg, h.uc) == NULL ) {
1648 0 : fd_method_error(ctx, -1, "invalid base58 encoding");
1649 0 : return 0;
1650 0 : }
1651 :
1652 0 : fd_replay_notif_msg_t * info = fd_rpc_history_get_block_info_by_hash( ctx->global->history, &h );
1653 :
1654 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":{\"context\":{\"slot\":%lu},\"value\":%s},\"id\":%s}" CRLF,
1655 0 : fd_rpc_history_latest_slot( ctx->global->history ), (info ? "true" : "false"), ctx->call_id);
1656 :
1657 0 : return 0;
1658 0 : }
1659 :
1660 : // Implementation of the "minimumLedgerSlot" methods
1661 : static int
1662 0 : method_minimumLedgerSlot(struct json_values* values, fd_rpc_ctx_t * ctx) {
1663 0 : (void) values;
1664 0 : fd_rpc_global_ctx_t * glob = ctx->global;
1665 0 : fd_webserver_t * ws = &ctx->global->ws;
1666 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
1667 0 : glob->blockstore->shmem->wmk, ctx->call_id); /* FIXME archival file */
1668 0 : return 0;
1669 0 : }
1670 :
1671 : // Implementation of the "requestAirdrop" methods
1672 : static int
1673 0 : method_requestAirdrop(struct json_values* values, fd_rpc_ctx_t * ctx) {
1674 0 : (void)values;
1675 0 : (void)ctx;
1676 0 : FD_LOG_WARNING(( "requestAirdrop is not implemented" ));
1677 0 : fd_method_error(ctx, -1, "requestAirdrop is not implemented");
1678 0 : return 0;
1679 0 : }
1680 :
1681 : // Implementation of the "sendTransaction" methods
1682 : static int
1683 0 : method_sendTransaction(struct json_values* values, fd_rpc_ctx_t * ctx) {
1684 0 : fd_webserver_t * ws = &ctx->global->ws;
1685 0 : static const uint ENCPATH[4] = {
1686 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1687 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
1688 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ENCODING,
1689 0 : (JSON_TOKEN_STRING<<16)
1690 0 : };
1691 0 : ulong enc_str_sz = 0;
1692 0 : const void* enc_str = json_get_value(values, ENCPATH, 4, &enc_str_sz);
1693 0 : fd_rpc_encoding_t enc;
1694 0 : if (enc_str == NULL || MATCH_STRING(enc_str, enc_str_sz, "base58"))
1695 0 : enc = FD_ENC_BASE58;
1696 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64"))
1697 0 : enc = FD_ENC_BASE64;
1698 0 : else {
1699 0 : fd_method_error(ctx, -1, "invalid data encoding %s", (const char*)enc_str);
1700 0 : return 0;
1701 0 : }
1702 :
1703 0 : static const uint DATAPATH[3] = {
1704 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
1705 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
1706 0 : (JSON_TOKEN_STRING<<16)
1707 0 : };
1708 0 : ulong arg_sz = 0;
1709 0 : const void* arg = json_get_value(values, DATAPATH, 3, &arg_sz);
1710 0 : if (arg == NULL) {
1711 0 : fd_method_error(ctx, -1, "sendTransaction requires a string as first parameter");
1712 0 : return 0;
1713 0 : }
1714 :
1715 0 : uchar data[FD_TXN_MTU];
1716 0 : ulong data_sz = FD_TXN_MTU;
1717 0 : if( enc == FD_ENC_BASE58 ) {
1718 0 : if( b58tobin( data, &data_sz, (const char*)arg, arg_sz ) ) {
1719 0 : fd_method_error(ctx, -1, "failed to decode base58 data");
1720 0 : return 0;
1721 0 : }
1722 0 : } else {
1723 0 : FD_TEST( enc == FD_ENC_BASE64 );
1724 0 : if( FD_BASE64_DEC_SZ( arg_sz ) > FD_TXN_MTU ) {
1725 0 : fd_method_error(ctx, -1, "failed to decode base64 data");
1726 0 : return 0;
1727 0 : }
1728 0 : long res = fd_base64_decode( data, (const char*)arg, arg_sz );
1729 0 : if( res < 0 ) {
1730 0 : fd_method_error(ctx, -1, "failed to decode base64 data");
1731 0 : return 0;
1732 0 : }
1733 0 : data_sz = (ulong)res;
1734 0 : }
1735 :
1736 0 : FD_LOG_NOTICE(( "received transaction of size %lu", data_sz ));
1737 :
1738 0 : uchar txn_out[FD_TXN_MAX_SZ];
1739 0 : ulong pay_sz = 0;
1740 0 : ulong txn_sz = fd_txn_parse_core(data, data_sz, txn_out, NULL, &pay_sz);
1741 0 : if ( txn_sz == 0 || txn_sz > FD_TXN_MAX_SZ ) {
1742 0 : fd_method_error(ctx, -1, "failed to parse transaction");
1743 0 : return 0;
1744 0 : }
1745 :
1746 0 : if( sendto( ctx->global->tpu_socket, data, data_sz, 0,
1747 0 : (const struct sockaddr*)fd_type_pun_const(&ctx->global->tpu_addr), sizeof(ctx->global->tpu_addr) ) < 0 ) {
1748 0 : fd_method_error(ctx, -1, "failed to send transaction data");
1749 0 : return 0;
1750 0 : }
1751 :
1752 0 : fd_txn_t * txn = (fd_txn_t *)txn_out;
1753 0 : fd_ed25519_sig_t const * sigs = (fd_ed25519_sig_t const *)(data + txn->signature_off);
1754 0 : char buf64[FD_BASE58_ENCODED_64_SZ];
1755 0 : fd_base58_encode_64((const uchar*)sigs, NULL, buf64);
1756 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":\"%s\",\"id\":%s}" CRLF, buf64, ctx->call_id);
1757 :
1758 0 : return 0;
1759 0 : }
1760 :
1761 : // Implementation of the "simulateTransaction" methods
1762 : static int
1763 0 : method_simulateTransaction(struct json_values* values, fd_rpc_ctx_t * ctx) {
1764 0 : (void)values;
1765 0 : (void)ctx;
1766 0 : FD_LOG_WARNING(( "simulateTransaction is not implemented" ));
1767 0 : fd_method_error(ctx, -1, "simulateTransaction is not implemented");
1768 0 : return 0;
1769 0 : }
1770 :
1771 : // Top level method dispatch function
1772 : void
1773 0 : fd_webserver_method_generic(struct json_values* values, void * cb_arg) {
1774 0 : fd_rpc_ctx_t ctx = *( fd_rpc_ctx_t *)cb_arg;
1775 :
1776 0 : snprintf(ctx.call_id, sizeof(ctx.call_id)-1, "null");
1777 :
1778 0 : static const uint PATH[2] = {
1779 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_JSONRPC,
1780 0 : (JSON_TOKEN_STRING<<16)
1781 0 : };
1782 0 : ulong arg_sz = 0;
1783 0 : const void* arg = json_get_value(values, PATH, 2, &arg_sz);
1784 0 : if (arg == NULL) {
1785 0 : fd_method_error(&ctx, -1, "missing jsonrpc member");
1786 0 : return;
1787 0 : }
1788 0 : if (!MATCH_STRING(arg, arg_sz, "2.0")) {
1789 0 : fd_method_error(&ctx, -1, "jsonrpc value must be 2.0");
1790 0 : return;
1791 0 : }
1792 :
1793 0 : static const uint PATH3[2] = {
1794 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ID,
1795 0 : (JSON_TOKEN_INTEGER<<16)
1796 0 : };
1797 0 : arg_sz = 0;
1798 0 : arg = json_get_value(values, PATH3, 2, &arg_sz);
1799 0 : if (arg != NULL) {
1800 0 : snprintf(ctx.call_id, sizeof(ctx.call_id)-1, "%lu", *(ulong*)arg); /* TODO check signedness of arg */
1801 0 : } else {
1802 0 : static const uint PATH4[2] = {
1803 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ID,
1804 0 : (JSON_TOKEN_STRING<<16)
1805 0 : };
1806 0 : arg_sz = 0;
1807 0 : arg = json_get_value(values, PATH4, 2, &arg_sz);
1808 0 : if (arg != NULL) {
1809 0 : snprintf(ctx.call_id, sizeof(ctx.call_id)-1, "\"%s\"", (const char *)arg);
1810 0 : } else {
1811 0 : fd_method_error(&ctx, -1, "missing id member");
1812 0 : return;
1813 0 : }
1814 0 : }
1815 :
1816 0 : static const uint PATH2[2] = {
1817 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_METHOD,
1818 0 : (JSON_TOKEN_STRING<<16)
1819 0 : };
1820 0 : arg_sz = 0;
1821 0 : arg = json_get_value(values, PATH2, 2, &arg_sz);
1822 0 : if (arg == NULL) {
1823 0 : fd_method_error(&ctx, -1, "missing method member");
1824 0 : return;
1825 0 : }
1826 0 : long meth_id = fd_webserver_json_keyword((const char*)arg, arg_sz);
1827 :
1828 0 : switch (meth_id) {
1829 0 : case KEYW_RPCMETHOD_GETACCOUNTINFO:
1830 0 : if (!method_getAccountInfo(values, &ctx))
1831 0 : return;
1832 0 : break;
1833 0 : case KEYW_RPCMETHOD_GETBALANCE:
1834 0 : if (!method_getBalance(values, &ctx))
1835 0 : return;
1836 0 : break;
1837 0 : case KEYW_RPCMETHOD_GETBLOCK:
1838 0 : if (!method_getBlock(values, &ctx))
1839 0 : return;
1840 0 : break;
1841 0 : case KEYW_RPCMETHOD_GETBLOCKCOMMITMENT:
1842 0 : if (!method_getBlockCommitment(values, &ctx))
1843 0 : return;
1844 0 : break;
1845 0 : case KEYW_RPCMETHOD_GETBLOCKHEIGHT:
1846 0 : if (!method_getBlockHeight(values, &ctx))
1847 0 : return;
1848 0 : break;
1849 0 : case KEYW_RPCMETHOD_GETBLOCKPRODUCTION:
1850 0 : if (!method_getBlockProduction(values, &ctx))
1851 0 : return;
1852 0 : break;
1853 0 : case KEYW_RPCMETHOD_GETBLOCKS:
1854 0 : if (!method_getBlocks(values, &ctx))
1855 0 : return;
1856 0 : break;
1857 0 : case KEYW_RPCMETHOD_GETBLOCKSWITHLIMIT:
1858 0 : if (!method_getBlocksWithLimit(values, &ctx))
1859 0 : return;
1860 0 : break;
1861 0 : case KEYW_RPCMETHOD_GETBLOCKTIME:
1862 0 : if (!method_getBlockTime(values, &ctx))
1863 0 : return;
1864 0 : break;
1865 0 : case KEYW_RPCMETHOD_GETCLUSTERNODES:
1866 0 : if (!method_getClusterNodes(values, &ctx))
1867 0 : return;
1868 0 : break;
1869 0 : case KEYW_RPCMETHOD_GETEPOCHINFO:
1870 0 : if (!method_getEpochInfo(values, &ctx))
1871 0 : return;
1872 0 : break;
1873 0 : case KEYW_RPCMETHOD_GETEPOCHSCHEDULE:
1874 0 : if (!method_getEpochSchedule(values, &ctx))
1875 0 : return;
1876 0 : break;
1877 0 : case KEYW_RPCMETHOD_GETFEEFORMESSAGE:
1878 0 : if (!method_getFeeForMessage(values, &ctx))
1879 0 : return;
1880 0 : break;
1881 0 : case KEYW_RPCMETHOD_GETFIRSTAVAILABLEBLOCK:
1882 0 : if (!method_getFirstAvailableBlock(values, &ctx))
1883 0 : return;
1884 0 : break;
1885 0 : case KEYW_RPCMETHOD_GETGENESISHASH:
1886 0 : if (!method_getGenesisHash(values, &ctx))
1887 0 : return;
1888 0 : break;
1889 0 : case KEYW_RPCMETHOD_GETHEALTH:
1890 0 : if (!method_getHealth(values, &ctx))
1891 0 : return;
1892 0 : break;
1893 0 : case KEYW_RPCMETHOD_GETHIGHESTSNAPSHOTSLOT:
1894 0 : if (!method_getHighestSnapshotSlot(values, &ctx))
1895 0 : return;
1896 0 : break;
1897 0 : case KEYW_RPCMETHOD_GETIDENTITY:
1898 0 : if (!method_getIdentity(values, &ctx))
1899 0 : return;
1900 0 : break;
1901 0 : case KEYW_RPCMETHOD_GETINFLATIONGOVERNOR:
1902 0 : if (!method_getInflationGovernor(values, &ctx))
1903 0 : return;
1904 0 : break;
1905 0 : case KEYW_RPCMETHOD_GETINFLATIONRATE:
1906 0 : if (!method_getInflationRate(values, &ctx))
1907 0 : return;
1908 0 : break;
1909 0 : case KEYW_RPCMETHOD_GETINFLATIONREWARD:
1910 0 : if (!method_getInflationReward(values, &ctx))
1911 0 : return;
1912 0 : break;
1913 0 : case KEYW_RPCMETHOD_GETLARGESTACCOUNTS:
1914 0 : if (!method_getLargestAccounts(values, &ctx))
1915 0 : return;
1916 0 : break;
1917 0 : case KEYW_RPCMETHOD_GETLATESTBLOCKHASH:
1918 0 : if (!method_getLatestBlockhash(values, &ctx))
1919 0 : return;
1920 0 : break;
1921 0 : case KEYW_RPCMETHOD_GETLEADERSCHEDULE:
1922 0 : if (!method_getLeaderSchedule(values, &ctx))
1923 0 : return;
1924 0 : break;
1925 0 : case KEYW_RPCMETHOD_GETMAXRETRANSMITSLOT:
1926 0 : if (!method_getMaxRetransmitSlot(values, &ctx))
1927 0 : return;
1928 0 : break;
1929 0 : case KEYW_RPCMETHOD_GETMAXSHREDINSERTSLOT:
1930 0 : if (!method_getMaxShredInsertSlot(values, &ctx))
1931 0 : return;
1932 0 : break;
1933 0 : case KEYW_RPCMETHOD_GETMINIMUMBALANCEFORRENTEXEMPTION:
1934 0 : if (!method_getMinimumBalanceForRentExemption(values, &ctx))
1935 0 : return;
1936 0 : break;
1937 0 : case KEYW_RPCMETHOD_GETMULTIPLEACCOUNTS:
1938 0 : if (!method_getMultipleAccounts(values, &ctx))
1939 0 : return;
1940 0 : break;
1941 0 : case KEYW_RPCMETHOD_GETPROGRAMACCOUNTS:
1942 0 : if (!method_getProgramAccounts(values, &ctx))
1943 0 : return;
1944 0 : break;
1945 0 : case KEYW_RPCMETHOD_GETRECENTPERFORMANCESAMPLES:
1946 0 : if (!method_getRecentPerformanceSamples(values, &ctx))
1947 0 : return;
1948 0 : break;
1949 0 : case KEYW_RPCMETHOD_GETRECENTPRIORITIZATIONFEES:
1950 0 : if (!method_getRecentPrioritizationFees(values, &ctx))
1951 0 : return;
1952 0 : break;
1953 0 : case KEYW_RPCMETHOD_GETSIGNATURESFORADDRESS:
1954 0 : if (!method_getSignaturesForAddress(values, &ctx))
1955 0 : return;
1956 0 : break;
1957 0 : case KEYW_RPCMETHOD_GETSIGNATURESTATUSES:
1958 0 : if (!method_getSignatureStatuses(values, &ctx))
1959 0 : return;
1960 0 : break;
1961 0 : case KEYW_RPCMETHOD_GETSLOT:
1962 0 : if (!method_getSlot(values, &ctx))
1963 0 : return;
1964 0 : break;
1965 0 : case KEYW_RPCMETHOD_GETSLOTLEADER:
1966 0 : if (!method_getSlotLeader(values, &ctx))
1967 0 : return;
1968 0 : break;
1969 0 : case KEYW_RPCMETHOD_GETSLOTLEADERS:
1970 0 : if (!method_getSlotLeaders(values, &ctx))
1971 0 : return;
1972 0 : break;
1973 0 : case KEYW_RPCMETHOD_GETSTAKEACTIVATION:
1974 0 : if (!method_getStakeActivation(values, &ctx))
1975 0 : return;
1976 0 : break;
1977 0 : case KEYW_RPCMETHOD_GETSTAKEMINIMUMDELEGATION:
1978 0 : if (!method_getStakeMinimumDelegation(values, &ctx))
1979 0 : return;
1980 0 : break;
1981 0 : case KEYW_RPCMETHOD_GETSUPPLY:
1982 0 : if (!method_getSupply(values, &ctx))
1983 0 : return;
1984 0 : break;
1985 0 : case KEYW_RPCMETHOD_GETTOKENACCOUNTBALANCE:
1986 0 : if (!method_getTokenAccountBalance(values, &ctx))
1987 0 : return;
1988 0 : break;
1989 0 : case KEYW_RPCMETHOD_GETTOKENACCOUNTSBYDELEGATE:
1990 0 : if (!method_getTokenAccountsByDelegate(values, &ctx))
1991 0 : return;
1992 0 : break;
1993 0 : case KEYW_RPCMETHOD_GETTOKENACCOUNTSBYOWNER:
1994 0 : if (!method_getTokenAccountsByOwner(values, &ctx))
1995 0 : return;
1996 0 : break;
1997 0 : case KEYW_RPCMETHOD_GETTOKENLARGESTACCOUNTS:
1998 0 : if (!method_getTokenLargestAccounts(values, &ctx))
1999 0 : return;
2000 0 : break;
2001 0 : case KEYW_RPCMETHOD_GETTOKENSUPPLY:
2002 0 : if (!method_getTokenSupply(values, &ctx))
2003 0 : return;
2004 0 : break;
2005 0 : case KEYW_RPCMETHOD_GETTRANSACTION:
2006 0 : if (!method_getTransaction(values, &ctx))
2007 0 : return;
2008 0 : break;
2009 0 : case KEYW_RPCMETHOD_GETTRANSACTIONCOUNT:
2010 0 : if (!method_getTransactionCount(values, &ctx))
2011 0 : return;
2012 0 : break;
2013 0 : case KEYW_RPCMETHOD_GETVERSION:
2014 0 : if (!method_getVersion(values, &ctx))
2015 0 : return;
2016 0 : break;
2017 0 : case KEYW_RPCMETHOD_GETVOTEACCOUNTS:
2018 0 : if (!method_getVoteAccounts(values, &ctx))
2019 0 : return;
2020 0 : break;
2021 0 : case KEYW_RPCMETHOD_ISBLOCKHASHVALID:
2022 0 : if (!method_isBlockhashValid(values, &ctx))
2023 0 : return;
2024 0 : break;
2025 0 : case KEYW_RPCMETHOD_MINIMUMLEDGERSLOT:
2026 0 : if (!method_minimumLedgerSlot(values, &ctx))
2027 0 : return;
2028 0 : break;
2029 0 : case KEYW_RPCMETHOD_REQUESTAIRDROP:
2030 0 : if (!method_requestAirdrop(values, &ctx))
2031 0 : return;
2032 0 : break;
2033 0 : case KEYW_RPCMETHOD_SENDTRANSACTION:
2034 0 : if (!method_sendTransaction(values, &ctx))
2035 0 : return;
2036 0 : break;
2037 0 : case KEYW_RPCMETHOD_SIMULATETRANSACTION:
2038 0 : if (!method_simulateTransaction(values, &ctx))
2039 0 : return;
2040 0 : break;
2041 0 : default:
2042 0 : fd_method_error(&ctx, -1, "unknown or unimplemented method %s", (const char*)arg);
2043 0 : return;
2044 0 : }
2045 0 : }
2046 :
2047 : static int
2048 0 : ws_method_accountSubscribe(ulong conn_id, struct json_values * values, fd_rpc_ctx_t * ctx) {
2049 0 : fd_webserver_t * ws = &ctx->global->ws;
2050 :
2051 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
2052 : // Path to argument
2053 0 : static const uint PATH[3] = {
2054 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
2055 0 : (JSON_TOKEN_LBRACKET<<16) | 0,
2056 0 : (JSON_TOKEN_STRING<<16)
2057 0 : };
2058 0 : ulong arg_sz = 0;
2059 0 : const void* arg = json_get_value(values, PATH, 3, &arg_sz);
2060 0 : if (arg == NULL) {
2061 0 : fd_method_simple_error( ctx, -1, "getAccountInfo requires a string as first parameter" );
2062 0 : return 0;
2063 0 : }
2064 0 : fd_pubkey_t acct;
2065 0 : if( fd_base58_decode_32((const char *)arg, acct.uc) == NULL ) {
2066 0 : fd_method_simple_error(ctx, -1, "invalid base58 encoding");
2067 0 : return 0;
2068 0 : }
2069 :
2070 0 : static const uint PATH2[4] = {
2071 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
2072 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
2073 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ENCODING,
2074 0 : (JSON_TOKEN_STRING<<16)
2075 0 : };
2076 0 : ulong enc_str_sz = 0;
2077 0 : const void* enc_str = json_get_value(values, PATH2, 4, &enc_str_sz);
2078 0 : fd_rpc_encoding_t enc;
2079 0 : if (enc_str == NULL || MATCH_STRING(enc_str, enc_str_sz, "base58"))
2080 0 : enc = FD_ENC_BASE58;
2081 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64"))
2082 0 : enc = FD_ENC_BASE64;
2083 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "base64+zstd"))
2084 0 : enc = FD_ENC_BASE64_ZSTD;
2085 0 : else if (MATCH_STRING(enc_str, enc_str_sz, "jsonParsed"))
2086 0 : enc = FD_ENC_JSON;
2087 0 : else {
2088 0 : fd_method_error(ctx, -1, "invalid data encoding %s", (const char*)enc_str);
2089 0 : return 0;
2090 0 : }
2091 :
2092 0 : static const uint PATH3[5] = {
2093 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
2094 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
2095 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_DATASLICE,
2096 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_LENGTH,
2097 0 : (JSON_TOKEN_INTEGER<<16)
2098 0 : };
2099 0 : static const uint PATH4[5] = {
2100 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_PARAMS,
2101 0 : (JSON_TOKEN_LBRACKET<<16) | 1,
2102 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_DATASLICE,
2103 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_OFFSET,
2104 0 : (JSON_TOKEN_INTEGER<<16)
2105 0 : };
2106 0 : ulong len_sz = 0;
2107 0 : const void* len_ptr = json_get_value(values, PATH3, 5, &len_sz);
2108 0 : ulong off_sz = 0;
2109 0 : const void* off_ptr = json_get_value(values, PATH4, 5, &off_sz);
2110 0 : if (len_ptr && off_ptr) {
2111 0 : if (enc == FD_ENC_JSON) {
2112 0 : fd_method_simple_error(ctx, -1, "cannot use jsonParsed encoding with slice");
2113 0 : return 0;
2114 0 : }
2115 0 : }
2116 :
2117 0 : fd_rpc_global_ctx_t * subs = ctx->global;
2118 0 : if( subs->sub_cnt >= FD_WS_MAX_SUBS ) {
2119 0 : fd_method_simple_error(ctx, -1, "too many subscriptions");
2120 0 : return 0;
2121 0 : }
2122 0 : struct fd_ws_subscription * sub = &subs->sub_list[ subs->sub_cnt++ ];
2123 0 : sub->conn_id = conn_id;
2124 0 : sub->meth_id = KEYW_WS_METHOD_ACCOUNTSUBSCRIBE;
2125 0 : strncpy(sub->call_id, ctx->call_id, sizeof(sub->call_id));
2126 0 : ulong subid = sub->subsc_id = ++(subs->last_subsc_id);
2127 0 : sub->acct_subscribe.acct = acct;
2128 0 : sub->acct_subscribe.enc = enc;
2129 0 : sub->acct_subscribe.off = (off_ptr ? *(long*)off_ptr : FD_LONG_UNSET);
2130 0 : sub->acct_subscribe.len = (len_ptr ? *(long*)len_ptr : FD_LONG_UNSET);
2131 :
2132 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
2133 0 : subid, sub->call_id);
2134 :
2135 0 : } FD_SPAD_FRAME_END;
2136 :
2137 0 : return 1;
2138 0 : }
2139 :
2140 : static int
2141 0 : ws_method_accountSubscribe_update(fd_rpc_ctx_t * ctx, fd_replay_notif_msg_t * msg, struct fd_ws_subscription * sub) {
2142 0 : fd_webserver_t * ws = &ctx->global->ws;
2143 0 : fd_web_reply_new( ws );
2144 :
2145 0 : FD_SPAD_FRAME_BEGIN( ctx->global->spad ) {
2146 0 : ulong val_sz;
2147 0 : fd_funk_rec_key_t recid = fd_funk_acc_key(&sub->acct_subscribe.acct);
2148 0 : fd_funk_txn_xid_t xid;
2149 0 : xid.ul[0] = xid.ul[1] = msg->slot_exec.slot;
2150 0 : const void * val = read_account_with_xid(ctx, &recid, &xid, &val_sz);
2151 0 : if (val == NULL) {
2152 : /* Account not in tranaction */
2153 0 : return 0;
2154 0 : }
2155 :
2156 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"method\":\"accountNotification\",\"params\":{\"result\":{\"context\":{\"apiVersion\":\"" FIREDANCER_VERSION "\",\"slot\":%lu},\"value\":",
2157 0 : msg->slot_exec.slot);
2158 0 : const char * err = fd_account_to_json( ws, sub->acct_subscribe.acct, sub->acct_subscribe.enc, val, val_sz, sub->acct_subscribe.off, sub->acct_subscribe.len, ctx->global->spad );
2159 0 : if( err ) {
2160 0 : FD_LOG_WARNING(( "error converting account to json: %s", err ));
2161 0 : return 0;
2162 0 : }
2163 0 : fd_web_reply_sprintf(ws, "},\"subscription\":%lu}}" CRLF, sub->subsc_id);
2164 0 : } FD_SPAD_FRAME_END;
2165 :
2166 0 : return 1;
2167 0 : }
2168 :
2169 : static int
2170 0 : ws_method_slotSubscribe(ulong conn_id, struct json_values * values, fd_rpc_ctx_t * ctx) {
2171 0 : (void)values;
2172 0 : fd_webserver_t * ws = &ctx->global->ws;
2173 :
2174 0 : fd_rpc_global_ctx_t * subs = ctx->global;
2175 0 : if( subs->sub_cnt >= FD_WS_MAX_SUBS ) {
2176 0 : fd_method_simple_error(ctx, -1, "too many subscriptions");
2177 0 : return 0;
2178 0 : }
2179 0 : struct fd_ws_subscription * sub = &subs->sub_list[ subs->sub_cnt++ ];
2180 0 : sub->conn_id = conn_id;
2181 0 : sub->meth_id = KEYW_WS_METHOD_SLOTSUBSCRIBE;
2182 0 : strncpy(sub->call_id, ctx->call_id, sizeof(sub->call_id));
2183 0 : ulong subid = sub->subsc_id = ++(subs->last_subsc_id);
2184 :
2185 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"result\":%lu,\"id\":%s}" CRLF,
2186 0 : subid, sub->call_id);
2187 :
2188 0 : return 1;
2189 0 : }
2190 :
2191 : static int
2192 0 : ws_method_slotSubscribe_update(fd_rpc_ctx_t * ctx, fd_replay_notif_msg_t * msg, struct fd_ws_subscription * sub) {
2193 0 : fd_webserver_t * ws = &ctx->global->ws;
2194 0 : fd_web_reply_new( ws );
2195 :
2196 0 : char bank_hash[50];
2197 0 : fd_base58_encode_32(msg->slot_exec.bank_hash.uc, 0, bank_hash);
2198 0 : fd_web_reply_sprintf(ws, "{\"jsonrpc\":\"2.0\",\"method\":\"slotNotification\",\"params\":{\"result\":{\"parent\":%lu,\"root\":%lu,\"slot\":%lu,\"bank_hash\":\"%s\"},\"subscription\":%lu}}" CRLF,
2199 0 : msg->slot_exec.parent, msg->slot_exec.root, msg->slot_exec.slot,
2200 0 : bank_hash, sub->subsc_id);
2201 0 : return 1;
2202 0 : }
2203 :
2204 : int
2205 0 : fd_webserver_ws_subscribe(struct json_values* values, ulong conn_id, void * cb_arg) {
2206 0 : fd_rpc_ctx_t ctx = *( fd_rpc_ctx_t *)cb_arg;
2207 0 : fd_webserver_t * ws = &ctx.global->ws;
2208 :
2209 0 : static const uint PATH[2] = {
2210 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_JSONRPC,
2211 0 : (JSON_TOKEN_STRING<<16)
2212 0 : };
2213 0 : ulong arg_sz = 0;
2214 0 : const void* arg = json_get_value(values, PATH, 2, &arg_sz);
2215 0 : if (arg == NULL) {
2216 0 : fd_web_reply_error( ws, -1, "missing jsonrpc member", "null" );
2217 0 : return 0;
2218 0 : }
2219 0 : if (!MATCH_STRING(arg, arg_sz, "2.0")) {
2220 0 : fd_web_reply_error( ws, -1, "jsonrpc value must be 2.0", "null" );
2221 0 : return 0;
2222 0 : }
2223 :
2224 0 : static const uint PATH3[2] = {
2225 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ID,
2226 0 : (JSON_TOKEN_INTEGER<<16)
2227 0 : };
2228 0 : arg_sz = 0;
2229 0 : arg = json_get_value(values, PATH3, 2, &arg_sz);
2230 0 : if (arg != NULL) {
2231 0 : snprintf(ctx.call_id, sizeof(ctx.call_id)-1, "%lu", *(ulong*)arg); /* TODO: check signedness of arg */
2232 0 : } else {
2233 0 : static const uint PATH4[2] = {
2234 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_ID,
2235 0 : (JSON_TOKEN_STRING<<16)
2236 0 : };
2237 0 : arg_sz = 0;
2238 0 : arg = json_get_value(values, PATH4, 2, &arg_sz);
2239 0 : if (arg != NULL) {
2240 0 : snprintf(ctx.call_id, sizeof(ctx.call_id)-1, "\"%s\"", (const char *)arg);
2241 0 : } else {
2242 0 : fd_web_reply_error( ws, -1, "missing id member", "null" );
2243 0 : return 0;
2244 0 : }
2245 0 : }
2246 :
2247 0 : static const uint PATH2[2] = {
2248 0 : (JSON_TOKEN_LBRACE<<16) | KEYW_JSON_METHOD,
2249 0 : (JSON_TOKEN_STRING<<16)
2250 0 : };
2251 0 : arg_sz = 0;
2252 0 : arg = json_get_value(values, PATH2, 2, &arg_sz);
2253 0 : if (arg == NULL) {
2254 0 : fd_web_reply_error( ws, -1, "missing method member", ctx.call_id );
2255 0 : return 0;
2256 0 : }
2257 0 : long meth_id = fd_webserver_json_keyword((const char*)arg, arg_sz);
2258 :
2259 0 : switch (meth_id) {
2260 0 : case KEYW_WS_METHOD_ACCOUNTSUBSCRIBE:
2261 0 : if (ws_method_accountSubscribe(conn_id, values, &ctx)) {
2262 0 : return 1;
2263 0 : }
2264 0 : return 0;
2265 0 : case KEYW_WS_METHOD_SLOTSUBSCRIBE:
2266 0 : if (ws_method_slotSubscribe(conn_id, values, &ctx)) {
2267 0 : return 1;
2268 0 : }
2269 0 : return 0;
2270 0 : }
2271 :
2272 0 : char text[4096];
2273 0 : snprintf( text, sizeof(text), "unknown websocket method: %s", (const char*)arg );
2274 0 : fd_web_reply_error( ws, -1, text, ctx.call_id );
2275 0 : return 0;
2276 0 : }
2277 :
2278 : void
2279 0 : fd_rpc_create_ctx(fd_rpcserver_args_t * args, fd_rpc_ctx_t ** ctx_p) {
2280 0 : fd_valloc_t valloc = fd_spad_virtual( args->spad );
2281 0 : fd_rpc_ctx_t * ctx = (fd_rpc_ctx_t *)fd_valloc_malloc(valloc, alignof(fd_rpc_ctx_t), sizeof(fd_rpc_ctx_t));
2282 0 : fd_rpc_global_ctx_t * gctx = (fd_rpc_global_ctx_t *)fd_valloc_malloc(valloc, alignof(fd_rpc_global_ctx_t), sizeof(fd_rpc_global_ctx_t));
2283 0 : fd_memset(ctx, 0, sizeof(fd_rpc_ctx_t));
2284 0 : fd_memset(gctx, 0, sizeof(fd_rpc_global_ctx_t));
2285 :
2286 0 : ctx->global = gctx;
2287 0 : gctx->spad = args->spad;
2288 0 : gctx->leaders = args->leaders;
2289 :
2290 0 : if( !args->offline ) {
2291 0 : gctx->tpu_socket = socket(AF_INET, SOCK_DGRAM, 0);
2292 0 : if( gctx->tpu_socket == -1 ) {
2293 0 : FD_LOG_ERR(( "socket failed (%i-%s)", errno, strerror( errno ) ));
2294 0 : }
2295 0 : struct sockaddr_in addrLocal;
2296 0 : memset( &addrLocal, 0, sizeof(addrLocal) );
2297 0 : addrLocal.sin_family = AF_INET;
2298 0 : if( bind(gctx->tpu_socket, (const struct sockaddr*)fd_type_pun_const(&addrLocal), sizeof(addrLocal)) == -1 ) {
2299 0 : FD_LOG_ERR(( "bind failed (%i-%s)", errno, strerror( errno ) ));
2300 0 : }
2301 0 : gctx->tpu_addr = args->tpu_addr;
2302 :
2303 0 : } else {
2304 0 : gctx->tpu_socket = -1;
2305 0 : }
2306 :
2307 0 : void * mem = fd_valloc_malloc( valloc, fd_perf_sample_deque_align(), fd_perf_sample_deque_footprint() );
2308 0 : gctx->perf_samples = fd_perf_sample_deque_join( fd_perf_sample_deque_new( mem ) );
2309 0 : FD_TEST( gctx->perf_samples );
2310 :
2311 0 : gctx->history = fd_rpc_history_create(args);
2312 :
2313 0 : FD_LOG_NOTICE(( "starting web server on port %u", (uint)args->port ));
2314 0 : if (fd_webserver_start(args->port, args->params, gctx->spad, &gctx->ws, ctx))
2315 0 : FD_LOG_ERR(("fd_webserver_start failed"));
2316 :
2317 0 : *ctx_p = ctx;
2318 0 : }
2319 :
2320 : void
2321 0 : fd_rpc_start_service(fd_rpcserver_args_t * args, fd_rpc_ctx_t * ctx) {
2322 0 : fd_rpc_global_ctx_t * gctx = ctx->global;
2323 :
2324 0 : gctx->funk = args->funk;
2325 0 : memcpy( gctx->blockstore, args->blockstore, sizeof(fd_blockstore_t) );
2326 0 : gctx->blockstore_fd = args->blockstore_fd;
2327 0 : }
2328 :
2329 : int
2330 0 : fd_rpc_ws_poll(fd_rpc_ctx_t * ctx) {
2331 0 : return fd_webserver_poll(&ctx->global->ws);
2332 0 : }
2333 :
2334 : int
2335 0 : fd_rpc_ws_fd(fd_rpc_ctx_t * ctx) {
2336 0 : return fd_webserver_fd(&ctx->global->ws);
2337 0 : }
2338 :
2339 : void
2340 0 : fd_webserver_ws_closed(ulong conn_id, void * cb_arg) {
2341 0 : fd_rpc_ctx_t * ctx = ( fd_rpc_ctx_t *)cb_arg;
2342 0 : fd_rpc_global_ctx_t * subs = ctx->global;
2343 0 : for( ulong i = 0; i < subs->sub_cnt; ++i ) {
2344 0 : if( subs->sub_list[i].conn_id == conn_id ) {
2345 0 : subs->sub_list[i] = subs->sub_list[--(subs->sub_cnt)];
2346 0 : --i;
2347 0 : }
2348 0 : }
2349 0 : }
2350 :
2351 : void
2352 0 : fd_rpc_replay_during_frag( fd_rpc_ctx_t * ctx, fd_replay_notif_msg_t * state, void const * msg, int sz ) {
2353 0 : (void)ctx;
2354 0 : FD_TEST( sz == (int)sizeof(fd_replay_notif_msg_t) );
2355 0 : fd_memcpy(state, msg, sizeof(fd_replay_notif_msg_t));
2356 0 : }
2357 :
2358 : void
2359 0 : fd_rpc_replay_after_frag(fd_rpc_ctx_t * ctx, fd_replay_notif_msg_t * msg) {
2360 0 : fd_rpc_global_ctx_t * subs = ctx->global;
2361 :
2362 0 : if( msg->type == FD_REPLAY_SLOT_TYPE ) {
2363 0 : long ts = fd_log_wallclock() / (long)1e9;
2364 0 : if( FD_UNLIKELY( ts - subs->perf_sample_ts >= 60 ) ) {
2365 :
2366 0 : if( FD_UNLIKELY( fd_perf_sample_deque_full( subs->perf_samples ) ) ) {
2367 0 : fd_perf_sample_deque_pop_head( subs->perf_samples );
2368 0 : }
2369 :
2370 : /* Record a new perf sample */
2371 :
2372 0 : if( FD_LIKELY( subs->perf_sample_snapshot.highest_slot ) ) {
2373 0 : fd_perf_sample_t perf_sample = { .num_slots = msg->slot_exec.slot - subs->perf_sample_snapshot.highest_slot,
2374 0 : .num_transactions = msg->slot_exec.transaction_count -
2375 0 : subs->perf_sample_snapshot.num_transactions,
2376 0 : .num_non_vote_transactions = 0,
2377 0 : .highest_slot = msg->slot_exec.slot };
2378 0 : fd_perf_sample_deque_push_tail( subs->perf_samples, perf_sample );
2379 0 : }
2380 :
2381 : /* Update the snapshot of perf sample to record a diff on next interval. */
2382 :
2383 0 : subs->perf_sample_snapshot = ( fd_perf_sample_t ){
2384 0 : .num_slots = 0,
2385 0 : .num_transactions = msg->slot_exec.transaction_count,
2386 0 : .num_non_vote_transactions = 0,
2387 0 : .highest_slot = msg->slot_exec.slot };
2388 :
2389 : /* Update the timestamp for checking interval. */
2390 :
2391 0 : subs->perf_sample_ts = ts;
2392 0 : }
2393 :
2394 0 : if( msg->slot_exec.shred_cnt == 0 ) return;
2395 :
2396 0 : fd_rpc_history_save( subs->history, subs->blockstore, msg );
2397 :
2398 0 : for( ulong j = 0; j < subs->sub_cnt; ++j ) {
2399 0 : struct fd_ws_subscription * sub = &subs->sub_list[ j ];
2400 0 : if( sub->meth_id == KEYW_WS_METHOD_SLOTSUBSCRIBE ) {
2401 0 : if( ws_method_slotSubscribe_update( ctx, msg, sub ) )
2402 0 : fd_web_ws_send( &subs->ws, sub->conn_id );
2403 0 : }
2404 0 : if( sub->meth_id == KEYW_WS_METHOD_ACCOUNTSUBSCRIBE ) {
2405 0 : if( ws_method_accountSubscribe_update( ctx, msg, sub ) )
2406 0 : fd_web_ws_send( &subs->ws, sub->conn_id );
2407 0 : }
2408 0 : }
2409 0 : }
2410 0 : }
2411 :
2412 : void
2413 0 : fd_rpc_stake_during_frag( fd_rpc_ctx_t * ctx, fd_multi_epoch_leaders_t * state, void const * msg, int sz ) {
2414 0 : (void)ctx; (void)sz;
2415 0 : fd_multi_epoch_leaders_stake_msg_init( state, msg );
2416 0 : }
2417 :
2418 : void
2419 0 : fd_rpc_stake_after_frag(fd_rpc_ctx_t * ctx, fd_multi_epoch_leaders_t * state) {
2420 0 : (void)ctx;
2421 0 : fd_multi_epoch_leaders_stake_msg_fini( state );
2422 0 : }
|