aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorW. J. van der Laan <laanwj@protonmail.com>2021-12-02 10:13:28 +0100
committerW. J. van der Laan <laanwj@protonmail.com>2021-12-02 10:35:02 +0100
commit6acda4b00b3fc1bfac02f5de590e1a5386cbc779 (patch)
tree17baca1d43fc95b6ffc8ecb33f9ac4eca3618be4
parent282cc0cda7a29c8851a59cdbfadde4b635af04b2 (diff)
parentffd09281fe26446fcefa0627c220a52706e35227 (diff)
downloadbitcoin-6acda4b00b3fc1bfac02f5de590e1a5386cbc779.tar.xz
Merge bitcoin/bitcoin#23155: rpc: various fixups for dumptxoutset
ffd09281fe26446fcefa0627c220a52706e35227 rpc: various fixups for dumptxoutset (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11) (parent PR: #15606) --- A few fixes to make this RPC actually useful when generating snapshots. - Generate an assumeutxo hash and display it (sort of a bugfix) - Add nchaintx to output (necessary for use in chainparams entry) - Add path of serialized UTXO file to output ACKs for top commit: laanwj: Code review ACK ffd09281fe26446fcefa0627c220a52706e35227 Tree-SHA512: b0b5fd5138dea0e21258b1b18ab75bf3fd1628522cc1dbafa81af9cb9fa96562a1c39124fdb31057f256bfc560f462f907e9fe5e209b577b3f57afae2b7be826
-rw-r--r--src/rpc/blockchain.cpp26
-rw-r--r--src/rpc/blockchain.h8
-rw-r--r--src/test/util/chainstate.h3
-rwxr-xr-xtest/functional/rpc_dumptxoutset.py4
4 files changed, 35 insertions, 6 deletions
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index 55048f6811..bd11d76866 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -15,10 +15,12 @@
#include <core_io.h>
#include <deploymentinfo.h>
#include <deploymentstatus.h>
+#include <fs.h>
#include <hash.h>
#include <index/blockfilterindex.h>
#include <index/coinstatsindex.h>
#include <node/blockstorage.h>
+#include <logging/timer.h>
#include <node/coinstats.h>
#include <node/context.h>
#include <node/utxo_snapshot.h>
@@ -2556,6 +2558,8 @@ static RPCHelpMan dumptxoutset()
{RPCResult::Type::STR_HEX, "base_hash", "the hash of the base of the snapshot"},
{RPCResult::Type::NUM, "base_height", "the height of the base of the snapshot"},
{RPCResult::Type::STR, "path", "the absolute path that the snapshot was written to"},
+ {RPCResult::Type::STR_HEX, "txoutset_hash", "the hash of the UTXO set contents"},
+ {RPCResult::Type::NUM, "nchaintx", "the number of transactions in the chain up to and including the base block"},
}
},
RPCExamples{
@@ -2578,7 +2582,8 @@ static RPCHelpMan dumptxoutset()
FILE* file{fsbridge::fopen(temppath, "wb")};
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
NodeContext& node = EnsureAnyNodeContext(request.context);
- UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), afile);
+ UniValue result = CreateUTXOSnapshot(
+ node, node.chainman->ActiveChainstate(), afile, path, temppath);
fs::rename(temppath, path);
result.pushKV("path", path.u8string());
@@ -2587,10 +2592,15 @@ static RPCHelpMan dumptxoutset()
};
}
-UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile)
+UniValue CreateUTXOSnapshot(
+ NodeContext& node,
+ CChainState& chainstate,
+ CAutoFile& afile,
+ const fs::path& path,
+ const fs::path& temppath)
{
std::unique_ptr<CCoinsViewCursor> pcursor;
- CCoinsStats stats{CoinStatsHashType::NONE};
+ CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
CBlockIndex* tip;
{
@@ -2619,6 +2629,10 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
CHECK_NONFATAL(tip);
}
+ LOG_TIME_SECONDS(strprintf("writing UTXO snapshot at height %s (%s) to file %s (via %s)",
+ tip->nHeight, tip->GetBlockHash().ToString(),
+ fs::PathToString(path), fs::PathToString(temppath)));
+
SnapshotMetadata metadata{tip->GetBlockHash(), stats.coins_count, tip->nChainTx};
afile << metadata;
@@ -2644,7 +2658,11 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
result.pushKV("coins_written", stats.coins_count);
result.pushKV("base_hash", tip->GetBlockHash().ToString());
result.pushKV("base_height", tip->nHeight);
-
+ result.pushKV("path", path.u8string());
+ result.pushKV("txoutset_hash", stats.hashSerialized.ToString());
+ // Cast required because univalue doesn't have serialization specified for
+ // `unsigned int`, nChainTx's type.
+ result.pushKV("nchaintx", uint64_t{tip->nChainTx});
return result;
}
diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h
index 65f6b1429e..0ad68a7fd6 100644
--- a/src/rpc/blockchain.h
+++ b/src/rpc/blockchain.h
@@ -7,6 +7,7 @@
#include <consensus/amount.h>
#include <core_io.h>
+#include <fs.h>
#include <streams.h>
#include <sync.h>
@@ -65,6 +66,11 @@ CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
* Helper to create UTXO snapshots given a chainstate and a file handle.
* @return a UniValue map containing metadata about the snapshot.
*/
-UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile);
+UniValue CreateUTXOSnapshot(
+ NodeContext& node,
+ CChainState& chainstate,
+ CAutoFile& afile,
+ const fs::path& path,
+ const fs::path& tmppath);
#endif // BITCOIN_RPC_BLOCKCHAIN_H
diff --git a/src/test/util/chainstate.h b/src/test/util/chainstate.h
index 555979d94e..a9092bd0ef 100644
--- a/src/test/util/chainstate.h
+++ b/src/test/util/chainstate.h
@@ -34,7 +34,8 @@ CreateAndActivateUTXOSnapshot(NodeContext& node, const fs::path root, F malleati
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
- UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), auto_outfile);
+ UniValue result = CreateUTXOSnapshot(
+ node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
BOOST_TEST_MESSAGE(
"Wrote UTXO snapshot to " << fs::PathToString(snapshot_path.make_preferred()) << ": " << result.write());
diff --git a/test/functional/rpc_dumptxoutset.py b/test/functional/rpc_dumptxoutset.py
index f54f600839..1721b6ffe8 100755
--- a/test/functional/rpc_dumptxoutset.py
+++ b/test/functional/rpc_dumptxoutset.py
@@ -45,6 +45,10 @@ class DumptxoutsetTest(BitcoinTestFramework):
assert_equal(
digest, '7ae82c986fa5445678d2a21453bb1c86d39e47af13da137640c2b1cf8093691c')
+ assert_equal(
+ out['txoutset_hash'], 'd4b614f476b99a6e569973bf1c0120d88b1a168076f8ce25691fb41dd1cef149')
+ assert_equal(out['nchaintx'], 101)
+
# Specifying a path to an existing file will fail.
assert_raises_rpc_error(
-8, '{} already exists'.format(FILENAME), node.dumptxoutset, FILENAME)