refactor: inline sorting and make available through argument

This commit is contained in:
Kittywhiskers Van Gogh 2024-06-28 08:17:42 +00:00
parent 3e0fcf471f
commit 8fb863008e
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 23 additions and 17 deletions

View File

@ -25,7 +25,7 @@ bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, co
} }
bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type,
std::vector<CAddressUnspentIndexEntry>& unspentOutputs) std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort)
{ {
AssertLockHeld(::cs_main); AssertLockHeld(::cs_main);
@ -35,12 +35,20 @@ bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressH
if (!block_tree_db.ReadAddressUnspentIndex(addressHash, type, unspentOutputs)) if (!block_tree_db.ReadAddressUnspentIndex(addressHash, type, unspentOutputs))
return error("Unable to get txids for address"); 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; return true;
} }
bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
const std::vector<CMempoolAddressDeltaKey>& addressDeltaIndex, const std::vector<CMempoolAddressDeltaKey>& addressDeltaIndex,
std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries) std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries,
const bool timestamp_sort)
{ {
if (!fAddressIndex) if (!fAddressIndex)
return error("Address index not enabled"); return error("Address index not enabled");
@ -48,6 +56,13 @@ bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
if (!mempool.getAddressIndex(addressDeltaIndex, addressDeltaEntries)) if (!mempool.getAddressIndex(addressDeltaIndex, addressDeltaEntries))
return error("Unable to get address delta information"); 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; return true;
} }

View File

@ -29,11 +29,12 @@ bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, co
const int32_t start = 0, const int32_t end = 0) const int32_t start = 0, const int32_t end = 0)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main); EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type,
std::vector<CAddressUnspentIndexEntry>& unspentOutputs) std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort = false)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main); EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
const std::vector<CMempoolAddressDeltaKey>& addressDeltaIndex, const std::vector<CMempoolAddressDeltaKey>& addressDeltaIndex,
std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries); std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries,
const bool timestamp_sort = false);
bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const CSpentIndexKey& key, bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const CSpentIndexKey& key,
CSpentIndexValue& value) CSpentIndexValue& value)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main); EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

View File

@ -701,14 +701,6 @@ static bool getAddressesFromParams(const UniValue& params, std::vector<std::pair
return true; return true;
} }
static bool heightSort(CAddressUnspentIndexEntry a, CAddressUnspentIndexEntry b) {
return a.second.m_block_height < b.second.m_block_height;
}
static bool timestampSort(CMempoolAddressDeltaEntry a, CMempoolAddressDeltaEntry b) {
return a.second.m_time < b.second.m_time;
}
static RPCHelpMan getaddressmempool() static RPCHelpMan getaddressmempool()
{ {
return RPCHelpMan{"getaddressmempool", return RPCHelpMan{"getaddressmempool",
@ -752,10 +744,9 @@ static RPCHelpMan getaddressmempool()
for (const auto& [hash, type] : addresses) { for (const auto& [hash, type] : addresses) {
input_addresses.push_back({type, hash}); input_addresses.push_back({type, hash});
} }
if (!GetMempoolAddressDeltaIndex(mempool, input_addresses, indexes)) { if (!GetMempoolAddressDeltaIndex(mempool, input_addresses, indexes, /* timestamp_sort = */ true)) {
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);
UniValue result(UniValue::VARR); UniValue result(UniValue::VARR);
@ -825,14 +816,13 @@ static RPCHelpMan getaddressutxos()
{ {
LOCK(::cs_main); LOCK(::cs_main);
for (const auto& address : addresses) { for (const auto& address : addresses) {
if (!GetAddressUnspentIndex(*pblocktree, address.first, address.second, unspentOutputs)) { if (!GetAddressUnspentIndex(*pblocktree, address.first, address.second, unspentOutputs,
/* height_sort = */ true)) {
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(unspentOutputs.begin(), unspentOutputs.end(), heightSort);
UniValue result(UniValue::VARR); UniValue result(UniValue::VARR);
for (const auto& [unspentKey, unspentValue] : unspentOutputs) { for (const auto& [unspentKey, unspentValue] : unspentOutputs) {