Skip to content

Commit 62252c9

Browse files
committed
interfaces: Stop exposing wallet destdata to gui
Stop giving GUI access to destdata rows in database. Replace with narrow API just for saving and reading receive request information. This simplifies code and should prevent the GUI from interfering with other destdata like address-used status. Note: No user-visible behavior is changing in this commit. New CWallet::SetAddressReceiveRequest() implementation avoids a bug in CWallet::AddDestData() where a modification would leave the previous value in memory while writing the new value to disk. But it doesn't matter because the GUI doesn't currently expose the ability to modify receive requests, only to add and erase them.
1 parent 985430d commit 62252c9

File tree

9 files changed

+39
-54
lines changed

9 files changed

+39
-54
lines changed

src/interfaces/wallet.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,11 @@ class Wallet
112112
//! Get wallet address list.
113113
virtual std::vector<WalletAddress> getAddresses() = 0;
114114

115-
//! Add dest data.
116-
virtual bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) = 0;
115+
//! Get receive requests.
116+
virtual std::vector<std::string> getAddressReceiveRequests() = 0;
117117

118-
//! Erase dest data.
119-
virtual bool eraseDestData(const CTxDestination& dest, const std::string& key) = 0;
120-
121-
//! Get dest values with prefix.
122-
virtual std::vector<std::string> getDestValues(const std::string& prefix) = 0;
118+
//! Save or remove receive request.
119+
virtual bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) = 0;
123120

124121
//! Lock coin.
125122
virtual void lockCoin(const COutPoint& output) = 0;

src/qt/recentrequeststablemodel.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
#include <qt/walletmodel.h>
1111

1212
#include <clientversion.h>
13+
#include <interfaces/wallet.h>
14+
#include <key_io.h>
1315
#include <streams.h>
16+
#include <util/string.h>
1417

1518
#include <utility>
1619

1720
RecentRequestsTableModel::RecentRequestsTableModel(WalletModel *parent) :
1821
QAbstractTableModel(parent), walletModel(parent)
1922
{
2023
// Load entries from wallet
21-
std::vector<std::string> vReceiveRequests;
22-
parent->loadReceiveRequests(vReceiveRequests);
23-
for (const std::string& request : vReceiveRequests)
24+
for (const std::string& request : parent->wallet().getAddressReceiveRequests()) {
2425
addNewRequest(request);
26+
}
2527

2628
/* These columns must match the indices in the ColumnIndex enumeration */
2729
columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
@@ -143,7 +145,7 @@ bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex
143145
for (int i = 0; i < count; ++i)
144146
{
145147
const RecentRequestEntry* rec = &list[row+i];
146-
if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
148+
if (!walletModel->wallet().setAddressReceiveRequest(DecodeDestination(rec->recipient.address.toStdString()), ToString(rec->id), ""))
147149
return false;
148150
}
149151

@@ -172,7 +174,7 @@ void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient
172174
CDataStream ss(SER_DISK, CLIENT_VERSION);
173175
ss << newEntry;
174176

175-
if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
177+
if (!walletModel->wallet().setAddressReceiveRequest(DecodeDestination(recipient.address.toStdString()), ToString(newEntry.id), ss.str()))
176178
return;
177179

178180
addNewRequest(newEntry);

src/qt/test/wallettests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void TestGUI(interfaces::Node& node)
264264
QCOMPARE(currentRowCount, initialRowCount+1);
265265

266266
// Check addition to wallet
267-
std::vector<std::string> requests = walletModel.wallet().getDestValues("rr");
267+
std::vector<std::string> requests = walletModel.wallet().getAddressReceiveRequests();
268268
QCOMPARE(requests.size(), size_t{1});
269269
RecentRequestEntry entry;
270270
CDataStream{MakeUCharSpan(requests[0]), SER_DISK, CLIENT_VERSION} >> entry;
@@ -286,7 +286,7 @@ void TestGUI(interfaces::Node& node)
286286
QCOMPARE(requestTableModel->rowCount({}), currentRowCount-1);
287287

288288
// Check removal from wallet
289-
QCOMPARE(walletModel.wallet().getDestValues("rr").size(), size_t{0});
289+
QCOMPARE(walletModel.wallet().getAddressReceiveRequests().size(), size_t{0});
290290
}
291291

292292
} // namespace

src/qt/walletmodel.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -463,25 +463,6 @@ void WalletModel::UnlockContext::CopyFrom(UnlockContext&& rhs)
463463
rhs.relock = false;
464464
}
465465

466-
void WalletModel::loadReceiveRequests(std::vector<std::string>& vReceiveRequests)
467-
{
468-
vReceiveRequests = m_wallet->getDestValues("rr"); // receive request
469-
}
470-
471-
bool WalletModel::saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
472-
{
473-
CTxDestination dest = DecodeDestination(sAddress);
474-
475-
std::stringstream ss;
476-
ss << nId;
477-
std::string key = "rr" + ss.str(); // "rr" prefix = "receive request" in destdata
478-
479-
if (sRequest.empty())
480-
return m_wallet->eraseDestData(dest, key);
481-
else
482-
return m_wallet->addDestData(dest, key, sRequest);
483-
}
484-
485466
bool WalletModel::bumpFee(uint256 hash, uint256& new_hash)
486467
{
487468
CCoinControl coin_control;

src/qt/walletmodel.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ class WalletModel : public QObject
135135

136136
UnlockContext requestUnlock();
137137

138-
void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
139-
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
140-
141138
bool bumpFee(uint256 hash, uint256& new_hash);
142139

143140
static bool isWalletEnabled();

src/wallet/interfaces.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,14 @@ class WalletImpl : public Wallet
199199
}
200200
return result;
201201
}
202-
bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) override
203-
{
202+
std::vector<std::string> getAddressReceiveRequests() override {
204203
LOCK(m_wallet->cs_wallet);
205-
WalletBatch batch{m_wallet->GetDatabase()};
206-
return m_wallet->AddDestData(batch, dest, key, value);
204+
return m_wallet->GetAddressReceiveRequests();
207205
}
208-
bool eraseDestData(const CTxDestination& dest, const std::string& key) override
209-
{
206+
bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) override {
210207
LOCK(m_wallet->cs_wallet);
211208
WalletBatch batch{m_wallet->GetDatabase()};
212-
return m_wallet->EraseDestData(batch, dest, key);
213-
}
214-
std::vector<std::string> getDestValues(const std::string& prefix) override
215-
{
216-
LOCK(m_wallet->cs_wallet);
217-
return m_wallet->GetDestValues(prefix);
209+
return m_wallet->SetAddressReceiveRequest(batch, dest, id, value);
218210
}
219211
void lockCoin(const COutPoint& output) override
220212
{

src/wallet/test/wallet_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
391391
LOCK(m_wallet.cs_wallet);
392392
WalletBatch batch{m_wallet.GetDatabase()};
393393
m_wallet.AddDestData(batch, dest, "misc", "val_misc");
394-
m_wallet.AddDestData(batch, dest, "rr0", "val_rr0");
395-
m_wallet.AddDestData(batch, dest, "rr1", "val_rr1");
394+
m_wallet.SetAddressReceiveRequest(batch, dest, "0", "val_rr0");
395+
m_wallet.SetAddressReceiveRequest(batch, dest, "1", "val_rr1");
396396

397-
auto values = m_wallet.GetDestValues("rr");
397+
auto values = m_wallet.GetAddressReceiveRequests();
398398
BOOST_CHECK_EQUAL(values.size(), 2U);
399399
BOOST_CHECK_EQUAL(values[0], "val_rr0");
400400
BOOST_CHECK_EQUAL(values[1], "val_rr1");

src/wallet/wallet.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3798,8 +3798,9 @@ bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, st
37983798
return false;
37993799
}
38003800

3801-
std::vector<std::string> CWallet::GetDestValues(const std::string& prefix) const
3801+
std::vector<std::string> CWallet::GetAddressReceiveRequests() const
38023802
{
3803+
const std::string prefix{"rr"};
38033804
std::vector<std::string> values;
38043805
for (const auto& address : m_address_book) {
38053806
for (const auto& data : address.second.destdata) {
@@ -3811,6 +3812,20 @@ std::vector<std::string> CWallet::GetDestValues(const std::string& prefix) const
38113812
return values;
38123813
}
38133814

3815+
bool CWallet::SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id, const std::string& value)
3816+
{
3817+
const std::string key{"rr" + id}; // "rr" prefix = "receive request" in destdata
3818+
CAddressBookData& data = m_address_book.at(dest);
3819+
if (value.empty()) {
3820+
if (!batch.EraseDestData(EncodeDestination(dest), key)) return false;
3821+
data.destdata.erase(key);
3822+
} else {
3823+
if (!batch.WriteDestData(EncodeDestination(dest), key, value)) return false;
3824+
data.destdata[key] = value;
3825+
}
3826+
return true;
3827+
}
3828+
38143829
std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error_string)
38153830
{
38163831
// Do some checking on wallet path. It should be either a:

src/wallet/wallet.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
876876
void LoadDestData(const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
877877
//! Look up a destination data tuple in the store, return true if found false otherwise
878878
bool GetDestData(const CTxDestination& dest, const std::string& key, std::string* value) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
879-
//! Get all destination values matching a prefix.
880-
std::vector<std::string> GetDestValues(const std::string& prefix) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
881879

882880
//! Holds a timestamp at which point the wallet is scheduled (externally) to be relocked. Caller must arrange for actual relocking to occur via Lock().
883881
int64_t nRelockTime GUARDED_BY(cs_wallet){0};
@@ -1084,6 +1082,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10841082

10851083
bool DelAddressBook(const CTxDestination& address);
10861084

1085+
std::vector<std::string> GetAddressReceiveRequests() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1086+
bool SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1087+
10871088
unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10881089

10891090
//! signify that a particular wallet feature is now used.

0 commit comments

Comments
 (0)