Skip to content

Commit 2e7cdb2

Browse files
committed
Wallet: added max value out filter for AvailableCoins.
Plus cleaned AvailableCoinsByAddress to make use of it.
1 parent b9220f4 commit 2e7cdb2

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/wallet/wallet.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,11 @@ bool CWallet::AvailableCoins(std::vector<COutput>* pCoins, // --> populates
24482448
for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++) {
24492449
const auto& output = pcoin->tx->vout[i];
24502450

2451+
// Filter by value if needed
2452+
if (coinsFilter.nMaxOutValue > 0 && output.nValue > coinsFilter.nMaxOutValue) {
2453+
continue;
2454+
}
2455+
24512456
// Filter by specific destinations if needed
24522457
if (coinsFilter.onlyFilteredDest && !coinsFilter.onlyFilteredDest->empty()) {
24532458
CTxDestination address;
@@ -2486,26 +2491,27 @@ std::map<CTxDestination , std::vector<COutput> > CWallet::AvailableCoinsByAddres
24862491
coinFilter.fIncludeColdStaking = true;
24872492
coinFilter.fOnlyConfirmed = fConfirmed;
24882493
coinFilter.fIncludeColdStaking = fIncludeColdStaking;
2494+
coinFilter.nMaxOutValue = maxCoinValue;
24892495
std::vector<COutput> vCoins;
2490-
// include cold
24912496
AvailableCoins(&vCoins, nullptr, coinFilter);
24922497

24932498
std::map<CTxDestination, std::vector<COutput> > mapCoins;
2494-
for (COutput& out : vCoins) {
2495-
if (maxCoinValue > 0 && out.tx->tx->vout[out.i].nValue > maxCoinValue)
2496-
continue;
2497-
2499+
for (const COutput& out : vCoins) {
24982500
CTxDestination address;
24992501
bool fColdStakeAddr = false;
25002502
if (!ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address, fColdStakeAddr)) {
2503+
bool isP2CS = out.tx->tx->vout[out.i].scriptPubKey.IsPayToColdStaking();
2504+
if (isP2CS && !fIncludeColdStaking) {
2505+
// It must never happen as the coin filtering process shouldn't had added the P2CS in the first place
2506+
assert(false);
2507+
}
25012508
// if this is a P2CS we don't have the owner key - check if we have the staking key
25022509
fColdStakeAddr = true;
2503-
if ( !out.tx->tx->vout[out.i].scriptPubKey.IsPayToColdStaking() ||
2504-
!ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address, fColdStakeAddr) )
2510+
if (!isP2CS || !ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address, fColdStakeAddr) )
25052511
continue;
25062512
}
25072513

2508-
mapCoins[address].push_back(out);
2514+
mapCoins[address].emplace_back(out);
25092515
}
25102516

25112517
return mapCoins;
@@ -3207,7 +3213,7 @@ std::string CWallet::CommitResult::ToString() const
32073213

32083214
CWallet::CommitResult CWallet::CommitTransaction(CTransactionRef tx, CReserveKey& opReservekey, CConnman* connman)
32093215
{
3210-
return CommitTransaction(tx, &opReservekey, connman);
3216+
return CommitTransaction(std::move(tx), &opReservekey, connman);
32113217
}
32123218

32133219
/**

src/wallet/wallet.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,15 +779,17 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
779779
bool _fOnlySpendable,
780780
std::set<CTxDestination>* _onlyFilteredDest,
781781
int _minDepth,
782-
bool _fIncludeLocked = false) :
782+
bool _fIncludeLocked = false,
783+
CAmount _nMaxOutValue = 0) :
783784
fIncludeDelegated(_fIncludeDelegated),
784785
fIncludeColdStaking(_fIncludeColdStaking),
785786
nCoinType(_nCoinType),
786787
fOnlyConfirmed(_fOnlyConfirmed),
787788
fOnlySpendable(_fOnlySpendable),
788789
onlyFilteredDest(_onlyFilteredDest),
789790
minDepth(_minDepth),
790-
fIncludeLocked(_fIncludeLocked) {}
791+
fIncludeLocked(_fIncludeLocked),
792+
nMaxOutValue(_nMaxOutValue) {}
791793

792794
bool fIncludeDelegated{true};
793795
bool fIncludeColdStaking{false};
@@ -797,6 +799,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
797799
std::set<CTxDestination>* onlyFilteredDest{nullptr};
798800
int minDepth{0};
799801
bool fIncludeLocked{false};
802+
// Select outputs with value <= nMaxOutValue
803+
CAmount nMaxOutValue{0};
800804
};
801805

802806
//! >> Available coins (generic)

0 commit comments

Comments
 (0)