Skip to content

Commit

Permalink
Cleanup names and misc improvements #774
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Vandeberg committed Feb 22, 2017
1 parent 27c0c6c commit 9113031
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 41 deletions.
2 changes: 1 addition & 1 deletion libraries/app/database_api.cpp
Expand Up @@ -1112,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 = steemit::chain::util::calculate_vshares( d.net_rshares.value > 0 ? d.net_rshares.value : 0 );
auto vshares = steemit::chain::util::calculate_claims( d.net_rshares.value > 0 ? d.net_rshares.value : 0 );

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

Expand Down
21 changes: 10 additions & 11 deletions libraries/chain/database.cpp
Expand Up @@ -70,7 +70,7 @@ using boost::container::flat_set;

struct reward_fund_context
{
uint128_t recent_rshares2 = 0;
uint128_t recent_claims = 0;
asset reward_balance = asset( 0, STEEM_SYMBOL );
share_type steem_awarded = 0;
};
Expand Down Expand Up @@ -1639,7 +1639,7 @@ share_type database::cashout_comment_helper( util::comment_reward_context& ctx,
}

if( !has_hardfork( STEEMIT_HARDFORK_0_17__774 ) )
adjust_rshares2( comment, util::calculate_vshares( comment.net_rshares.value ), 0 );
adjust_rshares2( comment, util::calculate_claims( comment.net_rshares.value ), 0 );
}

modify( cat, [&]( category_object& c )
Expand Down Expand Up @@ -1731,12 +1731,12 @@ void database::process_comment_cashout()
// Add all reward funds to the local cache and decay their recent rshares
modify( *itr, [&]( reward_fund_object& rfo )
{
rfo.recent_rshares2 -= ( rfo.recent_rshares2 * ( head_block_time() - rfo.last_update ).to_seconds() ) / STEEMIT_RECENT_RSHARES_DECAY_RATE.to_seconds();
rfo.recent_claims -= ( rfo.recent_claims * ( head_block_time() - rfo.last_update ).to_seconds() ) / STEEMIT_RECENT_RSHARES_DECAY_RATE.to_seconds();
rfo.last_update = head_block_time();
});

reward_fund_context rf_ctx;
rf_ctx.recent_rshares2 = itr->recent_rshares2;
rf_ctx.recent_claims = itr->recent_claims;
rf_ctx.reward_balance = itr->reward_balance;

funds.push_back( rf_ctx );
Expand All @@ -1754,8 +1754,7 @@ void database::process_comment_cashout()
if( current->net_rshares > 0 )
{
const auto& rf = get_reward_fund( *current );
funds[ rf.id._id ].recent_rshares2 += util::calculate_vshares( current->net_rshares.value, rf );
FC_ASSERT( funds[ rf.id._id ].recent_rshares2 < std::numeric_limits< uint64_t >::max() );
funds[ rf.id._id ].recent_claims += util::calculate_claims( current->net_rshares.value, rf );

This comment has been minimized.

Copy link
@abitmore

abitmore Mar 7, 2017

Contributor

This funds[ rf.id._id ] code is ugly.

This comment has been minimized.

Copy link
@abitmore

abitmore Mar 7, 2017

Contributor

Having get_reward_fund(...) inside the loop is inefficient.

This comment has been minimized.

Copy link
@abitmore

abitmore Mar 7, 2017

Contributor

Actually it's unnecessary to call get_reward_fund(...) here which will query database.
With code like rf.id._id we presume that 0 is root post and 1 is comment, with the comment object we can already know it's root post or comment.

++current;
}
}
Expand All @@ -1781,7 +1780,7 @@ void database::process_comment_cashout()
if( has_hardfork( STEEMIT_HARDFORK_0_17__771 ) )
{
auto fund_id = get_reward_fund( *current ).id._id;
ctx.total_reward_shares2 = funds[ fund_id ].recent_rshares2;
ctx.total_reward_shares2 = funds[ fund_id ].recent_claims;
ctx.total_reward_fund_steem = funds[ fund_id ].reward_balance;
funds[ fund_id ].steem_awarded += cashout_comment_helper( ctx, *current );
}
Expand All @@ -1800,7 +1799,7 @@ void database::process_comment_cashout()
if( funds.size() )
{
const auto& rf = get_reward_fund( *current );
funds[ rf.id._id ].recent_rshares2 += util::calculate_vshares( current->net_rshares.value, rf );
funds[ rf.id._id ].recent_claims += util::calculate_claims( current->net_rshares.value, rf );
}

auto reward = cashout_comment_helper( ctx, comment );
Expand All @@ -1825,7 +1824,7 @@ void database::process_comment_cashout()
{
modify( get< reward_fund_object, by_id >( reward_fund_id_type( i ) ), [&]( reward_fund_object& rfo )
{
rfo.recent_rshares2 = funds[ i ].recent_rshares2;
rfo.recent_claims = funds[ i ].recent_claims;
rfo.reward_balance -= funds[ i ].steem_awarded;
});
}
Expand Down Expand Up @@ -4079,7 +4078,7 @@ void database::validate_invariants()const
{
if( itr->net_rshares.value > 0 )
{
auto delta = util::calculate_vshares( itr->net_rshares.value );
auto delta = util::calculate_claims( itr->net_rshares.value );
total_rshares2 += delta;
}
if( itr->parent_author == STEEMIT_ROOT_POST_PARENT )
Expand Down Expand Up @@ -4153,7 +4152,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, util::calculate_vshares( c.net_rshares.value ) );
adjust_rshares2( c, 0, util::calculate_claims( c.net_rshares.value ) );
}

// Update category rshares
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/steemit/chain/steem_objects.hpp
Expand Up @@ -261,7 +261,7 @@ namespace steemit { namespace chain {
reward_fund_id_type id;
reward_fund_name_type name;
asset reward_balance = asset( 0, STEEM_SYMBOL );
fc::uint128_t recent_rshares2 = 0;
fc::uint128_t recent_claims = 0;
time_point_sec last_update;
uint16_t percent_content_rewards = 0;
uint64_t content_constant = 0;
Expand Down Expand Up @@ -516,7 +516,7 @@ FC_REFLECT( steemit::chain::reward_fund_object,
(id)
(name)
(reward_balance)
(recent_rshares2)
(recent_claims)
(last_update)
(percent_content_rewards)
(content_constant)
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/steemit/chain/util/reward.hpp
Expand Up @@ -38,9 +38,9 @@ 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 );
uint128_t calculate_claims( const uint128_t& rshares );

uint128_t calculate_vshares( const uint128_t& rshares, const reward_fund_object& rf );
uint128_t calculate_claims( const uint128_t& rshares, const reward_fund_object& rf );

inline bool is_comment_payout_dust( const price& p, uint64_t steem_payout )
{
Expand Down
8 changes: 4 additions & 4 deletions libraries/chain/steem_evaluator.cpp
Expand Up @@ -1345,8 +1345,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 = util::calculate_vshares( new_rshares );
old_rshares = util::calculate_vshares( old_rshares );
new_rshares = util::calculate_claims( new_rshares );
old_rshares = util::calculate_claims( old_rshares );

This comment has been minimized.

Copy link
@abitmore

abitmore Mar 9, 2017

Contributor

For better performance, these 2 calculations can be skipped after HF17, for example move them to the end: https://github.com/steemit/steem/blame/v0.17.0rc2/libraries/chain/steem_evaluator.cpp#L1456


const auto& cat = _db.get_category( comment.category );
_db.modify( cat, [&]( category_object& c ){
Expand Down Expand Up @@ -1529,8 +1529,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 = util::calculate_vshares( new_rshares );
old_rshares = util::calculate_vshares( old_rshares );
new_rshares = util::calculate_claims( new_rshares );
old_rshares = util::calculate_claims( old_rshares );

This comment has been minimized.

Copy link
@abitmore

abitmore Mar 9, 2017

Contributor

For better performance, these 2 calculations can be skipped after HF17, for example move them to the end: https://github.com/steemit/steem/blame/v0.17.0rc2/libraries/chain/steem_evaluator.cpp#L1557


_db.modify( comment, [&]( comment_object& c )
{
Expand Down
20 changes: 9 additions & 11 deletions libraries/chain/util/reward.cpp
Expand Up @@ -17,7 +17,7 @@ uint64_t get_rshare_reward( const comment_reward_context& ctx )

//idump( (ctx) );

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

u256 payout_u256 = ( rf * rs2 ) / total_rshares2;
Expand All @@ -42,16 +42,15 @@ uint64_t get_rshare_reward( const comment_reward_context& ctx, const reward_fund
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 total_claims = to256( ctx.total_reward_shares2 );

//idump( (ctx) );

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

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

Expand All @@ -66,21 +65,20 @@ uint64_t get_rshare_reward( const comment_reward_context& ctx, const reward_fund
} FC_CAPTURE_AND_RETHROW( (ctx) )
}

uint128_t calculate_vshares( const uint128_t& rshares )
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_vshares( const uint128_t& rshares, const reward_fund_object& rf )
uint128_t calculate_claims( const uint128_t& rshares, const reward_fund_object& rf )
{
if( rf.name == STEEMIT_POST_REWARD_FUND_NAME )
{
// r^2 + 2rs
uint128_t s = rf.content_constant;
uint128_t rshares_plus_s = rshares + s;
return rshares_plus_s * rshares_plus_s - s * s;
uint128_t rshares_plus_s = rshares + rf.content_constant;
return rshares_plus_s * rshares_plus_s + rf.content_constant * rf.content_constant;
}
else if( rf.name == STEEMIT_COMMENT_REWARD_FUND_NAME )
{
Expand Down
8 changes: 4 additions & 4 deletions tests/tests/operation_tests.cpp
Expand Up @@ -548,22 +548,22 @@ BOOST_AUTO_TEST_CASE( comment_apply )
{
com.net_rshares = 10;
com.abs_rshares = 10;
com.children_rshares2 = steemit::chain::util::calculate_vshares( 10 );
com.children_rshares2 = steemit::chain::util::calculate_claims( 10 );
});

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

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

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

tx.signatures.clear();
Expand Down
12 changes: 6 additions & 6 deletions tests/tests/operation_time_tests.cpp
Expand Up @@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE( reward_funds )
FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_CASE( recent_rshares2_decay )
BOOST_AUTO_TEST_CASE( recent_claims_decay )
{
try
{
Expand Down Expand Up @@ -316,7 +316,7 @@ BOOST_AUTO_TEST_CASE( recent_rshares2_decay )
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

auto alice_vshares = util::calculate_vshares( 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 ) );

generate_blocks( 5 );

Expand All @@ -334,12 +334,12 @@ BOOST_AUTO_TEST_CASE( recent_rshares2_decay )
{
const auto& post_rf = db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME );

BOOST_REQUIRE( post_rf.recent_rshares2 == alice_vshares );
BOOST_REQUIRE( post_rf.recent_claims == alice_vshares );
validate_database();
}

auto bob_cashout_time = db.get_comment( "bob", string( "test" ) ).cashout_time;
auto bob_vshares = util::calculate_vshares( 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 ) );

generate_block();

Expand All @@ -348,7 +348,7 @@ BOOST_AUTO_TEST_CASE( recent_rshares2_decay )
alice_vshares -= ( alice_vshares * STEEMIT_BLOCK_INTERVAL ) / STEEMIT_RECENT_RSHARES_DECAY_RATE.to_seconds();
const auto& post_rf = db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME );

BOOST_REQUIRE( post_rf.recent_rshares2 == alice_vshares );
BOOST_REQUIRE( post_rf.recent_claims == alice_vshares );

generate_block();

Expand All @@ -358,7 +358,7 @@ BOOST_AUTO_TEST_CASE( recent_rshares2_decay )
alice_vshares -= ( alice_vshares * STEEMIT_BLOCK_INTERVAL ) / STEEMIT_RECENT_RSHARES_DECAY_RATE.to_seconds();
const auto& post_rf = db.get< reward_fund_object, by_name >( STEEMIT_POST_REWARD_FUND_NAME );

BOOST_REQUIRE( post_rf.recent_rshares2 == alice_vshares + bob_vshares );
BOOST_REQUIRE( post_rf.recent_claims == alice_vshares + bob_vshares );
validate_database();
}
}
Expand Down

0 comments on commit 9113031

Please sign in to comment.