LCOV - code coverage report
Current view: top level - app/rpcserver - main.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 112 0.0 %
Date: 2024-11-13 11:58:15 Functions: 0 3 0.0 %

          Line data    Source code
       1             : #define _DEFAULT_SOURCE
       2             : #include <stdio.h>
       3             : #include <stdlib.h>
       4             : #include <signal.h>
       5             : #include <errno.h>
       6             : #include <unistd.h>
       7             : #include <netdb.h>
       8             : #include <sys/socket.h>
       9             : #include <netinet/in.h>
      10             : #include <arpa/inet.h>
      11             : #include "../../flamenco/rpcserver/fd_rpc_service.h"
      12             : #include "../../funk/fd_funk_filemap.h"
      13             : 
      14             : #define SHAM_LINK_CONTEXT fd_rpc_ctx_t
      15             : #define SHAM_LINK_STATE   fd_replay_notif_msg_t
      16             : #define SHAM_LINK_NAME    replay_sham_link
      17             : #include "sham_link.h"
      18             : 
      19             : #define SHAM_LINK_CONTEXT fd_rpc_ctx_t
      20             : #define SHAM_LINK_STATE   fd_stake_ci_t
      21             : #define SHAM_LINK_NAME    stake_sham_link
      22             : #include "sham_link.h"
      23             : 
      24             : static void
      25           0 : init_args( int * argc, char *** argv, fd_rpcserver_args_t * args ) {
      26           0 :   memset( args, 0, sizeof(fd_rpcserver_args_t) );
      27             : 
      28           0 :   args->valloc = fd_libc_alloc_virtual();
      29             : 
      30           0 :   char const * funk_file = fd_env_strip_cmdline_cstr( argc, argv, "--funk-file", NULL, NULL );
      31           0 :   if( FD_UNLIKELY( !funk_file ))
      32           0 :     FD_LOG_ERR(( "--funk-file argument is required" ));
      33           0 :   args->funk = fd_funk_open_file( funk_file, 1, 0, 0, 0, 0, FD_FUNK_READONLY, NULL );
      34           0 :   if( args->funk == NULL ) {
      35           0 :     FD_LOG_ERR(( "failed to join a funky" ));
      36           0 :   }
      37             : 
      38           0 :   const char * wksp_name = fd_env_strip_cmdline_cstr ( argc, argv, "--wksp-name-blockstore", NULL, "fd1_bstore.wksp" );
      39           0 :   FD_LOG_NOTICE(( "attaching to workspace \"%s\"", wksp_name ));
      40           0 :   fd_wksp_t * wksp = fd_wksp_attach( wksp_name );
      41           0 :   if( FD_UNLIKELY( !wksp ) )
      42           0 :     FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", wksp_name ));
      43           0 :   fd_wksp_tag_query_info_t info;
      44           0 :   ulong tag = 1;
      45           0 :   if( fd_wksp_tag_query( wksp, &tag, 1, &info, 1 ) <= 0 ) {
      46           0 :     FD_LOG_ERR(( "workspace \"%s\" does not contain a blockstore", wksp_name ));
      47           0 :   }
      48           0 :   void * shmem = fd_wksp_laddr_fast( wksp, info.gaddr_lo );
      49           0 :   args->blockstore = fd_blockstore_join( shmem );
      50           0 :   if( args->blockstore == NULL ) {
      51           0 :     FD_LOG_ERR(( "failed to join a blockstore" ));
      52           0 :   }
      53           0 :   FD_LOG_NOTICE(( "blockstore has slot root=%lu", args->blockstore->smr ));
      54           0 :   fd_wksp_mprotect( wksp, 1 );
      55             : 
      56           0 :   fd_pubkey_t identity_key[1]; /* Just the public key */
      57           0 :   memset( identity_key, 0xa5, sizeof(fd_pubkey_t) );
      58           0 :   args->stake_ci = fd_stake_ci_join( fd_stake_ci_new( aligned_alloc( fd_stake_ci_align(), fd_stake_ci_footprint() ), identity_key ) );
      59             : 
      60           0 :   args->port = (ushort)fd_env_strip_cmdline_ulong( argc, argv, "--port", NULL, 8899 );
      61             : 
      62           0 :   args->params.max_connection_cnt =    fd_env_strip_cmdline_ulong( argc, argv, "--max-connection-cnt",    NULL, 30 );
      63           0 :   args->params.max_ws_connection_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-connection-cnt", NULL, 10 );
      64           0 :   args->params.max_request_len =       fd_env_strip_cmdline_ulong( argc, argv, "--max-request-len",       NULL, 1<<16 );
      65           0 :   args->params.max_ws_recv_frame_len = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-recv-frame-len", NULL, 1<<16 );
      66           0 :   args->params.max_ws_send_frame_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-send-frame-cnt", NULL, 100 );
      67           0 :   args->params.outgoing_buffer_sz    = fd_env_strip_cmdline_ulong( argc, argv, "--max-send-buf",          NULL, 100U<<20U );
      68             : 
      69           0 :   const char * tpu_host = fd_env_strip_cmdline_cstr ( argc, argv, "--local-tpu-host", NULL, "127.0.0.1" );
      70           0 :   ulong tpu_port = fd_env_strip_cmdline_ulong( argc, argv, "--local-tpu-port", NULL, 9001U );
      71           0 :   memset( &args->tpu_addr, 0, sizeof(args->tpu_addr) );
      72           0 :   args->tpu_addr.sin_family = AF_INET;
      73           0 :   if( !inet_aton( tpu_host, &args->tpu_addr.sin_addr ) ) {
      74           0 :     struct hostent * hent = gethostbyname( tpu_host );
      75           0 :     if( hent == NULL ) {
      76           0 :       FD_LOG_WARNING(( "unable to resolve tpu host %s", tpu_host ));
      77           0 :       exit(-1);
      78           0 :     }
      79           0 :     args->tpu_addr.sin_addr.s_addr = ( (struct in_addr *)hent->h_addr_list[0] )->s_addr;
      80           0 :   }
      81           0 :   if( tpu_port < 1024 || tpu_port > (int)USHORT_MAX ) {
      82           0 :     FD_LOG_ERR(( "invalid tpu port number" ));
      83           0 :     exit(-1);
      84           0 :   }
      85           0 :   args->tpu_addr.sin_port = htons( (ushort)tpu_port );
      86           0 :   FD_LOG_NOTICE(( "using tpu %s:%u", inet_ntoa( args->tpu_addr.sin_addr ), (uint)ntohs( args->tpu_addr.sin_port ) ));
      87           0 : }
      88             : 
      89             : static void
      90           0 : init_args_offline( int * argc, char *** argv, fd_rpcserver_args_t * args ) {
      91           0 :   memset( args, 0, sizeof(fd_rpcserver_args_t) );
      92           0 :   args->offline = 1;
      93             : 
      94           0 :   args->valloc = fd_libc_alloc_virtual();
      95             : 
      96           0 :   char const * funk_file = fd_env_strip_cmdline_cstr( argc, argv, "--funk-file", NULL, NULL );
      97           0 :   if( FD_UNLIKELY( !funk_file ))
      98           0 :     FD_LOG_ERR(( "--funk-file argument is required" ));
      99           0 :   char const * restore = fd_env_strip_cmdline_cstr ( argc, argv, "--restore-funk", NULL, NULL );
     100           0 :   if( restore != NULL )
     101           0 :     args->funk = fd_funk_recover_checkpoint( funk_file, 1, restore, NULL );
     102           0 :   else
     103           0 :     args->funk = fd_funk_open_file( funk_file, 1, 0, 0, 0, 0, FD_FUNK_READONLY, NULL );
     104             : 
     105           0 :   fd_wksp_t * wksp;
     106           0 :   const char * wksp_name = fd_env_strip_cmdline_cstr ( argc, argv, "--wksp-name-blockstore", NULL, NULL );
     107           0 :   if( wksp_name != NULL ) {
     108           0 :     FD_LOG_NOTICE(( "attaching to workspace \"%s\"", wksp_name ));
     109           0 :     wksp = fd_wksp_attach( wksp_name );
     110           0 :     if( !wksp ) FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", wksp_name ));
     111           0 :   } else {
     112           0 :     char const * restore = fd_env_strip_cmdline_cstr ( argc, argv, "--restore-blockstore", NULL, NULL );
     113           0 :     if( restore == NULL ) FD_LOG_ERR(( "must use --wksp-name-blockstore or --restore-blockstore in offline mode" ));
     114           0 :     uint seed;
     115           0 :     ulong part_max;
     116           0 :     ulong data_max;
     117           0 :     int err = fd_wksp_restore_preview( restore, &seed, &part_max, &data_max );
     118           0 :     if( err ) FD_LOG_ERR(( "unable to restore %s: error %d", restore, err ));
     119           0 :     ulong page_cnt = (data_max + FD_SHMEM_GIGANTIC_PAGE_SZ-1U)/FD_SHMEM_GIGANTIC_PAGE_SZ;
     120           0 :     wksp = fd_wksp_new_anonymous( FD_SHMEM_GIGANTIC_PAGE_SZ, page_cnt, 0, "wksp-blockstore", 0UL );
     121           0 :     if( !wksp ) FD_LOG_ERR(( "unable to restore %s: failed to create wksp", restore ));
     122           0 :     FD_LOG_NOTICE(( "restoring blockstore wksp %s", restore ));
     123           0 :     fd_wksp_restore( wksp, restore, seed );
     124           0 :   }
     125           0 :   fd_wksp_tag_query_info_t info;
     126           0 :   ulong tag = 1;
     127           0 :   if( fd_wksp_tag_query( wksp, &tag, 1, &info, 1 ) <= 0 ) {
     128           0 :     FD_LOG_ERR(( "workspace does not contain a blockstore" ));
     129           0 :   }
     130           0 :   void * shmem = fd_wksp_laddr_fast( wksp, info.gaddr_lo );
     131           0 :   args->blockstore = fd_blockstore_join( shmem );
     132           0 :   if( args->blockstore == NULL ) {
     133           0 :     FD_LOG_ERR(( "failed to join a blockstore" ));
     134           0 :   }
     135           0 :   FD_LOG_NOTICE(( "blockstore has slot root=%lu", args->blockstore->smr ));
     136           0 :   fd_wksp_mprotect( wksp, 1 );
     137             : 
     138           0 :   args->port = (ushort)fd_env_strip_cmdline_ulong( argc, argv, "--port", NULL, 8899 );
     139             : 
     140           0 :   args->params.max_connection_cnt =    fd_env_strip_cmdline_ulong( argc, argv, "--max-connection-cnt",    NULL, 50 );
     141           0 :   args->params.max_ws_connection_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-connection-cnt", NULL, 10 );
     142           0 :   args->params.max_request_len =       fd_env_strip_cmdline_ulong( argc, argv, "--max-request-len",       NULL, 1<<16 );
     143           0 :   args->params.max_ws_recv_frame_len = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-recv-frame-len", NULL, 2048 );
     144           0 :   args->params.max_ws_send_frame_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-send-frame-cnt", NULL, 100 );
     145           0 :   args->params.outgoing_buffer_sz    = fd_env_strip_cmdline_ulong( argc, argv, "--max-send-buf",          NULL, 100U<<20U );
     146           0 : }
     147             : 
     148             : static int stopflag = 0;
     149             : static void
     150           0 : signal1( int sig ) {
     151           0 :   (void)sig;
     152           0 :   stopflag = 1;
     153           0 : }
     154             : 
     155             : int main( int argc, char ** argv ) {
     156             :   fd_boot( &argc, &argv );
     157             :   fd_rpcserver_args_t args;
     158             : 
     159             : #define SMAX 1LU<<28
     160             :   uchar * smem = aligned_alloc( FD_SCRATCH_SMEM_ALIGN,
     161             :                                 fd_ulong_align_up( fd_scratch_smem_footprint( SMAX  ), FD_SCRATCH_SMEM_ALIGN ) );
     162             :   ulong fmem[16U];
     163             :   fd_scratch_attach( smem, fmem, SMAX, 16U );
     164             : 
     165             :   replay_sham_link_t * rep_notify = NULL;
     166             :   stake_sham_link_t * stake_notify = NULL;
     167             : 
     168             :   ulong offline = fd_env_strip_cmdline_ulong( &argc, &argv, "--offline", NULL, 0 );
     169             :   if( !offline ) {
     170             :     init_args( &argc, &argv, &args );
     171             : 
     172             :     const char * wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-replay-notify", NULL, "fd1_replay_notif.wksp" );
     173             :     rep_notify = replay_sham_link_new( aligned_alloc( replay_sham_link_align(), replay_sham_link_footprint() ), wksp_name );
     174             : 
     175             :     wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-stake-out", NULL, "fd1_stake_out.wksp" );
     176             :     stake_notify = stake_sham_link_new( aligned_alloc( stake_sham_link_align(), stake_sham_link_footprint() ), wksp_name );
     177             : 
     178             :   } else {
     179             :     init_args_offline( &argc, &argv, &args );
     180             :   }
     181             : 
     182             :   struct sigaction sa = {
     183             :     .sa_handler = signal1,
     184             :     .sa_flags   = 0,
     185             :   };
     186             :   if( FD_UNLIKELY( sigaction( SIGTERM, &sa, NULL ) ) )
     187             :     FD_LOG_ERR(( "sigaction(SIGTERM) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     188             :   if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) ) )
     189             :     FD_LOG_ERR(( "sigaction(SIGINT) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     190             :   signal( SIGPIPE, SIG_IGN );
     191             : 
     192             :   fd_rpc_ctx_t * ctx = NULL;
     193             :   fd_rpc_create_ctx( &args, &ctx );
     194             :   fd_rpc_start_service( &args, ctx );
     195             : 
     196             :   if( args.offline ) {
     197             :     while( !stopflag ) {
     198             :       fd_rpc_ws_poll( ctx );
     199             :     }
     200             :     fd_rpc_stop_service( ctx );
     201             :     fd_halt();
     202             :     return 0;
     203             :   }
     204             : 
     205             :   replay_sham_link_start( rep_notify );
     206             :   stake_sham_link_start( stake_notify );
     207             :   while( !stopflag ) {
     208             :     fd_replay_notif_msg_t msg;
     209             :     replay_sham_link_poll( rep_notify, ctx, &msg );
     210             : 
     211             :     stake_sham_link_poll( stake_notify, ctx, args.stake_ci );
     212             : 
     213             :     fd_rpc_ws_poll( ctx );
     214             :   }
     215             : 
     216             :   fd_rpc_stop_service( ctx );
     217             : 
     218             :   fd_halt();
     219             :   return 0;
     220             : }

Generated by: LCOV version 1.14