Skip to content

Commit

Permalink
refactor: inline sorting and make available through argument
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jun 28, 2024
1 parent 3e0fcf4 commit 8fb8630
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
19 changes: 17 additions & 2 deletions src/rpc/index_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
std::vector<CAddressUnspentIndexEntry>& unspentOutputs)
std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort)
{
AssertLockHeld(::cs_main);

Expand All @@ -35,19 +35,34 @@ bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressH
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<CMempoolAddressDeltaKey>& addressDeltaIndex,
std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries)
std::vector<CMempoolAddressDeltaEntry>& 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;
}

Expand Down
5 changes: 3 additions & 2 deletions src/rpc/index_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, co
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<CAddressUnspentIndexEntry>& unspentOutputs)
std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort = false)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
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,
CSpentIndexValue& value)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
Expand Down
16 changes: 3 additions & 13 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,6 @@ static bool getAddressesFromParams(const UniValue& params, std::vector<std::pair
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()
{
return RPCHelpMan{"getaddressmempool",
Expand Down Expand Up @@ -752,10 +744,9 @@ static RPCHelpMan getaddressmempool()
for (const auto& [hash, type] : addresses) {
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");
}
std::sort(indexes.begin(), indexes.end(), timestampSort);

UniValue result(UniValue::VARR);

Expand Down Expand Up @@ -825,14 +816,13 @@ static RPCHelpMan getaddressutxos()
{
LOCK(::cs_main);
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");
}
}
}

std::sort(unspentOutputs.begin(), unspentOutputs.end(), heightSort);

UniValue result(UniValue::VARR);

for (const auto& [unspentKey, unspentValue] : unspentOutputs) {
Expand Down

0 comments on commit 8fb8630

Please sign in to comment.