Skip to content

Commit 2877c80

Browse files
committed
Store wallet request ids as integers instead of strings as suggested #27224 (comment)
1 parent be0325c commit 2877c80

File tree

8 files changed

+31
-26
lines changed

8 files changed

+31
-26
lines changed

src/interfaces/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Wallet
122122
virtual std::vector<std::string> getAddressReceiveRequests() = 0;
123123

124124
//! Save or remove receive request.
125-
virtual bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) = 0;
125+
virtual bool setAddressReceiveRequest(const CTxDestination& dest, int64_t id, const std::string& value) = 0;
126126

127127
//! Display address on external signer
128128
virtual bool displayAddress(const CTxDestination& dest) = 0;

src/qt/recentrequeststablemodel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex
149149
for (int i = 0; i < count; ++i)
150150
{
151151
const RecentRequestEntry* rec = &list[row+i];
152-
if (!walletModel->wallet().setAddressReceiveRequest(DecodeDestination(rec->recipient.address.toStdString()), ToString(rec->id), ""))
152+
if (!walletModel->wallet().setAddressReceiveRequest(DecodeDestination(rec->recipient.address.toStdString()), rec->id, ""))
153153
return false;
154154
}
155155

@@ -178,7 +178,7 @@ void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient
178178
DataStream ss{};
179179
ss << newEntry;
180180

181-
if (!walletModel->wallet().setAddressReceiveRequest(DecodeDestination(recipient.address.toStdString()), ToString(newEntry.id), ss.str()))
181+
if (!walletModel->wallet().setAddressReceiveRequest(DecodeDestination(recipient.address.toStdString()), newEntry.id, ss.str()))
182182
return;
183183

184184
addNewRequest(newEntry);

src/wallet/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class WalletImpl : public Wallet
227227
LOCK(m_wallet->cs_wallet);
228228
return m_wallet->GetAddressReceiveRequests();
229229
}
230-
bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) override {
230+
bool setAddressReceiveRequest(const CTxDestination& dest, int64_t id, const std::string& value) override {
231231
// Note: The setAddressReceiveRequest interface used by the GUI to store
232232
// receive requests is a little awkward and could be improved in the
233233
// future:

src/wallet/test/wallet_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,11 @@ BOOST_FIXTURE_TEST_CASE(LoadReceiveRequests, TestingSetup)
460460
WalletBatch batch{wallet->GetDatabase()};
461461
BOOST_CHECK(batch.WriteAddressPreviouslySpent(PKHash(), true));
462462
BOOST_CHECK(batch.WriteAddressPreviouslySpent(ScriptHash(), true));
463-
BOOST_CHECK(wallet->SetAddressReceiveRequest(batch, PKHash(), "0", "val_rr00"));
464-
BOOST_CHECK(wallet->EraseAddressReceiveRequest(batch, PKHash(), "0"));
465-
BOOST_CHECK(wallet->SetAddressReceiveRequest(batch, PKHash(), "1", "val_rr10"));
466-
BOOST_CHECK(wallet->SetAddressReceiveRequest(batch, PKHash(), "1", "val_rr11"));
467-
BOOST_CHECK(wallet->SetAddressReceiveRequest(batch, ScriptHash(), "2", "val_rr20"));
463+
BOOST_CHECK(wallet->SetAddressReceiveRequest(batch, PKHash(), 0, "val_rr00"));
464+
BOOST_CHECK(wallet->EraseAddressReceiveRequest(batch, PKHash(), 0));
465+
BOOST_CHECK(wallet->SetAddressReceiveRequest(batch, PKHash(), 1, "val_rr10"));
466+
BOOST_CHECK(wallet->SetAddressReceiveRequest(batch, PKHash(), 1, "val_rr11"));
467+
BOOST_CHECK(wallet->SetAddressReceiveRequest(batch, ScriptHash(), 2, "val_rr20"));
468468
});
469469
TestLoadWallet(name, format, [](std::shared_ptr<CWallet> wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet) {
470470
BOOST_CHECK(wallet->IsAddressPreviouslySpent(PKHash()));

src/wallet/wallet.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,7 @@ void CWallet::LoadAddressPreviouslySpent(const CTxDestination& dest)
28052805
m_address_book[dest].previously_spent = true;
28062806
}
28072807

2808-
void CWallet::LoadAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& request)
2808+
void CWallet::LoadAddressReceiveRequest(const CTxDestination& dest, int64_t id, const std::string& request)
28092809
{
28102810
m_address_book[dest].receive_requests[id] = request;
28112811
}
@@ -2827,14 +2827,14 @@ std::vector<std::string> CWallet::GetAddressReceiveRequests() const
28272827
return values;
28282828
}
28292829

2830-
bool CWallet::SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id, const std::string& value)
2830+
bool CWallet::SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, int64_t id, const std::string& value)
28312831
{
28322832
if (!batch.WriteAddressReceiveRequest(dest, id, value)) return false;
28332833
m_address_book[dest].receive_requests[id] = value;
28342834
return true;
28352835
}
28362836

2837-
bool CWallet::EraseAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id)
2837+
bool CWallet::EraseAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, int64_t id)
28382838
{
28392839
if (!batch.EraseAddressReceiveRequest(dest, id)) return false;
28402840
m_address_book[dest].receive_requests.erase(id);

src/wallet/wallet.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ struct CAddressBookData
232232
/**
233233
* Map containing data about previously generated receive requests
234234
* requesting funds to be sent to this address. Only present for IsMine
235-
* addresses. Map keys are decimal numbers uniquely identifying each
236-
* request, and map values are serialized RecentRequestEntry objects
237-
* containing BIP21 URI information including message and amount.
235+
* addresses. Map keys are numbers uniquely identifying each request, and
236+
* map values are serialized RecentRequestEntry objects containing BIP21 URI
237+
* information including message and amount.
238238
*/
239-
std::map<std::string, std::string> receive_requests{};
239+
std::map<int64_t, std::string> receive_requests{};
240240

241241
/** Accessor methods. */
242242
bool IsChange() const { return !label.has_value(); }
@@ -519,7 +519,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
519519
//! Marks destination as previously spent.
520520
void LoadAddressPreviouslySpent(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
521521
//! Appends payment request to destination.
522-
void LoadAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& request) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
522+
void LoadAddressReceiveRequest(const CTxDestination& dest, int64_t id, const std::string& request) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
523523

524524
//! Holds a timestamp at which point the wallet is scheduled (externally) to be relocked. Caller must arrange for actual relocking to occur via Lock().
525525
int64_t nRelockTime GUARDED_BY(cs_wallet){0};
@@ -750,8 +750,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
750750
bool SetAddressPreviouslySpent(WalletBatch& batch, const CTxDestination& dest, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
751751

752752
std::vector<std::string> GetAddressReceiveRequests() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
753-
bool SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
754-
bool EraseAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
753+
bool SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, int64_t id, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
754+
bool EraseAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, int64_t id) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
755755

756756
unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
757757

src/wallet/walletdb.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,12 @@ ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
627627
} else if (strKey.compare(0, 2, "rr") == 0) {
628628
// Load "rr##" keys where ## is a decimal number, and strValue
629629
// is a serialized RecentRequestEntry object.
630-
pwallet->LoadAddressReceiveRequest(dest, strKey.substr(2), strValue);
630+
int64_t request_id;
631+
if (ParseInt64(strKey.substr(2), &request_id)) {
632+
pwallet->LoadAddressReceiveRequest(dest, request_id, strValue);
633+
} else {
634+
pwallet->WalletLogPrintf("Warning: ignoring non-integer request id '%s' for address '%s'\n", strKey, strAddress);
635+
}
631636
}
632637
} else if (strType == DBKeys::HDCHAIN) {
633638
CHDChain chain;
@@ -1107,14 +1112,14 @@ bool WalletBatch::WriteAddressPreviouslySpent(const CTxDestination& dest, bool p
11071112
return previously_spent ? WriteIC(key, std::string("1")) : EraseIC(key);
11081113
}
11091114

1110-
bool WalletBatch::WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request)
1115+
bool WalletBatch::WriteAddressReceiveRequest(const CTxDestination& dest, int64_t id, const std::string& receive_request)
11111116
{
1112-
return WriteIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(EncodeDestination(dest), "rr" + id)), receive_request);
1117+
return WriteIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(EncodeDestination(dest), "rr" + ToString(id))), receive_request);
11131118
}
11141119

1115-
bool WalletBatch::EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id)
1120+
bool WalletBatch::EraseAddressReceiveRequest(const CTxDestination& dest, int64_t id)
11161121
{
1117-
return EraseIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(EncodeDestination(dest), "rr" + id)));
1122+
return EraseIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(EncodeDestination(dest), "rr" + ToString(id))));
11181123
}
11191124

11201125
bool WalletBatch::EraseAddressData(const CTxDestination& dest)

src/wallet/walletdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ class WalletBatch
266266
bool EraseLockedUTXO(const COutPoint& output);
267267

268268
bool WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent);
269-
bool WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request);
270-
bool EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id);
269+
bool WriteAddressReceiveRequest(const CTxDestination& dest, int64_t id, const std::string& receive_request);
270+
bool EraseAddressReceiveRequest(const CTxDestination& dest, int64_t id);
271271
bool EraseAddressData(const CTxDestination& dest);
272272

273273
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);

0 commit comments

Comments
 (0)