LCOV - code coverage report
Current view: top level - discof/restore - fd_snapin_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 556 0.0 %
Date: 2026-01-23 05:02:40 Functions: 0 17 0.0 %

          Line data    Source code
       1             : #include "fd_snapin_tile_private.h"
       2             : #include "utils/fd_ssctrl.h"
       3             : #include "utils/fd_ssmsg.h"
       4             : #include "utils/fd_vinyl_io_wd.h"
       5             : 
       6             : #include "../../disco/topo/fd_topo.h"
       7             : #include "../../disco/metrics/fd_metrics.h"
       8             : #include "../../disco/gui/fd_gui_config_parse.h"
       9             : #include "../../flamenco/accdb/fd_accdb_admin_v1.h"
      10             : #include "../../flamenco/accdb/fd_accdb_impl_v1.h"
      11             : #include "../../flamenco/runtime/fd_txncache.h"
      12             : #include "../../flamenco/runtime/fd_system_ids.h"
      13             : #include "../../flamenco/runtime/sysvar/fd_sysvar_slot_history.h"
      14             : #include "../../flamenco/types/fd_types.h"
      15             : 
      16             : #include "generated/fd_snapin_tile_seccomp.h"
      17             : 
      18             : #define NAME "snapin"
      19             : 
      20             : /* The snapin tile is a state machine that parses and loads a full
      21             :    and optionally an incremental snapshot.  It is currently responsible
      22             :    for loading accounts into an in-memory database, though this may
      23             :    change. */
      24             : 
      25             : /* 300 root slots in the slot deltas array, and each one references all
      26             :    151 prior blockhashes that it's able to. */
      27             : #define FD_SNAPIN_MAX_SLOT_DELTA_GROUPS (300UL*151UL)
      28             : 
      29             : struct fd_blockhash_entry {
      30             :   fd_hash_t blockhash;
      31             : 
      32             :   struct {
      33             :     ulong prev;
      34             :     ulong next;
      35             :   } map;
      36             : };
      37             : 
      38             : typedef struct fd_blockhash_entry fd_blockhash_entry_t;
      39             : 
      40             : #define MAP_NAME                           blockhash_map
      41           0 : #define MAP_KEY                            blockhash
      42             : #define MAP_KEY_T                          fd_hash_t
      43             : #define MAP_ELE_T                          fd_blockhash_entry_t
      44           0 : #define MAP_KEY_EQ(k0,k1)                  (!memcmp((k0),(k1), sizeof(fd_hash_t)))
      45           0 : #define MAP_KEY_HASH(key,seed)             (fd_hash((seed),(key),sizeof(fd_hash_t)))
      46           0 : #define MAP_PREV                           map.prev
      47           0 : #define MAP_NEXT                           map.next
      48             : #define MAP_OPTIMIZE_RANDOM_ACCESS_REMOVAL 1
      49             : #include "../../util/tmpl/fd_map_chain.c"
      50             : 
      51             : static inline int
      52           0 : should_shutdown( fd_snapin_tile_t * ctx ) {
      53           0 :   if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_SHUTDOWN && !ctx->use_vinyl ) ) {
      54             :     /* This only needs to be logged under funk.  When vinyl is enabled,
      55             :        snapwm will log instead. */
      56           0 :     ulong accounts_dup = ctx->metrics.accounts_ignored + ctx->metrics.accounts_replaced;
      57           0 :     ulong accounts     = ctx->metrics.accounts_loaded  - accounts_dup;
      58           0 :     long  elapsed_ns   = fd_log_wallclock() - ctx->boot_timestamp;
      59           0 :     FD_LOG_NOTICE(( "loaded %.1fM accounts (%.1fM dups) from snapshot in %.3f seconds",
      60           0 :                     (double)accounts/1e6,
      61           0 :                     (double)accounts_dup/1e6,
      62           0 :                     (double)elapsed_ns/1e9 ));
      63           0 :   }
      64           0 :   return ctx->state==FD_SNAPSHOT_STATE_SHUTDOWN;
      65           0 : }
      66             : 
      67             : static ulong
      68           0 : scratch_align( void ) {
      69           0 :   return 512UL;
      70           0 : }
      71             : 
      72             : static ulong
      73           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      74           0 :   (void)tile;
      75           0 :   ulong l = FD_LAYOUT_INIT;
      76           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_snapin_tile_t),      sizeof(fd_snapin_tile_t)                             );
      77           0 :   l = FD_LAYOUT_APPEND( l, fd_ssparse_align(),             fd_ssparse_footprint( 1UL<<24UL )                    );
      78           0 :   l = FD_LAYOUT_APPEND( l, fd_txncache_align(),            fd_txncache_footprint( tile->snapin.max_live_slots ) );
      79           0 :   l = FD_LAYOUT_APPEND( l, fd_ssmanifest_parser_align(),   fd_ssmanifest_parser_footprint()                     );
      80           0 :   l = FD_LAYOUT_APPEND( l, fd_slot_delta_parser_align(),   fd_slot_delta_parser_footprint()                     );
      81           0 :   l = FD_LAYOUT_APPEND( l, alignof(blockhash_group_t),     sizeof(blockhash_group_t)*FD_SNAPIN_MAX_SLOT_DELTA_GROUPS );
      82           0 :   if( !tile->snapin.use_vinyl ) {
      83           0 :     l = FD_LAYOUT_APPEND( l, alignof(fd_sstxncache_entry_t), sizeof(fd_sstxncache_entry_t)*FD_SNAPIN_TXNCACHE_MAX_ENTRIES );
      84           0 :   }
      85           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      86           0 : }
      87             : 
      88             : static void
      89           0 : metrics_write( fd_snapin_tile_t * ctx ) {
      90           0 :   FD_MGAUGE_SET( SNAPIN, STATE,                  (ulong)ctx->state );
      91           0 :   FD_MGAUGE_SET( SNAPIN, FULL_BYTES_READ,        ctx->metrics.full_bytes_read );
      92           0 :   FD_MGAUGE_SET( SNAPIN, INCREMENTAL_BYTES_READ, ctx->metrics.incremental_bytes_read );
      93           0 :   FD_MGAUGE_SET( SNAPIN, ACCOUNTS_LOADED,        ctx->metrics.accounts_loaded );
      94           0 :   FD_MGAUGE_SET( SNAPIN, ACCOUNTS_REPLACED,      ctx->metrics.accounts_replaced );
      95           0 :   FD_MGAUGE_SET( SNAPIN, ACCOUNTS_IGNORED,       ctx->metrics.accounts_ignored );
      96           0 : }
      97             : 
      98             : /* verify_slot_deltas_with_slot_history verifies the 'SlotHistory'
      99             :    sysvar account after loading a snapshot.  The full database
     100             :    architecture is only instantiated after snapshot loading, so this
     101             :    function uses a primitive/cache-free mechanism to query the parts of
     102             :    the account database that are available.
     103             : 
     104             :    Returns 0 if verification passed, -1 if not. */
     105             : 
     106             : static int
     107           0 : verify_slot_deltas_with_slot_history( fd_snapin_tile_t * ctx ) {
     108             :   /* Do a raw read of the slot history sysvar account from the database.
     109             :      Requires approx 500kB stack space. */
     110             : 
     111           0 :   fd_account_meta_t meta;
     112           0 :   uchar data[ FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ ];
     113           0 :   union {
     114           0 :     uchar buf[ FD_SYSVAR_SLOT_HISTORY_FOOTPRINT ];
     115           0 :     fd_slot_history_global_t o;
     116           0 :   } decoded;
     117           0 :   FD_STATIC_ASSERT( offsetof( __typeof__(decoded), buf)==offsetof( __typeof__(decoded), o ), memory_layout );
     118           0 :   fd_snapin_read_account( ctx, &fd_sysvar_slot_history_id, &meta, data, sizeof(data) );
     119             : 
     120           0 :   if( FD_UNLIKELY( !meta.lamports || !meta.dlen ) ) {
     121           0 :     FD_LOG_WARNING(( "SlotHistory sysvar account missing or empty" ));
     122           0 :     return -1;
     123           0 :   }
     124           0 :   if( FD_UNLIKELY( meta.dlen > FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ ) ) {
     125           0 :     FD_LOG_WARNING(( "SlotHistory sysvar account data too large: %u bytes", meta.dlen ));
     126           0 :     return -1;
     127           0 :   }
     128           0 :   if( FD_UNLIKELY( !fd_memeq( meta.owner, fd_sysvar_owner_id.uc, sizeof(fd_pubkey_t) ) ) ) {
     129           0 :     FD_BASE58_ENCODE_32_BYTES( meta.owner, owner_b58 );
     130           0 :     FD_LOG_WARNING(( "SlotHistory sysvar owner is invalid: %s != sysvar_owner_id", owner_b58 ));
     131           0 :     return -1;
     132           0 :   }
     133             : 
     134           0 :   if( FD_UNLIKELY(
     135           0 :       !fd_bincode_decode_static_global(
     136           0 :           slot_history,
     137           0 :           &decoded.o,
     138           0 :           data,
     139           0 :           meta.dlen,
     140           0 :           NULL )
     141           0 :   ) ) {
     142           0 :     FD_LOG_WARNING(( "SlotHistory sysvar account data is corrupt" ));
     143           0 :     return -1;
     144           0 :   }
     145             : 
     146           0 :   for( ulong i=0UL; i<ctx->txncache_entries_len; i++ ) {
     147           0 :     fd_sstxncache_entry_t const * entry = &ctx->txncache_entries[i];
     148           0 :     if( FD_UNLIKELY( fd_sysvar_slot_history_find_slot( &decoded.o, entry->slot )!=FD_SLOT_HISTORY_SLOT_FOUND ) ) {
     149           0 :       FD_LOG_WARNING(( "slot %lu missing from SlotHistory sysvar account", entry->slot ));
     150           0 :       return -1;
     151           0 :     }
     152           0 :   }
     153           0 :   return 0;
     154           0 : }
     155             : 
     156             : static int
     157             : verify_slot_deltas_with_bank_slot( fd_snapin_tile_t * ctx,
     158           0 :                                    ulong              bank_slot ) {
     159           0 :   for( ulong i=0UL; i<ctx->txncache_entries_len; i++ ) {
     160           0 :     fd_sstxncache_entry_t const * entry = &ctx->txncache_entries[i];
     161           0 :     if( FD_UNLIKELY( entry->slot>bank_slot ) ) return -1;
     162           0 :   }
     163           0 :   return 0;
     164           0 : }
     165             : 
     166             : static void
     167             : transition_malformed( fd_snapin_tile_t *  ctx,
     168           0 :                       fd_stem_context_t * stem ) {
     169           0 :   if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) return;
     170           0 :   ctx->state = FD_SNAPSHOT_STATE_ERROR;
     171           0 :   fd_stem_publish( stem, ctx->out_ct_idx, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
     172           0 : }
     173             : 
     174             : static int
     175             : populate_txncache( fd_snapin_tile_t *                     ctx,
     176             :                    fd_snapshot_manifest_blockhash_t const blockhashes[ static 301UL ],
     177           0 :                    ulong                                  blockhashes_len ) {
     178             :   /* Our txncache internally contains the fork structure for the chain,
     179             :      which we need to recreate here.  Because snapshots are only served
     180             :      for rooted slots, there is actually no forking, and the bank forks
     181             :      are just a single bank, the root, like
     182             : 
     183             :        _root
     184             : 
     185             :      But the txncache also must contain the 150 more recent banks prior
     186             :      to the root (151 rooted banks total), looking like,
     187             : 
     188             : 
     189             :        _root_150 -> _root_149 -> ... -> _root_2 -> _root_1 -> _root
     190             : 
     191             :      Our txncache is "slot agnostic" meaning there is no concept of a
     192             :      slot number in it.  It just has a fork tree structure.  So long as
     193             :      the fork tree is isomorphic to the actual bank forks, and each bank
     194             :      has the correct blockhash, it works.
     195             : 
     196             :      So the challenge is simply to create this chain of 151 forks in the
     197             :      txncache, with correct blockhashes, and then insert all the
     198             :      transactions into it.
     199             : 
     200             :      Constructing the chain of blockhashes is easy.  It is just the
     201             :      BLOCKHASH_QUEUE array in the manifest.  This array is unfortuantely
     202             :      not sorted and appears in random order, but it has a hash_index
     203             :      field which is a gapless index, starting at some arbitrary offset,
     204             :      so we can back out the 151 blockhashes we need from this, by first
     205             :      finding the max hash_index as _max and then collecting hash entries
     206             :      via,
     207             : 
     208             :        _root_150 -> _root_149 -> ... -> _root_2 -> _root_1 -> _root
     209             :        _max-150  -> _max-149  -> ... -> _max-2  -> _max-1  -> _max
     210             : 
     211             :      Now the remaining problem is inserting transactions into this
     212             :      chain.  Remember each transaction needs to be inserted with:
     213             : 
     214             :       (a) The fork ID (position of the bank in the chain) it was executed in.
     215             :       (b) The blockhash of the bank it referenced.
     216             : 
     217             :     (b) is trivial to retrieve, as it's in the actual slot_deltas entry
     218             :     in the manifest served by Agave.  But (a) is mildly annoying.  Agave
     219             :     serves slot_deltas based on slot, so we need an additional mapping
     220             :     from slot to position in our banks chain.  It turns out we have to
     221             :     go to yet another structure in the manifest to retrieve this, the
     222             :     ancestors array.  This is just an array of slot values,  so we need
     223             :     to sort it, and line it up against our banks chain like so,
     224             : 
     225             :        _root_150  -> _root_149  -> ... -> _root_2  -> _root_1  -> _root
     226             :        _max-150   -> _max-149   -> ... -> _max-2   -> _max-1   -> _max
     227             :        _slots_150 -> _slots_149 -> ... -> _slots_2 -> _slots_1 -> _slots
     228             : 
     229             :     From there we are done.
     230             : 
     231             :     Well almost ... if you were paying attention you might have noticed
     232             :     this is a lot of work and we are lazy.  Why don't we just ignore the
     233             :     slot mapping and assume everything executed at the root slot
     234             :     exactly?  The only invariant we should maintain from a memory
     235             :     perspective is that at most, across all active banks,
     236             :     FD_MAX_TXN_PER_SLOT transactions are stored per slot, but we
     237             :     have preserved that.  It is not true "per slot" technically, but
     238             :     it's true across all slots, and the memory is aggregated.  It will
     239             :     also always be true, even as slots are garbage collected, because
     240             :     entries are collected by referece blockhash, not executed slot.
     241             : 
     242             :     ... actually we can't do this.  There's more broken things here.
     243             :     The Agave status decided to only store 20 bytes for 32 byte
     244             :     transaction hashes to save on memory.  That's OK, but they didn't
     245             :     just take the first 20 bytes.  They instead, for each blockhash,
     246             :     take a random offset between 0 and 12, and store bytes
     247             :     [ offset, offset+20 ) of the transaction hash.  We need to know this
     248             :     offset to be able to query the txncache later, so we need to
     249             :     retrieve it from the slot_deltas entry in the manifest, and key it
     250             :     into our txncache.  Unfortunately this offset is stored per slot in
     251             :     the slot_deltas entry.  So we need to first go and retrieve the
     252             :     ancestors array, sort it, and line it up against our banks chain as
     253             :     described above, and then go through slot deltas, to retrieve the
     254             :     offset for each slot, and stick it into the appropriate bank in
     255             :     our chain. */
     256             : 
     257           0 :   FD_TEST( blockhashes_len<=301UL );
     258           0 :   FD_TEST( blockhashes_len>0UL );
     259             : 
     260           0 :   ulong seq_min = ULONG_MAX;
     261           0 :   for( ulong i=0UL; i<blockhashes_len; i++ ) seq_min = fd_ulong_min( seq_min, blockhashes[ i ].hash_index );
     262             : 
     263           0 :   ulong seq_max;
     264           0 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( seq_min, blockhashes_len, &seq_max ) ) ) {
     265           0 :     FD_LOG_WARNING(( "corrupt snapshot: blockhash queue sequence number wraparound (seq_min=%lu age_cnt=%lu)", seq_min, blockhashes_len ));
     266           0 :     transition_malformed( ctx, ctx->stem );
     267           0 :     return 1;
     268           0 :   }
     269             : 
     270             :   /* First let's construct the chain array as described above.  But
     271             :      index 0 will be the root, index 1 the root's parent, etc. */
     272             : 
     273           0 :   struct {
     274           0 :     int exists;
     275           0 :     uchar blockhash[ 32UL ];
     276           0 :     fd_txncache_fork_id_t fork_id;
     277           0 :     ulong txnhash_offset;
     278           0 :   } banks[ 301UL ] = {0};
     279             : 
     280           0 :   for( ulong i=0UL; i<blockhashes_len; i++ ) {
     281           0 :     fd_snapshot_manifest_blockhash_t const * elem = &blockhashes[ i ];
     282           0 :     ulong idx;
     283           0 :     if( FD_UNLIKELY( __builtin_usubl_overflow( elem->hash_index, seq_min, &idx ) ) ) {
     284           0 :       FD_LOG_WARNING(( "corrupt snapshot: gap in blockhash queue (seq=[%lu,%lu) idx=%lu)", seq_min, seq_max, blockhashes[ i ].hash_index ));
     285           0 :       transition_malformed( ctx, ctx->stem );
     286           0 :       return 1;
     287           0 :     }
     288             : 
     289           0 :     if( FD_UNLIKELY( idx>=blockhashes_len ) ) {
     290           0 :       FD_LOG_WARNING(( "corrupt snapshot: blockhash queue index out of range (seq_min=%lu age_cnt=%lu idx=%lu)", seq_min, blockhashes_len, idx ));
     291           0 :       transition_malformed( ctx, ctx->stem );
     292           0 :       return 1;
     293           0 :     }
     294             : 
     295           0 :     if( FD_UNLIKELY( banks[ blockhashes_len-1UL-idx ].exists ) ) {
     296           0 :       FD_LOG_WARNING(( "corrupt snapshot: duplicate blockhash hash_index %lu", elem->hash_index ));
     297           0 :       transition_malformed( ctx, ctx->stem );
     298           0 :       return 1;
     299           0 :     }
     300             : 
     301           0 :     banks[ blockhashes_len-1UL-idx ].fork_id.val = USHORT_MAX;
     302           0 :     banks[ blockhashes_len-1UL-idx ].txnhash_offset = ULONG_MAX;
     303           0 :     memcpy( banks[ blockhashes_len-1UL-idx ].blockhash, elem->hash, 32UL );
     304           0 :     banks[ blockhashes_len-1UL-idx ].exists = 1;
     305           0 :   }
     306             : 
     307           0 :   ulong chain_len = fd_ulong_min( blockhashes_len, 151UL );
     308             : 
     309             :   /* Now we need a hashset of just the 151 most recent blockhashes,
     310             :      anything else is a nonce transaction which we do not insert, or an
     311             :      already expired transaction which can also be discarded. */
     312             : 
     313           0 :   uchar * _map = fd_alloca_check( alignof(blockhash_map_t), blockhash_map_footprint( 1024UL ) );
     314           0 :   blockhash_map_t * blockhash_map = blockhash_map_join( blockhash_map_new( _map, 1024UL, ctx->seed ) );
     315           0 :   FD_TEST( blockhash_map );
     316             : 
     317           0 :   fd_blockhash_entry_t blockhash_pool[ 151UL ];
     318           0 :   for( ulong i=0UL; i<chain_len; i++ ) {
     319           0 :     fd_memcpy( blockhash_pool[ i ].blockhash.uc, banks[ i ].blockhash, 32UL );
     320             : 
     321           0 :     if( FD_UNLIKELY( blockhash_map_ele_query_const( blockhash_map, &blockhash_pool[ i ].blockhash, NULL, blockhash_pool ) ) ) {
     322           0 :       FD_BASE58_ENCODE_32_BYTES( banks[ i ].blockhash, blockhash_b58 );
     323           0 :       FD_LOG_WARNING(( "corrupt snapshot: duplicate blockhash %s in 151 most recent blockhashes", blockhash_b58 ));
     324           0 :       transition_malformed( ctx, ctx->stem );
     325           0 :       return 1;
     326           0 :     }
     327             : 
     328           0 :     blockhash_map_ele_insert( blockhash_map, &blockhash_pool[ i ], blockhash_pool );
     329           0 :   }
     330             : 
     331             :   /* Now load the blockhash offsets for these blockhashes ... */
     332           0 :   FD_TEST( ctx->blockhash_offsets_len ); /* Must be at least one else nothing would be rooted */
     333           0 :   for( ulong i=0UL; i<ctx->blockhash_offsets_len; i++ ) {
     334           0 :     fd_hash_t key;
     335           0 :     fd_memcpy( key.uc, ctx->blockhash_offsets[ i ].blockhash, 32UL );
     336           0 :     fd_blockhash_entry_t * entry = blockhash_map_ele_query( blockhash_map, &key, NULL, blockhash_pool );
     337           0 :     if( FD_UNLIKELY( !entry ) ) continue; /* Not in the most recent 151 blockhashes */
     338             : 
     339           0 :     ulong chain_idx = (ulong)(entry - blockhash_pool);
     340             : 
     341           0 :     if( FD_UNLIKELY( banks[ chain_idx ].txnhash_offset!=ULONG_MAX && banks[ chain_idx ].txnhash_offset!=ctx->blockhash_offsets[ i ].txnhash_offset ) ) {
     342           0 :       FD_BASE58_ENCODE_32_BYTES( entry->blockhash.uc, blockhash_b58 );
     343           0 :       FD_LOG_WARNING(( "corrupt snapshot: conflicting txnhash offsets for blockhash %s", blockhash_b58 ));
     344           0 :       transition_malformed( ctx, ctx->stem );
     345           0 :       return 1;
     346           0 :     }
     347             : 
     348           0 :     banks[ chain_idx ].txnhash_offset = ctx->blockhash_offsets[ i ].txnhash_offset;
     349           0 :   }
     350             : 
     351             :   /* Construct the linear fork chain in the txncache. */
     352             : 
     353           0 :   fd_txncache_fork_id_t parent = { .val = USHORT_MAX };
     354           0 :   for( ulong i=0UL; i<chain_len; i++ ) banks[ chain_len-1UL-i ].fork_id = parent = fd_txncache_attach_child( ctx->txncache, parent );
     355           0 :   for( ulong i=0UL; i<chain_len; i++ ) fd_txncache_attach_blockhash( ctx->txncache, banks[ i ].fork_id, banks[ i ].blockhash );
     356             : 
     357             :   /* Now insert all transactions as if they executed at the current
     358             :      root, per above. */
     359             : 
     360           0 :   ulong insert_cnt = 0UL;
     361           0 :   for( ulong i=0UL; i<ctx->txncache_entries_len; i++ ) {
     362           0 :     fd_sstxncache_entry_t const * entry = &ctx->txncache_entries[ i ];
     363           0 :     fd_hash_t key;
     364           0 :     fd_memcpy( key.uc, entry->blockhash, 32UL );
     365           0 :     if( FD_UNLIKELY( !blockhash_map_ele_query_const( blockhash_map, &key, NULL, blockhash_pool ) ) ) continue;
     366             : 
     367           0 :     insert_cnt++;
     368           0 :     fd_txncache_insert( ctx->txncache, banks[ 0UL ].fork_id, entry->blockhash, entry->txnhash );
     369           0 :   }
     370             : 
     371           0 :   if( !!ctx->use_vinyl && !!ctx->txncache_entries_len_vinyl_ptr ) {
     372           0 :     memcpy( ctx->txncache_entries_len_vinyl_ptr, &ctx->txncache_entries_len, sizeof(ulong) );
     373           0 :   }
     374             : 
     375           0 :   FD_LOG_INFO(( "inserted %lu/%lu transactions into the txncache", insert_cnt, ctx->txncache_entries_len ));
     376             : 
     377             :   /* Then finalize all the banks (freezing them) and setting the txnhash
     378             :      offset so future queries use the correct offset.  If the offset is
     379             :      ULONG_MAX this is valid, it means the blockhash had no transactions
     380             :      in it, so there's nothing in the status cache under that blockhash.
     381             : 
     382             :      Just set the offset to 0 in this case, it doesn't matter, but
     383             :      should be valid between 0 and 12 inclusive. */
     384           0 :   for( ulong i=0UL; i<chain_len; i++ ) {
     385           0 :     ulong txnhash_offset = banks[ chain_len-1UL-i ].txnhash_offset==ULONG_MAX ? 0UL : banks[ chain_len-1UL-i ].txnhash_offset;
     386           0 :     fd_txncache_finalize_fork( ctx->txncache, banks[ chain_len-1UL-i ].fork_id, txnhash_offset, banks[ chain_len-1UL-i ].blockhash );
     387           0 :   }
     388             : 
     389           0 :   for( ulong i=1UL; i<chain_len; i++ ) fd_txncache_advance_root( ctx->txncache, banks[ chain_len-1UL-i ].fork_id );
     390             : 
     391           0 :   ctx->txncache_root_fork_id = parent;
     392             : 
     393           0 :   return 0;
     394           0 : }
     395             : 
     396             : static void
     397           0 : process_manifest( fd_snapin_tile_t * ctx ) {
     398           0 :   fd_snapshot_manifest_t * manifest = fd_chunk_to_laddr( ctx->manifest_out.mem, ctx->manifest_out.chunk );
     399             : 
     400           0 :   ctx->bank_slot = manifest->slot;
     401           0 :   if( FD_UNLIKELY( verify_slot_deltas_with_bank_slot( ctx, manifest->slot ) ) ) {
     402           0 :     FD_LOG_WARNING(( "slot deltas verification failed" ));
     403           0 :     transition_malformed( ctx, ctx->stem );
     404           0 :     return;
     405           0 :   }
     406             : 
     407           0 :   if( FD_UNLIKELY( populate_txncache( ctx, manifest->blockhashes, manifest->blockhashes_len ) ) ) {
     408           0 :     FD_LOG_WARNING(( "populating txncache failed" ));
     409           0 :     transition_malformed( ctx, ctx->stem );
     410           0 :     return;
     411           0 :   }
     412             : 
     413           0 :   if( manifest->has_accounts_lthash ) {
     414           0 :     uchar const * sum = manifest->accounts_lthash;
     415           0 :     uchar hash32[32]; fd_blake3_hash( sum, FD_LTHASH_LEN_BYTES, hash32 );
     416           0 :     FD_BASE58_ENCODE_32_BYTES( sum,    sum_enc    );
     417           0 :     FD_BASE58_ENCODE_32_BYTES( hash32, hash32_enc );
     418           0 :     FD_LOG_INFO(( "snapshot manifest slot=%lu indicates lthash[..32]=%s blake3(lthash)=%s",
     419           0 :                   manifest->slot, sum_enc, hash32_enc ));
     420           0 :   }
     421             : 
     422           0 :   manifest->txncache_fork_id = ctx->txncache_root_fork_id.val;
     423             : 
     424           0 :   if( FD_LIKELY( !ctx->lthash_disabled ) ) {
     425           0 :     if( FD_UNLIKELY( !manifest->has_accounts_lthash ) ) {
     426           0 :       FD_LOG_WARNING(( "snapshot manifest missing accounts lthash field" ));
     427           0 :       transition_malformed( ctx, ctx->stem );
     428           0 :       return;
     429           0 :     }
     430             : 
     431           0 :     fd_lthash_value_t * expected_lthash = fd_chunk_to_laddr( ctx->hash_out.mem, ctx->hash_out.chunk );
     432           0 :     fd_memcpy( expected_lthash, manifest->accounts_lthash, sizeof(fd_lthash_value_t) );
     433           0 :     fd_stem_publish( ctx->stem, ctx->out_ct_idx, FD_SNAPSHOT_HASH_MSG_EXPECTED, ctx->hash_out.chunk, sizeof(fd_lthash_value_t), 0UL, 0UL, 0UL );
     434           0 :     ctx->hash_out.chunk = fd_dcache_compact_next( ctx->hash_out.chunk, sizeof(fd_lthash_value_t), ctx->hash_out.chunk0, ctx->hash_out.wmark );
     435           0 :   }
     436             : 
     437           0 :   ulong sig = ctx->full ? fd_ssmsg_sig( FD_SSMSG_MANIFEST_FULL ) :
     438           0 :                           fd_ssmsg_sig( FD_SSMSG_MANIFEST_INCREMENTAL );
     439           0 :   fd_stem_publish( ctx->stem, ctx->manifest_out.idx, sig, ctx->manifest_out.chunk, sizeof(fd_snapshot_manifest_t), 0UL, 0UL, 0UL );
     440           0 :   ctx->manifest_out.chunk = fd_dcache_compact_next( ctx->manifest_out.chunk, sizeof(fd_snapshot_manifest_t), ctx->manifest_out.chunk0, ctx->manifest_out.wmark );
     441           0 : }
     442             : 
     443             : 
     444             : static int
     445             : handle_data_frag( fd_snapin_tile_t *  ctx,
     446             :                   ulong               chunk,
     447             :                   ulong               sz,
     448           0 :                   fd_stem_context_t * stem ) {
     449           0 :   if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_FINISHING ) ) {
     450           0 :     transition_malformed( ctx, stem );
     451           0 :     return 0;
     452           0 :   }
     453           0 :   else if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) {
     454             :     /* Ignore all data frags after observing an error in the stream until
     455             :        we receive fail & init control messages to restart processing. */
     456           0 :     return 0;
     457           0 :   }
     458           0 :   else if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_PROCESSING ) ) {
     459           0 :     FD_LOG_ERR(( "invalid state for data frag %d", ctx->state ));
     460           0 :   }
     461             : 
     462           0 :   FD_TEST( chunk>=ctx->in.chunk0 && chunk<=ctx->in.wmark && sz<=ctx->in.mtu );
     463             : 
     464           0 :   if( FD_UNLIKELY( !ctx->lthash_disabled && ctx->buffered_batch.batch_cnt>0UL ) ) {
     465           0 :     fd_snapin_process_account_batch( ctx, NULL, &ctx->buffered_batch );
     466           0 :     return 1;
     467           0 :   }
     468             : 
     469           0 :   for(;;) {
     470           0 :     if( FD_UNLIKELY( sz-ctx->in.pos==0UL ) ) break;
     471             : 
     472           0 :     uchar const * data = (uchar const *)fd_chunk_to_laddr_const( ctx->in.wksp, chunk ) + ctx->in.pos;
     473             : 
     474           0 :     int early_exit = 0;
     475           0 :     fd_ssparse_advance_result_t result[1];
     476           0 :     int res = fd_ssparse_advance( ctx->ssparse, data, sz-ctx->in.pos, result );
     477           0 :     switch( res ) {
     478           0 :       case FD_SSPARSE_ADVANCE_ERROR:
     479           0 :         transition_malformed( ctx, stem );
     480           0 :         return 0;
     481           0 :       case FD_SSPARSE_ADVANCE_AGAIN:
     482           0 :         break;
     483           0 :       case FD_SSPARSE_ADVANCE_MANIFEST: {
     484           0 :         int res = fd_ssmanifest_parser_consume( ctx->manifest_parser,
     485           0 :                                                 result->manifest.data,
     486           0 :                                                 result->manifest.data_sz,
     487           0 :                                                 result->manifest.acc_vec_map,
     488           0 :                                                 result->manifest.acc_vec_pool );
     489           0 :         if( FD_UNLIKELY( res==FD_SSMANIFEST_PARSER_ADVANCE_ERROR ) ) {
     490           0 :           transition_malformed( ctx, stem );
     491           0 :           return 0;
     492           0 :         } else if( FD_LIKELY( res==FD_SSMANIFEST_PARSER_ADVANCE_DONE ) ) {
     493           0 :           ctx->flags.manifest_done = 1;
     494           0 :         }
     495           0 :         break;
     496           0 :       }
     497           0 :       case FD_SSPARSE_ADVANCE_STATUS_CACHE: {
     498           0 :         fd_slot_delta_parser_advance_result_t sd_result[1];
     499           0 :         ulong bytes_remaining = result->status_cache.data_sz;
     500             : 
     501           0 :         while( bytes_remaining ) {
     502           0 :           int res = fd_slot_delta_parser_consume( ctx->slot_delta_parser,
     503           0 :                                                   result->status_cache.data,
     504           0 :                                                   bytes_remaining,
     505           0 :                                                   sd_result );
     506           0 :           if( FD_UNLIKELY( res<0 ) ) {
     507           0 :             transition_malformed( ctx, stem );
     508           0 :             return 0;
     509           0 :           } else if( FD_LIKELY( res==FD_SLOT_DELTA_PARSER_ADVANCE_GROUP ) ) {
     510           0 :             if( FD_UNLIKELY( ctx->blockhash_offsets_len>=FD_SNAPIN_MAX_SLOT_DELTA_GROUPS ) ) FD_LOG_ERR(( "blockhash offsets overflow, max is %lu", FD_SNAPIN_MAX_SLOT_DELTA_GROUPS ));
     511             : 
     512           0 :             memcpy( ctx->blockhash_offsets[ ctx->blockhash_offsets_len ].blockhash, sd_result->group.blockhash, 32UL );
     513           0 :             ctx->blockhash_offsets[ ctx->blockhash_offsets_len ].txnhash_offset = sd_result->group.txnhash_offset;
     514           0 :             ctx->blockhash_offsets_len++;
     515           0 :           } else if( FD_LIKELY( res==FD_SLOT_DELTA_PARSER_ADVANCE_ENTRY ) ) {
     516           0 :             if( FD_UNLIKELY( ctx->txncache_entries_len>=FD_SNAPIN_TXNCACHE_MAX_ENTRIES ) ) FD_LOG_ERR(( "txncache entries overflow, max is %lu", FD_SNAPIN_TXNCACHE_MAX_ENTRIES ));
     517           0 :             ctx->txncache_entries[ ctx->txncache_entries_len++ ] = *sd_result->entry;
     518           0 :           }
     519             : 
     520           0 :           bytes_remaining           -= sd_result->bytes_consumed;
     521           0 :           result->status_cache.data += sd_result->bytes_consumed;
     522           0 :         }
     523             : 
     524           0 :         ctx->flags.status_cache_done = fd_slot_delta_parser_consume( ctx->slot_delta_parser, result->status_cache.data, 0UL, sd_result )==FD_SLOT_DELTA_PARSER_ADVANCE_DONE;
     525           0 :         break;
     526           0 :       }
     527           0 :       case FD_SSPARSE_ADVANCE_ACCOUNT_HEADER:
     528           0 :         early_exit = fd_snapin_process_account_header( ctx, result );
     529           0 :         break;
     530           0 :       case FD_SSPARSE_ADVANCE_ACCOUNT_DATA:
     531           0 :         early_exit = fd_snapin_process_account_data( ctx, result );
     532             : 
     533             :         /* We exepect ConfigKeys Vec to be length 2.  We expect the size
     534             :            of ConfigProgram-owned accounts to be
     535             :            FD_GUI_CONFIG_PARSE_MAX_VALID_ACCT_SZ, since this the size
     536             :            that the solana CLI allocates for them.  Although the Config
     537             :            program itself does not enforce this limit, the vast majority
     538             :            of accounts (with a tiny number of excpetions on devnet) are
     539             :            maintained with the solana cli. */
     540           0 :         if( FD_UNLIKELY( ctx->gui_out.idx!=ULONG_MAX && !memcmp( result->account_data.owner, fd_solana_config_program_id.key, sizeof(fd_hash_t) ) && result->account_data.data_sz && *(uchar *)result->account_data.data==2UL && result->account_data.data_sz<=FD_GUI_CONFIG_PARSE_MAX_VALID_ACCT_SZ ) ) {
     541           0 :           uchar * acct = fd_chunk_to_laddr( ctx->gui_out.mem, ctx->gui_out.chunk );
     542           0 :           fd_memcpy( acct, result->account_data.data, result->account_data.data_sz );
     543             : 
     544           0 :           fd_stem_publish( stem, ctx->gui_out.idx, 0UL, ctx->gui_out.chunk, result->account_data.data_sz, 0UL, 0UL, 0UL );
     545           0 :           ctx->gui_out.chunk = fd_dcache_compact_next( ctx->gui_out.chunk, result->account_data.data_sz, ctx->gui_out.chunk0, ctx->gui_out.wmark );
     546           0 :         }
     547           0 :         break;
     548           0 :       case FD_SSPARSE_ADVANCE_ACCOUNT_BATCH:
     549           0 :         early_exit = fd_snapin_process_account_batch( ctx, result, NULL );
     550           0 :         break;
     551           0 :       case FD_SSPARSE_ADVANCE_DONE:
     552           0 :         ctx->state = FD_SNAPSHOT_STATE_FINISHING;
     553           0 :         break;
     554           0 :       default:
     555           0 :         FD_LOG_ERR(( "unexpected fd_ssparse_advance result %d", res ));
     556           0 :         break;
     557           0 :     }
     558             : 
     559           0 :     if( FD_UNLIKELY( !ctx->flags.manifest_processed && ctx->flags.manifest_done && ctx->flags.status_cache_done ) ) {
     560           0 :       process_manifest( ctx );
     561           0 :       ctx->flags.manifest_processed = 1;
     562           0 :     }
     563             : 
     564           0 :     ctx->in.pos += result->bytes_consumed;
     565           0 :     if( FD_LIKELY( ctx->full ) ) ctx->metrics.full_bytes_read        += result->bytes_consumed;
     566           0 :     else                         ctx->metrics.incremental_bytes_read += result->bytes_consumed;
     567             : 
     568           0 :     if( FD_UNLIKELY( early_exit ) ) break;
     569           0 :   }
     570             : 
     571           0 :   int reprocess_frag = ctx->in.pos<sz;
     572           0 :   if( FD_LIKELY( !reprocess_frag ) ) ctx->in.pos = 0UL;
     573           0 :   return reprocess_frag;
     574           0 : }
     575             : 
     576             : static void
     577             : handle_control_frag( fd_snapin_tile_t *  ctx,
     578             :                      fd_stem_context_t * stem,
     579           0 :                      ulong               sig ) {
     580           0 :   switch( sig ) {
     581           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
     582           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_INCR:
     583           0 :       fd_ssparse_batch_enable( ctx->ssparse, ctx->use_vinyl || sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL );
     584           0 :       FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
     585           0 :       ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
     586           0 :       ctx->full = sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL;
     587           0 :       ctx->txncache_entries_len  = 0UL;
     588           0 :       ctx->blockhash_offsets_len = 0UL;
     589           0 :       fd_txncache_reset( ctx->txncache );
     590           0 :       fd_ssparse_reset( ctx->ssparse );
     591           0 :       fd_ssmanifest_parser_init( ctx->manifest_parser, fd_chunk_to_laddr( ctx->manifest_out.mem, ctx->manifest_out.chunk ) );
     592           0 :       fd_slot_delta_parser_init( ctx->slot_delta_parser );
     593           0 :       fd_memset( &ctx->flags,    0, sizeof(ctx->flags)    );
     594           0 :       fd_memset( &ctx->vinyl_op, 0, sizeof(ctx->vinyl_op) );
     595             : 
     596             :       /* Rewind metric counters (no-op unless recovering from a fail) */
     597           0 :       if( sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL ) {
     598           0 :         ctx->metrics.accounts_loaded   = ctx->metrics.full_accounts_loaded   = 0;
     599           0 :         ctx->metrics.accounts_replaced = ctx->metrics.full_accounts_replaced = 0;
     600           0 :         ctx->metrics.accounts_ignored  = ctx->metrics.full_accounts_ignored  = 0;
     601           0 :       } else {
     602           0 :         ctx->metrics.accounts_loaded   = ctx->metrics.full_accounts_loaded;
     603           0 :         ctx->metrics.accounts_replaced = ctx->metrics.full_accounts_replaced;
     604           0 :         ctx->metrics.accounts_ignored  = ctx->metrics.full_accounts_ignored;
     605             : 
     606           0 :         fd_funk_txn_xid_t incremental_xid = { .ul={ LONG_MAX, LONG_MAX } };
     607           0 :         fd_accdb_attach_child( ctx->accdb_admin, ctx->xid, &incremental_xid );
     608           0 :         fd_funk_txn_xid_copy( ctx->xid, &incremental_xid );
     609           0 :       }
     610           0 :       break;
     611             : 
     612           0 :     case FD_SNAPSHOT_MSG_CTRL_FAIL:
     613           0 :       if( ctx->state!=FD_SNAPSHOT_STATE_IDLE ) {
     614           0 :         ctx->state = FD_SNAPSHOT_STATE_IDLE;
     615             : 
     616           0 :         if( !ctx->use_vinyl && ctx->full ) {
     617           0 :           fd_accdb_v1_clear( ctx->accdb_admin );
     618           0 :         }
     619             : 
     620           0 :         if( !ctx->full ) {
     621           0 :           fd_accdb_cancel( ctx->accdb_admin, ctx->xid );
     622           0 :           fd_funk_txn_xid_copy( ctx->xid, fd_funk_last_publish( ctx->funk ) );
     623           0 :         }
     624           0 :       }
     625           0 :       break;
     626             : 
     627           0 :     case FD_SNAPSHOT_MSG_CTRL_NEXT: {
     628           0 :       FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING ||
     629           0 :                ctx->state==FD_SNAPSHOT_STATE_FINISHING  ||
     630           0 :                ctx->state==FD_SNAPSHOT_STATE_ERROR );
     631           0 :       if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_FINISHING ) ) {
     632           0 :         transition_malformed( ctx, stem );
     633           0 :         break;
     634           0 :       }
     635           0 :       ctx->state = FD_SNAPSHOT_STATE_IDLE;
     636             : 
     637             :       /* Backup metric counters */
     638           0 :       ctx->metrics.full_accounts_loaded   = ctx->metrics.accounts_loaded;
     639           0 :       ctx->metrics.full_accounts_replaced = ctx->metrics.accounts_replaced;
     640           0 :       ctx->metrics.full_accounts_ignored  = ctx->metrics.accounts_ignored;
     641           0 :       break;
     642           0 :     }
     643             : 
     644           0 :     case FD_SNAPSHOT_MSG_CTRL_DONE: {
     645           0 :       FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING ||
     646           0 :                ctx->state==FD_SNAPSHOT_STATE_FINISHING  ||
     647           0 :                ctx->state==FD_SNAPSHOT_STATE_ERROR );
     648           0 :       if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_FINISHING ) ) {
     649           0 :         transition_malformed( ctx, stem );
     650           0 :         break;
     651           0 :       }
     652           0 :       ctx->state = FD_SNAPSHOT_STATE_IDLE;
     653             : 
     654           0 :       if( !ctx->use_vinyl ) {
     655           0 :         if( FD_UNLIKELY( verify_slot_deltas_with_slot_history( ctx ) ) ) {
     656           0 :           FD_LOG_WARNING(( "slot deltas verification failed" ));
     657           0 :           transition_malformed( ctx, stem );
     658           0 :           break;
     659           0 :         }
     660           0 :       }
     661             : 
     662             :       /* Publish any remaining funk txn */
     663           0 :       if( FD_LIKELY( fd_funk_last_publish_is_frozen( ctx->funk ) ) ) {
     664           0 :         ctx->accdb_admin->base.gc_root_cnt = 0UL;
     665           0 :         ctx->accdb_admin->base.reclaim_cnt = 0UL;
     666           0 :         fd_accdb_advance_root( ctx->accdb_admin, ctx->xid );
     667           0 :         ctx->metrics.accounts_replaced += ctx->accdb_admin->base.gc_root_cnt;
     668             :         /* If an incremental snapshot 'reclaims' (deletes) an account,
     669             :            this removes both the full snapshot's original account, and
     670             :            the incremental snapshot's tombstone record.  Thus account
     671             :            for twice in metrics. */
     672           0 :         ctx->metrics.accounts_loaded   -= ctx->accdb_admin->base.reclaim_cnt;
     673           0 :         ctx->metrics.accounts_replaced += ctx->accdb_admin->base.reclaim_cnt;
     674           0 :       }
     675           0 :       FD_TEST( !fd_funk_last_publish_is_frozen( ctx->funk ) );
     676             : 
     677             :       /* Make 'Last published' XID equal the restored slot number */
     678           0 :       fd_funk_txn_xid_t target_xid = { .ul = { ctx->bank_slot, 0UL } };
     679           0 :       fd_accdb_attach_child( ctx->accdb_admin, ctx->xid, &target_xid );
     680           0 :       fd_accdb_advance_root( ctx->accdb_admin,           &target_xid );
     681           0 :       fd_funk_txn_xid_copy( ctx->xid, &target_xid );
     682             : 
     683           0 :       fd_stem_publish( stem, ctx->manifest_out.idx, fd_ssmsg_sig( FD_SSMSG_DONE ), 0UL, 0UL, 0UL, 0UL, 0UL );
     684           0 :       break;
     685           0 :     }
     686             : 
     687           0 :     case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:
     688           0 :       FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
     689           0 :       ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
     690           0 :       break;
     691             : 
     692           0 :     case FD_SNAPSHOT_MSG_CTRL_ERROR:
     693           0 :       ctx->state = FD_SNAPSHOT_STATE_ERROR;
     694           0 :       break;
     695             : 
     696           0 :     default:
     697           0 :       FD_LOG_ERR(( "unexpected control sig %lu", sig ));
     698           0 :       return;
     699           0 :   }
     700             : 
     701             :   /* Forward the control message down the pipeline */
     702           0 :   fd_stem_publish( stem, ctx->out_ct_idx, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
     703           0 : }
     704             : 
     705             : static inline int
     706             : returnable_frag( fd_snapin_tile_t *  ctx,
     707             :                  ulong               in_idx FD_PARAM_UNUSED,
     708             :                  ulong               seq    FD_PARAM_UNUSED,
     709             :                  ulong               sig,
     710             :                  ulong               chunk,
     711             :                  ulong               sz,
     712             :                  ulong               ctl    FD_PARAM_UNUSED,
     713             :                  ulong               tsorig FD_PARAM_UNUSED,
     714             :                  ulong               tspub  FD_PARAM_UNUSED,
     715           0 :                  fd_stem_context_t * stem ) {
     716           0 :   FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
     717             : 
     718           0 :   ctx->stem = stem;
     719           0 :   if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_DATA ) ) return handle_data_frag( ctx, chunk, sz, stem );
     720           0 :   else                                           handle_control_frag( ctx, stem, sig );
     721           0 :   ctx->stem = NULL;
     722             : 
     723           0 :   return 0;
     724           0 : }
     725             : 
     726             : static ulong
     727             : populate_allowed_fds( fd_topo_t      const * topo FD_PARAM_UNUSED,
     728             :                       fd_topo_tile_t const * tile FD_PARAM_UNUSED,
     729             :                       ulong                  out_fds_cnt,
     730           0 :                       int *                  out_fds ) {
     731           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     732             : 
     733           0 :   ulong out_cnt = 0;
     734           0 :   out_fds[ out_cnt++ ] = 2UL; /* stderr */
     735           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
     736           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     737           0 :   }
     738             : 
     739           0 :   return out_cnt;
     740           0 : }
     741             : 
     742             : static ulong
     743             : populate_allowed_seccomp( fd_topo_t const *      topo,
     744             :                           fd_topo_tile_t const * tile,
     745             :                           ulong                  out_cnt,
     746           0 :                           struct sock_filter *   out ) {
     747           0 :   (void)topo; (void)tile;
     748           0 :   populate_sock_filter_policy_fd_snapin_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     749           0 :   return sock_filter_policy_fd_snapin_tile_instr_cnt;
     750           0 : }
     751             : 
     752             : static void
     753             : privileged_init( fd_topo_t *      topo,
     754           0 :                  fd_topo_tile_t * tile ) {
     755           0 :   fd_snapin_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     756           0 :   memset( ctx, 0, sizeof(fd_snapin_tile_t) );
     757           0 :   FD_TEST( fd_rng_secure( &ctx->seed, 8UL ) );
     758             : 
     759           0 :   if( tile->snapin.use_vinyl ) {
     760           0 :     ctx->use_vinyl = 1;
     761           0 :   }
     762           0 : }
     763             : 
     764             : static inline fd_snapin_out_link_t
     765             : out1( fd_topo_t const *      topo,
     766             :       fd_topo_tile_t const * tile,
     767           0 :       char const *           name ) {
     768           0 :   ulong idx = fd_topo_find_tile_out_link( topo, tile, name, 0UL );
     769             : 
     770           0 :   if( FD_UNLIKELY( idx==ULONG_MAX ) ) return (fd_snapin_out_link_t){ .idx = ULONG_MAX, .mem = NULL, .chunk0 = 0, .wmark = 0, .chunk = 0, .mtu = 0 };
     771             : 
     772           0 :   ulong mtu = topo->links[ tile->out_link_id[ idx ] ].mtu;
     773           0 :   if( FD_UNLIKELY( mtu==0UL ) ) return (fd_snapin_out_link_t){ .idx = idx, .mem = NULL, .chunk0 = ULONG_MAX, .wmark = ULONG_MAX, .chunk = ULONG_MAX, .mtu = mtu };
     774             : 
     775           0 :   void * mem   = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
     776           0 :   ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
     777           0 :   ulong wmark  = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, mtu );
     778           0 :   return (fd_snapin_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0, .mtu = mtu };
     779           0 : }
     780             : 
     781             : FD_FN_UNUSED static void
     782             : unprivileged_init( fd_topo_t *      topo,
     783           0 :                    fd_topo_tile_t * tile ) {
     784           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     785             : 
     786           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     787           0 :   fd_snapin_tile_t * ctx  = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapin_tile_t),     sizeof(fd_snapin_tile_t)                             );
     788           0 :   void * _ssparse         = FD_SCRATCH_ALLOC_APPEND( l, fd_ssparse_align(),            fd_ssparse_footprint( 1UL<<24UL )                    );
     789           0 :   void * _txncache        = FD_SCRATCH_ALLOC_APPEND( l, fd_txncache_align(),           fd_txncache_footprint( tile->snapin.max_live_slots ) );
     790           0 :   void * _manifest_parser = FD_SCRATCH_ALLOC_APPEND( l, fd_ssmanifest_parser_align(),  fd_ssmanifest_parser_footprint()                              );
     791           0 :   void * _sd_parser       = FD_SCRATCH_ALLOC_APPEND( l, fd_slot_delta_parser_align(),  fd_slot_delta_parser_footprint()                              );
     792           0 :   ctx->blockhash_offsets  = FD_SCRATCH_ALLOC_APPEND( l, alignof(blockhash_group_t),     sizeof(blockhash_group_t)*FD_SNAPIN_MAX_SLOT_DELTA_GROUPS    );
     793             : 
     794           0 :   if( tile->snapin.use_vinyl ) {
     795             :     /* snapwm needs all txn_cache data in order to verify the slot
     796             :        deltas with the slot history.  To make this possible, snapin
     797             :        uses the dcache of the snapin_txn link as the scratch memory.
     798             :        The app field of the dcache is used to communicate the
     799             :        txncache_entries_len value. */
     800           0 :     fd_snapin_out_link_t snapin_txn = out1( topo, tile, "snapin_txn" );
     801           0 :     FD_TEST( !!snapin_txn.mem );
     802           0 :     fd_topo_link_t const * out_link_txn = &topo->links[ tile->out_link_id[ snapin_txn.idx ] ];
     803           0 :     ulong depth = out_link_txn->depth;
     804           0 :     FD_TEST( ( depth*snapin_txn.mtu )==( sizeof(fd_sstxncache_entry_t)*FD_SNAPIN_TXNCACHE_MAX_ENTRIES ) );
     805           0 :     ctx->txncache_entries                 = fd_chunk_to_laddr( snapin_txn.mem, snapin_txn.chunk0 );
     806           0 :     ctx->txncache_entries_len_vinyl_ptr   = (ulong*)fd_dcache_app_laddr( out_link_txn->dcache );
     807           0 :     memset( ctx->txncache_entries_len_vinyl_ptr, 0, sizeof(ulong) );
     808           0 :   } else {
     809           0 :     ctx->txncache_entries = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sstxncache_entry_t), sizeof(fd_sstxncache_entry_t)*FD_SNAPIN_TXNCACHE_MAX_ENTRIES );
     810           0 :     ctx->txncache_entries_len_vinyl_ptr = NULL;
     811           0 :   }
     812             : 
     813           0 :   ctx->full = 1;
     814           0 :   ctx->state = FD_SNAPSHOT_STATE_IDLE;
     815           0 :   ctx->lthash_disabled = tile->snapin.lthash_disabled;
     816             : 
     817           0 :   ctx->boot_timestamp = fd_log_wallclock();
     818             : 
     819           0 :   FD_TEST( fd_accdb_admin_v1_init( ctx->accdb_admin, fd_topo_obj_laddr( topo, tile->snapin.funk_obj_id ) ) );
     820           0 :   FD_TEST( fd_accdb_user_v1_init ( ctx->accdb,       fd_topo_obj_laddr( topo, tile->snapin.funk_obj_id ) ) );
     821           0 :   ctx->funk = fd_accdb_user_v1_funk( ctx->accdb );
     822           0 :   fd_funk_txn_xid_copy( ctx->xid, fd_funk_root( ctx->funk ) );
     823             : 
     824           0 :   void * _txncache_shmem = fd_topo_obj_laddr( topo, tile->snapin.txncache_obj_id );
     825           0 :   fd_txncache_shmem_t * txncache_shmem = fd_txncache_shmem_join( _txncache_shmem );
     826           0 :   FD_TEST( txncache_shmem );
     827           0 :   ctx->txncache = fd_txncache_join( fd_txncache_new( _txncache, txncache_shmem ) );
     828           0 :   FD_TEST( ctx->txncache );
     829             : 
     830           0 :   ctx->txncache_entries_len = 0UL;
     831           0 :   ctx->blockhash_offsets_len = 0UL;
     832             : 
     833           0 :   ctx->ssparse = fd_ssparse_new( _ssparse, 1UL<<24UL, ctx->seed );
     834           0 :   FD_TEST( ctx->ssparse );
     835             : 
     836           0 :   ctx->manifest_parser = fd_ssmanifest_parser_join( fd_ssmanifest_parser_new( _manifest_parser ) );
     837           0 :   FD_TEST( ctx->manifest_parser );
     838             : 
     839           0 :   ctx->slot_delta_parser = fd_slot_delta_parser_join( fd_slot_delta_parser_new( _sd_parser ) );
     840           0 :   FD_TEST( ctx->slot_delta_parser );
     841             : 
     842           0 :   fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
     843             : 
     844           0 :   if( FD_UNLIKELY( tile->kind_id ) ) FD_LOG_ERR(( "There can only be one `" NAME "` tile" ));
     845           0 :   if( FD_UNLIKELY( tile->in_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected 1", tile->in_cnt ));
     846             : 
     847           0 :   ctx->manifest_out = out1( topo, tile, "snapin_manif" );
     848           0 :   ctx->gui_out      = out1( topo, tile, "snapin_gui"   );
     849           0 :   ulong out_link_ct_idx = fd_topo_find_tile_out_link( topo, tile, "snapin_ct", 0UL );
     850           0 :   if( out_link_ct_idx==ULONG_MAX ) out_link_ct_idx = fd_topo_find_tile_out_link( topo, tile, "snapin_ls", 0UL );
     851           0 :   if( out_link_ct_idx==ULONG_MAX ) out_link_ct_idx = fd_topo_find_tile_out_link( topo, tile, "snapin_wm", 0UL );
     852           0 :   if( FD_UNLIKELY( out_link_ct_idx==ULONG_MAX ) ) FD_LOG_ERR(( "tile `" NAME "` missing required out link `snapin_ct` or `snapin_ls` or `snapin_wm`" ));
     853           0 :   fd_topo_link_t * snapin_out_link = &topo->links[ tile->out_link_id[ out_link_ct_idx ] ];
     854           0 :   ctx->out_ct_idx = out_link_ct_idx;
     855             : 
     856           0 :   if( FD_UNLIKELY( ctx->out_ct_idx==ULONG_MAX ) )       FD_LOG_ERR(( "tile `" NAME "` missing required out link `snapin_ct` or `snapin_ls` or `snapin_wm`" ));
     857           0 :   if( FD_UNLIKELY( ctx->manifest_out.idx==ULONG_MAX ) ) FD_LOG_ERR(( "tile `" NAME "` missing required out link `snapin_manif`" ));
     858             : 
     859           0 :   if( ( 0==strcmp( snapin_out_link->name, "snapin_ls" ) ) ||
     860           0 :       ( 0==strcmp( snapin_out_link->name, "snapin_wm" ) ) ) {
     861           0 :     ctx->hash_out = out1( topo, tile, snapin_out_link->name );
     862           0 :   }
     863             : 
     864           0 :   fd_ssparse_reset( ctx->ssparse );
     865           0 :   fd_ssmanifest_parser_init( ctx->manifest_parser, fd_chunk_to_laddr( ctx->manifest_out.mem, ctx->manifest_out.chunk ) );
     866           0 :   fd_slot_delta_parser_init( ctx->slot_delta_parser );
     867             : 
     868           0 :   fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0UL ] ];
     869           0 :   FD_TEST( 0==strcmp( in_link->name, "snapdc_in" ) );
     870           0 :   fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
     871           0 :   ctx->in.wksp                   = in_wksp->wksp;
     872           0 :   ctx->in.chunk0                 = fd_dcache_compact_chunk0( ctx->in.wksp, in_link->dcache );
     873           0 :   ctx->in.wmark                  = fd_dcache_compact_wmark( ctx->in.wksp, in_link->dcache, in_link->mtu );
     874           0 :   ctx->in.mtu                    = in_link->mtu;
     875           0 :   ctx->in.pos                    = 0UL;
     876             : 
     877           0 :   ctx->buffered_batch.batch_cnt     = 0UL;
     878           0 :   ctx->buffered_batch.remaining_idx = 0UL;
     879             : 
     880           0 :   fd_memset( &ctx->flags, 0, sizeof(ctx->flags) );
     881             : 
     882           0 :   if( tile->snapin.use_vinyl ) {
     883           0 :     ctx->use_vinyl = 1;
     884           0 :     fd_snapin_vinyl_unprivileged_init( ctx, topo, tile, NULL, NULL );
     885           0 :   }
     886           0 : }
     887             : 
     888             : /* Control fragments can result in one extra publish to forward the
     889             :    message down the pipeline, in addition to the result / malformed
     890             :    message. Can send one duplicate account message as well. */
     891           0 : #define STEM_BURST 3UL
     892             : 
     893           0 : #define STEM_LAZY  1000L
     894             : 
     895           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_snapin_tile_t
     896           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapin_tile_t)
     897             : 
     898             : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
     899           0 : #define STEM_CALLBACK_METRICS_WRITE   metrics_write
     900           0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
     901             : 
     902             : #include "../../disco/stem/fd_stem.c"
     903             : 
     904             : fd_topo_run_tile_t fd_tile_snapin = {
     905             :   .name                     = NAME,
     906             :   .populate_allowed_fds     = populate_allowed_fds,
     907             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     908             :   .scratch_align            = scratch_align,
     909             :   .scratch_footprint        = scratch_footprint,
     910             :   .privileged_init          = privileged_init,
     911             :   .unprivileged_init        = unprivileged_init,
     912             :   .run                      = stem_run,
     913             : };
     914             : 
     915             : #undef NAME

Generated by: LCOV version 1.14