Refactor primary wallet recreation

This commit is contained in:
Srdjan Obucina 2020-01-30 18:14:00 +01:00
parent 01a1f584df
commit 0c7ef96178
8 changed files with 94 additions and 62 deletions

View file

@ -28,7 +28,8 @@ void_result update_son_wallet_evaluator::do_evaluate(const son_wallet_update_ope
const auto& idx = db().get_index_type<son_wallet_index>().indices().get<by_id>(); const auto& idx = db().get_index_type<son_wallet_index>().indices().get<by_id>();
FC_ASSERT( idx.find(op.son_wallet_id) != idx.end() ); FC_ASSERT( idx.find(op.son_wallet_id) != idx.end() );
auto itr = idx.find(op.son_wallet_id); 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(); return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) } } FC_CAPTURE_AND_RETHROW( (op) ) }

View file

@ -30,6 +30,11 @@ class peerplays_sidechain_plugin : public graphene::app::plugin
virtual void plugin_startup() override; virtual void plugin_startup() override;
std::unique_ptr<detail::peerplays_sidechain_plugin_impl> my; std::unique_ptr<detail::peerplays_sidechain_plugin_impl> my;
son_id_type get_son_id();
son_object get_son_object();
bool is_active_son();
std::map<chain::public_key_type, fc::ecc::private_key>& get_private_keys();
}; };
} } //graphene::peerplays_sidechain } } //graphene::peerplays_sidechain

View file

@ -17,8 +17,7 @@ public:
graphene::peerplays_sidechain::sidechain_type get_sidechain(); graphene::peerplays_sidechain::sidechain_type get_sidechain();
std::vector<std::string> get_sidechain_addresses(); std::vector<std::string> get_sidechain_addresses();
virtual son_wallet_update_operation recreate_primary_wallet() = 0; virtual void recreate_primary_wallet() = 0;
virtual string recreate_primary_wallet(const vector<string>& participants) = 0;
protected: protected:
peerplays_sidechain_plugin& plugin; peerplays_sidechain_plugin& plugin;

View file

@ -60,8 +60,7 @@ public:
sidechain_net_handler_bitcoin(peerplays_sidechain_plugin& _plugin, const boost::program_options::variables_map& options); sidechain_net_handler_bitcoin(peerplays_sidechain_plugin& _plugin, const boost::program_options::variables_map& options);
virtual ~sidechain_net_handler_bitcoin(); virtual ~sidechain_net_handler_bitcoin();
son_wallet_update_operation recreate_primary_wallet(); void recreate_primary_wallet();
string recreate_primary_wallet(const vector<string>& participants);
bool connection_is_not_defined() const; bool connection_is_not_defined() const;

View file

@ -16,8 +16,7 @@ public:
virtual ~sidechain_net_manager(); virtual ~sidechain_net_manager();
bool create_handler(peerplays_sidechain::sidechain_type sidechain, const boost::program_options::variables_map& options); bool create_handler(peerplays_sidechain::sidechain_type sidechain, const boost::program_options::variables_map& options);
signed_transaction recreate_primary_wallet(); void recreate_primary_wallet();
string recreate_primary_wallet(peerplays_sidechain::sidechain_type sidechain, const vector<string>& participants);
private: private:
peerplays_sidechain_plugin& plugin; peerplays_sidechain_plugin& plugin;
graphene::chain::database& database; graphene::chain::database& database;

View file

@ -31,6 +31,11 @@ class peerplays_sidechain_plugin_impl
void plugin_initialize(const boost::program_options::variables_map& options); void plugin_initialize(const boost::program_options::variables_map& options);
void plugin_startup(); void plugin_startup();
son_id_type get_son_id();
son_object get_son_object();
bool is_active_son();
std::map<chain::public_key_type, fc::ecc::private_key>& get_private_keys();
void schedule_heartbeat_loop(); void schedule_heartbeat_loop();
void heartbeat_loop(); void heartbeat_loop();
void create_son_down_proposals(); 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<chain::son_index>().indices().get<by_id>();
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<chain::son_index>().indices().get<by_id>();
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<son_id_type> 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<chain::public_key_type, fc::ecc::private_key>& peerplays_sidechain_plugin_impl::get_private_keys()
{
return _private_keys;
}
void peerplays_sidechain_plugin_impl::schedule_heartbeat_loop() void peerplays_sidechain_plugin_impl::schedule_heartbeat_loop()
{ {
fc::time_point now = fc::time_point::now(); 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() void peerplays_sidechain_plugin_impl::recreate_primary_wallet()
{ {
chain::database& d = plugin.database(); net_manager->recreate_primary_wallet();
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()));
}
} }
void peerplays_sidechain_plugin_impl::process_deposits() { void peerplays_sidechain_plugin_impl::process_deposits() {
@ -431,5 +463,25 @@ void peerplays_sidechain_plugin::plugin_startup()
my->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<chain::public_key_type, fc::ecc::private_key>& peerplays_sidechain_plugin::get_private_keys()
{
return my->get_private_keys();
}
} } // graphene::peerplays_sidechain } } // graphene::peerplays_sidechain

View file

@ -253,7 +253,7 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
sidechain_net_handler_bitcoin::~sidechain_net_handler_bitcoin() { 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<son_wallet_index>().indices().get<by_id>(); const auto& idx_swi = database.get_index_type<son_wallet_index>().indices().get<by_id>();
auto obj = idx_swi.rbegin(); auto obj = idx_swi.rbegin();
if (obj != idx_swi.rend()) { 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() ) { if( pt.count( "error" ) && pt.get_child( "error" ).empty() ) {
ilog(__FUNCTION__); ilog(__FUNCTION__);
std::stringstream res;
boost::property_tree::json_parser::write_json(res, pt.get_child("result"));
son_wallet_update_operation op; son_wallet_update_operation op;
op.payer = database.get_global_properties().parameters.get_son_btc_account_id(); op.payer = database.get_global_properties().parameters.get_son_btc_account_id();
op.son_wallet_id = (*obj).id; op.son_wallet_id = (*obj).id;
op.sidechain = sidechain_type::bitcoin; 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<string>& participants ) {
ilog(__FUNCTION__);
string result = create_multisignature_wallet(participants);
return result;
} }
bool sidechain_net_handler_bitcoin::connection_is_not_defined() const bool sidechain_net_handler_bitcoin::connection_is_not_defined() const

View file

@ -24,10 +24,10 @@ bool sidechain_net_manager::create_handler(peerplays_sidechain::sidechain_type s
switch (sidechain) { switch (sidechain) {
case sidechain_type::bitcoin: { case sidechain_type::bitcoin: {
std::unique_ptr<sidechain_net_handler> h = std::unique_ptr<sidechain_net_handler>(new sidechain_net_handler_bitcoin(plugin, options)); std::unique_ptr<sidechain_net_handler> h = std::unique_ptr<sidechain_net_handler>(new sidechain_net_handler_bitcoin(plugin, options));
net_handlers.push_back(std::move(h)); net_handlers.push_back(std::move(h));
ret_val = true; ret_val = true;
break; break;
} }
default: default:
assert(false); assert(false);
@ -36,35 +36,10 @@ bool sidechain_net_manager::create_handler(peerplays_sidechain::sidechain_type s
return ret_val; return ret_val;
} }
signed_transaction sidechain_net_manager::recreate_primary_wallet() { void sidechain_net_manager::recreate_primary_wallet() {
ilog(__FUNCTION__);
signed_transaction trx = {};
const auto& idx = database.get_index_type<son_wallet_index>().indices().get<by_id>();
auto swo = idx.rbegin();
for ( size_t i = 0; i < net_handlers.size(); i++ ) { for ( size_t i = 0; i < net_handlers.size(); i++ ) {
son_wallet_update_operation op = net_handlers.at(i)->recreate_primary_wallet(); net_handlers.at(i)->recreate_primary_wallet();
if (swo != idx.rend()) {
if (op.son_wallet_id == swo->id) {
trx.operations.push_back(op);
}
}
} }
return trx;
}
string sidechain_net_manager::recreate_primary_wallet(peerplays_sidechain::sidechain_type sidechain, const vector<string>& 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 } } // graphene::peerplays_sidechain