LCOV - code coverage report
Current view: top level - disco/pack - fd_pack_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 814 0.0 %
Date: 2026-10-22 18:10:43 Functions: 0 19 0.0 %

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

Generated by: LCOV version 1.14