From f56cebed88833bad7228de9f204fb40cda1a119e Mon Sep 17 00:00:00 2001 From: Michael Vandeberg Date: Sun, 1 Sep 2019 13:07:14 -0700 Subject: [PATCH 1/6] Add division check in curation weight --- libraries/chain/database.cpp | 8 ++++---- libraries/chain/util/reward.cpp | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/libraries/chain/database.cpp b/libraries/chain/database.cpp index d4bd09d73e..6f586fae28 100644 --- a/libraries/chain/database.cpp +++ b/libraries/chain/database.cpp @@ -1829,7 +1829,7 @@ share_type database::pay_curators( const comment_object& c, share_type& max_rewa } for( auto& item : proxy_set ) - { + { try { uint128_t weight( item->weight ); auto claim = ( ( max_rewards.value * weight ) / total_weight ).to_uint64(); if( claim > 0 ) // min_amt is non-zero satoshis @@ -1852,12 +1852,12 @@ share_type database::pay_curators( const comment_object& c, share_type& max_rewa #endif post_push_virtual_operation( vop ); } - } + } FC_CAPTURE_AND_RETHROW( (*item) ) } } max_rewards -= unclaimed_rewards; return unclaimed_rewards; - } FC_CAPTURE_AND_RETHROW() + } FC_CAPTURE_AND_RETHROW( (max_rewards) ) } void fill_comment_reward_context_local_state( util::comment_reward_context& ctx, const comment_object& comment ) @@ -2029,7 +2029,7 @@ share_type database::cashout_comment_helper( util::comment_reward_context& ctx, } return claimed_reward; - } FC_CAPTURE_AND_RETHROW( (comment) ) + } FC_CAPTURE_AND_RETHROW( (comment)(ctx) ) } void database::process_comment_cashout() diff --git a/libraries/chain/util/reward.cpp b/libraries/chain/util/reward.cpp index ff72803d32..8161be31f9 100644 --- a/libraries/chain/util/reward.cpp +++ b/libraries/chain/util/reward.cpp @@ -100,7 +100,9 @@ uint128_t evaluate_reward_curve( const uint128_t& rshares, const protocol::curve case protocol::convergent_square_root: { const uint128_t& s = var1; - result = rshares / approx_sqrt( rshares + 2 * s ); + uint128_t sqrt = approx_sqrt( rshares + 2 * s ); + if( rshares < sqrt ) result = 0; + else result = rshares / approx_sqrt( rshares + 2 * s ); } break; } From 79a5256ec93b520a138ebafcf5f6dc556cc264d4 Mon Sep 17 00:00:00 2001 From: Michael Vandeberg Date: Sun, 1 Sep 2019 15:09:34 -0700 Subject: [PATCH 2/6] Test in CI --- Dockerfile | 1 + libraries/chain/util/reward.cpp | 4 +- tests/tests/basic_tests.cpp | 73 +++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2fbeddd5cc..6c2f5b171d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -82,6 +82,7 @@ RUN \ fi && \ cd tests && \ ctest -j$(nproc) --output-on-failure && \ + ./chain_test -t basic_tests/curation_weight_test && \ cd .. && \ ./libraries/mira/test/mira_test && \ ./programs/util/test_fixed_string && \ diff --git a/libraries/chain/util/reward.cpp b/libraries/chain/util/reward.cpp index 8161be31f9..ff72803d32 100644 --- a/libraries/chain/util/reward.cpp +++ b/libraries/chain/util/reward.cpp @@ -100,9 +100,7 @@ uint128_t evaluate_reward_curve( const uint128_t& rshares, const protocol::curve case protocol::convergent_square_root: { const uint128_t& s = var1; - uint128_t sqrt = approx_sqrt( rshares + 2 * s ); - if( rshares < sqrt ) result = 0; - else result = rshares / approx_sqrt( rshares + 2 * s ); + result = rshares / approx_sqrt( rshares + 2 * s ); } break; } diff --git a/tests/tests/basic_tests.cpp b/tests/tests/basic_tests.cpp index 46afe247dd..cf5fb0b7d0 100644 --- a/tests/tests/basic_tests.cpp +++ b/tests/tests/basic_tests.cpp @@ -32,6 +32,8 @@ #include #include +#include + #include #include #include "../db_fixture/database_fixture.hpp" @@ -357,4 +359,75 @@ BOOST_AUTO_TEST_CASE( adjust_balance_test ) BOOST_REQUIRE( db->get_balance( "alice", SBD_SYMBOL ) == asset( 0, SBD_SYMBOL ) ); } +uint8_t find_msb( const uint128_t& u ) +{ + uint64_t x; + uint8_t places; + x = (u.lo ? u.lo : 1); + places = (u.hi ? 64 : 0); + x = (u.hi ? u.hi : x); + return uint8_t( boost::multiprecision::detail::find_msb(x) + places ); +} + +uint64_t approx_sqrt( const uint128_t& x ) +{ + if( (x.lo == 0) && (x.hi == 0) ) + return 0; + + uint8_t msb_x = find_msb(x); + uint8_t msb_z = msb_x >> 1; + + uint128_t msb_x_bit = uint128_t(1) << msb_x; + uint64_t msb_z_bit = uint64_t (1) << msb_z; + + uint128_t mantissa_mask = msb_x_bit - 1; + uint128_t mantissa_x = x & mantissa_mask; + uint64_t mantissa_z_hi = (msb_x & 1) ? msb_z_bit : 0; + uint64_t mantissa_z_lo = (mantissa_x >> (msb_x - msb_z)).lo; + uint64_t mantissa_z = (mantissa_z_hi | mantissa_z_lo) >> 1; + uint64_t result = msb_z_bit | mantissa_z; + + return result; +} + +BOOST_AUTO_TEST_CASE( curation_weight_test ) +{ + fc::uint128_t rshares = 856158; + fc::uint128_t s = 2000000000000ull; + fc::uint128_t sqrt = approx_sqrt( rshares + 2 * s ); + uint64_t result = ( rshares / sqrt ).to_uint64(); + + BOOST_REQUIRE( sqrt.to_uint64() == 2002250 ); + BOOST_REQUIRE( result == 0 ); + + rshares = 0; + sqrt = approx_sqrt( rshares + 2 * s ); + result = ( rshares / sqrt ).to_uint64(); + + BOOST_REQUIRE( sqrt.to_uint64() == 2002250 ); + BOOST_REQUIRE( result == 0 ); + + result = ( uint128_t( 0 ) - uint128_t( 0 ) ).to_uint64(); + + BOOST_REQUIRE( result == 0 ); + rshares = 3351842535167ull; + + for( int64_t i = 856158; i >= 0; --i ) + { + uint64_t old_weight = util::evaluate_reward_curve( rshares - i, protocol::convergent_square_root, s ).to_uint64(); + uint64_t new_weight = util::evaluate_reward_curve( rshares, protocol::convergent_square_root, s ).to_uint64(); + + BOOST_REQUIRE( old_weight <= new_weight ); + + uint128_t w( new_weight - old_weight ); + + w *= 300; + w /= 300; + BOOST_REQUIRE( w.to_uint64() == new_weight - old_weight ); + } + + //idump( (delta)(old_weight)(new_weight) ); + +} + BOOST_AUTO_TEST_SUITE_END() From 043fe8dadb871d748a9a0d6cfae6ae6a2e7b23c5 Mon Sep 17 00:00:00 2001 From: Michael Vandeberg Date: Sun, 1 Sep 2019 15:28:05 -0700 Subject: [PATCH 3/6] Fix build for linux --- tests/tests/basic_tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests/basic_tests.cpp b/tests/tests/basic_tests.cpp index cf5fb0b7d0..ce6c5910f6 100644 --- a/tests/tests/basic_tests.cpp +++ b/tests/tests/basic_tests.cpp @@ -393,7 +393,7 @@ uint64_t approx_sqrt( const uint128_t& x ) BOOST_AUTO_TEST_CASE( curation_weight_test ) { fc::uint128_t rshares = 856158; - fc::uint128_t s = 2000000000000ull; + fc::uint128_t s = uint128_t( 0, 2000000000000ull ); fc::uint128_t sqrt = approx_sqrt( rshares + 2 * s ); uint64_t result = ( rshares / sqrt ).to_uint64(); @@ -410,7 +410,7 @@ BOOST_AUTO_TEST_CASE( curation_weight_test ) result = ( uint128_t( 0 ) - uint128_t( 0 ) ).to_uint64(); BOOST_REQUIRE( result == 0 ); - rshares = 3351842535167ull; + rshares = uint128_t( 0, 3351842535167ull ); for( int64_t i = 856158; i >= 0; --i ) { From 0146441e93471b2bd4efa8c45a9b3baa8defa4a2 Mon Sep 17 00:00:00 2001 From: Michael Vandeberg Date: Sun, 1 Sep 2019 16:55:22 -0700 Subject: [PATCH 4/6] Add logging --- libraries/chain/steem_evaluator.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libraries/chain/steem_evaluator.cpp b/libraries/chain/steem_evaluator.cpp index f02dd74748..473a453c86 100644 --- a/libraries/chain/steem_evaluator.cpp +++ b/libraries/chain/steem_evaluator.cpp @@ -2088,6 +2088,12 @@ void hf20_vote_evaluator( const vote_operation& o, database& _db ) w *= delta_t; w /= dgpo.reverse_auction_seconds; cv.weight = w.to_uint64(); + + if( voter.name == "lushburg" && comment.author == "kostybrat" && to_string( comment.permlink ) == "mushroom-butterfly" ) + { + ilog( "NOTIFYALERT" ); + idump( (reward_fund)(curve)(old_vote_rshares.value)(comment.vote_rshares.value)(old_weight)(new_weight)(max_vote_weight)(delta_t)(dgpo.reverse_auction_seconds)(cv.weight) ); + } } else { From 587574f7aefd030ac440df853e00263a4d92c610 Mon Sep 17 00:00:00 2001 From: Michael Vandeberg Date: Sun, 1 Sep 2019 18:12:46 -0700 Subject: [PATCH 5/6] min vote weight to 0 --- libraries/chain/steem_evaluator.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/libraries/chain/steem_evaluator.cpp b/libraries/chain/steem_evaluator.cpp index 473a453c86..61bc432185 100644 --- a/libraries/chain/steem_evaluator.cpp +++ b/libraries/chain/steem_evaluator.cpp @@ -2077,22 +2077,24 @@ void hf20_vote_evaluator( const vote_operation& o, database& _db ) auto curve = reward_fund.curation_reward_curve; uint64_t old_weight = util::evaluate_reward_curve( old_vote_rshares.value, curve, reward_fund.content_constant ).to_uint64(); uint64_t new_weight = util::evaluate_reward_curve( comment.vote_rshares.value, curve, reward_fund.content_constant ).to_uint64(); - cv.weight = new_weight - old_weight; - max_vote_weight = cv.weight; + if( old_weight >= new_weight ) // old_weight > new_weight should never happen + { + cv.weight = 0; + } + else + { + cv.weight = new_weight - old_weight; - /// discount weight by time - uint128_t w(max_vote_weight); - uint64_t delta_t = std::min( uint64_t((cv.last_update - comment.created).to_seconds()), uint64_t( dgpo.reverse_auction_seconds ) ); + max_vote_weight = cv.weight; - w *= delta_t; - w /= dgpo.reverse_auction_seconds; - cv.weight = w.to_uint64(); + /// discount weight by time + uint128_t w(max_vote_weight); + uint64_t delta_t = std::min( uint64_t((cv.last_update - comment.created).to_seconds()), uint64_t( dgpo.reverse_auction_seconds ) ); - if( voter.name == "lushburg" && comment.author == "kostybrat" && to_string( comment.permlink ) == "mushroom-butterfly" ) - { - ilog( "NOTIFYALERT" ); - idump( (reward_fund)(curve)(old_vote_rshares.value)(comment.vote_rshares.value)(old_weight)(new_weight)(max_vote_weight)(delta_t)(dgpo.reverse_auction_seconds)(cv.weight) ); + w *= delta_t; + w /= dgpo.reverse_auction_seconds; + cv.weight = w.to_uint64(); } } else From 44a135b8e5cfa813ac02cee9cdeea411da965d33 Mon Sep 17 00:00:00 2001 From: Michael Vandeberg Date: Sun, 1 Sep 2019 19:16:56 -0700 Subject: [PATCH 6/6] Bump version to 0.22.1 --- libraries/protocol/include/steem/protocol/config.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/protocol/include/steem/protocol/config.hpp b/libraries/protocol/include/steem/protocol/config.hpp index 0d69f3e272..5ad76e3e65 100644 --- a/libraries/protocol/include/steem/protocol/config.hpp +++ b/libraries/protocol/include/steem/protocol/config.hpp @@ -44,7 +44,7 @@ #else // IS LIVE STEEM NETWORK -#define STEEM_BLOCKCHAIN_VERSION ( version(0, 22, 0) ) +#define STEEM_BLOCKCHAIN_VERSION ( version(0, 22, 1) ) #define STEEM_INIT_PUBLIC_KEY_STR "STM8GC13uCZbP44HzMLV6zPZGwVQ8Nt4Kji8PapsPiNq1BK153XTX" #define STEEM_CHAIN_ID fc::sha256()