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