@@ -1757,16 +1757,23 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
1757
1757
return true ;
1758
1758
}
1759
1759
1760
+ enum FlushStateMode {
1761
+ FLUSH_STATE_IF_NEEDED,
1762
+ FLUSH_STATE_PERIODIC,
1763
+ FLUSH_STATE_ALWAYS
1764
+ };
1765
+
1760
1766
/* *
1761
1767
* Update the on-disk chain state.
1762
1768
* The caches and indexes are flushed if either they're too large, forceWrite is set, or
1763
1769
* fast is not set and it's been a while since the last write.
1764
1770
*/
1765
- bool static FlushStateToDisk (CValidationState &state, bool fast = false , bool forceWrite = false ) {
1771
+ bool static FlushStateToDisk (CValidationState &state, FlushStateMode mode ) {
1766
1772
LOCK (cs_main);
1767
1773
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 )) {
1770
1777
// Typical CCoins structures on disk are around 100 bytes in size.
1771
1778
// Pushing a new one to the database can cause it to be written
1772
1779
// 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
1799
1806
if (!pcoinsTip->Flush ())
1800
1807
return state.Abort (" Failed to write to coin database" );
1801
1808
// Update best block in wallet (so we can detect restored wallets).
1802
- if (forceWrite || !fast ) {
1809
+ if (mode != FLUSH_STATE_IF_NEEDED ) {
1803
1810
g_signals.SetBestChain (chainActive.GetLocator ());
1804
1811
}
1805
1812
nLastWrite = GetTimeMicros ();
@@ -1809,7 +1816,7 @@ bool static FlushStateToDisk(CValidationState &state, bool fast = false, bool fo
1809
1816
1810
1817
void FlushStateToDisk () {
1811
1818
CValidationState state;
1812
- FlushStateToDisk (state, false , true );
1819
+ FlushStateToDisk (state, FLUSH_STATE_ALWAYS );
1813
1820
}
1814
1821
1815
1822
// Update chainActive and related internal data structures.
@@ -1870,7 +1877,7 @@ bool static DisconnectTip(CValidationState &state) {
1870
1877
}
1871
1878
LogPrint (" bench" , " - Disconnect block: %.2fms\n " , (GetTimeMicros () - nStart) * 0.001 );
1872
1879
// Write the chain state to disk, if necessary.
1873
- if (!FlushStateToDisk (state, true ))
1880
+ if (!FlushStateToDisk (state, FLUSH_STATE_IF_NEEDED ))
1874
1881
return false ;
1875
1882
// Resurrect mempool transactions from the disconnected block.
1876
1883
BOOST_FOREACH (const CTransaction &tx, block.vtx ) {
@@ -1933,7 +1940,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *
1933
1940
int64_t nTime4 = GetTimeMicros (); nTimeFlush += nTime4 - nTime3;
1934
1941
LogPrint (" bench" , " - Flush: %.2fms [%.2fs]\n " , (nTime4 - nTime3) * 0.001 , nTimeFlush * 0.000001 );
1935
1942
// Write the chain state to disk, if necessary.
1936
- if (!FlushStateToDisk (state, true ))
1943
+ if (!FlushStateToDisk (state, FLUSH_STATE_IF_NEEDED ))
1937
1944
return false ;
1938
1945
int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
1939
1946
LogPrint (" bench" , " - Writing chainstate: %.2fms [%.2fs]\n " , (nTime5 - nTime4) * 0.001 , nTimeChainState * 0.000001 );
@@ -2118,7 +2125,7 @@ bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
2118
2125
} while (pindexMostWork != chainActive.Tip ());
2119
2126
2120
2127
// Write changes periodically to disk, after relay.
2121
- if (!FlushStateToDisk (state)) {
2128
+ if (!FlushStateToDisk (state, FLUSH_STATE_PERIODIC )) {
2122
2129
return false ;
2123
2130
}
2124
2131
@@ -3089,7 +3096,7 @@ bool InitBlockIndex() {
3089
3096
if (!ActivateBestChain (state, &block))
3090
3097
return error (" LoadBlockIndex() : genesis block cannot be activated" );
3091
3098
// 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 );
3093
3100
} catch (std::runtime_error &e) {
3094
3101
return error (" LoadBlockIndex() : failed to initialize block database: %s" , e.what ());
3095
3102
}
0 commit comments