LCOV - code coverage report
Current view: top level - discof/restore - fd_snapct_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 1775 0.0 %
Date: 2026-10-22 18:10:43 Functions: 0 85 0.0 %

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

Generated by: LCOV version 1.14