Skip to content

Commit

Permalink
qt: Decouple transaction label updates from transaction status updates (
Browse files Browse the repository at this point in the history
#3994)

* qt: Decouple tx label updates from tx status updates

* Update src/qt/transactionrecord.cpp

Co-authored-by: dustinface <35775977+xdustinface@users.noreply.github.com>

Co-authored-by: dustinface <35775977+xdustinface@users.noreply.github.com>
  • Loading branch information
UdjinM6 and xdustinface authored Feb 14, 2021
1 parent 6ec8b9a commit ebea362
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
17 changes: 16 additions & 1 deletion src/qt/transactionrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool TransactionRecord::showTransaction()
/*
* Decompose CWallet transaction to model transaction records.
*/
QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interfaces::WalletTx& wtx)
QList<TransactionRecord> TransactionRecord::decomposeTransaction(interfaces::Wallet& wallet, const interfaces::WalletTx& wtx)
{
QList<TransactionRecord> parts;
int64_t nTime = wtx.time;
Expand Down Expand Up @@ -59,6 +59,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interface
sub.type = TransactionRecord::RecvWithAddress;
sub.strAddress = EncodeDestination(wtx.txout_address[i]);
sub.txDest = wtx.txout_address[i];
sub.updateLabel(wallet);
}
else
{
Expand Down Expand Up @@ -118,6 +119,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interface
// Sent to Dash Address
sub.strAddress = EncodeDestination(address);
sub.txDest = address;
sub.updateLabel(wallet);
}
else
{
Expand Down Expand Up @@ -210,6 +212,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interface
sub.type = TransactionRecord::SendToAddress;
sub.strAddress = EncodeDestination(wtx.txout_address[nOut]);
sub.txDest = wtx.txout_address[nOut];
sub.updateLabel(wallet);
}
else
{
Expand Down Expand Up @@ -330,6 +333,18 @@ bool TransactionRecord::statusUpdateNeeded(int numBlocks, int chainLockHeight) c
|| (!status.lockedByChainLocks && status.cachedChainLockHeight != chainLockHeight);
}

void TransactionRecord::updateLabel(interfaces::Wallet& wallet)
{
if (IsValidDestination(txDest)) {
std::string name;
if (wallet.getAddress(txDest, &name)) {
label = QString::fromStdString(name);
} else {
label = "";
}
}
}

QString TransactionRecord::getTxHash() const
{
return QString::fromStdString(hash.ToString());
Expand Down
11 changes: 8 additions & 3 deletions src/qt/transactionrecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class TransactionStatus
bool lockedByChainLocks;
/// Sorting key based on status
std::string sortKey;
/// Label
QString label;

/** @name Generated (mined) transactions
@{*/
Expand Down Expand Up @@ -129,7 +127,7 @@ class TransactionRecord
/** Decompose CWallet transaction to model transaction records.
*/
static bool showTransaction();
static QList<TransactionRecord> decomposeTransaction(const interfaces::WalletTx& wtx);
static QList<TransactionRecord> decomposeTransaction(interfaces::Wallet& wallet, const interfaces::WalletTx& wtx);

/** @name Immutable transaction attributes
@{*/
Expand All @@ -152,6 +150,9 @@ class TransactionRecord
/** Whether the transaction was sent/received with a watch-only address */
bool involvesWatchAddress;

/// Label
QString label;

/** Return the unique identifier for this transaction (part) */
QString getTxHash() const;

Expand All @@ -165,6 +166,10 @@ class TransactionRecord
/** Return whether a status update is needed.
*/
bool statusUpdateNeeded(int numBlocks, int chainLockHeight) const;

/** Update label from address book.
*/
void updateLabel(interfaces::Wallet& wallet);
};

#endif // BITCOIN_QT_TRANSACTIONRECORD_H
27 changes: 9 additions & 18 deletions src/qt/transactiontablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class TransactionTablePriv
{
for (const auto& wtx : wallet.getWalletTxs()) {
if (TransactionRecord::showTransaction()) {
cachedWallet.append(TransactionRecord::decomposeTransaction(wtx));
cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, wtx));
}
}
}
Expand Down Expand Up @@ -132,7 +132,7 @@ class TransactionTablePriv
}
// Added -- insert at the right position
QList<TransactionRecord> toInsert =
TransactionRecord::decomposeTransaction(wtx);
TransactionRecord::decomposeTransaction(wallet, wtx);
if(!toInsert.isEmpty()) /* only if something to insert */
{
parent->beginInsertRows(QModelIndex(), lowerIndex, lowerIndex+toInsert.size()-1);
Expand Down Expand Up @@ -169,13 +169,13 @@ class TransactionTablePriv
}
}

void updateAddressBook(const QString& address, const QString& label, bool isMine, const QString& purpose, int status)
void updateAddressBook(interfaces::Wallet& wallet, const QString& address, const QString& label, bool isMine, const QString& purpose, int status)
{
std::string address2 = address.toStdString();
int index = 0;
for (auto& rec : cachedWallet) {
if (rec.strAddress == address2) {
rec.status.needsUpdate = true;
rec.updateLabel(wallet);
Q_EMIT parent->dataChanged(parent->index(index, TransactionTableModel::ToAddress), parent->index(index, TransactionTableModel::ToAddress));
}
index++;
Expand Down Expand Up @@ -204,15 +204,6 @@ class TransactionTablePriv
int64_t adjustedTime;
if (rec->statusUpdateNeeded(numBlocks, parent->getChainLockHeight()) && wallet.tryGetTxStatus(rec->hash, wtx, adjustedTime)) {
rec->updateStatus(wtx, numBlocks, adjustedTime, parent->getChainLockHeight());
// Update label
if (IsValidDestination(rec->txDest)) {
std::string name;
if (wallet.getAddress(rec->txDest, &name)) {
rec->status.label = QString::fromStdString(name);
} else {
rec->status.label = "";
}
}
}
return rec;
}
Expand Down Expand Up @@ -274,7 +265,7 @@ void TransactionTableModel::updateTransaction(const QString &hash, int status, b
void TransactionTableModel::updateAddressBook(const QString& address, const QString& label, bool isMine,
const QString& purpose, int status)
{
priv->updateAddressBook(address, label, isMine, purpose, status);
priv->updateAddressBook(walletModel->wallet(), address, label, isMine, purpose, status);
}

void TransactionTableModel::updateConfirmations()
Expand Down Expand Up @@ -439,7 +430,7 @@ QString TransactionTableModel::formatTxToAddress(const TransactionRecord *wtx, b
case TransactionRecord::SendToAddress:
case TransactionRecord::Generated:
case TransactionRecord::PrivateSend:
return formatAddressLabel(wtx->strAddress, wtx->status.label, tooltip) + watchAddress;
return formatAddressLabel(wtx->strAddress, wtx->label, tooltip) + watchAddress;
case TransactionRecord::SendToOther:
return QString::fromStdString(wtx->strAddress) + watchAddress;
case TransactionRecord::SendToSelf:
Expand All @@ -459,7 +450,7 @@ QVariant TransactionTableModel::addressColor(const TransactionRecord *wtx) const
case TransactionRecord::PrivateSend:
case TransactionRecord::RecvWithPrivateSend:
{
if (wtx->status.label.isEmpty()) {
if (wtx->label.isEmpty()) {
return GUIUtil::getThemedQColor(GUIUtil::ThemedColor::BAREADDRESS);
}
} break;
Expand Down Expand Up @@ -653,7 +644,7 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
case AddressRole:
return QString::fromStdString(rec->strAddress);
case LabelRole:
return rec->status.label;
return rec->label;
case AmountRole:
return qint64(rec->credit + rec->debit);
case TxHashRole:
Expand All @@ -663,7 +654,7 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
case TxPlainTextRole:
{
QString details;
QString txLabel = rec->status.label;
QString txLabel = rec->label;

details.append(formatTxDate(rec));
details.append(" ");
Expand Down

0 comments on commit ebea362

Please sign in to comment.