Skip to content
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

Bugfix: Wallet: Safely deal with change in the address book [part 2] #18546

Merged
merged 3 commits into from
Apr 7, 2020

Conversation

luke-jr
Copy link
Member

@luke-jr luke-jr commented Apr 6, 2020

Follow-up to #18192, not strictly necessary for 0.20

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK, just some style nits


typedef std::map<std::string, std::string> StringMap;
StringMap destdata;

bool IsChange() const { return m_change; }
const std::string& GetLabel() const { return m_label; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const std::string& GetLabel() const { return m_label; }
std::string GetLabel() const { return m_label; }

To avoid issues related to #17198

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how that relates to this. Can you give an example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the general idea that:

  • This doesn't improve performance
  • This might lead to use-after-free in future code

@luke-jr luke-jr force-pushed the bugfix_addressbook_change branch 2 times, most recently from 1ce3245 to a7051b0 Compare April 6, 2020 20:15
@maflcko
Copy link
Member

maflcko commented Apr 6, 2020

ACK a7051b0, only change is adding issue url and a test style fixup 🍖

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK a7051b046a, only change is adding issue url and a test style fixup 🍖
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjnBgwAgCMSg9b7BKGlOp9Lhs+tyN4elzNwljsK69gBnI+0djGfCCWnQ7mw73E+
LhHEVypuQwLFMy5XgEUx5J1yKrfC9M8Z+wuT8GuVOla86XEYsZ5rVRmTIGtfmwwi
TtkxrAT0sg6O/u7vE6DLnTpEdMsd37B/j7t++dmdWjdh5tANWONHUrtL7+G1dnTu
AFVKHFmlacPJc8mVfzqrLjbDKjquFlBVAGf2fxrzjxseJGybsanr9Y+ITiIeJ4vM
ba/yYXagM25/RCsd2GMWSiADpOIHjwQ2GXkSFaFevk1K5ySbDmueA/yjukbhW5s7
NMasUWQgU4wqzZanOiXtZO6M2wxmmXQcsWvBNt7loy1XceVV2m3GkL+JgcjhwNmm
Wjk0OwS2DBKMPrGeU6en28TfgylbfyRy8Rglsmnqx52jGreaFuXGR6wdKmd5N/lm
7FcH/T0a6BVCGlW3/h+6gEXo8oAqDOCeHxoMT+oa+zg4JO6TXyNXsF4HpVolZlqR
adBcWCOK
=sHmd
-----END PGP SIGNATURE-----

Timestamp of file with hash 20931cd3fd37cc3e39e8dda70e27cd4ebf620a0462cabe7b49ab467c15609ce5 -

@luke-jr luke-jr force-pushed the bugfix_addressbook_change branch from a7051b0 to 7a2ecf1 Compare April 6, 2020 20:52
@maflcko
Copy link
Member

maflcko commented Apr 6, 2020

re-ACK 7a2ecf1, only change is adding an assert_equal in the test 🔰

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

re-ACK 7a2ecf16df, only change is adding an assert_equal in the test 🔰
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUhbPAwAnxp3dHSnOH/9xKxdNaamDfRFxNnBiwE/BbHRVU79mP3Lned6OILzDgEm
ESHtX5jbEI/Ot0ZgVy2KNKU8inVVIsuX3CLo2FyOduzijpesTDCqPBxxPZZW8qqP
ieUdoe02jXwrmlAiZTCQOph6XEQv2efI5TyQYAJk5zuFW+w6+si3VycbEbvyuc3u
nV9gd/+0fr0TQQ6+C0HtRpHxNgUsYjPjVuPuEddt/qz4ZyO9oGyRdVl+rpV2pcOA
bkulHqz8VN9WAAms0L2PQ3/3ZAAw7lOmTfUNqJPYd2QjbQAXC8G61eXZ6cqJLS9t
9bFYlgxyYCv/Ok7Y4rBZWFGoAUlplH79U9CSU8q5IABqTTfaqDSnhOgbS8ZL9xmi
GXx9TVSvk05z1J1ACjB+Aj/17rev+TWZyzUQE9N8YXvzXtH4I6F9YrJflvfyIX+a
34rKjjh3PAYBCBdxzV1OiKzQZtLjkQCua9i1KZL4NZy8t2DY029trTgyLt2rE/HL
qrTWKMSm
=d7Nv
-----END PGP SIGNATURE-----

Timestamp of file with hash 6dc1f2ac8bbd8dde3e44f0db23fee8262cd7abfa5430c2159ed49a2ea0c0d643 -

@DrahtBot
Copy link
Contributor

DrahtBot commented Apr 7, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, 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.

@jnewbery
Copy link
Contributor

jnewbery commented Apr 7, 2020

utACK 7a2ecf1

@luke-jr
Copy link
Member Author

luke-jr commented Apr 7, 2020

Looks like #18192 actually introduced a bug with its name-reference hack: the copy constructor copied the reference to the original object's m_label.

2952c46 is effectively a bugfix for that.

If we don't merge this in 0.20, we need to add something like:

CAddressBookData(const CAddressBookData& other) : m_change(other.m_change), m_label(other.m_label), name(m_label), purpose(other.purpose) {}

(This bug is why #18550 is failing CI)

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 7a2ecf1

reset_balance(node, node.getnewaddress())

# It should still be change
assert node.getaddressinfo(changeaddr)['ischange']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test duly fails at this assertion before the changes in #18192.

jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 8, 2020
…book

Summary:
b5795a788639305bab86a8b3f6b75d6ce81be083 Wallet: Add warning comments and assert to CWallet::DelAddressBook (Luke Dashjr)
6d2905f57aaeb3ec3b63d31043f7673ca10003f2 Wallet: Avoid unnecessary/redundant m_address_book lookups (Luke Dashjr)
c751d886f499257627b308b11ffaa51c22db6cc0 Wallet: Avoid treating change-in-the-addressbook as non-change everywhere (Luke Dashjr)
8e64b8c84bcbd63caea06f3af087af1f0609eaf5 Wallet: New FindAddressBookEntry method to filter out change entries (and skip ->second everywhere) (Luke Dashjr)
65b6bdc2b164343ec3cc3d32a0297daff9e24fec Wallet: Add CAddressBookData::IsChange which returns true iff label has never been set (Luke Dashjr)
144b2f85da4d51bf7d72b987888ddcaf5b429eed Wallet: Require usage of new CAddressBookData::setLabel to change label (Luke Dashjr)
b86cd155f6f661052042048aa7cfc2a397afe4f7 scripted-diff: Wallet: Rename mapAddressBook to m_address_book (Luke Dashjr)

Pull request description:
  In many places, our code assumes that presence in the address book indicates a non-change key, and absence of an entry in mapAddressBook indicates change.

  This no longer holds true after #13756 (first released in 0.19) since it added a "used" DestData populated even for change addresses. Only avoid-reuse wallets should be affected by this issue.

  Thankfully, populating DestData does not write a label to the database, so we can retroactively fix this (so long as the user didn't see the change address and manually assign it a real label).

  Fixing it is accomplished by:

  * Adding a new bool to CAddressBookData to track if the label has ever been assigned, either by loading one from the database, or by assigning one at runtime.
  * `CAddressBookData::IsChange` and `CWallet::FindAddressBookEntry` are new methods to assist in excluding change from code that doesn't expect to see them.
  * For safety in merging, `CAddressBookData::name` has been made read-only (the actual data is stored in `m_label`, a new private member, and can be changed only with `setLabel` which updates the `m_change` flag), and `mapAddressBook` has been renamed to `m_address_book` (to force old code to be rebased to compile).

  A final commit also does some minor optimisation, avoiding redundant lookups in `m_address_book` when we already have a pointer to the `CAddressBookData`.

Backport of Core [[bitcoin/bitcoin#18192 | PR18192]]

Tests for this change to follow in [[bitcoin/bitcoin#18546 | PR18546]]

Test Plan: `ninja check check-functional` for sanity

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7810
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 8, 2020
…book [part 2]

Summary:
7a2ecf16df938dd95d3130a46082def7a02338eb Wallet: Change IsMine check in CWallet::DelAddressBook from assert to failure (Luke Dashjr)
2952c46b923042f2de801f319e03ed5c4c4eb735 Wallet: Replace CAddressBookData.name with GetLabel() method (Luke Dashjr)
d7092c392e10889cd7a080b3d22ed6446a59b87a QA: Test that change doesn't turn into non-change when spent in an avoid-reuse wallet (Luke Dashjr)

Pull request description:

  Follow-up to #18192, not strictly necessary for 0.20

Depends on D7810 and introduces tests for it too

Backport of Core [[bitcoin/bitcoin#18546 | PR18546]]

Test Plan: `ninja check check-functional`

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D7811
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants