From dc9476b2d31f5448106b11a597ec963671461e19 Mon Sep 17 00:00:00 2001 From: Srdjan Obucina Date: Thu, 26 Mar 2020 22:56:56 +0100 Subject: [PATCH] Mirror SON owner account weights from son-account.active.account_auths to active SONs --- libraries/chain/db_maint.cpp | 4 ++- .../include/graphene/chain/sidechain_defs.hpp | 2 ++ .../chain/include/graphene/chain/son_info.hpp | 9 ++++--- .../chain/son_wallet_deposit_object.hpp | 23 +++++++--------- .../chain/son_wallet_withdraw_object.hpp | 21 ++++++--------- .../chain/son_wallet_deposit_evaluator.cpp | 26 ++++++++++++++++--- .../chain/son_wallet_withdraw_evaluator.cpp | 26 ++++++++++++++++--- .../peerplays_sidechain_plugin.cpp | 2 +- .../sidechain_net_handler.cpp | 8 +++--- tests/tests/son_wallet_tests.cpp | 4 +-- 10 files changed, 81 insertions(+), 44 deletions(-) diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index 498a61ac..2df55da1 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -616,10 +616,12 @@ void database::update_active_sons() // Compare current and to-be lists of active sons auto cur_active_sons = gpo.active_sons; vector new_active_sons; + const auto &acc = get(gpo.parameters.son_account()); + assert(acc != acc_idx.end()); for( const son_object& son : sons ) { son_info swi; swi.son_id = son.id; - swi.total_votes = son.total_votes; + swi.weight = acc.active.account_auths.at(son.son_account); swi.signing_key = son.signing_key; swi.sidechain_public_keys = son.sidechain_public_keys; new_active_sons.push_back(swi); diff --git a/libraries/chain/include/graphene/chain/sidechain_defs.hpp b/libraries/chain/include/graphene/chain/sidechain_defs.hpp index f51b9eaf..43e76e2d 100644 --- a/libraries/chain/include/graphene/chain/sidechain_defs.hpp +++ b/libraries/chain/include/graphene/chain/sidechain_defs.hpp @@ -5,6 +5,7 @@ namespace graphene { namespace chain { enum class sidechain_type { + unknown, bitcoin, ethereum, eos, @@ -20,6 +21,7 @@ using sidechain_type = graphene::chain::sidechain_type; } } FC_REFLECT_ENUM(graphene::chain::sidechain_type, + (unknown) (bitcoin) (ethereum) (eos) diff --git a/libraries/chain/include/graphene/chain/son_info.hpp b/libraries/chain/include/graphene/chain/son_info.hpp index bc0c823b..37d54e74 100644 --- a/libraries/chain/include/graphene/chain/son_info.hpp +++ b/libraries/chain/include/graphene/chain/son_info.hpp @@ -12,14 +12,14 @@ namespace graphene { namespace chain { */ struct son_info { son_id_type son_id; - uint64_t total_votes = 0; + weight_type weight = 0; public_key_type signing_key; flat_map sidechain_public_keys; bool operator==(const son_info& rhs) { bool son_sets_equal = (son_id == rhs.son_id) && - (total_votes == rhs.total_votes) && + (weight == rhs.weight) && (signing_key == rhs.signing_key) && (sidechain_public_keys.size() == rhs.sidechain_public_keys.size()); @@ -33,4 +33,7 @@ namespace graphene { namespace chain { } } FC_REFLECT( graphene::chain::son_info, - (son_id)(total_votes)(signing_key)(sidechain_public_keys) ) + (son_id) + (weight) + (signing_key) + (sidechain_public_keys) ) diff --git a/libraries/chain/include/graphene/chain/son_wallet_deposit_object.hpp b/libraries/chain/include/graphene/chain/son_wallet_deposit_object.hpp index 6ddc44c2..80c749d2 100644 --- a/libraries/chain/include/graphene/chain/son_wallet_deposit_object.hpp +++ b/libraries/chain/include/graphene/chain/son_wallet_deposit_object.hpp @@ -18,7 +18,7 @@ namespace graphene { namespace chain { static const uint8_t type_id = son_wallet_deposit_object_type; time_point_sec timestamp; - sidechain_type sidechain; + sidechain_type sidechain = sidechain_type::unknown; std::string sidechain_uid; std::string sidechain_transaction_id; std::string sidechain_from; @@ -29,34 +29,29 @@ namespace graphene { namespace chain { chain::account_id_type peerplays_to; chain::asset peerplays_asset; - std::set expected_reports; + std::map expected_reports; std::set received_reports; - bool processed; + bool confirmed = false; + + bool processed = false; }; - struct by_sidechain; struct by_sidechain_uid; - struct by_processed; - struct by_sidechain_and_processed; + struct by_sidechain_and_confirmed_and_processed; using son_wallet_deposit_multi_index_type = multi_index_container< son_wallet_deposit_object, indexed_by< ordered_unique< tag, member >, - ordered_non_unique< tag, - member - >, ordered_unique< tag, member >, - ordered_non_unique< tag, - member - >, - ordered_non_unique< tag, + ordered_non_unique< tag, composite_key, + member, member > > @@ -70,4 +65,4 @@ FC_REFLECT_DERIVED( graphene::chain::son_wallet_deposit_object, (graphene::db::o (sidechain_uid) (sidechain_transaction_id) (sidechain_from) (sidechain_to) (sidechain_currency) (sidechain_amount) (peerplays_from) (peerplays_to) (peerplays_asset) (expected_reports) (received_reports) - (processed) ) + (confirmed) (processed) ) diff --git a/libraries/chain/include/graphene/chain/son_wallet_withdraw_object.hpp b/libraries/chain/include/graphene/chain/son_wallet_withdraw_object.hpp index ef39aadf..033adc07 100644 --- a/libraries/chain/include/graphene/chain/son_wallet_withdraw_object.hpp +++ b/libraries/chain/include/graphene/chain/son_wallet_withdraw_object.hpp @@ -18,7 +18,7 @@ namespace graphene { namespace chain { static const uint8_t type_id = son_wallet_withdraw_object_type; time_point_sec timestamp; - sidechain_type sidechain; + sidechain_type sidechain = sidechain_type::unknown; std::string peerplays_uid; std::string peerplays_transaction_id; chain::account_id_type peerplays_from; @@ -28,16 +28,16 @@ namespace graphene { namespace chain { std::string withdraw_currency; safe withdraw_amount; - std::set expected_reports; + std::map expected_reports; std::set received_reports; - bool processed; + bool confirmed = false; + + bool processed = false; }; struct by_peerplays_uid; - struct by_withdraw_sidechain; - struct by_processed; - struct by_withdraw_sidechain_and_processed; + struct by_withdraw_sidechain_and_confirmed_and_processed; using son_wallet_withdraw_multi_index_type = multi_index_container< son_wallet_withdraw_object, indexed_by< @@ -47,15 +47,10 @@ namespace graphene { namespace chain { ordered_unique< tag, member >, - ordered_non_unique< tag, - member - >, - ordered_non_unique< tag, - member - >, - ordered_non_unique< tag, + ordered_non_unique< tag, composite_key, + member, member > > diff --git a/libraries/chain/son_wallet_deposit_evaluator.cpp b/libraries/chain/son_wallet_deposit_evaluator.cpp index 18c21560..8f0978fa 100644 --- a/libraries/chain/son_wallet_deposit_evaluator.cpp +++ b/libraries/chain/son_wallet_deposit_evaluator.cpp @@ -25,8 +25,8 @@ void_result create_son_wallet_deposit_evaluator::do_evaluate(const son_wallet_de FC_ASSERT(expected, "Only active SON can create deposit"); } else { bool expected = false; - for (auto &son_id : swdo->expected_reports) { - if (op.son_id == son_id) { + for (auto &exp : swdo->expected_reports) { + if (op.son_id == exp.first) { expected = true; break; } @@ -65,7 +65,7 @@ object_id_type create_son_wallet_deposit_evaluator::do_apply(const son_wallet_de auto &gpo = db().get_global_properties(); for (auto &si : gpo.active_sons) { - swdo.expected_reports.insert(si.son_id); + swdo.expected_reports.insert(std::make_pair(si.son_id, si.weight)); auto stats_itr = db().get_index_type().indices().get().find(si.son_id); db().modify(*stats_itr, [&op, &si](son_statistics_object &sso) { @@ -78,12 +78,32 @@ object_id_type create_son_wallet_deposit_evaluator::do_apply(const son_wallet_de swdo.received_reports.insert(op.son_id); + uint64_t total_weight = 0; + for (const auto exp : swdo.expected_reports) { + total_weight = total_weight + exp.second; + } + uint64_t current_weight = 0; + for (const auto rec : swdo.received_reports) { + current_weight = current_weight + swdo.expected_reports.find(rec)->second; + } + swdo.confirmed = (current_weight > (total_weight * 2 / 3)); + swdo.processed = false; }); return new_son_wallet_deposit_object.id; } else { db().modify(*itr, [&op](son_wallet_deposit_object &swdo) { swdo.received_reports.insert(op.son_id); + + uint64_t total_weight = 0; + for (const auto exp : swdo.expected_reports) { + total_weight = total_weight + exp.second; + } + uint64_t current_weight = 0; + for (const auto rec : swdo.received_reports) { + current_weight = current_weight + swdo.expected_reports.find(rec)->second; + } + swdo.confirmed = (current_weight > (total_weight * 2 / 3)); }); auto stats_itr = db().get_index_type().indices().get().find(op.son_id); db().modify(*stats_itr, [&op](son_statistics_object &sso) { diff --git a/libraries/chain/son_wallet_withdraw_evaluator.cpp b/libraries/chain/son_wallet_withdraw_evaluator.cpp index 0d1e84d6..8d2d53a8 100644 --- a/libraries/chain/son_wallet_withdraw_evaluator.cpp +++ b/libraries/chain/son_wallet_withdraw_evaluator.cpp @@ -26,8 +26,8 @@ void_result create_son_wallet_withdraw_evaluator::do_evaluate(const son_wallet_w FC_ASSERT(expected, "Only active SON can create deposit"); } else { bool expected = false; - for (auto &son_id : swwo->expected_reports) { - if (op.son_id == son_id) { + for (auto &exp : swwo->expected_reports) { + if (op.son_id == exp.first) { expected = true; break; } @@ -65,7 +65,7 @@ object_id_type create_son_wallet_withdraw_evaluator::do_apply(const son_wallet_w auto &gpo = db().get_global_properties(); for (auto &si : gpo.active_sons) { - swwo.expected_reports.insert(si.son_id); + swwo.expected_reports.insert(std::make_pair(si.son_id, si.weight)); auto stats_itr = db().get_index_type().indices().get().find(si.son_id); db().modify(*stats_itr, [&op, &si](son_statistics_object &sso) { @@ -78,12 +78,32 @@ object_id_type create_son_wallet_withdraw_evaluator::do_apply(const son_wallet_w swwo.received_reports.insert(op.son_id); + uint64_t total_weight = 0; + for (const auto exp : swwo.expected_reports) { + total_weight = total_weight + exp.second; + } + uint64_t current_weight = 0; + for (const auto rec : swwo.received_reports) { + current_weight = current_weight + swwo.expected_reports.find(rec)->second; + } + swwo.confirmed = (current_weight > (total_weight * 2 / 3)); + swwo.processed = false; }); return new_son_wallet_withdraw_object.id; } else { db().modify(*itr, [&op](son_wallet_withdraw_object &swwo) { swwo.received_reports.insert(op.son_id); + + uint64_t total_weight = 0; + for (const auto exp : swwo.expected_reports) { + total_weight = total_weight + exp.second; + } + uint64_t current_weight = 0; + for (const auto rec : swwo.received_reports) { + current_weight = current_weight + swwo.expected_reports.find(rec)->second; + } + swwo.confirmed = (current_weight > (total_weight * 2 / 3)); }); auto stats_itr = db().get_index_type().indices().get().find(op.son_id); db().modify(*stats_itr, [&op](son_statistics_object &sso) { diff --git a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp index 07ecd0ef..ea286e67 100644 --- a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp +++ b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp @@ -137,7 +137,7 @@ void peerplays_sidechain_plugin_impl::plugin_initialize(const boost::program_opt config_ready_son = config_ready_son && !sons.empty(); #ifndef SUPPORT_MULTIPLE_SONS - FC_ASSERT(sons.size() == 1, "Multiple SONs not supported"); + //FC_ASSERT(sons.size() == 1, "Multiple SONs not supported"); #endif if (options.count("peerplays-private-key")) { diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp index 8069350f..c02d0b53 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp @@ -143,8 +143,8 @@ void sidechain_net_handler::sidechain_event_data_received(const sidechain_event_ } void sidechain_net_handler::process_deposits() { - const auto &idx = database.get_index_type().indices().get(); - const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, false)); + const auto &idx = database.get_index_type().indices().get(); + const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, true, false)); std::for_each(idx_range.first, idx_range.second, [&](const son_wallet_deposit_object &swdo) { ilog("Deposit to process: ${swdo}", ("swdo", swdo)); @@ -188,8 +188,8 @@ void sidechain_net_handler::process_deposits() { } void sidechain_net_handler::process_withdrawals() { - const auto &idx = database.get_index_type().indices().get(); - const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, false)); + const auto &idx = database.get_index_type().indices().get(); + const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, true, false)); std::for_each(idx_range.first, idx_range.second, [&](const son_wallet_withdraw_object &swwo) { ilog("Withdraw to process: ${swwo}", ("swwo", swwo)); diff --git a/tests/tests/son_wallet_tests.cpp b/tests/tests/son_wallet_tests.cpp index 7ef3abc0..a8dfc382 100644 --- a/tests/tests/son_wallet_tests.cpp +++ b/tests/tests/son_wallet_tests.cpp @@ -157,7 +157,7 @@ BOOST_AUTO_TEST_CASE( son_wallet_recreate_test ) { { son_info si; si.son_id = son_id_type(0); - si.total_votes = 1000; + si.weight = 1000; si.signing_key = alice_public_key; si.sidechain_public_keys[peerplays_sidechain::sidechain_type::bitcoin] = ""; op.sons.push_back(si); @@ -166,7 +166,7 @@ BOOST_AUTO_TEST_CASE( son_wallet_recreate_test ) { { son_info si; si.son_id = son_id_type(1); - si.total_votes = 1000; + si.weight = 1000; si.signing_key = bob_public_key; si.sidechain_public_keys[peerplays_sidechain::sidechain_type::bitcoin] = ""; op.sons.push_back(si);