SON206 - Plugin SON Heartbeat changes (#250)

* SON206 - Plugin SON Heartbeat changes

* SON206 - Plugin SON Heartbeat changes, comment removal

* SON206 - Plugin SON Heartbeat changes, stub testing and changes

* SON206 - Plugin SON Heartbeat changes, removing debugs prints
This commit is contained in:
satyakoneru 2020-01-07 00:06:49 +11:00 committed by Bobinson K B
parent 01fb1db6a6
commit 59a02b1460
3 changed files with 92 additions and 2 deletions

View file

@ -103,4 +103,8 @@ FC_REFLECT_DERIVED( graphene::chain::son_statistics_object,
(graphene::db::object),
(owner)
(txs_signed)
(total_downtime)
(current_interval_downtime)
(last_down_timestamp)
(last_active_timestamp)
)

View file

@ -3,6 +3,7 @@
#include <graphene/app/plugin.hpp>
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/son_object.hpp>
#include <graphene/chain/database.hpp>
#include <graphene/peerplays_sidechain/defs.hpp>

View file

@ -23,14 +23,18 @@ class peerplays_sidechain_plugin_impl
boost::program_options::options_description& cfg);
void plugin_initialize(const boost::program_options::variables_map& options);
void plugin_startup();
private:
void schedule_heartbeat_loop();
void heartbeat_loop();
private:
peerplays_sidechain_plugin& plugin;
bool config_ready_son;
bool config_ready_bitcoin;
std::unique_ptr<peerplays_sidechain::sidechain_net_manager> net_manager;
std::map<chain::public_key_type, fc::ecc::private_key> _private_keys;
std::set<chain::son_id_type> _sons;
fc::future<void> _heartbeat_task;
};
peerplays_sidechain_plugin_impl::peerplays_sidechain_plugin_impl(peerplays_sidechain_plugin& _plugin) :
@ -43,6 +47,14 @@ peerplays_sidechain_plugin_impl::peerplays_sidechain_plugin_impl(peerplays_sidec
peerplays_sidechain_plugin_impl::~peerplays_sidechain_plugin_impl()
{
try {
if( _heartbeat_task.valid() )
_heartbeat_task.cancel_and_wait(__FUNCTION__);
} catch(fc::canceled_exception&) {
//Expected exception. Move along.
} catch(fc::exception& e) {
edump((e.to_detail_string()));
}
}
void peerplays_sidechain_plugin_impl::plugin_set_program_options(
@ -74,6 +86,31 @@ void peerplays_sidechain_plugin_impl::plugin_initialize(const boost::program_opt
{
config_ready_son = options.count( "son-id" ) && options.count( "peerplays-private-key" );
if (config_ready_son) {
LOAD_VALUE_SET(options, "son-id", _sons, chain::son_id_type)
if( options.count("peerplays-private-key") )
{
const std::vector<std::string> key_id_to_wif_pair_strings = options["peerplays-private-key"].as<std::vector<std::string>>();
for (const std::string& key_id_to_wif_pair_string : key_id_to_wif_pair_strings)
{
auto key_id_to_wif_pair = graphene::app::dejsonify<std::pair<chain::public_key_type, std::string> >(key_id_to_wif_pair_string, 5);
ilog("Public Key: ${public}", ("public", key_id_to_wif_pair.first));
fc::optional<fc::ecc::private_key> private_key = graphene::utilities::wif_to_key(key_id_to_wif_pair.second);
if (!private_key)
{
// the key isn't in WIF format; see if they are still passing the old native private key format. This is
// just here to ease the transition, can be removed soon
try
{
private_key = fc::variant(key_id_to_wif_pair.second, 2).as<fc::ecc::private_key>(1);
}
catch (const fc::exception&)
{
FC_THROW("Invalid WIF-format private key ${key_string}", ("key_string", key_id_to_wif_pair.second));
}
}
_private_keys[key_id_to_wif_pair.first] = *private_key;
}
}
} else {
wlog("Haven't set up SON parameters");
throw;
@ -117,11 +154,59 @@ void peerplays_sidechain_plugin_impl::plugin_startup()
ilog("Bitcoin sidechain handler running");
}
if( !_sons.empty() && !_private_keys.empty() )
{
ilog("Starting heartbeats for ${n} sons.", ("n", _sons.size()));
schedule_heartbeat_loop();
} else
elog("No sons configured! Please add SON IDs and private keys to configuration.");
//if (config_ready_ethereum) {
// ilog("Ethereum sidechain handler running");
//}
}
void peerplays_sidechain_plugin_impl::schedule_heartbeat_loop()
{
fc::time_point now = fc::time_point::now();
int64_t time_to_next_heartbeat = 180000000;
fc::time_point next_wakeup( now + fc::microseconds( time_to_next_heartbeat ) );
_heartbeat_task = fc::schedule([this]{heartbeat_loop();},
next_wakeup, "SON Heartbeat Production");
}
void peerplays_sidechain_plugin_impl::heartbeat_loop()
{
chain::database& d = plugin.database();
chain::son_id_type son_id = *(_sons.begin());
const chain::global_property_object& gpo = d.get_global_properties();
auto it = std::find(gpo.active_sons.begin(), gpo.active_sons.end(), son_id);
if(it != gpo.active_sons.end()) {
ilog("peerplays_sidechain_plugin: sending heartbeat");
chain::son_heartbeat_operation op;
const auto& idx = d.get_index_type<chain::son_index>().indices().get<by_id>();
auto son_obj = idx.find( son_id );
op.owner_account = son_obj->son_account;
op.son_id = son_id;
op.ts = fc::time_point::now() + fc::seconds(0);
chain::signed_transaction trx = d.create_signed_transaction(_private_keys.begin()->second, op);
fc::future<bool> fut = fc::async( [&](){
try {
d.push_transaction(trx);
plugin.app().p2p_node()->broadcast(net::trx_message(trx));
return true;
} catch(fc::exception e){
ilog("peerplays_sidechain_plugin: sending heartbeat failed with exception ${e}",("e", e.what()));
return false;
}
});
fut.wait(fc::seconds(10));
}
schedule_heartbeat_loop();
}
} // end namespace detail
peerplays_sidechain_plugin::peerplays_sidechain_plugin() :