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