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 : #define SHAM_LINK_CONTEXT fd_rpc_ctx_t
31 : #define SHAM_LINK_NAME tower_sham_link
32 : #include "sham_link.h"
33 :
34 : static void
35 0 : init_args( int * argc, char *** argv, fd_rpcserver_args_t * args ) {
36 0 : memset( args, 0, sizeof(fd_rpcserver_args_t) );
37 :
38 0 : const char * mount_path = fd_env_strip_cmdline_cstr( argc, argv, "--mount-path", NULL, "/mnt/.fd" );
39 0 : if( FD_UNLIKELY( mount_path )) {
40 0 : strncpy( fd_shmem_private_base, mount_path, sizeof(fd_shmem_private_base)-1 );
41 0 : fd_shmem_private_base_len = strlen( mount_path );
42 0 : }
43 :
44 0 : const char * funk_wksp_name = fd_env_strip_cmdline_cstr( argc, argv, "--funk-wksp-name", NULL, "fd1_funk.wksp" );
45 0 : if( FD_UNLIKELY( !funk_wksp_name ))
46 0 : FD_LOG_ERR(( "--funk-wksp-name argument is required" ));
47 0 : FD_LOG_NOTICE(( "attaching to workspace \"%s\"", funk_wksp_name ));
48 0 : fd_wksp_t * funk_wksp = fd_wksp_attach( funk_wksp_name );
49 0 : if( FD_UNLIKELY( !funk_wksp ))
50 0 : FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", funk_wksp_name ));
51 0 : fd_wksp_tag_query_info_t info;
52 0 : ulong tag = 1;
53 0 : if( fd_wksp_tag_query( funk_wksp, &tag, 1, &info, 1 ) <= 0 ) {
54 0 : FD_LOG_ERR(( "workspace does not contain a funk" ));
55 0 : }
56 0 : void * funk_shmem = fd_wksp_laddr_fast( funk_wksp, info.gaddr_lo );
57 0 : fd_funk_t * funk = fd_funk_join( args->funk, funk_shmem );
58 0 : if( FD_UNLIKELY( !funk ))
59 0 : FD_LOG_ERR(( "failed to join funk" ));
60 :
61 0 : const char * store_wksp_name = fd_env_strip_cmdline_cstr( argc, argv, "--store-wksp-name", NULL, "fd1_store.wksp" );
62 0 : if( FD_UNLIKELY( !store_wksp_name ))
63 0 : FD_LOG_ERR(( "--store-wksp-name argument is required" ));
64 0 : FD_LOG_NOTICE(( "attaching to workspace \"%s\"", store_wksp_name ));
65 0 : fd_wksp_t * store_wksp = fd_wksp_attach( store_wksp_name );
66 0 : if( FD_UNLIKELY( !store_wksp ))
67 0 : FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", store_wksp_name ));
68 0 : if( fd_wksp_tag_query( store_wksp, &tag, 1, &info, 1 ) <= 0 ) {
69 0 : FD_LOG_ERR(( "workspace does not contain a store" ));
70 0 : }
71 0 : void * store_shmem = fd_wksp_laddr_fast( store_wksp, info.gaddr_lo );
72 0 : fd_store_t * store = fd_store_join( store_shmem );
73 0 : if( FD_UNLIKELY( !store ))
74 0 : FD_LOG_ERR(( "failed to join store" ));
75 0 : args->store = store;
76 :
77 0 : const char * identity_key_path = fd_env_strip_cmdline_cstr( argc, argv, "--identity-key-path", NULL, "/home/fire/.firedancer/fd1/identity.json" );
78 0 : uchar const * identity_key = fd_keyload_load( identity_key_path, /* pubkey only: */ 1 );
79 0 : if( FD_UNLIKELY( !identity_key ))
80 0 : FD_LOG_ERR(( "failed to load identity key from %s", identity_key_path ));
81 0 : memcpy( args->identity_key.uc, identity_key, sizeof(args->identity_key) );
82 :
83 0 : args->port = (ushort)fd_env_strip_cmdline_ulong( argc, argv, "--port", NULL, 8899 );
84 :
85 0 : args->params.max_connection_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-connection-cnt", NULL, 10 );
86 0 : args->params.max_ws_connection_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-connection-cnt", NULL, 10 );
87 0 : args->params.max_request_len = fd_env_strip_cmdline_ulong( argc, argv, "--max-request-len", NULL, 1<<16 );
88 0 : args->params.max_ws_recv_frame_len = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-recv-frame-len", NULL, 1<<16 );
89 0 : args->params.max_ws_send_frame_cnt = fd_env_strip_cmdline_ulong( argc, argv, "--max-ws-send-frame-cnt", NULL, 100 );
90 0 : args->params.outgoing_buffer_sz = fd_env_strip_cmdline_ulong( argc, argv, "--max-send-buf", NULL, 100U<<20U );
91 0 : args->block_index_max = fd_env_strip_cmdline_uint ( argc, argv, "--max-block_idx", NULL, 65536 );
92 0 : args->txn_index_max = fd_env_strip_cmdline_uint ( argc, argv, "--max-txn-idx", NULL, 1U<<21U );
93 0 : args->acct_index_max = fd_env_strip_cmdline_uint ( argc, argv, "--max-acct-idx", NULL, 1U<<21U );
94 0 : strncpy(args->history_file, fd_env_strip_cmdline_cstr ( argc, argv, "--rpc-history-file", NULL, "rpc_history" ), sizeof(args->history_file)-1 );
95 :
96 0 : const char * tpu_host = fd_env_strip_cmdline_cstr ( argc, argv, "--local-tpu-host", NULL, "127.0.0.1" );
97 0 : ulong tpu_port = fd_env_strip_cmdline_ulong( argc, argv, "--local-tpu-port", NULL, 9001U );
98 0 : memset( &args->tpu_addr, 0, sizeof(args->tpu_addr) );
99 0 : args->tpu_addr.sin_family = AF_INET;
100 0 : if( !inet_aton( tpu_host, &args->tpu_addr.sin_addr ) ) {
101 0 : struct hostent * hent = gethostbyname( tpu_host );
102 0 : if( hent == NULL ) {
103 0 : FD_LOG_WARNING(( "unable to resolve tpu host %s", tpu_host ));
104 0 : exit(-1);
105 0 : }
106 0 : args->tpu_addr.sin_addr.s_addr = ( (struct in_addr *)hent->h_addr_list[0] )->s_addr;
107 0 : }
108 0 : if( tpu_port < 1024 || tpu_port > (int)USHORT_MAX ) {
109 0 : FD_LOG_ERR(( "invalid tpu port number" ));
110 0 : exit(-1);
111 0 : }
112 0 : args->tpu_addr.sin_port = htons( (ushort)tpu_port );
113 0 : FD_LOG_NOTICE(( "using tpu %s:%u", inet_ntoa( args->tpu_addr.sin_addr ), (uint)ntohs( args->tpu_addr.sin_port ) ));
114 0 : }
115 :
116 : static void
117 0 : init_args_offline( int * argc, char *** argv, fd_rpcserver_args_t * args ) {
118 0 : memset( args, 0, sizeof(fd_rpcserver_args_t) );
119 0 : args->offline = 1;
120 :
121 0 : const char * funk_wksp_name = fd_env_strip_cmdline_cstr( argc, argv, "--funk-wksp-name", NULL, "fd1_funk.wksp" );
122 0 : if( FD_UNLIKELY( !funk_wksp_name ))
123 0 : FD_LOG_ERR(( "--funk-wksp-name argument is required" ));
124 0 : fd_wksp_t * funk_wksp = fd_wksp_attach( funk_wksp_name );
125 0 : if( FD_UNLIKELY( !funk_wksp ))
126 0 : FD_LOG_ERR(( "unable to attach to \"%s\"\n\tprobably does not exist or bad permissions", funk_wksp_name ));
127 0 : fd_wksp_tag_query_info_t info;
128 0 : ulong tag = 1;
129 0 : if( fd_wksp_tag_query( funk_wksp, &tag, 1, &info, 1 ) <= 0 ) {
130 0 : FD_LOG_ERR(( "workspace does not contain a funk" ));
131 0 : }
132 0 : void * funk_shmem = fd_wksp_laddr_fast( funk_wksp, info.gaddr_lo );
133 0 : fd_funk_t * funk = fd_funk_join( args->funk, funk_shmem );
134 0 : if( FD_UNLIKELY( !funk ))
135 0 : FD_LOG_ERR(( "failed to join funk" ));
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 : repair_sham_link_t * repair_notify = NULL;
165 : tower_sham_link_t * tower_notify = NULL;
166 :
167 : ulong offline = fd_env_strip_cmdline_ulong( &argc, &argv, "--offline", NULL, 0 );
168 : if( !offline ) {
169 : init_args( &argc, &argv, &args );
170 :
171 : const char * wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-replay-notify", NULL, "fd1_replay_notif.wksp" );
172 : rep_notify = replay_sham_link_new( aligned_alloc( replay_sham_link_align(), replay_sham_link_footprint() ), wksp_name );
173 :
174 : wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-stake-out", NULL, "fd1_stake_out.wksp" );
175 : stake_notify = stake_sham_link_new( aligned_alloc( stake_sham_link_align(), stake_sham_link_footprint() ), wksp_name );
176 :
177 : wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-repair-repla", NULL, "fd1_repair_repla.wksp" );
178 : repair_notify = repair_sham_link_new( aligned_alloc( repair_sham_link_align(), repair_sham_link_footprint() ), wksp_name );
179 :
180 : wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name-replay-tower", NULL, "fd1_replay_tower.wksp" );
181 : tower_notify = tower_sham_link_new( aligned_alloc( tower_sham_link_align(), tower_sham_link_footprint() ), wksp_name );
182 :
183 : } else {
184 : init_args_offline( &argc, &argv, &args );
185 : }
186 :
187 : #define SMAX 1LU<<30
188 : uchar * smem = aligned_alloc( FD_SPAD_ALIGN, SMAX );
189 : args.spad = fd_spad_join( fd_spad_new( smem, SMAX ) );
190 : fd_spad_push( args.spad );
191 :
192 : struct sigaction sa = {
193 : .sa_handler = signal1,
194 : .sa_flags = 0,
195 : };
196 : if( FD_UNLIKELY( sigaction( SIGTERM, &sa, NULL ) ) )
197 : FD_LOG_ERR(( "sigaction(SIGTERM) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
198 : if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) ) )
199 : FD_LOG_ERR(( "sigaction(SIGINT) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
200 : signal( SIGPIPE, SIG_IGN );
201 :
202 : fd_rpc_ctx_t * ctx = NULL;
203 : fd_rpc_create_ctx( &args, &ctx );
204 : fd_rpc_start_service( &args, ctx );
205 :
206 : if( args.offline ) {
207 : while( !stopflag ) {
208 : fd_rpc_ws_poll( ctx );
209 : }
210 : fd_halt();
211 : return 0;
212 : }
213 :
214 : replay_sham_link_start( rep_notify );
215 : stake_sham_link_start( stake_notify );
216 : repair_sham_link_start( repair_notify );
217 : tower_sham_link_start( tower_notify );
218 :
219 : while( !stopflag ) {
220 : replay_sham_link_poll( rep_notify, ctx );
221 : stake_sham_link_poll( stake_notify, ctx );
222 : repair_sham_link_poll( repair_notify, ctx );
223 : tower_sham_link_poll( tower_notify, ctx );
224 :
225 : fd_rpc_ws_poll( ctx );
226 : }
227 :
228 : fd_halt();
229 : return 0;
230 : }
231 :
232 : static void
233 0 : replay_sham_link_during_frag(fd_rpc_ctx_t * ctx, ulong sig, ulong ctl, void const * msg, int sz) {
234 0 : (void)sig;
235 0 : (void)ctl;
236 0 : fd_rpc_replay_during_frag( ctx, msg, sz );
237 0 : }
238 :
239 : static void
240 0 : replay_sham_link_after_frag(fd_rpc_ctx_t * ctx) {
241 0 : fd_rpc_replay_after_frag( ctx );
242 0 : }
243 :
244 : static void
245 0 : stake_sham_link_during_frag(fd_rpc_ctx_t * ctx, ulong sig, ulong ctl, void const * msg, int sz) {
246 0 : (void)sig;
247 0 : (void)ctl;
248 0 : fd_rpc_stake_during_frag( ctx, msg, sz );
249 0 : }
250 :
251 : static void
252 0 : stake_sham_link_after_frag(fd_rpc_ctx_t * ctx) {
253 0 : fd_rpc_stake_after_frag( ctx );
254 0 : }
255 :
256 : static void
257 0 : repair_sham_link_during_frag(fd_rpc_ctx_t * ctx, ulong sig, ulong ctl, void const * msg, int sz) {
258 0 : (void)sig;
259 0 : (void)ctl;
260 0 : fd_rpc_repair_during_frag( ctx, msg, sz );
261 0 : }
262 :
263 : static void
264 0 : repair_sham_link_after_frag(fd_rpc_ctx_t * ctx) {
265 0 : fd_rpc_repair_after_frag( ctx );
266 0 : }
267 :
268 : static void
269 0 : tower_sham_link_during_frag(fd_rpc_ctx_t * ctx, ulong sig, ulong ctl, void const * msg, int sz) {
270 0 : fd_rpc_tower_during_frag( ctx, sig, ctl, msg, sz );
271 0 : }
272 :
273 : static void
274 0 : tower_sham_link_after_frag(fd_rpc_ctx_t * ctx) {
275 0 : fd_rpc_tower_after_frag( ctx );
276 0 : }
|