Skip to content

Commit c06f236

Browse files
committed
refactor: Hand-replace some uint256S -> uint256
chainparams.cpp - workaround for MSVC bug triggering C7595 - Calling consteval constructors in initializer lists fails, but works on GCC (13.2.0) & Clang (17.0.6).
1 parent b74d8d5 commit c06f236

File tree

9 files changed

+33
-23
lines changed

9 files changed

+33
-23
lines changed

contrib/devtools/test_utxo_snapshots.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ echo
138138
echo "-- Now: add the following to CMainParams::m_assumeutxo_data"
139139
echo " in src/kernel/chainparams.cpp, and recompile:"
140140
echo
141-
echo " {${RPC_BASE_HEIGHT}, AssumeutxoHash{uint256S(\"0x${RPC_AU}\")}, ${RPC_NCHAINTX}, uint256S(\"0x${RPC_BLOCKHASH}\")},"
141+
echo " {.height = ${RPC_BASE_HEIGHT}, .hash_serialized = AssumeutxoHash{uint256{\"${RPC_AU}\"}}, .m_chain_tx_count = ${RPC_NCHAINTX}, .blockhash = consteval_ctor(uint256{\"${RPC_BLOCKHASH}\"})},"
142142
echo
143143
echo
144144
echo "-- IBDing more blocks to the server node (height=$FINAL_HEIGHT) so there is a diff between snapshot and tip..."

src/kernel/chainparams.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
#include <cstring>
2727
#include <type_traits>
2828

29+
// Workaround MSVC bug triggering C7595 when calling consteval constructors in
30+
// initializer lists.
31+
// A fix may be on the way:
32+
// https://developercommunity.visualstudio.com/t/consteval-conversion-function-fails/1579014
33+
#if defined(_MSC_VER)
34+
auto consteval_ctor(auto&& input) { return input; }
35+
#else
36+
#define consteval_ctor(input) (input)
37+
#endif
38+
2939
static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
3040
{
3141
CMutableTransaction txNew;
@@ -273,7 +283,7 @@ class CTestNetParams : public CChainParams {
273283
.height = 2'500'000,
274284
.hash_serialized = AssumeutxoHash{uint256S("0xf841584909f68e47897952345234e37fcd9128cd818f41ee6c3ca68db8071be7")},
275285
.m_chain_tx_count = 66484552,
276-
.blockhash = uint256S("0x0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f")
286+
.blockhash = consteval_ctor(uint256{"0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f"}),
277287
}
278288
};
279289

@@ -383,7 +393,7 @@ class SigNetParams : public CChainParams {
383393
.height = 160'000,
384394
.hash_serialized = AssumeutxoHash{uint256S("0xfe0a44309b74d6b5883d246cb419c6221bcccf0b308c9b59b7d70783dbdf928a")},
385395
.m_chain_tx_count = 2289496,
386-
.blockhash = uint256S("0x0000003ca3c99aff040f2563c2ad8f8ec88bd0fd6b8f0895cfaf1ef90353a62c")
396+
.blockhash = consteval_ctor(uint256{"0000003ca3c99aff040f2563c2ad8f8ec88bd0fd6b8f0895cfaf1ef90353a62c"}),
387397
}
388398
};
389399

@@ -499,21 +509,21 @@ class CRegTestParams : public CChainParams
499509
.height = 110,
500510
.hash_serialized = AssumeutxoHash{uint256S("0x6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1")},
501511
.m_chain_tx_count = 111,
502-
.blockhash = uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c")
512+
.blockhash = consteval_ctor(uint256{"696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c"}),
503513
},
504514
{
505515
// For use by fuzz target src/test/fuzz/utxo_snapshot.cpp
506516
.height = 200,
507517
.hash_serialized = AssumeutxoHash{uint256S("0x4f34d431c3e482f6b0d67b64609ece3964dc8d7976d02ac68dd7c9c1421738f2")},
508518
.m_chain_tx_count = 201,
509-
.blockhash = uint256S("0x5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"),
519+
.blockhash = consteval_ctor(uint256{"5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"}),
510520
},
511521
{
512522
// For use by test/functional/feature_assumeutxo.py
513523
.height = 299,
514524
.hash_serialized = AssumeutxoHash{uint256S("0xa4bf3407ccb2cc0145c49ebba8fa91199f8a3903daf0883875941497d2493c27")},
515525
.m_chain_tx_count = 334,
516-
.blockhash = uint256S("0x3bb7ce5eba0be48939b7a521ac1ba9316afee2c7bada3a0cca24188e6d7d96c0")
526+
.blockhash = consteval_ctor(uint256{"3bb7ce5eba0be48939b7a521ac1ba9316afee2c7bada3a0cca24188e6d7d96c0"}),
517527
},
518528
};
519529

src/test/fuzz/block_header.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ FUZZ_TARGET(block_header)
2323
}
2424
{
2525
const uint256 hash = block_header->GetHash();
26-
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
26+
constexpr uint256 u256_max{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"};
2727
assert(hash != u256_max);
2828
assert(block_header->GetBlockTime() == block_header->nTime);
2929
assert(block_header->IsNull() == (block_header->nBits == 0));

src/test/fuzz/integer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ FUZZ_TARGET(integer, .init = initialize_integer)
7878
} else {
7979
(void)CompressAmount(u64);
8080
}
81-
static const uint256 u256_min(uint256S("0000000000000000000000000000000000000000000000000000000000000000"));
82-
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
81+
constexpr uint256 u256_min{"0000000000000000000000000000000000000000000000000000000000000000"};
82+
constexpr uint256 u256_max{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"};
8383
const std::vector<uint256> v256{u256, u256_min, u256_max};
8484
(void)ComputeMerkleRoot(v256);
8585
(void)DecompressAmount(u64);

src/test/fuzz/miniscript.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ struct TestData {
4747
void Init() {
4848
unsigned char keydata[32] = {1};
4949
// All our signatures sign (and are required to sign) this constant message.
50-
auto const MESSAGE_HASH{uint256S("f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065")};
50+
constexpr uint256 MESSAGE_HASH{"0000000000000000f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065"};
5151
// We don't pass additional randomness when creating a schnorr signature.
52-
auto const EMPTY_AUX{uint256S("")};
52+
const auto EMPTY_AUX{uint256::ZERO};
5353

5454
for (size_t i = 0; i < 256; i++) {
5555
keydata[31] = i;

src/test/fuzz/muhash.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ FUZZ_TARGET(muhash)
2020
muhash.Insert(data);
2121
muhash.Insert(data2);
2222

23-
const std::string initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
23+
constexpr uint256 initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
2424
uint256 out;
2525
uint256 out2;
2626
CallOneOf(
@@ -57,14 +57,14 @@ FUZZ_TARGET(muhash)
5757
#endif
5858

5959
muhash.Finalize(out);
60-
out2 = uint256S(initial_state_hash);
60+
out2 = initial_state_hash;
6161
},
6262
[&] {
6363
// Test that removing all added elements brings the object back to it's initial state
6464
muhash.Remove(data);
6565
muhash.Remove(data2);
6666
muhash.Finalize(out);
67-
out2 = uint256S(initial_state_hash);
67+
out2 = initial_state_hash;
6868
});
6969
assert(out == out2);
7070
}

src/test/miniscript_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ struct TestData {
4848
TestData()
4949
{
5050
// All our signatures sign (and are required to sign) this constant message.
51-
auto const MESSAGE_HASH = uint256S("f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065");
51+
constexpr uint256 MESSAGE_HASH{"0000000000000000f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065"};
5252
// We don't pass additional randomness when creating a schnorr signature.
53-
auto const EMPTY_AUX{uint256S("")};
53+
const auto EMPTY_AUX{uint256::ZERO};
5454

5555
// We generate 255 public keys and 255 hashes of each type.
5656
for (int i = 1; i <= 255; ++i) {

src/test/script_standard_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ BOOST_AUTO_TEST_CASE(script_standard_taproot_builder)
393393
XOnlyPubKey key_2{ParseHex("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9")};
394394
CScript script_1 = CScript() << ToByteVector(key_1) << OP_CHECKSIG;
395395
CScript script_2 = CScript() << ToByteVector(key_2) << OP_CHECKSIG;
396-
uint256 hash_3 = uint256S("31fe7061656bea2a36aa60a2f7ef940578049273746935d296426dc0afd86b68");
396+
constexpr uint256 hash_3{"31fe7061656bea2a36aa60a2f7ef940578049273746935d296426dc0afd86b68"};
397397

398398
TaprootBuilder builder;
399399
BOOST_CHECK(builder.IsValid() && builder.IsComplete());

src/test/script_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,17 +1692,17 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
16921692

16931693
BOOST_AUTO_TEST_CASE(compute_tapbranch)
16941694
{
1695-
uint256 hash1 = uint256S("8ad69ec7cf41c2a4001fd1f738bf1e505ce2277acdcaa63fe4765192497f47a7");
1696-
uint256 hash2 = uint256S("f224a923cd0021ab202ab139cc56802ddb92dcfc172b9212261a539df79a112a");
1697-
uint256 result = uint256S("a64c5b7b943315f9b805d7a7296bedfcfd08919270a1f7a1466e98f8693d8cd9");
1695+
constexpr uint256 hash1{"8ad69ec7cf41c2a4001fd1f738bf1e505ce2277acdcaa63fe4765192497f47a7"};
1696+
constexpr uint256 hash2{"f224a923cd0021ab202ab139cc56802ddb92dcfc172b9212261a539df79a112a"};
1697+
constexpr uint256 result{"a64c5b7b943315f9b805d7a7296bedfcfd08919270a1f7a1466e98f8693d8cd9"};
16981698
BOOST_CHECK_EQUAL(ComputeTapbranchHash(hash1, hash2), result);
16991699
}
17001700

17011701
BOOST_AUTO_TEST_CASE(compute_tapleaf)
17021702
{
1703-
const uint8_t script[6] = {'f','o','o','b','a','r'};
1704-
uint256 tlc0 = uint256S("edbc10c272a1215dcdcc11d605b9027b5ad6ed97cd45521203f136767b5b9c06");
1705-
uint256 tlc2 = uint256S("8b5c4f90ae6bf76e259dbef5d8a59df06359c391b59263741b25eca76451b27a");
1703+
constexpr uint8_t script[6] = {'f','o','o','b','a','r'};
1704+
constexpr uint256 tlc0{"edbc10c272a1215dcdcc11d605b9027b5ad6ed97cd45521203f136767b5b9c06"};
1705+
constexpr uint256 tlc2{"8b5c4f90ae6bf76e259dbef5d8a59df06359c391b59263741b25eca76451b27a"};
17061706

17071707
BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc0, Span(script)), tlc0);
17081708
BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc2, Span(script)), tlc2);

0 commit comments

Comments
 (0)