forked from steem-witnesses/steem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_log.cpp
308 lines (271 loc) · 9.28 KB
/
block_log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <steemit/chain/block_log.hpp>
#include <fstream>
#include <fc/io/raw.hpp>
#define LOG_READ (std::ios::in | std::ios::binary)
#define LOG_WRITE (std::ios::out | std::ios::binary | std::ios::app)
namespace steemit { namespace chain {
namespace detail {
class block_log_impl {
public:
optional< signed_block > head;
block_id_type head_id;
std::fstream block_stream;
std::fstream index_stream;
fc::path block_file;
fc::path index_file;
bool block_write = false;
bool index_write = false;
inline void check_block_read()
{
try
{
if( block_write )
{
block_stream.close();
block_stream.open( block_file.generic_string().c_str(), LOG_READ );
block_write = false;
}
}
FC_LOG_AND_RETHROW()
}
inline void check_block_write()
{
try
{
if( !block_write )
{
block_stream.close();
block_stream.open( block_file.generic_string().c_str(), LOG_WRITE );
block_write = true;
}
}
FC_LOG_AND_RETHROW()
}
inline void check_index_read()
{
try
{
if( index_write )
{
index_stream.close();
index_stream.open( index_file.generic_string().c_str(), LOG_READ );
index_write = false;
}
}
FC_LOG_AND_RETHROW()
}
inline void check_index_write()
{
try
{
if( !index_write )
{
index_stream.close();
index_stream.open( index_file.generic_string().c_str(), LOG_WRITE );
index_write = true;
}
}
FC_LOG_AND_RETHROW()
}
};
}
block_log::block_log()
:my( new detail::block_log_impl() )
{
my->block_stream.exceptions( std::fstream::failbit | std::fstream::badbit );
my->index_stream.exceptions( std::fstream::failbit | std::fstream::badbit );
}
block_log::~block_log()
{
flush();
}
void block_log::open( const fc::path& file )
{
if( my->block_stream.is_open() )
my->block_stream.close();
if( my->index_stream.is_open() )
my->index_stream.close();
my->block_file = file;
my->index_file = fc::path( file.generic_string() + ".index" );
my->block_stream.open( my->block_file.generic_string().c_str(), LOG_WRITE );
my->index_stream.open( my->index_file.generic_string().c_str(), LOG_WRITE );
my->block_write = true;
my->index_write = true;
/* On startup of the block log, there are several states the log file and the index file can be
* in relation to eachother.
*
* Block Log
* Exists Is New
* +------------+------------+
* Exists | Check | Delete |
* Index | Head | Index |
* File +------------+------------+
* Is New | Replay | Do |
* | Log | Nothing |
* +------------+------------+
*
* Checking the heads of the files has several conditions as well.
* - If they are the same, do nothing.
* - If the index file head is not in the log file, delete the index and replay.
* - If the index file head is in the log, but not up to date, replay from index head.
*/
auto log_size = fc::file_size( my->block_file );
auto index_size = fc::file_size( my->index_file );
if( log_size )
{
ilog( "Log is nonempty" );
my->head = read_head();
my->head_id = my->head->id();
if( index_size )
{
my->check_block_read();
my->check_index_read();
ilog( "Index is nonempty" );
uint64_t block_pos;
my->block_stream.seekg( -sizeof( uint64_t), std::ios::end );
my->block_stream.read( (char*)&block_pos, sizeof( block_pos ) );
uint64_t index_pos;
my->index_stream.seekg( -sizeof( uint64_t), std::ios::end );
my->index_stream.read( (char*)&index_pos, sizeof( index_pos ) );
if( block_pos < index_pos )
{
ilog( "block_pos < index_pos, close and reopen index_stream" );
construct_index();
}
else if( block_pos > index_pos )
{
ilog( "Index is incomplete" );
construct_index();
}
}
else
{
ilog( "Index is empty" );
construct_index();
}
}
else if( index_size )
{
ilog( "Index is nonempty, remove and recreate it" );
my->index_stream.close();
fc::remove_all( my->index_file );
my->index_stream.open( my->index_file.generic_string().c_str(), LOG_WRITE );
my->index_write = true;
}
}
void block_log::close()
{
my.reset( new detail::block_log_impl() );
}
bool block_log::is_open()const
{
return my->block_stream.is_open();
}
uint64_t block_log::append( const signed_block& b )
{
try
{
my->check_block_write();
my->check_index_write();
uint64_t pos = my->block_stream.tellp();
uint64_t index_pos = my->index_stream.tellp();
FC_ASSERT( index_pos == sizeof( uint64_t ) * uint64_t( b.block_num() - 1 ), "Append to index file occuring at wrong position.", ( "position", (uint64_t) my->index_stream.tellp() )( "expected",( b.block_num() - 1 ) * sizeof( uint64_t ) ) );
auto data = fc::raw::pack( b );
my->block_stream.write( data.data(), data.size() );
my->block_stream.write( (char*)&pos, sizeof( pos ) );
my->index_stream.write( (char*)&pos, sizeof( pos ) );
my->head = b;
my->head_id = b.id();
return pos;
}
FC_LOG_AND_RETHROW()
}
void block_log::flush()
{
my->block_stream.flush();
my->index_stream.flush();
}
std::pair< signed_block, uint64_t > block_log::read_block( uint64_t pos )const
{
try
{
my->check_block_read();
my->block_stream.seekg( pos );
std::pair<signed_block,uint64_t> result;
fc::raw::unpack( my->block_stream, result.first );
result.second = uint64_t(my->block_stream.tellg()) + 8;
return result;
}
FC_LOG_AND_RETHROW()
}
optional< signed_block > block_log::read_block_by_num( uint32_t block_num )const
{
try
{
optional< signed_block > b;
uint64_t pos = get_block_pos( block_num );
if( pos != npos )
{
b = read_block( pos ).first;
FC_ASSERT( b->block_num() == block_num , "Wrong block was read from block log.", ( "returned", b->block_num() )( "expected", block_num ));
}
return b;
}
FC_LOG_AND_RETHROW()
}
uint64_t block_log::get_block_pos( uint32_t block_num ) const
{
try
{
my->check_index_read();
if( !( my->head.valid() && block_num <= protocol::block_header::num_from_id( my->head_id ) && block_num > 0 ) )
return npos;
my->index_stream.seekg( sizeof( uint64_t ) * ( block_num - 1 ) );
uint64_t pos;
my->index_stream.read( (char*)&pos, sizeof( pos ) );
return pos;
}
FC_LOG_AND_RETHROW()
}
signed_block block_log::read_head()const
{
try
{
my->check_block_read();
uint64_t pos;
my->block_stream.seekg( -sizeof(pos), std::ios::end );
my->block_stream.read( (char*)&pos, sizeof(pos) );
return read_block( pos ).first;
}
FC_LOG_AND_RETHROW()
}
const optional< signed_block >& block_log::head()const
{
return my->head;
}
void block_log::construct_index()
{
try
{
ilog( "Reconstructing Block Log Index..." );
my->index_stream.close();
fc::remove_all( my->index_file );
my->index_stream.open( my->index_file.generic_string().c_str(), LOG_WRITE );
my->index_write = true;
uint64_t pos = 0;
uint64_t end_pos;
my->check_block_read();
my->block_stream.seekg( -sizeof( uint64_t), std::ios::end );
my->block_stream.read( (char*)&end_pos, sizeof( end_pos ) );
signed_block tmp;
my->block_stream.seekg( pos );
while( pos < end_pos )
{
fc::raw::unpack( my->block_stream, tmp );
my->block_stream.read( (char*)&pos, sizeof( pos ) );
my->index_stream.write( (char*)&pos, sizeof( pos ) );
}
}
FC_LOG_AND_RETHROW()
}
} } // steemit::chain