Skip to content

Commit

Permalink
refactor: Hand-replace some uint256S -> uint256
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
hodlinator committed Aug 5, 2024
1 parent b74d8d5 commit c06f236
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion contrib/devtools/test_utxo_snapshots.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ echo
echo "-- Now: add the following to CMainParams::m_assumeutxo_data"
echo " in src/kernel/chainparams.cpp, and recompile:"
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 "-- IBDing more blocks to the server node (height=$FINAL_HEIGHT) so there is a diff between snapshot and tip..."
Expand Down
20 changes: 15 additions & 5 deletions src/kernel/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
#include <cstring>
#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)
{
CMutableTransaction txNew;
Expand Down Expand Up @@ -273,7 +283,7 @@ class CTestNetParams : public CChainParams {
.height = 2'500'000,
.hash_serialized = AssumeutxoHash{uint256S("0xf841584909f68e47897952345234e37fcd9128cd818f41ee6c3ca68db8071be7")},
.m_chain_tx_count = 66484552,
.blockhash = uint256S("0x0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f")
.blockhash = consteval_ctor(uint256{"0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f"}),
}
};

Expand Down Expand Up @@ -383,7 +393,7 @@ class SigNetParams : public CChainParams {
.height = 160'000,
.hash_serialized = AssumeutxoHash{uint256S("0xfe0a44309b74d6b5883d246cb419c6221bcccf0b308c9b59b7d70783dbdf928a")},
.m_chain_tx_count = 2289496,
.blockhash = uint256S("0x0000003ca3c99aff040f2563c2ad8f8ec88bd0fd6b8f0895cfaf1ef90353a62c")
.blockhash = consteval_ctor(uint256{"0000003ca3c99aff040f2563c2ad8f8ec88bd0fd6b8f0895cfaf1ef90353a62c"}),
}
};

Expand Down Expand Up @@ -499,21 +509,21 @@ class CRegTestParams : public CChainParams
.height = 110,
.hash_serialized = AssumeutxoHash{uint256S("0x6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1")},
.m_chain_tx_count = 111,
.blockhash = uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c")
.blockhash = consteval_ctor(uint256{"696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c"}),
},
{
// For use by fuzz target src/test/fuzz/utxo_snapshot.cpp
.height = 200,
.hash_serialized = AssumeutxoHash{uint256S("0x4f34d431c3e482f6b0d67b64609ece3964dc8d7976d02ac68dd7c9c1421738f2")},
.m_chain_tx_count = 201,
.blockhash = uint256S("0x5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"),
.blockhash = consteval_ctor(uint256{"5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"}),
},
{
// For use by test/functional/feature_assumeutxo.py
.height = 299,
.hash_serialized = AssumeutxoHash{uint256S("0xa4bf3407ccb2cc0145c49ebba8fa91199f8a3903daf0883875941497d2493c27")},
.m_chain_tx_count = 334,
.blockhash = uint256S("0x3bb7ce5eba0be48939b7a521ac1ba9316afee2c7bada3a0cca24188e6d7d96c0")
.blockhash = consteval_ctor(uint256{"3bb7ce5eba0be48939b7a521ac1ba9316afee2c7bada3a0cca24188e6d7d96c0"}),
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/block_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ FUZZ_TARGET(block_header)
}
{
const uint256 hash = block_header->GetHash();
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
constexpr uint256 u256_max{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"};
assert(hash != u256_max);
assert(block_header->GetBlockTime() == block_header->nTime);
assert(block_header->IsNull() == (block_header->nBits == 0));
Expand Down
4 changes: 2 additions & 2 deletions src/test/fuzz/integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ FUZZ_TARGET(integer, .init = initialize_integer)
} else {
(void)CompressAmount(u64);
}
static const uint256 u256_min(uint256S("0000000000000000000000000000000000000000000000000000000000000000"));
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
constexpr uint256 u256_min{"0000000000000000000000000000000000000000000000000000000000000000"};
constexpr uint256 u256_max{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"};
const std::vector<uint256> v256{u256, u256_min, u256_max};
(void)ComputeMerkleRoot(v256);
(void)DecompressAmount(u64);
Expand Down
4 changes: 2 additions & 2 deletions src/test/fuzz/miniscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ struct TestData {
void Init() {
unsigned char keydata[32] = {1};
// 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.
auto const EMPTY_AUX{uint256S("")};
const auto EMPTY_AUX{uint256::ZERO};

for (size_t i = 0; i < 256; i++) {
keydata[31] = i;
Expand Down
6 changes: 3 additions & 3 deletions src/test/fuzz/muhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FUZZ_TARGET(muhash)
muhash.Insert(data);
muhash.Insert(data2);

const std::string initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
constexpr uint256 initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
uint256 out;
uint256 out2;
CallOneOf(
Expand Down Expand Up @@ -57,14 +57,14 @@ FUZZ_TARGET(muhash)
#endif

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
muhash.Remove(data);
muhash.Remove(data2);
muhash.Finalize(out);
out2 = uint256S(initial_state_hash);
out2 = initial_state_hash;
});
assert(out == out2);
}
4 changes: 2 additions & 2 deletions src/test/miniscript_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ struct TestData {
TestData()
{
// 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.
auto const EMPTY_AUX{uint256S("")};
const auto EMPTY_AUX{uint256::ZERO};

// We generate 255 public keys and 255 hashes of each type.
for (int i = 1; i <= 255; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/script_standard_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ BOOST_AUTO_TEST_CASE(script_standard_taproot_builder)
XOnlyPubKey key_2{ParseHex("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9")};
CScript script_1 = CScript() << ToByteVector(key_1) << OP_CHECKSIG;
CScript script_2 = CScript() << ToByteVector(key_2) << OP_CHECKSIG;
uint256 hash_3 = uint256S("31fe7061656bea2a36aa60a2f7ef940578049273746935d296426dc0afd86b68");
constexpr uint256 hash_3{"31fe7061656bea2a36aa60a2f7ef940578049273746935d296426dc0afd86b68"};

TaprootBuilder builder;
BOOST_CHECK(builder.IsValid() && builder.IsComplete());
Expand Down
12 changes: 6 additions & 6 deletions src/test/script_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,17 +1692,17 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)

BOOST_AUTO_TEST_CASE(compute_tapbranch)
{
uint256 hash1 = uint256S("8ad69ec7cf41c2a4001fd1f738bf1e505ce2277acdcaa63fe4765192497f47a7");
uint256 hash2 = uint256S("f224a923cd0021ab202ab139cc56802ddb92dcfc172b9212261a539df79a112a");
uint256 result = uint256S("a64c5b7b943315f9b805d7a7296bedfcfd08919270a1f7a1466e98f8693d8cd9");
constexpr uint256 hash1{"8ad69ec7cf41c2a4001fd1f738bf1e505ce2277acdcaa63fe4765192497f47a7"};
constexpr uint256 hash2{"f224a923cd0021ab202ab139cc56802ddb92dcfc172b9212261a539df79a112a"};
constexpr uint256 result{"a64c5b7b943315f9b805d7a7296bedfcfd08919270a1f7a1466e98f8693d8cd9"};
BOOST_CHECK_EQUAL(ComputeTapbranchHash(hash1, hash2), result);
}

BOOST_AUTO_TEST_CASE(compute_tapleaf)
{
const uint8_t script[6] = {'f','o','o','b','a','r'};
uint256 tlc0 = uint256S("edbc10c272a1215dcdcc11d605b9027b5ad6ed97cd45521203f136767b5b9c06");
uint256 tlc2 = uint256S("8b5c4f90ae6bf76e259dbef5d8a59df06359c391b59263741b25eca76451b27a");
constexpr uint8_t script[6] = {'f','o','o','b','a','r'};
constexpr uint256 tlc0{"edbc10c272a1215dcdcc11d605b9027b5ad6ed97cd45521203f136767b5b9c06"};
constexpr uint256 tlc2{"8b5c4f90ae6bf76e259dbef5d8a59df06359c391b59263741b25eca76451b27a"};

BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc0, Span(script)), tlc0);
BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc2, Span(script)), tlc2);
Expand Down

0 comments on commit c06f236

Please sign in to comment.