BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
debug_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 
7 
11 
13 
16 
17 namespace graphene { namespace debug_witness {
18 
19 namespace detail {
20 
22 {
23  public:
25 
26  void debug_push_blocks( const std::string& src_filename, uint32_t count );
27  void debug_generate_blocks( const std::string& debug_key, uint32_t count );
28  void debug_update_object( const fc::variant_object& update );
29  void debug_stream_json_objects( const std::string& filename );
31  std::shared_ptr< graphene::debug_witness_plugin::debug_witness_plugin > get_plugin();
32 
34 };
35 
37 {}
38 
39 
40 void debug_api_impl::debug_push_blocks( const std::string& src_filename, uint32_t count )
41 {
42  if( count == 0 )
43  return;
44 
45  std::shared_ptr< graphene::chain::database > db = app.chain_database();
46  fc::path src_path = fc::path( src_filename );
47  if( fc::is_directory( src_path ) )
48  {
49  ilog( "Loading ${n} from block_database ${fn}", ("n", count)("fn", src_filename) );
51  bdb.open( src_path );
52  uint32_t first_block = db->head_block_num()+1;
53  for( uint32_t i=0; i<count; i++ )
54  {
56  if( !block.valid() )
57  {
58  wlog( "Block database ${fn} only contained ${i} of ${n} requested blocks", ("i", i)("n", count)("fn", src_filename) );
59  return;
60  }
61  try
62  {
63  db->push_block( *block );
64  }
65  catch( const fc::exception& e )
66  {
67  elog( "Got exception pushing block ${bn} : ${bid} (${i} of ${n})", ("bn", block->block_num())("bid", block->id())("i", i)("n", count) );
68  elog( "Exception backtrace: ${bt}", ("bt", e.to_detail_string()) );
69  }
70  }
71  ilog( "Completed loading block_database successfully" );
72  }
73 }
74 
75 void debug_api_impl::debug_generate_blocks( const std::string& debug_key, uint32_t count )
76 {
77  if( count == 0 )
78  return;
79 
81  FC_ASSERT( debug_private_key.valid() );
82  graphene::chain::public_key_type debug_public_key = debug_private_key->get_public_key();
83 
84  std::shared_ptr< graphene::chain::database > db = app.chain_database();
85  for( uint32_t i=0; i<count; i++ )
86  {
87  graphene::chain::witness_id_type scheduled_witness = db->get_scheduled_witness( 1 );
88  fc::time_point_sec scheduled_time = db->get_slot_time( 1 );
89  graphene::chain::public_key_type scheduled_key = scheduled_witness( *db ).signing_key;
90  if( scheduled_key != debug_public_key )
91  {
92  ilog( "Modified key for witness ${w}", ("w", scheduled_witness) );
94  update("_action", "update")("id", scheduled_witness)("signing_key", debug_public_key);
95  db->debug_update( update );
96  }
97  db->generate_block( scheduled_time, scheduled_witness, *debug_private_key, graphene::chain::database::skip_nothing );
98  }
99 }
100 
102 {
103  std::shared_ptr< graphene::chain::database > db = app.chain_database();
104  db->debug_update( update );
105 }
106 
107 std::shared_ptr< graphene::debug_witness_plugin::debug_witness_plugin > debug_api_impl::get_plugin()
108 {
110 }
111 
112 void debug_api_impl::debug_stream_json_objects( const std::string& filename )
113 {
114  get_plugin()->set_json_object_stream( filename );
115 }
116 
118 {
119  get_plugin()->flush_json_object_stream();
120 }
121 
122 } // detail
123 
125 {
126  my = std::make_shared< detail::debug_api_impl >(app);
127 }
128 
129 void debug_api::debug_push_blocks( std::string source_filename, uint32_t count )
130 {
131  my->debug_push_blocks( source_filename, count );
132 }
133 
134 void debug_api::debug_generate_blocks( std::string debug_key, uint32_t count )
135 {
136  my->debug_generate_blocks( debug_key, count );
137 }
138 
140 {
141  my->debug_update_object( update );
142 }
143 
144 void debug_api::debug_stream_json_objects( std::string filename )
145 {
146  my->debug_stream_json_objects( filename );
147 }
148 
150 {
151  my->debug_stream_json_objects_flush();
152 }
153 
154 
155 } } // graphene::debug_witness
void debug_update_object(const fc::variant_object &update)
Definition: debug_api.cpp:101
void debug_generate_blocks(std::string debug_key, uint32_t count)
Definition: debug_api.cpp:134
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:31
void debug_push_blocks(std::string src_filename, uint32_t count)
Definition: debug_api.cpp:129
An order-perserving dictionary of variant&#39;s.
Definition: api.cpp:56
#define elog(FORMAT,...)
Definition: logger.hpp:129
fc::optional< fc::ecc::private_key > wif_to_key(const std::string &wif_key)
std::shared_ptr< graphene::debug_witness_plugin::debug_witness_plugin > get_plugin()
Definition: debug_api.cpp:107
debug_api(graphene::app::application &app)
Definition: debug_api.cpp:124
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
bool valid() const
Definition: optional.hpp:186
void debug_push_blocks(const std::string &src_filename, uint32_t count)
Definition: debug_api.cpp:40
debug_api_impl(graphene::app::application &_app)
Definition: debug_api.cpp:36
void open(const fc::path &dbdir)
#define wlog(FORMAT,...)
Definition: logger.hpp:123
public_key get_public_key() const
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
std::shared_ptr< abstract_plugin > get_plugin(const string &name) const
void debug_update_object(fc::variant_object update)
Definition: debug_api.cpp:139
#define ilog(FORMAT,...)
Definition: logger.hpp:117
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
bool is_directory(const path &p)
Definition: filesystem.cpp:217
void debug_generate_blocks(const std::string &debug_key, uint32_t count)
Definition: debug_api.cpp:75
void debug_stream_json_objects(std::string filename)
Definition: debug_api.cpp:144
uint32_t block_num() const
Definition: block.hpp:34
optional< signed_block > fetch_by_number(uint32_t block_num) const
const block_id_type & id() const
Definition: block.cpp:41
std::shared_ptr< chain::database > chain_database() const
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
void debug_stream_json_objects(const std::string &filename)
Definition: debug_api.cpp:112