From 5faf29d318bbcfa24770430732c6fb18c0c43f0f Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Tue, 19 Sep 2023 00:09:11 +0530 Subject: [PATCH 01/19] refactor: use scoped weak enum instead of bare values for address type strong enums (enum class) cannot be converted implicitly to another type, requiring you to either use a static_cast or use to_underlying, which is a part of C++23, which this codebase doesn't support. the idea of scoping a weak enum into a namespace is courtesy of https://stackoverflow.com/a/46294875/13845753 --- src/addressindex.h | 10 ++++++++ src/core_write.cpp | 5 ++-- src/rpc/misc.cpp | 12 ++++----- src/spentindex.h | 13 +++++----- src/txmempool.cpp | 20 +++++++-------- src/validation.cpp | 64 +++++++++++++++++++++++----------------------- 6 files changed, 68 insertions(+), 56 deletions(-) diff --git a/src/addressindex.h b/src/addressindex.h index ea8853a947f5c..4efcbb0f50534 100644 --- a/src/addressindex.h +++ b/src/addressindex.h @@ -11,6 +11,16 @@ #include +namespace AddressType { +enum AddressType { + P2PK = 1, + P2PKH = 1, + P2SH = 2, + + UNKNOWN = 0 +}; +}; /* namespace AddressType */ + struct CMempoolAddressDelta { std::chrono::seconds time; diff --git a/src/core_write.cpp b/src/core_write.cpp index 10c82e2149a37..0714dc413cb73 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -215,9 +216,9 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, auto spentInfo = it->second; in.pushKV("value", ValueFromAmount(spentInfo.satoshis)); in.pushKV("valueSat", spentInfo.satoshis); - if (spentInfo.addressType == 1) { + if (spentInfo.addressType == AddressType::P2PK) { in.pushKV("address", EncodeDestination(PKHash(spentInfo.addressHash))); - } else if (spentInfo.addressType == 2) { + } else if (spentInfo.addressType == AddressType::P2SH) { in.pushKV("address", EncodeDestination(ScriptHash(spentInfo.addressHash))); } } diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 56eb970fab868..c6910382ba6f8 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -595,9 +595,9 @@ static UniValue mnauth(const JSONRPCRequest& request) static bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address) { - if (type == 2) { + if (type == AddressType::P2SH) { address = EncodeDestination(ScriptHash(hash)); - } else if (type == 1) { + } else if (type == AddressType::P2PK) { address = EncodeDestination(PKHash(hash)); } else { return false; @@ -609,12 +609,12 @@ static bool getIndexKey(const std::string& str, uint160& hashBytes, int& type) { CTxDestination dest = DecodeDestination(str); if (!IsValidDestination(dest)) { - type = 0; + type = AddressType::UNKNOWN; return false; } const PKHash *pkhash = std::get_if(&dest); const ScriptHash *scriptID = std::get_if(&dest); - type = pkhash ? 1 : 2; + type = pkhash ? AddressType::P2PK : AddressType::P2SH; hashBytes = pkhash ? uint160(*pkhash) : uint160(*scriptID); return true; } @@ -623,7 +623,7 @@ static bool getAddressesFromParams(const UniValue& params, std::vector::iterator it = values.begin(); it != values.end(); ++it) { uint160 hashBytes; - int type = 0; + int type{AddressType::UNKNOWN}; if (!getIndexKey(it->get_str(), hashBytes, type)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); } diff --git a/src/spentindex.h b/src/spentindex.h index 2de0df571d8f9..f563d651d25b5 100644 --- a/src/spentindex.h +++ b/src/spentindex.h @@ -6,10 +6,11 @@ #ifndef BITCOIN_SPENTINDEX_H #define BITCOIN_SPENTINDEX_H -#include +#include #include #include