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