Skip to content

Commit 3ec2ebc

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt/addresstablemodel.cpp
1 parent 827de03 commit 3ec2ebc

File tree

5 files changed

+78
-37
lines changed

5 files changed

+78
-37
lines changed

src/interface/wallet.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class WalletImpl : public Wallet
8585
}
8686
bool backupWallet(const std::string& filename) override { return m_wallet.BackupWallet(filename); }
8787
std::string getWalletName() override { return m_wallet.GetName(); }
88+
bool getKeyFromPool(bool internal, CPubKey& pub_key) override
89+
{
90+
return m_wallet.GetKeyFromPool(pub_key, internal);
91+
}
8892
bool getPubKey(const CKeyID& address, CPubKey& pub_key) override { return m_wallet.GetPubKey(address, pub_key); }
8993
bool getPrivKey(const CKeyID& address, CKey& key) override { return m_wallet.GetKey(address, key); }
9094
bool isSpendable(const CTxDestination& dest) override { return IsMine(m_wallet, dest) & ISMINE_SPENDABLE; }
@@ -93,6 +97,10 @@ class WalletImpl : public Wallet
9397
{
9498
return m_wallet.SetAddressBook(dest, name, purpose);
9599
}
100+
bool delAddressBook(const CTxDestination& dest) override
101+
{
102+
return m_wallet.DelAddressBook(dest);
103+
}
96104
bool getAddress(const CTxDestination& dest, std::string* name, isminetype* is_mine) override
97105
{
98106
LOCK(m_wallet.cs_wallet);
@@ -108,6 +116,16 @@ class WalletImpl : public Wallet
108116
}
109117
return true;
110118
}
119+
std::vector<WalletAddress> getAddresses() override
120+
{
121+
LOCK(m_wallet.cs_wallet);
122+
std::vector<WalletAddress> result;
123+
for (const auto& item : m_wallet.mapAddressBook) {
124+
result.emplace_back(item.first, IsMine(m_wallet, item.first), item.second.name, item.second.purpose);
125+
}
126+
return result;
127+
}
128+
void learnRelatedScripts(const CPubKey& key, OutputType type) override { m_wallet.LearnRelatedScripts(key, type); }
111129
bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) override
112130
{
113131
LOCK(m_wallet.cs_wallet);

src/interface/wallet.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_INTERFACE_WALLET_H
77

88
#include <amount.h> // For CAmount
9+
#include <pubkey.h> // For CTxDestination (CKeyID and CScriptID)
910
#include <script/ismine.h> // For isminefilter, isminetype
1011
#include <script/standard.h> // For CTxDestination
1112
#include <support/allocators/secure.h> // For SecureString
@@ -30,6 +31,7 @@ namespace interface {
3031

3132
class Handler;
3233
class PendingWalletTx;
34+
struct WalletAddress;
3335
struct WalletBalances;
3436
struct WalletTxOut;
3537

@@ -67,6 +69,9 @@ class Wallet
6769
//! Get wallet name.
6870
virtual std::string getWalletName() = 0;
6971

72+
// Get key from pool.
73+
virtual bool getKeyFromPool(bool internal, CPubKey& pub_key) = 0;
74+
7075
//! Get public key.
7176
virtual bool getPubKey(const CKeyID& address, CPubKey& pub_key) = 0;
7277

@@ -82,11 +87,21 @@ class Wallet
8287
//! Add or update address.
8388
virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) = 0;
8489

90+
// Remove address.
91+
virtual bool delAddressBook(const CTxDestination& dest) = 0;
92+
8593
//! Look up address in wallet, return whether exists.
8694
virtual bool getAddress(const CTxDestination& dest,
8795
std::string* name = nullptr,
8896
isminetype* is_mine = nullptr) = 0;
8997

98+
//! Get wallet address list.
99+
virtual std::vector<WalletAddress> getAddresses() = 0;
100+
101+
//! Add scripts to key store so old so software versions opening the wallet
102+
//! database can detect payments to newer address types.
103+
virtual void learnRelatedScripts(const CPubKey& key, OutputType type) = 0;
104+
90105
//! Add dest data.
91106
virtual bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) = 0;
92107

@@ -216,6 +231,20 @@ class PendingWalletTx
216231
std::string& reject_reason) = 0;
217232
};
218233

234+
//! Information about one wallet address.
235+
struct WalletAddress
236+
{
237+
CTxDestination dest;
238+
isminetype is_mine;
239+
std::string name;
240+
std::string purpose;
241+
242+
WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
243+
: dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
244+
{
245+
}
246+
};
247+
219248
//! Collection of wallet balances.
220249
struct WalletBalances
221250
{

src/qt/addresstablemodel.cpp

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <qt/guiutil.h>
88
#include <qt/walletmodel.h>
99

10+
#include <interface/node.h>
1011
#include <key_io.h>
1112
#include <wallet/wallet.h>
1213

@@ -67,28 +68,23 @@ static AddressTableEntry::Type translateTransactionType(const QString &strPurpos
6768
class AddressTablePriv
6869
{
6970
public:
70-
CWallet *wallet;
7171
QList<AddressTableEntry> cachedAddressTable;
7272
AddressTableModel *parent;
7373

74-
AddressTablePriv(CWallet *_wallet, AddressTableModel *_parent):
75-
wallet(_wallet), parent(_parent) {}
74+
AddressTablePriv(AddressTableModel *_parent):
75+
parent(_parent) {}
7676

77-
void refreshAddressTable()
77+
void refreshAddressTable(interface::Wallet& wallet)
7878
{
7979
cachedAddressTable.clear();
8080
{
81-
LOCK(wallet->cs_wallet);
82-
for (const std::pair<CTxDestination, CAddressBookData>& item : wallet->mapAddressBook)
81+
for (const auto& address : wallet.getAddresses())
8382
{
84-
const CTxDestination& address = item.first;
85-
bool fMine = IsMine(*wallet, address);
8683
AddressTableEntry::Type addressType = translateTransactionType(
87-
QString::fromStdString(item.second.purpose), fMine);
88-
const std::string& strName = item.second.name;
84+
QString::fromStdString(address.purpose), address.is_mine);
8985
cachedAddressTable.append(AddressTableEntry(addressType,
90-
QString::fromStdString(strName),
91-
QString::fromStdString(EncodeDestination(address))));
86+
QString::fromStdString(address.name),
87+
QString::fromStdString(EncodeDestination(address.dest))));
9288
}
9389
}
9490
// qLowerBound() and qUpperBound() require our cachedAddressTable list to be sorted in asc order
@@ -162,12 +158,12 @@ class AddressTablePriv
162158
}
163159
};
164160

165-
AddressTableModel::AddressTableModel(CWallet *_wallet, WalletModel *parent) :
166-
QAbstractTableModel(parent),walletModel(parent),wallet(_wallet),priv(0)
161+
AddressTableModel::AddressTableModel(WalletModel *parent) :
162+
QAbstractTableModel(parent),walletModel(parent),priv(0)
167163
{
168164
columns << tr("Label") << tr("Address");
169-
priv = new AddressTablePriv(wallet, this);
170-
priv->refreshAddressTable();
165+
priv = new AddressTablePriv(this);
166+
priv->refreshAddressTable(parent->wallet());
171167
}
172168

173169
AddressTableModel::~AddressTableModel()
@@ -244,7 +240,6 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
244240

245241
if(role == Qt::EditRole)
246242
{
247-
LOCK(wallet->cs_wallet); /* For SetAddressBook / DelAddressBook */
248243
CTxDestination curAddress = DecodeDestination(rec->address.toStdString());
249244
if(index.column() == Label)
250245
{
@@ -254,7 +249,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
254249
editStatus = NO_CHANGES;
255250
return false;
256251
}
257-
wallet->SetAddressBook(curAddress, value.toString().toStdString(), strPurpose);
252+
walletModel->wallet().setAddressBook(curAddress, value.toString().toStdString(), strPurpose);
258253
} else if(index.column() == Address) {
259254
CTxDestination newAddress = DecodeDestination(value.toString().toStdString());
260255
// Refuse to set invalid address, set error status and return false
@@ -271,7 +266,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
271266
}
272267
// Check for duplicate addresses to prevent accidental deletion of addresses, if you try
273268
// to paste an existing address over another address (with a different label)
274-
else if(wallet->mapAddressBook.count(newAddress))
269+
if (walletModel->wallet().getAddress(newAddress))
275270
{
276271
editStatus = DUPLICATE_ADDRESS;
277272
return false;
@@ -280,9 +275,9 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
280275
else if(rec->type == AddressTableEntry::Sending)
281276
{
282277
// Remove old entry
283-
wallet->DelAddressBook(curAddress);
278+
walletModel->wallet().delAddressBook(curAddress);
284279
// Add new entry with new address
285-
wallet->SetAddressBook(newAddress, rec->label.toStdString(), strPurpose);
280+
walletModel->wallet().setAddressBook(newAddress, value.toString().toStdString(), strPurpose);
286281
}
287282
}
288283
return true;
@@ -356,8 +351,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
356351
}
357352
// Check for duplicate addresses
358353
{
359-
LOCK(wallet->cs_wallet);
360-
if(wallet->mapAddressBook.count(DecodeDestination(strAddress)))
354+
if(walletModel->wallet().getAddress(DecodeDestination(strAddress)))
361355
{
362356
editStatus = DUPLICATE_ADDRESS;
363357
return QString();
@@ -368,7 +362,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
368362
{
369363
// Generate a new address to associate with given label
370364
CPubKey newKey;
371-
if(!wallet->GetKeyFromPool(newKey))
365+
if(!walletModel->wallet().getKeyFromPool(false /* internal */, newKey))
372366
{
373367
WalletModel::UnlockContext ctx(walletModel->requestUnlock());
374368
if(!ctx.isValid())
@@ -377,13 +371,13 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
377371
editStatus = WALLET_UNLOCK_FAILURE;
378372
return QString();
379373
}
380-
if(!wallet->GetKeyFromPool(newKey))
374+
if(!walletModel->wallet().getKeyFromPool(false /* internal */, newKey))
381375
{
382376
editStatus = KEY_GENERATION_FAILURE;
383377
return QString();
384378
}
385379
}
386-
wallet->LearnRelatedScripts(newKey, address_type);
380+
walletModel->wallet().learnRelatedScripts(newKey, address_type);
387381
strAddress = EncodeDestination(GetDestinationForKey(newKey, address_type));
388382
}
389383
else
@@ -392,7 +386,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
392386
}
393387

394388
// Add entry
395-
wallet->SetAddressBook(DecodeDestination(strAddress), strLabel,
389+
walletModel->wallet().setAddressBook(DecodeDestination(strAddress), strLabel,
396390
(type == Send ? "send" : "receive"));
397391
return QString::fromStdString(strAddress);
398392
}
@@ -407,7 +401,7 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex &parent
407401
// Also refuse to remove receiving addresses.
408402
return false;
409403
}
410-
wallet->DelAddressBook(DecodeDestination(rec->address.toStdString()));
404+
walletModel->wallet().delAddressBook(DecodeDestination(rec->address.toStdString()));
411405
return true;
412406
}
413407

@@ -416,12 +410,11 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex &parent
416410
QString AddressTableModel::labelForAddress(const QString &address) const
417411
{
418412
{
419-
LOCK(wallet->cs_wallet);
420413
CTxDestination destination = DecodeDestination(address.toStdString());
421-
std::map<CTxDestination, CAddressBookData>::iterator mi = wallet->mapAddressBook.find(destination);
422-
if (mi != wallet->mapAddressBook.end())
414+
std::string name;
415+
if (walletModel->wallet().getAddress(destination, &name))
423416
{
424-
return QString::fromStdString(mi->second.name);
417+
return QString::fromStdString(name);
425418
}
426419
}
427420
return QString();
@@ -441,7 +434,7 @@ int AddressTableModel::lookupAddress(const QString &address) const
441434
}
442435
}
443436

444-
OutputType AddressTableModel::GetDefaultAddressType() const { return wallet->m_default_address_type; };
437+
OutputType AddressTableModel::GetDefaultAddressType() const { return walletModel->wallet().getDefaultAddressType(); };
445438

446439
void AddressTableModel::emitDataChanged(int idx)
447440
{

src/qt/addresstablemodel.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ enum class OutputType;
1313
class AddressTablePriv;
1414
class WalletModel;
1515

16-
class CWallet;
16+
namespace interface {
17+
class Wallet;
18+
}
1719

1820
/**
1921
Qt model of the address book in the core. This allows views to access and modify the address book.
@@ -23,7 +25,7 @@ class AddressTableModel : public QAbstractTableModel
2325
Q_OBJECT
2426

2527
public:
26-
explicit AddressTableModel(CWallet *wallet, WalletModel *parent = 0);
28+
explicit AddressTableModel(WalletModel *parent = 0);
2729
~AddressTableModel();
2830

2931
enum ColumnIndex {
@@ -80,7 +82,6 @@ class AddressTableModel : public QAbstractTableModel
8082

8183
private:
8284
WalletModel *walletModel;
83-
CWallet *wallet;
8485
AddressTablePriv *priv;
8586
QStringList columns;
8687
EditStatus editStatus;

src/qt/walletmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ WalletModel::WalletModel(std::unique_ptr<interface::Wallet> wallet, interface::N
4949
fHaveWatchOnly = m_wallet->haveWatchOnly();
5050
fForceCheckBalanceChanged = false;
5151

52-
addressTableModel = new AddressTableModel(cwallet, this);
52+
addressTableModel = new AddressTableModel(this);
5353
transactionTableModel = new TransactionTableModel(platformStyle, cwallet, this);
5454
recentRequestsTableModel = new RecentRequestsTableModel(cwallet, this);
5555

0 commit comments

Comments
 (0)