Skip to content

Commit b1af6ff

Browse files
authored
Merge pull request #331 from fireice-uk/cache-backup
Implement cache file backup
2 parents 5a3d815 + 4591c72 commit b1af6ff

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/CryptoNoteCore/Blockchain.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,19 @@ namespace cn
480480
{
481481
logger(INFO) << "Loading blockchain";
482482
BlockCacheSerializer loader(*this, get_block_hash(m_blocks.back().bl), logger.getLogger());
483-
loader.load(appendPath(config_folder, m_currency.blocksCacheFileName()));
483+
const std::string &blocksCacheFileName = m_currency.blocksCacheFileName();
484+
loader.load(appendPath(config_folder, blocksCacheFileName));
484485

485486
if (!loader.loaded())
486487
{
487-
logger(WARNING, BRIGHT_YELLOW) << " No actual blockchain cache found, rebuilding internal structures";
488-
rebuildCache();
488+
std::string blockCacheBkpFileName = blocksCacheFileName + ".bkp";
489+
loader.load(appendPath(config_folder, blockCacheBkpFileName));
490+
491+
if (!loader.loaded())
492+
{
493+
logger(WARNING, BRIGHT_YELLOW) << " No actual blockchain cache found, rebuilding internal structures";
494+
rebuildCache();
495+
}
489496
}
490497
uint64_t checkBlockHeight = 24732;
491498
uint64_t checkMinimum = 13000000000000;
@@ -736,7 +743,13 @@ namespace cn
736743

737744
logger(INFO, BRIGHT_WHITE) << "Saving blockchain...";
738745
BlockCacheSerializer ser(*this, getTailId(), logger.getLogger());
739-
if (!ser.save(appendPath(m_config_folder, m_currency.blocksCacheFileName())))
746+
747+
const std::string &blocksCacheFileName = m_currency.blocksCacheFileName();
748+
std::string blockCacheBkpFileName = blocksCacheFileName + ".bkp";
749+
750+
std::rename(blocksCacheFileName.c_str(), blockCacheBkpFileName.c_str()); // fail here can be ignored
751+
752+
if (!ser.save(appendPath(m_config_folder, blocksCacheFileName)))
740753
{
741754
logger(ERROR, BRIGHT_RED) << "Failed to save blockchain cache";
742755
return false;
@@ -3287,4 +3300,4 @@ namespace cn
32873300
return m_checkpoints.is_in_checkpoint_zone(height);
32883301
}
32893302

3290-
} // namespace cn
3303+
} // namespace cn

0 commit comments

Comments
 (0)