Skip to content

Commit

Permalink
Merge bitcoin#30808: rpc: dumptxoutset height parameter follow-ups (2…
Browse files Browse the repository at this point in the history
…9553)

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 bitcoin#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 bitcoin#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
  • Loading branch information
achow101 committed Sep 4, 2024
2 parents f66011e + a3108a7 commit b8d2f58
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doc/assumeutxo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 31 additions & 16 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,23 @@ class NetworkDisable
};
};

/**
* 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.
*
Expand All @@ -2698,7 +2715,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,
Expand Down Expand Up @@ -2770,6 +2787,7 @@ static RPCHelpMan dumptxoutset()
CConnman& connman = EnsureConnman(node);
const CBlockIndex* invalidate_index{nullptr};
std::unique_ptr<NetworkDisable> disable_network;
std::unique_ptr<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 @@ -2790,10 +2808,15 @@ 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<NetworkDisable>(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<NetworkDisable>(connman);
}

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

Chainstate* chainstate;
Expand All @@ -2817,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;
Expand Down Expand Up @@ -3000,7 +3015,7 @@ static RPCHelpMan loadtxoutset()
}
},
RPCExamples{
HelpExampleCli("loadtxoutset", "utxo.dat")
HelpExampleCli("loadtxoutset -rpcclienttimeout=0", "utxo.dat")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
Expand Down

0 comments on commit b8d2f58

Please sign in to comment.