Skip to content

Commit a7be1cc

Browse files
author
MarcoFalke
committed
Merge bitcoin#16728: move-only: move coins statistics utils out of RPC
8a3b2eb move-only: move coins statistics utils out of RPC (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: bitcoin#15606 Issue: bitcoin#15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- In the short-term, this move-only commit will help with fuzzing (bitcoin#15606 (comment)). Later, these procedures will be used to compute statistics (particularly a content hash) for UTXO sets coming in from snapshots. Most easily reviewed with `git ... --color-moved=dimmed_zebra`. A nice follow-up would be adding unittests, which I'll do if nobody else gets around to it. ACKs for top commit: MarcoFalke: ACK 8a3b2eb, checked --color-moved=dimmed-zebra Tree-SHA512: a187d2f7590ad2450b8e8fa3d038c80a04fc3d903618c24222d7e3172250ce51badea35860c86101f2ba266eb4354e6efb8d7d508b353f29276e4665a1efdf74
2 parents d21e8aa + 8a3b2eb commit a7be1cc

File tree

4 files changed

+113
-71
lines changed

4 files changed

+113
-71
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ BITCOIN_CORE_H = \
156156
netbase.h \
157157
netmessagemaker.h \
158158
node/coin.h \
159+
node/coinstats.h \
159160
node/psbt.h \
160161
node/transaction.h \
161162
noui.h \
@@ -278,6 +279,7 @@ libbitcoin_server_a_SOURCES = \
278279
net.cpp \
279280
net_processing.cpp \
280281
node/coin.cpp \
282+
node/coinstats.cpp \
281283
node/psbt.cpp \
282284
node/transaction.cpp \
283285
noui.cpp \

src/node/coinstats.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include <node/coinstats.h>
7+
8+
#include <amount.h>
9+
#include <coins.h>
10+
#include <chain.h>
11+
#include <hash.h>
12+
#include <serialize.h>
13+
#include <validation.h>
14+
#include <uint256.h>
15+
#include <util/system.h>
16+
17+
#include <map>
18+
19+
#include <boost/thread.hpp>
20+
21+
22+
static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
23+
{
24+
assert(!outputs.empty());
25+
ss << hash;
26+
ss << VARINT(outputs.begin()->second.nHeight * 2 + outputs.begin()->second.fCoinBase ? 1u : 0u);
27+
stats.nTransactions++;
28+
for (const auto& output : outputs) {
29+
ss << VARINT(output.first + 1);
30+
ss << output.second.out.scriptPubKey;
31+
ss << VARINT(output.second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
32+
stats.nTransactionOutputs++;
33+
stats.nTotalAmount += output.second.out.nValue;
34+
stats.nBogoSize += 32 /* txid */ + 4 /* vout index */ + 4 /* height + coinbase */ + 8 /* amount */ +
35+
2 /* scriptPubKey len */ + output.second.out.scriptPubKey.size() /* scriptPubKey */;
36+
}
37+
ss << VARINT(0u);
38+
}
39+
40+
//! Calculate statistics about the unspent transaction output set
41+
bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
42+
{
43+
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
44+
assert(pcursor);
45+
46+
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
47+
stats.hashBlock = pcursor->GetBestBlock();
48+
{
49+
LOCK(cs_main);
50+
stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight;
51+
}
52+
ss << stats.hashBlock;
53+
uint256 prevkey;
54+
std::map<uint32_t, Coin> outputs;
55+
while (pcursor->Valid()) {
56+
boost::this_thread::interruption_point();
57+
COutPoint key;
58+
Coin coin;
59+
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
60+
if (!outputs.empty() && key.hash != prevkey) {
61+
ApplyStats(stats, ss, prevkey, outputs);
62+
outputs.clear();
63+
}
64+
prevkey = key.hash;
65+
outputs[key.n] = std::move(coin);
66+
} else {
67+
return error("%s: unable to read value", __func__);
68+
}
69+
pcursor->Next();
70+
}
71+
if (!outputs.empty()) {
72+
ApplyStats(stats, ss, prevkey, outputs);
73+
}
74+
stats.hashSerialized = ss.GetHash();
75+
stats.nDiskSize = view->EstimateSize();
76+
return true;
77+
}

src/node/coinstats.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_NODE_COINSTATS_H
7+
#define BITCOIN_NODE_COINSTATS_H
8+
9+
#include <amount.h>
10+
#include <uint256.h>
11+
12+
#include <cstdint>
13+
14+
class CCoinsView;
15+
16+
struct CCoinsStats
17+
{
18+
int nHeight;
19+
uint256 hashBlock;
20+
uint64_t nTransactions;
21+
uint64_t nTransactionOutputs;
22+
uint64_t nBogoSize;
23+
uint256 hashSerialized;
24+
uint64_t nDiskSize;
25+
CAmount nTotalAmount;
26+
27+
CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nBogoSize(0), nDiskSize(0), nTotalAmount(0) {}
28+
};
29+
30+
//! Calculate statistics about the unspent transaction output set
31+
bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats);
32+
33+
#endif // BITCOIN_NODE_COINSTATS_H

src/rpc/blockchain.cpp

+1-71
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <chain.h>
1111
#include <chainparams.h>
1212
#include <coins.h>
13+
#include <node/coinstats.h>
1314
#include <consensus/validation.h>
1415
#include <core_io.h>
1516
#include <hash.h>
@@ -909,77 +910,6 @@ static UniValue getblock(const JSONRPCRequest& request)
909910
return blockToJSON(block, tip, pblockindex, verbosity >= 2);
910911
}
911912

912-
struct CCoinsStats
913-
{
914-
int nHeight;
915-
uint256 hashBlock;
916-
uint64_t nTransactions;
917-
uint64_t nTransactionOutputs;
918-
uint64_t nBogoSize;
919-
uint256 hashSerialized;
920-
uint64_t nDiskSize;
921-
CAmount nTotalAmount;
922-
923-
CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nBogoSize(0), nDiskSize(0), nTotalAmount(0) {}
924-
};
925-
926-
static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
927-
{
928-
assert(!outputs.empty());
929-
ss << hash;
930-
ss << VARINT(outputs.begin()->second.nHeight * 2 + outputs.begin()->second.fCoinBase ? 1u : 0u);
931-
stats.nTransactions++;
932-
for (const auto& output : outputs) {
933-
ss << VARINT(output.first + 1);
934-
ss << output.second.out.scriptPubKey;
935-
ss << VARINT(output.second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
936-
stats.nTransactionOutputs++;
937-
stats.nTotalAmount += output.second.out.nValue;
938-
stats.nBogoSize += 32 /* txid */ + 4 /* vout index */ + 4 /* height + coinbase */ + 8 /* amount */ +
939-
2 /* scriptPubKey len */ + output.second.out.scriptPubKey.size() /* scriptPubKey */;
940-
}
941-
ss << VARINT(0u);
942-
}
943-
944-
//! Calculate statistics about the unspent transaction output set
945-
static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
946-
{
947-
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
948-
assert(pcursor);
949-
950-
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
951-
stats.hashBlock = pcursor->GetBestBlock();
952-
{
953-
LOCK(cs_main);
954-
stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight;
955-
}
956-
ss << stats.hashBlock;
957-
uint256 prevkey;
958-
std::map<uint32_t, Coin> outputs;
959-
while (pcursor->Valid()) {
960-
boost::this_thread::interruption_point();
961-
COutPoint key;
962-
Coin coin;
963-
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
964-
if (!outputs.empty() && key.hash != prevkey) {
965-
ApplyStats(stats, ss, prevkey, outputs);
966-
outputs.clear();
967-
}
968-
prevkey = key.hash;
969-
outputs[key.n] = std::move(coin);
970-
} else {
971-
return error("%s: unable to read value", __func__);
972-
}
973-
pcursor->Next();
974-
}
975-
if (!outputs.empty()) {
976-
ApplyStats(stats, ss, prevkey, outputs);
977-
}
978-
stats.hashSerialized = ss.GetHash();
979-
stats.nDiskSize = view->EstimateSize();
980-
return true;
981-
}
982-
983913
static UniValue pruneblockchain(const JSONRPCRequest& request)
984914
{
985915
RPCHelpMan{"pruneblockchain", "",

0 commit comments

Comments
 (0)