Skip to content

Commit

Permalink
Add extra assertions to detect a corrupt block log steemit#738 steemi…
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Vandeberg committed Jan 2, 2017
1 parent b427211 commit 77ee892
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ namespace detail {

bool is_included_block(const block_id_type& block_id)
{
uint32_t block_num = block_header::num_from_id(block_id);
block_id_type block_id_in_preferred_chain = _chain_db->get_block_id_for_num(block_num);
return block_id == block_id_in_preferred_chain;
uint32_t block_num = block_header::num_from_id(block_id);
block_id_type block_id_in_preferred_chain = _chain_db->get_block_id_for_num(block_num);
return block_id == block_id_in_preferred_chain;
}

/**
Expand Down
30 changes: 20 additions & 10 deletions libraries/chain/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,23 @@ namespace steemit { namespace chain {

uint64_t block_log::append( const signed_block& b )
{
my->check_block_write();
my->check_index_write();

uint64_t pos = my->block_stream.tellp();
fc::raw::pack( my->block_stream, b );
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;
try
{
my->check_block_write();
my->check_index_write();

uint64_t pos = my->block_stream.tellp();
FC_ASSERT( my->index_stream.tellp() == sizeof( 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()
Expand All @@ -208,7 +215,10 @@ namespace steemit { namespace chain {
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()
Expand Down

0 comments on commit 77ee892

Please sign in to comment.