Skip to content

Commit

Permalink
Refactor rewards calculations to use curve IDs in reward fund objects #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Vandeberg committed May 18, 2017
1 parent ca1355d commit 3cff2dc
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 56 deletions.
5 changes: 4 additions & 1 deletion libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,10 @@ void database_api::set_pending_payout( discussion& d )const
{
uint128_t vshares;
if( my->_db.has_hardfork( STEEMIT_HARDFORK_0_17__774 ) )
vshares = steemit::chain::util::calculate_claims( d.net_rshares.value > 0 ? d.net_rshares.value : 0 , my->_db.get_reward_fund( my->_db.get_comment( d.author, d.permlink ) ) );
{
const auto& rf = my->_db.get_reward_fund( my->_db.get_comment( d.author, d.permlink ) );
vshares = d.net_rshares.value > 0 ? steemit::chain::util::calculate_claims( d.net_rshares.value, rf.author_reward_curve, rf.content_constant ) : 0;
}
else
vshares = steemit::chain::util::calculate_claims( d.net_rshares.value > 0 ? d.net_rshares.value : 0 );

Expand Down
14 changes: 11 additions & 3 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1482,8 +1482,14 @@ share_type database::cashout_comment_helper( util::comment_reward_context& ctx,
{
fill_comment_reward_context_local_state( ctx, comment );

const share_type reward = has_hardfork( STEEMIT_HARDFORK_0_17__774 ) ?
util::get_rshare_reward( ctx, get_reward_fund( comment ) ) : util::get_rshare_reward( ctx );
if( has_hardfork( STEEMIT_HARDFORK_0_17__774 ) )
{
const auto rf = get_reward_fund( comment );
ctx.reward_curve = rf.author_reward_curve;
ctx.content_constant = rf.content_constant;
}

const share_type reward = util::get_rshare_reward( ctx );
uint128_t reward_tokens = uint128_t( reward.value );

if( reward_tokens > 0 )
Expand Down Expand Up @@ -1639,7 +1645,7 @@ void database::process_comment_cashout()
if( current->net_rshares > 0 )
{
const auto& rf = get_reward_fund( *current );
funds[ rf.id._id ].recent_claims += util::calculate_claims( current->net_rshares.value, rf );
funds[ rf.id._id ].recent_claims += util::calculate_claims( current->net_rshares.value, rf.author_reward_curve, rf.content_constant );
}

++current;
Expand Down Expand Up @@ -3756,6 +3762,8 @@ void database::apply_hardfork( uint32_t hardfork )
#ifndef IS_TEST_NET
rfo.recent_claims = STEEMIT_HF_17_RECENT_CLAIMS;
#endif
rfo.author_reward_curve = curve_id::quadratic;
rfo.curation_reward_curve = curve_id::quadratic_curation;
});

// As a shortcut in payout processing, we use the id as an array index.
Expand Down
14 changes: 14 additions & 0 deletions libraries/chain/include/steemit/chain/steem_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ namespace steemit { namespace chain {
time_point_sec effective_date;
};

enum curve_id
{
quadratic,
quadratic_curation,
linear,
square_root
};

class reward_fund_object : public object< reward_fund_object_type, reward_fund_object >
{
public:
Expand All @@ -268,6 +276,8 @@ namespace steemit { namespace chain {
uint128_t content_constant = 0;
uint16_t percent_curation_rewards = 0;
uint16_t percent_content_rewards = 0;
curve_id author_reward_curve;
curve_id curation_reward_curve;
};

struct by_price;
Expand Down Expand Up @@ -479,6 +489,8 @@ namespace steemit { namespace chain {
#include <steemit/chain/comment_object.hpp>
#include <steemit/chain/account_object.hpp>

FC_REFLECT_ENUM( steemit::chain::curve_id,
(quadratic)(quadratic_curation)(linear)(square_root))

FC_REFLECT( steemit::chain::limit_order_object,
(id)(created)(expiration)(seller)(orderid)(for_sale)(sell_price) )
Expand Down Expand Up @@ -524,5 +536,7 @@ FC_REFLECT( steemit::chain::reward_fund_object,
(content_constant)
(percent_curation_rewards)
(percent_content_rewards)
(author_reward_curve)
(curation_reward_curve)
)
CHAINBASE_SET_INDEX_TYPE( steemit::chain::reward_fund_object, steemit::chain::reward_fund_index )
12 changes: 6 additions & 6 deletions libraries/chain/include/steemit/chain/util/reward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,20 @@ struct comment_reward_context
uint128_t total_reward_shares2;
asset total_reward_fund_steem;
price current_steem_price;
curve_id reward_curve = quadratic;
uint128_t content_constant = STEEMIT_CONTENT_CONSTANT_HF0;
};

uint64_t get_rshare_reward( const comment_reward_context& ctx );

uint64_t get_rshare_reward( const comment_reward_context& ctx, const reward_fund_object& rf );

uint64_t get_vote_weight( uint64_t vote_rshares, const reward_fund_object& rf );
uint64_t get_vote_weight( uint64_t vote_rshares, const curve_id& curve, const uint128_t& content_constant );

inline uint128_t get_content_constant_s()
{
return STEEMIT_CONTENT_CONSTANT_HF0; // looking good for posters
}

uint128_t calculate_claims( const uint128_t& rshares );

uint128_t calculate_claims( const uint128_t& rshares, const reward_fund_object& rf );
uint128_t calculate_claims( const uint128_t& rshares, const curve_id& curve = quadratic, const uint128_t& content_constant = STEEMIT_CONTENT_CONSTANT_HF0 );

inline bool is_comment_payout_dust( const price& p, uint64_t steem_payout )
{
Expand All @@ -58,4 +56,6 @@ FC_REFLECT( steemit::chain::util::comment_reward_context,
(total_reward_shares2)
(total_reward_fund_steem)
(current_steem_price)
(reward_curve)
(content_constant)
)
4 changes: 2 additions & 2 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,8 +1345,8 @@ void vote_evaluator::do_apply( const vote_operation& o )
if( _db.has_hardfork( STEEMIT_HARDFORK_0_17__774 ) )
{
const auto& reward_fund = _db.get_reward_fund( comment );
uint64_t old_weight = util::get_vote_weight( old_vote_rshares.value, reward_fund );
uint64_t new_weight = util::get_vote_weight( comment.vote_rshares.value, reward_fund );
uint64_t old_weight = util::get_vote_weight( old_vote_rshares.value, reward_fund.curation_reward_curve, reward_fund.content_constant );
uint64_t new_weight = util::get_vote_weight( comment.vote_rshares.value, reward_fund.curation_reward_curve, reward_fund.content_constant );
cv.weight = new_weight - old_weight;
}
else if ( _db.has_hardfork( STEEMIT_HARDFORK_0_1 ) )
Expand Down
76 changes: 34 additions & 42 deletions libraries/chain/util/reward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,12 @@ uint64_t get_rshare_reward( const comment_reward_context& ctx )
FC_ASSERT( ctx.rshares > 0 );
FC_ASSERT( ctx.total_reward_shares2 > 0 );

u256 rs(ctx.rshares.value);
u256 rf(ctx.total_reward_fund_steem.amount.value);
u256 total_rshares2 = to256( ctx.total_reward_shares2 );

//idump( (ctx) );

u256 rs2 = to256( calculate_claims( ctx.rshares.value ) );
rs2 = ( rs2 * ctx.reward_weight ) / STEEMIT_100_PERCENT;

u256 payout_u256 = ( rf * rs2 ) / total_rshares2;
FC_ASSERT( payout_u256 <= u256( uint64_t( std::numeric_limits<int64_t>::max() ) ) );
uint64_t payout = static_cast< uint64_t >( payout_u256 );

if( is_comment_payout_dust( ctx.current_steem_price, payout ) )
payout = 0;

asset max_steem = to_steem( ctx.current_steem_price, ctx.max_sbd );

payout = std::min( payout, uint64_t( max_steem.amount.value ) );

return payout;
} FC_CAPTURE_AND_RETHROW( (ctx) )
}

uint64_t get_rshare_reward( const comment_reward_context& ctx, const reward_fund_object& rf_object )
{
try
{
FC_ASSERT( ctx.rshares > 0 );
FC_ASSERT( ctx.total_reward_shares2 > 0 );

u256 rf(ctx.total_reward_fund_steem.amount.value);
u256 total_claims = to256( ctx.total_reward_shares2 );

//idump( (ctx) );

u256 claim = to256( calculate_claims( ctx.rshares.value, rf_object ) );
u256 claim = to256( calculate_claims( ctx.rshares.value, ctx.reward_curve, ctx.content_constant ) );
claim = ( claim * ctx.reward_weight ) / STEEMIT_100_PERCENT;

u256 payout_u256 = ( rf * claim ) / total_claims;
Expand Down Expand Up @@ -81,28 +50,51 @@ uint64_t get_vote_weight( uint64_t vote_rshares, const reward_fund_object& rf )
return result;
}

uint64_t get_vote_weight( uint64_t vote_rshares, const curve_id& curve, const uint128_t& content_constant )
{
uint64_t result = 0;

switch( curve )
{
case quadratic_curation:
{
uint128_t two_alpha = content_constant * 2;
result = ( uint128_t( vote_rshares, 0 ) / ( two_alpha + vote_rshares ) ).to_uint64();
}
break;
case square_root:
//result = rshares;
break;
}

return result;
}

uint128_t calculate_claims( const uint128_t& rshares )
{
uint128_t s = get_content_constant_s();
uint128_t rshares_plus_s = rshares + s;
return rshares_plus_s * rshares_plus_s - s * s;
}

uint128_t calculate_claims( const uint128_t& rshares, const reward_fund_object& rf )
uint128_t calculate_claims( const uint128_t& rshares, const curve_id& curve, const uint128_t& content_constant )
{
uint128_t result = 0;
if( rf.name == STEEMIT_POST_REWARD_FUND_NAME || rf.name == STEEMIT_COMMENT_REWARD_FUND_NAME )
{
uint128_t s = rf.content_constant;
uint128_t rshares_plus_s = rshares + s;
result = rshares_plus_s * rshares_plus_s - s * s;
}
else

switch( curve )
{
wlog( "Unknown reward fund type ${rf}", ("rf",rf.name) );
case quadratic:
{
uint128_t rshares_plus_s = rshares + content_constant;
result = rshares_plus_s * rshares_plus_s - content_constant * content_constant;
}
break;
case linear:
result = rshares;
break;
}

return result;
}

} } }
} } } // steemit::chain::util
8 changes: 6 additions & 2 deletions tests/tests/operation_time_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ BOOST_AUTO_TEST_CASE( recent_claims_decay )
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

auto alice_vshares = util::calculate_claims( db.get_comment( "alice", string( "test" ) ).net_rshares.value, db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME ) );
auto alice_vshares = util::calculate_claims( db.get_comment( "alice", string( "test" ) ).net_rshares.value,
db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME ).author_reward_curve,
db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME ).content_constant );

generate_blocks( 5 );

Expand All @@ -341,7 +343,9 @@ BOOST_AUTO_TEST_CASE( recent_claims_decay )
}

auto bob_cashout_time = db.get_comment( "bob", string( "test" ) ).cashout_time;
auto bob_vshares = util::calculate_claims( db.get_comment( "bob", string( "test" ) ).net_rshares.value, db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME ) );
auto bob_vshares = util::calculate_claims( db.get_comment( "bob", string( "test" ) ).net_rshares.value,
db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME ).author_reward_curve,
db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME ).content_constant );

generate_block();

Expand Down

0 comments on commit 3cff2dc

Please sign in to comment.