[wallet] Remove return value from CommitTransaction()

CommitTransaction returns a bool to indicate success, but since commit
b3a74100b8 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.
This commit is contained in:
John Newbery 2019-04-03 17:55:24 -04:00
parent b6f486a02b
commit d1734f9a3b
10 changed files with 21 additions and 45 deletions

View File

@ -218,19 +218,14 @@ public:
} }
return tx; return tx;
} }
bool commitTransaction(CTransactionRef tx, void commitTransaction(CTransactionRef tx,
WalletValueMap value_map, WalletValueMap value_map,
WalletOrderForm order_form, WalletOrderForm order_form) override
std::string& reject_reason) override
{ {
auto locked_chain = m_wallet->chain().lock(); auto locked_chain = m_wallet->chain().lock();
LOCK(m_wallet->cs_wallet); LOCK(m_wallet->cs_wallet);
CValidationState state; CValidationState state;
if (!m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form), state)) { m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form), state);
reject_reason = state.GetRejectReason();
return false;
}
return true;
} }
bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); } bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); }
bool abandonTransaction(const uint256& txid) override bool abandonTransaction(const uint256& txid) override

View File

@ -141,10 +141,9 @@ public:
std::string& fail_reason) = 0; std::string& fail_reason) = 0;
//! Commit transaction. //! Commit transaction.
virtual bool commitTransaction(CTransactionRef tx, virtual void commitTransaction(CTransactionRef tx,
WalletValueMap value_map, WalletValueMap value_map,
WalletOrderForm order_form, WalletOrderForm order_form) = 0;
std::string& reject_reason) = 0;
//! Return whether transaction can be abandoned. //! Return whether transaction can be abandoned.
virtual bool transactionCanBeAbandoned(const uint256& txid) = 0; virtual bool transactionCanBeAbandoned(const uint256& txid) = 0;

View File

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

View File

@ -260,9 +260,7 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &tran
} }
auto& newTx = transaction.getWtx(); auto& newTx = transaction.getWtx();
std::string rejectReason; wallet().commitTransaction(newTx, {} /* mapValue */, std::move(vOrderForm));
if (!wallet().commitTransaction(newTx, {} /* mapValue */, std::move(vOrderForm), rejectReason))
return SendCoinsReturn(TransactionCommitFailed, QString::fromStdString(rejectReason));
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << *newTx; ssTx << *newTx;

View File

@ -139,7 +139,6 @@ public:
AmountWithFeeExceedsBalance, AmountWithFeeExceedsBalance,
DuplicateAddress, DuplicateAddress,
TransactionCreationFailed, // Error returned when wallet is still locked TransactionCreationFailed, // Error returned when wallet is still locked
TransactionCommitFailed,
AbsurdFee, AbsurdFee,
PaymentRequestExpired PaymentRequestExpired
}; };

View File

@ -394,11 +394,7 @@ Result CommitTransaction(CWallet& wallet, const uint256& txid, CMutableTransacti
mapValue["replaces_txid"] = oldWtx.GetHash().ToString(); mapValue["replaces_txid"] = oldWtx.GetHash().ToString();
CValidationState state; CValidationState state;
if (!wallet.CommitTransaction(tx, std::move(mapValue), oldWtx.vOrderForm, state)) { wallet.CommitTransaction(tx, std::move(mapValue), oldWtx.vOrderForm, state);
// NOTE: CommitTransaction never returns false, so this should never happen.
errors.push_back(strprintf("The transaction was rejected: %s", FormatStateMessage(state)));
return Result::WALLET_ERROR;
}
bumped_txid = tx->GetHash(); bumped_txid = tx->GetHash();
if (state.IsInvalid()) { if (state.IsInvalid()) {

View File

@ -343,10 +343,7 @@ static CTransactionRef SendMoney(interfaces::Chain::Lock& locked_chain, CWallet
throw JSONRPCError(RPC_WALLET_ERROR, strError); throw JSONRPCError(RPC_WALLET_ERROR, strError);
} }
CValidationState state; CValidationState state;
if (!pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state)) { pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state);
strError = strprintf("Error: The transaction was rejected! Reason given: %s", FormatStateMessage(state));
throw JSONRPCError(RPC_WALLET_ERROR, strError);
}
return tx; return tx;
} }
@ -928,11 +925,7 @@ static UniValue sendmany(const JSONRPCRequest& request)
if (!fCreated) if (!fCreated)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason); throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
CValidationState state; CValidationState state;
if (!pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state)) { pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, state);
strFailReason = strprintf("Transaction commit failed:: %s", FormatStateMessage(state));
throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
}
return tx->GetHash().GetHex(); return tx->GetHash().GetHex();
} }

View File

@ -452,7 +452,7 @@ public:
BOOST_CHECK(wallet->CreateTransaction(*locked_chain, {recipient}, tx, fee, changePos, error, dummy)); BOOST_CHECK(wallet->CreateTransaction(*locked_chain, {recipient}, tx, fee, changePos, error, dummy));
} }
CValidationState state; CValidationState state;
BOOST_CHECK(wallet->CommitTransaction(tx, {}, {}, state)); wallet->CommitTransaction(tx, {}, {}, state);
CMutableTransaction blocktx; CMutableTransaction blocktx;
{ {
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);

View File

@ -3286,7 +3286,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
return true; return true;
} }
bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state) void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state)
{ {
auto locked_chain = chain().lock(); auto locked_chain = chain().lock();
LOCK(cs_wallet); LOCK(cs_wallet);
@ -3314,7 +3314,11 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
// fInMempool flag is cached properly // fInMempool flag is cached properly
CWalletTx& wtx = mapWallet.at(wtxNew.GetHash()); CWalletTx& wtx = mapWallet.at(wtxNew.GetHash());
if (fBroadcastTransactions) { if (!fBroadcastTransactions) {
// Don't submit tx to the mempool
return;
}
std::string err_string; std::string err_string;
if (!wtx.SubmitMemoryPoolAndRelay(err_string, true, *locked_chain)) { if (!wtx.SubmitMemoryPoolAndRelay(err_string, true, *locked_chain)) {
WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", err_string); WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", err_string);
@ -3322,9 +3326,6 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
} }
} }
return true;
}
DBErrors CWallet::LoadWallet(bool& fFirstRunRet) DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
{ {
// Even if we don't use this lock in this function, we want to preserve // Even if we don't use this lock in this function, we want to preserve

View File

@ -1157,7 +1157,7 @@ public:
* @param orderForm[in] BIP 70 / BIP 21 order form details to be set on the transaction. * @param orderForm[in] BIP 70 / BIP 21 order form details to be set on the transaction.
* @param state[in,out] CValidationState object returning information about whether the transaction was accepted * @param state[in,out] CValidationState object returning information about whether the transaction was accepted
*/ */
bool CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state); void CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state);
bool DummySignTx(CMutableTransaction &txNew, const std::set<CTxOut> &txouts, bool use_max_sig = false) const bool DummySignTx(CMutableTransaction &txNew, const std::set<CTxOut> &txouts, bool use_max_sig = false) const
{ {