Skip to content

Commit

Permalink
Wallet: added max value out filter for AvailableCoins.
Browse files Browse the repository at this point in the history
Plus cleaned AvailableCoinsByAddress to make use of it.
  • Loading branch information
furszy committed Mar 10, 2021
1 parent b9220f4 commit 2e7cdb2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,11 @@ bool CWallet::AvailableCoins(std::vector<COutput>* pCoins, // --> populates
for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++) {
const auto& output = pcoin->tx->vout[i];

// Filter by value if needed
if (coinsFilter.nMaxOutValue > 0 && output.nValue > coinsFilter.nMaxOutValue) {
continue;
}

// Filter by specific destinations if needed
if (coinsFilter.onlyFilteredDest && !coinsFilter.onlyFilteredDest->empty()) {
CTxDestination address;
Expand Down Expand Up @@ -2486,26 +2491,27 @@ std::map<CTxDestination , std::vector<COutput> > CWallet::AvailableCoinsByAddres
coinFilter.fIncludeColdStaking = true;
coinFilter.fOnlyConfirmed = fConfirmed;
coinFilter.fIncludeColdStaking = fIncludeColdStaking;
coinFilter.nMaxOutValue = maxCoinValue;
std::vector<COutput> vCoins;
// include cold
AvailableCoins(&vCoins, nullptr, coinFilter);

std::map<CTxDestination, std::vector<COutput> > mapCoins;
for (COutput& out : vCoins) {
if (maxCoinValue > 0 && out.tx->tx->vout[out.i].nValue > maxCoinValue)
continue;

for (const COutput& out : vCoins) {
CTxDestination address;
bool fColdStakeAddr = false;
if (!ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address, fColdStakeAddr)) {
bool isP2CS = out.tx->tx->vout[out.i].scriptPubKey.IsPayToColdStaking();
if (isP2CS && !fIncludeColdStaking) {
// It must never happen as the coin filtering process shouldn't had added the P2CS in the first place
assert(false);
}
// if this is a P2CS we don't have the owner key - check if we have the staking key
fColdStakeAddr = true;
if ( !out.tx->tx->vout[out.i].scriptPubKey.IsPayToColdStaking() ||
!ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address, fColdStakeAddr) )
if (!isP2CS || !ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address, fColdStakeAddr) )
continue;
}

mapCoins[address].push_back(out);
mapCoins[address].emplace_back(out);
}

return mapCoins;
Expand Down Expand Up @@ -3207,7 +3213,7 @@ std::string CWallet::CommitResult::ToString() const

CWallet::CommitResult CWallet::CommitTransaction(CTransactionRef tx, CReserveKey& opReservekey, CConnman* connman)
{
return CommitTransaction(tx, &opReservekey, connman);
return CommitTransaction(std::move(tx), &opReservekey, connman);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,15 +779,17 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
bool _fOnlySpendable,
std::set<CTxDestination>* _onlyFilteredDest,
int _minDepth,
bool _fIncludeLocked = false) :
bool _fIncludeLocked = false,
CAmount _nMaxOutValue = 0) :
fIncludeDelegated(_fIncludeDelegated),
fIncludeColdStaking(_fIncludeColdStaking),
nCoinType(_nCoinType),
fOnlyConfirmed(_fOnlyConfirmed),
fOnlySpendable(_fOnlySpendable),
onlyFilteredDest(_onlyFilteredDest),
minDepth(_minDepth),
fIncludeLocked(_fIncludeLocked) {}
fIncludeLocked(_fIncludeLocked),
nMaxOutValue(_nMaxOutValue) {}

bool fIncludeDelegated{true};
bool fIncludeColdStaking{false};
Expand All @@ -797,6 +799,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
std::set<CTxDestination>* onlyFilteredDest{nullptr};
int minDepth{0};
bool fIncludeLocked{false};
// Select outputs with value <= nMaxOutValue
CAmount nMaxOutValue{0};
};

//! >> Available coins (generic)
Expand Down

0 comments on commit 2e7cdb2

Please sign in to comment.