BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
delayed_node_plugin.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
28 #include <graphene/app/api.hpp>
29 
31 #include <fc/rpc/websocket_api.hpp>
32 #include <fc/api.hpp>
33 
34 namespace graphene { namespace delayed_node {
35 namespace bpo = boost::program_options;
36 
37 namespace detail {
39  std::string remote_endpoint;
41  std::shared_ptr<fc::rpc::websocket_api_connection> client_connection;
46 };
47 }
48 
50  : my(nullptr)
51 {}
52 
54 {}
55 
56 void delayed_node_plugin::plugin_set_program_options(bpo::options_description& cli, bpo::options_description& cfg)
57 {
58  cli.add_options()
59  ("trusted-node", boost::program_options::value<std::string>(), "RPC endpoint of a trusted validating node (required for delayed_node)")
60  ;
61  cfg.add(cli);
62 }
63 
65 {
66  my->client_connection = std::make_shared<fc::rpc::websocket_api_connection>(
67  my->client.connect(my->remote_endpoint),
69  my->database_api = my->client_connection->get_remote_api<graphene::app::database_api>(0);
70  my->client_connection_closed = my->client_connection->closed.connect([this] {
72  });
73 }
74 
75 void delayed_node_plugin::plugin_initialize(const boost::program_options::variables_map& options)
76 {
77  FC_ASSERT(options.count("trusted-node") > 0);
78  my = std::unique_ptr<detail::delayed_node_plugin_impl>{ new detail::delayed_node_plugin_impl() };
79  my->remote_endpoint = "ws://" + options.at("trusted-node").as<std::string>();
80 }
81 
83 {
84  auto& db = database();
85  uint32_t synced_blocks = 0;
86  uint32_t pass_count = 0;
87  while( true )
88  {
89  graphene::chain::dynamic_global_property_object remote_dpo = my->database_api->get_dynamic_global_properties();
90  if( remote_dpo.last_irreversible_block_num <= db.head_block_num() )
91  {
92  if( remote_dpo.last_irreversible_block_num < db.head_block_num() )
93  {
94  wlog( "Trusted node seems to be behind delayed node" );
95  }
96  if( synced_blocks > 1 )
97  {
98  ilog( "Delayed node finished syncing ${n} blocks in ${k} passes", ("n", synced_blocks)("k", pass_count) );
99  }
100  break;
101  }
102  pass_count++;
103  while( remote_dpo.last_irreversible_block_num > db.head_block_num() )
104  {
105  fc::optional<graphene::chain::signed_block> block = my->database_api->get_block( db.head_block_num()+1 );
106  // TODO: during sync, decouple requesting blocks from preprocessing + applying them
107  FC_ASSERT(block, "Trusted node claims it has blocks it doesn't actually have.");
108  ilog("Pushing block #${n}", ("n", block->block_num()));
109  db.precompute_parallel( *block, graphene::chain::database::skip_nothing ).wait();
110  db.push_block(*block);
111  synced_blocks++;
112  }
113  }
114 }
115 
117 {
118  while( true )
119  {
120  try
121  {
122  fc::usleep( fc::microseconds( 296645 ) ); // wake up a little over 3Hz
123 
124  if( my->last_received_remote_head == my->last_processed_remote_head )
125  continue;
126 
128  my->last_processed_remote_head = my->last_received_remote_head;
129  }
130  catch( const fc::exception& e )
131  {
132  elog("Error during connection: ${e}", ("e", e.to_detail_string()));
133  }
134  }
135 }
136 
138 {
139  fc::async([this]()
140  {
141  mainloop();
142  });
143 
144  try
145  {
146  connect();
147  my->database_api->set_block_applied_callback([this]( const fc::variant& block_id )
148  {
149  fc::from_variant( block_id, my->last_received_remote_head, GRAPHENE_MAX_NESTED_OBJECTS );
150  } );
151  return;
152  }
153  catch (const fc::exception& e)
154  {
155  elog("Error during connection: ${e}", ("e", e.to_detail_string()));
156  }
157  fc::async([this]{connection_failed();});
158 }
159 
161 {
162  elog("Connection to trusted node failed; retrying in 5 seconds...");
164 }
165 
166 } }
auto async(Functor &&f, const char *desc FC_TASK_NAME_DEFAULT_ARG, priority prio=priority()) -> fc::future< decltype(f())>
Definition: thread.hpp:227
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:31
Maintains global state information (committee_member list, current fees)This is an implementation det...
#define GRAPHENE_NET_MAX_NESTED_OBJECTS
Definition: config.hpp:110
Definition: api.cpp:56
#define elog(FORMAT,...)
Definition: logger.hpp:129
virtual void plugin_set_program_options(boost::program_options::options_description &, boost::program_options::options_description &cfg) override
Fill in command line parameters used by the plugin.
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
std::string to_detail_string(log_level ll=log_level::all) const
Definition: exception.cpp:183
#define wlog(FORMAT,...)
Definition: logger.hpp:123
std::shared_ptr< fc::rpc::websocket_api_connection > client_connection
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
void usleep(const microseconds &u)
Definition: thread.cpp:368
chain::database & database()
Definition: plugin.hpp:114
microseconds seconds(int64_t s)
Definition: time.hpp:34
#define ilog(FORMAT,...)
Definition: logger.hpp:117
virtual void plugin_initialize(const boost::program_options::variables_map &options) override
Perform early startup routines and register plugin indexes, callbacks, etc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
virtual void plugin_startup() override
Begin normal runtime operations.
The database_api class implements the RPC API for the chain database.
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
uint32_t block_num() const
Definition: block.hpp:34
static time_point now()
Definition: time.cpp:13
boost::signals2::scoped_connection scoped_connection
Definition: signals.hpp:22
auto schedule(Functor &&f, const fc::time_point &t, const char *desc FC_TASK_NAME_DEFAULT_ARG, priority prio=priority()) -> fc::future< decltype(f())>
Definition: thread.hpp:231