Skip to content

Commit 76b3c37

Browse files
committed
refactor: wallet: return util::Result from GetReservedDestination methods
1 parent 006740b commit 76b3c37

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

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

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

580580
// 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
@@ -812,11 +812,13 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
812812
// Reserve a new key pair from key pool. If it fails, provide a dummy
813813
// destination in case we don't need change.
814814
CTxDestination dest;
815-
bilingual_str dest_err;
816-
if (!reservedest.GetReservedDestination(dest, true, dest_err)) {
817-
error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + dest_err;
815+
auto op_dest = reservedest.GetReservedDestination(true);
816+
if (!op_dest) {
817+
error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + util::ErrorString(op_dest);
818+
} else {
819+
dest = *op_dest;
820+
scriptChange = GetScriptForDestination(dest);
818821
}
819-
scriptChange = GetScriptForDestination(dest);
820822
// A valid destination implies a change script (and
821823
// vice-versa). An empty change script will abort later, if the
822824
// 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
@@ -2351,15 +2351,11 @@ util::Result<CTxDestination> CWallet::GetNewChangeDestination(const OutputType t
23512351
{
23522352
LOCK(cs_wallet);
23532353

2354-
CTxDestination dest;
2355-
bilingual_str error;
23562354
ReserveDestination reservedest(this, type);
2357-
if (!reservedest.GetReservedDestination(dest, true, error)) {
2358-
return util::Error{error};
2359-
}
2355+
auto op_dest = reservedest.GetReservedDestination(true);
2356+
if (op_dest) reservedest.KeepDestination();
23602357

2361-
reservedest.KeepDestination();
2362-
return dest;
2358+
return op_dest;
23632359
}
23642360

23652361
std::optional<int64_t> CWallet::GetOldestKeyPoolTime() const
@@ -2429,27 +2425,24 @@ std::set<std::string> CWallet::ListAddrBookLabels(const std::string& purpose) co
24292425
return label_set;
24302426
}
24312427

2432-
bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool internal, bilingual_str& error)
2428+
util::Result<CTxDestination> ReserveDestination::GetReservedDestination(bool internal)
24332429
{
24342430
m_spk_man = pwallet->GetScriptPubKeyMan(type, internal);
24352431
if (!m_spk_man) {
2436-
error = strprintf(_("Error: No %s addresses available."), FormatOutputType(type));
2437-
return false;
2432+
return util::Error{strprintf(_("Error: No %s addresses available."), FormatOutputType(type))};
24382433
}
24392434

2440-
24412435
if (nIndex == -1)
24422436
{
24432437
m_spk_man->TopUp();
24442438

24452439
CKeyPool keypool;
2446-
if (!m_spk_man->GetReservedDestination(type, internal, address, nIndex, keypool, error)) {
2447-
return false;
2448-
}
2440+
auto op_address = m_spk_man->GetReservedDestination(type, internal, nIndex, keypool);
2441+
if (!op_address) return op_address;
2442+
address = *op_address;
24492443
fInternal = keypool.fInternal;
24502444
}
2451-
dest = address;
2452-
return true;
2445+
return address;
24532446
}
24542447

24552448
void ReserveDestination::KeepDestination()

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class ReserveDestination
189189
}
190190

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

0 commit comments

Comments
 (0)