Skip to content

Commit 712a2b5

Browse files
committed
Merge bitcoin/bitcoin#30817: test: Add coverage for dumptxoutset failure robustness
c2b779d refactor: Manage dumptxoutset RAII classes with std::optional (Fabian Jahr) 4b5bf33 test: Add coverage for failing dumptxoutset behavior (Fabian Jahr) Pull request description: This adds a test that checks that network activity is not suspended if dumptxoutset fails in the middle of its process which is implemented with the `NetworkDisable` RAII class. I would have liked to add coverage for the `TemporaryRollback` RAII class but that seems a lot more tricky since the failure needs to happen at some point after the rollback and on the scale of our test chain here I couldn't find a way to do it yet. This was requested by pablomartin4btc here: bitcoin/bitcoin#30808 (review). To test the test you can comment out the content of the destructor of `NetworkDisable`. It also addresses the feedback by ryanofsky to use `std::optional` instead of `std::unique_ptr` for the management of the RAII object: bitcoin/bitcoin#30808 (comment) ACKs for top commit: achow101: ACK c2b779d pablomartin4btc: cr & tACK c2b779d tdb3: ACK c2b779d BrandonOdiwuor: Code Review ACK c2b779d theStack: ACK c2b779d Tree-SHA512: 9556e75014a2599bb870b70faf887608b332f2312626333f771d4ec11c04f863a2cf17e223ec473d4e8b0c9e8008394a4e0c321561f7ef3a2eec713dcfaea58a
2 parents fb52023 + c2b779d commit 712a2b5

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/rpc/blockchain.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include <condition_variable>
5757
#include <memory>
5858
#include <mutex>
59+
#include <optional>
5960

6061
using kernel::CCoinsStats;
6162
using kernel::CoinStatsHashType;
@@ -2786,8 +2787,8 @@ static RPCHelpMan dumptxoutset()
27862787

27872788
CConnman& connman = EnsureConnman(node);
27882789
const CBlockIndex* invalidate_index{nullptr};
2789-
std::unique_ptr<NetworkDisable> disable_network;
2790-
std::unique_ptr<TemporaryRollback> temporary_rollback;
2790+
std::optional<NetworkDisable> disable_network;
2791+
std::optional<TemporaryRollback> temporary_rollback;
27912792

27922793
// If the user wants to dump the txoutset of the current tip, we don't have
27932794
// to roll back at all
@@ -2812,18 +2813,16 @@ static RPCHelpMan dumptxoutset()
28122813
// automatically re-enables the network activity at the end of the
28132814
// process which may not be what the user wants.
28142815
if (connman.GetNetworkActive()) {
2815-
disable_network = std::make_unique<NetworkDisable>(connman);
2816+
disable_network.emplace(connman);
28162817
}
28172818

28182819
invalidate_index = WITH_LOCK(::cs_main, return node.chainman->ActiveChain().Next(target_index));
2819-
temporary_rollback = std::make_unique<TemporaryRollback>(*node.chainman, *invalidate_index);
2820+
temporary_rollback.emplace(*node.chainman, *invalidate_index);
28202821
}
28212822

28222823
Chainstate* chainstate;
28232824
std::unique_ptr<CCoinsViewCursor> cursor;
28242825
CCoinsStats stats;
2825-
UniValue result;
2826-
UniValue error;
28272826
{
28282827
// Lock the chainstate before calling PrepareUtxoSnapshot, to be able
28292828
// to get a UTXO database cursor while the chain is pointing at the
@@ -2847,7 +2846,7 @@ static RPCHelpMan dumptxoutset()
28472846
}
28482847
}
28492848

2850-
result = WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile, path, temppath, node.rpc_interruption_point);
2849+
UniValue result = WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile, path, temppath, node.rpc_interruption_point);
28512850
fs::rename(temppath, path);
28522851

28532852
result.pushKV("path", path.utf8string());

test/functional/rpc_dumptxoutset.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def run_test(self):
2828

2929
FILENAME = 'txoutset.dat'
3030
out = node.dumptxoutset(FILENAME, "latest")
31-
expected_path = node.datadir_path / self.chain / FILENAME
31+
expected_path = node.chain_path / FILENAME
3232

3333
assert expected_path.is_file()
3434

@@ -60,6 +60,16 @@ def run_test(self):
6060
assert_raises_rpc_error(
6161
-8, 'Invalid snapshot type "bogus" specified. Please specify "rollback" or "latest"', node.dumptxoutset, 'utxos.dat', "bogus")
6262

63+
self.log.info(f"Test that dumptxoutset failure does not leave the network activity suspended")
64+
rev_file = node.blocks_path / "rev00000.dat"
65+
bogus_file = node.blocks_path / "bogus.dat"
66+
rev_file.rename(bogus_file)
67+
assert_raises_rpc_error(
68+
-1, 'Could not roll back to requested height.', node.dumptxoutset, 'utxos.dat', rollback=99)
69+
assert_equal(node.getnetworkinfo()['networkactive'], True)
70+
71+
# Cleanup
72+
bogus_file.rename(rev_file)
6373

6474
if __name__ == '__main__':
6575
DumptxoutsetTest(__file__).main()

0 commit comments

Comments
 (0)