Skip to content

Commit 4741ca5

Browse files
committed
Merge #13020: Consistently log CValidationState on call failure
e4d0b44 Consistently log CValidationState on failure (Ben Woosley) Pull request description: This replaces potential silent failures and partial logging with full logging. Seems providing at least minimal visibility to the failure is a good practice. E.g. `FlushStateToDisk` can return a rare but meaningful out of disk space error that would be better to note than leave out. Note many of these are related to `ActivateBestChain` or `FlushStateToDisk`. Only a few cases of ignored state remain, e.g. LoadExternalBlockFile and RelayWalletTransaction, where I expect logging would likely be spammy. Tree-SHA512: fb0e521039e5a5250cd9c82e7a8676423b5e3899d495649c0e71752059d1984e5175f556386ade048f51a7d59f5c8e467df7fe91d746076f97d24c000ccf7891
2 parents 8b4081a + e4d0b44 commit 4741ca5

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
@@ -2206,14 +2206,18 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
22062206
void FlushStateToDisk() {
22072207
CValidationState state;
22082208
const CChainParams& chainparams = Params();
2209-
FlushStateToDisk(chainparams, state, FlushStateMode::ALWAYS);
2209+
if (!FlushStateToDisk(chainparams, state, FlushStateMode::ALWAYS)) {
2210+
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
2211+
}
22102212
}
22112213

22122214
void PruneAndFlush() {
22132215
CValidationState state;
22142216
fCheckForPruning = true;
22152217
const CChainParams& chainparams = Params();
2216-
FlushStateToDisk(chainparams, state, FlushStateMode::NONE);
2218+
if (!FlushStateToDisk(chainparams, state, FlushStateMode::NONE)) {
2219+
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
2220+
}
22172221
}
22182222

22192223
static void DoWarning(const std::string& strWarning)
@@ -3520,15 +3524,15 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons
35203524
}
35213525
if (!ret) {
35223526
GetMainSignals().BlockChecked(*pblock, state);
3523-
return error("%s: AcceptBlock FAILED (%s)", __func__, state.GetDebugMessage());
3527+
return error("%s: AcceptBlock FAILED (%s)", __func__, FormatStateMessage(state));
35243528
}
35253529
}
35263530

35273531
NotifyHeaderTip();
35283532

35293533
CValidationState state; // Only used to report errors, not invalidity - ignore it
35303534
if (!g_chainstate.ActivateBestChain(state, chainparams, pblock))
3531-
return error("%s: ActivateBestChain failed", __func__);
3535+
return error("%s: ActivateBestChain failed (%s)", __func__, FormatStateMessage(state));
35323536

35333537
return true;
35343538
}
@@ -3646,7 +3650,9 @@ void PruneBlockFilesManual(int nManualPruneHeight)
36463650
{
36473651
CValidationState state;
36483652
const CChainParams& chainparams = Params();
3649-
FlushStateToDisk(chainparams, state, FlushStateMode::NONE, nManualPruneHeight);
3653+
if (!FlushStateToDisk(chainparams, state, FlushStateMode::NONE, nManualPruneHeight)) {
3654+
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
3655+
}
36503656
}
36513657

36523658
/**
@@ -3900,6 +3906,7 @@ bool LoadChainTip(const CChainParams& chainparams)
39003906
LogPrintf("%s: Connecting genesis block...\n", __func__);
39013907
CValidationState state;
39023908
if (!ActivateBestChain(state, chainparams)) {
3909+
LogPrintf("%s: failed to activate chain (%s)\n", __func__, FormatStateMessage(state));
39033910
return false;
39043911
}
39053912
}
@@ -4014,7 +4021,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
40144021
if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
40154022
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
40164023
if (!g_chainstate.ConnectBlock(block, state, pindex, coins, chainparams))
4017-
return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4024+
return error("VerifyDB(): *** found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
40184025
}
40194026
}
40204027

@@ -4144,11 +4151,13 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
41444151
break;
41454152
}
41464153
if (!DisconnectTip(state, params, nullptr)) {
4147-
return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
4154+
return error("RewindBlockIndex: unable to disconnect block at height %i (%s)", pindex->nHeight, FormatStateMessage(state));
41484155
}
41494156
// Occasionally flush state to disk.
4150-
if (!FlushStateToDisk(params, state, FlushStateMode::PERIODIC))
4157+
if (!FlushStateToDisk(params, state, FlushStateMode::PERIODIC)) {
4158+
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", FormatStateMessage(state));
41514159
return false;
4160+
}
41524161
}
41534162

41544163
// Reduce validity flag and have-data flags.
@@ -4214,6 +4223,7 @@ bool RewindBlockIndex(const CChainParams& params) {
42144223
// it'll get called a bunch real soon.
42154224
CValidationState state;
42164225
if (!FlushStateToDisk(params, state, FlushStateMode::ALWAYS)) {
4226+
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", FormatStateMessage(state));
42174227
return false;
42184228
}
42194229
}
@@ -4300,7 +4310,7 @@ bool CChainState::LoadGenesisBlock(const CChainParams& chainparams)
43004310
CBlockIndex *pindex = AddToBlockIndex(block);
43014311
CValidationState state;
43024312
if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
4303-
return error("%s: genesis block not accepted", __func__);
4313+
return error("%s: genesis block not accepted (%s)", __func__, FormatStateMessage(state));
43044314
} catch (const std::runtime_error& e) {
43054315
return error("%s: failed to write genesis block: %s", __func__, e.what());
43064316
}

0 commit comments

Comments
 (0)