Skip to content

Commit d5a54ce

Browse files
committed
Merge #15305: [validation] Crash if disconnecting a block fails
a47df13 [qa] Test disconnect block failure -> shutdown (Suhas Daftuar) 4433ed0 [validation] Crash if disconnecting a block fails (Suhas Daftuar) Pull request description: If we're unable to disconnect a block during normal operation, then that is a failure of our local system (such as disk failure) or the chain that we are on (eg CVE-2018-17144), but cannot be due to failure of the (more work) chain that we're trying to validate. We should abort rather than stay on a less work chain. Fixes #14341. ACKs for top commit: practicalswift: utACK a47df13 TheBlueMatt: utACK a47df13. Didn't bother to review the test in detail, it looked fine. Debated whether invalidateblock should ever crash the node, but *not* crashing in the case of hitting a pruned block (which is the only change here) is clearly better, even if there are other cases I'd argue we should crash in. ryanofsky: utACK a47df13. Only change since last review is new comment. promag: ACK a47df13, it takes awhile to quit (RPC connection timeouts) but that's unrelated - hope to fix that soon. fanquake: ACK a47df13 Tree-SHA512: 4dec8cef6e7dbbe513c138fc5821a7ceab855e603ece3c16185b51a3830ab7ebbc844a28827bf64e75326f45325991dcb672f13bd7baede53304f27289c4af8d
2 parents d960d5c + a47df13 commit d5a54ce

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/validation.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,7 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha
21852185
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
21862186
CBlock& block = *pblock;
21872187
if (!ReadBlockFromDisk(block, pindexDelete, chainparams.GetConsensus()))
2188-
return AbortNode(state, "Failed to read block");
2188+
return error("DisconnectTip(): Failed to read block");
21892189
// Apply the block atomically to the chain state.
21902190
int64_t nStart = GetTimeMicros();
21912191
{
@@ -2442,6 +2442,11 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
24422442
// This is likely a fatal error, but keep the mempool consistent,
24432443
// just in case. Only remove from the mempool in this case.
24442444
UpdateMempoolForReorg(disconnectpool, false);
2445+
2446+
// If we're unable to disconnect a block during normal operation,
2447+
// then that is a failure of our local system -- we should abort
2448+
// rather than stay on a less work chain.
2449+
AbortNode(state, "Failed to disconnect block; see debug.log for details");
24452450
return false;
24462451
}
24472452
fBlocksDisconnected = true;

test/functional/feature_abortnode.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2019 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test bitcoind aborts if can't disconnect a block.
6+
7+
- Start a single node and generate 3 blocks.
8+
- Delete the undo data.
9+
- Mine a fork that requires disconnecting the tip.
10+
- Verify that bitcoind AbortNode's.
11+
"""
12+
13+
from test_framework.test_framework import BitcoinTestFramework
14+
from test_framework.util import wait_until, get_datadir_path, connect_nodes
15+
import os
16+
17+
class AbortNodeTest(BitcoinTestFramework):
18+
19+
def set_test_params(self):
20+
self.setup_clean_chain = True
21+
self.num_nodes = 2
22+
23+
def setup_network(self):
24+
self.setup_nodes()
25+
# We'll connect the nodes later
26+
27+
def run_test(self):
28+
self.nodes[0].generate(3)
29+
datadir = get_datadir_path(self.options.tmpdir, 0)
30+
31+
# Deleting the undo file will result in reorg failure
32+
os.unlink(os.path.join(datadir, 'regtest', 'blocks', 'rev00000.dat'))
33+
34+
# Connecting to a node with a more work chain will trigger a reorg
35+
# attempt.
36+
self.nodes[1].generate(3)
37+
with self.nodes[0].assert_debug_log(["Failed to disconnect block"]):
38+
connect_nodes(self.nodes[0], 1)
39+
self.nodes[1].generate(1)
40+
41+
# Check that node0 aborted
42+
self.log.info("Waiting for crash")
43+
wait_until(lambda: self.nodes[0].is_node_stopped(), timeout=60)
44+
self.log.info("Node crashed - now verifying restart fails")
45+
self.nodes[0].assert_start_raises_init_error()
46+
47+
if __name__ == '__main__':
48+
AbortNodeTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
'feature_bip68_sequence.py',
108108
'p2p_feefilter.py',
109109
'feature_reindex.py',
110+
'feature_abortnode.py',
110111
# vv Tests less than 30s vv
111112
'wallet_keypool_topup.py',
112113
'interface_zmq.py',

0 commit comments

Comments
 (0)