Merge #20016: uint256: 1 is a constant

4cc7171c9887e88027642927c979e507d7b78dda wallet: no need for duplicate storage for ABANDON_HASH constant (Anthony Towns)
82cf4641f4a161834d07ce83c18982d9b143c040 scripted-diff: Replace UINT256_ONE() with uint256::ONE (Anthony Towns)
183f308fff4caad3e3ada654b6fdf597d262c4c1 uint256: Update constructors to c++11, make ONE static (Anthony Towns)

Pull request description:

  `UINT256_ONE()` returns a reference to a global; mark it as const to be sure someone doesn't accidently modify it.

ACKs for top commit:
  promag:
    ACK 4cc7171c9887e88027642927c979e507d7b78dda
  MarcoFalke:
    re ACK 4cc7171c98, only change is some constexpr shuffling 🛁
  kallewoof:
    ACK 4cc7171c9887e88027642927c979e507d7b78dda

Tree-SHA512: 7f399658bfd9ffa4075bc2349049476d842b9579a67518fb7151f56eab36907ef24b1474ee1e89bdc69fe181abe7295dfe19e33b3623d43cec71fc00e356e347
This commit is contained in:
MarcoFalke 2020-09-28 09:16:47 +02:00
commit c95784e3d3
No known key found for this signature in database
GPG Key ID: D2EA4850E7528B25
8 changed files with 21 additions and 19 deletions

View File

@ -1375,7 +1375,7 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
if ((nHashType & 0x1f) == SIGHASH_SINGLE) { if ((nHashType & 0x1f) == SIGHASH_SINGLE) {
if (nIn >= txTo.vout.size()) { if (nIn >= txTo.vout.size()) {
// nOut out of range // nOut out of range
return UINT256_ONE(); return uint256::ONE;
} }
} }

View File

@ -28,7 +28,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un
{ {
if (nIn >= txTo.vin.size()) if (nIn >= txTo.vin.size())
{ {
return UINT256_ONE(); return uint256::ONE;
} }
CMutableTransaction txTmp(txTo); CMutableTransaction txTmp(txTo);
@ -58,7 +58,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un
unsigned int nOut = nIn; unsigned int nOut = nIn;
if (nOut >= txTmp.vout.size()) if (nOut >= txTmp.vout.size())
{ {
return UINT256_ONE(); return uint256::ONE;
} }
txTmp.vout.resize(nOut+1); txTmp.vout.resize(nOut+1);
for (unsigned int i = 0; i < nOut; i++) for (unsigned int i = 0; i < nOut; i++)

View File

@ -278,4 +278,10 @@ BOOST_AUTO_TEST_CASE( operator_with_self )
BOOST_CHECK(v == UintToArith256(uint256S("0"))); 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() BOOST_AUTO_TEST_SUITE_END()

View File

@ -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 char*);
template void base_blob<256>::SetHex(const std::string&); template void base_blob<256>::SetHex(const std::string&);
uint256& UINT256_ONE() { const uint256 uint256::ONE(1);
static uint256* one = new uint256(uint256S("0000000000000000000000000000000000000000000000000000000000000001"));
return *one;
}

View File

@ -20,10 +20,11 @@ protected:
static constexpr int WIDTH = BITS / 8; static constexpr int WIDTH = BITS / 8;
uint8_t m_data[WIDTH]; uint8_t m_data[WIDTH];
public: public:
base_blob() /* construct 0 value by default */
{ constexpr base_blob() : m_data() {}
memset(m_data, 0, sizeof(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<unsigned char>& vch); explicit base_blob(const std::vector<unsigned char>& vch);
@ -111,7 +112,7 @@ public:
*/ */
class uint160 : public base_blob<160> { class uint160 : public base_blob<160> {
public: public:
uint160() {} constexpr uint160() {}
explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {} explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
}; };
@ -122,8 +123,10 @@ public:
*/ */
class uint256 : public base_blob<256> { class uint256 : public base_blob<256> {
public: public:
uint256() {} constexpr uint256() {}
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {} explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
static const uint256 ONE;
}; };
/* uint256 from const char *. /* uint256 from const char *.
@ -147,6 +150,4 @@ inline uint256 uint256S(const std::string& str)
return rv; return rv;
} }
uint256& UINT256_ONE();
#endif // BITCOIN_UINT256_H #endif // BITCOIN_UINT256_H

View File

@ -655,7 +655,7 @@ std::unique_ptr<CKeyMetadata> LegacyScriptPubKeyMan::GetMetadata(const CTxDestin
uint256 LegacyScriptPubKeyMan::GetID() const uint256 LegacyScriptPubKeyMan::GetID() const
{ {
return UINT256_ONE(); return uint256::ONE;
} }
/** /**

View File

@ -324,8 +324,6 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::strin
return wallet; return wallet;
} }
const uint256 CWalletTx::ABANDON_HASH(UINT256_ONE());
/** @defgroup mapWallet /** @defgroup mapWallet
* *
* @{ * @{

View File

@ -275,7 +275,7 @@ private:
/** Constant used in hashBlock to indicate tx has been abandoned, only used at /** Constant used in hashBlock to indicate tx has been abandoned, only used at
* serialization/deserialization to avoid ambiguity with conflicted. * serialization/deserialization to avoid ambiguity with conflicted.
*/ */
static const uint256 ABANDON_HASH; static constexpr const uint256& ABANDON_HASH = uint256::ONE;
public: public:
/** /**