Skip to content

Commit

Permalink
When blocks are written to log, they are removed from chain state mem…
Browse files Browse the repository at this point in the history
…ory #675
  • Loading branch information
Michael Vandeberg committed Dec 7, 2016
1 parent 4cf920f commit 51f99ca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
36 changes: 19 additions & 17 deletions libraries/chain/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,18 @@ namespace steemit { namespace chain {
if( block_pos < index_pos )
{
ilog( "block_pos < index_pos, close and reopen index_stream" );
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;
construct_index( 0 );
construct_index();
}
else if( block_pos > index_pos )
{
construct_index( index_pos );
ilog( "Index is incomplete" );
construct_index();
}
}
else
{
ilog( "Index is empty, rebuild it" );
construct_index( 0 );
ilog( "Index is empty" );
construct_index();
}
}
else if( index_size )
Expand Down Expand Up @@ -204,7 +201,7 @@ namespace steemit { namespace chain {
{
optional< signed_block > b;
uint64_t pos = get_block_pos( block_num );
if( ~pos )
if( !pos )

This comment has been minimized.

Copy link
@arhag

arhag Dec 8, 2016

Contributor

This change in this line looks like a mistake to me.

b = read_block( pos ).first;
return b;
}
Expand All @@ -218,7 +215,6 @@ namespace steemit { namespace chain {
my->index_stream.seekg( sizeof( uint64_t ) * ( block_num - 1 ) );
uint64_t pos;
my->index_stream.read( (char*)&pos, sizeof( pos ) );
idump( (block_num)(pos) );
return pos;
}

Expand All @@ -237,23 +233,29 @@ namespace steemit { namespace chain {
return my->head;
}

void block_log::construct_index( uint64_t start_pos )
void block_log::construct_index()
{
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->check_index_write();

my->block_stream.seekg( -sizeof( uint64_t), std::ios::end );
my->block_stream.read( (char*)&end_pos, sizeof( end_pos ) );
signed_block tmp;

while( start_pos < end_pos )
my->block_stream.seekg( pos );

while( pos < end_pos )
{
my->block_stream.seekg( start_pos );
fc::raw::unpack( my->block_stream, tmp );
start_pos = uint64_t( my->block_stream.tellg() ) + 8;
my->index_stream.write( (char*)&start_pos, sizeof( start_pos ) );
my->block_stream.read( (char*)&pos, sizeof( pos ) );
my->index_stream.write( (char*)&pos, sizeof( pos ) );
}
}

} }
9 changes: 6 additions & 3 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ void database::reindex( const fc::path& data_dir, const fc::path& shared_mem_dir
skip_witness_schedule_check |
skip_authority_check |
skip_validate | /// no need to validate operations
skip_validate_invariants;
skip_validate_invariants |
skip_block_log;

with_write_lock( [&]()
{
Expand Down Expand Up @@ -3541,15 +3542,17 @@ void database::update_last_irreversible_block()
while( log_head_num < dpo.last_irreversible_block_num )
{
_block_log.append( *fetch_block_by_number( log_head_num + 1 ) );
_block_log.flush();

modify( get< block_stats_object >( log_head_num + 1 ), [&]( block_stats_object& bso )
// Block stats object IDs are block num - 1, so the ID is ( log_head_num + 1 ) - 1
modify( get< block_stats_object >( log_head_num ), [&]( block_stats_object& bso )
{
bso.packed_block.clear();
});

log_head_num++;
}

_block_log.flush();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/steemit/chain/block_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace steemit { namespace chain {
const optional< signed_block >& head()const;

private:
void construct_index( uint64_t start_pos );
void construct_index();

std::unique_ptr<detail::block_log_impl> my;
};
Expand Down

0 comments on commit 51f99ca

Please sign in to comment.