refactor: make CBlockTreeDB::Read*Index arguments const

With bonus code formatting. We cannot make the functions themselves
const as the iterators are non-const.
This commit is contained in:
Kittywhiskers Van Gogh 2024-06-27 18:57:01 +00:00
parent 625982e8d2
commit 9a6503d9e8
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
4 changed files with 44 additions and 39 deletions

View File

@ -9,8 +9,9 @@
#include <uint256.h> #include <uint256.h>
#include <validation.h> #include <validation.h>
bool GetAddressIndex(uint160 addressHash, AddressType type, bool GetAddressIndex(const uint160& addressHash, const AddressType type,
std::vector<std::pair<CAddressIndexKey, CAmount>>& addressIndex, int32_t start, int32_t end) std::vector<std::pair<CAddressIndexKey, CAmount>>& addressIndex,
const int32_t start, const int32_t end)
{ {
if (!fAddressIndex) if (!fAddressIndex)
return error("address index not enabled"); return error("address index not enabled");
@ -21,7 +22,7 @@ bool GetAddressIndex(uint160 addressHash, AddressType type,
return true; return true;
} }
bool GetAddressUnspentIndex(uint160 addressHash, AddressType type, bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type,
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& unspentOutputs) std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& unspentOutputs)
{ {
if (!fAddressIndex) if (!fAddressIndex)
@ -33,7 +34,7 @@ bool GetAddressUnspentIndex(uint160 addressHash, AddressType type,
return true; return true;
} }
bool GetSpentIndex(const CTxMemPool& mempool, CSpentIndexKey& key, CSpentIndexValue& value) bool GetSpentIndex(const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value)
{ {
if (!fSpentIndex) if (!fSpentIndex)
return false; return false;

View File

@ -22,12 +22,12 @@ struct CSpentIndexValue;
enum class AddressType : uint8_t; enum class AddressType : uint8_t;
bool GetAddressIndex(uint160 addressHash, AddressType type, bool GetAddressIndex(const uint160& addressHash, const AddressType type,
std::vector<std::pair<CAddressIndexKey, CAmount>>& addressIndex, std::vector<std::pair<CAddressIndexKey, CAmount>>& addressIndex,
int32_t start = 0, int32_t end = 0); const int32_t start = 0, const int32_t end = 0);
bool GetAddressUnspentIndex(uint160 addressHash, AddressType type, bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type,
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& unspentOutputs); std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& unspentOutputs);
bool GetSpentIndex(const CTxMemPool& mempool, CSpentIndexKey& key, CSpentIndexValue& value); bool GetSpentIndex(const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value);
bool GetTimestampIndex(const uint32_t high, const uint32_t low, std::vector<uint256>& hashes); bool GetTimestampIndex(const uint32_t high, const uint32_t low, std::vector<uint256>& hashes);
#endif // BITCOIN_RPC_CLIENT_H #endif // BITCOIN_RPC_CLIENT_H

View File

@ -263,13 +263,13 @@ bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockF
return WriteBatch(batch, true); return WriteBatch(batch, true);
} }
bool CBlockTreeDB::ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) { bool CBlockTreeDB::ReadSpentIndex(const CSpentIndexKey key, CSpentIndexValue& value) {
return Read(std::make_pair(DB_SPENTINDEX, key), value); return Read(std::make_pair(DB_SPENTINDEX, key), value);
} }
bool CBlockTreeDB::UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect) { bool CBlockTreeDB::UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue>>& vect) {
CDBBatch batch(*this); CDBBatch batch(*this);
for (std::vector<std::pair<CSpentIndexKey,CSpentIndexValue> >::const_iterator it=vect.begin(); it!=vect.end(); it++) { for (std::vector<std::pair<CSpentIndexKey,CSpentIndexValue>>::const_iterator it=vect.begin(); it!=vect.end(); it++) {
if (it->second.IsNull()) { if (it->second.IsNull()) {
batch.Erase(std::make_pair(DB_SPENTINDEX, it->first)); batch.Erase(std::make_pair(DB_SPENTINDEX, it->first));
} else { } else {
@ -279,9 +279,9 @@ bool CBlockTreeDB::UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey,
return WriteBatch(batch); return WriteBatch(batch);
} }
bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect) { bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& vect) {
CDBBatch batch(*this); CDBBatch batch(*this);
for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=vect.begin(); it!=vect.end(); it++) { for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>::const_iterator it=vect.begin(); it!=vect.end(); it++) {
if (it->second.IsNull()) { if (it->second.IsNull()) {
batch.Erase(std::make_pair(DB_ADDRESSUNSPENTINDEX, it->first)); batch.Erase(std::make_pair(DB_ADDRESSUNSPENTINDEX, it->first));
} else { } else {
@ -291,9 +291,9 @@ bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector<std::pair<CAddres
return WriteBatch(batch); return WriteBatch(batch);
} }
bool CBlockTreeDB::ReadAddressUnspentIndex(uint160 addressHash, AddressType type, bool CBlockTreeDB::ReadAddressUnspentIndex(const uint160& addressHash, const AddressType type,
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs) { std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& unspentOutputs)
{
std::unique_ptr<CDBIterator> pcursor(NewIterator()); std::unique_ptr<CDBIterator> pcursor(NewIterator());
pcursor->Seek(std::make_pair(DB_ADDRESSUNSPENTINDEX, CAddressIndexIteratorKey(type, addressHash))); pcursor->Seek(std::make_pair(DB_ADDRESSUNSPENTINDEX, CAddressIndexIteratorKey(type, addressHash)));
@ -316,24 +316,24 @@ bool CBlockTreeDB::ReadAddressUnspentIndex(uint160 addressHash, AddressType type
return true; return true;
} }
bool CBlockTreeDB::WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount > >&vect) { bool CBlockTreeDB::WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount>>& vect) {
CDBBatch batch(*this); CDBBatch batch(*this);
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=vect.begin(); it!=vect.end(); it++) for (std::vector<std::pair<CAddressIndexKey, CAmount>>::const_iterator it=vect.begin(); it!=vect.end(); it++)
batch.Write(std::make_pair(DB_ADDRESSINDEX, it->first), it->second); batch.Write(std::make_pair(DB_ADDRESSINDEX, it->first), it->second);
return WriteBatch(batch); return WriteBatch(batch);
} }
bool CBlockTreeDB::EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount > >&vect) { bool CBlockTreeDB::EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount>>& vect) {
CDBBatch batch(*this); CDBBatch batch(*this);
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=vect.begin(); it!=vect.end(); it++) for (std::vector<std::pair<CAddressIndexKey, CAmount>>::const_iterator it=vect.begin(); it!=vect.end(); it++)
batch.Erase(std::make_pair(DB_ADDRESSINDEX, it->first)); batch.Erase(std::make_pair(DB_ADDRESSINDEX, it->first));
return WriteBatch(batch); return WriteBatch(batch);
} }
bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, AddressType type, bool CBlockTreeDB::ReadAddressIndex(const uint160& addressHash, const AddressType type,
std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex, std::vector<std::pair<CAddressIndexKey, CAmount>>& addressIndex,
int start, int end) { const int32_t start, const int32_t end)
{
std::unique_ptr<CDBIterator> pcursor(NewIterator()); std::unique_ptr<CDBIterator> pcursor(NewIterator());
if (start > 0 && end > 0) { if (start > 0 && end > 0) {
@ -363,7 +363,7 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, AddressType type,
return true; return true;
} }
bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey &timestampIndex) { bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey& timestampIndex) {
CDBBatch batch(*this); CDBBatch batch(*this);
batch.Write(std::make_pair(DB_TIMESTAMPINDEX, timestampIndex), 0); batch.Write(std::make_pair(DB_TIMESTAMPINDEX, timestampIndex), 0);
return WriteBatch(batch); return WriteBatch(batch);
@ -376,8 +376,7 @@ bool CBlockTreeDB::EraseTimestampIndex(const CTimestampIndexKey& timestampIndex)
return WriteBatch(batch); return WriteBatch(batch);
} }
bool CBlockTreeDB::ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector<uint256> &hashes) { bool CBlockTreeDB::ReadTimestampIndex(const uint32_t high, const uint32_t low, std::vector<uint256>& hashes) {
std::unique_ptr<CDBIterator> pcursor(NewIterator()); std::unique_ptr<CDBIterator> pcursor(NewIterator());
pcursor->Seek(std::make_pair(DB_TIMESTAMPINDEX, CTimestampIndexIteratorKey(low))); pcursor->Seek(std::make_pair(DB_TIMESTAMPINDEX, CTimestampIndexIteratorKey(low)));

View File

@ -84,19 +84,24 @@ public:
bool ReadLastBlockFile(int &nFile); bool ReadLastBlockFile(int &nFile);
bool WriteReindexing(bool fReindexing); bool WriteReindexing(bool fReindexing);
void ReadReindexing(bool &fReindexing); void ReadReindexing(bool &fReindexing);
bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value);
bool UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect); bool ReadSpentIndex(const CSpentIndexKey key, CSpentIndexValue& value);
bool UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect); bool UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue>>& vect);
bool ReadAddressUnspentIndex(uint160 addressHash, AddressType type,
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &vect); bool ReadAddressUnspentIndex(const uint160& addressHash, const AddressType type,
bool WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount> > &vect); std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& vect);
bool EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount> > &vect); bool UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue>>& vect);
bool ReadAddressIndex(uint160 addressHash, AddressType type,
std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex, bool WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount>>& vect);
int start = 0, int end = 0); bool EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount>>& vect);
bool WriteTimestampIndex(const CTimestampIndexKey &timestampIndex); bool ReadAddressIndex(const uint160& addressHash, const AddressType type,
std::vector<std::pair<CAddressIndexKey, CAmount>>& addressIndex,
const int32_t start = 0, const int32_t end = 0);
bool WriteTimestampIndex(const CTimestampIndexKey& timestampIndex);
bool EraseTimestampIndex(const CTimestampIndexKey& timestampIndex); bool EraseTimestampIndex(const CTimestampIndexKey& timestampIndex);
bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector<uint256> &vect); bool ReadTimestampIndex(const uint32_t high, const uint32_t low, std::vector<uint256>& hashes);
bool WriteFlag(const std::string &name, bool fValue); bool WriteFlag(const std::string &name, bool fValue);
bool ReadFlag(const std::string &name, bool &fValue); bool ReadFlag(const std::string &name, bool &fValue);
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex); bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);