From cc252a53a91336f5cbe818995b869a03abeb4fd1 Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Fri, 13 Jan 2017 13:17:19 -0500 Subject: [PATCH] Move claim_rshare_reward() logic to reward.hpp, make stateless #782 --- libraries/chain/database.cpp | 62 ++++++++----------- .../chain/include/steemit/chain/database.hpp | 1 - .../include/steemit/chain/util/reward.hpp | 33 ++++++++++ libraries/chain/util/reward.cpp | 30 +++++++++ 4 files changed, 88 insertions(+), 38 deletions(-) diff --git a/libraries/chain/database.cpp b/libraries/chain/database.cpp index 6929b58abb..5eee3405bd 100644 --- a/libraries/chain/database.cpp +++ b/libraries/chain/database.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -2002,6 +2003,21 @@ share_type database::pay_curators( const comment_object& c, share_type max_rewar } FC_CAPTURE_AND_RETHROW() } +void fill_comment_reward_context_global_state( util::comment_reward_context& ctx, const database& db ) +{ + const dynamic_global_property_object& dgpo = db.get_dynamic_global_properties(); + ctx.total_reward_shares2 = dgpo.total_reward_shares2; + ctx.total_reward_fund_steem = dgpo.total_reward_fund_steem; + ctx.current_steem_price = db.get_feed_history().current_median_history; +} + +void fill_comment_reward_context_local_state( util::comment_reward_context& ctx, const comment_object& comment ) +{ + ctx.rshares = comment.net_rshares; + ctx.reward_weight = comment.reward_weight; + ctx.max_sbd = comment.max_accepted_payout; +} + void database::cashout_comment_helper( const comment_object& comment ) { try @@ -2010,7 +2026,11 @@ void database::cashout_comment_helper( const comment_object& comment ) if( comment.net_rshares > 0 ) { - uint128_t reward_tokens = uint128_t( claim_rshare_reward( comment.net_rshares, comment.reward_weight, to_steem( comment.max_accepted_payout ) ).value ); + util::comment_reward_context ctx; + fill_comment_reward_context_local_state( ctx, comment ); + fill_comment_reward_context_global_state( ctx, *this ); + const share_type reward = util::get_rshare_reward( ctx ); + uint128_t reward_tokens = uint128_t( reward.value ); asset total_payout; if( reward_tokens > 0 ) @@ -2065,6 +2085,10 @@ void database::cashout_comment_helper( const comment_object& comment ) c.total_payouts += total_payout; }); + modify( get_dynamic_global_properties(), [&]( dynamic_global_property_object& p ) + { + p.total_reward_fund_steem.amount -= reward; + }); } fc::uint128_t old_rshares2 = util::calculate_vshares( comment.net_rshares.value ); @@ -2430,42 +2454,6 @@ asset database::to_steem( const asset& sbd )const return util::to_steem( get_feed_history().current_median_history, sbd ); } -/** - * This method reduces the rshare^2 supply and returns the number of tokens are - * redeemed. - */ -share_type database::claim_rshare_reward( share_type rshares, uint16_t reward_weight, asset max_steem ) -{ - try - { - FC_ASSERT( rshares > 0 ); - - const auto& props = get_dynamic_global_properties(); - - u256 rs(rshares.value); - u256 rf(props.total_reward_fund_steem.amount.value); - u256 total_rshares2 = util::to256( props.total_reward_shares2 ); - - u256 rs2 = util::to256( util::calculate_vshares( rshares.value ) ); - rs2 = ( rs2 * reward_weight ) / STEEMIT_100_PERCENT; - - u256 payout_u256 = ( rf * rs2 ) / total_rshares2; - FC_ASSERT( payout_u256 <= u256( uint64_t( std::numeric_limits::max() ) ) ); - uint64_t payout = static_cast< uint64_t >( payout_u256 ); - - if( util::is_comment_payout_dust( get_feed_history().current_median_history, payout ) ) - payout = 0; - - payout = std::min( payout, uint64_t( max_steem.amount.value ) ); - - modify( props, [&]( dynamic_global_property_object& p ){ - p.total_reward_fund_steem.amount -= payout; - }); - - return payout; - } FC_CAPTURE_AND_RETHROW( (rshares)(max_steem) ) -} - void database::account_recovery_processing() { // Clear expired recovery requests diff --git a/libraries/chain/include/steemit/chain/database.hpp b/libraries/chain/include/steemit/chain/database.hpp index 4af8a78b75..e63bdf9851 100644 --- a/libraries/chain/include/steemit/chain/database.hpp +++ b/libraries/chain/include/steemit/chain/database.hpp @@ -326,7 +326,6 @@ namespace steemit { namespace chain { void expire_escrow_ratification(); void process_decline_voting_rights(); void update_median_feed(); - share_type claim_rshare_reward( share_type rshares, uint16_t reward_weight, asset max_steem ); asset get_liquidity_reward()const; asset get_content_reward()const; diff --git a/libraries/chain/include/steemit/chain/util/reward.hpp b/libraries/chain/include/steemit/chain/util/reward.hpp index bf5f8a2843..04b2370972 100644 --- a/libraries/chain/include/steemit/chain/util/reward.hpp +++ b/libraries/chain/include/steemit/chain/util/reward.hpp @@ -2,10 +2,34 @@ #include +#include #include +#include + +#include + +#include namespace steemit { namespace chain { namespace util { +using steemit::protocol::asset; +using steemit::protocol::price; +using steemit::protocol::share_type; + +using fc::uint128_t; + +struct comment_reward_context +{ + share_type rshares; + uint16_t reward_weight = 0; + asset max_sbd; + uint128_t total_reward_shares2; + asset total_reward_fund_steem; + price current_steem_price; +}; + +uint64_t get_rshare_reward( const comment_reward_context& ctx ); + inline uint128_t get_content_constant_s() { return uint128_t( uint64_t(2000000000000ll) ); // looking good for posters @@ -19,3 +43,12 @@ inline bool is_comment_payout_dust( const price& p, uint64_t steem_payout ) } } } } + +FC_REFLECT( steemit::chain::util::comment_reward_context, + (rshares) + (reward_weight) + (max_sbd) + (total_reward_shares2) + (total_reward_fund_steem) + (current_steem_price) + ) diff --git a/libraries/chain/util/reward.cpp b/libraries/chain/util/reward.cpp index 2fb3006528..1812409293 100644 --- a/libraries/chain/util/reward.cpp +++ b/libraries/chain/util/reward.cpp @@ -1,8 +1,38 @@ #include +#include namespace steemit { namespace chain { namespace util { +uint64_t get_rshare_reward( const comment_reward_context& ctx ) +{ + try + { + 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 ); + + u256 rs2 = to256( calculate_vshares( 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::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) ) +} + uint128_t calculate_vshares( const uint128_t& rshares ) { uint128_t s = get_content_constant_s();