Skip to content

Commit 8a19114

Browse files
committed
Merge #17154: wallet: Remove return value from CommitTransaction
9e95931 [wallet] Remove `state` argument from CWallet::CommitTransaction (John Newbery) d1734f9 [wallet] Remove return value from CommitTransaction() (John Newbery) b6f486a [wallet] Add doxygen comment to CWallet::CommitTransaction() (John Newbery) 8bba91b [wallet] Fix whitespace in CWallet::CommitTransaction() (John Newbery) Pull request description: `CommitTransaction()` returns a bool to indicate success, but since commit b3a7410 (#9302) it only returns true, even if the transaction was not successfully broadcast. This commit changes CommitTransaction() to return void. All dead code in `if (!CommitTransaction())` branches has been removed. Two additional commits fix up the idiosyncratic whitespace in `CommitTransaction` and add a doxygen comment for the function. ACKs for top commit: laanwj: ACK 9e95931 Tree-SHA512: a55a2c20369a45222fc0e02d0891495655a926e71c4f52cb72624768dd7b9c1dca716ea67d38420afb90f40c6e0fd448caa60c18fd693bb10ecb110b641820e6
2 parents c5ac7af + 9e95931 commit 8a19114

File tree

10 files changed

+52
-90
lines changed

10 files changed

+52
-90
lines changed

src/interfaces/wallet.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <interfaces/wallet.h>
66

77
#include <amount.h>
8-
#include <consensus/validation.h>
98
#include <interfaces/chain.h>
109
#include <interfaces/handler.h>
1110
#include <policy/fees.h>
@@ -216,19 +215,13 @@ class WalletImpl : public Wallet
216215
}
217216
return tx;
218217
}
219-
bool commitTransaction(CTransactionRef tx,
218+
void commitTransaction(CTransactionRef tx,
220219
WalletValueMap value_map,
221-
WalletOrderForm order_form,
222-
std::string& reject_reason) override
220+
WalletOrderForm order_form) override
223221
{
224222
auto locked_chain = m_wallet->chain().lock();
225223
LOCK(m_wallet->cs_wallet);
226-
CValidationState state;
227-
if (!m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form), state)) {
228-
reject_reason = state.GetRejectReason();
229-
return false;
230-
}
231-
return true;
224+
m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form));
232225
}
233226
bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); }
234227
bool abandonTransaction(const uint256& txid) override

src/interfaces/wallet.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ class Wallet
141141
std::string& fail_reason) = 0;
142142

143143
//! Commit transaction.
144-
virtual bool commitTransaction(CTransactionRef tx,
144+
virtual void commitTransaction(CTransactionRef tx,
145145
WalletValueMap value_map,
146-
WalletOrderForm order_form,
147-
std::string& reject_reason) = 0;
146+
WalletOrderForm order_form) = 0;
148147

149148
//! Return whether transaction can be abandoned.
150149
virtual bool transactionCanBeAbandoned(const uint256& txid) = 0;

src/qt/sendcoinsdialog.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ void SendCoinsDialog::processSendCoinsReturn(const WalletModel::SendCoinsReturn
558558
msgParams.second = CClientUIInterface::MSG_WARNING;
559559

560560
// This comment is specific to SendCoinsDialog usage of WalletModel::SendCoinsReturn.
561-
// WalletModel::TransactionCommitFailed is used only in WalletModel::sendCoins()
562-
// all others are used only in WalletModel::prepareTransaction()
561+
// All status values are used only in WalletModel::prepareTransaction()
563562
switch(sendCoinsReturn.status)
564563
{
565564
case WalletModel::InvalidAddress:
@@ -581,10 +580,6 @@ void SendCoinsDialog::processSendCoinsReturn(const WalletModel::SendCoinsReturn
581580
msgParams.first = tr("Transaction creation failed!");
582581
msgParams.second = CClientUIInterface::MSG_ERROR;
583582
break;
584-
case WalletModel::TransactionCommitFailed:
585-
msgParams.first = tr("The transaction was rejected with the following reason: %1").arg(sendCoinsReturn.reasonCommitFailed);
586-
msgParams.second = CClientUIInterface::MSG_ERROR;
587-
break;
588583
case WalletModel::AbsurdFee:
589584
msgParams.first = tr("A fee higher than %1 is considered an absurdly high fee.").arg(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), model->wallet().getDefaultMaxTxFee()));
590585
break;

src/qt/walletmodel.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &tran
260260
}
261261

262262
auto& newTx = transaction.getWtx();
263-
std::string rejectReason;
264-
if (!wallet().commitTransaction(newTx, {} /* mapValue */, std::move(vOrderForm), rejectReason))
265-
return SendCoinsReturn(TransactionCommitFailed, QString::fromStdString(rejectReason));
263+
wallet().commitTransaction(newTx, {} /* mapValue */, std::move(vOrderForm));
266264

267265
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
268266
ssTx << *newTx;

src/qt/walletmodel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ class WalletModel : public QObject
138138
AmountWithFeeExceedsBalance,
139139
DuplicateAddress,
140140
TransactionCreationFailed, // Error returned when wallet is still locked
141-
TransactionCommitFailed,
142141
AbsurdFee,
143142
PaymentRequestExpired
144143
};

src/wallet/feebumper.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <consensus/validation.h>
65
#include <interfaces/chain.h>
76
#include <wallet/coincontrol.h>
87
#include <wallet/feebumper.h>
@@ -393,21 +392,10 @@ Result CommitTransaction(CWallet& wallet, const uint256& txid, CMutableTransacti
393392
mapValue_t mapValue = oldWtx.mapValue;
394393
mapValue["replaces_txid"] = oldWtx.GetHash().ToString();
395394

396-
CValidationState state;
397-
if (!wallet.CommitTransaction(tx, std::move(mapValue), oldWtx.vOrderForm, state)) {
398-
// NOTE: CommitTransaction never returns false, so this should never happen.
399-
errors.push_back(strprintf("The transaction was rejected: %s", FormatStateMessage(state)));
400-
return Result::WALLET_ERROR;
401-
}
402-
403-
bumped_txid = tx->GetHash();
404-
if (state.IsInvalid()) {
405-
// This can happen if the mempool rejected the transaction. Report
406-
// what happened in the "errors" response.
407-
errors.push_back(strprintf("Error: The transaction was rejected: %s", FormatStateMessage(state)));
408-
}
395+
wallet.CommitTransaction(tx, std::move(mapValue), oldWtx.vOrderForm);
409396

410397
// mark the original tx as bumped
398+
bumped_txid = tx->GetHash();
411399
if (!wallet.MarkReplaced(oldWtx.GetHash(), bumped_txid)) {
412400
// TODO: see if JSON-RPC has a standard way of returning a response
413401
// along with an exception. It would be good to return information about

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <amount.h>
7-
#include <consensus/validation.h>
87
#include <core_io.h>
98
#include <init.h>
109
#include <interfaces/chain.h>
@@ -341,11 +340,7 @@ static CTransactionRef SendMoney(interfaces::Chain::Lock& locked_chain, CWallet
341340
strError = strprintf("Error: This transaction requires a transaction fee of at least %s", FormatMoney(nFeeRequired));
342341
throw JSONRPCError(RPC_WALLET_ERROR, strError);
343342
}
344-
CValidationState state;
345-
if (!pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state)) {
346-
strError = strprintf("Error: The transaction was rejected! Reason given: %s", FormatStateMessage(state));
347-
throw JSONRPCError(RPC_WALLET_ERROR, strError);
348-
}
343+
pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */);
349344
return tx;
350345
}
351346

@@ -926,12 +921,7 @@ static UniValue sendmany(const JSONRPCRequest& request)
926921
bool fCreated = pwallet->CreateTransaction(*locked_chain, vecSend, tx, nFeeRequired, nChangePosRet, strFailReason, coin_control);
927922
if (!fCreated)
928923
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
929-
CValidationState state;
930-
if (!pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state)) {
931-
strFailReason = strprintf("Transaction commit failed:: %s", FormatStateMessage(state));
932-
throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
933-
}
934-
924+
pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */);
935925
return tx->GetHash().GetHex();
936926
}
937927

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <stdint.h>
99
#include <vector>
1010

11-
#include <consensus/validation.h>
1211
#include <interfaces/chain.h>
1312
#include <policy/policy.h>
1413
#include <rpc/server.h>
@@ -451,8 +450,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
451450
auto locked_chain = m_chain->lock();
452451
BOOST_CHECK(wallet->CreateTransaction(*locked_chain, {recipient}, tx, fee, changePos, error, dummy));
453452
}
454-
CValidationState state;
455-
BOOST_CHECK(wallet->CommitTransaction(tx, {}, {}, state));
453+
wallet->CommitTransaction(tx, {}, {});
456454
CMutableTransaction blocktx;
457455
{
458456
LOCK(wallet->cs_wallet);

src/wallet/wallet.cpp

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3284,51 +3284,44 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
32843284
return true;
32853285
}
32863286

3287-
/**
3288-
* Call after CreateTransaction unless you want to abort
3289-
*/
3290-
bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state)
3287+
void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm)
32913288
{
3292-
{
3293-
auto locked_chain = chain().lock();
3294-
LOCK(cs_wallet);
3289+
auto locked_chain = chain().lock();
3290+
LOCK(cs_wallet);
32953291

3296-
CWalletTx wtxNew(this, std::move(tx));
3297-
wtxNew.mapValue = std::move(mapValue);
3298-
wtxNew.vOrderForm = std::move(orderForm);
3299-
wtxNew.fTimeReceivedIsTxTime = true;
3300-
wtxNew.fFromMe = true;
3292+
CWalletTx wtxNew(this, std::move(tx));
3293+
wtxNew.mapValue = std::move(mapValue);
3294+
wtxNew.vOrderForm = std::move(orderForm);
3295+
wtxNew.fTimeReceivedIsTxTime = true;
3296+
wtxNew.fFromMe = true;
33013297

3302-
WalletLogPrintf("CommitTransaction:\n%s", wtxNew.tx->ToString()); /* Continued */
3303-
{
3298+
WalletLogPrintf("CommitTransaction:\n%s", wtxNew.tx->ToString()); /* Continued */
33043299

3305-
// Add tx to wallet, because if it has change it's also ours,
3306-
// otherwise just for transaction history.
3307-
AddToWallet(wtxNew);
3300+
// Add tx to wallet, because if it has change it's also ours,
3301+
// otherwise just for transaction history.
3302+
AddToWallet(wtxNew);
33083303

3309-
// Notify that old coins are spent
3310-
for (const CTxIn& txin : wtxNew.tx->vin)
3311-
{
3312-
CWalletTx &coin = mapWallet.at(txin.prevout.hash);
3313-
coin.BindWallet(this);
3314-
NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
3315-
}
3316-
}
3304+
// Notify that old coins are spent
3305+
for (const CTxIn& txin : wtxNew.tx->vin) {
3306+
CWalletTx &coin = mapWallet.at(txin.prevout.hash);
3307+
coin.BindWallet(this);
3308+
NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
3309+
}
33173310

3318-
// Get the inserted-CWalletTx from mapWallet so that the
3319-
// fInMempool flag is cached properly
3320-
CWalletTx& wtx = mapWallet.at(wtxNew.GetHash());
3311+
// Get the inserted-CWalletTx from mapWallet so that the
3312+
// fInMempool flag is cached properly
3313+
CWalletTx& wtx = mapWallet.at(wtxNew.GetHash());
33213314

3322-
if (fBroadcastTransactions)
3323-
{
3324-
std::string err_string;
3325-
if (!wtx.SubmitMemoryPoolAndRelay(err_string, true, *locked_chain)) {
3326-
WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", err_string);
3327-
// TODO: if we expect the failure to be long term or permanent, instead delete wtx from the wallet and return failure.
3328-
}
3329-
}
3315+
if (!fBroadcastTransactions) {
3316+
// Don't submit tx to the mempool
3317+
return;
3318+
}
3319+
3320+
std::string err_string;
3321+
if (!wtx.SubmitMemoryPoolAndRelay(err_string, true, *locked_chain)) {
3322+
WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", err_string);
3323+
// TODO: if we expect the failure to be long term or permanent, instead delete wtx from the wallet and return failure.
33303324
}
3331-
return true;
33323325
}
33333326

33343327
DBErrors CWallet::LoadWallet(bool& fFirstRunRet)

src/wallet/wallet.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,16 @@ class CWallet final : public FillableSigningProvider, private interfaces::Chain:
11461146
*/
11471147
bool CreateTransaction(interfaces::Chain::Lock& locked_chain, const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut,
11481148
std::string& strFailReason, const CCoinControl& coin_control, bool sign = true);
1149-
bool CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state);
1149+
/**
1150+
* Submit the transaction to the node's mempool and then relay to peers.
1151+
* Should be called after CreateTransaction unless you want to abort
1152+
* broadcasting the transaction.
1153+
*
1154+
* @param tx[in] The transaction to be broadcast.
1155+
* @param mapValue[in] key-values to be set on the transaction.
1156+
* @param orderForm[in] BIP 70 / BIP 21 order form details to be set on the transaction.
1157+
*/
1158+
void CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm);
11501159

11511160
bool DummySignTx(CMutableTransaction &txNew, const std::set<CTxOut> &txouts, bool use_max_sig = false) const
11521161
{

0 commit comments

Comments
 (0)