Line data Source code
1 : #include "fd_snapct_tile.h"
2 : #include "utils/fd_sspeer.h"
3 : #include "utils/fd_ssping.h"
4 : #include "utils/fd_ssctrl.h"
5 : #include "utils/fd_ssarchive.h"
6 : #include "utils/fd_http_resolver.h"
7 : #include "utils/fd_ssmsg.h"
8 :
9 : #include "../../disco/topo/fd_topo.h"
10 : #include "../../disco/metrics/fd_metrics.h"
11 : #include "../../flamenco/gossip/fd_gossip_message.h"
12 : #include "../../waltz/openssl/fd_openssl_tile.h"
13 :
14 : #include <errno.h>
15 : #include <stdio.h>
16 : #include <fcntl.h>
17 : #include <unistd.h>
18 : #include <sys/stat.h>
19 : #include <netinet/tcp.h>
20 : #include <netinet/in.h>
21 :
22 : #include "generated/fd_snapct_tile_seccomp.h"
23 :
24 : #define NAME "snapct"
25 :
26 : /* FIXME: Do a finishing pass over the default.toml config options / comments */
27 :
28 0 : #define GOSSIP_PEERS_MAX (FD_CONTACT_INFO_TABLE_SIZE)
29 0 : #define SERVER_PEERS_MAX (FD_TOPO_SNAPSHOTS_SERVERS_MAX_RESOLVED)
30 0 : #define TOTAL_PEERS_MAX (GOSSIP_PEERS_MAX + SERVER_PEERS_MAX)
31 :
32 0 : #define IN_KIND_ACK (0)
33 0 : #define IN_KIND_SNAPLD (1)
34 0 : #define IN_KIND_GOSSIP (2)
35 : #define MAX_IN_LINKS (3)
36 :
37 0 : #define TEMP_FULL_SNAP_NAME ".snapshot.tar.bz2-partial"
38 0 : #define TEMP_INCR_SNAP_NAME ".incremental-snapshot.tar.bz2-partial"
39 :
40 : struct fd_snapct_out_link {
41 : ulong idx;
42 : fd_wksp_t * mem;
43 : ulong chunk0;
44 : ulong wmark;
45 : ulong chunk;
46 : ulong mtu;
47 : };
48 : typedef struct fd_snapct_out_link fd_snapct_out_link_t;
49 :
50 0 : #define FD_SNAPCT_COLLECTING_PEERS_TIMEOUT (90L*1000L*1000L*1000L) /* 1.5 minutes */
51 0 : #define FD_SNAPCT_WAITING_FOR_PEERS_TIMEOUT (30L*1000L*1000L*1000L) /* 30 seconds */
52 :
53 : struct gossip_ci_entry {
54 : fd_pubkey_t pubkey;
55 : int allowed;
56 : fd_ip4_port_t rpc_addr;
57 : ulong map_next;
58 : };
59 : typedef struct gossip_ci_entry gossip_ci_entry_t;
60 :
61 : #define MAP_NAME gossip_ci_map
62 0 : #define MAP_KEY pubkey
63 : #define MAP_ELE_T gossip_ci_entry_t
64 : #define MAP_KEY_T fd_pubkey_t
65 0 : #define MAP_NEXT map_next
66 0 : #define MAP_KEY_EQ(k0,k1) fd_pubkey_eq( k0, k1 )
67 0 : #define MAP_KEY_HASH(key,seed) fd_hash( seed, key, sizeof(fd_pubkey_t) )
68 : #include "../../util/tmpl/fd_map_chain.c"
69 :
70 : struct fd_snapct_tile {
71 : struct fd_topo_tile_snapct config;
72 : int gossip_enabled;
73 : int download_enabled;
74 :
75 : fd_ssping_t * ssping;
76 : fd_http_resolver_t * ssresolver;
77 : fd_sspeer_selector_t * selector;
78 : ulong selector_seed;
79 :
80 : int state;
81 : int malformed;
82 : long deadline_nanos;
83 : int flush_ack;
84 : fd_sspeer_t peer;
85 :
86 : struct {
87 : int dir_fd;
88 : int full_snapshot_fd;
89 : int incremental_snapshot_fd;
90 : } local_out;
91 :
92 : char http_full_snapshot_name[ PATH_MAX ];
93 : char http_incr_snapshot_name[ PATH_MAX ];
94 :
95 : fd_wksp_t const * gossip_in_mem;
96 : fd_wksp_t const * snapld_in_mem;
97 : uchar in_kind[ MAX_IN_LINKS ];
98 :
99 : struct {
100 : ulong full_slot;
101 : ulong slot;
102 : int pending;
103 : } predicted_incremental;
104 :
105 : struct {
106 : ulong full_snapshot_slot;
107 : char full_snapshot_path[ PATH_MAX ];
108 : ulong full_snapshot_size;
109 : int full_snapshot_zstd;
110 :
111 : uchar full_snapshot_hash[ FD_HASH_FOOTPRINT ];
112 : uchar incremental_snapshot_hash[ FD_HASH_FOOTPRINT ];
113 :
114 : ulong incremental_snapshot_slot;
115 : char incremental_snapshot_path[ PATH_MAX ];
116 : ulong incremental_snapshot_size;
117 : int incremental_snapshot_zstd;
118 : } local_in;
119 :
120 : struct {
121 : struct {
122 : ulong bytes_read;
123 : ulong bytes_written;
124 : ulong bytes_total;
125 : uint num_retries;
126 : } full;
127 :
128 : struct {
129 : ulong bytes_read;
130 : ulong bytes_written;
131 : ulong bytes_total;
132 : uint num_retries;
133 : } incremental;
134 : } metrics;
135 :
136 : struct {
137 : gossip_ci_entry_t * ci_table; /* flat array of all gossip entries, allowed or not */
138 : gossip_ci_map_t * ci_map; /* map from pubkey to only allowed gossip entries */
139 : ulong allowed_cnt; /* number of allowed entries in ci_map */
140 : int saturated;
141 : } gossip;
142 :
143 : long snapshot_start_timestamp_ns;
144 :
145 : fd_snapct_out_link_t out_ld;
146 : fd_snapct_out_link_t out_gui;
147 : fd_snapct_out_link_t out_rp;
148 : };
149 : typedef struct fd_snapct_tile fd_snapct_tile_t;
150 :
151 : static int
152 0 : gossip_enabled( fd_topo_tile_t const * tile ) {
153 0 : return tile->snapct.sources.gossip.allow_any || tile->snapct.sources.gossip.allow_list_cnt>0UL;
154 0 : }
155 :
156 : static int
157 0 : download_enabled( fd_topo_tile_t const * tile ) {
158 0 : return gossip_enabled( tile ) || tile->snapct.sources.servers_cnt>0UL;
159 0 : }
160 :
161 : FD_FN_CONST static inline ulong
162 0 : loose_footprint( fd_topo_tile_t const * tile ) {
163 0 : (void)tile;
164 : /* Leftover space for OpenSSL allocations */
165 0 : return 1<<26UL; /* 64 MiB */
166 0 : }
167 :
168 : static ulong
169 0 : scratch_align( void ) {
170 0 : return fd_ulong_max( alignof(fd_snapct_tile_t),
171 0 : fd_ulong_max( fd_ssping_align(),
172 0 : fd_ulong_max( alignof(gossip_ci_entry_t),
173 0 : fd_ulong_max( gossip_ci_map_align(),
174 0 : fd_ulong_max( fd_http_resolver_align(),
175 0 : fd_sspeer_selector_align() ) ) ) ) );
176 0 : }
177 :
178 : static ulong
179 0 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
180 0 : ulong l = FD_LAYOUT_INIT;
181 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
182 0 : l = FD_LAYOUT_APPEND( l, fd_ssping_align(), fd_ssping_footprint( TOTAL_PEERS_MAX ) );
183 0 : l = FD_LAYOUT_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX );
184 0 : l = FD_LAYOUT_APPEND( l, gossip_ci_map_align(), gossip_ci_map_footprint( gossip_ci_map_chain_cnt_est( GOSSIP_PEERS_MAX ) ) );
185 0 : l = FD_LAYOUT_APPEND( l, fd_http_resolver_align(), fd_http_resolver_footprint( SERVER_PEERS_MAX ) );
186 0 : l = FD_LAYOUT_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX ) );
187 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
188 0 : return FD_LAYOUT_FINI( l, scratch_align() );
189 0 : }
190 :
191 : static inline int
192 0 : should_shutdown( fd_snapct_tile_t * ctx ) {
193 0 : return ctx->state==FD_SNAPCT_STATE_SHUTDOWN;
194 0 : }
195 :
196 : static void
197 0 : metrics_write( fd_snapct_tile_t * ctx ) {
198 0 : FD_MGAUGE_SET( SNAPCT, FULL_BYTES_READ, ctx->metrics.full.bytes_read );
199 0 : FD_MGAUGE_SET( SNAPCT, FULL_BYTES_WRITTEN, ctx->metrics.full.bytes_written );
200 0 : FD_MGAUGE_SET( SNAPCT, FULL_BYTES_TOTAL, ctx->metrics.full.bytes_total );
201 0 : FD_MGAUGE_SET( SNAPCT, FULL_DOWNLOAD_RETRIES, ctx->metrics.full.num_retries );
202 :
203 0 : FD_MGAUGE_SET( SNAPCT, INCREMENTAL_BYTES_READ, ctx->metrics.incremental.bytes_read );
204 0 : FD_MGAUGE_SET( SNAPCT, INCREMENTAL_BYTES_WRITTEN, ctx->metrics.incremental.bytes_written );
205 0 : FD_MGAUGE_SET( SNAPCT, INCREMENTAL_BYTES_TOTAL, ctx->metrics.incremental.bytes_total );
206 0 : FD_MGAUGE_SET( SNAPCT, INCREMENTAL_DOWNLOAD_RETRIES, ctx->metrics.incremental.num_retries );
207 :
208 0 : FD_MGAUGE_SET( SNAPCT, PREDICTED_SLOT, ctx->predicted_incremental.slot );
209 :
210 0 : #if FD_HAS_OPENSSL
211 0 : FD_MCNT_SET( SNAPCT, SSL_ALLOC_ERRORS, fd_ossl_alloc_errors );
212 0 : #endif
213 :
214 0 : FD_MGAUGE_SET( SNAPCT, STATE, (ulong)ctx->state );
215 0 : }
216 :
217 : static void
218 : snapshot_path_gui_publish( fd_snapct_tile_t * ctx,
219 : fd_stem_context_t * stem,
220 : char const * path,
221 0 : int is_full ) {
222 : /* The messages below cannot be obtained directly from metrics. */
223 0 : fd_snapct_update_t * out = fd_chunk_to_laddr( ctx->out_gui.mem, ctx->out_gui.chunk );
224 0 : FD_TEST( fd_cstr_printf_check( out->read_path, PATH_MAX, NULL, "%s", path ) );
225 0 : out->is_download = 0;
226 0 : out->type = fd_int_if( is_full, FD_SNAPCT_SNAPSHOT_TYPE_FULL, FD_SNAPCT_SNAPSHOT_TYPE_INCREMENTAL );
227 0 : fd_stem_publish( stem, ctx->out_gui.idx, 0UL, ctx->out_gui.chunk, sizeof(fd_snapct_update_t) , 0UL, 0UL, 0UL );
228 0 : ctx->out_gui.chunk = fd_dcache_compact_next( ctx->out_gui.chunk, sizeof(fd_snapct_update_t), ctx->out_gui.chunk0, ctx->out_gui.wmark );
229 0 : }
230 :
231 : static void
232 0 : predict_incremental( fd_snapct_tile_t * ctx ) {
233 0 : if( FD_UNLIKELY( !ctx->config.incremental_snapshots ) ) return;
234 0 : if( FD_UNLIKELY( ctx->predicted_incremental.full_slot==ULONG_MAX ) ) return;
235 :
236 0 : fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
237 :
238 0 : if( FD_LIKELY( best.addr.l ) ) {
239 0 : if( FD_UNLIKELY( ctx->predicted_incremental.slot!=best.incr_slot ) ) {
240 0 : ctx->predicted_incremental.slot = best.incr_slot;
241 0 : ctx->predicted_incremental.pending = 1;
242 0 : }
243 0 : }
244 0 : }
245 :
246 : static void
247 : on_resolve( void * _ctx,
248 : fd_sspeer_key_t const * key,
249 : ulong full_slot,
250 : ulong incr_slot,
251 : uchar full_hash[ FD_HASH_FOOTPRINT ],
252 0 : uchar incr_hash[ FD_HASH_FOOTPRINT ] ) {
253 0 : fd_snapct_tile_t * ctx = (fd_snapct_tile_t *)_ctx;
254 :
255 0 : int status = fd_sspeer_selector_update_on_resolve( ctx->selector, key, full_slot, incr_slot, full_hash, incr_hash );
256 0 : if( FD_UNLIKELY( status<0 ) ) {
257 : /* The update may fail in normal operation, e.g. after a peer has
258 : been removed from the selector. The log level is set to a
259 : minimum accordingly. */
260 0 : if( FD_UNLIKELY( key==NULL ) ) {
261 0 : FD_LOG_DEBUG(( "selector update on resolve returned %d for NULL peer key", status ) );
262 0 : } else {
263 0 : if( FD_UNLIKELY( !key->is_url ) ) {
264 0 : FD_BASE58_ENCODE_32_BYTES( key->pubkey->key, pubkey_b58 );
265 0 : FD_LOG_DEBUG(( "selector update on resolve returned %d for peer with pubkey %s", status, pubkey_b58 ) );
266 0 : } else {
267 0 : FD_LOG_DEBUG(( "selector update on resolve returned %d for peer %s with addr " FD_IP4_ADDR_FMT ":%hu", status, key->url.hostname,
268 0 : FD_IP4_ADDR_FMT_ARGS( key->url.resolved_addr.addr ), fd_ushort_bswap( key->url.resolved_addr.port ) ));
269 0 : }
270 0 : }
271 0 : }
272 0 : fd_sspeer_selector_process_cluster_slot( ctx->selector, full_slot, incr_slot );
273 0 : predict_incremental( ctx );
274 0 : }
275 :
276 : static void
277 : on_ping( void * _ctx,
278 : fd_ip4_port_t addr,
279 0 : ulong latency ) {
280 0 : fd_snapct_tile_t * ctx = (fd_snapct_tile_t *)_ctx;
281 :
282 0 : ulong cnt = fd_sspeer_selector_update_on_ping( ctx->selector, addr, latency );
283 0 : if( FD_UNLIKELY( !cnt ) ) {
284 : /* The update may fail in normal operation, e.g. after a peer has
285 : been removed from the selector. The log level is set to a
286 : minimum accordingly. */
287 0 : FD_LOG_DEBUG(( "selector update on ping did not find address " FD_IP4_ADDR_FMT ":%hu",
288 0 : FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ) ));
289 0 : }
290 0 : predict_incremental( ctx );
291 0 : }
292 :
293 : static void
294 : on_snapshot_hash( fd_snapct_tile_t * ctx,
295 : fd_sspeer_key_t const * key,
296 : fd_ip4_port_t addr,
297 0 : fd_gossip_update_message_t const * msg ) {
298 0 : ulong full_slot = msg->snapshot_hashes->full_slot;
299 0 : ulong incr_slot = 0UL;
300 0 : uchar const * incr_hash = NULL;
301 :
302 0 : for( ulong i=0UL; i<msg->snapshot_hashes->incremental_len; i++ ) {
303 0 : if( FD_LIKELY( msg->snapshot_hashes->incremental[ i ].slot>incr_slot ) ) {
304 0 : incr_slot = msg->snapshot_hashes->incremental[ i ].slot;
305 0 : incr_hash = msg->snapshot_hashes->incremental[ i ].hash;
306 0 : }
307 0 : }
308 :
309 0 : if( FD_UNLIKELY( !addr.l ) ) {
310 : /* A peer that does not advertise an rpc_addr cannot be added to
311 : the selector: if previously added, remove it. The remove
312 : operation becomes a no-op if the peer is not found. */
313 0 : fd_sspeer_selector_remove( ctx->selector, key );
314 0 : return;
315 0 : }
316 0 : fd_sspeer_selector_add( ctx->selector, key, addr, ULONG_MAX, full_slot, incr_slot, msg->snapshot_hashes->full_hash, incr_hash );
317 0 : fd_sspeer_selector_process_cluster_slot( ctx->selector, full_slot, incr_slot );
318 0 : predict_incremental( ctx );
319 0 : }
320 :
321 : static void
322 : send_expected_slot( fd_snapct_tile_t * ctx,
323 : fd_stem_context_t * stem,
324 0 : ulong slot ) {
325 0 : uint tsorig; uint tspub;
326 0 : fd_ssmsg_slot_to_frag( slot, &tsorig, &tspub );
327 0 : fd_stem_publish( stem, ctx->out_rp.idx, FD_SSMSG_EXPECTED_SLOT, 0UL, 0UL, 0UL, tsorig, tspub );
328 0 : }
329 :
330 : static void
331 0 : rename_full_snapshot( fd_snapct_tile_t * ctx ) {
332 0 : FD_TEST( -1!=ctx->local_out.dir_fd );
333 :
334 0 : if( FD_LIKELY( -1!=ctx->local_out.full_snapshot_fd && ctx->http_full_snapshot_name[ 0 ]!='\0' ) ) {
335 0 : if( FD_UNLIKELY( -1==renameat( ctx->local_out.dir_fd, TEMP_FULL_SNAP_NAME, ctx->local_out.dir_fd, ctx->http_full_snapshot_name ) ) )
336 0 : FD_LOG_ERR(( "renameat() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
337 0 : }
338 0 : }
339 :
340 : static void
341 0 : rename_incr_snapshot( fd_snapct_tile_t * ctx ) {
342 0 : FD_TEST( -1!=ctx->local_out.dir_fd );
343 :
344 0 : if( FD_LIKELY( -1!=ctx->local_out.incremental_snapshot_fd && ctx->http_incr_snapshot_name[ 0 ]!='\0' ) ) {
345 0 : if( FD_UNLIKELY( -1==renameat( ctx->local_out.dir_fd, TEMP_INCR_SNAP_NAME, ctx->local_out.dir_fd, ctx->http_incr_snapshot_name ) ) )
346 0 : FD_LOG_ERR(( "renameat() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
347 0 : }
348 0 : }
349 :
350 : static ulong
351 : rlimit_file_cnt( fd_topo_t const * topo FD_PARAM_UNUSED,
352 0 : fd_topo_tile_t const * tile ) {
353 0 : ulong cnt = 1UL + /* stderr */
354 0 : 1UL; /* logfile */
355 0 : if( download_enabled( tile ) ) {
356 0 : cnt += 1UL + /* ssping socket */
357 0 : 2UL + /* dirfd + full snapshot download temp fd */
358 0 : tile->snapct.sources.servers_cnt; /* http resolver peer full sockets */
359 0 : if( tile->snapct.incremental_snapshots ) {
360 0 : cnt += 1UL + /* incr snapshot download temp fd */
361 0 : tile->snapct.sources.servers_cnt; /* http resolver peer incr sockets */
362 0 : }
363 0 : }
364 0 : return cnt;
365 0 : }
366 :
367 : static ulong
368 : populate_allowed_seccomp( fd_topo_t const * topo,
369 : fd_topo_tile_t const * tile,
370 : ulong out_cnt,
371 0 : struct sock_filter * out ) {
372 :
373 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
374 :
375 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
376 0 : fd_snapct_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
377 :
378 0 : int ping_fd = download_enabled( tile ) ? fd_ssping_get_sockfd( ctx->ssping ) : -1;
379 0 : populate_sock_filter_policy_fd_snapct_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)ctx->local_out.dir_fd, (uint)ctx->local_out.full_snapshot_fd, (uint)ctx->local_out.incremental_snapshot_fd, (uint)ping_fd );
380 0 : return sock_filter_policy_fd_snapct_tile_instr_cnt;
381 0 : }
382 :
383 : static ulong
384 : populate_allowed_fds( fd_topo_t const * topo,
385 : fd_topo_tile_t const * tile,
386 : ulong out_fds_cnt,
387 0 : int * out_fds ) {
388 0 : if( FD_UNLIKELY( out_fds_cnt<6UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
389 :
390 0 : ulong out_cnt = 0;
391 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
392 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
393 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
394 0 : }
395 :
396 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
397 :
398 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
399 0 : fd_snapct_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
400 0 : if( FD_LIKELY( -1!=ctx->local_out.dir_fd ) ) out_fds[ out_cnt++ ] = ctx->local_out.dir_fd;
401 0 : if( FD_LIKELY( -1!=ctx->local_out.full_snapshot_fd ) ) out_fds[ out_cnt++ ] = ctx->local_out.full_snapshot_fd;
402 0 : if( FD_LIKELY( -1!=ctx->local_out.incremental_snapshot_fd ) ) out_fds[ out_cnt++ ] = ctx->local_out.incremental_snapshot_fd;
403 0 : if( FD_LIKELY( download_enabled( tile ) ) ) out_fds[ out_cnt++ ] = fd_ssping_get_sockfd( ctx->ssping );
404 :
405 0 : return out_cnt;
406 0 : }
407 :
408 : static void
409 : init_load( fd_snapct_tile_t * ctx,
410 : fd_stem_context_t * stem,
411 : int full,
412 0 : int file ) {
413 0 : ctx->snapshot_start_timestamp_ns = fd_log_wallclock();
414 0 : fd_ssctrl_init_t * out = fd_chunk_to_laddr( ctx->out_ld.mem, ctx->out_ld.chunk );
415 0 : out->file = file;
416 0 : out->zstd = !file || (full ? ctx->local_in.full_snapshot_zstd : ctx->local_in.incremental_snapshot_zstd);
417 0 : if( file ) {
418 0 : out->slot = full ? ctx->local_in.full_snapshot_slot : ctx->local_in.incremental_snapshot_slot;
419 0 : if( full ) fd_memcpy( out->snapshot_hash, ctx->local_in.full_snapshot_hash, FD_HASH_FOOTPRINT );
420 0 : else fd_memcpy( out->snapshot_hash, ctx->local_in.incremental_snapshot_hash, FD_HASH_FOOTPRINT );
421 0 : } else {
422 0 : out->slot = full ? ctx->predicted_incremental.full_slot : ctx->predicted_incremental.slot;
423 0 : if( full ) fd_memcpy( out->snapshot_hash, ctx->peer.full_hash, FD_HASH_FOOTPRINT );
424 0 : else fd_memcpy( out->snapshot_hash, ctx->peer.incr_hash, FD_HASH_FOOTPRINT );
425 0 : }
426 :
427 0 : if( !file ) {
428 0 : out->addr = ctx->peer.addr;
429 0 : char encoded_hash[ FD_BASE58_ENCODED_32_SZ ];
430 0 : if( full ) {
431 0 : fd_base58_encode_32( ctx->peer.full_hash, NULL, encoded_hash );
432 0 : FD_TEST( fd_cstr_printf_check( out->path, PATH_MAX, &out->path_len, "/snapshot-%lu-%s.tar.zst", ctx->peer.full_slot, encoded_hash ) );
433 0 : FD_TEST( fd_cstr_printf_check( ctx->http_full_snapshot_name, PATH_MAX, NULL, "snapshot-%lu-%s.tar.zst", ctx->peer.full_slot, encoded_hash ) );
434 0 : } else {
435 0 : fd_base58_encode_32( ctx->peer.incr_hash, NULL, encoded_hash );
436 0 : FD_TEST( fd_cstr_printf_check( out->path, PATH_MAX, &out->path_len, "/incremental-snapshot-%lu-%lu-%s.tar.zst", ctx->peer.full_slot, ctx->peer.incr_slot, encoded_hash ) );
437 0 : FD_TEST( fd_cstr_printf_check( ctx->http_incr_snapshot_name, PATH_MAX, NULL, "incremental-snapshot-%lu-%lu-%s.tar.zst", ctx->peer.full_slot, ctx->peer.incr_slot, encoded_hash ) );
438 0 : }
439 :
440 0 : for( ulong i=0UL; i<SERVER_PEERS_MAX; i++ ) {
441 0 : if( FD_UNLIKELY( ctx->peer.addr.l==ctx->config.sources.servers[ i ].addr.l ) ) {
442 0 : fd_cstr_ncpy( out->hostname, ctx->config.sources.servers[ i ].hostname, sizeof(out->hostname) );
443 0 : out->is_https = ctx->config.sources.servers[ i ].is_https;
444 0 : break;
445 0 : }
446 0 : }
447 0 : }
448 0 : fd_stem_publish( stem, ctx->out_ld.idx, full ? FD_SNAPSHOT_MSG_CTRL_INIT_FULL : FD_SNAPSHOT_MSG_CTRL_INIT_INCR, ctx->out_ld.chunk, sizeof(fd_ssctrl_init_t), 0UL, 0UL, 0UL );
449 0 : ctx->out_ld.chunk = fd_dcache_compact_next( ctx->out_ld.chunk, sizeof(fd_ssctrl_init_t), ctx->out_ld.chunk0, ctx->out_ld.wmark );
450 0 : ctx->flush_ack = 0;
451 :
452 : /* If we are downloading the snapshot, we will get the snapshot size
453 : in bytes from a metadata message sent from snapld. */
454 0 : if( file ) {
455 0 : if( full ) ctx->metrics.full.bytes_total = ctx->local_in.full_snapshot_size;
456 0 : else ctx->metrics.incremental.bytes_total = ctx->local_in.incremental_snapshot_size;
457 0 : }
458 :
459 0 : if( !file ) {
460 0 : if( full ) {
461 : /* reset any written content in the full output snapshot */
462 0 : if( FD_UNLIKELY( -1==ftruncate( ctx->local_out.full_snapshot_fd, 0UL ) ) ) {
463 0 : FD_LOG_ERR(( "ftruncate(%s) failed (%i-%s)", ctx->http_full_snapshot_name, errno, fd_io_strerror( errno ) ));
464 0 : }
465 0 : if( FD_UNLIKELY( -1==lseek( ctx->local_out.full_snapshot_fd, 0L, SEEK_SET ) ) ) {
466 0 : FD_LOG_ERR(( "lseek(%s) failed (%i-%s)", ctx->http_full_snapshot_name, errno, fd_io_strerror( errno ) ));
467 0 : }
468 0 : } else {
469 : /* reset any written content in the incremental snapshot output
470 : file */
471 0 : if( FD_UNLIKELY( -1==ftruncate( ctx->local_out.incremental_snapshot_fd, 0UL ) ) ) {
472 0 : FD_LOG_ERR(( "ftruncate(%s) failed (%i-%s)", ctx->http_incr_snapshot_name, errno, fd_io_strerror( errno ) ));
473 0 : }
474 0 : if( FD_UNLIKELY( -1==lseek( ctx->local_out.incremental_snapshot_fd, 0L, SEEK_SET ) ) ) {
475 0 : FD_LOG_ERR(( "lseek(%s) failed (%i-%s)", ctx->http_incr_snapshot_name, errno, fd_io_strerror( errno ) ));
476 0 : }
477 0 : }
478 0 : }
479 :
480 : /* Regardless of whether we load the snapshot from a file or download
481 : it, we know the name of the snapshot and can publish it to the gui
482 : here. */
483 0 : if( full ) {
484 0 : if( FD_LIKELY( !!ctx->out_gui.mem ) ) {
485 0 : if( file ) {
486 0 : fd_cstr_fini( ctx->http_full_snapshot_name );
487 0 : snapshot_path_gui_publish( ctx, stem, ctx->local_in.full_snapshot_path, 1 );
488 0 : }
489 0 : else {
490 0 : char snapshot_path[ PATH_MAX+30UL ]; /* 30 is fd_cstr_nlen( "https://255.255.255.255:65536/", ULONG_MAX ) */
491 0 : FD_TEST( fd_cstr_printf_check( snapshot_path, sizeof(snapshot_path), NULL, "http://" FD_IP4_ADDR_FMT ":%hu/%s", FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_full_snapshot_name ) );
492 0 : snapshot_path_gui_publish( ctx, stem, snapshot_path, 1 );
493 0 : }
494 0 : }
495 0 : } else {
496 0 : if( FD_LIKELY( !!ctx->out_gui.mem ) ) {
497 0 : if( file ) {
498 0 : fd_cstr_fini( ctx->http_incr_snapshot_name );
499 0 : snapshot_path_gui_publish( ctx, stem, ctx->local_in.incremental_snapshot_path, 0 );
500 0 : } else {
501 0 : char snapshot_path[ PATH_MAX+30UL ]; /* 30 is fd_cstr_nlen( "https://255.255.255.255:65536/", ULONG_MAX ) */
502 0 : FD_TEST( fd_cstr_printf_check( snapshot_path, sizeof(snapshot_path), NULL, "http://" FD_IP4_ADDR_FMT ":%hu/%s", FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_incr_snapshot_name ) );
503 0 : snapshot_path_gui_publish( ctx, stem, snapshot_path, 0 );
504 0 : }
505 0 : }
506 0 : }
507 0 : }
508 :
509 : static void
510 : log_download( fd_snapct_tile_t * ctx,
511 : int full,
512 : fd_ip4_port_t addr,
513 0 : ulong slot ) {
514 0 : for( gossip_ci_map_iter_t iter = gossip_ci_map_iter_init( ctx->gossip.ci_map, ctx->gossip.ci_table );
515 0 : !gossip_ci_map_iter_done( iter, ctx->gossip.ci_map, ctx->gossip.ci_table );
516 0 : iter = gossip_ci_map_iter_next( iter, ctx->gossip.ci_map, ctx->gossip.ci_table ) ) {
517 0 : gossip_ci_entry_t const * ci_entry = gossip_ci_map_iter_ele_const( iter, ctx->gossip.ci_map, ctx->gossip.ci_table );
518 0 : if( ci_entry->rpc_addr.l==addr.l ) {
519 0 : FD_TEST( ci_entry->allowed );
520 0 : FD_BASE58_ENCODE_32_BYTES( ci_entry->pubkey.uc, pubkey_b58 );
521 0 : FD_LOG_NOTICE(( "downloading %s snapshot at slot %lu from allowed gossip peer %s at http://" FD_IP4_ADDR_FMT ":%hu/%s",
522 0 : full ? "full" : "incremental", slot, pubkey_b58,
523 0 : FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ),
524 0 : full ? ctx->http_full_snapshot_name : ctx->http_incr_snapshot_name ));
525 0 : return;
526 0 : }
527 0 : }
528 :
529 0 : for( ulong i=0UL; i<ctx->config.sources.servers_cnt; i++ ) {
530 0 : if( addr.l==ctx->config.sources.servers[ i ].addr.l ) {
531 0 : if( ctx->config.sources.servers[ i ].is_https ) {
532 0 : FD_LOG_NOTICE(( "downloading %s snapshot at slot %lu from configured server with index %lu at https://%s:%hu/%s",
533 0 : full ? "full" : "incremental", slot, i,
534 0 : ctx->config.sources.servers[ i ].hostname, fd_ushort_bswap( addr.port ),
535 0 : full ? ctx->http_full_snapshot_name : ctx->http_incr_snapshot_name ));
536 0 : } else {
537 0 : FD_LOG_NOTICE(( "downloading %s snapshot at slot %lu from configured server with index %lu at http://" FD_IP4_ADDR_FMT ":%hu/%s",
538 0 : full ? "full" : "incremental", slot, i,
539 0 : FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ),
540 0 : full ? ctx->http_full_snapshot_name : ctx->http_incr_snapshot_name ));
541 0 : }
542 0 : return;
543 0 : }
544 0 : }
545 :
546 0 : FD_TEST( 0 ); /* should not be possible */
547 0 : }
548 :
549 : static void
550 : log_completion( fd_snapct_tile_t * ctx,
551 0 : int full ) {
552 0 : double elapsed = (double)(fd_log_wallclock() - ctx->snapshot_start_timestamp_ns) / 1e9;
553 0 : FD_LOG_NOTICE(( "%s snapshot load completed in %.3f seconds", full ? "full" : "incremental", elapsed ));
554 0 : }
555 :
556 : static void
557 : after_credit( fd_snapct_tile_t * ctx,
558 : fd_stem_context_t * stem,
559 : int * opt_poll_in FD_PARAM_UNUSED,
560 0 : int * charge_busy FD_PARAM_UNUSED ) {
561 0 : long now = fd_log_wallclock();
562 :
563 0 : if( FD_LIKELY( ctx->ssping ) ) fd_ssping_advance( ctx->ssping, now, ctx->selector );
564 0 : if( FD_LIKELY( ctx->ssresolver ) ) fd_http_resolver_advance( ctx->ssresolver, now, ctx->selector );
565 :
566 : /* send an expected slot message as the predicted incremental
567 : could have changed as a result of the pinger, resolver, or from
568 : processing gossip frags in gossip_frag. */
569 0 : if( FD_LIKELY( ctx->predicted_incremental.pending ) ) {
570 0 : send_expected_slot( ctx, stem, ctx->predicted_incremental.slot );
571 0 : ctx->predicted_incremental.pending = 0;
572 0 : }
573 :
574 : /* Note: All state transitions should occur within this switch
575 : statement to make it easier to reason about the state management. */
576 :
577 0 : switch ( ctx->state ) {
578 :
579 : /* ============================================================== */
580 0 : case FD_SNAPCT_STATE_INIT: {
581 0 : if( FD_UNLIKELY( !ctx->download_enabled ) ) {
582 0 : ulong local_slot = ctx->config.incremental_snapshots ? ctx->local_in.incremental_snapshot_slot : ctx->local_in.full_snapshot_slot;
583 0 : send_expected_slot( ctx, stem, local_slot );
584 0 : FD_LOG_NOTICE(( "reading full snapshot at slot %lu from local file `%s`", ctx->local_in.full_snapshot_slot, ctx->local_in.full_snapshot_path ));
585 0 : ctx->predicted_incremental.full_slot = ctx->local_in.full_snapshot_slot;
586 0 : ctx->state = FD_SNAPCT_STATE_READING_FULL_FILE;
587 0 : init_load( ctx, stem, 1, 1 );
588 0 : break;
589 0 : }
590 0 : ctx->deadline_nanos = now+FD_SNAPCT_WAITING_FOR_PEERS_TIMEOUT;
591 0 : ctx->state = FD_SNAPCT_STATE_WAITING_FOR_PEERS;
592 0 : break;
593 0 : }
594 :
595 : /* ============================================================== */
596 0 : case FD_SNAPCT_STATE_WAITING_FOR_PEERS: {
597 0 : if( FD_UNLIKELY( now>ctx->deadline_nanos ) ) FD_LOG_ERR(( "timed out waiting for peers." ));
598 :
599 0 : fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 0, ULONG_MAX );
600 0 : if( FD_LIKELY( best.addr.l ) ) {
601 0 : ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS;
602 0 : ctx->deadline_nanos = now+FD_SNAPCT_COLLECTING_PEERS_TIMEOUT;
603 0 : }
604 0 : break;
605 0 : }
606 :
607 : /* ============================================================== */
608 0 : case FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL: {
609 0 : if( FD_UNLIKELY( now>ctx->deadline_nanos ) ) FD_LOG_ERR(( "timed out waiting for incremental snapshot peers." ));
610 :
611 0 : FD_TEST( ctx->predicted_incremental.full_slot!=ULONG_MAX );
612 0 : fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
613 0 : if( FD_LIKELY( best.addr.l ) ) {
614 0 : ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
615 0 : ctx->deadline_nanos = now;
616 0 : }
617 0 : break;
618 0 : }
619 :
620 : /* ============================================================== */
621 0 : case FD_SNAPCT_STATE_COLLECTING_PEERS: {
622 0 : if( FD_UNLIKELY( !ctx->gossip.saturated && now<ctx->deadline_nanos ) ) break;
623 :
624 0 : fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 0, ULONG_MAX );
625 0 : if( FD_UNLIKELY( !best.addr.l ) ) {
626 0 : if( !ctx->gossip_enabled ) {
627 0 : FD_LOG_ERR(( "no peers are available and discovery of new peers via gossip is disabled. aborting." ));
628 0 : }
629 0 : ctx->deadline_nanos = now + FD_SNAPCT_WAITING_FOR_PEERS_TIMEOUT;
630 0 : ctx->state = FD_SNAPCT_STATE_WAITING_FOR_PEERS;
631 0 : break;
632 0 : }
633 :
634 0 : fd_sscluster_slot_t cluster = fd_sspeer_selector_cluster_slot( ctx->selector );
635 0 : if( FD_UNLIKELY( cluster.incremental==ULONG_MAX && ctx->config.incremental_snapshots ) ) {
636 : /* We must have a cluster full slot to be in this state. */
637 0 : FD_TEST( cluster.full!=ULONG_MAX );
638 : /* fall back to full snapshot only if the highest cluster slot
639 : is a full snapshot only */
640 0 : FD_LOG_WARNING(( "incremental snapshots were enabled via [snapshots.incremental_snapshots], but no incremental snapshot is available in the cluster. "
641 0 : "falling back to full snapshots only." ));
642 0 : ctx->config.incremental_snapshots = 0;
643 0 : }
644 :
645 0 : ulong cluster_slot = ctx->config.incremental_snapshots ? cluster.incremental : cluster.full;
646 0 : ulong local_slot = ctx->config.incremental_snapshots ? ctx->local_in.incremental_snapshot_slot : ctx->local_in.full_snapshot_slot;
647 0 : ulong local_slot_with_download = local_slot;
648 0 : int local_too_old = local_slot!=ULONG_MAX && ctx->local_in.full_snapshot_slot!=ULONG_MAX && local_slot<fd_ulong_sat_sub( cluster_slot, ctx->config.sources.max_local_incremental_age );
649 0 : int local_full_only = ctx->local_in.incremental_snapshot_slot==ULONG_MAX && ctx->local_in.full_snapshot_slot!=ULONG_MAX;
650 0 : if( FD_LIKELY( (ctx->config.incremental_snapshots && local_full_only) || local_too_old ) ) {
651 0 : fd_sspeer_t best_incremental = fd_sspeer_selector_best( ctx->selector, 1, ctx->local_in.full_snapshot_slot );
652 0 : if( FD_LIKELY( best_incremental.addr.l ) ) {
653 0 : ctx->predicted_incremental.slot = best_incremental.incr_slot;
654 0 : local_slot_with_download = best_incremental.incr_slot;
655 0 : ctx->local_in.incremental_snapshot_slot = ULONG_MAX; /* don't use the local incremental snapshot */
656 0 : }
657 0 : }
658 :
659 0 : int can_use_local_full = local_slot_with_download!=ULONG_MAX && ctx->local_in.full_snapshot_slot!=ULONG_MAX &&
660 0 : local_slot_with_download>=fd_ulong_sat_sub( cluster_slot, ctx->config.sources.max_local_full_effective_age );
661 0 : if( FD_LIKELY( can_use_local_full ) ) {
662 0 : send_expected_slot( ctx, stem, local_slot_with_download );
663 :
664 0 : FD_LOG_NOTICE(( "reading full snapshot at slot %lu with cluster slot %lu from local file `%s`",
665 0 : ctx->local_in.full_snapshot_slot, cluster_slot, ctx->local_in.full_snapshot_path ));
666 0 : ctx->predicted_incremental.full_slot = ctx->local_in.full_snapshot_slot;
667 0 : ctx->state = FD_SNAPCT_STATE_READING_FULL_FILE;
668 0 : init_load( ctx, stem, 1, 1 );
669 0 : } else {
670 0 : if( FD_LIKELY( ctx->local_in.full_snapshot_slot!=ULONG_MAX ) ) {
671 0 : FD_LOG_NOTICE(( "local snapshot at slot %lu is too old for cluster slot %lu max age %u, downloading instead",
672 0 : local_slot, cluster_slot, ctx->config.sources.max_local_full_effective_age ));
673 0 : } else {
674 0 : FD_LOG_NOTICE(( "no local snapshot available, downloading from peer" ));
675 0 : }
676 :
677 0 : if( FD_UNLIKELY( !ctx->config.incremental_snapshots ) ) send_expected_slot( ctx, stem, best.full_slot );
678 :
679 0 : fd_sspeer_t best_incremental = fd_sspeer_selector_best( ctx->selector, 1, best.full_slot );
680 0 : if( FD_LIKELY( best_incremental.addr.l ) ) {
681 0 : ctx->predicted_incremental.slot = best_incremental.incr_slot;
682 0 : send_expected_slot( ctx, stem, best_incremental.incr_slot );
683 0 : }
684 :
685 0 : ctx->peer = best;
686 0 : ctx->state = FD_SNAPCT_STATE_READING_FULL_HTTP;
687 0 : ctx->predicted_incremental.full_slot = best.full_slot;
688 0 : init_load( ctx, stem, 1, 0 );
689 0 : log_download( ctx, 1, best.addr, best.full_slot );
690 0 : }
691 0 : break;
692 0 : }
693 :
694 : /* ============================================================== */
695 0 : case FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL: {
696 0 : if( FD_UNLIKELY( now<ctx->deadline_nanos ) ) break;
697 :
698 0 : fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
699 0 : if( FD_UNLIKELY( !best.addr.l ) ) {
700 0 : if( !ctx->gossip_enabled ) {
701 0 : FD_LOG_ERR(( "no incremental snapshot peers are available and discovery of new peers via gossip is disabled. aborting." ));
702 0 : }
703 0 : ctx->deadline_nanos = now + FD_SNAPCT_WAITING_FOR_PEERS_TIMEOUT;
704 0 : ctx->state = FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL;
705 0 : break;
706 0 : }
707 :
708 : /* decide whether to use the local incremental snapshot if one
709 : exists and is not too old, otherwise download a new incremental
710 : snapshot. */
711 0 : ulong cluster_slot = fd_sspeer_selector_cluster_slot( ctx->selector ).incremental;
712 0 : ulong local_slot = ctx->local_in.incremental_snapshot_slot;
713 0 : int local_too_old = local_slot<fd_ulong_sat_sub( cluster_slot, ctx->config.sources.max_local_incremental_age );
714 0 : if( FD_LIKELY( local_slot!=ULONG_MAX && !local_too_old ) ) {
715 0 : ctx->predicted_incremental.slot = local_slot;
716 0 : send_expected_slot( ctx, stem, local_slot );
717 :
718 0 : FD_LOG_NOTICE(( "reading incremental snapshot at slot %lu from local file `%s`", ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
719 0 : ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_FILE;
720 0 : init_load( ctx, stem, 0, 1 );
721 0 : } else {
722 0 : ctx->predicted_incremental.slot = best.incr_slot;
723 0 : send_expected_slot( ctx, stem, best.incr_slot );
724 :
725 0 : ctx->peer = best;
726 0 : ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP;
727 0 : init_load( ctx, stem, 0, 0 );
728 0 : log_download( ctx, 0, best.addr, best.incr_slot );
729 0 : }
730 0 : break;
731 0 : }
732 :
733 : /* ============================================================== */
734 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI:
735 0 : if( !ctx->flush_ack ) break;
736 :
737 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
738 0 : ctx->malformed = 0;
739 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
740 0 : ctx->flush_ack = 0;
741 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET;
742 0 : FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from local file `%s`",
743 0 : ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
744 0 : break;
745 0 : }
746 :
747 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE;
748 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_DONE, 0UL, 0UL, 0UL, 0UL, 0UL );
749 0 : ctx->flush_ack = 0;
750 0 : break;
751 :
752 : /* ============================================================== */
753 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE:
754 0 : if( !ctx->flush_ack ) break;
755 :
756 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
757 0 : ctx->malformed = 0;
758 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
759 0 : ctx->flush_ack = 0;
760 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET;
761 0 : FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from local file `%s`",
762 0 : ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
763 0 : break;
764 0 : }
765 :
766 0 : log_completion( ctx, 0/*incr*/ );
767 0 : ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
768 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
769 0 : break;
770 :
771 : /* ============================================================== */
772 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI:
773 0 : if( !ctx->flush_ack ) break;
774 :
775 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
776 0 : ctx->malformed = 0;
777 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
778 0 : ctx->flush_ack = 0;
779 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET;
780 0 : FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
781 0 : "blacklisting peer due to download failure.",
782 0 : ctx->predicted_incremental.slot,
783 0 : FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_incr_snapshot_name ));
784 0 : fd_ssping_invalidate( ctx->ssping, ctx->peer.addr, fd_log_wallclock() );
785 0 : fd_sspeer_selector_remove_by_addr( ctx->selector, ctx->peer.addr );
786 0 : break;
787 0 : }
788 :
789 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE;
790 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_DONE, 0UL, 0UL, 0UL, 0UL, 0UL );
791 0 : ctx->flush_ack = 0;
792 0 : break;
793 :
794 : /* ============================================================== */
795 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE:
796 0 : if( !ctx->flush_ack ) break;
797 :
798 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
799 0 : ctx->malformed = 0;
800 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
801 0 : ctx->flush_ack = 0;
802 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET;
803 0 : FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
804 0 : "blacklisting peer due to download failure.",
805 0 : ctx->predicted_incremental.slot,
806 0 : FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_incr_snapshot_name ));
807 0 : fd_ssping_invalidate( ctx->ssping, ctx->peer.addr, fd_log_wallclock() );
808 0 : fd_sspeer_selector_remove_by_addr( ctx->selector, ctx->peer.addr );
809 0 : break;
810 0 : }
811 :
812 0 : log_completion( ctx, 0/*incr*/ );
813 0 : ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
814 0 : rename_incr_snapshot( ctx );
815 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
816 0 : break;
817 :
818 : /* ============================================================== */
819 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI:
820 0 : if( !ctx->flush_ack ) break;
821 :
822 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
823 0 : ctx->malformed = 0;
824 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
825 0 : ctx->flush_ack = 0;
826 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET;
827 0 : FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from local file `%s`",
828 0 : ctx->local_in.full_snapshot_slot, ctx->local_in.full_snapshot_path ));
829 0 : break;
830 0 : }
831 :
832 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE;
833 0 : ulong sig = ctx->config.incremental_snapshots &&
834 0 : (ctx->local_in.incremental_snapshot_slot!=ULONG_MAX || ctx->download_enabled) ? FD_SNAPSHOT_MSG_CTRL_NEXT : FD_SNAPSHOT_MSG_CTRL_DONE;
835 0 : if( sig==FD_SNAPSHOT_MSG_CTRL_DONE && ctx->config.incremental_snapshots ) {
836 : /* set incremental snapshots to 0 if there is no local
837 : incremental snapshot and download is not enabled. */
838 0 : FD_LOG_WARNING(( "incremental snapshots were enabled via [snapshots.incremental_snapshots] "
839 0 : "but no incremental snapshot exists on disk and no snapshot peers are configured. "
840 0 : "skipping incremental snapshot load." ));
841 0 : ctx->config.incremental_snapshots = 0;
842 0 : }
843 0 : fd_stem_publish( stem, ctx->out_ld.idx, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
844 0 : ctx->flush_ack = 0;
845 0 : break;
846 :
847 : /* ============================================================== */
848 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE:
849 0 : if( !ctx->flush_ack ) break;
850 :
851 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
852 0 : ctx->malformed = 0;
853 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
854 0 : ctx->flush_ack = 0;
855 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET;
856 0 : FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from local file `%s`",
857 0 : ctx->local_in.full_snapshot_slot, ctx->local_in.full_snapshot_path ));
858 0 : break;
859 0 : }
860 :
861 0 : log_completion( ctx, 1/*full*/ );
862 0 : if( FD_LIKELY( !ctx->config.incremental_snapshots ) ) {
863 0 : ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
864 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
865 0 : break;
866 0 : }
867 :
868 0 : if( FD_LIKELY( ctx->download_enabled ) ) {
869 0 : ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
870 0 : ctx->deadline_nanos = 0L;
871 0 : } else {
872 0 : FD_LOG_NOTICE(( "reading incremental snapshot at slot %lu from local file `%s`", ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
873 0 : ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_FILE;
874 0 : init_load( ctx, stem, 0, 1 );
875 0 : }
876 0 : break;
877 :
878 : /* ============================================================== */
879 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI:
880 0 : if( !ctx->flush_ack ) break;
881 :
882 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
883 0 : ctx->malformed = 0;
884 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
885 0 : ctx->flush_ack = 0;
886 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
887 0 : FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
888 0 : "blacklisting peer due to download failure.",
889 0 : ctx->predicted_incremental.full_slot,
890 0 : FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_full_snapshot_name ));
891 0 : fd_ssping_invalidate( ctx->ssping, ctx->peer.addr, fd_log_wallclock() );
892 0 : fd_sspeer_selector_remove_by_addr( ctx->selector, ctx->peer.addr );
893 0 : break;
894 0 : }
895 :
896 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE;
897 0 : fd_stem_publish( stem, ctx->out_ld.idx, ctx->config.incremental_snapshots ? FD_SNAPSHOT_MSG_CTRL_NEXT : FD_SNAPSHOT_MSG_CTRL_DONE, 0UL, 0UL, 0UL, 0UL, 0UL );
898 0 : ctx->flush_ack = 0;
899 0 : break;
900 :
901 : /* ============================================================== */
902 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE:
903 0 : if( !ctx->flush_ack ) break;
904 :
905 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
906 0 : ctx->malformed = 0;
907 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
908 0 : ctx->flush_ack = 0;
909 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
910 0 : FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
911 0 : "blacklisting peer due to download failure.",
912 0 : ctx->predicted_incremental.full_slot,
913 0 : FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_full_snapshot_name ));
914 0 : fd_ssping_invalidate( ctx->ssping, ctx->peer.addr, fd_log_wallclock() );
915 0 : fd_sspeer_selector_remove_by_addr( ctx->selector, ctx->peer.addr );
916 0 : break;
917 0 : }
918 :
919 0 : rename_full_snapshot( ctx );
920 :
921 0 : log_completion( ctx, 1/*full*/ );
922 0 : if( FD_LIKELY( !ctx->config.incremental_snapshots ) ) {
923 0 : ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
924 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
925 0 : break;
926 0 : }
927 :
928 : /* Get the best incremental peer to download from */
929 0 : fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
930 0 : if( FD_UNLIKELY( !best.addr.l ) ) {
931 0 : ctx->deadline_nanos = now;
932 0 : ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
933 0 : break;
934 0 : }
935 :
936 0 : ctx->predicted_incremental.slot = best.incr_slot;
937 0 : send_expected_slot( ctx, stem, best.incr_slot );
938 :
939 0 : ctx->peer = best;
940 0 : ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP;
941 0 : init_load( ctx, stem, 0, 0 );
942 0 : log_download( ctx, 0, best.addr, best.incr_slot );
943 0 : break;
944 :
945 : /* ============================================================== */
946 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
947 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET:
948 0 : if( !ctx->flush_ack ) break;
949 :
950 0 : if( ctx->metrics.full.num_retries==ctx->config.max_retry_abort ) {
951 0 : FD_LOG_ERR(( "hit retry limit of %u for full snapshot, aborting", ctx->config.max_retry_abort ));
952 0 : }
953 :
954 0 : ctx->metrics.full.num_retries++;
955 0 : FD_LOG_NOTICE(( "retrying full snapshot download (attempt %u/%u)",
956 0 : ctx->metrics.full.num_retries, ctx->config.max_retry_abort ));
957 :
958 0 : ctx->metrics.full.bytes_read = 0UL;
959 0 : ctx->metrics.full.bytes_written = 0UL;
960 0 : ctx->metrics.full.bytes_total = 0UL;
961 :
962 0 : ctx->metrics.incremental.bytes_read = 0UL;
963 0 : ctx->metrics.incremental.bytes_written = 0UL;
964 0 : ctx->metrics.incremental.bytes_total = 0UL;
965 :
966 0 : if( !ctx->download_enabled ) {
967 : /* if we are unable to download new snapshots and unable to load
968 : our local snapshot, we must shutdown the validator. */
969 0 : FD_LOG_ERR(( "unable to load local snapshot %s and no snapshot peers were configured. aborting.", ctx->local_in.full_snapshot_path ));
970 0 : } else {
971 0 : if( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ) ctx->local_in.full_snapshot_slot = ULONG_MAX;
972 0 : ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS;
973 0 : ctx->deadline_nanos = 0L;
974 0 : }
975 0 : break;
976 :
977 : /* ============================================================== */
978 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET:
979 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
980 0 : if( !ctx->flush_ack ) break;
981 :
982 0 : if( ctx->metrics.incremental.num_retries==ctx->config.max_retry_abort ) {
983 0 : FD_LOG_ERR(("hit retry limit of %u for incremental snapshot. aborting", ctx->config.max_retry_abort ));
984 0 : }
985 :
986 0 : ctx->metrics.incremental.num_retries++;
987 0 : FD_LOG_NOTICE(( "retrying incremental snapshot download (attempt %u/%u)",
988 0 : ctx->metrics.incremental.num_retries, ctx->config.max_retry_abort ));
989 :
990 0 : ctx->metrics.incremental.bytes_read = 0UL;
991 0 : ctx->metrics.incremental.bytes_written = 0UL;
992 0 : ctx->metrics.incremental.bytes_total = 0UL;
993 :
994 0 : if( !ctx->download_enabled ) {
995 : /* if we are unable to download new snapshots and unable to load
996 : our local snapshot, we must shutdown the validator. */
997 0 : FD_LOG_ERR(( "unable to load local snapshot %s and no snapshot peers were configured. aborting.", ctx->local_in.incremental_snapshot_path ));
998 0 : } else {
999 0 : if( ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ctx->local_in.incremental_snapshot_slot = ULONG_MAX;
1000 0 : ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
1001 0 : ctx->deadline_nanos = 0L;
1002 0 : }
1003 0 : break;
1004 :
1005 : /* ============================================================== */
1006 0 : case FD_SNAPCT_STATE_READING_FULL_FILE:
1007 0 : if( FD_UNLIKELY( !ctx->flush_ack ) ) break;
1008 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
1009 0 : ctx->malformed = 0;
1010 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
1011 0 : ctx->flush_ack = 0;
1012 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET;
1013 0 : FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from local file `%s`",
1014 0 : ctx->local_in.full_snapshot_slot, ctx->local_in.full_snapshot_path ));
1015 0 : break;
1016 0 : }
1017 0 : FD_TEST( ctx->metrics.full.bytes_total!=0UL );
1018 0 : if( FD_UNLIKELY( ctx->metrics.full.bytes_read == ctx->metrics.full.bytes_total ) ) {
1019 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FINI, 0UL, 0UL, 0UL, 0UL, 0UL );
1020 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI;
1021 0 : ctx->flush_ack = 0;
1022 0 : }
1023 0 : break;
1024 :
1025 : /* ============================================================== */
1026 0 : case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE:
1027 0 : if( FD_UNLIKELY( !ctx->flush_ack ) ) break;
1028 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
1029 0 : ctx->malformed = 0;
1030 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
1031 0 : ctx->flush_ack = 0;
1032 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET;
1033 0 : FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from local file `%s`",
1034 0 : ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
1035 0 : break;
1036 0 : }
1037 0 : FD_TEST( ctx->metrics.incremental.bytes_total!=0UL );
1038 0 : if ( FD_UNLIKELY( ctx->metrics.incremental.bytes_read == ctx->metrics.incremental.bytes_total ) ) {
1039 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FINI, 0UL, 0UL, 0UL, 0UL, 0UL );
1040 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI;
1041 0 : ctx->flush_ack = 0;
1042 0 : }
1043 0 : break;
1044 :
1045 : /* ============================================================== */
1046 0 : case FD_SNAPCT_STATE_READING_FULL_HTTP:
1047 0 : if( FD_UNLIKELY( !ctx->flush_ack ) ) break;
1048 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
1049 0 : ctx->malformed = 0;
1050 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
1051 0 : ctx->flush_ack = 0;
1052 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
1053 0 : FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
1054 0 : "blacklisting peer due to download failure",
1055 0 : ctx->predicted_incremental.full_slot,
1056 0 : FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_full_snapshot_name ));
1057 0 : fd_ssping_invalidate( ctx->ssping, ctx->peer.addr, fd_log_wallclock() );
1058 0 : fd_sspeer_selector_remove_by_addr( ctx->selector, ctx->peer.addr );
1059 0 : break;
1060 0 : }
1061 0 : if( FD_UNLIKELY( ctx->metrics.full.bytes_total!=0UL && ctx->metrics.full.bytes_read==ctx->metrics.full.bytes_total ) ) {
1062 0 : ulong sig = FD_SNAPSHOT_MSG_CTRL_FINI;
1063 0 : fd_stem_publish( stem, ctx->out_ld.idx, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
1064 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI;
1065 0 : ctx->flush_ack = 0;
1066 0 : }
1067 0 : break;
1068 :
1069 : /* ============================================================== */
1070 0 : case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP:
1071 0 : if( FD_UNLIKELY( !ctx->flush_ack ) ) break;
1072 0 : if( FD_UNLIKELY( ctx->malformed ) ) {
1073 0 : ctx->malformed = 0;
1074 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
1075 0 : ctx->flush_ack = 0;
1076 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET;
1077 0 : FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
1078 0 : "blacklisting peer due to download failure",
1079 0 : ctx->predicted_incremental.slot,
1080 0 : FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_incr_snapshot_name ));
1081 0 : fd_ssping_invalidate( ctx->ssping, ctx->peer.addr, fd_log_wallclock() );
1082 0 : fd_sspeer_selector_remove_by_addr( ctx->selector, ctx->peer.addr );
1083 0 : break;
1084 0 : }
1085 0 : if ( FD_UNLIKELY( ctx->metrics.incremental.bytes_total!=0UL && ctx->metrics.incremental.bytes_read==ctx->metrics.incremental.bytes_total ) ) {
1086 0 : fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FINI, 0UL, 0UL, 0UL, 0UL, 0UL );
1087 0 : ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI;
1088 0 : ctx->flush_ack = 0;
1089 0 : }
1090 0 : break;
1091 :
1092 : /* ============================================================== */
1093 0 : case FD_SNAPCT_STATE_SHUTDOWN:
1094 : /* Transitioning to the shutdown state indicates snapshot load is
1095 : completed without errors. Otherwise, snapct would have aborted
1096 : earlier. */
1097 0 : break;
1098 :
1099 : /* ============================================================== */
1100 0 : default: FD_LOG_ERR(( "unexpected state %d", ctx->state ));
1101 0 : }
1102 0 : }
1103 :
1104 : static void
1105 : gossip_frag( fd_snapct_tile_t * ctx,
1106 : ulong sig,
1107 : ulong sz FD_PARAM_UNUSED,
1108 0 : ulong chunk ) {
1109 0 : FD_TEST( ctx->gossip_enabled );
1110 :
1111 0 : if( FD_UNLIKELY( sig==FD_GOSSIP_UPDATE_TAG_PEER_SATURATED ) ) {
1112 0 : FD_LOG_NOTICE(( "gossip peer discovery saturated" ));
1113 0 : ctx->gossip.saturated = 1;
1114 0 : return;
1115 0 : }
1116 :
1117 0 : if( !( sig==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ||
1118 0 : sig==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE ||
1119 0 : sig==FD_GOSSIP_UPDATE_TAG_SNAPSHOT_HASHES ) ) return;
1120 :
1121 0 : fd_gossip_update_message_t const * msg = fd_chunk_to_laddr_const( ctx->gossip_in_mem, chunk );
1122 0 : switch( msg->tag ) {
1123 0 : case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO: {
1124 0 : FD_TEST( msg->contact_info->idx<GOSSIP_PEERS_MAX );
1125 0 : fd_pubkey_t const * pubkey = (fd_pubkey_t const *)msg->origin;
1126 0 : gossip_ci_entry_t * entry = ctx->gossip.ci_table + msg->contact_info->idx;
1127 0 : if( FD_UNLIKELY( !fd_pubkey_eq( &entry->pubkey, pubkey ) ) ) {
1128 : /* Initialize the new gossip entry, which may or may not be allowed */
1129 0 : FD_TEST( fd_pubkey_check_zero( &entry->pubkey ) );
1130 0 : entry->pubkey = *pubkey;
1131 0 : entry->rpc_addr.l = 0UL;
1132 0 : if( ctx->config.sources.gossip.allow_any ) {
1133 0 : entry->allowed = 1;
1134 0 : for( ulong i=0UL; i<ctx->config.sources.gossip.block_list_cnt; i++ ) {
1135 0 : if( fd_pubkey_eq( pubkey, &ctx->config.sources.gossip.block_list[ i ] ) ) {
1136 0 : entry->allowed = 0;
1137 0 : break;
1138 0 : }
1139 0 : }
1140 0 : } else {
1141 0 : entry->allowed = 0;
1142 0 : for( ulong i=0UL; i<ctx->config.sources.gossip.allow_list_cnt; i++ ) {
1143 0 : if( fd_pubkey_eq( pubkey, &ctx->config.sources.gossip.allow_list[ i ] ) ) {
1144 0 : entry->allowed = 1;
1145 0 : break;
1146 0 : }
1147 0 : }
1148 0 : }
1149 0 : FD_TEST( ULONG_MAX==gossip_ci_map_idx_query_const( ctx->gossip.ci_map, pubkey, ULONG_MAX, ctx->gossip.ci_table ) );
1150 0 : if( entry->allowed ) {
1151 0 : gossip_ci_map_idx_insert( ctx->gossip.ci_map, msg->contact_info->idx, ctx->gossip.ci_table );
1152 0 : ctx->gossip.allowed_cnt++;
1153 : /* Allow-list shortcut: if an explicit allow list is
1154 : configured and all expected peers have arrived, declare
1155 : saturation immediately without waiting for the gossip
1156 : tile's general saturation signal. */
1157 0 : if( FD_UNLIKELY( !ctx->config.sources.gossip.allow_any &&
1158 0 : ctx->config.sources.gossip.allow_list_cnt>0UL &&
1159 0 : ctx->gossip.allowed_cnt==ctx->config.sources.gossip.allow_list_cnt ) ) {
1160 0 : FD_LOG_NOTICE(( "all %lu allowed gossip peers discovered", ctx->config.sources.gossip.allow_list_cnt ));
1161 0 : ctx->gossip.saturated = 1;
1162 0 : }
1163 0 : }
1164 0 : }
1165 0 : if( !entry->allowed ) break;
1166 : /* Maybe update the RPC address of a new or existing allowed gossip peer */
1167 0 : fd_ip4_port_t cur_addr = entry->rpc_addr;
1168 0 : fd_ip4_port_t new_addr;
1169 0 : new_addr.addr = msg->contact_info->value->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC ].is_ipv6 ? 0 : msg->contact_info->value->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC ].ip4;
1170 0 : new_addr.port = msg->contact_info->value->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC ].port;
1171 :
1172 0 : if( FD_UNLIKELY( new_addr.l!=cur_addr.l ) ) {
1173 0 : entry->rpc_addr = new_addr;
1174 0 : if( FD_LIKELY( !!cur_addr.l ) ) {
1175 0 : fd_ssping_remove( ctx->ssping, cur_addr );
1176 0 : }
1177 0 : fd_sspeer_key_t entry_key = {0};
1178 0 : *entry_key.pubkey = entry->pubkey;
1179 0 : entry_key.is_url = 0;
1180 0 : if( FD_LIKELY( !!new_addr.l ) ) {
1181 0 : fd_ssping_add( ctx->ssping, new_addr );
1182 : /* update address */
1183 0 : fd_sspeer_selector_add( ctx->selector, &entry_key, new_addr, ULONG_MAX, ULONG_MAX, ULONG_MAX, NULL, NULL );
1184 0 : } else {
1185 0 : fd_sspeer_selector_remove( ctx->selector, &entry_key );
1186 0 : }
1187 0 : if( !ctx->config.sources.gossip.allow_any ) {
1188 0 : FD_BASE58_ENCODE_32_BYTES( pubkey->uc, pubkey_b58 );
1189 0 : if( FD_LIKELY( !!new_addr.l ) ) {
1190 0 : FD_LOG_NOTICE(( "allowed gossip peer added with public key `%s` and RPC address `" FD_IP4_ADDR_FMT ":%hu`",
1191 0 : pubkey_b58, FD_IP4_ADDR_FMT_ARGS( new_addr.addr ), fd_ushort_bswap( new_addr.port ) ));
1192 0 : } else {
1193 0 : FD_LOG_WARNING(( "allowed gossip peer with public key `%s` does not advertise an RPC address", pubkey_b58 ));
1194 0 : }
1195 0 : }
1196 0 : }
1197 0 : break;
1198 0 : }
1199 0 : case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE: {
1200 0 : FD_TEST( msg->contact_info_remove->idx<GOSSIP_PEERS_MAX );
1201 0 : gossip_ci_entry_t * entry = ctx->gossip.ci_table + msg->contact_info_remove->idx;
1202 0 : ulong rem_idx = gossip_ci_map_idx_remove( ctx->gossip.ci_map, &entry->pubkey, ULONG_MAX, ctx->gossip.ci_table );
1203 0 : if( rem_idx==ULONG_MAX ) break;
1204 0 : FD_TEST( entry->allowed && rem_idx==msg->contact_info_remove->idx );
1205 0 : ctx->gossip.allowed_cnt--;
1206 0 : fd_ip4_port_t addr = entry->rpc_addr;
1207 0 : if( FD_LIKELY( !!addr.l ) ) {
1208 0 : fd_ssping_remove( ctx->ssping, addr );
1209 0 : fd_sspeer_key_t entry_key = {0};
1210 0 : *entry_key.pubkey = entry->pubkey;
1211 0 : entry_key.is_url = 0;
1212 0 : fd_sspeer_selector_remove( ctx->selector, &entry_key );
1213 0 : }
1214 0 : if( !ctx->config.sources.gossip.allow_any ) {
1215 0 : FD_BASE58_ENCODE_32_BYTES( entry->pubkey.uc, pubkey_b58 );
1216 0 : FD_LOG_WARNING(( "allowed gossip peer removed with public key `%s` and RPC address `" FD_IP4_ADDR_FMT ":%hu`",
1217 0 : pubkey_b58, FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ) ));
1218 0 : }
1219 0 : fd_memset( entry, 0, sizeof(*entry) );
1220 0 : break;
1221 0 : }
1222 0 : case FD_GOSSIP_UPDATE_TAG_SNAPSHOT_HASHES: {
1223 0 : ulong idx = gossip_ci_map_idx_query_const( ctx->gossip.ci_map, (fd_pubkey_t const *)msg->origin, ULONG_MAX, ctx->gossip.ci_table );
1224 0 : if( FD_LIKELY( idx!=ULONG_MAX ) ) {
1225 0 : gossip_ci_entry_t * entry = ctx->gossip.ci_table + idx;
1226 0 : FD_TEST( entry->allowed );
1227 0 : fd_sspeer_key_t entry_key = {0};
1228 0 : *entry_key.pubkey = entry->pubkey;
1229 0 : entry_key.is_url = 0;
1230 0 : on_snapshot_hash( ctx, &entry_key, entry->rpc_addr, msg );
1231 0 : }
1232 0 : break;
1233 0 : }
1234 0 : default:
1235 0 : FD_LOG_ERR(( "snapct: unexpected gossip tag %u", (uint)msg->tag ));
1236 0 : break;
1237 0 : }
1238 0 : }
1239 :
1240 : static void
1241 : snapld_frag( fd_snapct_tile_t * ctx,
1242 : ulong sig,
1243 : ulong sz,
1244 0 : ulong chunk ) {
1245 0 : if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_META ) ) {
1246 : /* Before snapld starts sending down data fragments, it first sends
1247 : a metadata message containing the total size of the snapshot as
1248 : well as the filename. This is only done for HTTP loading. */
1249 0 : int full;
1250 0 : switch( ctx->state ) {
1251 0 : case FD_SNAPCT_STATE_READING_FULL_HTTP: full = 1; break;
1252 0 : case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP: full = 0; break;
1253 :
1254 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
1255 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
1256 0 : return; /* Ignore */
1257 0 : default: FD_LOG_ERR(( "invalid meta frag in state %d", ctx->state ));
1258 0 : }
1259 :
1260 0 : FD_TEST( sz==sizeof(fd_ssctrl_meta_t) );
1261 0 : fd_ssctrl_meta_t const * meta = fd_chunk_to_laddr_const( ctx->snapld_in_mem, chunk );
1262 :
1263 0 : if( full ) ctx->metrics.full.bytes_total = meta->total_sz;
1264 0 : else ctx->metrics.incremental.bytes_total = meta->total_sz;
1265 :
1266 0 : return;
1267 0 : }
1268 0 : if( FD_UNLIKELY( sig!=FD_SNAPSHOT_MSG_DATA ) ) return;
1269 :
1270 0 : int full, file;
1271 0 : switch( ctx->state ) {
1272 : /* Expected cases, fall through below */
1273 0 : case FD_SNAPCT_STATE_READING_FULL_FILE: full = 1; file = 1; break;
1274 0 : case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE: full = 0; file = 1; break;
1275 0 : case FD_SNAPCT_STATE_READING_FULL_HTTP: full = 1; file = 0; break;
1276 0 : case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP: full = 0; file = 0; break;
1277 :
1278 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET:
1279 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET:
1280 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
1281 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
1282 : /* We are waiting for a reset to fully propagate through the
1283 : pipeline, just throw away any trailing data frags. */
1284 0 : return;
1285 :
1286 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI:
1287 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI:
1288 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI:
1289 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI:
1290 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE:
1291 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE:
1292 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE:
1293 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE:
1294 : /* Based on previously received data frags, we expected that the
1295 : current full / incremental snapshot was finished, but then we
1296 : received additional data frags. Unsafe to continue so throw
1297 : away the whole snapshot. */
1298 0 : if( !ctx->malformed ) {
1299 0 : ctx->malformed = 1;
1300 0 : FD_LOG_WARNING(( "complete snapshot loaded but read %lu extra bytes", sz ));
1301 0 : }
1302 0 : return;
1303 :
1304 0 : case FD_SNAPCT_STATE_WAITING_FOR_PEERS:
1305 0 : case FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL:
1306 0 : case FD_SNAPCT_STATE_COLLECTING_PEERS:
1307 0 : case FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL:
1308 0 : case FD_SNAPCT_STATE_SHUTDOWN:
1309 0 : default:
1310 0 : FD_LOG_ERR(( "invalid data frag in state %d", ctx->state ));
1311 0 : return;
1312 0 : }
1313 :
1314 0 : if( full ) FD_TEST( ctx->metrics.full.bytes_total !=0UL );
1315 0 : else FD_TEST( ctx->metrics.incremental.bytes_total!=0UL );
1316 :
1317 0 : if( full ) ctx->metrics.full.bytes_read += sz;
1318 0 : else ctx->metrics.incremental.bytes_read += sz;
1319 :
1320 0 : if( !file && -1!=ctx->local_out.dir_fd ) {
1321 0 : ulong written_sz = 0;
1322 0 : while( written_sz<sz ) {
1323 0 : uchar const * data = fd_chunk_to_laddr_const( ctx->snapld_in_mem, chunk );
1324 0 : int fd = full ? ctx->local_out.full_snapshot_fd : ctx->local_out.incremental_snapshot_fd;
1325 0 : long result = write( fd, data, sz );
1326 0 : if( FD_UNLIKELY( -1==result && errno==ENOSPC ) ) {
1327 0 : FD_LOG_ERR(( "Out of disk space when writing out snapshot data to `%s`", ctx->config.snapshots_path ));
1328 0 : } else if( FD_UNLIKELY( 0L>result ) ) {
1329 0 : FD_LOG_ERR(( "write() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1330 0 : }
1331 :
1332 0 : written_sz += (ulong)result;
1333 0 : }
1334 0 : if( full ) ctx->metrics.full.bytes_written += sz;
1335 0 : else ctx->metrics.incremental.bytes_written += sz;
1336 0 : }
1337 :
1338 0 : if( FD_UNLIKELY( ( full && ctx->metrics.full.bytes_read > ctx->metrics.full.bytes_total ) ||
1339 0 : (!full && ctx->metrics.incremental.bytes_read > ctx->metrics.incremental.bytes_total ) ) ) {
1340 0 : if( !ctx->malformed ) {
1341 0 : ctx->malformed = 1;
1342 0 : FD_LOG_WARNING(( "expected %s snapshot size of %lu bytes but read %lu bytes",
1343 0 : full ? "full" : "incremental",
1344 0 : full ? ctx->metrics.full.bytes_total : ctx->metrics.incremental.bytes_total,
1345 0 : full ? ctx->metrics.full.bytes_read : ctx->metrics.incremental.bytes_read ));
1346 :
1347 0 : }
1348 0 : }
1349 0 : }
1350 :
1351 : static void
1352 : ctrl_ack_frag( fd_snapct_tile_t * ctx,
1353 0 : ulong sig ) {
1354 0 : switch( sig ) {
1355 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
1356 0 : if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_READING_FULL_HTTP ||
1357 0 : ctx->state==FD_SNAPCT_STATE_READING_FULL_FILE ) ) {
1358 0 : FD_TEST( !ctx->flush_ack );
1359 0 : ctx->flush_ack = 1;
1360 0 : } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
1361 0 : break;
1362 :
1363 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_INCR:
1364 0 : if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP ||
1365 0 : ctx->state==FD_SNAPCT_STATE_READING_INCREMENTAL_FILE ) ) {
1366 0 : FD_TEST( !ctx->flush_ack );
1367 0 : ctx->flush_ack = 1;
1368 0 : } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
1369 0 : break;
1370 :
1371 0 : case FD_SNAPSHOT_MSG_CTRL_NEXT:
1372 0 : if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE ||
1373 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE ) ) {
1374 0 : FD_TEST( !ctx->flush_ack );
1375 0 : ctx->flush_ack = 1;
1376 0 : } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
1377 0 : break;
1378 :
1379 0 : case FD_SNAPSHOT_MSG_CTRL_DONE:
1380 0 : if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE ||
1381 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE ||
1382 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE ||
1383 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE ) ) {
1384 0 : FD_TEST( !ctx->flush_ack );
1385 0 : ctx->flush_ack = 1;
1386 0 : } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
1387 0 : break;
1388 :
1389 0 : case FD_SNAPSHOT_MSG_CTRL_FINI:
1390 0 : if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI ||
1391 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI ||
1392 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI ||
1393 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI ) ) {
1394 0 : FD_TEST( !ctx->flush_ack );
1395 0 : ctx->flush_ack = 1;
1396 0 : } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
1397 0 : break;
1398 :
1399 0 : case FD_SNAPSHOT_MSG_CTRL_FAIL:
1400 0 : if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET ||
1401 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ||
1402 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET ||
1403 0 : ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ) {
1404 0 : FD_TEST( !ctx->flush_ack );
1405 0 : ctx->flush_ack = 1;
1406 0 : } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
1407 0 : break;
1408 :
1409 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:
1410 0 : break;
1411 :
1412 0 : case FD_SNAPSHOT_MSG_CTRL_ERROR:
1413 0 : switch( ctx->state ) {
1414 0 : case FD_SNAPCT_STATE_READING_FULL_FILE:
1415 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI:
1416 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE:
1417 0 : case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE:
1418 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI:
1419 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE:
1420 0 : case FD_SNAPCT_STATE_READING_FULL_HTTP:
1421 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI:
1422 0 : case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE:
1423 0 : case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP:
1424 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI:
1425 0 : case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE:
1426 0 : FD_LOG_WARNING(( "received error from downstream tile while in state %s",
1427 0 : fd_snapct_state_str( (ulong)ctx->state ) ));
1428 0 : ctx->malformed = 1;
1429 0 : ctx->flush_ack = 1;
1430 0 : break;
1431 0 : default:
1432 0 : break;
1433 0 : }
1434 0 : break;
1435 0 : }
1436 0 : }
1437 :
1438 : static int
1439 : returnable_frag( fd_snapct_tile_t * ctx,
1440 : ulong in_idx,
1441 : ulong seq FD_PARAM_UNUSED,
1442 : ulong sig,
1443 : ulong chunk,
1444 : ulong sz,
1445 : ulong ctl FD_PARAM_UNUSED,
1446 : ulong tsorig FD_PARAM_UNUSED,
1447 : ulong tspub FD_PARAM_UNUSED,
1448 0 : fd_stem_context_t * stem FD_PARAM_UNUSED ) {
1449 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
1450 0 : gossip_frag( ctx, sig, sz, chunk );
1451 0 : } else if( ctx->in_kind[ in_idx ]==IN_KIND_SNAPLD ) {
1452 0 : snapld_frag( ctx, sig, sz, chunk );
1453 0 : } else if( ctx->in_kind[ in_idx ]==IN_KIND_ACK ) {
1454 0 : ctrl_ack_frag( ctx, sig );
1455 0 : } else FD_LOG_ERR(( "invalid in_kind %lu %u", in_idx, (uint)ctx->in_kind[ in_idx ] ));
1456 0 : return 0;
1457 0 : }
1458 :
1459 : static void
1460 : privileged_init( fd_topo_t * topo,
1461 0 : fd_topo_tile_t * tile ) {
1462 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1463 :
1464 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1465 0 : fd_snapct_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
1466 0 : void * _ssping = FD_SCRATCH_ALLOC_APPEND( l, fd_ssping_align(), fd_ssping_footprint( TOTAL_PEERS_MAX ) );
1467 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t)*GOSSIP_PEERS_MAX );
1468 0 : FD_SCRATCH_ALLOC_APPEND( l, gossip_ci_map_align(), gossip_ci_map_footprint( gossip_ci_map_chain_cnt_est( GOSSIP_PEERS_MAX ) ) );
1469 0 : void * _ssresolver = FD_SCRATCH_ALLOC_APPEND( l, fd_http_resolver_align(), fd_http_resolver_footprint( SERVER_PEERS_MAX ) );
1470 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX ) );
1471 :
1472 0 : #if FD_HAS_OPENSSL
1473 0 : void * _alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
1474 0 : fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), tile->kind_id );
1475 0 : fd_ossl_tile_init( alloc );
1476 0 : #endif
1477 :
1478 0 : ctx->ssping = NULL;
1479 0 : if( FD_LIKELY( download_enabled( tile ) ) ) ctx->ssping = fd_ssping_join( fd_ssping_new( _ssping, TOTAL_PEERS_MAX, 1UL, on_ping, ctx ) );
1480 0 : if( FD_LIKELY( tile->snapct.sources.servers_cnt ) ) ctx->ssresolver = fd_http_resolver_join( fd_http_resolver_new( _ssresolver, SERVER_PEERS_MAX, tile->snapct.incremental_snapshots, on_resolve, ctx ) );
1481 0 : else ctx->ssresolver = NULL;
1482 :
1483 0 : fd_ssarchive_remove_old_snapshots( tile->snapct.snapshots_path,
1484 0 : tile->snapct.max_full_snapshots_to_keep,
1485 0 : tile->snapct.max_incremental_snapshots_to_keep );
1486 :
1487 0 : ulong full_slot = ULONG_MAX;
1488 0 : ulong incremental_slot = ULONG_MAX;
1489 0 : int full_is_zstd = 0;
1490 0 : int incremental_is_zstd = 0;
1491 0 : char full_path[ PATH_MAX ] = {0};
1492 0 : char incremental_path[ PATH_MAX ] = {0};
1493 0 : uchar full_snapshot_hash[ FD_HASH_FOOTPRINT ] = {0};
1494 0 : uchar incremental_snapshot_hash[ FD_HASH_FOOTPRINT ] = {0};
1495 0 : if( FD_UNLIKELY( -1==fd_ssarchive_latest_pair( tile->snapct.snapshots_path,
1496 0 : tile->snapct.incremental_snapshots,
1497 0 : &full_slot,
1498 0 : &incremental_slot,
1499 0 : full_path,
1500 0 : incremental_path,
1501 0 : &full_is_zstd,
1502 0 : &incremental_is_zstd,
1503 0 : full_snapshot_hash,
1504 0 : incremental_snapshot_hash ) ) ) {
1505 0 : if( FD_UNLIKELY( !download_enabled( tile ) ) ) {
1506 0 : FD_LOG_ERR(( "No snapshots found in `%s` and no download sources are enabled. "
1507 0 : "Please enable downloading via [snapshots.sources] and restart.", tile->snapct.snapshots_path ));
1508 0 : }
1509 0 : ctx->local_in.full_snapshot_slot = ULONG_MAX;
1510 0 : ctx->local_in.incremental_snapshot_slot = ULONG_MAX;
1511 0 : ctx->local_in.full_snapshot_size = 0UL;
1512 0 : ctx->local_in.incremental_snapshot_size = 0UL;
1513 0 : ctx->local_in.full_snapshot_zstd = 0;
1514 0 : ctx->local_in.incremental_snapshot_zstd = 0;
1515 0 : fd_cstr_fini( ctx->local_in.full_snapshot_path );
1516 0 : fd_cstr_fini( ctx->local_in.incremental_snapshot_path );
1517 0 : fd_memset( ctx->local_in.full_snapshot_hash, 0, FD_HASH_FOOTPRINT );
1518 0 : fd_memset( ctx->local_in.incremental_snapshot_hash, 0, FD_HASH_FOOTPRINT );
1519 0 : } else {
1520 0 : FD_TEST( full_slot!=ULONG_MAX );
1521 :
1522 0 : ctx->local_in.full_snapshot_slot = full_slot;
1523 0 : ctx->local_in.incremental_snapshot_slot = incremental_slot;
1524 0 : ctx->local_in.full_snapshot_zstd = full_is_zstd;
1525 0 : ctx->local_in.incremental_snapshot_zstd = incremental_is_zstd;
1526 :
1527 0 : fd_cstr_ncpy( ctx->local_in.full_snapshot_path, full_path, PATH_MAX );
1528 0 : fd_memcpy( ctx->local_in.full_snapshot_hash, full_snapshot_hash, FD_HASH_FOOTPRINT );
1529 0 : struct stat full_stat;
1530 0 : if( FD_UNLIKELY( -1==stat( ctx->local_in.full_snapshot_path, &full_stat ) ) ) FD_LOG_ERR(( "stat() failed `%s` (%i-%s)", full_path, errno, fd_io_strerror( errno ) ));
1531 0 : if( FD_UNLIKELY( !S_ISREG( full_stat.st_mode ) ) ) FD_LOG_ERR(( "full snapshot path `%s` is not a regular file", full_path ));
1532 0 : ctx->local_in.full_snapshot_size = (ulong)full_stat.st_size;
1533 :
1534 0 : if( FD_LIKELY( incremental_slot!=ULONG_MAX ) ) {
1535 0 : fd_cstr_ncpy( ctx->local_in.incremental_snapshot_path, incremental_path, PATH_MAX );
1536 0 : fd_memcpy( ctx->local_in.incremental_snapshot_hash, incremental_snapshot_hash, FD_HASH_FOOTPRINT );
1537 0 : struct stat incremental_stat;
1538 0 : if( FD_UNLIKELY( -1==stat( ctx->local_in.incremental_snapshot_path, &incremental_stat ) ) ) FD_LOG_ERR(( "stat() failed `%s` (%i-%s)", incremental_path, errno, fd_io_strerror( errno ) ));
1539 0 : if( FD_UNLIKELY( !S_ISREG( incremental_stat.st_mode ) ) ) FD_LOG_ERR(( "incremental snapshot path `%s` is not a regular file", incremental_path ));
1540 0 : ctx->local_in.incremental_snapshot_size = (ulong)incremental_stat.st_size;
1541 0 : } else {
1542 0 : ctx->local_in.incremental_snapshot_size = 0UL;
1543 0 : fd_cstr_fini( ctx->local_in.incremental_snapshot_path );
1544 0 : }
1545 0 : }
1546 :
1547 0 : ctx->local_out.dir_fd = -1;
1548 0 : ctx->local_out.full_snapshot_fd = -1;
1549 0 : ctx->local_out.incremental_snapshot_fd = -1;
1550 0 : if( FD_LIKELY( download_enabled( tile ) ) ) {
1551 0 : ctx->local_out.dir_fd = open( tile->snapct.snapshots_path, O_DIRECTORY|O_CLOEXEC );
1552 0 : if( FD_UNLIKELY( -1==ctx->local_out.dir_fd ) ) FD_LOG_ERR(( "open(%s) failed (%i-%s)", tile->snapct.snapshots_path, errno, fd_io_strerror( errno ) ));
1553 :
1554 0 : ctx->local_out.full_snapshot_fd = openat( ctx->local_out.dir_fd, TEMP_FULL_SNAP_NAME, O_WRONLY|O_CREAT|O_TRUNC|O_NONBLOCK, S_IRUSR|S_IWUSR );
1555 0 : if( FD_UNLIKELY( -1==ctx->local_out.full_snapshot_fd ) ) FD_LOG_ERR(( "open(%s/%s) failed (%i-%s)", tile->snapct.snapshots_path, TEMP_FULL_SNAP_NAME, errno, fd_io_strerror( errno ) ));
1556 :
1557 0 : if( FD_LIKELY( tile->snapct.incremental_snapshots ) ) {
1558 0 : ctx->local_out.incremental_snapshot_fd = openat( ctx->local_out.dir_fd, TEMP_INCR_SNAP_NAME, O_WRONLY|O_CREAT|O_TRUNC|O_NONBLOCK, S_IRUSR|S_IWUSR );
1559 0 : if( FD_UNLIKELY( -1==ctx->local_out.incremental_snapshot_fd ) ) FD_LOG_ERR(( "open(%s/%s) failed (%i-%s)", tile->snapct.snapshots_path, TEMP_INCR_SNAP_NAME, errno, fd_io_strerror( errno ) ));
1560 0 : }
1561 0 : }
1562 :
1563 0 : FD_TEST( fd_rng_secure( &ctx->selector_seed, 8UL ) );
1564 0 : }
1565 :
1566 : static inline fd_snapct_out_link_t
1567 : out1( fd_topo_t const * topo,
1568 : fd_topo_tile_t const * tile,
1569 0 : char const * name ) {
1570 0 : ulong idx = ULONG_MAX;
1571 :
1572 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
1573 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
1574 0 : if( !strcmp( link->name, name ) ) {
1575 0 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had multiple output links named %s but expected one", tile->name, tile->kind_id, name ));
1576 0 : idx = i;
1577 0 : }
1578 0 : }
1579 :
1580 0 : if( FD_UNLIKELY( idx==ULONG_MAX ) ) return (fd_snapct_out_link_t){ .idx = ULONG_MAX, .mem = NULL, .chunk0 = 0, .wmark = 0, .chunk = 0, .mtu = 0 };
1581 :
1582 0 : ulong mtu = topo->links[ tile->out_link_id[ idx ] ].mtu;
1583 0 : if( FD_UNLIKELY( mtu==0UL ) ) return (fd_snapct_out_link_t){ .idx = idx, .mem = NULL, .chunk0 = ULONG_MAX, .wmark = ULONG_MAX, .chunk = ULONG_MAX, .mtu = mtu };
1584 :
1585 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
1586 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
1587 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, mtu );
1588 0 : return (fd_snapct_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0, .mtu = mtu };
1589 0 : }
1590 :
1591 : static void
1592 : unprivileged_init( fd_topo_t * topo,
1593 0 : fd_topo_tile_t * tile ) {
1594 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1595 :
1596 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1597 0 : fd_snapct_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
1598 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_ssping_align(), fd_ssping_footprint( TOTAL_PEERS_MAX ) );
1599 0 : void * _ci_table = FD_SCRATCH_ALLOC_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX );
1600 0 : void * _ci_map = FD_SCRATCH_ALLOC_APPEND( l, gossip_ci_map_align(), gossip_ci_map_footprint( gossip_ci_map_chain_cnt_est( GOSSIP_PEERS_MAX ) ) );
1601 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_http_resolver_align(), fd_http_resolver_footprint( SERVER_PEERS_MAX ) );
1602 0 : void * _selector = FD_SCRATCH_ALLOC_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX ) );
1603 :
1604 0 : ctx->config = tile->snapct;
1605 0 : ctx->gossip_enabled = gossip_enabled( tile );
1606 0 : ctx->download_enabled = download_enabled( tile );
1607 :
1608 0 : ctx->selector = fd_sspeer_selector_join( fd_sspeer_selector_new( _selector, TOTAL_PEERS_MAX, ctx->config.incremental_snapshots, ctx->selector_seed ) );
1609 :
1610 0 : if( ctx->config.sources.servers_cnt ) {
1611 0 : for( ulong i=0UL; i<tile->snapct.sources.servers_cnt; i++ ) {
1612 : /* The peers needs to be added to resolver and to selector.
1613 : Only if this succeeds, add the peer to ssping list. */
1614 0 : if( FD_LIKELY( !fd_http_resolver_add( ctx->ssresolver,
1615 0 : tile->snapct.sources.servers[ i ].addr,
1616 0 : tile->snapct.sources.servers[ i ].hostname,
1617 0 : tile->snapct.sources.servers[ i ].is_https,
1618 0 : ctx->selector ) ) ) {
1619 0 : fd_ssping_add( ctx->ssping, tile->snapct.sources.servers[ i ].addr );
1620 0 : }
1621 0 : }
1622 0 : }
1623 :
1624 0 : if( FD_UNLIKELY( !ctx->config.incremental_snapshots ) ) {
1625 0 : FD_LOG_WARNING(( "incremental snapshots disabled via [snapshots.incremental_snapshots]." ));
1626 0 : }
1627 :
1628 0 : ctx->state = FD_SNAPCT_STATE_INIT;
1629 0 : ctx->malformed = 0;
1630 0 : ctx->deadline_nanos = fd_log_wallclock() + FD_SNAPCT_WAITING_FOR_PEERS_TIMEOUT;
1631 0 : ctx->flush_ack = 0;
1632 0 : ctx->peer.addr.l = 0UL;
1633 :
1634 0 : fd_memset( ctx->http_full_snapshot_name, 0, PATH_MAX );
1635 0 : fd_memset( ctx->http_incr_snapshot_name, 0, PATH_MAX );
1636 :
1637 0 : ctx->gossip_in_mem = NULL;
1638 0 : int has_snapld_dc = 0, has_ack_loopback = 0;
1639 0 : FD_TEST( tile->in_cnt<=MAX_IN_LINKS );
1640 0 : for( ulong i=0UL; i<(tile->in_cnt); i++ ) {
1641 0 : fd_topo_link_t * in_link = &topo->links[ tile->in_link_id[ i ] ];
1642 0 : if( 0==strcmp( in_link->name, "gossip_out" ) ) {
1643 0 : ctx->in_kind[ i ] = IN_KIND_GOSSIP;
1644 0 : ctx->gossip_in_mem = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
1645 0 : } else if( 0==strcmp( in_link->name, "snapld_dc" ) ) {
1646 0 : ctx->in_kind[ i ] = IN_KIND_SNAPLD;
1647 0 : ctx->snapld_in_mem = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
1648 0 : FD_TEST( !has_snapld_dc );
1649 0 : has_snapld_dc = 1;
1650 0 : } else if( 0==strcmp( in_link->name, "snapin_ct" ) || 0==strcmp( in_link->name, "snapls_ct" ) ||
1651 0 : 0==strcmp( in_link->name, "snapwm_ct" ) || 0==strcmp( in_link->name, "snaplv_ct" ) ) {
1652 0 : ctx->in_kind[ i ] = IN_KIND_ACK;
1653 0 : FD_TEST( !has_ack_loopback );
1654 0 : has_ack_loopback = 1;
1655 0 : }
1656 0 : }
1657 0 : FD_TEST( has_snapld_dc && has_ack_loopback );
1658 0 : FD_TEST( ctx->gossip_enabled==(ctx->gossip_in_mem!=NULL) );
1659 :
1660 0 : ctx->predicted_incremental.full_slot = ULONG_MAX;
1661 0 : ctx->predicted_incremental.slot = ULONG_MAX;
1662 0 : ctx->predicted_incremental.pending = 0;
1663 :
1664 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
1665 :
1666 0 : fd_memset( _ci_table, 0, sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX );
1667 0 : ctx->gossip.ci_table = _ci_table;
1668 0 : ctx->gossip.ci_map = gossip_ci_map_join( gossip_ci_map_new( _ci_map, gossip_ci_map_chain_cnt_est( GOSSIP_PEERS_MAX ), 0UL ) );
1669 0 : ctx->gossip.allowed_cnt = 0UL;
1670 0 : ctx->gossip.saturated = !ctx->gossip_enabled;
1671 :
1672 0 : if( FD_UNLIKELY( tile->out_cnt<2UL || tile->out_cnt>3UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 2-3", tile->out_cnt ));
1673 0 : ctx->out_ld = out1( topo, tile, "snapct_ld" );
1674 0 : ctx->out_gui = out1( topo, tile, "snapct_gui" );
1675 0 : ctx->out_rp = out1( topo, tile, "snapct_repr" );
1676 0 : }
1677 :
1678 : /* after_credit can result in as many as 5 stem publishes in some code
1679 : paths, and returnable_frag can result in 1. */
1680 0 : #define STEM_BURST 6UL
1681 :
1682 0 : #define STEM_LAZY 1000L
1683 :
1684 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapct_tile_t
1685 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapct_tile_t)
1686 :
1687 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
1688 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
1689 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
1690 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
1691 :
1692 : #include "../../disco/stem/fd_stem.c"
1693 :
1694 : fd_topo_run_tile_t fd_tile_snapct = {
1695 : .name = NAME,
1696 : .rlimit_file_cnt_fn = rlimit_file_cnt,
1697 : .populate_allowed_seccomp = populate_allowed_seccomp,
1698 : .populate_allowed_fds = populate_allowed_fds,
1699 : .scratch_align = scratch_align,
1700 : .scratch_footprint = scratch_footprint,
1701 : .loose_footprint = loose_footprint,
1702 : .privileged_init = privileged_init,
1703 : .unprivileged_init = unprivileged_init,
1704 : .run = stem_run,
1705 : .keep_host_networking = 1,
1706 : .allow_connect = 1,
1707 : .allow_renameat = 1,
1708 : };
1709 :
1710 : #undef NAME
|