Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_fd_bank_h
2 : #define HEADER_fd_src_flamenco_runtime_fd_bank_h
3 :
4 : #include "../leaders/fd_leaders.h"
5 : #include "../features/fd_features.h"
6 : #include "../stakes/fd_stake_delegations.h"
7 : #include "../stakes/fd_vote_stakes.h"
8 : #include "../stakes/fd_epoch_credits.h"
9 : #include "../fd_rwlock.h"
10 : #include "fd_blockhashes.h"
11 : #include "fd_cost_tracker_store.h"
12 : #include "fd_slot_params.h"
13 : #include "sysvar/fd_sysvar_cache.h"
14 : #include "../../ballet/lthash/fd_lthash.h"
15 : #include "fd_txncache_shmem.h"
16 : #include "../progcache/fd_progcache_base.h"
17 :
18 : FD_PROTOTYPES_BEGIN
19 :
20 153 : #define FD_BANKS_MAGIC (0XF17EDA2C7EBA2451) /* FIREDANCER BANKS V1 */
21 196608 : #define FD_BANKS_MAX_BANKS (65534UL)
22 3075 : #define FD_BANKS_ALIGN (128UL)
23 :
24 : /* A fd_bank_t struct is the representation of the bank state on Solana
25 : for a given block. More specifically, the bank state corresponds to
26 : all information needed during execution that is not stored on-chain,
27 : but is instead cached in a validator's memory. Each of these bank
28 : fields are represented by a member of the fd_bank_t struct.
29 :
30 : Management of fd_bank_t structs must be fork-aware: the state of each
31 : fd_bank_t must be based on the fd_bank_t of its parent block. This
32 : state is managed by the fd_banks_t struct.
33 :
34 : In order to support fork-awareness, there are several key features
35 : that fd_banks_t and fd_bank_t MUST support:
36 : 1. Query for any non-rooted block's bank: create a fast lookup
37 : from bank index to bank
38 : 2. Be able to create a new bank for a given block from the bank of
39 : that block's parent and maintain some tree-like structure to
40 : track the parent-child relationships: copy the contents from a
41 : parent bank into a child bank.
42 : 3. Prune the set of active banks to keep the root updated as the
43 : network progresses: free resources of fd_bank_t structs that
44 : are are not direct descendants of the root bank (remove parents
45 : and any competing lineages). When a bank is marked as dead (ie.
46 : if the block corresponding to the bank is invalid), it also must
47 : be able to be eagerly pruned away.
48 : 4. Each bank will have field(s) that are concurrently read/write
49 : from multiple threads: add read-write locks to the fields that are
50 : concurrently written to.
51 : 5. In practice, a bank state for a given block can be very large and
52 : not all of the fields are written to every block. Therefore, it
53 : can be very expensive to copy the entire bank state for a given
54 : block each time a bank is created. In order to avoid large
55 : memcpys, we can use a CoW mechanism for certain fields.
56 : 6. In a similar vein, some fields are very large and are not written
57 : to very often, and are only read at the epoch boundary. The most
58 : notable example is the stake delegations cache. In order to
59 : handle this, we can use a delta-based approach where each bank
60 : only has a delta of the stake delegations. The root bank will own
61 : the full set of stake delegations. This means that the deltas are
62 : only applied to the root bank as each bank gets rooted. If the
63 : caller needs to access the full set of stake delegations for a
64 : given bank, they can assemble the full set of stake delegations by
65 : applying all of the deltas from the current bank and all of its
66 : ancestors up to the root bank.
67 :
68 : fd_banks_t is represented by a left-child, right-sibling n-ary tree
69 : (inspired by fd_ghost) to keep track of the parent-child fork tree.
70 : The underlying data structure is a pool of fd_bank_t structs. Banks
71 : are then accessed via an index into the bank pool (bank index).
72 :
73 : NOTE: The reason fd_banks_t is keyed by bank index and not by slot is
74 : to handle block equivocation: if there are two different blocks for
75 : the same slot, we need to be able to differentiate and handle both
76 : blocks against different banks. As mentioned above, the bank index is
77 : just an index into the bank pool. The caller is responsible for
78 : establishing a mapping from the bank index (which is managed by
79 : fd_banks_t) and runtime state (e.g. slot number).
80 :
81 : The fields in fd_bank_t can be categorized into two groups:
82 : 1. Simple fields: these are fields which don't need any special
83 : handling and are laid out contiguously in the fd_bank_t struct
84 : at bank->f.<field>.
85 : 2. Complex fields: these are fields which need special handling
86 : (e.g. locking, copy on write semantics, delta-based semantics).
87 : These types are not templatized and are manually defined below.
88 :
89 : Each field that is CoW has its own memory pool. The memory
90 : corresponding to the field is not located in the fd_bank_t struct and
91 : is instead represented by a pool/fork index. When the field is
92 : modified, a new element of the pool is acquired and the data is
93 : copied over from the parent.
94 :
95 : fd_stake_delegations_t stores its full state in fd_banks_t in
96 : out-of-line memory, with each bank carrying the fork index for its
97 : delta.
98 :
99 : The cost tracker is allocated from a pool. The lifetime of a cost
100 : tracker element starts when the bank is linked to a parent with a
101 : call to fd_banks_clone_from_parent() which makes the bank replayable.
102 : The lifetime of a cost tracker element ends when the bank is marked
103 : dead or when the bank is frozen.
104 :
105 : The lthash is a simple field that is laid out contiguously in the
106 : fd_bank_t struct, but is not templatized and it has its own lock.
107 :
108 : So, when a bank is cloned from a parent, the non CoW fields are copied
109 : over and the CoW fields just copy over a pool index. The CoW behavior
110 : is completely abstracted away from the caller as callers have to
111 : query/modify fields using specific APIs.
112 :
113 : The memory for the banks is bounded by the max number of unrooted
114 : blocks at any given time. Large per-fork state (vote stakes t-1
115 : sets, epoch credits, cost trackers) is backed by a file with an
116 : in-memory cache of cache_cnt entries. See fd_banks_footprint() for
117 : more details.
118 :
119 : There are also some important states that a bank can be in:
120 : - Initialized: This bank has been created and linked to a parent bank
121 : index with a call to fd_banks_new_bank(). However, it is not yet
122 : replayable.
123 : - Replayable: This bank has inherited state from its parent and now
124 : transactions can be executed against it. For a bank to become
125 : replayable, it must've been initialized beforehand.
126 : - Dead: This bank has been marked as dead. This means that the block
127 : that this bank is associated with is invalid. A bank can be marked
128 : dead before, during, or after it has finished replaying (i.e. the
129 : bank being marked dead just needs to be initialized). A bank
130 : can still be executing transactions while it is marked dead, but it
131 : shouldn't be dispatched any more work. In other words, a key
132 : invariant is that a bank's reference count should NEVER be increased
133 : after it has been marked dead.
134 : - Frozen: This bank has been marked as frozen and no other tasks
135 : should be dispatched to it. Any bank-specific resources will be
136 : released (e.g. cost tracker element). A bank can be marked frozen
137 : if the bank has finished executing all of its transactions or if the
138 : bank is marked as dead and has no outstanding references. A bank
139 : can only be copied from a parent bank (fd_banks_clone_from_parent)
140 : if the parent bank has been frozen. The program will crash if this
141 : invariant is violated.
142 : - Prunable: This bank has been marked for pruning away due to memory
143 : pressure on the banks. Additional references on the bank should not
144 : be accumulated after the bank has been marked prunable and once
145 : references reach zero, it is safe to evict the bank and free any
146 : related resources. At most one bank may be prunable. It must be a
147 : non-root, non-leader leaf in the initialized, replayable, or frozen
148 : state.
149 :
150 : The usage pattern is as follows:
151 :
152 : To create an initial bank:
153 : fd_bank_t * bank_init = fd_banks_init_bank( banks );
154 :
155 : To create a new bank. This simply provisions the memory for the bank
156 : but it should not be used to execute transactions against.
157 : ulong bank_index = fd_banks_new_bank( banks, parent_bank_index )->idx;
158 :
159 : To clone bank from parent banks. This makes a bank replayable by
160 : copying over the state from the parent bank into the child. It
161 : assumes that the bank index has been previously provisioned by a call
162 : to fd_banks_new_bank and that the parent bank index has been frozen.
163 : fd_bank_t * bank_clone = fd_banks_clone_from_parent( banks, bank_index );
164 :
165 : To ensure that the bank index we want to advance our root to is safe
166 : and that there are no outstanding references to the banks that are
167 : not descendants of the target bank.
168 : fd_banks_advance_root_prepare( banks, target_bank_idx, &advanceable_bank_idx_out );
169 :
170 : To advance the root bank. This assumes that the bank index is "safe"
171 : to advance to. This means that none of the ancestors of the bank
172 : index have a non-zero reference count.
173 : fd_banks_advance_root( banks, bank_index );
174 :
175 : To query some arbitrary bank:
176 : fd_bank_t * bank_query = fd_banks_bank_query( banks, bank_index );
177 :
178 : To access the fields in the bank if they are templatized:
179 :
180 : fd_struct_t const * field = fd_bank_field_query( bank );
181 : OR
182 : fd_struct field = fd_bank_field_get( bank );
183 :
184 : fd_struct_t * field = fd_bank_field_modify( bank );
185 : OR
186 : fd_bank_field_set( bank, value );
187 :
188 : If a bank is marked dead, the caller should call
189 : fd_banks_mark_bank_dead() to mark the bank and all of its descendants
190 : as dead. This does not actually free the underlying resources that
191 : the dead bank has allocated and instead just queues them up for
192 : pruning:
193 : fd_banks_mark_bank_dead( banks, dead_bank_idx, NULL, NULL );
194 :
195 : To actually prune away any dead banks, the caller should call:
196 : fd_banks_prune_one_bank( banks, cancel_info )
197 :
198 : The data used by an fd_bank_t or an fd_banks_t is stored in an
199 : fd_banks_t struct.
200 :
201 : If the fields are not templatized, their accessor and modifier
202 : patterns vary and are documented below.
203 : */
204 :
205 : /* The banks follow a state machine that generally transitions forward:
206 : All banks start off as INACTIVE. Once a bank is provisioned (when
207 : the first FEC is received from the reassembler), it is in the state
208 : INIT; at this point, the bank is not yet replayable but the memory
209 : has been reserved. At this point, it is part of the bank tree and
210 : additional children bank can be assigned to the bank. Once the bank
211 : is replayable, it is moved from INIT to REPLAYABLE and any relevant
212 : state is copied over from the parent bank. We know that the parent
213 : bank is done executing at this point. Transactions can now be
214 : dispatched and scheduled against the bank. If the block for the bank
215 : is done executing then it transitions to the state FROZEN and the
216 : fields in the bank should no longer change.
217 :
218 : A bank can be marked DEAD even before it enters the replayable or
219 : frozen state. A dead bank can only transition to INACTIVE.
220 :
221 : INACTIVE -> INIT -> REPLAYABLE -> FROZEN -> INACTIVE
222 : | \ | \ \ |
223 : | v | v v v
224 : | DEAD | DEAD PRUNABLE
225 : | \ | / |
226 : v v v v v
227 : PRUNABLE INACTIVE INACTIVE
228 : | \
229 : v v
230 : DEAD INACTIVE
231 :
232 : A bank can also transition directly from INIT or REPLAYABLE to
233 : INACTIVE when root advancement prunes an unrooted, unreferenced
234 : sibling subtree. */
235 :
236 80649 : #define FD_BANK_STATE_INACTIVE (0UL)
237 7368 : #define FD_BANK_STATE_INIT (1UL)
238 7326 : #define FD_BANK_STATE_REPLAYABLE (2UL)
239 5802 : #define FD_BANK_STATE_FROZEN (3UL)
240 393432 : #define FD_BANK_STATE_DEAD (4UL)
241 75 : #define FD_BANK_STATE_PRUNABLE (5UL)
242 :
243 :
244 : struct fd_bank {
245 :
246 : /* Fields used for internal pool and bank management */
247 : ulong idx; /* current fork idx of the bank (synchronized with the pool index) */
248 : ulong next; /* reserved for internal use by pool and fd_banks_advance_root */
249 : ulong parent_idx; /* index of the parent in the node pool */
250 : ulong child_idx; /* index of the left-child in the node pool */
251 : ulong sibling_idx; /* index of the right-sibling in the node pool */
252 : ulong dead_prev; /* dead banks list links, only valid while the bank is dead */
253 : ulong dead_next;
254 : ulong state; /* keeps track of the state of the bank */
255 : ulong bank_seq; /* app-wide bank sequence number */
256 : uchar is_leader; /* whether the bank is the leader */
257 :
258 : fd_txncache_fork_id_t txncache_fork_id __attribute__((aligned(64UL)));
259 : fd_progcache_fork_id_t progcache_fork_id;
260 : fd_accdb_fork_id_t accdb_fork_id;
261 : fd_accdb_fork_id_t parent_accdb_fork_id;
262 : ulong vote_stakes_fork_id;
263 : ushort stake_rewards_fork_id;
264 : ushort stake_delegations_fork_id;
265 : ushort epoch_credits_fork_id;
266 : ushort cost_tracker_fork_id;
267 :
268 : ulong banks_data_offset; /* offset from this fd_bank_t back to fd_banks_t */
269 :
270 : /* Written by replay only, on its own line */
271 :
272 : ulong refcnt __attribute__((aligned(64UL))); /* reference count on the bank, see replay for more details */
273 :
274 : long first_fec_set_received_nanos;
275 : long preparation_begin_nanos;
276 : long first_transaction_scheduled_nanos;
277 : long last_transaction_finished_nanos;
278 : long block_completed_nanos;
279 :
280 : ulong cert_task_cnt; /* footer cert verify tasks out on exec tiles */
281 : ulong execution_fees_pre_settle; /* for publish once those are done */
282 : ulong priority_fees_pre_settle;
283 :
284 : /* This field should only be accessed by the replay and executor
285 : tiles. Taken per transaction, so on its own line. */
286 : fd_rwlock_t lthash_lock __attribute__((aligned(64UL)));
287 :
288 : struct {
289 : fd_lthash_value_t lthash;
290 : fd_blockhashes_t block_hash_queue;
291 : fd_fee_rate_governor_t fee_rate_governor;
292 : ulong rbh_lamports_per_sig;
293 : ulong slot;
294 : ulong parent_slot;
295 : ulong capitalization;
296 : ulong parent_signature_cnt;
297 : ulong parent_txn_count; /* cumulative txn_count of all ancestors */
298 : ulong tick_height;
299 : ulong max_tick_height;
300 : ulong ticks_per_slot;
301 : ulong genesis_creation_time;
302 : fd_inflation_t inflation;
303 : ulong cluster_type;
304 : ulong total_epoch_stake; /* total staked to active vote accounts */
305 : ulong total_effective_stake; /* effective stake from stake delegations */
306 : ulong total_activating_stake;
307 : ulong total_deactivating_stake;
308 : ulong warmup_cooldown_rate_epoch; /* epoch when reduce_stake_warmup_cooldown */
309 : ulong block_height;
310 : ulong execution_fees;
311 : ulong priority_fees;
312 : ulong tips;
313 : ulong signature_count;
314 : fd_hash_t poh;
315 : ulong hard_fork_cnt;
316 : fd_hard_fork_t hard_forks[ FD_HARD_FORKS_MAX ]; /* never changes at runtime, required for snapshot creation */
317 : fd_hash_t bank_hash;
318 : fd_hash_t prev_bank_hash;
319 : fd_epoch_schedule_t epoch_schedule;
320 : fd_rent_t rent;
321 : fd_sysvar_cache_t sysvar_cache;
322 : fd_features_t features;
323 : ulong txn_count;
324 : ulong nonvote_txn_count;
325 : ulong failed_txn_count;
326 : ulong nonvote_failed_txn_count;
327 : ulong total_compute_units_used;
328 : ulong shred_cnt;
329 : ulong epoch;
330 : ulong identity_vote_idx;
331 : fd_slot_params_t slot_params; /* parameters that need to change with the reduce_slot_time feature gates */
332 : fd_slot_params_t slot_params_default; /* slot params this cluster uses when no reduce_slot_time gate is active */
333 : fd_hash_t block_id;
334 : ulong alpenglow_migration_slot;
335 : } f;
336 :
337 : };
338 : typedef struct fd_bank fd_bank_t;
339 :
340 : FD_STATIC_ASSERT( offsetof(fd_bank_t, txncache_fork_id)%64UL==0UL, fd_bank_exec_line );
341 : FD_STATIC_ASSERT( offsetof(fd_bank_t, banks_data_offset)+sizeof(ulong)-offsetof(fd_bank_t, txncache_fork_id)<=64UL, fd_bank_exec_line_sz );
342 : FD_STATIC_ASSERT( offsetof(fd_bank_t, refcnt)%64UL==0UL, fd_bank_refcnt_line );
343 : FD_STATIC_ASSERT( offsetof(fd_bank_t, lthash_lock)%64UL==0UL, fd_bank_lthash_lock_line );
344 : FD_STATIC_ASSERT( offsetof(fd_bank_t, f)-offsetof(fd_bank_t, lthash_lock)>=64UL, fd_bank_lthash_lock_alone );
345 :
346 : struct fd_banks_prune_cancel_info {
347 : fd_txncache_fork_id_t txncache_fork_id;
348 : fd_progcache_fork_id_t progcache_fork_id;
349 : fd_accdb_fork_id_t accdb_fork_id;
350 : ulong slot;
351 : ulong bank_seq;
352 : ulong bank_idx;
353 : };
354 : typedef struct fd_banks_prune_cancel_info fd_banks_prune_cancel_info_t;
355 :
356 : fd_stake_delegations_t *
357 : fd_bank_stake_delegations_modify( fd_bank_t * bank );
358 :
359 : /* fd_banks_t is the main struct used to manage the bank state. It can
360 : be used to query/modify/clone/publish the bank state.
361 :
362 : fd_banks_t contains some metadata to a pool to manage the banks.
363 : It also contains pointers to the CoW pools.
364 :
365 : The data is laid out contiguously in memory starting from fd_banks_t;
366 : this can be seen in fd_banks_footprint(). */
367 :
368 : struct fd_banks {
369 : ulong magic; /* ==FD_BANKS_MAGIC */
370 : int report_runtime_diffs; /* telemetry: emit the runtime events; report_runtime_diffs flag */
371 : ulong max_total_banks; /* Maximum number of banks */
372 : ulong cache_cnt; /* In-memory entries of each disk backed per-fork store */
373 : ulong max_stake_accounts; /* Maximum number of stake accounts */
374 : ulong max_vote_accounts; /* Maximum number of vote accounts */
375 : ulong root_idx; /* root idx */
376 : ulong bank_seq; /* app-wide bank sequence number counter; starts at 1 (0 is reserved as an invalid bank_seq sentinel) */
377 : ulong evict_rr_idx; /* internal index for round-robin banks eviction */
378 : ulong prunable_idx; /* index of pending prunable bank, ULONG_MAX if none */
379 :
380 : ulong curr_fork_width;
381 :
382 : ulong pool_offset; /* offset of pool from banks */
383 :
384 : ulong cost_tracker_offset; /* offset of cost tracker pool from banks */
385 :
386 : ulong stake_rewards_offset;
387 :
388 : ulong dead_banks_offset;
389 :
390 : ulong epoch_credits_offset;
391 :
392 : /* The set of epoch leaders for the current and previous epochs is
393 : allocated out-of-line and tracked by epoch_leaders_offset. Only
394 : two need to be stored because in the worst case we will have a root
395 : that sits behind an epoch boundary, with leaf banks executing into
396 : the next epoch. All banks that execute behind the boundary, will
397 : use the previous epoch's leader schedule, and all nodes after the
398 : epoch boundary are guaranteed to produce identical leader
399 : schedules. */
400 :
401 : ulong epoch_leaders_offset;
402 : ulong epoch_leaders_footprint;
403 :
404 : ulong stake_delegations_offset;
405 : ulong vote_stakes_offset;
406 : };
407 : typedef struct fd_banks fd_banks_t;
408 :
409 : /* fd_bank_report_runtime_diffs returns the banks-wide runtime-events
410 : flag (set once at replay tile init) for a bank. Callers use it to
411 : gate fd_event_runtime calls. */
412 :
413 : static inline int
414 21450 : fd_bank_report_runtime_diffs( fd_bank_t const * bank ) {
415 21450 : fd_banks_t const * banks_data = fd_type_pun_const( (uchar const *)bank - bank->banks_data_offset );
416 21450 : return banks_data->report_runtime_diffs;
417 21450 : }
418 :
419 : /* Bank accessors and mutators. Different accessors are emitted for
420 : different types depending on if the field has a lock or not. */
421 :
422 : fd_epoch_credits_store_t *
423 : fd_bank_epoch_credits( fd_bank_t const * bank );
424 :
425 : fd_stake_rewards_t const *
426 : fd_bank_stake_rewards_query( fd_bank_t * bank );
427 :
428 : fd_stake_rewards_t *
429 : fd_bank_stake_rewards_modify( fd_bank_t * bank );
430 :
431 : fd_epoch_leaders_t const *
432 : fd_bank_epoch_leaders_query( fd_bank_t const * bank,
433 : ulong epoch );
434 :
435 : fd_epoch_leaders_t *
436 : fd_bank_epoch_leaders_modify( fd_bank_t * bank,
437 : ulong epoch );
438 :
439 : fd_vote_stakes_t *
440 : fd_bank_vote_stakes( fd_bank_t const * bank );
441 :
442 : fd_cost_tracker_store_t *
443 : fd_bank_cost_tracker( fd_bank_t const * bank );
444 :
445 : fd_lthash_value_t const *
446 : fd_bank_lthash_locking_query( fd_bank_t * bank );
447 :
448 : void
449 : fd_bank_lthash_end_locking_query( fd_bank_t * bank );
450 :
451 : fd_lthash_value_t *
452 : fd_bank_lthash_locking_modify( fd_bank_t * bank );
453 :
454 : void
455 : fd_bank_lthash_end_locking_modify( fd_bank_t * bank );
456 :
457 : /* fd_banks_stake_delegations_root_query() will return a pointer to the
458 : full stake delegations for the current root. This function should
459 : only be called on boot. */
460 :
461 : fd_stake_delegations_t *
462 : fd_banks_stake_delegations_root_query( fd_banks_t * banks );
463 :
464 : /* fd_banks_pool_used_cnt returns the number of bank pool elements
465 : currently in use. */
466 :
467 : ulong
468 : fd_banks_pool_used_cnt( fd_banks_t * banks );
469 :
470 : /* fd_banks_pool_max_cnt returns the max number of bank pool elements. */
471 :
472 : ulong
473 : fd_banks_pool_max_cnt( fd_banks_t * banks );
474 :
475 : /* fd_banks_stake_delegations_evict_bank_fork evicts the stake
476 : delegations fork for the given bank. This is used to clean up
477 : resources during teardown. */
478 :
479 : void
480 : fd_banks_stake_delegations_evict_bank_fork( fd_banks_t * banks,
481 : fd_bank_t * bank );
482 :
483 : /* fd_banks_root() returns a pointer to the root bank respectively. */
484 :
485 : fd_bank_t *
486 : fd_banks_root( fd_banks_t * banks );
487 :
488 : /* fd_banks_align() returns the alignment of fd_banks_t */
489 :
490 : ulong
491 : fd_banks_align( void );
492 :
493 : /* fd_banks_footprint() returns the footprint of fd_banks_t. This
494 : includes the struct itself but also the footprint for all of the
495 : pools.
496 :
497 : The footprint of fd_banks_t is determined by the total number
498 : of banks that the bank manages. This is an analog for the max number
499 : of unrooted blocks the bank can manage at any given time. Large
500 : per-fork structs (cost trackers, epoch credits and t-1 vote stakes)
501 : keep cache_cnt entries in memory and spill the rest to disk. Returns
502 : 0 if cache_cnt is 0. */
503 :
504 : ulong
505 : fd_banks_footprint( ulong max_total_banks,
506 : ulong cache_cnt,
507 : ulong max_stake_accounts,
508 : ulong max_vote_accounts );
509 :
510 : /* fd_banks_new() creates a new fd_banks_t struct. This function
511 : lays out the memory for all of the constituent fd_bank_t structs
512 : and pools depending on max_total_banks. stake_delegations_fd identifies the backing file for
513 : this instance's stake delegation store. */
514 :
515 : void *
516 : fd_banks_new( void * mem,
517 : int stake_delegations_fd,
518 : ulong max_total_banks,
519 : ulong cache_cnt,
520 : ulong max_stake_accounts,
521 : ulong max_disk_records,
522 : ulong max_vote_accounts,
523 : ulong bench_max_cost_per_block, /* [development.bench], floors the block cost limit */
524 : ulong seed );
525 :
526 : /* fd_banks_join() joins an fd_banks_t struct. It takes in a valid
527 : banks_data_mem. Returns a pointer to the joined fd_banks_t struct
528 : on success and NULL on failure (logs details). */
529 :
530 : fd_banks_t *
531 : fd_banks_join( void * banks_data_mem );
532 :
533 : /* fd_banks_init_bank() initializes a new bank in the bank manager.
534 : This should only be used during bootup. The bank is set to the
535 : FROZEN state (skipping INIT/REPLAYABLE since no replay is needed for
536 : the initial root) and is established as the root bank. */
537 :
538 : fd_bank_t *
539 : fd_banks_init_bank( fd_banks_t * banks );
540 :
541 : /* fd_banks_get_bank_idx returns a bank for a given bank index. */
542 :
543 : fd_bank_t *
544 : fd_banks_bank_query( fd_banks_t * banks,
545 : ulong bank_idx );
546 :
547 : fd_bank_t *
548 : fd_banks_get_parent( fd_banks_t * banks,
549 : fd_bank_t * bank );
550 :
551 : /* fd_banks_clone_from_parent() clones a bank from a parent bank.
552 : This function links the child bank to its parent bank and copies
553 : over the data from the parent bank to the child. This function
554 : assumes that the child and parent banks both have been allocated.
555 : The parent bank must be frozen and the child bank must be initialized
556 : but not yet used. It also assumes that the parent bank is not dead.
557 :
558 : A more detailed note: not all of the data is copied over and this
559 : is a shallow clone. All of the CoW fields are not copied over and
560 : will only be done so if the caller explicitly calls
561 : fd_bank_{*}_modify(). This naming was chosen to emulate the
562 : semantics of the Agave client. */
563 :
564 : fd_bank_t *
565 : fd_banks_clone_from_parent( fd_banks_t * banks,
566 : ulong bank_idx );
567 :
568 : /* fd_banks_advance_root() advances the root bank to the bank manager.
569 : This should only be used when a bank is no longer needed and has no
570 : active refcnts. This will prune off the bank from the bank manager.
571 : It returns the new root bank. An invariant of this function is that
572 : the new root bank should be a child of the current root bank.
573 :
574 : All banks that are ancestors or siblings of the new root bank will be
575 : cancelled and their resources will be released back to the pool. */
576 :
577 : void
578 : fd_banks_advance_root( fd_banks_t * banks,
579 : ulong bank_idx );
580 :
581 : /* fd_banks_clear releases all banks back to the pool and resets the
582 : banks manager to its post-new state. Assumes no active references to
583 : any bank. WARNING: collision risk, resets bank_seq to 1 (0 is the
584 : reserved invalid sentinel). */
585 :
586 : void
587 : fd_banks_clear( fd_banks_t * banks );
588 :
589 : /* fd_banks_advance_root_prepare returns the highest block that can be
590 : safely advanced between the current root of the fork tree and the
591 : target block. See the note on safe publishing for more details. In
592 : general, a node in the fork tree can be pruned if:
593 : (1) the node itself can be pruned, and
594 : (2) all subtrees (except for the one on the rooted fork) forking off
595 : of the node can be pruned.
596 :
597 : This function is read-only: it does not modify any bank state. It
598 : walks from the target bank up to the current root to find the direct
599 : child of root on the path, then checks whether all sibling subtrees
600 : of that child can be pruned.
601 :
602 : Highest advanceable block is written to the out pointer. Returns 1
603 : if the advanceable block can be advanced beyond the current root.
604 : Returns 0 if no such block can be found (e.g. the root still has a
605 : non-zero refcnt, or a sibling subtree cannot be pruned). We will
606 : ONLY advance our advanceable_bank_idx to a child of the current root.
607 : In order to advance to the target bank,
608 : fd_banks_advance_root_prepare() must be called repeatedly. */
609 :
610 : int
611 : fd_banks_advance_root_prepare( fd_banks_t * banks,
612 : ulong target_bank_idx,
613 : ulong * advanceable_bank_idx_out );
614 :
615 : /* fd_banks_mark_bank_dead marks the current bank (and all of its
616 : descendants) as dead. Already-dead banks are not reported again. If
617 : opt_idxs is non-NULL, it is populated with each bank index newly
618 : marked dead. The caller is responsible for ensuring the buffer is
619 : large enough to hold the whole subtree. If opt_idxs_cnt is non-NULL,
620 : it is set to the number of banks newly marked dead. The caller is
621 : still responsible for handling the behavior of the dead bank
622 : correctly. After a bank is marked dead, the caller should never
623 : increment the reference count on the bank. */
624 :
625 : void
626 : fd_banks_mark_bank_dead( fd_banks_t * banks,
627 : ulong bank_idx,
628 : ulong * opt_idxs,
629 : ulong * opt_idxs_cnt );
630 :
631 : /* fd_banks_prune_one_bank will try to prune one bank that was
632 : marked as dead or prunable. It will not prune a bank that has a
633 : non-zero reference count. Returns 0 if nothing was pruned, 1 if a
634 : bank was pruned but no accdb/txncache cancellation is needed, or 2 if
635 : a bank was pruned and cancellation is needed. Whenever a bank is
636 : pruned (returns 1 or 2), cancel->bank_idx is populated if cancel is
637 : non-NULL. The remaining cancel fields are only populated if
638 : available. */
639 :
640 : int
641 : fd_banks_prune_one_bank( fd_banks_t * banks,
642 : fd_banks_prune_cancel_info_t * cancel );
643 :
644 : /* fd_banks_mark_bank_frozen marks the current bank as frozen. This
645 : should be done when the bank is no longer being updated: it should be
646 : done at the end of a slot. This also releases the memory for the
647 : cost tracker which only has to be persisted from the start of a slot
648 : to the end. */
649 :
650 : void
651 : fd_banks_mark_bank_frozen( fd_bank_t * bank );
652 :
653 : /* fd_banks_new_bank reserves a bank index for a new bank. New bank
654 : indices should always be available. After this function is called,
655 : the bank will be linked to its parent bank, but not yet replayable.
656 : After a call to fd_banks_clone_from_parent, the bank will be
657 : replayable. This assumes that there is a parent bank which exists
658 : and that there are available bank indices in the bank pool. It also
659 : assumes that the parent bank is not dead, inactive, or prunable. */
660 :
661 : fd_bank_t *
662 : fd_banks_new_bank( fd_banks_t * banks,
663 : ulong parent_bank_idx,
664 : long now,
665 : uchar is_leader );
666 :
667 :
668 : /* fd_banks_get_evictable_bank selects one evictable leaf according to
669 : the current fd_banks eviction policy, marks it prunable, records it
670 : as pending, and returns its bank index. Eviction collects all
671 : eligible leaves in DFS order (left-child, then siblings) and picks
672 : banks->evict_rr_idx % leaf_count, then increments evict_rr_idx.
673 : Only a single leaf may be prunable at a time (prunable_idx).
674 : The eviction is selected in a round-robin manner to avoid a livelock
675 : where we repeatedly evict and replay the same bank, halting progress.
676 : The root, leader banks (or banks we were previously leaders for),
677 : dead banks, inactive banks, already prunable banks, and
678 : protected_bank are not evictable. Pass NULL for protected_bank if no
679 : additional bank needs protection. Returns ULONG_MAX if there is no
680 : evictable bank, or if a prunable bank is already pending pruning.
681 : TODO: It is possible that we can still wedge under very adverse
682 : conditions even with the round-robin eviction policy. The starting
683 : evict_idx is different for each validator, ensuring that some nodes
684 : will still be able to make forward progress. */
685 :
686 : ulong
687 : fd_banks_get_evictable_bank( fd_banks_t * banks,
688 : fd_bank_t const * protected_bank );
689 :
690 : /* fd_banks_can_start_bank returns 1 if banks has capacity to start
691 : preparing another child bank. */
692 :
693 : int
694 : fd_banks_can_start_bank( fd_banks_t * banks );
695 :
696 : /* fd_bank_clear_bank() clears the contents of a bank. This should ONLY
697 : be used with banks that have no children and should only be used in
698 : testing and fuzzing. WARNING: This should NOT be used in production.
699 :
700 : This function will memset all non-CoW fields to 0.
701 :
702 : For all CoW fields, we will reset the indices to its parent. */
703 :
704 : void
705 : fd_banks_clear_bank( fd_banks_t * banks,
706 : fd_bank_t * bank );
707 :
708 : FD_PROTOTYPES_END
709 :
710 : #endif /* HEADER_fd_src_flamenco_runtime_fd_bank_h */
|