11#include < golos/chain/block_log.hpp>
22#include < fstream>
3+ #include < mutex>
34
45#define LOG_READ (std::ios::in | std::ios::binary)
56#define LOG_WRITE (std::ios::out | std::ios::binary | std::ios::app)
@@ -18,6 +19,7 @@ namespace golos {
1819 fc::path index_file;
1920 bool block_write;
2021 bool index_write;
22+ std::mutex mutex;
2123
2224 inline void check_block_read () {
2325 if (block_write) {
@@ -58,10 +60,8 @@ namespace golos {
5860
5961 block_log::block_log ()
6062 : my(new detail::block_log_impl()) {
61- my->block_stream .exceptions (
62- std::fstream::failbit | std::fstream::badbit);
63- my->index_stream .exceptions (
64- std::fstream::failbit | std::fstream::badbit);
63+ my->block_stream .exceptions (std::fstream::failbit | std::fstream::badbit);
64+ my->index_stream .exceptions (std::fstream::failbit | std::fstream::badbit);
6565 }
6666
6767 block_log::~block_log () {
@@ -153,20 +153,25 @@ namespace golos {
153153
154154 uint64_t block_log::append (const signed_block &b) {
155155 try {
156- my->check_block_write ();
157- my->check_index_write ();
158-
159- uint64_t pos = my->block_stream .tellp ();
160- FC_ASSERT (my->index_stream .tellp () == sizeof (uint64_t ) *
161- (b.block_num () -
162- 1 ), " Append to index file occuring at wrong position." , (" position" , (uint64_t )my->index_stream .tellp ())(" expected" ,
163- (b.block_num () - 1 ) * sizeof (uint64_t )));
164156 auto data = fc::raw::pack (b);
165- my->block_stream .write (data.data (), data.size ());
166- my->block_stream .write ((char *)&pos, sizeof (pos));
167- my->index_stream .write ((char *)&pos, sizeof (pos));
168- my->head = b;
169- my->head_id = b.id ();
157+ uint64_t pos;
158+ {
159+ std::lock_guard<std::mutex> lock (my->mutex );
160+ my->check_block_write ();
161+ my->check_index_write ();
162+
163+ FC_ASSERT (my->index_stream .tellp () == sizeof (uint64_t ) * (b.block_num () - 1 ),
164+ " Append to index file occuring at wrong position." ,
165+ (" position" , (uint64_t ) my->index_stream .tellp ())
166+ (" expected" , (b.block_num () - 1 ) * sizeof (uint64_t )));
167+
168+ pos = my->block_stream .tellp ();
169+ my->block_stream .write (data.data (), data.size ());
170+ my->block_stream .write ((char *) &pos, sizeof (pos));
171+ my->index_stream .write ((char *) &pos, sizeof (pos));
172+ my->head = b;
173+ my->head_id = b.id ();
174+ }
170175
171176 return pos;
172177 }
@@ -179,12 +184,14 @@ namespace golos {
179184 }
180185
181186 std::pair<signed_block, uint64_t > block_log::read_block (uint64_t pos) const {
182- my->check_block_read ();
183-
184- my->block_stream .seekg (pos);
185187 std::pair<signed_block, uint64_t > result;
186- fc::raw::unpack (my->block_stream , result.first );
187- result.second = uint64_t (my->block_stream .tellg ()) + 8 ;
188+ {
189+ std::lock_guard<std::mutex> lock (my->mutex );
190+ my->check_block_read ();
191+ my->block_stream .seekg (pos);
192+ fc::raw::unpack (my->block_stream , result.first );
193+ result.second = uint64_t (my->block_stream .tellg ()) + 8 ;
194+ }
188195 return result;
189196 }
190197
@@ -194,34 +201,44 @@ namespace golos {
194201 uint64_t pos = get_block_pos (block_num);
195202 if (pos != npos) {
196203 b = read_block (pos).first ;
197- FC_ASSERT (b->block_num () ==
198- block_num, " Wrong block was read from block log." , (" returned" , b->block_num ())(" expected" , block_num));
204+ FC_ASSERT (b->block_num () == block_num,
205+ " Wrong block was read from block log." ,
206+ (" returned" , b->block_num ())
207+ (" expected" , block_num));
199208 }
200209 return b;
201210 }
202211 FC_LOG_AND_RETHROW ()
203212 }
204213
205214 uint64_t block_log::get_block_pos (uint32_t block_num) const {
206- my->check_index_read ();
215+ uint64_t pos;
216+ {
217+ std::lock_guard<std::mutex> lock (my->mutex );
218+
219+ if (!(my->head .valid () &&
220+ block_num <= protocol::block_header::num_from_id (my->head_id ) &&
221+ block_num > 0 )
222+ ) {
223+ return npos;
224+ }
207225
208- if (!(my->head .valid () && block_num <=
209- protocol::block_header::num_from_id (my->head_id ) &&
210- block_num > 0 )) {
211- return npos;
226+ my->check_index_read ();
227+ my->index_stream .seekg (sizeof (uint64_t ) * (block_num - 1 ));
228+ my->index_stream .read ((char *) &pos, sizeof (pos));
212229 }
213- my->index_stream .seekg (sizeof (uint64_t ) * (block_num - 1 ));
214- uint64_t pos;
215- my->index_stream .read ((char *)&pos, sizeof (pos));
216230 return pos;
217231 }
218232
219233 signed_block block_log::read_head () const {
220- my->check_block_read ();
221-
222234 uint64_t pos;
223- my->block_stream .seekg (-sizeof (pos), std::ios::end);
224- my->block_stream .read ((char *)&pos, sizeof (pos));
235+ {
236+ std::lock_guard<std::mutex> lock (my->mutex );
237+ my->check_block_read ();
238+
239+ my->block_stream .seekg (-sizeof (pos), std::ios::end);
240+ my->block_stream .read ((char *) &pos, sizeof (pos));
241+ }
225242 return read_block (pos).first ;
226243 }
227244
0 commit comments