Skip to content

Commit d1734f9

Browse files
committed
[wallet] Remove return value from CommitTransaction()
CommitTransaction returns a bool to indicate success, but since commit b3a7410 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.
1 parent b6f486a commit d1734f9

File tree

10 files changed

+21
-45
lines changed

10 files changed

+21
-45
lines changed

src/interfaces/wallet.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,14 @@ class WalletImpl : public Wallet
218218
}
219219
return tx;
220220
}
221-
bool commitTransaction(CTransactionRef tx,
221+
void commitTransaction(CTransactionRef tx,
222222
WalletValueMap value_map,
223-
WalletOrderForm order_form,
224-
std::string& reject_reason) override
223+
WalletOrderForm order_form) override
225224
{
226225
auto locked_chain = m_wallet->chain().lock();
227226
LOCK(m_wallet->cs_wallet);
228227
CValidationState state;
229-
if (!m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form), state)) {
230-
reject_reason = state.GetRejectReason();
231-
return false;
232-
}
233-
return true;
228+
m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form), state);
234229
}
235230
bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); }
236231
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
@@ -139,7 +139,6 @@ class WalletModel : public QObject
139139
AmountWithFeeExceedsBalance,
140140
DuplicateAddress,
141141
TransactionCreationFailed, // Error returned when wallet is still locked
142-
TransactionCommitFailed,
143142
AbsurdFee,
144143
PaymentRequestExpired
145144
};

src/wallet/feebumper.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,7 @@ Result CommitTransaction(CWallet& wallet, const uint256& txid, CMutableTransacti
394394
mapValue["replaces_txid"] = oldWtx.GetHash().ToString();
395395

396396
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-
}
397+
wallet.CommitTransaction(tx, std::move(mapValue), oldWtx.vOrderForm, state);
402398

403399
bumped_txid = tx->GetHash();
404400
if (state.IsInvalid()) {

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,7 @@ static CTransactionRef SendMoney(interfaces::Chain::Lock& locked_chain, CWallet
343343
throw JSONRPCError(RPC_WALLET_ERROR, strError);
344344
}
345345
CValidationState state;
346-
if (!pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state)) {
347-
strError = strprintf("Error: The transaction was rejected! Reason given: %s", FormatStateMessage(state));
348-
throw JSONRPCError(RPC_WALLET_ERROR, strError);
349-
}
346+
pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state);
350347
return tx;
351348
}
352349

@@ -928,11 +925,7 @@ static UniValue sendmany(const JSONRPCRequest& request)
928925
if (!fCreated)
929926
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
930927
CValidationState state;
931-
if (!pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state)) {
932-
strFailReason = strprintf("Transaction commit failed:: %s", FormatStateMessage(state));
933-
throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
934-
}
935-
928+
pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state);
936929
return tx->GetHash().GetHex();
937930
}
938931

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
452452
BOOST_CHECK(wallet->CreateTransaction(*locked_chain, {recipient}, tx, fee, changePos, error, dummy));
453453
}
454454
CValidationState state;
455-
BOOST_CHECK(wallet->CommitTransaction(tx, {}, {}, state));
455+
wallet->CommitTransaction(tx, {}, {}, state);
456456
CMutableTransaction blocktx;
457457
{
458458
LOCK(wallet->cs_wallet);

src/wallet/wallet.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,7 +3286,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
32863286
return true;
32873287
}
32883288

3289-
bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state)
3289+
void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state)
32903290
{
32913291
auto locked_chain = chain().lock();
32923292
LOCK(cs_wallet);
@@ -3314,15 +3314,16 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
33143314
// fInMempool flag is cached properly
33153315
CWalletTx& wtx = mapWallet.at(wtxNew.GetHash());
33163316

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

3325-
return true;
3322+
std::string err_string;
3323+
if (!wtx.SubmitMemoryPoolAndRelay(err_string, true, *locked_chain)) {
3324+
WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", err_string);
3325+
// TODO: if we expect the failure to be long term or permanent, instead delete wtx from the wallet and return failure.
3326+
}
33263327
}
33273328

33283329
DBErrors CWallet::LoadWallet(bool& fFirstRunRet)

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ class CWallet final : public FillableSigningProvider, private interfaces::Chain:
11571157
* @param orderForm[in] BIP 70 / BIP 21 order form details to be set on the transaction.
11581158
* @param state[in,out] CValidationState object returning information about whether the transaction was accepted
11591159
*/
1160-
bool CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state);
1160+
void CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state);
11611161

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

0 commit comments

Comments
 (0)