Skip to content

Commit

Permalink
Merge pull request steemit#1151 from steemit/1148-vote-sqrt-fix
Browse files Browse the repository at this point in the history
Square Root Vote Weight Errors
  • Loading branch information
Michael Vandeberg authored Jun 2, 2017
2 parents 3e1e9f7 + 16ff152 commit d04a629
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
4 changes: 4 additions & 0 deletions libraries/chain/hardfork.d/0_6.hf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

// Fri Jun 24 14:00:00 UTC 2016
// Fri Jun 24 10:00:00 EDT 2016
#ifdef IS_TEST_NET
#define STEEMIT_HARDFORK_0_6_REVERSE_AUCTION_TIME (0)
#else
#define STEEMIT_HARDFORK_0_6_REVERSE_AUCTION_TIME (1467295200-(60*60*24*6))
#endif
#define STEEMIT_HARDFORK_0_6_VERSION hardfork_version( 0, 6 )
#endif
14 changes: 12 additions & 2 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,14 @@ void vote_evaluator::do_apply( const vote_operation& o )
uint64_t new_weight = util::evaluate_reward_curve( comment.vote_rshares.value, reward_fund.curation_reward_curve, reward_fund.content_constant ).to_uint64();
cv.weight = new_weight - old_weight;
#warning( "TODO: After HF19 caclulate which comment was the first to be paid on sqrt curation and activate the logic based on time" )

#ifndef IS_TEST_NET
/*
* Disabling this check so we can test the precalculation logic.
* It is not needed on live because the precalculation is only needed pre HF19
*/
if( !_db.has_hardfork( STEEMIT_HARDFORK_0_19__1052 ) )
#endif
{
old_weight = util::evaluate_reward_curve( old_vote_rshares.value, curve_id::square_root ).to_uint64();
new_weight = util::evaluate_reward_curve( comment.vote_rshares.value, curve_id::square_root ).to_uint64();
Expand Down Expand Up @@ -1395,8 +1402,9 @@ void vote_evaluator::do_apply( const vote_operation& o )
w *= delta_t;
w /= STEEMIT_REVERSE_AUCTION_WINDOW_SECONDS;
cv.weight = w.to_uint64();

#ifndef IS_TEST_NET
if( _db.has_hardfork( STEEMIT_HARDFORK_0_17 ) )
#endif
{
uint128_t w(sqrt_max_vote_weight);
w *= delta_t;
Expand All @@ -1416,7 +1424,7 @@ void vote_evaluator::do_apply( const vote_operation& o )
_db.modify( comment, [&]( comment_object& c )
{
c.total_vote_weight += max_vote_weight;
c.total_sqrt_vote_weight = sqrt_max_vote_weight;
c.total_sqrt_vote_weight += sqrt_max_vote_weight;
});
}
if( !_db.has_hardfork( STEEMIT_HARDFORK_0_17__774) )
Expand Down Expand Up @@ -1515,6 +1523,7 @@ void vote_evaluator::do_apply( const vote_operation& o )
_db.modify( comment, [&]( comment_object& c )
{
c.total_vote_weight -= itr->weight;
c.total_sqrt_vote_weight -= itr->sqrt_weight;
});

_db.modify( *itr, [&]( comment_vote_object& cv )
Expand All @@ -1523,6 +1532,7 @@ void vote_evaluator::do_apply( const vote_operation& o )
cv.vote_percent = o.weight;
cv.last_update = _db.head_block_time();
cv.weight = 0;
cv.sqrt_weight = 0;
cv.num_changes += 1;
});

Expand Down
92 changes: 92 additions & 0 deletions tests/tests/operation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6651,5 +6651,97 @@ BOOST_AUTO_TEST_CASE( comment_beneficiaries_apply )
FC_LOG_AND_RETHROW()
}

#warning( "TODO: This test is not needed after HF19" )
BOOST_AUTO_TEST_CASE( vote_weight_test )
{
try
{
BOOST_TEST_MESSAGE( "Test Comment Beneficiaries" );
ACTORS( (alice)(bob)(sam)(dave) )
generate_block();

set_price_feed( price( ASSET( "1.000 TESTS" ), ASSET( "1.000 TBD" ) ) );

comment_operation comment;
vote_operation vote;
signed_transaction tx;

comment.author = "alice";
comment.permlink = "test";
comment.parent_permlink = "test";
comment.title = "test";
comment.body = "foobar";

tx.operations.push_back( comment );
tx.set_expiration( db.head_block_time() + STEEMIT_MAX_TIME_UNTIL_EXPIRATION );
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

generate_blocks( 10 );

BOOST_TEST_MESSAGE( "--- Testing initial vote" );

vote.voter = "alice";
vote.author = "alice";
vote.permlink = "test";
vote.weight = STEEMIT_100_PERCENT;

tx.clear();
tx.operations.push_back( vote );
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

{
const auto& alice = db.get_account( "alice" );
const auto& alice_comment = db.get_comment( comment.author, comment.permlink );
const auto& alice_vote = db.get< comment_vote_object, by_comment_voter >( boost::make_tuple( alice_comment.id, alice.id ) );

BOOST_REQUIRE( alice_vote.weight == alice_vote.sqrt_weight );
BOOST_REQUIRE( alice_comment.total_vote_weight == alice_comment.total_sqrt_vote_weight );
}

generate_blocks( 10 );

BOOST_TEST_MESSAGE( "--- Testing second vote" );

vote.voter = "bob";

tx.clear();
tx.operations.push_back( vote );
tx.sign( bob_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

{
const auto& bob = db.get_account( "bob" );
const auto& alice_comment = db.get_comment( comment.author, comment.permlink );
const auto& bob_vote = db.get< comment_vote_object, by_comment_voter >( boost::make_tuple( alice_comment.id, bob.id ) );

BOOST_REQUIRE( bob_vote.weight == bob_vote.sqrt_weight );
BOOST_REQUIRE( alice_comment.total_vote_weight == alice_comment.total_sqrt_vote_weight );
}

generate_blocks( 10 );

BOOST_TEST_MESSAGE( "--- Testing removing a vote" );

vote.weight = 0;

tx.clear();
tx.operations.push_back( vote );
tx.sign( bob_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

{
const auto& bob = db.get_account( "bob" );
const auto& alice_comment = db.get_comment( comment.author, comment.permlink );
const auto& bob_vote = db.get< comment_vote_object, by_comment_voter >( boost::make_tuple( alice_comment.id, bob.id ) );

BOOST_REQUIRE( bob_vote.weight == bob_vote.sqrt_weight );
BOOST_REQUIRE( alice_comment.total_vote_weight == alice_comment.total_sqrt_vote_weight );
}
}
FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_SUITE_END()
#endif
2 changes: 1 addition & 1 deletion tests/tests/operation_time_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE( comment_payout_equalize )
const account_object& bob_account = db.get_account("bob");
const account_object& dave_account = db.get_account("dave");

BOOST_CHECK( alice_account.reward_sbd_balance == ASSET( "10720.000 TBD" ) );
BOOST_CHECK( alice_account.reward_sbd_balance == ASSET( "14288.000 TBD" ) );
BOOST_CHECK( bob_account.reward_sbd_balance == ASSET( "0.000 TBD" ) );
BOOST_CHECK( dave_account.reward_sbd_balance == alice_account.reward_sbd_balance );
}
Expand Down

0 comments on commit d04a629

Please sign in to comment.