From a4e5a03973840481d50dacea4e5d6d6da35537dd Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Mon, 10 Aug 2015 16:39:09 -0400 Subject: [PATCH] Resolve #161: Fix account creation fee scaling --- libraries/chain/account_evaluator.cpp | 5 ++-- libraries/chain/db_maint.cpp | 5 +--- .../graphene/chain/protocol/fee_schedule.hpp | 13 +++++++-- tests/common/database_fixture.cpp | 2 +- tests/tests/block_tests.cpp | 29 ++++++++++++++++++- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/libraries/chain/account_evaluator.cpp b/libraries/chain/account_evaluator.cpp index 5f7960b3..26006fd7 100644 --- a/libraries/chain/account_evaluator.cpp +++ b/libraries/chain/account_evaluator.cpp @@ -107,13 +107,12 @@ object_id_type account_create_evaluator::do_apply( const account_create_operatio ++p.accounts_registered_this_interval; }); - /** TODO: update fee scaling for account creation... + const auto& global_properties = db().get_global_properties(); if( dynamic_properties.accounts_registered_this_interval % global_properties.parameters.accounts_per_fee_scale == 0 ) db().modify(global_properties, [&dynamic_properties](global_property_object& p) { - p.parameters.current_fees.account_create_fee <<= p.parameters.account_fee_scale_bitshifts; + p.parameters.current_fees->get().basic_fee <<= p.parameters.account_fee_scale_bitshifts; }); - */ return new_acnt_object.id; } FC_CAPTURE_AND_RETHROW((o)) } diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index e886aee7..9519b5ae 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -461,12 +461,9 @@ void database::perform_chain_maintenance(const signed_block& next_block, const g modify(gpo, [this](global_property_object& p) { // Remove scaling of account registration fee - /* - /// TODO reimplement this const auto& dgpo = get_dynamic_global_properties(); - p.parameters.current_fees.account_create_fee >>= p.parameters.account_fee_scale_bitshifts * + p.parameters.current_fees->get().basic_fee >>= p.parameters.account_fee_scale_bitshifts * (dgpo.accounts_registered_this_interval / p.parameters.accounts_per_fee_scale); - */ if( p.pending_parameters ) { diff --git a/libraries/chain/include/graphene/chain/protocol/fee_schedule.hpp b/libraries/chain/include/graphene/chain/protocol/fee_schedule.hpp index 93caecf9..86cafa9b 100644 --- a/libraries/chain/include/graphene/chain/protocol/fee_schedule.hpp +++ b/libraries/chain/include/graphene/chain/protocol/fee_schedule.hpp @@ -5,7 +5,7 @@ namespace graphene { namespace chain { template struct transform_to_fee_parameters; template - struct transform_to_fee_parameters> + struct transform_to_fee_parameters> { typedef fc::static_variant< typename T::fee_parameters_type... > type; }; @@ -21,7 +21,7 @@ namespace graphene { namespace chain { static fee_schedule get_default(); /** - * Finds the appropriate fee parameter struct for the operation + * Finds the appropriate fee parameter struct for the operation * and then calculates the appropriate fee. */ asset calculate_fee( const operation& op, const price& core_exchange_rate = price::unit_price() )const; @@ -41,6 +41,13 @@ namespace graphene { namespace chain { FC_ASSERT( itr != parameters.end() ); return itr->template get(); } + template + typename Operation::fee_parameters_type& get() + { + auto itr = parameters.find( typename Operation::fee_parameters_type() ); + FC_ASSERT( itr != parameters.end() ); + return itr->template get(); + } /** * @note must be sorted by fee_parameters.which() and have no duplicates @@ -51,7 +58,7 @@ namespace graphene { namespace chain { typedef fee_schedule fee_schedule_type; -} } // graphene::chain +} } // graphene::chain FC_REFLECT_TYPENAME( graphene::chain::fee_parameters ) FC_REFLECT( graphene::chain::fee_schedule, (parameters)(scale) ) diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index 0f7b4f14..53cbd00a 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -822,7 +822,7 @@ void database_fixture::enable_fees() db.modify(global_property_id_type()(db), [](global_property_object& gpo) { gpo.parameters.current_fees = fee_schedule::get_default(); - } ); + }); } void database_fixture::upgrade_to_lifetime_member(account_id_type account) diff --git a/tests/tests/block_tests.cpp b/tests/tests/block_tests.cpp index 2c84005f..c16b49d7 100644 --- a/tests/tests/block_tests.cpp +++ b/tests/tests/block_tests.cpp @@ -680,7 +680,7 @@ BOOST_FIXTURE_TEST_CASE( double_sign_check, database_fixture ) t.to = alice.id; t.amount = amount; trx.operations.push_back(t); - for( auto& op : trx.operations ) db.current_fee_schedule().set_fee(op); + for( auto& op : trx.operations ) db.current_fee_schedule().set_fee(op); trx.validate(); BOOST_TEST_MESSAGE( "Verify that not-signing causes an exception" ); @@ -967,4 +967,31 @@ BOOST_FIXTURE_TEST_CASE( rsf_missed_blocks, database_fixture ) FC_LOG_AND_RETHROW() } +BOOST_FIXTURE_TEST_CASE( account_create_fee_scaling, database_fixture ) +{ try { + auto accounts_per_scale = db.get_global_properties().parameters.accounts_per_fee_scale; + db.modify(global_property_id_type()(db), [](global_property_object& gpo) + { + gpo.parameters.current_fees = fee_schedule::get_default(); + gpo.parameters.current_fees->get().basic_fee = 1; + }); + + BOOST_CHECK_EQUAL(db.get_global_properties().parameters.current_fees->get().basic_fee, 1); + for( int i = db.get_dynamic_global_properties().accounts_registered_this_interval; i < accounts_per_scale; ++i ) + create_account("shill" + fc::to_string(i)); + generate_block(); + BOOST_CHECK_EQUAL(db.get_global_properties().parameters.current_fees->get().basic_fee, 16); + for( int i = 0; i < accounts_per_scale; ++i ) + create_account("moreshills" + fc::to_string(i)); + generate_block(); + BOOST_CHECK_EQUAL(db.get_global_properties().parameters.current_fees->get().basic_fee, 256); + for( int i = 0; i < accounts_per_scale; ++i ) + create_account("moarshills" + fc::to_string(i)); + generate_block(); + BOOST_CHECK_EQUAL(db.get_global_properties().parameters.current_fees->get().basic_fee, 4096); + + generate_blocks(db.get_dynamic_global_properties().next_maintenance_time); + BOOST_CHECK_EQUAL(db.get_global_properties().parameters.current_fees->get().basic_fee, 1); +} FC_LOG_AND_RETHROW() } + BOOST_AUTO_TEST_SUITE_END()