Line data Source code
1 : #include "fd_vote_program.h"
2 : #include "../fd_borrowed_account.h"
3 : #include "../fd_executor.h"
4 : #include "../fd_pubkey_utils.h"
5 : #include "../sysvar/fd_sysvar_rent.h"
6 : #include "../sysvar/fd_sysvar.h"
7 :
8 : #include <limits.h>
9 : #include <math.h>
10 : #include <stdio.h>
11 : #include <string.h>
12 :
13 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L35
14 0 : #define MAX_LOCKOUT_HISTORY 31UL
15 :
16 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L36
17 0 : #define INITIAL_LOCKOUT 2UL
18 :
19 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L36
20 : #define MAX_EPOCH_CREDITS_HISTORY 64UL
21 :
22 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L42
23 0 : #define DEFAULT_PRIOR_VOTERS_OFFSET 114
24 :
25 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L45
26 0 : #define VOTE_CREDITS_GRACE_SLOTS 2
27 :
28 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L48
29 0 : #define VOTE_CREDITS_MAXIMUM_PER_SLOT 16
30 :
31 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L51
32 : #define VOTE_CREDITS_MAXIMUM_PER_SLOT_OLD 8
33 :
34 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/clock.rs#L147
35 : #define SLOT_DEFAULT 0UL
36 :
37 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/clock.rs#L147
38 : #define SLOT_MAX ULONG_MAX
39 :
40 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L886
41 0 : #define VERSION_OFFSET (4UL)
42 :
43 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L887
44 : #define DEFAULT_PRIOR_VOTERS_END (118)
45 :
46 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_1_14_11.rs#L6
47 0 : #define DEFAULT_PRIOR_VOTERS_OFFSET_1_14_11 (82UL)
48 :
49 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_1_14_11.rs#L60
50 : #define DEFAULT_PRIOR_VOTERS_END_1_14_11 (86UL)
51 :
52 : #define ACCOUNTS_MAX 4 /* Vote instructions take in at most 4 accounts */
53 :
54 : #define DEFAULT_COMPUTE_UNITS 2100UL
55 :
56 : /**********************************************************************/
57 : /* size_of */
58 : /**********************************************************************/
59 :
60 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L82
61 : static inline ulong
62 0 : size_of_versioned( int is_current ) {
63 0 : return fd_ulong_if( is_current, FD_VOTE_STATE_V3_SZ, FD_VOTE_STATE_V2_SZ );
64 0 : }
65 :
66 : /**********************************************************************/
67 : /* impl Lockout */
68 : /**********************************************************************/
69 :
70 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L104
71 : static inline ulong
72 0 : lockout( fd_vote_lockout_t * self ) {
73 : /* Confirmation count can never be greater than MAX_LOCKOUT_HISTORY, preventing overflow.
74 : Although Agave does not consider overflow, we do for fuzzing conformance. */
75 0 : ulong confirmation_count = fd_ulong_min( self->confirmation_count, MAX_LOCKOUT_HISTORY );
76 0 : return 1UL<<confirmation_count;
77 0 : }
78 :
79 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L110
80 : static inline ulong
81 0 : last_locked_out_slot( fd_vote_lockout_t * self ) {
82 0 : return fd_ulong_sat_add( self->slot, lockout( self ) );
83 0 : }
84 :
85 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L114
86 : static inline ulong
87 0 : is_locked_out_at_slot( fd_vote_lockout_t * self, ulong slot ) {
88 0 : return last_locked_out_slot( self ) >= slot;
89 0 : }
90 :
91 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L122
92 : static void
93 0 : increase_confirmation_count( fd_vote_lockout_t * self, uint by ) {
94 0 : self->confirmation_count = fd_uint_sat_add( self->confirmation_count, by );
95 0 : }
96 :
97 : /**********************************************************************/
98 : /* impl From<VoteState> for VoteState1_14_11 */
99 : /**********************************************************************/
100 :
101 : /* from_vote_state_1_14_11 converts a "current" vote state object into
102 : the older "v1.14.11" version. This destroys the "current" object in
103 : the process. spad is the bump allocator to be used, which must be
104 : the same as the one used for v1.14.11.
105 : */
106 :
107 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_1_14_11.rs#L67
108 : static void
109 : from_vote_state_1_14_11( fd_vote_state_t * vote_state,
110 : fd_vote_state_1_14_11_t * vote_state_1_14_11, /* out */
111 0 : fd_spad_t * spad ) {
112 0 : vote_state_1_14_11->node_pubkey = vote_state->node_pubkey; /* copy */
113 0 : vote_state_1_14_11->authorized_withdrawer = vote_state->authorized_withdrawer; /* copy */
114 0 : vote_state_1_14_11->commission = vote_state->commission; /* copy */
115 :
116 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_1_14_11.rs#L72
117 0 : if( vote_state->votes ) {
118 0 : uchar * deque_mem = fd_spad_alloc( spad,
119 0 : deq_fd_vote_lockout_t_align(),
120 0 : deq_fd_vote_lockout_t_footprint( deq_fd_landed_vote_t_cnt( vote_state->votes ) ) );
121 0 : vote_state_1_14_11->votes = deq_fd_vote_lockout_t_join(
122 0 : deq_fd_vote_lockout_t_new( deque_mem, deq_fd_landed_vote_t_cnt( vote_state->votes ) ) );
123 0 : for( deq_fd_landed_vote_t_iter_t iter = deq_fd_landed_vote_t_iter_init( vote_state->votes );
124 0 : !deq_fd_landed_vote_t_iter_done( vote_state->votes, iter );
125 0 : iter = deq_fd_landed_vote_t_iter_next( vote_state->votes, iter ) ) {
126 0 : fd_landed_vote_t const * landed_vote = deq_fd_landed_vote_t_iter_ele_const( vote_state->votes, iter );
127 0 : deq_fd_vote_lockout_t_push_tail_wrap( vote_state_1_14_11->votes, landed_vote->lockout );
128 0 : }
129 0 : }
130 :
131 0 : vote_state_1_14_11->has_root_slot = vote_state->has_root_slot; /* copy */
132 0 : vote_state_1_14_11->root_slot = vote_state->root_slot; /* copy */
133 0 : vote_state_1_14_11->authorized_voters = vote_state->authorized_voters; /* move */
134 0 : vote_state_1_14_11->prior_voters = vote_state->prior_voters; /* deep copy */
135 0 : vote_state_1_14_11->epoch_credits = vote_state->epoch_credits; /* move */
136 0 : vote_state_1_14_11->last_timestamp = vote_state->last_timestamp; /* deep copy */
137 :
138 : /* Clear moved objects */
139 0 : vote_state->authorized_voters.treap = NULL;
140 0 : vote_state->authorized_voters.pool = NULL;
141 0 : vote_state->epoch_credits = NULL;
142 :
143 0 : }
144 :
145 : /**********************************************************************/
146 : /* impl VoteAccount */
147 : /**********************************************************************/
148 :
149 : /* https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1074 */
150 : static fd_vote_state_versioned_t *
151 : get_state( fd_txn_account_t const * self,
152 : fd_spad_t * spad,
153 0 : int * err ) {
154 0 : int decode_err;
155 0 : fd_vote_state_versioned_t * res = fd_bincode_decode_spad(
156 0 : vote_state_versioned, spad,
157 0 : fd_txn_account_get_data( self ),
158 0 : fd_txn_account_get_data_len( self ),
159 0 : &decode_err );
160 0 : if( FD_UNLIKELY( decode_err ) ) {
161 0 : *err = FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
162 0 : return NULL;
163 0 : }
164 0 : *err = FD_EXECUTOR_INSTR_SUCCESS;
165 0 : return res;
166 0 : }
167 :
168 : static int
169 : set_state( fd_borrowed_account_t * self,
170 0 : fd_vote_state_versioned_t * state ) {
171 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L974 */
172 0 : uchar * data = NULL;
173 0 : ulong dlen = 0UL;
174 0 : int err = fd_borrowed_account_get_data_mut( self, &data, &dlen );
175 0 : if( FD_UNLIKELY( err ) ) {
176 0 : return err;
177 0 : }
178 :
179 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/src/transaction_context.rs#L978
180 0 : ulong serialized_size = fd_vote_state_versioned_size( state );
181 0 : if( FD_UNLIKELY( serialized_size > dlen ) )
182 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
183 :
184 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/src/transaction_context.rs#L983
185 0 : fd_bincode_encode_ctx_t encode =
186 0 : { .data = data,
187 0 : .dataend = data + dlen };
188 0 : do {
189 0 : int err = fd_vote_state_versioned_encode( state, &encode );
190 0 : if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "fd_vote_state_versioned_encode failed (%d)", err ));
191 0 : } while(0);
192 :
193 0 : return FD_EXECUTOR_INSTR_SUCCESS;
194 0 : }
195 :
196 : /**********************************************************************/
197 : /* impl AuthorizedVoters */
198 : /**********************************************************************/
199 :
200 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L17
201 : static void
202 : authorized_voters_new( ulong epoch,
203 : fd_pubkey_t const * pubkey,
204 : fd_spad_t * spad,
205 0 : fd_vote_authorized_voters_t * authorized_voters /* out */ ) {
206 0 : uchar * pool_mem = fd_spad_alloc( spad,
207 0 : fd_vote_authorized_voters_pool_align(),
208 0 : fd_vote_authorized_voters_pool_footprint( FD_VOTE_AUTHORIZED_VOTERS_MIN ) );
209 0 : authorized_voters->pool = fd_vote_authorized_voters_pool_join(
210 0 : fd_vote_authorized_voters_pool_new( pool_mem, FD_VOTE_AUTHORIZED_VOTERS_MIN ) );
211 :
212 0 : uchar * treap_mem = fd_spad_alloc( spad,
213 0 : fd_vote_authorized_voters_treap_align(),
214 0 : fd_vote_authorized_voters_treap_footprint( FD_VOTE_AUTHORIZED_VOTERS_MIN ) );
215 0 : authorized_voters->treap = fd_vote_authorized_voters_treap_join(
216 0 : fd_vote_authorized_voters_treap_new( treap_mem, FD_VOTE_AUTHORIZED_VOTERS_MIN ) );
217 0 : if( 0 == fd_vote_authorized_voters_pool_free( authorized_voters->pool ) ) {
218 0 : FD_LOG_ERR(( "Authorized_voter pool is empty" ));
219 0 : }
220 0 : fd_vote_authorized_voter_t * ele =
221 0 : fd_vote_authorized_voters_pool_ele_acquire( authorized_voters->pool );
222 0 : ele->epoch = epoch;
223 0 : ele->pubkey = *pubkey;
224 0 : ele->prio = (ulong)&ele->pubkey;
225 0 : fd_vote_authorized_voters_treap_ele_insert(
226 0 : authorized_voters->treap, ele, authorized_voters->pool );
227 0 : }
228 :
229 : static inline int
230 0 : authorized_voters_is_empty( fd_vote_authorized_voters_t * self ) {
231 0 : return fd_vote_authorized_voters_treap_ele_cnt( self->treap ) == 0;
232 0 : }
233 :
234 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L80
235 : static inline int
236 0 : authorized_voters_contains( fd_vote_authorized_voters_t * self, ulong epoch ) {
237 0 : return !!fd_vote_authorized_voters_treap_ele_query( self->treap, epoch, self->pool );
238 0 : }
239 :
240 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L72
241 : static inline fd_vote_authorized_voter_t *
242 0 : authorized_voters_last( fd_vote_authorized_voters_t * self ) {
243 0 : fd_vote_authorized_voters_treap_rev_iter_t iter =
244 0 : fd_vote_authorized_voters_treap_rev_iter_init( self->treap, self->pool );
245 0 : return fd_vote_authorized_voters_treap_rev_iter_ele( iter, self->pool );
246 0 : }
247 :
248 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L43
249 : static void
250 : authorized_voters_purge_authorized_voters( fd_vote_authorized_voters_t * self,
251 : ulong current_epoch,
252 0 : fd_exec_instr_ctx_t const * ctx /* spad */ ) {
253 :
254 0 : FD_SPAD_FRAME_BEGIN( ctx->txn_ctx->spad ) {
255 :
256 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L46
257 0 : ulong *expired_keys = fd_spad_alloc( ctx->txn_ctx->spad, alignof(ulong), fd_vote_authorized_voters_treap_ele_cnt(self->treap) * sizeof(ulong) );
258 0 : ulong key_cnt = 0;
259 0 : for( fd_vote_authorized_voters_treap_fwd_iter_t iter =
260 0 : fd_vote_authorized_voters_treap_fwd_iter_init( self->treap, self->pool );
261 0 : !fd_vote_authorized_voters_treap_fwd_iter_done( iter );
262 0 : iter = fd_vote_authorized_voters_treap_fwd_iter_next( iter, self->pool ) ) {
263 0 : fd_vote_authorized_voter_t * ele =
264 0 : fd_vote_authorized_voters_treap_fwd_iter_ele( iter, self->pool );
265 0 : if( ele->epoch < current_epoch ) expired_keys[key_cnt++] = ele->epoch;
266 0 : }
267 :
268 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L52
269 0 : for( ulong i = 0; i < key_cnt; i++ ) {
270 0 : fd_vote_authorized_voter_t * ele =
271 0 : fd_vote_authorized_voters_treap_ele_query( self->treap, expired_keys[i], self->pool );
272 0 : fd_vote_authorized_voters_treap_ele_remove( self->treap, ele, self->pool );
273 0 : fd_vote_authorized_voters_pool_ele_release( self->pool, ele );
274 : // fd_vote_authorized_voter_destroy( &self->pool[i], &ctx3 );
275 0 : }
276 :
277 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L60
278 0 : FD_TEST( !authorized_voters_is_empty( self ) );
279 :
280 0 : } FD_SPAD_FRAME_END;
281 :
282 0 : }
283 :
284 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L91
285 : static fd_vote_authorized_voter_t *
286 : authorized_voters_get_or_calculate_authorized_voter_for_epoch( fd_vote_authorized_voters_t * self,
287 : ulong epoch,
288 0 : int * existed ) {
289 0 : *existed = 0;
290 0 : ulong latest_epoch = 0;
291 0 : fd_vote_authorized_voter_t * res =
292 0 : fd_vote_authorized_voters_treap_ele_query( self->treap, epoch, self->pool );
293 : // "predecessor" would be more big-O optimal here, but mirroring labs logic
294 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L93
295 0 : if( FD_UNLIKELY( !res ) ) {
296 0 : for( fd_vote_authorized_voters_treap_fwd_iter_t iter =
297 0 : fd_vote_authorized_voters_treap_fwd_iter_init( self->treap, self->pool );
298 0 : !fd_vote_authorized_voters_treap_fwd_iter_done( iter );
299 0 : iter = fd_vote_authorized_voters_treap_fwd_iter_next( iter, self->pool ) ) {
300 0 : fd_vote_authorized_voter_t * ele =
301 0 : fd_vote_authorized_voters_treap_fwd_iter_ele( iter, self->pool );
302 0 : if( ele->epoch < epoch && ( latest_epoch == 0 || ele->epoch > latest_epoch ) ) {
303 0 : latest_epoch = ele->epoch;
304 0 : res = ele;
305 0 : }
306 0 : }
307 0 : *existed = 0;
308 0 : return res;
309 0 : } else {
310 0 : *existed = 1;
311 0 : return res;
312 0 : }
313 0 : return res;
314 0 : }
315 :
316 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L28
317 : static fd_vote_authorized_voter_t *
318 : authorized_voters_get_and_cache_authorized_voter_for_epoch( fd_vote_authorized_voters_t * self,
319 0 : ulong epoch ) {
320 0 : int existed = 0;
321 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L29
322 0 : fd_vote_authorized_voter_t * res =
323 0 : authorized_voters_get_or_calculate_authorized_voter_for_epoch( self, epoch, &existed );
324 0 : if( !res ) return NULL;
325 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L32
326 0 : if( !existed ) {
327 : /* insert cannot fail because !existed */
328 0 : if( 0 == fd_vote_authorized_voters_pool_free( self->pool) ) {
329 0 : FD_LOG_ERR(( "Authorized_voter pool is empty" ));
330 0 : }
331 0 : fd_vote_authorized_voter_t * ele = fd_vote_authorized_voters_pool_ele_acquire( self->pool );
332 0 : ele->epoch = epoch;
333 0 : ele->pubkey = res->pubkey;
334 0 : ele->prio = (ulong)&res->pubkey;
335 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/authorized_voters.rs#L33
336 0 : fd_vote_authorized_voters_treap_ele_insert( self->treap, ele, self->pool );
337 0 : }
338 0 : return res;
339 0 : }
340 :
341 : /**********************************************************************/
342 : /* impl VoteStateVersions */
343 : /**********************************************************************/
344 :
345 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L66
346 : static fd_landed_vote_t *
347 : landed_votes_from_lockouts( fd_vote_lockout_t * lockouts,
348 0 : fd_spad_t * spad ) {
349 0 : if( !lockouts ) return NULL;
350 :
351 : /* Allocate MAX_LOCKOUT_HISTORY (sane case) by default. In case the
352 : vote account is corrupt, allocate as many entries are needed. */
353 :
354 0 : ulong cnt = deq_fd_vote_lockout_t_cnt( lockouts );
355 0 : cnt = fd_ulong_max( cnt, MAX_LOCKOUT_HISTORY );
356 0 : uchar * deque_mem = fd_spad_alloc( spad,
357 0 : deq_fd_landed_vote_t_align(),
358 0 : deq_fd_landed_vote_t_footprint( cnt ) );
359 0 : fd_landed_vote_t * landed_votes = deq_fd_landed_vote_t_join( deq_fd_landed_vote_t_new( deque_mem, cnt ) );
360 :
361 0 : for( deq_fd_vote_lockout_t_iter_t iter = deq_fd_vote_lockout_t_iter_init( lockouts );
362 0 : !deq_fd_vote_lockout_t_iter_done( lockouts, iter );
363 0 : iter = deq_fd_vote_lockout_t_iter_next( lockouts, iter ) ) {
364 0 : fd_vote_lockout_t const * ele = deq_fd_vote_lockout_t_iter_ele_const( lockouts, iter );
365 :
366 0 : fd_landed_vote_t * elem = deq_fd_landed_vote_t_push_tail_nocopy( landed_votes );
367 0 : fd_landed_vote_new( elem );
368 :
369 0 : elem->latency = 0;
370 0 : elem->lockout.slot = ele->slot;
371 0 : elem->lockout.confirmation_count = ele->confirmation_count;
372 0 : }
373 :
374 0 : return landed_votes;
375 0 : }
376 :
377 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L70
378 : static inline int
379 0 : is_uninitialized( fd_vote_state_versioned_t * self ) {
380 0 : switch( self->discriminant ) {
381 0 : case fd_vote_state_versioned_enum_v0_23_5:;
382 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L73
383 0 : fd_pubkey_t pubkey_default = { 0 };
384 0 : return 0 ==
385 0 : memcmp( &self->inner.v0_23_5.authorized_voter, &pubkey_default, sizeof( fd_pubkey_t ) );
386 0 : case fd_vote_state_versioned_enum_v1_14_11:;
387 0 : return authorized_voters_is_empty( &self->inner.v1_14_11.authorized_voters );
388 0 : case fd_vote_state_versioned_enum_current:
389 0 : return authorized_voters_is_empty( &self->inner.current.authorized_voters );
390 0 : default:
391 0 : FD_LOG_ERR(( "missing handler or invalid vote state version: %u", self->discriminant ));
392 0 : }
393 0 : }
394 :
395 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L73
396 : static void
397 : convert_to_current( fd_vote_state_versioned_t * self,
398 0 : fd_spad_t * spad ) {
399 0 : switch( self->discriminant ) {
400 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L19
401 0 : case fd_vote_state_versioned_enum_v0_23_5: {
402 0 : fd_vote_state_0_23_5_t * state = &self->inner.v0_23_5;
403 0 : fd_vote_authorized_voters_t authorized_voters;
404 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L21
405 0 : authorized_voters_new(
406 0 : state->authorized_voter_epoch, &state->authorized_voter, spad, &authorized_voters );
407 :
408 : /* Temporary to hold current */
409 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L23
410 0 : fd_vote_state_t current = {
411 0 : .node_pubkey = state->node_pubkey, /* copy */
412 0 : .authorized_withdrawer = state->authorized_withdrawer, /* copy */
413 0 : .commission = state->commission, /* copy */
414 0 : .votes = landed_votes_from_lockouts( state->votes, spad ),
415 0 : .has_root_slot = state->has_root_slot, /* copy */
416 0 : .root_slot = state->root_slot, /* copy */
417 0 : .authorized_voters = authorized_voters,
418 0 : .prior_voters = (fd_vote_prior_voters_t) {
419 0 : .idx = 31UL,
420 0 : .is_empty = 1,
421 0 : },
422 0 : .epoch_credits = state->epoch_credits, /* move */
423 0 : .last_timestamp = state->last_timestamp, /* deep copy */
424 0 : };
425 :
426 : /* Move objects */
427 0 : state->epoch_credits = NULL;
428 :
429 : /* Emplace new vote state into target */
430 0 : self->discriminant = fd_vote_state_versioned_enum_current;
431 0 : self->inner.current = current;
432 :
433 0 : break;
434 0 : }
435 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_versions.rs#L44
436 0 : case fd_vote_state_versioned_enum_v1_14_11: {
437 0 : fd_vote_state_1_14_11_t * state = &self->inner.v1_14_11;
438 :
439 : /* Temporary to hold current */
440 0 : fd_vote_state_t current = {
441 0 : .node_pubkey = state->node_pubkey, /* copy */
442 0 : .authorized_withdrawer = state->authorized_withdrawer, /* copy */
443 0 : .commission = state->commission, /* copy */
444 0 : .votes = landed_votes_from_lockouts( state->votes, spad ),
445 0 : .has_root_slot = state->has_root_slot, /* copy */
446 0 : .root_slot = state->root_slot, /* copy */
447 0 : .authorized_voters = state->authorized_voters, /* move */
448 0 : .prior_voters = state->prior_voters, /* deep copy */
449 0 : .epoch_credits = state->epoch_credits, /* move */
450 0 : .last_timestamp = state->last_timestamp /* deep copy */
451 0 : };
452 :
453 : /* Move objects */
454 0 : state->authorized_voters.treap = NULL;
455 0 : state->authorized_voters.pool = NULL;
456 0 : state->epoch_credits = NULL;
457 :
458 : /* Emplace new vote state into target */
459 0 : self->discriminant = fd_vote_state_versioned_enum_current;
460 0 : self->inner.current = current;
461 :
462 0 : break;
463 0 : }
464 0 : case fd_vote_state_versioned_enum_current:
465 0 : break;
466 0 : default:
467 0 : FD_LOG_ERR( ( "unsupported vote state version: %u", self->discriminant ) );
468 0 : }
469 0 : }
470 :
471 : /**********************************************************************/
472 : /* impl VoteState */
473 : /**********************************************************************/
474 :
475 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L428
476 : static void
477 : vote_state_new( fd_vote_init_t * vote_init,
478 : fd_sol_sysvar_clock_t const * clock,
479 : fd_spad_t * spad,
480 0 : fd_vote_state_t * vote_state /* out */ ) {
481 0 : vote_state->node_pubkey = vote_init->node_pubkey;
482 0 : authorized_voters_new(
483 0 : clock->epoch, &vote_init->authorized_voter, spad, &vote_state->authorized_voters );
484 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L431
485 0 : vote_state->authorized_withdrawer = vote_init->authorized_withdrawer;
486 0 : vote_state->commission = vote_init->commission;
487 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L434
488 0 : vote_state->prior_voters.idx = 31;
489 0 : vote_state->prior_voters.is_empty = 1;
490 0 : }
491 :
492 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L985
493 : static inline int
494 : verify_authorized_signer( fd_pubkey_t const * authorized,
495 0 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX] ) {
496 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L989
497 0 : return fd_signers_contains( signers, authorized ) ?
498 0 : FD_EXECUTOR_INSTR_SUCCESS :
499 0 : FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
500 0 : }
501 :
502 : // lambda function: https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L873
503 : static inline int
504 : verify( fd_pubkey_t * epoch_authorized_voter,
505 : int authorized_withdrawer_signer,
506 0 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX] ) {
507 0 : if( authorized_withdrawer_signer )
508 0 : return 0;
509 0 : else
510 0 : return verify_authorized_signer( epoch_authorized_voter, signers );
511 0 : }
512 :
513 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L845
514 : static void
515 0 : pop_expired_votes( fd_vote_state_t * self, ulong next_vote_slot ) {
516 0 : while( !deq_fd_landed_vote_t_empty( self->votes ) ) {
517 0 : fd_landed_vote_t * vote = deq_fd_landed_vote_t_peek_tail( self->votes );
518 0 : if( !( is_locked_out_at_slot( &vote->lockout, next_vote_slot ) ) ) {
519 0 : deq_fd_landed_vote_t_pop_tail( self->votes );
520 0 : } else {
521 0 : break;
522 0 : }
523 0 : }
524 0 : }
525 :
526 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L855
527 : static void
528 0 : double_lockouts( fd_vote_state_t * self ) {
529 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L856
530 0 : ulong stack_depth = deq_fd_landed_vote_t_cnt( self->votes );
531 0 : ulong i = 0;
532 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L857
533 0 : for( deq_fd_landed_vote_t_iter_t iter = deq_fd_landed_vote_t_iter_init( self->votes );
534 0 : !deq_fd_landed_vote_t_iter_done( self->votes, iter );
535 0 : iter = deq_fd_landed_vote_t_iter_next( self->votes, iter ) ) {
536 0 : fd_landed_vote_t * v = deq_fd_landed_vote_t_iter_ele( self->votes, iter );
537 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L860
538 0 : if( stack_depth >
539 0 : fd_ulong_checked_add_expect(
540 0 : i,
541 0 : (ulong)v->lockout.confirmation_count,
542 0 : "`confirmation_count` and tower_size should be bounded by `MAX_LOCKOUT_HISTORY`" ) )
543 0 : {
544 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L864
545 0 : increase_confirmation_count( &v->lockout, 1 );
546 0 : }
547 0 : i++;
548 0 : }
549 0 : }
550 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L668
551 : static inline uchar
552 0 : compute_vote_latency( ulong voted_for_slot, ulong current_slot ) {
553 0 : return (uchar)fd_ulong_min( fd_ulong_sat_sub( current_slot, voted_for_slot ), UCHAR_MAX );
554 0 : }
555 :
556 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L673
557 : static ulong
558 0 : credits_for_vote_at_index( fd_vote_state_t * self, ulong index ) {
559 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L679
560 0 : fd_landed_vote_t * landed_vote = deq_fd_landed_vote_t_peek_index( self->votes, index );
561 0 : ulong latency = landed_vote == NULL ? 0 : landed_vote->latency;
562 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L683
563 0 : ulong max_credits = VOTE_CREDITS_MAXIMUM_PER_SLOT;
564 :
565 : // If latency is 0, this means that the Lockout was created and stored from a software version
566 : // that did not store vote latencies; in this case, 1 credit is awarded
567 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L691
568 0 : if( FD_UNLIKELY( latency == 0 ) ) {
569 0 : return 1;
570 0 : }
571 :
572 0 : ulong diff = 0;
573 0 : int cf = fd_ulong_checked_sub( latency, VOTE_CREDITS_GRACE_SLOTS, &diff );
574 0 : if( cf != 0 || diff == 0 ) {
575 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L697
576 0 : return max_credits;
577 0 : }
578 :
579 0 : ulong credits = 0;
580 0 : cf = fd_ulong_checked_sub( max_credits, diff, &credits );
581 0 : if( cf != 0 || credits == 0 ) {
582 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L705
583 0 : return 1;
584 0 : }
585 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L707
586 0 : return credits;
587 0 : }
588 :
589 : /* https://github.com/anza-xyz/solana-sdk/blob/vote-interface%40v3.0.0/vote-interface/src/state/vote_state_v3.rs#L282-L309 */
590 : static void
591 0 : increment_credits( fd_vote_state_t * self, ulong epoch, ulong credits ) {
592 : /* https://github.com/anza-xyz/solana-sdk/blob/vote-interface%40v3.0.0/vote-interface/src/state/vote_state_v3.rs#L286-L305 */
593 0 : if( FD_UNLIKELY( deq_fd_vote_epoch_credits_t_empty( self->epoch_credits ) ) ) {
594 : /* https://github.com/anza-xyz/solana-sdk/blob/vote-interface%40v3.0.0/vote-interface/src/state/vote_state_v3.rs#L286-L288 */
595 0 : deq_fd_vote_epoch_credits_t_push_tail_wrap(
596 0 : self->epoch_credits,
597 0 : ( fd_vote_epoch_credits_t ){ .epoch = epoch, .credits = 0, .prev_credits = 0 } );
598 0 : } else if( FD_LIKELY( epoch !=
599 0 : deq_fd_vote_epoch_credits_t_peek_tail( self->epoch_credits )->epoch ) ) {
600 : /* https://github.com/anza-xyz/solana-sdk/blob/vote-interface%40v3.0.0/vote-interface/src/state/vote_state_v3.rs#L290 */
601 0 : fd_vote_epoch_credits_t * last = deq_fd_vote_epoch_credits_t_peek_tail( self->epoch_credits );
602 :
603 0 : ulong credits = last->credits;
604 0 : ulong prev_credits = last->prev_credits;
605 :
606 : /* https://github.com/anza-xyz/solana-sdk/blob/vote-interface%40v3.0.0/vote-interface/src/state/vote_state_v3.rs#L292-L299 */
607 0 : if( FD_LIKELY( credits!=prev_credits ) ) {
608 0 : if( FD_UNLIKELY( deq_fd_vote_epoch_credits_t_cnt( self->epoch_credits )>=MAX_EPOCH_CREDITS_HISTORY ) ) {
609 : /* Although Agave performs a `.remove(0)` AFTER the call to
610 : `.push()`, there is an edge case where the epoch credits is
611 : full, making the call to `_push_tail()` unsafe. Since Agave's
612 : structures are dynamically allocated, it is safe for them to
613 : simply call `.push()` and then popping afterwards. We have to
614 : reverse the order of operations to maintain correct behavior
615 : and avoid overflowing the deque.
616 : https://github.com/anza-xyz/solana-sdk/blob/vote-interface%40v3.0.0/vote-interface/src/state/vote_state_v3.rs#L303 */
617 0 : deq_fd_vote_epoch_credits_t_pop_head( self->epoch_credits );
618 0 : }
619 :
620 : /* This will not fail because we already popped if we're at
621 : capacity, since the epoch_credits deque is allocated with a
622 : minimum capacity of MAX_EPOCH_CREDITS_HISTORY. */
623 0 : deq_fd_vote_epoch_credits_t_push_tail(
624 0 : self->epoch_credits,
625 0 : ( fd_vote_epoch_credits_t ){
626 0 : .epoch = epoch, .credits = credits, .prev_credits = credits } );
627 0 : } else {
628 : /* https://github.com/anza-xyz/solana-sdk/blob/vote-interface%40v3.0.0/vote-interface/src/state/vote_state_v3.rs#L297-L298 */
629 0 : deq_fd_vote_epoch_credits_t_peek_tail( self->epoch_credits )->epoch = epoch;
630 :
631 : /* Here we can perform the same deque size check and pop if
632 : we're beyond the maximum epoch credits len. */
633 0 : if( FD_UNLIKELY( deq_fd_vote_epoch_credits_t_cnt( self->epoch_credits )>MAX_EPOCH_CREDITS_HISTORY ) ) {
634 0 : deq_fd_vote_epoch_credits_t_pop_head( self->epoch_credits );
635 0 : }
636 0 : }
637 0 : }
638 :
639 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L663
640 0 : deq_fd_vote_epoch_credits_t_peek_tail( self->epoch_credits )->credits = fd_ulong_sat_add(
641 0 : deq_fd_vote_epoch_credits_t_peek_tail( self->epoch_credits )->credits, credits );
642 0 : }
643 :
644 : static inline ulong *
645 : last_voted_slot( fd_vote_state_t * self );
646 :
647 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L595
648 : static void
649 : process_next_vote_slot( fd_vote_state_t * self,
650 : ulong next_vote_slot,
651 : ulong epoch,
652 0 : ulong current_slot ) {
653 0 : ulong * last_voted_slot_ = last_voted_slot( self );
654 0 : if( FD_UNLIKELY( last_voted_slot_ && next_vote_slot <= *last_voted_slot_ ) ) return;
655 :
656 0 : pop_expired_votes( self, next_vote_slot );
657 :
658 0 : fd_landed_vote_t landed_vote = { .latency = compute_vote_latency( next_vote_slot, current_slot ),
659 0 : ( fd_vote_lockout_t ){ .slot = next_vote_slot } };
660 :
661 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L623
662 0 : if( FD_UNLIKELY( deq_fd_landed_vote_t_cnt( self->votes ) == MAX_LOCKOUT_HISTORY ) ) {
663 0 : ulong credits = credits_for_vote_at_index( self, 0 );
664 0 : fd_landed_vote_t landed_vote = deq_fd_landed_vote_t_pop_head( self->votes );
665 0 : self->has_root_slot = 1;
666 0 : self->root_slot = landed_vote.lockout.slot;
667 :
668 0 : increment_credits( self, epoch, credits );
669 0 : }
670 :
671 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L634
672 0 : deq_fd_landed_vote_t_push_tail_wrap( self->votes, landed_vote );
673 0 : double_lockouts( self );
674 0 : }
675 :
676 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L828
677 : static int
678 : get_and_update_authorized_voter( fd_vote_state_t * self,
679 : ulong current_epoch,
680 : fd_pubkey_t ** pubkey /* out */,
681 0 : fd_exec_instr_ctx_t const * ctx /* spad */ ) {
682 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L832
683 0 : fd_vote_authorized_voter_t * authorized_voter =
684 0 : authorized_voters_get_and_cache_authorized_voter_for_epoch( &self->authorized_voters,
685 0 : current_epoch );
686 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L835
687 0 : if( FD_UNLIKELY( !authorized_voter ) ) return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
688 0 : *pubkey = &authorized_voter->pubkey;
689 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L837
690 0 : authorized_voters_purge_authorized_voters( &self->authorized_voters, current_epoch, ctx );
691 0 : return FD_EXECUTOR_INSTR_SUCCESS;
692 0 : }
693 :
694 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L768
695 : static int
696 : set_new_authorized_voter( fd_vote_state_t * self,
697 : fd_pubkey_t const * authorized_pubkey,
698 : ulong current_epoch,
699 : ulong target_epoch,
700 : /* "verify" closure */ int authorized_withdrawer_signer,
701 : /* "verify" closure */ fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
702 0 : fd_exec_instr_ctx_t const * ctx ) {
703 0 : int rc;
704 0 : fd_pubkey_t * epoch_authorized_voter = NULL;
705 :
706 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L778
707 0 : rc = get_and_update_authorized_voter( self, current_epoch, &epoch_authorized_voter, ctx );
708 0 : if( FD_UNLIKELY( rc ) ) return rc;
709 :
710 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L779
711 0 : rc = verify( epoch_authorized_voter, authorized_withdrawer_signer, signers );
712 0 : if( FD_UNLIKELY( rc ) ) return rc;
713 :
714 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L786
715 0 : if( FD_UNLIKELY( authorized_voters_contains( &self->authorized_voters, target_epoch ) ) ) {
716 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_TOO_SOON_TO_REAUTHORIZE;
717 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
718 0 : }
719 :
720 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L791
721 0 : fd_vote_authorized_voter_t * latest_authorized =
722 0 : authorized_voters_last( &self->authorized_voters );
723 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L794
724 0 : if( FD_UNLIKELY( ( !latest_authorized ) ) ) return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
725 0 : ulong latest_epoch = latest_authorized->epoch;
726 0 : fd_pubkey_t * latest_authorized_pubkey = &latest_authorized->pubkey;
727 :
728 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L799
729 0 : if( 0 != memcmp( latest_authorized_pubkey, authorized_pubkey, sizeof( fd_pubkey_t ) ) ) {
730 0 : fd_vote_prior_voters_t * prior_voters = &self->prior_voters;
731 :
732 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L801
733 0 : ulong epoch_of_last_authorized_switch = 0UL;
734 0 : if( (!prior_voters->is_empty) & (prior_voters->idx < 32) ) {
735 0 : epoch_of_last_authorized_switch = prior_voters->buf[prior_voters->idx].epoch_end;
736 0 : }
737 :
738 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L810
739 0 : if( target_epoch <= latest_epoch )
740 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
741 :
742 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L815
743 0 : prior_voters->idx += 1UL;
744 0 : prior_voters->idx %= 32UL;
745 0 : prior_voters->buf[prior_voters->idx] =
746 0 : ( fd_vote_prior_voter_t ){ .pubkey = *latest_authorized_pubkey,
747 0 : .epoch_start = epoch_of_last_authorized_switch,
748 0 : .epoch_end = target_epoch };
749 0 : prior_voters->is_empty = 0;
750 0 : }
751 :
752 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L822
753 0 : if( 0 == fd_vote_authorized_voters_pool_free( self->authorized_voters.pool) ) {
754 0 : FD_LOG_ERR(( "Authorized_voter pool is empty" ));
755 0 : }
756 :
757 0 : fd_vote_authorized_voter_t * ele =
758 0 : fd_vote_authorized_voters_pool_ele_acquire( self->authorized_voters.pool );
759 0 : ele->epoch = target_epoch;
760 0 : ele->pubkey = *authorized_pubkey;
761 0 : ele->prio = (ulong)&ele->pubkey;
762 0 : fd_vote_authorized_voters_treap_ele_insert(
763 0 : self->authorized_voters.treap, ele, self->authorized_voters.pool );
764 :
765 0 : return 0;
766 0 : }
767 :
768 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L869
769 : static int
770 : process_timestamp( fd_vote_state_t * self,
771 : ulong slot,
772 : long timestamp,
773 0 : fd_exec_instr_ctx_t const * ctx ) {
774 0 : if( FD_UNLIKELY(
775 0 : ( slot < self->last_timestamp.slot || timestamp < self->last_timestamp.timestamp ) ||
776 0 : ( slot == self->last_timestamp.slot &&
777 0 : ( slot != self->last_timestamp.slot || timestamp != self->last_timestamp.timestamp ) &&
778 0 : self->last_timestamp.slot != 0 ) ) ) {
779 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_TIMESTAMP_TOO_OLD;
780 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
781 0 : }
782 0 : self->last_timestamp.slot = slot;
783 0 : self->last_timestamp.timestamp = timestamp;
784 :
785 0 : return 0;
786 0 : }
787 :
788 : /**********************************************************************/
789 : /* mod vote_state */
790 : /**********************************************************************/
791 :
792 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L166
793 : __attribute__((warn_unused_result)) static int
794 : set_vote_account_state( fd_borrowed_account_t * vote_account,
795 : fd_vote_state_t * vote_state,
796 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
797 : /* This is a horrible conditional expression in Agave.
798 : The terms were broken up into their own variables. */
799 :
800 0 : ulong vsz = size_of_versioned( 1 );
801 :
802 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L175
803 0 : fd_rent_t const rent = fd_sysvar_cache_rent_read_nofail( ctx->sysvar_cache );
804 0 : int resize_needed = fd_borrowed_account_get_data_len( vote_account ) < vsz;
805 0 : int resize_rent_exempt = fd_rent_exempt_minimum_balance( &rent, vsz ) <= fd_borrowed_account_get_lamports( vote_account );
806 :
807 : /* The resize operation itself is part of the horrible conditional,
808 : but behind a short-circuit operator. */
809 0 : int resize_failed = 0;
810 0 : if( resize_needed && resize_rent_exempt ) {
811 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L179
812 0 : resize_failed =
813 0 : fd_borrowed_account_set_data_length( vote_account, vsz ) != FD_EXECUTOR_INSTR_SUCCESS;
814 0 : }
815 :
816 0 : if( FD_UNLIKELY( resize_needed && ( !resize_rent_exempt || resize_failed ) ) ) {
817 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L184
818 0 : fd_vote_state_versioned_t v1_14_11;
819 0 : fd_vote_state_versioned_new_disc( &v1_14_11, fd_vote_state_versioned_enum_v1_14_11 );
820 0 : from_vote_state_1_14_11( vote_state, &v1_14_11.inner.v1_14_11, ctx->txn_ctx->spad );
821 0 : return set_state( vote_account, &v1_14_11 );
822 0 : }
823 :
824 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L189
825 : // TODO: This is stupid... optimize this...
826 0 : fd_vote_state_versioned_t new_current = { .discriminant = fd_vote_state_versioned_enum_current,
827 0 : .inner = { .current = *vote_state } };
828 0 : return set_state( vote_account, &new_current );
829 0 : }
830 :
831 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L727
832 : static inline fd_vote_lockout_t *
833 0 : last_lockout( fd_vote_state_t * self ) {
834 0 : if( deq_fd_landed_vote_t_empty( self->votes ) ) return NULL;
835 0 : fd_landed_vote_t * last_vote = deq_fd_landed_vote_t_peek_tail( self->votes );
836 0 : return &last_vote->lockout;
837 0 : }
838 :
839 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L731
840 : static inline ulong *
841 0 : last_voted_slot( fd_vote_state_t * self ) {
842 0 : fd_vote_lockout_t * last_lockout_ = last_lockout( self );
843 0 : if( FD_UNLIKELY( !last_lockout_ ) ) return NULL;
844 0 : return &last_lockout_->slot;
845 0 : }
846 :
847 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L573
848 : static uchar
849 0 : contains_slot( fd_vote_state_t * vote_state, ulong slot ) {
850 : /* Logic is copied from slice::binary_search_by() in Rust. While not fully optimized,
851 : it aims to achieve fuzzing conformance for both sorted and unsorted inputs. */
852 0 : ulong size = deq_fd_landed_vote_t_cnt( vote_state->votes );
853 0 : if( FD_UNLIKELY( size==0UL ) ) return 0;
854 :
855 0 : ulong base = 0UL;
856 0 : while( size>1UL ) {
857 0 : ulong half = size / 2UL;
858 0 : ulong mid = base + half;
859 0 : ulong mid_slot = deq_fd_landed_vote_t_peek_index_const( vote_state->votes, mid )->lockout.slot;
860 0 : base = (slot<mid_slot) ? base : mid;
861 0 : size -= half;
862 0 : }
863 :
864 0 : return deq_fd_landed_vote_t_peek_index_const( vote_state->votes, base )->lockout.slot==slot;
865 0 : }
866 :
867 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L201
868 : static int
869 : check_and_filter_proposed_vote_state( fd_vote_state_t * vote_state,
870 : fd_vote_lockout_t * proposed_lockouts,
871 : uchar * proposed_has_root,
872 : ulong * proposed_root,
873 : fd_hash_t const * proposed_hash,
874 : fd_slot_hash_t const * slot_hashes, /* deque */
875 0 : fd_exec_instr_ctx_t const * ctx ) {
876 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L208
877 0 : if( FD_UNLIKELY( deq_fd_vote_lockout_t_empty( proposed_lockouts ) ) ) {
878 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_EMPTY_SLOTS;
879 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
880 0 : }
881 0 : fd_landed_vote_t const * last_vote = NULL;
882 0 : if( !deq_fd_landed_vote_t_empty( vote_state->votes ) ) {
883 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L212
884 0 : last_vote = deq_fd_landed_vote_t_peek_tail( vote_state->votes );
885 0 : }
886 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L218
887 0 : if( FD_LIKELY( last_vote ) ) {
888 0 : if( FD_UNLIKELY( deq_fd_vote_lockout_t_peek_tail_const( proposed_lockouts )->slot <=
889 0 : last_vote->lockout.slot ) ) {
890 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_VOTE_TOO_OLD;
891 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
892 0 : }
893 0 : }
894 :
895 : /* must be nonempty, checked above */
896 0 : ulong last_vote_state_update_slot = deq_fd_vote_lockout_t_peek_tail_const( proposed_lockouts )->slot;
897 :
898 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L224
899 0 : if( FD_UNLIKELY( deq_fd_slot_hash_t_empty( slot_hashes ) ) ) {
900 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOTS_MISMATCH;
901 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
902 0 : }
903 :
904 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L227
905 0 : ulong earliest_slot_hash_in_history = deq_fd_slot_hash_t_peek_tail_const( slot_hashes )->slot;
906 :
907 : /* Check if the proposed vote is too old to be in the SlotHash history */
908 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L230
909 0 : if( FD_UNLIKELY( last_vote_state_update_slot < earliest_slot_hash_in_history ) ) {
910 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_VOTE_TOO_OLD;
911 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
912 0 : }
913 :
914 : /* Check if the proposed root is too old */
915 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L237
916 0 : if( *proposed_has_root ) {
917 0 : ulong const proposed_root_ = *proposed_root;
918 : /* If the new proposed root `R` is less than the earliest slot hash in the history
919 : such that we cannot verify whether the slot was actually was on this fork, set
920 : the root to the latest vote in the current vote that's less than R. */
921 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L242
922 0 : if( proposed_root_ < earliest_slot_hash_in_history ) {
923 0 : *proposed_has_root = vote_state->has_root_slot;
924 0 : *proposed_root = vote_state->root_slot;
925 0 : for( deq_fd_landed_vote_t_iter_t iter =
926 0 : deq_fd_landed_vote_t_iter_init_rev( vote_state->votes );
927 0 : !deq_fd_landed_vote_t_iter_done_rev( vote_state->votes, iter );
928 0 : iter = deq_fd_landed_vote_t_iter_prev( vote_state->votes, iter ) ) {
929 : /* Ensure we're iterating from biggest to smallest vote in the
930 : current vote state */
931 0 : fd_landed_vote_t const * vote = deq_fd_landed_vote_t_iter_ele_const( vote_state->votes, iter );
932 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L248
933 0 : if( vote->lockout.slot <= proposed_root_ ) {
934 0 : *proposed_has_root = 1;
935 0 : *proposed_root = vote->lockout.slot;
936 0 : break;
937 0 : }
938 :
939 0 : }
940 0 : }
941 0 : }
942 :
943 0 : FD_SPAD_FRAME_BEGIN( ctx->txn_ctx->spad ) {
944 :
945 : /* Index into the new proposed vote state's slots, starting with the root if it exists then
946 : we use this mutable root to fold checking the root slot into the below loop for performance */
947 0 : int has_root_to_check = *proposed_has_root;
948 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L259
949 0 : ulong root_to_check = *proposed_root;
950 0 : ulong proposed_lockouts_index = 0UL;
951 0 : ulong lockouts_len = deq_fd_vote_lockout_t_cnt( proposed_lockouts );
952 :
953 : /* Index into the slot_hashes, starting at the oldest known slot hash */
954 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L264
955 0 : ulong slot_hashes_index = deq_fd_slot_hash_t_cnt( slot_hashes );
956 0 : ulong * proposed_lockouts_indexes_to_filter = fd_spad_alloc( ctx->txn_ctx->spad, alignof(ulong), lockouts_len * sizeof(ulong) );
957 0 : ulong filter_index = 0UL;
958 :
959 :
960 : /* Note:
961 :
962 : 1) `vote_state_update.lockouts` is sorted from oldest/smallest vote to newest/largest
963 : vote, due to the way votes are applied to the vote state (newest votes
964 : pushed to the back).
965 :
966 : 2) Conversely, `slot_hashes` is sorted from newest/largest vote to
967 : the oldest/smallest vote
968 :
969 : Unlike for vote updates, vote state updates here can't only check votes older than the last vote
970 : because have to ensure that every slot is actually part of the history, not just the most
971 : recent ones */
972 :
973 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L279
974 0 : while( proposed_lockouts_index < lockouts_len && slot_hashes_index > 0 ) {
975 0 : ulong proposed_vote_slot =
976 0 : fd_ulong_if( has_root_to_check,
977 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L281
978 0 : root_to_check,
979 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L283
980 0 : deq_fd_vote_lockout_t_peek_index_const( proposed_lockouts,
981 0 : proposed_lockouts_index )
982 0 : ->slot );
983 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L285
984 0 : if( !has_root_to_check && proposed_lockouts_index > 0UL &&
985 0 : proposed_vote_slot <=
986 0 : deq_fd_vote_lockout_t_peek_index_const(
987 0 : proposed_lockouts,
988 0 : fd_ulong_checked_sub_expect(
989 0 : proposed_lockouts_index,
990 0 : 1,
991 0 : "`proposed_lockouts_index` is positive when checking `SlotsNotOrdered`" ) )
992 0 : ->slot ) {
993 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L293
994 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOTS_NOT_ORDERED;
995 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
996 0 : }
997 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L295
998 0 : ulong ancestor_slot =
999 0 : deq_fd_slot_hash_t_peek_index_const(
1000 0 : slot_hashes,
1001 0 : fd_ulong_checked_sub_expect(
1002 0 : slot_hashes_index,
1003 0 : 1UL,
1004 0 : "`slot_hashes_index` is positive when computing `ancestor_slot`" ) )
1005 0 : ->slot;
1006 : /* Find if this slot in the proposed vote state exists in the SlotHashes history
1007 : to confirm if it was a valid ancestor on this fork */
1008 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L303
1009 0 : if( proposed_vote_slot < ancestor_slot ) {
1010 0 : if( slot_hashes_index == deq_fd_slot_hash_t_cnt( slot_hashes ) ) {
1011 : /* The vote slot does not exist in the SlotHashes history because it's too old,
1012 : i.e. older than the oldest slot in the history. */
1013 0 : if( proposed_vote_slot >= earliest_slot_hash_in_history ) {
1014 0 : ctx->txn_ctx->custom_err = 0;
1015 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1016 0 : }
1017 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L310
1018 0 : if( !contains_slot( vote_state, proposed_vote_slot ) && !has_root_to_check ) {
1019 : /* If the vote slot is both:
1020 : 1) Too old
1021 : 2) Doesn't already exist in vote state
1022 : Then filter it out */
1023 0 : proposed_lockouts_indexes_to_filter[filter_index++] = proposed_lockouts_index; }
1024 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L318
1025 0 : if( has_root_to_check ) {
1026 0 : ulong new_proposed_root = root_to_check;
1027 : /* 1. Because `root_to_check.is_some()`, then we know that
1028 : we haven't checked the root yet in this loop, so
1029 : `proposed_vote_slot` == `new_proposed_root` == `vote_state_update.root` */
1030 0 : FD_TEST( new_proposed_root == proposed_vote_slot );
1031 : /* 2. We know from the assert earlier in the function that
1032 : `proposed_vote_slot < earliest_slot_hash_in_history`,
1033 : so from 1. we know that `new_proposed_root < earliest_slot_hash_in_history` */
1034 0 : if( new_proposed_root >= earliest_slot_hash_in_history ) {
1035 0 : ctx->txn_ctx->custom_err = 0;
1036 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1037 0 : }
1038 :
1039 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L329
1040 0 : has_root_to_check = 0;
1041 0 : root_to_check = ULONG_MAX;
1042 0 : } else {
1043 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L331
1044 0 : proposed_lockouts_index = fd_ulong_checked_add_expect(
1045 0 : proposed_lockouts_index,
1046 0 : 1,
1047 0 : "`proposed_lockouts_index` is bounded by `MAX_LOCKOUT_HISTORY` when "
1048 0 : "`proposed_vote_slot` is too old to be in SlotHashes history" );
1049 0 : }
1050 0 : continue;
1051 0 : } else {
1052 : /* If the vote slot is new enough to be in the slot history,
1053 : but is not part of the slot history, then it must belong to another fork,
1054 : which means this vote state update is invalid. */
1055 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L340
1056 0 : if( has_root_to_check ) {
1057 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_ROOT_ON_DIFFERENT_FORK;
1058 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1059 0 : } else {
1060 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOTS_MISMATCH;
1061 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1062 0 : }
1063 0 : }
1064 0 : } else if( proposed_vote_slot > ancestor_slot ) {
1065 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L347
1066 :
1067 : /* Decrement `slot_hashes_index` to find newer slots in the SlotHashes history */
1068 0 : slot_hashes_index = fd_ulong_checked_sub_expect(
1069 0 : slot_hashes_index,
1070 0 : 1,
1071 0 : "`slot_hashes_index` is positive when finding newer slots in SlotHashes history" );
1072 0 : continue;
1073 0 : } else {
1074 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L354
1075 :
1076 : /* Once the slot in `vote_state_update.lockouts` is found, bump to the next slot
1077 : in `vote_state_update.lockouts` and continue. If we were checking the root,
1078 : start checking the vote state instead. */
1079 0 : if( has_root_to_check ) {
1080 0 : has_root_to_check = 0;
1081 0 : root_to_check = ULONG_MAX;
1082 0 : } else {
1083 0 : proposed_lockouts_index = fd_ulong_checked_add_expect(
1084 0 : proposed_lockouts_index,
1085 0 : 1,
1086 0 : "`proposed_lockouts_index` is bounded by `MAX_LOCKOUT_HISTORY` "
1087 0 : "when match is found in SlotHashes history" );
1088 0 : slot_hashes_index = fd_ulong_checked_sub_expect(
1089 0 : slot_hashes_index,
1090 0 : 1,
1091 0 : "`slot_hashes_index` is positive when match is found in SlotHashes history" );
1092 0 : }
1093 0 : }
1094 0 : }
1095 :
1096 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L372
1097 0 : if( proposed_lockouts_index != deq_fd_vote_lockout_t_cnt( proposed_lockouts ) ) {
1098 : /* The last vote slot in the update did not exist in SlotHashes */
1099 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOTS_MISMATCH;
1100 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1101 0 : }
1102 :
1103 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L401
1104 0 : if( memcmp( &deq_fd_slot_hash_t_peek_index_const( slot_hashes, slot_hashes_index )->hash,
1105 0 : proposed_hash,
1106 0 : sizeof( fd_hash_t ) ) != 0 ) {
1107 : /* This means the newest vote in the slot has a match that
1108 : doesn't match the expected hash for that slot on this fork */
1109 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOTS_HASH_MISMATCH;
1110 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1111 0 : }
1112 :
1113 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L418
1114 : /* Filter out the irrelevant votes */
1115 0 : proposed_lockouts_index = 0UL;
1116 0 : ulong filter_votes_index = deq_fd_vote_lockout_t_cnt( proposed_lockouts );
1117 :
1118 : /* We need to iterate backwards here because proposed_lockouts_indexes_to_filter[ i ] is a
1119 : strictly increasing value. Forward iterating can lead to the proposed lockout indicies to get
1120 : shifted leading to popping the wrong proposed lockouts or out of bounds accessing. We need
1121 : to be sure of handling underflow in this case. */
1122 :
1123 0 : for( ulong i=filter_index; i>0UL && filter_votes_index>0UL; i-- ) {
1124 0 : proposed_lockouts_index = i - 1UL;
1125 0 : if( FD_UNLIKELY(proposed_lockouts_indexes_to_filter[ proposed_lockouts_index ]>=filter_votes_index ) ) {
1126 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
1127 0 : }
1128 :
1129 0 : deq_fd_vote_lockout_t_pop_idx_tail( proposed_lockouts, proposed_lockouts_indexes_to_filter[ proposed_lockouts_index ] );
1130 0 : filter_votes_index--;
1131 0 : }
1132 0 : } FD_SPAD_FRAME_END;
1133 :
1134 0 : return FD_EXECUTOR_INSTR_SUCCESS;
1135 0 : }
1136 :
1137 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L440
1138 : static int
1139 : check_slots_are_valid( fd_vote_state_t * vote_state,
1140 : ulong const * vote_slots,
1141 : fd_hash_t const * vote_hash,
1142 : fd_slot_hash_t const * slot_hashes, /* deque */
1143 0 : fd_exec_instr_ctx_t const * ctx ) {
1144 0 : ulong i = 0;
1145 0 : ulong j = deq_fd_slot_hash_t_cnt( slot_hashes );
1146 0 : ulong vote_slots_len = deq_ulong_cnt( vote_slots );
1147 :
1148 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L462
1149 0 : while( i < vote_slots_len && j > 0 ) {
1150 0 : ulong * last_voted_slot_ = last_voted_slot( vote_state );
1151 0 : if( FD_UNLIKELY( last_voted_slot_ &&
1152 0 : *deq_ulong_peek_index_const( vote_slots, i ) <= *last_voted_slot_ ) ) {
1153 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L469
1154 0 : i = fd_ulong_checked_add_expect(
1155 0 : i, 1, "`i` is bounded by `MAX_LOCKOUT_HISTORY` when finding larger slots" );
1156 0 : continue;
1157 0 : }
1158 :
1159 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L476
1160 0 : if( FD_UNLIKELY(
1161 0 : *deq_ulong_peek_index_const( vote_slots, i ) !=
1162 0 : deq_fd_slot_hash_t_peek_index_const( slot_hashes,
1163 0 : fd_ulong_checked_sub_expect( j, 1, "`j` is positive" ) )
1164 0 : ->slot ) ) {
1165 0 : j = fd_ulong_checked_sub_expect( j, 1, "`j` is positive when finding newer slots" );
1166 0 : continue;
1167 0 : }
1168 :
1169 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L486
1170 0 : i = fd_ulong_checked_add_expect(
1171 0 : i, 1, "`i` is bounded by `MAX_LOCKOUT_HISTORY` when hash is found" );
1172 0 : j = fd_ulong_checked_sub_expect( j, 1, "`j` is positive when hash is found" );
1173 0 : }
1174 :
1175 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L494
1176 0 : if( FD_UNLIKELY( j == deq_fd_slot_hash_t_cnt( slot_hashes ) ) ) {
1177 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_VOTE_TOO_OLD;
1178 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1179 0 : }
1180 0 : if( FD_UNLIKELY( i != vote_slots_len ) ) {
1181 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOTS_MISMATCH;
1182 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1183 0 : }
1184 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L514
1185 0 : if( FD_UNLIKELY( 0 != memcmp( &deq_fd_slot_hash_t_peek_index_const( slot_hashes, j )->hash,
1186 0 : vote_hash,
1187 0 : 32UL ) ) ) {
1188 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOTS_HASH_MISMATCH;
1189 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1190 0 : }
1191 0 : return 0;
1192 0 : }
1193 :
1194 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L565
1195 : static int
1196 : process_new_vote_state( fd_vote_state_t * vote_state,
1197 : fd_landed_vote_t * new_state,
1198 : int has_new_root,
1199 : ulong new_root,
1200 : int has_timestamp,
1201 : long timestamp,
1202 : ulong epoch,
1203 : ulong current_slot,
1204 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
1205 0 : int rc;
1206 :
1207 0 : FD_TEST( !deq_fd_landed_vote_t_empty( new_state ) );
1208 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L575
1209 0 : if( FD_UNLIKELY( deq_fd_landed_vote_t_cnt( new_state ) > MAX_LOCKOUT_HISTORY ) ) {
1210 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_TOO_MANY_VOTES;
1211 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1212 0 : };
1213 :
1214 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L579
1215 0 : if( FD_UNLIKELY( has_new_root && vote_state->has_root_slot ) ) {
1216 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L581
1217 0 : if( FD_UNLIKELY( new_root < vote_state->root_slot ) ) {
1218 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_ROOT_ROLL_BACK;
1219 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1220 0 : }
1221 0 : } else if( FD_UNLIKELY( !has_new_root && vote_state->has_root_slot ) ) {
1222 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L586
1223 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_ROOT_ROLL_BACK;
1224 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1225 0 : } else {
1226 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L588
1227 : /* no-op */
1228 0 : }
1229 :
1230 0 : fd_landed_vote_t * previous_vote = NULL;
1231 0 : for( deq_fd_landed_vote_t_iter_t iter = deq_fd_landed_vote_t_iter_init( new_state );
1232 0 : !deq_fd_landed_vote_t_iter_done( new_state, iter );
1233 0 : iter = deq_fd_landed_vote_t_iter_next( new_state, iter ) ) {
1234 0 : fd_landed_vote_t * vote = deq_fd_landed_vote_t_iter_ele( new_state, iter );
1235 0 : if( FD_LIKELY( vote->lockout.confirmation_count == 0 ) ) {
1236 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_ZERO_CONFIRMATIONS;
1237 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1238 0 : } else if( FD_UNLIKELY( vote->lockout.confirmation_count > MAX_LOCKOUT_HISTORY ) ) {
1239 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_CONFIRMATION_TOO_LARGE;
1240 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1241 0 : } else if( FD_LIKELY( has_new_root ) ) {
1242 0 : if( FD_UNLIKELY( vote->lockout.slot <= new_root && new_root != SLOT_DEFAULT ) ) {
1243 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOT_SMALLER_THAN_ROOT;
1244 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1245 0 : }
1246 0 : }
1247 :
1248 0 : if( FD_LIKELY( previous_vote ) ) {
1249 0 : if( FD_UNLIKELY( previous_vote->lockout.slot >= vote->lockout.slot ) ) {
1250 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_SLOTS_NOT_ORDERED;
1251 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1252 0 : } else if( FD_UNLIKELY( previous_vote->lockout.confirmation_count <=
1253 0 : vote->lockout.confirmation_count ) ) {
1254 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_CONFIRMATIONS_NOT_ORDERED;
1255 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1256 0 : } else if( FD_UNLIKELY( vote->lockout.slot >
1257 0 : last_locked_out_slot( &previous_vote->lockout ) ) ) {
1258 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_NEW_VOTE_STATE_LOCKOUT_MISMATCH;
1259 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1260 0 : }
1261 0 : }
1262 0 : previous_vote = vote;
1263 0 : }
1264 :
1265 0 : ulong current_vote_state_index = 0;
1266 0 : ulong new_vote_state_index = 0;
1267 :
1268 : /* Accumulate credits earned by newly rooted slots. The behavior changes with
1269 : timely_vote_credits: prior to this feature, there was a bug that counted a new root slot as 1
1270 : credit even if it had never been voted on. timely_vote_credits fixes this bug by only awarding
1271 : credits for slots actually voted on and finalized. */
1272 :
1273 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L635
1274 :
1275 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L641
1276 0 : ulong earned_credits = 0;
1277 :
1278 0 : if( FD_LIKELY( has_new_root ) ) {
1279 0 : for( deq_fd_landed_vote_t_iter_t iter = deq_fd_landed_vote_t_iter_init( vote_state->votes );
1280 0 : !deq_fd_landed_vote_t_iter_done( vote_state->votes, iter );
1281 0 : iter = deq_fd_landed_vote_t_iter_next( vote_state->votes, iter ) ) {
1282 0 : fd_landed_vote_t * current_vote = deq_fd_landed_vote_t_iter_ele( vote_state->votes, iter );
1283 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L647
1284 0 : if( FD_UNLIKELY( current_vote->lockout.slot <= new_root ) ) {
1285 : // this is safe because we're inside if has_new_root
1286 0 : earned_credits = fd_ulong_checked_add_expect(
1287 0 : credits_for_vote_at_index( vote_state,
1288 0 : current_vote_state_index ),
1289 0 : earned_credits,
1290 0 : "`earned_credits` does not overflow" );
1291 0 : current_vote_state_index = fd_ulong_checked_add_expect(
1292 0 : current_vote_state_index,
1293 0 : 1,
1294 0 : "`current_vote_state_index` is bounded by `MAX_LOCKOUT_HISTORY` "
1295 0 : "when processing new root" );
1296 0 : continue;
1297 0 : }
1298 0 : break;
1299 0 : }
1300 0 : }
1301 :
1302 : // For any slots newly added to the new vote state, the vote latency of that slot is not provided by the
1303 : // vote instruction contents, but instead is computed from the actual latency of the vote
1304 : // instruction. This prevents other validators from manipulating their own vote latencies within their vote states
1305 : // and forcing the rest of the cluster to accept these possibly fraudulent latency values. If the
1306 : // timly_vote_credits feature is not enabled then vote latency is set to 0 for new votes.
1307 : //
1308 : // For any slot that is in both the new state and the current state, the vote latency of the new state is taken
1309 : // from the current state.
1310 : //
1311 : // Thus vote latencies are set here for any newly vote-on slots when a vote instruction is received.
1312 : // They are copied into the new vote state after every vote for already voted-on slots.
1313 : // And when voted-on slots are rooted, the vote latencies stored in the vote state of all the rooted slots is used
1314 : // to compute credits earned.
1315 : // All validators compute the same vote latencies because all process the same vote instruction at the
1316 : // same slot, and the only time vote latencies are ever computed is at the time that their slot is first voted on;
1317 : // after that, the latencies are retained unaltered until the slot is rooted.
1318 :
1319 : // All the votes in our current vote state that are missing from the new vote state
1320 : // must have been expired by later votes. Check that the lockouts match this assumption.
1321 :
1322 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L686
1323 0 : while( current_vote_state_index < deq_fd_landed_vote_t_cnt( vote_state->votes ) &&
1324 0 : new_vote_state_index < deq_fd_landed_vote_t_cnt( new_state ) ) {
1325 0 : fd_landed_vote_t * current_vote =
1326 0 : deq_fd_landed_vote_t_peek_index( vote_state->votes, current_vote_state_index );
1327 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L690
1328 0 : fd_landed_vote_t * new_vote =
1329 0 : deq_fd_landed_vote_t_peek_index( new_state, new_vote_state_index );
1330 :
1331 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L696
1332 0 : if( FD_LIKELY( current_vote->lockout.slot < new_vote->lockout.slot ) ) {
1333 : /* The agave implementation of calculating the last locked out
1334 : slot does not calculate a min between the current vote's
1335 : confirmation count and max lockout history. The reason we do
1336 : this is to make sure that the fuzzers continue working:
1337 : the max lockout history can not be > MAX_LOCKOUT_HISTORY. */
1338 0 : ulong confirmation_count = fd_ulong_min( current_vote->lockout.confirmation_count, MAX_LOCKOUT_HISTORY );
1339 0 : ulong last_locked_out_slot = fd_ulong_sat_add( current_vote->lockout.slot,
1340 0 : (ulong)pow( INITIAL_LOCKOUT, (double)confirmation_count ) );
1341 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L697
1342 0 : if( last_locked_out_slot >= new_vote->lockout.slot ) {
1343 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L698
1344 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_LOCKOUT_CONFLICT;
1345 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1346 0 : }
1347 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L700
1348 0 : current_vote_state_index =
1349 0 : fd_ulong_checked_add_expect( current_vote_state_index,
1350 0 : 1,
1351 0 : "`current_vote_state_index` is bounded by "
1352 0 : "`MAX_LOCKOUT_HISTORY` when slot is less than proposed" );
1353 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L704
1354 0 : } else if( FD_UNLIKELY( current_vote->lockout.slot == new_vote->lockout.slot ) ) {
1355 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L707
1356 0 : if( new_vote->lockout.confirmation_count < current_vote->lockout.confirmation_count ) {
1357 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_CONFIRMATION_ROLL_BACK;
1358 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1359 0 : }
1360 :
1361 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L712
1362 0 : new_vote->latency =
1363 0 : deq_fd_landed_vote_t_peek_index( vote_state->votes, current_vote_state_index )->latency;
1364 :
1365 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L714
1366 0 : current_vote_state_index =
1367 0 : fd_ulong_checked_add_expect( current_vote_state_index,
1368 0 : 1,
1369 0 : "`current_vote_state_index` is bounded by "
1370 0 : "`MAX_LOCKOUT_HISTORY` when slot is equal to proposed" );
1371 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L717
1372 0 : new_vote_state_index =
1373 0 : fd_ulong_checked_add_expect( new_vote_state_index,
1374 0 : 1,
1375 0 : "`new_vote_state_index` is bounded by `MAX_LOCKOUT_HISTORY` "
1376 0 : "when slot is equal to proposed" );
1377 0 : } else {
1378 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L722
1379 0 : new_vote_state_index =
1380 0 : fd_ulong_checked_add_expect( new_vote_state_index,
1381 0 : 1,
1382 0 : "`new_vote_state_index` is bounded by `MAX_LOCKOUT_HISTORY` "
1383 0 : "when slot is greater than proposed" );
1384 0 : }
1385 0 : }
1386 :
1387 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L737
1388 0 : for( deq_fd_landed_vote_t_iter_t iter = deq_fd_landed_vote_t_iter_init( new_state );
1389 0 : !deq_fd_landed_vote_t_iter_done( new_state, iter );
1390 0 : iter = deq_fd_landed_vote_t_iter_next( new_state, iter ) ) {
1391 0 : fd_landed_vote_t * new_vote = deq_fd_landed_vote_t_iter_ele( new_state, iter );
1392 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L738
1393 0 : if( FD_UNLIKELY( new_vote->latency == 0 ) ) {
1394 : // this is unlikely because as validators upgrade, it should converge to the new vote state
1395 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L739
1396 0 : new_vote->latency = compute_vote_latency( new_vote->lockout.slot, current_slot );
1397 0 : }
1398 0 : }
1399 :
1400 : // doesn't matter what the value of slot if `is_some = 0` i.e. `Option::None`
1401 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L744
1402 0 : int both_none = !vote_state->has_root_slot && !has_new_root;
1403 0 : if( ( !both_none && ( vote_state->has_root_slot != has_new_root ||
1404 0 : vote_state->root_slot != new_root ) ) ) {
1405 0 : increment_credits( vote_state, epoch, earned_credits );
1406 0 : }
1407 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L750
1408 0 : if( FD_LIKELY( has_timestamp ) ) {
1409 : /* new_state asserted nonempty at function beginning */
1410 0 : if( deq_fd_landed_vote_t_empty( new_state ) ) {
1411 0 : FD_LOG_ERR(( "solana panic" ));
1412 : // TODO: solana panics ... unclear what to return
1413 0 : ctx->txn_ctx->custom_err = 0;
1414 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1415 0 : }
1416 0 : ulong last_slot = deq_fd_landed_vote_t_peek_tail( new_state )->lockout.slot;
1417 0 : rc = process_timestamp( vote_state, last_slot, timestamp, ctx );
1418 0 : if( FD_UNLIKELY( rc ) ) { return rc; }
1419 0 : vote_state->last_timestamp.timestamp = timestamp;
1420 0 : }
1421 :
1422 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L754
1423 0 : vote_state->has_root_slot = (uchar)has_new_root;
1424 0 : vote_state->root_slot = new_root;
1425 :
1426 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L755
1427 0 : deq_fd_landed_vote_t_remove_all( vote_state->votes );
1428 0 : for( deq_fd_landed_vote_t_iter_t iter = deq_fd_landed_vote_t_iter_init( new_state );
1429 0 : !deq_fd_landed_vote_t_iter_done( new_state, iter );
1430 0 : iter = deq_fd_landed_vote_t_iter_next( new_state, iter ) ) {
1431 0 : fd_landed_vote_t * landed_vote = deq_fd_landed_vote_t_iter_ele( new_state, iter );
1432 0 : deq_fd_landed_vote_t_push_tail_wrap( vote_state->votes, *landed_vote );
1433 0 : }
1434 :
1435 0 : return FD_EXECUTOR_INSTR_SUCCESS;
1436 0 : }
1437 :
1438 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L849
1439 : static int
1440 : authorize( fd_borrowed_account_t * vote_account,
1441 : fd_pubkey_t const * authorized,
1442 : fd_vote_authorize_t vote_authorize,
1443 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1444 : fd_sol_sysvar_clock_t const * clock,
1445 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
1446 0 : int rc = 0;
1447 :
1448 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L857
1449 :
1450 0 : fd_vote_state_versioned_t * vote_state_versioned = get_state( vote_account->acct,
1451 0 : ctx->txn_ctx->spad,
1452 0 : &rc );
1453 0 : if( FD_UNLIKELY( rc ) ) return rc;
1454 0 : convert_to_current( vote_state_versioned, ctx->txn_ctx->spad );
1455 0 : fd_vote_state_t * vote_state = &vote_state_versioned->inner.current;
1456 :
1457 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L861
1458 0 : switch( vote_authorize.discriminant ) {
1459 :
1460 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L862
1461 0 : case fd_vote_authorize_enum_voter:;
1462 :
1463 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L863
1464 0 : int authorized_withdrawer_signer =
1465 0 : FD_EXECUTOR_INSTR_SUCCESS ==
1466 0 : verify_authorized_signer( &vote_state->authorized_withdrawer, signers );
1467 :
1468 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L869-L872
1469 0 : ulong target_epoch;
1470 0 : rc = fd_ulong_checked_add( clock->leader_schedule_epoch, 1UL, &target_epoch );
1471 0 : if( FD_UNLIKELY( rc!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1472 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1473 0 : }
1474 :
1475 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L866
1476 0 : rc = set_new_authorized_voter( vote_state,
1477 0 : authorized,
1478 0 : clock->epoch,
1479 0 : target_epoch,
1480 0 : authorized_withdrawer_signer,
1481 0 : signers,
1482 0 : ctx );
1483 0 : if( FD_UNLIKELY( rc ) ) return rc;
1484 0 : break;
1485 :
1486 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L883
1487 0 : case fd_vote_authorize_enum_withdrawer:
1488 0 : rc = verify_authorized_signer( &vote_state->authorized_withdrawer, signers );
1489 0 : if( FD_UNLIKELY( rc ) ) return rc;
1490 0 : vote_state->authorized_withdrawer = *authorized;
1491 0 : break;
1492 :
1493 : // failing exhaustive check is fatal
1494 0 : default:
1495 0 : __builtin_unreachable();
1496 0 : }
1497 :
1498 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L890
1499 0 : return set_vote_account_state( vote_account, vote_state, ctx );
1500 0 : }
1501 :
1502 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L894
1503 : static int
1504 : update_validator_identity( fd_borrowed_account_t * vote_account,
1505 : fd_pubkey_t const * node_pubkey,
1506 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1507 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
1508 0 : int rc = 0;
1509 :
1510 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L900
1511 0 : fd_vote_state_versioned_t * vote_state_versioned = get_state( vote_account->acct,
1512 0 : ctx->txn_ctx->spad,
1513 0 : &rc );
1514 0 : if( FD_UNLIKELY( rc ) ) return rc;
1515 0 : convert_to_current( vote_state_versioned, ctx->txn_ctx->spad );
1516 0 : fd_vote_state_t * vote_state = &vote_state_versioned->inner.current;
1517 :
1518 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L905
1519 0 : rc = verify_authorized_signer( &vote_state->authorized_withdrawer, signers );
1520 0 : if( FD_UNLIKELY( rc ) ) return rc;
1521 :
1522 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L908
1523 0 : rc = verify_authorized_signer( node_pubkey, signers );
1524 0 : if( FD_UNLIKELY( rc ) ) return rc;
1525 :
1526 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L910
1527 0 : vote_state->node_pubkey = *node_pubkey;
1528 :
1529 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L912
1530 0 : return set_vote_account_state( vote_account, vote_state, ctx );
1531 0 : }
1532 :
1533 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L971
1534 : static int
1535 0 : is_commission_update_allowed( ulong slot, fd_epoch_schedule_t const * epoch_schedule ) {
1536 0 : if( FD_LIKELY( epoch_schedule->slots_per_epoch > 0UL ) ) {
1537 0 : ulong relative_slot = fd_ulong_sat_sub( slot, epoch_schedule->first_normal_slot );
1538 : // TODO underflow and overflow edge cases in addition to div by 0
1539 0 : relative_slot %= epoch_schedule->slots_per_epoch;
1540 0 : return fd_ulong_sat_mul( relative_slot, 2 ) <= epoch_schedule->slots_per_epoch;
1541 0 : } else {
1542 0 : return 1;
1543 0 : }
1544 0 : }
1545 :
1546 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L916
1547 : static int
1548 : update_commission( fd_borrowed_account_t * vote_account,
1549 : uchar commission,
1550 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1551 : fd_epoch_schedule_t const * epoch_schedule,
1552 : fd_sol_sysvar_clock_t const * clock,
1553 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
1554 0 : int rc = 0;
1555 :
1556 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L925
1557 0 : fd_vote_state_versioned_t * vote_state_versioned = NULL;
1558 0 : fd_vote_state_t * vote_state = NULL;
1559 :
1560 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L927
1561 0 : int enforce_commission_update_rule = 1;
1562 0 : vote_state_versioned = get_state( vote_account->acct, ctx->txn_ctx->spad, &rc );
1563 0 : if ( FD_LIKELY( rc==FD_EXECUTOR_INSTR_SUCCESS ) ) {
1564 0 : convert_to_current( vote_state_versioned, ctx->txn_ctx->spad );
1565 0 : vote_state = &vote_state_versioned->inner.current;
1566 0 : enforce_commission_update_rule = commission > vote_state->commission;
1567 0 : }
1568 :
1569 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L940
1570 0 : if( FD_LIKELY( enforce_commission_update_rule ) ) {
1571 0 : if( FD_UNLIKELY( !is_commission_update_allowed( clock->slot, epoch_schedule ) ) ) {
1572 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_COMMISSION_UPDATE_TOO_LATE;
1573 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1574 0 : }
1575 0 : }
1576 :
1577 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L949
1578 0 : if( !vote_state ) {
1579 0 : vote_state_versioned = get_state( vote_account->acct, ctx->txn_ctx->spad, &rc );
1580 0 : if( FD_UNLIKELY( rc ) ) return rc;
1581 0 : convert_to_current( vote_state_versioned, ctx->txn_ctx->spad );
1582 0 : vote_state = &vote_state_versioned->inner.current;
1583 0 : }
1584 :
1585 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L957
1586 0 : rc = verify_authorized_signer( &vote_state->authorized_withdrawer, signers );
1587 0 : if( FD_UNLIKELY( rc ) ) return rc;
1588 :
1589 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L959
1590 0 : vote_state->commission = commission;
1591 :
1592 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L961
1593 0 : return set_vote_account_state( vote_account, vote_state, ctx );
1594 0 : }
1595 :
1596 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L997
1597 : static int
1598 : withdraw( fd_exec_instr_ctx_t const * ctx,
1599 : fd_borrowed_account_t * vote_account,
1600 : ulong lamports,
1601 : ushort to_account_index,
1602 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1603 : fd_rent_t const * rent_sysvar,
1604 0 : fd_sol_sysvar_clock_t const * clock ) {
1605 0 : int rc = 0;
1606 :
1607 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1010
1608 0 : fd_vote_state_versioned_t * vote_state_versioned = get_state( vote_account->acct,
1609 0 : ctx->txn_ctx->spad,
1610 0 : &rc );
1611 0 : if( FD_UNLIKELY( rc ) ) return rc;
1612 0 : convert_to_current( vote_state_versioned, ctx->txn_ctx->spad );
1613 0 : fd_vote_state_t * vote_state = &vote_state_versioned->inner.current;
1614 :
1615 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1014
1616 0 : rc = verify_authorized_signer( &vote_state->authorized_withdrawer, signers );
1617 0 : if( FD_UNLIKELY( rc ) ) return rc;
1618 :
1619 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1016
1620 0 : if( FD_UNLIKELY( lamports > fd_borrowed_account_get_lamports( vote_account ) ) )
1621 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
1622 0 : ulong remaining_balance = fd_borrowed_account_get_lamports( vote_account ) - lamports;
1623 :
1624 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1021
1625 0 : if( FD_UNLIKELY( remaining_balance == 0 ) ) {
1626 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1014
1627 0 : int reject_active_vote_account_close = 0;
1628 :
1629 0 : ulong last_epoch_with_credits;
1630 0 : if( FD_LIKELY( !deq_fd_vote_epoch_credits_t_empty( vote_state->epoch_credits ) ) ) {
1631 0 : last_epoch_with_credits =
1632 0 : deq_fd_vote_epoch_credits_t_peek_tail_const( vote_state->epoch_credits )->epoch;
1633 0 : ulong current_epoch = clock->epoch;
1634 0 : reject_active_vote_account_close =
1635 0 : fd_ulong_sat_sub( current_epoch, last_epoch_with_credits ) < 2;
1636 0 : }
1637 :
1638 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1034
1639 0 : if( FD_UNLIKELY( reject_active_vote_account_close ) ) {
1640 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1036
1641 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_ACTIVE_VOTE_ACCOUNT_CLOSE;
1642 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1643 0 : } else {
1644 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1040
1645 0 : fd_vote_state_versioned_t vote_state_versions;
1646 0 : fd_vote_state_versioned_new_disc( &vote_state_versions,
1647 0 : fd_vote_state_versioned_enum_current );
1648 0 : vote_state_versions.inner.current.prior_voters.idx = 31;
1649 0 : vote_state_versions.inner.current.prior_voters.is_empty = 1;
1650 0 : fd_vote_state_t * default_vote_state = &vote_state_versions.inner.current;
1651 0 : rc = 0;
1652 0 : rc = set_vote_account_state( vote_account, default_vote_state, ctx );
1653 0 : if( FD_UNLIKELY( rc != 0 ) ) return rc;
1654 0 : }
1655 0 : } else {
1656 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1043
1657 0 : ulong min_rent_exempt_balance =
1658 0 : fd_rent_exempt_minimum_balance( rent_sysvar, fd_borrowed_account_get_data_len( vote_account ) );
1659 0 : if( remaining_balance < min_rent_exempt_balance ) {
1660 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
1661 0 : }
1662 0 : }
1663 :
1664 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1049
1665 0 : rc = fd_borrowed_account_checked_sub_lamports( vote_account, lamports );
1666 0 : if( FD_UNLIKELY( rc ) ) return rc;
1667 :
1668 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/vote/src/vote_state/mod.rs#L1019 */
1669 0 : fd_borrowed_account_drop( vote_account );
1670 :
1671 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/vote/src/vote_state/mod.rs#L1020-L1021 */
1672 0 : fd_guarded_borrowed_account_t to;
1673 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, to_account_index, &to );
1674 :
1675 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1053
1676 0 : rc = fd_borrowed_account_checked_add_lamports( &to, lamports );
1677 0 : if( FD_UNLIKELY( rc ) ) return rc;
1678 :
1679 0 : return 0;
1680 0 : }
1681 :
1682 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L760
1683 : static int
1684 : process_vote_unfiltered( fd_vote_state_t * vote_state,
1685 : ulong * vote_slots,
1686 : fd_vote_t const * vote,
1687 : fd_slot_hash_t const * slot_hashes, /* deque */
1688 : ulong epoch,
1689 : ulong current_slot,
1690 0 : fd_exec_instr_ctx_t const * ctx ) {
1691 0 : int rc;
1692 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L770
1693 0 : rc = check_slots_are_valid( vote_state, vote_slots, &vote->hash, slot_hashes, ctx );
1694 0 : if( FD_UNLIKELY( rc ) ) return rc;
1695 0 : for( deq_ulong_iter_t iter = deq_ulong_iter_init( vote_slots );
1696 0 : !deq_ulong_iter_done( vote_slots, iter );
1697 0 : iter = deq_ulong_iter_next( vote_slots, iter ) ) {
1698 0 : ulong * ele = deq_ulong_iter_ele( vote_slots, iter );
1699 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L772
1700 0 : process_next_vote_slot( vote_state, *ele, epoch, current_slot );
1701 0 : }
1702 0 : return 0;
1703 0 : }
1704 :
1705 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L783
1706 : static int
1707 : process_vote( fd_vote_state_t * vote_state,
1708 : fd_vote_t const * vote,
1709 : fd_slot_hash_t const * slot_hashes, /* deque */
1710 : ulong epoch,
1711 : ulong current_slot,
1712 0 : fd_exec_instr_ctx_t const * ctx ) {
1713 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L792
1714 0 : if( FD_UNLIKELY( deq_ulong_empty( vote->slots ) ) ) {
1715 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_EMPTY_SLOTS;
1716 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1717 0 : }
1718 :
1719 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L795
1720 0 : ulong earliest_slot_in_history = 0;
1721 0 : if( FD_UNLIKELY( !deq_fd_slot_hash_t_empty( slot_hashes ) ) ) {
1722 0 : earliest_slot_in_history = deq_fd_slot_hash_t_peek_tail_const( slot_hashes )->slot;
1723 0 : }
1724 :
1725 0 : ulong vote_slots_cnt = deq_ulong_cnt( vote->slots );
1726 0 : uchar * vote_slots_mem = fd_spad_alloc( ctx->txn_ctx->spad, deq_ulong_align(), deq_ulong_footprint( vote_slots_cnt ) );
1727 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L796
1728 0 : ulong * vote_slots = deq_ulong_join( deq_ulong_new( vote_slots_mem, vote_slots_cnt ) );
1729 0 : for( deq_ulong_iter_t iter = deq_ulong_iter_init( vote->slots );
1730 0 : !deq_ulong_iter_done( vote->slots, iter );
1731 0 : iter = deq_ulong_iter_next( vote->slots, iter ) ) {
1732 0 : ulong * ele = deq_ulong_iter_ele( vote->slots, iter );
1733 0 : if( FD_UNLIKELY( *ele >= earliest_slot_in_history ) ) {
1734 0 : vote_slots = deq_ulong_push_tail_wrap( vote_slots, *ele );
1735 0 : }
1736 0 : }
1737 :
1738 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L802
1739 0 : if( FD_UNLIKELY( deq_ulong_cnt( vote_slots ) == 0 ) ) {
1740 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_VOTES_TOO_OLD_ALL_FILTERED;
1741 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1742 0 : }
1743 :
1744 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L805
1745 0 : return process_vote_unfiltered(
1746 0 : vote_state, vote_slots, vote, slot_hashes, epoch, current_slot, ctx );
1747 0 : }
1748 :
1749 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1060
1750 : static int
1751 : initialize_account( fd_borrowed_account_t * vote_account,
1752 : fd_vote_init_t * vote_init,
1753 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1754 : fd_sol_sysvar_clock_t const * clock,
1755 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
1756 0 : int rc;
1757 :
1758 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1067
1759 0 : ulong data_len = fd_borrowed_account_get_data_len( vote_account );
1760 0 : if( FD_UNLIKELY( data_len != size_of_versioned( 1 ) ) ) {
1761 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1762 0 : }
1763 :
1764 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1074
1765 0 : fd_vote_state_versioned_t * versioned = get_state( vote_account->acct,
1766 0 : ctx->txn_ctx->spad,
1767 0 : &rc );
1768 0 : if( FD_UNLIKELY( rc ) ) return rc;
1769 :
1770 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1076
1771 0 : if( FD_UNLIKELY( !is_uninitialized( versioned ) ) ) {
1772 0 : return FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED;
1773 0 : }
1774 :
1775 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1081
1776 0 : rc = verify_authorized_signer( &vote_init->node_pubkey, signers );
1777 0 : if( FD_UNLIKELY( rc ) ) {
1778 0 : return rc;
1779 0 : }
1780 :
1781 : /*
1782 : * N.B. Technically we should destroy() to release memory before
1783 : * newing, otherwise the pointers are wiped and memory is leaked.
1784 : * We are probably fine for now since we are bump allocating
1785 : * everything and the enclosing frame will free everything when
1786 : * popped.
1787 : */
1788 : // reset the object
1789 0 : fd_vote_state_versioned_new( versioned );
1790 :
1791 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1083
1792 0 : vote_state_new( vote_init, clock, ctx->txn_ctx->spad, &versioned->inner.current );
1793 0 : return set_vote_account_state( vote_account, &versioned->inner.current, ctx );
1794 0 : }
1795 :
1796 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1086
1797 : static int
1798 : verify_and_get_vote_state( fd_borrowed_account_t * vote_account,
1799 : fd_sol_sysvar_clock_t const * clock,
1800 : fd_pubkey_t const * signers[FD_TXN_SIG_MAX],
1801 : fd_vote_state_t * vote_state /* out */,
1802 0 : fd_exec_instr_ctx_t const * ctx /* spad */ ) {
1803 0 : int rc = 0;
1804 :
1805 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1091
1806 0 : fd_vote_state_versioned_t * versioned = get_state( vote_account->acct,
1807 0 : ctx->txn_ctx->spad,
1808 0 : &rc );
1809 0 : if( FD_UNLIKELY( rc ) ) return rc;
1810 :
1811 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1093
1812 0 : if( FD_UNLIKELY( is_uninitialized( versioned ) ) )
1813 0 : return FD_EXECUTOR_INSTR_ERR_UNINITIALIZED_ACCOUNT;
1814 :
1815 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1097
1816 0 : convert_to_current( versioned, ctx->txn_ctx->spad );
1817 0 : *vote_state = versioned->inner.current;
1818 :
1819 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1098
1820 0 : fd_pubkey_t * authorized_voter = NULL;
1821 0 : rc = get_and_update_authorized_voter( vote_state, clock->epoch, &authorized_voter, ctx );
1822 0 : if( FD_UNLIKELY( rc ) ) return rc;
1823 :
1824 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1099
1825 0 : rc = verify_authorized_signer( authorized_voter, signers );
1826 0 : if( FD_UNLIKELY( rc ) ) return rc;
1827 :
1828 0 : return FD_EXECUTOR_INSTR_SUCCESS;
1829 0 : }
1830 :
1831 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1104
1832 : static int
1833 : process_vote_with_account( fd_borrowed_account_t * vote_account,
1834 : fd_slot_hash_t const * slot_hashes, /* deque */
1835 : fd_sol_sysvar_clock_t const * clock,
1836 : fd_vote_t * vote,
1837 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1838 0 : fd_exec_instr_ctx_t const * ctx ) {
1839 :
1840 0 : int rc;
1841 0 : fd_vote_state_t vote_state;
1842 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1112
1843 0 : rc = verify_and_get_vote_state( vote_account, clock, signers, &vote_state, ctx );
1844 0 : if( FD_UNLIKELY( rc ) ) return rc;
1845 :
1846 :
1847 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1117
1848 0 : rc = process_vote( &vote_state, vote, slot_hashes, clock->epoch, clock->slot, ctx );
1849 0 : if( FD_UNLIKELY( rc ) ) return rc;
1850 :
1851 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1126
1852 0 : if( FD_LIKELY( vote->has_timestamp ) ) {
1853 0 : if( FD_UNLIKELY( deq_ulong_cnt( vote->slots ) == 0 ) ) {
1854 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_EMPTY_SLOTS;
1855 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1856 0 : }
1857 :
1858 0 : ulong max = deq_ulong_peek_head( vote->slots ) ? *deq_ulong_peek_head( vote->slots ) : 0UL;
1859 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1127
1860 0 : for( deq_ulong_iter_t iter = deq_ulong_iter_init( vote->slots );
1861 0 : !deq_ulong_iter_done( vote->slots, iter );
1862 0 : iter = deq_ulong_iter_next( vote->slots, iter ) ) {
1863 0 : ulong * ele = deq_ulong_iter_ele( vote->slots, iter );
1864 0 : max = fd_ulong_max( max, *ele );
1865 0 : }
1866 0 : if( FD_UNLIKELY( !max ) ) {
1867 0 : ctx->txn_ctx->custom_err = FD_VOTE_ERR_EMPTY_SLOTS;
1868 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1869 0 : }
1870 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1131
1871 0 : rc = process_timestamp( &vote_state, max, vote->timestamp, ctx );
1872 0 : if( FD_UNLIKELY( rc ) ) return rc;
1873 0 : }
1874 :
1875 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1133
1876 0 : return set_vote_account_state( vote_account, &vote_state, ctx );
1877 0 : }
1878 :
1879 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1156
1880 : static int
1881 : do_process_vote_state_update( fd_vote_state_t * vote_state,
1882 : fd_slot_hash_t const * slot_hashes, /* deque */
1883 : ulong epoch,
1884 : ulong slot,
1885 : fd_vote_state_update_t * vote_state_update,
1886 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
1887 0 : int rc;
1888 :
1889 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1164
1890 0 : rc = check_and_filter_proposed_vote_state(
1891 0 : vote_state,
1892 0 : vote_state_update->lockouts, &vote_state_update->has_root, &vote_state_update->root, &vote_state_update->hash,
1893 0 : slot_hashes, ctx );
1894 0 : if( FD_UNLIKELY( rc ) ) return rc;
1895 :
1896 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1177
1897 0 : uchar * deque_mem = fd_spad_alloc( ctx->txn_ctx->spad,
1898 0 : deq_fd_landed_vote_t_align(),
1899 0 : deq_fd_landed_vote_t_footprint( deq_fd_vote_lockout_t_cnt( vote_state_update->lockouts ) ) );
1900 :
1901 0 : fd_landed_vote_t * landed_votes = deq_fd_landed_vote_t_join( deq_fd_landed_vote_t_new( deque_mem, deq_fd_vote_lockout_t_cnt( vote_state_update->lockouts ) ) );
1902 0 : for( deq_fd_vote_lockout_t_iter_t iter =
1903 0 : deq_fd_vote_lockout_t_iter_init( vote_state_update->lockouts );
1904 0 : !deq_fd_vote_lockout_t_iter_done( vote_state_update->lockouts, iter );
1905 0 : iter = deq_fd_vote_lockout_t_iter_next( vote_state_update->lockouts, iter ) ) {
1906 0 : fd_vote_lockout_t * lockout =
1907 0 : deq_fd_vote_lockout_t_iter_ele( vote_state_update->lockouts, iter );
1908 0 : deq_fd_landed_vote_t_push_tail_wrap( landed_votes,
1909 0 : ( fd_landed_vote_t ){ .latency = 0, .lockout = *lockout } );
1910 0 : }
1911 :
1912 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1171
1913 0 : return process_new_vote_state( vote_state,
1914 0 : landed_votes,
1915 0 : vote_state_update->has_root,
1916 0 : vote_state_update->root,
1917 0 : vote_state_update->has_timestamp,
1918 0 : vote_state_update->timestamp,
1919 0 : epoch,
1920 0 : slot,
1921 0 : ctx );
1922 0 : }
1923 :
1924 : static int
1925 : process_vote_state_update( fd_borrowed_account_t * vote_account,
1926 : fd_slot_hash_t const * slot_hashes,
1927 : fd_sol_sysvar_clock_t const * clock,
1928 : fd_vote_state_update_t * vote_state_update,
1929 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1930 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
1931 0 : int rc;
1932 :
1933 : /* A temporary hack to accumulate the stake-weighted bank hash from
1934 : all vote transactions. This determines whether our validator has
1935 : bank hash mismatched. TODO: move to a tile. */
1936 0 : if( FD_LIKELY( !!ctx->txn_ctx->bank_hash_cmp ) ) {
1937 : // tie in code for fd_bank_hash_cmp that helps us detect if we have forked from the cluster.
1938 : //
1939 : // There is no corresponding code in anza
1940 :
1941 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( ctx->txn_ctx->bank );
1942 0 : if( !vote_states ) {
1943 0 : FD_LOG_CRIT(( "vote_states is NULL" ));
1944 0 : }
1945 :
1946 0 : fd_vote_state_ele_t const * vote_state_ele = fd_vote_states_query_const( vote_states, vote_account->acct->pubkey );
1947 0 : if( !vote_state_ele ) {
1948 0 : FD_LOG_CRIT(( "vote_state is NULL" ));
1949 0 : }
1950 :
1951 0 : if( !deq_fd_vote_lockout_t_empty( vote_state_update->lockouts ) ) {
1952 0 : fd_vote_lockout_t * lockout = deq_fd_vote_lockout_t_peek_tail( vote_state_update->lockouts );
1953 0 : fd_bank_hash_cmp_t * bank_hash_cmp = ctx->txn_ctx->bank_hash_cmp;
1954 0 : if( FD_LIKELY( lockout && bank_hash_cmp ) ) {
1955 0 : fd_bank_hash_cmp_lock( bank_hash_cmp );
1956 0 : fd_bank_hash_cmp_insert(
1957 0 : bank_hash_cmp,
1958 0 : lockout->slot,
1959 0 : &vote_state_update->hash,
1960 0 : 0,
1961 0 : vote_state_ele->stake );
1962 0 : fd_bank_hash_cmp_unlock( bank_hash_cmp );
1963 0 : }
1964 0 : }
1965 :
1966 0 : fd_bank_vote_states_end_locking_query( ctx->txn_ctx->bank );
1967 0 : }
1968 :
1969 0 : fd_vote_state_t vote_state;
1970 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1144
1971 0 : rc = verify_and_get_vote_state( vote_account, clock, signers, &vote_state, ctx );
1972 0 : if( FD_UNLIKELY( rc ) ) return rc;
1973 :
1974 :
1975 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1145
1976 0 : rc = do_process_vote_state_update(
1977 0 : &vote_state, slot_hashes, clock->epoch, clock->slot, vote_state_update, ctx );
1978 0 : if( FD_UNLIKELY( rc ) ) {
1979 0 : return rc;
1980 0 : }
1981 :
1982 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1153
1983 0 : rc = set_vote_account_state( vote_account, &vote_state, ctx );
1984 :
1985 0 : return rc;
1986 0 : }
1987 :
1988 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1206
1989 : static int
1990 : do_process_tower_sync( fd_vote_state_t * vote_state,
1991 : fd_slot_hash_t const * slot_hashes, /* deque */
1992 : ulong epoch,
1993 : ulong slot,
1994 : fd_tower_sync_t * tower_sync,
1995 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
1996 :
1997 0 : do {
1998 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1214
1999 0 : int err = check_and_filter_proposed_vote_state(
2000 0 : vote_state,
2001 0 : tower_sync->lockouts, &tower_sync->has_root, &tower_sync->root, &tower_sync->hash,
2002 0 : slot_hashes, ctx );
2003 0 : if( FD_UNLIKELY( err ) ) return err;
2004 0 : } while(0);
2005 :
2006 0 : int err;
2007 0 : FD_SPAD_FRAME_BEGIN( ctx->txn_ctx->spad ) {
2008 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1221
2009 0 : err = process_new_vote_state(
2010 0 : vote_state,
2011 0 : landed_votes_from_lockouts( tower_sync->lockouts, ctx->txn_ctx->spad ),
2012 0 : tower_sync->has_root,
2013 0 : tower_sync->root,
2014 0 : tower_sync->has_timestamp,
2015 0 : tower_sync->timestamp,
2016 0 : epoch,
2017 0 : slot,
2018 0 : ctx );
2019 0 : } FD_SPAD_FRAME_END;
2020 :
2021 0 : return err;
2022 0 : }
2023 :
2024 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1186
2025 : static int
2026 : process_tower_sync( fd_borrowed_account_t * vote_account,
2027 : fd_slot_hash_t const * slot_hashes, /* deque */
2028 : fd_sol_sysvar_clock_t const * clock,
2029 : fd_tower_sync_t * tower_sync,
2030 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
2031 0 : fd_exec_instr_ctx_t const * ctx /* feature_set */ ) {
2032 : /* A temporary hack to accumulate the stake-weighted bank hash from
2033 : all vote transactions. This determines whether our validator has
2034 : bank hash mismatched. TODO: move to a tile. */
2035 0 : if( FD_LIKELY( !!ctx->txn_ctx->bank_hash_cmp ) ) {
2036 0 : if( !deq_fd_vote_lockout_t_empty( tower_sync->lockouts ) ) {
2037 0 : fd_vote_lockout_t * lockout = deq_fd_vote_lockout_t_peek_tail( tower_sync->lockouts );
2038 0 : fd_bank_hash_cmp_t * bank_hash_cmp = ctx->txn_ctx->bank_hash_cmp;
2039 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( ctx->txn_ctx->bank );
2040 0 : if( !vote_states ) {
2041 0 : FD_LOG_CRIT(( "vote_states is NULL" ));
2042 0 : }
2043 0 : fd_vote_state_ele_t const * vote_state_ele = fd_vote_states_query_const( vote_states, vote_account->acct->pubkey );
2044 0 : if( FD_LIKELY( lockout && bank_hash_cmp && vote_state_ele ) ) {
2045 0 : fd_bank_hash_cmp_lock( bank_hash_cmp );
2046 0 : fd_bank_hash_cmp_insert(
2047 0 : bank_hash_cmp,
2048 0 : lockout->slot,
2049 0 : &tower_sync->hash,
2050 0 : 0,
2051 0 : vote_state_ele->stake );
2052 0 : fd_bank_hash_cmp_unlock( bank_hash_cmp );
2053 0 : }
2054 0 : fd_bank_vote_states_end_locking_query( ctx->txn_ctx->bank );
2055 0 : }
2056 0 : }
2057 :
2058 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1194
2059 0 : fd_vote_state_t vote_state;
2060 0 : do {
2061 0 : int err = verify_and_get_vote_state( vote_account, clock, signers, &vote_state, ctx );
2062 0 : if( FD_UNLIKELY( err ) ) return err;
2063 0 : } while(0);
2064 :
2065 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1195
2066 0 : do {
2067 0 : int err = do_process_tower_sync( &vote_state, slot_hashes, clock->epoch, clock->slot, tower_sync, ctx );
2068 0 : if( FD_UNLIKELY( err ) ) return err;
2069 0 : } while(0);
2070 :
2071 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L1203
2072 0 : return set_vote_account_state( vote_account, &vote_state, ctx );
2073 0 : }
2074 :
2075 : /**********************************************************************/
2076 : /* FD-only encoders / decoders (doesn't map directly to Labs impl) */
2077 : /**********************************************************************/
2078 :
2079 : int
2080 : fd_vote_decode_compact_update( fd_compact_vote_state_update_t * compact_update,
2081 : fd_vote_state_update_t * vote_update,
2082 0 : fd_exec_instr_ctx_t const * ctx /* spad */ ) {
2083 : // Taken from:
2084 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L954
2085 0 : if( compact_update->root != ULONG_MAX ) {
2086 0 : vote_update->has_root = 1;
2087 0 : vote_update->root = compact_update->root;
2088 0 : } else {
2089 0 : vote_update->has_root = 0;
2090 0 : vote_update->root = ULONG_MAX;
2091 0 : }
2092 :
2093 0 : ulong lockouts_len = compact_update->lockouts_len;
2094 0 : ulong lockouts_max = fd_ulong_max( lockouts_len, MAX_LOCKOUT_HISTORY );
2095 :
2096 0 : uchar * deque_mem = fd_spad_alloc( ctx->txn_ctx->spad,
2097 0 : deq_fd_vote_lockout_t_align(),
2098 0 : deq_fd_vote_lockout_t_footprint( lockouts_max ) );
2099 0 : vote_update->lockouts = deq_fd_vote_lockout_t_join( deq_fd_vote_lockout_t_new( deque_mem, lockouts_max ) );
2100 0 : ulong slot = fd_ulong_if( vote_update->has_root, vote_update->root, 0 );
2101 :
2102 0 : for( ulong i=0; i < lockouts_len; ++i ) {
2103 0 : fd_vote_lockout_t * elem = deq_fd_vote_lockout_t_push_tail_nocopy( vote_update->lockouts );
2104 0 : fd_vote_lockout_new( elem );
2105 :
2106 0 : fd_lockout_offset_t * lock_offset = &compact_update->lockouts[i];
2107 :
2108 0 : ulong next_slot;
2109 0 : if( FD_UNLIKELY( __builtin_uaddl_overflow( slot, lock_offset->offset, &next_slot ) ) )
2110 0 : return 0;
2111 :
2112 0 : elem->slot = slot = next_slot;
2113 0 : elem->confirmation_count = (uint)lock_offset->confirmation_count;
2114 0 : }
2115 :
2116 0 : vote_update->hash = compact_update->hash;
2117 0 : vote_update->has_timestamp = compact_update->has_timestamp;
2118 0 : vote_update->timestamp = compact_update->timestamp;
2119 :
2120 0 : return 1;
2121 0 : }
2122 :
2123 : /// returns commission split as (voter_portion, staker_portion, was_split) tuple
2124 : ///
2125 : /// if commission calculation is 100% one way or other, indicate with false for was_split
2126 :
2127 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L543
2128 : void
2129 : fd_vote_commission_split( uchar commission,
2130 : ulong on,
2131 0 : fd_commission_split_t * result ) {
2132 0 : uint commission_split = fd_uint_min( (uint)commission, 100 );
2133 0 : result->is_split = ( commission_split != 0 && commission_split != 100 );
2134 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L545
2135 0 : if( commission_split == 0 ) {
2136 0 : result->voter_portion = 0;
2137 0 : result->staker_portion = on;
2138 0 : return;
2139 0 : }
2140 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L546
2141 0 : if( commission_split == 100 ) {
2142 0 : result->voter_portion = on;
2143 0 : result->staker_portion = 0;
2144 0 : return;
2145 0 : }
2146 : /* Note: order of operations may matter for int division. That's why I didn't make the
2147 : * optimization of getting out the common calculations */
2148 :
2149 : // ... This is copied from the solana comments...
2150 : //
2151 : // Calculate mine and theirs independently and symmetrically instead
2152 : // of using the remainder of the other to treat them strictly
2153 : // equally. This is also to cancel the rewarding if either of the
2154 : // parties should receive only fractional lamports, resulting in not
2155 : // being rewarded at all. Thus, note that we intentionally discard
2156 : // any residual fractional lamports.
2157 :
2158 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L548
2159 0 : result->voter_portion =
2160 0 : (ulong)( (uint128)on * (uint128)commission_split / (uint128)100 );
2161 0 : result->staker_portion =
2162 0 : (ulong)( (uint128)on * (uint128)( 100 - commission_split ) / (uint128)100 );
2163 0 : }
2164 :
2165 : /**********************************************************************/
2166 : /* mod vote_processor */
2167 : /**********************************************************************/
2168 :
2169 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L21
2170 : static int
2171 : process_authorize_with_seed_instruction(
2172 : /* invoke_context */
2173 : fd_exec_instr_ctx_t const * ctx,
2174 : /* transaction_context */
2175 : fd_borrowed_account_t * vote_account,
2176 : fd_pubkey_t const * new_authority,
2177 : fd_vote_authorize_t authorization_type,
2178 : fd_pubkey_t const * current_authority_derived_key_owner,
2179 : uchar const * current_authority_derived_key_seed,
2180 0 : ulong current_authority_derived_key_seed_len ) {
2181 0 : int rc = 0;
2182 :
2183 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L31
2184 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_clock_id );
2185 0 : if( FD_UNLIKELY( rc ) ) return rc;
2186 :
2187 0 : fd_sol_sysvar_clock_t clock_;
2188 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2189 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2190 :
2191 0 : fd_pubkey_t * expected_authority_keys[FD_TXN_SIG_MAX] = { 0 };
2192 0 : fd_pubkey_t single_signer = { 0 };
2193 :
2194 0 : if( ctx->instr->acct_cnt < 3 )
2195 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2196 :
2197 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L33
2198 0 : if( fd_instr_acc_is_signer_idx( ctx->instr, 2 ) ) {
2199 :
2200 : // https://github.com/anza-xyz/agave/blob/v2.1.14/programs/vote/src/vote_processor.rs#L34
2201 0 : fd_pubkey_t const * base_pubkey = NULL;
2202 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 2UL, &base_pubkey );
2203 0 : if( FD_UNLIKELY( rc ) ) return rc;
2204 :
2205 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L37
2206 0 : expected_authority_keys[0] = &single_signer;
2207 0 : rc = fd_pubkey_create_with_seed( ctx,
2208 0 : base_pubkey->uc,
2209 0 : (char const *)current_authority_derived_key_seed,
2210 0 : current_authority_derived_key_seed_len,
2211 0 : current_authority_derived_key_owner->uc,
2212 0 : /* insert */ expected_authority_keys[0]->uc );
2213 0 : if( FD_UNLIKELY( rc ) ) return rc;
2214 0 : }
2215 :
2216 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L43
2217 0 : return authorize( vote_account,
2218 0 : new_authority,
2219 0 : authorization_type,
2220 0 : (fd_pubkey_t const **)expected_authority_keys,
2221 0 : clock,
2222 0 : ctx );
2223 0 : }
2224 :
2225 : /**********************************************************************/
2226 : /* Entry point for the Vote Program */
2227 : /**********************************************************************/
2228 :
2229 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L57
2230 : int
2231 0 : fd_vote_program_execute( fd_exec_instr_ctx_t * ctx ) {
2232 : /* FD-specific init */
2233 0 : int rc = FD_EXECUTOR_INSTR_SUCCESS;
2234 :
2235 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L57
2236 0 : FD_EXEC_CU_UPDATE( ctx, DEFAULT_COMPUTE_UNITS );
2237 :
2238 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L64
2239 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) ) {
2240 0 : return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
2241 0 : }
2242 :
2243 0 : fd_guarded_borrowed_account_t me;
2244 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, 0, &me );
2245 :
2246 0 : switch( rc ) {
2247 0 : case FD_ACC_MGR_SUCCESS:
2248 0 : break;
2249 0 : case FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT:
2250 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/src/transaction_context.rs#L637
2251 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2252 0 : default:
2253 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/src/transaction_context.rs#L639
2254 0 : return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
2255 0 : }
2256 :
2257 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L65
2258 0 : if( FD_UNLIKELY( 0 != memcmp( fd_borrowed_account_get_owner( &me ),
2259 0 : fd_solana_vote_program_id.key,
2260 0 : sizeof( fd_pubkey_t ) ) ) ) {
2261 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L66
2262 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
2263 0 : }
2264 :
2265 : /* Replicate vote account changes to bank caches after processing the
2266 : transaction's instructions. */
2267 0 : ctx->txn_ctx->dirty_vote_acc = 1;
2268 :
2269 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L69
2270 0 : fd_pubkey_t const * signers[FD_TXN_SIG_MAX] = { 0 };
2271 0 : fd_exec_instr_ctx_get_signers( ctx, signers );
2272 :
2273 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L70
2274 0 : if( FD_UNLIKELY( ctx->instr->data==NULL ) ) {
2275 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2276 0 : }
2277 :
2278 0 : int decode_result;
2279 0 : ulong decoded_sz;
2280 0 : fd_vote_instruction_t * instruction = fd_bincode_decode1_spad(
2281 0 : vote_instruction, ctx->txn_ctx->spad,
2282 0 : ctx->instr->data, ctx->instr->data_sz,
2283 0 : &decode_result,
2284 0 : &decoded_sz );
2285 0 : if( FD_UNLIKELY( decode_result != FD_BINCODE_SUCCESS ) ) {
2286 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2287 0 : }
2288 0 : if( FD_UNLIKELY( decoded_sz > FD_TXN_MTU ) ) {
2289 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2290 0 : }
2291 :
2292 : /* PLEASE PRESERVE SWITCH-CASE ORDERING TO MIRROR LABS IMPL:
2293 : */
2294 0 : switch( instruction->discriminant ) {
2295 :
2296 : /* InitializeAccount
2297 : *
2298 : * Instruction:
2299 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L32
2300 : *
2301 : * Processor:
2302 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L71
2303 : */
2304 0 : case fd_vote_instruction_enum_initialize_account: {
2305 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L72
2306 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_rent_id );
2307 0 : if( FD_UNLIKELY( rc ) ) return rc;
2308 0 : fd_rent_t rent_;
2309 0 : fd_rent_t const * rent = fd_sysvar_cache_rent_read( ctx->sysvar_cache, &rent_ );
2310 0 : if( FD_UNLIKELY( !rent ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2311 :
2312 0 : if( FD_UNLIKELY( fd_borrowed_account_get_lamports( &me ) <
2313 0 : fd_rent_exempt_minimum_balance( rent, fd_borrowed_account_get_data_len( &me ) ) ) )
2314 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
2315 :
2316 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L76
2317 0 : rc = fd_sysvar_instr_acct_check( ctx, 2, &fd_sysvar_clock_id );
2318 0 : if( FD_UNLIKELY( rc ) ) return rc;
2319 0 : fd_sol_sysvar_clock_t clock_;
2320 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2321 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2322 :
2323 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L78
2324 0 : rc = initialize_account( &me,
2325 0 : &instruction->inner.initialize_account,
2326 0 : signers,
2327 0 : clock,
2328 0 : ctx );
2329 :
2330 0 : break;
2331 0 : }
2332 :
2333 : /* Authorize
2334 : *
2335 : * Instruction:
2336 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L40
2337 : *
2338 : * Processor:
2339 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L86
2340 : *
2341 : * Notes:
2342 : * - Up to two signers: the vote authority and the authorized withdrawer.
2343 : */
2344 0 : case fd_vote_instruction_enum_authorize: {
2345 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L87
2346 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_clock_id );
2347 0 : if( FD_UNLIKELY( rc ) ) return rc;
2348 0 : fd_sol_sysvar_clock_t clock_;
2349 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2350 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2351 :
2352 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L89
2353 0 : fd_pubkey_t const * voter_pubkey = &instruction->inner.authorize.pubkey;
2354 0 : fd_vote_authorize_t vote_authorize = instruction->inner.authorize.vote_authorize;
2355 :
2356 0 : rc = authorize( &me, voter_pubkey, vote_authorize, signers, clock, ctx );
2357 :
2358 0 : break;
2359 0 : }
2360 :
2361 : /* AuthorizeWithSeed
2362 : *
2363 : * Instruction:
2364 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L117
2365 : *
2366 : * Processor:
2367 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L98
2368 : */
2369 0 : case fd_vote_instruction_enum_authorize_with_seed: {
2370 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L99
2371 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 3 ) ) {
2372 0 : rc = FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
2373 0 : break;
2374 0 : }
2375 :
2376 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L100
2377 0 : fd_vote_authorize_with_seed_args_t * args = &instruction->inner.authorize_with_seed;
2378 :
2379 0 : rc = process_authorize_with_seed_instruction( ctx,
2380 0 : &me,
2381 0 : &args->new_authority,
2382 0 : args->authorization_type,
2383 0 : &args->current_authority_derived_key_owner,
2384 0 : args->current_authority_derived_key_seed,
2385 0 : args->current_authority_derived_key_seed_len );
2386 :
2387 0 : break;
2388 0 : }
2389 :
2390 : /* AuthorizeCheckedWithSeed
2391 : *
2392 : * Instruction:
2393 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L131
2394 : *
2395 : * Processor:
2396 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L111
2397 : */
2398 0 : case fd_vote_instruction_enum_authorize_checked_with_seed: {
2399 0 : fd_vote_authorize_checked_with_seed_args_t const * args =
2400 0 : &instruction->inner.authorize_checked_with_seed;
2401 :
2402 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L112
2403 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 4 ) ) {
2404 0 : rc = FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
2405 0 : break;
2406 0 : }
2407 :
2408 : // https://github.com/anza-xyz/agave/blob/v2.1.14/programs/vote/src/vote_processor.rs#L99-L100
2409 0 : fd_pubkey_t const * new_authority = NULL;
2410 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 3UL, &new_authority );
2411 0 : if( FD_UNLIKELY( rc ) ) return rc;
2412 :
2413 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L116
2414 0 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx->instr, 3 ) ) ) {
2415 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L117
2416 0 : rc = FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
2417 0 : break;
2418 0 : }
2419 :
2420 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L119
2421 0 : rc = process_authorize_with_seed_instruction( ctx,
2422 0 : &me,
2423 0 : new_authority,
2424 0 : args->authorization_type,
2425 0 : &args->current_authority_derived_key_owner,
2426 0 : args->current_authority_derived_key_seed,
2427 0 : args->current_authority_derived_key_seed_len );
2428 :
2429 0 : break;
2430 0 : }
2431 :
2432 : /* UpdateValidatorIdentity
2433 : *
2434 : * Instruction:
2435 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L65
2436 : *
2437 : * Processor:
2438 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L130
2439 : */
2440 0 : case fd_vote_instruction_enum_update_validator_identity: {
2441 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L131
2442 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 2 ) ) {
2443 0 : rc = FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
2444 0 : break;
2445 0 : }
2446 :
2447 : // https://github.com/anza-xyz/agave/blob/v2.1.14/programs/vote/src/vote_processor.rs#L118-L120
2448 0 : fd_pubkey_t const * node_pubkey = NULL;
2449 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 1UL, &node_pubkey );
2450 0 : if( FD_UNLIKELY( rc ) ) return rc;
2451 :
2452 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L135
2453 0 : rc = update_validator_identity( &me, node_pubkey, signers, ctx );
2454 :
2455 0 : break;
2456 0 : }
2457 :
2458 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L142
2459 0 : case fd_vote_instruction_enum_update_commission: {
2460 :
2461 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L149
2462 0 : fd_epoch_schedule_t epoch_schedule_;
2463 0 : fd_epoch_schedule_t const * epoch_schedule = fd_sysvar_cache_epoch_schedule_read( ctx->sysvar_cache, &epoch_schedule_ );
2464 0 : if( FD_UNLIKELY( !epoch_schedule ) ) {
2465 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2466 0 : }
2467 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L150
2468 :
2469 0 : fd_sol_sysvar_clock_t clock_;
2470 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2471 0 : if( FD_UNLIKELY( !clock ) ) {
2472 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2473 0 : }
2474 :
2475 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L145
2476 0 : rc = update_commission( &me,
2477 0 : instruction->inner.update_commission,
2478 0 : signers,
2479 0 : epoch_schedule,
2480 0 : clock,
2481 0 : ctx );
2482 :
2483 0 : break;
2484 0 : }
2485 :
2486 : /* Vote
2487 : *
2488 : * Instruction:
2489 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L49
2490 : */
2491 0 : case fd_vote_instruction_enum_vote:;
2492 : /* clang-format off */
2493 0 : __attribute__((fallthrough));
2494 : /* clang-format on */
2495 :
2496 : /* VoteSwitch
2497 : *
2498 : * Instruction:
2499 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L81
2500 : *
2501 : * Processor:
2502 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L154
2503 : */
2504 0 : case fd_vote_instruction_enum_vote_switch: {
2505 0 : if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, deprecate_legacy_vote_ixs ) ) {
2506 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2507 0 : }
2508 :
2509 0 : fd_vote_t * vote;
2510 0 : if( instruction->discriminant == fd_vote_instruction_enum_vote ) {
2511 0 : vote = &instruction->inner.vote;
2512 0 : } else if( instruction->discriminant == fd_vote_instruction_enum_vote_switch ) {
2513 0 : vote = &instruction->inner.vote_switch.vote;
2514 0 : } else {
2515 0 : __builtin_unreachable();
2516 0 : }
2517 :
2518 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L155
2519 0 : int err;
2520 0 : err = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_slot_hashes_id );
2521 0 : if( FD_UNLIKELY( err ) ) return err;
2522 :
2523 0 : if( FD_UNLIKELY( !fd_sysvar_cache_slot_hashes_is_valid( ctx->sysvar_cache ) ) ) {
2524 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2525 0 : }
2526 :
2527 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L157
2528 0 : err = fd_sysvar_instr_acct_check( ctx, 2, &fd_sysvar_clock_id );
2529 0 : if( FD_UNLIKELY( err ) ) return err;
2530 0 : fd_sol_sysvar_clock_t clock_;
2531 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2532 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2533 :
2534 0 : fd_slot_hash_t const * slot_hashes = fd_sysvar_cache_slot_hashes_join_const( ctx->sysvar_cache ); /* guaranteed to succeed */
2535 0 : rc = process_vote_with_account( &me, slot_hashes, clock, vote, signers, ctx );
2536 0 : fd_sysvar_cache_slot_hashes_leave_const( ctx->sysvar_cache, slot_hashes );
2537 :
2538 0 : break;
2539 0 : }
2540 :
2541 : /* UpdateVoteState
2542 : *
2543 : * Instruction:
2544 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L100
2545 : */
2546 0 : case fd_vote_instruction_enum_update_vote_state:;
2547 : /* clang-format off */
2548 0 : __attribute__((fallthrough));
2549 : /* clang-format on */
2550 :
2551 : /* UpdateVoteStateSwitch
2552 : *
2553 : * Instruction:
2554 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L107
2555 : *
2556 : * Processor:
2557 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L169
2558 : */
2559 0 : case fd_vote_instruction_enum_update_vote_state_switch: {
2560 0 : if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, deprecate_legacy_vote_ixs ) ) {
2561 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2562 0 : }
2563 :
2564 0 : fd_vote_state_update_t * vote_state_update;
2565 0 : switch( instruction->discriminant ) {
2566 0 : case fd_vote_instruction_enum_update_vote_state:
2567 0 : vote_state_update = &instruction->inner.update_vote_state;
2568 0 : break;
2569 0 : case fd_vote_instruction_enum_update_vote_state_switch:
2570 0 : vote_state_update = &instruction->inner.update_vote_state_switch.vote_state_update;
2571 0 : break;
2572 0 : default:
2573 0 : __builtin_unreachable();
2574 0 : }
2575 :
2576 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L171
2577 0 : if( FD_LIKELY( !fd_sysvar_cache_slot_hashes_is_valid( ctx->sysvar_cache ) ) ) {
2578 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2579 0 : }
2580 :
2581 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L172
2582 0 : fd_sol_sysvar_clock_t clock_;
2583 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2584 0 : if( FD_UNLIKELY( !clock ) )
2585 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2586 :
2587 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L173
2588 0 : fd_slot_hash_t const * slot_hashes = fd_sysvar_cache_slot_hashes_join_const( ctx->sysvar_cache );
2589 0 : rc = process_vote_state_update( &me, slot_hashes, clock, vote_state_update, signers, ctx );
2590 0 : fd_sysvar_cache_slot_hashes_leave_const( ctx->sysvar_cache, slot_hashes );
2591 :
2592 0 : break;
2593 0 : }
2594 :
2595 : /* CompactUpdateVoteState
2596 : *
2597 : * Instruction:
2598 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L139
2599 : *
2600 : * Notes:
2601 : * - Up to three signers: the vote authority, the authorized withdrawer, and the new authority.
2602 : * - Feature gated, but live on mainnet.
2603 : */
2604 0 : case fd_vote_instruction_enum_compact_update_vote_state:;
2605 0 : __attribute__((fallthrough));
2606 :
2607 : /* CompactUpdateVoteStateSwitch
2608 : *
2609 : * Instruction:
2610 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L146
2611 : *
2612 : * Processor:
2613 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L183
2614 : *
2615 : * Notes:
2616 : * - Up to three signers: the vote authority, the authorized withdrawer, and the new authority.
2617 : * - Feature gated, but live on mainnet.
2618 : */
2619 0 : case fd_vote_instruction_enum_compact_update_vote_state_switch: {
2620 : /* https://github.com/anza-xyz/agave/blob/dc4b9dcbbf859ff48f40d00db824bde063fdafcc/programs/vote/src/vote_processor.rs#L183-L191 */
2621 0 : if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, deprecate_legacy_vote_ixs ) ) {
2622 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2623 0 : }
2624 :
2625 0 : fd_compact_vote_state_update_t * vote_state_update = NULL;
2626 0 : if( instruction->discriminant == fd_vote_instruction_enum_compact_update_vote_state ) {
2627 0 : vote_state_update = &instruction->inner.compact_update_vote_state;
2628 0 : } else if( instruction->discriminant ==
2629 0 : fd_vote_instruction_enum_compact_update_vote_state_switch ) {
2630 0 : vote_state_update =
2631 0 : &instruction->inner.compact_update_vote_state_switch.compact_vote_state_update;
2632 0 : }
2633 :
2634 0 : fd_vote_state_update_t vote_update;
2635 0 : fd_vote_state_update_new( &vote_update );
2636 0 : if( FD_UNLIKELY( !fd_vote_decode_compact_update( vote_state_update, &vote_update, ctx ) ) )
2637 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2638 :
2639 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L185
2640 0 : if( FD_LIKELY( !fd_sysvar_cache_slot_hashes_is_valid( ctx->sysvar_cache ) ) ) {
2641 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2642 0 : }
2643 :
2644 0 : fd_sol_sysvar_clock_t clock_;
2645 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2646 0 : if( FD_UNLIKELY( !clock ) )
2647 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2648 :
2649 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L187
2650 0 : fd_slot_hash_t const * slot_hashes = fd_sysvar_cache_slot_hashes_join_const( ctx->sysvar_cache ); /* guaranteed to succeed */
2651 0 : rc = process_vote_state_update( &me, slot_hashes, clock, &vote_update, signers, ctx );
2652 0 : fd_sysvar_cache_slot_hashes_leave_const( ctx->sysvar_cache, slot_hashes );
2653 :
2654 0 : break;
2655 0 : }
2656 :
2657 : /* TowerSync(Switch)
2658 : *
2659 : * Instruction:
2660 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L151-L157
2661 : *
2662 : * Processor:
2663 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L196-L215
2664 : */
2665 :
2666 0 : case fd_vote_instruction_enum_tower_sync:
2667 0 : case fd_vote_instruction_enum_tower_sync_switch: {
2668 0 : fd_tower_sync_t * tower_sync = (instruction->discriminant == fd_vote_instruction_enum_tower_sync)
2669 0 : ? &instruction->inner.tower_sync
2670 0 : : &instruction->inner.tower_sync_switch.tower_sync;
2671 :
2672 0 : if( FD_LIKELY( !fd_sysvar_cache_slot_hashes_is_valid( ctx->sysvar_cache ) ) ) {
2673 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2674 0 : }
2675 :
2676 0 : fd_sol_sysvar_clock_t clock_;
2677 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2678 0 : if( FD_UNLIKELY( !clock ) ) {
2679 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2680 0 : }
2681 :
2682 0 : fd_slot_hash_t const * slot_hashes = fd_sysvar_cache_slot_hashes_join_const( ctx->sysvar_cache );
2683 0 : FD_TEST( slot_hashes );
2684 0 : rc = process_tower_sync( &me, slot_hashes, clock, tower_sync, signers, ctx );
2685 0 : fd_sysvar_cache_slot_hashes_leave_const( ctx->sysvar_cache, slot_hashes );
2686 :
2687 0 : break;
2688 0 : }
2689 :
2690 : /* Withdraw
2691 : *
2692 : * Instruction:
2693 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L57
2694 : *
2695 : * Processor:
2696 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L216
2697 : */
2698 0 : case fd_vote_instruction_enum_withdraw: {
2699 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 2 ) ) {
2700 0 : rc = FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
2701 0 : break;
2702 0 : }
2703 0 : fd_rent_t rent_;
2704 0 : fd_rent_t const * rent_sysvar = fd_sysvar_cache_rent_read( ctx->sysvar_cache, &rent_ );
2705 0 : if( FD_UNLIKELY( !rent_sysvar ) )
2706 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2707 0 : fd_sol_sysvar_clock_t clock_;
2708 0 : fd_sol_sysvar_clock_t const * clock_sysvar = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2709 0 : if( FD_UNLIKELY( !clock_sysvar ) )
2710 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2711 :
2712 0 : rc = withdraw( ctx,
2713 0 : &me,
2714 0 : instruction->inner.withdraw,
2715 0 : 1UL,
2716 0 : signers,
2717 0 : rent_sysvar,
2718 0 : clock_sysvar );
2719 :
2720 0 : break;
2721 0 : }
2722 :
2723 : /* AuthorizeChecked
2724 : *
2725 : * Instruction:
2726 : * https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/instruction.rs#L93
2727 : *
2728 : * Processor:
2729 : * https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L234
2730 : *
2731 : * Notes:
2732 : * - Up to three signers: the vote authority, the authorized withdrawer, and the new authority.
2733 : * - Feature gated, but live on mainnet.
2734 : */
2735 0 : case fd_vote_instruction_enum_authorize_checked: {
2736 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 4 ) ) {
2737 0 : rc = FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
2738 0 : break;
2739 0 : }
2740 :
2741 : // https://github.com/anza-xyz/agave/blob/v2.1.14/programs/vote/src/vote_processor.rs#L243-L245
2742 0 : fd_pubkey_t const * voter_pubkey = NULL;
2743 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 3UL, &voter_pubkey );
2744 0 : if( FD_UNLIKELY( rc ) ) return rc;
2745 :
2746 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L239
2747 0 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx->instr, 3 ) ) ) {
2748 0 : rc = FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
2749 0 : break;
2750 0 : }
2751 :
2752 : // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L242
2753 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_clock_id );
2754 0 : if( FD_UNLIKELY( rc ) ) return rc;
2755 0 : fd_sol_sysvar_clock_t clock_;
2756 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2757 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2758 :
2759 0 : rc = authorize( &me,
2760 0 : voter_pubkey,
2761 0 : instruction->inner.authorize_checked,
2762 0 : signers,
2763 0 : clock,
2764 0 : ctx );
2765 0 : break;
2766 0 : }
2767 :
2768 0 : default:
2769 0 : FD_LOG_ERR(( "unsupported vote instruction: %u", instruction->discriminant ));
2770 0 : }
2771 :
2772 0 : return rc;
2773 0 : }
2774 :
2775 : /**********************************************************************/
2776 : /* Public API */
2777 : /**********************************************************************/
2778 :
2779 : uint
2780 0 : fd_vote_state_versions_is_correct_and_initialized( fd_txn_account_t * vote_account ) {
2781 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L885
2782 0 : uint data_len_check = fd_txn_account_get_data_len( vote_account )==FD_VOTE_STATE_V3_SZ;
2783 0 : uchar test_data[DEFAULT_PRIOR_VOTERS_OFFSET] = {0};
2784 0 : uint data_check = memcmp((
2785 0 : fd_txn_account_get_data( vote_account )+VERSION_OFFSET), test_data, DEFAULT_PRIOR_VOTERS_OFFSET)!=0;
2786 0 : if (data_check && data_len_check) {
2787 0 : return 1;
2788 0 : }
2789 :
2790 : // VoteState1_14_11::is_correct_size_and_initialized
2791 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/vote_state_1_14_11.rs#L58
2792 0 : data_len_check = fd_txn_account_get_data_len( vote_account )==FD_VOTE_STATE_V2_SZ;
2793 0 : uchar test_data_1_14_11[DEFAULT_PRIOR_VOTERS_OFFSET_1_14_11] = {0};
2794 0 : data_check = memcmp( (
2795 0 : fd_txn_account_get_data( vote_account )+VERSION_OFFSET), test_data_1_14_11, DEFAULT_PRIOR_VOTERS_OFFSET_1_14_11)!=0;
2796 0 : return data_check && data_len_check;
2797 0 : }
2798 :
2799 : int
2800 : fd_vote_get_state( fd_txn_account_t const * self,
2801 : fd_spad_t * spad,
2802 0 : fd_vote_state_versioned_t * * versioned /* out */ ) {
2803 0 : int err = 0;
2804 0 : *versioned = get_state( self, spad, &err );
2805 0 : return err;
2806 0 : }
2807 :
2808 : void
2809 : fd_vote_convert_to_current( fd_vote_state_versioned_t * self,
2810 0 : fd_spad_t * spad ) {
2811 0 : convert_to_current( self, spad );
2812 0 : }
2813 :
2814 : static void
2815 : remove_vote_account( fd_txn_account_t * vote_account,
2816 0 : fd_bank_t * bank ) {
2817 :
2818 0 : fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
2819 0 : fd_vote_states_remove( vote_states, vote_account->pubkey );
2820 0 : fd_bank_vote_states_end_locking_modify( bank );
2821 0 : }
2822 :
2823 : static void
2824 : upsert_vote_account( fd_txn_account_t * vote_account,
2825 0 : fd_bank_t * bank ) {
2826 :
2827 0 : if( fd_vote_state_versions_is_correct_and_initialized( vote_account ) ) {
2828 0 : fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
2829 0 : fd_vote_states_update_from_account(
2830 0 : vote_states,
2831 0 : vote_account->pubkey,
2832 0 : fd_txn_account_get_data( vote_account ),
2833 0 : fd_txn_account_get_data_len( vote_account ) );
2834 0 : fd_bank_vote_states_end_locking_modify( bank );
2835 0 : } else {
2836 0 : remove_vote_account( vote_account, bank );
2837 0 : }
2838 0 : }
2839 :
2840 : void
2841 : fd_vote_store_account( fd_txn_account_t * vote_account,
2842 0 : fd_bank_t * bank ) {
2843 :
2844 0 : if( fd_txn_account_get_lamports( vote_account )==0UL ) {
2845 0 : remove_vote_account( vote_account, bank );
2846 0 : } else {
2847 0 : upsert_vote_account( vote_account, bank );
2848 0 : }
2849 0 : }
|