From 631849e7cce8e72012152ff396aec0f44470d381 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 5 Aug 2017 01:39:42 +0200 Subject: [PATCH] Cut back block_log index file if inconsistent --- libraries/chain/block_database.cpp | 12 +++++++----- .../chain/include/graphene/chain/block_database.hpp | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/libraries/chain/block_database.cpp b/libraries/chain/block_database.cpp index 87aeed28..3dcdcba4 100644 --- a/libraries/chain/block_database.cpp +++ b/libraries/chain/block_database.cpp @@ -45,14 +45,15 @@ void block_database::open( const fc::path& dbdir ) _block_num_to_pos.exceptions(std::ios_base::failbit | std::ios_base::badbit); _blocks.exceptions(std::ios_base::failbit | std::ios_base::badbit); - if( !fc::exists( dbdir/"index" ) ) + _index_filename = dbdir / "index"; + if( !fc::exists( _index_filename ) ) { - _block_num_to_pos.open( (dbdir/"index").generic_string().c_str(), std::fstream::binary | std::fstream::in | std::fstream::out | std::fstream::trunc); + _block_num_to_pos.open( _index_filename.generic_string().c_str(), std::fstream::binary | std::fstream::in | std::fstream::out | std::fstream::trunc); _blocks.open( (dbdir/"blocks").generic_string().c_str(), std::fstream::binary | std::fstream::in | std::fstream::out | std::fstream::trunc); } else { - _block_num_to_pos.open( (dbdir/"index").generic_string().c_str(), std::fstream::binary | std::fstream::in | std::fstream::out ); + _block_num_to_pos.open( _index_filename.generic_string().c_str(), std::fstream::binary | std::fstream::in | std::fstream::out ); _blocks.open( (dbdir/"blocks").generic_string().c_str(), std::fstream::binary | std::fstream::in | std::fstream::out ); } } FC_CAPTURE_AND_RETHROW( (dbdir) ) } @@ -121,7 +122,7 @@ bool block_database::contains( const block_id_type& id )const index_entry e; auto index_pos = sizeof(e)*block_header::num_from_id(id); _block_num_to_pos.seekg( 0, _block_num_to_pos.end ); - if ( _block_num_to_pos.tellg() <= index_pos ) + if ( _block_num_to_pos.tellg() < index_pos + sizeof(e) ) return false; _block_num_to_pos.seekg( index_pos ); _block_num_to_pos.read( (char*)&e, sizeof(e) ); @@ -220,7 +221,7 @@ optional block_database::last_index_entry()const { _blocks.seekg( 0, _block_num_to_pos.end ); const std::streampos blocks_size = _blocks.tellg(); - while( pos >= 0 ) + while( pos > 0 ) { pos -= sizeof(index_entry); _block_num_to_pos.seekg( pos ); @@ -245,6 +246,7 @@ optional block_database::last_index_entry()const { catch (const std::exception&) { } + fc::resize_file( _index_filename, pos ); } } catch (const fc::exception&) diff --git a/libraries/chain/include/graphene/chain/block_database.hpp b/libraries/chain/include/graphene/chain/block_database.hpp index 2c7ff812..d902cd1b 100644 --- a/libraries/chain/include/graphene/chain/block_database.hpp +++ b/libraries/chain/include/graphene/chain/block_database.hpp @@ -47,6 +47,7 @@ namespace graphene { namespace chain { optional last_id()const; private: optional last_index_entry()const; + fc::path _index_filename; mutable std::fstream _blocks; mutable std::fstream _block_num_to_pos; };