Skip to content

Commit

Permalink
Fix incorrect double casting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mjain-jump committed Sep 10, 2024
1 parent e499049 commit 18bbf83
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
1 change: 1 addition & 0 deletions contrib/test/txn-fixtures/program-tests.list
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ dump/test-vectors/txn/fixtures/programs/crash-38b53303426b0cb17570b27f89be658a89
dump/test-vectors/txn/fixtures/programs/crash-10c291c5d8f688fa03b3007887e577e636d2ea79.fix
dump/test-vectors/txn/fixtures/programs/crash-42295e53abb4382e2d579d46dcb1b20e63f030ea.fix
dump/test-vectors/txn/fixtures/programs/crash-6ef8cb5316bd5ce7aba8f27053acc63cb381780e.fix
dump/test-vectors/txn/fixtures/programs/crash-f2a184eee1b1e6d5d455923d93825767843fcea6.fix
8 changes: 3 additions & 5 deletions src/flamenco/runtime/fd_executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,11 @@ fd_executor_collect_fees( fd_exec_txn_ctx_t * txn_ctx ) {
fd_runtime_calculate_fee( txn_ctx, txn_ctx->txn_descriptor, txn_ctx->_txn_raw, &execution_fee, &priority_fee );

fd_epoch_bank_t * epoch_bank = fd_exec_epoch_ctx_epoch_bank( txn_ctx->slot_ctx->epoch_ctx );
ulong total_fee = 0UL;
ulong total_fee = fd_ulong_sat_add( execution_fee, priority_fee );

// https://github.com/anza-xyz/agave/blob/2e6ca8c1f62db62c1db7f19c9962d4db43d0d550/sdk/src/fee.rs#L54
if ( FD_FEATURE_ACTIVE( txn_ctx->slot_ctx, remove_rounding_in_fee_calculation ) ) {
total_fee = fd_ulong_sat_add( execution_fee, priority_fee );
} else {
total_fee = (ulong)round((double)fd_ulong_sat_add( execution_fee, priority_fee ));
if ( !FD_FEATURE_ACTIVE( txn_ctx->slot_ctx, remove_rounding_in_fee_calculation ) ) {
total_fee = fd_rust_cast_double_to_ulong( round( (double)total_fee ) );
}

err = fd_validate_fee_payer( rec, &epoch_bank->rent, total_fee );
Expand Down
8 changes: 4 additions & 4 deletions src/flamenco/types/fd_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ FD_FN_CONST static inline ulong
fd_rust_cast_double_to_ulong( double f ) {
ulong u = fd_dblbits( f );
/* Check if the exponent is all 1s (infinity or NaN )*/
if( fd_dblbits_bexp( u ) == 0x7FFUL ) {
if( fd_dblbits_bexp( u )==0x7FFUL ) {
/* Check if the mantissa is 0 (infinity) */
if( fd_dblbits_mant( u ) == 0 ) {
if( fd_dblbits_mant( u )==0 ) {
return ULONG_MAX;
} else {
/* NaN case */
Expand All @@ -32,12 +32,12 @@ fd_rust_cast_double_to_ulong( double f ) {
}

/* If the value is negative saturate to 0 */
if( fd_dblbits_sign( u ) == 1 ) {
if( fd_dblbits_sign( u )==1 ) {
return 0;
}

/* Saturate to max unsigned long value */
if( f > (double)ULONG_MAX ) {
if( f>=(double)ULONG_MAX ) {
return ULONG_MAX;
}

Expand Down
26 changes: 14 additions & 12 deletions src/flamenco/types/test_cast.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ main( int argc,
fd_boot( &argc, &argv );
fd_flamenco_boot( &argc, &argv );

double inf = fd_double( fd_dblbits_pack( 0UL, 0x7FFUL, 0UL ) );
double ninf = fd_double( fd_dblbits_pack( 1UL, 0x7FFUL, 0UL ) );
double nan = fd_double( fd_dblbits_pack( 1UL, 0x7FFUL, 1UL ) );
double neg = -1.0;
double pos = 42.0;
double max = (double)ULONG_MAX+1;
double inf = fd_double( fd_dblbits_pack( 0UL, 0x7FFUL, 0UL ) );
double ninf = fd_double( fd_dblbits_pack( 1UL, 0x7FFUL, 0UL ) );
double nan = fd_double( fd_dblbits_pack( 1UL, 0x7FFUL, 1UL ) );
double neg = -1.0;
double pos = 42.0;
double max = (double)ULONG_MAX;
double max_1 = (double)ULONG_MAX+1;

FD_TEST( fd_rust_cast_double_to_ulong( inf ) == ULONG_MAX );
FD_TEST( fd_rust_cast_double_to_ulong( ninf ) == ULONG_MAX );
FD_TEST( fd_rust_cast_double_to_ulong( nan ) == 0 );
FD_TEST( fd_rust_cast_double_to_ulong( neg ) == 0 );
FD_TEST( fd_rust_cast_double_to_ulong( pos ) == 42 );
FD_TEST( fd_rust_cast_double_to_ulong( max ) == ULONG_MAX );
FD_TEST( fd_rust_cast_double_to_ulong( inf ) == ULONG_MAX );
FD_TEST( fd_rust_cast_double_to_ulong( ninf ) == ULONG_MAX );
FD_TEST( fd_rust_cast_double_to_ulong( nan ) == 0 );
FD_TEST( fd_rust_cast_double_to_ulong( neg ) == 0 );
FD_TEST( fd_rust_cast_double_to_ulong( pos ) == 42 );
FD_TEST( fd_rust_cast_double_to_ulong( max ) == ULONG_MAX );
FD_TEST( fd_rust_cast_double_to_ulong( max_1 ) == ULONG_MAX );

fd_flamenco_halt();
fd_halt();
Expand Down

0 comments on commit 18bbf83

Please sign in to comment.