Line data Source code
1 : #include <limits.h>
2 :
3 : #include "../../../util/bits/fd_sat.h"
4 : #include "../../../util/bits/fd_uwide.h"
5 : #include "../../log_collector/fd_log_collector.h"
6 : #include "../sysvar/fd_sysvar_epoch_schedule.h"
7 : #include "../sysvar/fd_sysvar_rent.h"
8 : #include "../sysvar/fd_sysvar.h"
9 : #include "../fd_borrowed_account.h"
10 : #include "../fd_executor.h"
11 : #include "../fd_pubkey_utils.h"
12 : #include "../fd_system_ids.h"
13 : #include "../fd_runtime.h"
14 : #include "fd_stake_program.h"
15 : #include "fd_vote_program.h"
16 :
17 : /* A note on fd_borrowed_account_acquire_write:
18 :
19 : The stake program uses this function to prevent aliasing of accounts.
20 : (When the same account is passed via multiple instruction account
21 : indexes.) Internally, it acquires a transaction-wide mutex on the
22 : account. If called twice on the same account while the mutex is
23 : still locked, it returns an "AccountBorrowFailed" error.
24 :
25 : There is no exact equivalent to this in Agave/Rust.
26 :
27 : let handle = instruction_context.try_borrow_instruction_account(...)
28 :
29 : The above creates the lock on the account. However, that lock is
30 : **implicitly** released when 'handle' goes out of scope. Firedancer
31 : releases the handle **explicitly**. */
32 :
33 : /* The worst case stake instruction footprint can happen with one of the
34 : dynamically sized instruction types.
35 :
36 : Both these types have a dynamic size based on the size of the seed.
37 : authorize_with_seed
38 : authorize_checked_with_seed
39 :
40 : A loose, but reaosonable bound is that the seed takes up the entire
41 : FD_TXN_MTU to express.
42 :
43 : So, the worst case footprint is the sum of the size of the
44 : instruction and the transaction MTU. */
45 :
46 : #define FD_STAKE_INSTR_FOOTPRINT (FD_TXN_MTU + sizeof(fd_stake_instruction_t))
47 :
48 : /**********************************************************************/
49 : /* Errors */
50 : /**********************************************************************/
51 :
52 : // DO NOT REORDER: https://github.com/bincode-org/bincode/blob/trunk/docs/spec.md#enums
53 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L28
54 : #define FD_STAKE_ERR_NO_CREDITS_TO_REDEEM ( 0 )
55 0 : #define FD_STAKE_ERR_LOCKUP_IN_FORCE ( 1 )
56 0 : #define FD_STAKE_ERR_ALREADY_DEACTIVATED ( 2 )
57 0 : #define FD_STAKE_ERR_TOO_SOON_TO_REDELEGATE ( 3 )
58 0 : #define FD_STAKE_ERR_INSUFFICIENT_STAKE ( 4 )
59 0 : #define FD_STAKE_ERR_MERGE_TRANSIENT_STAKE ( 5 )
60 0 : #define FD_STAKE_ERR_MERGE_MISMATCH ( 6 )
61 0 : #define FD_STAKE_ERR_CUSTODIAN_MISSING ( 7 )
62 0 : #define FD_STAKE_ERR_CUSTODIAN_SIGNATURE_MISSING ( 8 )
63 0 : #define FD_STAKE_ERR_INSUFFICIENT_REFERENCE_VOTES ( 9 )
64 0 : #define FD_STAKE_ERR_VOTE_ADDRESS_MISMATCH ( 10 )
65 0 : #define FD_STAKE_ERR_MINIMUM_DELIQUENT_EPOCHS_FOR_DEACTIVATION_NOT_MET ( 11 )
66 0 : #define FD_STAKE_ERR_INSUFFICIENT_DELEGATION ( 12 )
67 : #define FD_STAKE_ERR_REDELEGATE_TRANSIENT_OR_INACTIVE_STAKE ( 13 )
68 : #define FD_STAKE_ERR_REDELEGATE_TO_SAME_VOTE_ACCOUNT ( 14 )
69 : #define FD_STAKE_ERR_REDELEGATED_STAKE_MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED ( 15 )
70 0 : #define FD_STAKE_ERR_EPOCH_REWARDS_ACTIVE ( 16 )
71 :
72 : /**********************************************************************/
73 : /* Constants */
74 : /**********************************************************************/
75 :
76 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/lib.rs#L31
77 0 : #define MINIMUM_DELEGATION_SOL ( 1 )
78 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/mod.rs#L18
79 0 : #define MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION ( 5 )
80 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L26-L28
81 0 : #define DEFAULT_WARMUP_COOLDOWN_RATE ( 0.25 )
82 0 : #define NEW_WARMUP_COOLDOWN_RATE ( 0.09 )
83 0 : #define DEFAULT_SLASH_PENALTY ( 12 )
84 :
85 : #define STAKE_AUTHORIZE_STAKER \
86 0 : ( ( fd_stake_authorize_t ){ .discriminant = fd_stake_authorize_enum_staker } )
87 : #define STAKE_AUTHORIZE_WITHDRAWER \
88 0 : ( ( fd_stake_authorize_t ){ .discriminant = fd_stake_authorize_enum_withdrawer } )
89 :
90 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L51
91 : #define DEFAULT_COMPUTE_UNITS 750UL
92 :
93 : /**********************************************************************/
94 : /* MergeKind */
95 : /**********************************************************************/
96 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1074-L1079
97 : struct merge_kind_inactive {
98 : fd_stake_meta_t meta;
99 : ulong active_stake;
100 : fd_stake_flags_t stake_flags;
101 : };
102 : typedef struct merge_kind_inactive merge_kind_inactive_t;
103 :
104 : struct merge_kind_activation_epoch {
105 : fd_stake_meta_t meta;
106 : fd_stake_t stake;
107 : fd_stake_flags_t stake_flags;
108 : };
109 : typedef struct merge_kind_activation_epoch merge_kind_activation_epoch_t;
110 :
111 : struct merge_kind_fully_active {
112 : fd_stake_meta_t meta;
113 : fd_stake_t stake;
114 : };
115 : typedef struct merge_kind_fully_active merge_kind_fully_active_t;
116 :
117 : union merge_kind_inner {
118 : merge_kind_inactive_t inactive;
119 : merge_kind_activation_epoch_t activation_epoch;
120 : merge_kind_fully_active_t fully_active;
121 : };
122 : typedef union merge_kind_inner merge_kind_inner_t;
123 :
124 : struct merge_kind {
125 : uint discriminant;
126 : merge_kind_inner_t inner;
127 : };
128 : typedef struct merge_kind merge_kind_t;
129 :
130 : enum { merge_kind_inactive = 0, merge_kind_activation_epoch = 1, merge_kind_fully_active = 2 };
131 :
132 : typedef fd_stake_history_entry_t stake_activation_status_t;
133 :
134 : struct effective_activating {
135 : ulong effective;
136 : ulong activating;
137 : };
138 : typedef struct effective_activating effective_activating_t;
139 :
140 : /**********************************************************************/
141 : /* Bincode */
142 : /**********************************************************************/
143 :
144 : static int
145 : get_state( fd_txn_account_t const * self,
146 0 : fd_stake_state_v2_t * out ) {
147 0 : int rc;
148 :
149 0 : fd_bincode_decode_ctx_t bincode_ctx = {
150 0 : .data = fd_txn_account_get_data( self ),
151 0 : .dataend = fd_txn_account_get_data( self ) + fd_txn_account_get_data_len( self ),
152 0 : };
153 :
154 0 : ulong total_sz = 0UL;
155 0 : rc = fd_stake_state_v2_decode_footprint( &bincode_ctx, &total_sz );
156 0 : if( FD_UNLIKELY( rc!=FD_BINCODE_SUCCESS ) ) {
157 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
158 0 : }
159 :
160 0 : fd_stake_state_v2_decode( out, &bincode_ctx );
161 :
162 0 : return 0;
163 0 : }
164 :
165 : static int
166 : set_state( fd_borrowed_account_t * borrowed_acct,
167 0 : fd_stake_state_v2_t const * state ) {
168 :
169 0 : uchar * data = NULL;
170 0 : ulong dlen = 0UL;
171 :
172 0 : int err = fd_borrowed_account_get_data_mut( borrowed_acct, &data, &dlen );
173 0 : if( FD_UNLIKELY( err ) ) return err;
174 :
175 0 : ulong serialized_size = fd_stake_state_v2_size( state );
176 0 : if( FD_UNLIKELY( serialized_size>dlen ) )
177 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
178 :
179 0 : fd_bincode_encode_ctx_t encode = {
180 0 : .data = data,
181 0 : .dataend = data + serialized_size,
182 0 : };
183 0 : do {
184 0 : int err = fd_stake_state_v2_encode( state, &encode );
185 0 : if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "fd_stake_state_v2_encode failed" ));
186 0 : } while(0);
187 :
188 0 : return 0;
189 0 : }
190 :
191 : /**********************************************************************/
192 : /* mod stake */
193 : /**********************************************************************/
194 :
195 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/lib.rs#L29
196 : static inline ulong
197 0 : get_minimum_delegation( fd_bank_t * bank ) {
198 0 : return fd_ulong_if( FD_FEATURE_ACTIVE_BANK( bank, stake_raise_minimum_delegation_to_1_sol ),
199 0 : MINIMUM_DELEGATION_SOL * LAMPORTS_PER_SOL,
200 0 : 1 );
201 0 : }
202 :
203 : /**********************************************************************/
204 : /* mod stake/state */
205 : /**********************************************************************/
206 :
207 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L30
208 : static inline double
209 0 : warmup_cooldown_rate( ulong current_epoch, ulong * new_rate_activation_epoch ) {
210 0 : return fd_double_if( current_epoch <
211 0 : ( new_rate_activation_epoch ? *new_rate_activation_epoch : ULONG_MAX ),
212 0 : DEFAULT_WARMUP_COOLDOWN_RATE,
213 0 : NEW_WARMUP_COOLDOWN_RATE );
214 0 : }
215 :
216 : /**********************************************************************/
217 : /* validated */
218 : /**********************************************************************/
219 :
220 : struct validated_delegated_info {
221 : ulong stake_amount;
222 : };
223 : typedef struct validated_delegated_info validated_delegated_info_t;
224 :
225 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L963
226 : static int
227 : validate_delegated_amount( fd_bank_t * bank,
228 : fd_borrowed_account_t * account,
229 : fd_stake_meta_t const * meta,
230 : validated_delegated_info_t * out,
231 0 : uint * custom_err ) {
232 0 : ulong stake_amount = fd_ulong_sat_sub( fd_borrowed_account_get_lamports( account ), meta->rent_exempt_reserve );
233 :
234 0 : if( FD_UNLIKELY( stake_amount<get_minimum_delegation( bank ) ) ) {
235 0 : *custom_err = FD_STAKE_ERR_INSUFFICIENT_DELEGATION;
236 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
237 0 : }
238 0 : out->stake_amount = stake_amount;
239 0 : return 0;
240 0 : }
241 :
242 : struct validated_split_info {
243 : ulong source_remaining_balance;
244 : ulong destination_rent_exempt_reserve;
245 : };
246 : typedef struct validated_split_info validated_split_info_t;
247 :
248 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L992
249 : static int
250 : validate_split_amount( fd_exec_instr_ctx_t const * invoke_context,
251 : uchar source_account_index,
252 : uchar destination_account_index,
253 : ulong lamports,
254 : fd_stake_meta_t const * source_meta,
255 : ulong additional_required_lamports,
256 : int source_is_active,
257 0 : validated_split_info_t * out ) {
258 0 : ulong source_lamports = 0;
259 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L1003-L1004 */
260 0 : fd_guarded_borrowed_account_t source_account = {0};
261 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( invoke_context, source_account_index, &source_account );
262 :
263 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1005
264 0 : source_lamports = fd_borrowed_account_get_lamports( &source_account );
265 :
266 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L1006 */
267 0 : fd_borrowed_account_drop( &source_account );
268 :
269 0 : ulong destination_lamports = 0;
270 0 : ulong destination_data_len = 0;
271 :
272 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L1007-L1008 */
273 0 : fd_guarded_borrowed_account_t destination_account = {0};
274 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( invoke_context, destination_account_index, &destination_account );
275 :
276 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1009-1010
277 0 : destination_lamports = fd_borrowed_account_get_lamports( &destination_account );
278 0 : destination_data_len = fd_borrowed_account_get_data_len( &destination_account );
279 :
280 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L1011 */
281 0 : fd_borrowed_account_drop( &destination_account );
282 :
283 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1013-L1021
284 0 : if( FD_UNLIKELY( lamports==0 ) ) return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
285 0 : if( FD_UNLIKELY( lamports>source_lamports ) ) return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
286 :
287 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1027-L1040
288 0 : ulong source_minimum_balance =
289 0 : fd_ulong_sat_add( source_meta->rent_exempt_reserve, additional_required_lamports );
290 0 : ulong source_remaining_balance = fd_ulong_sat_sub( source_lamports, lamports );
291 :
292 0 : if( FD_LIKELY( source_remaining_balance==0 ) ) {
293 0 : } else if( source_remaining_balance<source_minimum_balance ) {
294 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
295 0 : } else {
296 0 : };
297 :
298 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1042
299 0 : fd_rent_t rent_;
300 0 : fd_rent_t const * rent = fd_sysvar_cache_rent_read( invoke_context->sysvar_cache, &rent_ );
301 0 : if( FD_UNLIKELY( !rent ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
302 :
303 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1043
304 0 : ulong destination_rent_exempt_reserve =
305 0 : fd_rent_exempt_minimum_balance( rent, destination_data_len );
306 :
307 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1048
308 0 : if( FD_UNLIKELY(
309 0 : FD_FEATURE_ACTIVE_BANK( invoke_context->bank, require_rent_exempt_split_destination ) &&
310 0 : source_is_active && source_remaining_balance!=0 &&
311 0 : destination_lamports<destination_rent_exempt_reserve ) ) {
312 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
313 0 : }
314 :
315 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1059-L1066
316 0 : ulong destination_minimum_balance =
317 0 : fd_ulong_sat_add( destination_rent_exempt_reserve, additional_required_lamports );
318 0 : ulong destination_balance_deficit =
319 0 : fd_ulong_sat_sub( destination_minimum_balance, destination_lamports );
320 0 : if( FD_UNLIKELY( lamports<destination_balance_deficit ) ) {
321 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
322 0 : }
323 :
324 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1067-L1071
325 0 : out->source_remaining_balance = source_remaining_balance;
326 0 : out->destination_rent_exempt_reserve = destination_rent_exempt_reserve;
327 0 : return 0;
328 0 : }
329 :
330 : /**********************************************************************/
331 : /* impl Lockup */
332 : /**********************************************************************/
333 :
334 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L270
335 : static inline int
336 : lockup_is_in_force( fd_stake_lockup_t const * self,
337 : fd_sol_sysvar_clock_t const * clock,
338 0 : fd_pubkey_t const * custodian ) {
339 : // FIXME FD_LIKELY
340 0 : if( custodian && !memcmp( custodian, &self->custodian, sizeof( fd_pubkey_t ) ) ) {
341 0 : return 0;
342 0 : }
343 0 : return self->unix_timestamp>clock->unix_timestamp || self->epoch>clock->epoch;
344 0 : }
345 :
346 : /**********************************************************************/
347 : /* impl Authorized */
348 : /**********************************************************************/
349 :
350 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L359
351 : static inline int
352 : authorized_check( fd_stake_authorized_t const * self,
353 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
354 0 : fd_stake_authorize_t stake_authorize ) {
355 : /* clang-format off */
356 0 : switch( stake_authorize.discriminant ) {
357 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L365
358 0 : case fd_stake_authorize_enum_staker:
359 0 : if( FD_LIKELY( fd_signers_contains( signers, &self->staker ) ) ) {
360 0 : return 0;
361 0 : }
362 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
363 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L366
364 0 : case fd_stake_authorize_enum_withdrawer:
365 0 : if( FD_LIKELY( fd_signers_contains( signers, &self->withdrawer ) ) ) {
366 0 : return 0;
367 0 : }
368 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
369 0 : default:
370 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
371 0 : }
372 : /* clang-format on */
373 0 : }
374 :
375 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L371
376 : static int
377 : authorized_authorize( fd_stake_authorized_t * self,
378 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
379 : fd_pubkey_t const * new_authorized,
380 : fd_stake_authorize_t const * stake_authorize,
381 : fd_stake_lockup_custodian_args_t const * lockup_custodian_args,
382 0 : /* out */ uint * custom_err ) {
383 0 : int rc;
384 0 : switch( stake_authorize->discriminant ) {
385 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L379
386 0 : case fd_stake_authorize_enum_staker:
387 0 : if( FD_UNLIKELY( !fd_signers_contains( signers, &self->staker ) &&
388 0 : !fd_signers_contains( signers, &self->withdrawer ) ) ) {
389 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
390 0 : }
391 0 : self->staker = *new_authorized;
392 0 : break;
393 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L386
394 0 : case fd_stake_authorize_enum_withdrawer:
395 0 : if( FD_LIKELY( lockup_custodian_args ) ) {
396 0 : fd_stake_lockup_t const * lockup = &lockup_custodian_args->lockup;
397 0 : fd_sol_sysvar_clock_t const * clock = &lockup_custodian_args->clock;
398 0 : fd_pubkey_t const * custodian = lockup_custodian_args->custodian;
399 :
400 : // FIXME FD_LIKELY
401 0 : if( lockup_is_in_force( lockup, clock, NULL ) ) {
402 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L389-L402
403 0 : if( !custodian ) { // FIXME FD_LIKELY
404 0 : *custom_err = FD_STAKE_ERR_CUSTODIAN_MISSING;
405 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
406 0 : } else {
407 0 : if( FD_UNLIKELY( !fd_signers_contains( signers, custodian ) ) ) {
408 0 : *custom_err = FD_STAKE_ERR_CUSTODIAN_SIGNATURE_MISSING;
409 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
410 0 : }
411 :
412 0 : if( FD_UNLIKELY( lockup_is_in_force( lockup, clock, custodian ) ) ) {
413 0 : *custom_err = FD_STAKE_ERR_LOCKUP_IN_FORCE;
414 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
415 0 : }
416 0 : }
417 0 : }
418 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L405
419 0 : rc = authorized_check( self, signers, *stake_authorize );
420 0 : if( FD_UNLIKELY( rc ) ) return rc;
421 0 : self->withdrawer = *new_authorized;
422 0 : }
423 0 : }
424 0 : return 0;
425 0 : }
426 :
427 : /**********************************************************************/
428 : /* impl Meta */
429 : /**********************************************************************/
430 :
431 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L482
432 : static inline int
433 : set_lockup_meta( fd_stake_meta_t * self,
434 : fd_lockup_args_t const * lockup,
435 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
436 0 : fd_sol_sysvar_clock_t const * clock ) {
437 : // FIXME FD_LIKELY
438 0 : if( lockup_is_in_force( &self->lockup, clock, NULL ) ) {
439 0 : if( !fd_signers_contains( signers, &self->lockup.custodian ) ) {
440 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
441 0 : }
442 0 : } else if( !fd_signers_contains( signers, &self->authorized.withdrawer ) ) {
443 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
444 0 : }
445 :
446 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L498-L506
447 0 : if( lockup->unix_timestamp ) self->lockup.unix_timestamp = *lockup->unix_timestamp;
448 0 : if( lockup->epoch ) self->lockup.epoch = *lockup->epoch;
449 0 : if( lockup->custodian ) self->lockup.custodian = *lockup->custodian;
450 0 : return 0;
451 0 : }
452 :
453 : /**********************************************************************/
454 : /* impl Delegation */
455 : /**********************************************************************/
456 :
457 : typedef fd_stake_history_entry_t fd_stake_activation_status_t;
458 :
459 : fd_stake_history_entry_t const *
460 : fd_stake_history_ele_binary_search_const( fd_stake_history_t const * history,
461 0 : ulong epoch ) {
462 0 : ulong start = 0UL;
463 0 : ulong end = history->fd_stake_history_len - 1;
464 :
465 0 : while ( start<=end ) {
466 0 : ulong mid = start + ( end - start ) / 2UL;
467 0 : if( history->fd_stake_history[mid].epoch==epoch ) {
468 0 : return &history->fd_stake_history[mid].entry;
469 0 : } else if( history->fd_stake_history[mid].epoch<epoch ) {
470 0 : if ( mid==0 ) return NULL;
471 0 : end = mid - 1;
472 0 : } else {
473 0 : start = mid + 1;
474 0 : }
475 0 : }
476 0 : return NULL;
477 0 : }
478 :
479 : fd_stake_history_entry_t const *
480 : fd_stake_history_ele_query_const( fd_stake_history_t const * history,
481 0 : ulong epoch ) {
482 0 : if( 0 == history->fd_stake_history_len ) {
483 0 : return NULL;
484 0 : }
485 :
486 0 : if( epoch > history->fd_stake_history[0].epoch ) {
487 0 : return NULL;
488 0 : }
489 :
490 0 : ulong off = (history->fd_stake_history[0].epoch - epoch);
491 0 : if( off >= history->fd_stake_history_len ) {
492 0 : return fd_stake_history_ele_binary_search_const( history, epoch );
493 0 : }
494 :
495 0 : ulong e = (off + history->fd_stake_history_offset) & (history->fd_stake_history_size - 1);
496 :
497 0 : if ( history->fd_stake_history[e].epoch == epoch ) {
498 0 : return &history->fd_stake_history[e].entry;
499 0 : }
500 :
501 : // if the epoch did not match, we do a binary search
502 0 : return fd_stake_history_ele_binary_search_const( history, epoch );
503 0 : }
504 :
505 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L728
506 : static effective_activating_t
507 : stake_and_activating( fd_delegation_t const * self,
508 : ulong target_epoch,
509 : fd_stake_history_t const * history,
510 0 : ulong * new_rate_activation_epoch ) {
511 0 : ulong delegated_stake = self->stake;
512 :
513 0 : fd_stake_history_entry_t const * cluster_stake_at_activation_epoch;
514 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L736
515 0 : if( self->activation_epoch==ULONG_MAX ) {
516 0 : return ( effective_activating_t ){ .effective = delegated_stake, .activating = 0 };
517 0 : } else if( self->activation_epoch==self->deactivation_epoch ) {
518 0 : return ( effective_activating_t ){ .effective = 0, .activating = 0 };
519 0 : } else if( target_epoch==self->activation_epoch ) {
520 0 : return ( effective_activating_t ){ .effective = 0, .activating = delegated_stake };
521 0 : } else if( target_epoch<self->activation_epoch ) {
522 0 : return ( effective_activating_t ){ .effective = 0, .activating = 0 };
523 0 : } else if( history &&
524 0 : ( cluster_stake_at_activation_epoch = fd_stake_history_ele_query_const(
525 0 : history, self->activation_epoch ) ) ) {
526 0 : ulong prev_epoch = self->activation_epoch;
527 0 : fd_stake_history_entry_t const * prev_cluster_stake = cluster_stake_at_activation_epoch;
528 :
529 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L763
530 0 : ulong current_epoch;
531 0 : ulong current_effective_stake = 0;
532 0 : for( ;; ) {
533 0 : current_epoch = prev_epoch + 1;
534 0 : if( FD_LIKELY( prev_cluster_stake->activating==0 ) ) { // FIXME always optimize loop break?
535 0 : break;
536 0 : }
537 :
538 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L775-L780
539 0 : ulong remaining_activating_stake = delegated_stake - current_effective_stake;
540 0 : double weight = (double)remaining_activating_stake / (double)prev_cluster_stake->activating;
541 0 : double warmup_cooldown_rate_ =
542 0 : warmup_cooldown_rate( current_epoch, new_rate_activation_epoch );
543 :
544 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L782-L786
545 0 : double newly_effective_cluster_stake =
546 0 : (double)prev_cluster_stake->effective * warmup_cooldown_rate_;
547 0 : ulong newly_effective_stake =
548 0 : fd_ulong_max( fd_rust_cast_double_to_ulong( weight * newly_effective_cluster_stake ), 1 );
549 :
550 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L787-L792
551 0 : current_effective_stake += newly_effective_stake;
552 0 : if( FD_LIKELY( current_effective_stake>=delegated_stake ) ) {
553 0 : current_effective_stake = delegated_stake;
554 0 : break;
555 0 : }
556 :
557 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L793
558 0 : if( FD_LIKELY( current_epoch>=target_epoch ||
559 0 : current_epoch>=self->deactivation_epoch ) ) { // FIXME always optimize loop break
560 0 : break;
561 0 : }
562 :
563 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L796-L801
564 0 : fd_stake_history_entry_t const * current_cluster_stake =
565 0 : fd_stake_history_ele_query_const( history, current_epoch );
566 0 : if( FD_UNLIKELY( NULL != current_cluster_stake ) ) {
567 0 : prev_epoch = current_epoch;
568 0 : prev_cluster_stake = current_cluster_stake;
569 0 : } else {
570 : // FIXME always optimize loop break
571 0 : break;
572 0 : }
573 0 : }
574 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L804-L807
575 0 : return ( effective_activating_t ){ .effective = current_effective_stake,
576 0 : .activating = delegated_stake - current_effective_stake };
577 0 : } else {
578 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L810
579 0 : return ( effective_activating_t ){ .effective = delegated_stake, .activating = 0 };
580 0 : }
581 0 : }
582 :
583 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L641
584 : static fd_stake_activation_status_t
585 : stake_activating_and_deactivating( fd_delegation_t const * self,
586 : ulong target_epoch,
587 : fd_stake_history_t const * stake_history,
588 0 : ulong * new_rate_activation_epoch ) {
589 :
590 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L648
591 0 : effective_activating_t effective_activating =
592 0 : stake_and_activating( self, target_epoch, stake_history, new_rate_activation_epoch );
593 :
594 0 : ulong effective_stake = effective_activating.effective;
595 0 : ulong activating_stake = effective_activating.activating;
596 :
597 0 : fd_stake_history_entry_t const * cluster_stake_at_deactivation_epoch = NULL;
598 :
599 : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/stake/state.rs#L652
600 0 : if( target_epoch<self->deactivation_epoch ) {
601 : // if is bootstrap
602 0 : if( activating_stake==0 ) {
603 0 : return ( fd_stake_history_entry_t ){
604 0 : .effective = effective_stake, .deactivating = 0, .activating = 0 };
605 0 : } else {
606 0 : return ( fd_stake_history_entry_t ){
607 0 : .effective = effective_stake, .deactivating = 0, .activating = activating_stake };
608 0 : }
609 0 : } else if( target_epoch==self->deactivation_epoch ) {
610 : // https://github.com/anza-xyz/agave/blob/be16321eb0db3e12a57a32f59febbf54b92ebb7c/sdk/program/src/stake/state.rs#L662
611 0 : return ( fd_stake_history_entry_t ){
612 0 : .effective = effective_stake, .deactivating = effective_stake, .activating = 0 };
613 0 : } else if( stake_history &&
614 0 : ( cluster_stake_at_deactivation_epoch = fd_stake_history_ele_query_const( stake_history, self->deactivation_epoch ) ) ) {
615 : // https://github.com/anza-xyz/agave/blob/be16321eb0db3e12a57a32f59febbf54b92ebb7c/sdk/program/src/stake/state.rs#L665
616 0 : ulong prev_epoch = self->deactivation_epoch;
617 0 : fd_stake_history_entry_t const * prev_cluster_stake = cluster_stake_at_deactivation_epoch;
618 :
619 0 : ulong current_epoch;
620 0 : ulong current_effective_stake = effective_stake;
621 0 : for( ;; ) {
622 0 : current_epoch = prev_epoch + 1;
623 0 : if( prev_cluster_stake->deactivating==0 ) break;
624 :
625 0 : double weight = (double)current_effective_stake / (double)prev_cluster_stake->deactivating;
626 0 : double warmup_cooldown_rate_ =
627 0 : warmup_cooldown_rate( current_epoch, new_rate_activation_epoch );
628 :
629 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L697-L700
630 0 : double newly_not_effective_cluster_stake =
631 0 : (double)prev_cluster_stake->effective * warmup_cooldown_rate_;
632 0 : ulong newly_not_effective_stake =
633 0 : fd_ulong_max( fd_rust_cast_double_to_ulong( weight * newly_not_effective_cluster_stake ), 1 );
634 :
635 0 : current_effective_stake =
636 0 : fd_ulong_sat_sub( current_effective_stake, newly_not_effective_stake );
637 0 : if( current_effective_stake==0 ) break;
638 :
639 0 : if( current_epoch>=target_epoch ) break;
640 :
641 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L711-L713
642 0 : fd_stake_history_entry_t const * current_cluster_stake = NULL;
643 0 : if( ( current_cluster_stake = fd_stake_history_ele_query_const(stake_history, current_epoch ) ) ) {
644 0 : prev_epoch = current_epoch;
645 0 : prev_cluster_stake = current_cluster_stake;
646 0 : } else {
647 0 : break;
648 0 : }
649 0 : }
650 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L720
651 0 : return ( fd_stake_history_entry_t ){ .effective = current_effective_stake,
652 0 : .deactivating = current_effective_stake,
653 0 : .activating = 0 };
654 0 : } else {
655 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L723C16-L723C17
656 0 : return ( fd_stake_history_entry_t ){ .effective = 0, .activating = 0, .deactivating = 0 };
657 0 : }
658 0 : }
659 :
660 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L630
661 : static inline ulong
662 : delegation_stake( fd_delegation_t const * self,
663 : ulong epoch,
664 : fd_stake_history_t const * history,
665 0 : ulong * new_rate_activation_epoch ) {
666 0 : return stake_activating_and_deactivating( self, epoch, history, new_rate_activation_epoch )
667 0 : .effective;
668 0 : }
669 :
670 : /**********************************************************************/
671 : /* mod tools */
672 : /**********************************************************************/
673 :
674 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/tools.rs#L44
675 : static inline int
676 : acceptable_reference_epoch_credits( fd_vote_epoch_credits_t * epoch_credits,
677 0 : ulong current_epoch ) {
678 0 : ulong len = deq_fd_vote_epoch_credits_t_cnt( epoch_credits );
679 0 : ulong epoch_index[1] = { ULONG_MAX };
680 : // FIXME FD_LIKELY
681 0 : if( !__builtin_usubl_overflow( len, MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION, epoch_index ) ) {
682 0 : ulong epoch = current_epoch;
683 0 : for( ulong i = len - 1; i>=*epoch_index; i-- ) {
684 0 : ulong vote_epoch = deq_fd_vote_epoch_credits_t_peek_index( epoch_credits, i )->epoch;
685 0 : if( vote_epoch!=epoch ) { return 0; }
686 0 : epoch = fd_ulong_sat_sub( epoch, 1 );
687 0 : if( i==0 ) break;
688 0 : }
689 0 : return 1;
690 0 : } else {
691 0 : return 0;
692 0 : };
693 0 : }
694 :
695 : /* https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/tools.rs#L67-L83 */
696 : static inline int
697 0 : eligible_for_deactivate_delinquent( fd_vote_epoch_credits_t * epoch_credits, ulong current_epoch ) {
698 0 : if( FD_LIKELY( deq_fd_vote_epoch_credits_t_empty( epoch_credits ) ) ) {
699 0 : return 1;
700 0 : }
701 :
702 0 : fd_vote_epoch_credits_t * last = deq_fd_vote_epoch_credits_t_peek_tail( epoch_credits );
703 0 : if( FD_LIKELY( !last ) ) {
704 0 : return 1;
705 0 : } else {
706 0 : ulong epoch = last->epoch;
707 0 : ulong minimum_epoch = ULONG_MAX;
708 0 : int res = fd_ulong_checked_sub( current_epoch, MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION, &minimum_epoch );
709 0 : if( FD_LIKELY( res==0 ) ) {
710 0 : return epoch<=minimum_epoch;
711 0 : } else {
712 0 : return 0;
713 0 : }
714 0 : }
715 0 : }
716 :
717 : /**********************************************************************/
718 : /* impl StakeFlags */
719 : /**********************************************************************/
720 :
721 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/stake_flags.rs#L72
722 : #define STAKE_FLAGS_MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED \
723 : ( ( fd_stake_flags_t ){ .bits = 1 } )
724 :
725 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/stake_flags.rs#L75
726 0 : #define STAKE_FLAGS_EMPTY ( ( fd_stake_flags_t ){ .bits = 0 } )
727 :
728 : /**********************************************************************/
729 : /* impl Stake */
730 : /**********************************************************************/
731 :
732 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L915
733 : static int
734 : stake_split( fd_stake_t * self,
735 : ulong remaining_stake_delta,
736 : ulong split_stake_amount,
737 : uint * custom_err,
738 0 : fd_stake_t * out ) {
739 0 : if( FD_UNLIKELY( remaining_stake_delta>self->delegation.stake ) ) {
740 0 : *custom_err = FD_STAKE_ERR_INSUFFICIENT_STAKE;
741 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
742 0 : }
743 0 : self->delegation.stake -= remaining_stake_delta;
744 0 : fd_stake_t new;
745 0 : new = *self;
746 0 : new.delegation.stake = split_stake_amount;
747 0 : *out = new;
748 0 : return 0;
749 0 : }
750 :
751 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L934
752 : static int
753 0 : stake_deactivate( fd_stake_t * stake, ulong epoch, uint * custom_err ) {
754 0 : if( FD_UNLIKELY( stake->delegation.deactivation_epoch!=ULONG_MAX ) ) {
755 0 : *custom_err = FD_STAKE_ERR_ALREADY_DEACTIVATED;
756 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
757 0 : } else {
758 0 : stake->delegation.deactivation_epoch = epoch;
759 0 : return 0;
760 0 : }
761 0 : }
762 :
763 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L62
764 : int
765 : fd_new_warmup_cooldown_rate_epoch(
766 : fd_epoch_schedule_t const * epoch_schedule,
767 : fd_features_t const * features,
768 : ulong slot,
769 : /* out */ ulong * epoch,
770 : int * err
771 0 : ) {
772 0 : *err = 0;
773 :
774 0 : if( FD_UNLIKELY( !epoch_schedule ) ) {
775 0 : *epoch = ULONG_MAX;
776 0 : *err = FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
777 0 : return 1;
778 0 : }
779 : /* reduce_stake_warmup_cooldown is activated on all clusters, so we shouldn't have a `None` case. */
780 0 : if( FD_LIKELY( FD_FEATURE_ACTIVE( slot, features, reduce_stake_warmup_cooldown ) ) ) {
781 0 : ulong slot = features->reduce_stake_warmup_cooldown;
782 0 : *epoch = fd_slot_to_epoch( epoch_schedule, slot, NULL );
783 0 : return 1;
784 0 : }
785 0 : return 0;
786 0 : }
787 :
788 : /**********************************************************************/
789 : /* util */
790 : /**********************************************************************/
791 :
792 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/state.rs#L205
793 : FD_FN_CONST static inline ulong
794 0 : stake_state_v2_size_of( void ) {
795 0 : return 200;
796 0 : }
797 :
798 : /**********************************************************************/
799 : /* impl MergeKind */
800 : /**********************************************************************/
801 :
802 : static fd_stake_meta_t const *
803 0 : meta( merge_kind_t const * self ) {
804 0 : switch( self->discriminant ) {
805 0 : case merge_kind_inactive:
806 0 : return &self->inner.inactive.meta;
807 0 : case merge_kind_activation_epoch:
808 0 : return &self->inner.activation_epoch.meta;
809 0 : case merge_kind_fully_active:
810 0 : return &self->inner.fully_active.meta;
811 0 : default:
812 0 : FD_LOG_ERR( ( "invalid merge_kind_t discriminant" ) );
813 0 : }
814 0 : }
815 :
816 : static fd_stake_t const *
817 0 : active_stake( merge_kind_t const * self ) {
818 0 : switch( self->discriminant ) {
819 0 : case merge_kind_inactive:
820 0 : return NULL;
821 0 : case merge_kind_activation_epoch:
822 0 : return &self->inner.activation_epoch.stake;
823 0 : case merge_kind_fully_active:
824 0 : return &self->inner.fully_active.stake;
825 0 : default:
826 0 : FD_LOG_ERR( ( "invalid merge_kind_t discriminant" ) );
827 0 : }
828 0 : }
829 :
830 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1097
831 : static int
832 : get_if_mergeable( fd_exec_instr_ctx_t * invoke_context, // not const to log
833 : fd_stake_state_v2_t const * stake_state,
834 : ulong stake_lamports,
835 : fd_sol_sysvar_clock_t const * clock,
836 : fd_stake_history_t const * stake_history,
837 : merge_kind_t * out,
838 0 : uint * custom_err ) {
839 : // stake_history must be non-NULL
840 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1104
841 0 : switch( stake_state->discriminant ) {
842 0 : case fd_stake_state_v2_enum_stake: {
843 0 : fd_stake_meta_t const * meta = &stake_state->inner.stake.meta;
844 0 : fd_stake_t const * stake = &stake_state->inner.stake.stake;
845 0 : fd_stake_flags_t const * stake_flags = &stake_state->inner.stake.stake_flags;
846 :
847 0 : fd_epoch_schedule_t epoch_schedule_[1];
848 0 : fd_epoch_schedule_t const * epoch_schedule = fd_sysvar_cache_epoch_schedule_read( invoke_context->sysvar_cache, epoch_schedule_ );
849 0 : ulong new_rate_activation_epoch = ULONG_MAX;
850 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1111
851 0 : int err;
852 0 : int is_some = fd_new_warmup_cooldown_rate_epoch(
853 0 : epoch_schedule,
854 0 : fd_bank_features_query( invoke_context->bank ),
855 0 : fd_bank_slot_get( invoke_context->bank ),
856 0 : &new_rate_activation_epoch,
857 0 : &err );
858 0 : if( FD_UNLIKELY( err ) ) return err;
859 :
860 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1108
861 0 : fd_stake_history_entry_t status =
862 0 : stake_activating_and_deactivating( &stake->delegation,
863 0 : clock->epoch,
864 0 : stake_history,
865 0 : fd_ptr_if( is_some, &new_rate_activation_epoch, NULL ) );
866 :
867 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1115
868 0 : if( status.effective==0 && status.activating==0 && status.deactivating==0 ) {
869 :
870 0 : *out = ( merge_kind_t ){ .discriminant = merge_kind_inactive,
871 0 : .inner = { .inactive = { .meta = *meta,
872 0 : .active_stake = stake_lamports,
873 0 : .stake_flags = *stake_flags } } };
874 0 : return 0;
875 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1116
876 0 : } else if( status.effective==0 ) {
877 0 : *out = ( merge_kind_t ){ .discriminant = merge_kind_activation_epoch,
878 0 : .inner = { .activation_epoch = { .meta = *meta,
879 0 : .stake = *stake,
880 0 : .stake_flags = *stake_flags } } };
881 0 : return 0;
882 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1117
883 0 : } else if( status.activating==0 && status.deactivating==0 ) {
884 0 : *out = ( merge_kind_t ){ .discriminant = merge_kind_fully_active,
885 0 : .inner = { .fully_active = { .meta = *meta,
886 0 : .stake = *stake } } };
887 0 : return 0;
888 0 : } else {
889 0 : fd_log_collector_msg_literal( invoke_context, "stake account with transient stake cannot be merged" );
890 0 : *custom_err = FD_STAKE_ERR_MERGE_TRANSIENT_STAKE;
891 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
892 0 : }
893 0 : break;
894 0 : }
895 0 : case fd_stake_state_v2_enum_initialized: {
896 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1126
897 0 : *out = ( merge_kind_t ){ .discriminant = merge_kind_inactive,
898 0 : .inner = { .inactive = { .meta = stake_state->inner.initialized.meta,
899 0 : .active_stake = stake_lamports,
900 0 : .stake_flags = STAKE_FLAGS_EMPTY} } };
901 0 : break;
902 0 : }
903 0 : default:
904 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1128
905 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
906 0 : }
907 0 : return 0;
908 0 : }
909 :
910 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1132
911 : static int
912 : metas_can_merge( fd_exec_instr_ctx_t * invoke_context, // not const to log
913 : fd_stake_meta_t const * stake,
914 : fd_stake_meta_t const * source,
915 : fd_sol_sysvar_clock_t const * clock,
916 0 : uint * custom_err ) {
917 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1139
918 0 : int can_merge_lockups =
919 0 : ( !memcmp( &stake->lockup, &source->lockup, sizeof( fd_stake_lockup_t ) ) ) ||
920 0 : ( !lockup_is_in_force( &stake->lockup, clock, NULL ) &&
921 0 : !lockup_is_in_force( &source->lockup, clock, NULL ) );
922 :
923 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1146
924 0 : if( FD_LIKELY( !memcmp( &stake->authorized, &source->authorized, sizeof( fd_stake_authorized_t ) ) && can_merge_lockups ) ) {
925 0 : return 0;
926 0 : } else {
927 0 : fd_log_collector_msg_literal( invoke_context, "Unable to merge due to metadata mismatch" );
928 0 : *custom_err = FD_STAKE_ERR_MERGE_MISMATCH;
929 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
930 0 : }
931 0 : }
932 :
933 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1154
934 : static int
935 : active_delegations_can_merge( fd_exec_instr_ctx_t * invoke_context, // not const to log
936 : fd_delegation_t const * stake,
937 : fd_delegation_t const * source,
938 0 : uint * custom_err ) {
939 0 : if( memcmp( &stake->voter_pubkey, &source->voter_pubkey, sizeof(fd_pubkey_t) ) ) {
940 0 : fd_log_collector_msg_literal( invoke_context, "Unable to merge due to voter mismatch" );
941 0 : *custom_err = FD_STAKE_ERR_MERGE_MISMATCH;
942 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
943 0 : } else if( FD_LIKELY( stake->deactivation_epoch==ULONG_MAX && source->deactivation_epoch==ULONG_MAX ) ) {
944 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1162
945 0 : return 0;
946 0 : } else {
947 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1167
948 0 : fd_log_collector_msg_literal( invoke_context, "Unable to merge due to stake deactivation" );
949 0 : *custom_err = FD_STAKE_ERR_MERGE_MISMATCH;
950 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
951 0 : }
952 0 : }
953 :
954 : static int
955 : stake_weighted_credits_observed( fd_stake_t const * stake,
956 : ulong absorbed_lamports,
957 : ulong absorbed_credits_observed,
958 0 : ulong * out ) {
959 : /* https://github.com/anza-xyz/agave/blob/dc74c22960b4f2adfc672f6dc3bfaa74ec1d5d48/programs/stake/src/stake_state.rs#L1194 */
960 0 : if( FD_LIKELY( stake->credits_observed==absorbed_credits_observed ) ) {
961 0 : *out = stake->credits_observed;
962 0 : return 1;
963 0 : } else {
964 : /* https://github.com/anza-xyz/agave/blob/dc74c22960b4f2adfc672f6dc3bfaa74ec1d5d48/programs/stake/src/stake_state.rs#L1197 */
965 : /* let total_stake = u128::from(stake.delegation.stake.checked_add(absorbed_lamports)?); */
966 0 : ulong total_stake;
967 : /* If there is an overflow on the ulong addition then exit */
968 0 : if( FD_UNLIKELY( fd_ulong_checked_add( stake->delegation.stake, absorbed_lamports, &total_stake ) ) ) {
969 0 : return 0;
970 0 : }
971 :
972 : /* https://github.com/anza-xyz/agave/blob/9489096dc5b7f0a61a981f3d0fd393d264896c2a/programs/stake/src/stake_state.rs#L1198 */
973 : /* The multiplication of two 64 bit integers will never overflow the 128 bits */
974 0 : ulong stake_weighted_credits_h;
975 0 : ulong stake_weighted_credits_l;
976 : /* let stake_weighted_credits = */
977 : /* u128::from(stake.credits_observed).checked_mul(u128::from(stake.delegation.stake))?; */
978 0 : fd_uwide_mul( &stake_weighted_credits_h, &stake_weighted_credits_l,
979 0 : stake->credits_observed, stake->delegation.stake );
980 :
981 : /* https://github.com/anza-xyz/agave/blob/9489096dc5b7f0a61a981f3d0fd393d264896c2a/programs/stake/src/stake_state.rs#L1200 */
982 : /* The multiplication of two 64 bit integers will never overflow the 128 bits */
983 0 : ulong absorbed_weighted_credits_h;
984 0 : ulong absorbed_weighted_credits_l;
985 : /* let absorbed_weighted_credits = */
986 : /* u128::from(absorbed_credits_observed).checked_mul(u128::from(absorbed_lamports))?; */
987 0 : fd_uwide_mul( &absorbed_weighted_credits_h, &absorbed_weighted_credits_l,
988 0 : absorbed_credits_observed, absorbed_lamports );
989 :
990 : /* https://github.com/anza-xyz/agave/blob/9489096dc5b7f0a61a981f3d0fd393d264896c2a/programs/stake/src/stake_state.rs#L1204 */
991 : /* let total_weighted_credits = stake_weighted_credits */
992 : /* .checked_add(absorbed_weighted_credits)? */
993 : /* .checked_add(total_stake)? */
994 : /* .checked_sub(1)?; */
995 0 : ulong total_weighted_credits_partial_one_h;
996 0 : ulong total_weighted_credits_partial_one_l;
997 0 : ulong carry_out = fd_uwide_add( &total_weighted_credits_partial_one_h, &total_weighted_credits_partial_one_l,
998 0 : stake_weighted_credits_h, stake_weighted_credits_l,
999 0 : absorbed_weighted_credits_h, absorbed_weighted_credits_l, 0UL );
1000 : /* return on overflow */
1001 0 : if( FD_UNLIKELY( carry_out ) ) {
1002 0 : return 0;
1003 0 : }
1004 :
1005 0 : ulong total_weighted_credits_partial_two_h;
1006 0 : ulong total_weighted_credits_partial_two_l;
1007 0 : carry_out = fd_uwide_add( &total_weighted_credits_partial_two_h, &total_weighted_credits_partial_two_l,
1008 0 : total_weighted_credits_partial_one_h, total_weighted_credits_partial_one_l,
1009 0 : 0UL, total_stake, 0UL );
1010 : /* return on overflow */
1011 0 : if( FD_UNLIKELY( carry_out ) ) {
1012 0 : return 0;
1013 0 : }
1014 :
1015 : /* The only way we can underflow the subtraction of 1 is if the value of total_weighted_credits_partial_two is zero */
1016 0 : if( FD_UNLIKELY( total_weighted_credits_partial_two_h==0 && total_weighted_credits_partial_two_l==0 ) ) {
1017 0 : return 0;
1018 0 : }
1019 0 : ulong total_weighted_credits_h;
1020 0 : ulong total_weighted_credits_l;
1021 0 : fd_uwide_dec( &total_weighted_credits_h, &total_weighted_credits_l,
1022 0 : total_weighted_credits_partial_two_h, total_weighted_credits_partial_two_l, 1UL );
1023 :
1024 : /* https://github.com/anza-xyz/agave/blob/8a1b2dc3fa4b85e26fbce0db06a462d4853b0652/programs/stake/src/stake_state.rs#L1208 */
1025 : /* u64::try_from(total_weighted_credits.checked_div(total_stake)?).ok() */
1026 0 : ulong res_h;
1027 0 : ulong res_l;
1028 0 : if( FD_UNLIKELY( fd_uwide_div( &res_h, &res_l, total_weighted_credits_h, total_weighted_credits_l, total_stake ) ) ) {
1029 0 : return 0;
1030 0 : }
1031 0 : *out = res_l;
1032 0 : return 1;
1033 0 : }
1034 0 : }
1035 :
1036 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1239
1037 : static int
1038 : merge_delegation_stake_and_credits_observed( FD_FN_UNUSED fd_exec_instr_ctx_t const * invoke_context,
1039 : fd_stake_t * stake,
1040 : ulong absorbed_lamports,
1041 0 : ulong absorbed_credits_observed ) {
1042 0 : int rc;
1043 0 : int is_some = stake_weighted_credits_observed(
1044 0 : stake, absorbed_lamports, absorbed_credits_observed, &stake->credits_observed );
1045 0 : if( FD_UNLIKELY( !is_some ) ) return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
1046 0 : rc = fd_ulong_checked_add( stake->delegation.stake, absorbed_lamports, &stake->delegation.stake );
1047 0 : if( FD_UNLIKELY( rc ) ) return rc;
1048 0 : return 0;
1049 0 : }
1050 :
1051 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1171
1052 : static int
1053 : merge_kind_merge( merge_kind_t self,
1054 : fd_exec_instr_ctx_t * invoke_context, // not const to log
1055 : merge_kind_t source,
1056 : fd_sol_sysvar_clock_t const * clock,
1057 : fd_stake_state_v2_t * out,
1058 : int * is_some,
1059 0 : uint * custom_err ) {
1060 0 : int rc;
1061 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1177
1062 0 : rc = metas_can_merge( invoke_context, meta( &self ), meta( &source ), clock, custom_err );
1063 0 : if( FD_UNLIKELY( rc ) ) return rc;
1064 :
1065 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1178-L1188
1066 0 : fd_stake_t const * stake = active_stake( &self );
1067 0 : fd_stake_t const * source_ = active_stake( &source );
1068 :
1069 : // FIXME FD_LIKELY
1070 0 : if( stake && source_ ) {
1071 0 : rc = active_delegations_can_merge(
1072 0 : invoke_context, &stake->delegation, &source_->delegation, custom_err );
1073 0 : if( FD_UNLIKELY( rc ) ) return rc;
1074 0 : }
1075 :
1076 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1188
1077 : // FIXME FD_LIKELY
1078 0 : fd_stake_state_v2_t merged_state_ = {0};
1079 0 : fd_stake_state_v2_t * merged_state = &merged_state_;
1080 0 : if( self.discriminant==merge_kind_inactive && source.discriminant==merge_kind_inactive ) {
1081 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1189
1082 0 : merged_state = NULL;
1083 0 : } else if( self.discriminant==merge_kind_inactive && source.discriminant==merge_kind_activation_epoch ) {
1084 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1190
1085 0 : merged_state = NULL;
1086 0 : } else if( self.discriminant==merge_kind_activation_epoch && source.discriminant==merge_kind_inactive ) {
1087 0 : fd_stake_meta_t meta = self.inner.activation_epoch.meta;
1088 0 : fd_stake_t stake = self.inner.activation_epoch.stake;
1089 0 : fd_stake_flags_t stake_flags = self.inner.activation_epoch.stake_flags;
1090 0 : ulong source_lamports = source.inner.inactive.active_stake;
1091 0 : fd_stake_flags_t source_stake_flags = source.inner.inactive.stake_flags;
1092 :
1093 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1195
1094 0 : rc = fd_ulong_checked_add( stake.delegation.stake, source_lamports, &stake.delegation.stake );
1095 0 : if( FD_UNLIKELY( rc ) ) return rc;
1096 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1196
1097 0 : *merged_state = ( fd_stake_state_v2_t ){
1098 0 : .discriminant = fd_stake_state_v2_enum_stake,
1099 0 : .inner = { .stake = { .meta = meta,
1100 0 : .stake = stake,
1101 0 : .stake_flags = { .bits = stake_flags.bits | source_stake_flags.bits } } } };
1102 0 : } else if( self.discriminant==merge_kind_activation_epoch && source.discriminant==merge_kind_activation_epoch ) {
1103 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1203
1104 0 : fd_stake_meta_t meta = self.inner.activation_epoch.meta;
1105 0 : fd_stake_t stake = self.inner.activation_epoch.stake;
1106 0 : fd_stake_flags_t stake_flags = self.inner.activation_epoch.stake_flags;
1107 0 : fd_stake_meta_t source_meta = source.inner.activation_epoch.meta;
1108 0 : fd_stake_t source_stake = source.inner.activation_epoch.stake;
1109 0 : fd_stake_flags_t source_stake_flags = source.inner.activation_epoch.stake_flags;
1110 :
1111 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1206
1112 0 : ulong source_lamports = ULONG_MAX;
1113 0 : rc = fd_ulong_checked_add( source_meta.rent_exempt_reserve, source_stake.delegation.stake, &source_lamports );
1114 0 : if( FD_UNLIKELY( rc ) ) return rc;
1115 :
1116 : // // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1210
1117 0 : rc = merge_delegation_stake_and_credits_observed(invoke_context, &stake, source_lamports, source_stake.credits_observed );
1118 0 : if( FD_UNLIKELY( rc ) ) return rc;
1119 :
1120 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1215
1121 0 : *merged_state = ( fd_stake_state_v2_t ){
1122 0 : .discriminant = fd_stake_state_v2_enum_stake,
1123 0 : .inner = { .stake = { .meta = meta,
1124 0 : .stake = stake,
1125 0 : .stake_flags = { .bits = stake_flags.bits | source_stake_flags.bits } } } };
1126 0 : } else if( self.discriminant==merge_kind_fully_active && source.discriminant==merge_kind_fully_active ) {
1127 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1221
1128 0 : fd_stake_meta_t meta = self.inner.fully_active.meta;
1129 0 : fd_stake_t stake = self.inner.fully_active.stake;
1130 0 : fd_stake_t source_stake = source.inner.fully_active.stake;
1131 0 : rc = merge_delegation_stake_and_credits_observed(
1132 0 : invoke_context, &stake, source_stake.delegation.stake, source_stake.credits_observed );
1133 0 : if( FD_UNLIKELY( rc ) ) return rc;
1134 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1231
1135 0 : *merged_state = ( fd_stake_state_v2_t ){
1136 0 : .discriminant = fd_stake_state_v2_enum_stake,
1137 0 : .inner = { .stake = { .meta = meta, .stake = stake, .stake_flags = STAKE_FLAGS_EMPTY } } };
1138 0 : } else {
1139 0 : *custom_err = FD_STAKE_ERR_MERGE_MISMATCH;
1140 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1141 0 : }
1142 0 : if( !merged_state ) {
1143 0 : *is_some = 0;
1144 0 : return 0;
1145 0 : }
1146 0 : *is_some = 1;
1147 0 : *out = *merged_state;
1148 0 : return 0;
1149 0 : }
1150 :
1151 : /**********************************************************************/
1152 : /* mod stake_state */
1153 : /**********************************************************************/
1154 :
1155 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L72
1156 : static int
1157 : get_stake_status( fd_exec_instr_ctx_t const * invoke_context,
1158 : fd_stake_t * stake,
1159 : fd_sol_sysvar_clock_t const * clock,
1160 0 : fd_stake_activation_status_t * out ) {
1161 0 : fd_sysvar_cache_t const * sysvar_cache = invoke_context->sysvar_cache;
1162 0 : if( FD_UNLIKELY( !fd_sysvar_cache_stake_history_is_valid( sysvar_cache ) ) ) {
1163 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
1164 0 : }
1165 :
1166 0 : fd_epoch_schedule_t epoch_schedule_[1];
1167 0 : fd_epoch_schedule_t const * epoch_schedule = fd_sysvar_cache_epoch_schedule_read( sysvar_cache, epoch_schedule_ );
1168 :
1169 0 : ulong new_rate_activation_epoch = ULONG_MAX;
1170 0 : int err;
1171 0 : int is_some = fd_new_warmup_cooldown_rate_epoch(
1172 0 : epoch_schedule,
1173 0 : fd_bank_features_query( invoke_context->bank ),
1174 0 : fd_bank_slot_get( invoke_context->bank ),
1175 0 : &new_rate_activation_epoch,
1176 0 : &err );
1177 0 : if( FD_UNLIKELY( err ) ) return err;
1178 :
1179 0 : fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history_join_const( sysvar_cache );
1180 0 : *out = stake_activating_and_deactivating(
1181 0 : &stake->delegation,
1182 0 : clock->epoch,
1183 0 : stake_history,
1184 0 : fd_ptr_if( is_some, &new_rate_activation_epoch, NULL ) );
1185 0 : fd_sysvar_cache_stake_history_leave_const( sysvar_cache, stake_history );
1186 :
1187 0 : return 0;
1188 0 : }
1189 :
1190 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/vote/state/mod.rs#L740
1191 : static ulong
1192 0 : get_credits( fd_vote_state_t const * vote_state ) {
1193 :
1194 0 : return ( deq_fd_vote_epoch_credits_t_empty( vote_state->epoch_credits )
1195 0 : ? 0
1196 0 : : deq_fd_vote_epoch_credits_t_peek_index(
1197 0 : vote_state->epoch_credits,
1198 0 : deq_fd_vote_epoch_credits_t_cnt( vote_state->epoch_credits ) - 1 )
1199 0 : ->credits );
1200 0 : }
1201 :
1202 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L85
1203 : static int
1204 : redelegate_stake( fd_exec_instr_ctx_t const * ctx,
1205 : fd_stake_t * stake,
1206 : ulong stake_lamports,
1207 : fd_pubkey_t const * voter_pubkey,
1208 : fd_vote_state_t const * vote_state,
1209 : fd_sol_sysvar_clock_t const * clock,
1210 : fd_stake_history_t const * stake_history,
1211 0 : uint * custom_err ) {
1212 0 : fd_epoch_schedule_t epoch_schedule_[1];
1213 0 : fd_epoch_schedule_t const * epoch_schedule = fd_sysvar_cache_epoch_schedule_read( ctx->sysvar_cache, epoch_schedule_ );
1214 :
1215 0 : ulong new_rate_activation_epoch = ULONG_MAX;
1216 0 : int err;
1217 0 : int is_some = fd_new_warmup_cooldown_rate_epoch(
1218 0 : epoch_schedule,
1219 0 : fd_bank_features_query( ctx->bank ),
1220 0 : fd_bank_slot_get( ctx->bank ),
1221 0 : &new_rate_activation_epoch,
1222 0 : &err );
1223 0 : if( FD_UNLIKELY( err ) ) return err;
1224 :
1225 : // FIXME FD_LIKELY
1226 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L96
1227 0 : if( delegation_stake( &stake->delegation, clock->epoch, stake_history, fd_ptr_if( is_some, &new_rate_activation_epoch, NULL ) )!=0 ) {
1228 :
1229 0 : if( FD_LIKELY( !memcmp( &stake->delegation.voter_pubkey, voter_pubkey, sizeof( fd_pubkey_t ) ) ) &&
1230 0 : clock->epoch==stake->delegation.deactivation_epoch ) {
1231 0 : stake->delegation.deactivation_epoch = ULONG_MAX;
1232 0 : return 0;
1233 0 : } else {
1234 0 : *custom_err = FD_STAKE_ERR_TOO_SOON_TO_REDELEGATE;
1235 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1236 0 : }
1237 0 : }
1238 :
1239 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L114-L118
1240 0 : stake->delegation.stake = stake_lamports;
1241 0 : stake->delegation.activation_epoch = clock->epoch;
1242 0 : stake->delegation.deactivation_epoch = ULONG_MAX;
1243 0 : stake->delegation.voter_pubkey = *voter_pubkey;
1244 0 : stake->credits_observed = get_credits( vote_state );
1245 0 : return 0;
1246 0 : }
1247 :
1248 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L202
1249 : static fd_stake_t
1250 : new_stake( ulong stake,
1251 : fd_pubkey_t const * voter_pubkey,
1252 : fd_vote_state_t const * vote_state,
1253 0 : ulong activation_epoch ) {
1254 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L208
1255 0 : return ( fd_stake_t ){
1256 0 : .delegation = {.voter_pubkey = *voter_pubkey,
1257 0 : .stake = stake,
1258 0 : .activation_epoch = activation_epoch,
1259 0 : .deactivation_epoch = ULONG_MAX,
1260 0 : .warmup_cooldown_rate = DEFAULT_WARMUP_COOLDOWN_RATE},
1261 0 : .credits_observed = get_credits( vote_state ),
1262 0 : };
1263 0 : }
1264 :
1265 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L214
1266 : static int
1267 : initialize( fd_borrowed_account_t * stake_account,
1268 : fd_stake_authorized_t const * authorized,
1269 : fd_stake_lockup_t const * lockup,
1270 0 : fd_rent_t const * rent ) {
1271 :
1272 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L220
1273 :
1274 0 : if( FD_UNLIKELY( fd_borrowed_account_get_data_len( stake_account )!=stake_state_v2_size_of() ) )
1275 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1276 :
1277 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L224
1278 0 : fd_stake_state_v2_t stake_state = {0};
1279 0 : do {
1280 0 : int rc = get_state( stake_account->acct, &stake_state );
1281 0 : if( FD_UNLIKELY( rc ) ) return rc;
1282 0 : } while(0);
1283 :
1284 0 : if( FD_LIKELY( stake_state.discriminant==fd_stake_state_v2_enum_uninitialized ) ) {
1285 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L225
1286 0 : ulong rent_exempt_reserve = fd_rent_exempt_minimum_balance( rent, fd_borrowed_account_get_data_len( stake_account ) );
1287 :
1288 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L226
1289 0 : if( FD_LIKELY( fd_borrowed_account_get_lamports( stake_account )>=rent_exempt_reserve ) ) {
1290 0 : fd_stake_state_v2_t initialized = {
1291 0 : .discriminant = fd_stake_state_v2_enum_initialized,
1292 0 : .inner = { .initialized = { .meta = { .rent_exempt_reserve = rent_exempt_reserve,
1293 0 : .authorized = *authorized,
1294 0 : .lockup = *lockup } } } };
1295 0 : return set_state( stake_account, &initialized );
1296 0 : } else {
1297 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L233
1298 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
1299 0 : }
1300 :
1301 0 : } else {
1302 :
1303 : /// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L236
1304 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1305 :
1306 0 : }
1307 0 : }
1308 :
1309 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L243
1310 : static int
1311 : authorize( fd_borrowed_account_t * stake_account,
1312 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1313 : fd_pubkey_t const * new_authority,
1314 : fd_stake_authorize_t const * stake_authorize,
1315 : fd_sol_sysvar_clock_t const * clock,
1316 : fd_pubkey_t const * custodian,
1317 0 : uint * custom_err ) {
1318 0 : int rc;
1319 0 : fd_stake_state_v2_t stake_state = {0};
1320 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L251
1321 0 : rc = get_state( stake_account->acct, &stake_state );
1322 0 : if( FD_UNLIKELY( rc ) ) return rc;
1323 0 : switch( stake_state.discriminant ) {
1324 : /* FIXME check if the compiler can optimize away branching (given the layout of `meta` in both
1325 : * union members) and instead fallthrough */
1326 0 : case fd_stake_state_v2_enum_stake: {
1327 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L252
1328 0 : fd_stake_meta_t * meta = &stake_state.inner.stake.meta;
1329 :
1330 0 : fd_stake_lockup_custodian_args_t lockup_custodian_args = {
1331 0 : .lockup = meta->lockup, .clock = *clock, .custodian = (fd_pubkey_t *)custodian };
1332 0 : rc = authorized_authorize(
1333 0 : &meta->authorized, /* &mut self */
1334 0 : signers,
1335 0 : new_authority,
1336 0 : stake_authorize,
1337 0 : &lockup_custodian_args,
1338 0 : custom_err );
1339 0 : if( FD_UNLIKELY( rc ) ) return rc;
1340 :
1341 0 : return set_state( stake_account, &stake_state );
1342 0 : }
1343 0 : case fd_stake_state_v2_enum_initialized: {
1344 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L261
1345 0 : fd_stake_meta_t * meta = &stake_state.inner.initialized.meta;
1346 :
1347 0 : fd_stake_lockup_custodian_args_t lockup_custodian_args = {
1348 0 : .lockup = meta->lockup, .clock = *clock, .custodian = (fd_pubkey_t *)custodian };
1349 0 : rc = authorized_authorize(
1350 0 : &meta->authorized,
1351 0 : signers,
1352 0 : new_authority,
1353 0 : stake_authorize,
1354 0 : &lockup_custodian_args,
1355 0 : custom_err );
1356 0 : if( FD_UNLIKELY( rc ) ) return rc;
1357 :
1358 0 : return set_state( stake_account, &stake_state );
1359 0 : }
1360 0 : default:
1361 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L270
1362 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1363 0 : }
1364 0 : return rc;
1365 0 : }
1366 :
1367 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L275
1368 : static int
1369 : authorize_with_seed( fd_exec_instr_ctx_t const * ctx,
1370 : fd_borrowed_account_t * stake_account,
1371 : uchar authority_base_index,
1372 : char const * authority_seed,
1373 : ulong authority_seed_len,
1374 : fd_pubkey_t const * authority_owner,
1375 : fd_pubkey_t const * new_authority,
1376 : fd_stake_authorize_t const * stake_authorize,
1377 : fd_sol_sysvar_clock_t const * clock,
1378 0 : fd_pubkey_t const * custodian ) {
1379 0 : int rc;
1380 0 : fd_pubkey_t const * signers[FD_TXN_SIG_MAX] = {0};
1381 0 : fd_pubkey_t out = {0};
1382 0 : if( FD_LIKELY( fd_instr_acc_is_signer_idx( ctx->instr, authority_base_index, NULL ) ) ) {
1383 :
1384 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L289
1385 0 : fd_pubkey_t const * base_pubkey = NULL;
1386 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, authority_base_index, &base_pubkey );
1387 0 : if( FD_UNLIKELY( rc ) ) return rc;
1388 :
1389 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L293
1390 0 : rc = fd_pubkey_create_with_seed( ctx,
1391 0 : base_pubkey->uc,
1392 0 : authority_seed,
1393 0 : authority_seed_len,
1394 0 : authority_owner->uc,
1395 0 : /* out */ out.uc );
1396 0 : if( FD_UNLIKELY( rc ) ) return rc;
1397 0 : signers[0] = &out;
1398 0 : }
1399 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L299
1400 0 : return authorize( stake_account,
1401 0 : signers,
1402 0 : new_authority,
1403 0 : stake_authorize,
1404 0 : clock,
1405 0 : custodian,
1406 0 : &ctx->txn_out->err.custom_err );
1407 0 : }
1408 :
1409 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L310
1410 : static int
1411 : delegate( fd_exec_instr_ctx_t const * ctx,
1412 : uchar stake_account_index,
1413 : uchar vote_account_index,
1414 : fd_sol_sysvar_clock_t const * clock,
1415 : fd_stake_history_t const * stake_history,
1416 0 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX] ) {
1417 0 : int rc;
1418 :
1419 0 : fd_pubkey_t const * vote_pubkey;
1420 0 : fd_vote_state_versioned_t * vote_state = NULL;
1421 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L321-L322 */
1422 0 : fd_guarded_borrowed_account_t vote_account = {0};
1423 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, vote_account_index, &vote_account );
1424 :
1425 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L323
1426 0 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &vote_account ), fd_solana_vote_program_id.key, 32UL ) ) )
1427 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
1428 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L3326
1429 0 : vote_pubkey = vote_account.acct->pubkey;
1430 : // https://github.com/anza-xyz/agave/blob/a60fbc2288d626a4f1846052c8fcb98d3f9ea58d/programs/stake/src/stake_state.rs#L327
1431 0 : vote_state = fd_vote_get_state( vote_account.acct, ctx->runtime->stake_program.delegate.vote_state_mem );
1432 :
1433 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L328 */
1434 0 : fd_borrowed_account_drop( &vote_account );
1435 :
1436 0 : fd_stake_state_v2_t stake_state = {0};
1437 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L330 */
1438 0 : fd_guarded_borrowed_account_t stake_account = {0};
1439 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, stake_account_index, &stake_account );
1440 :
1441 0 : rc = get_state( stake_account.acct, &stake_state );
1442 0 : if( FD_UNLIKELY( rc ) ) return rc;
1443 :
1444 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L332
1445 0 : switch( stake_state.discriminant ) {
1446 0 : case fd_stake_state_v2_enum_initialized: {
1447 0 : fd_stake_meta_t meta = stake_state.inner.initialized.meta;
1448 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L334
1449 0 : rc = authorized_check( &meta.authorized, signers, STAKE_AUTHORIZE_STAKER );
1450 0 : if( FD_UNLIKELY( rc ) ) return rc;
1451 :
1452 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L335-L336
1453 0 : validated_delegated_info_t validated_delegated_info;
1454 0 : rc = validate_delegated_amount( ctx->bank,
1455 0 : &stake_account,
1456 0 : &meta,
1457 0 : &validated_delegated_info,
1458 0 : &ctx->txn_out->err.custom_err );
1459 0 : if( FD_UNLIKELY( rc ) ) return rc;
1460 0 : ulong stake_amount = validated_delegated_info.stake_amount;
1461 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L340
1462 0 : if( FD_UNLIKELY( !vote_state ) ) return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1463 0 : fd_vote_convert_to_current( vote_state,
1464 0 : ctx->runtime->stake_program.delegate.authorized_voters_mem,
1465 0 : ctx->runtime->stake_program.delegate.landed_votes_mem );
1466 0 : fd_stake_t stake = new_stake( stake_amount,
1467 0 : vote_pubkey,
1468 0 : &vote_state->inner.current,
1469 0 : clock->epoch );
1470 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L343
1471 0 : fd_stake_state_v2_t new_stake_state = { .discriminant = fd_stake_state_v2_enum_stake,
1472 0 : .inner = { .stake = {
1473 0 : .meta = meta,
1474 0 : .stake = stake,
1475 0 : .stake_flags = STAKE_FLAGS_EMPTY } } };
1476 0 : return set_state( &stake_account, &new_stake_state );
1477 0 : }
1478 0 : case fd_stake_state_v2_enum_stake: {
1479 0 : fd_stake_meta_t meta = stake_state.inner.stake.meta;
1480 0 : fd_stake_t stake = stake_state.inner.stake.stake;
1481 0 : fd_stake_flags_t stake_flags = stake_state.inner.stake.stake_flags;
1482 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L346
1483 0 : rc = authorized_check( &meta.authorized, signers, STAKE_AUTHORIZE_STAKER );
1484 0 : if( FD_UNLIKELY( rc ) ) return rc;
1485 :
1486 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L347-L348
1487 0 : validated_delegated_info_t validated_delegated_info;
1488 0 : rc = validate_delegated_amount( ctx->bank,
1489 0 : &stake_account,
1490 0 : &meta,
1491 0 : &validated_delegated_info,
1492 0 : &ctx->txn_out->err.custom_err );
1493 0 : if( FD_UNLIKELY( rc ) ) return rc;
1494 0 : ulong stake_amount = validated_delegated_info.stake_amount;
1495 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L354
1496 0 : if( FD_UNLIKELY( !vote_state ) ) return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1497 0 : fd_vote_convert_to_current( vote_state,
1498 0 : ctx->runtime->stake_program.delegate.authorized_voters_mem,
1499 0 : ctx->runtime->stake_program.delegate.landed_votes_mem );
1500 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L349
1501 0 : rc = redelegate_stake( ctx,
1502 0 : &stake,
1503 0 : stake_amount,
1504 0 : vote_pubkey,
1505 0 : &vote_state->inner.current,
1506 0 : clock,
1507 0 : stake_history,
1508 0 : &ctx->txn_out->err.custom_err );
1509 0 : if( FD_UNLIKELY( rc ) ) return rc;
1510 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L358
1511 0 : fd_stake_state_v2_t new_stake_state = { .discriminant = fd_stake_state_v2_enum_stake,
1512 0 : .inner = { .stake = {
1513 0 : .meta = meta,
1514 0 : .stake = stake,
1515 0 : .stake_flags = stake_flags } } };
1516 :
1517 0 : return set_state( &stake_account, &new_stake_state );
1518 0 : }
1519 0 : default:
1520 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L360
1521 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1522 0 : }
1523 :
1524 : /* implicit drop of stake account */
1525 0 : }
1526 :
1527 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L364
1528 : static int
1529 : deactivate( fd_borrowed_account_t * stake_account,
1530 : fd_sol_sysvar_clock_t const * clock,
1531 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1532 0 : uint * custom_err ) {
1533 0 : int rc;
1534 :
1535 0 : fd_stake_state_v2_t state = {0};
1536 0 : rc = get_state( stake_account->acct, &state );
1537 0 : if( FD_UNLIKELY( rc ) ) return rc;
1538 :
1539 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L370
1540 0 : if( state.discriminant==fd_stake_state_v2_enum_stake ) {
1541 0 : fd_stake_meta_t * meta = &state.inner.stake.meta;
1542 0 : fd_stake_t * stake = &state.inner.stake.stake;
1543 :
1544 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L371
1545 0 : rc = authorized_check( &meta->authorized, signers, STAKE_AUTHORIZE_STAKER );
1546 0 : if( FD_UNLIKELY( rc ) ) return rc;
1547 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L372
1548 0 : rc = stake_deactivate( stake, clock->epoch, custom_err );
1549 0 : if( FD_UNLIKELY( rc ) ) return rc;
1550 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L373
1551 0 : return set_state( stake_account, &state );
1552 0 : } else {
1553 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L375
1554 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1555 0 : }
1556 0 : }
1557 :
1558 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L379
1559 : static int
1560 : set_lockup( fd_borrowed_account_t * stake_account,
1561 : fd_lockup_args_t const * lockup,
1562 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
1563 0 : fd_sol_sysvar_clock_t const * clock ) {
1564 0 : int rc;
1565 :
1566 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L385
1567 0 : fd_stake_state_v2_t state = {0};
1568 0 : rc = get_state( stake_account->acct, &state );
1569 0 : if( FD_UNLIKELY( rc ) ) return rc;
1570 :
1571 0 : switch( state.discriminant ) {
1572 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L386
1573 0 : case fd_stake_state_v2_enum_initialized: {
1574 0 : fd_stake_meta_t * meta = &state.inner.initialized.meta;
1575 0 : rc = set_lockup_meta( meta, lockup, signers, clock );
1576 0 : if( FD_UNLIKELY( rc ) ) return rc;
1577 0 : return set_state( stake_account, &state );
1578 0 : }
1579 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L390
1580 0 : case fd_stake_state_v2_enum_stake: {
1581 0 : fd_stake_meta_t * meta = &state.inner.stake.meta;
1582 0 : rc = set_lockup_meta( meta, lockup, signers, clock );
1583 0 : if( FD_UNLIKELY( rc ) ) return rc;
1584 0 : return set_state( stake_account, &state );
1585 0 : }
1586 0 : default:
1587 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L394
1588 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1589 0 : }
1590 0 : }
1591 :
1592 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L398
1593 : static int
1594 : split( fd_exec_instr_ctx_t const * ctx,
1595 : uchar stake_account_index,
1596 : ulong lamports,
1597 : uchar split_index,
1598 0 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX] ) {
1599 0 : int rc;
1600 :
1601 0 : ulong split_lamport_balance = 0;
1602 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L407-L408 */
1603 0 : fd_guarded_borrowed_account_t split = {0};
1604 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, split_index, &split );
1605 :
1606 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L409
1607 0 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &split ), fd_solana_stake_program_id.key, 32UL ) ) )
1608 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
1609 :
1610 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L412
1611 0 : if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &split )!=stake_state_v2_size_of() ) )
1612 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1613 :
1614 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L415
1615 0 : fd_stake_state_v2_t split_get_state = {0};
1616 0 : rc = get_state( split.acct, &split_get_state );
1617 0 : if( FD_UNLIKELY( rc ) ) return rc;
1618 0 : if( FD_UNLIKELY( split_get_state.discriminant!=fd_stake_state_v2_enum_uninitialized ) ) {
1619 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1620 0 : }
1621 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L418
1622 0 : split_lamport_balance = fd_borrowed_account_get_lamports( &split );
1623 :
1624 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L419 */
1625 0 : fd_borrowed_account_drop( &split );
1626 :
1627 0 : fd_stake_state_v2_t stake_state = {0};
1628 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L420-L421 */
1629 0 : fd_guarded_borrowed_account_t stake_account = {0};
1630 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, stake_account_index, &stake_account );
1631 :
1632 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L422
1633 0 : if( FD_UNLIKELY( lamports>fd_borrowed_account_get_lamports( &stake_account ) ) )
1634 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
1635 :
1636 0 : rc = get_state( stake_account.acct, &stake_state );
1637 0 : if( FD_UNLIKELY( rc ) ) return rc;
1638 :
1639 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L426 */
1640 0 : fd_borrowed_account_drop( &stake_account );
1641 :
1642 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L428
1643 0 : switch( stake_state.discriminant ) {
1644 0 : case fd_stake_state_v2_enum_stake: {
1645 0 : fd_stake_meta_t * meta = &stake_state.inner.stake.meta;
1646 0 : fd_stake_t * stake = &stake_state.inner.stake.stake;
1647 0 : fd_stake_flags_t * stake_flags = &stake_state.inner.stake.stake_flags;
1648 :
1649 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L430
1650 0 : rc = authorized_check( &meta->authorized, signers, STAKE_AUTHORIZE_STAKER );
1651 0 : if( FD_UNLIKELY( rc ) ) return rc;
1652 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L431
1653 0 : ulong minimum_delegation = get_minimum_delegation( ctx->bank );
1654 :
1655 0 : int is_active;
1656 0 : if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( ctx->bank, require_rent_exempt_split_destination ) ) ) {
1657 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L434
1658 0 : fd_sol_sysvar_clock_t clock_;
1659 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
1660 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
1661 :
1662 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L435
1663 0 : fd_stake_activation_status_t status = {0};
1664 0 : rc = get_stake_status( ctx, stake, clock, &status );
1665 0 : if( FD_UNLIKELY( rc ) ) return rc;
1666 :
1667 0 : is_active = status.effective>0;
1668 0 : } else {
1669 0 : is_active = 0;
1670 0 : }
1671 :
1672 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L438
1673 0 : validated_split_info_t validated_split_info = {0};
1674 0 : rc = validate_split_amount( ctx,
1675 0 : stake_account_index,
1676 0 : split_index,
1677 0 : lamports,
1678 0 : meta,
1679 0 : minimum_delegation,
1680 0 : is_active,
1681 0 : &validated_split_info );
1682 0 : if( FD_UNLIKELY( rc ) ) return rc;
1683 :
1684 0 : ulong remaining_stake_delta;
1685 0 : ulong split_stake_amount;
1686 : // FIXME FD_LIKELY
1687 :
1688 0 : if( validated_split_info.source_remaining_balance==0 ) {
1689 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L456
1690 0 : remaining_stake_delta = fd_ulong_sat_sub( lamports, meta->rent_exempt_reserve );
1691 0 : split_stake_amount = remaining_stake_delta;
1692 0 : } else {
1693 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L469
1694 0 : if( FD_UNLIKELY( fd_ulong_sat_sub( stake->delegation.stake, lamports ) <
1695 0 : minimum_delegation ) ) {
1696 0 : ctx->txn_out->err.custom_err = FD_STAKE_ERR_INSUFFICIENT_DELEGATION;
1697 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1698 0 : }
1699 :
1700 0 : remaining_stake_delta = lamports;
1701 0 : split_stake_amount =
1702 0 : fd_ulong_sat_sub( lamports,
1703 0 : fd_ulong_sat_sub( validated_split_info.destination_rent_exempt_reserve,
1704 0 : split_lamport_balance )
1705 :
1706 0 : );
1707 0 : }
1708 :
1709 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L487
1710 0 : if( FD_UNLIKELY( split_stake_amount<minimum_delegation ) ) {
1711 0 : ctx->txn_out->err.custom_err = FD_STAKE_ERR_INSUFFICIENT_DELEGATION;
1712 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
1713 0 : }
1714 :
1715 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L491-L493
1716 0 : fd_stake_t split_stake = {0};
1717 0 : rc = stake_split( stake,
1718 0 : remaining_stake_delta,
1719 0 : split_stake_amount,
1720 0 : &ctx->txn_out->err.custom_err,
1721 0 : &split_stake );
1722 0 : if( FD_UNLIKELY( rc ) ) return rc;
1723 0 : fd_stake_meta_t split_meta = *meta;
1724 0 : split_meta.rent_exempt_reserve = validated_split_info.destination_rent_exempt_reserve;
1725 :
1726 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L495 */
1727 0 : fd_guarded_borrowed_account_t stake_account = {0};
1728 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, stake_account_index, &stake_account );
1729 :
1730 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L497
1731 0 : rc = set_state( &stake_account, &stake_state );
1732 0 : if( FD_UNLIKELY( rc ) ) return rc;
1733 :
1734 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L498 */
1735 0 : fd_borrowed_account_drop( &stake_account );
1736 :
1737 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L499 */
1738 0 : fd_guarded_borrowed_account_t split = {0};
1739 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, split_index, &split );
1740 :
1741 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L501
1742 0 : fd_stake_state_v2_t temp = { .discriminant = fd_stake_state_v2_enum_stake,
1743 0 : .inner = { .stake = { .meta = split_meta,
1744 0 : .stake = split_stake,
1745 0 : .stake_flags = *stake_flags } } };
1746 0 : rc = set_state( &split, &temp );
1747 0 : if( FD_UNLIKELY( rc ) ) return rc;
1748 :
1749 : /* implicit drop of split */
1750 0 : break;
1751 0 : }
1752 0 : case fd_stake_state_v2_enum_initialized: {
1753 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L504
1754 0 : fd_stake_meta_t * meta = &stake_state.inner.initialized.meta;
1755 0 : rc = authorized_check( &meta->authorized, signers, STAKE_AUTHORIZE_STAKER );
1756 0 : if( FD_UNLIKELY( rc ) ) return rc;
1757 :
1758 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L505
1759 0 : validated_split_info_t validated_split_info = {0};
1760 0 : rc = validate_split_amount( ctx,
1761 0 : stake_account_index,
1762 0 : split_index,
1763 0 : lamports,
1764 0 : meta,
1765 0 : 0,
1766 0 : 0,
1767 0 : &validated_split_info );
1768 0 : if( FD_UNLIKELY( rc ) ) return rc;
1769 :
1770 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L516
1771 0 : fd_stake_meta_t split_meta = *meta;
1772 0 : split_meta.rent_exempt_reserve = validated_split_info.destination_rent_exempt_reserve;
1773 :
1774 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L518 */
1775 0 : fd_guarded_borrowed_account_t split = {0};
1776 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, split_index, &split );
1777 :
1778 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L520
1779 0 : fd_stake_state_v2_t temp = { .discriminant = fd_stake_state_v2_enum_initialized,
1780 0 : .inner = { .initialized = { .meta = split_meta } } };
1781 0 : rc = set_state( &split, &temp );
1782 0 : if( FD_UNLIKELY( rc ) ) return rc;
1783 0 : break;
1784 0 : }
1785 0 : case fd_stake_state_v2_enum_uninitialized: {
1786 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L523
1787 0 : fd_pubkey_t const * stake_pubkey = NULL;
1788 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, stake_account_index, &stake_pubkey );
1789 0 : if( FD_UNLIKELY( rc ) ) return rc;
1790 :
1791 0 : if( FD_UNLIKELY( !fd_signers_contains( signers, stake_pubkey ) ) ) {
1792 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L527
1793 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1794 0 : }
1795 0 : break;
1796 0 : }
1797 0 : default:
1798 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L531
1799 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1800 0 : }
1801 :
1802 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L535 */
1803 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, stake_account_index, &stake_account );
1804 :
1805 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L537
1806 0 : if( FD_UNLIKELY( lamports==fd_borrowed_account_get_lamports( &stake_account ) ) ) {
1807 0 : fd_stake_state_v2_t uninitialized = {0};
1808 0 : uninitialized.discriminant = fd_stake_state_v2_enum_uninitialized;
1809 0 : rc = set_state( &stake_account, &uninitialized );
1810 0 : if( FD_UNLIKELY( rc ) ) return rc;
1811 0 : };
1812 :
1813 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L540 */
1814 0 : fd_borrowed_account_drop( &stake_account );
1815 :
1816 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L542 */
1817 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, split_index, &split );
1818 :
1819 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L544
1820 0 : rc = fd_borrowed_account_checked_add_lamports( &split, lamports );
1821 0 : if( FD_UNLIKELY( rc ) ) return rc;
1822 :
1823 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L545 */
1824 0 : fd_borrowed_account_drop( &split );
1825 :
1826 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L546-L547 */
1827 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, stake_account_index, &stake_account );
1828 :
1829 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L548 */
1830 0 : rc = fd_borrowed_account_checked_sub_lamports( &stake_account, lamports );
1831 0 : if( FD_UNLIKELY( rc ) ) return rc;
1832 :
1833 0 : return 0;
1834 0 : }
1835 :
1836 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L552
1837 : static int
1838 : merge( fd_exec_instr_ctx_t * ctx, // not const to log
1839 : uchar stake_account_index,
1840 : uchar source_account_index,
1841 : fd_sol_sysvar_clock_t const * clock,
1842 : fd_stake_history_t const * stake_history,
1843 0 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX] ) {
1844 0 : int rc;
1845 :
1846 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L562 */
1847 0 : fd_guarded_borrowed_account_t source_account = {0};
1848 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, source_account_index, &source_account );
1849 :
1850 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L565
1851 0 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &source_account ), fd_solana_stake_program_id.key, 32UL ) ) )
1852 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
1853 :
1854 0 : ushort stake_acc_idx_in_txn;
1855 0 : ushort source_acc_idx_in_txn;
1856 :
1857 0 : rc = fd_exec_instr_ctx_get_index_of_instr_account_in_transaction( ctx, stake_account_index, &stake_acc_idx_in_txn );
1858 0 : if( FD_UNLIKELY( rc ) ) return rc;
1859 :
1860 0 : rc = fd_exec_instr_ctx_get_index_of_instr_account_in_transaction( ctx, source_account_index, &source_acc_idx_in_txn );
1861 0 : if( FD_UNLIKELY( rc ) ) return rc;
1862 :
1863 : /* Close the stake_account-reference loophole
1864 : https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L569-L574 */
1865 0 : if( FD_UNLIKELY( stake_acc_idx_in_txn==source_acc_idx_in_txn ) )
1866 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1867 :
1868 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L575 */
1869 0 : fd_guarded_borrowed_account_t stake_account = {0};
1870 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, stake_account_index, &stake_account );
1871 :
1872 0 : fd_stake_state_v2_t stake_account_state = {0};
1873 0 : rc = get_state( stake_account.acct, &stake_account_state );
1874 0 : if( FD_UNLIKELY( rc ) ) return rc;
1875 :
1876 0 : merge_kind_t stake_merge_kind = {0};
1877 0 : fd_log_collector_msg_literal( ctx, "Checking if destination stake is mergeable" );
1878 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L579
1879 0 : rc = get_if_mergeable( ctx,
1880 0 : &stake_account_state,
1881 0 : fd_borrowed_account_get_lamports( &stake_account ),
1882 0 : clock,
1883 0 : stake_history,
1884 0 : &stake_merge_kind,
1885 0 : &ctx->txn_out->err.custom_err );
1886 0 : if( FD_UNLIKELY( rc ) )
1887 0 : return rc;
1888 :
1889 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L588
1890 0 : rc = authorized_check( &meta( &stake_merge_kind )->authorized, signers, STAKE_AUTHORIZE_STAKER );
1891 0 : if( FD_UNLIKELY( rc ) )
1892 0 : return rc;
1893 :
1894 0 : fd_stake_state_v2_t source_account_state = {0};
1895 0 : rc = get_state( source_account.acct, &source_account_state );
1896 0 : if( FD_UNLIKELY( rc ) ) return rc;
1897 :
1898 0 : merge_kind_t source_merge_kind = {0};
1899 0 : fd_log_collector_msg_literal( ctx, "Checking if source stake is mergeable" );
1900 : //https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L594
1901 0 : rc = get_if_mergeable( ctx,
1902 0 : &source_account_state,
1903 0 : fd_borrowed_account_get_lamports( &source_account ),
1904 0 : clock,
1905 0 : stake_history,
1906 0 : &source_merge_kind,
1907 0 : &ctx->txn_out->err.custom_err );
1908 0 : if( FD_UNLIKELY( rc ) ) return rc;
1909 :
1910 0 : fd_stake_state_v2_t merged_state = {0};
1911 0 : int is_some = 0;
1912 0 : fd_log_collector_msg_literal( ctx, "Merging stake accounts" );
1913 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L603
1914 0 : rc = merge_kind_merge( stake_merge_kind,
1915 0 : ctx,
1916 0 : source_merge_kind,
1917 0 : clock,
1918 0 : &merged_state,
1919 0 : &is_some,
1920 0 : &ctx->txn_out->err.custom_err );
1921 0 : if( FD_UNLIKELY( rc ) ) return rc;
1922 0 : if( is_some ) {
1923 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L608
1924 0 : rc = set_state( &stake_account, &merged_state );
1925 0 : if( FD_UNLIKELY( rc ) ) return rc;
1926 0 : }
1927 :
1928 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L608
1929 0 : fd_stake_state_v2_t uninitialized = {0};
1930 0 : uninitialized.discriminant = fd_stake_state_v2_enum_uninitialized;
1931 0 : rc = set_state( &source_account, &uninitialized );
1932 0 : if( FD_UNLIKELY( rc ) ) return rc;
1933 :
1934 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L611-L613
1935 0 : ulong lamports = fd_borrowed_account_get_lamports( &source_account );
1936 0 : rc = fd_borrowed_account_checked_sub_lamports( &source_account, lamports );
1937 0 : if( FD_UNLIKELY( rc ) ) return rc;
1938 0 : rc = fd_borrowed_account_checked_add_lamports( &stake_account, lamports );
1939 0 : if( FD_UNLIKELY( rc ) ) return rc;
1940 :
1941 0 : return 0;
1942 0 : }
1943 :
1944 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L136
1945 : static int
1946 : move_stake_or_lamports_shared_checks( fd_exec_instr_ctx_t * invoke_context, // not const to log
1947 : fd_borrowed_account_t * source_account,
1948 : ulong lamports,
1949 : fd_borrowed_account_t * destination_account,
1950 : ushort stake_authority_index,
1951 : merge_kind_t * source_merge_kind,
1952 : merge_kind_t * destination_merge_kind,
1953 0 : uint * custom_err ) {
1954 0 : int rc;
1955 :
1956 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L145-L153
1957 0 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( invoke_context->instr, stake_authority_index, NULL ) ) ) {
1958 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1959 0 : }
1960 :
1961 : // https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L132
1962 0 : fd_pubkey_t const * stake_authority_pubkey = NULL;
1963 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( invoke_context, stake_authority_index, &stake_authority_pubkey );
1964 0 : if( FD_UNLIKELY( rc ) ) return rc;
1965 :
1966 0 : fd_pubkey_t const * signers[FD_TXN_SIG_MAX] = { stake_authority_pubkey };
1967 :
1968 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L158
1969 0 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( source_account ), fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ||
1970 0 : memcmp( fd_borrowed_account_get_owner( destination_account ), fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) )
1971 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
1972 :
1973 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L163
1974 0 : if( FD_UNLIKELY( !memcmp( &source_account->acct->pubkey, &destination_account->acct->pubkey, sizeof(fd_pubkey_t) ) ) )
1975 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
1976 :
1977 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L168
1978 0 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( source_account ) ||
1979 0 : !fd_borrowed_account_is_writable( destination_account ) ) )
1980 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
1981 :
1982 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L173
1983 0 : if( lamports==0 )
1984 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1985 :
1986 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L177-L180
1987 0 : fd_sol_sysvar_clock_t clock_;
1988 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( invoke_context->sysvar_cache, &clock_ );
1989 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
1990 :
1991 0 : fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history_join_const( invoke_context->sysvar_cache );
1992 0 : if( FD_UNLIKELY( !stake_history ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
1993 :
1994 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L182
1995 0 : fd_stake_state_v2_t source_account_state = {0};
1996 0 : rc = get_state( source_account->acct, &source_account_state );
1997 0 : if( FD_UNLIKELY( rc ) ) return rc;
1998 :
1999 0 : rc = get_if_mergeable( invoke_context,
2000 0 : &source_account_state,
2001 0 : fd_borrowed_account_get_lamports( source_account ),
2002 0 : clock,
2003 0 : stake_history,
2004 0 : source_merge_kind,
2005 0 : &invoke_context->txn_out->err.custom_err );
2006 0 : if( FD_UNLIKELY( rc ) ) return rc;
2007 :
2008 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L191
2009 0 : rc = authorized_check( &meta( source_merge_kind )->authorized, signers, STAKE_AUTHORIZE_STAKER );
2010 0 : if( FD_UNLIKELY( rc ) ) return rc;
2011 :
2012 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L197
2013 0 : fd_stake_state_v2_t destination_account_state = {0};
2014 0 : rc = get_state( destination_account->acct, &destination_account_state );
2015 0 : if( FD_UNLIKELY( rc ) ) return rc;
2016 :
2017 0 : rc = get_if_mergeable( invoke_context,
2018 0 : &destination_account_state,
2019 0 : fd_borrowed_account_get_lamports( destination_account ),
2020 0 : clock,
2021 0 : stake_history,
2022 0 : destination_merge_kind,
2023 0 : &invoke_context->txn_out->err.custom_err );
2024 0 : if( FD_UNLIKELY( rc ) ) return rc;
2025 :
2026 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L206
2027 0 : rc = metas_can_merge( invoke_context, meta( source_merge_kind ), meta( destination_merge_kind ), clock, custom_err );
2028 0 : if( FD_UNLIKELY( rc ) ) return rc;
2029 :
2030 0 : return 0;
2031 0 : }
2032 :
2033 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L789
2034 : static int
2035 : move_stake(fd_exec_instr_ctx_t * ctx, // not const to log
2036 : ushort source_account_index,
2037 : ulong lamports,
2038 : ushort destination_account_index,
2039 : ushort stake_authority_index,
2040 0 : uint * custom_err ) {
2041 0 : int rc;
2042 :
2043 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L626 */
2044 0 : fd_guarded_borrowed_account_t source_account = {0};
2045 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, source_account_index, &source_account );
2046 :
2047 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L629 */
2048 0 : fd_guarded_borrowed_account_t destination_account = {0};
2049 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, destination_account_index, &destination_account );
2050 :
2051 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L804
2052 0 : merge_kind_t source_merge_kind = {0};
2053 0 : merge_kind_t destination_merge_kind = {0};
2054 0 : rc = move_stake_or_lamports_shared_checks( ctx,
2055 0 : &source_account,
2056 0 : lamports,
2057 0 : &destination_account,
2058 0 : stake_authority_index,
2059 0 : &source_merge_kind,
2060 0 : &destination_merge_kind,
2061 0 : &ctx->txn_out->err.custom_err );
2062 0 : if( FD_UNLIKELY( rc ) ) return rc;
2063 :
2064 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L816
2065 0 : if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &source_account )!=stake_state_v2_size_of() ||
2066 0 : fd_borrowed_account_get_data_len( &destination_account )!=stake_state_v2_size_of() ) )
2067 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2068 :
2069 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L823
2070 0 : if( source_merge_kind.discriminant!=merge_kind_fully_active )
2071 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2072 0 : fd_stake_meta_t * source_meta = &source_merge_kind.inner.fully_active.meta;
2073 0 : fd_stake_t * source_stake = &source_merge_kind.inner.fully_active.stake;
2074 :
2075 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L827
2076 0 : ulong minimum_delegation = get_minimum_delegation( ctx->bank );
2077 :
2078 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L831
2079 0 : if( FD_UNLIKELY( source_stake->delegation.stake<lamports ) )
2080 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2081 :
2082 0 : ulong source_final_stake = source_stake->delegation.stake - lamports;
2083 :
2084 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L836
2085 0 : if( FD_UNLIKELY( source_final_stake!=0 && source_final_stake<minimum_delegation ) )
2086 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2087 :
2088 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L841
2089 0 : fd_stake_meta_t * destination_meta = NULL;
2090 0 : switch( destination_merge_kind.discriminant ) {
2091 0 : case merge_kind_fully_active: {
2092 0 : fd_stake_t * destination_stake = &destination_merge_kind.inner.fully_active.stake;
2093 :
2094 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L844
2095 0 : if( FD_UNLIKELY( memcmp( &source_stake->delegation.voter_pubkey, &destination_stake->delegation.voter_pubkey, sizeof(fd_pubkey_t) ) ) ) {
2096 0 : *custom_err = FD_STAKE_ERR_VOTE_ADDRESS_MISMATCH;
2097 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
2098 0 : }
2099 :
2100 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L848
2101 0 : ulong destination_effective_stake = 0;
2102 0 : rc = fd_ulong_checked_add( destination_stake->delegation.stake, lamports, &destination_effective_stake );
2103 0 : if( FD_UNLIKELY( rc ) ) return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
2104 :
2105 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L855
2106 0 : if( FD_UNLIKELY( destination_effective_stake<minimum_delegation ) ) {
2107 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2108 0 : }
2109 :
2110 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L859
2111 0 : rc = merge_delegation_stake_and_credits_observed(
2112 0 : ctx, destination_stake, lamports, source_stake->credits_observed );
2113 0 : if( FD_UNLIKELY( rc ) ) return rc;
2114 0 : destination_meta = &destination_merge_kind.inner.fully_active.meta;
2115 :
2116 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L867
2117 0 : fd_stake_state_v2_t new_destination_state = {
2118 0 : .discriminant = fd_stake_state_v2_enum_stake,
2119 0 : .inner = { .stake = {
2120 0 : .meta = *destination_meta,
2121 0 : .stake = *destination_stake,
2122 0 : .stake_flags = STAKE_FLAGS_EMPTY} } };
2123 0 : rc = set_state( &destination_account, &new_destination_state );
2124 0 : if( FD_UNLIKELY( rc ) ) return rc;
2125 :
2126 0 : break;
2127 0 : }
2128 0 : case merge_kind_inactive: {
2129 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L877
2130 0 : if( lamports<minimum_delegation ) {
2131 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2132 0 : }
2133 :
2134 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L881
2135 0 : fd_stake_t * destination_stake = source_stake;
2136 0 : destination_stake->delegation.stake = lamports;
2137 :
2138 0 : destination_meta = &destination_merge_kind.inner.inactive.meta;
2139 :
2140 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L886
2141 0 : fd_stake_state_v2_t new_destination_state = {
2142 0 : .discriminant = fd_stake_state_v2_enum_stake,
2143 0 : .inner = { .stake = {
2144 0 : .meta = *destination_meta,
2145 0 : .stake = *destination_stake,
2146 0 : .stake_flags = STAKE_FLAGS_EMPTY} } };
2147 0 : rc = set_state( &destination_account, &new_destination_state );
2148 0 : if( FD_UNLIKELY( rc ) ) return rc;
2149 0 : break;
2150 0 : }
2151 0 : default:
2152 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L894
2153 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2154 0 : }
2155 :
2156 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L897-L910
2157 0 : if( source_final_stake==0) {
2158 0 : fd_stake_state_v2_t new_source_state = { .discriminant = fd_stake_state_v2_enum_initialized,
2159 0 : .inner = { .initialized = { .meta = *source_meta} } };
2160 0 : rc = set_state( &source_account, &new_source_state );
2161 0 : if( FD_UNLIKELY( rc ) ) return rc;
2162 :
2163 0 : } else {
2164 0 : source_stake->delegation.stake = source_final_stake;
2165 :
2166 0 : fd_stake_state_v2_t new_source_state = { .discriminant = fd_stake_state_v2_enum_stake,
2167 0 : .inner = { .stake = { .meta = *source_meta,
2168 0 : .stake = *source_stake,
2169 0 : .stake_flags = STAKE_FLAGS_EMPTY } } };
2170 0 : rc = set_state( &source_account, &new_source_state );
2171 0 : if( FD_UNLIKELY( rc ) ) return rc;
2172 0 : }
2173 :
2174 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L911-L914
2175 0 : rc = fd_borrowed_account_checked_sub_lamports( &source_account, lamports );
2176 0 : if( FD_UNLIKELY( rc ) ) return rc;
2177 0 : rc = fd_borrowed_account_checked_add_lamports( &destination_account, lamports );
2178 0 : if( FD_UNLIKELY( rc ) ) return rc;
2179 :
2180 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L915-L923
2181 0 : if( FD_UNLIKELY( fd_borrowed_account_get_lamports( &source_account )<source_meta->rent_exempt_reserve ) ||
2182 0 : fd_borrowed_account_get_lamports( &destination_account )<destination_meta->rent_exempt_reserve ) {
2183 0 : fd_log_collector_msg_literal( ctx, "Delegation calculations violated lamport balance assumptions" );
2184 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2185 0 : }
2186 :
2187 0 : return FD_EXECUTOR_INSTR_SUCCESS;
2188 0 : }
2189 :
2190 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L928
2191 : static int
2192 : move_lamports(fd_exec_instr_ctx_t * ctx, // not const to log
2193 : ushort source_account_index,
2194 : ulong lamports,
2195 : ushort destination_account_index,
2196 0 : ushort stake_authority_index ) {
2197 0 : int rc;
2198 :
2199 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L759 */
2200 0 : fd_guarded_borrowed_account_t source_account = {0};
2201 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK(ctx, source_account_index, &source_account );
2202 :
2203 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L762 */
2204 0 : fd_guarded_borrowed_account_t destination_account = {0};
2205 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, destination_account_index, &destination_account );
2206 :
2207 :
2208 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L943
2209 0 : merge_kind_t source_merge_kind = {0};
2210 0 : merge_kind_t destination_merge_kind = {0};
2211 0 : rc = move_stake_or_lamports_shared_checks( ctx,
2212 0 : &source_account,
2213 0 : lamports,
2214 0 : &destination_account,
2215 0 : stake_authority_index,
2216 0 : &source_merge_kind,
2217 0 : &destination_merge_kind,
2218 0 : &ctx->txn_out->err.custom_err );
2219 0 : if( FD_UNLIKELY( rc ) ) return rc;
2220 :
2221 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L953-L963
2222 0 : ulong source_free_lamports;
2223 0 : switch( source_merge_kind.discriminant ) {
2224 0 : case merge_kind_fully_active: {
2225 0 : source_free_lamports = fd_ulong_sat_sub( fd_ulong_sat_sub( fd_borrowed_account_get_lamports( &source_account ),
2226 0 : source_merge_kind.inner.fully_active.stake.delegation.stake ),
2227 0 : source_merge_kind.inner.fully_active.meta.rent_exempt_reserve );
2228 :
2229 0 : break;
2230 0 : }
2231 0 : case merge_kind_inactive: {
2232 0 : source_free_lamports = fd_ulong_sat_sub( source_merge_kind.inner.inactive.active_stake,
2233 0 : source_merge_kind.inner.inactive.meta.rent_exempt_reserve );
2234 0 : break;
2235 0 : }
2236 0 : default:
2237 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2238 0 : }
2239 :
2240 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L964
2241 0 : if( FD_UNLIKELY( lamports>source_free_lamports ) ) {
2242 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2243 0 : }
2244 :
2245 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_state.rs#L968-L970
2246 0 : rc = fd_borrowed_account_checked_sub_lamports( &source_account, lamports );
2247 0 : if( FD_UNLIKELY( rc ) ) return rc;
2248 :
2249 0 : rc = fd_borrowed_account_checked_add_lamports( &destination_account, lamports );
2250 0 : if( FD_UNLIKELY( rc ) ) return rc;
2251 :
2252 0 : return FD_EXECUTOR_INSTR_SUCCESS;
2253 0 : }
2254 :
2255 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L797
2256 : static int
2257 : withdraw( fd_exec_instr_ctx_t const * ctx,
2258 : uchar stake_account_index,
2259 : ulong lamports,
2260 : uchar to_index,
2261 : fd_sol_sysvar_clock_t const * clock,
2262 : fd_stake_history_t const * stake_history,
2263 : uchar withdraw_authority_index,
2264 : uchar * custodian_index,
2265 0 : ulong * new_rate_activation_epoch ) {
2266 :
2267 0 : int rc;
2268 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L809
2269 0 : fd_pubkey_t const * withdraw_authority_pubkey = NULL;
2270 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, withdraw_authority_index, &withdraw_authority_pubkey );
2271 0 : if( FD_UNLIKELY( rc ) ) return rc;
2272 :
2273 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L813
2274 0 : int is_signer = fd_instr_acc_is_signer_idx( ctx->instr, withdraw_authority_index, NULL );
2275 0 : if( FD_UNLIKELY( !is_signer ) ) return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
2276 :
2277 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L817
2278 0 : fd_pubkey_t const * signers[FD_TXN_SIG_MAX] = { withdraw_authority_pubkey };
2279 :
2280 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L819 */
2281 0 : fd_guarded_borrowed_account_t stake_account = {0};
2282 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, stake_account_index, &stake_account );
2283 :
2284 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L821
2285 0 : fd_stake_state_v2_t stake_state = {0};
2286 0 : rc = get_state( stake_account.acct, &stake_state );
2287 0 : if( FD_UNLIKELY( rc ) ) return rc;
2288 :
2289 0 : fd_stake_lockup_t lockup;
2290 0 : ulong reserve;
2291 0 : int is_staked;
2292 :
2293 0 : switch( stake_state.discriminant ) {
2294 0 : case fd_stake_state_v2_enum_stake: {
2295 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L822
2296 0 : fd_stake_meta_t * meta = &stake_state.inner.stake.meta;
2297 0 : fd_stake_t * stake = &stake_state.inner.stake.stake;
2298 :
2299 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L823
2300 0 : rc = authorized_check( &meta->authorized, signers, STAKE_AUTHORIZE_WITHDRAWER );
2301 0 : if( FD_UNLIKELY( rc ) ) return rc;
2302 :
2303 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L826
2304 0 : ulong staked = fd_ulong_if(
2305 0 : clock->epoch>=stake->delegation.deactivation_epoch,
2306 0 : delegation_stake(
2307 0 : &stake->delegation, clock->epoch, stake_history, new_rate_activation_epoch ),
2308 0 : stake->delegation.stake );
2309 :
2310 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L837
2311 0 : ulong staked_and_reserve = ULONG_MAX;
2312 0 : rc = fd_ulong_checked_add( staked, meta->rent_exempt_reserve, &staked_and_reserve );
2313 0 : if( FD_UNLIKELY( rc ) ) return rc;
2314 :
2315 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L838
2316 0 : lockup = meta->lockup;
2317 0 : reserve = staked_and_reserve;
2318 0 : is_staked = staked!=0;
2319 0 : break;
2320 0 : }
2321 0 : case fd_stake_state_v2_enum_initialized: {
2322 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L840
2323 0 : fd_stake_meta_t * meta = &stake_state.inner.initialized.meta;
2324 :
2325 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L841
2326 0 : rc = authorized_check( &meta->authorized, signers, STAKE_AUTHORIZE_WITHDRAWER );
2327 0 : if( FD_UNLIKELY( rc ) ) return rc;
2328 :
2329 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L844
2330 0 : lockup = meta->lockup;
2331 0 : reserve = meta->rent_exempt_reserve;
2332 0 : is_staked = 0;
2333 0 : break;
2334 0 : }
2335 0 : case fd_stake_state_v2_enum_uninitialized: {
2336 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L846
2337 0 : if( FD_UNLIKELY( !fd_signers_contains( signers, stake_account.acct->pubkey ) ) ) {
2338 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
2339 0 : }
2340 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L850
2341 0 : memset( &lockup, 0, sizeof( fd_stake_lockup_t ) ); /* Lockup::default(); */
2342 0 : reserve = 0;
2343 0 : is_staked = 0;
2344 0 : break;
2345 0 : }
2346 0 : default:
2347 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L852
2348 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2349 0 : }
2350 :
2351 : // FIXME FD_LIKELY
2352 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L857-L871
2353 0 : fd_pubkey_t custodian_pubkey_ = {0};
2354 0 : fd_pubkey_t const * custodian_pubkey = &custodian_pubkey_;
2355 0 : if( custodian_index ) {
2356 0 : int is_signer = fd_instr_acc_is_signer_idx( ctx->instr, *custodian_index, NULL );
2357 0 : if( is_signer ) {
2358 0 : int err = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, *custodian_index, &custodian_pubkey );
2359 0 : if( FD_UNLIKELY( err ) ) return err;
2360 0 : } else {
2361 0 : custodian_pubkey = NULL;
2362 0 : }
2363 0 : } else {
2364 0 : custodian_pubkey = NULL;
2365 0 : }
2366 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L871
2367 0 : if( FD_UNLIKELY( lockup_is_in_force( &lockup, clock, custodian_pubkey ) ) ) {
2368 0 : ctx->txn_out->err.custom_err = FD_STAKE_ERR_LOCKUP_IN_FORCE;
2369 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
2370 0 : };
2371 :
2372 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L875
2373 0 : ulong lamports_and_reserve = ULONG_MAX;
2374 0 : rc = fd_ulong_checked_add( lamports, reserve, &lamports_and_reserve );
2375 0 : if( FD_UNLIKELY( rc ) ) return rc;
2376 :
2377 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L877
2378 0 : if( FD_UNLIKELY( is_staked && lamports_and_reserve>fd_borrowed_account_get_lamports( &stake_account ) ) ) {
2379 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
2380 0 : }
2381 :
2382 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L883
2383 0 : if( FD_UNLIKELY( lamports!=fd_borrowed_account_get_lamports( &stake_account ) &&
2384 0 : lamports_and_reserve>fd_borrowed_account_get_lamports( &stake_account ) ) ) {
2385 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L886
2386 0 : FD_TEST( !is_staked );
2387 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
2388 0 : }
2389 :
2390 : // FIXME FD_LIKELY
2391 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L891
2392 0 : if( lamports==fd_borrowed_account_get_lamports( &stake_account ) ) {
2393 0 : fd_stake_state_v2_t uninitialized = {0};
2394 0 : uninitialized.discriminant = fd_stake_state_v2_enum_uninitialized;
2395 0 : rc = set_state( &stake_account, &uninitialized );
2396 0 : if( FD_UNLIKELY( rc ) ) return rc;
2397 0 : }
2398 :
2399 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L895
2400 0 : rc = fd_borrowed_account_checked_sub_lamports( &stake_account, lamports );
2401 0 : if( FD_UNLIKELY( rc ) ) return rc;
2402 :
2403 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L896 */
2404 0 : fd_borrowed_account_drop( &stake_account );
2405 :
2406 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L897 */
2407 0 : fd_guarded_borrowed_account_t to = {0};
2408 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, to_index, &to );
2409 :
2410 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L899
2411 0 : rc = fd_borrowed_account_checked_add_lamports( &to, lamports );
2412 0 : if( FD_UNLIKELY( rc ) ) return rc;
2413 :
2414 0 : return 0;
2415 0 : }
2416 :
2417 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L903
2418 : static int
2419 : deactivate_delinquent( fd_exec_instr_ctx_t * ctx,
2420 : fd_borrowed_account_t * stake_account,
2421 : ushort delinquent_vote_account_index,
2422 : ushort reference_vote_account_index,
2423 : ulong current_epoch,
2424 0 : uint * custom_err ) {
2425 0 : int rc;
2426 :
2427 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L911
2428 0 : fd_pubkey_t const * delinquent_vote_account_pubkey = NULL;
2429 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, delinquent_vote_account_index, &delinquent_vote_account_pubkey );
2430 0 : if( FD_UNLIKELY( rc ) ) return rc;
2431 :
2432 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L915 */
2433 0 : fd_guarded_borrowed_account_t delinquent_vote_account = {0};
2434 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, delinquent_vote_account_index, &delinquent_vote_account );
2435 :
2436 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L917
2437 0 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &delinquent_vote_account ), fd_solana_vote_program_id.key, 32UL ) ) )
2438 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
2439 :
2440 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L920-L922
2441 0 : fd_vote_state_versioned_t * delinquent_vote_state_versioned = fd_vote_get_state( delinquent_vote_account.acct, ctx->runtime->stake_program.deactivate_delinquent.delinquent_vote_state_mem );
2442 0 : if( FD_UNLIKELY( !delinquent_vote_state_versioned ) ) return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2443 0 : fd_vote_convert_to_current( delinquent_vote_state_versioned,
2444 0 : ctx->runtime->stake_program.deactivate_delinquent.delinquent_authorized_voters_mem,
2445 0 : ctx->runtime->stake_program.deactivate_delinquent.delinquent_landed_votes_mem );
2446 0 : fd_vote_state_t delinquent_vote_state = delinquent_vote_state_versioned->inner.current;
2447 :
2448 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_state.rs#L924 */
2449 0 : fd_guarded_borrowed_account_t reference_vote_account = {0};
2450 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, reference_vote_account_index, &reference_vote_account );
2451 :
2452 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L926
2453 0 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &reference_vote_account ), fd_solana_vote_program_id.key, 32UL ) ) )
2454 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
2455 :
2456 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L929-L932
2457 0 : fd_vote_state_versioned_t * reference_vote_state_versioned = fd_vote_get_state( reference_vote_account.acct, ctx->runtime->stake_program.deactivate_delinquent.reference_vote_state_mem );
2458 0 : if( FD_UNLIKELY( !reference_vote_state_versioned ) ) return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2459 0 : fd_vote_convert_to_current( reference_vote_state_versioned,
2460 0 : ctx->runtime->stake_program.deactivate_delinquent.reference_authorized_voters_mem,
2461 0 : ctx->runtime->stake_program.deactivate_delinquent.reference_landed_votes_mem );
2462 0 : fd_vote_state_t reference_vote_state = reference_vote_state_versioned->inner.current;
2463 :
2464 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L933
2465 0 : if( !acceptable_reference_epoch_credits( reference_vote_state.epoch_credits, current_epoch ) ) {
2466 0 : ctx->txn_out->err.custom_err = FD_STAKE_ERR_INSUFFICIENT_REFERENCE_VOTES;
2467 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
2468 0 : }
2469 :
2470 0 : fd_stake_state_v2_t stake_state = {0};
2471 0 : rc = get_state( stake_account->acct, &stake_state );
2472 0 : if( FD_UNLIKELY( rc ) ) return rc;
2473 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L937
2474 0 : if( FD_LIKELY( stake_state.discriminant==fd_stake_state_v2_enum_stake ) ) {
2475 0 : fd_stake_t * stake = &stake_state.inner.stake.stake;
2476 :
2477 0 : if( FD_UNLIKELY( memcmp( &stake->delegation.voter_pubkey, delinquent_vote_account_pubkey, sizeof(fd_pubkey_t) ) ) ) {
2478 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L939
2479 0 : *custom_err = FD_STAKE_ERR_VOTE_ADDRESS_MISMATCH;
2480 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
2481 0 : }
2482 :
2483 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L944
2484 0 : if( FD_LIKELY( eligible_for_deactivate_delinquent( delinquent_vote_state.epoch_credits,
2485 0 : current_epoch ) ) ) {
2486 0 : rc = stake_deactivate( stake, current_epoch, custom_err );
2487 0 : if( FD_UNLIKELY( rc ) ) return rc;
2488 0 : rc = set_state( stake_account, &stake_state );
2489 0 : } else {
2490 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L948
2491 0 : *custom_err = FD_STAKE_ERR_MINIMUM_DELIQUENT_EPOCHS_FOR_DEACTIVATION_NOT_MET;
2492 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
2493 0 : }
2494 0 : } else {
2495 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L951
2496 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2497 0 : }
2498 :
2499 0 : return rc;
2500 0 : }
2501 :
2502 : /**********************************************************************/
2503 : /* mod stake_instruction */
2504 : /**********************************************************************/
2505 :
2506 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L25
2507 : static int
2508 : get_optional_pubkey( fd_exec_instr_ctx_t * ctx,
2509 : ushort acc_idx,
2510 : int should_be_signer,
2511 0 : /* out */ fd_pubkey_t const ** pubkey ) {
2512 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L32
2513 0 : if( FD_LIKELY( acc_idx<ctx->instr->acct_cnt ) ) {
2514 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L33
2515 0 : if( FD_UNLIKELY( should_be_signer && !fd_instr_acc_is_signer_idx( ctx->instr, acc_idx, NULL ) ) ) {
2516 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
2517 0 : }
2518 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L39
2519 0 : int err = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, acc_idx, pubkey );
2520 0 : if( FD_UNLIKELY( err ) ) return err;
2521 0 : } else {
2522 0 : *pubkey = NULL;
2523 0 : }
2524 0 : return 0;
2525 0 : }
2526 :
2527 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L60
2528 : static int
2529 : get_stake_account( fd_exec_instr_ctx_t const * ctx,
2530 0 : fd_borrowed_account_t * out ) {
2531 0 : int err = fd_exec_instr_ctx_try_borrow_instr_account( ctx, 0, out );
2532 0 : if( FD_UNLIKELY( err ) ) return err;
2533 :
2534 0 : fd_borrowed_account_t * account = out;
2535 :
2536 : // https://github.com/https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L62-L65
2537 0 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( account ), fd_solana_stake_program_id.key, 32UL ) ) )
2538 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
2539 :
2540 0 : return FD_EXECUTOR_INSTR_SUCCESS;
2541 0 : }
2542 :
2543 : /**********************************************************************/
2544 : /* Public API */
2545 : /**********************************************************************/
2546 :
2547 : int
2548 0 : fd_stake_program_execute( fd_exec_instr_ctx_t * ctx ) {
2549 : /* Prevent execution of migrated native programs */
2550 0 : if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( ctx->bank, migrate_stake_program_to_core_bpf ) ) ) {
2551 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2552 0 : }
2553 :
2554 0 : FD_EXEC_CU_UPDATE( ctx, DEFAULT_COMPUTE_UNITS );
2555 :
2556 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L77
2557 0 : fd_pubkey_t const * signers[FD_TXN_SIG_MAX] = {0};
2558 0 : fd_exec_instr_ctx_get_signers( ctx, signers );
2559 :
2560 0 : if( FD_UNLIKELY( ctx->instr->data_sz>FD_STAKE_INSTR_FOOTPRINT ) ) {
2561 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2562 0 : }
2563 :
2564 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L79
2565 0 : uchar instr_mem[ FD_STAKE_INSTR_FOOTPRINT ] __attribute__((aligned(alignof(fd_stake_instruction_t))));
2566 0 : fd_stake_instruction_t * instruction = fd_bincode_decode_static(
2567 0 : stake_instruction, instr_mem,
2568 0 : ctx->instr->data,
2569 0 : ctx->instr->data_sz,
2570 0 : NULL );
2571 0 : if( FD_UNLIKELY( !instruction ) ) {
2572 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2573 0 : }
2574 :
2575 : /* The EpochRewards sysvar only exists after partitioned epoch rewards is activated.
2576 : If the sysvar exists, check the `active` field */
2577 :
2578 0 : int epoch_rewards_active = 0;
2579 0 : {
2580 0 : fd_sysvar_epoch_rewards_t epoch_rewards[1];
2581 0 : if( fd_sysvar_cache_epoch_rewards_read( ctx->sysvar_cache, epoch_rewards ) ) {
2582 0 : epoch_rewards_active = epoch_rewards->active;
2583 0 : }
2584 0 : }
2585 0 : if( epoch_rewards_active && instruction->discriminant!=fd_stake_instruction_enum_get_minimum_delegation ) {
2586 0 : ctx->txn_out->err.custom_err = FD_STAKE_ERR_EPOCH_REWARDS_ACTIVE;
2587 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
2588 0 : }
2589 :
2590 0 : int rc;
2591 : // PLEASE PRESERVE SWITCH-CASE ORDERING TO MIRROR AGAVE IMPL:
2592 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L84
2593 0 : switch( instruction->discriminant ) {
2594 :
2595 : /* Initialize
2596 : *
2597 : * Instruction:
2598 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L110
2599 : *
2600 : * Processor:
2601 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L85
2602 : */
2603 0 : case fd_stake_instruction_enum_initialize: {
2604 0 : fd_stake_authorized_t const * authorized = &instruction->inner.initialize.authorized;
2605 0 : fd_stake_lockup_t const * lockup = &instruction->inner.initialize.lockup;
2606 :
2607 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L86
2608 0 : fd_guarded_borrowed_account_t me = {0};
2609 0 : rc = get_stake_account( ctx, &me ); /* acquire_write */
2610 0 : if( FD_UNLIKELY( rc ) ) return rc;
2611 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L87
2612 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_rent_id );
2613 0 : if( FD_UNLIKELY( rc ) ) return rc;
2614 0 : fd_rent_t rent_;
2615 0 : fd_rent_t const * rent = fd_sysvar_cache_rent_read( ctx->sysvar_cache, &rent_ );
2616 0 : if( FD_UNLIKELY( !rent ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2617 :
2618 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L88
2619 0 : rc = initialize( &me, authorized, lockup, rent );
2620 :
2621 : /* implicit drop */
2622 :
2623 0 : break;
2624 0 : }
2625 :
2626 : /* Authorize
2627 : *
2628 : * Instruction:
2629 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L120
2630 : *
2631 : * Processor:
2632 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L90
2633 : */
2634 0 : case fd_stake_instruction_enum_authorize: {
2635 0 : fd_pubkey_t const * authorized_pubkey = &instruction->inner.authorize.pubkey;
2636 0 : fd_stake_authorize_t const * stake_authorize = &instruction->inner.authorize.stake_authorize;
2637 :
2638 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L91
2639 0 : fd_guarded_borrowed_account_t me = {0};
2640 0 : rc = get_stake_account( ctx, &me );
2641 0 : if( FD_UNLIKELY( rc ) ) return rc;
2642 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L92
2643 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_clock_id );
2644 0 : if( FD_UNLIKELY( rc ) ) return rc;
2645 0 : fd_sol_sysvar_clock_t clock_;
2646 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2647 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2648 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L94
2649 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<3 ) )
2650 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2651 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L95
2652 0 : fd_pubkey_t const * custodian_pubkey = NULL;
2653 0 : rc = get_optional_pubkey( ctx, 3, 0, &custodian_pubkey );
2654 0 : if( FD_UNLIKELY( rc ) ) return rc;
2655 :
2656 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L98
2657 0 : rc = authorize( &me,
2658 0 : signers,
2659 0 : authorized_pubkey,
2660 0 : stake_authorize,
2661 0 : clock,
2662 0 : custodian_pubkey,
2663 0 : &ctx->txn_out->err.custom_err );
2664 :
2665 : /* implicit drop */
2666 0 : break;
2667 0 : }
2668 :
2669 : /* AuthorizeWithSeed
2670 : *
2671 : * Instruction:
2672 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L211
2673 : *
2674 : * Processor:
2675 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L107
2676 : */
2677 0 : case fd_stake_instruction_enum_authorize_with_seed: {
2678 0 : fd_authorize_with_seed_args_t args = instruction->inner.authorize_with_seed;
2679 :
2680 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L108
2681 0 : fd_guarded_borrowed_account_t me = {0};
2682 0 : rc = get_stake_account( ctx, &me );
2683 0 : if( FD_UNLIKELY( rc ) ) return rc;
2684 :
2685 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L109
2686 0 : if( ctx->instr->acct_cnt<2 )
2687 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2688 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L110
2689 0 : rc = fd_sysvar_instr_acct_check( ctx, 2, &fd_sysvar_clock_id );
2690 0 : if( FD_UNLIKELY( rc ) ) return rc;
2691 0 : fd_sol_sysvar_clock_t clock_;
2692 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2693 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2694 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L112
2695 0 : fd_pubkey_t const * custodian_pubkey = NULL;
2696 0 : rc = get_optional_pubkey( ctx, 3, 0, &custodian_pubkey );
2697 0 : if( FD_UNLIKELY( rc ) ) return rc;
2698 :
2699 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L115
2700 0 : rc = authorize_with_seed( ctx,
2701 0 : &me,
2702 0 : 1,
2703 0 : (char const *)args.authority_seed,
2704 0 : args.authority_seed_len,
2705 0 : &args.authority_owner,
2706 0 : &args.new_authorized_pubkey,
2707 0 : &args.stake_authorize,
2708 0 : clock,
2709 0 : custodian_pubkey );
2710 :
2711 : /* implicit drop */
2712 0 : break;
2713 0 : }
2714 :
2715 : /* DelegateStake
2716 : *
2717 : * Instruction:
2718 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L135
2719 : *
2720 : * Processor:
2721 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L128
2722 : */
2723 0 : case fd_stake_instruction_enum_delegate_stake: {
2724 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L129
2725 0 : fd_guarded_borrowed_account_t me = {0};
2726 0 : rc = get_stake_account( ctx, &me );
2727 0 : if( FD_UNLIKELY( rc ) ) return rc;
2728 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L130
2729 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<2 ) )
2730 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2731 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L131
2732 0 : rc = fd_sysvar_instr_acct_check( ctx, 2, &fd_sysvar_clock_id );
2733 0 : if( FD_UNLIKELY( rc ) ) return rc;
2734 0 : fd_sol_sysvar_clock_t clock_;
2735 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2736 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2737 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L133
2738 0 : rc = fd_sysvar_instr_acct_check( ctx, 3, &fd_sysvar_stake_history_id );
2739 0 : if( FD_UNLIKELY( rc ) ) return rc;
2740 0 : if( FD_UNLIKELY( !fd_sysvar_cache_stake_history_is_valid( ctx->sysvar_cache ) ) ) {
2741 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2742 0 : }
2743 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L138
2744 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<5 ) )
2745 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2746 :
2747 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_instruction.rs#L138 */
2748 0 : fd_borrowed_account_drop( &me );
2749 :
2750 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L140
2751 0 : fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history_join_const( ctx->sysvar_cache );
2752 0 : rc = delegate( ctx,
2753 0 : 0,
2754 0 : 1,
2755 0 : clock,
2756 0 : stake_history,
2757 0 : signers );
2758 0 : fd_sysvar_cache_stake_history_leave_const( ctx->sysvar_cache, stake_history );
2759 0 : break;
2760 0 : }
2761 :
2762 : /* Split
2763 : *
2764 : * Instruction:
2765 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L143
2766 : *
2767 : * Processor:
2768 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L152
2769 : */
2770 0 : case fd_stake_instruction_enum_split: {
2771 0 : ulong lamports = instruction->inner.split;
2772 :
2773 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L153
2774 0 : fd_guarded_borrowed_account_t me = {0};
2775 0 : rc = get_stake_account( ctx, &me );
2776 0 : if( FD_UNLIKELY( rc ) ) return rc;
2777 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L154
2778 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<2 ) )
2779 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2780 :
2781 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_instruction.rs#L154 */
2782 0 : fd_borrowed_account_drop( &me );
2783 :
2784 : //https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L156
2785 0 : rc = split( ctx, 0, lamports, 1, signers );
2786 0 : break;
2787 0 : }
2788 :
2789 : /* Merge
2790 : *
2791 : * Instruction:
2792 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L201
2793 : *
2794 : * Processor:
2795 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L166
2796 : */
2797 0 : case fd_stake_instruction_enum_merge: {
2798 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L167
2799 0 : fd_guarded_borrowed_account_t me = {0};
2800 0 : rc = get_stake_account( ctx, &me );
2801 0 : if( FD_UNLIKELY( rc ) ) return rc;
2802 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L168
2803 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<2 ) )
2804 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2805 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L169
2806 0 : rc = fd_sysvar_instr_acct_check( ctx, 2, &fd_sysvar_clock_id );
2807 0 : if( FD_UNLIKELY( rc ) ) return rc;
2808 0 : fd_sol_sysvar_clock_t clock_;
2809 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2810 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2811 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L171
2812 0 : rc = fd_sysvar_instr_acct_check( ctx, 3, &fd_sysvar_stake_history_id );
2813 0 : if( FD_UNLIKELY( rc ) ) return rc;
2814 0 : if( FD_UNLIKELY( !fd_sysvar_cache_stake_history_is_valid( ctx->sysvar_cache ) ) ) {
2815 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2816 0 : }
2817 :
2818 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_instruction.rs#L175 */
2819 0 : fd_borrowed_account_drop( &me );
2820 :
2821 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L177
2822 0 : fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history_join_const( ctx->sysvar_cache );
2823 0 : rc = merge( ctx, 0, 1, clock, stake_history, signers );
2824 0 : fd_sysvar_cache_stake_history_leave_const( ctx->sysvar_cache, stake_history );
2825 0 : break;
2826 0 : }
2827 :
2828 : /* Withdraw
2829 : *
2830 : * Instruction:
2831 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L157
2832 : *
2833 : * Processor:
2834 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L188
2835 : */
2836 0 : case fd_stake_instruction_enum_withdraw: {
2837 0 : ulong lamports = instruction->inner.withdraw;
2838 :
2839 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L189
2840 0 : fd_guarded_borrowed_account_t me = {0};
2841 0 : rc = get_stake_account( ctx, &me ); /* calls acquire_write */
2842 0 : if( FD_UNLIKELY( rc ) ) return rc;
2843 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L190
2844 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<2 ) )
2845 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2846 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L191
2847 0 : rc = fd_sysvar_instr_acct_check( ctx, 2, &fd_sysvar_clock_id );
2848 0 : if( FD_UNLIKELY( rc ) ) return rc;
2849 0 : fd_sol_sysvar_clock_t clock_;
2850 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2851 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2852 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L193
2853 0 : rc = fd_sysvar_instr_acct_check( ctx, 3, &fd_sysvar_stake_history_id );
2854 0 : if( FD_UNLIKELY( rc ) ) return rc;
2855 0 : if( FD_UNLIKELY( !fd_sysvar_cache_stake_history_is_valid( ctx->sysvar_cache ) ) ) {
2856 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2857 0 : }
2858 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L198
2859 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<5 ) )
2860 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2861 :
2862 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_instruction.rs#L198 */
2863 0 : fd_borrowed_account_drop( &me );
2864 :
2865 0 : fd_epoch_schedule_t epoch_schedule_[1];
2866 0 : fd_epoch_schedule_t const * epoch_schedule = fd_sysvar_cache_epoch_schedule_read( ctx->sysvar_cache, epoch_schedule_ );
2867 0 : uchar custodian_index = 5;
2868 0 : ulong new_rate_activation_epoch = ULONG_MAX;
2869 0 : int err;
2870 0 : int is_some = fd_new_warmup_cooldown_rate_epoch(
2871 0 : epoch_schedule,
2872 0 : fd_bank_features_query( ctx->bank ),
2873 0 : fd_bank_slot_get( ctx->bank ),
2874 0 : &new_rate_activation_epoch,
2875 0 : &err );
2876 0 : if( FD_UNLIKELY( err ) ) return err;
2877 :
2878 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L200
2879 0 : fd_stake_history_t const * stake_history = fd_sysvar_cache_stake_history_join_const( ctx->sysvar_cache );
2880 0 : rc = withdraw(
2881 0 : ctx,
2882 0 : 0,
2883 0 : lamports,
2884 0 : 1,
2885 0 : clock,
2886 0 : stake_history,
2887 0 : 4,
2888 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L209-L215
2889 0 : fd_ptr_if( ctx->instr->acct_cnt>=6, &custodian_index, NULL ),
2890 0 : fd_ptr_if( is_some, &new_rate_activation_epoch, NULL ) );
2891 0 : fd_sysvar_cache_stake_history_leave_const( ctx->sysvar_cache, stake_history );
2892 :
2893 0 : break;
2894 0 : }
2895 : /* Deactivate
2896 : *
2897 : * Instruction:
2898 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L165
2899 : *
2900 : * Processor:
2901 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L217
2902 : */
2903 0 : case fd_stake_instruction_enum_deactivate: {
2904 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L218
2905 0 : fd_guarded_borrowed_account_t me = {0};
2906 0 : rc = get_stake_account( ctx, &me );
2907 0 : if( FD_UNLIKELY( rc ) ) return rc;
2908 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L219
2909 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_clock_id );
2910 0 : if( FD_UNLIKELY( rc ) ) return rc;
2911 0 : fd_sol_sysvar_clock_t clock_;
2912 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2913 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2914 :
2915 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L221
2916 0 : rc = deactivate( &me, clock, signers, &ctx->txn_out->err.custom_err );
2917 :
2918 : /* implicit drop */
2919 0 : break;
2920 0 : }
2921 :
2922 : /* SetLockup
2923 : *
2924 : * Instruction:
2925 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L175
2926 : *
2927 : * Processor:
2928 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L223
2929 : */
2930 0 : case fd_stake_instruction_enum_set_lockup: {
2931 0 : fd_lockup_args_t * lockup = &instruction->inner.set_lockup;
2932 :
2933 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L224
2934 0 : fd_guarded_borrowed_account_t me = {0};
2935 0 : rc = get_stake_account( ctx, &me );
2936 0 : if( FD_UNLIKELY( rc ) ) return rc;
2937 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L225
2938 0 : fd_sol_sysvar_clock_t clock_;
2939 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
2940 0 : if( FD_UNLIKELY( !clock ) )
2941 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2942 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L226
2943 0 : rc = set_lockup( &me, lockup, signers, clock );
2944 0 : break;
2945 0 : }
2946 :
2947 : /* InitializeChecked
2948 : *
2949 : * Instruction:
2950 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L224
2951 : *
2952 : * Processor:
2953 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L228
2954 : */
2955 0 : case fd_stake_instruction_enum_initialize_checked: {
2956 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L229
2957 0 : fd_guarded_borrowed_account_t me = {0};
2958 0 : rc = get_stake_account( ctx, &me );
2959 0 : if( FD_UNLIKELY( rc ) ) return rc;
2960 :
2961 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L230
2962 0 : rc = fd_exec_instr_ctx_check_num_insn_accounts( ctx, 4UL );
2963 0 : if( FD_UNLIKELY( rc ) ) return rc;
2964 :
2965 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_instruction.rs#L230-L236 */
2966 0 : fd_pubkey_t const * staker_pubkey = NULL;
2967 0 : fd_pubkey_t const * withdrawer_pubkey = NULL;
2968 :
2969 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 2UL, &staker_pubkey );
2970 0 : if( FD_UNLIKELY( rc ) ) return rc;
2971 :
2972 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 3UL, &withdrawer_pubkey );
2973 0 : if( FD_UNLIKELY( rc ) ) return rc;
2974 :
2975 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L237
2976 0 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx->instr, 3, NULL ) ) )
2977 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
2978 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L241
2979 0 : fd_stake_authorized_t authorized = { .staker = *staker_pubkey,
2980 0 : .withdrawer = *withdrawer_pubkey };
2981 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L246
2982 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_rent_id );
2983 0 : if( FD_UNLIKELY( rc ) ) return rc;
2984 0 : fd_rent_t rent_;
2985 0 : fd_rent_t const * rent = fd_sysvar_cache_rent_read( ctx->sysvar_cache, &rent_ );
2986 0 : if( FD_UNLIKELY( !rent ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2987 :
2988 0 : fd_stake_lockup_t lockup_default = {0};
2989 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L247
2990 0 : rc = initialize( &me, &authorized, &lockup_default, rent );
2991 :
2992 : /* implicit drop */
2993 0 : break;
2994 0 : }
2995 :
2996 : /* AuthorizeChecked
2997 : *
2998 : * Instruction:
2999 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L238
3000 : *
3001 : * Processor:
3002 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L249
3003 : */
3004 0 : case fd_stake_instruction_enum_authorize_checked: {
3005 0 : fd_stake_authorize_t const * stake_authorize = &instruction->inner.authorize_checked;
3006 :
3007 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L250
3008 0 : fd_guarded_borrowed_account_t me = {0};
3009 0 : rc = get_stake_account( ctx, &me );
3010 0 : if( FD_UNLIKELY( rc ) ) return rc;
3011 :
3012 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L251
3013 0 : rc = fd_sysvar_instr_acct_check( ctx, 1, &fd_sysvar_clock_id );
3014 0 : if( FD_UNLIKELY( rc ) ) return rc;
3015 0 : fd_sol_sysvar_clock_t clock_;
3016 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
3017 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
3018 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L253
3019 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<4 ) )
3020 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
3021 :
3022 : // https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_instruction.rs#L253
3023 0 : fd_pubkey_t const * authorized_pubkey = NULL;
3024 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 3UL, &authorized_pubkey );
3025 0 : if( FD_UNLIKELY( rc ) ) return rc;
3026 :
3027 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L257
3028 0 : int is_signer = fd_instr_acc_is_signer_idx( ctx->instr, 3, NULL );
3029 0 : if( FD_UNLIKELY( !is_signer ) ) return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
3030 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L260
3031 0 : fd_pubkey_t const * custodian_pubkey = NULL;
3032 0 : rc = get_optional_pubkey( ctx, 4, 0, &custodian_pubkey );
3033 0 : if( FD_UNLIKELY( rc ) ) return rc;
3034 :
3035 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L263
3036 0 : rc = authorize( &me,
3037 0 : signers,
3038 0 : authorized_pubkey,
3039 0 : stake_authorize,
3040 0 : clock,
3041 0 : custodian_pubkey,
3042 0 : &ctx->txn_out->err.custom_err );
3043 0 : break;
3044 0 : }
3045 :
3046 : /* AuthorizeCheckedWithSeed
3047 : *
3048 : * Instruction:
3049 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L252
3050 : *
3051 : * Processor:
3052 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L272
3053 : */
3054 0 : case fd_stake_instruction_enum_authorize_checked_with_seed: {
3055 0 : fd_authorize_checked_with_seed_args_t const * args =
3056 0 : &instruction->inner.authorize_checked_with_seed;
3057 :
3058 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L273
3059 0 : fd_guarded_borrowed_account_t me = {0};
3060 0 : rc = get_stake_account( ctx, &me );
3061 0 : if( FD_UNLIKELY( rc ) ) return rc;
3062 :
3063 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L274
3064 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<2 ) )
3065 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
3066 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L276
3067 0 : rc = fd_sysvar_instr_acct_check( ctx, 2, &fd_sysvar_clock_id );
3068 0 : if( FD_UNLIKELY( rc ) ) return rc;
3069 0 : fd_sol_sysvar_clock_t clock_;
3070 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
3071 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
3072 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L277
3073 0 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( ctx, 4U) ) )
3074 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
3075 :
3076 : // https://github.com/anza-xyz/agave/blob/v2.1.14/programs/stake/src/stake_instruction.rs#L277-L280
3077 0 : fd_pubkey_t const * authorized_pubkey = NULL;
3078 0 : rc = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 3UL, &authorized_pubkey );
3079 0 : if( FD_UNLIKELY( rc ) ) return rc;
3080 :
3081 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L281
3082 0 : int is_signer = fd_instr_acc_is_signer_idx( ctx->instr, 3, NULL );
3083 0 : if( FD_UNLIKELY( !is_signer ) ) return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
3084 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L284
3085 0 : fd_pubkey_t const * custodian_pubkey = NULL;
3086 0 : rc = get_optional_pubkey( ctx, 4, 0, &custodian_pubkey );
3087 0 : if( FD_UNLIKELY( rc ) ) return rc;
3088 :
3089 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L287
3090 0 : rc = authorize_with_seed( ctx,
3091 0 : &me,
3092 0 : 1,
3093 0 : (char const *)args->authority_seed,
3094 0 : args->authority_seed_len,
3095 0 : &args->authority_owner,
3096 0 : authorized_pubkey,
3097 0 : &args->stake_authorize,
3098 0 : clock,
3099 0 : custodian_pubkey );
3100 0 : break;
3101 0 : }
3102 :
3103 : /* SetLockupChecked
3104 : *
3105 : * Instruction:
3106 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L266
3107 : *
3108 : * Processor:
3109 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L300
3110 : */
3111 0 : case fd_stake_instruction_enum_set_lockup_checked: {
3112 0 : fd_lockup_checked_args_t * lockup_checked = &instruction->inner.set_lockup_checked;
3113 :
3114 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L301
3115 0 : fd_guarded_borrowed_account_t me = {0};
3116 0 : rc = get_stake_account( ctx, &me ); /* acquire_write */
3117 0 : if( FD_UNLIKELY( rc ) ) return rc;
3118 :
3119 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L302
3120 0 : fd_pubkey_t const * custodian_pubkey = NULL;
3121 0 : rc = get_optional_pubkey( ctx, 2, 1, &custodian_pubkey );
3122 0 : if( FD_UNLIKELY( rc ) ) return rc;
3123 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L305
3124 0 : fd_lockup_args_t lockup = { .unix_timestamp = lockup_checked->unix_timestamp,
3125 0 : .epoch = lockup_checked->epoch,
3126 0 : .custodian = (fd_pubkey_t *)custodian_pubkey }; // FIXME
3127 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L310
3128 0 : fd_sol_sysvar_clock_t clock_;
3129 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
3130 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
3131 :
3132 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L311
3133 0 : rc = set_lockup( &me, &lockup, signers, clock );
3134 0 : break;
3135 0 : }
3136 :
3137 : /* GetMinimumDelegation
3138 : *
3139 : * Instruction:
3140 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L278
3141 : *
3142 : * Processor:
3143 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L313
3144 : */
3145 0 : case fd_stake_instruction_enum_get_minimum_delegation: {
3146 0 : ulong minimum_delegation = get_minimum_delegation( ctx->bank );
3147 0 : fd_memcpy( &ctx->txn_out->details.return_data.program_id, fd_solana_stake_program_id.key, sizeof(fd_pubkey_t));
3148 0 : fd_memcpy(ctx->txn_out->details.return_data.data, (uchar*)(&minimum_delegation), sizeof(ulong));
3149 0 : ctx->txn_out->details.return_data.len = sizeof(ulong);
3150 0 : rc = 0;
3151 0 : goto done;
3152 0 : }
3153 :
3154 : /* DeactivateDelinquent
3155 : *
3156 : * Instruction:
3157 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/instruction.rs#L291
3158 : *
3159 : * Processor:
3160 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L321
3161 : */
3162 0 : case fd_stake_instruction_enum_deactivate_delinquent: {
3163 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L322
3164 0 : fd_guarded_borrowed_account_t me = {0};
3165 0 : rc = get_stake_account( ctx, &me );
3166 0 : if( FD_UNLIKELY( rc ) ) return rc;
3167 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L323
3168 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<3 ) )
3169 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
3170 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L325
3171 0 : fd_sol_sysvar_clock_t clock_;
3172 0 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, &clock_ );
3173 0 : if( FD_UNLIKELY( !clock ) ) return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
3174 :
3175 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L326
3176 0 : rc = deactivate_delinquent( ctx, &me, 1, 2, clock->epoch, &ctx->txn_out->err.custom_err );
3177 0 : break;
3178 0 : }
3179 :
3180 : /* Redelegate
3181 : *
3182 : * Deprecated:
3183 : * https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L336
3184 : */
3185 0 : case fd_stake_instruction_enum_redelegate: {
3186 0 : fd_guarded_borrowed_account_t me = {0};
3187 0 : rc = get_stake_account( ctx, &me );
3188 0 : if( FD_UNLIKELY( rc ) ) return rc;
3189 :
3190 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
3191 0 : }
3192 : /* MoveStake
3193 : *
3194 : * Instruction:
3195 : * https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/sdk/program/src/stake/instruction.rs#L330
3196 : *
3197 : * Processor:
3198 : * https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_instruction.rs#L356
3199 : */
3200 0 : case fd_stake_instruction_enum_move_stake: {
3201 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_instruction.rs#L361
3202 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<3 ) )
3203 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
3204 :
3205 0 : ulong lamports = instruction->inner.move_stake;
3206 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_instruction.rs#L362
3207 0 : rc = move_stake( ctx,
3208 0 : 0UL,
3209 0 : lamports,
3210 0 : 1UL,
3211 0 : 2UL,
3212 0 : &ctx->txn_out->err.custom_err );
3213 :
3214 0 : break;
3215 0 : }
3216 : /* MoveLamports
3217 : *
3218 : * Instruction:
3219 : * https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/sdk/program/src/stake/instruction.rs#L345
3220 : *
3221 : * Processor:
3222 : * https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_instruction.rs#L375
3223 : */
3224 0 : case fd_stake_instruction_enum_move_lamports: {
3225 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_instruction.rs#L380
3226 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt<3 ) )
3227 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
3228 :
3229 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_instruction.rs#L381
3230 0 : ulong lamports = instruction->inner.move_lamports;
3231 :
3232 : // https://github.com/anza-xyz/agave/blob/cdff19c7807b006dd63429114fb1d9573bf74172/programs/stake/src/stake_instruction.rs#L381
3233 0 : rc = move_lamports( ctx,
3234 0 : 0UL,
3235 0 : lamports,
3236 0 : 1UL,
3237 0 : 2UL );
3238 :
3239 0 : break;
3240 0 : }
3241 0 : default:
3242 0 : FD_LOG_ERR(( "unsupported stake instruction: %u", instruction->discriminant ));
3243 0 : }
3244 :
3245 0 : done:
3246 0 : return rc;
3247 0 : }
3248 :
3249 : /* Public API *********************************************************/
3250 :
3251 : static void
3252 : write_stake_config( fd_accdb_user_t * accdb,
3253 : fd_funk_txn_xid_t const * xid,
3254 0 : fd_stake_config_t const * stake_config ) {
3255 0 : ulong data_sz = fd_stake_config_size( stake_config );
3256 0 : fd_pubkey_t const * acc_key = &fd_solana_stake_program_config_id;
3257 :
3258 0 : fd_txn_account_t rec[1];
3259 0 : fd_funk_rec_prepare_t prepare = {0};
3260 0 : FD_TEST( fd_txn_account_init_from_funk_mutable( rec, acc_key, accdb, xid, 1, data_sz, &prepare ) );
3261 :
3262 0 : fd_txn_account_set_lamports( rec, 960480UL );
3263 0 : fd_txn_account_set_executable( rec, 0 );
3264 :
3265 0 : fd_bincode_encode_ctx_t ctx3;
3266 0 : ctx3.data = fd_txn_account_get_data_mut( rec );
3267 0 : ctx3.dataend = fd_txn_account_get_data_mut( rec ) + data_sz;
3268 0 : if( fd_stake_config_encode( stake_config, &ctx3 ) )
3269 0 : FD_LOG_ERR( ( "fd_stake_config_encode failed" ) );
3270 :
3271 0 : fd_txn_account_set_data( rec, stake_config, data_sz );
3272 :
3273 0 : fd_txn_account_mutable_fini( rec, accdb, &prepare );
3274 0 : }
3275 :
3276 : void
3277 : fd_stake_program_config_init( fd_accdb_user_t * accdb,
3278 0 : fd_funk_txn_xid_t const * xid ) {
3279 : // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/sdk/program/src/stake/config.rs#L26
3280 0 : fd_stake_config_t stake_config = {
3281 0 : .warmup_cooldown_rate = DEFAULT_WARMUP_COOLDOWN_RATE,
3282 0 : .slash_penalty = DEFAULT_SLASH_PENALTY,
3283 0 : };
3284 0 : write_stake_config( accdb, xid, &stake_config );
3285 0 : }
3286 :
3287 : int
3288 : fd_stake_get_state( fd_txn_account_t const * self,
3289 0 : fd_stake_state_v2_t * out ) {
3290 0 : return get_state( self, out );
3291 0 : }
3292 :
3293 : fd_stake_history_entry_t
3294 : fd_stake_activating_and_deactivating( fd_delegation_t const * self,
3295 : ulong target_epoch,
3296 : fd_stake_history_t const * stake_history,
3297 0 : ulong * new_rate_activation_epoch ) {
3298 0 : return stake_activating_and_deactivating(
3299 0 : self, target_epoch, stake_history, new_rate_activation_epoch );
3300 0 : }
|