Skip to content

Commit e5c600d

Browse files
committed
Merge bitcoin#34063: Make transaction_indentifier hex string constructor evaluated at comptime
5ac3579 refactor: Add compile-time-checked hex txid (rustaceanrob) Pull request description: Suggested by l0rinc as a comment in bitcoin#34004. There are tests that utilize `FromHex` that will only fail during runtime if malformed. Adds a compile time constructor that can be caught by LSPs. ACKs for top commit: l0rinc: ACK 5ac3579 maflcko: review ACK 5ac3579 🦎 rkrux: crACK 5ac3579 Tree-SHA512: b0bae2bf0b8cd8c9a90765a14c46146313cf8b224a29d58a253e65ca95c4205c0beddea9c49ae58901e72c8c5202b91695d074ffb1c48e448d2e5606eb1bd5b4
2 parents 41f2cc6 + 5ac3579 commit e5c600d

5 files changed

Lines changed: 19 additions & 18 deletions

File tree

src/primitives/transaction_identifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class transaction_identifier
3434

3535
public:
3636
transaction_identifier() : m_wrapped{} {}
37+
consteval explicit transaction_identifier(std::string_view hex_str) : m_wrapped{uint256{hex_str}} {}
3738

3839
template <typename Other>
3940
bool operator==(const Other& other) const { return Compare(other) == 0; }

src/test/bloom_tests.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ BOOST_AUTO_TEST_CASE(bloom_match)
140140
BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address");
141141

142142
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
143-
filter.insert(COutPoint(Txid::FromHex("90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 0));
143+
filter.insert(COutPoint{Txid{"90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"}, 0});
144144
BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match COutPoint");
145145

146146
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
147-
COutPoint prevOutPoint(Txid::FromHex("90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 0);
147+
COutPoint prevOutPoint{Txid{"90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"}, 0};
148148
{
149149
std::vector<unsigned char> data(32 + sizeof(unsigned int));
150150
memcpy(data.data(), prevOutPoint.hash.begin(), 32);
@@ -162,11 +162,11 @@ BOOST_AUTO_TEST_CASE(bloom_match)
162162
BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random address");
163163

164164
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
165-
filter.insert(COutPoint(Txid::FromHex("90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 1));
165+
filter.insert(COutPoint{Txid{"90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"}, 1});
166166
BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about");
167167

168168
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
169-
filter.insert(COutPoint(Txid::FromHex("000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 0));
169+
filter.insert(COutPoint{Txid{"000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"}, 0});
170170
BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about");
171171
}
172172

@@ -426,9 +426,9 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_p2pubkey_only)
426426
BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash());
427427

428428
// We should match the generation outpoint
429-
BOOST_CHECK(filter.contains(COutPoint(Txid::FromHex("147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b").value(), 0)));
429+
BOOST_CHECK(filter.contains(COutPoint{Txid{"147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"}, 0}));
430430
// ... but not the 4th transaction's output (its not pay-2-pubkey)
431-
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041").value(), 0)));
431+
BOOST_CHECK(!filter.contains(COutPoint{Txid{"02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"}, 0}));
432432
}
433433

434434
BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)
@@ -451,8 +451,8 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)
451451
BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash());
452452

453453
// We shouldn't match any outpoints (UPDATE_NONE)
454-
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b").value(), 0)));
455-
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041").value(), 0)));
454+
BOOST_CHECK(!filter.contains(COutPoint{Txid{"147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"}, 0}));
455+
BOOST_CHECK(!filter.contains(COutPoint{Txid{"02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"}, 0}));
456456
}
457457

458458
std::vector<unsigned char> BloomTest::RandomData()

src/test/merkleblock_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found)
2424
std::set<Txid> txids;
2525

2626
// Last txn in block.
27-
Txid txhash1{Txid::FromHex("74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20").value()};
27+
constexpr Txid txhash1{"74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"};
2828

2929
// Second txn in block.
30-
Txid txhash2{Txid::FromHex("f9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07").value()};
30+
constexpr Txid txhash2{"f9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07"};
3131

3232
txids.insert(txhash1);
3333
txids.insert(txhash2);
@@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found)
6363
CBlock block = getBlock13b8a();
6464

6565
std::set<Txid> txids2;
66-
txids2.insert(Txid::FromHex("c0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20").value());
66+
txids2.insert(Txid{"c0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"});
6767
CMerkleBlock merkleBlock(block, txids2);
6868

6969
BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());

src/test/transaction_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
509509
// create a big transaction of 4500 inputs signed by the same key
510510
for(uint32_t ij = 0; ij < 4500; ij++) {
511511
uint32_t i = mtx.vin.size();
512-
COutPoint outpoint(Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000100").value(), i);
512+
COutPoint outpoint{Txid{"0000000000000000000000000000000000000000000000000000000000000100"}, i};
513513

514514
mtx.vin.resize(mtx.vin.size() + 1);
515515
mtx.vin[i].prevout = outpoint;

src/test/txpackage_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ BOOST_AUTO_TEST_CASE(package_hash_tests)
7373
CTransactionRef ptx_3{MakeTransactionRef(tx_3)};
7474

7575
// It's easy to see that wtxids are sorted in lexicographical order:
76-
Wtxid wtxid_1{Wtxid::FromHex("85cd1a31eb38f74ed5742ec9cb546712ab5aaf747de28a9168b53e846cbda17f").value()};
77-
Wtxid wtxid_2{Wtxid::FromHex("b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b").value()};
78-
Wtxid wtxid_3{Wtxid::FromHex("e065bac15f62bb4e761d761db928ddee65a47296b2b776785abb912cdec474e3").value()};
76+
constexpr Wtxid wtxid_1{"85cd1a31eb38f74ed5742ec9cb546712ab5aaf747de28a9168b53e846cbda17f"};
77+
constexpr Wtxid wtxid_2{"b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b"};
78+
constexpr Wtxid wtxid_3{"e065bac15f62bb4e761d761db928ddee65a47296b2b776785abb912cdec474e3"};
7979
BOOST_CHECK_EQUAL(tx_1.GetWitnessHash(), wtxid_1);
8080
BOOST_CHECK_EQUAL(tx_2.GetWitnessHash(), wtxid_2);
8181
BOOST_CHECK_EQUAL(tx_3.GetWitnessHash(), wtxid_3);
@@ -84,9 +84,9 @@ BOOST_AUTO_TEST_CASE(package_hash_tests)
8484
BOOST_CHECK(wtxid_2.GetHex() < wtxid_3.GetHex());
8585

8686
// The txids are not (we want to test that sorting and hashing use wtxid, not txid):
87-
Txid txid_1{Txid::FromHex("bd0f71c1d5e50589063e134fad22053cdae5ab2320db5bf5e540198b0b5a4e69").value()};
88-
Txid txid_2{Txid::FromHex("b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b").value()};
89-
Txid txid_3{Txid::FromHex("ee707be5201160e32c4fc715bec227d1aeea5940fb4295605e7373edce3b1a93").value()};
87+
constexpr Txid txid_1{"bd0f71c1d5e50589063e134fad22053cdae5ab2320db5bf5e540198b0b5a4e69"};
88+
constexpr Txid txid_2{"b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b"};
89+
constexpr Txid txid_3{"ee707be5201160e32c4fc715bec227d1aeea5940fb4295605e7373edce3b1a93"};
9090
BOOST_CHECK_EQUAL(tx_1.GetHash(), txid_1);
9191
BOOST_CHECK_EQUAL(tx_2.GetHash(), txid_2);
9292
BOOST_CHECK_EQUAL(tx_3.GetHash(), txid_3);

0 commit comments

Comments
 (0)