Skip to content

Commit bd897fa

Browse files
committed
merge bitcoin#25656: return util::Result from GetReservedDestination methods
1 parent 56accfe commit bd897fa

File tree

8 files changed

+37
-50
lines changed

8 files changed

+37
-50
lines changed

src/coinjoin/client.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,12 +1575,10 @@ bool CCoinJoinClientSession::CreateCollateralTransaction(CMutableTransaction& tx
15751575
if (txout.nValue >= CoinJoin::GetCollateralAmount() * 2) {
15761576
// make our change address
15771577
CScript scriptChange;
1578-
CTxDestination dest;
1579-
bilingual_str error;
15801578
ReserveDestination reserveDest(m_wallet.get());
1581-
bool success = reserveDest.GetReservedDestination(dest, true, error);
1582-
assert(success); // should never fail, as we just unlocked
1583-
scriptChange = GetScriptForDestination(dest);
1579+
auto dest_opt = reserveDest.GetReservedDestination(true);
1580+
assert(dest_opt); // should never fail, as we just unlocked
1581+
scriptChange = GetScriptForDestination(*dest_opt);
15841582
reserveDest.KeepDestination();
15851583
// return change
15861584
txCollateral.vout.emplace_back(txout.nValue - CoinJoin::GetCollateralAmount(), scriptChange);

src/coinjoin/util.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ inline unsigned int GetSizeOfCompactSizeDiff(uint64_t nSizePrev, uint64_t nSizeN
2929
CKeyHolder::CKeyHolder(CWallet* pwallet) :
3030
reserveDestination(pwallet)
3131
{
32-
bilingual_str error;
33-
reserveDestination.GetReservedDestination(dest, false, error);
32+
auto dest_opt = reserveDestination.GetReservedDestination(false);
33+
assert(dest_opt);
34+
dest = *dest_opt;
3435
}
3536

3637
void CKeyHolder::KeepKey()
@@ -100,11 +101,10 @@ CTransactionBuilderOutput::CTransactionBuilderOutput(CTransactionBuilder* pTxBui
100101
nAmount(nAmountIn)
101102
{
102103
assert(pTxBuilder);
103-
CTxDestination txdest;
104-
bilingual_str error;
105104
LOCK(wallet.cs_wallet);
106-
dest.GetReservedDestination(txdest, false, error);
107-
script = ::GetScriptForDestination(txdest);
105+
auto dest_opt = dest.GetReservedDestination(false);
106+
assert(dest_opt);
107+
script = ::GetScriptForDestination(*dest_opt);
108108
}
109109

110110
bool CTransactionBuilderOutput::UpdateAmount(const CAmount nNewAmount)

src/wallet/scriptpubkeyman.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,18 @@ bool LegacyScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBat
288288
return true;
289289
}
290290

291-
bool LegacyScriptPubKeyMan::GetReservedDestination(bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error)
291+
util::Result<CTxDestination> LegacyScriptPubKeyMan::GetReservedDestination(bool internal, int64_t& index, CKeyPool& keypool)
292292
{
293293
LOCK(cs_KeyStore);
294294
if (!CanGetAddresses(internal)) {
295-
error = _("Error: Keypool ran out, please call keypoolrefill first");
296-
return false;
295+
return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
297296
}
298297

299298
if (!ReserveKeyFromKeyPool(index, keypool, internal)) {
300-
error = _("Error: Keypool ran out, please call keypoolrefill first");
301-
return false;
299+
return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
302300
}
303301
// TODO: unify with bitcoin and use here GetDestinationForKey even if we have no type
304-
address = PKHash(keypool.vchPubKey);
305-
return true;
302+
return CTxDestination(PKHash(keypool.vchPubKey));
306303
}
307304

308305
void LegacyScriptPubKeyMan::MarkUnusedAddresses(WalletBatch &batch, const CScript& script, const std::optional<int64_t>& block_time)
@@ -1905,17 +1902,12 @@ bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, Walle
19051902
return true;
19061903
}
19071904

1908-
bool DescriptorScriptPubKeyMan::GetReservedDestination(bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error)
1905+
util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetReservedDestination(bool internal, int64_t& index, CKeyPool& keypool)
19091906
{
19101907
LOCK(cs_desc_man);
19111908
auto op_dest = GetNewDestination();
19121909
index = m_wallet_descriptor.next_index - 1;
1913-
if (op_dest) {
1914-
address = *op_dest;
1915-
} else {
1916-
error = util::ErrorString(op_dest);
1917-
}
1918-
return bool(op_dest);
1910+
return op_dest;
19191911
}
19201912

19211913
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
@@ -171,7 +171,7 @@ class ScriptPubKeyMan
171171
virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) { return false; }
172172
virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; }
173173

174-
virtual bool GetReservedDestination(bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) { return false; }
174+
virtual util::Result<CTxDestination> GetReservedDestination(bool internal, int64_t& index, CKeyPool& keypool) { return util::Error{Untranslated("Not supported")}; }
175175
virtual void KeepDestination(int64_t index) {}
176176
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) {}
177177

@@ -325,7 +325,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
325325
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
326326
bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
327327

328-
bool GetReservedDestination(bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) override;
328+
util::Result<CTxDestination> GetReservedDestination(bool internal, int64_t& index, CKeyPool& keypool) override;
329329
void KeepDestination(int64_t index) override;
330330
void ReturnDestination(int64_t index, bool internal, const CTxDestination&) override;
331331

@@ -561,7 +561,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
561561
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
562562
bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
563563

564-
bool GetReservedDestination(bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) override;
564+
util::Result<CTxDestination> GetReservedDestination(bool internal, int64_t& index, CKeyPool& keypool) override;
565565
void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) override;
566566

567567
// 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
@@ -811,11 +811,13 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
811811
// Reserve a new key pair from key pool. If it fails, provide a dummy
812812
// destination in case we don't need change.
813813
CTxDestination dest;
814-
bilingual_str dest_err;
815-
if (!reservedest.GetReservedDestination(dest, true, dest_err)) {
816-
error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + dest_err;
814+
auto op_dest = reservedest.GetReservedDestination(true);
815+
if (!op_dest) {
816+
error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + util::ErrorString(op_dest);
817+
} else {
818+
dest = *op_dest;
819+
scriptChange = GetScriptForDestination(dest);
817820
}
818-
scriptChange = GetScriptForDestination(dest);
819821
// A valid destination implies a change script (and
820822
// vice-versa). An empty change script will abort later, if the
821823
// change keypool ran out, but change is required.

src/wallet/test/coinjoin_tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,13 @@ class CTransactionBuilderTestSetup : public TestChain100Setup
183183
CompactTallyItem tallyItem;
184184
ReserveDestination reserveDest(wallet.get());
185185
int nChangePosRet{RANDOM_CHANGE_POSITION};
186-
bilingual_str strError;
187186
CCoinControl coinControl;
188187
coinControl.m_feerate = CFeeRate(1000);
189188
{
190189
LOCK(wallet->cs_wallet);
191-
BOOST_CHECK(reserveDest.GetReservedDestination(tallyItem.txdest, false, strError));
190+
auto dest_opt = reserveDest.GetReservedDestination(false);
191+
BOOST_CHECK(dest_opt);
192+
tallyItem.txdest = *dest_opt;
192193
}
193194
for (CAmount nAmount : vecAmounts) {
194195
CTransactionRef tx;

src/wallet/wallet.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,15 +2366,11 @@ util::Result<CTxDestination> CWallet::GetNewChangeDestination()
23662366
{
23672367
LOCK(cs_wallet);
23682368

2369-
CTxDestination dest;
2370-
bilingual_str error;
23712369
ReserveDestination reservedest(this);
2372-
if (!reservedest.GetReservedDestination(dest, true, error)) {
2373-
return util::Error{error};
2374-
}
2370+
auto op_dest = reservedest.GetReservedDestination(true);
2371+
if (op_dest) reservedest.KeepDestination();
23752372

2376-
reservedest.KeepDestination();
2377-
return dest;
2373+
return op_dest;
23782374
}
23792375

23802376
std::optional<int64_t> CWallet::GetOldestKeyPoolTime() const
@@ -2443,12 +2439,11 @@ std::set<std::string> CWallet::ListAddrBookLabels(const std::string& purpose) co
24432439
return label_set;
24442440
}
24452441

2446-
bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool fInternalIn, bilingual_str& error)
2442+
util::Result<CTxDestination> ReserveDestination::GetReservedDestination(bool fInternalIn)
24472443
{
24482444
m_spk_man = pwallet->GetScriptPubKeyMan(fInternalIn);
24492445
if (!m_spk_man) {
2450-
error = _("Error: No addresses available.");
2451-
return false;
2446+
return util::Error{_("Error: No addresses available.")};
24522447
}
24532448

24542449
if (nIndex == -1)
@@ -2457,14 +2452,13 @@ bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool fInte
24572452

24582453
CKeyPool keypool;
24592454
int64_t index;
2460-
if (!m_spk_man->GetReservedDestination(fInternalIn, address, index, keypool, error)) {
2461-
return false;
2462-
}
2455+
auto op_address = m_spk_man->GetReservedDestination(fInternalIn, index, keypool);
2456+
if (!op_address) return op_address;
2457+
address = *op_address;
24632458
nIndex = index;
24642459
fInternal = keypool.fInternal;
24652460
}
2466-
dest = address;
2467-
return true;
2461+
return address;
24682462
}
24692463

24702464
void ReserveDestination::KeepDestination()

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class ReserveDestination
203203
}
204204

205205
//! Reserve an address
206-
bool GetReservedDestination(CTxDestination& pubkey, bool internal, bilingual_str& error);
206+
util::Result<CTxDestination> GetReservedDestination(bool internal);
207207
//! Return reserved address
208208
void ReturnDestination();
209209
//! Keep the address. Do not return its key to the keypool when this object goes out of scope

0 commit comments

Comments
 (0)