Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 84 additions & 35 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
nStartTime = GetTimeMillis();
crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;

if (kMasterKey.nDeriveIterations < 25000)
kMasterKey.nDeriveIterations = 25000;

Expand Down Expand Up @@ -1201,6 +1200,23 @@ static void ApproximateBestSubset(vector<pair<int64, pair<const CWalletTx*,unsig
}
}

// TODO: find appropriate place for this sort function
// move denoms down
bool less_then_denom (const COutput& out1, const COutput& out2)
{
const CWalletTx *pcoin1 = out1.tx;
const CWalletTx *pcoin2 = out2.tx;

bool found1 = false;
bool found2 = false;
BOOST_FOREACH(int64 d, darkSendDenominations) // loop through predefined denoms
{
if(pcoin1->vout[out1.i].nValue == d) found1 = true;
if(pcoin2->vout[out2.i].nValue == d) found2 = true;
}
return (!found1 && found2);
}

bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, vector<COutput> vCoins,
set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
{
Expand All @@ -1216,52 +1232,85 @@ bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfThe

random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);

BOOST_FOREACH(COutput output, vCoins)
// move denoms down on the list
sort(vCoins.begin(), vCoins.end(), less_then_denom);

// try to find nondenom first to prevent unneeded spending of mixed coins
for (unsigned int tryDenom = 0; tryDenom < 2; tryDenom++)
{
const CWalletTx *pcoin = output.tx;
if (fDebug) LogPrintf("tryDenom: %d\n", tryDenom);
setCoinsRet.clear();
nValueRet = 0;
nTotalLower = 0;

if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
continue;
BOOST_FOREACH(COutput output, vCoins)
{
const CWalletTx *pcoin = output.tx;

int i = output.i;
int64 n = pcoin->vout[i].nValue;
// if (fDebug) LogPrintf("value %s confirms %d\n", FormatMoney(pcoin->vout[output.i].nValue).c_str(), output.nDepth);
if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
continue;

pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
int i = output.i;
int64 n = pcoin->vout[i].nValue;

if (n == nTargetValue)
{
setCoinsRet.insert(coin.second);
nValueRet += coin.first;
return true;
}
else if (n < nTargetValue + CENT)
{
vValue.push_back(coin);
nTotalLower += n;
if (tryDenom == 0) // first run?
{
bool found = false;
BOOST_FOREACH(int64 d, darkSendDenominations) // loop through predefined denoms
if(n == d)
found = true;
if (found) continue; // we don't want denom values on first run
}

pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));

if (n == nTargetValue)
{
setCoinsRet.insert(coin.second);
nValueRet += coin.first;
return true;
}
else if (n < nTargetValue + CENT)
{
vValue.push_back(coin);
nTotalLower += n;
}
else if (n < coinLowestLarger.first)
{
coinLowestLarger = coin;
}
}
else if (n < coinLowestLarger.first)

if (nTotalLower == nTargetValue)
{
coinLowestLarger = coin;
for (unsigned int i = 0; i < vValue.size(); ++i)
{
setCoinsRet.insert(vValue[i].second);
nValueRet += vValue[i].first;
}
return true;
}
}

if (nTotalLower == nTargetValue)
{
for (unsigned int i = 0; i < vValue.size(); ++i)
if (nTotalLower < nTargetValue)
{
setCoinsRet.insert(vValue[i].second);
nValueRet += vValue[i].first;
if (coinLowestLarger.second.first == NULL) // there is no input larger than nTargetValue
{
if (tryDenom == 0)
// we didn't look at denom yet, let's do it
continue;
else
// we looked at everything possible and didn't find anything, no luck
return false;
}
setCoinsRet.insert(coinLowestLarger.second);
nValueRet += coinLowestLarger.first;
return true;
}
return true;
}

if (nTotalLower < nTargetValue)
{
if (coinLowestLarger.second.first == NULL)
return false;
setCoinsRet.insert(coinLowestLarger.second);
nValueRet += coinLowestLarger.first;
return true;
// nTotalLower > nTargetValue
break;

}

// Solve subset sum by stochastic approximation
Expand Down