Skip to content

Commit

Permalink
exclude locked coins from Available to mint amount
Browse files Browse the repository at this point in the history
  • Loading branch information
flo071 committed Jul 23, 2020
1 parent 72ebb0a commit 1f27e7e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/qt/sigmadialog.cpp
Expand Up @@ -125,8 +125,9 @@ void SigmaDialog::setWalletModel(WalletModel *model)

if (model && model->getOptionsModel()) {
connect(model, SIGNAL(balanceChanged(CAmount, CAmount, CAmount, CAmount, CAmount, CAmount, CAmount)),
this, SLOT(updateAvailableToMintBalance(CAmount)));
updateAvailableToMintBalance(model->getBalance());
this, SLOT(updateMintableBalance()));
connect(model, SIGNAL(updateMintable()), this, SLOT(updateMintableBalance()));
updateMintableBalance();
connect(model, SIGNAL(notifySigmaChanged(const std::vector<CSigmaEntry>, const std::vector<CSigmaEntry>)),
this, SLOT(updateCoins(const std::vector<CSigmaEntry>, const std::vector<CSigmaEntry>)));
model->checkSigmaAmount(true);
Expand Down Expand Up @@ -441,6 +442,11 @@ void SigmaDialog::updateAvailableToMintBalance(const CAmount& balance)
ui->availableAmount->setText(formattedBalance);
}

void SigmaDialog::updateMintableBalance()
{
updateAvailableToMintBalance(this->walletModel->getBalance(NULL, true));
}

// Coin Control: copy label "Quantity" to clipboard
void SigmaDialog::coinControlClipboardQuantity()
{
Expand Down
1 change: 1 addition & 0 deletions src/qt/sigmadialog.h
Expand Up @@ -72,6 +72,7 @@ private Q_SLOTS:
void on_sendButton_clicked();
void removeEntry(SendCoinsEntry* entry);
void updateAvailableToMintBalance(const CAmount& balance);
void updateMintableBalance();
void updateCoins(const std::vector<CSigmaEntry>& spendable, const std::vector<CSigmaEntry>& pending);

Q_SIGNALS:
Expand Down
16 changes: 14 additions & 2 deletions src/qt/walletmodel.cpp
Expand Up @@ -59,7 +59,7 @@ WalletModel::~WalletModel()
unsubscribeFromCoreSignals();
}

CAmount WalletModel::getBalance(const CCoinControl *coinControl) const
CAmount WalletModel::getBalance(const CCoinControl *coinControl, bool fExcludeLocked) const
{
if (coinControl)
{
Expand All @@ -73,7 +73,7 @@ CAmount WalletModel::getBalance(const CCoinControl *coinControl) const
return nBalance;
}

return wallet->GetBalance();
return wallet->GetBalance(fExcludeLocked);
}

CAmount WalletModel::getStake() const
Expand Down Expand Up @@ -732,6 +732,16 @@ void WalletModel::listCoins(std::map<QString, std::vector<COutput> >& mapCoins,
COutput out(&wallet->mapWallet[outpoint.hash], outpoint.n, nDepth, true, true);
if (outpoint.n < out.tx->vout.size() && wallet->IsMine(out.tx->vout[outpoint.n]) == ISMINE_SPENDABLE)
vCoins.push_back(out);

if(nCoinType == ALL_COINS){
// We are now taking ALL_COINS to mean everything sans mints
if(out.tx->vout[out.i].scriptPubKey.IsZerocoinMint() || out.tx->vout[out.i].scriptPubKey.IsSigmaMint())
continue;
} else if(nCoinType == ONLY_MINTS){
// Do not consider anything other than mints
if(!(out.tx->vout[out.i].scriptPubKey.IsZerocoinMint() || out.tx->vout[out.i].scriptPubKey.IsSigmaMint()))
continue;
}
}

BOOST_FOREACH(const COutput& out, vCoins)
Expand Down Expand Up @@ -765,12 +775,14 @@ void WalletModel::lockCoin(COutPoint& output)
{
LOCK2(cs_main, wallet->cs_wallet);
wallet->LockCoin(output);
Q_EMIT updateMintable();
}

void WalletModel::unlockCoin(COutPoint& output)
{
LOCK2(cs_main, wallet->cs_wallet);
wallet->UnlockCoin(output);
Q_EMIT updateMintable();
}

void WalletModel::listLockedCoins(std::vector<COutPoint>& vOutpts)
Expand Down
4 changes: 3 additions & 1 deletion src/qt/walletmodel.h
Expand Up @@ -137,7 +137,7 @@ class WalletModel : public QObject
TransactionTableModel *getTransactionTableModel();
RecentRequestsTableModel *getRecentRequestsTableModel();

CAmount getBalance(const CCoinControl *coinControl = NULL) const;
CAmount getBalance(const CCoinControl *coinControl = NULL, bool fExcludeLocked = false) const;
CAmount getStake() const;
CAmount getUnconfirmedBalance() const;
CAmount getImmatureBalance() const;
Expand Down Expand Up @@ -274,6 +274,8 @@ class WalletModel : public QObject
void balanceChanged(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance, const CAmount& stake,
const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance);

void updateMintable();

// Encryption status of wallet changed
void encryptionStatusChanged(int status);

Expand Down
17 changes: 12 additions & 5 deletions src/wallet/wallet.cpp
Expand Up @@ -2090,7 +2090,7 @@ CAmount CWalletTx::GetImmatureStakeCredit(bool fUseCache) const
return 0;
}

CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const {
CAmount CWalletTx::GetAvailableCredit(bool fUseCache, bool fExcludeLocked) const {
if (pwallet == 0)
return 0;

Expand All @@ -2099,7 +2099,7 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const {
return 0;

// We cannot use cache if vout contains mints due to it will not update when it spend
if (fUseCache && fAvailableCreditCached && !IsZerocoinMint() && !IsSigmaMint())
if (fUseCache && fAvailableCreditCached && !IsZerocoinMint() && !IsSigmaMint() && !fExcludeLocked)
return nAvailableCreditCached;

CAmount nCredit = 0;
Expand All @@ -2108,14 +2108,21 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const {
if (!pwallet->IsSpent(hashTx, i)) {
const CTxOut &txout = vout[i];
bool isPrivate = txout.scriptPubKey.IsZerocoinMint() || txout.scriptPubKey.IsSigmaMint();
nCredit += isPrivate ? 0 : pwallet->GetCredit(txout, ISMINE_SPENDABLE);
bool condition = isPrivate;
if (fExcludeLocked)
condition = (isPrivate || pwallet->IsLockedCoin(hashTx, i));
nCredit += condition ? 0 : pwallet->GetCredit(txout, ISMINE_SPENDABLE);
if (!MoneyRange(nCredit))
throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
}
}

nAvailableCreditCached = nCredit;
fAvailableCreditCached = true;

if (fExcludeLocked)
fAvailableCreditCached = false;

return nCredit;
}

Expand Down Expand Up @@ -2311,14 +2318,14 @@ void CWallet::ResendWalletTransactions(int64_t nBestBlockTime) {
*/


CAmount CWallet::GetBalance() const {
CAmount CWallet::GetBalance(bool fExcludeLocked) const {
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) {
const CWalletTx *pcoin = &(*it).second;
if (pcoin->IsTrusted())
nTotal += pcoin->GetAvailableCredit();
nTotal += pcoin->GetAvailableCredit(true, fExcludeLocked);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/wallet/wallet.h
Expand Up @@ -439,7 +439,7 @@ class CWalletTx : public CMerkleTx
CAmount GetCredit(const isminefilter& filter) const;
CAmount GetImmatureCredit(bool fUseCache=true) const;
CAmount GetImmatureStakeCredit(bool fUseCache=true) const;
CAmount GetAvailableCredit(bool fUseCache=true) const;
CAmount GetAvailableCredit(bool fUseCache=true, bool fExcludeLocked = false) const;
CAmount GetImmatureWatchOnlyCredit(const bool& fUseCache=true) const;
CAmount GetAvailableWatchOnlyCredit(const bool& fUseCache=true) const;
CAmount GetAnonymizedCredit(bool fUseCache=true) const;
Expand Down Expand Up @@ -836,7 +836,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
void ResendWalletTransactions(int64_t nBestBlockTime);
std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime);
//noirnode
CAmount GetBalance() const;
CAmount GetBalance(bool fExcludeLocked = false) const;
CAmount GetUnconfirmedBalance() const;
CAmount GetImmatureBalance() const;
CAmount GetStake() const;
Expand Down

0 comments on commit 1f27e7e

Please sign in to comment.