diff --git a/libraries/plugins/CMakeLists.txt b/libraries/plugins/CMakeLists.txt index a8bf2c23..128d8aa1 100644 --- a/libraries/plugins/CMakeLists.txt +++ b/libraries/plugins/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory( witness ) add_subdirectory( account_history ) add_subdirectory( market_history ) +add_subdirectory( delayed_node ) diff --git a/libraries/plugins/delayed_node/CMakeLists.txt b/libraries/plugins/delayed_node/CMakeLists.txt new file mode 100644 index 00000000..63dd73e5 --- /dev/null +++ b/libraries/plugins/delayed_node/CMakeLists.txt @@ -0,0 +1,21 @@ +file(GLOB HEADERS "include/graphene/delayed_node/*.hpp") + +add_library( graphene_delayed_node + delayed_node_plugin.cpp + ) + +target_link_libraries( graphene_delayed_node graphene_chain graphene_app ) +target_include_directories( graphene_delayed_node + PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) + +if(MSVC) + set_source_files_properties( delayed_node_plugin.cpp PROPERTIES COMPILE_FLAGS "/bigobj" ) +endif(MSVC) + +install( TARGETS + graphene_delayed_node + + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib +) diff --git a/libraries/plugins/delayed_node/delayed_node_plugin.cpp b/libraries/plugins/delayed_node/delayed_node_plugin.cpp new file mode 100644 index 00000000..de3a6e7f --- /dev/null +++ b/libraries/plugins/delayed_node/delayed_node_plugin.cpp @@ -0,0 +1,89 @@ +/* + * 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 +#include +#include + +#include +#include +#include + +namespace graphene { namespace delayed_node { +namespace bpo = boost::program_options; + +namespace detail { +struct delayed_node_plugin_impl { + std::string remote_endpoint; + int delay_blocks; + std::shared_ptr client_connection; + fc::api database_api; + boost::signals2::scoped_connection client_connection_closed; +}; +} + +delayed_node_plugin::delayed_node_plugin() + : my(new detail::delayed_node_plugin_impl) +{} + +void delayed_node_plugin::plugin_set_program_options(bpo::options_description&, bpo::options_description& cfg) +{ + cfg.add_options() + ("trusted_node", boost::program_options::value()->required(), "RPC endpoint of a trusted validating node") + ("delay_block_count", boost::program_options::value()->required(), "Number of blocks to delay before advancing chain state") + ; + +} + +void delayed_node_plugin::connect() +{ + fc::http::websocket_client client; + my->client_connection = std::make_shared(*client.connect(my->remote_endpoint)); + my->database_api = my->client_connection->get_remote_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) +{ + my->remote_endpoint = options.at("trusted_node").as(); + my->delay_blocks = options.at("delay_block_count").as(); + + try { + connect(); + } catch (const fc::exception& e) { + elog("Error during connection: ${e}", ("e", e.to_detail_string())); + connection_failed(); + } +} + +void delayed_node_plugin::connection_failed() +{ + elog("Connection to trusted node failed; retrying in 5 seconds..."); + fc::usleep(fc::seconds(5)); + + try { + connect(); + } catch (const fc::exception& e) { + elog("Error during connection: ${e}", ("e", e.to_detail_string())); + fc::async([this]{connection_failed();}); + } +} + +} } diff --git a/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp b/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp new file mode 100644 index 00000000..0ab64ad0 --- /dev/null +++ b/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp @@ -0,0 +1,44 @@ +/* + * 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. + */ +#pragma once + +#include + +namespace graphene { namespace delayed_node { +namespace detail { struct delayed_node_plugin_impl; } + +class delayed_node_plugin : public graphene::app::plugin +{ + std::unique_ptr my; +public: + delayed_node_plugin(); + virtual ~delayed_node_plugin(){} + + std::string plugin_name()const override { return "delayed_node"; } + virtual void plugin_set_program_options(boost::program_options::options_description&, + boost::program_options::options_description& cfg) override; + virtual void plugin_initialize(const boost::program_options::variables_map& options) override; + virtual void plugin_startup() override; + +protected: + void connection_failed(); + void connect(); +}; + +} } //graphene::account_history +