Scorum
debug_node_api.cpp
Go to the documentation of this file.
1 
2 #include <fc/filesystem.hpp>
3 #include <fc/optional.hpp>
4 #include <fc/variant_object.hpp>
5 
8 
10 
15 
16 #include <graphene/utilities/key_conversion.hpp>
17 
20 
21 namespace scorum {
22 namespace plugin {
23 namespace debug_node {
24 
25 namespace detail {
26 
28 {
29 public:
31  {
32  }
34  {
35  }
36 
37  virtual void maybe_get_private_key(fc::optional<fc::ecc::private_key>& result,
38  const scorum::chain::public_key_type& pubkey,
39  const std::string& account_name) override;
40 
41  std::string dev_key_prefix;
42  std::map<scorum::chain::public_key_type, fc::ecc::private_key> key_table;
43 };
44 
46 {
47 public:
49 
50  uint32_t debug_push_blocks(const std::string& src_filename, uint32_t count, bool skip_validate_invariants);
51  uint32_t debug_generate_blocks(const std::string& debug_key, uint32_t count);
52  uint32_t debug_generate_blocks_until(const std::string& debug_key,
53  const fc::time_point_sec& head_block_time,
54  bool generate_sparsely);
55  fc::optional<scorum::chain::signed_block> debug_pop_block();
56  // void debug_push_block( const scorum::chain::signed_block& block );
58  scorum::chain::hardfork_property_object debug_get_hardfork_property_object();
59  void debug_update_object(const fc::variant_object& update);
60  fc::variant_object debug_get_edits();
61  void debug_set_edits(const fc::variant_object& edits);
62  // void debug_save_db( std::string db_path );
63  void debug_stream_json_objects(const std::string& filename);
65  void debug_set_hardfork(uint32_t hardfork_id);
66  bool debug_has_hardfork(uint32_t hardfork_id);
67  void debug_get_json_schema(std::string& schema);
68  void debug_set_dev_key_prefix(std::string prefix);
69  void debug_get_dev_key(get_dev_key_result& result, const get_dev_key_args& args);
70  std::shared_ptr<scorum::plugin::debug_node::debug_node_plugin> get_plugin();
71 
74 };
75 
76 void debug_private_key_storage::maybe_get_private_key(fc::optional<fc::ecc::private_key>& result,
77  const scorum::chain::public_key_type& pubkey,
78  const std::string& account_name)
79 {
80  auto it = key_table.find(pubkey);
81  if (it != key_table.end())
82  {
83  result = it->second;
84  return;
85  }
86  fc::ecc::private_key gen_priv = fc::ecc::private_key::regenerate(fc::sha256::hash(dev_key_prefix + account_name));
87  chain::public_key_type gen_pub = gen_priv.get_public_key();
88  key_table[gen_pub] = gen_priv;
89  if ((pubkey == scorum::chain::public_key_type()) || (gen_pub == pubkey))
90  {
91  result = gen_priv;
92  return;
93  }
94 
95  result.reset();
96  return;
97 }
98 
100  : app(_app)
101 {
102 #ifdef SCORUM_INIT_PRIVATE_KEY
103  fc::ecc::private_key init_key = SCORUM_INIT_PRIVATE_KEY;
104  key_storage.key_table[init_key.get_public_key()] = init_key;
105 #endif
106 }
107 
109 {
110  key_storage.dev_key_prefix = prefix;
111  return;
112 }
113 
115 {
116  fc::ecc::private_key priv
117  = fc::ecc::private_key::regenerate(fc::sha256::hash(key_storage.dev_key_prefix + args.name));
118  result.private_key = graphene::utilities::key_to_wif(priv);
119  result.public_key = priv.get_public_key();
120  return;
121 }
122 
123 uint32_t
124 debug_node_api_impl::debug_push_blocks(const std::string& src_filename, uint32_t count, bool skip_validate_invariants)
125 {
126  if (count == 0)
127  return 0;
128 
129  std::shared_ptr<scorum::chain::database> db = app.chain_database();
130  fc::path src_path = fc::path(src_filename);
131  fc::path index_path = fc::path(src_filename + ".index");
132  if (fc::exists(src_path) && fc::exists(index_path) && !fc::is_directory(src_path) && !fc::is_directory(index_path))
133  {
134  ilog("Loading ${n} from block_log ${fn}", ("n", count)("fn", src_filename));
135  idump((src_filename)(count)(skip_validate_invariants));
137  log.open(src_path);
138  uint32_t first_block = db->head_block_num() + 1;
139  uint32_t skip_flags = scorum::chain::database::skip_nothing;
140  if (skip_validate_invariants)
141  skip_flags = skip_flags | scorum::chain::database::skip_validate_invariants;
142  for (uint32_t i = 0; i < count; i++)
143  {
144  // fc::optional< scorum::chain::signed_block > block = log.read_block( log.get_block_pos( first_block + i )
145  // );
146  uint64_t block_pos = log.get_block_pos(first_block + i);
147  if (block_pos == scorum::chain::block_log::npos)
148  {
149  wlog("Block database ${fn} only contained ${i} of ${n} requested blocks",
150  ("i", i)("n", count)("fn", src_filename));
151  return i;
152  }
153 
154  decltype(log.read_block(0)) result;
155 
156  try
157  {
158  result = log.read_block(block_pos);
159  }
160  catch (const fc::exception& e)
161  {
162  elog("Could not read block ${i} of ${n}", ("i", i)("n", count));
163  continue;
164  }
165 
166  try
167  {
168  db->push_block(result.first, skip_flags);
169  }
170  catch (const fc::exception& e)
171  {
172  elog("Got exception pushing block ${bn} : ${bid} (${i} of ${n})",
173  ("bn", result.first.block_num())("bid", result.first.id())("i", i)("n", count));
174  elog("Exception backtrace: ${bt}", ("bt", e.to_detail_string()));
175  }
176  }
177  ilog("Completed loading block_database successfully");
178  return count;
179  }
180  return 0;
181 }
182 
183 uint32_t debug_node_api_impl::debug_generate_blocks(const std::string& debug_key, uint32_t count)
184 {
185  return get_plugin()->debug_generate_blocks(debug_key, count, scorum::chain::database::skip_nothing, 0,
186  &key_storage);
187 }
188 
189 uint32_t debug_node_api_impl::debug_generate_blocks_until(const std::string& debug_key,
190  const fc::time_point_sec& head_block_time,
191  bool generate_sparsely)
192 {
193  return get_plugin()->debug_generate_blocks_until(debug_key, head_block_time, generate_sparsely,
195 }
196 
197 fc::optional<scorum::chain::signed_block> debug_node_api_impl::debug_pop_block()
198 {
199  std::shared_ptr<scorum::chain::database> db = app.chain_database();
200  return db->fetch_block_by_number(db->head_block_num());
201 }
202 
203 /*void debug_node_api_impl::debug_push_block( const scorum::chain::signed_block& block )
204 {
205  app.chain_database()->push_block( block );
206 }*/
207 
209 {
211 }
212 
213 scorum::chain::hardfork_property_object debug_node_api_impl::debug_get_hardfork_property_object()
214 {
216 }
217 
218 void debug_node_api_impl::debug_update_object(const fc::variant_object& update)
219 {
220  // get_plugin()->debug_update( update );
221 }
222 
223 /*fc::variant_object debug_node_api_impl::debug_get_edits()
224 {
225  fc::mutable_variant_object result;
226  get_plugin()->save_debug_updates( result );
227  return fc::variant_object( std::move( result ) );
228 }*/
229 
230 void debug_node_api_impl::debug_set_edits(const fc::variant_object& edits)
231 {
232  // get_plugin()->load_debug_updates( edits );
233 }
234 
235 std::shared_ptr<scorum::plugin::debug_node::debug_node_plugin> debug_node_api_impl::get_plugin()
236 {
237  return app.get_plugin<debug_node_plugin>("debug_node");
238 }
239 
240 void debug_node_api_impl::debug_stream_json_objects(const std::string& filename)
241 {
242  // get_plugin()->set_json_object_stream( filename );
243 }
244 
246 {
247  // get_plugin()->flush_json_object_stream();
248 }
249 
250 void debug_node_api_impl::debug_set_hardfork(uint32_t hardfork_id)
251 {
252  using namespace scorum::chain;
253 
254  if (hardfork_id > SCORUM_NUM_HARDFORKS)
255  return;
256 
257  get_plugin()->debug_update([=](database& db) { db.set_hardfork(hardfork_id, false); });
258 }
259 
260 bool debug_node_api_impl::debug_has_hardfork(uint32_t hardfork_id)
261 {
262  return app.chain_database()->get(scorum::chain::hardfork_property_id_type()).last_hardfork >= hardfork_id;
263 }
264 
265 } // detail
266 
268 {
269  my = std::make_shared<detail::debug_node_api_impl>(ctx.app);
270 }
271 
273 {
274 }
275 
276 uint32_t debug_node_api::debug_push_blocks(std::string source_filename, uint32_t count, bool skip_validate_invariants)
277 {
278  return my->debug_push_blocks(source_filename, count, skip_validate_invariants);
279 }
280 
281 uint32_t debug_node_api::debug_generate_blocks(std::string debug_key, uint32_t count)
282 {
283  return my->debug_generate_blocks(debug_key, count);
284 }
285 
286 uint32_t debug_node_api::debug_generate_blocks_until(std::string debug_key,
287  fc::time_point_sec head_block_time,
288  bool generate_sparsely)
289 {
290  return my->debug_generate_blocks_until(debug_key, head_block_time, generate_sparsely);
291 }
292 
293 fc::optional<scorum::chain::signed_block> debug_node_api::debug_pop_block()
294 {
295  return my->debug_pop_block();
296 }
297 
298 /*void debug_node_api::debug_push_block( scorum::chain::signed_block& block )
299 {
300  my->debug_push_block( block );
301 }*/
302 
304 {
305  return my->debug_get_witness_schedule();
306 }
307 
308 scorum::chain::hardfork_property_object debug_node_api::debug_get_hardfork_property_object()
309 {
310  return my->debug_get_hardfork_property_object();
311 }
312 
313 /*
314 void debug_node_api::debug_update_object( fc::variant_object update )
315 {
316  my->debug_update_object( update );
317 }
318 */
319 
320 /*
321 fc::variant_object debug_node_api::debug_get_edits()
322 {
323  return my->debug_get_edits();
324 }
325 */
326 
327 /*
328 void debug_node_api::debug_set_edits( fc::variant_object edits )
329 {
330  my->debug_set_edits(edits);
331 }
332 */
333 
335 {
336  my->debug_set_dev_key_prefix(prefix);
337 }
338 
340 {
342  my->debug_get_dev_key(result, args);
343  return result;
344 }
345 
346 /*
347 void debug_node_api::debug_stream_json_objects( std::string filename )
348 {
349  my->debug_stream_json_objects( filename );
350 }
351 
352 void debug_node_api::debug_stream_json_objects_flush()
353 {
354  my->debug_stream_json_objects_flush();
355 }
356 */
357 
358 void debug_node_api::debug_set_hardfork(uint32_t hardfork_id)
359 {
360  my->debug_set_hardfork(hardfork_id);
361 }
362 
363 bool debug_node_api::debug_has_hardfork(uint32_t hardfork_id)
364 {
365  return my->debug_has_hardfork(hardfork_id);
366 }
367 }
368 }
369 } // scorum::plugin::debug_node
std::shared_ptr< chain::database > chain_database() const
std::shared_ptr< abstract_plugin > get_plugin(const std::string &name) const
static const uint64_t npos
Definition: block_log.hpp:63
void open(const fc::path &file)
Definition: block_log.cpp:94
uint64_t get_block_pos(uint32_t block_num) const
Definition: block_log.cpp:255
std::pair< signed_block, uint64_t > read_block(uint64_t file_pos) const
Definition: block_log.cpp:223
tracks the blockchain state in an extensible manner
Definition: database.hpp:52
@ skip_validate_invariants
used to skip database invariant check on block application
Definition: database.hpp:90
void set_hardfork(uint32_t hardfork, bool process_now=true)
Definition: database.cpp:2137
debug_node_api(const scorum::app::api_context &ctx)
uint32_t debug_push_blocks(const std::string &src_filename, uint32_t count, bool skip_validate_invariants)
scorum::chain::hardfork_property_object debug_get_hardfork_property_object()
fc::optional< scorum::chain::signed_block > debug_pop_block()
void debug_get_dev_key(get_dev_key_result &result, const get_dev_key_args &args)
scorum::chain::witness_schedule_object debug_get_witness_schedule()
void debug_stream_json_objects(const std::string &filename)
uint32_t debug_generate_blocks_until(const std::string &debug_key, const fc::time_point_sec &head_block_time, bool generate_sparsely)
void debug_set_edits(const fc::variant_object &edits)
std::shared_ptr< scorum::plugin::debug_node::debug_node_plugin > get_plugin()
uint32_t debug_generate_blocks(const std::string &debug_key, uint32_t count)
void debug_update_object(const fc::variant_object &update)
std::map< scorum::chain::public_key_type, fc::ecc::private_key > key_table
virtual void maybe_get_private_key(fc::optional< fc::ecc::private_key > &result, const scorum::chain::public_key_type &pubkey, const std::string &account_name) override
scorum::chain::witness_schedule_object debug_get_witness_schedule()
fc::optional< scorum::chain::signed_block > debug_pop_block()
Pop a block from the blockchain, returning it.
get_dev_key_result debug_get_dev_key(get_dev_key_args args)
Get developer key. Use debug_set_key_prefix() to set a prefix if desired.
bool debug_has_hardfork(uint32_t hardfork_id)
void debug_set_dev_key_prefix(std::string prefix)
Set developer key prefix. This prefix only applies to the current API session. (Thus,...
uint32_t debug_generate_blocks(std::string debug_key, uint32_t count)
Generate blocks locally.
uint32_t debug_generate_blocks_until(std::string debug_key, fc::time_point_sec head_block_time, bool generate_sparsely=true)
Generate blocks locally until a specified head block time. Can generate them sparsely.
void debug_set_hardfork(uint32_t hardfork_id)
scorum::chain::hardfork_property_object debug_get_hardfork_property_object()
uint32_t debug_push_blocks(std::string src_filename, uint32_t count, bool skip_validate_invariants=false)
Push blocks from existing database.
void update(fc::flat_map< uuid_type, bet_resolved_operation > &results, const bet_data &bet, asset income, uuid_type game_uuid, bet_resolve_kind kind)
Definition: asset.cpp:15