Line data Source code
1 : #include "fd_stakes.h"
2 : #include "../runtime/fd_bank.h"
3 : #include "../runtime/program/fd_stake_program.h"
4 : #include "../runtime/program/vote/fd_vote_state_versioned.h"
5 : #include "../runtime/sysvar/fd_sysvar_stake_history.h"
6 : #include "fd_stake_delegations.h"
7 : #include "../accdb/fd_accdb_impl_v1.h"
8 :
9 : ulong
10 : fd_stake_weights_by_node( fd_vote_states_t const * vote_states,
11 12 : fd_vote_stake_weight_t * weights ) {
12 :
13 12 : ulong weights_cnt = 0;
14 12 : fd_vote_states_iter_t iter_[1];
15 12 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states ); !fd_vote_states_iter_done( iter ); fd_vote_states_iter_next( iter ) ) {
16 0 : fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
17 0 : if( FD_UNLIKELY( !vote_state->stake ) ) continue;
18 :
19 0 : fd_memcpy( weights[ weights_cnt ].vote_key.uc, &vote_state->vote_account, sizeof(fd_pubkey_t) );
20 0 : fd_memcpy( weights[ weights_cnt ].id_key.uc, &vote_state->node_account, sizeof(fd_pubkey_t) );
21 0 : weights[ weights_cnt ].stake = vote_state->stake;
22 0 : weights_cnt++;
23 0 : }
24 12 : sort_vote_weights_by_stake_vote_inplace( weights, weights_cnt );
25 12 : return weights_cnt;
26 12 : }
27 :
28 : /* We need to update the amount of stake that each vote account has for
29 : the given epoch. This can only be done after the stake history
30 : sysvar has been updated. We also cache the stakes for each of the
31 : vote accounts for the previous epoch.
32 :
33 : https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/stakes.rs#L471 */
34 : void
35 : fd_refresh_vote_accounts( fd_bank_t * bank,
36 : fd_stake_delegations_t const * stake_delegations,
37 : fd_stake_history_t const * history,
38 12 : ulong * new_rate_activation_epoch ) {
39 :
40 12 : ulong epoch = fd_bank_epoch_get( bank );
41 :
42 12 : ulong total_stake = 0UL;
43 :
44 12 : fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
45 12 : if( FD_UNLIKELY( !vote_states ) ) {
46 0 : FD_LOG_CRIT(( "vote_states is NULL" ));
47 0 : }
48 :
49 : /* Reset the vote stakes so we can re-compute them based on the most
50 : current stake delegation values. */
51 12 : fd_vote_states_reset_stakes( vote_states );
52 :
53 12 : fd_stake_delegations_iter_t iter_[1];
54 12 : for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
55 12 : !fd_stake_delegations_iter_done( iter );
56 12 : fd_stake_delegations_iter_next( iter ) ) {
57 0 : fd_stake_delegation_t const * stake_delegation = fd_stake_delegations_iter_ele( iter );
58 :
59 0 : fd_delegation_t delegation = {
60 0 : .voter_pubkey = stake_delegation->vote_account,
61 0 : .stake = stake_delegation->stake,
62 0 : .deactivation_epoch = stake_delegation->deactivation_epoch,
63 0 : .activation_epoch = stake_delegation->activation_epoch,
64 0 : .warmup_cooldown_rate = stake_delegation->warmup_cooldown_rate,
65 0 : };
66 :
67 0 : fd_stake_history_entry_t new_entry = fd_stake_activating_and_deactivating(
68 0 : &delegation,
69 0 : epoch,
70 0 : history,
71 0 : new_rate_activation_epoch );
72 :
73 0 : fd_vote_state_ele_t * vote_state = fd_vote_states_query( vote_states, &stake_delegation->vote_account );
74 0 : if( FD_LIKELY( vote_state ) ) {
75 0 : total_stake += new_entry.effective;
76 0 : vote_state->stake += new_entry.effective;
77 0 : }
78 0 : }
79 :
80 12 : fd_bank_total_epoch_stake_set( bank, total_stake );
81 :
82 12 : fd_bank_vote_states_end_locking_modify( bank );
83 12 : }
84 :
85 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/stakes.rs#L280 */
86 : void
87 : fd_stakes_activate_epoch( fd_bank_t * bank,
88 : fd_accdb_user_t * accdb,
89 : fd_funk_txn_xid_t const * xid,
90 : fd_capture_ctx_t * capture_ctx,
91 : fd_stake_delegations_t const * stake_delegations,
92 12 : ulong * new_rate_activation_epoch ) {
93 :
94 : /* First, we need to accumulate the stats for the current amount of
95 : effective, activating, and deactivating stake for the current
96 : epoch. Once this is computed, we can add update our stake history
97 : sysvar. Afterward, we can refresh the stake values for the vote
98 : accounts for the new epoch. */
99 :
100 12 : fd_stake_history_t stake_history[1];
101 12 : if( FD_UNLIKELY( !fd_sysvar_stake_history_read( accdb, xid, stake_history ) ) ) {
102 0 : FD_LOG_ERR(( "StakeHistory sysvar is missing from sysvar cache" ));
103 0 : }
104 :
105 12 : fd_epoch_stake_history_entry_pair_t new_elem = {
106 12 : .epoch = fd_bank_epoch_get( bank ),
107 12 : .entry = {
108 12 : .effective = 0UL,
109 12 : .activating = 0UL,
110 12 : .deactivating = 0UL
111 12 : }
112 12 : };
113 :
114 12 : fd_stake_delegations_iter_t iter_[1];
115 12 : for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
116 12 : !fd_stake_delegations_iter_done( iter );
117 12 : fd_stake_delegations_iter_next( iter ) ) {
118 0 : fd_stake_delegation_t const * stake_delegation = fd_stake_delegations_iter_ele( iter );
119 :
120 0 : fd_delegation_t delegation = {
121 0 : .voter_pubkey = stake_delegation->vote_account,
122 0 : .stake = stake_delegation->stake,
123 0 : .activation_epoch = stake_delegation->activation_epoch,
124 0 : .deactivation_epoch = stake_delegation->deactivation_epoch,
125 0 : .warmup_cooldown_rate = stake_delegation->warmup_cooldown_rate,
126 0 : };
127 :
128 0 : fd_stake_history_entry_t new_entry = fd_stake_activating_and_deactivating(
129 0 : &delegation,
130 0 : fd_bank_epoch_get( bank ),
131 0 : stake_history,
132 0 : new_rate_activation_epoch );
133 0 : new_elem.entry.effective += new_entry.effective;
134 0 : new_elem.entry.activating += new_entry.activating;
135 0 : new_elem.entry.deactivating += new_entry.deactivating;
136 0 : }
137 :
138 12 : fd_sysvar_stake_history_update( bank, accdb, xid, capture_ctx, &new_elem );
139 :
140 12 : if( FD_UNLIKELY( !fd_sysvar_stake_history_read( accdb, xid, stake_history ) ) ) {
141 0 : FD_LOG_ERR(( "StakeHistory sysvar is missing from sysvar cache" ));
142 0 : }
143 :
144 : /* Now increment the epoch and recompute the stakes for the vote
145 : accounts for the new epoch value. */
146 :
147 12 : fd_bank_epoch_set( bank, fd_bank_epoch_get( bank ) + 1UL );
148 :
149 12 : fd_refresh_vote_accounts( bank,
150 12 : stake_delegations,
151 12 : stake_history,
152 12 : new_rate_activation_epoch );
153 :
154 12 : }
155 :
156 : void
157 : fd_stakes_update_stake_delegation( fd_pubkey_t const * pubkey,
158 : fd_account_meta_t const * meta,
159 0 : fd_bank_t * bank ) {
160 :
161 0 : fd_stake_delegations_t * stake_delegations_delta = fd_bank_stake_delegations_delta_locking_modify( bank );
162 0 : if( FD_UNLIKELY( !stake_delegations_delta ) ) {
163 0 : FD_LOG_CRIT(( "unable to retrieve join to stake delegation delta" ));
164 0 : }
165 :
166 0 : if( meta->lamports==0UL ) {
167 0 : fd_stake_delegations_remove( stake_delegations_delta, pubkey );
168 0 : fd_bank_stake_delegations_delta_end_locking_modify( bank );
169 0 : return;
170 0 : }
171 :
172 0 : fd_stake_state_v2_t stake_state;
173 0 : int err = fd_stake_get_state( meta, &stake_state );
174 0 : if( FD_UNLIKELY( err!=0 ) ) {
175 0 : fd_stake_delegations_remove( stake_delegations_delta, pubkey );
176 0 : fd_bank_stake_delegations_delta_end_locking_modify( bank );
177 0 : return;
178 0 : }
179 :
180 0 : if( FD_UNLIKELY( !fd_stake_state_v2_is_stake( &stake_state ) ) ) {
181 0 : fd_stake_delegations_remove( stake_delegations_delta, pubkey );
182 0 : fd_bank_stake_delegations_delta_end_locking_modify( bank );
183 0 : return;
184 0 : }
185 :
186 0 : if( FD_UNLIKELY( fd_stake_state_v2_is_uninitialized( &stake_state ) ) ) {
187 0 : fd_stake_delegations_remove( stake_delegations_delta, pubkey );
188 0 : fd_bank_stake_delegations_delta_end_locking_modify( bank );
189 0 : return;
190 0 : }
191 :
192 0 : if( FD_UNLIKELY( stake_state.inner.stake.stake.delegation.stake==0UL ) ) {
193 0 : fd_stake_delegations_remove( stake_delegations_delta, pubkey );
194 0 : fd_bank_stake_delegations_delta_end_locking_modify( bank );
195 0 : return;
196 0 : }
197 :
198 0 : fd_stake_delegations_update( stake_delegations_delta,
199 0 : pubkey,
200 0 : &stake_state.inner.stake.stake.delegation.voter_pubkey,
201 0 : stake_state.inner.stake.stake.delegation.stake,
202 0 : stake_state.inner.stake.stake.delegation.activation_epoch,
203 0 : stake_state.inner.stake.stake.delegation.deactivation_epoch,
204 0 : stake_state.inner.stake.stake.credits_observed,
205 0 : stake_state.inner.stake.stake.delegation.warmup_cooldown_rate );
206 :
207 0 : fd_bank_stake_delegations_delta_end_locking_modify( bank );
208 0 : }
209 :
210 : void
211 : fd_stakes_update_vote_state( fd_pubkey_t const * pubkey,
212 : fd_account_meta_t const * meta,
213 0 : fd_bank_t * bank ) {
214 :
215 0 : fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
216 :
217 0 : if( meta->lamports==0UL ) {
218 0 : fd_vote_states_remove( vote_states, pubkey );
219 0 : fd_bank_vote_states_end_locking_modify( bank );
220 0 : return;
221 0 : }
222 :
223 0 : if( !fd_vsv_is_correct_size_and_initialized( meta ) ) {
224 0 : fd_vote_states_remove( vote_states, pubkey );
225 0 : fd_bank_vote_states_end_locking_modify( bank );
226 0 : return;
227 0 : }
228 :
229 0 : fd_vote_states_update_from_account( vote_states,
230 0 : pubkey,
231 0 : fd_account_data( meta ),
232 0 : meta->dlen );
233 0 : fd_bank_vote_states_end_locking_modify( bank );
234 0 : }
|