LCOV - code coverage report
Current view: top level - disco/pack - fd_pack.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 1722 0.0 %
Date: 2026-03-19 18:19:27 Functions: 0 44 0.0 %

          Line data    Source code
       1             : #define FD_UNALIGNED_ACCESS_STYLE 0
       2             : #include "fd_pack.h"
       3             : #include "fd_pack_cost.h"
       4             : #include "fd_pack_bitset.h"
       5             : #include "fd_pack_unwritable.h"
       6             : #include "fd_chkdup.h"
       7             : #include "fd_pack_tip_prog_blacklist.h"
       8             : #include <math.h> /* for sqrt */
       9             : #include <stddef.h> /* for offsetof */
      10             : #include "../metrics/fd_metrics.h"
      11             : 
      12             : #define FD_PACK_USE_NON_TEMPORAL_MEMCPY 1
      13             : 
      14             : /* Declare a bunch of helper structs used for pack-internal data
      15             :    structures. */
      16             : typedef struct {
      17             :   fd_ed25519_sig_t sig;
      18             : } wrapped_sig_t;
      19             : 
      20             : /* fd_pack_ord_txn_t: An fd_txn_p_t with information required to order
      21             :    it by priority. */
      22             : struct fd_pack_private_ord_txn {
      23             :   /* It's important that there be no padding here (asserted below)
      24             :      because the code casts back and forth from pointers to this element
      25             :      to pointers to the whole struct. */
      26             :   union {
      27             :     fd_txn_p_t   txn[1];  /* txn is an alias for txn_e->txnp */
      28             :     fd_txn_e_t   txn_e[1];
      29             :     fd_txn_e_t   _txn_e;  /* Non-array type needed for map_chain */
      30             :     struct{ uchar _sig_cnt; wrapped_sig_t sig; };
      31             :   };
      32             : 
      33             :   /* Since this struct can be in one of several trees, it's helpful to
      34             :      store which tree.  This should be one of the FD_ORD_TXN_ROOT_*
      35             :      values. */
      36             :   int root;
      37             : 
      38             :   /* The sig2txn map_chain fields */
      39             :   ushort sigmap_next;
      40             :   ushort sigmap_prev;
      41             : 
      42             :   /* Each transaction is inserted with an expiration "time."  This code
      43             :      doesn't care about the units (blocks, rdtsc tick, ns, etc.), and
      44             :      doesn't require transactions to be inserted in expiration date
      45             :      order. */
      46             :   ulong expires_at;
      47             :   /* expq_idx: When this object is part of one of the treaps, it's
      48             :      also in the expiration priority queue.  This field (which is
      49             :      manipulated behind the scenes by the fd_prq code) stores where so
      50             :      that if we delete this transaction, we can also delete it from the
      51             :      expiration priority queue. */
      52             :   ulong expq_idx;
      53             : 
      54             :   /* The noncemap map_chain fields */
      55             :   ushort noncemap_next;
      56             :   ushort noncemap_prev;
      57             : 
      58             :   /* We want rewards*compute_est to fit in a ulong so that r1/c1 < r2/c2 can be
      59             :      computed as r1*c2 < r2*c1, with the product fitting in a ulong.
      60             :      compute_est has a small natural limit of mid-20 bits. rewards doesn't have
      61             :      a natural limit, so there is some argument to be made for raising the
      62             :      limit for rewards to 40ish bits. The struct has better packing with
      63             :      uint/uint though. */
      64             :   uint                __attribute__((aligned(64))) /* We want the treap fields and the bitsets
      65             :                                                        to be on the same double cache line pair */
      66             :                rewards;     /* in Lamports */
      67             :   uint         compute_est; /* in compute units */
      68             : 
      69             :   /* The treap fields */
      70             :   ushort left;
      71             :   ushort right;
      72             :   ushort parent;
      73             :   ushort prio;
      74             :   ushort prev;
      75             :   ushort next;
      76             : 
      77             :   /* skip: if we skip this transaction more than FD_PACK_SKIP_CNT times
      78             :      for reasons that won't go away until the end of the block, then we
      79             :      want to skip it very quickly.  If skip is in [1, FD_PACK_SKIP_CNT],
      80             :      then that means we have to skip it `skip` more times before taking
      81             :      any action.  If skip>FD_PACK_SKIP_CNT, then it is a compressed slot
      82             :      number during which it should be skipped, and we'll skip it until
      83             :      the compressed slot reaches a new value.  skip is never 0. */
      84             :   ushort skip;
      85             : 
      86             :   FD_PACK_BITSET_DECLARE( rw_bitset ); /* all accts this txn references */
      87             :   FD_PACK_BITSET_DECLARE(  w_bitset ); /* accts this txn write-locks    */
      88             : 
      89             : };
      90             : typedef struct fd_pack_private_ord_txn fd_pack_ord_txn_t;
      91             : 
      92             : /* What we want is that the payload starts at byte 0 of
      93             :    fd_pack_ord_txn_t so that the trick with the signature map works
      94             :    properly.  GCC and Clang seem to disagree on the rules of offsetof.
      95             :    */
      96             : FD_STATIC_ASSERT( offsetof( fd_pack_ord_txn_t, txn          )==0UL, fd_pack_ord_txn_t );
      97             : FD_STATIC_ASSERT( offsetof( fd_pack_ord_txn_t, sig          )==1UL, fd_pack_ord_txn_t );
      98             : #if FD_USING_CLANG
      99             : FD_STATIC_ASSERT( offsetof( fd_txn_p_t,             payload )==0UL, fd_pack_ord_txn_t );
     100             : #else
     101             : FD_STATIC_ASSERT( offsetof( fd_pack_ord_txn_t, txn->payload )==0UL, fd_pack_ord_txn_t );
     102             : FD_STATIC_ASSERT( offsetof( fd_pack_ord_txn_t, txn_e->txnp  )==0UL, fd_pack_ord_txn_t );
     103             : #endif
     104             : 
     105             : /* FD_ORD_TXN_ROOT is essentially a small union packed into an int.  The low
     106             :    byte is the "tag".  The higher 3 bytes depend on the low byte. */
     107           0 : #define FD_ORD_TXN_ROOT_TAG_MASK        0xFF
     108           0 : #define FD_ORD_TXN_ROOT_FREE            0
     109           0 : #define FD_ORD_TXN_ROOT_PENDING         1
     110           0 : #define FD_ORD_TXN_ROOT_PENDING_VOTE    2
     111           0 : #define FD_ORD_TXN_ROOT_PENDING_BUNDLE  3
     112           0 : #define FD_ORD_TXN_ROOT_PENALTY( idx ) (4 | (idx)<<8)
     113             : 
     114             : /* if root & TAG_MASK == PENALTY, then PENALTY_ACCT_IDX(root) gives the index
     115             :    in the transaction's list of account addresses of which penalty treap the
     116             :    transaction is in. */
     117             : #define FD_ORD_TXN_ROOT_PENALTY_ACCT_IDX( root ) (((root) & 0xFF00)>>8)
     118             : 
     119           0 : #define FD_PACK_IN_USE_WRITABLE    (0x8000000000000000UL)
     120           0 : #define FD_PACK_IN_USE_BIT_CLEARED (0x4000000000000000UL)
     121             : 
     122             : /* Each non-empty microblock we schedule also has an overhead of 48
     123             :    bytes that counts towards shed limits.  That comes from the 32 byte
     124             :    hash, the hash count (8 bytes) and the transaction count (8 bytes).
     125             :    We don't have to pay this overhead if the microblock is empty, since
     126             :    those microblocks get dropped. */
     127           0 : #define MICROBLOCK_DATA_OVERHEAD 48UL
     128             : 
     129             : /* Keep track of accounts that are written to in each block so that we
     130             :    can reset the writer costs to 0.  If the number of accounts that are
     131             :    written to is above or equal to this, we'll just clear the whole
     132             :    writer cost map instead of only removing the elements we increased. */
     133           0 : #define DEFAULT_WRITTEN_LIST_MAX 16384UL
     134             : 
     135             : FD_STATIC_ASSERT( sizeof(fd_acct_addr_t)==sizeof(fd_pubkey_t), "" );
     136             : 
     137             : /* fd_pack_expq_t: An element of an fd_prq to sort the transactions by
     138             :    timeout.  This structure has several invariants for entries
     139             :    corresponding to pending transactions:
     140             :      expires_at == txn->expires_at
     141             :      txn->exp_prq_idx is the index of this structure
     142             :    Notice that prq is an array-based heap, which means the indexes of
     143             :    elements change.  The PRQ_TMP_ST macro is hijacked to keep that
     144             :    invariant up to date.
     145             : 
     146             :    Note: this could be easier if fd_heap supported deleting from the
     147             :    middle, but that's not possible with the current design of fd_heap,
     148             :    which omits a parent pointer for improved performance. */
     149             : struct fd_pack_expq {
     150             :   ulong               expires_at;
     151             :   fd_pack_ord_txn_t * txn;
     152             : };
     153             : typedef struct fd_pack_expq fd_pack_expq_t;
     154             : 
     155             : 
     156             : /* fd_pack_bitset_acct_mapping_t: An element of an fd_map_dynamic that
     157             :    maps an account address to the number of transactions that are
     158             :    referencing it and the bit that is reserved to indicate it in the
     159             :    bitset, if any. */
     160             : struct fd_pack_bitset_acct_mapping {
     161             :   fd_acct_addr_t key; /* account address */
     162             :   ulong          ref_cnt;
     163             : 
     164             :   /* first_instance and first_instance_was_write are only valid when
     165             :      bit==FD_PACK_BITSET_FIRST_INSTANCE, which is set when ref_cnt
     166             :      transitions from 0 to 1.  These just exist to implement the
     167             :      optimization that accounts referenced a single time aren't
     168             :      allocated a bit, but this seems to be an important optimization. */
     169             :   fd_pack_ord_txn_t * first_instance;
     170             :   int                 first_instance_was_write;
     171             : 
     172             :   /* bit is in [0, FD_PACK_BITSET_MAX) U
     173             :      { FD_PACK_BITSET_FIRST_INSTANCE, FD_PACK_BITSET_SLOWPATH }. */
     174             :   ushort              bit;
     175             : };
     176             : typedef struct fd_pack_bitset_acct_mapping fd_pack_bitset_acct_mapping_t;
     177             : 
     178             : 
     179             : 
     180             : /* pack maintains a small state machine related to initializer bundles.
     181             :    See the header file for more details about it, but it's
     182             :    also summarized here:
     183             :    * NOT_INITIALIZED: The starting state for each block
     184             :    * PENDING: an initializer bundle has been scheduled, but pack has
     185             :      not observed its result yet, so we don't know if it was successful
     186             :      or not.
     187             :    * FAILED: the most recently scheduled initializer bundle failed
     188             :      for reasons other than already being executed.  Most commonly, this
     189             :      could be because of a bug in the code that generated the
     190             :      initializer bundle, a lack of fee payer balance, or an expired
     191             :      blockhash.
     192             :    * READY: the most recently scheduled initialization bundle succeeded
     193             :      and normal bundles can be scheduled in this slot. */
     194           0 : #define FD_PACK_IB_STATE_NOT_INITIALIZED 0
     195           0 : #define FD_PACK_IB_STATE_PENDING         1
     196           0 : #define FD_PACK_IB_STATE_FAILED          2
     197           0 : #define FD_PACK_IB_STATE_READY           3
     198             : 
     199             : 
     200             : /* Returns 1 if x.rewards/x.compute < y.rewards/y.compute. Not robust. */
     201           0 : #define COMPARE_WORSE(x,y) ( ((ulong)((x)->rewards)*(ulong)((y)->compute_est)) < ((ulong)((y)->rewards)*(ulong)((x)->compute_est)) )
     202             : 
     203             : /* Declare all the data structures */
     204             : 
     205             : 
     206             : /* Define the big max-"heap" that we pull transactions off to schedule.
     207             :    The priority is given by reward/compute.  We may want to add in some
     208             :    additional terms at a later point.  In order to cheaply remove nodes,
     209             :    we actually use a treap.  */
     210             : #define POOL_NAME       trp_pool
     211           0 : #define POOL_T          fd_pack_ord_txn_t
     212             : #define POOL_IDX_T      ushort
     213           0 : #define POOL_NEXT       parent
     214             : #include "../../util/tmpl/fd_pool.c"
     215             : 
     216             : #define TREAP_T         fd_pack_ord_txn_t
     217             : #define TREAP_NAME      treap
     218             : #define TREAP_QUERY_T   void *                                         /* We don't use query ... */
     219             : #define TREAP_CMP(a,b)  (__extension__({ (void)(a); (void)(b); -1; })) /* which means we don't need to give a real
     220             :                                                                           implementation to cmp either */
     221           0 : #define TREAP_IDX_T     ushort
     222             : #define TREAP_OPTIMIZE_ITERATION 1
     223           0 : #define TREAP_LT        COMPARE_WORSE
     224             : #include "../../util/tmpl/fd_treap.c"
     225             : 
     226             : 
     227             : #define MAP_NAME              sig2txn
     228             : #define MAP_OPTIMIZE_RANDOM_ACCESS_REMOVAL 1
     229             : #define MAP_MULTI              1
     230           0 : #define MAP_ELE_T              fd_pack_ord_txn_t
     231           0 : #define MAP_PREV               sigmap_prev
     232           0 : #define MAP_NEXT               sigmap_next
     233           0 : #define MAP_IDX_T              ushort
     234             : #define MAP_KEY_T              wrapped_sig_t
     235           0 : #define MAP_KEY                sig
     236           0 : #define MAP_KEY_EQ(k0,k1)      (!memcmp( (k0),(k1), FD_TXN_SIGNATURE_SZ) )
     237           0 : #define MAP_KEY_HASH(key,seed) fd_hash( (seed), (key), 64UL )
     238             : #include "../../util/tmpl/fd_map_chain.c"
     239             : 
     240             : 
     241             : /* noncemap: A map from (nonce account, nonce authority, recent
     242             :    blockhash) to a durable nonce transaction containing it.  We only
     243             :    want to allow one transaction in the pool at a time with a given
     244             :    (nonce account, recent blockhash) tuple value.  The question is: can
     245             :    adding this limitation cause us to throw out potentially valuable
     246             :    transaction?  The answer is yes, but only very rarely, and the
     247             :    savings are worth it.  Suppose we have durable nonce transactions t1
     248             :    and t2 that advance the same nonce account and have the same value
     249             :    for the recent blockhash.
     250             : 
     251             :    - If t1 lands on chain, then it will advance the nonce account, and
     252             :    t2 will certainly not land on chain.
     253             :    - If t1 fails with AlreadyExecuted, that means the nonce account was
     254             :    advanced when t1 landed in a previous block, so t2 will certainly not
     255             :    land on chain.
     256             :    - If t1 fails with BlockhashNotFound, then the nonce account was
     257             :    advanced in some previous transaction, so again, t2 will certainly
     258             :    not land on chain.
     259             :    - If t1 does not land on chain because of an issue with the fee
     260             :    payer, it's possible that t2 could land on chain if it used a
     261             :    different fee payer, but historical data shows this is unlikely.
     262             :    - If t1 does not land on chain because it is part of a bundle that
     263             :    fails for an unrelated reason, it's possible that t2 could land on
     264             :    chain, but again, historical data says this is rare.
     265             : 
     266             :    We need to include the nonce authority in the hash to prevent one
     267             :    user from being able to DoS another user. */
     268             : 
     269             : typedef struct {
     270             :   uchar const * recent_blockhash;
     271             :   fd_acct_addr_t const * nonce_acct;
     272             :   fd_acct_addr_t const * nonce_auth;
     273             : } noncemap_extract_t;
     274             : 
     275             : /* k must be a valid, durable nonce transaction.  No error checking is
     276             :    done. */
     277             : static inline void
     278             : noncemap_extract( fd_txn_e_t const   * k,
     279           0 :                   noncemap_extract_t * out ) {
     280           0 :   fd_txn_t const * txn = TXN(k->txnp);
     281           0 :   out->recent_blockhash = fd_txn_get_recent_blockhash( txn, k->txnp->payload );
     282             : 
     283           0 :   ulong nonce_idx = k->txnp->payload[ txn->instr[ 0 ].acct_off+0 ];
     284           0 :   ulong autho_idx = k->txnp->payload[ txn->instr[ 0 ].acct_off+2 ];
     285             : 
     286           0 :   ulong imm_cnt = fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
     287           0 :   fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, k->txnp->payload );
     288           0 :   fd_acct_addr_t const * alt_adj = k->alt_accts - imm_cnt;
     289           0 :   out->nonce_acct = fd_ptr_if( nonce_idx<imm_cnt, accts, alt_adj )+nonce_idx;
     290             :   /* The nonce authority must be a signer, so it must be an immediate
     291             :      account. */
     292           0 :   out->nonce_auth = accts+autho_idx;
     293           0 : }
     294             : 
     295             : static inline int
     296             : noncemap_key_eq_internal( fd_txn_e_t const * k0,
     297           0 :                           fd_txn_e_t const * k1 ) {
     298           0 :   noncemap_extract_t e0[1], e1[1];
     299           0 :   noncemap_extract( k0, e0 );
     300           0 :   noncemap_extract( k1, e1 );
     301             : 
     302           0 :   if( FD_UNLIKELY( memcmp( e0->recent_blockhash, e1->recent_blockhash, 32UL ) ) ) return 0;
     303           0 :   if( FD_UNLIKELY( memcmp( e0->nonce_acct,       e1->nonce_acct,       32UL ) ) ) return 0;
     304           0 :   if( FD_UNLIKELY( memcmp( e0->nonce_auth,       e1->nonce_auth,       32UL ) ) ) return 0;
     305           0 :   return 1;
     306           0 : }
     307             : 
     308             : static inline ulong
     309             : noncemap_key_hash_internal( ulong              seed,
     310           0 :                             fd_txn_e_t const * k ) {
     311             :   /* TODO: This takes >100 cycles! */
     312           0 :   noncemap_extract_t e[1];
     313           0 :   noncemap_extract( k, e );
     314           0 :   return fd_hash( seed,              e->recent_blockhash, 32UL ) ^
     315           0 :          fd_hash( seed+ 864394383UL, e->nonce_acct,       32UL ) ^
     316           0 :          fd_hash( seed+3818662446UL, e->nonce_auth,       32UL );
     317           0 : }
     318             : 
     319             : #define MAP_NAME               noncemap
     320             : #define MAP_OPTIMIZE_RANDOM_ACCESS_REMOVAL 1
     321             : #define MAP_MULTI              0
     322           0 : #define MAP_ELE_T              fd_pack_ord_txn_t
     323           0 : #define MAP_PREV               noncemap_prev
     324           0 : #define MAP_NEXT               noncemap_next
     325           0 : #define MAP_IDX_T              ushort
     326             : #define MAP_KEY_T              fd_txn_e_t
     327           0 : #define MAP_KEY                _txn_e
     328           0 : #define MAP_KEY_EQ(k0,k1)      noncemap_key_eq_internal( (k0), (k1) )
     329           0 : #define MAP_KEY_HASH(key,seed) noncemap_key_hash_internal( (seed), (key) )
     330             : #include "../../util/tmpl/fd_map_chain.c"
     331             : 
     332             : 
     333             : static const fd_acct_addr_t null_addr = { 0 };
     334             : 
     335             : #define MAP_NAME              acct_uses
     336           0 : #define MAP_T                 fd_pack_addr_use_t
     337           0 : #define MAP_KEY_T             fd_acct_addr_t
     338           0 : #define MAP_KEY_NULL          null_addr
     339             : #if FD_HAS_AVX
     340           0 : # define MAP_KEY_INVAL(k)     _mm256_testz_si256( wb_ldu( (k).b ), wb_ldu( (k).b ) )
     341             : #else
     342             : # define MAP_KEY_INVAL(k)     MAP_KEY_EQUAL(k, null_addr)
     343             : #endif
     344           0 : #define MAP_KEY_EQUAL(k0,k1)  (!memcmp((k0).b,(k1).b, FD_TXN_ACCT_ADDR_SZ))
     345             : #define MAP_KEY_EQUAL_IS_SLOW 1
     346             : #define MAP_MEMOIZE           0
     347           0 : #define MAP_KEY_HASH(key,s)   ((uint)fd_ulong_hash( fd_ulong_load_8( (key).b ) ))
     348             : #include "../../util/tmpl/fd_map_dynamic.c"
     349             : 
     350             : 
     351             : #define MAP_NAME              bitset_map
     352           0 : #define MAP_T                 fd_pack_bitset_acct_mapping_t
     353           0 : #define MAP_KEY_T             fd_acct_addr_t
     354           0 : #define MAP_KEY_NULL          null_addr
     355             : #if FD_HAS_AVX
     356           0 : # define MAP_KEY_INVAL(k)     _mm256_testz_si256( wb_ldu( (k).b ), wb_ldu( (k).b ) )
     357             : #else
     358             : # define MAP_KEY_INVAL(k)     MAP_KEY_EQUAL(k, null_addr)
     359             : #endif
     360           0 : #define MAP_KEY_EQUAL(k0,k1)  (!memcmp((k0).b,(k1).b, FD_TXN_ACCT_ADDR_SZ))
     361             : #define MAP_KEY_EQUAL_IS_SLOW 1
     362             : #define MAP_MEMOIZE           0
     363           0 : #define MAP_KEY_HASH(key,s)   ((uint)fd_ulong_hash( fd_ulong_load_8( (key).b ) ))
     364             : #include "../../util/tmpl/fd_map_dynamic.c"
     365             : 
     366             : 
     367             : /* Since transactions can also expire, we also maintain a parallel
     368             :    priority queue.  This means elements are simultaneously part of the
     369             :    treap (ordered by priority) and the expiration queue (ordered by
     370             :    expiration).  It's tempting to use the priority field of the treap
     371             :    for this purpose, but that can result in degenerate treaps in some
     372             :    cases. */
     373             : #define PRQ_NAME             expq
     374           0 : #define PRQ_T                fd_pack_expq_t
     375           0 : #define PRQ_TIMEOUT_T        ulong
     376           0 : #define PRQ_TIMEOUT          expires_at
     377           0 : #define PRQ_TMP_ST(p,t)      do {                                   \
     378           0 :                                (p)[0] = (t);                        \
     379           0 :                                t.txn->expq_idx = (ulong)((p)-heap); \
     380           0 :                              } while( 0 )
     381             : #include "../../util/tmpl/fd_prq.c"
     382             : 
     383             : /* With realistic traffic patterns, we often see many, many transactions
     384             :    competing for the same writable account.  Since only one of these can
     385             :    execute at a time, we sometimes waste lots of scheduling time going
     386             :    through them one at a time.  To combat that, when a transaction
     387             :    writes to an account with more than PENALTY_TREAP_THRESHOLD
     388             :    references (readers or writers), instead of inserting it into the
     389             :    main treap, we insert it into a penalty treap for that specific hot
     390             :    account address.  These transactions are not immediately available
     391             :    for scheduling.  Then, when a transaction that writes to the hot
     392             :    address completes, we move the most lucrative transaction from the
     393             :    penalty treap to the main treap, making it available for scheduling.
     394             :    This policy may slightly violate the price-time priority scheduling
     395             :    approach pack normally uses: if the most lucrative transaction
     396             :    competing for hot state arrives after PENALTY_TREAP_THRESHOLD has
     397             :    been hit, it may be scheduled second instead of first.  However, if
     398             :    the account is in use at the time the new transaction arrives, it
     399             :    will be scheduled next, as desired.  This minor difference seems
     400             :    reasonable to reduce complexity.
     401             : 
     402             :    fd_pack_penalty_treap is one account-specific penalty treap.  All the
     403             :    transactions in the penalty_treap treap write to key.
     404             : 
     405             :    penalty_map is the fd_map_dynamic that maps accounts to their
     406             :    respective penalty treaps. */
     407             : struct fd_pack_penalty_treap {
     408             :   fd_acct_addr_t key;
     409             :   treap_t penalty_treap[1];
     410             : };
     411             : typedef struct fd_pack_penalty_treap fd_pack_penalty_treap_t;
     412             : 
     413             : #define MAP_NAME              penalty_map
     414           0 : #define MAP_T                 fd_pack_penalty_treap_t
     415           0 : #define MAP_KEY_T             fd_acct_addr_t
     416           0 : #define MAP_KEY_NULL          null_addr
     417             : #if FD_HAS_AVX
     418           0 : # define MAP_KEY_INVAL(k)     _mm256_testz_si256( wb_ldu( (k).b ), wb_ldu( (k).b ) )
     419             : #else
     420             : # define MAP_KEY_INVAL(k)     MAP_KEY_EQUAL(k, null_addr)
     421             : #endif
     422           0 : #define MAP_KEY_EQUAL(k0,k1)  (!memcmp((k0).b,(k1).b, FD_TXN_ACCT_ADDR_SZ))
     423             : #define MAP_KEY_EQUAL_IS_SLOW 1
     424             : #define MAP_MEMOIZE           0
     425           0 : #define MAP_KEY_HASH(key,s)   ((uint)fd_ulong_hash( fd_ulong_load_8( (key).b ) ))
     426             : #include "../../util/tmpl/fd_map_dynamic.c"
     427             : 
     428             : /* PENALTY_TREAP_THRESHOLD: How many references to an account do we
     429             :    allow before subsequent transactions that write to the account go to
     430             :    the penalty treap. */
     431           0 : #define PENALTY_TREAP_THRESHOLD 64UL
     432             : 
     433             : 
     434             : /* FD_PACK_SKIP_CNT: How many times we'll skip a transaction (for
     435             :    reasons other than account conflicts) before we won't consider it
     436             :    until the next slot.  For performance reasons, this doesn't reset at
     437             :    the end of a slot, so e.g. we might skip twice in slot 1, then three
     438             :    times in slot 2, which would be enough to prevent considering it
     439             :    until slot 3.  The main reason this is not 1 is that some skips that
     440             :    seem permanent until the end of the slot can actually go away based
     441             :    on rebates. */
     442           0 : #define FD_PACK_SKIP_CNT 50UL
     443             : 
     444             : /* Finally, we can now declare the main pack data structure */
     445             : struct fd_pack_private {
     446             :   ulong      pack_depth;
     447             :   ulong      bundle_meta_sz; /* if 0, bundles are disabled */
     448             :   ulong      bank_tile_cnt;
     449             : 
     450             :   fd_pack_limits_t lim[1];
     451             : 
     452             :   ulong      pending_txn_cnt; /* Summed across all treaps */
     453             :   ulong      microblock_cnt; /* How many microblocks have we
     454             :                                 generated in this block? */
     455             :   ulong      data_bytes_consumed; /* How much data is in this block so
     456             :                                      far ? */
     457             :   /* There's a limit on the total amount that transactions in a block
     458             :      can allocate.  How much of that limit have we consumed? */
     459             :   ulong      alloc_consumed;
     460             : 
     461             :   /* counters / gauge for schedule outcome enums */
     462             :   ulong      sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_CNT ];
     463             : 
     464             :   fd_rng_t * rng;
     465             : 
     466             :   ulong      cumulative_block_cost;
     467             :   ulong      cumulative_vote_cost;
     468             : 
     469             :   /* expire_before: Any transactions with expires_at strictly less than
     470             :      the current expire_before are removed from the available pending
     471             :      transaction.  Here, "expire" is used as a verb: cause all
     472             :      transactions before this time to expire. */
     473             :   ulong      expire_before;
     474             : 
     475             :   /* outstanding_microblock_mask: a bitmask indicating which banking
     476             :      tiles have outstanding microblocks, i.e. fd_pack has generated a
     477             :      microblock for that banking tile and the banking tile has not yet
     478             :      notified fd_pack that it has completed it. */
     479             :   ulong      outstanding_microblock_mask;
     480             : 
     481             :   /* The actual footprint for the pool and maps is allocated
     482             :      in the same order in which they are declared immediately following
     483             :      the struct.  I.e. these pointers point to memory not far after the
     484             :      struct.  The trees are just pointers into the pool so don't take up
     485             :      more space. */
     486             : 
     487             :   fd_pack_ord_txn_t * pool;
     488             : 
     489             :   /* Treaps (sorted by priority) of pending transactions.  We store the
     490             :      pending simple votes and transactions that come from bundles
     491             :      separately. */
     492             :   treap_t pending[1];
     493             :   treap_t pending_votes[1];
     494             :   treap_t pending_bundles[1];
     495             : 
     496             :   /* penalty_treaps: an fd_map_dynamic mapping hotly contended account
     497             :      addresses to treaps of transactions that write to them.  We try not
     498             :      to allow more than roughly PENALTY_TREAP_THRESHOLD transactions in
     499             :      the main treap that write to each account, though this is not
     500             :      exact. */
     501             :   fd_pack_penalty_treap_t * penalty_treaps;
     502             : 
     503             :   /* initializer_bundle_state: The current state of the initialization
     504             :      bundle state machine.  One of the FD_PACK_IB_STATE_* values.  See
     505             :      the long comment in the header and the comments attached to the
     506             :      respective values for a discussion of what each state means and the
     507             :      transitions between them. */
     508             :   int   initializer_bundle_state;
     509             : 
     510             :   /* relative_bundle_idx: the number of bundles that have been inserted
     511             :      since the last time pending_bundles was empty.  See the long
     512             :      comment about encoding this index in the rewards field of each
     513             :      transaction in the bundle, and why it is important that this reset
     514             :      to 0 as frequently as possible. */
     515             :   ulong relative_bundle_idx;
     516             : 
     517             :   /* pending{_votes}_smallest: keep a conservative estimate of the
     518             :      smallest transaction (by cost units and by bytes) in each heap.
     519             :      Both CUs and bytes should be set to ULONG_MAX is the treap is
     520             :      empty. */
     521             :   fd_pack_smallest_t pending_smallest[1];
     522             :   fd_pack_smallest_t pending_votes_smallest[1];
     523             : 
     524             :   /* expiration_q: At the same time that a transaction is in exactly one
     525             :      of the above treaps, it is also in the expiration queue, sorted by
     526             :      its expiration time.  This enables deleting all transactions that
     527             :      have expired, regardless of which treap they are in. */
     528             :   fd_pack_expq_t * expiration_q;
     529             : 
     530             :   /* acct_in_use: Map from account address to bitmask indicating which
     531             :      bank tiles are using the account and whether that use is read or
     532             :      write (msb). */
     533             :   fd_pack_addr_use_t   * acct_in_use;
     534             : 
     535             :   /* bitset_{w, rw}_in_use stores a subset of the information in
     536             :      acct_in_use using the compressed set format explained at the top of
     537             :      this file.  rw_in_use stores accounts in use for read or write
     538             :      while w_in_use stores only those in use for write. */
     539             :   FD_PACK_BITSET_DECLARE( bitset_rw_in_use );
     540             :   FD_PACK_BITSET_DECLARE( bitset_w_in_use  );
     541             : 
     542             :   /* writer_costs: Map from account addresses to the sum of costs of
     543             :      transactions that write to the account.  Used for enforcing limits
     544             :      on the max write cost per account per block. */
     545             :   fd_pack_addr_use_t   * writer_costs;
     546             : 
     547             :   /* top_writers: A simple max heap of the top 5 writers in the slot,
     548             :      used by downstream consumers for monitoring purposes. */
     549             :   fd_pack_addr_use_t top_writers[ FD_PACK_TOP_WRITERS_CNT ];
     550             : 
     551             :   /* At the end of every slot, we have to clear out writer_costs.  The
     552             :      map is large, but typically very sparsely populated.  As an
     553             :      optimization, we keep track of the elements of the map that we've
     554             :      actually used, up to a maximum.  If we use more than the maximum,
     555             :      we revert to the old way of just clearing the whole map.
     556             : 
     557             :      written_list indexed [0, written_list_cnt).
     558             :      written_list_cnt in  [0, written_list_max).
     559             : 
     560             :      written_list_cnt==written_list_max-1 means that the list may be
     561             :      incomplete and should be ignored. */
     562             :   fd_pack_addr_use_t * * written_list;
     563             :   ulong                  written_list_cnt;
     564             :   ulong                  written_list_max;
     565             : 
     566             :   /* Noncemap is a map_chain that maps from tuples (nonce account,
     567             :      recent blockhash value, nonce authority) to a transaction.  This
     568             :      map stores exactly the transactions in pool that have the nonce
     569             :      flag set. */
     570             :   noncemap_t * noncemap;
     571             : 
     572             :   sig2txn_t * signature_map; /* Stores pointers into pool for deleting by signature */
     573             : 
     574             :   /* bundle_temp_map: A fd_map_dynamic (although it could be an fd_map)
     575             :      used during fd_pack_try_schedule_bundle to store information about
     576             :      what accounts are used by transactions in the bundle.  It's empty
     577             :      (in a map sense) outside of calls to try_schedule_bundle, and each
     578             :      call to try_schedule_bundle clears it after use.  If bundles are
     579             :      disabled, this is a valid fd_map_dynamic, but it's as small as
     580             :      convenient and remains empty. */
     581             :   fd_pack_addr_use_t * bundle_temp_map;
     582             : 
     583             : 
     584             :   /* use_by_bank: An array of size (max_txn_per_microblock *
     585             :      FD_TXN_ACCT_ADDR_MAX) for each banking tile.  Only the MSB of
     586             :      in_use_by is relevant.  Addressed use_by_bank[i][j] where i is in
     587             :      [0, bank_tile_cnt) and j is in [0, use_by_bank_cnt[i]).  Used
     588             :      mostly for clearing the proper bits of acct_in_use when a
     589             :      microblock finishes.
     590             : 
     591             :      use_by_bank_txn: indexed [i][j], where i is in [0, bank_tile_cnt)
     592             :      and j is in [0, max_txn_per_microblock).  Transaction j in the
     593             :      microblock currently scheduled to bank i uses account addresses in
     594             :      use_by_bank[i][k] where k is in [0, use_by_bank[i][j]).  For
     595             :      example, if use_by_bank[i][0] = 2 and use_by_bank[i][1] = 3, then
     596             :      all the accounts that the first transaction in the outstanding
     597             :      microblock for bank 0 uses are contained in the set
     598             :                { use_by_bank[i][0], use_by_bank[i][1] },
     599             :      and all the accounts in the second transaction in the microblock
     600             :      are in the set
     601             :         { use_by_bank[i][0], use_by_bank[i][1], use_by_bank[i][2] }.
     602             :      Each transaction writes to at least one account (the fee payer)
     603             :      that no other transaction scheduled to the bank uses, which means
     604             :      that use_by_bank_txn[i][j] - use_by_bank_txn[i][j-1] >= 1 (with 0
     605             :      for use_by_bank_txn[i][-1]).  This means we can stop iterating when
     606             :      use_by_bank_txn[i][j] == use_by_bank_cnt[i].  */
     607             :   fd_pack_addr_use_t * use_by_bank    [ FD_PACK_MAX_EXECLE_TILES ];
     608             :   ulong                use_by_bank_cnt[ FD_PACK_MAX_EXECLE_TILES ];
     609             :   ulong *              use_by_bank_txn[ FD_PACK_MAX_EXECLE_TILES ];
     610             : 
     611             :   fd_histf_t txn_per_microblock [ 1 ];
     612             :   fd_histf_t vote_per_microblock[ 1 ];
     613             : 
     614             :   fd_histf_t scheduled_cus_per_block[ 1 ];
     615             :   fd_histf_t rebated_cus_per_block  [ 1 ];
     616             :   fd_histf_t net_cus_per_block      [ 1 ];
     617             :   fd_histf_t pct_cus_per_block      [ 1 ];
     618             :   ulong      cumulative_rebated_cus;
     619             : 
     620             : 
     621             :   /* compressed_slot_number: a number in (FD_PACK_SKIP_CNT, USHORT_MAX]
     622             :      that advances each time we start packing for a new slot. */
     623             :   ushort     compressed_slot_number;
     624             : 
     625             :   /* bitset_avail: a stack of which bits are not currently reserved and
     626             :      can be used to represent an account address.
     627             :      Indexed [0, bitset_avail_cnt].  Element 0 is fixed at
     628             :      FD_PACK_BITSET_SLOWPATH. */
     629             :   ushort bitset_avail[ 1UL+FD_PACK_BITSET_MAX ];
     630             :   ulong  bitset_avail_cnt;
     631             : 
     632             :   /* acct_to_bitset: an fd_map_dynamic that maps acct addresses to the
     633             :      reference count, which bit, etc. */
     634             :   fd_pack_bitset_acct_mapping_t * acct_to_bitset;
     635             : 
     636             :   /* chdkup: scratch memory chkdup needs for its internal processing */
     637             :   fd_chkdup_t chkdup[ 1 ];
     638             : 
     639             :   /* bundle_meta: an array, parallel to the pool, with each element
     640             :      having size bundle_meta_sz.  I.e. if pool[i] has an associated
     641             :      bundle meta, it's located at bundle_meta[j] for j in
     642             :      [i*bundle_meta_sz, (i+1)*bundle_meta_sz). */
     643             :   void * bundle_meta;
     644             : };
     645             : 
     646             : typedef struct fd_pack_private fd_pack_t;
     647             : 
     648             : FD_STATIC_ASSERT( offsetof(fd_pack_t, pending_txn_cnt)==FD_PACK_PENDING_TXN_CNT_OFF, txn_cnt_off );
     649             : 
     650             : /* Forward-declare some helper functions */
     651             : static ulong delete_transaction( fd_pack_t * pack, fd_pack_ord_txn_t * txn, int delete_full_bundle, int move_from_penalty_treap );
     652             : static inline void insert_bundle_impl( fd_pack_t * pack, ulong bundle_idx, ulong txn_cnt, fd_pack_ord_txn_t * * bundle, ulong expires_at );
     653             : 
     654             : FD_FN_PURE ulong
     655             : fd_pack_footprint( ulong                    pack_depth,
     656             :                    ulong                    bundle_meta_sz,
     657             :                    ulong                    bank_tile_cnt,
     658           0 :                    fd_pack_limits_t const * limits         ) {
     659           0 :   if( FD_UNLIKELY( (bank_tile_cnt==0) | (bank_tile_cnt>FD_PACK_MAX_EXECLE_TILES) ) ) return 0UL;
     660           0 :   if( FD_UNLIKELY( pack_depth<4UL ) ) return 0UL;
     661             : 
     662           0 :   int enable_bundles = !!bundle_meta_sz;
     663           0 :   ulong l;
     664           0 :   ulong extra_depth        = fd_ulong_if( enable_bundles, 1UL+2UL*FD_PACK_MAX_TXN_PER_BUNDLE, 1UL ); /* space for use between init and fini */
     665           0 :   ulong max_acct_in_treap  = pack_depth * FD_TXN_ACCT_ADDR_MAX;
     666           0 :   ulong max_txn_per_mblk   = fd_ulong_max( limits->max_txn_per_microblock,
     667           0 :                                            fd_ulong_if( enable_bundles, FD_PACK_MAX_TXN_PER_BUNDLE, 0UL ) );
     668           0 :   ulong max_acct_in_flight = bank_tile_cnt * (FD_TXN_ACCT_ADDR_MAX * max_txn_per_mblk + 1UL);
     669           0 :   ulong max_txn_in_flight  = bank_tile_cnt * max_txn_per_mblk;
     670             : 
     671           0 :   ulong max_w_per_block    = fd_ulong_min( limits->max_cost_per_block / FD_PACK_COST_PER_WRITABLE_ACCT,
     672           0 :                                            max_txn_per_mblk * limits->max_microblocks_per_block * FD_TXN_ACCT_ADDR_MAX );
     673           0 :   ulong written_list_max   = fd_ulong_min( max_w_per_block>>1, DEFAULT_WRITTEN_LIST_MAX );
     674           0 :   ulong bundle_temp_accts  = fd_ulong_if( enable_bundles, FD_PACK_MAX_TXN_PER_BUNDLE*FD_TXN_ACCT_ADDR_MAX, 1UL );
     675           0 :   ulong sig_chain_cnt      = sig2txn_chain_cnt_est( pack_depth );
     676           0 :   ulong nonce_chain_cnt    = noncemap_chain_cnt_est( pack_depth );
     677             : 
     678             :   /* log base 2, but with a 2* so that the hash table stays sparse */
     679           0 :   int lg_uses_tbl_sz = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_flight                        ) );
     680           0 :   int lg_max_writers = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_w_per_block                           ) );
     681           0 :   int lg_acct_in_trp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_treap                         ) );
     682           0 :   int lg_penalty_trp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_treap/PENALTY_TREAP_THRESHOLD ) );
     683           0 :   int lg_bundle_temp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*bundle_temp_accts                         ) );
     684             : 
     685           0 :   l = FD_LAYOUT_INIT;
     686           0 :   l = FD_LAYOUT_APPEND( l, FD_PACK_ALIGN,       sizeof(fd_pack_t)                               );
     687           0 :   l = FD_LAYOUT_APPEND( l, trp_pool_align (),   trp_pool_footprint ( pack_depth+extra_depth   ) ); /* pool           */
     688           0 :   l = FD_LAYOUT_APPEND( l, penalty_map_align(), penalty_map_footprint( lg_penalty_trp         ) ); /* penalty_treaps */
     689           0 :   l = FD_LAYOUT_APPEND( l, expq_align     (),   expq_footprint     ( pack_depth               ) ); /* expiration prq */
     690           0 :   l = FD_LAYOUT_APPEND( l, acct_uses_align(),   acct_uses_footprint( lg_uses_tbl_sz           ) ); /* acct_in_use    */
     691           0 :   l = FD_LAYOUT_APPEND( l, acct_uses_align(),   acct_uses_footprint( lg_max_writers           ) ); /* writer_costs   */
     692           0 :   l = FD_LAYOUT_APPEND( l, 32UL,                sizeof(fd_pack_addr_use_t*)*written_list_max    ); /* written_list   */
     693           0 :   l = FD_LAYOUT_APPEND( l, noncemap_align (),   noncemap_footprint ( nonce_chain_cnt          ) ); /* noncemap       */
     694           0 :   l = FD_LAYOUT_APPEND( l, sig2txn_align  (),   sig2txn_footprint  ( sig_chain_cnt            ) ); /* signature_map  */
     695           0 :   l = FD_LAYOUT_APPEND( l, acct_uses_align(),   acct_uses_footprint( lg_bundle_temp           ) ); /* bundle_temp_map*/
     696           0 :   l = FD_LAYOUT_APPEND( l, 32UL,                sizeof(fd_pack_addr_use_t)*max_acct_in_flight   ); /* use_by_bank    */
     697           0 :   l = FD_LAYOUT_APPEND( l, 32UL,                sizeof(ulong)*max_txn_in_flight                 ); /* use_by_bank_txn*/
     698           0 :   l = FD_LAYOUT_APPEND( l, bitset_map_align(),  bitset_map_footprint( lg_acct_in_trp          ) ); /* acct_to_bitset */
     699           0 :   l = FD_LAYOUT_APPEND( l, 64UL,                (pack_depth+extra_depth)*bundle_meta_sz         ); /* bundle_meta */
     700           0 :   return FD_LAYOUT_FINI( l, FD_PACK_ALIGN );
     701           0 : }
     702             : 
     703             : void *
     704             : fd_pack_new( void                   * mem,
     705             :              ulong                    pack_depth,
     706             :              ulong                    bundle_meta_sz,
     707             :              ulong                    bank_tile_cnt,
     708             :              fd_pack_limits_t const * limits,
     709           0 :              fd_rng_t               * rng           ) {
     710             : 
     711           0 :   int enable_bundles = !!bundle_meta_sz;
     712           0 :   ulong extra_depth        = fd_ulong_if( enable_bundles, 1UL+2UL*FD_PACK_MAX_TXN_PER_BUNDLE, 1UL );
     713           0 :   ulong max_acct_in_treap  = pack_depth * FD_TXN_ACCT_ADDR_MAX;
     714           0 :   ulong max_txn_per_mblk   = fd_ulong_max( limits->max_txn_per_microblock,
     715           0 :                                            fd_ulong_if( enable_bundles, FD_PACK_MAX_TXN_PER_BUNDLE, 0UL ) );
     716           0 :   ulong max_acct_in_flight = bank_tile_cnt * (FD_TXN_ACCT_ADDR_MAX * max_txn_per_mblk + 1UL);
     717           0 :   ulong max_txn_in_flight  = bank_tile_cnt * max_txn_per_mblk;
     718             : 
     719           0 :   ulong max_w_per_block    = fd_ulong_min( limits->max_cost_per_block / FD_PACK_COST_PER_WRITABLE_ACCT,
     720           0 :                                            max_txn_per_mblk * limits->max_microblocks_per_block * FD_TXN_ACCT_ADDR_MAX );
     721           0 :   ulong written_list_max   = fd_ulong_min( max_w_per_block>>1, DEFAULT_WRITTEN_LIST_MAX );
     722           0 :   ulong bundle_temp_accts  = fd_ulong_if( enable_bundles, FD_PACK_MAX_TXN_PER_BUNDLE*FD_TXN_ACCT_ADDR_MAX, 1UL );
     723           0 :   ulong sig_chain_cnt      = sig2txn_chain_cnt_est( pack_depth );
     724           0 :   ulong nonce_chain_cnt    = noncemap_chain_cnt_est( pack_depth );
     725             : 
     726             :   /* log base 2, but with a 2* so that the hash table stays sparse */
     727           0 :   int lg_uses_tbl_sz = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_flight                        ) );
     728           0 :   int lg_max_writers = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_w_per_block                           ) );
     729           0 :   int lg_acct_in_trp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_treap                         ) );
     730           0 :   int lg_penalty_trp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_treap/PENALTY_TREAP_THRESHOLD ) );
     731           0 :   int lg_bundle_temp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*bundle_temp_accts                         ) );
     732             : 
     733           0 :   FD_SCRATCH_ALLOC_INIT( l, mem );
     734           0 :   fd_pack_t * pack    = FD_SCRATCH_ALLOC_APPEND( l,  FD_PACK_ALIGN,       sizeof(fd_pack_t)                             );
     735             :   /* The pool has one extra element that is used between insert_init and
     736             :      cancel/fini. */
     737           0 :   void * _pool        = FD_SCRATCH_ALLOC_APPEND( l,  trp_pool_align(),    trp_pool_footprint ( pack_depth+extra_depth ) );
     738           0 :   void * _penalty_map = FD_SCRATCH_ALLOC_APPEND( l,  penalty_map_align(), penalty_map_footprint( lg_penalty_trp       ) );
     739           0 :   void * _expq        = FD_SCRATCH_ALLOC_APPEND( l,  expq_align(),        expq_footprint     ( pack_depth             ) );
     740           0 :   void * _uses        = FD_SCRATCH_ALLOC_APPEND( l,  acct_uses_align(),   acct_uses_footprint( lg_uses_tbl_sz         ) );
     741           0 :   void * _writer_cost = FD_SCRATCH_ALLOC_APPEND( l,  acct_uses_align(),   acct_uses_footprint( lg_max_writers         ) );
     742           0 :   void * _written_lst = FD_SCRATCH_ALLOC_APPEND( l,  32UL,                sizeof(fd_pack_addr_use_t*)*written_list_max  );
     743           0 :   void * _noncemap    = FD_SCRATCH_ALLOC_APPEND( l,  noncemap_align(),    noncemap_footprint ( nonce_chain_cnt        ) );
     744           0 :   void * _sig_map     = FD_SCRATCH_ALLOC_APPEND( l,  sig2txn_align(),     sig2txn_footprint  ( sig_chain_cnt          ) );
     745           0 :   void * _bundle_temp = FD_SCRATCH_ALLOC_APPEND( l,  acct_uses_align(),   acct_uses_footprint( lg_bundle_temp         ) );
     746           0 :   void * _use_by_bank = FD_SCRATCH_ALLOC_APPEND( l,  32UL,                sizeof(fd_pack_addr_use_t)*max_acct_in_flight );
     747           0 :   void * _use_by_txn  = FD_SCRATCH_ALLOC_APPEND( l,  32UL,                sizeof(ulong)*max_txn_in_flight               );
     748           0 :   void * _acct_bitset = FD_SCRATCH_ALLOC_APPEND( l,  bitset_map_align(),  bitset_map_footprint( lg_acct_in_trp        ) );
     749           0 :   void * bundle_meta  = FD_SCRATCH_ALLOC_APPEND( l,  64UL,                (pack_depth+extra_depth)*bundle_meta_sz       );
     750             : 
     751           0 :   pack->pack_depth                  = pack_depth;
     752           0 :   pack->bundle_meta_sz              = bundle_meta_sz;
     753           0 :   pack->bank_tile_cnt               = bank_tile_cnt;
     754           0 :   pack->lim[0]                      = *limits;
     755           0 :   pack->pending_txn_cnt             = 0UL;
     756           0 :   pack->microblock_cnt              = 0UL;
     757           0 :   pack->data_bytes_consumed         = 0UL;
     758           0 :   pack->alloc_consumed              = 0UL;
     759           0 :   memset( pack->sched_results, 0, sizeof(pack->sched_results) );
     760           0 :   pack->rng                         = rng;
     761           0 :   pack->cumulative_block_cost       = 0UL;
     762           0 :   pack->cumulative_vote_cost        = 0UL;
     763           0 :   pack->expire_before               = 0UL;
     764           0 :   pack->outstanding_microblock_mask = 0UL;
     765           0 :   pack->cumulative_rebated_cus      = 0UL;
     766             : 
     767             : 
     768           0 :   trp_pool_new(  _pool,        pack_depth+extra_depth );
     769             : 
     770           0 :   fd_pack_ord_txn_t * pool = trp_pool_join( _pool );
     771           0 :   treap_seed( pool, pack_depth+extra_depth, fd_rng_ulong( rng ) );
     772           0 :   for( ulong i=0UL; i<pack_depth+extra_depth; i++ ) pool[i].root = FD_ORD_TXN_ROOT_FREE;
     773             : 
     774           0 :   (void)trp_pool_leave( pool );
     775             : 
     776           0 :   penalty_map_new( _penalty_map, lg_penalty_trp, 0UL );
     777             : 
     778             :   /* These treaps can have at most pack_depth elements at any moment,
     779             :      but they come from a pool of size pack_depth+extra_depth. */
     780           0 :   treap_new( (void*)pack->pending,         pack_depth+extra_depth );
     781           0 :   treap_new( (void*)pack->pending_votes,   pack_depth+extra_depth );
     782           0 :   treap_new( (void*)pack->pending_bundles, pack_depth+extra_depth );
     783             : 
     784           0 :   pack->pending_smallest->cus         = ULONG_MAX;
     785           0 :   pack->pending_smallest->bytes       = ULONG_MAX;
     786           0 :   pack->pending_votes_smallest->cus   = ULONG_MAX;
     787           0 :   pack->pending_votes_smallest->bytes = ULONG_MAX;
     788             : 
     789           0 :   expq_new( _expq, pack_depth );
     790             : 
     791           0 :   FD_PACK_BITSET_CLEAR( pack->bitset_rw_in_use );
     792           0 :   FD_PACK_BITSET_CLEAR( pack->bitset_w_in_use  );
     793             : 
     794           0 :   acct_uses_new( _uses,        lg_uses_tbl_sz, 0UL );
     795           0 :   acct_uses_new( _writer_cost, lg_max_writers, 0UL );
     796           0 :   acct_uses_new( _bundle_temp, lg_bundle_temp, 0UL );
     797             : 
     798           0 :   pack->written_list     = _written_lst;
     799           0 :   pack->written_list_cnt = 0UL;
     800           0 :   pack->written_list_max = written_list_max;
     801             : 
     802           0 :   noncemap_new( _noncemap, nonce_chain_cnt, fd_rng_ulong( rng ) );
     803             : 
     804           0 :   sig2txn_new( _sig_map, sig_chain_cnt, fd_rng_ulong( rng ) );
     805             : 
     806           0 :   fd_pack_addr_use_t * use_by_bank     = (fd_pack_addr_use_t *)_use_by_bank;
     807           0 :   ulong *              use_by_bank_txn = (ulong *)_use_by_txn;
     808           0 :   for( ulong i=0UL; i<bank_tile_cnt; i++ ) {
     809           0 :     pack->use_by_bank    [i] = use_by_bank + i*(FD_TXN_ACCT_ADDR_MAX*max_txn_per_mblk+1UL);
     810           0 :     pack->use_by_bank_cnt[i] = 0UL;
     811           0 :     pack->use_by_bank_txn[i] = use_by_bank_txn + i*max_txn_per_mblk;
     812           0 :     pack->use_by_bank_txn[i][0] = 0UL;
     813           0 :   }
     814           0 :   for( ulong i=bank_tile_cnt; i<FD_PACK_MAX_EXECLE_TILES; i++ ) {
     815           0 :     pack->use_by_bank    [i] = NULL;
     816           0 :     pack->use_by_bank_cnt[i] = 0UL;
     817           0 :     pack->use_by_bank_txn[i] = NULL;
     818           0 :   }
     819             : 
     820           0 :   fd_histf_new( pack->txn_per_microblock,  FD_MHIST_MIN( PACK, TOTAL_TRANSACTIONS_PER_MICROBLOCK_COUNT ),
     821           0 :                                            FD_MHIST_MAX( PACK, TOTAL_TRANSACTIONS_PER_MICROBLOCK_COUNT ) );
     822           0 :   fd_histf_new( pack->vote_per_microblock, FD_MHIST_MIN( PACK, VOTES_PER_MICROBLOCK_COUNT ),
     823           0 :                                            FD_MHIST_MAX( PACK, VOTES_PER_MICROBLOCK_COUNT ) );
     824             : 
     825           0 :   fd_histf_new( pack->scheduled_cus_per_block, FD_MHIST_MIN( PACK, CUS_SCHEDULED ),
     826           0 :                                                FD_MHIST_MAX( PACK, CUS_SCHEDULED ) );
     827           0 :   fd_histf_new( pack->rebated_cus_per_block,   FD_MHIST_MIN( PACK, CUS_REBATED   ),
     828           0 :                                                FD_MHIST_MAX( PACK, CUS_REBATED   ) );
     829           0 :   fd_histf_new( pack->net_cus_per_block,       FD_MHIST_MIN( PACK, CUS_NET       ),
     830           0 :                                                FD_MHIST_MAX( PACK, CUS_NET       ) );
     831           0 :   fd_histf_new( pack->pct_cus_per_block,       FD_MHIST_MIN( PACK, CUS_PCT       ),
     832           0 :                                                FD_MHIST_MAX( PACK, CUS_PCT       ) );
     833             : 
     834           0 :   pack->compressed_slot_number = (ushort)(FD_PACK_SKIP_CNT+1);
     835             : 
     836           0 :   pack->bitset_avail[ 0 ] = FD_PACK_BITSET_SLOWPATH;
     837           0 :   for( ulong i=0UL; i<FD_PACK_BITSET_MAX; i++ ) pack->bitset_avail[ i+1UL ] = (ushort)i;
     838           0 :   pack->bitset_avail_cnt = FD_PACK_BITSET_MAX;
     839             : 
     840           0 :   bitset_map_new( _acct_bitset, lg_acct_in_trp, 0UL );
     841             : 
     842           0 :   fd_chkdup_new( pack->chkdup, rng );
     843             : 
     844           0 :   pack->bundle_meta = bundle_meta;
     845             : 
     846           0 :   return mem;
     847           0 : }
     848             : 
     849             : fd_pack_t *
     850           0 : fd_pack_join( void * mem ) {
     851           0 :   FD_SCRATCH_ALLOC_INIT( l, mem );
     852           0 :   fd_pack_t * pack  = FD_SCRATCH_ALLOC_APPEND( l, FD_PACK_ALIGN, sizeof(fd_pack_t) );
     853             : 
     854           0 :   int enable_bundles = !!pack->bundle_meta_sz;
     855           0 :   ulong pack_depth             = pack->pack_depth;
     856           0 :   ulong extra_depth            = fd_ulong_if( enable_bundles, 1UL+2UL*FD_PACK_MAX_TXN_PER_BUNDLE, 1UL );
     857           0 :   ulong bank_tile_cnt          = pack->bank_tile_cnt;
     858           0 :   ulong max_txn_per_microblock = fd_ulong_max( pack->lim->max_txn_per_microblock,
     859           0 :                                                fd_ulong_if( enable_bundles, FD_PACK_MAX_TXN_PER_BUNDLE, 0UL ) );
     860             : 
     861           0 :   ulong max_acct_in_treap  = pack_depth * FD_TXN_ACCT_ADDR_MAX;
     862           0 :   ulong max_acct_in_flight = bank_tile_cnt * (FD_TXN_ACCT_ADDR_MAX * max_txn_per_microblock + 1UL);
     863           0 :   ulong max_txn_in_flight  = bank_tile_cnt * max_txn_per_microblock;
     864           0 :   ulong max_w_per_block    = fd_ulong_min( pack->lim->max_cost_per_block / FD_PACK_COST_PER_WRITABLE_ACCT,
     865           0 :                                            max_txn_per_microblock * pack->lim->max_microblocks_per_block * FD_TXN_ACCT_ADDR_MAX );
     866           0 :   ulong written_list_max   = fd_ulong_min( max_w_per_block>>1, DEFAULT_WRITTEN_LIST_MAX );
     867           0 :   ulong bundle_temp_accts  = fd_ulong_if( enable_bundles, FD_PACK_MAX_TXN_PER_BUNDLE*FD_TXN_ACCT_ADDR_MAX, 1UL );
     868           0 :   ulong sig_chain_cnt      = sig2txn_chain_cnt_est( pack_depth );
     869           0 :   ulong nonce_chain_cnt    = noncemap_chain_cnt_est( pack_depth );
     870             : 
     871           0 :   int lg_uses_tbl_sz = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_flight                        ) );
     872           0 :   int lg_max_writers = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_w_per_block                           ) );
     873           0 :   int lg_acct_in_trp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_treap                         ) );
     874           0 :   int lg_penalty_trp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_treap/PENALTY_TREAP_THRESHOLD ) );
     875           0 :   int lg_bundle_temp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*bundle_temp_accts                         ) );
     876             : 
     877             : 
     878           0 :   pack->pool          = trp_pool_join(   FD_SCRATCH_ALLOC_APPEND( l, trp_pool_align(),   trp_pool_footprint   ( pack_depth+extra_depth  ) ) );
     879           0 :   pack->penalty_treaps= penalty_map_join(FD_SCRATCH_ALLOC_APPEND( l, penalty_map_align(),penalty_map_footprint( lg_penalty_trp          ) ) );
     880           0 :   pack->expiration_q  = expq_join    (   FD_SCRATCH_ALLOC_APPEND( l, expq_align(),       expq_footprint       ( pack_depth              ) ) );
     881           0 :   pack->acct_in_use   = acct_uses_join(  FD_SCRATCH_ALLOC_APPEND( l, acct_uses_align(),  acct_uses_footprint  ( lg_uses_tbl_sz          ) ) );
     882           0 :   pack->writer_costs  = acct_uses_join(  FD_SCRATCH_ALLOC_APPEND( l, acct_uses_align(),  acct_uses_footprint  ( lg_max_writers          ) ) );
     883           0 :   /* */                                  FD_SCRATCH_ALLOC_APPEND( l, 32UL,               sizeof(fd_pack_addr_use_t*)*written_list_max       );
     884           0 :   pack->noncemap      = noncemap_join(   FD_SCRATCH_ALLOC_APPEND( l, noncemap_align(),   noncemap_footprint   ( nonce_chain_cnt         ) ) );
     885           0 :   pack->signature_map = sig2txn_join(    FD_SCRATCH_ALLOC_APPEND( l, sig2txn_align(),    sig2txn_footprint    ( sig_chain_cnt           ) ) );
     886           0 :   pack->bundle_temp_map=acct_uses_join(  FD_SCRATCH_ALLOC_APPEND( l, acct_uses_align(),  acct_uses_footprint  ( lg_bundle_temp          ) ) );
     887           0 :   /* */                                  FD_SCRATCH_ALLOC_APPEND( l, 32UL,               sizeof(fd_pack_addr_use_t)*max_acct_in_flight      );
     888           0 :   /* */                                  FD_SCRATCH_ALLOC_APPEND( l, 32UL,               sizeof(ulong)*max_txn_in_flight                    );
     889           0 :   pack->acct_to_bitset= bitset_map_join( FD_SCRATCH_ALLOC_APPEND( l, bitset_map_align(), bitset_map_footprint( lg_acct_in_trp           ) ) );
     890           0 :   /* */                                  FD_SCRATCH_ALLOC_APPEND( l, 64UL,               (pack_depth+extra_depth)*pack->bundle_meta_sz      );
     891             : 
     892           0 :   FD_MGAUGE_SET( PACK, PENDING_TRANSACTIONS_HEAP_SIZE, pack->pack_depth );
     893           0 :   memset( pack->top_writers, 0, sizeof(pack->top_writers) );
     894             : 
     895           0 :   return pack;
     896           0 : }
     897             : 
     898             : 
     899             : /* Returns 0 on failure, 1 on success for a vote, 2 on success for a
     900             :    non-vote. */
     901             : static int
     902             : fd_pack_estimate_rewards_and_compute( fd_txn_e_t             * txne,
     903             :                                       fd_pack_ord_txn_t      * out,
     904           0 :                                       fd_pack_limits_t const * lim ) {
     905           0 :   fd_txn_t * txn = TXN(txne->txnp);
     906           0 :   ulong sig_rewards = FD_PACK_FEE_PER_SIGNATURE * txn->signature_cnt; /* Easily in [5000, 635000] */
     907             : 
     908           0 :   ulong requested_execution_cus;
     909           0 :   ulong priority_rewards;
     910           0 :   ulong precompile_sigs;
     911           0 :   ulong requested_loaded_accounts_data_cost;
     912           0 :   ulong allocated_data;
     913           0 :   ulong cost_estimate = fd_pack_compute_cost( txn, txne->txnp->payload, &txne->txnp->flags, &requested_execution_cus, &priority_rewards, &precompile_sigs, &requested_loaded_accounts_data_cost, &allocated_data );
     914             : 
     915           0 :   if( FD_UNLIKELY( !cost_estimate ) ) return 0;
     916             : 
     917             :   /* precompile_sigs <= 16320, so after the addition,
     918             :      sig_rewards < 83,000,000 */
     919           0 :   sig_rewards += FD_PACK_FEE_PER_SIGNATURE * precompile_sigs;
     920           0 :   sig_rewards = sig_rewards * FD_PACK_TXN_FEE_BURN_PCT / 100UL;
     921             : 
     922             :   /* No fancy CU estimation in this version of pack
     923             :   for( ulong i=0UL; i<(ulong)txn->instr_cnt; i++ ) {
     924             :     uchar prog_id_idx = txn->instr[ i ].program_id;
     925             :     fd_acct_addr_t const * acct_addr = fd_txn_get_acct_addrs( txn, txnp->payload ) + (ulong)prog_id_idx;
     926             :   }
     927             :   */
     928           0 :   out->rewards                              = (priority_rewards < (UINT_MAX - sig_rewards)) ? (uint)(sig_rewards + priority_rewards) : UINT_MAX;
     929           0 :   out->compute_est                          = (uint)cost_estimate;
     930           0 :   out->txn->pack_cu.requested_exec_plus_acct_data_cus = (uint)(requested_execution_cus + requested_loaded_accounts_data_cost);
     931           0 :   out->txn->pack_cu.non_execution_cus       = (uint)(cost_estimate - requested_execution_cus - requested_loaded_accounts_data_cost);
     932           0 :   out->txn->pack_alloc                      = (uint)allocated_data;
     933             : 
     934             :   /* If a transaction allocates a lot, we want to treat it as if it
     935             :      requests more CUs.  However, we use compute_est in the block
     936             :      limit calculations, so we can't touch it.  To have the same
     937             :      effect, we decrease rewards.
     938             :      divisor is 1 unless
     939             :             allocated_data                       cost_estimate
     940             :       ----------------------------    >=       ------------------
     941             :       max_allocated_data_per_block             max_cost_per_block
     942             : 
     943             :      0       <=allocated_data      <=20 * 1024^2
     944             :      48*10^6 <= max_cost_per_block < 2^32
     945             :      1020    <= cost_estimate      < 1.6 * 10^6
     946             :      max_allocated_data_per_block = 100 * 1000^2
     947             :      So the numerator (<2^57) and denominator (<2^48) can't overflow.
     948             :      1 <= divisor <= 1 + (max_cost_per_block * .000206)
     949             :      */
     950           0 :   ulong divisor = 1UL + (allocated_data * lim->max_cost_per_block) / (cost_estimate * lim->max_allocated_data_per_block);
     951           0 :   out->rewards /= (uint)divisor;
     952             : 
     953           0 :   return fd_int_if( txne->txnp->flags & FD_TXN_P_FLAGS_IS_SIMPLE_VOTE, 1, 2 );
     954           0 : }
     955             : 
     956             : /* Returns 0 on failure, 1 if not a durable nonce transaction, and 2 if
     957             :    it is.  FIXME: These return codes are set to harmonize with
     958             :    estimate_rewards_and_compute but -1/0/1 makes a lot more sense to me.
     959             :    */
     960             : static int
     961           0 : fd_pack_validate_durable_nonce( fd_txn_e_t * txne ) {
     962           0 :   fd_txn_t const * txn = TXN(txne->txnp);
     963             : 
     964             :   /* First instruction invokes system program with 4 bytes of
     965             :      instruction data with the little-endian value 4.  It also has 3
     966             :      accounts: the nonce account, recent blockhashes sysvar, and the
     967             :      nonce authority.  It seems like technically the nonce authority may
     968             :      not need to be passed in, but we disallow that.  We also allow
     969             :      trailing data and trailing accounts.  We want to organize the
     970             :      checks somewhat to minimize cache misses. */
     971           0 :   if( FD_UNLIKELY( txn->instr_cnt==0            ) ) return 1;
     972           0 :   if( FD_UNLIKELY( txn->instr[ 0 ].data_sz<4UL  ) ) return 1;
     973           0 :   if( FD_UNLIKELY( txn->instr[ 0 ].acct_cnt<3UL ) ) return 1; /* It seems like technically 2 is allowed, but never used */
     974           0 :   if( FD_LIKELY  ( fd_uint_load_4( txne->txnp->payload + txn->instr[ 0 ].data_off )!=4U ) ) return 1;
     975             :   /* The program has to be a static account */
     976           0 :   fd_acct_addr_t const * accts = fd_txn_get_acct_addrs( txn, txne->txnp->payload );
     977           0 :   if( FD_UNLIKELY( !fd_memeq( accts[ txn->instr[ 0 ].program_id ].b, null_addr.b, 32UL       ) ) ) return 1;
     978           0 :   if( FD_UNLIKELY( !fd_txn_is_signer( txn, txne->txnp->payload[ txn->instr[ 0 ].acct_off+2 ] ) ) ) return 0;
     979             :   /* We could check recent blockhash, but it's not necessary */
     980           0 :   return 2;
     981           0 : }
     982             : 
     983             : /* Can the fee payer afford to pay a transaction with the specified
     984             :    price?  Returns 1 if so, 0 otherwise.  This is just a stub that
     985             :    always returns 1 for now, and the real check is deferred to the bank
     986             :    tile.  In general, this function can't be totally accurate, because
     987             :    the transactions immediately prior to this one can affect the balance
     988             :    of this fee payer, but a simple check here may be helpful for
     989             :    reducing spam. */
     990             : static int
     991             : fd_pack_can_fee_payer_afford( fd_acct_addr_t const * acct_addr,
     992           0 :                               ulong                  price /* in lamports */) {
     993           0 :   (void)acct_addr;
     994           0 :   (void)price;
     995           0 :   return 1;
     996           0 : }
     997             : 
     998             : 
     999             : 
    1000             : 
    1001             : 
    1002           0 : fd_txn_e_t * fd_pack_insert_txn_init(   fd_pack_t * pack                   ) { return trp_pool_ele_acquire( pack->pool )->txn_e; }
    1003           0 : void         fd_pack_insert_txn_cancel( fd_pack_t * pack, fd_txn_e_t * txn ) { trp_pool_ele_release( pack->pool, (fd_pack_ord_txn_t*)txn ); }
    1004             : 
    1005           0 : #define REJECT( reason ) do {                                       \
    1006           0 :                            trp_pool_ele_release( pack->pool, ord ); \
    1007           0 :                            return FD_PACK_INSERT_REJECT_ ## reason; \
    1008           0 :                          } while( 0 )
    1009             : 
    1010             : /* These require txn, accts, and alt_adj to be defined as per usual */
    1011           0 : #define ACCT_IDX_TO_PTR( idx ) (__extension__( {                                               \
    1012           0 :       ulong __idx = (idx);                                                                     \
    1013           0 :       fd_ptr_if( __idx<fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM ), accts, alt_adj )+__idx; \
    1014           0 :       }))
    1015           0 : #define ACCT_ITER_TO_PTR( iter ) (__extension__( {                                             \
    1016           0 :       ulong __idx = fd_txn_acct_iter_idx( iter );                                              \
    1017           0 :       fd_ptr_if( __idx<fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM ), accts, alt_adj )+__idx; \
    1018           0 :       }))
    1019             : 
    1020             : 
    1021             : /* Tries to find the worst transaction in any treap in pack.  If that
    1022             :    transaction's score is worse than or equal to threshold_score, it
    1023             :    initiates a delete and returns the number of deleted transactions
    1024             :    (potentially more than 1 for a bundle).  If it's higher than
    1025             :    threshold_score, it returns 0.  To force this function to delete the
    1026             :    worst transaction if there are any eligible ones, pass FLT_MAX as
    1027             :    threshold_score. */
    1028             : static inline ulong
    1029             : delete_worst( fd_pack_t * pack,
    1030             :               float       threshold_score,
    1031           0 :               int         is_vote ) {
    1032             :   /* If the tree is full, we want to see if this is better than the
    1033             :      worst element in the pool before inserting.  If the new transaction
    1034             :      is better than that one, we'll delete it and insert the new
    1035             :      transaction. Otherwise, we'll throw away this transaction.
    1036             : 
    1037             :      We want to bias the definition of "worst" here to provide better
    1038             :      quality of service.  For example, if the pool is filled with
    1039             :      transactions that all write to the same account or are all votes,
    1040             :      we want to bias towards treating one of those transactions as the
    1041             :      worst, even if they pay slightly higher fees per computer unit,
    1042             :      since we know we won't actually be able to schedule them all.
    1043             : 
    1044             :      This is a tricky task, however.  All our notions of priority and
    1045             :      better/worse are based on static information about the transaction,
    1046             :      and there's not an easy way to take into account global
    1047             :      information, for example, how many other transactions contend with
    1048             :      this one.  One idea is to build a heap (not a treap, since we only
    1049             :      need pop-min, insert, and delete) with one element for each element
    1050             :      in the pool, with a "delete me" score that's related but not
    1051             :      identical to the normal score.  This would allow building in some
    1052             :      global information.  The downside is that the global information
    1053             :      that gets integrated is static.  E.g. if you bias a transaction's
    1054             :      "delete me" score to make it more likely to be deleted because
    1055             :      there are many conflicting transactions in the pool, the score
    1056             :      stays biased, even if the global conditions change (unless you come
    1057             :      up with some complicated re-scoring scheme).  This can work, since
    1058             :      when the pool is full, the global bias factors are unlikely to
    1059             :      change significantly at the relevant timescales.
    1060             : 
    1061             :      However, rather than this, we implement a simpler probabilistic
    1062             :      scheme.  We'll sample M transactions, find the worst transaction in
    1063             :      each of the M treaps, compute a "delete me" score for those <= M
    1064             :      transactions, and delete the worst.  If one penalty treap is
    1065             :      starting to get big, then it becomes very likely that the random
    1066             :      sample will find it and choose to delete a transaction from it.
    1067             : 
    1068             :      The exact formula for the "delete me" score should be the matter of
    1069             :      some more intense quantitative research.  For now, we'll just use
    1070             :      this:
    1071             : 
    1072             :      Treap with N transactions        Scale Factor
    1073             :      Pending                      1.0 unless inserting a vote and votes < 25%
    1074             :      Pending votes                1.0 until 75% of depth, then 0
    1075             :      Penalty treap                1.0 at <= 100 transactions, then sqrt(100/N)
    1076             :      Pending bundles              inf (since the rewards value is fudged)
    1077             : 
    1078             :      We'll also use M=8. */
    1079             : 
    1080           0 :   float worst_score = FLT_MAX;
    1081           0 :   fd_pack_ord_txn_t * worst = NULL;
    1082           0 :   for( ulong i=0UL; i<8UL; i++ ) {
    1083           0 :     uint  pool_max = (uint)trp_pool_max( pack->pool );
    1084           0 :     ulong sample_i = fd_rng_uint_roll( pack->rng, pool_max );
    1085             : 
    1086           0 :     fd_pack_ord_txn_t * sample = &pack->pool[ sample_i ];
    1087             :     /* Presumably if we're calling this, the pool is almost entirely
    1088             :        full, so the probability of choosing a free one is small.  If
    1089             :        it does happen, find the first one that isn't free. */
    1090           0 :     while( FD_UNLIKELY( sample->root==FD_ORD_TXN_ROOT_FREE ) ) sample = &pack->pool[ (++sample_i)%pool_max ];
    1091             : 
    1092           0 :     int       root_idx   = sample->root;
    1093           0 :     float     multiplier = 0.0f; /* The smaller this is, the more biased we'll be to deleting it */
    1094           0 :     treap_t * treap;
    1095           0 :     switch( root_idx & FD_ORD_TXN_ROOT_TAG_MASK ) {
    1096           0 :       default:
    1097           0 :       case FD_ORD_TXN_ROOT_FREE: {
    1098           0 :         FD_LOG_CRIT(( "Double free detected" ));
    1099           0 :         return ULONG_MAX; /* Can't be hit */
    1100           0 :       }
    1101           0 :       case FD_ORD_TXN_ROOT_PENDING: {
    1102           0 :         treap = pack->pending;
    1103           0 :         ulong vote_cnt = treap_ele_cnt( pack->pending_votes );
    1104           0 :         if( FD_LIKELY( !is_vote || (vote_cnt>=pack->pack_depth/4UL ) ) ) multiplier = 1.0f;
    1105           0 :         break;
    1106           0 :       }
    1107           0 :       case FD_ORD_TXN_ROOT_PENDING_VOTE: {
    1108           0 :         treap = pack->pending_votes;
    1109           0 :         ulong vote_cnt = treap_ele_cnt( pack->pending_votes );
    1110           0 :         if( FD_LIKELY( is_vote || (vote_cnt<=3UL*pack->pack_depth/4UL ) ) ) multiplier = 1.0f;
    1111           0 :         break;
    1112           0 :       }
    1113           0 :       case FD_ORD_TXN_ROOT_PENDING_BUNDLE: {
    1114             :         /* We don't have a way to tell how much these actually pay in
    1115             :            rewards, so we just assume they are very high. */
    1116           0 :         treap = pack->pending_bundles;
    1117             :         /* We cap rewards at UINT_MAX lamports for estimation, and min
    1118             :            CUs is about 1000, which means rewards/compute < 5e6.
    1119             :            FLT_MAX is around 3e38. That means, 1e20*rewards/compute is
    1120             :            much less than FLT_MAX, so we won't have any issues with
    1121             :            overflow.  On the other hand, if rewards==1 lamport and
    1122             :            compute is 2 million CUs, 1e20*1/2e6 is still higher than any
    1123             :            normal transaction. */
    1124           0 :         multiplier = 1e20f;
    1125           0 :         break;
    1126           0 :       }
    1127           0 :       case FD_ORD_TXN_ROOT_PENALTY( 0 ): {
    1128           0 :         fd_txn_t * txn = TXN( sample->txn );
    1129           0 :         fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, sample->txn->payload );
    1130           0 :         fd_acct_addr_t const * alt_adj = sample->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    1131           0 :         fd_acct_addr_t penalty_acct = *ACCT_IDX_TO_PTR( FD_ORD_TXN_ROOT_PENALTY_ACCT_IDX( root_idx ) );
    1132           0 :         fd_pack_penalty_treap_t * q = penalty_map_query( pack->penalty_treaps, penalty_acct, NULL );
    1133           0 :         FD_TEST( q );
    1134           0 :         ulong cnt = treap_ele_cnt( q->penalty_treap );
    1135           0 :         treap = q->penalty_treap;
    1136             : 
    1137           0 :         multiplier = sqrtf( 100.0f / (float)fd_ulong_max( 100UL, cnt ) );
    1138           0 :         break;
    1139           0 :       }
    1140           0 :     }
    1141             :     /* Get the worst from the sampled treap */
    1142           0 :     treap_fwd_iter_t _cur=treap_fwd_iter_init( treap, pack->pool );
    1143           0 :     FD_TEST( !treap_fwd_iter_done( _cur ) ); /* It can't be empty because we just sampled an element from it. */
    1144           0 :     sample = treap_fwd_iter_ele( _cur, pack->pool );
    1145             : 
    1146           0 :     float score = multiplier * (float)sample->rewards / (float)sample->compute_est;
    1147           0 :     worst = fd_ptr_if( score<worst_score, sample, worst );
    1148           0 :     worst_score = fd_float_if( worst_score<score, worst_score, score );
    1149           0 :   }
    1150             : 
    1151           0 :   if( FD_UNLIKELY( !worst                      ) ) return 0;
    1152           0 :   if( FD_UNLIKELY( threshold_score<worst_score ) ) return 0;
    1153             : 
    1154           0 :   return delete_transaction( pack, worst, 1, 1 );
    1155           0 : }
    1156             : 
    1157             : static inline int
    1158             : validate_transaction( fd_pack_t               * pack,
    1159             :                       fd_pack_ord_txn_t const * ord,
    1160             :                       fd_txn_t          const * txn,
    1161             :                       fd_acct_addr_t    const * accts,
    1162             :                       fd_acct_addr_t    const * alt_adj,
    1163           0 :                       int                       check_bundle_blacklist ) {
    1164           0 :   int writes_to_sysvar = 0;
    1165           0 :   for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_WRITABLE );
    1166           0 :       iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    1167           0 :     writes_to_sysvar |= fd_pack_unwritable_contains( ACCT_ITER_TO_PTR( iter ) );
    1168           0 :   }
    1169             : 
    1170           0 :   int bundle_blacklist = 0;
    1171           0 :   if( FD_UNLIKELY( check_bundle_blacklist ) ) {
    1172           0 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_ALL );
    1173           0 :         iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    1174           0 :       bundle_blacklist |= (3==fd_pack_tip_prog_check_blacklist( ACCT_ITER_TO_PTR( iter ) ));
    1175           0 :     }
    1176           0 :   }
    1177             : 
    1178           0 :   fd_acct_addr_t const * alt     = ord->txn_e->alt_accts;
    1179           0 :   fd_chkdup_t * chkdup = pack->chkdup;
    1180           0 :   ulong imm_cnt = fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    1181           0 :   ulong alt_cnt = fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_ALT );
    1182             : 
    1183             :   /* Throw out transactions ... */
    1184             :   /*           ... that are unfunded */
    1185           0 :   if( FD_UNLIKELY( !fd_pack_can_fee_payer_afford( accts, ord->rewards    ) ) ) return FD_PACK_INSERT_REJECT_UNAFFORDABLE;
    1186             :   /*           ... that are so big they'll never run */
    1187           0 :   if( FD_UNLIKELY( ord->compute_est >= pack->lim->max_cost_per_block       ) ) return FD_PACK_INSERT_REJECT_TOO_LARGE;
    1188             :   /*           ... that load too many accounts (ignoring 9LZdXeKGeBV6hRLdxS1rHbHoEUsKqesCC2ZAPTPKJAbK) */
    1189           0 :   if( FD_UNLIKELY( fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_ALL )>64UL     ) ) return FD_PACK_INSERT_REJECT_ACCOUNT_CNT;
    1190             :   /*           ... that duplicate an account address */
    1191           0 :   if( FD_UNLIKELY( fd_chkdup_check( chkdup, accts, imm_cnt, alt, alt_cnt ) ) ) return FD_PACK_INSERT_REJECT_DUPLICATE_ACCT;
    1192             :   /*           ... that try to write to a sysvar */
    1193           0 :   if( FD_UNLIKELY( writes_to_sysvar                                        ) ) return FD_PACK_INSERT_REJECT_WRITES_SYSVAR;
    1194             :   /*           ... that use an account that violates bundle rules */
    1195           0 :   if( FD_UNLIKELY( bundle_blacklist & 1                                    ) ) return FD_PACK_INSERT_REJECT_BUNDLE_BLACKLIST;
    1196             :   /*           ... that have an instruction with too many accounts */
    1197             :   /*               TODO: move this check into the transaction parser
    1198             :                    when limit_instruction_accounts is activated
    1199             :                    everywhere. */
    1200           0 :   for( ushort i=0; i<txn->instr_cnt; i++ ) {
    1201           0 :     if( FD_UNLIKELY( txn->instr[ i ].acct_cnt > FD_PACK_MAX_ACCOUNTS_PER_INSTRUCTION ) ) return FD_PACK_INSERT_REJECT_INSTR_ACCT_CNT;
    1202           0 :   }
    1203             : 
    1204           0 :   return 0;
    1205           0 : }
    1206             : 
    1207             : 
    1208             : 
    1209             : /* returns cumulative penalty "points", i.e. the sum of the populated
    1210             :    section of penalties (which also tells the caller how much of the
    1211             :    array is populated. */
    1212             : static inline ulong
    1213             : populate_bitsets( fd_pack_t         * pack,
    1214             :                   fd_pack_ord_txn_t * ord,
    1215             :                   ushort              penalties  [ static FD_TXN_ACCT_ADDR_MAX ],
    1216           0 :                   uchar               penalty_idx[ static FD_TXN_ACCT_ADDR_MAX ] ) {
    1217           0 :   FD_PACK_BITSET_CLEAR( ord->rw_bitset );
    1218           0 :   FD_PACK_BITSET_CLEAR( ord->w_bitset  );
    1219             : 
    1220           0 :   fd_txn_t * txn   = TXN(ord->txn);
    1221           0 :   uchar * payload  = ord->txn->payload;
    1222             : 
    1223           0 :   fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, payload );
    1224             :   /* alt_adj is the pointer to the ALT expansion, adjusted so that if
    1225             :      account address n is the first that comes from the ALT, it can be
    1226             :      accessed with adj_lut[n]. */
    1227           0 :   fd_acct_addr_t const * alt_adj = ord->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    1228             : 
    1229           0 :   ulong  cumulative_penalty = 0UL;
    1230           0 :   ulong  penalty_i          = 0UL;
    1231             : 
    1232           0 :   for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_WRITABLE );
    1233           0 :       iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    1234           0 :     fd_acct_addr_t acct = *ACCT_ITER_TO_PTR( iter );
    1235           0 :     fd_pack_bitset_acct_mapping_t * q = bitset_map_query( pack->acct_to_bitset, acct, NULL );
    1236           0 :     if( FD_UNLIKELY( q==NULL ) ) {
    1237           0 :       q = bitset_map_insert( pack->acct_to_bitset, acct );
    1238           0 :       q->ref_cnt                  = 0UL;
    1239           0 :       q->first_instance           = ord;
    1240           0 :       q->first_instance_was_write = 1;
    1241           0 :       q->bit                      = FD_PACK_BITSET_FIRST_INSTANCE;
    1242           0 :     } else if( FD_UNLIKELY( q->bit == FD_PACK_BITSET_FIRST_INSTANCE ) ) {
    1243           0 :       q->bit = pack->bitset_avail[ pack->bitset_avail_cnt ];
    1244           0 :       pack->bitset_avail_cnt = fd_ulong_if( !!pack->bitset_avail_cnt, pack->bitset_avail_cnt-1UL, 0UL );
    1245             : 
    1246           0 :       FD_PACK_BITSET_SETN( q->first_instance->rw_bitset, q->bit );
    1247           0 :       if( q->first_instance_was_write ) FD_PACK_BITSET_SETN( q->first_instance->w_bitset, q->bit );
    1248           0 :     }
    1249           0 :     ulong penalty = fd_ulong_max( q->ref_cnt, PENALTY_TREAP_THRESHOLD )-PENALTY_TREAP_THRESHOLD;
    1250           0 :     if( FD_UNLIKELY( penalty ) ) {
    1251           0 :       penalties  [ penalty_i ] = (ushort)penalty;
    1252           0 :       penalty_idx[ penalty_i ] = (uchar )fd_txn_acct_iter_idx( iter );
    1253           0 :       penalty_i++;
    1254           0 :       cumulative_penalty += penalty;
    1255           0 :     }
    1256             : 
    1257           0 :     q->ref_cnt++;
    1258           0 :     FD_PACK_BITSET_SETN( ord->rw_bitset, q->bit );
    1259           0 :     FD_PACK_BITSET_SETN( ord->w_bitset , q->bit );
    1260           0 :   }
    1261             : 
    1262           0 :   for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_READONLY );
    1263           0 :       iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    1264             : 
    1265           0 :     fd_acct_addr_t acct = *ACCT_ITER_TO_PTR( iter );
    1266           0 :     if( FD_UNLIKELY( fd_pack_unwritable_contains( &acct ) ) ) continue;
    1267             : 
    1268           0 :     fd_pack_bitset_acct_mapping_t * q = bitset_map_query( pack->acct_to_bitset, acct, NULL );
    1269           0 :     if( FD_UNLIKELY( q==NULL ) ) {
    1270           0 :       q = bitset_map_insert( pack->acct_to_bitset, acct );
    1271           0 :       q->ref_cnt                  = 0UL;
    1272           0 :       q->first_instance           = ord;
    1273           0 :       q->first_instance_was_write = 0;
    1274           0 :       q->bit                      = FD_PACK_BITSET_FIRST_INSTANCE;
    1275           0 :     } else if( FD_UNLIKELY( q->bit == FD_PACK_BITSET_FIRST_INSTANCE ) ) {
    1276           0 :       q->bit = pack->bitset_avail[ pack->bitset_avail_cnt ];
    1277           0 :       pack->bitset_avail_cnt = fd_ulong_if( !!pack->bitset_avail_cnt, pack->bitset_avail_cnt-1UL, 0UL );
    1278             : 
    1279           0 :       FD_PACK_BITSET_SETN( q->first_instance->rw_bitset, q->bit );
    1280           0 :       if( q->first_instance_was_write ) FD_PACK_BITSET_SETN( q->first_instance->w_bitset, q->bit );
    1281           0 :     }
    1282             : 
    1283           0 :     q->ref_cnt++;
    1284           0 :     FD_PACK_BITSET_SETN( ord->rw_bitset, q->bit );
    1285           0 :   }
    1286           0 :   return cumulative_penalty;
    1287           0 : }
    1288             : 
    1289             : int
    1290             : fd_pack_insert_txn_fini( fd_pack_t  * pack,
    1291             :                          fd_txn_e_t * txne,
    1292             :                          ulong        expires_at,
    1293           0 :                          ulong      * delete_cnt ) {
    1294           0 :   *delete_cnt = 0UL;
    1295             : 
    1296           0 :   fd_pack_ord_txn_t * ord = (fd_pack_ord_txn_t *)txne;
    1297             : 
    1298           0 :   fd_txn_t * txn   = TXN(txne->txnp);
    1299           0 :   uchar * payload  = txne->txnp->payload;
    1300             : 
    1301           0 :   fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, payload );
    1302             :   /* alt_adj is the pointer to the ALT expansion, adjusted so that if
    1303             :      account address n is the first that comes from the ALT, it can be
    1304             :      accessed with adj_lut[n]. */
    1305           0 :   fd_acct_addr_t const * alt_adj = ord->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    1306             : 
    1307           0 :   ord->expires_at = expires_at;
    1308             : 
    1309           0 :   int est_result = fd_pack_estimate_rewards_and_compute( txne, ord, pack->lim );
    1310           0 :   if( FD_UNLIKELY( !est_result ) ) REJECT( ESTIMATION_FAIL );
    1311           0 :   int is_vote          = est_result==1;
    1312             : 
    1313           0 :   int nonce_result = fd_pack_validate_durable_nonce( txne );
    1314           0 :   if( FD_UNLIKELY( !nonce_result ) ) REJECT( INVALID_NONCE );
    1315           0 :   int is_durable_nonce = nonce_result==2;
    1316           0 :   ord->txn->flags &= ~FD_TXN_P_FLAGS_DURABLE_NONCE;
    1317           0 :   ord->txn->flags |= fd_uint_if( is_durable_nonce, FD_TXN_P_FLAGS_DURABLE_NONCE, 0U );
    1318             : 
    1319           0 :   int validation_result = validate_transaction( pack, ord, txn, accts, alt_adj, !!pack->bundle_meta_sz );
    1320           0 :   if( FD_UNLIKELY( validation_result ) ) {
    1321           0 :     trp_pool_ele_release( pack->pool, ord );
    1322           0 :     return validation_result;
    1323           0 :   }
    1324             : 
    1325             :   /* Reject any transactions that have already expired */
    1326           0 :   if( FD_UNLIKELY( expires_at<pack->expire_before                          ) ) REJECT( EXPIRED          );
    1327             : 
    1328           0 :   int replaces = 0;
    1329             :   /* If it's a durable nonce and we already have one, delete one or the
    1330             :      other. */
    1331           0 :   if( FD_UNLIKELY( is_durable_nonce ) ) {
    1332           0 :     fd_pack_ord_txn_t * same_nonce = noncemap_ele_query( pack->noncemap, txne, NULL, pack->pool );
    1333           0 :     if( FD_LIKELY( same_nonce ) ) { /* Seems like most nonce transactions are effectively duplicates */
    1334           0 :       if( FD_LIKELY( same_nonce->root == FD_ORD_TXN_ROOT_PENDING_BUNDLE || COMPARE_WORSE( ord, same_nonce ) ) ) REJECT( NONCE_PRIORITY );
    1335           0 :       ulong _delete_cnt = delete_transaction( pack, same_nonce, 0, 0 ); /* Not a bundle, so delete_full_bundle is 0 */
    1336           0 :       *delete_cnt += _delete_cnt;
    1337           0 :       replaces = 1;
    1338           0 :     }
    1339           0 :   }
    1340             : 
    1341           0 :   if( FD_UNLIKELY( pack->pending_txn_cnt == pack->pack_depth ) ) {
    1342           0 :     float threshold_score = (float)ord->rewards/(float)ord->compute_est;
    1343           0 :     ulong _delete_cnt = delete_worst( pack, threshold_score, is_vote );
    1344           0 :     *delete_cnt += _delete_cnt;
    1345           0 :     if( FD_UNLIKELY( !_delete_cnt ) ) REJECT( PRIORITY );
    1346           0 :     replaces = 1;
    1347           0 :   }
    1348             : 
    1349           0 :   ord->txn->flags &= ~(FD_TXN_P_FLAGS_BUNDLE | FD_TXN_P_FLAGS_INITIALIZER_BUNDLE);
    1350           0 :   ord->skip = FD_PACK_SKIP_CNT;
    1351             : 
    1352             :   /* At this point, we know we have space to insert the transaction and
    1353             :      we've committed to insert it. */
    1354             : 
    1355             :   /* Since the pool uses ushorts, the size of the pool is < USHORT_MAX.
    1356             :      Each transaction can reference an account at most once, which means
    1357             :      that the total number of references for an account is < USHORT_MAX.
    1358             :      If these were ulongs, the array would be 512B, which is kind of a
    1359             :      lot to zero out.*/
    1360           0 :   ushort penalties[ FD_TXN_ACCT_ADDR_MAX ] = {0};
    1361           0 :   uchar  penalty_idx[ FD_TXN_ACCT_ADDR_MAX ];
    1362           0 :   ulong cumulative_penalty = populate_bitsets( pack, ord, penalties, penalty_idx );
    1363             : 
    1364           0 :   treap_t * insert_into = pack->pending;
    1365             : 
    1366           0 :   if( FD_UNLIKELY( cumulative_penalty && !is_vote ) ) { /* Optimize for high parallelism case */
    1367             :     /* Compute a weighted random choice */
    1368           0 :     ulong roll = (ulong)fd_rng_uint_roll( pack->rng, (uint)cumulative_penalty ); /* cumulative_penalty < USHORT_MAX*64 < UINT_MAX */
    1369           0 :     ulong i = 0UL;
    1370             :     /* Find the right one.  This can be done in O(log N), but I imagine
    1371             :        N is normally so small that doesn't matter. */
    1372           0 :     while( roll>=penalties[i] ) roll -= (ulong)penalties[i++];
    1373             : 
    1374           0 :     fd_acct_addr_t penalty_acct = *ACCT_IDX_TO_PTR( penalty_idx[i] );
    1375           0 :     fd_pack_penalty_treap_t * q = penalty_map_query( pack->penalty_treaps, penalty_acct, NULL );
    1376           0 :     if( FD_UNLIKELY( q==NULL ) ) {
    1377           0 :       q = penalty_map_insert( pack->penalty_treaps, penalty_acct );
    1378           0 :       treap_new( q->penalty_treap, pack->pack_depth );
    1379           0 :     }
    1380           0 :     insert_into = q->penalty_treap;
    1381           0 :     ord->root = FD_ORD_TXN_ROOT_PENALTY( penalty_idx[i] );
    1382           0 :   } else {
    1383           0 :     ord->root = fd_int_if( is_vote, FD_ORD_TXN_ROOT_PENDING_VOTE, FD_ORD_TXN_ROOT_PENDING );
    1384             : 
    1385           0 :     fd_pack_smallest_t * smallest = fd_ptr_if( is_vote, &pack->pending_votes_smallest[0], pack->pending_smallest );
    1386           0 :     smallest->cus   = fd_ulong_min( smallest->cus,   ord->compute_est       );
    1387           0 :     smallest->bytes = fd_ulong_min( smallest->bytes, txne->txnp->payload_sz );
    1388           0 :   }
    1389             : 
    1390           0 :   pack->pending_txn_cnt++;
    1391             : 
    1392           0 :   sig2txn_ele_insert( pack->signature_map, ord, pack->pool );
    1393             : 
    1394           0 :   if( FD_UNLIKELY( is_durable_nonce ) ) noncemap_ele_insert( pack->noncemap, ord, pack->pool );
    1395             : 
    1396           0 :   fd_pack_expq_t temp[ 1 ] = {{ .expires_at = expires_at, .txn = ord }};
    1397           0 :   expq_insert( pack->expiration_q, temp );
    1398             : 
    1399           0 :   if( FD_LIKELY( is_vote ) ) insert_into = pack->pending_votes;
    1400             : 
    1401           0 :   treap_ele_insert( insert_into, ord, pack->pool );
    1402           0 :   return (is_vote) | (replaces<<1) | (is_durable_nonce<<2);
    1403           0 : }
    1404             : #undef REJECT
    1405             : 
    1406             : fd_txn_e_t * const *
    1407             : fd_pack_insert_bundle_init( fd_pack_t          * pack,
    1408             :                             fd_txn_e_t *       * bundle,
    1409           0 :                             ulong                txn_cnt ) {
    1410           0 :   FD_TEST( txn_cnt<=FD_PACK_MAX_TXN_PER_BUNDLE  );
    1411           0 :   FD_TEST( trp_pool_free( pack->pool )>=txn_cnt );
    1412           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) bundle[ i ] = trp_pool_ele_acquire( pack->pool )->txn_e;
    1413           0 :   return bundle;
    1414           0 : }
    1415             : 
    1416             : void
    1417             : fd_pack_insert_bundle_cancel( fd_pack_t          * pack,
    1418             :                               fd_txn_e_t * const * bundle,
    1419           0 :                               ulong                txn_cnt ) {
    1420             :   /* There's no real reason these have to be released in reverse, but it
    1421             :      seems fitting to release them in the opposite order they were
    1422             :      acquired. */
    1423           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) trp_pool_ele_release( pack->pool, (fd_pack_ord_txn_t*)bundle[ txn_cnt-1UL-i ] );
    1424           0 : }
    1425             : 
    1426             : /* Explained below */
    1427             : #define BUNDLE_L_PRIME 37896771UL
    1428             : #define BUNDLE_N       312671UL
    1429           0 : #define RC_TO_REL_BUNDLE_IDX( r, c ) (BUNDLE_N - ((ulong)(r) * 1UL<<32)/((ulong)(c) * BUNDLE_L_PRIME))
    1430             : 
    1431             : int
    1432             : fd_pack_insert_bundle_fini( fd_pack_t          * pack,
    1433             :                             fd_txn_e_t * const * bundle,
    1434             :                             ulong                txn_cnt,
    1435             :                             ulong                expires_at,
    1436             :                             int                  initializer_bundle,
    1437             :                             void         const * bundle_meta,
    1438           0 :                             ulong              * delete_cnt ) {
    1439             : 
    1440           0 :   int err = 0;
    1441           0 :   *delete_cnt = 0UL;
    1442             : 
    1443           0 :   ulong pending_b_txn_cnt = treap_ele_cnt( pack->pending_bundles );
    1444             :     /* We want to prevent bundles from consuming the whole treap, but in
    1445             :        general, we assume bundles are lucrative.  We'll set the policy
    1446             :        on capping bundles at half of the pack depth.  We assume that the
    1447             :        bundles are coming in a pre-prioritized order, so it doesn't make
    1448             :        sense to drop an earlier bundle for this one.  That means that
    1449             :        really, the best thing to do is drop this one. */
    1450           0 :   if( FD_UNLIKELY( (!initializer_bundle)&(pending_b_txn_cnt+txn_cnt>pack->pack_depth/2UL) ) ) err = FD_PACK_INSERT_REJECT_PRIORITY;
    1451             : 
    1452           0 :   if( FD_UNLIKELY( expires_at<pack->expire_before                                         ) ) err = FD_PACK_INSERT_REJECT_EXPIRED;
    1453             : 
    1454             : 
    1455           0 :   int   replaces      = 0;
    1456           0 :   ulong nonce_txn_cnt = 0UL;
    1457             : 
    1458             :   /* Collect nonce hashes to detect duplicate nonces.
    1459             :      Use a constant-time duplicate-detection algorithm -- Vacant entries
    1460             :      have the MSB set, occupied entries are the noncemap hash, with the
    1461             :      MSB set to 0. */
    1462           0 :   ulong nonce_hash63[ FD_PACK_MAX_TXN_PER_BUNDLE ];
    1463           0 :   for( ulong i=0UL; i<FD_PACK_MAX_TXN_PER_BUNDLE; i++ ) {
    1464           0 :     nonce_hash63[ i ] = ULONG_MAX-i;
    1465           0 :   }
    1466             : 
    1467           0 :   for( ulong i=0UL; (i<txn_cnt) && !err; i++ ) {
    1468           0 :     fd_pack_ord_txn_t * ord = (fd_pack_ord_txn_t *)bundle[ i ];
    1469             : 
    1470           0 :     fd_txn_t const * txn     = TXN(bundle[ i ]->txnp);
    1471           0 :     uchar    const * payload = bundle[ i ]->txnp->payload;
    1472             : 
    1473           0 :     fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, payload );
    1474           0 :     fd_acct_addr_t const * alt_adj = ord->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    1475             : 
    1476           0 :     int est_result = fd_pack_estimate_rewards_and_compute( bundle[ i ], ord, pack->lim );
    1477           0 :     if( FD_UNLIKELY( !est_result   ) ) { err = FD_PACK_INSERT_REJECT_ESTIMATION_FAIL; break; }
    1478           0 :     int nonce_result = fd_pack_validate_durable_nonce( ord->txn_e );
    1479           0 :     if( FD_UNLIKELY( !nonce_result ) ) { err = FD_PACK_INSERT_REJECT_INVALID_NONCE;   break; }
    1480           0 :     int is_durable_nonce = nonce_result==2;
    1481           0 :     nonce_txn_cnt += !!is_durable_nonce;
    1482             : 
    1483           0 :     bundle[ i ]->txnp->flags |= FD_TXN_P_FLAGS_BUNDLE;
    1484           0 :     bundle[ i ]->txnp->flags &= ~(FD_TXN_P_FLAGS_INITIALIZER_BUNDLE | FD_TXN_P_FLAGS_DURABLE_NONCE);
    1485           0 :     bundle[ i ]->txnp->flags |= fd_uint_if( initializer_bundle, FD_TXN_P_FLAGS_INITIALIZER_BUNDLE, 0U );
    1486           0 :     bundle[ i ]->txnp->flags |= fd_uint_if( is_durable_nonce,   FD_TXN_P_FLAGS_DURABLE_NONCE,      0U );
    1487           0 :     ord->skip = FD_PACK_SKIP_CNT;
    1488           0 :     ord->expires_at = expires_at;
    1489             : 
    1490           0 :     if( FD_UNLIKELY( is_durable_nonce ) ) {
    1491           0 :       nonce_hash63[ i ] = noncemap_key_hash( ord->txn_e, pack->noncemap->seed ) & 0x7FFFFFFFFFFFFFFFUL;
    1492           0 :       fd_pack_ord_txn_t * same_nonce = noncemap_ele_query( pack->noncemap, ord->txn_e, NULL, pack->pool );
    1493           0 :       if( FD_LIKELY( same_nonce ) ) {
    1494             :         /* bundles take priority over non-bundles, and earlier bundles
    1495             :            take priority over later bundles. */
    1496           0 :         if( FD_UNLIKELY( same_nonce->txn->flags & FD_TXN_P_FLAGS_BUNDLE ) ) {
    1497           0 :           err = FD_PACK_INSERT_REJECT_NONCE_PRIORITY;
    1498           0 :           break;
    1499           0 :         } else {
    1500           0 :           ulong _delete_cnt = delete_transaction( pack, same_nonce, 0, 0 );
    1501           0 :           *delete_cnt += _delete_cnt;
    1502           0 :           replaces = 1;
    1503           0 :         }
    1504           0 :       }
    1505           0 :     }
    1506             : 
    1507           0 :     int validation_result = validate_transaction( pack, ord, txn, accts, alt_adj, !initializer_bundle );
    1508           0 :     if( FD_UNLIKELY( validation_result ) ) { err = validation_result; break; }
    1509           0 :   }
    1510             : 
    1511           0 :   if( FD_UNLIKELY( err ) ) {
    1512           0 :     fd_pack_insert_bundle_cancel( pack, bundle, txn_cnt );
    1513           0 :     return err;
    1514           0 :   }
    1515             : 
    1516           0 :   if( FD_UNLIKELY( initializer_bundle && pending_b_txn_cnt>0UL ) ) {
    1517           0 :     treap_rev_iter_t _cur=treap_rev_iter_init( pack->pending_bundles, pack->pool );
    1518           0 :     FD_TEST( !treap_rev_iter_done( _cur ) );
    1519           0 :     fd_pack_ord_txn_t * cur = treap_rev_iter_ele( _cur, pack->pool );
    1520           0 :     int is_ib = !!(cur->txn->flags & FD_TXN_P_FLAGS_INITIALIZER_BUNDLE);
    1521             : 
    1522             :     /* Delete the previous IB if there is one */
    1523           0 :     if( FD_UNLIKELY( is_ib && 0UL==RC_TO_REL_BUNDLE_IDX( cur->rewards, cur->compute_est ) ) ) {
    1524           0 :       ulong _delete_cnt = delete_transaction( pack, cur, 1, 0 );
    1525           0 :       *delete_cnt += _delete_cnt;
    1526           0 :     }
    1527           0 :   }
    1528             : 
    1529           0 :   while( FD_UNLIKELY( pack->pending_txn_cnt+txn_cnt > pack->pack_depth ) ) {
    1530           0 :     ulong _delete_cnt = delete_worst( pack, FLT_MAX, 0 );
    1531           0 :     *delete_cnt += _delete_cnt;
    1532           0 :     if( FD_UNLIKELY( !_delete_cnt ) ) {
    1533           0 :       fd_pack_insert_bundle_cancel( pack, bundle, txn_cnt );
    1534           0 :       return FD_PACK_INSERT_REJECT_PRIORITY;
    1535           0 :     }
    1536           0 :     replaces = 1;
    1537           0 :   }
    1538             : 
    1539           0 :   if( FD_UNLIKELY( !pending_b_txn_cnt ) ) {
    1540           0 :     pack->relative_bundle_idx = 1UL;
    1541           0 :   }
    1542             : 
    1543           0 :   if( FD_LIKELY( bundle_meta ) ) {
    1544           0 :     memcpy( (uchar *)pack->bundle_meta + (ulong)((fd_pack_ord_txn_t *)bundle[0]-pack->pool)*pack->bundle_meta_sz, bundle_meta, pack->bundle_meta_sz );
    1545           0 :   }
    1546             : 
    1547           0 :   if( FD_UNLIKELY( nonce_txn_cnt>1UL ) ) {
    1548             :     /* Do a ILP-friendly duplicate detect, naive O(n^2) algo.  With max
    1549             :        5 txns per bundle, this requires 10 comparisons.  ~ 25 cycle.  */
    1550           0 :     uint conflict_detected = 0u;
    1551           0 :     for( ulong i=0UL; i<FD_PACK_MAX_TXN_PER_BUNDLE-1; i++ ) {
    1552           0 :       for( ulong j=i+1; j<FD_PACK_MAX_TXN_PER_BUNDLE; j++ ) {
    1553           0 :         ulong const ele_i = nonce_hash63[ i ];
    1554           0 :         ulong const ele_j = nonce_hash63[ j ];
    1555           0 :         conflict_detected |= (ele_i==ele_j);
    1556           0 :       }
    1557           0 :     }
    1558           0 :     if( FD_UNLIKELY( conflict_detected ) ) {
    1559           0 :       fd_pack_insert_bundle_cancel( pack, bundle, txn_cnt );
    1560           0 :       return FD_PACK_INSERT_REJECT_NONCE_CONFLICT;
    1561           0 :     }
    1562           0 :   }
    1563             : 
    1564             :   /* We put bundles in a treap just like all the other transactions, but
    1565             :      we actually want to sort them in a very specific order; the order
    1566             :      within the bundle is determined at bundle creation time, and the
    1567             :      order among the bundles is FIFO.  However, it's going to be a pain
    1568             :      to use a different sorting function for this treap, since it's
    1569             :      fixed as part of the treap creation for performance.  Don't fear
    1570             :      though; we can pull a cool math trick out of the bag to shoehorn
    1571             :      the order we'd like into the sort function we need, and to get even
    1572             :      more.
    1573             : 
    1574             :      Recall that the sort function is r_i/c_i, smallest to largest,
    1575             :      where r_i is the rewards and c_i is the cost units.  r_i and c_i
    1576             :      are both uints, and the comparison is done by cross-multiplication
    1577             :      as ulongs.  We actually use the c_i value for testing if
    1578             :      transactions fit, etc.  so let's assume that's fixed, and we know
    1579             :      it's in the range [1020, 1,556,782].
    1580             : 
    1581             :      This means, if c_0, c_1, ... c_4 are the CU costs of the
    1582             :      transactions in the first bundle, we require r_0/c_0 > r_1/c_1 >
    1583             :      ... > r_4/c_4.  Then, if c_5, ... c_9 are the CU costs of the
    1584             :      transactions in the second bundle, we also require that r_4/c_4 >
    1585             :      r_5/c_5.  For convenience, we'll impose a slightly stronger
    1586             :      constraint: we want the kth bundle to obey L*(N-k) <= r_i/c_i <
    1587             :      L*(N+1-k), for fixed constants L and N, real and integer,
    1588             :      respectively, that we'll determine. For example, this means r_4/c_4
    1589             :      >= L*N > r_5/c_5.  This enables us to group the transactions in the
    1590             :      same bundle more easily.
    1591             : 
    1592             :      For convenience in the math below, we'll set j=N-k and relabel the
    1593             :      transactions from the jth bundle c_0, ... c_4.
    1594             :      From above, we know that Lj <= r_4/c_4.  We'd like to make it as
    1595             :      close as possible given that r_4 is an integers.  Thus, put
    1596             :      r_4 = ceil( c_4 * Lj ).  r_4 is clearly an integer, and it satisfies
    1597             :      the required inequality because:
    1598             :             r_4/c_4 = ceil( c_4 * Lj)/c_4 >= c_4*Lj / c_4 >= Lj.
    1599             : 
    1600             :      Following in the same spirit, put r_3 = ceil( c_3 * (r_4+1)/c_4 ).
    1601             :      Again, r_3 is clearly an integer, and
    1602             :                 r_3/c_3  = ceil(c_3*(r_4+1)/c_4)/c_3
    1603             :                         >= (c_3*(r_4+1))/(c_3 * c_4)
    1604             :                         >= r_4/c_4 + 1/c_4
    1605             :                         >  r_4/c_4.
    1606             :      Following the pattern, we put
    1607             :                 r_2 = ceil( c_2 * (r_3+1)/c_3 )
    1608             :                 r_1 = ceil( c_1 * (r_2+1)/c_2 )
    1609             :                 r_0 = ceil( c_0 * (r_1+1)/c_1 )
    1610             :      which work for the same reason that as r_3.
    1611             : 
    1612             :      We now need for r_0 to satisfy the final inequality with L, and
    1613             :      we'll use this to guide our choice of L.  Theoretically, r_0 can be
    1614             :      expressed in terms of L, j, and c_0, ... c_4, but that's a truly
    1615             :      inscrutible expression.  Instead, we need some bounds so we can get
    1616             :      rid of all the ceil using the property that x <= ceil(x) < x+1.
    1617             :                      c_4 * Lj <= r_4 < c_4 * Lj + 1
    1618             :      The lower bound on r_3 is easy:
    1619             :          r_3 >= c_3 * (c_4 * Lj + 1)/c_4 = c_3 * Lj + c_3/c_4
    1620             :      For the upper bound,
    1621             :          r_3 < 1 + c_3*(r_4+1)/c_4 < 1 + c_3*(c_4*Lj+1 + 1)/c_4
    1622             :                                    = 1 + c_3 * Lj + 2*c_3/c_4
    1623             :      Continuing similarly gives
    1624             :        c_2*Lj +                     c_2/c_3 + c_2/c_4 <= r_2
    1625             :        c_1*Lj +           c_1/c_2 + c_1/c_c + c_1/c_4 <= r_1
    1626             :        c_0*Lj + c_0/c_1 + c_0/c_2 + c_0/c_3 + c_0/c_4 <= r_0
    1627             :      and
    1628             :        r_2 < 1 + c_2*Lj +                       2c_2/c_3 + 2c_2/c_4
    1629             :        r_1 < 1 + c_1*Lj +            2c_1/c_2 + 2c_1/c_3 + 2c_1/c_4
    1630             :        r_0 < 1 + c_0*Lj + 2c_0/c_1 + 2c_0/c_2 + 2c_0/c_3 + 2c_0/c_4.
    1631             : 
    1632             :      Setting L(j+1)>=(1 + c_0*Lj+2c_0/c_1+2c_0/c_2+2c_0/c_3+2c_0/c_4)/c_0
    1633             :      is then sufficient to ensure the whole sequence of 5 fits between Lj
    1634             :      and L(j+1).  Simplifying gives
    1635             :               L<= 1/c_0 + 2/c_1 + 2/c_2 + 2/c_3 + 2/c_4
    1636             :      but L must be a constant and not depend on individual values of c_i,
    1637             :      so, given that c_i >= 1020, we set L = 9/1020.
    1638             : 
    1639             :      Now all that remains is to determine N.  It's a bit unfortunate
    1640             :      that we require N, since it limits our capacity, but it's necessary
    1641             :      in any system that tries to compute priorities to enforce a FIFO
    1642             :      order.  If we've inserted more than N bundles without ever having
    1643             :      the bundle treap go empty, we'll briefly break the FIFO ordering as
    1644             :      we underflow.
    1645             : 
    1646             :      Thus, we'd like to make N as big as possible, avoiding overflow.
    1647             :      r_0, ..., r_4 are all uints, and taking the bounds from above,
    1648             :      given that for any i, i' c_i/c_{i'} < 1527, we have
    1649             :                r_i < 1 + 1556782 * Lj + 8*1527.
    1650             :      To avoid overflow, we assert the right-hand side is < 2^32, which
    1651             :      implies N <= 312671.
    1652             : 
    1653             :      We want to use a fixed point representation for L so that the
    1654             :      entire computation can be done with integer arithmetic.  We can do
    1655             :      the arithmetic as ulongs, which means defining L' >= L * 2^s, and
    1656             :      we compute ceil( c_4*Lj ) as floor( (c_4 * L' * j + 2^s - 1)/2^s ),
    1657             :      so c_4 * L' * j + 2^s should fit in a ulong.  With j<=N, this gives
    1658             :      s<=32, so we set s=32, which means L' = 37896771 >= 9/1020 * 2^32.
    1659             :      Note that 1 + 1556782 * L' * N + 8*1527 + 2^32 is approximately
    1660             :      2^63.999993.
    1661             : 
    1662             :      Note that this is all checked by a proof of the code translated
    1663             :      into Z3.  Unfortunately CBMC was too slow to prove this code
    1664             :      directly. */
    1665           0 : #define BUNDLE_L_PRIME 37896771UL
    1666           0 : #define BUNDLE_N       312671UL
    1667             : 
    1668           0 :   if( FD_UNLIKELY( pack->relative_bundle_idx>BUNDLE_N ) ) {
    1669           0 :     FD_LOG_WARNING(( "Too many bundles inserted without allowing pending bundles to go empty. "
    1670           0 :                      "Ordering of bundles may be incorrect." ));
    1671           0 :     pack->relative_bundle_idx = 1UL;
    1672           0 :   }
    1673           0 :   ulong bundle_idx = fd_ulong_if( initializer_bundle, 0UL, pack->relative_bundle_idx );
    1674           0 :   insert_bundle_impl( pack, bundle_idx, txn_cnt, (fd_pack_ord_txn_t * *)bundle, expires_at );
    1675             :   /* if IB this is max( 1, x ), which is x.  Otherwise, this is max(x,
    1676             :      x+1) which is x++ */
    1677           0 :   pack->relative_bundle_idx = fd_ulong_max( bundle_idx+1UL, pack->relative_bundle_idx );
    1678             : 
    1679           0 :   return (0) | (replaces<<1) | ((!!nonce_txn_cnt)<<2);
    1680           0 : }
    1681             : static inline void
    1682             : insert_bundle_impl( fd_pack_t           * pack,
    1683             :                     ulong                 bundle_idx,
    1684             :                     ulong                 txn_cnt,
    1685             :                     fd_pack_ord_txn_t * * bundle,
    1686           0 :                     ulong                 expires_at ) {
    1687           0 :   ulong prev_reward = ((BUNDLE_L_PRIME * (BUNDLE_N - bundle_idx))) - 1UL;
    1688           0 :   ulong prev_cost = 1UL<<32;
    1689             : 
    1690             :   /* Assign last to first */
    1691           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
    1692           0 :     fd_pack_ord_txn_t * ord = bundle[ txn_cnt-1UL - i ];
    1693           0 :     ord->rewards = (uint)(((ulong)ord->compute_est * (prev_reward + 1UL) + prev_cost-1UL)/prev_cost);
    1694           0 :     ord->root    = FD_ORD_TXN_ROOT_PENDING_BUNDLE;
    1695           0 :     prev_reward = ord->rewards;
    1696           0 :     prev_cost   = ord->compute_est;
    1697             : 
    1698             :     /* The penalty information isn't used for bundles. */
    1699           0 :     ushort penalties  [ FD_TXN_ACCT_ADDR_MAX ];
    1700           0 :     uchar  penalty_idx[ FD_TXN_ACCT_ADDR_MAX ];
    1701           0 :     populate_bitsets( pack, ord, penalties, penalty_idx );
    1702             : 
    1703           0 :     treap_ele_insert( pack->pending_bundles, ord, pack->pool );
    1704           0 :     pack->pending_txn_cnt++;
    1705             : 
    1706           0 :     if( FD_UNLIKELY( ord->txn->flags & FD_TXN_P_FLAGS_DURABLE_NONCE ) ) noncemap_ele_insert( pack->noncemap, ord, pack->pool );
    1707           0 :     sig2txn_ele_insert( pack->signature_map, ord, pack->pool );
    1708             : 
    1709           0 :     fd_pack_expq_t temp[ 1 ] = {{ .expires_at = expires_at, .txn = ord }};
    1710           0 :     expq_insert( pack->expiration_q, temp );
    1711           0 :   }
    1712             : 
    1713           0 : }
    1714             : 
    1715             : void const *
    1716           0 : fd_pack_peek_bundle_meta( fd_pack_t const * pack ) {
    1717           0 :   int ib_state = pack->initializer_bundle_state;
    1718           0 :   if( FD_UNLIKELY( (ib_state==FD_PACK_IB_STATE_PENDING) | (ib_state==FD_PACK_IB_STATE_FAILED) ) ) return NULL;
    1719             : 
    1720           0 :   treap_rev_iter_t _cur=treap_rev_iter_init( pack->pending_bundles, pack->pool );
    1721           0 :   if( FD_UNLIKELY( treap_rev_iter_done( _cur ) ) ) return NULL; /* empty */
    1722             : 
    1723           0 :   fd_pack_ord_txn_t * cur = treap_rev_iter_ele( _cur, pack->pool );
    1724           0 :   int is_ib = !!(cur->txn->flags & FD_TXN_P_FLAGS_INITIALIZER_BUNDLE);
    1725           0 :   if( FD_UNLIKELY( is_ib ) ) return NULL;
    1726             : 
    1727           0 :   return (void const *)((uchar const *)pack->bundle_meta + (ulong)_cur * pack->bundle_meta_sz);
    1728           0 : }
    1729             : 
    1730             : void
    1731           0 : fd_pack_set_initializer_bundles_ready( fd_pack_t * pack ) {
    1732           0 :   pack->initializer_bundle_state = FD_PACK_IB_STATE_READY;
    1733           0 : }
    1734             : 
    1735             : void
    1736           0 : fd_pack_metrics_write( fd_pack_t const * pack ) {
    1737           0 :   ulong pending_regular = treap_ele_cnt( pack->pending        );
    1738           0 :   ulong pending_votes  = treap_ele_cnt( pack->pending_votes   );
    1739           0 :   ulong pending_bundle = treap_ele_cnt( pack->pending_bundles );
    1740           0 :   ulong conflicting    = pack->pending_txn_cnt - pending_votes - pending_bundle - treap_ele_cnt( pack->pending );
    1741           0 :   FD_MGAUGE_SET( PACK, AVAILABLE_TRANSACTIONS_ALL,         pack->pending_txn_cnt       );
    1742           0 :   FD_MGAUGE_SET( PACK, AVAILABLE_TRANSACTIONS_REGULAR,     pending_regular             );
    1743           0 :   FD_MGAUGE_SET( PACK, AVAILABLE_TRANSACTIONS_VOTES,       pending_votes               );
    1744           0 :   FD_MGAUGE_SET( PACK, AVAILABLE_TRANSACTIONS_CONFLICTING, conflicting                 );
    1745           0 :   FD_MGAUGE_SET( PACK, AVAILABLE_TRANSACTIONS_BUNDLES,     pending_bundle              );
    1746           0 :   FD_MGAUGE_SET( PACK, SMALLEST_PENDING_TRANSACTION,       pack->pending_smallest->cus );
    1747             : 
    1748           0 :   FD_MCNT_ENUM_COPY( PACK, TRANSACTION_SCHEDULE, pack->sched_results );
    1749           0 : }
    1750             : 
    1751             : void
    1752           0 : fd_pack_get_sched_metrics( fd_pack_t const * pack, ulong * metrics ) {
    1753           0 :   fd_memcpy( metrics, pack->sched_results, sizeof(pack->sched_results) );
    1754           0 : }
    1755             : 
    1756             : typedef struct {
    1757             :   ushort clear_rw_bit;
    1758             :   ushort clear_w_bit;
    1759             : } release_result_t;
    1760             : 
    1761             : static inline release_result_t
    1762             : release_bit_reference( fd_pack_t            * pack,
    1763           0 :                        fd_acct_addr_t const * acct ) {
    1764             : 
    1765           0 :   fd_pack_bitset_acct_mapping_t * q = bitset_map_query( pack->acct_to_bitset, *acct, NULL );
    1766           0 :   FD_TEST( q ); /* q==NULL not be possible */
    1767             : 
    1768           0 :   q->ref_cnt--;
    1769             : 
    1770           0 :   if( FD_UNLIKELY( q->ref_cnt==0UL ) ) {
    1771           0 :     ushort bit = q->bit;
    1772           0 :     bitset_map_remove( pack->acct_to_bitset, q );
    1773           0 :     if( FD_LIKELY( bit<FD_PACK_BITSET_MAX ) ) pack->bitset_avail[ ++(pack->bitset_avail_cnt) ] = bit;
    1774             : 
    1775           0 :     fd_pack_addr_use_t * use = acct_uses_query( pack->acct_in_use,  *acct, NULL );
    1776           0 :     if( FD_LIKELY( use ) ) {
    1777           0 :       use->in_use_by |= FD_PACK_IN_USE_BIT_CLEARED;
    1778           0 :       release_result_t ret = { .clear_rw_bit = bit,
    1779           0 :                                .clear_w_bit = fd_ushort_if( !!(use->in_use_by & FD_PACK_IN_USE_WRITABLE), bit, FD_PACK_BITSET_MAX ) };
    1780           0 :       return ret;
    1781           0 :     }
    1782           0 :   }
    1783           0 :   release_result_t ret = { .clear_rw_bit = FD_PACK_BITSET_MAX, .clear_w_bit = FD_PACK_BITSET_MAX };
    1784           0 :   return ret;
    1785           0 : }
    1786             : 
    1787             : typedef struct {
    1788             :   ulong cus_scheduled;
    1789             :   ulong txns_scheduled;
    1790             :   ulong bytes_scheduled;
    1791             :   ulong alloc_scheduled;
    1792             : } sched_return_t;
    1793             : 
    1794             : static inline sched_return_t
    1795             : fd_pack_schedule_impl( fd_pack_t          * pack,
    1796             :                        treap_t            * sched_from,
    1797             :                        ulong                cu_limit,
    1798             :                        ulong                txn_limit,
    1799             :                        ulong                byte_limit,
    1800             :                        ulong                alloc_limit,
    1801             :                        ulong                bank_tile,
    1802             :                        fd_pack_smallest_t * smallest_in_treap,
    1803             :                        ulong              * use_by_bank_txn,
    1804           0 :                        fd_txn_e_t         * out ) {
    1805             : 
    1806           0 :   fd_pack_ord_txn_t  * pool         = pack->pool;
    1807           0 :   fd_pack_addr_use_t * acct_in_use  = pack->acct_in_use;
    1808           0 :   fd_pack_addr_use_t * writer_costs = pack->writer_costs;
    1809             : 
    1810           0 :   fd_pack_addr_use_t ** written_list     = pack->written_list;
    1811           0 :   ulong                 written_list_cnt = pack->written_list_cnt;
    1812           0 :   ulong                 written_list_max = pack->written_list_max;
    1813             : 
    1814           0 :   FD_PACK_BITSET_DECLARE( bitset_rw_in_use );
    1815           0 :   FD_PACK_BITSET_DECLARE( bitset_w_in_use  );
    1816           0 :   FD_PACK_BITSET_COPY( bitset_rw_in_use, pack->bitset_rw_in_use );
    1817           0 :   FD_PACK_BITSET_COPY( bitset_w_in_use,  pack->bitset_w_in_use  );
    1818             : 
    1819           0 :   fd_pack_addr_use_t * use_by_bank     = pack->use_by_bank    [bank_tile];
    1820           0 :   ulong                use_by_bank_cnt = pack->use_by_bank_cnt[bank_tile];
    1821             : 
    1822           0 :   ulong max_write_cost_per_acct = pack->lim->max_write_cost_per_acct;
    1823             : 
    1824           0 :   ushort compressed_slot_number = pack->compressed_slot_number;
    1825             : 
    1826           0 :   ulong txns_scheduled  = 0UL;
    1827           0 :   ulong cus_scheduled   = 0UL;
    1828           0 :   ulong bytes_scheduled = 0UL;
    1829           0 :   ulong alloc_scheduled = 0UL;
    1830             : 
    1831           0 :   ulong bank_tile_mask = 1UL << bank_tile;
    1832             : 
    1833           0 :   ulong fast_path     = 0UL;
    1834           0 :   ulong slow_path     = 0UL;
    1835           0 :   ulong cu_limit_c    = 0UL;
    1836           0 :   ulong byte_limit_c  = 0UL;
    1837           0 :   ulong alloc_limit_c = 0UL;
    1838           0 :   ulong write_limit_c = 0UL;
    1839           0 :   ulong skip_c        = 0UL;
    1840             : 
    1841           0 :   ulong min_cus   = ULONG_MAX;
    1842           0 :   ulong min_bytes = ULONG_MAX;
    1843             : 
    1844           0 :   if( FD_UNLIKELY( (cu_limit<smallest_in_treap->cus) | (txn_limit==0UL) | (byte_limit<smallest_in_treap->bytes) ) ) {
    1845           0 :     sched_return_t to_return = { .cus_scheduled = 0UL, .txns_scheduled = 0UL, .bytes_scheduled = 0UL };
    1846           0 :     return to_return;
    1847           0 :   }
    1848             : 
    1849           0 :   treap_rev_iter_t prev = treap_idx_null();
    1850           0 :   for( treap_rev_iter_t _cur=treap_rev_iter_init( sched_from, pool ); !treap_rev_iter_done( _cur ); _cur=prev ) {
    1851             :     /* Capture next so that we can delete while we iterate. */
    1852           0 :     prev = treap_rev_iter_next( _cur, pool );
    1853             : 
    1854           0 : #   if FD_HAS_X86
    1855           0 :     _mm_prefetch( &(pool[ prev ].prev),      _MM_HINT_T0 );
    1856           0 : #   endif
    1857             : 
    1858           0 :     fd_pack_ord_txn_t * cur = treap_rev_iter_ele( _cur, pool );
    1859             : 
    1860           0 :     min_cus   = fd_ulong_min( min_cus,   cur->compute_est     );
    1861           0 :     min_bytes = fd_ulong_min( min_bytes, cur->txn->payload_sz );
    1862             : 
    1863           0 :     ulong conflicts = 0UL;
    1864             : 
    1865           0 :     if( FD_UNLIKELY( cur->compute_est>cu_limit ) ) {
    1866             :       /* Too big to be scheduled at the moment, but might be okay for
    1867             :          the next microblock, so we don't want to delay it. */
    1868           0 :       cu_limit_c++;
    1869           0 :       continue;
    1870           0 :     }
    1871             : 
    1872           0 :     if( FD_UNLIKELY( cur->txn->pack_alloc>alloc_limit ) ) {
    1873             :       /* We don't want to consider this until the next block, but
    1874             :          checking alloc is as cheap as checking cur->skip, so there's
    1875             :          not a big difference. */
    1876           0 :       alloc_limit_c++;
    1877           0 :       continue;
    1878           0 :     }
    1879             : 
    1880             :     /* Likely? Unlikely? */
    1881           0 :     if( FD_LIKELY( !FD_PACK_BITSET_INTERSECT4_EMPTY( bitset_rw_in_use, bitset_w_in_use, cur->w_bitset, cur->rw_bitset ) ) ) {
    1882           0 :       fast_path++;
    1883           0 :       continue;
    1884           0 :     }
    1885             : 
    1886           0 :     if( FD_UNLIKELY( cur->skip==compressed_slot_number ) ) {
    1887           0 :       skip_c++;
    1888           0 :       continue;
    1889           0 :     }
    1890             : 
    1891             :     /* If skip>FD_PACK_MAX_SKIP but not compressed_slot_number, it means
    1892             :        it's the compressed slot number of a previous slot.  We don't
    1893             :        care unless we're going to update the value though, so we don't
    1894             :        need to eagerly reset it to FD_PACK_MAX_SKIP.
    1895             :        compressed_slot_number is a ushort, so it's possible for it to
    1896             :        roll over, but the transaction lifetime is much shorter than
    1897             :        that, so it won't be a problem. */
    1898             : 
    1899           0 :     if( FD_UNLIKELY( cur->txn->payload_sz>byte_limit ) ) {
    1900           0 :       byte_limit_c++;
    1901           0 :       continue;
    1902           0 :     }
    1903             : 
    1904             : 
    1905           0 :     fd_txn_t const * txn = TXN(cur->txn);
    1906           0 :     fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, cur->txn->payload );
    1907           0 :     fd_acct_addr_t const * alt_adj = cur->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    1908             :     /* Check conflicts between this transaction's writable accounts and
    1909             :        current readers */
    1910           0 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_WRITABLE );
    1911           0 :         iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    1912             : 
    1913           0 :       fd_acct_addr_t acct = *ACCT_ITER_TO_PTR( iter );
    1914             : 
    1915           0 :       fd_pack_addr_use_t * in_wcost_table = acct_uses_query( writer_costs, acct, NULL );
    1916           0 :       if( FD_UNLIKELY( in_wcost_table && in_wcost_table->total_cost+cur->compute_est > max_write_cost_per_acct ) ) {
    1917             :         /* Can't be scheduled until the next block */
    1918           0 :         conflicts = ULONG_MAX;
    1919           0 :         break;
    1920           0 :       }
    1921             : 
    1922           0 :       fd_pack_addr_use_t * use = acct_uses_query( acct_in_use, acct, NULL );
    1923           0 :       if( FD_UNLIKELY( use ) ) conflicts |= use->in_use_by; /* break? */
    1924           0 :     }
    1925             : 
    1926           0 :     if( FD_UNLIKELY( conflicts==ULONG_MAX ) ) {
    1927             :       /* The logic for how to adjust skip is a bit complicated, and we
    1928             :          want to do it branchlessly. Let psc=FD_PACK_SKIP_CNT,
    1929             :            Before                   After
    1930             :              1               compressed_slot_number
    1931             :            x in [2, psc]             x-1
    1932             :            x where x>psc            psc-1
    1933             : 
    1934             :          Set A=min(x, 5), B=min(A-2, compressed_slot_number-1), and
    1935             :          note that compressed_slot_number is in [psc+1, USHORT_MAX].
    1936             :          Then:
    1937             :              x                A     A-2          B      B+1
    1938             :              1                1  USHORT_MAX    csn-1    csn
    1939             :            x in [2, psc]      x     x-2         x-2     x-1
    1940             :            x where x>psc     psc   psc-2       psc-2   psc-1
    1941             :          So B+1 is the desired value. */
    1942           0 :       cur->skip = (ushort)(1+fd_ushort_min( (ushort)(compressed_slot_number-1),
    1943           0 :                                             (ushort)(fd_ushort_min( cur->skip, FD_PACK_SKIP_CNT )-2) ) );
    1944           0 :       write_limit_c++;
    1945           0 :       continue;
    1946           0 :     }
    1947             : 
    1948           0 :     if( FD_UNLIKELY( conflicts ) ) {
    1949           0 :       slow_path++;
    1950           0 :       continue;
    1951           0 :     }
    1952             : 
    1953             :     /* Check conflicts between this transaction's readonly accounts and
    1954             :        current writers */
    1955           0 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_READONLY );
    1956           0 :         iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    1957             : 
    1958           0 :       fd_acct_addr_t const * acct = ACCT_ITER_TO_PTR( iter );
    1959           0 :       if( fd_pack_unwritable_contains( acct ) ) continue; /* No need to track sysvars because they can't be writable */
    1960             : 
    1961           0 :       fd_pack_addr_use_t * use = acct_uses_query( acct_in_use,  *acct, NULL );
    1962           0 :       if( use ) conflicts |= (use->in_use_by & FD_PACK_IN_USE_WRITABLE) ? use->in_use_by : 0UL;
    1963           0 :     }
    1964             : 
    1965           0 :     if( FD_UNLIKELY( conflicts ) ) {
    1966           0 :       slow_path++;
    1967           0 :       continue;
    1968           0 :     }
    1969             : 
    1970             :     /* Include this transaction in the microblock! */
    1971           0 :     FD_PACK_BITSET_OR( bitset_rw_in_use, cur->rw_bitset );
    1972           0 :     FD_PACK_BITSET_OR( bitset_w_in_use,  cur->w_bitset  );
    1973             : 
    1974           0 :     fd_txn_p_t * out_txnp = out->txnp;
    1975           0 :     if(
    1976           0 : #if FD_HAS_AVX512 && FD_PACK_USE_NON_TEMPORAL_MEMCPY
    1977           0 :         FD_LIKELY( cur->txn->payload_sz>=1024UL )
    1978             : #else
    1979             :         0
    1980             : #endif
    1981           0 :       ) {
    1982           0 : #if FD_HAS_AVX512 && FD_PACK_USE_NON_TEMPORAL_MEMCPY
    1983           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+   0UL), _mm512_load_epi64( cur->txn->payload+   0UL ) );
    1984           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+  64UL), _mm512_load_epi64( cur->txn->payload+  64UL ) );
    1985           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 128UL), _mm512_load_epi64( cur->txn->payload+ 128UL ) );
    1986           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 192UL), _mm512_load_epi64( cur->txn->payload+ 192UL ) );
    1987           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 256UL), _mm512_load_epi64( cur->txn->payload+ 256UL ) );
    1988           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 320UL), _mm512_load_epi64( cur->txn->payload+ 320UL ) );
    1989           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 384UL), _mm512_load_epi64( cur->txn->payload+ 384UL ) );
    1990           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 448UL), _mm512_load_epi64( cur->txn->payload+ 448UL ) );
    1991           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 512UL), _mm512_load_epi64( cur->txn->payload+ 512UL ) );
    1992           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 576UL), _mm512_load_epi64( cur->txn->payload+ 576UL ) );
    1993           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 640UL), _mm512_load_epi64( cur->txn->payload+ 640UL ) );
    1994           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 704UL), _mm512_load_epi64( cur->txn->payload+ 704UL ) );
    1995           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 768UL), _mm512_load_epi64( cur->txn->payload+ 768UL ) );
    1996           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 832UL), _mm512_load_epi64( cur->txn->payload+ 832UL ) );
    1997           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 896UL), _mm512_load_epi64( cur->txn->payload+ 896UL ) );
    1998           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+ 960UL), _mm512_load_epi64( cur->txn->payload+ 960UL ) );
    1999           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+1024UL), _mm512_load_epi64( cur->txn->payload+1024UL ) );
    2000           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+1088UL), _mm512_load_epi64( cur->txn->payload+1088UL ) );
    2001           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+1152UL), _mm512_load_epi64( cur->txn->payload+1152UL ) );
    2002           0 :       _mm512_stream_si512( (void*)(out_txnp->payload+1216UL), _mm512_load_epi64( cur->txn->payload+1216UL ) );
    2003             :       /* Copied out to 1280 bytes, which copies some other fields we needed to
    2004             :          copy anyway. */
    2005           0 :       FD_STATIC_ASSERT( offsetof(fd_txn_p_t, payload_sz     )+sizeof(((fd_txn_p_t*)NULL)->payload_sz    )<=1280UL, nt_memcpy );
    2006           0 :       FD_STATIC_ASSERT( offsetof(fd_txn_p_t, blockhash_slot )+sizeof(((fd_txn_p_t*)NULL)->blockhash_slot)<=1280UL, nt_memcpy );
    2007           0 :       FD_STATIC_ASSERT( offsetof(fd_txn_p_t, scheduler_arrival_time_nanos )+sizeof(((fd_txn_p_t*)NULL)->scheduler_arrival_time_nanos )<=1280UL, nt_memcpy );
    2008           0 :       FD_STATIC_ASSERT( offsetof(fd_txn_p_t, source_tpu     )+sizeof(((fd_txn_p_t*)NULL)->source_tpu    )<=1280UL, nt_memcpy );
    2009           0 :       FD_STATIC_ASSERT( offsetof(fd_txn_p_t, source_ipv4    )+sizeof(((fd_txn_p_t*)NULL)->source_ipv4   )<=1280UL, nt_memcpy );
    2010           0 :       FD_STATIC_ASSERT( offsetof(fd_txn_p_t, flags          )+sizeof(((fd_txn_p_t*)NULL)->flags         )<=1280UL, nt_memcpy );
    2011           0 :       FD_STATIC_ASSERT( offsetof(fd_txn_p_t, _              )                                            <=1280UL, nt_memcpy );
    2012           0 :       const ulong offset_into_txn = 1280UL - offsetof(fd_txn_p_t, _ );
    2013           0 :       fd_memcpy( offset_into_txn+(uchar *)TXN(out_txnp), offset_into_txn+(uchar const *)txn,
    2014           0 :           fd_ulong_max( offset_into_txn, fd_txn_footprint( txn->instr_cnt, txn->addr_table_lookup_cnt ) )-offset_into_txn );
    2015           0 : #endif
    2016           0 :     } else {
    2017           0 :       fd_memcpy( out_txnp->payload, cur->txn->payload, cur->txn->payload_sz                                           );
    2018           0 :       fd_memcpy( TXN(out_txnp),     txn,               fd_txn_footprint( txn->instr_cnt, txn->addr_table_lookup_cnt ) );
    2019           0 :       out_txnp->payload_sz                      = cur->txn->payload_sz;
    2020           0 :       out_txnp->pack_cu.requested_exec_plus_acct_data_cus = cur->txn->pack_cu.requested_exec_plus_acct_data_cus;
    2021           0 :       out_txnp->pack_cu.non_execution_cus       = cur->txn->pack_cu.non_execution_cus;
    2022           0 :       out_txnp->scheduler_arrival_time_nanos    = cur->txn->scheduler_arrival_time_nanos;
    2023           0 :       out_txnp->source_tpu                      = cur->txn->source_tpu;
    2024           0 :       out_txnp->source_ipv4                     = cur->txn->source_ipv4;
    2025           0 :       out_txnp->flags                           = cur->txn->flags;
    2026           0 :     }
    2027             :     /* Copy the ALT accounts from the source fd_txn_e_t */
    2028           0 :     ulong alt_acct_cnt = (ulong)txn->addr_table_adtl_cnt;
    2029           0 : #if FD_HAS_AVX512 && FD_PACK_USE_NON_TEMPORAL_MEMCPY
    2030             :     /* In order to use non-temporal copies, we have to copy a full cache
    2031             :        line (which fits two pubkeys) at a time.  If alt_acct_cnt is odd,
    2032             :        this copies one extra address, but it touches the same number of
    2033             :        cache lines, since both the source and destination are aligned
    2034             :        to 64 bytes. The max is even, so this can never read out of bounds. */
    2035           0 :     fd_acct_addr_t       * dst = out->alt_accts;
    2036           0 :     fd_acct_addr_t const * src = cur->txn_e->alt_accts;
    2037           0 :     for( ulong i=0UL; i<alt_acct_cnt; i+=2UL ) {
    2038           0 :       _mm512_stream_si512( (void*)(dst+i), _mm512_load_epi64( src+i ) );
    2039           0 :     }
    2040             : #else
    2041             :     fd_memcpy( out->alt_accts, cur->txn_e->alt_accts, alt_acct_cnt * sizeof(fd_acct_addr_t) );
    2042             : #endif
    2043           0 :     out++;
    2044             : 
    2045           0 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_WRITABLE );
    2046           0 :         iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    2047           0 :       fd_acct_addr_t acct_addr = *ACCT_ITER_TO_PTR( iter );
    2048             : 
    2049           0 :       fd_pack_addr_use_t * in_wcost_table = acct_uses_query( writer_costs, acct_addr, NULL );
    2050           0 :       if( !in_wcost_table ) {
    2051           0 :         in_wcost_table = acct_uses_insert( writer_costs, acct_addr );
    2052           0 :         in_wcost_table->total_cost = 0UL;
    2053           0 :         written_list[ written_list_cnt ] = in_wcost_table;
    2054           0 :         written_list_cnt = fd_ulong_min( written_list_cnt+1UL, written_list_max-1UL );
    2055           0 :       }
    2056           0 :       in_wcost_table->total_cost += cur->compute_est;
    2057             : 
    2058           0 :       fd_pack_addr_use_t * use = acct_uses_insert( acct_in_use, acct_addr );
    2059           0 :       use->in_use_by = bank_tile_mask | FD_PACK_IN_USE_WRITABLE;
    2060             : 
    2061           0 :       use_by_bank[use_by_bank_cnt++] = *use;
    2062             : 
    2063             :       /* If there aren't any more references to this account in the
    2064             :          heap, it can't cause any conflicts.  That means we actually
    2065             :          don't need to record that we are using it, which is good
    2066             :          because we want to release the bit. */
    2067           0 :       release_result_t ret = release_bit_reference( pack, &acct_addr );
    2068           0 :       FD_PACK_BITSET_CLEARN( bitset_rw_in_use, ret.clear_rw_bit );
    2069           0 :       FD_PACK_BITSET_CLEARN( bitset_w_in_use,  ret.clear_w_bit  );
    2070           0 :     }
    2071           0 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_READONLY );
    2072           0 :         iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    2073             : 
    2074           0 :       fd_acct_addr_t acct_addr = *ACCT_ITER_TO_PTR( iter );
    2075             : 
    2076           0 :       if( fd_pack_unwritable_contains( &acct_addr ) ) continue; /* No need to track sysvars because they can't be writable */
    2077             : 
    2078           0 :       fd_pack_addr_use_t * use = acct_uses_query( acct_in_use,  acct_addr, NULL );
    2079           0 :       if( !use ) { use = acct_uses_insert( acct_in_use, acct_addr ); use->in_use_by = 0UL; }
    2080             : 
    2081           0 :       if( !(use->in_use_by & bank_tile_mask) ) use_by_bank[use_by_bank_cnt++] = *use;
    2082           0 :       use->in_use_by |= bank_tile_mask;
    2083           0 :       use->in_use_by &= ~FD_PACK_IN_USE_BIT_CLEARED;
    2084             : 
    2085             : 
    2086           0 :       release_result_t ret = release_bit_reference( pack, &acct_addr );
    2087           0 :       FD_PACK_BITSET_CLEARN( bitset_rw_in_use, ret.clear_rw_bit );
    2088           0 :       FD_PACK_BITSET_CLEARN( bitset_w_in_use,  ret.clear_w_bit  );
    2089           0 :     }
    2090             : 
    2091           0 :     txns_scheduled  += 1UL;                      txn_limit       -= 1UL;
    2092           0 :     cus_scheduled   += cur->compute_est;         cu_limit        -= cur->compute_est;
    2093           0 :     bytes_scheduled += cur->txn->payload_sz;     byte_limit      -= cur->txn->payload_sz;
    2094           0 :     alloc_scheduled += cur->txn->pack_alloc;     alloc_limit     -= cur->txn->pack_alloc;
    2095             : 
    2096           0 :     *(use_by_bank_txn++) = use_by_bank_cnt;
    2097             : 
    2098           0 :     if( FD_UNLIKELY( cur->txn->flags & FD_TXN_P_FLAGS_DURABLE_NONCE ) ) noncemap_ele_remove_fast( pack->noncemap, cur, pack->pool );
    2099           0 :     sig2txn_ele_remove_fast( pack->signature_map, cur, pool );
    2100             : 
    2101           0 :     cur->root = FD_ORD_TXN_ROOT_FREE;
    2102           0 :     expq_remove( pack->expiration_q, cur->expq_idx );
    2103           0 :     treap_idx_remove( sched_from, _cur, pool );
    2104           0 :     trp_pool_idx_release( pool, _cur );
    2105           0 :     pack->pending_txn_cnt--;
    2106             : 
    2107           0 :     if( FD_UNLIKELY( (cu_limit<smallest_in_treap->cus) | (txn_limit==0UL) | (byte_limit<smallest_in_treap->bytes) ) ) break;
    2108           0 :   }
    2109             : 
    2110           0 :   pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_TAKEN_IDX       ] += txns_scheduled;
    2111           0 :   pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_CU_LIMIT_IDX    ] += cu_limit_c;
    2112           0 :   pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_FAST_PATH_IDX   ] += fast_path;
    2113           0 :   pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_BYTE_LIMIT_IDX  ] += byte_limit_c;
    2114           0 :   pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_ALLOC_LIMIT_IDX ] += alloc_limit_c;
    2115           0 :   pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_WRITE_COST_IDX  ] += write_limit_c;
    2116           0 :   pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_SLOW_PATH_IDX   ] += slow_path;
    2117           0 :   pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_DEFER_SKIP_IDX  ] += skip_c;
    2118             : 
    2119             :   /* If we scanned the whole treap and didn't break early, we now have a
    2120             :      better estimate of the smallest. */
    2121           0 :   if( FD_UNLIKELY( treap_rev_iter_done( prev ) ) ) {
    2122           0 :     smallest_in_treap->cus   = min_cus;
    2123           0 :     smallest_in_treap->bytes = min_bytes;
    2124           0 :   }
    2125             : 
    2126           0 :   pack->use_by_bank_cnt[bank_tile] = use_by_bank_cnt;
    2127           0 :   FD_PACK_BITSET_COPY( pack->bitset_rw_in_use, bitset_rw_in_use );
    2128           0 :   FD_PACK_BITSET_COPY( pack->bitset_w_in_use,  bitset_w_in_use  );
    2129             : 
    2130           0 :   pack->written_list_cnt = written_list_cnt;
    2131             : 
    2132           0 :   sched_return_t to_return = { .cus_scheduled=cus_scheduled,     .txns_scheduled=txns_scheduled,
    2133           0 :                                .bytes_scheduled=bytes_scheduled, .alloc_scheduled=alloc_scheduled };
    2134           0 :   return to_return;
    2135           0 : }
    2136             : 
    2137             : int
    2138             : fd_pack_microblock_complete( fd_pack_t * pack,
    2139           0 :                              ulong       bank_tile ) {
    2140             :   /* If the account is in use writably, and it's in use by this banking
    2141             :      tile, then this banking tile must be the sole writer to it, so it's
    2142             :      always okay to clear the writable bit. */
    2143           0 :   ulong clear_mask = ~((1UL<<bank_tile) | FD_PACK_IN_USE_WRITABLE);
    2144             : 
    2145             :   /* If nothing outstanding, bail quickly */
    2146           0 :   if( FD_UNLIKELY( !(pack->outstanding_microblock_mask & (1UL<<bank_tile)) ) ) return 0;
    2147             : 
    2148           0 :   FD_PACK_BITSET_DECLARE( bitset_rw_in_use );
    2149           0 :   FD_PACK_BITSET_DECLARE( bitset_w_in_use  );
    2150           0 :   FD_PACK_BITSET_COPY( bitset_rw_in_use, pack->bitset_rw_in_use );
    2151           0 :   FD_PACK_BITSET_COPY( bitset_w_in_use,  pack->bitset_w_in_use  );
    2152             : 
    2153           0 :   fd_pack_addr_use_t * base = pack->use_by_bank[bank_tile];
    2154             : 
    2155           0 :   fd_pack_ord_txn_t       * best         = NULL;
    2156           0 :   fd_pack_penalty_treap_t * best_penalty = NULL;
    2157           0 :   ulong                     txn_cnt      = 0UL;
    2158             : 
    2159           0 :   for( ulong i=0UL; i<pack->use_by_bank_cnt[bank_tile]; i++ ) {
    2160           0 :     fd_pack_addr_use_t * use = acct_uses_query( pack->acct_in_use, base[i].key, NULL );
    2161           0 :     FD_TEST( use );
    2162           0 :     use->in_use_by &= clear_mask;
    2163             : 
    2164             :     /* In order to properly bound the size of bitset_map, we need to
    2165             :        release the "reference" to the account when we schedule it.
    2166             :        However, that poses a bit of a problem here, because by the time
    2167             :        we complete the microblock, that account could have been assigned
    2168             :        a different bit in the bitset.  The scheduling step tells us if
    2169             :        that is the case, and if so, we know that the bits in
    2170             :        bitset_w_in_use and bitset_rw_in_use were already cleared as
    2171             :        necessary.
    2172             : 
    2173             :        Note that it's possible for BIT_CLEARED to be set and then unset
    2174             :        by later uses, but then the account would be in use on other
    2175             :        banks, so we wouldn't try to observe the old value.  For example:
    2176             :        Suppose bit 0->account A, bit 1->account B, and we have two
    2177             :        transactions that read A, B.  We schedule a microblock to bank 0,
    2178             :        taking both transactions, which sets the counts for A, B to 0,
    2179             :        and releases the bits, clearing bits 0 and 1, and setting
    2180             :        BIT_CLEARED.  Then we get two more transactions that read
    2181             :        accounts C, D, A, B, and they get assigned 0->C, 1->D, 2->A,
    2182             :        3->B.  We try to schedule a microblock to bank 1 that takes one
    2183             :        of those transactions.  This unsets BIT_CLEARED for A, B.
    2184             :        Finally, the first microblock completes.  Even though the bitset
    2185             :        map has the new bits for A and B which are "wrong" compared to
    2186             :        when the transaction was initially scheduled, those bits have
    2187             :        already been cleared and reset properly in the bitset as needed.
    2188             :        A and B will still be in use by bank 1, so we won't clear any
    2189             :        bits.  If, on the other hand, the microblock scheduled to bank 1
    2190             :        completes first, bits 0 and 1 will be cleared for accounts C and
    2191             :        D, while bits 2 and 3 will remain set, which is correct.  Then
    2192             :        when bank 0 completes, bits 2 and 3 will be cleared. */
    2193           0 :     if( FD_LIKELY( !use->in_use_by ) ) { /* if in_use_by==0, doesn't include BIT_CLEARED */
    2194           0 :       fd_pack_bitset_acct_mapping_t * q = bitset_map_query( pack->acct_to_bitset, base[i].key, NULL );
    2195           0 :       FD_TEST( q );
    2196           0 :       FD_PACK_BITSET_CLEARN( bitset_w_in_use,  q->bit );
    2197           0 :       FD_PACK_BITSET_CLEARN( bitset_rw_in_use, q->bit );
    2198             : 
    2199             :       /* Because this account is no longer in use, it might be possible
    2200             :          to schedule a transaction that writes to it.  Check its
    2201             :          penalty treap if it has one, and potentially move it to the
    2202             :          main treap. */
    2203           0 :       fd_pack_penalty_treap_t * p_trp = penalty_map_query( pack->penalty_treaps, base[i].key, NULL );
    2204           0 :       if( FD_UNLIKELY( p_trp ) ) {
    2205           0 :         fd_pack_ord_txn_t * best_in_trp = treap_rev_iter_ele( treap_rev_iter_init( p_trp->penalty_treap, pack->pool ), pack->pool );
    2206           0 :         if( FD_UNLIKELY( !best || COMPARE_WORSE( best, best_in_trp ) ) ) {
    2207           0 :           best         = best_in_trp;
    2208           0 :           best_penalty = p_trp;
    2209           0 :         }
    2210           0 :       }
    2211           0 :     }
    2212             : 
    2213           0 :     if( FD_LIKELY( !(use->in_use_by & ~FD_PACK_IN_USE_BIT_CLEARED) ) ) acct_uses_remove( pack->acct_in_use, use );
    2214             : 
    2215           0 :     if( FD_UNLIKELY( i+1UL==pack->use_by_bank_txn[ bank_tile ][ txn_cnt ] ) ) {
    2216           0 :       txn_cnt++;
    2217           0 :       if( FD_LIKELY( best ) ) {
    2218             :         /* move best to the main treap */
    2219           0 :         treap_ele_remove( best_penalty->penalty_treap, best, pack->pool );
    2220           0 :         best->root = FD_ORD_TXN_ROOT_PENDING;
    2221           0 :         treap_ele_insert( pack->pending,               best, pack->pool );
    2222             : 
    2223           0 :         pack->pending_smallest->cus   = fd_ulong_min( pack->pending_smallest->cus,   best->compute_est             );
    2224           0 :         pack->pending_smallest->bytes = fd_ulong_min( pack->pending_smallest->bytes, best->txn_e->txnp->payload_sz );
    2225             : 
    2226           0 :         if( FD_UNLIKELY( !treap_ele_cnt( best_penalty->penalty_treap ) ) ) {
    2227           0 :           treap_delete( treap_leave( best_penalty->penalty_treap ) );
    2228             :           /* Removal invalidates any pointers we got from
    2229             :              penalty_map_query, but we immediately set these to NULL, so
    2230             :              we're not keeping any pointers around. */
    2231           0 :           penalty_map_remove( pack->penalty_treaps, best_penalty );
    2232           0 :         }
    2233           0 :         best         = NULL;
    2234           0 :         best_penalty = NULL;
    2235           0 :       }
    2236           0 :     }
    2237           0 :   }
    2238             : 
    2239           0 :   pack->use_by_bank_cnt[bank_tile] = 0UL;
    2240             : 
    2241           0 :   FD_PACK_BITSET_COPY( pack->bitset_rw_in_use, bitset_rw_in_use );
    2242           0 :   FD_PACK_BITSET_COPY( pack->bitset_w_in_use,  bitset_w_in_use  );
    2243             : 
    2244             :   /* outstanding_microblock_mask never has the writable bit set, so we
    2245             :      don't care about clearing it here either. */
    2246           0 :   pack->outstanding_microblock_mask &= clear_mask;
    2247           0 :   return 1;
    2248           0 : }
    2249             : 
    2250           0 : #define TRY_BUNDLE_NO_READY_BUNDLES      0
    2251           0 : #define TRY_BUNDLE_HAS_CONFLICTS       (-1)
    2252           0 : #define TRY_BUNDLE_DOES_NOT_FIT        (-2)
    2253           0 : #define TRY_BUNDLE_SUCCESS(n)          ( n) /* schedule bundle with n transactions */
    2254             : static inline int
    2255             : fd_pack_try_schedule_bundle( fd_pack_t  * pack,
    2256             :                              ulong        bank_tile,
    2257           0 :                              fd_txn_e_t * out ) {
    2258           0 :   int state = pack->initializer_bundle_state;
    2259           0 :   if( FD_UNLIKELY( (state==FD_PACK_IB_STATE_PENDING) | (state==FD_PACK_IB_STATE_FAILED ) ) ) return TRY_BUNDLE_NO_READY_BUNDLES;
    2260             : 
    2261           0 :   fd_pack_ord_txn_t * pool    = pack->pool;
    2262           0 :   treap_t           * bundles = pack->pending_bundles;
    2263             : 
    2264           0 :   int require_ib;
    2265           0 :   if( FD_UNLIKELY( state==FD_PACK_IB_STATE_NOT_INITIALIZED ) ) { require_ib = 1; }
    2266           0 :   if( FD_LIKELY  ( state==FD_PACK_IB_STATE_READY           ) ) { require_ib = 0; }
    2267             : 
    2268           0 :   treap_rev_iter_t _cur  = treap_rev_iter_init( bundles, pool );
    2269           0 :   ulong bundle_idx = ULONG_MAX;
    2270             : 
    2271             :   /* Skip any that we've marked as won't fit in this block */
    2272           0 :   while( FD_UNLIKELY( !treap_rev_iter_done( _cur ) && treap_rev_iter_ele( _cur, pool )->skip==pack->compressed_slot_number ) ) {
    2273           0 :     _cur = treap_rev_iter_next( _cur, pool );
    2274           0 :     pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_DEFER_SKIP_IDX ]++;
    2275           0 :   }
    2276             : 
    2277           0 :   if( FD_UNLIKELY( treap_rev_iter_done( _cur ) ) ) return TRY_BUNDLE_NO_READY_BUNDLES;
    2278             : 
    2279           0 :   treap_rev_iter_t   _txn0 = _cur;
    2280           0 :   fd_pack_ord_txn_t * txn0 = treap_rev_iter_ele( _txn0, pool );
    2281           0 :   int is_ib = !!(txn0->txn->flags & FD_TXN_P_FLAGS_INITIALIZER_BUNDLE);
    2282           0 :   bundle_idx = RC_TO_REL_BUNDLE_IDX( txn0->rewards, txn0->compute_est );
    2283             : 
    2284           0 :   if( FD_UNLIKELY( require_ib & !is_ib ) ) return TRY_BUNDLE_NO_READY_BUNDLES;
    2285             : 
    2286             :   /* At this point, we have our candidate bundle, so we'll schedule it
    2287             :      if we can.  If we can't, we won't schedule anything. */
    2288             : 
    2289             : 
    2290           0 :   fd_pack_addr_use_t * bundle_temp_inserted[ FD_PACK_MAX_TXN_PER_BUNDLE * FD_TXN_ACCT_ADDR_MAX ];
    2291           0 :   ulong bundle_temp_inserted_cnt = 0UL;
    2292             : 
    2293           0 :   ulong bank_tile_mask = 1UL << bank_tile;
    2294             : 
    2295           0 :   int doesnt_fit   = 0;
    2296           0 :   int has_conflict = 0;
    2297           0 :   ulong txn_cnt = 0UL;
    2298             : 
    2299           0 :   ulong cu_limit         = pack->lim->max_cost_per_block           - pack->cumulative_block_cost;
    2300           0 :   ulong byte_limit       = pack->lim->max_data_bytes_per_block     - pack->data_bytes_consumed;
    2301           0 :   ulong microblock_limit = pack->lim->max_microblocks_per_block    - pack->microblock_cnt;
    2302           0 :   ulong alloc_limit      = pack->lim->max_allocated_data_per_block - pack->alloc_consumed;
    2303             : 
    2304           0 :   FD_PACK_BITSET_DECLARE( bitset_rw_in_use );
    2305           0 :   FD_PACK_BITSET_DECLARE( bitset_w_in_use  );
    2306           0 :   FD_PACK_BITSET_COPY( bitset_rw_in_use, pack->bitset_rw_in_use );
    2307           0 :   FD_PACK_BITSET_COPY( bitset_w_in_use,  pack->bitset_w_in_use  );
    2308             : 
    2309             :   /* last_use_in_txn_cnt[i+1] Keeps track of the number of accounts that
    2310             :      have their last reference in transaction i of the bundle.  This
    2311             :      esoteric value is important for computing use_by_bank_txn.
    2312             :      last_use_in_txn_cnt[0] is garbage. */
    2313           0 :   ulong last_use_in_txn_cnt[ 1UL+FD_PACK_MAX_TXN_PER_BUNDLE ] = { 0UL };
    2314             : 
    2315           0 :   fd_pack_addr_use_t   null_use[1]    = {{{{ 0 }}, { 0 }}};
    2316             : 
    2317           0 :   while( !(doesnt_fit | has_conflict) & !treap_rev_iter_done( _cur ) ) {
    2318           0 :     fd_pack_ord_txn_t * cur = treap_rev_iter_ele( _cur, pool );
    2319           0 :     ulong this_bundle_idx = RC_TO_REL_BUNDLE_IDX( cur->rewards, cur->compute_est );
    2320           0 :     if( FD_UNLIKELY( this_bundle_idx!=bundle_idx ) ) break;
    2321             : 
    2322           0 :     if( FD_UNLIKELY( cur->compute_est>cu_limit ) ) {
    2323           0 :       doesnt_fit = 1;
    2324           0 :       pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_CU_LIMIT_IDX ]++;
    2325           0 :       break;
    2326           0 :     }
    2327           0 :     cu_limit -= cur->compute_est;
    2328             : 
    2329             :     /* Each transaction in a bundle turns into a microblock */
    2330           0 :     if( FD_UNLIKELY( microblock_limit==0UL ) ) {
    2331           0 :       doesnt_fit = 1;
    2332           0 :       FD_MCNT_INC( PACK, MICROBLOCK_PER_BLOCK_LIMIT, 1UL );
    2333           0 :       break;
    2334           0 :     }
    2335           0 :     microblock_limit--;
    2336             : 
    2337           0 :     if( FD_UNLIKELY( cur->txn->payload_sz+MICROBLOCK_DATA_OVERHEAD>byte_limit ) ) {
    2338           0 :       doesnt_fit = 1;
    2339           0 :       pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_BYTE_LIMIT_IDX ]++;
    2340           0 :       break;
    2341           0 :     }
    2342           0 :     byte_limit -= cur->txn->payload_sz + MICROBLOCK_DATA_OVERHEAD;
    2343             : 
    2344           0 :     if( FD_UNLIKELY( cur->txn->pack_alloc>alloc_limit ) ) {
    2345           0 :       doesnt_fit = 1;
    2346           0 :       pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_ALLOC_LIMIT_IDX ]++;
    2347           0 :       break;
    2348           0 :     }
    2349           0 :     alloc_limit -= cur->txn->pack_alloc;
    2350             : 
    2351           0 :     if( FD_UNLIKELY( !FD_PACK_BITSET_INTERSECT4_EMPTY( pack->bitset_rw_in_use, pack->bitset_w_in_use, cur->w_bitset, cur->rw_bitset ) ) ) {
    2352           0 :       has_conflict = 1;
    2353           0 :       pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_FAST_PATH_IDX ]++;
    2354           0 :       break;
    2355           0 :     }
    2356             : 
    2357             :     /* Don't update the actual in-use bitset, because the transactions
    2358             :        in the bundle are allowed to conflict with each other. */
    2359           0 :     FD_PACK_BITSET_OR( bitset_rw_in_use, cur->rw_bitset );
    2360           0 :     FD_PACK_BITSET_OR( bitset_w_in_use,  cur->w_bitset  );
    2361             : 
    2362             : 
    2363           0 :     fd_txn_t const * txn = TXN(cur->txn);
    2364           0 :     fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, cur->txn->payload );
    2365           0 :     fd_acct_addr_t const * alt_adj = cur->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    2366             : 
    2367             :     /* Check conflicts between this transaction's writable accounts and
    2368             :        current readers */
    2369           0 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_WRITABLE );
    2370           0 :         iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    2371             : 
    2372           0 :       fd_acct_addr_t acct = *ACCT_ITER_TO_PTR( iter );
    2373             : 
    2374           0 :       fd_pack_addr_use_t * in_bundle_temp = acct_uses_query( pack->bundle_temp_map, acct, null_use );
    2375           0 :       ulong current_cost                  = acct_uses_query( pack->writer_costs,    acct, null_use )->total_cost;
    2376           0 :       ulong carried_cost                  = (ulong)in_bundle_temp->carried_cost;
    2377           0 :       if( FD_UNLIKELY( current_cost + carried_cost + cur->compute_est > pack->lim->max_write_cost_per_acct ) ) {
    2378           0 :         doesnt_fit = 1;
    2379           0 :         pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_WRITE_COST_IDX ]++;
    2380           0 :         break;
    2381           0 :       }
    2382             : 
    2383           0 :       if( FD_LIKELY( in_bundle_temp==null_use ) ) { /* Not in temp bundle table yet */
    2384           0 :         in_bundle_temp    = acct_uses_insert( pack->bundle_temp_map, acct );
    2385           0 :         in_bundle_temp->_ = 0UL;
    2386           0 :         bundle_temp_inserted[ bundle_temp_inserted_cnt++ ] = in_bundle_temp;
    2387           0 :       }
    2388           0 :       in_bundle_temp->carried_cost += (uint)cur->compute_est; /* < 2^21, but >0 */
    2389           0 :       in_bundle_temp->ref_cnt++;
    2390           0 :       last_use_in_txn_cnt[ in_bundle_temp->last_use_in ]--;
    2391           0 :       in_bundle_temp->last_use_in = (ushort)(txn_cnt+1UL);
    2392           0 :       last_use_in_txn_cnt[ in_bundle_temp->last_use_in ]++;
    2393             : 
    2394           0 :       if( FD_UNLIKELY( acct_uses_query( pack->acct_in_use, acct, null_use )->in_use_by ) ) {
    2395           0 :         has_conflict = 1;
    2396           0 :         pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_SLOW_PATH_IDX ]++;
    2397           0 :         break;
    2398           0 :       }
    2399           0 :     }
    2400           0 :     if( has_conflict | doesnt_fit ) break;
    2401             : 
    2402             :     /* Check conflicts between this transaction's readonly accounts and
    2403             :        current writers */
    2404           0 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_READONLY );
    2405           0 :         iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    2406             : 
    2407           0 :       fd_acct_addr_t const * acct = ACCT_ITER_TO_PTR( iter );
    2408           0 :       if( fd_pack_unwritable_contains( acct ) ) continue; /* No need to track sysvars because they can't be writable */
    2409             : 
    2410           0 :       fd_pack_addr_use_t * in_bundle_temp = acct_uses_query( pack->bundle_temp_map, *acct, null_use );
    2411           0 :       if( FD_LIKELY( in_bundle_temp==null_use ) ) { /* Not in temp bundle table yet */
    2412           0 :         in_bundle_temp = acct_uses_insert( pack->bundle_temp_map, *acct );
    2413           0 :         in_bundle_temp->_ = 0UL;
    2414           0 :         bundle_temp_inserted[ bundle_temp_inserted_cnt++ ] = in_bundle_temp;
    2415           0 :       }
    2416           0 :       in_bundle_temp->ref_cnt++;
    2417           0 :       last_use_in_txn_cnt[ in_bundle_temp->last_use_in ]--;
    2418           0 :       in_bundle_temp->last_use_in = (ushort)(txn_cnt+1UL);
    2419           0 :       last_use_in_txn_cnt[ in_bundle_temp->last_use_in ]++;
    2420             : 
    2421           0 :       if( FD_UNLIKELY( acct_uses_query( pack->acct_in_use,  *acct, null_use )->in_use_by & FD_PACK_IN_USE_WRITABLE ) ) {
    2422           0 :         has_conflict = 1;
    2423           0 :         pack->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_SLOW_PATH_IDX ]++;
    2424           0 :         break;
    2425           0 :       }
    2426           0 :     }
    2427             : 
    2428           0 :     if( has_conflict | doesnt_fit ) break;
    2429             : 
    2430           0 :     txn_cnt++;
    2431           0 :     _cur = treap_rev_iter_next( _cur, pool );
    2432           0 :   }
    2433           0 :   int retval = fd_int_if( doesnt_fit, TRY_BUNDLE_DOES_NOT_FIT,
    2434           0 :                                       fd_int_if( has_conflict, TRY_BUNDLE_HAS_CONFLICTS, TRY_BUNDLE_SUCCESS( (int)txn_cnt ) ) );
    2435             : 
    2436           0 :   if( FD_UNLIKELY( retval<=0 ) ) {
    2437           0 :     for( ulong i=0UL; i<bundle_temp_inserted_cnt; i++ ) {
    2438           0 :       acct_uses_remove( pack->bundle_temp_map, bundle_temp_inserted[ bundle_temp_inserted_cnt-i-1UL ] );
    2439           0 :     }
    2440           0 :     FD_TEST( acct_uses_key_cnt( pack->bundle_temp_map )==0UL );
    2441             : 
    2442           0 :     if( FD_UNLIKELY( retval==TRY_BUNDLE_DOES_NOT_FIT ) ) {
    2443             :       /* Decrement the skip count for the bundle we just tried. */
    2444             : 
    2445           0 :       for( _cur=_txn0; !treap_rev_iter_done( _cur ); _cur=treap_rev_iter_next( _cur, pool ) ) {
    2446           0 :         fd_pack_ord_txn_t * cur = treap_rev_iter_ele( _cur, pool );
    2447           0 :         ulong this_bundle_idx = RC_TO_REL_BUNDLE_IDX( cur->rewards, cur->compute_est );
    2448           0 :         if( FD_UNLIKELY( this_bundle_idx!=bundle_idx ) ) break;
    2449             : 
    2450             :         /* See fd_pack_schedule_impl for this line */
    2451           0 :         cur->skip = (ushort)(1+fd_ushort_min( (ushort)(pack->compressed_slot_number-1),
    2452           0 :               (ushort)(fd_ushort_min( cur->skip, FD_PACK_SKIP_CNT )-2) ) );
    2453           0 :       }
    2454           0 :     }
    2455           0 :     return retval;
    2456           0 :   }
    2457             : 
    2458             :   /* This bundle passed validation, so now we'll take it! */
    2459           0 :   pack->outstanding_microblock_mask |= bank_tile_mask;
    2460             : 
    2461           0 :   treap_rev_iter_t   _end  = _cur;
    2462           0 :   treap_rev_iter_t   _next;
    2463             : 
    2464             :   /* We'll carefully incrementally construct use_by_bank and
    2465             :      use_by_bank_txn based on the contents of bundle_temp and
    2466             :      last_use_in_txn_cnt. */
    2467           0 :   fd_pack_addr_use_t * use_by_bank     = pack->use_by_bank    [bank_tile];
    2468           0 :   ulong              * use_by_bank_txn = pack->use_by_bank_txn[bank_tile];
    2469           0 :   ulong cum_sum = 0UL;
    2470           0 :   for( ulong k=0UL; k<txn_cnt; k++ ) { use_by_bank_txn[k] = cum_sum; cum_sum += last_use_in_txn_cnt[ k+1UL ]; }
    2471           0 :   pack->use_by_bank_cnt[bank_tile] = cum_sum;
    2472             : 
    2473             : 
    2474           0 :   for( _cur=_txn0; _cur!=_end; _cur=_next ) {
    2475           0 :     _next = treap_rev_iter_next( _cur, pool );
    2476             : 
    2477           0 :     fd_pack_ord_txn_t * cur = treap_rev_iter_ele( _cur, pool );
    2478           0 :     fd_txn_t const    * txn = TXN(cur->txn);
    2479           0 :     fd_txn_p_t        * out_txnp = out->txnp;
    2480           0 :     fd_memcpy( out_txnp->payload, cur->txn->payload, cur->txn->payload_sz                                           );
    2481           0 :     fd_memcpy( TXN(out_txnp),     txn,               fd_txn_footprint( txn->instr_cnt, txn->addr_table_lookup_cnt ) );
    2482           0 :     out_txnp->payload_sz                      = cur->txn->payload_sz;
    2483           0 :     out_txnp->pack_cu.requested_exec_plus_acct_data_cus = cur->txn->pack_cu.requested_exec_plus_acct_data_cus;
    2484           0 :     out_txnp->pack_cu.non_execution_cus       = cur->txn->pack_cu.non_execution_cus;
    2485           0 :     out_txnp->scheduler_arrival_time_nanos    = cur->txn->scheduler_arrival_time_nanos;
    2486           0 :     out_txnp->source_tpu                      = cur->txn->source_tpu;
    2487           0 :     out_txnp->source_ipv4                     = cur->txn->source_ipv4;
    2488           0 :     out_txnp->flags                           = cur->txn->flags;
    2489             :     /* Copy the ALT accounts from the source fd_txn_e_t */
    2490           0 :     ulong alt_acct_cnt = (ulong)txn->addr_table_adtl_cnt;
    2491           0 :     fd_memcpy( out->alt_accts, cur->txn_e->alt_accts, alt_acct_cnt * sizeof(fd_acct_addr_t) );
    2492           0 :     out++;
    2493             : 
    2494           0 :     pack->cumulative_block_cost += cur->compute_est;
    2495           0 :     pack->data_bytes_consumed   += cur->txn->payload_sz + MICROBLOCK_DATA_OVERHEAD;
    2496           0 :     pack->alloc_consumed        += cur->txn->pack_alloc;
    2497           0 :     pack->microblock_cnt        += 1UL;
    2498             : 
    2499           0 :     if( FD_UNLIKELY( cur->txn->flags & FD_TXN_P_FLAGS_DURABLE_NONCE ) ) noncemap_ele_remove_fast( pack->noncemap, cur, pack->pool );
    2500           0 :     sig2txn_ele_remove_fast( pack->signature_map, cur, pack->pool );
    2501             : 
    2502           0 :     cur->root = FD_ORD_TXN_ROOT_FREE;
    2503           0 :     expq_remove( pack->expiration_q, cur->expq_idx );
    2504           0 :     treap_idx_remove( pack->pending_bundles, _cur, pack->pool );
    2505           0 :     trp_pool_idx_release( pack->pool, _cur );
    2506           0 :     pack->pending_txn_cnt--;
    2507           0 :   }
    2508             : 
    2509             : 
    2510           0 :   for( ulong i=0UL; i<bundle_temp_inserted_cnt; i++ ) {
    2511             :     /* In order to clear bundle_temp_map with the typical trick, we need
    2512             :        to iterate through bundle_temp_inserted backwards. */
    2513           0 :     fd_pack_addr_use_t * addr_use = bundle_temp_inserted[ bundle_temp_inserted_cnt-i-1UL ];
    2514             : 
    2515           0 :     int any_writers = addr_use->carried_cost>0U; /* Did any transaction in this bundle write lock this account address? */
    2516             : 
    2517           0 :     if( FD_LIKELY( any_writers ) ) { /* UNLIKELY? */
    2518           0 :       fd_pack_addr_use_t * in_wcost_table = acct_uses_query( pack->writer_costs, addr_use->key, NULL );
    2519           0 :       if( !in_wcost_table ) {
    2520           0 :         in_wcost_table = acct_uses_insert( pack->writer_costs, addr_use->key );
    2521           0 :         in_wcost_table->total_cost = 0UL;
    2522           0 :         pack->written_list[ pack->written_list_cnt ] = in_wcost_table;
    2523           0 :         pack->written_list_cnt = fd_ulong_min( pack->written_list_cnt+1UL, pack->written_list_max-1UL );
    2524           0 :       }
    2525           0 :       in_wcost_table->total_cost += (ulong)addr_use->carried_cost;
    2526           0 :     }
    2527             : 
    2528             :     /* in_use_by must be set before releasing the bit reference */
    2529           0 :     fd_pack_addr_use_t * use = acct_uses_query( pack->acct_in_use, addr_use->key, NULL );
    2530           0 :     if( !use ) { use = acct_uses_insert( pack->acct_in_use, addr_use->key ); use->in_use_by = 0UL; }
    2531           0 :     use->in_use_by |= bank_tile_mask | fd_ulong_if( any_writers, FD_PACK_IN_USE_WRITABLE, 0UL );
    2532           0 :     use->in_use_by &= ~FD_PACK_IN_USE_BIT_CLEARED;
    2533             : 
    2534           0 :     use_by_bank[ use_by_bank_txn[ addr_use->last_use_in-1UL ]++ ] = *use;
    2535             : 
    2536           0 :     for( ulong k=0UL; k<(ulong)addr_use->ref_cnt; k++ ) {
    2537           0 :       release_result_t ret = release_bit_reference( pack, &(addr_use->key) );
    2538           0 :       FD_PACK_BITSET_CLEARN( bitset_rw_in_use, ret.clear_rw_bit );
    2539           0 :       FD_PACK_BITSET_CLEARN( bitset_w_in_use,  ret.clear_w_bit  );
    2540           0 :     }
    2541             : 
    2542           0 :     acct_uses_remove( pack->bundle_temp_map, addr_use );
    2543           0 :   }
    2544             : 
    2545           0 :   FD_PACK_BITSET_COPY( pack->bitset_rw_in_use, bitset_rw_in_use );
    2546           0 :   FD_PACK_BITSET_COPY( pack->bitset_w_in_use,  bitset_w_in_use  );
    2547             : 
    2548           0 :   if( FD_UNLIKELY( is_ib ) ) {
    2549           0 :     pack->initializer_bundle_state = FD_PACK_IB_STATE_PENDING;
    2550           0 :   }
    2551           0 :   return retval;
    2552           0 : }
    2553             : 
    2554             : 
    2555             : ulong
    2556             : fd_pack_schedule_next_microblock( fd_pack_t *  pack,
    2557             :                                   ulong        total_cus,
    2558             :                                   float        vote_fraction,
    2559             :                                   ulong        bank_tile,
    2560             :                                   int          schedule_flags,
    2561           0 :                                   fd_txn_e_t * out ) {
    2562             : 
    2563             :   /* TODO: Decide if these are exactly how we want to handle limits */
    2564           0 :   total_cus = fd_ulong_min( total_cus, pack->lim->max_cost_per_block - pack->cumulative_block_cost );
    2565           0 :   ulong vote_cus = fd_ulong_min( (ulong)((float)total_cus * vote_fraction),
    2566           0 :                                  pack->lim->max_vote_cost_per_block - pack->cumulative_vote_cost );
    2567           0 :   ulong vote_reserved_txns = fd_ulong_min( vote_cus/FD_PACK_SIMPLE_VOTE_COST,
    2568           0 :                                            (ulong)((float)pack->lim->max_txn_per_microblock * vote_fraction) );
    2569             : 
    2570             : 
    2571           0 :   if( FD_UNLIKELY( (pack->microblock_cnt>=pack->lim->max_microblocks_per_block) ) ) {
    2572           0 :     FD_MCNT_INC( PACK, MICROBLOCK_PER_BLOCK_LIMIT, 1UL );
    2573           0 :     return 0UL;
    2574           0 :   }
    2575           0 :   if( FD_UNLIKELY( pack->data_bytes_consumed+MICROBLOCK_DATA_OVERHEAD+FD_TXN_MIN_SERIALIZED_SZ>pack->lim->max_data_bytes_per_block) ) {
    2576           0 :     FD_MCNT_INC( PACK, DATA_PER_BLOCK_LIMIT, 1UL );
    2577           0 :     return 0UL;
    2578           0 :   }
    2579             : 
    2580           0 :   ulong * use_by_bank_txn = pack->use_by_bank_txn[ bank_tile ];
    2581             : 
    2582           0 :   ulong cu_limit    = total_cus - vote_cus;
    2583           0 :   ulong txn_limit   = pack->lim->max_txn_per_microblock - vote_reserved_txns;
    2584           0 :   ulong scheduled   = 0UL;
    2585           0 :   ulong byte_limit  = pack->lim->max_data_bytes_per_block - pack->data_bytes_consumed - MICROBLOCK_DATA_OVERHEAD;
    2586           0 :   ulong alloc_limit = pack->lim->max_allocated_data_per_block - pack->alloc_consumed;
    2587             : 
    2588           0 :   sched_return_t status = {0}, status1 = {0};
    2589             : 
    2590           0 :   if( FD_LIKELY( schedule_flags & FD_PACK_SCHEDULE_VOTE ) ) {
    2591             :     /* Schedule vote transactions */
    2592           0 :     status1= fd_pack_schedule_impl( pack, pack->pending_votes, vote_cus, vote_reserved_txns, byte_limit, alloc_limit, bank_tile,
    2593           0 :         pack->pending_votes_smallest, use_by_bank_txn, out+scheduled );
    2594             : 
    2595           0 :     scheduled                   += status1.txns_scheduled;
    2596           0 :     pack->cumulative_vote_cost  += status1.cus_scheduled;
    2597           0 :     pack->cumulative_block_cost += status1.cus_scheduled;
    2598           0 :     pack->data_bytes_consumed   += status1.bytes_scheduled;
    2599           0 :     byte_limit                  -= status1.bytes_scheduled;
    2600           0 :     pack->alloc_consumed        += status1.alloc_scheduled;
    2601           0 :     alloc_limit                 -= status1.alloc_scheduled;
    2602           0 :     use_by_bank_txn             += status1.txns_scheduled;
    2603             :     /* Add any remaining CUs/txns to the non-vote limits */
    2604           0 :     txn_limit += vote_reserved_txns - status1.txns_scheduled;
    2605           0 :     cu_limit  += vote_cus - status1.cus_scheduled;
    2606           0 :   }
    2607             : 
    2608             :   /* Bundle can't mix with votes, so only try to schedule a bundle if we
    2609             :      didn't get any votes. */
    2610           0 :   if( FD_UNLIKELY( !!(schedule_flags & FD_PACK_SCHEDULE_BUNDLE) & (status1.txns_scheduled==0UL) ) ) {
    2611           0 :     int bundle_result = fd_pack_try_schedule_bundle( pack, bank_tile, out );
    2612           0 :     if( FD_UNLIKELY( bundle_result>0                         ) ) return (ulong)bundle_result;
    2613           0 :     if( FD_UNLIKELY( bundle_result==TRY_BUNDLE_HAS_CONFLICTS ) ) return 0UL;
    2614             :     /* in the NO_READY_BUNDLES or DOES_NOT_FIT case, we schedule like
    2615             :        normal. */
    2616             :     /* We have the early returns here because try_schedule_bundle does
    2617             :        the bookeeping internally, since the calculations are a bit
    2618             :        different in that case. */
    2619           0 :   }
    2620             : 
    2621             : 
    2622             :   /* Fill any remaining space with non-vote transactions */
    2623           0 :   if( FD_LIKELY( schedule_flags & FD_PACK_SCHEDULE_TXN ) ) {
    2624           0 :     status = fd_pack_schedule_impl( pack, pack->pending,       cu_limit, txn_limit,          byte_limit, alloc_limit, bank_tile,
    2625           0 :         pack->pending_smallest,       use_by_bank_txn, out+scheduled );
    2626             : 
    2627           0 :     scheduled                   += status.txns_scheduled;
    2628           0 :     pack->cumulative_block_cost += status.cus_scheduled;
    2629           0 :     pack->data_bytes_consumed   += status.bytes_scheduled;
    2630           0 :     pack->alloc_consumed        += status.alloc_scheduled;
    2631           0 :   }
    2632             : 
    2633           0 :   ulong nonempty = (ulong)(scheduled>0UL);
    2634           0 :   pack->microblock_cnt              += nonempty;
    2635           0 :   pack->outstanding_microblock_mask |= nonempty << bank_tile;
    2636           0 :   pack->data_bytes_consumed         += nonempty * MICROBLOCK_DATA_OVERHEAD;
    2637             : 
    2638             :   /* Update metrics counters */
    2639           0 :   fd_pack_metrics_write( pack );
    2640           0 :   FD_MGAUGE_SET( PACK, CUS_CONSUMED_IN_BLOCK,         pack->cumulative_block_cost          );
    2641             : 
    2642           0 :   fd_histf_sample( pack->txn_per_microblock,  scheduled              );
    2643           0 :   fd_histf_sample( pack->vote_per_microblock, status1.txns_scheduled );
    2644             : 
    2645           0 : #if FD_HAS_AVX512 && FD_PACK_USE_NON_TEMPORAL_MEMCPY
    2646           0 :   _mm_sfence();
    2647           0 : #endif
    2648             : 
    2649           0 :   return scheduled;
    2650           0 : }
    2651             : 
    2652           0 : ulong fd_pack_bank_tile_cnt     ( fd_pack_t const * pack ) { return pack->bank_tile_cnt;         }
    2653           0 : ulong fd_pack_current_block_cost( fd_pack_t const * pack ) { return pack->cumulative_block_cost; }
    2654             : 
    2655             : 
    2656             : void
    2657           0 : fd_pack_set_block_limits( fd_pack_t * pack, fd_pack_limits_t const * limits ) {
    2658           0 :   FD_TEST( limits->max_cost_per_block      >= FD_PACK_MAX_COST_PER_BLOCK_LOWER_BOUND      );
    2659           0 :   FD_TEST( limits->max_vote_cost_per_block >= FD_PACK_MAX_VOTE_COST_PER_BLOCK_LOWER_BOUND );
    2660           0 :   FD_TEST( limits->max_write_cost_per_acct >= FD_PACK_MAX_WRITE_COST_PER_ACCT_LOWER_BOUND );
    2661             : 
    2662           0 :   pack->lim->max_microblocks_per_block    = limits->max_microblocks_per_block;
    2663           0 :   pack->lim->max_data_bytes_per_block     = limits->max_data_bytes_per_block;
    2664           0 :   pack->lim->max_cost_per_block           = limits->max_cost_per_block;
    2665           0 :   pack->lim->max_vote_cost_per_block      = limits->max_vote_cost_per_block;
    2666           0 :   pack->lim->max_write_cost_per_acct      = limits->max_write_cost_per_acct;
    2667           0 :   pack->lim->max_allocated_data_per_block = limits->max_allocated_data_per_block;
    2668           0 : }
    2669             : 
    2670             : void
    2671           0 : fd_pack_get_block_limits( fd_pack_t * pack, fd_pack_limits_usage_t * opt_limits_usage, fd_pack_limits_t * opt_limits ) {
    2672           0 :   if( FD_LIKELY( opt_limits_usage ) ) {
    2673           0 :     opt_limits_usage->block_cost          = pack->cumulative_block_cost;
    2674           0 :     opt_limits_usage->vote_cost           = pack->cumulative_vote_cost;
    2675           0 :     opt_limits_usage->block_data_bytes    = pack->data_bytes_consumed;
    2676           0 :     opt_limits_usage->microblocks         = pack->microblock_cnt;
    2677           0 :     opt_limits_usage->alloc               = pack->alloc_consumed;
    2678           0 :   }
    2679           0 :   if( FD_LIKELY( opt_limits ) ) fd_memcpy( opt_limits, pack->lim, sizeof(fd_pack_limits_t) );
    2680           0 : }
    2681             : 
    2682             : void
    2683           0 : fd_pack_get_top_writers( fd_pack_t const * pack, fd_pack_addr_use_t top_writers[static FD_PACK_TOP_WRITERS_CNT] ) {
    2684           0 :   fd_memcpy( top_writers, pack->top_writers, sizeof(pack->top_writers) );
    2685           0 : }
    2686             : 
    2687             : void
    2688           0 : fd_pack_get_pending_smallest( fd_pack_t * pack, fd_pack_smallest_t * opt_pending_smallest, fd_pack_smallest_t * opt_votes_smallest ) {
    2689           0 :   if( FD_LIKELY( opt_pending_smallest ) ) fd_memcpy( opt_pending_smallest, pack->pending_smallest,       sizeof(fd_pack_smallest_t) );
    2690           0 :   if( FD_LIKELY( opt_votes_smallest ) )   fd_memcpy( opt_votes_smallest,   pack->pending_votes_smallest, sizeof(fd_pack_smallest_t) );
    2691           0 : }
    2692             : 
    2693             : void
    2694             : fd_pack_rebate_cus( fd_pack_t              * pack,
    2695           0 :                     fd_pack_rebate_t const * rebate ) {
    2696           0 :   if( FD_UNLIKELY( (rebate->ib_result!=0) & (pack->initializer_bundle_state==FD_PACK_IB_STATE_PENDING ) ) ) {
    2697           0 :     pack->initializer_bundle_state = fd_int_if( rebate->ib_result==1, FD_PACK_IB_STATE_READY, FD_PACK_IB_STATE_FAILED );
    2698           0 :   }
    2699             : 
    2700           0 :   pack->cumulative_block_cost  -= rebate->total_cost_rebate;
    2701           0 :   pack->cumulative_vote_cost   -= rebate->vote_cost_rebate;
    2702           0 :   pack->data_bytes_consumed    -= rebate->data_bytes_rebate;
    2703           0 :   pack->alloc_consumed         -= rebate->alloc_rebate;
    2704           0 :   pack->cumulative_rebated_cus += rebate->total_cost_rebate;
    2705             :   /* For now, we want to ignore the microblock count rebate.  There are
    2706             :      3 places the microblock count is kept (here, in the pack tile, and
    2707             :      in the PoH tile), and they all need to count microblocks that end
    2708             :      up being empty in the same way.  It would be better from a
    2709             :      DoS-resistance perspective for them all not to count empty
    2710             :      microblocks towards the total, but there's a race condition:
    2711             :      suppose pack schedules a microblock containing one transaction that
    2712             :      doesn't land on chain, the slot ends, and then pack informs PoH of
    2713             :      the number of microblocks before the final rebate comes through.
    2714             :      This isn't unsolvable, but it's pretty gross, so it's probably
    2715             :      better to just not apply the rebate for now. */
    2716           0 :   (void)rebate->microblock_cnt_rebate;
    2717             : 
    2718           0 :   fd_pack_addr_use_t * writer_costs = pack->writer_costs;
    2719           0 :   for( ulong i=0UL; i<rebate->writer_cnt; i++ ) {
    2720           0 :     fd_pack_addr_use_t * in_wcost_table = acct_uses_query( writer_costs, rebate->writer_rebates[i].key, NULL );
    2721           0 :     if( FD_UNLIKELY( !in_wcost_table ) ) FD_LOG_ERR(( "Rebate to unknown written account" ));
    2722           0 :     in_wcost_table->total_cost -= rebate->writer_rebates[i].rebate_cus;
    2723             :     /* Important: Even if this is 0, don't delete it from the table so
    2724             :        that the insert order doesn't get messed up. */
    2725           0 :   }
    2726           0 : }
    2727             : 
    2728             : 
    2729             : ulong
    2730             : fd_pack_expire_before( fd_pack_t * pack,
    2731           0 :                        ulong       expire_before ) {
    2732           0 :   expire_before = fd_ulong_max( expire_before, pack->expire_before );
    2733           0 :   ulong deleted_cnt = 0UL;
    2734           0 :   fd_pack_expq_t * prq = pack->expiration_q;
    2735           0 :   while( (expq_cnt( prq )>0UL) & (prq->expires_at<expire_before) ) {
    2736           0 :     fd_pack_ord_txn_t * expired = prq->txn;
    2737             : 
    2738             :     /* fd_pack_delete_transaction also removes it from the heap */
    2739             :     /* All the transactions in the same bundle have the same expiration
    2740             :        time, so this loop will end up deleting them all, even with
    2741             :        delete_full_bundle set to 0. */
    2742           0 :     ulong _delete_cnt = delete_transaction( pack, expired, 0, 1 );
    2743           0 :     deleted_cnt += _delete_cnt;
    2744           0 :     FD_TEST( _delete_cnt );
    2745           0 :   }
    2746             : 
    2747           0 :   pack->expire_before = expire_before;
    2748           0 :   return deleted_cnt;
    2749           0 : }
    2750             : 
    2751             : void
    2752           0 : fd_pack_end_block( fd_pack_t * pack ) {
    2753             :   /* rounded division */
    2754           0 :   ulong pct_cus_per_block = (pack->cumulative_block_cost*100UL + (pack->lim->max_cost_per_block>>1))/pack->lim->max_cost_per_block;
    2755           0 :   fd_histf_sample( pack->pct_cus_per_block,       pct_cus_per_block                                          );
    2756           0 :   fd_histf_sample( pack->net_cus_per_block,       pack->cumulative_block_cost                                );
    2757           0 :   fd_histf_sample( pack->rebated_cus_per_block,   pack->cumulative_rebated_cus                               );
    2758           0 :   fd_histf_sample( pack->scheduled_cus_per_block, pack->cumulative_rebated_cus + pack->cumulative_block_cost );
    2759             : 
    2760           0 :   pack->microblock_cnt              = 0UL;
    2761           0 :   pack->data_bytes_consumed         = 0UL;
    2762           0 :   pack->cumulative_block_cost       = 0UL;
    2763           0 :   pack->cumulative_vote_cost        = 0UL;
    2764           0 :   pack->cumulative_rebated_cus      = 0UL;
    2765           0 :   pack->outstanding_microblock_mask = 0UL;
    2766           0 :   pack->alloc_consumed              = 0UL;
    2767             : 
    2768           0 :   pack->initializer_bundle_state = FD_PACK_IB_STATE_NOT_INITIALIZED;
    2769             : 
    2770           0 :   acct_uses_clear( pack->acct_in_use  );
    2771           0 :   memset( pack->top_writers, 0, sizeof(pack->top_writers) );
    2772             : 
    2773           0 :   if( FD_LIKELY( pack->written_list_cnt<pack->written_list_max-1UL ) ) {
    2774             :     /* The less dangerous way of doing this is to instead record the
    2775             :        keys we inserted and do a query followed by a delete for each
    2776             :        key.  The downside of that is that keys are 32 bytes and a
    2777             :        pointer is only 8 bytes, plus the computational cost for the
    2778             :        query.
    2779             : 
    2780             :        However, if we're careful, we can pull this off.  We require two
    2781             :        things.  First, we started from an empty map and did nothing but
    2782             :        insert and update.  In particular, no deletions.  Second, we have
    2783             :        to be careful to delete in the opposite order that we inserted.
    2784             :        This is essentially like unwinding the inserts we did.  The
    2785             :        common case is that the element after the one we delete will be
    2786             :        empty, so we'll hit that case.  It's possible that there's
    2787             :        another independent probe sequence that will be entirely intact
    2788             :        starting in the element after, but we'll never hit the MAP_MOVE
    2789             :        case. */
    2790           0 :     for( ulong i=0UL; i<pack->written_list_cnt; i++ ) {
    2791           0 :       fd_pack_addr_use_t * writer = pack->written_list[ pack->written_list_cnt - 1UL - i ];
    2792             :       /* build a small max heap with the top writer costs */
    2793           0 :       if( FD_UNLIKELY( !fd_pack_unwritable_contains( &writer->key ) && !FD_PACK_TOP_WRITERS_SORT_BEFORE( pack->top_writers[ FD_PACK_TOP_WRITERS_CNT-1UL ], (*writer) ) ) ) {
    2794           0 :           pack->top_writers[ FD_PACK_TOP_WRITERS_CNT-1UL ] = *writer;
    2795           0 :           fd_pack_writer_cost_sort_insert( pack->top_writers, FD_PACK_TOP_WRITERS_CNT );
    2796           0 :       }
    2797             : 
    2798             :       /* Clearing the cost field here is unnecessary (since it gets
    2799             :          cleared on insert), but makes debugging a bit easier. */
    2800           0 :       writer->total_cost = 0UL;
    2801           0 :       acct_uses_remove( pack->writer_costs, writer );
    2802           0 :     }
    2803           0 :   } else {
    2804           0 :     acct_uses_clear( pack->writer_costs );
    2805           0 :   }
    2806           0 :   pack->written_list_cnt = 0UL;
    2807             : 
    2808             :   /* compressed_slot_number is > FD_PACK_SKIP_CNT, which means +1 is the
    2809             :      max unless it overflows. */
    2810           0 :   pack->compressed_slot_number = fd_ushort_max( (ushort)(pack->compressed_slot_number+1), (ushort)(FD_PACK_SKIP_CNT+1) );
    2811             : 
    2812           0 :   FD_PACK_BITSET_CLEAR( pack->bitset_rw_in_use );
    2813           0 :   FD_PACK_BITSET_CLEAR( pack->bitset_w_in_use  );
    2814             : 
    2815           0 :   for( ulong i=0UL; i<pack->bank_tile_cnt; i++ ) pack->use_by_bank_cnt[i] = 0UL;
    2816             : 
    2817             :   /* If our stake is low and we don't become leader often, end_block
    2818             :      might get called on the order of O(1/hr), which feels too
    2819             :      infrequent to do anything related to metrics.  However, we only
    2820             :      update the histograms when we are leader, so this is actually a
    2821             :      good place to copy them. */
    2822           0 :   FD_MHIST_COPY( PACK, TOTAL_TRANSACTIONS_PER_MICROBLOCK_COUNT, pack->txn_per_microblock  );
    2823           0 :   FD_MHIST_COPY( PACK, VOTES_PER_MICROBLOCK_COUNT,              pack->vote_per_microblock );
    2824             : 
    2825           0 :   FD_MGAUGE_SET( PACK, CUS_CONSUMED_IN_BLOCK, 0UL                           );
    2826           0 :   FD_MHIST_COPY( PACK, CUS_SCHEDULED,         pack->scheduled_cus_per_block );
    2827           0 :   FD_MHIST_COPY( PACK, CUS_REBATED,           pack->rebated_cus_per_block   );
    2828           0 :   FD_MHIST_COPY( PACK, CUS_NET,               pack->net_cus_per_block       );
    2829           0 :   FD_MHIST_COPY( PACK, CUS_PCT,               pack->pct_cus_per_block       );
    2830           0 : }
    2831             : 
    2832             : static void
    2833             : release_tree( treap_t           * treap,
    2834             :               sig2txn_t         * signature_map,
    2835             :               noncemap_t        * noncemap,
    2836           0 :               fd_pack_ord_txn_t * pool ) {
    2837           0 :   treap_fwd_iter_t next;
    2838           0 :   for( treap_fwd_iter_t it=treap_fwd_iter_init( treap, pool ); !treap_fwd_iter_done( it ); it=next ) {
    2839           0 :     next = treap_fwd_iter_next( it, pool );
    2840           0 :     ulong idx = treap_fwd_iter_idx( it );
    2841           0 :     pool[ idx ].root = FD_ORD_TXN_ROOT_FREE;
    2842           0 :     treap_idx_remove       ( treap,         idx, pool );
    2843           0 :     sig2txn_idx_remove_fast( signature_map, idx, pool );
    2844           0 :     trp_pool_idx_release   ( pool,          idx       );
    2845           0 :     if( pool[ idx ].txn->flags & FD_TXN_P_FLAGS_DURABLE_NONCE ) {
    2846           0 :       noncemap_idx_remove_fast( noncemap, idx, pool );
    2847           0 :     }
    2848           0 :   }
    2849           0 : }
    2850             : 
    2851             : void
    2852           0 : fd_pack_clear_all( fd_pack_t * pack ) {
    2853           0 :   pack->pending_txn_cnt        = 0UL;
    2854           0 :   pack->microblock_cnt         = 0UL;
    2855           0 :   pack->cumulative_block_cost  = 0UL;
    2856           0 :   pack->cumulative_vote_cost   = 0UL;
    2857           0 :   pack->cumulative_rebated_cus = 0UL;
    2858           0 :   pack->data_bytes_consumed    = 0UL;
    2859           0 :   pack->alloc_consumed         = 0UL;
    2860             : 
    2861           0 :   pack->pending_smallest->cus         = ULONG_MAX;
    2862           0 :   pack->pending_smallest->bytes       = ULONG_MAX;
    2863           0 :   pack->pending_votes_smallest->cus   = ULONG_MAX;
    2864           0 :   pack->pending_votes_smallest->bytes = ULONG_MAX;
    2865             : 
    2866           0 :   release_tree( pack->pending,         pack->signature_map, pack->noncemap, pack->pool );
    2867           0 :   release_tree( pack->pending_votes,   pack->signature_map, pack->noncemap, pack->pool );
    2868           0 :   release_tree( pack->pending_bundles, pack->signature_map, pack->noncemap, pack->pool );
    2869             : 
    2870           0 :   ulong const pool_max = trp_pool_max( pack->pool );
    2871           0 :   for( ulong i=0UL; i<pool_max; i++ ) {
    2872           0 :     if( FD_UNLIKELY( pack->pool[ i ].root!=FD_ORD_TXN_ROOT_FREE ) ) {
    2873           0 :       fd_pack_ord_txn_t * const del = pack->pool + i;
    2874           0 :       fd_txn_t * txn = TXN( del->txn );
    2875           0 :       fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, del->txn->payload );
    2876           0 :       fd_acct_addr_t const * alt_adj = del->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    2877           0 :       fd_acct_addr_t penalty_acct = *ACCT_IDX_TO_PTR( FD_ORD_TXN_ROOT_PENALTY_ACCT_IDX( del->root ) );
    2878           0 :       fd_pack_penalty_treap_t * penalty_treap = penalty_map_query( pack->penalty_treaps, penalty_acct, NULL );
    2879           0 :       FD_TEST( penalty_treap );
    2880           0 :       release_tree( penalty_treap->penalty_treap, pack->signature_map, pack->noncemap, pack->pool );
    2881           0 :     }
    2882           0 :   }
    2883             : 
    2884           0 :   pack->compressed_slot_number = (ushort)(FD_PACK_SKIP_CNT+1);
    2885             : 
    2886           0 :   expq_remove_all( pack->expiration_q );
    2887             : 
    2888           0 :   acct_uses_clear( pack->acct_in_use  );
    2889           0 :   acct_uses_clear( pack->writer_costs );
    2890             : 
    2891           0 :   penalty_map_clear( pack->penalty_treaps );
    2892             : 
    2893           0 :   FD_PACK_BITSET_CLEAR( pack->bitset_rw_in_use );
    2894           0 :   FD_PACK_BITSET_CLEAR( pack->bitset_w_in_use  );
    2895           0 :   bitset_map_clear( pack->acct_to_bitset );
    2896           0 :   pack->bitset_avail[ 0 ] = FD_PACK_BITSET_SLOWPATH;
    2897           0 :   for( ulong i=0UL; i<FD_PACK_BITSET_MAX; i++ ) pack->bitset_avail[ i+1UL ] = (ushort)i;
    2898           0 :   pack->bitset_avail_cnt = FD_PACK_BITSET_MAX;
    2899             : 
    2900           0 :   for( ulong i=0UL; i<pack->bank_tile_cnt; i++ ) pack->use_by_bank_cnt[i] = 0UL;
    2901           0 : }
    2902             : 
    2903             : 
    2904             : /* If delete_full_bundle is non-zero and the transaction to delete is
    2905             :    part of a bundle, the rest of the bundle it is part of will be
    2906             :    deleted as well.
    2907             :    If move_from_penalty_treap is non-zero and the transaction to delete
    2908             :    is in the pending treap, move the best transaction in any of the
    2909             :    conflicting penalty treaps to the pending treap (if there is one). */
    2910             : static ulong
    2911             : delete_transaction( fd_pack_t         * pack,
    2912             :                     fd_pack_ord_txn_t * containing,
    2913             :                     int                 delete_full_bundle,
    2914           0 :                     int                 move_from_penalty_treap ) {
    2915             : 
    2916           0 :   fd_txn_t * txn = TXN( containing->txn );
    2917           0 :   fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, containing->txn->payload );
    2918           0 :   fd_acct_addr_t const * alt_adj = containing->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    2919             : 
    2920           0 :   treap_t * root = NULL;
    2921           0 :   int root_idx = containing->root;
    2922           0 :   fd_pack_penalty_treap_t * penalty_treap = NULL;
    2923           0 :   switch( root_idx & FD_ORD_TXN_ROOT_TAG_MASK ) {
    2924           0 :     case FD_ORD_TXN_ROOT_FREE:           FD_LOG_CRIT(( "Double free detected" ));
    2925           0 :     case FD_ORD_TXN_ROOT_PENDING:        root = pack->pending;         break;
    2926           0 :     case FD_ORD_TXN_ROOT_PENDING_VOTE:   root = pack->pending_votes;   break;
    2927           0 :     case FD_ORD_TXN_ROOT_PENDING_BUNDLE: root = pack->pending_bundles; break;
    2928           0 :     case FD_ORD_TXN_ROOT_PENALTY( 0 ): {
    2929           0 :       fd_acct_addr_t penalty_acct = *ACCT_IDX_TO_PTR( FD_ORD_TXN_ROOT_PENALTY_ACCT_IDX( root_idx ) );
    2930           0 :       penalty_treap = penalty_map_query( pack->penalty_treaps, penalty_acct, NULL );
    2931           0 :       FD_TEST( penalty_treap );
    2932           0 :       root = penalty_treap->penalty_treap;
    2933           0 :       break;
    2934           0 :     }
    2935           0 :   }
    2936             : 
    2937           0 :   ulong delete_cnt = 0UL;
    2938           0 :   if( FD_UNLIKELY( delete_full_bundle & (root==pack->pending_bundles) ) ) {
    2939             :     /* When we delete, the structure of the treap may move around, but
    2940             :        pointers to inside the pool will remain valid */
    2941           0 :     fd_pack_ord_txn_t * bundle_ptrs[ FD_PACK_MAX_TXN_PER_BUNDLE-1UL ];
    2942           0 :     fd_pack_ord_txn_t * pool       = pack->pool;
    2943           0 :     ulong               cnt        = 0UL;
    2944           0 :     ulong               bundle_idx = RC_TO_REL_BUNDLE_IDX( containing->rewards, containing->compute_est );
    2945             : 
    2946             :     /* Iterate in both directions from the current transaction */
    2947           0 :     for( treap_fwd_iter_t _cur=treap_fwd_iter_next( (treap_fwd_iter_t)treap_idx_fast( containing, pool ), pool );
    2948           0 :         !treap_fwd_iter_done( _cur ); _cur=treap_fwd_iter_next( _cur, pool ) ) {
    2949           0 :       fd_pack_ord_txn_t * cur = treap_fwd_iter_ele( _cur, pool );
    2950           0 :       if( FD_LIKELY( bundle_idx==RC_TO_REL_BUNDLE_IDX( cur->rewards, cur->compute_est ) ) ) {
    2951           0 :         bundle_ptrs[ cnt++ ] = cur;
    2952           0 :       } else {
    2953           0 :         break;
    2954           0 :       }
    2955           0 :       FD_TEST( cnt<FD_PACK_MAX_TXN_PER_BUNDLE );
    2956           0 :     }
    2957             : 
    2958           0 :     for( treap_rev_iter_t _cur=treap_rev_iter_next( (treap_rev_iter_t)treap_idx_fast( containing, pool ), pool );
    2959           0 :         !treap_rev_iter_done( _cur ); _cur=treap_rev_iter_next( _cur, pool ) ) {
    2960           0 :       fd_pack_ord_txn_t * cur = treap_rev_iter_ele( _cur, pool );
    2961           0 :       if( FD_LIKELY( bundle_idx==RC_TO_REL_BUNDLE_IDX( cur->rewards, cur->compute_est ) ) ) {
    2962           0 :         bundle_ptrs[ cnt++ ] = cur;
    2963           0 :       } else {
    2964           0 :         break;
    2965           0 :       }
    2966           0 :       FD_TEST( cnt<FD_PACK_MAX_TXN_PER_BUNDLE );
    2967           0 :     }
    2968             : 
    2969             :     /* Delete them each, setting delete_full_bundle to 0 to avoid
    2970             :        infinite recursion. */
    2971           0 :     for( ulong k=0UL; k<cnt; k++ ) delete_cnt += delete_transaction( pack, bundle_ptrs[ k ], 0, 0 );
    2972           0 :   }
    2973             : 
    2974             : 
    2975           0 :   if( FD_UNLIKELY( move_from_penalty_treap & (root==pack->pending) ) ) {
    2976             : 
    2977           0 :     fd_pack_ord_txn_t       * best         = NULL;
    2978           0 :     fd_pack_penalty_treap_t * best_penalty = NULL;
    2979             : 
    2980           0 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_WRITABLE );
    2981           0 :         iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    2982           0 :       fd_pack_penalty_treap_t * p_trp = penalty_map_query( pack->penalty_treaps, *ACCT_ITER_TO_PTR( iter ), NULL );
    2983           0 :       if( FD_UNLIKELY( p_trp ) ) {
    2984           0 :         fd_pack_ord_txn_t * best_in_trp = treap_rev_iter_ele( treap_rev_iter_init( p_trp->penalty_treap, pack->pool ), pack->pool );
    2985           0 :         if( FD_UNLIKELY( !best || COMPARE_WORSE( best, best_in_trp ) ) ) {
    2986           0 :           best         = best_in_trp;
    2987           0 :           best_penalty = p_trp;
    2988           0 :         }
    2989           0 :       }
    2990           0 :     }
    2991             : 
    2992           0 :     if( FD_LIKELY( best ) ) {
    2993             :       /* move best to the main treap */
    2994           0 :       treap_ele_remove( best_penalty->penalty_treap, best, pack->pool );
    2995           0 :       best->root = FD_ORD_TXN_ROOT_PENDING;
    2996           0 :       treap_ele_insert( pack->pending,               best, pack->pool );
    2997             : 
    2998           0 :       pack->pending_smallest->cus   = fd_ulong_min( pack->pending_smallest->cus,   best->compute_est             );
    2999           0 :       pack->pending_smallest->bytes = fd_ulong_min( pack->pending_smallest->bytes, best->txn_e->txnp->payload_sz );
    3000             : 
    3001           0 :       if( FD_UNLIKELY( !treap_ele_cnt( best_penalty->penalty_treap ) ) ) {
    3002           0 :         treap_delete( treap_leave( best_penalty->penalty_treap ) );
    3003           0 :         penalty_map_remove( pack->penalty_treaps, best_penalty );
    3004           0 :       }
    3005           0 :     }
    3006           0 :   }
    3007             : 
    3008           0 :   for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_ALL );
    3009           0 :       iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    3010           0 :     if( FD_UNLIKELY( fd_pack_unwritable_contains( ACCT_ITER_TO_PTR( iter ) ) ) ) continue;
    3011             : 
    3012           0 :     release_result_t ret = release_bit_reference( pack, ACCT_ITER_TO_PTR( iter ) );
    3013           0 :     FD_PACK_BITSET_CLEARN( pack->bitset_rw_in_use, ret.clear_rw_bit );
    3014           0 :     FD_PACK_BITSET_CLEARN( pack->bitset_w_in_use,  ret.clear_w_bit  );
    3015           0 :   }
    3016             : 
    3017           0 :   if( FD_UNLIKELY( containing->txn->flags & FD_TXN_P_FLAGS_DURABLE_NONCE ) ) {
    3018           0 :     noncemap_ele_remove_fast( pack->noncemap, containing, pack->pool );
    3019           0 :   }
    3020           0 :   expq_remove( pack->expiration_q, containing->expq_idx );
    3021           0 :   containing->root = FD_ORD_TXN_ROOT_FREE;
    3022           0 :   treap_ele_remove( root, containing, pack->pool );
    3023           0 :   sig2txn_ele_remove_fast( pack->signature_map, containing, pack->pool );
    3024           0 :   trp_pool_ele_release( pack->pool, containing );
    3025             : 
    3026           0 :   delete_cnt += 1UL;
    3027           0 :   pack->pending_txn_cnt--;
    3028             : 
    3029           0 :   if( FD_UNLIKELY( penalty_treap && treap_ele_cnt( root )==0UL ) ) {
    3030           0 :     penalty_map_remove( pack->penalty_treaps, penalty_treap );
    3031           0 :   }
    3032             : 
    3033           0 :   return delete_cnt;
    3034           0 : }
    3035             : 
    3036             : ulong
    3037             : fd_pack_delete_transaction( fd_pack_t              * pack,
    3038           0 :                             fd_ed25519_sig_t const * sig0 ) {
    3039           0 :   ulong cnt = 0;
    3040           0 :   ulong next = ULONG_MAX;
    3041           0 :   for( ulong idx = sig2txn_idx_query_const( pack->signature_map, (wrapped_sig_t const *)sig0, ULONG_MAX, pack->pool );
    3042           0 :       idx!=ULONG_MAX; idx=next ) {
    3043             :     /* Iterating while deleting, not just this element, but perhaps the
    3044             :        whole bundle, feels a bit dangerous, but is actually fine because
    3045             :        a bundle can't contain two transactions with the same signature.
    3046             :        That means we know next is not part of the same bundle as idx,
    3047             :        which means that deleting idx will not delete next. */
    3048           0 :     next = sig2txn_idx_next_const( idx, ULONG_MAX, pack->pool );
    3049           0 :     cnt += delete_transaction( pack, pack->pool+idx, 1, 1 );
    3050           0 :   }
    3051             : 
    3052           0 :   return cnt;
    3053           0 : }
    3054             : 
    3055             : 
    3056             : int
    3057             : fd_pack_verify( fd_pack_t * pack,
    3058           0 :                 void      * scratch ) {
    3059             :   /* Invariants:
    3060             :      sig2txn_query has exact same contents as all treaps combined
    3061             :      root matches treap
    3062             :      Keys of acct_to_bitset is exactly union of all accounts in all
    3063             :             transactions in treaps, with ref counted appropriately
    3064             :      bits in bitset_avail is complement of bits allocated in
    3065             :             acct_to_bitset
    3066             :      expires_at consistent between treap, prq
    3067             :      use_by_bank does not contain duplicates
    3068             :      use_by_bank consistent with acct_in_use
    3069             :      elements in pool but not in a treap have root set to free
    3070             :      all penalty treaps have at least one transaction
    3071             :      all elements in penalty treaps are in the one that the root indicates
    3072             :      */
    3073             : 
    3074             :   /* TODO:
    3075             :      bitset_{r}w_in_use = bitset_map_query( everything in acct_in_use that doesn't have FD_PACK_IN_USE_BIT_CLEARED )
    3076             :      bitset_w_in_use & bitset_rw_in_use == bitset_w_in_use
    3077             :      */
    3078           0 : #define VERIFY_TEST( cond, ... ) do {   \
    3079           0 :     if( FD_UNLIKELY( !(cond) ) ) {      \
    3080           0 :       FD_LOG_WARNING(( __VA_ARGS__ ));  \
    3081           0 :       return -(__LINE__);               \
    3082           0 :     }                                   \
    3083           0 :   } while( 0 )
    3084             : 
    3085           0 :   ulong max_acct_in_treap  = pack->pack_depth * FD_TXN_ACCT_ADDR_MAX;
    3086           0 :   int lg_acct_in_trp = fd_ulong_find_msb( fd_ulong_pow2_up( 2UL*max_acct_in_treap  ) );
    3087           0 :   void * _bitset_map_copy = scratch;
    3088           0 :   void * _bitset_map_orig = bitset_map_leave( pack->acct_to_bitset );
    3089           0 :   fd_memcpy( _bitset_map_copy, _bitset_map_orig, bitset_map_footprint( lg_acct_in_trp ) );
    3090             : 
    3091           0 :   fd_pack_bitset_acct_mapping_t * bitset_copy = bitset_map_join( _bitset_map_copy );
    3092             : 
    3093             :   /* Check that each bit is in exactly one place */
    3094           0 :   FD_PACK_BITSET_DECLARE( processed ); FD_PACK_BITSET_CLEAR( processed );
    3095           0 :   FD_PACK_BITSET_DECLARE( bit       ); FD_PACK_BITSET_CLEAR( bit       );
    3096           0 :   FD_PACK_BITSET_DECLARE( full      ); FD_PACK_BITSET_CLEAR( full      );
    3097             : 
    3098           0 :   if( FD_UNLIKELY( pack->bitset_avail[0]!=FD_PACK_BITSET_SLOWPATH ) ) return -1;
    3099           0 :   for( ulong i=1UL; i<=pack->bitset_avail_cnt; i++ ) {
    3100           0 :     FD_PACK_BITSET_CLEAR( bit );
    3101           0 :     FD_PACK_BITSET_SETN( bit, pack->bitset_avail[ i ] );
    3102           0 :     VERIFY_TEST( FD_PACK_BITSET_INTERSECT4_EMPTY( bit, bit, processed, processed ),
    3103           0 :         "bit %hu in avail set twice", pack->bitset_avail[ i ] );
    3104           0 :     FD_PACK_BITSET_OR( processed, bit );
    3105           0 :   }
    3106             : 
    3107           0 :   ulong total_references = 0UL;
    3108           0 :   for( ulong i=0UL; i<bitset_map_slot_cnt( bitset_copy ); i++ ) {
    3109           0 :     if( !bitset_map_key_inval( bitset_copy[ i ].key ) ) {
    3110           0 :       VERIFY_TEST( bitset_copy[ i ].ref_cnt>0UL, "account address in table with 0 ref count" );
    3111             : 
    3112           0 :       total_references += bitset_copy[ i ].ref_cnt;
    3113             : 
    3114           0 :       FD_PACK_BITSET_CLEAR( bit );
    3115           0 :       FD_PACK_BITSET_SETN( bit, bitset_copy[ i ].bit );
    3116           0 :       VERIFY_TEST( FD_PACK_BITSET_INTERSECT4_EMPTY( bit, bit, processed, processed ), "bit %hu used twice", bitset_copy[ i ].bit );
    3117           0 :       FD_PACK_BITSET_OR( processed, bit );
    3118           0 :     }
    3119           0 :   }
    3120           0 :   for( ulong i=0UL; i<FD_PACK_BITSET_MAX; i++ ) {
    3121           0 :     FD_PACK_BITSET_CLEAR( bit );
    3122           0 :     FD_PACK_BITSET_SETN( bit, i );
    3123           0 :     VERIFY_TEST( !FD_PACK_BITSET_INTERSECT4_EMPTY( bit, bit, processed, processed ), "bit %lu missing", i );
    3124           0 :     FD_PACK_BITSET_SETN( full, i );
    3125           0 :   }
    3126             : 
    3127             : 
    3128           0 :   fd_pack_ord_txn_t  * pool = pack->pool;
    3129           0 :   treap_t * treaps[ 3 ] = { pack->pending, pack->pending_votes, pack->pending_bundles };
    3130           0 :   ulong txn_cnt = 0UL;
    3131             : 
    3132           0 :   for( ulong k=0UL; k<3UL+penalty_map_slot_cnt( pack->penalty_treaps ); k++ ) {
    3133           0 :     treap_t * treap = NULL;
    3134             : 
    3135           0 :     if( k<3UL ) treap = treaps[ k ];
    3136           0 :     else if( FD_LIKELY( penalty_map_key_inval( pack->penalty_treaps[ k-3UL ].key ) ) ) continue;
    3137           0 :     else {
    3138           0 :       treap = pack->penalty_treaps[ k-3UL ].penalty_treap;
    3139           0 :       VERIFY_TEST( treap_ele_cnt( treap )>0UL, "empty penalty treap in map" );
    3140           0 :     }
    3141             : 
    3142           0 :     for( treap_rev_iter_t _cur=treap_rev_iter_init( treap, pool ); !treap_rev_iter_done( _cur );
    3143           0 :         _cur=treap_rev_iter_next( _cur, pool ) ) {
    3144           0 :       txn_cnt++;
    3145           0 :       fd_pack_ord_txn_t const * cur = treap_rev_iter_ele_const( _cur, pool );
    3146           0 :       fd_txn_t const * txn = TXN(cur->txn);
    3147           0 :       fd_acct_addr_t const * accts   = fd_txn_get_acct_addrs( txn, cur->txn->payload );
    3148           0 :       fd_acct_addr_t const * alt_adj = cur->txn_e->alt_accts - fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
    3149             : 
    3150           0 :       fd_ed25519_sig_t const * sig0 = fd_txn_get_signatures( txn, cur->txn->payload );
    3151             : 
    3152           0 :       fd_pack_ord_txn_t const * in_tbl = sig2txn_ele_query_const( pack->signature_map, (wrapped_sig_t const *)sig0, NULL, pool );
    3153           0 :       VERIFY_TEST( in_tbl, "signature missing from sig2txn" );
    3154             : 
    3155           0 :       VERIFY_TEST( (ulong)(cur->root & FD_ORD_TXN_ROOT_TAG_MASK)==fd_ulong_min( k, 3UL )+1UL, "treap element had bad root" );
    3156           0 :       if( FD_LIKELY( (cur->root & FD_ORD_TXN_ROOT_TAG_MASK)==FD_ORD_TXN_ROOT_PENALTY(0) ) ) {
    3157           0 :         fd_acct_addr_t const * penalty_acct = ACCT_IDX_TO_PTR( FD_ORD_TXN_ROOT_PENALTY_ACCT_IDX( cur->root ) );
    3158           0 :         VERIFY_TEST( !memcmp( penalty_acct, pack->penalty_treaps[ k-3UL ].key.b, 32UL ), "transaction in wrong penalty treap" );
    3159           0 :       }
    3160           0 :       VERIFY_TEST( cur->expires_at>=pack->expire_before, "treap element expired" );
    3161             : 
    3162           0 :       fd_pack_expq_t const * eq = pack->expiration_q + cur->expq_idx;
    3163           0 :       VERIFY_TEST( eq->txn==cur, "expq inconsistent" );
    3164           0 :       VERIFY_TEST( eq->expires_at==cur->expires_at, "expq expires_at inconsistent" );
    3165             : 
    3166           0 :       FD_PACK_BITSET_DECLARE( complement );
    3167           0 :       FD_PACK_BITSET_COPY( complement, full );
    3168           0 :       for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_WRITABLE );
    3169           0 :           iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    3170           0 :         fd_acct_addr_t acct = *ACCT_ITER_TO_PTR( iter );
    3171             : 
    3172           0 :         fd_pack_bitset_acct_mapping_t * q = bitset_map_query( bitset_copy, acct, NULL );
    3173           0 :         VERIFY_TEST( q, "account in transaction missing from bitset mapping" );
    3174           0 :         VERIFY_TEST( q->ref_cnt>0UL, "account in transaction ref_cnt already 0" );
    3175           0 :         q->ref_cnt--;
    3176           0 :         total_references--;
    3177             : 
    3178           0 :         FD_PACK_BITSET_CLEAR( bit );
    3179           0 :         FD_PACK_BITSET_SETN( bit, q->bit );
    3180           0 :         if( q->bit<FD_PACK_BITSET_MAX ) {
    3181           0 :           VERIFY_TEST( !FD_PACK_BITSET_INTERSECT4_EMPTY( bit, bit, cur->rw_bitset, cur->rw_bitset ), "missing from rw bitset" );
    3182           0 :           VERIFY_TEST( !FD_PACK_BITSET_INTERSECT4_EMPTY( bit, bit, cur->w_bitset,  cur->w_bitset  ), "missing from w bitset" );
    3183           0 :         }
    3184           0 :         FD_PACK_BITSET_CLEARN( complement, q->bit );
    3185           0 :       }
    3186           0 :       VERIFY_TEST( FD_PACK_BITSET_INTERSECT4_EMPTY( complement, complement, cur->w_bitset,  cur->w_bitset ), "extra in w bitset" );
    3187             : 
    3188           0 :       for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_READONLY );
    3189           0 :           iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) {
    3190             : 
    3191           0 :         fd_acct_addr_t acct = *ACCT_ITER_TO_PTR( iter );
    3192           0 :         if( FD_UNLIKELY( fd_pack_unwritable_contains( &acct ) ) ) continue;
    3193           0 :         fd_pack_bitset_acct_mapping_t * q = bitset_map_query( bitset_copy, acct, NULL );
    3194           0 :         VERIFY_TEST( q, "account in transaction missing from bitset mapping" );
    3195           0 :         VERIFY_TEST( q->ref_cnt>0UL, "account in transaction ref_cnt already 0" );
    3196           0 :         q->ref_cnt--;
    3197           0 :         total_references--;
    3198             : 
    3199           0 :         FD_PACK_BITSET_CLEAR( bit );
    3200           0 :         FD_PACK_BITSET_SETN( bit, q->bit );
    3201           0 :         if( q->bit<FD_PACK_BITSET_MAX ) {
    3202           0 :           VERIFY_TEST( !FD_PACK_BITSET_INTERSECT4_EMPTY( bit, bit, cur->rw_bitset, cur->rw_bitset ), "missing from rw bitset" );
    3203           0 :         }
    3204           0 :         FD_PACK_BITSET_CLEARN( complement, q->bit );
    3205           0 :       }
    3206           0 :       VERIFY_TEST( FD_PACK_BITSET_INTERSECT4_EMPTY( complement, complement, cur->rw_bitset,  cur->rw_bitset ), "extra in rw bitset" );
    3207           0 :     }
    3208           0 :   }
    3209             : 
    3210           0 :   bitset_map_leave( bitset_copy );
    3211           0 :   VERIFY_TEST( txn_cnt==pack->pending_txn_cnt, "txn_cnt" );
    3212             : 
    3213           0 :   VERIFY_TEST( total_references==0UL, "extra references in bitset mapping" );
    3214           0 :   ulong sig2txn_key_cnt = 0UL;
    3215           0 :   for( sig2txn_iter_t iter = sig2txn_iter_init( pack->signature_map, pool );
    3216           0 :       !sig2txn_iter_done( iter, pack->signature_map, pool );
    3217           0 :       iter = sig2txn_iter_next( iter, pack->signature_map, pool ) ) {
    3218           0 :     sig2txn_key_cnt++;
    3219           0 :   }
    3220           0 :   VERIFY_TEST( txn_cnt==sig2txn_key_cnt, "extra signatures in sig2txn" );
    3221           0 :   VERIFY_TEST( !sig2txn_verify( pack->signature_map, trp_pool_max( pool ), pool ), "sig2txn corrupt" );
    3222             : 
    3223             :   /* Count noncemap keys */
    3224           0 :   ulong noncemap_key_cnt = 0UL;
    3225           0 :   for( noncemap_iter_t iter = noncemap_iter_init( pack->noncemap, pool );
    3226           0 :       !noncemap_iter_done( iter, pack->noncemap, pool );
    3227           0 :       iter = noncemap_iter_next( iter, pack->noncemap, pool ) ) {
    3228           0 :     noncemap_key_cnt++;
    3229             :     /* Ensure element is in pool */
    3230           0 :     fd_pack_ord_txn_t const * ord = noncemap_iter_ele_const( iter, pack->noncemap, pool );
    3231           0 :     VERIFY_TEST( ord->txn->flags & FD_TXN_P_FLAGS_DURABLE_NONCE, "invalid entry in noncemap" );
    3232             : 
    3233             :     /* Although pack allows multiple transactions with the same
    3234             :        signature in sig2txn (MAP_MULTI==1), the noncemap checks prevent
    3235             :        multiple nonce transactions with the same signature. */
    3236           0 :     wrapped_sig_t sig = FD_LOAD( wrapped_sig_t, fd_txn_get_signatures( TXN( ord->txn ), ord->txn->payload ) );
    3237           0 :     VERIFY_TEST( ord==sig2txn_ele_query_const( pack->signature_map, &sig, NULL, pool ), "noncemap and sig2txn desynced" );
    3238           0 :   }
    3239           0 :   VERIFY_TEST( txn_cnt>=noncemap_key_cnt, "phantom txns in noncemap" );
    3240           0 :   VERIFY_TEST( !noncemap_verify( pack->noncemap, trp_pool_max( pool ), pool ), "noncemap corrupt" );
    3241             : 
    3242           0 :   ulong slots_found = 0UL;
    3243           0 :   ulong const pool_max = trp_pool_max( pool );
    3244           0 :   for( ulong i=0UL; i<pool_max; i++ ) {
    3245           0 :     fd_pack_ord_txn_t * ord = pack->pool + i;
    3246           0 :     if( ord->root!=FD_ORD_TXN_ROOT_FREE ) slots_found++;
    3247           0 :   }
    3248           0 :   VERIFY_TEST( slots_found==txn_cnt, "phantom slots in pool" );
    3249             : 
    3250           0 :   bitset_map_join( _bitset_map_orig );
    3251             : 
    3252           0 :   int lg_uses_tbl_sz = acct_uses_lg_slot_cnt( pack->acct_in_use );
    3253             : 
    3254           0 :   void * _acct_in_use_copy = scratch;
    3255           0 :   void * _acct_in_use_orig = acct_uses_leave( pack->acct_in_use );
    3256           0 :   fd_memcpy( _acct_in_use_copy, _acct_in_use_orig, acct_uses_footprint( lg_uses_tbl_sz ) );
    3257             : 
    3258           0 :   fd_pack_addr_use_t * acct_in_use_copy = acct_uses_join( _acct_in_use_copy );
    3259             : 
    3260           0 :   FD_PACK_BITSET_DECLARE(  w_complement );
    3261           0 :   FD_PACK_BITSET_DECLARE( rw_complement );
    3262           0 :   FD_PACK_BITSET_COPY(  w_complement, full );
    3263           0 :   FD_PACK_BITSET_COPY( rw_complement, full );
    3264             : 
    3265           0 :   FD_PACK_BITSET_DECLARE( rw_bitset );  FD_PACK_BITSET_COPY( rw_bitset, pack->bitset_rw_in_use );
    3266           0 :   FD_PACK_BITSET_DECLARE(  w_bitset );  FD_PACK_BITSET_COPY(  w_bitset, pack->bitset_w_in_use  );
    3267             : 
    3268             : 
    3269           0 :   ulong const EMPTY_MASK = ~(FD_PACK_IN_USE_WRITABLE | FD_PACK_IN_USE_BIT_CLEARED);
    3270             : 
    3271           0 :   for( ulong bank=0UL; bank<pack->bank_tile_cnt; bank++ ) {
    3272             : 
    3273           0 :     fd_pack_addr_use_t const * base = pack->use_by_bank[ bank ];
    3274           0 :     ulong bank_mask = 1UL << bank;
    3275             : 
    3276           0 :     for( ulong i=0UL; i<pack->use_by_bank_cnt[ bank ]; i++ ) {
    3277           0 :       fd_pack_addr_use_t * use = acct_uses_query( acct_in_use_copy, base[i].key, NULL );
    3278           0 :       VERIFY_TEST( use, "acct in use by bank not in acct_in_use, or in uses_by_bank twice" );
    3279             : 
    3280           0 :       VERIFY_TEST( use->in_use_by & bank_mask, "acct in uses_by_bank doesn't have corresponding bit set in acct_in_use, or it was in the list twice" );
    3281             : 
    3282           0 :       fd_pack_bitset_acct_mapping_t * q = bitset_map_query( pack->acct_to_bitset, base[i].key, NULL );
    3283             :       /* The normal case is that the acct->bit mapping is preserved
    3284             :          while in use by other transactions in the pending list.  This
    3285             :          might not always happen though.  It's okay for the mapping to
    3286             :          get deleted while the acct is in use, which is noted with
    3287             :          BIT_CLEARED.  If that is set, the mapping may not exist, or it
    3288             :          may have been re-created, perhaps with a different bit. */
    3289           0 :       if( q==NULL ) VERIFY_TEST( use->in_use_by & FD_PACK_IN_USE_BIT_CLEARED, "acct in use not in acct_to_bitset, but not marked as cleared" );
    3290           0 :       else if( !(use->in_use_by & FD_PACK_IN_USE_BIT_CLEARED) ) {
    3291           0 :         FD_PACK_BITSET_CLEAR( bit );
    3292           0 :         FD_PACK_BITSET_SETN( bit, q->bit );
    3293           0 :         if( q->bit<FD_PACK_BITSET_MAX ) {
    3294           0 :           VERIFY_TEST( !FD_PACK_BITSET_INTERSECT4_EMPTY( bit, bit, rw_bitset, rw_bitset ), "missing from rw bitset" );
    3295           0 :           if( use->in_use_by & FD_PACK_IN_USE_WRITABLE ) {
    3296           0 :             VERIFY_TEST( !FD_PACK_BITSET_INTERSECT4_EMPTY( bit, bit, w_bitset, w_bitset ), "missing from w bitset" );
    3297           0 :             FD_PACK_BITSET_CLEARN( w_complement, q->bit );
    3298           0 :           }
    3299           0 :         }
    3300           0 :         FD_PACK_BITSET_CLEARN( rw_complement, q->bit );
    3301           0 :       }
    3302           0 :       if( use->in_use_by & FD_PACK_IN_USE_WRITABLE ) VERIFY_TEST( (use->in_use_by & EMPTY_MASK)==bank_mask, "writable, but in use by multiple" );
    3303             : 
    3304           0 :       use->in_use_by &= ~bank_mask;
    3305           0 :       if( !(use->in_use_by & EMPTY_MASK) ) acct_uses_remove( acct_in_use_copy, use );
    3306           0 :     }
    3307           0 :   }
    3308           0 :   VERIFY_TEST( acct_uses_key_cnt( acct_in_use_copy )==0UL, "stray uses in acct_in_use" );
    3309           0 :   VERIFY_TEST( FD_PACK_BITSET_INTERSECT4_EMPTY( rw_complement, rw_complement, rw_bitset,  rw_bitset ), "extra in rw bitset" );
    3310           0 :   VERIFY_TEST( FD_PACK_BITSET_INTERSECT4_EMPTY(  w_complement,  w_complement,  w_bitset,   w_bitset ), "extra in w bitset" );
    3311             : 
    3312           0 :   acct_uses_leave( acct_in_use_copy );
    3313             : 
    3314           0 :   acct_uses_join( _acct_in_use_orig );
    3315           0 :   return 0;
    3316           0 : }
    3317             : 
    3318           0 : void * fd_pack_leave ( fd_pack_t * pack ) { FD_COMPILER_MFENCE(); return (void *)pack; }
    3319           0 : void * fd_pack_delete( void      * mem  ) { FD_COMPILER_MFENCE(); return mem;          }

Generated by: LCOV version 1.14