Skip to content

Commit

Permalink
Implement square root curation curve #1052
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Vandeberg committed May 10, 2017
1 parent d2ed05d commit 95f0f12
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
18 changes: 18 additions & 0 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3846,6 +3846,24 @@ void database::apply_hardfork( uint32_t hardfork )
rfo.author_reward_curve = curve_id::linear;
rfo.curation_reward_curve = curve_id::square_root;
});

const auto& cidx = get_index< comment_index, by_cashout_time >();
const auto& vidx = get_index< comment_vote_index, by_comment_voter >();
for( auto c_itr = cidx.begin(); c_itr != cidx.end() && c_itr->cashout_time != fc::time_point_sec::maximum(); ++c_itr )
{
modify( *c_itr, [&]( comment_object& c )
{
c.total_vote_weight = c.total_sqrt_vote_weight;
});

for( auto v_itr = vidx.lower_bound( c_itr->id ); v_itr != vidx.end() && v_itr->comment == c_itr->id; ++v_itr )
{
modify( *v_itr, [&]( comment_vote_object& v )
{
v.weight = v.sqrt_weight;
});
}
}
}
break;
default:
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/hardfork.d/0_19.hf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define STEEMIT_HARDFORK_0_19__987 (STEEMIT_HARDFORK_0_19)
#define STEEMIT_HARDFORK_0_19__997 (STEEMIT_HARDFORK_0_19)
#define STEEMIT_HARDFORK_0_19__1051 (STEEMIT_HARDFORK_0_19)
#define STEEMIT_HARDFORK_0_19__1052 (STEEMIT_HARDFORK_0_19)
#define STEEMIT_HARDFORK_0_19__1053 (STEEMIT_HARDFORK_0_19)

#define STEEMIT_HARDFORK_0_19_VERSION hardfork_version( 0, 19 )
Expand Down
6 changes: 4 additions & 2 deletions libraries/chain/include/steemit/chain/comment_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace steemit { namespace chain {
time_point_sec cashout_time; /// 24 hours from the weighted average of vote time
time_point_sec max_cashout_time;
uint64_t total_vote_weight = 0; /// the total weight of voting rewards, used to calculate pro-rata share of curation payouts
uint64_t total_sqrt_vote_weight = 0; /// Temporary total weight of votes for post hf19 curation reward curve

uint16_t reward_weight = 0;

Expand Down Expand Up @@ -120,6 +121,7 @@ namespace steemit { namespace chain {
account_id_type voter;
comment_id_type comment;
uint64_t weight = 0; ///< defines the score this vote receives, used by vote payout calc. 0 if a negative vote or changed votes.
uint64_t sqrt_weight = 0; ///< Temporary vote weight for post hf19 curation curve
int64_t rshares = 0; ///< The number of rshares this vote is responsible for
int16_t vote_percent = 0; ///< The percent weight of the vote
time_point_sec last_update; ///< The time of the last update of the vote
Expand Down Expand Up @@ -250,13 +252,13 @@ FC_REFLECT( steemit::chain::comment_object,
(depth)(children)
(net_rshares)(abs_rshares)(vote_rshares)
(children_abs_rshares)(cashout_time)(max_cashout_time)
(total_vote_weight)(reward_weight)(total_payout_value)(curator_payout_value)(beneficiary_payout_value)(author_rewards)(net_votes)(root_comment)
(total_vote_weight)(total_sqrt_vote_weight)(reward_weight)(total_payout_value)(curator_payout_value)(beneficiary_payout_value)(author_rewards)(net_votes)(root_comment)
(max_accepted_payout)(percent_steem_dollars)(allow_replies)(allow_votes)(allow_curation_rewards)
(beneficiaries)
)
CHAINBASE_SET_INDEX_TYPE( steemit::chain::comment_object, steemit::chain::comment_index )

FC_REFLECT( steemit::chain::comment_vote_object,
(id)(voter)(comment)(weight)(rshares)(vote_percent)(last_update)(num_changes)
(id)(voter)(comment)(weight)(sqrt_weight)(rshares)(vote_percent)(last_update)(num_changes)
)
CHAINBASE_SET_INDEX_TYPE( steemit::chain::comment_vote_object, steemit::chain::comment_vote_index )
17 changes: 17 additions & 0 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,7 @@ void vote_evaluator::do_apply( const vote_operation& o )
old_rshares = util::evaluate_reward_curve( old_rshares );

uint64_t max_vote_weight = 0;
uint64_t sqrt_max_vote_weight = 0;

/** this verifies uniqueness of voter
*
Expand Down Expand Up @@ -1345,6 +1346,13 @@ void vote_evaluator::do_apply( const vote_operation& o )
uint64_t old_weight = util::evaluate_reward_curve( old_vote_rshares.value, reward_fund.curation_reward_curve, reward_fund.content_constant ).to_uint64();
uint64_t new_weight = util::evaluate_reward_curve( comment.vote_rshares.value, reward_fund.curation_reward_curve, reward_fund.content_constant ).to_uint64();
cv.weight = new_weight - old_weight;
if( !_db.has_hardfork( STEEMIT_HARDFORK_0_19__1052 ) )
{
old_weight = util::evaluate_reward_curve( old_vote_rshares.value, curve_id::square_root ).to_uint64();
new_weight = util::evaluate_reward_curve( comment.vote_rshares.value, curve_id::square_root ).to_uint64();
cv.sqrt_weight = new_weight - old_weight;
sqrt_max_vote_weight = cv.sqrt_weight;
}
}
else if ( _db.has_hardfork( STEEMIT_HARDFORK_0_1 ) )
{
Expand All @@ -1371,6 +1379,14 @@ void vote_evaluator::do_apply( const vote_operation& o )
w *= delta_t;
w /= STEEMIT_REVERSE_AUCTION_WINDOW_SECONDS;
cv.weight = w.to_uint64();

if( _db.has_hardfork( STEEMIT_HARDFORK_0_17 ) )
{
uint128_t w(sqrt_max_vote_weight);
w *= delta_t;
w /= STEEMIT_REVERSE_AUCTION_WINDOW_SECONDS;
cv.sqrt_weight = w.to_uint64();
}
}
}
else
Expand All @@ -1384,6 +1400,7 @@ void vote_evaluator::do_apply( const vote_operation& o )
_db.modify( comment, [&]( comment_object& c )
{
c.total_vote_weight += max_vote_weight;
c.total_sqrt_vote_weight = sqrt_max_vote_weight;
});
}
if( !_db.has_hardfork( STEEMIT_HARDFORK_0_17__774) )
Expand Down
33 changes: 32 additions & 1 deletion libraries/chain/util/reward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@

namespace steemit { namespace chain { namespace util {

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;
}

uint64_t get_rshare_reward( const comment_reward_context& ctx )
{
try
Expand Down Expand Up @@ -53,7 +84,7 @@ uint128_t evaluate_reward_curve( const uint128_t& rshares, const curve_id& curve
}
break;
case linear:
result = rshares;
result = approx_sqrt( rshares );
break;
case square_root:
break;
Expand Down

0 comments on commit 95f0f12

Please sign in to comment.