Skip to content

Commit a6fc293

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25656: refactor: wallet: return util::Result from GetReservedDestination methods
76b3c37 refactor: wallet: return util::Result from `GetReservedDestination` methods (Sebastian Falbesoner) Pull request description: This PR is a follow-up to #25218, as suggested in comment bitcoin/bitcoin#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. ACKs for top commit: furszy: ACK 76b3c37 Tree-SHA512: bf15560a88d645bcf8768024013d36012cd65caaa4a613e8a055dfd8f29cb4a219c19084606992bad177920cdca3a732ec168e9b9526f9295491f2cf79cc6815
2 parents aac2008 + 76b3c37 commit a6fc293

File tree

5 files changed

+26
-40
lines changed

5 files changed

+26
-40
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -292,26 +292,22 @@ bool LegacyScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBat
292292
return true;
293293
}
294294

295-
bool LegacyScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error)
295+
util::Result<CTxDestination> LegacyScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool)
296296
{
297297
if (LEGACY_OUTPUT_TYPES.count(type) == 0) {
298-
error = _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");
299-
return false;
298+
return util::Error{_("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types")};
300299
}
301300
assert(type != OutputType::BECH32M);
302301

303302
LOCK(cs_KeyStore);
304303
if (!CanGetAddresses(internal)) {
305-
error = _("Error: Keypool ran out, please call keypoolrefill first");
306-
return false;
304+
return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
307305
}
308306

309307
if (!ReserveKeyFromKeyPool(index, keypool, internal)) {
310-
error = _("Error: Keypool ran out, please call keypoolrefill first");
311-
return false;
308+
return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
312309
}
313-
address = GetDestinationForKey(keypool.vchPubKey, type);
314-
return true;
310+
return GetDestinationForKey(keypool.vchPubKey, type);
315311
}
316312

317313
bool LegacyScriptPubKeyMan::TopUpInactiveHDChain(const CKeyID seed_id, int64_t index, bool internal)
@@ -1760,17 +1756,12 @@ bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, Walle
17601756
return true;
17611757
}
17621758

1763-
bool DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error)
1759+
util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool)
17641760
{
17651761
LOCK(cs_desc_man);
17661762
auto op_dest = GetNewDestination(type);
17671763
index = m_wallet_descriptor.next_index - 1;
1768-
if (op_dest) {
1769-
address = *op_dest;
1770-
} else {
1771-
error = util::ErrorString(op_dest);
1772-
}
1773-
return bool(op_dest);
1764+
return op_dest;
17741765
}
17751766

17761767
void DescriptorScriptPubKeyMan::ReturnDestination(int64_t index, bool internal, const CTxDestination& addr)

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class ScriptPubKeyMan
179179
virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) { return false; }
180180
virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; }
181181

182-
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) { return false; }
182+
virtual util::Result<CTxDestination> GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool) { return util::Error{Untranslated("Not supported")}; }
183183
virtual void KeepDestination(int64_t index, const OutputType& type) {}
184184
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) {}
185185

@@ -366,7 +366,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
366366
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
367367
bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
368368

369-
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) override;
369+
util::Result<CTxDestination> GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool) override;
370370
void KeepDestination(int64_t index, const OutputType& type) override;
371371
void ReturnDestination(int64_t index, bool internal, const CTxDestination&) override;
372372

@@ -576,7 +576,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
576576
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
577577
bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
578578

579-
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) override;
579+
util::Result<CTxDestination> GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool) override;
580580
void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) override;
581581

582582
// Tops up the descriptor cache and m_map_script_pub_keys. The cache is stored in the wallet file

src/wallet/spend.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,13 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
814814
// Reserve a new key pair from key pool. If it fails, provide a dummy
815815
// destination in case we don't need change.
816816
CTxDestination dest;
817-
bilingual_str dest_err;
818-
if (!reservedest.GetReservedDestination(dest, true, dest_err)) {
819-
error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + dest_err;
817+
auto op_dest = reservedest.GetReservedDestination(true);
818+
if (!op_dest) {
819+
error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + util::ErrorString(op_dest);
820+
} else {
821+
dest = *op_dest;
822+
scriptChange = GetScriptForDestination(dest);
820823
}
821-
scriptChange = GetScriptForDestination(dest);
822824
// A valid destination implies a change script (and
823825
// vice-versa). An empty change script will abort later, if the
824826
// change keypool ran out, but change is required.

src/wallet/wallet.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,15 +2359,11 @@ util::Result<CTxDestination> CWallet::GetNewChangeDestination(const OutputType t
23592359
{
23602360
LOCK(cs_wallet);
23612361

2362-
CTxDestination dest;
2363-
bilingual_str error;
23642362
ReserveDestination reservedest(this, type);
2365-
if (!reservedest.GetReservedDestination(dest, true, error)) {
2366-
return util::Error{error};
2367-
}
2363+
auto op_dest = reservedest.GetReservedDestination(true);
2364+
if (op_dest) reservedest.KeepDestination();
23682365

2369-
reservedest.KeepDestination();
2370-
return dest;
2366+
return op_dest;
23712367
}
23722368

23732369
std::optional<int64_t> CWallet::GetOldestKeyPoolTime() const
@@ -2437,27 +2433,24 @@ std::set<std::string> CWallet::ListAddrBookLabels(const std::string& purpose) co
24372433
return label_set;
24382434
}
24392435

2440-
bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool internal, bilingual_str& error)
2436+
util::Result<CTxDestination> ReserveDestination::GetReservedDestination(bool internal)
24412437
{
24422438
m_spk_man = pwallet->GetScriptPubKeyMan(type, internal);
24432439
if (!m_spk_man) {
2444-
error = strprintf(_("Error: No %s addresses available."), FormatOutputType(type));
2445-
return false;
2440+
return util::Error{strprintf(_("Error: No %s addresses available."), FormatOutputType(type))};
24462441
}
24472442

2448-
24492443
if (nIndex == -1)
24502444
{
24512445
m_spk_man->TopUp();
24522446

24532447
CKeyPool keypool;
2454-
if (!m_spk_man->GetReservedDestination(type, internal, address, nIndex, keypool, error)) {
2455-
return false;
2456-
}
2448+
auto op_address = m_spk_man->GetReservedDestination(type, internal, nIndex, keypool);
2449+
if (!op_address) return op_address;
2450+
address = *op_address;
24572451
fInternal = keypool.fInternal;
24582452
}
2459-
dest = address;
2460-
return true;
2453+
return address;
24612454
}
24622455

24632456
void ReserveDestination::KeepDestination()

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class ReserveDestination
191191
}
192192

193193
//! Reserve an address
194-
bool GetReservedDestination(CTxDestination& pubkey, bool internal, bilingual_str& error);
194+
util::Result<CTxDestination> GetReservedDestination(bool internal);
195195
//! Return reserved address
196196
void ReturnDestination();
197197
//! Keep the address. Do not return it's key to the keypool when this object goes out of scope

0 commit comments

Comments
 (0)