LCOV - code coverage report
Current view: top level - discof/rpcserver - fd_rpc_service.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 2069 0.0 %
Date: 2025-03-20 12:08:36 Functions: 0 83 0.0 %

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

Generated by: LCOV version 1.14