From 9a6503d9e8d582b0b552be4937748ba6c2bdfd68 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Thu, 27 Jun 2024 18:57:01 +0000 Subject: [PATCH] refactor: make CBlockTreeDB::Read*Index arguments const With bonus code formatting. We cannot make the functions themselves const as the iterators are non-const. --- src/rpc/index_util.cpp | 9 +++++---- src/rpc/index_util.h | 8 ++++---- src/txdb.cpp | 37 ++++++++++++++++++------------------- src/txdb.h | 29 +++++++++++++++++------------ 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/rpc/index_util.cpp b/src/rpc/index_util.cpp index 4b8bf64a61..fd04ef0231 100644 --- a/src/rpc/index_util.cpp +++ b/src/rpc/index_util.cpp @@ -9,8 +9,9 @@ #include #include -bool GetAddressIndex(uint160 addressHash, AddressType type, - std::vector>& addressIndex, int32_t start, int32_t end) +bool GetAddressIndex(const uint160& addressHash, const AddressType type, + std::vector>& addressIndex, + const int32_t start, const int32_t end) { if (!fAddressIndex) return error("address index not enabled"); @@ -21,7 +22,7 @@ bool GetAddressIndex(uint160 addressHash, AddressType type, return true; } -bool GetAddressUnspentIndex(uint160 addressHash, AddressType type, +bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type, std::vector>& unspentOutputs) { if (!fAddressIndex) @@ -33,7 +34,7 @@ bool GetAddressUnspentIndex(uint160 addressHash, AddressType type, return true; } -bool GetSpentIndex(const CTxMemPool& mempool, CSpentIndexKey& key, CSpentIndexValue& value) +bool GetSpentIndex(const CTxMemPool& mempool, const CSpentIndexKey& key, CSpentIndexValue& value) { if (!fSpentIndex) return false; diff --git a/src/rpc/index_util.h b/src/rpc/index_util.h index 15b82eb2a5..adf260b05c 100644 --- a/src/rpc/index_util.h +++ b/src/rpc/index_util.h @@ -22,12 +22,12 @@ struct CSpentIndexValue; enum class AddressType : uint8_t; -bool GetAddressIndex(uint160 addressHash, AddressType type, +bool GetAddressIndex(const uint160& addressHash, const AddressType type, std::vector>& addressIndex, - int32_t start = 0, int32_t end = 0); -bool GetAddressUnspentIndex(uint160 addressHash, AddressType type, + const int32_t start = 0, const int32_t end = 0); +bool GetAddressUnspentIndex(const uint160& addressHash, const AddressType type, std::vector>& 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& hashes); #endif // BITCOIN_RPC_CLIENT_H diff --git a/src/txdb.cpp b/src/txdb.cpp index 9018be6849..f464ca300e 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -263,13 +263,13 @@ bool CBlockTreeDB::WriteBatchSync(const std::vector >&vect) { +bool CBlockTreeDB::UpdateSpentIndex(const std::vector>& vect) { CDBBatch batch(*this); - for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) { + for (std::vector>::const_iterator it=vect.begin(); it!=vect.end(); it++) { if (it->second.IsNull()) { batch.Erase(std::make_pair(DB_SPENTINDEX, it->first)); } else { @@ -279,9 +279,9 @@ bool CBlockTreeDB::UpdateSpentIndex(const std::vector >&vect) { +bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector>& vect) { CDBBatch batch(*this); - for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) { + for (std::vector>::const_iterator it=vect.begin(); it!=vect.end(); it++) { if (it->second.IsNull()) { batch.Erase(std::make_pair(DB_ADDRESSUNSPENTINDEX, it->first)); } else { @@ -291,9 +291,9 @@ bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector > &unspentOutputs) { - +bool CBlockTreeDB::ReadAddressUnspentIndex(const uint160& addressHash, const AddressType type, + std::vector>& unspentOutputs) +{ std::unique_ptr pcursor(NewIterator()); pcursor->Seek(std::make_pair(DB_ADDRESSUNSPENTINDEX, CAddressIndexIteratorKey(type, addressHash))); @@ -316,24 +316,24 @@ bool CBlockTreeDB::ReadAddressUnspentIndex(uint160 addressHash, AddressType type return true; } -bool CBlockTreeDB::WriteAddressIndex(const std::vector >&vect) { +bool CBlockTreeDB::WriteAddressIndex(const std::vector>& vect) { CDBBatch batch(*this); - for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) + for (std::vector>::const_iterator it=vect.begin(); it!=vect.end(); it++) batch.Write(std::make_pair(DB_ADDRESSINDEX, it->first), it->second); return WriteBatch(batch); } -bool CBlockTreeDB::EraseAddressIndex(const std::vector >&vect) { +bool CBlockTreeDB::EraseAddressIndex(const std::vector>& vect) { CDBBatch batch(*this); - for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) + for (std::vector>::const_iterator it=vect.begin(); it!=vect.end(); it++) batch.Erase(std::make_pair(DB_ADDRESSINDEX, it->first)); return WriteBatch(batch); } -bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, AddressType type, - std::vector > &addressIndex, - int start, int end) { - +bool CBlockTreeDB::ReadAddressIndex(const uint160& addressHash, const AddressType type, + std::vector>& addressIndex, + const int32_t start, const int32_t end) +{ std::unique_ptr pcursor(NewIterator()); if (start > 0 && end > 0) { @@ -363,7 +363,7 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, AddressType type, return true; } -bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey ×tampIndex) { +bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey& timestampIndex) { CDBBatch batch(*this); batch.Write(std::make_pair(DB_TIMESTAMPINDEX, timestampIndex), 0); return WriteBatch(batch); @@ -376,8 +376,7 @@ bool CBlockTreeDB::EraseTimestampIndex(const CTimestampIndexKey& timestampIndex) return WriteBatch(batch); } -bool CBlockTreeDB::ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector &hashes) { - +bool CBlockTreeDB::ReadTimestampIndex(const uint32_t high, const uint32_t low, std::vector& hashes) { std::unique_ptr pcursor(NewIterator()); pcursor->Seek(std::make_pair(DB_TIMESTAMPINDEX, CTimestampIndexIteratorKey(low))); diff --git a/src/txdb.h b/src/txdb.h index c2627aa079..b267617400 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -84,19 +84,24 @@ public: bool ReadLastBlockFile(int &nFile); bool WriteReindexing(bool fReindexing); void ReadReindexing(bool &fReindexing); - bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value); - bool UpdateSpentIndex(const std::vector >&vect); - bool UpdateAddressUnspentIndex(const std::vector >&vect); - bool ReadAddressUnspentIndex(uint160 addressHash, AddressType type, - std::vector > &vect); - bool WriteAddressIndex(const std::vector > &vect); - bool EraseAddressIndex(const std::vector > &vect); - bool ReadAddressIndex(uint160 addressHash, AddressType type, - std::vector > &addressIndex, - int start = 0, int end = 0); - bool WriteTimestampIndex(const CTimestampIndexKey ×tampIndex); + + bool ReadSpentIndex(const CSpentIndexKey key, CSpentIndexValue& value); + bool UpdateSpentIndex(const std::vector>& vect); + + bool ReadAddressUnspentIndex(const uint160& addressHash, const AddressType type, + std::vector>& vect); + bool UpdateAddressUnspentIndex(const std::vector>& vect); + + bool WriteAddressIndex(const std::vector>& vect); + bool EraseAddressIndex(const std::vector>& vect); + bool ReadAddressIndex(const uint160& addressHash, const AddressType type, + std::vector>& addressIndex, + const int32_t start = 0, const int32_t end = 0); + + bool WriteTimestampIndex(const CTimestampIndexKey& timestampIndex); bool EraseTimestampIndex(const CTimestampIndexKey& timestampIndex); - bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector &vect); + bool ReadTimestampIndex(const uint32_t high, const uint32_t low, std::vector& hashes); + bool WriteFlag(const std::string &name, bool fValue); bool ReadFlag(const std::string &name, bool &fValue); bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function insertBlockIndex);