From c3bddf47b253cc08f541bb073a8623d6029f46fb Mon Sep 17 00:00:00 2001 From: lateminer Date: Tue, 9 Jan 2018 23:25:37 +0300 Subject: [PATCH] UI to alert of respend attempt affecting wallet. https://github.com/bitcoinxt/bitcoinxt/commit/ba478d229b39b4f11534fa930b5d8b6cbb97a6c0 --- src/qt/guiconstants.h | 4 ++++ src/qt/transactionfilterproxy.cpp | 4 ++-- src/qt/transactionrecord.cpp | 8 ++++++-- src/qt/transactionrecord.h | 23 +++++++++++++++++++---- src/qt/transactiontablemodel.cpp | 24 +++++++++++++++++++----- src/qt/transactiontablemodel.h | 4 ++++ src/ui_interface.h | 3 ++- src/wallet/wallet.cpp | 14 ++++++++++++++ src/wallet/wallet.h | 4 ++++ 9 files changed, 74 insertions(+), 14 deletions(-) diff --git a/src/qt/guiconstants.h b/src/qt/guiconstants.h index 54b1fc7ec..268d39666 100644 --- a/src/qt/guiconstants.h +++ b/src/qt/guiconstants.h @@ -33,6 +33,10 @@ static const bool DEFAULT_SPLASHSCREEN = true; #define COLOR_TX_STATUS_DANGER QColor(200, 100, 100) /* Transaction list -- TX status decoration - default color */ #define COLOR_BLACK QColor(0, 0, 0) +/* Transaction list -- has conflicting transactions */ +#define COLOR_HASCONFLICTING QColor(255, 255, 255) +/* Transaction list -- has conflicting transactions - background */ +#define COLOR_HASCONFLICTING_BG QColor(192, 0, 0) /* Tooltips longer than this (in characters) are converted into rich text, so that they can be word-wrapped. diff --git a/src/qt/transactionfilterproxy.cpp b/src/qt/transactionfilterproxy.cpp index 7981eb7c9..fca95eece 100644 --- a/src/qt/transactionfilterproxy.cpp +++ b/src/qt/transactionfilterproxy.cpp @@ -25,7 +25,7 @@ TransactionFilterProxy::TransactionFilterProxy(QObject *parent) : watchOnlyFilter(WatchOnlyFilter_All), minAmount(0), limitRows(-1), - showInactive(true) + showInactive(false) { } @@ -41,7 +41,7 @@ bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex & qint64 amount = llabs(index.data(TransactionTableModel::AmountRole).toLongLong()); int status = index.data(TransactionTableModel::StatusRole).toInt(); - if(!showInactive && status == TransactionStatus::Conflicted) + if(!showInactive && status == TransactionStatus::Conflicted && type == TransactionRecord::Other) return false; if(!(TYPE(type) & typeFilter)) return false; diff --git a/src/qt/transactionrecord.cpp b/src/qt/transactionrecord.cpp index 5e1a3a2f2..f9626e2e9 100644 --- a/src/qt/transactionrecord.cpp +++ b/src/qt/transactionrecord.cpp @@ -195,6 +195,8 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx) status.depth = wtx.GetDepthInMainChain(); status.cur_num_blocks = chainActive.Height(); + status.hasConflicting = false; + if (!CheckFinalTx(wtx)) { if (wtx.nLockTime < LOCKTIME_THRESHOLD) @@ -238,6 +240,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx) if (status.depth < 0) { status.status = TransactionStatus::Conflicted; + status.hasConflicting = !(wtx.GetConflicts().empty()); } else if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0) { @@ -246,6 +249,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx) else if (status.depth == 0) { status.status = TransactionStatus::Unconfirmed; + status.hasConflicting = !(wtx.GetConflicts().empty()); if (wtx.isAbandoned()) status.status = TransactionStatus::Abandoned; } @@ -261,10 +265,10 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx) } -bool TransactionRecord::statusUpdateNeeded() +bool TransactionRecord::statusUpdateNeeded(int64_t nConflictsReceived) { AssertLockHeld(cs_main); - return status.cur_num_blocks != chainActive.Height(); + return (status.cur_num_blocks != chainActive.Height() || status.cur_num_conflicts != nConflictsReceived); } QString TransactionRecord::getTxID() const diff --git a/src/qt/transactionrecord.h b/src/qt/transactionrecord.h index 97aed1059..63d396337 100644 --- a/src/qt/transactionrecord.h +++ b/src/qt/transactionrecord.h @@ -20,9 +20,17 @@ class TransactionStatus { public: TransactionStatus(): - countsForBalance(false), sortKey(""), - matures_in(0), status(Offline), depth(0), open_for(0), cur_num_blocks(-1) - { } + countsForBalance(false), + sortKey(""), + matures_in(0), + status(Offline), + hasConflicting(false), + depth(0), + open_for(0), + cur_num_blocks(-1), + cur_num_conflicts(-1) + { + } enum Status { Confirmed, /**< Have 6 or more confirmations (normal tx) or fully mature (mined tx) **/ @@ -53,6 +61,10 @@ public: /** @name Reported status @{*/ Status status; + + // Has conflicting transactions spending same prevout + bool hasConflicting; + qint64 depth; qint64 open_for; /**< Timestamp if status==OpenUntilDate, otherwise number of additional blocks that need to be mined before @@ -61,6 +73,9 @@ public: /** Current number of blocks (to know whether cached status is still valid) */ int cur_num_blocks; + + /** Number of conflicts received into wallet as of last status update */ + int64_t cur_num_conflicts; }; /** UI model for a transaction. A core transaction can be represented by multiple UI transactions if it has @@ -138,7 +153,7 @@ public: /** Return whether a status update is needed. */ - bool statusUpdateNeeded(); + bool statusUpdateNeeded(int64_t nConflictsReceived); }; #endif // BITCOIN_QT_TRANSACTIONRECORD_H diff --git a/src/qt/transactiontablemodel.cpp b/src/qt/transactiontablemodel.cpp index b29ecf834..ea29865ce 100644 --- a/src/qt/transactiontablemodel.cpp +++ b/src/qt/transactiontablemodel.cpp @@ -166,8 +166,12 @@ public: parent->endRemoveRows(); break; case CT_UPDATED: - // Miscellaneous updates -- nothing to do, status update will take care of this, and is only computed for - // visible transactions. + Q_EMIT parent->dataChanged(parent->index(lowerIndex, parent->Status), parent->index(upperIndex-1, parent->Amount)); + break; + case CT_GOT_CONFLICT: + Q_EMIT parent->message(parent->tr("Conflict Received"), + parent->tr("WARNING: Transaction may never be confirmed. Its input was seen being spent by another transaction on the network."), + CClientUIInterface::MSG_WARNING); break; } } @@ -187,20 +191,21 @@ public: // stuck if the core is holding the locks for a longer time - for // example, during a wallet rescan. // - // If a status update is needed (blocks came in since last check), - // update the status of this transaction from the wallet. Otherwise, + // If a status update is needed (blocks or conflicts came in since last check), + // update the status of this transaction from the wallet. Otherwise, // simply re-use the cached status. TRY_LOCK(cs_main, lockMain); if(lockMain) { TRY_LOCK(wallet->cs_wallet, lockWallet); - if(lockWallet && rec->statusUpdateNeeded()) + if(lockWallet && rec->statusUpdateNeeded(wallet->nConflictsReceived)) { std::map::iterator mi = wallet->mapWallet.find(rec->hash); if(mi != wallet->mapWallet.end()) { rec->updateStatus(mi->second); + rec->status.cur_num_conflicts = wallet->nConflictsReceived; } } } @@ -247,6 +252,7 @@ TransactionTableModel::TransactionTableModel(const PlatformStyle *platformStyle, priv->refreshWallet(); connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit())); + connect(this, SIGNAL(message(QString,QString,unsigned int)), walletModel, SIGNAL(message(QString,QString,unsigned int))); subscribeToCoreSignals(); } @@ -380,6 +386,8 @@ QString TransactionTableModel::formatTxType(const TransactionRecord *wtx) const return tr("Payment to yourself"); case TransactionRecord::Generated: return tr("Mined"); + case TransactionRecord::Other: + return tr("Other"); default: return QString(); } @@ -577,7 +585,13 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const return formatTooltip(rec); case Qt::TextAlignmentRole: return column_alignments[index.column()]; + case Qt::BackgroundColorRole: + if (rec->status.hasConflicting) + return COLOR_HASCONFLICTING_BG; + break; case Qt::ForegroundRole: + if (rec->status.hasConflicting) + return COLOR_HASCONFLICTING; // Use the "danger" color for abandoned transactions if(rec->status.status == TransactionStatus::Abandoned) { diff --git a/src/qt/transactiontablemodel.h b/src/qt/transactiontablemodel.h index 6932646e1..048b64451 100644 --- a/src/qt/transactiontablemodel.h +++ b/src/qt/transactiontablemodel.h @@ -104,6 +104,10 @@ private: QVariant txWatchonlyDecoration(const TransactionRecord *wtx) const; QVariant txAddressDecoration(const TransactionRecord *wtx) const; +Q_SIGNALS: + // Fired when a message should be reported to the user + void message(const QString &title, const QString &message, unsigned int style); + public Q_SLOTS: /* New transaction, or transaction changed status */ void updateTransaction(const QString &hash, int status, bool showTransaction); diff --git a/src/ui_interface.h b/src/ui_interface.h index ef72ced16..a7628ca0f 100644 --- a/src/ui_interface.h +++ b/src/ui_interface.h @@ -22,7 +22,8 @@ enum ChangeType { CT_NEW, CT_UPDATED, - CT_DELETED + CT_DELETED, + CT_GOT_CONFLICT }; /** Signals for UI communication. */ diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index bd8b19330..b38e77b63 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1196,6 +1196,20 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletD // Notify UI of new or updated transaction NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED); + // Notifications for existing transactions that now have conflicts with this one + if (fInsertedNew) + { + BOOST_FOREACH(const uint256& conflictHash, wtxIn.GetConflicts()) + { + CWalletTx& txConflict = mapWallet[conflictHash]; + NotifyTransactionChanged(this, conflictHash, CT_UPDATED); //Updates UI table + if (IsFromMe(txConflict) || IsMine(txConflict)) + { + NotifyTransactionChanged(this, conflictHash, CT_GOT_CONFLICT); //Throws dialog + } + } + } + // notify an external script when a wallet transaction comes in or is updated std::string strCmd = GetArg("-walletnotify", ""); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 159cb9806..f87feb20c 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -535,6 +535,9 @@ public: MasterKeyMap mapMasterKeys; unsigned int nMasterKeyMaxID; + // Increment to cause UI refresh, similar to new block + int64_t nConflictsReceived; + CWallet() { SetNull(); @@ -566,6 +569,7 @@ public: nLastResend = 0; nTimeFirstKey = 0; fBroadcastTransactions = false; + nConflictsReceived = 0; } std::map mapWallet;