Debian Codegraph @

src/blockchain_utilities/blockchain_stats.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include <boost/algorithm/string.hpp>
30 #include <boost/filesystem.hpp>
31 #include "common/command_line.h"
32 #include "common/varint.h"
33 #include "cryptonote_basic/cryptonote_boost_serialization.h"
34 #include "cryptonote_core/tx_pool.h"
35 #include "cryptonote_core/cryptonote_core.h"
36 #include "cryptonote_core/blockchain.h"
37 #include "blockchain_db/blockchain_db.h"
38 #include "version.h"
39
40 #undef MONERO_DEFAULT_LOG_CATEGORY
41 #define MONERO_DEFAULT_LOG_CATEGORY "bcutil"
42
43 namespace po = boost::program_options;
44 using namespace epee;
45 using namespace cryptonote;
46
47 static bool stop_requested = false;
48
49 static bool do_inputs, do_outputs, do_ringsize, do_hours, do_emission, do_fees, do_diff;
50
51 static struct tm prevtm, currtm;
52 static uint64_t prevsz, currsz;
53 static uint64_t prevtxs, currtxs;
54 static uint64_t currblks;
55 static uint64_t h;
56 static uint64_t totins, totouts, totrings;
57 static boost::multiprecision::uint128_t prevemission, prevfees;
58 static boost::multiprecision::uint128_t emission, fees;
59 static boost::multiprecision::uint128_t totdiff, mindiff, maxdiff;
60
61 #define MAX_INOUT 0xffffffff
62 #define MAX_RINGS 0xffffffff
63
64 static uint32_t minins = MAX_INOUT, maxins;
65 static uint32_t minouts = MAX_INOUT, maxouts;
66 static uint32_t minrings = MAX_RINGS, maxrings;
67 static uint32_t io, tottxs;
68 static uint32_t txhr[24];
69
70 static void doprint()
71 {
72 char timebuf[64];
73
74 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d", &prevtm);
75 prevtm = currtm;
76 std::cout << timebuf << "\t" << currblks << "\t" << h << "\t" << currtxs << "\t" << prevtxs + currtxs << "\t" << currsz << "\t" << prevsz + currsz;
77 prevsz += currsz;
78 currsz = 0;
79 prevtxs += currtxs;
80 currtxs = 0;
81 if (!tottxs)
82 tottxs = 1;
83 if (do_emission) {
84 std::cout << "\t" << print_money(emission) << "\t" << print_money(prevemission + emission);
85 prevemission += emission;
86 emission = 0;
87 }
88 if (do_fees) {
89 std::cout << "\t" << print_money(fees) << "\t" << print_money(prevfees + fees);
90 prevfees += fees;
91 fees = 0;
92 }
93 if (do_diff) {
94 std::cout << "\t" << (maxdiff ? mindiff : 0) << "\t" << maxdiff << "\t" << totdiff / currblks;
95 mindiff = 0; maxdiff = 0; totdiff = 0;
96 }
97 if (do_inputs) {
98 std::cout << "\t" << (maxins ? minins : 0) << "\t" << maxins << "\t" << totins * 1.0 / tottxs;
99 minins = MAX_INOUT; maxins = 0; totins = 0;
100 }
101 if (do_outputs) {
102 std::cout << "\t" << (maxouts ? minouts : 0) << "\t" << maxouts << "\t" << totouts * 1.0 / tottxs;
103 minouts = MAX_INOUT; maxouts = 0; totouts = 0;
104 }
105 if (do_ringsize) {
106 std::cout << "\t" << (maxrings ? minrings : 0) << "\t" << maxrings << "\t" << totrings * 1.0 / tottxs;
107 minrings = MAX_RINGS; maxrings = 0; totrings = 0;
108 }
109 if (do_hours) {
110 for (int i=0; i<24; i++) {
111 std::cout << "\t" << txhr[i];
112 txhr[i] = 0;
113 }
114 }
115 currblks = 0;
116 tottxs = 0;
117 std::cout << ENDL;
118 }
119
120 int main(int argc, char* argv[])
121 {
122 TRY_ENTRY();
123
124 epee::string_tools::set_module_name_and_folder(argv[0]);
125
126 uint32_t log_level = 0;
127 uint64_t block_start = 0;
128 uint64_t block_stop = 0;
129
130 tools::on_startup();
131
132 boost::filesystem::path output_file_path;
133
134 po::options_description desc_cmd_only("Command line options");
135 po::options_description desc_cmd_sett("Command line options and settings options");
136 const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
137 const command_line::arg_descriptor<uint64_t> arg_block_start = {"block-start", "start at block number", block_start};
138 const command_line::arg_descriptor<uint64_t> arg_block_stop = {"block-stop", "Stop at block number", block_stop};
139 const command_line::arg_descriptor<bool> arg_inputs = {"with-inputs", "with input stats", false};
140 const command_line::arg_descriptor<bool> arg_outputs = {"with-outputs", "with output stats", false};
141 const command_line::arg_descriptor<bool> arg_ringsize = {"with-ringsize", "with ringsize stats", false};
142 const command_line::arg_descriptor<bool> arg_hours = {"with-hours", "with txns per hour", false};
143 const command_line::arg_descriptor<bool> arg_emission = {"with-emission", "with coin emission", false};
144 const command_line::arg_descriptor<bool> arg_fees = {"with-fees", "with txn fees", false};
145 const command_line::arg_descriptor<bool> arg_diff = {"with-diff", "with difficulty", false};
146
147 command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir);
148 command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
149 command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
150 command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on);
151 command_line::add_arg(desc_cmd_sett, arg_log_level);
152 command_line::add_arg(desc_cmd_sett, arg_block_start);
153 command_line::add_arg(desc_cmd_sett, arg_block_stop);
154 command_line::add_arg(desc_cmd_sett, arg_inputs);
155 command_line::add_arg(desc_cmd_sett, arg_outputs);
156 command_line::add_arg(desc_cmd_sett, arg_ringsize);
157 command_line::add_arg(desc_cmd_sett, arg_hours);
158 command_line::add_arg(desc_cmd_sett, arg_emission);
159 command_line::add_arg(desc_cmd_sett, arg_fees);
160 command_line::add_arg(desc_cmd_sett, arg_diff);
161 command_line::add_arg(desc_cmd_only, command_line::arg_help);
162
163 po::options_description desc_options("Allowed options");
164 desc_options.add(desc_cmd_only).add(desc_cmd_sett);
165
166 po::variables_map vm;
167 bool r = command_line::handle_error_helper(desc_options, [&]()
168 {
169 auto parser = po::command_line_parser(argc, argv).options(desc_options);
170 po::store(parser.run(), vm);
171 po::notify(vm);
172 return true;
173 });
174 if (! r)
175 return 1;
176
177 if (command_line::get_arg(vm, command_line::arg_help))
178 {
179 std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL;
180 std::cout << desc_options << std::endl;
181 return 1;
182 }
183
184 mlog_configure(mlog_get_default_log_path("monero-blockchain-stats.log"), true);
185 if (!command_line::is_arg_defaulted(vm, arg_log_level))
186 mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str());
187 else
188 mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str());
189
190 LOG_PRINT_L0("Starting...");
191
192 std::string opt_data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir);
193 const network_type net_type = core::get_network_type_from_args(vm);
194 block_start = command_line::get_arg(vm, arg_block_start);
195 block_stop = command_line::get_arg(vm, arg_block_stop);
196 do_inputs = command_line::get_arg(vm, arg_inputs);
197 do_outputs = command_line::get_arg(vm, arg_outputs);
198 do_ringsize = command_line::get_arg(vm, arg_ringsize);
199 do_hours = command_line::get_arg(vm, arg_hours);
200 do_emission = command_line::get_arg(vm, arg_emission);
201 do_fees = command_line::get_arg(vm, arg_fees);
202 do_diff = command_line::get_arg(vm, arg_diff);
203
204 LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
205 std::unique_ptr<Blockchain> core_storage;
206 tx_memory_pool m_mempool(*core_storage);
207 core_storage.reset(new Blockchain(m_mempool));
208 BlockchainDB *db = new_db();
209 if (db == NULL)
210 {
211 LOG_ERROR("Failed to initialize a database");
212 throw std::runtime_error("Failed to initialize a database");
213 }
214
215 const std::string filename = (boost::filesystem::path(opt_data_dir) / db->get_db_name()).string();
216 LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
217
218 try
219 {
220 db->open(filename, DBF_RDONLY);
221 }
222 catch (const std::exception& e)
223 {
224 LOG_PRINT_L0("Error opening database: " << e.what());
225 return 1;
226 }
227 r = core_storage->init(db, net_type);
228
229 CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage");
230 LOG_PRINT_L0("Source blockchain storage initialized OK");
231
232 tools::signal_handler::install([](int type) {
233 stop_requested = true;
234 });
235
236 const uint64_t db_height = db->height();
237 if (!block_stop)
238 block_stop = db_height;
239 MINFO("Starting from height " << block_start << ", stopping at height " << block_stop);
240
241 /*
242 * The default output can be plotted with GnuPlot using these commands:
243 set key autotitle columnhead
244 set title "Monero Blockchain Growth"
245 set timefmt "%Y-%m-%d"
246 set xdata time
247 set xrange ["2014-04-17":*]
248 set format x "%Y-%m-%d"
249 set yrange [0:*]
250 set y2range [0:*]
251 set ylabel "Txs/Day"
252 set y2label "Bytes"
253 set y2tics nomirror
254 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
255 */
256
257 // spit out a comment that GnuPlot can use as an index
258 std::cout << ENDL << "# DATA" << ENDL;
259 std::cout << "Date\tBlocks/day\tBlocks\tTxs/Day\tTxs\tBytes/Day\tBytes";
260 if (do_emission)
261 std::cout << "\tEmission/day\tEmission";
262 if (do_fees)
263 std::cout << "\tFees/day\tFees";
264 if (do_diff)
265 std::cout << "\tDiffMin\tDiffMax\tDiffAvg";
266 if (do_inputs)
267 std::cout << "\tInMin\tInMax\tInAvg";
268 if (do_outputs)
269 std::cout << "\tOutMin\tOutMax\tOutAvg";
270 if (do_ringsize)
271 std::cout << "\tRingMin\tRingMax\tRingAvg";
272 if (do_inputs || do_outputs || do_ringsize)
273 std::cout << std::setprecision(2) << std::fixed;
274 if (do_hours) {
275 char buf[8];
276 unsigned int i;
277 for (i=0; i<24; i++) {
278 sprintf(buf, "\t%02u:00", i);
279 std::cout << buf;
280 }
281 }
282 std::cout << ENDL;
283
284 for (h = block_start; h < block_stop; ++h)
285 {
286 cryptonote::blobdata bd = db->get_block_blob_from_height(h);
287 cryptonote::block blk;
288 if (!cryptonote::parse_and_validate_block_from_blob(bd, blk))
289 {
290 LOG_PRINT_L0("Bad block from db");
291 return 1;
292 }
293 time_t tt = blk.timestamp;
294 epee::misc_utils::get_gmt_time(tt, currtm);
295 if (!prevtm.tm_year)
296 prevtm = currtm;
297 // catch change of day
298 if (currtm.tm_mday > prevtm.tm_mday || (currtm.tm_mday == 1 && prevtm.tm_mday > 27))
299 {
300 // check for timestamp fudging around month ends
301 if (!(prevtm.tm_mday == 1 && currtm.tm_mday > 27))
302 doprint();
303 }
304 currsz += bd.size();
305 uint64_t coinbase_amount;
306 uint64_t tx_fee_amount = 0;
307 for (const auto& tx_id : blk.tx_hashes)
308 {
309 if (tx_id == crypto::null_hash)
310 {
311 throw std::runtime_error("Aborting: tx == null_hash");
312 }
313 if (!db->get_pruned_tx_blob(tx_id, bd))
314 {
315 throw std::runtime_error("Aborting: tx not found");
316 }
317 transaction tx;
318 if (!parse_and_validate_tx_base_from_blob(bd, tx))
319 {
320 LOG_PRINT_L0("Bad txn from db");
321 return 1;
322 }
323 currsz += bd.size();
324 if (db->get_prunable_tx_blob(tx_id, bd))
325 currsz += bd.size();
326 currtxs++;
327 if (do_fees || do_emission) {
328 tx_fee_amount += get_tx_fee(tx);
329 }
330 if (do_hours)
331 txhr[currtm.tm_hour]++;
332 if (do_inputs) {
333 io = tx.vin.size();
334 if (io < minins)
335 minins = io;
336 else if (io > maxins)
337 maxins = io;
338 totins += io;
339 }
340 if (do_ringsize) {
341 const cryptonote::txin_to_key& tx_in_to_key
342 = boost::get<cryptonote::txin_to_key>(tx.vin[0]);
343 io = tx_in_to_key.key_offsets.size();
344 if (io < minrings)
345 minrings = io;
346 else if (io > maxrings)
347 maxrings = io;
348 totrings += io;
349 }
350 if (do_outputs) {
351 io = tx.vout.size();
352 if (io < minouts)
353 minouts = io;
354 else if (io > maxouts)
355 maxouts = io;
356 totouts += io;
357 }
358 tottxs++;
359 }
360 if (do_diff) {
361 difficulty_type diff = db->get_block_difficulty(h);
362 if (!mindiff || diff < mindiff)
363 mindiff = diff;
364 if (diff > maxdiff)
365 maxdiff = diff;
366 totdiff += diff;
367 }
368 if (do_emission) {
369 coinbase_amount = get_outs_money_amount(blk.miner_tx);
370 emission += coinbase_amount - tx_fee_amount;
371 }
372 if (do_fees) {
373 fees += tx_fee_amount;
374 }
375 currblks++;
376
377 if (stop_requested)
378 break;
379 }
380 if (currblks)
381 doprint();
382
383 core_storage->deinit();
384 return 0;
385
386 CATCH_ENTRY("Stats reporting error", 1);
387 }