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

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include <linux/futex.h> /* FUTEX_WAKE */
       3             : #include <sys/syscall.h> /* SYS_futex */
       4             : #include <unistd.h> /* syscall(2) */
       5             : 
       6             : #include "fd_replay_tile.h"
       7             : #include "fd_replay_tile_private.h"
       8             : #include "../../ballet/bls/fd_bls12_381.h"
       9             : #include "fd_sched.h"
      10             : #include "fd_execrp.h"
      11             : #include "generated/fd_replay_tile_seccomp.h"
      12             : 
      13             : #include "../admin/fd_adminctl.h"
      14             : #include <errno.h>
      15             : #include <fcntl.h>
      16             : #include "../genesis/fd_genesi_tile.h"
      17             : #include "../poh/fd_poh.h"
      18             : #include "../poh/fd_poh_tile.h"
      19             : #include "../votor/fd_votor_tile.h"
      20             : #include "../resolv/fd_resolv_tile.h"
      21             : #include "../restore/utils/fd_ssload.h"
      22             : #include "../restore/utils/fd_wfs.h"
      23             : #include "../../flamenco/gossip/fd_gossip_message.h"
      24             : 
      25             : #include "../../disco/tiles.h"
      26             : #include "../../disco/fd_txn_m.h"
      27             : #include "../../disco/shred/fd_fec_set.h"
      28             : #include "../../disco/shred/fd_shred_tile.h"
      29             : #include "../../disco/pack/fd_pack.h"
      30             : #include "../../disco/pack/fd_pack_cost.h"
      31             : #include "../backup/fd_snapmk_tile.h"
      32             : #include "../../disco/keyguard/fd_keyload.h"
      33             : #include "../../disco/genesis/fd_genesis_cluster.h"
      34             : #include "../../discof/genesis/genesis_hash.h"
      35             : #include "../../util/pod/fd_pod.h"
      36             : #include "../../util/fd_boolau.h"
      37             : #include "../../flamenco/rewards/fd_rewards.h"
      38             : #include "../../flamenco/leaders/fd_multi_epoch_leaders.h"
      39             : #include "../../flamenco/stakes/fd_stakes.h"
      40             : #include "../../flamenco/progcache/fd_progcache_admin.h"
      41             : #include "../../flamenco/rewards/fd_rewards.h"
      42             : #include "../../disco/metrics/fd_metrics.h"
      43             : #include "../../flamenco/events/fd_event_runtime.h"
      44             : #include "../../disco/events/generated/fd_event_gen.h"
      45             : #include "../rotor/fd_rotor_tile.h"
      46             : #include "../../flamenco/runtime/fd_runtime.h"
      47             : #include "../../flamenco/runtime/fd_runtime_stack_tmpl.h"
      48             : 
      49             : #include "../../flamenco/runtime/sysvar/fd_sysvar_cache.h"
      50             : #include "../../flamenco/runtime/sysvar/fd_sysvar_stake_history.h"
      51             : #include "../../flamenco/runtime/sysvar/fd_sysvar_clock.h"
      52             : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
      53             : #include "../../flamenco/runtime/sysvar/fd_sysvar_rent.h"
      54             : #include "../../flamenco/runtime/program/fd_precompiles.h"
      55             : #include "../../flamenco/runtime/program/vote/fd_vote_state_versioned.h"
      56             : #include "../../flamenco/runtime/program/vote/fd_vote_codec_tmpl.h"
      57             : #include "../../flamenco/runtime/tests/fd_dump_pb.h"
      58             : #include "../../disco/events/fd_event_report.h"
      59             : #include "../../disco/metrics/fd_metrics_sample.h"
      60             : 
      61             : /* Replay concepts:
      62             : 
      63             :    - Blocks are aggregations of entries aka. microblocks which are
      64             :      groupings of txns and are constructed by the block producer (see
      65             :      fd_pack).
      66             : 
      67             :    - Entries are grouped into entry batches by the block producer (see
      68             :      fd_pack / fd_shredder).
      69             : 
      70             :    - Entry batches are divided into chunks known as shreds by the block
      71             :      producer (see fd_shredder).
      72             : 
      73             :    - Shreds are grouped into forward-error-correction sets (FEC sets) by
      74             :      the block producer (see fd_shredder).
      75             : 
      76             :    - Shreds are transmitted to the rest of the cluster via the Turbine
      77             :      protocol (see fd_shredder / fd_shred).
      78             : 
      79             :    - Once enough shreds within a FEC set are received to recover the
      80             :      entirety of the shred data encoded by that FEC set, the receiver
      81             :      can "complete" the FEC set (see fd_fec_resolver).
      82             : 
      83             :    - If shreds in the FEC set are missing such that it can't complete,
      84             :      the receiver can use the Repair protocol to request missing shreds
      85             :      in FEC set (see fd_repair).
      86             : 
      87             :   -  The current Repair protocol does not support requesting coding
      88             :      shreds.  As a result, some FEC sets might be actually complete
      89             :      (contain all data shreds).  Repair currently hacks around this by
      90             :      forcing completion but the long-term solution is to add support for
      91             :      fec_repairing coding shreds via Repair.
      92             : 
      93             :   - FEC sets are delivered in partial-order to the Replay tile by the
      94             :     Repair tile.  Currently Replay only supports replaying entry batches
      95             :     so FEC sets need to reassembled into an entry batch before they can
      96             :     be replayed.  The new Dispatcher will change this by taking a FEC
      97             :     set as input instead. */
      98             : 
      99             : /* Agave defines TIME_TO_COMPLETE_BLOCK_BROADCAST to adjust their slot
     100             :    end.  Our own telemetry data of finishing pack => shredding last
     101             :    batch suggests roughly the same delay, so we mirror the constant. */
     102             : 
     103           0 : #define AG_TIME_TO_COMPLETE_BROADCAST_NS (6000000L)
     104             : 
     105           0 : #define IN_KIND_SNAP       ( 0)
     106           0 : #define IN_KIND_GENESIS    ( 1)
     107           0 : #define IN_KIND_IPECHO     ( 2)
     108           0 : #define IN_KIND_RESOLV     ( 4)
     109           0 : #define IN_KIND_POH        ( 5)
     110           0 : #define IN_KIND_EXECRP     ( 6)
     111           0 : #define IN_KIND_REPAIR     ( 7)
     112           0 : #define IN_KIND_TXSEND     ( 8)
     113           0 : #define IN_KIND_RPC        ( 9)
     114           0 : #define IN_KIND_GOSSIP_OUT (10)
     115           0 : #define IN_KIND_SNAPMK     (11)
     116           0 : #define IN_KIND_ADMIN      (12)
     117           0 : #define IN_KIND_VOTOR      (13)
     118             : 
     119             : #define DEBUG_LOGGING 0
     120             : 
     121             : /* The first bank that the replay tile produces either for genesis
     122             :    or the snapshot boot will always be at bank index 0. */
     123           0 : #define FD_REPLAY_BOOT_BANK_IDX (0UL)
     124             : 
     125             : /* wfs_resolve_leader_gate_auto resolves to true when
     126             :    wait_for_vote_to_start_leader is configured as auto, otherwise it
     127             :    passes the config value through.  True keeps the leader gate armed,
     128             :    which is correct for a non-WFS boot and for NOOP. */
     129             : 
     130             : static inline int
     131           0 : wfs_resolve_leader_gate_auto( int wait_for_vote_to_start_leader ) {
     132           0 :   return fd_int_if( wait_for_vote_to_start_leader==FD_BOOLAU_AUTO,
     133           0 :                     FD_BOOLAU_TRUE,
     134           0 :                     wait_for_vote_to_start_leader );
     135           0 : }
     136             : 
     137             : static inline ulong
     138           0 : fd_block_id_ele_get_idx( fd_block_id_ele_t * ele_arr, fd_block_id_ele_t * ele ) {
     139           0 :   return (ulong)(ele - ele_arr);
     140           0 : }
     141             : 
     142             : static inline fd_block_id_ele_t *
     143             : fd_block_id_ele_query( fd_replay_tile_t * ctx,
     144             :                        fd_hash_t const *  block_id,
     145           0 :                        ulong              slot ) {
     146           0 :   ag_block_id_t key = ag_block_id( slot, block_id->uc );
     147           0 :   return fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &key, NULL, ctx->block_id_arr );
     148           0 : }
     149             : 
     150             : FD_FN_CONST static inline ulong
     151           0 : scratch_align( void ) {
     152           0 :   return 128UL;
     153           0 : }
     154             : FD_FN_PURE static inline ulong
     155           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     156           0 :   ulong chain_cnt = fd_ag_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
     157             : 
     158           0 :   ulong l = FD_LAYOUT_INIT;
     159           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_replay_tile_t),           sizeof(fd_replay_tile_t) );
     160           0 :   l = FD_LAYOUT_APPEND( l, fd_genesis_align(),                  fd_genesis_footprint( fd_genesis_account_max( tile->replay.genesis_max_message_size ) ) );
     161           0 :   l = FD_LAYOUT_APPEND( l, fd_runtime_stack_align(),            fd_runtime_stack_footprint( FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKED_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKE_ACCOUNTS ) );
     162           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_block_id_ele_t),          sizeof(fd_block_id_ele_t) * tile->replay.max_live_slots );
     163           0 :   l = FD_LAYOUT_APPEND( l, alignof(ulong),                      sizeof(ulong) * tile->replay.max_live_slots );
     164           0 :   if( FD_UNLIKELY( tile->replay.report_runtime_diffs ) ) {
     165           0 :     l = FD_LAYOUT_APPEND( l, alignof(fd_hash_t),                  sizeof(fd_hash_t) * FD_FEC_BLK_MAX * tile->replay.max_live_slots );
     166           0 :     l = FD_LAYOUT_APPEND( l, 8UL,                                 FD_EVENT_RUNTIME_SLOT_DIFFS_FOOTPRINT * tile->replay.max_live_slots );
     167           0 :   }
     168           0 :   l = FD_LAYOUT_APPEND( l, fd_ag_block_id_map_align(),          fd_ag_block_id_map_footprint( chain_cnt ) );
     169           0 :   l = FD_LAYOUT_APPEND( l, fd_txncache_align(),                 fd_txncache_footprint( tile->replay.max_live_slots ) );
     170           0 :   l = FD_LAYOUT_APPEND( l, fd_accdb_align(),                    fd_accdb_footprint( tile->replay.max_live_slots, 0 ) );
     171           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_reception_stats_t),       sizeof(fd_reception_stats_t)*tile->replay.max_live_slots );
     172           0 :   l = FD_LAYOUT_APPEND( l, fd_sched_align(),                    fd_sched_footprint( tile->replay.sched_depth, tile->replay.max_live_slots, tile->replay.max_shreds_per_block, tile->replay.max_txn_per_slot ) );
     173           0 :   l = FD_LAYOUT_APPEND( l, fd_vote_tracker_align(),             fd_vote_tracker_footprint() );
     174           0 :   l = FD_LAYOUT_APPEND( l, fd_capture_ctx_align(),              fd_capture_ctx_footprint() );
     175           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_dump_proto_ctx_t),        sizeof(fd_dump_proto_ctx_t) );
     176           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_event_block_completed_t), sizeof(fd_event_block_completed_t) );
     177           0 :   l = FD_LAYOUT_APPEND( l, fd_timing_slot_pool_align(),         fd_timing_slot_pool_footprint( FD_REPLAY_TXN_TIMING_SLOTS ) );
     178           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_replay_txn_timing_t),     FD_REPLAY_TXN_TIMING_SLOTS*tile->replay.max_txn_per_slot*sizeof(fd_replay_txn_timing_t) );
     179           0 :   l = FD_LAYOUT_APPEND( l, alignof(ulong),                      tile->replay.max_live_slots*sizeof(ulong) );
     180             : 
     181           0 :   if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
     182           0 :     l = FD_LAYOUT_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
     183           0 :   }
     184             : 
     185           0 :   l = FD_LAYOUT_FINI( l, scratch_align() );
     186             : 
     187           0 :   return l;
     188           0 : }
     189             : 
     190             : /* wait_info_healthy returns 1 if the replay tile is healthy: caught up
     191             :    and not falling behind turbine by more than 12 slots. */
     192             : 
     193             : static inline int
     194             : wait_info_healthy( int   caught_up,
     195             :                    ulong catch_up_max_fec_slot,
     196           0 :                    ulong reset_slot ) {
     197           0 :   ulong turbine_slot = catch_up_max_fec_slot==ULONG_MAX ? 0UL : catch_up_max_fec_slot;
     198           0 :   ulong health_reset = reset_slot==ULONG_MAX            ? 0UL : reset_slot;
     199           0 :   return caught_up && !( turbine_slot>health_reset && turbine_slot-health_reset>12UL );
     200           0 : }
     201             : 
     202             : /* query_next_leader_slot returns the next slot at or after from the
     203             :    one we lead, ULONG_MAX if the schedules hold none. */
     204             : 
     205             : static ulong
     206             : query_next_leader_slot( fd_replay_tile_t * ctx,
     207           0 :                         ulong              from ) {
     208           0 :   if( FD_UNLIKELY( ctx->next_leader_query_start==ULONG_MAX ||
     209           0 :                    from<ctx->next_leader_query_start       ||
     210           0 :                    from>ctx->next_leader_query_slot        ) ) {
     211           0 :     ctx->next_leader_query_start = from;
     212           0 :     ctx->next_leader_query_slot  = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, from, ctx->identity_pubkey );
     213           0 :   }
     214           0 :   return ctx->next_leader_query_slot;
     215           0 : }
     216             : 
     217             : /* refresh_epoch_info republishes the epoch fields wait_info exposes.
     218             :    Call wherever reset_slot moves. */
     219             : 
     220             : static void
     221             : refresh_epoch_info( fd_replay_tile_t * ctx,
     222             :                     fd_bank_t const *  bank,
     223           0 :                     ulong              slot ) {
     224           0 :   fd_epoch_schedule_t const * sched = &bank->f.epoch_schedule;
     225           0 :   ulong ep = fd_slot_to_epoch( sched, slot, NULL );
     226           0 :   ctx->epoch_end_slot  = fd_epoch_slot0( sched, ep ) + fd_epoch_slot_cnt( sched, ep ) - 1UL;
     227           0 :   ctx->slots_per_epoch = sched->slots_per_epoch;
     228           0 :   ctx->ns_per_slot     = bank->f.slot_params.ns_per_slot;
     229           0 : }
     230             : 
     231             : /* wait_info_publish writes the wait_info topology object so the wait
     232             :    command can read a consistent snapshot without touching metrics. */
     233             : 
     234             : static void
     235           0 : wait_info_publish( fd_replay_tile_t * ctx ) {
     236           0 :   if( FD_UNLIKELY( !ctx->wait_info ) ) return; /* not in this topology */
     237             : 
     238             :   /* Query the schedule, not ctx->next_leader_slot: there ULONG_MAX
     239             :      means already consumed, but the wire maps it to 0, which the gap
     240             :      check reads as no upcoming leader slot and passes. */
     241             : 
     242           0 :   ulong highwater = fd_ulong_if( ctx->highwater_leader_slot==ULONG_MAX, 0UL, ctx->highwater_leader_slot );
     243           0 :   ulong from = fd_ulong_max( ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot+1UL,
     244           0 :                              fd_ulong_if( !highwater, 0UL, highwater+1UL ) );
     245           0 :   int   next_leader_known = !!fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, from );
     246           0 :   ulong nl                = query_next_leader_slot( ctx, from );
     247           0 :   ulong next_leader       = nl==ULONG_MAX ? 0UL : nl;
     248           0 :   int   healthy           = wait_info_healthy( ctx->caught_up, ctx->catch_up_max_fec_slot, ctx->reset_slot );
     249             : 
     250           0 :   fd_wait_info_write_begin( ctx->wait_info );
     251           0 :   ctx->wait_info->info.caught_up                  = healthy;
     252           0 :   ctx->wait_info->info.reset_slot                 = ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot;
     253           0 :   ctx->wait_info->info.tip_slot                   = ctx->catch_up_max_fec_slot==ULONG_MAX ? 0UL : ctx->catch_up_max_fec_slot;
     254           0 :   ctx->wait_info->info.leader_highwater_slot      = highwater;
     255           0 :   ctx->wait_info->info.next_leader_known          = next_leader_known;
     256           0 :   ctx->wait_info->info.next_leader_slot           = ctx->leader_bank ? ctx->leader_bank->f.slot : next_leader;
     257           0 :   ctx->wait_info->info.leader_slot                = ctx->leader_bank ? ctx->leader_bank->f.slot : 0UL;
     258           0 :   ctx->wait_info->info.epoch_end_slot             = ctx->epoch_end_slot;
     259           0 :   ctx->wait_info->info.slots_per_epoch            = ctx->slots_per_epoch;
     260           0 :   ctx->wait_info->info.ns_per_slot                = ctx->ns_per_slot;
     261           0 :   ctx->wait_info->info.delinquent_known           = ctx->delinquent_known;
     262           0 :   ctx->wait_info->info.delinquent_stake_lamports  = ctx->delinquent_stake_lamports;
     263           0 :   ctx->wait_info->info.cluster_active_stake_lamports = ctx->cluster_active_stake_lamports;
     264           0 :   ctx->wait_info->info.snap_active                = ctx->snapmk.active;
     265           0 :   ctx->wait_info->info.snap_finished_full         = ctx->snapmk.snap_finished_full;
     266           0 :   ctx->wait_info->info.snap_finished_incr         = ctx->snapmk.snap_finished_incr;
     267           0 :   ctx->wait_info->info.snap_full_interval_blocks  = ctx->snapmk.full_interval_blocks;
     268           0 :   ctx->wait_info->info.snap_incr_interval_blocks  = ctx->snapmk.incremental_interval_blocks;
     269           0 :   ctx->wait_info->info.snap_produced_incr_cnt     = ctx->snapmk.snap_produced_incr_cnt;
     270           0 :   fd_wait_info_write_end( ctx->wait_info );
     271           0 : }
     272             : 
     273             : static inline void
     274           0 : metrics_write( fd_replay_tile_t * ctx ) {
     275           0 :   fd_accdb_flush_metrics( ctx->accdb );
     276             : 
     277           0 :   ulong store_fec_spilled    = 0UL;
     278           0 :   ulong store_fec_spill_bytes= 0UL;
     279           0 :   ulong store_fec_spill_reads= 0UL;
     280           0 :   ulong store_fec_spill_read_bytes = 0UL;
     281           0 :   ulong store_fec_cache_used = 0UL;
     282           0 :   ulong store_fec_cache_max  = 0UL;
     283           0 :   ulong store_fec_cache_pinned = 0UL;
     284           0 :   if( FD_LIKELY( ctx->store ) ) {
     285           0 :     store_fec_spilled          = FD_VOLATILE_CONST( ctx->store->fec_spill_cnt );
     286           0 :     store_fec_spill_bytes      = FD_VOLATILE_CONST( ctx->store->fec_spill_bytes );
     287           0 :     store_fec_spill_reads      = FD_VOLATILE_CONST( ctx->store->fec_spill_read_cnt );
     288           0 :     store_fec_spill_read_bytes = FD_VOLATILE_CONST( ctx->store->fec_spill_read_bytes );
     289           0 :     store_fec_cache_pinned     = FD_VOLATILE_CONST( ctx->store->cache_pinned_cnt );
     290             : 
     291           0 :     store_fec_cache_max = FD_VOLATILE_CONST( ctx->store->cache_slot_cnt );
     292           0 :     ulong store_fec_cache_free = fd_ulong_min( FD_VOLATILE_CONST( ctx->store->cache_free_cnt ), store_fec_cache_max );
     293           0 :     store_fec_cache_used = store_fec_cache_max - store_fec_cache_free;
     294           0 :   }
     295             : 
     296           0 :   FD_MHIST_COPY( REPLAY, STORE_QUERY_DURATION_NANOS,     ctx->metrics.store_query_work        );
     297           0 :   FD_MCNT_SET  ( REPLAY, STORE_FEC_QUERIED,              ctx->metrics.store_query_cnt         );
     298           0 :   FD_MCNT_SET  ( REPLAY, STORE_FEC_QUERY_MISSED,        ctx->metrics.store_query_missing_cnt );
     299           0 :   FD_MCNT_SET  ( REPLAY, STORE_FEC_SPILLED,          store_fec_spilled                    );
     300           0 :   FD_MCNT_SET  ( REPLAY, STORE_FEC_SPILLED_BYTES,      store_fec_spill_bytes                );
     301           0 :   FD_MCNT_SET  ( REPLAY, STORE_FEC_SPILL_READ,       store_fec_spill_reads                );
     302           0 :   FD_MCNT_SET  ( REPLAY, STORE_FEC_SPILL_READ_BYTES, store_fec_spill_read_bytes           );
     303           0 :   FD_MGAUGE_SET( REPLAY, STORE_FEC_CACHE_USED,       store_fec_cache_used                 );
     304           0 :   FD_MGAUGE_SET( REPLAY, STORE_FEC_CACHE_CAPACITY,   store_fec_cache_max                  );
     305           0 :   FD_MGAUGE_SET( REPLAY, STORE_FEC_CACHE_PINNED,     store_fec_cache_pinned               );
     306             : 
     307           0 :   FD_MGAUGE_SET( REPLAY, ROOT_SLOT, ctx->consensus_root_slot==ULONG_MAX ? 0UL : ctx->consensus_root_slot );
     308           0 :   ulong leader_slot = ctx->leader_bank ? ctx->leader_bank->f.slot : 0UL;
     309             : 
     310           0 :   if( FD_LIKELY( ctx->leader_bank ) ) {
     311           0 :     FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, leader_slot );
     312           0 :     FD_MGAUGE_SET( REPLAY, LEADER_SLOT, leader_slot );
     313           0 :   } else {
     314           0 :     FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, ctx->next_leader_slot==ULONG_MAX ? 0UL : ctx->next_leader_slot );
     315           0 :     FD_MGAUGE_SET( REPLAY, LEADER_SLOT, 0UL );
     316           0 :   }
     317           0 :   FD_MGAUGE_SET( REPLAY, RESET_SLOT, ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot );
     318           0 :   if( FD_UNLIKELY( ctx->reset_slot!=ULONG_MAX && ctx->consensus_root_slot!=ULONG_MAX ) ) {
     319           0 :     FD_MGAUGE_SET( REPLAY, ROOT_SLOT_DISTANCE, fd_ulong_sat_sub( ctx->reset_slot, ctx->consensus_root_slot ) );
     320           0 :   }
     321           0 :   FD_MGAUGE_SET( REPLAY, LAST_REWARDED_VOTE_SLOT, fd_ulong_if( ctx->metrics.voted_slot==ULONG_MAX, 0UL, ctx->metrics.voted_slot ) );
     322             : 
     323           0 :   FD_MGAUGE_SET( REPLAY, BANK_USED, fd_banks_pool_used_cnt( ctx->banks ) );
     324             : 
     325           0 :   FD_MCNT_SET( REPLAY, BANK_FROZEN, ctx->metrics.slots_total );
     326           0 :   FD_MGAUGE_SET( REPLAY, CHAIN_TXN_HEIGHT, ctx->metrics.transactions_total );
     327             : 
     328           0 :   FD_MGAUGE_SET( REPLAY, LAST_REASSEMBLY_DEQUEUED_SLOT,      ctx->metrics.reasm_latest_slot );
     329           0 :   FD_MGAUGE_SET( REPLAY, LAST_REASSEMBLY_DEQUEUED_FEC_INDEX, ctx->metrics.reasm_latest_fec_idx );
     330             : 
     331           0 :   fd_sched_metrics_write( ctx->sched, ctx->clock );
     332             : 
     333           0 :   FD_MCNT_SET( REPLAY, FEC_POLL_SKIPPED_SCHED_FULL,          ctx->metrics.sched_full );
     334           0 :   FD_MCNT_SET( REPLAY, FEC_POLL_SKIPPED_LEADER_FREEZE_WAIT,  ctx->metrics.leader_bid_wait );
     335           0 :   FD_MCNT_SET( REPLAY, FEC_POLL_SKIPPED_BANK_FULL,           ctx->metrics.banks_full );
     336           0 :   FD_MCNT_SET( REPLAY, FEC_PARENT_MISSING_DROPPED,           ctx->metrics.parent_unavailable );
     337           0 :   FD_MCNT_SET( REPLAY, LEADER_WINDOW_ALREADY_PRODUCED,       ctx->metrics.leader_slot_already_produced );
     338           0 :   FD_MCNT_SET( REPLAY, LEADER_WINDOW_SUPERSEDED,             ctx->metrics.leader_window_superseded     );
     339           0 :   FD_MCNT_SET( REPLAY, FEC_ROOTED_PAST_SKIPPED,              ctx->metrics.fec_rooted_past    );
     340           0 :   FD_MCNT_SET( REPLAY, ROOT_ADVANCE_DEFERRED,               ctx->metrics.storage_root_behind );
     341             : 
     342           0 :   fd_progcache_admin_metrics_t const * pcm = &fd_progcache_admin_metrics_g;
     343           0 :   FD_MCNT_SET( REPLAY, PROGCACHE_ROOTED, pcm->root_cnt );
     344             : 
     345             :   /* Shared metrics for the program cache */
     346           0 :   ulong pc_class_used[ FD_PROGCACHE_CACHE_CLASS_CNT ];
     347           0 :   ulong pc_class_max [ FD_PROGCACHE_CACHE_CLASS_CNT ];
     348           0 :   fd_progcache_cache_class_occupancy( ctx->progcache, pc_class_used, pc_class_max );
     349           0 :   FD_MGAUGE_ENUM_COPY( REPLAY, PROGCACHE_CLASS_USED,     pc_class_used );
     350           0 :   FD_MGAUGE_ENUM_COPY( REPLAY, PROGCACHE_CLASS_CAPACITY, pc_class_max  );
     351             : 
     352           0 :   FD_ACCDB_METRICS_WRITE( REPLAY, fd_accdb_metrics( ctx->accdb ) );
     353           0 : }
     354             : 
     355             : static void
     356             : metrics_trigger( fd_replay_tile_t * ctx,
     357             :                  uint               reason,
     358           0 :                  ulong              slot ) {
     359           0 :   FD_COMPILER_MFENCE();
     360           0 :   metrics_write( ctx );
     361           0 :   FD_COMPILER_MFENCE();
     362           0 :   fd_event_metrics_trigger( reason, slot, ULONG_MAX );
     363           0 : }
     364             : 
     365             : /* Agave: delinquent once the last vote is this far behind.
     366             :    https://github.com/anza-xyz/agave/blob/v4.2.1/rpc-client-types/src/request.rs#L166 */
     367             : 
     368           0 : #define DELINQUENT_VALIDATOR_SLOT_DISTANCE 128UL
     369             : 
     370             : /* Sampling interval for the O(vote accounts) stake walk, a quarter of
     371             :    DELINQUENT_VALIDATOR_SLOT_DISTANCE so the figure cannot lag far
     372             :    behind the threshold it feeds.  Stamping is not gated by it: a cert
     373             :    not stamped when it arrives is lost. */
     374             : 
     375           0 : #define DELINQUENT_SAMPLE_SLOTS 32UL
     376             : 
     377             : static ushort
     378             : replay_voter_rank( fd_replay_tile_t * ctx,
     379             :                    fd_bank_t *        bank,
     380           0 :                    ulong              epoch ) {
     381           0 :   ulong fork_id    = bank->vote_stakes_fork_id;
     382           0 :   ulong fork_epoch = fd_vote_stakes_fork_epoch( fork_id );
     383           0 :   int   iter_kind  = FD_VOTE_STAKES_ITER_T_2;
     384           0 :   if( FD_UNLIKELY( epoch!=fork_epoch ) ) {
     385           0 :     if( FD_UNLIKELY( !fork_epoch || epoch!=fork_epoch-1UL ) ) return USHORT_MAX;
     386           0 :     iter_kind = FD_VOTE_STAKES_ITER_T_3;
     387           0 :   }
     388             : 
     389           0 :   fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
     390           0 :   uchar __attribute__((aligned(FD_VOTE_STAKES_ITER_ALIGN))) iter_mem[ FD_VOTE_STAKES_ITER_FOOTPRINT ];
     391           0 :   for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_iter_init( vote_stakes, fork_id, iter_kind, iter_mem );
     392           0 :        !fd_vote_stakes_iter_done( vote_stakes, fork_id, iter_kind, iter );
     393           0 :        fd_vote_stakes_iter_next( vote_stakes, fork_id, iter_kind, iter ) ) {
     394           0 :     fd_vote_stakes_ele_t ele[1];
     395           0 :     fd_vote_stakes_iter_ele( vote_stakes, fork_id, iter_kind, iter, ele );
     396           0 :     if( FD_UNLIKELY( fd_pubkey_eq( &ele->node_account, ctx->identity_pubkey ) ) ) return ele->rank;
     397           0 :   }
     398           0 :   return USHORT_MAX;
     399           0 : }
     400             : 
     401             : /* ag_rank_is_current returns 1 if the rank's last observed vote is
     402             :    recent enough. */
     403             : 
     404             : static inline int
     405             : ag_rank_is_current( ulong last_settled,
     406           0 :                     ulong rank_last_voted ) {
     407           0 :   return fd_ulong_sat_sub( last_settled, rank_last_voted )<DELINQUENT_VALIDATOR_SLOT_DISTANCE;
     408           0 : }
     409             : 
     410             : /* ag_delinquent_epoch_roll clears the rank table when the epoch
     411             :    advances and raises the settled watermark.  Ranks are remapped across
     412             :    the rollover, so the previous epoch's figure no longer describes the
     413             :    current stake set and reads unknown until a sample repopulates the
     414             :    table.  The watermark cannot regress. */
     415             : 
     416             : static void
     417             : ag_delinquent_epoch_roll( fd_replay_tile_t * ctx,
     418             :                           ulong              reward_slot,
     419           0 :                           ulong              reward_epoch ) {
     420           0 :   if( FD_UNLIKELY( ctx->ag_last_voted_epoch==ULONG_MAX ||
     421           0 :                    reward_epoch>ctx->ag_last_voted_epoch ) ) {
     422           0 :     fd_memset( ctx->ag_last_voted, 0, sizeof(ctx->ag_last_voted) );
     423           0 :     ctx->ag_last_voted_epoch = reward_epoch;
     424           0 :     ctx->delinquent_known    = 0;
     425           0 :     ctx->ag_stamp_since      = ULONG_MAX;
     426           0 :   }
     427           0 :   ctx->ag_last_settled = fd_ulong_max( ctx->ag_last_settled, reward_slot );
     428           0 : }
     429             : 
     430             : /* ag_update_delinquent stamps the reward cert's signers into
     431             :    ag_last_voted and recomputes delinquent stake. */
     432             : 
     433             : static void
     434             : ag_update_delinquent( fd_replay_tile_t *   ctx,
     435             :                       fd_bank_t const *    bank,
     436             :                       ulong                reward_slot,
     437             :                       ulong                reward_epoch,
     438           0 :                       fd_bls_set_t const * reward_set ) {
     439           0 :   if( FD_UNLIKELY( !ctx->wait_info ) ) return; /* not in this topology */
     440             : 
     441           0 :   ulong fork_id    = bank->vote_stakes_fork_id;
     442           0 :   ulong fork_epoch = fd_vote_stakes_fork_epoch( fork_id );
     443             : 
     444             :   /* Roll on the fork's epoch, not the cert's: the cert trails the bank
     445             :      by FD_NUM_SLOTS_FOR_REWARD slots, so it still names the old epoch
     446             :      over the first slots of a new one. */
     447           0 :   ag_delinquent_epoch_roll( ctx, ctx->ag_last_settled, fork_epoch );
     448             : 
     449             :   /* Only T-2 exposes stake and is_valid. */
     450           0 :   if( FD_UNLIKELY( reward_epoch!=fork_epoch ) ) return;
     451             : 
     452             :   /* Ranks are epoch-scoped, and forks can complete an epoch-E block
     453             :      after an epoch-E+1 one. */
     454           0 :   if( FD_UNLIKELY( ctx->ag_last_voted_epoch!=ULONG_MAX &&
     455           0 :                    reward_epoch<ctx->ag_last_voted_epoch ) ) return;
     456             : 
     457             :   /* Raise the watermark, so stamps age out as certs settle. */
     458           0 :   ag_delinquent_epoch_roll( ctx, reward_slot, reward_epoch );
     459             : 
     460             :   /* An empty cert is absence of evidence, so it stamps nothing.  With no
     461             :      figure yet there is nothing to age either, but once one is known the
     462             :      walk below must still run: the roll above advanced ag_last_settled,
     463             :      so existing stamps age out even while certs are absent. */
     464           0 :   if( FD_UNLIKELY( fd_bls_set_is_null( reward_set ) && !ctx->delinquent_known ) ) return;
     465             : 
     466           0 :   if( FD_UNLIKELY( ctx->ag_stamp_since==ULONG_MAX ) ) ctx->ag_stamp_since = reward_slot;
     467             : 
     468             :   /* Stamp straight off the cert: O(signers), no vote stakes walk.  No
     469             :      need to filter: entries the walk skips are never read. */
     470           0 :   for( ulong rank=fd_bls_set_const_iter_init( reward_set );
     471           0 :        !fd_bls_set_const_iter_done( rank );
     472           0 :        rank=fd_bls_set_const_iter_next( reward_set, rank ) ) {
     473           0 :     if( FD_UNLIKELY( rank>=AG_VAT_MAX ) ) break; /* FD_BLS_SET_MAX is sized separately */
     474           0 :     ctx->ag_last_voted[ rank ] = fd_ulong_max( ctx->ag_last_voted[ rank ], reward_slot );
     475           0 :   }
     476             : 
     477           0 :   if( FD_LIKELY( ctx->delinquent_known &&
     478           0 :                  reward_slot<ctx->delinquent_sample_slot+DELINQUENT_SAMPLE_SLOTS ) ) return;
     479           0 :   if( FD_UNLIKELY( reward_slot<ctx->ag_stamp_since+DELINQUENT_SAMPLE_SLOTS ) ) return;
     480             :   /* Slots arrive out of order across forks, so hold the watermark. */
     481           0 :   ctx->delinquent_sample_slot = fd_ulong_max( ctx->delinquent_sample_slot, reward_slot );
     482             : 
     483           0 :   fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
     484           0 :   ulong              delinquent  = 0UL;
     485           0 :   ulong              total       = 0UL;
     486             : 
     487           0 :   uchar __attribute__((aligned(FD_VOTE_STAKES_ITER_ALIGN))) iter_mem[ FD_VOTE_STAKES_ITER_FOOTPRINT ];
     488           0 :   for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_iter_init( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2, iter_mem );
     489           0 :        !fd_vote_stakes_iter_done( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2, iter );
     490           0 :        fd_vote_stakes_iter_next( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2, iter ) ) {
     491           0 :     fd_vote_stakes_ele_t ele[1];
     492           0 :     fd_vote_stakes_iter_ele( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2, iter, ele );
     493           0 :     ulong  stake = ele->stake;
     494           0 :     ushort rank  = ele->rank;
     495           0 :     if( FD_UNLIKELY( !ele->is_valid || rank==FD_VOTE_STAKES_ALPENGLOW_RANK_NULL ) ) continue;
     496           0 :     FD_TEST( rank<AG_VAT_MAX );
     497             : 
     498           0 :     total += stake;
     499           0 :     if( FD_UNLIKELY( !ag_rank_is_current( ctx->ag_last_settled, ctx->ag_last_voted[ rank ] ) ) ) {
     500           0 :       delinquent += stake;
     501           0 :     }
     502           0 :   }
     503             : 
     504           0 :   ctx->delinquent_stake_lamports     = delinquent;
     505           0 :   ctx->cluster_active_stake_lamports = total;
     506             : 
     507             :   /* Over-counts delinquency, so a partial union blocks rather than
     508             :      passes. */
     509           0 :   ctx->delinquent_known = 1;
     510           0 : }
     511             : 
     512             : static int
     513             : replay_reward_cert_voted( fd_replay_tile_t * ctx,
     514             :                           fd_bank_t *        bank,
     515             :                           ushort *           rank_out,
     516           0 :                           ushort *           count_out ) {
     517           0 :   *rank_out = USHORT_MAX;
     518           0 :   *count_out = USHORT_MAX;
     519           0 :   if( FD_UNLIKELY( bank->f.slot<FD_NUM_SLOTS_FOR_REWARD ) ) return 0;
     520             : 
     521           0 :   ulong  reward_slot  = bank->f.slot-FD_NUM_SLOTS_FOR_REWARD;
     522           0 :   ulong  reward_epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, reward_slot, NULL );
     523           0 :   ushort rank         = replay_voter_rank( ctx, bank, reward_epoch );
     524           0 :   *rank_out = rank;
     525             : 
     526           0 :   fd_block_footer_t const * footer = bank==ctx->leader_bank ? ctx->leader_footer : fd_sched_get_footer( ctx->sched, bank->idx );
     527           0 :   if( FD_UNLIKELY( !footer ) ) return 0;
     528             : 
     529             :   /* A validator included in both reward certificates is counted once. */
     530           0 :   fd_bls_set_t reward_set[ fd_bls_set_word_cnt ];
     531           0 :   fd_bls_set_null( reward_set );
     532           0 :   if( FD_UNLIKELY( footer->has_skip_reward_cert ) ) fd_bls_set_union( reward_set, reward_set, footer->skip_reward_cert.signer_set  );
     533           0 :   if( FD_LIKELY( footer->has_notar_reward_cert ) ) fd_bls_set_union( reward_set, reward_set, footer->notar_reward_cert.signer_set );
     534           0 :   *count_out = (ushort)fd_bls_set_cnt( reward_set );
     535             : 
     536           0 :   ag_update_delinquent( ctx, bank, reward_slot, reward_epoch, reward_set );
     537             : 
     538           0 :   if( FD_UNLIKELY( rank==USHORT_MAX ) ) return 0;
     539             : 
     540             :   /* bits at or past the cert's nbits are left clear at decode, so the
     541             :      set test alone bounds the rank */
     542           0 :   int in_cert = fd_bls_set_test( reward_set, rank );
     543             : 
     544           0 :   if( FD_UNLIKELY( in_cert && ( ctx->metrics.voted_slot==ULONG_MAX || reward_slot>ctx->metrics.voted_slot ) ) ) ctx->metrics.voted_slot = reward_slot;
     545           0 :   return in_cert;
     546           0 : }
     547             : 
     548             : static void
     549             : publish_epoch_info( fd_replay_tile_t *  ctx,
     550             :                     fd_stem_context_t * stem,
     551             :                     fd_bank_t *         bank,
     552           0 :                     int                 next_epoch ) {
     553           0 :   fd_epoch_schedule_t const * schedule = &bank->f.epoch_schedule;
     554           0 :   ulong epoch = fd_slot_to_epoch( schedule, bank->f.slot, NULL ) + fd_ulong_if( next_epoch, 1UL, 0UL );
     555             : 
     556           0 :   fd_features_t const * features = &bank->f.features;
     557             : 
     558           0 :   fd_runtime_stack_t * runtime_stack = ctx->runtime_stack;
     559             : 
     560           0 :   fd_epoch_info_msg_t * epoch_info_msg = fd_chunk_to_laddr( ctx->epoch_out->mem, ctx->epoch_out->chunk );
     561             : 
     562           0 :   epoch_info_msg->staked_vote_cnt   = next_epoch ? runtime_stack->epoch_weights.next_stake_weights_cnt : runtime_stack->epoch_weights.stake_weights_cnt;
     563           0 :   epoch_info_msg->staked_id_cnt     = next_epoch ? runtime_stack->epoch_weights.next_id_weights_cnt    : runtime_stack->epoch_weights.id_weights_cnt;
     564           0 :   epoch_info_msg->epoch_schedule    = *schedule;
     565           0 :   epoch_info_msg->features          = *features;
     566           0 :   epoch_info_msg->epoch             = epoch;
     567           0 :   epoch_info_msg->start_slot        = fd_epoch_slot0( schedule, epoch );
     568           0 :   epoch_info_msg->slot_cnt          = fd_epoch_slot_cnt( schedule, epoch );
     569           0 :   epoch_info_msg->ns_per_slot       = fd_slot_params_at_slot( bank, epoch_info_msg->start_slot ).ns_per_slot;
     570             : 
     571           0 :   fd_vote_stake_weight_t * stake_weights = fd_type_pun( epoch_info_msg + 1 );
     572           0 :   fd_vote_stake_weight_t * src_stake_weights = next_epoch ? runtime_stack->epoch_weights.next_stake_weights : runtime_stack->epoch_weights.stake_weights;
     573           0 :   memcpy( stake_weights, src_stake_weights, epoch_info_msg->staked_vote_cnt * sizeof(fd_vote_stake_weight_t) );
     574             : 
     575           0 :   fd_stake_weight_t * id_weights = fd_epoch_info_msg_id_weights( epoch_info_msg );
     576           0 :   fd_stake_weight_t * src_id_weights = next_epoch ? runtime_stack->epoch_weights.next_id_weights : runtime_stack->epoch_weights.id_weights;
     577           0 :   fd_memcpy( id_weights, src_id_weights, epoch_info_msg->staked_id_cnt * sizeof(fd_stake_weight_t) );
     578             : 
     579           0 :   ulong epoch_info_sz = fd_epoch_info_msg_sz( epoch_info_msg->staked_vote_cnt, epoch_info_msg->staked_id_cnt );
     580           0 :   ulong epoch_info_sig = 4UL;
     581           0 :   fd_stem_publish( stem, ctx->epoch_out->idx, epoch_info_sig, ctx->epoch_out->chunk, epoch_info_sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     582           0 :   ctx->epoch_out->chunk = fd_dcache_compact_next( ctx->epoch_out->chunk, epoch_info_sz, ctx->epoch_out->chunk0, ctx->epoch_out->wmark );
     583             : 
     584           0 :   fd_multi_epoch_leaders_epoch_msg_init( ctx->mleaders, epoch_info_msg );
     585           0 :   fd_multi_epoch_leaders_epoch_msg_fini( ctx->mleaders );
     586             : 
     587           0 :   ctx->next_leader_query_start = ULONG_MAX;
     588           0 : }
     589             : 
     590             : static void
     591             : publish_replay_out( fd_replay_tile_t *  ctx,
     592             :                     fd_stem_context_t * stem,
     593             :                     ulong               sig,
     594           0 :                     ulong               sz ) {
     595           0 :   ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
     596           0 :   if( FD_UNLIKELY( sig!=REPLAY_SIG_TXN_EXECUTED ) ) {
     597           0 :     fd_replay_out_link_t * out = sig==REPLAY_SIG_MISSING_FEC ? ctx->rotor_out : ctx->slot_out;
     598           0 :     if( FD_LIKELY( out->idx!=ULONG_MAX ) ) { /* backtest has no rotor */
     599           0 :       fd_memcpy( fd_chunk_to_laddr( out->mem, out->chunk ), fd_chunk_to_laddr_const( ctx->replay_out->mem, ctx->replay_out->chunk ), sz );
     600           0 :       fd_stem_publish( stem, out->idx, sig, out->chunk, sz, 0UL, 0UL, tspub );
     601           0 :       out->chunk = fd_dcache_compact_next( out->chunk, sz, out->chunk0, out->wmark );
     602           0 :     }
     603           0 :   }
     604           0 :   fd_stem_publish( stem, ctx->replay_out->idx, sig, ctx->replay_out->chunk, sz, 0UL, 0UL, tspub );
     605           0 :   ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sz, ctx->replay_out->chunk0, ctx->replay_out->wmark );
     606           0 : }
     607             : 
     608             : /**********************************************************************/
     609             : /* Transaction execution state machine helpers                        */
     610             : /**********************************************************************/
     611             : 
     612             : static inline void
     613             : timing_slot_release( fd_replay_tile_t * ctx,
     614           0 :                      ulong              bank_idx ) {
     615           0 :   ulong tslot = ctx->timing_slot_of_bank[ bank_idx ];
     616           0 :   if( FD_LIKELY( tslot!=fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) ) ) {
     617           0 :     fd_timing_slot_pool_idx_release( ctx->timing_slot_pool, tslot );
     618           0 :     ctx->timing_slot_of_bank[ bank_idx ] = fd_timing_slot_pool_idx_null( ctx->timing_slot_pool );
     619           0 :   }
     620           0 : }
     621             : 
     622             : static void
     623             : replay_block_start( fd_replay_tile_t * ctx,
     624             :                     ulong              bank_idx,
     625             :                     ulong              parent_bank_idx,
     626           0 :                     ulong              slot ) {
     627           0 :   long before = fd_clock_tile_now( ctx->clock );
     628             : 
     629           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
     630           0 :   FD_CHECK_CRIT( bank, "invariant violation: bank is NULL" );
     631           0 :   FD_CHECK_CRIT( bank->state==FD_BANK_STATE_INIT, "invariant violation: bank is not in correct state" );
     632             : 
     633           0 :   bank->preparation_begin_nanos = before;
     634             : 
     635           0 :   FD_TEST( ctx->timing_slot_of_bank[ bank_idx ]==fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) );
     636           0 :   if( FD_LIKELY( fd_timing_slot_pool_free( ctx->timing_slot_pool ) ) ) {
     637           0 :     ulong tslot = fd_timing_slot_pool_idx_acquire( ctx->timing_slot_pool );
     638           0 :     fd_timing_slot_pool_ele( ctx->timing_slot_pool, tslot )->cnt = 0UL;
     639           0 :     ctx->timing_slot_of_bank[ bank_idx ] = tslot;
     640           0 :   }
     641             : 
     642           0 :   fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
     643           0 :   FD_CHECK_CRIT( parent_bank, "invariant violation: parent bank is NULL" );
     644           0 :   FD_CHECK_CRIT( parent_bank->state==FD_BANK_STATE_FROZEN || parent_bank->state==FD_BANK_STATE_PRUNABLE, "invariant violation: parent bank is not in correct state" );
     645             : 
     646             :   /* Clone the bank from the parent.  We must special case the first
     647             :      slot that is executed as the snapshot does not provide a parent
     648             :      block id. */
     649             : 
     650           0 :   bank = fd_banks_clone_from_parent( ctx->banks, bank_idx );
     651           0 :   if( FD_UNLIKELY( !bank ) ) {
     652           0 :     FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", bank_idx ));
     653           0 :   }
     654           0 :   fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, slot );
     655           0 :   FD_TEST( leader );
     656           0 :   bank->f.slot               = slot;
     657           0 :   bank->leader               = *leader;
     658           0 :   bank->txncache_fork_id     = fd_txncache_attach_child ( ctx->txncache,  parent_bank->txncache_fork_id  );
     659           0 :   bank->progcache_fork_id    = fd_progcache_attach_child( ctx->progcache, parent_bank->progcache_fork_id );
     660           0 :   bank->accdb_fork_id        = fd_accdb_attach_child    ( ctx->accdb,     parent_bank->accdb_fork_id     );
     661           0 :   bank->parent_accdb_fork_id = parent_bank->accdb_fork_id;
     662             : 
     663           0 :   ulong new_epoch  = fd_slot_to_epoch( &parent_bank->f.epoch_schedule, slot, NULL );
     664           0 :   ulong root_epoch = fd_slot_to_epoch( &parent_bank->f.epoch_schedule, ctx->published_root_slot, NULL );
     665           0 :   if( FD_UNLIKELY( new_epoch>root_epoch+1UL ) ) {
     666           0 :     FD_LOG_CRIT(( "firedancer replay does not support replaying more than one epoch ahead of the current root" ));
     667           0 :   }
     668             : 
     669             :   /* Update required runtime state and handle potential boundary. */
     670             : 
     671           0 :   int is_epoch_boundary = 0;
     672           0 :   fd_runtime_block_execute_prepare( ctx->banks, bank, ctx->accdb, ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
     673           0 :   FD_CHECK_CRIT( !is_epoch_boundary || FD_FEATURE_ACTIVE_BANK( bank, alpenglow ), "alpenglow feature is not active" );
     674             : 
     675           0 :   ulong max_tick_height;
     676           0 :   if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS!=fd_runtime_compute_max_tick_height( parent_bank->f.ticks_per_slot, slot, &max_tick_height ) ) ) {
     677           0 :     FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, parent_bank->f.ticks_per_slot ));
     678           0 :   }
     679           0 :   bank->f.max_tick_height = max_tick_height;
     680             :   /* in alpenglow, we expect only one tick per block.  Instead of
     681             :      adjusting max tick height, we match agave behavior by setting
     682             :      tick height to max tick height - 1. These fields must stay in
     683             :      line with agave behavior. */
     684           0 :   bank->f.tick_height = bank->f.max_tick_height - 1UL;
     685           0 :   bank->f.slot_params.hashes_per_tick = 1UL;
     686           0 :   fd_sched_set_poh_params( ctx->sched, bank->idx, bank->f.tick_height, bank->f.max_tick_height, bank->f.slot_params.hashes_per_tick, &parent_bank->f.poh );
     687             : 
     688           0 :   FD_LOG_DEBUG(( "replay_block_start: bank_idx=%lu slot=%lu parent_bank_idx=%lu", bank_idx, slot, parent_bank_idx ));
     689           0 : }
     690             : 
     691             : static void
     692           0 : cost_tracker_snap( fd_bank_t * bank, fd_replay_slot_completed_t * slot_info ) {
     693           0 :   if( FD_LIKELY( bank->cost_tracker_fork_id!=USHORT_MAX ) ) {
     694           0 :     fd_cost_tracker_t const * cost_tracker = fd_cost_tracker_store_pin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
     695           0 :     if( FD_UNLIKELY( cost_tracker->block_cost_limit==0UL ) ) {
     696           0 :       memset( &slot_info->cost_tracker, -1 /* ULONG_MAX */, sizeof(slot_info->cost_tracker) );
     697           0 :     } else {
     698           0 :       slot_info->cost_tracker.block_cost                   = cost_tracker->block_cost;
     699           0 :       slot_info->cost_tracker.allocated_accounts_data_size = cost_tracker->allocated_accounts_data_size;
     700           0 :       slot_info->cost_tracker.block_cost_limit             = cost_tracker->block_cost_limit;
     701           0 :       slot_info->cost_tracker.account_cost_limit           = cost_tracker->account_cost_limit;
     702           0 :     }
     703           0 :     fd_cost_tracker_store_unpin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
     704           0 :   } else {
     705           0 :     memset( &slot_info->cost_tracker, -1 /* ULONG_MAX */, sizeof(slot_info->cost_tracker) );
     706           0 :   }
     707           0 :   slot_info->cost_tracker.pool_idx = fd_ulong_if( bank->cost_tracker_fork_id==USHORT_MAX, ULONG_MAX, bank->cost_tracker_fork_id );
     708           0 : }
     709             : 
     710             : static int
     711           0 : sched_dead_reason_to_event( int sched_reason ) {
     712           0 :   switch( sched_reason ) {
     713           0 :     case FD_SCHED_DEAD_REASON_UNPARSEABLE_CONTENT:         return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_UNPARSEABLE_CONTENT;
     714           0 :     case FD_SCHED_DEAD_REASON_SHORT_BLOCK:                 return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_SHORT_BLOCK;
     715           0 :     case FD_SCHED_DEAD_REASON_TOO_MANY_TXNS:               return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_MANY_TXNS;
     716           0 :     case FD_SCHED_DEAD_REASON_TOO_MANY_MICROBLOCKS:        return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_MANY_MICROBLOCKS;
     717           0 :     case FD_SCHED_DEAD_REASON_DUPLICATE_ACCOUNT:           return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_DUPLICATE_ACCOUNT;
     718           0 :     case FD_SCHED_DEAD_REASON_TRAILING_ENTRY:              return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TRAILING_ENTRY;
     719           0 :     case FD_SCHED_DEAD_REASON_TOO_MANY_TICKS:              return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_MANY_TICKS;
     720           0 :     case FD_SCHED_DEAD_REASON_TOO_FEW_TICKS:               return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_FEW_TICKS;
     721           0 :     case FD_SCHED_DEAD_REASON_ZERO_MICROBLOCKS:            return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ZERO_MICROBLOCKS;
     722           0 :     case FD_SCHED_DEAD_REASON_WRONG_HASHES_PER_TICK:       return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_WRONG_HASHES_PER_TICK;
     723           0 :     case FD_SCHED_DEAD_REASON_INCONSISTENT_TICK_HASHES:    return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_INCONSISTENT_TICK_HASHES;
     724           0 :     case FD_SCHED_DEAD_REASON_TICK_HASHES_OVERFLOW:        return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TICK_HASHES_OVERFLOW;
     725           0 :     case FD_SCHED_DEAD_REASON_TICK_HASHES_OVERFLOW_INGEST: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TICK_HASHES_OVERFLOW_INGEST;
     726           0 :     case FD_SCHED_DEAD_REASON_ZERO_HASH_TICK:              return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ZERO_HASH_TICK;
     727           0 :     case FD_SCHED_DEAD_REASON_ZERO_HASH_TICK_INGEST:       return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ZERO_HASH_TICK_INGEST;
     728           0 :     case FD_SCHED_DEAD_REASON_TICK_HASH_MISMATCH:          return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TICK_HASH_MISMATCH;
     729           0 :     case FD_SCHED_DEAD_REASON_ENTRY_HASH_MISMATCH:         return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ENTRY_HASH_MISMATCH;
     730           0 :     case FD_SCHED_DEAD_REASON_ENTRY_HASH_MISMATCH_INGEST:  return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ENTRY_HASH_MISMATCH_INGEST;
     731           0 :     case FD_SCHED_DEAD_REASON_DEAD_ANCESTOR:               return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_PARENT_DEAD;
     732           0 :     case FD_SCHED_DEAD_REASON_BAD_BLOCK_MARKER:            return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_BLOCK_MARKER;
     733           0 :     case FD_SCHED_DEAD_REASON_ALPENGLOW_HASH_CNT:          return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ALPENGLOW_HASH_CNT;
     734           0 :     case FD_SCHED_DEAD_REASON_MISSING_PARENT_MARKER:       return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MISSING_PARENT_MARKER;
     735           0 :     case FD_SCHED_DEAD_REASON_MULTIPLE_BLOCK_HEADERS:      return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MULTIPLE_BLOCK_HEADERS;
     736           0 :     case FD_SCHED_DEAD_REASON_GENESIS_CERT_OUT_OF_ORDER:   return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_GENESIS_CERT_OUT_OF_ORDER;
     737           0 :     case FD_SCHED_DEAD_REASON_MULTIPLE_BLOCK_FOOTERS:      return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MULTIPLE_BLOCK_FOOTERS;
     738           0 :     case FD_SCHED_DEAD_REASON_ENTRY_AFTER_BLOCK_FOOTER:    return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ENTRY_AFTER_BLOCK_FOOTER;
     739           0 :     case FD_SCHED_DEAD_REASON_INVALID_ALPENTICK_POSITION:  return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_INVALID_ALPENTICK_POSITION;
     740           0 :     case FD_SCHED_DEAD_REASON_MISSING_BLOCK_FOOTER:        return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MISSING_BLOCK_FOOTER;
     741           0 :     case FD_SCHED_DEAD_REASON_SPURIOUS_UPDATE_PARENT:      return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_SPURIOUS_UPDATE_PARENT;
     742           0 :     default: FD_LOG_CRIT(( "unmapped scheduler dead reason %d", sched_reason ));
     743           0 :   }
     744           0 : }
     745             : 
     746             : static int
     747           0 : sched_block_dead_reason_to_event( fd_replay_tile_t * ctx, ulong bank_idx ) {
     748           0 :   if( FD_UNLIKELY( fd_sched_block_is_discarded( ctx->sched, bank_idx ) ) ) return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD;
     749           0 :   return sched_dead_reason_to_event( fd_sched_get_dead_reason( ctx->sched, bank_idx ) );
     750           0 : }
     751             : 
     752             : /* The stats array is keyed by bank idx. */
     753             : 
     754             : static void
     755             : block_completed_event_fill_reception( fd_replay_tile_t *           ctx,
     756             :                                       fd_event_block_completed_t * ev,
     757           0 :                                       ulong                        bank_idx ) {
     758           0 :   ev->lowest_verified_fec_index    = 0U;
     759           0 :   ev->last_completed_fec_set_index = UINT_MAX;
     760           0 :   fd_rotor_fec_metrics_t const * rm = NULL;
     761             : 
     762           0 :     fd_bank_t const * bank = bank_idx==ULONG_MAX ? NULL : fd_banks_bank_query( ctx->banks, bank_idx );
     763           0 :     if( FD_UNLIKELY( !bank ) ) return;
     764             : 
     765           0 :     fd_block_id_ele_t const * ele = &ctx->block_id_arr[ bank_idx ];
     766           0 :     ev->fec_set_count = ele->fec_cnt;
     767             : 
     768           0 :     if( FD_LIKELY( bank->parent_idx!=ULONG_MAX ) ) {
     769           0 :       fd_block_id_ele_t const * parent = &ctx->block_id_arr[ bank->parent_idx ];
     770           0 :       fd_memcpy( ev->parent_block_id, parent->dmr.uc, sizeof(ev->parent_block_id) );
     771           0 :       if( !ev->parent_slot ) ev->parent_slot = parent->slot;
     772           0 :     }
     773             : 
     774             :     /* A mismatched bank_seq means the entry belongs to a previous
     775             :        occupant of this bank idx, i.e. we have no stats for this block. */
     776           0 :     fd_reception_stats_t const * s = &ctx->reception_stats[ bank_idx ];
     777           0 :     if( FD_UNLIKELY( s->bank_seq!=bank->bank_seq ) ) return;
     778           0 :     rm = &s->metrics.rotor;
     779           0 :     ev->votor_repaired = !!rm->votor_repaired;
     780             : 
     781           0 :     if( FD_UNLIKELY( !rm->stats_valid ) ) return;
     782             : 
     783           0 :     ev->turbine_shred_count   = rm->blk_turbine_cnt;
     784           0 :     ev->repair_shred_count    = rm->blk_repair_cnt;
     785           0 :     ev->recovered_shred_count = rm->blk_recovered_cnt;
     786           0 :     ev->data_shred_count      = rm->blk_data_cnt;
     787           0 :     ev->parity_shred_count    = rm->blk_parity_cnt;
     788           0 :     ev->slot_complete_flag    = !!rm->blk_slot_complete;
     789             : 
     790           0 :     ev->last_completed_fec_set_index        = rm->blk_last_completed_fec_idx;
     791           0 :     ev->repair_request_window_count         = rm->blk_req_window_cnt;
     792           0 :     ev->repair_request_highest_window_count = rm->blk_req_highest_cnt;
     793           0 :     ev->repair_request_orphan_count         = rm->blk_req_orphan_cnt;
     794           0 :     ev->repair_request_shred_block_id_count = rm->blk_req_shred_bid_cnt;
     795           0 :     ev->repair_request_parent_count         = rm->blk_req_parent_cnt;
     796           0 :     ev->repair_request_fec_root_count       = rm->blk_req_fec_root_cnt;
     797           0 :     ev->repair_requests_retransmitted       = rm->blk_req_retransmit_cnt;
     798           0 :     ev->repair_responses_received           = rm->blk_repair_responses;
     799           0 :     ev->first_shred_received_time           = rm->blk_first_shred_ts_nanos;
     800           0 :     ev->last_shred_received_time            = rm->blk_last_shred_ts_nanos;
     801           0 :     ev->first_repair_request_time           = rm->blk_first_req_ts_nanos;
     802           0 :     ev->last_repair_received_time           = rm->blk_last_repair_resp_ts_nanos;
     803             : 
     804             :     /* always zero for alpenglow*/
     805           0 :     ev->lowest_verified_fec_index  = 0;
     806           0 :     ev->repair_failed_chain_verify = 0;
     807           0 :     ev->chain_confirmed            = 0;
     808           0 : }
     809             : 
     810             : static void
     811             : block_completed_event_fill_bank( fd_replay_tile_t *           ctx,
     812             :                                  fd_event_block_completed_t * ev,
     813           0 :                                  fd_bank_t *                  bank ) {
     814           0 :   fd_bank_t * parent_bank = fd_banks_get_parent( ctx->banks, bank );
     815             : 
     816           0 :   int   prepared = bank->preparation_begin_nanos!=0L;
     817           0 :   ulong slot     = prepared ? bank->f.slot : ctx->block_id_arr[ bank->idx ].slot;
     818             : 
     819           0 :   ev->bank_seq    = bank->bank_seq;
     820           0 :   ev->slot        = slot;
     821           0 :   ev->parent_slot = prepared                    ? bank->f.parent_slot
     822           0 :                   : bank->parent_idx!=ULONG_MAX ? ctx->block_id_arr[ bank->parent_idx ].slot
     823           0 :                   :                               0UL;
     824           0 :   if(      FD_LIKELY( prepared )                ) ev->epoch = bank->f.epoch;
     825           0 :   else if( FD_LIKELY( ctx->notified_root_bank ) ) ev->epoch = fd_slot_to_epoch( &ctx->notified_root_bank->f.epoch_schedule, slot, NULL );
     826           0 :   fd_block_id_ele_t const * ele = &ctx->block_id_arr[ bank->idx ];
     827           0 :   fd_memcpy( ev->block_id, ele->block_id_seen ? ele->dmr.uc : ele->latest_mr.uc, sizeof(ev->block_id) );
     828           0 :   if( FD_LIKELY( bank->block_completed_nanos ) ) fd_memcpy( ev->bank_hash, bank->f.bank_hash.uc, sizeof(ev->bank_hash) );
     829             : 
     830           0 :   ev->first_fec_set_received_time      = bank->is_leader ? 0UL : (ulong)bank->first_fec_set_received_nanos;
     831           0 :   ev->preparation_begin_time           = (ulong)bank->preparation_begin_nanos;
     832           0 :   ev->first_transaction_scheduled_time = (ulong)bank->first_transaction_scheduled_nanos;
     833           0 :   ev->last_transaction_finished_time   = (ulong)bank->last_transaction_finished_nanos;
     834           0 :   ev->block_completed_time             = (ulong)bank->block_completed_nanos;
     835           0 :   ev->parent_block_completed_time      = parent_bank ? (ulong)parent_bank->block_completed_nanos : 0UL;
     836             : 
     837           0 :   if( FD_UNLIKELY( bank->cost_tracker_fork_id!=USHORT_MAX ) ) {
     838           0 :     fd_cost_tracker_t const * ct = fd_cost_tracker_store_pin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
     839           0 :     ev->cost_tracker_block_cost                   = ct->block_cost;
     840           0 :     ev->cost_tracker_allocated_accounts_data_size = ct->allocated_accounts_data_size;
     841           0 :     ev->cost_tracker_block_cost_limit             = ct->block_cost_limit;
     842           0 :     ev->cost_tracker_account_cost_limit           = ct->account_cost_limit;
     843           0 :     fd_cost_tracker_store_unpin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
     844           0 :   }
     845             : 
     846           0 :   ev->bank_idx = bank->idx;
     847           0 :   if( FD_LIKELY( prepared ) ) {
     848           0 :     ev->txncache_fork_id            = bank->txncache_fork_id.val;
     849           0 :     ev->progcache_fork_id           = bank->progcache_fork_id;
     850           0 :     ev->accdb_fork_id               = bank->accdb_fork_id.val;
     851           0 :     ev->vote_stakes_fork_id         = bank->vote_stakes_fork_id;
     852           0 :     ev->stake_rewards_fork_id       = bank->stake_rewards_fork_id;
     853           0 :     ev->epoch_credits_fork_id       = bank->epoch_credits_fork_id;
     854           0 :     ev->stake_delegations_fork_id   = bank->stake_delegations_fork_id;
     855           0 :     ev->cost_tracker_pool_idx       = fd_ulong_if( bank->cost_tracker_fork_id==USHORT_MAX, ULONG_MAX, bank->cost_tracker_fork_id );
     856           0 :   }
     857             : 
     858           0 :   block_completed_event_fill_reception( ctx, ev, bank->idx );
     859           0 : }
     860             : 
     861             : static int
     862           0 : pack_end_reason_to_event( int reason ) {
     863           0 :   switch( reason ) {
     864           0 :     case FD_PACK_END_SLOT_REASON_TIME:       return FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_TIME;
     865           0 :     case FD_PACK_END_SLOT_REASON_MICROBLOCK: return FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_MICROBLOCK_LIMIT;
     866           0 :     case FD_PACK_END_SLOT_REASON_ABANDONED:  return FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_ABANDONED;
     867           0 :     default:                                 FD_LOG_CRIT(( "unmapped pack end reason %d", reason ));
     868           0 :   }
     869           0 : }
     870             : 
     871             : static void
     872             : block_completed_event_fill_leader( fd_replay_tile_t *           ctx,
     873             :                                    fd_event_block_completed_t * ev,
     874           0 :                                    fd_bank_t *                  bank ) {
     875           0 :   FD_TEST( ctx->leader_stats.slot==bank->f.slot );
     876             : 
     877           0 :   ev->became_leader_time     = (ulong)ctx->leader_stats.became_leader_nanos;
     878           0 :   ev->leader_slot_start_time = (ulong)ctx->leader_stats.leader_slot_start_nanos;
     879             : 
     880           0 :   ev->first_fec_set_received_time = (ulong)ctx->leader_stats.first_fec_returned_nanos;
     881           0 :   ev->recovered_shred_count = 0UL;
     882             : 
     883           0 :   ev->microblock_count = ctx->leader_stats.microblock_count;
     884           0 :   ev->pack_block_cost  = ctx->leader_stats.pack_block_cost;
     885           0 :   ev->pack_vote_cost   = ctx->leader_stats.pack_vote_cost;
     886           0 :   ev->pack_data_bytes  = ctx->leader_stats.pack_data_bytes;
     887           0 :   ev->bundle_txn_count = ctx->leader_stats.bundle_txn_count;
     888           0 :   ev->pack_start_time  = (ulong)ctx->leader_stats.pack_start_nanos;
     889           0 :   ev->pack_end_time    = (ulong)ctx->leader_stats.pack_end_nanos;
     890           0 :   ev->pack_end_reason  = pack_end_reason_to_event( ctx->leader_stats.pack_end_reason );
     891           0 : }
     892             : 
     893             : static void
     894             : block_completed_event_fill_leader_txn_timing( fd_replay_tile_t *           ctx,
     895             :                                               fd_event_block_completed_t * ev,
     896           0 :                                               fd_bank_t *                  bank ) {
     897           0 :   ulong tidx = ctx->leader_stats.timing_table_idx;
     898           0 :   if( FD_UNLIKELY( !ctx->leader_txn_timing || tidx>=FD_LEADER_TXN_TIMING_TABLE_CNT ) ) return;
     899           0 :   fd_leader_txn_timing_table_t const * table = fd_leader_txn_timing_table_const( ctx->leader_txn_timing, tidx, ctx->max_txn_per_slot );
     900           0 :   if( FD_UNLIKELY( table->slot!=bank->f.slot ) ) return;
     901             : 
     902           0 :   ulong cnt = fd_ulong_min( table->cnt, FD_EVENT_BLOCK_COMPLETED_TXN_TIMING_MAX );
     903           0 :   long  first_dispatch = LONG_MAX;
     904           0 :   for( ulong i=0UL; i<cnt; i++ ) {
     905           0 :     fd_leader_txn_timing_rec_t const * rec = &table->rec[ i ];
     906           0 :     first_dispatch = fd_long_min( first_dispatch, rec->dispatched_ticks );
     907           0 :     ev->txn_timing[ i ] = (fd_event_block_completed_txn_timing_t){
     908           0 :       .received_time   = (ulong)rec->received_ns,
     909           0 :       .dispatched_time = rec->dispatched_ticks==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, rec->dispatched_ticks ),
     910           0 :       .replayed_time   = rec->replayed_ticks  ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, rec->replayed_ticks   ),
     911           0 :       .poh_mixed_time  = rec->poh_mixed_ticks ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, rec->poh_mixed_ticks  ),
     912           0 :     };
     913           0 :   }
     914           0 :   ev->txn_timing_cnt = cnt;
     915             : 
     916           0 :   ev->first_transaction_scheduled_time = first_dispatch==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, first_dispatch );
     917           0 : }
     918             : 
     919             : static void
     920             : report_block_completed( fd_replay_tile_t *                 ctx,
     921             :                         fd_bank_t *                        bank,
     922             :                         int                                is_leader,
     923           0 :                         fd_replay_slot_completed_t const * slot_info ) {
     924           0 :   if( FD_LIKELY( !fd_event_tl ) ) return;
     925             : 
     926           0 :   fd_event_block_completed_t * ev = ctx->block_completed_event;
     927           0 :   memset( ev, 0, FD_EVENT_BLOCK_COMPLETED_PREFIX_SZ );
     928             : 
     929           0 :   block_completed_event_fill_bank( ctx, ev, bank );
     930             : 
     931           0 :   if( FD_LIKELY( slot_info->cost_tracker.block_cost_limit!=ULONG_MAX ) ) {
     932           0 :     ev->cost_tracker_block_cost                   = slot_info->cost_tracker.block_cost;
     933           0 :     ev->cost_tracker_allocated_accounts_data_size = slot_info->cost_tracker.allocated_accounts_data_size;
     934           0 :     ev->cost_tracker_block_cost_limit             = slot_info->cost_tracker.block_cost_limit;
     935           0 :     ev->cost_tracker_account_cost_limit           = slot_info->cost_tracker.account_cost_limit;
     936           0 :   }
     937           0 :   ev->cost_tracker_pool_idx = slot_info->cost_tracker.pool_idx;
     938             : 
     939           0 :   ev->root_slot    = ctx->consensus_root_slot;
     940           0 :   ev->storage_slot = ctx->published_root_slot;
     941           0 :   ev->caught_up            = !!ctx->caught_up;
     942           0 :   ev->turbine_slot         = ctx->catch_up_max_fec_slot==ULONG_MAX ? 0UL : ctx->catch_up_max_fec_slot;
     943           0 :   ev->fork_width           = ctx->banks->curr_fork_width;
     944           0 :   ev->snapshot_in_progress = !!ctx->snapmk.active;
     945           0 :   ev->live_bank_count      = fd_banks_pool_used_cnt( ctx->banks );
     946           0 :   ev->is_leader    = is_leader;
     947             : 
     948           0 :   ev->pack_end_reason = FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_NOT_LEADER;
     949           0 :   if( FD_UNLIKELY( is_leader ) ) block_completed_event_fill_leader( ctx, ev, bank );
     950             : 
     951           0 :   ev->dead             = 0;
     952           0 :   ev->dead_reason      = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD;
     953           0 :   ev->dead_time        = 0UL;
     954           0 :   ev->abandoned        = 0;
     955           0 :   ev->abandoned_reason = FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
     956           0 :   ev->abandoned_time   = 0UL;
     957             : 
     958           0 :   if( FD_UNLIKELY( is_leader ) ) {
     959           0 :     block_completed_event_fill_leader_txn_timing( ctx, ev, bank );
     960           0 :   } else {
     961           0 :     ulong tslot = ctx->timing_slot_of_bank[ bank->idx ];
     962           0 :     if( FD_LIKELY( tslot!=fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) ) ) {
     963           0 :       fd_replay_txn_timing_slot_t const * slot = fd_timing_slot_pool_ele( ctx->timing_slot_pool, tslot );
     964           0 :       fd_replay_txn_timing_t const *      rec  = ctx->timing_rec + tslot*ctx->max_txn_per_slot;
     965           0 :       ulong cnt = fd_ulong_min( slot->cnt, FD_EVENT_BLOCK_COMPLETED_TXN_TIMING_MAX );
     966           0 :       for( ulong i=0UL; i<cnt; i++ ) {
     967           0 :         fd_replay_txn_timing_t const * t = &rec[ i ];
     968           0 :         ev->txn_timing[ i ] = (fd_event_block_completed_txn_timing_t){
     969           0 :           .received_time             = (ulong)t->received_ns, /* already wallclock */
     970           0 :           .parsed_time               = t->parsed_ticks        ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->parsed_ticks         ),
     971           0 :           .sigverify_dispatched_time = t->sigverify_disp_ticks==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->sigverify_disp_ticks ),
     972           0 :           .sigverify_done_time       = t->sigverify_done_ticks==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->sigverify_done_ticks ),
     973           0 :           .dispatched_time           = t->exec_disp_ticks     ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->exec_disp_ticks      ),
     974           0 :           .replayed_time             = t->exec_done_ticks     ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->exec_done_ticks      ),
     975           0 :         };
     976           0 :       }
     977           0 :       ev->txn_timing_cnt = cnt;
     978           0 :     }
     979           0 :   }
     980             : 
     981           0 :   if( FD_UNLIKELY( !ev->first_transaction_scheduled_time ) ) ev->first_transaction_scheduled_time = ev->last_transaction_finished_time;
     982             : 
     983           0 :   fd_event_report_block_completed( ev );
     984           0 : }
     985             : 
     986             : static void
     987             : update_metric_identity_balance( fd_replay_tile_t *  ctx,
     988             :                                 fd_accdb_fork_id_t  fork_id,
     989           0 :                                 fd_pubkey_t const * identity ) {
     990           0 :   ulong identity_balance = fd_accdb_lamports( ctx->accdb, fork_id, identity->uc );
     991           0 :   FD_MGAUGE_SET( REPLAY, IDENTITY_BALANCE_LAMPORTS, identity_balance );
     992           0 : }
     993             : 
     994             : static void
     995             : update_metric_vote_account( fd_replay_tile_t *  ctx,
     996             :                             fd_bank_t const *   bank,
     997             :                             fd_accdb_fork_id_t  fork_id,
     998           0 :                             fd_pubkey_t const * vote_key ) {
     999           0 :   ulong epoch_credits = 0UL;
    1000           0 :   fd_acc_t ro = fd_accdb_read_one( ctx->accdb, fork_id, vote_key->uc );
    1001             :   /* Unstaked voters are never admitted; don't flag them.  Once set,
    1002             :      sticky until an epoch boundary actually admits the account. */
    1003           0 :   int admitted = fd_vote_stakes_query_t_1( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, vote_key, NULL );
    1004             :   /* SIMD-0123: delegator rewards do not count towards the balance
    1005             :      https://github.com/anza-xyz/agave/blob/v4.4/vote/src/vote_account.rs#L225-L234 */
    1006           0 :   ulong available_balance = ro.lamports;
    1007           0 :   if( FD_FEATURE_ACTIVE_BANK( bank, block_revenue_sharing ) ) {
    1008           0 :     ulong pending_delegator_rewards = 0UL;
    1009           0 :     if( FD_LIKELY( !fd_vote_account_pending_delegator_rewards( ro.data, ro.data_len, &pending_delegator_rewards ) ) ) {
    1010           0 :       available_balance = fd_ulong_sat_sub( ro.lamports, pending_delegator_rewards );
    1011           0 :     }
    1012           0 :   }
    1013           0 :   if( FD_UNLIKELY( !fd_stakes_vote_account_is_admissible( bank, available_balance, ro.owner, ro.data, ro.data_len ) ) ) {
    1014           0 :     ctx->vote_account_inadmissible = ctx->vote_account_staked || admitted;
    1015           0 :   } else if( FD_UNLIKELY( ctx->vote_account_inadmissible ) ) {
    1016           0 :     ctx->vote_account_inadmissible = ctx->vote_account_staked && !admitted;
    1017           0 :   }
    1018             : 
    1019           0 :   if( FD_LIKELY( ro.lamports ) ) {
    1020           0 :     fd_vote_state_versioned_t vsv[1];
    1021           0 :     if( FD_LIKELY( fd_vote_state_versioned_deserialize( vsv, ro.data, ro.data_len ) ) ) {
    1022           0 :       fd_vote_epoch_credits_t const * ec = fd_vsv_get_epoch_credits( vsv );
    1023           0 :       if( !deq_fd_vote_epoch_credits_t_empty( ec ) ) {
    1024           0 :         fd_vote_epoch_credits_t const * last_ec = deq_fd_vote_epoch_credits_t_peek_tail_const( ec );
    1025           0 :         if( last_ec->epoch==bank->f.epoch ) {
    1026           0 :           epoch_credits = fd_ulong_sat_sub( last_ec->credits, last_ec->prev_credits ); /* credits is the lifetime total */
    1027           0 :         }
    1028           0 :       }
    1029           0 :     }
    1030           0 :   }
    1031           0 :   fd_accdb_unread_one( ctx->accdb, &ro );
    1032             : 
    1033           0 :   FD_MGAUGE_SET( REPLAY, EPOCH_CREDITS,                epoch_credits                         );
    1034           0 :   FD_MGAUGE_SET( REPLAY, IS_VOTE_ACCOUNT_INADMISSIBLE, (ulong)ctx->vote_account_inadmissible );
    1035           0 : }
    1036             : 
    1037             : static void
    1038             : update_metric_active_stake( fd_bank_t const *   bank,
    1039           0 :                             fd_pubkey_t const * vote_key ) {
    1040           0 :   ulong my_active_stake = 0UL;
    1041             : 
    1042           0 :   fd_vote_stakes_ele_t ele[1];
    1043           0 :   if( fd_vote_stakes_query_t_1( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, vote_key, ele ) ) my_active_stake = ele->stake;
    1044             : 
    1045           0 :   FD_MGAUGE_SET( REPLAY, ACTIVE_STAKE_LAMPORTS, my_active_stake );
    1046           0 : }
    1047             : 
    1048             : static void
    1049             : update_metric_balances( fd_replay_tile_t * ctx,
    1050           0 :                         fd_bank_t *        bank ) {
    1051           0 :   fd_accdb_fork_id_t fork_id = bank->accdb_fork_id;
    1052           0 :   fd_node_info_t node_info[1]; fd_node_info_read( node_info, ctx->node_info );
    1053           0 :   if( !fd_pubkey_check_zero( &node_info->identity ) ) {
    1054           0 :     update_metric_identity_balance( ctx, fork_id, &node_info->identity );
    1055           0 :   }
    1056             : 
    1057           0 :   if( !fd_pubkey_check_zero( &node_info->vote_account ) ) {
    1058           0 :     update_metric_vote_account( ctx, bank, fork_id, &node_info->vote_account );
    1059           0 :     update_metric_active_stake(      bank,          &node_info->vote_account );
    1060           0 :   }
    1061           0 :   FD_MGAUGE_SET( REPLAY, CLUSTER_ACTIVE_STAKE_LAMPORTS, bank->f.total_epoch_stake );
    1062           0 : }
    1063             : 
    1064             : /* try_advance_root_ag attempts to advance root to finalized_block_id.  For
    1065             :    something to be rooted, it must be BOTH finalized AND replayed.  This
    1066             :    walks up the bank lineage in the block id map to find and set root.
    1067             : 
    1068             :    ON FINALIZED
    1069             : 
    1070             :    If finalized is ahead of replayed => root does not advance
    1071             :    If replayed is ahead of finalized => root advances
    1072             : 
    1073             :    ON REPLAYED
    1074             : 
    1075             :    If finalized is ahead of replayed => root advances
    1076             :    If replayed is ahead of finalized => root does not advance
    1077             : 
    1078             :    consensus_root moves straight to the finalized block, and
    1079             :    try_notify_consensus_root hands it off as REPLAY_SIG_ROOT_ADVANCED. */
    1080             : 
    1081             : static void
    1082             : try_advance_root_ag( fd_replay_tile_t * ctx,
    1083           0 :                      ag_block_id_t      finalized_block_id ) {
    1084             : 
    1085           0 :   if( FD_UNLIKELY( finalized_block_id.slot<=ctx->consensus_root_slot ) ) return;
    1086             : 
    1087           0 :   ag_block_id_t ancestor_block_id = finalized_block_id;
    1088           0 :   fd_bank_t *   finalized_bank    = NULL;
    1089             : 
    1090           0 :   while( FD_LIKELY( ancestor_block_id.slot>ctx->consensus_root_slot ) ) {
    1091           0 :     fd_block_id_ele_t * ele  = fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &ancestor_block_id, NULL, ctx->block_id_arr );
    1092           0 :     fd_bank_t *         bank = ele ? fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, ele ) ) : NULL;
    1093           0 :     if( FD_UNLIKELY( !bank || bank->bank_seq!=ele->bank_seq || bank->state!=FD_BANK_STATE_FROZEN || bank->parent_idx==ULONG_MAX ) ) break; /* not replayed */
    1094           0 :     if( !finalized_bank ) finalized_bank = bank;
    1095           0 :     ancestor_block_id = ctx->block_id_arr[ bank->parent_idx ].block_info;
    1096           0 :   }
    1097             : 
    1098           0 :   if( FD_LIKELY( ancestor_block_id.slot==ctx->consensus_root_slot && !memcmp( ancestor_block_id.hash, ctx->consensus_root.uc, sizeof(fd_hash_t) ) ) ) {
    1099           0 :     ctx->consensus_root_slot = finalized_block_id.slot;
    1100           0 :     memcpy( ctx->consensus_root.uc, finalized_block_id.hash, sizeof(fd_hash_t) );
    1101           0 :     if( FD_UNLIKELY( ctx->next_leader_slot!=ULONG_MAX && finalized_block_id.slot>ctx->votor_leader->parent_slot ) ) ctx->next_leader_slot = ULONG_MAX;
    1102           0 :     update_metric_balances( ctx, finalized_bank ); /* Alpenglow has no optimistic confirmation */
    1103           0 :     return;
    1104           0 :   }
    1105             : 
    1106             :     /* When a block id is finalized ahead of replay, we need to cache it
    1107             :        and process it when replay catches up.  Certificates can arrive
    1108             :        out of order, so lo keeps the oldest and hi the newest. */
    1109             : 
    1110           0 :   if( FD_UNLIKELY( finalized_block_id.slot<ctx->finalized_block_id_lo.slot || ctx->finalized_block_id_lo.slot<=ctx->consensus_root_slot ) ) {
    1111           0 :     if( FD_UNLIKELY( ctx->finalized_block_id_lo.slot>ctx->finalized_block_id_hi.slot ) ) fd_swap( ctx->finalized_block_id_lo, ctx->finalized_block_id_hi );
    1112           0 :     ctx->finalized_block_id_lo = finalized_block_id;
    1113           0 :   } else if( FD_LIKELY( finalized_block_id.slot>ctx->finalized_block_id_hi.slot ) ) {
    1114           0 :     ctx->finalized_block_id_hi = finalized_block_id;
    1115           0 :   }
    1116           0 : }
    1117             : 
    1118             : static void
    1119             : publish_slot_completed( fd_replay_tile_t *        ctx,
    1120             :                         fd_stem_context_t *       stem,
    1121             :                         fd_bank_t *               bank,
    1122             :                         int                       is_initial,
    1123             :                         int                       is_leader,
    1124             :                         ulong                     execution_fees_pre_settle,
    1125             :                         ulong                     priority_fees_pre_settle,
    1126           0 :                         fd_block_footer_t const * footer ) {
    1127             : 
    1128           0 :   ulong slot = bank->f.slot;
    1129             : 
    1130           0 :   if( FD_UNLIKELY( slot>=ctx->reset_slot ) ) {
    1131           0 :     ctx->reset_slot = slot;
    1132             :     /* Do not touch next_leader_slot: try_fini_leader uses ULONG_MAX
    1133             :        to continue the leader window. */
    1134           0 :     refresh_epoch_info( ctx, bank, slot );
    1135           0 :   }
    1136             : 
    1137           0 :   if( FD_UNLIKELY( is_initial ) ) bank->block_completed_nanos = fd_clock_tile_now( ctx->clock );
    1138             : 
    1139           0 :   fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ bank->idx ];
    1140             : 
    1141             :   /* HACKY: hacky way of checking if we should send a null parent block
    1142             :      id */
    1143           0 :   fd_hash_t block_id        = block_id_ele->dmr;
    1144           0 :   fd_hash_t parent_block_id = {0};
    1145           0 :   if( FD_LIKELY( !is_initial ) ) {
    1146           0 :     fd_block_id_ele_t const * parent_ele = &ctx->block_id_arr[ bank->parent_idx ];
    1147           0 :     parent_block_id = parent_ele->dmr;
    1148           0 :   }
    1149             : 
    1150           0 :   fd_hash_t const * bank_hash  = &bank->f.bank_hash;
    1151           0 :   fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( &bank->f.block_hash_queue );
    1152           0 :   FD_TEST( block_hash );
    1153             : 
    1154           0 :   if( FD_LIKELY( !is_initial ) ) fd_txncache_finalize_fork( ctx->txncache, bank->txncache_fork_id, 0UL, block_hash->uc );
    1155             : 
    1156           0 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
    1157           0 :   ulong slot_idx;
    1158           0 :   ulong epoch = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
    1159             : 
    1160           0 :   ctx->metrics.slots_total++;
    1161           0 :   ctx->metrics.transactions_total = bank->f.parent_txn_count + bank->f.txn_count;
    1162             : 
    1163             :   /* Caught up once replay completes a slot within a few slots of the
    1164             :      cluster tip.  Require the tip to have advanced a few times first so
    1165             :      a brief view of the tip right after boot does not count. */
    1166           0 :   if( FD_UNLIKELY( !ctx->caught_up && !is_initial &&
    1167           0 :                    ctx->catch_up_tip_advance_cnt>=12UL &&
    1168           0 :                    ctx->catch_up_max_fec_slot<slot+3UL ) ) {
    1169           0 :     ctx->caught_up = 1;
    1170           0 :     FD_MGAUGE_SET( REPLAY, IS_CAUGHT_UP, 1UL );
    1171           0 :     metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_CAUGHT_UP, slot );
    1172           0 :     double boot_secs = (double)(fd_log_wallclock()-ctx->boot_timestamp_nanos)/1e9;
    1173           0 :     FD_LOG_NOTICE(( "caught up to cluster at slot %s%lu%s %s(%.1f seconds since boot)%s",
    1174           0 :                     fd_log_style_bold(), slot, fd_log_style_normal(),
    1175           0 :                     fd_log_style_dim(), boot_secs, fd_log_style_normal() ));
    1176           0 :   }
    1177             : 
    1178           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1179           0 :   slot_info->slot                  = slot;
    1180           0 :   slot_info->root_slot             = ctx->consensus_root_slot;
    1181           0 :   slot_info->storage_slot          = ctx->published_root_slot;
    1182           0 :   slot_info->epoch                 = epoch;
    1183           0 :   slot_info->slot_in_epoch         = slot_idx;
    1184           0 :   slot_info->slots_per_epoch       = fd_epoch_slot_cnt( epoch_schedule, epoch );
    1185           0 :   slot_info->block_height          = bank->f.block_height;
    1186           0 :   slot_info->parent_slot           = bank->f.parent_slot;
    1187           0 :   slot_info->block_id              = block_id;
    1188           0 :   slot_info->parent_block_id       = parent_block_id;
    1189           0 :   slot_info->bank_hash             = *bank_hash;
    1190           0 :   slot_info->block_hash            = *block_hash;
    1191           0 :   slot_info->transaction_count     = bank->f.parent_txn_count + bank->f.txn_count;
    1192             : 
    1193           0 :   fd_inflation_t inflation = bank->f.inflation;
    1194           0 :   slot_info->inflation.foundation      = inflation.foundation;
    1195           0 :   slot_info->inflation.foundation_term = inflation.foundation_term;
    1196           0 :   slot_info->inflation.terminal        = inflation.terminal;
    1197           0 :   slot_info->inflation.initial         = inflation.initial;
    1198           0 :   slot_info->inflation.taper           = inflation.taper;
    1199             : 
    1200           0 :   fd_rent_t rent = bank->f.rent;
    1201           0 :   slot_info->rent.burn_percent            = rent.burn_percent;
    1202           0 :   slot_info->rent.lamports_per_uint8_year = rent.lamports_per_uint8_year;
    1203           0 :   slot_info->rent.exemption_threshold     = rent.exemption_threshold;
    1204             : 
    1205           0 :   slot_info->first_fec_set_received_nanos      = bank->first_fec_set_received_nanos;
    1206           0 :   slot_info->preparation_begin_nanos           = bank->preparation_begin_nanos;
    1207           0 :   slot_info->first_transaction_scheduled_nanos = bank->first_transaction_scheduled_nanos;
    1208           0 :   slot_info->last_transaction_finished_nanos   = bank->last_transaction_finished_nanos;
    1209           0 :   slot_info->completion_time_nanos             = fd_clock_tile_now( ctx->clock );
    1210           0 :   slot_info->footer                            = footer ? *footer : (fd_block_footer_t){0};
    1211           0 :   if( !slot_info->first_transaction_scheduled_nanos ) { /* edge case: empty slot */
    1212           0 :     slot_info->first_transaction_scheduled_nanos = slot_info->last_transaction_finished_nanos;
    1213           0 :   }
    1214             : 
    1215             :   /* refcnt should be incremented by 1 for each consumer that uses
    1216             :      `bank_idx`.  Each consumer should decrement the bank's refcnt once
    1217             :      they are done using the bank. */
    1218           0 :   if( FD_LIKELY( ctx->rpc_enabled ) ) bank->refcnt++; /* rpc tile */
    1219           0 :   slot_info->bank_idx = bank->idx;
    1220           0 :   slot_info->bank_seq = bank->bank_seq;
    1221           0 :   slot_info->accdb_fork_id = bank->accdb_fork_id;
    1222             : 
    1223           0 :   fd_bank_t * parent_bank = fd_banks_get_parent( ctx->banks, bank );
    1224           0 :   slot_info->parent_bank_idx = parent_bank ? parent_bank->idx      : ULONG_MAX;
    1225           0 :   slot_info->parent_bank_seq = parent_bank ? parent_bank->bank_seq : ULONG_MAX;
    1226           0 :   if( FD_LIKELY( parent_bank ) ) {
    1227           0 :     ulong total_txn_cnt          = bank->f.txn_count;
    1228           0 :     ulong nonvote_txn_cnt        = bank->f.nonvote_txn_count;
    1229           0 :     ulong failed_txn_cnt         = bank->f.failed_txn_count;
    1230           0 :     ulong nonvote_failed_txn_cnt = bank->f.nonvote_failed_txn_count;
    1231             : 
    1232           0 :     slot_info->nonvote_success = nonvote_txn_cnt - nonvote_failed_txn_cnt;
    1233           0 :     slot_info->nonvote_failed  = nonvote_failed_txn_cnt;
    1234           0 :     slot_info->vote_failed     = failed_txn_cnt - nonvote_failed_txn_cnt;
    1235           0 :     slot_info->vote_success    = total_txn_cnt - nonvote_txn_cnt - slot_info->vote_failed;
    1236           0 :   } else {
    1237           0 :     slot_info->vote_failed     = ULONG_MAX;
    1238           0 :     slot_info->vote_success    = ULONG_MAX;
    1239           0 :     slot_info->nonvote_success = ULONG_MAX;
    1240           0 :     slot_info->nonvote_failed  = ULONG_MAX;
    1241           0 :   }
    1242             : 
    1243           0 :   slot_info->is_leader = is_leader;
    1244           0 :   slot_info->transaction_fee = execution_fees_pre_settle;
    1245           0 :   slot_info->transaction_fee -= (slot_info->transaction_fee>>1); /* burn */
    1246           0 :   slot_info->priority_fee = priority_fees_pre_settle;
    1247           0 :   slot_info->tips = bank->f.tips;
    1248           0 :   slot_info->shred_cnt = bank->f.shred_cnt;
    1249             : 
    1250           0 :   slot_info->voted = replay_reward_cert_voted( ctx, bank, &slot_info->voted_rank, &slot_info->vote_count );
    1251             : 
    1252           0 :   slot_info->vote_balance    = ULONG_MAX;
    1253           0 :   slot_info->vote_commission = USHORT_MAX;
    1254           0 :   if( FD_UNLIKELY( ctx->has_vote_account && slot%512UL==0UL ) ) {
    1255           0 :     fd_acc_t acc = fd_accdb_read_one( ctx->accdb, bank->accdb_fork_id, ctx->vote_account->uc );
    1256           0 :     if( FD_LIKELY( acc.lamports ) ) {
    1257           0 :       slot_info->vote_balance = acc.lamports;
    1258             : 
    1259           0 :       ushort bps;
    1260           0 :       if( FD_LIKELY( !fd_vote_account_commission_bps( acc.data, acc.data_len, FD_FEATURE_ACTIVE_BANK( bank, commission_rate_in_basis_points ), &bps ) ) ) slot_info->vote_commission = bps;
    1261           0 :     }
    1262           0 :     fd_accdb_unread_one( ctx->accdb, &acc );
    1263           0 :   }
    1264             : 
    1265           0 :   publish_replay_out( ctx, stem, REPLAY_SIG_SLOT_COMPLETED, sizeof(fd_replay_slot_completed_t) );
    1266             : 
    1267           0 :   fd_replay_next_leader_t * next_leader = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1268           0 :   next_leader->slot             = ctx->reset_slot;
    1269           0 :   next_leader->next_leader_slot = ctx->leader_bank ? ctx->leader_bank->f.slot :
    1270           0 :                                   query_next_leader_slot( ctx, fd_ulong_max( ctx->reset_slot+1UL, fd_ulong_if( ctx->highwater_leader_slot==ULONG_MAX, 0UL, ctx->highwater_leader_slot+1UL ) ) );
    1271           0 :   publish_replay_out( ctx, stem, REPLAY_SIG_NEXT_LEADER, sizeof(fd_replay_next_leader_t) );
    1272             : 
    1273             :   /* Skip the telemetry event for the initial boot block (snapshot /
    1274             :      genesis): it was not replayed. */
    1275           0 :   if( FD_LIKELY( !is_initial ) ) report_block_completed( ctx, bank, is_leader, slot_info );
    1276           0 :   timing_slot_release( ctx, bank->idx );
    1277             : 
    1278           0 :   if( FD_UNLIKELY( slot==ctx->finalized_block_id_lo.slot && !memcmp( block_id.uc, ctx->finalized_block_id_lo.hash, sizeof(fd_hash_t) ) ) ) { /* finalized ahead of replay, replay caught up */
    1279           0 :     try_advance_root_ag( ctx, ctx->finalized_block_id_lo );
    1280           0 :     ctx->finalized_block_id_lo = ctx->finalized_block_id_hi; /* hi becomes lo once lo is reached */
    1281           0 :   }
    1282           0 : }
    1283             : 
    1284             : static void
    1285             : report_block_incomplete( fd_replay_tile_t * ctx,
    1286             :                          ulong              slot,
    1287             :                          fd_hash_t const *  block_id,
    1288             :                          fd_bank_t *        bank,
    1289             :                          int                dead_reason,
    1290           0 :                          int                abandoned_reason ) {
    1291           0 :   if( FD_LIKELY( !fd_event_tl ) ) return;
    1292             : 
    1293           0 :   fd_event_block_completed_t * ev = ctx->block_completed_event;
    1294           0 :   memset( ev, 0, FD_EVENT_BLOCK_COMPLETED_PREFIX_SZ );
    1295           0 :   if( FD_LIKELY( bank ) ) {
    1296           0 :     block_completed_event_fill_bank( ctx, ev, bank );
    1297           0 :     ev->is_leader = !!bank->is_leader;
    1298           0 :   } else {
    1299           0 :     ev->slot = slot;
    1300           0 :     if( FD_LIKELY( ctx->notified_root_bank ) ) ev->epoch = fd_slot_to_epoch( &ctx->notified_root_bank->f.epoch_schedule, slot, NULL );
    1301           0 :     fd_memcpy( ev->block_id, block_id->uc, sizeof(ev->block_id) );
    1302           0 :     block_completed_event_fill_reception( ctx, ev, ULONG_MAX );
    1303           0 :   }
    1304           0 :   ev->root_slot    = ctx->consensus_root_slot;
    1305           0 :   ev->storage_slot = ctx->published_root_slot;
    1306           0 :   ev->caught_up            = !!ctx->caught_up;
    1307           0 :   ev->turbine_slot         = ctx->catch_up_max_fec_slot==ULONG_MAX ? 0UL : ctx->catch_up_max_fec_slot;
    1308           0 :   ev->fork_width           = ctx->banks->curr_fork_width;
    1309           0 :   ev->snapshot_in_progress = !!ctx->snapmk.active;
    1310           0 :   ev->live_bank_count      = fd_banks_pool_used_cnt( ctx->banks );
    1311             : 
    1312           0 :   int  dead      = dead_reason     !=FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD;
    1313           0 :   int  abandoned = abandoned_reason!=FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
    1314           0 :   long now       = fd_clock_tile_now( ctx->clock );
    1315             : 
    1316           0 :   ev->dead             = dead;
    1317           0 :   ev->dead_reason      = dead_reason;
    1318           0 :   ev->dead_time        = dead      ? (ulong)now : 0UL;
    1319           0 :   ev->abandoned        = abandoned;
    1320           0 :   ev->abandoned_reason = abandoned_reason;
    1321           0 :   ev->abandoned_time   = abandoned ? (ulong)now : 0UL;
    1322             : 
    1323           0 :   ev->pack_end_reason = FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_NOT_LEADER;
    1324           0 :   if( FD_UNLIKELY( bank && bank->is_leader ) ) {
    1325           0 :     block_completed_event_fill_leader( ctx, ev, bank );
    1326           0 :     block_completed_event_fill_leader_txn_timing( ctx, ev, bank );
    1327           0 :   }
    1328             : 
    1329           0 :   fd_event_report_block_completed( ev );
    1330           0 : }
    1331             : 
    1332             : static void
    1333             : publish_slot_dead( fd_replay_tile_t *        ctx,
    1334             :                    fd_stem_context_t *       stem,
    1335             :                    ulong                     slot,
    1336             :                    fd_hash_t const *         block_id,
    1337           0 :                    fd_block_footer_t const * footer ) {
    1338           0 :   fd_replay_slot_dead_t * slot_dead = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1339           0 :   slot_dead->slot                   = slot;
    1340           0 :   slot_dead->block_id               = *block_id;
    1341           0 :   slot_dead->footer                 = footer ? *footer : (fd_block_footer_t){0};
    1342           0 :   publish_replay_out( ctx, stem, REPLAY_SIG_SLOT_DEAD, sizeof(fd_replay_slot_dead_t) );
    1343           0 : }
    1344             : 
    1345             : static void
    1346             : publish_txn_executed( fd_replay_tile_t *  ctx,
    1347             :                       fd_stem_context_t * stem,
    1348             :                       ulong               bank_idx,
    1349           0 :                       ulong               txn_idx ) {
    1350           0 :   fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, txn_idx );
    1351             : 
    1352           0 :   FD_TEST( txn_info->index_in_slot<ctx->max_txn_per_slot );
    1353           0 :   ulong tslot = ctx->timing_slot_of_bank[ bank_idx ];
    1354           0 :   if( FD_LIKELY( tslot!=fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) ) ) {
    1355           0 :     fd_replay_txn_timing_slot_t * slot = fd_timing_slot_pool_ele( ctx->timing_slot_pool, tslot );
    1356           0 :     fd_replay_txn_timing_t * t = ctx->timing_rec + tslot*ctx->max_txn_per_slot + txn_info->index_in_slot;
    1357           0 :     t->received_ns          = txn_info->received_ns;
    1358           0 :     t->parsed_ticks         = txn_info->tick_parsed;
    1359           0 :     t->sigverify_disp_ticks = txn_info->tick_sigverify_disp;
    1360           0 :     t->sigverify_done_ticks = txn_info->tick_sigverify_done;
    1361           0 :     t->exec_disp_ticks      = txn_info->tick_exec_disp;
    1362           0 :     t->exec_done_ticks      = txn_info->tick_exec_done;
    1363           0 :     slot->cnt = fd_ulong_max( slot->cnt, txn_info->index_in_slot+1UL );
    1364           0 :   }
    1365             : 
    1366           0 :   fd_replay_txn_executed_t * txn_executed = fd_type_pun( fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk ) );
    1367           0 :   *txn_executed->txn = *fd_sched_get_txn( ctx->sched, txn_idx );
    1368           0 :   txn_executed->txn_err = txn_info->txn_err;
    1369           0 :   txn_executed->is_committable = !!(txn_info->flags&FD_SCHED_TXN_IS_COMMITTABLE);
    1370           0 :   txn_executed->is_fees_only = !!(txn_info->flags&FD_SCHED_TXN_IS_FEES_ONLY);
    1371           0 :   txn_executed->is_noop = !!(txn_info->flags&FD_SCHED_TXN_IS_NOOP);
    1372           0 :   txn_executed->is_simple_vote = txn_info->is_simple_vote;
    1373           0 :   txn_executed->tick_parsed = txn_info->tick_parsed;
    1374           0 :   txn_executed->tick_sigverify_disp = txn_info->tick_sigverify_disp;
    1375           0 :   txn_executed->tick_sigverify_done = txn_info->tick_sigverify_done;
    1376           0 :   txn_executed->tick_exec_disp = txn_info->tick_exec_disp;
    1377           0 :   txn_executed->tick_exec_done = txn_info->tick_exec_done;
    1378             : 
    1379           0 :   txn_executed->tick_load_start = txn_info->tick_load_start;
    1380           0 :   txn_executed->tick_check_start = txn_info->tick_check_start;
    1381           0 :   txn_executed->tick_exec_start = txn_info->tick_exec_start;
    1382           0 :   txn_executed->tick_commit_start = txn_info->tick_commit_start;
    1383           0 :   txn_executed->tick_commit_end = txn_info->tick_commit_end;
    1384             : 
    1385           0 :   txn_executed->slot = txn_info->slot;
    1386           0 :   txn_executed->bank_seq = txn_info->bank_seq;
    1387           0 :   txn_executed->index_in_slot = txn_info->index_in_slot;
    1388           0 :   txn_executed->exec_tile_idx = txn_info->exec_tile_idx;
    1389           0 :   txn_executed->sigverify_exec_tile_idx = txn_info->sigverify_exec_tile_idx;
    1390           0 :   txn_executed->compute_units_consumed = txn_info->compute_units_consumed;
    1391           0 :   txn_executed->max_compute_units = txn_info->max_compute_units;
    1392           0 :   txn_executed->transaction_fee = txn_info->transaction_fee;
    1393           0 :   txn_executed->priority_fee = txn_info->priority_fee;
    1394           0 :   txn_executed->tips = txn_info->tips;
    1395           0 :   publish_replay_out( ctx, stem, REPLAY_SIG_TXN_EXECUTED, sizeof(*txn_executed) );
    1396           0 : }
    1397             : 
    1398             : /* Emit the runtime_block event for a finalized bank (replayed or
    1399             :    leader-produced).  The fee arguments are the bank's values snapshotted
    1400             :    before fd_runtime_block_execute_finalize, which settles (zeroes) the
    1401             :    fee fields and applies the tip commission. */
    1402             : 
    1403             : static void
    1404             : replay_runtime_block_emit( fd_replay_tile_t * ctx,
    1405             :                            fd_bank_t *        bank,
    1406             :                            ulong              execution_fees,
    1407             :                            ulong              priority_fees,
    1408           0 :                            ulong              tips ) {
    1409           0 :   fd_block_id_ele_t const * ele        = &ctx->block_id_arr[ bank->idx ];
    1410           0 :   fd_hash_t         const * block_id   = &ele->dmr;
    1411           0 :   fd_hash_t parent_block_id = {0};
    1412           0 :   if( FD_LIKELY( bank->parent_idx!=ULONG_MAX ) ) {
    1413           0 :     fd_block_id_ele_t const * parent_ele = &ctx->block_id_arr[ bank->parent_idx ];
    1414           0 :     parent_block_id = parent_ele->dmr;
    1415           0 :   }
    1416           0 :   fd_pubkey_t leader = {0};
    1417           0 :   fd_pubkey_t const * _leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, bank->f.slot );
    1418           0 :   if( FD_LIKELY( _leader ) ) leader = *_leader;
    1419           0 :   fd_sol_sysvar_clock_t clock = {0};
    1420           0 :   if( FD_UNLIKELY( !fd_sysvar_clock_read( ctx->accdb, bank->accdb_fork_id, &clock ) ) ) FD_LOG_ERR(( "failed to read clock sysvar for slot %lu", bank->f.slot ));
    1421           0 :   fd_event_runtime_block_emit( bank, block_id->uc, parent_block_id.uc, leader.uc,
    1422           0 :                                execution_fees, priority_fees, tips, bank->f.shred_cnt, &clock,
    1423           0 :                                ctx->fec_chain + bank->idx*FD_FEC_BLK_MAX,
    1424           0 :                                ctx->block_id_arr[ bank->idx ].fec_cnt );
    1425           0 : }
    1426             : 
    1427             : static void
    1428             : mark_bank_dead( fd_replay_tile_t *        ctx,
    1429             :                 fd_stem_context_t *       stem,
    1430             :                 ulong                     bank_idx,
    1431             :                 int                       dead_reason,
    1432             :                 int                       abandoned_reason,
    1433             :                 fd_block_footer_t const * footer );
    1434             : 
    1435             : 
    1436             : /**********************************************************************/
    1437             : /* Leader bank management                                             */
    1438             : /**********************************************************************/
    1439             : 
    1440             : static fd_bank_t *
    1441             : prepare_leader_bank( fd_replay_tile_t * ctx,
    1442             :                      fd_bank_t *        parent_bank,
    1443             :                      ulong              slot,
    1444           0 :                      long               now ) {
    1445             : 
    1446             :   /* Make sure that we are not already leader. */
    1447           0 :   FD_TEST( ctx->leader_bank==NULL );
    1448             : 
    1449           0 :   ctx->leader_bank = fd_banks_new_bank( ctx->banks, parent_bank->idx, now, 1 );
    1450           0 :   if( FD_UNLIKELY( !ctx->leader_bank ) ) {
    1451           0 :     FD_LOG_CRIT(( "invariant violation: leader bank is NULL for slot %lu", slot ));
    1452           0 :   }
    1453             : 
    1454           0 :   ctx->leader_bank = fd_banks_clone_from_parent( ctx->banks, ctx->leader_bank->idx );
    1455           0 :   if( FD_UNLIKELY( !ctx->leader_bank ) ) {
    1456           0 :     FD_LOG_CRIT(( "invariant violation: bank is NULL for slot %lu", slot ));
    1457           0 :   }
    1458             : 
    1459             :   /* pin the cost tracker for the duration of the leader slot */
    1460           0 :   fd_cost_tracker_store_pin( fd_bank_cost_tracker( ctx->leader_bank ), ctx->leader_bank->cost_tracker_fork_id );
    1461             : 
    1462           0 :   ctx->leader_bank->preparation_begin_nanos = now;
    1463             : 
    1464           0 :   ctx->leader_bank->f.slot = slot;
    1465           0 :   ctx->leader_bank->leader = ctx->identity_pubkey[ 0 ];
    1466             : 
    1467             :   /* Clear the previous occupant of this reused bank idx from whichever
    1468             :      map it is currently keyed in. */
    1469           0 :   fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ ctx->leader_bank->idx ];
    1470           0 :   if( FD_LIKELY( fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr )==block_id_ele ) ) {
    1471           0 :     FD_TEST( fd_ag_block_id_map_ele_remove( ctx->ag_block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr ) );
    1472           0 :   }
    1473             : 
    1474           0 :   block_id_ele->block_id_seen  = 0;
    1475           0 :   block_id_ele->slot           = slot;
    1476           0 :   block_id_ele->bank_seq       = ctx->leader_bank->bank_seq;
    1477           0 :   block_id_ele->latest_fec_idx = 0U;
    1478           0 :   block_id_ele->fec_cnt        = 0U;
    1479           0 :   memset( &block_id_ele->latest_mr,  0, sizeof(fd_hash_t) );
    1480           0 :   memset( &block_id_ele->dmr,        0, sizeof(fd_hash_t) );
    1481             : 
    1482           0 :   ctx->leader_bank->txncache_fork_id     = fd_txncache_attach_child ( ctx->txncache,  parent_bank->txncache_fork_id  );
    1483           0 :   ctx->leader_bank->progcache_fork_id    = fd_progcache_attach_child( ctx->progcache, parent_bank->progcache_fork_id );
    1484           0 :   ctx->leader_bank->accdb_fork_id        = fd_accdb_attach_child    ( ctx->accdb,     parent_bank->accdb_fork_id     );
    1485           0 :   ctx->leader_bank->parent_accdb_fork_id = parent_bank->accdb_fork_id;
    1486             : 
    1487           0 :   int is_epoch_boundary = 0;
    1488           0 :   fd_runtime_block_execute_prepare( ctx->banks, ctx->leader_bank, ctx->accdb, ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
    1489           0 :   FD_CHECK_CRIT( !is_epoch_boundary || FD_FEATURE_ACTIVE_BANK( ctx->leader_bank, alpenglow ), "alpenglow feature is not active" );
    1490             : 
    1491           0 :   ulong max_tick_height;
    1492           0 :   if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS!=fd_runtime_compute_max_tick_height( parent_bank->f.ticks_per_slot, slot, &max_tick_height ) ) ) {
    1493           0 :     FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, parent_bank->f.ticks_per_slot ));
    1494           0 :   }
    1495           0 :   ctx->leader_bank->f.max_tick_height = max_tick_height;
    1496             : 
    1497             :   /* Now that a bank has been created for the leader slot, increment the
    1498             :      reference count until we are done with the leader slot. */
    1499           0 :   ctx->leader_bank->refcnt++;
    1500             : 
    1501           0 :   return ctx->leader_bank;
    1502           0 : }
    1503             : 
    1504             : static inline void
    1505           0 : maybe_switch_identity( fd_replay_tile_t * ctx ) {
    1506             : 
    1507           0 :   if( FD_LIKELY( fd_keyswitch_state_query( ctx->keyswitch )!=FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) return;
    1508             : 
    1509             :   /* Switch identity */
    1510             : 
    1511           0 :   FD_LOG_DEBUG(( "keyswitch: switching identity" ));
    1512             : 
    1513           0 :   memcpy( ctx->identity_pubkey, ctx->keyswitch->bytes, 32UL );
    1514           0 :   ctx->identity_dirty = 1;
    1515             : 
    1516           0 :   ctx->metrics.voted_slot = ULONG_MAX;
    1517             : 
    1518           0 :   fd_node_info_write_begin( ctx->node_info );
    1519           0 :   ctx->node_info->info.identity = *ctx->identity_pubkey;
    1520           0 :   fd_node_info_write_end  ( ctx->node_info );
    1521             : 
    1522           0 :   ctx->next_leader_query_start = ULONG_MAX;
    1523             : 
    1524           0 :   ctx->identity_vote_rooted = 0;
    1525           0 :   ctx->identity_idx++;
    1526           0 :   fd_vote_tracker_reset( ctx->vote_tracker );
    1527             : 
    1528             :   /* identity_vote_rooted was just reset, so the gate rests on the flag
    1529             :      alone and a surviving auto would read as armed (see fd_wfs.h). */
    1530           0 :   if( FD_UNLIKELY( ctx->wait_for_vote_to_start_leader==FD_BOOLAU_AUTO ) ) {
    1531           0 :     FD_LOG_CRIT(( "wait_for_vote_to_start_leader unresolved (auto) at identity rotation" ));
    1532           0 :   }
    1533             : 
    1534             :   /* Save the current sequence so the voter knows what sequence to
    1535             :      consume up to. */
    1536           0 :   ctx->keyswitch->result = fd_mcache_seq_query( ctx->slot_out_seq );
    1537           0 :   fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
    1538           0 : }
    1539             : 
    1540             : static void
    1541             : construct_footer_certs( fd_replay_tile_t const * ctx,
    1542             :                         ulong                    leader_slot,
    1543             :                         ulong                    migration_slot,
    1544           0 :                         fd_block_footer_t *      footer ) {
    1545           0 :   fd_votor_certed_t const * fin = ctx->votor_final;
    1546           0 :   int use_fast = fin->slot!=ULONG_MAX && fin->kind==AG_CERT_KIND_FAST_FINAL;
    1547           0 :   int use_slow = fin->slot!=ULONG_MAX && fin->kind==AG_CERT_KIND_FINAL;
    1548             : 
    1549           0 :   footer->has_fast_final_cert = use_fast;
    1550           0 :   footer->has_final_cert      = use_slow;
    1551           0 :   if( footer->has_fast_final_cert ) {
    1552           0 :     footer->has_fast_final_cert = fd_block_footer_cert_from_agg( &footer->fast_final_cert, fin->slot, fin->block_id.uc, &fin->agg );
    1553           0 :   }
    1554           0 :   if( footer->has_final_cert ) {
    1555           0 :     footer->has_final_cert = fd_block_footer_cert_from_agg( &footer->final_cert, fin->slot, NULL,             &fin->agg  ) &&
    1556           0 :                              fd_block_footer_cert_from_agg( &footer->notar_cert, fin->slot, fin->block_id.uc, &fin->agg2 );
    1557           0 :   }
    1558             : 
    1559           0 :   int reward_ok = migration_slot!=ULONG_MAX &&
    1560           0 :                   leader_slot>=migration_slot+FD_NUM_SLOTS_FOR_REWARD+1UL;
    1561           0 :   if( FD_LIKELY( reward_ok ) ) {
    1562           0 :     ulong                     reward_slot = leader_slot-FD_NUM_SLOTS_FOR_REWARD;
    1563           0 :     fd_votor_reward_t const * reward      = &ctx->votor_reward[ reward_slot%(FD_NUM_SLOTS_FOR_REWARD+AG_SLOTS_PER_WINDOW+1UL) ];
    1564           0 :     footer->has_notar_reward_cert = reward->slot==reward_slot && !fd_bls_set_is_null( reward->agg_notar.set ) && fd_block_footer_cert_from_agg( &footer->notar_reward_cert, reward_slot, reward->block_id.uc, &reward->agg_notar );
    1565           0 :     footer->has_skip_reward_cert  = reward->slot==reward_slot && !fd_bls_set_is_null( reward->agg_skip.set  ) && fd_block_footer_cert_from_agg( &footer->skip_reward_cert,  reward_slot, NULL,                &reward->agg_skip  );
    1566           0 :   }
    1567           0 : }
    1568             : 
    1569             : static void
    1570             : publish_leader_footer( fd_replay_tile_t *        ctx,
    1571             :                        fd_stem_context_t *       stem,
    1572             :                        ulong                     slot,
    1573           0 :                        fd_block_footer_t const * footer ) {
    1574           0 :   fd_replay_leader_footer_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1575           0 :   msg->slot   = slot;
    1576           0 :   msg->footer = *footer;
    1577             : 
    1578           0 :   ulong sz = sizeof(fd_replay_leader_footer_t);
    1579           0 :   publish_replay_out( ctx, stem, REPLAY_SIG_LEADER_FOOTER, sz );
    1580           0 : }
    1581             : 
    1582             : /* For the most part, try_become_leader_ag mirrors try_become_leader
    1583             :    with modifications for Alpenglow-specific changes, in particular
    1584             :    1. no more PoH-hashing and 2. picking the reset bank is based on
    1585             :    ParentReady rather than fork choice. */
    1586             : 
    1587             : static inline int
    1588             : try_become_leader_ag( fd_replay_tile_t *  ctx,
    1589             :                       fd_stem_context_t * stem,
    1590             :                       uint                reason,
    1591           0 :                       ulong               end_slot ) {
    1592             : 
    1593             :   /* Mirrors check from try_become_leader. */
    1594             : 
    1595           0 :   if( FD_LIKELY( ctx->next_leader_slot==ULONG_MAX ||
    1596           0 :                  ctx->is_leader ||
    1597             :                  /* (!ctx->identity_vote_rooted && ctx->wait_for_vote_to_start_leader) ||  // deprecated */
    1598           0 :                  ctx->replay_out->idx==ULONG_MAX ||
    1599           0 :                  !ctx->wfs_complete ) ) {
    1600           0 :     return 0;
    1601           0 :   }
    1602             : 
    1603           0 :   ulong             parent_slot     = ctx->votor_leader->parent_slot;
    1604           0 :   fd_hash_t const * parent_block_id = &ctx->votor_leader->parent_block_id;
    1605             : 
    1606           0 :   fd_block_id_ele_t * block_id_ele = fd_block_id_ele_query( ctx, parent_block_id, parent_slot );
    1607           0 :   if( FD_UNLIKELY( !block_id_ele ) ) return 0;
    1608           0 :   fd_bank_t * reset_bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele ) );
    1609           0 :   if( FD_UNLIKELY( !reset_bank || reset_bank->bank_seq!=block_id_ele->bank_seq || reset_bank->state!=FD_BANK_STATE_FROZEN ) ) return 0;
    1610             : 
    1611             :   /* A finalization above the parent, or a root that excludes it, means
    1612             :      we should give up our leader slot. */
    1613           0 :   if( FD_UNLIKELY( ( ctx->votor_final->slot!=ULONG_MAX && ctx->votor_final->slot>parent_slot ) ||
    1614           0 :                    ( ctx->consensus_root_slot!=ULONG_MAX && ctx->consensus_root_slot>parent_slot ) ||
    1615           0 :                    ( ctx->consensus_root_slot==parent_slot && !fd_hash_eq( &ctx->consensus_root, parent_block_id ) ) ) ) {
    1616           0 :     ctx->next_leader_slot = ULONG_MAX;
    1617           0 :     return 0;
    1618           0 :   }
    1619             : 
    1620             :   /* Certs made a later window ParentReady after votor sent this one:
    1621             :      the cluster skipped it. */
    1622           0 :   if( FD_UNLIKELY( ctx->next_leader_slot<ctx->votor_parent_ready_slot ) ) {
    1623           0 :     ctx->metrics.leader_window_superseded++;
    1624           0 :     ctx->next_leader_slot = ULONG_MAX;
    1625           0 :     return 0;
    1626           0 :   }
    1627             : 
    1628           0 :   if( FD_UNLIKELY( !fd_banks_can_start_bank( ctx->banks ) ) ) return 0;
    1629           0 :   if( FD_UNLIKELY( ctx->halt_replay ) ) return 0;
    1630           0 :   if( !ctx->supports_leader ) return 0;
    1631           0 :   if( FD_UNLIKELY( fd_accdb_cmd_pending( ctx->accdb ) ) ) return 0; /* leader bank attaches an accdb fork */
    1632             : 
    1633             :   /* Don't become leader if the slot is not scheduled for the identity.
    1634             :      This can only happen in cases where the identity just switched. */
    1635           0 :   fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, ctx->next_leader_slot );
    1636           0 :   if( FD_UNLIKELY( !leader || !fd_pubkey_eq( leader, ctx->identity_pubkey ) ) ) {
    1637           0 :     ctx->next_leader_slot = ULONG_MAX;
    1638           0 :     return 0;
    1639           0 :   }
    1640             : 
    1641             :   /* The cluster already produced a slot of this window, eg. our own
    1642             :      past slots replayed during catch-up after a restart. */
    1643           0 :   ulong window_end = ctx->next_leader_slot - ctx->next_leader_slot%AG_SLOTS_PER_WINDOW + AG_SLOTS_PER_WINDOW;
    1644           0 :   for( ulong idx=0UL; idx<ctx->block_id_len; idx++ ) {
    1645           0 :     fd_block_id_ele_t const * ele = &ctx->block_id_arr[ idx ];
    1646           0 :     if( FD_LIKELY( ele->slot<ctx->next_leader_slot || ele->slot>=window_end ) ) continue;
    1647           0 :     fd_bank_t const * bank = fd_banks_bank_query( ctx->banks, idx );
    1648           0 :     if( FD_UNLIKELY( bank && bank->bank_seq==ele->bank_seq && !bank->is_leader ) ) {
    1649           0 :       ctx->metrics.leader_slot_already_produced++;
    1650           0 :       ctx->next_leader_slot = ULONG_MAX;
    1651           0 :       return 0;
    1652           0 :     }
    1653           0 :   }
    1654             : 
    1655             :   /* In Alpenglow, the "reset" block is signaled by ParentReady (a state
    1656             :      transition in the Votor consensus logic).  ParentReady can occur
    1657             :      ahead of replay, so we publish reset here, once we know we are
    1658             :      certain we can build off the ParentReady bank. */
    1659             : 
    1660           0 :   ctx->reset_dmr             = *parent_block_id;
    1661           0 :   ctx->reset_slot            = parent_slot;
    1662           0 :   ctx->reset_timestamp_nanos = fd_clock_tile_now( ctx->clock );
    1663             : 
    1664           0 :   FD_TEST( ctx->next_leader_slot>ctx->reset_slot );
    1665           0 :   long now = fd_tickcount();
    1666           0 :   ctx->next_leader_tickcount = now;
    1667             : 
    1668           0 :   fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1669             : 
    1670           0 :   reset->bank_idx         = reset_bank->idx;
    1671           0 :   reset->timestamp        = ctx->reset_timestamp_nanos;
    1672           0 :   reset->completed_slot   = ctx->reset_slot;
    1673           0 :   reset->hashcnt_per_tick = reset_bank->f.slot_params.hashes_per_tick;
    1674           0 :   reset->ticks_per_slot   = reset_bank->f.ticks_per_slot;
    1675           0 :   reset->tick_duration_ns = reset_bank->f.slot_params.ns_per_slot_adjusted/reset->ticks_per_slot;
    1676             : 
    1677           0 :   fd_memcpy( reset->completed_cmr, &block_id_ele->latest_mr, sizeof(fd_hash_t) );
    1678           0 :   fd_memcpy( reset->completed_dmr, parent_block_id,          sizeof(fd_hash_t) );
    1679             : 
    1680           0 :   fd_blockhashes_t const * block_hash_queue = &reset_bank->f.block_hash_queue;
    1681           0 :   fd_hash_t const * last_hash = fd_blockhashes_peek_last_hash( block_hash_queue );
    1682           0 :   FD_TEST( last_hash );
    1683           0 :   fd_memcpy( reset->completed_blockhash, last_hash->uc, sizeof(fd_hash_t) );
    1684             : 
    1685           0 :   reset->max_microblocks_in_slot = FD_POH_ALPENGLOW_MAX_MICROBLOCKS_PER_SLOT;
    1686           0 :   reset->next_leader_slot = ctx->next_leader_slot;
    1687           0 :   reset->wfs_paused       = !ctx->wfs_complete;
    1688             : 
    1689           0 :   publish_replay_out( ctx, stem, REPLAY_SIG_RESET, sizeof(fd_poh_reset_t) );
    1690             : 
    1691             :   /* The remainder of this function mirrors try_become_leader. */
    1692             : 
    1693           0 :   long now_nanos = fd_clock_epoch_y( ctx->clock->epoch, now );
    1694             : 
    1695           0 :   ctx->is_leader = 1;
    1696           0 :   ctx->recv_poh  = 0;
    1697             : 
    1698           0 :   memset( &ctx->leader_stats, 0, sizeof(ctx->leader_stats) );
    1699           0 :   ctx->leader_stats.slot                = ctx->next_leader_slot;
    1700           0 :   ctx->leader_stats.timing_table_idx    = ULONG_MAX;
    1701           0 :   ctx->leader_stats.became_leader_nanos = now_nanos;
    1702           0 :   ctx->leader_stats.leader_slot_start_nanos = fd_clock_epoch_y( ctx->clock->epoch, ctx->next_leader_tickcount );
    1703             : 
    1704           0 :   FD_TEST( ctx->highwater_leader_slot==ULONG_MAX || ctx->highwater_leader_slot<ctx->next_leader_slot );
    1705           0 :   ctx->highwater_leader_slot = ctx->next_leader_slot;
    1706             : 
    1707           0 :   FD_LOG_INFO(( "becoming leader for slot %lu, parent slot is %lu", ctx->next_leader_slot, ctx->reset_slot ));
    1708             : 
    1709           0 :   fd_bank_t * bank = prepare_leader_bank( ctx, reset_bank, ctx->next_leader_slot, now_nanos );
    1710             : 
    1711           0 :   fd_bundle_crank_tip_payment_config_t config[1] = { 0 };
    1712           0 :   fd_pubkey_t tip_receiver_owner = {0};
    1713             : 
    1714           0 :   if( FD_UNLIKELY( ctx->bundle.enabled ) ) {
    1715           0 :     fd_acct_addr_t tip_payment_config[1];
    1716           0 :     fd_acct_addr_t tip_receiver[1];
    1717           0 :     fd_bundle_crank_get_addresses( ctx->bundle.gen, bank->f.epoch, tip_payment_config, tip_receiver );
    1718             : 
    1719           0 :     fd_acc_t tip_config_acc = fd_accdb_read_one( ctx->accdb, bank->accdb_fork_id, tip_payment_config->b );
    1720           0 :     if( FD_UNLIKELY( !tip_config_acc.lamports ) ) {
    1721           0 :       FD_BASE58_ENCODE_32_BYTES( tip_payment_config->b, tip_config_acc_b58 );
    1722           0 :       FD_LOG_WARNING(( "tip payment config account %s does not exist", tip_config_acc_b58 ));
    1723           0 :       fd_accdb_unread_one( ctx->accdb, &tip_config_acc );
    1724           0 :     } else if( FD_UNLIKELY( tip_config_acc.data_len<sizeof(fd_bundle_crank_tip_payment_config_t) ) ) {
    1725           0 :       FD_LOG_HEXDUMP_WARNING(( "invalid tip payment config account data", tip_config_acc.data, tip_config_acc.data_len ));
    1726           0 :       fd_accdb_unread_one( ctx->accdb, &tip_config_acc );
    1727           0 :     } else {
    1728           0 :       memcpy( config, tip_config_acc.data, sizeof(fd_bundle_crank_tip_payment_config_t) );
    1729           0 :       fd_accdb_unread_one( ctx->accdb, &tip_config_acc );
    1730           0 :     }
    1731             : 
    1732             :     /* It is possible that the tip receiver account does not exist yet
    1733             :        if it is the first time in an epoch. */
    1734           0 :     fd_acc_t tip_receiver_acc = fd_accdb_read_one( ctx->accdb, bank->accdb_fork_id, tip_receiver->b );
    1735           0 :     if( FD_LIKELY( tip_receiver_acc.lamports ) ) {
    1736           0 :       fd_memcpy( tip_receiver_owner.uc, tip_receiver_acc.owner, 32UL );
    1737           0 :     }
    1738           0 :     fd_accdb_unread_one( ctx->accdb, &tip_receiver_acc );
    1739           0 :   }
    1740             : 
    1741             : 
    1742             :   /* Like Agave's block_timeout, the block ends relative to the window's
    1743             :      ParentReady.  If that is already past, end at once rather than run
    1744             :      into votor's skip timeout. */
    1745           0 :   long slot_end_ns = ctx->votor_leader->parent_ready_ns
    1746           0 :                    + (long)( ( ctx->next_leader_slot%AG_SLOTS_PER_WINDOW+1UL )*bank->f.slot_params.ns_per_slot )
    1747           0 :                    - (long)FD_TARGET_SLOT_ADJUSTMENT_NS - AG_TIME_TO_COMPLETE_BROADCAST_NS;
    1748             : 
    1749           0 :   fd_became_leader_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1750           0 :   msg->slot                = ctx->next_leader_slot;
    1751           0 :   msg->block_height        = bank->f.block_height;
    1752           0 :   msg->slot_start_ns       = now_nanos;
    1753           0 :   msg->slot_end_ns         = fd_long_max( slot_end_ns, now_nanos );
    1754           0 :   msg->bank_idx            = bank->idx;
    1755           0 :   msg->bank_seq            = bank->bank_seq;
    1756           0 :   msg->ticks_per_slot      = bank->f.ticks_per_slot;
    1757           0 :   msg->hashcnt_per_tick    = 1UL; /* one tick per block, no hash budget (see replay_block_start) */
    1758           0 :   msg->tick_duration_ns    = bank->f.slot_params.ns_per_slot_adjusted/msg->ticks_per_slot;
    1759           0 :   msg->bundle->config[0]   = config[0];
    1760           0 :   memcpy( msg->bundle->last_blockhash,     bank->f.poh.hash,      sizeof(fd_hash_t)   );
    1761           0 :   memcpy( msg->bundle->tip_receiver_owner, tip_receiver_owner.uc, sizeof(fd_pubkey_t) );
    1762             : 
    1763           0 :   msg->max_microblocks_in_slot = FD_POH_ALPENGLOW_MAX_MICROBLOCKS_PER_SLOT;
    1764             : 
    1765           0 :   msg->total_skipped_ticks = 0UL; /* even when slots are skipped, ticks increment by exactly one for every block */
    1766           0 :   msg->epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, ctx->next_leader_slot, NULL );
    1767             : 
    1768           0 :   fd_cost_tracker_t const * cost_tracker = fd_cost_tracker_store_peek( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
    1769             : 
    1770           0 :   msg->limits.slot_max_cost                     = cost_tracker->block_cost_limit;
    1771           0 :   msg->limits.slot_max_vote_cost                = FD_PACK_MAX_VOTE_COST_PER_BLOCK_UPPER_BOUND;
    1772           0 :   msg->limits.slot_max_write_cost_per_acct      = cost_tracker->account_cost_limit;
    1773           0 :   msg->limits.slot_max_allocated_data_per_block = cost_tracker->data_size_limit;
    1774           0 :   msg->limits.slot_max_data_shreds              = bank->f.slot_params.max_shred_idx;
    1775             : 
    1776           0 :   publish_replay_out( ctx, stem, REPLAY_SIG_BECAME_LEADER, sizeof(fd_became_leader_t) );
    1777           0 :   metrics_trigger( ctx, reason, reason==FD_METRICS_SAMPLE_REASON_LEADER_START ? msg->slot : end_slot );
    1778             : 
    1779           0 :   ctx->leader_window_start_ns = ctx->votor_leader->parent_ready_ns;
    1780           0 :   ctx->next_leader_slot       = ULONG_MAX;
    1781           0 :   ctx->next_leader_tickcount  = LONG_MAX;
    1782             : 
    1783           0 :   return 1;
    1784           0 : }
    1785             : 
    1786             : /* replay_block_complete checks the footer's bank hash, then freezes and
    1787             :    publishes a finalized bank, whose footer certs all verified.  Returns
    1788             :    1 if it marked the bank dead. */
    1789             : 
    1790             : static int
    1791             : replay_block_complete( fd_replay_tile_t *        ctx,
    1792             :                        fd_stem_context_t *       stem,
    1793             :                        fd_bank_t *               bank,
    1794             :                        fd_block_footer_t const * footer,
    1795             :                        ulong                     execution_fees_pre_settle,
    1796           0 :                        ulong                     priority_fees_pre_settle ) {
    1797           0 :   fd_hash_t const * footer_bank_hash = &footer->bank_hash;
    1798           0 :   if( FD_UNLIKELY( memcmp( footer_bank_hash->uc, bank->f.bank_hash.uc, sizeof(fd_hash_t) ) ) ) {
    1799           0 :     FD_BASE58_ENCODE_32_BYTES( footer_bank_hash->uc,   footer_bank_hash_b58   );
    1800           0 :     FD_BASE58_ENCODE_32_BYTES( bank->f.bank_hash.uc, executed_bank_hash_b58 );
    1801           0 :     FD_LOG_WARNING(( "slot %lu: bank hash mismatch, footer declares %s but executed %s. ", bank->f.slot, footer_bank_hash_b58, executed_bank_hash_b58 ));
    1802           0 :     mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_FOOTER, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, footer );
    1803           0 :     return 1;
    1804           0 :   } else {
    1805           0 :     FD_BASE58_ENCODE_32_BYTES( footer_bank_hash->uc,   footer_bank_hash_b58 );
    1806           0 :     FD_BASE58_ENCODE_32_BYTES( bank->f.bank_hash.uc, executed_bank_hash_b58 );
    1807           0 :     FD_LOG_INFO(( "slot %lu: bank hash matches, footer declares %s, executed %s", bank->f.slot, footer_bank_hash_b58, executed_bank_hash_b58 ));
    1808           0 :   }
    1809             : 
    1810             :   /* Copy out cost tracker fields before freezing */
    1811           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1812           0 :   cost_tracker_snap( bank, slot_info );
    1813             : 
    1814             :   /* fetch identity balance infrequently and after set-identity. */
    1815           0 :   slot_info->identity_balance = ULONG_MAX;
    1816           0 :   if( FD_UNLIKELY( ctx->identity_dirty || bank->f.slot%4096UL==0UL ) ) {
    1817           0 :     ctx->identity_dirty = 0;
    1818           0 :     slot_info->identity_balance = fd_accdb_lamports( ctx->accdb, bank->accdb_fork_id, ctx->identity_pubkey->uc );
    1819           0 :   }
    1820             : 
    1821             :   /* Mark the bank as frozen. */
    1822           0 :   fd_block_id_ele_t const * block_id_ele = &ctx->block_id_arr[ bank->idx ];
    1823           0 :   bank->f.block_id = block_id_ele->dmr;
    1824           0 :   fd_banks_mark_bank_frozen( bank );
    1825           0 :   bank->block_completed_nanos = fd_clock_tile_now( ctx->clock );
    1826             : 
    1827             :   /**********************************************************************/
    1828             :   /* Bank hash comparison, and halt if there's a mismatch after replay  */
    1829             :   /**********************************************************************/
    1830             : 
    1831             :   /* Must be last so we can measure completion time correctly, even
    1832             :      though we could technically do this before the hash cmp. */
    1833           0 :   publish_slot_completed( ctx, stem, bank, 0, 0 /* is_leader */, execution_fees_pre_settle, priority_fees_pre_settle, footer );
    1834             : 
    1835             :   /* The footer's finalization cert can also finalize. */
    1836           0 :   if( footer->has_fast_final_cert ) { try_advance_root_ag( ctx, ag_block_id( footer->fast_final_cert.slot, footer->fast_final_cert.block_id.uc ) ); }
    1837           0 :   else if( footer->has_final_cert ) { try_advance_root_ag( ctx, ag_block_id( footer->final_cert.slot,      footer->notar_cert.block_id.uc      ) ); }
    1838             : 
    1839             :   /* If enabled, dump the block to a file and reset the dumping
    1840             :      context state */
    1841           0 :   if( FD_UNLIKELY( ctx->dump_proto_ctx && ctx->dump_proto_ctx->dump_block_to_pb ) ) {
    1842           0 :     fd_dump_block_to_protobuf( ctx->block_dump_ctx, ctx->banks, bank, ctx->accdb, ctx->dump_proto_ctx, ctx->runtime_stack );
    1843           0 :     fd_block_dump_context_reset( ctx->block_dump_ctx );
    1844           0 :   }
    1845           0 :   return 0;
    1846           0 : }
    1847             : 
    1848             : static int
    1849             : replay_block_finalize( fd_replay_tile_t *  ctx,
    1850             :                        fd_stem_context_t * stem,
    1851           0 :                        fd_bank_t *         bank ) {
    1852           0 :   bank->last_transaction_finished_nanos = fd_clock_tile_now( ctx->clock );
    1853             : 
    1854             :   /* Set poh hash in bank. */
    1855           0 :   fd_hash_t * poh = fd_sched_get_poh( ctx->sched, bank->idx );
    1856           0 :   bank->f.poh = *poh;
    1857             : 
    1858             :   /* Set shred count in bank. */
    1859           0 :   bank->f.shred_cnt = fd_sched_get_shred_cnt( ctx->sched, bank->idx );
    1860             : 
    1861           0 :   ulong execution_fees_pre_settle = bank->f.execution_fees;
    1862           0 :   ulong priority_fees_pre_settle  = bank->f.priority_fees;
    1863           0 :   ulong tips_pre_settle           = bank->f.tips;
    1864             : 
    1865           0 :   fd_block_footer_t const * footer = NULL;
    1866           0 :   footer = fd_sched_get_footer( ctx->sched, bank->idx );
    1867             : 
    1868             :   /* Footer certs go to idle exec tiles as cert verify tasks, the rest
    1869             :      verify here, then finalize applies the footer while the tasks run.
    1870             :      The bank completes on its last cert task done. */
    1871           0 :   fd_block_footer_cert_t const * cert  [ FD_ALPENGLOW_FOOTER_CERT_MAX ];
    1872           0 :   uint                           tag   [ FD_ALPENGLOW_FOOTER_CERT_MAX ];
    1873           0 :   ulong                          quorum[ FD_ALPENGLOW_FOOTER_CERT_MAX ];
    1874             : 
    1875           0 :   ulong cnt   = 0UL;
    1876           0 :   ulong tiles = 0UL;
    1877           0 :   if( footer && !ctx->capture_ctx && !ctx->report_runtime_diffs ) {
    1878           0 :     cnt = fd_alpenglow_footer_certs( footer, cert, tag, quorum );
    1879           0 :     tiles = fd_sched_cert_dispatch( ctx->sched, bank->idx, cnt );
    1880           0 :   }
    1881             : 
    1882           0 :   if( FD_UNLIKELY( !tiles ) ) {
    1883             :     /* Nowhere to dispatch to ... just run cert verification here, now. */
    1884           0 :     if( FD_UNLIKELY( fd_runtime_block_execute_finalize( bank, ctx->accdb, ctx->capture_ctx, footer, ctx->shred_version, ctx->footer_cache ) ) ) goto bad_footer;
    1885           0 :   } else {
    1886           0 :     FD_TEST( ctx->shred_version );
    1887           0 :     ulong k = 0UL;
    1888           0 :     for( ; tiles; k++, tiles=fd_ulong_pop_lsb( tiles ) ) {
    1889           0 :       ulong t = (ulong)fd_ulong_find_lsb( tiles );
    1890             : 
    1891           0 :       ctx->cert_payload_sz[ t ]  = fd_alpenglow_cert_payload( cert[ k ], tag[ k ], ctx->shred_version, ctx->cert_payload[ t ] );
    1892             : 
    1893           0 :       blst_p2_affine const * hm = fd_footer_cache_hash_query( ctx->footer_cache, ctx->cert_payload[ t ], ctx->cert_payload_sz[ t ]  );
    1894             : 
    1895           0 :       fd_replay_out_link_t * exec_out = &ctx->exec_out[ t ];
    1896           0 :       fd_execrp_cert_verify_msg_t * msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
    1897           0 :       msg->bank_idx      = bank->idx;
    1898           0 :       msg->quorum        = quorum[ k ];
    1899           0 :       msg->tag           = tag[ k ];
    1900           0 :       msg->shred_version = ctx->shred_version;
    1901           0 :       msg->has_hm        = !!hm;
    1902           0 :       if( hm ) msg->hm   = *hm;
    1903           0 :       *msg->cert         = *cert[ k ];
    1904             : 
    1905           0 :       bank->refcnt++;
    1906             : 
    1907           0 :       fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_CERT_VERIFY<<32) | t, exec_out->chunk, sizeof(fd_execrp_cert_verify_msg_t), 0UL, 0UL, 0UL );
    1908           0 :       exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*msg), exec_out->chunk0, exec_out->wmark );
    1909           0 :     }
    1910           0 :     bank->cert_task_cnt             = k;
    1911           0 :     bank->execution_fees_pre_settle = execution_fees_pre_settle;
    1912           0 :     bank->priority_fees_pre_settle  = priority_fees_pre_settle;
    1913           0 :     for( ulong i=k; i<cnt; i++ ) {
    1914           0 :       if( FD_UNLIKELY( fd_alpenglow_cert_verify( bank, cert[ i ], tag[ i ], quorum[ i ], ctx->shred_version, ctx->footer_cache, NULL, NULL ) ) ) goto bad_footer;
    1915           0 :     }
    1916           0 :     if( FD_UNLIKELY( fd_runtime_block_execute_finalize_unverified( bank, ctx->accdb, footer, ctx->footer_cache ) ) ) goto bad_footer;
    1917           0 :   }
    1918             : 
    1919           0 :   if( FD_UNLIKELY( ctx->report_runtime_diffs ) ) replay_runtime_block_emit( ctx, bank, execution_fees_pre_settle, priority_fees_pre_settle, tips_pre_settle );
    1920             : 
    1921           0 :   if( FD_UNLIKELY( bank->cert_task_cnt ) ) return 0; /* completes on its last cert task done */
    1922           0 :   return replay_block_complete( ctx, stem, bank, footer, execution_fees_pre_settle, priority_fees_pre_settle );
    1923             : 
    1924           0 : bad_footer:
    1925           0 :   mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_FOOTER, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
    1926           0 :   return 1;
    1927           0 : }
    1928             : 
    1929             : static int
    1930             : try_fini_leader( fd_replay_tile_t *  ctx,
    1931           0 :                  fd_stem_context_t * stem ) {
    1932             : 
    1933             :   /* If we are leader, we can only unbecome the leader iff we have
    1934             :      received the poh hash from the poh tile and block id from reasm.
    1935             :      The block id entry is claimed for the leader slot in
    1936             :      prepare_leader_bank, so a slot mismatch here means the claim
    1937             :      discipline broke. */
    1938             : 
    1939           0 :   if( FD_LIKELY( !ctx->is_leader ) ) return 0;
    1940           0 :   if( !ctx->recv_poh ) return 0;
    1941             : 
    1942             :   /* The consensus root only advances through FROZEN banks and the
    1943             :      leader bank is frozen only below, so a root at or past our slot
    1944             :      can never include the leader bank.
    1945             : 
    1946             :      We can't include votor-only messages, because it's possible our
    1947             :      block was retransmitted but replay hasn't processed the FEC yet
    1948             :      (due to bespoke repair <=> replay concurrency).  */
    1949             : 
    1950           0 :   if( FD_UNLIKELY( !ctx->block_id_arr[ ctx->leader_bank->idx ].block_id_seen &&
    1951           0 :                    ctx->consensus_root_slot!=ULONG_MAX &&
    1952           0 :                    ctx->consensus_root_slot>=ctx->leader_bank->f.slot ) ) {
    1953           0 :     ulong bank_idx = ctx->leader_bank->idx;
    1954           0 :     metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_LEADER_END, ctx->leader_bank->f.slot );
    1955           0 :     ctx->leader_bank->refcnt--;
    1956           0 :     ctx->leader_bank = NULL;
    1957           0 :     ctx->recv_poh    = 0;
    1958           0 :     ctx->is_leader   = 0;
    1959           0 :     mark_bank_dead( ctx, stem, bank_idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED, NULL );
    1960           0 :     return 1;
    1961           0 :   }
    1962             : 
    1963           0 :   if( !ctx->block_id_arr[ ctx->leader_bank->idx ].block_id_seen ) return 0;
    1964           0 :   FD_TEST( ctx->block_id_arr[ ctx->leader_bank->idx ].slot==ctx->leader_bank->f.slot );
    1965             : 
    1966           0 :   ulong curr_slot = ctx->leader_bank->f.slot;
    1967             : 
    1968           0 :   ulong execution_fees_pre_settle;
    1969           0 :   ulong priority_fees_pre_settle;
    1970           0 :   ulong tips_pre_settle;
    1971             : 
    1972             :   /* Already finalized above, when the footer was published. */
    1973             : 
    1974           0 :   execution_fees_pre_settle = ctx->leader_execution_fees;
    1975           0 :   priority_fees_pre_settle  = ctx->leader_priority_fees;
    1976           0 :   tips_pre_settle           = ctx->leader_tips;
    1977             : 
    1978           0 :   ctx->leader_bank->f.shred_cnt = (ulong)ctx->block_id_arr[ ctx->leader_bank->idx ].fec_cnt*FD_FEC_SHRED_CNT;
    1979             : 
    1980           0 :   if( FD_UNLIKELY( ctx->report_runtime_diffs ) ) replay_runtime_block_emit( ctx, ctx->leader_bank, execution_fees_pre_settle, priority_fees_pre_settle, tips_pre_settle );
    1981             : 
    1982           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    1983           0 :   cost_tracker_snap( ctx->leader_bank, slot_info );
    1984           0 :   slot_info->identity_balance = ULONG_MAX;
    1985           0 :   if( FD_UNLIKELY( ctx->identity_dirty || curr_slot%4096UL==0UL ) ) {
    1986           0 :     ctx->identity_dirty         = 0;
    1987           0 :     slot_info->identity_balance = fd_accdb_lamports( ctx->accdb, ctx->leader_bank->accdb_fork_id, ctx->identity_pubkey->uc );
    1988           0 :   }
    1989             : 
    1990           0 :   ctx->leader_bank->f.block_id = ctx->block_id_arr[ ctx->leader_bank->idx ].dmr;
    1991           0 :   fd_banks_mark_bank_frozen( ctx->leader_bank );
    1992           0 :   ctx->leader_bank->block_completed_nanos = fd_clock_tile_now( ctx->clock );
    1993             : 
    1994           0 :   publish_slot_completed( ctx, stem, ctx->leader_bank, 0, 1 /* is_leader */, execution_fees_pre_settle, priority_fees_pre_settle, ctx->leader_footer );
    1995             : 
    1996             :   /* The reference on the bank is finally no longer needed. */
    1997           0 :   ctx->leader_bank->refcnt--;
    1998             : 
    1999           0 :   fd_bank_t * completed = ctx->leader_bank;
    2000             : 
    2001             :   /* We are no longer leader so we can clear the bank index we use for
    2002             :      being the leader. */
    2003           0 :   ctx->leader_bank = NULL;
    2004           0 :   ctx->recv_poh    = 0;
    2005           0 :   ctx->is_leader   = 0;
    2006             : 
    2007           0 :   if( FD_UNLIKELY( ctx->next_leader_slot==ULONG_MAX && (curr_slot+1UL)%AG_SLOTS_PER_WINDOW ) ) {
    2008           0 :     *ctx->votor_leader = (fd_votor_leader_t){
    2009           0 :       .slot            = curr_slot+1UL,
    2010           0 :       .parent_slot     = curr_slot,
    2011           0 :       .parent_block_id = ctx->block_id_arr[ completed->idx ].dmr,
    2012           0 :       .parent_ready_ns = ctx->leader_window_start_ns
    2013           0 :     };
    2014           0 :     ctx->next_leader_slot = curr_slot+1UL;
    2015           0 :     if( try_become_leader_ag( ctx, stem, FD_METRICS_SAMPLE_REASON_LEADER_END_START, curr_slot ) ) return 1;
    2016           0 :   }
    2017             : 
    2018           0 :   metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_LEADER_END, curr_slot );
    2019           0 :   return 1;
    2020           0 : }
    2021             : 
    2022             : static void
    2023             : publish_root_advanced( fd_replay_tile_t *  ctx,
    2024             :                        fd_stem_context_t * stem,
    2025           0 :                        fd_bank_t *         bank ) {
    2026             : 
    2027             :   /* If the new consensus root is in the next epoch from the one the
    2028             :      replay tile currently holds, send the next epoch's leader schedule.
    2029             :      We can't use the new root's parent slot safely here. */
    2030           0 :   if( FD_UNLIKELY( bank->f.epoch>fd_slot_to_epoch( &bank->f.epoch_schedule, ctx->notified_root_slot, NULL ) ) ) {
    2031           0 :     fd_runtime_update_next_leaders( bank, ctx->runtime_stack );
    2032           0 :     publish_epoch_info( ctx, stem, bank, 1 );
    2033           0 :   }
    2034             : 
    2035           0 :   if( ctx->rpc_enabled ) {
    2036           0 :     bank->refcnt++;
    2037           0 :     FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for rpc", bank->idx, bank->f.slot, bank->refcnt ));
    2038             : 
    2039             :     /* Under Alpenglow Agave treats a rooted bank as both confirmed
    2040             :        and finalized.  The rpc tile holds an independent reference for
    2041             :        each alias. */
    2042           0 :     bank->refcnt++;
    2043           0 :     FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for rpc (confirmed alias)", bank->idx, bank->f.slot, bank->refcnt ));
    2044           0 :   }
    2045             : 
    2046             :   /* Increment the reference count on the consensus root bank to account
    2047             :      for the number of resolv tiles that are waiting on it. */
    2048           0 :   bank->refcnt += ctx->resolv_tile_cnt;
    2049           0 :   FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for resolv", bank->idx, bank->f.slot, bank->refcnt ));
    2050             : 
    2051           0 :   fd_replay_root_advanced_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    2052             :   /* f.* first: gcc 11 SLP otherwise materialises the 27 KB idx..f.slot gap element-wise */
    2053           0 :   msg->slot      = bank->f.slot;
    2054           0 :   msg->bank_hash = bank->f.bank_hash;
    2055           0 :   msg->block_id  = bank->f.block_id;
    2056           0 :   msg->bank_idx  = bank->idx;
    2057           0 :   msg->bank_seq  = bank->bank_seq;
    2058             : 
    2059           0 :   publish_replay_out( ctx, stem, REPLAY_SIG_ROOT_ADVANCED, sizeof(fd_replay_root_advanced_t) );
    2060             : 
    2061             :   /* Drop every FEC set below the new root from the store. */
    2062             : 
    2063           0 :   fd_store_publish( ctx->store, ctx->map_join, bank->f.slot );
    2064           0 : }
    2065             : 
    2066             : /* publish_boot_slot announces the snapshot slot, which the validator
    2067             :    did not replay, as completed and rooted. */
    2068             : 
    2069             : static void
    2070             : publish_boot_slot( fd_replay_tile_t *  ctx,
    2071             :                    fd_stem_context_t * stem,
    2072           0 :                    fd_bank_t *         bank ) {
    2073           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    2074           0 :   cost_tracker_snap( bank, slot_info );
    2075           0 :   slot_info->identity_balance = fd_accdb_lamports( ctx->accdb, bank->accdb_fork_id, ctx->identity_pubkey->uc );
    2076             : 
    2077           0 :   fd_block_footer_t footer = { .bank_hash = bank->f.bank_hash }; /* dummy footer for the snapshot slot */
    2078           0 :   publish_slot_completed( ctx, stem, bank, 1, 0 /* is_leader */, 0, 0, &footer );
    2079           0 :   publish_root_advanced( ctx, stem, bank );
    2080           0 : }
    2081             : 
    2082             : /* Determine the default slot params to use for slots where no
    2083             :    reduce_slot_time feature gate is in effect. This is important for
    2084             :    the inflation calculations, which use the slot times for
    2085             :    historical slots as input. Therefore we need the same semantics
    2086             :    as Agave, even after the reduce_slot_time feature gates are
    2087             :    active. */
    2088             : static fd_slot_params_t
    2089           0 : restore_default_slot_params( fd_bank_t const * bank ) {
    2090             : 
    2091             :   /* A reduction is effective if the effective ns_per_slot is less than
    2092             :      the 400ms value.
    2093             :      https://github.com/anza-xyz/agave/blob/v4.2/runtime/src/slot_params.rs#L332-L350 */
    2094           0 :   int reduction_effective = fd_slot_params_lookup( &FD_SLOT_PARAMS_400MS,
    2095           0 :                                                    &bank->f.features,
    2096           0 :                                                    &bank->f.epoch_schedule,
    2097           0 :                                                    bank->f.slot ).ns_per_slot < FD_SLOT_PARAMS_400MS.ns_per_slot;
    2098             : 
    2099             :   /* In order to behave correctly in real networks, if a reduction is
    2100             :      effective then we use the 400ms slot params as the default. */
    2101           0 :   if( reduction_effective ) {
    2102           0 :     return FD_SLOT_PARAMS_400MS;
    2103           0 :   }
    2104             : 
    2105             :   /* If a reduction is not effective, then we can rely on the slot
    2106             :      times having remained constant throughout the lifetime of the
    2107             :      cluster, and can use the slot params from the manifest. Note that
    2108             :      in test clusters these may differ from the 400ms values. */
    2109           0 :   return bank->f.slot_params;
    2110           0 : }
    2111             : 
    2112             : /* refresh_vote_account_staked recomputes vote_account_staked from the
    2113             :    root stake delegations and seeds vote_account_inadmissible if stake
    2114             :    effective in the root's epoch was not admitted at its boundary.
    2115             :    Admitted implies staked, so only a non-admitted vote account pays
    2116             :    for the scan.  Called at boot and when the root crosses an epoch. */
    2117             : 
    2118             : static void
    2119             : refresh_vote_account_staked( fd_replay_tile_t * ctx,
    2120           0 :                              fd_bank_t *        bank ) {
    2121           0 :   fd_node_info_t node_info[1]; fd_node_info_read( node_info, ctx->node_info );
    2122           0 :   fd_pubkey_t const * vote_key = &node_info->vote_account;
    2123           0 :   int has_vote_key = !fd_pubkey_check_zero( vote_key );
    2124           0 :   int admitted     = has_vote_key && fd_vote_stakes_query_t_1( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, vote_key, NULL );
    2125           0 :   ctx->vote_account_staked = admitted;
    2126           0 :   if( FD_LIKELY( admitted || !has_vote_key ) ) return;
    2127             : 
    2128           0 :   fd_stake_history_t         stake_history_[1];
    2129           0 :   fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history_view( &bank->f.sysvar_cache, stake_history_ );
    2130           0 :   fd_stake_history_entry_t   status        = fd_stake_delegations_vote_account_status(
    2131           0 :       fd_banks_stake_delegations_root_query( ctx->banks ), vote_key, bank->f.epoch, stake_history,
    2132           0 :       &bank->f.warmup_cooldown_rate_epoch, FD_FEATURE_ACTIVE_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ) );
    2133           0 :   ctx->vote_account_staked       = status.effective || status.activating;
    2134           0 :   ctx->vote_account_inadmissible = !!status.effective;
    2135           0 : }
    2136             : 
    2137             : static void
    2138             : init_after_snapshot( fd_replay_tile_t *  ctx,
    2139           0 :                      fd_stem_context_t * stem ) {
    2140             :   /* snapin built the root stake delegations while writing the accounts
    2141             :      db index.  Refresh finalizes them in memory. */
    2142           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
    2143           0 :   if( FD_UNLIKELY( !bank ) ) {
    2144           0 :     FD_LOG_CRIT(( "invariant violation: replay bank is NULL at bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
    2145           0 :   }
    2146             : 
    2147           0 :   char const * one_offs[ 16UL ];
    2148           0 :   for( ulong i=0UL; i<ctx->enable_features_cnt; i++ ) one_offs[ i ] = ctx->enable_features[ i ];
    2149           0 :   fd_features_enable_one_offs( &bank->f.features, one_offs, (uint)ctx->enable_features_cnt, 0UL );
    2150           0 :   FD_CHECK_CRIT( FD_FEATURE_ACTIVE_BANK( bank, alpenglow ), "alpenglow feature is not active" );
    2151           0 :   bank->f.alpenglow_migration_slot = fd_alpenglow_migration_slot( bank, ctx->accdb );
    2152             : 
    2153             :   /* Set slot params based on the feature gates in the snapshot,
    2154             :      and assert that these are consistent with the values from the
    2155             :      manifest. These assertions match Agave:
    2156             :      https://github.com/anza-xyz/agave/blob/v4.2/runtime/src/bank.rs#L4839-L4869 */
    2157           0 :   fd_slot_params_t manifest_params = bank->f.slot_params;
    2158           0 :   bank->f.slot_params_default      = restore_default_slot_params( bank );
    2159           0 :   bank->f.slot_params              = fd_slot_params_at_slot( bank, bank->f.slot );
    2160           0 :   FD_TEST( bank->f.slot_params.ns_per_slot    == manifest_params.ns_per_slot  );
    2161           0 :   FD_TEST( bank->f.slot_params.slots_per_year == manifest_params.slots_per_year );
    2162             : 
    2163           0 :   fd_runtime_update_next_leaders( bank, ctx->runtime_stack );
    2164           0 :   fd_runtime_update_leaders( bank, ctx->runtime_stack );
    2165             : 
    2166             :   /* Typically, when we cross an epoch boundary during normal
    2167             :      operation, we publish the stake weights for the new epoch.  But
    2168             :      since we are starting from a snapshot, we need to publish two
    2169             :      epochs worth of stake weights: the previous epoch (which is
    2170             :      needed for voting on the current epoch), and the current epoch
    2171             :      (which is needed for voting on the next epoch). */
    2172           0 :   publish_epoch_info( ctx, stem, bank, 0 );
    2173           0 :   publish_epoch_info( ctx, stem, bank, 1 );
    2174           0 :   fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, bank->f.slot );
    2175           0 :   FD_TEST( leader );
    2176           0 :   bank->leader = *leader;
    2177             : 
    2178           0 :   fd_progcache_reset( ctx->progcache );
    2179           0 :   bank->progcache_fork_id = fd_progcache_fork_id_initial();
    2180             : 
    2181           0 :   bank->f.warmup_cooldown_rate_epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, bank->f.features.reduce_stake_warmup_cooldown, NULL );
    2182           0 :   fd_stake_delegations_t * root_delegations = fd_banks_stake_delegations_root_query( ctx->banks );
    2183           0 :   fd_stake_history_t stake_history_[1];
    2184           0 :   fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history_view( &bank->f.sysvar_cache, stake_history_ );
    2185             :   /* Despite claims like https://github.com/solana-program/stake/pull/81
    2186             :      that the stake history sysvar is contiguous, testnet has in fact
    2187             :      had a gap at epoch 386. */
    2188           0 :   if( FD_UNLIKELY( !fd_sysvar_stake_history_is_contiguous( stake_history ) ) ) {
    2189           0 :     FD_LOG_INFO(( "stake history sysvar (covering epoch %lu to %lu over %lu entries) is not contiguous; some fast paths will be disabled", stake_history->entries[ 0 ].epoch, stake_history->entries[ stake_history->len-1UL ].epoch, stake_history->len ));
    2190           0 :   }
    2191           0 :   fd_stake_delegations_refresh(
    2192           0 :       root_delegations,
    2193           0 :       bank->f.epoch,
    2194           0 :       stake_history, /* may be NULL */
    2195           0 :       &bank->f.warmup_cooldown_rate_epoch,
    2196           0 :       FD_FEATURE_ACTIVE_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ),
    2197           0 :       FD_FEATURE_ACTIVE_BANK( bank, remove_inactive_stakes ) );
    2198           0 :   fd_stake_history_entry_t totals = fd_stake_delegations_totals( root_delegations );
    2199           0 :   bank->f.total_effective_stake    = totals.effective;
    2200           0 :   bank->f.total_activating_stake   = totals.activating;
    2201           0 :   bank->f.total_deactivating_stake = totals.deactivating;
    2202             : 
    2203             :   /* Emit the stake-delegations boot baseline from the finalized root
    2204             :      cache (snapshot accepted and refreshed, or genesis loaded), so the
    2205             :      rows match the post-reconciliation cache rather than the raw
    2206             :      account stream. */
    2207           0 :   if( FD_UNLIKELY( ctx->report_runtime_diffs ) ) {
    2208           0 :     fd_stake_delegations_iter_t iter_[1];
    2209           0 :     for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, root_delegations );
    2210           0 :          !fd_stake_delegations_iter_done( iter );
    2211           0 :          fd_stake_delegations_iter_next( iter ) ) {
    2212           0 :       fd_stake_delegation_t const * d = fd_stake_delegations_iter_ele( iter );
    2213           0 :       ulong ae = d->activation_epoch  ==(ushort)USHORT_MAX ? ULONG_MAX : (ulong)d->activation_epoch;
    2214           0 :       ulong de = d->deactivation_epoch==(ushort)USHORT_MAX ? ULONG_MAX : (ulong)d->deactivation_epoch;
    2215           0 :       fd_event_runtime_stake_delegation_bootup_emit( bank->f.slot, bank->f.epoch, d->stake_account.uc,
    2216           0 :                                                      d->vote_account.uc, d->stake, ae, de, d->credits_observed );
    2217           0 :     }
    2218           0 :   }
    2219             : 
    2220           0 :   fd_vote_stakes_refresh( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, ctx->accdb, bank->accdb_fork_id );
    2221             : 
    2222           0 :   refresh_vote_account_staked( ctx, bank );
    2223             : 
    2224             :   /* After both snapshots have been loaded in, we can determine if we should
    2225             :      start distributing rewards. */
    2226             : 
    2227           0 :   fd_rewards_recalculate_partitioned_rewards( bank, ctx->accdb, ctx->runtime_stack, ctx->capture_ctx );
    2228             : 
    2229             :   /* Signals fd_startup_gate */
    2230           0 :   FD_MGAUGE_SET( REPLAY, IS_RUNTIME_LOADED, 1UL );
    2231           0 :   metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_REPLAY_READY, bank->f.slot );
    2232           0 : }
    2233             : 
    2234             : static ulong
    2235             : enforce_nanosecond_clock_bounds( fd_replay_tile_t * ctx,
    2236             :                                  fd_bank_t *        bank,
    2237           0 :                                  ulong              now_nanos ) {
    2238           0 :   long lo, hi;
    2239           0 :   fd_alpenglow_producer_time_bounds( bank, ctx->accdb, &lo, &hi );
    2240           0 :   return (ulong)fd_long_max( lo, fd_long_min( hi, (long)fd_ulong_min( now_nanos, (ulong)LONG_MAX ) ) );
    2241           0 : }
    2242             : 
    2243             : static void
    2244             : process_poh_message( fd_replay_tile_t *                 ctx,
    2245             :                      fd_stem_context_t *                stem,
    2246           0 :                      fd_poh_leader_slot_ended_t const * slot_ended ) {
    2247             : 
    2248           0 :   FD_TEST( ctx->is_booted );
    2249           0 :   FD_TEST( ctx->is_leader );
    2250           0 :   FD_TEST( ctx->leader_bank!=NULL );
    2251             : 
    2252           0 :   FD_TEST( ctx->highwater_leader_slot>=slot_ended->slot );
    2253           0 :   FD_TEST( ctx->next_leader_slot>ctx->highwater_leader_slot );
    2254             : 
    2255           0 :   if( FD_LIKELY( ctx->leader_stats.slot==slot_ended->slot ) ) {
    2256           0 :     ctx->leader_stats.microblock_count = slot_ended->microblock_count;
    2257           0 :     ctx->leader_stats.pack_block_cost  = slot_ended->pack_block_cost;
    2258           0 :     ctx->leader_stats.pack_vote_cost   = slot_ended->pack_vote_cost;
    2259           0 :     ctx->leader_stats.pack_data_bytes  = slot_ended->pack_data_bytes;
    2260           0 :     ctx->leader_stats.bundle_txn_count = slot_ended->bundle_txn_count;
    2261           0 :     ctx->leader_stats.pack_end_reason  = slot_ended->pack_end_reason;
    2262           0 :     ctx->leader_stats.pack_start_nanos = slot_ended->pack_start_ns;
    2263           0 :     ctx->leader_stats.pack_end_nanos   = slot_ended->pack_end_ns;
    2264           0 :     ctx->leader_stats.timing_table_idx = slot_ended->timing_table_idx;
    2265           0 :   }
    2266             : 
    2267             :   /* unpin when we know leader bank done executing txns */
    2268           0 :   fd_cost_tracker_store_unpin( fd_bank_cost_tracker( ctx->leader_bank ), ctx->leader_bank->cost_tracker_fork_id );
    2269             : 
    2270           0 :   if( FD_UNLIKELY( !slot_ended->completed ) ) {
    2271             :     /* The leader slot was aborted by a reset mid-production.  The
    2272             :        block-complete entry was never emitted, so no slot-complete FEC
    2273             :        (and thus no block id) will ever arrive. */
    2274           0 :     ulong bank_idx = ctx->leader_bank->idx;
    2275           0 :     metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_LEADER_END, slot_ended->slot );
    2276           0 :     ctx->leader_bank->refcnt--;
    2277           0 :     ctx->leader_bank = NULL;
    2278           0 :     ctx->recv_poh    = 0;
    2279           0 :     ctx->is_leader   = 0;
    2280           0 :     mark_bank_dead( ctx, stem, bank_idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_RESET, NULL );
    2281           0 :     return;
    2282           0 :   }
    2283             : 
    2284             :   /* Update the poh hash in the bank.  We will want to maintain a refcnt
    2285             :      on the bank until we have received the block id for the block after
    2286             :      it has been shredded. */
    2287             : 
    2288           0 :   memcpy( &ctx->leader_bank->f.poh, slot_ended->blockhash, sizeof(fd_hash_t) );
    2289             : 
    2290           0 :   ctx->recv_poh = 1;
    2291             : 
    2292           0 :   ctx->leader_bank->last_transaction_finished_nanos = fd_clock_tile_now( ctx->clock );
    2293           0 :   fd_sched_block_add_done( ctx->sched, ctx->leader_bank->idx, ctx->leader_bank->parent_idx, ctx->leader_bank->f.slot );
    2294             : 
    2295             :   /* cache fees before runtime zeros */
    2296             : 
    2297           0 :   ctx->leader_execution_fees = ctx->leader_bank->f.execution_fees;
    2298           0 :   ctx->leader_priority_fees  = ctx->leader_bank->f.priority_fees;
    2299           0 :   ctx->leader_tips           = ctx->leader_bank->f.tips;
    2300             : 
    2301           0 :   fd_block_footer_t * footer = ctx->leader_footer;
    2302           0 :   fd_memset( footer, 0, sizeof(fd_block_footer_t) );
    2303           0 :   footer->block_producer_time_nanos = enforce_nanosecond_clock_bounds( ctx, ctx->leader_bank, (ulong)fd_clock_tile_now( ctx->clock ) );
    2304             : 
    2305           0 :   ulong migration_slot = fd_alpenglow_migration_slot( ctx->leader_bank, ctx->accdb );
    2306           0 :   construct_footer_certs( ctx, ctx->leader_bank->f.slot, migration_slot, footer );
    2307             : 
    2308             :   /* The block goes out regardless: the certs are already committed to
    2309             :      the bank hash, so there is nothing left to fall back to. */
    2310           0 :   if( FD_UNLIKELY( fd_runtime_block_execute_finalize( ctx->leader_bank, ctx->accdb, ctx->capture_ctx, footer, ctx->shred_version, ctx->footer_cache ) ) ) {
    2311           0 :     FD_LOG_WARNING(( "slot %lu: our own block footer certs did not apply; the block we produce will be dead to the cluster", ctx->leader_bank->f.slot ));
    2312           0 :   }
    2313           0 :   footer->bank_hash = ctx->leader_bank->f.bank_hash;
    2314             : 
    2315           0 :   publish_leader_footer( ctx, stem, ctx->leader_bank->f.slot, footer );
    2316           0 : }
    2317             : 
    2318             : static void
    2319             : boot_genesis( fd_replay_tile_t *        ctx,
    2320             :               fd_stem_context_t *       stem,
    2321           0 :               fd_genesis_meta_t const * meta ) {
    2322             : 
    2323           0 :   if( FD_UNLIKELY( ctx->wfs_enabled ) ) {
    2324           0 :     FD_LOG_ERR(( "wait_for_supermajority is enabled but the validator is booting from genesis. "
    2325           0 :                  "Remove [consensus.wait_for_supermajority_at_slot] and "
    2326           0 :                  "[consensus.wait_for_supermajority_with_bank_hash] configuration and restart." ));
    2327           0 :   }
    2328             : 
    2329             :   /* Firedancer only boots Alpenglow clusters from genesis.  The
    2330             :      genesis certificate certifies slot 0 with a zero block id. */
    2331           0 :   fd_memset( &ctx->initial_block_id, 0, sizeof(fd_hash_t) );
    2332             : 
    2333             :   /* If we are bootstrapping, we can't wait to wait for our identity
    2334             :      vote to be rooted as this creates a circular dependency. */
    2335           0 :   ctx->identity_vote_rooted = 1;
    2336             : 
    2337           0 :   ctx->caught_up = 1;
    2338           0 :   FD_MGAUGE_SET( REPLAY, IS_CAUGHT_UP, 1UL );
    2339           0 :   metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_CAUGHT_UP, 0UL );
    2340             : 
    2341           0 :   ctx->hard_fork_cnt = 0UL;
    2342             : 
    2343           0 :   uchar const * genesis_blob = (uchar const *)( meta+1 );
    2344           0 :   FD_TEST( meta->bootstrap && meta->has_lthash );
    2345           0 :   FD_TEST( fd_genesis_parse( ctx->genesis, genesis_blob, meta->blob_sz ) );
    2346             : 
    2347           0 :   fd_bank_t * bank = fd_banks_init_bank( ctx->banks );
    2348           0 :   FD_TEST( bank );
    2349           0 :   bank->f.slot = 0UL;
    2350           0 :   FD_TEST( bank->idx==FD_REPLAY_BOOT_BANK_IDX );
    2351             : 
    2352           0 :   static const fd_accdb_fork_id_t accdb_root = { .val = USHORT_MAX };
    2353           0 :   bank->accdb_fork_id = fd_accdb_attach_child( ctx->accdb, accdb_root );
    2354           0 :   bank->parent_accdb_fork_id = bank->accdb_fork_id;
    2355             : 
    2356           0 :   fd_runtime_read_genesis( ctx->banks, bank, ctx->accdb, NULL, &meta->genesis_hash, &meta->lthash, ctx->genesis, genesis_blob, ctx->runtime_stack );
    2357           0 :   FD_TEST( fd_sysvar_cache_restore( bank, ctx->accdb ) );
    2358             : 
    2359           0 :   bank->txncache_fork_id  = fd_txncache_attach_child ( ctx->txncache, (fd_txncache_fork_id_t){USHORT_MAX} );
    2360           0 :   bank->progcache_fork_id = fd_progcache_attach_child( ctx->progcache, fd_progcache_fork_id_initial()     );
    2361             : 
    2362           0 :   fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( &bank->f.block_hash_queue );
    2363           0 :   fd_txncache_finalize_fork( ctx->txncache, bank->txncache_fork_id, 0UL, block_hash->uc );
    2364             : 
    2365             :   /* We call this after fd_runtime_read_genesis, which sets up the
    2366             :      slot_bank needed in blockstore_init. */
    2367           0 :   init_after_snapshot( ctx, stem );
    2368             : 
    2369           0 :   ctx->published_root_slot = 0UL;
    2370           0 :   fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX, 0UL );
    2371             : 
    2372           0 :   bank->f.block_height = 1UL;
    2373             : 
    2374           0 :   ctx->consensus_root          = ctx->initial_block_id;
    2375           0 :   ctx->consensus_root_slot     = 0UL;
    2376           0 :   ctx->finalized_block_id_lo   = ag_block_id( 0UL, ctx->initial_block_id.uc );
    2377           0 :   ctx->finalized_block_id_hi   = ctx->finalized_block_id_lo;
    2378           0 :   ctx->notified_root           = ctx->initial_block_id;
    2379           0 :   ctx->notified_root_slot      = 0UL;
    2380           0 :   ctx->notified_root_bank      = bank;
    2381           0 :   ctx->published_root_slot     = 0UL;
    2382           0 :   ctx->published_root_bank_idx = 0UL;
    2383           0 :   if( FD_UNLIKELY( ctx->snapmk.full_interval_blocks ) ) {
    2384           0 :     ulong interval = ctx->snapmk.full_interval_blocks;
    2385           0 :     ctx->snapmk.next_full_block_height = ((bank->f.block_height/interval)+1UL)*interval;
    2386           0 :   }
    2387           0 :   if( FD_UNLIKELY( ctx->snapmk.incremental_interval_blocks ) ) {
    2388           0 :     ulong interval = ctx->snapmk.incremental_interval_blocks;
    2389           0 :     ctx->snapmk.next_incremental_block_height = ((bank->f.block_height/interval)+1UL)*interval;
    2390           0 :   }
    2391             : 
    2392           0 :   ctx->reset_slot            = 0UL;
    2393           0 :   ctx->reset_dmr             = ctx->initial_block_id;
    2394           0 :   ctx->epoch_end_slot        = 0UL;
    2395           0 :   ctx->slots_per_epoch       = 0UL;
    2396           0 :   ctx->ns_per_slot           = 0UL;
    2397           0 :   ctx->reset_timestamp_nanos = fd_clock_tile_now( ctx->clock );
    2398             : 
    2399           0 :   ctx->has_cluster_type = 1;
    2400             : 
    2401           0 :   ctx->is_booted = 1;
    2402             : 
    2403           0 :   fd_hash_t initial_block_id = ctx->initial_block_id;
    2404             : 
    2405           0 :   fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
    2406           0 :   block_id_ele->latest_mr      = initial_block_id;
    2407           0 :   block_id_ele->dmr            = initial_block_id;
    2408           0 :   block_id_ele->slot           = 0UL;
    2409           0 :   block_id_ele->bank_seq       = bank->bank_seq;
    2410           0 :   block_id_ele->block_id_seen  = 1;
    2411           0 :   block_id_ele->latest_fec_idx = 0U;
    2412           0 :   block_id_ele->block_info     = ag_block_id( 0UL, initial_block_id.uc );
    2413           0 :   bank->f.block_id             = initial_block_id;
    2414           0 :   FD_TEST( fd_ag_block_id_map_ele_insert( ctx->ag_block_id_map, block_id_ele, ctx->block_id_arr ) );
    2415             : 
    2416           0 :   fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    2417           0 :   cost_tracker_snap( bank, slot_info );
    2418             : 
    2419           0 :   slot_info->identity_balance = fd_accdb_lamports( ctx->accdb, bank->accdb_fork_id, ctx->identity_pubkey->uc );
    2420             : 
    2421           0 :   FD_LOG_INFO(( "replay ready at slot %lu (%.3f s after snapshot done, %.3f s since boot)",
    2422           0 :                 0UL, 0.0, (double)(fd_log_wallclock()-ctx->boot_timestamp_nanos)/1e9 ));
    2423             : 
    2424           0 :   fd_block_footer_t footer = { .bank_hash = bank->f.bank_hash  }; /* dummy footer for genesis slot */
    2425           0 :   publish_slot_completed( ctx, stem, bank, 1, 0 /* is_leader */, 0, 0, &footer );
    2426           0 :   publish_root_advanced( ctx, stem, bank );
    2427             : 
    2428           0 :   if( FD_LIKELY( ctx->replay_out->idx!=ULONG_MAX ) ) {
    2429           0 :     fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    2430             : 
    2431           0 :     reset->bank_idx         = bank->idx;
    2432           0 :     reset->timestamp        = ctx->reset_timestamp_nanos;
    2433           0 :     reset->completed_slot   = ctx->reset_slot;
    2434           0 :     reset->hashcnt_per_tick = bank->f.slot_params.hashes_per_tick;
    2435           0 :     reset->ticks_per_slot   = bank->f.ticks_per_slot;
    2436           0 :     reset->tick_duration_ns = bank->f.slot_params.ns_per_slot_adjusted/reset->ticks_per_slot;
    2437             : 
    2438           0 :     fd_memcpy( reset->completed_cmr, &block_id_ele->latest_mr, sizeof(fd_hash_t) );
    2439           0 :     fd_memcpy( reset->completed_dmr, &block_id_ele->dmr,       sizeof(fd_hash_t) );
    2440             : 
    2441           0 :     fd_blockhashes_t const * block_hash_queue = &bank->f.block_hash_queue;
    2442           0 :     fd_hash_t const * last_hash = fd_blockhashes_peek_last_hash( block_hash_queue );
    2443           0 :     FD_TEST( last_hash );
    2444           0 :     fd_memcpy( reset->completed_blockhash, last_hash->uc, sizeof(fd_hash_t) );
    2445             : 
    2446           0 :     reset->max_microblocks_in_slot = fd_poh_max_microblocks_per_slot( bank->f.ticks_per_slot, reset->hashcnt_per_tick );
    2447           0 :     reset->next_leader_slot = ctx->next_leader_slot;
    2448           0 :     reset->wfs_paused       = !ctx->wfs_complete;
    2449             : 
    2450           0 :     publish_replay_out( ctx, stem, REPLAY_SIG_RESET, sizeof(fd_poh_reset_t) );
    2451           0 :   }
    2452           0 : }
    2453             : 
    2454             : static inline void
    2455           0 : maybe_verify_cluster_type( fd_replay_tile_t * ctx ) {
    2456           0 :   if( FD_UNLIKELY( !ctx->has_cluster_type || !ctx->has_genesis_hash ) ) {
    2457           0 :     return;
    2458           0 :   }
    2459             : 
    2460           0 :   FD_BASE58_ENCODE_32_BYTES( ctx->genesis_hash->uc, hash_cstr );
    2461           0 :   ulong cluster = fd_genesis_cluster_identify( hash_cstr );
    2462             :   /* Map pyth-related clusters to unknown. */
    2463           0 :   switch( cluster ) {
    2464           0 :     case FD_CLUSTER_PYTHNET:
    2465           0 :     case FD_CLUSTER_PYTHTEST:
    2466           0 :       cluster = FD_CLUSTER_UNKNOWN;
    2467           0 :   }
    2468             : 
    2469           0 :   if( FD_UNLIKELY( cluster!=ctx->cluster_type ) ) {
    2470           0 :     FD_LOG_ERR(( "Your genesis.bin file at `%s` has a genesis hash of `%s` which means the cluster is %s "
    2471           0 :                  "but the snapshot you loaded is for a different cluster %s. If you are trying to join the "
    2472           0 :                  "%s cluster, you can delete the genesis.bin file and restart the node to download the correct "
    2473           0 :                  "genesis file automatically.",
    2474           0 :                  ctx->genesis_path,
    2475           0 :                  hash_cstr,
    2476           0 :                  fd_genesis_cluster_name( cluster ),
    2477           0 :                  fd_genesis_cluster_name( ctx->cluster_type ),
    2478           0 :                  fd_genesis_cluster_name( cluster ) ));
    2479           0 :   }
    2480           0 : }
    2481             : 
    2482             : static void
    2483             : on_snapshot_message( fd_replay_tile_t *  ctx,
    2484             :                      fd_stem_context_t * stem,
    2485             :                      ulong               in_idx,
    2486             :                      ulong               chunk,
    2487           0 :                      ulong               sig ) {
    2488           0 :   ulong msg = fd_ssmsg_sig_message( sig );
    2489           0 :   if( FD_LIKELY( msg==FD_SSMSG_DONE ) ) {
    2490           0 :     long snapshot_done_nanos = fd_log_wallclock();
    2491             : 
    2492             :     /* An end of message notification indicates the snapshot is loaded.
    2493             :        Replay is able to start executing from this point onwards. */
    2494             :     /* TODO: replay should finish booting. Could make replay a
    2495             :        state machine and set the state here accordingly. */
    2496           0 :     ctx->is_booted = 1;
    2497             : 
    2498           0 :     fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
    2499           0 :     if( FD_UNLIKELY( !bank ) ) {
    2500           0 :       FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
    2501           0 :     }
    2502             : 
    2503           0 :     ulong snapshot_slot = bank->f.slot;
    2504             : 
    2505           0 :     fd_hash_t bank_hash = bank->f.bank_hash;
    2506           0 :     if( FD_UNLIKELY( ctx->wfs_enabled ) ) {
    2507             :       /* Classify boot against WFS (see fd_wfs.h). */
    2508           0 :       int wfs_mode = fd_wfs_mode( ctx->wait_for_supermajority_at_slot,
    2509           0 :                                   ctx->wfs_hash_is_zero,
    2510           0 :                                   (ulong)ctx->expected_shred_version,
    2511           0 :                                   snapshot_slot );
    2512           0 :       switch( wfs_mode ) {
    2513           0 :         case FD_WFS_MODE_MATCH: {
    2514           0 :           if( FD_UNLIKELY( memcmp( ctx->expected_bank_hash.uc, bank_hash.uc, sizeof(fd_hash_t) ) ) ) {
    2515           0 :             FD_BASE58_ENCODE_32_BYTES( ctx->expected_bank_hash.uc, expected_bank_hash_cstr );
    2516           0 :             FD_BASE58_ENCODE_32_BYTES( bank_hash.uc,               actual_bank_hash_cstr );
    2517           0 :             FD_LOG_ERR(( "[consensus.wait_for_supermajority_with_bank_hash] expected_bank_hash=%s does not match snapshot slot"
    2518           0 :                          "=%lu bank_hash=%s. If you are loading a snapshot from the network, check that the slot matches the "
    2519           0 :                          "cluster restart slot.", expected_bank_hash_cstr, snapshot_slot, actual_bank_hash_cstr ));
    2520           0 :           }
    2521             :           /* Not gated on wfs_complete: gossip can signal the cluster
    2522             :              back before we boot, which says nothing about our vote. */
    2523           0 :           if( FD_UNLIKELY( ctx->wait_for_vote_to_start_leader==FD_BOOLAU_AUTO ) ) {
    2524           0 :             FD_LOG_NOTICE(( "auto-overriding wait_for_vote_to_start_leader to false "
    2525           0 :                             "(WFS target slot %lu, boot slot %lu)",
    2526           0 :                             ctx->wait_for_supermajority_at_slot, snapshot_slot ));
    2527           0 :             ctx->wait_for_vote_to_start_leader = FD_BOOLAU_FALSE;
    2528           0 :           }
    2529           0 :           if( FD_LIKELY( !ctx->wfs_complete ) ) {
    2530           0 :             FD_LOG_NOTICE(( "waiting for supermajority at snapshot slot %lu", snapshot_slot ));
    2531           0 :           }
    2532           0 :           break;
    2533           0 :         }
    2534           0 :         case FD_WFS_MODE_ERROR: {
    2535           0 :           FD_LOG_ERR(( "snapshot slot %lu is behind [consensus.wait_for_supermajority_at_slot] %lu "
    2536           0 :                        "and no incremental snapshot bridged the gap. "
    2537           0 :                        "Provide a snapshot at that slot, or clear it and "
    2538           0 :                        "[consensus.wait_for_supermajority_with_bank_hash] to disable WFS.",
    2539           0 :                        snapshot_slot, ctx->wait_for_supermajority_at_slot ));
    2540           0 :           break;
    2541           0 :         }
    2542           0 :         case FD_WFS_MODE_NOOP: {
    2543           0 :           FD_LOG_WARNING(( "snapshot slot %lu is ahead of [consensus.wait_for_supermajority_at_slot] %lu, "
    2544           0 :                            "skipping wait for supermajority and bank hash verification",
    2545           0 :                            snapshot_slot, ctx->wait_for_supermajority_at_slot ));
    2546           0 :           ctx->wfs_complete = 1;
    2547           0 :           ctx->wait_for_vote_to_start_leader = wfs_resolve_leader_gate_auto( ctx->wait_for_vote_to_start_leader );
    2548           0 :           break;
    2549           0 :         }
    2550           0 :         default: {
    2551             :           /* DISABLED and UNRESOLVED are unreachable under wfs_enabled. */
    2552           0 :           FD_LOG_CRIT(( "unexpected WFS mode %d (%s) at boot slot %lu",
    2553           0 :                         wfs_mode, fd_wfs_mode_str( wfs_mode ), snapshot_slot ));
    2554           0 :         }
    2555           0 :       }
    2556             : 
    2557           0 :       if( FD_UNLIKELY( ctx->wait_for_vote_to_start_leader==FD_BOOLAU_AUTO ) ) {
    2558           0 :         FD_LOG_CRIT(( "wait_for_vote_to_start_leader unresolved (auto) after WFS boot classification" ));
    2559           0 :       }
    2560           0 :     }
    2561             : 
    2562             :     /* Manifest message must arrive before DONE */
    2563           0 :     if( FD_UNLIKELY( !ctx->has_expected_genesis_timestamp ) ) {
    2564           0 :       FD_LOG_CRIT(( "snapshot DONE received before manifest" ));
    2565           0 :     }
    2566             : 
    2567             :     /* FIXME: This is a hack when the block id of the snapshot slot
    2568             :        is not provided in the snapshot (Agave versions <4.1). A
    2569             :        possible solution is to get the block id of the snapshot slot
    2570             :        from repair. */
    2571           0 :     fd_hash_t manifest_block_id = ctx->has_manifest_block_id ? ctx->manifest_block_id : ctx->initial_block_id;
    2572             : 
    2573           0 :     FD_TEST( fd_sysvar_cache_restore( bank, ctx->accdb ) );
    2574             :     /* Agave zeroes manifest rent_params; reload from sysvar account */
    2575           0 :     FD_TEST( fd_sysvar_rent_read( ctx->accdb, bank->accdb_fork_id, &bank->f.rent ) );
    2576             : 
    2577           0 :     ctx->consensus_root          = manifest_block_id;
    2578           0 :     ctx->consensus_root_slot     = snapshot_slot;
    2579           0 :     ctx->finalized_block_id_lo   = ag_block_id( snapshot_slot, manifest_block_id.uc );
    2580           0 :     ctx->finalized_block_id_hi   = ctx->finalized_block_id_lo;
    2581           0 :     ctx->notified_root           = manifest_block_id;
    2582           0 :     ctx->notified_root_slot      = snapshot_slot;
    2583           0 :     ctx->notified_root_bank      = bank;
    2584           0 :     ctx->published_root_slot     = ctx->consensus_root_slot;
    2585           0 :     ctx->published_root_bank_idx = 0UL;
    2586           0 :     if( FD_UNLIKELY( ctx->snapmk.full_interval_blocks ) ) {
    2587           0 :       ulong interval = ctx->snapmk.full_interval_blocks;
    2588           0 :       ctx->snapmk.next_full_block_height = ((bank->f.block_height/interval)+1UL)*interval;
    2589           0 :     }
    2590           0 :     if( FD_UNLIKELY( ctx->snapmk.incremental_interval_blocks ) ) {
    2591           0 :       ulong interval = ctx->snapmk.incremental_interval_blocks;
    2592           0 :       ctx->snapmk.next_incremental_block_height = ((bank->f.block_height/interval)+1UL)*interval;
    2593           0 :     }
    2594             : 
    2595           0 :     ctx->reset_slot            = snapshot_slot;
    2596           0 :     ctx->reset_dmr             = manifest_block_id;
    2597           0 :     refresh_epoch_info( ctx, bank, snapshot_slot );
    2598           0 :     ctx->reset_timestamp_nanos = fd_clock_tile_now( ctx->clock );
    2599             : 
    2600           0 :     fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX, snapshot_slot );
    2601           0 :     FD_TEST( bank->idx==0UL );
    2602             : 
    2603           0 :     fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
    2604           0 :     block_id_ele->latest_mr      = manifest_block_id;
    2605           0 :     block_id_ele->dmr            = manifest_block_id;
    2606           0 :     block_id_ele->slot           = snapshot_slot;
    2607           0 :     block_id_ele->bank_seq       = bank->bank_seq;
    2608           0 :     block_id_ele->block_id_seen  = 1;
    2609           0 :     block_id_ele->latest_fec_idx = 0U;
    2610           0 :     bank->f.block_id             = manifest_block_id;
    2611             : 
    2612           0 :     block_id_ele->block_info = ag_block_id( snapshot_slot, manifest_block_id.uc );
    2613           0 :     FD_TEST( fd_ag_block_id_map_ele_insert( ctx->ag_block_id_map, block_id_ele, ctx->block_id_arr ) );
    2614             : 
    2615             :     /* We call this after fd_runtime_read_genesis, which sets up the
    2616             :        slot_bank needed in blockstore_init. */
    2617           0 :     init_after_snapshot( ctx, stem );
    2618             : 
    2619           0 :     long now = fd_log_wallclock();
    2620           0 :     FD_LOG_INFO(( "replay ready at slot %lu (%.3f s after snapshot done, %.3f s since boot)",
    2621           0 :                   snapshot_slot, (double)(now-snapshot_done_nanos)/1e9, (double)(now-ctx->boot_timestamp_nanos)/1e9 ));
    2622             : 
    2623           0 :     publish_boot_slot( ctx, stem, bank );
    2624             : 
    2625           0 :     if( FD_LIKELY( ctx->replay_out->idx!=ULONG_MAX ) ) {
    2626           0 :       fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    2627             : 
    2628           0 :       reset->bank_idx         = bank->idx;
    2629           0 :       reset->timestamp        = ctx->reset_timestamp_nanos;
    2630           0 :       reset->completed_slot   = ctx->reset_slot;
    2631           0 :       reset->hashcnt_per_tick = bank->f.slot_params.hashes_per_tick;
    2632           0 :       reset->ticks_per_slot   = bank->f.ticks_per_slot;
    2633           0 :       reset->tick_duration_ns = bank->f.slot_params.ns_per_slot_adjusted/reset->ticks_per_slot;
    2634             : 
    2635           0 :       fd_memcpy( reset->completed_cmr, &block_id_ele->latest_mr, sizeof(fd_hash_t) );
    2636           0 :       fd_memcpy( reset->completed_dmr, &block_id_ele->dmr,       sizeof(fd_hash_t) );
    2637             : 
    2638           0 :       fd_blockhashes_t const * block_hash_queue = &bank->f.block_hash_queue;
    2639           0 :       fd_hash_t const * last_hash = fd_blockhashes_peek_last_hash( block_hash_queue );
    2640           0 :       FD_TEST( last_hash );
    2641           0 :       fd_memcpy( reset->completed_blockhash, last_hash->uc, sizeof(fd_hash_t) );
    2642             : 
    2643           0 :       reset->max_microblocks_in_slot = fd_poh_max_microblocks_per_slot( bank->f.ticks_per_slot, reset->hashcnt_per_tick );
    2644           0 :       reset->next_leader_slot = ctx->next_leader_slot;
    2645           0 :       reset->wfs_paused       = !ctx->wfs_complete;
    2646             : 
    2647           0 :       publish_replay_out( ctx, stem, REPLAY_SIG_RESET, sizeof(fd_poh_reset_t) );
    2648           0 :     }
    2649             : 
    2650           0 :     return;
    2651           0 :   }
    2652             : 
    2653           0 :   switch( msg ) {
    2654           0 :     case FD_SSMSG_MANIFEST_FULL:
    2655           0 :     case FD_SSMSG_MANIFEST_INCREMENTAL: {
    2656             :       /* We may either receive a full snapshot manifest or an
    2657             :          incremental snapshot manifest.  Note that this external message
    2658             :          id is only used temporarily because replay cannot yet receive
    2659             :          the firedancer-internal snapshot manifest message. */
    2660           0 :       if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
    2661           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 ));
    2662             : 
    2663             :       /* Malformed manifests are rejected recoverably by snapin via
    2664             :          fd_ssload_manifest_validate.  If recover fails here, then the
    2665             :          bank is partially mutated, and we must abort. */
    2666           0 :       if( FD_UNLIKELY( fd_ssload_recover( fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ),
    2667           0 :                                           ctx->banks,
    2668           0 :                                           fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX ),
    2669           0 :                                           ctx->blockhash_seed ) ) ) {
    2670           0 :         FD_LOG_ERR(( "Snapshot manifest recovery failed, aborting." ));
    2671           0 :       }
    2672             : 
    2673           0 :       ctx->has_cluster_type = 1;
    2674           0 :       ctx->cluster_type     = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX )->f.cluster_type;
    2675             : 
    2676           0 :       fd_snapshot_manifest_t const * manifest = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    2677             :       /* hard_fork_cnt already validated by fd_ssload_recover. */
    2678           0 :       ctx->hard_fork_cnt = manifest->hard_fork_cnt;
    2679           0 :       for( ulong i=0UL; i<manifest->hard_fork_cnt; i++ ) {
    2680           0 :         ctx->hard_forks[ i ] = manifest->hard_forks[ i ];
    2681           0 :       }
    2682           0 :       ctx->has_expected_genesis_timestamp = 1;
    2683           0 :       ctx->expected_genesis_timestamp     = manifest->creation_time_seconds;
    2684           0 :       ctx->has_manifest_block_id          = manifest->has_block_id;
    2685           0 :       if( manifest->has_block_id ) memcpy( ctx->manifest_block_id.uc, manifest->block_id, 32UL );
    2686           0 :       if( FD_UNLIKELY( msg==FD_SSMSG_MANIFEST_FULL ) ) {
    2687           0 :         ctx->snapmk.base_slot = manifest->slot;
    2688           0 :       }
    2689           0 :       break;
    2690           0 :     }
    2691           0 :     default: {
    2692           0 :       FD_LOG_ERR(( "Received unknown snapshot message with msg %lu", msg ));
    2693           0 :       return;
    2694           0 :     }
    2695           0 :   }
    2696             : 
    2697           0 :   return;
    2698           0 : }
    2699             : 
    2700             : static void
    2701             : dispatch_task( fd_replay_tile_t *  ctx,
    2702             :                fd_stem_context_t * stem,
    2703           0 :                fd_sched_task_t *   task ) {
    2704             : 
    2705           0 :   switch( task->task_type ) {
    2706           0 :     case FD_SCHED_TT_TXN_EXEC: {
    2707           0 :       fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_exec->txn_idx );
    2708             : 
    2709           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->txn_exec->bank_idx );
    2710           0 :       FD_TEST( bank );
    2711             : 
    2712             :       /* Add the transaction to the block dumper if necessary. This
    2713             :          logic doesn't need to be fork-aware since it's only meant to
    2714             :          be used in backtest. */
    2715           0 :       if( FD_UNLIKELY( ctx->dump_proto_ctx && ctx->dump_proto_ctx->dump_block_to_pb ) ) {
    2716           0 :         fd_dump_block_to_protobuf_collect_tx( ctx->block_dump_ctx, txn_p );
    2717           0 :       }
    2718             : 
    2719           0 :       bank->refcnt++;
    2720             : 
    2721           0 :       fd_cost_tracker_store_pin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
    2722             : 
    2723           0 :       if( FD_UNLIKELY( !bank->first_transaction_scheduled_nanos ) ) bank->first_transaction_scheduled_nanos = fd_clock_tile_now( ctx->clock );
    2724             : 
    2725           0 :       FD_TEST( task->txn_exec->exec_idx<ctx->exec_cnt );
    2726           0 :       fd_replay_out_link_t * exec_out = &ctx->exec_out[ task->txn_exec->exec_idx ];
    2727           0 :       fd_execrp_txn_exec_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
    2728           0 :       fd_txn_p_copy( exec_msg->txn, txn_p );
    2729           0 :       exec_msg->bank_idx = task->txn_exec->bank_idx;
    2730           0 :       exec_msg->txn_idx  = task->txn_exec->txn_idx;
    2731           0 :       memcpy( exec_msg->fec_merkle_root, ctx->block_id_arr[ task->txn_exec->bank_idx ].latest_mr.uc, 32UL );
    2732           0 :       exec_msg->index_in_slot = fd_sched_get_txn_info( ctx->sched, task->txn_exec->txn_idx )->index_in_slot;
    2733           0 :       if( FD_UNLIKELY( ctx->capture_ctx ) ) {
    2734           0 :         exec_msg->capture_txn_idx = ctx->capture_ctx->current_txn_idx++;
    2735           0 :       }
    2736           0 :       fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_TXN_EXEC<<32) | task->txn_exec->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
    2737           0 :       exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
    2738           0 :       break;
    2739           0 :     }
    2740           0 :     case FD_SCHED_TT_TXN_SIGVERIFY: {
    2741           0 :       fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_sigverify->txn_idx );
    2742             : 
    2743           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->txn_sigverify->bank_idx );
    2744           0 :       FD_TEST( bank );
    2745           0 :       bank->refcnt++;
    2746             : 
    2747           0 :       FD_TEST( task->txn_sigverify->exec_idx<ctx->exec_cnt );
    2748           0 :       fd_replay_out_link_t * exec_out = &ctx->exec_out[ task->txn_sigverify->exec_idx ];
    2749           0 :       fd_execrp_txn_sigverify_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
    2750           0 :       fd_txn_p_copy( exec_msg->txn, txn_p );
    2751           0 :       exec_msg->bank_idx = task->txn_sigverify->bank_idx;
    2752           0 :       exec_msg->txn_idx  = task->txn_sigverify->txn_idx;
    2753           0 :       fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_TXN_SIGVERIFY<<32) | task->txn_sigverify->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, 0UL );
    2754           0 :       exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
    2755           0 :       break;
    2756           0 :     };
    2757           0 :     case FD_SCHED_TT_POH_HASH: {
    2758           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->poh_hash->bank_idx );
    2759           0 :       FD_TEST( bank );
    2760           0 :       bank->refcnt++;
    2761             : 
    2762           0 :       FD_TEST( task->poh_hash->exec_idx<ctx->exec_cnt );
    2763           0 :       fd_replay_out_link_t * exec_out = &ctx->exec_out[ task->poh_hash->exec_idx ];
    2764           0 :       fd_execrp_poh_hash_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
    2765           0 :       exec_msg->bank_idx = task->poh_hash->bank_idx;
    2766           0 :       exec_msg->cnt      = task->poh_hash->cnt;
    2767           0 :       exec_msg->hashcnt  = task->poh_hash->hashcnt;
    2768           0 :       FD_STATIC_ASSERT( FD_SCHED_POH_PARA==FD_EXECRP_POH_PARA, poh batch width mismatch );
    2769           0 :       memcpy( exec_msg->hash, task->poh_hash->hash, task->poh_hash->cnt*sizeof(fd_hash_t) );
    2770           0 :       fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_POH_HASH<<32) | task->poh_hash->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, 0UL );
    2771           0 :       exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
    2772           0 :       break;
    2773           0 :     };
    2774           0 :     default: {
    2775           0 :       FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
    2776           0 :     }
    2777           0 :   }
    2778           0 : }
    2779             : 
    2780             : static void
    2781             : mark_bank_dead( fd_replay_tile_t *        ctx,
    2782             :                 fd_stem_context_t *       stem,
    2783             :                 ulong                     bank_idx,
    2784             :                 int                       dead_reason,
    2785             :                 int                       abandoned_reason,
    2786           0 :                 fd_block_footer_t const * footer ) {
    2787           0 :   ulong * dead_idxs     = ctx->dead_idxs;
    2788           0 :   ulong   dead_idxs_cnt = 0UL;
    2789           0 :   fd_banks_mark_bank_dead( ctx->banks, bank_idx, dead_idxs, &dead_idxs_cnt );
    2790             : 
    2791           0 :   fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ bank_idx ];
    2792           0 :   if( block_id_ele->block_id_seen ) publish_slot_dead( ctx, stem, block_id_ele->slot, &block_id_ele->dmr, footer );
    2793           0 :   else if( dead_reason!=FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD && !memcmp( block_id_ele->block_info.hash, ((fd_hash_t){0}).uc, sizeof(fd_hash_t) ) ) publish_slot_dead( ctx, stem, block_id_ele->slot, &(fd_hash_t){ 0 }, footer ); /* an incomplete turbine copy, keyed {slot,0}: rotor frees its eager blk */
    2794             : 
    2795             :   /* Report each newly dead bank now (dead_idxs excludes already-dead,
    2796             :      already-reported subtrees): the failing bank with its real reason and
    2797             :      descendants as parent_dead, or the whole lineage with the caller's
    2798             :      abandoned flavor.  Exactly one of the two reasons is set.
    2799             :      A previously PRUNABLE (evicted) bank converted by this walk reports
    2800             :      here like any other: eviction emits no row, and the sched-drain
    2801             :      emission is gated on the state still being PRUNABLE.  Blocks still
    2802             :      receiving FECs dedup the slot-completion report via dead_reported. */
    2803           0 :   int abandoned = abandoned_reason!=FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
    2804           0 :   for( ulong i=0UL; i<dead_idxs_cnt; i++ ) {
    2805           0 :     fd_block_id_ele_t * ele = &ctx->block_id_arr[ dead_idxs[ i ] ];
    2806           0 :     fd_bank_t *         bank = fd_banks_bank_query( ctx->banks, dead_idxs[ i ] );
    2807           0 :     timing_slot_release( ctx, dead_idxs[ i ] );
    2808           0 :     int dr = abandoned                 ? FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD
    2809           0 :            : dead_idxs[ i ]==bank_idx  ? dead_reason
    2810           0 :            :                             FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_PARENT_DEAD;
    2811           0 :     fd_hash_t * block_id = ele->block_id_seen ? &ele->dmr : &ele->latest_mr;
    2812           0 :     report_block_incomplete( ctx, ele->slot, block_id, bank, dr, abandoned_reason );
    2813           0 :   }
    2814           0 : }
    2815             : 
    2816             : static int
    2817             : try_replay( fd_replay_tile_t *  ctx,
    2818           0 :             fd_stem_context_t * stem ) {
    2819             : 
    2820           0 :   if( FD_UNLIKELY( !ctx->is_booted ) ) return 0;
    2821             : 
    2822             :   /* Hold off executing until the computed shred version is known, so
    2823             :      footer certs verify under it. */
    2824           0 :   if( FD_UNLIKELY( !ctx->shred_version ) ) return 0;
    2825             : 
    2826             :   /* Starting a block attaches an accdb fork, which would spin on a
    2827             :      pending root.  Nothing of the block dispatches before its start. */
    2828           0 :   if( FD_UNLIKELY( fd_sched_block_start_pending( ctx->sched ) && fd_accdb_cmd_pending( ctx->accdb ) ) ) return 0;
    2829             : 
    2830           0 :   int charge_busy = 0;
    2831           0 :   fd_sched_task_t task[ 1 ];
    2832           0 :   if( FD_UNLIKELY( !fd_sched_task_next_ready( ctx->sched, task ) ) ) {
    2833           0 :     return charge_busy; /* Nothing to execute or do. */
    2834           0 :   }
    2835             : 
    2836           0 :   charge_busy = 1;
    2837             : 
    2838           0 :   switch( task->task_type ) {
    2839           0 :     case FD_SCHED_TT_BLOCK_START: {
    2840           0 :       replay_block_start( ctx, task->block_start->bank_idx, task->block_start->parent_bank_idx, task->block_start->slot );
    2841           0 :       fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_START, ULONG_MAX, ULONG_MAX, NULL );
    2842           0 :       break;
    2843           0 :     }
    2844           0 :     case FD_SCHED_TT_BLOCK_END: {
    2845           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->block_end->bank_idx );
    2846           0 :       int dead = 0;
    2847           0 :       if( FD_LIKELY( bank->state==FD_BANK_STATE_REPLAYABLE ) ) dead = replay_block_finalize( ctx, stem, bank );
    2848           0 :       fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_END, ULONG_MAX, ULONG_MAX, NULL );
    2849           0 :       if( FD_UNLIKELY( dead ) ) fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
    2850           0 :       break;
    2851           0 :     }
    2852           0 :     case FD_SCHED_TT_TXN_EXEC:
    2853           0 :     case FD_SCHED_TT_TXN_SIGVERIFY:
    2854           0 :     case FD_SCHED_TT_POH_HASH: {
    2855             :       /* Common case: we have a transaction we need to execute. */
    2856           0 :       dispatch_task( ctx, stem, task );
    2857           0 :       break;
    2858           0 :     }
    2859           0 :     case FD_SCHED_TT_MARK_DEAD: {
    2860           0 :       int dr = sched_block_dead_reason_to_event( ctx, task->mark_dead->bank_idx );
    2861           0 :       int ar = dr==FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD ? FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED
    2862           0 :                                                                  : FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
    2863           0 :       mark_bank_dead( ctx, stem, task->mark_dead->bank_idx, dr, ar, NULL );
    2864           0 :       break;
    2865           0 :     }
    2866           0 :     default: {
    2867           0 :       FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
    2868           0 :     }
    2869           0 :   }
    2870             : 
    2871           0 :   return charge_busy;
    2872           0 : }
    2873             : 
    2874           0 : #define PROCESS_FEC_WAIT 0  /* Put back on dcache for later retry */
    2875           0 : #define PROCESS_FEC_OK   1  /* Process FEC set */
    2876           0 : #define PROCESS_FEC_DROP 2  /* Drop FEC set and notify rotor to start redelivery */
    2877           0 : #define PROCESS_FEC_SKIP 3  /* Good to skip this FEC set, drop from dcache */
    2878             : 
    2879             : /* live_block_id_ele returns ele if it is backed by a live bank (same
    2880             :    bank_seq, not PRUNABLE), else NULL.  Map entries outlive their banks
    2881             :    through eviction.  TODO if we remove block_id_eles with banks, we
    2882             :    can get rid of this */
    2883             : static fd_block_id_ele_t *
    2884             : live_block_id_ele( fd_replay_tile_t *  ctx,
    2885           0 :                    fd_block_id_ele_t * ele ) {
    2886           0 :   if( FD_UNLIKELY( !ele ) ) return NULL;
    2887           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, ele ) );
    2888           0 :   if( FD_UNLIKELY( !bank || bank->bank_seq!=ele->bank_seq || bank->state==FD_BANK_STATE_PRUNABLE ) ) return NULL;
    2889           0 :   return ele;
    2890           0 : }
    2891             : 
    2892             : static int
    2893             : can_process_rotor_fec( fd_replay_tile_t      * ctx,
    2894             :                        fd_rotor_replay_fec_t * fec,
    2895           0 :                        int *                   evict_banks_out ) {
    2896             :   /* We can process a FEC set if a few conditions are met:
    2897             :      - sched has capacity
    2898             :      - banks has capacity.  Evict if we don't (see below)
    2899             :      Our leader FECs bind to the existing leader bank and never enter
    2900             :      sched, so neither capacity gate applies to them.  One that is not
    2901             :      for the current leader slot has no bank to bind to. */
    2902             : 
    2903           0 :   if( FD_UNLIKELY( fec->is_leader && ( !ctx->leader_bank || ctx->leader_bank->f.slot!=fec->slot ) ) ) return PROCESS_FEC_SKIP;
    2904             : 
    2905           0 :   if( FD_UNLIKELY( !fec->is_leader && fd_sched_can_ingest_cnt( ctx->sched )==0UL ) ) {
    2906           0 :     FD_TEST( !fd_sched_is_drained( ctx->sched ) );
    2907           0 :     ctx->metrics.sched_full++;
    2908           0 :     return PROCESS_FEC_WAIT;
    2909           0 :   }
    2910             : 
    2911             :   /* Recovery redelivery re-publishes the entire ancestry path from the
    2912             :      rotor root, but most of those blocks are already replayed.  A
    2913             :      fully replayed block is always keyed by {slot, block_id_real}; a
    2914             :      turbine block still in flight is keyed by {slot, 0}.  Rotor fills
    2915             :      in block_id on redelivered FECs whenever it knows it, so try the id
    2916             :      key first and fall back to the slot key for a turbine version.
    2917             : 
    2918             :      A turbine FEC only counts a {slot, block_id} hit if that block is
    2919             :      complete.  An incomplete one is the repaired copy of the same
    2920             :      block, a different stream that may be ahead of or behind the
    2921             :      turbine copy: rotor merges the two copies into the turbine one
    2922             :      without changing its known_id, so the turbine copy keeps building
    2923             :      its own {slot, 0} bank, and the repaired one is abandoned once the
    2924             :      turbine bank completes and takes its {slot, block_id} key. */
    2925           0 :   {
    2926           0 :     fd_block_id_ele_t * self = NULL;
    2927           0 :     if( FD_LIKELY( !fd_hash_check_zero( &fec->block_id ) ) ) {
    2928           0 :       ag_block_id_t self_key = ag_block_id( fec->slot, fec->block_id.uc );
    2929           0 :       self = live_block_id_ele( ctx, fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &self_key, NULL, ctx->block_id_arr ) );
    2930           0 :       if( FD_UNLIKELY( self && !self->block_id_seen && !fec->known_id ) ) self = NULL;
    2931           0 :     }
    2932           0 :     if( FD_LIKELY( !self && !fec->known_id ) ) {
    2933           0 :       ag_block_id_t self_key = { .slot = fec->slot };
    2934           0 :       self = live_block_id_ele( ctx, fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &self_key, NULL, ctx->block_id_arr ) );
    2935           0 :     }
    2936           0 :     if( FD_UNLIKELY( self && ( self->block_id_seen || self->latest_fec_idx>=fec->fec_set_idx ) ) ) return PROCESS_FEC_SKIP;
    2937           0 :   }
    2938             : 
    2939             :   /* Rooted past: the block is rooted or on a dead fork, so no
    2940             :      redelivery can help.  A DROP would ask for another, and during
    2941             :      catch-up that one also lands behind the backlog, rooted past. */
    2942             : 
    2943           0 :   if( FD_UNLIKELY( ctx->published_root_slot!=ULONG_MAX &&
    2944           0 :                    ( fec->slot<=ctx->published_root_slot ||
    2945           0 :                      fec->parent_slot<ctx->published_root_slot ) ) ) {
    2946           0 :     ctx->metrics.fec_rooted_past++;
    2947           0 :     return PROCESS_FEC_SKIP;
    2948           0 :   }
    2949             : 
    2950           0 :   ulong               parent_bank_idx = UINT_MAX;
    2951           0 :   fd_block_id_ele_t * parent          = NULL;
    2952           0 :   if( FD_UNLIKELY( fec->fec_set_idx==0 ) ) {
    2953           0 :     ag_block_id_t parent_key = ag_block_id( fec->parent_slot, fec->parent_block_id.uc );
    2954           0 :     parent = fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &parent_key, NULL, ctx->block_id_arr );
    2955           0 :     if( FD_UNLIKELY( !parent ) ) {
    2956           0 :       FD_BASE58_ENCODE_32_BYTES( fec->parent_block_id.uc, parent_key_b58 );
    2957           0 :       FD_LOG_INFO(( "parent bank not found for slot %lu fec set idx %u, parent slot %lu parent block_id %s", fec->slot, fec->fec_set_idx, fec->parent_slot, parent_key_b58 ));
    2958             : 
    2959             :       /* We cannot tell here why the parent is missing -- dropped on a
    2960             :          dead lineage, evicted with its tracking slot reused, or rooted
    2961             :          past -- and the latter two are routine and recoverable, so do
    2962             :          not emit events; they would be emitted in bulk during the
    2963             :          redelivery that recovers them. */
    2964           0 :       ctx->metrics.parent_unavailable++;
    2965           0 :       return PROCESS_FEC_DROP;
    2966           0 :     }
    2967           0 :     parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent );
    2968           0 :   } else {
    2969           0 :     ag_block_id_t key = { .slot = fec->slot };
    2970           0 :     if( FD_LIKELY( fec->known_id ) ) key = ag_block_id( fec->slot, fec->block_id.uc );
    2971             : 
    2972           0 :     parent = fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &key, NULL, ctx->block_id_arr );
    2973           0 :     if( FD_UNLIKELY( !parent ) ) {
    2974           0 :       FD_BASE58_ENCODE_32_BYTES( fec->block_id.uc, block_id_b58 );
    2975           0 :       FD_LOG_INFO(( "parent bank not found for slot %lu fec set idx %u slot_bid %s. parent slot %lu", fec->slot, fec->fec_set_idx, block_id_b58, fec->parent_slot ));
    2976           0 :       ctx->metrics.parent_unavailable++;
    2977           0 :       return PROCESS_FEC_DROP; // either pruned or bank evicted
    2978           0 :     }
    2979           0 :     parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent );
    2980           0 :   }
    2981             : 
    2982           0 :   ctx->metrics.reasm_latest_slot    = fec->slot;
    2983           0 :   ctx->metrics.reasm_latest_fec_idx = fec->fec_set_idx;
    2984             : 
    2985             :   /* If the FEC we are building off of is for a prunable bank, we must
    2986             :      wait to process the FEC until the bank has been evicted. */
    2987           0 :   fd_bank_t * parent_fec_bank = parent_bank_idx==UINT_MAX ? NULL : fd_banks_bank_query( ctx->banks, parent_bank_idx );
    2988           0 :   if( FD_UNLIKELY( parent_fec_bank && parent_fec_bank->bank_seq==parent->bank_seq && parent_fec_bank->state==FD_BANK_STATE_PRUNABLE ) ) {
    2989           0 :     FD_LOG_DEBUG(( "waiting to process FEC set (slot=%lu, fec_set_idx=%u) because parent bank is being pruned", fec->slot, fec->fec_set_idx ));
    2990           0 :     return PROCESS_FEC_WAIT;
    2991           0 :   }
    2992             : 
    2993           0 :   if( FD_UNLIKELY( ctx->is_leader &&
    2994           0 :                    fec->fec_set_idx==0U &&
    2995           0 :                    parent_bank_idx==ctx->leader_bank->idx &&
    2996           0 :                    parent->bank_seq==ctx->leader_bank->bank_seq ) ) {
    2997             :     /* This guards against a rare race where we receive the FEC set for
    2998             :        the slot right after our leader rotation before we freeze the
    2999             :        bank for the last slot in our leader rotation.  Leader slot
    3000             :        freezing happens only after if we've received the final PoH hash
    3001             :        from the poh tile as well as the final FEC set for the leader
    3002             :        slot.  So the race happens when FEC sets are delivered and
    3003             :        processed sooner than the PoH hash, aka when the
    3004             :        poh=>shred=>replay path for the block id beats the poh=>replay
    3005             :        path for the poh hash.  To mitigate this race, we must block on
    3006             :        ingesting the FEC set for the ensuing slot before the leader
    3007             :        bank freezes, because that would violate ordering invariants in
    3008             :        banks and sched.  Both the bank index and sequence must match
    3009             :        because bank indices are recycled after eviction. */
    3010           0 :     FD_TEST( ctx->block_id_arr[ ctx->leader_bank->idx ].block_id_seen );
    3011           0 :     FD_TEST( !ctx->recv_poh );
    3012           0 :     ctx->metrics.leader_bid_wait++;
    3013           0 :     return PROCESS_FEC_WAIT;
    3014           0 :   }
    3015             : 
    3016             :   /* Should we evict banks if there are no more free banks?  The answer
    3017             :      is it depends.  Eviction should only happen if we can make no
    3018             :      forward replay progress.  This can only happen if:
    3019             :      1. banks are full
    3020             :      2. sched is drained: pending txns could complete a block and
    3021             :         eventually advance the root.
    3022             :      AND
    3023             :      3. next reasm FEC needs a new bank.  A fec that chains off of a
    3024             :         bank that is already allocated can be processed.  A FEC can
    3025             :         require a new bank in three ways:
    3026             :         - fec_set_idx==0: we don't have any free banks to provision a
    3027             :           new bank for this FEC.
    3028             :         - equivocation: a FEC may be in the middle of a block, but if
    3029             :           it's the first equivocating FEC detected, we need to allocate
    3030             :           a new bank for the version of the block.
    3031             :         - backfill: the parent FEC's bank was never created or has been
    3032             :           evicted and must be reconstructed.
    3033             : 
    3034             : 
    3035             :      - Without reasm there is no backfill: if the parent bank was
    3036             :        evicted or its bank idx was reused, this FEC can't be
    3037             :        processed, so drop it and notify rotor to redeliver.
    3038             :      - Rotor equivocating slots are guaranteed to be delivered starting
    3039             :        from FEC 0, so even equivocations mid-slot are delivered from
    3040             :        fec 0 */
    3041             : 
    3042           0 :   int invalid_parent = !parent_fec_bank || parent_fec_bank->bank_seq!=parent->bank_seq;
    3043           0 :   if( FD_UNLIKELY( invalid_parent ) ) {
    3044           0 :     FD_LOG_INFO(( "parent bank evicted for slot %lu fec set idx %u, parent slot %lu", fec->slot, fec->fec_set_idx, fec->parent_slot ));
    3045           0 :     ctx->metrics.parent_unavailable++;
    3046           0 :     return PROCESS_FEC_DROP;
    3047           0 :   } else if( FD_UNLIKELY( fec->fec_set_idx!=0U && parent->latest_fec_idx!=fec->fec_set_idx - FD_FEC_SHRED_CNT ) ) {
    3048             :     /* Similar to the very first condition in can_process_rotor_fec,
    3049             :        but this would hit if we were halfway through replaying a slot
    3050             :        and then requested redelivery from rotor. Then we can skip
    3051             :        replaying the first half of the slot. */
    3052           0 :     FD_LOG_INFO(( "fec redelivered for slot %lu fec set idx %u, parent slot %lu. bank_idx %lu, latest_fec_idx %u", fec->slot, fec->fec_set_idx, fec->parent_slot, fd_block_id_ele_get_idx( ctx->block_id_arr, parent ), parent->latest_fec_idx ));
    3053           0 :     return PROCESS_FEC_SKIP; // context for slot exists, but this is an earlier or non-contiguous FEC. Safe to skip.
    3054           0 :   }
    3055             : 
    3056           0 :   if( FD_UNLIKELY( !fd_banks_can_start_bank( ctx->banks ) ) ) {
    3057           0 :     int is_new_block = fec->fec_set_idx==0U && !fec->is_leader;
    3058           0 :     if( FD_UNLIKELY( is_new_block ) ) {
    3059           0 :       ctx->metrics.banks_full++;
    3060           0 :       if( FD_UNLIKELY( fd_sched_is_drained( ctx->sched ) && !ctx->snapmk.active ) ) *evict_banks_out = 1; /* see can_process_fec */
    3061           0 :       return PROCESS_FEC_WAIT;
    3062           0 :     }
    3063           0 :   }
    3064             : 
    3065             :   /* Otherwise, banks may not be full, so we can always create a new
    3066             :      bank if needed.  Or, if banks are full, the current fec set's
    3067             :      ancestor (idx 0) already created a bank for this slot. */
    3068           0 :   return PROCESS_FEC_OK;
    3069           0 : }
    3070             : 
    3071             : static int
    3072             : try_notify_consensus_root( fd_replay_tile_t *  ctx,
    3073           0 :                            fd_stem_context_t * stem ) {
    3074             : 
    3075             :   /* Other tiles cannot prune while we are leader - replay may still be
    3076             :      waiting for the remaining FEC sets of our own slots to be
    3077             :      delivered, and if the upstream tiles prune too early, they may fail
    3078             :      to forward our own leader FECs.  Then we would be permanently stuck
    3079             :      in the leader state. */
    3080           0 :   if( FD_UNLIKELY( ctx->is_leader ) ) return 0;
    3081             : 
    3082           0 :   if( FD_LIKELY( ctx->notified_root_slot==ctx->consensus_root_slot &&
    3083           0 :                  fd_hash_eq( &ctx->notified_root, &ctx->consensus_root ) ) ) return 0;
    3084             : 
    3085           0 :   fd_block_id_ele_t * block_id_ele = fd_block_id_ele_query( ctx, &ctx->consensus_root, ctx->consensus_root_slot );
    3086           0 :   if( FD_UNLIKELY( !block_id_ele ) ) return 0;
    3087             : 
    3088           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele ) );
    3089           0 :   if( FD_UNLIKELY( !bank ||
    3090           0 :                    bank->bank_seq!=block_id_ele->bank_seq ||
    3091           0 :                    !fd_hash_eq( &bank->f.block_id, &ctx->consensus_root ) ||
    3092           0 :                    bank->state==FD_BANK_STATE_PRUNABLE ) ) return 0;
    3093             : 
    3094           0 :   fd_sched_root_notify( ctx->sched, bank->idx );
    3095           0 :   publish_root_advanced( ctx, stem, bank );
    3096             : 
    3097           0 :   ctx->notified_root      = ctx->consensus_root;
    3098           0 :   ctx->notified_root_slot = ctx->consensus_root_slot;
    3099           0 :   ctx->notified_root_bank = bank;
    3100           0 :   return 1;
    3101           0 : }
    3102             : 
    3103             : /* Periodic snapshots use block height, matching Agave.  Manually
    3104             :    scheduled snapshots remain slot based. */
    3105             : 
    3106             : static int
    3107             : snapshot_due_for_root( fd_replay_tile_t * ctx,
    3108             :                        ulong              published_root_block_height,
    3109             :                        ulong              advanceable_root_slot,
    3110             :                        ulong              advanceable_root_block_height,
    3111             :                        ulong              consensus_root_block_height,
    3112           0 :                        int *              out_incremental ) {
    3113           0 :   *out_incremental = 0;
    3114           0 :   if( FD_LIKELY( !ctx->snapmk.supported || ctx->snapmk.active ) ) return 0;
    3115             : 
    3116           0 :   int   caught_up   = ctx->caught_up; /* suspend periodic snaps until caught up */
    3117           0 :   ulong full_target = ULONG_MAX;
    3118           0 :   ulong interval    = ctx->snapmk.full_interval_blocks;
    3119           0 :   if( FD_UNLIKELY( interval && caught_up ) ) {
    3120           0 :     if( FD_UNLIKELY( ctx->snapmk.next_full_block_height==ULONG_MAX ) ) {
    3121           0 :       ctx->snapmk.next_full_block_height = ((published_root_block_height/interval)+1UL)*interval;
    3122           0 :     }
    3123             : 
    3124             :     /* If snapshot production fell behind by more than one interval,
    3125             :        skip ahead to the latest due interval. */
    3126           0 :     full_target = fd_ulong_max( ctx->snapmk.next_full_block_height,
    3127           0 :                                 (consensus_root_block_height/interval)*interval );
    3128           0 :   }
    3129             : 
    3130             :   /* Manual slot scheduling always requests a full snapshot. */
    3131           0 :   if( FD_UNLIKELY( advanceable_root_slot>=ctx->snapmk.scheduled_at_slot ) ) return 1;
    3132             : 
    3133             :   /* An incremental snapshot is only possible once a full snapshot
    3134             :      exists to serve as its base. */
    3135           0 :   ulong incremental_target = ULONG_MAX;
    3136           0 :   ulong incr_interval      = ctx->snapmk.incremental_interval_blocks;
    3137           0 :   if( FD_UNLIKELY( incr_interval && caught_up && ctx->snapmk.base_slot!=ULONG_MAX ) ) {
    3138           0 :     if( FD_UNLIKELY( ctx->snapmk.next_incremental_block_height==ULONG_MAX ) ) {
    3139           0 :       ctx->snapmk.next_incremental_block_height = ((published_root_block_height/incr_interval)+1UL)*incr_interval;
    3140           0 :     }
    3141           0 :     incremental_target = fd_ulong_max( ctx->snapmk.next_incremental_block_height,
    3142           0 :                                        (consensus_root_block_height/incr_interval)*incr_interval );
    3143           0 :   }
    3144             : 
    3145             :   /* A full snapshot due at the same block height supersedes the
    3146             :      incremental snapshot. */
    3147           0 :   ulong target      = full_target;
    3148           0 :   int   incremental = incremental_target<full_target;
    3149           0 :   if( FD_UNLIKELY( incremental ) ) target = incremental_target;
    3150           0 :   if( FD_LIKELY( advanceable_root_block_height<target ) ) return 0;
    3151           0 :   *out_incremental = incremental;
    3152           0 :   return 1;
    3153           0 : }
    3154             : 
    3155             : static void
    3156             : snapmk_start( fd_replay_tile_t *  ctx,
    3157             :               fd_stem_context_t * stem,
    3158             :               int                 incremental );
    3159             : 
    3160             : static int
    3161             : try_advance_published_root( fd_replay_tile_t *  ctx,
    3162           0 :                             fd_stem_context_t * stem ) {
    3163             : 
    3164           0 :   if( FD_LIKELY( ctx->published_root_slot==ctx->consensus_root_slot ) ) return 0;
    3165             : 
    3166             :   /* accdb pauses advance_root while producing a snapshot, so submitting
    3167             :      one would stall the next wait_cmd until the snapshot completes. */
    3168           0 :   if( FD_UNLIKELY( ctx->snapmk.active ) ) return 0;
    3169             : 
    3170             :   /* Don't spin in advance_root on the previous root, replay instead and
    3171             :      retry next iteration. */
    3172           0 :   if( FD_UNLIKELY( fd_accdb_cmd_pending( ctx->accdb ) ) ) return 0;
    3173             : 
    3174             :   /* Let a waiting block start take the idle accdb first, else it would
    3175             :      wait out the whole drain. */
    3176           0 :   if( FD_UNLIKELY( fd_sched_block_start_pending( ctx->sched ) ) ) return 0;
    3177             : 
    3178             :   /* If the new root is not available because the bank is/has been
    3179             :      evicted, we can't advance the root.  Try again later. */
    3180             : 
    3181           0 :   fd_block_id_ele_t * block_id_ele = fd_block_id_ele_query( ctx, &ctx->consensus_root, ctx->consensus_root_slot );
    3182           0 :   if( FD_UNLIKELY( !block_id_ele ) ) return 0;
    3183           0 :   fd_bank_t * target_bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele ) );
    3184           0 :   if( FD_UNLIKELY( !target_bank ||
    3185           0 :                    target_bank->bank_seq!=block_id_ele->bank_seq ||
    3186           0 :                    !fd_hash_eq( &target_bank->f.block_id, &ctx->consensus_root ) ||
    3187           0 :                    target_bank->state==FD_BANK_STATE_PRUNABLE ) ) {
    3188           0 :     return 0;
    3189           0 :   }
    3190             : 
    3191           0 :   fd_bank_t * published_root_bank = fd_banks_bank_query( ctx->banks, ctx->published_root_bank_idx );
    3192           0 :   FD_TEST( published_root_bank );
    3193           0 :   ulong published_root_block_height = published_root_bank->f.block_height;
    3194           0 :   ulong consensus_root_block_height = target_bank->f.block_height;
    3195             : 
    3196             :   /* If the identity vote has been seen on a bank that should be rooted,
    3197             :      then we are now ready to produce blocks. */
    3198           0 :   if( FD_UNLIKELY( !ctx->identity_vote_rooted ) ) {
    3199           0 :     if( target_bank->f.identity_vote_idx==ctx->identity_idx ) ctx->identity_vote_rooted = 1;
    3200           0 :   }
    3201             : 
    3202           0 :   ulong advanceable_root_idx = ULONG_MAX;
    3203           0 :   if( FD_UNLIKELY( !fd_banks_advance_root_prepare( ctx->banks, target_bank->idx, &advanceable_root_idx ) ) ) {
    3204           0 :     ctx->metrics.storage_root_behind++;
    3205           0 :     return 0;
    3206           0 :   }
    3207             : 
    3208           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, advanceable_root_idx );
    3209           0 :   FD_TEST( bank );
    3210             : 
    3211           0 :   if( FD_UNLIKELY( advanceable_root_idx>=ctx->block_id_len ) ) {
    3212           0 :     FD_LOG_CRIT(( "invariant violation: advanceable root ele out of bounds [0, %lu) index %lu", ctx->block_id_len, advanceable_root_idx ));
    3213           0 :   }
    3214             :   /* Telemetry: banks still mid-replay (started but never completed) on
    3215             :      forks not descending from the new root are about to be cancelled by
    3216             :      fd_banks_advance_root without ever emitting a block_completed row.
    3217             :      Emit them as abandoned with reason `pruned`: the block was not
    3218             :      invalid, it lost the fork race.  Every live bank is linked in the
    3219             :      bank tree under the current root, so one preorder pass that skips
    3220             :      the new root's subtree visits exactly the banks about to go. */
    3221           0 :   if( FD_UNLIKELY( fd_event_tl ) ) {
    3222           0 :     ulong       root_idx = fd_banks_root( ctx->banks )->idx;
    3223           0 :     ulong       s        = root_idx;
    3224           0 :     fd_bank_t * b        = fd_banks_root( ctx->banks );
    3225           0 :     for(;;) {
    3226           0 :       if( FD_LIKELY( s!=advanceable_root_idx ) ) {
    3227           0 :         if( FD_UNLIKELY( b->state==FD_BANK_STATE_INIT || b->state==FD_BANK_STATE_REPLAYABLE ) ) {
    3228           0 :           fd_block_id_ele_t * ele = &ctx->block_id_arr[ s ];
    3229           0 :           report_block_incomplete( ctx, ele->slot, ele->block_id_seen ? &ele->dmr : &ele->latest_mr, b, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED );
    3230           0 :         }
    3231           0 :         if( FD_LIKELY( b->child_idx!=ULONG_MAX ) ) { s = b->child_idx; b = fd_banks_bank_query( ctx->banks, s ); continue; }
    3232           0 :       }
    3233           0 :       while( s!=root_idx && b->sibling_idx==ULONG_MAX ) { s = b->parent_idx; b = fd_banks_bank_query( ctx->banks, s ); }
    3234           0 :       if( s==root_idx ) break;
    3235           0 :       s = b->sibling_idx; b = fd_banks_bank_query( ctx->banks, s );
    3236           0 :     }
    3237           0 :   }
    3238             : 
    3239           0 :   ulong advanceable_root_slot = bank->f.slot;
    3240           0 :   int   root_epoch_changed    = bank->f.epoch!=published_root_bank->f.epoch;
    3241           0 :   fd_txncache_advance_root( ctx->txncache, bank->txncache_fork_id );
    3242           0 :   fd_progcache_advance_root( ctx->progcache, bank->progcache_fork_id );
    3243           0 :   fd_accdb_advance_root( ctx->accdb, bank->accdb_fork_id );
    3244           0 :   fd_sched_advance_root( ctx->sched, advanceable_root_idx );
    3245           0 :   fd_banks_advance_root( ctx->banks, advanceable_root_idx );
    3246           0 :   if( FD_UNLIKELY( root_epoch_changed ) ) refresh_vote_account_staked( ctx, bank );
    3247             : 
    3248           0 :   for( ulong b=0UL; b<ctx->max_live_slots; b++ ) {
    3249           0 :     if( FD_UNLIKELY( ctx->timing_slot_of_bank[ b ]!=fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) && !fd_banks_bank_query( ctx->banks, b ) ) ) timing_slot_release( ctx, b );
    3250           0 :   }
    3251             : 
    3252           0 :   int snap_incremental;
    3253           0 :   int snap_due = snapshot_due_for_root( ctx,
    3254           0 :                                         published_root_block_height,
    3255           0 :                                         advanceable_root_slot,
    3256           0 :                                         bank->f.block_height,
    3257           0 :                                         consensus_root_block_height,
    3258           0 :                                         &snap_incremental );
    3259             : 
    3260           0 :   ctx->published_root_slot     = advanceable_root_slot;
    3261           0 :   ctx->published_root_bank_idx = advanceable_root_idx;
    3262             : 
    3263           0 :   if( FD_UNLIKELY( snap_due ) ) {
    3264           0 :     snapmk_start( ctx, stem, snap_incremental );
    3265           0 :     if( FD_UNLIKELY( !snap_incremental ) ) {
    3266           0 :       if( FD_UNLIKELY( ctx->snapmk.full_interval_blocks &&
    3267           0 :                        bank->f.block_height>=ctx->snapmk.next_full_block_height ) ) {
    3268           0 :         ulong interval = ctx->snapmk.full_interval_blocks;
    3269           0 :         ctx->snapmk.next_full_block_height = ((bank->f.block_height/interval)+1UL)*interval;
    3270           0 :       }
    3271           0 :       if( FD_UNLIKELY( advanceable_root_slot>=ctx->snapmk.scheduled_at_slot ) ) {
    3272           0 :         ctx->snapmk.scheduled_at_slot = ULONG_MAX;
    3273           0 :       }
    3274           0 :     }
    3275           0 :     if( FD_UNLIKELY( ctx->snapmk.incremental_interval_blocks ) ) {
    3276           0 :       ulong interval = ctx->snapmk.incremental_interval_blocks;
    3277           0 :       ctx->snapmk.next_incremental_block_height = ((bank->f.block_height/interval)+1UL)*interval;
    3278           0 :     }
    3279           0 :   }
    3280             : 
    3281           0 :   return 1;
    3282           0 : }
    3283             : 
    3284             : static int
    3285           0 : try_prune_sched( fd_replay_tile_t * ctx ) {
    3286           0 :   ulong bank_idx;
    3287           0 :   int   pruned = 0;
    3288           0 :   while( (bank_idx=fd_sched_pruned_block_next( ctx->sched ) )!=ULONG_MAX ) {
    3289           0 :     fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
    3290           0 :     FD_TEST( bank );
    3291             :     /* An evicted bank drains here with its verdict final: every
    3292             :        in-flight task has resolved, so still PRUNABLE means none ruled
    3293             :        it invalid (that would have converted it to DEAD and reported a
    3294             :        dead row), and this is the block's only row.  A frozen victim
    3295             :        already reported a completed row. */
    3296           0 :     if( FD_UNLIKELY( bank->state==FD_BANK_STATE_PRUNABLE && !bank->block_completed_nanos ) ) {
    3297           0 :       fd_block_id_ele_t * ele = &ctx->block_id_arr[ bank_idx ];
    3298           0 :       report_block_incomplete( ctx, ele->slot, ele->block_id_seen ? &ele->dmr : &ele->latest_mr, bank,
    3299           0 :                                FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD,
    3300           0 :                                FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_EVICTED );
    3301           0 :     }
    3302           0 :     bank->refcnt--;
    3303           0 :     FD_LOG_DEBUG(( "bank (idx=%lu) refcnt decremented to %lu for sched", bank->idx, bank->refcnt ));
    3304           0 :     pruned = 1;
    3305           0 :   }
    3306           0 :   return pruned;
    3307           0 : }
    3308             : 
    3309             : static int
    3310           0 : try_prune_bank( fd_replay_tile_t * ctx ) {
    3311             :   /* A cancellation purges accdb, which would spin on a pending root.
    3312             :      Check before popping the bank so the cancel info isn't lost. */
    3313           0 :   if( FD_UNLIKELY( fd_accdb_cmd_pending( ctx->accdb ) ) ) return 0;
    3314             : 
    3315           0 :   fd_banks_prune_cancel_info_t cancel_info[ 1 ];
    3316             : 
    3317           0 :   int pruned = fd_banks_prune_one_bank( ctx->banks, cancel_info );
    3318           0 :   switch( pruned ) {
    3319           0 :     case 2: { /* pruning bank + cancellation is needed */
    3320           0 :       fd_txncache_cancel_fork( ctx->txncache,  cancel_info->txncache_fork_id );
    3321           0 :       fd_progcache_cancel_fork( ctx->progcache, cancel_info->progcache_fork_id );
    3322           0 :       fd_accdb_purge( ctx->accdb, cancel_info->accdb_fork_id );
    3323           0 :       __attribute__((fallthrough));
    3324           0 :     }
    3325           0 :     case 1: { /* pruning bank + no cancellation is needed */
    3326             :       /* A sched block exists, and can be marked dead, for a bank as
    3327             :          soon as its first FEC has been ingested, which can happen
    3328             :          before the bank ever set itself up for actual execution (e.g. a
    3329             :          block that parses as bad on its very first FEC).  So always
    3330             :          instruct sched to prune the block whenever banks prunes the
    3331             :          bank.  The txncache/progcache/accdb forks, on the other hand,
    3332             :          are only created once the bank started actual execution. */
    3333           0 :       fd_sched_cancel( ctx->sched, cancel_info->bank_idx );
    3334           0 :       timing_slot_release( ctx, cancel_info->bank_idx );
    3335           0 :       return 1;
    3336           0 :     }
    3337           0 :     case 0: /* no bank to prune */
    3338           0 :       return 0;
    3339           0 :     default:
    3340           0 :       FD_LOG_ERR(( "unreachable" ));
    3341           0 :   }
    3342           0 : }
    3343             : 
    3344             : static void
    3345             : after_credit( fd_replay_tile_t *  ctx,
    3346             :               fd_stem_context_t * stem,
    3347             :               int *               opt_poll_in,
    3348           0 :               int *               charge_busy ) {
    3349           0 :   if( FD_UNLIKELY( ctx->halt_replay && !ctx->is_leader ) ) return;
    3350           0 :   if( FD_UNLIKELY( !ctx->is_booted || !ctx->wfs_complete ) ) return;
    3351             : 
    3352             :   /* The overall priority for the replay tile in order is:
    3353             :      1. Make sure replay has room to progress:
    3354             :         a. evicting pending FECs from the reassembler
    3355             :         b. queueing up evictable banks for pruning if needed
    3356             :         c. clearing any pending bank eviction victims.
    3357             :      2. Drain outstanding bank references from the scheduler.  This
    3358             :         happens after a block gets completed or a fork gets pruned.
    3359             :      3. Notify sched and bank consumers of a new consensus root, then
    3360             :         advance the storage root once old references drain.
    3361             :      4. Replay.  If there is work to do for replay, do it.  This is
    3362             :         more important than ingesting more FEC sets.
    3363             :      5. If replay has nothing to do, ingest more FEC sets.
    3364             :      WARNING: The ordering here is VERY load bearing and it should not
    3365             :      be changed without extreme caution. */
    3366             : 
    3367           0 :   if( FD_UNLIKELY( try_prune_sched( ctx ) ) ) {
    3368           0 :     *charge_busy = 1;
    3369           0 :     *opt_poll_in = 0;
    3370           0 :     return;
    3371           0 :   }
    3372             : 
    3373           0 :   if( FD_UNLIKELY( try_notify_consensus_root( ctx, stem ) ) ) {
    3374           0 :     *charge_busy = 1;
    3375           0 :     *opt_poll_in = 0;
    3376           0 :     return;
    3377           0 :   }
    3378             : 
    3379           0 :   if( FD_UNLIKELY( try_prune_bank( ctx ) ) ) {
    3380           0 :     *charge_busy = 1;
    3381           0 :     *opt_poll_in = 0;
    3382           0 :     return;
    3383           0 :   }
    3384             : 
    3385           0 :   if( FD_UNLIKELY( try_become_leader_ag( ctx, stem, FD_METRICS_SAMPLE_REASON_LEADER_START, 0UL ) ) ) {
    3386           0 :     *charge_busy = 1;
    3387           0 :     *opt_poll_in = 0;
    3388           0 :     return;
    3389           0 :   }
    3390             : 
    3391           0 :   if( FD_UNLIKELY( try_fini_leader( ctx, stem ) ) ) {
    3392           0 :     *charge_busy = 1;
    3393           0 :     *opt_poll_in = 0;
    3394           0 :     return;
    3395           0 :   }
    3396             : 
    3397           0 :   if( FD_UNLIKELY( try_advance_published_root( ctx, stem ) ) ) {
    3398           0 :     *charge_busy = 1;
    3399           0 :     *opt_poll_in = 0;
    3400           0 :     return;
    3401           0 :   }
    3402             : 
    3403           0 :   if( FD_LIKELY( try_replay( ctx, stem ) ) ) {
    3404           0 :     *charge_busy = 1;
    3405           0 :     *opt_poll_in = 0;
    3406           0 :     return;
    3407           0 :   }
    3408             : 
    3409           0 :   ctx->execrp_idle_cnt++;
    3410           0 : }
    3411             : 
    3412             : static int
    3413             : before_frag( fd_replay_tile_t * ctx,
    3414             :              ulong              in_idx,
    3415             :              ulong              seq FD_PARAM_UNUSED,
    3416           0 :              ulong              sig ) {
    3417             : 
    3418           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP_OUT && sig!=FD_GOSSIP_UPDATE_TAG_WFS_DONE ) ) return 1;
    3419           0 :   return 0;
    3420           0 : }
    3421             : 
    3422             : static void
    3423             : process_exec_task_done( fd_replay_tile_t *          ctx,
    3424             :                         fd_stem_context_t *         stem,
    3425             :                         fd_execrp_task_done_msg_t * msg,
    3426           0 :                         ulong                       sig ) {
    3427             : 
    3428           0 :   ulong exec_tile_idx = sig&0xFFFFFFFFUL;
    3429             : 
    3430           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
    3431           0 :   FD_TEST( bank );
    3432           0 :   bank->refcnt--;
    3433             : 
    3434           0 :   switch( sig>>32 ) {
    3435           0 :     case FD_EXECRP_TT_TXN_EXEC: {
    3436           0 :       ulong txn_idx           = msg->txn_exec->txn_idx;
    3437           0 :       ulong max_compute_units = fd_cost_tracker_store_peek( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id )->block_cost_limit;
    3438           0 :       fd_cost_tracker_store_unpin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
    3439             : 
    3440           0 :       if( FD_UNLIKELY( !ctx->identity_vote_rooted ) ) {
    3441             :         /* Query the txn signature against our recently generated vote
    3442             :            txn signatures.  If the query is successful, then we have
    3443             :            seen our own vote transaction land and this should be marked
    3444             :            in the bank.  We go through this exercise until we've seen
    3445             :            our vote rooted. */
    3446           0 :         fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, txn_idx );
    3447             : 
    3448           0 :         fd_pubkey_t * identity_pubkey_out = NULL;
    3449           0 :         if( fd_vote_tracker_query_sig( ctx->vote_tracker, fd_type_pun_const( txn_p->payload+TXN( txn_p )->signature_off ), &identity_pubkey_out ) && fd_pubkey_eq( identity_pubkey_out, ctx->identity_pubkey ) ) {
    3450           0 :           bank->f.identity_vote_idx = ctx->identity_idx;
    3451           0 :         }
    3452           0 :       }
    3453           0 :       if( FD_UNLIKELY( !msg->txn_exec->is_committable && bank->state!=FD_BANK_STATE_DEAD) ) {
    3454             :         /* Every transaction in a valid block has to execute.
    3455             :            Otherwise, we should mark the block as dead.  Non-committable
    3456             :            means the txn failed before account loading or failed the cost
    3457             :            tracker; txn_err distinguishes the cases for telemetry. */
    3458           0 :         int dead_reason;
    3459           0 :         switch( msg->txn_exec->txn_err ) {
    3460           0 :           case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_BLOCK_COST_LIMIT:
    3461           0 :             dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BLOCK_COST_LIMIT;
    3462           0 :             break;
    3463           0 :           case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_ACCOUNT_COST_LIMIT:
    3464           0 :             dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ACCOUNT_COST_LIMIT;
    3465           0 :             break;
    3466           0 :           case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_VOTE_COST_LIMIT:
    3467           0 :             dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_VOTE_COST_LIMIT;
    3468           0 :             break;
    3469           0 :           case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT:
    3470           0 :           case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_ACCOUNT_DATA_TOTAL_LIMIT:
    3471           0 :             dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ACCOUNT_DATA_LIMIT;
    3472           0 :             break;
    3473           0 :           case FD_RUNTIME_TXN_ERR_ACCOUNT_LOADED_TWICE: /* exec-path duplicate, same taxonomy as the chkdup ruling */
    3474           0 :             dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_DUPLICATE_ACCOUNT;
    3475           0 :             break;
    3476           0 :           default:
    3477           0 :             dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TXN_FAILED_TO_LOAD;
    3478           0 :         }
    3479           0 :         mark_bank_dead( ctx, stem, bank->idx, dead_reason, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
    3480           0 :         fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
    3481           0 :       }
    3482           0 :       int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_EXEC, txn_idx, exec_tile_idx, NULL );
    3483           0 :       FD_TEST( res==0 );
    3484           0 :       fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, txn_idx );
    3485           0 :       txn_info->flags |= FD_SCHED_TXN_EXEC_DONE;
    3486           0 :       txn_info->is_simple_vote = msg->txn_exec->is_simple_vote;
    3487           0 :       txn_info->bank_seq = msg->txn_exec->bank_seq;
    3488             : 
    3489           0 :       txn_info->tick_load_start        = msg->txn_exec->tick_load_start;
    3490           0 :       txn_info->tick_check_start       = msg->txn_exec->tick_check_start;
    3491           0 :       txn_info->tick_exec_start        = msg->txn_exec->tick_exec_start;
    3492           0 :       txn_info->tick_commit_start      = msg->txn_exec->tick_commit_start;
    3493           0 :       txn_info->tick_commit_end        = msg->txn_exec->tick_commit_end;
    3494             : 
    3495           0 :       txn_info->compute_units_consumed = msg->txn_exec->compute_units_consumed;
    3496           0 :       txn_info->max_compute_units      = max_compute_units;
    3497           0 :       txn_info->transaction_fee        = msg->txn_exec->transaction_fee;
    3498           0 :       txn_info->priority_fee           = msg->txn_exec->priority_fee;
    3499           0 :       txn_info->tips                   = msg->txn_exec->tips;
    3500           0 :       if( FD_LIKELY( !(txn_info->flags&FD_SCHED_TXN_SIGVERIFY_DONE)||!txn_info->txn_err ) ) { /* Set execution status if sigverify hasn't happened yet or if sigverify was a success. */
    3501           0 :         txn_info->txn_err = msg->txn_exec->txn_err;
    3502           0 :         txn_info->flags  |= fd_ulong_if( msg->txn_exec->is_committable, FD_SCHED_TXN_IS_COMMITTABLE, 0UL );
    3503           0 :         txn_info->flags  |= fd_ulong_if( msg->txn_exec->is_fees_only,   FD_SCHED_TXN_IS_FEES_ONLY,   0UL );
    3504           0 :         txn_info->flags  |= fd_ulong_if( msg->txn_exec->is_noop,        FD_SCHED_TXN_IS_NOOP,        0UL );
    3505           0 :       }
    3506           0 :       if( FD_UNLIKELY( (txn_info->flags&FD_SCHED_TXN_REPLAY_DONE)==FD_SCHED_TXN_REPLAY_DONE ) ) { /* UNLIKELY because generally exec happens before sigverify. */
    3507           0 :         publish_txn_executed( ctx, stem, bank->idx, txn_idx );
    3508           0 :       }
    3509           0 :       break;
    3510           0 :     }
    3511           0 :     case FD_EXECRP_TT_TXN_SIGVERIFY: {
    3512           0 :       ulong txn_idx = msg->txn_sigverify->txn_idx;
    3513           0 :       fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, txn_idx );
    3514           0 :       txn_info->flags |= FD_SCHED_TXN_SIGVERIFY_DONE;
    3515           0 :       if( FD_UNLIKELY( msg->txn_sigverify->err ) ) {
    3516           0 :         txn_info->txn_err = FD_RUNTIME_TXN_ERR_SIGNATURE_FAILURE;
    3517           0 :         txn_info->flags  &= ~FD_SCHED_TXN_IS_COMMITTABLE;
    3518           0 :         txn_info->flags  &= ~FD_SCHED_TXN_IS_FEES_ONLY;
    3519           0 :         txn_info->flags  &= ~FD_SCHED_TXN_IS_NOOP;
    3520           0 :       }
    3521           0 :       if( FD_UNLIKELY( msg->txn_sigverify->err && bank->state!=FD_BANK_STATE_DEAD ) ) {
    3522             :         /* Every transaction in a valid block has to sigverify.
    3523             :            Otherwise, we should mark the block as dead.  Also freeze the
    3524             :            bank if possible. */
    3525           0 :         mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_SIGVERIFY_FAILED, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
    3526           0 :         fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
    3527           0 :       }
    3528           0 :       int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_SIGVERIFY, txn_idx, exec_tile_idx, NULL );
    3529           0 :       FD_TEST( res==0 );
    3530           0 :       if( FD_LIKELY( (txn_info->flags&FD_SCHED_TXN_REPLAY_DONE)==FD_SCHED_TXN_REPLAY_DONE ) ) {
    3531           0 :         publish_txn_executed( ctx, stem, bank->idx, txn_idx );
    3532           0 :       }
    3533           0 :       break;
    3534           0 :     }
    3535           0 :     case FD_EXECRP_TT_POH_HASH: {
    3536           0 :       int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_POH_HASH, ULONG_MAX, exec_tile_idx, msg->poh_hash );
    3537           0 :       if( FD_UNLIKELY( res && bank->state!=FD_BANK_STATE_DEAD ) ) {
    3538           0 :         mark_bank_dead( ctx, stem, bank->idx, sched_dead_reason_to_event( res ), FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
    3539           0 :       }
    3540           0 :       break;
    3541           0 :     }
    3542           0 :     case FD_EXECRP_TT_CERT_VERIFY: {
    3543           0 :       FD_TEST( bank->cert_task_cnt );
    3544           0 :       bank->cert_task_cnt--;
    3545           0 :       if( msg->cert_verify->has_hm ) fd_footer_cache_hash_insert( ctx->footer_cache, ctx->cert_payload[ exec_tile_idx ], ctx->cert_payload_sz[ exec_tile_idx ], &msg->cert_verify->hm );
    3546           0 :       if( FD_UNLIKELY( msg->cert_verify->err && bank->state!=FD_BANK_STATE_DEAD ) ) {
    3547           0 :         mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_FOOTER, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
    3548           0 :         fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
    3549           0 :       }
    3550             :       /* Last one: freeze and publish, before sched can start its child */
    3551           0 :       if( !bank->cert_task_cnt && bank->state==FD_BANK_STATE_REPLAYABLE && !fd_sched_block_is_discarded( ctx->sched, bank->idx ) ) {
    3552           0 :         if( FD_UNLIKELY( replay_block_complete( ctx, stem, bank, fd_sched_get_footer( ctx->sched, bank->idx ), bank->execution_fees_pre_settle, bank->priority_fees_pre_settle ) ) ) {
    3553           0 :           fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
    3554           0 :         }
    3555           0 :       }
    3556           0 :       FD_TEST( !fd_sched_task_done( ctx->sched, FD_SCHED_TT_CERT_VERIFY, ULONG_MAX, exec_tile_idx, NULL ) );
    3557           0 :       break;
    3558           0 :     }
    3559           0 :     default: FD_LOG_CRIT(( "unexpected sig 0x%lx", sig ));
    3560           0 :   }
    3561             : 
    3562             :   /* Reference counter just decreased, and an exec tile just got freed
    3563             :      up.  If there's a need to be more aggressively pruning, we could
    3564             :      check here if more slots just became publishable and publish.  Not
    3565             :      publishing here shouldn't bloat the fork tree too much though.  We
    3566             :      mark minority forks dead as soon as we can, and execution dispatch
    3567             :      stops on dead blocks.  So shortly afterwards, dead blocks should be
    3568             :      eligible for pruning as in-flight transactions retire from the
    3569             :      execution pipeline. */
    3570             : 
    3571           0 : }
    3572             : 
    3573             : /* Essentially an inlined version of try_process_fec that reads off the
    3574             :    dcache directly, instead of the reasm out_queue.
    3575             :    All FECs processed by this function must be safe to be forever
    3576             :    removed from the dcache. */
    3577             : static void
    3578             : process_rotor_fec( fd_replay_tile_t      * ctx,
    3579             :                    fd_stem_context_t     * stem,
    3580           0 :                    fd_rotor_replay_fec_t * fec ) {
    3581           0 :   if( FD_LIKELY( fec->metrics.highest_fec_complete_slot && ( ctx->catch_up_max_fec_slot==ULONG_MAX || fec->metrics.highest_fec_complete_slot>ctx->catch_up_max_fec_slot ) ) ) {
    3582           0 :     ctx->catch_up_max_fec_slot = fec->metrics.highest_fec_complete_slot;
    3583           0 :     ctx->catch_up_tip_advance_cnt++;
    3584           0 :   }
    3585             : 
    3586           0 :   ulong parent_bank_idx = ULONG_MAX;
    3587           0 :   if( FD_UNLIKELY( fec->fec_set_idx==0 ) ) {
    3588           0 :     ag_block_id_t parent_key = ag_block_id( fec->parent_slot, fec->parent_block_id.uc );
    3589           0 :     fd_block_id_ele_t * parent = fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &parent_key, NULL, ctx->block_id_arr );
    3590           0 :     FD_TEST( parent );
    3591           0 :     parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent );
    3592           0 :   } else {
    3593             :     /* mid-slot FEC: rotor tile promises to populate block_id
    3594             :        correctly for blocks with known block id.  If we are processing
    3595             :        the last FEC set of a slot received through turbine though, the
    3596             :        block_id_map will still be keyed with {0}, but the block_id on
    3597             :        the FEC will have the computed DMR.  We first try to replay
    3598             :        this FEC on a bank keyed with the computed DMR - if it doesn't
    3599             :        exist, it must belong to the original turbine version. */
    3600           0 :     ag_block_id_t key = { .slot = fec->slot };
    3601           0 :     if( FD_LIKELY( fec->known_id ) ) {
    3602           0 :       key = ag_block_id( fec->slot, fec->block_id.uc );
    3603           0 :     }
    3604             : 
    3605           0 :     fd_block_id_ele_t * parent = fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &key, NULL, ctx->block_id_arr );
    3606           0 :     FD_TEST( parent );
    3607           0 :     parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent );
    3608           0 :   }
    3609             : 
    3610           0 :   fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
    3611           0 :   FD_TEST( parent_bank );
    3612             : 
    3613           0 :   if( FD_UNLIKELY( parent_bank->state==FD_BANK_STATE_DEAD ) ) {
    3614           0 :     FD_LOG_WARNING(( "parent bank is dead for slot %lu, fec set idx %u, parent slot %lu, dropping", fec->slot, fec->fec_set_idx, fec->parent_slot ));
    3615           0 :     return;
    3616           0 :   }
    3617             : 
    3618           0 :   long work = fd_tickcount();
    3619             : 
    3620           0 :   fd_store_fec_data_view_t data_view[1];
    3621           0 :   fd_rwlock_read( &ctx->store->fec_lock );
    3622           0 :   fd_store_fec_t * store_fec = fd_store_query( ctx->map_join, &fec->mr );
    3623           0 :   ctx->metrics.store_query_cnt++;
    3624           0 :   int view_err = store_fec ? fd_store_fec_data_view( ctx->store, ctx->store_disk_fd, store_fec, data_view ) : -1;
    3625           0 :   fd_rwlock_unread( &ctx->store->fec_lock );
    3626             : 
    3627             :   /* A missing store entry is expected: rotor (the store publisher)
    3628             :      removes FEC sets on publish, so a FEC delivered for a slice that has
    3629             :      since been pruned/rooted is no longer in the store.  The lookup and
    3630             :      payload pin are one lifetime transaction under fec_lock: remove may
    3631             :      unlink the key only after the view has pinned the pool element. */
    3632           0 :   if( FD_UNLIKELY( !store_fec || view_err ) ) {
    3633           0 :     ctx->metrics.store_query_missing_cnt++;
    3634           0 :     FD_BASE58_ENCODE_32_BYTES( fec->mr.key, key_b58 );
    3635           0 :     FD_LOG_INFO(( "store fec for slot: %lu unavailable (pruned by publish); abandoning slice. root: %lu. merkle: %s", fec->slot, ctx->consensus_root_slot, key_b58 ));
    3636           0 :     return;
    3637           0 :   }
    3638             : 
    3639           0 :   long now = fd_log_wallclock();
    3640           0 :   fd_block_id_ele_t * block_id_ele;
    3641           0 :   fd_bank_t *         bank;
    3642           0 :   if( FD_UNLIKELY( fec->fec_set_idx==0U ) ) {
    3643           0 :     bank = fec->is_leader ? ctx->leader_bank : fd_banks_new_bank( ctx->banks, parent_bank_idx, now, 0 );
    3644           0 :     if( FD_UNLIKELY( fec->is_leader && ctx->leader_stats.slot==fec->slot ) ) {
    3645           0 :       ctx->leader_stats.first_fec_returned_nanos = now;
    3646           0 :     }
    3647             : 
    3648           0 :     block_id_ele = &ctx->block_id_arr[ bank->idx ];
    3649             :     /* Clear the previous occupant of this reused bank idx  */
    3650           0 :     if( FD_LIKELY( fd_ag_block_id_map_ele_query( ctx->ag_block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr )==block_id_ele ) ) {
    3651           0 :       FD_TEST( fd_ag_block_id_map_ele_remove( ctx->ag_block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr ) );
    3652           0 :     }
    3653             : 
    3654           0 :     block_id_ele->block_id_seen  = 0;
    3655           0 :     block_id_ele->bank_seq       = bank->bank_seq;
    3656           0 :     block_id_ele->slot           = fec->slot;
    3657           0 :     block_id_ele->latest_fec_idx = 0U;
    3658           0 :     block_id_ele->fec_cnt        = 0U;
    3659           0 :     block_id_ele->block_info     = fec->known_id ? ag_block_id( fec->slot, fec->block_id.uc ) : (ag_block_id_t){ .slot = fec->slot };
    3660             : 
    3661             :     /* If this fec 0 is rebuilding an evicted block (recovery redelivery),
    3662             :        the evicted bank's stale map entry may still hold this key at a
    3663             :        DIFFERENT idx. */
    3664           0 :     fd_ag_block_id_map_ele_remove( ctx->ag_block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr ); // evicted / stale entry
    3665           0 :     FD_TEST( fd_ag_block_id_map_ele_insert( ctx->ag_block_id_map, block_id_ele, ctx->block_id_arr ) );
    3666           0 :   } else { /* FEC for the middle or end of a block */
    3667             :     /* Assign bank idx + seqno to the FEC.  Update block id pool ele.
    3668             :        The block stays keyed by {slot, 0} in the compound map. */
    3669           0 :     block_id_ele = &ctx->block_id_arr[ parent_bank_idx ];
    3670           0 :     block_id_ele->latest_fec_idx = fec->fec_set_idx;
    3671           0 :     bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
    3672           0 :   }
    3673             : 
    3674           0 :   block_id_ele->latest_mr = fec->mr;
    3675             : 
    3676           0 :   if( FD_UNLIKELY( ctx->report_runtime_diffs && block_id_ele->fec_cnt<FD_FEC_BLK_MAX ) ) {
    3677           0 :     ctx->fec_chain[ bank->idx*FD_FEC_BLK_MAX + block_id_ele->fec_cnt ] = fec->mr;
    3678           0 :   }
    3679           0 :   block_id_ele->fec_cnt++;
    3680             : 
    3681           0 :   fd_reception_stats_t * stats = &ctx->reception_stats[ bank->idx ];
    3682           0 :   stats->bank_seq      = bank->bank_seq;
    3683           0 :   stats->fec_set_idx   = fec->fec_set_idx;
    3684           0 :   stats->metrics.rotor = fec->metrics; /* votor_repaired always, the counters only if stats_valid */
    3685             : 
    3686           0 :   if( FD_UNLIKELY( fec->slot_complete ) ) {
    3687           0 :     FD_BASE58_ENCODE_32_BYTES( fec->block_id.uc, block_id_b58 );
    3688           0 :     FD_LOG_INFO(( "slot %lu fec set idx %u slot_bid %s is complete, inserted to map", fec->slot, fec->fec_set_idx, block_id_b58 ));
    3689           0 :     block_id_ele->block_id_seen = 1;
    3690           0 :     block_id_ele->dmr           = fec->block_id;
    3691             : 
    3692             :     /* block is complete and DMR block id is now known so re-key.  The
    3693             :        map does not support duplicate keys: rotor guarantees at most one
    3694             :        delivery stream per block (a turbine copy of a slot with a
    3695             :        votor-driven version is abandoned in the rotor, see
    3696             :        fd_rotor_tile.h), so {slot, block_id} can never already be
    3697             :        occupied by another LIVE bank.  It can however be occupied by a
    3698             :        STALE entry: a previously-completed copy of this block whose bank
    3699             :        was evicted (eviction does not remove map entries), now being
    3700             :        rebuilt through recovery redelivery.  Evict the stale entry before
    3701             :        inserting. */
    3702           0 :     FD_TEST( fd_ag_block_id_map_ele_remove( ctx->ag_block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr )==block_id_ele );
    3703           0 :     ag_block_id_t key = ag_block_id( fec->slot, fec->block_id.uc );
    3704           0 :     block_id_ele->block_info = key;
    3705           0 :     fd_ag_block_id_map_ele_remove( ctx->ag_block_id_map, &key, NULL, ctx->block_id_arr ); // evicted / stale entry
    3706           0 :     FD_TEST( fd_ag_block_id_map_ele_insert( ctx->ag_block_id_map, block_id_ele, ctx->block_id_arr ) );
    3707           0 :   }
    3708             : 
    3709             :   /* For leader FECs, don't insert the FEC into the scheduler. */
    3710           0 :   if( FD_UNLIKELY( fec->is_leader ) ) {
    3711           0 :     fd_store_fec_data_view_release( ctx->store, data_view );
    3712           0 :     return;
    3713           0 :   }
    3714             : 
    3715             :   /* Forks form a partial ordering over FEC sets. The Rotor tile
    3716             :      delivers FEC sets in-order per fork, but FEC set ordering across
    3717             :      forks is arbitrary */
    3718           0 :   fd_sched_fec_t sched_fec[ 1 ];
    3719           0 :   sched_fec->shred_cnt         = FD_FEC_SHRED_CNT;
    3720           0 :   sched_fec->is_last_in_batch  = !!fec->data_complete;
    3721           0 :   sched_fec->is_last_in_block  = !!fec->slot_complete;
    3722           0 :   sched_fec->bank_idx          = bank->idx;
    3723           0 :   sched_fec->parent_bank_idx   = bank->parent_idx;
    3724           0 :   sched_fec->slot              = fec->slot;
    3725           0 :   sched_fec->parent_slot       = fec->parent_slot;
    3726           0 :   sched_fec->is_first_in_block = fec->fec_set_idx==0U;
    3727           0 :   sched_fec->fec               = store_fec;
    3728           0 :   sched_fec->data              = data_view->data;
    3729           0 :   sched_fec->alut_ctx->fork_id = fd_banks_bank_query( ctx->banks, ctx->published_root_bank_idx )->accdb_fork_id;
    3730           0 :   sched_fec->alut_ctx->accdb   = ctx->accdb;
    3731           0 :   sched_fec->alut_ctx->els     = ctx->published_root_slot;
    3732           0 :   sched_fec->completed_ns      = fec->metrics.fec_completed_ts_nanos;
    3733             : 
    3734           0 :   if( sched_fec->is_first_in_block ) {
    3735           0 :     bank->refcnt++;
    3736           0 :     FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for sched", bank->idx, sched_fec->slot, bank->refcnt ));
    3737           0 :   }
    3738             : 
    3739           0 :   int ingested = fd_sched_fec_ingest( ctx->sched, sched_fec );
    3740           0 :   fd_store_fec_data_view_release( ctx->store, data_view );
    3741             : 
    3742           0 :   if( FD_UNLIKELY( !ingested ) ) {
    3743           0 :     int dr = sched_block_dead_reason_to_event( ctx, sched_fec->bank_idx );
    3744           0 :     int ar = dr==FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD ? FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED
    3745           0 :                                                                : FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
    3746           0 :     mark_bank_dead( ctx, stem, sched_fec->bank_idx, dr, ar, NULL );
    3747           0 :     return;
    3748           0 :   }
    3749             : 
    3750           0 :   fd_histf_sample( ctx->metrics.store_query_work, (ulong)( fd_tickcount() - work ) );
    3751           0 :   ctx->execrp_idle_cnt = 0UL;
    3752           0 :   return;
    3753           0 : }
    3754             : 
    3755             : static void
    3756           0 : process_resolv_slot_completed( fd_replay_tile_t * ctx, ulong bank_idx ) {
    3757           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
    3758           0 :   FD_TEST( bank );
    3759           0 :   bank->refcnt--;
    3760           0 :   FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt decremented to %lu for resolv", bank->idx, bank->f.slot, bank->refcnt ));
    3761           0 : }
    3762             : 
    3763             : static void
    3764             : process_vote_txn_sent( fd_replay_tile_t *  ctx,
    3765           0 :                        fd_txn_m_t *        txnm ) {
    3766             :   /* The send tile has signed and sent a vote.  Add this vote to the
    3767             :      vote tracker.  We go through this exercise until the client has
    3768             :      seen a vote corresponding to the current identity rooted. */
    3769           0 :   if( FD_UNLIKELY( !ctx->identity_vote_rooted ) ) {
    3770           0 :     uchar *    payload = (uchar *)txnm + sizeof(fd_txn_m_t);
    3771           0 :     uchar      txn_mem[ FD_TXN_MAX_SZ ] __attribute__((aligned(alignof(fd_txn_t))));
    3772           0 :     fd_txn_t * txn = (fd_txn_t *)txn_mem;
    3773           0 :     if( FD_UNLIKELY( !fd_txn_parse( payload, txnm->payload_sz, txn_mem, NULL ) ) ) {
    3774           0 :       FD_LOG_CRIT(( "Could not parse txn from send tile" ));
    3775           0 :     }
    3776             :     /* The identity of the validator that the signed the vote will
    3777             :        always be the first signer in the vote transaction. */
    3778           0 :     fd_pubkey_t * vote_identity = fd_type_pun( payload+txn->acct_addr_off );
    3779           0 :     fd_vote_tracker_insert( ctx->vote_tracker, vote_identity, fd_type_pun_const( payload+txn->signature_off ) );
    3780           0 :   }
    3781           0 : }
    3782             : 
    3783             : static inline void
    3784           0 : maybe_verify_shred_version( fd_replay_tile_t * ctx ) {
    3785           0 :   if( FD_LIKELY( ctx->has_genesis_hash && ctx->hard_fork_cnt!=ULONG_MAX ) ) {
    3786           0 :     ctx->shred_version = compute_shred_version( ctx->genesis_hash->uc, ctx->hard_forks, ctx->hard_fork_cnt );
    3787           0 :   }
    3788             : 
    3789           0 :   if( FD_LIKELY( ctx->expected_shred_version && ctx->ipecho_shred_version ) ) {
    3790           0 :     if( FD_UNLIKELY( ctx->expected_shred_version!=ctx->ipecho_shred_version ) ) {
    3791           0 :       FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from ipecho", ctx->expected_shred_version, ctx->ipecho_shred_version ) );
    3792           0 :     }
    3793           0 :   }
    3794             : 
    3795             :   /* The configured shred version is the post-restart one.  Compare it
    3796             :      only on the boot bank: before boot the hard forks are the full
    3797             :      snapshot's, predating the restart. */
    3798           0 :   if( FD_UNLIKELY( ctx->wfs_enabled && !ctx->is_booted && ctx->expected_shred_version ) ) return;
    3799             : 
    3800           0 :   if( FD_LIKELY( ctx->shred_version && (ctx->expected_shred_version || ctx->ipecho_shred_version) ) ) {
    3801           0 :     ushort expected_shred_version = ctx->expected_shred_version ? ctx->expected_shred_version : ctx->ipecho_shred_version;
    3802           0 :     ushort actual_shred_version   = ctx->shred_version;
    3803             : 
    3804           0 :     if( FD_UNLIKELY( expected_shred_version!=actual_shred_version ) ) {
    3805           0 :       char const * expected_source = ctx->expected_shred_version ?
    3806           0 :                                      "[consensus.expected_shred_version]" : "the boot entrypoint";
    3807           0 :       FD_LOG_ERR(( "genesis.bin at `%s` plus the loaded snapshot's hard_forks give shred version %hu, "
    3808           0 :                    "but %s reports %hu. Delete genesis.bin to re-download it, or the snapshot if it "
    3809           0 :                    "predates the latest hard fork, and restart.",
    3810           0 :                    ctx->genesis_path, actual_shred_version, expected_source, expected_shred_version ));
    3811           0 :     }
    3812           0 :   }
    3813           0 : }
    3814             : 
    3815             : static inline void
    3816           0 : maybe_verify_genesis_timestamp( fd_replay_tile_t * ctx ) {
    3817           0 :   if( FD_LIKELY( !ctx->has_expected_genesis_timestamp || !ctx->has_genesis_timestamp ) ) return;
    3818           0 :   if( FD_LIKELY( ctx->genesis_timestamp==ctx->expected_genesis_timestamp ) ) return;
    3819             : 
    3820           0 :   FD_LOG_ERR(( "Your genesis.bin file at `%s` has a genesis timestamp of %lu but the snapshot you loaded has a genesis "
    3821           0 :                "timestamp of %lu. This either means that the genesis.bin file you have is for a different cluster than "
    3822           0 :                "the one you are trying to connect to, or you have loaded a snapshot for the wrong cluster. In either "
    3823           0 :                "case, you can delete the problematic file and restart the node to download the correct one automatically.",
    3824           0 :                ctx->genesis_path, ctx->genesis_timestamp, ctx->expected_genesis_timestamp ));
    3825           0 : }
    3826             : 
    3827             : /* snapmk_start instructs the snapmk tile to start producing a snapshot. */
    3828             : 
    3829             : static void
    3830             : snapmk_start( fd_replay_tile_t *  ctx,
    3831             :               fd_stem_context_t * stem,
    3832           0 :               int                 incremental ) {
    3833             : 
    3834           0 :   FD_CHECK_CRIT( !ctx->snapmk.active, "snapshot creation already in progress" );
    3835             : 
    3836             :   /* pin current produced bank */
    3837           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, ctx->published_root_bank_idx );
    3838           0 :   FD_CHECK_CRIT( bank, "invalid published_root_bank_idx" );
    3839             : 
    3840           0 :   if( FD_UNLIKELY( incremental ) ) {
    3841           0 :     FD_CHECK_CRIT( ctx->snapmk.base_slot!=ULONG_MAX, "incremental snapshot without a base full snapshot" );
    3842           0 :     FD_CHECK_CRIT( bank->f.slot>ctx->snapmk.base_slot, "incremental snapshot at or below its base slot" );
    3843           0 :   }
    3844             : 
    3845           0 :   bank->refcnt++;
    3846           0 :   ctx->snapmk.bank_idx    = bank->idx;
    3847           0 :   ctx->snapmk.incremental = !!incremental;
    3848             : 
    3849             :   /* Snapmk reads the bank's epoch credits in place, so they stay
    3850             :      pinned in memory until snapmk_done. */
    3851           0 :   FD_TEST( fd_epoch_credits_view_init( ctx->snapmk.epoch_credits_view, fd_bank_epoch_credits( bank ), bank->epoch_credits_fork_id ) );
    3852             : 
    3853             :   /* Send SNAP_START message to snapmk. */
    3854           0 :   fd_replay_snap_start_t * msg = fd_chunk_to_laddr( ctx->snapmk_out->mem, ctx->snapmk_out->chunk );
    3855           0 :   *msg = (fd_replay_snap_start_t) {
    3856           0 :     .bank_idx  = ctx->published_root_bank_idx,
    3857           0 :     .base_slot = incremental ? ctx->snapmk.base_slot : bank->f.slot,
    3858           0 :     .slot      = bank->f.slot,
    3859           0 :     .leader    = bank->leader
    3860           0 :   };
    3861           0 :   ulong out_idx = ctx->snapmk_out->idx;
    3862           0 :   ulong sig     = REPLAY_SIG_SNAP_START;
    3863           0 :   ulong chunk   = ctx->snapmk_out->chunk;
    3864           0 :   ulong tspub   = fd_frag_meta_ts_comp( fd_tickcount() );
    3865           0 :   ulong sz      = sizeof(fd_replay_snap_start_t);
    3866           0 :   ulong seq     = fd_stem_publish( stem, out_idx, sig, chunk, sz, 0UL, 0UL, tspub );
    3867           0 :   ctx->snapmk_out->chunk = fd_dcache_compact_next( ctx->snapmk_out->chunk, sz, ctx->snapmk_out->chunk0, ctx->snapmk_out->wmark );
    3868             : 
    3869             :   /* wake up the snapmk tile */
    3870           0 :   fd_frag_meta_t * replay_snapmk = stem->mcaches[ out_idx ];
    3871           0 :   ulong *          snap_sync     = fd_mcache_seq_laddr( replay_snapmk );
    3872           0 :   fd_mcache_seq_update( snap_sync, fd_seq_inc( seq, 1UL ) );
    3873           0 :   long ret = syscall( SYS_futex, snap_sync, FUTEX_WAKE, 1 );
    3874           0 :   if( FD_UNLIKELY( ret<0 ) ) {
    3875           0 :     FD_LOG_ERR(( "FUTEX_WAKE(snap_sync,seq=%u) failed (%i-%s)", (uint)seq, errno, fd_io_strerror( errno ) ));
    3876           0 :   }
    3877             : 
    3878             :   /* update internal state */
    3879           0 :   ctx->snapmk.active = 1;
    3880           0 : }
    3881             : 
    3882             : /* snapmk_done reacts to the snapmk tile reporting completion. */
    3883             : 
    3884             : static void
    3885             : snapmk_done( fd_replay_tile_t *  ctx,
    3886             :              fd_stem_context_t * stem,
    3887           0 :              int                 success ) {
    3888           0 :   (void)stem;
    3889             : 
    3890           0 :   FD_CHECK_CRIT( ctx->snapmk.active, "spurious snap complete msg (not creating snapshot)" );
    3891             : 
    3892             :   /* release bank */
    3893           0 :   fd_bank_t * bank = fd_banks_bank_query( ctx->banks, ctx->snapmk.bank_idx );
    3894           0 :   FD_CHECK_CRIT( bank, "invalid snapmk.bank_idx" );
    3895           0 :   FD_CHECK_CRIT( bank->refcnt > 0UL, "invalid snapmk.bank_idx refcnt" );
    3896             : 
    3897             :   /* A completed full snapshot becomes the base of later incrementals. */
    3898           0 :   if( FD_LIKELY( success && !ctx->snapmk.incremental ) ) {
    3899           0 :     ctx->snapmk.base_slot = bank->f.slot;
    3900           0 :   }
    3901             : 
    3902           0 :   if( FD_LIKELY( success ) ) {
    3903           0 :     if( FD_UNLIKELY( ctx->snapmk.incremental ) ) {
    3904           0 :       ctx->snapmk.snap_finished_incr = bank->f.slot;
    3905           0 :       ctx->snapmk.snap_produced_incr_cnt++;
    3906           0 :     } else {
    3907           0 :       ctx->snapmk.snap_finished_full = bank->f.slot;
    3908           0 :     }
    3909           0 :   }
    3910             : 
    3911           0 :   fd_epoch_credits_view_fini( ctx->snapmk.epoch_credits_view );
    3912           0 :   bank->refcnt--;
    3913           0 :   ctx->snapmk.active = 0;
    3914           0 : }
    3915             : 
    3916             : static void
    3917             : msg_snapmk( fd_replay_tile_t *  ctx,
    3918             :             fd_stem_context_t * stem,
    3919             :             ulong               msg_type,
    3920             :             ulong               in_idx,
    3921           0 :             ulong               chunk ) {
    3922           0 :   switch( msg_type ) {
    3923           0 :   case FD_SNAPMK_MSG_FOUND: {
    3924           0 :     fd_snapmk_msg_found_t const * found = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    3925           0 :     if( FD_UNLIKELY( found->base_slot==ULONG_MAX ) ) {
    3926           0 :       if( found->slot > ctx->snapmk.snap_finished_full ) ctx->snapmk.snap_finished_full = found->slot;
    3927           0 :     } else {
    3928           0 :       if( found->slot > ctx->snapmk.snap_finished_incr ) ctx->snapmk.snap_finished_incr = found->slot;
    3929           0 :     }
    3930           0 :     break;
    3931           0 :   }
    3932           0 :   case FD_SNAPMK_MSG_DELETED: {
    3933             :     /* Zeroing loses older surviving snapshots, but keeping a stale
    3934             :        watermark would claim one that no longer exists. */
    3935           0 :     fd_snapmk_msg_deleted_t const * deleted = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    3936           0 :     if( FD_UNLIKELY( deleted->base_slot==ULONG_MAX ) ) {
    3937           0 :       if( deleted->slot==ctx->snapmk.snap_finished_full ) ctx->snapmk.snap_finished_full = 0UL;
    3938           0 :     } else {
    3939           0 :       if( deleted->slot==ctx->snapmk.snap_finished_incr ) ctx->snapmk.snap_finished_incr = 0UL;
    3940           0 :     }
    3941           0 :     break;
    3942           0 :   }
    3943           0 :   case FD_SNAPMK_MSG_CREATED:
    3944           0 :     snapmk_done( ctx, stem, 1 );
    3945           0 :     break;
    3946           0 :   case FD_SNAPMK_MSG_FAILED:
    3947           0 :     snapmk_done( ctx, stem, 0 );
    3948           0 :     break;
    3949           0 :   default:
    3950           0 :     break;
    3951           0 :   }
    3952           0 : }
    3953             : 
    3954             : /* admin command handlers
    3955             :    every admin command must trigger one response frag */
    3956             : 
    3957             : static void
    3958             : admin_respond( fd_replay_tile_t *  ctx,
    3959             :                fd_stem_context_t * stem,
    3960             :                ulong               orig,
    3961           0 :                ulong               err ) {
    3962           0 :   ulong ctl   = fd_frag_meta_ctl( orig, 0, 0, !!err );
    3963           0 :   ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
    3964           0 :   fd_stem_publish( stem, ctx->admin_out_idx, err, 0UL, 0UL, ctl, 0UL, tspub );
    3965           0 : }
    3966             : 
    3967             : static void
    3968             : admin_snap_create( fd_replay_tile_t *  ctx,
    3969             :                    fd_stem_context_t * stem,
    3970           0 :                    ulong               sig ) {
    3971           0 :   ulong target_slot = sig;
    3972             : 
    3973           0 :   if( FD_UNLIKELY( !ctx->snapmk.supported ) ) {
    3974           0 :     FD_LOG_WARNING(( "admin requested snapshot creation, but current config cannot create snapshots. increase [layout.snapzp_tile_count]?" ));
    3975           0 :     admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_ADMINCTL_RESULT_UNSUPPORTED );
    3976           0 :     return;
    3977           0 :   }
    3978             : 
    3979           0 :   if( FD_UNLIKELY( !ctx->is_booted ) ) {
    3980           0 :     FD_LOG_WARNING(( "admin requested snapshot creation, but client has not yet started" ));
    3981           0 :     admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_SNAPSHOT_CREATE_RESULT_NOT_READY );
    3982           0 :     return;
    3983           0 :   }
    3984             : 
    3985           0 :   if( FD_UNLIKELY( target_slot ) ) {
    3986           0 :     if( FD_UNLIKELY( target_slot<=ctx->published_root_slot ) ) {
    3987           0 :       FD_LOG_WARNING(( "admin requested snapshot creation at slot %lu, but rooting is already past it (published root slot %lu)", target_slot, ctx->published_root_slot ));
    3988           0 :       admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_SNAPSHOT_CREATE_RESULT_SLOT_IN_PAST );
    3989           0 :       return;
    3990           0 :     }
    3991             : 
    3992             : 
    3993           0 :     if( FD_UNLIKELY( ctx->snapmk.scheduled_at_slot!=ULONG_MAX &&
    3994           0 :                      ctx->snapmk.scheduled_at_slot!=target_slot ) ) {
    3995           0 :       FD_LOG_WARNING(( "admin requested snapshot creation at slot %lu, but a snapshot is already scheduled at slot %lu. ignoring ...", target_slot, ctx->snapmk.scheduled_at_slot ));
    3996           0 :       admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_SNAPSHOT_CREATE_RESULT_BUSY );
    3997           0 :       return;
    3998           0 :     }
    3999             : 
    4000           0 :     ctx->snapmk.scheduled_at_slot = target_slot;
    4001           0 :     FD_LOG_NOTICE(( "snapshot creation scheduled at slot %lu", target_slot ));
    4002           0 :     admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_ADMINCTL_RESULT_SUCCESS );
    4003           0 :     return;
    4004           0 :   }
    4005             : 
    4006           0 :   if( FD_UNLIKELY( ctx->snapmk.active ) ) {
    4007           0 :     FD_LOG_WARNING(( "admin requested snapshot creation, but currently busy creating another snapshot. ignoring ..." ));
    4008           0 :     admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_SNAPSHOT_CREATE_RESULT_BUSY );
    4009           0 :     return;
    4010           0 :   }
    4011             : 
    4012           0 :   snapmk_start( ctx, stem, 0 );
    4013           0 :   admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_ADMINCTL_RESULT_SUCCESS );
    4014           0 : }
    4015             : 
    4016             : static void
    4017             : msg_admin( fd_replay_tile_t *  ctx,
    4018             :            fd_stem_context_t * stem,
    4019             :            ulong               orig,
    4020           0 :            ulong               sig ) {
    4021           0 :   switch( orig ) {
    4022           0 :   case FD_ADMINCTL_CMD_SNAP_CREATE:
    4023           0 :     admin_snap_create( ctx, stem, sig );
    4024           0 :     break;
    4025           0 :   default:
    4026           0 :     FD_LOG_CRIT(( "unknown admin cmd (orig=%lu, sig=%lu)", orig, sig ));
    4027           0 :   }
    4028           0 : }
    4029             : 
    4030             : static inline int
    4031             : returnable_frag( fd_replay_tile_t *  ctx,
    4032             :                  ulong               in_idx,
    4033             :                  ulong               seq,
    4034             :                  ulong               sig,
    4035             :                  ulong               chunk,
    4036             :                  ulong               sz,
    4037             :                  ulong               ctl,
    4038             :                  ulong               tsorig,
    4039             :                  ulong               tspub,
    4040           0 :                  fd_stem_context_t * stem ) {
    4041           0 :   (void)seq;
    4042           0 :   (void)ctl;
    4043           0 :   (void)tsorig;
    4044           0 :   (void)tspub;
    4045             : 
    4046           0 :   if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
    4047           0 :     FD_LOG_CRIT(( "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 ));
    4048             : 
    4049           0 :   switch( ctx->in_kind[in_idx] ) {
    4050           0 :     case IN_KIND_GENESIS: {
    4051           0 :       fd_genesis_meta_t const * meta = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    4052           0 :       ctx->has_genesis_hash = 1;
    4053           0 :       ctx->has_genesis_timestamp = 1;
    4054           0 :       ctx->genesis_timestamp = meta->creation_time_seconds;
    4055           0 :       *ctx->genesis_hash = meta->genesis_hash;
    4056           0 :       fd_node_info_write_begin( ctx->node_info );
    4057           0 :       ctx->node_info->info.genesis_hash = *ctx->genesis_hash;
    4058           0 :       fd_node_info_write_end( ctx->node_info );
    4059           0 :       if( FD_LIKELY( meta->bootstrap ) ) {
    4060           0 :         boot_genesis( ctx, stem, meta );
    4061           0 :       } else {
    4062           0 :         uchar const * genesis_blob = (uchar const *)( meta+1 );
    4063           0 :         FD_TEST( fd_genesis_parse( ctx->genesis, genesis_blob, meta->blob_sz ) );
    4064           0 :       }
    4065           0 :       ctx->has_genesis_timestamp = 1;
    4066           0 :       ctx->genesis_timestamp     = ctx->genesis->creation_time;
    4067             : 
    4068           0 :       maybe_verify_cluster_type( ctx );
    4069           0 :       maybe_verify_shred_version( ctx );
    4070           0 :       maybe_verify_genesis_timestamp( ctx );
    4071           0 :       break;
    4072           0 :     }
    4073           0 :     case IN_KIND_IPECHO: {
    4074           0 :       FD_TEST( sig && sig<=USHORT_MAX );
    4075           0 :       ctx->ipecho_shred_version = (ushort)sig;
    4076           0 :       maybe_verify_shred_version( ctx );
    4077           0 :       break;
    4078           0 :     }
    4079           0 :     case IN_KIND_SNAP: {
    4080           0 :       on_snapshot_message( ctx, stem, in_idx, chunk, sig );
    4081           0 :       maybe_verify_cluster_type( ctx );
    4082           0 :       maybe_verify_shred_version( ctx );
    4083           0 :       maybe_verify_genesis_timestamp( ctx );
    4084           0 :       break;
    4085           0 :     }
    4086           0 :     case IN_KIND_EXECRP: {
    4087           0 :       process_exec_task_done( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ), sig );
    4088           0 :       ctx->execrp_idle_cnt = 0UL;
    4089           0 :       break;
    4090           0 :     }
    4091           0 :     case IN_KIND_POH: {
    4092           0 :       process_poh_message( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
    4093           0 :       break;
    4094           0 :     }
    4095           0 :     case IN_KIND_RESOLV: {
    4096           0 :       fd_resolv_slot_exchanged_t * exchanged_slot = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    4097           0 :       process_resolv_slot_completed( ctx, exchanged_slot->bank_idx );
    4098           0 :       break;
    4099           0 :     }
    4100           0 :     case IN_KIND_VOTOR: {
    4101           0 :       if( FD_UNLIKELY( sig==FD_VOTOR_SIG_LEADER ) ) {
    4102           0 :         fd_votor_leader_t const * leader = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    4103           0 :         *ctx->votor_leader    = *leader;
    4104           0 :         ctx->next_leader_slot = leader->slot;
    4105           0 :         try_become_leader_ag( ctx, stem, FD_METRICS_SAMPLE_REASON_LEADER_START, 0UL );
    4106           0 :       } else if( FD_LIKELY( sig==FD_VOTOR_SIG_CERTED ) ) {
    4107           0 :         fd_votor_certed_t const * certed = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    4108           0 :         fd_votor_certed_t *       fin    = ctx->votor_final;
    4109           0 :         switch( certed->kind ) {
    4110           0 :         case AG_CERT_KIND_FINAL:
    4111           0 :           if( fin->slot==ULONG_MAX || certed->slot>fin->slot ) *fin = *certed;
    4112           0 :           try_advance_root_ag( ctx, ag_block_id( certed->slot, certed->block_id.uc ) );
    4113           0 :           break;
    4114           0 :         case AG_CERT_KIND_FAST_FINAL: /* fast beats slow at the same slot */
    4115           0 :           if( fin->slot==ULONG_MAX || certed->slot>fin->slot || ( certed->slot==fin->slot && fin->kind==AG_CERT_KIND_FINAL ) ) *fin = *certed;
    4116           0 :           try_advance_root_ag( ctx, ag_block_id( certed->slot, certed->block_id.uc ) );
    4117           0 :           break;
    4118           0 :         default: break;
    4119           0 :         }
    4120           0 :       } else if( FD_UNLIKELY( sig==FD_VOTOR_SIG_PARENT_READY ) ) {
    4121           0 :         fd_votor_parent_ready_t const * pr = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    4122           0 :         ctx->votor_parent_ready_slot = fd_ulong_max( ctx->votor_parent_ready_slot, pr->slot );
    4123           0 :       } else if( FD_UNLIKELY( sig==FD_VOTOR_SIG_REWARD ) ) {
    4124           0 :         fd_votor_reward_t const * reward = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    4125           0 :         fd_votor_reward_t *       ring   = &ctx->votor_reward[ reward->slot%(FD_NUM_SLOTS_FOR_REWARD+AG_SLOTS_PER_WINDOW+1UL) ];
    4126           0 :         if( ring->slot==ULONG_MAX || reward->slot>=ring->slot ) *ring = *reward;
    4127           0 :       }
    4128           0 :       break;
    4129           0 :     }
    4130           0 :     case IN_KIND_REPAIR: {
    4131           0 :       if( FD_UNLIKELY( sig!=ROTOR_SIG_FEC_REPLAY ) ) break;
    4132             : 
    4133             :       /* process rotor incoming FECs. 1 to keep frag for retry, returning 0 consumes it. */
    4134             : 
    4135           0 :       fd_rotor_replay_fec_t * fec = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
    4136           0 :       if( FD_UNLIKELY( !( ctx->execrp_idle_cnt>=2UL*ctx->in_cnt || ctx->is_leader ) ) ) return 1; /* pace behind exec notifications, retry */
    4137             : 
    4138           0 :       int evict_banks = 0;
    4139           0 :       int res = can_process_rotor_fec( ctx, fec, &evict_banks );
    4140             : 
    4141             :       /* drain_rotor_fecs: a bank eviction broke the replayable chain of
    4142             :          FECs delivered from rotor (we lost a parent we needed to
    4143             :          replay off of).  While draining, ignore DROP/SKIP FECs until
    4144             :          one is replayable again (PROCESS_FEC_OK, i.e. its parent
    4145             :          context is present), then resume normal processing.  WAIT must
    4146             :          fall through to the retry/eviction logic below: WAIT's contract
    4147             :          is keep-and-retry, and the redelivered FEC that rebuilds the
    4148             :          evicted block can itself return WAIT+evict_banks (banks full) --
    4149             :          swallowing it would consume the frag, never queue the eviction,
    4150             :          and wedge recovery. */
    4151           0 :       if( FD_UNLIKELY( ctx->drain_rotor_fecs ) ) {
    4152           0 :         if( FD_LIKELY( res==PROCESS_FEC_OK ) ) {
    4153           0 :           ctx->drain_rotor_fecs = 0; /* chain re-established, resume */
    4154           0 :         }
    4155           0 :         else if( res!=PROCESS_FEC_WAIT ) return 0; /* swallow DROPs/SKIPs, do evict_banks if needed */
    4156           0 :       }
    4157             : 
    4158           0 :       switch( res ) {
    4159           0 :         case PROCESS_FEC_OK: {
    4160           0 :           process_rotor_fec( ctx, stem, fec );
    4161           0 :           return 0;
    4162           0 :         }
    4163           0 :         case PROCESS_FEC_DROP: {
    4164             :           /* enter drain state */
    4165           0 :           ctx->drain_rotor_fecs = 1;
    4166           0 :           publish_replay_out( ctx, stem, REPLAY_SIG_MISSING_FEC, 0 );
    4167           0 :           return 0;
    4168           0 :         }
    4169           0 :         case PROCESS_FEC_SKIP: { return 0; }
    4170           0 :         case PROCESS_FEC_WAIT: {
    4171             :           /* queue an eviction, then retry the frag. */
    4172           0 :           if( FD_UNLIKELY( evict_banks ) ) {
    4173           0 :             ulong evictable_bank_idx = fd_banks_get_evictable_bank( ctx->banks, ctx->notified_root_bank );
    4174           0 :             if( FD_UNLIKELY( evictable_bank_idx==ULONG_MAX ) ) {
    4175           0 :               FD_LOG_DEBUG(( "replay has no banks to mark as prunable, it's possible that there is one bank already marked as prunable" ));
    4176           0 :               return 1;
    4177           0 :             }
    4178             : 
    4179           0 :             FD_LOG_WARNING(( "banks full, evicting bank (idx=%lu)", evictable_bank_idx ));
    4180             : 
    4181           0 :             timing_slot_release( ctx, evictable_bank_idx );
    4182             : 
    4183           0 :             if( FD_UNLIKELY( fd_sched_block_is_discarded( ctx->sched, evictable_bank_idx ) ) ) {
    4184           0 :               fd_block_id_ele_t * ele = &ctx->block_id_arr[ evictable_bank_idx ];
    4185           0 :               report_block_incomplete( ctx, ele->slot, ele->block_id_seen ? &ele->dmr : &ele->latest_mr, fd_banks_bank_query( ctx->banks, evictable_bank_idx ),
    4186           0 :                                        FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED );
    4187           0 :             }
    4188             : 
    4189             :             /* Send a notification to other tiles to drop a reference to the
    4190             :                evictable bank.  The RPC tile is the only tile which holds onto
    4191             :                non-rooted banks, non-transiently. */
    4192           0 :             fd_replay_drop_bank_ref_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
    4193           0 :             fd_sched_block_abandon( ctx->sched, evictable_bank_idx, FD_SCHED_ABANDON_DISCARDED );
    4194           0 :             msg->bank_idx = evictable_bank_idx;
    4195           0 :             publish_replay_out( ctx, stem, REPLAY_SIG_DROP_BANK_REF, sizeof(fd_replay_drop_bank_ref_t) );
    4196           0 :           }
    4197           0 :           return 1;
    4198           0 :         }
    4199           0 :         default:
    4200           0 :           FD_LOG_CRIT(( "unhandled process_rotor_fec result: %d", res ));
    4201           0 :       }
    4202           0 :       break;
    4203           0 :     }
    4204           0 :     case IN_KIND_TXSEND: {
    4205           0 :       process_vote_txn_sent( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
    4206           0 :       break;
    4207           0 :     }
    4208           0 :     case IN_KIND_GOSSIP_OUT: {
    4209           0 :       FD_TEST( sig==FD_GOSSIP_UPDATE_TAG_WFS_DONE );
    4210           0 :       if( FD_UNLIKELY( ctx->wfs_complete ) ) break;
    4211           0 :       ctx->wfs_complete = 1;
    4212           0 :       maybe_verify_shred_version( ctx );
    4213             : 
    4214             :       /* Recalculate next_leader_tickcount relative to now.  The
    4215             :          original value was computed at boot time (in boot_genesis or
    4216             :          on_snapshot_message). */
    4217           0 :       ctx->next_leader_tickcount = LONG_MAX;
    4218           0 :       if( FD_LIKELY( ctx->next_leader_slot!=ULONG_MAX ) ) {
    4219           0 :         fd_block_id_ele_t * block_id_ele = fd_block_id_ele_query( ctx, &ctx->reset_dmr, ctx->reset_slot );
    4220           0 :         if( FD_LIKELY( block_id_ele ) ) {
    4221           0 :           fd_bank_t * reset_bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele ) );
    4222           0 :           if( FD_LIKELY( reset_bank && reset_bank->bank_seq==block_id_ele->bank_seq && reset_bank->state!=FD_BANK_STATE_PRUNABLE ) ) {
    4223           0 :             double slot_duration_ticks = (double)reset_bank->f.slot_params.ns_per_slot_adjusted*ctx->tick_per_ns;
    4224           0 :             ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*slot_duration_ticks) + fd_tickcount();
    4225           0 :           }
    4226           0 :         }
    4227           0 :       }
    4228             : 
    4229           0 :       FD_LOG_NOTICE(( "Done waiting for supermajority. More than 80 percent of cluster stake has joined." ));
    4230             : 
    4231           0 :       if( FD_LIKELY( ctx->replay_out->idx!=ULONG_MAX ) ) {
    4232           0 :         publish_replay_out( ctx, stem, REPLAY_SIG_WFS_DONE, 0UL );
    4233           0 :       }
    4234           0 :       break;
    4235           0 :     }
    4236           0 :     case IN_KIND_RPC: {
    4237           0 :       fd_bank_t * bank = fd_banks_bank_query( ctx->banks, sig );
    4238           0 :       FD_TEST( bank );
    4239           0 :       bank->refcnt--;
    4240           0 :       FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt decremented to %lu for %s", bank->idx, bank->f.slot, bank->refcnt, ctx->in_kind[ in_idx ]==IN_KIND_RPC ? "rpc" : "gui" ));
    4241           0 :       break;
    4242           0 :     }
    4243           0 :     case IN_KIND_SNAPMK:
    4244           0 :       msg_snapmk( ctx, stem, sig, in_idx, chunk );
    4245           0 :       break;
    4246           0 :     case IN_KIND_ADMIN:
    4247           0 :       msg_admin( ctx, stem, fd_frag_meta_ctl_orig( ctl ), sig );
    4248           0 :       break;
    4249           0 :     default:
    4250           0 :       FD_LOG_ERR(( "unhandled kind %d", ctx->in_kind[ in_idx ] ));
    4251           0 :   }
    4252             : 
    4253           0 :   return 0;
    4254           0 : }
    4255             : 
    4256             : #undef PROCESS_FEC_DROP
    4257             : #undef PROCESS_FEC_WAIT
    4258             : #undef PROCESS_FEC_SKIP
    4259             : #undef PROCESS_FEC_OK
    4260             : 
    4261             : static inline fd_replay_out_link_t
    4262             : out1( fd_topo_t const *      topo,
    4263             :       fd_topo_tile_t const * tile,
    4264           0 :       char const *           name ) {
    4265           0 :   ulong idx = ULONG_MAX;
    4266             : 
    4267           0 :   for( ulong i=0UL; i<tile->out_cnt; i++ ) {
    4268           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
    4269           0 :     if( !strcmp( link->name, name ) ) {
    4270           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 ));
    4271           0 :       idx = i;
    4272           0 :     }
    4273           0 :   }
    4274             : 
    4275           0 :   if( FD_UNLIKELY( idx==ULONG_MAX ) ) return (fd_replay_out_link_t){ .idx = ULONG_MAX, .mem = NULL, .chunk0 = 0, .wmark = 0, .chunk = 0 };
    4276             : 
    4277           0 :   void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
    4278           0 :   ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
    4279           0 :   ulong wmark  = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
    4280             : 
    4281           0 :   return (fd_replay_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
    4282           0 : }
    4283             : 
    4284             : static void
    4285             : privileged_init( fd_topo_t const *      topo,
    4286           0 :                  fd_topo_tile_t const * tile ) {
    4287           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    4288             : 
    4289           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    4290           0 :   fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
    4291             : 
    4292           0 :   if( FD_UNLIKELY( !strcmp( tile->replay.identity_key_path, "" ) ) ) FD_LOG_ERR(( "identity_key_path not set" ));
    4293             : 
    4294           0 :   ctx->identity_pubkey[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->replay.identity_key_path, /* pubkey only: */ 1 ) );
    4295           0 :   ctx->identity_idx         = 0UL;
    4296           0 :   ctx->identity_dirty       = 0;
    4297             : 
    4298           0 :   ctx->vote_account_staked       = 0;
    4299           0 :   ctx->vote_account_inadmissible = 0;
    4300             : 
    4301           0 :   ctx->metrics.voted_slot = ULONG_MAX;
    4302             : 
    4303           0 :   ctx->has_vote_account = !!tile->replay.vote_account_path[ 0 ];
    4304           0 :   if( FD_LIKELY( ctx->has_vote_account ) ) {
    4305           0 :     if( FD_UNLIKELY( !fd_base58_decode_32( tile->replay.vote_account_path, ctx->vote_account->uc ) ) ) {
    4306           0 :       uchar const * vote_key = fd_keyload_load( tile->replay.vote_account_path, /* pubkey only: */ 1 );
    4307           0 :       fd_memcpy( ctx->vote_account->uc, vote_key, sizeof(fd_pubkey_t) );
    4308           0 :     }
    4309           0 :   }
    4310             : 
    4311           0 :   ctx->bundle.enabled = tile->replay.bundle.enabled;
    4312           0 :   if( FD_UNLIKELY( !tile->replay.bundle.vote_account_path[0] ) ) {
    4313           0 :     ctx->bundle.enabled = 0;
    4314           0 :   }
    4315             : 
    4316           0 :   if( FD_UNLIKELY( ctx->bundle.enabled ) ) {
    4317           0 :     if( FD_UNLIKELY( !fd_base58_decode_32( tile->replay.bundle.vote_account_path, ctx->bundle.vote_account.uc ) ) ) {
    4318           0 :       const uchar * vote_key = fd_keyload_load( tile->replay.bundle.vote_account_path, /* pubkey only: */ 1 );
    4319           0 :       fd_memcpy( ctx->bundle.vote_account.uc, vote_key, 32UL );
    4320           0 :     }
    4321           0 :   }
    4322             : 
    4323           0 :   FD_TEST( fd_rng_secure( &ctx->rng_seed,             sizeof(ctx->rng_seed) ) );
    4324           0 :   FD_TEST( fd_rng_secure( &ctx->blockhash_seed,       sizeof(ulong) )         );
    4325           0 :   FD_TEST( fd_rng_secure( &ctx->vote_tracker_seed,    sizeof(ulong) )         );
    4326           0 :   FD_TEST( fd_rng_secure( &ctx->ag_block_id_map_seed, sizeof(ulong) )         );
    4327           0 :   FD_TEST( fd_rng_secure( &ctx->initial_block_id,     sizeof(fd_hash_t) )     );
    4328           0 :   FD_TEST( fd_rng_secure( &ctx->runtime_stack_seed,   sizeof(ulong) )         );
    4329             : 
    4330           0 :   ctx->store_disk_fd = -1;
    4331           0 :   ulong store_obj_id = fd_pod_query_ulong( topo->props, "store", ULONG_MAX );
    4332           0 :   if( FD_LIKELY( store_obj_id!=ULONG_MAX ) ) {
    4333           0 :     fd_store_t * store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
    4334           0 :     FD_TEST( store && store->magic==FD_STORE_MAGIC );
    4335           0 :     ctx->store_disk_fd = FD_STORE_FD_RO;
    4336           0 :     if( FD_UNLIKELY( fcntl( ctx->store_disk_fd, F_GETFD )<0 ) )
    4337           0 :       FD_LOG_ERR(( "store file descriptor was not inherited (%i-%s)", errno, fd_io_strerror( errno ) ));
    4338           0 :   }
    4339           0 : }
    4340             : 
    4341             : static void
    4342             : unprivileged_init( fd_topo_t const *      topo,
    4343           0 :                    fd_topo_tile_t const * tile ) {
    4344           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    4345           0 :   void * block_id_map_mem = NULL;
    4346           0 :   ulong chain_cnt  = fd_ag_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
    4347             : 
    4348           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    4349           0 :   fd_replay_tile_t * ctx    = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t),   sizeof(fd_replay_tile_t) );
    4350           0 :   void * genesis_mem        = FD_SCRATCH_ALLOC_APPEND( l, fd_genesis_align(),          fd_genesis_footprint( fd_genesis_account_max( tile->replay.genesis_max_message_size ) ) );
    4351           0 :   void * runtime_stack_mem  = FD_SCRATCH_ALLOC_APPEND( l, fd_runtime_stack_align(),    fd_runtime_stack_footprint( FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKED_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKE_ACCOUNTS ) );
    4352           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 );
    4353           0 :   void * dead_idxs_mem      = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong),              sizeof(ulong) * tile->replay.max_live_slots );
    4354           0 :   void * fec_chain_mem      = tile->replay.report_runtime_diffs ?
    4355           0 :                               FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_hash_t),           sizeof(fd_hash_t) * FD_FEC_BLK_MAX * tile->replay.max_live_slots ) : NULL;
    4356           0 :   void * slot_diffs_mem     = tile->replay.report_runtime_diffs ?
    4357           0 :                               FD_SCRATCH_ALLOC_APPEND( l, 8UL,                          FD_EVENT_RUNTIME_SLOT_DIFFS_FOOTPRINT * tile->replay.max_live_slots ) : NULL;
    4358           0 :     block_id_map_mem        = FD_SCRATCH_ALLOC_APPEND( l, fd_ag_block_id_map_align(),  fd_ag_block_id_map_footprint( chain_cnt ) );
    4359           0 :   void * _txncache          = FD_SCRATCH_ALLOC_APPEND( l, fd_txncache_align(),         fd_txncache_footprint( tile->replay.max_live_slots ) );
    4360           0 :   void * _accdb             = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(),            fd_accdb_footprint( tile->replay.max_live_slots, 0 ) );
    4361           0 :   void * recp_stats_mem     = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_reception_stats_t), sizeof(fd_reception_stats_t)*tile->replay.max_live_slots );
    4362           0 :   void * sched_mem          = FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(),            fd_sched_footprint( tile->replay.sched_depth, tile->replay.max_live_slots, tile->replay.max_shreds_per_block, tile->replay.max_txn_per_slot ) );
    4363           0 :   void * vote_tracker_mem   = FD_SCRATCH_ALLOC_APPEND( l, fd_vote_tracker_align(),     fd_vote_tracker_footprint() );
    4364           0 :   void * _capture_ctx       = FD_SCRATCH_ALLOC_APPEND( l, fd_capture_ctx_align(),      fd_capture_ctx_footprint() );
    4365           0 :   void * dump_proto_ctx_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
    4366           0 :   void * block_completed_ev = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_event_block_completed_t), sizeof(fd_event_block_completed_t) );
    4367           0 :   void * timing_pool_mem    = FD_SCRATCH_ALLOC_APPEND( l, fd_timing_slot_pool_align(),  fd_timing_slot_pool_footprint( FD_REPLAY_TXN_TIMING_SLOTS ) );
    4368           0 :   void * timing_rec_mem     = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_txn_timing_t), FD_REPLAY_TXN_TIMING_SLOTS*tile->replay.max_txn_per_slot*sizeof(fd_replay_txn_timing_t) );
    4369           0 :   void * timing_of_bank_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong),               tile->replay.max_live_slots*sizeof(ulong) );
    4370           0 :   void * block_dump_ctx     = NULL;
    4371           0 :   if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
    4372           0 :     block_dump_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
    4373           0 :   }
    4374             : 
    4375           0 :   ctx->genesis = fd_genesis_new( genesis_mem, fd_genesis_account_max( tile->replay.genesis_max_message_size ) );
    4376           0 :   FD_TEST( ctx->genesis );
    4377             : 
    4378           0 :   ctx->runtime_stack = fd_runtime_stack_join( fd_runtime_stack_new( runtime_stack_mem, FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKED_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKE_ACCOUNTS, ctx->runtime_stack_seed ) );
    4379           0 :   FD_TEST( ctx->runtime_stack );
    4380             : 
    4381           0 :   ctx->wksp = topo->workspaces[ topo->objs[ tile->tile_obj_id ].wksp_id ].wksp;
    4382             : 
    4383           0 :   ulong store_obj_id = fd_pod_query_ulong( topo->props, "store", ULONG_MAX );
    4384           0 :   FD_TEST( store_obj_id!=ULONG_MAX );
    4385           0 :   ctx->store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
    4386           0 :   FD_TEST( ctx->store );
    4387           0 :   FD_TEST( fd_store_map_ljoin( ctx->store, ctx->map_join ) );
    4388             : 
    4389           0 :   ulong banks_obj_id = fd_pod_query_ulong( topo->props, "banks", ULONG_MAX );
    4390           0 :   FD_TEST( banks_obj_id!=ULONG_MAX );
    4391             : 
    4392           0 :   ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
    4393           0 :   FD_TEST( ctx->banks );
    4394             : 
    4395           0 :   ulong footer_cache_obj_id = fd_pod_query_ulong( topo->props, "footer_cache", ULONG_MAX );
    4396           0 :   ctx->footer_cache = NULL;
    4397           0 :   if( footer_cache_obj_id!=ULONG_MAX ) {
    4398           0 :     ctx->footer_cache = fd_footer_cache_join( fd_topo_obj_laddr( topo, footer_cache_obj_id ) );
    4399           0 :     FD_TEST( ctx->footer_cache );
    4400           0 :   }
    4401             : 
    4402           0 :   ctx->leader_txn_timing = NULL;
    4403           0 :   ulong ldr_tt_obj_id = fd_pod_query_ulong( topo->props, "ldr_tt", ULONG_MAX );
    4404           0 :   if( FD_LIKELY( ldr_tt_obj_id!=ULONG_MAX ) ) ctx->leader_txn_timing = fd_topo_obj_laddr( topo, ldr_tt_obj_id );
    4405             : 
    4406           0 :   ulong node_info_obj_id = fd_pod_query_ulong( topo->props, "node_info", ULONG_MAX );
    4407           0 :   FD_TEST( node_info_obj_id!=ULONG_MAX );
    4408           0 :   ctx->node_info = fd_node_info_box_join( fd_topo_obj_laddr( topo, node_info_obj_id ) );
    4409           0 :   FD_TEST( ctx->node_info );
    4410           0 :   fd_node_info_write_begin( ctx->node_info );
    4411           0 :   ctx->node_info->info.identity = *ctx->identity_pubkey;
    4412           0 :   if( FD_LIKELY( ctx->has_vote_account ) ) ctx->node_info->info.vote_account = *ctx->vote_account;
    4413           0 :   fd_node_info_write_end( ctx->node_info );
    4414             : 
    4415           0 :   ulong wait_info_obj_id = fd_pod_query_ulong( topo->props, "wait_info", ULONG_MAX );
    4416           0 :   if( FD_LIKELY( wait_info_obj_id!=ULONG_MAX ) ) {
    4417           0 :     ctx->wait_info = fd_wait_info_box_join( fd_topo_obj_laddr( topo, wait_info_obj_id ) );
    4418           0 :     FD_TEST( ctx->wait_info );
    4419           0 :   } else {
    4420           0 :     ctx->wait_info = NULL;
    4421           0 :   }
    4422             : 
    4423           0 :   FD_MGAUGE_SET( REPLAY, BANK_CAPACITY, fd_banks_pool_max_cnt( ctx->banks ) );
    4424             : 
    4425           0 :   ctx->delinquent_known              = 0;
    4426           0 :   ctx->delinquent_sample_slot        = 0UL;
    4427           0 :   ctx->delinquent_stake_lamports     = 0UL;
    4428           0 :   ctx->cluster_active_stake_lamports = 0UL;
    4429           0 :   ctx->ag_last_voted_epoch = ULONG_MAX;
    4430           0 :   ctx->ag_last_settled     = 0UL;
    4431           0 :   ctx->ag_stamp_since      = ULONG_MAX;
    4432           0 :   ctx->consensus_root_slot = ULONG_MAX;
    4433           0 :   ctx->consensus_root      = ctx->initial_block_id;
    4434           0 :   ctx->finalized_block_id_lo = ag_block_id( ULONG_MAX, ctx->initial_block_id.uc );
    4435           0 :   ctx->finalized_block_id_hi = ctx->finalized_block_id_lo;
    4436           0 :   ctx->notified_root_slot  = ULONG_MAX;
    4437           0 :   ctx->notified_root       = ctx->initial_block_id;
    4438           0 :   ctx->notified_root_bank = NULL;
    4439           0 :   ctx->published_root_slot = ULONG_MAX;
    4440             : 
    4441           0 :   ctx->expected_shred_version = tile->replay.expected_shred_version;
    4442           0 :   ctx->ipecho_shred_version = 0;
    4443           0 :   ctx->shred_version        = 0;
    4444           0 :   fd_memcpy( ctx->genesis_path, tile->replay.genesis_path, sizeof(ctx->genesis_path) );
    4445           0 :   ctx->has_genesis_hash = 0;
    4446           0 :   ctx->has_cluster_type = 0;
    4447           0 :   ctx->has_genesis_timestamp          = 0;
    4448           0 :   ctx->has_expected_genesis_timestamp = 0;
    4449           0 :   ctx->cluster_type = FD_CLUSTER_UNKNOWN;
    4450           0 :   ctx->hard_fork_cnt = ULONG_MAX;
    4451           0 :   ctx->has_manifest_block_id = 0;
    4452             : 
    4453           0 :   if( FD_UNLIKELY( ctx->bundle.enabled ) ) {
    4454           0 :     if( FD_UNLIKELY( !fd_bundle_crank_gen_init( ctx->bundle.gen,
    4455           0 :              (fd_acct_addr_t const *)tile->replay.bundle.tip_distribution_program_addr,
    4456           0 :              (fd_acct_addr_t const *)tile->replay.bundle.tip_payment_program_addr,
    4457           0 :              (fd_acct_addr_t const *)ctx->bundle.vote_account.uc,
    4458           0 :              (fd_acct_addr_t const *)ctx->bundle.vote_account.uc, "NAN", 0UL ) ) ) {
    4459           0 :       FD_LOG_ERR(( "failed to initialize bundle crank gen" ));
    4460           0 :     }
    4461           0 :   }
    4462             : 
    4463           0 :   FD_TEST( tile->replay.enable_features_cnt<=sizeof(ctx->enable_features)/sizeof(ctx->enable_features[0]) );
    4464           0 :   ctx->enable_features_cnt = tile->replay.enable_features_cnt;
    4465           0 :   for( ulong i=0UL; i<tile->replay.enable_features_cnt; i++ ) {
    4466           0 :     fd_memcpy( ctx->enable_features[ i ], tile->replay.enable_features[ i ], FD_BASE58_ENCODED_32_SZ );
    4467           0 :   }
    4468             : 
    4469           0 :   ulong progcache_obj_id; FD_TEST( (progcache_obj_id = fd_pod_query_ulong( topo->props, "progcache", ULONG_MAX ) )!=ULONG_MAX );
    4470           0 :   FD_TEST( fd_progcache_shmem_join( ctx->progcache, fd_topo_obj_laddr( topo, progcache_obj_id       ) ) );
    4471             : 
    4472           0 :   void * _txncache_shmem = fd_topo_obj_laddr( topo, tile->replay.txncache_obj_id );
    4473           0 :   fd_txncache_shmem_t * txncache_shmem = fd_txncache_shmem_join( _txncache_shmem );
    4474           0 :   FD_TEST( txncache_shmem );
    4475           0 :   ctx->txncache = fd_txncache_join( fd_txncache_new( _txncache, txncache_shmem ) );
    4476           0 :   FD_TEST( ctx->txncache );
    4477             : 
    4478           0 :   void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->replay.accdb_obj_id );
    4479           0 :   fd_accdb_shmem_t * accdb_shmem = fd_accdb_shmem_join( _accdb_shmem );
    4480           0 :   FD_TEST( accdb_shmem );
    4481           0 :   fd_sleep_t * accdb_sleep = topo->sleep_obj_id!=ULONG_MAX ? fd_sleep_join( fd_topo_obj_laddr( topo, topo->sleep_obj_id ) ) : NULL;
    4482           0 :   ctx->accdb = fd_accdb_join( fd_accdb_new( _accdb, accdb_shmem, FD_ACCDB_FD_RW, 0UL, NULL, accdb_sleep, fd_topo_find_tile( topo, "accdb", 0UL ), 0 ) );
    4483           0 :   FD_TEST( ctx->accdb );
    4484             : 
    4485           0 :   ctx->capture_ctx = NULL;
    4486           0 :   if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) ) ) {
    4487           0 :     ctx->capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( _capture_ctx ) );
    4488           0 :     ctx->capture_ctx->solcap_start_slot = tile->replay.capture_start_slot;
    4489           0 :     ctx->capture_ctx->capture_solcap = 1;
    4490           0 :   }
    4491             : 
    4492           0 :   ctx->block_completed_event = block_completed_ev;
    4493             : 
    4494           0 :   ctx->timing_slot_pool = fd_timing_slot_pool_join( fd_timing_slot_pool_new( timing_pool_mem, FD_REPLAY_TXN_TIMING_SLOTS ) );
    4495           0 :   FD_TEST( ctx->timing_slot_pool );
    4496           0 :   ctx->timing_rec          = timing_rec_mem;
    4497           0 :   ctx->timing_slot_of_bank = timing_of_bank_mem;
    4498           0 :   for( ulong i=0UL; i<tile->replay.max_live_slots; i++ ) ctx->timing_slot_of_bank[ i ] = fd_timing_slot_pool_idx_null( ctx->timing_slot_pool );
    4499             : 
    4500           0 :   ctx->dump_proto_ctx = NULL;
    4501           0 :   if( FD_UNLIKELY( strcmp( "", tile->replay.dump_proto_dir ) ) ) {
    4502           0 :     ctx->dump_proto_ctx                        = dump_proto_ctx_mem;
    4503           0 :     ctx->dump_proto_ctx->dump_proto_output_dir = tile->replay.dump_proto_dir;
    4504           0 :     if( FD_LIKELY( tile->replay.dump_block_to_pb ) ) {
    4505           0 :       ctx->dump_proto_ctx->dump_block_to_pb = !!tile->replay.dump_block_to_pb;
    4506           0 :     }
    4507           0 :   }
    4508             : 
    4509           0 :   if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
    4510           0 :     ctx->block_dump_ctx = fd_block_dump_context_join( fd_block_dump_context_new( block_dump_ctx ) );
    4511           0 :   } else {
    4512           0 :     ctx->block_dump_ctx = NULL;
    4513           0 :   }
    4514             : 
    4515           0 :   ctx->is_booted = 0;
    4516             : 
    4517           0 :   ctx->tick_per_ns = fd_tempo_tick_per_ns( NULL );
    4518             : 
    4519           0 :   fd_clock_tile_init( ctx->clock );
    4520             : 
    4521           0 :   ctx->max_txn_per_slot     = tile->replay.max_txn_per_slot;
    4522           0 :   ctx->max_shreds_per_block = tile->replay.max_shreds_per_block;
    4523             : 
    4524           0 :   FD_TEST( fd_rng_new( ctx->rng, ctx->rng_seed, 0UL ) );
    4525             : 
    4526           0 :   ctx->reception_stats     = recp_stats_mem;
    4527           0 :   ctx->reception_stats_cnt = tile->replay.max_live_slots;
    4528           0 :   FD_TEST( ctx->reception_stats_cnt );
    4529           0 :   memset( ctx->reception_stats, 0, sizeof(fd_reception_stats_t)*ctx->reception_stats_cnt );
    4530             : 
    4531           0 :   ctx->leader_stats.slot = ULONG_MAX;
    4532           0 :   ctx->sched = fd_sched_join( fd_sched_new( sched_mem, ctx->rng, tile->replay.sched_depth, tile->replay.max_live_slots, ctx->max_shreds_per_block, ctx->max_txn_per_slot, fd_topo_tile_name_cnt( topo, "execrp" ), 1 ) );
    4533           0 :   FD_TEST( ctx->sched );
    4534             : 
    4535           0 :   ctx->in_cnt          = tile->in_cnt;
    4536           0 :   ctx->execrp_idle_cnt = 0UL;
    4537             : 
    4538           0 :   ctx->vote_tracker = fd_vote_tracker_join( fd_vote_tracker_new( vote_tracker_mem, ctx->vote_tracker_seed ) );
    4539           0 :   FD_TEST( ctx->vote_tracker );
    4540             : 
    4541           0 :   ctx->identity_vote_rooted = 0;
    4542             : 
    4543           0 :   ctx->wait_for_vote_to_start_leader = tile->replay.wait_for_vote_to_start_leader;
    4544             : 
    4545           0 :   ctx->wfs_hash_is_zero = !memcmp( tile->replay.wait_for_supermajority_with_bank_hash.uc, ((fd_hash_t){0}).uc, sizeof(fd_hash_t) );
    4546           0 :   ctx->wfs_enabled = fd_wfs_configured( tile->replay.wait_for_supermajority_at_slot,
    4547           0 :                                         ctx->wfs_hash_is_zero,
    4548           0 :                                         (ulong)tile->replay.expected_shred_version );
    4549           0 :   ctx->wait_for_supermajority_at_slot = tile->replay.wait_for_supermajority_at_slot;
    4550           0 :   ctx->expected_bank_hash = tile->replay.wait_for_supermajority_with_bank_hash;
    4551           0 :   ctx->wfs_complete = !ctx->wfs_enabled;
    4552             : 
    4553             :   /* With WFS, auto is resolved at boot instead (on_snapshot_message). */
    4554           0 :   if( FD_UNLIKELY( !ctx->wfs_enabled ) ) {
    4555           0 :     ctx->wait_for_vote_to_start_leader = wfs_resolve_leader_gate_auto( ctx->wait_for_vote_to_start_leader );
    4556           0 :   }
    4557             : 
    4558           0 :   ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
    4559           0 :   FD_TEST( ctx->mleaders );
    4560           0 :   ctx->next_leader_query_start = ULONG_MAX;
    4561             : 
    4562           0 :   ctx->is_leader             = 0;
    4563           0 :   ctx->drain_rotor_fecs      = 0;
    4564           0 :   ctx->supports_leader       = fd_topo_find_tile( topo, "pack", 0UL )!=ULONG_MAX;
    4565           0 :   ctx->snapmk.active                        = 0;
    4566           0 :   ctx->snapmk.supported                     = fd_topo_find_tile( topo, "snapmk", 0UL )!=ULONG_MAX;
    4567           0 :   ctx->snapmk.scheduled_at_slot             = ULONG_MAX;
    4568           0 :   ctx->snapmk.full_interval_blocks          = tile->replay.full_snapshot_interval_blocks;
    4569           0 :   ctx->snapmk.next_full_block_height        = ULONG_MAX;
    4570           0 :   ctx->snapmk.incremental_interval_blocks   = tile->replay.incremental_snapshot_interval_blocks;
    4571           0 :   ctx->snapmk.next_incremental_block_height = ULONG_MAX;
    4572           0 :   ctx->snapmk.base_slot                     = ULONG_MAX;
    4573           0 :   ctx->snapmk.snap_finished_full            = 0UL;
    4574           0 :   ctx->snapmk.snap_finished_incr            = 0UL;
    4575           0 :   ctx->snapmk.snap_produced_incr_cnt        = 0UL;
    4576           0 :   if( FD_UNLIKELY( !ctx->snapmk.supported ) ) {
    4577           0 :     ctx->snapmk.full_interval_blocks        = 0UL;
    4578           0 :     ctx->snapmk.incremental_interval_blocks = 0UL;
    4579           0 :   }
    4580           0 :   ctx->reset_slot            = 0UL;
    4581           0 :   ctx->reset_dmr             = ctx->initial_block_id;
    4582           0 :   ctx->epoch_end_slot        = 0UL;
    4583           0 :   ctx->slots_per_epoch       = 0UL;
    4584           0 :   ctx->ns_per_slot           = 0UL;
    4585           0 :   ctx->reset_timestamp_nanos = 0UL;
    4586           0 :   ctx->next_leader_slot      = ULONG_MAX;
    4587           0 :   ctx->next_leader_tickcount = LONG_MAX;
    4588           0 :   ctx->highwater_leader_slot = ULONG_MAX;
    4589             : 
    4590           0 :   ctx->votor_final->slot       = ULONG_MAX;
    4591           0 :   ctx->votor_parent_ready_slot = 0UL;
    4592           0 :   for( ulong i=0UL; i<FD_NUM_SLOTS_FOR_REWARD+AG_SLOTS_PER_WINDOW+1UL; i++ ) ctx->votor_reward[ i ].slot = ULONG_MAX;
    4593             : 
    4594           0 :   ctx->caught_up                = 0;
    4595           0 :   ctx->catch_up_max_fec_slot    = ULONG_MAX;
    4596           0 :   ctx->catch_up_tip_advance_cnt = 0UL;
    4597           0 :   ctx->boot_timestamp_nanos     = tile->replay.boot_timestamp_nanos;
    4598           0 :   ctx->leader_bank = NULL;
    4599             : 
    4600           0 :   ctx->block_id_len   = tile->replay.max_live_slots;
    4601           0 :   ctx->max_live_slots = tile->replay.max_live_slots;
    4602           0 :   ctx->block_id_arr = (fd_block_id_ele_t *)block_id_arr_mem;
    4603           0 :   ctx->dead_idxs    = (ulong *)dead_idxs_mem;
    4604             : 
    4605           0 :   ctx->fec_chain    = (fd_hash_t *)fec_chain_mem;
    4606           0 :   if( FD_UNLIKELY( slot_diffs_mem ) ) fd_event_runtime_slot_diffs_init( slot_diffs_mem, tile->replay.max_live_slots );
    4607             : 
    4608           0 :   ctx->ag_block_id_map = fd_ag_block_id_map_join( fd_ag_block_id_map_new( block_id_map_mem, chain_cnt, ctx->ag_block_id_map_seed ) );
    4609           0 :   FD_TEST( ctx->ag_block_id_map );
    4610             : 
    4611           0 :   for( ulong i=0UL; i<tile->replay.max_live_slots; i++ ) {
    4612           0 :     ctx->block_id_arr[ i ].block_id_seen = 0;
    4613           0 :     ctx->block_id_arr[ i ].fec_cnt       = 0U;
    4614           0 :     memset( &ctx->block_id_arr[ i ].block_info, 0, sizeof(ag_block_id_t) );
    4615           0 :   }
    4616             : 
    4617           0 :   ctx->resolv_tile_cnt = fd_topo_tile_name_cnt( topo, "resolv" );
    4618             : 
    4619           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
    4620           0 :   FD_TEST( ctx->keyswitch );
    4621           0 :   ctx->halt_replay = 0;
    4622             : 
    4623           0 :   FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
    4624           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
    4625           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
    4626           0 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
    4627             : 
    4628           0 :     if( FD_LIKELY( link->dcache ) ) {
    4629           0 :       ctx->in[ i ].mem    = link_wksp->wksp;
    4630           0 :       ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
    4631           0 :       ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
    4632           0 :       ctx->in[ i ].mtu    = link->mtu;
    4633           0 :     }
    4634             : 
    4635           0 :     if(      !strcmp( link->name, "genesi_out"    ) ) ctx->in_kind[ i ] = IN_KIND_GENESIS;
    4636           0 :     else if( !strcmp( link->name, "ipecho_out"    ) ) ctx->in_kind[ i ] = IN_KIND_IPECHO;
    4637           0 :     else if( !strcmp( link->name, "snapin_manif"  ) ) ctx->in_kind[ i ] = IN_KIND_SNAP;
    4638           0 :     else if( !strcmp( link->name, "execrp_replay" ) ) ctx->in_kind[ i ] = IN_KIND_EXECRP;
    4639           0 :     else if( !strcmp( link->name, "poh_replay"    ) ) ctx->in_kind[ i ] = IN_KIND_POH;
    4640           0 :     else if( !strcmp( link->name, "resolv_replay" ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
    4641           0 :     else if( !strcmp( link->name, "shred_out"     ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR;
    4642           0 :     else if( !strcmp( link->name, "repair_out"    ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR;
    4643           0 :     else if( !strcmp( link->name, "txsend_out"    ) ) ctx->in_kind[ i ] = IN_KIND_TXSEND;
    4644           0 :     else if( !strcmp( link->name, "rpc_replay"    ) ) ctx->in_kind[ i ] = IN_KIND_RPC;
    4645           0 :     else if( !strcmp( link->name, "gossip_misc"   ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_OUT;
    4646           0 :     else if( !strcmp( link->name, "snapmk_out"    ) ) ctx->in_kind[ i ] = IN_KIND_SNAPMK;
    4647           0 :     else if( !strcmp( link->name, "admin_replay"  ) ) ctx->in_kind[ i ] = IN_KIND_ADMIN;
    4648           0 :     else if( !strcmp( link->name, "votor_out"     ) ) ctx->in_kind[ i ] = IN_KIND_VOTOR;
    4649           0 :     else FD_LOG_ERR(( "unexpected input link name %s", link->name ));
    4650             : 
    4651           0 :     if( ctx->in_kind[ i ]==IN_KIND_ADMIN ) {
    4652           0 :       FD_TEST( ( ctx->admin_out_idx = fd_topo_find_tile_out_link( topo, tile, "replay_admin", 0UL ) )!=ULONG_MAX );
    4653           0 :     }
    4654           0 :   }
    4655             : 
    4656           0 :   *ctx->epoch_out  = out1( topo, tile, "replay_epoch" ); FD_TEST( ctx->epoch_out->idx!=ULONG_MAX );
    4657           0 :   *ctx->replay_out = out1( topo, tile, "replay_out"   ); FD_TEST( ctx->replay_out->idx!=ULONG_MAX );
    4658           0 :   *ctx->slot_out   = out1( topo, tile, "replay_slot"  ); FD_TEST( ctx->slot_out->idx!=ULONG_MAX );
    4659           0 :   *ctx->rotor_out  = out1( topo, tile, "replay_rotor" );
    4660           0 :   *ctx->snapmk_out = out1( topo, tile, "replay_snapmk" ); FD_TEST( ctx->snapmk.supported == (ctx->snapmk_out->idx!=ULONG_MAX) );
    4661             : 
    4662           0 :   ctx->exec_cnt = 0UL;
    4663           0 :   for( ulong i=0UL; i<FD_SCHED_MAX_EXEC_TILE_CNT; i++ ) ctx->exec_out[ i ].idx = ULONG_MAX;
    4664           0 :   for( ulong i=0UL; i<tile->out_cnt; i++ ) {
    4665           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
    4666           0 :     if( strcmp( link->name, "replay_execrp" ) ) continue;
    4667           0 :     FD_TEST( link->kind_id<FD_SCHED_MAX_EXEC_TILE_CNT && ctx->exec_out[ link->kind_id ].idx==ULONG_MAX );
    4668           0 :     void * mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
    4669           0 :     ulong chunk0 = fd_dcache_compact_chunk0( mem, link->dcache );
    4670           0 :     ctx->exec_out[ link->kind_id ] = (fd_replay_out_link_t){
    4671           0 :       .idx    = i,
    4672           0 :       .mem    = mem,
    4673           0 :       .chunk0 = chunk0,
    4674           0 :       .wmark  = fd_dcache_compact_wmark( mem, link->dcache, link->mtu ),
    4675           0 :       .chunk  = chunk0,
    4676           0 :     };
    4677           0 :     ctx->exec_cnt++;
    4678           0 :   }
    4679           0 :   FD_TEST( ctx->exec_cnt && ctx->exec_cnt==fd_topo_tile_name_cnt( topo, "execrp" ) );
    4680           0 :   for( ulong i=0UL; i<ctx->exec_cnt; i++ ) FD_TEST( ctx->exec_out[ i ].idx!=ULONG_MAX );
    4681             : 
    4682           0 :   ctx->slot_out_seq   = fd_mcache_seq_laddr_const( topo->links[ tile->out_link_id[ ctx->slot_out->idx   ] ].mcache );
    4683             : 
    4684           0 :   ctx->rpc_enabled = fd_topo_find_tile( topo, "rpc", 0UL )!=ULONG_MAX;
    4685             : 
    4686           0 :   if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) ) ) {
    4687           0 :     ulong idx = fd_topo_find_tile_out_link( topo, tile, "cap_repl", 0UL );
    4688           0 :     FD_TEST( idx!=ULONG_MAX );
    4689           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ idx ] ];
    4690             : 
    4691             : 
    4692           0 :     fd_capture_link_buf_t * cap_repl_out = ctx->cap_repl_out;
    4693           0 :     cap_repl_out->base.vt = &fd_capture_link_buf_vt;
    4694           0 :     cap_repl_out->idx     = idx;
    4695           0 :     cap_repl_out->mem     = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
    4696           0 :     cap_repl_out->chunk0  = fd_dcache_compact_chunk0( cap_repl_out->mem, link->dcache );
    4697           0 :     cap_repl_out->wmark   = fd_dcache_compact_wmark( cap_repl_out->mem, link->dcache, link->mtu );
    4698           0 :     cap_repl_out->chunk   = cap_repl_out->chunk0;
    4699           0 :     cap_repl_out->mcache  = link->mcache;
    4700           0 :     cap_repl_out->depth   = fd_mcache_depth( link->mcache );
    4701           0 :     cap_repl_out->seq     = 0UL;
    4702             : 
    4703           0 :     ctx->capture_ctx->capctx_type.buf  = cap_repl_out;
    4704           0 :     ctx->capture_ctx->capture_link    = &cap_repl_out->base;
    4705           0 :     ctx->capture_ctx->current_txn_idx = 0UL;
    4706             : 
    4707             : 
    4708           0 :     ulong consumer_tile_idx = fd_topo_find_tile( topo, "solcap", 0UL );
    4709           0 :     fd_topo_tile_t const * consumer_tile = &topo->tiles[ consumer_tile_idx ];
    4710           0 :     cap_repl_out->fseq = NULL;
    4711           0 :     for( ulong j = 0UL; j < consumer_tile->in_cnt; j++ ) {
    4712           0 :       if( FD_UNLIKELY( consumer_tile->in_link_id[ j ]  == link->id ) ) {
    4713           0 :         cap_repl_out->fseq = fd_fseq_join( fd_topo_obj_laddr( topo, consumer_tile->in_link_fseq_obj_id[ j ] ) );
    4714           0 :         FD_TEST( cap_repl_out->fseq );
    4715           0 :         break;
    4716           0 :       }
    4717           0 :     }
    4718           0 :   }
    4719             : 
    4720           0 :   fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
    4721             : 
    4722           0 :   fd_histf_join( fd_metrics_histf_new_ns( ctx->metrics.store_query_work,   FD_MHIST_EDGES_NS( REPLAY, STORE_QUERY_DURATION_NANOS ) ) );
    4723             : 
    4724             :   /* Ensure precompiles are available, crash fast otherwise */
    4725           0 :   fd_precompiles();
    4726             : 
    4727           0 :   ctx->report_runtime_diffs = tile->replay.report_runtime_diffs;
    4728           0 :   ctx->banks->report_runtime_diffs = tile->replay.report_runtime_diffs;
    4729             : 
    4730           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
    4731           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
    4732           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
    4733           0 : }
    4734             : 
    4735             : static ulong
    4736             : populate_allowed_seccomp( fd_topo_t const *      topo,
    4737             :                           fd_topo_tile_t const * tile,
    4738             :                           ulong                  out_cnt,
    4739           0 :                           struct sock_filter *   out ) {
    4740           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    4741           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    4742           0 :   fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
    4743           0 :   populate_sock_filter_policy_fd_replay_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), FD_ACCDB_FD_RW, (uint)ctx->store_disk_fd, FD_STAKE_DELEGATIONS_FD, FD_EPOCH_CREDITS_FD, FD_COST_TRACKER_FD, FD_VOTE_STAKES_FD );
    4744           0 :   return sock_filter_policy_fd_replay_tile_instr_cnt;
    4745           0 : }
    4746             : 
    4747             : static ulong
    4748             : populate_allowed_fds( fd_topo_t const *      topo,
    4749             :                       fd_topo_tile_t const * tile,
    4750             :                       ulong                  out_fds_cnt,
    4751           0 :                       int *                  out_fds ) {
    4752           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    4753           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    4754           0 :   fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
    4755           0 :   if( FD_UNLIKELY( out_fds_cnt<8UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
    4756             : 
    4757           0 :   ulong out_cnt = 0UL;
    4758           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
    4759           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
    4760           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
    4761           0 :   out_fds[ out_cnt++ ] = FD_ACCDB_FD_RW; /* accounts db */
    4762           0 :   out_fds[ out_cnt++ ] = FD_STAKE_DELEGATIONS_FD; /* stake delegation disk spill */
    4763           0 :   out_fds[ out_cnt++ ] = FD_EPOCH_CREDITS_FD; /* epoch credits disk spill */
    4764           0 :   out_fds[ out_cnt++ ] = FD_COST_TRACKER_FD; /* cost tracker disk spill */
    4765           0 :   out_fds[ out_cnt++ ] = FD_VOTE_STAKES_FD; /* vote stakes disk spill */
    4766           0 :   if( FD_LIKELY( ctx->store_disk_fd>=0 ) )
    4767           0 :     out_fds[ out_cnt++ ] = ctx->store_disk_fd;
    4768             : 
    4769           0 :   return out_cnt;
    4770           0 : }
    4771             : 
    4772             : static inline void
    4773           0 : during_housekeeping( fd_replay_tile_t * ctx ) {
    4774           0 :   wait_info_publish( ctx );
    4775             : 
    4776           0 :   if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) fd_clock_tile_recal( ctx->clock );
    4777             : 
    4778           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_UNHALT_PENDING ) ) {
    4779           0 :     FD_CHECK_CRIT( ctx->halt_replay, "state machine corruption" );
    4780           0 :     FD_LOG_DEBUG(( "keyswitch: unhalting replay" ));
    4781           0 :     ctx->halt_replay = 0;
    4782           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
    4783           0 :   }
    4784             : 
    4785           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
    4786           0 :     if( FD_LIKELY( !ctx->halt_replay ) ) FD_LOG_DEBUG(( "keyswitch: halting replay" ));
    4787           0 :     ctx->halt_replay = 1;
    4788           0 :     if( !ctx->is_leader && ctx->is_booted ) maybe_switch_identity( ctx );
    4789           0 :   }
    4790           0 : }
    4791             : 
    4792             : #undef DEBUG_LOGGING
    4793             : 
    4794             : /* Credits are checked against the out link with the fewest, so the
    4795             :    burst is the most frags one stem iteration publishes to any one out
    4796             :    link: 4, from boot (slot completed, next leader, root advanced,
    4797             :    reset) or try_fini_leader chaining into the next leader slot (slot
    4798             :    completed, next leader, reset, became leader).  after_credit and
    4799             :    returnable_frag never both publish in one iteration: every
    4800             :    after_credit path that publishes returns nonzero, which skips the
    4801             :    input poll. */
    4802           0 : #define STEM_BURST (4UL)
    4803             : 
    4804             : /* fd_tempo_lazy_default( 16384 ) where 16384 is the minimum out-link
    4805             :    depth (i.e. cr_max) but excludes replay_epoch and replay_slot, which
    4806             :    are so infrequent credit availability is a non-issue, and the
    4807             :    per-tile replay_execrp links, whose credits execrp returns per
    4808             :    frag.  */
    4809           0 : #define STEM_LAZY ((long)36865)
    4810             : 
    4811           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_replay_tile_t
    4812           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_replay_tile_t)
    4813             : 
    4814           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
    4815           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
    4816           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
    4817           0 : #define STEM_CALLBACK_RETURNABLE_FRAG     returnable_frag
    4818           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
    4819             : 
    4820             : #include "../../disco/stem/fd_stem.c"
    4821             : 
    4822             : static ulong
    4823           0 : max_event_sz( fd_topo_tile_t const * tile ) {
    4824             :   /* replay always emits block_completed; runtime_block (the largest
    4825             :      runtime event) is added when runtime diffs are on. */
    4826           0 :   ulong sz = fd_ulong_max( sizeof(fd_event_block_completed_t), sizeof(fd_event_metrics_trigger_t) );
    4827           0 :   if( tile->replay.report_runtime_diffs && sizeof(fd_event_runtime_block_t)>sz ) sz = sizeof(fd_event_runtime_block_t);
    4828           0 :   return sz;
    4829           0 : }
    4830             : 
    4831             : fd_topo_run_tile_t fd_tile_replay = {
    4832             :   .name                     = "replay",
    4833             :   .max_event_sz             = max_event_sz,
    4834             :   .populate_allowed_seccomp = populate_allowed_seccomp,
    4835             :   .populate_allowed_fds     = populate_allowed_fds,
    4836             :   .scratch_align            = scratch_align,
    4837             :   .scratch_footprint        = scratch_footprint,
    4838             :   .privileged_init          = privileged_init,
    4839             :   .unprivileged_init        = unprivileged_init,
    4840             :   .run                      = stem_run,
    4841             : };

Generated by: LCOV version 1.14