Skip to content

Conversation

theStack
Copy link
Contributor

@theStack theStack commented Jul 20, 2022

This PR is a follow-up to #25218, as suggested in comment #25218 (comment). The interfaces of the methods ReserveDestination::GetReservedDestination, {Legacy,Descriptor,}ScriptPubKeyMan::GetReservedDestination are improved by returning util::Result<CTxDestination> instead of bool in order to get rid of the two CTxDestination& and bilingual_str& out-parameters.

@@ -179,7 +179,7 @@ class ScriptPubKeyMan
virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) { return false; }
virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; }

virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) { return false; }
virtual BResult<CTxDestination> GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool) { return {}; }
Copy link
Member

Choose a reason for hiding this comment

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

Because CTxDestination default value is CNoDestination, this will return a succeed BResult object.
Would be better to do:

return Untranslated("Not supported");

(same as we are doing for the base GetNewDestination)

Copy link
Member

Choose a reason for hiding this comment

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

I would have expected for {} to return an empty error message. Is there any way to avoid this confusion at compile time?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would have expected for {} to return an empty error message. Is there any way to avoid this confusion at compile time?

I think the silent conversion of bilingual_str into errors is an anti-feature. Not every translated string is necessarily an error string, and the conversion makes it not possible to use a BResult<bilingual_str> type that returns a normal translated string. #25665 improves this by adding an explicit error contructor

Copy link
Member

@furszy furszy Jul 21, 2022

Choose a reason for hiding this comment

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

I would have expected for {} to return an empty error message. Is there any way to avoid this confusion at compile time?

Yeah @MarcoFalke, my bad here, I shouldn't had reviewed the PR before sleep...

As we internally use an std::variant, and the first element is the bilingual_str. The {} will indeed initialize the variant with an empty error message.

What I tried to say on the message is that I don't see how empty error strings are useful. The caller cannot know where/why the function failed (which is the main purpose of returning an error string in the first place).

Thinking out loud without test it:
Probably we could prevent this behavior by removing the empty constructor or by asserting that an error string cannot be empty.

I think the silent conversion of bilingual_str into errors is an anti-feature. Not every translated string is necessarily an error string, and the conversion makes it not possible to use a BResult<bilingual_str> type that returns a normal translated string. #25665 improves this by adding an explicit error contructor

You know, the initial version that did for the generic CallResult was actually implemented in that way. I was using an OperationResult<T, E> ErrorOut(string) helper function instead of the silent conversion.

I have 25665 on my review list 👍🏼 . The last time that I checked it (brief check, thus why I haven't commented yet), I wasn't so sure about the internal std:.variant removal because every result was going to have the succeed object initialized AND the errors AND warnings in memory. And conceptually, a function can succeed OR fail. Not both.

But.. will comment there soon :). Need to finish up few other reviews first. Been focused on the nice, and long, #24914 lately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to return return Untranslated("Not supported"); for the sake of being consistent with other methods in the same class (GetNewDestination) right now, as suggested by furszy. The discussion here about general improvements to BResults seem to better fit to #25665, this PR is merely a refactor.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 21, 2022

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25616 (refactor: Return util::Result from WalletLoader methods by w0xlt)
  • #25526 (wallet: avoid double keypool TopUp() calls on descriptor wallets by furszy)
  • #25297 (wallet: speedup transactions sync, rescan and load by grouping all independent db writes on a single batched db transaction by furszy)

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.

@theStack
Copy link
Contributor Author

Force-pushed with changes as suggested by @furszy (#25656 (comment), #25656 (comment) (for consistency reasons), #25656 (comment)).

Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

Code review ACK 224e1e1

@theStack theStack changed the title refactor: wallet: return BResult from GetReservedDestination methods refactor: wallet: return util::Result from GetReservedDestination methods Aug 5, 2022
@theStack theStack force-pushed the 202207-refactor-wallet_use_bresult_for_getreserveddestination branch from 224e1e1 to 76b3c37 Compare August 5, 2022 15:20
@theStack
Copy link
Contributor Author

theStack commented Aug 5, 2022

Rebased on master, util::Result is used instead of BResult now accordingly. Also squashed the two commits into one as I think it's easier to review.

Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

ACK 76b3c37

@maflcko maflcko merged commit a6fc293 into bitcoin:master Aug 10, 2022
@theStack theStack deleted the 202207-refactor-wallet_use_bresult_for_getreserveddestination branch August 10, 2022 12:21
@jonatack
Copy link
Member

Post-merge ACK

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 10, 2022
@bitcoin bitcoin locked and limited conversation to collaborators Aug 13, 2023
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.

7 participants