From 224f01269c2cd8f9a9dcb5a0812c508f6a204ae3 Mon Sep 17 00:00:00 2001 From: Srdjan Obucina Date: Sat, 22 Feb 2020 02:15:37 +0100 Subject: [PATCH] Fix repeating tasks that should be executed by scheduled SON only --- .../peerplays_sidechain_plugin.hpp | 1 + .../peerplays_sidechain_plugin.cpp | 14 ++++ .../sidechain_net_handler.cpp | 74 ++++++++----------- 3 files changed, 47 insertions(+), 42 deletions(-) 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 e9a868f1..bc1f6d21 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 @@ -28,6 +28,7 @@ class peerplays_sidechain_plugin : public graphene::app::plugin std::unique_ptr my; std::set& get_sons(); + son_id_type& get_current_son_id(); son_object get_son_object(son_id_type son_id); bool is_active_son(son_id_type son_id); std::map& get_private_keys(); diff --git a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp index 5af5e5fc..bb722339 100644 --- a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp +++ b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp @@ -35,6 +35,7 @@ class peerplays_sidechain_plugin_impl void plugin_startup(); std::set& get_sons(); + son_id_type& get_current_son_id(); son_object get_son_object(son_id_type son_id); bool is_active_son(son_id_type son_id); std::map& get_private_keys(); @@ -55,6 +56,8 @@ class peerplays_sidechain_plugin_impl bool config_ready_bitcoin; bool config_ready_peerplays; + son_id_type current_son_id; + std::unique_ptr net_manager; std::map _private_keys; std::set _sons; @@ -70,6 +73,7 @@ peerplays_sidechain_plugin_impl::peerplays_sidechain_plugin_impl(peerplays_sidec config_ready_son(false), config_ready_bitcoin(false), config_ready_peerplays(false), + current_son_id(son_id_type(std::numeric_limits().max())), net_manager(nullptr) { } @@ -225,6 +229,10 @@ std::set& peerplays_sidechain_plugin_impl::get_sons() return _sons; } +son_id_type& peerplays_sidechain_plugin_impl::get_current_son_id() { + return current_son_id; +} + son_object peerplays_sidechain_plugin_impl::get_son_object(son_id_type son_id) { const auto& idx = plugin.database().get_index_type().indices().get(); @@ -403,6 +411,8 @@ void peerplays_sidechain_plugin_impl::on_block_applied( const signed_block& b ) // check if we control scheduled SON if( _sons.find( next_son_id ) != _sons.end() ) { + current_son_id = next_son_id; + create_son_down_proposals(); recreate_primary_wallet(); @@ -536,6 +546,10 @@ std::set& peerplays_sidechain_plugin::get_sons() return my->get_sons(); } +son_id_type& peerplays_sidechain_plugin::get_current_son_id() { + return my->get_current_son_id(); +} + son_object peerplays_sidechain_plugin::get_son_object(son_id_type son_id) { return my->get_son_object(son_id); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp index 0ad94111..062c3094 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp @@ -164,29 +164,24 @@ void sidechain_net_handler::process_deposits() { const chain::global_property_object& gpo = plugin.database().get_global_properties(); - for (son_id_type son_id : plugin.get_sons()) { - if (plugin.is_active_son(son_id)) { + son_wallet_deposit_process_operation p_op; + p_op.payer = GRAPHENE_SON_ACCOUNT; + p_op.son_wallet_deposit_id = swdo.id; - son_wallet_deposit_process_operation p_op; - p_op.payer = GRAPHENE_SON_ACCOUNT; - p_op.son_wallet_deposit_id = swdo.id; + proposal_create_operation proposal_op; + proposal_op.fee_paying_account = plugin.get_son_object(plugin.get_current_son_id()).son_account; + proposal_op.proposed_ops.emplace_back( op_wrapper( p_op ) ); + uint32_t lifetime = ( gpo.parameters.block_interval * gpo.active_witnesses.size() ) * 3; + proposal_op.expiration_time = time_point_sec( plugin.database().head_block_time().sec_since_epoch() + lifetime ); - proposal_create_operation proposal_op; - proposal_op.fee_paying_account = plugin.get_son_object(son_id).son_account; - proposal_op.proposed_ops.emplace_back( op_wrapper( p_op ) ); - uint32_t lifetime = ( gpo.parameters.block_interval * gpo.active_witnesses.size() ) * 3; - proposal_op.expiration_time = time_point_sec( plugin.database().head_block_time().sec_since_epoch() + lifetime ); - - signed_transaction trx = plugin.database().create_signed_transaction(plugin.get_private_key(son_id), proposal_op); - trx.validate(); - try { - plugin.database().push_transaction(trx, database::validation_steps::skip_block_size_check); - if(plugin.app().p2p_node()) - plugin.app().p2p_node()->broadcast(net::trx_message(trx)); - } catch(fc::exception e){ - ilog("sidechain_net_handler: sending proposal for transfer operation failed with exception ${e}",("e", e.what())); - } - } + signed_transaction trx = plugin.database().create_signed_transaction(plugin.get_private_key(plugin.get_current_son_id()), proposal_op); + trx.validate(); + try { + plugin.database().push_transaction(trx, database::validation_steps::skip_block_size_check); + if(plugin.app().p2p_node()) + plugin.app().p2p_node()->broadcast(net::trx_message(trx)); + } catch(fc::exception e){ + ilog("sidechain_net_handler: sending proposal for transfer operation failed with exception ${e}",("e", e.what())); } }); } @@ -204,29 +199,24 @@ void sidechain_net_handler::process_withdrawals() { const chain::global_property_object& gpo = plugin.database().get_global_properties(); - for (son_id_type son_id : plugin.get_sons()) { - if (plugin.is_active_son(son_id)) { + son_wallet_withdraw_process_operation p_op; + p_op.payer = GRAPHENE_SON_ACCOUNT; + p_op.son_wallet_withdraw_id = swwo.id; - son_wallet_withdraw_process_operation p_op; - p_op.payer = GRAPHENE_SON_ACCOUNT; - p_op.son_wallet_withdraw_id = swwo.id; + proposal_create_operation proposal_op; + proposal_op.fee_paying_account = plugin.get_son_object(plugin.get_current_son_id()).son_account; + proposal_op.proposed_ops.emplace_back( op_wrapper( p_op ) ); + uint32_t lifetime = ( gpo.parameters.block_interval * gpo.active_witnesses.size() ) * 3; + proposal_op.expiration_time = time_point_sec( plugin.database().head_block_time().sec_since_epoch() + lifetime ); - proposal_create_operation proposal_op; - proposal_op.fee_paying_account = plugin.get_son_object(son_id).son_account; - proposal_op.proposed_ops.emplace_back( op_wrapper( p_op ) ); - uint32_t lifetime = ( gpo.parameters.block_interval * gpo.active_witnesses.size() ) * 3; - proposal_op.expiration_time = time_point_sec( plugin.database().head_block_time().sec_since_epoch() + lifetime ); - - signed_transaction trx = plugin.database().create_signed_transaction(plugin.get_private_key(son_id), proposal_op); - trx.validate(); - try { - plugin.database().push_transaction(trx, database::validation_steps::skip_block_size_check); - if(plugin.app().p2p_node()) - plugin.app().p2p_node()->broadcast(net::trx_message(trx)); - } catch(fc::exception e){ - ilog("sidechain_net_handler: sending proposal for transfer operation failed with exception ${e}",("e", e.what())); - } - } + signed_transaction trx = plugin.database().create_signed_transaction(plugin.get_private_key(plugin.get_current_son_id()), proposal_op); + trx.validate(); + try { + plugin.database().push_transaction(trx, database::validation_steps::skip_block_size_check); + if(plugin.app().p2p_node()) + plugin.app().p2p_node()->broadcast(net::trx_message(trx)); + } catch(fc::exception e){ + ilog("sidechain_net_handler: sending proposal for transfer operation failed with exception ${e}",("e", e.what())); } }); }