Skip to content

Commit 4b8495a

Browse files
stickies-vdenavila
authored andcommitted
refactor: remove TxidFromString
TxidFromString was deprecated due to missing 64-character length-check and hex-check, replace it with the more robust Txid::FromHex.
1 parent 361926b commit 4b8495a

File tree

6 files changed

+28
-46
lines changed

6 files changed

+28
-46
lines changed

src/qt/coincontroldialog.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,17 @@ void CoinControlDialog::showMenu(const QPoint &point)
174174
contextMenuItem = item;
175175

176176
// disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
177-
if (item->data(COLUMN_ADDRESS, TxHashRole).toString().length() == 64) // transaction hash is 64 characters (this means it is a child node, so it is not a parent node in tree mode)
178-
{
177+
auto txid{Txid::FromHex(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString())};
178+
if (txid) { // a valid txid means this is a child node, and not a parent node in tree mode
179179
m_copy_transaction_outpoint_action->setEnabled(true);
180-
if (model->wallet().isLockedCoin(COutPoint(TxidFromString(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt())))
181-
{
180+
if (model->wallet().isLockedCoin(COutPoint(*txid, item->data(COLUMN_ADDRESS, VOutRole).toUInt()))) {
182181
lockAction->setEnabled(false);
183182
unlockAction->setEnabled(true);
184-
}
185-
else
186-
{
183+
} else {
187184
lockAction->setEnabled(true);
188185
unlockAction->setEnabled(false);
189186
}
190-
}
191-
else // this means click on parent node in tree mode -> disable all
192-
{
187+
} else { // this means click on parent node in tree mode -> disable all
193188
m_copy_transaction_outpoint_action->setEnabled(false);
194189
lockAction->setEnabled(false);
195190
unlockAction->setEnabled(false);
@@ -240,7 +235,7 @@ void CoinControlDialog::lockCoin()
240235
if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
241236
contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
242237

243-
COutPoint outpt(TxidFromString(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
238+
COutPoint outpt(Txid::FromHex(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()).value(), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
244239
model->wallet().lockCoin(outpt, /* write_to_db = */ true);
245240
contextMenuItem->setDisabled(true);
246241
contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
@@ -250,7 +245,7 @@ void CoinControlDialog::lockCoin()
250245
// context menu action: unlock coin
251246
void CoinControlDialog::unlockCoin()
252247
{
253-
COutPoint outpt(TxidFromString(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
248+
COutPoint outpt(Txid::FromHex(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()).value(), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
254249
model->wallet().unlockCoin(outpt);
255250
contextMenuItem->setDisabled(false);
256251
contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
@@ -340,9 +335,10 @@ void CoinControlDialog::radioListMode(bool checked)
340335
// checkbox clicked by user
341336
void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
342337
{
343-
if (column == COLUMN_CHECKBOX && item->data(COLUMN_ADDRESS, TxHashRole).toString().length() == 64) // transaction hash is 64 characters (this means it is a child node, so it is not a parent node in tree mode)
344-
{
345-
COutPoint outpt(TxidFromString(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt());
338+
if (column != COLUMN_CHECKBOX) return;
339+
auto txid{Txid::FromHex(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString())};
340+
if (txid) { // a valid txid means this is a child node, and not a parent node in tree mode
341+
COutPoint outpt(*txid, item->data(COLUMN_ADDRESS, VOutRole).toUInt());
346342

347343
if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
348344
m_coin_control.UnSelect(outpt);

src/test/bloom_tests.cpp

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

140140
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
141-
filter.insert(COutPoint(TxidFromString("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0));
141+
filter.insert(COutPoint(Txid::FromHex("90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 0));
142142
BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match COutPoint");
143143

144144
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
145-
COutPoint prevOutPoint(TxidFromString("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0);
145+
COutPoint prevOutPoint(Txid::FromHex("90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 0);
146146
{
147147
std::vector<unsigned char> data(32 + sizeof(unsigned int));
148148
memcpy(data.data(), prevOutPoint.hash.begin(), 32);
@@ -160,11 +160,11 @@ BOOST_AUTO_TEST_CASE(bloom_match)
160160
BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random address");
161161

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

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

@@ -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(TxidFromString("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0)));
429+
BOOST_CHECK(filter.contains(COutPoint(Txid::FromHex("147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b").value(), 0)));
430430
// ... but not the 4th transaction's output (its not pay-2-pubkey)
431-
BOOST_CHECK(!filter.contains(COutPoint(TxidFromString("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0)));
431+
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041").value(), 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(TxidFromString("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0)));
455-
BOOST_CHECK(!filter.contains(COutPoint(TxidFromString("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0)));
454+
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b").value(), 0)));
455+
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041").value(), 0)));
456456
}
457457

458458
static std::vector<unsigned char> 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{TxidFromString("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")};
27+
Txid txhash1{Txid::FromHex("74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20").value()};
2828

2929
// Second txn in block.
30-
Txid txhash2{TxidFromString("0xf9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07")};
30+
Txid txhash2{Txid::FromHex("f9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07").value()};
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(TxidFromString("0xc0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"));
66+
txids2.insert(Txid::FromHex("c0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20").value());
6767
CMerkleBlock merkleBlock(block, txids2);
6868

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

src/test/transaction_tests.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ BOOST_AUTO_TEST_CASE(tx_valid)
223223
fValid = false;
224224
break;
225225
}
226-
COutPoint outpoint{TxidFromString(vinput[0].get_str()), uint32_t(vinput[1].getInt<int>())};
226+
COutPoint outpoint{Txid::FromHex(vinput[0].get_str()).value(), uint32_t(vinput[1].getInt<int>())};
227227
mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
228228
if (vinput.size() >= 4)
229229
{
@@ -311,7 +311,7 @@ BOOST_AUTO_TEST_CASE(tx_invalid)
311311
fValid = false;
312312
break;
313313
}
314-
COutPoint outpoint{TxidFromString(vinput[0].get_str()), uint32_t(vinput[1].getInt<int>())};
314+
COutPoint outpoint{Txid::FromHex(vinput[0].get_str()).value(), uint32_t(vinput[1].getInt<int>())};
315315
mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
316316
if (vinput.size() >= 4)
317317
{
@@ -542,7 +542,7 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
542542
// create a big transaction of 4500 inputs signed by the same key
543543
for(uint32_t ij = 0; ij < 4500; ij++) {
544544
uint32_t i = mtx.vin.size();
545-
COutPoint outpoint(TxidFromString("0000000000000000000000000000000000000000000000000000000000000100"), i);
545+
COutPoint outpoint(Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000100").value(), i);
546546

547547
mtx.vin.resize(mtx.vin.size() + 1);
548548
mtx.vin[i].prevout = outpoint;
@@ -1028,12 +1028,4 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
10281028
}
10291029
}
10301030

1031-
BOOST_AUTO_TEST_CASE(test_TxidFromString)
1032-
{
1033-
// Make sure TxidFromString respects string_view length and stops reading at
1034-
// end of the substring.
1035-
BOOST_CHECK_EQUAL(TxidFromString(std::string_view("ABCD1234", 4)).ToString(),
1036-
"000000000000000000000000000000000000000000000000000000000000abcd");
1037-
}
1038-
10391031
BOOST_AUTO_TEST_SUITE_END()

src/test/txpackage_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ BOOST_FIXTURE_TEST_CASE(package_hash_tests, TestChain100Setup)
7878
BOOST_CHECK(wtxid_2.GetHex() < wtxid_3.GetHex());
7979

8080
// The txids are not (we want to test that sorting and hashing use wtxid, not txid):
81-
Txid txid_1{TxidFromString("0xbd0f71c1d5e50589063e134fad22053cdae5ab2320db5bf5e540198b0b5a4e69")};
82-
Txid txid_2{TxidFromString("0xb4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b")};
83-
Txid txid_3{TxidFromString("0xee707be5201160e32c4fc715bec227d1aeea5940fb4295605e7373edce3b1a93")};
81+
Txid txid_1{Txid::FromHex("bd0f71c1d5e50589063e134fad22053cdae5ab2320db5bf5e540198b0b5a4e69").value()};
82+
Txid txid_2{Txid::FromHex("b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b").value()};
83+
Txid txid_3{Txid::FromHex("ee707be5201160e32c4fc715bec227d1aeea5940fb4295605e7373edce3b1a93").value()};
8484
BOOST_CHECK_EQUAL(tx_1.GetHash(), txid_1);
8585
BOOST_CHECK_EQUAL(tx_2.GetHash(), txid_2);
8686
BOOST_CHECK_EQUAL(tx_3.GetHash(), txid_3);

src/util/transaction_identifier.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,4 @@ using Txid = transaction_identifier<false>;
7272
/** Wtxid commits to all transaction fields including the witness. */
7373
using Wtxid = transaction_identifier<true>;
7474

75-
/** DEPRECATED due to missing length-check and hex-check, please use the safer FromHex, or FromUint256 */
76-
inline Txid TxidFromString(std::string_view str)
77-
{
78-
return Txid::FromUint256(uint256S(str));
79-
}
80-
8175
#endif // BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H

0 commit comments

Comments
 (0)