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