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 :
15 : FD_PROTOTYPES_BEGIN
16 :
17 6 : #define FD_BANKS_MAGIC (0XF17EDA2C7EBA2450) /* FIREDANCER BANKS V0 */
18 :
19 : /* TODO: Some optimizations, cleanups, future work:
20 : 1. Simple data types (ulong, int, etc) should be stored as their
21 : underlying type instead of a byte array.
22 : 2. Perhaps make the query/modify scoping more explicit. Right now,
23 : the caller is free to use the API wrong if there are no locks.
24 : Maybe just expose a different API if there are no locks?
25 : 3. Rename locks to suffix with _query_locking and _query_locking_end
26 : 4. Don't templatize out more complex types.
27 : */
28 :
29 : /* A fd_bank_t struct is the representation of the bank state on Solana
30 : for a given block. More specifically, the bank state corresponds to
31 : all information needed during execution that is not stored on-chain,
32 : but is instead cached in a validator's memory. Each of these bank
33 : fields are repesented by a member of the fd_bank_t struct.
34 :
35 : Management of fd_bank_t structs must be fork-aware: the state of each
36 : fd_bank_t must be based on the fd_bank_t of it's parent block. This
37 : state is managed by the fd_banks_t struct.
38 :
39 : In order to support fork-awareness, there are several key features
40 : that fd_banks_t and fd_bank_t MUST support:
41 : 1. Query for any non-rooted block's bank: create a fast lookup
42 : from bank index to bank
43 : 2. Be able to create a new bank for a given block from the bank of
44 : that block's parent and maintain some tree-like structure to
45 : track the parent-child relationships: copy the contents from a
46 : parent bank into a child bank.
47 : 3. Prune the set of active banks to keep the root updated as the
48 : network progresses: free resources of fd_bank_t structs that
49 : are are not direct descendants of the root bank (remove parents
50 : and any competing lineages).
51 : 4. Each bank will have field(s) that are concurrently read/write
52 : from multiple threads: add read-write locks to the fields that are
53 : concurrently written to.
54 : 5. In practice, a bank state for a given block can be very large and
55 : not all of the fields are written to every block. Therefore, it can
56 : be very expensive to copy the entire bank state for a given block
57 : each time a bank is created. In order to avoid large memcpys, we
58 : can use a CoW mechanism for certain fields.
59 : 6. In a similar vein, some fields are very large and are not written
60 : to very often, and are only read at the epoch boundary. The most
61 : notable example is the stake delegations cache. In order to handle
62 : this, we can use a delta-based approach where each bank only has
63 : a delta of the stake delegations. The root bank will own the full
64 : set of stake delegations. This means that the deltas are only
65 : applied to the root bank as each bank gets rooted. If the caller
66 : needs to access the full set of stake delegations for a given
67 : bank, they can assemble the full set of stake delegations by
68 : applying all of the deltas from the current bank and all of its
69 : ancestors up to the root bank.
70 :
71 : Each field of a fd_bank_t has a pre-specified set of fields including
72 : - name: the name of the field
73 : - footprint: the size of the field in bytes
74 : - align: the alignment of the field
75 : - CoW: whether the field is CoW
76 : - has_lock: whether the field has a rw-lock
77 : - type: type of the field
78 :
79 : fd_banks_t is represented by a left-child, right-sibling n-ary tree
80 : (inspired by fd_ghost) to keep track of the parent-child fork tree.
81 : The underlying data structure is a pool of fd_bank_t structs. Banks
82 : are then accessed via an index into the bank pool (bank index).
83 :
84 : NOTE: The reason fd_banks_t is keyed by bank index and not by slot is
85 : to handle block equivocation: if there are two different blocks for
86 : the same slot, we need to be able to differentiate and handle both
87 : blocks against different banks. As mentioned above, the bank index is
88 : just an index into the bank pool. The caller is responsible for
89 : establishing a mapping from the bank index (which is managed by
90 : fd_banks_t) and runtime state (e.g. slot number).
91 :
92 : Most of the fields a fd_bank_t are templatized and can support CoW
93 : sematics or locking semantics.
94 :
95 : Each field in fd_bank_t that is not CoW is laid out contiguously in
96 : the fd_bank_t struct as simple uchar buffers. This allows for a simple
97 : memcpy to clone the bank state from a parent to a child.
98 :
99 : Each field that is CoW has its own memory pool. The memory
100 : corresponding to the field is not located in the fd_bank_t struct and
101 : is instead represented by a pool index and a dirty flag. If the field
102 : is modified, then the dirty flag is set, and an element of the pool
103 : is acquired and the data is copied over from the parent pool idx.
104 :
105 : Not all fields in the bank are templatized: stake_delegations and
106 : the cost_tracker.
107 :
108 : Currently, there is a delta-based field, fd_stake_delegations_t.
109 : Each bank stores a delta-based representation in the form of an
110 : aligned uchar buffer. The full state is stored in fd_banks_t also as
111 : a uchar buffer which corresponds to the full state of stake
112 : delegations for the current root. fd_banks_t also reserves another
113 : buffer which can store the full state of the stake delegations.
114 :
115 : The cost tracker is allocated from a pool. The lifetime of a cost
116 : tracker element starts when the bank is linked to a parent with a
117 : call to fd_banks_clone_from_parent() which makes the bank replayable.
118 : The lifetime of a cost tracker element ends when the bank is marked
119 : dead or when the bank is frozen.
120 :
121 : So, when a bank is cloned from a parent, the non CoW fields are copied
122 : over and the CoW fields just copy over a pool index. The CoW behavior
123 : is completely abstracted away from the caller as callers have to
124 : query/modify fields using specific APIs.
125 :
126 : The memory for the banks is based off of two bounds:
127 : 1. the max number of unrooted blocks at any given time. Most fields
128 : can be bounded by this value.
129 : 2. the max number of forks that execute through any 1 block. We bound
130 : fields that are only written to at the epoch boundary by
131 : the max fork width that can execute through the boundary instead of
132 : by the max number of banks. See fd_banks_footprint() for more
133 : details.
134 :
135 : There are also some important states that a bank can be in:
136 : - Initialized: This bank has been created and linked to a parent bank
137 : index with a call to fd_banks_new_bank(). However, it is not yet
138 : replayable.
139 : - Replayable: This bank has inherited state from its parent and now
140 : transactions can be executed against it. For a bank to become
141 : replayable, it must've been initialized beforehand.
142 : - Dead: This bank has been marked as dead. This means that the block
143 : that this bank is associated with is invalid. A bank can be marked
144 : dead before, during, or after it has finished replaying. A bank
145 : can still be executing transactions while it is marked dead, but it
146 : shouldn't be dispatched any more work.
147 : - Frozen: This bank has been marked as frozen and no other tasks
148 : should be dispatched to it. Any bank-specific resources will be
149 : released (e.g. cost tracker element). A bank can be marked frozen
150 : in two cases:
151 : 1. The bank has finished executing all of its transactions
152 : 2. The bank has been marked dead and there are no outstanding
153 : references to the bank.
154 : A bank can only be copied from a parent bank
155 : (fd_banks_clone_from_parent) if the parent bank has been frozen.
156 : The program will crash if this invariant is violated.
157 :
158 : NOTE: An important invariant is that if a templatized field is CoW,
159 : then it must have a rw-lock.
160 :
161 : NOTE: Another important invariant is that if a templatized field is
162 : limiting its fork width, then it must be CoW.
163 :
164 : The usage pattern is as follows:
165 :
166 : To create an initial bank:
167 : fd_bank_t * bank_init = fd_bank_init_bank( banks );
168 :
169 : To create a new bank:
170 : ulong bank_index = fd_banks_new_bank( banks, parent_bank_index );
171 :
172 : To clone bank from parent banks:
173 : fd_bank_t * bank_clone = fd_banks_clone_from_parent( banks, bank_index, parent_bank_index );
174 :
175 : To advance the root bank
176 : fd_bank_t * root_bank = fd_banks_advance_root( banks, bank_index );
177 :
178 : To query some arbitrary bank:
179 : fd_bank_t * bank_query = fd_banks_bank_query( banks, bank_index );
180 :
181 : To access fields in the bank if a field does not have a lock:
182 :
183 : fd_struct_t const * field = fd_bank_field_query( bank );
184 : OR
185 : fd_struct field = fd_bank_field_get( bank );
186 :
187 : To modify fields in the bank if a field does not have a lock:
188 :
189 : fd_struct_t * field = fd_bank_field_modify( bank );
190 : OR
191 : fd_bank_field_set( bank, value );
192 :
193 : To access fields in the bank if the field has a lock:
194 :
195 : fd_struct_t const * field = fd_bank_field_locking_query( bank );
196 : ... use field ...
197 : fd_bank_field_locking_end_query( bank );
198 :
199 : To modify fields in the bank if the field has a lock:
200 :
201 : fd_struct_t * field = fd_bank_field_locking_modify( bank );
202 : ... use field ...
203 : fd_bank_field_locking_end_locking_modify( bank ); */
204 :
205 : /* Define additional fields to the bank struct here. If trying to add
206 : a CoW field to the bank, define a pool for it as done below. */
207 :
208 : #define FD_BANKS_ITER(X) \
209 : /* type, name, footprint, align, CoW, limit fork width, has lock */ \
210 12 : X(fd_blockhashes_t, block_hash_queue, sizeof(fd_blockhashes_t), alignof(fd_blockhashes_t), 0, 0, 0 ) /* Block hash queue */ \
211 360 : 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 */ \
212 360 : X(ulong, slot, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Slot */ \
213 360 : X(ulong, parent_slot, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Parent slot */ \
214 360 : X(ulong, capitalization, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Capitalization */ \
215 360 : X(ulong, lamports_per_signature, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Lamports per signature */ \
216 360 : X(ulong, prev_lamports_per_signature, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Previous lamports per signature */ \
217 360 : X(ulong, transaction_count, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Transaction count */ \
218 360 : X(ulong, parent_signature_cnt, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Parent signature count */ \
219 360 : X(ulong, tick_height, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Tick height */ \
220 360 : X(ulong, max_tick_height, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Max tick height */ \
221 360 : X(ulong, hashes_per_tick, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Hashes per tick */ \
222 360 : X(uint128, ns_per_slot, sizeof(uint128), alignof(uint128), 0, 0, 0 ) /* NS per slot */ \
223 360 : X(ulong, ticks_per_slot, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Ticks per slot */ \
224 360 : X(ulong, genesis_creation_time, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Genesis creation time */ \
225 360 : X(double, slots_per_year, sizeof(double), alignof(double), 0, 0, 0 ) /* Slots per year */ \
226 360 : X(fd_inflation_t, inflation, sizeof(fd_inflation_t), alignof(fd_inflation_t), 0, 0, 0 ) /* Inflation */ \
227 360 : X(ulong, total_epoch_stake, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Total epoch stake */ \
228 360 : /* This is only used for the get_epoch_stake syscall. */ \
229 360 : /* If we are executing in epoch E, this is the total */ \
230 360 : /* stake at the end of epoch E-1. */ \
231 360 : X(ulong, block_height, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Block height */ \
232 360 : X(ulong, execution_fees, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Execution fees */ \
233 360 : X(ulong, priority_fees, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Priority fees */ \
234 360 : X(ulong, signature_count, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Signature count */ \
235 360 : X(fd_hash_t, poh, sizeof(fd_hash_t), alignof(fd_hash_t), 0, 0, 0 ) /* PoH */ \
236 360 : 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 */ \
237 360 : X(fd_cluster_version_t, cluster_version, sizeof(fd_cluster_version_t), alignof(fd_cluster_version_t), 0, 0, 0 ) /* Cluster version */ \
238 360 : X(fd_hash_t, bank_hash, sizeof(fd_hash_t), alignof(fd_hash_t), 0, 0, 0 ) /* Bank hash */ \
239 360 : X(fd_hash_t, prev_bank_hash, sizeof(fd_hash_t), alignof(fd_hash_t), 0, 0, 0 ) /* Previous bank hash */ \
240 360 : X(fd_hash_t, genesis_hash, sizeof(fd_hash_t), alignof(fd_hash_t), 0, 0, 0 ) /* Genesis hash */ \
241 360 : X(fd_epoch_schedule_t, epoch_schedule, sizeof(fd_epoch_schedule_t), alignof(fd_epoch_schedule_t), 0, 0, 0 ) /* Epoch schedule */ \
242 360 : X(fd_rent_t, rent, sizeof(fd_rent_t), alignof(fd_rent_t), 0, 0, 0 ) /* Rent */ \
243 360 : X(fd_lthash_value_t, lthash, sizeof(fd_lthash_value_t), alignof(fd_lthash_value_t), 0, 0, 1 ) /* LTHash */ \
244 360 : X(fd_sysvar_cache_t, sysvar_cache, sizeof(fd_sysvar_cache_t), alignof(fd_sysvar_cache_t), 0, 0, 0 ) /* Sysvar cache */ \
245 360 : /* then there can be 100k unique leaders in the worst */ \
246 360 : /* case. We also can assume 432k slots per epoch. */ \
247 360 : X(fd_features_t, features, sizeof(fd_features_t), alignof(fd_features_t), 0, 0, 0 ) /* Features */ \
248 360 : X(ulong, txn_count, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Transaction count */ \
249 360 : X(ulong, nonvote_txn_count, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Nonvote transaction count */ \
250 360 : X(ulong, failed_txn_count, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Failed transaction count */ \
251 360 : X(ulong, nonvote_failed_txn_count, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Nonvote failed transaction count */ \
252 360 : X(ulong, total_compute_units_used, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Total compute units used */ \
253 360 : X(ulong, slots_per_epoch, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Slots per epoch */ \
254 360 : X(ulong, shred_cnt, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Shred count */ \
255 360 : X(ulong, epoch, sizeof(ulong), alignof(ulong), 0, 0, 0 ) /* Epoch */ \
256 360 : X(int, has_identity_vote, sizeof(int), alignof(int), 0, 0, 0 ) /* Has identity vote */ \
257 360 : X(fd_epoch_rewards_t, epoch_rewards, FD_EPOCH_REWARDS_FOOTPRINT, FD_EPOCH_REWARDS_ALIGN, 1, 1, 1 ) /* Epoch rewards */ \
258 360 : 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, */ \
259 360 : 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 */ \
260 30 : /* epoch E is the one that is currently being executed */ \
261 360 : 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 */ \
262 30 : /* epoch E-1 if epoch E is currently being executed */ \
263 360 : 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 */ \
264 : /* epoch E-2 if epoch E is currently being executed */
265 :
266 : /* Invariant Every CoW field must have a rw-lock */
267 : #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
268 : FD_STATIC_ASSERT( (cow == 1 && has_lock == 1 ) || (cow == 0 ), CoW fields must have a rw-lock ); \
269 : FD_STATIC_ASSERT( (cow == 1 && limit_fork_width == 1) || (limit_fork_width == 0), CoW must be 1 if limit_fork_width is 1 );
270 : FD_BANKS_ITER(X)
271 : #undef X
272 :
273 : /* If a member of the bank is CoW then it needs a corresponding pool
274 : which is defined here. If a type if not a CoW then it does not need
275 : to be in a pool and is laid out contigiously in the bank struct. */
276 :
277 : /* Declare a pool object wrapper for all CoW fields. */
278 : #define HAS_COW_1(name, footprint, align) \
279 : static const ulong fd_bank_##name##_align = align; \
280 : static const ulong fd_bank_##name##_footprint = footprint; \
281 : \
282 : struct fd_bank_##name { \
283 : ulong next; \
284 : uchar data[footprint]__attribute__((aligned(align))); \
285 : }; \
286 : typedef struct fd_bank_##name fd_bank_##name##_t;
287 :
288 : /* Do nothing if CoW is not enabled. */
289 : #define HAS_COW_0(name, footprint, align)
290 :
291 : #define X(type, name, footprint, align, cow, limit_fork_width, has_lock) \
292 : HAS_COW_##cow(name, footprint, align)
293 : FD_BANKS_ITER(X)
294 :
295 : struct fd_bank_cost_tracker {
296 : ulong next;
297 : uchar data[FD_COST_TRACKER_FOOTPRINT] __attribute__((aligned(FD_COST_TRACKER_ALIGN)));
298 : };
299 : typedef struct fd_bank_cost_tracker fd_bank_cost_tracker_t;
300 :
301 : #undef X
302 : #undef HAS_COW_0
303 : #undef HAS_COW_1
304 :
305 : #define POOL_NAME fd_bank_epoch_leaders_pool
306 183 : #define POOL_T fd_bank_epoch_leaders_t
307 : #include "../../util/tmpl/fd_pool.c"
308 :
309 : #define POOL_NAME fd_bank_epoch_rewards_pool
310 162 : #define POOL_T fd_bank_epoch_rewards_t
311 : #include "../../util/tmpl/fd_pool.c"
312 :
313 : #define POOL_NAME fd_bank_vote_states_pool
314 177 : #define POOL_T fd_bank_vote_states_t
315 : #include "../../util/tmpl/fd_pool.c"
316 :
317 : #define POOL_NAME fd_bank_vote_states_prev_pool
318 192 : #define POOL_T fd_bank_vote_states_prev_t
319 : #include "../../util/tmpl/fd_pool.c"
320 :
321 : #define POOL_NAME fd_bank_vote_states_prev_prev_pool
322 162 : #define POOL_T fd_bank_vote_states_prev_prev_t
323 : #include "../../util/tmpl/fd_pool.c"
324 :
325 : #define POOL_NAME fd_bank_cost_tracker_pool
326 396 : #define POOL_T fd_bank_cost_tracker_t
327 : #include "../../util/tmpl/fd_pool.c"
328 :
329 198 : #define FD_BANK_FLAGS_INIT (0x00000001UL) /* Initialized. Not yet replayable. */
330 66 : #define FD_BANK_FLAGS_REPLAYABLE (0x00000002UL) /* Replayable. */
331 69 : #define FD_BANK_FLAGS_FROZEN (0x00000004UL) /* Frozen. We finished replaying or because it was a snapshot/genesis loaded bank. */
332 42 : #define FD_BANK_FLAGS_DEAD (0x00000008UL) /* Dead. We stopped replaying it before we could finish it (e.g. invalid block or pruned minority fork). */
333 42 : #define FD_BANK_FLAGS_ROOTED (0x00000010UL) /* Rooted. Part of the consnensus root fork. */
334 0 : #define FD_BANK_FLAGS_EXEC_RECORDING (0x00000100UL) /* Enable execution recording. */
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 663 : fd_banks_get_##name##_pool( fd_banks_t * banks ) { \
711 663 : return fd_bank_##name##_pool_join( (uchar *)banks + banks->name##_pool_offset ); \
712 663 : } \
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 :
795 : /* fd_banks_join() joins a new fd_banks_t struct. */
796 :
797 : fd_banks_t *
798 : fd_banks_join( void * mem );
799 :
800 : /* fd_banks_leave() leaves a bank. */
801 :
802 : void *
803 : fd_banks_leave( fd_banks_t * banks );
804 :
805 : /* fd_banks_delete() deletes a bank. */
806 :
807 : void *
808 : fd_banks_delete( void * shmem );
809 :
810 : /* fd_banks_init_bank() initializes a new bank in the bank manager.
811 : This should only be used during bootup. This returns an initial
812 : fd_bank_t with the corresponding bank index set to 0. */
813 :
814 : fd_bank_t *
815 : fd_banks_init_bank( fd_banks_t * banks );
816 :
817 : /* fd_banks_get_bank_idx returns a bank for a given bank index. */
818 :
819 : static inline fd_bank_t *
820 : fd_banks_bank_mem_query( fd_banks_t * banks,
821 0 : ulong bank_idx ) {
822 0 : return fd_banks_pool_ele( fd_banks_get_bank_pool( banks ), bank_idx );
823 0 : }
824 :
825 : static inline fd_bank_t *
826 : fd_banks_bank_query( fd_banks_t * banks,
827 126 : ulong bank_idx ) {
828 126 : fd_bank_t * bank = fd_banks_pool_ele( fd_banks_get_bank_pool( banks ), bank_idx );
829 126 : return (bank->flags&FD_BANK_FLAGS_INIT) ? bank : NULL;
830 126 : }
831 :
832 : static inline fd_bank_t *
833 : fd_banks_get_parent( fd_banks_t * banks,
834 0 : fd_bank_t * bank ) {
835 0 : return fd_banks_pool_ele( fd_banks_get_bank_pool( banks ), bank->parent_idx );
836 0 : }
837 :
838 : /* fd_banks_clone_from_parent() clones a bank from a parent bank.
839 : This function links the child bank to its parent bank and copies
840 : over the data from the parent bank to the child. This function
841 : assumes that the child and parent banks both have been allocated.
842 : The parent bank must be frozen and the child bank must be initialized
843 : but not yet used.
844 :
845 : A more detailed note: not all of the data is copied over and this
846 : is a shallow clone. All of the CoW fields are not copied over and
847 : will only be done so if the caller explicitly calls
848 : fd_bank_{*}_modify(). This naming was chosen to emulate the
849 : semantics of the Agave client. */
850 :
851 : fd_bank_t *
852 : fd_banks_clone_from_parent( fd_banks_t * banks,
853 : ulong bank_idx,
854 : ulong parent_bank_idx );
855 :
856 : /* fd_banks_advance_root() advances the root bank to the bank manager.
857 : This should only be used when a bank is no longer needed and has no
858 : active refcnts. This will prune off the bank from the bank manager.
859 : It returns the new root bank. An invariant of this function is that
860 : the new root bank should be a child of the current root bank.
861 :
862 : All banks that are ancestors or siblings of the new root bank will be
863 : cancelled and their resources will be released back to the pool. */
864 :
865 : fd_bank_t const *
866 : fd_banks_advance_root( fd_banks_t * banks,
867 : ulong bank_idx );
868 :
869 : /* fd_bank_clear_bank() clears the contents of a bank. This should ONLY
870 : be used with banks that have no children and should only be used in
871 : testing and fuzzing.
872 :
873 : This function will memset all non-CoW fields to 0.
874 :
875 : For all CoW fields, we will reset the indices to its parent. */
876 :
877 : void
878 : fd_banks_clear_bank( fd_banks_t * banks,
879 : fd_bank_t * bank );
880 :
881 : /* fd_banks_advance_root_prepare returns the highest block that can be
882 : safely advanced between the current root of the fork tree and the
883 : target block. See the note on safe publishing for more details. In
884 : general, a node in the fork tree can be pruned if:
885 : (1) the node itself can be pruned, and
886 : (2) all subtrees (except for the one on the rooted fork) forking off
887 : of the node can be pruned.
888 : The highest publishable block is the highest block on the rooted fork
889 : where the above is true, or the rooted child block of such if there
890 : is one.
891 :
892 : This function assumes that the given target block has been rooted by
893 : consensus. It will mark every block on the rooted fork as rooted, up
894 : to the given target block. It will also mark minority forks as dead.
895 :
896 : Highest advanceable block is written to the out pointer. Returns 1
897 : if the advanceable block can be advanced beyond the current root.
898 : Returns 0 if no such block can be found. We will ONLY advance our
899 : advanceable_bank_idx to a child of the current root. In order to
900 : advance to the target bank, fd_banks_advance_root_prepare() must be
901 : called repeatedly. */
902 :
903 : int
904 : fd_banks_advance_root_prepare( fd_banks_t * banks,
905 : ulong target_bank_idx,
906 : ulong * advanceable_bank_idx_out );
907 :
908 : /* fd_banks_mark_bank_dead marks the current bank (and all of its
909 : descendants) as dead. The caller is still responsible for handling
910 : the behavior of the dead bank correctly. */
911 :
912 : void
913 : fd_banks_mark_bank_dead( fd_banks_t * banks,
914 : fd_bank_t * bank );
915 :
916 : /* fd_banks_mark_bank_frozen marks the current bank as frozen. This
917 : should be done when the bank is no longer being updated: it should be
918 : done at the end of a slot. This also releases the memory for the
919 : cost tracker which only has to be persisted from the start of a slot
920 : to the end. */
921 :
922 : void
923 : fd_banks_mark_bank_frozen( fd_banks_t * banks,
924 : fd_bank_t * bank );
925 :
926 : /* fd_banks_new_bank reserves a bank index for a new bank. New bank
927 : indicies should always be available. After this function is called,
928 : the bank will be linked to its parent bank, but not yet replayable.
929 : After a call to fd_banks_clone_from_parent, the bank will be
930 : replayable. This assumes that there is a parent bank which exists
931 : and the there are available bank indices in the bank pool. */
932 :
933 : fd_bank_t *
934 : fd_banks_new_bank( fd_banks_t * banks,
935 : ulong parent_bank_idx,
936 : long now );
937 :
938 :
939 : /* fd_banks_is_full returns 1 if the banks are full, 0 otherwise. */
940 :
941 : static inline int
942 0 : fd_banks_is_full( fd_banks_t * banks ) {
943 0 : return fd_banks_pool_free( fd_banks_get_bank_pool( banks ) )==0UL;
944 0 : }
945 :
946 : /* fd_banks_validate does validation on the banks struct to make sure
947 : that there are no corruptions/invariant violations. It returns 0
948 : if no issues have been detected and 1 otherwise.
949 :
950 : List of checks that the function currently performs:
951 : 1. CoW fields have not acquired more elements than the max amount of
952 : allocated banks.
953 : (Add more checks as needed here)
954 : */
955 :
956 : int
957 : fd_banks_validate( fd_banks_t * banks );
958 :
959 : FD_PROTOTYPES_END
960 :
961 : #endif /* HEADER_fd_src_flamenco_runtime_fd_bank_h */
|