LCOV - code coverage report
Current view: top level - app/rpcserver - main.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 133 0.0 %
Date: 2025-10-13 04:42:14 Functions: 0 9 0.0 %

          Line data    Source code
       1             : #define _DEFAULT_SOURCE
       2             : 
       3             : #include <fcntl.h>
       4             : #include <stdio.h>
       5             : #include <stdlib.h>
       6             : #include <signal.h>
       7             : #include <errno.h>
       8             : #include <unistd.h>
       9             : #include <netdb.h>
      10             : #include <sys/socket.h>
      11             : #include <netinet/in.h>
      12             : #include <arpa/inet.h>
      13             : #include "../../discof/rpcserver/fd_rpc_service.h"
      14             : #include "../../funk/fd_funk.h"
      15             : #include "../../util/shmem/fd_shmem_private.h"
      16             : #include "../../disco/keyguard/fd_keyload.h"
      17             : 
      18             : #define SHAM_LINK_CONTEXT fd_rpc_ctx_t
      19             : #define SHAM_LINK_NAME    replay_sham_link
      20             : #include "sham_link.h"
      21             : 
      22             : #define SHAM_LINK_CONTEXT fd_rpc_ctx_t
      23             : #define SHAM_LINK_NAME    stake_sham_link
      24             : #include "sham_link.h"
      25             : 
      26             : #define SHAM_LINK_CONTEXT fd_rpc_ctx_t
      27             : #define SHAM_LINK_NAME    repair_sham_link
      28             : #include "sham_link.h"
      29             : 
      30             : static void
      31           0 : init_args( int * argc, char *** argv, fd_rpcserver_args_t * args ) {
      32           0 :   memset( args, 0, sizeof(fd_rpcserver_args_t) );
      33             : 
      34           0 :   const char * mount_path = fd_env_strip_cmdline_cstr( argc, argv, "--mount-path", NULL, "/mnt/.fd" );
      35           0 :   if( FD_UNLIKELY( mount_path )) {
      36           0 :     strncpy( fd_shmem_private_base, mount_path, sizeof(fd_shmem_private_base)-1 );
      37           0 :     fd_shmem_private_base_len = strlen( mount_path );
      38           0 :   }
      39             : 
      40           0 :   const char * funk_wksp_name = fd_env_strip_cmdline_cstr( argc, argv, "--funk-wksp-name", NULL, "fd1_funk.wksp" );
      41           0 :   if( FD_UNLIKELY( !funk_wksp_name ))
      42           0 :     FD_LOG_ERR(( "--funk-wksp-name argument is required" ));
      43           0 :   FD_LOG_NOTICE(( "attaching to workspace \"%s\"", funk_wksp_name ));
      44           0 :   fd_wksp_t * funk_wksp = fd_wksp_attach( funk_wksp_name );
      45           0 :   if( FD_UNLIKELY( !funk_wksp ))
      46           0 :     FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", funk_wksp_name ));
      47           0 :   fd_wksp_tag_query_info_t info;
      48           0 :   ulong tag = 1;
      49           0 :   if( fd_wksp_tag_query( funk_wksp, &tag, 1, &info, 1 ) <= 0 ) {
      50           0 :     FD_LOG_ERR(( "workspace does not contain a funk" ));
      51           0 :   }
      52           0 :   void * funk_shmem = fd_wksp_laddr_fast( funk_wksp, info.gaddr_lo );
      53           0 :   fd_funk_t * funk = fd_funk_join( args->funk, funk_shmem );
      54           0 :   if( FD_UNLIKELY( !funk ))
      55           0 :     FD_LOG_ERR(( "failed to join funk" ));
      56             : 
      57           0 :   const char * store_wksp_name = fd_env_strip_cmdline_cstr( argc, argv, "--store-wksp-name", NULL, "fd1_store.wksp" );
      58           0 :   if( FD_UNLIKELY( !store_wksp_name ))
      59           0 :     FD_LOG_ERR(( "--store-wksp-name argument is required" ));
      60           0 :   FD_LOG_NOTICE(( "attaching to workspace \"%s\"", store_wksp_name ));
      61           0 :   fd_wksp_t * store_wksp = fd_wksp_attach( store_wksp_name );
      62           0 :   if( FD_UNLIKELY( !store_wksp ))
      63           0 :     FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", store_wksp_name ));
      64           0 :   if( fd_wksp_tag_query( store_wksp, &tag, 1, &info, 1 ) <= 0 ) {
      65           0 :     FD_LOG_ERR(( "workspace does not contain a store" ));
      66           0 :   }
      67           0 :   void * store_shmem = fd_wksp_laddr_fast( store_wksp, info.gaddr_lo );
      68           0 :   fd_store_t * store = fd_store_join( store_shmem );
      69           0 :   if( FD_UNLIKELY( !store ))
      70           0 :     FD_LOG_ERR(( "failed to join store" ));
      71           0 :   args->store = store;
      72             : 
      73           0 :   const char * identity_key_path = fd_env_strip_cmdline_cstr( argc, argv, "--identity-key-path", NULL, "/home/fire/.firedancer/fd1/identity.json" );
      74           0 :   uchar const * identity_key = fd_keyload_load( identity_key_path, /* pubkey only: */ 1 );
      75           0 :   if( FD_UNLIKELY( !identity_key ))
      76           0 :     FD_LOG_ERR(( "failed to load identity key from %s", identity_key_path ));
      77           0 :   memcpy( args->identity_key.uc, identity_key, sizeof(args->identity_key) );
      78             : 
      79           0 :   args->port    = (ushort)fd_env_strip_cmdline_ulong( argc, argv, "--port", NULL, 8899 );
      80             : 
      81           0 :   args->params.max_connection_cnt =    fd_env_strip_cmdline_ulong( argc, argv, "--max-connection-cnt",    NULL, 10 );
      82           0 :   args->params.max_ws_connection_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-connection-cnt", NULL, 10 );
      83           0 :   args->params.max_request_len =       fd_env_strip_cmdline_ulong( argc, argv, "--max-request-len",       NULL, 1<<16 );
      84           0 :   args->params.max_ws_recv_frame_len = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-recv-frame-len", NULL, 1<<16 );
      85           0 :   args->params.max_ws_send_frame_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-send-frame-cnt", NULL, 100 );
      86           0 :   args->params.outgoing_buffer_sz    = fd_env_strip_cmdline_ulong( argc, argv, "--max-send-buf",          NULL, 100U<<20U );
      87           0 :   args->block_index_max              = fd_env_strip_cmdline_uint ( argc, argv, "--max-block_idx",         NULL, 65536 );
      88           0 :   args->txn_index_max                = fd_env_strip_cmdline_uint ( argc, argv, "--max-txn-idx",           NULL, 1U<<21U );
      89           0 :   args->acct_index_max               = fd_env_strip_cmdline_uint ( argc, argv, "--max-acct-idx",          NULL, 1U<<21U );
      90           0 :   strncpy(args->history_file,          fd_env_strip_cmdline_cstr ( argc, argv, "--rpc-history-file",      NULL, "rpc_history" ), sizeof(args->history_file)-1 );
      91             : 
      92           0 :   const char * tpu_host = fd_env_strip_cmdline_cstr ( argc, argv, "--local-tpu-host", NULL, "127.0.0.1" );
      93           0 :   ulong tpu_port = fd_env_strip_cmdline_ulong( argc, argv, "--local-tpu-port", NULL, 9001U );
      94           0 :   memset( &args->tpu_addr, 0, sizeof(args->tpu_addr) );
      95           0 :   args->tpu_addr.sin_family = AF_INET;
      96           0 :   if( !inet_aton( tpu_host, &args->tpu_addr.sin_addr ) ) {
      97           0 :     struct hostent * hent = gethostbyname( tpu_host );
      98           0 :     if( hent == NULL ) {
      99           0 :       FD_LOG_WARNING(( "unable to resolve tpu host %s", tpu_host ));
     100           0 :       exit(-1);
     101           0 :     }
     102           0 :     args->tpu_addr.sin_addr.s_addr = ( (struct in_addr *)hent->h_addr_list[0] )->s_addr;
     103           0 :   }
     104           0 :   if( tpu_port < 1024 || tpu_port > (int)USHORT_MAX ) {
     105           0 :     FD_LOG_ERR(( "invalid tpu port number" ));
     106           0 :     exit(-1);
     107           0 :   }
     108           0 :   args->tpu_addr.sin_port = htons( (ushort)tpu_port );
     109           0 :   FD_LOG_NOTICE(( "using tpu %s:%u", inet_ntoa( args->tpu_addr.sin_addr ), (uint)ntohs( args->tpu_addr.sin_port ) ));
     110           0 : }
     111             : 
     112             : static void
     113           0 : init_args_offline( int * argc, char *** argv, fd_rpcserver_args_t * args ) {
     114           0 :   memset( args, 0, sizeof(fd_rpcserver_args_t) );
     115           0 :   args->offline = 1;
     116             : 
     117           0 :   const char * funk_wksp_name = fd_env_strip_cmdline_cstr( argc, argv, "--funk-wksp-name", NULL, "fd1_funk.wksp" );
     118           0 :   if( FD_UNLIKELY( !funk_wksp_name ))
     119           0 :     FD_LOG_ERR(( "--funk-wksp-name argument is required" ));
     120           0 :   fd_wksp_t * funk_wksp = fd_wksp_attach( funk_wksp_name );
     121           0 :   if( FD_UNLIKELY( !funk_wksp ))
     122           0 :     FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", funk_wksp_name ));
     123           0 :   fd_wksp_tag_query_info_t info;
     124           0 :   ulong tag = 1;
     125           0 :   if( fd_wksp_tag_query( funk_wksp, &tag, 1, &info, 1 ) <= 0 ) {
     126           0 :     FD_LOG_ERR(( "workspace does not contain a funk" ));
     127           0 :   }
     128           0 :   void * funk_shmem = fd_wksp_laddr_fast( funk_wksp, info.gaddr_lo );
     129           0 :   fd_funk_t * funk = fd_funk_join( args->funk, funk_shmem );
     130           0 :   if( FD_UNLIKELY( !funk ))
     131           0 :     FD_LOG_ERR(( "failed to join funk" ));
     132             : 
     133           0 :   args->port = (ushort)fd_env_strip_cmdline_ulong( argc, argv, "--port", NULL, 8899 );
     134             : 
     135           0 :   args->params.max_connection_cnt =    fd_env_strip_cmdline_ulong( argc, argv, "--max-connection-cnt",    NULL, 50 );
     136           0 :   args->params.max_ws_connection_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-connection-cnt", NULL, 10 );
     137           0 :   args->params.max_request_len =       fd_env_strip_cmdline_ulong( argc, argv, "--max-request-len",       NULL, 1<<16 );
     138           0 :   args->params.max_ws_recv_frame_len = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-recv-frame-len", NULL, 2048 );
     139           0 :   args->params.max_ws_send_frame_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-send-frame-cnt", NULL, 100 );
     140           0 :   args->params.outgoing_buffer_sz    = fd_env_strip_cmdline_ulong( argc, argv, "--max-send-buf",          NULL, 100U<<20U );
     141           0 :   args->block_index_max              = fd_env_strip_cmdline_uint ( argc, argv, "--max-block_idx",         NULL, 65536 );
     142           0 :   args->txn_index_max                = fd_env_strip_cmdline_uint ( argc, argv, "--max-txn-idx",           NULL, 1048576 );
     143           0 :   args->acct_index_max               = fd_env_strip_cmdline_uint ( argc, argv, "--max-acct-idx",          NULL, 1048576 );
     144           0 :   strncpy(args->history_file,          fd_env_strip_cmdline_cstr ( argc, argv, "--rpc-history-file",      NULL, "rpc_history" ), sizeof(args->history_file)-1 );
     145           0 : }
     146             : 
     147             : static int stopflag = 0;
     148             : static void
     149           0 : signal1( int sig ) {
     150           0 :   (void)sig;
     151           0 :   stopflag = 1;
     152           0 : }
     153             : 
     154             : int main( int argc, char ** argv ) {
     155             :   fd_boot( &argc, &argv );
     156             :   fd_rpcserver_args_t args;
     157             : 
     158             :   replay_sham_link_t * rep_notify = NULL;
     159             :   stake_sham_link_t * stake_notify = NULL;
     160             :   repair_sham_link_t * repair_notify = NULL;
     161             : 
     162             :   ulong offline = fd_env_strip_cmdline_ulong( &argc, &argv, "--offline", NULL, 0 );
     163             :   if( !offline ) {
     164             :     init_args( &argc, &argv, &args );
     165             : 
     166             :     const char * wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-replay-out", NULL, "fd1_replay_out.wksp" );
     167             :     rep_notify = replay_sham_link_new( aligned_alloc( replay_sham_link_align(), replay_sham_link_footprint() ), wksp_name );
     168             : 
     169             :     wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-replay-stake", NULL, "fd1_replay_stake.wksp" );
     170             :     stake_notify = stake_sham_link_new( aligned_alloc( stake_sham_link_align(), stake_sham_link_footprint() ), wksp_name );
     171             : 
     172             :     wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-repair-repla", NULL, "fd1_repair_repla.wksp" );
     173             :     repair_notify = repair_sham_link_new( aligned_alloc( repair_sham_link_align(), repair_sham_link_footprint() ), wksp_name );
     174             : 
     175             :   } else {
     176             :     init_args_offline( &argc, &argv, &args );
     177             :   }
     178             : 
     179             : #define SMAX 1LU<<30
     180             :   uchar * smem = aligned_alloc( FD_SPAD_ALIGN, SMAX );
     181             :   args.spad = fd_spad_join( fd_spad_new( smem, SMAX ) );
     182             :   fd_spad_push( args.spad );
     183             : 
     184             :   struct sigaction sa = {
     185             :     .sa_handler = signal1,
     186             :     .sa_flags   = 0,
     187             :   };
     188             :   if( FD_UNLIKELY( sigaction( SIGTERM, &sa, NULL ) ) )
     189             :     FD_LOG_ERR(( "sigaction(SIGTERM) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     190             :   if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) ) )
     191             :     FD_LOG_ERR(( "sigaction(SIGINT) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     192             :   signal( SIGPIPE, SIG_IGN );
     193             : 
     194             :   fd_rpc_ctx_t * ctx = NULL;
     195             :   fd_rpc_create_ctx( &args, &ctx );
     196             :   fd_rpc_start_service( &args, ctx );
     197             : 
     198             :   if( args.offline ) {
     199             :     while( !stopflag ) {
     200             :       fd_rpc_ws_poll( ctx );
     201             :     }
     202             :     fd_halt();
     203             :     return 0;
     204             :   }
     205             : 
     206             :   replay_sham_link_start( rep_notify );
     207             :   stake_sham_link_start( stake_notify );
     208             :   repair_sham_link_start( repair_notify );
     209             : 
     210             :   while( !stopflag ) {
     211             :     replay_sham_link_poll( rep_notify, ctx );
     212             :     stake_sham_link_poll( stake_notify, ctx );
     213             :     repair_sham_link_poll( repair_notify, ctx );
     214             : 
     215             :     fd_rpc_ws_poll( ctx );
     216             :   }
     217             : 
     218             :   fd_halt();
     219             :   return 0;
     220             : }
     221             : 
     222             : static void
     223           0 : replay_sham_link_during_frag(fd_rpc_ctx_t * ctx, ulong sig, ulong ctl, void const * msg, ulong sz) {
     224           0 :   (void)sig;
     225           0 :   (void)ctl;
     226           0 :   fd_rpc_replay_during_frag( ctx, msg, sig, sz, ctl );
     227           0 : }
     228             : 
     229             : static void
     230           0 : replay_sham_link_after_frag(fd_rpc_ctx_t * ctx, ulong sig) {
     231           0 :   fd_rpc_replay_after_frag( ctx, sig );
     232           0 : }
     233             : 
     234             : static void
     235           0 : stake_sham_link_during_frag(fd_rpc_ctx_t * ctx, ulong sig, ulong ctl, void const * msg, ulong sz) {
     236           0 :   (void)sig;
     237           0 :   (void)ctl;
     238           0 :   fd_rpc_stake_during_frag( ctx, msg, sz );
     239           0 : }
     240             : 
     241             : static void
     242           0 : stake_sham_link_after_frag(fd_rpc_ctx_t * ctx, ulong sig) {
     243           0 :   (void)sig;
     244           0 :   fd_rpc_stake_after_frag( ctx );
     245           0 : }
     246             : 
     247             : static void
     248           0 : repair_sham_link_during_frag(fd_rpc_ctx_t * ctx, ulong sig, ulong ctl, void const * msg, ulong sz) {
     249           0 :   (void)sig;
     250           0 :   (void)ctl;
     251           0 :   fd_rpc_repair_during_frag( ctx, msg, sz );
     252           0 : }
     253             : 
     254             : static void
     255           0 : repair_sham_link_after_frag(fd_rpc_ctx_t * ctx, ulong sig) {
     256           0 :   (void)sig;
     257           0 :   fd_rpc_repair_after_frag( ctx );
     258           0 : }

Generated by: LCOV version 1.14