Debian Codegraph @

monero/src/blockchain_utilities/blockchain_depth.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/range/adaptor/transformed.hpp>
30 #include <boost/filesystem/path.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include "common/command_line.h"
33 #include "common/varint.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 int main(int argc, char* argv[])
48 {
49 TRY_ENTRY();
50
51 epee::string_tools::set_module_name_and_folder(argv[0]);
52
53 uint32_t log_level = 0;
54
55 tools::on_startup();
56
57 boost::filesystem::path output_file_path;
58
59 po::options_description desc_cmd_only("Command line options");
60 po::options_description desc_cmd_sett("Command line options and settings options");
61 const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
62 const command_line::arg_descriptor<std::string> arg_txid = {"txid", "Get min depth for this txid", ""};
63 const command_line::arg_descriptor<uint64_t> arg_height = {"height", "Get min depth for all txes at this height", 0};
64 const command_line::arg_descriptor<bool> arg_include_coinbase = {"include-coinbase", "Include coinbase in the average", false};
65
66 command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir);
67 command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
68 command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
69 command_line::add_arg(desc_cmd_sett, arg_log_level);
70 command_line::add_arg(desc_cmd_sett, arg_txid);
71 command_line::add_arg(desc_cmd_sett, arg_height);
72 command_line::add_arg(desc_cmd_sett, arg_include_coinbase);
73 command_line::add_arg(desc_cmd_only, command_line::arg_help);
74
75 po::options_description desc_options("Allowed options");
76 desc_options.add(desc_cmd_only).add(desc_cmd_sett);
77
78 po::variables_map vm;
79 bool r = command_line::handle_error_helper(desc_options, [&]()
80 {
81 auto parser = po::command_line_parser(argc, argv).options(desc_options);
82 po::store(parser.run(), vm);
83 po::notify(vm);
84 return true;
85 });
86 if (! r)
87 return 1;
88
89 if (command_line::get_arg(vm, command_line::arg_help))
90 {
91 std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL;
92 std::cout << desc_options << std::endl;
93 return 1;
94 }
95
96 mlog_configure(mlog_get_default_log_path("monero-blockchain-depth.log"), true);
97 if (!command_line::is_arg_defaulted(vm, arg_log_level))
98 mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str());
99 else
100 mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str());
101
102 LOG_PRINT_L0("Starting...");
103
104 std::string opt_data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir);
105 bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on);
106 bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on);
107 network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET;
108 std::string opt_txid_string = command_line::get_arg(vm, arg_txid);
109 uint64_t opt_height = command_line::get_arg(vm, arg_height);
110 bool opt_include_coinbase = command_line::get_arg(vm, arg_include_coinbase);
111
112 if (!opt_txid_string.empty() && opt_height)
113 {
114 std::cerr << "txid and height cannot be given at the same time" << std::endl;
115 return 1;
116 }
117 crypto::hash opt_txid = crypto::null_hash;
118 if (!opt_txid_string.empty())
119 {
120 if (!epee::string_tools::hex_to_pod(opt_txid_string, opt_txid))
121 {
122 std::cerr << "Invalid txid" << std::endl;
123 return 1;
124 }
125 }
126
127 // If we wanted to use the memory pool, we would set up a fake_core.
128
129 // Use Blockchain instead of lower-level BlockchainDB for two reasons:
130 // 1. Blockchain has the init() method for easy setup
131 // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash()
132 //
133 // cannot match blockchain_storage setup above with just one line,
134 // e.g.
135 // Blockchain* core_storage = new Blockchain(NULL);
136 // because unlike blockchain_storage constructor, which takes a pointer to
137 // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object.
138 LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
139 std::unique_ptr<Blockchain> core_storage;
140 tx_memory_pool m_mempool(*core_storage);
141 core_storage.reset(new Blockchain(m_mempool));
142 BlockchainDB *db = new_db();
143 if (db == NULL)
144 {
145 LOG_ERROR("Failed to initialize a database");
146 throw std::runtime_error("Failed to initialize a database");
147 }
148 LOG_PRINT_L0("database: LMDB");
149
150 const std::string filename = (boost::filesystem::path(opt_data_dir) / db->get_db_name()).string();
151 LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
152
153 try
154 {
155 db->open(filename, DBF_RDONLY);
156 }
157 catch (const std::exception& e)
158 {
159 LOG_PRINT_L0("Error opening database: " << e.what());
160 return 1;
161 }
162 r = core_storage->init(db, net_type);
163
164 CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage");
165 LOG_PRINT_L0("Source blockchain storage initialized OK");
166
167 std::vector<crypto::hash> start_txids;
168 if (!opt_txid_string.empty())
169 {
170 start_txids.push_back(opt_txid);
171 }
172 else
173 {
174 const cryptonote::blobdata bd = db->get_block_blob_from_height(opt_height);
175 cryptonote::block b;
176 if (!cryptonote::parse_and_validate_block_from_blob(bd, b))
177 {
178 LOG_PRINT_L0("Bad block from db");
179 return 1;
180 }
181 for (const crypto::hash &txid: b.tx_hashes)
182 start_txids.push_back(txid);
183 if (opt_include_coinbase)
184 start_txids.push_back(cryptonote::get_transaction_hash(b.miner_tx));
185 }
186
187 if (start_txids.empty())
188 {
189 LOG_PRINT_L0("No transaction(s) to check");
190 return 1;
191 }
192
193 std::vector<uint64_t> depths;
194 for (const crypto::hash &start_txid: start_txids)
195 {
196 uint64_t depth = 0;
197 bool coinbase = false;
198
199 LOG_PRINT_L0("Checking depth for txid " << start_txid);
200 std::vector<crypto::hash> txids(1, start_txid);
201 while (!coinbase)
202 {
203 LOG_PRINT_L0("Considering "<< txids.size() << " transaction(s) at depth " << depth);
204 std::vector<crypto::hash> new_txids;
205 for (const crypto::hash &txid: txids)
206 {
207 cryptonote::blobdata bd;
208 if (!db->get_pruned_tx_blob(txid, bd))
209 {
210 LOG_PRINT_L0("Failed to get txid " << txid << " from db");
211 return 1;
212 }
213 cryptonote::transaction tx;
214 if (!cryptonote::parse_and_validate_tx_base_from_blob(bd, tx))
215 {
216 LOG_PRINT_L0("Bad tx: " << txid);
217 return 1;
218 }
219 for (size_t ring = 0; ring < tx.vin.size(); ++ring)
220 {
221 if (tx.vin[ring].type() == typeid(cryptonote::txin_gen))
222 {
223 MDEBUG(txid << " is a coinbase transaction");
224 coinbase = true;
225 goto done;
226 }
227 if (tx.vin[ring].type() == typeid(cryptonote::txin_to_key))
228 {
229 const cryptonote::txin_to_key &txin = boost::get<cryptonote::txin_to_key>(tx.vin[ring]);
230 const uint64_t amount = txin.amount;
231 auto absolute_offsets = cryptonote::relative_output_offsets_to_absolute(txin.key_offsets);
232 for (uint64_t offset: absolute_offsets)
233 {
234 const output_data_t od = db->get_output_key(amount, offset);
235 const crypto::hash block_hash = db->get_block_hash_from_height(od.height);
236 bd = db->get_block_blob(block_hash);
237 cryptonote::block b;
238 if (!cryptonote::parse_and_validate_block_from_blob(bd, b))
239 {
240 LOG_PRINT_L0("Bad block from db");
241 return 1;
242 }
243 // find the tx which created this output
244 bool found = false;
245 for (size_t out = 0; out < b.miner_tx.vout.size(); ++out)
246 {
247 if (b.miner_tx.vout[out].target.type() == typeid(cryptonote::txout_to_key))
248 {
249 const auto &txout = boost::get<cryptonote::txout_to_key>(b.miner_tx.vout[out].target);
250 if (txout.key == od.pubkey)
251 {
252 found = true;
253 new_txids.push_back(cryptonote::get_transaction_hash(b.miner_tx));
254 MDEBUG("adding txid: " << cryptonote::get_transaction_hash(b.miner_tx));
255 break;
256 }
257 }
258 else
259 {
260 LOG_PRINT_L0("Bad vout type in txid " << cryptonote::get_transaction_hash(b.miner_tx));
261 return 1;
262 }
263 }
264 for (const crypto::hash &block_txid: b.tx_hashes)
265 {
266 if (found)
267 break;
268 if (!db->get_pruned_tx_blob(block_txid, bd))
269 {
270 LOG_PRINT_L0("Failed to get txid " << block_txid << " from db");
271 return 1;
272 }
273 cryptonote::transaction tx2;
274 if (!cryptonote::parse_and_validate_tx_base_from_blob(bd, tx2))
275 {
276 LOG_PRINT_L0("Bad tx: " << block_txid);
277 return 1;
278 }
279 for (size_t out = 0; out < tx2.vout.size(); ++out)
280 {
281 if (tx2.vout[out].target.type() == typeid(cryptonote::txout_to_key))
282 {
283 const auto &txout = boost::get<cryptonote::txout_to_key>(tx2.vout[out].target);
284 if (txout.key == od.pubkey)
285 {
286 found = true;
287 new_txids.push_back(block_txid);
288 MDEBUG("adding txid: " << block_txid);
289 break;
290 }
291 }
292 else
293 {
294 LOG_PRINT_L0("Bad vout type in txid " << block_txid);
295 return 1;
296 }
297 }
298 }
299 if (!found)
300 {
301 LOG_PRINT_L0("Output originating transaction not found");
302 return 1;
303 }
304 }
305 }
306 else
307 {
308 LOG_PRINT_L0("Bad vin type in txid " << txid);
309 return 1;
310 }
311 }
312 }
313 if (!coinbase)
314 {
315 std::swap(txids, new_txids);
316 ++depth;
317 }
318 }
319 done:
320 LOG_PRINT_L0("Min depth for txid " << start_txid << ": " << depth);
321 depths.push_back(depth);
322 }
323
324 uint64_t cumulative_depth = 0;
325 for (uint64_t depth: depths)
326 cumulative_depth += depth;
327 LOG_PRINT_L0("Average min depth for " << start_txids.size() << " transaction(s): " << cumulative_depth/(float)depths.size());
328 LOG_PRINT_L0("Median min depth for " << start_txids.size() << " transaction(s): " << epee::misc_utils::median(depths));
329
330 core_storage->deinit();
331 return 0;
332
333 CATCH_ENTRY("Depth query error", 1);
334 }