Electroneum
blockchain_stats.cpp File Reference
Include dependency graph for blockchain_stats.cpp:

Go to the source code of this file.

Macros

#define ELECTRONEUM_DEFAULT_LOG_CATEGORY   "bcutil"
 

Functions

int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ ELECTRONEUM_DEFAULT_LOG_CATEGORY

#define ELECTRONEUM_DEFAULT_LOG_CATEGORY   "bcutil"

Definition at line 41 of file blockchain_stats.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 49 of file blockchain_stats.cpp.

50 {
51  TRY_ENTRY();
52 
54 
55  std::string default_db_type = "lmdb";
56 
57  std::string available_dbs = cryptonote::blockchain_db_types(", ");
58  available_dbs = "available: " + available_dbs;
59 
60  uint32_t log_level = 0;
61  uint64_t block_start = 0;
62  uint64_t block_stop = 0;
63 
65 
66  boost::filesystem::path output_file_path;
67 
68  po::options_description desc_cmd_only("Command line options");
69  po::options_description desc_cmd_sett("Command line options and settings options");
70  const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
71  const command_line::arg_descriptor<std::string> arg_database = {
72  "database", available_dbs.c_str(), default_db_type
73  };
74  const command_line::arg_descriptor<uint64_t> arg_block_start = {"block-start", "start at block number", block_start};
75  const command_line::arg_descriptor<uint64_t> arg_block_stop = {"block-stop", "Stop at block number", block_stop};
76  const command_line::arg_descriptor<bool> arg_inputs = {"with-inputs", "with input stats", false};
77  const command_line::arg_descriptor<bool> arg_outputs = {"with-outputs", "with output stats", false};
78  const command_line::arg_descriptor<bool> arg_ringsize = {"with-ringsize", "with ringsize stats", false};
79  const command_line::arg_descriptor<bool> arg_hours = {"with-hours", "with txns per hour", false};
80 
84  command_line::add_arg(desc_cmd_sett, arg_log_level);
85  command_line::add_arg(desc_cmd_sett, arg_database);
86  command_line::add_arg(desc_cmd_sett, arg_block_start);
87  command_line::add_arg(desc_cmd_sett, arg_block_stop);
88  command_line::add_arg(desc_cmd_sett, arg_inputs);
89  command_line::add_arg(desc_cmd_sett, arg_outputs);
90  command_line::add_arg(desc_cmd_sett, arg_ringsize);
91  command_line::add_arg(desc_cmd_sett, arg_hours);
93 
94  po::options_description desc_options("Allowed options");
95  desc_options.add(desc_cmd_only).add(desc_cmd_sett);
96 
97  po::variables_map vm;
98  bool r = command_line::handle_error_helper(desc_options, [&]()
99  {
100  auto parser = po::command_line_parser(argc, argv).options(desc_options);
101  po::store(parser.run(), vm);
102  po::notify(vm);
103  return true;
104  });
105  if (! r)
106  return 1;
107 
109  {
110  std::cout << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")" << ENDL << ENDL;
111  std::cout << desc_options << std::endl;
112  return 1;
113  }
114 
115  mlog_configure(mlog_get_default_log_path("electroneum-blockchain-stats.log"), true);
118  else
119  mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str());
120 
121  LOG_PRINT_L0("Starting...");
122 
124  bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on);
125  bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on);
126  network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET;
127  block_start = command_line::get_arg(vm, arg_block_start);
128  block_stop = command_line::get_arg(vm, arg_block_stop);
129  bool do_inputs = command_line::get_arg(vm, arg_inputs);
130  bool do_outputs = command_line::get_arg(vm, arg_outputs);
131  bool do_ringsize = command_line::get_arg(vm, arg_ringsize);
132  bool do_hours = command_line::get_arg(vm, arg_hours);
133 
134  std::string db_type = command_line::get_arg(vm, arg_database);
136  {
137  std::cerr << "Invalid database type: " << db_type << std::endl;
138  return 1;
139  }
140 
141  LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
142  std::unique_ptr<Blockchain> core_storage;
143  tx_memory_pool m_mempool(*core_storage);
144  core_storage.reset(new Blockchain(m_mempool));
145  BlockchainDB *db = new_db(db_type);
146  if (db == NULL)
147  {
148  LOG_ERROR("Attempted to use non-existent database type: " << db_type);
149  throw std::runtime_error("Attempting to use non-existent database type");
150  }
151  LOG_PRINT_L0("database: " << db_type);
152 
153  const std::string filename = (boost::filesystem::path(opt_data_dir) / db->get_db_name()).string();
154  LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
155 
156  try
157  {
158  db->open(filename, DBF_RDONLY);
159  }
160  catch (const std::exception& e)
161  {
162  LOG_PRINT_L0("Error opening database: " << e.what());
163  return 1;
164  }
165  r = core_storage->init(db, net_type);
166 
167  CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage");
168  LOG_PRINT_L0("Source blockchain storage initialized OK");
169 
170  tools::signal_handler::install([](int type) {
171  stop_requested = true;
172  });
173 
174  const uint64_t db_height = db->height();
175  if (!block_stop)
176  block_stop = db_height;
177  MINFO("Starting from height " << block_start << ", stopping at height " << block_stop);
178 
179 /*
180  * The default output can be plotted with GnuPlot using these commands:
181 set key autotitle columnhead
182 set title "Electroneum Blockchain Growth"
183 set timefmt "%Y-%m-%d"
184 set xdata time
185 set xrange ["2014-04-17":*]
186 set format x "%Y-%m-%d"
187 set yrange [0:*]
188 set y2range [0:*]
189 set ylabel "Txs/Day"
190 set y2label "Bytes"
191 set y2tics nomirror
192 plot 'stats.csv' index "DATA" using (timecolumn(1,"%Y-%m-%d")):4 with lines, '' using (timecolumn(1,"%Y-%m-%d")):7 axes x1y2 with lines
193  */
194 
195  // spit out a comment that GnuPlot can use as an index
196  std::cout << ENDL << "# DATA" << ENDL;
197  std::cout << "Date\tBlocks/day\tBlocks\tTxs/Day\tTxs\tBytes/Day\tBytes";
198  if (do_inputs)
199  std::cout << "\tInMin\tInMax\tInAvg";
200  if (do_outputs)
201  std::cout << "\tOutMin\tOutMax\tOutAvg";
202  if (do_ringsize)
203  std::cout << "\tRingMin\tRingMax\tRingAvg";
204  if (do_hours) {
205  char buf[8];
206  unsigned int i;
207  for (i=0; i<24; i++) {
208  sprintf(buf, "\t%02u:00", i);
209  std::cout << buf;
210  }
211  }
212  std::cout << ENDL;
213 
214  struct tm prevtm = {0}, currtm;
215  uint64_t prevsz = 0, currsz = 0;
216  uint64_t prevtxs = 0, currtxs = 0;
217  uint64_t currblks = 0;
218  uint64_t totins = 0, totouts = 0, totrings = 0;
219  uint32_t minins = 10, maxins = 0;
220  uint32_t minouts = 10, maxouts = 0;
221  uint32_t minrings = 50, maxrings = 0;
222  uint32_t io, tottxs = 0;
223  uint32_t txhr[24] = {0};
224  unsigned int i;
225 
226  for (uint64_t h = block_start; h < block_stop; ++h)
227  {
229  cryptonote::block blk;
231  {
232  LOG_PRINT_L0("Bad block from db");
233  return 1;
234  }
235  time_t tt = blk.timestamp;
236  char timebuf[64];
237  epee::misc_utils::get_gmt_time(tt, currtm);
238  if (!prevtm.tm_year)
239  prevtm = currtm;
240  // catch change of day
241  if (currtm.tm_mday > prevtm.tm_mday || (currtm.tm_mday == 1 && prevtm.tm_mday > 27))
242  {
243  // check for timestamp fudging around month ends
244  if (prevtm.tm_mday == 1 && currtm.tm_mday > 27)
245  goto skip;
246  strftime(timebuf, sizeof(timebuf), "%Y-%m-%d", &prevtm);
247  prevtm = currtm;
248  std::cout << timebuf << "\t" << currblks << "\t" << h << "\t" << currtxs << "\t" << prevtxs + currtxs << "\t" << currsz << "\t" << prevsz + currsz;
249  prevsz += currsz;
250  currsz = 0;
251  currblks = 0;
252  prevtxs += currtxs;
253  currtxs = 0;
254  if (!tottxs)
255  tottxs = 1;
256  if (do_inputs) {
257  std::cout << "\t" << (maxins ? minins : 0) << "\t" << maxins << "\t" << totins / tottxs;
258  minins = 10; maxins = 0; totins = 0;
259  }
260  if (do_outputs) {
261  std::cout << "\t" << (maxouts ? minouts : 0) << "\t" << maxouts << "\t" << totouts / tottxs;
262  minouts = 10; maxouts = 0; totouts = 0;
263  }
264  if (do_ringsize) {
265  std::cout << "\t" << (maxrings ? minrings : 0) << "\t" << maxrings << "\t" << totrings / tottxs;
266  minrings = 50; maxrings = 0; totrings = 0;
267  }
268  tottxs = 0;
269  if (do_hours) {
270  for (i=0; i<24; i++) {
271  std::cout << "\t" << txhr[i];
272  txhr[i] = 0;
273  }
274  }
275  std::cout << ENDL;
276  }
277 skip:
278  currsz += bd.size();
279  for (const auto& tx_id : blk.tx_hashes)
280  {
281  if (tx_id == crypto::null_hash)
282  {
283  throw std::runtime_error("Aborting: tx == null_hash");
284  }
285  if (!db->get_tx_blob(tx_id, bd))
286  {
287  throw std::runtime_error("Aborting: tx not found");
288  }
289  transaction tx;
290  if (!parse_and_validate_tx_from_blob(bd, tx))
291  {
292  LOG_PRINT_L0("Bad txn from db");
293  return 1;
294  }
295  currsz += bd.size();
296  currtxs++;
297  if (do_hours)
298  txhr[currtm.tm_hour]++;
299  if (do_inputs) {
300  io = tx.vin.size();
301  if (io < minins)
302  minins = io;
303  else if (io > maxins)
304  maxins = io;
305  totins += io;
306  }
307  if (do_ringsize) {
308  const cryptonote::txin_to_key& tx_in_to_key
309  = boost::get<cryptonote::txin_to_key>(tx.vin[0]);
310  io = tx_in_to_key.key_offsets.size();
311  if (io < minrings)
312  minrings = io;
313  else if (io > maxrings)
314  maxrings = io;
315  totrings += io;
316  }
317  if (do_outputs) {
318  io = tx.vout.size();
319  if (io < minouts)
320  minouts = io;
321  else if (io > maxouts)
322  maxouts = io;
323  totouts += io;
324  }
325  tottxs++;
326  }
327  currblks++;
328 
329  if (stop_requested)
330  break;
331  }
332 
333  core_storage->deinit();
334  return 0;
335 
336  CATCH_ENTRY("Stats reporting error", 1);
337 }
const char *const ELECTRONEUM_RELEASE_NAME
virtual bool get_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the transaction blob with the given hash
std::vector< crypto::hash > tx_hashes
virtual std::string get_db_name() const =0
gets the name of the folder the BlockchainDB&#39;s file(s) should be in
#define DBF_RDONLY
#define MINFO(x)
Definition: misc_log_ex.h:75
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const =0
fetch a block blob by height
bool set_module_name_and_folder(const std::string &path_to_process_)
Definition: string_tools.h:249
::std::string string
Definition: gtest-port.h:1097
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
void mlog_set_log(const char *log)
Definition: mlog.cpp:288
#define CATCH_ENTRY(location, return_val)
Definition: misc_log_ex.h:152
std::string mlog_get_default_log_path(const char *default_filename)
Definition: mlog.cpp:72
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
Definition: mlog.cpp:148
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
const command_line::arg_descriptor< bool, false > arg_stagenet_on
const arg_descriptor< bool > arg_help
std::vector< uint64_t > key_offsets
std::string blockchain_db_types(const std::string &sep)
virtual void open(const std::string &filename, const int db_flags=0)=0
open a db, or create it if necessary.
bool on_startup()
Definition: util.cpp:778
const char *const ELECTRONEUM_VERSION_FULL
bool blockchain_valid_db_type(const std::string &db_type)
const command_line::arg_descriptor< bool, false > arg_testnet_on
BlockchainDB * new_db(const std::string &db_type)
bool handle_error_helper(const boost::program_options::options_description &desc, F parser)
Definition: command_line.h:237
#define TRY_ENTRY()
Definition: misc_log_ex.h:151
unsigned int uint32_t
Definition: stdint.h:126
const command_line::arg_descriptor< std::string > arg_log_level
bool get_gmt_time(time_t t, struct tm &tm)
unsigned __int64 uint64_t
Definition: stdint.h:136
const command_line::arg_descriptor< std::string, false, true, 2 > arg_data_dir
virtual uint64_t height() const =0
fetch the current blockchain height
bool parse_and_validate_tx_from_blob(const blobdata &tx_blob, transaction &tx)
const char * buf
Definition: slow_memmem.cpp:74
void add_arg(boost::program_options::options_description &description, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg, bool unique=true)
Definition: command_line.h:188
static bool install(T t)
installs a signal handler
Definition: util.h:164
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:94
std::string blobdata
Definition: blobdatatype.h:39
The BlockchainDB backing store interface declaration/contract.
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
#define ENDL
Definition: misc_log_ex.h:149
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
bool init(BlockchainDB *db, const network_type nettype=MAINNET, bool offline=false, const cryptonote::test_options *test_options=NULL, difficulty_type fixed_difficulty=0, const GetCheckpointsCallback &get_checkpoints=nullptr, bool ignore_bsig=false, bool fallback_to_pow=false)
Initialize the Blockchain state.
Definition: blockchain.cpp:334
std::string to_string(t_connection_type type)
bool is_arg_defaulted(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
Definition: command_line.h:265
bool deinit()
Uninitializes the blockchain state.
Definition: blockchain.cpp:660
bool parse_and_validate_block_from_blob(const blobdata &b_blob, block &b, crypto::hash *block_hash)
Here is the call graph for this function: