LCOV - code coverage report
Current view: top level - app/fdctl/run/tiles - fd_pack.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 22 554 4.0 %
Date: 2025-03-10 12:29:05 Functions: 2 15 13.3 %

          Line data    Source code
       1             : #include "../../../../disco/tiles.h"
       2             : 
       3             : #include "generated/pack_seccomp.h"
       4             : 
       5             : #include "../../../../disco/topo/fd_pod_format.h"
       6             : #include "../../../../disco/keyguard/fd_keyload.h"
       7             : #include "../../../../disco/keyguard/fd_keyswitch.h"
       8             : #include "../../../../disco/keyguard/fd_keyguard.h"
       9             : #include "../../../../disco/shred/fd_shredder.h"
      10             : #include "../../../../disco/metrics/fd_metrics.h"
      11             : #include "../../../../disco/pack/fd_pack.h"
      12             : #include "../../../../disco/pack/fd_pack_pacing.h"
      13             : 
      14             : #include "../../../../ballet/base64/fd_base64.h"
      15             : 
      16             : #include <linux/unistd.h>
      17             : 
      18             : /* fd_pack is responsible for taking verified transactions, and
      19             :    arranging them into "microblocks" (groups) of transactions to
      20             :    be executed serially.  It can try to do clever things so that
      21             :    multiple microblocks can execute in parallel, if they don't
      22             :    write to the same accounts. */
      23             : 
      24           0 : #define IN_KIND_RESOLV (0UL)
      25           0 : #define IN_KIND_POH    (1UL)
      26           0 : #define IN_KIND_BANK   (2UL)
      27           0 : #define IN_KIND_SIGN   (3UL)
      28             : 
      29             : #define MAX_SLOTS_PER_EPOCH          432000UL
      30             : 
      31             : /* Pace microblocks, but only slightly.  This helps keep performance
      32             :    more stable.  This limit is 2,000 microblocks/second/bank.  At 31
      33             :    transactions/microblock, that's 62k txn/sec/bank. */
      34           0 : #define MICROBLOCK_DURATION_NS  (0L)
      35             : 
      36             : /* There are 151 accepted blockhashes, but those don't include skips.
      37             :    This check is neither precise nor accurate, but just good enough.
      38             :    The bank tile does the final check.  We give a little margin for a
      39             :    few percent skip rate. */
      40           0 : #define TRANSACTION_LIFETIME_SLOTS 160UL
      41             : 
      42             : /* About 6 kB on the stack */
      43             : #define FD_PACK_PACK_MAX_OUT FD_PACK_MAX_BANK_TILES
      44             : 
      45             : /* Time is normally a long, but pack expects a ulong.  Add -LONG_MIN to
      46             :    the time values so that LONG_MIN maps to 0, LONG_MAX maps to
      47             :    ULONG_MAX, and everything in between maps linearly with a slope of 1.
      48             :    Just subtracting LONG_MIN results in signed integer overflow, which
      49             :    is U.B. */
      50             : #define TIME_OFFSET 0x8000000000000000UL
      51             : FD_STATIC_ASSERT( (ulong)LONG_MIN+TIME_OFFSET==0UL,       time_offset );
      52             : FD_STATIC_ASSERT( (ulong)LONG_MAX+TIME_OFFSET==ULONG_MAX, time_offset );
      53             : 
      54             : 
      55             : /* Optionally allow a larger limit for benchmarking */
      56           0 : #define LARGER_MAX_COST_PER_BLOCK (18UL*48000000UL)
      57             : 
      58             : /* 1.5 M cost units, enough for 1 max size transaction */
      59             : const ulong CUS_PER_MICROBLOCK = 1500000UL;
      60             : 
      61             : #define SMALL_MICROBLOCKS 1
      62             : 
      63             : #if SMALL_MICROBLOCKS
      64             : const float VOTE_FRACTION = 1.0f; /* schedule all available votes first */
      65           3 : #define EFFECTIVE_TXN_PER_MICROBLOCK 1UL
      66             : #else
      67             : const float VOTE_FRACTION = 0.75f; /* TODO: Is this the right value? */
      68             : #define EFFECTIVE_TXN_PER_MICROBLOCK MAX_TXN_PER_MICROBLOCK
      69             : #endif
      70             : 
      71             : /* There's overhead associated with each microblock the bank tile tries
      72             :    to execute it, so the optimal strategy is not to produce a microblock
      73             :    with a single transaction as soon as we receive it.  Basically, if we
      74             :    have less than 31 transactions, we want to wait a little to see if we
      75             :    receive additional transactions before we schedule a microblock.  We
      76             :    can model the optimum amount of time to wait, but the equation is
      77             :    complicated enough that we want to compute it before compile time.
      78             :    wait_duration[i] for i in [0, 31] gives the time in nanoseconds pack
      79             :    should wait after receiving its most recent transaction before
      80             :    scheduling if it has i transactions available.  Unsurprisingly,
      81             :    wait_duration[31] is 0.  wait_duration[0] is ULONG_MAX, so we'll
      82             :    always wait if we have 0 transactions. */
      83             : FD_IMPORT( wait_duration, "src/disco/pack/pack_delay.bin", ulong, 6, "" );
      84             : 
      85             : 
      86             : 
      87             : #if FD_PACK_USE_EXTRA_STORAGE
      88             : /* When we are done being leader for a slot and we are leader in the
      89             :    very next slot, it can still take some time to transition.  This is
      90             :    because the bank has to be finalized, a hash calculated, and various
      91             :    other things done in the replay stage to create the new child bank.
      92             : 
      93             :    During that time, pack cannot send transactions to banks so it needs
      94             :    to be able to buffer.  Typically, these so called "leader
      95             :    transitions" are short (<15 millis), so a low value here would
      96             :    suffice.  However, in some cases when there is memory pressure on the
      97             :    NUMA node or when the operating system context switches relevant
      98             :    threads out, it can take significantly longer.
      99             : 
     100             :    To prevent drops in these cases and because we assume banks are fast
     101             :    enough to drain this buffer once we do become leader, we set this
     102             :    buffer size to be quite large. */
     103             : 
     104             : #define DEQUE_NAME extra_txn_deq
     105             : #define DEQUE_T    fd_txn_e_t
     106             : #define DEQUE_MAX  (128UL*1024UL)
     107             : #include "../../../../util/tmpl/fd_deque.c"
     108             : 
     109             : #endif
     110             : 
     111             : typedef struct {
     112             :   fd_acct_addr_t commission_pubkey[1];
     113             :   ulong          commission;
     114             : } block_builder_info_t;
     115             : 
     116             : typedef struct {
     117             :   fd_wksp_t * mem;
     118             :   ulong       chunk0;
     119             :   ulong       wmark;
     120             : } fd_pack_in_ctx_t;
     121             : 
     122             : typedef struct {
     123             :   fd_pack_t *  pack;
     124             :   fd_txn_e_t * cur_spot;
     125             :   int          is_bundle; /* is the current transaction a bundle */
     126             : 
     127             :   /* The value passed to fd_pack_new, etc. */
     128             :   ulong    max_pending_transactions;
     129             : 
     130             :   /* The leader slot we are currently packing for, or ULONG_MAX if we
     131             :      are not the leader. */
     132             :   ulong  leader_slot;
     133             :   void const * leader_bank;
     134             : 
     135             :   /* The number of microblocks we have packed for the current leader
     136             :      slot.  Will always be <= slot_max_microblocks.  We must track
     137             :      this so that when we are done we can tell the PoH tile how many
     138             :      microblocks to expect in the slot. */
     139             :   ulong slot_microblock_cnt;
     140             : 
     141             :   /* The maximum number of microblocks that can be packed in this slot.
     142             :      Provided by the PoH tile when we become leader.*/
     143             :   ulong slot_max_microblocks;
     144             : 
     145             :   /* Cap (in bytes) of the amount of transaction data we produce in each
     146             :      block to avoid hitting the shred limits.  See where this is set for
     147             :      more explanation. */
     148             :   ulong slot_max_data;
     149             :   int   larger_shred_limits_per_block;
     150             : 
     151             :   /* Cost limit (in cost units) for each block.  Typically
     152             :      FD_PACK_MAX_COST_PER_BLOCK or LARDER_MAX_COST_PER_BLOCK. */
     153             :   ulong slot_max_cost;
     154             : 
     155             :   /* If drain_banks is non-zero, then the pack tile must wait until all
     156             :      banks 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_banks;
     162             : 
     163             :   /* Updated during housekeeping and used only for checking if the
     164             :      leader slot has ended.  Might be off by one housekeeping duration,
     165             :      but that should be small relative to a slot duration. */
     166             :   long  approx_wallclock_ns;
     167             : 
     168             :   fd_rng_t * rng;
     169             : 
     170             :   /* The end wallclock time of the leader slot we are currently packing
     171             :      for, if we are currently packing for a slot.
     172             : 
     173             :      _slot_end_ns is used as a temporary between during_frag and
     174             :      after_frag in case the tile gets overrun. */
     175             :   long _slot_end_ns;
     176             :   long slot_end_ns;
     177             : 
     178             :   /* pacer and ticks_per_ns are used for pacing CUs through the slot,
     179             :      i.e. deciding when to schedule a microblock given the number of CUs
     180             :      that have been consumed so far.  pacer is an opaque pacing object,
     181             :      which is initialized when the pack tile is packing a slot.
     182             :      ticks_per_ns is the cached value from tempo. */
     183             :   fd_pack_pacing_t pacer[1];
     184             :   double           ticks_per_ns;
     185             : 
     186             :   /* last_successful_insert stores the tickcount of the last
     187             :      successful transaction insert. */
     188             :   long last_successful_insert;
     189             : 
     190             :   /* highest_observed_slot stores the highest slot number we've seen
     191             :      from any transaction coming from the resolv tile.  When this
     192             :      increases, we expire old transactions. */
     193             :   ulong highest_observed_slot;
     194             : 
     195             :   /* microblock_duration_ns, and wait_duration
     196             :      respectively scaled to be in ticks instead of nanoseconds */
     197             :   ulong microblock_duration_ticks;
     198             :   ulong wait_duration_ticks[ MAX_TXN_PER_MICROBLOCK+1UL ];
     199             : 
     200             : #if FD_PACK_USE_EXTRA_STORAGE
     201             :   /* In addition to the available transactions that pack knows about, we
     202             :      also store a larger ring buffer for handling cases when pack is
     203             :      full.  This is an fd_deque. */
     204             :   fd_txn_e_t * extra_txn_deq;
     205             :   int          insert_to_extra; /* whether the last insert was into pack or the extra deq */
     206             : #endif
     207             : 
     208             :   fd_pack_in_ctx_t in[ 32 ];
     209             :   int              in_kind[ 32 ];
     210             : 
     211             :   ulong    bank_cnt;
     212             :   ulong    bank_idle_bitset; /* bit i is 1 if we've observed *bank_current[i]==bank_expect[i] */
     213             :   int      poll_cursor; /* in [0, bank_cnt), the next bank to poll */
     214             :   int      use_consumed_cus;
     215             :   long     skip_cnt;
     216             :   ulong *  bank_current[ FD_PACK_PACK_MAX_OUT ];
     217             :   ulong    bank_expect[ FD_PACK_PACK_MAX_OUT  ];
     218             :   /* bank_ready_at[x] means don't check bank x until tickcount is at
     219             :      least bank_ready_at[x]. */
     220             :   long     bank_ready_at[ FD_PACK_PACK_MAX_OUT  ];
     221             : 
     222             :   fd_wksp_t * out_mem;
     223             :   ulong       out_chunk0;
     224             :   ulong       out_wmark;
     225             :   ulong       out_chunk;
     226             : 
     227             :   ulong      insert_result[ FD_PACK_INSERT_RETVAL_CNT ];
     228             :   fd_histf_t schedule_duration[ 1 ];
     229             :   fd_histf_t no_sched_duration[ 1 ];
     230             :   fd_histf_t insert_duration  [ 1 ];
     231             :   fd_histf_t complete_duration[ 1 ];
     232             : 
     233             :   struct {
     234             :     uint metric_state;
     235             :     long metric_state_begin;
     236             :     long metric_timing[ 16 ];
     237             :   };
     238             : 
     239             :   struct {
     240             :     long time;
     241             :     ulong all[ FD_METRICS_TOTAL_SZ ];
     242             :   } last_sched_metrics[1];
     243             : 
     244             :   struct {
     245             :     ulong id;
     246             :     ulong txn_cnt;
     247             :     ulong txn_received;
     248             :     ulong min_blockhash_slot;
     249             :     fd_txn_e_t * _txn[ FD_PACK_MAX_TXN_PER_BUNDLE ];
     250             :     fd_txn_e_t * const * bundle; /* points to _txn when non-NULL */
     251             :   } current_bundle[1];
     252             : 
     253             :   block_builder_info_t blk_engine_cfg[1];
     254             : 
     255             :   struct {
     256             :     int                   enabled;
     257             :     int                   ib_inserted; /* in this slot */
     258             :     fd_acct_addr_t        vote_pubkey[1];
     259             :     fd_acct_addr_t        identity_pubkey[1];
     260             :     fd_bundle_crank_gen_t gen[1];
     261             :     fd_acct_addr_t        tip_receiver_owner[1];
     262             :     ulong                 epoch;
     263             :     fd_bundle_crank_tip_payment_config_t prev_config[1]; /* as of start of slot, then updated */
     264             :     uchar                 recent_blockhash[32];
     265             :     fd_ed25519_sig_t      last_sig[1];
     266             : 
     267             :     fd_keyswitch_t *      keyswitch;
     268             :     fd_keyguard_client_t  keyguard_client[1];
     269             : 
     270             :     ulong                 metrics[4];
     271             :   } crank[1];
     272             : 
     273             : 
     274             :   /* Used between during_frag and after_frag */
     275             :   ulong pending_rebate_cnt;
     276             :   fd_txn_p_t pending_rebate[ MAX_TXN_PER_MICROBLOCK ]; /* indexed [0, pending_rebate_cnt) */
     277             : } fd_pack_ctx_t;
     278             : 
     279           0 : #define BUNDLE_META_SZ 40UL
     280             : FD_STATIC_ASSERT( sizeof(block_builder_info_t)==BUNDLE_META_SZ, blk_engine_cfg );
     281             : 
     282           0 : #define FD_PACK_METRIC_STATE_TRANSACTIONS 0
     283           0 : #define FD_PACK_METRIC_STATE_BANKS        1
     284           0 : #define FD_PACK_METRIC_STATE_LEADER       2
     285           0 : #define FD_PACK_METRIC_STATE_MICROBLOCKS  3
     286             : 
     287             : /* Updates one component of the metric state.  If the state has changed,
     288             :    records the change. */
     289             : static inline void
     290             : update_metric_state( fd_pack_ctx_t * ctx,
     291             :                      long            effective_as_of,
     292             :                      int             type,
     293           0 :                      int             status ) {
     294           0 :   uint current_state = fd_uint_insert_bit( ctx->metric_state, type, status );
     295           0 :   if( FD_UNLIKELY( current_state!=ctx->metric_state ) ) {
     296           0 :     ctx->metric_timing[ ctx->metric_state ] += effective_as_of - ctx->metric_state_begin;
     297           0 :     ctx->metric_state_begin = effective_as_of;
     298           0 :     ctx->metric_state = current_state;
     299           0 :   }
     300           0 : }
     301             : 
     302             : static inline void
     303           0 : remove_ib( fd_pack_ctx_t * ctx ) {
     304             :   /* It's likely the initializer bundle is long scheduled, but we want to
     305             :      try deleting it just in case. */
     306           0 :   if( FD_UNLIKELY( ctx->crank->enabled & ctx->crank->ib_inserted ) )
     307           0 :     fd_pack_delete_transaction( ctx->pack, (fd_ed25519_sig_t const *)ctx->crank->last_sig );
     308           0 :   ctx->crank->ib_inserted = 0;
     309           0 : }
     310             : 
     311             : 
     312             : FD_FN_CONST static inline ulong
     313           3 : scratch_align( void ) {
     314           3 :   return 4096UL;
     315           3 : }
     316             : 
     317             : FD_FN_PURE static inline ulong
     318           3 : scratch_footprint( fd_topo_tile_t const * tile ) {
     319           3 :   fd_pack_limits_t limits[1] = {{
     320           3 :     .max_cost_per_block        = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK,
     321           3 :     .max_vote_cost_per_block   = FD_PACK_MAX_VOTE_COST_PER_BLOCK,
     322           3 :     .max_write_cost_per_acct   = FD_PACK_MAX_WRITE_COST_PER_ACCT,
     323           3 :     .max_data_bytes_per_block  = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
     324           3 :     .max_txn_per_microblock    = EFFECTIVE_TXN_PER_MICROBLOCK,
     325           3 :     .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
     326           3 :   }};
     327             : 
     328           3 :   ulong l = FD_LAYOUT_INIT;
     329           3 :   l = FD_LAYOUT_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t )                                   );
     330           3 :   l = FD_LAYOUT_APPEND( l, fd_rng_align(),           fd_rng_footprint()                                        );
     331           3 :   l = FD_LAYOUT_APPEND( l, fd_pack_align(),          fd_pack_footprint( tile->pack.max_pending_transactions,
     332           3 :                                                                         BUNDLE_META_SZ,
     333           3 :                                                                         tile->pack.bank_tile_count,
     334           3 :                                                                         limits                               ) );
     335             : #if FD_PACK_USE_EXTRA_STORAGE
     336             :   l = FD_LAYOUT_APPEND( l, extra_txn_deq_align(),    extra_txn_deq_footprint()                                 );
     337             : #endif
     338           3 :   return FD_LAYOUT_FINI( l, scratch_align() );
     339           3 : }
     340             : 
     341             : static inline void
     342             : log_end_block_metrics( fd_pack_ctx_t * ctx,
     343             :                        long            now,
     344           0 :                        char const    * reason ) {
     345           0 : #define DELTA( m ) (fd_metrics_tl[ MIDX(COUNTER, PACK, TRANSACTION_SCHEDULE_##m) ] - ctx->last_sched_metrics->all[ MIDX(COUNTER, PACK, TRANSACTION_SCHEDULE_##m) ])
     346           0 : #define AVAIL( m ) (fd_metrics_tl[ MIDX(GAUGE, PACK, AVAILABLE_TRANSACTIONS_##m) ])
     347           0 :     FD_LOG_INFO(( "pack_end_block(slot=%lu,%s,%lx,ticks_since_last_schedule=%ld,reasons=%lu,%lu,%lu,%lu,%lu,%lu,%lu;remaining=%lu+%lu+%lu+%lu;smallest=%lu;cus=%lu->%lu)",
     348           0 :           ctx->leader_slot, reason, ctx->bank_idle_bitset, now-ctx->last_sched_metrics->time,
     349           0 :           DELTA( TAKEN ), DELTA( CU_LIMIT ), DELTA( FAST_PATH ), DELTA( BYTE_LIMIT ), DELTA( WRITE_COST ), DELTA( SLOW_PATH ), DELTA( DEFER_SKIP ),
     350           0 :           AVAIL(REGULAR), AVAIL(VOTES), AVAIL(BUNDLES), AVAIL(CONFLICTING),
     351           0 :           (fd_metrics_tl[ MIDX(GAUGE, PACK, SMALLEST_PENDING_TRANSACTION) ]),
     352           0 :           (ctx->last_sched_metrics->all[ MIDX(GAUGE, PACK, CUS_CONSUMED_IN_BLOCK) ]),
     353           0 :           (fd_metrics_tl               [ MIDX(GAUGE, PACK, CUS_CONSUMED_IN_BLOCK) ])
     354           0 :     ));
     355           0 : #undef AVAIL
     356           0 : #undef DELTA
     357           0 : }
     358             : 
     359             : static inline void
     360           0 : metrics_write( fd_pack_ctx_t * ctx ) {
     361           0 :   FD_MCNT_ENUM_COPY( PACK, TRANSACTION_INSERTED,          ctx->insert_result  );
     362           0 :   FD_MCNT_ENUM_COPY( PACK, METRIC_TIMING,        ((ulong*)ctx->metric_timing) );
     363           0 :   FD_MCNT_ENUM_COPY( PACK, BUNDLE_CRANK_STATUS,           ctx->crank->metrics );
     364           0 :   FD_MHIST_COPY( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS, ctx->schedule_duration );
     365           0 :   FD_MHIST_COPY( PACK, NO_SCHED_MICROBLOCK_DURATION_SECONDS, ctx->no_sched_duration );
     366           0 :   FD_MHIST_COPY( PACK, INSERT_TRANSACTION_DURATION_SECONDS,  ctx->insert_duration   );
     367           0 :   FD_MHIST_COPY( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS, ctx->complete_duration );
     368             : 
     369           0 :   fd_pack_metrics_write( ctx->pack );
     370           0 : }
     371             : 
     372             : static inline void
     373           0 : during_housekeeping( fd_pack_ctx_t * ctx ) {
     374           0 :   ctx->approx_wallclock_ns = fd_log_wallclock();
     375             : 
     376           0 :   if( FD_UNLIKELY( ctx->crank->enabled && fd_keyswitch_state_query( ctx->crank->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
     377           0 :     fd_memcpy( ctx->crank->identity_pubkey, ctx->crank->keyswitch->bytes, 32UL );
     378           0 :     fd_keyswitch_state( ctx->crank->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
     379           0 :   }
     380           0 : }
     381             : 
     382             : static inline void
     383             : before_credit( fd_pack_ctx_t *     ctx,
     384             :                fd_stem_context_t * stem,
     385           0 :                int *               charge_busy ) {
     386           0 :   (void)stem;
     387             : 
     388           0 :   if( FD_UNLIKELY( (ctx->cur_spot!=NULL) & !ctx->is_bundle ) ) {
     389           0 :     *charge_busy = 1;
     390             : 
     391             :     /* If we were overrun while processing a frag from an in, then
     392             :        cur_spot is left dangling and not cleaned up, so clean it up here
     393             :        (by returning the slot to the pool of free slots).  If the last
     394             :        transaction was a bundle, then we don't want to return it.  When
     395             :        we try to process the first transaction in the next bundle, we'll
     396             :        see we never got the full bundle and cancel the whole last
     397             :        bundle, returning all the storage to the pool. */
     398             : #if FD_PACK_USE_EXTRA_STORAGE
     399             :     if( FD_LIKELY( !ctx->insert_to_extra ) ) fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
     400             :     else                                     extra_txn_deq_remove_tail( ctx->extra_txn_deq       );
     401             : #else
     402           0 :     fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
     403           0 : #endif
     404           0 :     ctx->cur_spot = NULL;
     405           0 :   }
     406           0 : }
     407             : 
     408             : #if FD_PACK_USE_EXTRA_STORAGE
     409             : /* insert_from_extra: helper method to pop the transaction at the head
     410             :    off the extra txn deque and insert it into pack.  Requires that
     411             :    ctx->extra_txn_deq is non-empty, but it's okay to call it if pack is
     412             :    full.  Returns the result of fd_pack_insert_txn_fini. */
     413             : static inline int
     414             : insert_from_extra( fd_pack_ctx_t * ctx ) {
     415             :   fd_txn_e_t       * spot       = fd_pack_insert_txn_init( ctx->pack );
     416             :   fd_txn_e_t const * insert     = extra_txn_deq_peek_head( ctx->extra_txn_deq );
     417             :   fd_txn_t   const * insert_txn = TXN(insert->txnp);
     418             :   fd_memcpy( spot->txnp->payload, insert->txnp->payload, insert->txnp->payload_sz                                                     );
     419             :   fd_memcpy( TXN(spot->txnp),     insert_txn,            fd_txn_footprint( insert_txn->instr_cnt, insert_txn->addr_table_lookup_cnt ) );
     420             :   fd_memcpy( spot->alt_accts,     insert->alt_accts,     insert_txn->addr_table_adtl_cnt*sizeof(fd_acct_addr_t)                       );
     421             :   spot->txnp->payload_sz = insert->txnp->payload_sz;
     422             :   extra_txn_deq_remove_head( ctx->extra_txn_deq );
     423             : 
     424             :   ulong blockhash_slot = insert->txnp->blockhash_slot;
     425             : 
     426             :   long insert_duration = -fd_tickcount();
     427             :   int result = fd_pack_insert_txn_fini( ctx->pack, spot, blockhash_slot );
     428             :   insert_duration      += fd_tickcount();
     429             :   ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
     430             :   fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
     431             :   FD_MCNT_INC( PACK, TRANSACTION_INSERTED_FROM_EXTRA, 1UL );
     432             :   return result;
     433             : }
     434             : #endif
     435             : 
     436             : static inline void
     437             : after_credit( fd_pack_ctx_t *     ctx,
     438             :               fd_stem_context_t * stem,
     439             :               int *               opt_poll_in,
     440           0 :               int *               charge_busy ) {
     441           0 :   (void)opt_poll_in;
     442             : 
     443           0 :   if( FD_UNLIKELY( (ctx->skip_cnt--)>0L ) ) return; /* It would take ages for this to hit LONG_MIN */
     444             : 
     445           0 :   long now = fd_tickcount();
     446             : 
     447           0 :   int pacing_bank_cnt = (int)fd_pack_pacing_enabled_bank_cnt( ctx->pacer, now );
     448           0 :   if( FD_UNLIKELY( !pacing_bank_cnt ) ) return;
     449             : 
     450           0 :   ulong bank_cnt = ctx->bank_cnt;
     451             : 
     452             :   /* If we're using CU rebates, then we have one in for each bank in
     453             :      addition to the two normal ones.  That means that after_credit will
     454             :      be called about (bank_cnt/2) times more frequently per transaction
     455             :      we receive. */
     456           0 :   fd_long_store_if( ctx->use_consumed_cus, &(ctx->skip_cnt), (long)(bank_cnt/2UL) );
     457             : 
     458             :   /* If any banks are busy, check one of the busy ones see if it is
     459             :      still busy. */
     460           0 :   if( FD_LIKELY( ctx->bank_idle_bitset!=fd_ulong_mask_lsb( (int)bank_cnt ) ) ) {
     461           0 :     int   poll_cursor = ctx->poll_cursor;
     462           0 :     ulong busy_bitset = (~ctx->bank_idle_bitset) & fd_ulong_mask_lsb( (int)bank_cnt );
     463             : 
     464             :     /* Suppose bank_cnt is 4 and idle_bitset looks something like this
     465             :        (pretending it's a uchar):
     466             :                 0000 1001
     467             :                        ^ busy cursor is 1
     468             :        Then busy_bitset is
     469             :                 0000 0110
     470             :        Rotate it right by 2 bits
     471             :                 1000 0001
     472             :        Find lsb returns 0, so busy cursor remains 2, and we poll bank 2.
     473             : 
     474             :        If instead idle_bitset were
     475             :                 0000 1110
     476             :                        ^
     477             :        The rotated version would be
     478             :                 0100 0000
     479             :        Find lsb will return 6, so busy cursor would be set to 0, and
     480             :        we'd poll bank 0, which is the right one. */
     481           0 :     poll_cursor++;
     482           0 :     poll_cursor = (poll_cursor + fd_ulong_find_lsb( fd_ulong_rotate_right( busy_bitset, (poll_cursor&63) ) )) & 63;
     483             : 
     484           0 :     if( FD_UNLIKELY(
     485             :         /* if microblock duration is 0, bypass the bank_ready_at check
     486             :            to avoid a potential cache miss.  Can't use an ifdef here
     487             :            because FD_UNLIKELY is a macro, but the compiler should
     488             :            eliminate the check easily. */
     489           0 :         ( (MICROBLOCK_DURATION_NS==0L) || (ctx->bank_ready_at[poll_cursor]<now) ) &&
     490           0 :         (fd_fseq_query( ctx->bank_current[poll_cursor] )==ctx->bank_expect[poll_cursor]) ) ) {
     491           0 :       *charge_busy = 1;
     492           0 :       ctx->bank_idle_bitset |= 1UL<<poll_cursor;
     493             : 
     494           0 :       long complete_duration = -fd_tickcount();
     495           0 :       int completed = fd_pack_microblock_complete( ctx->pack, (ulong)poll_cursor );
     496           0 :       complete_duration      += fd_tickcount();
     497           0 :       if( FD_LIKELY( completed ) ) fd_histf_sample( ctx->complete_duration, (ulong)complete_duration );
     498           0 :     }
     499             : 
     500           0 :     ctx->poll_cursor = poll_cursor;
     501           0 :   }
     502             : 
     503             : 
     504             :   /* If we time out on our slot, then stop being leader.  This can only
     505             :      happen in the first after_credit after a housekeeping. */
     506           0 :   if( FD_UNLIKELY( ctx->approx_wallclock_ns>=ctx->slot_end_ns && ctx->leader_slot!=ULONG_MAX ) ) {
     507           0 :     *charge_busy = 1;
     508             : 
     509           0 :     if( FD_UNLIKELY( ctx->slot_microblock_cnt<ctx->slot_max_microblocks )) {
     510             :       /* As an optimization, The PoH tile will automatically end a slot
     511             :          if it receives the maximum allowed microblocks, since it knows
     512             :          there is nothing left to receive.  In that case, we don't need
     513             :          to send a DONE_PACKING notification, since they are already on
     514             :          the next slot.  If we did send one it would just get dropped. */
     515           0 :       fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
     516           0 :       done_packing->microblocks_in_slot = ctx->slot_microblock_cnt;
     517             : 
     518           0 :       fd_stem_publish( stem, 0UL, fd_disco_poh_sig( ctx->leader_slot, POH_PKT_TYPE_DONE_PACKING, ULONG_MAX ), ctx->out_chunk, sizeof(fd_done_packing_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     519           0 :       ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, sizeof(fd_done_packing_t), ctx->out_chunk0, ctx->out_wmark );
     520           0 :     }
     521             : 
     522           0 :     log_end_block_metrics( ctx, now, "time" );
     523           0 :     ctx->drain_banks         = 1;
     524           0 :     ctx->leader_slot         = ULONG_MAX;
     525           0 :     ctx->slot_microblock_cnt = 0UL;
     526           0 :     fd_pack_end_block( ctx->pack );
     527           0 :     remove_ib( ctx );
     528             : 
     529           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER,       0 );
     530           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS,        0 );
     531           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS,  0 );
     532           0 :     return;
     533           0 :   }
     534             : 
     535             :   /* Am I leader? If not, see about inserting at most one transaction
     536             :      from extra storage.  It's important not to insert too many
     537             :      transactions here, or we won't end up servicing dedup_pack enough.
     538             :      If extra storage is empty or pack is full, do nothing. */
     539           0 :   if( FD_UNLIKELY( ctx->leader_slot==ULONG_MAX ) ) {
     540             : #if FD_PACK_USE_EXTRA_STORAGE
     541             :     if( FD_UNLIKELY( !extra_txn_deq_empty( ctx->extra_txn_deq ) &&
     542             :          fd_pack_avail_txn_cnt( ctx->pack )<ctx->max_pending_transactions ) ) {
     543             :       *charge_busy = 1;
     544             : 
     545             :       int result = insert_from_extra( ctx );
     546             :       if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
     547             :     }
     548             : #endif
     549           0 :     return;
     550           0 :   }
     551             : 
     552             :   /* Am I in drain mode?  If so, check if I can exit it */
     553           0 :   if( FD_UNLIKELY( ctx->drain_banks ) ) {
     554           0 :     if( FD_LIKELY( ctx->bank_idle_bitset==fd_ulong_mask_lsb( (int)bank_cnt ) ) ) ctx->drain_banks = 0;
     555           0 :     else                                                                         return;
     556           0 :   }
     557             : 
     558             :   /* Have I sent the max allowed microblocks? Nothing to do. */
     559           0 :   if( FD_UNLIKELY( ctx->slot_microblock_cnt>=ctx->slot_max_microblocks ) ) return;
     560             : 
     561             :   /* Do I have enough transactions and/or have I waited enough time? */
     562           0 :   if( FD_UNLIKELY( (ulong)(now-ctx->last_successful_insert) <
     563           0 :         ctx->wait_duration_ticks[ fd_ulong_min( fd_pack_avail_txn_cnt( ctx->pack ), MAX_TXN_PER_MICROBLOCK ) ] ) ) {
     564           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, 0 );
     565           0 :     return;
     566           0 :   }
     567             : 
     568           0 :   int any_ready     = 0;
     569           0 :   int any_scheduled = 0;
     570             : 
     571           0 :   *charge_busy = 1;
     572             : 
     573           0 :   if( FD_LIKELY( ctx->crank->enabled ) ) {
     574           0 :     block_builder_info_t const * top_meta = fd_pack_peek_bundle_meta( ctx->pack );
     575           0 :     if( FD_UNLIKELY( top_meta ) ) {
     576             :       /* Have bundles, in a reasonable state to crank. */
     577             : 
     578           0 :       fd_txn_e_t * _bundle[ 1UL ];
     579           0 :       fd_txn_e_t * const * bundle = fd_pack_insert_bundle_init( ctx->pack, _bundle, 1UL );
     580             : 
     581           0 :       ulong txn_sz = fd_bundle_crank_generate( ctx->crank->gen, ctx->crank->prev_config, top_meta->commission_pubkey,
     582           0 :           ctx->crank->identity_pubkey, ctx->crank->tip_receiver_owner, ctx->crank->epoch, top_meta->commission,
     583           0 :           bundle[0]->txnp->payload, TXN( bundle[0]->txnp ) );
     584             : 
     585           0 :       if( FD_LIKELY( txn_sz==0UL ) ) { /* Everything in good shape! */
     586           0 :         fd_pack_insert_bundle_cancel( ctx->pack, bundle, 1UL );
     587           0 :         fd_pack_set_initializer_bundles_ready( ctx->pack );
     588           0 :         ctx->crank->metrics[ 0 ]++;
     589           0 :       }
     590           0 :       else if( FD_LIKELY( txn_sz<ULONG_MAX ) ) {
     591           0 :         bundle[0]->txnp->payload_sz = (ushort)txn_sz;
     592           0 :         memcpy( bundle[0]->txnp->payload+TXN(bundle[0]->txnp)->recent_blockhash_off, ctx->crank->recent_blockhash, 32UL );
     593             : 
     594           0 :         fd_keyguard_client_sign( ctx->crank->keyguard_client, bundle[0]->txnp->payload+1UL,
     595           0 :             bundle[0]->txnp->payload+65UL, txn_sz-65UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
     596             : 
     597           0 :         memcpy( ctx->crank->last_sig, bundle[0]->txnp->payload+1UL, 64UL );
     598             : 
     599           0 :         ctx->crank->ib_inserted = 1;
     600           0 :         int retval = fd_pack_insert_bundle_fini( ctx->pack, bundle, 1UL, ctx->leader_slot-1UL, 1, NULL );
     601           0 :         ctx->insert_result[ retval + FD_PACK_INSERT_RETVAL_OFF ]++;
     602           0 :         if( FD_UNLIKELY( retval<0 ) ) {
     603           0 :           ctx->crank->metrics[ 3 ]++;
     604           0 :           FD_LOG_WARNING(( "inserting initializer bundle returned %i", retval ));
     605           0 :         } else {
     606             :           /* Update the cached copy of the on-chain state.  This seems a
     607             :              little dangerous, since we're updating it as if the bundle
     608             :              succeeded without knowing if that's true, but here's why
     609             :              it's safe:
     610             : 
     611             :              From now until we get the rebate call for this initializer
     612             :              bundle (which lets us know if it succeeded or failed), pack
     613             :              will be in [Pending] state, which means peek_bundle_meta
     614             :              will return NULL, so we won't read this state.
     615             : 
     616             :              Then, if the initializer bundle failed, we'll go into
     617             :              [Failed] IB state until the end of the block, which will
     618             :              cause top_meta to remain NULL so we don't read these values
     619             :              again.
     620             : 
     621             :              Otherwise, the initializer bundle succeeded, which means
     622             :              that these are the right values to use. */
     623           0 :           fd_bundle_crank_apply( ctx->crank->gen, ctx->crank->prev_config, top_meta->commission_pubkey,
     624           0 :                                  ctx->crank->tip_receiver_owner, ctx->crank->epoch, top_meta->commission );
     625           0 :           ctx->crank->metrics[ 1 ]++;
     626           0 :         }
     627           0 :       } else {
     628             :         /* Already logged a warning in this case */
     629           0 :         fd_pack_insert_bundle_cancel( ctx->pack, bundle, 1UL );
     630           0 :         ctx->crank->metrics[ 2 ]++;
     631           0 :       }
     632           0 :     }
     633           0 :   }
     634             : 
     635             :   /* Try to schedule the next microblock.  Do we have any idle bank
     636             :      tiles in the first `pacing_bank_cnt`? */
     637           0 :   if( FD_LIKELY( ctx->bank_idle_bitset & fd_ulong_mask_lsb( pacing_bank_cnt ) ) ) { /* Optimize for schedule */
     638           0 :     any_ready = 1;
     639             : 
     640           0 :     int i = fd_ulong_find_lsb( ctx->bank_idle_bitset );
     641             : 
     642           0 :     fd_txn_p_t * microblock_dst = fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
     643           0 :     long schedule_duration = -fd_tickcount();
     644           0 :     ulong schedule_cnt = fd_pack_schedule_next_microblock( ctx->pack, CUS_PER_MICROBLOCK, VOTE_FRACTION, (ulong)i, microblock_dst );
     645           0 :     schedule_duration      += fd_tickcount();
     646           0 :     fd_histf_sample( (schedule_cnt>0UL) ? ctx->schedule_duration : ctx->no_sched_duration, (ulong)schedule_duration );
     647             : 
     648           0 :     if( FD_LIKELY( schedule_cnt ) ) {
     649           0 :       any_scheduled = 1;
     650           0 :       long  now2   = fd_tickcount();
     651           0 :       ulong tsorig = (ulong)fd_frag_meta_ts_comp( now  ); /* A bound on when we observed bank was idle */
     652           0 :       ulong tspub  = (ulong)fd_frag_meta_ts_comp( now2 );
     653           0 :       ulong chunk  = ctx->out_chunk;
     654           0 :       ulong msg_sz = schedule_cnt*sizeof(fd_txn_p_t);
     655           0 :       fd_microblock_bank_trailer_t * trailer = (fd_microblock_bank_trailer_t*)(microblock_dst+schedule_cnt);
     656           0 :       trailer->bank = ctx->leader_bank;
     657           0 :       trailer->microblock_idx = ctx->slot_microblock_cnt;
     658           0 :       trailer->is_bundle = !!(microblock_dst->flags & FD_TXN_P_FLAGS_BUNDLE);
     659             : 
     660           0 :       ulong sig = fd_disco_poh_sig( ctx->leader_slot, POH_PKT_TYPE_MICROBLOCK, (ulong)i );
     661           0 :       fd_stem_publish( stem, 0UL, sig, chunk, msg_sz+sizeof(fd_microblock_bank_trailer_t), 0UL, tsorig, tspub );
     662           0 :       ctx->bank_expect[ i ] = stem->seqs[0]-1UL;
     663           0 :       ctx->bank_ready_at[i] = now2 + (long)ctx->microblock_duration_ticks;
     664           0 :       ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, msg_sz+sizeof(fd_microblock_bank_trailer_t), ctx->out_chunk0, ctx->out_wmark );
     665           0 :       ctx->slot_microblock_cnt += fd_ulong_if( trailer->is_bundle, schedule_cnt, 1UL );
     666             : 
     667           0 :       ctx->bank_idle_bitset = fd_ulong_pop_lsb( ctx->bank_idle_bitset );
     668           0 :       ctx->skip_cnt         = (long)schedule_cnt * fd_long_if( ctx->use_consumed_cus, (long)bank_cnt/2L, 1L );
     669           0 :       fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now2 );
     670             : 
     671           0 :       memcpy( ctx->last_sched_metrics->all, (ulong const *)fd_metrics_tl, sizeof(ctx->last_sched_metrics->all) );
     672           0 :       ctx->last_sched_metrics->time = now2;
     673           0 :     }
     674           0 :   }
     675             : 
     676           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS,       any_ready     );
     677           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, any_scheduled );
     678           0 :   now = fd_tickcount();
     679           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
     680             : 
     681             : #if FD_PACK_USE_EXTRA_STORAGE
     682             :   if( FD_UNLIKELY( !extra_txn_deq_empty( ctx->extra_txn_deq ) ) ) {
     683             :     /* Don't start pulling from the extra storage until the available
     684             :        transaction count drops below half. */
     685             :     ulong avail_space   = (ulong)fd_long_max( 0L, (long)(ctx->max_pending_transactions>>1)-(long)fd_pack_avail_txn_cnt( ctx->pack ) );
     686             :     ulong qty_to_insert = fd_ulong_min( 10UL, fd_ulong_min( extra_txn_deq_cnt( ctx->extra_txn_deq ), avail_space ) );
     687             :     int any_successes = 0;
     688             :     for( ulong i=0UL; i<qty_to_insert; i++ ) any_successes |= (0<=insert_from_extra( ctx ));
     689             :     if( FD_LIKELY( any_successes ) ) ctx->last_successful_insert = now;
     690             :   }
     691             : #endif
     692             : 
     693             :   /* Did we send the maximum allowed microblocks? Then end the slot. */
     694           0 :   if( FD_UNLIKELY( ctx->slot_microblock_cnt==ctx->slot_max_microblocks )) {
     695           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER,       0 );
     696           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS,        0 );
     697           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS,  0 );
     698             :     /* The pack object also does this accounting and increases this
     699             :        metric, but we end the slot early so won't see it unless we also
     700             :        increment it here. */
     701           0 :     FD_MCNT_INC( PACK, MICROBLOCK_PER_BLOCK_LIMIT, 1UL );
     702           0 :     log_end_block_metrics( ctx, now, "microblock" );
     703           0 :     ctx->drain_banks         = 1;
     704           0 :     ctx->leader_slot         = ULONG_MAX;
     705           0 :     ctx->slot_microblock_cnt = 0UL;
     706           0 :     fd_pack_end_block( ctx->pack );
     707           0 :     remove_ib( ctx );
     708             : 
     709           0 :   }
     710           0 : }
     711             : 
     712             : 
     713             : /* At this point, we have started receiving frag seq with details in
     714             :     mline at time now.  Speculatively process it here. */
     715             : 
     716             : static inline void
     717             : during_frag( fd_pack_ctx_t * ctx,
     718             :              ulong           in_idx,
     719             :              ulong           seq FD_PARAM_UNUSED,
     720             :              ulong           sig,
     721             :              ulong           chunk,
     722             :              ulong           sz,
     723           0 :              ulong           ctl FD_PARAM_UNUSED ) {
     724             : 
     725           0 :   uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     726             : 
     727           0 :   switch( ctx->in_kind[ in_idx ] ) {
     728           0 :   case IN_KIND_POH: {
     729             :       /* Not interested in stamped microblocks, only leader updates. */
     730           0 :     if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
     731             : 
     732             :     /* There was a leader transition.  Handle it. */
     733           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(fd_became_leader_t) ) )
     734           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 ));
     735             : 
     736           0 :   long now_ticks = fd_tickcount();
     737           0 :   long now_ns    = fd_log_wallclock();
     738             : 
     739           0 :     if( FD_UNLIKELY( ctx->leader_slot!=ULONG_MAX ) ) {
     740           0 :       FD_LOG_WARNING(( "switching to slot %lu while packing for slot %lu. Draining bank tiles.", fd_disco_poh_sig_slot( sig ), ctx->leader_slot ));
     741           0 :       log_end_block_metrics( ctx, now_ticks, "switch" );
     742           0 :       ctx->drain_banks         = 1;
     743           0 :       ctx->leader_slot         = ULONG_MAX;
     744           0 :       ctx->slot_microblock_cnt = 0UL;
     745           0 :       fd_pack_end_block( ctx->pack );
     746           0 :       remove_ib( ctx );
     747           0 :     }
     748           0 :     ctx->leader_slot = fd_disco_poh_sig_slot( sig );
     749             : 
     750           0 :     ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->leader_slot, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
     751           0 :     FD_MCNT_INC( PACK, TRANSACTION_EXPIRED, exp_cnt );
     752             : 
     753           0 :     fd_became_leader_t * became_leader = (fd_became_leader_t *)dcache_entry;
     754           0 :     ctx->leader_bank          = became_leader->bank;
     755           0 :     ctx->slot_max_microblocks = became_leader->max_microblocks_in_slot;
     756             :     /* Reserve some space in the block for ticks */
     757           0 :     ctx->slot_max_data        = (ctx->larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK)
     758           0 :                                       - 48UL*(became_leader->ticks_per_slot+became_leader->total_skipped_ticks);
     759             :     /* ticks_per_ns is probably relatively stable over 400ms, but not
     760             :        over several hours, so we need to compute the slot duration in
     761             :        milliseconds first and then convert to ticks.  This doesn't need
     762             :        to be super accurate, but we don't want it to vary wildly. */
     763           0 :     long end_ticks = now_ticks + (long)((double)fd_long_max( became_leader->slot_end_ns - now_ns, 1L )*ctx->ticks_per_ns);
     764             :     /* We may still get overrun, but then we'll never use this and just
     765             :        reinitialize it the next time when we actually become leader. */
     766           0 :     fd_pack_pacing_init( ctx->pacer, now_ticks, end_ticks, (float)ctx->ticks_per_ns, ctx->slot_max_cost );
     767             : 
     768           0 :     if( FD_UNLIKELY( ctx->crank->enabled ) ) {
     769             :       /* If we get overrun, we'll just never use these values, but the
     770             :          old values aren't really useful either. */
     771           0 :       ctx->crank->epoch = became_leader->epoch;
     772           0 :       *(ctx->crank->prev_config) = *(became_leader->bundle->config);
     773           0 :       memcpy( ctx->crank->recent_blockhash,   became_leader->bundle->last_blockhash,     32UL );
     774           0 :       memcpy( ctx->crank->tip_receiver_owner, became_leader->bundle->tip_receiver_owner, 32UL );
     775           0 :     }
     776             : 
     777           0 :     FD_LOG_INFO(( "pack_became_leader(slot=%lu,ends_at=%ld)", ctx->leader_slot, became_leader->slot_end_ns ));
     778             : 
     779             :     /* The dcache might get overrun, so set slot_end_ns to 0, so if it does
     780             :        the slot will get skipped.  Then update it in the `after_frag` case
     781             :        below to the correct value. */
     782           0 :     ctx->slot_end_ns = 0L;
     783           0 :     ctx->_slot_end_ns = became_leader->slot_end_ns;
     784             : 
     785           0 :     update_metric_state( ctx, fd_tickcount(), FD_PACK_METRIC_STATE_LEADER, 1 );
     786           0 :     return;
     787           0 :   }
     788           0 :   case IN_KIND_BANK: {
     789           0 :     FD_TEST( ctx->use_consumed_cus );
     790             :       /* For a previous slot */
     791           0 :     if( FD_UNLIKELY( fd_disco_bank_sig_slot( sig )!=ctx->leader_slot ) ) return;
     792             : 
     793           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz<sizeof(fd_microblock_trailer_t)
     794           0 :           || sz>sizeof(fd_microblock_trailer_t)+sizeof(fd_txn_p_t)*MAX_TXN_PER_MICROBLOCK ) )
     795           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 ));
     796             : 
     797           0 :     ctx->pending_rebate_cnt = (sz-sizeof(fd_microblock_trailer_t))/sizeof(fd_txn_p_t);
     798           0 :     fd_memcpy( ctx->pending_rebate, dcache_entry, sz-sizeof(fd_microblock_trailer_t) );
     799           0 :     return;
     800           0 :   }
     801           0 :   case IN_KIND_RESOLV: {
     802           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_TPU_RESOLVED_MTU ) )
     803           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 ));
     804             : 
     805           0 :     fd_txn_m_t * txnm = (fd_txn_m_t *)dcache_entry;
     806           0 :     FD_TEST( txnm->payload_sz<=FD_TPU_MTU );
     807           0 :     FD_TEST( txnm->txn_t_sz<=FD_TXN_MAX_SZ );
     808           0 :     fd_txn_t * txn  = fd_txn_m_txn_t( txnm );
     809             : 
     810           0 :     if( FD_UNLIKELY( (ctx->leader_slot==ULONG_MAX) & (sig>ctx->highest_observed_slot) ) ) {
     811             :       /* Using the resolv tile's knowledge of the current slot is a bit
     812             :          of a hack, since we don't get any info if there are no
     813             :          transactions and we're not leader.  We're actually in exactly
     814             :          the case where that's okay though.  The point of calling
     815             :          expire_before long before we become leader is so that we don't
     816             :          drop new but low-fee-paying transactions when pack is clogged
     817             :          with expired but high-fee-paying transactions.  That can only
     818             :          happen if we are getting transactions. */
     819           0 :       ctx->highest_observed_slot = sig;
     820           0 :       ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->highest_observed_slot, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
     821           0 :       FD_MCNT_INC( PACK, TRANSACTION_EXPIRED, exp_cnt );
     822           0 :     }
     823             : 
     824             : 
     825           0 :     if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) {
     826           0 :       ctx->is_bundle = 1;
     827           0 :       if( FD_LIKELY( txnm->block_engine.bundle_id!=ctx->current_bundle->id ) ) {
     828           0 :         if( FD_UNLIKELY( ctx->current_bundle->bundle ) ) {
     829           0 :           FD_MCNT_INC( PACK, TRANSACTION_DROPPED_PARTIAL_BUNDLE, ctx->current_bundle->txn_received );
     830           0 :           fd_pack_insert_bundle_cancel( ctx->pack, ctx->current_bundle->bundle, ctx->current_bundle->txn_cnt );
     831           0 :         }
     832           0 :         ctx->current_bundle->id                 = txnm->block_engine.bundle_id;
     833           0 :         ctx->current_bundle->txn_cnt            = txnm->block_engine.bundle_txn_cnt;
     834           0 :         ctx->current_bundle->min_blockhash_slot = ULONG_MAX;
     835           0 :         ctx->current_bundle->txn_received       = 0UL;
     836             : 
     837           0 :         if( FD_UNLIKELY( ctx->current_bundle->txn_cnt==0UL ) ) {
     838           0 :           FD_MCNT_INC( PACK, TRANSACTION_DROPPED_PARTIAL_BUNDLE, 1UL );
     839           0 :           ctx->current_bundle->id = 0UL;
     840           0 :           return;
     841           0 :         }
     842           0 :         ctx->blk_engine_cfg->commission = txnm->block_engine.commission;
     843           0 :         memcpy( ctx->blk_engine_cfg->commission_pubkey->b, txnm->block_engine.commission_pubkey, 32UL );
     844             : 
     845           0 :         ctx->current_bundle->bundle = fd_pack_insert_bundle_init( ctx->pack, ctx->current_bundle->_txn, ctx->current_bundle->txn_cnt );
     846           0 :       }
     847           0 :       ctx->cur_spot                           = ctx->current_bundle->bundle[ ctx->current_bundle->txn_received ];
     848           0 :       ctx->current_bundle->min_blockhash_slot = fd_ulong_min( ctx->current_bundle->min_blockhash_slot, sig );
     849           0 :     } else {
     850           0 :       ctx->is_bundle = 0;
     851             : #if FD_PACK_USE_EXTRA_STORAGE
     852             :       if( FD_LIKELY( ctx->leader_slot!=ULONG_MAX || fd_pack_avail_txn_cnt( ctx->pack )<ctx->max_pending_transactions ) ) {
     853             :         ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
     854             :         ctx->insert_to_extra = 0;
     855             :       } else {
     856             :         if( FD_UNLIKELY( extra_txn_deq_full( ctx->extra_txn_deq ) ) ) {
     857             :           extra_txn_deq_remove_head( ctx->extra_txn_deq );
     858             :           FD_MCNT_INC( PACK, TRANSACTION_DROPPED_FROM_EXTRA, 1UL );
     859             :         }
     860             :         ctx->cur_spot = extra_txn_deq_peek_tail( extra_txn_deq_insert_tail( ctx->extra_txn_deq ) );
     861             :         /* We want to store the current time in cur_spot so that we can
     862             :            track its expiration better.  We just stash it in the CU
     863             :            fields, since those aren't important right now. */
     864             :         ctx->cur_spot->txnp->blockhash_slot = sig;
     865             :         ctx->insert_to_extra                = 1;
     866             :         FD_MCNT_INC( PACK, TRANSACTION_INSERTED_TO_EXTRA, 1UL );
     867             :       }
     868             : #else
     869           0 :       ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
     870           0 : #endif
     871           0 :     }
     872             : 
     873             :     /* We get transactions from the resolv tile.
     874             :        The transactions should have been parsed and verified. */
     875           0 :     FD_MCNT_INC( PACK, NORMAL_TRANSACTION_RECEIVED, 1UL );
     876             : 
     877             : 
     878           0 :     fd_memcpy( ctx->cur_spot->txnp->payload, fd_txn_m_payload( txnm ), txnm->payload_sz              );
     879           0 :     fd_memcpy( TXN(ctx->cur_spot->txnp),     txn,                      txnm->txn_t_sz                );
     880           0 :     fd_memcpy( ctx->cur_spot->alt_accts,     fd_txn_m_alut( txnm ),    32UL*txn->addr_table_adtl_cnt );
     881           0 :     ctx->cur_spot->txnp->payload_sz = txnm->payload_sz;
     882             : 
     883             :   #if DETAILED_LOGGING
     884             :     FD_LOG_NOTICE(( "Pack got a packet. Payload size: %lu, txn footprint: %lu", txnm->payload_sz, txnm->txn_t_sz ));
     885             :   #endif
     886           0 :     break;
     887           0 :   }
     888           0 :   }
     889           0 : }
     890             : 
     891             : 
     892             : /* After the transaction has been fully received, and we know we were
     893             :    not overrun while reading it, insert it into pack. */
     894             : 
     895             : static inline void
     896             : after_frag( fd_pack_ctx_t *     ctx,
     897             :             ulong               in_idx,
     898             :             ulong               seq,
     899             :             ulong               sig,
     900             :             ulong               sz,
     901             :             ulong               tsorig,
     902           0 :             fd_stem_context_t * stem ) {
     903           0 :   (void)seq;
     904           0 :   (void)sz;
     905           0 :   (void)tsorig;
     906           0 :   (void)stem;
     907             : 
     908           0 :   long now = fd_tickcount();
     909             : 
     910           0 :   switch( ctx->in_kind[ in_idx ] ) {
     911           0 :   case IN_KIND_POH: {
     912           0 :     if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
     913             : 
     914           0 :     ctx->slot_end_ns = ctx->_slot_end_ns;
     915           0 :     fd_pack_set_block_limits( ctx->pack, ctx->slot_max_microblocks, ctx->slot_max_data );
     916           0 :     fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now );
     917             : 
     918           0 :     break;
     919           0 :   }
     920           0 :   case IN_KIND_BANK: {
     921             :     /* For a previous slot */
     922           0 :     if( FD_UNLIKELY( fd_disco_bank_sig_slot( sig )!=ctx->leader_slot ) ) return;
     923             : 
     924           0 :     fd_pack_rebate_cus( ctx->pack, ctx->pending_rebate, ctx->pending_rebate_cnt );
     925           0 :     ctx->pending_rebate_cnt = 0UL;
     926           0 :     fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now );
     927           0 :     break;
     928           0 :   }
     929           0 :   case IN_KIND_RESOLV: {
     930             :     /* Normal transaction case */
     931             : #if FD_PACK_USE_EXTRA_STORAGE
     932             :     if( FD_LIKELY( !ctx->insert_to_extra ) ) {
     933             : #else
     934           0 :     if( 1 ) {
     935           0 : #endif
     936           0 :     if( FD_UNLIKELY( ctx->is_bundle ) ) {
     937           0 :       if( FD_UNLIKELY( ctx->current_bundle->txn_cnt==0UL ) ) return;
     938           0 :       if( FD_UNLIKELY( ++(ctx->current_bundle->txn_received)==ctx->current_bundle->txn_cnt ) ) {
     939           0 :         long insert_duration = -fd_tickcount();
     940           0 :         int result = fd_pack_insert_bundle_fini( ctx->pack, ctx->current_bundle->bundle, ctx->current_bundle->txn_cnt, ctx->current_bundle->min_blockhash_slot, 0, ctx->blk_engine_cfg );
     941           0 :         insert_duration      += fd_tickcount();
     942           0 :         ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ] += ctx->current_bundle->txn_received;
     943           0 :         fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
     944           0 :         ctx->current_bundle->bundle = NULL;
     945           0 :       }
     946           0 :     } else {
     947           0 :       ulong blockhash_slot = sig;
     948           0 :       long insert_duration = -fd_tickcount();
     949           0 :       int result = fd_pack_insert_txn_fini( ctx->pack, ctx->cur_spot, blockhash_slot );
     950           0 :       insert_duration      += fd_tickcount();
     951           0 :       ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
     952           0 :       fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
     953           0 :       if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
     954           0 :     }
     955           0 :     }
     956             : 
     957           0 :     ctx->cur_spot = NULL;
     958           0 :     break;
     959           0 :   }
     960           0 :   }
     961             : 
     962           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
     963           0 : }
     964             : 
     965             : static void
     966             : privileged_init( fd_topo_t *      topo,
     967           0 :                  fd_topo_tile_t * tile ) {
     968           0 :   if( FD_LIKELY( !tile->pack.bundle.enabled ) ) return;
     969             : 
     970           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     971             : 
     972           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     973           0 :   fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
     974             : 
     975           0 :   if( FD_UNLIKELY( !strcmp( tile->pack.bundle.identity_key_path, "" ) ) )
     976           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     977             : 
     978           0 :   const uchar * identity_key = fd_keyload_load( tile->pack.bundle.identity_key_path, /* pubkey only: */ 1 );
     979           0 :   fd_memcpy( ctx->crank->identity_pubkey->b, identity_key, 32UL );
     980             : 
     981           0 :   if( FD_UNLIKELY( !fd_base58_decode_32( tile->pack.bundle.vote_account_path, ctx->crank->vote_pubkey->b ) ) ) {
     982           0 :     const uchar * vote_key = fd_keyload_load( tile->pack.bundle.vote_account_path, /* pubkey only: */ 1 );
     983           0 :     fd_memcpy( ctx->crank->vote_pubkey->b, vote_key, 32UL );
     984           0 :   }
     985           0 : }
     986             : 
     987             : static void
     988             : unprivileged_init( fd_topo_t *      topo,
     989           0 :                    fd_topo_tile_t * tile ) {
     990           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     991             : 
     992           0 :   fd_pack_limits_t limits[1] = {{
     993           0 :     .max_cost_per_block        = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK,
     994           0 :     .max_vote_cost_per_block   = FD_PACK_MAX_VOTE_COST_PER_BLOCK,
     995           0 :     .max_write_cost_per_acct   = FD_PACK_MAX_WRITE_COST_PER_ACCT,
     996           0 :     .max_data_bytes_per_block  = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
     997           0 :     .max_txn_per_microblock    = EFFECTIVE_TXN_PER_MICROBLOCK,
     998           0 :     .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
     999           0 :   }};
    1000             : 
    1001           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 ));
    1002             : 
    1003           0 :   ulong pack_footprint = fd_pack_footprint( tile->pack.max_pending_transactions, BUNDLE_META_SZ, tile->pack.bank_tile_count, limits );
    1004             : 
    1005           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1006           0 :   fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
    1007           0 :   fd_rng_t *      rng = fd_rng_join( fd_rng_new( FD_SCRATCH_ALLOC_APPEND( l, fd_rng_align(), fd_rng_footprint() ), 0U, 0UL ) );
    1008           0 :   if( FD_UNLIKELY( !rng ) ) FD_LOG_ERR(( "fd_rng_new failed" ));
    1009             : 
    1010           0 :   ctx->pack = fd_pack_join( fd_pack_new( FD_SCRATCH_ALLOC_APPEND( l, fd_pack_align(), pack_footprint ),
    1011           0 :                                          tile->pack.max_pending_transactions, BUNDLE_META_SZ, tile->pack.bank_tile_count,
    1012           0 :                                          limits, rng ) );
    1013           0 :   if( FD_UNLIKELY( !ctx->pack ) ) FD_LOG_ERR(( "fd_pack_new failed" ));
    1014             : 
    1015           0 :   if( FD_UNLIKELY( tile->in_cnt>32UL ) ) FD_LOG_ERR(( "Too many input links (%lu>32) to pack tile", tile->in_cnt ));
    1016             : 
    1017           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
    1018           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
    1019             : 
    1020           0 :     if( FD_LIKELY(      !strcmp( link->name, "resolv_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
    1021           0 :     else if( FD_LIKELY( !strcmp( link->name, "dedup_pack"  ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
    1022           0 :     else if( FD_LIKELY( !strcmp( link->name, "poh_pack"    ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
    1023           0 :     else if( FD_LIKELY( !strcmp( link->name, "bank_poh"    ) ) ) ctx->in_kind[ i ] = IN_KIND_BANK;
    1024           0 :     else if( FD_LIKELY( !strcmp( link->name, "sign_pack"   ) ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
    1025           0 :     else FD_LOG_ERR(( "pack tile has unexpected input link %lu %s", i, link->name ));
    1026           0 :   }
    1027             : 
    1028           0 :   ulong out_cnt = fd_topo_link_consumer_cnt( topo, &topo->links[ tile->out_link_id[ 0 ] ] );
    1029             : 
    1030           0 :   if( FD_UNLIKELY( !out_cnt                                ) ) FD_LOG_ERR(( "pack tile connects to no banking tiles" ));
    1031           0 :   if( FD_UNLIKELY( out_cnt>FD_PACK_PACK_MAX_OUT            ) ) FD_LOG_ERR(( "pack tile connects to too many banking tiles" ));
    1032           0 :   if( FD_UNLIKELY( out_cnt!=tile->pack.bank_tile_count+1UL ) ) FD_LOG_ERR(( "pack tile connects to %lu banking tiles, but tile->pack.bank_tile_count is %lu", out_cnt-1UL, tile->pack.bank_tile_count ));
    1033             : 
    1034             : 
    1035           0 :   ctx->crank->enabled = tile->pack.bundle.enabled;
    1036           0 :   if( FD_UNLIKELY( tile->pack.bundle.enabled ) ) {
    1037           0 :     if( FD_UNLIKELY( !fd_bundle_crank_gen_init( ctx->crank->gen, (fd_acct_addr_t const *)tile->pack.bundle.tip_distribution_program_addr,
    1038           0 :             (fd_acct_addr_t const *)tile->pack.bundle.tip_payment_program_addr,
    1039           0 :             (fd_acct_addr_t const *)ctx->crank->vote_pubkey->b,
    1040           0 :             (fd_acct_addr_t const *)tile->pack.bundle.tip_distribution_authority, tile->pack.bundle.commission_bps ) ) ) {
    1041           0 :       FD_LOG_ERR(( "constructing bundle generator failed" ));
    1042           0 :     }
    1043             : 
    1044           0 :     ulong sign_in_idx  = fd_topo_find_tile_in_link ( topo, tile, "sign_pack", tile->kind_id );
    1045           0 :     ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "pack_sign", tile->kind_id );
    1046           0 :     FD_TEST( sign_in_idx!=ULONG_MAX );
    1047           0 :     fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
    1048           0 :     fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
    1049           0 :     if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new( ctx->crank->keyguard_client,
    1050           0 :             sign_out->mcache,
    1051           0 :             sign_out->dcache,
    1052           0 :             sign_in->mcache,
    1053           0 :             sign_in->dcache ) ) ) ) {
    1054           0 :       FD_LOG_ERR(( "failed to construct keyguard" ));
    1055           0 :     }
    1056             :     /* Initialize enough of the prev config that it produces a
    1057             :        transaction */
    1058           0 :     ctx->crank->prev_config->discriminator       = 0x82ccfa1ee0aa0c9bUL;
    1059           0 :     ctx->crank->prev_config->tip_receiver->b[1]  = 1;
    1060           0 :     ctx->crank->prev_config->block_builder->b[2] = 1;
    1061             : 
    1062           0 :     memset( ctx->crank->tip_receiver_owner, '\0', 32UL );
    1063           0 :     memset( ctx->crank->recent_blockhash,   '\0', 32UL );
    1064           0 :     memset( ctx->crank->last_sig,           '\0', 64UL );
    1065           0 :     ctx->crank->ib_inserted    = 0;
    1066           0 :     ctx->crank->epoch          = 0UL;
    1067           0 :     ctx->crank->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
    1068           0 :     FD_TEST( ctx->crank->keyswitch );
    1069           0 :   } else {
    1070           0 :     memset( ctx->crank, '\0', sizeof(ctx->crank) );
    1071           0 :   }
    1072             : 
    1073             : 
    1074             : #if FD_PACK_USE_EXTRA_STORAGE
    1075             :   ctx->extra_txn_deq = extra_txn_deq_join( extra_txn_deq_new( FD_SCRATCH_ALLOC_APPEND( l, extra_txn_deq_align(),
    1076             :                                                                                           extra_txn_deq_footprint() ) ) );
    1077             : #endif
    1078             : 
    1079           0 :   ctx->cur_spot                      = NULL;
    1080           0 :   ctx->is_bundle                     = 0;
    1081           0 :   ctx->max_pending_transactions      = tile->pack.max_pending_transactions;
    1082           0 :   ctx->leader_slot                   = ULONG_MAX;
    1083           0 :   ctx->leader_bank                   = NULL;
    1084           0 :   ctx->slot_microblock_cnt           = 0UL;
    1085           0 :   ctx->slot_max_microblocks          = 0UL;
    1086           0 :   ctx->slot_max_data                 = 0UL;
    1087           0 :   ctx->larger_shred_limits_per_block = tile->pack.larger_shred_limits_per_block;
    1088           0 :   ctx->slot_max_cost                 = limits->max_cost_per_block;
    1089           0 :   ctx->drain_banks                   = 0;
    1090           0 :   ctx->approx_wallclock_ns           = fd_log_wallclock();
    1091           0 :   ctx->rng                           = rng;
    1092           0 :   ctx->ticks_per_ns                  = fd_tempo_tick_per_ns( NULL );
    1093           0 :   ctx->last_successful_insert        = 0L;
    1094           0 :   ctx->highest_observed_slot         = 0UL;
    1095           0 :   ctx->microblock_duration_ticks     = (ulong)(fd_tempo_tick_per_ns( NULL )*(double)MICROBLOCK_DURATION_NS  + 0.5);
    1096             : #if FD_PACK_USE_EXTRA_STORAGE
    1097             :   ctx->insert_to_extra               = 0;
    1098             : #endif
    1099           0 :   ctx->use_consumed_cus              = tile->pack.use_consumed_cus;
    1100           0 :   ctx->crank->enabled                = tile->pack.bundle.enabled;
    1101             : 
    1102           0 :   ctx->wait_duration_ticks[ 0 ] = ULONG_MAX;
    1103           0 :   for( ulong i=1UL; i<MAX_TXN_PER_MICROBLOCK+1UL; i++ ) {
    1104           0 :     ctx->wait_duration_ticks[ i ]=(ulong)(fd_tempo_tick_per_ns( NULL )*(double)wait_duration[ i ] + 0.5);
    1105           0 :   }
    1106             : 
    1107             : 
    1108           0 :   ctx->bank_cnt         = tile->pack.bank_tile_count;
    1109           0 :   ctx->poll_cursor      = 0;
    1110           0 :   ctx->skip_cnt         = 0L;
    1111           0 :   ctx->bank_idle_bitset = fd_ulong_mask_lsb( (int)tile->pack.bank_tile_count );
    1112           0 :   for( ulong i=0UL; i<tile->pack.bank_tile_count; i++ ) {
    1113           0 :     ulong busy_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "bank_busy.%lu", i );
    1114           0 :     FD_TEST( busy_obj_id!=ULONG_MAX );
    1115           0 :     ctx->bank_current[ i ] = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
    1116           0 :     ctx->bank_expect[ i ] = ULONG_MAX;
    1117           0 :     if( FD_UNLIKELY( !ctx->bank_current[ i ] ) ) FD_LOG_ERR(( "banking tile %lu has no busy flag", i ));
    1118           0 :     ctx->bank_ready_at[ i ] = 0L;
    1119           0 :     FD_TEST( ULONG_MAX==fd_fseq_query( ctx->bank_current[ i ] ) );
    1120           0 :   }
    1121             : 
    1122           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
    1123           0 :     fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
    1124           0 :     fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
    1125             : 
    1126           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
    1127           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
    1128           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
    1129           0 :   }
    1130             : 
    1131           0 :   ctx->out_mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
    1132           0 :   ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
    1133           0 :   ctx->out_wmark  = fd_dcache_compact_wmark ( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
    1134           0 :   ctx->out_chunk  = ctx->out_chunk0;
    1135             : 
    1136             :   /* Initialize metrics storage */
    1137           0 :   memset( ctx->insert_result, '\0', FD_PACK_INSERT_RETVAL_CNT * sizeof(ulong) );
    1138           0 :   fd_histf_join( fd_histf_new( ctx->schedule_duration, FD_MHIST_SECONDS_MIN( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS ),
    1139           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS ) ) );
    1140           0 :   fd_histf_join( fd_histf_new( ctx->no_sched_duration, FD_MHIST_SECONDS_MIN( PACK, NO_SCHED_MICROBLOCK_DURATION_SECONDS ),
    1141           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, NO_SCHED_MICROBLOCK_DURATION_SECONDS ) ) );
    1142           0 :   fd_histf_join( fd_histf_new( ctx->insert_duration,   FD_MHIST_SECONDS_MIN( PACK, INSERT_TRANSACTION_DURATION_SECONDS  ),
    1143           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, INSERT_TRANSACTION_DURATION_SECONDS  ) ) );
    1144           0 :   fd_histf_join( fd_histf_new( ctx->complete_duration, FD_MHIST_SECONDS_MIN( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS ),
    1145           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS  ) ) );
    1146           0 :   ctx->metric_state = 0;
    1147           0 :   ctx->metric_state_begin = fd_tickcount();
    1148           0 :   memset( ctx->metric_timing,      '\0', 16*sizeof(long)                 );
    1149           0 :   memset( ctx->current_bundle,     '\0', sizeof(ctx->current_bundle)     );
    1150           0 :   memset( ctx->blk_engine_cfg,     '\0', sizeof(ctx->blk_engine_cfg)     );
    1151           0 :   memset( ctx->last_sched_metrics, '\0', sizeof(ctx->last_sched_metrics) );
    1152             : 
    1153           0 :   FD_LOG_INFO(( "packing microblocks of at most %lu transactions to %lu bank tiles", EFFECTIVE_TXN_PER_MICROBLOCK, tile->pack.bank_tile_count ));
    1154             : 
    1155           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
    1156           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
    1157           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
    1158             : 
    1159           0 : }
    1160             : 
    1161             : static ulong
    1162             : populate_allowed_seccomp( fd_topo_t const *      topo,
    1163             :                           fd_topo_tile_t const * tile,
    1164             :                           ulong                  out_cnt,
    1165           0 :                           struct sock_filter *   out ) {
    1166           0 :   (void)topo;
    1167           0 :   (void)tile;
    1168             : 
    1169           0 :   populate_sock_filter_policy_pack( out_cnt, out, (uint)fd_log_private_logfile_fd() );
    1170           0 :   return sock_filter_policy_pack_instr_cnt;
    1171           0 : }
    1172             : 
    1173             : static ulong
    1174             : populate_allowed_fds( fd_topo_t const *      topo,
    1175             :                       fd_topo_tile_t const * tile,
    1176             :                       ulong                  out_fds_cnt,
    1177           0 :                       int *                  out_fds ) {
    1178           0 :   (void)topo;
    1179           0 :   (void)tile;
    1180             : 
    1181           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
    1182             : 
    1183           0 :   ulong out_cnt = 0UL;
    1184           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
    1185           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
    1186           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
    1187           0 :   return out_cnt;
    1188           0 : }
    1189             : 
    1190           0 : #define STEM_BURST (1UL)
    1191             : 
    1192             : /* We want lazy (measured in ns) to be small enough that the producer
    1193             :     and the consumer never have to wait for credits.  For most tango
    1194             :     links, we use a default worst case speed coming from 100 Gbps
    1195             :     Ethernet.  That's not very suitable for microblocks that go from
    1196             :     pack to bank.  Instead we manually estimate the very aggressive
    1197             :     1000ns per microblock, and then reduce it further (in line with the
    1198             :     default lazy value computation) to ensure the random value chosen
    1199             :     based on this won't lead to credit return stalls. */
    1200           0 : #define STEM_LAZY  (128L*3000L)
    1201             : 
    1202           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_pack_ctx_t
    1203           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_pack_ctx_t)
    1204             : 
    1205           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
    1206           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
    1207           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
    1208           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
    1209           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
    1210           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
    1211             : 
    1212             : #include "../../../../disco/stem/fd_stem.c"
    1213             : 
    1214             : fd_topo_run_tile_t fd_tile_pack = {
    1215             :   .name                     = "pack",
    1216             :   .populate_allowed_seccomp = populate_allowed_seccomp,
    1217             :   .populate_allowed_fds     = populate_allowed_fds,
    1218             :   .scratch_align            = scratch_align,
    1219             :   .scratch_footprint        = scratch_footprint,
    1220             :   .privileged_init          = privileged_init,
    1221             :   .unprivileged_init        = unprivileged_init,
    1222             :   .run                      = stem_run,
    1223             : };

Generated by: LCOV version 1.14