This PR is a follow-up to #25218, as suggested in comment #25218 (review). 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.
refactor: wallet: return util::Result from `GetReservedDestination` methods #25656
pull theStack wants to merge 1 commits into bitcoin:master from theStack:202207-refactor-wallet_use_bresult_for_getreserveddestination changing 5 files +26 −40-
theStack commented at 10:16 PM on July 20, 2022: contributor
- fanquake added the label Refactoring on Jul 20, 2022
-
in src/wallet/spend.cpp:715 in d7c37ed989 outdated
713 | + if (op_dest.HasRes()) { 714 | + dest = op_dest.ReleaseObj(); 715 | + } else { 716 | + error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + op_dest.GetError(); 717 | } 718 | scriptChange = GetScriptForDestination(dest);
furszy commented at 10:26 PM on July 20, 2022:Can remove the bottom
scriptChange = GetScriptForDestination(dest);call ifGetReservedDestinationfailed (no valid destination).auto op_dest = reservedest.GetReservedDestination(true); if (!op_dest) { error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + op_dest.GetError(); } else { scriptChange = GetScriptForDestination(op_dest.GetObj()); }
theStack commented at 8:00 PM on July 21, 2022:Thanks, done. Scrictly speaking moving the scriptChange assignment into the conditional is a bit more than a refactor (it could have been done also before the BResult change), but it also makes sense to not unnecessarily call
GetScriptForDestinationin the failure case.in src/wallet/scriptpubkeyman.h:182 in d7c37ed989 outdated
178 | @@ -179,7 +179,7 @@ class ScriptPubKeyMan 179 | virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) { return false; } 180 | virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; } 181 | 182 | - virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) { return false; } 183 | + virtual BResult<CTxDestination> GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool) { return {}; }
furszy commented at 10:30 PM on July 20, 2022:Because CTxDestination default value is
CNoDestination, this will return a succeedBResultobject. Would be better to do:return Untranslated("Not supported");(same as we are doing for the base
GetNewDestination)
MarcoFalke commented at 5:30 AM on July 21, 2022:I would have expected for
{}to return an empty error message. Is there any way to avoid this confusion at compile time?
ryanofsky commented at 1:01 PM on July 21, 2022: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
furszy commented at 2:01 PM on July 21, 2022: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 thebilingual_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
CallResultwas actually implemented in that way. I was using anOperationResult<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.
theStack commented at 8:03 PM on July 21, 2022: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.in src/wallet/wallet.cpp:2443 in d7c37ed989 outdated
2442 | + if (op_address.HasRes()) { 2443 | + address = op_address.ReleaseObj(); 2444 | + } else { 2445 | + return op_address.GetError(); 2446 | } 2447 | fInternal = keypool.fInternal;
furszy commented at 10:37 PM on July 20, 2022:What about this:
const auto& op_address = m_spk_man->GetReservedDestination(type, internal, nIndex, keypool); if (!op_address) return op_address; fInternal = keypool.fInternal; address = op_address.ReleaseObj();
theStack commented at 8:03 PM on July 21, 2022:Thanks, done.
DrahtBot commented at 6:47 AM on July 21, 2022: contributor<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--174a7506f384e20aa4161008e828411d-->
Conflicts
Reviewers, this pull request conflicts with the following ones:
- #25616 (refactor: Return
util::Resultfrom 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 force-pushed on Jul 21, 2022theStack commented at 8:05 PM on July 21, 2022: contributorForce-pushed with changes as suggested by @furszy (https://github.com/bitcoin/bitcoin/pull/25656#discussion_r926102164, #25656 (review) (for consistency reasons), #25656 (review)).
furszy approvedfurszy commented at 1:18 PM on July 22, 2022: memberCode review ACK 224e1e1f
DrahtBot added the label Needs rebase on Aug 5, 2022refactor: wallet: return util::Result from `GetReservedDestination` methods 76b3c37fcbtheStack renamed this:refactor: wallet: return BResult from `GetReservedDestination` methods
refactor: wallet: return util::Result from `GetReservedDestination` methods
on Aug 5, 2022theStack force-pushed on Aug 5, 2022theStack commented at 3:21 PM on August 5, 2022: contributorRebased on master,
util::Resultis used instead ofBResultnow accordingly. Also squashed the two commits into one as I think it's easier to review.DrahtBot removed the label Needs rebase on Aug 5, 2022furszy approvedfurszy commented at 1:39 PM on August 8, 2022: memberACK 76b3c37f
MarcoFalke merged this on Aug 10, 2022MarcoFalke closed this on Aug 10, 2022theStack deleted the branch on Aug 10, 2022jonatack commented at 3:57 PM on August 10, 2022: contributorPost-merge ACK
sidhujag referenced this in commit e80a59057a on Aug 10, 2022bitcoin locked this on Aug 13, 2023
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 21:13 UTC
More mirrored repositories can be found on mirror.b10c.me