From 2e124b07c68b6ec435834b0cab69ed4f607490db Mon Sep 17 00:00:00 2001 From: Srdjan Obucina Date: Thu, 26 Dec 2019 06:31:21 +0100 Subject: [PATCH] Extend GPO.active_sons to contain votes and all public keys --- libraries/chain/db_maint.cpp | 54 ++++++++++++++++--- libraries/chain/db_witness_schedule.cpp | 5 +- .../graphene/chain/global_property_object.hpp | 3 +- .../chain/include/graphene/chain/son_info.hpp | 23 ++++++++ libraries/wallet/wallet.cpp | 11 +++- 5 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 libraries/chain/include/graphene/chain/son_info.hpp diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index 8fb72566..db8db162 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -445,20 +446,59 @@ void database::update_active_sons() } } ); + // Compare current and to-be lists of active sons + //const global_property_object& gpo = get_global_properties(); + auto cur_active_sons = gpo.active_sons; + vector new_active_sons; + for( const son_object& son : sons ) { + son_info swi; + swi.son_id = son.id; + swi.total_votes = son.total_votes; + swi.signing_key = son.signing_key; + swi.sidechain_public_keys = son.sidechain_public_keys; + new_active_sons.push_back(swi); + } + + bool son_sets_equal = (cur_active_sons.size() == new_active_sons.size()); + if (son_sets_equal) { + for( size_t i = 0; i < cur_active_sons.size(); i++ ) + { + son_sets_equal = son_sets_equal && + cur_active_sons.at(i).son_id == new_active_sons.at(i).son_id && + cur_active_sons.at(i).total_votes == new_active_sons.at(i).total_votes && + cur_active_sons.at(i).signing_key == new_active_sons.at(i).signing_key; + + if (cur_active_sons.at(i).sidechain_public_keys.size() == new_active_sons.at(i).sidechain_public_keys.size()) { + + } else { + son_sets_equal = false; + } + } + } + + if (son_sets_equal) { + ilog( "Active SONs set NOT CHANGED" ); + } else { + ilog( "Active SONs set CHANGED" ); + // Store new SON info, initiate wallet recreation and transfer of funds + } + modify(gpo, [&]( global_property_object& gp ){ gp.active_sons.clear(); - gp.active_sons.reserve(sons.size()); - std::transform(sons.begin(), sons.end(), - std::inserter(gp.active_sons, gp.active_sons.end()), - [](const son_object& s) { - return s.id; - }); + gp.active_sons.reserve(new_active_sons.size()); + gp.active_sons.insert(gp.active_sons.end(), new_active_sons.begin(), new_active_sons.end()); }); const son_schedule_object& sso = son_schedule_id_type()(*this); modify(sso, [&](son_schedule_object& _sso) { - flat_set active_sons(gpo.active_sons.begin(), gpo.active_sons.end()); + flat_set active_sons; + active_sons.reserve(gpo.active_sons.size()); + std::transform(gpo.active_sons.begin(), gpo.active_sons.end(), + std::inserter(active_sons, active_sons.end()), + [](const son_info& swi) { + return swi.son_id; + }); _sso.scheduler.update(active_sons); }); } FC_CAPTURE_AND_RETHROW() } diff --git a/libraries/chain/db_witness_schedule.cpp b/libraries/chain/db_witness_schedule.cpp index 3ce11443..31caad4b 100644 --- a/libraries/chain/db_witness_schedule.cpp +++ b/libraries/chain/db_witness_schedule.cpp @@ -27,6 +27,7 @@ #include #include #include +#include namespace graphene { namespace chain { @@ -200,8 +201,8 @@ void database::update_son_schedule() _sso.current_shuffled_sons.clear(); _sso.current_shuffled_sons.reserve( gpo.active_sons.size() ); - for( const son_id_type& w : gpo.active_sons ) - _sso.current_shuffled_sons.push_back( w ); + for( const son_info& w : gpo.active_sons ) + _sso.current_shuffled_sons.push_back( w.son_id ); auto now_hi = uint64_t(head_block_time().sec_since_epoch()) << 32; for( uint32_t i = 0; i < _sso.current_shuffled_sons.size(); ++i ) diff --git a/libraries/chain/include/graphene/chain/global_property_object.hpp b/libraries/chain/include/graphene/chain/global_property_object.hpp index 130648e9..1d985a2d 100644 --- a/libraries/chain/include/graphene/chain/global_property_object.hpp +++ b/libraries/chain/include/graphene/chain/global_property_object.hpp @@ -27,6 +27,7 @@ #include #include #include +#include #include namespace graphene { namespace chain { @@ -51,7 +52,7 @@ namespace graphene { namespace chain { uint32_t next_available_vote_id = 0; vector active_committee_members; // updated once per maintenance interval flat_set active_witnesses; // updated once per maintenance interval - vector active_sons; // updated once per maintenance interval + vector active_sons; // updated once per maintenance interval }; /** diff --git a/libraries/chain/include/graphene/chain/son_info.hpp b/libraries/chain/include/graphene/chain/son_info.hpp new file mode 100644 index 00000000..8200ffa2 --- /dev/null +++ b/libraries/chain/include/graphene/chain/son_info.hpp @@ -0,0 +1,23 @@ +#pragma once +#include +#include + +namespace graphene { namespace chain { + using namespace graphene::db; + + /** + * @class son_info + * @brief tracks information about a SON info required to re/create primary wallet + * @ingroup object + */ + struct son_info { + son_id_type son_id; + uint64_t total_votes = 0; + public_key_type signing_key; + flat_map sidechain_public_keys; + }; + +} } + +FC_REFLECT( graphene::chain::son_info, + (son_id)(total_votes)(signing_key)(sidechain_public_keys) ) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index e88f4a00..be837940 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -1942,7 +1942,14 @@ public: map list_active_sons() { try { global_property_object gpo = get_global_properties(); - std::vector> son_objects = _remote_db->get_sons(gpo.active_sons); + vector son_ids; + son_ids.reserve(gpo.active_sons.size()); + std::transform(gpo.active_sons.begin(), gpo.active_sons.end(), + std::inserter(son_ids, son_ids.end()), + [](const son_info& swi) { + return swi.son_id; + }); + std::vector> son_objects = _remote_db->get_sons(son_ids); vector owners; owners.resize(son_objects.size()); std::transform(son_objects.begin(), son_objects.end(), owners.begin(), @@ -1952,7 +1959,7 @@ public: }); vector> accs = _remote_db->get_accounts(owners); map result; - std::transform(accs.begin(), accs.end(), gpo.active_sons.begin(), + std::transform(accs.begin(), accs.end(), son_ids.begin(), std::inserter(result, result.end()), [](fc::optional& acct, son_id_type& sid) { FC_ASSERT(acct, "Invalid active SONs list in global properties.");