Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_stakes_fd_stakes_h
2 : #define HEADER_fd_src_flamenco_stakes_fd_stakes_h
3 :
4 : #include "fd_stake_delegations.h"
5 : #include "fd_stake_types.h"
6 : #include "fd_stake_weight.h"
7 : #include "fd_vote_stakes.h"
8 : #include "../types/fd_cast.h"
9 :
10 : /* The warmup cooldown rate can only be one of two values: 0.25 or 0.09. */
11 7271 : #define FD_STAKE_WARMUP_COOLDOWN_RATE_ENUM_025 (0)
12 14542 : #define FD_STAKE_WARMUP_COOLDOWN_RATE_ENUM_009 (1)
13 0 : #define FD_STAKE_WARMUP_COOLDOWN_RATE_025 (0.25)
14 7271 : #define FD_STAKE_WARMUP_COOLDOWN_RATE_009 (0.09)
15 :
16 : FD_PROTOTYPES_BEGIN
17 :
18 : fd_stake_state_t const *
19 : fd_stakes_get_state( fd_acc_t const * acc );
20 :
21 : fd_stake_history_entry_t
22 : stake_activating_and_deactivating( fd_delegation_t const * self,
23 : ulong target_epoch,
24 : fd_stake_history_t const * stake_history,
25 : ulong * new_rate_activation_epoch );
26 :
27 : /* fd_stake_warmup_cooldown_rate gives the warmup/cooldown rate enum
28 : for a given epoch. In Agave, the per-delegation warmup_cooldown_rate
29 : field was deprecated (since v1.16.7) and unused in calculations.
30 : The rate is always determined by the epoch. */
31 :
32 : static inline uchar
33 7271 : fd_stake_warmup_cooldown_rate( ulong current_epoch, ulong * new_rate_activation_epoch ) {
34 7271 : ulong activation_epoch = new_rate_activation_epoch ? *new_rate_activation_epoch : ULONG_MAX;
35 7271 : return current_epoch<activation_epoch
36 7271 : ? (uchar)FD_STAKE_WARMUP_COOLDOWN_RATE_ENUM_025
37 7271 : : (uchar)FD_STAKE_WARMUP_COOLDOWN_RATE_ENUM_009;
38 7271 : }
39 :
40 : static inline double
41 7271 : fd_stake_warmup_cooldown_rate_to_double( uchar warmup_cooldown_rate ) {
42 7271 : return warmup_cooldown_rate==FD_STAKE_WARMUP_COOLDOWN_RATE_ENUM_025 ? FD_STAKE_WARMUP_COOLDOWN_RATE_025 : FD_STAKE_WARMUP_COOLDOWN_RATE_009;
43 7271 : }
44 :
45 : /* Caller must ensure cluster_portion is nonzero. */
46 :
47 : static inline ulong
48 : fd_stake_calculate_change_allowance_float( ulong current_epoch,
49 : ulong account_portion,
50 : ulong cluster_portion,
51 : ulong cluster_effective,
52 7273 : ulong * new_rate_activation_epoch ) {
53 7273 : double weight = (double)account_portion / (double)cluster_portion;
54 7273 : double warmup_cooldown_rate = fd_stake_warmup_cooldown_rate_to_double( fd_stake_warmup_cooldown_rate( current_epoch, new_rate_activation_epoch ) );
55 7273 : double newly_changed_cluster_stake = (double)cluster_effective * warmup_cooldown_rate;
56 7273 : return fd_rust_cast_double_to_ulong( weight * newly_changed_cluster_stake );
57 7273 : }
58 :
59 : ulong
60 : fd_stake_calculate_activation_allowance( ulong current_epoch,
61 : ulong account_activating_stake,
62 : fd_stake_history_entry_t const * prev_epoch_cluster_state,
63 : ulong * opt_rate_change_activation_epoch );
64 :
65 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/stake_delegation.rs#L27-L41 */
66 : fd_stake_history_entry_t
67 : fd_delegation_activation_status( fd_delegation_t const * delegation,
68 : ulong target_epoch,
69 : fd_stake_history_t const * stake_history,
70 : ulong * new_rate_activation_epoch,
71 : int use_fixed_point_stake_math );
72 :
73 : int
74 : fd_delegation_is_inactive( fd_delegation_t const * delegation,
75 : ulong target_epoch,
76 : fd_stake_history_t const * stake_history,
77 : ulong * new_rate_activation_epoch,
78 : int use_fixed_point_stake_math );
79 :
80 : fd_stake_history_entry_t
81 : fd_stake_delegation_activation_status( fd_stake_delegation_t const * delegation,
82 : ulong target_epoch,
83 : fd_stake_history_t const * stake_history,
84 : ulong * new_rate_activation_epoch,
85 : int use_fixed_point_stake_math );
86 :
87 : int
88 : fd_stake_delegation_is_inactive( fd_stake_delegation_t const * delegation,
89 : ulong target_epoch,
90 : fd_stake_history_t const * stake_history,
91 : ulong * new_rate_activation_epoch,
92 : int use_fixed_point_stake_math );
93 :
94 : /* fd_stake_weights_by_node converts Stakes (unordered list of (vote
95 : acc, active stake) tuples) to an ordered list of (stake, vote pubkey, node
96 : identity) sorted by (stake descending, vote pubkey descending).
97 :
98 : iter_kind selects the vote stakes set: FD_VOTE_STAKES_ITER_T_1 (next
99 : epoch), T_2 (current epoch) or T_3 (previous epoch).
100 :
101 : weights points to an array suitable to hold ...
102 :
103 : fd_vote_accounts_pair_t_map_size( accs->vote_accounts_pool,
104 : accs->vote_accounts_root )
105 :
106 : ... items. On return, weights be an ordered list.
107 :
108 : Returns the number of items in weights (which is <= no of vote accs). */
109 :
110 : ulong
111 : fd_stake_weights_by_node( fd_vote_stakes_t * vote_stakes,
112 : ulong fork_id,
113 : int iter_kind,
114 : fd_vote_stake_weight_t * weights );
115 :
116 : void
117 : fd_stakes_activate_epoch( fd_bank_t * bank,
118 : fd_runtime_stack_t * runtime_stack,
119 : fd_accdb_t * accdb,
120 : fd_capture_ctx_t * capture_ctx,
121 : fd_stake_delegations_t * stake_delegations,
122 : ulong * new_rate_activation_epoch );
123 :
124 : /* fd_stakes_vote_account_is_admissible returns 1 if a vote account
125 : with the given state passes the validator admission ticket filter
126 : applied at the epoch boundary */
127 :
128 : int
129 : fd_stakes_vote_account_is_admissible( fd_bank_t const * bank,
130 : ulong lamports,
131 : uchar const * owner,
132 : uchar const * data,
133 : ulong data_len );
134 :
135 : /* rewarded_epoch selects the epoch whose effective delegated stakes
136 : should be accumulated for rewards. ULONG_MAX disables this work. */
137 : void
138 : fd_refresh_vote_accounts( fd_bank_t * bank,
139 : fd_accdb_t * accdb,
140 : fd_runtime_stack_t * runtime_stack,
141 : fd_stake_delegations_t const * stake_delegations,
142 : fd_stake_history_t const * history,
143 : ulong rewarded_epoch,
144 : ulong * new_rate_activation_epoch );
145 :
146 : /* fd_stakes_update_delegation is used to maintain the in-memory cache
147 : of the stake delegations that is used at the epoch boundary. Entries
148 : in the cache will be inserted/updated/removed based on the state of
149 : the stake account. */
150 :
151 : void
152 : fd_stakes_update_stake_delegation( fd_pubkey_t const * pubkey,
153 : fd_acc_t const * acc,
154 : fd_bank_t * bank,
155 : fd_txn_in_t const * txn_in );
156 :
157 : FD_PROTOTYPES_END
158 :
159 : #endif /* HEADER_fd_src_flamenco_stakes_fd_stakes_h */
|