From 183f308fff4caad3e3ada654b6fdf597d262c4c1 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Fri, 25 Sep 2020 15:01:38 +1000 Subject: [PATCH 1/3] uint256: Update constructors to c++11, make ONE static Replace the memset with C++11 value/aggregate initialisation of the m_data array, which still ensures the unspecified values end up as zero-initialised. This then allows changing UINT256_ONE() from dynamically allocating an object, to a simpler referencing a static allocation. --- src/test/uint256_tests.cpp | 6 ++++++ src/uint256.cpp | 5 +---- src/uint256.h | 17 ++++++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index c0ae2f8cf2..ae626d4613 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -278,4 +278,10 @@ BOOST_AUTO_TEST_CASE( operator_with_self ) BOOST_CHECK(v == UintToArith256(uint256S("0"))); } +BOOST_AUTO_TEST_CASE( check_ONE ) +{ + uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001"); + BOOST_CHECK_EQUAL(one, uint256::ONE); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/uint256.cpp b/src/uint256.cpp index ee1b34eadd..d074df2f20 100644 --- a/src/uint256.cpp +++ b/src/uint256.cpp @@ -80,7 +80,4 @@ template std::string base_blob<256>::ToString() const; template void base_blob<256>::SetHex(const char*); template void base_blob<256>::SetHex(const std::string&); -uint256& UINT256_ONE() { - static uint256* one = new uint256(uint256S("0000000000000000000000000000000000000000000000000000000000000001")); - return *one; -} +const uint256 uint256::ONE(1); diff --git a/src/uint256.h b/src/uint256.h index 8ab747ef49..3e245763b3 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -20,10 +20,11 @@ class base_blob static constexpr int WIDTH = BITS / 8; uint8_t m_data[WIDTH]; public: - base_blob() - { - memset(m_data, 0, sizeof(m_data)); - } + /* construct 0 value by default */ + constexpr base_blob() : m_data() {} + + /* constructor for constants between 1 and 255 */ + constexpr explicit base_blob(uint8_t v) : m_data{v} {} explicit base_blob(const std::vector& vch); @@ -111,7 +112,7 @@ class base_blob */ class uint160 : public base_blob<160> { public: - uint160() {} + constexpr uint160() {} explicit uint160(const std::vector& vch) : base_blob<160>(vch) {} }; @@ -122,8 +123,10 @@ class uint160 : public base_blob<160> { */ class uint256 : public base_blob<256> { public: - uint256() {} + constexpr uint256() {} + constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {} explicit uint256(const std::vector& vch) : base_blob<256>(vch) {} + static const uint256 ONE; }; /* uint256 from const char *. @@ -147,6 +150,6 @@ inline uint256 uint256S(const std::string& str) return rv; } -uint256& UINT256_ONE(); +inline const uint256& UINT256_ONE() { return uint256::ONE; } #endif // BITCOIN_UINT256_H From 82cf4641f4a161834d07ce83c18982d9b143c040 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sat, 26 Sep 2020 16:01:56 +1000 Subject: [PATCH 2/3] scripted-diff: Replace UINT256_ONE() with uint256::ONE -BEGIN VERIFY SCRIPT- sed -i '/inline.* UINT256_ONE() {/,+1d' src/uint256.h sed -i 's/UINT256_ONE()/uint256::ONE/' $(git grep -l UINT256_ONE) -END VERIFY SCRIPT- --- src/script/interpreter.cpp | 2 +- src/test/sighash_tests.cpp | 4 ++-- src/uint256.h | 2 -- src/wallet/scriptpubkeyman.cpp | 2 +- src/wallet/wallet.cpp | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index 7b2457a5e3..50a6192476 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1375,7 +1375,7 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn if ((nHashType & 0x1f) == SIGHASH_SINGLE) { if (nIn >= txTo.vout.size()) { // nOut out of range - return UINT256_ONE(); + return uint256::ONE; } } diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp index c0bb92258b..bc862de78a 100644 --- a/src/test/sighash_tests.cpp +++ b/src/test/sighash_tests.cpp @@ -28,7 +28,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un { if (nIn >= txTo.vin.size()) { - return UINT256_ONE(); + return uint256::ONE; } CMutableTransaction txTmp(txTo); @@ -58,7 +58,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un unsigned int nOut = nIn; if (nOut >= txTmp.vout.size()) { - return UINT256_ONE(); + return uint256::ONE; } txTmp.vout.resize(nOut+1); for (unsigned int i = 0; i < nOut; i++) diff --git a/src/uint256.h b/src/uint256.h index 3e245763b3..c55cb31456 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -150,6 +150,4 @@ inline uint256 uint256S(const std::string& str) return rv; } -inline const uint256& UINT256_ONE() { return uint256::ONE; } - #endif // BITCOIN_UINT256_H diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 51715462c5..435716e56a 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -655,7 +655,7 @@ std::unique_ptr LegacyScriptPubKeyMan::GetMetadata(const CTxDestin uint256 LegacyScriptPubKeyMan::GetID() const { - return UINT256_ONE(); + return uint256::ONE; } /** diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 66857dbb39..d7a13644df 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -324,7 +324,7 @@ std::shared_ptr CreateWallet(interfaces::Chain& chain, const std::strin return wallet; } -const uint256 CWalletTx::ABANDON_HASH(UINT256_ONE()); +const uint256 CWalletTx::ABANDON_HASH(uint256::ONE); /** @defgroup mapWallet * From 4cc7171c9887e88027642927c979e507d7b78dda Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sat, 26 Sep 2020 16:41:10 +1000 Subject: [PATCH 3/3] wallet: no need for duplicate storage for ABANDON_HASH constant --- src/wallet/wallet.cpp | 2 -- src/wallet/wallet.h | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index d7a13644df..6f320096eb 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -324,8 +324,6 @@ std::shared_ptr CreateWallet(interfaces::Chain& chain, const std::strin return wallet; } -const uint256 CWalletTx::ABANDON_HASH(uint256::ONE); - /** @defgroup mapWallet * * @{ diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index f15712dd0e..169f266980 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -275,7 +275,7 @@ class CWalletTx /** Constant used in hashBlock to indicate tx has been abandoned, only used at * serialization/deserialization to avoid ambiguity with conflicted. */ - static const uint256 ABANDON_HASH; + static constexpr const uint256& ABANDON_HASH = uint256::ONE; public: /**