From 7319127142d84b3bce6d512f9f834dfe91fe7a8b Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Tue, 15 Mar 2022 15:53:38 +0300 Subject: [PATCH] #319 - add map for sidechain_vote_ids --- libraries/app/database_api.cpp | 12 ++-- libraries/chain/db_maint.cpp | 68 +++++++++---------- .../include/graphene/chain/son_object.hpp | 15 ++-- libraries/chain/son_evaluator.cpp | 4 +- libraries/wallet/wallet.cpp | 50 +++----------- 5 files changed, 58 insertions(+), 91 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index 16fbba99..8f4d0083 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -2192,7 +2192,7 @@ votes_info database_api_impl::get_votes(const string &account_name_or_id) const votes_for_sons.reserve(son_bitcoin_ids.size()); for (const auto &son : son_bitcoin_ids) { const auto &son_obj = son.as(6); - votes_for_sons.emplace_back(votes_info_object{son_obj.vote_id_bitcoin, son_obj.id}); + votes_for_sons.emplace_back(votes_info_object{son_obj.get_bitcoin_vote_id(), son_obj.id}); } result.votes_for_bitcoin_sons = std::move(votes_for_sons); } @@ -2202,7 +2202,7 @@ votes_info database_api_impl::get_votes(const string &account_name_or_id) const votes_for_sons.reserve(son_hive_ids.size()); for (const auto &son : son_hive_ids) { const auto &son_obj = son.as(6); - votes_for_sons.emplace_back(votes_info_object{son_obj.vote_id_hive, son_obj.id}); + votes_for_sons.emplace_back(votes_info_object{son_obj.get_hive_vote_id(), son_obj.id}); } result.votes_for_hive_sons = std::move(votes_for_sons); } @@ -2376,16 +2376,16 @@ voters_info database_api_impl::get_voters(const string &account_name_or_id) cons //! Info for son voters if (son_object) { - const auto &son_bitcoin_voters = get_voters_by_id(son_object->vote_id_bitcoin); + const auto &son_bitcoin_voters = get_voters_by_id(son_object->get_bitcoin_vote_id()); voters_info_object voters_for_bitcoin_son; - voters_for_bitcoin_son.vote_id = son_object->vote_id_bitcoin; + voters_for_bitcoin_son.vote_id = son_object->get_bitcoin_vote_id(); voters_for_bitcoin_son.voters.reserve(son_bitcoin_voters.size()); for (const auto &voter : son_bitcoin_voters) { voters_for_bitcoin_son.voters.emplace_back(voter.get_id()); } - const auto &son_hive_voters = get_voters_by_id(son_object->vote_id_hive); + const auto &son_hive_voters = get_voters_by_id(son_object->get_hive_vote_id()); voters_info_object voters_for_hive_son; - voters_for_hive_son.vote_id = son_object->vote_id_hive; + voters_for_hive_son.vote_id = son_object->get_hive_vote_id(); voters_for_hive_son.voters.reserve(son_hive_voters.size()); for (const auto &voter : son_hive_voters) { voters_for_hive_son.voters.emplace_back(voter.get_id()); diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index c1e2287d..ff128259 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -92,33 +92,16 @@ vector> database::sort_votable_objects< count = std::min(count, refs.size()); std::partial_sort(refs.begin(), refs.begin() + count, refs.end(), [this, sidechain](const son_object& a, const son_object& b)->bool { - share_type oa_vote = 0; - share_type ob_vote = 0; - switch (sidechain) { - case sidechain_type::bitcoin: - oa_vote = _vote_tally_buffer[a.vote_id_bitcoin]; - ob_vote = _vote_tally_buffer[b.vote_id_bitcoin]; - break; - case sidechain_type::hive: - oa_vote = _vote_tally_buffer[a.vote_id_hive]; - ob_vote = _vote_tally_buffer[b.vote_id_hive]; - break; - default: - FC_THROW("Unexpected sidechain type"); - }; + if(sidechain != sidechain_type::bitcoin && sidechain != sidechain_type::hive) + FC_THROW("Unexpected sidechain type"); + + const share_type oa_vote = _vote_tally_buffer[a.get_sidechain_vote_id(sidechain)]; + const share_type ob_vote = _vote_tally_buffer[b.get_sidechain_vote_id(sidechain)]; if( oa_vote != ob_vote ) return oa_vote > ob_vote; - switch (sidechain) { - case sidechain_type::bitcoin: - return a.vote_id_bitcoin < b.vote_id_bitcoin; - case sidechain_type::hive: - return a.vote_id_hive < b.vote_id_hive; - default: - FC_THROW("Unexpected sidechain type"); - }; - return 0; + return a.get_sidechain_vote_id(sidechain) < b.get_sidechain_vote_id(sidechain); }); refs.resize(count, refs.front()); @@ -209,8 +192,10 @@ void database::pay_sons() uint64_t total_votes = 0; for( const son_object& son : sons ) { - total_votes += _vote_tally_buffer[son.vote_id_bitcoin]; - total_votes += _vote_tally_buffer[son.vote_id_hive]; + for(const auto& vote_id : son.sidechain_vote_ids) + { + total_votes += _vote_tally_buffer[vote_id.second]; + } } int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0); auto get_weight = [&bits_to_drop]( uint64_t son_votes ) { @@ -229,11 +214,16 @@ void database::pay_sons() const son_statistics_object& s = static_cast(o); const auto& idx = get_index_type().indices().get(); auto son_obj = idx.find( s.owner ); - auto son_weight = get_weight(_vote_tally_buffer[son_obj->vote_id_bitcoin]) + - get_weight(_vote_tally_buffer[son_obj->vote_id_hive]); - if( now < HARDFORK_SON2_TIME ) { - son_weight = get_weight_before_son2_hf(_vote_tally_buffer[son_obj->vote_id_bitcoin]) + - get_weight_before_son2_hf(_vote_tally_buffer[son_obj->vote_id_hive]); + uint16_t son_weight = 0; + if( now >= HARDFORK_SON2_TIME ) { + for (const auto& vote_id : son_obj->sidechain_vote_ids) { + son_weight += get_weight(_vote_tally_buffer[vote_id.second]); + } + } + else { + for (const auto& vote_id : son_obj->sidechain_vote_ids) { + son_weight += get_weight_before_son2_hf(_vote_tally_buffer[vote_id.second]); + } } uint64_t txs_signed = 0; for (const auto &ts : s.txs_signed) { @@ -253,11 +243,16 @@ void database::pay_sons() if(txs_signed > 0){ const auto& idx = get_index_type().indices().get(); auto son_obj = idx.find( s.owner ); - auto son_weight = get_weight(_vote_tally_buffer[son_obj->vote_id_bitcoin]) + - get_weight(_vote_tally_buffer[son_obj->vote_id_hive]); - if( now < HARDFORK_SON2_TIME ) { - son_weight = get_weight_before_son2_hf(_vote_tally_buffer[son_obj->vote_id_bitcoin]) + - get_weight_before_son2_hf(_vote_tally_buffer[son_obj->vote_id_hive]); + uint16_t son_weight = 0; + if( now >= HARDFORK_SON2_TIME ) { + for (const auto& vote_id : son_obj->sidechain_vote_ids) { + son_weight += get_weight(_vote_tally_buffer[vote_id.second]); + } + } + else { + for (const auto& vote_id : son_obj->sidechain_vote_ids) { + son_weight += get_weight_before_son2_hf(_vote_tally_buffer[vote_id.second]); + } } share_type pay = (txs_signed * son_weight * son_budget.value)/weighted_total_txs_signed; modify( *son_obj, [&]( son_object& _son_obj) @@ -722,7 +717,8 @@ void database::update_active_sons() }); } modify( son, [local_vote_buffer_ref]( son_object& obj ){ - obj.total_votes = local_vote_buffer_ref[obj.vote_id_bitcoin]; + //! FIXME - only bitcoin_vote_id ??? + obj.total_votes = local_vote_buffer_ref[obj.get_bitcoin_vote_id()]; if(obj.status == son_status::request_maintenance) obj.status = son_status::in_maintenance; }); diff --git a/libraries/chain/include/graphene/chain/son_object.hpp b/libraries/chain/include/graphene/chain/son_object.hpp index 0ec7a43b..65ef0a2a 100644 --- a/libraries/chain/include/graphene/chain/son_object.hpp +++ b/libraries/chain/include/graphene/chain/son_object.hpp @@ -64,8 +64,7 @@ namespace graphene { namespace chain { static const uint8_t type_id = son_object_type; account_id_type son_account; - vote_id_type vote_id_bitcoin; - vote_id_type vote_id_hive; + flat_map sidechain_vote_ids; uint64_t total_votes = 0; string url; vesting_balance_id_type deposit; @@ -79,8 +78,9 @@ namespace graphene { namespace chain { bool has_valid_config()const; bool has_valid_config(time_point_sec head_block_time)const; - inline vote_id_type get_vote_id_bitcoin() const { return vote_id_bitcoin; } - inline vote_id_type get_vote_id_hive() const { return vote_id_hive; } + inline vote_id_type get_sidechain_vote_id(sidechain_type sidechain) const { return sidechain_vote_ids.at(sidechain); } + inline vote_id_type get_bitcoin_vote_id() const { return get_sidechain_vote_id(sidechain_type::bitcoin); } + inline vote_id_type get_hive_vote_id() const { return get_sidechain_vote_id(sidechain_type::hive); } }; struct by_account; @@ -96,10 +96,10 @@ namespace graphene { namespace chain { member >, ordered_unique< tag, - const_mem_fun + const_mem_fun >, ordered_unique< tag, - const_mem_fun + const_mem_fun > > >; @@ -125,8 +125,7 @@ FC_REFLECT_ENUM(graphene::chain::son_status, (inactive)(active)(request_maintena FC_REFLECT_DERIVED( graphene::chain::son_object, (graphene::db::object), (son_account) - (vote_id_bitcoin) - (vote_id_hive) + (sidechain_vote_ids) (total_votes) (url) (deposit) diff --git a/libraries/chain/son_evaluator.cpp b/libraries/chain/son_evaluator.cpp index 2e175662..255bd8eb 100644 --- a/libraries/chain/son_evaluator.cpp +++ b/libraries/chain/son_evaluator.cpp @@ -47,8 +47,8 @@ object_id_type create_son_evaluator::do_apply(const son_create_operation& op) const auto& new_son_object = db().create( [&]( son_object& obj ){ obj.son_account = op.owner_account; - obj.vote_id_bitcoin = vote_id_bitcoin; - obj.vote_id_hive = vote_id_hive; + obj.sidechain_vote_ids[sidechain_type::bitcoin] = vote_id_bitcoin; + obj.sidechain_vote_ids[sidechain_type::hive] = vote_id_hive; obj.url = op.url; obj.deposit = op.deposit; obj.signing_key = op.signing_key; diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 7f22accd..caf1e5be 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -2759,28 +2759,18 @@ public: fc::optional son_obj = _remote_db->get_son_by_account_id(son_account_id); if (!son_obj) FC_THROW("Account ${son} is not registered as a son", ("son", son)); - - vote_id_type sidechain_vote_id; - switch (sidechain) { - case sidechain_type::bitcoin: - sidechain_vote_id = son_obj->vote_id_bitcoin; - break; - case sidechain_type::hive: - sidechain_vote_id = son_obj->vote_id_hive; - break; - default: - FC_THROW("Invalid sidechain type"); - }; + if(sidechain != sidechain_type::bitcoin && sidechain != sidechain_type::hive) + FC_THROW("Unexpected sidechain type"); if (approve) { - auto insert_result = voting_account_object.options.votes.insert(sidechain_vote_id); + auto insert_result = voting_account_object.options.votes.insert(son_obj->get_sidechain_vote_id(sidechain)); if (!insert_result.second) FC_THROW("Account ${account} has already voted for son ${son} for sidechain ${sidechain}", ("account", voting_account)("son", son)("sidechain", sidechain)); } else { - unsigned votes_removed = voting_account_object.options.votes.erase(sidechain_vote_id); + unsigned votes_removed = voting_account_object.options.votes.erase(son_obj->get_sidechain_vote_id(sidechain)); if (!votes_removed) FC_THROW("Account ${account} has already unvoted for son ${son} for sidechain ${sidechain}", ("account", voting_account)("son", son)("sidechain", sidechain)); } @@ -2816,19 +2806,10 @@ public: account_id_type son_owner_account_id = get_account_id(son); fc::optional son_obj = _remote_db->get_son_by_account_id(son_owner_account_id); if (!son_obj) - FC_THROW("Account ${son} is not registered as a SON", ("son", son)); - vote_id_type sidechain_vote_id; - switch (sidechain) { - case sidechain_type::bitcoin: - sidechain_vote_id = son_obj->vote_id_bitcoin; - break; - case sidechain_type::hive: - sidechain_vote_id = son_obj->vote_id_hive; - break; - default: - FC_THROW("Invalid sidechain type"); - }; - auto insert_result = voting_account_object.options.votes.insert(sidechain_vote_id); + FC_THROW("Account ${son} is not registered as a SON", ("son", son)); + if(sidechain != sidechain_type::bitcoin && sidechain != sidechain_type::hive) + FC_THROW("Unexpected sidechain type"); + auto insert_result = voting_account_object.options.votes.insert(son_obj->get_sidechain_vote_id(sidechain)); if (!insert_result.second) FC_THROW("Account ${account} was already voting for SON ${son}", ("account", voting_account)("son", son)); } @@ -2838,18 +2819,9 @@ public: fc::optional son_obj = _remote_db->get_son_by_account_id(son_owner_account_id); if (!son_obj) FC_THROW("Account ${son} is not registered as a SON", ("son", son)); - vote_id_type sidechain_vote_id; - switch (sidechain) { - case sidechain_type::bitcoin: - sidechain_vote_id = son_obj->vote_id_bitcoin; - break; - case sidechain_type::hive: - sidechain_vote_id = son_obj->vote_id_hive; - break; - default: - FC_THROW("Invalid sidechain type"); - }; - unsigned votes_removed = voting_account_object.options.votes.erase(sidechain_vote_id); + if(sidechain != sidechain_type::bitcoin && sidechain != sidechain_type::hive) + FC_THROW("Unexpected sidechain type"); + unsigned votes_removed = voting_account_object.options.votes.erase(son_obj->get_sidechain_vote_id(sidechain)); if (!votes_removed) FC_THROW("Account ${account} is already not voting for SON ${son}", ("account", voting_account)("son", son)); }