Remove direct bitcoin calls from qt/addresstablemodel.cpp

This commit is contained in:
Russell Yanofsky 2017-04-18 13:01:23 -04:00 committed by xdustinface
parent 7bb2d25c3d
commit 50f7d661ab
5 changed files with 71 additions and 35 deletions

View File

@ -87,6 +87,10 @@ public:
}
int64_t getKeysLeftSinceAutoBackup() override { return m_wallet.nKeysLeftSinceAutoBackup; }
std::string getWalletName() override { return m_wallet.GetName(); }
bool getKeyFromPool(bool internal, CPubKey& pub_key) override
{
return m_wallet.GetKeyFromPool(pub_key, internal);
}
bool getPubKey(const CKeyID& address, CPubKey& pub_key) override { return m_wallet.GetPubKey(address, pub_key); }
bool getPrivKey(const CKeyID& address, CKey& key) override { return m_wallet.GetKey(address, key); }
bool isSpendable(const CTxDestination& dest) override { return IsMine(m_wallet, dest) & ISMINE_SPENDABLE; }
@ -95,6 +99,10 @@ public:
{
return m_wallet.SetAddressBook(dest, name, purpose);
}
bool delAddressBook(const CTxDestination& dest) override
{
return m_wallet.DelAddressBook(dest);
}
bool getAddress(const CTxDestination& dest, std::string* name, isminetype* is_mine) override
{
LOCK(m_wallet.cs_wallet);
@ -110,6 +118,15 @@ public:
}
return true;
}
std::vector<WalletAddress> getAddresses() override
{
LOCK(m_wallet.cs_wallet);
std::vector<WalletAddress> result;
for (const auto& item : m_wallet.mapAddressBook) {
result.emplace_back(item.first, IsMine(m_wallet, item.first), item.second.name, item.second.purpose);
}
return result;
}
bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) override
{
LOCK(m_wallet.cs_wallet);

View File

@ -7,6 +7,7 @@
#include <amount.h> // For CAmount
#include <fs.h> // For fs::path
#include <pubkey.h> // For CTxDestination (CKeyID and CScriptID)
#include <script/ismine.h> // For isminefilter, isminetype
#include <script/standard.h> // For CTxDestination
#include <support/allocators/secure.h> // For SecureString
@ -30,6 +31,7 @@ namespace interface {
class Handler;
class PendingWalletTx;
struct WalletAddress;
struct WalletBalances;
struct WalletTxOut;
@ -73,6 +75,9 @@ public:
//! Get wallet name.
virtual std::string getWalletName() = 0;
// Get key from pool.
virtual bool getKeyFromPool(bool internal, CPubKey& pub_key) = 0;
//! Get public key.
virtual bool getPubKey(const CKeyID& address, CPubKey& pub_key) = 0;
@ -88,11 +93,17 @@ public:
//! Add or update address.
virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) = 0;
// Remove address.
virtual bool delAddressBook(const CTxDestination& dest) = 0;
//! Look up address in wallet, return whether exists.
virtual bool getAddress(const CTxDestination& dest,
std::string* name = nullptr,
isminetype* is_mine = nullptr) = 0;
//! Get wallet address list.
virtual std::vector<WalletAddress> getAddresses() = 0;
//! Add dest data.
virtual bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) = 0;
@ -224,6 +235,20 @@ public:
std::string& reject_reason) = 0;
};
//! Information about one wallet address.
struct WalletAddress
{
CTxDestination dest;
isminetype is_mine;
std::string name;
std::string purpose;
WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
: dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
{
}
};
//! Collection of wallet balances.
struct WalletBalances
{

View File

@ -8,6 +8,7 @@
#include <qt/guiutil.h>
#include <qt/walletmodel.h>
#include <interface/node.h>
#include <wallet/wallet.h>
#include <QFont>
@ -67,28 +68,23 @@ static AddressTableEntry::Type translateTransactionType(const QString &strPurpos
class AddressTablePriv
{
public:
CWallet *wallet;
QList<AddressTableEntry> cachedAddressTable;
AddressTableModel *parent;
AddressTablePriv(CWallet *_wallet, AddressTableModel *_parent):
wallet(_wallet), parent(_parent) {}
AddressTablePriv(AddressTableModel *_parent):
parent(_parent) {}
void refreshAddressTable()
void refreshAddressTable(interface::Wallet& wallet)
{
cachedAddressTable.clear();
{
LOCK(wallet->cs_wallet);
for (const std::pair<const CTxDestination, CAddressBookData>& item : wallet->mapAddressBook)
for (const auto& address : wallet.getAddresses())
{
const CTxDestination& address = item.first;
bool fMine = IsMine(*wallet, address);
AddressTableEntry::Type addressType = translateTransactionType(
QString::fromStdString(item.second.purpose), fMine);
const std::string& strName = item.second.name;
QString::fromStdString(address.purpose), address.is_mine);
cachedAddressTable.append(AddressTableEntry(addressType,
QString::fromStdString(strName),
QString::fromStdString(EncodeDestination(address))));
QString::fromStdString(address.name),
QString::fromStdString(EncodeDestination(address.dest))));
}
}
// qLowerBound() and qUpperBound() require our cachedAddressTable list to be sorted in asc order
@ -162,12 +158,12 @@ public:
}
};
AddressTableModel::AddressTableModel(CWallet *_wallet, WalletModel *parent) :
QAbstractTableModel(parent),walletModel(parent),wallet(_wallet),priv(0)
AddressTableModel::AddressTableModel(WalletModel *parent) :
QAbstractTableModel(parent),walletModel(parent),priv(0)
{
columns << tr("Label") << tr("Address");
priv = new AddressTablePriv(wallet, this);
priv->refreshAddressTable();
priv = new AddressTablePriv(this);
priv->refreshAddressTable(parent->wallet());
}
AddressTableModel::~AddressTableModel()
@ -244,7 +240,6 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
if(role == Qt::EditRole)
{
LOCK(wallet->cs_wallet); /* For SetAddressBook / DelAddressBook */
CTxDestination curAddress = DecodeDestination(rec->address.toStdString());
if(index.column() == Label)
{
@ -254,7 +249,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
editStatus = NO_CHANGES;
return false;
}
wallet->SetAddressBook(curAddress, value.toString().toStdString(), strPurpose);
walletModel->wallet().setAddressBook(curAddress, value.toString().toStdString(), strPurpose);
} else if(index.column() == Address) {
CTxDestination newAddress = DecodeDestination(value.toString().toStdString());
// Refuse to set invalid address, set error status and return false
@ -271,7 +266,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
}
// Check for duplicate addresses to prevent accidental deletion of addresses, if you try
// to paste an existing address over another address (with a different label)
else if(wallet->mapAddressBook.count(newAddress))
if (walletModel->wallet().getAddress(newAddress))
{
editStatus = DUPLICATE_ADDRESS;
return false;
@ -280,9 +275,9 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
else if(rec->type == AddressTableEntry::Sending)
{
// Remove old entry
wallet->DelAddressBook(curAddress);
walletModel->wallet().delAddressBook(curAddress);
// Add new entry with new address
wallet->SetAddressBook(newAddress, rec->label.toStdString(), strPurpose);
walletModel->wallet().setAddressBook(newAddress, value.toString().toStdString(), strPurpose);
}
}
return true;
@ -356,8 +351,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
}
// Check for duplicate addresses
{
LOCK(wallet->cs_wallet);
if(wallet->mapAddressBook.count(DecodeDestination(strAddress)))
if(walletModel->wallet().getAddress(DecodeDestination(strAddress)))
{
editStatus = DUPLICATE_ADDRESS;
return QString();
@ -368,7 +362,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
{
// Generate a new address to associate with given label
CPubKey newKey;
if(!wallet->GetKeyFromPool(newKey, false))
if(!walletModel->wallet().getKeyFromPool(false /* internal */, newKey))
{
WalletModel::UnlockContext ctx(walletModel->requestUnlock());
if(!ctx.isValid())
@ -377,7 +371,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
editStatus = WALLET_UNLOCK_FAILURE;
return QString();
}
if(!wallet->GetKeyFromPool(newKey, false))
if(!walletModel->wallet().getKeyFromPool(false /* internal */, newKey))
{
editStatus = KEY_GENERATION_FAILURE;
return QString();
@ -391,7 +385,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
}
// Add entry
wallet->SetAddressBook(DecodeDestination(strAddress), strLabel,
walletModel->wallet().setAddressBook(DecodeDestination(strAddress), strLabel,
(type == Send ? "send" : "receive"));
return QString::fromStdString(strAddress);
}
@ -406,7 +400,7 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex &parent
// Also refuse to remove receiving addresses.
return false;
}
wallet->DelAddressBook(DecodeDestination(rec->address.toStdString()));
walletModel->wallet().delAddressBook(DecodeDestination(rec->address.toStdString()));
return true;
}
@ -422,11 +416,10 @@ QString AddressTableModel::labelForAddress(const QString &address) const
QString AddressTableModel::labelForDestination(const CTxDestination &dest) const
{
{
LOCK(wallet->cs_wallet);
std::map<CTxDestination, CAddressBookData>::iterator mi = wallet->mapAddressBook.find(dest);
if (mi != wallet->mapAddressBook.end())
std::string name;
if (walletModel->wallet().getAddress(dest, &name))
{
return QString::fromStdString(mi->second.name);
return QString::fromStdString(name);
}
}
return QString();

View File

@ -13,7 +13,9 @@
class AddressTablePriv;
class WalletModel;
class CWallet;
namespace interface {
class Wallet;
}
/**
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
Q_OBJECT
public:
explicit AddressTableModel(CWallet *wallet, WalletModel *parent = 0);
explicit AddressTableModel(WalletModel *parent = 0);
~AddressTableModel();
enum ColumnIndex {
@ -79,7 +81,6 @@ public:
private:
WalletModel *walletModel;
CWallet *wallet;
AddressTablePriv *priv;
QStringList columns;
EditStatus editStatus;

View File

@ -50,7 +50,7 @@ WalletModel::WalletModel(std::unique_ptr<interface::Wallet> wallet, interface::N
fHaveWatchOnly = m_wallet->haveWatchOnly();
fForceCheckBalanceChanged = false;
addressTableModel = new AddressTableModel(cwallet, this);
addressTableModel = new AddressTableModel(this);
transactionTableModel = new TransactionTableModel(cwallet, this);
recentRequestsTableModel = new RecentRequestsTableModel(cwallet, this);