Debian Codegraph @

monero/src/blockchain_utilities/blockchain_export.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "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, arg_log_level);
72 command_line::add_arg(desc_cmd_sett, arg_block_start);
73 command_line::add_arg(desc_cmd_sett, arg_block_stop);
74 command_line::add_arg(desc_cmd_sett, arg_blocks_dat);
75
76 command_line::add_arg(desc_cmd_only, command_line::arg_help);
77
78 po::options_description desc_options("Allowed options");
79 desc_options.add(desc_cmd_only).add(desc_cmd_sett);
80
81 po::variables_map vm;
82 bool r = command_line::handle_error_helper(desc_options, [&]()
83 {
84 po::store(po::parse_command_line(argc, argv, desc_options), vm);
85 po::notify(vm);
86 return true;
87 });
88 if (! r)
89 return 1;
90
91 if (command_line::get_arg(vm, command_line::arg_help))
92 {
93 std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL;
94 std::cout << desc_options << std::endl;
95 return 1;
96 }
97
98 mlog_configure(mlog_get_default_log_path("monero-blockchain-export.log"), true);
99 if (!command_line::is_arg_defaulted(vm, arg_log_level))
100 mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str());
101 else
102 mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str());
103 block_start = command_line::get_arg(vm, arg_block_start);
104 block_stop = command_line::get_arg(vm, arg_block_stop);
105
106 LOG_PRINT_L0("Starting...");
107
108 bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on);
109 bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on);
110 if (opt_testnet && opt_stagenet)
111 {
112 std::cerr << "Can't specify more than one of --testnet and --stagenet" << std::endl;
113 return 1;
114 }
115 bool opt_blocks_dat = command_line::get_arg(vm, arg_blocks_dat);
116
117 std::string m_config_folder;
118
119 m_config_folder = command_line::get_arg(vm, cryptonote::arg_data_dir);
120
121 if (command_line::has_arg(vm, arg_output_file))
122 output_file_path = boost::filesystem::path(command_line::get_arg(vm, arg_output_file));
123 else
124 output_file_path = boost::filesystem::path(m_config_folder) / "export" / BLOCKCHAIN_RAW;
125 LOG_PRINT_L0("Export output file: " << output_file_path.string());
126
127 // If we wanted to use the memory pool, we would set up a fake_core.
128
129 // Use Blockchain instead of lower-level BlockchainDB for two reasons:
130 // 1. Blockchain has the init() method for easy setup
131 // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash()
132 //
133 // cannot match blockchain_storage setup above with just one line,
134 // e.g.
135 // Blockchain* core_storage = new Blockchain(NULL);
136 // because unlike blockchain_storage constructor, which takes a pointer to
137 // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object.
138 LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
139 Blockchain* core_storage = NULL;
140 tx_memory_pool m_mempool(*core_storage);
141 core_storage = new Blockchain(m_mempool);
142
143 BlockchainDB* db = new_db();
144 if (db == NULL)
145 {
146 LOG_ERROR("Failed to initialize a database");
147 throw std::runtime_error("Failed to initialize a database");
148 }
149 LOG_PRINT_L0("database: LMDB");
150
151 boost::filesystem::path folder(m_config_folder);
152 folder /= db->get_db_name();
153 const std::string filename = folder.string();
154
155 LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
156 try
157 {
158 db->open(filename, DBF_RDONLY);
159 }
160 catch (const std::exception& e)
161 {
162 LOG_PRINT_L0("Error opening database: " << e.what());
163 return 1;
164 }
165 r = core_storage->init(db, opt_testnet ? cryptonote::TESTNET : opt_stagenet ? cryptonote::STAGENET : cryptonote::MAINNET);
166
167 if (core_storage->get_blockchain_pruning_seed() && !opt_blocks_dat)
168 {
169 LOG_PRINT_L0("Blockchain is pruned, cannot export");
170 return 1;
171 }
172
173 CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage");
174 LOG_PRINT_L0("Source blockchain storage initialized OK");
175 LOG_PRINT_L0("Exporting blockchain raw data...");
176
177 if (opt_blocks_dat)
178 {
179 BlocksdatFile blocksdat;
180 r = blocksdat.store_blockchain_raw(core_storage, NULL, output_file_path, block_stop);
181 }
182 else
183 {
184 BootstrapFile bootstrap;
185 r = bootstrap.store_blockchain_raw(core_storage, NULL, output_file_path, block_start, block_stop);
186 }
187 CHECK_AND_ASSERT_MES(r, 1, "Failed to export blockchain raw data");
188 LOG_PRINT_L0("Blockchain raw data exported OK");
189 return 0;
190
191 CATCH_ENTRY("Export error", 1);
192 }