LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_bank.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 10 13 76.9 %
Date: 2026-10-22 18:10:43 Functions: 5 154 3.2 %

          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          12 : #define FD_BANKS_MAGIC     (0XF17EDA2C7EBA2451) /* FIREDANCER BANKS V1 */
      21           0 : #define FD_BANKS_MAX_BANKS (65534UL)
      22         204 : #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          24 : #define FD_BANK_STATE_INACTIVE   (0UL)
     237          12 : #define FD_BANK_STATE_INIT       (1UL)
     238          12 : #define FD_BANK_STATE_REPLAYABLE (2UL)
     239          12 : #define FD_BANK_STATE_FROZEN     (3UL)
     240           0 : #define FD_BANK_STATE_DEAD       (4UL)
     241           0 : #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             :   fd_pubkey_t leader; /* slot leader of this block */
     258             : 
     259             :   fd_txncache_fork_id_t  txncache_fork_id __attribute__((aligned(64UL)));
     260             :   fd_progcache_fork_id_t progcache_fork_id;
     261             :   fd_accdb_fork_id_t     accdb_fork_id;
     262             :   fd_accdb_fork_id_t     parent_accdb_fork_id;
     263             :   ulong                  vote_stakes_fork_id;
     264             :   ushort                 stake_rewards_fork_id;
     265             :   ushort                 stake_delegations_fork_id;
     266             :   ushort                 epoch_credits_fork_id;
     267             :   ushort                 cost_tracker_fork_id;
     268             : 
     269             :   ulong banks_data_offset; /* offset from this fd_bank_t back to fd_banks_t */
     270             : 
     271             :   /* Written by replay only, on its own line */
     272             : 
     273             :   ulong refcnt __attribute__((aligned(64UL))); /* reference count on the bank, see replay for more details */
     274             : 
     275             :   long first_fec_set_received_nanos;
     276             :   long preparation_begin_nanos;
     277             :   long first_transaction_scheduled_nanos;
     278             :   long last_transaction_finished_nanos;
     279             :   long block_completed_nanos;
     280             : 
     281             :   ulong cert_task_cnt; /* footer cert verify tasks out on exec tiles */
     282             :   ulong execution_fees_pre_settle; /* for publish once those are done */
     283             :   ulong priority_fees_pre_settle;
     284             : 
     285             :   /* This field should only be accessed by the replay and executor
     286             :      tiles.  Taken per transaction, so on its own line. */
     287             :   fd_rwlock_t lthash_lock __attribute__((aligned(64UL)));
     288             : 
     289             :   struct {
     290             :     fd_lthash_value_t      lthash;
     291             :     fd_blockhashes_t       block_hash_queue;
     292             :     fd_fee_rate_governor_t fee_rate_governor;
     293             :     ulong                  rbh_lamports_per_sig;
     294             :     ulong                  slot;
     295             :     ulong                  parent_slot;
     296             :     ulong                  capitalization;
     297             :     ulong                  parent_signature_cnt;
     298             :     ulong                  parent_txn_count; /* cumulative txn_count of all ancestors */
     299             :     ulong                  tick_height;
     300             :     ulong                  max_tick_height;
     301             :     ulong                  ticks_per_slot;
     302             :     ulong                  genesis_creation_time;
     303             :     fd_inflation_t         inflation;
     304             :     ulong                  cluster_type;
     305             :     ulong                  total_epoch_stake; /* total staked to active vote accounts */
     306             :     ulong                  total_effective_stake; /* effective stake from stake delegations */
     307             :     ulong                  total_activating_stake;
     308             :     ulong                  total_deactivating_stake;
     309             :     ulong                  warmup_cooldown_rate_epoch; /* epoch when reduce_stake_warmup_cooldown */
     310             :     ulong                  block_height;
     311             :     ulong                  execution_fees;
     312             :     ulong                  priority_fees;
     313             :     ulong                  tips;
     314             :     ulong                  signature_count;
     315             :     fd_hash_t              poh;
     316             :     ulong                  hard_fork_cnt;
     317             :     fd_hard_fork_t         hard_forks[ FD_HARD_FORKS_MAX ]; /* never changes at runtime, required for snapshot creation */
     318             :     fd_hash_t              bank_hash;
     319             :     fd_hash_t              prev_bank_hash;
     320             :     fd_epoch_schedule_t    epoch_schedule;
     321             :     fd_rent_t              rent;
     322             :     fd_sysvar_cache_t      sysvar_cache;
     323             :     fd_features_t          features;
     324             :     ulong                  txn_count;
     325             :     ulong                  nonvote_txn_count;
     326             :     ulong                  failed_txn_count;
     327             :     ulong                  nonvote_failed_txn_count;
     328             :     ulong                  total_compute_units_used;
     329             :     ulong                  shred_cnt;
     330             :     ulong                  epoch;
     331             :     ulong                  identity_vote_idx;
     332             :     fd_slot_params_t       slot_params; /* parameters that need to change with the reduce_slot_time feature gates */
     333             :     fd_slot_params_t       slot_params_default; /* slot params this cluster uses when no reduce_slot_time gate is active */
     334             :     fd_hash_t              block_id;
     335             :     ulong                  alpenglow_migration_slot;
     336             :   } f;
     337             : 
     338             : };
     339             : typedef struct fd_bank fd_bank_t;
     340             : 
     341             : FD_STATIC_ASSERT( offsetof(fd_bank_t, txncache_fork_id)%64UL==0UL, fd_bank_exec_line );
     342             : 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 );
     343             : FD_STATIC_ASSERT( offsetof(fd_bank_t, refcnt)%64UL==0UL, fd_bank_refcnt_line );
     344             : FD_STATIC_ASSERT( offsetof(fd_bank_t, lthash_lock)%64UL==0UL, fd_bank_lthash_lock_line );
     345             : FD_STATIC_ASSERT( offsetof(fd_bank_t, f)-offsetof(fd_bank_t, lthash_lock)>=64UL, fd_bank_lthash_lock_alone );
     346             : 
     347             : struct fd_banks_prune_cancel_info {
     348             :   fd_txncache_fork_id_t  txncache_fork_id;
     349             :   fd_progcache_fork_id_t progcache_fork_id;
     350             :   fd_accdb_fork_id_t     accdb_fork_id;
     351             :   ulong                  slot;
     352             :   ulong                  bank_seq;
     353             :   ulong                  bank_idx;
     354             : };
     355             : typedef struct fd_banks_prune_cancel_info fd_banks_prune_cancel_info_t;
     356             : 
     357             : fd_stake_delegations_t *
     358             : fd_bank_stake_delegations_modify( fd_bank_t * bank );
     359             : 
     360             : /* fd_banks_t is the main struct used to manage the bank state.  It can
     361             :    be used to query/modify/clone/publish the bank state.
     362             : 
     363             :    fd_banks_t contains some metadata to a pool to manage the banks.
     364             :    It also contains pointers to the CoW pools.
     365             : 
     366             :    The data is laid out contiguously in memory starting from fd_banks_t;
     367             :    this can be seen in fd_banks_footprint(). */
     368             : 
     369             : struct fd_banks {
     370             :   ulong magic;                       /* ==FD_BANKS_MAGIC */
     371             :   int   report_runtime_diffs;        /* telemetry: emit the runtime events; report_runtime_diffs flag */
     372             :   ulong max_total_banks;             /* Maximum number of banks */
     373             :   ulong cache_cnt;                   /* In-memory entries of each disk backed per-fork store */
     374             :   ulong max_stake_accounts;          /* Maximum number of stake accounts */
     375             :   ulong max_vote_accounts;           /* Maximum number of vote accounts */
     376             :   ulong root_idx;                    /* root idx */
     377             :   ulong bank_seq;                    /* app-wide bank sequence number counter; starts at 1 (0 is reserved as an invalid bank_seq sentinel) */
     378             :   ulong evict_rr_idx;                /* internal index for round-robin banks eviction */
     379             :   ulong prunable_idx;                /* index of pending prunable bank, ULONG_MAX if none */
     380             : 
     381             :   ulong curr_fork_width;
     382             : 
     383             :   ulong pool_offset; /* offset of pool from banks */
     384             : 
     385             :   ulong cost_tracker_offset; /* offset of cost tracker pool from banks */
     386             : 
     387             :   ulong stake_rewards_offset;
     388             : 
     389             :   ulong dead_banks_offset;
     390             : 
     391             :   ulong epoch_credits_offset;
     392             : 
     393             :   /* The set of epoch leaders for the current and previous epochs is
     394             :      allocated out-of-line and tracked by epoch_leaders_offset.  Only
     395             :      two need to be stored because in the worst case we will have a root
     396             :      that sits behind an epoch boundary, with leaf banks executing into
     397             :      the next epoch.  All banks that execute behind the boundary, will
     398             :      use the previous epoch's leader schedule, and all nodes after the
     399             :      epoch boundary are guaranteed to produce identical leader
     400             :      schedules. */
     401             : 
     402             :   ulong epoch_leaders_offset;
     403             :   ulong epoch_leaders_footprint;
     404             : 
     405             :   ulong stake_delegations_offset;
     406             :   ulong vote_stakes_offset;
     407             : };
     408             : typedef struct fd_banks fd_banks_t;
     409             : 
     410             : /* fd_bank_report_runtime_diffs returns the banks-wide runtime-events
     411             :    flag (set once at replay tile init) for a bank.  Callers use it to
     412             :    gate fd_event_runtime calls. */
     413             : 
     414             : static inline int
     415        4489 : fd_bank_report_runtime_diffs( fd_bank_t const * bank ) {
     416        4489 :   fd_banks_t const * banks_data = fd_type_pun_const( (uchar const *)bank - bank->banks_data_offset );
     417        4489 :   return banks_data->report_runtime_diffs;
     418        4489 : }
     419             : 
     420             : /* Bank accessors and mutators.  Different accessors are emitted for
     421             :    different types depending on if the field has a lock or not. */
     422             : 
     423             : fd_epoch_credits_store_t *
     424             : fd_bank_epoch_credits( fd_bank_t const * bank );
     425             : 
     426             : fd_stake_rewards_t const *
     427             : fd_bank_stake_rewards_query( fd_bank_t * bank );
     428             : 
     429             : fd_stake_rewards_t *
     430             : fd_bank_stake_rewards_modify( fd_bank_t * bank );
     431             : 
     432             : fd_epoch_leaders_t const *
     433             : fd_bank_epoch_leaders_query( fd_bank_t const * bank,
     434             :                              ulong             epoch );
     435             : 
     436             : fd_epoch_leaders_t *
     437             : fd_bank_epoch_leaders_modify( fd_bank_t * bank,
     438             :                               ulong       epoch );
     439             : 
     440             : fd_vote_stakes_t *
     441             : fd_bank_vote_stakes( fd_bank_t const * bank );
     442             : 
     443             : fd_cost_tracker_store_t *
     444             : fd_bank_cost_tracker( fd_bank_t const * bank );
     445             : 
     446             : fd_lthash_value_t const *
     447             : fd_bank_lthash_locking_query( fd_bank_t * bank );
     448             : 
     449             : void
     450             : fd_bank_lthash_end_locking_query( fd_bank_t * bank );
     451             : 
     452             : fd_lthash_value_t *
     453             : fd_bank_lthash_locking_modify( fd_bank_t * bank );
     454             : 
     455             : void
     456             : fd_bank_lthash_end_locking_modify( fd_bank_t * bank );
     457             : 
     458             : /* fd_banks_stake_delegations_root_query() will return a pointer to the
     459             :    full stake delegations for the current root. This function should
     460             :    only be called on boot. */
     461             : 
     462             : fd_stake_delegations_t *
     463             : fd_banks_stake_delegations_root_query( fd_banks_t * banks );
     464             : 
     465             : /* fd_banks_pool_used_cnt returns the number of bank pool elements
     466             :    currently in use. */
     467             : 
     468             : ulong
     469             : fd_banks_pool_used_cnt( fd_banks_t * banks );
     470             : 
     471             : /* fd_banks_pool_max_cnt returns the max number of bank pool elements. */
     472             : 
     473             : ulong
     474             : fd_banks_pool_max_cnt( fd_banks_t * banks );
     475             : 
     476             : /* fd_banks_stake_delegations_evict_bank_fork evicts the stake
     477             :    delegations fork for the given bank.  This is used to clean up
     478             :    resources during teardown. */
     479             : 
     480             : void
     481             : fd_banks_stake_delegations_evict_bank_fork( fd_banks_t * banks,
     482             :                                             fd_bank_t *  bank );
     483             : 
     484             : /* fd_banks_root() returns a pointer to the root bank respectively. */
     485             : 
     486             : fd_bank_t *
     487             : fd_banks_root( fd_banks_t * banks );
     488             : 
     489             : /* fd_banks_align() returns the alignment of fd_banks_t */
     490             : 
     491             : ulong
     492             : fd_banks_align( void );
     493             : 
     494             : /* fd_banks_footprint() returns the footprint of fd_banks_t.  This
     495             :    includes the struct itself but also the footprint for all of the
     496             :    pools.
     497             : 
     498             :    The footprint of fd_banks_t is determined by the total number
     499             :    of banks that the bank manages.  This is an analog for the max number
     500             :    of unrooted blocks the bank can manage at any given time.  Large
     501             :    per-fork structs (cost trackers, epoch credits and t-1 vote stakes)
     502             :    keep cache_cnt entries in memory and spill the rest to disk.  Returns
     503             :    0 if cache_cnt is 0. */
     504             : 
     505             : ulong
     506             : fd_banks_footprint( ulong max_total_banks,
     507             :                     ulong cache_cnt,
     508             :                     ulong max_stake_accounts,
     509             :                     ulong max_vote_accounts );
     510             : 
     511             : /* fd_banks_new() creates a new fd_banks_t struct.  This function
     512             :    lays out the memory for all of the constituent fd_bank_t structs
     513             :    and pools depending on max_total_banks.  stake_delegations_fd identifies the backing file for
     514             :    this instance's stake delegation store. */
     515             : 
     516             : void *
     517             : fd_banks_new( void * mem,
     518             :               int    stake_delegations_fd,
     519             :               ulong  max_total_banks,
     520             :               ulong  cache_cnt,
     521             :               ulong  max_stake_accounts,
     522             :               ulong  max_disk_records,
     523             :               ulong  max_vote_accounts,
     524             :               ulong  bench_max_cost_per_block, /* [development.bench], floors the block cost limit */
     525             :               ulong  seed );
     526             : 
     527             : /* fd_banks_join() joins an fd_banks_t struct.  It takes in a valid
     528             :    banks_data_mem.  Returns a pointer to the joined fd_banks_t struct
     529             :    on success and NULL on failure (logs details). */
     530             : 
     531             : fd_banks_t *
     532             : fd_banks_join( void * banks_data_mem );
     533             : 
     534             : /* fd_banks_init_bank() initializes a new bank in the bank manager.
     535             :    This should only be used during bootup.  The bank is set to the
     536             :    FROZEN state (skipping INIT/REPLAYABLE since no replay is needed for
     537             :    the initial root) and is established as the root bank. */
     538             : 
     539             : fd_bank_t *
     540             : fd_banks_init_bank( fd_banks_t * banks );
     541             : 
     542             : /* fd_banks_get_bank_idx returns a bank for a given bank index. */
     543             : 
     544             : fd_bank_t *
     545             : fd_banks_bank_query( fd_banks_t * banks,
     546             :                      ulong        bank_idx );
     547             : 
     548             : fd_bank_t *
     549             : fd_banks_get_parent( fd_banks_t * banks,
     550             :                      fd_bank_t *  bank );
     551             : 
     552             : /* fd_banks_clone_from_parent() clones a bank from a parent bank.
     553             :    This function links the child bank to its parent bank and copies
     554             :    over the data from the parent bank to the child.  This function
     555             :    assumes that the child and parent banks both have been allocated.
     556             :    The parent bank must be frozen and the child bank must be initialized
     557             :    but not yet used.  It also assumes that the parent bank is not dead.
     558             : 
     559             :    A more detailed note: not all of the data is copied over and this
     560             :    is a shallow clone.  All of the CoW fields are not copied over and
     561             :    will only be done so if the caller explicitly calls
     562             :    fd_bank_{*}_modify().  This naming was chosen to emulate the
     563             :    semantics of the Agave client. */
     564             : 
     565             : fd_bank_t *
     566             : fd_banks_clone_from_parent( fd_banks_t * banks,
     567             :                             ulong        bank_idx );
     568             : 
     569             : /* fd_banks_advance_root() advances the root bank to the bank manager.
     570             :    This should only be used when a bank is no longer needed and has no
     571             :    active refcnts.  This will prune off the bank from the bank manager.
     572             :    It returns the new root bank.  An invariant of this function is that
     573             :    the new root bank should be a child of the current root bank.
     574             : 
     575             :    All banks that are ancestors or siblings of the new root bank will be
     576             :    cancelled and their resources will be released back to the pool. */
     577             : 
     578             : void
     579             : fd_banks_advance_root( fd_banks_t * banks,
     580             :                        ulong        bank_idx );
     581             : 
     582             : /* fd_banks_clear releases all banks back to the pool and resets the
     583             :    banks manager to its post-new state.  Assumes no active references to
     584             :    any bank.  WARNING: collision risk, resets bank_seq to 1 (0 is the
     585             :    reserved invalid sentinel). */
     586             : 
     587             : void
     588             : fd_banks_clear( fd_banks_t * banks );
     589             : 
     590             : /* fd_banks_advance_root_prepare returns the highest block that can be
     591             :    safely advanced between the current root of the fork tree and the
     592             :    target block.  See the note on safe publishing for more details.  In
     593             :    general, a node in the fork tree can be pruned if:
     594             :    (1) the node itself can be pruned, and
     595             :    (2) all subtrees (except for the one on the rooted fork) forking off
     596             :        of the node can be pruned.
     597             : 
     598             :    This function is read-only: it does not modify any bank state.  It
     599             :    walks from the target bank up to the current root to find the direct
     600             :    child of root on the path, then checks whether all sibling subtrees
     601             :    of that child can be pruned.
     602             : 
     603             :    Highest advanceable block is written to the out pointer.  Returns 1
     604             :    if the advanceable block can be advanced beyond the current root.
     605             :    Returns 0 if no such block can be found (e.g. the root still has a
     606             :    non-zero refcnt, or a sibling subtree cannot be pruned).  We will
     607             :    ONLY advance our advanceable_bank_idx to a child of the current root.
     608             :    In order to advance to the target bank,
     609             :    fd_banks_advance_root_prepare() must be called repeatedly. */
     610             : 
     611             : int
     612             : fd_banks_advance_root_prepare( fd_banks_t * banks,
     613             :                                ulong        target_bank_idx,
     614             :                                ulong *      advanceable_bank_idx_out );
     615             : 
     616             : /* fd_banks_mark_bank_dead marks the current bank (and all of its
     617             :    descendants) as dead.  Already-dead banks are not reported again.  If
     618             :    opt_idxs is non-NULL, it is populated with each bank index newly
     619             :    marked dead.  The caller is responsible for ensuring the buffer is
     620             :    large enough to hold the whole subtree.  If opt_idxs_cnt is non-NULL,
     621             :    it is set to the number of banks newly marked dead.  The caller is
     622             :    still responsible for handling the behavior of the dead bank
     623             :    correctly.  After a bank is marked dead, the caller should never
     624             :    increment the reference count on the bank. */
     625             : 
     626             : void
     627             : fd_banks_mark_bank_dead( fd_banks_t * banks,
     628             :                          ulong        bank_idx,
     629             :                          ulong *      opt_idxs,
     630             :                          ulong *      opt_idxs_cnt );
     631             : 
     632             : /* fd_banks_prune_one_bank will try to prune one bank that was
     633             :    marked as dead or prunable.  It will not prune a bank that has a
     634             :    non-zero reference count.  Returns 0 if nothing was pruned, 1 if a
     635             :    bank was pruned but no accdb/txncache cancellation is needed, or 2 if
     636             :    a bank was pruned and cancellation is needed.  Whenever a bank is
     637             :    pruned (returns 1 or 2), cancel->bank_idx is populated if cancel is
     638             :    non-NULL.  The remaining cancel fields are only populated if
     639             :    available. */
     640             : 
     641             : int
     642             : fd_banks_prune_one_bank( fd_banks_t *                   banks,
     643             :                          fd_banks_prune_cancel_info_t * cancel );
     644             : 
     645             : /* fd_banks_mark_bank_frozen marks the current bank as frozen.  This
     646             :    should be done when the bank is no longer being updated: it should be
     647             :    done at the end of a slot.  This also releases the memory for the
     648             :    cost tracker which only has to be persisted from the start of a slot
     649             :    to the end. */
     650             : 
     651             : void
     652             : fd_banks_mark_bank_frozen( fd_bank_t * bank );
     653             : 
     654             : /* fd_banks_new_bank reserves a bank index for a new bank.  New bank
     655             :    indices should always be available.  After this function is called,
     656             :    the bank will be linked to its parent bank, but not yet replayable.
     657             :    After a call to fd_banks_clone_from_parent, the bank will be
     658             :    replayable.  This assumes that there is a parent bank which exists
     659             :    and that there are available bank indices in the bank pool.  It also
     660             :    assumes that the parent bank is not dead, inactive, or prunable. */
     661             : 
     662             : fd_bank_t *
     663             : fd_banks_new_bank( fd_banks_t * banks,
     664             :                    ulong        parent_bank_idx,
     665             :                    long         now,
     666             :                    uchar        is_leader );
     667             : 
     668             : 
     669             : /* fd_banks_get_evictable_bank selects one evictable leaf according to
     670             :    the current fd_banks eviction policy, marks it prunable, records it
     671             :    as pending, and returns its bank index.  Eviction collects all
     672             :    eligible leaves in DFS order (left-child, then siblings) and picks
     673             :    banks->evict_rr_idx % leaf_count, then increments evict_rr_idx.
     674             :    Only a single leaf may be prunable at a time (prunable_idx).
     675             :    The eviction is selected in a round-robin manner to avoid a livelock
     676             :    where we repeatedly evict and replay the same bank, halting progress.
     677             :    The root, leader banks (or banks we were previously leaders for),
     678             :    dead banks, inactive banks, already prunable banks, and
     679             :    protected_bank are not evictable.  Pass NULL for protected_bank if no
     680             :    additional bank needs protection.  Returns ULONG_MAX if there is no
     681             :    evictable bank, or if a prunable bank is already pending pruning.
     682             :    TODO: It is possible that we can still wedge under very adverse
     683             :    conditions even with the round-robin eviction policy.  The starting
     684             :    evict_idx is different for each validator, ensuring that some nodes
     685             :    will still be able to make forward progress. */
     686             : 
     687             : ulong
     688             : fd_banks_get_evictable_bank( fd_banks_t *      banks,
     689             :                              fd_bank_t const * protected_bank );
     690             : 
     691             : /* fd_banks_can_start_bank returns 1 if banks has capacity to start
     692             :    preparing another child bank. */
     693             : 
     694             : int
     695             : fd_banks_can_start_bank( fd_banks_t * banks );
     696             : 
     697             : /* fd_bank_clear_bank() clears the contents of a bank. This should ONLY
     698             :    be used with banks that have no children and should only be used in
     699             :    testing and fuzzing.  WARNING: This should NOT be used in production.
     700             : 
     701             :    This function will memset all non-CoW fields to 0.
     702             : 
     703             :    For all CoW fields, we will reset the indices to its parent. */
     704             : 
     705             : void
     706             : fd_banks_clear_bank( fd_banks_t * banks,
     707             :                      fd_bank_t *  bank );
     708             : 
     709             : FD_PROTOTYPES_END
     710             : 
     711             : #endif /* HEADER_fd_src_flamenco_runtime_fd_bank_h */

Generated by: LCOV version 1.14