Skip to content

Commit

Permalink
validation: flush state after initial sync
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtoth committed Apr 20, 2019
1 parent 56376f3 commit b3770f4
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
// Dump addresses to banlist.dat every 15 minutes (900s)
static constexpr int DUMP_BANS_INTERVAL = 60 * 15;

//! Check if initial sync is done and no change in block height every 30s
static constexpr int SYNC_CHECK_INTERVAL = 30;

std::unique_ptr<CConnman> g_connman;
std::unique_ptr<PeerLogicValidation> peerLogic;
std::unique_ptr<BanMan> g_banman;
Expand Down Expand Up @@ -1250,6 +1253,28 @@ bool AppInitLockDataDirectory()
return true;
}

/**
* Once initial sync is finished, flush state to protect against data loss
*/
static void FlushAfterSync()
{
if (IsInitialBlockDownload()) {
scheduler.scheduleFromNow(FlushAfterSync, SYNC_CHECK_INTERVAL * 1000);
return;
}

static int last_chain_height = -1;
LOCK(cs_main);
int current_height = chainActive.Height();
if (last_chain_height == -1 || last_chain_height != current_height) {
last_chain_height = current_height;
scheduler.scheduleFromNow(FlushAfterSync, SYNC_CHECK_INTERVAL * 1000);
return;
}

FlushStateToDisk();
}

bool AppInitMain(InitInterfaces& interfaces)
{
const CChainParams& chainparams = Params();
Expand Down Expand Up @@ -1858,5 +1883,7 @@ bool AppInitMain(InitInterfaces& interfaces)
g_banman->DumpBanlist();
}, DUMP_BANS_INTERVAL * 1000);

scheduler.scheduleFromNow(FlushAfterSync, SYNC_CHECK_INTERVAL * 1000);

return true;
}

0 comments on commit b3770f4

Please sign in to comment.