refactor: move Get{Address*, Timestamp, Spent}Index out of validation

With bonus const'ing of CTxMemPool::get*Index
This commit is contained in:
Kittywhiskers Van Gogh 2024-06-26 18:19:26 +00:00
parent 37e026a038
commit 5ad49ad668
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
8 changed files with 98 additions and 59 deletions

View File

@ -286,6 +286,7 @@ BITCOIN_CORE_H = \
reverse_iterator.h \ reverse_iterator.h \
rpc/blockchain.h \ rpc/blockchain.h \
rpc/client.h \ rpc/client.h \
rpc/index_util.h \
rpc/mining.h \ rpc/mining.h \
rpc/protocol.h \ rpc/protocol.h \
rpc/rawtransaction_util.h \ rpc/rawtransaction_util.h \
@ -502,6 +503,7 @@ libbitcoin_server_a_SOURCES = \
rpc/blockchain.cpp \ rpc/blockchain.cpp \
rpc/coinjoin.cpp \ rpc/coinjoin.cpp \
rpc/evo.cpp \ rpc/evo.cpp \
rpc/index_util.cpp \
rpc/masternode.cpp \ rpc/masternode.cpp \
rpc/governance.cpp \ rpc/governance.cpp \
rpc/mining.cpp \ rpc/mining.cpp \

View File

@ -30,6 +30,7 @@
#include <policy/fees.h> #include <policy/fees.h>
#include <policy/policy.h> #include <policy/policy.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <rpc/index_util.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <rpc/server_util.h> #include <rpc/server_util.h>
#include <rpc/util.h> #include <rpc/util.h>

59
src/rpc/index_util.cpp Normal file
View File

@ -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 <rpc/index_util.h>
#include <txmempool.h>
#include <uint256.h>
#include <validation.h>
bool GetAddressIndex(uint160 addressHash, AddressType type,
std::vector<std::pair<CAddressIndexKey, CAmount>>& 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<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& 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<uint256>& 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;
}

33
src/rpc/index_util.h Normal file
View File

@ -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 <cstdint>
#include <vector>
#include <amount.h>
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<std::pair<CAddressIndexKey, CAmount>>& addressIndex,
int32_t start = 0, int32_t end = 0);
bool GetAddressUnspentIndex(uint160 addressHash, AddressType type,
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& unspentOutputs);
bool GetSpentIndex(CTxMemPool& mempool, CSpentIndexKey& key, CSpentIndexValue& value);
bool GetTimestampIndex(const uint32_t high, const uint32_t low, std::vector<uint256>& hashes);
#endif // BITCOIN_RPC_CLIENT_H

View File

@ -19,6 +19,7 @@
#include <net.h> #include <net.h>
#include <node/context.h> #include <node/context.h>
#include <rpc/blockchain.h> #include <rpc/blockchain.h>
#include <rpc/index_util.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <rpc/server_util.h> #include <rpc/server_util.h>
#include <rpc/util.h> #include <rpc/util.h>
@ -823,7 +824,7 @@ static RPCHelpMan getaddressutxos()
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs; std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
for (const auto& address : addresses) { 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"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
} }
} }

View File

@ -26,6 +26,7 @@
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <psbt.h> #include <psbt.h>
#include <rpc/blockchain.h> #include <rpc/blockchain.h>
#include <rpc/index_util.h>
#include <rpc/rawtransaction_util.h> #include <rpc/rawtransaction_util.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <rpc/server_util.h> #include <rpc/server_util.h>

View File

@ -1055,56 +1055,6 @@ PackageMempoolAcceptResult ProcessNewPackage(CChainState& active_chainstate, CTx
return result; return result;
} }
bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector<uint256> &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<std::pair<CAddressIndexKey, CAmount> > &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<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &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) double ConvertBitsToDouble(unsigned int nBits)
{ {
int nShift = (nBits >> 24) & 0xff; int nShift = (nBits >> 24) & 0xff;

View File

@ -22,7 +22,6 @@
#include <txdb.h> #include <txdb.h>
#include <txmempool.h> // For CTxMemPool::cs #include <txmempool.h> // For CTxMemPool::cs
#include <serialize.h> #include <serialize.h>
#include <spentindex.h>
#include <util/check.h> #include <util/check.h>
#include <util/hasher.h> #include <util/hasher.h>
@ -379,13 +378,6 @@ public:
ScriptError GetScriptError() const { return error; } ScriptError GetScriptError() const { return error; }
}; };
bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector<uint256> &hashes);
bool GetSpentIndex(CTxMemPool& mempool, CSpentIndexKey &key, CSpentIndexValue &value);
bool GetAddressIndex(uint160 addressHash, AddressType type,
std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,
int start = 0, int end = 0);
bool GetAddressUnspent(uint160 addressHash, AddressType type,
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs);
/** Initializes the script-execution cache */ /** Initializes the script-execution cache */
void InitScriptExecutionCache(); void InitScriptExecutionCache();