| 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 "bootstrap_file.h" |
| 30 | #include "blocksdat_file.h" |
| 31 | #include "common/command_line.h" |
| 32 | #include "cryptonote_core/tx_pool.h" |
| 33 | #include "cryptonote_core/cryptonote_core.h" |
| 34 | #include "blockchain_db/blockchain_db.h" |
| 35 | #include "version.h" |
| 36 | |
| 37 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 38 | #define MONERO_DEFAULT_LOG_CATEGORY "bcutil" |
| 39 | |
| 40 | namespace po = boost::program_options; |
| 41 | using namespace epee; |
| 42 | |
| 43 | int main(int argc, char* argv[]) |
| 44 | { |
| 45 | TRY_ENTRY(); |
| 46 | |
| 47 | epee::string_tools::set_module_name_and_folder(argv[0]); |
| 48 | |
| 49 | uint32_t log_level = 0; |
| 50 | uint64_t block_start = 0; |
| 51 | uint64_t block_stop = 0; |
| 52 | bool blocks_dat = false; |
| 53 | |
| 54 | tools::on_startup(); |
| 55 | |
| 56 | boost::filesystem::path output_file_path; |
| 57 | |
| 58 | po::options_description desc_cmd_only("Command line options"); |
| 59 | po::options_description desc_cmd_sett("Command line options and settings options"); |
| 60 | const command_line::arg_descriptor<std::string> arg_output_file = {"output-file", "Specify output file", "", true}; |
| 61 | const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""}; |
| 62 | const command_line::arg_descriptor<uint64_t> arg_block_start = {"block-start", "Start at block number", block_start}; |
| 63 | const command_line::arg_descriptor<uint64_t> arg_block_stop = {"block-stop", "Stop at block number", block_stop}; |
| 64 | const command_line::arg_descriptor<bool> arg_blocks_dat = {"blocksdat", "Output in blocks.dat format", blocks_dat}; |
| 65 | |
| 66 | |
| 67 | command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir); |
| 68 | command_line::add_arg(desc_cmd_sett, arg_output_file); |
| 69 | command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); |
| 70 | command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); |
| 71 | command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); |
| 72 | command_line::add_arg(desc_cmd_sett, arg_log_level); |
| 73 | command_line::add_arg(desc_cmd_sett, arg_block_start); |
| 74 | command_line::add_arg(desc_cmd_sett, arg_block_stop); |
| 75 | command_line::add_arg(desc_cmd_sett, arg_blocks_dat); |
| 76 | |
| 77 | command_line::add_arg(desc_cmd_only, command_line::arg_help); |
| 78 | |
| 79 | po::options_description desc_options("Allowed options"); |
| 80 | desc_options.add(desc_cmd_only).add(desc_cmd_sett); |
| 81 | |
| 82 | po::variables_map vm; |
| 83 | bool r = command_line::handle_error_helper(desc_options, [&]() |
| 84 | { |
| 85 | po::store(po::parse_command_line(argc, argv, desc_options), vm); |
| 86 | po::notify(vm); |
| 87 | return true; |
| 88 | }); |
| 89 | if (! r) |
| 90 | return 1; |
| 91 | |
| 92 | if (command_line::get_arg(vm, command_line::arg_help)) |
| 93 | { |
| 94 | std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL; |
| 95 | std::cout << desc_options << std::endl; |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | mlog_configure(mlog_get_default_log_path("monero-blockchain-export.log"), true); |
| 100 | if (!command_line::is_arg_defaulted(vm, arg_log_level)) |
| 101 | mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str()); |
| 102 | else |
| 103 | mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str()); |
| 104 | block_start = command_line::get_arg(vm, arg_block_start); |
| 105 | block_stop = command_line::get_arg(vm, arg_block_stop); |
| 106 | |
| 107 | LOG_PRINT_L0("Starting..."); |
| 108 | |
| 109 | bool opt_blocks_dat = command_line::get_arg(vm, arg_blocks_dat); |
| 110 | |
| 111 | std::string m_config_folder; |
| 112 | |
| 113 | m_config_folder = command_line::get_arg(vm, cryptonote::arg_data_dir); |
| 114 | |
| 115 | if (command_line::has_arg(vm, arg_output_file)) |
| 116 | output_file_path = boost::filesystem::path(command_line::get_arg(vm, arg_output_file)); |
| 117 | else |
| 118 | output_file_path = boost::filesystem::path(m_config_folder) / "export" / BLOCKCHAIN_RAW; |
| 119 | LOG_PRINT_L0("Export output file: " << output_file_path.string()); |
| 120 | |
| 121 | // If we wanted to use the memory pool, we would set up a fake_core. |
| 122 | |
| 123 | // Use Blockchain instead of lower-level BlockchainDB for two reasons: |
| 124 | // 1. Blockchain has the init() method for easy setup |
| 125 | // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash() |
| 126 | // |
| 127 | // cannot match blockchain_storage setup above with just one line, |
| 128 | // e.g. |
| 129 | // Blockchain* core_storage = new Blockchain(NULL); |
| 130 | // because unlike blockchain_storage constructor, which takes a pointer to |
| 131 | // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object. |
| 132 | LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)"); |
| 133 | Blockchain* core_storage = NULL; |
| 134 | tx_memory_pool m_mempool(*core_storage); |
| 135 | core_storage = new Blockchain(m_mempool); |
| 136 | |
| 137 | BlockchainDB* db = new_db(); |
| 138 | if (db == NULL) |
| 139 | { |
| 140 | LOG_ERROR("Failed to initialize a database"); |
| 141 | throw std::runtime_error("Failed to initialize a database"); |
| 142 | } |
| 143 | LOG_PRINT_L0("database: LMDB"); |
| 144 | |
| 145 | boost::filesystem::path folder(m_config_folder); |
| 146 | folder /= db->get_db_name(); |
| 147 | const std::string filename = folder.string(); |
| 148 | |
| 149 | LOG_PRINT_L0("Loading blockchain from folder " << filename << " ..."); |
| 150 | try |
| 151 | { |
| 152 | db->open(filename, DBF_RDONLY); |
| 153 | } |
| 154 | catch (const std::exception& e) |
| 155 | { |
| 156 | LOG_PRINT_L0("Error opening database: " << e.what()); |
| 157 | return 1; |
| 158 | } |
| 159 | const network_type net_type = core::get_network_type_from_args(vm); |
| 160 | r = core_storage->init(db, net_type); |
| 161 | |
| 162 | if (core_storage->get_blockchain_pruning_seed() && !opt_blocks_dat) |
| 163 | { |
| 164 | LOG_PRINT_L0("Blockchain is pruned, cannot export"); |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage"); |
| 169 | LOG_PRINT_L0("Source blockchain storage initialized OK"); |
| 170 | LOG_PRINT_L0("Exporting blockchain raw data..."); |
| 171 | |
| 172 | if (opt_blocks_dat) |
| 173 | { |
| 174 | BlocksdatFile blocksdat; |
| 175 | r = blocksdat.store_blockchain_raw(core_storage, NULL, output_file_path, block_stop); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | BootstrapFile bootstrap; |
| 180 | r = bootstrap.store_blockchain_raw(core_storage, NULL, output_file_path, block_start, block_stop); |
| 181 | } |
| 182 | CHECK_AND_ASSERT_MES(r, 1, "Failed to export blockchain raw data"); |
| 183 | LOG_PRINT_L0("Blockchain raw data exported OK"); |
| 184 | return 0; |
| 185 | |
| 186 | CATCH_ENTRY("Export error", 1); |
| 187 | } |