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