| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <lmdb.h> |
| 31 | #include <boost/algorithm/string.hpp> |
| 32 | #include <boost/range/adaptor/transformed.hpp> |
| 33 | #include <boost/filesystem.hpp> |
| 34 | #include "common/util.h" |
| 35 | #include "misc_log_ex.h" |
| 36 | #include "misc_language.h" |
| 37 | #include "wallet_errors.h" |
| 38 | #include "ringdb.h" |
| 39 | #include "cryptonote_config.h" |
| 40 | |
| 41 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 42 | #define MONERO_DEFAULT_LOG_CATEGORY "wallet.ringdb" |
| 43 | |
| 44 | #define V1TAG ((uint64_t)798237759845202) |
| 45 | |
| 46 | static int compare_hash32(const MDB_val *a, const MDB_val *b) |
| 47 | { |
| 48 | uint32_t *va = (uint32_t*) a->mv_data; |
| 49 | uint32_t *vb = (uint32_t*) b->mv_data; |
| 50 | for (int n = 7; n >= 0; n--) |
| 51 | { |
| 52 | if (va[n] == vb[n]) |
| 53 | continue; |
| 54 | return va[n] < vb[n] ? -1 : 1; |
| 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int compare_uint64(const MDB_val *a, const MDB_val *b) |
| 61 | { |
| 62 | const uint64_t va = *(const uint64_t*) a->mv_data; |
| 63 | const uint64_t vb = *(const uint64_t*) b->mv_data; |
| 64 | return va < vb ? -1 : va > vb; |
| 65 | } |
| 66 | |
| 67 | static std::string compress_ring(const std::vector<uint64_t> &ring, uint64_t tag) |
| 68 | { |
| 69 | std::string s; |
| 70 | s += tools::get_varint_data(tag); |
| 71 | for (uint64_t out: ring) |
| 72 | s += tools::get_varint_data(out); |
| 73 | return s; |
| 74 | } |
| 75 | |
| 76 | static std::vector<uint64_t> decompress_ring(const std::string &s, uint64_t tag) |
| 77 | { |
| 78 | std::vector<uint64_t> ring; |
| 79 | int read = 0; |
| 80 | for (std::string::const_iterator i = s.begin(); i != s.cend(); std::advance(i, read)) |
| 81 | { |
| 82 | uint64_t out; |
| 83 | std::string tmp(i, s.cend()); |
| 84 | read = tools::read_varint(tmp.begin(), tmp.end(), out); |
| 85 | THROW_WALLET_EXCEPTION_IF(read <= 0 || read > 256, tools::error::wallet_internal_error, "Internal error decompressing ring"); |
| 86 | if (tag) |
| 87 | { |
| 88 | if (tag != out) |
| 89 | return {}; |
| 90 | tag = 0; |
| 91 | continue; |
| 92 | } |
| 93 | ring.push_back(out); |
| 94 | } |
| 95 | return ring; |
| 96 | } |
| 97 | |
| 98 | std::string get_rings_filename(boost::filesystem::path filename) |
| 99 | { |
| 100 | if (!boost::filesystem::is_directory(filename)) |
| 101 | filename.remove_filename(); |
| 102 | return filename.string(); |
| 103 | } |
| 104 | |
| 105 | static crypto::chacha_iv make_iv(const crypto::key_image &key_image, const crypto::chacha_key &key, uint8_t field) |
| 106 | { |
| 107 | uint8_t buffer[sizeof(key_image) + sizeof(key) + sizeof(config::HASH_KEY_RINGDB) + sizeof(field)]; |
| 108 | memcpy(buffer, &key_image, sizeof(key_image)); |
| 109 | memcpy(buffer + sizeof(key_image), &key, sizeof(key)); |
| 110 | memcpy(buffer + sizeof(key_image) + sizeof(key), config::HASH_KEY_RINGDB, sizeof(config::HASH_KEY_RINGDB)); |
| 111 | memcpy(buffer + sizeof(key_image) + sizeof(key) + sizeof(config::HASH_KEY_RINGDB), &field, sizeof(field)); |
| 112 | crypto::hash hash; |
| 113 | // if field is 0, backward compat mode: hash without the field |
| 114 | crypto::cn_fast_hash(buffer, sizeof(buffer) - !field, hash.data); |
| 115 | static_assert(sizeof(hash) >= CHACHA_IV_SIZE, "Incompatible hash and chacha IV sizes"); |
| 116 | crypto::chacha_iv iv; |
| 117 | memcpy(&iv, &hash, CHACHA_IV_SIZE); |
| 118 | return iv; |
| 119 | } |
| 120 | |
| 121 | static std::string encrypt(const std::string &plaintext, const crypto::key_image &key_image, const crypto::chacha_key &key, uint8_t field) |
| 122 | { |
| 123 | const crypto::chacha_iv iv = make_iv(key_image, key, field); |
| 124 | std::string ciphertext; |
| 125 | ciphertext.resize(plaintext.size() + sizeof(iv)); |
| 126 | crypto::chacha20(plaintext.data(), plaintext.size(), key, iv, &ciphertext[sizeof(iv)]); |
| 127 | memcpy(&ciphertext[0], &iv, sizeof(iv)); |
| 128 | return ciphertext; |
| 129 | } |
| 130 | |
| 131 | static std::string encrypt(const crypto::key_image &key_image, const crypto::chacha_key &key, uint8_t field) |
| 132 | { |
| 133 | return encrypt(std::string((const char*)&key_image, sizeof(key_image)), key_image, key, field); |
| 134 | } |
| 135 | |
| 136 | static std::string decrypt(const std::string &ciphertext, const crypto::key_image &key_image, const crypto::chacha_key &key, uint8_t field) |
| 137 | { |
| 138 | const crypto::chacha_iv iv = make_iv(key_image, key, field); |
| 139 | std::string plaintext; |
| 140 | THROW_WALLET_EXCEPTION_IF(ciphertext.size() < sizeof(iv), tools::error::wallet_internal_error, "Bad ciphertext text"); |
| 141 | plaintext.resize(ciphertext.size() - sizeof(iv)); |
| 142 | crypto::chacha20(ciphertext.data() + sizeof(iv), ciphertext.size() - sizeof(iv), key, iv, &plaintext[0]); |
| 143 | return plaintext; |
| 144 | } |
| 145 | |
| 146 | static void store_relative_ring(MDB_txn *txn, MDB_dbi &dbi, const crypto::key_image &key_image, const std::vector<uint64_t> &relative_ring, const crypto::chacha_key &chacha_key) |
| 147 | { |
| 148 | MDB_val key, data; |
| 149 | std::string key_ciphertext = encrypt(key_image, chacha_key, 0); |
| 150 | key.mv_data = (void*)key_ciphertext.data(); |
| 151 | key.mv_size = key_ciphertext.size(); |
| 152 | std::string compressed_ring = compress_ring(relative_ring, V1TAG); |
| 153 | std::string data_ciphertext = encrypt(compressed_ring, key_image, chacha_key, 1); |
| 154 | data.mv_size = data_ciphertext.size(); |
| 155 | data.mv_data = (void*)data_ciphertext.c_str(); |
| 156 | int dbr = mdb_put(txn, dbi, &key, &data, 0); |
| 157 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set ring for key image in LMDB table: " + std::string(mdb_strerror(dbr))); |
| 158 | } |
| 159 | |
| 160 | static int resize_env(MDB_env *env, const char *db_path, size_t needed) |
| 161 | { |
| 162 | MDB_envinfo mei; |
| 163 | MDB_stat mst; |
| 164 | int ret; |
| 165 | |
| 166 | needed = std::max(needed, (size_t)(100ul * 1024 * 1024)); // at least 100 MB |
| 167 | |
| 168 | ret = mdb_env_info(env, &mei); |
| 169 | if (ret) |
| 170 | return ret; |
| 171 | ret = mdb_env_stat(env, &mst); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | uint64_t size_used = mst.ms_psize * mei.me_last_pgno; |
| 175 | uint64_t mapsize = mei.me_mapsize; |
| 176 | if (size_used + needed > mei.me_mapsize) |
| 177 | { |
| 178 | try |
| 179 | { |
| 180 | boost::filesystem::path path(db_path); |
| 181 | boost::filesystem::space_info si = boost::filesystem::space(path); |
| 182 | if(si.available < needed) |
| 183 | { |
| 184 | MERROR("!! WARNING: Insufficient free space to extend database !!: " << (si.available >> 20L) << " MB available"); |
| 185 | return ENOSPC; |
| 186 | } |
| 187 | } |
| 188 | catch(...) |
| 189 | { |
| 190 | // print something but proceed. |
| 191 | MWARNING("Unable to query free disk space."); |
| 192 | } |
| 193 | |
| 194 | mapsize += needed; |
| 195 | } |
| 196 | return mdb_env_set_mapsize(env, mapsize); |
| 197 | } |
| 198 | |
| 199 | static size_t get_ring_data_size(size_t n_entries) |
| 200 | { |
| 201 | return n_entries * (32 + 1024); // highball 1kB for the ring data to make sure |
| 202 | } |
| 203 | |
| 204 | enum { BLACKBALL_BLACKBALL, BLACKBALL_UNBLACKBALL, BLACKBALL_QUERY, BLACKBALL_CLEAR}; |
| 205 | |
| 206 | namespace tools |
| 207 | { |
| 208 | |
| 209 | ringdb::ringdb(std::string filename, const std::string &genesis): |
| 210 | filename(filename), |
| 211 | env(NULL) |
| 212 | { |
| 213 | MDB_txn *txn; |
| 214 | bool tx_active = false; |
| 215 | int dbr; |
| 216 | |
| 217 | tools::create_directories_if_necessary(filename); |
| 218 | |
| 219 | dbr = mdb_env_create(&env); |
| 220 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LDMB environment: " + std::string(mdb_strerror(dbr))); |
| 221 | dbr = mdb_env_set_maxdbs(env, 2); |
| 222 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set max env dbs: " + std::string(mdb_strerror(dbr))); |
| 223 | const std::string actual_filename = get_rings_filename(filename); |
| 224 | dbr = mdb_env_open(env, actual_filename.c_str(), 0, 0664); |
| 225 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open rings database file '" |
| 226 | + actual_filename + "': " + std::string(mdb_strerror(dbr))); |
| 227 | |
| 228 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 229 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 230 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 231 | tx_active = true; |
| 232 | |
| 233 | dbr = mdb_dbi_open(txn, ("rings-" + genesis).c_str(), MDB_CREATE, &dbi_rings); |
| 234 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 235 | mdb_set_compare(txn, dbi_rings, compare_hash32); |
| 236 | |
| 237 | dbr = mdb_dbi_open(txn, ("blackballs2-" + genesis).c_str(), MDB_CREATE | MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, &dbi_blackballs); |
| 238 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 239 | mdb_set_dupsort(txn, dbi_blackballs, compare_uint64); |
| 240 | |
| 241 | dbr = mdb_txn_commit(txn); |
| 242 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr))); |
| 243 | tx_active = false; |
| 244 | } |
| 245 | |
| 246 | ringdb::~ringdb() |
| 247 | { |
| 248 | close(); |
| 249 | } |
| 250 | |
| 251 | void ringdb::close() |
| 252 | { |
| 253 | if (env) |
| 254 | { |
| 255 | mdb_dbi_close(env, dbi_rings); |
| 256 | mdb_dbi_close(env, dbi_blackballs); |
| 257 | mdb_env_close(env); |
| 258 | env = NULL; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | bool ringdb::add_rings(const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx) |
| 263 | { |
| 264 | MDB_txn *txn; |
| 265 | int dbr; |
| 266 | bool tx_active = false; |
| 267 | |
| 268 | dbr = resize_env(env, filename.c_str(), get_ring_data_size(tx.vin.size())); |
| 269 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size"); |
| 270 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 271 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 272 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 273 | tx_active = true; |
| 274 | |
| 275 | for (const auto &in: tx.vin) |
| 276 | { |
| 277 | if (in.type() != typeid(cryptonote::txin_to_key)) |
| 278 | continue; |
| 279 | const auto &txin = boost::get<cryptonote::txin_to_key>(in); |
| 280 | const uint32_t ring_size = txin.key_offsets.size(); |
| 281 | if (ring_size == 1) |
| 282 | continue; |
| 283 | |
| 284 | store_relative_ring(txn, dbi_rings, txin.k_image, txin.key_offsets, chacha_key); |
| 285 | } |
| 286 | |
| 287 | dbr = mdb_txn_commit(txn); |
| 288 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn adding ring to database: " + std::string(mdb_strerror(dbr))); |
| 289 | tx_active = false; |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | bool ringdb::remove_rings(const crypto::chacha_key &chacha_key, const std::vector<crypto::key_image> &key_images) |
| 294 | { |
| 295 | MDB_txn *txn; |
| 296 | int dbr; |
| 297 | bool tx_active = false; |
| 298 | |
| 299 | dbr = resize_env(env, filename.c_str(), 0); |
| 300 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size"); |
| 301 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 302 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 303 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 304 | tx_active = true; |
| 305 | |
| 306 | for (const crypto::key_image &key_image: key_images) |
| 307 | { |
| 308 | MDB_val key, data; |
| 309 | std::string key_ciphertext = encrypt(key_image, chacha_key, 0); |
| 310 | key.mv_data = (void*)key_ciphertext.data(); |
| 311 | key.mv_size = key_ciphertext.size(); |
| 312 | |
| 313 | dbr = mdb_get(txn, dbi_rings, &key, &data); |
| 314 | THROW_WALLET_EXCEPTION_IF(dbr && dbr != MDB_NOTFOUND, tools::error::wallet_internal_error, "Failed to look for key image in LMDB table: " + std::string(mdb_strerror(dbr))); |
| 315 | if (dbr == MDB_NOTFOUND) |
| 316 | continue; |
| 317 | THROW_WALLET_EXCEPTION_IF(data.mv_size <= 0, tools::error::wallet_internal_error, "Invalid ring data size"); |
| 318 | |
| 319 | MDEBUG("Removing ring data for key image " << key_image); |
| 320 | dbr = mdb_del(txn, dbi_rings, &key, NULL); |
| 321 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to remove ring to database: " + std::string(mdb_strerror(dbr))); |
| 322 | } |
| 323 | |
| 324 | dbr = mdb_txn_commit(txn); |
| 325 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn removing ring to database: " + std::string(mdb_strerror(dbr))); |
| 326 | tx_active = false; |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | bool ringdb::remove_rings(const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx) |
| 331 | { |
| 332 | std::vector<crypto::key_image> key_images; |
| 333 | key_images.reserve(tx.vin.size()); |
| 334 | for (const auto &in: tx.vin) |
| 335 | { |
| 336 | if (in.type() != typeid(cryptonote::txin_to_key)) |
| 337 | continue; |
| 338 | const auto &txin = boost::get<cryptonote::txin_to_key>(in); |
| 339 | const uint32_t ring_size = txin.key_offsets.size(); |
| 340 | if (ring_size == 1) |
| 341 | continue; |
| 342 | key_images.push_back(txin.k_image); |
| 343 | } |
| 344 | return remove_rings(chacha_key, key_images); |
| 345 | } |
| 346 | |
| 347 | bool ringdb::get_rings(const crypto::chacha_key &chacha_key, const std::vector<crypto::key_image> &key_images, std::vector<std::vector<uint64_t>> &all_outs) |
| 348 | { |
| 349 | MDB_txn *txn; |
| 350 | int dbr; |
| 351 | bool tx_active = false; |
| 352 | |
| 353 | all_outs.clear(); |
| 354 | all_outs.reserve(key_images.size()); |
| 355 | |
| 356 | dbr = resize_env(env, filename.c_str(), 0); |
| 357 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size: " + std::string(mdb_strerror(dbr))); |
| 358 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 359 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 360 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 361 | tx_active = true; |
| 362 | |
| 363 | for (size_t i = 0; i < key_images.size(); ++i) |
| 364 | { |
| 365 | const crypto::key_image &key_image = key_images[i]; |
| 366 | |
| 367 | MDB_val key, data; |
| 368 | std::string key_ciphertext = encrypt(key_image, chacha_key, 0); |
| 369 | key.mv_data = (void*)key_ciphertext.data(); |
| 370 | key.mv_size = key_ciphertext.size(); |
| 371 | dbr = mdb_get(txn, dbi_rings, &key, &data); |
| 372 | THROW_WALLET_EXCEPTION_IF(dbr && dbr != MDB_NOTFOUND, tools::error::wallet_internal_error, "Failed to look for key image in LMDB table: " + std::string(mdb_strerror(dbr))); |
| 373 | if (dbr == MDB_NOTFOUND) |
| 374 | return false; |
| 375 | THROW_WALLET_EXCEPTION_IF(data.mv_size <= 0, tools::error::wallet_internal_error, "Invalid ring data size"); |
| 376 | |
| 377 | std::vector<uint64_t> outs; |
| 378 | bool try_v0 = false; |
| 379 | std::string data_plaintext = decrypt(std::string((const char*)data.mv_data, data.mv_size), key_image, chacha_key, 1); |
| 380 | try { outs = decompress_ring(data_plaintext, V1TAG); if (outs.empty()) try_v0 = true; } |
| 381 | catch(...) { try_v0 = true; } |
| 382 | if (try_v0) |
| 383 | { |
| 384 | data_plaintext = decrypt(std::string((const char*)data.mv_data, data.mv_size), key_image, chacha_key, 0); |
| 385 | outs = decompress_ring(data_plaintext, 0); |
| 386 | } |
| 387 | MDEBUG("Found ring for key image " << key_image << ":"); |
| 388 | MDEBUG("Relative: " << boost::join(outs | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " ")); |
| 389 | outs = cryptonote::relative_output_offsets_to_absolute(outs); |
| 390 | MDEBUG("Absolute: " << boost::join(outs | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " ")); |
| 391 | all_outs.push_back(std::move(outs)); |
| 392 | |
| 393 | } |
| 394 | |
| 395 | dbr = mdb_txn_commit(txn); |
| 396 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn getting ring from database: " + std::string(mdb_strerror(dbr))); |
| 397 | tx_active = false; |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool ringdb::get_ring(const crypto::chacha_key &chacha_key, const crypto::key_image &key_image, std::vector<uint64_t> &outs) |
| 402 | { |
| 403 | std::vector<std::vector<uint64_t>> all_outs; |
| 404 | if (!get_rings(chacha_key, std::vector<crypto::key_image>(1, key_image), all_outs)) |
| 405 | return false; |
| 406 | outs = std::move(all_outs.front()); |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | bool ringdb::set_rings(const crypto::chacha_key &chacha_key, const std::vector<std::pair<crypto::key_image, std::vector<uint64_t>>> &rings, bool relative) |
| 411 | { |
| 412 | MDB_txn *txn; |
| 413 | int dbr; |
| 414 | bool tx_active = false; |
| 415 | |
| 416 | size_t n_outs = 0; |
| 417 | for (const auto &e: rings) |
| 418 | n_outs += e.second.size(); |
| 419 | dbr = resize_env(env, filename.c_str(), n_outs * 64); |
| 420 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size: " + std::string(mdb_strerror(dbr))); |
| 421 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 422 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 423 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 424 | tx_active = true; |
| 425 | |
| 426 | for (const auto &e: rings) |
| 427 | store_relative_ring(txn, dbi_rings, e.first, relative ? e.second : cryptonote::absolute_output_offsets_to_relative(e.second), chacha_key); |
| 428 | |
| 429 | dbr = mdb_txn_commit(txn); |
| 430 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn setting ring to database: " + std::string(mdb_strerror(dbr))); |
| 431 | tx_active = false; |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | bool ringdb::set_ring(const crypto::chacha_key &chacha_key, const crypto::key_image &key_image, const std::vector<uint64_t> &outs, bool relative) |
| 436 | { |
| 437 | std::vector<std::pair<crypto::key_image, std::vector<uint64_t>>> rings; |
| 438 | rings.push_back(std::make_pair(key_image, outs)); |
| 439 | return set_rings(chacha_key, rings, relative); |
| 440 | } |
| 441 | |
| 442 | bool ringdb::blackball_worker(const std::vector<std::pair<uint64_t, uint64_t>> &outputs, int op) |
| 443 | { |
| 444 | MDB_txn *txn; |
| 445 | MDB_cursor *cursor; |
| 446 | int dbr; |
| 447 | bool tx_active = false; |
| 448 | bool ret = true; |
| 449 | |
| 450 | THROW_WALLET_EXCEPTION_IF(outputs.size() > 1 && op == BLACKBALL_QUERY, tools::error::wallet_internal_error, "Blackball query only makes sense for a single output"); |
| 451 | |
| 452 | dbr = resize_env(env, filename.c_str(), 32 * 2 * outputs.size()); // a pubkey, and some slack |
| 453 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size: " + std::string(mdb_strerror(dbr))); |
| 454 | dbr = mdb_txn_begin(env, NULL, 0, &txn); |
| 455 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 456 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);}); |
| 457 | tx_active = true; |
| 458 | |
| 459 | dbr = mdb_cursor_open(txn, dbi_blackballs, &cursor); |
| 460 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create cursor for blackballs table: " + std::string(mdb_strerror(dbr))); |
| 461 | |
| 462 | MDB_val key, data; |
| 463 | for (const std::pair<uint64_t, uint64_t> &output: outputs) |
| 464 | { |
| 465 | key.mv_data = (void*)&output.first; |
| 466 | key.mv_size = sizeof(output.first); |
| 467 | data.mv_data = (void*)&output.second; |
| 468 | data.mv_size = sizeof(output.second); |
| 469 | |
| 470 | switch (op) |
| 471 | { |
| 472 | case BLACKBALL_BLACKBALL: |
| 473 | MDEBUG("Marking output " << output.first << "/" << output.second << " as spent"); |
| 474 | dbr = mdb_cursor_put(cursor, &key, &data, MDB_NODUPDATA); |
| 475 | if (dbr == MDB_KEYEXIST) |
| 476 | dbr = 0; |
| 477 | break; |
| 478 | case BLACKBALL_UNBLACKBALL: |
| 479 | MDEBUG("Marking output " << output.first << "/" << output.second << " as unspent"); |
| 480 | dbr = mdb_cursor_get(cursor, &key, &data, MDB_GET_BOTH); |
| 481 | if (dbr == 0) |
| 482 | dbr = mdb_cursor_del(cursor, 0); |
| 483 | break; |
| 484 | case BLACKBALL_QUERY: |
| 485 | dbr = mdb_cursor_get(cursor, &key, &data, MDB_GET_BOTH); |
| 486 | THROW_WALLET_EXCEPTION_IF(dbr && dbr != MDB_NOTFOUND, tools::error::wallet_internal_error, "Failed to lookup in blackballs table: " + std::string(mdb_strerror(dbr))); |
| 487 | ret = dbr != MDB_NOTFOUND; |
| 488 | if (dbr == MDB_NOTFOUND) |
| 489 | dbr = 0; |
| 490 | break; |
| 491 | case BLACKBALL_CLEAR: |
| 492 | break; |
| 493 | default: |
| 494 | THROW_WALLET_EXCEPTION(tools::error::wallet_internal_error, "Invalid blackball op"); |
| 495 | } |
| 496 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to query blackballs table: " + std::string(mdb_strerror(dbr))); |
| 497 | } |
| 498 | |
| 499 | mdb_cursor_close(cursor); |
| 500 | |
| 501 | if (op == BLACKBALL_CLEAR) |
| 502 | { |
| 503 | dbr = mdb_drop(txn, dbi_blackballs, 0); |
| 504 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to clear blackballs table: " + std::string(mdb_strerror(dbr))); |
| 505 | } |
| 506 | |
| 507 | dbr = mdb_txn_commit(txn); |
| 508 | THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn blackballing output to database: " + std::string(mdb_strerror(dbr))); |
| 509 | tx_active = false; |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | bool ringdb::blackball(const std::vector<std::pair<uint64_t, uint64_t>> &outputs) |
| 514 | { |
| 515 | return blackball_worker(outputs, BLACKBALL_BLACKBALL); |
| 516 | } |
| 517 | |
| 518 | bool ringdb::blackball(const std::pair<uint64_t, uint64_t> &output) |
| 519 | { |
| 520 | std::vector<std::pair<uint64_t, uint64_t>> outputs(1, output); |
| 521 | return blackball_worker(outputs, BLACKBALL_BLACKBALL); |
| 522 | } |
| 523 | |
| 524 | bool ringdb::unblackball(const std::pair<uint64_t, uint64_t> &output) |
| 525 | { |
| 526 | std::vector<std::pair<uint64_t, uint64_t>> outputs(1, output); |
| 527 | return blackball_worker(outputs, BLACKBALL_UNBLACKBALL); |
| 528 | } |
| 529 | |
| 530 | bool ringdb::blackballed(const std::pair<uint64_t, uint64_t> &output) |
| 531 | { |
| 532 | std::vector<std::pair<uint64_t, uint64_t>> outputs(1, output); |
| 533 | return blackball_worker(outputs, BLACKBALL_QUERY); |
| 534 | } |
| 535 | |
| 536 | bool ringdb::clear_blackballs() |
| 537 | { |
| 538 | return blackball_worker(std::vector<std::pair<uint64_t, uint64_t>>(), BLACKBALL_CLEAR); |
| 539 | } |
| 540 | |
| 541 | } |