Skip to content

Commit

Permalink
[Model] Connect fIncludeDelegations to CWallet::CreateTransaction
Browse files Browse the repository at this point in the history
Be sure to exclude p2cs coins in MN wizard and send-delegation
  • Loading branch information
random-zebra committed Mar 14, 2020
1 parent 4715915 commit d633b04
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/qt/pivx/coldstakingwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ void ColdStakingWidget::onSendClicked(){
QList<SendCoinsRecipient> recipients;
recipients.append(dest);

// Prepare transaction for getting txFee earlier
// Prepare transaction for getting txFee earlier (exlude delegated coins)
WalletModelTransaction currentTransaction(recipients);
WalletModel::SendCoinsReturn prepareStatus = walletModel->prepareTransaction(currentTransaction, CoinControlDialog::coinControl);
WalletModel::SendCoinsReturn prepareStatus = walletModel->prepareTransaction(currentTransaction, CoinControlDialog::coinControl, false);

// process prepareStatus and on error generate message shown to user
GuiTransactionsUtils::ProcessSendCoinsReturnAndInform(
Expand Down
3 changes: 2 additions & 1 deletion src/qt/pivx/masternodewizarddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ bool MasterNodeWizardDialog::createMN()
WalletModelTransaction currentTransaction(recipients);
WalletModel::SendCoinsReturn prepareStatus;

prepareStatus = walletModel->prepareTransaction(currentTransaction);
// no coincontrol, no P2CS delegations
prepareStatus = walletModel->prepareTransaction(currentTransaction, nullptr, false);

QString returnMsg = "Unknown error";
// process prepareStatus and on error generate message shown to user
Expand Down
6 changes: 3 additions & 3 deletions src/qt/pivx/send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ void SendWidget::onResetCustomOptions(bool fRefreshAmounts)
CoinControlDialog::coinControl->SetNull();
ui->btnChangeAddress->setActive(false);
ui->btnCoinControl->setActive(false);
if (!ui->checkBoxDelegations->isVisible()) ui->checkBoxDelegations->setVisible(true);
if (ui->checkBoxDelegations->isChecked()) ui->checkBoxDelegations->setChecked(false);
if (fRefreshAmounts) {
refreshAmounts();
Expand Down Expand Up @@ -347,12 +346,13 @@ void SendWidget::onSendClicked()
}
}

bool SendWidget::send(QList<SendCoinsRecipient> recipients){
bool SendWidget::send(QList<SendCoinsRecipient> recipients)
{
// prepare transaction for getting txFee earlier
WalletModelTransaction currentTransaction(recipients);
WalletModel::SendCoinsReturn prepareStatus;

prepareStatus = walletModel->prepareTransaction(currentTransaction, CoinControlDialog::coinControl);
prepareStatus = walletModel->prepareTransaction(currentTransaction, CoinControlDialog::coinControl, fDelegationsChecked);

// process prepareStatus and on error generate message shown to user
GuiTransactionsUtils::ProcessSendCoinsReturnAndInform(
Expand Down
8 changes: 4 additions & 4 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CAmount WalletModel::getBalance(const CCoinControl* coinControl, bool fIncludeDe
if (coinControl) {
CAmount nBalance = 0;
std::vector<COutput> vCoins;
wallet->AvailableCoins(&vCoins, true, coinControl);
wallet->AvailableCoins(&vCoins, coinControl, fIncludeDelegated);
for (const COutput& out : vCoins)
if (out.fSpendable)
nBalance += out.tx->vout[out.i].nValue;
Expand Down Expand Up @@ -364,7 +364,7 @@ bool WalletModel::updateAddressBookLabels(const CTxDestination& dest, const std:
return false;
}

WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction& transaction, const CCoinControl* coinControl)
WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction& transaction, const CCoinControl* coinControl, bool fIncludeDelegations)
{
CAmount total = 0;
QList<SendCoinsRecipient> recipients = transaction.getRecipients();
Expand Down Expand Up @@ -441,7 +441,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
return DuplicateAddress;
}

CAmount nBalance = getBalance(coinControl);
CAmount nBalance = getBalance(coinControl, fIncludeDelegations);

if (total > nBalance) {
return AmountExceedsBalance;
Expand Down Expand Up @@ -473,7 +473,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
recipients[0].inputType,
recipients[0].useSwiftTX,
0,
true);
fIncludeDelegations);
transaction.setTransactionFee(nFeeRequired);

if (recipients[0].useSwiftTX && newTx->GetValueOut() > sporkManager.GetSporkValue(SPORK_5_MAX_VALUE) * COIN) {
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class WalletModel : public QObject
const CWalletTx* getTx(uint256 id);

// prepare transaction for getting txfee before sending coins
SendCoinsReturn prepareTransaction(WalletModelTransaction& transaction, const CCoinControl* coinControl = NULL);
SendCoinsReturn prepareTransaction(WalletModelTransaction& transaction, const CCoinControl* coinControl = NULL, bool fIncludeDelegations = true);

// Send coins to a list of recipients
SendCoinsReturn sendCoins(WalletModelTransaction& transaction);
Expand Down
10 changes: 9 additions & 1 deletion src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,15 @@ UniValue listunspent(const UniValue& params, bool fHelp)
std::vector<COutput> vecOutputs;
assert(pwalletMain != NULL);
LOCK2(cs_main, pwalletMain->cs_wallet);
pwalletMain->AvailableCoins(&vecOutputs, false, NULL, false, ALL_COINS, false, nWatchonlyConfig);
pwalletMain->AvailableCoins(&vecOutputs,
nullptr, // coin control
true, // include delegated
false, // include cold staking
ALL_COINS, // coin type
false, // only confirmed
false, // include zero value
false, // use IX
nWatchonlyConfig);
for (const COutput& out : vecOutputs) {
if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth)
continue;
Expand Down
50 changes: 35 additions & 15 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,11 @@ bool CWallet::GetMasternodeVinAndKeys(CTxIn& txinRet, CPubKey& pubKeyRet, CKey&

// Find possible candidates (remove delegated)
std::vector<COutput> vPossibleCoins;
AvailableCoins(&vPossibleCoins, true, NULL, false, ONLY_10000, false, 1, false, false);
AvailableCoins(&vPossibleCoins,
nullptr, // coin control
false, // fIncludeDelegated
false, // fIncludeColdStaking
ONLY_10000); // coin type

if (vPossibleCoins.empty()) {
LogPrintf("CWallet::GetMasternodeVinAndKeys -- Could not locate any valid masternode vin\n");
Expand Down Expand Up @@ -1912,16 +1916,16 @@ void CWallet::GetAvailableP2CSCoins(std::vector<COutput>& vCoins) const {
/**
* populate vCoins with vector of available COutputs.
*/
bool CWallet::AvailableCoins(
std::vector<COutput>* pCoins,
bool fOnlyConfirmed,
const CCoinControl* coinControl,
bool fIncludeZeroValue,
AvailableCoinsType nCoinType,
bool fUseIX,
int nWatchonlyConfig,
bool fIncludeColdStaking,
bool fIncludeDelegated) const
bool CWallet::AvailableCoins(std::vector<COutput>* pCoins, // --> populates when != nullptr
const CCoinControl* coinControl, // Default: nullptr
bool fIncludeDelegated, // Default: true
bool fIncludeColdStaking, // Default: false
AvailableCoinsType nCoinType, // Default: ALL_COINS
bool fOnlyConfirmed, // Default: true
bool fIncludeZeroValue, // Default: false
bool fUseIX, // Default: false
int nWatchonlyConfig // Default: 1
) const
{
if (pCoins) pCoins->clear();
const bool fCoinsSelected = (coinControl != nullptr) && coinControl->HasSelected();
Expand Down Expand Up @@ -2004,8 +2008,13 @@ bool CWallet::AvailableCoins(
std::map<CBitcoinAddress, std::vector<COutput> > CWallet::AvailableCoinsByAddress(bool fConfirmed, CAmount maxCoinValue)
{
std::vector<COutput> vCoins;
// include cold and delegated coins
AvailableCoins(&vCoins, fConfirmed, nullptr, false, ALL_COINS, false, 1, true, true);
// include cold
AvailableCoins(&vCoins,
nullptr, // coin control
true, // fIncludeDelegated
true, // fIncludeColdStaking
ALL_COINS, // coin type
fConfirmed); // only confirmed

std::map<CBitcoinAddress, std::vector<COutput> > mapCoins;
for (COutput out : vCoins) {
Expand Down Expand Up @@ -2090,7 +2099,11 @@ bool CWallet::StakeableCoins(std::vector<COutput>* pCoins)
const bool fIncludeCold = (sporkManager.IsSporkActive(SPORK_17_COLDSTAKING_ENFORCEMENT) &&
GetBoolArg("-coldstaking", true));

return AvailableCoins(pCoins, true, nullptr, false, STAKEABLE_COINS, false, 1, fIncludeCold, false);
return AvailableCoins(pCoins,
nullptr, // coin control
false, // fIncludeDelegated
fIncludeCold, // fIncludeColdStaking
STAKEABLE_COINS); // coin type
}

bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet) const
Expand Down Expand Up @@ -2204,7 +2217,14 @@ bool CWallet::SelectCoinsToSpend(const CAmount& nTargetValue, std::set<std::pair
{
// Note: this function should never be used for "always free" tx types like dstx
std::vector<COutput> vCoins;
AvailableCoins(&vCoins, true, coinControl, false, coin_type, useIX, 1, fIncludeColdStaking, fIncludeDelegated);
AvailableCoins(&vCoins,
coinControl,
fIncludeDelegated,
fIncludeColdStaking,
coin_type,
true, // fOnlyConfirmed
false, // fIncludeZeroValue
useIX);

// coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
if (coinControl && coinControl->HasSelected()) {
Expand Down
13 changes: 11 additions & 2 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,18 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
bool CanSupportFeature(enum WalletFeature wf);

//! >> Available coins (generic)
bool AvailableCoins(std::vector<COutput>* pCoins, bool fOnlyConfirmed = true, const CCoinControl* coinControl = NULL, bool fIncludeZeroValue = false, AvailableCoinsType nCoinType = ALL_COINS, bool fUseIX = false, int nWatchonlyConfig = 1, bool fIncludeColdStaking=false, bool fIncludeDelegated=true) const;
bool AvailableCoins(std::vector<COutput>* pCoins, // --> populates when != nullptr
const CCoinControl* coinControl = nullptr,
bool fIncludeDelegated = true,
bool fIncludeColdStaking = false,
AvailableCoinsType nCoinType = ALL_COINS,
bool fOnlyConfirmed = true,
bool fIncludeZeroValue = false,
bool fUseIX = false,
int nWatchonlyConfig = 1
) const;
//! >> Available coins (spending)
bool SelectCoinsToSpend(const CAmount& nTargetValue, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl = NULL, AvailableCoinsType coin_type = ALL_COINS, bool useIX = true, bool fIncludeColdStaking=false, bool fIncludeDelegated=true) const;
bool SelectCoinsToSpend(const CAmount& nTargetValue, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl = nullptr, AvailableCoinsType coin_type = ALL_COINS, bool useIX = true, bool fIncludeColdStaking = false, bool fIncludeDelegated = true) const;
bool SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet) const;
//! >> Available coins (staking)
bool StakeableCoins(std::vector<COutput>* pCoins = nullptr);
Expand Down

0 comments on commit d633b04

Please sign in to comment.