Skip to content

Commit

Permalink
refactor: move exception into function body, allow silencing exception
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Dec 4, 2024
1 parent dd1b366 commit 2cc946a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 38 deletions.
4 changes: 1 addition & 3 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,7 @@ static RPCHelpMan getblockhashes()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!IsTimestampIndexAvailable()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Timestamp index is disabled. You should run Dash Core with -timestampindex (requires reindex)");
}
IsTimestampIndexAvailable();

unsigned int high = request.params[0].get_int();
unsigned int low = request.params[1].get_int();
Expand Down
32 changes: 19 additions & 13 deletions src/rpc/index_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
#include <rpc/index_util.h>

#include <node/blockstorage.h>
#include <rpc/protocol.h>
#include <rpc/request.h>
#include <txdb.h>
#include <txmempool.h>
#include <uint256.h>

bool IsAddressIndexAvailable()
bool IsAddressIndexAvailable(const bool silent)
{
if (!fAddressIndex && !silent) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Address index is disabled. You should run Dash Core with -addressindex (requires reindex)");
}
return fAddressIndex;
}

Expand All @@ -21,8 +26,7 @@ bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, co
{
AssertLockHeld(::cs_main);

if (!fAddressIndex)
return error("Address index not enabled");
IsAddressIndexAvailable();

if (!block_tree_db.ReadAddressIndex(addressHash, type, addressIndex, start, end))
return error("Unable to get txids for address");
Expand All @@ -35,8 +39,7 @@ bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressH
{
AssertLockHeld(::cs_main);

if (!fAddressIndex)
return error("Address index not enabled");
IsAddressIndexAvailable();

if (!block_tree_db.ReadAddressUnspentIndex(addressHash, type, unspentOutputs))
return error("Unable to get txids for address");
Expand All @@ -56,8 +59,7 @@ bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries,
const bool timestamp_sort)
{
if (!fAddressIndex)
return error("Address index not enabled");
IsAddressIndexAvailable();

if (!mempool.getAddressIndex(addressDeltaIndex, addressDeltaEntries))
return error("Unable to get address delta information");
Expand All @@ -72,8 +74,11 @@ bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
return true;
}

bool IsSpentIndexAvailable()
bool IsSpentIndexAvailable(const bool silent)
{
if (!fSpentIndex && !silent) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Spent index is disabled. You should run Dash Core with -spentindex (requires reindex)");
}
return fSpentIndex;
}

Expand All @@ -82,8 +87,7 @@ bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const
{
AssertLockHeld(::cs_main);

if (!fSpentIndex)
return error("Spent index not enabled");
IsSpentIndexAvailable();

if (mempool.getSpentIndex(key, value))
return true;
Expand All @@ -94,8 +98,11 @@ bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const
return true;
}

bool IsTimestampIndexAvailable()
bool IsTimestampIndexAvailable(const bool silent)
{
if (!fTimestampIndex && !silent) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Timestamp index is disabled. You should run Dash Core with -timestampindex (requires reindex)");
}
return fTimestampIndex;
}

Expand All @@ -104,8 +111,7 @@ bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const u
{
AssertLockHeld(::cs_main);

if (!fTimestampIndex)
return error("Timestamp index not enabled");
IsTimestampIndexAvailable();

if (!block_tree_db.ReadTimestampIndex(high, low, hashes))
return error("Unable to get hashes for timestamps");
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/index_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum class AddressType : uint8_t;

extern RecursiveMutex cs_main;

bool IsAddressIndexAvailable();
bool IsAddressIndexAvailable(const bool silent = false);
bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type,
std::vector<CAddressIndexEntry>& addressIndex,
const int32_t start = 0, const int32_t end = 0)
Expand All @@ -37,12 +37,12 @@ bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries,
const bool timestamp_sort = false);

bool IsSpentIndexAvailable();
bool IsSpentIndexAvailable(const bool silent = false);
bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const CSpentIndexKey& key,
CSpentIndexValue& value)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

bool IsTimestampIndexAvailable();
bool IsTimestampIndexAvailable(const bool silent = false);
bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low,
std::vector<uint256>& hashes)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
Expand Down
24 changes: 6 additions & 18 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,7 @@ static RPCHelpMan getaddressmempool()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!IsAddressIndexAvailable()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Address index is disabled. You should run Dash Core with -addressindex (requires reindex)");
}
IsAddressIndexAvailable();

CTxMemPool& mempool = EnsureAnyMemPool(request.context);

Expand Down Expand Up @@ -818,9 +816,7 @@ static RPCHelpMan getaddressutxos()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!IsAddressIndexAvailable()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Address index is disabled. You should run Dash Core with -addressindex (requires reindex)");
}
IsAddressIndexAvailable();

std::vector<std::pair<uint160, AddressType> > addresses;

Expand Down Expand Up @@ -894,9 +890,7 @@ static RPCHelpMan getaddressdeltas()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!IsAddressIndexAvailable()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Address index is disabled. You should run Dash Core with -addressindex (requires reindex)");
}
IsAddressIndexAvailable();

UniValue startValue = find_value(request.params[0].get_obj(), "start");
UniValue endValue = find_value(request.params[0].get_obj(), "end");
Expand Down Expand Up @@ -988,9 +982,7 @@ static RPCHelpMan getaddressbalance()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!IsAddressIndexAvailable()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Address index is disabled. You should run Dash Core with -addressindex (requires reindex)");
}
IsAddressIndexAvailable();

std::vector<std::pair<uint160, AddressType> > addresses;

Expand Down Expand Up @@ -1064,9 +1056,7 @@ static RPCHelpMan getaddresstxids()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!IsAddressIndexAvailable()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Address index is disabled. You should run Dash Core with -addressindex (requires reindex)");
}
IsAddressIndexAvailable();

std::vector<std::pair<uint160, AddressType> > addresses;

Expand Down Expand Up @@ -1157,9 +1147,7 @@ static RPCHelpMan getspentinfo()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!IsSpentIndexAvailable()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "Spent index is disabled. You should run Dash Core with -spentindex (requires reindex)");
}
IsSpentIndexAvailable();

UniValue txidValue = find_value(request.params[0].get_obj(), "txid");
UniValue indexValue = find_value(request.params[0].get_obj(), "index");
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo

// Add spent information if spentindex is enabled
CSpentIndexTxInfo txSpentInfo;
if (IsSpentIndexAvailable()) {
if (IsSpentIndexAvailable(/*silent=*/true)) {
txSpentInfo = CSpentIndexTxInfo{};
for (const auto& txin : tx.vin) {
if (!tx.IsCoinBase()) {
Expand Down

0 comments on commit 2cc946a

Please sign in to comment.