Skip to content

Commit

Permalink
Instruction processing fixes (#1631)
Browse files Browse the repository at this point in the history
* runtime: fix fd_exec_instr_test

* runtime: hardcode require_custodian_for_locked_stake_authorize feature

---------

Co-authored-by: Richard Patel <[email protected]>
  • Loading branch information
ripatel-fd and riptl authored Apr 24, 2024
1 parent e30d4ae commit 7eecb38
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 81 deletions.
111 changes: 40 additions & 71 deletions src/flamenco/runtime/program/fd_stake_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -915,38 +915,38 @@ stake_weighted_credits_observed( fd_stake_t const * stake,
/* stake_weighted_credits = stake->credits_observed + stake->delegation.stake */
ulong stake_weighted_credits_h;
ulong stake_weighted_credits_l;
fd_uwide_mul( &stake_weighted_credits_h, &stake_weighted_credits_l,
fd_uwide_mul( &stake_weighted_credits_h, &stake_weighted_credits_l,
stake->credits_observed, stake->delegation.stake );

/* absorbed_weighted_credits = absorbed_credits_observed * absorbed_lamports */
ulong absorbed_weighted_credits_h;
ulong absorbed_weighted_credits_l;
fd_uwide_mul( &absorbed_weighted_credits_h, &absorbed_weighted_credits_l,
fd_uwide_mul( &absorbed_weighted_credits_h, &absorbed_weighted_credits_l,
absorbed_credits_observed, absorbed_lamports );

/* total_weighted_credits = stake_weighted_credits + total_stake + absorbed_weighted_credits - 1*/
ulong total_weighted_credits_partial_one_h;
ulong total_weighted_credits_partial_one_l;
fd_uwide_add( &total_weighted_credits_partial_one_h, &total_weighted_credits_partial_one_l,
fd_uwide_add( &total_weighted_credits_partial_one_h, &total_weighted_credits_partial_one_l,
stake_weighted_credits_h, stake_weighted_credits_l,
absorbed_weighted_credits_h, absorbed_weighted_credits_l, 0 );

ulong total_weighted_credits_partial_two_h;
ulong total_weighted_credits_partial_two_l;
fd_uwide_add( &total_weighted_credits_partial_two_h, &total_weighted_credits_partial_two_l,
fd_uwide_add( &total_weighted_credits_partial_two_h, &total_weighted_credits_partial_two_l,
total_weighted_credits_partial_one_h, total_weighted_credits_partial_one_l,
total_stake_h, total_stake_l, 0 );

ulong total_weighted_credits_h;
ulong total_weighted_credits_l;
fd_uwide_dec( &total_weighted_credits_h, &total_weighted_credits_l,
fd_uwide_dec( &total_weighted_credits_h, &total_weighted_credits_l,
total_weighted_credits_partial_two_h, total_weighted_credits_partial_two_l, 1 );

/* FIXME: fd_uwide_div doesn't support denominator that is an fd_uwide */
/* res = totalWeighted_credits / total_stake */
ulong res_h;
ulong res_l;
FD_TEST(( total_stake_h == 0 ));
FD_TEST(( total_stake_h == 0 ));
fd_uwide_div( &res_h, &res_l, total_weighted_credits_h, total_weighted_credits_l, total_stake_l );
FD_TEST(( res_h == 0 ));
//*out = total_weighted_credits / total_stake;
Expand Down Expand Up @@ -1234,7 +1234,6 @@ authorize( fd_exec_instr_ctx_t const * ctx,
fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
fd_pubkey_t const * new_authority,
fd_stake_authorize_t const * stake_authorize,
int require_custodian_for_locked_stake_authorize,
fd_sol_sysvar_clock_t const * clock,
fd_pubkey_t const * custodian,
uint * custom_err ) {
Expand All @@ -1255,7 +1254,7 @@ authorize( fd_exec_instr_ctx_t const * ctx,
signers,
new_authority,
stake_authorize,
fd_ptr_if( require_custodian_for_locked_stake_authorize, &lockup_custodian_args, NULL ),
&lockup_custodian_args,
custom_err );
if( FD_UNLIKELY( rc ) ) return rc;

Expand All @@ -1271,7 +1270,7 @@ authorize( fd_exec_instr_ctx_t const * ctx,
signers,
new_authority,
stake_authorize,
fd_ptr_if( require_custodian_for_locked_stake_authorize, &lockup_custodian_args, NULL ),
&lockup_custodian_args,
custom_err );
if( FD_UNLIKELY( rc ) ) return rc;

Expand All @@ -1294,7 +1293,6 @@ authorize_with_seed( fd_exec_instr_ctx_t const * ctx,
fd_pubkey_t const * authority_owner,
fd_pubkey_t const * new_authority,
fd_stake_authorize_t const * stake_authorize,
int require_custodian_for_locked_stake_authorize,
fd_sol_sysvar_clock_t const * clock,
fd_pubkey_t const * custodian ) {
int rc;
Expand All @@ -1321,7 +1319,6 @@ authorize_with_seed( fd_exec_instr_ctx_t const * ctx,
signers,
new_authority,
stake_authorize,
require_custodian_for_locked_stake_authorize,
clock,
custodian,
&ctx->txn_ctx->custom_err );
Expand Down Expand Up @@ -2323,42 +2320,24 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
rc = get_stake_account( &ctx, &me );
if( FD_UNLIKELY( rc ) ) return rc;

int require_custodian_for_locked_stake_authorize =
FD_FEATURE_ACTIVE( ctx.slot_ctx, require_custodian_for_locked_stake_authorize );

if( FD_LIKELY( require_custodian_for_locked_stake_authorize ) ) {
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 1, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 1, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;

if( FD_UNLIKELY( ctx.instr->acct_cnt < 3 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
if( FD_UNLIKELY( ctx.instr->acct_cnt < 3 ) )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;

fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 3, 0, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;
rc = authorize( &ctx,
me,
0,
signers,
authorized_pubkey,
stake_authorize,
require_custodian_for_locked_stake_authorize,
clock,
custodian_pubkey,
&ctx.txn_ctx->custom_err );
} else {
fd_sol_sysvar_clock_t clock_default = { 0 };
rc = authorize( &ctx,
me,
0,
signers,
authorized_pubkey,
stake_authorize,
require_custodian_for_locked_stake_authorize,
&clock_default,
NULL,
&ctx.txn_ctx->custom_err );
}
fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 3, 0, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;
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 @@ -2384,32 +2363,24 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
if( ctx.instr->acct_cnt < 2 )
return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;

int require_custodian_for_locked_stake_authorize =
FD_FEATURE_ACTIVE( ctx.slot_ctx, require_custodian_for_locked_stake_authorize );

if( FD_LIKELY( require_custodian_for_locked_stake_authorize ) ) {
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 2, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;
fd_sol_sysvar_clock_t const * clock = fd_sysvar_from_instr_acct_clock( &ctx, 2, &rc );
if( FD_UNLIKELY( !clock ) ) return rc;

fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 3, 0, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;
fd_pubkey_t const * custodian_pubkey = NULL;
rc = get_optional_pubkey( ctx, 3, 0, &custodian_pubkey );
if( FD_UNLIKELY( rc ) ) return rc;

rc = authorize_with_seed( &ctx,
me,
0,
1,
(char const *)args.authority_seed,
args.authority_seed_len,
&args.authority_owner,
&args.new_authorized_pubkey,
&args.stake_authorize,
require_custodian_for_locked_stake_authorize,
clock,
custodian_pubkey );
} else {
rc = FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}
rc = authorize_with_seed( &ctx,
me,
0,
1,
(char const *)args.authority_seed,
args.authority_seed_len,
&args.authority_owner,
&args.new_authorized_pubkey,
&args.stake_authorize,
clock,
custodian_pubkey );

fd_borrowed_account_release_write( me ); /* implicit drop */
break;
Expand Down Expand Up @@ -2706,7 +2677,6 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
signers,
authorized_pubkey,
stake_authorize,
1,
clock,
custodian_pubkey,
&ctx.txn_ctx->custom_err );
Expand Down Expand Up @@ -2765,7 +2735,6 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
&args->authority_owner,
authorized_pubkey,
&args->stake_authorize,
1,
clock,
custodian_pubkey );
} else {
Expand Down
24 changes: 15 additions & 9 deletions src/flamenco/runtime/tests/fd_exec_instr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../context/fd_exec_slot_ctx.h"
#include "../context/fd_exec_txn_ctx.h"
#include "../../../funk/fd_funk.h"
#include "../../../util/bits/fd_float.h"
#include <assert.h>

#pragma GCC diagnostic ignored "-Wformat-extra-args"
Expand Down Expand Up @@ -73,6 +74,7 @@ fd_exec_instr_test_runner_new( void * mem,
FD_LOG_WARNING(( "fd_funk_new() failed" ));
return NULL;
}
fd_funk_start_write( funk );

fd_exec_instr_test_runner_t * runner = runner_mem;
runner->funk = funk;
Expand All @@ -88,12 +90,18 @@ fd_exec_instr_test_runner_delete( fd_exec_instr_test_runner_t * runner ) {
}

static int
fd_double_is_normal( double x ) {
union {
double d;
ulong ul;
} u = { .d = x };
return !( (!(u.ul>>52 & 0x7ff)) && (u.ul<<1) );
fd_double_is_normal( double dbl ) {
ulong x = fd_dblbits( dbl );
int is_denorm =
( fd_dblbits_bexp( x ) == 0 ) |
( fd_dblbits_mant( x ) != 0 );
int is_inf =
( fd_dblbits_bexp( x ) == 2047 ) &
( fd_dblbits_mant( x ) == 0 );
int is_nan =
( fd_dblbits_bexp( x ) == 2047 ) &
( fd_dblbits_mant( x ) != 0 );
return !( is_denorm | is_inf | is_nan );
}

static int
Expand Down Expand Up @@ -158,8 +166,7 @@ _context_create( fd_exec_instr_test_runner_t * runner,
fd_scratch_push();

/* Allocate contexts */
fd_wksp_t * wksp = fd_wksp_new_anonymous( FD_SHMEM_GIGANTIC_PAGE_SZ, 5, 0, "wksp", 0UL );
uchar * epoch_ctx_mem = fd_wksp_alloc_laddr( wksp, fd_exec_epoch_ctx_align(), fd_exec_epoch_ctx_footprint(), FD_EXEC_EPOCH_CTX_MAGIC );;
uchar * epoch_ctx_mem = fd_scratch_alloc( fd_exec_epoch_ctx_align(), fd_exec_epoch_ctx_footprint() );
uchar * slot_ctx_mem = fd_scratch_alloc( FD_EXEC_SLOT_CTX_ALIGN, FD_EXEC_SLOT_CTX_FOOTPRINT );
uchar * txn_ctx_mem = fd_scratch_alloc( FD_EXEC_TXN_CTX_ALIGN, FD_EXEC_TXN_CTX_FOOTPRINT );

Expand Down Expand Up @@ -351,7 +358,6 @@ _context_destroy( fd_exec_instr_test_runner_t * runner,

fd_exec_slot_ctx_delete ( fd_exec_slot_ctx_leave ( slot_ctx ) );
fd_exec_epoch_ctx_delete( fd_exec_epoch_ctx_leave( epoch_ctx ) );
fd_wksp_free_laddr( epoch_ctx );
fd_acc_mgr_delete( acc_mgr );
fd_scratch_pop();
fd_funk_txn_cancel( runner->funk, funk_txn, 1 );
Expand Down
2 changes: 1 addition & 1 deletion src/flamenco/runtime/tests/fd_exec_sol_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ static ulong hardcoded_features[] =
{ 0xd924059c5749c4c1, // secp256k1_program_enabled
0x8f688d4e3ab17a60, // enable_early_verification_of_account_modifications
0x50a615bae8ca3874, // native_programs_consume_cu
0x65b79c7f3e7441b3, // require_custodian_for_locked_stake_authorize
};

static ulong supported_features[] =
{ 0xe8f97382b03240a1, // system_transfer_zero_check
0x10a1e092dd7f1573, // dedupe_config_program_signers
0xfba69c4970d7ad9d, // vote_stake_checked_instructions
0x65b79c7f3e7441b3, // require_custodian_for_locked_stake_authorize
0x74b022574093eeec, // reduce_stake_warmup_cooldown
0xd56fc1708dc98c13, // stake_redelegate_instruction
0x6d22c4ce75df6f0b, // stake_merge_with_unmatched_credits_observed
Expand Down

0 comments on commit 7eecb38

Please sign in to comment.