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