Skip to content

Commit e1dfa4f

Browse files
committed
Merge bitcoin/bitcoin#32237: qt: Update SetHexDeprecated to FromHex
868816d refactor: Remove SetHexDeprecated (marcofleon) 6b63218 qt: Update SetHexDeprecated to FromHex (marcofleon) Pull request description: This is part of bitcoin/bitcoin#32189. I'm separating this out because it's not immediately obvious that it's just a refactor. `SetHexDeprecated()` doesn't do any correctness checks on the input, while `FromHex()` does, so it's theoretically possible that there's a behavior change. Replaces `uint256::SetHexDeprecated()` calls with `Txid::FromHex()` in four locations: - `TransactionTableModel::updateTransaction` - `TransactionView::contextualMenu` - `TransactionView::abandonTx` - `TransactionView::bumpFee` The input strings in these cases aren't user input, so they should only be valid hex strings from `GetHex()` (through `TransactionRecord::getTxHash()`). These conversions should be safe without additional checks. ACKs for top commit: laanwj: Code review ACK 868816d w0xlt: Code review ACK bitcoin/bitcoin@868816d BrandonOdiwuor: Code Review ACK 868816d TheCharlatan: ACK 868816d hebasto: ACK 868816d, I have reviewed the code and it looks OK. Tree-SHA512: 121f149dcc7358231d0327cb3212ec96486a88410174d3c74ab8cbd61bad35185bc0a9740d534492b714811f72a6736bc7ac6eeae590c0ea1365c61cc791da37
2 parents b8cefeb + 868816d commit e1dfa4f

File tree

5 files changed

+26
-61
lines changed

5 files changed

+26
-61
lines changed

src/qt/transactiontablemodel.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ void TransactionTableModel::updateAmountColumnTitle()
276276

277277
void TransactionTableModel::updateTransaction(const QString &hash, int status, bool showTransaction)
278278
{
279-
uint256 updated;
280-
updated.SetHexDeprecated(hash.toStdString());
279+
Txid updated = Txid::FromHex(hash.toStdString()).value();
281280

282281
priv->updateWallet(walletModel->wallet(), updated, status, showTransaction);
283282
}

src/qt/transactionview.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,13 @@ void TransactionView::contextualMenu(const QPoint &point)
394394
if (selection.empty())
395395
return;
396396

397-
// check if transaction can be abandoned, disable context menu action in case it doesn't
398-
uint256 hash;
399-
hash.SetHexDeprecated(selection.at(0).data(TransactionTableModel::TxHashRole).toString().toStdString());
397+
// If the hash from the TxHashRole (QVariant / QString) is invalid, exit
398+
QString hashQStr = selection.at(0).data(TransactionTableModel::TxHashRole).toString();
399+
std::optional<Txid> maybeHash = Txid::FromHex(hashQStr.toStdString());
400+
if (!maybeHash)
401+
return;
402+
403+
Txid hash = *maybeHash;
400404
abandonAction->setEnabled(model->wallet().transactionCanBeAbandoned(hash));
401405
bumpFeeAction->setEnabled(model->wallet().transactionCanBeBumped(hash));
402406
copyAddressAction->setEnabled(GUIUtil::hasEntryData(transactionView, 0, TransactionTableModel::AddressRole));
@@ -414,9 +418,8 @@ void TransactionView::abandonTx()
414418
QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
415419

416420
// get the hash from the TxHashRole (QVariant / QString)
417-
uint256 hash;
418421
QString hashQStr = selection.at(0).data(TransactionTableModel::TxHashRole).toString();
419-
hash.SetHexDeprecated(hashQStr.toStdString());
422+
Txid hash = Txid::FromHex(hashQStr.toStdString()).value();
420423

421424
// Abandon the wallet transaction over the walletModel
422425
model->wallet().abandonTransaction(hash);
@@ -429,9 +432,8 @@ void TransactionView::bumpFee([[maybe_unused]] bool checked)
429432
QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
430433

431434
// get the hash from the TxHashRole (QVariant / QString)
432-
uint256 hash;
433435
QString hashQStr = selection.at(0).data(TransactionTableModel::TxHashRole).toString();
434-
hash.SetHexDeprecated(hashQStr.toStdString());
436+
Txid hash = Txid::FromHex(hashQStr.toStdString()).value();
435437

436438
// Bump tx fee over the walletModel
437439
uint256 newHash;

src/test/uint256_tests.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,14 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
147147
uint256{"0000000000000000000000000000000000000000000000000000000000000001"});
148148
}
149149

150-
BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
150+
BOOST_AUTO_TEST_CASE(methods) // GetHex FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
151151
{
152152
BOOST_CHECK_EQUAL(R1L.GetHex(), R1L.ToString());
153153
BOOST_CHECK_EQUAL(R2L.GetHex(), R2L.ToString());
154154
BOOST_CHECK_EQUAL(OneL.GetHex(), OneL.ToString());
155155
BOOST_CHECK_EQUAL(MaxL.GetHex(), MaxL.ToString());
156156
uint256 TmpL(R1L);
157157
BOOST_CHECK_EQUAL(TmpL, R1L);
158-
// Verify previous values don't persist when setting to truncated string.
159-
TmpL.SetHexDeprecated("21");
160-
BOOST_CHECK_EQUAL(TmpL.ToString(), "0000000000000000000000000000000000000000000000000000000000000021");
161158
BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
162159
BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), uint256());
163160

src/uint256.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,6 @@ std::string base_blob<BITS>::GetHex() const
1717
return HexStr(m_data_rev);
1818
}
1919

20-
template <unsigned int BITS>
21-
void base_blob<BITS>::SetHexDeprecated(const std::string_view str)
22-
{
23-
std::fill(m_data.begin(), m_data.end(), 0);
24-
25-
const auto trimmed = util::RemovePrefixView(util::TrimStringView(str), "0x");
26-
27-
// Note: if we are passed a greater number of digits than would fit as bytes
28-
// in m_data, we will be discarding the leftmost ones.
29-
// str="12bc" in a WIDTH=1 m_data => m_data[] == "\0xbc", not "0x12".
30-
size_t digits = 0;
31-
for (const char c : trimmed) {
32-
if (::HexDigit(c) == -1) break;
33-
++digits;
34-
}
35-
unsigned char* p1 = m_data.data();
36-
unsigned char* pend = p1 + WIDTH;
37-
while (digits > 0 && p1 < pend) {
38-
*p1 = ::HexDigit(trimmed[--digits]);
39-
if (digits > 0) {
40-
*p1 |= ((unsigned char)::HexDigit(trimmed[--digits]) << 4);
41-
p1++;
42-
}
43-
}
44-
}
45-
4620
template <unsigned int BITS>
4721
std::string base_blob<BITS>::ToString() const
4822
{
@@ -52,12 +26,10 @@ std::string base_blob<BITS>::ToString() const
5226
// Explicit instantiations for base_blob<160>
5327
template std::string base_blob<160>::GetHex() const;
5428
template std::string base_blob<160>::ToString() const;
55-
template void base_blob<160>::SetHexDeprecated(std::string_view);
5629

5730
// Explicit instantiations for base_blob<256>
5831
template std::string base_blob<256>::GetHex() const;
5932
template std::string base_blob<256>::ToString() const;
60-
template void base_blob<256>::SetHexDeprecated(std::string_view);
6133

6234
const uint256 uint256::ZERO(0);
6335
const uint256 uint256::ONE(1);

src/uint256.h

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ class base_blob
6969

7070
/** @name Hex representation
7171
*
72-
* The hex representation used by GetHex(), ToString(), FromHex() and
73-
* SetHexDeprecated() is unusual, since it shows bytes of the base_blob in
74-
* reverse order. For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is
75-
* represented as "78563412" instead of the more typical "12345678"
76-
* representation that would be shown in a hex editor or used by typical
72+
* The hex representation used by GetHex(), ToString(), and FromHex()
73+
* is unusual, since it shows bytes of the base_blob in reverse order.
74+
* For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is represented
75+
* as "78563412" instead of the more typical "12345678" representation
76+
* that would be shown in a hex editor or used by typical
7777
* byte-array / hex conversion functions like python's bytes.hex() and
7878
* bytes.fromhex().
7979
*
@@ -92,20 +92,6 @@ class base_blob
9292
*
9393
* @{*/
9494
std::string GetHex() const;
95-
/** Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
96-
*
97-
* - Hex numbers that don't specify enough bytes to fill the internal array
98-
* will be treated as setting the beginning of it, which corresponds to
99-
* the least significant bytes when converted to base_uint.
100-
*
101-
* - Hex numbers specifying too many bytes will have the numerically most
102-
* significant bytes (the beginning of the string) narrowed away.
103-
*
104-
* - An odd count of hex digits will result in the high bits of the leftmost
105-
* byte being zero.
106-
* "0x123" => {0x23, 0x1, 0x0, ..., 0x0}
107-
*/
108-
void SetHexDeprecated(std::string_view str);
10995
std::string ToString() const;
11096
/**@}*/
11197

@@ -158,7 +144,16 @@ std::optional<uintN_t> FromHex(std::string_view str)
158144
{
159145
if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
160146
uintN_t rv;
161-
rv.SetHexDeprecated(str);
147+
unsigned char* p1 = rv.begin();
148+
unsigned char* pend = rv.end();
149+
size_t digits = str.size();
150+
while (digits > 0 && p1 < pend) {
151+
*p1 = ::HexDigit(str[--digits]);
152+
if (digits > 0) {
153+
*p1 |= ((unsigned char)::HexDigit(str[--digits]) << 4);
154+
p1++;
155+
}
156+
}
162157
return rv;
163158
}
164159
/**

0 commit comments

Comments
 (0)