From 5ad49ad66890665bddce2eaa8a8b671693e445fe Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 26 Jun 2024 18:19:26 +0000 Subject: [PATCH] refactor: move Get{Address*, Timestamp, Spent}Index out of validation With bonus const'ing of CTxMemPool::get*Index --- src/Makefile.am | 2 ++ src/rpc/blockchain.cpp | 1 + src/rpc/index_util.cpp | 59 ++++++++++++++++++++++++++++++++++++++ src/rpc/index_util.h | 33 +++++++++++++++++++++ src/rpc/misc.cpp | 3 +- src/rpc/rawtransaction.cpp | 1 + src/validation.cpp | 50 -------------------------------- src/validation.h | 8 ------ 8 files changed, 98 insertions(+), 59 deletions(-) create mode 100644 src/rpc/index_util.cpp create mode 100644 src/rpc/index_util.h diff --git a/src/Makefile.am b/src/Makefile.am index c5d69a6c0a..f1a3cda67d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -286,6 +286,7 @@ BITCOIN_CORE_H = \ reverse_iterator.h \ rpc/blockchain.h \ rpc/client.h \ + rpc/index_util.h \ rpc/mining.h \ rpc/protocol.h \ rpc/rawtransaction_util.h \ @@ -502,6 +503,7 @@ libbitcoin_server_a_SOURCES = \ rpc/blockchain.cpp \ rpc/coinjoin.cpp \ rpc/evo.cpp \ + rpc/index_util.cpp \ rpc/masternode.cpp \ rpc/governance.cpp \ rpc/mining.cpp \ diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 3b2874b910..bef4af863d 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include diff --git a/src/rpc/index_util.cpp b/src/rpc/index_util.cpp new file mode 100644 index 0000000000..65ddf3e49b --- /dev/null +++ b/src/rpc/index_util.cpp @@ -0,0 +1,59 @@ +// Copyright (c) 2016 BitPay, Inc. +// Copyright (c) 2024 The Dash Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include + +bool GetAddressIndex(uint160 addressHash, AddressType type, + std::vector>& addressIndex, int32_t start, int32_t end) +{ + if (!fAddressIndex) + return error("address index not enabled"); + + if (!pblocktree->ReadAddressIndex(addressHash, type, addressIndex, start, end)) + return error("unable to get txids for address"); + + return true; +} + +bool GetAddressUnspentIndex(uint160 addressHash, AddressType type, + std::vector>& unspentOutputs) +{ + if (!fAddressIndex) + return error("address index not enabled"); + + if (!pblocktree->ReadAddressUnspentIndex(addressHash, type, unspentOutputs)) + return error("unable to get txids for address"); + + return true; +} + +bool GetSpentIndex(CTxMemPool& mempool, CSpentIndexKey& key, CSpentIndexValue& value) +{ + if (!fSpentIndex) + return false; + + if (mempool.getSpentIndex(key, value)) + return true; + + if (!pblocktree->ReadSpentIndex(key, value)) + return false; + + return true; +} + +bool GetTimestampIndex(const uint32_t high, const uint32_t low, std::vector& hashes) +{ + if (!fTimestampIndex) + return error("Timestamp index not enabled"); + + if (!pblocktree->ReadTimestampIndex(high, low, hashes)) + return error("Unable to get hashes for timestamps"); + + return true; +} diff --git a/src/rpc/index_util.h b/src/rpc/index_util.h new file mode 100644 index 0000000000..af01cadf9c --- /dev/null +++ b/src/rpc/index_util.h @@ -0,0 +1,33 @@ +// Copyright (c) 2016 BitPay, Inc. +// Copyright (c) 2024 The Dash Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_RPC_INDEX_UTIL_H +#define BITCOIN_RPC_INDEX_UTIL_H + +#include +#include + +#include + +class CTxMemPool; +class uint160; +class uint256; +struct CAddressIndexKey; +struct CAddressUnspentKey; +struct CAddressUnspentValue; +struct CSpentIndexKey; +struct CSpentIndexValue; + +enum class AddressType : uint8_t; + +bool GetAddressIndex(uint160 addressHash, AddressType type, + std::vector>& addressIndex, + int32_t start = 0, int32_t end = 0); +bool GetAddressUnspentIndex(uint160 addressHash, AddressType type, + std::vector>& unspentOutputs); +bool GetSpentIndex(CTxMemPool& mempool, CSpentIndexKey& key, CSpentIndexValue& value); +bool GetTimestampIndex(const uint32_t high, const uint32_t low, std::vector& hashes); + +#endif // BITCOIN_RPC_CLIENT_H diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 44a18f6341..9a4a078a32 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -823,7 +824,7 @@ static RPCHelpMan getaddressutxos() std::vector > unspentOutputs; for (const auto& address : addresses) { - if (!GetAddressUnspent(address.first, address.second, unspentOutputs)) { + if (!GetAddressUnspentIndex(address.first, address.second, unspentOutputs)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 02c5814538..e28aeaf4db 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/src/validation.cpp b/src/validation.cpp index 21b0e63c55..7152919049 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1055,56 +1055,6 @@ PackageMempoolAcceptResult ProcessNewPackage(CChainState& active_chainstate, CTx return result; } -bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector &hashes) -{ - if (!fTimestampIndex) - return error("Timestamp index not enabled"); - - if (!pblocktree->ReadTimestampIndex(high, low, hashes)) - return error("Unable to get hashes for timestamps"); - - return true; -} - -bool GetSpentIndex(CTxMemPool& mempool, CSpentIndexKey &key, CSpentIndexValue &value) -{ - if (!fSpentIndex) - return false; - - if (mempool.getSpentIndex(key, value)) - return true; - - if (!pblocktree->ReadSpentIndex(key, value)) - return false; - - return true; -} - -bool GetAddressIndex(uint160 addressHash, AddressType type, - std::vector > &addressIndex, int start, int end) -{ - if (!fAddressIndex) - return error("address index not enabled"); - - if (!pblocktree->ReadAddressIndex(addressHash, type, addressIndex, start, end)) - return error("unable to get txids for address"); - - return true; -} - -bool GetAddressUnspent(uint160 addressHash, AddressType type, - std::vector > &unspentOutputs) -{ - if (!fAddressIndex) - return error("address index not enabled"); - - if (!pblocktree->ReadAddressUnspentIndex(addressHash, type, unspentOutputs)) - return error("unable to get txids for address"); - - return true; -} - - double ConvertBitsToDouble(unsigned int nBits) { int nShift = (nBits >> 24) & 0xff; diff --git a/src/validation.h b/src/validation.h index 7306edd7f5..9b2541fd93 100644 --- a/src/validation.h +++ b/src/validation.h @@ -22,7 +22,6 @@ #include #include // For CTxMemPool::cs #include -#include #include #include @@ -379,13 +378,6 @@ public: ScriptError GetScriptError() const { return error; } }; -bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector &hashes); -bool GetSpentIndex(CTxMemPool& mempool, CSpentIndexKey &key, CSpentIndexValue &value); -bool GetAddressIndex(uint160 addressHash, AddressType type, - std::vector > &addressIndex, - int start = 0, int end = 0); -bool GetAddressUnspent(uint160 addressHash, AddressType type, - std::vector > &unspentOutputs); /** Initializes the script-execution cache */ void InitScriptExecutionCache();