Skip to content

Commit b936239

Browse files
committed
index, rpc: Add use_index option for gettxoutsetinfo
1 parent bb7788b commit b936239

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

src/node/coinstats.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
104104
stats.nHeight = Assert(pindex)->nHeight;
105105
stats.hashBlock = pindex->GetBlockHash();
106106

107-
// Use CoinStatsIndex if it is available and a hash_type of Muhash or None was requested
108-
if ((stats.m_hash_type == CoinStatsHashType::MUHASH || stats.m_hash_type == CoinStatsHashType::NONE) && g_coin_stats_index) {
109-
stats.from_index = true;
107+
// Use CoinStatsIndex if it is requested and available and a hash_type of Muhash or None was requested
108+
if ((stats.m_hash_type == CoinStatsHashType::MUHASH || stats.m_hash_type == CoinStatsHashType::NONE) && g_coin_stats_index && stats.index_requested) {
109+
stats.index_used = true;
110110
return g_coin_stats_index->LookUpStats(pindex, stats);
111111
}
112112

src/node/coinstats.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ struct CCoinsStats
3939
//! The number of coins contained.
4040
uint64_t coins_count{0};
4141

42-
bool from_index{false};
42+
//! Signals if the coinstatsindex should be used (when available).
43+
bool index_requested{true};
44+
//! Signals if the coinstatsindex was used to retrieve the statistics.
45+
bool index_used{false};
4346

4447
// Following values are only available from coinstats index
4548
CAmount total_subsidy{0};

src/rpc/blockchain.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ static RPCHelpMan gettxoutsetinfo()
11021102
{
11031103
{"hash_type", RPCArg::Type::STR, RPCArg::Default{"hash_serialized_2"}, "Which UTXO set hash should be calculated. Options: 'hash_serialized_2' (the legacy algorithm), 'muhash', 'none'."},
11041104
{"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The block hash or height of the target height (only available with coinstatsindex)", "", {"", "string or numeric"}},
1105+
{"use_index", RPCArg::Type::BOOL, RPCArg::Default{true}, "Use coinstatsindex, if available."},
11051106
},
11061107
RPCResult{
11071108
RPCResult::Type::OBJ, "", "",
@@ -1148,6 +1149,7 @@ static RPCHelpMan gettxoutsetinfo()
11481149
CBlockIndex* pindex{nullptr};
11491150
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
11501151
CCoinsStats stats{hash_type};
1152+
stats.index_requested = request.params[2].isNull() || request.params[2].get_bool();
11511153

11521154
NodeContext& node = EnsureAnyNodeContext(request.context);
11531155
ChainstateManager& chainman = EnsureChainman(node);
@@ -1183,7 +1185,7 @@ static RPCHelpMan gettxoutsetinfo()
11831185
ret.pushKV("muhash", stats.hashSerialized.GetHex());
11841186
}
11851187
ret.pushKV("total_amount", ValueFromAmount(stats.nTotalAmount));
1186-
if (!stats.from_index) {
1188+
if (!stats.index_used) {
11871189
ret.pushKV("transactions", static_cast<int64_t>(stats.nTransactions));
11881190
ret.pushKV("disk_size", stats.nDiskSize);
11891191
} else {

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
128128
{ "gettxout", 2, "include_mempool" },
129129
{ "gettxoutproof", 0, "txids" },
130130
{ "gettxoutsetinfo", 1, "hash_or_height" },
131+
{ "gettxoutsetinfo", 2, "use_index"},
131132
{ "lockunspent", 0, "unlock" },
132133
{ "lockunspent", 1, "transactions" },
133134
{ "send", 0, "outputs" },

test/functional/feature_coinstatsindex.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def skip_test_if_missing_module(self):
5050

5151
def run_test(self):
5252
self._test_coin_stats_index()
53+
self._test_use_index_option()
5354

5455
def block_sanity_check(self, block_info):
5556
block_subsidy = 50
@@ -236,6 +237,16 @@ def _test_coin_stats_index(self):
236237
res10 = index_node.gettxoutsetinfo('muhash')
237238
assert(res8['txouts'] < res10['txouts'])
238239

240+
def _test_use_index_option(self):
241+
self.log.info("Test use_index option for nodes running the index")
242+
243+
self.connect_nodes(0, 1)
244+
self.nodes[0].waitforblockheight(110)
245+
res = self.nodes[0].gettxoutsetinfo('muhash')
246+
option_res = self.nodes[1].gettxoutsetinfo(hash_type='muhash', hash_or_height=None, use_index=False)
247+
del res['disk_size'], option_res['disk_size']
248+
assert_equal(res, option_res)
249+
239250

240251
if __name__ == '__main__':
241252
CoinStatsIndexTest().main()

0 commit comments

Comments
 (0)