Skip to content

Commit ffd0928

Browse files
committed
rpc: various fixups for dumptxoutset
- Actually generate an assumeutxo hash and display it - Add nchaintx to output (necessary for use in chainparams entry) - Add path of serialized UTXO file to output
1 parent ab25ef8 commit ffd0928

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>
@@ -2547,6 +2549,8 @@ static RPCHelpMan dumptxoutset()
25472549
{RPCResult::Type::STR_HEX, "base_hash", "the hash of the base of the snapshot"},
25482550
{RPCResult::Type::NUM, "base_height", "the height of the base of the snapshot"},
25492551
{RPCResult::Type::STR, "path", "the absolute path that the snapshot was written to"},
2552+
{RPCResult::Type::STR_HEX, "txoutset_hash", "the hash of the UTXO set contents"},
2553+
{RPCResult::Type::NUM, "nchaintx", "the number of transactions in the chain up to and including the base block"},
25502554
}
25512555
},
25522556
RPCExamples{
@@ -2569,7 +2573,8 @@ static RPCHelpMan dumptxoutset()
25692573
FILE* file{fsbridge::fopen(temppath, "wb")};
25702574
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
25712575
NodeContext& node = EnsureAnyNodeContext(request.context);
2572-
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), afile);
2576+
UniValue result = CreateUTXOSnapshot(
2577+
node, node.chainman->ActiveChainstate(), afile, path, temppath);
25732578
fs::rename(temppath, path);
25742579

25752580
result.pushKV("path", path.u8string());
@@ -2578,10 +2583,15 @@ static RPCHelpMan dumptxoutset()
25782583
};
25792584
}
25802585

2581-
UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile)
2586+
UniValue CreateUTXOSnapshot(
2587+
NodeContext& node,
2588+
CChainState& chainstate,
2589+
CAutoFile& afile,
2590+
const fs::path& path,
2591+
const fs::path& temppath)
25822592
{
25832593
std::unique_ptr<CCoinsViewCursor> pcursor;
2584-
CCoinsStats stats{CoinStatsHashType::NONE};
2594+
CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
25852595
CBlockIndex* tip;
25862596

25872597
{
@@ -2610,6 +2620,10 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
26102620
CHECK_NONFATAL(tip);
26112621
}
26122622

2623+
LOG_TIME_SECONDS(strprintf("writing UTXO snapshot at height %s (%s) to file %s (via %s)",
2624+
tip->nHeight, tip->GetBlockHash().ToString(),
2625+
fs::PathToString(path), fs::PathToString(temppath)));
2626+
26132627
SnapshotMetadata metadata{tip->GetBlockHash(), stats.coins_count, tip->nChainTx};
26142628

26152629
afile << metadata;
@@ -2635,7 +2649,11 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
26352649
result.pushKV("coins_written", stats.coins_count);
26362650
result.pushKV("base_hash", tip->GetBlockHash().ToString());
26372651
result.pushKV("base_height", tip->nHeight);
2638-
2652+
result.pushKV("path", path.u8string());
2653+
result.pushKV("txoutset_hash", stats.hashSerialized.ToString());
2654+
// Cast required because univalue doesn't have serialization specified for
2655+
// `unsigned int`, nChainTx's type.
2656+
result.pushKV("nchaintx", uint64_t{tip->nChainTx});
26392657
return result;
26402658
}
26412659

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

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)