2015-08-17 18:21:30 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2015, Cryptonomex, Inc.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* This source code is provided for evaluation in private test networks only, until September 8, 2015. After this date, this license expires and
|
|
|
|
|
* the code may not be used, modified or distributed for any purpose. Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
|
* are permitted until September 8, 2015, provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. The code and/or derivative works are used only for private test networks consisting of no more than 10 P2P nodes.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <graphene/delayed_node/delayed_node_plugin.hpp>
|
2015-10-05 18:25:35 +00:00
|
|
|
#include <graphene/chain/protocol/types.hpp>
|
2015-08-17 18:21:30 +00:00
|
|
|
#include <graphene/chain/database.hpp>
|
|
|
|
|
#include <graphene/app/api.hpp>
|
|
|
|
|
|
|
|
|
|
#include <fc/network/http/websocket.hpp>
|
|
|
|
|
#include <fc/rpc/websocket_api.hpp>
|
|
|
|
|
#include <fc/api.hpp>
|
2015-08-17 21:09:19 +00:00
|
|
|
#include <fc/smart_ref_impl.hpp>
|
2015-08-17 18:21:30 +00:00
|
|
|
|
2015-09-11 21:47:27 +00:00
|
|
|
|
2015-08-17 18:21:30 +00:00
|
|
|
namespace graphene { namespace delayed_node {
|
|
|
|
|
namespace bpo = boost::program_options;
|
|
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
struct delayed_node_plugin_impl {
|
|
|
|
|
std::string remote_endpoint;
|
2015-08-17 20:37:53 +00:00
|
|
|
fc::http::websocket_client client;
|
2015-08-17 18:21:30 +00:00
|
|
|
std::shared_ptr<fc::rpc::websocket_api_connection> client_connection;
|
|
|
|
|
fc::api<graphene::app::database_api> database_api;
|
|
|
|
|
boost::signals2::scoped_connection client_connection_closed;
|
2015-10-05 18:25:35 +00:00
|
|
|
graphene::chain::block_id_type last_received_remote_head;
|
|
|
|
|
graphene::chain::block_id_type last_processed_remote_head;
|
2015-08-17 18:21:30 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delayed_node_plugin::delayed_node_plugin()
|
|
|
|
|
: my(new detail::delayed_node_plugin_impl)
|
|
|
|
|
{}
|
|
|
|
|
|
2015-08-17 20:37:53 +00:00
|
|
|
delayed_node_plugin::~delayed_node_plugin()
|
|
|
|
|
{}
|
|
|
|
|
|
2015-09-24 16:03:48 +00:00
|
|
|
void delayed_node_plugin::plugin_set_program_options(bpo::options_description& cli, bpo::options_description& cfg)
|
2015-08-17 18:21:30 +00:00
|
|
|
{
|
2015-09-24 16:03:48 +00:00
|
|
|
cli.add_options()
|
2015-08-17 20:37:53 +00:00
|
|
|
("trusted-node", boost::program_options::value<std::string>()->required(), "RPC endpoint of a trusted validating node (required)")
|
2015-08-17 18:21:30 +00:00
|
|
|
;
|
2015-09-24 16:03:48 +00:00
|
|
|
cfg.add(cli);
|
2015-08-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void delayed_node_plugin::connect()
|
|
|
|
|
{
|
2015-08-17 20:37:53 +00:00
|
|
|
my->client_connection = std::make_shared<fc::rpc::websocket_api_connection>(*my->client.connect(my->remote_endpoint));
|
2015-08-17 18:21:30 +00:00
|
|
|
my->database_api = my->client_connection->get_remote_api<graphene::app::database_api>(0);
|
|
|
|
|
my->client_connection_closed = my->client_connection->closed.connect([this] {
|
|
|
|
|
connection_failed();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void delayed_node_plugin::plugin_initialize(const boost::program_options::variables_map& options)
|
|
|
|
|
{
|
2015-08-17 20:37:53 +00:00
|
|
|
my->remote_endpoint = "ws://" + options.at("trusted-node").as<std::string>();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 18:25:35 +00:00
|
|
|
void delayed_node_plugin::sync_with_trusted_node()
|
2015-08-17 20:37:53 +00:00
|
|
|
{
|
2015-10-05 18:25:35 +00:00
|
|
|
auto& db = database();
|
|
|
|
|
uint32_t synced_blocks = 0;
|
|
|
|
|
uint32_t pass_count = 0;
|
|
|
|
|
while( true )
|
|
|
|
|
{
|
|
|
|
|
graphene::chain::dynamic_global_property_object remote_dpo = my->database_api->get_dynamic_global_properties();
|
|
|
|
|
if( remote_dpo.last_irreversible_block_num <= db.head_block_num() )
|
|
|
|
|
{
|
|
|
|
|
if( remote_dpo.last_irreversible_block_num < db.head_block_num() )
|
|
|
|
|
{
|
|
|
|
|
wlog( "Trusted node seems to be behind delayed node" );
|
|
|
|
|
}
|
|
|
|
|
if( synced_blocks > 1 )
|
|
|
|
|
{
|
|
|
|
|
ilog( "Delayed node finished syncing ${n} blocks in ${k} passes", ("n", synced_blocks)("k", pass_count) );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pass_count++;
|
|
|
|
|
while( remote_dpo.last_irreversible_block_num > db.head_block_num() )
|
|
|
|
|
{
|
|
|
|
|
fc::optional<graphene::chain::signed_block> block = my->database_api->get_block( db.head_block_num()+1 );
|
|
|
|
|
FC_ASSERT(block, "Trusted node claims it has blocks it doesn't actually have.");
|
|
|
|
|
ilog("Pushing block #${n}", ("n", block->block_num()));
|
|
|
|
|
db.push_block(*block);
|
|
|
|
|
synced_blocks++;
|
2015-08-17 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-17 18:21:30 +00:00
|
|
|
|
2015-10-05 18:25:35 +00:00
|
|
|
void delayed_node_plugin::mainloop()
|
2015-08-17 20:37:53 +00:00
|
|
|
{
|
2015-10-05 18:25:35 +00:00
|
|
|
while( true )
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
fc::usleep( fc::microseconds( 296645 ) ); // wake up a little over 3Hz
|
2015-08-17 20:37:53 +00:00
|
|
|
|
2015-10-05 18:25:35 +00:00
|
|
|
if( my->last_received_remote_head == my->last_processed_remote_head )
|
|
|
|
|
continue;
|
2015-08-26 22:01:48 +00:00
|
|
|
|
2015-10-05 18:25:35 +00:00
|
|
|
sync_with_trusted_node();
|
|
|
|
|
my->last_processed_remote_head = my->last_received_remote_head;
|
|
|
|
|
}
|
|
|
|
|
catch( const fc::exception& e )
|
|
|
|
|
{
|
|
|
|
|
elog("Error during connection: ${e}", ("e", e.to_detail_string()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-17 20:37:53 +00:00
|
|
|
|
2015-10-05 18:25:35 +00:00
|
|
|
void delayed_node_plugin::plugin_startup()
|
|
|
|
|
{
|
|
|
|
|
fc::async([this]()
|
|
|
|
|
{
|
|
|
|
|
mainloop();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connect();
|
|
|
|
|
my->database_api->set_block_applied_callback([this]( const fc::variant& block_id )
|
|
|
|
|
{
|
|
|
|
|
fc::from_variant( block_id, my->last_received_remote_head );
|
|
|
|
|
} );
|
2015-08-17 20:37:53 +00:00
|
|
|
return;
|
2015-10-05 18:25:35 +00:00
|
|
|
}
|
|
|
|
|
catch (const fc::exception& e)
|
|
|
|
|
{
|
2015-08-17 18:21:30 +00:00
|
|
|
elog("Error during connection: ${e}", ("e", e.to_detail_string()));
|
|
|
|
|
}
|
2015-08-17 20:37:53 +00:00
|
|
|
fc::async([this]{connection_failed();});
|
2015-08-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void delayed_node_plugin::connection_failed()
|
|
|
|
|
{
|
|
|
|
|
elog("Connection to trusted node failed; retrying in 5 seconds...");
|
2015-10-05 18:25:35 +00:00
|
|
|
fc::schedule([this]{connect();}, fc::time_point::now() + fc::seconds(5));
|
2015-08-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} }
|