Line data Source code
1 : #ifndef HEADER_fd_src_disco_pack_fd_pack_h
2 : #define HEADER_fd_src_disco_pack_fd_pack_h
3 :
4 : /* fd_pack defines methods that prioritizes Solana transactions,
5 : selecting a subset (potentially all) and ordering them to attempt to
6 : maximize the overall profitability of the validator. */
7 :
8 : #include "../../ballet/fd_ballet_base.h"
9 : #include "../../ballet/txn/fd_txn.h"
10 : #include "../shred/fd_shred_batch.h"
11 : #include "fd_est_tbl.h"
12 : #include "fd_microblock.h"
13 : #include "fd_pack_rebate_sum.h"
14 :
15 153 : #define FD_PACK_ALIGN (128UL)
16 :
17 3481713398 : #define FD_PACK_MAX_BANK_TILES 62UL
18 :
19 27172641 : #define FD_PACK_FEE_PER_SIGNATURE (5000UL) /* In lamports */
20 :
21 : /* Each block is limited to 32k parity shreds. We don't want pack to
22 : produce a block with so many transactions we can't shred it, but the
23 : correspondence between transactions and parity shreds is somewhat
24 : complicated, so we need to use conservative limits. */
25 410 : #define FD_PACK_MAX_DATA_PER_BLOCK (FD_SHRED_BATCH_BLOCK_DATA_SZ_MAX)
26 :
27 : /* Optionally allow up to 1M shreds per block for benchmarking. */
28 0 : #define LARGER_MAX_DATA_PER_BLOCK (32UL*FD_SHRED_BATCH_BLOCK_DATA_SZ_MAX)
29 :
30 : /* ---- End consensus-critical constants */
31 :
32 27201372 : #define FD_TXN_P_FLAGS_IS_SIMPLE_VOTE ( 1U)
33 13587390 : #define FD_TXN_P_FLAGS_BUNDLE ( 2U)
34 13588056 : #define FD_TXN_P_FLAGS_INITIALIZER_BUNDLE ( 4U)
35 126 : #define FD_TXN_P_FLAGS_SANITIZE_SUCCESS ( 8U)
36 276 : #define FD_TXN_P_FLAGS_EXECUTE_SUCCESS (16U)
37 0 : #define FD_TXN_P_FLAGS_FEES_ONLY (32U)
38 27172641 : #define FD_TXN_P_FLAGS_DURABLE_NONCE (64U)
39 :
40 171 : #define FD_TXN_P_FLAGS_RESULT_MASK (0xFF000000U)
41 :
42 : /* A bundle is a sequence of between 1 and FD_PACK_MAX_TXN_PER_BUNDLE
43 : transactions (both inclusive) that executes and commits atomically.
44 : */
45 13998 : #define FD_PACK_MAX_TXN_PER_BUNDLE 5UL
46 :
47 : /* The percentage of the transaction fees that are burned */
48 13586319 : #define FD_PACK_TXN_FEE_BURN_PCT 50UL
49 :
50 : /* The Solana network and Firedancer implementation details impose
51 : several limits on what pack can produce. These limits are grouped in
52 : this one struct fd_pack_limits_t, which is just a convenient way to
53 : pass them around. The limits listed below are arithmetic limits.
54 : The limits imposed by practical constraints are almost certainly
55 : much, much tighter. */
56 : struct fd_pack_limits {
57 : /* max_{cost, vote_cost}_per_block, max_write_cost_per_acct are
58 : consensus-critical limits and must be agreed on cluster-wide. A
59 : block that consumes more than max_cost_per_block cost units
60 : (closely related to, but not identical to CUs) in total is invalid.
61 : Similarly, a block where the sum of the cost of all vote
62 : transactions exceeds max_vote_cost_per_block cost units is invalid.
63 : Similarly, a block in where the sum of the cost of all transactions
64 : that write to a given account exceeds max_write_cost_per_acct is
65 : invalid. */
66 : ulong max_cost_per_block; /* in [0, ULONG_MAX) */
67 : ulong max_vote_cost_per_block; /* in [0, max_cost_per_block] */
68 : ulong max_write_cost_per_acct; /* in [0, max_cost_per_block] */
69 :
70 : /* max_data_bytes_per_block is derived from consensus-critical limits
71 : on the number of shreds in a block, but is not directly enforced.
72 : Separation of concerns means that it's not a good idea for pack to
73 : know exactly how the block will be shredded, but at the same time,
74 : we don't want to end up in a situation where we produced a block
75 : that had too many shreds, because the shred tile's only recourse
76 : would be to kill the block. To address this, pack limits the size
77 : of the data it puts into the block to a limit that we can prove
78 : will never cause the shred tile to produce too many shreds.
79 :
80 : This limit includes transaction and microblock headers for
81 : non-empty microblocks that pack produces. */
82 : ulong max_data_bytes_per_block; /* in [0, ULONG_MAX - 183] */
83 :
84 : /* max_txn_per_microblock and max_microblocks_per_block are
85 : Firedancer-imposed implementation limits to bound the amount of
86 : memory consumption that pack uses. Pack will produce microblocks
87 : with no more than max_txn_per_microblock transactions.
88 : Additionally, once pack produces max_microblocks_per_block
89 : non-empty microblocks in a block, all subsequent attempts to
90 : schedule a microblock will return an empty microblock until
91 : fd_pack_end_block is called. */
92 : ulong max_txn_per_microblock; /* in [0, 16777216] */
93 : ulong max_microblocks_per_block; /* in [0, 1e12) */
94 :
95 : };
96 : typedef struct fd_pack_limits fd_pack_limits_t;
97 :
98 : /* fd_pack_addr_use_t: Used for three distinct purposes:
99 : - to record that an address is in use and can't be used again until
100 : certain microblocks finish execution
101 : - to keep track of the cost of all transactions that write to the
102 : specified account.
103 : - to keep track of the write cost for accounts referenced by
104 : transactions in a bundle and which transactions use which
105 : accounts.
106 : Making these separate structs might make it more clear, but then
107 : they'd have identical shape and result in several fd_map_dynamic sets
108 : of functions with identical code. It doesn't seem like the compiler
109 : is very good at merging code like that, so in order to reduce code
110 : bloat, we'll just combine them. */
111 : struct fd_pack_private_addr_use_record {
112 : fd_acct_addr_t key; /* account address */
113 : union {
114 : ulong _;
115 : ulong in_use_by; /* Bitmask indicating which banks */
116 : ulong total_cost; /* In cost units/CUs */
117 : struct { uint carried_cost; /* In cost units */
118 : ushort ref_cnt; /* In transactions */
119 : ushort last_use_in; }; /* In transactions */
120 : };
121 : };
122 : typedef struct fd_pack_private_addr_use_record fd_pack_addr_use_t;
123 :
124 : /* The point of this array it to keep a simple heap of the top 5
125 : writable accounts by cus in a given slot. If we choose to only
126 : updated this heap after execution when CUs are rebated, then we could
127 : get away with making the heap exactly 5 elements long, and we would
128 : end up with the top 5 writers at the end of the slot.
129 :
130 : Unfortunately, this misses accounts for transactions that aren't
131 : rebated (e.g. votes), since we don't rebate 0 cus. Therefore we will
132 : also add writers to this heap at schedule time, using the requested
133 : CU amount. Since requested CUs is an upper bound, if we do this
134 : while also sizing the heap to exactly 5, then a scheduled transaction
135 : might evict a writer that was already rebated and was supposed to end
136 : up in the heap.
137 :
138 : To fix this, we'll increase the size of the heap by the maximum
139 : amount of in-flight, to-be-rebated transactions which currently is
140 : just the number of banks times the number of transactions per
141 : microblock, which is currently limited to 5 in the case of bundles.
142 :
143 : If/when the system is redesigned to include more than 5 transactions
144 : per microblock, then there is a small chance that the final state of
145 : the heap is incorrect. */
146 3481666574 : #define FD_PACK_TOP_WRITERS_HEAP_SZ (5*FD_PACK_MAX_BANK_TILES + 5UL + 1UL)
147 0 : #define FD_PACK_TOP_WRITERS_CNT (5UL)
148 77404355254 : #define FD_PACK_TOP_WRITERS_SORT_BEFORE(writer1,writer2) ( (memcmp( (writer1).key.b, &(uchar[32]){ 0 }, FD_TXN_ACCT_ADDR_SZ ) && (writer1).total_cost>(writer2).total_cost) || !memcmp( (writer2).key.b, &(uchar[32]){ 0 }, FD_TXN_ACCT_ADDR_SZ ))
149 :
150 : #define SORT_NAME fd_pack_writer_cost_sort
151 81895374199 : #define SORT_KEY_T fd_pack_addr_use_t
152 77404355254 : #define SORT_BEFORE(a,b) (FD_PACK_TOP_WRITERS_SORT_BEFORE(a,b))
153 : #include "../../util/tmpl/fd_sort.c"
154 :
155 : /* fd_pack_limit_usage_t is used to store the actual per-slot resource
156 : utilization. Each utilization field has a corresponding limit field
157 : in fd_pack_limits_t which is greater than or equal to the utilization
158 : field. */
159 : struct fd_pack_limits_usage {
160 : ulong block_cost;
161 : ulong vote_cost;
162 :
163 : /* Contains the top 5 writers in the block. If there are less than 5
164 : writeable accounts, unused slots will have their pubkey zeroed out. */
165 : fd_pack_addr_use_t top_write_acct_costs[ FD_PACK_TOP_WRITERS_CNT ];
166 : ulong block_data_bytes;
167 : ulong microblocks;
168 : };
169 :
170 : typedef struct fd_pack_limits_usage fd_pack_limits_usage_t;
171 :
172 : /* fd_pack_smallest: We want to keep track of the smallest transaction
173 : in each treap. That way, if we know the amount of space left in the
174 : block is less than the smallest transaction in the heap, we can just
175 : skip the heap. Since transactions can be deleted, etc. maintaining
176 : this precisely is hard, but we can maintain a conservative value
177 : fairly cheaply. Since the CU limit or the byte limit can be the one
178 : that matters, we keep track of the smallest by both. */
179 : struct fd_pack_smallest {
180 : ulong cus;
181 : ulong bytes;
182 : };
183 : typedef struct fd_pack_smallest fd_pack_smallest_t;
184 :
185 : /* Forward declare opaque handle */
186 : struct fd_pack_private;
187 : typedef struct fd_pack_private fd_pack_t;
188 :
189 : /* fd_pack_{align,footprint} return the required alignment and
190 : footprint in bytes for a region of memory to be used as a pack
191 : object.
192 :
193 : pack_depth sets the maximum number of pending transactions that pack
194 : stores and may eventually schedule. pack_depth must be at least 4.
195 :
196 : If bundle_meta_sz is non-zero, then the bundle-related functions on
197 : this pack object can be used, and it can schedule bundles.
198 : Additionally, if bundle_meta_sz is non-zero, then a region of size
199 : bundle_meta_sz bytes (with no additional alignment) will be reserved
200 : for each bundle.
201 :
202 : Note: if you'd like to use bundles, but don't require metadata for
203 : the bundles, simply use a small positive value (e.g. 1), always pass
204 : NULL in insert_bundle_fini, and never call fd_pack_peek_bundle_meta.
205 :
206 : bank_tile_cnt sets the number of bank tiles to which this pack object
207 : can schedule transactions. bank_tile_cnt must be in [1,
208 : FD_PACK_MAX_BANK_TILES].
209 :
210 : limits sets various limits for the blocks and microblocks that pack
211 : can produce. */
212 :
213 153 : FD_FN_CONST static inline ulong fd_pack_align ( void ) { return FD_PACK_ALIGN; }
214 :
215 : FD_FN_PURE ulong
216 : fd_pack_footprint( ulong pack_depth,
217 : ulong bundle_meta_sz,
218 : ulong bank_tile_cnt,
219 : fd_pack_limits_t const * limits );
220 :
221 :
222 : /* fd_pack_new formats a region of memory to be suitable for use as a
223 : pack object. mem is a non-NULL pointer to a region of memory in the
224 : local address space with the required alignment and footprint.
225 : pack_depth, bundle_meta_sz, bank_tile_cnt, and limits are as above.
226 : rng is a local join to a random number generator used to perturb
227 : estimates.
228 :
229 : Returns `mem` (which will be properly formatted as a pack object) on
230 : success and NULL on failure. Logs details on failure. The caller
231 : will not be joined to the pack object when this function returns. */
232 : void * fd_pack_new( void * mem,
233 : ulong pack_depth,
234 : ulong bundle_meta_sz,
235 : ulong bank_tile_cnt,
236 : fd_pack_limits_t const * limits,
237 : fd_rng_t * rng );
238 :
239 : /* fd_pack_join joins the caller to the pack object. Every successful
240 : join should have a matching leave. Returns mem. */
241 : fd_pack_t * fd_pack_join( void * mem );
242 :
243 :
244 : /* fd_pack_avail_txn_cnt returns the number of transactions that this
245 : pack object has available to schedule but that have not been
246 : scheduled yet. pack must be a valid local join. The return value
247 : will be in [0, pack_depth). */
248 :
249 : /* For performance reasons, implement this here. The offset is STATIC_ASSERTed
250 : in fd_pack.c. */
251 54923 : #define FD_PACK_PENDING_TXN_CNT_OFF 72
252 : FD_FN_PURE static inline ulong
253 54923 : fd_pack_avail_txn_cnt( fd_pack_t const * pack ) {
254 54923 : return *((ulong const *)((uchar const *)pack + FD_PACK_PENDING_TXN_CNT_OFF));
255 54923 : }
256 :
257 : /* fd_pack_current_block_cost returns the number of CUs that have been
258 : scheduled in the current block, net of any rebates. It should be
259 : between 0 and the specified value of max_cost_per_block, but it can
260 : be slightly higher due to temporary cost model nonsense. Due to
261 : rebates, this number may decrease as the block progresses. pack must
262 : be a valid local join. */
263 : FD_FN_PURE ulong fd_pack_current_block_cost( fd_pack_t const * pack );
264 :
265 : /* fd_pack_bank_tile_cnt: returns the value of bank_tile_cnt provided in
266 : pack when the pack object was initialized with fd_pack_new. pack
267 : must be a valid local join. The result will be in [1,
268 : FD_PACK_MAX_BANK_TILES]. */
269 : FD_FN_PURE ulong fd_pack_bank_tile_cnt( fd_pack_t const * pack );
270 :
271 : /* fd_pack_set_block_limits: Updates the limits provided fd_pack_new to
272 : these new values. Any future microblocks produced by this pack
273 : object will not cause a block to have more than
274 : limits->max_microblocks_per_block non-empty microblocks or more than
275 : limits->max_data_bytes_per_block data bytes (counting microblock
276 : headers as before). future microblocks will also exclude those that
277 : cause the total block cost to exceed limits->max_cost_per_block.
278 : Similarly those that cause the total vote-only cost to exceed
279 : limits->max_vote_cost_per_block. Also, those that cause the total
280 : per-account, per block write cost to exceed
281 : limits->max_write_cost_per_acct. Note that
282 : limits->max_txn_per_microblock is ignored. Limits are inclusive, as
283 : per usual (i.e. a block may have exactly max_microblocks_per_block
284 : microblocks, but not more). pack must be a valid local join.
285 :
286 : The typical place to call this is immediately after
287 : fd_pack_end_block; if this is called after some microblocks have been
288 : produced for the current block, and the current block already exceeds
289 : the limits, all the remaining microblocks in the block will be empty,
290 : but the call is valid. */
291 : void fd_pack_set_block_limits( fd_pack_t * pack, fd_pack_limits_t const * limits );
292 :
293 : /* fd_pack_get_block_limits: Copies the currently active pack limits
294 : into opt_limits, if opt_limits is not NULL. Copies the current limit
295 : utilization in opt_limits_usage, if opt_limits_usage is not NULL.
296 :
297 : Limit utilization is updated both when each transactions is scheduled
298 : and when any used resources are rebated. */
299 : void fd_pack_get_block_limits( fd_pack_t * pack, fd_pack_limits_usage_t * opt_limits_usage, fd_pack_limits_t * opt_limits );
300 :
301 : /* Copies the currently smallest pending,
302 : non-conflicting, non-vote transaction into opt_pending_smallest iff
303 : it is not NULL and the smallest pending vote into opt_vote_smallest
304 : iff it is not NULL. These values are updates any time a new
305 : transaction is inserted into the pending treap, or moved from another
306 : treap into the pending treap. */
307 : void fd_pack_get_pending_smallest( fd_pack_t * pack, fd_pack_smallest_t * opt_pending_smallest, fd_pack_smallest_t * opt_votes_smallest );
308 :
309 : /* Return values for fd_pack_insert_txn_fini: Non-negative values
310 : indicate the transaction was accepted and may be returned in a future
311 : microblock. Negative values indicate that the transaction was
312 : rejected and will never be returned in a future microblock.
313 : Transactions can be rejected through no fault of their own, so it
314 : doesn't necessarily imply bad behavior.
315 :
316 : The non-negative (success) codes are essentially a bitflag of three
317 : bits:
318 : * (1) whether the transaction met the criteria for a simple vote or
319 : not,
320 : * (2) whether this transaction replaced a previously accepted, low
321 : priority transaction, rather than being accepted in addition to
322 : all the previously accepted transactions.
323 : * (4) whether this transaction is a durable nonce transaction
324 :
325 : Since pack maintains a heap with a fixed max size of pack_depth,
326 : replacing transaction is necessary whenever the heap is full.
327 : Additionally, only one transaction with a given (nonce account, nonce
328 : authority, recent blockhash) value is allowed in pack's heap at a
329 : time, which means if there's already a lower priority transaction
330 : with the same nonce info, then this transaction will replace it.
331 : When the heap is full, and a nonce transaction is inserted, these
332 : return values don't allow you to disambiguate whether the replaced
333 : transaction had the same nonce info or not.
334 :
335 : Vote and durable nonce transactions are mutually exclusive.
336 :
337 : The negative (failure) codes are a normal enumeration (not a
338 : bitflag).
339 : * PRIORITY: pack's heap was full and the transaction's priority was
340 : lower than the worst currently accepted transaction.
341 : * NONCE_PRIORITY: pack's heap had a transaction with the same
342 : durable nonce info that was higher priority.
343 : * DUPLICATE: the transaction is a duplicate of a currently accepted
344 : transaction.
345 : * UNAFFORDABLE: the fee payer could not afford the transaction fee
346 : (not yet implemented).
347 : * ADDR_LUT: the transaction tried to load an account from an address
348 : lookup table, which is not yet supported.
349 : * EXPIRED: the transaction was already expired upon insertion based
350 : on the provided value of expires_at compared to the last call to
351 : fd_pack_expire_before.
352 : * TOO_LARGE: the transaction requested too many CUs and would never
353 : be scheduled if it had been accepted.
354 : * ACCOUNT_CNT: the transaction tried to load more than 64 account
355 : addresses.
356 : * DUPLICATE_ACCT: the transaction included an account address twice
357 : in its list of account addresses to load.
358 : * ESTIMATION_FAIL: estimation of the transaction's compute cost and
359 : fee failed, typically because the transaction contained a
360 : malformed ComputeBudgetProgram instruction.
361 : * WRITES_SYSVAR: the transaction attempts to write-lock a sysvar.
362 : Write-locking a sysvar can cause heavy contention. Agave
363 : solves this by downgrading these to read locks, but we instead
364 : solve it by refusing to pack such transactions.
365 : * INVALID_NONCE: the transaction looks like a durable nonce
366 : transaction, but the nonce authority did not sign the transaction.
367 : * BUNDLE_BLACKLIST: bundles are enabled and the transaction uses an
368 : account in the bundle blacklist.
369 : * NONCE_CONFLICT: bundle with two transactions that attempt to lock
370 : the exact same durable nonce (nonce account, authority, and block
371 : hash).
372 :
373 : NOTE: The corresponding enum in metrics.xml must be kept in sync
374 : with any changes to these return values. */
375 : #define FD_PACK_INSERT_ACCEPT_NONCE_NONVOTE_REPLACE ( 6)
376 : #define FD_PACK_INSERT_ACCEPT_NONCE_NONVOTE_ADD ( 4)
377 : #define FD_PACK_INSERT_ACCEPT_VOTE_REPLACE ( 3)
378 : #define FD_PACK_INSERT_ACCEPT_NONVOTE_REPLACE ( 2)
379 : #define FD_PACK_INSERT_ACCEPT_VOTE_ADD ( 1)
380 : #define FD_PACK_INSERT_ACCEPT_NONVOTE_ADD ( 0)
381 0 : #define FD_PACK_INSERT_REJECT_PRIORITY ( -1)
382 9 : #define FD_PACK_INSERT_REJECT_NONCE_PRIORITY ( -2)
383 : #define FD_PACK_INSERT_REJECT_DUPLICATE ( -3)
384 0 : #define FD_PACK_INSERT_REJECT_UNAFFORDABLE ( -4)
385 : #define FD_PACK_INSERT_REJECT_ADDR_LUT ( -5)
386 12 : #define FD_PACK_INSERT_REJECT_EXPIRED ( -6)
387 0 : #define FD_PACK_INSERT_REJECT_TOO_LARGE ( -7)
388 3 : #define FD_PACK_INSERT_REJECT_ACCOUNT_CNT ( -8)
389 3 : #define FD_PACK_INSERT_REJECT_DUPLICATE_ACCT ( -9)
390 3 : #define FD_PACK_INSERT_REJECT_ESTIMATION_FAIL (-10)
391 93 : #define FD_PACK_INSERT_REJECT_WRITES_SYSVAR (-11)
392 3 : #define FD_PACK_INSERT_REJECT_INVALID_NONCE (-12)
393 0 : #define FD_PACK_INSERT_REJECT_BUNDLE_BLACKLIST (-13)
394 243 : #define FD_PACK_INSERT_REJECT_NONCE_CONFLICT (-14)
395 :
396 : /* The FD_PACK_INSERT_{ACCEPT, REJECT}_* values defined above are in the
397 : range [-FD_PACK_INSERT_RETVAL_OFF,
398 : -FD_PACK_INSERT_RETVAL_OFF+FD_PACK_INSERT_RETVAL_CNT ) */
399 3192 : #define FD_PACK_INSERT_RETVAL_OFF 14
400 30 : #define FD_PACK_INSERT_RETVAL_CNT 21
401 :
402 : FD_STATIC_ASSERT( FD_PACK_INSERT_REJECT_NONCE_CONFLICT>=-FD_PACK_INSERT_RETVAL_OFF, pack_retval );
403 : FD_STATIC_ASSERT( FD_PACK_INSERT_ACCEPT_NONCE_NONVOTE_REPLACE<FD_PACK_INSERT_RETVAL_CNT-FD_PACK_INSERT_RETVAL_OFF, pack_retval );
404 :
405 : /* fd_pack_insert_txn_{init,fini,cancel} execute the process of
406 : inserting a new transaction into the pool of available transactions
407 : that may be scheduled by the pack object.
408 :
409 : fd_pack_insert_txn_init returns a piece of memory from the txnmem
410 : region where the transaction should be stored. The lifetime of this
411 : memory is managed by fd_pack as explained below.
412 :
413 : Every call to fd_pack_insert_init must be paired with a call to
414 : exactly one of _fini or _cancel. Calling fd_pack_insert_txn_fini
415 : finalizes the transaction insert process and makes the newly-inserted
416 : transaction available for scheduling. Calling
417 : fd_pack_insert_txn_cancel aborts the transaction insertion process.
418 : The txn pointer passed to _fini or _cancel must come from the most
419 : recent call to _init.
420 :
421 : The caller of these methods should not retain any read or write
422 : interest in the transaction after _fini or _cancel have been called.
423 :
424 : expires_at (for _fini only) bounds the lifetime of the inserted
425 : transaction. No particular unit is prescribed, and it need not be
426 : higher than the previous call to txn_fini. If fd_pack_expire_before
427 : has been previously called with a value larger (strictly) than the
428 : provided expires_at, the transaction will be rejected with EXPIRED.
429 : See fd_pack_expire_before for more details.
430 :
431 : pack must be a local join of a pack object. From the caller's
432 : perspective, these functions cannot fail, though pack may reject a
433 : transaction for a variety of reasons. fd_pack_insert_txn_fini
434 : returns one of the FD_PACK_INSERT_ACCEPT_* or FD_PACK_INSERT_REJECT_*
435 : codes explained above.
436 : */
437 : fd_txn_e_t * fd_pack_insert_txn_init ( fd_pack_t * pack );
438 : int fd_pack_insert_txn_fini ( fd_pack_t * pack, fd_txn_e_t * txn, ulong expires_at, ulong * delete_cnt );
439 : void fd_pack_insert_txn_cancel( fd_pack_t * pack, fd_txn_e_t * txn );
440 :
441 : /* fd_pack_insert_bundle_{init,fini,cancel} are parallel to the
442 : similarly named fd_pack_insert_txn functions but can be used to
443 : insert a bundle instead of a transaction.
444 :
445 : fd_pack_insert_bundle_init populates and returns bundle.
446 : Specifically, it populates bundle[0], ... bundle[txn_cnt-1] with
447 : pointers to fd_txn_p_t structs that should receive a new transaction.
448 : The pointers themselves should not be changed which is what the const
449 : indicates, but the contents of the fd_txn_p_t structs must be changed
450 : in order for this to be useful. bundle must be a pointer to the
451 : first element of an array of at least txn_cnt pointers.
452 :
453 : The bundle consists of the transactions in the order they are
454 : provided. I.e. bundle[0] will execute first in the bundle.
455 :
456 : Like with insert_txn, every call to fd_pack_insert_bundle_init must
457 : be paired with a call to exactly one of _fini or _cancel. Calling
458 : fd_pack_insert_bundle_fini finalizes the bundle insertion process and
459 : makes the newly-inserted bundle available for scheduling. Calling
460 : fd_pack_insert_bundle_cancel aborts the transaction insertion
461 : process. There can be at most two outstanding bundles, of which one
462 : should be an initializer bundle. The bundle argument passed to _fini
463 : or _cancel must be the return value of a call to _init with the same
464 : value of txn_cnt. Additionally, it is okay to interleave calls to
465 : the insert_txn family of functions with calls to the insert_bundle
466 : family of functions.
467 :
468 : The caller of these methods should not retain any read or write
469 : interest in the fd_txn_p_t structs that the entries of bundle
470 : point to after _fini or _cancel have been called.
471 :
472 : expires_at has the same meaning as above. Although transactions in
473 : the bundle may have different recent blockhashes, all transactions in
474 : the bundle have the same expires_at value, since if one expires, the
475 : whole bundle becomes invalid.
476 :
477 : If initializer_bundle is non-zero, this bundle will be inserted at
478 : the front of the bundle queue so that it is the next bundle
479 : scheduled. Otherwise, the bundle will be inserted at the back of the
480 : bundle queue, and will be scheduled in FIFO order with the rest of
481 : the bundles. If an initializer bundle is already present in pack's
482 : pending transactions, that bundle will be deleted. Additionally, if
483 : initializer_bundle is non-zero, the transactions in the bundle will
484 : not be checked against the bundle blacklist; otherwise, the check
485 : will be performed as normal. See the section below on initializer
486 : bundles for more details.
487 :
488 : Other than the blacklist check, transactions in a bundle are subject
489 : to the same checks as other transactions. If any transaction in the
490 : bundle fails validation, the whole bundle will be rejected.
491 :
492 : _fini also accepts bundle_meta, an optional opaque pointer to a
493 : region of memory of size bundle_meta_sz (as provided in pack_new).
494 : If bundle_meta is non-NULL, the contents of the memory will be copied
495 : to a metadata region associated with this bundle and can be retrieved
496 : later with fd_pack_peek_bundle_meta. The contents of bundle_meta is
497 : not retrievable if initializer_bundle is non-zero, so you may wish to
498 : just pass NULL in that case. This function does not retain any
499 : interest in the contents of bundle_meta after it returns.
500 :
501 : txn_cnt must be in [1, MAX_TXN_PER_BUNDLE]. A txn_cnt of 1 inserts a
502 : single-transaction bundle which is transaction with extremely high
503 : priority. That said, inserting transactions as bundles instead of
504 : transactions can hurt performance and throughput by introducing
505 : unnecessary stalls.
506 :
507 : fd_pack_insert_bundle_fini returns one of the FD_PACK_INSERT_ACCEPT_*
508 : or FD_PACK_INSERT_REJECT_* codes explained above. If there are
509 : multiple reasons for rejecting a bundle, the which of the reasons it
510 : returns is unspecified. delete_cnt is the number of existing
511 : transactions that were deleted as a side effect of insertion.
512 :
513 : These functions must not be called if the pack object was initialized
514 : with bundle_meta_sz==0. */
515 :
516 : fd_txn_e_t * const * fd_pack_insert_bundle_init ( fd_pack_t * pack, fd_txn_e_t * * bundle, ulong txn_cnt );
517 : int fd_pack_insert_bundle_fini ( fd_pack_t * pack, fd_txn_e_t * const * bundle, ulong txn_cnt,
518 : ulong expires_at, int initializer_bundle, void const * bundle_meta, ulong * delete_cnt );
519 : void fd_pack_insert_bundle_cancel( fd_pack_t * pack, fd_txn_e_t * const * bundle, ulong txn_cnt );
520 :
521 :
522 : /* =========== More details about initializer bundles ===============
523 : Initializer bundles are a special type of bundle with special support
524 : from the pack object to facilitate preparing on-chain state for the
525 : execution of bundles by this validator. This design is a bit
526 : complicated, but it eliminates excessive coupling between pack and
527 : block engine details.
528 :
529 : The pack object maintains a small state machine (initializer bundle
530 : abbreviated IB):
531 :
532 : [Not Initialized] ------------------------->|
533 : ^ | Schedule an
534 : | End Rebate shows | IB
535 : | block IB failed |
536 : |<----------[Failed]--------------| v
537 : | --===[Pending]
538 : |<------------------------------/ ^ |
539 : | End block /---| |
540 : | | | Rebate shows
541 : | Schedule | | IB succeeded
542 : | another IB | |
543 : | End block | V
544 : -----------------------------------===[Ready]
545 :
546 :
547 : When attempting to schedule a bundle the pack object checks the
548 : state, and employs the following rules:
549 : * [Not Initialized]: If the top bundle is an IB, schedule it,
550 : removing it like normal, then transition to [Pending]. Otherwise,
551 : do not schedule a bundle.
552 : * [Pending]: Do not schedule a bundle.
553 : * [Failed]: Do not schedule a bundle
554 : * [Ready]: Attempt to schedule the next bundle. If scheduling an IB,
555 : transition to [Pending].
556 :
557 : As described in the state machine, ending the block (via
558 : fd_pack_end_block) transitions to [Not Initialized], and calls to
559 : fd_pack_rebate_cus control the transition out of [Pending].
560 :
561 : This design supports a typical block engine system where some state
562 : may need to be initialized at the start of the slot and some state
563 : may need to change between runs of transactions (e.g. 5 transactions
564 : from block builder A followed by 5 transactions from block builder
565 : B). This can be done by inserting an initializer bundle whenever the
566 : top non-initializer bundle's metadata state (retrievable with
567 : fd_pack_peek_bundle_meta) doesn't match the current on-chain state.
568 : Since the initializer bundle will execute before the bundle that was
569 : previously the top one, by the time the non-initializer bundle
570 : executes, the on-chain state will be correctly configured. In this
571 : scheme, in the rare case that an initializer bundle was inserted but
572 : never executed, it should be deleted at the end of the slot.
573 :
574 : If at the start of the slot, it is determined that the on-chain state
575 : is in good shape, the state machine can transition directly to
576 : [Ready] by calling fd_pack_set_initializer_bundles_ready.
577 :
578 : Initializer bundles are not exempt from expiration, but it should not
579 : be a problem if they are always inserted with the most recent
580 : blockhash and deleted at the end of the slot.
581 :
582 : Additionally, a bundle marked as an IB is exempted from the bundle
583 : account blacklist checks. For this reason, it's important that IB be
584 : generated by trusted code with minimal or sanitized
585 : attacker-controlled input. */
586 :
587 :
588 : /* fd_pack_peek_bundle_meta returns a constant pointer to the bundle
589 : metadata associated with the bundle currently in line to be scheduled
590 : next, or NULL in any of the following cases:
591 : * There are no bundles
592 : * The bundle currently in line to be scheduled next is an IB
593 : * The bundle state is currently [Pending] or [Failed].
594 :
595 : The lifetime of the returned pointer is until the next pack insert,
596 : schedule, delete, or expire call. The size of the region pointed to
597 : by the returned pointer is bundle_meta_sz. If this bundle was
598 : inserted with bundle_meta==NULL, then the contents of the region
599 : pointed to by the returned pointer are arbitrary, but it will be safe
600 : to read.
601 :
602 : Pack doesn't do anything special to ensure the returned pointer
603 : points to memory with any particular alignment. It will naturally
604 : have an alignment of at least GCD( 64, bundle_meta_sz ). */
605 : void const * fd_pack_peek_bundle_meta( fd_pack_t const * pack );
606 :
607 : /* fd_pack_set_initializer_bundles_ready sets the IB state machine state
608 : (see long initializer bundle comment above) to the [Ready] state.
609 : This function makes it easy to use bundles without initializer
610 : bundles. pack must be a valid local join. */
611 : void fd_pack_set_initializer_bundles_ready( fd_pack_t * pack );
612 :
613 :
614 : /* FD_PACK_SCHEDULE_{VOTE,BUNDLE,TXN} form a set of bitflags used in
615 : fd_pack_schedule_next_microblock below. They control what types of
616 : scheduling are allowed. The names should be self-explanatory. */
617 492921 : #define FD_PACK_SCHEDULE_VOTE 1
618 493020 : #define FD_PACK_SCHEDULE_BUNDLE 2
619 493017 : #define FD_PACK_SCHEDULE_TXN 4
620 :
621 : /* fd_pack_schedule_next_microblock schedules pending transactions.
622 : These transaction either form a microblock, which is a set of
623 : non-conflicting transactions, or a bundle. The semantics of this
624 : function are a bit different depending on which one it picks, but
625 : there are some reasons why they both use this function.
626 :
627 : For both codepaths, pack must be a local join of a pack object.
628 : schedule_flags must be a bitwise combination of the
629 : FD_PACK_SCHEDULE_* values defined above. When the bit is set
630 : corresponding to a transaction type, this function will consider
631 : scheduling transactions of that type. Passing 0 for schedule_flags
632 : is a no-op. The full policy is as follows:
633 : 1. If the VOTE bit is set, attempt to schedule votes. This is the
634 : microblock case.
635 : 2. If the BUNDLE bit is set, and step 1 did not schedule any votes,
636 : attempt to schedule bundles. This is the bundle case.
637 : 3. If the TXN bit is set, and step 2 did not schedule any bundles
638 : for a reason other than account conflicts, attempt to schedule
639 : normal transactions. This is the microblock case.
640 : Note that it is possible to schedule a microblock containing both
641 : votes and normal transactions, but bundles cannot be combined with
642 : either other type. Additionally, if the BUNDLE bit is not set, step
643 : 2 will not schedule any bundles for that reason, which is a reason
644 : other than account conflicts, so that clause will always be
645 : satisfied.
646 :
647 : Microblock case:
648 : Transactions part of the scheduled microblock are copied to out in no
649 : particular order. The cumulative cost of these transactions will not
650 : exceed total_cus, and the number of transactions will not exceed the
651 : value of max_txn_per_microblock given in fd_pack_new.
652 :
653 : The block will not contain more than
654 : vote_fraction*max_txn_per_microblock votes, and votes in total will
655 : not consume more than vote_fraction*total_cus of the microblock.
656 :
657 : Bundle case:
658 : Transactions part of the scheduled bundled are copied in execution
659 : order (i.e. out[0] must be executed first). The number of
660 : transactions will not exceed FD_PACK_MAX_TXN_PER_BUNDLE.
661 : max_txn_per_microblock, total_cus, and vote_fraction are ignored,
662 : though the block-level limits are respected.
663 :
664 : Both cases:
665 : The non_execution_cus and requested_exec_plus_acct_data_cus fields of
666 : each transaction will be populated with the non execution CUs and
667 : requested execution CUs (including cus derived from the requested
668 : loaded accounts data size), respectively. The sum of these two
669 : values is the total cost of the transaction, i.e. what is used for
670 : all limits, including the total_cus value. The lower 3 bits of the
671 : flags field will be populated (simple vote, bundle, initializer
672 : bundle). Inspecting these flags is the proper way to tell which
673 : codepath executed.
674 :
675 : Returns the number of transactions in the scheduled microblock or
676 : bundle. The return value may be 0 if there are no eligible
677 : transactions at the moment. */
678 :
679 : ulong
680 : fd_pack_schedule_next_microblock( fd_pack_t * pack,
681 : ulong total_cus,
682 : float vote_fraction,
683 : ulong bank_tile,
684 : int schedule_flags,
685 : fd_txn_p_t * out );
686 :
687 :
688 : /* fd_pack_rebate_cus adjusts the compute unit accounting for the
689 : specified transactions to take into account the actual consumed CUs
690 : after execution. When a transaction is scheduled by
691 : schedule_next_microblock, pack assumes that it uses all the CUs it
692 : requests for the purposes of several CU limits. If it doesn't use
693 : all the requested CUs, this function "rebates" them to pack so that
694 : they can be consumed by a different transaction in the block.
695 :
696 : pack must be a valid local join of a pack object. rebate must point
697 : to a valid rebate report produced by fd_pack_rebate_sum_t.
698 :
699 : IMPORTANT: CU limits are reset at the end of each block, so this
700 : should not be called for transactions from a prior block.
701 : Specifically, there must not be a call to fd_pack_end_block between
702 : the call to schedule_next_microblock this is paired with and the call
703 : to rebate_cus.
704 :
705 : This function operates independently of microblock_complete. In
706 : general, you probably need to call both. microblock_complete must be
707 : called before scheduling another microblock to that bank tile, while
708 : rebate_cus is optional and has much more relaxed ordering
709 : constraints. The restriction about intervening calls to end_block
710 : and that this must come after schedule_next_microblock are the only
711 : ordering constraints. */
712 : void fd_pack_rebate_cus( fd_pack_t * pack, fd_pack_rebate_t const * rebate );
713 :
714 : /* fd_pack_microblock_complete signals that the bank_tile with index
715 : bank_tile has completed its previously scheduled microblock. This
716 : permits the scheduling of transactions that conflict with the
717 : previously scheduled microblock. It is safe to call this multiple
718 : times after a microblock or even if bank_tile does not have a
719 : previously scheduled; in this case, the function will return 0 and
720 : act as a no-op. Returns 1 if the bank_tile had an outstanding,
721 : previously scheduled microblock to mark as completed. */
722 : int fd_pack_microblock_complete( fd_pack_t * pack, ulong bank_tile );
723 :
724 : /* fd_pack_expire_before deletes all available transactions with
725 : expires_at values strictly less than expire_before. pack must be a
726 : local join of a pack object. Returns the number of transactions
727 : deleted. Subsequent calls to fd_pack_expire_before with the same or
728 : a smaller value are no-ops. */
729 : ulong fd_pack_expire_before( fd_pack_t * pack, ulong expire_before );
730 :
731 : /* fd_pack_delete_txn removes a transaction (identified by its first
732 : signature) from the pool of available transactions. Returns a
733 : nonzero count of the number of transactions deleted, if the
734 : transaction was found (and then removed) and 0 if not. The count
735 : might be >1 if a bundle was caused to be deleted. */
736 : ulong fd_pack_delete_transaction( fd_pack_t * pack, fd_ed25519_sig_t const * sig0 );
737 :
738 : /* fd_pack_end_block resets some state to prepare for the next block.
739 : Specifically, the per-block limits are cleared and transactions in
740 : the microblocks scheduled after the call to this function are allowed
741 : to conflict with transactions in microblocks scheduled before the
742 : call to this function, even within gap microblocks. */
743 : void fd_pack_end_block( fd_pack_t * pack );
744 :
745 :
746 : /* fd_pack_clear_all resets the state associated with this pack object.
747 : All pending transactions are removed from the pool of available
748 : transactions and all limits are reset. */
749 : void fd_pack_clear_all( fd_pack_t * pack );
750 :
751 :
752 : /* fd_pack_metrics_write writes period metric values to the metrics
753 : system. pack must be a valid local join. */
754 : void
755 : fd_pack_metrics_write( fd_pack_t const * pack );
756 :
757 :
758 : /* fd_pack_leave leaves a local join of a pack object. Returns pack. */
759 : void * fd_pack_leave( fd_pack_t * pack );
760 : /* fd_pack_delete unformats a memory region used to store a pack object
761 : and returns ownership of the memory to the caller. Returns mem. */
762 : void * fd_pack_delete( void * mem );
763 :
764 : /* fd_pack_verify (for debugging use primarily) checks to ensure several
765 : invariants are satisfied. scratch must point to the first byte of a
766 : piece of memory meeting the same alignment and footprint constraints
767 : as pack. Returns 0 on success and a negative value on failure
768 : (logging a warning with details). */
769 : int fd_pack_verify( fd_pack_t * pack, void * scratch );
770 :
771 : FD_PROTOTYPES_END
772 :
773 : #endif /* HEADER_fd_src_disco_pack_fd_pack_h */
|