From 448872f51691ae01cf2002ad38f3bda3cd5bd31d Mon Sep 17 00:00:00 2001 From: Alexander Miller Date: Mon, 30 Nov 2026 13:48:47 -0500 Subject: [PATCH] qt: serve transaction-list labels from the address table's rows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The transaction list resolved each row's label with `AddressTableModel::labelForAddress`, which asks the wallet under a blocking `cs_wallet` lock. That ran on every repaint of a visible row and on every filter or sort pass over the history, and it waited whenever the wallet held its lock, for example for each connected block. On a wallet with a long history this freezes the GUI while the node catches up. The transaction table now reads labels from the address table model's rows (`cachedLabelForAddress`). It keeps the decode-then-encode normalisation of the wallet lookup and gives no label to an address that does not decode, as the wallet does. Its label source is the address table model the wallet model was created with, which lists every address, and the wallet model passes it explicitly. That holds after the Sign Message dialog has had `WalletModel::refresh()` hand out a legacy-only model. `WalletModel` registers for address-book changes before its address table model reads the address book, so changes made while the history loads are no longer lost, and it updates the complete model and the current one only. Because nothing reads the wallet on repaint any more, an address-book change invalidates the transaction rows that show that address. Notifications can arrive in another order than the writes they report (`CWallet::SetAddressBookWithDB` notifies after releasing the wallet lock), so each one is also followed by a comparison of the entries it named with the wallet. That comparison runs from the event loop and uses a new non-blocking read, `interfaces::Wallet::tryGetAddresses()`, a single `TRY_LOCK(cs_wallet)` for every recorded address. Where the complete address table model's row differs from the wallet's entry, it applies the wallet's state the way a notification would. An address whose encoding does not decode to a destination the wallet can look up is left as its notifications left it, and if a listener delivers a newer notification while a correction is applied, the remaining entries are read again rather than overwritten with the older read. While the lock is busy, as while a block is connected or the wallet is being encrypted, it retries after 250 ms and never waits. However the notifications were ordered, the models end at the wallet's state. The address table models still take a change at once, inside the wallet's notification; for a change made on the GUI thread, `CWallet::DelAddressBook` and `CWallet::GetNewDestination` raise it with the wallet lock held. The transaction table only records the address there, and only if its label changed: the unlabelled receiving entries the wallet files while it syncs or rescans, purpose-only changes and the removal of unlabelled entries cost nothing. It makes one pass over the history from the event loop, after the notification has returned. That one pass covers every address changed meanwhile, so a burst of changes costs one pass instead of one per change. The pass sends a single `dataChanged()` for the address column, from the first affected row to the last. A single signal is needed for correctness: `QSortFilterProxyModel` sorts the rows of each signal against the rest, assuming the rest is in order, so per-range signals left a list sorted by label wrong. Filters still see every covered row, and a list sorted by date is not sorted again. If a listener adds or removes rows meanwhile, the pass is repeated instead of dropping rows. Dialog actions (coin control, send entries, the edit-address duplicate check) and `purposeForAddress` still read the wallet. Tests use the synthetic wallet and a real descriptor wallet. They cover: - no `getAddress` call and no wait while another thread holds the wallet lock; - equivalence with the wallet across address-book states and a `pk_hash_only` refresh; - both delivery orders of a payment and its label; - changes during model construction; - no row signal inside the wallet's notification; - one pass for a burst; - one signal for scattered rows, with search and address sort following; - re-notification after a row change; - stored address text in upper case or with white space; - undecodable addresses; - no pass for a change that leaves the label as it was; - reordered notifications (relabel, add and delete) converging on the wallet's state, for the table, the rows, a search and an address sort; - the comparison not blocking while the wallet lock is held, and retrying; - one wallet read per burst; - no change when the notifications were in order; - deletion of the wallet model with a comparison pending; - an entry the wallet holds under a destination its address does not name left alone; - a newer notification delivered during the comparison not overwritten; - the legacy-only model for entries relabelled, added and removed. Each fails without its part of the change. Measured on 100,000 synthetic rows with two proxies: - a relabel of an address shown by half the rows: about 0.35 s, none of it under the wallet lock (before: 0.3 s, all of it inside the notification); - a burst of 1,000 relabels: about 0.35 s (before: 1.1 to 1.7 s); - a relabel of a rarely used address now costs one filter pass over the span of its rows (about 0.35 s here). Known limits: a failed address-book database write sends no notification, so the list keeps the old label until the next change to that address or a restart. While the wallet lock is busy, a label from notifications that arrived out of order can show until the next retry finds the lock free (every 250 ms). The non-blocking read calls `IsMine`, which can wait on the wallet's descriptor locks as `tryGetTxStatus` can. A row inserted between a label change made on the GUI thread and the following label pass can be placed wrongly in a list sorted on the address column if its own label did not change; the next sort corrects it. Each lookup still decodes and re-encodes the address. **Proposed release note** > GUI: The transaction list now takes labels from the GUI's copy of the address book instead of asking the wallet on every repaint, so scrolling, searching and sorting a long history no longer stall while the wallet is busy, for example while the node catches up. When a label changes, the list updates the affected rows once the change has been applied, in a single pass for any number of changes made together (such as a rescan or a script that sets many labels), and a list sorted by label is re-sorted correctly. On a wallet with a very long history, changing a label can take a fraction of a second while the list filters its rows again. A label changed from the RPC console or another RPC client appears in the list as soon as the GUI processes the change, also when several clients change the same label at once. The Sending and Receiving addresses windows also keep updating after the Sign Message dialog's address book has been used. (#445) Refs #445 🤖 Generated with [Claude Code](https://claude.com/claude-code) Squashed commits: c9202a4c3328 qt: serve transaction-list labels from the address table's rows fdc77b3d19ff qt: keep the label tests independent of host pauses 35fae9f76980 qt: keep transaction labels on the complete address table model 88065de2104c qt: register for address book changes before reading the address book 8964afedb132 qt: update only the address table models in use a634bd5dacd8 qt: notify changed labels' rows once per event-loop pass 51e2985a872e qt: give no label to an address that does not decode 5cf0dc198552 qt: hand the transaction table its label source ad5224b9a37b qt: reserve the label tests' vectors before filling them 4bae798f1928 qt: skip the label pass when an entry's label stays the same 0efeedae4c67 qt, wallet: add a try-lock read of address book entries 5803d8fecbb2 qt: compare notified address book entries with the wallet 4afa5dbf8faa qt: leave unaddressable entries alone and reread after nested changes Co-Authored-By: Claude Opus 5.5 Squashed-from: 4afa5dbf8faa3c1323eaa109bd6c902a79beba6d --- src/interfaces/wallet.h | 6 + src/qt/addresstablemodel.cpp | 34 +- src/qt/addresstablemodel.h | 16 + src/qt/test/syntheticwallet.cpp | 61 +- src/qt/test/syntheticwallet.h | 21 + src/qt/test/wallettests.cpp | 1648 ++++++++++++++++++++++++++++++ src/qt/transactiontablemodel.cpp | 99 +- src/qt/transactiontablemodel.h | 37 +- src/qt/walletmodel.cpp | 139 ++- src/qt/walletmodel.h | 17 + src/wallet/interfaces.cpp | 29 +- 11 files changed, 2084 insertions(+), 23 deletions(-) diff --git a/src/interfaces/wallet.h b/src/interfaces/wallet.h index 4a8d88869c..ad28c9167b 100644 --- a/src/interfaces/wallet.h +++ b/src/interfaces/wallet.h @@ -151,6 +151,12 @@ class Wallet //! Get wallet address list. virtual std::vector getAddresses() = 0; + //! Get the address book entries for dests, as getAddresses() lists them, + //! if the wallet lock is free. entries[i] is the entry for dests[i], or + //! std::nullopt if the wallet has no such entry other than a change one. + //! Returns false, and leaves entries alone, if the lock is busy. + virtual bool tryGetAddresses(const std::vector& dests, std::vector>& entries) = 0; + //! Get output types the wallet can hand out as receive addresses. virtual std::vector getAvailableAddressTypes() = 0; diff --git a/src/qt/addresstablemodel.cpp b/src/qt/addresstablemodel.cpp index 9ce3c06370..41370497e0 100644 --- a/src/qt/addresstablemodel.cpp +++ b/src/qt/addresstablemodel.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -170,6 +171,16 @@ class AddressTablePriv return &*it; } + /* Find the entry for a destination, encoded as the rows are. The wallet + has no entry for a destination that is not valid, such as an address + that does not decode, so neither does this, even if a row's encoding + is the empty string that such a destination encodes to. */ + const AddressTableEntry* find(const CTxDestination& dest) const + { + if (!IsValidDestination(dest)) return nullptr; + return find(QString::fromStdString(EncodeDestination(dest))); + } + AddressTableEntry *index(int idx) { if(idx >= 0 && idx < cachedAddressTable.size()) @@ -438,6 +449,23 @@ QString AddressTableModel::labelForAddress(const QString &address) const return QString(); } +QString AddressTableModel::cachedLabelForAddress(const QString &address) const +{ + // Decode the address as getAddressData() does, so that a non-canonical + // or invalid string finds the entry the wallet would find. + const AddressTableEntry* entry = priv->find(DecodeDestination(address.toStdString())); + return entry ? entry->label : QString(); +} + +std::optional AddressTableModel::changeToEntry(const QString &address, const interfaces::WalletAddress* entry) const +{ + const AddressTableEntry* row = priv->find(address); + if (!entry) return row ? std::optional{CT_DELETED} : std::nullopt; + if (!row) return CT_NEW; + if (row->label == QString::fromStdString(entry->name) && row->type == translateTransactionType(entry->purpose, entry->is_mine)) return std::nullopt; + return CT_UPDATED; +} + std::optional AddressTableModel::purposeForAddress(const QString &address) const { wallet::AddressPurpose purpose; @@ -453,10 +481,10 @@ bool AddressTableModel::getAddressData(const QString &address, CTxDestination destination = DecodeDestination(address.toStdString()); // The wallet blocks while an encryption holds its locks, whoever started // it, and the address book cannot change until it returns, so answer from - // the rows this model already holds. A repaint of the transaction list - // looks up its labels here without any user input. + // the rows this model already holds. The dialogs that look up a label + // or a purpose here would otherwise wait until the encryption ends. if (walletModel->isEncryptingWallet()) { - const AddressTableEntry* entry = priv->find(QString::fromStdString(EncodeDestination(destination))); + const AddressTableEntry* entry = priv->find(destination); if (!entry) return false; if (name) *name = entry->label.toStdString(); if (purpose) *purpose = translateAddressPurpose(entry->type); diff --git a/src/qt/addresstablemodel.h b/src/qt/addresstablemodel.h index 04969462f9..5c311ab038 100644 --- a/src/qt/addresstablemodel.h +++ b/src/qt/addresstablemodel.h @@ -17,6 +17,7 @@ class WalletModel; namespace interfaces { class Wallet; +struct WalletAddress; } namespace wallet { enum class AddressPurpose; @@ -75,9 +76,24 @@ class AddressTableModel : public QAbstractTableModel /** Look up label for address in address book, if not found return empty string. */ QString labelForAddress(const QString &address) const; + /** Look up label for address in this model's rows, without reading the + wallet. If not found return empty string. The rows follow the + wallet's address book notifications, so the answer lags the wallet + until those are delivered: use it to show a label, as the transaction + list does on every repaint and filter pass, not to decide what to + write. */ + QString cachedLabelForAddress(const QString &address) const; + /** Look up purpose for address in address book, if not found return empty string. */ std::optional purposeForAddress(const QString &address) const; + /** The status (a ChangeType) updateEntry() needs to make this model's + row for an encoded address show entry, the address book entry the + wallet lists for it, or to remove the row if entry is null. + std::nullopt if the row already shows it: same label and type, or no + row and no entry. */ + std::optional changeToEntry(const QString &address, const interfaces::WalletAddress* entry) const; + /* Look up row index of an address in the model. Return -1 if not found. */ diff --git a/src/qt/test/syntheticwallet.cpp b/src/qt/test/syntheticwallet.cpp index 1199aff094..4e9bfded13 100644 --- a/src/qt/test/syntheticwallet.cpp +++ b/src/qt/test/syntheticwallet.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -196,6 +197,8 @@ class SyntheticWallet : public interfaces::Wallet bool getAddress(const CTxDestination& dest, std::string* name, wallet::AddressPurpose* purpose) override { std::unique_lock lock{m_state->mutex}; + ++m_state->get_address_calls; + m_state->condition.notify_all(); WaitForEncryption(lock); for (const auto& entry : m_state->address_book) { if (entry.dest != dest) continue; @@ -207,9 +210,39 @@ class SyntheticWallet : public interfaces::Wallet } std::vector getAddresses() override { - std::unique_lock lock{m_state->mutex}; - WaitForEncryption(lock); - return m_state->address_book; + std::function entered; + std::function returning; + { + std::lock_guard lock{m_state->mutex}; + entered = m_state->get_addresses_entered; + returning = m_state->get_addresses_returning; + } + if (entered) entered(); + std::vector addresses; + { + std::unique_lock lock{m_state->mutex}; + WaitForEncryption(lock); + addresses = m_state->address_book; + } + if (returning) returning(); + return addresses; + } + bool tryGetAddresses(const std::vector& dests, std::vector>& entries) override + { + std::lock_guard lock{m_state->mutex}; + ++m_state->try_get_addresses_calls; + if (m_state->encrypt_in_progress || m_state->WalletLockExcludesCaller()) return false; + entries.clear(); + entries.reserve(dests.size()); + for (const CTxDestination& dest : dests) { + const auto entry{std::find_if(m_state->address_book.begin(), m_state->address_book.end(), [&dest](const auto& e) { return e.dest == dest; })}; + if (entry == m_state->address_book.end()) { + entries.emplace_back(); + } else { + entries.emplace_back(*entry); + } + } + return true; } std::vector getAvailableAddressTypes() override { return {OutputType::P2MR}; } std::vector getAddressReceiveRequests() override { return {}; } @@ -549,7 +582,7 @@ class SyntheticWallet : public interfaces::Wallet bool tryGetTxStatus(const Txid& txid, interfaces::WalletTxStatus& tx_status, int& num_blocks, int64_t& block_time) override { std::lock_guard lock{m_state->mutex}; - if (m_state->encrypt_in_progress || !m_state->wallet_tx || m_state->wallet_tx->tx->GetHash() != txid) return false; + if (m_state->encrypt_in_progress || m_state->WalletLockExcludesCaller() || !m_state->wallet_tx || m_state->wallet_tx->tx->GetHash() != txid) return false; tx_status = m_state->wallet_tx_status; num_blocks = tx_status.block_height; block_time = 0; @@ -786,7 +819,20 @@ class SyntheticWallet : public interfaces::Wallet } }); } - std::unique_ptr handleAddressBookChanged(AddressBookChangedFn) override { return interfaces::MakeCleanupHandler([] {}); } + std::unique_ptr handleAddressBookChanged(AddressBookChangedFn fn) override + { + { + std::lock_guard lock{m_state->mutex}; + m_state->address_book_changed = std::move(fn); + } + std::weak_ptr weak_state{m_state}; + return interfaces::MakeCleanupHandler([weak_state] { + if (auto state = weak_state.lock()) { + std::lock_guard lock{state->mutex}; + state->address_book_changed = {}; + } + }); + } std::unique_ptr handleTransactionChanged(TransactionChangedFn fn) override { uint64_t id; @@ -835,10 +881,11 @@ class SyntheticWallet : public interfaces::Wallet private: //! Stand in for the wallet locks that CWallet::EncryptWallet holds for - //! the whole operation. + //! the whole operation, or that another thread holds while + //! wallet_lock_held is set. void WaitForEncryption(std::unique_lock& lock) const { - m_state->condition.wait(lock, [this] { return !m_state->encrypt_in_progress; }); + m_state->condition.wait(lock, [this] { return !m_state->encrypt_in_progress && !m_state->WalletLockExcludesCaller(); }); } wallet::PQCUsageReport m_report; diff --git a/src/qt/test/syntheticwallet.h b/src/qt/test/syntheticwallet.h index b2ad11d769..dc19eb860c 100644 --- a/src/qt/test/syntheticwallet.h +++ b/src/qt/test/syntheticwallet.h @@ -91,6 +91,27 @@ struct SyntheticWalletState { //! The address book, returned by getAddresses() and searched by //! getAddress(). Both wait on the encryption latch. std::vector address_book; + //! Called by getAddresses() before and after it reads address_book, so + //! a test can change the address book around that read. + std::function get_addresses_entered; + std::function get_addresses_returning; + //! Number of getAddress() calls, counted before the call waits, with + //! condition notified so a test can see the call. + int get_address_calls{0}; + //! Number of tryGetAddresses() calls, which give up instead of waiting. + int try_get_addresses_calls{0}; + //! Listener for the address book notification, so a test can raise it + //! the way the wallet does after changing address_book. + interfaces::Wallet::AddressBookChangedFn address_book_changed; + //! Stand-in for another thread holding the wallet lock, as the wallet + //! does while it connects a block: calls that wait on the encryption + //! latch also wait while this is set, and tryGetTxStatus() gives up. + bool wallet_lock_held{false}; + //! The thread holding it, if a test sets one. As cs_wallet is a + //! recursive mutex, that thread's own calls neither wait nor give up. + std::thread::id wallet_lock_owner; + //! Whether the lock stand-in keeps the calling thread out. + bool WalletLockExcludesCaller() const { return wallet_lock_held && wallet_lock_owner != std::this_thread::get_id(); } bool psbt_sign_entered{false}; bool allow_psbt_reservation{true}; bool allow_psbt_completion{true}; diff --git a/src/qt/test/wallettests.cpp b/src/qt/test/wallettests.cpp index f5d507a858..91a25e518b 100644 --- a/src/qt/test/wallettests.cpp +++ b/src/qt/test/wallettests.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -51,12 +52,15 @@ #include #include #include +#include #include #include #include #include #include +#include +#include #include #include #include @@ -95,6 +99,7 @@ #include #include #include +#include #include #include #include @@ -4135,6 +4140,1628 @@ void TestEncryptWalletRepaintUsesCachedLabels(interfaces::Node& node) }, 5000)); } +//! A payment from elsewhere to the given addresses of the wallet, one output +//! each, which the transaction list shows as one row per output in output +//! order. prevout_n makes the transaction id distinct. +interfaces::WalletTx MakeSyntheticPayment(const std::vector& destinations, uint32_t prevout_n) +{ + CMutableTransaction mtx; + mtx.vin.emplace_back(COutPoint{Txid{}, prevout_n}); + for (const CTxDestination& dest : destinations) mtx.vout.emplace_back(COIN, GetScriptForDestination(dest)); + return interfaces::WalletTx{ + .tx = MakeTransactionRef(mtx), + .txin_is_mine = {false}, + .txout_is_mine = std::vector(destinations.size(), true), + .txout_is_change = std::vector(destinations.size(), false), + .txout_address = destinations, + .txout_address_is_mine = std::vector(destinations.size(), true), + .credit = static_cast(destinations.size()) * COIN, + .debit = 0, + .change = 0, + .time = 1, + .value_map = {}, + .is_coinbase = false, + }; +} + +//! Change the synthetic wallet's address book and raise its notification, as +//! CWallet::SetAddressBook and DelAddressBook do. Run it on the GUI thread +//! for a change the GUI makes, which the wallet model receives at once, or +//! on another thread for one an RPC makes, which it receives queued. +std::function SyntheticAddressBookChange(const std::shared_ptr& state, + const CTxDestination& dest, const std::string& label, + wallet::AddressPurpose purpose, ChangeType status) +{ + return [state, dest, label, purpose, status] { + const bool is_mine{purpose == wallet::AddressPurpose::RECEIVE}; + interfaces::Wallet::AddressBookChangedFn notify; + { + std::lock_guard lock{state->mutex}; + auto& book{state->address_book}; + const auto entry{std::find_if(book.begin(), book.end(), [&dest](const auto& e) { return e.dest == dest; })}; + if (status == CT_DELETED) { + if (entry != book.end()) book.erase(entry); + } else if (entry == book.end()) { + book.emplace_back(dest, is_mine, purpose, label); + } else { + entry->name = label; + entry->purpose = purpose; + } + notify = state->address_book_changed; + } + if (notify) notify(dest, label, is_mine, purpose, status); + }; +} + +//! Make a transaction the synthetic wallet's own and raise its notification, +//! as the wallet does when it adds or updates a transaction. +std::function SyntheticTransactionChange(const std::shared_ptr& state, + const interfaces::WalletTx& wtx, ChangeType status) +{ + return [state, wtx, status] { + std::vector notify; + { + std::lock_guard lock{state->mutex}; + state->wallet_tx = wtx; + for (const auto& [id, fn] : state->transaction_changed) notify.push_back(fn); + } + for (const auto& fn : notify) fn(wtx.tx->GetHash(), status); + }; +} + +//! Deliver the posted events as the event loop does, and then what they +//! post in turn: address book notifications queue the transaction table's +//! label pass and the wallet model's comparison with the wallet, and a +//! correction that comparison makes queues another label pass. +void DeliverPostedEvents() +{ + for (int round = 0; round < 3; ++round) QCoreApplication::sendPostedEvents(); +} + +//! Raise wallet notifications from another thread, as the wallet does when an +//! RPC or a connected block changes it, so the models receive them queued, +//! then deliver them as the event loop does. The notifications only post +//! events, so the thread cannot wait on the GUI thread, and everything they +//! posted is queued once it has ended. +void NotifyFromOtherThread(const std::function& notify) +{ + std::thread thread{notify}; + thread.join(); + DeliverPostedEvents(); +} + +//! Set the synthetic wallet's address book entry for dest, or remove it if +//! label is std::nullopt, without a notification: what a writer leaves in +//! the wallet before it notifies. +void SetSyntheticAddressBookEntry(const std::shared_ptr& state, const CTxDestination& dest, + const std::optional& label, wallet::AddressPurpose purpose) +{ + std::lock_guard lock{state->mutex}; + auto& book{state->address_book}; + const auto entry{std::find_if(book.begin(), book.end(), [&dest](const auto& e) { return e.dest == dest; })}; + if (!label) { + if (entry != book.end()) book.erase(entry); + } else if (entry == book.end()) { + book.emplace_back(dest, /*is_mine=*/purpose == wallet::AddressPurpose::RECEIVE, purpose, *label); + } else { + entry->name = *label; + entry->purpose = purpose; + } +} + +//! Number of tryGetAddresses() calls the synthetic wallet has had. +int SyntheticWalletReads(qt_test::SyntheticWalletState& state) +{ + std::lock_guard lock{state.mutex}; + return state.try_get_addresses_calls; +} + +//! Raise an address book notification without changing the synthetic +//! wallet's address book, as a writer does whose notification comes after +//! another writer has changed the same entry again. +std::function SyntheticLateAddressBookNotification(const std::shared_ptr& state, + const CTxDestination& dest, const std::string& label, + wallet::AddressPurpose purpose, ChangeType status) +{ + return [state, dest, label, purpose, status] { + interfaces::Wallet::AddressBookChangedFn notify; + { + std::lock_guard lock{state->mutex}; + notify = state->address_book_changed; + } + if (notify) notify(dest, label, /*is_mine=*/purpose == wallet::AddressPurpose::RECEIVE, purpose, status); + }; +} + +//! The rows the transaction table's dataChanged() signals in changed covered, +//! with -1 if one covered more than the address column, the only column that +//! shows the label. +std::set NotifiedLabelRows(const QSignalSpy& changed) +{ + std::set rows; + for (const QList& args : changed) { + const QModelIndex top{args.at(0).value()}; + const QModelIndex bottom{args.at(1).value()}; + if (top.column() != TransactionTableModel::ToAddress || bottom.column() != TransactionTableModel::ToAddress) rows.insert(-1); + for (int row = top.row(); row <= bottom.row(); ++row) rows.insert(row); + } + return rows; +} + +//! Run a function when leaving the scope. Declared after a JoinOnExit, it +//! releases the helper thread when a failed check returns early, so the +//! join does not wait for the thread's bound. +struct RunOnExit { + std::function run; + + ~RunOnExit() { run(); } +}; + +QString TransactionLabel(const TransactionTableModel& table, int row) +{ + return table.index(row, TransactionTableModel::ToAddress).data(TransactionTableModel::LabelRole).toString(); +} + +//! Transaction rows show labels from the address table model's rows. Reading +//! the label roles, painting the list, filtering it by label and sorting it +//! by the label column never ask the wallet, which makes each such read wait +//! whenever another thread holds the wallet lock, as the wallet does for each +//! block it connects (#445). +void TestTransactionLabelsSkipWalletLock(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{PKHash{*uint160::FromHex("0000000000000000000000000000000000000011")}}; + const CTxDestination bob{WitnessV2P2MR{*uint256::FromHex("0000000000000000000000000000000000000000000000000000000000000012")}}; + const CTxDestination blank{WitnessV2P2MR{*uint256::FromHex("0000000000000000000000000000000000000000000000000000000000000013")}}; + const CTxDestination unknown{WitnessV2P2MR{*uint256::FromHex("0000000000000000000000000000000000000000000000000000000000000014")}}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->address_book.emplace_back(bob, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "bob"); + state->address_book.emplace_back(blank, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, ""); + state->wallet_tx = MakeSyntheticPayment({alice, bob, blank, unknown}, 1); + state->wallet_tx_status.block_height = 1; + state->wallet_tx_status.depth_in_main_chain = 1; + state->wallet_tx_status.is_trusted = true; + state->wallet_tx_status.is_in_main_chain = true; + + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + TransactionView view{env.platform_style.get()}; + view.setModel(model.get()); + view.resize(1000, 600); + TransactionFilterProxy* const proxy{view.findChild()}; + QTableView* const list{view.findChild()}; + QVERIFY(proxy); + QVERIFY(list); + QCOMPARE(table->rowCount({}), 4); + QCOMPARE(proxy->rowCount({}), 4); + + // Another thread takes the wallet lock and holds it until the GUI thread + // is done. It lets go as soon as the GUI thread asks the wallet for an + // address, a call that waits for the lock, so that the test fails at once + // rather than hanging; the bound only ends a GUI thread that never + // finishes, and no pause of the process can fail the test. + int calls_before{0}; + { + std::lock_guard lock{state->mutex}; + state->wallet_lock_held = true; + calls_before = state->get_address_calls; + } + bool gui_done{false}; + bool held_throughout{false}; + std::thread holder{[state, &gui_done, &held_throughout, calls_before] { + std::unique_lock lock{state->mutex}; + const bool woken{state->condition.wait_for(lock, std::chrono::seconds{60}, [&] { + return gui_done || state->get_address_calls != calls_before; + })}; + held_throughout = woken && state->get_address_calls == calls_before; + state->wallet_lock_held = false; + lock.unlock(); + state->condition.notify_all(); + }}; + const auto finish{[&state, &gui_done] { + { + std::lock_guard lock{state->mutex}; + gui_done = true; + } + state->condition.notify_all(); + }}; + const JoinOnExit join_holder{holder}; + const RunOnExit finish_on_exit{finish}; + + // What a repaint, a tooltip, a copy and an export of each row ask for. + const std::array labels{QStringLiteral("alice"), QStringLiteral("bob"), QString{}, QString{}}; + for (int row = 0; row < 4; ++row) { + const QModelIndex index{table->index(row, TransactionTableModel::ToAddress)}; + const QString address{index.data(TransactionTableModel::AddressRole).toString()}; + QCOMPARE(index.data(TransactionTableModel::LabelRole).toString(), labels[row]); + QCOMPARE(index.data(Qt::EditRole).toString(), QStringLiteral("%1 (%2)").arg(labels[row], address)); + QVERIFY(index.data(Qt::ToolTipRole).toString().endsWith(QStringLiteral("%1 (%2)").arg(labels[row], address))); + QCOMPARE(index.data(Qt::ForegroundRole).isValid(), labels[row].isEmpty()); + QVERIFY(index.data(TransactionTableModel::TxPlainTextRole).toString().contains(address)); + } + view.grab(); + proxy->setSearchString(QStringLiteral("alice")); + QCOMPARE(proxy->rowCount({}), 1); + QCOMPARE(proxy->index(0, 0).data(TransactionTableModel::LabelRole).toString(), QStringLiteral("alice")); + proxy->setSearchString(QStringLiteral("BOB")); + QCOMPARE(proxy->rowCount({}), 1); + proxy->setSearchString(QString{}); + QCOMPARE(proxy->rowCount({}), 4); + // Sorting by the label column orders the unlabelled rows, shown as + // " (address)", before the labelled ones. + list->sortByColumn(TransactionTableModel::ToAddress, Qt::AscendingOrder); + QCOMPARE(proxy->index(2, 0).data(TransactionTableModel::LabelRole).toString(), QStringLiteral("alice")); + QCOMPARE(proxy->index(3, 0).data(TransactionTableModel::LabelRole).toString(), QStringLiteral("bob")); + view.grab(); + finish(); + holder.join(); + QVERIFY2(held_throughout, "GUI thread asked the wallet for a label while another thread held the wallet lock"); +} + +//! What one order of delivering a payment and its label left behind. +struct LabelDeliveryOutcome { + int table_rows{0}; + int shown_rows{0}; + QString label; + int address_rows{0}; + + bool operator==(const LabelDeliveryOutcome&) const = default; +}; + +//! Deliver a payment to a new address and the label for that address in the +//! given order, then again, while the transaction list searches for the +//! label. +void DeliverPaymentAndLabel(SyntheticModelEnvironment& env, bool label_first, LabelDeliveryOutcome& outcome) +{ + auto state{std::make_shared()}; + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + TransactionView view{env.platform_style.get()}; + view.setModel(model.get()); + TransactionFilterProxy* const proxy{view.findChild()}; + QVERIFY(proxy); + proxy->setSearchString(QStringLiteral("carol")); + QCOMPARE(table->rowCount({}), 0); + + const CTxDestination carol{WitnessV2P2MR{*uint256::FromHex("0000000000000000000000000000000000000000000000000000000000000021")}}; + const auto payment{SyntheticTransactionChange(state, MakeSyntheticPayment({carol}, 2), CT_NEW)}; + const auto label{SyntheticAddressBookChange(state, carol, "carol", wallet::AddressPurpose::RECEIVE, CT_NEW)}; + if (label_first) { + NotifyFromOtherThread(label); + QCOMPARE(addresses->rowCount({}), 1); + QCOMPARE(table->rowCount({}), 0); + NotifyFromOtherThread(payment); + QCOMPARE(table->rowCount({}), 1); + } else { + // The payment arrives first, as when an RPC sends it and then sets + // the label: the search hides the row while its label is unknown. + NotifyFromOtherThread(payment); + QCOMPARE(table->rowCount({}), 1); + QCOMPARE(proxy->rowCount({}), 0); + NotifyFromOtherThread(label); + QCOMPARE(addresses->rowCount({}), 1); + } + // Shown once both, and the label pass they queue, have been delivered. + QCOMPARE(proxy->rowCount({}), 1); + + // Repeated and updating notifications change nothing. + NotifyFromOtherThread([&] { + label(); + payment(); + SyntheticAddressBookChange(state, carol, "carol", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + SyntheticTransactionChange(state, MakeSyntheticPayment({carol}, 2), CT_UPDATED)(); + }); + outcome = { + .table_rows = table->rowCount({}), + .shown_rows = proxy->rowCount({}), + .label = TransactionLabel(*table, 0), + .address_rows = addresses->rowCount({}), + }; +} + +//! A payment and the label for its address reach the GUI as separate queued +//! notifications, in either order. Rows show the label from the address +//! table model, so a label update invalidates the rows that show the +//! address: a search for the label finds the row whichever arrives first, +//! and repeated notifications leave a single row. +void TestTransactionLabelDeliveryOrder(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + LabelDeliveryOutcome payment_first; + DeliverPaymentAndLabel(env, /*label_first=*/false, payment_first); + if (QTest::currentTestFailed()) return; + LabelDeliveryOutcome label_first; + DeliverPaymentAndLabel(env, /*label_first=*/true, label_first); + if (QTest::currentTestFailed()) return; + QVERIFY(payment_first == label_first); + QCOMPARE(payment_first.table_rows, 1); + QCOMPARE(payment_first.shown_rows, 1); + QCOMPARE(payment_first.label, QStringLiteral("carol")); + QCOMPARE(payment_first.address_rows, 1); +} + +//! Editing or removing a label while the transaction list is shown updates +//! the rows that show that address. They are notified in one signal for the +//! address column, from the first row that shows the address to the last. +void TestLabelChangeUpdatesItsRows(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{*uint256::FromHex("0000000000000000000000000000000000000000000000000000000000000031")}}; + const CTxDestination bob{WitnessV2P2MR{*uint256::FromHex("0000000000000000000000000000000000000000000000000000000000000032")}}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->address_book.emplace_back(bob, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "bob"); + // Rows 0 and 2 show alice, row 1 shows bob. + state->wallet_tx = MakeSyntheticPayment({alice, bob, alice}, 3); + + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + TransactionView view{env.platform_style.get()}; + view.setModel(model.get()); + view.resize(1000, 600); + view.show(); + TransactionFilterProxy* const proxy{view.findChild()}; + QVERIFY(proxy); + proxy->setSearchString(QStringLiteral("carol")); + QCOMPARE(table->rowCount({}), 3); + QCOMPARE(proxy->rowCount({}), 0); + + QSignalSpy changed{table, &QAbstractItemModel::dataChanged}; + QVERIFY(changed.isValid()); + + // Rename alice from the GUI, as the address book page does: the wallet + // notifies the model at once, and its rows once the event loop runs. + SyntheticAddressBookChange(state, alice, "carol", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + QCoreApplication::sendPostedEvents(); + QCOMPARE(changed.size(), 1); + QCOMPARE(NotifiedLabelRows(changed), (std::set{0, 1, 2})); + QCOMPARE(TransactionLabel(*table, 0), QStringLiteral("carol")); + QCOMPARE(TransactionLabel(*table, 1), QStringLiteral("bob")); + QCOMPARE(TransactionLabel(*table, 2), QStringLiteral("carol")); + QCOMPARE(proxy->rowCount({}), 2); + view.grab(); + + // Remove it. + changed.clear(); + SyntheticAddressBookChange(state, alice, "", wallet::AddressPurpose::RECEIVE, CT_DELETED)(); + QCoreApplication::sendPostedEvents(); + QCOMPARE(changed.size(), 1); + QCOMPARE(NotifiedLabelRows(changed), (std::set{0, 1, 2})); + QCOMPARE(TransactionLabel(*table, 0), QString{}); + QCOMPARE(TransactionLabel(*table, 1), QStringLiteral("bob")); + QCOMPARE(TransactionLabel(*table, 2), QString{}); + QCOMPARE(proxy->rowCount({}), 0); + + // Relabel bob from another thread, as the setlabel RPC does. + changed.clear(); + NotifyFromOtherThread(SyntheticAddressBookChange(state, bob, "carol", wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + QCOMPARE(changed.size(), 1); + QCOMPARE(NotifiedLabelRows(changed), (std::set{1})); + QCOMPARE(TransactionLabel(*table, 1), QStringLiteral("carol")); + QCOMPARE(proxy->rowCount({}), 1); + view.grab(); +} + +//! A label removed on the GUI thread, as the address book page removes a +//! sending address, reaches the wallet model inside the wallet's +//! notification, which CWallet::DelAddressBook raises with cs_wallet held +//! inside a database transaction; CWallet::GetNewDestination also notifies +//! with it held, while CWallet::SetAddressBookWithDB notifies after +//! releasing it. The address table models take the change at once. The +//! transaction rows are notified only once that call has returned, so the +//! proxies' filtering and sorting, which read transaction statuses, never +//! run under the wallet lock while every other thread waits for it. +void TestLabelChangeWaitsForWalletNotification(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const auto p2mr{[](uint8_t n) { return CTxDestination{WitnessV2P2MR{uint256{n}}}; }}; + const CTxDestination alice{p2mr(0x41)}; + const CTxDestination bob{p2mr(0x42)}; + state->address_book.emplace_back(alice, /*is_mine=*/false, wallet::AddressPurpose::SEND, "alice"); + state->address_book.emplace_back(bob, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "bob"); + state->wallet_tx = MakeSyntheticPayment({alice, bob, alice}, 4); + + std::set notified; + bool notified_under_lock{false}; + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + TransactionView view{env.platform_style.get()}; + view.setModel(model.get()); + TransactionFilterProxy* const proxy{view.findChild()}; + QVERIFY(proxy); + proxy->setSearchString(QStringLiteral("alice")); + QCOMPARE(proxy->rowCount({}), 2); + QObject::connect(table, &QAbstractItemModel::dataChanged, table, [&](const QModelIndex& top, const QModelIndex& bottom) { + { + std::lock_guard lock{state->mutex}; + notified_under_lock = notified_under_lock || state->wallet_lock_held; + } + for (int row = top.row(); row <= bottom.row(); ++row) notified.insert(row); + }); + + // Remove alice on the GUI thread while it holds the wallet lock, as + // CWallet::DelAddressBook does while it notifies. + const auto hold_wallet_lock{[&state](bool held) { + { + std::lock_guard lock{state->mutex}; + state->wallet_lock_held = held; + state->wallet_lock_owner = held ? std::this_thread::get_id() : std::thread::id{}; + } + state->condition.notify_all(); + }}; + hold_wallet_lock(true); + SyntheticAddressBookChange(state, alice, "", wallet::AddressPurpose::SEND, CT_DELETED)(); + const bool notified_before_return{!notified.empty()}; + hold_wallet_lock(false); + QVERIFY2(!notified_before_return, "transaction rows were notified inside the wallet's notification"); + QCOMPARE(addresses->lookupAddress(QString::fromStdString(EncodeDestination(alice))), -1); + QCOMPARE(TransactionLabel(*table, 0), QString{}); + + QCoreApplication::sendPostedEvents(); + QVERIFY(!notified_under_lock); + QCOMPARE(notified, (std::set{0, 1, 2})); + QCOMPARE(proxy->rowCount({}), 0); +} + +//! The transaction list shows nothing of an address book entry but its +//! label, so a change that leaves the label as it was queues no pass over +//! the history, such as the unlabelled receiving entry the wallet files for +//! each address it finds used while it connects blocks or rescans. A label +//! added, changed or removed still queues one. +void TestLabelPassOnlyForLabelChanges(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0xb1}}}}; + const CTxDestination bob{WitnessV2P2MR{uint256{uint8_t{0xb2}}}}; + const CTxDestination carol{WitnessV2P2MR{uint256{uint8_t{0xb3}}}}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->address_book.emplace_back(bob, /*is_mine=*/false, wallet::AddressPurpose::SEND, ""); + state->wallet_tx = MakeSyntheticPayment({alice, bob, carol}, 11); + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + const auto passes{[table](const std::function& change) { + const uint64_t before{table->labelPassCount()}; + NotifyFromOtherThread(change); + return table->labelPassCount() - before; + }}; + const auto change{[&state](const CTxDestination& dest, const std::string& label, wallet::AddressPurpose purpose, ChangeType status) { + return SyntheticAddressBookChange(state, dest, label, purpose, status); + }}; + + // A new entry with an empty label, as for a newly used address. + QCOMPARE(passes(change(carol, "", wallet::AddressPurpose::RECEIVE, CT_NEW)), uint64_t{0}); + QCOMPARE(addresses->rowCount({}), 3); + // A change of purpose only. + QCOMPARE(passes(change(alice, "alice", wallet::AddressPurpose::SEND, CT_UPDATED)), uint64_t{0}); + // The removal of an unlabelled entry. + QCOMPARE(passes(change(bob, "", wallet::AddressPurpose::SEND, CT_DELETED)), uint64_t{0}); + QCOMPARE(addresses->rowCount({}), 2); + // An entry no address decodes to, which no row can show. + QCOMPARE(passes(change(CNoDestination{}, "nowhere", wallet::AddressPurpose::SEND, CT_NEW)), uint64_t{0}); + + // A label added to an entry, changed, removed with its entry, and a new + // entry with a label. + QCOMPARE(passes(change(carol, "carol", wallet::AddressPurpose::RECEIVE, CT_UPDATED)), uint64_t{1}); + QCOMPARE(passes(change(alice, "alicia", wallet::AddressPurpose::SEND, CT_UPDATED)), uint64_t{1}); + QCOMPARE(passes(change(carol, "", wallet::AddressPurpose::RECEIVE, CT_DELETED)), uint64_t{1}); + QCOMPARE(passes(change(bob, "bob", wallet::AddressPurpose::SEND, CT_NEW)), uint64_t{1}); + QCOMPARE(TransactionLabel(*table, 0), QStringLiteral("alicia")); + QCOMPARE(TransactionLabel(*table, 1), QStringLiteral("bob")); + QCOMPARE(TransactionLabel(*table, 2), QString{}); +} + +//! A burst of address book changes, as a rescan makes when it files a +//! receiving entry for each address it finds used, or a script calling +//! setlabel in a loop, costs one pass over the history and one read of the +//! wallet's entries, and leaves the rows and the list as delivering the +//! changes one at a time does. +void TestLabelChangeBurstMakesOnePass(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + constexpr int ADDRESSES{20}; + std::vector book; + book.reserve(ADDRESSES); + for (int i = 0; i < ADDRESSES; ++i) book.emplace_back(WitnessV2P2MR{uint256{static_cast(0x60 + i)}}); + // Each address is shown by three rows, apart from each other. + std::vector outputs; + for (int round = 0; round < 3; ++round) outputs.insert(outputs.end(), book.begin(), book.end()); + + struct Outcome { + uint64_t passes{0}; + int wallet_reads{0}; + std::vector labels; + std::vector shown; + }; + const auto deliver{[&](bool burst, Outcome& outcome) { + auto state{std::make_shared()}; + for (int i = 0; i < ADDRESSES; ++i) { + state->address_book.emplace_back(book[i], /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, strprintf("old%d", i)); + } + state->wallet_tx = MakeSyntheticPayment(outputs, 5); + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + TransactionView view{env.platform_style.get()}; + view.setModel(model.get()); + TransactionFilterProxy* const proxy{view.findChild()}; + QVERIFY(proxy); + proxy->setSearchString(QStringLiteral("new")); + QCOMPARE(proxy->rowCount({}), 0); + + // Relabel every address but the last, and remove the last. + std::vector> changes; + for (int i = 0; i + 1 < ADDRESSES; ++i) { + changes.push_back(SyntheticAddressBookChange(state, book[i], strprintf("new%d", i), wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + } + changes.push_back(SyntheticAddressBookChange(state, book.back(), "", wallet::AddressPurpose::RECEIVE, CT_DELETED)); + const uint64_t passes_before{table->labelPassCount()}; + const int reads_before{SyntheticWalletReads(*state)}; + if (burst) { + NotifyFromOtherThread([&changes] { + for (const auto& change : changes) change(); + }); + } else { + for (const auto& change : changes) NotifyFromOtherThread(change); + } + outcome.passes = table->labelPassCount() - passes_before; + outcome.wallet_reads = SyntheticWalletReads(*state) - reads_before; + for (int row = 0; row < table->rowCount({}); ++row) outcome.labels.push_back(TransactionLabel(*table, row)); + for (int row = 0; row < proxy->rowCount({}); ++row) outcome.shown.push_back(proxy->index(row, 0).data(TransactionTableModel::LabelRole).toString()); + // Rows with the same date may be listed in either order. + std::sort(outcome.shown.begin(), outcome.shown.end()); + }}; + Outcome burst; + deliver(/*burst=*/true, burst); + if (QTest::currentTestFailed()) return; + Outcome separate; + deliver(/*burst=*/false, separate); + if (QTest::currentTestFailed()) return; + QCOMPARE(burst.passes, uint64_t{1}); + QCOMPARE(separate.passes, uint64_t{ADDRESSES}); + // The models compare the changed entries with the wallet in one read. + QCOMPARE(burst.wallet_reads, 1); + QCOMPARE(separate.wallet_reads, ADDRESSES); + QCOMPARE(burst.labels, separate.labels); + QCOMPARE(burst.shown, separate.shown); + QCOMPARE(burst.labels.size(), outputs.size()); + for (size_t row = 0; row < burst.labels.size(); ++row) { + const int i{static_cast(row % ADDRESSES)}; + QCOMPARE(burst.labels[row], i + 1 < ADDRESSES ? QString::fromStdString(strprintf("new%d", i)) : QString{}); + } + QCOMPARE(burst.shown.size(), size_t{3 * (ADDRESSES - 1)}); +} + +//! Rows are ordered by transaction, so the rows that show one address are +//! scattered. Relabelling an address that many rows show sends one signal, +//! for the address column only, and a label search and a sort on the +//! address column follow the change: a proxy sorts the rows of a signal +//! among the others, so it would misplace rows whose labels changed but +//! that came in a later signal. +void TestLabelChangeSendsOneSignal(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0x71}}}}; + const CTxDestination bob{WitnessV2P2MR{uint256{uint8_t{0x72}}}}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->address_book.emplace_back(bob, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "bob"); + // Alice shows every other row, so each of her rows is a range of its own. + constexpr int ROWS{200}; + std::vector outputs; + outputs.reserve(ROWS); + for (int row = 0; row < ROWS; ++row) outputs.push_back(row % 2 == 0 ? alice : bob); + state->wallet_tx = MakeSyntheticPayment(outputs, 6); + + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + TransactionView view{env.platform_style.get()}; + view.setModel(model.get()); + view.resize(1000, 600); + TransactionFilterProxy* const proxy{view.findChild()}; + QTableView* const list{view.findChild()}; + QVERIFY(proxy); + QVERIFY(list); + QCOMPARE(table->rowCount({}), ROWS); + proxy->setSearchString(QStringLiteral("zed")); + QCOMPARE(proxy->rowCount({}), 0); + + QSignalSpy changed{table, &QAbstractItemModel::dataChanged}; + QVERIFY(changed.isValid()); + NotifyFromOtherThread(SyntheticAddressBookChange(state, alice, "zed", wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + QCOMPARE(changed.size(), 1); + const std::set notified{NotifiedLabelRows(changed)}; + QVERIFY(!notified.count(-1)); + QCOMPARE(*notified.begin(), 0); + QCOMPARE(*notified.rbegin(), ROWS - 2); + QCOMPARE(proxy->rowCount({}), ROWS / 2); + + // Sorted on the address column, which shows "label (address)", bob's + // rows come first; after renaming alice to "able", hers do. + proxy->setSearchString(QString{}); + list->sortByColumn(TransactionTableModel::ToAddress, Qt::AscendingOrder); + const auto sorted_labels{[proxy] { + std::vector labels; + for (int row = 0; row < proxy->rowCount({}); ++row) labels.push_back(proxy->index(row, 0).data(TransactionTableModel::LabelRole).toString()); + return labels; + }}; + std::vector expected(ROWS / 2, QStringLiteral("bob")); + expected.resize(ROWS, QStringLiteral("zed")); + QCOMPARE(sorted_labels(), expected); + NotifyFromOtherThread(SyntheticAddressBookChange(state, alice, "able", wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + expected.assign(ROWS / 2, QStringLiteral("able")); + expected.resize(ROWS, QStringLiteral("bob")); + QCOMPARE(sorted_labels(), expected); +} + +//! A listener that adds rows while a label pass notifies them, as a nested +//! event loop delivering a transaction does, leaves the listeners after it +//! with row numbers that no longer hold. A later pass notifies the rows +//! again, by their new numbers, rather than dropping them. +void TestLabelPassKeepsRowsAfterRowChange(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0x91}}}}; + const CTxDestination bob{WitnessV2P2MR{uint256{uint8_t{0x92}}}}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->address_book.emplace_back(bob, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "bob"); + state->wallet_tx = MakeSyntheticPayment({alice, bob, alice, bob, alice}, 7); + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + QCOMPARE(table->rowCount({}), 5); + + const auto another_payment{SyntheticTransactionChange(state, MakeSyntheticPayment({alice}, 8), CT_NEW)}; + bool inserted{false}; + std::set notified_after_insert; + QObject::connect(table, &QAbstractItemModel::dataChanged, table, [&](const QModelIndex& top, const QModelIndex& bottom) { + if (!inserted) { + inserted = true; + another_payment(); + QCoreApplication::sendPostedEvents(); + return; + } + for (int row = top.row(); row <= bottom.row(); ++row) notified_after_insert.insert(row); + }); + NotifyFromOtherThread(SyntheticAddressBookChange(state, alice, "carol", wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + QCoreApplication::sendPostedEvents(); + QVERIFY(inserted); + QCOMPARE(table->rowCount({}), 6); + std::set alice_rows; + for (int row = 0; row < table->rowCount({}); ++row) { + if (table->index(row, 0).data(TransactionTableModel::AddressRole).toString() == QString::fromStdString(EncodeDestination(alice))) alice_rows.insert(row); + } + QCOMPARE(alice_rows.size(), size_t{4}); + for (int row : alice_rows) { + QVERIFY2(notified_after_insert.count(row), qPrintable(QString::number(row))); + QCOMPARE(TransactionLabel(*table, row), QStringLiteral("carol")); + } +} + +//! A payment the wallet cannot attribute to one of its addresses shows the +//! address the wallet stored as text. A spelling that decodes to an +//! address, one in upper case or with surrounding white space, shows that +//! address's label and follows it; one that does not decode shows none. +void TestStoredAddressTextFollowsLabel(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + const CTxDestination bech32{WitnessV2P2MR{uint256{uint8_t{0x81}}}}; + const CTxDestination base58{PKHash{uint160{std::vector(20, 0x82)}}}; + const CTxDestination other{WitnessV2P2MR{uint256{uint8_t{0x83}}}}; + const std::string bech32_text{EncodeDestination(bech32)}; + struct Case { + CTxDestination dest; + std::string stored; + bool shows_label; + }; + const std::array cases{{ + {bech32, ToUpper(bech32_text), true}, + {base58, " " + EncodeDestination(base58) + "\t\n", true}, + // bech32 does not allow surrounding white space. + {bech32, " " + bech32_text, false}, + }}; + for (const Case& test : cases) { + auto state{std::make_shared()}; + state->address_book.emplace_back(test.dest, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "before"); + state->address_book.emplace_back(other, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "other"); + // Output 0 pays the address. Output 1 is the wallet's but not paid to + // one of its addresses, so its row shows the text the wallet stored. + interfaces::WalletTx payment{MakeSyntheticPayment({test.dest, other}, 9)}; + payment.txout_address_is_mine[1] = false; + payment.value_map["from"] = test.stored; + state->wallet_tx = payment; + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + TransactionView view{env.platform_style.get()}; + view.setModel(model.get()); + TransactionFilterProxy* const proxy{view.findChild()}; + QVERIFY(proxy); + QCOMPARE(table->rowCount({}), 2); + QCOMPARE(table->index(1, 0).data(TransactionTableModel::TypeRole).toInt(), static_cast(TransactionRecord::RecvFromOther)); + QCOMPARE(table->index(1, 0).data(TransactionTableModel::AddressRole).toString(), QString::fromStdString(test.stored)); + QCOMPARE(TransactionLabel(*table, 1), test.shows_label ? QStringLiteral("before") : QString{}); + proxy->setSearchString(QStringLiteral("after")); + + QSignalSpy changed{table, &QAbstractItemModel::dataChanged}; + QVERIFY(changed.isValid()); + NotifyFromOtherThread(SyntheticAddressBookChange(state, test.dest, "after", wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + QCOMPARE(NotifiedLabelRows(changed), test.shows_label ? (std::set{0, 1}) : (std::set{0})); + QCOMPARE(TransactionLabel(*table, 1), test.shows_label ? QStringLiteral("after") : QString{}); + QCOMPARE(proxy->rowCount({}), test.shows_label ? 2 : 1); + } +} + +//! An address that does not decode, or a row without an address, has no +//! label, as the wallet finds no entry for it, even if the address table +//! holds an entry whose encoding is the empty string such an address +//! encodes to. +void TestUndecodableAddressHasNoLabel(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0xa1}}}}; + const CTxDestination other{WitnessV2P2MR{uint256{uint8_t{0xa2}}}}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->address_book.emplace_back(CNoDestination{}, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "ghost"); + // Output 1 is the wallet's but not paid to one of its addresses, and the + // wallet stored no text for it, so its row has no address. + interfaces::WalletTx payment{MakeSyntheticPayment({alice, other}, 10)}; + payment.txout_address_is_mine[1] = false; + state->wallet_tx = payment; + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + QCOMPARE(addresses->rowCount({}), 2); + QCOMPARE(addresses->lookupAddress(QString{}), 0); + QCOMPARE(table->rowCount({}), 2); + QCOMPARE(table->index(1, 0).data(TransactionTableModel::AddressRole).toString(), QString{}); + QCOMPARE(TransactionLabel(*table, 0), QStringLiteral("alice")); + QCOMPARE(TransactionLabel(*table, 1), QString{}); + for (const QString& address : {QString{}, QStringLiteral("not an address"), QStringLiteral(" ")}) { + QCOMPARE(addresses->cachedLabelForAddress(address), QString{}); + } +} + +//! Another thread holds the synthetic wallet's lock stand-in, as the wallet +//! holds cs_wallet while it connects a block, until release() or the end of +//! the scope. Its bound only ends a test whose GUI thread waits for the lock. +class SyntheticWalletLockHolder +{ +public: + explicit SyntheticWalletLockHolder(std::shared_ptr state) : m_state{std::move(state)} + { + m_thread = std::thread{[this] { + std::unique_lock lock{m_state->mutex}; + m_state->wallet_lock_held = true; + m_state->wallet_lock_owner = std::this_thread::get_id(); + m_state->condition.notify_all(); + m_state->condition.wait_for(lock, std::chrono::seconds{60}, [this] { return m_release; }); + m_state->wallet_lock_held = false; + m_state->wallet_lock_owner = {}; + m_state->condition.notify_all(); + }}; + std::unique_lock lock{m_state->mutex}; + m_state->condition.wait_for(lock, std::chrono::seconds{60}, [this] { return m_state->wallet_lock_held || m_release; }); + } + ~SyntheticWalletLockHolder() { release(); } + void release() + { + { + std::lock_guard lock{m_state->mutex}; + m_release = true; + } + m_state->condition.notify_all(); + if (m_thread.joinable()) m_thread.join(); + } + +private: + std::shared_ptr m_state; + //! Guarded by m_state->mutex. + bool m_release{false}; + std::thread m_thread; +}; + +//! Two writers relabel one address: the first writes "zoe", the second +//! "abe". CWallet::SetAddressBookWithDB notifies after releasing the wallet +//! lock, so the notifications can arrive as "abe" and then "zoe". The models +//! take them in that order and then compare the address with the wallet, so +//! the address table, the transaction rows, a label search and a sort on the +//! address column end at the wallet's "abe". +void TestReorderedLabelNotificationsConverge(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0xc1}}}}; + const CTxDestination bob{WitnessV2P2MR{uint256{uint8_t{0xc2}}}}; + const QString alice_text{QString::fromStdString(EncodeDestination(alice))}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "carl"); + state->address_book.emplace_back(bob, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "bob"); + state->wallet_tx = MakeSyntheticPayment({alice, bob, alice, bob, alice, bob}, 12); + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + TransactionView view{env.platform_style.get()}; + view.setModel(model.get()); + TransactionFilterProxy* const proxy{view.findChild()}; + QTableView* const list{view.findChild()}; + QVERIFY(proxy); + QVERIFY(list); + list->sortByColumn(TransactionTableModel::ToAddress, Qt::AscendingOrder); + const auto sorted_labels{[proxy] { + std::vector labels; + for (int row = 0; row < proxy->rowCount({}); ++row) labels.push_back(proxy->index(row, 0).data(TransactionTableModel::LabelRole).toString()); + return labels; + }}; + + // Both writes have been made; the wallet holds the second. + SetSyntheticAddressBookEntry(state, alice, "abe", wallet::AddressPurpose::RECEIVE); + const int reads_before{SyntheticWalletReads(*state)}; + std::thread{[&state, &alice] { + SyntheticLateAddressBookNotification(state, alice, "abe", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + SyntheticLateAddressBookNotification(state, alice, "zoe", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + }}.join(); + // The notifications, in the order they arrived. + QCoreApplication::sendPostedEvents(); + QCOMPARE(addresses->cachedLabelForAddress(alice_text), QStringLiteral("zoe")); + // Then the comparison with the wallet. + DeliverPostedEvents(); + QCOMPARE(SyntheticWalletReads(*state) - reads_before, 1); + QCOMPARE(addresses->cachedLabelForAddress(alice_text), QStringLiteral("abe")); + QCOMPARE(addresses->index(addresses->lookupAddress(alice_text), AddressTableModel::Label, {}).data(Qt::EditRole).toString(), QStringLiteral("abe")); + for (int row : {0, 2, 4}) QCOMPARE(TransactionLabel(*table, row), QStringLiteral("abe")); + const std::vector expected{QStringLiteral("abe"), QStringLiteral("abe"), QStringLiteral("abe"), + QStringLiteral("bob"), QStringLiteral("bob"), QStringLiteral("bob")}; + QCOMPARE(sorted_labels(), expected); + proxy->setSearchString(QStringLiteral("zoe")); + QCOMPARE(proxy->rowCount({}), 0); + proxy->setSearchString(QStringLiteral("abe")); + QCOMPARE(proxy->rowCount({}), 3); +} + +//! An entry added and removed, or removed and added again, by writers whose +//! notifications arrive the other way round, ends as the wallet holds it. +void TestReorderedAddAndRemoveConverge(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination carol{WitnessV2P2MR{uint256{uint8_t{0xd1}}}}; + const CTxDestination dave{WitnessV2P2MR{uint256{uint8_t{0xd2}}}}; + const QString carol_text{QString::fromStdString(EncodeDestination(carol))}; + const QString dave_text{QString::fromStdString(EncodeDestination(dave))}; + state->address_book.emplace_back(dave, /*is_mine=*/false, wallet::AddressPurpose::SEND, "eve"); + state->wallet_tx = MakeSyntheticPayment({carol, dave}, 13); + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + QCOMPARE(addresses->rowCount({}), 1); + + // Carol is added as "dora" and removed again, but the removal is + // delivered first, and then the addition. + // Dave is removed and added again as "eve2", but the addition is + // delivered first, while his old entry is still listed, and then the + // removal. + SetSyntheticAddressBookEntry(state, dave, "eve2", wallet::AddressPurpose::SEND); + std::thread{[&] { + SyntheticLateAddressBookNotification(state, carol, "", wallet::AddressPurpose::SEND, CT_DELETED)(); + SyntheticLateAddressBookNotification(state, carol, "dora", wallet::AddressPurpose::SEND, CT_NEW)(); + SyntheticLateAddressBookNotification(state, dave, "eve2", wallet::AddressPurpose::SEND, CT_NEW)(); + SyntheticLateAddressBookNotification(state, dave, "", wallet::AddressPurpose::SEND, CT_DELETED)(); + }}.join(); + QCoreApplication::sendPostedEvents(); + QVERIFY(addresses->lookupAddress(carol_text) >= 0); + QCOMPARE(addresses->lookupAddress(dave_text), -1); + DeliverPostedEvents(); + QCOMPARE(addresses->lookupAddress(carol_text), -1); + QCOMPARE(addresses->cachedLabelForAddress(dave_text), QStringLiteral("eve2")); + QCOMPARE(addresses->rowCount({}), 1); + QCOMPARE(TransactionLabel(*table, 0), QString{}); + QCOMPARE(TransactionLabel(*table, 1), QStringLiteral("eve2")); +} + +//! While another thread holds the wallet lock, the comparison with the +//! wallet does not wait for it: it keeps the addresses, tries again after +//! MODEL_UPDATE_DELAY, takes in the addresses notified meanwhile, and +//! brings the models to the wallet's state once the lock is free. +void TestAddressBookReconcileWaitsForFreeLock(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0xe1}}}}; + const CTxDestination bob{WitnessV2P2MR{uint256{uint8_t{0xe2}}}}; + const QString alice_text{QString::fromStdString(EncodeDestination(alice))}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "carl"); + state->address_book.emplace_back(bob, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "bob"); + state->wallet_tx = MakeSyntheticPayment({alice, bob}, 14); + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + const int reads_before{SyntheticWalletReads(*state)}; + int calls_before{0}; + { + std::lock_guard lock{state->mutex}; + calls_before = state->get_address_calls; + } + + SyntheticWalletLockHolder holder{state}; + SetSyntheticAddressBookEntry(state, alice, "abe", wallet::AddressPurpose::RECEIVE); + NotifyFromOtherThread([&] { + SyntheticLateAddressBookNotification(state, alice, "abe", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + SyntheticLateAddressBookNotification(state, alice, "zoe", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + }); + QCOMPARE(SyntheticWalletReads(*state) - reads_before, 1); + QCOMPARE(addresses->cachedLabelForAddress(alice_text), QStringLiteral("zoe")); + QTimer* const retry{model->findChild(QStringLiteral("addressBookRetryTimer"))}; + QVERIFY(retry); + QVERIFY(retry->isActive()); + QCOMPARE(retry->interval(), static_cast(std::chrono::milliseconds{MODEL_UPDATE_DELAY}.count())); + // A notification while the retry waits is kept for it, not read at once. + NotifyFromOtherThread(SyntheticAddressBookChange(state, bob, "bea", wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + QCOMPARE(SyntheticWalletReads(*state) - reads_before, 1); + + holder.release(); + { + std::lock_guard lock{state->mutex}; + QCOMPARE(state->get_address_calls, calls_before); + } + // The timer fires. + retry->stop(); + QVERIFY(QMetaObject::invokeMethod(retry, "timeout")); + DeliverPostedEvents(); + QCOMPARE(SyntheticWalletReads(*state) - reads_before, 2); + QVERIFY(!retry->isActive()); + QCOMPARE(addresses->cachedLabelForAddress(alice_text), QStringLiteral("abe")); + QCOMPARE(TransactionLabel(*table, 0), QStringLiteral("abe")); + QCOMPARE(TransactionLabel(*table, 1), QStringLiteral("bea")); +} + +//! When the notifications arrived in the order of the writes, the +//! comparison with the wallet finds nothing to change: it updates no +//! address table row and queues no label pass. +void TestAddressBookReconcileInOrderChangesNothing(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0xf1}}}}; + const CTxDestination bob{WitnessV2P2MR{uint256{uint8_t{0xf2}}}}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->wallet_tx = MakeSyntheticPayment({alice, bob}, 15); + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + const int reads_before{SyntheticWalletReads(*state)}; + const uint64_t passes_before{table->labelPassCount()}; + + std::thread{[&] { + SyntheticAddressBookChange(state, alice, "anna", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + SyntheticAddressBookChange(state, bob, "bo", wallet::AddressPurpose::SEND, CT_NEW)(); + SyntheticAddressBookChange(state, bob, "", wallet::AddressPurpose::SEND, CT_DELETED)(); + }}.join(); + // The notifications. + QCoreApplication::sendPostedEvents(); + QSignalSpy changed{addresses, &QAbstractItemModel::dataChanged}; + QSignalSpy inserted{addresses, &QAbstractItemModel::rowsInserted}; + QSignalSpy removed{addresses, &QAbstractItemModel::rowsRemoved}; + // The label pass they queued and the comparison with the wallet. + DeliverPostedEvents(); + QCOMPARE(SyntheticWalletReads(*state) - reads_before, 1); + QCOMPARE(changed.size(), 0); + QCOMPARE(inserted.size(), 0); + QCOMPARE(removed.size(), 0); + QCOMPARE(table->labelPassCount() - passes_before, uint64_t{1}); + QCOMPARE(TransactionLabel(*table, 0), QStringLiteral("anna")); + QCOMPARE(TransactionLabel(*table, 1), QString{}); +} + +//! The wallet model can be deleted while a comparison with the wallet is +//! queued or its retry timer is armed: neither runs afterwards. +void TestModelDeletedWithAddressBookReconcilePending(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0xa5}}}}; + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->wallet_tx = MakeSyntheticPayment({alice}, 16); + + // Queued. + { + auto model{env.makeModel(state)}; + std::thread{SyntheticAddressBookChange(state, alice, "anna", wallet::AddressPurpose::RECEIVE, CT_UPDATED)}.join(); + QCoreApplication::sendPostedEvents(); + QCOMPARE(model->getAddressTableModel()->cachedLabelForAddress(QString::fromStdString(EncodeDestination(alice))), QStringLiteral("anna")); + const int reads_before{SyntheticWalletReads(*state)}; + model.reset(); + DeliverPostedEvents(); + QCOMPARE(SyntheticWalletReads(*state), reads_before); + } + + // Waiting for its retry. + auto model{env.makeModel(state)}; + { + SyntheticWalletLockHolder holder{state}; + NotifyFromOtherThread(SyntheticAddressBookChange(state, alice, "ada", wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + } + QPointer retry{model->findChild(QStringLiteral("addressBookRetryTimer"))}; + QVERIFY(retry); + QVERIFY(retry->isActive()); + const int reads_before{SyntheticWalletReads(*state)}; + model.reset(); + QVERIFY(retry.isNull()); + DeliverPostedEvents(); + QCOMPARE(SyntheticWalletReads(*state), reads_before); +} + +//! The message signing dialog has the wallet model hand out an address +//! table model that lists only legacy addresses. Comparing a notified entry +//! with the wallet gives that model no entry a notification would not give +//! it. +void TestAddressBookReconcileKeepsLegacyOnlyModel(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination legacy{PKHash{uint160{std::vector(20, 0x5a)}}}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0x5b}}}}; + const QString alice_text{QString::fromStdString(EncodeDestination(alice))}; + state->address_book.emplace_back(legacy, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "lee"); + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "carl"); + state->wallet_tx = MakeSyntheticPayment({legacy, alice}, 17); + auto model{env.makeModel(state)}; + AddressTableModel* const complete{model->getAddressTableModel()}; + model->refresh(/*pk_hash_only=*/true); + AddressTableModel* const legacy_only{model->getAddressTableModel()}; + QVERIFY(legacy_only != complete); + QCOMPARE(legacy_only->rowCount({}), 1); + + // A relabel of alice in order, and then two whose notifications arrive + // the other way round. + NotifyFromOtherThread(SyntheticAddressBookChange(state, alice, "cara", wallet::AddressPurpose::RECEIVE, CT_UPDATED)); + SetSyntheticAddressBookEntry(state, alice, "abe", wallet::AddressPurpose::RECEIVE); + NotifyFromOtherThread([&] { + SyntheticLateAddressBookNotification(state, alice, "abe", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + SyntheticLateAddressBookNotification(state, alice, "zoe", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + }); + QCOMPARE(complete->cachedLabelForAddress(alice_text), QStringLiteral("abe")); + QCOMPARE(legacy_only->rowCount({}), 1); + QCOMPARE(legacy_only->lookupAddress(alice_text), -1); +} + +//! An entry the wallet holds under a destination its address string does +//! not decode to, such as a public key, whose encoding is the empty string, +//! cannot be looked up by that string: the wallet read would find no entry +//! and the comparison would remove the row. The row stays as its +//! notifications left it, and no read is made for it. +void TestAddressBookReconcileSkipsUndecodableEntry(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const CTxDestination key{PubKeyDestination{GenerateRandomKey().GetPubKey()}}; + const CTxDestination alice{WitnessV2P2MR{uint256{uint8_t{0x61}}}}; + QVERIFY(EncodeDestination(key).empty()); + state->address_book.emplace_back(key, /*is_mine=*/false, wallet::AddressPurpose::SEND, "key"); + state->address_book.emplace_back(alice, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "alice"); + state->wallet_tx = MakeSyntheticPayment({alice}, 18); + auto model{env.makeModel(state)}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + const auto key_row_label{[addresses] { + const int row{addresses->lookupAddress(QString{})}; + return row < 0 ? QStringLiteral("(not listed)") : addresses->index(row, AddressTableModel::Label, {}).data(Qt::EditRole).toString(); + }}; + QCOMPARE(key_row_label(), QStringLiteral("key")); + + const int reads_before{SyntheticWalletReads(*state)}; + NotifyFromOtherThread(SyntheticAddressBookChange(state, key, "key2", wallet::AddressPurpose::SEND, CT_UPDATED)); + QCOMPARE(key_row_label(), QStringLiteral("key2")); + QCOMPARE(addresses->rowCount({}), 2); + QCOMPARE(SyntheticWalletReads(*state) - reads_before, 0); +} + +//! The comparison with the wallet reads every recorded entry, then applies +//! the differences one by one. If a listener of one row runs a nested event +//! loop that delivers a newer notification for an address still to come, +//! the entry read for that address is older than the notification, so it is +//! read again instead of applied. +void TestAddressBookReconcileRereadsAfterNestedNotification(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + // The comparison goes through the addresses in the order of their + // encodings, so name them by it. + std::array dests{CTxDestination{WitnessV2P2MR{uint256{uint8_t{0x62}}}}, CTxDestination{WitnessV2P2MR{uint256{uint8_t{0x63}}}}}; + if (EncodeDestination(dests[1]) < EncodeDestination(dests[0])) std::swap(dests[0], dests[1]); + const CTxDestination& first{dests[0]}; + const CTxDestination& second{dests[1]}; + const QString first_text{QString::fromStdString(EncodeDestination(first))}; + const QString second_text{QString::fromStdString(EncodeDestination(second))}; + state->address_book.emplace_back(first, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "f0"); + state->address_book.emplace_back(second, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "s0"); + state->wallet_tx = MakeSyntheticPayment({first, second}, 19); + auto model{env.makeModel(state)}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + AddressTableModel* const addresses{model->getAddressTableModel()}; + + // Both entries were written twice and the notifications arrive the other + // way round, so the comparison has to correct both. + SetSyntheticAddressBookEntry(state, first, "f2", wallet::AddressPurpose::RECEIVE); + SetSyntheticAddressBookEntry(state, second, "s2", wallet::AddressPurpose::RECEIVE); + std::thread{[&] { + for (const auto& [dest, label] : {std::pair{first, "f2"}, std::pair{first, "f1"}, std::pair{second, "s2"}, std::pair{second, "s1"}}) { + SyntheticLateAddressBookNotification(state, dest, label, wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + } + }}.join(); + QCoreApplication::sendPostedEvents(); + QCOMPARE(addresses->cachedLabelForAddress(first_text), QStringLiteral("f1")); + QCOMPARE(addresses->cachedLabelForAddress(second_text), QStringLiteral("s1")); + + // While the comparison corrects the first, a listener runs a nested event + // loop in which the second is relabelled again, as from the GUI, and + // that notification is applied and compared with the wallet. + bool nested{false}; + QObject::connect(addresses, &QAbstractItemModel::dataChanged, addresses, [&](const QModelIndex& top) { + if (nested || addresses->index(top.row(), AddressTableModel::Address, {}).data(Qt::EditRole).toString() != first_text) return; + nested = true; + SyntheticAddressBookChange(state, second, "s3", wallet::AddressPurpose::RECEIVE, CT_UPDATED)(); + QCoreApplication::sendPostedEvents(); + }); + DeliverPostedEvents(); + QVERIFY(nested); + QCOMPARE(addresses->cachedLabelForAddress(first_text), QStringLiteral("f2")); + QCOMPARE(addresses->cachedLabelForAddress(second_text), QStringLiteral("s3")); + QCOMPARE(TransactionLabel(*table, 0), QStringLiteral("f2")); + QCOMPARE(TransactionLabel(*table, 1), QStringLiteral("s3")); +} + +//! While the message signing dialog's legacy-only model is the current one, +//! the comparison with the wallet changes both models as the notifications +//! would have, had they arrived in the order of the writes: a legacy entry +//! relabelled (CT_UPDATED for a row both models list), a non-legacy entry +//! removed and added again (CT_NEW) and a legacy entry added and removed +//! again (CT_DELETED). +void TestAddressBookReconcileWithLegacyOnlyModel(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + const CTxDestination lee{PKHash{uint160{std::vector(20, 0x71)}}}; + const CTxDestination lou{PKHash{uint160{std::vector(20, 0x72)}}}; + const CTxDestination nina{WitnessV2P2MR{uint256{uint8_t{0x73}}}}; + struct Write { + CTxDestination dest; + std::string label; + ChangeType status; + }; + struct Scenario { + const char* name; + std::vector writes; + }; + const std::array scenarios{{ + {"legacy relabelled", {{lee, "lee1", CT_UPDATED}, {lee, "lee2", CT_UPDATED}}}, + {"non-legacy removed and added again", {{nina, "", CT_DELETED}, {nina, "nina2", CT_NEW}}}, + {"legacy added and removed again", {{lou, "lou", CT_NEW}, {lou, "", CT_DELETED}}}, + }}; + // The rows of a model, as address and label. + const auto rows{[](const AddressTableModel& table) { + std::vector> rows; + for (int row = 0; row < table.rowCount({}); ++row) { + rows.emplace_back(table.index(row, AddressTableModel::Address, {}).data(Qt::EditRole).toString(), + table.index(row, AddressTableModel::Label, {}).data(Qt::EditRole).toString()); + } + return rows; + }}; + const auto encode{[](const CTxDestination& dest) { return QString::fromStdString(EncodeDestination(dest)); }}; + const auto row{[&encode](const CTxDestination& dest, const char* label) { return std::pair{encode(dest), QString::fromUtf8(label)}; }}; + const std::array>, 3> legacy_expected{{ + {row(lee, "lee2")}, + {row(lee, "lee"), row(nina, "nina2")}, + {row(lee, "lee")}, + }}; + for (size_t i = 0; i < scenarios.size(); ++i) { + const Scenario& scenario{scenarios[i]}; + std::array>, 2> complete_rows; + std::array>, 2> legacy_rows; + for (const bool reordered : {false, true}) { + auto state{std::make_shared()}; + state->address_book.emplace_back(lee, /*is_mine=*/false, wallet::AddressPurpose::SEND, "lee"); + state->address_book.emplace_back(nina, /*is_mine=*/false, wallet::AddressPurpose::SEND, "nina"); + state->wallet_tx = MakeSyntheticPayment({lee, lou, nina}, 20); + auto model{env.makeModel(state)}; + AddressTableModel* const complete{model->getAddressTableModel()}; + model->refresh(/*pk_hash_only=*/true); + AddressTableModel* const legacy_only{model->getAddressTableModel()}; + QCOMPARE(legacy_only->rowCount({}), 1); + // The wallet holds the result of the writes; the notifications + // arrive in their order or the other way round. + for (const Write& write : scenario.writes) { + SetSyntheticAddressBookEntry(state, write.dest, write.status == CT_DELETED ? std::nullopt : std::optional{write.label}, wallet::AddressPurpose::SEND); + } + std::vector arrivals{scenario.writes}; + if (reordered) std::reverse(arrivals.begin(), arrivals.end()); + NotifyFromOtherThread([&] { + for (const Write& write : arrivals) SyntheticLateAddressBookNotification(state, write.dest, write.label, wallet::AddressPurpose::SEND, write.status)(); + }); + complete_rows[reordered] = rows(*complete); + legacy_rows[reordered] = rows(*legacy_only); + } + QVERIFY2(complete_rows[0] == complete_rows[1], scenario.name); + QVERIFY2(legacy_rows[0] == legacy_rows[1], scenario.name); + // The legacy-only model lists the relabelled legacy entry, the + // non-legacy entry a CT_NEW gives it as a notification would, and + // not the removed one. + std::vector> expected{legacy_expected[i]}; + std::sort(expected.begin(), expected.end()); + std::vector> shown{legacy_rows[1]}; + std::sort(shown.begin(), shown.end()); + QVERIFY2(shown == expected, scenario.name); + } +} + +//! Compare the label of each address in the address table model the +//! transaction list reads, and of each transaction row, with the one the +//! wallet gives. +void VerifyLabelsMatchWallet(WalletModel& wallet_model, const AddressTableModel& labels, const QStringList& addresses) +{ + const auto wallet_label{[&wallet_model](const QString& address) { + std::string name; + if (!wallet_model.wallet().getAddress(DecodeDestination(address.toStdString()), &name, /*purpose=*/nullptr)) return QString{}; + return QString::fromStdString(name); + }}; + for (const QString& address : addresses) { + const QString cached{labels.cachedLabelForAddress(address)}; + const QString expected{wallet_label(address)}; + QVERIFY2(cached == expected, qPrintable(QStringLiteral("\"%1\": shown \"%2\", wallet \"%3\"").arg(address, cached, expected))); + } + TransactionTableModel* const transactions{wallet_model.getTransactionTableModel()}; + for (int row = 0; row < transactions->rowCount({}); ++row) { + const QModelIndex index{transactions->index(row, TransactionTableModel::ToAddress)}; + const QString address{index.data(TransactionTableModel::AddressRole).toString()}; + const QString shown{index.data(TransactionTableModel::LabelRole).toString()}; + const QString expected{wallet_label(address)}; + QVERIFY2(shown == expected, qPrintable(QStringLiteral("row %1 \"%2\": shown \"%3\", wallet \"%4\"").arg(row).arg(address, shown, expected))); + } +} + +//! Labels shown from the address table model equal the wallet's for every +//! address book state: no entry, an empty label, receiving and sending +//! entries, a change address the address book holds data for, an entry +//! removed, relabelled or added, with the change delivered at once or queued, +//! and for spellings the wallet decodes or rejects. They stay equal after the +//! message signing dialog has had the wallet model hand out a model that +//! lists only legacy addresses, and reading them never waits for the wallet +//! lock. +void TestTransactionLabelsMatchWallet(interfaces::Node& node) +{ + TestChain100Setup test{ChainType::REGTEST, {.extra_args = {"-p2mronly=0", "-keypool=1"}}}; + auto wallet_loader = interfaces::MakeWalletLoader(*test.m_node.chain, *Assert(test.m_node.args)); + test.m_node.wallet_loader = wallet_loader.get(); + node.setContext(&test.m_node); + const std::shared_ptr wallet{SetupDescriptorsWallet(node, test, /*watch_only=*/false, /*coinbase_offset=*/0, + QT_WALLET_BECH32_DESCRIPTOR_OUTPUT_TYPES, /*fund_wallet=*/false)}; + { + const CBlockIndex* const tip{WITH_LOCK(node.context()->chainman->GetMutex(), return node.context()->chainman->ActiveChain().Tip())}; + LOCK(wallet->cs_wallet); + wallet->SetLastBlockProcessed(tip->nHeight, tip->GetBlockHash()); + } + + std::vector receiving; + for (const char* label : {"alice", ""}) { + auto dest{wallet->GetNewDestination(OutputType::BECH32, label)}; + QVERIFY(dest); + receiving.push_back(*dest); + } + std::vector change; + for (int i = 0; i < 2; ++i) { + auto dest{wallet->GetNewChangeDestination(OutputType::BECH32)}; + QVERIFY(dest); + change.push_back(*dest); + } + const CTxDestination& receive_labelled{receiving[0]}; + const CTxDestination& receive_blank{receiving[1]}; + // SetupDescriptorsWallet files the coinbase key's legacy address with an + // empty label. + const CTxDestination legacy{PKHash{test.coinbaseKey.GetPubKey()}}; + // A change address the address book holds only spending data for, which + // getAddresses() and getAddress() leave out, and one it holds nothing for. + const CTxDestination& change_with_data{change[0]}; + const CTxDestination& change_unfiled{change[1]}; + const CTxDestination send_legacy{PKHash{GenerateRandomKey().GetPubKey()}}; + const CTxDestination send_segwit{WitnessV0KeyHash{GenerateRandomKey().GetPubKey()}}; + const CTxDestination send_p2mr{WitnessV2P2MR{*uint256::FromHex("0000000000000000000000000000000000000000000000000000000000000041")}}; + const CTxDestination send_blank{WitnessV0KeyHash{GenerateRandomKey().GetPubKey()}}; + const CTxDestination removed{WitnessV0KeyHash{GenerateRandomKey().GetPubKey()}}; + const CTxDestination never{WitnessV0KeyHash{GenerateRandomKey().GetPubKey()}}; + QVERIFY(wallet->SetAddressBook(legacy, "miner", /*purpose=*/{})); + QVERIFY(wallet->SetAddressBook(send_legacy, "bob", wallet::AddressPurpose::SEND)); + QVERIFY(wallet->SetAddressBook(send_segwit, "carol", wallet::AddressPurpose::SEND)); + QVERIFY(wallet->SetAddressBook(send_p2mr, "erin", wallet::AddressPurpose::SEND)); + QVERIFY(wallet->SetAddressBook(send_blank, "", wallet::AddressPurpose::SEND)); + QVERIFY(wallet->SetAddressBook(removed, "dave", wallet::AddressPurpose::SEND)); + QVERIFY(wallet->DelAddressBook(removed)); + + // A payment to every receiving and change address, and one spending it to + // every sending address, so the transaction list has a row for each. + CMutableTransaction payment; + payment.vin.emplace_back(COutPoint{Txid::FromUint256(uint256::ONE), 0}); + for (const CTxDestination& dest : {receive_labelled, receive_blank, legacy, change_with_data, change_unfiled}) { + payment.vout.emplace_back(COIN, GetScriptForDestination(dest)); + } + const CTransactionRef payment_tx{MakeTransactionRef(payment)}; + CMutableTransaction spend; + spend.vin.emplace_back(COutPoint{payment_tx->GetHash(), 0}); + for (const CTxDestination& dest : {send_legacy, send_segwit, send_p2mr, send_blank, removed, never}) { + spend.vout.emplace_back(COIN / 10, GetScriptForDestination(dest)); + } + { + LOCK(wallet->cs_wallet); + wallet::WalletBatch batch{wallet->GetDatabase()}; + QVERIFY(wallet->SetAddressPreviouslySpent(batch, change_with_data, /*used=*/true)); + QVERIFY(wallet->AddToWallet(payment_tx, wallet::TxStateInactive{})); + QVERIFY(wallet->AddToWallet(MakeTransactionRef(spend), wallet::TxStateInactive{})); + } + + std::unique_ptr platform_style{PlatformStyle::instantiate("other")}; + MiniGUI mini_gui{node, platform_style.get()}; + mini_gui.initModelForWallet(node, wallet, platform_style.get()); + WalletModel& wallet_model{*mini_gui.walletModel}; + TransactionTableModel* const transactions{wallet_model.getTransactionTableModel()}; + // The model the address book pages show and the transaction list reads. + AddressTableModel* const labels{wallet_model.getAddressTableModel()}; + QCOMPARE(transactions->rowCount({}), 11); + + const auto encode{[](const CTxDestination& dest) { return QString::fromStdString(EncodeDestination(dest)); }}; + QStringList addresses; + for (const CTxDestination& dest : {receive_labelled, receive_blank, legacy, change_with_data, change_unfiled, + send_legacy, send_segwit, send_p2mr, send_blank, removed, never}) { + addresses << encode(dest); + } + // Spellings the wallet decodes to a filed address, and strings it rejects. + addresses << encode(receive_labelled).toUpper() << encode(send_segwit).toUpper() + << QStringLiteral(" %1\t").arg(encode(legacy)) << encode(legacy).toUpper() + << encode(send_legacy).chopped(1) + QStringLiteral("x") << QStringLiteral("not an address") << QString{}; + const auto shown_label{[labels, &encode](const CTxDestination& dest) { + return labels->cachedLabelForAddress(encode(dest)); + }}; + + VerifyLabelsMatchWallet(wallet_model, *labels, addresses); + if (QTest::currentTestFailed()) return; + QCOMPARE(labels->cachedLabelForAddress(encode(receive_labelled).toUpper()), QStringLiteral("alice")); + QCOMPARE(labels->cachedLabelForAddress(QStringLiteral(" %1\t").arg(encode(legacy))), QStringLiteral("miner")); + QCOMPARE(shown_label(send_p2mr), QStringLiteral("erin")); + + // Changes the GUI makes reach the model at once. + QVERIFY(wallet->SetAddressBook(receive_labelled, "alice2", /*purpose=*/{})); + QVERIFY(wallet->DelAddressBook(send_legacy)); + QVERIFY(wallet->SetAddressBook(change_with_data, "frank", wallet::AddressPurpose::RECEIVE)); + QVERIFY(wallet->SetAddressBook(never, "gina", wallet::AddressPurpose::SEND)); + QVERIFY(wallet->SetAddressBook(send_blank, "", /*purpose=*/{})); + VerifyLabelsMatchWallet(wallet_model, *labels, addresses); + if (QTest::currentTestFailed()) return; + QCOMPARE(shown_label(change_with_data), QStringLiteral("frank")); + + // Changes made on another thread, as by an RPC, reach it queued. + std::thread rpc{[&] { + wallet->SetAddressBook(receive_blank, "hank", /*purpose=*/{}); + wallet->DelAddressBook(send_segwit); + wallet->SetAddressBook(removed, "dave2", wallet::AddressPurpose::SEND); + }}; + rpc.join(); + QCoreApplication::sendPostedEvents(); + QCOMPARE(shown_label(removed), QStringLiteral("dave2")); + VerifyLabelsMatchWallet(wallet_model, *labels, addresses); + if (QTest::currentTestFailed()) return; + + // The message signing dialog has the wallet model hand out an address + // table model that lists only legacy addresses. The transaction list + // keeps showing the labels of the others. + wallet_model.refresh(/*pk_hash_only=*/true); + AddressTableModel* const legacy_only{wallet_model.getAddressTableModel()}; + QVERIFY(legacy_only != labels); + QCOMPARE(legacy_only->rowCount({}), 1); + QCOMPARE(legacy_only->index(0, AddressTableModel::Address, {}).data(Qt::EditRole).toString(), encode(legacy)); + VerifyLabelsMatchWallet(wallet_model, *labels, addresses); + if (QTest::currentTestFailed()) return; + QCOMPARE(shown_label(receive_labelled), QStringLiteral("alice2")); + QCOMPARE(shown_label(send_p2mr), QStringLiteral("erin")); + + // Both models keep following the address book. + QVERIFY(wallet->SetAddressBook(receive_labelled, "alice3", /*purpose=*/{})); + QVERIFY(wallet->DelAddressBook(send_p2mr)); + QVERIFY(wallet->SetAddressBook(legacy, "miner2", /*purpose=*/{})); + QVERIFY(wallet->SetAddressBook(send_legacy, "bob2", wallet::AddressPurpose::SEND)); + const auto listed_label{[&encode](const AddressTableModel& table, const CTxDestination& dest) { + const int row{table.lookupAddress(encode(dest))}; + return row < 0 ? QStringLiteral("(not listed)") : table.index(row, AddressTableModel::Label, {}).data(Qt::EditRole).toString(); + }}; + QCOMPARE(legacy_only->rowCount({}), 2); + QCOMPARE(listed_label(*legacy_only, legacy), QStringLiteral("miner2")); + QCOMPARE(listed_label(*legacy_only, send_legacy), QStringLiteral("bob2")); + QCOMPARE(listed_label(*labels, receive_labelled), QStringLiteral("alice3")); + QCOMPARE(listed_label(*labels, send_p2mr), QStringLiteral("(not listed)")); + QCOMPARE(listed_label(*labels, send_legacy), QStringLiteral("bob2")); + VerifyLabelsMatchWallet(wallet_model, *labels, addresses); + if (QTest::currentTestFailed()) return; + QCOMPARE(shown_label(receive_labelled), QStringLiteral("alice3")); + QCOMPARE(shown_label(send_p2mr), QString{}); + + // Each time the message signing dialog opens its address book, the wallet + // model hands out another model, and nothing shows the one before. Only + // the models in use follow the address book, so an address book change + // costs the same however often the dialog has opened it. + wallet_model.refresh(/*pk_hash_only=*/true); + AddressTableModel* const legacy_only_again{wallet_model.getAddressTableModel()}; + QVERIFY(legacy_only_again != legacy_only); + QVERIFY(wallet->SetAddressBook(legacy, "miner3", /*purpose=*/{})); + QCOMPARE(listed_label(*legacy_only_again, legacy), QStringLiteral("miner3")); + QCOMPARE(listed_label(*labels, legacy), QStringLiteral("miner3")); + QCOMPARE(listed_label(*legacy_only, legacy), QStringLiteral("miner2")); + + // Another thread holds the wallet lock, as the wallet does for each block + // it connects, until the GUI thread is done. The bound only ends a GUI + // thread that waits for the lock, so no pause of the process can fail + // the test. Read, paint, filter and sort the list meanwhile. + TransactionView& view{mini_gui.transactionView}; + view.resize(1000, 600); + TransactionFilterProxy* const proxy{view.findChild()}; + QTableView* const list{view.findChild()}; + QVERIFY(proxy); + QVERIFY(list); + std::mutex hold_mutex; + std::condition_variable hold_cv; + bool held{false}; + bool gui_done{false}; + bool holder_timed_out{false}; + std::thread holder{[&] { + LOCK(wallet->cs_wallet); + std::unique_lock lock{hold_mutex}; + held = true; + hold_cv.notify_all(); + holder_timed_out = !hold_cv.wait_for(lock, std::chrono::seconds{60}, [&gui_done] { return gui_done; }); + }}; + const auto finish{[&] { + { + std::lock_guard lock{hold_mutex}; + gui_done = true; + } + hold_cv.notify_all(); + }}; + const JoinOnExit join_holder{holder}; + const RunOnExit finish_on_exit{finish}; + { + std::unique_lock lock{hold_mutex}; + QVERIFY(hold_cv.wait_for(lock, std::chrono::seconds{60}, [&held] { return held; })); + } + for (int row = 0; row < transactions->rowCount({}); ++row) { + const QModelIndex index{transactions->index(row, TransactionTableModel::ToAddress)}; + index.data(TransactionTableModel::LabelRole); + index.data(Qt::DisplayRole); + index.data(Qt::ForegroundRole); + index.data(Qt::ToolTipRole); + } + view.grab(); + proxy->setSearchString(QStringLiteral("alice3")); + const int found{proxy->rowCount({})}; + proxy->setSearchString(QString{}); + list->sortByColumn(TransactionTableModel::ToAddress, Qt::AscendingOrder); + view.grab(); + finish(); + holder.join(); + QVERIFY2(!holder_timed_out, "GUI thread waited for the wallet lock while showing transaction labels"); + QCOMPARE(found, 1); + VerifyLabelsMatchWallet(wallet_model, *labels, addresses); +} + +//! The wallet model registers for address book changes before its address +//! table model reads the address book. Changes another thread makes while +//! the models load, which takes seconds for a long history, reach the +//! transaction list once the constructor has returned, wherever they fall +//! around that read, and leave each address listed once. +void TestAddressBookChangesWhileModelLoads(interfaces::Node& node) +{ + SyntheticModelEnvironment env{node}; + auto state{std::make_shared()}; + const auto p2mr{[](uint8_t n) { return CTxDestination{WitnessV2P2MR{uint256{n}}}; }}; + // Filed before the model registers: in the read only. + const CTxDestination before{p2mr(0x51)}; + // Filed after it registers and before the read: in the read and notified. + const CTxDestination during{p2mr(0x52)}; + // Filed after the read, before the constructor returns. + const CTxDestination after{p2mr(0x53)}; + // Filed and relabelled, or filed and removed, before the read. + const CTxDestination relabelled{p2mr(0x54)}; + const CTxDestination removed{p2mr(0x55)}; + // Filed before the read, and relabelled or removed after it. + const CTxDestination relabelled_later{p2mr(0x56)}; + const CTxDestination removed_later{p2mr(0x57)}; + state->address_book.emplace_back(before, /*is_mine=*/true, wallet::AddressPurpose::RECEIVE, "before"); + state->wallet_tx = MakeSyntheticPayment({before, during, after, relabelled, removed, relabelled_later, removed_later}, 5); + + const auto change{[&state](const CTxDestination& dest, const std::string& label, ChangeType status) { + return SyntheticAddressBookChange(state, dest, label, wallet::AddressPurpose::RECEIVE, status); + }}; + // Make the changes on another thread, as an RPC does while the GUI thread + // is inside the constructor. The notifications are only posted: the + // constructor does not run the event loop. + const auto change_elsewhere{[](std::vector> changes) { + std::thread thread{[changes] { + for (const auto& change : changes) change(); + }}; + thread.join(); + }}; + { + std::lock_guard lock{state->mutex}; + state->get_addresses_entered = [&] { + change_elsewhere({ + change(during, "during", CT_NEW), + change(relabelled, "first", CT_NEW), + change(relabelled, "second", CT_UPDATED), + change(removed, "gone", CT_NEW), + change(removed, "", CT_DELETED), + change(relabelled_later, "old", CT_NEW), + change(removed_later, "doomed", CT_NEW), + }); + }; + state->get_addresses_returning = [&] { + change_elsewhere({ + change(after, "after", CT_NEW), + change(relabelled_later, "new", CT_UPDATED), + change(removed_later, "", CT_DELETED), + }); + }; + } + auto model{env.makeModel(state)}; + { + std::lock_guard lock{state->mutex}; + state->get_addresses_entered = {}; + state->get_addresses_returning = {}; + } + AddressTableModel* const addresses{model->getAddressTableModel()}; + TransactionTableModel* const table{model->getTransactionTableModel()}; + QCOMPARE(table->rowCount({}), 7); + + // The constructor has returned with what the read found, and nothing + // raised meanwhile has been applied yet. + QCOMPARE(addresses->rowCount({}), 5); + QCOMPARE(TransactionLabel(*table, 2), QString{}); + QCOMPARE(TransactionLabel(*table, 5), QStringLiteral("old")); + + QCoreApplication::sendPostedEvents(); + std::vector book; + { + std::lock_guard lock{state->mutex}; + book = state->address_book; + } + QCOMPARE(book.size(), size_t{5}); + QCOMPARE(addresses->rowCount({}), 5); + for (const auto& entry : book) { + const QString address{QString::fromStdString(EncodeDestination(entry.dest))}; + QVERIFY2(addresses->lookupAddress(address) >= 0, qPrintable(address)); + QCOMPARE(addresses->cachedLabelForAddress(address), QString::fromStdString(entry.name)); + } + const std::array labels{QStringLiteral("before"), QStringLiteral("during"), QStringLiteral("after"), + QStringLiteral("second"), QString{}, QStringLiteral("new"), QString{}}; + for (int row = 0; row < 7; ++row) QCOMPARE(TransactionLabel(*table, row), labels[row]); + + // A change raised on the GUI thread reaches the wallet model at once, not + // queued. The constructor only reads the wallet, so it raises none; if it + // did, during the read, the read would include the change, and the + // models still under construction must not apply it a second time. + auto gui_state{std::make_shared()}; + const CTxDestination from_gui{p2mr(0x58)}; + gui_state->wallet_tx = MakeSyntheticPayment({from_gui}, 6); + { + std::lock_guard lock{gui_state->mutex}; + gui_state->get_addresses_entered = SyntheticAddressBookChange(gui_state, from_gui, "gui", wallet::AddressPurpose::RECEIVE, CT_NEW); + } + auto gui_model{env.makeModel(gui_state)}; + { + std::lock_guard lock{gui_state->mutex}; + gui_state->get_addresses_entered = {}; + } + QCoreApplication::sendPostedEvents(); + QCOMPARE(gui_model->getAddressTableModel()->rowCount({}), 1); + QCOMPARE(gui_model->getAddressTableModel()->cachedLabelForAddress(QString::fromStdString(EncodeDestination(from_gui))), QStringLiteral("gui")); + QCOMPARE(TransactionLabel(*gui_model->getTransactionTableModel(), 0), QStringLiteral("gui")); +} + //! BitcoinGUI::updateWalletStatus reads the current wallet whenever any wallet //! reports a status change, so another wallet's notification while the //! current one is being encrypted must not send the GUI thread into the @@ -5527,6 +7154,27 @@ void WalletTests::walletTests() TestEncryptWalletKeepsGuiResponsive(m_node); TestEncryptWalletDefersTransactionUpdates(m_node); TestEncryptWalletRepaintUsesCachedLabels(m_node); + TestTransactionLabelsSkipWalletLock(m_node); + TestTransactionLabelDeliveryOrder(m_node); + TestLabelChangeUpdatesItsRows(m_node); + TestLabelChangeWaitsForWalletNotification(m_node); + TestLabelPassOnlyForLabelChanges(m_node); + TestLabelChangeBurstMakesOnePass(m_node); + TestLabelChangeSendsOneSignal(m_node); + TestLabelPassKeepsRowsAfterRowChange(m_node); + TestStoredAddressTextFollowsLabel(m_node); + TestUndecodableAddressHasNoLabel(m_node); + TestReorderedLabelNotificationsConverge(m_node); + TestReorderedAddAndRemoveConverge(m_node); + TestAddressBookReconcileWaitsForFreeLock(m_node); + TestAddressBookReconcileInOrderChangesNothing(m_node); + TestModelDeletedWithAddressBookReconcilePending(m_node); + TestAddressBookReconcileKeepsLegacyOnlyModel(m_node); + TestAddressBookReconcileSkipsUndecodableEntry(m_node); + TestAddressBookReconcileRereadsAfterNestedNotification(m_node); + TestAddressBookReconcileWithLegacyOnlyModel(m_node); + TestTransactionLabelsMatchWallet(m_node); + TestAddressBookChangesWhileModelLoads(m_node); TestEncryptWalletOtherWalletStatusChange(m_node); TestEncryptWalletFailureReportsError(m_node); TestEncryptWalletAlreadyEncryptedByAnotherRequest(m_node); diff --git a/src/qt/transactiontablemodel.cpp b/src/qt/transactiontablemodel.cpp index 3e512beb92..1b4cfb2f0c 100644 --- a/src/qt/transactiontablemodel.cpp +++ b/src/qt/transactiontablemodel.cpp @@ -17,11 +17,17 @@ #include #include +#include #include #include +#include +#include #include #include +#include +#include +#include #include #include @@ -247,13 +253,16 @@ class TransactionTablePriv } }; -TransactionTableModel::TransactionTableModel(const PlatformStyle *_platformStyle, WalletModel *parent): +TransactionTableModel::TransactionTableModel(const PlatformStyle *_platformStyle, AddressTableModel *address_table_model, WalletModel *parent): QAbstractTableModel(parent), walletModel(parent), + m_address_table_model(Assert(address_table_model)), priv(new TransactionTablePriv(this)), platformStyle(_platformStyle) { subscribeToCoreSignals(); + connect(this, &QAbstractItemModel::rowsInserted, this, [this] { ++m_row_changes; }); + connect(this, &QAbstractItemModel::rowsRemoved, this, [this] { ++m_row_changes; }); columns << QString() << tr("Date") << tr("Type") << tr("Label") << QbitUnits::getAmountColumnTitle(walletModel->getOptionsModel()->getDisplayUnit()); priv->refreshWallet(walletModel->wallet()); @@ -385,7 +394,7 @@ QString TransactionTableModel::formatTxDate(const TransactionRecord *wtx) const */ QString TransactionTableModel::lookupAddress(const std::string &address, bool tooltip) const { - QString label = walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(address)); + QString label = m_address_table_model->cachedLabelForAddress(QString::fromStdString(address)); QString description; if(!label.isEmpty()) { @@ -398,6 +407,86 @@ QString TransactionTableModel::lookupAddress(const std::string &address, bool to return description; } +/* Whether a row whose address is row_address shows the label of one of the + encoded addresses in changed. Labels are looked up by the row address + decoded and encoded again, which gives the row address back when it was + encoded from an output. An address the wallet stored as text + (RecvFromOther, SendToOther and their coinbase form) can decode to + another spelling only when it has surrounding white space or no lower + case letter, as a bech32 address in upper case, so only such a spelling + needs decoding to compare. */ +static bool ShowsChangedLabel(const std::string& row_address, const std::unordered_set& changed) +{ + if (changed.count(row_address)) return true; + if (util::TrimStringView(row_address).size() == row_address.size() && + std::any_of(row_address.begin(), row_address.end(), [](char c) { return c >= 'a' && c <= 'z'; })) { + return false; + } + const CTxDestination destination{DecodeDestination(row_address)}; + return IsValidDestination(destination) && changed.count(EncodeDestination(destination)); +} + +void TransactionTableModel::updateAddressLabel(const QString &address) +{ + // Rows read their labels from the address table model, not from the + // wallet, so nothing reads a changed label again unless told to. Finding + // the rows that show the address takes a pass over the whole history, + // and notifying them makes every proxy filter and sort them again, which + // reads their status. Do neither inside the wallet's notification: for a + // change made on this thread, CWallet::DelAddressBook (inside a database + // transaction) and CWallet::GetNewDestination notify with cs_wallet held, + // so every other thread that needs the wallet would wait. Make one pass + // for all the addresses changed until the event loop runs it. + m_changed_labels.insert(address.toStdString()); + queueLabelPass(); +} + +void TransactionTableModel::queueLabelPass() +{ + if (m_label_pass_queued) return; + m_label_pass_queued = true; + QMetaObject::invokeMethod(this, [this] { updateChangedLabels(); }, Qt::QueuedConnection); +} + +void TransactionTableModel::updateChangedLabels() +{ + m_label_pass_queued = false; + std::unordered_set changed; + changed.swap(m_changed_labels); + if (changed.empty()) return; + ++m_label_passes; + + int first{-1}; + int last{-1}; + for (int row = 0; row < priv->size(); ++row) { + if (!ShowsChangedLabel(priv->cachedWallet.at(row).address, changed)) continue; + if (first < 0) first = row; + last = row; + } + if (first < 0) return; + // Send one signal, from the first to the last row that shows a changed + // label. A proxy sorts the rows of a signal again among its other rows, + // which it takes to be in order. The rows of one address are scattered, + // as rows are ordered by transaction, and all their labels have already + // changed, so a proxy sorted on the address column only places them + // right if they all come in one signal. A proxy filters every row a + // signal covers, whatever its columns, which costs no more than a change + // of its search text, but sorts them again only if they include its sort + // column. Only the address column shows the label, in its text, colour + // and sort key, and the label role is the same for every column, so a + // list sorted by date is left as it is. + QPointer model{this}; + const uint64_t row_changes{m_row_changes}; + Q_EMIT dataChanged(index(first, ToAddress), index(last, ToAddress)); + // A listener may run a nested event loop that deletes this model, or + // that adds or removes rows, so that the listeners after it got row + // numbers that no longer hold. Then notify the rows again. + if (model && m_row_changes != row_changes) { + m_changed_labels.insert(changed.begin(), changed.end()); + queueLabelPass(); + } +} + QString TransactionTableModel::formatTxType(const TransactionRecord *wtx) const { switch(wtx->type) @@ -461,7 +550,7 @@ QVariant TransactionTableModel::addressColor(const TransactionRecord *wtx) const case TransactionRecord::SendToAddress: case TransactionRecord::Generated: { - QString label = walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(wtx->address)); + QString label = m_address_table_model->cachedLabelForAddress(QString::fromStdString(wtx->address)); if(label.isEmpty()) return COLOR_BAREADDRESS; } break; @@ -613,7 +702,7 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const case AddressRole: return QString::fromStdString(rec->address); case LabelRole: - return walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(rec->address)); + return m_address_table_model->cachedLabelForAddress(QString::fromStdString(rec->address)); case AmountRole: return qint64(rec->credit + rec->debit); case TxHashRole: @@ -624,7 +713,7 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const { QString details; QDateTime date = QDateTime::fromSecsSinceEpoch(rec->time); - QString txLabel = walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(rec->address)); + QString txLabel = m_address_table_model->cachedLabelForAddress(QString::fromStdString(rec->address)); details.append(date.toString("M/d/yy HH:mm")); details.append(" "); diff --git a/src/qt/transactiontablemodel.h b/src/qt/transactiontablemodel.h index 6fd0fbfe21..771fb952d3 100644 --- a/src/qt/transactiontablemodel.h +++ b/src/qt/transactiontablemodel.h @@ -10,14 +10,18 @@ #include #include +#include #include #include +#include +#include #include namespace interfaces { class Handler; } +class AddressTableModel; class PlatformStyle; class TransactionRecord; class TransactionTablePriv; @@ -30,7 +34,9 @@ class TransactionTableModel : public QAbstractTableModel Q_OBJECT public: - explicit TransactionTableModel(const PlatformStyle *platformStyle, WalletModel *parent = nullptr); + /** The rows show labels from address_table_model, which must list every + address: the wallet model passes the one it keeps complete. */ + explicit TransactionTableModel(const PlatformStyle *platformStyle, AddressTableModel *address_table_model, WalletModel *parent = nullptr); ~TransactionTableModel(); enum ColumnIndex { @@ -83,9 +89,28 @@ class TransactionTableModel : public QAbstractTableModel being encrypted. Called by the wallet model once the encryption has released the wallet, whoever started it. */ void deliverDeferredUpdates(); + /** The address book entry for an encoded address was added, changed or + removed. Rows show labels from an address table model, so views are + notified of the rows that show this address, in one signal for the + address column from the first such row to the last. That happens in + one pass for every address changed meanwhile, once control has + returned to the event loop: this is called from inside the wallet's + notification, which for a change made on this thread can still hold + the wallet lock (CWallet::DelAddressBook and + CWallet::GetNewDestination notify with cs_wallet held, the deletion + inside a database transaction). Called by the wallet model once its + address table models have the update, if it changed the label. */ + void updateAddressLabel(const QString &address); + /** Number of label passes over the rows so far, for tests. */ + uint64_t labelPassCount() const { return m_label_passes; } private: WalletModel *walletModel; + /** The address table model the rows read their labels from, which lists + every address. The wallet model keeps it current, also after + WalletModel::refresh() has handed out one that lists only legacy + addresses. */ + AddressTableModel* const m_address_table_model; std::unique_ptr m_handler_transaction_changed; std::unique_ptr m_handler_show_progress; QStringList columns; @@ -96,6 +121,14 @@ class TransactionTableModel : public QAbstractTableModel the wallet, which blocks while the encryption holds its locks, so they wait here for deliverDeferredUpdates(). */ std::vector> m_deferred_updates; + /** Encoded addresses whose address book entry changed since the last + label pass, and whether the next pass is queued. */ + std::unordered_set m_changed_labels; + bool m_label_pass_queued{false}; + uint64_t m_label_passes{0}; + /** Counts insertions and removals of rows, so that a label pass whose + listeners change the rows can tell that its row numbers are stale. */ + uint64_t m_row_changes{0}; const PlatformStyle *platformStyle; void subscribeToCoreSignals(); @@ -111,6 +144,8 @@ class TransactionTableModel : public QAbstractTableModel QString formatTooltip(const TransactionRecord *rec) const; QVariant txStatusDecoration(const TransactionRecord *wtx) const; QVariant txAddressDecoration(const TransactionRecord *wtx) const; + void queueLabelPass(); + void updateChangedLabels(); public Q_SLOTS: /* New transaction, or transaction changed status */ diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 84427bcd61..e851ce3913 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -35,6 +36,7 @@ #include #include #include +#include #include #include @@ -73,6 +75,10 @@ struct WalletModel::BumpFeeProgress { unsigned int total{0}; }; +static void NotifyAddressBookChanged(WalletModel *walletmodel, + const CTxDestination &address, const std::string &label, bool isMine, + wallet::AddressPurpose purpose, ChangeType status); + WalletModel::WalletModel(std::unique_ptr wallet, ClientModel& client_model, const PlatformStyle *platformStyle, QObject *parent) : QObject(parent), m_wallet(std::move(wallet)), @@ -89,8 +95,18 @@ WalletModel::WalletModel(std::unique_ptr wallet, ClientModel m_last_encryption_status = readEncryptionStatus(); m_last_pqc_key_validation_info = m_wallet->getPQCKeyValidationInfo(); + // Register for address book changes before the address table model + // reads the address book, which the transaction list shows labels from. + // A change another thread makes while the models load is then queued, as + // this model lives on this thread, and applied once this constructor has + // returned, instead of being lost; one the read already includes is + // skipped, or applied again with the state it already has. A change made + // on this thread would be applied at once, but the constructor only + // reads the wallet. The other handlers follow in subscribeToCoreSignals(). + m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind(NotifyAddressBookChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); addressTableModel = new AddressTableModel(this); - transactionTableModel = new TransactionTableModel(platformStyle, this); + m_complete_address_table_model = addressTableModel; + transactionTableModel = new TransactionTableModel(platformStyle, m_complete_address_table_model, this); recentRequestsTableModel = new RecentRequestsTableModel(this); subscribeToCoreSignals(); @@ -201,8 +217,123 @@ void WalletModel::updateTransaction() void WalletModel::updateAddressBook(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status) { - if(addressTableModel) - addressTableModel->updateEntry(address, label, isMine, purpose, status); + // Before the constructor has created the models, which read the address + // book, there is nothing to update. + if (!m_complete_address_table_model) return; + ++m_address_book_notifications; + QPointer model{this}; + applyAddressBookChange(address, label, isMine, purpose, status); + if (!model) return; + // Notifications can arrive in another order than the writes they report: + // CWallet::SetAddressBookWithDB notifies after releasing cs_wallet, so of + // two writers to one entry the first to write can be the last to notify, + // and the models would keep its label. Compare the entry with the wallet + // once the event loop runs. Each notification is followed by such a + // comparison, so the models end at the wallet's state. + m_unreconciled_addresses.insert(address); + queueAddressBookReconcile(); +} + +void WalletModel::applyAddressBookChange(const QString& address, const QString& label, + bool isMine, wallet::AddressPurpose purpose, int status) +{ + // The address book pages and the transaction list keep the complete + // address table model, and the transaction list shows its labels. + // refresh() may since have handed out another one, which the dialog that + // asked for it shows; nothing shows those it handed out before. Update + // the two in use, then have the transaction list notify the rows that + // show this address, which it does once this call has returned. + // Listeners may run nested event loops that delete this model, so check + // it after each. + // + // The transaction list shows nothing of an entry but its label, so skip + // its pass over the history when the label stays as it was, as for the + // unlabelled receiving entry the wallet files for each address it finds + // used while it connects blocks or rescans, a change of purpose only, or + // the removal of an unlabelled entry. + QPointer model{this}; + const QString shown{m_complete_address_table_model->cachedLabelForAddress(address)}; + m_complete_address_table_model->updateEntry(address, label, isMine, purpose, status); + if (!model) return; + if (addressTableModel && addressTableModel != m_complete_address_table_model) addressTableModel->updateEntry(address, label, isMine, purpose, status); + if (!model) return; + if (transactionTableModel && m_complete_address_table_model->cachedLabelForAddress(address) != shown) { + transactionTableModel->updateAddressLabel(address); + } +} + +void WalletModel::queueAddressBookReconcile() +{ + // One queued call at a time, and none while the retry timer waits for + // the wallet lock: it covers every address recorded until it fires. + if (m_address_book_reconcile_queued || (m_address_book_retry_timer && m_address_book_retry_timer->isActive())) return; + m_address_book_reconcile_queued = true; + QMetaObject::invokeMethod(this, [this] { reconcileAddressBook(); }, Qt::QueuedConnection); +} + +void WalletModel::reconcileAddressBook() +{ + m_address_book_reconcile_queued = false; + // An entry the wallet holds under a destination its encoding does not + // name, such as one that encodes to the empty string, or an address this + // chain does not decode, cannot be looked up by that encoding: the read + // would find no entry and remove the row. Leave such a row as its + // notifications left it. + std::vector addresses; + std::vector destinations; + for (auto it{m_unreconciled_addresses.begin()}; it != m_unreconciled_addresses.end();) { + CTxDestination destination{DecodeDestination(it->toStdString())}; + if (!IsValidDestination(destination)) { + it = m_unreconciled_addresses.erase(it); + continue; + } + addresses.push_back(*it); + destinations.push_back(std::move(destination)); + ++it; + } + if (addresses.empty()) return; + // Never wait for the wallet lock on this thread. While it is busy, as + // while the wallet connects a block or is being encrypted, keep the + // addresses and try again later. + std::vector> entries; + if (!m_wallet->tryGetAddresses(destinations, entries)) { + if (!m_address_book_retry_timer) { + m_address_book_retry_timer = new QTimer(this); + m_address_book_retry_timer->setObjectName(QStringLiteral("addressBookRetryTimer")); + m_address_book_retry_timer->setSingleShot(true); + m_address_book_retry_timer->setInterval(MODEL_UPDATE_DELAY); + connect(m_address_book_retry_timer, &QTimer::timeout, this, &WalletModel::reconcileAddressBook); + } + m_address_book_retry_timer->start(); + return; + } + m_unreconciled_addresses.clear(); + if (!Assume(entries.size() == addresses.size())) return; + // Bring each row that differs from the wallet's entry to it, as a + // notification of that entry would. A model the message signing dialog + // was handed gets the same change, so it takes in no entry that a + // notification would not give it. + QPointer model{this}; + for (size_t i = 0; i < addresses.size(); ++i) { + const interfaces::WalletAddress* const entry{entries[i] ? &*entries[i] : nullptr}; + const std::optional change{m_complete_address_table_model->changeToEntry(addresses[i], entry)}; + if (!change) continue; + const uint64_t notifications{m_address_book_notifications}; + if (entry) { + applyAddressBookChange(addresses[i], QString::fromStdString(entry->name), entry->is_mine, entry->purpose, *change); + } else { + applyAddressBookChange(addresses[i], QString{}, /*isMine=*/false, wallet::AddressPurpose::SEND, *change); + } + if (!model) return; + // A listener may run a nested event loop that delivers a newer + // notification, which the entries read above for the addresses still + // to come may predate. Read those again instead of applying them. + if (m_address_book_notifications != notifications) { + for (size_t later = i + 1; later < addresses.size(); ++later) m_unreconciled_addresses.insert(addresses[later]); + queueAddressBookReconcile(); + return; + } + } } void WalletModel::updateCanGetAddresses() @@ -746,7 +877,7 @@ void WalletModel::subscribeToCoreSignals() // Connect signals to wallet m_handler_unload = m_wallet->handleUnload(std::bind(&NotifyUnload, this)); m_handler_status_changed = m_wallet->handleStatusChanged(std::bind(&NotifyKeyStoreStatusChanged, this)); - m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind(NotifyAddressBookChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); + // The constructor has registered m_handler_address_book_changed. m_handler_transaction_changed = m_wallet->handleTransactionChanged(std::bind(NotifyTransactionChanged, this, std::placeholders::_1, std::placeholders::_2)); m_handler_show_progress = m_wallet->handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2)); m_handler_can_get_addrs_changed = m_wallet->handleCanGetAddressesChanged(std::bind(NotifyCanGetAddressesChanged, this)); diff --git a/src/qt/walletmodel.h b/src/qt/walletmodel.h index 8149a16d43..b2991d276a 100644 --- a/src/qt/walletmodel.h +++ b/src/qt/walletmodel.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -265,6 +266,19 @@ class WalletModel : public QObject OptionsModel *optionsModel; AddressTableModel* addressTableModel{nullptr}; + //! The address table model created with this model, which lists every + //! address. The address book pages and the transaction list keep it after + //! refresh() has replaced addressTableModel. + AddressTableModel* m_complete_address_table_model{nullptr}; + //! Encoded addresses an address book notification reported, to compare + //! with the wallet in reconcileAddressBook(). + std::set m_unreconciled_addresses; + bool m_address_book_reconcile_queued{false}; + //! Counts the address book notifications applied, so reconcileAddressBook() + //! can tell that a listener delivered one while it applied an entry. + uint64_t m_address_book_notifications{0}; + //! Runs reconcileAddressBook() again while the wallet lock is busy. + QTimer* m_address_book_retry_timer{nullptr}; TransactionTableModel* transactionTableModel{nullptr}; RecentRequestsTableModel* recentRequestsTableModel{nullptr}; @@ -278,6 +292,9 @@ class WalletModel : public QObject void subscribeToCoreSignals(); void unsubscribeFromCoreSignals(); + void applyAddressBookChange(const QString& address, const QString& label, bool isMine, wallet::AddressPurpose purpose, int status); + void queueAddressBookReconcile(); + void reconcileAddressBook(); void checkBalanceChanged(const interfaces::WalletBalances& new_balances); Q_SIGNALS: diff --git a/src/wallet/interfaces.cpp b/src/wallet/interfaces.cpp index d8301f20d4..f044bcf8ed 100644 --- a/src/wallet/interfaces.cpp +++ b/src/wallet/interfaces.cpp @@ -291,18 +291,41 @@ class WalletImpl : public Wallet } return true; } + //! An address book entry as getAddresses() and tryGetAddresses() list it. + WalletAddress listedAddress(const CTxDestination& dest, const std::string& label, const std::optional& purpose) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) + { + bool is_mine = m_wallet->IsMine(dest); + // In very old wallets, address purpose may not be recorded so we derive it from IsMine + return {dest, is_mine, purpose.value_or(is_mine ? AddressPurpose::RECEIVE : AddressPurpose::SEND), label}; + } std::vector getAddresses() override { LOCK(m_wallet->cs_wallet); std::vector result; m_wallet->ForEachAddrBookEntry([&](const CTxDestination& dest, const std::string& label, bool is_change, const std::optional& purpose) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) { if (is_change) return; - bool is_mine = m_wallet->IsMine(dest); - // In very old wallets, address purpose may not be recorded so we derive it from IsMine - result.emplace_back(dest, is_mine, purpose.value_or(is_mine ? AddressPurpose::RECEIVE : AddressPurpose::SEND), label); + result.push_back(listedAddress(dest, label, purpose)); }); return result; } + bool tryGetAddresses(const std::vector& dests, std::vector>& entries) override + { + TRY_LOCK(m_wallet->cs_wallet, locked_wallet); + if (!locked_wallet) { + return false; + } + entries.clear(); + entries.reserve(dests.size()); + for (const CTxDestination& dest : dests) { + const CAddressBookData* entry{m_wallet->FindAddressBookEntry(dest, /*allow_change=*/false)}; + if (!entry) { + entries.emplace_back(); + continue; + } + entries.emplace_back(listedAddress(dest, entry->GetLabel(), entry->purpose)); + } + return true; + } std::vector getAvailableAddressTypes() override { LOCK(m_wallet->cs_wallet);