Skip to content

Commit c95784e

Browse files
author
MarcoFalke
committed
Merge #20016: uint256: 1 is a constant
4cc7171 wallet: no need for duplicate storage for ABANDON_HASH constant (Anthony Towns) 82cf464 scripted-diff: Replace UINT256_ONE() with uint256::ONE (Anthony Towns) 183f308 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 4cc7171 MarcoFalke: re ACK 4cc7171, only change is some constexpr shuffling 🛁 kallewoof: ACK 4cc7171 Tree-SHA512: 7f399658bfd9ffa4075bc2349049476d842b9579a67518fb7151f56eab36907ef24b1474ee1e89bdc69fe181abe7295dfe19e33b3623d43cec71fc00e356e347
2 parents e7e6f0b + 4cc7171 commit c95784e

File tree

8 files changed

+21
-19
lines changed

8 files changed

+21
-19
lines changed

src/script/interpreter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
13751375
if ((nHashType & 0x1f) == SIGHASH_SINGLE) {
13761376
if (nIn >= txTo.vout.size()) {
13771377
// nOut out of range
1378-
return UINT256_ONE();
1378+
return uint256::ONE;
13791379
}
13801380
}
13811381

src/test/sighash_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un
2828
{
2929
if (nIn >= txTo.vin.size())
3030
{
31-
return UINT256_ONE();
31+
return uint256::ONE;
3232
}
3333
CMutableTransaction txTmp(txTo);
3434

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

src/test/uint256_tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,10 @@ BOOST_AUTO_TEST_CASE( operator_with_self )
278278
BOOST_CHECK(v == UintToArith256(uint256S("0")));
279279
}
280280

281+
BOOST_AUTO_TEST_CASE( check_ONE )
282+
{
283+
uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");
284+
BOOST_CHECK_EQUAL(one, uint256::ONE);
285+
}
286+
281287
BOOST_AUTO_TEST_SUITE_END()

src/uint256.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,4 @@ template std::string base_blob<256>::ToString() const;
8080
template void base_blob<256>::SetHex(const char*);
8181
template void base_blob<256>::SetHex(const std::string&);
8282

83-
uint256& UINT256_ONE() {
84-
static uint256* one = new uint256(uint256S("0000000000000000000000000000000000000000000000000000000000000001"));
85-
return *one;
86-
}
83+
const uint256 uint256::ONE(1);

src/uint256.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ class base_blob
2020
static constexpr int WIDTH = BITS / 8;
2121
uint8_t m_data[WIDTH];
2222
public:
23-
base_blob()
24-
{
25-
memset(m_data, 0, sizeof(m_data));
26-
}
23+
/* construct 0 value by default */
24+
constexpr base_blob() : m_data() {}
25+
26+
/* constructor for constants between 1 and 255 */
27+
constexpr explicit base_blob(uint8_t v) : m_data{v} {}
2728

2829
explicit base_blob(const std::vector<unsigned char>& vch);
2930

@@ -111,7 +112,7 @@ class base_blob
111112
*/
112113
class uint160 : public base_blob<160> {
113114
public:
114-
uint160() {}
115+
constexpr uint160() {}
115116
explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
116117
};
117118

@@ -122,8 +123,10 @@ class uint160 : public base_blob<160> {
122123
*/
123124
class uint256 : public base_blob<256> {
124125
public:
125-
uint256() {}
126+
constexpr uint256() {}
127+
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
126128
explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
129+
static const uint256 ONE;
127130
};
128131

129132
/* uint256 from const char *.
@@ -147,6 +150,4 @@ inline uint256 uint256S(const std::string& str)
147150
return rv;
148151
}
149152

150-
uint256& UINT256_ONE();
151-
152153
#endif // BITCOIN_UINT256_H

src/wallet/scriptpubkeyman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ std::unique_ptr<CKeyMetadata> LegacyScriptPubKeyMan::GetMetadata(const CTxDestin
655655

656656
uint256 LegacyScriptPubKeyMan::GetID() const
657657
{
658-
return UINT256_ONE();
658+
return uint256::ONE;
659659
}
660660

661661
/**

src/wallet/wallet.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,6 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::strin
324324
return wallet;
325325
}
326326

327-
const uint256 CWalletTx::ABANDON_HASH(UINT256_ONE());
328-
329327
/** @defgroup mapWallet
330328
*
331329
* @{

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class CWalletTx
275275
/** Constant used in hashBlock to indicate tx has been abandoned, only used at
276276
* serialization/deserialization to avoid ambiguity with conflicted.
277277
*/
278-
static const uint256 ABANDON_HASH;
278+
static constexpr const uint256& ABANDON_HASH = uint256::ONE;
279279

280280
public:
281281
/**

0 commit comments

Comments
 (0)