From f011c31b1a2ba123cf034eaacff2dc9b96dbc5ff Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Mon, 25 Sep 2023 15:16:15 +0700 Subject: [PATCH] refactor: make AddressType a strong enum, remove uint8_t for address_type Co-authored-by: Konstantin Akimov --- src/addressindex.cpp | 2 +- src/addressindex.h | 38 +++++++++++++++++++------------------- src/rpc/misc.cpp | 21 +++++++++++---------- src/spentindex.h | 12 ++++++------ src/txdb.cpp | 4 ++-- src/txdb.h | 4 ++-- src/txmempool.cpp | 8 ++++---- src/txmempool.h | 2 +- src/validation.cpp | 16 ++++++++-------- src/validation.h | 4 ++-- 10 files changed, 56 insertions(+), 55 deletions(-) diff --git a/src/addressindex.cpp b/src/addressindex.cpp index 1f9719c9a2..98459c2e3e 100644 --- a/src/addressindex.cpp +++ b/src/addressindex.cpp @@ -24,7 +24,7 @@ inline std::vector TrimScriptP2PK(const T1& input) { return std::vector(input.begin() + 1, input.end() - 1); }; -bool AddressBytesFromScript(const CScript& script, uint8_t& address_type, uint160& address_bytes) { +bool AddressBytesFromScript(const CScript& script, AddressType& address_type, uint160& address_bytes) { if (script.IsPayToScriptHash()) { address_type = AddressType::P2SH; address_bytes = uint160(TrimScriptP2SH(script)); diff --git a/src/addressindex.h b/src/addressindex.h index 5193a92bb9..fec8a66489 100644 --- a/src/addressindex.h +++ b/src/addressindex.h @@ -11,21 +11,21 @@ #include #include #include +#include #include #include class CScript; -namespace AddressType { -enum AddressType { +enum class AddressType : uint8_t { P2PK = 1, P2PKH = 1, P2SH = 2, UNKNOWN = 0 }; -}; /* namespace AddressType */ +template<> struct is_serializable_enum : std::true_type {}; struct CMempoolAddressDelta { @@ -46,21 +46,21 @@ public: struct CMempoolAddressDeltaKey { public: - uint8_t m_address_type{AddressType::UNKNOWN}; + AddressType m_address_type{AddressType::UNKNOWN}; uint160 m_address_bytes; uint256 m_tx_hash; uint32_t m_tx_index{0}; bool m_tx_spent{false}; public: - CMempoolAddressDeltaKey(uint8_t address_type, uint160 address_bytes, uint256 tx_hash, uint32_t tx_index, bool tx_spent) : + CMempoolAddressDeltaKey(AddressType address_type, uint160 address_bytes, uint256 tx_hash, uint32_t tx_index, bool tx_spent) : m_address_type{address_type}, m_address_bytes{address_bytes}, m_tx_hash{tx_hash}, m_tx_index{tx_index}, m_tx_spent{tx_spent} {}; - CMempoolAddressDeltaKey(uint8_t address_type, uint160 address_bytes) : + CMempoolAddressDeltaKey(AddressType address_type, uint160 address_bytes) : m_address_type{address_type}, m_address_bytes{address_bytes} {}; }; @@ -77,7 +77,7 @@ struct CMempoolAddressDeltaKeyCompare struct CAddressIndexKey { public: - uint8_t m_address_type{AddressType::UNKNOWN}; + AddressType m_address_type{AddressType::UNKNOWN}; uint160 m_address_bytes; int32_t m_block_height{0}; uint32_t m_block_tx_pos{0}; @@ -90,7 +90,7 @@ public: SetNull(); } - CAddressIndexKey(uint8_t address_type, uint160 address_bytes, int32_t block_height, uint32_t block_tx_pos, uint256 tx_hash, + CAddressIndexKey(AddressType address_type, uint160 address_bytes, int32_t block_height, uint32_t block_tx_pos, uint256 tx_hash, uint32_t tx_index, bool tx_spent) : m_address_type{address_type}, m_address_bytes{address_bytes}, @@ -117,7 +117,7 @@ public: template void Serialize(Stream& s) const { - ser_writedata8(s, m_address_type); + ser_writedata8(s, ToUnderlying(m_address_type)); m_address_bytes.Serialize(s); // Heights are stored big-endian for key sorting in LevelDB ser_writedata32be(s, m_block_height); @@ -129,7 +129,7 @@ public: template void Unserialize(Stream& s) { - m_address_type = ser_readdata8(s); + m_address_type = static_cast(ser_readdata8(s)); m_address_bytes.Unserialize(s); m_block_height = ser_readdata32be(s); m_block_tx_pos = ser_readdata32be(s); @@ -141,7 +141,7 @@ public: struct CAddressIndexIteratorKey { public: - uint8_t m_address_type{AddressType::UNKNOWN}; + AddressType m_address_type{AddressType::UNKNOWN}; uint160 m_address_bytes; public: @@ -149,7 +149,7 @@ public: SetNull(); } - CAddressIndexIteratorKey(uint8_t address_type, uint160 address_bytes) : + CAddressIndexIteratorKey(AddressType address_type, uint160 address_bytes) : m_address_type{address_type}, m_address_bytes{address_bytes} {}; void SetNull() { @@ -164,20 +164,20 @@ public: template void Serialize(Stream& s) const { - ser_writedata8(s, m_address_type); + ser_writedata8(s, ToUnderlying(m_address_type)); m_address_bytes.Serialize(s); } template void Unserialize(Stream& s) { - m_address_type = ser_readdata8(s); + m_address_type = static_cast(ser_readdata8(s)); m_address_bytes.Unserialize(s); } }; struct CAddressIndexIteratorHeightKey { public: - uint8_t m_address_type{AddressType::UNKNOWN}; + AddressType m_address_type{AddressType::UNKNOWN}; uint160 m_address_bytes; int32_t m_block_height{0}; @@ -186,7 +186,7 @@ public: SetNull(); } - CAddressIndexIteratorHeightKey(uint8_t address_type, uint160 address_bytes, int32_t block_height) : + CAddressIndexIteratorHeightKey(AddressType address_type, uint160 address_bytes, int32_t block_height) : m_address_type{address_type}, m_address_bytes{address_bytes}, m_block_height{block_height} {}; void SetNull() { @@ -202,19 +202,19 @@ public: template void Serialize(Stream& s) const { - ser_writedata8(s, m_address_type); + ser_writedata8(s, ToUnderlying(m_address_type)); m_address_bytes.Serialize(s); ser_writedata32be(s, m_block_height); } template void Unserialize(Stream& s) { - m_address_type = ser_readdata8(s); + m_address_type = static_cast(ser_readdata8(s)); m_address_bytes.Unserialize(s); m_block_height = ser_readdata32be(s); } }; -bool AddressBytesFromScript(const CScript& script, uint8_t& address_type, uint160& address_bytes); +bool AddressBytesFromScript(const CScript& script, AddressType& address_type, uint160& address_bytes); #endif // BITCOIN_ADDRESSINDEX_H diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index bdb9c5f986..7313af1859 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -4,6 +4,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include @@ -593,7 +594,7 @@ static UniValue mnauth(const JSONRPCRequest& request) return fSuccess; } -static bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address) +static bool getAddressFromIndex(const AddressType& type, const uint160 &hash, std::string &address) { if (type == AddressType::P2SH) { address = EncodeDestination(ScriptHash(hash)); @@ -605,7 +606,7 @@ static bool getAddressFromIndex(const int &type, const uint160 &hash, std::strin return true; } -static bool getIndexKey(const std::string& str, uint160& hashBytes, int& type) +static bool getIndexKey(const std::string& str, uint160& hashBytes, AddressType& type) { CTxDestination dest = DecodeDestination(str); if (!IsValidDestination(dest)) { @@ -619,11 +620,11 @@ static bool getIndexKey(const std::string& str, uint160& hashBytes, int& type) return true; } -static bool getAddressesFromParams(const UniValue& params, std::vector > &addresses) +static bool getAddressesFromParams(const UniValue& params, std::vector > &addresses) { if (params[0].isStr()) { uint160 hashBytes; - int type{AddressType::UNKNOWN}; + AddressType type{AddressType::UNKNOWN}; if (!getIndexKey(params[0].get_str(), hashBytes, type)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); } @@ -637,7 +638,7 @@ static bool getAddressesFromParams(const UniValue& params, std::vector > addresses; + std::vector > addresses; if (!getAddressesFromParams(request.params, addresses)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); @@ -760,7 +761,7 @@ static UniValue getaddressutxos(const JSONRPCRequest& request) }, }.Check(request); - std::vector > addresses; + std::vector > addresses; if (!getAddressesFromParams(request.params, addresses)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); @@ -842,7 +843,7 @@ static UniValue getaddressdeltas(const JSONRPCRequest& request) } } - std::vector > addresses; + std::vector > addresses; if (!getAddressesFromParams(request.params, addresses)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); @@ -908,7 +909,7 @@ static UniValue getaddressbalance(const JSONRPCRequest& request) }, }.Check(request); - std::vector > addresses; + std::vector > addresses; if (!getAddressesFromParams(request.params, addresses)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); @@ -973,7 +974,7 @@ static UniValue getaddresstxids(const JSONRPCRequest& request) }, }.Check(request); - std::vector > addresses; + std::vector > addresses; if (!getAddressesFromParams(request.params, addresses)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); diff --git a/src/spentindex.h b/src/spentindex.h index 7bf4e58b07..944ba638ca 100644 --- a/src/spentindex.h +++ b/src/spentindex.h @@ -47,7 +47,7 @@ public: uint32_t m_tx_index{0}; int32_t m_block_height{0}; CAmount m_amount{0}; - uint8_t m_address_type{AddressType::UNKNOWN}; + AddressType m_address_type{AddressType::UNKNOWN}; uint160 m_address_bytes; public: @@ -55,7 +55,7 @@ public: SetNull(); } - CSpentIndexValue(uint256 txin_hash, uint32_t txin_index, int32_t block_height, CAmount amount, uint8_t address_type, + CSpentIndexValue(uint256 txin_hash, uint32_t txin_index, int32_t block_height, CAmount amount, AddressType address_type, uint160 address_bytes) : m_tx_hash{txin_hash}, m_tx_index{txin_index}, @@ -101,7 +101,7 @@ struct CSpentIndexTxInfo struct CAddressUnspentKey { public: - uint8_t m_address_type{AddressType::UNKNOWN}; + AddressType m_address_type{AddressType::UNKNOWN}; uint160 m_address_bytes; uint256 m_tx_hash; uint32_t m_tx_index{0}; @@ -111,7 +111,7 @@ public: SetNull(); } - CAddressUnspentKey(uint8_t address_type, uint160 address_bytes, uint256 tx_hash, uint32_t tx_index) : + CAddressUnspentKey(AddressType address_type, uint160 address_bytes, uint256 tx_hash, uint32_t tx_index) : m_address_type{address_type}, m_address_bytes{address_bytes}, m_tx_hash{tx_hash}, m_tx_index{tx_index} {}; void SetNull() { @@ -128,7 +128,7 @@ public: template void Serialize(Stream& s) const { - ser_writedata8(s, m_address_type); + ser_writedata8(s, ToUnderlying(m_address_type)); m_address_bytes.Serialize(s); m_tx_hash.Serialize(s); ser_writedata32(s, m_tx_index); @@ -136,7 +136,7 @@ public: template void Unserialize(Stream& s) { - m_address_type = ser_readdata8(s); + m_address_type = static_cast(ser_readdata8(s)); m_address_bytes.Unserialize(s); m_tx_hash.Unserialize(s); m_tx_index = ser_readdata32(s); diff --git a/src/txdb.cpp b/src/txdb.cpp index 1fc8094f47..1775204924 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -266,7 +266,7 @@ bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector > &unspentOutputs) { std::unique_ptr pcursor(NewIterator()); @@ -305,7 +305,7 @@ bool CBlockTreeDB::EraseAddressIndex(const std::vector > &addressIndex, int start, int end) { diff --git a/src/txdb.h b/src/txdb.h index 9b73e4780b..48f440bad5 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -109,11 +109,11 @@ public: bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value); bool UpdateSpentIndex(const std::vector >&vect); bool UpdateAddressUnspentIndex(const std::vector >&vect); - bool ReadAddressUnspentIndex(uint160 addressHash, int type, + 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, int type, + bool ReadAddressIndex(uint160 addressHash, AddressType type, std::vector > &addressIndex, int start = 0, int end = 0); bool WriteTimestampIndex(const CTimestampIndexKey ×tampIndex); diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 6ad711829b..ee7a2cf549 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -475,7 +475,7 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC const Coin& coin = view.AccessCoin(input.prevout); const CTxOut &prevout = coin.out; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; if (!AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes)) { @@ -491,7 +491,7 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC for (unsigned int k = 0; k < tx.vout.size(); k++) { const CTxOut &out = tx.vout[k]; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; if (!AddressBytesFromScript(out.scriptPubKey, address_type, address_bytes)) { @@ -506,7 +506,7 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC mapAddressInserted.insert(std::make_pair(txhash, inserted)); } -bool CTxMemPool::getAddressIndex(std::vector > &addresses, +bool CTxMemPool::getAddressIndex(std::vector > &addresses, std::vector > &results) { LOCK(cs); @@ -549,7 +549,7 @@ void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCac const Coin& coin = view.AccessCoin(input.prevout); const CTxOut &prevout = coin.out; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; if (!AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes)) { diff --git a/src/txmempool.h b/src/txmempool.h index ba09ac8999..9a8719b7ae 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -603,7 +603,7 @@ public: void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main); void addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view); - bool getAddressIndex(std::vector > &addresses, + bool getAddressIndex(std::vector > &addresses, std::vector > &results); bool removeAddressIndex(const uint256 txhash); diff --git a/src/validation.cpp b/src/validation.cpp index b574d42ed6..be41201b94 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1037,7 +1037,7 @@ bool GetSpentIndex(CTxMemPool& mempool, CSpentIndexKey &key, CSpentIndexValue &v return true; } -bool GetAddressIndex(uint160 addressHash, int type, +bool GetAddressIndex(uint160 addressHash, AddressType type, std::vector > &addressIndex, int start, int end) { if (!fAddressIndex) @@ -1049,7 +1049,7 @@ bool GetAddressIndex(uint160 addressHash, int type, return true; } -bool GetAddressUnspent(uint160 addressHash, int type, +bool GetAddressUnspent(uint160 addressHash, AddressType type, std::vector > &unspentOutputs) { if (!fAddressIndex) @@ -1706,7 +1706,7 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI for (unsigned int k = tx.vout.size(); k-- > 0;) { const CTxOut &out = tx.vout[k]; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; if (!AddressBytesFromScript(out.scriptPubKey, address_type, address_bytes)) { @@ -1759,7 +1759,7 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI const Coin &coin = view.AccessCoin(tx.vin[j].prevout); const CTxOut &prevout = coin.out; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; if (!AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes)) { @@ -2224,7 +2224,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, const Coin& coin = view.AccessCoin(tx.vin[j].prevout); const CTxOut &prevout = coin.out; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes); @@ -2278,7 +2278,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, for (unsigned int k = 0; k < tx.vout.size(); k++) { const CTxOut &out = tx.vout[k]; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; if (!AddressBytesFromScript(out.scriptPubKey, address_type, address_bytes)) { @@ -4805,7 +4805,7 @@ bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& i const Coin& coin = inputs.AccessCoin(tx->vin[j].prevout); const CTxOut& prevout = coin.out; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes); @@ -4830,7 +4830,7 @@ bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& i for (size_t k = 0; k < tx->vout.size(); k++) { const CTxOut& out = tx->vout[k]; - uint8_t address_type{0}; + AddressType address_type{AddressType::UNKNOWN}; uint160 address_bytes; if (!AddressBytesFromScript(out.scriptPubKey, address_type, address_bytes)) { diff --git a/src/validation.h b/src/validation.h index 38029774a1..1c69b69f48 100644 --- a/src/validation.h +++ b/src/validation.h @@ -307,10 +307,10 @@ public: bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector &hashes); bool GetSpentIndex(CTxMemPool& mempool, CSpentIndexKey &key, CSpentIndexValue &value); -bool GetAddressIndex(uint160 addressHash, int type, +bool GetAddressIndex(uint160 addressHash, AddressType type, std::vector > &addressIndex, int start = 0, int end = 0); -bool GetAddressUnspent(uint160 addressHash, int type, +bool GetAddressUnspent(uint160 addressHash, AddressType type, std::vector > &unspentOutputs); /** Initializes the script-execution cache */ void InitScriptExecutionCache();