Skip to content

Commit

Permalink
Move claim_rshare_reward() logic to reward.hpp, make stateless steemi…
Browse files Browse the repository at this point in the history
  • Loading branch information
theoreticalbts committed Jan 13, 2017
1 parent c26b83e commit cc252a5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 38 deletions.
62 changes: 25 additions & 37 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <steemit/chain/util/asset.hpp>
#include <steemit/chain/util/reward.hpp>
#include <steemit/chain/util/uint256.hpp>
#include <steemit/chain/util/reward.hpp>

#include <fc/smart_ref_impl.hpp>
#include <fc/uint128.hpp>
Expand Down Expand Up @@ -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
Expand All @@ -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 )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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<int64_t>::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
Expand Down
1 change: 0 additions & 1 deletion libraries/chain/include/steemit/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions libraries/chain/include/steemit/chain/util/reward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,34 @@

#include <steemit/chain/util/asset.hpp>

#include <steemit/protocol/asset.hpp>
#include <steemit/protocol/config.hpp>
#include <steemit/protocol/types.hpp>

#include <fc/reflect/reflect.hpp>

#include <fc/uint128.hpp>

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
Expand All @@ -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)
)
30 changes: 30 additions & 0 deletions libraries/chain/util/reward.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@

#include <steemit/chain/util/reward.hpp>
#include <steemit/chain/util/uint256.hpp>

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

uint128_t calculate_vshares( const uint128_t& rshares )
{
uint128_t s = get_content_constant_s();
Expand Down

0 comments on commit cc252a5

Please sign in to comment.