From ee9d11214e6aa3d90adf6b1d3ec8892ac8c6ca3b Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 26 Jun 2024 19:24:21 +0000 Subject: [PATCH] refactor: move pair value swapping out of CTxMemPool::getAddressIndex() --- src/rpc/index_util.cpp | 13 +++++++++++++ src/rpc/index_util.h | 3 +++ src/rpc/misc.cpp | 13 +++++++------ src/txmempool.cpp | 7 ++++--- src/txmempool.h | 2 +- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/rpc/index_util.cpp b/src/rpc/index_util.cpp index 804e956d86..0680012e56 100644 --- a/src/rpc/index_util.cpp +++ b/src/rpc/index_util.cpp @@ -34,6 +34,19 @@ bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type, return true; } +bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, + const std::vector& addressDeltaIndex, + std::vector& addressDeltaEntries) +{ + if (!fAddressIndex) + return error("Address index not enabled"); + + if (!mempool.getAddressIndex(addressDeltaIndex, addressDeltaEntries)) + return error("Unable to get address delta information"); + + return true; +} + bool GetSpentIndex(const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value) { if (!fSpentIndex) diff --git a/src/rpc/index_util.h b/src/rpc/index_util.h index fa0cd4f212..889a544786 100644 --- a/src/rpc/index_util.h +++ b/src/rpc/index_util.h @@ -24,6 +24,9 @@ bool GetAddressIndex(const uint160& addressHash, const AddressType type, const int32_t start = 0, const int32_t end = 0); bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type, std::vector& unspentOutputs); +bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, + const std::vector& addressDeltaIndex, + std::vector& addressDeltaEntries); bool GetSpentIndex(const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value); bool GetTimestampIndex(const uint32_t high, const uint32_t low, std::vector& hashes); diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 0ec2ddad8b..6c2f8bd664 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -740,20 +740,21 @@ 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 input_addresses; std::vector indexes; - - CTxMemPool& mempool = EnsureAnyMemPool(request.context); - if (!mempool.getAddressIndex(addresses, indexes)) { + for (const auto& [hash, type] : addresses) { + input_addresses.push_back({type, hash}); + } + if (!GetMempoolAddressDeltaIndex(mempool, input_addresses, indexes)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } - std::sort(indexes.begin(), indexes.end(), timestampSort); UniValue result(UniValue::VARR); diff --git a/src/txmempool.cpp b/src/txmempool.cpp index a6f0d84579..b62c9f9fd5 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -470,13 +470,14 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry& entry, const CCoinsViewC mapAddressInserted.insert(std::make_pair(txhash, inserted)); } -bool CTxMemPool::getAddressIndex(const std::vector>& addresses, +bool CTxMemPool::getAddressIndex(const std::vector& addresses, std::vector& results) const { LOCK(cs); for (const auto& address : addresses) { - addressDeltaMap::const_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++; } diff --git a/src/txmempool.h b/src/txmempool.h index 739f139597..12efa08b51 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -636,7 +636,7 @@ public: 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(const std::vector>& addresses, + bool getAddressIndex(const std::vector& addresses, std::vector& results) const; bool removeAddressIndex(const uint256 txhash);