Debian Codegraph @

src/blockchain_utilities/blocksdat_file.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 "blocksdat_file.h"
30
31 #undef MONERO_DEFAULT_LOG_CATEGORY
32 #define MONERO_DEFAULT_LOG_CATEGORY "bcutil"
33
34 namespace po = boost::program_options;
35
36 using namespace cryptonote;
37 using namespace epee;
38
39 namespace
40 {
41 std::string refresh_string = "\r \r";
42 }
43
44
45
46 bool BlocksdatFile::open_writer(const boost::filesystem::path& file_path, uint64_t block_stop)
47 {
48 const boost::filesystem::path dir_path = file_path.parent_path();
49 if (!dir_path.empty())
50 {
51 if (boost::filesystem::exists(dir_path))
52 {
53 if (!boost::filesystem::is_directory(dir_path))
54 {
55 MFATAL("export directory path is a file: " << dir_path);
56 return false;
57 }
58 }
59 else
60 {
61 if (!boost::filesystem::create_directory(dir_path))
62 {
63 MFATAL("Failed to create directory " << dir_path);
64 return false;
65 }
66 }
67 }
68
69 m_raw_data_file = new std::ofstream();
70
71 MINFO("creating file");
72
73 m_raw_data_file->open(file_path.string(), std::ios_base::binary | std::ios_base::out | std::ios::trunc);
74 if (m_raw_data_file->fail())
75 return false;
76
77 initialize_file(block_stop);
78
79 return true;
80 }
81
82
83 bool BlocksdatFile::initialize_file(uint64_t block_stop)
84 {
85 const uint32_t nblocks = (block_stop + 1) / HASH_OF_HASHES_STEP;
86 unsigned char nblocksc[4];
87
88 nblocksc[0] = nblocks & 0xff;
89 nblocksc[1] = (nblocks >> 8) & 0xff;
90 nblocksc[2] = (nblocks >> 16) & 0xff;
91 nblocksc[3] = (nblocks >> 24) & 0xff;
92
93 // 4 bytes little endian
94 *m_raw_data_file << nblocksc[0];
95 *m_raw_data_file << nblocksc[1];
96 *m_raw_data_file << nblocksc[2];
97 *m_raw_data_file << nblocksc[3];
98
99 return true;
100 }
101
102 void BlocksdatFile::write_block(const crypto::hash& block_hash, uint64_t weight)
103 {
104 m_hashes.push_back(block_hash);
105 m_weights.push_back(weight);
106 while (m_hashes.size() >= HASH_OF_HASHES_STEP)
107 {
108 crypto::hash hash;
109 crypto::cn_fast_hash(m_hashes.data(), HASH_OF_HASHES_STEP * sizeof(crypto::hash), hash);
110 memmove(m_hashes.data(), m_hashes.data() + HASH_OF_HASHES_STEP, (m_hashes.size() - HASH_OF_HASHES_STEP) * sizeof(crypto::hash));
111 m_hashes.resize(m_hashes.size() - HASH_OF_HASHES_STEP);
112 const std::string data_hashes(hash.data, sizeof(hash));
113 *m_raw_data_file << data_hashes;
114 crypto::cn_fast_hash(m_weights.data(), HASH_OF_HASHES_STEP * sizeof(uint64_t), hash);
115 memmove(m_weights.data(), m_weights.data() + HASH_OF_HASHES_STEP, (m_weights.size() - HASH_OF_HASHES_STEP) * sizeof(uint64_t));
116 m_weights.resize(m_weights.size() - HASH_OF_HASHES_STEP);
117 const std::string data_weights(hash.data, sizeof(hash));
118 *m_raw_data_file << data_weights;
119 }
120 }
121
122 bool BlocksdatFile::close()
123 {
124 if (m_raw_data_file->fail())
125 return false;
126
127 m_raw_data_file->flush();
128 delete m_raw_data_file;
129 return true;
130 }
131
132
133 bool BlocksdatFile::store_blockchain_raw(Blockchain* _blockchain_storage, tx_memory_pool* _tx_pool, boost::filesystem::path& output_file, uint64_t requested_block_stop)
134 {
135 uint64_t num_blocks_written = 0;
136 m_blockchain_storage = _blockchain_storage;
137 uint64_t progress_interval = 100;
138 block b;
139
140 uint64_t block_start = 0;
141 uint64_t block_stop = 0;
142 MINFO("source blockchain height: " << m_blockchain_storage->get_current_blockchain_height()-1);
143 if ((requested_block_stop > 0) && (requested_block_stop < m_blockchain_storage->get_current_blockchain_height()))
144 {
145 MINFO("Using requested block height: " << requested_block_stop);
146 block_stop = requested_block_stop;
147 }
148 else
149 {
150 block_stop = m_blockchain_storage->get_current_blockchain_height() - 1;
151 MINFO("Using block height of source blockchain: " << block_stop);
152 }
153 MINFO("Storing blocks raw data...");
154 if (!BlocksdatFile::open_writer(output_file, block_stop))
155 {
156 MFATAL("failed to open raw file for write");
157 return false;
158 }
159 for (m_cur_height = block_start; m_cur_height <= block_stop; ++m_cur_height)
160 {
161 // this method's height refers to 0-based height (genesis block = height 0)
162 crypto::hash hash = m_blockchain_storage->get_block_id_by_height(m_cur_height);
163 uint64_t weight = m_blockchain_storage->get_db().get_block_weight(m_cur_height);
164 write_block(hash, weight);
165 if (m_cur_height % NUM_BLOCKS_PER_CHUNK == 0) {
166 num_blocks_written += NUM_BLOCKS_PER_CHUNK;
167 }
168 if (m_cur_height % progress_interval == 0) {
169 std::cout << refresh_string;
170 std::cout << "block " << m_cur_height << "/" << block_stop << std::flush;
171 }
172 }
173 // print message for last block, which may not have been printed yet due to progress_interval
174 std::cout << refresh_string;
175 std::cout << "block " << m_cur_height-1 << "/" << block_stop << ENDL;
176
177 MINFO("Number of blocks exported: " << num_blocks_written);
178
179 return BlocksdatFile::close();
180 }
181