mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 20:42:59 +01:00
Merge #10165: [Wallet] Refactoring by using CInputCoin instead of std::pair
c37e32a
[Wallet] Prevent CInputCoin to be in a null state (NicolasDorier)f597dcb
[Wallet] Simplify code using CInputCoin (NicolasDorier)e78bc45
[Wallet] Decouple CInputCoin from CWalletTx (NicolasDorier)fd44ac1
[Wallet] Rename std::pair<const CWalletTx*, unsigned int> to CInputCoin (NicolasDorier) Tree-SHA512: d24361fc514a0566bce1c3953d766dfe4fece79c549cb4db2600695a4ce08e85caa61b7717812618e523a2f2a1669877dad2752ed079e2ed2d27249f9bc8590e fix error, based on 1 0 1 6 5 fix error, based on 1 0 1 6 5
This commit is contained in:
parent
d9a4dea613
commit
69b42ce671
@ -48,7 +48,7 @@ static void CoinSelection(benchmark::State& state)
|
||||
addCoin(1000 * COIN, wallet, vCoins);
|
||||
addCoin(3 * COIN, wallet, vCoins);
|
||||
|
||||
std::set<std::pair<const CWalletTx*, unsigned int> > setCoinsRet;
|
||||
std::set<CInputCoin> setCoinsRet;
|
||||
CAmount nValueRet;
|
||||
bool success = wallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, setCoinsRet, nValueRet);
|
||||
assert(success);
|
||||
|
@ -31,7 +31,7 @@ extern UniValue importwallet(const JSONRPCRequest& request);
|
||||
|
||||
std::vector<std::unique_ptr<CWalletTx>> wtxn;
|
||||
|
||||
typedef std::set<std::pair<const CWalletTx*,unsigned int> > CoinSet;
|
||||
typedef std::set<CInputCoin> CoinSet;
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
|
||||
|
||||
|
@ -76,10 +76,10 @@ const uint256 CMerkleTx::ABANDON_HASH(uint256S("00000000000000000000000000000000
|
||||
|
||||
struct CompareValueOnly
|
||||
{
|
||||
bool operator()(const std::pair<CAmount, std::pair<const CWalletTx*, unsigned int> >& t1,
|
||||
const std::pair<CAmount, std::pair<const CWalletTx*, unsigned int> >& t2) const
|
||||
bool operator()(const CInputCoin& t1,
|
||||
const CInputCoin& t2) const
|
||||
{
|
||||
return t1.first < t2.first;
|
||||
return t1.txout.nValue < t2.txout.nValue;
|
||||
}
|
||||
};
|
||||
|
||||
@ -2601,7 +2601,7 @@ void CWallet::AvailableCoins(std::vector<COutput>& vCoins, bool fOnlySafe, const
|
||||
}
|
||||
}
|
||||
|
||||
static void ApproximateBestSubset(const std::vector<std::pair<CAmount, std::pair<const CWalletTx*,unsigned int> > >& vValue, const CAmount& nTotalLower, const CAmount& nTargetValue,
|
||||
static void ApproximateBestSubset(const std::vector<CInputCoin>& vValue, const CAmount& nTotalLower, const CAmount& nTargetValue,
|
||||
std::vector<char>& vfBest, CAmount& nBest, bool fUseInstantSend = false, int iterations = 1000)
|
||||
{
|
||||
std::vector<char> vfIncluded;
|
||||
@ -2626,7 +2626,7 @@ static void ApproximateBestSubset(const std::vector<std::pair<CAmount, std::pair
|
||||
{
|
||||
for (unsigned int i = 0; i < vValue.size(); i++)
|
||||
{
|
||||
if (fUseInstantSend && nTotal + vValue[i].first > sporkManager.GetSporkValue(SPORK_5_INSTANTSEND_MAX_VALUE)*COIN) {
|
||||
if (fUseInstantSend && nTotal + vValue[i].txout.nValue > sporkManager.GetSporkValue(SPORK_5_INSTANTSEND_MAX_VALUE)*COIN) {
|
||||
continue;
|
||||
}
|
||||
//The solver here uses a randomized algorithm,
|
||||
@ -2637,7 +2637,7 @@ static void ApproximateBestSubset(const std::vector<std::pair<CAmount, std::pair
|
||||
//the selection random.
|
||||
if (nPass == 0 ? insecure_rand.rand32()&1 : !vfIncluded[i])
|
||||
{
|
||||
nTotal += vValue[i].first;
|
||||
nTotal += vValue[i].txout.nValue;
|
||||
vfIncluded[i] = true;
|
||||
if (nTotal >= nTargetValue)
|
||||
{
|
||||
@ -2647,7 +2647,7 @@ static void ApproximateBestSubset(const std::vector<std::pair<CAmount, std::pair
|
||||
nBest = nTotal;
|
||||
vfBest = vfIncluded;
|
||||
}
|
||||
nTotal -= vValue[i].first;
|
||||
nTotal -= vValue[i].txout.nValue;
|
||||
vfIncluded[i] = false;
|
||||
}
|
||||
}
|
||||
@ -2682,7 +2682,7 @@ bool less_then_denom (const COutput& out1, const COutput& out2)
|
||||
}
|
||||
|
||||
bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMine, const int nConfTheirs, const uint64_t nMaxAncestors, std::vector<COutput> vCoins,
|
||||
std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, CAmount& nValueRet, AvailableCoinsType nCoinType, bool fUseInstantSend) const
|
||||
std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, AvailableCoinsType nCoinType, bool fUseInstantSend) const
|
||||
{
|
||||
setCoinsRet.clear();
|
||||
nValueRet = 0;
|
||||
@ -2694,12 +2694,8 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
}
|
||||
|
||||
// List of values less than target
|
||||
std::pair<CAmount, std::pair<const CWalletTx*,unsigned int> > coinLowestLarger;
|
||||
coinLowestLarger.first = fUseInstantSend
|
||||
? sporkManager.GetSporkValue(SPORK_5_INSTANTSEND_MAX_VALUE)*COIN
|
||||
: std::numeric_limits<CAmount>::max();
|
||||
coinLowestLarger.second.first = NULL;
|
||||
std::vector<std::pair<CAmount, std::pair<const CWalletTx*,unsigned int> > > vValue;
|
||||
boost::optional<CInputCoin> coinLowestLarger;
|
||||
std::vector<CInputCoin> vValue;
|
||||
CAmount nTotalLower = 0;
|
||||
|
||||
random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
|
||||
@ -2741,8 +2737,10 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
continue;
|
||||
|
||||
int i = output.i;
|
||||
CAmount n = pcoin->tx->vout[i].nValue;
|
||||
if (tryDenom == 0 && CPrivateSend::IsDenominatedAmount(n)) continue; // we don't want denom values on first run
|
||||
|
||||
CInputCoin coin = CInputCoin(pcoin, i);
|
||||
|
||||
if (tryDenom == 0 && CPrivateSend::IsDenominatedAmount(coin.txout.nValue)) continue; // we don't want denom values on first run
|
||||
|
||||
if (nCoinType == ONLY_DENOMINATED) {
|
||||
// Make sure it's actually anonymized
|
||||
@ -2751,20 +2749,18 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
if (nRounds < privateSendClient.nPrivateSendRounds) continue;
|
||||
}
|
||||
|
||||
std::pair<CAmount, std::pair<const CWalletTx*,unsigned int> > coin = std::make_pair(n, std::make_pair(pcoin, i));
|
||||
|
||||
if (n == nTargetValue)
|
||||
if (coin.txout.nValue == nTargetValue)
|
||||
{
|
||||
setCoinsRet.insert(coin.second);
|
||||
nValueRet += coin.first;
|
||||
setCoinsRet.insert(coin);
|
||||
nValueRet += coin.txout.nValue;
|
||||
return true;
|
||||
}
|
||||
else if (n < nTargetValue + nMinChange)
|
||||
else if (coin.txout.nValue < nTargetValue + nMinChange)
|
||||
{
|
||||
vValue.push_back(coin);
|
||||
nTotalLower += n;
|
||||
nTotalLower += coin.txout.nValue;
|
||||
}
|
||||
else if (n < coinLowestLarger.first)
|
||||
else if (!coinLowestLarger || coin.txout.nValue < coinLowestLarger->txout.nValue)
|
||||
{
|
||||
coinLowestLarger = coin;
|
||||
}
|
||||
@ -2774,15 +2770,15 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
{
|
||||
for (unsigned int i = 0; i < vValue.size(); ++i)
|
||||
{
|
||||
setCoinsRet.insert(vValue[i].second);
|
||||
nValueRet += vValue[i].first;
|
||||
setCoinsRet.insert(vValue[i]);
|
||||
nValueRet += vValue[i].txout.nValue;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (nTotalLower < nTargetValue)
|
||||
{
|
||||
if (coinLowestLarger.second.first == NULL) // there is no input larger than nTargetValue
|
||||
if (!coinLowestLarger) // there is no input larger than nTargetValue
|
||||
{
|
||||
if (tryDenom == 0)
|
||||
// we didn't look at denom yet, let's do it
|
||||
@ -2791,8 +2787,8 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
// we looked at everything possible and didn't find anything, no luck
|
||||
return false;
|
||||
}
|
||||
setCoinsRet.insert(coinLowestLarger.second);
|
||||
nValueRet += coinLowestLarger.first;
|
||||
setCoinsRet.insert(coinLowestLarger.get());
|
||||
nValueRet += coinLowestLarger->txout.nValue;
|
||||
// There is no change in PS, so we know the fee beforehand,
|
||||
// can see if we exceeded the max fee and thus fail quickly.
|
||||
return nCoinType == ONLY_DENOMINATED ? (nValueRet - nTargetValue <= maxTxFee) : true;
|
||||
@ -2800,7 +2796,6 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
|
||||
// nTotalLower > nTargetValue
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
// Solve subset sum by stochastic approximation
|
||||
@ -2815,11 +2810,11 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
|
||||
// If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
|
||||
// or the next bigger coin is closer), return the bigger coin
|
||||
if (coinLowestLarger.second.first &&
|
||||
((nBest != nTargetValue && nBest < nTargetValue + nMinChange) || coinLowestLarger.first <= nBest))
|
||||
if (coinLowestLarger &&
|
||||
((nBest != nTargetValue && nBest < nTargetValue + nMinChange) || coinLowestLarger->txout.nValue <= nBest))
|
||||
{
|
||||
setCoinsRet.insert(coinLowestLarger.second);
|
||||
nValueRet += coinLowestLarger.first;
|
||||
setCoinsRet.insert(coinLowestLarger.get());
|
||||
nValueRet += coinLowestLarger->txout.nValue;
|
||||
}
|
||||
else {
|
||||
std::string s = "CWallet::SelectCoinsMinConf best subset: ";
|
||||
@ -2827,9 +2822,9 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
{
|
||||
if (vfBest[i])
|
||||
{
|
||||
setCoinsRet.insert(vValue[i].second);
|
||||
nValueRet += vValue[i].first;
|
||||
s += FormatMoney(vValue[i].first) + " ";
|
||||
setCoinsRet.insert(vValue[i]);
|
||||
nValueRet += vValue[i].txout.nValue;
|
||||
s += FormatMoney(vValue[i].txout.nValue) + " ";
|
||||
}
|
||||
}
|
||||
LogPrint(BCLog::SELECTCOINS, "%s - total %s\n", s, FormatMoney(nBest));
|
||||
@ -2840,7 +2835,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
|
||||
return nCoinType == ONLY_DENOMINATED ? (nValueRet - nTargetValue <= maxTxFee) : true;
|
||||
}
|
||||
|
||||
bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl, AvailableCoinsType nCoinType, bool fUseInstantSend) const
|
||||
bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl, AvailableCoinsType nCoinType, bool fUseInstantSend) const
|
||||
{
|
||||
// Note: this function should never be used for "always free" tx types like dstx
|
||||
|
||||
@ -2861,7 +2856,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
|
||||
if(nRounds < privateSendClient.nPrivateSendRounds) continue;
|
||||
}
|
||||
nValueRet += out.tx->tx->vout[out.i].nValue;
|
||||
setCoinsRet.insert(std::make_pair(out.tx, out.i));
|
||||
setCoinsRet.insert(CInputCoin(out.tx, out.i));
|
||||
|
||||
if (!coinControl->fRequireAllInputs && nValueRet >= nTargetValue) {
|
||||
// stop when we added at least one input and enough inputs to have at least nTargetValue funds
|
||||
@ -2873,7 +2868,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
|
||||
}
|
||||
|
||||
// calculate value from preset inputs and store them
|
||||
std::set<std::pair<const CWalletTx*, uint32_t> > setPresetCoins;
|
||||
std::set<CInputCoin> setPresetCoins;
|
||||
CAmount nValueFromPresetInputs = 0;
|
||||
|
||||
std::vector<COutPoint> vPresetInputs;
|
||||
@ -2895,7 +2890,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
|
||||
if (nRounds < privateSendClient.nPrivateSendRounds) continue;
|
||||
}
|
||||
nValueFromPresetInputs += pcoin->tx->vout[outpoint.n].nValue;
|
||||
setPresetCoins.insert(std::make_pair(pcoin, outpoint.n));
|
||||
setPresetCoins.insert(CInputCoin(pcoin, outpoint.n));
|
||||
} else
|
||||
return false; // TODO: Allow non-wallet inputs
|
||||
}
|
||||
@ -2903,7 +2898,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
|
||||
// remove preset inputs from vCoins
|
||||
for (std::vector<COutput>::iterator it = vCoins.begin(); it != vCoins.end() && coinControl && coinControl->HasSelected();)
|
||||
{
|
||||
if (setPresetCoins.count(std::make_pair(it->tx, it->i)))
|
||||
if (setPresetCoins.count(CInputCoin(it->tx, it->i)))
|
||||
it = vCoins.erase(it);
|
||||
else
|
||||
++it;
|
||||
@ -3435,7 +3430,7 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletT
|
||||
assert(txNew.nLockTime < LOCKTIME_THRESHOLD);
|
||||
|
||||
{
|
||||
std::set<std::pair<const CWalletTx*,unsigned int> > setCoins;
|
||||
std::set<CInputCoin> setCoins;
|
||||
std::vector<CTxDSIn> vecTxDSInTmp;
|
||||
LOCK2(cs_main, cs_wallet);
|
||||
{
|
||||
@ -3617,9 +3612,9 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletT
|
||||
// nLockTime set above actually works.
|
||||
vecTxDSInTmp.clear();
|
||||
for (const auto& coin : setCoins) {
|
||||
CTxIn txin = CTxIn(coin.first->GetHash(),coin.second,CScript(),
|
||||
CTxIn txin = CTxIn(coin.outpoint,CScript(),
|
||||
std::numeric_limits<unsigned int>::max()-1);
|
||||
vecTxDSInTmp.push_back(CTxDSIn(txin, coin.first->tx->vout[coin.second].scriptPubKey));
|
||||
vecTxDSInTmp.push_back(CTxDSIn(txin, coin.txout.scriptPubKey));
|
||||
txNew.vin.push_back(txin);
|
||||
}
|
||||
|
||||
|
@ -518,7 +518,34 @@ public:
|
||||
};
|
||||
|
||||
|
||||
class CInputCoin {
|
||||
public:
|
||||
CInputCoin(const CWalletTx* walletTx, unsigned int i)
|
||||
{
|
||||
if (!walletTx)
|
||||
throw std::invalid_argument("walletTx should not be null");
|
||||
if (i >= walletTx->tx->vout.size())
|
||||
throw std::out_of_range("The output index is out of range");
|
||||
|
||||
outpoint = COutPoint(walletTx->GetHash(), i);
|
||||
txout = walletTx->tx->vout[i];
|
||||
}
|
||||
|
||||
COutPoint outpoint;
|
||||
CTxOut txout;
|
||||
|
||||
bool operator<(const CInputCoin& rhs) const {
|
||||
return outpoint < rhs.outpoint;
|
||||
}
|
||||
|
||||
bool operator!=(const CInputCoin& rhs) const {
|
||||
return outpoint != rhs.outpoint;
|
||||
}
|
||||
|
||||
bool operator==(const CInputCoin& rhs) const {
|
||||
return outpoint == rhs.outpoint;
|
||||
}
|
||||
};
|
||||
|
||||
class COutput
|
||||
{
|
||||
@ -680,7 +707,7 @@ private:
|
||||
* all coins from coinControl are selected; Never select unconfirmed coins
|
||||
* if they are not ours
|
||||
*/
|
||||
bool SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, CAmount& nValueRet, const CCoinControl *coinControl = NULL, AvailableCoinsType nCoinType=ALL_COINS, bool fUseInstantSend = true) const;
|
||||
bool SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CCoinControl *coinControl = NULL, AvailableCoinsType nCoinType=ALL_COINS, bool fUseInstantSend = true) const;
|
||||
|
||||
CWalletDB *pwalletdbEncryption;
|
||||
|
||||
@ -869,7 +896,7 @@ public:
|
||||
* completion the coin set and corresponding actual target value is
|
||||
* assembled
|
||||
*/
|
||||
bool SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, uint64_t nMaxAncestors, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, CAmount& nValueRet, AvailableCoinsType nCoinType=ALL_COINS, bool fUseInstantSend = false) const;
|
||||
bool SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, uint64_t nMaxAncestors, std::vector<COutput> vCoins, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, AvailableCoinsType nCoinType=ALL_COINS, bool fUseInstantSend = false) const;
|
||||
|
||||
// Coin selection
|
||||
bool SelectPSInOutPairsByDenominations(int nDenom, CAmount nValueMin, CAmount nValueMax, std::vector< std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsRet);
|
||||
|
Loading…
Reference in New Issue
Block a user