Skip to content

Commit 9e23b11

Browse files
committed
merge bitcoin#25218: introduce generic 'Result' class and connect it to CreateTransaction and GetNewDestination
excludes: - 7a45c33 continuation of 9f5845c from dash#6733
1 parent ec764fd commit 9e23b11

21 files changed

+202
-243
lines changed

src/bench/wallet_loading.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ static void BenchUnloadWallet(std::shared_ptr<CWallet>&& wallet)
4747

4848
static void AddTx(CWallet& wallet)
4949
{
50-
bilingual_str error;
51-
CTxDestination dest;
52-
wallet.GetNewDestination("", dest, error);
50+
const auto& dest = wallet.GetNewDestination("");
51+
assert(dest.HasRes());
5352

5453
CMutableTransaction mtx;
55-
mtx.vout.push_back({COIN, GetScriptForDestination(dest)});
54+
mtx.vout.push_back({COIN, GetScriptForDestination(dest.GetObj())});
5655
mtx.vin.push_back(CTxIn());
5756

5857
wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});

src/coinjoin/util.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,13 @@ bool CTransactionBuilder::Commit(bilingual_str& strResult)
282282
CTransactionRef tx;
283283
{
284284
LOCK2(m_wallet.cs_wallet, ::cs_main);
285-
FeeCalculation fee_calc_out;
286-
if (auto txr = wallet::CreateTransaction(m_wallet, vecSend, nChangePosRet, strResult, coinControl, fee_calc_out)) {
287-
tx = txr->tx;
288-
nFeeRet = txr->fee;
289-
nChangePosRet = txr->change_pos;
285+
auto ret = wallet::CreateTransaction(m_wallet, vecSend, nChangePosRet, coinControl);
286+
if (ret) {
287+
tx = ret.GetObj().tx;
288+
nFeeRet = ret.GetObj().fee;
289+
nChangePosRet = ret.GetObj().change_pos;
290290
} else {
291+
strResult = ret.GetError();
291292
return false;
292293
}
293294
}

src/interfaces/wallet.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Wallet
112112
virtual std::string getWalletName() = 0;
113113

114114
// Get a new address.
115-
virtual bool getNewDestination(const std::string label, CTxDestination& dest) = 0;
115+
virtual BResult<CTxDestination> getNewDestination(const std::string label) = 0;
116116

117117
//! Get public key.
118118
virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
@@ -167,12 +167,11 @@ class Wallet
167167
virtual std::vector<COutPoint> listProTxCoins() = 0;
168168

169169
//! Create transaction.
170-
virtual CTransactionRef createTransaction(const std::vector<wallet::CRecipient>& recipients,
170+
virtual BResult<CTransactionRef> createTransaction(const std::vector<wallet::CRecipient>& recipients,
171171
const wallet::CCoinControl& coin_control,
172172
bool sign,
173173
int& change_pos,
174-
CAmount& fee,
175-
bilingual_str& fail_reason) = 0;
174+
CAmount& fee) = 0;
176175

177176
//! Commit transaction.
178177
virtual void commitTransaction(CTransactionRef tx,

src/qt/addresstablemodel.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,23 +366,21 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
366366
else if(type == Receive)
367367
{
368368
// Generate a new address to associate with given label
369-
CTxDestination dest;
370-
if(!walletModel->wallet().getNewDestination(strLabel, dest))
371-
{
369+
auto op_dest = walletModel->wallet().getNewDestination(strLabel);
370+
if (!op_dest) {
372371
WalletModel::UnlockContext ctx(walletModel->requestUnlock());
373-
if(!ctx.isValid())
374-
{
372+
if (!ctx.isValid()) {
375373
// Unlock wallet failed or was cancelled
376374
editStatus = WALLET_UNLOCK_FAILURE;
377375
return QString();
378376
}
379-
if(!walletModel->wallet().getNewDestination(strLabel, dest))
380-
{
377+
op_dest = walletModel->wallet().getNewDestination(strLabel);
378+
if (!op_dest) {
381379
editStatus = KEY_GENERATION_FAILURE;
382380
return QString();
383381
}
384382
}
385-
strAddress = EncodeDestination(dest);
383+
strAddress = EncodeDestination(op_dest.GetObj());
386384
}
387385
else
388386
{

src/qt/walletmodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
251251
}
252252

253253
CAmount nFeeRequired = 0;
254-
bilingual_str error;
255254
int nChangePosRet = -1;
256255

257256
auto& newTx = transaction.getWtx();
258-
newTx = m_wallet->createTransaction(vecSend, coinControl, !wallet().privateKeysDisabled() /* sign */, nChangePosRet, nFeeRequired, error);
257+
const auto& res = m_wallet->createTransaction(vecSend, coinControl, !wallet().privateKeysDisabled() /* sign */, nChangePosRet, nFeeRequired);
258+
newTx = res ? res.GetObj() : nullptr;
259259
transaction.setTransactionFee(nFeeRequired);
260260
if (fSubtractFeeFromAmount && newTx)
261261
transaction.reassignAmounts(nChangePosRet);
@@ -266,7 +266,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
266266
{
267267
return SendCoinsReturn(AmountWithFeeExceedsBalance);
268268
}
269-
Q_EMIT message(tr("Send Coins"), QString::fromStdString(error.translated),
269+
Q_EMIT message(tr("Send Coins"), QString::fromStdString(res.GetError().translated),
270270
CClientUIInterface::MSG_ERROR);
271271
return TransactionCreationFailed;
272272
}

src/rpc/evo.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,12 @@ static void FundSpecialTx(CWallet& wallet, CMutableTransaction& tx, const Specia
281281
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("No funds at specified address %s", EncodeDestination(fundDest)));
282282
}
283283

284-
bilingual_str strFailReason;
285-
FeeCalculation fee_calc_out;
286-
auto txr = CreateTransaction(wallet, vecSend, RANDOM_CHANGE_POSITION, strFailReason, coinControl, fee_calc_out,
287-
true, tx.vExtraPayload.size());
288-
if (!txr) {
289-
throw JSONRPCError(RPC_INTERNAL_ERROR, strFailReason.original);
284+
auto res = CreateTransaction(wallet, vecSend, RANDOM_CHANGE_POSITION, coinControl, /*sign=*/true, tx.vExtraPayload.size());
285+
if (!res) {
286+
throw JSONRPCError(RPC_INTERNAL_ERROR, res.GetError().original);
290287
}
291-
CTransactionRef newTx = txr->tx;
292288

289+
const CTransactionRef& newTx = res.GetObj().tx;
293290
tx.vin = newTx->vin;
294291
tx.vout = newTx->vout;
295292

src/test/util/wallet.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqq
2424
#ifdef ENABLE_WALLET
2525
std::string getnewaddress(CWallet& w)
2626
{
27-
CTxDestination dest;
28-
bilingual_str error;
29-
if (!w.GetNewDestination("", dest, error)) assert(false);
27+
auto op_dest = w.GetNewDestination("");
28+
assert(op_dest.HasRes());
3029

31-
return EncodeDestination(dest);
30+
return EncodeDestination(op_dest.GetObj());
3231
}
3332

3433
// void importaddress(CWallet& wallet, const std::string& address)

src/wallet/interfaces.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,10 @@ class WalletImpl : public Wallet
175175
}
176176
int64_t getKeysLeftSinceAutoBackup() override { return m_wallet->nKeysLeftSinceAutoBackup; }
177177
std::string getWalletName() override { return m_wallet->GetName(); }
178-
bool getNewDestination(const std::string label, CTxDestination& dest) override
178+
BResult<CTxDestination> getNewDestination(const std::string label) override
179179
{
180180
LOCK(m_wallet->cs_wallet);
181-
bilingual_str error;
182-
return m_wallet->GetNewDestination(label, dest, error);
181+
return m_wallet->GetNewDestination(label);
183182
}
184183
bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override
185184
{
@@ -288,22 +287,20 @@ class WalletImpl : public Wallet
288287
LOCK(m_wallet->cs_wallet);
289288
return m_wallet->ListProTxCoins();
290289
}
291-
CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
290+
BResult<CTransactionRef> createTransaction(const std::vector<CRecipient>& recipients,
292291
const CCoinControl& coin_control,
293292
bool sign,
294293
int& change_pos,
295-
CAmount& fee,
296-
bilingual_str& fail_reason) override
294+
CAmount& fee) override
297295
{
298296
LOCK(m_wallet->cs_wallet);
299-
FeeCalculation fee_calc_out;
300-
std::optional<CreatedTransactionResult> txr = CreateTransaction(*m_wallet, recipients, change_pos,
301-
fail_reason, coin_control, fee_calc_out, sign);
302-
if (!txr) return {};
303-
fee = txr->fee;
304-
change_pos = txr->change_pos;
297+
const auto& res = CreateTransaction(*m_wallet, recipients, change_pos, coin_control, sign);
298+
if (!res) return res.GetError();
299+
const auto& txr = res.GetObj();
300+
fee = txr.fee;
301+
change_pos = txr.change_pos;
305302

306-
return txr->tx;
303+
return txr.tx;
307304
}
308305
void commitTransaction(CTransactionRef tx,
309306
WalletValueMap value_map,

src/wallet/rpc/addresses.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ RPCHelpMan getnewaddress()
4646
if (!request.params[0].isNull())
4747
label = LabelFromValue(request.params[0]);
4848

49-
CTxDestination dest;
50-
bilingual_str error;
51-
if (!pwallet->GetNewDestination(label, dest, error)) {
52-
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original);
49+
auto op_dest = pwallet->GetNewDestination(label);
50+
if (!op_dest) {
51+
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, op_dest.GetError().original);
5352
}
54-
return EncodeDestination(dest);
53+
return EncodeDestination(op_dest.GetObj());
5554
},
5655
};
5756
}
@@ -80,12 +79,11 @@ RPCHelpMan getrawchangeaddress()
8079
throw JSONRPCError(RPC_WALLET_ERROR, "Error: This wallet has no available keys");
8180
}
8281

83-
CTxDestination dest;
84-
bilingual_str error;
85-
if (!pwallet->GetNewChangeDestination(dest, error)) {
86-
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original);
82+
auto op_dest = pwallet->GetNewChangeDestination();
83+
if (!op_dest) {
84+
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, op_dest.GetError().original);
8785
}
88-
return EncodeDestination(dest);
86+
return EncodeDestination(op_dest.GetObj());
8987
},
9088
};
9189
}

src/wallet/rpc/spend.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,16 @@ UniValue SendMoney(CWallet& wallet, const CCoinControl &coin_control, std::vecto
6565
}
6666

6767
// Send
68-
bilingual_str error;
69-
FeeCalculation fee_calc_out;
70-
std::optional<CreatedTransactionResult> txr = CreateTransaction(wallet, recipients, RANDOM_CHANGE_POSITION, error, coin_control, fee_calc_out, true);
71-
if (!txr) {
72-
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, error.original);
68+
auto res = CreateTransaction(wallet, recipients, RANDOM_CHANGE_POSITION, coin_control, /*sign=*/true);
69+
if (!res) {
70+
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, res.GetError().original);
7371
}
74-
CTransactionRef tx = txr->tx;
72+
const CTransactionRef& tx = res.GetObj().tx;
7573
wallet.CommitTransaction(tx, std::move(map_value), {} /* orderForm */);
7674
if (verbose) {
7775
UniValue entry(UniValue::VOBJ);
7876
entry.pushKV("txid", tx->GetHash().GetHex());
79-
entry.pushKV("fee_reason", StringForFeeReason(fee_calc_out.reason));
77+
entry.pushKV("fee_reason", StringForFeeReason(res.GetObj().fee_calc.reason));
8078
return entry;
8179
}
8280
return tx->GetHash().GetHex();

0 commit comments

Comments
 (0)