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