Skip to content

Commit

Permalink
Move stateless vshares curve functions to reward.hpp steemit#782
Browse files Browse the repository at this point in the history
  • Loading branch information
theoreticalbts committed Jan 13, 2017
1 parent bf258c2 commit c26b83e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 33 deletions.
4 changes: 3 additions & 1 deletion libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <steemit/protocol/get_config.hpp>

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

#include <fc/bloom_filter.hpp>
#include <fc/smart_ref_impl.hpp>
#include <fc/crypto/hex.hpp>
Expand Down Expand Up @@ -1110,7 +1112,7 @@ void database_api::set_pending_payout( discussion& d )const
u256 total_r2 = to256( props.total_reward_shares2 );

if( props.total_reward_shares2 > 0 ){
auto vshares = my->_db.calculate_vshares( d.net_rshares.value > 0 ? d.net_rshares.value : 0 );
auto vshares = steemit::chain::util::calculate_vshares( d.net_rshares.value > 0 ? d.net_rshares.value : 0 );

//int64_t abs_net_rshares = llabs(d.net_rshares.value);

Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ add_library( steemit_chain
shared_authority.cpp
block_log.cpp

util/reward.cpp

${HEADERS}
${hardfork_hpp_file}
"${CMAKE_CURRENT_BINARY_DIR}/include/steemit/chain/hardfork.hpp"
Expand Down
23 changes: 6 additions & 17 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,7 @@ share_type database::pay_discussions( const comment_object& c, share_type max_re
if( c.children_rshares2 > 0 )
{
const auto& comment_by_parent = get_index< comment_index >().indices().get< by_parent >();
fc::uint128_t total_rshares2( c.children_rshares2 - calculate_vshares( c.net_rshares.value ) );
fc::uint128_t total_rshares2( c.children_rshares2 - util::calculate_vshares( c.net_rshares.value ) );
child_queue.push_back( c.id );

// Pre-order traversal of the tree of child comments
Expand All @@ -1924,7 +1924,7 @@ share_type database::pay_discussions( const comment_object& c, share_type max_re

if( cur.net_rshares > 0 )
{
auto claim = static_cast< uint64_t >( ( util::to256( calculate_vshares( cur.net_rshares.value ) ) * max_rewards.value ) / util::to256( total_rshares2 ) );
auto claim = static_cast< uint64_t >( ( util::to256( util::calculate_vshares( cur.net_rshares.value ) ) * max_rewards.value ) / util::to256( total_rshares2 ) );
unclaimed_rewards -= claim;

if( claim > 0 )
Expand Down Expand Up @@ -2067,7 +2067,7 @@ void database::cashout_comment_helper( const comment_object& comment )

}

fc::uint128_t old_rshares2 = calculate_vshares( comment.net_rshares.value );
fc::uint128_t old_rshares2 = util::calculate_vshares( comment.net_rshares.value );
adjust_rshares2( comment, old_rshares2, 0 );
}

Expand Down Expand Up @@ -2376,17 +2376,6 @@ uint16_t database::get_curation_rewards_percent() const
return STEEMIT_1_PERCENT * 50;
}

uint128_t database::get_content_constant_s() const
{
return uint128_t( uint64_t(2000000000000ll) ); // looking good for posters
}

uint128_t database::calculate_vshares( uint128_t rshares ) const
{
auto s = get_content_constant_s();
return ( rshares + s ) * ( rshares + s ) - s * s;
}

/**
* Iterates over all conversion requests with a conversion date before
* the head block time and then converts them to/from steem/sbd at the
Expand Down Expand Up @@ -2457,7 +2446,7 @@ share_type database::claim_rshare_reward( share_type rshares, uint16_t reward_we
u256 rf(props.total_reward_fund_steem.amount.value);
u256 total_rshares2 = util::to256( props.total_reward_shares2 );

u256 rs2 = util::to256( calculate_vshares( rshares.value ) );
u256 rs2 = util::to256( util::calculate_vshares( rshares.value ) );
rs2 = ( rs2 * reward_weight ) / STEEMIT_100_PERCENT;

u256 payout_u256 = ( rf * rs2 ) / total_rshares2;
Expand Down Expand Up @@ -4287,7 +4276,7 @@ void database::validate_invariants()const
{
if( itr->net_rshares.value > 0 )
{
auto delta = calculate_vshares( itr->net_rshares.value );
auto delta = util::calculate_vshares( itr->net_rshares.value );
total_rshares2 += delta;
}
if( itr->parent_author == STEEMIT_ROOT_POST_PARENT )
Expand Down Expand Up @@ -4355,7 +4344,7 @@ void database::perform_vesting_share_split( uint32_t magnitude )
for( const auto& c : comments )
{
if( c.net_rshares.value > 0 )
adjust_rshares2( c, 0, calculate_vshares( c.net_rshares.value ) );
adjust_rshares2( c, 0, util::calculate_vshares( c.net_rshares.value ) );
}

// Update category rshares
Expand Down
3 changes: 0 additions & 3 deletions libraries/chain/include/steemit/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,6 @@ namespace steemit { namespace chain {
uint16_t get_discussion_rewards_percent() const;
uint16_t get_curation_rewards_percent() const;

uint128_t get_content_constant_s() const;
uint128_t calculate_vshares( uint128_t rshares ) const;

void pay_liquidity_reward();

/**
Expand Down
7 changes: 7 additions & 0 deletions libraries/chain/include/steemit/chain/util/reward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

namespace steemit { namespace chain { namespace util {

inline uint128_t get_content_constant_s()
{
return uint128_t( uint64_t(2000000000000ll) ); // looking good for posters
}

uint128_t calculate_vshares( const uint128_t& rshares );

inline bool is_comment_payout_dust( const price& p, uint64_t steem_payout )
{
return to_sbd( p, asset( steem_payout, STEEM_SYMBOL ) ) < STEEMIT_MIN_PAYOUT_SBD;
Expand Down
19 changes: 11 additions & 8 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <steemit/chain/witness_objects.hpp>
#include <steemit/chain/block_summary_object.hpp>

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

#ifndef IS_LOW_MEM
#include <diff_match_patch.h>
#include <boost/locale/encoding_utf.hpp>
Expand Down Expand Up @@ -1177,8 +1179,8 @@ void vote_evaluator::do_apply( const vote_operation& o )
fc::uint128_t new_rshares = std::max( comment.net_rshares.value, int64_t(0));

/// calculate rshares2 value
new_rshares = _db.calculate_vshares( new_rshares );
old_rshares = _db.calculate_vshares( old_rshares );
new_rshares = util::calculate_vshares( new_rshares );
old_rshares = util::calculate_vshares( old_rshares );

const auto& cat = _db.get_category( comment.category );
_db.modify( cat, [&]( category_object& c ){
Expand Down Expand Up @@ -1230,16 +1232,17 @@ void vote_evaluator::do_apply( const vote_operation& o )
total2 *= total2;
cv.weight = static_cast<uint64_t>( rshares3 / total2 );
} else {// cv.weight = W(R_1) - W(R_0)
const uint128_t two_s = 2 * util::get_content_constant_s();
if( _db.has_hardfork( STEEMIT_HARDFORK_0_1 ) )
{
uint64_t old_weight = ( ( std::numeric_limits< uint64_t >::max() * fc::uint128_t( old_vote_rshares.value ) ) / ( 2 * _db.get_content_constant_s() + old_vote_rshares.value ) ).to_uint64();
uint64_t new_weight = ( ( std::numeric_limits< uint64_t >::max() * fc::uint128_t( comment.vote_rshares.value ) ) / ( 2 * _db.get_content_constant_s() + comment.vote_rshares.value ) ).to_uint64();
uint64_t old_weight = ( ( std::numeric_limits< uint64_t >::max() * fc::uint128_t( old_vote_rshares.value ) ) / ( two_s + old_vote_rshares.value ) ).to_uint64();
uint64_t new_weight = ( ( std::numeric_limits< uint64_t >::max() * fc::uint128_t( comment.vote_rshares.value ) ) / ( two_s + comment.vote_rshares.value ) ).to_uint64();
cv.weight = new_weight - old_weight;
}
else
{
uint64_t old_weight = ( ( std::numeric_limits< uint64_t >::max() * fc::uint128_t( 1000000 * old_vote_rshares.value ) ) / ( 2 * _db.get_content_constant_s() + ( 1000000 * old_vote_rshares.value ) ) ).to_uint64();
uint64_t new_weight = ( ( std::numeric_limits< uint64_t >::max() * fc::uint128_t( 1000000 * comment.vote_rshares.value ) ) / ( 2 * _db.get_content_constant_s() + ( 1000000 * comment.vote_rshares.value ) ) ).to_uint64();
uint64_t old_weight = ( ( std::numeric_limits< uint64_t >::max() * fc::uint128_t( 1000000 * old_vote_rshares.value ) ) / ( two_s + ( 1000000 * old_vote_rshares.value ) ) ).to_uint64();
uint64_t new_weight = ( ( std::numeric_limits< uint64_t >::max() * fc::uint128_t( 1000000 * comment.vote_rshares.value ) ) / ( two_s + ( 1000000 * comment.vote_rshares.value ) ) ).to_uint64();
cv.weight = new_weight - old_weight;
}
}
Expand Down Expand Up @@ -1346,8 +1349,8 @@ void vote_evaluator::do_apply( const vote_operation& o )
fc::uint128_t new_rshares = std::max( comment.net_rshares.value, int64_t(0));

/// calculate rshares2 value
new_rshares = _db.calculate_vshares( new_rshares );
old_rshares = _db.calculate_vshares( old_rshares );
new_rshares = util::calculate_vshares( new_rshares );
old_rshares = util::calculate_vshares( old_rshares );

_db.modify( comment, [&]( comment_object& c )
{
Expand Down
13 changes: 13 additions & 0 deletions libraries/chain/util/reward.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

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

namespace steemit { namespace chain { namespace util {

uint128_t calculate_vshares( 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;
}

} } }
10 changes: 6 additions & 4 deletions tests/tests/operation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <steemit/chain/hardfork.hpp>
#include <steemit/chain/steem_objects.hpp>

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

#include <fc/crypto/digest.hpp>

#include "../common/database_fixture.hpp"
Expand Down Expand Up @@ -546,22 +548,22 @@ BOOST_AUTO_TEST_CASE( comment_apply )
{
com.net_rshares = 10;
com.abs_rshares = 10;
com.children_rshares2 = db.calculate_vshares( 10 );
com.children_rshares2 = steemit::chain::util::calculate_vshares( 10 );
});

db.modify( mod_bob_comment, [&]( comment_object& com)
{
com.children_rshares2 = db.calculate_vshares( 10 );
com.children_rshares2 = steemit::chain::util::calculate_vshares( 10 );
});

db.modify( mod_alice_comment, [&]( comment_object& com)
{
com.children_rshares2 = db.calculate_vshares( 10 );
com.children_rshares2 = steemit::chain::util::calculate_vshares( 10 );
});

db.modify( db.get_dynamic_global_properties(), [&]( dynamic_global_property_object& o)
{
o.total_reward_shares2 = db.calculate_vshares( 10 );
o.total_reward_shares2 = steemit::chain::util::calculate_vshares( 10 );
});

tx.signatures.clear();
Expand Down

0 comments on commit c26b83e

Please sign in to comment.