Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ class Wallet
//! Get wallet address list.
virtual std::vector<WalletAddress> 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<CTxDestination>& dests, std::vector<std::optional<WalletAddress>>& entries) = 0;

//! Get output types the wallet can hand out as receive addresses.
virtual std::vector<OutputType> getAvailableAddressTypes() = 0;

Expand Down
34 changes: 31 additions & 3 deletions src/qt/addresstablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <qt/guiutil.h>
#include <qt/walletmodel.h>

#include <interfaces/wallet.h>
#include <key_io.h>
#include <wallet/types.h>
#include <wallet/wallet.h>
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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<int> AddressTableModel::changeToEntry(const QString &address, const interfaces::WalletAddress* entry) const
{
const AddressTableEntry* row = priv->find(address);
if (!entry) return row ? std::optional<int>{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<wallet::AddressPurpose> AddressTableModel::purposeForAddress(const QString &address) const
{
wallet::AddressPurpose purpose;
Expand All @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/qt/addresstablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class WalletModel;

namespace interfaces {
class Wallet;
struct WalletAddress;
}
namespace wallet {
enum class AddressPurpose;
Expand Down Expand Up @@ -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<wallet::AddressPurpose> 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<int> changeToEntry(const QString &address, const interfaces::WalletAddress* entry) const;

/* Look up row index of an address in the model.
Return -1 if not found.
*/
Expand Down
61 changes: 54 additions & 7 deletions src/qt/test/syntheticwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <wallet/coincontrol.h>
#include <wallet/wallet.h>

#include <algorithm>
#include <chrono>
#include <optional>
#include <set>
Expand Down Expand Up @@ -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;
Expand All @@ -207,9 +210,39 @@ class SyntheticWallet : public interfaces::Wallet
}
std::vector<interfaces::WalletAddress> getAddresses() override
{
std::unique_lock lock{m_state->mutex};
WaitForEncryption(lock);
return m_state->address_book;
std::function<void()> entered;
std::function<void()> 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<interfaces::WalletAddress> 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<CTxDestination>& dests, std::vector<std::optional<interfaces::WalletAddress>>& 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<OutputType> getAvailableAddressTypes() override { return {OutputType::P2MR}; }
std::vector<std::string> getAddressReceiveRequests() override { return {}; }
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -786,7 +819,20 @@ class SyntheticWallet : public interfaces::Wallet
}
});
}
std::unique_ptr<interfaces::Handler> handleAddressBookChanged(AddressBookChangedFn) override { return interfaces::MakeCleanupHandler([] {}); }
std::unique_ptr<interfaces::Handler> handleAddressBookChanged(AddressBookChangedFn fn) override
{
{
std::lock_guard lock{m_state->mutex};
m_state->address_book_changed = std::move(fn);
}
std::weak_ptr<SyntheticWalletState> 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<interfaces::Handler> handleTransactionChanged(TransactionChangedFn fn) override
{
uint64_t id;
Expand Down Expand Up @@ -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<std::mutex>& 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;
Expand Down
21 changes: 21 additions & 0 deletions src/qt/test/syntheticwallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ struct SyntheticWalletState {
//! The address book, returned by getAddresses() and searched by
//! getAddress(). Both wait on the encryption latch.
std::vector<interfaces::WalletAddress> 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<void()> get_addresses_entered;
std::function<void()> 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};
Expand Down
Loading
Loading