Skip to content

Commit 3ccbc37

Browse files
committed
refactoring: FlushStateToDisk -> CChainState
Also renames global methods for clarity: - ::FlushStateToDisk() -> CChainState::ForceFlushStateToDisk() - This performs an unconditional flush. - ::PruneAndFlush() -> CChainState::PruneAndFlush()
1 parent 4d66886 commit 3ccbc37

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void Shutdown(InitInterfaces& interfaces)
259259

260260
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
261261
if (pcoinsTip != nullptr) {
262-
FlushStateToDisk();
262+
::ChainstateActive().ForceFlushStateToDisk();
263263
}
264264

265265
// After there are no more peers/RPC left to give us new data which may generate
@@ -275,7 +275,7 @@ void Shutdown(InitInterfaces& interfaces)
275275
{
276276
LOCK(cs_main);
277277
if (pcoinsTip != nullptr) {
278-
FlushStateToDisk();
278+
::ChainstateActive().ForceFlushStateToDisk();
279279
}
280280
pcoinsTip.reset();
281281
pcoinscatcher.reset();
@@ -1692,7 +1692,7 @@ bool AppInitMain(InitInterfaces& interfaces)
16921692
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK);
16931693
if (!fReindex) {
16941694
uiInterface.InitMessage(_("Pruning blockstore..."));
1695-
PruneAndFlush();
1695+
::ChainstateActive().PruneAndFlush();
16961696
}
16971697
}
16981698

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ static UniValue gettxoutsetinfo(const JSONRPCRequest& request)
10771077
UniValue ret(UniValue::VOBJ);
10781078

10791079
CCoinsStats stats;
1080-
FlushStateToDisk();
1080+
::ChainstateActive().ForceFlushStateToDisk();
10811081
if (GetUTXOStats(pcoinsdbview.get(), stats)) {
10821082
ret.pushKV("height", (int64_t)stats.nHeight);
10831083
ret.pushKV("bestblock", stats.hashBlock.GetHex());
@@ -2283,7 +2283,7 @@ UniValue scantxoutset(const JSONRPCRequest& request)
22832283
std::unique_ptr<CCoinsViewCursor> pcursor;
22842284
{
22852285
LOCK(cs_main);
2286-
FlushStateToDisk();
2286+
::ChainstateActive().ForceFlushStateToDisk();
22872287
pcursor = std::unique_ptr<CCoinsViewCursor>(pcoinsdbview->Cursor());
22882288
assert(pcursor);
22892289
}

src/validation.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,7 @@ std::unique_ptr<CCoinsViewDB> pcoinsdbview;
173173
std::unique_ptr<CCoinsViewCache> pcoinsTip;
174174
std::unique_ptr<CBlockTreeDB> pblocktree;
175175

176-
enum class FlushStateMode {
177-
NONE,
178-
IF_NEEDED,
179-
PERIODIC,
180-
ALWAYS
181-
};
182-
183176
// See definition for documentation
184-
static bool FlushStateToDisk(const CChainParams& chainParams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight=0);
185177
static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight);
186178
static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight);
187179
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
@@ -854,7 +846,7 @@ static bool AcceptToMemoryPoolWithTime(const CChainParams& chainparams, CTxMemPo
854846
}
855847
// After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
856848
CValidationState stateDummy;
857-
FlushStateToDisk(chainparams, stateDummy, FlushStateMode::PERIODIC);
849+
::ChainstateActive().FlushStateToDisk(chainparams, stateDummy, FlushStateMode::PERIODIC);
858850
return res;
859851
}
860852

@@ -1951,16 +1943,12 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
19511943
return true;
19521944
}
19531945

1954-
/**
1955-
* Update the on-disk chain state.
1956-
* The caches and indexes are flushed depending on the mode we're called with
1957-
* if they're too large, if it's been a while since the last write,
1958-
* or always and in all cases if we're in prune mode and are deleting files.
1959-
*
1960-
* If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do anything
1961-
* besides checking if we need to prune.
1962-
*/
1963-
bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight) {
1946+
bool CChainState::FlushStateToDisk(
1947+
const CChainParams& chainparams,
1948+
CValidationState &state,
1949+
FlushStateMode mode,
1950+
int nManualPruneHeight)
1951+
{
19641952
int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
19651953
LOCK(cs_main);
19661954
static int64_t nLastWrite = 0;
@@ -2058,27 +2046,28 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
20582046
}
20592047
if (full_flush_completed) {
20602048
// Update best block in wallet (so we can detect restored wallets).
2061-
GetMainSignals().ChainStateFlushed(::ChainActive().GetLocator());
2049+
GetMainSignals().ChainStateFlushed(m_chain.GetLocator());
20622050
}
20632051
} catch (const std::runtime_error& e) {
20642052
return AbortNode(state, std::string("System error while flushing: ") + e.what());
20652053
}
20662054
return true;
20672055
}
20682056

2069-
void FlushStateToDisk() {
2057+
void CChainState::ForceFlushStateToDisk() {
20702058
CValidationState state;
20712059
const CChainParams& chainparams = Params();
2072-
if (!FlushStateToDisk(chainparams, state, FlushStateMode::ALWAYS)) {
2060+
if (!this->FlushStateToDisk(chainparams, state, FlushStateMode::ALWAYS)) {
20732061
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
20742062
}
20752063
}
20762064

2077-
void PruneAndFlush() {
2065+
void CChainState::PruneAndFlush() {
20782066
CValidationState state;
20792067
fCheckForPruning = true;
20802068
const CChainParams& chainparams = Params();
2081-
if (!FlushStateToDisk(chainparams, state, FlushStateMode::NONE)) {
2069+
2070+
if (!this->FlushStateToDisk(chainparams, state, FlushStateMode::NONE)) {
20822071
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
20832072
}
20842073
}
@@ -3587,7 +3576,8 @@ void PruneBlockFilesManual(int nManualPruneHeight)
35873576
{
35883577
CValidationState state;
35893578
const CChainParams& chainparams = Params();
3590-
if (!FlushStateToDisk(chainparams, state, FlushStateMode::NONE, nManualPruneHeight)) {
3579+
if (!::ChainstateActive().FlushStateToDisk(
3580+
chainparams, state, FlushStateMode::NONE, nManualPruneHeight)) {
35913581
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
35923582
}
35933583
}
@@ -4183,7 +4173,7 @@ bool RewindBlockIndex(const CChainParams& params) {
41834173
// and skip it here, we're about to -reindex-chainstate anyway, so
41844174
// it'll get called a bunch real soon.
41854175
CValidationState state;
4186-
if (!FlushStateToDisk(params, state, FlushStateMode::ALWAYS)) {
4176+
if (!::ChainstateActive().FlushStateToDisk(params, state, FlushStateMode::ALWAYS)) {
41874177
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", FormatStateMessage(state));
41884178
return false;
41894179
}

src/validation.h

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <utility>
3232
#include <vector>
3333

34+
class CChainState;
3435
class CBlockIndex;
3536
class CBlockTreeDB;
3637
class CBlockUndo;
@@ -277,10 +278,6 @@ void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
277278
*/
278279
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
279280

280-
/** Flush all state, indexes and buffers to disk. */
281-
void FlushStateToDisk();
282-
/** Prune block files and flush state to disk. */
283-
void PruneAndFlush();
284281
/** Prune block files up to a given height */
285282
void PruneBlockFilesManual(int nManualPruneHeight);
286283

@@ -432,6 +429,14 @@ enum DisconnectResult
432429

433430
class ConnectTrace;
434431

432+
/** @see CChainState::FlushStateToDisk */
433+
enum class FlushStateMode {
434+
NONE,
435+
IF_NEEDED,
436+
PERIODIC,
437+
ALWAYS
438+
};
439+
435440
struct CBlockIndexWorkComparator
436441
{
437442
bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const;
@@ -508,6 +513,28 @@ class CChainState {
508513

509514
bool LoadBlockIndex(const Consensus::Params& consensus_params, CBlockTreeDB& blocktree) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
510515

516+
/**
517+
* Update the on-disk chain state.
518+
* The caches and indexes are flushed depending on the mode we're called with
519+
* if they're too large, if it's been a while since the last write,
520+
* or always and in all cases if we're in prune mode and are deleting files.
521+
*
522+
* If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do anything
523+
* besides checking if we need to prune.
524+
*/
525+
bool FlushStateToDisk(
526+
const CChainParams& chainparams,
527+
CValidationState &state,
528+
FlushStateMode mode,
529+
int nManualPruneHeight = 0);
530+
531+
//! Unconditionally flush all changes to disk.
532+
void ForceFlushStateToDisk();
533+
534+
//! Prune blockfiles from the disk if necessary and then flush chainstate changes
535+
//! if we pruned.
536+
void PruneAndFlush();
537+
511538
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) LOCKS_EXCLUDED(cs_main);
512539

513540
/**

0 commit comments

Comments
 (0)