| 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 <array> |
| 31 | #include <lmdb.h> |
| 32 | #include <boost/algorithm/string.hpp> |
| 33 | #include <boost/system/error_code.hpp> |
| 34 | #include <boost/filesystem.hpp> |
| 35 | #include "common/command_line.h" |
| 36 | #include "common/pruning.h" |
| 37 | #include "cryptonote_core/cryptonote_core.h" |
| 38 | #include "cryptonote_core/blockchain.h" |
| 39 | #include "blockchain_db/blockchain_db.h" |
| 40 | #include "blockchain_db/lmdb/db_lmdb.h" |
| 41 | #include "version.h" |
| 42 | |
| 43 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 44 | #define MONERO_DEFAULT_LOG_CATEGORY "bcutil" |
| 45 | |
| 46 | #define MDB_val_set(var, val) MDB_val var = {sizeof(val), (void *)&val} |
| 47 | |
| 48 | namespace po = boost::program_options; |
| 49 | using namespace epee; |
| 50 | using namespace cryptonote; |
| 51 | |
| 52 | static std::string db_path; |
| 53 | |
| 54 | // default to fast:1 |
| 55 | static uint64_t records_per_sync = 128; |
| 56 | static const size_t slack = 512 * 1024 * 1024; |
| 57 | |
| 58 | static std::error_code replace_file(const boost::filesystem::path& replacement_name, const boost::filesystem::path& replaced_name) |
| 59 | { |
| 60 | std::error_code ec = tools::replace_file(replacement_name.string(), replaced_name.string()); |
| 61 | if (ec) |
| 62 | MERROR("Error renaming " << replacement_name << " to " << replaced_name << ": " << ec.message()); |
| 63 | return ec; |
| 64 | } |
| 65 | |
| 66 | static void open(MDB_env *&env, const boost::filesystem::path &path, uint64_t db_flags, bool readonly) |
| 67 | { |
| 68 | int dbr; |
| 69 | int flags = 0; |
| 70 | |
| 71 | if (db_flags & DBF_FAST) |
| 72 | flags |= MDB_NOSYNC; |
| 73 | if (db_flags & DBF_FASTEST) |
| 74 | flags |= MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC; |
| 75 | if (readonly) |
| 76 | flags |= MDB_RDONLY; |
| 77 | |
| 78 | dbr = mdb_env_create(&env); |
| 79 | if (dbr) throw std::runtime_error("Failed to create LDMB environment: " + std::string(mdb_strerror(dbr))); |
| 80 | dbr = mdb_env_set_maxdbs(env, 32); |
| 81 | if (dbr) throw std::runtime_error("Failed to set max env dbs: " + std::string(mdb_strerror(dbr))); |
| 82 | dbr = mdb_env_open(env, path.string().c_str(), flags, 0664); |
| 83 | if (dbr) throw std::runtime_error("Failed to open database file '" |
| 84 | + path.string() + "': " + std::string(mdb_strerror(dbr))); |
| 85 | } |
| 86 | |
| 87 | static void close(MDB_env *env) |
| 88 | { |
| 89 | mdb_env_close(env); |
| 90 | } |
| 91 | |
| 92 | static void add_size(MDB_env *env, uint64_t bytes) |
| 93 | { |
| 94 | try |
| 95 | { |
| 96 | boost::filesystem::path path(db_path); |
| 97 | boost::filesystem::space_info si = boost::filesystem::space(path); |
| 98 | if(si.available < bytes) |
| 99 | { |
| 100 | MERROR("!! WARNING: Insufficient free space to extend database !!: " << |
| 101 | (si.available >> 20L) << " MB available, " << (bytes >> 20L) << " MB needed"); |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | catch(...) |
| 106 | { |
| 107 | // print something but proceed. |
| 108 | MWARNING("Unable to query free disk space."); |
| 109 | } |
| 110 | |
| 111 | MDB_envinfo mei; |
| 112 | mdb_env_info(env, &mei); |
| 113 | MDB_stat mst; |
| 114 | mdb_env_stat(env, &mst); |
| 115 | |
| 116 | uint64_t new_mapsize = (uint64_t)mei.me_mapsize + bytes; |
| 117 | new_mapsize += (new_mapsize % mst.ms_psize); |
| 118 | |
| 119 | int result = mdb_env_set_mapsize(env, new_mapsize); |
| 120 | if (result) |
| 121 | throw std::runtime_error("Failed to set new mapsize to " + std::to_string(new_mapsize) + ": " + std::string(mdb_strerror(result))); |
| 122 | |
| 123 | MGINFO("LMDB Mapsize increased." << " Old: " << mei.me_mapsize / (1024 * 1024) << "MiB" << ", New: " << new_mapsize / (1024 * 1024) << "MiB"); |
| 124 | } |
| 125 | |
| 126 | static void check_resize(MDB_env *env, size_t bytes) |
| 127 | { |
| 128 | MDB_envinfo mei; |
| 129 | MDB_stat mst; |
| 130 | |
| 131 | mdb_env_info(env, &mei); |
| 132 | mdb_env_stat(env, &mst); |
| 133 | |
| 134 | uint64_t size_used = mst.ms_psize * mei.me_last_pgno; |
| 135 | if (size_used + bytes + slack >= mei.me_mapsize) |
| 136 | add_size(env, size_used + bytes + 2 * slack - mei.me_mapsize); |
| 137 | } |
| 138 | |
| 139 | static bool resize_point(size_t nrecords, MDB_env *env, MDB_txn **txn, size_t &bytes) |
| 140 | { |
| 141 | if (nrecords % records_per_sync && bytes <= slack / 2) |
| 142 | return false; |
| 143 | int dbr = mdb_txn_commit(*txn); |
| 144 | if (dbr) throw std::runtime_error("Failed to commit txn: " + std::string(mdb_strerror(dbr))); |
| 145 | check_resize(env, bytes); |
| 146 | dbr = mdb_txn_begin(env, NULL, 0, txn); |
| 147 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 148 | bytes = 0; |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | static void copy_table(MDB_env *env0, MDB_env *env1, const char *table, unsigned int flags, unsigned int putflags, int (*cmp)(const MDB_val*, const MDB_val*)=0) |
| 153 | { |
| 154 | MDB_dbi dbi0, dbi1; |
| 155 | MDB_txn *txn0, *txn1; |
| 156 | MDB_cursor *cur0, *cur1; |
| 157 | bool tx_active0 = false, tx_active1 = false; |
| 158 | int dbr; |
| 159 | |
| 160 | MINFO("Copying " << table); |
| 161 | |
| 162 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){ |
| 163 | if (tx_active1) mdb_txn_abort(txn1); |
| 164 | if (tx_active0) mdb_txn_abort(txn0); |
| 165 | }); |
| 166 | |
| 167 | dbr = mdb_txn_begin(env0, NULL, MDB_RDONLY, &txn0); |
| 168 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 169 | tx_active0 = true; |
| 170 | dbr = mdb_txn_begin(env1, NULL, 0, &txn1); |
| 171 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 172 | tx_active1 = true; |
| 173 | |
| 174 | dbr = mdb_dbi_open(txn0, table, flags, &dbi0); |
| 175 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 176 | if (cmp) |
| 177 | ((flags & MDB_DUPSORT) ? mdb_set_dupsort : mdb_set_compare)(txn0, dbi0, cmp); |
| 178 | |
| 179 | dbr = mdb_dbi_open(txn1, table, flags, &dbi1); |
| 180 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 181 | if (cmp) |
| 182 | ((flags & MDB_DUPSORT) ? mdb_set_dupsort : mdb_set_compare)(txn1, dbi1, cmp); |
| 183 | |
| 184 | dbr = mdb_txn_commit(txn1); |
| 185 | if (dbr) throw std::runtime_error("Failed to commit txn: " + std::string(mdb_strerror(dbr))); |
| 186 | tx_active1 = false; |
| 187 | MDB_stat stats; |
| 188 | dbr = mdb_env_stat(env0, &stats); |
| 189 | if (dbr) throw std::runtime_error("Failed to stat " + std::string(table) + " LMDB table: " + std::string(mdb_strerror(dbr))); |
| 190 | check_resize(env1, (stats.ms_branch_pages + stats.ms_overflow_pages + stats.ms_leaf_pages) * stats.ms_psize); |
| 191 | dbr = mdb_txn_begin(env1, NULL, 0, &txn1); |
| 192 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 193 | tx_active1 = true; |
| 194 | |
| 195 | dbr = mdb_drop(txn1, dbi1, 0); |
| 196 | if (dbr) throw std::runtime_error("Failed to empty " + std::string(table) + " LMDB table: " + std::string(mdb_strerror(dbr))); |
| 197 | |
| 198 | dbr = mdb_cursor_open(txn0, dbi0, &cur0); |
| 199 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 200 | dbr = mdb_cursor_open(txn1, dbi1, &cur1); |
| 201 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 202 | |
| 203 | MDB_val k; |
| 204 | MDB_val v; |
| 205 | MDB_cursor_op op = MDB_FIRST; |
| 206 | size_t nrecords = 0, bytes = 0; |
| 207 | while (1) |
| 208 | { |
| 209 | int ret = mdb_cursor_get(cur0, &k, &v, op); |
| 210 | op = MDB_NEXT; |
| 211 | if (ret == MDB_NOTFOUND) |
| 212 | break; |
| 213 | if (ret) |
| 214 | throw std::runtime_error("Failed to enumerate " + std::string(table) + " records: " + std::string(mdb_strerror(ret))); |
| 215 | |
| 216 | bytes += k.mv_size + v.mv_size; |
| 217 | if (resize_point(++nrecords, env1, &txn1, bytes)) |
| 218 | { |
| 219 | dbr = mdb_cursor_open(txn1, dbi1, &cur1); |
| 220 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 221 | } |
| 222 | |
| 223 | ret = mdb_cursor_put(cur1, &k, &v, putflags); |
| 224 | if (ret) |
| 225 | throw std::runtime_error("Failed to write " + std::string(table) + " record: " + std::string(mdb_strerror(ret))); |
| 226 | } |
| 227 | |
| 228 | mdb_cursor_close(cur1); |
| 229 | mdb_cursor_close(cur0); |
| 230 | mdb_txn_commit(txn1); |
| 231 | tx_active1 = false; |
| 232 | mdb_txn_commit(txn0); |
| 233 | tx_active0 = false; |
| 234 | mdb_dbi_close(env1, dbi1); |
| 235 | mdb_dbi_close(env0, dbi0); |
| 236 | } |
| 237 | |
| 238 | static bool is_v1_tx(MDB_cursor *c_txs_pruned, MDB_val *tx_id) |
| 239 | { |
| 240 | MDB_val v; |
| 241 | int ret = mdb_cursor_get(c_txs_pruned, tx_id, &v, MDB_SET); |
| 242 | if (ret) |
| 243 | throw std::runtime_error("Failed to find transaction pruned data: " + std::string(mdb_strerror(ret))); |
| 244 | if (v.mv_size == 0) |
| 245 | throw std::runtime_error("Invalid transaction pruned data"); |
| 246 | return cryptonote::is_v1_tx(cryptonote::blobdata_ref{(const char*)v.mv_data, v.mv_size}); |
| 247 | } |
| 248 | |
| 249 | static void prune(MDB_env *env0, MDB_env *env1) |
| 250 | { |
| 251 | MDB_dbi dbi0_blocks, dbi0_txs_pruned, dbi0_txs_prunable, dbi0_tx_indices, dbi1_txs_prunable, dbi1_txs_prunable_tip, dbi1_properties; |
| 252 | MDB_txn *txn0, *txn1; |
| 253 | MDB_cursor *cur0_txs_pruned, *cur0_txs_prunable, *cur0_tx_indices, *cur1_txs_prunable, *cur1_txs_prunable_tip; |
| 254 | bool tx_active0 = false, tx_active1 = false; |
| 255 | int dbr; |
| 256 | |
| 257 | MGINFO("Creating pruned txs_prunable"); |
| 258 | |
| 259 | epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){ |
| 260 | if (tx_active1) mdb_txn_abort(txn1); |
| 261 | if (tx_active0) mdb_txn_abort(txn0); |
| 262 | }); |
| 263 | |
| 264 | dbr = mdb_txn_begin(env0, NULL, MDB_RDONLY, &txn0); |
| 265 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 266 | tx_active0 = true; |
| 267 | dbr = mdb_txn_begin(env1, NULL, 0, &txn1); |
| 268 | if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); |
| 269 | tx_active1 = true; |
| 270 | |
| 271 | dbr = mdb_dbi_open(txn0, "txs_pruned", MDB_INTEGERKEY, &dbi0_txs_pruned); |
| 272 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 273 | mdb_set_compare(txn0, dbi0_txs_pruned, BlockchainLMDB::compare_uint64); |
| 274 | dbr = mdb_cursor_open(txn0, dbi0_txs_pruned, &cur0_txs_pruned); |
| 275 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 276 | |
| 277 | dbr = mdb_dbi_open(txn0, "txs_prunable", MDB_INTEGERKEY, &dbi0_txs_prunable); |
| 278 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 279 | mdb_set_compare(txn0, dbi0_txs_prunable, BlockchainLMDB::compare_uint64); |
| 280 | dbr = mdb_cursor_open(txn0, dbi0_txs_prunable, &cur0_txs_prunable); |
| 281 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 282 | |
| 283 | dbr = mdb_dbi_open(txn0, "tx_indices", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, &dbi0_tx_indices); |
| 284 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 285 | mdb_set_dupsort(txn0, dbi0_tx_indices, BlockchainLMDB::compare_hash32); |
| 286 | dbr = mdb_cursor_open(txn0, dbi0_tx_indices, &cur0_tx_indices); |
| 287 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 288 | |
| 289 | dbr = mdb_dbi_open(txn1, "txs_prunable", MDB_INTEGERKEY, &dbi1_txs_prunable); |
| 290 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 291 | mdb_set_compare(txn1, dbi1_txs_prunable, BlockchainLMDB::compare_uint64); |
| 292 | dbr = mdb_cursor_open(txn1, dbi1_txs_prunable, &cur1_txs_prunable); |
| 293 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 294 | |
| 295 | dbr = mdb_dbi_open(txn1, "txs_prunable_tip", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, &dbi1_txs_prunable_tip); |
| 296 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 297 | mdb_set_dupsort(txn1, dbi1_txs_prunable_tip, BlockchainLMDB::compare_uint64); |
| 298 | dbr = mdb_cursor_open(txn1, dbi1_txs_prunable_tip, &cur1_txs_prunable_tip); |
| 299 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 300 | |
| 301 | dbr = mdb_drop(txn1, dbi1_txs_prunable, 0); |
| 302 | if (dbr) throw std::runtime_error("Failed to empty LMDB table: " + std::string(mdb_strerror(dbr))); |
| 303 | dbr = mdb_drop(txn1, dbi1_txs_prunable_tip, 0); |
| 304 | if (dbr) throw std::runtime_error("Failed to empty LMDB table: " + std::string(mdb_strerror(dbr))); |
| 305 | |
| 306 | dbr = mdb_dbi_open(txn1, "properties", 0, &dbi1_properties); |
| 307 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 308 | |
| 309 | MDB_val k, v; |
| 310 | uint32_t pruning_seed = tools::make_pruning_seed(tools::get_random_stripe(), CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 311 | static char pruning_seed_key[] = "pruning_seed"; |
| 312 | k.mv_data = pruning_seed_key; |
| 313 | k.mv_size = strlen("pruning_seed") + 1; |
| 314 | v.mv_data = (void*)&pruning_seed; |
| 315 | v.mv_size = sizeof(pruning_seed); |
| 316 | dbr = mdb_put(txn1, dbi1_properties, &k, &v, 0); |
| 317 | if (dbr) throw std::runtime_error("Failed to save pruning seed: " + std::string(mdb_strerror(dbr))); |
| 318 | |
| 319 | MDB_stat stats; |
| 320 | dbr = mdb_dbi_open(txn0, "blocks", 0, &dbi0_blocks); |
| 321 | if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr))); |
| 322 | dbr = mdb_stat(txn0, dbi0_blocks, &stats); |
| 323 | if (dbr) throw std::runtime_error("Failed to query size of blocks: " + std::string(mdb_strerror(dbr))); |
| 324 | mdb_dbi_close(env0, dbi0_blocks); |
| 325 | const uint64_t blockchain_height = stats.ms_entries; |
| 326 | size_t nrecords = 0, bytes = 0; |
| 327 | |
| 328 | MDB_cursor_op op = MDB_FIRST; |
| 329 | while (1) |
| 330 | { |
| 331 | int ret = mdb_cursor_get(cur0_tx_indices, &k, &v, op); |
| 332 | op = MDB_NEXT; |
| 333 | if (ret == MDB_NOTFOUND) |
| 334 | break; |
| 335 | if (ret) throw std::runtime_error("Failed to enumerate records: " + std::string(mdb_strerror(ret))); |
| 336 | |
| 337 | const txindex *ti = (const txindex*)v.mv_data; |
| 338 | const uint64_t block_height = ti->data.block_id; |
| 339 | MDB_val_set(kk, ti->data.tx_id); |
| 340 | if (block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height) |
| 341 | { |
| 342 | MDEBUG(block_height << "/" << blockchain_height << " is in tip"); |
| 343 | MDB_val_set(vv, block_height); |
| 344 | dbr = mdb_cursor_put(cur1_txs_prunable_tip, &kk, &vv, 0); |
| 345 | if (dbr) throw std::runtime_error("Failed to write prunable tx tip data: " + std::string(mdb_strerror(dbr))); |
| 346 | bytes += kk.mv_size + vv.mv_size; |
| 347 | } |
| 348 | if (tools::has_unpruned_block(block_height, blockchain_height, pruning_seed) || is_v1_tx(cur0_txs_pruned, &kk)) |
| 349 | { |
| 350 | MDB_val vv; |
| 351 | dbr = mdb_cursor_get(cur0_txs_prunable, &kk, &vv, MDB_SET); |
| 352 | if (dbr) throw std::runtime_error("Failed to read prunable tx data: " + std::string(mdb_strerror(dbr))); |
| 353 | bytes += kk.mv_size + vv.mv_size; |
| 354 | if (resize_point(++nrecords, env1, &txn1, bytes)) |
| 355 | { |
| 356 | dbr = mdb_cursor_open(txn1, dbi1_txs_prunable, &cur1_txs_prunable); |
| 357 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 358 | dbr = mdb_cursor_open(txn1, dbi1_txs_prunable_tip, &cur1_txs_prunable_tip); |
| 359 | if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr))); |
| 360 | } |
| 361 | dbr = mdb_cursor_put(cur1_txs_prunable, &kk, &vv, 0); |
| 362 | if (dbr) throw std::runtime_error("Failed to write prunable tx data: " + std::string(mdb_strerror(dbr))); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | MDEBUG("" << block_height << "/" << blockchain_height << " should be pruned, dropping"); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | mdb_cursor_close(cur1_txs_prunable_tip); |
| 371 | mdb_cursor_close(cur1_txs_prunable); |
| 372 | mdb_cursor_close(cur0_txs_prunable); |
| 373 | mdb_cursor_close(cur0_txs_pruned); |
| 374 | mdb_cursor_close(cur0_tx_indices); |
| 375 | mdb_txn_commit(txn1); |
| 376 | tx_active1 = false; |
| 377 | mdb_txn_commit(txn0); |
| 378 | tx_active0 = false; |
| 379 | mdb_dbi_close(env1, dbi1_properties); |
| 380 | mdb_dbi_close(env1, dbi1_txs_prunable_tip); |
| 381 | mdb_dbi_close(env1, dbi1_txs_prunable); |
| 382 | mdb_dbi_close(env0, dbi0_txs_prunable); |
| 383 | mdb_dbi_close(env0, dbi0_txs_pruned); |
| 384 | mdb_dbi_close(env0, dbi0_tx_indices); |
| 385 | } |
| 386 | |
| 387 | static bool parse_db_sync_mode(std::string db_sync_mode, uint64_t &db_flags) |
| 388 | { |
| 389 | std::vector<std::string> options; |
| 390 | boost::trim(db_sync_mode); |
| 391 | boost::split(options, db_sync_mode, boost::is_any_of(" :")); |
| 392 | |
| 393 | for(const auto &option : options) |
| 394 | MDEBUG("option: " << option); |
| 395 | |
| 396 | // default to fast:async:1 |
| 397 | uint64_t DEFAULT_FLAGS = DBF_FAST; |
| 398 | |
| 399 | db_flags = 0; |
| 400 | |
| 401 | if(options.size() == 0) |
| 402 | { |
| 403 | // default to fast:async:1 |
| 404 | db_flags = DEFAULT_FLAGS; |
| 405 | } |
| 406 | |
| 407 | bool safemode = false; |
| 408 | if(options.size() >= 1) |
| 409 | { |
| 410 | if(options[0] == "safe") |
| 411 | { |
| 412 | safemode = true; |
| 413 | db_flags = DBF_SAFE; |
| 414 | } |
| 415 | else if(options[0] == "fast") |
| 416 | { |
| 417 | db_flags = DBF_FAST; |
| 418 | } |
| 419 | else if(options[0] == "fastest") |
| 420 | { |
| 421 | db_flags = DBF_FASTEST; |
| 422 | records_per_sync = 1000; // default to fastest:async:1000 |
| 423 | } |
| 424 | else |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | if(options.size() >= 2 && !safemode) |
| 429 | { |
| 430 | char *endptr; |
| 431 | uint64_t bps = strtoull(options[1].c_str(), &endptr, 0); |
| 432 | if (*endptr != '\0') |
| 433 | return false; |
| 434 | records_per_sync = bps; |
| 435 | } |
| 436 | |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | int main(int argc, char* argv[]) |
| 441 | { |
| 442 | TRY_ENTRY(); |
| 443 | |
| 444 | epee::string_tools::set_module_name_and_folder(argv[0]); |
| 445 | |
| 446 | uint32_t log_level = 0; |
| 447 | |
| 448 | tools::on_startup(); |
| 449 | |
| 450 | boost::filesystem::path output_file_path; |
| 451 | |
| 452 | po::options_description desc_cmd_only("Command line options"); |
| 453 | po::options_description desc_cmd_sett("Command line options and settings options"); |
| 454 | const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""}; |
| 455 | const command_line::arg_descriptor<std::string> arg_db_sync_mode = { |
| 456 | "db-sync-mode" |
| 457 | , "Specify sync option, using format [safe|fast|fastest]:[nrecords_per_sync]." |
| 458 | , "fast:1000" |
| 459 | }; |
| 460 | const command_line::arg_descriptor<bool> arg_copy_pruned_database = {"copy-pruned-database", "Copy database anyway if already pruned"}; |
| 461 | |
| 462 | command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir); |
| 463 | command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); |
| 464 | command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); |
| 465 | command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); |
| 466 | command_line::add_arg(desc_cmd_sett, arg_log_level); |
| 467 | command_line::add_arg(desc_cmd_sett, arg_db_sync_mode); |
| 468 | command_line::add_arg(desc_cmd_sett, arg_copy_pruned_database); |
| 469 | command_line::add_arg(desc_cmd_only, command_line::arg_help); |
| 470 | |
| 471 | po::options_description desc_options("Allowed options"); |
| 472 | desc_options.add(desc_cmd_only).add(desc_cmd_sett); |
| 473 | |
| 474 | po::variables_map vm; |
| 475 | bool r = command_line::handle_error_helper(desc_options, [&]() |
| 476 | { |
| 477 | auto parser = po::command_line_parser(argc, argv).options(desc_options); |
| 478 | po::store(parser.run(), vm); |
| 479 | po::notify(vm); |
| 480 | return true; |
| 481 | }); |
| 482 | if (! r) |
| 483 | return 1; |
| 484 | |
| 485 | if (command_line::get_arg(vm, command_line::arg_help)) |
| 486 | { |
| 487 | std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL; |
| 488 | std::cout << desc_options << std::endl; |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | mlog_configure(mlog_get_default_log_path("monero-blockchain-prune.log"), true); |
| 493 | if (!command_line::is_arg_defaulted(vm, arg_log_level)) |
| 494 | mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str()); |
| 495 | else |
| 496 | mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str()); |
| 497 | |
| 498 | MINFO("Starting..."); |
| 499 | |
| 500 | const network_type net_type = core::get_network_type_from_args(vm); |
| 501 | bool opt_copy_pruned_database = command_line::get_arg(vm, arg_copy_pruned_database); |
| 502 | std::string data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir); |
| 503 | while (boost::ends_with(data_dir, "/") || boost::ends_with(data_dir, "\\")) |
| 504 | data_dir.pop_back(); |
| 505 | |
| 506 | std::string db_sync_mode = command_line::get_arg(vm, arg_db_sync_mode); |
| 507 | uint64_t db_flags = 0; |
| 508 | if (!parse_db_sync_mode(db_sync_mode, db_flags)) |
| 509 | { |
| 510 | MERROR("Invalid db sync mode: " << db_sync_mode); |
| 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | // If we wanted to use the memory pool, we would set up a fake_core. |
| 515 | |
| 516 | // Use Blockchain instead of lower-level BlockchainDB for two reasons: |
| 517 | // 1. Blockchain has the init() method for easy setup |
| 518 | // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash() |
| 519 | // |
| 520 | // cannot match blockchain_storage setup above with just one line, |
| 521 | // e.g. |
| 522 | // Blockchain* core_storage = new Blockchain(NULL); |
| 523 | // because unlike blockchain_storage constructor, which takes a pointer to |
| 524 | // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object. |
| 525 | MINFO("Initializing source blockchain (BlockchainDB)"); |
| 526 | std::array<std::unique_ptr<Blockchain>, 2> core_storage; |
| 527 | Blockchain *blockchain = NULL; |
| 528 | tx_memory_pool m_mempool(*blockchain); |
| 529 | boost::filesystem::path paths[2]; |
| 530 | bool already_pruned = false; |
| 531 | for (size_t n = 0; n < core_storage.size(); ++n) |
| 532 | { |
| 533 | core_storage[n].reset(new Blockchain(m_mempool)); |
| 534 | |
| 535 | BlockchainDB* db = new_db(); |
| 536 | if (db == NULL) |
| 537 | { |
| 538 | MERROR("Failed to initialize a database"); |
| 539 | throw std::runtime_error("Failed to initialize a database"); |
| 540 | } |
| 541 | |
| 542 | if (n == 1) |
| 543 | { |
| 544 | paths[1] = boost::filesystem::path(data_dir) / (db->get_db_name() + "-pruned"); |
| 545 | if (boost::filesystem::exists(paths[1])) |
| 546 | { |
| 547 | if (!boost::filesystem::is_directory(paths[1])) |
| 548 | { |
| 549 | MERROR("LMDB needs a directory path, but a file was passed: " << paths[1].string()); |
| 550 | return 1; |
| 551 | } |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | if (!boost::filesystem::create_directories(paths[1])) |
| 556 | { |
| 557 | MERROR("Failed to create directory: " << paths[1].string()); |
| 558 | return 1; |
| 559 | } |
| 560 | } |
| 561 | db_path = paths[1].string(); |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | paths[0] = boost::filesystem::path(data_dir) / db->get_db_name(); |
| 566 | } |
| 567 | |
| 568 | MINFO("Loading blockchain from folder " << paths[n] << " ..."); |
| 569 | |
| 570 | try |
| 571 | { |
| 572 | db->open(paths[n].string(), n == 0 ? DBF_RDONLY : 0); |
| 573 | } |
| 574 | catch (const std::exception& e) |
| 575 | { |
| 576 | MERROR("Error opening database: " << e.what()); |
| 577 | return 1; |
| 578 | } |
| 579 | r = core_storage[n]->init(db, net_type); |
| 580 | |
| 581 | std::string source_dest = n == 0 ? "source" : "pruned"; |
| 582 | CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize " << source_dest << " blockchain storage"); |
| 583 | MINFO(source_dest << " blockchain storage initialized OK"); |
| 584 | if (n == 0 && core_storage[0]->get_blockchain_pruning_seed()) |
| 585 | { |
| 586 | if (!opt_copy_pruned_database) |
| 587 | { |
| 588 | MERROR("Blockchain is already pruned, use --" << arg_copy_pruned_database.name << " to copy it anyway"); |
| 589 | return 1; |
| 590 | } |
| 591 | already_pruned = true; |
| 592 | } |
| 593 | } |
| 594 | core_storage[0]->deinit(); |
| 595 | core_storage[0].reset(NULL); |
| 596 | core_storage[1]->deinit(); |
| 597 | core_storage[1].reset(NULL); |
| 598 | |
| 599 | MINFO("Pruning..."); |
| 600 | MDB_env *env0 = NULL, *env1 = NULL; |
| 601 | open(env0, paths[0], db_flags, true); |
| 602 | open(env1, paths[1], db_flags, false); |
| 603 | copy_table(env0, env1, "blocks", MDB_INTEGERKEY, MDB_APPEND); |
| 604 | copy_table(env0, env1, "block_info", MDB_INTEGERKEY | MDB_DUPSORT| MDB_DUPFIXED, MDB_APPENDDUP, BlockchainLMDB::compare_uint64); |
| 605 | copy_table(env0, env1, "block_heights", MDB_INTEGERKEY | MDB_DUPSORT| MDB_DUPFIXED, 0, BlockchainLMDB::compare_hash32); |
| 606 | //copy_table(env0, env1, "txs", MDB_INTEGERKEY); |
| 607 | copy_table(env0, env1, "txs_pruned", MDB_INTEGERKEY, MDB_APPEND); |
| 608 | copy_table(env0, env1, "txs_prunable_hash", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, MDB_APPEND); |
| 609 | // not copied: prunable, prunable_tip |
| 610 | copy_table(env0, env1, "tx_indices", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, 0, BlockchainLMDB::compare_hash32); |
| 611 | copy_table(env0, env1, "tx_outputs", MDB_INTEGERKEY, MDB_APPEND); |
| 612 | copy_table(env0, env1, "output_txs", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, MDB_APPENDDUP, BlockchainLMDB::compare_uint64); |
| 613 | copy_table(env0, env1, "output_amounts", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, MDB_APPENDDUP, BlockchainLMDB::compare_uint64); |
| 614 | copy_table(env0, env1, "spent_keys", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, MDB_NODUPDATA, BlockchainLMDB::compare_hash32); |
| 615 | copy_table(env0, env1, "txpool_meta", 0, MDB_NODUPDATA, BlockchainLMDB::compare_hash32); |
| 616 | copy_table(env0, env1, "txpool_blob", 0, MDB_NODUPDATA, BlockchainLMDB::compare_hash32); |
| 617 | copy_table(env0, env1, "hf_versions", MDB_INTEGERKEY, MDB_APPEND); |
| 618 | copy_table(env0, env1, "properties", 0, 0, BlockchainLMDB::compare_string); |
| 619 | if (already_pruned) |
| 620 | { |
| 621 | copy_table(env0, env1, "txs_prunable", MDB_INTEGERKEY, MDB_APPEND, BlockchainLMDB::compare_uint64); |
| 622 | copy_table(env0, env1, "txs_prunable_tip", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, MDB_NODUPDATA, BlockchainLMDB::compare_uint64); |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | prune(env0, env1); |
| 627 | } |
| 628 | close(env1); |
| 629 | close(env0); |
| 630 | |
| 631 | MINFO("Swapping databases, pre-pruning blockchain will be left in " << paths[0].string() + "-old and can be removed if desired"); |
| 632 | if (replace_file(paths[0].string(), paths[0].string() + "-old") || replace_file(paths[1].string(), paths[0].string())) |
| 633 | { |
| 634 | MERROR("Blockchain pruned OK, but renaming failed"); |
| 635 | return 1; |
| 636 | } |
| 637 | |
| 638 | MINFO("Blockchain pruned OK"); |
| 639 | return 0; |
| 640 | |
| 641 | CATCH_ENTRY("Pruning error", 1); |
| 642 | } |