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