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