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