Skip to content

Commit e4d0b44

Browse files
committed
Consistently log CValidationState on failure
Seems providing at least minimal visibility to the failure is a good practice. The only remaining ignored state is in LoadExternalBlockFile, where logging would likely be spammy.
1 parent 615f7c2 commit e4d0b44

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
684684
// scan for better chains in the block chain database, that are not yet connected in the active best chain
685685
CValidationState state;
686686
if (!ActivateBestChain(state, chainparams)) {
687-
LogPrintf("Failed to connect best block\n");
687+
LogPrintf("Failed to connect best block (%s)\n", FormatStateMessage(state));
688688
StartShutdown();
689689
return;
690690
}

src/net_processing.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,8 +1100,10 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
11001100
}
11011101
} // release cs_main before calling ActivateBestChain
11021102
if (need_activate_chain) {
1103-
CValidationState dummy;
1104-
ActivateBestChain(dummy, Params(), a_recent_block);
1103+
CValidationState state;
1104+
if (!ActivateBestChain(state, Params(), a_recent_block)) {
1105+
LogPrint(BCLog::NET, "failed to activate chain (%s)\n", FormatStateMessage(state));
1106+
}
11051107
}
11061108

11071109
LOCK(cs_main);
@@ -1992,8 +1994,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19921994
LOCK(cs_most_recent_block);
19931995
a_recent_block = most_recent_block;
19941996
}
1995-
CValidationState dummy;
1996-
ActivateBestChain(dummy, Params(), a_recent_block);
1997+
CValidationState state;
1998+
if (!ActivateBestChain(state, Params(), a_recent_block)) {
1999+
LogPrint(BCLog::NET, "failed to activate chain (%s)\n", FormatStateMessage(state));
2000+
}
19972001
}
19982002

19992003
LOCK(cs_main);

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
8585
{
8686
CValidationState state;
8787
if (!ActivateBestChain(state, chainparams)) {
88-
throw std::runtime_error("ActivateBestChain failed.");
88+
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", FormatStateMessage(state)));
8989
}
9090
}
9191
nScriptCheckThreads = 3;

src/validation.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,14 +2181,18 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
21812181
void FlushStateToDisk() {
21822182
CValidationState state;
21832183
const CChainParams& chainparams = Params();
2184-
FlushStateToDisk(chainparams, state, FlushStateMode::ALWAYS);
2184+
if (!FlushStateToDisk(chainparams, state, FlushStateMode::ALWAYS)) {
2185+
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
2186+
}
21852187
}
21862188

21872189
void PruneAndFlush() {
21882190
CValidationState state;
21892191
fCheckForPruning = true;
21902192
const CChainParams& chainparams = Params();
2191-
FlushStateToDisk(chainparams, state, FlushStateMode::NONE);
2193+
if (!FlushStateToDisk(chainparams, state, FlushStateMode::NONE)) {
2194+
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
2195+
}
21922196
}
21932197

21942198
static void DoWarning(const std::string& strWarning)
@@ -3489,15 +3493,15 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons
34893493
}
34903494
if (!ret) {
34913495
GetMainSignals().BlockChecked(*pblock, state);
3492-
return error("%s: AcceptBlock FAILED (%s)", __func__, state.GetDebugMessage());
3496+
return error("%s: AcceptBlock FAILED (%s)", __func__, FormatStateMessage(state));
34933497
}
34943498
}
34953499

34963500
NotifyHeaderTip();
34973501

34983502
CValidationState state; // Only used to report errors, not invalidity - ignore it
34993503
if (!g_chainstate.ActivateBestChain(state, chainparams, pblock))
3500-
return error("%s: ActivateBestChain failed", __func__);
3504+
return error("%s: ActivateBestChain failed (%s)", __func__, FormatStateMessage(state));
35013505

35023506
return true;
35033507
}
@@ -3615,7 +3619,9 @@ void PruneBlockFilesManual(int nManualPruneHeight)
36153619
{
36163620
CValidationState state;
36173621
const CChainParams& chainparams = Params();
3618-
FlushStateToDisk(chainparams, state, FlushStateMode::NONE, nManualPruneHeight);
3622+
if (!FlushStateToDisk(chainparams, state, FlushStateMode::NONE, nManualPruneHeight)) {
3623+
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
3624+
}
36193625
}
36203626

36213627
/**
@@ -3869,6 +3875,7 @@ bool LoadChainTip(const CChainParams& chainparams)
38693875
LogPrintf("%s: Connecting genesis block...\n", __func__);
38703876
CValidationState state;
38713877
if (!ActivateBestChain(state, chainparams)) {
3878+
LogPrintf("%s: failed to activate chain (%s)\n", __func__, FormatStateMessage(state));
38723879
return false;
38733880
}
38743881
}
@@ -3983,7 +3990,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
39833990
if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
39843991
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
39853992
if (!g_chainstate.ConnectBlock(block, state, pindex, coins, chainparams))
3986-
return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3993+
return error("VerifyDB(): *** found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
39873994
}
39883995
}
39893996

@@ -4110,11 +4117,13 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
41104117
break;
41114118
}
41124119
if (!DisconnectTip(state, params, nullptr)) {
4113-
return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
4120+
return error("RewindBlockIndex: unable to disconnect block at height %i (%s)", pindex->nHeight, FormatStateMessage(state));
41144121
}
41154122
// Occasionally flush state to disk.
4116-
if (!FlushStateToDisk(params, state, FlushStateMode::PERIODIC))
4123+
if (!FlushStateToDisk(params, state, FlushStateMode::PERIODIC)) {
4124+
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", FormatStateMessage(state));
41174125
return false;
4126+
}
41184127
}
41194128

41204129
// Reduce validity flag and have-data flags.
@@ -4180,6 +4189,7 @@ bool RewindBlockIndex(const CChainParams& params) {
41804189
// it'll get called a bunch real soon.
41814190
CValidationState state;
41824191
if (!FlushStateToDisk(params, state, FlushStateMode::ALWAYS)) {
4192+
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", FormatStateMessage(state));
41834193
return false;
41844194
}
41854195
}
@@ -4266,7 +4276,7 @@ bool CChainState::LoadGenesisBlock(const CChainParams& chainparams)
42664276
CBlockIndex *pindex = AddToBlockIndex(block);
42674277
CValidationState state;
42684278
if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
4269-
return error("%s: genesis block not accepted", __func__);
4279+
return error("%s: genesis block not accepted (%s)", __func__, FormatStateMessage(state));
42704280
} catch (const std::runtime_error& e) {
42714281
return error("%s: failed to write genesis block: %s", __func__, e.what());
42724282
}

0 commit comments

Comments
 (0)