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