LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_bank.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 131 182 72.0 %
Date: 2025-11-23 04:57:59 Functions: 44 3500 1.3 %

          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 "../types/fd_types.h"
       5             : #include "../leaders/fd_leaders.h"
       6             : #include "../features/fd_features.h"
       7             : #include "../rewards/fd_epoch_rewards.h"
       8             : #include "../stakes/fd_stake_delegations.h"
       9             : #include "../stakes/fd_vote_states.h"
      10             : #include "../fd_rwlock.h"
      11             : #include "fd_blockhashes.h"
      12             : #include "sysvar/fd_sysvar_cache.h"
      13             : #include "../../ballet/lthash/fd_lthash.h"
      14             : #include "fd_txncache_shmem.h"
      15             : 
      16             : FD_PROTOTYPES_BEGIN
      17             : 
      18           6 : #define FD_BANKS_MAGIC (0XF17EDA2C7EBA2450) /* FIREDANCER BANKS V0 */
      19             : 
      20             : /* TODO: Some optimizations, cleanups, future work:
      21             :    1. Simple data types (ulong, int, etc) should be stored as their
      22             :       underlying type instead of a byte array.
      23             :    2. Perhaps make the query/modify scoping more explicit. Right now,
      24             :       the caller is free to use the API wrong if there are no locks.
      25             :       Maybe just expose a different API if there are no locks?
      26             :    3. Rename locks to suffix with _query_locking and _query_locking_end
      27             :    4. Don't templatize out more complex types.
      28             :   */
      29             : 
      30             : /* A fd_bank_t struct is the representation of the bank state on Solana
      31             :    for a given block. More specifically, the bank state corresponds to
      32             :    all information needed during execution that is not stored on-chain,
      33             :    but is instead cached in a validator's memory. Each of these bank
      34             :    fields are repesented by a member of the fd_bank_t struct.
      35             : 
      36             :    Management of fd_bank_t structs must be fork-aware: the state of each
      37             :    fd_bank_t must be based on the fd_bank_t of it's parent block. This
      38             :    state is managed by the fd_banks_t struct.
      39             : 
      40             :    In order to support fork-awareness, there are several key features
      41             :    that fd_banks_t and fd_bank_t MUST support:
      42             :    1. Query for any non-rooted block's bank: create a fast lookup
      43             :       from bank index to bank
      44             :    2. Be able to create a new bank for a given block from the bank of
      45             :       that block's parent and maintain some tree-like structure to
      46             :       track the parent-child relationships: copy the contents from a
      47             :       parent bank into a child bank.
      48             :    3. Prune the set of active banks to keep the root updated as the
      49             :       network progresses: free resources of fd_bank_t structs that
      50             :       are are not direct descendants of the root bank (remove parents
      51             :       and any competing lineages).
      52             :    4. Each bank will have field(s) that are concurrently read/write
      53             :       from multiple threads: add read-write locks to the fields that are
      54             :       concurrently written to.
      55             :    5. In practice, a bank state for a given block can be very large and
      56             :       not all of the fields are written to every block. Therefore, it can
      57             :       be very expensive to copy the entire bank state for a given block
      58             :       each time a bank is created. In order to avoid large memcpys, we
      59             :       can use a CoW mechanism for certain fields.
      60             :    6. In a similar vein, some fields are very large and are not written
      61             :       to very often, and are only read at the epoch boundary. The most
      62             :       notable example is the stake delegations cache. In order to handle
      63             :       this, we can use a delta-based approach where each bank only has
      64             :       a delta of the stake delegations. The root bank will own the full
      65             :       set of stake delegations. This means that the deltas are only
      66             :       applied to the root bank as each bank gets rooted. If the caller
      67             :       needs to access the full set of stake delegations for a given
      68             :       bank, they can assemble the full set of stake delegations by
      69             :       applying all of the deltas from the current bank and all of its
      70             :       ancestors up to the root bank.
      71             : 
      72             :   Each field of a fd_bank_t has a pre-specified set of fields including
      73             :     - name: the name of the field
      74             :     - footprint: the size of the field in bytes
      75             :     - align: the alignment of the field
      76             :     - CoW: whether the field is CoW
      77             :     - has_lock: whether the field has a rw-lock
      78             :     - type: type of the field
      79             : 
      80             :   fd_banks_t is represented by a left-child, right-sibling n-ary tree
      81             :   (inspired by fd_ghost) to keep track of the parent-child fork tree.
      82             :   The underlying data structure is a pool of fd_bank_t structs.  Banks
      83             :   are then accessed via an index into the bank pool (bank index).
      84             : 
      85             :   NOTE: The reason fd_banks_t is keyed by bank index and not by slot is
      86             :   to handle block equivocation: if there are two different blocks for
      87             :   the same slot, we need to be able to differentiate and handle both
      88             :   blocks against different banks.  As mentioned above, the bank index is
      89             :   just an index into the bank pool.  The caller is responsible for
      90             :   establishing a mapping from the bank index (which is managed by
      91             :   fd_banks_t) and runtime state (e.g. slot number).
      92             : 
      93             :   Most of the fields a fd_bank_t are templatized and can support CoW
      94             :   sematics or locking semantics.
      95             : 
      96             :   Each field in fd_bank_t that is not CoW is laid out contiguously in
      97             :   the fd_bank_t struct as simple uchar buffers. This allows for a simple
      98             :   memcpy to clone the bank state from a parent to a child.
      99             : 
     100             :   Each field that is CoW has its own memory pool. The memory
     101             :   corresponding to the field is not located in the fd_bank_t struct and
     102             :   is instead represented by a pool index and a dirty flag. If the field
     103             :   is modified, then the dirty flag is set, and an element of the pool
     104             :   is acquired and the data is copied over from the parent pool idx.
     105             : 
     106             :   Not all fields in the bank are templatized: stake_delegations and
     107             :   the cost_tracker.
     108             : 
     109             :   Currently, there is a delta-based field, fd_stake_delegations_t.
     110             :   Each bank stores a delta-based representation in the form of an
     111             :   aligned uchar buffer.  The full state is stored in fd_banks_t also as
     112             :   a uchar buffer which corresponds to the full state of stake
     113             :   delegations for the current root.  fd_banks_t also reserves another
     114             :   buffer which can store the full state of the stake delegations.
     115             : 
     116             :   The cost tracker is allocated from a pool.  The lifetime of a cost
     117             :   tracker element starts when the bank is linked to a parent with a
     118             :   call to fd_banks_clone_from_parent() which makes the bank replayable.
     119             :   The lifetime of a cost tracker element ends when the bank is marked
     120             :   dead or when the bank is frozen.
     121             : 
     122             :   So, when a bank is cloned from a parent, the non CoW fields are copied
     123             :   over and the CoW fields just copy over a pool index. The CoW behavior
     124             :   is completely abstracted away from the caller as callers have to
     125             :   query/modify fields using specific APIs.
     126             : 
     127             :   The memory for the banks is based off of two bounds:
     128             :   1. the max number of unrooted blocks at any given time. Most fields
     129             :      can be bounded by this value.
     130             :   2. the max number of forks that execute through any 1 block.  We bound
     131             :      fields that are only written to at the epoch boundary by
     132             :      the max fork width that can execute through the boundary instead of
     133             :      by the max number of banks.  See fd_banks_footprint() for more
     134             :      details.
     135             : 
     136             :   There are also some important states that a bank can be in:
     137             :   - Initialized: This bank has been created and linked to a parent bank
     138             :     index with a call to fd_banks_new_bank().  However, it is not yet
     139             :     replayable.
     140             :   - Replayable: This bank has inherited state from its parent and now
     141             :     transactions can be executed against it.  For a bank to become
     142             :     replayable, it must've been initialized beforehand.
     143             :   - Dead: This bank has been marked as dead.  This means that the block
     144             :     that this bank is associated with is invalid.  A bank can be marked
     145             :     dead before, during, or after it has finished replaying.  A bank
     146             :     can still be executing transactions while it is marked dead, but it
     147             :     shouldn't be dispatched any more work.
     148             :   - Frozen: This bank has been marked as frozen and no other tasks
     149             :     should be dispatched to it.  Any bank-specific resources will be
     150             :     released (e.g. cost tracker element).  A bank can be marked frozen
     151             :     in two cases:
     152             :       1. The bank has finished executing all of its transactions
     153             :       2. The bank has been marked dead and there are no outstanding
     154             :          references to the bank.
     155             :     A bank can only be copied from a parent bank
     156             :     (fd_banks_clone_from_parent) if the parent bank has been frozen.
     157             :     The program will crash if this invariant is violated.
     158             : 
     159             :   NOTE: An important invariant is that if a templatized field is CoW,
     160             :   then it must have a rw-lock.
     161             : 
     162             :   NOTE: Another important invariant is that if a templatized field is
     163             :   limiting its fork width, then it must be CoW.
     164             : 
     165             :   The usage pattern is as follows:
     166             : 
     167             :    To create an initial bank:
     168             :    fd_bank_t * bank_init = fd_bank_init_bank( banks );
     169             : 
     170             :    To create a new bank:
     171             :    ulong bank_index = fd_banks_new_bank( banks, parent_bank_index );
     172             : 
     173             :    To clone bank from parent banks:
     174             :    fd_bank_t * bank_clone = fd_banks_clone_from_parent( banks, bank_index, parent_bank_index );
     175             : 
     176             :    To advance the root bank
     177             :    fd_bank_t * root_bank = fd_banks_advance_root( banks, bank_index );
     178             : 
     179             :    To query some arbitrary bank:
     180             :    fd_bank_t * bank_query = fd_banks_bank_query( banks, bank_index );
     181             : 
     182             :   To access fields in the bank if a field does not have a lock:
     183             : 
     184             :   fd_struct_t const * field = fd_bank_field_query( bank );
     185             :   OR
     186             :   fd_struct field = fd_bank_field_get( bank );
     187             : 
     188             :   To modify fields in the bank if a field does not have a lock:
     189             : 
     190             :   fd_struct_t * field = fd_bank_field_modify( bank );
     191             :   OR
     192             :   fd_bank_field_set( bank, value );
     193             : 
     194             :   To access fields in the bank if the field has a lock:
     195             : 
     196             :   fd_struct_t const * field = fd_bank_field_locking_query( bank );
     197             :   ... use field ...
     198             :   fd_bank_field_locking_end_query( bank );
     199             : 
     200             :   To modify fields in the bank if the field has a lock:
     201             : 
     202             :   fd_struct_t * field = fd_bank_field_locking_modify( bank );
     203             :   ... use field ...
     204             :   fd_bank_field_locking_end_locking_modify( bank ); */
     205             : 
     206             : /* Define additional fields to the bank struct here. If trying to add
     207             :    a CoW field to the bank, define a pool for it as done below. */
     208             : 
     209             : #define FD_BANKS_ITER(X)                                                                                                                                                                                                                               \
     210             :   /* type,                             name,                        footprint,                                 align,                                      CoW, limit fork width, has lock */                                                          \
     211          12 :   X(fd_blockhashes_t,                  block_hash_queue,            sizeof(fd_blockhashes_t),                  alignof(fd_blockhashes_t),                  0,   0,                0    )  /* Block hash queue */                                       \
     212         339 :   X(fd_fee_rate_governor_t,            fee_rate_governor,           sizeof(fd_fee_rate_governor_t),            alignof(fd_fee_rate_governor_t),            0,   0,                0    )  /* Fee rate governor */                                      \
     213         339 :   X(ulong,                             rbh_lamports_per_sig,        sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Recent Block Hashes lamports per signature */             \
     214         339 :   X(ulong,                             slot,                        sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Slot */                                                   \
     215         339 :   X(ulong,                             parent_slot,                 sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Parent slot */                                            \
     216         339 :   X(ulong,                             capitalization,              sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Capitalization */                                         \
     217         339 :   X(ulong,                             transaction_count,           sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Transaction count */                                      \
     218         339 :   X(ulong,                             parent_signature_cnt,        sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Parent signature count */                                 \
     219         339 :   X(ulong,                             tick_height,                 sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Tick height */                                            \
     220         339 :   X(ulong,                             max_tick_height,             sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Max tick height */                                        \
     221         339 :   X(ulong,                             hashes_per_tick,             sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Hashes per tick */                                        \
     222         339 :   X(fd_w_u128_t,                       ns_per_slot,                 sizeof(fd_w_u128_t),                       alignof(fd_w_u128_t),                       0,   0,                0    )  /* NS per slot */                                            \
     223         339 :   X(ulong,                             ticks_per_slot,              sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Ticks per slot */                                         \
     224         339 :   X(ulong,                             genesis_creation_time,       sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Genesis creation time */                                  \
     225         339 :   X(double,                            slots_per_year,              sizeof(double),                            alignof(double),                            0,   0,                0    )  /* Slots per year */                                         \
     226         339 :   X(fd_inflation_t,                    inflation,                   sizeof(fd_inflation_t),                    alignof(fd_inflation_t),                    0,   0,                0    )  /* Inflation */                                              \
     227         339 :   X(ulong,                             cluster_type,                sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Cluster type */                                           \
     228         339 :   X(ulong,                             total_epoch_stake,           sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Total epoch stake */                                      \
     229         339 :                                                                                                                                                                                           /* This is only used for the get_epoch_stake syscall. */     \
     230         339 :                                                                                                                                                                                           /* If we are executing in epoch E, this is the total */      \
     231         339 :                                                                                                                                                                                           /* stake at the end of epoch E-1. */                         \
     232         339 :   X(ulong,                             block_height,                sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Block height */                                           \
     233         339 :   X(ulong,                             execution_fees,              sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Execution fees */                                         \
     234         339 :   X(ulong,                             priority_fees,               sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Priority fees */                                          \
     235         339 :   X(ulong,                             tips,                        sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Tips collected */                                         \
     236         339 :   X(ulong,                             signature_count,             sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Signature count */                                        \
     237         339 :   X(fd_hash_t,                         poh,                         sizeof(fd_hash_t),                         alignof(fd_hash_t),                         0,   0,                0    )  /* PoH */                                                    \
     238         339 :   X(fd_sol_sysvar_last_restart_slot_t, last_restart_slot,           sizeof(fd_sol_sysvar_last_restart_slot_t), alignof(fd_sol_sysvar_last_restart_slot_t), 0,   0,                0    )  /* Last restart slot */                                      \
     239         339 :   X(fd_hash_t,                         bank_hash,                   sizeof(fd_hash_t),                         alignof(fd_hash_t),                         0,   0,                0    )  /* Bank hash */                                              \
     240         339 :   X(fd_hash_t,                         prev_bank_hash,              sizeof(fd_hash_t),                         alignof(fd_hash_t),                         0,   0,                0    )  /* Previous bank hash */                                     \
     241         339 :   X(fd_hash_t,                         genesis_hash,                sizeof(fd_hash_t),                         alignof(fd_hash_t),                         0,   0,                0    )  /* Genesis hash */                                           \
     242         339 :   X(fd_epoch_schedule_t,               epoch_schedule,              sizeof(fd_epoch_schedule_t),               alignof(fd_epoch_schedule_t),               0,   0,                0    )  /* Epoch schedule */                                         \
     243         339 :   X(fd_rent_t,                         rent,                        sizeof(fd_rent_t),                         alignof(fd_rent_t),                         0,   0,                0    )  /* Rent */                                                   \
     244         339 :   X(fd_lthash_value_t,                 lthash,                      sizeof(fd_lthash_value_t),                 alignof(fd_lthash_value_t),                 0,   0,                1    )  /* LTHash */                                                 \
     245         339 :   X(fd_sysvar_cache_t,                 sysvar_cache,                sizeof(fd_sysvar_cache_t),                 alignof(fd_sysvar_cache_t),                 0,   0,                0    )  /* Sysvar cache */                                           \
     246         339 :                                                                                                                                                                                           /* then there can be 100k unique leaders in the worst */     \
     247         339 :                                                                                                                                                                                           /* case. We also can assume 432k slots per epoch. */         \
     248         339 :   X(fd_features_t,                     features,                    sizeof(fd_features_t),                     alignof(fd_features_t),                     0,   0,                0    )  /* Features */                                               \
     249         339 :   X(ulong,                             txn_count,                   sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Transaction count */                                      \
     250         339 :   X(ulong,                             nonvote_txn_count,           sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Nonvote transaction count */                              \
     251         339 :   X(ulong,                             failed_txn_count,            sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Failed transaction count */                               \
     252         339 :   X(ulong,                             nonvote_failed_txn_count,    sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Nonvote failed transaction count */                       \
     253         339 :   X(ulong,                             total_compute_units_used,    sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Total compute units used */                               \
     254         339 :   X(ulong,                             slots_per_epoch,             sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Slots per epoch */                                        \
     255         339 :   X(ulong,                             shred_cnt,                   sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Shred count */                                            \
     256         339 :   X(ulong,                             epoch,                       sizeof(ulong),                             alignof(ulong),                             0,   0,                0    )  /* Epoch */                                                  \
     257         339 :   X(int,                               has_identity_vote,           sizeof(int),                               alignof(int),                               0,   0,                0    )  /* Has identity vote */                                      \
     258         339 :   X(fd_epoch_rewards_t,                epoch_rewards,               FD_EPOCH_REWARDS_FOOTPRINT,                FD_EPOCH_REWARDS_ALIGN,                     1,   1,                1    )  /* Epoch rewards */                                          \
     259         339 :   X(fd_epoch_leaders_t,                epoch_leaders,               FD_EPOCH_LEADERS_MAX_FOOTPRINT,            FD_EPOCH_LEADERS_ALIGN,                     1,   1,                1    )  /* Epoch leaders. If our system supports 100k vote accs, */  \
     260         339 :   X(fd_vote_states_t,                  vote_states,                 FD_VOTE_STATES_FOOTPRINT,                  FD_VOTE_STATES_ALIGN,                       1,   0,                1    )  /* Vote states for all vote accounts as of epoch E if */     \
     261          30 :                                                                                                                                                                                           /* epoch E is the one that is currently being executed */    \
     262         339 :   X(fd_vote_states_t,                  vote_states_prev,            FD_VOTE_STATES_FOOTPRINT,                  FD_VOTE_STATES_ALIGN,                       1,   1,                1    )  /* Vote states for all vote accounts as of of the end of */  \
     263          30 :                                                                                                                                                                                           /* epoch E-1 if epoch E is currently being executed */       \
     264         339 :   X(fd_vote_states_t,                  vote_states_prev_prev,       FD_VOTE_STATES_FOOTPRINT,                  FD_VOTE_STATES_ALIGN,                       1,   1,                1    )  /* Vote states for all vote accounts as of the end of */     \
     265             :                                                                                                                                                                                           /* epoch E-2 if epoch E is currently being executed */
     266             : 
     267             : /* Invariant Every CoW field must have a rw-lock */
     268             : #define X(type, name, footprint, align, cow, limit_fork_width, has_lock)                                              \
     269             :   FD_STATIC_ASSERT( (cow == 1 && has_lock == 1        ) || (cow == 0            ),  CoW fields must have a rw-lock ); \
     270             :   FD_STATIC_ASSERT( (cow == 1 && limit_fork_width == 1) || (limit_fork_width == 0), CoW must be 1 if limit_fork_width is 1 );
     271             :   FD_BANKS_ITER(X)
     272             : #undef X
     273             : 
     274             : /* If a member of the bank is CoW then it needs a corresponding pool
     275             :    which is defined here. If a type if not a CoW then it does not need
     276             :    to be in a pool and is laid out contigiously in the bank struct. */
     277             : 
     278             : /* Declare a pool object wrapper for all CoW fields. */
     279             : #define HAS_COW_1(name, footprint, align)                    \
     280             :   static const ulong fd_bank_##name##_align     = align;     \
     281             :   static const ulong fd_bank_##name##_footprint = footprint; \
     282             :                                                              \
     283             :   struct fd_bank_##name {                                    \
     284             :     ulong next;                                              \
     285             :     uchar data[footprint]__attribute__((aligned(align)));    \
     286             :   };                                                         \
     287             :   typedef struct fd_bank_##name fd_bank_##name##_t;
     288             : 
     289             : /* Do nothing if CoW is not enabled. */
     290             : #define HAS_COW_0(name, footprint, align)
     291             : 
     292             : #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
     293             :   HAS_COW_##cow(name, footprint, align)
     294             :   FD_BANKS_ITER(X)
     295             : 
     296             : struct fd_bank_cost_tracker {
     297             :   ulong next;
     298             :   uchar data[FD_COST_TRACKER_FOOTPRINT] __attribute__((aligned(FD_COST_TRACKER_ALIGN)));
     299             : };
     300             : typedef struct fd_bank_cost_tracker fd_bank_cost_tracker_t;
     301             : 
     302             : #undef X
     303             : #undef HAS_COW_0
     304             : #undef HAS_COW_1
     305             : 
     306             : #define POOL_NAME fd_bank_epoch_leaders_pool
     307         213 : #define POOL_T    fd_bank_epoch_leaders_t
     308             : #include "../../util/tmpl/fd_pool.c"
     309             : 
     310             : #define POOL_NAME fd_bank_epoch_rewards_pool
     311         195 : #define POOL_T    fd_bank_epoch_rewards_t
     312             : #include "../../util/tmpl/fd_pool.c"
     313             : 
     314             : #define POOL_NAME fd_bank_vote_states_pool
     315         210 : #define POOL_T    fd_bank_vote_states_t
     316             : #include "../../util/tmpl/fd_pool.c"
     317             : 
     318             : #define POOL_NAME fd_bank_vote_states_prev_pool
     319         225 : #define POOL_T    fd_bank_vote_states_prev_t
     320             : #include "../../util/tmpl/fd_pool.c"
     321             : 
     322             : #define POOL_NAME fd_bank_vote_states_prev_prev_pool
     323         195 : #define POOL_T    fd_bank_vote_states_prev_prev_t
     324             : #include "../../util/tmpl/fd_pool.c"
     325             : 
     326             : #define POOL_NAME fd_bank_cost_tracker_pool
     327         396 : #define POOL_T    fd_bank_cost_tracker_t
     328             : #include "../../util/tmpl/fd_pool.c"
     329             : 
     330         198 : #define FD_BANK_FLAGS_INIT              (0x00000001UL) /* Initialized.  Not yet replayable. */
     331          75 : #define FD_BANK_FLAGS_REPLAYABLE        (0x00000002UL) /* Replayable. */
     332          69 : #define FD_BANK_FLAGS_FROZEN            (0x00000004UL) /* Frozen.  We finished replaying or because it was a snapshot/genesis loaded bank. */
     333          51 : #define FD_BANK_FLAGS_DEAD              (0x00000008UL) /* Dead.  We stopped replaying it before we could finish it (e.g. invalid block or pruned minority fork). */
     334         114 : #define FD_BANK_FLAGS_ROOTED            (0x00000010UL) /* Rooted.  Part of the consnensus root fork.  */
     335             : 
     336             : /* As mentioned above, the overall layout of the bank struct:
     337             :    - Fields used for internal pool/bank management
     338             :    - Non-Cow fields
     339             :    - CoW fields
     340             :    - Locks for CoW fields
     341             : 
     342             :    The CoW fields are laid out contiguously in the bank struct.
     343             :    The locks for the CoW fields are laid out contiguously after the
     344             :    CoW fields.
     345             : 
     346             :    (r) Field is owned by the replay tile, and should be updated only by
     347             :        the replay tile.
     348             : */
     349             : 
     350             : struct fd_bank {
     351             : 
     352             :   /* Fields used for internal pool and bank management */
     353             :   ulong idx;         /* current fork idx of the bank (synchronized with the pool index) */
     354             :   ulong next;        /* reserved for internal use by pool and fd_banks_advance_root */
     355             :   ulong parent_idx;  /* index of the parent in the node pool */
     356             :   ulong child_idx;   /* index of the left-child in the node pool */
     357             :   ulong sibling_idx; /* index of the right-sibling in the node pool */
     358             :   ulong flags;       /* (r) keeps track of the state of the bank, as well as some configurations */
     359             : 
     360             :   /* Define non-templatized types here. */
     361             : 
     362             :   /* Cost tracker. */
     363             : 
     364             :   ulong       cost_tracker_pool_idx;
     365             :   ulong       cost_tracker_pool_offset;
     366             :   fd_rwlock_t cost_tracker_lock;
     367             : 
     368             :   /* Stake delegations delta. */
     369             : 
     370             :   uchar       stake_delegations_delta[FD_STAKE_DELEGATIONS_DELTA_FOOTPRINT] __attribute__((aligned(FD_STAKE_DELEGATIONS_ALIGN)));
     371             :   int         stake_delegations_delta_dirty;
     372             :   fd_rwlock_t stake_delegations_delta_lock;
     373             : 
     374             :   ulong refcnt; /* (r) reference count on the bank, see replay for more details */
     375             : 
     376             :   fd_txncache_fork_id_t txncache_fork_id; /* fork id used by the txn cache */
     377             : 
     378             :   /* Timestamps written and read only by replay */
     379             : 
     380             :   long first_fec_set_received_nanos;
     381             :   long preparation_begin_nanos;
     382             :   long first_transaction_scheduled_nanos;
     383             :   long last_transaction_finished_nanos;
     384             : 
     385             :   /* First, layout all non-CoW fields contiguously. This is done to
     386             :      allow for cloning the bank state with a simple memcpy. Each
     387             :      non-CoW field is just represented as a byte array. */
     388             : 
     389             :   struct {
     390             : 
     391             :   #define HAS_COW_1(type, name, footprint, align)
     392             : 
     393             :   #define HAS_COW_0(type, name, footprint, align) \
     394             :     uchar name[footprint] __attribute__((aligned(align)));
     395             : 
     396             :   #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
     397             :     HAS_COW_##cow(type, name, footprint, align)
     398             :   FD_BANKS_ITER(X)
     399             :   #undef X
     400             :   #undef HAS_COW_0
     401             :   #undef HAS_COW_1
     402             : 
     403             :   } non_cow;
     404             : 
     405             :   /* Now, layout all information needed for CoW fields. These are only
     406             :      copied when explicitly requested by the caller. The field's data
     407             :      is located at teh pool idx in the pool. If the dirty flag has been
     408             :      set, then the element has been copied over for this bank. */
     409             : 
     410             :   #define HAS_COW_1(type, name, footprint, align) \
     411             :     int   name##_dirty;                           \
     412             :     ulong name##_pool_idx;                        \
     413             :     ulong name##_pool_offset;                     \
     414             :     ulong name##_pool_lock_offset;
     415             : 
     416             :   #define HAS_COW_0(type, name, footprint, align)
     417             : 
     418             :   #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
     419             :     HAS_COW_##cow(type, name, footprint, align)
     420             :   FD_BANKS_ITER(X)
     421             :   #undef X
     422             :   #undef HAS_COW_0
     423             :   #undef HAS_COW_1
     424             : 
     425             :   /* Now emit locks for all fields that need a rwlock. */
     426             : 
     427             :   #define HAS_LOCK_1(type, name, footprint, align) \
     428             :     fd_rwlock_t name##_lock;
     429             : 
     430             :   #define HAS_LOCK_0(type, name, footprint, align) /* Do nothing for these. */
     431             : 
     432             :   #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
     433             :     HAS_LOCK_##has_lock(type, name, footprint, align)
     434             :   FD_BANKS_ITER(X)
     435             :   #undef X
     436             :   #undef HAS_LOCK_0
     437             :   #undef HAS_LOCK_1
     438             : 
     439             : };
     440             : typedef struct fd_bank fd_bank_t;
     441             : 
     442             : #define HAS_COW_1(type, name, footprint, align)                                  \
     443             : static inline void                                                               \
     444         480 : fd_bank_set_##name##_pool( fd_bank_t * bank, fd_bank_##name##_t * bank_pool ) {  \
     445         480 :   void * bank_pool_mem = fd_bank_##name##_pool_leave( bank_pool );               \
     446         480 :   if( FD_UNLIKELY( !bank_pool_mem ) ) {                                          \
     447           0 :     FD_LOG_CRIT(( "Failed to leave bank pool" ));                                \
     448           0 :   }                                                                              \
     449         480 :   bank->name##_pool_offset = (ulong)bank_pool_mem - (ulong)bank;                 \
     450         480 : }                                                                                \
     451             : static inline fd_bank_##name##_t *                                               \
     452         108 : fd_bank_get_##name##_pool( fd_bank_t * bank ) {                                  \
     453         108 :   return fd_bank_##name##_pool_join( (uchar *)bank + bank->name##_pool_offset ); \
     454         108 : }                                                                                \
     455             : static inline void                                                               \
     456         480 : fd_bank_set_##name##_pool_lock( fd_bank_t * bank, fd_rwlock_t * rwlock ) {       \
     457         480 :   bank->name##_pool_lock_offset = (ulong)rwlock - (ulong)bank;                   \
     458         480 : }                                                                                \
     459             : static inline fd_rwlock_t *                                                      \
     460          36 : fd_bank_get_##name##_pool_lock( fd_bank_t * bank ) {                             \
     461          36 :   return (fd_rwlock_t *)( (uchar *)bank + bank->name##_pool_lock_offset );       \
     462          36 : }
     463             : #define HAS_COW_0(type, name, footprint, align) /* Do nothing for these. */
     464             : 
     465             : #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
     466             :   HAS_COW_##cow(type, name, footprint, align)
     467             : FD_BANKS_ITER(X)
     468             : #undef X
     469             : #undef HAS_COW_0
     470             : #undef HAS_COW_1
     471             : 
     472             : /* Do the same setup for the cost tracker pool. */
     473             : 
     474             : static inline void
     475         102 : fd_bank_set_cost_tracker_pool( fd_bank_t * bank, fd_bank_cost_tracker_t * cost_tracker_pool ) {
     476         102 :   void * cost_tracker_pool_mem = fd_bank_cost_tracker_pool_leave( cost_tracker_pool );
     477         102 :   if( FD_UNLIKELY( !cost_tracker_pool_mem ) ) {
     478           0 :     FD_LOG_CRIT(( "Failed to leave cost tracker pool" ));
     479           0 :   }
     480         102 :   bank->cost_tracker_pool_offset = (ulong)cost_tracker_pool_mem - (ulong)bank;
     481         102 : }
     482             : 
     483             : static inline fd_bank_cost_tracker_t *
     484         264 : fd_bank_get_cost_tracker_pool( fd_bank_t * bank ) {
     485         264 :   return fd_bank_cost_tracker_pool_join( (uchar *)bank + bank->cost_tracker_pool_offset );
     486         264 : }
     487             : 
     488             : /* fd_bank_t is the alignment for the bank state. */
     489             : 
     490             : ulong
     491             : fd_bank_align( void );
     492             : 
     493             : /* fd_bank_t is the footprint for the bank state. This does NOT
     494             :    include the footprint for the CoW state. */
     495             : 
     496             : ulong
     497             : fd_bank_footprint( void );
     498             : 
     499             : /**********************************************************************/
     500             : /* fd_banks_t is the main struct used to manage the bank state.  It can
     501             :    be used to query/modify/clone/publish the bank state.
     502             : 
     503             :    fd_banks_t contains some metadata to a pool to manage the banks.
     504             :    It also contains pointers to the CoW pools.
     505             : 
     506             :    The data is laid out contiguously in memory starting from fd_banks_t;
     507             :    this can be seen in fd_banks_footprint(). */
     508             : 
     509             : #define POOL_NAME fd_banks_pool
     510         420 : #define POOL_T    fd_bank_t
     511             : #include "../../util/tmpl/fd_pool.c"
     512             : 
     513             : struct fd_banks {
     514             :   ulong       magic;           /* ==FD_BANKS_MAGIC */
     515             :   ulong       max_total_banks; /* Maximum number of banks */
     516             :   ulong       max_fork_width;  /* Maximum fork width executing through
     517             :                                   any given slot. */
     518             :   ulong       root_idx;        /* root idx */
     519             : 
     520             :   /* This lock is only used to serialize banks fork tree reads with
     521             :      respect to fork tree writes.  In other words, tree traversals
     522             :      cannot happen at the same time as a tree pruning operation or a
     523             :      tree insertion operation.  So the public APIs on banks take either
     524             :      a read lock or a write lock depending on what they do on the fork
     525             :      tree.  For example, publishing takes a write lock, and bank lookups
     526             :      take a read lock.  Notably, individual banks can still be
     527             :      concurrently accessed or modified, and this lock does not offer
     528             :      synchronization on individual fields within a bank. */
     529             :   fd_rwlock_t rwlock;
     530             : 
     531             :   ulong       pool_offset;     /* offset of pool from banks */
     532             : 
     533             :   ulong       cost_tracker_pool_offset; /* offset of cost tracker pool from banks */
     534             : 
     535             :   /* stake_delegations_root will be the full state of stake delegations
     536             :      for the current root. It can get updated in two ways:
     537             :      1. On boot the snapshot will be directly read into the rooted
     538             :         stake delegations because we assume that any and all snapshots
     539             :         are a rooted slot.
     540             :      2. Calls to fd_banks_publish() will apply all of the stake
     541             :         delegation deltas from each of the banks that are about to be
     542             :         published.  */
     543             : 
     544             :   uchar stake_delegations_root[FD_STAKE_DELEGATIONS_FOOTPRINT] __attribute__((aligned(FD_STAKE_DELEGATIONS_ALIGN)));
     545             : 
     546             :   /* stake_delegations_frontier is reserved memory that can represent
     547             :      the full state of stake delegations for the current frontier. This
     548             :      is done by taking the stake_delegations_root and applying all of
     549             :      the deltas from the current bank and all of its ancestors up to the
     550             :      root bank. */
     551             : 
     552             :   uchar stake_delegations_frontier[FD_STAKE_DELEGATIONS_FOOTPRINT] __attribute__((aligned(FD_STAKE_DELEGATIONS_ALIGN)));
     553             : 
     554             :   /* Layout all CoW pools. */
     555             : 
     556             :   #define HAS_COW_1(type, name, footprint, align)                   \
     557             :     ulong       name##_pool_offset; /* offset of pool from banks */ \
     558             :     fd_rwlock_t name##_pool_lock;   /* lock for the pool */
     559             : 
     560             :   #define HAS_COW_0(type, name, footprint, align) /* Do nothing for these. */
     561             : 
     562             :   #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
     563             :     HAS_COW_##cow(type, name, footprint, align)
     564             :   FD_BANKS_ITER(X)
     565             :   #undef X
     566             :   #undef HAS_COW_0
     567             :   #undef HAS_COW_1
     568             : };
     569             : typedef struct fd_banks fd_banks_t;
     570             : 
     571             : /* Bank accesssors. Different accessors are emitted for different types
     572             :    depending on if the field has a lock or not. */
     573             : 
     574             : #define HAS_LOCK_1(type, name)                                     \
     575             :   type const * fd_bank_##name##_locking_query( fd_bank_t * bank ); \
     576             :   void fd_bank_##name##_end_locking_query( fd_bank_t * bank );     \
     577             :   type * fd_bank_##name##_locking_modify( fd_bank_t * bank );      \
     578             :   void fd_bank_##name##_end_locking_modify( fd_bank_t * bank );
     579             : 
     580             : #define HAS_LOCK_0(type, name)                                   \
     581             :   type const * fd_bank_##name##_query( fd_bank_t const * bank ); \
     582             :   type * fd_bank_##name##_modify( fd_bank_t * bank );
     583             : 
     584             : #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
     585             :   void fd_bank_##name##_set( fd_bank_t * bank, type value );             \
     586             :   type fd_bank_##name##_get( fd_bank_t const * bank );                   \
     587             :   HAS_LOCK_##has_lock(type, name)
     588             : FD_BANKS_ITER(X)
     589             : #undef X
     590             : 
     591             : /* Define accessor and mutator functions for the non-templatized
     592             :    fields. */
     593             : 
     594             : static inline fd_cost_tracker_t *
     595           0 : fd_bank_cost_tracker_locking_modify( fd_bank_t * bank ) {
     596           0 :   fd_bank_cost_tracker_t * cost_tracker_pool = fd_bank_get_cost_tracker_pool( bank );
     597           0 :   FD_TEST( bank->cost_tracker_pool_idx!=fd_bank_cost_tracker_pool_idx_null( cost_tracker_pool ) );
     598           0 :   uchar * cost_tracker_mem = fd_bank_cost_tracker_pool_ele( cost_tracker_pool, bank->cost_tracker_pool_idx )->data;
     599           0 :   FD_TEST( cost_tracker_mem );
     600           0 :   fd_rwlock_write( &bank->cost_tracker_lock );
     601           0 :   return fd_type_pun( cost_tracker_mem );
     602           0 : }
     603             : 
     604             : static inline void
     605           0 : fd_bank_cost_tracker_end_locking_modify( fd_bank_t * bank ) {
     606           0 :   fd_rwlock_unwrite( &bank->cost_tracker_lock );
     607           0 : }
     608             : 
     609             : static inline fd_cost_tracker_t const *
     610           0 : fd_bank_cost_tracker_locking_query( fd_bank_t * bank ) {
     611           0 :   fd_bank_cost_tracker_t * cost_tracker_pool = fd_bank_get_cost_tracker_pool( bank );
     612           0 :   FD_TEST( bank->cost_tracker_pool_idx!=fd_bank_cost_tracker_pool_idx_null( cost_tracker_pool ) );
     613           0 :   uchar * cost_tracker_mem = fd_bank_cost_tracker_pool_ele( cost_tracker_pool, bank->cost_tracker_pool_idx )->data;
     614           0 :   FD_TEST( cost_tracker_mem );
     615           0 :   fd_rwlock_read( &bank->cost_tracker_lock );
     616           0 :   return fd_type_pun_const( cost_tracker_mem );
     617           0 : }
     618             : 
     619             : static inline void
     620           0 : fd_bank_cost_tracker_end_locking_query( fd_bank_t * bank ) {
     621           0 :   fd_rwlock_unread( &bank->cost_tracker_lock );
     622           0 : }
     623             : 
     624             : #undef HAS_LOCK_0
     625             : #undef HAS_LOCK_1
     626             : 
     627             : /* Each bank has a fd_stake_delegations_t object which is delta-based.
     628             :    The usage pattern is the same as other bank fields:
     629             :    1. fd_bank_stake_dleegations_delta_locking_modify( bank ) will return
     630             :       a mutable pointer to the stake delegations delta object. If the
     631             :       caller has not yet initialized the delta object, then it will
     632             :       be initialized. Because it is a delta it is not copied over from
     633             :       a parent bank.
     634             :    2. fd_bank_stake_delegations_delta_locking_query( bank ) will return
     635             :       a const pointer to the stake delegations delta object. If the
     636             :       delta object has not been initialized, then NULL is returned.
     637             :    3. fd_bank_stake_delegations_delta_locking_end_modify( bank ) will
     638             :       release the write lock on the object.
     639             :    4. fd_bank_stake_delegations_delta_locking_end_query( bank ) will
     640             :       release a read lock on the object.
     641             : */
     642             : 
     643             : static inline fd_stake_delegations_t *
     644          15 : fd_bank_stake_delegations_delta_locking_modify( fd_bank_t * bank ) {
     645          15 :   fd_rwlock_write( &bank->stake_delegations_delta_lock );
     646          15 :   if( !bank->stake_delegations_delta_dirty ) {
     647          15 :     bank->stake_delegations_delta_dirty = 1;
     648          15 :     fd_stake_delegations_new( bank->stake_delegations_delta, FD_STAKE_DELEGATIONS_MAX_PER_SLOT, 1 );
     649          15 :   }
     650          15 :   return fd_stake_delegations_join( bank->stake_delegations_delta );
     651          15 : }
     652             : 
     653             : static inline void
     654          15 : fd_bank_stake_delegations_delta_end_locking_modify( fd_bank_t * bank ) {
     655          15 :   fd_rwlock_unwrite( &bank->stake_delegations_delta_lock );
     656          15 : }
     657             : 
     658             : static inline fd_stake_delegations_t *
     659           0 : fd_bank_stake_delegations_delta_locking_query( fd_bank_t * bank ) {
     660           0 :   fd_rwlock_read( &bank->stake_delegations_delta_lock );
     661           0 :   return bank->stake_delegations_delta_dirty ? fd_stake_delegations_join( bank->stake_delegations_delta ) : NULL;
     662           0 : }
     663             : 
     664             : static inline void
     665           0 : fd_bank_stake_delegations_delta_end_locking_query( fd_bank_t * bank ) {
     666           0 :   fd_rwlock_unread( &bank->stake_delegations_delta_lock );
     667           0 : }
     668             : 
     669             : /* fd_bank_stake_delegations_frontier_query() will return a pointer to
     670             :    the full stake delegations for the current frontier. The caller is
     671             :    responsible that there are no concurrent readers or writers to
     672             :    the stake delegations returned by this function.
     673             : 
     674             :    Under the hood, the function copies the rooted stake delegations and
     675             :    applies all of the deltas for the direct ancestry from the current
     676             :    bank up to the rooted bank to the copy. */
     677             : 
     678             : fd_stake_delegations_t *
     679             : fd_bank_stake_delegations_frontier_query( fd_banks_t * banks,
     680             :                                           fd_bank_t *  bank );
     681             : 
     682             : /* fd_banks_stake_delegations_root_query() will return a pointer to the
     683             :    full stake delegations for the current root. This function should
     684             :    only be called on boot. */
     685             : 
     686             : fd_stake_delegations_t *
     687             : fd_banks_stake_delegations_root_query( fd_banks_t * banks );
     688             : 
     689             : /* Simple getters and setters for the pools/maps in fd_banks_t.  Notably,
     690             :    the pool for the fd_bank_t structs as well as a map and pool pair of
     691             :    the CoW structs in the banks. */
     692             : 
     693             : static inline fd_bank_t *
     694         399 : fd_banks_get_bank_pool( fd_banks_t const * banks ) {
     695         399 :   return fd_banks_pool_join( ((uchar *)banks + banks->pool_offset) );
     696         399 : }
     697             : 
     698             : static inline void
     699             : fd_banks_set_bank_pool( fd_banks_t * banks,
     700           6 :                         fd_bank_t *  bank_pool ) {
     701           6 :   void * bank_pool_mem = fd_banks_pool_leave( bank_pool );
     702           6 :   if( FD_UNLIKELY( !bank_pool_mem ) ) {
     703           0 :     FD_LOG_CRIT(( "Failed to leave bank pool" ));
     704           0 :   }
     705           6 :   banks->pool_offset = (ulong)bank_pool_mem - (ulong)banks;
     706           6 : }
     707             : 
     708             : #define HAS_COW_1(type, name, footprint, align)                                    \
     709             : static inline fd_bank_##name##_t *                                                 \
     710         825 : fd_banks_get_##name##_pool( fd_banks_t * banks ) {                                 \
     711         825 :   return fd_bank_##name##_pool_join( (uchar *)banks + banks->name##_pool_offset ); \
     712         825 : }                                                                                  \
     713             : static inline void                                                                 \
     714          30 : fd_banks_set_##name##_pool( fd_banks_t * banks, fd_bank_##name##_t * bank_pool ) { \
     715          30 :   void * bank_pool_mem = fd_bank_##name##_pool_leave( bank_pool );                 \
     716          30 :   if( FD_UNLIKELY( !bank_pool_mem ) ) {                                            \
     717           0 :     FD_LOG_CRIT(( "Failed to leave bank pool" ));                                  \
     718           0 :   }                                                                                \
     719          30 :   banks->name##_pool_offset = (ulong)bank_pool_mem - (ulong)banks;                 \
     720          30 : }
     721             : 
     722             : #define HAS_COW_0(type, name, footprint, align) /* Do nothing for these. */
     723             : 
     724             : #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
     725             :   HAS_COW_##cow(type, name, footprint, align)
     726             : FD_BANKS_ITER(X)
     727             : #undef X
     728             : #undef HAS_COW_0
     729             : #undef HAS_COW_1
     730             : 
     731             : static inline fd_bank_cost_tracker_t *
     732         111 : fd_banks_get_cost_tracker_pool( fd_banks_t * banks ) {
     733         111 :   return fd_bank_cost_tracker_pool_join( (uchar *)banks + banks->cost_tracker_pool_offset );
     734         111 : }
     735             : 
     736             : static inline void
     737             : fd_banks_set_cost_tracker_pool( fd_banks_t *             banks,
     738           6 :                                 fd_bank_cost_tracker_t * cost_tracker_pool ) {
     739           6 :   void * cost_tracker_pool_mem = fd_bank_cost_tracker_pool_leave( cost_tracker_pool );
     740           6 :   if( FD_UNLIKELY( !cost_tracker_pool_mem ) ) {
     741           0 :     FD_LOG_CRIT(( "Failed to leave cost tracker pool" ));
     742           0 :   }
     743           6 :   banks->cost_tracker_pool_offset = (ulong)cost_tracker_pool_mem - (ulong)banks;
     744           6 : }
     745             : 
     746             : /* fd_banks_root() and fd_banks_root_const() returns a non-const and
     747             :    const pointer to the root bank respectively. */
     748             : 
     749             : FD_FN_PURE static inline fd_bank_t const *
     750           0 : fd_banks_root_const( fd_banks_t const * banks ) {
     751           0 :   return fd_banks_pool_ele_const( fd_banks_get_bank_pool( banks ), banks->root_idx );
     752           0 : }
     753             : 
     754             : FD_FN_PURE static inline fd_bank_t *
     755          42 : fd_banks_root( fd_banks_t * banks ) {
     756          42 :   return fd_banks_pool_ele( fd_banks_get_bank_pool( banks ), banks->root_idx );
     757          42 : }
     758             : 
     759             : /* fd_banks_align() returns the alignment of fd_banks_t */
     760             : 
     761             : ulong
     762             : fd_banks_align( void );
     763             : 
     764             : /* fd_banks_footprint() returns the footprint of fd_banks_t.  This
     765             :    includes the struct itself but also the footprint for all of the
     766             :    pools.
     767             : 
     768             :    The footprint of fd_banks_t is determined by the total number
     769             :    of banks that the bank manages.  This is an analog for the max number
     770             :    of unrooted blocks the bank can manage at any given time.
     771             : 
     772             :    We can also further bound the memory footprint of the banks by the
     773             :    max width of forks that can exist at any given time.  The reason for
     774             :    this is that there are several large CoW structs that are only
     775             :    written to during the epoch boundary (e.g. epoch_rewards,
     776             :    epoch_stakes, etc.).  These structs are read-only afterwards. This
     777             :    means if we also bound the max number of forks that can execute
     778             :    through the epoch boundary, we can bound the memory footprint of
     779             :    the banks. */
     780             : 
     781             : ulong
     782             : fd_banks_footprint( ulong max_total_banks,
     783             :                     ulong max_fork_width );
     784             : 
     785             : /* fd_banks_new() creates a new fd_banks_t struct.  This function lays
     786             :    out the memory for all of the constituent fd_bank_t structs and
     787             :    pools depending on the max_total_banks and the max_fork_width for a
     788             :    given block. */
     789             : 
     790             : void *
     791             : fd_banks_new( void * mem,
     792             :               ulong  max_total_banks,
     793             :               ulong  max_fork_width,
     794             :               int    larger_max_cost_per_block,
     795             :               ulong  seed );
     796             : 
     797             : /* fd_banks_join() joins a new fd_banks_t struct. */
     798             : 
     799             : fd_banks_t *
     800             : fd_banks_join( void * mem );
     801             : 
     802             : /* fd_banks_leave() leaves a bank. */
     803             : 
     804             : void *
     805             : fd_banks_leave( fd_banks_t * banks );
     806             : 
     807             : /* fd_banks_delete() deletes a bank. */
     808             : 
     809             : void *
     810             : fd_banks_delete( void * shmem );
     811             : 
     812             : /* fd_banks_init_bank() initializes a new bank in the bank manager.
     813             :    This should only be used during bootup. This returns an initial
     814             :    fd_bank_t with the corresponding bank index set to 0. */
     815             : 
     816             : fd_bank_t *
     817             : fd_banks_init_bank( fd_banks_t * banks );
     818             : 
     819             : /* fd_banks_get_bank_idx returns a bank for a given bank index. */
     820             : 
     821             : static inline fd_bank_t *
     822             : fd_banks_bank_mem_query( fd_banks_t * banks,
     823           0 :                          ulong        bank_idx ) {
     824           0 :   return fd_banks_pool_ele( fd_banks_get_bank_pool( banks ), bank_idx );
     825           0 : }
     826             : 
     827             : static inline fd_bank_t *
     828             : fd_banks_bank_query( fd_banks_t * banks,
     829         126 :                      ulong        bank_idx ) {
     830         126 :   fd_bank_t * bank = fd_banks_pool_ele( fd_banks_get_bank_pool( banks ), bank_idx );
     831         126 :   return (bank->flags&FD_BANK_FLAGS_INIT) ? bank : NULL;
     832         126 : }
     833             : 
     834             : static inline fd_bank_t *
     835             : fd_banks_get_parent( fd_banks_t * banks,
     836           0 :                      fd_bank_t *  bank ) {
     837           0 :   return fd_banks_pool_ele( fd_banks_get_bank_pool( banks ), bank->parent_idx );
     838           0 : }
     839             : 
     840             : /* fd_banks_clone_from_parent() clones a bank from a parent bank.
     841             :    This function links the child bank to its parent bank and copies
     842             :    over the data from the parent bank to the child.  This function
     843             :    assumes that the child and parent banks both have been allocated.
     844             :    The parent bank must be frozen and the child bank must be initialized
     845             :    but not yet used.
     846             : 
     847             :    A more detailed note: not all of the data is copied over and this
     848             :    is a shallow clone.  All of the CoW fields are not copied over and
     849             :    will only be done so if the caller explicitly calls
     850             :    fd_bank_{*}_modify().  This naming was chosen to emulate the
     851             :    semantics of the Agave client. */
     852             : 
     853             : fd_bank_t *
     854             : fd_banks_clone_from_parent( fd_banks_t * banks,
     855             :                             ulong        bank_idx,
     856             :                             ulong        parent_bank_idx );
     857             : 
     858             : /* fd_banks_advance_root() advances the root bank to the bank manager.
     859             :    This should only be used when a bank is no longer needed and has no
     860             :    active refcnts.  This will prune off the bank from the bank manager.
     861             :    It returns the new root bank.  An invariant of this function is that
     862             :    the new root bank should be a child of the current root bank.
     863             : 
     864             :    All banks that are ancestors or siblings of the new root bank will be
     865             :    cancelled and their resources will be released back to the pool. */
     866             : 
     867             : fd_bank_t const *
     868             : fd_banks_advance_root( fd_banks_t * banks,
     869             :                        ulong        bank_idx );
     870             : 
     871             : /* fd_bank_clear_bank() clears the contents of a bank. This should ONLY
     872             :    be used with banks that have no children and should only be used in
     873             :    testing and fuzzing.
     874             : 
     875             :    This function will memset all non-CoW fields to 0.
     876             : 
     877             :    For all CoW fields, we will reset the indices to its parent. */
     878             : 
     879             : void
     880             : fd_banks_clear_bank( fd_banks_t * banks,
     881             :                      fd_bank_t *  bank );
     882             : 
     883             : /* fd_banks_advance_root_prepare returns the highest block that can be
     884             :    safely advanced between the current root of the fork tree and the
     885             :    target block.  See the note on safe publishing for more details.  In
     886             :    general, a node in the fork tree can be pruned if:
     887             :    (1) the node itself can be pruned, and
     888             :    (2) all subtrees (except for the one on the rooted fork) forking off
     889             :        of the node can be pruned.
     890             :    The highest publishable block is the highest block on the rooted fork
     891             :    where the above is true, or the rooted child block of such if there
     892             :    is one.
     893             : 
     894             :    This function assumes that the given target block has been rooted by
     895             :    consensus.  It will mark every block on the rooted fork as rooted, up
     896             :    to the given target block.  It will also mark minority forks as dead.
     897             : 
     898             :    Highest advanceable block is written to the out pointer.  Returns 1
     899             :    if the advanceable block can be advanced beyond the current root.
     900             :    Returns 0 if no such block can be found.  We will ONLY advance our
     901             :    advanceable_bank_idx to a child of the current root.  In order to
     902             :    advance to the target bank, fd_banks_advance_root_prepare() must be
     903             :    called repeatedly. */
     904             : 
     905             : int
     906             : fd_banks_advance_root_prepare( fd_banks_t * banks,
     907             :                                ulong        target_bank_idx,
     908             :                                ulong *      advanceable_bank_idx_out );
     909             : 
     910             : /* fd_banks_mark_bank_dead marks the current bank (and all of its
     911             :    descendants) as dead.  The caller is still responsible for handling
     912             :    the behavior of the dead bank correctly. */
     913             : 
     914             : void
     915             : fd_banks_mark_bank_dead( fd_banks_t * banks,
     916             :                          fd_bank_t *  bank );
     917             : 
     918             : /* fd_banks_mark_bank_frozen marks the current bank as frozen.  This
     919             :    should be done when the bank is no longer being updated: it should be
     920             :    done at the end of a slot.  This also releases the memory for the
     921             :    cost tracker which only has to be persisted from the start of a slot
     922             :    to the end. */
     923             : 
     924             : void
     925             : fd_banks_mark_bank_frozen( fd_banks_t * banks,
     926             :                            fd_bank_t *  bank );
     927             : 
     928             : /* fd_banks_new_bank reserves a bank index for a new bank.  New bank
     929             :    indicies should always be available.  After this function is called,
     930             :    the bank will be linked to its parent bank, but not yet replayable.
     931             :    After a call to fd_banks_clone_from_parent, the bank will be
     932             :    replayable.  This assumes that there is a parent bank which exists
     933             :    and the there are available bank indices in the bank pool. */
     934             : 
     935             : fd_bank_t *
     936             : fd_banks_new_bank( fd_banks_t * banks,
     937             :                    ulong        parent_bank_idx,
     938             :                    long         now );
     939             : 
     940             : 
     941             : /* fd_banks_is_full returns 1 if the banks are full, 0 otherwise. */
     942             : 
     943             : static inline int
     944           0 : fd_banks_is_full( fd_banks_t * banks ) {
     945           0 :   return fd_banks_pool_free( fd_banks_get_bank_pool( banks ) )==0UL;
     946           0 : }
     947             : 
     948             : /* fd_banks_validate does validation on the banks struct to make sure
     949             :    that there are no corruptions/invariant violations.  It returns 0
     950             :    if no issues have been detected and 1 otherwise.
     951             : 
     952             :    List of checks that the function currently performs:
     953             :    1. CoW fields have not acquired more elements than the max amount of
     954             :       allocated banks.
     955             :    (Add more checks as needed here)
     956             : */
     957             : 
     958             : int
     959             : fd_banks_validate( fd_banks_t * banks );
     960             : 
     961             : FD_PROTOTYPES_END
     962             : 
     963             : #endif /* HEADER_fd_src_flamenco_runtime_fd_bank_h */

Generated by: LCOV version 1.14