LCOV - code coverage report
Current view: top level - app/rpcserver - main.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 127 0.0 %
Date: 2025-08-05 05:04:49 Functions: 0 7 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             : 
      16             : #define SHAM_LINK_CONTEXT fd_rpc_ctx_t
      17             : #define SHAM_LINK_STATE   fd_replay_notif_msg_t
      18             : #define SHAM_LINK_NAME    replay_sham_link
      19             : #include "sham_link.h"
      20             : 
      21             : #define SHAM_LINK_CONTEXT fd_rpc_ctx_t
      22             : #define SHAM_LINK_STATE   fd_multi_epoch_leaders_t
      23             : #define SHAM_LINK_NAME    stake_sham_link
      24             : #include "sham_link.h"
      25             : 
      26             : static void
      27           0 : init_args( int * argc, char *** argv, fd_rpcserver_args_t * args ) {
      28           0 :   memset( args, 0, sizeof(fd_rpcserver_args_t) );
      29             : 
      30           0 :   const char * funk_wksp_name = fd_env_strip_cmdline_cstr( argc, argv, "--funk-wksp-name", NULL, "fd1_funk.wksp" );
      31           0 :   if( FD_UNLIKELY( !funk_wksp_name ))
      32           0 :     FD_LOG_ERR(( "--funk-wksp-name argument is required" ));
      33           0 :   fd_wksp_t * funk_wksp = fd_wksp_attach( funk_wksp_name );
      34           0 :   if( FD_UNLIKELY( !funk_wksp ))
      35           0 :     FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", funk_wksp_name ));
      36           0 :   fd_wksp_tag_query_info_t info;
      37           0 :   ulong tag = 1;
      38           0 :   if( fd_wksp_tag_query( funk_wksp, &tag, 1, &info, 1 ) <= 0 ) {
      39           0 :     FD_LOG_ERR(( "workspace does not contain a funk" ));
      40           0 :   }
      41           0 :   void * funk_shmem = fd_wksp_laddr_fast( funk_wksp, info.gaddr_lo );
      42           0 :   fd_funk_t * funk = fd_funk_join( args->funk, funk_shmem );
      43           0 :   if( FD_UNLIKELY( !funk ))
      44           0 :     FD_LOG_ERR(( "failed to join funk" ));
      45             : 
      46             : 
      47           0 :   const char * wksp_name = fd_env_strip_cmdline_cstr ( argc, argv, "--wksp-name-blockstore", NULL, "fd1_blockstore.wksp" );
      48           0 :   FD_LOG_NOTICE(( "attaching to workspace \"%s\"", wksp_name ));
      49           0 :   fd_wksp_t * wksp = fd_wksp_attach( wksp_name );
      50           0 :   if( FD_UNLIKELY( !wksp ) )
      51           0 :     FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", wksp_name ));
      52           0 :   tag = 1;
      53           0 :   if( fd_wksp_tag_query( wksp, &tag, 1, &info, 1 ) <= 0 ) {
      54           0 :     FD_LOG_ERR(( "workspace \"%s\" does not contain a blockstore", wksp_name ));
      55           0 :   }
      56           0 :   fd_wksp_mprotect( wksp, 1 );
      57             : 
      58           0 :   args->leaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( aligned_alloc( fd_multi_epoch_leaders_align(), fd_multi_epoch_leaders_footprint() ) ) );
      59           0 :   args->port    = (ushort)fd_env_strip_cmdline_ulong( argc, argv, "--port", NULL, 8899 );
      60             : 
      61           0 :   args->params.max_connection_cnt =    fd_env_strip_cmdline_ulong( argc, argv, "--max-connection-cnt",    NULL, 30 );
      62           0 :   args->params.max_ws_connection_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-connection-cnt", NULL, 10 );
      63           0 :   args->params.max_request_len =       fd_env_strip_cmdline_ulong( argc, argv, "--max-request-len",       NULL, 1<<16 );
      64           0 :   args->params.max_ws_recv_frame_len = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-recv-frame-len", NULL, 1<<16 );
      65           0 :   args->params.max_ws_send_frame_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-send-frame-cnt", NULL, 100 );
      66           0 :   args->params.outgoing_buffer_sz    = fd_env_strip_cmdline_ulong( argc, argv, "--max-send-buf",          NULL, 100U<<20U );
      67           0 :   args->block_index_max              = fd_env_strip_cmdline_uint ( argc, argv, "--max-block_idx",         NULL, 65536 );
      68           0 :   args->txn_index_max                = fd_env_strip_cmdline_uint ( argc, argv, "--max-txn-idx",           NULL, 1048576 );
      69           0 :   args->acct_index_max               = fd_env_strip_cmdline_uint ( argc, argv, "--max-acct-idx",          NULL, 1048576 );
      70           0 :   strncpy(args->history_file,          fd_env_strip_cmdline_cstr ( argc, argv, "--rpc-history-file",      NULL, "rpc_history" ), sizeof(args->history_file)-1 );
      71             : 
      72           0 :   const char * tpu_host = fd_env_strip_cmdline_cstr ( argc, argv, "--local-tpu-host", NULL, "127.0.0.1" );
      73           0 :   ulong tpu_port = fd_env_strip_cmdline_ulong( argc, argv, "--local-tpu-port", NULL, 9001U );
      74           0 :   memset( &args->tpu_addr, 0, sizeof(args->tpu_addr) );
      75           0 :   args->tpu_addr.sin_family = AF_INET;
      76           0 :   if( !inet_aton( tpu_host, &args->tpu_addr.sin_addr ) ) {
      77           0 :     struct hostent * hent = gethostbyname( tpu_host );
      78           0 :     if( hent == NULL ) {
      79           0 :       FD_LOG_WARNING(( "unable to resolve tpu host %s", tpu_host ));
      80           0 :       exit(-1);
      81           0 :     }
      82           0 :     args->tpu_addr.sin_addr.s_addr = ( (struct in_addr *)hent->h_addr_list[0] )->s_addr;
      83           0 :   }
      84           0 :   if( tpu_port < 1024 || tpu_port > (int)USHORT_MAX ) {
      85           0 :     FD_LOG_ERR(( "invalid tpu port number" ));
      86           0 :     exit(-1);
      87           0 :   }
      88           0 :   args->tpu_addr.sin_port = htons( (ushort)tpu_port );
      89           0 :   FD_LOG_NOTICE(( "using tpu %s:%u", inet_ntoa( args->tpu_addr.sin_addr ), (uint)ntohs( args->tpu_addr.sin_port ) ));
      90           0 : }
      91             : 
      92             : static void
      93           0 : init_args_offline( int * argc, char *** argv, fd_rpcserver_args_t * args ) {
      94           0 :   memset( args, 0, sizeof(fd_rpcserver_args_t) );
      95           0 :   args->offline = 1;
      96             : 
      97           0 :   const char * funk_wksp_name = fd_env_strip_cmdline_cstr( argc, argv, "--funk-wksp-name", NULL, "fd1_funk.wksp" );
      98           0 :   if( FD_UNLIKELY( !funk_wksp_name ))
      99           0 :     FD_LOG_ERR(( "--funk-wksp-name argument is required" ));
     100           0 :   fd_wksp_t * funk_wksp = fd_wksp_attach( funk_wksp_name );
     101           0 :   if( FD_UNLIKELY( !funk_wksp ))
     102           0 :     FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", funk_wksp_name ));
     103           0 :   fd_wksp_tag_query_info_t info;
     104           0 :   ulong tag = 1;
     105           0 :   if( fd_wksp_tag_query( funk_wksp, &tag, 1, &info, 1 ) <= 0 ) {
     106           0 :     FD_LOG_ERR(( "workspace does not contain a funk" ));
     107           0 :   }
     108           0 :   void * funk_shmem = fd_wksp_laddr_fast( funk_wksp, info.gaddr_lo );
     109           0 :   fd_funk_t * funk = fd_funk_join( args->funk, funk_shmem );
     110           0 :   if( FD_UNLIKELY( !funk ))
     111           0 :     FD_LOG_ERR(( "failed to join funk" ));
     112             : 
     113           0 :   fd_wksp_t * wksp;
     114           0 :   const char * wksp_name = fd_env_strip_cmdline_cstr ( argc, argv, "--wksp-name-blockstore", NULL, NULL );
     115           0 :   if( wksp_name != NULL ) {
     116           0 :     FD_LOG_NOTICE(( "attaching to workspace \"%s\"", wksp_name ));
     117           0 :     wksp = fd_wksp_attach( wksp_name );
     118           0 :     if( !wksp ) FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", wksp_name ));
     119           0 :   } else {
     120           0 :     char const * restore = fd_env_strip_cmdline_cstr ( argc, argv, "--restore-blockstore", NULL, NULL );
     121           0 :     if( restore == NULL ) FD_LOG_ERR(( "must use --wksp-name-blockstore or --restore-blockstore in offline mode" ));
     122           0 :     fd_wksp_preview_t preview[1];
     123           0 :     int err = fd_wksp_preview( restore, preview );
     124           0 :     if( err ) FD_LOG_ERR(( "unable to restore %s: error %d", restore, err ));
     125           0 :     ulong page_cnt = (preview->data_max + FD_SHMEM_GIGANTIC_PAGE_SZ-1U)/FD_SHMEM_GIGANTIC_PAGE_SZ;
     126           0 :     wksp = fd_wksp_new_anonymous( FD_SHMEM_GIGANTIC_PAGE_SZ, page_cnt, 0, "wksp-blockstore", 0UL );
     127           0 :     if( !wksp ) FD_LOG_ERR(( "unable to restore %s: failed to create wksp", restore ));
     128           0 :     FD_LOG_NOTICE(( "restoring blockstore wksp %s", restore ));
     129           0 :     fd_wksp_restore( wksp, restore, preview->seed );
     130           0 :   }
     131           0 :   tag = 1;
     132           0 :   if( fd_wksp_tag_query( wksp, &tag, 1, &info, 1 ) <= 0 ) {
     133           0 :     FD_LOG_ERR(( "workspace does not contain a blockstore" ));
     134           0 :   }
     135           0 :   fd_wksp_mprotect( wksp, 1 );
     136             : 
     137           0 :   args->port = (ushort)fd_env_strip_cmdline_ulong( argc, argv, "--port", NULL, 8899 );
     138             : 
     139           0 :   args->params.max_connection_cnt =    fd_env_strip_cmdline_ulong( argc, argv, "--max-connection-cnt",    NULL, 50 );
     140           0 :   args->params.max_ws_connection_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-connection-cnt", NULL, 10 );
     141           0 :   args->params.max_request_len =       fd_env_strip_cmdline_ulong( argc, argv, "--max-request-len",       NULL, 1<<16 );
     142           0 :   args->params.max_ws_recv_frame_len = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-recv-frame-len", NULL, 2048 );
     143           0 :   args->params.max_ws_send_frame_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-send-frame-cnt", NULL, 100 );
     144           0 :   args->params.outgoing_buffer_sz    = fd_env_strip_cmdline_ulong( argc, argv, "--max-send-buf",          NULL, 100U<<20U );
     145           0 :   args->block_index_max              = fd_env_strip_cmdline_uint ( argc, argv, "--max-block_idx",         NULL, 65536 );
     146           0 :   args->txn_index_max                = fd_env_strip_cmdline_uint ( argc, argv, "--max-txn-idx",           NULL, 1048576 );
     147           0 :   args->acct_index_max               = fd_env_strip_cmdline_uint ( argc, argv, "--max-acct-idx",          NULL, 1048576 );
     148           0 :   strncpy(args->history_file,          fd_env_strip_cmdline_cstr ( argc, argv, "--rpc-history-file",      NULL, "rpc_history" ), sizeof(args->history_file)-1 );
     149           0 : }
     150             : 
     151             : static int stopflag = 0;
     152             : static void
     153           0 : signal1( int sig ) {
     154           0 :   (void)sig;
     155           0 :   stopflag = 1;
     156           0 : }
     157             : 
     158             : int main( int argc, char ** argv ) {
     159             :   fd_boot( &argc, &argv );
     160             :   fd_rpcserver_args_t args;
     161             : 
     162             :   replay_sham_link_t * rep_notify = NULL;
     163             :   stake_sham_link_t * stake_notify = NULL;
     164             : 
     165             :   ulong offline = fd_env_strip_cmdline_ulong( &argc, &argv, "--offline", NULL, 0 );
     166             :   if( !offline ) {
     167             :     init_args( &argc, &argv, &args );
     168             : 
     169             :     const char * wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-replay-notify", NULL, "fd1_replay_notif.wksp" );
     170             :     rep_notify = replay_sham_link_new( aligned_alloc( replay_sham_link_align(), replay_sham_link_footprint() ), wksp_name );
     171             : 
     172             :     wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-stake-out", NULL, "fd1_stake_out.wksp" );
     173             :     stake_notify = stake_sham_link_new( aligned_alloc( stake_sham_link_align(), stake_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             :   while( !stopflag ) {
     209             :     fd_replay_notif_msg_t msg;
     210             :     replay_sham_link_poll( rep_notify, ctx, &msg );
     211             : 
     212             :     stake_sham_link_poll( stake_notify, ctx, args.leaders );
     213             : 
     214             :     fd_rpc_ws_poll( ctx );
     215             :   }
     216             : 
     217             :   fd_halt();
     218             :   return 0;
     219             : }
     220             : 
     221             : static void
     222           0 : replay_sham_link_during_frag(fd_rpc_ctx_t * ctx, fd_replay_notif_msg_t * state, void const * msg, int sz) {
     223           0 :   fd_rpc_replay_during_frag( ctx, state, msg, sz );
     224           0 : }
     225             : 
     226             : static void
     227           0 : replay_sham_link_after_frag(fd_rpc_ctx_t * ctx, fd_replay_notif_msg_t * msg) {
     228           0 :   fd_rpc_replay_after_frag( ctx, msg );
     229           0 : }
     230             : 
     231             : static void
     232           0 : stake_sham_link_during_frag(fd_rpc_ctx_t * ctx, fd_multi_epoch_leaders_t * state, void const * msg, int sz) {
     233           0 :   fd_rpc_stake_during_frag( ctx, state, msg, sz );
     234           0 : }
     235             : 
     236             : static void
     237           0 : stake_sham_link_after_frag(fd_rpc_ctx_t * ctx, fd_multi_epoch_leaders_t * state) {
     238           0 :   fd_rpc_stake_after_frag( ctx, state );
     239           0 : }

Generated by: LCOV version 1.14