LCOV - code coverage report
Current view: top level - discof/replay - fd_replay_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 1418 0.0 %
Date: 2025-11-29 04:46:19 Functions: 0 44 0.0 %

          Line data    Source code
       1             : #include "fd_replay_tile.h"
       2             : #include "fd_sched.h"
       3             : #include "fd_exec.h"
       4             : #include "fd_vote_tracker.h"
       5             : #include "generated/fd_replay_tile_seccomp.h"
       6             : 
       7             : #include "../genesis/fd_genesi_tile.h"
       8             : #include "../poh/fd_poh.h"
       9             : #include "../poh/fd_poh_tile.h"
      10             : #include "../tower/fd_tower_tile.h"
      11             : #include "../resolv/fd_resolv_tile.h"
      12             : #include "../restore/utils/fd_ssload.h"
      13             : 
      14             : #include "../../disco/tiles.h"
      15             : #include "../../disco/fd_txn_m.h"
      16             : #include "../../disco/store/fd_store.h"
      17             : #include "../../disco/pack/fd_pack.h"
      18             : #include "../../discof/reasm/fd_reasm.h"
      19             : #include "../../disco/keyguard/fd_keyload.h"
      20             : #include "../../disco/genesis/fd_genesis_cluster.h"
      21             : #include "../../util/pod/fd_pod.h"
      22             : #include "../../flamenco/accdb/fd_accdb_admin.h"
      23             : #include "../../flamenco/accdb/fd_accdb_impl_v1.h"
      24             : #include "../../flamenco/rewards/fd_rewards.h"
      25             : #include "../../flamenco/leaders/fd_multi_epoch_leaders.h"
      26             : #include "../../flamenco/progcache/fd_progcache_admin.h"
      27             : #include "../../disco/metrics/fd_metrics.h"
      28             : 
      29             : #include "../../flamenco/runtime/fd_runtime.h"
      30             : #include "../../flamenco/runtime/fd_runtime_stack.h"
      31             : #include "../../flamenco/fd_flamenco_base.h"
      32             : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
      33             : 
      34             : #include "../../flamenco/runtime/tests/fd_dump_pb.h"
      35             : 
      36             : #include <errno.h>
      37             : #include <stdio.h>
      38             : 
      39             : /* Replay concepts:
      40             : 
      41             :    - Blocks are aggregations of entries aka. microblocks which are
      42             :      groupings of txns and are constructed by the block producer (see
      43             :      fd_pack).
      44             : 
      45             :    - Entries are grouped into entry batches by the block producer (see
      46             :      fd_pack / fd_shredder).
      47             : 
      48             :    - Entry batches are divided into chunks known as shreds by the block
      49             :      producer (see fd_shredder).
      50             : 
      51             :    - Shreds are grouped into forward-error-correction sets (FEC sets) by
      52             :      the block producer (see fd_shredder).
      53             : 
      54             :    - Shreds are transmitted to the rest of the cluster via the Turbine
      55             :      protocol (see fd_shredder / fd_shred).
      56             : 
      57             :    - Once enough shreds within a FEC set are received to recover the
      58             :      entirety of the shred data encoded by that FEC set, the receiver
      59             :      can "complete" the FEC set (see fd_fec_resolver).
      60             : 
      61             :    - If shreds in the FEC set are missing such that it can't complete,
      62             :      the receiver can use the Repair protocol to request missing shreds
      63             :      in FEC set (see fd_repair).
      64             : 
      65             :   -  The current Repair protocol does not support requesting coding
      66             :      shreds.  As a result, some FEC sets might be actually complete
      67             :      (contain all data shreds).  Repair currently hacks around this by
      68             :      forcing completion but the long-term solution is to add support for
      69             :      fec_repairing coding shreds via Repair.
      70             : 
      71             :   - FEC sets are delivered in partial-order to the Replay tile by the
      72             :     Repair tile.  Currently Replay only supports replaying entry batches
      73             :     so FEC sets need to reassembled into an entry batch before they can
      74             :     be replayed.  The new Dispatcher will change this by taking a FEC
      75             :     set as input instead. */
      76             : 
      77           0 : #define IN_KIND_SNAP    ( 0)
      78           0 : #define IN_KIND_GENESIS ( 1)
      79           0 : #define IN_KIND_IPECHO  ( 2)
      80           0 : #define IN_KIND_TOWER   ( 3)
      81           0 : #define IN_KIND_RESOLV  ( 4)
      82           0 : #define IN_KIND_POH     ( 5)
      83           0 : #define IN_KIND_EXEC    ( 6)
      84           0 : #define IN_KIND_SHRED   ( 7)
      85           0 : #define IN_KIND_VTXN    ( 8)
      86           0 : #define IN_KIND_GUI     ( 9)
      87           0 : #define IN_KIND_RPC     (10)
      88             : 
      89             : #define DEBUG_LOGGING 0
      90             : 
      91             : /* The first bank that that the replay tile produces either for genesis
      92             :    or the snapshot boot will always be at bank index 0. */
      93           0 : #define FD_REPLAY_BOOT_BANK_IDX (0UL)
      94             : 
      95             : struct fd_replay_in_link {
      96             :   fd_wksp_t * mem;
      97             :   ulong       chunk0;
      98             :   ulong       wmark;
      99             :   ulong       mtu;
     100             : };
     101             : 
     102             : typedef struct fd_replay_in_link fd_replay_in_link_t;
     103             : 
     104             : struct fd_replay_out_link {
     105             :   ulong       idx;
     106             :   fd_wksp_t * mem;
     107             :   ulong       chunk0;
     108             :   ulong       wmark;
     109             :   ulong       chunk;
     110             : };
     111             : 
     112             : typedef struct fd_replay_out_link fd_replay_out_link_t;
     113             : 
     114             : /* fd_block_id_map is a simple map of block-ids to bank indices.  The
     115             :    map sits on top of an array of fd_block_id_ele_t.  This serves as a
     116             :    translation layer between block ids to bank indices. */
     117             : 
     118             : struct fd_block_id_ele {
     119             :   fd_hash_t block_id;
     120             :   ulong     slot; /* = FD_SLOT_NULL if not initialized */
     121             :   ulong     next_;
     122             : };
     123             : typedef struct fd_block_id_ele fd_block_id_ele_t;
     124             : 
     125             : #define MAP_NAME               fd_block_id_map
     126             : #define MAP_ELE_T              fd_block_id_ele_t
     127             : #define MAP_KEY_T              fd_hash_t
     128           0 : #define MAP_KEY                block_id
     129           0 : #define MAP_NEXT               next_
     130           0 : #define MAP_KEY_EQ(k0,k1)      (!memcmp((k0),(k1), sizeof(fd_hash_t)))
     131           0 : #define MAP_KEY_HASH(key,seed) (fd_hash((seed),(key),sizeof(fd_hash_t)))
     132             : #include "../../util/tmpl/fd_map_chain.c"
     133             : 
     134             : static inline ulong
     135           0 : fd_block_id_ele_get_idx( fd_block_id_ele_t * ele_arr, fd_block_id_ele_t * ele ) {
     136           0 :   return (ulong)(ele - ele_arr);
     137           0 : }
     138             : 
     139             : struct fd_replay_tile {
     140             :   fd_wksp_t * wksp;
     141             : 
     142             :   fd_accdb_admin_t     accdb_admin[1];
     143             :   fd_accdb_user_t      accdb[1];
     144             :   fd_progcache_admin_t progcache_admin[1];
     145             : 
     146             :   fd_txncache_t * txncache;
     147             :   fd_store_t *    store;
     148             :   fd_banks_t *    banks;
     149             : 
     150             :   /* This flag is 1 If we have seen a vote signature that our node has
     151             :      sent out get rooted at least one time.  The value is 0 otherwise.
     152             :      We can't become leader and pack blocks until this flag has been
     153             :      set.  This parallels the Agave 'has_new_vote_been_rooted'.
     154             : 
     155             :      TODO: Add a flag to the toml to make this optional. */
     156             :   int has_identity_vote_rooted;
     157             : 
     158             :   fd_reasm_t * reasm;
     159             : 
     160             :   /* Replay state machine. */
     161             :   fd_sched_t *         sched;
     162             :   ulong                exec_cnt;
     163             :   fd_replay_out_link_t exec_out[ 1 ]; /* Sending work down to exec tiles */
     164             : 
     165             :   fd_vote_tracker_t *  vote_tracker;
     166             : 
     167             :   int   has_genesis_hash;
     168             :   uchar genesis_hash[ 32UL ];
     169             :   ulong cluster_type;
     170             : 
     171             : #define FD_REPLAY_HARD_FORKS_MAX (64UL)
     172             :   ulong hard_forks_cnt;
     173             :   ulong hard_forks[ FD_REPLAY_HARD_FORKS_MAX ];
     174             : 
     175             :   ushort expected_shred_version;
     176             :   ushort ipecho_shred_version;
     177             : 
     178             :   /* A note on publishing ...
     179             : 
     180             :      The watermarks are used to publish our fork-aware structures.  For
     181             :      example, store, banks, and txncache need to be published to release
     182             :      resources occupied by rooted or dead blocks.  In general,
     183             :      publishing has the effect of pruning forks in those structures,
     184             :      indicating that it is ok to release the memory being occupied by
     185             :      the blocks on said forks.  Tower is responsible for informing us of
     186             :      the latest block on the consensus rooted fork.  As soon as we can,
     187             :      we should move the published root as close as possible to the
     188             :      latest consensus root, publishing/pruning everything on the fork
     189             :      tree along the way.  That is, all the blocks that directly descend
     190             :      from the current published root (inclusive) to the new published
     191             :      root (exclusive) on the rooted fork, as well as all the minority
     192             :      forks that branch from said blocks.
     193             : 
     194             :      Ideally, we'd move the published root to the consensus root
     195             :      immediately upon receiving a new consensus root.  However, that's
     196             :      not always safe to do.  One thing we need to be careful about is
     197             :      making sure that there are no more users/consumers of
     198             :      soon-to-be-pruned blocks, lest a use-after-free occurs.  This can
     199             :      be done by using a reference counter for each block.  Any
     200             :      concurrent activity, such as transaction execution in the exec
     201             :      tiles, should retain a refcnt on the block for as
     202             :      long as it needs access to the shared fork-aware structures related
     203             :      to that block.  Eventually, refcnt on a given block will drop down
     204             :      to 0 as the block either finishes replaying or gets marked as dead,
     205             :      and any other tile that has retained a refcnt on the block releases
     206             :      it.  At that point, it becomes a candidate for pruning.  The key to
     207             :      safe publishing then becomes figuring out how far we could advance
     208             :      the published root, such that every minority fork branching off of
     209             :      blocks in between the current published root (inclusive) and the
     210             :      new published root (exclusive) is safe to be pruned.  This is a
     211             :      straightforward tree traversal, where if a block B on the rooted
     212             :      fork has refcnt 0, and all minority forks branching off of B also
     213             :      have refcnt 0, then B is safe to be pruned.  We advance the
     214             :      published root to the farthest consecutively prunable block on the
     215             :      rooted fork.  Note that reasm presents the replay tile with a clean
     216             :      view of the world where every block is chained off of a parent
     217             :      block.  So there are no orpahned/dangling tree nodes to worry
     218             :      about.  The world is a nice single tree as far as replay is
     219             :      concerned.
     220             : 
     221             :      In the following fork tree, every node is a block and the number in
     222             :      parentheses is the refcnt on the block.  The chain marked with
     223             :      double slashes is the rooted fork.  Suppose the published root is
     224             :      at block P, and consensus root is at block T.  We can't publish
     225             :      past block P because Q has refcnt 1.
     226             : 
     227             : 
     228             :           P(0)
     229             :         /    \\
     230             :       Q(1)    A(0)
     231             :             / ||  \
     232             :         X(0) B(0)  C(0)
     233             :        /      || \
     234             :       Y(0)   M(0) R(0)
     235             :             / ||   /  \
     236             :         D(2) T(0) J(0) L(0)
     237             :               ||
     238             :               ..
     239             :               ..
     240             :               ..
     241             :               ||
     242             :       blocks we might be actively replaying
     243             : 
     244             : 
     245             :      When refcnt on Q drops to 0, we would be able to advance the
     246             :      published root to block M, because blocks P, A, and B, as well as
     247             :      all subtrees branching off of them, have refcnt 0, and therefore
     248             :      can be pruned.  Block M itself cannot be pruned yet because its
     249             :      child block D has refcnt 2.  After publishing/pruning, the fork
     250             :      tree would be:
     251             : 
     252             : 
     253             :              M(0)
     254             :             / ||
     255             :         D(2) T(0)
     256             :               ||
     257             :               ..
     258             :               ..
     259             :               ..
     260             :               ||
     261             :       blocks we might be actively replaying
     262             : 
     263             : 
     264             :      As a result, the shared fork-aware structures can free resources
     265             :      for blocks P, A, B, and all subtrees branching off of them.
     266             : 
     267             :      For the reference counting part, the replay tile is the sole entity
     268             :      that can update the refcnt.  This ensures that all refcnt increment
     269             :      and decrement attempts are serialized at the replay tile, and that
     270             :      there are no racy resurrection of a soon-to-be-pruned block.  If a
     271             :      refcnt increment request arrives after a block has been pruned,
     272             :      replay simply rejects the request.
     273             : 
     274             :      A note on the implementation of the above ...
     275             : 
     276             :      Upon receiving a new consensus root, we descend down the rooted
     277             :      fork from the current published root to the new consensus root.  On
     278             :      each node/block of the rooted fork, we do a summation of the refcnt
     279             :      on the block and all the minority fork blocks branching from the
     280             :      block.  If the summation is 0, the block is safe for pruning.  We
     281             :      advance the published root to the far end of the consecutive run of
     282             :      0 refcnt sums originating from the current published root.  On our
     283             :      descent down the minority forks, we also mark any block that hasn't
     284             :      finished replaying as dead, so we don't waste time executing them.
     285             :      No more transactions shall be dispatched for execution from dead
     286             :      blocks.
     287             : 
     288             :      Blocks start out with a refcnt of 0.  Other tiles may send a
     289             :      request to the replay tile for a reference on a block.  The
     290             :      transaction dispatcher is another source of refcnt updates.  On
     291             :      every dispatch of a transaction for block B, we increment the
     292             :      refcnt for B.  And on every transaction finalization, we decrement
     293             :      the refcnt for B.  This means that whenever the refcnt on a block
     294             :      is 0, there is no more reference on that block from the execution
     295             :      pipeline.  While it might be tempting to simply increment the
     296             :      refcnt once when we start replaying a block, and decrement the
     297             :      refcnt once when we finish a block, this more fine-grained refcnt
     298             :      update strategy allows for aborting and potentially immediate
     299             :      pruning of blocks under interleaved block replay.  Upon receiving a
     300             :      new consensus root, we can simply look at the refcnt on minority
     301             :      fork blocks, and a refcnt of 0 would imply that the block is safe
     302             :      for pruning, even if we haven't finished replaying it.  Without the
     303             :      fine-grained refcnt, we would need to first stop dispatching from
     304             :      the aborted block, and then wait for a full drain of the execution
     305             :      pipeline to know for sure that there are no more in-flight
     306             :      transactions executing on the aborted block.  Note that this will
     307             :      allow the refcnt on any block to transiently drop down to 0.  We
     308             :      will not mistakenly prune an actively replaying block, aka a leaf
     309             :      node, that is chaining off of the rooted fork, because the
     310             :      consensus root is always an ancestor of the actively replaying tip.
     311             :      */
     312             :   fd_hash_t consensus_root;          /* The most recent block to have reached max lockout in the tower. */
     313             :   ulong     consensus_root_slot;     /* slot number of the above. */
     314             :   ulong     consensus_root_bank_idx; /* bank index of the above. */
     315             :   ulong     published_root_slot;     /* slot number of the published root. */
     316             :   ulong     published_root_bank_idx; /* bank index of the published root. */
     317             : 
     318             :   /* We need to maintain a tile-local mapping of block-ids to bank index
     319             :      and vice versa.  This translation layer is needed for conversion
     320             :      since tower operates on block-ids and downstream consumers of FEC
     321             :      sets operate on bank indices.  This mapping must happen both ways:
     322             :      1. tower sends us block ids and we must map them to bank indices.
     323             :      2. when a block is completed, we must map the bank index to a block
     324             :         id to send a slot complete message to tower. */
     325             :   ulong               block_id_len;
     326             :   fd_block_id_ele_t * block_id_arr;
     327             :   fd_block_id_map_t * block_id_map;
     328             : 
     329             :   /* Capture-related configs */
     330             :   fd_capture_ctx_t * capture_ctx;
     331             :   FILE *             capture_file;
     332             : 
     333             :   /* Whether the runtime has been booted either from snapshot loading
     334             :      or from genesis. */
     335             :   int is_booted;
     336             : 
     337             :   /* Buffer to store vote towers that need to be published to the Tower
     338             :      tile. */
     339             : 
     340             :   fd_multi_epoch_leaders_t * mleaders;
     341             : 
     342             :   int larger_max_cost_per_block;
     343             : 
     344             :   fd_pubkey_t identity_pubkey[1]; /* TODO: Keyswitch */
     345             : 
     346             :   /* When we transition to becoming leader, we can only unbecome the
     347             :      leader if we have received a block id from the FEC reassembler, and
     348             :      a message from PoH that the leader slot has ended.  After both of
     349             :      these conditions are met, then we are free to unbecome the leader.
     350             :   */
     351             :   int         is_leader;
     352             :   int         recv_poh;
     353             :   int         recv_block_id;
     354             :   ulong       next_leader_slot;
     355             :   long        next_leader_tickcount;
     356             :   ulong       highwater_leader_slot;
     357             :   ulong       reset_slot;
     358             :   fd_bank_t * reset_bank;
     359             :   fd_hash_t   reset_block_id;
     360             :   long        reset_timestamp_nanos;
     361             :   double      slot_duration_nanos;
     362             :   double      slot_duration_ticks;
     363             :   fd_bank_t * leader_bank; /* ==NULL if not currently the leader */
     364             : 
     365             :   ulong  resolv_tile_cnt;
     366             : 
     367             :   int in_kind[ 64 ];
     368             :   fd_replay_in_link_t in[ 64 ];
     369             : 
     370             :   fd_replay_out_link_t replay_out[1];
     371             : 
     372             :   fd_replay_out_link_t stake_out[1];
     373             : 
     374             :   /* The gui tile needs to reliably own a reference to the most recent
     375             :      completed active bank.  Replay needs to know if the gui as a
     376             :      consumer is enabled so it can increment the bank's refcnt before
     377             :      publishing the bank_idx to the gui. */
     378             :   int gui_enabled;
     379             :   int rpc_enabled;
     380             : 
     381             : # if FD_HAS_FLATCC
     382             :   /* For dumping blocks to protobuf. For backtest only. */
     383             :   fd_block_dump_ctx_t * block_dump_ctx;
     384             : # endif
     385             : 
     386             :   /* We need a few pieces of information to compute the right addresses
     387             :      for bundle crank information that we need to send to pack. */
     388             :   struct {
     389             :     int                   enabled;
     390             :     fd_pubkey_t           vote_account;
     391             :     fd_bundle_crank_gen_t gen[1];
     392             :   } bundle;
     393             : 
     394             :   struct {
     395             :     fd_histf_t store_read_wait[ 1 ];
     396             :     fd_histf_t store_read_work[ 1 ];
     397             :     fd_histf_t store_publish_wait[ 1 ];
     398             :     fd_histf_t store_publish_work[ 1 ];
     399             :     fd_histf_t store_link_wait[ 1 ];
     400             :     fd_histf_t store_link_work[ 1 ];
     401             : 
     402             :     ulong slots_total;
     403             :     ulong transactions_total;
     404             :   } metrics;
     405             : 
     406             :   uchar __attribute__((aligned(FD_MULTI_EPOCH_LEADERS_ALIGN))) mleaders_mem[ FD_MULTI_EPOCH_LEADERS_FOOTPRINT ];
     407             : 
     408             :   fd_runtime_stack_t runtime_stack;
     409             : };
     410             : 
     411             : typedef struct fd_replay_tile fd_replay_tile_t;
     412             : 
     413             : FD_FN_CONST static inline ulong
     414           0 : scratch_align( void ) {
     415           0 :   return 128UL;
     416           0 : }
     417             : FD_FN_PURE static inline ulong
     418           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     419           0 :   ulong chain_cnt = fd_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
     420             : 
     421           0 :   ulong l = FD_LAYOUT_INIT;
     422           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_replay_tile_t),  sizeof(fd_replay_tile_t) );
     423           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_block_id_ele_t), sizeof(fd_block_id_ele_t) * tile->replay.max_live_slots );
     424           0 :   l = FD_LAYOUT_APPEND( l, fd_block_id_map_align(),    fd_block_id_map_footprint( chain_cnt ) );
     425           0 :   l = FD_LAYOUT_APPEND( l, fd_txncache_align(),        fd_txncache_footprint( tile->replay.max_live_slots ) );
     426           0 :   l = FD_LAYOUT_APPEND( l, fd_reasm_align(),           fd_reasm_footprint( 1 << 20 ) );
     427           0 :   l = FD_LAYOUT_APPEND( l, fd_sched_align(),           fd_sched_footprint( tile->replay.max_live_slots ) );
     428           0 :   l = FD_LAYOUT_APPEND( l, fd_vote_tracker_align(),    fd_vote_tracker_footprint() );
     429           0 :   l = FD_LAYOUT_APPEND( l, fd_capture_ctx_align(),     fd_capture_ctx_footprint() );
     430             : 
     431           0 : # if FD_HAS_FLATCC
     432           0 :   if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
     433           0 :     l = FD_LAYOUT_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
     434           0 :   }
     435           0 : # endif
     436             : 
     437           0 :   l = FD_LAYOUT_FINI( l, scratch_align() );
     438             : 
     439           0 :   return l;
     440           0 : }
     441             : 
     442             : static inline void
     443           0 : metrics_write( fd_replay_tile_t * ctx ) {
     444           0 :   FD_MHIST_COPY( REPLAY, STORE_LINK_WAIT,    ctx->metrics.store_link_wait );
     445           0 :   FD_MHIST_COPY( REPLAY, STORE_LINK_WORK,    ctx->metrics.store_link_work );
     446           0 :   FD_MHIST_COPY( REPLAY, STORE_READ_WAIT,    ctx->metrics.store_read_wait );
     447           0 :   FD_MHIST_COPY( REPLAY, STORE_READ_WORK,    ctx->metrics.store_read_work );
     448           0 :   FD_MHIST_COPY( REPLAY, STORE_PUBLISH_WAIT, ctx->metrics.store_publish_wait );
     449           0 :   FD_MHIST_COPY( REPLAY, STORE_PUBLISH_WORK, ctx->metrics.store_publish_work );
     450             : 
     451           0 :   FD_MGAUGE_SET( REPLAY, ROOT_SLOT, ctx->consensus_root_slot==ULONG_MAX ? 0UL : ctx->consensus_root_slot );
     452           0 :   ulong leader_slot = ctx->leader_bank ? fd_bank_slot_get( ctx->leader_bank ) : 0UL;
     453           0 :   FD_MGAUGE_SET( REPLAY, LEADER_SLOT, leader_slot );
     454             : 
     455           0 :   if( FD_LIKELY( ctx->leader_bank ) ) {
     456           0 :     FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, leader_slot );
     457           0 :     FD_MGAUGE_SET( REPLAY, LEADER_SLOT, leader_slot );
     458           0 :   } else {
     459           0 :     FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, ctx->next_leader_slot==ULONG_MAX ? 0UL : ctx->next_leader_slot );
     460           0 :     FD_MGAUGE_SET( REPLAY, LEADER_SLOT, 0UL );
     461           0 :   }
     462           0 :   FD_MGAUGE_SET( REPLAY, RESET_SLOT, ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot );
     463             : 
     464           0 :   fd_bank_t * bank_pool = fd_banks_get_bank_pool( ctx->banks );
     465           0 :   ulong live_banks = fd_banks_pool_max( bank_pool ) - fd_banks_pool_free( bank_pool );
     466           0 :   FD_MGAUGE_SET( REPLAY, LIVE_BANKS, live_banks );
     467             : 
     468           0 :   FD_MCNT_SET( REPLAY, SLOTS_TOTAL, ctx->metrics.slots_total );
     469           0 :   FD_MCNT_SET( REPLAY, TRANSACTIONS_TOTAL, ctx->metrics.transactions_total );
     470             : 
     471           0 :   FD_MCNT_SET( REPLAY, PROGCACHE_ROOTED,  ctx->progcache_admin->metrics.root_cnt );
     472           0 :   FD_MCNT_SET( REPLAY, PROGCACHE_GC_ROOT, ctx->progcache_admin->metrics.gc_root_cnt );
     473           0 : }
     474             : 
     475             : static inline ulong
     476             : generate_stake_weight_msg( ulong                       epoch,
     477             :                            fd_epoch_schedule_t const * epoch_schedule,
     478             :                            fd_vote_states_t const *    epoch_stakes,
     479           0 :                            ulong *                     stake_weight_msg_out ) {
     480           0 :   fd_stake_weight_msg_t *  stake_weight_msg = (fd_stake_weight_msg_t *)fd_type_pun( stake_weight_msg_out );
     481           0 :   fd_vote_stake_weight_t * stake_weights    = stake_weight_msg->weights;
     482             : 
     483           0 :   stake_weight_msg->epoch             = epoch;
     484           0 :   stake_weight_msg->start_slot        = fd_epoch_slot0( epoch_schedule, epoch );
     485           0 :   stake_weight_msg->slot_cnt          = epoch_schedule->slots_per_epoch;
     486           0 :   stake_weight_msg->excluded_stake    = 0UL;
     487           0 :   stake_weight_msg->vote_keyed_lsched = 1UL;
     488             : 
     489             :   /* FIXME: SIMD-0180 - hack to (de)activate in testnet vs mainnet.
     490             :      This code can be removed once the feature is active. */
     491           0 :   if( (1==epoch_schedule->warmup && epoch<FD_SIMD0180_ACTIVE_EPOCH_TESTNET) ||
     492           0 :       (0==epoch_schedule->warmup && epoch<FD_SIMD0180_ACTIVE_EPOCH_MAINNET) ) {
     493           0 :     stake_weight_msg->vote_keyed_lsched = 0UL;
     494           0 :   }
     495             : 
     496             :   /* epoch_stakes from manifest are already filtered (stake>0), but not sorted */
     497           0 :   fd_vote_states_iter_t iter_[1];
     498           0 :   ulong idx = 0UL;
     499           0 :   for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, epoch_stakes ); !fd_vote_states_iter_done( iter ); fd_vote_states_iter_next( iter ) ) {
     500           0 :     fd_vote_state_ele_t * vote_state = fd_vote_states_iter_ele( iter );
     501           0 :     if( FD_UNLIKELY( !vote_state->stake ) ) continue;
     502             : 
     503           0 :     stake_weights[ idx ].stake = vote_state->stake;
     504           0 :     memcpy( stake_weights[ idx ].id_key.uc, &vote_state->node_account, sizeof(fd_pubkey_t) );
     505           0 :     memcpy( stake_weights[ idx ].vote_key.uc, &vote_state->vote_account, sizeof(fd_pubkey_t) );
     506           0 :     idx++;
     507           0 :   }
     508           0 :   stake_weight_msg->staked_cnt = idx;
     509           0 :   sort_vote_weights_by_stake_vote_inplace( stake_weights, idx );
     510             : 
     511           0 :   return fd_stake_weight_msg_sz( idx );
     512           0 : }
     513             : 
     514             : static void
     515             : publish_stake_weights( fd_replay_tile_t *   ctx,
     516             :                        fd_stem_context_t *  stem,
     517             :                        fd_bank_t *          bank,
     518           0 :                        int                  current_epoch ) {
     519           0 :   fd_epoch_schedule_t const * schedule = fd_bank_epoch_schedule_query( bank );
     520           0 :   ulong epoch = fd_slot_to_epoch( schedule, fd_bank_slot_get( bank ), NULL );
     521             : 
     522           0 :   fd_vote_states_t const * vote_states_prev;
     523           0 :   if( FD_LIKELY( current_epoch ) ) vote_states_prev = fd_bank_vote_states_prev_locking_query( bank );
     524           0 :   else                             vote_states_prev = fd_bank_vote_states_prev_prev_locking_query( bank );
     525             : 
     526           0 :   ulong * stake_weights_msg = fd_chunk_to_laddr( ctx->stake_out->mem, ctx->stake_out->chunk );
     527           0 :   ulong stake_weights_sz = generate_stake_weight_msg( epoch+fd_ulong_if( current_epoch, 1UL, 0UL), schedule, vote_states_prev, stake_weights_msg );
     528           0 :   ulong stake_weights_sig = 4UL;
     529           0 :   fd_stem_publish( stem, ctx->stake_out->idx, stake_weights_sig, ctx->stake_out->chunk, stake_weights_sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     530           0 :   ctx->stake_out->chunk = fd_dcache_compact_next( ctx->stake_out->chunk, stake_weights_sz, ctx->stake_out->chunk0, ctx->stake_out->wmark );
     531             : 
     532           0 :   if( FD_LIKELY( current_epoch ) ) fd_bank_vote_states_prev_end_locking_query( bank );
     533           0 :   else                             fd_bank_vote_states_prev_prev_end_locking_query( bank );
     534             : 
     535           0 :   fd_multi_epoch_leaders_stake_msg_init( ctx->mleaders, fd_type_pun_const( stake_weights_msg ) );
     536           0 :   fd_multi_epoch_leaders_stake_msg_fini( ctx->mleaders );
     537           0 : }
     538             : 
     539             : /**********************************************************************/
     540             : /* Transaction execution state machine helpers                        */
     541             : /**********************************************************************/
     542             : 
     543             : static fd_bank_t *
     544             : replay_block_start( fd_replay_tile_t *  ctx,
     545             :                     fd_stem_context_t * stem,
     546             :                     ulong               bank_idx,
     547             :                     ulong               parent_bank_idx,
     548           0 :                     ulong               slot ) {
     549           0 :   long before = fd_log_wallclock();
     550             : 
     551             :   /* Switch to a new block that we don't have a bank for. */
     552             : 
     553           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
     554           0 :   if( FD_UNLIKELY( !bank ) ) {
     555           0 :     FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", bank_idx ));
     556           0 :   }
     557           0 :   if( FD_UNLIKELY( bank->flags!=FD_BANK_FLAGS_INIT ) ) {
     558           0 :     FD_LOG_CRIT(( "invariant violation: bank is not in correct state for bank index %lu", bank_idx ));
     559           0 :   }
     560             : 
     561           0 :   bank->preparation_begin_nanos = before;
     562             : 
     563           0 :   fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
     564           0 :   if( FD_UNLIKELY( !parent_bank ) ) {
     565           0 :     FD_LOG_CRIT(( "invariant violation: parent bank is NULL for bank index %lu", parent_bank_idx ));
     566           0 :   }
     567           0 :   if( FD_UNLIKELY( !(parent_bank->flags&FD_BANK_FLAGS_FROZEN) ) ) {
     568           0 :     FD_LOG_CRIT(( "invariant violation: parent bank is not frozen for bank index %lu", parent_bank_idx ));
     569           0 :   }
     570           0 :   ulong parent_slot = fd_bank_slot_get( parent_bank );
     571             : 
     572             :   /* Clone the bank from the parent.  We must special case the first
     573             :      slot that is executed as the snapshot does not provide a parent
     574             :      block id. */
     575             : 
     576           0 :   bank = fd_banks_clone_from_parent( ctx->banks, bank_idx, parent_bank_idx );
     577           0 :   if( FD_UNLIKELY( !bank ) ) {
     578           0 :     FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", bank_idx ));
     579           0 :   }
     580           0 :   fd_bank_slot_set( bank, slot );
     581           0 :   fd_bank_parent_slot_set( bank, parent_slot );
     582           0 :   bank->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, parent_bank->txncache_fork_id );
     583             : 
     584             :   /* Create a new funk txn for the block. */
     585             : 
     586           0 :   fd_funk_txn_xid_t xid        = { .ul = { slot, bank_idx } };
     587           0 :   fd_funk_txn_xid_t parent_xid = { .ul = { parent_slot, parent_bank_idx } };
     588           0 :   fd_accdb_attach_child( ctx->accdb_admin, &parent_xid, &xid );
     589           0 :   fd_progcache_txn_attach_child( ctx->progcache_admin, &parent_xid, &xid );
     590             : 
     591             :   /* Update any required runtime state and handle any potential epoch
     592             :      boundary change. */
     593             : 
     594           0 :   if( ctx->capture_ctx ) {
     595           0 :     fd_solcap_writer_set_slot( ctx->capture_ctx->capture, slot );
     596           0 :   }
     597             : 
     598           0 :   fd_bank_shred_cnt_set( bank, 0UL );
     599           0 :   fd_bank_execution_fees_set( bank, 0UL );
     600           0 :   fd_bank_priority_fees_set( bank, 0UL );
     601           0 :   fd_bank_tips_set( bank, 0UL );
     602             : 
     603           0 :   fd_bank_has_identity_vote_set( bank, 0 );
     604             : 
     605             :   /* Set the tick height. */
     606           0 :   fd_bank_tick_height_set( bank, fd_bank_max_tick_height_get( bank ) );
     607             : 
     608             :   /* Update block height. */
     609           0 :   fd_bank_block_height_set( bank, fd_bank_block_height_get( bank ) + 1UL );
     610             : 
     611           0 :   ulong * max_tick_height = fd_bank_max_tick_height_modify( bank );
     612           0 :   ulong   ticks_per_slot  = fd_bank_ticks_per_slot_get( bank );
     613           0 :   if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS != fd_runtime_compute_max_tick_height( ticks_per_slot, slot, max_tick_height ) ) ) {
     614           0 :     FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
     615           0 :   }
     616             : 
     617           0 :   int is_epoch_boundary = 0;
     618           0 :   fd_runtime_block_execute_prepare( ctx->banks, bank, ctx->accdb, &ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
     619           0 :   if( FD_UNLIKELY( is_epoch_boundary ) ) publish_stake_weights( ctx, stem, bank, 1 );
     620             : 
     621           0 :   return bank;
     622           0 : }
     623             : 
     624             : static void
     625           0 : cost_tracker_snap( fd_bank_t * bank, fd_replay_slot_completed_t * slot_info ) {
     626           0 :   if( bank->cost_tracker_pool_idx!=fd_bank_cost_tracker_pool_idx_null( fd_bank_get_cost_tracker_pool( bank ) ) ) {
     627           0 :     fd_cost_tracker_t const * cost_tracker = fd_bank_cost_tracker_locking_query( bank );
     628           0 :     slot_info->cost_tracker.block_cost                   = cost_tracker->block_cost;
     629           0 :     slot_info->cost_tracker.vote_cost                    = cost_tracker->vote_cost;
     630           0 :     slot_info->cost_tracker.allocated_accounts_data_size = cost_tracker->allocated_accounts_data_size;
     631           0 :     slot_info->cost_tracker.block_cost_limit             = cost_tracker->block_cost_limit;
     632           0 :     slot_info->cost_tracker.vote_cost_limit              = cost_tracker->vote_cost_limit;
     633           0 :     slot_info->cost_tracker.account_cost_limit           = cost_tracker->account_cost_limit;
     634           0 :     fd_bank_cost_tracker_end_locking_query( bank );
     635           0 :   } else {
     636           0 :     memset( &slot_info->cost_tracker, 0, sizeof(slot_info->cost_tracker) );
     637           0 :   }
     638           0 : }
     639             : 
     640             : static ulong
     641           0 : get_identity_balance( fd_replay_tile_t * ctx, fd_funk_txn_xid_t xid ) {
     642           0 :   ulong identity_balance = ULONG_MAX;
     643           0 :   fd_txn_account_t identity_acc[1];
     644           0 :   fd_funk_t * funk = fd_accdb_user_v1_funk( ctx->accdb );
     645           0 :   int err = fd_txn_account_init_from_funk_readonly( identity_acc,
     646           0 :                                                     ctx->identity_pubkey,
     647           0 :                                                     funk,
     648           0 :                                                     &xid );
     649           0 :   if( FD_LIKELY( !err && identity_acc->meta ) ) identity_balance = identity_acc->meta->lamports;
     650             : 
     651           0 :   return identity_balance;
     652           0 : }
     653             : 
     654             : static void
     655             : publish_slot_completed( fd_replay_tile_t *  ctx,
     656             :                         fd_stem_context_t * stem,
     657             :                         fd_bank_t *         bank,
     658             :                         int                 is_initial,
     659           0 :                         int                 is_leader ) {
     660             : 
     661           0 :   ulong slot = fd_bank_slot_get( bank );
     662             : 
     663           0 :   fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ bank->idx ];
     664             : 
     665             :   /* HACKY: hacky way of checking if we should send a null parent block
     666             :      id */
     667           0 :   fd_hash_t parent_block_id = {0};
     668           0 :   if( FD_UNLIKELY( !is_initial ) ) {
     669           0 :     parent_block_id = ctx->block_id_arr[ bank->parent_idx ].block_id;
     670           0 :   }
     671             : 
     672           0 :   fd_hash_t const * bank_hash  = fd_bank_bank_hash_query( bank );
     673           0 :   fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( fd_bank_block_hash_queue_query( bank ) );
     674           0 :   FD_TEST( bank_hash  );
     675           0 :   FD_TEST( block_hash );
     676             : 
     677           0 :   if( FD_LIKELY( !is_initial ) ) fd_txncache_finalize_fork( ctx->txncache, bank->txncache_fork_id, 0UL, block_hash->uc );
     678             : 
     679           0 :   fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
     680           0 :   ulong slot_idx;
     681           0 :   ulong epoch = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
     682             : 
     683           0 :   ctx->metrics.slots_total++;
     684           0 :   ctx->metrics.transactions_total = fd_bank_txn_count_get( bank );
     685             : 
     686           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
     687           0 :   slot_info->slot                  = slot;
     688           0 :   slot_info->root_slot             = ctx->consensus_root_slot;
     689           0 :   slot_info->storage_slot          = ctx->published_root_slot;
     690           0 :   slot_info->epoch                 = epoch;
     691           0 :   slot_info->slot_in_epoch         = slot_idx;
     692           0 :   slot_info->block_height          = fd_bank_block_height_get( bank );
     693           0 :   slot_info->parent_slot           = fd_bank_parent_slot_get( bank );
     694           0 :   slot_info->block_id              = block_id_ele->block_id;
     695           0 :   slot_info->parent_block_id       = parent_block_id;
     696           0 :   slot_info->bank_hash             = *bank_hash;
     697           0 :   slot_info->block_hash            = *block_hash;
     698           0 :   slot_info->transaction_count     = fd_bank_txn_count_get( bank );
     699             : 
     700           0 :   fd_inflation_t inflation = fd_bank_inflation_get( bank );
     701           0 :   slot_info->inflation.foundation      = inflation.foundation;
     702           0 :   slot_info->inflation.foundation_term = inflation.foundation_term;
     703           0 :   slot_info->inflation.terminal        = inflation.terminal;
     704           0 :   slot_info->inflation.initial         = inflation.initial;
     705           0 :   slot_info->inflation.taper           = inflation.taper;
     706             : 
     707           0 :   fd_rent_t rent = fd_bank_rent_get( bank );
     708           0 :   slot_info->rent.burn_percent            = rent.burn_percent;
     709           0 :   slot_info->rent.lamports_per_uint8_year = rent.lamports_per_uint8_year;
     710           0 :   slot_info->rent.exemption_threshold     = rent.exemption_threshold;
     711             : 
     712           0 :   slot_info->first_fec_set_received_nanos      = bank->first_fec_set_received_nanos;
     713           0 :   slot_info->preparation_begin_nanos           = bank->preparation_begin_nanos;
     714           0 :   slot_info->first_transaction_scheduled_nanos = bank->first_transaction_scheduled_nanos;
     715           0 :   slot_info->last_transaction_finished_nanos   = bank->last_transaction_finished_nanos;
     716           0 :   slot_info->completion_time_nanos             = fd_log_wallclock();
     717             : 
     718             :   /* refcnt should be incremented by 1 for each consumer that uses
     719             :      `bank_idx`.  Each consumer should decrement the bank's refcnt once
     720             :      they are done usin the bank. */
     721           0 :   bank->refcnt++; /* tower_tile */
     722           0 :   if( FD_LIKELY( ctx->gui_enabled ) ) bank->refcnt++; /* gui tile */
     723           0 :   slot_info->bank_idx = bank->idx;
     724             : 
     725           0 :   slot_info->parent_bank_idx = ULONG_MAX;
     726           0 :   fd_bank_t * parent_bank = fd_banks_get_parent( ctx->banks, bank );
     727           0 :   if( FD_LIKELY( parent_bank && ctx->gui_enabled ) ) {
     728           0 :     parent_bank->refcnt++;
     729           0 :     slot_info->parent_bank_idx = parent_bank->idx;
     730           0 :   }
     731             : 
     732           0 :   slot_info->is_leader = is_leader;
     733             : 
     734           0 :   fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_SLOT_COMPLETED, ctx->replay_out->chunk, sizeof(fd_replay_slot_completed_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     735           0 :   ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_replay_slot_completed_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
     736           0 : }
     737             : 
     738             : static void
     739             : publish_slot_dead( fd_replay_tile_t *  ctx,
     740             :                    fd_stem_context_t * stem,
     741           0 :                    fd_bank_t *         bank ) {
     742           0 :   fd_replay_slot_dead_t * slot_dead = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
     743           0 :   slot_dead->slot                   = fd_bank_slot_get( bank );
     744           0 :   slot_dead->block_id               = ctx->block_id_arr[ bank->idx ].block_id;
     745           0 :   fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_SLOT_DEAD, ctx->replay_out->chunk, sizeof(fd_replay_slot_dead_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     746           0 :   ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_replay_slot_dead_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
     747           0 : }
     748             : 
     749             : static void
     750             : replay_block_finalize( fd_replay_tile_t *  ctx,
     751             :                        fd_stem_context_t * stem,
     752           0 :                        fd_bank_t *         bank ) {
     753             : 
     754           0 :   bank->last_transaction_finished_nanos = fd_log_wallclock();
     755             : 
     756           0 :   if( FD_UNLIKELY( ctx->capture_ctx ) ) fd_solcap_writer_flush( ctx->capture_ctx->capture );
     757             : 
     758           0 :   FD_TEST( !(bank->flags&FD_BANK_FLAGS_FROZEN) );
     759             : 
     760             :   /* Set poh hash in bank. */
     761           0 :   fd_hash_t * poh = fd_sched_get_poh( ctx->sched, bank->idx );
     762           0 :   fd_bank_poh_set( bank, *poh );
     763             : 
     764             :   /* Set shred count in bank. */
     765           0 :   fd_bank_shred_cnt_set( bank, fd_sched_get_shred_cnt( ctx->sched, bank->idx ) );
     766             : 
     767             :   /* Do hashing and other end-of-block processing. */
     768           0 :   fd_runtime_block_execute_finalize( bank, ctx->accdb, ctx->capture_ctx );
     769             : 
     770             :   /* Copy out cost tracker fields before freezing */
     771           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
     772           0 :   cost_tracker_snap( bank, slot_info );
     773             : 
     774             :   /* fetch identity / vote balance updates infrequently */
     775           0 :   ulong slot = fd_bank_slot_get( bank );
     776           0 :   fd_funk_txn_xid_t xid = { .ul = { slot, bank->idx } };
     777           0 :   slot_info->identity_balance = FD_UNLIKELY( slot%4096==0UL ) ? get_identity_balance( ctx, xid ) : ULONG_MAX;
     778             : 
     779             :   /* Mark the bank as frozen. */
     780           0 :   fd_banks_mark_bank_frozen( ctx->banks, bank );
     781             : 
     782             :   /**********************************************************************/
     783             :   /* Bank hash comparison, and halt if there's a mismatch after replay  */
     784             :   /**********************************************************************/
     785             : 
     786           0 :   fd_hash_t const * bank_hash = fd_bank_bank_hash_query( bank );
     787           0 :   FD_TEST( bank_hash );
     788             : 
     789             :   /* Must be last so we can measure completion time correctly, even
     790             :      though we could technically do this before the hash cmp and vote
     791             :      tower stuff. */
     792           0 :   publish_slot_completed( ctx, stem, bank, 0, 0 /* is_leader */ );
     793             : 
     794           0 : # if FD_HAS_FLATCC
     795             :   /* If enabled, dump the block to a file and reset the dumping
     796             :      context state */
     797           0 :   if( FD_UNLIKELY( ctx->capture_ctx && ctx->capture_ctx->dump_block_to_pb ) ) {
     798           0 :     fd_funk_t * funk = fd_accdb_user_v1_funk( ctx->accdb );
     799           0 :     fd_dump_block_to_protobuf( ctx->block_dump_ctx, ctx->banks, bank, funk, ctx->capture_ctx );
     800           0 :     fd_block_dump_context_reset( ctx->block_dump_ctx );
     801           0 :   }
     802           0 : # endif
     803           0 : }
     804             : 
     805             : /**********************************************************************/
     806             : /* Leader bank management                                             */
     807             : /**********************************************************************/
     808             : 
     809             : static fd_bank_t *
     810             : prepare_leader_bank( fd_replay_tile_t *  ctx,
     811             :                      ulong               slot,
     812             :                      long                now,
     813             :                      fd_hash_t const *   parent_block_id,
     814           0 :                      fd_stem_context_t * stem ) {
     815           0 :   long before = fd_log_wallclock();
     816             : 
     817             :   /* Make sure that we are not already leader. */
     818           0 :   FD_TEST( ctx->leader_bank==NULL );
     819             : 
     820           0 :   fd_block_id_ele_t * parent_ele = fd_block_id_map_ele_query( ctx->block_id_map, parent_block_id, NULL, ctx->block_id_arr );
     821           0 :   if( FD_UNLIKELY( !parent_ele ) ) {
     822           0 :     FD_LOG_CRIT(( "invariant violation: parent bank index not found for merkle root %s", FD_BASE58_ENC_32_ALLOCA( parent_block_id->uc ) ));
     823           0 :   }
     824           0 :   ulong parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent_ele );
     825             : 
     826           0 :   fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
     827           0 :   if( FD_UNLIKELY( !parent_bank ) ) {
     828           0 :     FD_LOG_CRIT(( "invariant violation: parent bank not found for bank index %lu", parent_bank_idx ));
     829           0 :   }
     830           0 :   ulong parent_slot = fd_bank_slot_get( parent_bank );
     831             : 
     832           0 :   ctx->leader_bank = fd_banks_new_bank( ctx->banks, parent_bank_idx, now );
     833           0 :   if( FD_UNLIKELY( !ctx->leader_bank ) ) {
     834           0 :     FD_LOG_CRIT(( "invariant violation: leader bank is NULL for slot %lu", slot ));
     835           0 :   }
     836             : 
     837           0 :   if( FD_UNLIKELY( !fd_banks_clone_from_parent( ctx->banks, ctx->leader_bank->idx, parent_bank_idx ) ) ) {
     838           0 :     FD_LOG_CRIT(( "invariant violation: bank is NULL for slot %lu", slot ));
     839           0 :   }
     840             : 
     841           0 :   ctx->leader_bank->preparation_begin_nanos = before;
     842             : 
     843           0 :   fd_bank_slot_set( ctx->leader_bank, slot );
     844           0 :   fd_bank_parent_slot_set( ctx->leader_bank, parent_slot );
     845           0 :   ctx->leader_bank->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, parent_bank->txncache_fork_id );
     846             :   /* prepare the funk transaction for the leader bank */
     847           0 :   fd_funk_txn_xid_t xid        = { .ul = { slot, ctx->leader_bank->idx } };
     848           0 :   fd_funk_txn_xid_t parent_xid = { .ul = { parent_slot, parent_bank_idx } };
     849           0 :   fd_accdb_attach_child( ctx->accdb_admin, &parent_xid, &xid );
     850           0 :   fd_progcache_txn_attach_child( ctx->progcache_admin, &parent_xid, &xid );
     851             : 
     852           0 :   fd_bank_execution_fees_set( ctx->leader_bank, 0UL );
     853           0 :   fd_bank_priority_fees_set( ctx->leader_bank, 0UL );
     854           0 :   fd_bank_shred_cnt_set( ctx->leader_bank, 0UL );
     855           0 :   fd_bank_tips_set( ctx->leader_bank, 0UL );
     856             : 
     857             :   /* Set the tick height. */
     858           0 :   fd_bank_tick_height_set( ctx->leader_bank, fd_bank_max_tick_height_get( ctx->leader_bank ) );
     859             : 
     860             :   /* Update block height. */
     861           0 :   fd_bank_block_height_set( ctx->leader_bank, fd_bank_block_height_get( ctx->leader_bank ) + 1UL );
     862             : 
     863           0 :   ulong * max_tick_height = fd_bank_max_tick_height_modify( ctx->leader_bank );
     864           0 :   ulong   ticks_per_slot  = fd_bank_ticks_per_slot_get( ctx->leader_bank );
     865           0 :   if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS != fd_runtime_compute_max_tick_height( ticks_per_slot, slot, max_tick_height ) ) ) {
     866           0 :     FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
     867           0 :   }
     868             : 
     869           0 :   int is_epoch_boundary = 0;
     870           0 :   fd_runtime_block_execute_prepare( ctx->banks, ctx->leader_bank, ctx->accdb, &ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
     871           0 :   if( FD_UNLIKELY( is_epoch_boundary ) ) publish_stake_weights( ctx, stem, ctx->leader_bank, 1 );
     872             : 
     873             :   /* Now that a bank has been created for the leader slot, increment the
     874             :      reference count until we are done with the leader slot. */
     875           0 :   ctx->leader_bank->refcnt++;
     876             : 
     877           0 :   return ctx->leader_bank;
     878           0 : }
     879             : 
     880             : static void
     881             : fini_leader_bank( fd_replay_tile_t *  ctx,
     882           0 :                   fd_stem_context_t * stem ) {
     883             : 
     884           0 :   FD_TEST( ctx->leader_bank!=NULL );
     885           0 :   FD_TEST( ctx->is_leader );
     886           0 :   FD_TEST( ctx->recv_block_id );
     887           0 :   FD_TEST( ctx->recv_poh );
     888             : 
     889           0 :   ctx->leader_bank->last_transaction_finished_nanos = fd_log_wallclock();
     890             : 
     891           0 :   ulong curr_slot = fd_bank_slot_get( ctx->leader_bank );
     892             : 
     893           0 :   fd_sched_block_add_done( ctx->sched, ctx->leader_bank->idx, ctx->leader_bank->parent_idx, curr_slot );
     894             : 
     895             :   /* Do hashing and other end-of-block processing */
     896           0 :   fd_funk_t * funk = fd_accdb_user_v1_funk( ctx->accdb );
     897           0 :   fd_funk_txn_map_t * txn_map = fd_funk_txn_map( funk );
     898           0 :   if( FD_UNLIKELY( !txn_map->map ) ) {
     899           0 :     FD_LOG_ERR(( "Could not find valid funk transaction map" ));
     900           0 :   }
     901             : 
     902           0 :   fd_runtime_block_execute_finalize( ctx->leader_bank, ctx->accdb, ctx->capture_ctx );
     903             : 
     904           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
     905           0 :   cost_tracker_snap( ctx->leader_bank, slot_info );
     906           0 :   fd_funk_txn_xid_t xid = { .ul = { curr_slot, ctx->leader_bank->idx } };
     907           0 :   slot_info->identity_balance = FD_UNLIKELY( curr_slot%4096==0UL ) ? get_identity_balance( ctx, xid ) : ULONG_MAX;
     908             : 
     909           0 :   fd_banks_mark_bank_frozen( ctx->banks, ctx->leader_bank );
     910             : 
     911           0 :   fd_hash_t const * bank_hash  = fd_bank_bank_hash_query( ctx->leader_bank );
     912           0 :   FD_TEST( bank_hash );
     913             : 
     914           0 :   publish_slot_completed( ctx, stem, ctx->leader_bank, 0, 1 /* is_leader */ );
     915             : 
     916             :   /* The reference on the bank is finally no longer needed. */
     917           0 :   ctx->leader_bank->refcnt--;
     918             : 
     919             :   /* We are no longer leader so we can clear the bank index we use for
     920             :      being the leader. */
     921           0 :   ctx->leader_bank   = NULL;
     922           0 :   ctx->recv_block_id = 0;
     923           0 :   ctx->recv_poh      = 0;
     924           0 :   ctx->is_leader     = 0;
     925           0 : }
     926             : 
     927             : static void
     928             : publish_root_advanced( fd_replay_tile_t *  ctx,
     929           0 :                        fd_stem_context_t * stem ) {
     930             : 
     931             :   /* FIXME: for now we want to send the child of the consensus root to
     932             :      avoid data races with funk root advancing.  This is a temporary
     933             :      hack because currently it is not safe to query against the xid for
     934             :      the root that is being advanced in funk.  This doesn't eliminate
     935             :      the data race that exists in funk, but reduces how often it occurs.
     936             : 
     937             :      Case that causes a data race:
     938             :      replay: we are advancing the root from slot A->B
     939             :      resolv: we are resolving ALUTs against slot B */
     940             : 
     941           0 :   fd_bank_t * consensus_root_bank = fd_banks_bank_query( ctx->banks, ctx->consensus_root_bank_idx );
     942           0 :   if( FD_UNLIKELY( !consensus_root_bank ) ) {
     943           0 :     FD_LOG_CRIT(( "invariant violation: consensus root bank is NULL at bank index %lu", ctx->consensus_root_bank_idx ));
     944           0 :   }
     945             : 
     946           0 :   if( FD_UNLIKELY( consensus_root_bank->child_idx==ULONG_MAX ) ) {
     947           0 :     return;
     948           0 :   }
     949             : 
     950           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, consensus_root_bank->child_idx );
     951           0 :   if( FD_UNLIKELY( !bank ) ) {
     952           0 :     FD_LOG_CRIT(( "invariant violation: consensus root bank child is NULL at bank index %lu", consensus_root_bank->child_idx ));
     953           0 :   }
     954             : 
     955             :   /* Increment the reference count on the consensus root bank to account
     956             :      for the number of exec tiles that are waiting on it. */
     957           0 :   bank->refcnt += ctx->resolv_tile_cnt;
     958             : 
     959           0 :   fd_replay_root_advanced_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
     960           0 :   msg->bank_idx = bank->idx;
     961             : 
     962           0 :   fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_ROOT_ADVANCED, ctx->replay_out->chunk, sizeof(fd_replay_root_advanced_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     963           0 :   ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_replay_root_advanced_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
     964           0 : }
     965             : 
     966             : /* init_funk performs pre-flight checks for the account database and
     967             :    program cache.  Ensures that the account database was set up
     968             :    correctly by bootstrap components (e.g. genesis or snapshot loader).
     969             :    Mirrors the account database's fork tree down to the program cache. */
     970             : 
     971             : static void
     972             : init_funk( fd_replay_tile_t * ctx,
     973           0 :            ulong              bank_slot ) {
     974             :   /* Ensure that the loaded bank root corresponds to the account
     975             :      database's root. */
     976           0 :   fd_funk_t * funk = ctx->accdb_admin->funk;
     977           0 :   if( FD_UNLIKELY( !funk->shmem ) ) {
     978           0 :     FD_LOG_CRIT(( "failed to initialize account database: replay tile is not joined to database shared memory objects" ));
     979           0 :   }
     980           0 :   fd_funk_txn_xid_t const * accdb_pub = fd_funk_last_publish( funk );
     981           0 :   if( FD_UNLIKELY( accdb_pub->ul[0]!=bank_slot ) ) {
     982           0 :     FD_LOG_CRIT(( "failed to initialize account database: accdb is at slot %lu, but chain state is at slot %lu\n"
     983           0 :                   "This is a bug in startup components.",
     984           0 :                   accdb_pub->ul[0], bank_slot ));
     985           0 :   }
     986           0 :   if( FD_UNLIKELY( fd_funk_last_publish_is_frozen( funk ) ) ) {
     987           0 :     FD_LOG_CRIT(( "failed to initialize account database: accdb fork graph is not clean.\n"
     988           0 :                   "The account database should only contain state for the root slot at this point,\n"
     989           0 :                   "but there are incomplete database transactions leftover.\n"
     990           0 :                   "This is a bug in startup components."  ));
     991           0 :   }
     992             : 
     993             :   /* The program cache tracks the account database's fork graph at all
     994             :      times.  Perform initial synchronization: pivot from funk 'root' (a
     995             :      sentinel XID) to 'last publish' (the bootstrap root slot). */
     996           0 :   if( FD_UNLIKELY( !ctx->progcache_admin->funk->shmem ) ) {
     997           0 :     FD_LOG_CRIT(( "failed to initialize account database: replay tile is not joined to program cache" ));
     998           0 :   }
     999           0 :   fd_progcache_clear( ctx->progcache_admin );
    1000           0 :   fd_progcache_txn_attach_child( ctx->progcache_admin, fd_funk_root( ctx->progcache_admin->funk ), fd_funk_last_publish( ctx->accdb_admin->funk ) );
    1001           0 :   fd_progcache_txn_advance_root( ctx->progcache_admin,                                             fd_funk_last_publish( ctx->accdb_admin->funk ) );
    1002           0 : }
    1003             : 
    1004             : static void
    1005           0 : init_after_snapshot( fd_replay_tile_t * ctx ) {
    1006             :   /* Now that the snapshot has been loaded in, we have to refresh the
    1007             :      stake delegations since the manifest does not contain the full set
    1008             :      of data required for the stake delegations. See
    1009             :      fd_stake_delegations.h for why this is required. */
    1010             : 
    1011           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
    1012           0 :   if( FD_UNLIKELY( !bank ) ) {
    1013           0 :     FD_LOG_CRIT(( "invariant violation: replay bank is NULL at bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
    1014           0 :   }
    1015             : 
    1016           0 :   fd_funk_t * funk = fd_accdb_user_v1_funk( ctx->accdb );
    1017           0 :   fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->idx } };
    1018           0 :   init_funk( ctx, fd_bank_slot_get( bank ) );
    1019             : 
    1020           0 :   fd_stake_delegations_t * root_delegations = fd_banks_stake_delegations_root_query( ctx->banks );
    1021             : 
    1022           0 :   fd_stake_delegations_refresh( root_delegations, funk, &xid );
    1023             : 
    1024             :   /* After both snapshots have been loaded in, we can determine if we should
    1025             :      start distributing rewards. */
    1026             : 
    1027           0 :   fd_rewards_recalculate_partitioned_rewards( ctx->banks, bank, funk, &xid, &ctx->runtime_stack, ctx->capture_ctx );
    1028             : 
    1029           0 :   ulong snapshot_slot = fd_bank_slot_get( bank );
    1030           0 :   if( FD_UNLIKELY( !snapshot_slot ) ) {
    1031             :     /* Genesis-specific setup. */
    1032             :     /* FIXME: This branch does not set up a new block exec ctx
    1033             :        properly. Needs to do whatever prepare_new_block_execution
    1034             :        does, but just hacking that in breaks stuff. */
    1035           0 :     fd_runtime_update_leaders( bank, &ctx->runtime_stack );
    1036             : 
    1037           0 :     ulong hashcnt_per_slot = fd_bank_hashes_per_tick_get( bank ) * fd_bank_ticks_per_slot_get( bank );
    1038           0 :     fd_hash_t * poh = fd_bank_poh_modify( bank );
    1039           0 :     while( hashcnt_per_slot-- ) {
    1040           0 :       fd_sha256_hash( poh->hash, 32UL, poh->hash );
    1041           0 :     }
    1042             : 
    1043           0 :     int is_epoch_boundary = 0;
    1044           0 :     fd_runtime_block_execute_prepare( ctx->banks, bank, ctx->accdb, &ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
    1045           0 :     FD_TEST( !is_epoch_boundary );
    1046           0 :     fd_runtime_block_execute_finalize( bank, ctx->accdb, ctx->capture_ctx );
    1047             : 
    1048           0 :     snapshot_slot = 0UL;
    1049           0 :   }
    1050             : 
    1051           0 :   if( FD_UNLIKELY( ctx->capture_ctx ) ) fd_solcap_writer_flush( ctx->capture_ctx->capture );
    1052           0 : }
    1053             : 
    1054             : static inline int
    1055             : maybe_become_leader( fd_replay_tile_t *  ctx,
    1056           0 :                      fd_stem_context_t * stem ) {
    1057           0 :   FD_TEST( ctx->is_booted );
    1058           0 :   if( FD_LIKELY( ctx->next_leader_slot==ULONG_MAX || ctx->is_leader || !ctx->has_identity_vote_rooted || ctx->replay_out->idx==ULONG_MAX ) ) return 0;
    1059             : 
    1060           0 :   FD_TEST( ctx->next_leader_slot>ctx->reset_slot );
    1061           0 :   long now = fd_tickcount();
    1062           0 :   if( FD_LIKELY( now<ctx->next_leader_tickcount ) ) return 0;
    1063             : 
    1064             :   /* TODO:
    1065             :   if( FD_UNLIKELY( ctx->halted_switching_key ) ) return 0; */
    1066             : 
    1067             :   /* If a prior leader is still in the process of publishing their slot,
    1068             :      delay ours to let them finish ... unless they are so delayed that
    1069             :      we risk getting skipped by the leader following us.  1.2 seconds
    1070             :      is a reasonable default here, although any value between 0 and 1.6
    1071             :      seconds could be considered reasonable.  This is arbitrary and
    1072             :      chosen due to intuition. */
    1073           0 :   if( FD_UNLIKELY( now<ctx->next_leader_tickcount+(long)(3.0*ctx->slot_duration_ticks) ) ) {
    1074           0 :     FD_TEST( ctx->reset_bank );
    1075             : 
    1076             :     /* TODO: Make the max_active_descendant calculation more efficient
    1077             :        by caching it in the bank structure and updating it as banks are
    1078             :        created and completed. */
    1079           0 :     ulong max_active_descendant = 0UL;
    1080           0 :     ulong child_idx = ctx->reset_bank->child_idx;
    1081           0 :     while( child_idx!=ULONG_MAX ) {
    1082           0 :       fd_bank_t const * child_bank = fd_banks_bank_query( ctx->banks, child_idx );
    1083           0 :       max_active_descendant = fd_ulong_max( max_active_descendant, fd_bank_slot_get( child_bank ) );
    1084           0 :       child_idx = child_bank->sibling_idx;
    1085           0 :     }
    1086             : 
    1087             :     /* If the max_active_descendant is >= next_leader_slot, we waited
    1088             :        too long and a leader after us started publishing to try and skip
    1089             :        us.  Just start our leader slot immediately, we might win ... */
    1090           0 :     if( FD_LIKELY( max_active_descendant>=ctx->reset_slot && max_active_descendant<ctx->next_leader_slot ) ) {
    1091             :       /* If one of the leaders between the reset slot and our leader
    1092             :          slot is in the process of publishing (they have a descendant
    1093             :          bank that is in progress of being replayed), then keep waiting.
    1094             :          We probably wouldn't get a leader slot out before they
    1095             :          finished.
    1096             : 
    1097             :          Unless... we are past the deadline to start our slot by more
    1098             :          than 1.2 seconds, in which case we should probably start it to
    1099             :          avoid getting skipped by the leader behind us. */
    1100           0 :       return 0;
    1101           0 :     }
    1102           0 :   }
    1103             : 
    1104           0 :   long now_nanos = fd_log_wallclock();
    1105             : 
    1106           0 :   ctx->is_leader     = 1;
    1107           0 :   ctx->recv_poh      = 0;
    1108           0 :   ctx->recv_block_id = 0;
    1109             : 
    1110           0 :   FD_TEST( ctx->highwater_leader_slot==ULONG_MAX || ctx->highwater_leader_slot<ctx->next_leader_slot );
    1111           0 :   ctx->highwater_leader_slot = ctx->next_leader_slot;
    1112             : 
    1113           0 :   FD_LOG_INFO(( "becoming leader for slot %lu, parent slot is %lu", ctx->next_leader_slot, ctx->reset_slot ));
    1114             : 
    1115             :   /* Acquires bank, sets up initial state, and refcnts it. */
    1116           0 :   fd_bank_t *       bank = prepare_leader_bank( ctx, ctx->next_leader_slot, now_nanos, &ctx->reset_block_id, stem );
    1117           0 :   fd_funk_txn_xid_t xid  = { .ul = { ctx->next_leader_slot, ctx->leader_bank->idx } };
    1118             : 
    1119           0 :   fd_bundle_crank_tip_payment_config_t config[1]             = { 0 };
    1120           0 :   fd_acct_addr_t                       tip_receiver_owner[1] = { 0 };
    1121             : 
    1122           0 :   if( FD_UNLIKELY( ctx->bundle.enabled ) ) {
    1123           0 :     fd_acct_addr_t tip_payment_config[1];
    1124           0 :     fd_acct_addr_t tip_receiver[1];
    1125           0 :     fd_bundle_crank_get_addresses( ctx->bundle.gen, fd_bank_epoch_get( bank ), tip_payment_config, tip_receiver );
    1126             : 
    1127           0 :     fd_funk_t * funk = fd_accdb_user_v1_funk( ctx->accdb );
    1128           0 :     fd_txn_account_t tip_config_acc[1];
    1129           0 :     int err = fd_txn_account_init_from_funk_readonly( tip_config_acc,
    1130           0 :                                                       (fd_hash_t *)tip_payment_config->b,
    1131           0 :                                                       funk,
    1132           0 :                                                       &xid );
    1133           0 :     if( FD_UNLIKELY( err ) ) {
    1134           0 :       FD_LOG_CRIT(( "failed to initialize tip payment config account: err=%d", err ));
    1135           0 :     }
    1136           0 :     memcpy( config, fd_txn_account_get_data( tip_config_acc ), sizeof(fd_bundle_crank_tip_payment_config_t) );
    1137             : 
    1138             :     /* It is possible that the tip receiver account does not exist yet
    1139             :        if it is the first time in an epoch. */
    1140           0 :     fd_txn_account_t tip_receiver_acc[1];
    1141           0 :     err = fd_txn_account_init_from_funk_readonly( tip_receiver_acc,
    1142           0 :                                                   (fd_hash_t *)tip_receiver->b,
    1143           0 :                                                   funk,
    1144           0 :                                                   &xid );
    1145           0 :     if( FD_LIKELY( !err ) ) {
    1146           0 :       memcpy( tip_receiver_owner, tip_receiver_acc->meta->owner, sizeof(fd_acct_addr_t) );
    1147           0 :     }
    1148           0 :   }
    1149             : 
    1150             : 
    1151           0 :   fd_became_leader_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1152           0 :   msg->slot = ctx->next_leader_slot;
    1153           0 :   msg->slot_start_ns = now_nanos;
    1154           0 :   msg->slot_end_ns   = now_nanos+(long)ctx->slot_duration_nanos;
    1155           0 :   msg->bank = NULL;
    1156           0 :   msg->bank_idx = bank->idx;
    1157           0 :   msg->ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
    1158           0 :   msg->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
    1159           0 :   msg->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)msg->ticks_per_slot);
    1160           0 :   msg->bundle->config[0]       = config[0];
    1161           0 :   memcpy( msg->bundle->last_blockhash,     (fd_hash_t *)fd_bank_poh_query( bank )->hash, 32UL );
    1162           0 :   memcpy( msg->bundle->tip_receiver_owner, tip_receiver_owner,                           32UL );
    1163             : 
    1164             : 
    1165           0 :   if( FD_UNLIKELY( msg->hashcnt_per_tick==1UL ) ) {
    1166             :     /* Low power producer, maximum of one microblock per tick in the slot */
    1167           0 :     msg->max_microblocks_in_slot = msg->ticks_per_slot;
    1168           0 :   } else {
    1169             :     /* See the long comment in after_credit for this limit */
    1170           0 :     msg->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, msg->ticks_per_slot*(msg->hashcnt_per_tick-1UL) );
    1171           0 :   }
    1172             : 
    1173           0 :   msg->total_skipped_ticks = msg->ticks_per_slot*(ctx->next_leader_slot-ctx->reset_slot);
    1174           0 :   msg->epoch = fd_slot_to_epoch( fd_bank_epoch_schedule_query( bank ), ctx->next_leader_slot, NULL );
    1175             : 
    1176           0 :   fd_cost_tracker_t const * cost_tracker = fd_bank_cost_tracker_locking_query( bank );
    1177             : 
    1178           0 :   msg->limits.slot_max_cost = ctx->larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : cost_tracker->block_cost_limit;
    1179           0 :   msg->limits.slot_max_vote_cost = cost_tracker->vote_cost_limit;
    1180           0 :   msg->limits.slot_max_write_cost_per_acct = cost_tracker->account_cost_limit;
    1181             : 
    1182           0 :   fd_bank_cost_tracker_end_locking_query( bank );
    1183             : 
    1184           0 :   if( FD_UNLIKELY( msg->ticks_per_slot+msg->total_skipped_ticks>USHORT_MAX ) ) {
    1185             :     /* There can be at most USHORT_MAX skipped ticks, because the
    1186             :        parent_offset field in the shred data is only 2 bytes wide. */
    1187           0 :     FD_LOG_ERR(( "too many skipped ticks %lu for slot %lu, chain must halt", msg->ticks_per_slot+msg->total_skipped_ticks, ctx->next_leader_slot ));
    1188           0 :   }
    1189             : 
    1190           0 :   fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_BECAME_LEADER, ctx->replay_out->chunk, sizeof(fd_became_leader_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
    1191           0 :   ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_became_leader_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
    1192             : 
    1193           0 :   ctx->next_leader_slot = ULONG_MAX;
    1194           0 :   ctx->next_leader_tickcount = LONG_MAX;
    1195             : 
    1196           0 :   return 1;
    1197           0 : }
    1198             : 
    1199             : static void
    1200             : process_poh_message( fd_replay_tile_t *                 ctx,
    1201           0 :                      fd_poh_leader_slot_ended_t const * slot_ended ) {
    1202             : 
    1203           0 :   FD_TEST( ctx->is_booted );
    1204           0 :   FD_TEST( ctx->is_leader );
    1205           0 :   FD_TEST( ctx->leader_bank!=NULL );
    1206             : 
    1207           0 :   FD_TEST( ctx->highwater_leader_slot>=slot_ended->slot );
    1208           0 :   FD_TEST( ctx->next_leader_slot>ctx->highwater_leader_slot );
    1209             : 
    1210             :   /* Update the poh hash in the bank.  We will want to maintain a refcnt
    1211             :      on the bank until we have recieved the block id for the block after
    1212             :      it has been shredded. */
    1213             : 
    1214           0 :   memcpy( fd_bank_poh_modify( ctx->leader_bank ), slot_ended->blockhash, sizeof(fd_hash_t) );
    1215             : 
    1216           0 :   ctx->recv_poh = 1;
    1217           0 : }
    1218             : 
    1219             : static void
    1220             : publish_reset( fd_replay_tile_t *  ctx,
    1221             :                fd_stem_context_t * stem,
    1222           0 :                fd_bank_t *         bank ) {
    1223           0 :   if( FD_UNLIKELY( ctx->replay_out->idx==ULONG_MAX ) ) return;
    1224             : 
    1225           0 :   fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( fd_bank_block_hash_queue_query( bank ) );
    1226           0 :   FD_TEST( block_hash );
    1227             : 
    1228           0 :   fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1229             : 
    1230           0 :   reset->bank_idx         = bank->idx;
    1231           0 :   reset->timestamp        = fd_log_wallclock();
    1232           0 :   reset->completed_slot   = fd_bank_slot_get( bank );
    1233           0 :   reset->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
    1234           0 :   reset->ticks_per_slot   = fd_bank_ticks_per_slot_get( bank );
    1235           0 :   reset->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)reset->ticks_per_slot);
    1236           0 :   fd_memcpy( reset->completed_blockhash, block_hash->uc, sizeof(fd_hash_t) );
    1237             : 
    1238           0 :   ulong ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
    1239           0 :   if( FD_UNLIKELY( reset->hashcnt_per_tick==1UL ) ) {
    1240             :     /* Low power producer, maximum of one microblock per tick in the slot */
    1241           0 :     reset->max_microblocks_in_slot = ticks_per_slot;
    1242           0 :   } else {
    1243             :     /* See the long comment in after_credit for this limit */
    1244           0 :     reset->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, ticks_per_slot*(reset->hashcnt_per_tick-1UL) );
    1245           0 :   }
    1246           0 :   reset->next_leader_slot = ctx->next_leader_slot;
    1247             : 
    1248           0 :   if( FD_LIKELY( ctx->rpc_enabled ) ) bank->refcnt++;
    1249             : 
    1250           0 :   fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_RESET, ctx->replay_out->chunk, sizeof(fd_poh_reset_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
    1251           0 :   ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_poh_reset_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
    1252           0 : }
    1253             : 
    1254             : static void
    1255             : boot_genesis( fd_replay_tile_t *  ctx,
    1256             :               fd_stem_context_t * stem,
    1257             :               ulong               in_idx,
    1258           0 :               ulong               chunk ) {
    1259             :   /* If we are bootstrapping, we can't wait to wait for our identity
    1260             :      vote to be rooted as this creates a circular dependency. */
    1261           0 :   ctx->has_identity_vote_rooted = 1;
    1262             : 
    1263           0 :   uchar const * lthash       = (uchar*)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    1264           0 :   uchar const * genesis_hash = (uchar*)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk )+sizeof(fd_lthash_value_t);
    1265             : 
    1266             :   // TODO: Do not pass the fd_types type between tiles, it have offsets
    1267             :   // that are unsafe and can't be validated as being in-bounds.  Need to
    1268             :   // pass an actual owned genesis type.
    1269           0 :   fd_genesis_solana_global_t const * genesis = fd_type_pun( (uchar*)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk )+sizeof(fd_hash_t)+sizeof(fd_lthash_value_t) );
    1270             : 
    1271           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
    1272           0 :   FD_TEST( bank );
    1273           0 :   fd_funk_txn_xid_t xid = { .ul = { 0UL, FD_REPLAY_BOOT_BANK_IDX } };
    1274             : 
    1275             :   /* Do genesis-related processing in a non-rooted transaction */
    1276           0 :   fd_funk_txn_xid_t root_xid; fd_funk_txn_xid_set_root( &root_xid );
    1277           0 :   fd_funk_txn_xid_t target_xid = { .ul = { 0UL, 0UL } };
    1278           0 :   fd_accdb_attach_child( ctx->accdb_admin, &root_xid, &target_xid );
    1279           0 :   fd_runtime_read_genesis( ctx->banks, bank, ctx->accdb, &xid, NULL, fd_type_pun_const( genesis_hash ), fd_type_pun_const( lthash ), genesis, &ctx->runtime_stack );
    1280           0 :   fd_accdb_advance_root( ctx->accdb_admin, &target_xid );
    1281             : 
    1282           0 :   static const fd_txncache_fork_id_t txncache_root = { .val = USHORT_MAX };
    1283           0 :   bank->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, txncache_root );
    1284             : 
    1285           0 :   fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( fd_bank_block_hash_queue_query( bank ) );
    1286           0 :   fd_txncache_finalize_fork( ctx->txncache, bank->txncache_fork_id, 0UL, block_hash->uc );
    1287             : 
    1288           0 :   publish_stake_weights( ctx, stem, bank, 0 );
    1289           0 :   publish_stake_weights( ctx, stem, bank, 1 );
    1290             : 
    1291             :   /* We call this after fd_runtime_read_genesis, which sets up the
    1292             :      slot_bank needed in blockstore_init. */
    1293           0 :   init_after_snapshot( ctx );
    1294             : 
    1295             :   /* Initialize store for genesis case, similar to snapshot case */
    1296           0 :   fd_hash_t genesis_block_id = { .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
    1297           0 :   fd_store_exacq( ctx->store );
    1298           0 :   if( FD_UNLIKELY( fd_store_root( ctx->store ) ) ) {
    1299           0 :     FD_LOG_CRIT(( "invariant violation: store root is not 0 for genesis" ));
    1300           0 :   }
    1301           0 :   fd_store_insert( ctx->store, 0, &genesis_block_id );
    1302           0 :   ctx->store->slot0 = 0UL; /* Genesis slot */
    1303           0 :   fd_store_exrel( ctx->store );
    1304             : 
    1305           0 :   ctx->published_root_slot = 0UL;
    1306           0 :   fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX, 0UL );
    1307             : 
    1308           0 :   fd_bank_block_height_set( bank, 1UL );
    1309             : 
    1310           0 :   ctx->consensus_root          = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
    1311           0 :   ctx->consensus_root_slot     = 0UL;
    1312           0 :   ctx->consensus_root_bank_idx = 0UL;
    1313           0 :   ctx->published_root_slot     = 0UL;
    1314           0 :   ctx->published_root_bank_idx = 0UL;
    1315             : 
    1316           0 :   ctx->reset_slot            = 0UL;
    1317           0 :   ctx->reset_bank            = bank;
    1318           0 :   ctx->reset_timestamp_nanos = fd_log_wallclock();
    1319           0 :   ctx->next_leader_slot      = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, 1UL, ctx->identity_pubkey );
    1320           0 :   if( FD_LIKELY( ctx->next_leader_slot != ULONG_MAX ) ) {
    1321           0 :     ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
    1322           0 :   } else {
    1323           0 :     ctx->next_leader_tickcount = LONG_MAX;
    1324           0 :   }
    1325             : 
    1326           0 :   ctx->is_booted = 1;
    1327           0 :   maybe_become_leader( ctx, stem );
    1328             : 
    1329           0 :   fd_hash_t initial_block_id = { .ul = { FD_RUNTIME_INITIAL_BLOCK_ID } };
    1330           0 :   fd_reasm_fec_t * fec       = fd_reasm_insert( ctx->reasm, &initial_block_id, NULL, 0 /* genesis slot */, 0, 0, 0, 0, 1, 0 ); /* FIXME manifest block_id */
    1331           0 :   fec->bank_idx              = 0UL;
    1332             : 
    1333             : 
    1334           0 :   fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
    1335           0 :   FD_TEST( block_id_ele );
    1336           0 :   block_id_ele->block_id = initial_block_id;
    1337           0 :   block_id_ele->slot     = 0UL;
    1338             : 
    1339           0 :   FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
    1340             : 
    1341           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1342           0 :   slot_info->identity_balance = get_identity_balance( ctx, xid );
    1343             : 
    1344           0 :   publish_slot_completed( ctx, stem, bank, 1, 0 /* is_leader */ );
    1345           0 :   publish_root_advanced( ctx, stem );
    1346           0 :   publish_reset( ctx, stem, bank );
    1347           0 : }
    1348             : 
    1349             : static inline void
    1350           0 : maybe_verify_cluster_type( fd_replay_tile_t * ctx ) {
    1351           0 :   if( FD_UNLIKELY( !ctx->is_booted || !ctx->has_genesis_hash ) ) {
    1352           0 :     return;
    1353           0 :   }
    1354             : 
    1355           0 :   FD_BASE58_ENCODE_32_BYTES( ctx->genesis_hash, hash_cstr );
    1356           0 :   ulong cluster = fd_genesis_cluster_identify( hash_cstr );
    1357             :   /* Map pyth-related clusters to unkwown. */
    1358           0 :   switch( cluster ) {
    1359           0 :     case FD_CLUSTER_PYTHNET:
    1360           0 :     case FD_CLUSTER_PYTHTEST:
    1361           0 :       cluster = FD_CLUSTER_UNKNOWN;
    1362           0 :   }
    1363             : 
    1364           0 :   if( cluster!=ctx->cluster_type ) {
    1365           0 :     FD_LOG_ERR(( "cluster type mismatch: (genesis) %s != (snapshot) %s", fd_genesis_cluster_name( cluster ), fd_genesis_cluster_name( ctx->cluster_type ) ));
    1366           0 :   }
    1367           0 : }
    1368             : 
    1369             : static void
    1370             : on_snapshot_message( fd_replay_tile_t *  ctx,
    1371             :                      fd_stem_context_t * stem,
    1372             :                      ulong               in_idx,
    1373             :                      ulong               chunk,
    1374           0 :                      ulong               sig ) {
    1375           0 :   ulong msg = fd_ssmsg_sig_message( sig );
    1376           0 :   if( FD_LIKELY( msg==FD_SSMSG_DONE ) ) {
    1377             :     /* An end of message notification indicates the snapshot is loaded.
    1378             :        Replay is able to start executing from this point onwards. */
    1379             :     /* TODO: replay should finish booting. Could make replay a
    1380             :        state machine and set the state here accordingly. */
    1381           0 :     ctx->is_booted = 1;
    1382             : 
    1383           0 :     fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
    1384           0 :     if( FD_UNLIKELY( !bank ) ) {
    1385           0 :       FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
    1386           0 :     }
    1387             : 
    1388           0 :     ulong snapshot_slot = fd_bank_slot_get( bank );
    1389             :     /* FIXME: This is a hack because the block id of the snapshot slot
    1390             :        is not provided in the snapshot.  A possible solution is to get
    1391             :        the block id of the snapshot slot from repair. */
    1392           0 :     fd_hash_t manifest_block_id = { .ul = { FD_RUNTIME_INITIAL_BLOCK_ID } };
    1393             : 
    1394           0 :     fd_store_exacq( ctx->store );
    1395           0 :     FD_TEST( !fd_store_root( ctx->store ) );
    1396           0 :     fd_store_insert( ctx->store, 0, &manifest_block_id );
    1397           0 :     ctx->store->slot0 = snapshot_slot; /* FIXME manifest_block_id */
    1398           0 :     fd_store_exrel( ctx->store );
    1399             : 
    1400             :     /* Typically, when we cross an epoch boundary during normal
    1401             :        operation, we publish the stake weights for the new epoch.  But
    1402             :        since we are starting from a snapshot, we need to publish two
    1403             :        epochs worth of stake weights: the previous epoch (which is
    1404             :        needed for voting on the current epoch), and the current epoch
    1405             :        (which is needed for voting on the next epoch). */
    1406           0 :     publish_stake_weights( ctx, stem, bank, 0 );
    1407           0 :     publish_stake_weights( ctx, stem, bank, 1 );
    1408             : 
    1409           0 :     ctx->consensus_root          = manifest_block_id;
    1410           0 :     ctx->consensus_root_slot     = snapshot_slot;
    1411           0 :     ctx->consensus_root_bank_idx = 0UL;
    1412           0 :     ctx->published_root_slot     = ctx->consensus_root_slot;
    1413           0 :     ctx->published_root_bank_idx = 0UL;
    1414             : 
    1415           0 :     ctx->reset_slot            = snapshot_slot;
    1416           0 :     ctx->reset_bank            = bank;
    1417           0 :     ctx->reset_timestamp_nanos = fd_log_wallclock();
    1418           0 :     ctx->next_leader_slot      = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, 1UL, ctx->identity_pubkey );
    1419           0 :     if( FD_LIKELY( ctx->next_leader_slot != ULONG_MAX ) ) {
    1420           0 :       ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
    1421           0 :     } else {
    1422           0 :       ctx->next_leader_tickcount = LONG_MAX;
    1423           0 :     }
    1424             : 
    1425           0 :     fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX, snapshot_slot );
    1426           0 :     FD_TEST( bank->idx==0UL );
    1427             : 
    1428           0 :     fd_funk_txn_xid_t xid = { .ul = { snapshot_slot, FD_REPLAY_BOOT_BANK_IDX } };
    1429             : 
    1430           0 :     fd_funk_t * funk = fd_accdb_user_v1_funk( ctx->accdb );
    1431           0 :     fd_features_restore( bank, funk, &xid );
    1432             : 
    1433           0 :     fd_runtime_update_leaders( bank, &ctx->runtime_stack );
    1434             : 
    1435           0 :     fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
    1436           0 :     FD_TEST( block_id_ele );
    1437           0 :     block_id_ele->block_id = manifest_block_id;
    1438           0 :     block_id_ele->slot     = snapshot_slot;
    1439           0 :     FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
    1440             : 
    1441             :     /* We call this after fd_runtime_read_genesis, which sets up the
    1442             :        slot_bank needed in blockstore_init. */
    1443           0 :     init_after_snapshot( ctx );
    1444             : 
    1445           0 :     fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1446           0 :     slot_info->identity_balance = get_identity_balance( ctx, xid );
    1447             : 
    1448           0 :     publish_slot_completed( ctx, stem, bank, 1, 0 /* is_leader */ );
    1449           0 :     publish_root_advanced( ctx, stem );
    1450             : 
    1451           0 :     fd_reasm_fec_t * fec = fd_reasm_insert( ctx->reasm, &manifest_block_id, NULL, snapshot_slot, 0, 0, 0, 0, 1, 0 ); /* FIXME manifest block_id */
    1452           0 :     fec->bank_idx        = 0UL;
    1453             : 
    1454           0 :     ctx->cluster_type = fd_bank_cluster_type_get( bank );
    1455             : 
    1456           0 :     maybe_verify_cluster_type( ctx );
    1457             : 
    1458           0 :     return;
    1459           0 :   }
    1460             : 
    1461           0 :   switch( msg ) {
    1462           0 :     case FD_SSMSG_MANIFEST_FULL:
    1463           0 :     case FD_SSMSG_MANIFEST_INCREMENTAL: {
    1464             :       /* We may either receive a full snapshot manifest or an
    1465             :          incremental snapshot manifest.  Note that this external message
    1466             :          id is only used temporarily because replay cannot yet receive
    1467             :          the firedancer-internal snapshot manifest message. */
    1468           0 :       if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
    1469           0 :         FD_LOG_ERR(( "chunk %lu from in %d corrupt, not in range [%lu,%lu]", chunk, ctx->in_kind[ in_idx ], ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
    1470             : 
    1471           0 :       fd_ssload_recover( fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ),
    1472           0 :                          ctx->banks,
    1473           0 :                          fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX ),
    1474           0 :                          ctx->runtime_stack.stakes.vote_credits );
    1475             : 
    1476           0 :       fd_snapshot_manifest_t const * manifest = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    1477           0 :       ctx->hard_forks_cnt = manifest->hard_forks_len;
    1478           0 :       for( ulong i=0UL; i<manifest->hard_forks_len; i++ ) ctx->hard_forks[ i ] = manifest->hard_forks[ i ];
    1479           0 :       break;
    1480           0 :     }
    1481           0 :     default: {
    1482           0 :       FD_LOG_ERR(( "Received unknown snapshot message with msg %lu", msg ));
    1483           0 :       return;
    1484           0 :     }
    1485           0 :   }
    1486             : 
    1487           0 :   return;
    1488           0 : }
    1489             : 
    1490             : static void
    1491             : dispatch_task( fd_replay_tile_t *  ctx,
    1492             :                fd_stem_context_t * stem,
    1493           0 :                fd_sched_task_t *   task ) {
    1494             : 
    1495           0 :   switch( task->task_type ) {
    1496           0 :     case FD_SCHED_TT_TXN_EXEC: {
    1497           0 :       fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_exec->txn_idx );
    1498             : 
    1499             :       /* FIXME: this should be done during txn parsing so that we don't
    1500             :          have to loop over all accounts a second time. */
    1501             :       /* Insert or reverify invoked programs for this epoch, if needed. */
    1502           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->txn_exec->bank_idx );
    1503             : 
    1504           0 : #     if FD_HAS_FLATCC
    1505             :       /* Add the transaction to the block dumper if necessary. This
    1506             :          logic doesn't need to be fork-aware since it's only meant to
    1507             :          be used in backtest. */
    1508           0 :       if( FD_UNLIKELY( ctx->capture_ctx && ctx->capture_ctx->dump_block_to_pb ) ) {
    1509           0 :         fd_dump_block_to_protobuf_collect_tx( ctx->block_dump_ctx, txn_p );
    1510           0 :       }
    1511           0 : #     endif
    1512             : 
    1513           0 :       bank->refcnt++;
    1514             : 
    1515           0 :       if( FD_UNLIKELY( !bank->first_transaction_scheduled_nanos ) ) bank->first_transaction_scheduled_nanos = fd_log_wallclock();
    1516             : 
    1517           0 :       fd_replay_out_link_t *   exec_out = ctx->exec_out;
    1518           0 :       fd_exec_txn_exec_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
    1519           0 :       memcpy( &exec_msg->txn, txn_p, sizeof(fd_txn_p_t) );
    1520           0 :       exec_msg->bank_idx = task->txn_exec->bank_idx;
    1521           0 :       exec_msg->txn_idx  = task->txn_exec->txn_idx;
    1522           0 :       fd_stem_publish( stem, exec_out->idx, (FD_EXEC_TT_TXN_EXEC<<32) | task->txn_exec->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
    1523           0 :       exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
    1524           0 :       break;
    1525           0 :     }
    1526           0 :     case FD_SCHED_TT_TXN_SIGVERIFY: {
    1527           0 :       fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_sigverify->txn_idx );
    1528             : 
    1529           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->txn_sigverify->bank_idx );
    1530           0 :       bank->refcnt++;
    1531             : 
    1532           0 :       fd_replay_out_link_t *        exec_out = ctx->exec_out;
    1533           0 :       fd_exec_txn_sigverify_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
    1534           0 :       memcpy( &exec_msg->txn, txn_p, sizeof(fd_txn_p_t) );
    1535           0 :       exec_msg->bank_idx = task->txn_sigverify->bank_idx;
    1536           0 :       exec_msg->txn_idx  = task->txn_sigverify->txn_idx;
    1537           0 :       fd_stem_publish( stem, exec_out->idx, (FD_EXEC_TT_TXN_SIGVERIFY<<32) | task->txn_sigverify->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, 0UL );
    1538           0 :       exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
    1539           0 :       break;
    1540           0 :     };
    1541           0 :     default: {
    1542           0 :       FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
    1543           0 :     }
    1544           0 :   }
    1545           0 : }
    1546             : 
    1547             : /* Returns 1 if charge_busy. */
    1548             : static int
    1549             : replay( fd_replay_tile_t *  ctx,
    1550           0 :         fd_stem_context_t * stem ) {
    1551             : 
    1552           0 :   if( FD_UNLIKELY( !ctx->is_booted ) ) return 0;
    1553             : 
    1554           0 :   int charge_busy = 0;
    1555           0 :   fd_sched_task_t task[ 1 ];
    1556           0 :   if( FD_UNLIKELY( !fd_sched_task_next_ready( ctx->sched, task ) ) ) {
    1557           0 :     return charge_busy; /* Nothing to execute or do. */
    1558           0 :   }
    1559             : 
    1560           0 :   charge_busy = 1;
    1561             : 
    1562           0 :   switch( task->task_type ) {
    1563           0 :     case FD_SCHED_TT_BLOCK_START: {
    1564           0 :       replay_block_start( ctx, stem, task->block_start->bank_idx, task->block_start->parent_bank_idx, task->block_start->slot );
    1565           0 :       fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_START, ULONG_MAX, ULONG_MAX );
    1566           0 :       break;
    1567           0 :     }
    1568           0 :     case FD_SCHED_TT_BLOCK_END: {
    1569           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->block_end->bank_idx );
    1570           0 :       if( FD_LIKELY( !(bank->flags&FD_BANK_FLAGS_DEAD) ) ) replay_block_finalize( ctx, stem, bank );
    1571           0 :       fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_END, ULONG_MAX, ULONG_MAX );
    1572           0 :       break;
    1573           0 :     }
    1574           0 :     case FD_SCHED_TT_TXN_EXEC:
    1575           0 :     case FD_SCHED_TT_TXN_SIGVERIFY: {
    1576             :       /* Likely/common case: we have a transaction we actually need to
    1577             :          execute. */
    1578           0 :       dispatch_task( ctx, stem, task );
    1579           0 :       break;
    1580           0 :     }
    1581           0 :     default: {
    1582           0 :       FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
    1583           0 :     }
    1584           0 :   }
    1585             : 
    1586           0 :   return charge_busy;
    1587           0 : }
    1588             : 
    1589             : static int
    1590           0 : can_process_fec( fd_replay_tile_t * ctx ) {
    1591           0 :   fd_reasm_fec_t * fec;
    1592           0 :   if( FD_UNLIKELY( !fd_sched_can_ingest( ctx->sched, 1UL ) ) ) return 0;
    1593           0 :   if( FD_UNLIKELY( (fec = fd_reasm_peek( ctx->reasm ))==NULL ) ) return 0;
    1594             : 
    1595           0 :   if( FD_UNLIKELY( ctx->is_leader && fec->fec_set_idx==0U && fd_reasm_parent( ctx->reasm, fec )->bank_idx==ctx->leader_bank->idx ) ) {
    1596             :     /* There's a race that's exceedingly rare, where we receive the
    1597             :        FEC set for the slot right after our leader rotation before we
    1598             :        freeze the bank for the last slot in our leader rotation.
    1599             :        Leader slot freezing happens only after if we've received the
    1600             :        final PoH hash from the poh tile as well as the final FEC set
    1601             :        for the leader slot.  So the race happens when FEC sets are
    1602             :        delivered and processed sooner than the PoH hash, aka when the
    1603             :        poh=>shred=>replay path for the block id somehow beats the
    1604             :        poh=>replay path for the poh hash.  To mitigate this race,
    1605             :        we must block on ingesting the FEC set for the ensuing slot
    1606             :        before the leader bank freezes, because that would violate
    1607             :        ordering invariants in banks and sched. */
    1608           0 :     FD_TEST( ctx->recv_block_id );
    1609           0 :     FD_TEST( !ctx->recv_poh );
    1610           0 :     return 0;
    1611           0 :   }
    1612             : 
    1613             :   /* If fec_set_idx is 0, we need a new bank for a new slot.  Banks must
    1614             :      not be full in this case. */
    1615           0 :   if( FD_UNLIKELY( fd_banks_is_full( ctx->banks ) && fec->fec_set_idx==0 ) ) return 0;
    1616             : 
    1617             :   /* Otherwise, banks may not be full, so we can always create a new
    1618             :      bank if needed.  Or, if banks are full, the current fec set's
    1619             :      ancestor (idx 0) already created a bank for this slot.*/
    1620           0 :   return 1;
    1621           0 : }
    1622             : 
    1623             : static void
    1624             : process_fec_set( fd_replay_tile_t *  ctx,
    1625             :                  fd_stem_context_t * stem,
    1626           0 :                  fd_reasm_fec_t *    reasm_fec ) {
    1627           0 :   long now = fd_log_wallclock();
    1628             : 
    1629             :   /* Linking only requires a shared lock because the fields that are
    1630             :      modified are only read on publish which uses exclusive lock. */
    1631             : 
    1632           0 :   long shacq_start, shacq_end, shrel_end;
    1633             : 
    1634           0 :   FD_STORE_SHARED_LOCK( ctx->store, shacq_start, shacq_end, shrel_end ) {
    1635           0 :     if( FD_UNLIKELY( !fd_store_link( ctx->store, &reasm_fec->key, &reasm_fec->cmr ) ) ) FD_LOG_WARNING(( "failed to link %s %s. slot %lu fec_set_idx %u", FD_BASE58_ENC_32_ALLOCA( &reasm_fec->key ), FD_BASE58_ENC_32_ALLOCA( &reasm_fec->cmr ), reasm_fec->slot, reasm_fec->fec_set_idx ));
    1636           0 :   } FD_STORE_SHARED_LOCK_END;
    1637           0 :   fd_histf_sample( ctx->metrics.store_link_wait, (ulong)fd_long_max( shacq_end - shacq_start, 0L ) );
    1638           0 :   fd_histf_sample( ctx->metrics.store_link_work, (ulong)fd_long_max( shrel_end - shacq_end,   0L ) );
    1639             : 
    1640             :   /* Update the reasm_fec with the correct bank index and parent bank
    1641             :      index.  If the FEC belongs to a leader, we have already allocated
    1642             :      a bank index for the FEC and it just needs to be propagated to the
    1643             :      reasm_fec. */
    1644             : 
    1645           0 :   reasm_fec->parent_bank_idx = fd_reasm_parent( ctx->reasm, reasm_fec )->bank_idx;
    1646             : 
    1647           0 :   if( FD_UNLIKELY( reasm_fec->leader ) ) {
    1648             :     /* If we are the leader we just need to copy in the bank index that
    1649             :        the leader slot is using. */
    1650           0 :     FD_TEST( ctx->leader_bank!=NULL );
    1651           0 :     reasm_fec->bank_idx = ctx->leader_bank->idx;
    1652           0 :   } else if( FD_UNLIKELY( reasm_fec->fec_set_idx==0U ) ) {
    1653             :     /* If we are seeing a FEC with fec set idx 0, this means that we are
    1654             :        starting a new slot, and we need a new bank index. */
    1655           0 :     reasm_fec->bank_idx = fd_banks_new_bank( ctx->banks, reasm_fec->parent_bank_idx, now )->idx;
    1656           0 :   } else {
    1657             :     /* We are continuing to execute through a slot that we already have
    1658             :        a bank index for. */
    1659           0 :     reasm_fec->bank_idx = reasm_fec->parent_bank_idx;
    1660           0 :   }
    1661             : 
    1662           0 :   if( FD_UNLIKELY( reasm_fec->slot_complete ) ) {
    1663             :     /* Once the block id for a block is known it must be added to the
    1664             :        leader block mapping. */
    1665           0 :     fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ reasm_fec->bank_idx ];
    1666           0 :     FD_TEST( block_id_ele );
    1667             : 
    1668             :     /* If an entry already exists for this bank index in the block id
    1669             :        map, we can safely remove it and replace it with the new entry.
    1670             :        This is safe because we know that the old entry for this fork
    1671             :        index has already been pruned away. */
    1672           0 :     if( FD_LIKELY( block_id_ele->slot!=FD_SLOT_NULL && fd_block_id_map_ele_query( ctx->block_id_map, &block_id_ele->block_id, NULL, ctx->block_id_arr ) ) ) {
    1673           0 :       FD_TEST( fd_block_id_map_ele_remove( ctx->block_id_map, &block_id_ele->block_id, NULL, ctx->block_id_arr ) );
    1674           0 :     }
    1675             : 
    1676           0 :     block_id_ele->block_id = reasm_fec->key;
    1677           0 :     block_id_ele->slot     = reasm_fec->slot;
    1678             : 
    1679           0 :     FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
    1680             : 
    1681           0 :     if( FD_UNLIKELY( reasm_fec->leader ) ) {
    1682           0 :       ctx->recv_block_id = 1;
    1683           0 :     }
    1684           0 :   }
    1685             : 
    1686           0 :   if( FD_UNLIKELY( reasm_fec->leader ) ) {
    1687           0 :     return;
    1688           0 :   }
    1689             : 
    1690             :   /* Forks form a partial ordering over FEC sets. The Repair tile
    1691             :      delivers FEC sets in-order per fork, but FEC set ordering across
    1692             :      forks is arbitrary */
    1693           0 :   fd_sched_fec_t sched_fec[ 1 ];
    1694             : 
    1695             : # if DEBUG_LOGGING
    1696             :   FD_LOG_INFO(( "replay processing FEC set for slot %lu fec_set_idx %u, mr %s cmr %s", reasm_fec->slot, reasm_fec->fec_set_idx, FD_BASE58_ENC_32_ALLOCA( &reasm_fec->key ), FD_BASE58_ENC_32_ALLOCA( &reasm_fec->cmr ) ));
    1697             : # endif
    1698             : 
    1699             :   /* Read FEC set from the store.  This should happen before we try to
    1700             :      ingest the FEC set.  This allows us to filter out frags that were
    1701             :      in-flight when we published away minority forks that the frags land
    1702             :      on.  These frags would have no bank to execute against, because
    1703             :      their corresponding banks, or parent banks, have also been pruned
    1704             :      during publishing.  A query against store will rightfully tell us
    1705             :      that the underlying data is not found, implying that this is for a
    1706             :      minority fork that we can safely ignore. */
    1707           0 :   FD_STORE_SHARED_LOCK( ctx->store, shacq_start, shacq_end, shrel_end ) {
    1708           0 :     fd_store_fec_t * store_fec = fd_store_query( ctx->store, &reasm_fec->key );
    1709           0 :     if( FD_UNLIKELY( !store_fec ) ) {
    1710             :       /* The only case in which a FEC is not found in the store after
    1711             :          repair has notified is if the FEC was on a minority fork that
    1712             :          has already been published away.  In this case we abandon the
    1713             :          entire slice because it is no longer relevant.  */
    1714           0 :       FD_LOG_WARNING(( "store fec for slot: %lu is on minority fork already pruned by publish. abandoning slice. root: %lu. pruned merkle: %s", reasm_fec->slot, ctx->consensus_root_slot, FD_BASE58_ENC_32_ALLOCA( &reasm_fec->key ) ));
    1715           0 :       return;
    1716           0 :     }
    1717           0 :     FD_TEST( store_fec );
    1718           0 :     sched_fec->fec       = store_fec;
    1719           0 :     sched_fec->shred_cnt = reasm_fec->data_cnt;
    1720           0 :   } FD_STORE_SHARED_LOCK_END;
    1721             : 
    1722           0 :   fd_histf_sample( ctx->metrics.store_read_wait, (ulong)fd_long_max( shacq_end - shacq_start, 0UL ) );
    1723           0 :   fd_histf_sample( ctx->metrics.store_read_work, (ulong)fd_long_max( shrel_end - shacq_end,   0UL ) );
    1724             : 
    1725           0 :   sched_fec->is_last_in_batch       = !!reasm_fec->data_complete;
    1726           0 :   sched_fec->is_last_in_block       = !!reasm_fec->slot_complete;
    1727           0 :   sched_fec->bank_idx               = reasm_fec->bank_idx;
    1728           0 :   sched_fec->parent_bank_idx        = reasm_fec->parent_bank_idx;
    1729           0 :   sched_fec->slot                   = reasm_fec->slot;
    1730           0 :   sched_fec->parent_slot            = reasm_fec->slot - reasm_fec->parent_off;
    1731           0 :   sched_fec->is_first_in_block      = reasm_fec->fec_set_idx==0U;
    1732           0 :   fd_funk_txn_xid_copy( sched_fec->alut_ctx->xid, fd_funk_last_publish( ctx->accdb_admin->funk ) );
    1733           0 :   sched_fec->alut_ctx->accdb[0]     = ctx->accdb[0];
    1734           0 :   sched_fec->alut_ctx->els          = ctx->published_root_slot;
    1735             : 
    1736           0 :   if( FD_UNLIKELY( !fd_sched_fec_ingest( ctx->sched, sched_fec ) ) ) {
    1737           0 :     fd_bank_t * bank = fd_banks_bank_query( ctx->banks, sched_fec->bank_idx );
    1738           0 :     publish_slot_dead( ctx, stem, bank );
    1739           0 :     fd_banks_mark_bank_dead( ctx->banks, bank );
    1740           0 :   }
    1741           0 : }
    1742             : 
    1743             : static void
    1744             : funk_publish( fd_replay_tile_t * ctx,
    1745             :               ulong              slot,
    1746           0 :               ulong              bank_idx ) {
    1747           0 :   fd_funk_txn_xid_t xid = { .ul[0] = slot, .ul[1] = bank_idx };
    1748           0 :   FD_LOG_DEBUG(( "publishing slot=%lu", slot ));
    1749             : 
    1750             :   /* This is the standard case.  Publish all transactions up to and
    1751             :      including the watermark.  This will publish any in-prep ancestors
    1752             :      of root_txn as well. */
    1753           0 :   fd_accdb_advance_root( ctx->accdb_admin, &xid );
    1754           0 :   fd_progcache_txn_advance_root( ctx->progcache_admin, &xid );
    1755           0 : }
    1756             : 
    1757             : static int
    1758           0 : advance_published_root( fd_replay_tile_t * ctx ) {
    1759             : 
    1760           0 :   fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &ctx->consensus_root, NULL, ctx->block_id_arr );
    1761           0 :   if( FD_UNLIKELY( !block_id_ele ) ) {
    1762           0 :     FD_LOG_CRIT(( "invariant violation: block id ele not found for consensus root %s", FD_BASE58_ENC_32_ALLOCA( &ctx->consensus_root ) ));
    1763           0 :   }
    1764           0 :   ulong target_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
    1765             : 
    1766           0 :   fd_sched_root_notify( ctx->sched, target_bank_idx );
    1767             : 
    1768             :   /* If the identity vote has been seen on a bank that should be rooted,
    1769             :      then we are now ready to produce blocks. */
    1770           0 :   if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
    1771           0 :     fd_bank_t * root_bank = fd_banks_bank_query( ctx->banks, target_bank_idx );
    1772           0 :     if( FD_UNLIKELY( !root_bank ) ) FD_LOG_CRIT(( "invariant violation: root bank not found for bank index %lu", target_bank_idx ));
    1773           0 :     if( FD_LIKELY( fd_bank_has_identity_vote_get( root_bank ) ) ) ctx->has_identity_vote_rooted = 1;
    1774           0 :   }
    1775             : 
    1776           0 :   ulong advanceable_root_idx = ULONG_MAX;
    1777           0 :   if( FD_UNLIKELY( !fd_banks_advance_root_prepare( ctx->banks, target_bank_idx, &advanceable_root_idx ) ) ) return 0;
    1778             : 
    1779           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, advanceable_root_idx );
    1780           0 :   FD_TEST( bank );
    1781             : 
    1782           0 :   fd_block_id_ele_t * advanceable_root_ele = &ctx->block_id_arr[ advanceable_root_idx ];
    1783           0 :   if( FD_UNLIKELY( !advanceable_root_ele ) ) {
    1784           0 :     FD_LOG_CRIT(( "invariant violation: advanceable root ele not found for bank index %lu", advanceable_root_idx ));
    1785           0 :   }
    1786             : 
    1787           0 :   long exacq_start, exacq_end, exrel_end;
    1788           0 :   FD_STORE_EXCLUSIVE_LOCK( ctx->store, exacq_start, exacq_end, exrel_end ) {
    1789           0 :     fd_store_publish( ctx->store, &advanceable_root_ele->block_id );
    1790           0 :   } FD_STORE_EXCLUSIVE_LOCK_END;
    1791             : 
    1792           0 :   fd_histf_sample( ctx->metrics.store_publish_wait, (ulong)fd_long_max( exacq_end-exacq_start, 0UL ) );
    1793           0 :   fd_histf_sample( ctx->metrics.store_publish_work, (ulong)fd_long_max( exrel_end-exacq_end,   0UL ) );
    1794             : 
    1795           0 :   ulong advanceable_root_slot = fd_bank_slot_get( bank );
    1796           0 :   funk_publish( ctx, advanceable_root_slot, bank->idx );
    1797             : 
    1798           0 :   fd_txncache_advance_root( ctx->txncache, bank->txncache_fork_id );
    1799           0 :   fd_sched_advance_root( ctx->sched, advanceable_root_idx );
    1800           0 :   fd_banks_advance_root( ctx->banks, advanceable_root_idx );
    1801           0 :   fd_reasm_publish( ctx->reasm, &advanceable_root_ele->block_id );
    1802             : 
    1803           0 :   ctx->published_root_slot     = advanceable_root_slot;
    1804           0 :   ctx->published_root_bank_idx = advanceable_root_idx;
    1805             : 
    1806           0 :   return 1;
    1807           0 : }
    1808             : 
    1809             : static void
    1810             : after_credit( fd_replay_tile_t *  ctx,
    1811             :               fd_stem_context_t * stem,
    1812             :               int *               opt_poll_in,
    1813           0 :               int *               charge_busy ) {
    1814           0 :   if( FD_UNLIKELY( !ctx->is_booted ) ) return;
    1815             : 
    1816           0 :   if( FD_UNLIKELY( maybe_become_leader( ctx, stem ) ) ) {
    1817           0 :     *charge_busy = 1;
    1818           0 :     *opt_poll_in = 0;
    1819           0 :     return;
    1820           0 :   }
    1821             : 
    1822             :   /* If we are leader, we can only unbecome the leader iff we have
    1823             :      received the poh hash from the poh tile and block id from reasm. */
    1824           0 :   if( FD_UNLIKELY( ctx->is_leader && ctx->recv_block_id && ctx->recv_poh ) ) {
    1825           0 :     fini_leader_bank( ctx, stem );
    1826           0 :     *charge_busy = 1;
    1827           0 :     *opt_poll_in = 0;
    1828           0 :     return;
    1829           0 :   }
    1830             : 
    1831             :   /* If the published_root is not caught up to the consensus root, then
    1832             :      we should try to advance the published root. */
    1833           0 :   if( FD_UNLIKELY( ctx->consensus_root_bank_idx!=ctx->published_root_bank_idx && advance_published_root( ctx ) ) ) {
    1834           0 :     *charge_busy = 1;
    1835           0 :     *opt_poll_in = 0;
    1836           0 :     return;
    1837           0 :   }
    1838             : 
    1839             :   /* If the reassembler has a fec that is ready, we should process it
    1840             :      and pass it to the scheduler. */
    1841             : 
    1842             :   /* FIXME: The reasm logic needs to get reworked to support
    1843             :      equivocation more robustly. */
    1844           0 :   if( FD_LIKELY( can_process_fec( ctx ) ) ) {
    1845           0 :     fd_reasm_fec_t * fec = fd_reasm_peek( ctx->reasm );
    1846             : 
    1847             :     /* If fec->eqvoc is set that means that equivocation mid-block was
    1848             :        detected in fd_reasm_t.  We need to replay up to and including
    1849             :        the equivocating FEC on a new bank. */
    1850             : 
    1851           0 :     if( FD_UNLIKELY( fec->eqvoc ) ) {
    1852           0 :       FD_LOG_WARNING(( "Block equivocation detected at slot %lu", fec->slot ));
    1853             : 
    1854             :       /* We need to figure out which and how many FECs we need to
    1855             :          (re)insert into the scheduler.  We work backwards from the
    1856             :          equivocating FEC, querying for chained merkle roots until we
    1857             :          reach the first FEC in the slot.
    1858             :          TODO: replace the magic number with a constant for the max
    1859             :                number of fecs possible in a slot with fix-32. */
    1860           0 :       fd_reasm_fec_t * fecs[ 1024 ] = { [0] = fec };
    1861           0 :       ulong            fec_cnt      = 1UL;
    1862           0 :       while( fecs[ fec_cnt-1UL ]->fec_set_idx!=0UL ) {
    1863           0 :         fec = fd_reasm_query( ctx->reasm, &fecs[ fec_cnt-1UL ]->cmr );
    1864           0 :         fecs[ fec_cnt++ ] = fec;
    1865           0 :       }
    1866             : 
    1867             :       /* If we don't have enough space in the scheduler to ingest all of
    1868             :          FECs, we can't proceed yet. */
    1869           0 :       if( FD_UNLIKELY( !fd_sched_can_ingest( ctx->sched, fec_cnt ) ) ) return;
    1870             : 
    1871             :       /* Now that we have validated that sched can ingest all of the
    1872             :          required FECs, it is finally safe to remove the equivocating
    1873             :          fec from the reasm deque. */
    1874           0 :       fd_reasm_out( ctx->reasm );
    1875             : 
    1876             :       /* Now we can process all of the FECs. */
    1877           0 :       for( ulong i=fec_cnt; i>0UL; i-- ) {
    1878           0 :         process_fec_set( ctx, stem, fecs[i-1UL] );
    1879           0 :       }
    1880           0 :     } else {
    1881             :       /* Standard case. */
    1882           0 :       fec = fd_reasm_out( ctx->reasm );
    1883           0 :       process_fec_set( ctx, stem, fec );
    1884           0 :     }
    1885             : 
    1886           0 :     *charge_busy = 1;
    1887           0 :     *opt_poll_in = 0;
    1888           0 :     return;
    1889           0 :   }
    1890             : 
    1891           0 :   *charge_busy = replay( ctx, stem );
    1892           0 :   *opt_poll_in = !*charge_busy;
    1893           0 : }
    1894             : 
    1895             : static int
    1896             : before_frag( fd_replay_tile_t * ctx,
    1897             :              ulong              in_idx,
    1898             :              ulong              seq FD_PARAM_UNUSED,
    1899           0 :              ulong              sig FD_PARAM_UNUSED ) {
    1900             : 
    1901           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_SHRED ) ) {
    1902             :     /* If reasm is full, we can not insert any more FEC sets.  We must
    1903             :        not consume any frags from shred_out until reasm can process more
    1904             :        FEC sets. */
    1905             : 
    1906           0 :     if( FD_UNLIKELY( !fd_reasm_free( ctx->reasm ) ) ) {
    1907           0 :       return -1;
    1908           0 :     }
    1909           0 :   }
    1910             : 
    1911           0 :   return 0;
    1912           0 : }
    1913             : 
    1914             : static void
    1915             : process_solcap_account_update( fd_replay_tile_t *                          ctx,
    1916           0 :                                fd_capture_ctx_account_update_msg_t const * msg ) {
    1917             : 
    1918           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
    1919           0 :   if( FD_UNLIKELY( !bank ) ) {
    1920           0 :     FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", msg->bank_idx ));
    1921           0 :   }
    1922             : 
    1923           0 :   if( FD_UNLIKELY( !ctx->capture_ctx || !ctx->capture_ctx->capture ) ) return;
    1924           0 :   if( FD_UNLIKELY( fd_bank_slot_get( bank )<ctx->capture_ctx->solcap_start_slot ) ) return;
    1925             : 
    1926           0 :   uchar const * account_data = (uchar const *)fd_type_pun_const( msg )+sizeof(fd_capture_ctx_account_update_msg_t);
    1927           0 :   fd_solcap_write_account( ctx->capture_ctx->capture, &msg->pubkey, &msg->info, account_data, msg->data_sz );
    1928           0 : }
    1929             : 
    1930             : static void
    1931             : process_exec_task_done( fd_replay_tile_t *        ctx,
    1932             :                         fd_stem_context_t *       stem,
    1933             :                         fd_exec_task_done_msg_t * msg,
    1934           0 :                         ulong                     sig ) {
    1935           0 :   if( FD_UNLIKELY( sig==0UL ) ) {
    1936             :     // FIXME remove this branch with new solcap
    1937           0 :     process_solcap_account_update( ctx, fd_type_pun( msg ) );
    1938           0 :     return;
    1939           0 :   }
    1940             : 
    1941           0 :   ulong exec_tile_idx = sig&0xFFFFFFFFUL;
    1942             : 
    1943           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
    1944           0 :   bank->refcnt--;
    1945             : 
    1946           0 :   switch( sig>>32 ) {
    1947           0 :     case FD_EXEC_TT_TXN_EXEC: {
    1948           0 :       if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
    1949             :         /* Query the txn signature against our recently generated vote
    1950             :            txn signatures.  If the query is successful, then we have
    1951             :            seen our own vote transaction land and this should be marked
    1952             :            in the bank.  We go through this exercise until we've seen
    1953             :            our vote rooted. */
    1954           0 :         fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, msg->txn_exec->txn_idx );
    1955           0 :         if( fd_vote_tracker_query_sig( ctx->vote_tracker, fd_type_pun_const( txn_p->payload+TXN( txn_p )->signature_off ) ) ) {
    1956           0 :           *fd_bank_has_identity_vote_modify( bank ) += 1;
    1957           0 :         }
    1958           0 :       }
    1959           0 :       if( FD_UNLIKELY( msg->txn_exec->err && !(bank->flags&FD_BANK_FLAGS_DEAD) ) ) {
    1960             :         /* Every transaction in a valid block has to execute.
    1961             :            Otherwise, we should mark the block as dead.  Also freeze the
    1962             :            bank if possible. */
    1963           0 :         publish_slot_dead( ctx, stem, bank );
    1964           0 :         fd_banks_mark_bank_dead( ctx->banks, bank );
    1965           0 :         fd_sched_block_abandon( ctx->sched, bank->idx );
    1966           0 :       }
    1967           0 :       if( FD_UNLIKELY( (bank->flags&FD_BANK_FLAGS_DEAD) && bank->refcnt==0UL ) ) {
    1968           0 :         fd_banks_mark_bank_frozen( ctx->banks, bank );
    1969           0 :       }
    1970           0 :       fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_EXEC, msg->txn_exec->txn_idx, exec_tile_idx );
    1971           0 :       break;
    1972           0 :     }
    1973           0 :     case FD_EXEC_TT_TXN_SIGVERIFY: {
    1974           0 :       if( FD_UNLIKELY( msg->txn_sigverify->err && !(bank->flags&FD_BANK_FLAGS_DEAD) ) ) {
    1975             :         /* Every transaction in a valid block has to sigverify.
    1976             :            Otherwise, we should mark the block as dead.  Also freeze the
    1977             :            bank if possible. */
    1978           0 :         publish_slot_dead( ctx, stem, bank );
    1979           0 :         fd_banks_mark_bank_dead( ctx->banks, bank );
    1980           0 :         fd_sched_block_abandon( ctx->sched, bank->idx );
    1981           0 :       }
    1982           0 :       if( FD_UNLIKELY( (bank->flags&FD_BANK_FLAGS_DEAD) && bank->refcnt==0UL ) ) {
    1983           0 :         fd_banks_mark_bank_frozen( ctx->banks, bank );
    1984           0 :       }
    1985           0 :       fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_SIGVERIFY, msg->txn_sigverify->txn_idx, exec_tile_idx );
    1986           0 :       break;
    1987           0 :     }
    1988           0 :     default: FD_LOG_CRIT(( "unexpected sig 0x%lx", sig ));
    1989           0 :   }
    1990             : 
    1991             :   /* Reference counter just decreased, and an exec tile just got freed
    1992             :      up.  If there's a need to be more aggressively pruning, we could
    1993             :      check here if more slots just became publishable and publish.  Not
    1994             :      publishing here shouldn't bloat the fork tree too much though.  We
    1995             :      mark minority forks dead as soon as we can, and execution dispatch
    1996             :      stops on dead blocks.  So shortly afterwards, dead blocks should be
    1997             :      eligible for pruning as in-flight transactions retire from the
    1998             :      execution pipeline. */
    1999             : 
    2000           0 : }
    2001             : 
    2002             : static void
    2003             : process_tower_slot_done( fd_replay_tile_t *           ctx,
    2004             :                          fd_stem_context_t *          stem,
    2005           0 :                          fd_tower_slot_done_t const * msg ) {
    2006           0 :   fd_bank_t * replay_bank = fd_banks_bank_query( ctx->banks, msg->replay_bank_idx );
    2007           0 :   if( FD_UNLIKELY( !replay_bank ) ) FD_LOG_CRIT(( "invariant violation: bank not found for bank index %lu", msg->replay_bank_idx ));
    2008           0 :   replay_bank->refcnt--;
    2009             : 
    2010           0 :   ctx->reset_block_id = msg->reset_block_id;
    2011           0 :   ctx->reset_slot     = msg->reset_slot;
    2012           0 :   ctx->reset_timestamp_nanos = fd_log_wallclock();
    2013           0 :   ulong min_leader_slot = fd_ulong_max( msg->reset_slot+1UL, fd_ulong_if( ctx->highwater_leader_slot==ULONG_MAX, 0UL, ctx->highwater_leader_slot+1UL ) );
    2014           0 :   ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, min_leader_slot, ctx->identity_pubkey );
    2015           0 :   if( FD_LIKELY( ctx->next_leader_slot != ULONG_MAX ) ) {
    2016           0 :     ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
    2017           0 :   } else {
    2018           0 :     ctx->next_leader_tickcount = LONG_MAX;
    2019           0 :   }
    2020             : 
    2021           0 :   fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &msg->reset_block_id, NULL, ctx->block_id_arr );
    2022           0 :   if( FD_UNLIKELY( !block_id_ele ) ) {
    2023           0 :     FD_LOG_CRIT(( "invariant violation: block id ele doesn't exist for reset block id: %s, slot: %lu", FD_BASE58_ENC_32_ALLOCA( &msg->reset_block_id ), msg->reset_slot ));
    2024           0 :   }
    2025           0 :   ulong reset_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
    2026             : 
    2027           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, reset_bank_idx );
    2028           0 :   if( FD_UNLIKELY( !bank ) ) {
    2029           0 :     FD_LOG_CRIT(( "invariant violation: bank not found for bank index %lu", reset_bank_idx ));
    2030           0 :   }
    2031             : 
    2032           0 :   if( FD_LIKELY( msg->root_slot!=ULONG_MAX ) ) FD_TEST( msg->root_slot<=msg->reset_slot );
    2033           0 :   ctx->reset_bank = bank;
    2034             : 
    2035           0 :   if( FD_LIKELY( ctx->replay_out->idx!=ULONG_MAX ) ) {
    2036           0 :     fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    2037             : 
    2038           0 :     reset->bank_idx = bank->idx;
    2039           0 :     reset->timestamp = ctx->reset_timestamp_nanos;
    2040           0 :     reset->completed_slot = ctx->reset_slot;
    2041           0 :     reset->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
    2042           0 :     reset->ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
    2043           0 :     reset->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)reset->ticks_per_slot);
    2044             : 
    2045           0 :     fd_memcpy( reset->completed_block_id, &block_id_ele->block_id, sizeof(fd_hash_t) );
    2046             : 
    2047           0 :     fd_blockhashes_t const * block_hash_queue = fd_bank_block_hash_queue_query( bank );
    2048           0 :     fd_hash_t const * last_hash = fd_blockhashes_peek_last_hash( block_hash_queue );
    2049           0 :     FD_TEST( last_hash );
    2050           0 :     fd_memcpy( reset->completed_blockhash, last_hash->uc, sizeof(fd_hash_t) );
    2051             : 
    2052           0 :     ulong ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
    2053           0 :     if( FD_UNLIKELY( reset->hashcnt_per_tick==1UL ) ) {
    2054             :       /* Low power producer, maximum of one microblock per tick in the slot */
    2055           0 :       reset->max_microblocks_in_slot = ticks_per_slot;
    2056           0 :     } else {
    2057             :       /* See the long comment in after_credit for this limit */
    2058           0 :       reset->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, ticks_per_slot*(reset->hashcnt_per_tick-1UL) );
    2059           0 :     }
    2060           0 :     reset->next_leader_slot = ctx->next_leader_slot;
    2061             : 
    2062           0 :     if( FD_LIKELY( ctx->rpc_enabled ) ) bank->refcnt++;
    2063             : 
    2064           0 :     fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_RESET, ctx->replay_out->chunk, sizeof(fd_poh_reset_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
    2065           0 :     ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_poh_reset_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
    2066           0 :   }
    2067             : 
    2068           0 :   FD_LOG_INFO(( "tower_slot_done(reset_slot=%lu, next_leader_slot=%lu, vote_slot=%lu)", msg->reset_slot, ctx->next_leader_slot, msg->vote_slot ));
    2069           0 :   maybe_become_leader( ctx, stem );
    2070             : 
    2071           0 :   if( FD_LIKELY( msg->root_slot!=ULONG_MAX ) ) {
    2072             : 
    2073           0 :     FD_TEST( msg->root_slot>=ctx->consensus_root_slot );
    2074           0 :     fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &msg->root_block_id, NULL, ctx->block_id_arr );
    2075           0 :     FD_TEST( block_id_ele );
    2076             : 
    2077           0 :     ctx->consensus_root_slot     = msg->root_slot;
    2078           0 :     ctx->consensus_root          = msg->root_block_id;
    2079           0 :     ctx->consensus_root_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
    2080             : 
    2081           0 :     publish_root_advanced( ctx, stem );
    2082           0 :   }
    2083             : 
    2084           0 :   ulong distance = 0UL;
    2085           0 :   fd_bank_t * parent = bank;
    2086           0 :   while( parent ) {
    2087           0 :     if( FD_UNLIKELY( parent->idx==ctx->consensus_root_bank_idx ) ) break;
    2088           0 :     parent = fd_banks_get_parent( ctx->banks, parent );
    2089           0 :     distance++;
    2090           0 :   }
    2091             : 
    2092           0 :   FD_MGAUGE_SET( REPLAY, ROOT_DISTANCE, distance );
    2093           0 : }
    2094             : 
    2095             : static void
    2096             : process_fec_complete( fd_replay_tile_t * ctx,
    2097           0 :                       uchar const *      shred_buf ) {
    2098           0 :   fd_shred_t const * shred = (fd_shred_t const *)fd_type_pun_const( shred_buf );
    2099             : 
    2100           0 :   fd_hash_t const * merkle_root         = (fd_hash_t const *)fd_type_pun_const( shred_buf + FD_SHRED_DATA_HEADER_SZ );
    2101           0 :   fd_hash_t const * chained_merkle_root = (fd_hash_t const *)fd_type_pun_const( shred_buf + FD_SHRED_DATA_HEADER_SZ + sizeof(fd_hash_t) );
    2102           0 :   int               is_leader_fec       = *(int const *)     fd_type_pun_const( shred_buf + FD_SHRED_DATA_HEADER_SZ + sizeof(fd_hash_t) + sizeof(fd_hash_t) );
    2103             : 
    2104           0 :   int data_complete = !!( shred->data.flags & FD_SHRED_DATA_FLAG_DATA_COMPLETE );
    2105           0 :   int slot_complete = !!( shred->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE );
    2106             : 
    2107           0 :   FD_TEST( !fd_reasm_query( ctx->reasm, merkle_root ) );
    2108           0 :   if( FD_UNLIKELY( shred->slot - shred->data.parent_off == fd_reasm_slot0( ctx->reasm ) && shred->fec_set_idx == 0) ) {
    2109           0 :     chained_merkle_root = &fd_reasm_root( ctx->reasm )->key;
    2110           0 :   }
    2111             : 
    2112           0 :   FD_TEST( fd_reasm_free( ctx->reasm ) );
    2113             : 
    2114           0 :   FD_TEST( fd_reasm_insert( ctx->reasm, merkle_root, chained_merkle_root, shred->slot, shred->fec_set_idx, shred->data.parent_off, (ushort)(shred->idx - shred->fec_set_idx + 1), data_complete, slot_complete, is_leader_fec ) );
    2115           0 : }
    2116             : 
    2117             : static void
    2118           0 : process_resolv_slot_completed( fd_replay_tile_t * ctx, ulong bank_idx ) {
    2119           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
    2120           0 :   FD_TEST( bank );
    2121             : 
    2122           0 :   bank->refcnt--;
    2123           0 : }
    2124             : 
    2125             : static void
    2126             : process_vote_txn_sent( fd_replay_tile_t *  ctx,
    2127           0 :                        fd_txn_m_t *        txnm ) {
    2128             :   /* The send tile has signed and sent a vote.  Add this vote to the
    2129             :      vote tracker.  We go through this exercise until we've seen our
    2130             :      vote rooted. */
    2131           0 :   if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
    2132           0 :     uchar *    payload = ((uchar *)txnm) + sizeof(fd_txn_m_t);
    2133           0 :     uchar      txn_mem[ FD_TXN_MAX_SZ ] __attribute__((aligned(alignof(fd_txn_t))));
    2134           0 :     fd_txn_t * txn = (fd_txn_t *)txn_mem;
    2135           0 :     if( FD_UNLIKELY( !fd_txn_parse( payload, txnm->payload_sz, txn_mem, NULL ) ) ) {
    2136           0 :       FD_LOG_CRIT(( "Could not parse txn from send tile" ));
    2137           0 :     }
    2138           0 :     fd_vote_tracker_insert( ctx->vote_tracker, fd_type_pun_const( payload+txn->signature_off ) );
    2139           0 :   }
    2140           0 : }
    2141             : 
    2142             : static inline void
    2143           0 : maybe_verify_shred_version( fd_replay_tile_t * ctx ) {
    2144           0 :   if( FD_LIKELY( ctx->expected_shred_version && ctx->ipecho_shred_version ) ) {
    2145           0 :     if( FD_UNLIKELY( ctx->expected_shred_version!=ctx->ipecho_shred_version ) ) {
    2146           0 :       FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from ipecho", ctx->expected_shred_version, ctx->ipecho_shred_version ) );
    2147           0 :     }
    2148           0 :   }
    2149             : 
    2150           0 :   if( FD_LIKELY( ctx->has_genesis_hash && ctx->hard_forks_cnt!=ULONG_MAX && (ctx->expected_shred_version || ctx->ipecho_shred_version) ) ) {
    2151           0 :     ushort expected_shred_version = ctx->expected_shred_version ? ctx->expected_shred_version : ctx->ipecho_shred_version;
    2152             : 
    2153           0 :     union {
    2154           0 :       uchar  c[ 32 ];
    2155           0 :       ushort s[ 16 ];
    2156           0 :     } running_hash;
    2157           0 :     fd_memcpy( running_hash.c, ctx->genesis_hash, sizeof(fd_hash_t) );
    2158             : 
    2159           0 :     ulong processed = 0UL;
    2160           0 :     ulong min_value = 0UL;
    2161           0 :     while( processed<ctx->hard_forks_cnt ) {
    2162           0 :       ulong min_index = ULONG_MAX;
    2163           0 :       for( ulong i=0UL; i<ctx->hard_forks_cnt; i++ ) {
    2164           0 :         if( ctx->hard_forks[ i ]>=min_value && (min_index==ULONG_MAX || ctx->hard_forks[ i ]<ctx->hard_forks[ min_index ] ) ) {
    2165           0 :           min_index = i;
    2166           0 :         }
    2167           0 :       }
    2168             : 
    2169           0 :       FD_TEST( min_index!=ULONG_MAX );
    2170           0 :       min_value = ctx->hard_forks[ min_index ];
    2171           0 :       ulong min_count = 0UL;
    2172           0 :       for( ulong i=0UL; i<ctx->hard_forks_cnt; i++ ) {
    2173           0 :         if( ctx->hard_forks[ i ]==min_value ) min_count++;
    2174           0 :       }
    2175             : 
    2176           0 :       uchar data[ 48UL ];
    2177           0 :       fd_memcpy( data, running_hash.c, sizeof(fd_hash_t) );
    2178           0 :       fd_memcpy( data+32UL, &min_value, sizeof(ulong) );
    2179           0 :       fd_memcpy( data+40UL, &min_count, sizeof(ulong) );
    2180             : 
    2181           0 :       FD_TEST( fd_sha256_hash( data, 48UL, running_hash.c ) );
    2182           0 :       processed += min_count;
    2183           0 :       min_value += 1UL;
    2184           0 :     }
    2185             : 
    2186           0 :     ushort xor = 0;
    2187           0 :     for( ulong i=0UL; i<16UL; i++ ) xor ^= running_hash.s[ i ];
    2188             : 
    2189           0 :     xor = fd_ushort_bswap( xor );
    2190           0 :     xor = fd_ushort_if( xor<USHORT_MAX, (ushort)(xor + 1), USHORT_MAX );
    2191             : 
    2192           0 :     if( FD_UNLIKELY( expected_shred_version!=xor ) ) {
    2193           0 :       FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from genesis hash %s and hard forks", expected_shred_version, xor, FD_BASE58_ENC_32_ALLOCA( &ctx->genesis_hash ) ));
    2194           0 :     }
    2195           0 :   }
    2196           0 : }
    2197             : 
    2198             : static inline int
    2199             : returnable_frag( fd_replay_tile_t *  ctx,
    2200             :                  ulong               in_idx,
    2201             :                  ulong               seq,
    2202             :                  ulong               sig,
    2203             :                  ulong               chunk,
    2204             :                  ulong               sz,
    2205             :                  ulong               ctl,
    2206             :                  ulong               tsorig,
    2207             :                  ulong               tspub,
    2208           0 :                  fd_stem_context_t * stem ) {
    2209           0 :   (void)seq;
    2210           0 :   (void)ctl;
    2211           0 :   (void)tsorig;
    2212           0 :   (void)tspub;
    2213             : 
    2214           0 :   if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
    2215           0 :     FD_LOG_ERR(( "chunk %lu %lu from in %d corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in_kind[ in_idx ], ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
    2216             : 
    2217           0 :   switch( ctx->in_kind[in_idx] ) {
    2218           0 :     case IN_KIND_GENESIS: {
    2219           0 :       uchar const * src = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    2220           0 :       ctx->has_genesis_hash = 1;
    2221           0 :       if( FD_LIKELY( sig==GENESI_SIG_BOOTSTRAP_COMPLETED ) ) {
    2222           0 :         boot_genesis( ctx, stem, in_idx, chunk );
    2223           0 :         fd_memcpy( ctx->genesis_hash, src+sizeof(fd_lthash_value_t), sizeof(fd_hash_t) );
    2224           0 :       } else {
    2225           0 :         fd_memcpy( ctx->genesis_hash, src, sizeof(fd_hash_t) );
    2226           0 :       }
    2227             : 
    2228           0 :       maybe_verify_cluster_type( ctx );
    2229           0 :       maybe_verify_shred_version( ctx );
    2230           0 :       break;
    2231           0 :     }
    2232           0 :     case IN_KIND_IPECHO: {
    2233           0 :       FD_TEST( sig && sig<=USHORT_MAX );
    2234           0 :       ctx->ipecho_shred_version = (ushort)sig;
    2235           0 :       maybe_verify_shred_version( ctx );
    2236           0 :       break;
    2237           0 :     }
    2238           0 :     case IN_KIND_SNAP:
    2239           0 :       on_snapshot_message( ctx, stem, in_idx, chunk, sig );
    2240           0 :       maybe_verify_shred_version( ctx );
    2241           0 :       break;
    2242           0 :     case IN_KIND_EXEC: {
    2243           0 :       process_exec_task_done( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ), sig );
    2244           0 :       break;
    2245           0 :     }
    2246           0 :     case IN_KIND_POH: {
    2247           0 :       process_poh_message( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
    2248           0 :       break;
    2249           0 :     }
    2250           0 :     case IN_KIND_RESOLV: {
    2251           0 :       fd_resolv_slot_exchanged_t * exchanged_slot = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    2252           0 :       process_resolv_slot_completed( ctx, exchanged_slot->bank_idx );
    2253           0 :       break;
    2254           0 :     }
    2255           0 :     case IN_KIND_TOWER: {
    2256           0 :       if     ( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_DONE      ) ) process_tower_slot_done( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
    2257           0 :       else if( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_CONFIRMED ) ) {
    2258           0 :         fd_tower_slot_confirmed_t const * msg = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    2259             : 
    2260             :         /* Implement replay plugin API here */
    2261             : 
    2262           0 :         switch( msg->kind ) {
    2263           0 :         case FD_TOWER_SLOT_CONFIRMED_OPTIMISTIC: break;
    2264           0 :         case FD_TOWER_SLOT_CONFIRMED_ROOTED:     break;
    2265           0 :         }
    2266           0 :       };
    2267           0 :       break;
    2268           0 :     }
    2269           0 :     case IN_KIND_SHRED: {
    2270             :       /* TODO: This message/sz should be defined. */
    2271           0 :       if( sz==FD_SHRED_DATA_HEADER_SZ + sizeof(fd_hash_t) + sizeof(fd_hash_t) + sizeof(int) ) {
    2272             :         /* If receive a FEC complete message. */
    2273           0 :         process_fec_complete( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
    2274           0 :       }
    2275           0 :       break;
    2276           0 :     }
    2277           0 :     case IN_KIND_VTXN: {
    2278           0 :       process_vote_txn_sent( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
    2279           0 :       break;
    2280           0 :     }
    2281           0 :     case IN_KIND_RPC:
    2282           0 :     case IN_KIND_GUI: {
    2283           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, sig );
    2284           0 :       FD_TEST( bank );
    2285           0 :       bank->refcnt--;
    2286           0 :       break;
    2287           0 :     }
    2288           0 :     default:
    2289           0 :       FD_LOG_ERR(( "unhandled kind %d", ctx->in_kind[ in_idx ] ));
    2290           0 :   }
    2291             : 
    2292           0 :   return 0;
    2293           0 : }
    2294             : 
    2295             : static inline fd_replay_out_link_t
    2296             : out1( fd_topo_t const *      topo,
    2297             :       fd_topo_tile_t const * tile,
    2298           0 :       char const *           name ) {
    2299           0 :   ulong idx = ULONG_MAX;
    2300             : 
    2301           0 :   for( ulong i=0UL; i<tile->out_cnt; i++ ) {
    2302           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
    2303           0 :     if( !strcmp( link->name, name ) ) {
    2304           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 ));
    2305           0 :       idx = i;
    2306           0 :     }
    2307           0 :   }
    2308             : 
    2309           0 :   if( FD_UNLIKELY( idx==ULONG_MAX ) ) return (fd_replay_out_link_t){ .idx = ULONG_MAX, .mem = NULL, .chunk0 = 0, .wmark = 0, .chunk = 0 };
    2310             : 
    2311           0 :   void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
    2312           0 :   ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
    2313           0 :   ulong wmark  = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
    2314             : 
    2315           0 :   return (fd_replay_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
    2316           0 : }
    2317             : 
    2318             : static void
    2319             : privileged_init( fd_topo_t *      topo,
    2320           0 :                  fd_topo_tile_t * tile ) {
    2321           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    2322             : 
    2323           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    2324           0 :   fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
    2325             : 
    2326           0 :   if( FD_UNLIKELY( !strcmp( tile->replay.identity_key_path, "" ) ) ) FD_LOG_ERR(( "identity_key_path not set" ));
    2327             : 
    2328           0 :   ctx->identity_pubkey[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->replay.identity_key_path, /* pubkey only: */ 1 ) );
    2329             : 
    2330           0 :   if( FD_UNLIKELY( !tile->replay.bundle.vote_account_path[0] ) ) {
    2331           0 :     tile->replay.bundle.enabled = 0;
    2332           0 :   }
    2333           0 :   if( FD_UNLIKELY( tile->replay.bundle.enabled ) ) {
    2334           0 :     if( FD_UNLIKELY( !fd_base58_decode_32( tile->replay.bundle.vote_account_path, ctx->bundle.vote_account.uc ) ) ) {
    2335           0 :       const uchar * vote_key = fd_keyload_load( tile->replay.bundle.vote_account_path, /* pubkey only: */ 1 );
    2336           0 :       fd_memcpy( ctx->bundle.vote_account.uc, vote_key, 32UL );
    2337           0 :     }
    2338           0 :   }
    2339           0 : }
    2340             : 
    2341             : static void
    2342             : unprivileged_init( fd_topo_t *      topo,
    2343           0 :                    fd_topo_tile_t * tile ) {
    2344           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    2345             : 
    2346           0 :   ulong chain_cnt = fd_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
    2347             : 
    2348           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    2349           0 :   fd_replay_tile_t * ctx   = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t),   sizeof(fd_replay_tile_t) );
    2350           0 :   void * block_id_arr_mem  = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_block_id_ele_t),  sizeof(fd_block_id_ele_t) * tile->replay.max_live_slots );
    2351           0 :   void * block_id_map_mem  = FD_SCRATCH_ALLOC_APPEND( l, fd_block_id_map_align(),     fd_block_id_map_footprint( chain_cnt ) );
    2352           0 :   void * _txncache         = FD_SCRATCH_ALLOC_APPEND( l, fd_txncache_align(),         fd_txncache_footprint( tile->replay.max_live_slots ) );
    2353           0 :   void * reasm_mem         = FD_SCRATCH_ALLOC_APPEND( l, fd_reasm_align(),            fd_reasm_footprint( 1 << 20 ) );
    2354           0 :   void * sched_mem         = FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(),            fd_sched_footprint( tile->replay.max_live_slots ) );
    2355           0 :   void * vote_tracker_mem  = FD_SCRATCH_ALLOC_APPEND( l, fd_vote_tracker_align(),     fd_vote_tracker_footprint() );
    2356           0 :   void * _capture_ctx      = FD_SCRATCH_ALLOC_APPEND( l, fd_capture_ctx_align(),      fd_capture_ctx_footprint() );
    2357           0 : # if FD_HAS_FLATCC
    2358           0 :   void * block_dump_ctx    = NULL;
    2359           0 :   if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
    2360           0 :     block_dump_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
    2361           0 :   }
    2362           0 : # endif
    2363             : 
    2364           0 :   ulong store_obj_id = fd_pod_query_ulong( topo->props, "store", ULONG_MAX );
    2365           0 :   FD_TEST( store_obj_id!=ULONG_MAX );
    2366           0 :   ctx->store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
    2367           0 :   FD_TEST( ctx->store );
    2368             : 
    2369           0 :   ulong banks_obj_id = fd_pod_query_ulong( topo->props, "banks", ULONG_MAX );
    2370           0 :   FD_TEST( banks_obj_id!=ULONG_MAX );
    2371           0 :   ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
    2372           0 :   FD_TEST( ctx->banks );
    2373             : 
    2374           0 :   fd_bank_t * bank_pool = fd_banks_get_bank_pool( ctx->banks );
    2375           0 :   FD_MGAUGE_SET( REPLAY, MAX_LIVE_BANKS, fd_banks_pool_max( bank_pool ) );
    2376             : 
    2377           0 :   fd_bank_t * bank = fd_banks_init_bank( ctx->banks );
    2378           0 :   fd_bank_slot_set( bank, 0UL );
    2379           0 :   FD_TEST( bank );
    2380           0 :   FD_TEST( bank->idx==FD_REPLAY_BOOT_BANK_IDX );
    2381             : 
    2382           0 :   ctx->consensus_root_slot = ULONG_MAX;
    2383           0 :   ctx->consensus_root      = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
    2384           0 :   ctx->published_root_slot = ULONG_MAX;
    2385             : 
    2386           0 :   ctx->expected_shred_version = tile->replay.expected_shred_version;
    2387           0 :   ctx->ipecho_shred_version = 0;
    2388           0 :   ctx->has_genesis_hash = 0;
    2389           0 :   ctx->cluster_type = FD_CLUSTER_UNKNOWN;
    2390           0 :   ctx->hard_forks_cnt = ULONG_MAX;
    2391             : 
    2392           0 :   if( FD_UNLIKELY( tile->replay.bundle.enabled ) ) {
    2393           0 :     ctx->bundle.enabled = 1;
    2394           0 :     if( FD_UNLIKELY( !fd_bundle_crank_gen_init( ctx->bundle.gen,
    2395           0 :              (fd_acct_addr_t const *)tile->replay.bundle.tip_distribution_program_addr,
    2396           0 :              (fd_acct_addr_t const *)tile->replay.bundle.tip_payment_program_addr,
    2397           0 :              (fd_acct_addr_t const *)ctx->bundle.vote_account.uc,
    2398           0 :              (fd_acct_addr_t const *)ctx->bundle.vote_account.uc, "NAN", 0UL ) ) ) {
    2399           0 :       FD_LOG_ERR(( "failed to initialize bundle crank gen" ));
    2400           0 :     }
    2401           0 :   } else {
    2402           0 :     ctx->bundle.enabled = 0;
    2403           0 :   }
    2404             : 
    2405           0 :   fd_features_t * features = fd_bank_features_modify( bank );
    2406           0 :   fd_features_enable_cleaned_up( features, &FD_RUNTIME_CLUSTER_VERSION );
    2407             : 
    2408           0 :   char const * one_off_features[ 16UL ];
    2409           0 :   FD_TEST( tile->replay.enable_features_cnt<=sizeof(one_off_features)/sizeof(one_off_features[0]) );
    2410           0 :   for( ulong i=0UL; i<tile->replay.enable_features_cnt; i++ ) one_off_features[ i ] = tile->replay.enable_features[i];
    2411           0 :   fd_features_enable_one_offs( features, one_off_features, (uint)tile->replay.enable_features_cnt, 0UL );
    2412             : 
    2413           0 :   FD_TEST( fd_accdb_admin_join    ( ctx->accdb_admin,     fd_topo_obj_laddr( topo, tile->replay.funk_obj_id      ) ) );
    2414           0 :   FD_TEST( fd_accdb_user_v1_init  ( ctx->accdb,           fd_topo_obj_laddr( topo, tile->replay.funk_obj_id      ) ) );
    2415           0 :   FD_TEST( fd_progcache_admin_join( ctx->progcache_admin, fd_topo_obj_laddr( topo, tile->replay.progcache_obj_id ) ) );
    2416             : 
    2417           0 :   void * _txncache_shmem = fd_topo_obj_laddr( topo, tile->replay.txncache_obj_id );
    2418           0 :   fd_txncache_shmem_t * txncache_shmem = fd_txncache_shmem_join( _txncache_shmem );
    2419           0 :   FD_TEST( txncache_shmem );
    2420           0 :   ctx->txncache = fd_txncache_join( fd_txncache_new( _txncache, txncache_shmem ) );
    2421           0 :   FD_TEST( ctx->txncache );
    2422             : 
    2423           0 :   ctx->capture_ctx = NULL;
    2424           0 :   if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) || strcmp( "", tile->replay.dump_proto_dir ) ) ) {
    2425           0 :     ctx->capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( _capture_ctx ) );
    2426           0 :   }
    2427             : 
    2428           0 :   if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) ) ) {
    2429           0 :     ctx->capture_ctx->checkpt_freq = ULONG_MAX;
    2430           0 :     ctx->capture_file = fopen( tile->replay.solcap_capture, "w+" );
    2431           0 :     if( FD_UNLIKELY( !ctx->capture_file ) ) FD_LOG_ERR(( "fopen(%s) failed (%d-%s)", tile->replay.solcap_capture, errno, fd_io_strerror( errno ) ));
    2432             : 
    2433           0 :     ctx->capture_ctx->capture_txns = 0;
    2434           0 :     ctx->capture_ctx->solcap_start_slot = tile->replay.capture_start_slot;
    2435           0 :     fd_solcap_writer_init( ctx->capture_ctx->capture, ctx->capture_file );
    2436           0 :   }
    2437             : 
    2438           0 :   if( FD_UNLIKELY( strcmp( "", tile->replay.dump_proto_dir ) ) ) {
    2439           0 :     ctx->capture_ctx->dump_proto_output_dir = tile->replay.dump_proto_dir;
    2440           0 :     if( FD_LIKELY( tile->replay.dump_block_to_pb ) ) ctx->capture_ctx->dump_block_to_pb = tile->replay.dump_block_to_pb;
    2441           0 :   }
    2442             : 
    2443           0 : # if FD_HAS_FLATCC
    2444           0 :   if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
    2445           0 :     ctx->block_dump_ctx = fd_block_dump_context_join( fd_block_dump_context_new( block_dump_ctx ) );
    2446           0 :   } else {
    2447           0 :     ctx->block_dump_ctx = NULL;
    2448           0 :   }
    2449           0 : # endif
    2450             : 
    2451           0 :   ctx->exec_cnt = fd_topo_tile_name_cnt( topo, "exec" );
    2452             : 
    2453           0 :   ctx->is_booted = 0;
    2454             : 
    2455           0 :   ctx->larger_max_cost_per_block = tile->replay.larger_max_cost_per_block;
    2456             : 
    2457           0 :   ctx->reasm = fd_reasm_join( fd_reasm_new( reasm_mem, 1 << 20, 0 ) );
    2458           0 :   FD_TEST( ctx->reasm );
    2459             : 
    2460           0 :   ctx->sched = fd_sched_join( fd_sched_new( sched_mem, tile->replay.max_live_slots, ctx->exec_cnt ), tile->replay.max_live_slots );
    2461           0 :   FD_TEST( ctx->sched );
    2462             : 
    2463           0 :   ctx->vote_tracker = fd_vote_tracker_join( fd_vote_tracker_new( vote_tracker_mem, 0UL ) );
    2464           0 :   FD_TEST( ctx->vote_tracker );
    2465             : 
    2466           0 :   ctx->has_identity_vote_rooted = 0;
    2467             : 
    2468           0 :   ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
    2469           0 :   FD_TEST( ctx->mleaders );
    2470             : 
    2471           0 :   ctx->is_leader             = 0;
    2472           0 :   ctx->reset_slot            = 0UL;
    2473           0 :   ctx->reset_bank            = NULL;
    2474           0 :   ctx->reset_block_id        = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
    2475           0 :   ctx->reset_timestamp_nanos = 0UL;
    2476           0 :   ctx->next_leader_slot      = ULONG_MAX;
    2477           0 :   ctx->next_leader_tickcount = LONG_MAX;
    2478           0 :   ctx->highwater_leader_slot = ULONG_MAX;
    2479           0 :   ctx->slot_duration_nanos   = 350L*1000L*1000L; /* TODO: Not fixed ... not always 400ms ... */
    2480           0 :   ctx->slot_duration_ticks   = (double)ctx->slot_duration_nanos*fd_tempo_tick_per_ns( NULL );
    2481           0 :   ctx->leader_bank           = NULL;
    2482             : 
    2483             :   /* TODO: We need a real seed here. */
    2484           0 :   ctx->block_id_len = tile->replay.max_live_slots;
    2485           0 :   ctx->block_id_arr = (fd_block_id_ele_t *)block_id_arr_mem;
    2486           0 :   ctx->block_id_map = fd_block_id_map_join( fd_block_id_map_new( block_id_map_mem, chain_cnt, 999UL ) );
    2487           0 :   FD_TEST( ctx->block_id_map );
    2488             : 
    2489           0 :   for( ulong i=0UL; i<tile->replay.max_live_slots; i++ ) {
    2490           0 :     ctx->block_id_arr[ i ].slot = FD_SLOT_NULL;
    2491           0 :   }
    2492             : 
    2493           0 :   ctx->resolv_tile_cnt = fd_topo_tile_name_cnt( topo, "resolv" );
    2494             : 
    2495           0 :   FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
    2496           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
    2497           0 :     fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
    2498           0 :     fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
    2499             : 
    2500           0 :     if( FD_LIKELY( link->dcache ) ) {
    2501           0 :       ctx->in[ i ].mem    = link_wksp->wksp;
    2502           0 :       ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
    2503           0 :       ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
    2504           0 :       ctx->in[ i ].mtu    = link->mtu;
    2505           0 :     }
    2506             : 
    2507           0 :     if(      !strcmp( link->name, "genesi_out"   ) ) ctx->in_kind[ i ] = IN_KIND_GENESIS;
    2508           0 :     else if( !strcmp( link->name, "ipecho_out"   ) ) ctx->in_kind[ i ] = IN_KIND_IPECHO;
    2509           0 :     else if( !strcmp( link->name, "snapin_manif" ) ) ctx->in_kind[ i ] = IN_KIND_SNAP;
    2510           0 :     else if( !strcmp( link->name, "exec_replay"  ) ) ctx->in_kind[ i ] = IN_KIND_EXEC;
    2511           0 :     else if( !strcmp( link->name, "tower_out"    ) ) ctx->in_kind[ i ] = IN_KIND_TOWER;
    2512           0 :     else if( !strcmp( link->name, "poh_replay"   ) ) ctx->in_kind[ i ] = IN_KIND_POH;
    2513           0 :     else if( !strcmp( link->name, "resolv_repla" ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
    2514           0 :     else if( !strcmp( link->name, "shred_out"    ) ) ctx->in_kind[ i ] = IN_KIND_SHRED;
    2515           0 :     else if( !strcmp( link->name, "send_out"     ) ) ctx->in_kind[ i ] = IN_KIND_VTXN;
    2516           0 :     else if( !strcmp( link->name, "gui_replay"   ) ) ctx->in_kind[ i ] = IN_KIND_GUI;
    2517           0 :     else if( !strcmp( link->name, "rpc_replay"   ) ) ctx->in_kind[ i ] = IN_KIND_RPC;
    2518           0 :     else FD_LOG_ERR(( "unexpected input link name %s", link->name ));
    2519           0 :   }
    2520             : 
    2521           0 :   *ctx->stake_out  = out1( topo, tile, "replay_stake" ); FD_TEST( ctx->stake_out->idx!=ULONG_MAX );
    2522           0 :   *ctx->replay_out = out1( topo, tile, "replay_out" ); FD_TEST( ctx->replay_out->idx!=ULONG_MAX );
    2523             : 
    2524           0 :   ulong idx = fd_topo_find_tile_out_link( topo, tile, "replay_exec", 0UL );
    2525           0 :   FD_TEST( idx!=ULONG_MAX );
    2526           0 :   fd_topo_link_t * link = &topo->links[ tile->out_link_id[ idx ] ];
    2527             : 
    2528           0 :   fd_replay_out_link_t * exec_out = ctx->exec_out;
    2529           0 :   exec_out->idx    = idx;
    2530           0 :   exec_out->mem    = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
    2531           0 :   exec_out->chunk0 = fd_dcache_compact_chunk0( exec_out->mem, link->dcache );
    2532           0 :   exec_out->wmark  = fd_dcache_compact_wmark( exec_out->mem, link->dcache, link->mtu );
    2533           0 :   exec_out->chunk  = exec_out->chunk0;
    2534             : 
    2535           0 :   ctx->gui_enabled = fd_topo_find_tile( topo, "gui", 0UL )!=ULONG_MAX;
    2536           0 :   ctx->rpc_enabled = fd_topo_find_tile( topo, "rpc", 0UL )!=ULONG_MAX;
    2537             : 
    2538           0 :   fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
    2539             : 
    2540           0 :   fd_histf_join( fd_histf_new( ctx->metrics.store_link_wait,    FD_MHIST_SECONDS_MIN( REPLAY, STORE_LINK_WAIT ),
    2541           0 :                                                                 FD_MHIST_SECONDS_MAX( REPLAY, STORE_LINK_WAIT ) ) );
    2542           0 :   fd_histf_join( fd_histf_new( ctx->metrics.store_link_work,    FD_MHIST_SECONDS_MIN( REPLAY, STORE_LINK_WORK ),
    2543           0 :                                                                 FD_MHIST_SECONDS_MAX( REPLAY, STORE_LINK_WORK ) ) );
    2544           0 :   fd_histf_join( fd_histf_new( ctx->metrics.store_read_wait,    FD_MHIST_SECONDS_MIN( REPLAY, STORE_READ_WAIT ),
    2545           0 :                                                                 FD_MHIST_SECONDS_MAX( REPLAY, STORE_READ_WAIT ) ) );
    2546           0 :   fd_histf_join( fd_histf_new( ctx->metrics.store_read_work,    FD_MHIST_SECONDS_MIN( REPLAY, STORE_READ_WORK ),
    2547           0 :                                                                 FD_MHIST_SECONDS_MAX( REPLAY, STORE_READ_WORK ) ) );
    2548           0 :   fd_histf_join( fd_histf_new( ctx->metrics.store_publish_wait, FD_MHIST_SECONDS_MIN( REPLAY, STORE_PUBLISH_WAIT ),
    2549           0 :                                                                 FD_MHIST_SECONDS_MAX( REPLAY, STORE_PUBLISH_WAIT ) ) );
    2550           0 :   fd_histf_join( fd_histf_new( ctx->metrics.store_publish_work, FD_MHIST_SECONDS_MIN( REPLAY, STORE_PUBLISH_WORK ),
    2551           0 :                                                                 FD_MHIST_SECONDS_MAX( REPLAY, STORE_PUBLISH_WORK ) ) );
    2552             : 
    2553           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
    2554           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
    2555           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
    2556           0 : }
    2557             : 
    2558             : static ulong
    2559             : populate_allowed_seccomp( fd_topo_t const *      topo FD_FN_UNUSED,
    2560             :                           fd_topo_tile_t const * tile FD_FN_UNUSED,
    2561             :                           ulong                  out_cnt,
    2562           0 :                           struct sock_filter *   out ) {
    2563             : 
    2564           0 :   populate_sock_filter_policy_fd_replay_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
    2565           0 :   return sock_filter_policy_fd_replay_tile_instr_cnt;
    2566           0 : }
    2567             : 
    2568             : static ulong
    2569             : populate_allowed_fds( fd_topo_t const *      topo FD_FN_UNUSED,
    2570             :                       fd_topo_tile_t const * tile FD_FN_UNUSED,
    2571             :                       ulong                  out_fds_cnt,
    2572           0 :                       int *                  out_fds ) {
    2573             : 
    2574           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
    2575             : 
    2576           0 :   ulong out_cnt = 0UL;
    2577           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
    2578           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
    2579           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
    2580           0 :   return out_cnt;
    2581           0 : }
    2582             : 
    2583             : #undef DEBUG_LOGGING
    2584             : 
    2585             : /* counting carefully, after_credit can generate at most 7 frags and
    2586             :    returnable_frag boot_genesis can also generate at most 7 frags, so 14
    2587             :    is a conservative bound. */
    2588           0 : #define STEM_BURST (14UL)
    2589             : 
    2590             : /* TODO: calculate this properly/fix stem to work with larger numbers of links */
    2591             : /* 1000 chosen empirically as anything larger slowed down replay times. Need to calculate
    2592             :    this properly. */
    2593           0 : #define STEM_LAZY ((long)10e3)
    2594             : 
    2595           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_replay_tile_t
    2596           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_replay_tile_t)
    2597             : 
    2598           0 : #define STEM_CALLBACK_METRICS_WRITE   metrics_write
    2599           0 : #define STEM_CALLBACK_AFTER_CREDIT    after_credit
    2600           0 : #define STEM_CALLBACK_BEFORE_FRAG     before_frag
    2601           0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
    2602             : 
    2603             : #include "../../disco/stem/fd_stem.c"
    2604             : 
    2605             : fd_topo_run_tile_t fd_tile_replay = {
    2606             :   .name                     = "replay",
    2607             :   .populate_allowed_seccomp = populate_allowed_seccomp,
    2608             :   .populate_allowed_fds     = populate_allowed_fds,
    2609             :   .scratch_align            = scratch_align,
    2610             :   .scratch_footprint        = scratch_footprint,
    2611             :   .privileged_init          = privileged_init,
    2612             :   .unprivileged_init        = unprivileged_init,
    2613             :   .run                      = stem_run,
    2614             : };

Generated by: LCOV version 1.14