diff --git a/libraries/chain/son_wallet_evaluator.cpp b/libraries/chain/son_wallet_evaluator.cpp index 08744862..d3cac39c 100644 --- a/libraries/chain/son_wallet_evaluator.cpp +++ b/libraries/chain/son_wallet_evaluator.cpp @@ -28,7 +28,8 @@ void_result update_son_wallet_evaluator::do_evaluate(const son_wallet_update_ope const auto& idx = db().get_index_type().indices().get(); FC_ASSERT( idx.find(op.son_wallet_id) != idx.end() ); auto itr = idx.find(op.son_wallet_id); - //FC_ASSERT( itr.addresses[op.sidechain] == "", "Sidechain wallet address already set"); + FC_ASSERT( itr->addresses.find(peerplays_sidechain::sidechain_type::bitcoin) == itr->addresses.end() || + itr->addresses.at(peerplays_sidechain::sidechain_type::bitcoin).empty(), "Sidechain wallet address already set"); return void_result(); } FC_CAPTURE_AND_RETHROW( (op) ) } diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/peerplays_sidechain_plugin.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/peerplays_sidechain_plugin.hpp index e986fe8e..9d67d1a1 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/peerplays_sidechain_plugin.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/peerplays_sidechain_plugin.hpp @@ -30,6 +30,11 @@ class peerplays_sidechain_plugin : public graphene::app::plugin virtual void plugin_startup() override; std::unique_ptr my; + + son_id_type get_son_id(); + son_object get_son_object(); + bool is_active_son(); + std::map& get_private_keys(); }; } } //graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler.hpp index 399b7608..5de12d5d 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler.hpp @@ -17,8 +17,7 @@ public: graphene::peerplays_sidechain::sidechain_type get_sidechain(); std::vector get_sidechain_addresses(); - virtual son_wallet_update_operation recreate_primary_wallet() = 0; - virtual string recreate_primary_wallet(const vector& participants) = 0; + virtual void recreate_primary_wallet() = 0; protected: peerplays_sidechain_plugin& plugin; diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp index 2e469138..803b24de 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp @@ -60,8 +60,7 @@ public: sidechain_net_handler_bitcoin(peerplays_sidechain_plugin& _plugin, const boost::program_options::variables_map& options); virtual ~sidechain_net_handler_bitcoin(); - son_wallet_update_operation recreate_primary_wallet(); - string recreate_primary_wallet(const vector& participants); + void recreate_primary_wallet(); bool connection_is_not_defined() const; diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_manager.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_manager.hpp index e920d915..bd6f1ab3 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_manager.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_manager.hpp @@ -16,8 +16,7 @@ public: virtual ~sidechain_net_manager(); bool create_handler(peerplays_sidechain::sidechain_type sidechain, const boost::program_options::variables_map& options); - signed_transaction recreate_primary_wallet(); - string recreate_primary_wallet(peerplays_sidechain::sidechain_type sidechain, const vector& participants); + void recreate_primary_wallet(); private: peerplays_sidechain_plugin& plugin; graphene::chain::database& database; diff --git a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp index f6342880..c93f954a 100644 --- a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp +++ b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp @@ -31,6 +31,11 @@ class peerplays_sidechain_plugin_impl void plugin_initialize(const boost::program_options::variables_map& options); void plugin_startup(); + son_id_type get_son_id(); + son_object get_son_object(); + bool is_active_son(); + std::map& get_private_keys(); + void schedule_heartbeat_loop(); void heartbeat_loop(); void create_son_down_proposals(); @@ -180,6 +185,46 @@ void peerplays_sidechain_plugin_impl::plugin_startup() //} } +son_id_type peerplays_sidechain_plugin_impl::get_son_id() +{ + return *(_sons.begin()); +} + +son_object peerplays_sidechain_plugin_impl::get_son_object() +{ + const auto& idx = plugin.database().get_index_type().indices().get(); + auto son_obj = idx.find( get_son_id() ); + if (son_obj == idx.end()) + return {}; + return *son_obj; +} + +bool peerplays_sidechain_plugin_impl::is_active_son() +{ + const auto& idx = plugin.database().get_index_type().indices().get(); + auto son_obj = idx.find( get_son_id() ); + if (son_obj == idx.end()) + return false; + + const chain::global_property_object& gpo = plugin.database().get_global_properties(); + vector active_son_ids; + active_son_ids.reserve(gpo.active_sons.size()); + std::transform(gpo.active_sons.begin(), gpo.active_sons.end(), + std::inserter(active_son_ids, active_son_ids.end()), + [](const son_info& swi) { + return swi.son_id; + }); + + auto it = std::find(active_son_ids.begin(), active_son_ids.end(), get_son_id()); + + return (it != active_son_ids.end()); +} + +std::map& peerplays_sidechain_plugin_impl::get_private_keys() +{ + return _private_keys; +} + void peerplays_sidechain_plugin_impl::schedule_heartbeat_loop() { fc::time_point now = fc::time_point::now(); @@ -287,20 +332,7 @@ void peerplays_sidechain_plugin_impl::create_son_down_proposals() void peerplays_sidechain_plugin_impl::recreate_primary_wallet() { - chain::database& d = plugin.database(); - signed_transaction trx = net_manager->recreate_primary_wallet(); - auto dyn_props = d.get_dynamic_global_properties(); - trx.set_reference_block( dyn_props.head_block_id ); - trx.set_expiration( d.head_block_time() + d.get_global_properties().parameters.maximum_time_until_expiration ); - d.current_fee_schedule().set_fee( trx.operations.back() ); - - trx.sign(_private_keys.begin()->second, d.get_chain_id()); - - try { - d.push_transaction(trx, database::validation_steps::skip_block_size_check); - } catch (fc::exception e) { - ilog("peerplays_sidechain_plugin_impl: sending son wallet update operations failed with exception ${e}",("e", e.what())); - } + net_manager->recreate_primary_wallet(); } void peerplays_sidechain_plugin_impl::process_deposits() { @@ -431,5 +463,25 @@ void peerplays_sidechain_plugin::plugin_startup() my->plugin_startup(); } +son_id_type peerplays_sidechain_plugin::get_son_id() +{ + return my->get_son_id(); +} + +son_object peerplays_sidechain_plugin::get_son_object() +{ + return my->get_son_object(); +} + +bool peerplays_sidechain_plugin::is_active_son() +{ + return my->is_active_son(); +} + +std::map& peerplays_sidechain_plugin::get_private_keys() +{ + return my->get_private_keys(); +} + } } // graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp index 886e7125..99e64750 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp @@ -253,7 +253,7 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain sidechain_net_handler_bitcoin::~sidechain_net_handler_bitcoin() { } -son_wallet_update_operation sidechain_net_handler_bitcoin::recreate_primary_wallet() { +void sidechain_net_handler_bitcoin::recreate_primary_wallet() { const auto& idx_swi = database.get_index_type().indices().get(); auto obj = idx_swi.rbegin(); if (obj != idx_swi.rend()) { @@ -275,23 +275,25 @@ son_wallet_update_operation sidechain_net_handler_bitcoin::recreate_primary_wall if( pt.count( "error" ) && pt.get_child( "error" ).empty() ) { ilog(__FUNCTION__); + std::stringstream res; + boost::property_tree::json_parser::write_json(res, pt.get_child("result")); + son_wallet_update_operation op; op.payer = database.get_global_properties().parameters.get_son_btc_account_id(); op.son_wallet_id = (*obj).id; op.sidechain = sidechain_type::bitcoin; - op.address = ss.str(); + op.address = res.str(); - return op; + signed_transaction trx = database.create_signed_transaction(plugin.get_private_keys().begin()->second, op); + + try { + database.push_transaction(trx); + } catch (fc::exception e) { + ilog("sidechain_net_handler_bitcoin: sending son wallet update operation failed with exception ${e}",("e", e.what())); + } } } } - return {}; -} - -string sidechain_net_handler_bitcoin::recreate_primary_wallet( const vector& participants ) { - ilog(__FUNCTION__); - string result = create_multisignature_wallet(participants); - return result; } bool sidechain_net_handler_bitcoin::connection_is_not_defined() const diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_manager.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_manager.cpp index 5ed57c76..1c17a890 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_manager.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_manager.cpp @@ -24,10 +24,10 @@ bool sidechain_net_manager::create_handler(peerplays_sidechain::sidechain_type s switch (sidechain) { case sidechain_type::bitcoin: { - std::unique_ptr h = std::unique_ptr(new sidechain_net_handler_bitcoin(plugin, options)); - net_handlers.push_back(std::move(h)); - ret_val = true; - break; + std::unique_ptr h = std::unique_ptr(new sidechain_net_handler_bitcoin(plugin, options)); + net_handlers.push_back(std::move(h)); + ret_val = true; + break; } default: assert(false); @@ -36,35 +36,10 @@ bool sidechain_net_manager::create_handler(peerplays_sidechain::sidechain_type s return ret_val; } -signed_transaction sidechain_net_manager::recreate_primary_wallet() { - ilog(__FUNCTION__); - - signed_transaction trx = {}; - - const auto& idx = database.get_index_type().indices().get(); - auto swo = idx.rbegin(); - +void sidechain_net_manager::recreate_primary_wallet() { for ( size_t i = 0; i < net_handlers.size(); i++ ) { - son_wallet_update_operation op = net_handlers.at(i)->recreate_primary_wallet(); - - if (swo != idx.rend()) { - if (op.son_wallet_id == swo->id) { - trx.operations.push_back(op); - } - } + net_handlers.at(i)->recreate_primary_wallet(); } - - return trx; -} - -string sidechain_net_manager::recreate_primary_wallet(peerplays_sidechain::sidechain_type sidechain, const vector& participants) { - ilog(__FUNCTION__); - for ( size_t i = 0; i < net_handlers.size(); i++ ) { - if (net_handlers.at(i)->get_sidechain() == sidechain) { - return net_handlers.at(i)->recreate_primary_wallet(participants); - } - } - return ""; } } } // graphene::peerplays_sidechain