Merge pull request #134 from UdjinM6/fix_creating_denominated

fixing pre-mix process of creation denominated amounts
This commit is contained in:
evan82 2015-01-27 10:57:35 -07:00
commit 1598d8e7a6
4 changed files with 75 additions and 109 deletions

View File

@ -1394,7 +1394,6 @@ void CDarkSendPool::CompletedTransaction(bool error, std::string lastMessageNew)
// To avoid race conditions, we'll only let DS run once per block
cachedLastSuccess = chainActive.Tip()->nHeight;
splitUpInARow = 0;
}
lastMessage = lastMessageNew;
@ -1420,9 +1419,9 @@ bool CDarkSendPool::DoAutomaticDenominating(bool fDryRun, bool ready)
if(fMasterNode) return false;
if(state == POOL_STATUS_ERROR || state == POOL_STATUS_SUCCESS) return false;
if(chainActive.Tip()->nHeight-cachedLastSuccess < minBlockSpacing) {
LogPrintf("CDarkSendPool::DoAutomaticDenominating - Last successful darksend was too recent\n");
strAutoDenomResult = "Last successful darksend was too recent";
if(chainActive.Tip()->nHeight - cachedLastSuccess < minBlockSpacing) {
LogPrintf("CDarkSendPool::DoAutomaticDenominating - Last successful darksend action was too recent\n");
strAutoDenomResult = "Last successful darksend action was too recent";
return false;
}
if(!fEnableDarksend) {
@ -1451,14 +1450,14 @@ bool CDarkSendPool::DoAutomaticDenominating(bool fDryRun, bool ready)
// ** find the coins we'll use
std::vector<CTxIn> vCoins;
std::vector<COutput> vCoins2;
int64_t nValueMin = 0.01*COIN;
int64_t nValueMin = CENT;
int64_t nValueIn = 0;
// should not be less than fees in DARKSEND_FEE + few (lets say 5) smallest denoms
int64_t nLowestDenom = DARKSEND_FEE + darkSendDenominations[darkSendDenominations.size() - 1]*5;
// if there is no DS fee yet
if(!pwalletMain->HasDarksendFeeInputs())
// if there are no DS collateral inputs yet
if(!pwalletMain->HasCollateralInputs())
// should have some additional amount for them
nLowestDenom += (DARKSEND_COLLATERAL*4)+DARKSEND_FEE*2;
@ -1467,22 +1466,24 @@ bool CDarkSendPool::DoAutomaticDenominating(bool fDryRun, bool ready)
// if balanceNeedsAnonymized is more than pool max, take the pool max
if(nBalanceNeedsAnonymized > DARKSEND_POOL_MAX) nBalanceNeedsAnonymized = DARKSEND_POOL_MAX;
// if balanceNeedsAnonymized is more than (non-anonymized - COIN), take (non-anonymized - COIN)
int64_t nBalanceNotYetAnonymized = pwalletMain->GetBalance() - pwalletMain->GetAnonymizedBalance() - COIN;
// if balanceNeedsAnonymized is more than non-anonymized, take non-anonymized
int64_t nBalanceNotYetAnonymized = pwalletMain->GetBalance() - pwalletMain->GetAnonymizedBalance();
if(nBalanceNeedsAnonymized > nBalanceNotYetAnonymized) nBalanceNeedsAnonymized = nBalanceNotYetAnonymized;
if(nBalanceNeedsAnonymized < nLowestDenom)
{
if(nBalanceNeedsAnonymized > 0)
nBalanceNeedsAnonymized = nLowestDenom;
else
{
// if(nBalanceNeedsAnonymized > nValueMin)
// nBalanceNeedsAnonymized = nLowestDenom;
// else
// {
LogPrintf("DoAutomaticDenominating : No funds detected in need of denominating \n");
strAutoDenomResult = "No funds detected in need of denominating";
return false;
}
// }
}
if (fDebug) LogPrintf("DoAutomaticDenominating : nLowestDenom=%d, nBalanceNeedsAnonymized=%d\n", nLowestDenom, nBalanceNeedsAnonymized);
// select coins that should be given to the pool
if (!pwalletMain->SelectCoinsDark(nValueMin, nBalanceNeedsAnonymized, vCoins, nValueIn, 0, nDarksendRounds))
{
@ -1494,16 +1495,16 @@ bool CDarkSendPool::DoAutomaticDenominating(bool fDryRun, bool ready)
if(!fDryRun) return CreateDenominated(nBalanceNeedsAnonymized);
return true;
} else {
LogPrintf("DoAutomaticDenominating : No funds detected in need of denominating (2)\n");
strAutoDenomResult = "No funds detected in need of denominating (2)";
LogPrintf("DoAutomaticDenominating : Can't denominate - no compatible inputs left\n");
strAutoDenomResult = "Can't denominate - no compatible inputs left";
return false;
}
}
//check to see if we have the fee sized inputs, it requires these
if(!pwalletMain->HasDarksendFeeInputs()){
if(!fDryRun) SplitUpMoney(true);
//check to see if we have the collateral sized inputs, it requires these
if(!pwalletMain->HasCollateralInputs()){
if(!fDryRun) MakeCollateralAmounts();
return true;
}
@ -1717,31 +1718,15 @@ bool CDarkSendPool::SendRandomPaymentToSelf()
}
// Split up large inputs or create fee sized inputs
bool CDarkSendPool::SplitUpMoney(bool justCollateral)
bool CDarkSendPool::MakeCollateralAmounts()
{
if((chainActive.Tip()->nHeight - lastSplitUpBlock) < 10){
LogPrintf("SplitUpMoney - Too soon to split up again\n");
return false;
}
if(splitUpInARow >= 2){
LogPrintf("Error: Darksend SplitUpMoney was called multiple times in a row. This should not happen. Please submit a detailed explanation of the steps it took to create this error and submit to evan@darkcoin.io. \n");
fEnableDarksend = false;
return false;
}
// should split up to remaining amount only...
int64_t nTotalBalance = pwalletMain->GetDenominatedBalance(false);
int64_t nTotalBalance = pwalletMain->GetBalance();
int64_t nSplitBalance = nAnonymizeDarkcoinAmount*COIN - pwalletMain->GetDenominatedBalance();
if(nSplitBalance > nTotalBalance) nSplitBalance = nTotalBalance;
// ...or up to 1 DRK if it's just collateral
if(justCollateral && nSplitBalance > 1*COIN) nSplitBalance = 1*COIN;
int64_t nTotalOut = 0;
lastSplitUpBlock = chainActive.Tip()->nHeight;
LogPrintf("DoAutomaticDenominating: Split up large input (justCollateral %d):\n", justCollateral);
LogPrintf(" -- nSplitBalance %d\n", nSplitBalance);
LogPrintf(" -- denom %d \n", pwalletMain->GetDenominatedBalance(false));
LogPrintf("DoAutomaticDenominating: MakeCollateralAmounts: nSplitBalance %d nTotalBalance %d\n", nSplitBalance, pwalletMain->GetDenominatedBalance(false));
// make our change address
CReserveKey reservekey(pwalletMain);
@ -1756,54 +1741,37 @@ bool CDarkSendPool::SplitUpMoney(bool justCollateral)
std::string strFail = "";
vector< pair<CScript, int64_t> > vecSend;
int64_t a = 1*COIN;
// ****** Add fees ************ /
vecSend.push_back(make_pair(scriptChange, (DARKSEND_COLLATERAL*2)+DARKSEND_FEE));
nTotalOut += (DARKSEND_COLLATERAL*2)+DARKSEND_FEE;
vecSend.push_back(make_pair(scriptChange, (DARKSEND_COLLATERAL*2)+DARKSEND_FEE));
nTotalOut += (DARKSEND_COLLATERAL*2)+DARKSEND_FEE;
// ****** Add outputs in bases of two from 1 darkcoin *** /
if(!justCollateral){
bool continuing = true;
while(continuing){
if(nTotalOut + a < nSplitBalance){
//LogPrintf(" nTotalOut %d, added %d\n", nTotalOut, a);
vecSend.push_back(make_pair(scriptChange, a));
nTotalOut += a;
} else {
continuing = false;
}
a = a * 2;
}
}
if((justCollateral && nTotalOut <= 0.1*COIN) || vecSend.size() < 1) {
LogPrintf("SplitUpMoney: Not enough outputs to make a transaction\n");
return false;
}
if((!justCollateral && nTotalOut <= 1*COIN) || vecSend.size() < 1){
LogPrintf("SplitUpMoney: Not enough outputs to make a transaction\n");
if(nTotalOut > nSplitBalance) {
LogPrintf("MakeCollateralAmounts: Not enough balance to split\n");
return false;
}
CCoinControl *coinControl=NULL;
bool success = pwalletMain->CreateTransaction(vecSend, wtx, reservekey,
nFeeRet, strFail, coinControl, justCollateral ? ALL_COINS : ONLY_NONDENOMINATED);
// try to use non-denominated and not mn-like funds
bool success = pwalletMain->CreateTransaction(vecSend, wtx, reservekey,
nFeeRet, strFail, coinControl, ONLY_NONDENOMINATED_NOTMN);
if(!success){
LogPrintf("SplitUpMoney: Error - %s\n", strFail.c_str());
return false;
// if we failed (most likeky not enough funds), try to use denominated instead -
// MN-like funds should not be touched in any case and we can't mix denominated without collaterals anyway
success = pwalletMain->CreateTransaction(vecSend, wtx, reservekey,
nFeeRet, strFail, coinControl, ONLY_DENOMINATED);
if(!success){
LogPrintf("MakeCollateralAmounts: Error - %s\n", strFail.c_str());
return false;
}
}
pwalletMain->CommitTransaction(wtx, reservekey);
// use the same cachedLastSuccess as for DS mixinx to prevent race
if(pwalletMain->CommitTransaction(wtx, reservekey))
cachedLastSuccess = chainActive.Tip()->nHeight;
LogPrintf("SplitUpMoney Success: tx %s\n", wtx.GetHash().GetHex().c_str());
LogPrintf("MakeCollateralAmounts Success: tx %s\n", wtx.GetHash().GetHex().c_str());
splitUpInARow++;
return true;
}
@ -1824,8 +1792,8 @@ bool CDarkSendPool::CreateDenominated(int64_t nTotalValue)
vector< pair<CScript, int64_t> > vecSend;
int64_t nValueLeft = nTotalValue;
// ****** Add fees ************ /
if(!pwalletMain->HasDarksendFeeInputs()) {
// ****** Add collateral outputs ************ /
if(!pwalletMain->HasCollateralInputs()) {
vecSend.push_back(make_pair(scriptChange, (DARKSEND_COLLATERAL*2)+DARKSEND_FEE));
nValueLeft -= (DARKSEND_COLLATERAL*2)+DARKSEND_FEE;
vecSend.push_back(make_pair(scriptChange, (DARKSEND_COLLATERAL*2)+DARKSEND_FEE));
@ -1861,13 +1829,15 @@ bool CDarkSendPool::CreateDenominated(int64_t nTotalValue)
CCoinControl *coinControl=NULL;
bool success = pwalletMain->CreateTransaction(vecSend, wtx, reservekey,
nFeeRet, strFail, coinControl, ONLY_NONDENOMINATED);
nFeeRet, strFail, coinControl, ONLY_NONDENOMINATED_NOTMN);
if(!success){
LogPrintf("CreateDenominated: Error - %s\n", strFail.c_str());
return false;
}
pwalletMain->CommitTransaction(wtx, reservekey);
// use the same cachedLastSuccess as for DS mixinx to prevent race
if(pwalletMain->CommitTransaction(wtx, reservekey))
cachedLastSuccess = chainActive.Tip()->nHeight;
LogPrintf("CreateDenominated Success: tx %s\n", wtx.GetHash().GetHex().c_str());
@ -2129,6 +2099,9 @@ bool CDarkSendSigner::VerifyMessage(CPubKey pubkey, vector<unsigned char>& vchSi
return false;
}
if (fDebug && pubkey2.GetID() != pubkey.GetID())
LogPrintf("CDarkSendSigner::VerifyMessage -- keys don't match: %s %s", pubkey2.GetID().ToString(), pubkey.GetID().ToString());
return (pubkey2.GetID() == pubkey.GetID());
}

View File

@ -259,8 +259,6 @@ public:
int64_t sessionTotalValue; //used for autoDenom
std::vector<CTransaction> vecSessionCollateral;
int lastSplitUpBlock;
int splitUpInARow; // how many splits we've done since a success?
int cachedLastSuccess;
int cachedNumBlocks; //used for the overview screen
int minBlockSpacing; //required blocks between mixes
@ -279,11 +277,9 @@ public:
/* DarkSend uses collateral addresses to trust parties entering the pool
to behave themselves. If they don't it takes their money. */
lastSplitUpBlock = 0;
cachedLastSuccess = 0;
cachedNumBlocks = 0;
unitTest = false;
splitUpInARow = 0;
txCollateral = CTransaction();
minBlockSpacing = 1;
nDsqCount = 0;
@ -423,7 +419,7 @@ public:
// used for liquidity providers
bool SendRandomPaymentToSelf();
// split up large inputs or make fee sized inputs
bool SplitUpMoney(bool justCollateral=false);
bool MakeCollateralAmounts();
bool CreateDenominated(int64_t nTotalValue);
// get the denominations for a list of outputs (returns a bitshifted integer)
int GetDenominations(const std::vector<CTxOut>& vout);

View File

@ -1237,12 +1237,13 @@ void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const
CTxIn vin = CTxIn(out.tx->GetHash(), out.i);
int rounds = GetInputDarksendRounds(vin);
if(rounds >= 0) found = true;
} else if(coin_type == ONLY_NONDENOMINATED) {
} else if(coin_type == ONLY_NONDENOMINATED || coin_type == ONLY_NONDENOMINATED_NOTMN) {
found = true;
if (IsCollateralAmount(pcoin->vout[i].nValue)) continue; // do not use collateral amounts
BOOST_FOREACH(int64_t d, darkSendDenominations)
if(pcoin->vout[i].nValue == d)
found = false;
if(found && coin_type == ONLY_NONDENOMINATED_NOTMN) found = (pcoin->vout[i].nValue != 1000*COIN); // do not use MN funds
} else {
found = true;
}
@ -1459,9 +1460,6 @@ bool CWallet::SelectCoins(int64_t nTargetValue, set<pair<const CWalletTx*,unsign
//if we're doing only denominated, we need to round up to the nearest .1DRK
if(coin_type == ONLY_DENOMINATED){
// denominate our funds
std::vector<CTxOut> vOut;
// Make outputs by looping through denominations, from large to small
BOOST_FOREACH(int64_t v, darkSendDenominations)
{
@ -1593,7 +1591,7 @@ bool CWallet::SelectCoinsDark(int64_t nValueMin, int64_t nValueMax, std::vector<
nValueRet = 0;
vector<COutput> vCoins;
AvailableCoins(vCoins, false, coinControl, nDarksendRoundsMin < 0 ? ONLY_NONDENOMINATED : ONLY_DENOMINATED);
AvailableCoins(vCoins, false, coinControl, nDarksendRoundsMin < 0 ? ONLY_NONDENOMINATED_NOTMN : ONLY_DENOMINATED);
set<pair<const CWalletTx*,unsigned int> > setCoinsRet2;
@ -1643,13 +1641,8 @@ bool CWallet::SelectCoinsCollateral(std::vector<CTxIn>& setCoinsRet, int64_t& nV
BOOST_FOREACH(const COutput& out, vCoins)
{
// collateral inputs will always be a multiple of DARSEND_COLLATERAL, up to five
if(
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 5)+DARKSEND_FEE ||
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 4)+DARKSEND_FEE ||
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 3)+DARKSEND_FEE ||
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 2)+DARKSEND_FEE ||
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 1)+DARKSEND_FEE
){
if(IsCollateralAmount(out.tx->vout[out.i].nValue))
{
CTxIn vin = CTxIn(out.tx->GetHash(),out.i);
vin.prevPubKey = out.tx->vout[out.i].scriptPubKey; // the inputs PubKey
@ -1692,27 +1685,27 @@ int CWallet::CountInputsWithAmount(int64_t nInputAmount)
return nTotal;
}
bool CWallet::HasDarksendFeeInputs() const
bool CWallet::HasCollateralInputs() const
{
CCoinControl *coinControl=NULL;
vector<COutput> vCoins;
AvailableCoins(vCoins, false, coinControl, ALL_COINS);
bool found_collateral = false;
int nFound = 0;
BOOST_FOREACH(const COutput& out, vCoins)
{
if(
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 5)+DARKSEND_FEE ||
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 4)+DARKSEND_FEE ||
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 3)+DARKSEND_FEE ||
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 2)+DARKSEND_FEE ||
out.tx->vout[out.i].nValue == (DARKSEND_COLLATERAL * 1)+DARKSEND_FEE
) found_collateral = true;
if(IsCollateralAmount(out.tx->vout[out.i].nValue)) nFound++;
}
return nFound > 1; // should have more than one just in case
}
return found_collateral;
bool CWallet::IsCollateralAmount(int64_t nInputAmount) const
{
return nInputAmount == (DARKSEND_COLLATERAL * 5)+DARKSEND_FEE ||
nInputAmount == (DARKSEND_COLLATERAL * 4)+DARKSEND_FEE ||
nInputAmount == (DARKSEND_COLLATERAL * 3)+DARKSEND_FEE ||
nInputAmount == (DARKSEND_COLLATERAL * 2)+DARKSEND_FEE ||
nInputAmount == (DARKSEND_COLLATERAL * 1)+DARKSEND_FEE;
}
bool CWallet::SelectCoinsWithoutDenomination(int64_t nTargetValue, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const
@ -1852,6 +1845,8 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
strFailReason = _("Insufficient funds");
else if (coin_type == ONLY_NONDENOMINATED)
strFailReason = _("Unable to locate enough Darksend non-denominated funds for this transaction");
else if (coin_type == ONLY_NONDENOMINATED_NOTMN)
strFailReason = _("Unable to locate enough Darksend non-denominated funds for this transaction that are not equal 1000 DRK");
else
strFailReason = _("Unable to locate enough Darksend denominated funds for this transaction");
@ -2023,7 +2018,7 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, std:
if (!wtxNew.AcceptToMemoryPool(false))
{
// This must not fail. The transaction has already been signed and recorded.
LogPrintf("CommitTransaction() : Error: Transaction not valid");
LogPrintf("CommitTransaction() : Error: Transaction not valid\n");
return false;
}
wtxNew.RelayWalletTransaction(strCommand);

View File

@ -55,7 +55,8 @@ enum AvailableCoinsType
{
ALL_COINS = 1,
ONLY_DENOMINATED = 2,
ONLY_NONDENOMINATED = 3
ONLY_NONDENOMINATED = 3,
ONLY_NONDENOMINATED_NOTMN = 4 // ONLY_NONDENOMINATED and not 1000 DRK at the same time
};
@ -136,7 +137,8 @@ public:
bool SelectCoinsByDenominations(int nDenom, int64_t nValueMin, int64_t nValueMax, std::vector<CTxIn>& setCoinsRet, vector<COutput>& vCoins, int64_t& nValueRet, int nDarksendRoundsMin, int nDarksendRoundsMax);
bool SelectCoinsDarkDenominated(int64_t nTargetValue, std::vector<CTxIn>& setCoinsRet, int64_t& nValueRet) const;
bool SelectCoinsMasternode(CTxIn& vin, int64_t& nValueRet, CScript& pubScript) const;
bool HasDarksendFeeInputs() const;
bool HasCollateralInputs() const;
bool IsCollateralAmount(int64_t nInputAmount) const;
int CountInputsWithAmount(int64_t nInputAmount);
bool SelectCoinsCollateral(std::vector<CTxIn>& setCoinsRet, int64_t& nValueRet) const ;