From ee9545a2c464ee0cf77558a46f9570e956e060ff Mon Sep 17 00:00:00 2001 From: kstdl Date: Tue, 12 Dec 2017 15:37:58 +0300 Subject: [PATCH] fix reindex on peerplays network --- libraries/app/database_api.cpp | 29 ++++++++++++++++++- .../app/include/graphene/app/database_api.hpp | 7 ++++- libraries/chain/asset_evaluator.cpp | 8 ++--- libraries/chain/asset_object.cpp | 12 ++++---- libraries/chain/db_balance.cpp | 5 ++-- .../graphene/chain/protocol/asset_ops.hpp | 4 +-- .../chain/protocol/chain_parameters.hpp | 20 +++++++++---- libraries/chain/lottery_evaluator.cpp | 2 +- libraries/chain/protocol/asset_ops.cpp | 2 +- libraries/wallet/wallet.cpp | 2 +- tests/common/database_fixture.cpp | 2 +- tests/tests/lottery_tests.cpp | 2 +- 12 files changed, 69 insertions(+), 26 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index 05679f9f..e18e61f7 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -119,7 +119,8 @@ class database_api_impl : public std::enable_shared_from_this unsigned limit = 100, asset_id_type start = asset_id_type() )const; asset get_lottery_balance( asset_id_type lottery_id )const; - + sweeps_vesting_balance_object get_sweeps_vesting_balance_object( account_id_type account )const; + asset get_sweeps_vesting_balance_available_for_claim( account_id_type account )const; // Markets / feeds vector get_limit_orders(asset_id_type a, asset_id_type b, uint32_t limit)const; @@ -1036,6 +1037,7 @@ vector database_api_impl::get_lotteries(asset_id_type stop, asset_id_type start )const { vector result; + if( limit > 100 ) limit = 100; const auto& assets = _db.get_index_type().indices().get(); const auto range = assets.equal_range( boost::make_tuple( true ) ); @@ -1062,6 +1064,31 @@ asset database_api_impl::get_lottery_balance( asset_id_type lottery_id )const return _db.get_balance( lottery_id ); } +sweeps_vesting_balance_object database_api::get_sweeps_vesting_balance_object( account_id_type account )const +{ + return my->get_sweeps_vesting_balance_object( account ); +} + +sweeps_vesting_balance_object database_api_impl::get_sweeps_vesting_balance_object( account_id_type account )const +{ + const auto& vesting_idx = _db.get_index_type().indices().get(); + auto account_balance = vesting_idx.find(account); + FC_ASSERT( account_balance != vesting_idx.end(), "NO SWEEPS VESTING BALANCE" ); + return *account_balance; +} + +asset database_api::get_sweeps_vesting_balance_available_for_claim( account_id_type account )const +{ + return my->get_sweeps_vesting_balance_available_for_claim( account ); +} + +asset database_api_impl::get_sweeps_vesting_balance_available_for_claim( account_id_type account )const +{ + const auto& vesting_idx = _db.get_index_type().indices().get(); + auto account_balance = vesting_idx.find(account); + FC_ASSERT( account_balance != vesting_idx.end(), "NO SWEEPS VESTING BALANCE" ); + return account_balance->available_for_claim(); +} ////////////////////////////////////////////////////////////////////// // Peerplays // diff --git a/libraries/app/include/graphene/app/database_api.hpp b/libraries/app/include/graphene/app/database_api.hpp index 689f9532..ca02180e 100644 --- a/libraries/app/include/graphene/app/database_api.hpp +++ b/libraries/app/include/graphene/app/database_api.hpp @@ -359,6 +359,9 @@ class database_api vector get_lotteries( asset_id_type stop = asset_id_type(), unsigned limit = 100, asset_id_type start = asset_id_type() )const; + + sweeps_vesting_balance_object get_sweeps_vesting_balance_object( account_id_type account )const; + asset get_sweeps_vesting_balance_available_for_claim( account_id_type account )const; /** * @brief Get balance of lottery assets */ @@ -750,9 +753,11 @@ FC_API(graphene::app::database_api, (get_unmatched_bets_for_bettor) (get_all_unmatched_bets_for_bettor) - // Lottery assets + // Sweeps (get_lotteries) (get_lottery_balance) + (get_sweeps_vesting_balance_object) + (get_sweeps_vesting_balance_available_for_claim) // Markets / feeds (get_order_book) diff --git a/libraries/chain/asset_evaluator.cpp b/libraries/chain/asset_evaluator.cpp index 6448dd12..4a3a2b1c 100644 --- a/libraries/chain/asset_evaluator.cpp +++ b/libraries/chain/asset_evaluator.cpp @@ -118,9 +118,9 @@ void_result asset_create_evaluator::do_evaluate( const asset_create_operation& o FC_ASSERT( op.precision == op.bitasset_opts->short_backing_asset(d).precision ); } - if( op.extension.which() == asset_extension::tag::value ) { + if( op.extensions.which() == asset_extension::tag::value ) { FC_ASSERT( op.common_options.max_supply >= 5 ); - op.extension.get().validate(); + op.extensions.get().validate(); } return void_result(); } FC_CAPTURE_AND_RETHROW( (op) ) } @@ -156,9 +156,9 @@ object_id_type asset_create_evaluator::do_apply( const asset_create_operation& o a.symbol = op.symbol; a.precision = op.precision; a.options = op.common_options; - if( op.extension.which() == asset_extension::tag::value ) { + if( op.extensions.which() == asset_extension::tag::value ) { a.precision = 0; - a.lottery_options = op.extension.get(); //a.lottery_options->balance = asset( 0, a.lottery_options->ticket_price.asset_id ); + a.lottery_options = op.extensions.get(); //a.lottery_options->balance = asset( 0, a.lottery_options->ticket_price.asset_id ); a.lottery_options->owner = a.id; db().create([&](lottery_balance_object& lbo) { lbo.lottery_id = a.id; diff --git a/libraries/chain/asset_object.cpp b/libraries/chain/asset_object.cpp index 7545f6d9..26080c03 100644 --- a/libraries/chain/asset_object.cpp +++ b/libraries/chain/asset_object.cpp @@ -223,14 +223,14 @@ map< account_id_type, vector< uint16_t > > asset_object::distribute_winners_part for( auto t = tickets.begin(); t != tickets.begin() + holders.size(); ++t ) *t += percents_to_distribute / holders.size(); } - + auto sweeps_distribution_percentage = db.get_global_properties().parameters.extensions.get().sweeps_distribution_percentage; for( int c = 0; c < winner_numbers.size(); ++c ) { auto winner_num = winner_numbers[c]; lottery_reward_operation reward_op; reward_op.lottery = get_id(); reward_op.winner = holders[winner_num]; reward_op.win_percentage = tickets[c]; - reward_op.amount = asset( jackpot * tickets[c] * ( 1. - db.get_global_properties().parameters.sweeps_distribution_percentage / (double)GRAPHENE_100_PERCENT ) / GRAPHENE_100_PERCENT , db.get_balance(id).asset_id ); + reward_op.amount = asset( jackpot * tickets[c] * ( 1. - sweeps_distribution_percentage / (double)GRAPHENE_100_PERCENT ) / GRAPHENE_100_PERCENT , db.get_balance(id).asset_id ); db.apply_operation(eval, reward_op); structurized_participants[ holders[ winner_num ] ].push_back( tickets[c] ); @@ -244,9 +244,9 @@ void asset_object::distribute_sweeps_holders_part( database& db ) auto& asset_bal_idx = db.get_index_type< account_balance_index >().indices().get< by_asset_balance >(); - auto global_params = db.get_global_properties().parameters; - uint64_t distribution_asset_supply = global_params.sweeps_distribution_asset( db ).dynamic_data( db ).current_supply.value; - const auto range = asset_bal_idx.equal_range( boost::make_tuple( global_params.sweeps_distribution_asset ) ); + auto sweeps_params = db.get_global_properties().parameters.extensions.get(); + uint64_t distribution_asset_supply = sweeps_params.sweeps_distribution_asset( db ).dynamic_data( db ).current_supply.value; + const auto range = asset_bal_idx.equal_range( boost::make_tuple( sweeps_params.sweeps_distribution_asset ) ); uint64_t holders_sum = 0; for( const account_balance_object& holder_balance : boost::make_iterator_range( range.first, range.second ) ) @@ -256,7 +256,7 @@ void asset_object::distribute_sweeps_holders_part( database& db ) holders_sum += holder_part; } uint64_t balance_rest = db.get_balance( get_id() ).amount.value * SWEEPS_VESTING_BALANCE_MULTIPLIER - holders_sum; - db.adjust_sweeps_vesting_balance( SWEEPS_ACCUMULATOR_ACCOUNT, balance_rest ); + db.adjust_sweeps_vesting_balance( sweeps_params.sweeps_vesting_accumulator_account, balance_rest ); db.adjust_balance( get_id(), -db.get_balance( get_id() ) ); } diff --git a/libraries/chain/db_balance.cpp b/libraries/chain/db_balance.cpp index 6cf88681..f3ca7ac8 100644 --- a/libraries/chain/db_balance.cpp +++ b/libraries/chain/db_balance.cpp @@ -121,7 +121,7 @@ void database::adjust_sweeps_vesting_balance(account_id_type account, int64_t de if( delta == 0 ) return; - asset_id_type asset_id = get_global_properties().parameters.sweeps_distribution_asset; + asset_id_type asset_id = get_global_properties().parameters.extensions.get().sweeps_distribution_asset; auto& index = get_index_type().indices().get(); auto itr = index.find(account); @@ -139,8 +139,9 @@ void database::adjust_sweeps_vesting_balance(account_id_type account, int64_t de } else { if( delta < 0 ) FC_ASSERT( itr->get_balance() >= -delta, "Insufficient Balance: ${a}'s balance of ${b} is less than required ${r}", ("a",account)("b",itr->get_balance())("r",-delta)); - modify(*itr, [&delta,&asset_id](sweeps_vesting_balance_object& b) { + modify(*itr, [&delta,&asset_id,this](sweeps_vesting_balance_object& b) { b.adjust_balance( asset( delta, asset_id ) ); + b.last_claim_date = head_block_time(); }); } } diff --git a/libraries/chain/include/graphene/chain/protocol/asset_ops.hpp b/libraries/chain/include/graphene/chain/protocol/asset_ops.hpp index 60e1390b..1639d4cd 100644 --- a/libraries/chain/include/graphene/chain/protocol/asset_ops.hpp +++ b/libraries/chain/include/graphene/chain/protocol/asset_ops.hpp @@ -219,7 +219,7 @@ namespace graphene { namespace chain { /// For BitAssets, set this to true if the asset implements a @ref prediction_market; false otherwise bool is_prediction_market = false; // containing lottery_asset_options now - asset_extension extension; + asset_extension extensions; account_id_type fee_payer()const { return issuer; } void validate()const; @@ -683,7 +683,7 @@ FC_REFLECT( graphene::chain::asset_create_operation, (common_options) (bitasset_opts) (is_prediction_market) - (extension) + (extensions) ) FC_REFLECT( graphene::chain::asset_update_operation, (fee) diff --git a/libraries/chain/include/graphene/chain/protocol/chain_parameters.hpp b/libraries/chain/include/graphene/chain/protocol/chain_parameters.hpp index b4293bcc..8814eab5 100644 --- a/libraries/chain/include/graphene/chain/protocol/chain_parameters.hpp +++ b/libraries/chain/include/graphene/chain/protocol/chain_parameters.hpp @@ -32,6 +32,7 @@ namespace graphene { namespace chain { struct fee_schedule; } } namespace graphene { namespace chain { +<<<<<<< HEAD struct parameter_extension { optional< bet_multiplier_type > min_bet_multiplier; @@ -46,6 +47,14 @@ namespace graphene { namespace chain { optional < uint16_t > son_count; }; +======= + struct sweeps_parameters_extension { + uint16_t sweeps_distribution_percentage = SWEEPS_DEFAULT_DISTRIBUTION_PERCENTAGE; + asset_id_type sweeps_distribution_asset = SWEEPS_DEFAULT_DISTRIBUTION_ASSET; + account_id_type sweeps_vesting_accumulator_account = SWEEPS_ACCUMULATOR_ACCOUNT; + }; + typedef static_variant parameter_extension; +>>>>>>> fix reindex on peerplays network struct chain_parameters { /** using a smart ref breaks the circular dependency created between operations and the fee schedule */ @@ -96,9 +105,8 @@ namespace graphene { namespace chain { // uint16_t sweeps_distribution_percentage = SWEEPS_DEFAULT_DISTRIBUTION_PERCENTAGE; asset_id_type sweeps_distribution_asset = SWEEPS_DEFAULT_DISTRIBUTION_ASSET; - - - extension extensions; + + extension extensions = sweeps_parameters_extension(); /** defined in fee_schedule.cpp */ void validate()const; @@ -146,6 +154,10 @@ FC_REFLECT( graphene::chain::parameter_extension, (son_count) ) +FC_REFLECT( graphene::chain::sweeps_parameters_extension, + (sweeps_distribution_percentage) + (sweeps_distribution_asset) ) + FC_REFLECT( graphene::chain::chain_parameters, (current_fees) (block_interval) @@ -189,7 +201,5 @@ FC_REFLECT( graphene::chain::chain_parameters, (maximum_tournament_start_time_in_future) (maximum_tournament_start_delay) (maximum_tournament_number_of_wins) - (sweeps_distribution_percentage) - (sweeps_distribution_asset) (extensions) ) diff --git a/libraries/chain/lottery_evaluator.cpp b/libraries/chain/lottery_evaluator.cpp index 67f16557..eadedbb3 100644 --- a/libraries/chain/lottery_evaluator.cpp +++ b/libraries/chain/lottery_evaluator.cpp @@ -30,7 +30,7 @@ #include #include -#include +#include #include diff --git a/libraries/chain/protocol/asset_ops.cpp b/libraries/chain/protocol/asset_ops.cpp index 02ee278d..683e9d89 100644 --- a/libraries/chain/protocol/asset_ops.cpp +++ b/libraries/chain/protocol/asset_ops.cpp @@ -79,7 +79,7 @@ share_type asset_create_operation::calculate_fee(const asset_create_operation::f { auto core_fee_required = param.long_symbol; - if( extension.which() == asset_extension::tag::value ) { + if( extensions.which() == asset_extension::tag::value ) { core_fee_required = param.lottery_asset; } else { switch(symbol.size()) { diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index c913bbaa..587cb9b9 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -1460,7 +1460,7 @@ public: create_op.precision = 0; create_op.common_options = common; - create_op.extension = lottery_opts; + create_op.extensions = lottery_opts; signed_transaction tx; tx.operations.push_back( create_op ); diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index ca8c9c3e..5a04a98b 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -250,7 +250,7 @@ void database_fixture::verify_asset_supplies( const database& db ) for( const sweeps_vesting_balance_object& svbo: db.get_index_type< sweeps_vesting_balance_index >().indices() ) sweeps_vestings += svbo.balance; - total_balances[db.get_global_properties().parameters.sweeps_distribution_asset] += sweeps_vestings / SWEEPS_VESTING_BALANCE_MULTIPLIER; + total_balances[db.get_global_properties().parameters.extensions.get().sweeps_distribution_asset] += sweeps_vestings / SWEEPS_VESTING_BALANCE_MULTIPLIER; total_balances[asset_id_type()] += db.get_dynamic_global_properties().witness_budget; for( const auto& item : total_debts ) diff --git a/tests/tests/lottery_tests.cpp b/tests/tests/lottery_tests.cpp index 19350772..624502fd 100644 --- a/tests/tests/lottery_tests.cpp +++ b/tests/tests/lottery_tests.cpp @@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE( create_lottery_asset_test ) lottery_options.is_active = test_asset_id.instance.value % 2; lottery_options.ending_on_soldout = true; - creator.extension = lottery_options; + creator.extensions = lottery_options; trx.operations.push_back(std::move(creator)); PUSH_TX( db, trx, ~0 );