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).
This commit is contained in:
Hodlinator 2024-08-05 14:51:47 +02:00
parent b74d8d58fa
commit c06f2368e2
No known key found for this signature in database
9 changed files with 33 additions and 23 deletions

View File

@ -138,7 +138,7 @@ echo
echo "-- Now: add the following to CMainParams::m_assumeutxo_data" echo "-- Now: add the following to CMainParams::m_assumeutxo_data"
echo " in src/kernel/chainparams.cpp, and recompile:" echo " in src/kernel/chainparams.cpp, and recompile:"
echo echo
echo " {${RPC_BASE_HEIGHT}, AssumeutxoHash{uint256S(\"0x${RPC_AU}\")}, ${RPC_NCHAINTX}, uint256S(\"0x${RPC_BLOCKHASH}\")}," echo " {.height = ${RPC_BASE_HEIGHT}, .hash_serialized = AssumeutxoHash{uint256{\"${RPC_AU}\"}}, .m_chain_tx_count = ${RPC_NCHAINTX}, .blockhash = consteval_ctor(uint256{\"${RPC_BLOCKHASH}\"})},"
echo echo
echo echo
echo "-- IBDing more blocks to the server node (height=$FINAL_HEIGHT) so there is a diff between snapshot and tip..." echo "-- IBDing more blocks to the server node (height=$FINAL_HEIGHT) so there is a diff between snapshot and tip..."

View File

@ -26,6 +26,16 @@
#include <cstring> #include <cstring>
#include <type_traits> #include <type_traits>
// Workaround MSVC bug triggering C7595 when calling consteval constructors in
// initializer lists.
// A fix may be on the way:
// https://developercommunity.visualstudio.com/t/consteval-conversion-function-fails/1579014
#if defined(_MSC_VER)
auto consteval_ctor(auto&& input) { return input; }
#else
#define consteval_ctor(input) (input)
#endif
static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward) static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
{ {
CMutableTransaction txNew; CMutableTransaction txNew;
@ -273,7 +283,7 @@ public:
.height = 2'500'000, .height = 2'500'000,
.hash_serialized = AssumeutxoHash{uint256S("0xf841584909f68e47897952345234e37fcd9128cd818f41ee6c3ca68db8071be7")}, .hash_serialized = AssumeutxoHash{uint256S("0xf841584909f68e47897952345234e37fcd9128cd818f41ee6c3ca68db8071be7")},
.m_chain_tx_count = 66484552, .m_chain_tx_count = 66484552,
.blockhash = uint256S("0x0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f") .blockhash = consteval_ctor(uint256{"0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f"}),
} }
}; };
@ -383,7 +393,7 @@ public:
.height = 160'000, .height = 160'000,
.hash_serialized = AssumeutxoHash{uint256S("0xfe0a44309b74d6b5883d246cb419c6221bcccf0b308c9b59b7d70783dbdf928a")}, .hash_serialized = AssumeutxoHash{uint256S("0xfe0a44309b74d6b5883d246cb419c6221bcccf0b308c9b59b7d70783dbdf928a")},
.m_chain_tx_count = 2289496, .m_chain_tx_count = 2289496,
.blockhash = uint256S("0x0000003ca3c99aff040f2563c2ad8f8ec88bd0fd6b8f0895cfaf1ef90353a62c") .blockhash = consteval_ctor(uint256{"0000003ca3c99aff040f2563c2ad8f8ec88bd0fd6b8f0895cfaf1ef90353a62c"}),
} }
}; };
@ -499,21 +509,21 @@ public:
.height = 110, .height = 110,
.hash_serialized = AssumeutxoHash{uint256S("0x6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1")}, .hash_serialized = AssumeutxoHash{uint256S("0x6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1")},
.m_chain_tx_count = 111, .m_chain_tx_count = 111,
.blockhash = uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c") .blockhash = consteval_ctor(uint256{"696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c"}),
}, },
{ {
// For use by fuzz target src/test/fuzz/utxo_snapshot.cpp // For use by fuzz target src/test/fuzz/utxo_snapshot.cpp
.height = 200, .height = 200,
.hash_serialized = AssumeutxoHash{uint256S("0x4f34d431c3e482f6b0d67b64609ece3964dc8d7976d02ac68dd7c9c1421738f2")}, .hash_serialized = AssumeutxoHash{uint256S("0x4f34d431c3e482f6b0d67b64609ece3964dc8d7976d02ac68dd7c9c1421738f2")},
.m_chain_tx_count = 201, .m_chain_tx_count = 201,
.blockhash = uint256S("0x5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"), .blockhash = consteval_ctor(uint256{"5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"}),
}, },
{ {
// For use by test/functional/feature_assumeutxo.py // For use by test/functional/feature_assumeutxo.py
.height = 299, .height = 299,
.hash_serialized = AssumeutxoHash{uint256S("0xa4bf3407ccb2cc0145c49ebba8fa91199f8a3903daf0883875941497d2493c27")}, .hash_serialized = AssumeutxoHash{uint256S("0xa4bf3407ccb2cc0145c49ebba8fa91199f8a3903daf0883875941497d2493c27")},
.m_chain_tx_count = 334, .m_chain_tx_count = 334,
.blockhash = uint256S("0x3bb7ce5eba0be48939b7a521ac1ba9316afee2c7bada3a0cca24188e6d7d96c0") .blockhash = consteval_ctor(uint256{"3bb7ce5eba0be48939b7a521ac1ba9316afee2c7bada3a0cca24188e6d7d96c0"}),
}, },
}; };

View File

@ -23,7 +23,7 @@ FUZZ_TARGET(block_header)
} }
{ {
const uint256 hash = block_header->GetHash(); const uint256 hash = block_header->GetHash();
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); constexpr uint256 u256_max{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"};
assert(hash != u256_max); assert(hash != u256_max);
assert(block_header->GetBlockTime() == block_header->nTime); assert(block_header->GetBlockTime() == block_header->nTime);
assert(block_header->IsNull() == (block_header->nBits == 0)); assert(block_header->IsNull() == (block_header->nBits == 0));

View File

@ -78,8 +78,8 @@ FUZZ_TARGET(integer, .init = initialize_integer)
} else { } else {
(void)CompressAmount(u64); (void)CompressAmount(u64);
} }
static const uint256 u256_min(uint256S("0000000000000000000000000000000000000000000000000000000000000000")); constexpr uint256 u256_min{"0000000000000000000000000000000000000000000000000000000000000000"};
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); constexpr uint256 u256_max{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"};
const std::vector<uint256> v256{u256, u256_min, u256_max}; const std::vector<uint256> v256{u256, u256_min, u256_max};
(void)ComputeMerkleRoot(v256); (void)ComputeMerkleRoot(v256);
(void)DecompressAmount(u64); (void)DecompressAmount(u64);

View File

@ -47,9 +47,9 @@ struct TestData {
void Init() { void Init() {
unsigned char keydata[32] = {1}; unsigned char keydata[32] = {1};
// All our signatures sign (and are required to sign) this constant message. // All our signatures sign (and are required to sign) this constant message.
auto const MESSAGE_HASH{uint256S("f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065")}; constexpr uint256 MESSAGE_HASH{"0000000000000000f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065"};
// We don't pass additional randomness when creating a schnorr signature. // We don't pass additional randomness when creating a schnorr signature.
auto const EMPTY_AUX{uint256S("")}; const auto EMPTY_AUX{uint256::ZERO};
for (size_t i = 0; i < 256; i++) { for (size_t i = 0; i < 256; i++) {
keydata[31] = i; keydata[31] = i;

View File

@ -20,7 +20,7 @@ FUZZ_TARGET(muhash)
muhash.Insert(data); muhash.Insert(data);
muhash.Insert(data2); muhash.Insert(data2);
const std::string initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"}; constexpr uint256 initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
uint256 out; uint256 out;
uint256 out2; uint256 out2;
CallOneOf( CallOneOf(
@ -57,14 +57,14 @@ FUZZ_TARGET(muhash)
#endif #endif
muhash.Finalize(out); muhash.Finalize(out);
out2 = uint256S(initial_state_hash); out2 = initial_state_hash;
}, },
[&] { [&] {
// Test that removing all added elements brings the object back to it's initial state // Test that removing all added elements brings the object back to it's initial state
muhash.Remove(data); muhash.Remove(data);
muhash.Remove(data2); muhash.Remove(data2);
muhash.Finalize(out); muhash.Finalize(out);
out2 = uint256S(initial_state_hash); out2 = initial_state_hash;
}); });
assert(out == out2); assert(out == out2);
} }

View File

@ -48,9 +48,9 @@ struct TestData {
TestData() TestData()
{ {
// All our signatures sign (and are required to sign) this constant message. // All our signatures sign (and are required to sign) this constant message.
auto const MESSAGE_HASH = uint256S("f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065"); constexpr uint256 MESSAGE_HASH{"0000000000000000f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065"};
// We don't pass additional randomness when creating a schnorr signature. // We don't pass additional randomness when creating a schnorr signature.
auto const EMPTY_AUX{uint256S("")}; const auto EMPTY_AUX{uint256::ZERO};
// We generate 255 public keys and 255 hashes of each type. // We generate 255 public keys and 255 hashes of each type.
for (int i = 1; i <= 255; ++i) { for (int i = 1; i <= 255; ++i) {

View File

@ -393,7 +393,7 @@ BOOST_AUTO_TEST_CASE(script_standard_taproot_builder)
XOnlyPubKey key_2{ParseHex("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9")}; XOnlyPubKey key_2{ParseHex("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9")};
CScript script_1 = CScript() << ToByteVector(key_1) << OP_CHECKSIG; CScript script_1 = CScript() << ToByteVector(key_1) << OP_CHECKSIG;
CScript script_2 = CScript() << ToByteVector(key_2) << OP_CHECKSIG; CScript script_2 = CScript() << ToByteVector(key_2) << OP_CHECKSIG;
uint256 hash_3 = uint256S("31fe7061656bea2a36aa60a2f7ef940578049273746935d296426dc0afd86b68"); constexpr uint256 hash_3{"31fe7061656bea2a36aa60a2f7ef940578049273746935d296426dc0afd86b68"};
TaprootBuilder builder; TaprootBuilder builder;
BOOST_CHECK(builder.IsValid() && builder.IsComplete()); BOOST_CHECK(builder.IsValid() && builder.IsComplete());

View File

@ -1692,17 +1692,17 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
BOOST_AUTO_TEST_CASE(compute_tapbranch) BOOST_AUTO_TEST_CASE(compute_tapbranch)
{ {
uint256 hash1 = uint256S("8ad69ec7cf41c2a4001fd1f738bf1e505ce2277acdcaa63fe4765192497f47a7"); constexpr uint256 hash1{"8ad69ec7cf41c2a4001fd1f738bf1e505ce2277acdcaa63fe4765192497f47a7"};
uint256 hash2 = uint256S("f224a923cd0021ab202ab139cc56802ddb92dcfc172b9212261a539df79a112a"); constexpr uint256 hash2{"f224a923cd0021ab202ab139cc56802ddb92dcfc172b9212261a539df79a112a"};
uint256 result = uint256S("a64c5b7b943315f9b805d7a7296bedfcfd08919270a1f7a1466e98f8693d8cd9"); constexpr uint256 result{"a64c5b7b943315f9b805d7a7296bedfcfd08919270a1f7a1466e98f8693d8cd9"};
BOOST_CHECK_EQUAL(ComputeTapbranchHash(hash1, hash2), result); BOOST_CHECK_EQUAL(ComputeTapbranchHash(hash1, hash2), result);
} }
BOOST_AUTO_TEST_CASE(compute_tapleaf) BOOST_AUTO_TEST_CASE(compute_tapleaf)
{ {
const uint8_t script[6] = {'f','o','o','b','a','r'}; constexpr uint8_t script[6] = {'f','o','o','b','a','r'};
uint256 tlc0 = uint256S("edbc10c272a1215dcdcc11d605b9027b5ad6ed97cd45521203f136767b5b9c06"); constexpr uint256 tlc0{"edbc10c272a1215dcdcc11d605b9027b5ad6ed97cd45521203f136767b5b9c06"};
uint256 tlc2 = uint256S("8b5c4f90ae6bf76e259dbef5d8a59df06359c391b59263741b25eca76451b27a"); constexpr uint256 tlc2{"8b5c4f90ae6bf76e259dbef5d8a59df06359c391b59263741b25eca76451b27a"};
BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc0, Span(script)), tlc0); BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc0, Span(script)), tlc0);
BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc2, Span(script)), tlc2); BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc2, Span(script)), tlc2);