refactor: move pair value swapping out of CTxMemPool::getAddressIndex()

This commit is contained in:
Kittywhiskers Van Gogh 2024-06-26 19:24:21 +00:00
parent 808842b1a3
commit ee9d11214e
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
5 changed files with 28 additions and 10 deletions

View File

@ -34,6 +34,19 @@ bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type,
return true; return true;
} }
bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
const std::vector<CMempoolAddressDeltaKey>& addressDeltaIndex,
std::vector<CMempoolAddressDeltaEntry>& 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) bool GetSpentIndex(const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value)
{ {
if (!fSpentIndex) if (!fSpentIndex)

View File

@ -24,6 +24,9 @@ bool GetAddressIndex(const uint160& addressHash, const AddressType type,
const int32_t start = 0, const int32_t end = 0); const int32_t start = 0, const int32_t end = 0);
bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type, bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type,
std::vector<CAddressUnspentIndexEntry>& unspentOutputs); std::vector<CAddressUnspentIndexEntry>& unspentOutputs);
bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
const std::vector<CMempoolAddressDeltaKey>& addressDeltaIndex,
std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries);
bool GetSpentIndex(const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value); bool GetSpentIndex(const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value);
bool GetTimestampIndex(const uint32_t high, const uint32_t low, std::vector<uint256>& hashes); bool GetTimestampIndex(const uint32_t high, const uint32_t low, std::vector<uint256>& hashes);

View File

@ -740,20 +740,21 @@ static RPCHelpMan getaddressmempool()
}, },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
std::vector<std::pair<uint160, AddressType> > addresses; std::vector<std::pair<uint160, AddressType>> addresses;
if (!getAddressesFromParams(request.params, addresses)) { if (!getAddressesFromParams(request.params, addresses)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
} }
std::vector<CMempoolAddressDeltaKey> input_addresses;
std::vector<CMempoolAddressDeltaEntry> indexes; std::vector<CMempoolAddressDeltaEntry> indexes;
for (const auto& [hash, type] : addresses) {
CTxMemPool& mempool = EnsureAnyMemPool(request.context); input_addresses.push_back({type, hash});
if (!mempool.getAddressIndex(addresses, indexes)) { }
if (!GetMempoolAddressDeltaIndex(mempool, input_addresses, indexes)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
} }
std::sort(indexes.begin(), indexes.end(), timestampSort); std::sort(indexes.begin(), indexes.end(), timestampSort);
UniValue result(UniValue::VARR); UniValue result(UniValue::VARR);

View File

@ -470,13 +470,14 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry& entry, const CCoinsViewC
mapAddressInserted.insert(std::make_pair(txhash, inserted)); mapAddressInserted.insert(std::make_pair(txhash, inserted));
} }
bool CTxMemPool::getAddressIndex(const std::vector<std::pair<uint160, AddressType>>& addresses, bool CTxMemPool::getAddressIndex(const std::vector<CMempoolAddressDeltaKey>& addresses,
std::vector<CMempoolAddressDeltaEntry>& results) const std::vector<CMempoolAddressDeltaEntry>& results) const
{ {
LOCK(cs); LOCK(cs);
for (const auto& address : addresses) { for (const auto& address : addresses) {
addressDeltaMap::const_iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey(address.second, address.first)); addressDeltaMap::const_iterator ait = mapAddress.lower_bound(address);
while (ait != mapAddress.end() && (*ait).first.m_address_bytes == address.first && (*ait).first.m_address_type == address.second) { 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); results.push_back(*ait);
ait++; ait++;
} }

View File

@ -636,7 +636,7 @@ public:
void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, 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); void addAddressIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view);
bool getAddressIndex(const std::vector<std::pair<uint160, AddressType>>& addresses, bool getAddressIndex(const std::vector<CMempoolAddressDeltaKey>& addresses,
std::vector<CMempoolAddressDeltaEntry>& results) const; std::vector<CMempoolAddressDeltaEntry>& results) const;
bool removeAddressIndex(const uint256 txhash); bool removeAddressIndex(const uint256 txhash);