Skip to content

Commit 6acda4b

Browse files
committed
Merge bitcoin/bitcoin#23155: rpc: various fixups for dumptxoutset
ffd0928 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 ffd0928 Tree-SHA512: b0b5fd5138dea0e21258b1b18ab75bf3fd1628522cc1dbafa81af9cb9fa96562a1c39124fdb31057f256bfc560f462f907e9fe5e209b577b3f57afae2b7be826
2 parents 282cc0c + ffd0928 commit 6acda4b

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/rpc/blockchain.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
#include <core_io.h>
1616
#include <deploymentinfo.h>
1717
#include <deploymentstatus.h>
18+
#include <fs.h>
1819
#include <hash.h>
1920
#include <index/blockfilterindex.h>
2021
#include <index/coinstatsindex.h>
2122
#include <node/blockstorage.h>
23+
#include <logging/timer.h>
2224
#include <node/coinstats.h>
2325
#include <node/context.h>
2426
#include <node/utxo_snapshot.h>
@@ -2556,6 +2558,8 @@ static RPCHelpMan dumptxoutset()
25562558
{RPCResult::Type::STR_HEX, "base_hash", "the hash of the base of the snapshot"},
25572559
{RPCResult::Type::NUM, "base_height", "the height of the base of the snapshot"},
25582560
{RPCResult::Type::STR, "path", "the absolute path that the snapshot was written to"},
2561+
{RPCResult::Type::STR_HEX, "txoutset_hash", "the hash of the UTXO set contents"},
2562+
{RPCResult::Type::NUM, "nchaintx", "the number of transactions in the chain up to and including the base block"},
25592563
}
25602564
},
25612565
RPCExamples{
@@ -2578,7 +2582,8 @@ static RPCHelpMan dumptxoutset()
25782582
FILE* file{fsbridge::fopen(temppath, "wb")};
25792583
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
25802584
NodeContext& node = EnsureAnyNodeContext(request.context);
2581-
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), afile);
2585+
UniValue result = CreateUTXOSnapshot(
2586+
node, node.chainman->ActiveChainstate(), afile, path, temppath);
25822587
fs::rename(temppath, path);
25832588

25842589
result.pushKV("path", path.u8string());
@@ -2587,10 +2592,15 @@ static RPCHelpMan dumptxoutset()
25872592
};
25882593
}
25892594

2590-
UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile)
2595+
UniValue CreateUTXOSnapshot(
2596+
NodeContext& node,
2597+
CChainState& chainstate,
2598+
CAutoFile& afile,
2599+
const fs::path& path,
2600+
const fs::path& temppath)
25912601
{
25922602
std::unique_ptr<CCoinsViewCursor> pcursor;
2593-
CCoinsStats stats{CoinStatsHashType::NONE};
2603+
CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
25942604
CBlockIndex* tip;
25952605

25962606
{
@@ -2619,6 +2629,10 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
26192629
CHECK_NONFATAL(tip);
26202630
}
26212631

2632+
LOG_TIME_SECONDS(strprintf("writing UTXO snapshot at height %s (%s) to file %s (via %s)",
2633+
tip->nHeight, tip->GetBlockHash().ToString(),
2634+
fs::PathToString(path), fs::PathToString(temppath)));
2635+
26222636
SnapshotMetadata metadata{tip->GetBlockHash(), stats.coins_count, tip->nChainTx};
26232637

26242638
afile << metadata;
@@ -2644,7 +2658,11 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
26442658
result.pushKV("coins_written", stats.coins_count);
26452659
result.pushKV("base_hash", tip->GetBlockHash().ToString());
26462660
result.pushKV("base_height", tip->nHeight);
2647-
2661+
result.pushKV("path", path.u8string());
2662+
result.pushKV("txoutset_hash", stats.hashSerialized.ToString());
2663+
// Cast required because univalue doesn't have serialization specified for
2664+
// `unsigned int`, nChainTx's type.
2665+
result.pushKV("nchaintx", uint64_t{tip->nChainTx});
26482666
return result;
26492667
}
26502668

src/rpc/blockchain.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <consensus/amount.h>
99
#include <core_io.h>
10+
#include <fs.h>
1011
#include <streams.h>
1112
#include <sync.h>
1213

@@ -65,6 +66,11 @@ CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
6566
* Helper to create UTXO snapshots given a chainstate and a file handle.
6667
* @return a UniValue map containing metadata about the snapshot.
6768
*/
68-
UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile);
69+
UniValue CreateUTXOSnapshot(
70+
NodeContext& node,
71+
CChainState& chainstate,
72+
CAutoFile& afile,
73+
const fs::path& path,
74+
const fs::path& tmppath);
6975

7076
#endif // BITCOIN_RPC_BLOCKCHAIN_H

src/test/util/chainstate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ CreateAndActivateUTXOSnapshot(NodeContext& node, const fs::path root, F malleati
3434
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
3535
CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
3636

37-
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), auto_outfile);
37+
UniValue result = CreateUTXOSnapshot(
38+
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
3839
BOOST_TEST_MESSAGE(
3940
"Wrote UTXO snapshot to " << fs::PathToString(snapshot_path.make_preferred()) << ": " << result.write());
4041

test/functional/rpc_dumptxoutset.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def run_test(self):
4545
assert_equal(
4646
digest, '7ae82c986fa5445678d2a21453bb1c86d39e47af13da137640c2b1cf8093691c')
4747

48+
assert_equal(
49+
out['txoutset_hash'], 'd4b614f476b99a6e569973bf1c0120d88b1a168076f8ce25691fb41dd1cef149')
50+
assert_equal(out['nchaintx'], 101)
51+
4852
# Specifying a path to an existing file will fail.
4953
assert_raises_rpc_error(
5054
-8, '{} already exists'.format(FILENAME), node.dumptxoutset, FILENAME)

0 commit comments

Comments
 (0)