application.cpp: Cleanup re-indexing logic #587 DOES NOT COMPILE

This commit is contained in:
theoreticalbts 2016-02-16 14:11:05 -05:00
parent 567a40170e
commit 62a414ac85

View file

@ -308,13 +308,25 @@ namespace detail {
} }
_chain_db->add_checkpoints( loaded_checkpoints ); _chain_db->add_checkpoints( loaded_checkpoints );
if( _options->count("replay-blockchain") ) bool need_reindex = false;
{ std::string reindex_reason = "(not needed)";
ilog("Replaying blockchain on user request.");
_chain_db->reindex(_data_dir/"blockchain", initial_state());
} else if( clean ) {
auto is_new = [&]() -> bool while( true )
{
if( _options->count("replay-blockchain" ) )
{
need_reindex = true;
reindex_reason = "user request";
break;
}
if( !clean )
{
need_reindex = true;
reindex_reason = "unclean shutdown";
break;
}
auto check_is_new = [&]() -> bool
{ {
// directory doesn't exist // directory doesn't exist
if( !fc::exists( _data_dir ) ) if( !fc::exists( _data_dir ) )
@ -323,7 +335,7 @@ namespace detail {
return ( fc::directory_iterator( _data_dir ) == fc::directory_iterator() ); return ( fc::directory_iterator( _data_dir ) == fc::directory_iterator() );
}; };
auto is_outdated = [&]() -> bool auto check_is_outdated = [&]() -> bool
{ {
if( !fc::exists( _data_dir / "db_version" ) ) if( !fc::exists( _data_dir / "db_version" ) )
return true; return true;
@ -332,55 +344,54 @@ namespace detail {
return (version_str != GRAPHENE_CURRENT_DB_VERSION); return (version_str != GRAPHENE_CURRENT_DB_VERSION);
}; };
bool need_reindex = (!is_new() && is_outdated()); bool is_new = check_is_new();
std::string reindex_reason = "version upgrade";
if( !need_reindex ) if( is_new )
{ {
try need_reindex = false;
{ break;
_chain_db->open(_data_dir / "blockchain", initial_state);
}
catch( const fc::exception& e )
{
ilog( "caught exception ${e} in open()", ("e", e.to_detail_string()) );
need_reindex = true;
reindex_reason = "exception in open()";
}
} }
if( need_reindex ) if( is_outdated )
{ {
ilog("Replaying blockchain due to ${reason}", ("reason", reindex_reason) ); need_reindex = true;
reindex_reason = "outdated datadir";
fc::remove_all( _data_dir / "db_version" ); break;
_chain_db->reindex(_data_dir / "blockchain", initial_state());
// doing this down here helps ensure that DB will be wiped
// if any of the above steps were interrupted on a previous run
if( !fc::exists( _data_dir / "db_version" ) )
{
std::ofstream db_version(
(_data_dir / "db_version").generic_string().c_str(),
std::ios::out | std::ios::binary | std::ios::trunc );
std::string version_string = GRAPHENE_CURRENT_DB_VERSION;
db_version.write( version_string.c_str(), version_string.size() );
db_version.close();
}
} }
} else {
wlog("Detected unclean shutdown. Replaying blockchain...");
_chain_db->reindex(_data_dir / "blockchain", initial_state());
} }
if (!_options->count("genesis-json") && if( !need_reindex )
_chain_db->get_chain_id() != graphene::egenesis::get_egenesis_chain_id()) { {
elog("Detected old database. Nuking and starting over."); try
_chain_db->wipe(_data_dir / "blockchain", true); {
_chain_db.reset(); _chain_db->open(_data_dir / "blockchain", initial_state);
_chain_db = std::make_shared<chain::database>(); }
_chain_db->add_checkpoints(loaded_checkpoints); catch( const fc::exception& e )
_chain_db->open(_data_dir / "blockchain", initial_state); {
ilog( "caught exception ${e} in open()", ("e", e.to_detail_string()) );
need_reindex = true;
reindex_reason = "exception in open()";
}
}
if( need_reindex )
{
ilog("Replaying blockchain due to ${reason}", ("reason", reindex_reason) );
fc::remove_all( _data_dir / "db_version" );
_chain_db->reindex(_data_dir / "blockchain", initial_state());
// doing this down here helps ensure that DB will be wiped
// if any of the above steps were interrupted on a previous run
if( !fc::exists( _data_dir / "db_version" ) )
{
std::ofstream db_version(
(_data_dir / "db_version").generic_string().c_str(),
std::ios::out | std::ios::binary | std::ios::trunc );
std::string version_string = GRAPHENE_CURRENT_DB_VERSION;
db_version.write( version_string.c_str(), version_string.size() );
db_version.close();
}
} }
if( _options->count("force-validate") ) if( _options->count("force-validate") )