diff --git a/src/rpc/index_util.cpp b/src/rpc/index_util.cpp index abe6912868787..30ca50caa10cb 100644 --- a/src/rpc/index_util.cpp +++ b/src/rpc/index_util.cpp @@ -6,21 +6,29 @@ #include #include +#include +#include #include #include #include +static void EnsureAddressIndexAvailable() +{ + if (!fAddressIndex) { + throw JSONRPCError(RPC_INVALID_REQUEST, "Address index is disabled. You should run Dash Core with -addressindex (requires reindex)"); + } +} + bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, std::vector& addressIndex, const int32_t start, const int32_t end) { AssertLockHeld(::cs_main); + EnsureAddressIndexAvailable(); - if (!fAddressIndex) - return error("Address index not enabled"); - - if (!block_tree_db.ReadAddressIndex(addressHash, type, addressIndex, start, end)) + if (!block_tree_db.ReadAddressIndex(addressHash, type, addressIndex, start, end)) { return error("Unable to get txids for address"); + } return true; } @@ -29,9 +37,7 @@ bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressH std::vector& unspentOutputs, const bool height_sort) { AssertLockHeld(::cs_main); - - if (!fAddressIndex) - return error("Address index not enabled"); + EnsureAddressIndexAvailable(); if (!block_tree_db.ReadAddressUnspentIndex(addressHash, type, unspentOutputs)) return error("Unable to get txids for address"); @@ -51,8 +57,7 @@ bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, std::vector& addressDeltaEntries, const bool timestamp_sort) { - if (!fAddressIndex) - return error("Address index not enabled"); + EnsureAddressIndexAvailable(); if (!mempool.getAddressIndex(addressDeltaIndex, addressDeltaEntries)) return error("Unable to get address delta information"); @@ -72,8 +77,9 @@ bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const { AssertLockHeld(::cs_main); - if (!fSpentIndex) - return error("Spent index not enabled"); + if (!fSpentIndex) { + throw JSONRPCError(RPC_INVALID_REQUEST, "Spent index is disabled. You should run Dash Core with -spentindex (requires reindex)"); + } if (mempool.getSpentIndex(key, value)) return true; @@ -89,8 +95,9 @@ bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const u { AssertLockHeld(::cs_main); - if (!fTimestampIndex) - return error("Timestamp index not enabled"); + if (!fTimestampIndex) { + throw JSONRPCError(RPC_INVALID_REQUEST, "Timestamp index is disabled. You should run Dash Core with -timestampindex (requires reindex)"); + } if (!block_tree_db.ReadTimestampIndex(high, low, hashes)) return error("Unable to get hashes for timestamps"); diff --git a/src/rpc/index_util.h b/src/rpc/index_util.h index a2a632c8d14fc..5f92de755f6d8 100644 --- a/src/rpc/index_util.h +++ b/src/rpc/index_util.h @@ -24,20 +24,27 @@ enum class AddressType : uint8_t; extern RecursiveMutex cs_main; +//! throws JSONRPCError if address index is unavailable bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, std::vector& addressIndex, const int32_t start = 0, const int32_t end = 0) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); +//! throws JSONRPCError if address index is unavailable bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, std::vector& unspentOutputs, const bool height_sort = false) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); +//! throws JSONRPCError if address index is unavailable bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, const std::vector& addressDeltaIndex, std::vector& addressDeltaEntries, const bool timestamp_sort = false); + +//! throws JSONRPCError if spent index is unavailable bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + +//! throws JSONRPCError if timestamp index is unavailable bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low, std::vector& hashes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 0b8acb5d5cb99..d9d37ed3d3e66 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -810,9 +810,7 @@ static RPCHelpMan getaddressutxos() }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - std::vector > addresses; - if (!getAddressesFromParams(request.params, addresses)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); } @@ -883,8 +881,6 @@ static RPCHelpMan getaddressdeltas() }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - - UniValue startValue = find_value(request.params[0].get_obj(), "start"); UniValue endValue = find_value(request.params[0].get_obj(), "end"); @@ -975,7 +971,6 @@ static RPCHelpMan getaddressbalance() }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - std::vector > addresses; if (!getAddressesFromParams(request.params, addresses)) { @@ -1048,7 +1043,6 @@ static RPCHelpMan getaddresstxids() }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - std::vector > addresses; if (!getAddressesFromParams(request.params, addresses)) { @@ -1138,7 +1132,6 @@ static RPCHelpMan getspentinfo() }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - UniValue txidValue = find_value(request.params[0].get_obj(), "txid"); UniValue indexValue = find_value(request.params[0].get_obj(), "index"); diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 09e7c7d862397..78ae6ec3a51d3 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -71,27 +71,32 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo // data into the returned UniValue. uint256 txid = tx.GetHash(); + CSpentIndexTxInfo *txSpentInfoPtr{nullptr}; // Add spent information if spentindex is enabled CSpentIndexTxInfo txSpentInfo; - for (const auto& txin : tx.vin) { - if (!tx.IsCoinBase()) { + if (fSpentIndex) { + txSpentInfo = CSpentIndexTxInfo{}; + for (const auto& txin : tx.vin) { + if (!tx.IsCoinBase()) { + CSpentIndexValue spentInfo; + CSpentIndexKey spentKey(txin.prevout.hash, txin.prevout.n); + if (GetSpentIndex(*active_chainstate.m_blockman.m_block_tree_db, mempool, spentKey, spentInfo)) { + txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo); + } + } + } + for (unsigned int i = 0; i < tx.vout.size(); i++) { CSpentIndexValue spentInfo; - CSpentIndexKey spentKey(txin.prevout.hash, txin.prevout.n); + CSpentIndexKey spentKey(txid, i); if (GetSpentIndex(*active_chainstate.m_blockman.m_block_tree_db, mempool, spentKey, spentInfo)) { txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo); } } - } - for (unsigned int i = 0; i < tx.vout.size(); i++) { - CSpentIndexValue spentInfo; - CSpentIndexKey spentKey(txid, i); - if (GetSpentIndex(*active_chainstate.m_blockman.m_block_tree_db, mempool, spentKey, spentInfo)) { - txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo); - } + txSpentInfoPtr = &txSpentInfo; } - TxToUniv(tx, uint256(), entry, true, /* txundo = */ nullptr, &txSpentInfo); + TxToUniv(tx, uint256(), entry, true, /* txundo = */ nullptr, txSpentInfoPtr); bool chainLock = false; if (!hashBlock.IsNull()) {