Skip to content

Commit a206950

Browse files
committed
Introduce separate flushing modes
1 parent 51ce901 commit a206950

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/main.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,16 +1757,23 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
17571757
return true;
17581758
}
17591759

1760+
enum FlushStateMode {
1761+
FLUSH_STATE_IF_NEEDED,
1762+
FLUSH_STATE_PERIODIC,
1763+
FLUSH_STATE_ALWAYS
1764+
};
1765+
17601766
/**
17611767
* Update the on-disk chain state.
17621768
* The caches and indexes are flushed if either they're too large, forceWrite is set, or
17631769
* fast is not set and it's been a while since the last write.
17641770
*/
1765-
bool static FlushStateToDisk(CValidationState &state, bool fast = false, bool forceWrite = false) {
1771+
bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
17661772
LOCK(cs_main);
17671773
static int64_t nLastWrite = 0;
1768-
if (forceWrite || pcoinsTip->GetCacheSize() > nCoinCacheSize ||
1769-
(!fast && GetTimeMicros() > nLastWrite + DATABASE_WRITE_INTERVAL * 1000000)) {
1774+
if ((mode == FLUSH_STATE_ALWAYS) ||
1775+
((mode == FLUSH_STATE_PERIODIC || mode == FLUSH_STATE_IF_NEEDED) && pcoinsTip->GetCacheSize() > nCoinCacheSize) ||
1776+
(mode == FLUSH_STATE_PERIODIC && GetTimeMicros() > nLastWrite + DATABASE_WRITE_INTERVAL * 1000000)) {
17701777
// Typical CCoins structures on disk are around 100 bytes in size.
17711778
// Pushing a new one to the database can cause it to be written
17721779
// twice (once in the log, and once in the tables). This is already
@@ -1799,7 +1806,7 @@ bool static FlushStateToDisk(CValidationState &state, bool fast = false, bool fo
17991806
if (!pcoinsTip->Flush())
18001807
return state.Abort("Failed to write to coin database");
18011808
// Update best block in wallet (so we can detect restored wallets).
1802-
if (forceWrite || !fast) {
1809+
if (mode != FLUSH_STATE_IF_NEEDED) {
18031810
g_signals.SetBestChain(chainActive.GetLocator());
18041811
}
18051812
nLastWrite = GetTimeMicros();
@@ -1809,7 +1816,7 @@ bool static FlushStateToDisk(CValidationState &state, bool fast = false, bool fo
18091816

18101817
void FlushStateToDisk() {
18111818
CValidationState state;
1812-
FlushStateToDisk(state, false, true);
1819+
FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
18131820
}
18141821

18151822
// Update chainActive and related internal data structures.
@@ -1870,7 +1877,7 @@ bool static DisconnectTip(CValidationState &state) {
18701877
}
18711878
LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
18721879
// Write the chain state to disk, if necessary.
1873-
if (!FlushStateToDisk(state, true))
1880+
if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
18741881
return false;
18751882
// Resurrect mempool transactions from the disconnected block.
18761883
BOOST_FOREACH(const CTransaction &tx, block.vtx) {
@@ -1933,7 +1940,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *
19331940
int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
19341941
LogPrint("bench", " - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
19351942
// Write the chain state to disk, if necessary.
1936-
if (!FlushStateToDisk(state, true))
1943+
if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
19371944
return false;
19381945
int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
19391946
LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
@@ -2118,7 +2125,7 @@ bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
21182125
} while(pindexMostWork != chainActive.Tip());
21192126

21202127
// Write changes periodically to disk, after relay.
2121-
if (!FlushStateToDisk(state)) {
2128+
if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
21222129
return false;
21232130
}
21242131

@@ -3089,7 +3096,7 @@ bool InitBlockIndex() {
30893096
if (!ActivateBestChain(state, &block))
30903097
return error("LoadBlockIndex() : genesis block cannot be activated");
30913098
// Force a chainstate write so that when we VerifyDB in a moment, it doesnt check stale data
3092-
return FlushStateToDisk(state, false, true);
3099+
return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
30933100
} catch(std::runtime_error &e) {
30943101
return error("LoadBlockIndex() : failed to initialize block database: %s", e.what());
30953102
}

0 commit comments

Comments
 (0)