mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 03:52:49 +01:00
refactor: removed duplicated code with errors messages for txindex, timestampindex, spentindex
This commit is contained in:
parent
dd1b36636c
commit
a275bda266
@ -870,10 +870,6 @@ static RPCHelpMan getblockhashes()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](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)");
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int high = request.params[0].get_int();
|
unsigned int high = request.params[0].get_int();
|
||||||
unsigned int low = request.params[1].get_int();
|
unsigned int low = request.params[1].get_int();
|
||||||
std::vector<uint256> blockHashes;
|
std::vector<uint256> blockHashes;
|
||||||
|
@ -6,13 +6,17 @@
|
|||||||
#include <rpc/index_util.h>
|
#include <rpc/index_util.h>
|
||||||
|
|
||||||
#include <node/blockstorage.h>
|
#include <node/blockstorage.h>
|
||||||
|
#include <rpc/protocol.h>
|
||||||
|
#include <rpc/request.h>
|
||||||
#include <txdb.h>
|
#include <txdb.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
|
|
||||||
bool IsAddressIndexAvailable()
|
static void EnsureAddressIndexAvailable()
|
||||||
{
|
{
|
||||||
return fAddressIndex;
|
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,
|
bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type,
|
||||||
@ -20,12 +24,11 @@ bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, co
|
|||||||
const int32_t start, const int32_t end)
|
const int32_t start, const int32_t end)
|
||||||
{
|
{
|
||||||
AssertLockHeld(::cs_main);
|
AssertLockHeld(::cs_main);
|
||||||
|
EnsureAddressIndexAvailable();
|
||||||
|
|
||||||
if (!fAddressIndex)
|
if (!block_tree_db.ReadAddressIndex(addressHash, type, addressIndex, start, end)) {
|
||||||
return error("Address index not enabled");
|
|
||||||
|
|
||||||
if (!block_tree_db.ReadAddressIndex(addressHash, type, addressIndex, start, end))
|
|
||||||
return error("Unable to get txids for address");
|
return error("Unable to get txids for address");
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -34,9 +37,7 @@ bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressH
|
|||||||
std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort)
|
std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort)
|
||||||
{
|
{
|
||||||
AssertLockHeld(::cs_main);
|
AssertLockHeld(::cs_main);
|
||||||
|
EnsureAddressIndexAvailable();
|
||||||
if (!fAddressIndex)
|
|
||||||
return error("Address index not enabled");
|
|
||||||
|
|
||||||
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");
|
||||||
@ -56,8 +57,7 @@ bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
|
|||||||
std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries,
|
std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries,
|
||||||
const bool timestamp_sort)
|
const bool timestamp_sort)
|
||||||
{
|
{
|
||||||
if (!fAddressIndex)
|
EnsureAddressIndexAvailable();
|
||||||
return error("Address index not enabled");
|
|
||||||
|
|
||||||
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");
|
||||||
@ -72,18 +72,14 @@ bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsSpentIndexAvailable()
|
|
||||||
{
|
|
||||||
return fSpentIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
AssertLockHeld(::cs_main);
|
AssertLockHeld(::cs_main);
|
||||||
|
|
||||||
if (!fSpentIndex)
|
if (!fSpentIndex) {
|
||||||
return error("Spent index not enabled");
|
throw JSONRPCError(RPC_INVALID_REQUEST, "Spent index is disabled. You should run Dash Core with -spentindex (requires reindex)");
|
||||||
|
}
|
||||||
|
|
||||||
if (mempool.getSpentIndex(key, value))
|
if (mempool.getSpentIndex(key, value))
|
||||||
return true;
|
return true;
|
||||||
@ -94,18 +90,14 @@ bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsTimestampIndexAvailable()
|
|
||||||
{
|
|
||||||
return fTimestampIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low,
|
bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low,
|
||||||
std::vector<uint256>& hashes)
|
std::vector<uint256>& hashes)
|
||||||
{
|
{
|
||||||
AssertLockHeld(::cs_main);
|
AssertLockHeld(::cs_main);
|
||||||
|
|
||||||
if (!fTimestampIndex)
|
if (!fTimestampIndex) {
|
||||||
return error("Timestamp index not enabled");
|
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))
|
if (!block_tree_db.ReadTimestampIndex(high, low, hashes))
|
||||||
return error("Unable to get hashes for timestamps");
|
return error("Unable to get hashes for timestamps");
|
||||||
|
@ -24,25 +24,27 @@ enum class AddressType : uint8_t;
|
|||||||
|
|
||||||
extern RecursiveMutex cs_main;
|
extern RecursiveMutex cs_main;
|
||||||
|
|
||||||
bool IsAddressIndexAvailable();
|
//! throws JSONRPCError if address index is unavailable
|
||||||
bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type,
|
bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type,
|
||||||
std::vector<CAddressIndexEntry>& addressIndex,
|
std::vector<CAddressIndexEntry>& addressIndex,
|
||||||
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);
|
||||||
|
//! throws JSONRPCError if address index is unavailable
|
||||||
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, const bool height_sort = false)
|
std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort = false)
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||||
|
//! throws JSONRPCError if address index is unavailable
|
||||||
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);
|
const bool timestamp_sort = false);
|
||||||
|
|
||||||
bool IsSpentIndexAvailable();
|
//! throws JSONRPCError if spent index is unavailable
|
||||||
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);
|
||||||
|
|
||||||
bool IsTimestampIndexAvailable();
|
//! throws JSONRPCError if timestamp index is unavailable
|
||||||
bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low,
|
bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low,
|
||||||
std::vector<uint256>& hashes)
|
std::vector<uint256>& hashes)
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||||
|
@ -742,10 +742,6 @@ static RPCHelpMan getaddressmempool()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](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)");
|
|
||||||
}
|
|
||||||
|
|
||||||
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||||
|
|
||||||
std::vector<std::pair<uint160, AddressType>> addresses;
|
std::vector<std::pair<uint160, AddressType>> addresses;
|
||||||
@ -818,12 +814,7 @@ static RPCHelpMan getaddressutxos()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](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)");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::pair<uint160, AddressType> > addresses;
|
std::vector<std::pair<uint160, AddressType> > addresses;
|
||||||
|
|
||||||
if (!getAddressesFromParams(request.params, addresses)) {
|
if (!getAddressesFromParams(request.params, addresses)) {
|
||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
|
||||||
}
|
}
|
||||||
@ -894,10 +885,6 @@ static RPCHelpMan getaddressdeltas()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](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)");
|
|
||||||
}
|
|
||||||
|
|
||||||
UniValue startValue = find_value(request.params[0].get_obj(), "start");
|
UniValue startValue = find_value(request.params[0].get_obj(), "start");
|
||||||
UniValue endValue = find_value(request.params[0].get_obj(), "end");
|
UniValue endValue = find_value(request.params[0].get_obj(), "end");
|
||||||
|
|
||||||
@ -988,10 +975,6 @@ static RPCHelpMan getaddressbalance()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](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)");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::pair<uint160, AddressType> > addresses;
|
std::vector<std::pair<uint160, AddressType> > addresses;
|
||||||
|
|
||||||
if (!getAddressesFromParams(request.params, addresses)) {
|
if (!getAddressesFromParams(request.params, addresses)) {
|
||||||
@ -1064,10 +1047,6 @@ static RPCHelpMan getaddresstxids()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](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)");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::pair<uint160, AddressType> > addresses;
|
std::vector<std::pair<uint160, AddressType> > addresses;
|
||||||
|
|
||||||
if (!getAddressesFromParams(request.params, addresses)) {
|
if (!getAddressesFromParams(request.params, addresses)) {
|
||||||
@ -1157,10 +1136,6 @@ static RPCHelpMan getspentinfo()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](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)");
|
|
||||||
}
|
|
||||||
|
|
||||||
UniValue txidValue = find_value(request.params[0].get_obj(), "txid");
|
UniValue txidValue = find_value(request.params[0].get_obj(), "txid");
|
||||||
UniValue indexValue = find_value(request.params[0].get_obj(), "index");
|
UniValue indexValue = find_value(request.params[0].get_obj(), "index");
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo
|
|||||||
|
|
||||||
// Add spent information if spentindex is enabled
|
// Add spent information if spentindex is enabled
|
||||||
CSpentIndexTxInfo txSpentInfo;
|
CSpentIndexTxInfo txSpentInfo;
|
||||||
if (IsSpentIndexAvailable()) {
|
if (fSpentIndex) {
|
||||||
txSpentInfo = CSpentIndexTxInfo{};
|
txSpentInfo = CSpentIndexTxInfo{};
|
||||||
for (const auto& txin : tx.vin) {
|
for (const auto& txin : tx.vin) {
|
||||||
if (!tx.IsCoinBase()) {
|
if (!tx.IsCoinBase()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user