Skip to content

Commit f597dcb

Browse files
committed
[Wallet] Simplify code using CInputCoin
1 parent e78bc45 commit f597dcb

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

src/wallet/wallet.cpp

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ const uint256 CMerkleTx::ABANDON_HASH(uint256S("00000000000000000000000000000000
6565

6666
struct CompareValueOnly
6767
{
68-
bool operator()(const std::pair<CAmount, CInputCoin>& t1,
69-
const std::pair<CAmount, CInputCoin>& t2) const
68+
bool operator()(const CInputCoin& t1,
69+
const CInputCoin& t2) const
7070
{
71-
return t1.first < t2.first;
71+
return t1.txout.nValue < t2.txout.nValue;
7272
}
7373
};
7474

@@ -2032,7 +2032,7 @@ void CWallet::AvailableCoins(std::vector<COutput>& vCoins, bool fOnlySafe, const
20322032
}
20332033
}
20342034

2035-
static void ApproximateBestSubset(const std::vector<std::pair<CAmount, CInputCoin> >& vValue, const CAmount& nTotalLower, const CAmount& nTargetValue,
2035+
static void ApproximateBestSubset(const std::vector<CInputCoin>& vValue, const CAmount& nTotalLower, const CAmount& nTargetValue,
20362036
std::vector<char>& vfBest, CAmount& nBest, int iterations = 1000)
20372037
{
20382038
std::vector<char> vfIncluded;
@@ -2059,7 +2059,7 @@ static void ApproximateBestSubset(const std::vector<std::pair<CAmount, CInputCoi
20592059
//the selection random.
20602060
if (nPass == 0 ? insecure_rand.rand32()&1 : !vfIncluded[i])
20612061
{
2062-
nTotal += vValue[i].first;
2062+
nTotal += vValue[i].txout.nValue;
20632063
vfIncluded[i] = true;
20642064
if (nTotal >= nTargetValue)
20652065
{
@@ -2069,7 +2069,7 @@ static void ApproximateBestSubset(const std::vector<std::pair<CAmount, CInputCoi
20692069
nBest = nTotal;
20702070
vfBest = vfIncluded;
20712071
}
2072-
nTotal -= vValue[i].first;
2072+
nTotal -= vValue[i].txout.nValue;
20732073
vfIncluded[i] = false;
20742074
}
20752075
}
@@ -2085,9 +2085,8 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
20852085
nValueRet = 0;
20862086

20872087
// List of values less than target
2088-
std::pair<CAmount, CInputCoin> coinLowestLarger;
2089-
coinLowestLarger.first = std::numeric_limits<CAmount>::max();
2090-
std::vector<std::pair<CAmount, CInputCoin> > vValue;
2088+
CInputCoin coinLowestLarger;
2089+
std::vector<CInputCoin> vValue;
20912090
CAmount nTotalLower = 0;
20922091

20932092
random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
@@ -2106,22 +2105,21 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
21062105
continue;
21072106

21082107
int i = output.i;
2109-
CAmount n = pcoin->tx->vout[i].nValue;
21102108

2111-
std::pair<CAmount,CInputCoin> coin = std::make_pair(n, CInputCoin(pcoin, i));
2109+
CInputCoin coin = CInputCoin(pcoin, i);
21122110

2113-
if (n == nTargetValue)
2111+
if (coin.txout.nValue == nTargetValue)
21142112
{
2115-
setCoinsRet.insert(coin.second);
2116-
nValueRet += coin.first;
2113+
setCoinsRet.insert(coin);
2114+
nValueRet += coin.txout.nValue;
21172115
return true;
21182116
}
2119-
else if (n < nTargetValue + MIN_CHANGE)
2117+
else if (coin.txout.nValue < nTargetValue + MIN_CHANGE)
21202118
{
21212119
vValue.push_back(coin);
2122-
nTotalLower += n;
2120+
nTotalLower += coin.txout.nValue;
21232121
}
2124-
else if (n < coinLowestLarger.first)
2122+
else if (coinLowestLarger.IsNull() || coin.txout.nValue < coinLowestLarger.txout.nValue)
21252123
{
21262124
coinLowestLarger = coin;
21272125
}
@@ -2131,18 +2129,18 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
21312129
{
21322130
for (unsigned int i = 0; i < vValue.size(); ++i)
21332131
{
2134-
setCoinsRet.insert(vValue[i].second);
2135-
nValueRet += vValue[i].first;
2132+
setCoinsRet.insert(vValue[i]);
2133+
nValueRet += vValue[i].txout.nValue;
21362134
}
21372135
return true;
21382136
}
21392137

21402138
if (nTotalLower < nTargetValue)
21412139
{
2142-
if (coinLowestLarger.second.IsNull())
2140+
if (coinLowestLarger.IsNull())
21432141
return false;
2144-
setCoinsRet.insert(coinLowestLarger.second);
2145-
nValueRet += coinLowestLarger.first;
2142+
setCoinsRet.insert(coinLowestLarger);
2143+
nValueRet += coinLowestLarger.txout.nValue;
21462144
return true;
21472145
}
21482146

@@ -2158,25 +2156,25 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
21582156

21592157
// If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
21602158
// or the next bigger coin is closer), return the bigger coin
2161-
if (coinLowestLarger.second.IsNull() &&
2162-
((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger.first <= nBest))
2159+
if (!coinLowestLarger.IsNull() &&
2160+
((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger.txout.nValue <= nBest))
21632161
{
2164-
setCoinsRet.insert(coinLowestLarger.second);
2165-
nValueRet += coinLowestLarger.first;
2162+
setCoinsRet.insert(coinLowestLarger);
2163+
nValueRet += coinLowestLarger.txout.nValue;
21662164
}
21672165
else {
21682166
for (unsigned int i = 0; i < vValue.size(); i++)
21692167
if (vfBest[i])
21702168
{
2171-
setCoinsRet.insert(vValue[i].second);
2172-
nValueRet += vValue[i].first;
2169+
setCoinsRet.insert(vValue[i]);
2170+
nValueRet += vValue[i].txout.nValue;
21732171
}
21742172

21752173
if (LogAcceptCategory(BCLog::SELECTCOINS)) {
21762174
LogPrint(BCLog::SELECTCOINS, "SelectCoins() best subset: ");
21772175
for (unsigned int i = 0; i < vValue.size(); i++) {
21782176
if (vfBest[i]) {
2179-
LogPrint(BCLog::SELECTCOINS, "%s ", FormatMoney(vValue[i].first));
2177+
LogPrint(BCLog::SELECTCOINS, "%s ", FormatMoney(vValue[i].txout.nValue));
21802178
}
21812179
}
21822180
LogPrint(BCLog::SELECTCOINS, "total %s\n", FormatMoney(nBest));

0 commit comments

Comments
 (0)