LCOV - code coverage report
Current view: top level - disco/pack - fd_pack_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 793 0.0 %
Date: 2026-08-17 04:34:10 Functions: 0 17 0.0 %

          Line data    Source code
       1             : #include "../tiles.h"
       2             : 
       3             : #include "generated/fd_pack_tile_seccomp.h"
       4             : 
       5             : #include "../../util/pod/fd_pod_format.h"
       6             : #include "../../discof/replay/fd_replay_tile.h" // layering violation
       7             : #include "../fd_txn_m.h"
       8             : #include "../keyguard/fd_keyload.h"
       9             : #include "../keyguard/fd_keyswitch.h"
      10             : #include "../keyguard/fd_keyguard.h"
      11             : #include "../keyguard/fd_keyguard_client.h"
      12             : #include "../metrics/fd_metrics.h"
      13             : #include "../pack/fd_pack.h"
      14             : #include "../pack/fd_pack_cost.h"
      15             : #include "../pack/fd_pack_pacing.h"
      16             : 
      17             : #include <string.h>
      18             : 
      19             : /* fd_pack is responsible for taking verified transactions, and
      20             :    arranging them into "microblocks" (groups) of transactions to
      21             :    be executed serially.  It can try to do clever things so that
      22             :    multiple microblocks can execute in parallel, if they don't
      23             :    write to the same accounts. */
      24             : 
      25           0 : #define IN_KIND_RESOLV       (0UL)
      26           0 : #define IN_KIND_POH          (1UL)
      27           0 : #define IN_KIND_EXECLE       (2UL)
      28           0 : #define IN_KIND_SIGN         (3UL)
      29           0 : #define IN_KIND_REPLAY       (4UL)
      30           0 : #define IN_KIND_EXECUTED_TXN (5UL)
      31             : 
      32             : /* Pace microblocks, but only slightly.  This helps keep performance
      33             :    more stable.  This limit is 2,000 microblocks/second/execle.  At
      34             :    MAX_TXN_PER_MICROBLOCK transactions/microblock, that's
      35             :    2000*MAX_TXN_PER_MICROBLOCK txn/sec/execle. */
      36           0 : #define MICROBLOCK_DURATION_NS  (0L)
      37             : 
      38             : /* There are 151 accepted blockhashes, but those don't include skips.
      39             :    This check is neither precise nor accurate, but just good enough.
      40             :    The execle tile does the final check.  We give a little margin for a
      41             :    few percent skip rate. */
      42           0 : #define TRANSACTION_LIFETIME_SLOTS 160UL
      43             : 
      44             : /* Time is normally a long, but pack expects a ulong.  Add -LONG_MIN to
      45             :    the time values so that LONG_MIN maps to 0, LONG_MAX maps to
      46             :    ULONG_MAX, and everything in between maps linearly with a slope of 1.
      47             :    Just subtracting LONG_MIN results in signed integer overflow, which
      48             :    is U.B. */
      49             : #define TIME_OFFSET 0x8000000000000000UL
      50             : FD_STATIC_ASSERT( (ulong)LONG_MIN+TIME_OFFSET==0UL,       time_offset );
      51             : FD_STATIC_ASSERT( (ulong)LONG_MAX+TIME_OFFSET==ULONG_MAX, time_offset );
      52             : 
      53             : /* 1.6 M cost units, enough for 1 max size transaction */
      54             : const ulong CUS_PER_MICROBLOCK = 1600000UL;
      55             : 
      56             : #define SMALL_MICROBLOCKS 1
      57             : 
      58             : #if SMALL_MICROBLOCKS
      59             : const float VOTE_FRACTION = 1.0f; /* schedule all available votes first */
      60           0 : #define EFFECTIVE_TXN_PER_MICROBLOCK 1UL
      61             : #else
      62             : const float VOTE_FRACTION = 0.75f; /* TODO: Is this the right value? */
      63             : #define EFFECTIVE_TXN_PER_MICROBLOCK MAX_TXN_PER_MICROBLOCK
      64             : #endif
      65             : 
      66             : #if !SMALL_MICROBLOCKS
      67             : /* There's overhead associated with each microblock the execle tile
      68             :    tries to execute it, so the optimal strategy is not to produce a
      69             :    microblock with a single transaction as soon as we receive it.
      70             :    Basically, if we have less than MAX_TXN_PER_MICROBLOCK transactions,
      71             :    we want to wait a little to see if we receive additional transactions
      72             :    before we schedule a microblock.  We can model the optimum amount of
      73             :    time to wait, but the equation is complicated enough that we want to
      74             :    compute it before compile time. wait_duration[i] for i in
      75             :    [0, MAX_TXN_PER_MICROBLOCK] gives the time in nanoseconds pack should
      76             :    wait after receiving its most recent transaction before scheduling if
      77             :    it has i transactions available.  Unsurprisingly,
      78             :    wait_duration[MAX_TXN_PER_MICROBLOCK] is 0.  wait_duration[0] is
      79             :    ULONG_MAX, so we'll always wait if we have 0 transactions. */
      80             : FD_IMPORT( wait_duration, "src/disco/pack/pack_delay.bin", ulong, 6, "" );
      81             : #endif
      82             : 
      83             : 
      84             : 
      85             : #if FD_PACK_USE_EXTRA_STORAGE
      86             : /* When we are done being leader for a slot and we are leader in the
      87             :    very next slot, it can still take some time to transition.  This is
      88             :    because the bank has to be finalized, a hash calculated, and various
      89             :    other things done in the replay stage to create the new child bank.
      90             : 
      91             :    During that time, pack cannot send transactions to execles so it
      92             :    needs to be able to buffer.  Typically, these so called "leader
      93             :    transitions" are short (<15 millis), so a low value here would
      94             :    suffice.  However, in some cases when there is memory pressure on the
      95             :    NUMA node or when the operating system context switches relevant
      96             :    threads out, it can take significantly longer.
      97             : 
      98             :    To prevent drops in these cases and because we assume execles are
      99             :    fast enough to drain this buffer once we do become leader, we set
     100             :    this buffer size to be quite large. */
     101             : 
     102             : #define DEQUE_NAME extra_txn_deq
     103             : #define DEQUE_T    fd_txn_e_t
     104             : #define DEQUE_MAX  (128UL*1024UL)
     105             : #include "../../../../util/tmpl/fd_deque.c"
     106             : 
     107             : #endif
     108             : 
     109             : /* Sync with src/app/shared/fd_config.c */
     110           0 : #define FD_PACK_STRATEGY_PERF     0
     111           0 : #define FD_PACK_STRATEGY_BALANCED 1
     112             : 
     113             : static char const * const schedule_strategy_strings[2] = { "PRF", "BAL" };
     114             : 
     115             : 
     116             : typedef struct {
     117             :   fd_acct_addr_t commission_pubkey[1];
     118             :   ulong          commission;
     119             : } block_builder_info_t;
     120             : 
     121             : typedef struct {
     122             :   long time;
     123             :   ulong sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_CNT ];
     124             : } fd_pack_sched_results_snap_t;
     125             : 
     126             : typedef struct {
     127             :   fd_wksp_t * mem;
     128             :   ulong       chunk0;
     129             :   ulong       wmark;
     130             : } fd_pack_in_ctx_t;
     131             : 
     132             : typedef struct {
     133             :   fd_pack_t *  pack;
     134             :   fd_txn_e_t * cur_spot;
     135             :   int          is_bundle; /* is the current transaction a bundle */
     136             : 
     137             :   uchar executed_txn_sig[ 64UL ];
     138             :   uchar txn_committed;
     139             : 
     140             :   /* One of the FD_PACK_STRATEGY_* values defined above */
     141             :   int      strategy;
     142             : 
     143             :   /* The value passed to fd_pack_new, etc. */
     144             :   ulong    max_pending_transactions;
     145             : 
     146             :   /* The leader slot we are currently packing for, or ULONG_MAX if we
     147             :      are not the leader. */
     148             :   ulong  leader_slot;
     149             :   void const * leader_bank;
     150             :   ulong        leader_bank_idx;
     151             :   ulong        leader_bank_seq;
     152             : 
     153             :   fd_became_leader_t _became_leader[1];
     154             : 
     155             :   /* The number of microblocks we have packed for the current leader
     156             :      slot.  Will always be <= slot_max_microblocks.  We must track
     157             :      this so that when we are done we can tell the PoH tile how many
     158             :      microblocks to expect in the slot. */
     159             :   ulong slot_microblock_cnt;
     160             : 
     161             :   /* Counter which increments when we've finished packing for a slot */
     162             :   uint pack_idx;
     163             : 
     164             :   ulong pack_txn_cnt; /* total num transactions packed since startup */
     165             : 
     166             :   /* The maximum number of microblocks that can be packed in this slot.
     167             :      Provided by the PoH tile when we become leader.*/
     168             :   ulong slot_max_microblocks;
     169             : 
     170             :   /* Cap (in bytes) of the amount of transaction data we produce in each
     171             :      block to avoid hitting the shred limits.  See where this is set for
     172             :      more explanation. */
     173             :   ulong slot_max_data;
     174             :   int   larger_shred_limits_per_block;
     175             : 
     176             :   /* Consensus critical slot cost limits. */
     177             :   struct {
     178             :     ulong slot_max_cost;
     179             :     ulong slot_max_vote_cost;
     180             :     ulong slot_max_write_cost_per_acct;
     181             :     ulong slot_max_allocated_data_per_block;
     182             :     ulong slot_max_data_shreds;
     183             :   } limits;
     184             : 
     185             :   /* If drain_execle is non-zero, then the pack tile must wait until all
     186             :      execle are idle before scheduling any more microblocks.  This is
     187             :      primarily helpful in irregular leader transitions, e.g. while being
     188             :      leader for slot N, we switch forks to a slot M (!=N+1) in which we
     189             :      are also leader.  We don't want to execute microblocks for
     190             :      different slots concurrently. */
     191             :   int drain_execle;
     192             : 
     193             :   /* Updated during housekeeping and used only for checking if the
     194             :      leader slot has ended.  Might be off by one housekeeping duration,
     195             :      but that should be small relative to a slot duration. */
     196             :   long  approx_wallclock_ns;
     197             : 
     198             :   /* approx_tickcount is updated in during_housekeeping() with
     199             :      fd_tickcount() and will match approx_wallclock_ns.  This is done
     200             :      because we need to include an accurate nanosecond timestamp in
     201             :      every fd_txn_p_t but don't want to have to call the expensive
     202             :      fd_log_wallclock() in in the critical path. We can use
     203             :      fd_tempo_tick_per_ns() to convert from ticks to nanoseconds over
     204             :      small periods of time. */
     205             :   long  approx_tickcount;
     206             : 
     207             :   fd_rng_t * rng;
     208             : 
     209             :   uint  rng_seed;
     210             :   ulong rng_idx;
     211             : 
     212             :   /* The end wallclock time of the leader slot we are currently packing
     213             :      for, if we are currently packing for a slot.*/
     214             :   long slot_end_ns;
     215             : 
     216             :   /* The current dynamic upper bound on total microblocks for this slot.
     217             :      Monotonically decreasing over the slot lifetime. */
     218             :   ulong slot_dynamic_max_microblocks;
     219             : 
     220             :   /* Set by during_housekeeping when the dynamic bound drops below
     221             :      slot_max_microblocks.  Consumed by after_credit which publishes
     222             :      the updated bound to POH over the pack_poh link. */
     223             :   int pending_reduce_mb_bound;
     224             : 
     225             :   /* pacer and ticks_per_ns are used for pacing CUs through the slot,
     226             :      i.e. deciding when to schedule a microblock given the number of CUs
     227             :      that have been consumed so far.  pacer is an opaque pacing object,
     228             :      which is initialized when the pack tile is packing a slot.
     229             :      ticks_per_ns is the cached value from tempo. */
     230             :   fd_pack_pacing_t pacer[1];
     231             :   double           ticks_per_ns;
     232             : 
     233             :   /* last_successful_insert stores the tickcount of the last
     234             :      successful transaction insert. */
     235             :   long last_successful_insert;
     236             : 
     237             :   /* highest_observed_slot stores the highest slot number we've seen
     238             :      from any transaction coming from the resolv tile.  When this
     239             :      increases, we expire old transactions. */
     240             :   ulong highest_observed_slot;
     241             : 
     242             :   /* microblock_duration_ns, and wait_duration
     243             :      respectively scaled to be in ticks instead of nanoseconds */
     244             :   ulong microblock_duration_ticks;
     245             : #if !SMALL_MICROBLOCKS
     246             :   ulong wait_duration_ticks[ MAX_TXN_PER_MICROBLOCK+1UL ];
     247             : #endif
     248             : 
     249             : #if FD_PACK_USE_EXTRA_STORAGE
     250             :   /* In addition to the available transactions that pack knows about, we
     251             :      also store a larger ring buffer for handling cases when pack is
     252             :      full.  This is an fd_deque. */
     253             :   fd_txn_e_t * extra_txn_deq;
     254             :   int          insert_to_extra; /* whether the last insert was into pack or the extra deq */
     255             : #endif
     256             : 
     257             :   fd_pack_in_ctx_t in[ 32 ];
     258             :   int              in_kind[ 32 ];
     259             : 
     260             :   ulong    execle_cnt;
     261             :   ulong    execle_idle_bitset; /* bit i is 1 if we've observed *execle_current[i]==execle_expect[i] */
     262             :   int      poll_cursor; /* in [0, execle_cnt), the next execle to poll */
     263             :   int      use_consumed_cus;
     264             :   long     skip_cnt;
     265             :   ulong *  execle_current[ FD_PACK_MAX_EXECLE_TILES ];
     266             :   ulong    execle_expect[ FD_PACK_MAX_EXECLE_TILES  ];
     267             :   /* execle_ready_at[x] means don't check execle x until tickcount is at
     268             :      least execle_ready_at[x]. */
     269             :   long     execle_ready_at[ FD_PACK_MAX_EXECLE_TILES  ];
     270             : 
     271             :   fd_wksp_t * execle_out_mem;
     272             :   ulong       execle_out_chunk0;
     273             :   ulong       execle_out_wmark;
     274             :   ulong       execle_out_chunk;
     275             : 
     276             :   fd_wksp_t * poh_out_mem;
     277             :   ulong       poh_out_chunk0;
     278             :   ulong       poh_out_wmark;
     279             :   ulong       poh_out_chunk;
     280             : 
     281             :   ulong      insert_result[ FD_PACK_INSERT_RETVAL_CNT ];
     282             :   fd_histf_t schedule_duration[ 1 ];
     283             :   fd_histf_t no_sched_duration[ 1 ];
     284             :   fd_histf_t insert_duration  [ 1 ];
     285             :   fd_histf_t complete_duration[ 1 ];
     286             : 
     287             :   struct {
     288             :     uint metric_state;
     289             :     long metric_state_begin;
     290             :     long metric_timing[ 16 ];
     291             :   };
     292             : 
     293             :   /* last_sched_metrics is a snapshot of the schedule outcome counters
     294             :      during the last schedule which included at least one successful
     295             :      outcome. */
     296             :   fd_pack_sched_results_snap_t last_sched_metrics[ 1 ];
     297             : 
     298             :     /* last_sched_metrics is a snapshot of the schedule outcome counters
     299             :        at the last start-of-leader-block event. */
     300             :   fd_pack_sched_results_snap_t start_block_sched_metrics[ 1 ];
     301             : 
     302             :   struct {
     303             :     ulong id;
     304             :     ulong txn_cnt;
     305             :     ulong txn_received;
     306             :     ulong min_blockhash_slot;
     307             :     fd_txn_e_t * _txn[ FD_PACK_MAX_TXN_PER_BUNDLE ];
     308             :     fd_txn_e_t * const * bundle; /* points to _txn when non-NULL */
     309             :   } current_bundle[1];
     310             : 
     311             :   block_builder_info_t blk_engine_cfg[1];
     312             : 
     313             :   struct {
     314             :     int                   enabled;
     315             :     int                   ib_inserted; /* in this slot */
     316             :     fd_acct_addr_t        vote_pubkey[1];
     317             :     fd_acct_addr_t        identity_pubkey[1];
     318             :     fd_bundle_crank_gen_t gen[1];
     319             :     fd_acct_addr_t        tip_receiver_owner[1];
     320             :     ulong                 epoch;
     321             :     fd_bundle_crank_tip_payment_config_t prev_config[1]; /* as of start of slot, then updated */
     322             :     uchar                 recent_blockhash[32];
     323             :     fd_ed25519_sig_t      last_sig[1];
     324             : 
     325             :     fd_keyswitch_t *      keyswitch;
     326             :     fd_keyguard_client_t  keyguard_client[1];
     327             : 
     328             :     ulong                 metrics[4];
     329             :   } crank[1];
     330             : 
     331             : 
     332             :   /* Used between during_frag and after_frag */
     333             :   ulong pending_rebate_sz;
     334             :   union{ fd_pack_rebate_t rebate[1]; uchar footprint[USHORT_MAX]; } rebate[1];
     335             : } fd_pack_ctx_t;
     336             : 
     337           0 : #define BUNDLE_META_SZ 40UL
     338             : FD_STATIC_ASSERT( sizeof(block_builder_info_t)==BUNDLE_META_SZ, blk_engine_cfg );
     339             : 
     340           0 : #define FD_PACK_METRIC_STATE_TRANSACTIONS 0
     341           0 : #define FD_PACK_METRIC_STATE_EXECLES      1
     342           0 : #define FD_PACK_METRIC_STATE_LEADER       2
     343           0 : #define FD_PACK_METRIC_STATE_MICROBLOCKS  3
     344             : 
     345             : /* Updates one component of the metric state.  If the state has changed,
     346             :    records the change. */
     347             : static inline void
     348             : update_metric_state( fd_pack_ctx_t * ctx,
     349             :                      long            effective_as_of,
     350             :                      int             type,
     351           0 :                      int             status ) {
     352           0 :   uint current_state = fd_uint_insert_bit( ctx->metric_state, type, status );
     353           0 :   if( FD_UNLIKELY( current_state!=ctx->metric_state ) ) {
     354           0 :     ctx->metric_timing[ ctx->metric_state ] += effective_as_of - ctx->metric_state_begin;
     355           0 :     ctx->metric_state_begin = effective_as_of;
     356           0 :     ctx->metric_state = current_state;
     357           0 :   }
     358           0 : }
     359             : 
     360             : static inline void
     361           0 : remove_ib( fd_pack_ctx_t * ctx ) {
     362             :   /* It's likely the initializer bundle is long scheduled, but we want to
     363             :      try deleting it just in case. */
     364           0 :   if( FD_UNLIKELY( ctx->crank->enabled & ctx->crank->ib_inserted ) ) {
     365           0 :     ulong deleted = fd_pack_delete_transaction( ctx->pack, (fd_ed25519_sig_t const *)ctx->crank->last_sig );
     366           0 :     FD_MCNT_INC( PACK, TXN_DELETED, deleted );
     367           0 :   }
     368           0 :   ctx->crank->ib_inserted = 0;
     369           0 : }
     370             : 
     371             : 
     372             : FD_FN_CONST static inline ulong
     373           0 : scratch_align( void ) {
     374           0 :   return 4096UL;
     375           0 : }
     376             : 
     377             : FD_FN_PURE static inline ulong
     378           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     379           0 :   fd_pack_limits_t limits[1] = {{
     380           0 :     .max_cost_per_block           = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK_UPPER_BOUND,
     381           0 :     .max_vote_cost_per_block      = FD_PACK_MAX_VOTE_COST_PER_BLOCK_UPPER_BOUND,
     382           0 :     .max_write_cost_per_acct      = FD_PACK_MAX_WRITE_COST_PER_ACCT_UPPER_BOUND,
     383           0 :     .max_data_bytes_per_block     = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
     384           0 :     .max_txn_per_microblock       = EFFECTIVE_TXN_PER_MICROBLOCK,
     385           0 :     .max_microblocks_per_block    = (ulong)UINT_MAX, /* Limit not known yet */
     386           0 :     .max_allocated_data_per_block = FD_PACK_MAX_ALLOCATED_DATA_PER_BLOCK,
     387           0 :   }};
     388             : 
     389           0 :   ulong l = FD_LAYOUT_INIT;
     390           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t )                                   );
     391           0 :   l = FD_LAYOUT_APPEND( l, fd_rng_align(),           fd_rng_footprint()                                        );
     392           0 :   l = FD_LAYOUT_APPEND( l, fd_pack_align(),          fd_pack_footprint( tile->pack.max_pending_transactions,
     393           0 :                                                                         BUNDLE_META_SZ,
     394           0 :                                                                         tile->pack.execle_tile_count,
     395           0 :                                                                         limits                               ) );
     396             : #if FD_PACK_USE_EXTRA_STORAGE
     397             :   l = FD_LAYOUT_APPEND( l, extra_txn_deq_align(),    extra_txn_deq_footprint()                                 );
     398             : #endif
     399           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     400           0 : }
     401             : 
     402             : static inline void
     403             : log_end_block_metrics( fd_pack_ctx_t * ctx,
     404             :                        long            now,
     405             :                        char const    * reason,
     406           0 :                        ulong           cus_consumed_in_block ) {
     407           0 : #define DELTA( m ) (fd_metrics_tl[ MIDX(COUNTER, PACK, TXN_SCHEDULED_##m) ] - ctx->last_sched_metrics->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_##m##_IDX ])
     408           0 : #define AVAIL( m ) (fd_metrics_tl[ MIDX(GAUGE, PACK, TXN_AVAILABLE_##m) ])
     409           0 :     FD_LOG_INFO(( "pack_end_block(slot=%lu,%s,%lx,ticks_since_last_schedule=%ld,reasons=%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu;remaining=%lu+%lu+%lu+%lu;smallest=%lu;cus=%lu->%lu)",
     410           0 :           ctx->leader_slot, reason, ctx->execle_idle_bitset, now-ctx->last_sched_metrics->time,
     411           0 :           DELTA( TAKEN ), DELTA( CU_LIMIT ), DELTA( FAST_PATH ), DELTA( BYTE_LIMIT ), DELTA( ALLOC_LIMIT ), DELTA( WRITE_COST ), DELTA( SLOW_PATH ), DELTA( DEFER_SKIP ),
     412           0 :           AVAIL(REGULAR), AVAIL(VOTES), AVAIL(BUNDLES), AVAIL(CONFLICTING),
     413           0 :           (fd_metrics_tl[ MIDX(GAUGE, PACK, TXN_PENDING_SMALLEST_CU) ]),
     414           0 :           (cus_consumed_in_block),
     415           0 :           (fd_metrics_tl[ MIDX(GAUGE, PACK, BLOCK_CU_CONSUMED) ])
     416           0 :     ));
     417           0 : #undef AVAIL
     418           0 : #undef DELTA
     419           0 : }
     420             : 
     421             : static inline void
     422           0 : get_done_packing( fd_pack_ctx_t * ctx, fd_done_packing_t * done_packing, int reason ) {
     423           0 :     done_packing->microblocks_in_slot = ctx->slot_microblock_cnt;
     424           0 :     done_packing->end_slot_reason = reason;
     425           0 :     fd_pack_get_block_limits( ctx->pack, done_packing->limits_usage, done_packing->limits );
     426             : 
     427           0 : #define DELTA( mem, m ) (fd_metrics_tl[ MIDX(COUNTER, PACK, TXN_SCHEDULED_##m) ] - ctx->mem->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_##m##_IDX ])
     428           0 :     done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_TAKEN_IDX       ] = DELTA( start_block_sched_metrics, TAKEN       );
     429           0 :     done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_CU_LIMIT_IDX    ] = DELTA( start_block_sched_metrics, CU_LIMIT    );
     430           0 :     done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_FAST_PATH_IDX   ] = DELTA( start_block_sched_metrics, FAST_PATH   );
     431           0 :     done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_BYTE_LIMIT_IDX  ] = DELTA( start_block_sched_metrics, BYTE_LIMIT  );
     432           0 :     done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_ALLOC_LIMIT_IDX ] = DELTA( start_block_sched_metrics, ALLOC_LIMIT );
     433           0 :     done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_WRITE_COST_IDX  ] = DELTA( start_block_sched_metrics, WRITE_COST  );
     434           0 :     done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_SLOW_PATH_IDX   ] = DELTA( start_block_sched_metrics, SLOW_PATH   );
     435           0 :     done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_DEFER_SKIP_IDX  ] = DELTA( start_block_sched_metrics, DEFER_SKIP  );
     436             : 
     437           0 :     done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_TAKEN_IDX       ] = DELTA( last_sched_metrics, TAKEN       );
     438           0 :     done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_CU_LIMIT_IDX    ] = DELTA( last_sched_metrics, CU_LIMIT    );
     439           0 :     done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_FAST_PATH_IDX   ] = DELTA( last_sched_metrics, FAST_PATH   );
     440           0 :     done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_BYTE_LIMIT_IDX  ] = DELTA( last_sched_metrics, BYTE_LIMIT  );
     441           0 :     done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_ALLOC_LIMIT_IDX ] = DELTA( last_sched_metrics, ALLOC_LIMIT );
     442           0 :     done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_WRITE_COST_IDX  ] = DELTA( last_sched_metrics, WRITE_COST  );
     443           0 :     done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_SLOW_PATH_IDX   ] = DELTA( last_sched_metrics, SLOW_PATH   );
     444           0 :     done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_DEFER_SKIP_IDX  ] = DELTA( last_sched_metrics, DEFER_SKIP  );
     445           0 : #undef DELTA
     446             : 
     447           0 :   fd_pack_get_pending_smallest( ctx->pack, done_packing->pending_smallest, done_packing->pending_votes_smallest );
     448           0 : }
     449             : 
     450             : static inline void
     451           0 : metrics_write( fd_pack_ctx_t * ctx ) {
     452           0 :   FD_MCNT_ENUM_COPY( PACK, TXN_INSERTED,          ctx->insert_result  );
     453           0 :   FD_MCNT_ENUM_COPY( PACK, STATE_DURATION_NANOS,        ((ulong*)ctx->metric_timing) );
     454           0 :   FD_MCNT_ENUM_COPY( PACK, BUNDLE_CRANK_RESULT,           ctx->crank->metrics );
     455           0 :   FD_MHIST_COPY( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS, ctx->schedule_duration );
     456           0 :   FD_MHIST_COPY( PACK, NO_SCHEDULE_MICROBLOCK_DURATION_SECONDS, ctx->no_sched_duration );
     457           0 :   FD_MHIST_COPY( PACK, INSERT_TRANSACTION_DURATION_SECONDS,  ctx->insert_duration   );
     458           0 :   FD_MHIST_COPY( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS, ctx->complete_duration );
     459             : 
     460           0 :   fd_pack_metrics_write( ctx->pack );
     461           0 : }
     462             : 
     463             : /* compute_dynamic_max_microblocks: Computes the upper bound on total
     464             :    microblocks based on remaining time and bank count.
     465             : 
     466             :    The basic idea here is that if there is 1ms left in the slot, we
     467             :    don't expect to schedule 130k microblocks.  We can reduce our
     468             :    remaining budget which allows POH to advance hashing a bit further
     469             :    and avoid having to do a lot of hashing after the slot ends.
     470             : 
     471             :    The fastest we can execute a transaction is about 1us.  With n
     472             :    execle tiles, that means we can execute at most n txn/us.  If we have
     473             :    k ms left in the block, only reserve up to k*n*1000 microblocks. */
     474             : 
     475             : static inline ulong
     476           0 : compute_dynamic_max_microblocks( fd_pack_ctx_t * ctx ) {
     477           0 :   long  now = ctx->approx_wallclock_ns + (long)((double)(fd_tickcount() - ctx->approx_tickcount) / ctx->ticks_per_ns);
     478           0 :   long  end = ctx->slot_end_ns;
     479             : 
     480             :   /* If the slot has ended, don't reserve any more microblocks. */
     481           0 :   if( FD_UNLIKELY( now>=end ) ) return ctx->slot_microblock_cnt;
     482             : 
     483             :   /* remaining_ns * n / 1000 = (remaining_ns/1e6 ms) * n * 1000.
     484             : 
     485             :      Overflow: remaining_ns is at most ~4e8, n at most 64, so
     486             :      remaining_ns * n is at most ~2.6e10 << 1.8e19. */
     487             : 
     488           0 :   ulong remaining_ns = (ulong)(end - now);
     489           0 :   ulong n            = ctx->execle_cnt;
     490           0 :   ulong cnt          = ctx->slot_microblock_cnt;
     491           0 :   ulong R            = ctx->slot_max_microblocks - cnt;
     492           0 :   ulong can_execute  = remaining_ns * n / 1000UL;
     493             : 
     494           0 :   return cnt + fd_ulong_min( R, can_execute );
     495           0 : }
     496             : 
     497             : static inline void
     498           0 : during_housekeeping( fd_pack_ctx_t * ctx ) {
     499           0 :   ctx->approx_wallclock_ns = fd_log_wallclock();
     500           0 :   ctx->approx_tickcount = fd_tickcount();
     501             : 
     502           0 :   if( FD_UNLIKELY( ctx->crank->enabled && fd_keyswitch_state_query( ctx->crank->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     503           0 :     fd_memcpy( ctx->crank->identity_pubkey, ctx->crank->keyswitch->bytes, 32UL );
     504           0 :     fd_keyswitch_state( ctx->crank->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     505           0 :   }
     506             : 
     507           0 :   if( FD_LIKELY( ctx->leader_slot!=ULONG_MAX ) ) {
     508           0 :     ulong raw = compute_dynamic_max_microblocks( ctx );
     509           0 :     ulong prev = ctx->slot_dynamic_max_microblocks;
     510             : 
     511             :     /* Enforce monotonically decreasing. This ensures pack's bound is
     512             :        always smaller than POH's.  */
     513           0 :     ctx->slot_dynamic_max_microblocks = fd_ulong_min( raw, prev );
     514             : 
     515             :     /* If the bound decreased, we must update pack's internal scheduling
     516             :        limit.  Otherwise, pack could schedule a microblock that exceeds
     517             :        the remaining capacity from the tile's and poh's perspectives
     518             :        (e.g. pack might produce a 5-transaction microblock when only 4
     519             :        microblocks worth of space remain). */
     520           0 :     if( FD_UNLIKELY( ctx->slot_dynamic_max_microblocks < prev ) ) {
     521           0 :       fd_pack_limits_t limits[1];
     522           0 :       limits->max_cost_per_block           = ctx->limits.slot_max_cost;
     523           0 :       limits->max_data_bytes_per_block     = ctx->slot_max_data;
     524           0 :       limits->max_microblocks_per_block    = ctx->slot_dynamic_max_microblocks;
     525           0 :       limits->max_vote_cost_per_block      = ctx->limits.slot_max_vote_cost;
     526           0 :       limits->max_write_cost_per_acct      = ctx->limits.slot_max_write_cost_per_acct;
     527           0 :       limits->max_txn_per_microblock       = ULONG_MAX; /* unused */
     528           0 :       limits->max_allocated_data_per_block = ctx->limits.slot_max_allocated_data_per_block;
     529           0 :       fd_pack_set_block_limits( ctx->pack, limits );
     530             : 
     531           0 :       ctx->pending_reduce_mb_bound = 1; /* publish bound decrease */
     532           0 :     }
     533           0 :   }
     534           0 : }
     535             : 
     536             : static inline void
     537             : before_credit( fd_pack_ctx_t *     ctx,
     538             :                fd_stem_context_t * stem,
     539           0 :                int *               charge_busy ) {
     540           0 :   (void)stem;
     541             : 
     542           0 :   if( FD_UNLIKELY( (ctx->cur_spot!=NULL) & !ctx->is_bundle ) ) {
     543           0 :     *charge_busy = 1;
     544             : 
     545             :     /* If we were overrun while processing a frag from an in, then
     546             :        cur_spot is left dangling and not cleaned up, so clean it up here
     547             :        (by returning the slot to the pool of free slots).  If the last
     548             :        transaction was a bundle, then we don't want to return it.  When
     549             :        we try to process the first transaction in the next bundle, we'll
     550             :        see we never got the full bundle and cancel the whole last
     551             :        bundle, returning all the storage to the pool. */
     552             : #if FD_PACK_USE_EXTRA_STORAGE
     553             :     if( FD_LIKELY( !ctx->insert_to_extra ) ) fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
     554             :     else                                     extra_txn_deq_remove_tail( ctx->extra_txn_deq       );
     555             : #else
     556           0 :     fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
     557           0 : #endif
     558           0 :     ctx->cur_spot = NULL;
     559           0 :   }
     560           0 : }
     561             : 
     562             : #if FD_PACK_USE_EXTRA_STORAGE
     563             : /* insert_from_extra: helper method to pop the transaction at the head
     564             :    off the extra txn deque and insert it into pack.  Requires that
     565             :    ctx->extra_txn_deq is non-empty, but it's okay to call it if pack is
     566             :    full.  Returns the result of fd_pack_insert_txn_fini. */
     567             : static inline int
     568             : insert_from_extra( fd_pack_ctx_t * ctx ) {
     569             :   fd_txn_e_t       * spot       = fd_pack_insert_txn_init( ctx->pack );
     570             :   fd_txn_e_t const * insert     = extra_txn_deq_peek_head( ctx->extra_txn_deq );
     571             :   fd_txn_t   const * insert_txn = TXN(insert->txnp);
     572             :   fd_memcpy( spot->txnp->payload, insert->txnp->payload, insert->txnp->payload_sz                                                     );
     573             :   fd_memcpy( TXN(spot->txnp),     insert_txn,            fd_txn_footprint( insert_txn->instr_cnt, insert_txn->addr_table_lookup_cnt ) );
     574             :   fd_memcpy( spot->alt_accts,     insert->alt_accts,     insert_txn->addr_table_adtl_cnt*sizeof(fd_acct_addr_t)                       );
     575             :   spot->txnp->payload_sz = insert->txnp->payload_sz;
     576             :   spot->txnp->source_tpu  = insert->txnp->source_tpu;
     577             :   spot->txnp->source_ipv4 = insert->txnp->source_ipv4;
     578             :   spot->txnp->scheduler_arrival_time_nanos = insert->txnp->scheduler_arrival_time_nanos;
     579             :   spot->txnp->first_seen_nanos = insert->txnp->first_seen_nanos;
     580             :   extra_txn_deq_remove_head( ctx->extra_txn_deq );
     581             : 
     582             :   ulong blockhash_slot = insert->txnp->blockhash_slot;
     583             : 
     584             :   ulong deleted;
     585             :   long insert_duration = -fd_tickcount();
     586             :   int result = fd_pack_insert_txn_fini( ctx->pack, spot, blockhash_slot, &deleted );
     587             :   insert_duration      += fd_tickcount();
     588             : 
     589             :   FD_MCNT_INC( PACK, TXN_DELETED, deleted );
     590             :   ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
     591             :   fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
     592             :   FD_MCNT_INC( PACK, TXN_EXTRA_RETRIEVED, 1UL );
     593             :   return result;
     594             : }
     595             : #endif
     596             : 
     597             : static inline void
     598             : after_credit( fd_pack_ctx_t *     ctx,
     599             :               fd_stem_context_t * stem,
     600             :               int *               opt_poll_in FD_PARAM_UNUSED,
     601           0 :               int *               charge_busy ) {
     602           0 :   if( FD_UNLIKELY( (ctx->skip_cnt--)>0L ) ) return; /* It would take ages for this to hit LONG_MIN */
     603             : 
     604           0 :   long now = fd_tickcount();
     605             : 
     606           0 :   int pacing_execle_cnt = (int)fd_pack_pacing_enabled_bank_cnt( ctx->pacer, now );
     607             : 
     608           0 :   ulong execle_cnt = ctx->execle_cnt;
     609             : 
     610             : 
     611             :   /* If any execle are busy, check one of the busy ones see if it is
     612             :      still busy. */
     613           0 :   if( FD_LIKELY( ctx->execle_idle_bitset!=fd_ulong_mask_lsb( (int)execle_cnt ) ) ) {
     614           0 :     int   poll_cursor = ctx->poll_cursor;
     615           0 :     ulong busy_bitset = (~ctx->execle_idle_bitset) & fd_ulong_mask_lsb( (int)execle_cnt );
     616             : 
     617             :     /* Suppose execle_cnt is 4 and idle_bitset looks something like this
     618             :        (pretending it's a uchar):
     619             :                 0000 1001
     620             :                        ^ busy cursor is 1
     621             :        Then busy_bitset is
     622             :                 0000 0110
     623             :        Rotate it right by 2 bits
     624             :                 1000 0001
     625             :        Find lsb returns 0, so busy cursor remains 2, and we poll
     626             :        execle 2.
     627             : 
     628             :        If instead idle_bitset were
     629             :                 0000 1110
     630             :                        ^
     631             :        The rotated version would be
     632             :                 0100 0000
     633             :        Find lsb will return 6, so busy cursor would be set to 0, and
     634             :        we'd poll execle 0, which is the right one. */
     635           0 :     poll_cursor++;
     636           0 :     poll_cursor = (poll_cursor + fd_ulong_find_lsb( fd_ulong_rotate_right( busy_bitset, (poll_cursor&63) ) )) & 63;
     637             : 
     638           0 :     if( FD_UNLIKELY(
     639             :         /* if microblock duration is 0, bypass the execle_ready_at check
     640             :            to avoid a potential cache miss.  Can't use an ifdef here
     641             :            because FD_UNLIKELY is a macro, but the compiler should
     642             :            eliminate the check easily. */
     643           0 :         ( (MICROBLOCK_DURATION_NS==0L) || (ctx->execle_ready_at[poll_cursor]<now) ) &&
     644           0 :         (fd_fseq_query( ctx->execle_current[poll_cursor] )==ctx->execle_expect[poll_cursor]) ) ) {
     645           0 :       *charge_busy = 1;
     646           0 :       ctx->execle_idle_bitset |= 1UL<<poll_cursor;
     647             : 
     648           0 :       long complete_duration = -fd_tickcount();
     649           0 :       int completed = fd_pack_microblock_complete( ctx->pack, (ulong)poll_cursor );
     650           0 :       complete_duration      += fd_tickcount();
     651           0 :       if( FD_LIKELY( completed ) ) fd_histf_sample( ctx->complete_duration, (ulong)complete_duration );
     652           0 :     }
     653             : 
     654           0 :     ctx->poll_cursor = poll_cursor;
     655           0 :   }
     656             : 
     657             : 
     658             :   /* If we time out on our slot, then stop being leader.  This can only
     659             :      happen in the first after_credit after a housekeeping. */
     660           0 :   if( FD_UNLIKELY( ctx->approx_wallclock_ns>=ctx->slot_end_ns && ctx->leader_slot!=ULONG_MAX ) ) {
     661           0 :     *charge_busy = 1;
     662             : 
     663           0 :     fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out_mem, ctx->poh_out_chunk );
     664           0 :     get_done_packing( ctx, done_packing, FD_PACK_END_SLOT_REASON_TIME ); /* needs to be called before fd_pack_end_block */
     665           0 :     fd_pack_end_block( ctx->pack );
     666           0 :     fd_pack_get_top_writers( ctx->pack, done_packing->limits_usage->top_writers ); /* needs to be called after fd_pack_end_block */
     667             : 
     668           0 :     fd_stem_publish( stem, 1UL, fd_disco_execle_sig( ctx->leader_slot, ctx->pack_idx ), ctx->poh_out_chunk, sizeof(fd_done_packing_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     669           0 :     ctx->poh_out_chunk = fd_dcache_compact_next( ctx->poh_out_chunk, sizeof(fd_done_packing_t), ctx->poh_out_chunk0, ctx->poh_out_wmark );
     670           0 :     ctx->pack_idx++;
     671             : 
     672           0 :     log_end_block_metrics( ctx, now, "time", done_packing->limits_usage->block_cost );
     673           0 :     ctx->drain_execle        = 1;
     674           0 :     ctx->leader_slot         = ULONG_MAX;
     675           0 :     ctx->slot_microblock_cnt = 0UL;
     676           0 :     remove_ib( ctx );
     677             : 
     678           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER,       0 );
     679           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_EXECLES,      0 );
     680           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS,  0 );
     681           0 :     return;
     682           0 :   }
     683             : 
     684             :   /* Am I leader? If not, see about inserting at most one transaction
     685             :      from extra storage.  It's important not to insert too many
     686             :      transactions here, or we won't end up servicing dedup_pack enough.
     687             :      If extra storage is empty or pack is full, do nothing. */
     688           0 :   if( FD_UNLIKELY( ctx->leader_slot==ULONG_MAX ) ) {
     689             : #if FD_PACK_USE_EXTRA_STORAGE
     690             :     if( FD_UNLIKELY( !extra_txn_deq_empty( ctx->extra_txn_deq ) &&
     691             :          fd_pack_avail_txn_cnt( ctx->pack )<ctx->max_pending_transactions ) ) {
     692             :       *charge_busy = 1;
     693             : 
     694             :       int result = insert_from_extra( ctx );
     695             :       if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
     696             :     }
     697             : #endif
     698           0 :     return;
     699           0 :   }
     700             : 
     701             :   /* Am I in drain mode?  If so, check if I can exit it */
     702           0 :   if( FD_UNLIKELY( ctx->drain_execle ) ) {
     703           0 :     if( FD_LIKELY( ctx->execle_idle_bitset==fd_ulong_mask_lsb( (int)execle_cnt ) ) ) {
     704           0 :       ctx->drain_execle = 0;
     705             : 
     706             :       /* Pack notifies poh when execle are drained so that poh can
     707             :          relinquish pack's ownership over the slot execle (by decrementing
     708             :          its Arc). We do this by sending a FD_PACK_MSG_DONE_DRAINING
     709             :          sig over the pack_poh mcache.
     710             : 
     711             :          TODO: This is only needed for Frankendancer, not Firedancer,
     712             :          which manages bank lifetime different. */
     713           0 :       fd_stem_publish( stem, 1UL, FD_PACK_MSG_DONE_DRAINING, 0UL, 0UL, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     714           0 :       *charge_busy = 1;
     715           0 :       return;
     716           0 :     } else {
     717           0 :       return;
     718           0 :     }
     719           0 :   }
     720             : 
     721           0 :   if( FD_UNLIKELY( ctx->pending_reduce_mb_bound ) ) {
     722           0 :     ctx->pending_reduce_mb_bound = 0;
     723           0 :     ulong * dst = fd_chunk_to_laddr( ctx->poh_out_mem, ctx->poh_out_chunk );
     724           0 :     *dst = ctx->slot_dynamic_max_microblocks;
     725           0 :     fd_stem_publish( stem, 1UL, FD_PACK_MSG_REDUCE_MB_BOUND, ctx->poh_out_chunk, sizeof(ulong), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     726           0 :     ctx->poh_out_chunk = fd_dcache_compact_next( ctx->poh_out_chunk, sizeof(ulong), ctx->poh_out_chunk0, ctx->poh_out_wmark );
     727           0 :     *charge_busy = 1;
     728           0 :     return;
     729           0 :   }
     730             : 
     731             :   /* Have I sent the max allowed microblocks? Nothing to do. */
     732           0 :   if( FD_UNLIKELY( ctx->slot_microblock_cnt>=ctx->slot_dynamic_max_microblocks ) ) return;
     733             : 
     734             :   /* Do I have enough transactions and/or have I waited enough time? */
     735             : #if !SMALL_MICROBLOCKS
     736             :   if( FD_UNLIKELY( (ulong)(now-ctx->last_successful_insert) <
     737             :         ctx->wait_duration_ticks[ fd_ulong_min( fd_pack_avail_txn_cnt( ctx->pack ), MAX_TXN_PER_MICROBLOCK ) ] ) ) {
     738             :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, 0 );
     739             :     return;
     740             :   }
     741             : #endif
     742             : 
     743           0 :   int any_ready     = 0;
     744           0 :   int any_scheduled = 0;
     745             : 
     746           0 :   *charge_busy = 1;
     747             : 
     748           0 :   if( FD_LIKELY( ctx->crank->enabled ) ) {
     749           0 :     block_builder_info_t const * top_meta = fd_pack_peek_bundle_meta( ctx->pack );
     750           0 :     if( FD_UNLIKELY( top_meta ) ) {
     751             :       /* Have bundles, in a reasonable state to crank. */
     752             : 
     753           0 :       fd_txn_e_t * _bundle[ 1UL ];
     754           0 :       fd_txn_e_t * const * bundle = fd_pack_insert_bundle_init( ctx->pack, _bundle, 1UL );
     755             : 
     756           0 :       ulong txn_sz = fd_bundle_crank_generate( ctx->crank->gen, ctx->crank->prev_config, top_meta->commission_pubkey,
     757           0 :           ctx->crank->identity_pubkey, ctx->crank->tip_receiver_owner, ctx->crank->epoch, top_meta->commission,
     758           0 :           bundle[0]->txnp->payload, TXN( bundle[0]->txnp ) );
     759             : 
     760           0 :       if( FD_LIKELY( txn_sz==0UL ) ) { /* Everything in good shape! */
     761           0 :         fd_pack_insert_bundle_cancel( ctx->pack, bundle, 1UL );
     762           0 :         fd_pack_set_initializer_bundles_ready( ctx->pack );
     763           0 :         ctx->crank->metrics[ 0 ]++; /* BUNDLE_CRANK_RESULT_NOT_NEEDED */
     764           0 :       }
     765           0 :       else if( FD_LIKELY( txn_sz<ULONG_MAX ) ) {
     766           0 :         bundle[0]->txnp->payload_sz  = (ushort)txn_sz;
     767           0 :         bundle[0]->txnp->source_tpu  = FD_TXN_M_TPU_SOURCE_BUNDLE;
     768           0 :         bundle[0]->txnp->source_ipv4 = 0; /* not applicable */
     769           0 :         bundle[0]->txnp->scheduler_arrival_time_nanos = ctx->approx_wallclock_ns + (long)((double)(fd_tickcount() - ctx->approx_tickcount) / ctx->ticks_per_ns);
     770           0 :         bundle[0]->txnp->first_seen_nanos = bundle[0]->txnp->scheduler_arrival_time_nanos;
     771           0 :         memcpy( bundle[0]->txnp->payload+TXN(bundle[0]->txnp)->recent_blockhash_off, ctx->crank->recent_blockhash, 32UL );
     772             : 
     773           0 :         fd_keyguard_client_sign( ctx->crank->keyguard_client, bundle[0]->txnp->payload+1UL,
     774           0 :             bundle[0]->txnp->payload+65UL, txn_sz-65UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
     775             : 
     776           0 :         memcpy( ctx->crank->last_sig, bundle[0]->txnp->payload+1UL, 64UL );
     777             : 
     778           0 :         ctx->crank->ib_inserted = 1;
     779           0 :         ulong deleted;
     780           0 :         int retval = fd_pack_insert_bundle_fini( ctx->pack, bundle, 1UL, ctx->leader_slot-1UL, 1, NULL, &deleted );
     781           0 :         FD_MCNT_INC( PACK, TXN_DELETED, deleted );
     782           0 :         ctx->insert_result[ retval + FD_PACK_INSERT_RETVAL_OFF ]++;
     783           0 :         if( FD_UNLIKELY( retval<0 ) ) {
     784           0 :           ctx->crank->metrics[ 3 ]++; /* BUNDLE_CRANK_RESULT_INSERTION_FAILED */
     785           0 :           FD_LOG_WARNING(( "inserting initializer bundle returned %i", retval ));
     786           0 :         } else {
     787             :           /* Update the cached copy of the on-chain state.  This seems a
     788             :              little dangerous, since we're updating it as if the bundle
     789             :              succeeded without knowing if that's true, but here's why
     790             :              it's safe:
     791             : 
     792             :              From now until we get the rebate call for this initializer
     793             :              bundle (which lets us know if it succeeded or failed), pack
     794             :              will be in [Pending] state, which means peek_bundle_meta
     795             :              will return NULL, so we won't read this state.
     796             : 
     797             :              Then, if the initializer bundle failed, we'll go into
     798             :              [Failed] IB state until the end of the block, which will
     799             :              cause top_meta to remain NULL so we don't read these values
     800             :              again.
     801             : 
     802             :              Otherwise, the initializer bundle succeeded, which means
     803             :              that these are the right values to use. */
     804           0 :           fd_bundle_crank_apply( ctx->crank->gen, ctx->crank->prev_config, top_meta->commission_pubkey,
     805           0 :                                  ctx->crank->tip_receiver_owner, ctx->crank->epoch, top_meta->commission );
     806           0 :           ctx->crank->metrics[ 1 ]++; /* BUNDLE_CRANK_RESULT_INSERTED */
     807           0 :         }
     808           0 :       } else {
     809             :         /* Already logged a warning in this case */
     810           0 :         fd_pack_insert_bundle_cancel( ctx->pack, bundle, 1UL );
     811           0 :         ctx->crank->metrics[ 2 ]++; /* BUNDLE_CRANK_RESULT_CREATION_FAILED' */
     812           0 :       }
     813           0 :     }
     814           0 :   }
     815             : 
     816             :   /* Try to schedule the next microblock. */
     817           0 :   if( FD_LIKELY( ctx->execle_idle_bitset ) ) { /* Optimize for schedule */
     818           0 :     any_ready = 1;
     819             : 
     820           0 :     int i = fd_ulong_find_lsb( ctx->execle_idle_bitset );
     821             : 
     822           0 :     int flags;
     823             : 
     824           0 :     switch( ctx->strategy ) {
     825           0 :       default:
     826           0 :       case FD_PACK_STRATEGY_PERF:
     827           0 :         flags = FD_PACK_SCHEDULE_VOTE | FD_PACK_SCHEDULE_BUNDLE | FD_PACK_SCHEDULE_TXN;
     828           0 :         break;
     829           0 :       case FD_PACK_STRATEGY_BALANCED:
     830             :         /* We want to exempt votes from pacing, so we always allow
     831             :            scheduling votes.  It doesn't really make much sense to pace
     832             :            bundles, because they get scheduled in FIFO order.  However,
     833             :            we keep pacing for normal transactions.  For example, if
     834             :            pacing_execle_cnt is 0, then pack won't schedule normal
     835             :            transactions to any execle tile. */
     836           0 :         flags = FD_PACK_SCHEDULE_VOTE | fd_int_if( i==0,                FD_PACK_SCHEDULE_BUNDLE, 0 )
     837           0 :                                       | fd_int_if( i<pacing_execle_cnt, FD_PACK_SCHEDULE_TXN,    0 );
     838           0 :         break;
     839           0 :     }
     840             : 
     841           0 :     fd_txn_e_t * microblock_dst = fd_chunk_to_laddr( ctx->execle_out_mem, ctx->execle_out_chunk );
     842           0 :     long schedule_duration = -fd_tickcount();
     843           0 :     ulong schedule_cnt = fd_pack_schedule_next_microblock( ctx->pack, CUS_PER_MICROBLOCK, VOTE_FRACTION, (ulong)i, flags, microblock_dst );
     844           0 :     schedule_duration      += fd_tickcount();
     845           0 :     fd_histf_sample( (schedule_cnt>0UL) ? ctx->schedule_duration : ctx->no_sched_duration, (ulong)schedule_duration );
     846             : 
     847           0 :     if( FD_LIKELY( schedule_cnt ) ) {
     848           0 :       any_scheduled = 1;
     849           0 :       long  now2   = fd_tickcount();
     850           0 :       ulong tsorig = (ulong)fd_frag_meta_ts_comp( now  ); /* A bound on when we observed execle was idle */
     851           0 :       ulong tspub  = (ulong)fd_frag_meta_ts_comp( now2 );
     852           0 :       ulong chunk  = ctx->execle_out_chunk;
     853           0 :       ulong msg_sz = schedule_cnt*sizeof(fd_txn_e_t);
     854           0 :       fd_microblock_execle_trailer_t * trailer = (fd_microblock_execle_trailer_t*)(microblock_dst+schedule_cnt);
     855           0 :       trailer->bank = ctx->leader_bank;
     856           0 :       trailer->bank_idx = ctx->leader_bank_idx;
     857           0 :       trailer->bank_seq = ctx->leader_bank_seq;
     858           0 :       trailer->microblock_idx = ctx->slot_microblock_cnt;
     859           0 :       trailer->pack_idx = ctx->pack_idx;
     860           0 :       trailer->pack_txn_idx = ctx->pack_txn_cnt;
     861           0 :       trailer->is_bundle = !!(microblock_dst->txnp->flags & FD_TXN_P_FLAGS_BUNDLE);
     862             : 
     863             :       /* When sending MAX_TXN_PER_MICROBLOCK transactions as fd_txn_e_t
     864             :          to execle, there must be room for the trailer at the end. */
     865           0 :       FD_STATIC_ASSERT( MAX_TXN_PER_MICROBLOCK*sizeof(fd_txn_e_t)+sizeof(fd_microblock_execle_trailer_t)<=MAX_MICROBLOCK_SZ, pack_execle_mtu );
     866             : 
     867           0 :       ulong sig = fd_disco_poh_sig( ctx->leader_slot, POH_PKT_TYPE_MICROBLOCK, (ulong)i );
     868           0 :       fd_stem_publish( stem, 0UL, sig, chunk, msg_sz+sizeof(fd_microblock_execle_trailer_t), 0UL, tsorig, tspub );
     869           0 :       ctx->execle_expect[ i ] = stem->seqs[0]-1UL;
     870           0 :       ctx->execle_ready_at[i] = now2 + (long)ctx->microblock_duration_ticks;
     871           0 :       ctx->execle_out_chunk = fd_dcache_compact_next( ctx->execle_out_chunk, msg_sz+sizeof(fd_microblock_execle_trailer_t), ctx->execle_out_chunk0, ctx->execle_out_wmark );
     872           0 :       ctx->slot_microblock_cnt += fd_ulong_if( trailer->is_bundle, schedule_cnt, 1UL );
     873           0 :       ctx->pack_idx += fd_uint_if( trailer->is_bundle, (uint)schedule_cnt, 1U );
     874           0 :       ctx->pack_txn_cnt += schedule_cnt;
     875             : 
     876           0 :       ctx->execle_idle_bitset = fd_ulong_pop_lsb( ctx->execle_idle_bitset );
     877           0 :       ctx->skip_cnt           = (long)schedule_cnt * fd_long_if( ctx->use_consumed_cus, (long)execle_cnt/2L, 1L );
     878           0 :       fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now2 );
     879             : 
     880           0 :       ctx->last_sched_metrics->time = now2;
     881           0 :       fd_pack_get_sched_metrics( ctx->pack, ctx->last_sched_metrics->sched_results );
     882             : 
     883             :       /* If we're using CU rebates, then we have one in for each execle
     884             :          in addition to the two normal ones.  We want to skip schedule
     885             :          attempts for (execle_cnt + 1) link polls after a successful
     886             :          schedule attempt. */
     887           0 :       fd_long_store_if( ctx->use_consumed_cus, &(ctx->skip_cnt), (long)(ctx->execle_cnt + 1) );
     888           0 :     }
     889           0 :   }
     890             : 
     891           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_EXECLES,     any_ready     );
     892           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, any_scheduled );
     893           0 :   now = fd_tickcount();
     894           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
     895             : 
     896             : #if FD_PACK_USE_EXTRA_STORAGE
     897             :   if( FD_UNLIKELY( !extra_txn_deq_empty( ctx->extra_txn_deq ) ) ) {
     898             :     /* Don't start pulling from the extra storage until the available
     899             :        transaction count drops below half. */
     900             :     ulong avail_space   = (ulong)fd_long_max( 0L, (long)(ctx->max_pending_transactions>>1)-(long)fd_pack_avail_txn_cnt( ctx->pack ) );
     901             :     ulong qty_to_insert = fd_ulong_min( 10UL, fd_ulong_min( extra_txn_deq_cnt( ctx->extra_txn_deq ), avail_space ) );
     902             :     int any_successes = 0;
     903             :     for( ulong i=0UL; i<qty_to_insert; i++ ) any_successes |= (0<=insert_from_extra( ctx ));
     904             :     if( FD_LIKELY( any_successes ) ) ctx->last_successful_insert = now;
     905             :   }
     906             : #endif
     907             : 
     908             :   /* Did we send the maximum allowed microblocks? Then end the slot. */
     909           0 :   if( FD_UNLIKELY( ctx->slot_microblock_cnt==ctx->slot_dynamic_max_microblocks )) {
     910           0 :     *charge_busy = 1;
     911             : 
     912           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER,       0 );
     913           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_EXECLES,      0 );
     914           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS,  0 );
     915             :     /* The pack object also does this accounting and increases this
     916             :        metric, but we end the slot early so won't see it unless we also
     917             :        increment it here. */
     918           0 :     FD_MCNT_INC( PACK, MICROBLOCK_PER_BLOCK_LIMIT_REACHED, 1UL );
     919             : 
     920           0 :     fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out_mem, ctx->poh_out_chunk );
     921           0 :     get_done_packing( ctx, done_packing, FD_PACK_END_SLOT_REASON_MICROBLOCK );
     922           0 :     fd_pack_end_block( ctx->pack );
     923           0 :     fd_pack_get_top_writers( ctx->pack, done_packing->limits_usage->top_writers );
     924             : 
     925           0 :     fd_stem_publish( stem, 1UL, fd_disco_execle_sig( ctx->leader_slot, ctx->pack_idx ), ctx->poh_out_chunk, sizeof(fd_done_packing_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     926           0 :     ctx->poh_out_chunk = fd_dcache_compact_next( ctx->poh_out_chunk, sizeof(fd_done_packing_t), ctx->poh_out_chunk0, ctx->poh_out_wmark );
     927           0 :     ctx->pack_idx++;
     928             : 
     929           0 :     log_end_block_metrics( ctx, now, "microblock", done_packing->limits_usage->block_cost );
     930           0 :     ctx->drain_execle        = 1;
     931           0 :     ctx->leader_slot         = ULONG_MAX;
     932           0 :     ctx->slot_microblock_cnt = 0UL;
     933           0 :     remove_ib( ctx );
     934             : 
     935           0 :     return;
     936           0 :   }
     937           0 : }
     938             : 
     939             : 
     940             : /* At this point, we have started receiving frag seq with details in
     941             :     mline at time now.  Speculatively process it here. */
     942             : 
     943             : static inline void
     944             : during_frag( fd_pack_ctx_t * ctx,
     945             :              ulong           in_idx,
     946             :              ulong           seq FD_PARAM_UNUSED,
     947             :              ulong           sig,
     948             :              ulong           chunk,
     949             :              ulong           sz,
     950           0 :              ulong           ctl FD_PARAM_UNUSED ) {
     951             : 
     952           0 :   uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     953             : 
     954           0 :   switch( ctx->in_kind[ in_idx ] ) {
     955           0 :   case IN_KIND_REPLAY: {
     956           0 :     if( FD_LIKELY( sig==REPLAY_SIG_TXN_EXECUTED ) ) {
     957           0 :       fd_replay_txn_executed_t const * txn_executed = fd_type_pun_const( dcache_entry );
     958           0 :       ctx->txn_committed = !!txn_executed->is_committable;
     959           0 :       if( FD_UNLIKELY( !txn_executed->is_committable ) ) return;
     960           0 :       memcpy( ctx->executed_txn_sig, fd_txn_get_signatures( TXN(txn_executed->txn), txn_executed->txn->payload ), FD_TXN_SIGNATURE_SZ );
     961           0 :       return;
     962           0 :     }
     963           0 :     if( FD_LIKELY( sig!=REPLAY_SIG_BECAME_LEADER ) ) return;
     964             : 
     965             :     /* There was a leader transition.  Handle it. */
     966           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(fd_became_leader_t) ) )
     967           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     968             : 
     969           0 :     fd_memcpy( ctx->_became_leader, dcache_entry, sizeof(fd_became_leader_t) );
     970           0 :     return;
     971           0 :   }
     972           0 :   case IN_KIND_POH: {
     973             :       /* Not interested in stamped microblocks, only leader updates. */
     974           0 :     if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
     975             : 
     976             :     /* There was a leader transition.  Handle it. */
     977           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(fd_became_leader_t) ) )
     978           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     979             : 
     980           0 :     fd_memcpy( ctx->_became_leader, dcache_entry, sizeof(fd_became_leader_t) );
     981           0 :     return;
     982           0 :   }
     983           0 :   case IN_KIND_EXECLE: {
     984           0 :     FD_TEST( ctx->use_consumed_cus );
     985             :       /* For a previous slot */
     986           0 :     if( FD_UNLIKELY( sig!=ctx->leader_slot ) ) return;
     987             : 
     988           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz<FD_PACK_REBATE_MIN_SZ
     989           0 :           || sz>FD_PACK_REBATE_MAX_SZ ) )
     990           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     991             : 
     992           0 :     ctx->pending_rebate_sz = sz;
     993           0 :     fd_memcpy( ctx->rebate, dcache_entry, sz );
     994           0 :     return;
     995           0 :   }
     996           0 :   case IN_KIND_RESOLV: {
     997           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_TPU_RESOLVED_MTU ) )
     998           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     999             : 
    1000           0 :     fd_txn_m_t * txnm = (fd_txn_m_t *)dcache_entry;
    1001           0 :     ulong payload_sz  = txnm->payload_sz;
    1002           0 :     ulong txn_t_sz    = txnm->txn_t_sz;
    1003           0 :     uint  source_ipv4 = txnm->source_ipv4;
    1004           0 :     uchar source_tpu  = txnm->source_tpu;
    1005           0 :     FD_TEST( payload_sz<=FD_TPU_MTU    );
    1006           0 :     FD_TEST( txn_t_sz  <=FD_TXN_MAX_SZ );
    1007           0 :     fd_txn_t * txn  = fd_txn_m_txn_t( txnm );
    1008             : 
    1009           0 :     ulong addr_table_sz = 32UL*txn->addr_table_adtl_cnt;
    1010           0 :     FD_TEST( addr_table_sz<=32UL*FD_TXN_ACCT_ADDR_MAX );
    1011             : 
    1012           0 :     if( FD_UNLIKELY( (ctx->leader_slot==ULONG_MAX) & (sig>ctx->highest_observed_slot) ) ) {
    1013             :       /* Using the resolv tile's knowledge of the current slot is a bit
    1014             :          of a hack, since we don't get any info if there are no
    1015             :          transactions and we're not leader.  We're actually in exactly
    1016             :          the case where that's okay though.  The point of calling
    1017             :          expire_before long before we become leader is so that we don't
    1018             :          drop new but low-fee-paying transactions when pack is clogged
    1019             :          with expired but high-fee-paying transactions.  That can only
    1020             :          happen if we are getting transactions. */
    1021           0 :       ctx->highest_observed_slot = sig;
    1022           0 :       ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->highest_observed_slot, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
    1023           0 :       FD_MCNT_INC( PACK, TXN_EXPIRED, exp_cnt );
    1024           0 :     }
    1025             : 
    1026             : 
    1027           0 :     ulong bundle_id = txnm->block_engine.bundle_id;
    1028           0 :     if( FD_UNLIKELY( bundle_id ) ) {
    1029           0 :       ctx->is_bundle = 1;
    1030           0 :       if( FD_LIKELY( bundle_id!=ctx->current_bundle->id ) ) {
    1031           0 :         if( FD_UNLIKELY( ctx->current_bundle->bundle ) ) {
    1032           0 :           FD_MCNT_INC( PACK, TXN_PARTIAL_BUNDLE, ctx->current_bundle->txn_received );
    1033           0 :           fd_pack_insert_bundle_cancel( ctx->pack, ctx->current_bundle->bundle, ctx->current_bundle->txn_cnt );
    1034           0 :         }
    1035           0 :         ctx->current_bundle->id                 = bundle_id;
    1036           0 :         ctx->current_bundle->txn_cnt            = txnm->block_engine.bundle_txn_cnt;
    1037           0 :         ctx->current_bundle->min_blockhash_slot = ULONG_MAX;
    1038           0 :         ctx->current_bundle->txn_received       = 0UL;
    1039             : 
    1040           0 :         if( FD_UNLIKELY( ctx->current_bundle->txn_cnt==0UL ) ) {
    1041           0 :           FD_MCNT_INC( PACK, TXN_PARTIAL_BUNDLE, 1UL );
    1042           0 :           ctx->current_bundle->id = 0UL;
    1043           0 :           return;
    1044           0 :         }
    1045           0 :         ctx->blk_engine_cfg->commission = txnm->block_engine.commission;
    1046           0 :         memcpy( ctx->blk_engine_cfg->commission_pubkey->b, txnm->block_engine.commission_pubkey, 32UL );
    1047             : 
    1048           0 :         ctx->current_bundle->bundle = fd_pack_insert_bundle_init( ctx->pack, ctx->current_bundle->_txn, ctx->current_bundle->txn_cnt );
    1049           0 :       }
    1050           0 :       ctx->cur_spot                           = ctx->current_bundle->bundle[ ctx->current_bundle->txn_received ];
    1051           0 :       ctx->current_bundle->min_blockhash_slot = fd_ulong_min( ctx->current_bundle->min_blockhash_slot, sig );
    1052           0 :     } else {
    1053           0 :       ctx->is_bundle = 0;
    1054             : #if FD_PACK_USE_EXTRA_STORAGE
    1055             :       if( FD_LIKELY( ctx->leader_slot!=ULONG_MAX || fd_pack_avail_txn_cnt( ctx->pack )<ctx->max_pending_transactions ) ) {
    1056             :         ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
    1057             :         ctx->insert_to_extra = 0;
    1058             :       } else {
    1059             :         if( FD_UNLIKELY( extra_txn_deq_full( ctx->extra_txn_deq ) ) ) {
    1060             :           extra_txn_deq_remove_head( ctx->extra_txn_deq );
    1061             :           FD_MCNT_INC( PACK, TXN_EXTRA_DROPPED, 1UL );
    1062             :         }
    1063             :         ctx->cur_spot = extra_txn_deq_peek_tail( extra_txn_deq_insert_tail( ctx->extra_txn_deq ) );
    1064             :         /* We want to store the current time in cur_spot so that we can
    1065             :            track its expiration better.  We just stash it in the CU
    1066             :            fields, since those aren't important right now. */
    1067             :         ctx->cur_spot->txnp->blockhash_slot = sig;
    1068             :         ctx->insert_to_extra                = 1;
    1069             :         FD_MCNT_INC( PACK, TXN_EXTRA_INSERTED, 1UL );
    1070             :       }
    1071             : #else
    1072           0 :       ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
    1073           0 : #endif
    1074           0 :     }
    1075             : 
    1076             :     /* We get transactions from the resolv tile.
    1077             :        The transactions should have been parsed and verified. */
    1078           0 :     FD_MCNT_INC( PACK, TXN_NORMAL_RX, 1UL );
    1079             : 
    1080           0 :     fd_memcpy( ctx->cur_spot->txnp->payload, fd_txn_m_payload( txnm ), payload_sz    );
    1081           0 :     fd_memcpy( TXN(ctx->cur_spot->txnp),     txn,                      txn_t_sz      );
    1082           0 :     fd_memcpy( ctx->cur_spot->alt_accts,     fd_txn_m_alut( txnm ),    addr_table_sz );
    1083           0 :     ctx->cur_spot->txnp->scheduler_arrival_time_nanos = ctx->approx_wallclock_ns + (long)((double)(fd_tickcount() - ctx->approx_tickcount) / ctx->ticks_per_ns);
    1084           0 :     ctx->cur_spot->txnp->first_seen_nanos = txnm->first_seen_nanos;
    1085           0 :     ctx->cur_spot->txnp->payload_sz  = payload_sz;
    1086           0 :     ctx->cur_spot->txnp->source_ipv4 = source_ipv4;
    1087           0 :     ctx->cur_spot->txnp->source_tpu  = source_tpu;
    1088             : 
    1089           0 :     break;
    1090           0 :   }
    1091           0 :   case IN_KIND_EXECUTED_TXN: {
    1092           0 :     FD_TEST( sz==64UL );
    1093           0 :     fd_memcpy( ctx->executed_txn_sig, dcache_entry, sz );
    1094           0 :     break;
    1095           0 :   }
    1096           0 :   }
    1097           0 : }
    1098             : 
    1099             : 
    1100             : /* After the transaction has been fully received, and we know we were
    1101             :    not overrun while reading it, insert it into pack. */
    1102             : 
    1103             : static inline void
    1104             : after_frag( fd_pack_ctx_t *     ctx,
    1105             :             ulong               in_idx,
    1106             :             ulong               seq,
    1107             :             ulong               sig,
    1108             :             ulong               sz,
    1109             :             ulong               tsorig,
    1110             :             ulong               tspub,
    1111           0 :             fd_stem_context_t * stem ) {
    1112           0 :   (void)seq;
    1113           0 :   (void)sz;
    1114           0 :   (void)tsorig;
    1115           0 :   (void)tspub;
    1116           0 :   (void)stem;
    1117             : 
    1118           0 :   long now = fd_tickcount();
    1119             : 
    1120           0 :   ulong leader_slot = ULONG_MAX;
    1121           0 :   switch( ctx->in_kind[ in_idx ] ) {
    1122           0 :     case IN_KIND_REPLAY:
    1123           0 :       if( FD_LIKELY( sig==REPLAY_SIG_TXN_EXECUTED && ctx->txn_committed ) ) {
    1124           0 :         ulong deleted = fd_pack_delete_transaction( ctx->pack, fd_type_pun( ctx->executed_txn_sig ) );
    1125           0 :         FD_MCNT_INC( PACK, TXN_ALREADY_EXECUTED, deleted );
    1126           0 :       }
    1127           0 :       if( FD_UNLIKELY( sig!=REPLAY_SIG_BECAME_LEADER ) ) return;
    1128           0 :       leader_slot = ctx->_became_leader->slot;
    1129             : 
    1130           0 :       ctx->start_block_sched_metrics->time = now;
    1131           0 :       fd_pack_get_sched_metrics( ctx->pack, ctx->start_block_sched_metrics->sched_results );
    1132           0 :       break;
    1133           0 :     case IN_KIND_POH:
    1134           0 :       if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
    1135           0 :       leader_slot = fd_disco_poh_sig_slot( sig );
    1136           0 :       break;
    1137           0 :     default:
    1138           0 :       break;
    1139           0 :   }
    1140             : 
    1141           0 :   switch( ctx->in_kind[ in_idx ] ) {
    1142           0 :   case IN_KIND_REPLAY:
    1143           0 :   case IN_KIND_POH: {
    1144           0 :     long now_ticks = fd_tickcount();
    1145           0 :     long now_ns    = fd_log_wallclock();
    1146             : 
    1147           0 :     if( FD_UNLIKELY( ctx->leader_slot!=ULONG_MAX ) ) {
    1148           0 :       fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out_mem, ctx->poh_out_chunk );
    1149           0 :       get_done_packing( ctx, done_packing, FD_PACK_END_SLOT_REASON_LEADER_SWITCH );
    1150           0 :       fd_pack_end_block( ctx->pack );
    1151           0 :       fd_pack_get_top_writers( ctx->pack, done_packing->limits_usage->top_writers );
    1152             : 
    1153           0 :       fd_stem_publish( stem, 1UL, fd_disco_execle_sig( ctx->leader_slot, ctx->pack_idx ), ctx->poh_out_chunk, sizeof(fd_done_packing_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
    1154           0 :       ctx->poh_out_chunk = fd_dcache_compact_next( ctx->poh_out_chunk, sizeof(fd_done_packing_t), ctx->poh_out_chunk0, ctx->poh_out_wmark );
    1155           0 :       ctx->pack_idx++;
    1156             : 
    1157           0 :       FD_LOG_WARNING(( "switching to slot %lu while packing for slot %lu. Draining execle tiles.", leader_slot, ctx->leader_slot ));
    1158           0 :       log_end_block_metrics( ctx, now_ticks, "switch", done_packing->limits_usage->block_cost );
    1159           0 :       ctx->drain_execle        = 1;
    1160           0 :       ctx->leader_slot         = ULONG_MAX;
    1161           0 :       ctx->slot_microblock_cnt = 0UL;
    1162           0 :       remove_ib( ctx );
    1163           0 :     }
    1164           0 :     ctx->leader_slot = leader_slot;
    1165             : 
    1166           0 :     ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->leader_slot, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
    1167           0 :     FD_MCNT_INC( PACK, TXN_EXPIRED, exp_cnt );
    1168             : 
    1169           0 :     ctx->leader_bank          = ctx->_became_leader->bank;
    1170           0 :     ctx->leader_bank_idx      = ctx->_became_leader->bank_idx;
    1171           0 :     ctx->leader_bank_seq      = ctx->_became_leader->bank_seq;
    1172           0 :     ctx->slot_max_microblocks = ctx->_became_leader->max_microblocks_in_slot;
    1173             : 
    1174           0 :     ulong base_max_data = ctx->larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK;
    1175           0 :     if( FD_LIKELY( !ctx->larger_shred_limits_per_block ) ) {
    1176             :       /* Compute base_max_data to ensure that we don't overflow
    1177             :          slot_max_data_shreds. See FD_SHRED_BATCH_BLOCK_DATA_SZ_MAX in
    1178             :          fd_shred_batch.h. Some of the terms are
    1179             :          based on the worst-case number of FEC sets in a block, which
    1180             :          scales with the slot time reductions:
    1181             :          - pad_ohead = per-batch padding (OHEAD_PAD in fd_shred_batch.h)
    1182             :          - hdr_ohead = per-batch header (OHEAD_HDR in fd_shred_batch.h)
    1183             :          - reg_ohead = only used for the last batch
    1184             :                        (OHEAD_REG in fd_shred_batch.h) */
    1185           0 :       ulong fec_set_cnt     = ctx->_became_leader->limits.slot_max_data_shreds/32UL;
    1186           0 :       ulong pad_ohead       = ( fec_set_cnt/2UL )*FD_SHREDDER_CHAINED_FEC_SET_PAYLOAD_SZ;
    1187           0 :       ulong hdr_ohead       = fec_set_cnt*8UL;
    1188           0 :       ulong reg_ohead       = 8192UL;
    1189           0 :       FD_TEST( fec_set_cnt >= 2UL ); /* guard against underflow */
    1190           0 :       ulong fec_data        = ( fec_set_cnt - 2UL )*FD_SHREDDER_CHAINED_FEC_SET_PAYLOAD_SZ;
    1191           0 :       FD_TEST( fec_data    >= pad_ohead+hdr_ohead+reg_ohead );
    1192           0 :       base_max_data         = fec_data - pad_ohead - hdr_ohead - reg_ohead;
    1193           0 :     }
    1194             :     /* Reserve some space in the block for ticks */
    1195           0 :     ctx->slot_max_data        = base_max_data
    1196           0 :                                       - 48UL*(ctx->_became_leader->ticks_per_slot+ctx->_became_leader->total_skipped_ticks);
    1197             : 
    1198           0 :     ctx->limits.slot_max_cost                     = ctx->_became_leader->limits.slot_max_cost;
    1199           0 :     ctx->limits.slot_max_vote_cost                = ctx->_became_leader->limits.slot_max_vote_cost;
    1200           0 :     ctx->limits.slot_max_write_cost_per_acct      = ctx->_became_leader->limits.slot_max_write_cost_per_acct;
    1201           0 :     ctx->limits.slot_max_allocated_data_per_block = ctx->_became_leader->limits.slot_max_allocated_data_per_block;
    1202           0 :     ctx->limits.slot_max_data_shreds              = ctx->_became_leader->limits.slot_max_data_shreds;
    1203             : 
    1204             :     /* ticks_per_ns is probably relatively stable over 400ms, but not
    1205             :        over several hours, so we need to compute the slot duration in
    1206             :        milliseconds first and then convert to ticks.  This doesn't need
    1207             :        to be super accurate, but we don't want it to vary wildly. */
    1208           0 :     long end_ticks = now_ticks + (long)((double)fd_long_max( ctx->_became_leader->slot_end_ns - now_ns, 1L )*ctx->ticks_per_ns);
    1209             :     /* We may still get overrun, but then we'll never use this and just
    1210             :        reinitialize it the next time when we actually become leader. */
    1211           0 :     fd_pack_pacing_init( ctx->pacer, now_ticks, end_ticks, (float)ctx->ticks_per_ns, ctx->limits.slot_max_cost );
    1212             : 
    1213           0 :     if( FD_UNLIKELY( ctx->crank->enabled ) ) {
    1214             :       /* If we get overrun, we'll just never use these values, but the
    1215             :          old values aren't really useful either. */
    1216           0 :       ctx->crank->epoch = ctx->_became_leader->epoch;
    1217           0 :       *(ctx->crank->prev_config) = *(ctx->_became_leader->bundle->config);
    1218           0 :       memcpy( ctx->crank->recent_blockhash,   ctx->_became_leader->bundle->last_blockhash,     32UL );
    1219           0 :       memcpy( ctx->crank->tip_receiver_owner, ctx->_became_leader->bundle->tip_receiver_owner, 32UL );
    1220           0 :     }
    1221             : 
    1222           0 :     FD_LOG_INFO(( "pack_became_leader(slot=%lu,ends_at=%ld)", ctx->leader_slot, ctx->_became_leader->slot_end_ns ));
    1223             : 
    1224           0 :     update_metric_state( ctx, fd_tickcount(), FD_PACK_METRIC_STATE_LEADER, 1 );
    1225             : 
    1226           0 :     ctx->slot_end_ns = ctx->_became_leader->slot_end_ns;
    1227           0 :     ctx->slot_dynamic_max_microblocks  = ctx->slot_max_microblocks;
    1228           0 :     ctx->pending_reduce_mb_bound       = 0;
    1229           0 :     fd_pack_limits_t limits[ 1 ];
    1230           0 :     limits->max_cost_per_block = ctx->limits.slot_max_cost;
    1231           0 :     limits->max_data_bytes_per_block = ctx->slot_max_data;
    1232           0 :     limits->max_microblocks_per_block = ctx->slot_max_microblocks;
    1233           0 :     limits->max_vote_cost_per_block = ctx->limits.slot_max_vote_cost;
    1234           0 :     limits->max_write_cost_per_acct = ctx->limits.slot_max_write_cost_per_acct;
    1235           0 :     limits->max_txn_per_microblock = ULONG_MAX; /* unused */
    1236           0 :     limits->max_allocated_data_per_block = ctx->limits.slot_max_allocated_data_per_block;
    1237           0 :     fd_pack_set_block_limits( ctx->pack, limits );
    1238           0 :     fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now );
    1239             : 
    1240           0 :     break;
    1241           0 :   }
    1242           0 :   case IN_KIND_EXECLE: {
    1243             :     /* For a previous slot */
    1244           0 :     if( FD_UNLIKELY( sig!=ctx->leader_slot ) ) return;
    1245             : 
    1246           0 :     fd_pack_rebate_cus( ctx->pack, ctx->rebate->rebate );
    1247           0 :     ctx->pending_rebate_sz = 0UL;
    1248           0 :     fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now );
    1249           0 :     break;
    1250           0 :   }
    1251           0 :   case IN_KIND_RESOLV: {
    1252             :     /* Normal transaction case */
    1253             : #if FD_PACK_USE_EXTRA_STORAGE
    1254             :     if( FD_LIKELY( !ctx->insert_to_extra ) ) {
    1255             : #else
    1256           0 :     if( 1 ) {
    1257           0 : #endif
    1258           0 :     if( FD_UNLIKELY( ctx->is_bundle ) ) {
    1259           0 :       if( FD_UNLIKELY( ctx->current_bundle->txn_cnt==0UL ) ) return;
    1260           0 :       if( FD_UNLIKELY( ++(ctx->current_bundle->txn_received)==ctx->current_bundle->txn_cnt ) ) {
    1261           0 :         ulong deleted;
    1262           0 :         long insert_duration = -fd_tickcount();
    1263           0 :         int result = fd_pack_insert_bundle_fini( ctx->pack, ctx->current_bundle->bundle, ctx->current_bundle->txn_cnt, ctx->current_bundle->min_blockhash_slot, 0, ctx->blk_engine_cfg, &deleted );
    1264           0 :         insert_duration      += fd_tickcount();
    1265           0 :         FD_MCNT_INC( PACK, TXN_DELETED, deleted );
    1266           0 :         ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ] += ctx->current_bundle->txn_received;
    1267           0 :         fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
    1268           0 :         ctx->current_bundle->bundle = NULL;
    1269           0 :       }
    1270           0 :     } else {
    1271           0 :       ulong blockhash_slot = sig;
    1272           0 :       ulong deleted;
    1273           0 :       long insert_duration = -fd_tickcount();
    1274           0 :       int result = fd_pack_insert_txn_fini( ctx->pack, ctx->cur_spot, blockhash_slot, &deleted );
    1275           0 :       insert_duration      += fd_tickcount();
    1276           0 :       FD_MCNT_INC( PACK, TXN_DELETED, deleted );
    1277           0 :       ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
    1278           0 :       fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
    1279           0 :       if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
    1280           0 :     }
    1281           0 :     }
    1282             : 
    1283           0 :     ctx->cur_spot = NULL;
    1284           0 :     break;
    1285           0 :   }
    1286           0 :   case IN_KIND_EXECUTED_TXN: {
    1287           0 :     ulong deleted = fd_pack_delete_transaction( ctx->pack, fd_type_pun( ctx->executed_txn_sig ) );
    1288           0 :     FD_MCNT_INC( PACK, TXN_ALREADY_EXECUTED, deleted );
    1289           0 :     break;
    1290           0 :   }
    1291           0 :   }
    1292             : 
    1293           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
    1294           0 : }
    1295             : 
    1296             : static void
    1297             : privileged_init( fd_topo_t const *      topo,
    1298           0 :                  fd_topo_tile_t const * tile ) {
    1299           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1300             : 
    1301           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1302           0 :   fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
    1303             : 
    1304           0 :   if( FD_UNLIKELY( !fd_rng_secure( &ctx->rng_seed, sizeof(uint) ) ) ) {
    1305           0 :     FD_LOG_CRIT(( "fd_rng_secure failed" ));
    1306           0 :   }
    1307           0 :   if( FD_UNLIKELY( !fd_rng_secure( &ctx->rng_idx, sizeof(ulong) ) ) ) {
    1308           0 :     FD_LOG_CRIT(( "fd_rng_secure failed" ));
    1309           0 :   }
    1310             : 
    1311           0 :   if( FD_LIKELY( !tile->pack.bundle.enabled ) ) return;
    1312           0 :   if( FD_UNLIKELY( !tile->pack.bundle.vote_account_path[0] ) ) {
    1313           0 :     FD_LOG_WARNING(( "Disabling bundle crank because no vote account was specified" ));
    1314           0 :     return;
    1315           0 :   }
    1316             : 
    1317           0 :   if( FD_UNLIKELY( !strcmp( tile->pack.bundle.identity_key_path, "" ) ) )
    1318           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
    1319             : 
    1320           0 :   const uchar * identity_key = fd_keyload_load( tile->pack.bundle.identity_key_path, /* pubkey only: */ 1 );
    1321           0 :   fd_memcpy( ctx->crank->identity_pubkey->b, identity_key, 32UL );
    1322             : 
    1323           0 :   if( FD_UNLIKELY( !fd_base58_decode_32( tile->pack.bundle.vote_account_path, ctx->crank->vote_pubkey->b ) ) ) {
    1324           0 :     const uchar * vote_key = fd_keyload_load( tile->pack.bundle.vote_account_path, /* pubkey only: */ 1 );
    1325           0 :     fd_memcpy( ctx->crank->vote_pubkey->b, vote_key, 32UL );
    1326           0 :   }
    1327           0 : }
    1328             : 
    1329             : static void
    1330             : unprivileged_init( fd_topo_t const *      topo,
    1331           0 :                    fd_topo_tile_t const * tile ) {
    1332           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1333             : 
    1334           0 :   if( FD_UNLIKELY( tile->pack.max_pending_transactions >= USHORT_MAX-10UL ) ) FD_LOG_ERR(( "pack tile supports up to %lu pending transactions", USHORT_MAX-11UL ));
    1335             : 
    1336           0 :   fd_pack_limits_t limits_upper[1] = {{
    1337           0 :     .max_cost_per_block           = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK_UPPER_BOUND,
    1338           0 :     .max_vote_cost_per_block      = FD_PACK_MAX_VOTE_COST_PER_BLOCK_UPPER_BOUND,
    1339           0 :     .max_write_cost_per_acct      = FD_PACK_MAX_WRITE_COST_PER_ACCT_UPPER_BOUND,
    1340           0 :     .max_data_bytes_per_block     = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
    1341           0 :     .max_txn_per_microblock       = EFFECTIVE_TXN_PER_MICROBLOCK,
    1342           0 :     .max_microblocks_per_block    = (ulong)UINT_MAX, /* Limit not known yet */
    1343           0 :     .max_allocated_data_per_block = FD_PACK_MAX_ALLOCATED_DATA_PER_BLOCK,
    1344           0 :   }};
    1345             : 
    1346           0 :   ulong pack_footprint = fd_pack_footprint( tile->pack.max_pending_transactions, BUNDLE_META_SZ, tile->pack.execle_tile_count, limits_upper );
    1347             : 
    1348           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1349           0 :   fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
    1350           0 :   fd_rng_t *      rng = fd_rng_join( fd_rng_new( FD_SCRATCH_ALLOC_APPEND( l, fd_rng_align(), fd_rng_footprint() ), ctx->rng_seed, ctx->rng_idx ) );
    1351           0 :   if( FD_UNLIKELY( !rng ) ) FD_LOG_ERR(( "fd_rng_new failed" ));
    1352             : 
    1353           0 :   fd_pack_limits_t limits_lower[1] = {{
    1354           0 :     .max_cost_per_block           = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK_LOWER_BOUND,
    1355           0 :     .max_vote_cost_per_block      = FD_PACK_MAX_VOTE_COST_PER_BLOCK_LOWER_BOUND,
    1356           0 :     .max_write_cost_per_acct      = FD_PACK_MAX_WRITE_COST_PER_ACCT_LOWER_BOUND,
    1357           0 :     .max_data_bytes_per_block     = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
    1358           0 :     .max_txn_per_microblock       = EFFECTIVE_TXN_PER_MICROBLOCK,
    1359           0 :     .max_microblocks_per_block    = (ulong)UINT_MAX, /* Limit not known yet */
    1360           0 :     .max_allocated_data_per_block = FD_PACK_MAX_ALLOCATED_DATA_PER_BLOCK,
    1361           0 :   }};
    1362             : 
    1363           0 :   ctx->pack = fd_pack_join( fd_pack_new( FD_SCRATCH_ALLOC_APPEND( l, fd_pack_align(), pack_footprint ),
    1364           0 :                                          tile->pack.max_pending_transactions, BUNDLE_META_SZ, tile->pack.execle_tile_count,
    1365           0 :                                          limits_upper,
    1366           0 :                                          fd_type_pun_const( tile->pack.acct_blocklist ), tile->pack.acct_blocklist_cnt,
    1367           0 :                                          rng ) );
    1368           0 :   if( FD_UNLIKELY( !ctx->pack ) ) FD_LOG_ERR(( "fd_pack_new failed" ));
    1369             : 
    1370           0 :   fd_pack_set_block_limits( ctx->pack, limits_lower );
    1371             : 
    1372           0 :   if( FD_UNLIKELY( tile->in_cnt>32UL ) ) FD_LOG_ERR(( "Too many input links (%lu>32) to pack tile", tile->in_cnt ));
    1373             : 
    1374           0 :   FD_TEST( tile->in_cnt<sizeof( ctx->in_kind )/sizeof( ctx->in_kind[0] ) );
    1375           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
    1376           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
    1377             : 
    1378           0 :     if( FD_LIKELY(      !strcmp( link->name, "resolv_pack"  ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
    1379           0 :     else if( FD_LIKELY( !strcmp( link->name, "resolh_pack"  ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
    1380           0 :     else if( FD_LIKELY( !strcmp( link->name, "poh_pack"     ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
    1381           0 :     else if( FD_LIKELY( !strcmp( link->name, "pohh_pack"    ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
    1382           0 :     else if( FD_LIKELY( !strcmp( link->name, "bank_pack"    ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE;
    1383           0 :     else if( FD_LIKELY( !strcmp( link->name, "execle_pack"  ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE;
    1384           0 :     else if( FD_LIKELY( !strcmp( link->name, "sign_pack"    ) ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
    1385           0 :     else if( FD_LIKELY( !strcmp( link->name, "replay_out"   ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY;
    1386           0 :     else if( FD_LIKELY( !strcmp( link->name, "executed_txn" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECUTED_TXN;
    1387           0 :     else FD_LOG_ERR(( "pack tile has unexpected input link %lu %s", i, link->name ));
    1388           0 :   }
    1389             : 
    1390           0 :   ulong execle_cnt = 0UL;
    1391           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
    1392           0 :     fd_topo_tile_t const * consumer_tile = &topo->tiles[ i ];
    1393           0 :     if( FD_UNLIKELY( strcmp( consumer_tile->name, "execle" ) && strcmp( consumer_tile->name, "replay" ) ) ) continue;
    1394           0 :     for( ulong j=0UL; j<consumer_tile->in_cnt; j++ ) {
    1395           0 :       if( FD_UNLIKELY( consumer_tile->in_link_id[ j ]==tile->out_link_id[ 0 ] ) ) execle_cnt++;
    1396           0 :     }
    1397           0 :   }
    1398             : 
    1399             :   // if( FD_UNLIKELY( !execle_cnt                            ) ) FD_LOG_ERR(( "pack tile connects to no execle tiles" ));
    1400           0 :   if( FD_UNLIKELY( execle_cnt>FD_PACK_MAX_EXECLE_TILES       ) ) FD_LOG_ERR(( "pack tile connects to too many execle tiles" ));
    1401             :   // if( FD_UNLIKELY( execle_cnt!=tile->pack.execle_tile_count ) ) FD_LOG_ERR(( "pack tile connects to %lu execle tiles, but tile->pack.execle_tile_count is %lu", execle_cnt, tile->pack.execle_tile_count ));
    1402             : 
    1403           0 :   FD_TEST( (tile->pack.schedule_strategy>=0) & (tile->pack.schedule_strategy<=FD_PACK_STRATEGY_BALANCED) );
    1404             : 
    1405           0 :   ctx->crank->enabled = tile->pack.bundle.enabled;
    1406           0 :   if( FD_UNLIKELY( tile->pack.bundle.enabled ) ) {
    1407           0 :     if( FD_UNLIKELY( !fd_bundle_crank_gen_init( ctx->crank->gen, (fd_acct_addr_t const *)tile->pack.bundle.tip_distribution_program_addr,
    1408           0 :             (fd_acct_addr_t const *)tile->pack.bundle.tip_payment_program_addr,
    1409           0 :             (fd_acct_addr_t const *)ctx->crank->vote_pubkey->b,
    1410           0 :             (fd_acct_addr_t const *)tile->pack.bundle.tip_distribution_authority,
    1411           0 :             schedule_strategy_strings[ tile->pack.schedule_strategy ],
    1412           0 :             tile->pack.bundle.commission_bps ) ) ) {
    1413           0 :       FD_LOG_ERR(( "constructing bundle generator failed" ));
    1414           0 :     }
    1415             : 
    1416           0 :     ulong sign_in_idx  = fd_topo_find_tile_in_link ( topo, tile, "sign_pack", tile->kind_id );
    1417           0 :     ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "pack_sign", tile->kind_id );
    1418           0 :     FD_TEST( sign_in_idx!=ULONG_MAX );
    1419           0 :     fd_topo_link_t const * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
    1420           0 :     fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
    1421           0 :     if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new( ctx->crank->keyguard_client,
    1422           0 :             sign_out->mcache,
    1423           0 :             sign_out->dcache,
    1424           0 :             sign_in->mcache,
    1425           0 :             sign_in->dcache,
    1426           0 :             sign_out->mtu ) ) ) ) {
    1427           0 :       FD_LOG_ERR(( "failed to construct keyguard" ));
    1428           0 :     }
    1429             :     /* Initialize enough of the prev config that it produces a
    1430             :        transaction */
    1431           0 :     ctx->crank->prev_config->discriminator       = 0x82ccfa1ee0aa0c9bUL;
    1432           0 :     ctx->crank->prev_config->tip_receiver->b[1]  = 1;
    1433           0 :     ctx->crank->prev_config->block_builder->b[2] = 1;
    1434             : 
    1435           0 :     memset( ctx->crank->tip_receiver_owner, '\0', 32UL );
    1436           0 :     memset( ctx->crank->recent_blockhash,   '\0', 32UL );
    1437           0 :     memset( ctx->crank->last_sig,           '\0', 64UL );
    1438           0 :     ctx->crank->ib_inserted    = 0;
    1439           0 :     ctx->crank->epoch          = 0UL;
    1440           0 :     ctx->crank->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
    1441           0 :     FD_TEST( ctx->crank->keyswitch );
    1442           0 :   } else {
    1443           0 :     memset( ctx->crank, '\0', sizeof(ctx->crank) );
    1444           0 :   }
    1445             : 
    1446             : 
    1447             : #if FD_PACK_USE_EXTRA_STORAGE
    1448             :   ctx->extra_txn_deq = extra_txn_deq_join( extra_txn_deq_new( FD_SCRATCH_ALLOC_APPEND( l, extra_txn_deq_align(),
    1449             :                                                                                           extra_txn_deq_footprint() ) ) );
    1450             : #endif
    1451             : 
    1452           0 :   ctx->cur_spot                      = NULL;
    1453           0 :   ctx->is_bundle                     = 0;
    1454           0 :   ctx->strategy                      = tile->pack.schedule_strategy;
    1455           0 :   ctx->max_pending_transactions      = tile->pack.max_pending_transactions;
    1456           0 :   ctx->leader_slot                   = ULONG_MAX;
    1457           0 :   ctx->leader_bank                   = NULL;
    1458           0 :   ctx->leader_bank_idx               = ULONG_MAX;
    1459           0 :   ctx->leader_bank_seq               = ULONG_MAX;
    1460           0 :   ctx->pack_idx                      = 0UL;
    1461           0 :   ctx->slot_microblock_cnt           = 0UL;
    1462           0 :   ctx->pack_txn_cnt                  = 0UL;
    1463           0 :   ctx->slot_max_microblocks          = 0UL;
    1464           0 :   ctx->slot_dynamic_max_microblocks  = 0UL;
    1465           0 :   ctx->pending_reduce_mb_bound       = 0;
    1466           0 :   ctx->slot_max_data                 = 0UL;
    1467           0 :   ctx->larger_shred_limits_per_block = tile->pack.larger_shred_limits_per_block;
    1468           0 :   ctx->drain_execle                  = 0;
    1469           0 :   ctx->approx_wallclock_ns           = fd_log_wallclock();
    1470           0 :   ctx->approx_tickcount              = fd_tickcount();
    1471           0 :   ctx->rng                           = rng;
    1472           0 :   ctx->ticks_per_ns                  = fd_tempo_tick_per_ns( NULL );
    1473           0 :   ctx->last_successful_insert        = 0L;
    1474           0 :   ctx->highest_observed_slot         = 0UL;
    1475           0 :   ctx->microblock_duration_ticks     = (ulong)(fd_tempo_tick_per_ns( NULL )*(double)MICROBLOCK_DURATION_NS  + 0.5);
    1476             : #if FD_PACK_USE_EXTRA_STORAGE
    1477             :   ctx->insert_to_extra               = 0;
    1478             : #endif
    1479           0 :   ctx->use_consumed_cus              = tile->pack.use_consumed_cus;
    1480           0 :   ctx->crank->enabled                = tile->pack.bundle.enabled;
    1481             : 
    1482             : #if !SMALL_MICROBLOCKS
    1483             :   ctx->wait_duration_ticks[ 0 ] = ULONG_MAX;
    1484             :   for( ulong i=1UL; i<MAX_TXN_PER_MICROBLOCK+1UL; i++ ) {
    1485             :     ctx->wait_duration_ticks[ i ]=(ulong)(fd_tempo_tick_per_ns( NULL )*(double)wait_duration[ i ] + 0.5);
    1486             :   }
    1487             : #endif
    1488             : 
    1489           0 :   ctx->limits.slot_max_cost                = limits_lower->max_cost_per_block;
    1490           0 :   ctx->limits.slot_max_vote_cost           = limits_lower->max_vote_cost_per_block;
    1491           0 :   ctx->limits.slot_max_write_cost_per_acct = limits_lower->max_write_cost_per_acct;
    1492             : 
    1493           0 :   ctx->execle_cnt       = tile->pack.execle_tile_count;
    1494           0 :   ctx->poll_cursor      = 0;
    1495           0 :   ctx->skip_cnt         = 0L;
    1496           0 :   ctx->execle_idle_bitset = fd_ulong_mask_lsb( (int)tile->pack.execle_tile_count );
    1497           0 :   for( ulong i=0UL; i<tile->pack.execle_tile_count; i++ ) {
    1498           0 :     ulong busy_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "execle_busy.%lu", i );
    1499           0 :     FD_TEST( busy_obj_id!=ULONG_MAX );
    1500           0 :     ctx->execle_current[ i ] = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
    1501           0 :     ctx->execle_expect[ i ] = ULONG_MAX;
    1502           0 :     if( FD_UNLIKELY( !ctx->execle_current[ i ] ) ) FD_LOG_ERR(( "execle tile %lu has no busy flag", i ));
    1503           0 :     ctx->execle_ready_at[ i ] = 0L;
    1504           0 :     FD_TEST( ULONG_MAX==fd_fseq_query( ctx->execle_current[ i ] ) );
    1505           0 :   }
    1506             : 
    1507           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
    1508           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
    1509           0 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
    1510             : 
    1511           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
    1512           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
    1513           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
    1514           0 :   }
    1515             : 
    1516           0 :   ctx->execle_out_mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
    1517           0 :   ctx->execle_out_chunk0 = fd_dcache_compact_chunk0( ctx->execle_out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
    1518           0 :   ctx->execle_out_wmark  = fd_dcache_compact_wmark ( ctx->execle_out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
    1519           0 :   ctx->execle_out_chunk  = ctx->execle_out_chunk0;
    1520             : 
    1521           0 :   ctx->poh_out_mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 1 ] ].dcache_obj_id ].wksp_id ].wksp;
    1522           0 :   ctx->poh_out_chunk0 = fd_dcache_compact_chunk0( ctx->poh_out_mem, topo->links[ tile->out_link_id[ 1 ] ].dcache );
    1523           0 :   ctx->poh_out_wmark  = fd_dcache_compact_wmark ( ctx->poh_out_mem, topo->links[ tile->out_link_id[ 1 ] ].dcache, topo->links[ tile->out_link_id[ 1 ] ].mtu );
    1524           0 :   ctx->poh_out_chunk  = ctx->poh_out_chunk0;
    1525             : 
    1526             :   /* Initialize metrics storage */
    1527           0 :   memset( ctx->insert_result, '\0', FD_PACK_INSERT_RETVAL_CNT * sizeof(ulong) );
    1528           0 :   fd_histf_join( fd_histf_new( ctx->schedule_duration, FD_MHIST_SECONDS_MIN( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS ),
    1529           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS ) ) );
    1530           0 :   fd_histf_join( fd_histf_new( ctx->no_sched_duration, FD_MHIST_SECONDS_MIN( PACK, NO_SCHEDULE_MICROBLOCK_DURATION_SECONDS ),
    1531           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, NO_SCHEDULE_MICROBLOCK_DURATION_SECONDS ) ) );
    1532           0 :   fd_histf_join( fd_histf_new( ctx->insert_duration,   FD_MHIST_SECONDS_MIN( PACK, INSERT_TRANSACTION_DURATION_SECONDS  ),
    1533           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, INSERT_TRANSACTION_DURATION_SECONDS  ) ) );
    1534           0 :   fd_histf_join( fd_histf_new( ctx->complete_duration, FD_MHIST_SECONDS_MIN( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS ),
    1535           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS ) ) );
    1536           0 :   ctx->metric_state = 0;
    1537           0 :   ctx->metric_state_begin = fd_tickcount();
    1538           0 :   memset( ctx->metric_timing,             '\0', 16*sizeof(long)                        );
    1539           0 :   memset( ctx->current_bundle,            '\0', sizeof(ctx->current_bundle)            );
    1540           0 :   memset( ctx->blk_engine_cfg,            '\0', sizeof(ctx->blk_engine_cfg)            );
    1541           0 :   memset( ctx->last_sched_metrics,        '\0', sizeof(ctx->last_sched_metrics)        );
    1542           0 :   memset( ctx->start_block_sched_metrics, '\0', sizeof(ctx->start_block_sched_metrics) );
    1543           0 :   memset( ctx->crank->metrics,            '\0', sizeof(ctx->crank->metrics)            );
    1544             : 
    1545           0 :   FD_LOG_INFO(( "packing microblocks of at most %lu transactions to %lu execle tiles using strategy %i", EFFECTIVE_TXN_PER_MICROBLOCK, tile->pack.execle_tile_count, ctx->strategy ));
    1546             : 
    1547           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
    1548           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
    1549           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
    1550           0 : }
    1551             : 
    1552             : static ulong
    1553             : populate_allowed_seccomp( fd_topo_t const *      topo,
    1554             :                           fd_topo_tile_t const * tile,
    1555             :                           ulong                  out_cnt,
    1556           0 :                           struct sock_filter *   out ) {
    1557           0 :   (void)topo;
    1558           0 :   (void)tile;
    1559             : 
    1560           0 :   populate_sock_filter_policy_fd_pack_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
    1561           0 :   return sock_filter_policy_fd_pack_tile_instr_cnt;
    1562           0 : }
    1563             : 
    1564             : static ulong
    1565             : populate_allowed_fds( fd_topo_t const *      topo,
    1566             :                       fd_topo_tile_t const * tile,
    1567             :                       ulong                  out_fds_cnt,
    1568           0 :                       int *                  out_fds ) {
    1569           0 :   (void)topo;
    1570           0 :   (void)tile;
    1571             : 
    1572           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
    1573             : 
    1574           0 :   ulong out_cnt = 0UL;
    1575           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
    1576           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
    1577           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
    1578           0 :   return out_cnt;
    1579           0 : }
    1580             : 
    1581             : /* Pack can publish a frag in the following scenarios:
    1582             : 
    1583             :    after_credit:
    1584             :      A. TIMED_OUT. Sets ctx->leader_slot=ULONG_MAX. return.
    1585             :      B. DONE_DRAINING. *after* DONE_PACKING. return.
    1586             :      C. REDUCE_MB_BOUND. return.
    1587             :      D. SCHEDULE_MB. *doesn't* return.
    1588             :      E. EXHAUST_MICROBLOCKS. Sets ctx->leader_slot=ULONG_MAX. return.
    1589             :    after_frag:
    1590             :          F. LEADER_SWITCH. Requires ctx->leader_slot!=ULONG_MAX
    1591             : 
    1592             :      It isn't possible to get a burst of 3, but a burst of 2 is possible
    1593             :      in these situations.
    1594             : 
    1595             :      C -> F
    1596             :      D -> F
    1597             :      D -> E
    1598             :  */
    1599           0 : #define STEM_BURST (2UL)
    1600             : 
    1601             : /* We want lazy (measured in ns) to be small enough that the producer
    1602             :     and the consumer never have to wait for credits.  For most tango
    1603             :     links, we use a default worst case speed coming from 100 Gbps
    1604             :     Ethernet.  That's not very suitable for microblocks that go from
    1605             :     pack to bank.  Instead we manually estimate the very aggressive
    1606             :     1000ns per microblock, and then reduce it further (in line with the
    1607             :     default lazy value computation) to ensure the random value chosen
    1608             :     based on this won't lead to credit return stalls. */
    1609           0 : #define STEM_LAZY  (128L*3000L)
    1610             : 
    1611           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_pack_ctx_t
    1612           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_pack_ctx_t)
    1613             : 
    1614           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
    1615           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
    1616           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
    1617           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
    1618           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
    1619           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
    1620             : 
    1621             : #include "../stem/fd_stem.c"
    1622             : 
    1623             : fd_topo_run_tile_t fd_tile_pack = {
    1624             :   .name                     = "pack",
    1625             :   .populate_allowed_seccomp = populate_allowed_seccomp,
    1626             :   .populate_allowed_fds     = populate_allowed_fds,
    1627             :   .scratch_align            = scratch_align,
    1628             :   .scratch_footprint        = scratch_footprint,
    1629             :   .privileged_init          = privileged_init,
    1630             :   .unprivileged_init        = unprivileged_init,
    1631             :   .run                      = stem_run,
    1632             : };

Generated by: LCOV version 1.14