Skip to content

Commit

Permalink
Merge bitcoin#30817: test: Add coverage for dumptxoutset failure robu…
Browse files Browse the repository at this point in the history
…stness

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#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#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
  • Loading branch information
achow101 committed Sep 9, 2024
2 parents fb52023 + c2b779d commit 712a2b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <condition_variable>
#include <memory>
#include <mutex>
#include <optional>

using kernel::CCoinsStats;
using kernel::CoinStatsHashType;
Expand Down Expand Up @@ -2786,8 +2787,8 @@ static RPCHelpMan dumptxoutset()

CConnman& connman = EnsureConnman(node);
const CBlockIndex* invalidate_index{nullptr};
std::unique_ptr<NetworkDisable> disable_network;
std::unique_ptr<TemporaryRollback> temporary_rollback;
std::optional<NetworkDisable> disable_network;
std::optional<TemporaryRollback> temporary_rollback;

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

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

Chainstate* chainstate;
std::unique_ptr<CCoinsViewCursor> cursor;
CCoinsStats stats;
UniValue result;
UniValue error;
{
// Lock the chainstate before calling PrepareUtxoSnapshot, to be able
// to get a UTXO database cursor while the chain is pointing at the
Expand All @@ -2847,7 +2846,7 @@ static RPCHelpMan dumptxoutset()
}
}

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

result.pushKV("path", path.utf8string());
Expand Down
12 changes: 11 additions & 1 deletion test/functional/rpc_dumptxoutset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def run_test(self):

FILENAME = 'txoutset.dat'
out = node.dumptxoutset(FILENAME, "latest")
expected_path = node.datadir_path / self.chain / FILENAME
expected_path = node.chain_path / FILENAME

assert expected_path.is_file()

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

self.log.info(f"Test that dumptxoutset failure does not leave the network activity suspended")
rev_file = node.blocks_path / "rev00000.dat"
bogus_file = node.blocks_path / "bogus.dat"
rev_file.rename(bogus_file)
assert_raises_rpc_error(
-1, 'Could not roll back to requested height.', node.dumptxoutset, 'utxos.dat', rollback=99)
assert_equal(node.getnetworkinfo()['networkactive'], True)

# Cleanup
bogus_file.rename(rev_file)

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

0 comments on commit 712a2b5

Please sign in to comment.