Electroneum
Loading...
Searching...
No Matches
blockchain_ancestry.cpp
Go to the documentation of this file.
1// Copyright (c) 2014-2019, 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 <unordered_map>
30#include <unordered_set>
31#include <boost/range/adaptor/transformed.hpp>
32#include <boost/algorithm/string.hpp>
36#include "common/command_line.h"
37#include "common/varint.h"
44#include "version.h"
45
46#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
47#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "bcutil"
48
49namespace po = boost::program_options;
50using namespace epee;
51using namespace cryptonote;
52
53static bool stop_requested = false;
54static uint64_t cached_txes = 0, cached_blocks = 0, cached_outputs = 0, total_txes = 0, total_blocks = 0, total_outputs = 0;
55static bool opt_cache_outputs = false, opt_cache_txes = false, opt_cache_blocks = false;
56
58{
61
62 bool operator==(const ancestor &other) const { return amount == other.amount && offset == other.offset; }
63
64 template <typename t_archive> void serialize(t_archive &a, const unsigned int ver)
65 {
66 a & amount;
67 a & offset;
68 }
69};
71
72namespace std
73{
74 template<> struct hash<ancestor>
75 {
76 size_t operator()(const ancestor &a) const
77 {
78 return a.amount ^ a.offset; // not that bad, since amount almost always have a high bit set, and offset doesn't
79 }
80 };
81}
82
84{
85 std::vector<std::pair<uint64_t, std::vector<uint64_t>>> vin;
86 std::vector<crypto::public_key> vout;
88
91 {
92 coinbase = tx.vin.size() == 1 && tx.vin[0].type() == typeid(cryptonote::txin_gen);
93 if (!coinbase)
94 {
95 vin.reserve(tx.vin.size());
96 for (size_t ring = 0; ring < tx.vin.size(); ++ring)
97 {
98 if (tx.vin[ring].type() == typeid(cryptonote::txin_to_key))
99 {
100 const cryptonote::txin_to_key &txin = boost::get<cryptonote::txin_to_key>(tx.vin[ring]);
101 vin.push_back(std::make_pair(txin.amount, cryptonote::relative_output_offsets_to_absolute(txin.key_offsets)));
102 }
103 else
104 {
105 LOG_PRINT_L0("Bad vin type in txid " << get_transaction_hash(tx));
106 throw std::runtime_error("Bad vin type");
107 }
108 }
109 }
110 vout.reserve(tx.vout.size());
111 for (size_t out = 0; out < tx.vout.size(); ++out)
112 {
113 if (tx.vout[out].target.type() == typeid(cryptonote::txout_to_key))
114 {
115 const auto &txout = boost::get<cryptonote::txout_to_key>(tx.vout[out].target);
116 vout.push_back(txout.key);
117 }
118 else
119 {
120 LOG_PRINT_L0("Bad vout type in txid " << get_transaction_hash(tx));
121 throw std::runtime_error("Bad vout type");
122 }
123 }
124 }
125
126 template <typename t_archive> void serialize(t_archive &a, const unsigned int ver)
127 {
128 a & coinbase;
129 a & vin;
130 a & vout;
131 }
132};
133
135{
137 std::unordered_map<crypto::hash, std::unordered_set<ancestor>> ancestry;
138 std::unordered_map<ancestor, crypto::hash> output_cache;
139 std::unordered_map<crypto::hash, ::tx_data_t> tx_cache;
140 std::vector<cryptonote::block> block_cache;
141
143
144 template <typename t_archive> void serialize(t_archive &a, const unsigned int ver)
145 {
146 a & height;
147 a & ancestry;
148 a & output_cache;
149 if (ver < 1)
150 {
151 std::unordered_map<crypto::hash, cryptonote::transaction> old_tx_cache;
152 a & old_tx_cache;
153 for (const auto i: old_tx_cache)
154 tx_cache.insert(std::make_pair(i.first, ::tx_data_t(i.second)));
155 }
156 else
157 {
158 a & tx_cache;
159 }
160 if (ver < 2)
161 {
162 std::unordered_map<uint64_t, cryptonote::block> old_block_cache;
163 a & old_block_cache;
164 block_cache.resize(old_block_cache.size());
165 for (const auto i: old_block_cache)
166 block_cache[i.first] = i.second;
167 }
168 else
169 {
170 a & block_cache;
171 }
172 }
173};
175
176static void add_ancestor(std::unordered_map<ancestor, unsigned int> &ancestry, uint64_t amount, uint64_t offset)
177{
178 std::pair<std::unordered_map<ancestor, unsigned int>::iterator, bool> p = ancestry.insert(std::make_pair(ancestor{amount, offset}, 1));
179 if (!p.second)
180 {
181 ++p.first->second;
182 }
183}
184
185static size_t get_full_ancestry(const std::unordered_map<ancestor, unsigned int> &ancestry)
186{
187 size_t count = 0;
188 for (const auto &i: ancestry)
189 count += i.second;
190 return count;
191}
192
193static size_t get_deduplicated_ancestry(const std::unordered_map<ancestor, unsigned int> &ancestry)
194{
195 return ancestry.size();
196}
197
198static void add_ancestry(std::unordered_map<crypto::hash, std::unordered_set<ancestor>> &ancestry, const crypto::hash &txid, const std::unordered_set<ancestor> &ancestors)
199{
200 std::pair<std::unordered_map<crypto::hash, std::unordered_set<ancestor>>::iterator, bool> p = ancestry.insert(std::make_pair(txid, ancestors));
201 if (!p.second)
202 {
203 for (const auto &e: ancestors)
204 p.first->second.insert(e);
205 }
206}
207
208static void add_ancestry(std::unordered_map<crypto::hash, std::unordered_set<ancestor>> &ancestry, const crypto::hash &txid, const ancestor &new_ancestor)
209{
210 std::pair<std::unordered_map<crypto::hash, std::unordered_set<ancestor>>::iterator, bool> p = ancestry.insert(std::make_pair(txid, std::unordered_set<ancestor>()));
211 p.first->second.insert(new_ancestor);
212}
213
214static std::unordered_set<ancestor> get_ancestry(const std::unordered_map<crypto::hash, std::unordered_set<ancestor>> &ancestry, const crypto::hash &txid)
215{
216 std::unordered_map<crypto::hash, std::unordered_set<ancestor>>::const_iterator i = ancestry.find(txid);
217 if (i == ancestry.end())
218 {
219 //MERROR("txid ancestry not found: " << txid);
220 //throw std::runtime_error("txid ancestry not found");
221 return std::unordered_set<ancestor>();
222 }
223 return i->second;
224}
225
226static bool get_block_from_height(ancestry_state_t &state, BlockchainDB *db, uint64_t height, cryptonote::block &b)
227{
228 ++total_blocks;
229 if (state.block_cache.size() > height && !state.block_cache[height].miner_tx.vin.empty())
230 {
231 ++cached_blocks;
232 b = state.block_cache[height];
233 return true;
234 }
237 {
238 LOG_PRINT_L0("Bad block from db");
239 return false;
240 }
241 if (opt_cache_blocks)
242 {
243 state.block_cache.resize(height + 1);
244 state.block_cache[height] = b;
245 }
246 return true;
247}
248
249static bool get_transaction(ancestry_state_t &state, BlockchainDB *db, const crypto::hash &txid, ::tx_data_t &tx_data)
250{
251 std::unordered_map<crypto::hash, ::tx_data_t>::const_iterator i = state.tx_cache.find(txid);
252 ++total_txes;
253 if (i != state.tx_cache.end())
254 {
255 ++cached_txes;
256 tx_data = i->second;
257 return true;
258 }
259
261 if (!db->get_pruned_tx_blob(txid, bd))
262 {
263 LOG_PRINT_L0("Failed to get txid " << txid << " from db");
264 return false;
265 }
268 {
269 LOG_PRINT_L0("Bad tx: " << txid);
270 return false;
271 }
272 tx_data = ::tx_data_t(tx);
273 if (opt_cache_txes)
274 state.tx_cache.insert(std::make_pair(txid, tx_data));
275 return true;
276}
277
278static bool get_output_txid(ancestry_state_t &state, BlockchainDB *db, uint64_t amount, uint64_t offset, crypto::hash &txid)
279{
280 ++total_outputs;
281 std::unordered_map<ancestor, crypto::hash>::const_iterator i = state.output_cache.find({amount, offset});
282 if (i != state.output_cache.end())
283 {
284 ++cached_outputs;
285 txid = i->second;
286 return true;
287 }
288
289 const output_data_t od = db->get_output_key(amount, offset, false);
291 if (!get_block_from_height(state, db, od.height, b))
292 return false;
293
294 for (size_t out = 0; out < b.miner_tx.vout.size(); ++out)
295 {
296 if (b.miner_tx.vout[out].target.type() == typeid(cryptonote::txout_to_key))
297 {
298 const auto &txout = boost::get<cryptonote::txout_to_key>(b.miner_tx.vout[out].target);
299 if (txout.key == od.pubkey)
300 {
302 if (opt_cache_outputs)
303 state.output_cache.insert(std::make_pair(ancestor{amount, offset}, txid));
304 return true;
305 }
306 }
307 else
308 {
309 LOG_PRINT_L0("Bad vout type in txid " << cryptonote::get_transaction_hash(b.miner_tx));
310 return false;
311 }
312 }
313 for (const crypto::hash &block_txid: b.tx_hashes)
314 {
315 ::tx_data_t tx_data3;
316 if (!get_transaction(state, db, block_txid, tx_data3))
317 return false;
318
319 for (size_t out = 0; out < tx_data3.vout.size(); ++out)
320 {
321 if (tx_data3.vout[out] == od.pubkey)
322 {
323 txid = block_txid;
324 if (opt_cache_outputs)
325 state.output_cache.insert(std::make_pair(ancestor{amount, offset}, txid));
326 return true;
327 }
328 }
329 }
330 return false;
331}
332
333int main(int argc, char* argv[])
334{
335 TRY_ENTRY();
336
338
339 std::string default_db_type = "lmdb";
340
341 std::string available_dbs = cryptonote::blockchain_db_types(", ");
342 available_dbs = "available: " + available_dbs;
343
344 uint32_t log_level = 0;
345
347
348 boost::filesystem::path output_file_path;
349
350 po::options_description desc_cmd_only("Command line options");
351 po::options_description desc_cmd_sett("Command line options and settings options");
352 const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
353 const command_line::arg_descriptor<std::string> arg_database = {
354 "database", available_dbs.c_str(), default_db_type
355 };
356 const command_line::arg_descriptor<std::string> arg_txid = {"txid", "Get ancestry for this txid", ""};
357 const command_line::arg_descriptor<std::string> arg_output = {"output", "Get ancestry for this output (amount/offset format)", ""};
358 const command_line::arg_descriptor<uint64_t> arg_height = {"height", "Get ancestry for all txes at this height", 0};
359 const command_line::arg_descriptor<bool> arg_refresh = {"refresh", "Refresh the whole chain first", false};
360 const command_line::arg_descriptor<bool> arg_cache_outputs = {"cache-outputs", "Cache outputs (memory hungry)", false};
361 const command_line::arg_descriptor<bool> arg_cache_txes = {"cache-txes", "Cache txes (memory hungry)", false};
362 const command_line::arg_descriptor<bool> arg_cache_blocks = {"cache-blocks", "Cache blocks (memory hungry)", false};
363 const command_line::arg_descriptor<bool> arg_include_coinbase = {"include-coinbase", "Including coinbase tx in per height average", false};
364 const command_line::arg_descriptor<bool> arg_show_cache_stats = {"show-cache-stats", "Show cache statistics", false};
365
369 command_line::add_arg(desc_cmd_sett, arg_log_level);
370 command_line::add_arg(desc_cmd_sett, arg_database);
371 command_line::add_arg(desc_cmd_sett, arg_txid);
372 command_line::add_arg(desc_cmd_sett, arg_output);
373 command_line::add_arg(desc_cmd_sett, arg_height);
374 command_line::add_arg(desc_cmd_sett, arg_refresh);
375 command_line::add_arg(desc_cmd_sett, arg_cache_outputs);
376 command_line::add_arg(desc_cmd_sett, arg_cache_txes);
377 command_line::add_arg(desc_cmd_sett, arg_cache_blocks);
378 command_line::add_arg(desc_cmd_sett, arg_include_coinbase);
379 command_line::add_arg(desc_cmd_sett, arg_show_cache_stats);
381
382 po::options_description desc_options("Allowed options");
383 desc_options.add(desc_cmd_only).add(desc_cmd_sett);
384
385 po::variables_map vm;
386 bool r = command_line::handle_error_helper(desc_options, [&]()
387 {
388 auto parser = po::command_line_parser(argc, argv).options(desc_options);
389 po::store(parser.run(), vm);
390 po::notify(vm);
391 return true;
392 });
393 if (! r)
394 return 1;
395
397 {
398 std::cout << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")" << ENDL << ENDL;
399 std::cout << desc_options << std::endl;
400 return 1;
401 }
402
403 mlog_configure(mlog_get_default_log_path("electroneum-blockchain-ancestry.log"), true);
404 if (!command_line::is_arg_defaulted(vm, arg_log_level))
405 mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str());
406 else
407 mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str());
408
409 LOG_PRINT_L0("Starting...");
410
411 std::string opt_data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir);
413 bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on);
414 network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET;
415 std::string opt_txid_string = command_line::get_arg(vm, arg_txid);
416 std::string opt_output_string = command_line::get_arg(vm, arg_output);
417 uint64_t opt_height = command_line::get_arg(vm, arg_height);
418 bool opt_refresh = command_line::get_arg(vm, arg_refresh);
419 opt_cache_outputs = command_line::get_arg(vm, arg_cache_outputs);
420 opt_cache_txes = command_line::get_arg(vm, arg_cache_txes);
421 opt_cache_blocks = command_line::get_arg(vm, arg_cache_blocks);
422 bool opt_include_coinbase = command_line::get_arg(vm, arg_include_coinbase);
423 bool opt_show_cache_stats = command_line::get_arg(vm, arg_show_cache_stats);
424
425 if ((!opt_txid_string.empty()) + !!opt_height + !opt_output_string.empty() > 1)
426 {
427 std::cerr << "Only one of --txid, --height, --output can be given" << std::endl;
428 return 1;
429 }
430 crypto::hash opt_txid = crypto::null_hash;
431 uint64_t output_amount = 0, output_offset = 0;
432 if (!opt_txid_string.empty())
433 {
434 if (!epee::string_tools::hex_to_pod(opt_txid_string, opt_txid))
435 {
436 std::cerr << "Invalid txid" << std::endl;
437 return 1;
438 }
439 }
440 else if (!opt_output_string.empty())
441 {
442 if (sscanf(opt_output_string.c_str(), "%" SCNu64 "/%" SCNu64, &output_amount, &output_offset) != 2)
443 {
444 std::cerr << "Invalid output" << std::endl;
445 return 1;
446 }
447 }
448
449 std::string db_type = command_line::get_arg(vm, arg_database);
451 {
452 std::cerr << "Invalid database type: " << db_type << std::endl;
453 return 1;
454 }
455
456 // If we wanted to use the memory pool, we would set up a fake_core.
457
458 // Use Blockchain instead of lower-level BlockchainDB for two reasons:
459 // 1. Blockchain has the init() method for easy setup
460 // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash()
461 //
462 // cannot match blockchain_storage setup above with just one line,
463 // e.g.
464 // Blockchain* core_storage = new Blockchain(NULL);
465 // because unlike blockchain_storage constructor, which takes a pointer to
466 // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object.
467 LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
468 std::unique_ptr<Blockchain> core_storage;
469 tx_memory_pool m_mempool(*core_storage);
470 core_storage.reset(new Blockchain(m_mempool));
471 BlockchainDB *db = new_db(db_type);
472 if (db == NULL)
473 {
474 LOG_ERROR("Attempted to use non-existent database type: " << db_type);
475 throw std::runtime_error("Attempting to use non-existent database type");
476 }
477 LOG_PRINT_L0("database: " << db_type);
478
479 const std::string filename = (boost::filesystem::path(opt_data_dir) / db->get_db_name()).string();
480 LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
481
482 try
483 {
484 db->open(filename, DBF_RDONLY);
485 }
486 catch (const std::exception& e)
487 {
488 LOG_PRINT_L0("Error opening database: " << e.what());
489 return 1;
490 }
491 r = core_storage->init(db, net_type);
492
493 CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage");
494 LOG_PRINT_L0("Source blockchain storage initialized OK");
495
496 std::vector<crypto::hash> start_txids;
497
499
500 const std::string state_file_path = (boost::filesystem::path(opt_data_dir) / "ancestry-state.bin").string();
501 LOG_PRINT_L0("Loading state data from " << state_file_path);
502 std::ifstream state_data_in;
503 state_data_in.open(state_file_path, std::ios_base::binary | std::ios_base::in);
504 if (!state_data_in.fail())
505 {
506 try
507 {
509 a >> state;
510 }
511 catch (const std::exception &e)
512 {
513 MERROR("Failed to load state data from " << state_file_path << ", restarting from scratch");
515 }
516 state_data_in.close();
517 }
518
519 tools::signal_handler::install([](int type) {
520 stop_requested = true;
521 });
522
523 // forward method
524 const uint64_t db_height = db->height();
525 if (opt_refresh)
526 {
527 MINFO("Starting from height " << state.height);
528 state.block_cache.reserve(db_height);
529 for (uint64_t h = state.height; h < db_height; ++h)
530 {
531 size_t block_ancestry_size = 0;
533 ++total_blocks;
536 {
537 LOG_PRINT_L0("Bad block from db");
538 return 1;
539 }
540 if (opt_cache_blocks)
541 {
542 state.block_cache.resize(h + 1);
543 state.block_cache[h] = b;
544 }
545 std::vector<crypto::hash> txids;
546 txids.reserve(1 + b.tx_hashes.size());
547 if (opt_include_coinbase)
548 txids.push_back(cryptonote::get_transaction_hash(b.miner_tx));
549 for (const auto &h: b.tx_hashes)
550 txids.push_back(h);
551 for (const crypto::hash &txid: txids)
552 {
553 printf("%lu/%lu \r", (unsigned long)h, (unsigned long)db_height);
554 fflush(stdout);
555 ::tx_data_t tx_data;
556 std::unordered_map<crypto::hash, ::tx_data_t>::const_iterator i = state.tx_cache.find(txid);
557 ++total_txes;
558 if (i != state.tx_cache.end())
559 {
560 ++cached_txes;
561 tx_data = i->second;
562 }
563 else
564 {
566 if (!db->get_pruned_tx_blob(txid, bd))
567 {
568 LOG_PRINT_L0("Failed to get txid " << txid << " from db");
569 return 1;
570 }
573 {
574 LOG_PRINT_L0("Bad tx: " << txid);
575 return 1;
576 }
577 tx_data = ::tx_data_t(tx);
578 if (opt_cache_txes)
579 state.tx_cache.insert(std::make_pair(txid, tx_data));
580 }
581 if (tx_data.coinbase)
582 {
583 add_ancestry(state.ancestry, txid, std::unordered_set<ancestor>());
584 }
585 else
586 {
587 for (size_t ring = 0; ring < tx_data.vin.size(); ++ring)
588 {
589 const uint64_t amount = tx_data.vin[ring].first;
590 const std::vector<uint64_t> &absolute_offsets = tx_data.vin[ring].second;
591 for (uint64_t offset: absolute_offsets)
592 {
593 add_ancestry(state.ancestry, txid, ancestor{amount, offset});
594 // find the tx which created this output
595 bool found = false;
596 crypto::hash output_txid;
597 if (!get_output_txid(state, db, amount, offset, output_txid))
598 {
599 LOG_PRINT_L0("Output originating transaction not found");
600 return 1;
601 }
602 add_ancestry(state.ancestry, txid, get_ancestry(state.ancestry, output_txid));
603 }
604 }
605 }
606 const size_t ancestry_size = get_ancestry(state.ancestry, txid).size();
607 block_ancestry_size += ancestry_size;
608 MINFO(txid << ": " << ancestry_size);
609 }
610 if (!txids.empty())
611 {
612 std::string stats_msg;
613 MINFO("Height " << h << ": " << (block_ancestry_size / txids.size()) << " average over " << txids.size() << stats_msg);
614 }
615 state.height = h;
616 if (stop_requested)
617 break;
618 }
619
620 LOG_PRINT_L0("Saving state data to " << state_file_path);
621 std::ofstream state_data_out;
622 state_data_out.open(state_file_path, std::ios_base::binary | std::ios_base::out | std::ios::trunc);
623 if (!state_data_out.fail())
624 {
625 try
626 {
628 a << state;
629 }
630 catch (const std::exception &e)
631 {
632 MERROR("Failed to save state data to " << state_file_path);
633 }
634 state_data_out.close();
635 }
636 }
637 else
638 {
639 if (state.height < db_height)
640 {
641 MWARNING("The state file is only built up to height " << state.height << ", but the blockchain reached height " << db_height);
642 MWARNING("You may want to run with --refresh if you want to get ancestry for newer data");
643 }
644 }
645
646 if (!opt_txid_string.empty())
647 {
648 start_txids.push_back(opt_txid);
649 }
650 else if (!opt_output_string.empty())
651 {
652 crypto::hash txid;
653 if (!get_output_txid(state, db, output_amount, output_offset, txid))
654 {
655 LOG_PRINT_L0("Output not found in db");
656 return 1;
657 }
658 start_txids.push_back(txid);
659 }
660 else
661 {
662 const cryptonote::blobdata bd = db->get_block_blob_from_height(opt_height);
665 {
666 LOG_PRINT_L0("Bad block from db");
667 return 1;
668 }
669 for (const crypto::hash &txid: b.tx_hashes)
670 start_txids.push_back(txid);
671 }
672
673 if (start_txids.empty())
674 {
675 LOG_PRINT_L0("No transaction(s) to check");
676 return 1;
677 }
678
679 for (const crypto::hash &start_txid: start_txids)
680 {
681 LOG_PRINT_L0("Checking ancestry for txid " << start_txid);
682
683 std::unordered_map<ancestor, unsigned int> ancestry;
684
685 std::list<crypto::hash> txids;
686 txids.push_back(start_txid);
687 while (!txids.empty())
688 {
689 const crypto::hash txid = txids.front();
690 txids.pop_front();
691
692 if (stop_requested)
693 goto done;
694
695 ::tx_data_t tx_data2;
696 if (!get_transaction(state, db, txid, tx_data2))
697 return 1;
698
699 const bool coinbase = tx_data2.coinbase;
700 if (coinbase)
701 continue;
702
703 for (size_t ring = 0; ring < tx_data2.vin.size(); ++ring)
704 {
705 {
706 const uint64_t amount = tx_data2.vin[ring].first;
707 auto absolute_offsets = tx_data2.vin[ring].second;
708 for (uint64_t offset: absolute_offsets)
709 {
710 add_ancestor(ancestry, amount, offset);
711
712 // find the tx which created this output
713 bool found = false;
714 crypto::hash output_txid;
715 if (!get_output_txid(state, db, amount, offset, output_txid))
716 {
717 LOG_PRINT_L0("Output originating transaction not found");
718 return 1;
719 }
720
721 add_ancestry(state.ancestry, txid, get_ancestry(state.ancestry, output_txid));
722 txids.push_back(output_txid);
723 MDEBUG("adding txid: " << output_txid);
724 }
725 }
726 }
727 }
728
729 MINFO("Ancestry for " << start_txid << ": " << get_deduplicated_ancestry(ancestry) << " / " << get_full_ancestry(ancestry));
730 for (const auto &i: ancestry)
731 {
732 MINFO(cryptonote::print_etn(i.first.amount) << "/" << i.first.offset << ": " << i.second);
733 }
734 }
735
736done:
737 core_storage->deinit();
738
739 if (opt_show_cache_stats)
740 MINFO("cache: txes " << std::to_string(cached_txes*100./total_txes)
741 << "%, blocks " << std::to_string(cached_blocks*100./total_blocks)
742 << "%, outputs " << std::to_string(cached_outputs*100./total_outputs)
743 << "%");
744
745 return 0;
746
747 CATCH_ENTRY("Depth query error", 1);
748}
int main()
uint64_t height
#define DBF_RDONLY
The BlockchainDB backing store interface declaration/contract.
virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t &height) const =0
fetch a block blob by height
virtual uint64_t height() const =0
fetch the current blockchain height
virtual std::string get_db_name() const =0
gets the name of the folder the BlockchainDB's file(s) should be in
virtual bool get_pruned_tx_blob(const crypto::hash &h, cryptonote::blobdata &tx) const =0
fetches the pruned transaction blob with the given hash
virtual output_data_t get_output_key(const uint64_t &amount, const uint64_t &index, bool include_commitmemt=true) const =0
get some of an output's data
virtual void open(const std::string &filename, const int db_flags=0)=0
open a db, or create it if necessary.
Transaction pool, handles transactions which are not part of a block.
Definition tx_pool.h:95
static bool install(T t)
installs a signal handler
Definition util.h:164
BOOST_CLASS_VERSION(nodetool::node_server< cryptonote::t_cryptonote_protocol_handler< tests::proxy_core > >, 1)
#define SCNu64
Definition inttypes.h:245
#define MERROR(x)
Definition misc_log_ex.h:73
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 CATCH_ENTRY(location, return_val)
std::string mlog_get_default_log_path(const char *default_filename)
Definition mlog.cpp:72
#define MWARNING(x)
Definition misc_log_ex.h:74
#define MDEBUG(x)
Definition misc_log_ex.h:76
void mlog_set_log(const char *log)
Definition mlog.cpp:288
#define ENDL
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
#define LOG_ERROR(x)
Definition misc_log_ex.h:98
#define MINFO(x)
Definition misc_log_ex.h:75
#define TRY_ENTRY()
#define LOG_PRINT_L0(x)
Definition misc_log_ex.h:99
void add_arg(boost::program_options::options_description &description, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg, bool unique=true)
const arg_descriptor< bool > arg_help
bool is_arg_defaulted(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
bool handle_error_helper(const boost::program_options::options_description &desc, F parser)
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
POD_CLASS hash
Definition hash.h:50
Holds cryptonote related classes and helpers.
Definition ban.cpp:40
const command_line::arg_descriptor< std::string, false, true, 2 > arg_data_dir
std::vector< uint64_t > relative_output_offsets_to_absolute(const std::vector< uint64_t > &off)
bool parse_and_validate_block_from_blob(const blobdata &b_blob, block &b, crypto::hash *block_hash)
const command_line::arg_descriptor< bool, false > arg_testnet_on
BlockchainDB * new_db(const std::string &db_type)
crypto::hash get_transaction_hash(const transaction &t)
bool blockchain_valid_db_type(const std::string &db_type)
const command_line::arg_descriptor< bool, false > arg_stagenet_on
std::string blobdata
std::string blockchain_db_types(const std::string &sep)
std::string print_etn(uint64_t amount, unsigned int decimal_point)
bool parse_and_validate_tx_base_from_blob(const blobdata &tx_blob, transaction &tx)
bool set_module_name_and_folder(const std::string &path_to_process_)
bool hex_to_pod(const std::string &hex_str, t_pod_type &s)
mdb_size_t count(MDB_cursor *cur)
STL namespace.
bool on_startup()
Definition util.cpp:778
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
#define false
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
bool operator==(const ancestor &other) const
void serialize(t_archive &a, const unsigned int ver)
std::unordered_map< crypto::hash, ::tx_data_t > tx_cache
std::unordered_map< ancestor, crypto::hash > output_cache
std::vector< cryptonote::block > block_cache
std::unordered_map< crypto::hash, std::unordered_set< ancestor > > ancestry
void serialize(t_archive &a, const unsigned int ver)
std::vector< crypto::hash > tx_hashes
a struct containing output metadata
uint64_t height
the height of the block which created the output
crypto::public_key pubkey
the output's public key (for spend verification)
std::vector< uint64_t > key_offsets
size_t operator()(const ancestor &a) const
std::vector< std::pair< uint64_t, std::vector< uint64_t > > > vin
tx_data_t(const cryptonote::transaction &tx)
std::vector< crypto::public_key > vout
void serialize(t_archive &a, const unsigned int ver)
provides the implementation of varint's
const char *const ELECTRONEUM_RELEASE_NAME
const char *const ELECTRONEUM_VERSION_FULL