From b9362392aef2689bc106c20925859ede555d082b Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Sun, 28 Feb 2021 19:27:00 +0100 Subject: [PATCH] index, rpc: Add use_index option for gettxoutsetinfo --- src/node/coinstats.cpp | 6 +++--- src/node/coinstats.h | 5 ++++- src/rpc/blockchain.cpp | 4 +++- src/rpc/client.cpp | 1 + test/functional/feature_coinstatsindex.py | 11 +++++++++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/node/coinstats.cpp b/src/node/coinstats.cpp index eacd7e2ab58..38c1d292504 100644 --- a/src/node/coinstats.cpp +++ b/src/node/coinstats.cpp @@ -104,9 +104,9 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats.nHeight = Assert(pindex)->nHeight; stats.hashBlock = pindex->GetBlockHash(); - // Use CoinStatsIndex if it is available and a hash_type of Muhash or None was requested - if ((stats.m_hash_type == CoinStatsHashType::MUHASH || stats.m_hash_type == CoinStatsHashType::NONE) && g_coin_stats_index) { - stats.from_index = true; + // Use CoinStatsIndex if it is requested and available and a hash_type of Muhash or None was requested + if ((stats.m_hash_type == CoinStatsHashType::MUHASH || stats.m_hash_type == CoinStatsHashType::NONE) && g_coin_stats_index && stats.index_requested) { + stats.index_used = true; return g_coin_stats_index->LookUpStats(pindex, stats); } diff --git a/src/node/coinstats.h b/src/node/coinstats.h index ba565eba436..8be256edc93 100644 --- a/src/node/coinstats.h +++ b/src/node/coinstats.h @@ -39,7 +39,10 @@ struct CCoinsStats //! The number of coins contained. uint64_t coins_count{0}; - bool from_index{false}; + //! Signals if the coinstatsindex should be used (when available). + bool index_requested{true}; + //! Signals if the coinstatsindex was used to retrieve the statistics. + bool index_used{false}; // Following values are only available from coinstats index CAmount total_subsidy{0}; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 1067a7c4bbd..4a4c432a012 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1102,6 +1102,7 @@ static RPCHelpMan gettxoutsetinfo() { {"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'."}, {"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"}}, + {"use_index", RPCArg::Type::BOOL, RPCArg::Default{true}, "Use coinstatsindex, if available."}, }, RPCResult{ RPCResult::Type::OBJ, "", "", @@ -1148,6 +1149,7 @@ static RPCHelpMan gettxoutsetinfo() CBlockIndex* pindex{nullptr}; const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())}; CCoinsStats stats{hash_type}; + stats.index_requested = request.params[2].isNull() || request.params[2].get_bool(); NodeContext& node = EnsureAnyNodeContext(request.context); ChainstateManager& chainman = EnsureChainman(node); @@ -1183,7 +1185,7 @@ static RPCHelpMan gettxoutsetinfo() ret.pushKV("muhash", stats.hashSerialized.GetHex()); } ret.pushKV("total_amount", ValueFromAmount(stats.nTotalAmount)); - if (!stats.from_index) { + if (!stats.index_used) { ret.pushKV("transactions", static_cast(stats.nTransactions)); ret.pushKV("disk_size", stats.nDiskSize); } else { diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 9ccfa36450e..9c8582c7a39 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -128,6 +128,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "gettxout", 2, "include_mempool" }, { "gettxoutproof", 0, "txids" }, { "gettxoutsetinfo", 1, "hash_or_height" }, + { "gettxoutsetinfo", 2, "use_index"}, { "lockunspent", 0, "unlock" }, { "lockunspent", 1, "transactions" }, { "send", 0, "outputs" }, diff --git a/test/functional/feature_coinstatsindex.py b/test/functional/feature_coinstatsindex.py index d517c149cd8..9b368f929ed 100755 --- a/test/functional/feature_coinstatsindex.py +++ b/test/functional/feature_coinstatsindex.py @@ -50,6 +50,7 @@ class CoinStatsIndexTest(BitcoinTestFramework): def run_test(self): self._test_coin_stats_index() + self._test_use_index_option() def block_sanity_check(self, block_info): block_subsidy = 50 @@ -236,6 +237,16 @@ class CoinStatsIndexTest(BitcoinTestFramework): res10 = index_node.gettxoutsetinfo('muhash') assert(res8['txouts'] < res10['txouts']) + def _test_use_index_option(self): + self.log.info("Test use_index option for nodes running the index") + + self.connect_nodes(0, 1) + self.nodes[0].waitforblockheight(110) + res = self.nodes[0].gettxoutsetinfo('muhash') + option_res = self.nodes[1].gettxoutsetinfo(hash_type='muhash', hash_or_height=None, use_index=False) + del res['disk_size'], option_res['disk_size'] + assert_equal(res, option_res) + if __name__ == '__main__': CoinStatsIndexTest().main()