mirror of
https://github.com/dashpay/dash.git
synced 2024-12-24 19:42:46 +01:00
refactor: inline sorting and make available through argument
This commit is contained in:
parent
3e0fcf471f
commit
8fb863008e
@ -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);
|
||||
|
||||
@ -35,12 +35,20 @@ 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");
|
||||
@ -48,6 +56,13 @@ bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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",
|
||||
@ -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);
|
||||
|
||||
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user