Merge bitcoin-core/gui#807: refactor: interfaces, make 'createTransaction' less error-prone
4c0d4f6f93refactor: interfaces, make 'createTransaction' less error-prone (furszy)e2c3ec9bf4refactor: move CreatedTransactionResult to types.h (furszy)45372175c3gui: remove AmountWithFeeExceedsBalance error special case (furszy) Pull request description: Bundle all function's outputs inside the `util::Result` returned object. Removals: - The input-output 'change_pos' ref arg from `createTransaction`, which has been a source of bugs in the past. - The 'fee' ref arg from `createTransaction`, which is currently only set when the transaction creation process succeeds. - The no longer needed `AmountWithFeeExceedsBalance` error (more info about its re-introduction at [bitcoin#25269](https://github.com/bitcoin/bitcoin/pull/25269) and [bitcoin#34299](https://github.com/bitcoin/bitcoin/pull/34299). Additionally, this PR moves the `CreatedTransactionResult` struct into its own file. This change is made to avoid further expanding the GUI dependencies on `wallet.h`. Structurally, the GUI should only access the model/interfaces and never the wallet directly. ACKs for top commit: stratospher: ACK4c0d4f6. hebasto: ACK4c0d4f6f93. Tree-SHA512: 4fc61f08ca2e66e46001defb3a2e852265713e75006c98f0c465bd48afe42e7b0d626d28d578741906fdd26e907d6919f06dc640c55c44efc3dfa766fdbf38a4
This commit is contained in:
7 files changed
+33
-46
No files matched your search
@@ -40,6 +40,7 @@ namespace node {
|
|||||||
enum class TransactionError;
|
enum class TransactionError;
|
||||||
} // namespace node
|
} // namespace node
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
|
struct CreatedTransactionResult;
|
||||||
class CCoinControl;
|
class CCoinControl;
|
||||||
class CWallet;
|
class CWallet;
|
||||||
enum class AddressPurpose;
|
enum class AddressPurpose;
|
||||||
@@ -142,11 +143,10 @@ public:
|
|||||||
virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
|
virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
|
||||||
|
|
||||||
//! Create transaction.
|
//! Create transaction.
|
||||||
virtual util::Result<CTransactionRef> createTransaction(const std::vector<wallet::CRecipient>& recipients,
|
virtual util::Result<wallet::CreatedTransactionResult> createTransaction(const std::vector<wallet::CRecipient>& recipients,
|
||||||
const wallet::CCoinControl& coin_control,
|
const wallet::CCoinControl& coin_control,
|
||||||
bool sign,
|
bool sign,
|
||||||
int& change_pos,
|
std::optional<unsigned int> change_pos) = 0;
|
||||||
CAmount& fee) = 0;
|
|
||||||
|
|
||||||
//! Commit transaction.
|
//! Commit transaction.
|
||||||
virtual void commitTransaction(CTransactionRef tx,
|
virtual void commitTransaction(CTransactionRef tx,
|
||||||
|
|||||||
@@ -742,9 +742,6 @@ void SendCoinsDialog::processSendCoinsReturn(const WalletModel::SendCoinsReturn
|
|||||||
case WalletModel::AmountExceedsBalance:
|
case WalletModel::AmountExceedsBalance:
|
||||||
msgParams.first = tr("The amount exceeds your balance.");
|
msgParams.first = tr("The amount exceeds your balance.");
|
||||||
break;
|
break;
|
||||||
case WalletModel::AmountWithFeeExceedsBalance:
|
|
||||||
msgParams.first = tr("The total exceeds your balance when the %1 transaction fee is included.").arg(msgArg);
|
|
||||||
break;
|
|
||||||
case WalletModel::DuplicateAddress:
|
case WalletModel::DuplicateAddress:
|
||||||
msgParams.first = tr("Duplicate address found: addresses should only be used once each.");
|
msgParams.first = tr("Duplicate address found: addresses should only be used once each.");
|
||||||
break;
|
break;
|
||||||
|
|||||||
+13
-16
@@ -23,6 +23,7 @@
|
|||||||
#include <psbt.h>
|
#include <psbt.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
#include <wallet/coincontrol.h>
|
#include <wallet/coincontrol.h>
|
||||||
|
#include <wallet/types.h>
|
||||||
#include <wallet/wallet.h>
|
#include <wallet/wallet.h>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -149,6 +150,8 @@ bool WalletModel::validateAddress(const QString& address) const
|
|||||||
|
|
||||||
WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl)
|
WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl)
|
||||||
{
|
{
|
||||||
|
transaction.getWtx() = nullptr; // reset tx output
|
||||||
|
|
||||||
CAmount total = 0;
|
CAmount total = 0;
|
||||||
bool fSubtractFeeFromAmount = false;
|
bool fSubtractFeeFromAmount = false;
|
||||||
QList<SendCoinsRecipient> recipients = transaction.getRecipients();
|
QList<SendCoinsRecipient> recipients = transaction.getRecipients();
|
||||||
@@ -199,27 +202,21 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CAmount nFeeRequired = 0;
|
|
||||||
int nChangePosRet = -1;
|
|
||||||
|
|
||||||
auto& newTx = transaction.getWtx();
|
auto& newTx = transaction.getWtx();
|
||||||
const auto& res = m_wallet->createTransaction(vecSend, coinControl, /*sign=*/!wallet().privateKeysDisabled(), nChangePosRet, nFeeRequired);
|
const auto& res = m_wallet->createTransaction(vecSend, coinControl, /*sign=*/!wallet().privateKeysDisabled(), /*change_pos=*/std::nullopt);
|
||||||
newTx = res ? *res : nullptr;
|
if (!res) {
|
||||||
transaction.setTransactionFee(nFeeRequired);
|
|
||||||
if (fSubtractFeeFromAmount && newTx)
|
|
||||||
transaction.reassignAmounts(nChangePosRet);
|
|
||||||
|
|
||||||
if(!newTx)
|
|
||||||
{
|
|
||||||
if(!fSubtractFeeFromAmount && (total + nFeeRequired) > nBalance)
|
|
||||||
{
|
|
||||||
return SendCoinsReturn(AmountWithFeeExceedsBalance);
|
|
||||||
}
|
|
||||||
Q_EMIT message(tr("Send Coins"), QString::fromStdString(util::ErrorString(res).translated),
|
Q_EMIT message(tr("Send Coins"), QString::fromStdString(util::ErrorString(res).translated),
|
||||||
CClientUIInterface::MSG_ERROR);
|
CClientUIInterface::MSG_ERROR);
|
||||||
return TransactionCreationFailed;
|
return TransactionCreationFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newTx = res->tx;
|
||||||
|
CAmount nFeeRequired = res->fee;
|
||||||
|
transaction.setTransactionFee(nFeeRequired);
|
||||||
|
if (fSubtractFeeFromAmount && newTx) {
|
||||||
|
transaction.reassignAmounts(static_cast<int>(res->change_pos.value_or(-1)));
|
||||||
|
}
|
||||||
|
|
||||||
// Reject absurdly high fee. (This can never happen because the
|
// Reject absurdly high fee. (This can never happen because the
|
||||||
// wallet never creates transactions with fee greater than
|
// wallet never creates transactions with fee greater than
|
||||||
// m_default_max_tx_fee. This merely a belt-and-suspenders check).
|
// m_default_max_tx_fee. This merely a belt-and-suspenders check).
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ public:
|
|||||||
InvalidAmount,
|
InvalidAmount,
|
||||||
InvalidAddress,
|
InvalidAddress,
|
||||||
AmountExceedsBalance,
|
AmountExceedsBalance,
|
||||||
AmountWithFeeExceedsBalance,
|
|
||||||
DuplicateAddress,
|
DuplicateAddress,
|
||||||
TransactionCreationFailed, // Error returned when wallet is still locked
|
TransactionCreationFailed, // Error returned when wallet is still locked
|
||||||
AbsurdFee
|
AbsurdFee
|
||||||
|
|||||||
@@ -257,21 +257,13 @@ public:
|
|||||||
LOCK(m_wallet->cs_wallet);
|
LOCK(m_wallet->cs_wallet);
|
||||||
return m_wallet->ListLockedCoins(outputs);
|
return m_wallet->ListLockedCoins(outputs);
|
||||||
}
|
}
|
||||||
util::Result<CTransactionRef> createTransaction(const std::vector<CRecipient>& recipients,
|
util::Result<wallet::CreatedTransactionResult> createTransaction(const std::vector<CRecipient>& recipients,
|
||||||
const CCoinControl& coin_control,
|
const CCoinControl& coin_control,
|
||||||
bool sign,
|
bool sign,
|
||||||
int& change_pos,
|
std::optional<unsigned int> change_pos) override
|
||||||
CAmount& fee) override
|
|
||||||
{
|
{
|
||||||
LOCK(m_wallet->cs_wallet);
|
LOCK(m_wallet->cs_wallet);
|
||||||
auto res = CreateTransaction(*m_wallet, recipients, change_pos == -1 ? std::nullopt : std::make_optional(change_pos),
|
return CreateTransaction(*m_wallet, recipients, change_pos, coin_control, sign);
|
||||||
coin_control, sign);
|
|
||||||
if (!res) return util::Error{util::ErrorString(res)};
|
|
||||||
const auto& txr = *res;
|
|
||||||
fee = txr.fee;
|
|
||||||
change_pos = txr.change_pos ? int(*txr.change_pos) : -1;
|
|
||||||
|
|
||||||
return txr.tx;
|
|
||||||
}
|
}
|
||||||
void commitTransaction(CTransactionRef tx,
|
void commitTransaction(CTransactionRef tx,
|
||||||
WalletValueMap value_map,
|
WalletValueMap value_map,
|
||||||
|
|||||||
+1
-11
@@ -10,6 +10,7 @@
|
|||||||
#include <util/result.h>
|
#include <util/result.h>
|
||||||
#include <wallet/coinselection.h>
|
#include <wallet/coinselection.h>
|
||||||
#include <wallet/transaction.h>
|
#include <wallet/transaction.h>
|
||||||
|
#include <wallet/types.h>
|
||||||
#include <wallet/wallet.h>
|
#include <wallet/wallet.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
@@ -186,17 +187,6 @@ util::Result<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& av
|
|||||||
const CAmount& nTargetValue, const CCoinControl& coin_control,
|
const CAmount& nTargetValue, const CCoinControl& coin_control,
|
||||||
const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
|
const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
|
||||||
|
|
||||||
struct CreatedTransactionResult
|
|
||||||
{
|
|
||||||
CTransactionRef tx;
|
|
||||||
CAmount fee;
|
|
||||||
FeeCalculation fee_calc;
|
|
||||||
std::optional<unsigned int> change_pos;
|
|
||||||
|
|
||||||
CreatedTransactionResult(CTransactionRef _tx, CAmount _fee, std::optional<unsigned int> _change_pos, const FeeCalculation& _fee_calc)
|
|
||||||
: tx(_tx), fee(_fee), fee_calc(_fee_calc), change_pos(_change_pos) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a height-based locktime for new transactions (uses the height of the
|
* Set a height-based locktime for new transactions (uses the height of the
|
||||||
* current chain tip unless we are not synced with the current chain
|
* current chain tip unless we are not synced with the current chain
|
||||||
|
|||||||
+13
-1
@@ -14,7 +14,7 @@
|
|||||||
#ifndef BITCOIN_WALLET_TYPES_H
|
#ifndef BITCOIN_WALLET_TYPES_H
|
||||||
#define BITCOIN_WALLET_TYPES_H
|
#define BITCOIN_WALLET_TYPES_H
|
||||||
|
|
||||||
#include <type_traits>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
/**
|
/**
|
||||||
@@ -30,6 +30,18 @@ enum class AddressPurpose {
|
|||||||
SEND,
|
SEND,
|
||||||
REFUND, //!< Never set in current code may be present in older wallet databases
|
REFUND, //!< Never set in current code may be present in older wallet databases
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct CreatedTransactionResult
|
||||||
|
{
|
||||||
|
CTransactionRef tx;
|
||||||
|
CAmount fee;
|
||||||
|
FeeCalculation fee_calc;
|
||||||
|
std::optional<unsigned int> change_pos;
|
||||||
|
|
||||||
|
CreatedTransactionResult(CTransactionRef _tx, CAmount _fee, std::optional<unsigned int> _change_pos, const FeeCalculation& _fee_calc)
|
||||||
|
: tx(_tx), fee(_fee), fee_calc(_fee_calc), change_pos(_change_pos) {}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace wallet
|
} // namespace wallet
|
||||||
|
|
||||||
#endif // BITCOIN_WALLET_TYPES_H
|
#endif // BITCOIN_WALLET_TYPES_H
|
||||||
Reference in new issue
Block a user