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