Skip to content

Commit

Permalink
clean up vote_stake_checked_instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
kbhargava-jump committed Aug 5, 2024
1 parent d258d43 commit cfe1b6f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 128 deletions.
198 changes: 90 additions & 108 deletions src/flamenco/runtime/program/fd_stake_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,13 +1055,11 @@ merge_kind_merge( merge_kind_t self,

// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1206
ulong source_lamports = ULONG_MAX;
rc = fd_ulong_checked_add(
source_meta.rent_exempt_reserve, source_stake.delegation.stake, &source_lamports );
rc = fd_ulong_checked_add( source_meta.rent_exempt_reserve, source_stake.delegation.stake, &source_lamports );
if( FD_UNLIKELY( rc ) ) return rc;

// // https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1210
rc = merge_delegation_stake_and_credits_observed(
invoke_context, &stake, source_lamports, source_stake.credits_observed );
rc = merge_delegation_stake_and_credits_observed(invoke_context, &stake, source_lamports, source_stake.credits_observed );
if( FD_UNLIKELY( rc ) ) return rc;

// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_state.rs#L1215
Expand Down Expand Up @@ -2908,29 +2906,25 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
rc = get_stake_account( &ctx, &me );
if( FD_UNLIKELY( rc ) ) return rc;

if( FD_LIKELY( FD_FEATURE_ACTIVE( ctx.slot_ctx, vote_stake_checked_instructions ) ) ) {
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L230
if( FD_UNLIKELY( ctx.instr->acct_cnt<4 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L231-L236
fd_pubkey_t const * staker_pubkey = &ctx.instr->acct_pubkeys[2];
fd_pubkey_t const * withdrawer_pubkey = &ctx.instr->acct_pubkeys[3];
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L237
if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx.instr, 3 ) ) )
return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L241
fd_stake_authorized_t authorized = { .staker = *staker_pubkey,
.withdrawer = *withdrawer_pubkey };
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L246
fd_rent_t const * rent = fd_sysvar_from_instr_acct_rent( &ctx, 1, &rc );
if( FD_UNLIKELY( !rent ) ) return rc;

fd_stake_lockup_t lockup_default = {0};
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L247
rc = initialize( &ctx, me, 0, &authorized, &lockup_default, rent );
} else {
rc = FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L230
if( FD_UNLIKELY( ctx.instr->acct_cnt<4 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L231-L236
fd_pubkey_t const * staker_pubkey = &ctx.instr->acct_pubkeys[2];
fd_pubkey_t const * withdrawer_pubkey = &ctx.instr->acct_pubkeys[3];
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L237
if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx.instr, 3 ) ) )
return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L241
fd_stake_authorized_t authorized = { .staker = *staker_pubkey,
.withdrawer = *withdrawer_pubkey };
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L246
fd_rent_t const * rent = fd_sysvar_from_instr_acct_rent( &ctx, 1, &rc );
if( FD_UNLIKELY( !rent ) ) return rc;

fd_stake_lockup_t lockup_default = {0};
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L247
rc = initialize( &ctx, me, 0, &authorized, &lockup_default, rent );

fd_borrowed_account_release_write( me ); /* implicit drop */
break;
Expand All @@ -2952,36 +2946,32 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
rc = get_stake_account( &ctx, &me );
if( FD_UNLIKELY( rc ) ) return rc;

if( FD_LIKELY( FD_FEATURE_ACTIVE( ctx.slot_ctx, vote_stake_checked_instructions ) ) ) {
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L251
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 1, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L253
if( FD_UNLIKELY( ctx.instr->acct_cnt<4 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L254
fd_pubkey_t const * authorized_pubkey = &ctx.instr->acct_pubkeys[3];
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L257
int is_signer = fd_instr_acc_is_signer_idx( ctx.instr, 3 );
if( FD_UNLIKELY( !is_signer ) ) return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L260
fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 4, 0, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L251
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 1, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L253
if( FD_UNLIKELY( ctx.instr->acct_cnt<4 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L254
fd_pubkey_t const * authorized_pubkey = &ctx.instr->acct_pubkeys[3];
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L257
int is_signer = fd_instr_acc_is_signer_idx( ctx.instr, 3 );
if( FD_UNLIKELY( !is_signer ) ) return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L260
fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 4, 0, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;

// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L263
rc = authorize( &ctx,
me,
0,
signers,
authorized_pubkey,
stake_authorize,
clock,
custodian_pubkey,
&ctx.txn_ctx->custom_err );
} else {
rc = FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L263
rc = authorize( &ctx,
me,
0,
signers,
authorized_pubkey,
stake_authorize,
clock,
custodian_pubkey,
&ctx.txn_ctx->custom_err );

fd_borrowed_account_release_write( me ); /* implicit drop */
break;
Expand All @@ -3006,41 +2996,37 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
rc = get_stake_account( &ctx, &me );
if( FD_UNLIKELY( rc ) ) return rc;

if( FD_LIKELY( FD_FEATURE_ACTIVE( ctx.slot_ctx, vote_stake_checked_instructions ) ) ) {
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L274
if( FD_UNLIKELY( ctx.instr->acct_cnt<2 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L276
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 2, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L277
if( FD_UNLIKELY( ctx.instr->acct_cnt<4 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L278
fd_pubkey_t const * authorized_pubkey = &ctx.instr->acct_pubkeys[3];
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L281
int is_signer = fd_instr_acc_is_signer_idx( ctx.instr, 3 );
if( FD_UNLIKELY( !is_signer ) ) return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L284
fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 4, 0, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L274
if( FD_UNLIKELY( ctx.instr->acct_cnt<2 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L276
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 2, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L277
if( FD_UNLIKELY( ctx.instr->acct_cnt<4 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L278
fd_pubkey_t const * authorized_pubkey = &ctx.instr->acct_pubkeys[3];
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L281
int is_signer = fd_instr_acc_is_signer_idx( ctx.instr, 3 );
if( FD_UNLIKELY( !is_signer ) ) return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L284
fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 4, 0, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;

// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L287
rc = authorize_with_seed( &ctx,
me,
0,
1,
(char const *)args->authority_seed,
args->authority_seed_len,
&args->authority_owner,
authorized_pubkey,
&args->stake_authorize,
clock,
custodian_pubkey );
} else {
rc = FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L287
rc = authorize_with_seed( &ctx,
me,
0,
1,
(char const *)args->authority_seed,
args->authority_seed_len,
&args->authority_owner,
authorized_pubkey,
&args->stake_authorize,
clock,
custodian_pubkey );

fd_borrowed_account_release_write( me ); /* implicit drop */
break;
Expand All @@ -3062,25 +3048,21 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
rc = get_stake_account( &ctx, &me ); /* acquire_write */
if( FD_UNLIKELY( rc ) ) return rc;

if( FD_LIKELY( FD_FEATURE_ACTIVE( ctx.slot_ctx, vote_stake_checked_instructions ) ) ) {
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L302
fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 2, 1, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L305
fd_lockup_args_t lockup = { .unix_timestamp = lockup_checked->unix_timestamp,
.epoch = lockup_checked->epoch,
.custodian = (fd_pubkey_t *)custodian_pubkey }; // FIXME
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L310
fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock( ctx.slot_ctx->sysvar_cache );
if( FD_UNLIKELY( !clock ) )
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L302
fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 2, 1, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L305
fd_lockup_args_t lockup = { .unix_timestamp = lockup_checked->unix_timestamp,
.epoch = lockup_checked->epoch,
.custodian = (fd_pubkey_t *)custodian_pubkey }; // FIXME
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L310
fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock( ctx.slot_ctx->sysvar_cache );
if( FD_UNLIKELY( !clock ) )
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;

// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L311
rc = set_lockup( &ctx, me, 0, &lockup, signers, clock );
} else {
rc = FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}
// https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/stake/src/stake_instruction.rs#L311
rc = set_lockup( &ctx, me, 0, &lockup, signers, clock );

fd_borrowed_account_release_write( me ); /* implicit drop */
break;
Expand Down
35 changes: 15 additions & 20 deletions src/flamenco/runtime/program/fd_vote_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -2791,30 +2791,25 @@ fd_vote_program_execute( fd_exec_instr_ctx_t ctx ) {
* - Feature gated, but live on mainnet.
*/
case fd_vote_instruction_enum_authorize_checked: {
if( FD_LIKELY( FD_FEATURE_ACTIVE( ctx.slot_ctx, vote_stake_checked_instructions ) ) ) {
if( FD_UNLIKELY( ctx.instr->acct_cnt < 4 ) ) {
rc = FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
break;
}

// https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L236
fd_pubkey_t const * voter_pubkey = &ctx.instr->acct_pubkeys[3];

// https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L239
if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx.instr, 3 ) ) ) {
rc = FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
break;
}
if( FD_UNLIKELY( ctx.instr->acct_cnt < 4 ) ) {
rc = FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
break;
}

// https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L242
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 1, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;
// https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L236
fd_pubkey_t const * voter_pubkey = &ctx.instr->acct_pubkeys[3];

rc = authorize( 0, me, voter_pubkey, instruction.inner.authorize_checked, signers, clock, &ctx );
} else {
rc = FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
// https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L239
if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx.instr, 3 ) ) ) {
rc = FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
break;
}

// https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L242
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 1, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;

rc = authorize( 0, me, voter_pubkey, instruction.inner.authorize_checked, signers, clock, &ctx );
break;
}

Expand Down

0 comments on commit cfe1b6f

Please sign in to comment.