mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-29 02:11:24 +02:00
index, rpc: Add use_index option for gettxoutsetinfo
This commit is contained in:
@ -104,9 +104,9 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
|
|||||||
stats.nHeight = Assert(pindex)->nHeight;
|
stats.nHeight = Assert(pindex)->nHeight;
|
||||||
stats.hashBlock = pindex->GetBlockHash();
|
stats.hashBlock = pindex->GetBlockHash();
|
||||||
|
|
||||||
// Use CoinStatsIndex if it is available and a hash_type of Muhash or None was requested
|
// 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) {
|
if ((stats.m_hash_type == CoinStatsHashType::MUHASH || stats.m_hash_type == CoinStatsHashType::NONE) && g_coin_stats_index && stats.index_requested) {
|
||||||
stats.from_index = true;
|
stats.index_used = true;
|
||||||
return g_coin_stats_index->LookUpStats(pindex, stats);
|
return g_coin_stats_index->LookUpStats(pindex, stats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,10 @@ struct CCoinsStats
|
|||||||
//! The number of coins contained.
|
//! The number of coins contained.
|
||||||
uint64_t coins_count{0};
|
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
|
// Following values are only available from coinstats index
|
||||||
CAmount total_subsidy{0};
|
CAmount total_subsidy{0};
|
||||||
|
@ -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_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"}},
|
{"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{
|
||||||
RPCResult::Type::OBJ, "", "",
|
RPCResult::Type::OBJ, "", "",
|
||||||
@ -1148,6 +1149,7 @@ static RPCHelpMan gettxoutsetinfo()
|
|||||||
CBlockIndex* pindex{nullptr};
|
CBlockIndex* pindex{nullptr};
|
||||||
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
|
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
|
||||||
CCoinsStats stats{hash_type};
|
CCoinsStats stats{hash_type};
|
||||||
|
stats.index_requested = request.params[2].isNull() || request.params[2].get_bool();
|
||||||
|
|
||||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||||
ChainstateManager& chainman = EnsureChainman(node);
|
ChainstateManager& chainman = EnsureChainman(node);
|
||||||
@ -1183,7 +1185,7 @@ static RPCHelpMan gettxoutsetinfo()
|
|||||||
ret.pushKV("muhash", stats.hashSerialized.GetHex());
|
ret.pushKV("muhash", stats.hashSerialized.GetHex());
|
||||||
}
|
}
|
||||||
ret.pushKV("total_amount", ValueFromAmount(stats.nTotalAmount));
|
ret.pushKV("total_amount", ValueFromAmount(stats.nTotalAmount));
|
||||||
if (!stats.from_index) {
|
if (!stats.index_used) {
|
||||||
ret.pushKV("transactions", static_cast<int64_t>(stats.nTransactions));
|
ret.pushKV("transactions", static_cast<int64_t>(stats.nTransactions));
|
||||||
ret.pushKV("disk_size", stats.nDiskSize);
|
ret.pushKV("disk_size", stats.nDiskSize);
|
||||||
} else {
|
} else {
|
||||||
|
@ -128,6 +128,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
|
|||||||
{ "gettxout", 2, "include_mempool" },
|
{ "gettxout", 2, "include_mempool" },
|
||||||
{ "gettxoutproof", 0, "txids" },
|
{ "gettxoutproof", 0, "txids" },
|
||||||
{ "gettxoutsetinfo", 1, "hash_or_height" },
|
{ "gettxoutsetinfo", 1, "hash_or_height" },
|
||||||
|
{ "gettxoutsetinfo", 2, "use_index"},
|
||||||
{ "lockunspent", 0, "unlock" },
|
{ "lockunspent", 0, "unlock" },
|
||||||
{ "lockunspent", 1, "transactions" },
|
{ "lockunspent", 1, "transactions" },
|
||||||
{ "send", 0, "outputs" },
|
{ "send", 0, "outputs" },
|
||||||
|
@ -50,6 +50,7 @@ class CoinStatsIndexTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
self._test_coin_stats_index()
|
self._test_coin_stats_index()
|
||||||
|
self._test_use_index_option()
|
||||||
|
|
||||||
def block_sanity_check(self, block_info):
|
def block_sanity_check(self, block_info):
|
||||||
block_subsidy = 50
|
block_subsidy = 50
|
||||||
@ -236,6 +237,16 @@ class CoinStatsIndexTest(BitcoinTestFramework):
|
|||||||
res10 = index_node.gettxoutsetinfo('muhash')
|
res10 = index_node.gettxoutsetinfo('muhash')
|
||||||
assert(res8['txouts'] < res10['txouts'])
|
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__':
|
if __name__ == '__main__':
|
||||||
CoinStatsIndexTest().main()
|
CoinStatsIndexTest().main()
|
||||||
|
Reference in New Issue
Block a user