From 598b9bba5aa95d0b5f78ab90ac491f742819047a Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Wed, 4 Sep 2024 01:03:08 +0200 Subject: [PATCH 1/3] rpc: Don't re-enable previously disabled network after dumptxoutset Also fixes a typo in the RPC help text. --- src/rpc/blockchain.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index ce03477fb21..e57e461b29a 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2698,7 +2698,7 @@ static RPCHelpMan dumptxoutset() "This call may take several minutes. Make sure to use no RPC timeout (bitcoin-cli -rpcclienttimeout=0)", { {"path", RPCArg::Type::STR, RPCArg::Optional::NO, "Path to the output file. If relative, will be prefixed by datadir."}, - {"type", RPCArg::Type::STR, RPCArg::Default(""), "The type of snapshot to create. Can be \"latest\" to create a snapshot of the current UTXO set or \"rollback\" to temporarily roll back the state of the node to a historical block before creating the snapshot of a historical UTXO set. This parameter can be omitted if a separate \"rollback\" named parameter is specified indicating the height or hash of a specific historical block. If \"rollback\" is specified and separate \"rollback\" named parameter is not specified, this will roll back to the latest valid snapshot block that currently be loaded with loadtxoutset."}, + {"type", RPCArg::Type::STR, RPCArg::Default(""), "The type of snapshot to create. Can be \"latest\" to create a snapshot of the current UTXO set or \"rollback\" to temporarily roll back the state of the node to a historical block before creating the snapshot of a historical UTXO set. This parameter can be omitted if a separate \"rollback\" named parameter is specified indicating the height or hash of a specific historical block. If \"rollback\" is specified and separate \"rollback\" named parameter is not specified, this will roll back to the latest valid snapshot block that can currently be loaded with loadtxoutset."}, {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "", { {"rollback", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, @@ -2790,7 +2790,12 @@ static RPCHelpMan dumptxoutset() // this so we don't punish peers that send us that send us data that // seems wrong in this temporary state. For example a normal new block // would be classified as a block connecting an invalid block. - disable_network = std::make_unique(connman); + // Skip if the network is already disabled because this + // 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(connman); + } invalidate_index = WITH_LOCK(::cs_main, return node.chainman->ActiveChain().Next(target_index)); InvalidateBlock(*node.chainman, invalidate_index->GetBlockHash()); From c5eaae3b89c67c664c24a53908bea608785e481d Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Wed, 4 Sep 2024 01:11:30 +0200 Subject: [PATCH 2/3] doc: Add -rpcclienttimeout=0 to loadtxoutset examples --- doc/assumeutxo.md | 2 +- src/rpc/blockchain.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/assumeutxo.md b/doc/assumeutxo.md index 4584eebf436..2b3f2bac471 100644 --- a/doc/assumeutxo.md +++ b/doc/assumeutxo.md @@ -17,7 +17,7 @@ Once you've obtained the snapshot, you can use the RPC command `loadtxoutset` to load it. ``` -$ bitcoin-cli loadtxoutset /path/to/input +$ bitcoin-cli -rpcclienttimeout=0 loadtxoutset /path/to/input ``` After the snapshot has loaded, the syncing process of both the snapshot chain diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index e57e461b29a..43d5dd46208 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -3005,7 +3005,7 @@ static RPCHelpMan loadtxoutset() } }, RPCExamples{ - HelpExampleCli("loadtxoutset", "utxo.dat") + HelpExampleCli("loadtxoutset -rpcclienttimeout=0", "utxo.dat") }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { From a3108a7c5692d137b70b8442b4741936277e89be Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Wed, 4 Sep 2024 01:53:07 +0200 Subject: [PATCH 3/3] rpc: Manage dumptxoutset rollback with RAII class --- src/rpc/blockchain.cpp | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 43d5dd46208..3ba89d08d98 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2683,6 +2683,23 @@ public: }; }; +/** + * RAII class that temporarily rolls back the local chain in it's constructor + * and rolls it forward again in it's destructor. + */ +class TemporaryRollback +{ + ChainstateManager& m_chainman; + const CBlockIndex& m_invalidate_index; +public: + TemporaryRollback(ChainstateManager& chainman, const CBlockIndex& index) : m_chainman(chainman), m_invalidate_index(index) { + InvalidateBlock(m_chainman, m_invalidate_index.GetBlockHash()); + }; + ~TemporaryRollback() { + ReconsiderBlock(m_chainman, m_invalidate_index.GetBlockHash()); + }; +}; + /** * Serialize the UTXO set to a file for loading elsewhere. * @@ -2770,6 +2787,7 @@ static RPCHelpMan dumptxoutset() CConnman& connman = EnsureConnman(node); const CBlockIndex* invalidate_index{nullptr}; std::unique_ptr disable_network; + std::unique_ptr temporary_rollback; // If the user wants to dump the txoutset of the current tip, we don't have // to roll back at all @@ -2798,7 +2816,7 @@ static RPCHelpMan dumptxoutset() } invalidate_index = WITH_LOCK(::cs_main, return node.chainman->ActiveChain().Next(target_index)); - InvalidateBlock(*node.chainman, invalidate_index->GetBlockHash()); + temporary_rollback = std::make_unique(*node.chainman, *invalidate_index); } Chainstate* chainstate; @@ -2822,23 +2840,15 @@ static RPCHelpMan dumptxoutset() // sister block of invalidate_index. This block (or a descendant) would // be activated as the new tip and we would not get to new_tip_index. if (target_index != chainstate->m_chain.Tip()) { - LogInfo("Failed to roll back to requested height, reverting to tip.\n"); - error = JSONRPCError(RPC_MISC_ERROR, "Could not roll back to requested height."); + LogWarning("dumptxoutset failed to roll back to requested height, reverting to tip.\n"); + throw JSONRPCError(RPC_MISC_ERROR, "Could not roll back to requested height."); } else { std::tie(cursor, stats, tip) = PrepareUTXOSnapshot(*chainstate, node.rpc_interruption_point); } } - if (error.isNull()) { - result = WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile, path, temppath, node.rpc_interruption_point); - fs::rename(temppath, path); - } - if (invalidate_index) { - ReconsiderBlock(*node.chainman, invalidate_index->GetBlockHash()); - } - if (!error.isNull()) { - throw error; - } + result = WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile, path, temppath, node.rpc_interruption_point); + fs::rename(temppath, path); result.pushKV("path", path.utf8string()); return result;