Skip to content

Commit

Permalink
Merge pull request #3495 from steemit/20190901-fix-curation-weight
Browse files Browse the repository at this point in the history
20190901 fix curation weight
  • Loading branch information
Michael Vandeberg authored Sep 2, 2019
2 parents 51c7bf9 + 587574f commit a0dabd3
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ RUN \
fi && \
cd tests && \
ctest -j$(nproc) --output-on-failure && \
./chain_test -t basic_tests/curation_weight_test && \
cd .. && \
./libraries/mira/test/mira_test && \
./programs/util/test_fixed_string && \
Expand Down
8 changes: 4 additions & 4 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ share_type database::pay_curators( const comment_object& c, share_type& max_rewa
}

for( auto& item : proxy_set )
{
{ try {
uint128_t weight( item->weight );
auto claim = ( ( max_rewards.value * weight ) / total_weight ).to_uint64();
if( claim > 0 ) // min_amt is non-zero satoshis
Expand All @@ -1852,12 +1852,12 @@ share_type database::pay_curators( const comment_object& c, share_type& max_rewa
#endif
post_push_virtual_operation( vop );
}
}
} FC_CAPTURE_AND_RETHROW( (*item) ) }
}
max_rewards -= unclaimed_rewards;

return unclaimed_rewards;
} FC_CAPTURE_AND_RETHROW()
} FC_CAPTURE_AND_RETHROW( (max_rewards) )
}

void fill_comment_reward_context_local_state( util::comment_reward_context& ctx, const comment_object& comment )
Expand Down Expand Up @@ -2029,7 +2029,7 @@ share_type database::cashout_comment_helper( util::comment_reward_context& ctx,
}

return claimed_reward;
} FC_CAPTURE_AND_RETHROW( (comment) )
} FC_CAPTURE_AND_RETHROW( (comment)(ctx) )
}

void database::process_comment_cashout()
Expand Down
24 changes: 16 additions & 8 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2077,17 +2077,25 @@ void hf20_vote_evaluator( const vote_operation& o, database& _db )
auto curve = reward_fund.curation_reward_curve;
uint64_t old_weight = util::evaluate_reward_curve( old_vote_rshares.value, curve, reward_fund.content_constant ).to_uint64();
uint64_t new_weight = util::evaluate_reward_curve( comment.vote_rshares.value, curve, reward_fund.content_constant ).to_uint64();
cv.weight = new_weight - old_weight;

max_vote_weight = cv.weight;
if( old_weight >= new_weight ) // old_weight > new_weight should never happen
{
cv.weight = 0;
}
else
{
cv.weight = new_weight - old_weight;

max_vote_weight = cv.weight;

/// discount weight by time
uint128_t w(max_vote_weight);
uint64_t delta_t = std::min( uint64_t((cv.last_update - comment.created).to_seconds()), uint64_t( dgpo.reverse_auction_seconds ) );
/// discount weight by time
uint128_t w(max_vote_weight);
uint64_t delta_t = std::min( uint64_t((cv.last_update - comment.created).to_seconds()), uint64_t( dgpo.reverse_auction_seconds ) );

w *= delta_t;
w /= dgpo.reverse_auction_seconds;
cv.weight = w.to_uint64();
w *= delta_t;
w /= dgpo.reverse_auction_seconds;
cv.weight = w.to_uint64();
}
}
else
{
Expand Down
73 changes: 73 additions & 0 deletions tests/tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <steem/protocol/steem_operations.hpp>
#include <steem/chain/account_object.hpp>

#include <steem/chain/util/reward.hpp>

#include <fc/crypto/digest.hpp>
#include <fc/crypto/hex.hpp>
#include "../db_fixture/database_fixture.hpp"
Expand Down Expand Up @@ -357,4 +359,75 @@ BOOST_AUTO_TEST_CASE( adjust_balance_test )
BOOST_REQUIRE( db->get_balance( "alice", SBD_SYMBOL ) == asset( 0, SBD_SYMBOL ) );
}

uint8_t find_msb( const uint128_t& u )
{
uint64_t x;
uint8_t places;
x = (u.lo ? u.lo : 1);
places = (u.hi ? 64 : 0);
x = (u.hi ? u.hi : x);
return uint8_t( boost::multiprecision::detail::find_msb(x) + places );
}

uint64_t approx_sqrt( const uint128_t& x )
{
if( (x.lo == 0) && (x.hi == 0) )
return 0;

uint8_t msb_x = find_msb(x);
uint8_t msb_z = msb_x >> 1;

uint128_t msb_x_bit = uint128_t(1) << msb_x;
uint64_t msb_z_bit = uint64_t (1) << msb_z;

uint128_t mantissa_mask = msb_x_bit - 1;
uint128_t mantissa_x = x & mantissa_mask;
uint64_t mantissa_z_hi = (msb_x & 1) ? msb_z_bit : 0;
uint64_t mantissa_z_lo = (mantissa_x >> (msb_x - msb_z)).lo;
uint64_t mantissa_z = (mantissa_z_hi | mantissa_z_lo) >> 1;
uint64_t result = msb_z_bit | mantissa_z;

return result;
}

BOOST_AUTO_TEST_CASE( curation_weight_test )
{
fc::uint128_t rshares = 856158;
fc::uint128_t s = uint128_t( 0, 2000000000000ull );
fc::uint128_t sqrt = approx_sqrt( rshares + 2 * s );
uint64_t result = ( rshares / sqrt ).to_uint64();

BOOST_REQUIRE( sqrt.to_uint64() == 2002250 );
BOOST_REQUIRE( result == 0 );

rshares = 0;
sqrt = approx_sqrt( rshares + 2 * s );
result = ( rshares / sqrt ).to_uint64();

BOOST_REQUIRE( sqrt.to_uint64() == 2002250 );
BOOST_REQUIRE( result == 0 );

result = ( uint128_t( 0 ) - uint128_t( 0 ) ).to_uint64();

BOOST_REQUIRE( result == 0 );
rshares = uint128_t( 0, 3351842535167ull );

for( int64_t i = 856158; i >= 0; --i )
{
uint64_t old_weight = util::evaluate_reward_curve( rshares - i, protocol::convergent_square_root, s ).to_uint64();
uint64_t new_weight = util::evaluate_reward_curve( rshares, protocol::convergent_square_root, s ).to_uint64();

BOOST_REQUIRE( old_weight <= new_weight );

uint128_t w( new_weight - old_weight );

w *= 300;
w /= 300;
BOOST_REQUIRE( w.to_uint64() == new_weight - old_weight );
}

//idump( (delta)(old_weight)(new_weight) );

}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit a0dabd3

Please sign in to comment.