Skip to content

Commit b8d2f58

Browse files
committed
Merge bitcoin/bitcoin#30808: rpc: dumptxoutset height parameter follow-ups (29553)
a3108a7 rpc: Manage dumptxoutset rollback with RAII class (Fabian Jahr) c5eaae3 doc: Add -rpcclienttimeout=0 to loadtxoutset examples (Fabian Jahr) 598b9bb rpc: Don't re-enable previously disabled network after dumptxoutset (Fabian Jahr) Pull request description: First, this addresses two left-over comments in #29553: - When running `dumptxoutset` the network gets disabled in the beginning and then re-enabled at the end. The network would be re-enabled even if the user had already disabled the network themself before running `dumptxoutset`. The network is now not re-enabled anymore since that might not be what the user wants. - The `-rpcclienttimeout=0` option is added to `loadtxoutset` examples in documentation Additionally, pablomartin4btc notified me that he found his node stuck at the invalidated height after some late testing after #29553 was merged. We could not find the actual source of the issue since his logs got lost. However, it seems likely that some kind of disruption stopped the process before the node could roll forward again. We fixed this issue for network disablement with a RAII class previously and it seems logical that this can happen the same way for the rollback part so I suggest to also fix it the same way. An example to reproduce the issue described above as I think it happened: Remove the `!` in the following line in `PrepareUTXOSnapshot()` to simulate an issue occurring during `GetUTXOStats()`. ``` if (!maybe_stats) { ``` This leaves the node in the following state on master: ``` $ build/src/bitcoin-cli -rpcclienttimeout=0 -named dumptxoutset utxo-859750.dat rollback=859750 error code: -32603 error message: Unable to read UTXO set $ build/src/bitcoin-cli getchaintips [ { "height": 859762, "hash": "00000000000000000002ec7a0fcca3aeca5b35545b52eb925766670aacc704ad", "branchlen": 12, "status": "headers-only" }, { "height": 859750, "hash": "0000000000000000000010897b6b88a18f9478050200d8d048013c58bfd6229e", "branchlen": 0, "status": "active" }, ``` (Note that the first tip is `headers-only` and not `invalid` only because I started `dumptxoutset` before my node had fully synced to the tip. pablomartin4btc saw it as `invalid`.) ACKs for top commit: maflcko: re-ACK a3108a7 🐸 achow101: ACK a3108a7 pablomartin4btc: cr ACK a3108a7 Tree-SHA512: d2ab32f62de2253312e27d7d753ec0995da3fe7a22ffc3d6c7cfa3b68a4a144c59210aa82b7a704c2a29c3b2aad6ea74972e3e8bb979ee4b7082a20f4bfddc9c
2 parents f66011e + a3108a7 commit b8d2f58

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

doc/assumeutxo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Once you've obtained the snapshot, you can use the RPC command `loadtxoutset` to
1717
load it.
1818

1919
```
20-
$ bitcoin-cli loadtxoutset /path/to/input
20+
$ bitcoin-cli -rpcclienttimeout=0 loadtxoutset /path/to/input
2121
```
2222

2323
After the snapshot has loaded, the syncing process of both the snapshot chain

src/rpc/blockchain.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,23 @@ class NetworkDisable
26832683
};
26842684
};
26852685

2686+
/**
2687+
* RAII class that temporarily rolls back the local chain in it's constructor
2688+
* and rolls it forward again in it's destructor.
2689+
*/
2690+
class TemporaryRollback
2691+
{
2692+
ChainstateManager& m_chainman;
2693+
const CBlockIndex& m_invalidate_index;
2694+
public:
2695+
TemporaryRollback(ChainstateManager& chainman, const CBlockIndex& index) : m_chainman(chainman), m_invalidate_index(index) {
2696+
InvalidateBlock(m_chainman, m_invalidate_index.GetBlockHash());
2697+
};
2698+
~TemporaryRollback() {
2699+
ReconsiderBlock(m_chainman, m_invalidate_index.GetBlockHash());
2700+
};
2701+
};
2702+
26862703
/**
26872704
* Serialize the UTXO set to a file for loading elsewhere.
26882705
*
@@ -2698,7 +2715,7 @@ static RPCHelpMan dumptxoutset()
26982715
"This call may take several minutes. Make sure to use no RPC timeout (bitcoin-cli -rpcclienttimeout=0)",
26992716
{
27002717
{"path", RPCArg::Type::STR, RPCArg::Optional::NO, "Path to the output file. If relative, will be prefixed by datadir."},
2701-
{"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."},
2718+
{"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."},
27022719
{"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
27032720
{
27042721
{"rollback", RPCArg::Type::NUM, RPCArg::Optional::OMITTED,
@@ -2770,6 +2787,7 @@ static RPCHelpMan dumptxoutset()
27702787
CConnman& connman = EnsureConnman(node);
27712788
const CBlockIndex* invalidate_index{nullptr};
27722789
std::unique_ptr<NetworkDisable> disable_network;
2790+
std::unique_ptr<TemporaryRollback> temporary_rollback;
27732791

27742792
// If the user wants to dump the txoutset of the current tip, we don't have
27752793
// to roll back at all
@@ -2790,10 +2808,15 @@ static RPCHelpMan dumptxoutset()
27902808
// this so we don't punish peers that send us that send us data that
27912809
// seems wrong in this temporary state. For example a normal new block
27922810
// would be classified as a block connecting an invalid block.
2793-
disable_network = std::make_unique<NetworkDisable>(connman);
2811+
// Skip if the network is already disabled because this
2812+
// automatically re-enables the network activity at the end of the
2813+
// process which may not be what the user wants.
2814+
if (connman.GetNetworkActive()) {
2815+
disable_network = std::make_unique<NetworkDisable>(connman);
2816+
}
27942817

27952818
invalidate_index = WITH_LOCK(::cs_main, return node.chainman->ActiveChain().Next(target_index));
2796-
InvalidateBlock(*node.chainman, invalidate_index->GetBlockHash());
2819+
temporary_rollback = std::make_unique<TemporaryRollback>(*node.chainman, *invalidate_index);
27972820
}
27982821

27992822
Chainstate* chainstate;
@@ -2817,23 +2840,15 @@ static RPCHelpMan dumptxoutset()
28172840
// sister block of invalidate_index. This block (or a descendant) would
28182841
// be activated as the new tip and we would not get to new_tip_index.
28192842
if (target_index != chainstate->m_chain.Tip()) {
2820-
LogInfo("Failed to roll back to requested height, reverting to tip.\n");
2821-
error = JSONRPCError(RPC_MISC_ERROR, "Could not roll back to requested height.");
2843+
LogWarning("dumptxoutset failed to roll back to requested height, reverting to tip.\n");
2844+
throw JSONRPCError(RPC_MISC_ERROR, "Could not roll back to requested height.");
28222845
} else {
28232846
std::tie(cursor, stats, tip) = PrepareUTXOSnapshot(*chainstate, node.rpc_interruption_point);
28242847
}
28252848
}
28262849

2827-
if (error.isNull()) {
2828-
result = WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile, path, temppath, node.rpc_interruption_point);
2829-
fs::rename(temppath, path);
2830-
}
2831-
if (invalidate_index) {
2832-
ReconsiderBlock(*node.chainman, invalidate_index->GetBlockHash());
2833-
}
2834-
if (!error.isNull()) {
2835-
throw error;
2836-
}
2850+
result = WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile, path, temppath, node.rpc_interruption_point);
2851+
fs::rename(temppath, path);
28372852

28382853
result.pushKV("path", path.utf8string());
28392854
return result;
@@ -3000,7 +3015,7 @@ static RPCHelpMan loadtxoutset()
30003015
}
30013016
},
30023017
RPCExamples{
3003-
HelpExampleCli("loadtxoutset", "utxo.dat")
3018+
HelpExampleCli("loadtxoutset -rpcclienttimeout=0", "utxo.dat")
30043019
},
30053020
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
30063021
{

0 commit comments

Comments
 (0)