| 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/algorithm/string.hpp> |
| 31 | #include <boost/filesystem.hpp> |
| 32 | #include "common/unordered_containers_boost_serialization.h" |
| 33 | #include "common/command_line.h" |
| 34 | #include "common/varint.h" |
| 35 | #include "serialization/crypto.h" |
| 36 | #include "cryptonote_basic/cryptonote_boost_serialization.h" |
| 37 | #include "cryptonote_core/tx_pool.h" |
| 38 | #include "cryptonote_core/cryptonote_core.h" |
| 39 | #include "cryptonote_core/blockchain.h" |
| 40 | #include "blockchain_db/blockchain_db.h" |
| 41 | #include "wallet/ringdb.h" |
| 42 | #include "version.h" |
| 43 | |
| 44 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 45 | #define MONERO_DEFAULT_LOG_CATEGORY "bcutil" |
| 46 | |
| 47 | namespace po = boost::program_options; |
| 48 | using namespace epee; |
| 49 | using namespace cryptonote; |
| 50 | |
| 51 | static uint64_t records_per_sync = 200; |
| 52 | static uint64_t db_flags = 0; |
| 53 | static MDB_dbi dbi_relative_rings; |
| 54 | static MDB_dbi dbi_outputs; |
| 55 | static MDB_dbi dbi_processed_txidx; |
| 56 | static MDB_dbi dbi_spent; |
| 57 | static MDB_dbi dbi_per_amount; |
| 58 | static MDB_dbi dbi_ring_instances; |
| 59 | static MDB_dbi dbi_stats; |
| 60 | static MDB_env *env = NULL; |
| 61 | |
| 62 | struct output_data |
| 63 | { |
| 64 | uint64_t amount; |
| 65 | uint64_t offset; |
| 66 | output_data(): amount(0), offset(0) {} |
| 67 | output_data(uint64_t a, uint64_t i): amount(a), offset(i) {} |
| 68 | bool operator==(const output_data &other) const { return other.amount == amount && other.offset == offset; } |
| 69 | }; |
| 70 | |
| 71 | // |
| 72 | // relative_rings: key_image -> vector<uint64_t> |
| 73 | // outputs: 128 bits -> set of key images |
| 74 | // processed_txidx: string -> uint64_t |
| 75 | // spent: amount -> offset |
| 76 | // ring_instances: vector<uint64_t> -> uint64_t |
| 77 | // stats: string -> arbitrary |
| 78 | // |
| 79 | |
| 80 | static bool parse_db_sync_mode(std::string db_sync_mode) |
| 81 | { |
| 82 | std::vector<std::string> options; |
| 83 | boost::trim(db_sync_mode); |
| 84 | boost::split(options, db_sync_mode, boost::is_any_of(" :")); |
| 85 | |
| 86 | for(const auto &option : options) |
| 87 | MDEBUG("option: " << option); |
| 88 | |
| 89 | // default to fast:async:1 |
| 90 | uint64_t DEFAULT_FLAGS = DBF_FAST; |
| 91 | |
| 92 | if(options.size() == 0) |
| 93 | { |
| 94 | // default to fast:async:1 |
| 95 | db_flags = DEFAULT_FLAGS; |
| 96 | } |
| 97 | |
| 98 | bool safemode = false; |
| 99 | if(options.size() >= 1) |
| 100 | { |
| 101 | if(options[0] == "safe") |
| 102 | { |
| 103 | safemode = true; |
| 104 | db_flags = DBF_SAFE; |
| 105 | } |
| 106 | else if(options[0] == "fast") |
| 107 | { |
| 108 | db_flags = DBF_FAST; |
| 109 | } |
| 110 | else if(options[0] == "fastest") |
| 111 | { |
| 112 | db_flags = DBF_FASTEST; |
| 113 | records_per_sync = 1000; // default to fastest:async:1000 |
| 114 | } |
| 115 | else |
| 116 | db_flags = DEFAULT_FLAGS; |
| 117 | } |
| 118 | |
| 119 | if(options.size() >= 2 && !safemode) |
| 120 | { |
| 121 | char *endptr; |
| 122 | uint64_t bps = strtoull(options[1].c_str(), &endptr, 0); |
| 123 | if (*endptr == '\0') |
| 124 | records_per_sync = bps; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | static std::string get_default_db_path() |
| 131 | { |
| 132 | boost::filesystem::path dir = tools::get_default_data_dir(); |
| 133 | // remove .bitmonero, replace with .shared-ringdb |
| 134 | dir = dir.remove_filename(); |
| 135 | dir /= ".shared-ringdb"; |
| 136 | return dir.string(); |
| 137 | } |
| 138 | |
| 139 | static std::string get_cache_filename(boost::filesystem::path filename) |
| 140 | { |
| 141 | if (!boost::filesystem::is_directory(filename)) |
| 142 | filename.remove_filename(); |
| 143 | return filename.string(); |
| 144 | } |
| 145 | |
| 146 | static int compare_hash32(const MDB_val *a, const MDB_val *b) |
| 147 | { |
| 148 | const uint32_t *va = (const uint32_t*) a->mv_data; |
| 149 | const uint32_t *vb = (const uint32_t*) b->mv_data; |
| 150 | for (int n = 7; n >= 0; n--) |
| 151 | { |
| 152 | if (va[n] == vb[n]) |
| 153 | continue; |
| 154 | return va[n] < vb[n] ? -1 : 1; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | int compare_uint64(const MDB_val *a, const MDB_val *b) |
| 161 | { |
| 162 | const uint64_t va = *(const uint64_t *)a->mv_data; |
| 163 | const uint64_t vb = *(const uint64_t *)b->mv_data; |
| 164 | return (va < vb) ? -1 : va > vb; |
| 165 | } |
| 166 | |
| 167 | static int compare_double64(const MDB_val *a, const MDB_val *b) |
| 168 | { |
| 169 | const uint64_t va = *(const uint64_t*) a->mv_data; |
| 170 | const uint64_t vb = *(const uint64_t*) b->mv_data; |
| 171 | if (va == vb) |
| 172 | { |
| 173 | const uint64_t va = ((const uint64_t*) a->mv_data)[1]; |
| 174 | const uint64_t vb = ((const uint64_t*) b->mv_data)[1]; |
| 175 | return va < vb ? -1 : va > vb; |
| 176 | } |
| 177 | return va < vb ? -1 : va > vb; |
| 178 | } |
| 179 | |
| 180 | static int resize_env(const char *db_path) |
| 181 | { |
| 182 | MDB_envinfo mei; |
| 183 | MDB_stat mst; |
| 184 | int ret; |
| 185 | |
| 186 | size_t needed = 1000ul * 1024 * 1024; // at least 1000 MB |
| 187 | |
| 188 | ret = mdb_env_info(env, &mei); |
| 189 | if (ret) |
| 190 | return ret; |
| 191 | ret = mdb_env_stat(env, &mst); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | uint64_t size_used = mst.ms_psize * mei.me_last_pgno; |
| 195 | uint64_t mapsize = mei.me_mapsize; |
| 196 | if (size_used + needed > mei.me_mapsize) |
| 197 | { |
| 198 | try |
| 199 | { |
| 200 | boost::filesystem::path path(db_path); |
| 201 | boost::filesystem::space_info si = boost::filesystem::space(path); |
| 202 | if(si.available < needed) |
| 203 | { |
| 204 | MERROR("!! WARNING: Insufficient free space to extend database !!: " << (si.available >> 20L) << " MB available"); |
| 205 | return ENOSPC; |
| 206 | } |
| 207 | } |
| 208 | catch(...) |
| 209 | { |
| 210 | // print something but proceed. |
| 211 | MWARNING("Unable to query free disk space."); |
| 212 | } |
| 213 | |
| 214 | mapsize += needed; |
| 215 | } |
| 216 | return mdb_env_set_mapsize(env, mapsize); |
| 217 | } |
| 218 | |
| 219 | static void init(std::string cache_filename) |
| 220 | { |
| 221 | MDB_txn *txn; |
| 222 | bool tx_active = false; |
| 223 | int dbr; |
| 224 | |
| 225 | MINFO("Creating spent output cache in " << cache_filename); |
| 226 | |
| 227 | tools::create_directories_if_necessary(cache_filename); |
| 228 | |
| 229 | int flags = 0; |
| 230 | if (db_flags & DBF_FAST) |
| 231 | flags |= MDB_NOSYNC; |
| 232 | if (db_flags & DBF_FASTEST) |
| 233 | flags |= MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC; |
| 234 | |
| 235 | dbr = mdb_env_create(&env); |
| 236 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LDMB environment: " + std::string(mdb_strerror(dbr))); |
| 237 | dbr = mdb_env_set_maxdbs(env, 7); |
| 238 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to set max env dbs: " + std::string(mdb_strerror(dbr))); |
| 239 | const std::string actual_filename = get_cache_filename(cache_filename); |
| 240 | dbr = mdb_env_open(env, actual_filename.c_str(), flags, 0664); |
| 241 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open rings database file '" |
| 242 | + actual_filename + "': " + std::string(mdb_strerror(dbr))); |
| 243 | |
| 244 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 245 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 246 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 247 | tx_active = true; |
| 248 | |
| 249 | dbr = mdb_dbi_open(txn, "relative_rings", MDB_CREATE | MDB_INTEGERKEY, &dbi_relative_rings); |
| 250 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 251 | mdb_set_compare(txn, dbi_relative_rings, compare_hash32); |
| 252 | |
| 253 | dbr = mdb_dbi_open(txn, "outputs", MDB_CREATE | MDB_INTEGERKEY, &dbi_outputs); |
| 254 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 255 | mdb_set_compare(txn, dbi_outputs, compare_double64); |
| 256 | |
| 257 | dbr = mdb_dbi_open(txn, "processed_txidx", MDB_CREATE, &dbi_processed_txidx); |
| 258 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 259 | |
| 260 | dbr = mdb_dbi_open(txn, "spent", MDB_CREATE | MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, &dbi_spent); |
| 261 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 262 | mdb_set_dupsort(txn, dbi_spent, compare_uint64); |
| 263 | |
| 264 | dbr = mdb_dbi_open(txn, "per_amount", MDB_CREATE | MDB_INTEGERKEY, &dbi_per_amount); |
| 265 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 266 | mdb_set_compare(txn, dbi_per_amount, compare_uint64); |
| 267 | |
| 268 | dbr = mdb_dbi_open(txn, "ring_instances", MDB_CREATE, &dbi_ring_instances); |
| 269 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 270 | |
| 271 | dbr = mdb_dbi_open(txn, "stats", MDB_CREATE, &dbi_stats); |
| 272 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 273 | |
| 274 | dbr = mdb_txn_commit(txn); |
| 275 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr))); |
| 276 | tx_active = false; |
| 277 | } |
| 278 | |
| 279 | static void close() |
| 280 | { |
| 281 | if (env) |
| 282 | { |
| 283 | mdb_dbi_close(env, dbi_relative_rings); |
| 284 | mdb_dbi_close(env, dbi_outputs); |
| 285 | mdb_dbi_close(env, dbi_processed_txidx); |
| 286 | mdb_dbi_close(env, dbi_per_amount); |
| 287 | mdb_dbi_close(env, dbi_spent); |
| 288 | mdb_dbi_close(env, dbi_ring_instances); |
| 289 | mdb_dbi_close(env, dbi_stats); |
| 290 | mdb_env_close(env); |
| 291 | env = NULL; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | static std::string compress_ring(const std::vector<uint64_t> &ring, std::string s = "") |
| 296 | { |
| 297 | const size_t sz = s.size(); |
| 298 | s.resize(s.size() + 12 * ring.size()); |
| 299 | char *ptr = (char*)s.data() + sz; |
| 300 | for (uint64_t out: ring) |
| 301 | tools::write_varint(ptr, out); |
| 302 | if (ptr > s.data() + sz + 12 * ring.size()) |
| 303 | throw std::runtime_error("varint output overflow"); |
| 304 | s.resize(ptr - s.data()); |
| 305 | return s; |
| 306 | } |
| 307 | |
| 308 | static std::string compress_ring(uint64_t amount, const std::vector<uint64_t> &ring) |
| 309 | { |
| 310 | char s[12], *ptr = s; |
| 311 | tools::write_varint(ptr, amount); |
| 312 | if (ptr > s + sizeof(s)) |
| 313 | throw std::runtime_error("varint output overflow"); |
| 314 | return compress_ring(ring, std::string(s, ptr-s)); |
| 315 | } |
| 316 | |
| 317 | static std::vector<uint64_t> decompress_ring(const std::string &s) |
| 318 | { |
| 319 | std::vector<uint64_t> ring; |
| 320 | int read = 0; |
| 321 | for (std::string::const_iterator i = s.begin(); i != s.cend(); std::advance(i, read)) |
| 322 | { |
| 323 | uint64_t out; |
| 324 | std::string tmp(i, s.cend()); |
| 325 | read = tools::read_varint(tmp.begin(), tmp.end(), out); |
| 326 | CHECK_AND_ASSERT_THROW_MES(read > 0 && read <= 256, "Internal error decompressing ring"); |
| 327 | ring.push_back(out); |
| 328 | } |
| 329 | return ring; |
| 330 | } |
| 331 | |
| 332 | static bool for_all_transactions(const std::string &filename, uint64_t &start_idx, uint64_t &n_txes, const std::function<bool(const cryptonote::transaction_prefix&)> &f) |
| 333 | { |
| 334 | MDB_env *env; |
| 335 | MDB_dbi dbi; |
| 336 | MDB_txn *txn; |
| 337 | MDB_cursor *cur; |
| 338 | int dbr; |
| 339 | bool tx_active = false; |
| 340 | MDB_val k; |
| 341 | MDB_val v; |
| 342 | |
| 343 | dbr = mdb_env_create(&env); |
| 344 | if (dbr) throw std::runtime_error("Failed to create LDMB environment: " + std::string(mdb_strerror(dbr))); |
| 345 | dbr = mdb_env_set_maxdbs(env, 2); |
| 346 | if (dbr) throw std::runtime_error("Failed to set max env dbs: " + std::string(mdb_strerror(dbr))); |
| 347 | const std::string actual_filename = filename; |
| 348 | dbr = mdb_env_open(env, actual_filename.c_str(), 0, 0664); |
| 349 | if (dbr) throw std::runtime_error("Failed to open rings database file '" |
| 350 | + actual_filename + "': " + std::string(mdb_strerror(dbr))); |
| 351 | |
| 352 | dbr = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
| 353 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 354 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 355 | tx_active = true; |
| 356 | |
| 357 | dbr = mdb_dbi_open(txn, "txs_pruned", MDB_INTEGERKEY, &dbi); |
| 358 | if (dbr) |
| 359 | dbr = mdb_dbi_open(txn, "txs", MDB_INTEGERKEY, &dbi); |
| 360 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 361 | dbr = mdb_cursor_open(txn, dbi, &cur); |
| 362 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 363 | MDB_stat stat; |
| 364 | dbr = mdb_stat(txn, dbi, &stat); |
| 365 | if (dbr) throw std::runtime_error("Failed to query m_block_info: " + std::string(mdb_strerror(dbr))); |
| 366 | n_txes = stat.ms_entries; |
| 367 | |
| 368 | bool fret = true; |
| 369 | |
| 370 | k.mv_size = sizeof(uint64_t); |
| 371 | k.mv_data = &start_idx; |
| 372 | MDB_cursor_op op = MDB_SET; |
| 373 | while (1) |
| 374 | { |
| 375 | int ret = mdb_cursor_get(cur, &k, &v, op); |
| 376 | op = MDB_NEXT; |
| 377 | if (ret == MDB_NOTFOUND) |
| 378 | break; |
| 379 | if (ret) |
| 380 | throw std::runtime_error("Failed to enumerate transactions: " + std::string(mdb_strerror(ret))); |
| 381 | |
| 382 | if (k.mv_size != sizeof(uint64_t)) |
| 383 | throw std::runtime_error("Bad key size"); |
| 384 | const uint64_t idx = *(uint64_t*)k.mv_data; |
| 385 | if (idx < start_idx) |
| 386 | continue; |
| 387 | |
| 388 | cryptonote::transaction_prefix tx; |
| 389 | blobdata bd; |
| 390 | bd.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size); |
| 391 | binary_archive<false> ba{epee::strspan<std::uint8_t>(bd)}; |
| 392 | bool r = do_serialize(ba, tx); |
| 393 | CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob"); |
| 394 | |
| 395 | start_idx = *(uint64_t*)k.mv_data; |
| 396 | if (!f(tx)) { |
| 397 | fret = false; |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | mdb_cursor_close(cur); |
| 403 | dbr = mdb_txn_commit(txn); |
| 404 | if (dbr) throw std::runtime_error("Failed to commit db transaction: " + std::string(mdb_strerror(dbr))); |
| 405 | tx_active = false; |
| 406 | mdb_dbi_close(env, dbi); |
| 407 | mdb_env_close(env); |
| 408 | return fret; |
| 409 | } |
| 410 | |
| 411 | static bool for_all_transactions(const std::string &filename, const uint64_t &start_idx, uint64_t &n_txes, const std::function<bool(bool, uint64_t, const cryptonote::transaction_prefix&)> &f) |
| 412 | { |
| 413 | MDB_env *env; |
| 414 | MDB_dbi dbi_blocks, dbi_txs; |
| 415 | MDB_txn *txn; |
| 416 | MDB_cursor *cur_blocks, *cur_txs; |
| 417 | int dbr; |
| 418 | bool tx_active = false; |
| 419 | MDB_val k; |
| 420 | MDB_val v; |
| 421 | |
| 422 | dbr = mdb_env_create(&env); |
| 423 | if (dbr) throw std::runtime_error("Failed to create LDMB environment: " + std::string(mdb_strerror(dbr))); |
| 424 | dbr = mdb_env_set_maxdbs(env, 3); |
| 425 | if (dbr) throw std::runtime_error("Failed to set max env dbs: " + std::string(mdb_strerror(dbr))); |
| 426 | const std::string actual_filename = filename; |
| 427 | dbr = mdb_env_open(env, actual_filename.c_str(), 0, 0664); |
| 428 | if (dbr) throw std::runtime_error("Failed to open rings database file '" |
| 429 | + actual_filename + "': " + std::string(mdb_strerror(dbr))); |
| 430 | |
| 431 | dbr = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
| 432 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 433 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 434 | tx_active = true; |
| 435 | |
| 436 | dbr = mdb_dbi_open(txn, "blocks", MDB_INTEGERKEY, &dbi_blocks); |
| 437 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 438 | dbr = mdb_dbi_open(txn, "txs_pruned", MDB_INTEGERKEY, &dbi_txs); |
| 439 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 440 | |
| 441 | dbr = mdb_cursor_open(txn, dbi_blocks, &cur_blocks); |
| 442 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 443 | dbr = mdb_cursor_open(txn, dbi_txs, &cur_txs); |
| 444 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 445 | |
| 446 | MDB_stat stat; |
| 447 | dbr = mdb_stat(txn, dbi_blocks, &stat); |
| 448 | if (dbr) throw std::runtime_error("Failed to query txs stat: " + std::string(mdb_strerror(dbr))); |
| 449 | uint64_t n_blocks = stat.ms_entries; |
| 450 | dbr = mdb_stat(txn, dbi_txs, &stat); |
| 451 | if (dbr) throw std::runtime_error("Failed to query txs stat: " + std::string(mdb_strerror(dbr))); |
| 452 | n_txes = stat.ms_entries; |
| 453 | |
| 454 | bool fret = true; |
| 455 | |
| 456 | MDB_cursor_op op_blocks = MDB_FIRST; |
| 457 | MDB_cursor_op op_txs = MDB_FIRST; |
| 458 | uint64_t tx_idx = 0; |
| 459 | while (1) |
| 460 | { |
| 461 | int ret = mdb_cursor_get(cur_blocks, &k, &v, op_blocks); |
| 462 | op_blocks = MDB_NEXT; |
| 463 | if (ret == MDB_NOTFOUND) |
| 464 | break; |
| 465 | if (ret) |
| 466 | throw std::runtime_error("Failed to enumerate blocks: " + std::string(mdb_strerror(ret))); |
| 467 | |
| 468 | if (k.mv_size != sizeof(uint64_t)) |
| 469 | throw std::runtime_error("Bad key size"); |
| 470 | uint64_t height = *(const uint64_t*)k.mv_data; |
| 471 | blobdata bd; |
| 472 | bd.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size); |
| 473 | block b; |
| 474 | if (!parse_and_validate_block_from_blob(bd, b)) |
| 475 | throw std::runtime_error("Failed to parse block from blob retrieved from the db"); |
| 476 | |
| 477 | ret = mdb_cursor_get(cur_txs, &k, &v, op_txs); |
| 478 | if (ret) |
| 479 | throw std::runtime_error("Failed to fetch transaction " + string_tools::pod_to_hex(get_transaction_hash(b.miner_tx)) + ": " + std::string(mdb_strerror(ret))); |
| 480 | op_txs = MDB_NEXT; |
| 481 | |
| 482 | bool last_block = height == n_blocks - 1; |
| 483 | if (start_idx <= tx_idx++ && !f(last_block && b.tx_hashes.empty(), height, b.miner_tx)) |
| 484 | { |
| 485 | fret = false; |
| 486 | break; |
| 487 | } |
| 488 | for (size_t i = 0; i < b.tx_hashes.size(); ++i) |
| 489 | { |
| 490 | const crypto::hash& txid = b.tx_hashes[i]; |
| 491 | ret = mdb_cursor_get(cur_txs, &k, &v, op_txs); |
| 492 | if (ret) |
| 493 | throw std::runtime_error("Failed to fetch transaction " + string_tools::pod_to_hex(txid) + ": " + std::string(mdb_strerror(ret))); |
| 494 | if (start_idx <= tx_idx++) |
| 495 | { |
| 496 | cryptonote::transaction_prefix tx; |
| 497 | bd.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size); |
| 498 | CHECK_AND_ASSERT_MES(parse_and_validate_tx_prefix_from_blob(bd, tx), false, "Failed to parse transaction from blob"); |
| 499 | if (!f(last_block && i == b.tx_hashes.size() - 1, height, tx)) |
| 500 | { |
| 501 | fret = false; |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | if (!fret) |
| 507 | break; |
| 508 | } |
| 509 | |
| 510 | mdb_cursor_close(cur_blocks); |
| 511 | mdb_cursor_close(cur_txs); |
| 512 | mdb_txn_commit(txn); |
| 513 | tx_active = false; |
| 514 | mdb_dbi_close(env, dbi_blocks); |
| 515 | mdb_dbi_close(env, dbi_txs); |
| 516 | mdb_env_close(env); |
| 517 | return fret; |
| 518 | } |
| 519 | |
| 520 | static uint64_t find_first_diverging_transaction(const std::string &first_filename, const std::string &second_filename) |
| 521 | { |
| 522 | MDB_env *env[2]; |
| 523 | MDB_dbi dbi[2]; |
| 524 | MDB_txn *txn[2]; |
| 525 | MDB_cursor *cur[2]; |
| 526 | int dbr; |
| 527 | bool tx_active[2] = { false, false }; |
| 528 | uint64_t n_txes[2]; |
| 529 | MDB_val k; |
| 530 | MDB_val v[2]; |
| 531 | |
| 532 | epee::misc_utils::auto_scope_leave_caller txn_dtor[2]; |
| 533 | for (int i = 0; i < 2; ++i) |
| 534 | { |
| 535 | dbr = mdb_env_create(&env[i]); |
| 536 | if (dbr) throw std::runtime_error("Failed to create LDMB environment: " + std::string(mdb_strerror(dbr))); |
| 537 | dbr = mdb_env_set_maxdbs(env[i], 2); |
| 538 | if (dbr) throw std::runtime_error("Failed to set max env dbs: " + std::string(mdb_strerror(dbr))); |
| 539 | const std::string actual_filename = i ? second_filename : first_filename; |
| 540 | dbr = mdb_env_open(env[i], actual_filename.c_str(), 0, 0664); |
| 541 | if (dbr) throw std::runtime_error("Failed to open rings database file '" |
| 542 | + actual_filename + "': " + std::string(mdb_strerror(dbr))); |
| 543 | |
| 544 | dbr = mdb_txn_begin(env[i], NULL, MDB_RDONLY, &txn[i]); |
| 545 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 546 | txn_dtor[i] = epee::misc_utils::create_scope_leave_handler([&, i](){if (tx_active[i]) mdb_txn_abort(txn[i]);}); |
| 547 | tx_active[i] = true; |
| 548 | |
| 549 | dbr = mdb_dbi_open(txn[i], "txs_pruned", MDB_INTEGERKEY, &dbi[i]); |
| 550 | if (dbr) |
| 551 | dbr = mdb_dbi_open(txn[i], "txs", MDB_INTEGERKEY, &dbi[i]); |
| 552 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 553 | dbr = mdb_cursor_open(txn[i], dbi[i], &cur[i]); |
| 554 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 555 | MDB_stat stat; |
| 556 | dbr = mdb_stat(txn[i], dbi[i], &stat); |
| 557 | if (dbr) throw std::runtime_error("Failed to query m_block_info: " + std::string(mdb_strerror(dbr))); |
| 558 | n_txes[i] = stat.ms_entries; |
| 559 | } |
| 560 | |
| 561 | if (n_txes[0] == 0 || n_txes[1] == 0) |
| 562 | throw std::runtime_error("No transaction in the database"); |
| 563 | uint64_t lo = 0, hi = std::min(n_txes[0], n_txes[1]) - 1; |
| 564 | while (lo <= hi) |
| 565 | { |
| 566 | uint64_t mid = (lo + hi) / 2; |
| 567 | |
| 568 | k.mv_size = sizeof(uint64_t); |
| 569 | k.mv_data = (void*)∣ |
| 570 | dbr = mdb_cursor_get(cur[0], &k, &v[0], MDB_SET); |
| 571 | if (dbr) throw std::runtime_error("Failed to query transaction: " + std::string(mdb_strerror(dbr))); |
| 572 | dbr = mdb_cursor_get(cur[1], &k, &v[1], MDB_SET); |
| 573 | if (dbr) throw std::runtime_error("Failed to query transaction: " + std::string(mdb_strerror(dbr))); |
| 574 | if (v[0].mv_size == v[1].mv_size && !memcmp(v[0].mv_data, v[1].mv_data, v[0].mv_size)) |
| 575 | lo = mid + 1; |
| 576 | else |
| 577 | hi = mid - 1; |
| 578 | } |
| 579 | |
| 580 | for (int i = 0; i < 2; ++i) |
| 581 | { |
| 582 | mdb_cursor_close(cur[i]); |
| 583 | dbr = mdb_txn_commit(txn[i]); |
| 584 | if (dbr) throw std::runtime_error("Failed to query transaction: " + std::string(mdb_strerror(dbr))); |
| 585 | tx_active[i] = false; |
| 586 | mdb_dbi_close(env[i], dbi[i]); |
| 587 | mdb_env_close(env[i]); |
| 588 | } |
| 589 | return hi; |
| 590 | } |
| 591 | |
| 592 | static std::vector<uint64_t> canonicalize(const std::vector<uint64_t> &v) |
| 593 | { |
| 594 | std::vector<uint64_t> c; |
| 595 | c.reserve(v.size()); |
| 596 | c.push_back(v[0]); |
| 597 | for (size_t n = 1; n < v.size(); ++n) |
| 598 | { |
| 599 | if (v[n] != 0) |
| 600 | c.push_back(v[n]); |
| 601 | } |
| 602 | if (c.size() < v.size()) |
| 603 | { |
| 604 | MINFO("Ring has duplicate member(s): " << |
| 605 | boost::join(v | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " ")); |
| 606 | } |
| 607 | return c; |
| 608 | } |
| 609 | |
| 610 | static uint64_t get_num_spent_outputs() |
| 611 | { |
| 612 | MDB_txn *txn; |
| 613 | bool tx_active = false; |
| 614 | |
| 615 | int dbr = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
| 616 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 617 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 618 | tx_active = true; |
| 619 | |
| 620 | MDB_cursor *cur; |
| 621 | dbr = mdb_cursor_open(txn, dbi_spent, &cur); |
| 622 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open cursor for spent outputs: " + std::string(mdb_strerror(dbr))); |
| 623 | MDB_val k, v; |
| 624 | mdb_size_t count = 0, tmp; |
| 625 | |
| 626 | MDB_cursor_op op = MDB_FIRST; |
| 627 | while (1) |
| 628 | { |
| 629 | dbr = mdb_cursor_get(cur, &k, &v, op); |
| 630 | op = MDB_NEXT_NODUP; |
| 631 | if (dbr == MDB_NOTFOUND) |
| 632 | break; |
| 633 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to get first/next spent output: " + std::string(mdb_strerror(dbr))); |
| 634 | dbr = mdb_cursor_count(cur, &tmp); |
| 635 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to count entries: " + std::string(mdb_strerror(dbr))); |
| 636 | count += tmp; |
| 637 | } |
| 638 | |
| 639 | mdb_cursor_close(cur); |
| 640 | dbr = mdb_txn_commit(txn); |
| 641 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to commit txn: " + std::string(mdb_strerror(dbr))); |
| 642 | tx_active = false; |
| 643 | |
| 644 | return count; |
| 645 | } |
| 646 | |
| 647 | static bool add_spent_output(MDB_cursor *cur, const output_data &od) |
| 648 | { |
| 649 | MDB_val k = {sizeof(od.amount), (void*)&od.amount}; |
| 650 | MDB_val v = {sizeof(od.offset), (void*)&od.offset}; |
| 651 | int dbr = mdb_cursor_put(cur, &k, &v, MDB_NODUPDATA); |
| 652 | if (dbr == MDB_KEYEXIST) |
| 653 | return false; |
| 654 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to add spent output: " + std::string(mdb_strerror(dbr))); |
| 655 | return true; |
| 656 | } |
| 657 | |
| 658 | static bool is_output_spent(MDB_cursor *cur, const output_data &od) |
| 659 | { |
| 660 | MDB_val k = {sizeof(od.amount), (void*)&od.amount}; |
| 661 | MDB_val v = {sizeof(od.offset), (void*)&od.offset}; |
| 662 | int dbr = mdb_cursor_get(cur, &k, &v, MDB_GET_BOTH); |
| 663 | CHECK_AND_ASSERT_THROW_MES(!dbr || dbr == MDB_NOTFOUND, "Failed to get spent output: " + std::string(mdb_strerror(dbr))); |
| 664 | bool spent = dbr == 0; |
| 665 | return spent; |
| 666 | } |
| 667 | |
| 668 | static std::vector<output_data> get_spent_outputs(MDB_txn *txn) |
| 669 | { |
| 670 | MDB_cursor *cur; |
| 671 | int dbr = mdb_cursor_open(txn, dbi_spent, &cur); |
| 672 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open cursor for spent outputs: " + std::string(mdb_strerror(dbr))); |
| 673 | MDB_val k, v; |
| 674 | mdb_size_t count = 0; |
| 675 | dbr = mdb_cursor_get(cur, &k, &v, MDB_FIRST); |
| 676 | if (dbr != MDB_NOTFOUND) |
| 677 | { |
| 678 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to get first spent output: " + std::string(mdb_strerror(dbr))); |
| 679 | dbr = mdb_cursor_count(cur, &count); |
| 680 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to count entries: " + std::string(mdb_strerror(dbr))); |
| 681 | } |
| 682 | std::vector<output_data> outs; |
| 683 | outs.reserve(count); |
| 684 | while (1) |
| 685 | { |
| 686 | outs.push_back({*(const uint64_t*)k.mv_data, *(const uint64_t*)v.mv_data}); |
| 687 | dbr = mdb_cursor_get(cur, &k, &v, MDB_NEXT); |
| 688 | if (dbr == MDB_NOTFOUND) |
| 689 | break; |
| 690 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to get next spent output: " + std::string(mdb_strerror(dbr))); |
| 691 | } |
| 692 | mdb_cursor_close(cur); |
| 693 | return outs; |
| 694 | } |
| 695 | |
| 696 | static void get_per_amount_outputs(MDB_txn *txn, uint64_t amount, uint64_t &total, uint64_t &spent) |
| 697 | { |
| 698 | MDB_cursor *cur; |
| 699 | int dbr = mdb_cursor_open(txn, dbi_per_amount, &cur); |
| 700 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open cursor for per amount outputs: " + std::string(mdb_strerror(dbr))); |
| 701 | MDB_val k, v; |
| 702 | k.mv_size = sizeof(uint64_t); |
| 703 | k.mv_data = (void*)&amount; |
| 704 | dbr = mdb_cursor_get(cur, &k, &v, MDB_SET); |
| 705 | if (dbr == MDB_NOTFOUND) |
| 706 | { |
| 707 | total = spent = 0; |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to get per amount outputs: " + std::string(mdb_strerror(dbr))); |
| 712 | total = ((const uint64_t*)v.mv_data)[0]; |
| 713 | spent = ((const uint64_t*)v.mv_data)[1]; |
| 714 | } |
| 715 | mdb_cursor_close(cur); |
| 716 | } |
| 717 | |
| 718 | static void inc_per_amount_outputs(MDB_txn *txn, uint64_t amount, uint64_t total, uint64_t spent) |
| 719 | { |
| 720 | MDB_cursor *cur; |
| 721 | int dbr = mdb_cursor_open(txn, dbi_per_amount, &cur); |
| 722 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open cursor for per amount outputs: " + std::string(mdb_strerror(dbr))); |
| 723 | MDB_val k, v; |
| 724 | k.mv_size = sizeof(uint64_t); |
| 725 | k.mv_data = (void*)&amount; |
| 726 | dbr = mdb_cursor_get(cur, &k, &v, MDB_SET); |
| 727 | if (dbr == 0) |
| 728 | { |
| 729 | total += ((const uint64_t*)v.mv_data)[0]; |
| 730 | spent += ((const uint64_t*)v.mv_data)[1]; |
| 731 | } |
| 732 | else |
| 733 | { |
| 734 | CHECK_AND_ASSERT_THROW_MES(dbr == MDB_NOTFOUND, "Failed to get per amount outputs: " + std::string(mdb_strerror(dbr))); |
| 735 | } |
| 736 | uint64_t data[2] = {total, spent}; |
| 737 | v.mv_size = 2 * sizeof(uint64_t); |
| 738 | v.mv_data = (void*)data; |
| 739 | dbr = mdb_cursor_put(cur, &k, &v, 0); |
| 740 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to write record for per amount outputs: " + std::string(mdb_strerror(dbr))); |
| 741 | mdb_cursor_close(cur); |
| 742 | } |
| 743 | |
| 744 | static uint64_t get_processed_txidx(const std::string &name) |
| 745 | { |
| 746 | MDB_txn *txn; |
| 747 | bool tx_active = false; |
| 748 | |
| 749 | int dbr = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
| 750 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 751 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 752 | tx_active = true; |
| 753 | |
| 754 | uint64_t height = 0; |
| 755 | MDB_val k, v; |
| 756 | k.mv_data = (void*)name.c_str(); |
| 757 | k.mv_size = name.size(); |
| 758 | dbr = mdb_get(txn, dbi_processed_txidx, &k, &v); |
| 759 | if (dbr != MDB_NOTFOUND) |
| 760 | { |
| 761 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to get processed height: " + std::string(mdb_strerror(dbr))); |
| 762 | height = *(const uint64_t*)v.mv_data; |
| 763 | } |
| 764 | |
| 765 | dbr = mdb_txn_commit(txn); |
| 766 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to commit txn: " + std::string(mdb_strerror(dbr))); |
| 767 | tx_active = false; |
| 768 | |
| 769 | return height; |
| 770 | } |
| 771 | |
| 772 | static void set_processed_txidx(MDB_txn *txn, const std::string &name, uint64_t height) |
| 773 | { |
| 774 | MDB_val k, v; |
| 775 | k.mv_data = (void*)name.c_str(); |
| 776 | k.mv_size = name.size(); |
| 777 | v.mv_data = (void*)&height; |
| 778 | v.mv_size = sizeof(height); |
| 779 | int dbr = mdb_put(txn, dbi_processed_txidx, &k, &v, 0); |
| 780 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to set processed height: " + std::string(mdb_strerror(dbr))); |
| 781 | } |
| 782 | |
| 783 | static bool get_relative_ring(MDB_txn *txn, const crypto::key_image &ki, std::vector<uint64_t> &ring) |
| 784 | { |
| 785 | MDB_val k, v; |
| 786 | k.mv_data = (void*)&ki; |
| 787 | k.mv_size = sizeof(ki); |
| 788 | int dbr = mdb_get(txn, dbi_relative_rings, &k, &v); |
| 789 | if (dbr == MDB_NOTFOUND) |
| 790 | return false; |
| 791 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to get relative ring: " + std::string(mdb_strerror(dbr))); |
| 792 | ring = decompress_ring(std::string((const char*)v.mv_data, v.mv_size)); |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | static void set_relative_ring(MDB_txn *txn, const crypto::key_image &ki, const std::vector<uint64_t> &ring) |
| 797 | { |
| 798 | const std::string sring = compress_ring(ring); |
| 799 | MDB_val k, v; |
| 800 | k.mv_data = (void*)&ki; |
| 801 | k.mv_size = sizeof(ki); |
| 802 | v.mv_data = (void*)sring.c_str(); |
| 803 | v.mv_size = sring.size(); |
| 804 | int dbr = mdb_put(txn, dbi_relative_rings, &k, &v, 0); |
| 805 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to set relative ring: " + std::string(mdb_strerror(dbr))); |
| 806 | } |
| 807 | |
| 808 | static std::string keep_under_511(const std::string &s) |
| 809 | { |
| 810 | if (s.size() <= 511) |
| 811 | return s; |
| 812 | crypto::hash hash; |
| 813 | crypto::cn_fast_hash(s.data(), s.size(), hash); |
| 814 | return std::string((const char*)&hash, 32); |
| 815 | } |
| 816 | |
| 817 | static uint64_t get_ring_instances(MDB_txn *txn, uint64_t amount, const std::vector<uint64_t> &ring) |
| 818 | { |
| 819 | const std::string sring = keep_under_511(compress_ring(amount, ring)); |
| 820 | MDB_val k, v; |
| 821 | k.mv_data = (void*)sring.data(); |
| 822 | k.mv_size = sring.size(); |
| 823 | int dbr = mdb_get(txn, dbi_ring_instances, &k, &v); |
| 824 | if (dbr == MDB_NOTFOUND) |
| 825 | return 0; |
| 826 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to get ring instances: " + std::string(mdb_strerror(dbr))); |
| 827 | return *(const uint64_t*)v.mv_data; |
| 828 | } |
| 829 | |
| 830 | static uint64_t get_ring_subset_instances(MDB_txn *txn, uint64_t amount, const std::vector<uint64_t> &ring) |
| 831 | { |
| 832 | uint64_t instances = get_ring_instances(txn, amount, ring); |
| 833 | if (ring.size() > 11) |
| 834 | return instances; |
| 835 | |
| 836 | uint64_t extra = 0; |
| 837 | std::vector<uint64_t> subset; |
| 838 | subset.reserve(ring.size()); |
| 839 | for (uint64_t mask = 1; mask < (((uint64_t)1) << ring.size()) - 1; ++mask) |
| 840 | { |
| 841 | subset.resize(0); |
| 842 | for (size_t i = 0; i < ring.size(); ++i) |
| 843 | if ((mask >> i) & 1) |
| 844 | subset.push_back(ring[i]); |
| 845 | extra += get_ring_instances(txn, amount, subset); |
| 846 | } |
| 847 | return instances + extra; |
| 848 | } |
| 849 | |
| 850 | static uint64_t inc_ring_instances(MDB_txn *txn, uint64_t amount, const std::vector<uint64_t> &ring) |
| 851 | { |
| 852 | const std::string sring = keep_under_511(compress_ring(amount, ring)); |
| 853 | MDB_val k, v; |
| 854 | k.mv_data = (void*)sring.data(); |
| 855 | k.mv_size = sring.size(); |
| 856 | |
| 857 | int dbr = mdb_get(txn, dbi_ring_instances, &k, &v); |
| 858 | CHECK_AND_ASSERT_THROW_MES(!dbr || dbr == MDB_NOTFOUND, "Failed to get ring instances: " + std::string(mdb_strerror(dbr))); |
| 859 | |
| 860 | uint64_t count; |
| 861 | if (dbr == MDB_NOTFOUND) |
| 862 | count = 1; |
| 863 | else |
| 864 | count = 1 + *(const uint64_t*)v.mv_data; |
| 865 | |
| 866 | v.mv_data = &count; |
| 867 | v.mv_size = sizeof(count); |
| 868 | dbr = mdb_put(txn, dbi_ring_instances, &k, &v, 0); |
| 869 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to set ring instances: " + std::string(mdb_strerror(dbr))); |
| 870 | |
| 871 | return count; |
| 872 | } |
| 873 | |
| 874 | static std::vector<crypto::key_image> get_key_images(MDB_txn *txn, const output_data &od) |
| 875 | { |
| 876 | MDB_val k, v; |
| 877 | k.mv_data = (void*)&od; |
| 878 | k.mv_size = sizeof(od); |
| 879 | int dbr = mdb_get(txn, dbi_outputs, &k, &v); |
| 880 | CHECK_AND_ASSERT_THROW_MES(!dbr || dbr == MDB_NOTFOUND, "Failed to get output: " + std::string(mdb_strerror(dbr))); |
| 881 | if (dbr == MDB_NOTFOUND) |
| 882 | return {}; |
| 883 | CHECK_AND_ASSERT_THROW_MES(v.mv_size % 32 == 0, "Unexpected record size"); |
| 884 | std::vector<crypto::key_image> key_images; |
| 885 | key_images.reserve(v.mv_size / 32); |
| 886 | const crypto::key_image *ki = (const crypto::key_image*)v.mv_data; |
| 887 | for (size_t n = 0; n < v.mv_size / 32; ++n) |
| 888 | key_images.push_back(*ki++); |
| 889 | return key_images; |
| 890 | } |
| 891 | |
| 892 | static void add_key_image(MDB_txn *txn, const output_data &od, const crypto::key_image &ki) |
| 893 | { |
| 894 | MDB_val k, v; |
| 895 | k.mv_data = (void*)&od; |
| 896 | k.mv_size = sizeof(od); |
| 897 | int dbr = mdb_get(txn, dbi_outputs, &k, &v); |
| 898 | CHECK_AND_ASSERT_THROW_MES(!dbr || dbr == MDB_NOTFOUND, "Failed to get output"); |
| 899 | std::string data; |
| 900 | if (!dbr) |
| 901 | { |
| 902 | CHECK_AND_ASSERT_THROW_MES(v.mv_size % 32 == 0, "Unexpected record size"); |
| 903 | data = std::string((const char*)v.mv_data, v.mv_size); |
| 904 | } |
| 905 | data += std::string((const char*)&ki, sizeof(ki)); |
| 906 | |
| 907 | v.mv_data = (void*)data.data(); |
| 908 | v.mv_size = data.size(); |
| 909 | dbr = mdb_put(txn, dbi_outputs, &k, &v, 0); |
| 910 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to set outputs: " + std::string(mdb_strerror(dbr))); |
| 911 | } |
| 912 | |
| 913 | static bool get_stat(MDB_txn *txn, const char *key, uint64_t &data) |
| 914 | { |
| 915 | MDB_val k, v; |
| 916 | k.mv_data = (void*)key; |
| 917 | k.mv_size = strlen(key); |
| 918 | int dbr = mdb_get(txn, dbi_stats, &k, &v); |
| 919 | if (dbr == MDB_NOTFOUND) |
| 920 | return false; |
| 921 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to get stat record"); |
| 922 | CHECK_AND_ASSERT_THROW_MES(v.mv_size == sizeof(uint64_t), "Unexpected record size"); |
| 923 | data = *(const uint64_t*)v.mv_data; |
| 924 | return true; |
| 925 | } |
| 926 | |
| 927 | static void set_stat(MDB_txn *txn, const char *key, uint64_t data) |
| 928 | { |
| 929 | MDB_val k, v; |
| 930 | k.mv_data = (void*)key; |
| 931 | k.mv_size = strlen(key); |
| 932 | v.mv_data = (void*)&data; |
| 933 | v.mv_size = sizeof(uint64_t); |
| 934 | int dbr = mdb_put(txn, dbi_stats, &k, &v, 0); |
| 935 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to set stat record"); |
| 936 | } |
| 937 | |
| 938 | static void inc_stat(MDB_txn *txn, const char *key) |
| 939 | { |
| 940 | uint64_t data; |
| 941 | if (!get_stat(txn, key, data)) |
| 942 | data = 0; |
| 943 | ++data; |
| 944 | set_stat(txn, key, data); |
| 945 | } |
| 946 | |
| 947 | static void open_db(const std::string &filename, MDB_env **env, MDB_txn **txn, MDB_cursor **cur, MDB_dbi *dbi) |
| 948 | { |
| 949 | tools::create_directories_if_necessary(filename); |
| 950 | |
| 951 | int flags = MDB_RDONLY; |
| 952 | if (db_flags & DBF_FAST) |
| 953 | flags |= MDB_NOSYNC; |
| 954 | if (db_flags & DBF_FASTEST) |
| 955 | flags |= MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC; |
| 956 | |
| 957 | int dbr = mdb_env_create(env); |
| 958 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LDMB environment: " + std::string(mdb_strerror(dbr))); |
| 959 | dbr = mdb_env_set_maxdbs(*env, 1); |
| 960 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to set max env dbs: " + std::string(mdb_strerror(dbr))); |
| 961 | const std::string actual_filename = filename; |
| 962 | MINFO("Opening monero blockchain at " << actual_filename); |
| 963 | dbr = mdb_env_open(*env, actual_filename.c_str(), flags, 0664); |
| 964 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open rings database file '" |
| 965 | + actual_filename + "': " + std::string(mdb_strerror(dbr))); |
| 966 | |
| 967 | dbr = mdb_txn_begin(*env, NULL, MDB_RDONLY, txn); |
| 968 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 969 | |
| 970 | dbr = mdb_dbi_open(*txn, "output_amounts", MDB_CREATE | MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, dbi); |
| 971 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 972 | mdb_set_dupsort(*txn, *dbi, compare_uint64); |
| 973 | |
| 974 | dbr = mdb_cursor_open(*txn, *dbi, cur); |
| 975 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 976 | } |
| 977 | |
| 978 | static void close_db(MDB_env *env, MDB_txn *txn, MDB_cursor *cur, MDB_dbi dbi) |
| 979 | { |
| 980 | mdb_txn_abort(txn); |
| 981 | mdb_cursor_close(cur); |
| 982 | mdb_dbi_close(env, dbi); |
| 983 | mdb_env_close(env); |
| 984 | } |
| 985 | |
| 986 | static void get_num_outputs(MDB_txn *txn, MDB_cursor *cur, MDB_dbi dbi, uint64_t &pre_rct, uint64_t &rct) |
| 987 | { |
| 988 | uint64_t amount = 0; |
| 989 | MDB_val k = { sizeof(amount), (void*)&amount }, v; |
| 990 | int dbr = mdb_cursor_get(cur, &k, &v, MDB_SET); |
| 991 | if (dbr == MDB_NOTFOUND) |
| 992 | { |
| 993 | rct = 0; |
| 994 | } |
| 995 | else |
| 996 | { |
| 997 | if (dbr) throw std::runtime_error("Record 0 not found: " + std::string(mdb_strerror(dbr))); |
| 998 | mdb_size_t count = 0; |
| 999 | dbr = mdb_cursor_count(cur, &count); |
| 1000 | if (dbr) throw std::runtime_error("Failed to count records: " + std::string(mdb_strerror(dbr))); |
| 1001 | rct = count; |
| 1002 | } |
| 1003 | MDB_stat s; |
| 1004 | dbr = mdb_stat(txn, dbi, &s); |
| 1005 | if (dbr) throw std::runtime_error("Failed to count records: " + std::string(mdb_strerror(dbr))); |
| 1006 | if (s.ms_entries < rct) throw std::runtime_error("Inconsistent records: " + std::string(mdb_strerror(dbr))); |
| 1007 | pre_rct = s.ms_entries - rct; |
| 1008 | } |
| 1009 | |
| 1010 | static crypto::hash get_genesis_block_hash(const std::string &filename) |
| 1011 | { |
| 1012 | MDB_env *env; |
| 1013 | MDB_dbi dbi; |
| 1014 | MDB_txn *txn; |
| 1015 | int dbr; |
| 1016 | bool tx_active = false; |
| 1017 | |
| 1018 | dbr = mdb_env_create(&env); |
| 1019 | if (dbr) throw std::runtime_error("Failed to create LDMB environment: " + std::string(mdb_strerror(dbr))); |
| 1020 | dbr = mdb_env_set_maxdbs(env, 1); |
| 1021 | if (dbr) throw std::runtime_error("Failed to set max env dbs: " + std::string(mdb_strerror(dbr))); |
| 1022 | const std::string actual_filename = filename; |
| 1023 | dbr = mdb_env_open(env, actual_filename.c_str(), 0, 0664); |
| 1024 | if (dbr) throw std::runtime_error("Failed to open rings database file '" |
| 1025 | + actual_filename + "': " + std::string(mdb_strerror(dbr))); |
| 1026 | |
| 1027 | dbr = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
| 1028 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1029 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 1030 | tx_active = true; |
| 1031 | |
| 1032 | dbr = mdb_dbi_open(txn, "block_info", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, &dbi); |
| 1033 | mdb_set_dupsort(txn, dbi, compare_uint64); |
| 1034 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 1035 | uint64_t zero = 0; |
| 1036 | MDB_val k = { sizeof(uint64_t), (void*)&zero}, v; |
| 1037 | dbr = mdb_get(txn, dbi, &k, &v); |
| 1038 | if (dbr) throw std::runtime_error("Failed to retrieve genesis block: " + std::string(mdb_strerror(dbr))); |
| 1039 | crypto::hash genesis_block_hash = *(const crypto::hash*)(((const uint64_t*)v.mv_data) + 5); |
| 1040 | mdb_dbi_close(env, dbi); |
| 1041 | mdb_txn_abort(txn); |
| 1042 | mdb_env_close(env); |
| 1043 | tx_active = false; |
| 1044 | return genesis_block_hash; |
| 1045 | } |
| 1046 | |
| 1047 | static std::vector<std::pair<uint64_t, uint64_t>> load_outputs(const std::string &filename) |
| 1048 | { |
| 1049 | std::vector<std::pair<uint64_t, uint64_t>> outputs; |
| 1050 | uint64_t amount = std::numeric_limits<uint64_t>::max(); |
| 1051 | FILE *f; |
| 1052 | |
| 1053 | f = fopen(filename.c_str(), "r"); |
| 1054 | if (!f) |
| 1055 | { |
| 1056 | MERROR("Failed to load outputs from " << filename << ": " << strerror(errno)); |
| 1057 | return {}; |
| 1058 | } |
| 1059 | while (1) |
| 1060 | { |
| 1061 | char s[256]; |
| 1062 | if (!fgets(s, sizeof(s), f)) |
| 1063 | { |
| 1064 | MERROR("Error reading from " << filename << ": " << strerror(errno)); |
| 1065 | break; |
| 1066 | } |
| 1067 | if (feof(f)) |
| 1068 | break; |
| 1069 | const size_t len = strlen(s); |
| 1070 | if (len > 0 && s[len - 1] == '\n') |
| 1071 | s[len - 1] = 0; |
| 1072 | if (!s[0]) |
| 1073 | continue; |
| 1074 | uint64_t offset, num_offsets; |
| 1075 | if (sscanf(s, "@%" PRIu64, &amount) == 1) |
| 1076 | { |
| 1077 | continue; |
| 1078 | } |
| 1079 | if (amount == std::numeric_limits<uint64_t>::max()) |
| 1080 | { |
| 1081 | MERROR("Bad format in " << filename); |
| 1082 | continue; |
| 1083 | } |
| 1084 | if (sscanf(s, "%" PRIu64 "*%" PRIu64, &offset, &num_offsets) == 2 && num_offsets < std::numeric_limits<uint64_t>::max() - offset) |
| 1085 | { |
| 1086 | while (num_offsets-- > 0) |
| 1087 | outputs.push_back(std::make_pair(amount, offset++)); |
| 1088 | } |
| 1089 | else if (sscanf(s, "%" PRIu64, &offset) == 1) |
| 1090 | { |
| 1091 | outputs.push_back(std::make_pair(amount, offset)); |
| 1092 | } |
| 1093 | else |
| 1094 | { |
| 1095 | MERROR("Bad format in " << filename); |
| 1096 | continue; |
| 1097 | } |
| 1098 | } |
| 1099 | fclose(f); |
| 1100 | return outputs; |
| 1101 | } |
| 1102 | |
| 1103 | static bool export_spent_outputs(MDB_cursor *cur, const std::string &filename) |
| 1104 | { |
| 1105 | FILE *f = fopen(filename.c_str(), "w"); |
| 1106 | if (!f) |
| 1107 | { |
| 1108 | MERROR("Failed to open " << filename << ": " << strerror(errno)); |
| 1109 | return false; |
| 1110 | } |
| 1111 | |
| 1112 | uint64_t pending_amount = std::numeric_limits<uint64_t>::max(); |
| 1113 | std::vector<uint64_t> pending_offsets; |
| 1114 | MDB_val k, v; |
| 1115 | MDB_cursor_op op = MDB_FIRST; |
| 1116 | while (1) |
| 1117 | { |
| 1118 | int dbr = mdb_cursor_get(cur, &k, &v, op); |
| 1119 | if (dbr == MDB_NOTFOUND) |
| 1120 | break; |
| 1121 | op = MDB_NEXT; |
| 1122 | if (dbr) |
| 1123 | { |
| 1124 | fclose(f); |
| 1125 | MERROR("Failed to enumerate spent outputs: " << mdb_strerror(dbr)); |
| 1126 | return false; |
| 1127 | } |
| 1128 | const uint64_t amount = *(const uint64_t*)k.mv_data; |
| 1129 | const uint64_t offset = *(const uint64_t*)v.mv_data; |
| 1130 | if (!pending_offsets.empty() && (amount != pending_amount || pending_offsets.back()+1 != offset)) |
| 1131 | { |
| 1132 | if (pending_offsets.size() == 1) |
| 1133 | fprintf(f, "%" PRIu64 "\n", pending_offsets.front()); |
| 1134 | else |
| 1135 | fprintf(f, "%" PRIu64 "*%zu\n", pending_offsets.front(), pending_offsets.size()); |
| 1136 | pending_offsets.clear(); |
| 1137 | } |
| 1138 | if (pending_amount != amount) |
| 1139 | { |
| 1140 | fprintf(f, "@%" PRIu64 "\n", amount); |
| 1141 | pending_amount = amount; |
| 1142 | } |
| 1143 | pending_offsets.push_back(offset); |
| 1144 | } |
| 1145 | if (!pending_offsets.empty()) |
| 1146 | { |
| 1147 | if (pending_offsets.size() == 1) |
| 1148 | fprintf(f, "%" PRIu64 "\n", pending_offsets.front()); |
| 1149 | else |
| 1150 | fprintf(f, "%" PRIu64 "*%zu\n", pending_offsets.front(), pending_offsets.size()); |
| 1151 | pending_offsets.clear(); |
| 1152 | } |
| 1153 | fclose(f); |
| 1154 | return true; |
| 1155 | } |
| 1156 | |
| 1157 | int main(int argc, char* argv[]) |
| 1158 | { |
| 1159 | TRY_ENTRY(); |
| 1160 | |
| 1161 | epee::string_tools::set_module_name_and_folder(argv[0]); |
| 1162 | |
| 1163 | uint32_t log_level = 0; |
| 1164 | |
| 1165 | tools::on_startup(); |
| 1166 | |
| 1167 | boost::filesystem::path output_file_path; |
| 1168 | |
| 1169 | po::options_description desc_cmd_only("Command line options"); |
| 1170 | po::options_description desc_cmd_sett("Command line options and settings options"); |
| 1171 | const command_line::arg_descriptor<std::string> arg_blackball_db_dir = { |
| 1172 | "spent-output-db-dir", "Specify spent output database directory", |
| 1173 | get_default_db_path(), |
| 1174 | }; |
| 1175 | const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""}; |
| 1176 | const command_line::arg_descriptor<bool> arg_rct_only = {"rct-only", "Only work on ringCT outputs", false}; |
| 1177 | const command_line::arg_descriptor<bool> arg_check_subsets = {"check-subsets", "Check ring subsets (very expensive)", false}; |
| 1178 | const command_line::arg_descriptor<bool> arg_verbose = {"verbose", "Verbose output)", false}; |
| 1179 | const command_line::arg_descriptor<std::vector<std::string> > arg_inputs = {"inputs", "Path to Monero DB, and path to any fork DBs"}; |
| 1180 | const command_line::arg_descriptor<std::string> arg_db_sync_mode = { |
| 1181 | "db-sync-mode" |
| 1182 | , "Specify sync option, using format [safe|fast|fastest]:[nrecords_per_sync]." |
| 1183 | , "fast:1000" |
| 1184 | }; |
| 1185 | const command_line::arg_descriptor<std::string> arg_extra_spent_list = {"extra-spent-list", "Optional list of known spent outputs",""}; |
| 1186 | const command_line::arg_descriptor<std::string> arg_export = {"export", "Filename to export the backball list to"}; |
| 1187 | const command_line::arg_descriptor<bool> arg_force_chain_reaction_pass = {"force-chain-reaction-pass", "Run the chain reaction pass even if no new blockchain data was processed"}; |
| 1188 | const command_line::arg_descriptor<bool> arg_historical_stat = {"historical-stat", "Report historical stat of spent outputs for every 10000 blocks window"}; |
| 1189 | |
| 1190 | command_line::add_arg(desc_cmd_sett, arg_blackball_db_dir); |
| 1191 | command_line::add_arg(desc_cmd_sett, arg_log_level); |
| 1192 | command_line::add_arg(desc_cmd_sett, arg_rct_only); |
| 1193 | command_line::add_arg(desc_cmd_sett, arg_check_subsets); |
| 1194 | command_line::add_arg(desc_cmd_sett, arg_verbose); |
| 1195 | command_line::add_arg(desc_cmd_sett, arg_db_sync_mode); |
| 1196 | command_line::add_arg(desc_cmd_sett, arg_extra_spent_list); |
| 1197 | command_line::add_arg(desc_cmd_sett, arg_export); |
| 1198 | command_line::add_arg(desc_cmd_sett, arg_force_chain_reaction_pass); |
| 1199 | command_line::add_arg(desc_cmd_sett, arg_historical_stat); |
| 1200 | command_line::add_arg(desc_cmd_sett, arg_inputs); |
| 1201 | command_line::add_arg(desc_cmd_only, command_line::arg_help); |
| 1202 | |
| 1203 | po::options_description desc_options("Allowed options"); |
| 1204 | desc_options.add(desc_cmd_only).add(desc_cmd_sett); |
| 1205 | |
| 1206 | po::positional_options_description positional_options; |
| 1207 | positional_options.add(arg_inputs.name, -1); |
| 1208 | |
| 1209 | po::variables_map vm; |
| 1210 | bool r = command_line::handle_error_helper(desc_options, [&]() |
| 1211 | { |
| 1212 | auto parser = po::command_line_parser(argc, argv).options(desc_options).positional(positional_options); |
| 1213 | po::store(parser.run(), vm); |
| 1214 | po::notify(vm); |
| 1215 | return true; |
| 1216 | }); |
| 1217 | if (! r) |
| 1218 | return 1; |
| 1219 | |
| 1220 | if (command_line::get_arg(vm, command_line::arg_help)) |
| 1221 | { |
| 1222 | std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL; |
| 1223 | std::cout << desc_options << std::endl; |
| 1224 | return 1; |
| 1225 | } |
| 1226 | |
| 1227 | mlog_configure(mlog_get_default_log_path("monero-blockchain-mark-spent-outputs.log"), true); |
| 1228 | if (!command_line::is_arg_defaulted(vm, arg_log_level)) |
| 1229 | mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str()); |
| 1230 | else |
| 1231 | mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str()); |
| 1232 | |
| 1233 | LOG_PRINT_L0("Starting..."); |
| 1234 | |
| 1235 | output_file_path = command_line::get_arg(vm, arg_blackball_db_dir); |
| 1236 | bool opt_rct_only = command_line::get_arg(vm, arg_rct_only); |
| 1237 | bool opt_check_subsets = command_line::get_arg(vm, arg_check_subsets); |
| 1238 | bool opt_verbose = command_line::get_arg(vm, arg_verbose); |
| 1239 | bool opt_force_chain_reaction_pass = command_line::get_arg(vm, arg_force_chain_reaction_pass); |
| 1240 | bool opt_historical_stat = command_line::get_arg(vm, arg_historical_stat); |
| 1241 | std::string opt_export = command_line::get_arg(vm, arg_export); |
| 1242 | std::string extra_spent_list = command_line::get_arg(vm, arg_extra_spent_list); |
| 1243 | std::vector<std::pair<uint64_t, uint64_t>> extra_spent_outputs = extra_spent_list.empty() ? std::vector<std::pair<uint64_t, uint64_t>>() : load_outputs(extra_spent_list); |
| 1244 | |
| 1245 | |
| 1246 | std::string db_sync_mode = command_line::get_arg(vm, arg_db_sync_mode); |
| 1247 | if (!parse_db_sync_mode(db_sync_mode)) |
| 1248 | { |
| 1249 | MERROR("Invalid db sync mode: " << db_sync_mode); |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
| 1253 | const std::vector<std::string> inputs = command_line::get_arg(vm, arg_inputs); |
| 1254 | if (inputs.empty()) |
| 1255 | { |
| 1256 | LOG_PRINT_L0("No inputs given"); |
| 1257 | return 1; |
| 1258 | } |
| 1259 | |
| 1260 | const std::string cache_dir = (output_file_path / "spent-outputs-cache").string(); |
| 1261 | init(cache_dir); |
| 1262 | |
| 1263 | LOG_PRINT_L0("Scanning for spent outputs..."); |
| 1264 | |
| 1265 | const uint64_t start_blackballed_outputs = get_num_spent_outputs(); |
| 1266 | |
| 1267 | tools::ringdb ringdb(output_file_path.string(), epee::string_tools::pod_to_hex(get_genesis_block_hash(inputs[0]))); |
| 1268 | |
| 1269 | bool stop_requested = false; |
| 1270 | tools::signal_handler::install([&stop_requested](int type) { |
| 1271 | stop_requested = true; |
| 1272 | }); |
| 1273 | |
| 1274 | int dbr = resize_env(cache_dir.c_str()); |
| 1275 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to resize LMDB database: " + std::string(mdb_strerror(dbr))); |
| 1276 | |
| 1277 | // open first db |
| 1278 | MDB_env *env0; |
| 1279 | MDB_txn *txn0; |
| 1280 | MDB_dbi dbi0; |
| 1281 | MDB_cursor *cur0; |
| 1282 | open_db(inputs[0], &env0, &txn0, &cur0, &dbi0); |
| 1283 | |
| 1284 | std::vector<output_data> work_spent; |
| 1285 | |
| 1286 | if (opt_historical_stat) |
| 1287 | { |
| 1288 | if (!start_blackballed_outputs) |
| 1289 | { |
| 1290 | MINFO("Spent outputs database is empty. Either you haven't run the analysis mode yet, or there is really no output marked as spent."); |
| 1291 | goto skip_secondary_passes; |
| 1292 | } |
| 1293 | MDB_txn *txn; |
| 1294 | int dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 1295 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1296 | MDB_cursor *cur; |
| 1297 | dbr = mdb_cursor_open(txn, dbi_spent, &cur); |
| 1298 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 1299 | |
| 1300 | const uint64_t STAT_WINDOW = 10000; |
| 1301 | uint64_t outs_total = 0; |
| 1302 | uint64_t outs_spent = 0; |
| 1303 | std::unordered_map<uint64_t, uint64_t> outs_per_amount; |
| 1304 | uint64_t start_idx = 0, n_txes; |
| 1305 | uint64_t prev_height = 0; |
| 1306 | for_all_transactions(inputs[0], start_idx, n_txes, [&](bool last_tx, uint64_t height, const cryptonote::transaction_prefix &tx)->bool |
| 1307 | { |
| 1308 | if (height != prev_height) |
| 1309 | { |
| 1310 | if (height % 100 == 0) std::cout << "\r" << height << ": " << (100.0f * outs_spent / outs_total) << "% ( " << outs_spent << " / " << outs_total << " ) \r" << std::flush; |
| 1311 | if (height % STAT_WINDOW == 0) |
| 1312 | { |
| 1313 | uint64_t window_front = (height / STAT_WINDOW - 1) * STAT_WINDOW; |
| 1314 | uint64_t window_back = window_front + STAT_WINDOW - 1; |
| 1315 | LOG_PRINT_L0(window_front << "-" << window_back << ": " << (100.0f * outs_spent / outs_total) << "% ( " << outs_spent << " / " << outs_total << " )"); |
| 1316 | outs_total = outs_spent = 0; |
| 1317 | } |
| 1318 | } |
| 1319 | prev_height = height; |
| 1320 | for (const auto &out: tx.vout) |
| 1321 | { |
| 1322 | ++outs_total; |
| 1323 | CHECK_AND_ASSERT_THROW_MES(out.target.type() == typeid(txout_to_key), "Out target type is not txout_to_key: height=" + std::to_string(height)); |
| 1324 | uint64_t out_global_index = outs_per_amount[out.amount]++; |
| 1325 | if (is_output_spent(cur, output_data(out.amount, out_global_index))) |
| 1326 | ++outs_spent; |
| 1327 | } |
| 1328 | if (last_tx) |
| 1329 | { |
| 1330 | uint64_t window_front = (height / STAT_WINDOW) * STAT_WINDOW; |
| 1331 | uint64_t window_back = height; |
| 1332 | LOG_PRINT_L0(window_front << "-" << window_back << ": " << (100.0f * outs_spent / outs_total) << "% ( " << outs_spent << " / " << outs_total << " )"); |
| 1333 | } |
| 1334 | if (stop_requested) |
| 1335 | { |
| 1336 | MINFO("Stopping scan..."); |
| 1337 | return false; |
| 1338 | } |
| 1339 | return true; |
| 1340 | }); |
| 1341 | mdb_cursor_close(cur); |
| 1342 | dbr = mdb_txn_commit(txn); |
| 1343 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr))); |
| 1344 | goto skip_secondary_passes; |
| 1345 | } |
| 1346 | |
| 1347 | if (!extra_spent_outputs.empty()) |
| 1348 | { |
| 1349 | MINFO("Adding " << extra_spent_outputs.size() << " extra spent outputs"); |
| 1350 | MDB_txn *txn; |
| 1351 | int dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 1352 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1353 | MDB_cursor *cur; |
| 1354 | dbr = mdb_cursor_open(txn, dbi_spent, &cur); |
| 1355 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 1356 | |
| 1357 | std::vector<std::pair<uint64_t, uint64_t>> blackballs; |
| 1358 | for (const std::pair<uint64_t, uint64_t> &output: extra_spent_outputs) |
| 1359 | { |
| 1360 | if (!is_output_spent(cur, output_data(output.first, output.second))) |
| 1361 | { |
| 1362 | blackballs.push_back(output); |
| 1363 | if (add_spent_output(cur, output_data(output.first, output.second))) |
| 1364 | inc_stat(txn, output.first ? "pre-rct-extra" : "rct-ring-extra"); |
| 1365 | } |
| 1366 | } |
| 1367 | if (!blackballs.empty()) |
| 1368 | { |
| 1369 | ringdb.blackball(blackballs); |
| 1370 | blackballs.clear(); |
| 1371 | } |
| 1372 | mdb_cursor_close(cur); |
| 1373 | dbr = mdb_txn_commit(txn); |
| 1374 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr))); |
| 1375 | } |
| 1376 | |
| 1377 | for (size_t n = 0; n < inputs.size(); ++n) |
| 1378 | { |
| 1379 | const std::string canonical = boost::filesystem::canonical(inputs[n]).string(); |
| 1380 | uint64_t start_idx = get_processed_txidx(canonical); |
| 1381 | if (n > 0 && start_idx == 0) |
| 1382 | { |
| 1383 | start_idx = find_first_diverging_transaction(inputs[0], inputs[n]); |
| 1384 | LOG_PRINT_L0("First diverging transaction at " << start_idx); |
| 1385 | } |
| 1386 | LOG_PRINT_L0("Reading blockchain from " << inputs[n] << " from " << start_idx); |
| 1387 | MDB_txn *txn; |
| 1388 | int dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 1389 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1390 | MDB_cursor *cur; |
| 1391 | dbr = mdb_cursor_open(txn, dbi_spent, &cur); |
| 1392 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 1393 | size_t records = 0; |
| 1394 | const std::string filename = inputs[n]; |
| 1395 | std::vector<std::pair<uint64_t, uint64_t>> blackballs; |
| 1396 | uint64_t n_txes; |
| 1397 | for_all_transactions(filename, start_idx, n_txes, [&](const cryptonote::transaction_prefix &tx)->bool |
| 1398 | { |
| 1399 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1400 | const bool miner_tx = tx.vin.size() == 1 && tx.vin[0].type() == typeid(txin_gen); |
| 1401 | for (const auto &in: tx.vin) |
| 1402 | { |
| 1403 | if (in.type() != typeid(txin_to_key)) |
| 1404 | continue; |
| 1405 | const auto &txin = boost::get<txin_to_key>(in); |
| 1406 | if (opt_rct_only && txin.amount != 0) |
| 1407 | continue; |
| 1408 | |
| 1409 | const std::vector<uint64_t> absolute = cryptonote::relative_output_offsets_to_absolute(txin.key_offsets); |
| 1410 | if (n == 0) |
| 1411 | for (uint64_t out: absolute) |
| 1412 | add_key_image(txn, output_data(txin.amount, out), txin.k_image); |
| 1413 | |
| 1414 | std::vector<uint64_t> relative_ring; |
| 1415 | std::vector<uint64_t> new_ring = canonicalize(txin.key_offsets); |
| 1416 | const uint32_t ring_size = txin.key_offsets.size(); |
| 1417 | const uint64_t instances = inc_ring_instances(txn, txin.amount, new_ring); |
| 1418 | uint64_t pa_total = 0, pa_spent = 0; |
| 1419 | if (!opt_rct_only) |
| 1420 | get_per_amount_outputs(txn, txin.amount, pa_total, pa_spent); |
| 1421 | if (n == 0 && ring_size == 1) |
| 1422 | { |
| 1423 | const std::pair<uint64_t, uint64_t> output = std::make_pair(txin.amount, absolute[0]); |
| 1424 | if (opt_verbose) |
| 1425 | { |
| 1426 | MINFO("Marking output " << output.first << "/" << output.second << " as spent, due to being used in a 1-ring"); |
| 1427 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1428 | } |
| 1429 | blackballs.push_back(output); |
| 1430 | if (add_spent_output(cur, output_data(txin.amount, absolute[0]))) |
| 1431 | inc_stat(txn, txin.amount ? "pre-rct-ring-size-1" : "rct-ring-size-1"); |
| 1432 | } |
| 1433 | else if (n == 0 && instances == new_ring.size()) |
| 1434 | { |
| 1435 | for (size_t o = 0; o < new_ring.size(); ++o) |
| 1436 | { |
| 1437 | const std::pair<uint64_t, uint64_t> output = std::make_pair(txin.amount, absolute[o]); |
| 1438 | if (opt_verbose) |
| 1439 | { |
| 1440 | MINFO("Marking output " << output.first << "/" << output.second << " as spent, due to being used in " << new_ring.size() << " identical " << new_ring.size() << "-rings"); |
| 1441 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1442 | } |
| 1443 | blackballs.push_back(output); |
| 1444 | if (add_spent_output(cur, output_data(txin.amount, absolute[o]))) |
| 1445 | inc_stat(txn, txin.amount ? "pre-rct-duplicate-rings" : "rct-duplicate-rings"); |
| 1446 | } |
| 1447 | } |
| 1448 | else if (n == 0 && !opt_rct_only && pa_spent + 1 == pa_total) |
| 1449 | { |
| 1450 | for (size_t o = 0; o < pa_total; ++o) |
| 1451 | { |
| 1452 | const std::pair<uint64_t, uint64_t> output = std::make_pair(txin.amount, o); |
| 1453 | if (opt_verbose) |
| 1454 | { |
| 1455 | MINFO("Marking output " << output.first << "/" << output.second << " as spent, due to as many outputs of that amount being spent as exist so far"); |
| 1456 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1457 | } |
| 1458 | blackballs.push_back(output); |
| 1459 | if (add_spent_output(cur, output_data(txin.amount, o))) |
| 1460 | inc_stat(txn, txin.amount ? "pre-rct-full-count" : "rct-full-count"); |
| 1461 | } |
| 1462 | } |
| 1463 | else if (n == 0 && opt_check_subsets && get_ring_subset_instances(txn, txin.amount, new_ring) >= new_ring.size()) |
| 1464 | { |
| 1465 | for (size_t o = 0; o < new_ring.size(); ++o) |
| 1466 | { |
| 1467 | const std::pair<uint64_t, uint64_t> output = std::make_pair(txin.amount, absolute[o]); |
| 1468 | if (opt_verbose) |
| 1469 | { |
| 1470 | MINFO("Marking output " << output.first << "/" << output.second << " as spent, due to being used in " << new_ring.size() << " subsets of " << new_ring.size() << "-rings"); |
| 1471 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1472 | } |
| 1473 | blackballs.push_back(output); |
| 1474 | if (add_spent_output(cur, output_data(txin.amount, absolute[o]))) |
| 1475 | inc_stat(txn, txin.amount ? "pre-rct-subset-rings" : "rct-subset-rings"); |
| 1476 | } |
| 1477 | } |
| 1478 | else if (n > 0 && get_relative_ring(txn, txin.k_image, relative_ring)) |
| 1479 | { |
| 1480 | MDEBUG("Key image " << txin.k_image << " already seen: rings " << |
| 1481 | boost::join(relative_ring | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " ") << |
| 1482 | ", " << boost::join(txin.key_offsets | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " ")); |
| 1483 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1484 | if (relative_ring != txin.key_offsets) |
| 1485 | { |
| 1486 | MDEBUG("Rings are different"); |
| 1487 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1488 | const std::vector<uint64_t> r0 = cryptonote::relative_output_offsets_to_absolute(relative_ring); |
| 1489 | const std::vector<uint64_t> r1 = cryptonote::relative_output_offsets_to_absolute(txin.key_offsets); |
| 1490 | std::vector<uint64_t> common; |
| 1491 | for (uint64_t out: r0) |
| 1492 | { |
| 1493 | if (std::find(r1.begin(), r1.end(), out) != r1.end()) |
| 1494 | common.push_back(out); |
| 1495 | } |
| 1496 | if (common.empty()) |
| 1497 | { |
| 1498 | MERROR("Rings for the same key image are disjoint"); |
| 1499 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1500 | } |
| 1501 | else if (common.size() == 1) |
| 1502 | { |
| 1503 | const std::pair<uint64_t, uint64_t> output = std::make_pair(txin.amount, common[0]); |
| 1504 | if (opt_verbose) |
| 1505 | { |
| 1506 | MINFO("Marking output " << output.first << "/" << output.second << " as spent, due to being used in rings with a single common element"); |
| 1507 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1508 | } |
| 1509 | blackballs.push_back(output); |
| 1510 | if (add_spent_output(cur, output_data(txin.amount, common[0]))) |
| 1511 | inc_stat(txn, txin.amount ? "pre-rct-key-image-attack" : "rct-key-image-attack"); |
| 1512 | } |
| 1513 | else |
| 1514 | { |
| 1515 | MDEBUG("The intersection has more than one element, it's still ok"); |
| 1516 | std::cout << "\r" << start_idx << "/" << n_txes << " \r" << std::flush; |
| 1517 | for (const auto &out: r0) |
| 1518 | if (std::find(common.begin(), common.end(), out) != common.end()) |
| 1519 | new_ring.push_back(out); |
| 1520 | new_ring = cryptonote::absolute_output_offsets_to_relative(new_ring); |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | if (n == 0) |
| 1525 | { |
| 1526 | set_relative_ring(txn, txin.k_image, new_ring); |
| 1527 | if (!opt_rct_only) |
| 1528 | inc_per_amount_outputs(txn, txin.amount, 0, 1); |
| 1529 | } |
| 1530 | } |
| 1531 | set_processed_txidx(txn, canonical, start_idx+1); |
| 1532 | if (!opt_rct_only) |
| 1533 | { |
| 1534 | for (const auto &out: tx.vout) |
| 1535 | { |
| 1536 | uint64_t amount = out.amount; |
| 1537 | if (miner_tx && tx.version >= 2) |
| 1538 | amount = 0; |
| 1539 | |
| 1540 | if (opt_rct_only && amount != 0) |
| 1541 | continue; |
| 1542 | if (out.target.type() != typeid(txout_to_key)) |
| 1543 | continue; |
| 1544 | inc_per_amount_outputs(txn, amount, 1, 0); |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | ++records; |
| 1549 | if (records >= records_per_sync) |
| 1550 | { |
| 1551 | if (!blackballs.empty()) |
| 1552 | { |
| 1553 | ringdb.blackball(blackballs); |
| 1554 | blackballs.clear(); |
| 1555 | } |
| 1556 | mdb_cursor_close(cur); |
| 1557 | dbr = mdb_txn_commit(txn); |
| 1558 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr))); |
| 1559 | int dbr = resize_env(cache_dir.c_str()); |
| 1560 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to resize LMDB database: " + std::string(mdb_strerror(dbr))); |
| 1561 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 1562 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1563 | dbr = mdb_cursor_open(txn, dbi_spent, &cur); |
| 1564 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 1565 | records = 0; |
| 1566 | } |
| 1567 | |
| 1568 | if (stop_requested) |
| 1569 | { |
| 1570 | MINFO("Stopping scan..."); |
| 1571 | return false; |
| 1572 | } |
| 1573 | return true; |
| 1574 | }); |
| 1575 | mdb_cursor_close(cur); |
| 1576 | dbr = mdb_txn_commit(txn); |
| 1577 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr))); |
| 1578 | LOG_PRINT_L0("blockchain from " << inputs[n] << " processed till tx idx " << start_idx); |
| 1579 | if (stop_requested) |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | if (stop_requested) |
| 1584 | goto skip_secondary_passes; |
| 1585 | |
| 1586 | if (opt_force_chain_reaction_pass || get_num_spent_outputs() > start_blackballed_outputs) |
| 1587 | { |
| 1588 | MDB_txn *txn; |
| 1589 | dbr = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
| 1590 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1591 | work_spent = get_spent_outputs(txn); |
| 1592 | mdb_txn_abort(txn); |
| 1593 | } |
| 1594 | |
| 1595 | while (!work_spent.empty()) |
| 1596 | { |
| 1597 | LOG_PRINT_L0("Secondary pass on " << work_spent.size() << " spent outputs"); |
| 1598 | |
| 1599 | int dbr = resize_env(cache_dir.c_str()); |
| 1600 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to resize LMDB database: " + std::string(mdb_strerror(dbr))); |
| 1601 | |
| 1602 | MDB_txn *txn; |
| 1603 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 1604 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1605 | MDB_cursor *cur; |
| 1606 | dbr = mdb_cursor_open(txn, dbi_spent, &cur); |
| 1607 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 1608 | |
| 1609 | std::vector<std::pair<uint64_t, uint64_t>> blackballs; |
| 1610 | std::vector<output_data> scan_spent = std::move(work_spent); |
| 1611 | work_spent.clear(); |
| 1612 | for (const output_data &od: scan_spent) |
| 1613 | { |
| 1614 | std::vector<crypto::key_image> key_images = get_key_images(txn, od); |
| 1615 | for (const crypto::key_image &ki: key_images) |
| 1616 | { |
| 1617 | std::vector<uint64_t> relative_ring; |
| 1618 | CHECK_AND_ASSERT_THROW_MES(get_relative_ring(txn, ki, relative_ring), "Relative ring not found"); |
| 1619 | std::vector<uint64_t> absolute = cryptonote::relative_output_offsets_to_absolute(relative_ring); |
| 1620 | size_t known = 0; |
| 1621 | uint64_t last_unknown = 0; |
| 1622 | for (uint64_t out: absolute) |
| 1623 | { |
| 1624 | output_data new_od(od.amount, out); |
| 1625 | if (is_output_spent(cur, new_od)) |
| 1626 | ++known; |
| 1627 | else |
| 1628 | last_unknown = out; |
| 1629 | } |
| 1630 | if (known == absolute.size() - 1) |
| 1631 | { |
| 1632 | const std::pair<uint64_t, uint64_t> output = std::make_pair(od.amount, last_unknown); |
| 1633 | if (opt_verbose) |
| 1634 | { |
| 1635 | MINFO("Marking output " << output.first << "/" << output.second << " as spent, due to being used in a " << |
| 1636 | absolute.size() << "-ring where all other outputs are known to be spent"); |
| 1637 | } |
| 1638 | blackballs.push_back(output); |
| 1639 | if (add_spent_output(cur, output_data(od.amount, last_unknown))) |
| 1640 | inc_stat(txn, od.amount ? "pre-rct-chain-reaction" : "rct-chain-reaction"); |
| 1641 | work_spent.push_back(output_data(od.amount, last_unknown)); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | if (stop_requested) |
| 1646 | { |
| 1647 | MINFO("Stopping secondary passes. Secondary passes are not incremental, they will re-run fully."); |
| 1648 | return 0; |
| 1649 | } |
| 1650 | } |
| 1651 | if (!blackballs.empty()) |
| 1652 | { |
| 1653 | ringdb.blackball(blackballs); |
| 1654 | blackballs.clear(); |
| 1655 | } |
| 1656 | mdb_cursor_close(cur); |
| 1657 | dbr = mdb_txn_commit(txn); |
| 1658 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr))); |
| 1659 | } |
| 1660 | |
| 1661 | skip_secondary_passes: |
| 1662 | uint64_t diff = get_num_spent_outputs() - start_blackballed_outputs; |
| 1663 | LOG_PRINT_L0(std::to_string(diff) << " new outputs marked as spent, " << get_num_spent_outputs() << " total outputs marked as spent"); |
| 1664 | |
| 1665 | MDB_txn *txn; |
| 1666 | dbr = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
| 1667 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1668 | uint64_t pre_rct = 0, rct = 0; |
| 1669 | get_num_outputs(txn0, cur0, dbi0, pre_rct, rct); |
| 1670 | MINFO("Total pre-rct outputs: " << pre_rct); |
| 1671 | MINFO("Total rct outputs: " << rct); |
| 1672 | static const struct { const char *key; uint64_t base; } stat_keys[] = { |
| 1673 | { "pre-rct-ring-size-1", pre_rct }, { "rct-ring-size-1", rct }, |
| 1674 | { "pre-rct-duplicate-rings", pre_rct }, { "rct-duplicate-rings", rct }, |
| 1675 | { "pre-rct-subset-rings", pre_rct }, { "rct-subset-rings", rct }, |
| 1676 | { "pre-rct-full-count", pre_rct }, { "rct-full-count", rct }, |
| 1677 | { "pre-rct-key-image-attack", pre_rct }, { "rct-key-image-attack", rct }, |
| 1678 | { "pre-rct-extra", pre_rct }, { "rct-ring-extra", rct }, |
| 1679 | { "pre-rct-chain-reaction", pre_rct }, { "rct-chain-reaction", rct }, |
| 1680 | }; |
| 1681 | for (const auto &key: stat_keys) |
| 1682 | { |
| 1683 | uint64_t data; |
| 1684 | if (!get_stat(txn, key.key, data)) |
| 1685 | data = 0; |
| 1686 | float percent = key.base ? 100.0f * data / key.base : 0.0f; |
| 1687 | MINFO(key.key << ": " << data << " (" << percent << "%)"); |
| 1688 | } |
| 1689 | mdb_txn_abort(txn); |
| 1690 | |
| 1691 | if (!opt_export.empty()) |
| 1692 | { |
| 1693 | MDB_txn *txn; |
| 1694 | int dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 1695 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 1696 | MDB_cursor *cur; |
| 1697 | dbr = mdb_cursor_open(txn, dbi_spent, &cur); |
| 1698 | CHECK_AND_ASSERT_THROW_MES(!dbr, "Failed to open LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 1699 | export_spent_outputs(cur, opt_export); |
| 1700 | mdb_cursor_close(cur); |
| 1701 | mdb_txn_abort(txn); |
| 1702 | } |
| 1703 | |
| 1704 | LOG_PRINT_L0("Blockchain spent output data exported OK"); |
| 1705 | close_db(env0, txn0, cur0, dbi0); |
| 1706 | close(); |
| 1707 | return 0; |
| 1708 | |
| 1709 | CATCH_ENTRY("Error", 1); |
| 1710 | } |