LCOV - code coverage report
Current view: top level - discof/restore - fd_snapct_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 147 1569 9.4 %
Date: 2026-08-14 04:54:57 Functions: 5 70 7.1 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_snapct_tile.h"
       3             : #include "utils/fd_sspeer.h"
       4             : #include "utils/fd_ssping.h"
       5             : #include "utils/fd_ssctrl.h"
       6             : #include "utils/fd_ssarchive.h"
       7             : #include "utils/fd_http_resolver.h"
       8             : #include "utils/fd_ssmsg.h"
       9             : #include "../backup/fd_snap_pool.h"
      10             : 
      11             : #include "../../disco/topo/fd_topo.h"
      12             : #include "../../disco/topo/fd_dns_resolve.h"
      13             : #include "../../disco/metrics/fd_metrics.h"
      14             : #include "../../flamenco/gossip/fd_gossip_message.h"
      15             : #include "../../waltz/openssl/fd_openssl_tile.h"
      16             : #include "../../waltz/resolv/fd_netdb.h"
      17             : #include "../../waltz/resolv/fd_adns.h"
      18             : 
      19             : #include <errno.h>
      20             : #include <stdio.h>
      21             : #include <fcntl.h>
      22             : #include <unistd.h>
      23             : #include <sys/stat.h>
      24             : #include <netinet/tcp.h>
      25             : #include <netinet/in.h>
      26             : 
      27             : #include "generated/fd_snapct_tile_seccomp.h"
      28             : 
      29             : #define NAME "snapct"
      30             : 
      31             : /* FIXME: Do a finishing pass over the default.toml config options / comments */
      32             : 
      33          57 : #define GOSSIP_PEERS_MAX (FD_CONTACT_INFO_TABLE_SIZE)
      34          27 : #define SERVER_PEERS_MAX (FD_TOPO_SNAPSHOTS_SERVERS_MAX_RESOLVED)
      35          27 : #define TOTAL_PEERS_MAX  (GOSSIP_PEERS_MAX + SERVER_PEERS_MAX)
      36             : 
      37           0 : #define IN_KIND_ACK    (0)
      38           0 : #define IN_KIND_SNAPLD (1)
      39           0 : #define IN_KIND_GOSSIP (2)
      40             : #define MAX_IN_LINKS   (4)
      41             : 
      42             : struct fd_snapct_out_link {
      43             :   ulong       idx;
      44             :   fd_wksp_t * mem;
      45             :   ulong       chunk0;
      46             :   ulong       wmark;
      47             :   ulong       chunk;
      48             :   ulong       mtu;
      49             : };
      50             : typedef struct fd_snapct_out_link fd_snapct_out_link_t;
      51             : 
      52           0 : #define FD_SNAPCT_COLLECTING_PEERS_TIMEOUT (90L*1000L*1000L*1000L) /* 1.5 minutes */
      53             : 
      54             : struct gossip_ci_entry {
      55             :   fd_pubkey_t   pubkey;
      56             :   int           allowed;
      57             :   fd_ip4_port_t rpc_addr;
      58             :   ulong         map_next;
      59             : };
      60             : typedef struct gossip_ci_entry gossip_ci_entry_t;
      61             : 
      62             : #define MAP_NAME               gossip_ci_map
      63           9 : #define MAP_KEY                pubkey
      64             : #define MAP_ELE_T              gossip_ci_entry_t
      65             : #define MAP_KEY_T              fd_pubkey_t
      66          12 : #define MAP_NEXT               map_next
      67          12 : #define MAP_KEY_EQ(k0,k1)      fd_pubkey_eq( k0, k1 )
      68          51 : #define MAP_KEY_HASH(key,seed) fd_hash( seed, key, sizeof(fd_pubkey_t) )
      69             : #include "../../util/tmpl/fd_map_chain.c"
      70             : 
      71             : /* Standalone blacklist keyed on fd_sspeer_key_t (peer identity).
      72             :    Keying on identity rather than network address is intentional:
      73             :    a peer's address can change freely, but its identity cannot.
      74             :    For gossip peers the identity is the pubkey; for URL peers it is
      75             :    the (hostname, resolved_addr) pair.  Blacklisted peers are
      76             :    permanently banned for the bootstrap lifetime.  The blacklist uses
      77             :    its own dedicated pool so entries are never evicted by pool
      78             :    pressure, unlike the ssping peer pool. */
      79             : 
      80             : struct fd_sspeer_blacklist_entry {
      81             :   fd_sspeer_key_t key;
      82             :   ulong           pool_next;
      83             :   ulong           map_next;
      84             : };
      85             : typedef struct fd_sspeer_blacklist_entry fd_sspeer_blacklist_entry_t;
      86             : 
      87             : #define POOL_NAME  blacklist_pool
      88          30 : #define POOL_T     fd_sspeer_blacklist_entry_t
      89             : #define POOL_IDX_T ulong
      90      394008 : #define POOL_NEXT  pool_next
      91             : #include "../../util/tmpl/fd_pool.c"
      92             : 
      93             : #define MAP_NAME               blacklist_map
      94          18 : #define MAP_KEY                key
      95             : #define MAP_ELE_T              fd_sspeer_blacklist_entry_t
      96             : #define MAP_KEY_T              fd_sspeer_key_t
      97          51 : #define MAP_NEXT               map_next
      98          39 : #define MAP_KEY_EQ(k0,k1)      (fd_sspeer_key_eq(k0,k1))
      99          63 : #define MAP_KEY_HASH(key,seed) (fd_sspeer_key_hash(key,seed))
     100             : #include "../../util/tmpl/fd_map_chain.c"
     101             : 
     102             : struct fd_snapct_tile {
     103             :   struct fd_topo_tile_snapct config;
     104             :   int                        gossip_enabled;
     105             :   int                        download_enabled;
     106             : 
     107             :   fd_netdb_fds_t netdb_fds[1];
     108             : 
     109             :   fd_adns_t * adns;
     110             :   struct {
     111             :     char   hostname[ 256UL ];
     112             :     ushort port; /* net order */
     113             :     int    is_https;
     114             :     int    resolved;
     115             :     long   retry_nanos; /* re-queue due time, 0 while in flight */
     116             :   } dns_servers[ FD_TOPO_SNAPSHOTS_SERVERS_MAX ];
     117             :   struct {
     118             :     char   hostname[ 256UL ];
     119             :     ushort port;
     120             :     int    resolved;
     121             :     long   retry_nanos;
     122             :   } dns_entrypoints[ FD_TOPO_GOSSIP_ENTRYPOINTS_MAX ];
     123             : 
     124             :   ulong resolved_servers_cnt;
     125             :   struct {
     126             :     fd_ip4_port_t addr;
     127             :     char          hostname[ 256UL ];
     128             :     int           is_https;
     129             :   } resolved_servers[ FD_TOPO_SNAPSHOTS_SERVERS_MAX_RESOLVED ];
     130             : 
     131             :   ulong         resolved_entrypoints_cnt;
     132             :   fd_ip4_port_t resolved_entrypoints[ FD_TOPO_GOSSIP_ENTRYPOINTS_MAX ];
     133             : 
     134             :   fd_ssping_t *          ssping;
     135             :   fd_http_resolver_t *   ssresolver;
     136             :   fd_sspeer_selector_t * selector;
     137             :   ulong                  selector_seed;
     138             : 
     139             :   fd_sspeer_blacklist_entry_t * blacklist_pool;
     140             :   blacklist_map_t *             blacklist_map;
     141             : 
     142             :   int           state;
     143             :   int           malformed;
     144             :   int           load_complete;
     145             :   long          deadline_nanos;
     146             :   int           flush_ack;
     147             :   int           flush_ack_cnt;
     148             :   fd_sspeer_t   peer;
     149             : 
     150             :   struct {
     151             :     int dir_fd;
     152             :     int full_snapshot_fd;
     153             :     int incremental_snapshot_fd;
     154             :     char full_snapshot_name[ FD_SNAP_NAME_MAX ];
     155             :     char incremental_snapshot_name[ FD_SNAP_NAME_MAX ];
     156             :   } local_out;
     157             : 
     158             :   char http_full_snapshot_name[ PATH_MAX ];
     159             :   char http_incr_snapshot_name[ PATH_MAX ];
     160             : 
     161             :   void const * gossip_in_mem;
     162             :   void const * snapld_in_mem;
     163             :   uchar        in_kind[ MAX_IN_LINKS ];
     164             : 
     165             :   struct {
     166             :     ulong full_slot;
     167             :     ulong slot;
     168             :     int   pending;
     169             :   } predicted_incremental;
     170             : 
     171             :   struct {
     172             :     ulong full_snapshot_slot;
     173             :     char  full_snapshot_path[ PATH_MAX ];
     174             :     ulong full_snapshot_size;
     175             :     int   full_snapshot_zstd;
     176             : 
     177             :     uchar full_snapshot_hash[ FD_HASH_FOOTPRINT ];
     178             :     uchar incremental_snapshot_hash[ FD_HASH_FOOTPRINT ];
     179             : 
     180             :     ulong incremental_snapshot_slot;
     181             :     char  incremental_snapshot_path[ PATH_MAX ];
     182             :     ulong incremental_snapshot_size;
     183             :     int   incremental_snapshot_zstd;
     184             :   } local_in;
     185             : 
     186             :   struct {
     187             :     struct {
     188             :       ulong bytes_read;
     189             :       ulong bytes_written;
     190             :       ulong bytes_total;
     191             :       uint  num_retries;
     192             :     } full;
     193             : 
     194             :     struct {
     195             :       ulong bytes_read;
     196             :       ulong bytes_written;
     197             :       ulong bytes_total;
     198             :       uint  num_retries;
     199             :     } incremental;
     200             :   } metrics;
     201             : 
     202             :   struct {
     203             :     gossip_ci_entry_t * ci_table;  /* flat array of all gossip entries, allowed or not */
     204             :     gossip_ci_map_t *   ci_map;    /* map from pubkey to only allowed gossip entries */
     205             :     ulong               allowed_cnt; /* number of allowed entries in ci_map */
     206             :     int                 saturated;
     207             :   } gossip;
     208             : 
     209             :   long snapshot_start_timestamp_ns;
     210             : 
     211             :   fd_snapct_out_link_t out_ld;
     212             :   fd_snapct_out_link_t out_gui;
     213             :   fd_snapct_out_link_t out_rp;
     214             : };
     215             : typedef struct fd_snapct_tile fd_snapct_tile_t;
     216             : 
     217             : static int
     218           0 : gossip_enabled( fd_topo_tile_t const * tile ) {
     219           0 :   return tile->snapct.sources.gossip.allow_any || tile->snapct.sources.gossip.allow_list_cnt>0UL;
     220           0 : }
     221             : 
     222             : static int
     223           0 : download_enabled( fd_topo_tile_t const * tile ) {
     224           0 :   return gossip_enabled( tile ) || tile->snapct.sources.servers_cnt>0UL;
     225           0 : }
     226             : 
     227             : FD_FN_CONST static inline ulong
     228           0 : loose_footprint( fd_topo_tile_t const * tile ) {
     229           0 :   (void)tile;
     230             :   /* Leftover space for OpenSSL allocations */
     231           0 :   return 1<<26UL; /* 64 MiB */
     232           0 : }
     233             : 
     234           0 : #define ADNS_REQS_MAX (FD_TOPO_SNAPSHOTS_SERVERS_MAX+FD_TOPO_GOSSIP_ENTRYPOINTS_MAX)
     235             : 
     236             : static ulong
     237          90 : scratch_align( void ) {
     238          90 :   return fd_ulong_max( alignof(fd_snapct_tile_t),
     239          90 :          fd_ulong_max( fd_ssping_align(),
     240          90 :          fd_ulong_max( alignof(gossip_ci_entry_t),
     241          90 :          fd_ulong_max( gossip_ci_map_align(),
     242          90 :          fd_ulong_max( fd_http_resolver_align(),
     243          90 :          fd_ulong_max( fd_sspeer_selector_align(),
     244          90 :          fd_ulong_max( blacklist_pool_align(),
     245          90 :          fd_ulong_max( blacklist_map_align(),
     246          90 :                        fd_adns_align() ) ) ) ) ) ) ) );
     247          90 : }
     248             : 
     249             : static ulong
     250          30 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
     251          30 :   ulong l = FD_LAYOUT_INIT;
     252          30 :   l = FD_LAYOUT_APPEND( l, alignof(fd_snapct_tile_t),  sizeof(fd_snapct_tile_t)                                                   );
     253          30 :   l = FD_LAYOUT_APPEND( l, fd_ssping_align(),          fd_ssping_footprint( TOTAL_PEERS_MAX )                                     );
     254          30 :   l = FD_LAYOUT_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX                               );
     255          30 :   l = FD_LAYOUT_APPEND( l, gossip_ci_map_align(),      gossip_ci_map_footprint( gossip_ci_map_chain_cnt_est( GOSSIP_PEERS_MAX ) ) );
     256          30 :   l = FD_LAYOUT_APPEND( l, fd_http_resolver_align(),   fd_http_resolver_footprint( SERVER_PEERS_MAX )                             );
     257          30 :   l = FD_LAYOUT_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX )                            );
     258          30 :   l = FD_LAYOUT_APPEND( l, blacklist_pool_align(),     blacklist_pool_footprint( TOTAL_PEERS_MAX )                               );
     259          30 :   l = FD_LAYOUT_APPEND( l, blacklist_map_align(),      blacklist_map_footprint( blacklist_map_chain_cnt_est( TOTAL_PEERS_MAX ) )  );
     260          30 :   l = FD_LAYOUT_APPEND( l, fd_adns_align(),            fd_adns_footprint( ADNS_REQS_MAX )                                         );
     261          30 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),           fd_alloc_footprint()                                                       );
     262          30 :   return FD_LAYOUT_FINI( l, scratch_align() );
     263          30 : }
     264             : 
     265             : static inline int
     266           0 : should_shutdown( fd_snapct_tile_t * ctx ) {
     267           0 :   return ctx->state==FD_SNAPCT_STATE_SHUTDOWN;
     268           0 : }
     269             : 
     270             : static void
     271           0 : metrics_write( fd_snapct_tile_t * ctx ) {
     272           0 :   FD_MGAUGE_SET( SNAPCT, FULL_BYTES_READ,               ctx->metrics.full.bytes_read );
     273           0 :   FD_MGAUGE_SET( SNAPCT, FULL_BYTES_WRITTEN,            ctx->metrics.full.bytes_written );
     274           0 :   FD_MGAUGE_SET( SNAPCT, FULL_SIZE_BYTES,              ctx->metrics.full.bytes_total );
     275           0 :   FD_MGAUGE_SET( SNAPCT, FULL_RETRY,          ctx->metrics.full.num_retries );
     276             : 
     277           0 :   FD_MGAUGE_SET( SNAPCT, INCREMENTAL_BYTES_READ,        ctx->metrics.incremental.bytes_read );
     278           0 :   FD_MGAUGE_SET( SNAPCT, INCREMENTAL_BYTES_WRITTEN,     ctx->metrics.incremental.bytes_written );
     279           0 :   FD_MGAUGE_SET( SNAPCT, INCREMENTAL_SIZE_BYTES,       ctx->metrics.incremental.bytes_total );
     280           0 :   FD_MGAUGE_SET( SNAPCT, INCREMENTAL_RETRY,   ctx->metrics.incremental.num_retries );
     281             : 
     282           0 :   FD_MGAUGE_SET( SNAPCT, PREDICTED_SLOT,                ctx->predicted_incremental.slot );
     283             : 
     284           0 : #if FD_HAS_OPENSSL
     285           0 :   FD_MCNT_SET(   SNAPCT, SSL_ALLOC_FAILED,                fd_ossl_alloc_errors );
     286           0 : #endif
     287             : 
     288           0 :   FD_MGAUGE_SET( SNAPCT, STATE, (ulong)ctx->state );
     289           0 : }
     290             : 
     291             : static void
     292             : snapshot_path_gui_publish( fd_snapct_tile_t *  ctx,
     293             :                            fd_stem_context_t * stem,
     294             :                            char const *        path,
     295           0 :                            int                 is_full ) {
     296             :   /* The messages below cannot be obtained directly from metrics. */
     297           0 :   fd_snapct_update_t * out = fd_chunk_to_laddr( ctx->out_gui.mem, ctx->out_gui.chunk );
     298           0 :   FD_TEST( fd_cstr_printf_check( out->read_path, PATH_MAX, NULL, "%s", path ) );
     299           0 :   out->is_download = 0;
     300           0 :   out->type = fd_int_if( is_full, FD_SNAPCT_SNAPSHOT_TYPE_FULL, FD_SNAPCT_SNAPSHOT_TYPE_INCREMENTAL );
     301           0 :   fd_stem_publish( stem, ctx->out_gui.idx, 0UL, ctx->out_gui.chunk, sizeof(fd_snapct_update_t) , 0UL, 0UL, 0UL );
     302           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 );
     303           0 : }
     304             : 
     305             : static void
     306           0 : predict_incremental( fd_snapct_tile_t * ctx ) {
     307           0 :   if( FD_UNLIKELY( !ctx->config.incremental_snapshots ) ) return;
     308           0 :   if( FD_UNLIKELY( ctx->predicted_incremental.full_slot==FD_SSPEER_SLOT_UNKNOWN ) ) return;
     309             : 
     310           0 :   fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
     311             : 
     312           0 :   if( FD_LIKELY( best.addr.l ) ) {
     313           0 :     if( FD_UNLIKELY( ctx->predicted_incremental.slot!=best.incr_slot ) ) {
     314           0 :       ctx->predicted_incremental.slot    = best.incr_slot;
     315           0 :       ctx->predicted_incremental.pending = 1;
     316           0 :     }
     317           0 :   }
     318           0 : }
     319             : 
     320             : static void
     321             : on_resolve( void *                  _ctx,
     322             :             fd_sspeer_key_t const * key,
     323             :             fd_ip4_port_t           addr,
     324             :             ulong                   full_slot,
     325             :             ulong                   incr_slot,
     326             :             uchar                   full_hash[ FD_HASH_FOOTPRINT ],
     327           0 :             uchar                   incr_hash[ FD_HASH_FOOTPRINT ] ) {
     328           0 :   fd_snapct_tile_t * ctx = (fd_snapct_tile_t *)_ctx;
     329             : 
     330           0 :   if( FD_UNLIKELY( full_slot!=FD_SSPEER_SLOT_UNKNOWN && full_slot>=FD_SSPEER_PLAUSIBLE_MAX_SLOT ) ) return;
     331           0 :   if( FD_UNLIKELY( incr_slot!=FD_SSPEER_SLOT_UNKNOWN && incr_slot>=FD_SSPEER_PLAUSIBLE_MAX_SLOT ) ) return;
     332             : 
     333             :   /* Do not update peers that have been permanently blacklisted. */
     334           0 :   if( FD_UNLIKELY( key && blacklist_map_ele_query( ctx->blacklist_map, key, NULL, ctx->blacklist_pool ) ) ) return;
     335             :   /* Do not re-add peers whose addr is temporarily banned by ssping. */
     336           0 :   if( FD_UNLIKELY( fd_ssping_is_invalidated( ctx->ssping, addr ) ) ) return;
     337             : 
     338             :   /* add() handles both new and existing peers, so peers removed from
     339             :      the selector during a previous blacklist, timeout, or failed
     340             :      re-resolve are re-added with the freshly resolved data. */
     341           0 :   ulong score = fd_sspeer_selector_add( ctx->selector, key, addr, FD_SSPEER_LATENCY_UNKNOWN,
     342           0 :                                         full_slot, incr_slot, full_hash, incr_hash );
     343           0 :   if( FD_UNLIKELY( score==FD_SSPEER_SCORE_INVALID ) ) {
     344           0 :     if( FD_UNLIKELY( key==NULL ) ) {
     345           0 :       FD_LOG_DEBUG(( "selector add on resolve failed for NULL peer key" ));
     346           0 :     } else {
     347           0 :       if( FD_UNLIKELY( !key->is_url ) ) {
     348           0 :         FD_BASE58_ENCODE_32_BYTES( key->pubkey->key, pubkey_b58 );
     349           0 :         FD_LOG_DEBUG(( "selector add on resolve failed for peer with pubkey %s", pubkey_b58 ));
     350           0 :       } else {
     351           0 :         FD_LOG_DEBUG(( "selector add on resolve failed for peer %s with addr " FD_IP4_ADDR_FMT ":%hu", key->url.hostname,
     352           0 :                        FD_IP4_ADDR_FMT_ARGS( key->url.resolved_addr.addr ), fd_ushort_bswap( key->url.resolved_addr.port ) ));
     353           0 :       }
     354           0 :     }
     355           0 :   }
     356           0 :   fd_sspeer_selector_process_cluster_slot( ctx->selector );
     357           0 :   predict_incremental( ctx );
     358           0 : }
     359             : 
     360             : static void
     361             : on_ping( void *        _ctx,
     362             :          fd_ip4_port_t addr,
     363           0 :          ulong         latency ) {
     364           0 :   fd_snapct_tile_t * ctx = (fd_snapct_tile_t *)_ctx;
     365             : 
     366           0 :   ulong cnt = fd_sspeer_selector_update_on_ping( ctx->selector, addr, latency );
     367           0 :   if( FD_UNLIKELY( !cnt ) ) {
     368             :     /* The update may fail in normal operation, e.g. after a peer has
     369             :        been removed from the selector.  The log level is set to a
     370             :        minimum accordingly. */
     371           0 :     FD_LOG_DEBUG(( "selector update on ping did not find address " FD_IP4_ADDR_FMT ":%hu",
     372           0 :                    FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ) ));
     373           0 :   }
     374           0 :   predict_incremental( ctx );
     375           0 : }
     376             : 
     377             : static void
     378             : on_snapshot_hash( fd_snapct_tile_t *                 ctx,
     379             :                   fd_sspeer_key_t const *            key,
     380             :                   fd_ip4_port_t                      addr,
     381           0 :                   fd_gossip_update_message_t const * msg ) {
     382           0 :   ulong         full_slot = msg->snapshot_hashes->full_slot;
     383           0 :   ulong         incr_slot = FD_SSPEER_SLOT_UNKNOWN;
     384           0 :   uchar const * incr_hash = NULL;
     385             : 
     386           0 :   for( ulong i=0UL; i<msg->snapshot_hashes->incremental_len; i++ ) {
     387           0 :     if( FD_LIKELY( !incr_hash || msg->snapshot_hashes->incremental[ i ].slot>incr_slot ) ) {
     388           0 :       incr_slot = msg->snapshot_hashes->incremental[ i ].slot;
     389           0 :       incr_hash = msg->snapshot_hashes->incremental[ i ].hash;
     390           0 :     }
     391           0 :   }
     392             : 
     393           0 :   if( FD_UNLIKELY( full_slot>=FD_SSPEER_PLAUSIBLE_MAX_SLOT ) ) return;
     394           0 :   if( FD_UNLIKELY( incr_slot!=FD_SSPEER_SLOT_UNKNOWN && incr_slot>=FD_SSPEER_PLAUSIBLE_MAX_SLOT ) ) return;
     395             : 
     396           0 :   if( FD_UNLIKELY( !addr.l ) ) {
     397             :     /* A peer that does not advertise an rpc_addr cannot be added to
     398             :        the selector: if previously added, remove it.  The remove
     399             :        operation becomes a no-op if the peer is not found. */
     400           0 :     fd_sspeer_selector_remove( ctx->selector, key );
     401           0 :     return;
     402           0 :   }
     403             :   /* Do not re-add peers that have been permanently blacklisted. */
     404           0 :   if( FD_UNLIKELY( blacklist_map_ele_query( ctx->blacklist_map, key, NULL, ctx->blacklist_pool ) ) ) return;
     405             :   /* Do not re-add peers whose addr is temporarily banned by ssping. */
     406           0 :   if( FD_UNLIKELY( fd_ssping_is_invalidated( ctx->ssping, addr ) ) ) return;
     407             :   /* The add may fail due to capacity/pool exhaustion.  The cluster
     408             :      slot is recomputed from tracked peers only, so a failed add will
     409             :      not influence the cluster slot. */
     410           0 :   fd_sspeer_selector_add( ctx->selector, key, addr, FD_SSPEER_LATENCY_UNKNOWN,
     411           0 :                           full_slot, incr_slot,
     412           0 :                           msg->snapshot_hashes->full_hash, incr_hash );
     413           0 :   fd_sspeer_selector_process_cluster_slot( ctx->selector );
     414           0 :   predict_incremental( ctx );
     415           0 : }
     416             : 
     417             : static void
     418             : send_expected_slot( fd_snapct_tile_t *  ctx,
     419             :                     fd_stem_context_t * stem,
     420           0 :                     ulong               slot ) {
     421           0 :   uint tsorig; uint tspub;
     422           0 :   fd_ssmsg_slot_to_frag( slot, &tsorig, &tspub );
     423           0 :   fd_stem_publish( stem, ctx->out_rp.idx, FD_SSMSG_EXPECTED_SLOT, 0UL, 0UL, 0UL, tsorig, tspub );
     424           0 : }
     425             : 
     426             : /* snapshot_pool_select picks a snapshot file to overwrite with newly
     427             :    downloaded data.  pool[return value] gives the inode to recycle. */
     428             : 
     429             : static uint
     430             : snapshot_pool_select( fd_backup_inode_t const * pool,
     431             :                       uint                      slot0,
     432           0 :                       uint                      slot1 ) {
     433           0 :   uint  selected = UINT_MAX;
     434           0 :   ulong oldest   = ULONG_MAX;
     435           0 :   for( uint i=slot0; i<slot1; i++ ) {
     436           0 :     if( FD_UNLIKELY( pool[ i ].full_slot==ULONG_MAX ) ) return i;
     437           0 :     ulong slot = pool[ i ].incr_slot==ULONG_MAX ? pool[ i ].full_slot : pool[ i ].incr_slot;
     438           0 :     if( slot<oldest ) {
     439           0 :       selected = i;
     440           0 :       oldest   = slot;
     441           0 :     }
     442           0 :   }
     443           0 :   return selected;
     444           0 : }
     445             : 
     446             : /* snapshot_output_prepare recycles a 'partial' snapshot file or
     447             :    overwrites an older snapshot. */
     448             : 
     449             : static void
     450             : snapshot_output_prepare( fd_snapct_tile_t * ctx,
     451           0 :                          int                full ) {
     452           0 :   int    fd   = full ? ctx->local_out.full_snapshot_fd   : ctx->local_out.incremental_snapshot_fd;
     453           0 :   char * name = full ? ctx->local_out.full_snapshot_name : ctx->local_out.incremental_snapshot_name;
     454             : 
     455           0 :   FD_TEST( ctx->local_out.dir_fd!=-1 && fd!=-1 );
     456           0 :   FD_TEST( fd>=FD_SNAP_FD( 0U ) && fd<FD_SNAP_FD( FD_SNAP_MAX ) );
     457             : 
     458           0 :   char partial_name[ FD_SNAP_NAME_MAX ];
     459           0 :   fd_snap_pool_partial_name( partial_name, (uint)(fd-FD_SNAP_FD( 0U )) );
     460           0 :   if( strcmp( name, partial_name ) ) {
     461           0 :     char const * local_path = full ? ctx->local_in.full_snapshot_path : ctx->local_in.incremental_snapshot_path;
     462           0 :     char const * local_name = strrchr( local_path, '/' );
     463           0 :     local_name = local_name ? local_name+1 : local_path;
     464           0 :     if( !strcmp( name, local_name ) ) {
     465           0 :       if( full ) ctx->local_in.full_snapshot_slot        = ULONG_MAX;
     466           0 :       else       ctx->local_in.incremental_snapshot_slot = ULONG_MAX;
     467           0 :     }
     468             : 
     469           0 :     if( FD_UNLIKELY( -1==renameat( ctx->local_out.dir_fd, name, ctx->local_out.dir_fd, partial_name ) ) )
     470           0 :       FD_LOG_ERR(( "renameat(%s, %s) failed (%i-%s)", name, partial_name, errno, fd_io_strerror( errno ) ));
     471           0 :     fd_cstr_ncpy( name, partial_name, FD_SNAP_NAME_MAX );
     472           0 :   }
     473             : 
     474           0 :   if( FD_UNLIKELY( -1==ftruncate( fd, 0UL ) ) )
     475           0 :     FD_LOG_ERR(( "ftruncate(%s) failed (%i-%s)", name, errno, fd_io_strerror( errno ) ));
     476           0 :   if( FD_UNLIKELY( -1==lseek( fd, 0L, SEEK_SET ) ) )
     477           0 :     FD_LOG_ERR(( "lseek(%s) failed (%i-%s)", name, errno, fd_io_strerror( errno ) ));
     478           0 : }
     479             : 
     480             : static void
     481           0 : rename_full_snapshot( fd_snapct_tile_t * ctx ) {
     482           0 :   FD_TEST( -1!=ctx->local_out.dir_fd );
     483             : 
     484           0 :   if( FD_LIKELY( -1!=ctx->local_out.full_snapshot_fd && ctx->http_full_snapshot_name[ 0 ]!='\0' ) ) {
     485           0 :     int err = renameat2( ctx->local_out.dir_fd, ctx->local_out.full_snapshot_name,
     486           0 :                          ctx->local_out.dir_fd, ctx->http_full_snapshot_name, RENAME_NOREPLACE );
     487           0 :     if( FD_UNLIKELY( err && errno!=EEXIST ) )
     488           0 :       FD_LOG_ERR(( "renameat2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     489           0 :     if( FD_LIKELY( !err ) )
     490           0 :       fd_cstr_ncpy( ctx->local_out.full_snapshot_name, ctx->http_full_snapshot_name, FD_SNAP_NAME_MAX );
     491           0 :   }
     492           0 : }
     493             : 
     494             : static void
     495           0 : rename_incr_snapshot( fd_snapct_tile_t * ctx ) {
     496           0 :   FD_TEST( -1!=ctx->local_out.dir_fd );
     497             : 
     498           0 :   if( FD_LIKELY( -1!=ctx->local_out.incremental_snapshot_fd && ctx->http_incr_snapshot_name[ 0 ]!='\0' ) ) {
     499           0 :     int err = renameat2( ctx->local_out.dir_fd, ctx->local_out.incremental_snapshot_name,
     500           0 :                          ctx->local_out.dir_fd, ctx->http_incr_snapshot_name, RENAME_NOREPLACE );
     501           0 :     if( FD_UNLIKELY( err && errno!=EEXIST ) )
     502           0 :       FD_LOG_ERR(( "renameat2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     503           0 :     if( FD_LIKELY( !err ) )
     504           0 :       fd_cstr_ncpy( ctx->local_out.incremental_snapshot_name, ctx->http_incr_snapshot_name, FD_SNAP_NAME_MAX );
     505           0 :   }
     506           0 : }
     507             : 
     508             : static ulong
     509             : rlimit_file_cnt( fd_topo_t const *      topo FD_PARAM_UNUSED,
     510           0 :                  fd_topo_tile_t const * tile ) {
     511           0 :   ulong cnt = 1UL +                             /* stderr */
     512           0 :               1UL +                             /* logfile */
     513           0 :               1UL;                              /* boot control pipe */
     514           0 :   if( download_enabled( tile ) ) {
     515           0 :     cnt +=    FD_SSPING_FD_CNT +                /* ssping sockets */
     516           0 :               2UL +                             /* dirfd + full snapshot pool fd */
     517           0 :               tile->snapct.sources.servers_cnt; /* http resolver peer full sockets */
     518           0 :     if( tile->snapct.incremental_snapshots ) {
     519           0 :       cnt +=  1UL +                             /* incremental snapshot pool fd */
     520           0 :               tile->snapct.sources.servers_cnt; /* http resolver peer incr sockets */
     521           0 :     }
     522           0 :   }
     523           0 :   return cnt;
     524           0 : }
     525             : 
     526             : static ulong
     527             : populate_allowed_seccomp( fd_topo_t const *      topo,
     528             :                           fd_topo_tile_t const * tile,
     529             :                           ulong                  out_cnt,
     530           0 :                           struct sock_filter *   out ) {
     531             : 
     532           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     533             : 
     534           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     535           0 :   fd_snapct_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
     536             : 
     537           0 :   int min_ping_fd = INT_MAX;
     538           0 :   int max_ping_fd = 0;
     539           0 :   if( download_enabled( tile ) ) {
     540           0 :     min_ping_fd = FD_SSPING_FD_MIN;
     541           0 :     max_ping_fd = FD_SSPING_FD_MIN + (int)FD_SSPING_FD_CNT - 1;
     542           0 :   }
     543             : 
     544           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)min_ping_fd, (uint)max_ping_fd, (uint)ctx->netdb_fds->etc_hosts, (uint)ctx->netdb_fds->etc_resolv_conf );
     545           0 :   return sock_filter_policy_fd_snapct_tile_instr_cnt;
     546           0 : }
     547             : 
     548             : static ulong
     549             : populate_allowed_fds( fd_topo_t const *      topo,
     550             :                       fd_topo_tile_t const * tile,
     551             :                       ulong                  out_fds_cnt,
     552           0 :                       int *                  out_fds ) {
     553           0 :   if( FD_UNLIKELY( out_fds_cnt<7UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu is too small", out_fds_cnt ));
     554             : 
     555           0 :   ulong out_cnt = 0;
     556           0 :   out_fds[ out_cnt++ ] = 2UL; /* stderr */
     557           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
     558           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     559           0 :   }
     560             : 
     561           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     562             : 
     563           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     564           0 :   fd_snapct_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t), sizeof(fd_snapct_tile_t) );
     565           0 :   if( FD_LIKELY( -1!=ctx->local_out.dir_fd ) )                  out_fds[ out_cnt++ ] = ctx->local_out.dir_fd;
     566           0 :   if( FD_LIKELY( -1!=ctx->local_out.full_snapshot_fd ) )        out_fds[ out_cnt++ ] = ctx->local_out.full_snapshot_fd;
     567           0 :   if( FD_LIKELY( -1!=ctx->local_out.incremental_snapshot_fd ) ) out_fds[ out_cnt++ ] = ctx->local_out.incremental_snapshot_fd;
     568           0 :   if( FD_LIKELY( -1!=ctx->netdb_fds->etc_hosts ) )              out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_hosts;
     569           0 :   if( FD_LIKELY( -1!=ctx->netdb_fds->etc_resolv_conf ) )        out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_resolv_conf;
     570           0 :   if( FD_LIKELY( download_enabled( tile ) ) ) {
     571           0 :     if( FD_UNLIKELY( out_cnt+FD_SSPING_FD_CNT > out_fds_cnt ) ) {
     572           0 :       FD_LOG_ERR(( "out_fds_cnt %lu must be at least %lu", out_fds_cnt, out_cnt + FD_SSPING_FD_CNT ));
     573           0 :     }
     574           0 :     for( int i=FD_SSPING_FD_MIN; i<FD_SSPING_FD_MIN+(int)FD_SSPING_FD_CNT; i++ ) {
     575           0 :       out_fds[ out_cnt++ ] = i;
     576           0 :     }
     577           0 :   }
     578             : 
     579           0 :   return out_cnt;
     580           0 : }
     581             : 
     582             : static void
     583             : init_load( fd_snapct_tile_t *  ctx,
     584             :            fd_stem_context_t * stem,
     585             :            int                 full,
     586           0 :            int                 file ) {
     587           0 :   ctx->snapshot_start_timestamp_ns = fd_log_wallclock();
     588           0 :   fd_ssctrl_init_t * out = fd_chunk_to_laddr( ctx->out_ld.mem, ctx->out_ld.chunk );
     589           0 :   out->file = file;
     590           0 :   out->zstd = !file || (full ? ctx->local_in.full_snapshot_zstd : ctx->local_in.incremental_snapshot_zstd);
     591           0 :   if( file ) {
     592           0 :     out->slot = full ? ctx->local_in.full_snapshot_slot : ctx->local_in.incremental_snapshot_slot;
     593           0 :     if( full ) fd_memcpy( out->snapshot_hash, ctx->local_in.full_snapshot_hash, FD_HASH_FOOTPRINT );
     594           0 :     else       fd_memcpy( out->snapshot_hash, ctx->local_in.incremental_snapshot_hash, FD_HASH_FOOTPRINT );
     595           0 :   } else {
     596           0 :     out->slot = full ? ctx->predicted_incremental.full_slot : ctx->predicted_incremental.slot;
     597           0 :     if( full ) fd_memcpy( out->snapshot_hash, ctx->peer.full_hash, FD_HASH_FOOTPRINT );
     598           0 :     else       fd_memcpy( out->snapshot_hash, ctx->peer.incr_hash, FD_HASH_FOOTPRINT );
     599           0 :   }
     600             : 
     601           0 :   out->is_redirect = !file; /* always use redirect for HTTP downloads */
     602             : 
     603           0 :   if( file ) out->file_sz = full ? ctx->local_in.full_snapshot_size : ctx->local_in.incremental_snapshot_size;
     604           0 :   else       out->file_sz = 0UL;
     605             : 
     606           0 :   if( !file ) {
     607           0 :     out->addr = ctx->peer.addr;
     608           0 :     if( full ) {
     609           0 :       FD_TEST( fd_cstr_printf_check( out->path, PATH_MAX, &out->path_len, "/snapshot.tar.bz2" ) );
     610           0 :       FD_TEST( fd_cstr_printf_check( ctx->http_full_snapshot_name, PATH_MAX, NULL, "snapshot.tar.bz2" ) );
     611           0 :     } else {
     612           0 :       FD_TEST( fd_cstr_printf_check( out->path, PATH_MAX, &out->path_len, "/incremental-snapshot.tar.bz2" ) );
     613           0 :       FD_TEST( fd_cstr_printf_check( ctx->http_incr_snapshot_name, PATH_MAX, NULL, "incremental-snapshot.tar.bz2" ) );
     614           0 :     }
     615             : 
     616           0 :     out->is_https = 0; /* if not found in the config list, it's not https */
     617           0 :     out->hostname[0] = '\0'; /* .. and it doesn't have a hostname either. */
     618           0 :     for( ulong i=0UL; i<ctx->resolved_servers_cnt; i++ ) {
     619           0 :       if( FD_UNLIKELY( ctx->peer.addr.l==ctx->resolved_servers[ i ].addr.l ) ) {
     620           0 :         fd_cstr_ncpy( out->hostname, ctx->resolved_servers[ i ].hostname, sizeof(out->hostname) );
     621           0 :         out->is_https = ctx->resolved_servers[ i ].is_https;
     622           0 :         break;
     623           0 :       }
     624           0 :     }
     625           0 :   }
     626           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 );
     627           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 );
     628           0 :   ctx->flush_ack = 0;
     629           0 :   ctx->load_complete = 0;
     630             : 
     631           0 :   if( !file ) snapshot_output_prepare( ctx, full );
     632             : 
     633             :   /* Clear stale http_*_snapshot_name for file loads before rename
     634             :      functions run.  GUI publish is deferred to the META handler. */
     635           0 :   if( file ) {
     636           0 :     if( full ) fd_cstr_fini( ctx->http_full_snapshot_name );
     637           0 :     else       fd_cstr_fini( ctx->http_incr_snapshot_name );
     638           0 :   }
     639           0 : }
     640             : 
     641             : static void
     642             : log_download( fd_snapct_tile_t * ctx,
     643             :               int                full,
     644             :               fd_ip4_port_t      addr,
     645           0 :               ulong              slot ) {
     646           0 :   for( gossip_ci_map_iter_t iter = gossip_ci_map_iter_init( ctx->gossip.ci_map, ctx->gossip.ci_table );
     647           0 :       !gossip_ci_map_iter_done( iter, ctx->gossip.ci_map, ctx->gossip.ci_table );
     648           0 :       iter = gossip_ci_map_iter_next( iter, ctx->gossip.ci_map, ctx->gossip.ci_table ) ) {
     649           0 :     gossip_ci_entry_t const * ci_entry = gossip_ci_map_iter_ele_const( iter, ctx->gossip.ci_map, ctx->gossip.ci_table );
     650           0 :     if( ci_entry->rpc_addr.l==addr.l ) {
     651           0 :       FD_TEST( ci_entry->allowed );
     652             : 
     653           0 :       int is_entrypoint = 0;
     654           0 :       for( ulong i=0UL; i<ctx->resolved_entrypoints_cnt; i++ ) {
     655           0 :         if( FD_UNLIKELY( ctx->resolved_entrypoints[ i ].addr==addr.addr ) ) { is_entrypoint = 1; break; }
     656           0 :       }
     657           0 :       char const * kind = ctx->config.sources.gossip.allow_any ? "untrusted gossip peer" : "trusted gossip peer";
     658           0 :       if( FD_UNLIKELY( is_entrypoint ) ) kind = "entrypoint gossip peer";
     659             : 
     660           0 :       FD_BASE58_ENCODE_32_BYTES( ci_entry->pubkey.uc, pubkey_b58 );
     661           0 :       FD_LOG_NOTICE(( "downloading %s snapshot from %s %s%s%s",
     662           0 :                       full ? "full" : "incremental", kind, fd_log_style_bold(), pubkey_b58, fd_log_style_normal() ));
     663           0 :       FD_LOG_INFO(( "downloading %s snapshot at slot %lu from %s %s at " FD_IP4_ADDR_FMT ":%hu",
     664           0 :                     full ? "full" : "incremental", slot, kind, pubkey_b58,
     665           0 :                     FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ) ));
     666           0 :       return;
     667           0 :     }
     668           0 :   }
     669             : 
     670           0 :   for( ulong i=0UL; i<ctx->resolved_servers_cnt; i++ ) {
     671           0 :     if( addr.l==ctx->resolved_servers[ i ].addr.l ) {
     672           0 :       char const * scheme = ctx->resolved_servers[ i ].is_https ? "https" : "http";
     673           0 :       if( ctx->resolved_servers[ i ].hostname[ 0 ] ) {
     674           0 :         FD_LOG_NOTICE(( "downloading %s snapshot from %s%s://%s:%hu%s",
     675           0 :                         full ? "full" : "incremental",
     676           0 :                         fd_log_style_bold(), scheme, ctx->resolved_servers[ i ].hostname, fd_ushort_bswap( addr.port ), fd_log_style_normal() ));
     677           0 :         FD_LOG_INFO(( "downloading %s snapshot at slot %lu from configured server with index %lu at %s://%s:%hu",
     678           0 :                       full ? "full" : "incremental", slot, i,
     679           0 :                       scheme, ctx->resolved_servers[ i ].hostname, fd_ushort_bswap( addr.port ) ));
     680           0 :       } else {
     681           0 :         FD_LOG_NOTICE(( "downloading %s snapshot from %s%s://" FD_IP4_ADDR_FMT ":%hu%s",
     682           0 :                         full ? "full" : "incremental",
     683           0 :                         fd_log_style_bold(), scheme, FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ), fd_log_style_normal() ));
     684           0 :         FD_LOG_INFO(( "downloading %s snapshot at slot %lu from configured server with index %lu at %s://" FD_IP4_ADDR_FMT ":%hu",
     685           0 :                       full ? "full" : "incremental", slot, i,
     686           0 :                       scheme, FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ) ));
     687           0 :       }
     688           0 :       return;
     689           0 :     }
     690           0 :   }
     691             : 
     692           0 :   FD_TEST( 0 ); /* should not be possible */
     693           0 : }
     694             : 
     695             : static void
     696             : log_completion( fd_snapct_tile_t * ctx,
     697           0 :                 int                full ) {
     698           0 :   double elapsed = (double)(fd_log_wallclock() - ctx->snapshot_start_timestamp_ns) / 1e9;
     699           0 :   FD_LOG_INFO(( "%s snapshot load completed in %.3f seconds", full ? "full" : "incremental", elapsed ));
     700           0 : }
     701             : 
     702             : /* Blacklist the current peer: invalidate in ssping, remove from the
     703             :    selector, and add to the dedicated blacklist map.  Skips the insert
     704             :    if the peer is already blacklisted (dedup).  Warns if the pool is
     705             :    full (the ssping ban still provides temporary protection). */
     706             : static void
     707          24 : blacklist_peer( fd_snapct_tile_t * ctx ) {
     708          24 :   fd_ssping_invalidate( ctx->ssping, ctx->peer.addr, fd_log_wallclock() );
     709          24 :   fd_sspeer_selector_remove_by_addr( ctx->selector, ctx->peer.addr );
     710          24 :   fd_sspeer_selector_process_cluster_slot( ctx->selector );
     711          24 :   if( FD_UNLIKELY( blacklist_map_ele_query( ctx->blacklist_map, &ctx->peer.key, NULL, ctx->blacklist_pool ) ) ) return;
     712          21 :   if( FD_LIKELY( blacklist_pool_free( ctx->blacklist_pool ) ) ) {
     713          18 :     fd_sspeer_blacklist_entry_t * bl = blacklist_pool_ele_acquire( ctx->blacklist_pool );
     714          18 :     bl->key = ctx->peer.key;
     715          18 :     blacklist_map_ele_insert( ctx->blacklist_map, bl, ctx->blacklist_pool );
     716          18 :     if( FD_UNLIKELY( !ctx->peer.key.is_url ) ) {
     717          18 :       FD_BASE58_ENCODE_32_BYTES( ctx->peer.key.pubkey->uc, pubkey_b58 );
     718          18 :       FD_LOG_WARNING(( "permanently blacklisted peer identity %s", pubkey_b58 ));
     719          18 :     } else {
     720           0 :       FD_LOG_WARNING(( "permanently blacklisted peer identity %s",
     721           0 :                        ctx->peer.key.url.hostname[0] ? ctx->peer.key.url.hostname : "(none)" ));
     722           0 :     }
     723          18 :   } else {
     724           3 :     FD_LOG_WARNING(( "blacklist pool full, peer banned via ssping only" ));
     725           3 :   }
     726          21 : }
     727             : 
     728           0 : #define DNS_RETRY_NANOS       (15L*1000L*1000L*1000L)
     729           0 : #define DNS_REQ_ID_ENTRYPOINT (0x100UL) /* req_id: server idx, or this bit + entrypoint idx */
     730             : 
     731             : static void
     732             : dns_queue( fd_snapct_tile_t * ctx,
     733           0 :            long               now ) {
     734           0 :   for( ulong i=0UL; i<ctx->config.sources.servers_cnt; i++ ) {
     735           0 :     if( FD_LIKELY( ctx->dns_servers[ i ].resolved || !ctx->dns_servers[ i ].retry_nanos || ctx->dns_servers[ i ].retry_nanos>now ) ) continue;
     736           0 :     if( FD_UNLIKELY( fd_adns_resolve( ctx->adns, ctx->dns_servers[ i ].hostname, i ) ) ) break;
     737           0 :     ctx->dns_servers[ i ].retry_nanos = 0L; /* in flight */
     738           0 :   }
     739           0 :   for( ulong i=0UL; i<ctx->config.entrypoints_cnt; i++ ) {
     740           0 :     if( FD_LIKELY( ctx->dns_entrypoints[ i ].resolved || !ctx->dns_entrypoints[ i ].retry_nanos || ctx->dns_entrypoints[ i ].retry_nanos>now ) ) continue;
     741           0 :     if( FD_UNLIKELY( fd_adns_resolve( ctx->adns, ctx->dns_entrypoints[ i ].hostname, DNS_REQ_ID_ENTRYPOINT|i ) ) ) break;
     742           0 :     ctx->dns_entrypoints[ i ].retry_nanos = 0L;
     743           0 :   }
     744           0 : }
     745             : 
     746             : static void
     747             : dns_advance( fd_snapct_tile_t * ctx,
     748           0 :              long               now ) {
     749           0 :   dns_queue( ctx, now );
     750             : 
     751           0 :   fd_adns_result_t res[ 1 ];
     752           0 :   while( fd_adns_advance( ctx->adns, now, res ) ) {
     753           0 :     if( FD_UNLIKELY( res->req_id & DNS_REQ_ID_ENTRYPOINT ) ) {
     754           0 :       ulong i = res->req_id & ~DNS_REQ_ID_ENTRYPOINT;
     755           0 :       if( FD_UNLIKELY( res->err ) ) {
     756           0 :         FD_LOG_WARNING(( "could not resolve [gossip.entrypoints] entry \"%s\" (%s), retrying", ctx->config.entrypoints[ i ], fd_gai_strerror( res->err ) ));
     757           0 :         ctx->dns_entrypoints[ i ].retry_nanos = now+DNS_RETRY_NANOS;
     758           0 :         continue;
     759           0 :       }
     760           0 :       ctx->dns_entrypoints[ i ].resolved = 1;
     761           0 :       ctx->resolved_entrypoints[ ctx->resolved_entrypoints_cnt++ ] = (fd_ip4_port_t){ .addr=res->addrs[ 0 ], .port=ctx->dns_entrypoints[ i ].port };
     762           0 :       continue;
     763           0 :     }
     764             : 
     765           0 :     ulong i = res->req_id;
     766           0 :     if( FD_UNLIKELY( res->err ) ) {
     767           0 :       FD_LOG_WARNING(( "could not resolve [snapshots.sources.servers] entry \"%s\" (%s), retrying", ctx->config.sources.servers[ i ], fd_gai_strerror( res->err ) ));
     768           0 :       ctx->dns_servers[ i ].retry_nanos = now+DNS_RETRY_NANOS;
     769           0 :       continue;
     770           0 :     }
     771           0 :     ctx->dns_servers[ i ].resolved = 1;
     772             : 
     773           0 :     ulong addr_cnt = fd_ulong_min( res->addr_cnt, FD_TOPO_MAX_RESOLVED_ADDRS );
     774           0 :     for( ulong j=0UL; j<addr_cnt; j++ ) {
     775           0 :       ulong k = ctx->resolved_servers_cnt++;
     776           0 :       ctx->resolved_servers[ k ].addr     = (fd_ip4_port_t){ .addr=res->addrs[ j ], .port=ctx->dns_servers[ i ].port };
     777           0 :       ctx->resolved_servers[ k ].is_https = ctx->dns_servers[ i ].is_https;
     778           0 :       fd_cstr_ncpy( ctx->resolved_servers[ k ].hostname, ctx->dns_servers[ i ].hostname, sizeof(ctx->resolved_servers[ k ].hostname) );
     779             : 
     780             :       /* The peer needs to be added to resolver and to selector.
     781             :          Only if this succeeds, add the peer to ssping list. */
     782           0 :       if( FD_LIKELY( !fd_http_resolver_add( ctx->ssresolver,
     783           0 :                                             ctx->resolved_servers[ k ].addr,
     784           0 :                                             ctx->resolved_servers[ k ].hostname,
     785           0 :                                             ctx->resolved_servers[ k ].is_https,
     786           0 :                                             ctx->selector ) ) ) {
     787           0 :         fd_ssping_add( ctx->ssping, ctx->resolved_servers[ k ].addr );
     788           0 :       }
     789           0 :     }
     790           0 :   }
     791           0 : }
     792             : 
     793             : static void
     794             : after_credit( fd_snapct_tile_t *  ctx,
     795             :               fd_stem_context_t * stem,
     796             :               int *               opt_poll_in FD_PARAM_UNUSED,
     797           0 :               int *               charge_busy FD_PARAM_UNUSED ) {
     798           0 :   long now = fd_log_wallclock();
     799             : 
     800           0 :   if( FD_LIKELY( ctx->adns ) ) dns_advance( ctx, now );
     801           0 :   if( FD_LIKELY( ctx->ssping ) ) fd_ssping_advance( ctx->ssping, now, ctx->selector );
     802           0 :   if( FD_LIKELY( ctx->ssresolver ) ) fd_http_resolver_advance( ctx->ssresolver, now, ctx->selector );
     803             : 
     804             :   /* Advances above may remove peers, making cluster_slot dirty.
     805             :      Recompute so best() calls below use up-to-date scores.
     806             :      No-op when the dirty flag is not set internally. */
     807           0 :   fd_sspeer_selector_process_cluster_slot( ctx->selector );
     808             : 
     809             :   /* send an expected slot message as the predicted incremental
     810             :      could have changed as a result of the pinger, resolver, or from
     811             :      processing gossip frags in gossip_frag. */
     812           0 :   if( FD_LIKELY( ctx->predicted_incremental.pending ) ) {
     813           0 :     send_expected_slot( ctx, stem, ctx->predicted_incremental.slot );
     814           0 :     ctx->predicted_incremental.pending = 0;
     815           0 :   }
     816             : 
     817             :   /* Note: All state transitions should occur within this switch
     818             :      statement to make it easier to reason about the state management. */
     819             : 
     820           0 :   switch ( ctx->state ) {
     821             : 
     822             :     /* ============================================================== */
     823           0 :     case FD_SNAPCT_STATE_INIT: {
     824           0 :       if( FD_UNLIKELY( !ctx->download_enabled ) ) {
     825           0 :         ulong local_slot = ctx->config.incremental_snapshots ? ctx->local_in.incremental_snapshot_slot : ctx->local_in.full_snapshot_slot;
     826           0 :         send_expected_slot( ctx, stem, local_slot );
     827           0 :         FD_LOG_NOTICE(( "reading full snapshot from file %s%s%s", fd_log_style_dim(), ctx->local_in.full_snapshot_path, fd_log_style_normal() ));
     828           0 :         FD_LOG_INFO(( "reading full snapshot at slot %lu from local file `%s`", ctx->local_in.full_snapshot_slot, ctx->local_in.full_snapshot_path ));
     829           0 :         ctx->predicted_incremental.full_slot = ctx->local_in.full_snapshot_slot;
     830           0 :         ctx->state = FD_SNAPCT_STATE_READING_FULL_FILE;
     831           0 :         init_load( ctx, stem, 1, 1 );
     832           0 :         break;
     833           0 :       }
     834           0 :       ctx->deadline_nanos = now+ctx->config.wait_for_peers_timeout_nanos;
     835           0 :       ctx->state = FD_SNAPCT_STATE_WAITING_FOR_PEERS;
     836           0 :       break;
     837           0 :     }
     838             : 
     839             :     /* ============================================================== */
     840           0 :     case FD_SNAPCT_STATE_WAITING_FOR_PEERS: {
     841           0 :       if( FD_UNLIKELY( now>ctx->deadline_nanos ) ) FD_LOG_ERR(( "timed out waiting for peers." ));
     842             : 
     843           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 0, FD_SSPEER_SLOT_UNKNOWN );
     844           0 :       if( FD_LIKELY( best.addr.l ) ) {
     845           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS;
     846           0 :         ctx->deadline_nanos = now+FD_SNAPCT_COLLECTING_PEERS_TIMEOUT;
     847           0 :       }
     848           0 :       break;
     849           0 :     }
     850             : 
     851             :     /* ============================================================== */
     852           0 :     case FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL: {
     853           0 :       if( FD_UNLIKELY( now>ctx->deadline_nanos ) ) FD_LOG_ERR(( "timed out waiting for incremental snapshot peers." ));
     854             : 
     855           0 :       FD_TEST( ctx->predicted_incremental.full_slot!=FD_SSPEER_SLOT_UNKNOWN );
     856           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
     857           0 :       if( FD_LIKELY( best.addr.l ) ) {
     858           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
     859           0 :         ctx->deadline_nanos = now;
     860           0 :       }
     861           0 :       break;
     862           0 :     }
     863             : 
     864             :     /* ============================================================== */
     865           0 :     case FD_SNAPCT_STATE_COLLECTING_PEERS: {
     866           0 :       if( FD_UNLIKELY( !ctx->gossip.saturated && now<ctx->deadline_nanos ) ) break;
     867             : 
     868           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 0, FD_SSPEER_SLOT_UNKNOWN );
     869           0 :       if( FD_UNLIKELY( !best.addr.l ) ) {
     870           0 :         if( !ctx->gossip_enabled ) {
     871           0 :           FD_LOG_ERR(( "no peers are available and discovery of new peers via gossip is disabled. aborting." ));
     872           0 :         }
     873           0 :         ctx->deadline_nanos = now + ctx->config.wait_for_peers_timeout_nanos;
     874           0 :         ctx->state = FD_SNAPCT_STATE_WAITING_FOR_PEERS;
     875           0 :         break;
     876           0 :       }
     877             : 
     878           0 :       fd_sscluster_slot_t cluster = fd_sspeer_selector_cluster_slot( ctx->selector );
     879           0 :       if( FD_UNLIKELY( cluster.incremental==FD_SSPEER_SLOT_UNKNOWN && ctx->config.incremental_snapshots ) ) {
     880             :         /* We must have a cluster full slot to be in this state. */
     881           0 :         FD_TEST( cluster.full!=FD_SSPEER_SLOT_UNKNOWN );
     882             :         /* fall back to full snapshot only if the highest cluster slot
     883             :            is a full snapshot only */
     884           0 :         FD_LOG_WARNING(( "incremental snapshots were enabled via [snapshots.incremental_snapshots], but no incremental snapshot is available in the cluster. "
     885           0 :                          "falling back to full snapshots only." ));
     886           0 :         ctx->config.incremental_snapshots = 0;
     887           0 :       }
     888             : 
     889           0 :       ulong cluster_slot = ctx->config.incremental_snapshots ? cluster.incremental : cluster.full;
     890             : 
     891             :       /* Determine the best effective slot achievable using the local
     892             :          full snapshot.  When incrementals are disabled, the effective
     893             :          slot is the full snapshot slot itself.  When enabled, it is the
     894             :          best incremental we can pair with the local full (either from
     895             :          a local file or downloaded from a peer). */
     896             : 
     897           0 :       ulong local_effective_slot = ULONG_MAX;
     898           0 :       if( FD_LIKELY( ctx->local_in.full_snapshot_slot!=ULONG_MAX ) ) {
     899           0 :         if( FD_LIKELY( ctx->config.incremental_snapshots ) ) {
     900           0 :           ulong local_incr = ctx->local_in.incremental_snapshot_slot;
     901           0 :           if( local_incr!=ULONG_MAX && local_incr>=fd_ulong_sat_sub( cluster_slot, ctx->config.sources.max_local_incremental_age ) ) {
     902           0 :             local_effective_slot = local_incr;
     903           0 :           } else {
     904           0 :             fd_sspeer_t best_incr = fd_sspeer_selector_best( ctx->selector, 1, ctx->local_in.full_snapshot_slot );
     905           0 :             if( FD_LIKELY( best_incr.addr.l ) ) {
     906           0 :               ctx->predicted_incremental.slot         = best_incr.incr_slot;
     907           0 :               ctx->local_in.incremental_snapshot_slot = ULONG_MAX; /* don't use the local incremental */
     908           0 :               local_effective_slot                    = best_incr.incr_slot;
     909           0 :             }
     910           0 :           }
     911           0 :         } else {
     912           0 :           local_effective_slot = ctx->local_in.full_snapshot_slot;
     913           0 :         }
     914           0 :       }
     915             : 
     916           0 :       int can_use_local_full = local_effective_slot!=ULONG_MAX &&
     917           0 :                                local_effective_slot>=fd_ulong_sat_sub( cluster_slot, ctx->config.sources.max_local_full_effective_age );
     918           0 :       if( FD_LIKELY( can_use_local_full ) ) {
     919           0 :         send_expected_slot( ctx, stem, local_effective_slot );
     920             : 
     921           0 :         FD_LOG_NOTICE(( "reading full snapshot from file %s%s%s", fd_log_style_dim(), ctx->local_in.full_snapshot_path, fd_log_style_normal() ));
     922           0 :         FD_LOG_INFO(( "reading full snapshot at slot %lu with cluster slot %lu from local file `%s`",
     923           0 :                       ctx->local_in.full_snapshot_slot, cluster_slot, ctx->local_in.full_snapshot_path ));
     924           0 :         ctx->predicted_incremental.full_slot = ctx->local_in.full_snapshot_slot;
     925           0 :         ctx->state                           = FD_SNAPCT_STATE_READING_FULL_FILE;
     926           0 :         init_load( ctx, stem, 1, 1 );
     927           0 :       } else {
     928           0 :         if( FD_LIKELY( ctx->local_in.full_snapshot_slot!=ULONG_MAX ) ) {
     929           0 :           if( local_effective_slot==ULONG_MAX ) {
     930           0 :             if( ctx->local_in.incremental_snapshot_slot!=ULONG_MAX ) {
     931           0 :               FD_LOG_INFO(( "local full snapshot at slot %lu cannot be used because local incremental snapshot at slot %lu "
     932           0 :                             "is too old and no downloadable incremental could be found (cluster slot %lu), downloading instead",
     933           0 :                             ctx->local_in.full_snapshot_slot, ctx->local_in.incremental_snapshot_slot, cluster_slot ));
     934           0 :             } else {
     935           0 :               FD_LOG_NOTICE(( "local full snapshot at slot %lu cannot be used because no matching incremental snapshot "
     936           0 :                               "could be found (cluster slot %lu), downloading instead",
     937           0 :                               ctx->local_in.full_snapshot_slot, cluster_slot ));
     938           0 :             }
     939           0 :           } else {
     940           0 :             FD_LOG_NOTICE(( "local full snapshot at slot %lu (effective slot %lu) is too old for cluster slot %lu max age %u, downloading instead",
     941           0 :                             ctx->local_in.full_snapshot_slot, local_effective_slot, cluster_slot, ctx->config.sources.max_local_full_effective_age ));
     942           0 :           }
     943           0 :         } else {
     944           0 :           FD_LOG_INFO(( "no local snapshot available, downloading from peer" ));
     945           0 :         }
     946             : 
     947           0 :         if( FD_UNLIKELY( !ctx->config.incremental_snapshots ) ) {
     948           0 :           send_expected_slot( ctx, stem, best.full_slot );
     949           0 :         } else {
     950           0 :           fd_sspeer_t best_incremental = fd_sspeer_selector_best( ctx->selector, 1, best.full_slot );
     951           0 :           if( FD_LIKELY( best_incremental.addr.l ) ) {
     952           0 :             ctx->predicted_incremental.slot = best_incremental.incr_slot;
     953           0 :             send_expected_slot( ctx, stem, best_incremental.incr_slot );
     954           0 :           }
     955           0 :         }
     956             : 
     957           0 :         ctx->peer                            = best;
     958           0 :         ctx->state                           = FD_SNAPCT_STATE_READING_FULL_HTTP;
     959           0 :         ctx->predicted_incremental.full_slot = best.full_slot;
     960           0 :         init_load( ctx, stem, 1, 0 );
     961           0 :         log_download( ctx, 1, best.addr, best.full_slot );
     962           0 :       }
     963           0 :       break;
     964           0 :     }
     965             : 
     966             :     /* ============================================================== */
     967           0 :     case FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL: {
     968           0 :       if( FD_UNLIKELY( now<ctx->deadline_nanos ) ) break;
     969             : 
     970           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
     971           0 :       if( FD_UNLIKELY( !best.addr.l ) ) {
     972           0 :         if( !ctx->gossip_enabled ) {
     973           0 :           FD_LOG_ERR(( "no incremental snapshot peers are available and discovery of new peers via gossip is disabled. aborting." ));
     974           0 :         }
     975           0 :         ctx->deadline_nanos = now + ctx->config.wait_for_peers_timeout_nanos;
     976           0 :         ctx->state = FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL;
     977           0 :         break;
     978           0 :       }
     979             : 
     980             :       /* decide whether to use the local incremental snapshot if one
     981             :          exists and is not too old, otherwise download a new incremental
     982             :          snapshot. */
     983           0 :       ulong cluster_slot  = fd_sspeer_selector_cluster_slot( ctx->selector ).incremental;
     984           0 :       ulong local_slot    = ctx->local_in.incremental_snapshot_slot;
     985           0 :       int   local_too_old = local_slot<fd_ulong_sat_sub( cluster_slot, ctx->config.sources.max_local_incremental_age );
     986           0 :       if( FD_LIKELY( local_slot!=ULONG_MAX && !local_too_old ) ) {
     987           0 :         ctx->predicted_incremental.slot = local_slot;
     988           0 :         send_expected_slot( ctx, stem, local_slot );
     989             : 
     990           0 :         FD_LOG_NOTICE(( "reading incremental snapshot from file %s%s%s", fd_log_style_dim(), ctx->local_in.incremental_snapshot_path, fd_log_style_normal() ));
     991           0 :         FD_LOG_INFO(( "reading incremental snapshot at slot %lu from local file `%s`", ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
     992           0 :         ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_FILE;
     993           0 :         init_load( ctx, stem, 0, 1 );
     994           0 :       } else {
     995           0 :         ctx->predicted_incremental.slot = best.incr_slot;
     996           0 :         send_expected_slot( ctx, stem, best.incr_slot );
     997             : 
     998           0 :         ctx->peer  = best;
     999           0 :         ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP;
    1000           0 :         init_load( ctx, stem, 0, 0 );
    1001           0 :         log_download( ctx, 0, best.addr, best.incr_slot );
    1002           0 :       }
    1003           0 :       break;
    1004           0 :     }
    1005             : 
    1006             :     /* ============================================================== */
    1007           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI:
    1008           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1009           0 :         ctx->malformed = 0;
    1010           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1011           0 :         ctx->flush_ack = 0;
    1012           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET;
    1013           0 :         FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from local file `%s`",
    1014           0 :                          ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
    1015           0 :         break;
    1016           0 :       }
    1017             : 
    1018           0 :       if( ctx->flush_ack < ctx->flush_ack_cnt ) break;
    1019             : 
    1020           0 :       ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE;
    1021           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_DONE, 0UL, 0UL, 0UL, 0UL, 0UL );
    1022           0 :       ctx->flush_ack = 0;
    1023           0 :       break;
    1024             : 
    1025             :     /* ============================================================== */
    1026           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE:
    1027           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1028           0 :         ctx->malformed = 0;
    1029           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1030           0 :         ctx->flush_ack = 0;
    1031           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET;
    1032           0 :         FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from local file `%s`",
    1033           0 :                          ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
    1034           0 :         break;
    1035           0 :       }
    1036             : 
    1037           0 :       if( ctx->flush_ack < ctx->flush_ack_cnt ) break;
    1038             : 
    1039           0 :       log_completion( ctx, 0/*incr*/ );
    1040           0 :       ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
    1041           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
    1042           0 :       break;
    1043             : 
    1044             :     /* ============================================================== */
    1045           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI:
    1046           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1047           0 :         ctx->malformed = 0;
    1048           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1049           0 :         ctx->flush_ack = 0;
    1050           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET;
    1051           0 :         FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
    1052           0 :                          "blacklisting peer due to download failure.",
    1053           0 :                          ctx->predicted_incremental.slot,
    1054           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_incr_snapshot_name ));
    1055           0 :         blacklist_peer( ctx );
    1056           0 :         break;
    1057           0 :       }
    1058             : 
    1059           0 :       if( ctx->flush_ack < ctx->flush_ack_cnt ) break;
    1060             : 
    1061           0 :       ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE;
    1062           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_DONE, 0UL, 0UL, 0UL, 0UL, 0UL );
    1063           0 :       ctx->flush_ack = 0;
    1064           0 :       break;
    1065             : 
    1066             :     /* ============================================================== */
    1067           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE:
    1068           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1069           0 :         ctx->malformed = 0;
    1070           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1071           0 :         ctx->flush_ack = 0;
    1072           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET;
    1073           0 :         FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
    1074           0 :                          "blacklisting peer due to download failure.",
    1075           0 :                          ctx->predicted_incremental.slot,
    1076           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_incr_snapshot_name ));
    1077           0 :         blacklist_peer( ctx );
    1078           0 :         break;
    1079           0 :       }
    1080             : 
    1081           0 :       if( ctx->flush_ack < ctx->flush_ack_cnt ) break;
    1082             : 
    1083           0 :       log_completion( ctx, 0/*incr*/ );
    1084           0 :       ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
    1085           0 :       rename_incr_snapshot( ctx );
    1086           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
    1087           0 :       break;
    1088             : 
    1089             :     /* ============================================================== */
    1090           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI:
    1091           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1092           0 :         ctx->malformed = 0;
    1093           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1094           0 :         ctx->flush_ack = 0;
    1095           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET;
    1096           0 :         FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from local file `%s`",
    1097           0 :                          ctx->local_in.full_snapshot_slot, ctx->local_in.full_snapshot_path ));
    1098           0 :         break;
    1099           0 :       }
    1100             : 
    1101           0 :       if( ctx->flush_ack < ctx->flush_ack_cnt ) break;
    1102             : 
    1103           0 :       ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE;
    1104           0 :       ulong sig = ctx->config.incremental_snapshots &&
    1105           0 :                   (ctx->local_in.incremental_snapshot_slot!=ULONG_MAX || ctx->download_enabled) ? FD_SNAPSHOT_MSG_CTRL_NEXT : FD_SNAPSHOT_MSG_CTRL_DONE;
    1106           0 :       if( sig==FD_SNAPSHOT_MSG_CTRL_DONE && ctx->config.incremental_snapshots ) {
    1107             :         /* set incremental snapshots to 0 if there is no local
    1108             :             incremental snapshot and download is not enabled. */
    1109           0 :         FD_LOG_INFO(( "incremental snapshots were enabled via [snapshots.incremental_snapshots] "
    1110           0 :                       "but no incremental snapshot exists on disk and no snapshot peers are configured. "
    1111           0 :                       "skipping incremental snapshot load." ));
    1112           0 :         ctx->config.incremental_snapshots = 0;
    1113           0 :       }
    1114           0 :       fd_stem_publish( stem, ctx->out_ld.idx, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
    1115           0 :       ctx->flush_ack = 0;
    1116           0 :       break;
    1117             : 
    1118             :     /* ============================================================== */
    1119           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE:
    1120           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1121           0 :         ctx->malformed = 0;
    1122           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1123           0 :         ctx->flush_ack = 0;
    1124           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET;
    1125           0 :         FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from local file `%s`",
    1126           0 :                          ctx->local_in.full_snapshot_slot, ctx->local_in.full_snapshot_path ));
    1127           0 :         break;
    1128           0 :       }
    1129             : 
    1130           0 :       if( ctx->flush_ack < ctx->flush_ack_cnt ) break;
    1131             : 
    1132           0 :       log_completion( ctx, 1/*full*/ );
    1133           0 :       if( FD_LIKELY( !ctx->config.incremental_snapshots ) ) {
    1134           0 :         ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
    1135           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
    1136           0 :         break;
    1137           0 :       }
    1138             : 
    1139           0 :       if( FD_LIKELY( ctx->download_enabled ) ) {
    1140           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
    1141           0 :         ctx->deadline_nanos = 0L;
    1142           0 :       } else {
    1143           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 ));
    1144           0 :         ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_FILE;
    1145           0 :         init_load( ctx, stem, 0, 1 );
    1146           0 :       }
    1147           0 :       break;
    1148             : 
    1149             :     /* ============================================================== */
    1150           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI:
    1151           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1152           0 :         ctx->malformed = 0;
    1153           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1154           0 :         ctx->flush_ack = 0;
    1155           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
    1156           0 :         FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
    1157           0 :                          "blacklisting peer due to download failure.",
    1158           0 :                          ctx->predicted_incremental.full_slot,
    1159           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_full_snapshot_name ));
    1160           0 :         blacklist_peer( ctx );
    1161           0 :         break;
    1162           0 :       }
    1163             : 
    1164           0 :       if( ctx->flush_ack < ctx->flush_ack_cnt ) break;
    1165             : 
    1166           0 :       ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE;
    1167           0 :       fd_stem_publish( stem, ctx->out_ld.idx, ctx->config.incremental_snapshots ? FD_SNAPSHOT_MSG_CTRL_NEXT : FD_SNAPSHOT_MSG_CTRL_DONE, 0UL, 0UL, 0UL, 0UL, 0UL );
    1168           0 :       ctx->flush_ack = 0;
    1169           0 :       break;
    1170             : 
    1171             :     /* ============================================================== */
    1172           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE:
    1173           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1174           0 :         ctx->malformed = 0;
    1175           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1176           0 :         ctx->flush_ack = 0;
    1177           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
    1178           0 :         FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
    1179           0 :                          "blacklisting peer due to download failure.",
    1180           0 :                          ctx->predicted_incremental.full_slot,
    1181           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_full_snapshot_name ));
    1182           0 :         blacklist_peer( ctx );
    1183           0 :         break;
    1184           0 :       }
    1185             : 
    1186           0 :       if( ctx->flush_ack < ctx->flush_ack_cnt ) break;
    1187             : 
    1188           0 :       rename_full_snapshot( ctx );
    1189             : 
    1190           0 :       log_completion( ctx, 1/*full*/ );
    1191           0 :       if( FD_LIKELY( !ctx->config.incremental_snapshots ) ) {
    1192           0 :         ctx->state = FD_SNAPCT_STATE_SHUTDOWN;
    1193           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_SHUTDOWN, 0UL, 0UL, 0UL, 0UL, 0UL );
    1194           0 :         break;
    1195           0 :       }
    1196             : 
    1197             :       /* Get the best incremental peer to download from */
    1198           0 :       fd_sspeer_t best = fd_sspeer_selector_best( ctx->selector, 1, ctx->predicted_incremental.full_slot );
    1199           0 :       if( FD_UNLIKELY( !best.addr.l ) ) {
    1200           0 :         ctx->deadline_nanos = now;
    1201           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
    1202           0 :         break;
    1203           0 :       }
    1204             : 
    1205           0 :       ctx->predicted_incremental.slot = best.incr_slot;
    1206           0 :       send_expected_slot( ctx, stem, best.incr_slot );
    1207             : 
    1208           0 :       ctx->peer  = best;
    1209           0 :       ctx->state = FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP;
    1210           0 :       init_load( ctx, stem, 0, 0 );
    1211           0 :       log_download( ctx, 0, best.addr, best.incr_slot );
    1212           0 :       break;
    1213             : 
    1214             :     /* ============================================================== */
    1215           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
    1216           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET:
    1217           0 :       if( FD_UNLIKELY( ctx->flush_ack<ctx->flush_ack_cnt ) ) break;
    1218             : 
    1219           0 :       if( ctx->metrics.full.num_retries==ctx->config.max_retry_abort ) {
    1220           0 :         FD_LOG_ERR(( "hit retry limit of %u for full snapshot, aborting", ctx->config.max_retry_abort ));
    1221           0 :       }
    1222             : 
    1223           0 :       ctx->metrics.full.num_retries++;
    1224           0 :       FD_LOG_NOTICE(( "retrying full snapshot download (attempt %u/%u)",
    1225           0 :                       ctx->metrics.full.num_retries, ctx->config.max_retry_abort ));
    1226             : 
    1227           0 :       ctx->metrics.full.bytes_read           = 0UL;
    1228           0 :       ctx->metrics.full.bytes_written        = 0UL;
    1229           0 :       ctx->metrics.full.bytes_total          = 0UL;
    1230             : 
    1231           0 :       ctx->metrics.incremental.bytes_read    = 0UL;
    1232           0 :       ctx->metrics.incremental.bytes_written = 0UL;
    1233           0 :       ctx->metrics.incremental.bytes_total   = 0UL;
    1234             : 
    1235           0 :       if( !ctx->download_enabled ) {
    1236             :         /* if we are unable to download new snapshots and unable to load
    1237             :            our local snapshot, we must shutdown the validator. */
    1238           0 :         FD_LOG_ERR(( "unable to load local snapshot %s and no snapshot peers were configured. aborting.", ctx->local_in.full_snapshot_path ));
    1239           0 :       } else {
    1240           0 :         if( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ) ctx->local_in.full_snapshot_slot = ULONG_MAX;
    1241           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS;
    1242           0 :         ctx->deadline_nanos = 0L;
    1243           0 :       }
    1244           0 :       break;
    1245             : 
    1246             :     /* ============================================================== */
    1247           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET:
    1248           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
    1249           0 :       if( FD_UNLIKELY( ctx->flush_ack<ctx->flush_ack_cnt ) ) break;
    1250             : 
    1251           0 :       if( ctx->metrics.incremental.num_retries==ctx->config.max_retry_abort ) {
    1252           0 :         FD_LOG_ERR(("hit retry limit of %u for incremental snapshot. aborting", ctx->config.max_retry_abort ));
    1253           0 :       }
    1254             : 
    1255           0 :       ctx->metrics.incremental.num_retries++;
    1256           0 :       FD_LOG_NOTICE(( "retrying incremental snapshot download (attempt %u/%u)",
    1257           0 :                       ctx->metrics.incremental.num_retries, ctx->config.max_retry_abort ));
    1258             : 
    1259           0 :       ctx->metrics.incremental.bytes_read    = 0UL;
    1260           0 :       ctx->metrics.incremental.bytes_written = 0UL;
    1261           0 :       ctx->metrics.incremental.bytes_total   = 0UL;
    1262             : 
    1263           0 :       if( !ctx->download_enabled ) {
    1264             :         /* if we are unable to download new snapshots and unable to load
    1265             :            our local snapshot, we must shutdown the validator. */
    1266           0 :         FD_LOG_ERR(( "unable to load local snapshot %s and no snapshot peers were configured. aborting.", ctx->local_in.incremental_snapshot_path ));
    1267           0 :       } else {
    1268           0 :         if( ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ctx->local_in.incremental_snapshot_slot = ULONG_MAX;
    1269           0 :         ctx->state = FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL;
    1270           0 :         ctx->deadline_nanos = 0L;
    1271           0 :       }
    1272           0 :       break;
    1273             : 
    1274             :     /* ============================================================== */
    1275           0 :     case FD_SNAPCT_STATE_READING_FULL_FILE:
    1276           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1277           0 :         ctx->malformed = 0;
    1278           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1279           0 :         ctx->flush_ack = 0;
    1280           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET;
    1281           0 :         FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from local file `%s`",
    1282           0 :                          ctx->local_in.full_snapshot_slot, ctx->local_in.full_snapshot_path ));
    1283           0 :         break;
    1284           0 :       }
    1285           0 :       if( FD_UNLIKELY( ctx->flush_ack < ctx->flush_ack_cnt ) ) break;
    1286           0 :       if( FD_UNLIKELY( ctx->load_complete ) ) {
    1287           0 :         ctx->load_complete = 0;
    1288           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FINI, 0UL, 0UL, 0UL, 0UL, 0UL );
    1289           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI;
    1290           0 :         ctx->flush_ack = 0;
    1291           0 :       }
    1292           0 :       break;
    1293             : 
    1294             :     /* ============================================================== */
    1295           0 :     case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE:
    1296           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1297           0 :         ctx->malformed = 0;
    1298           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1299           0 :         ctx->flush_ack = 0;
    1300           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET;
    1301           0 :         FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from local file `%s`",
    1302           0 :                          ctx->local_in.incremental_snapshot_slot, ctx->local_in.incremental_snapshot_path ));
    1303           0 :         break;
    1304           0 :       }
    1305           0 :       if( FD_UNLIKELY( ctx->flush_ack < ctx->flush_ack_cnt ) ) break;
    1306           0 :       if( FD_UNLIKELY( ctx->load_complete ) ) {
    1307           0 :         ctx->load_complete = 0;
    1308           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FINI, 0UL, 0UL, 0UL, 0UL, 0UL );
    1309           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI;
    1310           0 :         ctx->flush_ack = 0;
    1311           0 :       }
    1312           0 :       break;
    1313             : 
    1314             :     /* ============================================================== */
    1315           0 :     case FD_SNAPCT_STATE_READING_FULL_HTTP:
    1316           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1317           0 :         ctx->malformed = 0;
    1318           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1319           0 :         ctx->flush_ack = 0;
    1320           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET;
    1321           0 :         FD_LOG_WARNING(( "failed to load full snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
    1322           0 :                          "blacklisting peer due to download failure",
    1323           0 :                          ctx->predicted_incremental.full_slot,
    1324           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_full_snapshot_name ));
    1325           0 :         blacklist_peer( ctx );
    1326           0 :         break;
    1327           0 :       }
    1328           0 :       if( FD_UNLIKELY( ctx->flush_ack < ctx->flush_ack_cnt ) ) break;
    1329           0 :       if( FD_UNLIKELY( ctx->load_complete ) ) {
    1330           0 :         ctx->load_complete = 0;
    1331           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FINI, 0UL, 0UL, 0UL, 0UL, 0UL );
    1332           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI;
    1333           0 :         ctx->flush_ack = 0;
    1334           0 :       }
    1335           0 :       break;
    1336             : 
    1337             :     /* ============================================================== */
    1338           0 :     case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP:
    1339           0 :       if( FD_UNLIKELY( ctx->malformed ) ) {
    1340           0 :         ctx->malformed = 0;
    1341           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FAIL, 0UL, 0UL, 0UL, 0UL, 0UL );
    1342           0 :         ctx->flush_ack = 0;
    1343           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET;
    1344           0 :         FD_LOG_WARNING(( "failed to load incremental snapshot at slot %lu from http://" FD_IP4_ADDR_FMT ":%hu/%s. "
    1345           0 :                          "blacklisting peer due to download failure",
    1346           0 :                          ctx->predicted_incremental.slot,
    1347           0 :                          FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ), ctx->http_incr_snapshot_name ));
    1348           0 :         blacklist_peer( ctx );
    1349           0 :         break;
    1350           0 :       }
    1351           0 :       if( FD_UNLIKELY( ctx->flush_ack < ctx->flush_ack_cnt ) ) break;
    1352           0 :       if( FD_UNLIKELY( ctx->load_complete ) ) {
    1353           0 :         ctx->load_complete = 0;
    1354           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_FINI, 0UL, 0UL, 0UL, 0UL, 0UL );
    1355           0 :         ctx->state = FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI;
    1356           0 :         ctx->flush_ack = 0;
    1357           0 :       }
    1358           0 :       break;
    1359             : 
    1360             :     /* ============================================================== */
    1361           0 :     case FD_SNAPCT_STATE_SHUTDOWN:
    1362             :       /* Transitioning to the shutdown state indicates snapshot load is
    1363             :          completed without errors.  Otherwise, snapct would have aborted
    1364             :          earlier. */
    1365           0 :       break;
    1366             : 
    1367             :     /* ============================================================== */
    1368           0 :     default: FD_LOG_ERR(( "unexpected state %s", fd_snapct_state_str( ctx->state ) ));
    1369           0 :   }
    1370           0 : }
    1371             : 
    1372             : static void
    1373             : gossip_frag( fd_snapct_tile_t *  ctx,
    1374             :              ulong               sig,
    1375             :              ulong               sz FD_PARAM_UNUSED,
    1376          27 :              ulong               chunk ) {
    1377          27 :   FD_TEST( ctx->gossip_enabled );
    1378             : 
    1379          27 :   if( FD_UNLIKELY( sig==FD_GOSSIP_UPDATE_TAG_PEER_SATURATED ) ) {
    1380           0 :     ctx->gossip.saturated = 1;
    1381           0 :     return;
    1382           0 :   }
    1383             : 
    1384          27 :   if( !( sig==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ||
    1385          27 :          sig==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE ||
    1386          27 :          sig==FD_GOSSIP_UPDATE_TAG_SNAPSHOT_HASHES ) ) return;
    1387             : 
    1388          27 :   fd_gossip_update_message_t const * msg = fd_chunk_to_laddr_const( ctx->gossip_in_mem, chunk );
    1389          27 :   switch( msg->tag ) {
    1390          21 :     case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO: {
    1391          21 :       FD_TEST( msg->contact_info->idx<GOSSIP_PEERS_MAX );
    1392          21 :       fd_pubkey_t const * pubkey = (fd_pubkey_t const *)msg->origin;
    1393          21 :       gossip_ci_entry_t * entry = ctx->gossip.ci_table + msg->contact_info->idx;
    1394          21 :       if( FD_UNLIKELY( !fd_pubkey_eq( &entry->pubkey, pubkey ) ) ) {
    1395             :         /* Initialize the new gossip entry, which may or may not be allowed */
    1396          18 :         FD_TEST( fd_pubkey_check_zero( &entry->pubkey ) );
    1397          18 :         entry->pubkey      = *pubkey;
    1398          18 :         entry->rpc_addr.l  = 0UL;
    1399          18 :         if( ctx->config.sources.gossip.allow_any ) {
    1400           9 :           entry->allowed = 1;
    1401           9 :           for( ulong i=0UL; i<ctx->config.sources.gossip.block_list_cnt; i++ ) {
    1402           3 :             if( fd_pubkey_eq( pubkey, &ctx->config.sources.gossip.block_list[ i ] ) ) {
    1403           3 :               entry->allowed = 0;
    1404           3 :               break;
    1405           3 :             }
    1406           3 :           }
    1407           9 :         } else {
    1408           9 :           entry->allowed = 0;
    1409           9 :           for( ulong i=0UL; i<ctx->config.sources.gossip.allow_list_cnt; i++ ) {
    1410           3 :             if( fd_pubkey_eq( pubkey, &ctx->config.sources.gossip.allow_list[ i ] ) ) {
    1411           3 :               entry->allowed = 1;
    1412           3 :               break;
    1413           3 :             }
    1414           3 :           }
    1415           9 :         }
    1416          18 :         FD_TEST(  ULONG_MAX==gossip_ci_map_idx_query_const( ctx->gossip.ci_map, pubkey, ULONG_MAX, ctx->gossip.ci_table ) );
    1417          18 :         if( entry->allowed ) {
    1418           9 :           gossip_ci_map_idx_insert( ctx->gossip.ci_map, msg->contact_info->idx, ctx->gossip.ci_table );
    1419           9 :           ctx->gossip.allowed_cnt++;
    1420             :           /* Allow-list shortcut: if an explicit allow list is
    1421             :              configured and all expected peers have arrived, declare
    1422             :              saturation immediately without waiting for the gossip
    1423             :              tile's general saturation signal. */
    1424           9 :           if( FD_UNLIKELY( !ctx->config.sources.gossip.allow_any &&
    1425           9 :                            ctx->config.sources.gossip.allow_list_cnt>0UL &&
    1426           9 :                            ctx->gossip.allowed_cnt==ctx->config.sources.gossip.allow_list_cnt ) ) {
    1427           3 :             FD_LOG_NOTICE(( "all %lu allowed gossip peers discovered", ctx->config.sources.gossip.allow_list_cnt ));
    1428           3 :             ctx->gossip.saturated = 1;
    1429           3 :           }
    1430           9 :         }
    1431          18 :       }
    1432          21 :       if( !entry->allowed ) break;
    1433             :       /* Maybe update the RPC address of a new or existing allowed gossip peer */
    1434          12 :       fd_ip4_port_t cur_addr = entry->rpc_addr;
    1435          12 :       fd_ip4_port_t new_addr;
    1436          12 :       new_addr.addr = msg->contact_info->value->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC ].is_ipv6 ? 0 : msg->contact_info->value->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC ].ip4;
    1437          12 :       new_addr.port = msg->contact_info->value->sockets[ FD_GOSSIP_CONTACT_INFO_SOCKET_RPC ].port;
    1438             : 
    1439          12 :       if( FD_UNLIKELY( new_addr.l!=cur_addr.l ) ) {
    1440           0 :         fd_sspeer_key_t entry_key = {0};
    1441           0 :         *entry_key.pubkey = entry->pubkey;
    1442           0 :         entry_key.is_url  = 0;
    1443           0 :         if( FD_LIKELY( !!new_addr.l ) ) {
    1444             :           /* Do not re-add peers that have been permanently blacklisted
    1445             :              or whose new address is temporarily banned by ssping.
    1446             :              Bookkeeping (ssping cleanup, rpc_addr update) still runs
    1447             :              so the CI table and ssping pool stay consistent. */
    1448           0 :           if( FD_LIKELY( !blacklist_map_ele_query( ctx->blacklist_map, &entry_key, NULL, ctx->blacklist_pool ) &&
    1449           0 :                          !fd_ssping_is_invalidated( ctx->ssping, new_addr ) ) ) {
    1450           0 :             if( FD_LIKELY( FD_SSPEER_SCORE_INVALID!=fd_sspeer_selector_add( ctx->selector, &entry_key, new_addr,
    1451           0 :                                                                             FD_SSPEER_LATENCY_UNKNOWN, FD_SSPEER_SLOT_UNKNOWN,
    1452           0 :                                                                             FD_SSPEER_SLOT_UNKNOWN, NULL, NULL ) ) ) {
    1453           0 :               fd_ssping_add( ctx->ssping, new_addr );
    1454           0 :             }
    1455           0 :           }
    1456           0 :         } else {
    1457           0 :           fd_sspeer_selector_remove( ctx->selector, &entry_key );
    1458           0 :         }
    1459           0 :         if( FD_LIKELY( !!cur_addr.l ) ) {
    1460           0 :           fd_ssping_remove( ctx->ssping, cur_addr );
    1461           0 :         }
    1462           0 :         entry->rpc_addr = new_addr;
    1463           0 :         if( !ctx->config.sources.gossip.allow_any ) {
    1464           0 :           FD_BASE58_ENCODE_32_BYTES( pubkey->uc, pubkey_b58 );
    1465           0 :           if( FD_LIKELY( !!new_addr.l ) ) {
    1466           0 :             FD_LOG_NOTICE(( "allowed gossip peer added with public key `%s` and RPC address `" FD_IP4_ADDR_FMT ":%hu`",
    1467           0 :                             pubkey_b58, FD_IP4_ADDR_FMT_ARGS( new_addr.addr ), fd_ushort_bswap( new_addr.port ) ));
    1468           0 :           } else {
    1469           0 :             FD_LOG_WARNING(( "allowed gossip peer with public key `%s` does not advertise an RPC address", pubkey_b58 ));
    1470           0 :           }
    1471           0 :         }
    1472           0 :       }
    1473          12 :       break;
    1474          21 :     }
    1475           6 :     case FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE: {
    1476           6 :       FD_TEST( msg->contact_info_remove->idx<GOSSIP_PEERS_MAX );
    1477           6 :       gossip_ci_entry_t * entry = ctx->gossip.ci_table + msg->contact_info_remove->idx;
    1478           6 :       ulong rem_idx = gossip_ci_map_idx_remove( ctx->gossip.ci_map, &entry->pubkey, ULONG_MAX, ctx->gossip.ci_table );
    1479           6 :       if( rem_idx!=ULONG_MAX ) {
    1480           3 :         FD_TEST( entry->allowed && rem_idx==msg->contact_info_remove->idx );
    1481           3 :         ctx->gossip.allowed_cnt--;
    1482           3 :         fd_ip4_port_t addr = entry->rpc_addr;
    1483           3 :         if( FD_LIKELY( !!addr.l ) ) {
    1484           0 :           fd_ssping_remove( ctx->ssping, addr );
    1485           0 :           fd_sspeer_key_t entry_key = {0};
    1486           0 :           *entry_key.pubkey = entry->pubkey;
    1487           0 :           entry_key.is_url  = 0;
    1488           0 :           fd_sspeer_selector_remove( ctx->selector, &entry_key );
    1489           0 :         }
    1490           3 :         if( !ctx->config.sources.gossip.allow_any ) {
    1491           0 :           FD_BASE58_ENCODE_32_BYTES( entry->pubkey.uc, pubkey_b58 );
    1492           0 :           FD_LOG_WARNING(( "allowed gossip peer removed with public key `%s` and RPC address `" FD_IP4_ADDR_FMT ":%hu`",
    1493           0 :                            pubkey_b58, FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ) ));
    1494           0 :         }
    1495           3 :       }
    1496           6 :       fd_memset( entry, 0, sizeof(*entry) );
    1497           6 :       break;
    1498           6 :     }
    1499           0 :     case FD_GOSSIP_UPDATE_TAG_SNAPSHOT_HASHES: {
    1500           0 :       ulong idx = gossip_ci_map_idx_query_const( ctx->gossip.ci_map, (fd_pubkey_t const *)msg->origin, ULONG_MAX, ctx->gossip.ci_table );
    1501           0 :       if( FD_LIKELY( idx!=ULONG_MAX ) ) {
    1502           0 :         gossip_ci_entry_t * entry = ctx->gossip.ci_table + idx;
    1503           0 :         FD_TEST( entry->allowed );
    1504           0 :         fd_sspeer_key_t entry_key = {0};
    1505           0 :         *entry_key.pubkey = entry->pubkey;
    1506           0 :         entry_key.is_url  = 0;
    1507           0 :         on_snapshot_hash( ctx, &entry_key, entry->rpc_addr, msg );
    1508           0 :       }
    1509           0 :       break;
    1510           0 :     }
    1511           0 :     default:
    1512           0 :       FD_LOG_ERR(( "snapct: unexpected gossip tag %u", (uint)msg->tag ));
    1513           0 :       break;
    1514          27 :   }
    1515          27 : }
    1516             : 
    1517             : /* Validate and handle a pipeline control ack for INIT_{FULL,INCR},
    1518             :    NEXT, DONE, and FINI.  Returns 0 on success, and -1 if the sig was
    1519             :    unrecognized or the state was unexpected. */
    1520             : static int
    1521             : process_ctrl_ack( fd_snapct_tile_t * ctx,
    1522           0 :                   ulong              sig ) {
    1523           0 :   switch( sig ) {
    1524           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
    1525           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_READING_FULL_HTTP ||
    1526           0 :                      ctx->state==FD_SNAPCT_STATE_READING_FULL_FILE ) ) {
    1527           0 :         ctx->flush_ack++;
    1528           0 :         FD_TEST( ctx->flush_ack <= ctx->flush_ack_cnt );
    1529           0 :       } else if( FD_UNLIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET ||
    1530           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ) ) {
    1531             :         /* Safe to ignore -- stale ack from before RESET. */
    1532           0 :       } else return -1;
    1533           0 :       return 0;
    1534             : 
    1535           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_INCR:
    1536           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP ||
    1537           0 :                      ctx->state==FD_SNAPCT_STATE_READING_INCREMENTAL_FILE ) ) {
    1538           0 :         ctx->flush_ack++;
    1539           0 :         FD_TEST( ctx->flush_ack <= ctx->flush_ack_cnt );
    1540           0 :       } else if( FD_UNLIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET ||
    1541           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ) {
    1542             :         /* Safe to ignore -- stale ack from before RESET. */
    1543           0 :       } else return -1;
    1544           0 :       return 0;
    1545             : 
    1546           0 :     case FD_SNAPSHOT_MSG_CTRL_NEXT:
    1547           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE ||
    1548           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE ) ) {
    1549           0 :         ctx->flush_ack++;
    1550           0 :         FD_TEST( ctx->flush_ack <= ctx->flush_ack_cnt );
    1551           0 :       } else if( FD_UNLIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET ||
    1552           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ) ) {
    1553             :         /* Safe to ignore -- stale ack from before RESET. */
    1554           0 :       } else return -1;
    1555           0 :       return 0;
    1556             : 
    1557           0 :     case FD_SNAPSHOT_MSG_CTRL_DONE:
    1558           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE ||
    1559           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE ||
    1560           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE ||
    1561           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE ) ) {
    1562           0 :         ctx->flush_ack++;
    1563           0 :         FD_TEST( ctx->flush_ack <= ctx->flush_ack_cnt );
    1564           0 :       } else if( FD_UNLIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET ||
    1565           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ||
    1566           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET ||
    1567           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ) {
    1568             :         /* Safe to ignore -- stale ack from before RESET. */
    1569           0 :       } else return -1;
    1570           0 :       return 0;
    1571             : 
    1572           0 :     case FD_SNAPSHOT_MSG_CTRL_FINI:
    1573           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI ||
    1574           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI ||
    1575           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI ||
    1576           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI ) ) {
    1577           0 :         ctx->flush_ack++;
    1578           0 :         FD_TEST( ctx->flush_ack <= ctx->flush_ack_cnt );
    1579           0 :       } else if( FD_UNLIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET ||
    1580           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ||
    1581           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET ||
    1582           0 :                               ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ) {
    1583             :         /* Safe to ignore -- stale ack from before RESET. */
    1584           0 :       } else return -1;
    1585           0 :       return 0;
    1586             : 
    1587           0 :     default:
    1588           0 :       return -1;
    1589           0 :   }
    1590           0 : }
    1591             : 
    1592             : static void
    1593             : snapld_frag( fd_snapct_tile_t *  ctx,
    1594             :              ulong               sig,
    1595             :              ulong               sz,
    1596             :              ulong               chunk,
    1597          15 :              fd_stem_context_t * stem ) {
    1598          15 :   if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_META ) ) {
    1599             :     /* Before snapld starts sending down data fragments, it first sends
    1600             :        a metadata message containing the total size of the snapshot.
    1601             :        Both file and HTTP paths send this message. */
    1602           0 :     int full, file;
    1603           0 :     switch( ctx->state ) {
    1604           0 :       case FD_SNAPCT_STATE_READING_FULL_FILE:        full = 1; file = 1; break;
    1605           0 :       case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE: full = 0; file = 1; break;
    1606           0 :       case FD_SNAPCT_STATE_READING_FULL_HTTP:        full = 1; file = 0; break;
    1607           0 :       case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP: full = 0; file = 0; break;
    1608             : 
    1609           0 :       case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET:
    1610           0 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET:
    1611           0 :       case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
    1612           0 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
    1613           0 :         return; /* Ignore */
    1614           0 :       default: FD_LOG_ERR(( "invalid meta frag in state %s", fd_snapct_state_str( ctx->state ) ));
    1615           0 :     }
    1616             : 
    1617           0 :     FD_TEST( sz==sizeof(fd_ssctrl_meta_t) );
    1618           0 :     fd_ssctrl_meta_t const * meta = fd_chunk_to_laddr_const( ctx->snapld_in_mem, chunk );
    1619             : 
    1620           0 :     if( FD_UNLIKELY( meta->total_sz==0UL ) ) {
    1621           0 :       if( FD_UNLIKELY( !ctx->malformed ) ) {
    1622           0 :         FD_LOG_WARNING(( "received zero-length metadata for %s snapshot, marking malformed", full ? "full" : "incremental" ));
    1623           0 :         ctx->malformed = 1;
    1624           0 :         fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
    1625           0 :       }
    1626           0 :       return;
    1627           0 :     }
    1628             : 
    1629           0 :     if( full ) ctx->metrics.full.bytes_total        = meta->total_sz;
    1630           0 :     else       ctx->metrics.incremental.bytes_total = meta->total_sz;
    1631             : 
    1632             :     /* Publish snapshot path to GUI.  For file loads, use the local
    1633             :        path directly.  For HTTP downloads, construct the full URL from
    1634             :        the resolved snapshot name. */
    1635           0 :     if( file ) {
    1636           0 :       if( FD_LIKELY( !!ctx->out_gui.mem ) ) {
    1637           0 :         snapshot_path_gui_publish( ctx, stem, full ? ctx->local_in.full_snapshot_path : ctx->local_in.incremental_snapshot_path, full );
    1638           0 :       }
    1639           0 :     } else {
    1640           0 :       if( full ) fd_cstr_ncpy( ctx->http_full_snapshot_name, meta->resolved_name, PATH_MAX );
    1641           0 :       else       fd_cstr_ncpy( ctx->http_incr_snapshot_name, meta->resolved_name, PATH_MAX );
    1642             : 
    1643           0 :       if( FD_LIKELY( !!ctx->out_gui.mem ) ) {
    1644           0 :         char snapshot_path[ PATH_MAX+30UL ]; /* 30 is fd_cstr_nlen( "https://255.255.255.255:65536/", ULONG_MAX ) */
    1645           0 :         FD_TEST( fd_cstr_printf_check( snapshot_path, sizeof(snapshot_path), NULL, "http://" FD_IP4_ADDR_FMT ":%hu/%s",
    1646           0 :                  FD_IP4_ADDR_FMT_ARGS( ctx->peer.addr.addr ), fd_ushort_bswap( ctx->peer.addr.port ),
    1647           0 :                  full ? ctx->http_full_snapshot_name : ctx->http_incr_snapshot_name ) );
    1648           0 :         snapshot_path_gui_publish( ctx, stem, snapshot_path, full );
    1649           0 :       }
    1650           0 :     }
    1651             : 
    1652           0 :     return;
    1653           0 :   }
    1654          15 :   if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_CTRL_FAIL ) ) {
    1655           0 :     if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET ||
    1656           0 :                    ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ||
    1657           0 :                    ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET ||
    1658           0 :                    ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ) {
    1659           0 :       ctx->flush_ack++;
    1660           0 :       FD_TEST( ctx->flush_ack <= ctx->flush_ack_cnt );
    1661           0 :     } else {
    1662           0 :       FD_LOG_ERR(( "unexpected control frag %lu (%s) in state %d (%s)", sig, fd_ssctrl_msg_ctrl_str( sig ), ctx->state, fd_snapct_state_str( ctx->state ) ));
    1663           0 :     }
    1664           0 :     return;
    1665           0 :   }
    1666          15 :   if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_CTRL_ERROR ) ) {
    1667             :     /* CTRL_ERROR message directly from snapld can be snapld-generated
    1668             :        or snapct-generated-and-forwarded. */
    1669           0 :     switch( ctx->state ) {
    1670           0 :       case FD_SNAPCT_STATE_READING_FULL_FILE:
    1671           0 :       case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI:
    1672           0 :       case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE:
    1673           0 :       case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE:
    1674           0 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI:
    1675           0 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE:
    1676           0 :       case FD_SNAPCT_STATE_READING_FULL_HTTP:
    1677           0 :       case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI:
    1678           0 :       case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE:
    1679           0 :       case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP:
    1680           0 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI:
    1681           0 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE:
    1682           0 :         FD_LOG_WARNING(( "received error from snapld in state %d (%s)",
    1683           0 :                          ctx->state, fd_snapct_state_str( ctx->state ) ));
    1684           0 :         ctx->malformed = 1;
    1685           0 :         break;
    1686           0 :       default:
    1687           0 :         break;
    1688           0 :     }
    1689           0 :     return;
    1690           0 :   }
    1691          15 :   if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_LOAD_COMPLETE ) ) {
    1692          15 :     int full = 0;
    1693          15 :     switch( ctx->state ) {
    1694           3 :       case FD_SNAPCT_STATE_READING_FULL_FILE:
    1695           6 :       case FD_SNAPCT_STATE_READING_FULL_HTTP:        full = 1; break;
    1696           0 :       case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE:
    1697           3 :       case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP: full = 0; break;
    1698           3 :       case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET:
    1699           3 :       case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
    1700           3 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET:
    1701           6 :       case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
    1702           6 :         return; /* Ignore during reset. */
    1703           0 :       default:
    1704           0 :         FD_LOG_ERR(( "invalid load_complete in state %s", fd_snapct_state_str( ctx->state ) ));
    1705           0 :         return;
    1706          15 :     }
    1707           9 :     if( FD_UNLIKELY( ctx->malformed ) ) return;
    1708             :     /* Validate that all expected bytes were received. */
    1709           6 :     ulong bytes_read  = full ? ctx->metrics.full.bytes_read  : ctx->metrics.incremental.bytes_read;
    1710           6 :     ulong bytes_total = full ? ctx->metrics.full.bytes_total : ctx->metrics.incremental.bytes_total;
    1711           6 :     if( FD_UNLIKELY( !bytes_total || bytes_read!=bytes_total ) ) {
    1712           0 :       ctx->malformed = 1;
    1713           0 :       FD_LOG_WARNING(( "load_complete but bytes_read %lu != bytes_total %lu for %s snapshot",
    1714           0 :                        bytes_read, bytes_total, full ? "full" : "incremental" ));
    1715           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
    1716           0 :       return;
    1717           0 :     }
    1718           6 :     ctx->load_complete = 1;
    1719           6 :     return;
    1720           6 :   }
    1721           0 :   if( FD_UNLIKELY( sig!=FD_SNAPSHOT_MSG_DATA ) ) {
    1722           0 :     if( process_ctrl_ack( ctx, sig ) ) {
    1723           0 :       FD_LOG_ERR(( "unexpected control frag %lu (%s) in state %d (%s)", sig, fd_ssctrl_msg_ctrl_str( sig ), ctx->state, fd_snapct_state_str( ctx->state ) ));
    1724           0 :     }
    1725           0 :     return;
    1726           0 :   }
    1727             : 
    1728           0 :   int full, file;
    1729           0 :   switch( ctx->state ) {
    1730             :     /* Expected cases, fall through below */
    1731           0 :     case FD_SNAPCT_STATE_READING_FULL_FILE:        full = 1; file = 1; break;
    1732           0 :     case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE: full = 0; file = 1; break;
    1733           0 :     case FD_SNAPCT_STATE_READING_FULL_HTTP:        full = 1; file = 0; break;
    1734           0 :     case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP: full = 0; file = 0; break;
    1735             : 
    1736           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET:
    1737           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET:
    1738           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET:
    1739           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET:
    1740             :       /* We are waiting for a reset to fully propagate through the
    1741             :          pipeline, just throw away any trailing data frags. */
    1742           0 :       return;
    1743             : 
    1744           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI:
    1745           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI:
    1746           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI:
    1747           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI:
    1748           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE:
    1749           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE:
    1750           0 :     case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE:
    1751           0 :     case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE:
    1752             :       /* Based on previously received data frags, we expected that the
    1753             :          current full / incremental snapshot was finished, but then we
    1754             :          received additional data frags.  Unsafe to continue so throw
    1755             :          away the whole snapshot.  Do not publish MSG_CTRL_ERROR here:
    1756             :          data forwarding is already complete, and the state machine
    1757             :          will publish MSG_CTRL_FAIL on the next tick. */
    1758           0 :       if( !ctx->malformed ) {
    1759           0 :         ctx->malformed = 1;
    1760           0 :         FD_LOG_WARNING(( "complete snapshot loaded but read %lu extra bytes", sz ));
    1761           0 :       }
    1762           0 :       return;
    1763             : 
    1764           0 :     case FD_SNAPCT_STATE_WAITING_FOR_PEERS:
    1765           0 :     case FD_SNAPCT_STATE_WAITING_FOR_PEERS_INCREMENTAL:
    1766           0 :     case FD_SNAPCT_STATE_COLLECTING_PEERS:
    1767           0 :     case FD_SNAPCT_STATE_COLLECTING_PEERS_INCREMENTAL:
    1768           0 :     case FD_SNAPCT_STATE_SHUTDOWN:
    1769           0 :     default:
    1770           0 :       FD_LOG_ERR(( "invalid data frag in state %s", fd_snapct_state_str( ctx->state ) ));
    1771           0 :       return;
    1772           0 :   }
    1773             : 
    1774           0 :   if( FD_UNLIKELY( full  && ctx->metrics.full.bytes_total==0UL ) ) {
    1775           0 :     if( !ctx->malformed ) {
    1776           0 :       ctx->malformed = 1;
    1777           0 :       FD_LOG_WARNING(( "received data frag for full snapshot with zero bytes_total" ));
    1778           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
    1779           0 :     }
    1780           0 :     return;
    1781           0 :   }
    1782           0 :   if( FD_UNLIKELY( !full && ctx->metrics.incremental.bytes_total==0UL ) ) {
    1783           0 :     if( !ctx->malformed ) {
    1784           0 :       ctx->malformed = 1;
    1785           0 :       FD_LOG_WARNING(( "received data frag for incremental snapshot with zero bytes_total" ));
    1786           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
    1787           0 :     }
    1788           0 :     return;
    1789           0 :   }
    1790             : 
    1791           0 :   if( full ) ctx->metrics.full.bytes_read        += sz;
    1792           0 :   else       ctx->metrics.incremental.bytes_read += sz;
    1793             : 
    1794           0 :   if( !file && -1!=ctx->local_out.dir_fd ) {
    1795           0 :     uchar const * data = fd_chunk_to_laddr_const( ctx->snapld_in_mem, chunk );
    1796           0 :     int fd = full ? ctx->local_out.full_snapshot_fd : ctx->local_out.incremental_snapshot_fd;
    1797           0 :     ulong written_sz = 0;
    1798           0 :     while( written_sz<sz ) {
    1799           0 :       long result = write( fd, data+written_sz, sz-written_sz );
    1800           0 :       if( FD_UNLIKELY( -1==result && errno==EINTR ) ) continue;
    1801           0 :       if( FD_UNLIKELY( -1==result && errno==ENOSPC ) ) {
    1802           0 :         FD_LOG_ERR(( "Out of disk space when writing out snapshot data to `%s`", ctx->config.snapshots_path ));
    1803           0 :       } else if( FD_UNLIKELY( 0L>result ) ) {
    1804           0 :         FD_LOG_ERR(( "write() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
    1805           0 :       } else if( FD_UNLIKELY( 0L==result ) ) {
    1806           0 :         FD_LOG_ERR(( "write() returned 0 for non-zero count" ));
    1807           0 :       }
    1808             : 
    1809           0 :       written_sz += (ulong)result;
    1810           0 :     }
    1811           0 :     if( full ) ctx->metrics.full.bytes_written        += sz;
    1812           0 :     else       ctx->metrics.incremental.bytes_written += sz;
    1813           0 :   }
    1814             : 
    1815           0 :   if( FD_UNLIKELY( ( full && ctx->metrics.full.bytes_read        > ctx->metrics.full.bytes_total ) ||
    1816           0 :                    (!full && ctx->metrics.incremental.bytes_read > ctx->metrics.incremental.bytes_total ) ) ) {
    1817           0 :     if( !ctx->malformed ) {
    1818           0 :       ctx->malformed = 1;
    1819           0 :       FD_LOG_WARNING(( "expected %s snapshot size of %lu bytes but read %lu bytes",
    1820           0 :                        full ? "full" : "incremental",
    1821           0 :                        full ? ctx->metrics.full.bytes_total : ctx->metrics.incremental.bytes_total,
    1822           0 :                        full ? ctx->metrics.full.bytes_read  : ctx->metrics.incremental.bytes_read ));
    1823           0 :       fd_stem_publish( stem, ctx->out_ld.idx, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
    1824           0 :     }
    1825           0 :     return;
    1826           0 :   }
    1827           0 : }
    1828             : 
    1829             : static void
    1830             : ctrl_ack_frag( fd_snapct_tile_t *  ctx,
    1831           0 :                ulong               sig ) {
    1832           0 :   switch( sig ) {
    1833           0 :     case FD_SNAPSHOT_MSG_CTRL_FAIL:
    1834           0 :       if( FD_LIKELY( ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_RESET ||
    1835           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_FULL_FILE_RESET ||
    1836           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_RESET ||
    1837           0 :                      ctx->state==FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_RESET ) ) {
    1838           0 :         ctx->flush_ack++;
    1839           0 :         FD_TEST( ctx->flush_ack <= ctx->flush_ack_cnt );
    1840           0 :       } else {
    1841           0 :         FD_LOG_ERR(( "unexpected control frag %lu (%s) in state %d (%s)", sig, fd_ssctrl_msg_ctrl_str( sig ), ctx->state, fd_snapct_state_str( ctx->state ) ));
    1842           0 :       }
    1843           0 :       return;
    1844             : 
    1845           0 :     case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:
    1846           0 :       return;
    1847             : 
    1848           0 :     case FD_SNAPSHOT_MSG_CTRL_ERROR:
    1849           0 :       switch( ctx->state ) {
    1850           0 :         case FD_SNAPCT_STATE_READING_FULL_FILE:
    1851           0 :         case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_FINI:
    1852           0 :         case FD_SNAPCT_STATE_FLUSHING_FULL_FILE_DONE:
    1853           0 :         case FD_SNAPCT_STATE_READING_INCREMENTAL_FILE:
    1854           0 :         case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_FINI:
    1855           0 :         case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_FILE_DONE:
    1856           0 :         case FD_SNAPCT_STATE_READING_FULL_HTTP:
    1857           0 :         case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_FINI:
    1858           0 :         case FD_SNAPCT_STATE_FLUSHING_FULL_HTTP_DONE:
    1859           0 :         case FD_SNAPCT_STATE_READING_INCREMENTAL_HTTP:
    1860           0 :         case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_FINI:
    1861           0 :         case FD_SNAPCT_STATE_FLUSHING_INCREMENTAL_HTTP_DONE:
    1862             :           /* Do not publish MSG_CTRL_ERROR: the error originated
    1863             :              downstream, so re-publishing would be redundant.
    1864             :              MSG_CTRL_FAIL follows on the next state machine tick. */
    1865           0 :           FD_LOG_WARNING(( "received error from downstream tile while in state %s",
    1866           0 :                            fd_snapct_state_str( ctx->state ) ));
    1867           0 :           ctx->malformed = 1;
    1868           0 :           break;
    1869           0 :         default:
    1870           0 :           break;
    1871           0 :       }
    1872           0 :       return;
    1873             : 
    1874           0 :     default:
    1875           0 :       break;
    1876           0 :   }
    1877           0 :   if( process_ctrl_ack( ctx, sig ) ) {
    1878           0 :     FD_LOG_ERR(( "unexpected control frag %lu (%s) in state %d (%s)", sig, fd_ssctrl_msg_ctrl_str( sig ), ctx->state, fd_snapct_state_str( ctx->state ) ));
    1879           0 :   }
    1880           0 : }
    1881             : 
    1882             : static int
    1883             : returnable_frag( fd_snapct_tile_t *  ctx,
    1884             :                  ulong               in_idx,
    1885             :                  ulong               seq    FD_PARAM_UNUSED,
    1886             :                  ulong               sig,
    1887             :                  ulong               chunk,
    1888             :                  ulong               sz,
    1889             :                  ulong               ctl    FD_PARAM_UNUSED,
    1890             :                  ulong               tsorig FD_PARAM_UNUSED,
    1891             :                  ulong               tspub  FD_PARAM_UNUSED,
    1892           0 :                  fd_stem_context_t * stem ) {
    1893           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
    1894           0 :     gossip_frag( ctx, sig, sz, chunk );
    1895           0 :   } else if( ctx->in_kind[ in_idx ]==IN_KIND_SNAPLD ) {
    1896           0 :     snapld_frag( ctx, sig, sz, chunk, stem );
    1897           0 :   } else if( ctx->in_kind[ in_idx ]==IN_KIND_ACK ) {
    1898           0 :     ctrl_ack_frag( ctx, sig );
    1899           0 :   } else FD_LOG_ERR(( "invalid in_kind %lu %u", in_idx, (uint)ctx->in_kind[ in_idx ] ));
    1900           0 :   return 0;
    1901           0 : }
    1902             : 
    1903             : static void
    1904             : privileged_init( fd_topo_t const *      topo,
    1905           0 :                  fd_topo_tile_t const * tile ) {
    1906           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1907             : 
    1908           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1909           0 :   fd_snapct_tile_t * ctx         = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t),  sizeof(fd_snapct_tile_t) );
    1910           0 :   void *             _ssping     = FD_SCRATCH_ALLOC_APPEND( l, fd_ssping_align(),          fd_ssping_footprint( TOTAL_PEERS_MAX ) );
    1911           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t)*GOSSIP_PEERS_MAX );
    1912           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, gossip_ci_map_align(),      gossip_ci_map_footprint( gossip_ci_map_chain_cnt_est( GOSSIP_PEERS_MAX ) ) );
    1913           0 :   void *             _ssresolver = FD_SCRATCH_ALLOC_APPEND( l, fd_http_resolver_align(),   fd_http_resolver_footprint( SERVER_PEERS_MAX )  );
    1914           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX ) );
    1915           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, blacklist_pool_align(),     blacklist_pool_footprint( TOTAL_PEERS_MAX ) );
    1916           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, blacklist_map_align(),      blacklist_map_footprint( blacklist_map_chain_cnt_est( TOTAL_PEERS_MAX ) ) );
    1917           0 :                                    FD_SCRATCH_ALLOC_APPEND( l, fd_adns_align(),            fd_adns_footprint( ADNS_REQS_MAX ) );
    1918             : 
    1919           0 : #if FD_HAS_OPENSSL
    1920           0 :   void * _alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
    1921           0 :   fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), tile->kind_id );
    1922           0 :   fd_ossl_tile_init( alloc );
    1923           0 : #endif
    1924             : 
    1925           0 :   ctx->ssping = NULL;
    1926           0 :   if( FD_LIKELY( download_enabled( tile ) ) )         ctx->ssping = fd_ssping_join( fd_ssping_new( _ssping, TOTAL_PEERS_MAX, 1UL, on_ping, ctx ) );
    1927           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 ) );
    1928           0 :   else                                                ctx->ssresolver = NULL;
    1929             : 
    1930           0 :   ctx->netdb_fds->etc_hosts       = -1;
    1931           0 :   ctx->netdb_fds->etc_resolv_conf = -1;
    1932           0 :   if( FD_LIKELY( download_enabled( tile ) ) ) FD_TEST( fd_netdb_open_fds( ctx->netdb_fds ) );
    1933           0 :   ctx->resolved_entrypoints_cnt = 0UL;
    1934           0 :   ctx->resolved_servers_cnt     = 0UL;
    1935             : 
    1936           0 :   fd_snap_pool_layout_t layout = fd_snap_pool_layout(
    1937           0 :       tile->snapct.max_full_snapshots_to_keep,
    1938           0 :       tile->snapct.max_incremental_snapshots_to_keep,
    1939           0 :       tile->snapct.incremental_snapshots,
    1940           0 :       download_enabled( tile ) );
    1941           0 :   uint snap_full_max     = (uint)layout.full_max;
    1942           0 :   uint snap_incr_max     = (uint)layout.incr_max;
    1943           0 :   uint retained_snap_max = (uint)layout.retained_max;
    1944           0 :   uint scratch_full_cnt  = (uint)layout.scratch_full;
    1945           0 :   uint scratch_incr_cnt  = (uint)layout.scratch_incr;
    1946           0 :   uint snap_max          = (uint)layout.max;
    1947             : 
    1948           0 :   ulong full_slot = ULONG_MAX;
    1949           0 :   ulong incremental_slot = ULONG_MAX;
    1950           0 :   int full_is_zstd = 0;
    1951           0 :   int incremental_is_zstd = 0;
    1952           0 :   char full_path[ PATH_MAX ] = {0};
    1953           0 :   char incremental_path[ PATH_MAX ] = {0};
    1954           0 :   uchar full_snapshot_hash[ FD_HASH_FOOTPRINT ] = {0};
    1955           0 :   uchar incremental_snapshot_hash[ FD_HASH_FOOTPRINT ] = {0};
    1956           0 :   if( FD_UNLIKELY( -1==fd_ssarchive_latest_pair( tile->snapct.snapshots_path,
    1957           0 :                                                  tile->snapct.incremental_snapshots,
    1958           0 :                                                  &full_slot,
    1959           0 :                                                  &incremental_slot,
    1960           0 :                                                  full_path,
    1961           0 :                                                  incremental_path,
    1962           0 :                                                  &full_is_zstd,
    1963           0 :                                                  &incremental_is_zstd,
    1964           0 :                                                  full_snapshot_hash,
    1965           0 :                                                  incremental_snapshot_hash ) ) ) {
    1966           0 :     if( FD_UNLIKELY( !download_enabled( tile ) ) ) {
    1967           0 :       FD_LOG_ERR(( "No snapshots found in `%s` and no download sources are enabled. "
    1968           0 :                    "Please enable downloading via [snapshots.sources] and restart.", tile->snapct.snapshots_path ));
    1969           0 :     }
    1970           0 :     ctx->local_in.full_snapshot_slot        = ULONG_MAX;
    1971           0 :     ctx->local_in.incremental_snapshot_slot = ULONG_MAX;
    1972           0 :     ctx->local_in.full_snapshot_size        = 0UL;
    1973           0 :     ctx->local_in.incremental_snapshot_size = 0UL;
    1974           0 :     ctx->local_in.full_snapshot_zstd        = 0;
    1975           0 :     ctx->local_in.incremental_snapshot_zstd = 0;
    1976           0 :     fd_cstr_fini( ctx->local_in.full_snapshot_path );
    1977           0 :     fd_cstr_fini( ctx->local_in.incremental_snapshot_path );
    1978           0 :     fd_memset( ctx->local_in.full_snapshot_hash, 0, FD_HASH_FOOTPRINT );
    1979           0 :     fd_memset( ctx->local_in.incremental_snapshot_hash, 0, FD_HASH_FOOTPRINT );
    1980           0 :   } else {
    1981           0 :     FD_TEST( full_slot!=ULONG_MAX );
    1982             : 
    1983           0 :     ctx->local_in.full_snapshot_slot        = full_slot;
    1984           0 :     ctx->local_in.incremental_snapshot_slot = incremental_slot;
    1985           0 :     ctx->local_in.full_snapshot_zstd        = full_is_zstd;
    1986           0 :     ctx->local_in.incremental_snapshot_zstd = incremental_is_zstd;
    1987             : 
    1988           0 :     fd_cstr_ncpy( ctx->local_in.full_snapshot_path, full_path, PATH_MAX );
    1989           0 :     fd_memcpy( ctx->local_in.full_snapshot_hash, full_snapshot_hash, FD_HASH_FOOTPRINT );
    1990           0 :     struct stat full_stat;
    1991           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 ) ));
    1992           0 :     if( FD_UNLIKELY( !S_ISREG( full_stat.st_mode ) ) ) FD_LOG_ERR(( "full snapshot path `%s` is not a regular file", full_path ));
    1993           0 :     ctx->local_in.full_snapshot_size = (ulong)full_stat.st_size;
    1994             : 
    1995           0 :     if( FD_LIKELY( incremental_slot!=ULONG_MAX ) ) {
    1996           0 :       fd_cstr_ncpy( ctx->local_in.incremental_snapshot_path, incremental_path, PATH_MAX );
    1997           0 :       fd_memcpy( ctx->local_in.incremental_snapshot_hash, incremental_snapshot_hash, FD_HASH_FOOTPRINT );
    1998           0 :       struct stat incremental_stat;
    1999           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 ) ));
    2000           0 :       if( FD_UNLIKELY( !S_ISREG( incremental_stat.st_mode ) ) ) FD_LOG_ERR(( "incremental snapshot path `%s` is not a regular file", incremental_path ));
    2001           0 :       ctx->local_in.incremental_snapshot_size = (ulong)incremental_stat.st_size;
    2002           0 :     } else {
    2003           0 :       ctx->local_in.incremental_snapshot_size = 0UL;
    2004           0 :       fd_cstr_fini( ctx->local_in.incremental_snapshot_path );
    2005           0 :     }
    2006           0 :   }
    2007             : 
    2008           0 :   ctx->local_out.dir_fd                  = -1;
    2009           0 :   ctx->local_out.full_snapshot_fd        = -1;
    2010           0 :   ctx->local_out.incremental_snapshot_fd = -1;
    2011           0 :   fd_cstr_fini( ctx->local_out.full_snapshot_name );
    2012           0 :   fd_cstr_fini( ctx->local_out.incremental_snapshot_name );
    2013           0 :   if( FD_LIKELY( download_enabled( tile ) ) ) {
    2014           0 :     ctx->local_out.dir_fd = open( tile->snapct.snapshots_path, O_RDONLY|O_DIRECTORY|O_CLOEXEC );
    2015           0 :     if( FD_UNLIKELY( -1==ctx->local_out.dir_fd ) )
    2016           0 :       FD_LOG_ERR(( "open(%s) failed (%i-%s)", tile->snapct.snapshots_path, errno, fd_io_strerror( errno ) ));
    2017             : 
    2018           0 :     FD_TEST( snap_max && snap_max<=FD_SNAP_MAX );
    2019           0 :     fd_backup_inode_t pool[ FD_SNAP_MAX ] = {0};
    2020           0 :     if( FD_UNLIKELY( snap_max<FD_SNAP_MAX && -1!=fcntl( FD_SNAP_FD( snap_max ), F_GETFD ) ) ) {
    2021           0 :       FD_LOG_ERR(( "inherited snapshot pool is larger than the expected %u slots", snap_max ));
    2022           0 :     }
    2023           0 :     fd_snap_pool_recover( ctx->local_out.dir_fd, tile->snapct.snapshots_path, pool, snap_max );
    2024           0 :     if( FD_LIKELY( snap_full_max ) ) {
    2025           0 :       uint full_idx = snapshot_pool_select( pool, 0U, snap_full_max );
    2026           0 :       FD_TEST( full_idx!=UINT_MAX );
    2027           0 :       ctx->local_out.full_snapshot_fd = FD_SNAP_FD( full_idx );
    2028           0 :       fd_cstr_ncpy( ctx->local_out.full_snapshot_name, pool[ full_idx ].name, FD_SNAP_NAME_MAX );
    2029           0 :     } else {
    2030           0 :       uint full_idx = retained_snap_max; /* scratch full slot */
    2031           0 :       FD_TEST( scratch_full_cnt && full_idx<snap_max );
    2032           0 :       ctx->local_out.full_snapshot_fd = FD_SNAP_FD( full_idx );
    2033           0 :       fd_cstr_ncpy( ctx->local_out.full_snapshot_name, pool[ full_idx ].name, FD_SNAP_NAME_MAX );
    2034           0 :     }
    2035             : 
    2036           0 :     if( FD_LIKELY( tile->snapct.incremental_snapshots && snap_incr_max ) ) {
    2037           0 :       uint incr_idx = snapshot_pool_select( pool, snap_full_max, retained_snap_max );
    2038           0 :       FD_TEST( incr_idx!=UINT_MAX );
    2039           0 :       ctx->local_out.incremental_snapshot_fd = FD_SNAP_FD( incr_idx );
    2040           0 :       fd_cstr_ncpy( ctx->local_out.incremental_snapshot_name, pool[ incr_idx ].name, FD_SNAP_NAME_MAX );
    2041           0 :     } else if( FD_LIKELY( tile->snapct.incremental_snapshots ) ) {
    2042           0 :       uint incr_idx = retained_snap_max + scratch_full_cnt; /* scratch incremental slot */
    2043           0 :       FD_TEST( scratch_incr_cnt && incr_idx<snap_max );
    2044           0 :       ctx->local_out.incremental_snapshot_fd = FD_SNAP_FD( incr_idx );
    2045           0 :       fd_cstr_ncpy( ctx->local_out.incremental_snapshot_name, pool[ incr_idx ].name, FD_SNAP_NAME_MAX );
    2046           0 :     }
    2047             : 
    2048           0 :     FD_TEST( ctx->local_out.full_snapshot_fd!=ctx->local_out.incremental_snapshot_fd );
    2049           0 :   }
    2050             : 
    2051           0 :   if( FD_LIKELY( fd_sandbox_gettid()==fd_sandbox_getpid() ) ) {
    2052           0 :     for( uint i=0U; i<snap_max; i++ ) {
    2053           0 :       int fd = FD_SNAP_FD( i );
    2054           0 :       if( fd==ctx->local_out.full_snapshot_fd || fd==ctx->local_out.incremental_snapshot_fd ) continue;
    2055           0 :       if( FD_UNLIKELY( close( fd ) ) )
    2056           0 :         FD_LOG_ERR(( "close(snapshot pool fd %d) failed (%i-%s)", fd, errno, fd_io_strerror( errno ) ));
    2057           0 :     }
    2058           0 :   }
    2059             : 
    2060           0 :   FD_TEST( fd_rng_secure( &ctx->selector_seed, 8UL ) );
    2061           0 : }
    2062             : 
    2063             : static inline fd_snapct_out_link_t
    2064             : out1( fd_topo_t const *      topo,
    2065             :       fd_topo_tile_t const * tile,
    2066           0 :       char const *           name ) {
    2067           0 :   ulong idx = ULONG_MAX;
    2068             : 
    2069           0 :   for( ulong i=0UL; i<tile->out_cnt; i++ ) {
    2070           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
    2071           0 :     if( !strcmp( link->name, name ) ) {
    2072           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 ));
    2073           0 :       idx = i;
    2074           0 :     }
    2075           0 :   }
    2076             : 
    2077           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 };
    2078             : 
    2079           0 :   ulong mtu = topo->links[ tile->out_link_id[ idx ] ].mtu;
    2080           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 };
    2081             : 
    2082           0 :   void * mem   = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
    2083           0 :   ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
    2084           0 :   ulong wmark  = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, mtu );
    2085           0 :   return (fd_snapct_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0, .mtu = mtu };
    2086           0 : }
    2087             : 
    2088             : static void
    2089             : unprivileged_init( fd_topo_t const *      topo,
    2090           0 :                    fd_topo_tile_t const * tile ) {
    2091           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    2092             : 
    2093           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    2094           0 :   fd_snapct_tile_t * ctx  = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapct_tile_t),  sizeof(fd_snapct_tile_t)       );
    2095           0 :                             FD_SCRATCH_ALLOC_APPEND( l, fd_ssping_align(),          fd_ssping_footprint( TOTAL_PEERS_MAX ) );
    2096           0 :   void * _ci_table        = FD_SCRATCH_ALLOC_APPEND( l, alignof(gossip_ci_entry_t), sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX );
    2097           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 ) ) );
    2098           0 :                             FD_SCRATCH_ALLOC_APPEND( l, fd_http_resolver_align(),   fd_http_resolver_footprint( SERVER_PEERS_MAX ) );
    2099           0 :   void * _selector        = FD_SCRATCH_ALLOC_APPEND( l, fd_sspeer_selector_align(), fd_sspeer_selector_footprint( TOTAL_PEERS_MAX ) );
    2100           0 :   void * _bl_pool         = FD_SCRATCH_ALLOC_APPEND( l, blacklist_pool_align(),     blacklist_pool_footprint( TOTAL_PEERS_MAX ) );
    2101           0 :   void * _bl_map          = FD_SCRATCH_ALLOC_APPEND( l, blacklist_map_align(),      blacklist_map_footprint( blacklist_map_chain_cnt_est( TOTAL_PEERS_MAX ) ) );
    2102           0 :   void * _adns            = FD_SCRATCH_ALLOC_APPEND( l, fd_adns_align(),            fd_adns_footprint( ADNS_REQS_MAX ) );
    2103             : 
    2104           0 :   ctx->config = tile->snapct;
    2105           0 :   ctx->gossip_enabled   = gossip_enabled( tile );
    2106           0 :   ctx->download_enabled = download_enabled( tile );
    2107             : 
    2108           0 :   ctx->adns = NULL;
    2109           0 :   if( FD_LIKELY( ctx->download_enabled ) ) {
    2110           0 :     ctx->adns = fd_adns_join( fd_adns_new( _adns, ADNS_REQS_MAX ) );
    2111           0 :     FD_TEST( ctx->adns );
    2112           0 :     for( ulong i=0UL; i<ctx->config.sources.servers_cnt; i++ ) {
    2113           0 :       fd_dns_peer_parse( ctx->config.sources.servers[ i ], "snapshots.sources.servers", ctx->dns_servers[ i ].hostname, &ctx->dns_servers[ i ].port, &ctx->dns_servers[ i ].is_https );
    2114           0 :       ctx->dns_servers[ i ].resolved = 0;
    2115           0 :       FD_TEST( !fd_adns_resolve( ctx->adns, ctx->dns_servers[ i ].hostname, i ) );
    2116           0 :       ctx->dns_servers[ i ].retry_nanos = 0L; /* in flight */
    2117           0 :     }
    2118           0 :     for( ulong i=0UL; i<ctx->config.entrypoints_cnt; i++ ) {
    2119           0 :       fd_dns_peer_parse( ctx->config.entrypoints[ i ], "gossip.entrypoints", ctx->dns_entrypoints[ i ].hostname, &ctx->dns_entrypoints[ i ].port, NULL );
    2120           0 :       ctx->dns_entrypoints[ i ].resolved = 0;
    2121           0 :       FD_TEST( !fd_adns_resolve( ctx->adns, ctx->dns_entrypoints[ i ].hostname, DNS_REQ_ID_ENTRYPOINT|i ) );
    2122           0 :       ctx->dns_entrypoints[ i ].retry_nanos = 0L;
    2123           0 :     }
    2124           0 :   }
    2125             : 
    2126           0 :   ctx->selector       = fd_sspeer_selector_join( fd_sspeer_selector_new( _selector, TOTAL_PEERS_MAX, ctx->selector_seed ) );
    2127           0 :   ctx->blacklist_pool = blacklist_pool_join( blacklist_pool_new( _bl_pool, TOTAL_PEERS_MAX ) );
    2128           0 :   ctx->blacklist_map  = blacklist_map_join( blacklist_map_new( _bl_map, blacklist_map_chain_cnt_est( TOTAL_PEERS_MAX ), ctx->selector_seed ) );
    2129             : 
    2130           0 :   if( FD_UNLIKELY( !ctx->config.incremental_snapshots ) ) {
    2131           0 :     FD_LOG_WARNING(( "incremental snapshots disabled via [snapshots.incremental_snapshots]." ));
    2132           0 :   }
    2133             : 
    2134           0 :   ctx->state          = FD_SNAPCT_STATE_INIT;
    2135           0 :   ctx->malformed      = 0;
    2136           0 :   ctx->load_complete  = 0;
    2137           0 :   FD_CHECK_ERR( ctx->config.wait_for_peers_timeout_nanos>0L, "snapct wait_for_peers_timeout_nanos must be positive" );
    2138           0 :   ctx->deadline_nanos = fd_log_wallclock() + ctx->config.wait_for_peers_timeout_nanos;
    2139           0 :   ctx->flush_ack      = 0;
    2140           0 :   ctx->flush_ack_cnt  = 0;
    2141           0 :   ctx->peer.addr.l    = 0UL;
    2142             : 
    2143           0 :   fd_memset( ctx->http_full_snapshot_name, 0, PATH_MAX );
    2144           0 :   fd_memset( ctx->http_incr_snapshot_name, 0, PATH_MAX );
    2145             : 
    2146           0 :   ctx->gossip_in_mem = NULL;
    2147           0 :   int has_snapld_dc = 0, ack_cnt = 0;
    2148           0 :   FD_TEST( tile->in_cnt<=MAX_IN_LINKS );
    2149           0 :   for( ulong i=0UL; i<(tile->in_cnt); i++ ) {
    2150           0 :     fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ i ] ];
    2151           0 :     if( 0==strcmp( in_link->name, "gossip_out" ) ) {
    2152           0 :       ctx->in_kind[ i ]  = IN_KIND_GOSSIP;
    2153           0 :       ctx->gossip_in_mem = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
    2154           0 :     } else if( 0==strcmp( in_link->name, "snapld_dc" ) ) {
    2155           0 :       ctx->in_kind[ i ]  = IN_KIND_SNAPLD;
    2156           0 :       ctx->snapld_in_mem = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
    2157           0 :       FD_TEST( !has_snapld_dc );
    2158           0 :       has_snapld_dc = 1;
    2159           0 :     } else if( 0==strcmp( in_link->name, "snapin_ct" ) || 0==strcmp( in_link->name, "snapwr_ct" ) ){
    2160           0 :       ctx->in_kind[ i ] = IN_KIND_ACK;
    2161           0 :       ack_cnt++;
    2162           0 :     }
    2163           0 :   }
    2164           0 :   FD_TEST( has_snapld_dc && ack_cnt>0 );
    2165           0 :   ctx->flush_ack_cnt = ack_cnt + 1; /* +1 for snapld (acks via snapld_dc) */
    2166           0 :   FD_TEST( ctx->gossip_enabled==(ctx->gossip_in_mem!=NULL) );
    2167             : 
    2168           0 :   ctx->predicted_incremental.full_slot = FD_SSPEER_SLOT_UNKNOWN;
    2169           0 :   ctx->predicted_incremental.slot      = FD_SSPEER_SLOT_UNKNOWN;
    2170           0 :   ctx->predicted_incremental.pending   = 0;
    2171             : 
    2172           0 :   fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
    2173             : 
    2174           0 :   fd_memset( _ci_table, 0, sizeof(gossip_ci_entry_t) * GOSSIP_PEERS_MAX );
    2175           0 :   ctx->gossip.ci_table             = _ci_table;
    2176           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 ) );
    2177           0 :   ctx->gossip.allowed_cnt          = 0UL;
    2178           0 :   ctx->gossip.saturated            = !ctx->gossip_enabled;
    2179             : 
    2180           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 ));
    2181           0 :   ctx->out_ld  = out1( topo, tile, "snapct_ld"   );
    2182           0 :   ctx->out_gui = out1( topo, tile, "snapct_gui"  );
    2183           0 :   ctx->out_rp  = out1( topo, tile, "snapct_repr" );
    2184           0 : }
    2185             : 
    2186             : /* after_credit can result in as many as 5 stem publishes in some code
    2187             :    paths, and returnable_frag can result in 1. */
    2188           0 : #define STEM_BURST 6UL
    2189             : 
    2190           0 : #define STEM_LAZY  1000L
    2191             : 
    2192           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_snapct_tile_t
    2193           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapct_tile_t)
    2194             : 
    2195             : #define STEM_CALLBACK_SHOULD_SHUTDOWN     should_shutdown
    2196           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
    2197           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
    2198           0 : #define STEM_CALLBACK_RETURNABLE_FRAG     returnable_frag
    2199             : 
    2200             : #include "../../disco/stem/fd_stem.c"
    2201             : 
    2202             : #ifndef FD_TILE_TEST
    2203             : fd_topo_run_tile_t fd_tile_snapct = {
    2204             :   .name                     = NAME,
    2205             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
    2206             :   .populate_allowed_seccomp = populate_allowed_seccomp,
    2207             :   .populate_allowed_fds     = populate_allowed_fds,
    2208             :   .scratch_align            = scratch_align,
    2209             :   .scratch_footprint        = scratch_footprint,
    2210             :   .loose_footprint          = loose_footprint,
    2211             :   .privileged_init          = privileged_init,
    2212             :   .unprivileged_init        = unprivileged_init,
    2213             :   .run                      = stem_run,
    2214             :   .keep_host_networking     = 1,
    2215             :   .allow_connect            = 1,
    2216             :   .allow_renameat           = 1,
    2217             : };
    2218             : #endif
    2219             : 
    2220             : #undef NAME

Generated by: LCOV version 1.14