From 73829bd97f070f8264ab9a98a8a68a3a21be1ec4 Mon Sep 17 00:00:00 2001 From: Sandip Patel Date: Wed, 23 Oct 2019 11:56:38 +0530 Subject: [PATCH 1/3] Fixed GPOS vesting factor issue when proxy is set --- libraries/chain/db_maint.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index 81fce8f9..182c04fc 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -727,9 +727,13 @@ void deprecate_annual_members( database& db ) double database::calculate_vesting_factor(const account_object& stake_account) { - // get last time voted form stats - const auto &stats = stake_account.statistics(*this); - fc::time_point_sec last_date_voted = stats.last_vote_time; + fc::time_point_sec last_date_voted; + // get last time voted form account stats + // check last_vote_time of proxy voting account if proxy is set + if (stake_account.options.voting_account == GRAPHENE_PROXY_TO_SELF_ACCOUNT) + last_date_voted = stake_account.statistics(*this).last_vote_time; + else + last_date_voted = stake_account.options.voting_account(*this).statistics(*this).last_vote_time; // get global data related to gpos const auto &gpo = this->get_global_properties(); From ccdea033f398b7de760e08d9b220a54fb50f9456 Mon Sep 17 00:00:00 2001 From: Sandip Patel Date: Wed, 23 Oct 2019 17:41:16 +0530 Subject: [PATCH 2/3] Added unit test for proxy voting --- tests/tests/gpos_tests.cpp | 87 +++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/tests/tests/gpos_tests.cpp b/tests/tests/gpos_tests.cpp index 11104409..81f56500 100644 --- a/tests/tests/gpos_tests.cpp +++ b/tests/tests/gpos_tests.cpp @@ -832,8 +832,94 @@ BOOST_AUTO_TEST_CASE( competing_proposals ) */ BOOST_AUTO_TEST_CASE( proxy_voting ) { + ACTORS((alice)(bob)); try { + // move to hardfork + generate_blocks( HARDFORK_GPOS_TIME ); + generate_block(); + + // database api + graphene::app::database_api db_api(db); + + const auto& core = asset_id_type()(db); + + // send some asset to alice and bob + transfer( committee_account, alice_id, core.amount( 1000 ) ); + transfer( committee_account, bob_id, core.amount( 1000 ) ); + generate_block(); + + // add some vesting to alice and bob + create_vesting(alice_id, core.amount(100), vesting_balance_type::gpos); + generate_block(); + + // total balance is 100 rest of data at 0 + auto gpos_info = db_api.get_gpos_info(alice_id); + BOOST_CHECK_EQUAL(gpos_info.vesting_factor, 0); + BOOST_CHECK_EQUAL(gpos_info.award.amount.value, 0); + BOOST_CHECK_EQUAL(gpos_info.total_amount.value, 100); + + create_vesting(bob_id, core.amount(100), vesting_balance_type::gpos); + generate_block(); + + gpos_info = db_api.get_gpos_info(bob_id); + BOOST_CHECK_EQUAL(gpos_info.vesting_factor, 0); + BOOST_CHECK_EQUAL(gpos_info.award.amount.value, 0); + BOOST_CHECK_EQUAL(gpos_info.total_amount.value, 200); + + auto now = db.head_block_time(); + update_gpos_global(518400, 86400, now); + + BOOST_CHECK_EQUAL(db.get_global_properties().parameters.gpos_period(), 518400); + BOOST_CHECK_EQUAL(db.get_global_properties().parameters.gpos_subperiod(), 86400); + BOOST_CHECK_EQUAL(db.get_global_properties().parameters.gpos_period_start(), now.sec_since_epoch()); + + // alice assign bob as voting account + graphene::chain::account_update_operation op; + op.account = alice_id; + op.new_options = alice_id(db).options; + op.new_options->voting_account = bob_id; + trx.operations.push_back(op); + set_expiration(db, trx); + trx.validate(); + sign(trx, alice_private_key); + PUSH_TX( db, trx, ~0 ); + trx.clear(); + + generate_block(); + + // vote for witness1 + auto witness1 = witness_id_type(1)(db); + vote_for(bob_id, witness1.vote_id, bob_private_key); + + generate_blocks(db.get_dynamic_global_properties().next_maintenance_time); + + // check vesting factor of current subperiod + BOOST_CHECK_EQUAL(db_api.get_gpos_info(alice_id).vesting_factor, 1); + BOOST_CHECK_EQUAL(db_api.get_gpos_info(bob_id).vesting_factor, 1); + + generate_blocks(db.get_dynamic_global_properties().next_maintenance_time); + generate_block(); + + // GPOS 2nd subperiod started. + // vesting factor decay + BOOST_CHECK_EQUAL(db_api.get_gpos_info(alice_id).vesting_factor, 0.83333333333333337); + BOOST_CHECK_EQUAL(db_api.get_gpos_info(bob_id).vesting_factor, 0.83333333333333337); + + // vote for witness2 + auto witness2 = witness_id_type(2)(db); + vote_for(bob_id, witness2.vote_id, bob_private_key); + + // vesting factor should be 1 for both alice and bob for the current subperiod + BOOST_CHECK_EQUAL(db_api.get_gpos_info(alice_id).vesting_factor, 1); + BOOST_CHECK_EQUAL(db_api.get_gpos_info(bob_id).vesting_factor, 1); + + generate_blocks(db.get_dynamic_global_properties().next_maintenance_time); + generate_block(); + + // vesting factor decay + BOOST_CHECK_EQUAL(db_api.get_gpos_info(alice_id).vesting_factor, 0.83333333333333337); + BOOST_CHECK_EQUAL(db_api.get_gpos_info(bob_id).vesting_factor, 0.83333333333333337); } catch (fc::exception &e) { edump((e.to_detail_string())); @@ -949,5 +1035,4 @@ BOOST_AUTO_TEST_CASE( database_api ) throw; } } - BOOST_AUTO_TEST_SUITE_END() From 8bbab4c113aa77164a8382bd025eb4b0de748acd Mon Sep 17 00:00:00 2001 From: Sandip Patel Date: Wed, 23 Oct 2019 18:25:33 +0530 Subject: [PATCH 3/3] Review changes --- tests/tests/gpos_tests.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/tests/gpos_tests.cpp b/tests/tests/gpos_tests.cpp index 81f56500..5b089685 100644 --- a/tests/tests/gpos_tests.cpp +++ b/tests/tests/gpos_tests.cpp @@ -906,6 +906,14 @@ BOOST_AUTO_TEST_CASE( proxy_voting ) BOOST_CHECK_EQUAL(db_api.get_gpos_info(alice_id).vesting_factor, 0.83333333333333337); BOOST_CHECK_EQUAL(db_api.get_gpos_info(bob_id).vesting_factor, 0.83333333333333337); + generate_blocks(db.get_dynamic_global_properties().next_maintenance_time); + generate_block(); + + // GPOS 3rd subperiod started + // vesting factor decay + BOOST_CHECK_EQUAL(db_api.get_gpos_info(alice_id).vesting_factor, 0.66666666666666663); + BOOST_CHECK_EQUAL(db_api.get_gpos_info(bob_id).vesting_factor, 0.66666666666666663); + // vote for witness2 auto witness2 = witness_id_type(2)(db); vote_for(bob_id, witness2.vote_id, bob_private_key);