Skip to content

Refactoring: Clarify code using encrypted_batch in CWallet#14144

Closed
domob1812 wants to merge 2 commits into
bitcoin:masterfrom
domob1812:encrypted-batch
Closed

Refactoring: Clarify code using encrypted_batch in CWallet#14144
domob1812 wants to merge 2 commits into
bitcoin:masterfrom
domob1812:encrypted-batch

Conversation

@domob1812
Copy link
Copy Markdown
Contributor

@domob1812 domob1812 commented Sep 4, 2018

This is a pure refactoring (no functional changes) that clarifies the use of CWallet::encrypted_batch as well as the code using it according to my proposal in #14139. (A more detailed description and rationale can be found there.)

The change is split in two commits that correspond to logical units (matching the description in the proposal): A general cleanup of the code and the use of RAII (as also recommended in the developer guidelines) for setting/resetting encrypted_batch temporarily. In case the second commit is seen as overengineering, I'm happy to change the PR to include just the first commit.

The second commit (using RAII) obsoletes #14138.

@DrahtBot
Copy link
Copy Markdown
Contributor

DrahtBot commented Sep 4, 2018

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #17261 (Make ScriptPubKeyMan an actual interface and the wallet to have multiple by achow101)
  • #17260 (Split some CWallet functions into new LegacyScriptPubKeyMan by achow101)
  • #16546 ([WIP] External signer support - Wallet Box edition by Sjors)
  • #16528 ([WIP] Native Descriptor Wallets (take 2) by achow101)
  • #14942 (wallet: Make scan / abort status private to CWallet by Empact)

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.

@maflcko
Copy link
Copy Markdown
Member

maflcko commented Sep 20, 2018

Still need rebase, so closing for now. Let me know when you want to work on this again, so I can reopen.

@maflcko maflcko closed this Sep 20, 2018
@domob1812
Copy link
Copy Markdown
Contributor Author

Sorry, I was on vacation without proper internet access and am still travelling now (but with perhaps better options to work on this). So please reopen, or feel free to wait until I've rebased it (should happen this or next week at the latest) and reopen then.

@domob1812
Copy link
Copy Markdown
Contributor Author

Thanks for reopening, I've now rebased.

Comment thread src/wallet/wallet.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is set_encrypted_batch created in the case of encrypted_batch != nullptr?

Copy link
Copy Markdown
Contributor 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 a way to get the required construction/destruction points with RAII without having this variable (a local variable just inside the conditional block won't work as it will get destructed too soon). Note that the code as it is creates an empty std::unique_ptr unconditionally (here), and only creates the WithScopedValue object if encrypted_batch == nullptr. That gives us the desired RAII semantics.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for clarifying!

@domob1812
Copy link
Copy Markdown
Contributor Author

Rebased

@meshcollider
Copy link
Copy Markdown
Contributor

utACK 77a2ff4

Comment thread src/wallet/wallet.cpp Outdated
@domob1812
Copy link
Copy Markdown
Contributor Author

Rebased

@domob1812
Copy link
Copy Markdown
Contributor Author

Rebased

This is a pure refactor (no functional changes) that clarifies the usage
and code around CWallet::encrypted_batch.  For the rationale, see
bitcoin#14139 (comment).
Instead of explicitly resetting CWallet::encrypted_batch where it was
changed locally, use an RAII helper class to do it reliably.  This
ensures that the cleanup happens on all code paths, and corresponds to
the corresponding recommendation in the developer guidelines.

This implements the second part of the proposal made in
bitcoin#14139.
@domob1812
Copy link
Copy Markdown
Contributor Author

Rebased

@Sjors
Copy link
Copy Markdown
Member

Sjors commented Aug 15, 2019

@achow101 maybe rebase #16341 on this, so there's more clarity before the major box refactor?

@DrahtBot
Copy link
Copy Markdown
Contributor

Needs rebase

@domob1812
Copy link
Copy Markdown
Contributor Author

Since this has not been merged in over a year, it seems there's no interest. Thus I won't invest any more time trying to keep it rebased, especially with the recent refactoring. I'm still happy to revive this if there is clear interest (then please let me know).

@domob1812 domob1812 closed this Nov 14, 2019
@ryanofsky
Copy link
Copy Markdown
Contributor

This also overlaps with current PR #17369. WithScopedValue could help remove some of the cleanup code that PR adds to LegacyScriptPubKeyMan::Encrypt, which I think it would an improvement. And it might help with the descriptor wallets PR #15764 if there is encryption code there (I haven't looked).

I think more ideally, we could get rid of encrypted_batch member, and just add a generic batch or context member to SigningProvider methods, so the batch could just be passed around as a normal argument, instead of stored off to the side temporarily and checked for everywhere. But this would be a bigger change and might not be worth it.

@ryanofsky
Copy link
Copy Markdown
Contributor

To summarize comment above, it's a concept ACK for this PR and extending it to current and future signing providers, since I think it's an improvement even though a more comprehensive solution may be possible.

Can't say whether the PR should be reopened though. The biggest challenge for wallet code is always getting review, and there is a lot of related code still needing review https://github.com/bitcoin/bitcoin/projects/12

@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.

9 participants