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