LCOV - code coverage report
Current view: top level - discof/restore - fd_snapct_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 957 0.0 %
Date: 2025-12-07 04:58:33 Functions: 0 28 0.0 %

          Line data    Source code
       1             : #include "fd_snapct_tile.h"
       2             : #include "utils/fd_ssping.h"
       3             : #include "utils/fd_ssctrl.h"
       4             : #include "utils/fd_ssarchive.h"
       5             : #include "utils/fd_http_resolver.h"
       6             : #include "utils/fd_ssmsg.h"
       7             : 
       8             : #include "../../disco/topo/fd_topo.h"
       9             : #include "../../disco/metrics/fd_metrics.h"
      10             : #include "../../flamenco/gossip/fd_gossip_types.h"
      11             : #include "../../waltz/openssl/fd_openssl_tile.h"
      12             : 
      13             : #include <errno.h>
      14             : #include <stdio.h>
      15             : #include <fcntl.h>
      16             : #include <unistd.h>
      17             : #include <sys/stat.h>
      18             : #include <netinet/tcp.h>
      19             : #include <netinet/in.h>
      20             : 
      21             : #include "generated/fd_snapct_tile_seccomp.h"
      22             : 
      23             : #define NAME "snapct"
      24             : 
      25             : /* FIXME: Implement full_effective_age_cancel_threshold */
      26             : /* FIXME: Implement min_speed_mib and other download health logic */
      27             : /* FIXME: Implement max_retry_abort and retry logic in general */
      28             : /* FIXME: Add more timeout config options and have consistent behavior */
      29             : /* FIXME: Do a finishing pass over the default.toml config options / comments */
      30             : /* FIXME: Improve behavior when using incremental_snapshots = false */
      31             : /* FIXME: Handle cases where no explicitly allowed peers advertise RPC */
      32             : /* FIXME: Make the code more strict about duplicate IP:port's */
      33             : /* FIXME: Handle cases where the slot number we start downloading differs from advertised */
      34             : 
      35           0 : #define GOSSIP_PEERS_MAX (FD_CONTACT_INFO_TABLE_SIZE)
      36           0 : #define SERVER_PEERS_MAX (FD_TOPO_SNAPSHOTS_SERVERS_MAX_RESOLVED)
      37           0 : #define TOTAL_PEERS_MAX  (GOSSIP_PEERS_MAX + SERVER_PEERS_MAX)
      38             : 
      39           0 : #define IN_KIND_ACK    (0)
      40           0 : #define IN_KIND_SNAPLD (1)
      41           0 : #define IN_KIND_GOSSIP (2)
      42             : #define MAX_IN_LINKS   (3)
      43             : 
      44           0 : #define TEMP_FULL_SNAP_NAME ".snapshot.tar.bz2-partial"
      45           0 : #define TEMP_INCR_SNAP_NAME ".incremental-snapshot.tar.bz2-partial"
      46             : 
      47             : struct fd_snapct_out_link {
      48             :   ulong       idx;
      49             :   fd_wksp_t * mem;
      50             :   ulong       chunk0;
      51             :   ulong       wmark;
      52             :   ulong       chunk;
      53             :   ulong       mtu;
      54             : };
      55             : typedef struct fd_snapct_out_link fd_snapct_out_link_t;
      56             : 
      57             : #define FD_SNAPCT_GOSSIP_FRESH_DEADLINE_NANOS      (10L*1000L*1000L*1000L)    /* gossip contact info is pushed every ~7.5 seconds */
      58           0 : #define FD_SNAPCT_GOSSIP_SATURATION_CHECK_INTERVAL (      10L*1000L*1000L)
      59           0 : #define FD_SNAPCT_GOSSIP_SATURATION_THRESHOLD      (0.05)                     /* 5% fresh peers */
      60             : 
      61           0 : #define FD_SNAPCT_COLLECTING_PEERS_TIMEOUT         (2L*60L*1000L*1000L*1000L) /* 2 minutes */
      62           0 : #define FD_SNAPCT_WAITING_FOR_PEERS_TIMEOUT        (2L*60L*1000L*1000L*1000L) /* 2 minutes */
      63             : 
      64             : struct gossip_ci_entry {
      65             :   fd_pubkey_t   pubkey;
      66             :   int           allowed;
      67             :   fd_ip4_port_t rpc_addr;
      68             :   long          added_nanos;
      69             :   ulong         map_next;
      70             : };
      71             : typedef struct gossip_ci_entry gossip_ci_entry_t;
      72             : 
      73             : #define MAP_NAME               gossip_ci_map
      74           0 : #define MAP_KEY                pubkey
      75             : #define MAP_ELE_T              gossip_ci_entry_t
      76             : #define MAP_KEY_T              fd_pubkey_t
      77           0 : #define MAP_NEXT               map_next
      78           0 : #define MAP_KEY_EQ(k0,k1)      fd_pubkey_eq( k0, k1 )
      79           0 : #define MAP_KEY_HASH(key,seed) fd_hash( seed, key, sizeof(fd_pubkey_t) )
      80             : #include "../../util/tmpl/fd_map_chain.c"
      81             : 
      82             : struct fd_snapct_tile {
      83             :   struct fd_topo_tile_snapct config;
      84             :   int                        gossip_enabled;
      85             :   int                        download_enabled;
      86             : 
      87             :   fd_ssping_t *          ssping;
      88             :   fd_http_resolver_t *   ssresolver;
      89             :   fd_sspeer_selector_t * selector;
      90             : 
      91             :   int           state;
      92             :   int           malformed;
      93             :   long          deadline_nanos;
      94             :   int           flush_ack;
      95             :   fd_ip4_port_t addr;
      96             : 
      97             :   struct {
      98             :     int dir_fd;
      99             :     int full_snapshot_fd;
     100             :     int incremental_snapshot_fd;
     101             :   } local_out;
     102             : 
     103             :   char http_full_snapshot_name[ PATH_MAX ];
     104             :   char http_incr_snapshot_name[ PATH_MAX ];
     105             : 
     106             :   fd_wksp_t const * gossip_in_mem;
     107             :   fd_wksp_t const * snapld_in_mem;
     108             :   uchar             in_kind[ MAX_IN_LINKS ];
     109             : 
     110             :   struct {
     111             :     ulong full_slot;
     112             :     ulong slot;
     113             :     int   dirty;
     114             :   } predicted_incremental;
     115             : 
     116             :   struct {
     117             :     ulong full_snapshot_slot;
     118             :     char  full_snapshot_path[ PATH_MAX ];
     119             :     ulong full_snapshot_size;
     120             :     int   full_snapshot_zstd;
     121             : 
     122             :     ulong incremental_snapshot_slot;
     123             :     char  incremental_snapshot_path[ PATH_MAX ];
     124             :     ulong incremental_snapshot_size;
     125             :     int   incremental_snapshot_zstd;
     126             :   } local_in;
     127             : 
     128             :   struct {
     129             :     struct {
     130             :       ulong bytes_read;
     131             :       ulong bytes_written;
     132             :       ulong bytes_total;
     133             :       uint  num_retries;
     134             :     } full;
     135             : 
     136             :     struct {
     137             :       ulong bytes_read;
     138             :       ulong bytes_written;
     139             :       ulong bytes_total;
     140             :       uint  num_retries;
     141             :     } incremental;
     142             :   } metrics;
     143             : 
     144             :   struct {
     145             :     gossip_ci_entry_t * ci_table;  /* flat array of all gossip entries, allowed or not */
     146             :     gossip_ci_map_t *   ci_map;    /* map from pubkey to only allowed gossip entries */
     147             :     ulong               fresh_cnt;
     148             :     ulong               total_cnt;
     149             :     int                 saturated;
     150             :     long                next_saturated_check;
     151             :   } gossip;
     152             : 
     153             :   fd_snapct_out_link_t out_ld;
     154             :   fd_snapct_out_link_t out_gui;
     155             :   fd_snapct_out_link_t out_rp;
     156             : };
     157             : typedef struct fd_snapct_tile fd_snapct_tile_t;
     158             : 
     159             : static int
     160           0 : gossip_enabled( fd_topo_tile_t const * tile ) {
     161           0 :   return tile->snapct.sources.gossip.allow_any || tile->snapct.sources.gossip.allow_list_cnt>0UL;
     162           0 : }
     163             : 
     164             : static int
     165           0 : download_enabled( fd_topo_tile_t const * tile ) {
     166           0 :   return gossip_enabled( tile ) || tile->snapct.sources.servers_cnt>0UL;
     167           0 : }
     168             : 
     169             : FD_FN_CONST static inline ulong
     170           0 : loose_footprint( fd_topo_tile_t const * tile ) {
     171           0 :   (void)tile;
     172             :   /* Leftover space for OpenSSL allocations */
     173           0 :   return 1<<26UL; /* 64 MiB */
     174           0 : }
     175             : 
     176             : static ulong
     177           0 : scratch_align( void ) {
     178           0 :   return fd_ulong_max( alignof(fd_snapct_tile_t),
     179           0 :          fd_ulong_max( fd_ssping_align(),
     180           0 :          fd_ulong_max( alignof(gossip_ci_entry_t),
     181           0 :          fd_ulong_max( gossip_ci_map_align(),
     182           0 :          fd_ulong_max( fd_http_resolver_align(),
     183           0 :                        fd_sspeer_selector_align() ) ) ) ) );
     184           0 : }
     185             : 
     186             : static ulong
     187           0 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
     188           0 :   ulong l = FD_LAYOUT_INIT;
     189           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_snapct_tile_t),  sizeof(fd_snapct_tile_t)                                                   );
     190           0 :   l = FD_LAYOUT_APPEND( l, fd_ssping_align(),          fd_ssping_footprint( TOTAL_PEERS_MAX )                                     );
     191           0 :   l = FD_LAYOUT_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX                               );
     192           0 :   l = FD_LAYOUT_APPEND( l, gossip_ci_map_align(),      gossip_ci_map_footprint( gossip_ci_map_chain_cnt_est( GOSSIP_PEERS_MAX ) ) );
     193           0 :   l = FD_LAYOUT_APPEND( l, fd_http_resolver_align(),   fd_http_resolver_footprint( SERVER_PEERS_MAX )                             );
     194           0 :   l = FD_LAYOUT_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX )                            );
     195           0 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),           fd_alloc_footprint()                                                       );
     196           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     197           0 : }
     198             : 
     199             : static inline int
     200           0 : should_shutdown( fd_snapct_tile_t * ctx ) {
     201           0 :   return ctx->state==FD_SNAPCT_STATE_SHUTDOWN;
     202           0 : }
     203             : 
     204             : static void
     205           0 : during_housekeeping( fd_snapct_tile_t * ctx ) {
     206           0 :   long now = fd_log_wallclock();
     207             : 
     208           0 :   if( FD_UNLIKELY( !ctx->gossip.saturated && now>ctx->gossip.next_saturated_check ) ) {
     209           0 :     ctx->gossip.next_saturated_check = now + FD_SNAPCT_GOSSIP_SATURATION_CHECK_INTERVAL;
     210             : 
     211           0 :     ulong fresh_cnt = 0UL;
     212           0 :     ulong total_cnt = 0UL;
     213           0 :     for( gossip_ci_map_iter_t iter = gossip_ci_map_iter_init( ctx->gossip.ci_map, ctx->gossip.ci_table );
     214           0 :          !gossip_ci_map_iter_done( iter, ctx->gossip.ci_map, ctx->gossip.ci_table );
     215           0 :          iter = gossip_ci_map_iter_next( iter, ctx->gossip.ci_map, ctx->gossip.ci_table ) ) {
     216           0 :       gossip_ci_entry_t const * ci_entry = gossip_ci_map_iter_ele_const( iter, ctx->gossip.ci_map, ctx->gossip.ci_table );
     217           0 :       if( FD_UNLIKELY( ci_entry->added_nanos>(now-FD_SNAPCT_GOSSIP_FRESH_DEADLINE_NANOS) ) ) fresh_cnt++;
     218           0 :       total_cnt++;
     219           0 :     }
     220           0 :     ctx->gossip.fresh_cnt = fresh_cnt;
     221           0 :     ctx->gossip.total_cnt = total_cnt;
     222             : 
     223           0 :     if( total_cnt!=0UL && total_cnt==ctx->config.sources.gossip.allow_list_cnt ) ctx->gossip.saturated = 1;
     224           0 :     else {
     225           0 :       double fresh = total_cnt ? (double)fresh_cnt/(double)total_cnt : 1.0;
     226           0 :       ctx->gossip.saturated = fresh<FD_SNAPCT_GOSSIP_SATURATION_THRESHOLD;
     227           0 :     }
     228           0 :   }
     229           0 : }
     230             : 
     231             : static void
     232           0 : metrics_write( fd_snapct_tile_t * ctx ) {
     233             :   /* FIXME: Track/report FULL_NUM_RETRIES & INCREMENTAL_NUM_RETRIES */
     234             : 
     235           0 :   FD_MGAUGE_SET( SNAPCT, FULL_BYTES_READ,               ctx->metrics.full.bytes_read );
     236           0 :   FD_MGAUGE_SET( SNAPCT, FULL_BYTES_WRITTEN,            ctx->metrics.full.bytes_written );
     237           0 :   FD_MGAUGE_SET( SNAPCT, FULL_BYTES_TOTAL,              ctx->metrics.full.bytes_total );
     238           0 :   FD_MGAUGE_SET( SNAPCT, FULL_DOWNLOAD_RETRIES,         ctx->metrics.full.num_retries );
     239             : 
     240           0 :   FD_MGAUGE_SET( SNAPCT, INCREMENTAL_BYTES_READ,        ctx->metrics.incremental.bytes_read );
     241           0 :   FD_MGAUGE_SET( SNAPCT, INCREMENTAL_BYTES_WRITTEN,     ctx->metrics.incremental.bytes_written );
     242           0 :   FD_MGAUGE_SET( SNAPCT, INCREMENTAL_BYTES_TOTAL,       ctx->metrics.incremental.bytes_total );
     243           0 :   FD_MGAUGE_SET( SNAPCT, INCREMENTAL_DOWNLOAD_RETRIES,  ctx->metrics.incremental.num_retries );
     244             : 
     245           0 :   FD_MGAUGE_SET( SNAPCT, GOSSIP_FRESH_COUNT,            ctx->gossip.fresh_cnt );
     246           0 :   FD_MGAUGE_SET( SNAPCT, GOSSIP_TOTAL_COUNT,            ctx->gossip.total_cnt );
     247             : 
     248           0 :   FD_MGAUGE_SET( SNAPCT, PREDICTED_SLOT,                ctx->predicted_incremental.slot );
     249             : 
     250           0 : #if FD_HAS_OPENSSL
     251           0 :   FD_MCNT_SET(   SNAPCT, SSL_ALLOC_ERRORS,                fd_ossl_alloc_errors );
     252           0 : #endif
     253             : 
     254           0 :   FD_MGAUGE_SET( SNAPCT, STATE, (ulong)ctx->state );
     255           0 : }
     256             : 
     257             : static void
     258             : snapshot_path_gui_publish( fd_snapct_tile_t *  ctx,
     259             :                            fd_stem_context_t * stem,
     260             :                            char const *        path,
     261           0 :                            int                 is_full ) {
     262             :   /* FIXME: Consider whether we can get everything we need from metrics
     263             :      rather than creating an entire link for this rare message */
     264           0 :   fd_snapct_update_t * out = fd_chunk_to_laddr( ctx->out_gui.mem, ctx->out_gui.chunk );
     265           0 :   FD_TEST( fd_cstr_printf_check( out->read_path, PATH_MAX, NULL, "%s", path ) );
     266           0 :   out->is_download = 0;
     267           0 :   out->type = fd_int_if( is_full, FD_SNAPCT_SNAPSHOT_TYPE_FULL, FD_SNAPCT_SNAPSHOT_TYPE_INCREMENTAL );
     268           0 :   fd_stem_publish( stem, ctx->out_gui.idx, 0UL, ctx->out_gui.chunk, sizeof(fd_snapct_update_t) , 0UL, 0UL, 0UL );
     269           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 );
     270           0 : }
     271             : 
     272             : static void
     273           0 : predict_incremental( fd_snapct_tile_t * ctx ) {
     274           0 :   if( FD_UNLIKELY( !ctx->config.incremental_snapshots ) ) return;
     275           0 :   if( FD_UNLIKELY( ctx->predicted_incremental.full_slot==ULONG_MAX ) ) return;
     276             : 
     277           0 :   fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
     278             : 
     279           0 :   if( FD_LIKELY( best.addr.l ) ) {
     280           0 :     if( FD_UNLIKELY( ctx->predicted_incremental.slot!=best.ssinfo.incremental.slot ) ) {
     281           0 :       ctx->predicted_incremental.slot  = best.ssinfo.incremental.slot;
     282           0 :       ctx->predicted_incremental.dirty = 1;
     283           0 :     }
     284           0 :   }
     285           0 : }
     286             : 
     287             : static void
     288             : on_resolve( void *              _ctx,
     289             :             fd_ip4_port_t       addr,
     290           0 :             fd_ssinfo_t const * ssinfo ) {
     291           0 :   fd_snapct_tile_t * ctx = (fd_snapct_tile_t *)_ctx;
     292             : 
     293           0 :   fd_sspeer_selector_add( ctx->selector, addr, ULONG_MAX, ssinfo );
     294           0 :   fd_sspeer_selector_process_cluster_slot( ctx->selector, ssinfo->full.slot, ssinfo->incremental.slot );
     295           0 :   predict_incremental( ctx );
     296           0 : }
     297             : 
     298             : static void
     299             : on_ping( void *        _ctx,
     300             :          fd_ip4_port_t addr,
     301           0 :          ulong         latency ) {
     302           0 :   fd_snapct_tile_t * ctx = (fd_snapct_tile_t *)_ctx;
     303             : 
     304           0 :   fd_sspeer_selector_add( ctx->selector, addr, latency, NULL );
     305           0 :   predict_incremental( ctx );
     306           0 : }
     307             : 
     308             : static void
     309             : on_snapshot_hash( fd_snapct_tile_t *                 ctx,
     310             :                   fd_ip4_port_t                      addr,
     311           0 :                   fd_gossip_update_message_t const * msg ) {
     312           0 :   ulong full_slot = msg->snapshot_hashes.full->slot;
     313           0 :   ulong incr_slot = 0UL;
     314             : 
     315           0 :   for( ulong i=0UL; i<msg->snapshot_hashes.incremental_len; i++ ) {
     316           0 :     if( FD_LIKELY( msg->snapshot_hashes.incremental[ i ].slot>incr_slot ) ) {
     317           0 :       incr_slot = msg->snapshot_hashes.incremental[ i ].slot;
     318           0 :     }
     319           0 :   }
     320             : 
     321           0 :   fd_ssinfo_t ssinfo = { .full = { .slot = msg->snapshot_hashes.full->slot },
     322           0 :                         .incremental = { .slot = incr_slot, .base_slot = full_slot } };
     323             : 
     324           0 :   fd_sspeer_selector_add( ctx->selector, addr, ULONG_MAX, &ssinfo );
     325           0 :   fd_sspeer_selector_process_cluster_slot( ctx->selector, full_slot, incr_slot );
     326           0 :   predict_incremental( ctx );
     327           0 : }
     328             : 
     329             : static void
     330             : send_expected_slot( fd_snapct_tile_t *  ctx,
     331             :                     fd_stem_context_t * stem,
     332           0 :                     ulong               slot ) {
     333           0 :   uint tsorig; uint tspub;
     334           0 :   fd_ssmsg_slot_to_frag( slot, &tsorig, &tspub );
     335           0 :   fd_stem_publish( stem, ctx->out_rp.idx, FD_SSMSG_EXPECTED_SLOT, 0UL, 0UL, 0UL, tsorig, tspub );
     336           0 : }
     337             : 
     338             : static void
     339           0 : rename_snapshots( fd_snapct_tile_t * ctx ) {
     340           0 :   FD_TEST( -1!=ctx->local_out.dir_fd );
     341             : 
     342             :   /* FIXME: We should rename the full snapshot earlier as soon as the
     343             :      download is complete.  That way, if the validator crashes during the
     344             :      incremental load, we can still use the snapshot on the next run. */
     345             : 
     346           0 :   if( FD_LIKELY( -1!=ctx->local_out.full_snapshot_fd && ctx->http_full_snapshot_name[ 0 ]!='\0' ) ) {
     347           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 ) ) )
     348           0 :       FD_LOG_ERR(( "renameat() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     349           0 :   }
     350           0 :   if( FD_LIKELY( -1!=ctx->local_out.incremental_snapshot_fd && ctx->http_incr_snapshot_name[ 0 ]!='\0' ) ) {
     351           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 ) ) )
     352           0 :       FD_LOG_ERR(( "renameat() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     353           0 :   }
     354           0 : }
     355             : 
     356             : static ulong
     357             : rlimit_file_cnt( fd_topo_t const *      topo FD_PARAM_UNUSED,
     358           0 :                  fd_topo_tile_t const * tile ) {
     359           0 :   ulong cnt = 1UL +                             /* stderr */
     360           0 :               1UL;                              /* logfile */
     361           0 :   if( download_enabled( tile ) ) {
     362           0 :     cnt +=    1UL +                             /* ssping socket */
     363           0 :               2UL +                             /* dirfd + full snapshot download temp fd */
     364           0 :               tile->snapct.sources.servers_cnt; /* http resolver peer full sockets */
     365           0 :     if( tile->snapct.incremental_snapshots ) {
     366           0 :       cnt +=  1UL +                             /* incr snapshot download temp fd */
     367           0 :               tile->snapct.sources.servers_cnt; /* http resolver peer incr sockets */
     368           0 :     }
     369           0 :   }
     370           0 :   return cnt;
     371           0 : }
     372             : 
     373             : static ulong
     374             : populate_allowed_seccomp( fd_topo_t const *      topo,
     375             :                           fd_topo_tile_t const * tile,
     376             :                           ulong                  out_cnt,
     377           0 :                           struct sock_filter *   out ) {
     378             : 
     379           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     380             : 
     381           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     382           0 :   fd_snapct_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
     383             : 
     384           0 :   int ping_fd = download_enabled( tile ) ? fd_ssping_get_sockfd( ctx->ssping ) : -1;
     385           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 );
     386           0 :   return sock_filter_policy_fd_snapct_tile_instr_cnt;
     387           0 : }
     388             : 
     389             : static ulong
     390             : populate_allowed_fds( fd_topo_t const *      topo,
     391             :                       fd_topo_tile_t const * tile,
     392             :                       ulong                  out_fds_cnt,
     393           0 :                       int *                  out_fds ) {
     394           0 :   if( FD_UNLIKELY( out_fds_cnt<6UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     395             : 
     396           0 :   ulong out_cnt = 0;
     397           0 :   out_fds[ out_cnt++ ] = 2UL; /* stderr */
     398           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
     399           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     400           0 :   }
     401             : 
     402           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     403             : 
     404           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     405           0 :   fd_snapct_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
     406           0 :   if( FD_LIKELY( -1!=ctx->local_out.dir_fd ) )                  out_fds[ out_cnt++ ] = ctx->local_out.dir_fd;
     407           0 :   if( FD_LIKELY( -1!=ctx->local_out.full_snapshot_fd ) )        out_fds[ out_cnt++ ] = ctx->local_out.full_snapshot_fd;
     408           0 :   if( FD_LIKELY( -1!=ctx->local_out.incremental_snapshot_fd ) ) out_fds[ out_cnt++ ] = ctx->local_out.incremental_snapshot_fd;
     409           0 :   if( FD_LIKELY( download_enabled( tile ) ) )                   out_fds[ out_cnt++ ] = fd_ssping_get_sockfd( ctx->ssping );
     410             : 
     411           0 :   return out_cnt;
     412           0 : }
     413             : 
     414             : static void
     415             : init_load( fd_snapct_tile_t *  ctx,
     416             :            fd_stem_context_t * stem,
     417             :            int full,
     418           0 :            int file ) {
     419           0 :   fd_ssctrl_init_t * out = fd_chunk_to_laddr( ctx->out_ld.mem, ctx->out_ld.chunk );
     420           0 :   out->file = file;
     421           0 :   out->zstd = !file || (full ? ctx->local_in.full_snapshot_zstd : ctx->local_in.incremental_snapshot_zstd);
     422           0 :   if( !file ) {
     423           0 :     out->addr = ctx->addr;
     424           0 :     for( ulong i=0UL; i<SERVER_PEERS_MAX; i++ ) {
     425           0 :       if( FD_UNLIKELY( ctx->addr.l==ctx->config.sources.servers[ i ].addr.l ) ) {
     426           0 :         fd_cstr_ncpy( out->hostname, ctx->config.sources.servers[ i ].hostname, sizeof(out->hostname) );
     427           0 :         out->is_https = ctx->config.sources.servers[ i ].is_https;
     428           0 :         break;
     429           0 :       }
     430           0 :     }
     431           0 :   }
     432           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 );
     433           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 );
     434           0 :   ctx->flush_ack = 0;
     435             : 
     436           0 :   if( file ) {
     437             :     /* When loading from a local file and not from HTTP, there is no
     438             :        future metadata message to initialize total size / filename, as
     439             :        these are already known immediately. */
     440           0 :     if( full ) {
     441           0 :       ctx->metrics.full.bytes_total = ctx->local_in.full_snapshot_size;
     442           0 :       fd_cstr_fini( ctx->http_full_snapshot_name );
     443           0 :       if( FD_LIKELY( !!ctx->out_gui.mem ) ) {
     444           0 :         snapshot_path_gui_publish( ctx, stem, ctx->local_in.full_snapshot_path, 1 );
     445           0 :       }
     446           0 :     } else {
     447           0 :       ctx->metrics.incremental.bytes_total = ctx->local_in.incremental_snapshot_size;
     448           0 :       fd_cstr_fini( ctx->http_incr_snapshot_name );
     449           0 :       if( FD_LIKELY( !!ctx->out_gui.mem ) ) {
     450           0 :         snapshot_path_gui_publish( ctx, stem, ctx->local_in.incremental_snapshot_path, 0 );
     451           0 :       }
     452           0 :     }
     453           0 :   }
     454           0 : }
     455             : 
     456             : static void
     457             : log_download( fd_snapct_tile_t * ctx,
     458             :               int                full,
     459             :               fd_ip4_port_t      addr,
     460           0 :               ulong              slot ) {
     461           0 :   for( gossip_ci_map_iter_t iter = gossip_ci_map_iter_init( ctx->gossip.ci_map, ctx->gossip.ci_table );
     462           0 :       !gossip_ci_map_iter_done( iter, ctx->gossip.ci_map, ctx->gossip.ci_table );
     463           0 :       iter = gossip_ci_map_iter_next( iter, ctx->gossip.ci_map, ctx->gossip.ci_table ) ) {
     464           0 :     gossip_ci_entry_t const * ci_entry = gossip_ci_map_iter_ele_const( iter, ctx->gossip.ci_map, ctx->gossip.ci_table );
     465           0 :     if( ci_entry->rpc_addr.l==addr.l ) {
     466           0 :       FD_TEST( ci_entry->allowed );
     467           0 :       FD_BASE58_ENCODE_32_BYTES( ci_entry->pubkey.uc, pubkey_b58 );
     468           0 :       FD_LOG_NOTICE(( "downloading %s snapshot at slot %lu from allowed gossip peer %s at http://" FD_IP4_ADDR_FMT ":%hu/%s",
     469           0 :                       full ? "full" : "incremental", slot, pubkey_b58,
     470           0 :                       FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ),
     471           0 :                       full ? "snapshot.tar.bz2" : "incremental-snapshot.tar.bz2" ));
     472           0 :       return;
     473           0 :     }
     474           0 :   }
     475             : 
     476           0 :   for( ulong i=0UL; i<ctx->config.sources.servers_cnt; i++ ) {
     477           0 :     if( addr.l==ctx->config.sources.servers[ i ].addr.l ) {
     478           0 :       if( ctx->config.sources.servers[ i ].is_https ) {
     479           0 :         FD_LOG_NOTICE(( "downloading %s snapshot at slot %lu from configured server with index %lu at https://%s:%hu/%s",
     480           0 :                         full ? "full" : "incremental", slot, i,
     481           0 :                         ctx->config.sources.servers[ i ].hostname, fd_ushort_bswap( addr.port ),
     482           0 :                         full ? "snapshot.tar.bz2" : "incremental-snapshot.tar.bz2" ));
     483           0 :       } else {
     484           0 :         FD_LOG_NOTICE(( "downloading %s snapshot at slot %lu from configured server with index %lu at http://" FD_IP4_ADDR_FMT ":%hu/%s",
     485           0 :                         full ? "full" : "incremental", slot, i,
     486           0 :                         FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ),
     487           0 :                         full ? "snapshot.tar.bz2" : "incremental-snapshot.tar.bz2" ));
     488           0 :       }
     489           0 :       return;
     490           0 :     }
     491           0 :   }
     492             : 
     493           0 :   FD_TEST( 0 ); /* should not be possible */
     494           0 : }
     495             : 
     496             : static void
     497             : after_credit( fd_snapct_tile_t *  ctx,
     498             :               fd_stem_context_t * stem,
     499             :               int *               opt_poll_in FD_PARAM_UNUSED,
     500           0 :               int *               charge_busy FD_PARAM_UNUSED ) {
     501           0 :   long now = fd_log_wallclock();
     502             : 
     503           0 :   if( FD_LIKELY( ctx->ssping ) ) fd_ssping_advance( ctx->ssping, now, ctx->selector );
     504           0 :   if( FD_LIKELY( ctx->ssresolver ) ) fd_http_resolver_advance( ctx->ssresolver, now, ctx->selector );
     505             : 
     506             :   /* send an expected slot message as the predicted incremental
     507             :      could have changed as a result of the pinger, resolver, or from
     508             :      processing gossip frags in gossip_frag. */
     509           0 :   if( FD_LIKELY( ctx->predicted_incremental.dirty ) ) {
     510           0 :     send_expected_slot( ctx, stem, ctx->predicted_incremental.slot );
     511           0 :     ctx->predicted_incremental.dirty = 0;
     512           0 :   }
     513             : 
     514             :   /* Note: All state transitions should occur within this switch
     515             :      statement to make it easier to reason about the state management. */
     516             : 
     517             :   /* FIXME: Collapse WAITING_FOR_PEERS and COLLECTING_PEERS states for
     518             :      both full and incremental variants? */
     519             :   /* FIXME: Add INIT state so that we don't put the !download_enabled
     520             :      logic in waiting_for_peers, which is weird. */
     521             : 
     522           0 :   switch ( ctx->state ) {
     523             : 
     524             :     /* ============================================================== */
     525           0 :     case FD_SNAPCT_STATE_WAITING_FOR_PEERS: {
     526           0 :       if( FD_UNLIKELY( now>ctx->deadline_nanos ) ) FD_LOG_ERR(( "timed out waiting for peers." ));
     527             : 
     528           0 :       if( FD_UNLIKELY( !ctx->download_enabled ) ) {
     529           0 :         ulong local_slot = ctx->config.incremental_snapshots ? ctx->local_in.incremental_snapshot_slot : ctx->local_in.full_snapshot_slot;
     530           0 :         send_expected_slot( ctx, stem, local_slot );
     531           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 ));
     532           0 :         ctx->predicted_incremental.full_slot = ctx->local_in.full_snapshot_slot;
     533           0 :         ctx->state                           = FD_SNAPCT_STATE_READING_FULL_FILE;
     534           0 :         init_load( ctx, stem, 1, 1 );
     535           0 :         break;
     536           0 :       }
     537             : 
     538           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 0, ULONG_MAX );
     539           0 :       if( FD_LIKELY( best.addr.l ) ) {
     540           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS;
     541           0 :         ctx->deadline_nanos = now+FD_SNAPCT_COLLECTING_PEERS_TIMEOUT;
     542           0 :       }
     543           0 :       break;
     544           0 :     }
     545             : 
     546             :     /* ============================================================== */
     547           0 :     case FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL: {
     548             :       /* FIXME: Handle the case where we have no download peers enabled,
     549             :          boot off the local full snapshot but do not have a local incr. */
     550           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 0, ULONG_MAX );
     551           0 :       if( FD_LIKELY( best.addr.l ) ) {
     552           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
     553           0 :         ctx->deadline_nanos = now;
     554           0 :       }
     555           0 :       break;
     556           0 :     }
     557             : 
     558             :     /* ============================================================== */
     559           0 :     case FD_SNAPCT_STATE_COLLECTING_PEERS: {
     560           0 :       if( FD_UNLIKELY( !ctx->gossip.saturated && now<ctx->deadline_nanos ) ) break;
     561             : 
     562           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 0, ULONG_MAX );
     563           0 :       if( FD_UNLIKELY( !best.addr.l ) ) {
     564           0 :         ctx->state = FD_SNAPCT_STATE_WAITING_FOR_PEERS;
     565           0 :         break;
     566           0 :       }
     567             : 
     568           0 :       fd_sscluster_slot_t cluster = fd_sspeer_selector_cluster_slot( ctx->selector );
     569           0 :       if( FD_UNLIKELY( cluster.incremental==ULONG_MAX && ctx->config.incremental_snapshots ) ) {
     570             :         /* We must have a cluster full slot to be in this state. */
     571           0 :         FD_TEST( cluster.full!=ULONG_MAX );
     572             :         /* fall back to full snapshot only if the highest cluster slot
     573             :            is a full snapshot only */
     574           0 :         ctx->config.incremental_snapshots = 0;
     575           0 :       }
     576             : 
     577             :       /* FIXME: Revisit the local age logic with new effective age
     578             :          concept.  Measure cluster slot based on snapshots we can
     579             :          download / trust.  Reevaluate incremental age after the full
     580             :          snapshot download is completed. etc. etc. */
     581             : 
     582           0 :       ulong       cluster_slot    = ctx->config.incremental_snapshots ? cluster.incremental : cluster.full;
     583           0 :       ulong       local_slot      = ctx->config.incremental_snapshots ? ctx->local_in.incremental_snapshot_slot : ctx->local_in.full_snapshot_slot;
     584           0 :       ulong       local_slot_with_download = local_slot;
     585           0 :       int         local_too_old   = local_slot!=ULONG_MAX && local_slot<fd_ulong_sat_sub( cluster_slot, ctx->config.sources.max_local_incremental_age );
     586           0 :       int         local_full_only = ctx->local_in.incremental_snapshot_slot==ULONG_MAX && ctx->local_in.full_snapshot_slot!=ULONG_MAX;
     587           0 :       if( FD_LIKELY( (ctx->config.incremental_snapshots && local_full_only) || local_too_old ) ) {
     588           0 :         fd_sspeer_t best_incremental = fd_sspeer_selector_best( ctx->selector, 1, ctx->local_in.full_snapshot_slot );
     589           0 :         if( FD_LIKELY( best_incremental.addr.l ) ) {
     590           0 :           ctx->predicted_incremental.slot = best_incremental.ssinfo.incremental.slot;
     591           0 :           local_slot_with_download = best_incremental.ssinfo.incremental.slot;
     592           0 :           ctx->local_in.incremental_snapshot_slot = ULONG_MAX; /* don't use the local incremental snapshot */
     593           0 :         }
     594           0 :       }
     595             : 
     596           0 :       int can_use_local_full = local_slot_with_download!=ULONG_MAX && local_slot_with_download>=fd_ulong_sat_sub( cluster_slot, ctx->config.sources.max_local_full_effective_age );
     597           0 :       if( FD_LIKELY( can_use_local_full ) ) {
     598           0 :         send_expected_slot( ctx, stem, local_slot );
     599             : 
     600           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 ));
     601           0 :         ctx->predicted_incremental.full_slot = ctx->local_in.full_snapshot_slot;
     602           0 :         ctx->state                           = FD_SNAPCT_STATE_READING_FULL_FILE;
     603           0 :         init_load( ctx, stem, 1, 1 );
     604           0 :       } else {
     605           0 :         if( FD_UNLIKELY( !ctx->config.incremental_snapshots ) ) send_expected_slot( ctx, stem, best.ssinfo.full.slot );
     606             : 
     607           0 :         fd_sspeer_t best_incremental = fd_sspeer_selector_best( ctx->selector, 1, best.ssinfo.full.slot );
     608           0 :         if( FD_LIKELY( best_incremental.addr.l ) ) {
     609           0 :           ctx->predicted_incremental.slot = best_incremental.ssinfo.incremental.slot;
     610           0 :           send_expected_slot( ctx, stem, best_incremental.ssinfo.incremental.slot );
     611           0 :         }
     612             : 
     613           0 :         ctx->addr                            = best.addr;
     614           0 :         ctx->state                           = FD_SNAPCT_STATE_READING_FULL_HTTP;
     615           0 :         ctx->predicted_incremental.full_slot = best.ssinfo.full.slot;
     616           0 :         init_load( ctx, stem, 1, 0 );
     617           0 :         log_download( ctx, 1, best.addr, best.ssinfo.full.slot );
     618           0 :       }
     619           0 :       break;
     620           0 :     }
     621             : 
     622             :     /* ============================================================== */
     623           0 :     case FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL: {
     624           0 :       if( FD_UNLIKELY( now<ctx->deadline_nanos ) ) break;
     625             : 
     626           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
     627           0 :       if( FD_UNLIKELY( !best.addr.l ) ) {
     628           0 :         ctx->state = FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL;
     629           0 :         break;
     630           0 :       }
     631             : 
     632             :       /* FIXME: predicted_incremental? */
     633             : 
     634           0 :       ctx->addr = best.addr;
     635           0 :       ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP;
     636           0 :       init_load( ctx, stem, 0, 0 );
     637           0 :       log_download( ctx, 0, best.addr, best.ssinfo.incremental.slot );
     638           0 :       break;
     639           0 :     }
     640             : 
     641             :     /* ============================================================== */
     642           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE:
     643           0 :       if( !ctx->flush_ack ) break;
     644             : 
     645           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
     646           0 :         ctx->malformed = 0;
     647           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     648           0 :         ctx->flush_ack = 0;
     649           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET;
     650           0 :         FD_LOG_WARNING(( "error reading snapshot from local file `%s`", ctx->local_in.incremental_snapshot_path ));
     651           0 :         break;
     652           0 :       }
     653             : 
     654           0 :       ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
     655           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
     656           0 :       break;
     657             : 
     658             :     /* ============================================================== */
     659           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP:
     660           0 :       if( !ctx->flush_ack ) break;
     661             : 
     662           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
     663           0 :         ctx->malformed = 0;
     664           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     665           0 :         ctx->flush_ack = 0;
     666           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET;
     667           0 :         FD_LOG_WARNING(( "error downloading snapshot from http://" FD_IP4_ADDR_FMT ":%hu/incremental-snapshot.tar.bz2",
     668           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->addr.addr ), fd_ushort_bswap( ctx->addr.port ) ));
     669           0 :         fd_ssping_invalidate( ctx->ssping, ctx->addr, fd_log_wallclock() );
     670           0 :         fd_sspeer_selector_remove( ctx->selector, ctx->addr );
     671           0 :         break;
     672           0 :       }
     673             : 
     674           0 :       ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
     675           0 :       rename_snapshots( ctx );
     676           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
     677           0 :       break;
     678             : 
     679             :     /* ============================================================== */
     680           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE:
     681           0 :       if( !ctx->flush_ack ) break;
     682             : 
     683           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
     684           0 :         ctx->malformed = 0;
     685           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     686           0 :         ctx->flush_ack = 0;
     687           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET;
     688           0 :         FD_LOG_WARNING(( "error reading snapshot from local file `%s`", ctx->local_in.full_snapshot_path ));
     689           0 :         break;
     690           0 :       }
     691             : 
     692           0 :       if( FD_LIKELY( !ctx->config.incremental_snapshots ) ) {
     693           0 :         ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
     694           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
     695           0 :         break;
     696           0 :       }
     697             : 
     698           0 :       if( FD_LIKELY( ctx->local_in.incremental_snapshot_slot==ULONG_MAX ) ) {
     699           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
     700           0 :         ctx->deadline_nanos = 0L;
     701           0 :       } else {
     702           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 ));
     703           0 :         ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_FILE;
     704           0 :         init_load( ctx, stem, 0, 1 );
     705           0 :       }
     706           0 :       break;
     707             : 
     708             :     /* ============================================================== */
     709           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP:
     710           0 :       if( !ctx->flush_ack ) break;
     711             : 
     712           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
     713           0 :         ctx->malformed = 0;
     714           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     715           0 :         ctx->flush_ack = 0;
     716           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
     717           0 :         FD_LOG_WARNING(( "error downloading snapshot from http://" FD_IP4_ADDR_FMT ":%hu/snapshot.tar.bz2",
     718           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->addr.addr ), fd_ushort_bswap( ctx->addr.port ) ));
     719           0 :         fd_ssping_invalidate( ctx->ssping, ctx->addr, fd_log_wallclock() );
     720           0 :         fd_sspeer_selector_remove( ctx->selector, ctx->addr );
     721           0 :         break;
     722           0 :       }
     723             : 
     724           0 :       if( FD_LIKELY( !ctx->config.incremental_snapshots ) ) {
     725           0 :         ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
     726           0 :         rename_snapshots( ctx );
     727           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
     728           0 :         break;
     729           0 :       }
     730             : 
     731             :       /* Get the best incremental peer to download from */
     732           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
     733           0 :       if( FD_UNLIKELY( !best.addr.l ) ) {
     734             :         /* FIXME: We should just transition to collecting_peers_incremental
     735             :            here rather than failing the full snapshot? */
     736           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     737           0 :         ctx->flush_ack = 0;
     738           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
     739           0 :         break;
     740           0 :       }
     741             : 
     742           0 :       if( FD_UNLIKELY( ctx->predicted_incremental.slot!=best.ssinfo.incremental.slot ) ) {
     743           0 :         ctx->predicted_incremental.slot = best.ssinfo.incremental.slot;
     744           0 :         send_expected_slot( ctx, stem, best.ssinfo.incremental.slot );
     745           0 :       }
     746             : 
     747           0 :       ctx->addr = best.addr;
     748           0 :       ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP;
     749           0 :       init_load( ctx, stem, 0, 0 );
     750           0 :       log_download( ctx, 0, best.addr, best.ssinfo.incremental.slot );
     751           0 :       break;
     752             : 
     753             :     /* ============================================================== */
     754           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
     755           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET:
     756           0 :       if( !ctx->flush_ack ) break;
     757             : 
     758           0 :       ctx->metrics.full.bytes_read           = 0UL;
     759           0 :       ctx->metrics.full.bytes_written        = 0UL;
     760           0 :       ctx->metrics.full.bytes_total          = 0UL;
     761             : 
     762           0 :       ctx->metrics.incremental.bytes_read    = 0UL;
     763           0 :       ctx->metrics.incremental.bytes_written = 0UL;
     764           0 :       ctx->metrics.incremental.bytes_total   = 0UL;
     765             : 
     766           0 :       ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS;
     767           0 :       ctx->deadline_nanos = 0L;
     768           0 :       break;
     769             : 
     770             :     /* ============================================================== */
     771           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET:
     772           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
     773           0 :       if( !ctx->flush_ack ) break;
     774             : 
     775           0 :       ctx->metrics.incremental.bytes_read    = 0UL;
     776           0 :       ctx->metrics.incremental.bytes_written = 0UL;
     777           0 :       ctx->metrics.incremental.bytes_total   = 0UL;
     778             : 
     779           0 :       ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
     780           0 :       ctx->deadline_nanos = 0L;
     781           0 :       break;
     782             : 
     783             :     /* ============================================================== */
     784           0 :     case FD_SNAPCT_STATE_READING_FULL_FILE:
     785           0 :       if( FD_UNLIKELY( !ctx->flush_ack ) ) break;
     786           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
     787           0 :         ctx->malformed = 0;
     788           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     789           0 :         ctx->flush_ack = 0;
     790           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET;
     791           0 :         FD_LOG_WARNING(( "error reading snapshot from local file `%s`", ctx->local_in.full_snapshot_path ));
     792           0 :         break;
     793           0 :       }
     794           0 :       FD_TEST( ctx->metrics.full.bytes_total!=0UL );
     795           0 :       if( FD_UNLIKELY( ctx->metrics.full.bytes_read == ctx->metrics.full.bytes_total ) ) {
     796           0 :         ulong sig = ctx->config.incremental_snapshots ? FD_SNAPSHOT_MSG_CTRL_NEXT : FD_SNAPSHOT_MSG_CTRL_DONE;
     797           0 :         fd_stem_publish( stem, ctx->out_ld.idx, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
     798           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE;
     799           0 :         ctx->flush_ack = 0;
     800           0 :       }
     801           0 :       break;
     802             : 
     803             :     /* ============================================================== */
     804           0 :     case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE:
     805           0 :       if( FD_UNLIKELY( !ctx->flush_ack ) ) break;
     806           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
     807           0 :         ctx->malformed = 0;
     808           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     809           0 :         ctx->flush_ack = 0;
     810           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET;
     811           0 :         FD_LOG_WARNING(( "error reading snapshot from local file `%s`", ctx->local_in.incremental_snapshot_path ));
     812           0 :         break;
     813           0 :       }
     814           0 :       FD_TEST( ctx->metrics.incremental.bytes_total!=0UL );
     815           0 :       if ( FD_UNLIKELY( ctx->metrics.incremental.bytes_read == ctx->metrics.incremental.bytes_total ) ) {
     816           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_DONE, 0UL, 0UL, 0UL, 0UL, 0UL );
     817           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE;
     818           0 :         ctx->flush_ack = 0;
     819           0 :       }
     820           0 :       break;
     821             : 
     822             :     /* ============================================================== */
     823           0 :     case FD_SNAPCT_STATE_READING_FULL_HTTP:
     824           0 :       if( FD_UNLIKELY( !ctx->flush_ack ) ) break;
     825           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
     826           0 :         ctx->malformed = 0;
     827           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     828           0 :         ctx->flush_ack = 0;
     829           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
     830           0 :         FD_LOG_WARNING(( "error downloading snapshot from http://" FD_IP4_ADDR_FMT ":%hu/snapshot.tar.bz2",
     831           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->addr.addr ), fd_ushort_bswap( ctx->addr.port ) ));
     832           0 :         fd_ssping_invalidate( ctx->ssping, ctx->addr, fd_log_wallclock() );
     833           0 :         fd_sspeer_selector_remove( ctx->selector, ctx->addr );
     834           0 :         break;
     835           0 :       }
     836           0 :       if( FD_UNLIKELY( ctx->metrics.full.bytes_total!=0UL && ctx->metrics.full.bytes_read==ctx->metrics.full.bytes_total ) ) {
     837           0 :         ulong sig = ctx->config.incremental_snapshots ? FD_SNAPSHOT_MSG_CTRL_NEXT : FD_SNAPSHOT_MSG_CTRL_DONE;
     838           0 :         fd_stem_publish( stem, ctx->out_ld.idx, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
     839           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP;
     840           0 :         ctx->flush_ack = 0;
     841           0 :       }
     842           0 :       break;
     843             : 
     844             :     /* ============================================================== */
     845           0 :     case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP:
     846           0 :       if( FD_UNLIKELY( !ctx->flush_ack ) ) break;
     847           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
     848           0 :         ctx->malformed = 0;
     849           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
     850           0 :         ctx->flush_ack = 0;
     851           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET;
     852           0 :         FD_LOG_WARNING(( "error downloading snapshot from http://" FD_IP4_ADDR_FMT ":%hu/incremental-snapshot.tar.bz2",
     853           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->addr.addr ), fd_ushort_bswap( ctx->addr.port ) ));
     854           0 :         fd_ssping_invalidate( ctx->ssping, ctx->addr, fd_log_wallclock() );
     855           0 :         fd_sspeer_selector_remove( ctx->selector, ctx->addr );
     856           0 :         break;
     857           0 :       }
     858           0 :       if ( FD_UNLIKELY( ctx->metrics.incremental.bytes_total!=0UL && ctx->metrics.incremental.bytes_read==ctx->metrics.incremental.bytes_total ) ) {
     859           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_DONE, 0UL, 0UL, 0UL, 0UL, 0UL );
     860           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP;
     861           0 :         ctx->flush_ack = 0;
     862           0 :       }
     863           0 :       break;
     864             : 
     865             :     /* ============================================================== */
     866           0 :     case FD_SNAPCT_STATE_SHUTDOWN:
     867           0 :       break;
     868             : 
     869             :     /* ============================================================== */
     870           0 :     default: FD_LOG_ERR(( "unexpected state %d", ctx->state ));
     871           0 :   }
     872           0 : }
     873             : 
     874             : static void
     875             : gossip_frag( fd_snapct_tile_t *  ctx,
     876             :              ulong               sig,
     877             :              ulong               sz FD_PARAM_UNUSED,
     878           0 :              ulong               chunk ) {
     879           0 :   FD_TEST( ctx->gossip_enabled );
     880             : 
     881           0 :   if( !( sig==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ||
     882           0 :          sig==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE ||
     883           0 :          sig==FD_GOSSIP_UPDATE_TAG_SNAPSHOT_HASHES ) ) return;
     884             : 
     885           0 :   fd_gossip_update_message_t const * msg    = fd_chunk_to_laddr_const( ctx->gossip_in_mem, chunk );
     886           0 :   fd_pubkey_t const *                pubkey = (fd_pubkey_t const *)msg->origin_pubkey;
     887           0 :   switch( msg->tag ) {
     888           0 :     case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO: {
     889           0 :       FD_TEST( msg->contact_info.idx<GOSSIP_PEERS_MAX );
     890           0 :       gossip_ci_entry_t * entry = ctx->gossip.ci_table + msg->contact_info.idx;
     891           0 :       if( FD_UNLIKELY( !fd_pubkey_eq( &entry->pubkey, pubkey ) ) ) {
     892             :         /* Initialize the new gossip entry, which may or may not be allowed */
     893           0 :         FD_TEST( fd_pubkey_check_zero( &entry->pubkey ) );
     894           0 :         entry->pubkey      = *pubkey;
     895           0 :         entry->rpc_addr.l  = 0UL;
     896           0 :         entry->added_nanos = fd_log_wallclock();
     897           0 :         if( ctx->config.sources.gossip.allow_any ) {
     898           0 :           entry->allowed = 1;
     899           0 :           for( ulong i=0UL; i<ctx->config.sources.gossip.block_list_cnt; i++ ) {
     900           0 :             if( fd_pubkey_eq( pubkey, &ctx->config.sources.gossip.block_list[ i ] ) ) {
     901           0 :               entry->allowed = 0;
     902           0 :               break;
     903           0 :             }
     904           0 :           }
     905           0 :         } else {
     906           0 :           entry->allowed = 0;
     907           0 :           for( ulong i=0UL; i<ctx->config.sources.gossip.allow_list_cnt; i++ ) {
     908           0 :             if( fd_pubkey_eq( pubkey, &ctx->config.sources.gossip.allow_list[ i ] ) ) {
     909           0 :               entry->allowed = 1;
     910           0 :               break;
     911           0 :             }
     912           0 :           }
     913           0 :         }
     914           0 :         FD_TEST(  ULONG_MAX==gossip_ci_map_idx_query_const( ctx->gossip.ci_map, pubkey, ULONG_MAX, ctx->gossip.ci_table ) );
     915           0 :         if( entry->allowed ) gossip_ci_map_idx_insert( ctx->gossip.ci_map, msg->contact_info.idx, ctx->gossip.ci_table );
     916           0 :       }
     917           0 :       if( !entry->allowed ) break;
     918             :       /* Maybe update the RPC address of a new or existing allowed gossip peer */
     919           0 :       fd_ip4_port_t cur_addr = entry->rpc_addr;
     920           0 :       fd_ip4_port_t new_addr = msg->contact_info.contact_info->sockets[ FD_CONTACT_INFO_SOCKET_RPC ];
     921           0 :       if( FD_UNLIKELY( new_addr.l!=cur_addr.l ) ) {
     922           0 :         entry->rpc_addr = new_addr;
     923           0 :         if( FD_LIKELY( !!cur_addr.l ) ) {
     924           0 :           int removed = fd_ssping_remove( ctx->ssping, cur_addr );
     925           0 :           if( FD_LIKELY( removed ) ) fd_sspeer_selector_remove( ctx->selector, cur_addr );
     926           0 :         }
     927           0 :         if( FD_LIKELY( !!new_addr.l ) ) fd_ssping_add( ctx->ssping, new_addr );
     928           0 :         if( !ctx->config.sources.gossip.allow_any ) {
     929           0 :           FD_BASE58_ENCODE_32_BYTES( pubkey->uc, pubkey_b58 );
     930           0 :           if( FD_LIKELY( !!new_addr.l ) ) {
     931           0 :             FD_LOG_NOTICE(( "allowed gossip peer added with public key `%s` and RPC address `" FD_IP4_ADDR_FMT ":%hu`",
     932           0 :                             pubkey_b58, FD_IP4_ADDR_FMT_ARGS( new_addr.addr ), fd_ushort_bswap( new_addr.port ) ));
     933           0 :           } else {
     934           0 :             FD_LOG_WARNING(( "allowed gossip peer with public key `%s` does not advertise an RPC address", pubkey_b58 ));
     935           0 :           }
     936           0 :         }
     937           0 :       }
     938           0 :       break;
     939           0 :     }
     940           0 :     case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE: {
     941           0 :       FD_TEST( msg->contact_info.idx<GOSSIP_PEERS_MAX );
     942           0 :       gossip_ci_entry_t * entry = ctx->gossip.ci_table + msg->contact_info.idx;
     943           0 :       if( FD_UNLIKELY( !fd_pubkey_eq( &entry->pubkey, pubkey ) ) ) {
     944           0 :         FD_TEST( fd_pubkey_check_zero( &entry->pubkey ) );
     945           0 :         break;
     946           0 :       }
     947           0 :       ulong rem_idx = gossip_ci_map_idx_remove( ctx->gossip.ci_map, pubkey, ULONG_MAX, ctx->gossip.ci_table );
     948           0 :       if( rem_idx==ULONG_MAX ) break;
     949           0 :       FD_TEST( entry->allowed && rem_idx==msg->contact_info.idx );
     950           0 :       fd_ip4_port_t addr = entry->rpc_addr;
     951           0 :       if( FD_LIKELY( !!addr.l ) ) {
     952           0 :         int removed = fd_ssping_remove( ctx->ssping, addr );
     953           0 :         if( FD_LIKELY( removed ) ) fd_sspeer_selector_remove( ctx->selector, addr );
     954           0 :       }
     955           0 :       if( !ctx->config.sources.gossip.allow_any ) {
     956           0 :         FD_BASE58_ENCODE_32_BYTES( pubkey->uc, pubkey_b58 );
     957           0 :         FD_LOG_WARNING(( "allowed gossip peer removed with public key `%s` and RPC address `" FD_IP4_ADDR_FMT ":%hu`",
     958           0 :                          pubkey_b58, FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ) ));
     959           0 :       }
     960           0 :       fd_memset( entry, 0, sizeof(*entry) );
     961           0 :       break;
     962           0 :     }
     963           0 :     case FD_GOSSIP_UPDATE_TAG_SNAPSHOT_HASHES: {
     964           0 :       ulong idx = gossip_ci_map_idx_query_const( ctx->gossip.ci_map, pubkey, ULONG_MAX, ctx->gossip.ci_table );
     965           0 :       if( FD_LIKELY( idx!=ULONG_MAX ) ) {
     966           0 :         gossip_ci_entry_t * entry = ctx->gossip.ci_table + idx;
     967           0 :         FD_TEST( entry->allowed );
     968           0 :         on_snapshot_hash( ctx, entry->rpc_addr, msg );
     969           0 :       }
     970           0 :       break;
     971           0 :     }
     972           0 :     default:
     973           0 :       FD_LOG_ERR(( "snapct: unexpected gossip tag %u", (uint)msg->tag ));
     974           0 :       break;
     975           0 :   }
     976           0 : }
     977             : 
     978             : static void
     979             : snapld_frag( fd_snapct_tile_t *  ctx,
     980             :              ulong               sig,
     981             :              ulong               sz,
     982             :              ulong               chunk,
     983           0 :              fd_stem_context_t * stem ) {
     984           0 :   if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_META ) ) {
     985             :     /* Before snapld starts sending down data fragments, it first sends
     986             :        a metadata message containing the total size of the snapshot as
     987             :        well as the filename.  This is only done for HTTP loading. */
     988           0 :     int full;
     989           0 :     switch( ctx->state ) {
     990           0 :       case FD_SNAPCT_STATE_READING_FULL_HTTP:        full = 1; break;
     991           0 :       case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP: full = 0; break;
     992             : 
     993           0 :       case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
     994           0 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
     995           0 :         return; /* Ignore */
     996           0 :       default: FD_LOG_ERR(( "invalid meta frag in state %d", ctx->state ));
     997           0 :     }
     998             : 
     999           0 :     FD_TEST( sz==sizeof(fd_ssctrl_meta_t) );
    1000           0 :     fd_ssctrl_meta_t const * meta = fd_chunk_to_laddr_const( ctx->snapld_in_mem, chunk );
    1001             : 
    1002           0 :     fd_memcpy( full ? ctx->http_full_snapshot_name : ctx->http_incr_snapshot_name, meta->name, PATH_MAX );
    1003             : 
    1004           0 :     if( FD_LIKELY( !!ctx->out_gui.mem ) ) {
    1005           0 :       char snapshot_path[ PATH_MAX+30UL ]; /* 30 is fd_cstr_nlen( "https://255.255.255.255:65536/", ULONG_MAX ) */
    1006           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->addr.addr ), fd_ushort_bswap( ctx->addr.port ), meta->name ) );
    1007           0 :       snapshot_path_gui_publish( ctx, stem, snapshot_path, full );
    1008           0 :     }
    1009             : 
    1010           0 :     if( full ) ctx->metrics.full.bytes_total        = meta->total_sz;
    1011           0 :     else       ctx->metrics.incremental.bytes_total = meta->total_sz;
    1012             : 
    1013           0 :     return;
    1014           0 :   }
    1015           0 :   if( FD_UNLIKELY( sig!=FD_SNAPSHOT_MSG_DATA ) ) return;
    1016             : 
    1017           0 :   int full, file;
    1018           0 :   switch( ctx->state ) {
    1019             :     /* Expected cases, fall through below */
    1020           0 :     case FD_SNAPCT_STATE_READING_FULL_FILE:        full = 1; file = 1; break;
    1021           0 :     case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE: full = 0; file = 1; break;
    1022           0 :     case FD_SNAPCT_STATE_READING_FULL_HTTP:        full = 1; file = 0; break;
    1023           0 :     case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP: full = 0; file = 0; break;
    1024             : 
    1025           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET:
    1026           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET:
    1027           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
    1028           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
    1029             :       /* We are waiting for a reset to fully propagate through the
    1030             :          pipeline, just throw away any trailing data frags. */
    1031           0 :       return;
    1032             : 
    1033           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE:
    1034           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE:
    1035           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP:
    1036           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP:
    1037             :       /* Based on previously received data frags, we expected that the
    1038             :          current full / incremental snapshot was finished, but then we
    1039             :          received additional data frags.  Unsafe to continue so throw
    1040             :          away the whole snapshot. */
    1041           0 :       if( !ctx->malformed ) {
    1042           0 :         ctx->malformed = 1;
    1043           0 :         FD_LOG_WARNING(( "complete snapshot loaded but read %lu extra bytes", sz ));
    1044           0 :       }
    1045           0 :       return;
    1046             : 
    1047           0 :     case FD_SNAPCT_STATE_WAITING_FOR_PEERS:
    1048           0 :     case FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL:
    1049           0 :     case FD_SNAPCT_STATE_COLLECTING_PEERS:
    1050           0 :     case FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL:
    1051           0 :     case FD_SNAPCT_STATE_SHUTDOWN:
    1052           0 :     default:
    1053           0 :       FD_LOG_ERR(( "invalid data frag in state %d", ctx->state ));
    1054           0 :       return;
    1055           0 :   }
    1056             : 
    1057           0 :   if( full ) FD_TEST( ctx->metrics.full.bytes_total       !=0UL );
    1058           0 :   else       FD_TEST( ctx->metrics.incremental.bytes_total!=0UL );
    1059             : 
    1060           0 :   if( full ) ctx->metrics.full.bytes_read        += sz;
    1061           0 :   else       ctx->metrics.incremental.bytes_read += sz;
    1062             : 
    1063           0 :   if( !file && -1!=ctx->local_out.dir_fd ) {
    1064           0 :     uchar const * data = fd_chunk_to_laddr_const( ctx->snapld_in_mem, chunk );
    1065           0 :     int fd = full ? ctx->local_out.full_snapshot_fd : ctx->local_out.incremental_snapshot_fd;
    1066           0 :     long result = write( fd, data, sz );
    1067           0 :     if( FD_UNLIKELY( -1==result && errno==ENOSPC ) ) {
    1068           0 :       FD_LOG_ERR(( "Out of disk space when writing out snapshot data to `%s`", ctx->config.snapshots_path ));
    1069           0 :     } else if( FD_UNLIKELY( 0L>result ) ) {
    1070           0 :       FD_LOG_ERR(( "write() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
    1071           0 :     } else if( FD_UNLIKELY( sz!=(ulong)result ) ) {
    1072           0 :       FD_LOG_ERR(( "paritial write(%lu)=%ld", sz, result ));
    1073           0 :     }
    1074           0 :     if( full ) ctx->metrics.full.bytes_written        += sz;
    1075           0 :     else       ctx->metrics.incremental.bytes_written += sz;
    1076           0 :   }
    1077             : 
    1078           0 :   if( FD_UNLIKELY( ( full && ctx->metrics.full.bytes_read        > ctx->metrics.full.bytes_total ) ||
    1079           0 :                    (!full && ctx->metrics.incremental.bytes_read > ctx->metrics.incremental.bytes_total ) ) ) {
    1080           0 :     if( !ctx->malformed ) {
    1081           0 :       ctx->malformed = 1;
    1082           0 :       FD_LOG_WARNING(( "expected %s snapshot size of %lu bytes but read %lu bytes",
    1083           0 :                        full ? "full" : "incremental",
    1084           0 :                        full ? ctx->metrics.full.bytes_total : ctx->metrics.incremental.bytes_total,
    1085           0 :                        full ? ctx->metrics.full.bytes_read  : ctx->metrics.incremental.bytes_read ));
    1086             : 
    1087           0 :     }
    1088           0 :   }
    1089           0 : }
    1090             : 
    1091             : static void
    1092             : snapin_frag( fd_snapct_tile_t *  ctx,
    1093           0 :              ulong               sig ) {
    1094           0 :   switch( sig ) {
    1095           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
    1096           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_READING_FULL_HTTP ||
    1097           0 :                      ctx->state==FD_SNAPCT_STATE_READING_FULL_FILE ) ) {
    1098           0 :         FD_TEST( !ctx->flush_ack );
    1099           0 :         ctx->flush_ack = 1;
    1100           0 :       } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
    1101           0 :       break;
    1102             : 
    1103           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_INCR:
    1104           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP ||
    1105           0 :                      ctx->state==FD_SNAPCT_STATE_READING_INCREMENTAL_FILE ) ) {
    1106           0 :         FD_TEST( !ctx->flush_ack );
    1107           0 :         ctx->flush_ack = 1;
    1108           0 :       } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
    1109           0 :       break;
    1110             : 
    1111           0 :     case FD_SNAPSHOT_MSG_CTRL_NEXT:
    1112           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP ||
    1113           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE ) ) {
    1114           0 :         FD_TEST( !ctx->flush_ack );
    1115           0 :         ctx->flush_ack = 1;
    1116           0 :       } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
    1117           0 :       break;
    1118             : 
    1119           0 :     case FD_SNAPSHOT_MSG_CTRL_DONE:
    1120           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP ||
    1121           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE ||
    1122           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP ||
    1123           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE ) ) {
    1124           0 :         FD_TEST( !ctx->flush_ack );
    1125           0 :         ctx->flush_ack = 1;
    1126           0 :       } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
    1127           0 :       break;
    1128             : 
    1129           0 :     case FD_SNAPSHOT_MSG_CTRL_FAIL:
    1130           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET ||
    1131           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ||
    1132           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET ||
    1133           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ) {
    1134           0 :         FD_TEST( !ctx->flush_ack );
    1135           0 :         ctx->flush_ack = 1;
    1136           0 :       } else FD_LOG_ERR(( "invalid control frag %lu in state %d", sig, ctx->state ));
    1137           0 :       break;
    1138             : 
    1139           0 :     case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:
    1140           0 :       break;
    1141             : 
    1142           0 :     case FD_SNAPSHOT_MSG_CTRL_ERROR:
    1143           0 :       switch( ctx->state ) {
    1144           0 :         case FD_SNAPCT_STATE_READING_FULL_FILE:
    1145           0 :         case FD_SNAPCT_STATE_FLUSHING_FULL_FILE:
    1146           0 :         case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE:
    1147           0 :         case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE:
    1148           0 :         case FD_SNAPCT_STATE_READING_FULL_HTTP:
    1149           0 :         case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP:
    1150           0 :         case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP:
    1151           0 :         case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP:
    1152           0 :           ctx->malformed = 1;
    1153           0 :           break;
    1154           0 :         default:
    1155           0 :           break;
    1156           0 :       }
    1157           0 :       break;
    1158           0 :   }
    1159           0 : }
    1160             : 
    1161             : static int
    1162             : returnable_frag( fd_snapct_tile_t *  ctx,
    1163             :                  ulong               in_idx,
    1164             :                  ulong               seq    FD_PARAM_UNUSED,
    1165             :                  ulong               sig,
    1166             :                  ulong               chunk,
    1167             :                  ulong               sz,
    1168             :                  ulong               ctl    FD_PARAM_UNUSED,
    1169             :                  ulong               tsorig FD_PARAM_UNUSED,
    1170             :                  ulong               tspub  FD_PARAM_UNUSED,
    1171           0 :                  fd_stem_context_t * stem ) {
    1172           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
    1173           0 :     gossip_frag( ctx, sig, sz, chunk );
    1174           0 :   } else if( ctx->in_kind[ in_idx ]==IN_KIND_SNAPLD ) {
    1175           0 :     snapld_frag( ctx, sig, sz, chunk, stem );
    1176           0 :   } else if( ctx->in_kind[ in_idx ]==IN_KIND_ACK ) {
    1177           0 :     snapin_frag( ctx, sig );
    1178           0 :   } else FD_LOG_ERR(( "invalid in_kind %lu %u", in_idx, (uint)ctx->in_kind[ in_idx ] ));
    1179           0 :   return 0;
    1180           0 : }
    1181             : 
    1182             : static void
    1183             : privileged_init( fd_topo_t *      topo,
    1184           0 :                  fd_topo_tile_t * tile ) {
    1185           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1186             : 
    1187           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1188           0 :   fd_snapct_tile_t * ctx         = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t),  sizeof(fd_snapct_tile_t) );
    1189           0 :   void *             _ssping     = FD_SCRATCH_ALLOC_APPEND( l, fd_ssping_align(),          fd_ssping_footprint( TOTAL_PEERS_MAX ) );
    1190           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t)*GOSSIP_PEERS_MAX );
    1191           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, gossip_ci_map_align(),      gossip_ci_map_footprint( gossip_ci_map_chain_cnt_est( GOSSIP_PEERS_MAX ) ) );
    1192           0 :   void *             _ssresolver = FD_SCRATCH_ALLOC_APPEND( l, fd_http_resolver_align(),   fd_http_resolver_footprint( SERVER_PEERS_MAX )  );
    1193           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX ) );
    1194             : 
    1195           0 : #if FD_HAS_OPENSSL
    1196           0 :   void * _alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
    1197           0 :   fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), tile->kind_id );
    1198           0 :   fd_ossl_tile_init( alloc );
    1199           0 : #endif
    1200             : 
    1201           0 :   ctx->ssping = NULL;
    1202           0 :   if( FD_LIKELY( download_enabled( tile ) ) )         ctx->ssping = fd_ssping_join( fd_ssping_new( _ssping, TOTAL_PEERS_MAX, 1UL, on_ping, ctx ) );
    1203           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 ) );
    1204           0 :   else                                                ctx->ssresolver = NULL;
    1205             : 
    1206             :   /* FIXME: We will keep too many snapshots if we have local snapshots
    1207             :      but elect not to use them due to their age. */
    1208           0 :   fd_ssarchive_remove_old_snapshots( tile->snapct.snapshots_path,
    1209           0 :                                      tile->snapct.max_full_snapshots_to_keep,
    1210           0 :                                      tile->snapct.max_incremental_snapshots_to_keep );
    1211             : 
    1212           0 :   ulong full_slot = ULONG_MAX;
    1213           0 :   ulong incremental_slot = ULONG_MAX;
    1214           0 :   int full_is_zstd = 0;
    1215           0 :   int incremental_is_zstd = 0;
    1216           0 :   char full_path[ PATH_MAX ] = {0};
    1217           0 :   char incremental_path[ PATH_MAX ] = {0};
    1218           0 :   if( FD_UNLIKELY( -1==fd_ssarchive_latest_pair( tile->snapct.snapshots_path,
    1219           0 :                                                  tile->snapct.incremental_snapshots,
    1220           0 :                                                  &full_slot,
    1221           0 :                                                  &incremental_slot,
    1222           0 :                                                  full_path,
    1223           0 :                                                  incremental_path,
    1224           0 :                                                  &full_is_zstd,
    1225           0 :                                                  &incremental_is_zstd ) ) ) {
    1226           0 :     if( FD_UNLIKELY( !download_enabled( tile ) ) ) {
    1227           0 :       FD_LOG_ERR(( "No snapshots found in `%s` and no download sources are enabled. "
    1228           0 :                    "Please enable downloading via [snapshots.sources] and restart.", tile->snapct.snapshots_path ));
    1229           0 :     }
    1230           0 :     ctx->local_in.full_snapshot_slot        = ULONG_MAX;
    1231           0 :     ctx->local_in.incremental_snapshot_slot = ULONG_MAX;
    1232           0 :     ctx->local_in.full_snapshot_size        = 0UL;
    1233           0 :     ctx->local_in.incremental_snapshot_size = 0UL;
    1234           0 :     ctx->local_in.full_snapshot_zstd        = 0;
    1235           0 :     ctx->local_in.incremental_snapshot_zstd = 0;
    1236           0 :     fd_cstr_fini( ctx->local_in.full_snapshot_path );
    1237           0 :     fd_cstr_fini( ctx->local_in.incremental_snapshot_path );
    1238           0 :   } else {
    1239           0 :     FD_TEST( full_slot!=ULONG_MAX );
    1240             : 
    1241           0 :     ctx->local_in.full_snapshot_slot        = full_slot;
    1242           0 :     ctx->local_in.incremental_snapshot_slot = incremental_slot;
    1243           0 :     ctx->local_in.full_snapshot_zstd        = full_is_zstd;
    1244           0 :     ctx->local_in.incremental_snapshot_zstd = incremental_is_zstd;
    1245             : 
    1246           0 :     strncpy( ctx->local_in.full_snapshot_path, full_path, PATH_MAX );
    1247           0 :     struct stat full_stat;
    1248           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 ) ));
    1249           0 :     if( FD_UNLIKELY( !S_ISREG( full_stat.st_mode ) ) ) FD_LOG_ERR(( "full snapshot path `%s` is not a regular file", full_path ));
    1250           0 :     ctx->local_in.full_snapshot_size = (ulong)full_stat.st_size;
    1251             : 
    1252           0 :     if( FD_LIKELY( incremental_slot!=ULONG_MAX ) ) {
    1253           0 :       strncpy( ctx->local_in.incremental_snapshot_path, incremental_path, PATH_MAX );
    1254           0 :       struct stat incremental_stat;
    1255           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 ) ));
    1256           0 :       if( FD_UNLIKELY( !S_ISREG( incremental_stat.st_mode ) ) ) FD_LOG_ERR(( "incremental snapshot path `%s` is not a regular file", incremental_path ));
    1257           0 :       ctx->local_in.incremental_snapshot_size = (ulong)incremental_stat.st_size;
    1258           0 :     } else {
    1259           0 :       ctx->local_in.incremental_snapshot_size = 0UL;
    1260           0 :       fd_cstr_fini( ctx->local_in.incremental_snapshot_path );
    1261           0 :     }
    1262           0 :   }
    1263             : 
    1264           0 :   ctx->local_out.dir_fd                  = -1;
    1265           0 :   ctx->local_out.full_snapshot_fd        = -1;
    1266           0 :   ctx->local_out.incremental_snapshot_fd = -1;
    1267           0 :   if( FD_LIKELY( download_enabled( tile ) ) ) {
    1268           0 :     ctx->local_out.dir_fd = open( tile->snapct.snapshots_path, O_DIRECTORY|O_CLOEXEC );
    1269           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 ) ));
    1270             : 
    1271           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 );
    1272           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 ) ));
    1273             : 
    1274           0 :     if( FD_LIKELY( tile->snapct.incremental_snapshots ) ) {
    1275           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 );
    1276           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 ) ));
    1277           0 :     }
    1278           0 :   }
    1279           0 : }
    1280             : 
    1281             : static inline fd_snapct_out_link_t
    1282             : out1( fd_topo_t const *      topo,
    1283             :       fd_topo_tile_t const * tile,
    1284           0 :       char const *           name ) {
    1285           0 :   ulong idx = ULONG_MAX;
    1286             : 
    1287           0 :   for( ulong i=0UL; i<tile->out_cnt; i++ ) {
    1288           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
    1289           0 :     if( !strcmp( link->name, name ) ) {
    1290           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 ));
    1291           0 :       idx = i;
    1292           0 :     }
    1293           0 :   }
    1294             : 
    1295           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 };
    1296             : 
    1297           0 :   ulong mtu = topo->links[ tile->out_link_id[ idx ] ].mtu;
    1298           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 };
    1299             : 
    1300           0 :   void * mem   = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
    1301           0 :   ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
    1302           0 :   ulong wmark  = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, mtu );
    1303           0 :   return (fd_snapct_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0, .mtu = mtu };
    1304           0 : }
    1305             : 
    1306             : static void
    1307             : unprivileged_init( fd_topo_t *      topo,
    1308           0 :                    fd_topo_tile_t * tile ) {
    1309           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1310             : 
    1311           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1312           0 :   fd_snapct_tile_t * ctx  = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t),  sizeof(fd_snapct_tile_t)       );
    1313           0 :                             FD_SCRATCH_ALLOC_APPEND( l, fd_ssping_align(),          fd_ssping_footprint( TOTAL_PEERS_MAX ) );
    1314           0 :   void * _ci_table        = FD_SCRATCH_ALLOC_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX );
    1315           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 ) ) );
    1316           0 :                             FD_SCRATCH_ALLOC_APPEND( l, fd_http_resolver_align(),   fd_http_resolver_footprint( SERVER_PEERS_MAX ) );
    1317           0 :   void * _selector        = FD_SCRATCH_ALLOC_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX ) );
    1318             : 
    1319           0 :   fd_memcpy( &ctx->config, &tile->snapct, sizeof(ctx->config) );
    1320           0 :   ctx->gossip_enabled   = gossip_enabled( tile );
    1321           0 :   ctx->download_enabled = download_enabled( tile );
    1322             : 
    1323           0 :   if( ctx->config.sources.servers_cnt ) {
    1324           0 :     for( ulong i=0UL; i<tile->snapct.sources.servers_cnt; i++ ) {
    1325           0 :       fd_ssping_add       ( ctx->ssping, tile->snapct.sources.servers[ i ].addr );
    1326           0 :       fd_http_resolver_add( ctx->ssresolver,
    1327           0 :                             tile->snapct.sources.servers[ i ].addr,
    1328           0 :                             tile->snapct.sources.servers[ i ].hostname,
    1329           0 :                             tile->snapct.sources.servers[ i ].is_https );
    1330           0 :     }
    1331           0 :   }
    1332             : 
    1333           0 :   ctx->selector = fd_sspeer_selector_join( fd_sspeer_selector_new( _selector, TOTAL_PEERS_MAX, ctx->config.incremental_snapshots, 1UL ) );
    1334             : 
    1335           0 :   ctx->state          = FD_SNAPCT_STATE_WAITING_FOR_PEERS;
    1336           0 :   ctx->malformed      = 0;
    1337           0 :   ctx->deadline_nanos = fd_log_wallclock() + FD_SNAPCT_WAITING_FOR_PEERS_TIMEOUT;
    1338           0 :   ctx->flush_ack      = 0;
    1339           0 :   ctx->addr.l         = 0UL;
    1340             : 
    1341           0 :   fd_memset( ctx->http_full_snapshot_name, 0, PATH_MAX );
    1342           0 :   fd_memset( ctx->http_incr_snapshot_name, 0, PATH_MAX );
    1343             : 
    1344           0 :   ctx->gossip_in_mem = NULL;
    1345           0 :   int has_snapld_dc = 0, has_ack_loopback = 0;
    1346           0 :   FD_TEST( tile->in_cnt<=MAX_IN_LINKS );
    1347           0 :   for( ulong i=0UL; i<(tile->in_cnt); i++ ) {
    1348           0 :     fd_topo_link_t * in_link = &topo->links[ tile->in_link_id[ i ] ];
    1349           0 :     if( 0==strcmp( in_link->name, "gossip_out" ) ) {
    1350           0 :       ctx->in_kind[ i ]  = IN_KIND_GOSSIP;
    1351           0 :       ctx->gossip_in_mem = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
    1352           0 :     } else if( 0==strcmp( in_link->name, "snapld_dc" ) ) {
    1353           0 :       ctx->in_kind[ i ]  = IN_KIND_SNAPLD;
    1354           0 :       ctx->snapld_in_mem = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
    1355           0 :       FD_TEST( !has_snapld_dc );
    1356           0 :       has_snapld_dc = 1;
    1357           0 :     } else if( 0==strcmp( in_link->name, "snapin_ct" ) || 0==strcmp( in_link->name, "snapls_ct" ) ) {
    1358           0 :       ctx->in_kind[ i ] = IN_KIND_ACK;
    1359           0 :       FD_TEST( !has_ack_loopback );
    1360           0 :       has_ack_loopback = 1;
    1361           0 :     }
    1362           0 :   }
    1363           0 :   FD_TEST( has_snapld_dc && has_ack_loopback );
    1364           0 :   FD_TEST( ctx->gossip_enabled==(ctx->gossip_in_mem!=NULL) );
    1365             : 
    1366           0 :   ctx->predicted_incremental.full_slot = ULONG_MAX;
    1367           0 :   ctx->predicted_incremental.slot      = ULONG_MAX;
    1368           0 :   ctx->predicted_incremental.dirty     = 0;
    1369             : 
    1370           0 :   fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
    1371             : 
    1372           0 :   fd_memset( _ci_table, 0, sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX );
    1373           0 :   ctx->gossip.ci_table             = _ci_table;
    1374           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 ) );
    1375           0 :   ctx->gossip.fresh_cnt            = 0UL;
    1376           0 :   ctx->gossip.total_cnt            = 0UL;
    1377           0 :   ctx->gossip.saturated            = !ctx->gossip_enabled;
    1378           0 :   ctx->gossip.next_saturated_check = 0;
    1379             : 
    1380           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 ));
    1381           0 :   ctx->out_ld  = out1( topo, tile, "snapct_ld"   );
    1382           0 :   ctx->out_gui = out1( topo, tile, "snapct_gui"  );
    1383           0 :   ctx->out_rp  = out1( topo, tile, "snapct_repr" );
    1384           0 : }
    1385             : 
    1386             : /* after_credit can result in as many as 5 stem publishes in some code
    1387             :    paths, and returnable_frag can result in 1. */
    1388           0 : #define STEM_BURST 6UL
    1389             : 
    1390           0 : #define STEM_LAZY  1000L
    1391             : 
    1392           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_snapct_tile_t
    1393           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapct_tile_t)
    1394             : 
    1395             : #define STEM_CALLBACK_SHOULD_SHUTDOWN     should_shutdown
    1396           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
    1397           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
    1398           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
    1399           0 : #define STEM_CALLBACK_RETURNABLE_FRAG     returnable_frag
    1400             : 
    1401             : #include "../../disco/stem/fd_stem.c"
    1402             : 
    1403             : fd_topo_run_tile_t fd_tile_snapct = {
    1404             :   .name                     = NAME,
    1405             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
    1406             :   .populate_allowed_seccomp = populate_allowed_seccomp,
    1407             :   .populate_allowed_fds     = populate_allowed_fds,
    1408             :   .scratch_align            = scratch_align,
    1409             :   .scratch_footprint        = scratch_footprint,
    1410             :   .loose_footprint          = loose_footprint,
    1411             :   .privileged_init          = privileged_init,
    1412             :   .unprivileged_init        = unprivileged_init,
    1413             :   .run                      = stem_run,
    1414             :   .keep_host_networking     = 1,
    1415             :   .allow_connect            = 1,
    1416             :   .allow_renameat           = 1,
    1417             : };
    1418             : 
    1419             : #undef NAME

Generated by: LCOV version 1.14