Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
blocksdat_file.cpp
Go to the documentation of this file.
1
// Copyrights(c) 2017-2021, The Electroneum Project
2
// Copyrights(c) 2014-2019, The Monero Project
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 "
blocksdat_file.h
"
31
32
#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
33
#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "bcutil"
34
35
namespace
po = boost::program_options;
36
37
using namespace
cryptonote
;
38
using namespace
epee
;
39
40
namespace
41
{
42
std::string refresh_string =
"\r \r"
;
43
}
44
45
46
47
bool
BlocksdatFile::open_writer
(
const
boost::filesystem::path& file_path,
uint64_t
block_stop)
48
{
49
const
boost::filesystem::path dir_path = file_path.parent_path();
50
if
(!dir_path.empty())
51
{
52
if
(boost::filesystem::exists(dir_path))
53
{
54
if
(!boost::filesystem::is_directory(dir_path))
55
{
56
MFATAL
(
"export directory path is a file: "
<< dir_path);
57
return
false
;
58
}
59
}
60
else
61
{
62
if
(!boost::filesystem::create_directory(dir_path))
63
{
64
MFATAL
(
"Failed to create directory "
<< dir_path);
65
return
false
;
66
}
67
}
68
}
69
70
m_raw_data_file
=
new
std::ofstream();
71
72
MINFO
(
"creating file"
);
73
74
m_raw_data_file
->open(file_path.string(), std::ios_base::binary | std::ios_base::out | std::ios::trunc);
75
if
(
m_raw_data_file
->fail())
76
return
false
;
77
78
initialize_file
(block_stop);
79
80
return
true
;
81
}
82
83
84
bool
BlocksdatFile::initialize_file
(
uint64_t
block_stop)
85
{
86
const
uint32_t
nblocks = block_stop + 1;
87
unsigned
char
nblocksc[4];
88
89
nblocksc[0] = nblocks & 0xff;
90
nblocksc[1] = (nblocks >> 8) & 0xff;
91
nblocksc[2] = (nblocks >> 16) & 0xff;
92
nblocksc[3] = (nblocks >> 24) & 0xff;
93
94
// 4 bytes little endian
95
*
m_raw_data_file
<< nblocksc[0];
96
*
m_raw_data_file
<< nblocksc[1];
97
*
m_raw_data_file
<< nblocksc[2];
98
*
m_raw_data_file
<< nblocksc[3];
99
100
return
true
;
101
}
102
103
void
BlocksdatFile::write_block
(
const
crypto::hash
& block_hash)
104
{
105
const
std::string data(block_hash.data,
sizeof
(block_hash));
106
*
m_raw_data_file
<< data;
107
}
108
109
bool
BlocksdatFile::close
()
110
{
111
if
(
m_raw_data_file
->fail())
112
return
false
;
113
114
m_raw_data_file
->flush();
115
delete
m_raw_data_file
;
116
return
true
;
117
}
118
119
120
bool
BlocksdatFile::store_blockchain_raw
(
Blockchain
* _blockchain_storage,
tx_memory_pool
* _tx_pool, boost::filesystem::path& output_file,
uint64_t
requested_block_stop)
121
{
122
uint64_t
num_blocks_written = 0;
123
m_blockchain_storage
= _blockchain_storage;
124
uint64_t
progress_interval = 100;
125
block
b;
126
127
uint64_t
block_start = 0;
128
uint64_t
block_stop = 0;
129
MINFO
(
"source blockchain height: "
<<
m_blockchain_storage
->get_current_blockchain_height()-1);
130
if
((requested_block_stop > 0) && (requested_block_stop < m_blockchain_storage->get_current_blockchain_height()))
131
{
132
MINFO
(
"Using requested block height: "
<< requested_block_stop);
133
block_stop = requested_block_stop;
134
}
135
else
136
{
137
block_stop =
m_blockchain_storage
->get_current_blockchain_height() - 1;
138
MINFO
(
"Using block height of source blockchain: "
<< block_stop);
139
}
140
MINFO
(
"Storing blocks raw data..."
);
141
if
(!
BlocksdatFile::open_writer
(output_file, block_stop))
142
{
143
MFATAL
(
"failed to open raw file for write"
);
144
return
false
;
145
}
146
for
(m_cur_height = block_start; m_cur_height <= block_stop; ++m_cur_height)
147
{
148
// this method's height refers to 0-based height (genesis block = height 0)
149
crypto::hash
hash =
m_blockchain_storage
->get_block_id_by_height(m_cur_height);
150
write_block
(hash);
151
if
(m_cur_height %
NUM_BLOCKS_PER_CHUNK
== 0) {
152
num_blocks_written +=
NUM_BLOCKS_PER_CHUNK
;
153
}
154
if
(m_cur_height % progress_interval == 0) {
155
std::cout << refresh_string;
156
std::cout <<
"block "
<< m_cur_height <<
"/"
<< block_stop << std::flush;
157
}
158
}
159
// print message for last block, which may not have been printed yet due to progress_interval
160
std::cout << refresh_string;
161
std::cout <<
"block "
<< m_cur_height-1 <<
"/"
<< block_stop <<
ENDL
;
162
163
MINFO
(
"Number of blocks exported: "
<< num_blocks_written);
164
165
return
BlocksdatFile::close
();
166
}
167
NUM_BLOCKS_PER_CHUNK
#define NUM_BLOCKS_PER_CHUNK
Definition
blockchain_utilities.h:39
blocksdat_file.h
BlocksdatFile::open_writer
bool open_writer(const boost::filesystem::path &file_path, uint64_t block_stop)
Definition
blocksdat_file.cpp:47
BlocksdatFile::initialize_file
bool initialize_file(uint64_t block_stop)
Definition
blocksdat_file.cpp:84
BlocksdatFile::close
bool close()
Definition
blocksdat_file.cpp:109
BlocksdatFile::m_raw_data_file
std::ofstream * m_raw_data_file
Definition
blocksdat_file.h:69
BlocksdatFile::m_blockchain_storage
Blockchain * m_blockchain_storage
Definition
blocksdat_file.h:67
BlocksdatFile::store_blockchain_raw
bool store_blockchain_raw(cryptonote::Blockchain *cs, cryptonote::tx_memory_pool *txp, boost::filesystem::path &output_file, uint64_t use_block_height=0)
Definition
blocksdat_file.cpp:120
BlocksdatFile::write_block
void write_block(const crypto::hash &block_hash)
Definition
blocksdat_file.cpp:103
cryptonote::Blockchain
Definition
blockchain.h:98
cryptonote::tx_memory_pool
Transaction pool, handles transactions which are not part of a block.
Definition
tx_pool.h:95
MFATAL
#define MFATAL(x)
Definition
misc_log_ex.h:72
ENDL
#define ENDL
Definition
misc_log_ex.h:149
MINFO
#define MINFO(x)
Definition
misc_log_ex.h:75
crypto::hash
POD_CLASS hash
Definition
hash.h:50
cryptonote
Holds cryptonote related classes and helpers.
Definition
ban.cpp:40
epee
Definition
ado_db_helper.h:67
uint32_t
unsigned int uint32_t
Definition
stdint.h:126
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:136
cryptonote::block
Definition
cryptonote_basic.h:464
src
blockchain_utilities
blocksdat_file.cpp
Generated on
for Electroneum by
1.17.0