Skip to content

Commit ecd81df

Browse files
committed
Make CoinControl a required argument to CreateTransaction
1 parent 8fdd23a commit ecd81df

File tree

7 files changed

+27
-28
lines changed

7 files changed

+27
-28
lines changed

src/qt/sendcoinsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ void SendCoinsDialog::on_sendButton_clicked()
281281

282282
ctrl.signalRbf = ui->optInRBF->isChecked();
283283

284-
prepareStatus = model->prepareTransaction(currentTransaction, &ctrl);
284+
prepareStatus = model->prepareTransaction(currentTransaction, ctrl);
285285

286286
// process prepareStatus and on error generate message shown to user
287287
processSendCoinsReturn(prepareStatus,

src/qt/walletmodel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ bool WalletModel::validateAddress(const QString &address)
191191
return addressParsed.IsValid();
192192
}
193193

194-
WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction &transaction, const CCoinControl *coinControl)
194+
WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl)
195195
{
196196
CAmount total = 0;
197197
bool fSubtractFeeFromAmount = false;
@@ -258,7 +258,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
258258
return DuplicateAddress;
259259
}
260260

261-
CAmount nBalance = getBalance(coinControl);
261+
CAmount nBalance = getBalance(&coinControl);
262262

263263
if(total > nBalance)
264264
{

src/qt/walletmodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class WalletModel : public QObject
154154
};
155155

156156
// prepare transaction for getting txfee before sending coins
157-
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl *coinControl = NULL);
157+
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl);
158158

159159
// Send coins to a list of recipients
160160
SendCoinsReturn sendCoins(WalletModelTransaction &transaction);

src/wallet/rpcwallet.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ UniValue getaddressesbyaccount(const JSONRPCRequest& request)
356356
return ret;
357357
}
358358

359-
static void SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew, CCoinControl *coin_control = nullptr)
359+
static void SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew, const CCoinControl& coin_control)
360360
{
361361
CAmount curBalance = pwallet->GetBalance();
362362

@@ -472,7 +472,7 @@ UniValue sendtoaddress(const JSONRPCRequest& request)
472472

473473
EnsureWalletIsUnlocked(pwallet);
474474

475-
SendMoney(pwallet, address.Get(), nAmount, fSubtractFeeFromAmount, wtx, &coin_control);
475+
SendMoney(pwallet, address.Get(), nAmount, fSubtractFeeFromAmount, wtx, coin_control);
476476

477477
return wtx.GetHash().GetHex();
478478
}
@@ -898,7 +898,8 @@ UniValue sendfrom(const JSONRPCRequest& request)
898898
if (nAmount > nBalance)
899899
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
900900

901-
SendMoney(pwallet, address.Get(), nAmount, false, wtx);
901+
CCoinControl no_coin_control; // This is a deprecated API
902+
SendMoney(pwallet, address.Get(), nAmount, false, wtx, no_coin_control);
902903

903904
return wtx.GetHash().GetHex();
904905
}
@@ -1033,7 +1034,7 @@ UniValue sendmany(const JSONRPCRequest& request)
10331034
CAmount nFeeRequired = 0;
10341035
int nChangePosRet = -1;
10351036
std::string strFailReason;
1036-
bool fCreated = pwallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired, nChangePosRet, strFailReason, &coin_control);
1037+
bool fCreated = pwallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired, nChangePosRet, strFailReason, coin_control);
10371038
if (!fCreated)
10381039
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
10391040
CValidationState state;

src/wallet/test/wallet_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "rpc/server.h"
1414
#include "test/test_bitcoin.h"
1515
#include "validation.h"
16+
#include "wallet/coincontrol.h"
1617
#include "wallet/test/wallet_test_fixture.h"
1718

1819
#include <boost/test/unit_test.hpp>
@@ -617,7 +618,8 @@ class ListCoinsTestingSetup : public TestChain100Setup
617618
CAmount fee;
618619
int changePos = -1;
619620
std::string error;
620-
BOOST_CHECK(wallet->CreateTransaction({recipient}, wtx, reservekey, fee, changePos, error));
621+
CCoinControl dummy;
622+
BOOST_CHECK(wallet->CreateTransaction({recipient}, wtx, reservekey, fee, changePos, error, dummy));
621623
CValidationState state;
622624
BOOST_CHECK(wallet->CommitTransaction(wtx, reservekey, nullptr, state));
623625
auto it = wallet->mapWallet.find(wtx.GetHash());

src/wallet/wallet.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,9 +2469,9 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nC
24692469

24702470
CReserveKey reservekey(this);
24712471
CWalletTx wtx;
2472-
if (!CreateTransaction(vecSend, wtx, reservekey, nFeeRet, nChangePosInOut, strFailReason, &coinControl, false))
2472+
if (!CreateTransaction(vecSend, wtx, reservekey, nFeeRet, nChangePosInOut, strFailReason, coinControl, false)) {
24732473
return false;
2474-
2474+
}
24752475
if (nChangePosInOut != -1)
24762476
tx.vout.insert(tx.vout.begin() + nChangePosInOut, wtx.tx->vout[nChangePosInOut]);
24772477

@@ -2502,7 +2502,7 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nC
25022502
}
25032503

25042504
bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRet,
2505-
int& nChangePosInOut, std::string& strFailReason, const CCoinControl* coinControl, bool sign)
2505+
int& nChangePosInOut, std::string& strFailReason, const CCoinControl& coin_control, bool sign)
25062506
{
25072507
CAmount nValue = 0;
25082508
int nChangePosRequest = nChangePosInOut;
@@ -2567,20 +2567,17 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletT
25672567
LOCK2(cs_main, cs_wallet);
25682568
{
25692569
std::vector<COutput> vAvailableCoins;
2570-
AvailableCoins(vAvailableCoins, true, coinControl);
2570+
AvailableCoins(vAvailableCoins, true, &coin_control);
25712571

25722572
// Create change script that will be used if we need change
25732573
// TODO: pass in scriptChange instead of reservekey so
25742574
// change transaction isn't always pay-to-bitcoin-address
25752575
CScript scriptChange;
25762576

25772577
// coin control: send change to custom address
2578-
if (coinControl && !boost::get<CNoDestination>(&coinControl->destChange))
2579-
scriptChange = GetScriptForDestination(coinControl->destChange);
2580-
2581-
// no coin control: send change to newly generated address
2582-
else
2583-
{
2578+
if (!boost::get<CNoDestination>(&coin_control.destChange)) {
2579+
scriptChange = GetScriptForDestination(coin_control.destChange);
2580+
} else { // no coin control: send change to newly generated address
25842581
// Note: We use a new key here to keep it from being obvious which side is the change.
25852582
// The drawback is that by not reusing a previous key, the change may be lost if a
25862583
// backup is restored, if the backup doesn't have the new private key for the change.
@@ -2654,7 +2651,7 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletT
26542651
if (pick_new_inputs) {
26552652
nValueIn = 0;
26562653
setCoins.clear();
2657-
if (!SelectCoins(vAvailableCoins, nValueToSelect, setCoins, nValueIn, coinControl))
2654+
if (!SelectCoins(vAvailableCoins, nValueToSelect, setCoins, nValueIn, &coin_control))
26582655
{
26592656
strFailReason = _("Insufficient funds");
26602657
return false;
@@ -2705,8 +2702,7 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletT
27052702
// to avoid conflicting with other possible uses of nSequence,
27062703
// and in the spirit of "smallest possible change from prior
27072704
// behavior."
2708-
bool rbf = coinControl ? coinControl->signalRbf : fWalletRbf;
2709-
const uint32_t nSequence = rbf ? MAX_BIP125_RBF_SEQUENCE : (std::numeric_limits<unsigned int>::max() - 1);
2705+
const uint32_t nSequence = coin_control.signalRbf ? MAX_BIP125_RBF_SEQUENCE : (std::numeric_limits<unsigned int>::max() - 1);
27102706
for (const auto& coin : setCoins)
27112707
txNew.vin.push_back(CTxIn(coin.outpoint,CScript(),
27122708
nSequence));
@@ -2727,15 +2723,15 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletT
27272723

27282724
// Allow to override the default confirmation target over the CoinControl instance
27292725
int currentConfirmationTarget = nTxConfirmTarget;
2730-
if (coinControl && coinControl->nConfirmTarget > 0)
2731-
currentConfirmationTarget = coinControl->nConfirmTarget;
2726+
if (coin_control.nConfirmTarget > 0)
2727+
currentConfirmationTarget = coin_control.nConfirmTarget;
27322728

27332729
// Allow to override the default fee estimate mode over the CoinControl instance
2734-
bool conservative_estimate = CalculateEstimateType(coinControl ? coinControl->m_fee_mode : FeeEstimateMode::UNSET, rbf);
2730+
bool conservative_estimate = CalculateEstimateType(coin_control.m_fee_mode, coin_control.signalRbf);
27352731

27362732
CAmount nFeeNeeded = GetMinimumFee(nBytes, currentConfirmationTarget, ::mempool, ::feeEstimator, &feeCalc, false /* ignoreGlobalPayTxFee */, conservative_estimate);
2737-
if (coinControl && coinControl->fOverrideFeeRate)
2738-
nFeeNeeded = coinControl->nFeeRate.GetFee(nBytes);
2733+
if (coin_control.fOverrideFeeRate)
2734+
nFeeNeeded = coin_control.nFeeRate.GetFee(nBytes);
27392735

27402736
// If we made it here and we aren't even able to meet the relay fee on the next pass, give up
27412737
// because we must be at the maximum allowed fee.

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
949949
* @note passing nChangePosInOut as -1 will result in setting a random position
950950
*/
951951
bool CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRet, int& nChangePosInOut,
952-
std::string& strFailReason, const CCoinControl *coinControl = NULL, bool sign = true);
952+
std::string& strFailReason, const CCoinControl& coin_control, bool sign = true);
953953
bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, CConnman* connman, CValidationState& state);
954954

955955
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& entries);

0 commit comments

Comments
 (0)