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