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/addressindex.h b/src/addressindex.h index 42f8227237..e2dc67a7d8 100644 --- a/src/addressindex.h +++ b/src/addressindex.h @@ -17,6 +17,9 @@ #include class CScript; +struct CAddressIndexKey; +struct CMempoolAddressDelta; +struct CMempoolAddressDeltaKey; enum class AddressType : uint8_t { P2PK_OR_P2PKH = 1, @@ -26,6 +29,9 @@ enum class AddressType : uint8_t { }; template<> struct is_serializable_enum : std::true_type {}; +using CAddressIndexEntry = std::pair; +using CMempoolAddressDeltaEntry = std::pair; + struct CMempoolAddressDelta { public: diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 3b2874b910..524736d560 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -834,7 +835,7 @@ static RPCHelpMan getblockhashes() unsigned int low = request.params[1].get_int(); std::vector blockHashes; - if (!GetTimestampIndex(high, low, blockHashes)) { + if (LOCK(::cs_main); !GetTimestampIndex(*pblocktree, high, low, blockHashes)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for block hashes"); } diff --git a/src/rpc/index_util.cpp b/src/rpc/index_util.cpp new file mode 100644 index 0000000000..4e362af88c --- /dev/null +++ b/src/rpc/index_util.cpp @@ -0,0 +1,98 @@ +// 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(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, + std::vector& addressIndex, + const int32_t start, const int32_t end) +{ + AssertLockHeld(::cs_main); + + if (!fAddressIndex) + return error("Address index not enabled"); + + if (!block_tree_db.ReadAddressIndex(addressHash, type, addressIndex, start, end)) + return error("Unable to get txids for address"); + + return true; +} + +bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, + std::vector& unspentOutputs, const bool height_sort) +{ + AssertLockHeld(::cs_main); + + if (!fAddressIndex) + return error("Address index not enabled"); + + if (!block_tree_db.ReadAddressUnspentIndex(addressHash, type, unspentOutputs)) + return error("Unable to get txids for address"); + + if (height_sort) { + std::sort(unspentOutputs.begin(), unspentOutputs.end(), + [](const CAddressUnspentIndexEntry &a, const CAddressUnspentIndexEntry &b) { + return a.second.m_block_height < b.second.m_block_height; + }); + } + + return true; +} + +bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, + const std::vector& addressDeltaIndex, + std::vector& addressDeltaEntries, + const bool timestamp_sort) +{ + if (!fAddressIndex) + return error("Address index not enabled"); + + if (!mempool.getAddressIndex(addressDeltaIndex, addressDeltaEntries)) + return error("Unable to get address delta information"); + + if (timestamp_sort) { + std::sort(addressDeltaEntries.begin(), addressDeltaEntries.end(), + [](const CMempoolAddressDeltaEntry &a, const CMempoolAddressDeltaEntry &b) { + return a.second.m_time < b.second.m_time; + }); + } + + return true; +} + +bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const CSpentIndexKey& key, + CSpentIndexValue& value) +{ + AssertLockHeld(::cs_main); + + if (!fSpentIndex) + return error("Spent index not enabled"); + + if (mempool.getSpentIndex(key, value)) + return true; + + if (!block_tree_db.ReadSpentIndex(key, value)) + return error("Unable to get spend information"); + + return true; +} + +bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low, + std::vector& hashes) +{ + AssertLockHeld(::cs_main); + + if (!fTimestampIndex) + return error("Timestamp index not enabled"); + + if (!block_tree_db.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..3155bf2263 --- /dev/null +++ b/src/rpc/index_util.h @@ -0,0 +1,45 @@ +// 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 +#include +#include +#include +#include + +class CBlockTreeDB; +class CTxMemPool; +class uint160; +class uint256; + +enum class AddressType : uint8_t; + +extern RecursiveMutex cs_main; + +bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, + std::vector& addressIndex, + const int32_t start = 0, const int32_t end = 0) + EXCLUSIVE_LOCKS_REQUIRED(::cs_main); +bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, + std::vector& unspentOutputs, const bool height_sort = false) + EXCLUSIVE_LOCKS_REQUIRED(::cs_main); +bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, + const std::vector& addressDeltaIndex, + std::vector& addressDeltaEntries, + const bool timestamp_sort = false); +bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const CSpentIndexKey& key, + CSpentIndexValue& value) + EXCLUSIVE_LOCKS_REQUIRED(::cs_main); +bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low, + std::vector& hashes) + EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + +#endif // BITCOIN_RPC_CLIENT_H diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 44a18f6341..377acc2679 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -700,16 +701,6 @@ static bool getAddressesFromParams(const UniValue& params, std::vector a, - std::pair b) { - return a.second.m_block_height < b.second.m_block_height; -} - -static bool timestampSort(std::pair a, - std::pair b) { - return a.second.m_time < b.second.m_time; -} - static RPCHelpMan getaddressmempool() { return RPCHelpMan{"getaddressmempool", @@ -741,22 +732,22 @@ static RPCHelpMan getaddressmempool() }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { + CTxMemPool& mempool = EnsureAnyMemPool(request.context); - std::vector > addresses; - + std::vector> addresses; if (!getAddressesFromParams(request.params, addresses)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); } - std::vector > indexes; - - CTxMemPool& mempool = EnsureAnyMemPool(request.context); - if (!mempool.getAddressIndex(addresses, indexes)) { + std::vector input_addresses; + std::vector indexes; + for (const auto& [hash, type] : addresses) { + input_addresses.push_back({type, hash}); + } + if (!GetMempoolAddressDeltaIndex(mempool, input_addresses, indexes, /* timestamp_sort = */ true)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } - std::sort(indexes.begin(), indexes.end(), timestampSort); - UniValue result(UniValue::VARR); for (const auto& [mempoolAddressKey, mempoolAddressDelta] : indexes) { @@ -820,16 +811,18 @@ static RPCHelpMan getaddressutxos() throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); } - std::vector > unspentOutputs; + std::vector unspentOutputs; - for (const auto& address : addresses) { - if (!GetAddressUnspent(address.first, address.second, unspentOutputs)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + { + LOCK(::cs_main); + for (const auto& address : addresses) { + if (!GetAddressUnspentIndex(*pblocktree, address.first, address.second, unspentOutputs, + /* height_sort = */ true)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + } } } - std::sort(unspentOutputs.begin(), unspentOutputs.end(), heightSort); - UniValue result(UniValue::VARR); for (const auto& [unspentKey, unspentValue] : unspentOutputs) { @@ -905,16 +898,19 @@ static RPCHelpMan getaddressdeltas() throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); } - std::vector > addressIndex; + std::vector addressIndex; - for (const auto& address : addresses) { - if (start > 0 && end > 0) { - if (!GetAddressIndex(address.first, address.second, addressIndex, start, end)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); - } - } else { - if (!GetAddressIndex(address.first, address.second, addressIndex)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + { + LOCK(::cs_main); + for (const auto& address : addresses) { + if (start > 0 && end > 0) { + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex, start, end)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + } + } else { + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + } } } } @@ -974,16 +970,21 @@ static RPCHelpMan getaddressbalance() throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); } - std::vector > addressIndex; - - for (const auto& address : addresses) { - if (!GetAddressIndex(address.first, address.second, addressIndex)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); - } - } + std::vector addressIndex; ChainstateManager& chainman = EnsureAnyChainman(request.context); - int nHeight = WITH_LOCK(cs_main, return chainman.ActiveChain().Height()); + + int nHeight; + { + LOCK(::cs_main); + for (const auto& address : addresses) { + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + } + } + nHeight = chainman.ActiveChain().Height(); + } + CAmount balance = 0; CAmount balance_spendable = 0; @@ -1053,16 +1054,19 @@ static RPCHelpMan getaddresstxids() } } - std::vector > addressIndex; + std::vector addressIndex; - for (const auto& address : addresses) { - if (start > 0 && end > 0) { - if (!GetAddressIndex(address.first, address.second, addressIndex, start, end)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); - } - } else { - if (!GetAddressIndex(address.first, address.second, addressIndex)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + { + LOCK(::cs_main); + for (const auto& address : addresses) { + if (start > 0 && end > 0) { + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex, start, end)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + } + } else { + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); + } } } } @@ -1134,7 +1138,7 @@ static RPCHelpMan getspentinfo() CSpentIndexValue value; CTxMemPool& mempool = EnsureAnyMemPool(request.context); - if (!GetSpentIndex(mempool, key, value)) { + if (LOCK(::cs_main); !GetSpentIndex(*pblocktree, mempool, key, value)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get spent info"); } diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 02c5814538..da0010acc6 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -58,6 +59,8 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempool, CChainState& active_chainstate, llmq::CChainLocksHandler& clhandler, llmq::CInstantSendManager& isman, UniValue& entry) { + LOCK(::cs_main); + // Call into TxToUniv() in bitcoin-common to decode the transaction hex. // // Blockchain contextual information (confirmations and blocktime) is not @@ -72,7 +75,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo if (!tx.IsCoinBase()) { CSpentIndexValue spentInfo; CSpentIndexKey spentKey(txin.prevout.hash, txin.prevout.n); - if (GetSpentIndex(mempool, spentKey, spentInfo)) { + if (GetSpentIndex(*pblocktree, mempool, spentKey, spentInfo)) { txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo); } } @@ -80,7 +83,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo for (unsigned int i = 0; i < tx.vout.size(); i++) { CSpentIndexValue spentInfo; CSpentIndexKey spentKey(txid, i); - if (GetSpentIndex(mempool, spentKey, spentInfo)) { + if (GetSpentIndex(*pblocktree, mempool, spentKey, spentInfo)) { txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo); } } @@ -89,8 +92,6 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo bool chainLock = false; if (!hashBlock.IsNull()) { - LOCK(cs_main); - entry.pushKV("blockhash", hashBlock.GetHex()); CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock); if (pindex) { diff --git a/src/spentindex.h b/src/spentindex.h index 944ba638ca..626762fc5f 100644 --- a/src/spentindex.h +++ b/src/spentindex.h @@ -16,6 +16,14 @@ #include +struct CAddressUnspentKey; +struct CAddressUnspentValue; +struct CSpentIndexKey; +struct CSpentIndexValue; + +using CAddressUnspentIndexEntry = std::pair; +using CSpentIndexEntry = std::pair; + struct CSpentIndexKey { public: uint256 m_tx_hash; diff --git a/src/txdb.cpp b/src/txdb.cpp index 9018be6849..038432c8fa 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -263,13 +263,13 @@ bool CBlockTreeDB::WriteBatchSync(const std::vector >&vect) { +bool CBlockTreeDB::UpdateSpentIndex(const std::vector& vect) { CDBBatch batch(*this); - for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) { + for (std::vector>::const_iterator it=vect.begin(); it!=vect.end(); it++) { if (it->second.IsNull()) { batch.Erase(std::make_pair(DB_SPENTINDEX, it->first)); } else { @@ -279,9 +279,9 @@ bool CBlockTreeDB::UpdateSpentIndex(const std::vector >&vect) { +bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector& vect) { CDBBatch batch(*this); - for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) { + for (std::vector::const_iterator it=vect.begin(); it!=vect.end(); it++) { if (it->second.IsNull()) { batch.Erase(std::make_pair(DB_ADDRESSUNSPENTINDEX, it->first)); } else { @@ -291,9 +291,9 @@ bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector > &unspentOutputs) { - +bool CBlockTreeDB::ReadAddressUnspentIndex(const uint160& addressHash, const AddressType type, + std::vector& unspentOutputs) +{ std::unique_ptr pcursor(NewIterator()); pcursor->Seek(std::make_pair(DB_ADDRESSUNSPENTINDEX, CAddressIndexIteratorKey(type, addressHash))); @@ -316,24 +316,24 @@ bool CBlockTreeDB::ReadAddressUnspentIndex(uint160 addressHash, AddressType type return true; } -bool CBlockTreeDB::WriteAddressIndex(const std::vector >&vect) { +bool CBlockTreeDB::WriteAddressIndex(const std::vector& vect) { CDBBatch batch(*this); - for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) + for (std::vector::const_iterator it=vect.begin(); it!=vect.end(); it++) batch.Write(std::make_pair(DB_ADDRESSINDEX, it->first), it->second); return WriteBatch(batch); } -bool CBlockTreeDB::EraseAddressIndex(const std::vector >&vect) { +bool CBlockTreeDB::EraseAddressIndex(const std::vector& vect) { CDBBatch batch(*this); - for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) + for (std::vector::const_iterator it=vect.begin(); it!=vect.end(); it++) batch.Erase(std::make_pair(DB_ADDRESSINDEX, it->first)); return WriteBatch(batch); } -bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, AddressType type, - std::vector > &addressIndex, - int start, int end) { - +bool CBlockTreeDB::ReadAddressIndex(const uint160& addressHash, const AddressType type, + std::vector& addressIndex, + const int32_t start, const int32_t end) +{ std::unique_ptr pcursor(NewIterator()); if (start > 0 && end > 0) { @@ -363,7 +363,7 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, AddressType type, return true; } -bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey ×tampIndex) { +bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey& timestampIndex) { CDBBatch batch(*this); batch.Write(std::make_pair(DB_TIMESTAMPINDEX, timestampIndex), 0); return WriteBatch(batch); @@ -376,8 +376,7 @@ bool CBlockTreeDB::EraseTimestampIndex(const CTimestampIndexKey& timestampIndex) return WriteBatch(batch); } -bool CBlockTreeDB::ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector &hashes) { - +bool CBlockTreeDB::ReadTimestampIndex(const uint32_t high, const uint32_t low, std::vector& hashes) { std::unique_ptr pcursor(NewIterator()); pcursor->Seek(std::make_pair(DB_TIMESTAMPINDEX, CTimestampIndexIteratorKey(low))); diff --git a/src/txdb.h b/src/txdb.h index c2627aa079..54c79ade14 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -84,19 +84,24 @@ public: bool ReadLastBlockFile(int &nFile); bool WriteReindexing(bool fReindexing); void ReadReindexing(bool &fReindexing); - bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value); - bool UpdateSpentIndex(const std::vector >&vect); - bool UpdateAddressUnspentIndex(const std::vector >&vect); - bool ReadAddressUnspentIndex(uint160 addressHash, AddressType type, - std::vector > &vect); - bool WriteAddressIndex(const std::vector > &vect); - bool EraseAddressIndex(const std::vector > &vect); - bool ReadAddressIndex(uint160 addressHash, AddressType type, - std::vector > &addressIndex, - int start = 0, int end = 0); - bool WriteTimestampIndex(const CTimestampIndexKey ×tampIndex); + + bool ReadSpentIndex(const CSpentIndexKey key, CSpentIndexValue& value); + bool UpdateSpentIndex(const std::vector& vect); + + bool ReadAddressUnspentIndex(const uint160& addressHash, const AddressType type, + std::vector& vect); + bool UpdateAddressUnspentIndex(const std::vector& vect); + + bool WriteAddressIndex(const std::vector& vect); + bool EraseAddressIndex(const std::vector& vect); + bool ReadAddressIndex(const uint160& addressHash, const AddressType type, + std::vector& addressIndex, + const int32_t start = 0, const int32_t end = 0); + + bool WriteTimestampIndex(const CTimestampIndexKey& timestampIndex); bool EraseTimestampIndex(const CTimestampIndexKey& timestampIndex); - bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector &vect); + bool ReadTimestampIndex(const uint32_t high, const uint32_t low, std::vector& hashes); + bool WriteFlag(const std::string &name, bool fValue); bool ReadFlag(const std::string &name, bool &fValue); bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function insertBlockIndex); diff --git a/src/txmempool.cpp b/src/txmempool.cpp index ef089a2a38..1f125a4c17 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -427,7 +427,7 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces } } -void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view) +void CTxMemPool::addAddressIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view) { LOCK(cs); const CTransaction& tx = entry.GetTx(); @@ -470,13 +470,14 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC mapAddressInserted.insert(std::make_pair(txhash, inserted)); } -bool CTxMemPool::getAddressIndex(std::vector > &addresses, - std::vector > &results) +bool CTxMemPool::getAddressIndex(const std::vector& addresses, + std::vector& results) const { LOCK(cs); for (const auto& address : addresses) { - addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey(address.second, address.first)); - while (ait != mapAddress.end() && (*ait).first.m_address_bytes == address.first && (*ait).first.m_address_type == address.second) { + addressDeltaMap::const_iterator ait = mapAddress.lower_bound(address); + while (ait != mapAddress.end() && (*ait).first.m_address_bytes == address.m_address_bytes + && (*ait).first.m_address_type == address.m_address_type) { results.push_back(*ait); ait++; } @@ -500,7 +501,7 @@ bool CTxMemPool::removeAddressIndex(const uint256 txhash) return true; } -void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view) +void CTxMemPool::addSpentIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view) { LOCK(cs); @@ -530,12 +531,11 @@ void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCac mapSpentInserted.insert(make_pair(txhash, inserted)); } -bool CTxMemPool::getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) +bool CTxMemPool::getSpentIndex(const CSpentIndexKey& key, CSpentIndexValue& value) const { LOCK(cs); - mapSpentIndex::iterator it; + mapSpentIndex::const_iterator it = mapSpent.find(key); - it = mapSpent.find(key); if (it != mapSpent.end()) { value = it->second; return true; diff --git a/src/txmempool.h b/src/txmempool.h index a4717b7e88..12efa08b51 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -635,13 +635,13 @@ public: void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main); void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main); - void addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view); - bool getAddressIndex(std::vector > &addresses, - std::vector > &results); + void addAddressIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view); + bool getAddressIndex(const std::vector& addresses, + std::vector& results) const; bool removeAddressIndex(const uint256 txhash); - void addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view); - bool getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value); + void addSpentIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view); + bool getSpentIndex(const CSpentIndexKey& key, CSpentIndexValue& value) const; bool removeSpentIndex(const uint256 txhash); void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs); diff --git a/src/validation.cpp b/src/validation.cpp index 21b0e63c55..15be0df400 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; @@ -1735,9 +1685,9 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI return DISCONNECT_FAILED; } - std::vector > addressIndex; - std::vector > addressUnspentIndex; - std::vector > spentIndex; + std::vector addressIndex; + std::vector addressUnspentIndex; + std::vector spentIndex; std::optional mnlist_updates_opt{std::nullopt}; if (!m_chain_helper->special_tx->UndoSpecialTxsInBlock(block, pindex, mnlist_updates_opt)) { @@ -2202,9 +2152,9 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, int nInputs = 0; unsigned int nSigOps = 0; blockundo.vtxundo.reserve(block.vtx.size() - 1); - std::vector > addressIndex; - std::vector > addressUnspentIndex; - std::vector > spentIndex; + std::vector addressIndex; + std::vector addressUnspentIndex; + std::vector spentIndex; bool fDIP0001Active_context = pindex->nHeight >= Params().GetConsensus().DIP0001Height; @@ -4832,9 +4782,9 @@ bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& i pindex->GetBlockHash().ToString(), state.ToString()); } - std::vector > addressIndex; - std::vector > addressUnspentIndex; - std::vector > spentIndex; + std::vector addressIndex; + std::vector addressUnspentIndex; + std::vector spentIndex; for (size_t i = 0; i < block.vtx.size(); i++) { const CTransactionRef& tx = block.vtx[i]; 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();