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