-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: encapsulate wallet's address book access #25337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: encapsulate wallet's address book access #25337
Conversation
https://github.com/bitcoin/bitcoin/pull/25337/checks?check_run_id=6844212884: CXX wallet/rpc/libbitcoin_wallet_a-coins.o
wallet/interfaces.cpp:213:49: error: calling function 'IsMine' requires holding mutex 'm_wallet->cs_wallet' exclusively [-Werror,-Wthread-safety-analysis]
result.emplace_back(dest, m_wallet->IsMine(dest), label, purpose);
^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrt thread-safety annotations, I'd suggest to:
- rebase this PR to include #24931
- ensure that every commit does not introduce new
-Wthread-safety
warnings when compiling with clang - in the "wallet: implement ForEachAddrBookEntry method" commit (ead512a) apply the following patch:
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -653,7 +653,8 @@ public:
* Stops when the provided 'ListAddrBookFunc' returns false.
*/
using ListAddrBookFunc = std::function<void(const CTxDestination& dest, const std::string& label, const std::string& purpose, bool is_change)>;
- void ForEachAddrBookEntry(const ListAddrBookFunc& func = [](const CTxDestination&, const std::string&, const std::string&, bool is_change){ return; }) const;
+ void ForEachAddrBookEntry(const ListAddrBookFunc& func = [](const CTxDestination&, const std::string&, const std::string&, bool is_change){ return; }) const
+ EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
/**
* Marks all outputs in each one of the destinations dirty, so their cache is
- in the "refactor: use ForEachAddrBookEntry in interfaces::wallet::getAddresses" commit (c629bef) apply the following patch:
--- a/src/wallet/interfaces.cpp
+++ b/src/wallet/interfaces.cpp
@@ -208,7 +208,7 @@ public:
{
LOCK(m_wallet->cs_wallet);
std::vector<WalletAddress> result;
- m_wallet->ForEachAddrBookEntry([&](const CTxDestination& dest, const std::string& label, const std::string& purpose, bool is_change) {
+ m_wallet->ForEachAddrBookEntry([&](const CTxDestination& dest, const std::string& label, const std::string& purpose, bool is_change) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) {
if (is_change) return;
result.emplace_back(dest, m_wallet->IsMine(dest), label, purpose);
});
- and maybe skip the last commit?
61b4654
to
005d111
Compare
Thanks for the review hebasto! Great suggestions 👌🏼 , have applied them all.
Ensured. Have compiled every commit with --enable-debug`.
Done. Agree that it's not related to the scope of this work. Will push it in another PR. |
3648db9
to
21c6342
Compare
Thanks for the review achow101 Updated per feedback. Removed |
ACK 21c6342 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
21c6342
to
4be2c0d
Compare
Thanks for the feedback! Only modification is a variable rename |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost-ACK, changes look good to me. Just two suggestions below, one nit, one potential behaviour change.
4be2c0d
to
2e94d08
Compare
… functionality Mainly to not access 'm_address_book' externally.
Plus remove open bracket jump line
2e94d08
to
d69045e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK d69045e ✅
ACK d69045e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK d69045e
It's an improvement in code organization, as ideally only CWallet
would be aware of m_address_book
.
This field is still called in walletdb.cpp
and wallettool.cpp
, but may eventually be handled in another PR.
d69045e test: add coverage for 'listreceivedbyaddress' no change addrs return (furszy) 324f00a refactor: 'ListReceived' use optional for filtered address (furszy) b459fc1 refactor: RPC 'ListReceived', encapsulate m_address_book access (furszy) fa9f2ab refactor: RPC 'listlabels', encapsulate 'CWallet::ListAddrBookLabels' functionality (furszy) 83e42c4 refactor: use 'ForEachAddrBookEntry' in RPC 'getaddressesbylabel' (furszy) 2b48642 refactor: use ForEachAddrBookEntry in interfaces::getAddresses (furszy) 032842a wallet: implement ForEachAddrBookEntry method (furszy) 09649bc refactor: implement general 'ListAddrBookAddresses' for addressbook destinations lookup (furszy) 192eb1e refactor: getAddress don't access m_address_book, use FindAddressEntry function (furszy) Pull request description: ### Context The wallet's `m_address_book` field is being accessed directly from several places across the sources. ### Problem Code structure wise, we shouldn't be accessing it directly. It could end up being modified by mistake (from a place that has nothing to do with the wallet like an RPC command or the GUI) and cause a bigger issue: like an address book entry 'purpose' string change, which if done badly (from 'send' to 'receive'), could end up in a user sharing a "receive" address that he/she doesn't own. ### Solution Encapsulate `m_address_book` access inside the wallet. ------------------------------------------------------- Extra Note: This is the initial step towards decoupling the address book functionality from the wallet's sources. In other words, the creation of the `AddressBookManager` (which will be coming in a follow-up PR). ACKs for top commit: achow101: ACK d69045e theStack: ACK d69045e ✅ w0xlt: ACK bitcoin@d69045e Tree-SHA512: dba17acd86f171b4e9af0223bbbcad380048570f6a2f6a92732a51f01abe8806debaf65c9e9e5569fa76a541903cbb50adcb5f56ef77858151c698ae6b218e2a
@@ -114,7 +114,7 @@ class Wallet | |||
std::string* purpose) = 0; | |||
|
|||
//! Get wallet address list. | |||
virtual std::vector<WalletAddress> getAddresses() = 0; | |||
virtual std::vector<WalletAddress> getAddresses() const = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for future reference, virtual src/interfaces/
methods should not be const since it breaks multiprocess code in #10102, which adds alternate implementation of these interfaces which do IPC, and don't wrap local CWallets.
In general virtual
const
methods don't make sense unless you can guarantee that every override of the virtual method can const, which is not something you normally know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k 👍🏼, got it.
Context
The wallet's
m_address_book
field is being accessed directly from several places across the sources.Problem
Code structure wise, we shouldn't be accessing it directly. It could end up being modified by mistake (from a place that has nothing to do with the wallet like an RPC command or the GUI) and cause a bigger issue: like an address book entry 'purpose' string change, which if done badly (from 'send' to 'receive'), could end up in a user sharing a "receive" address that he/she doesn't own.
Solution
Encapsulate
m_address_book
access inside the wallet.Extra Note:
This is the initial step towards decoupling the address book functionality from the wallet's sources. In other words, the creation of the
AddressBookManager
(which will be coming in a follow-up PR).