Skip to content

Commit

Permalink
refactor coin selection for staking
Browse files Browse the repository at this point in the history
  • Loading branch information
Tranz5 committed Aug 20, 2014
1 parent ecc0857 commit d9a625a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/main.cpp
Expand Up @@ -47,7 +47,6 @@ unsigned int nStakeMaxAge = 60 * 60 * 24 * 30; // stake age of full weight - 30
unsigned int nStakeTargetSpacing = 1 * 30; // 1-minute block spacing
int64 nChainStartTime = 1371910049;
int nCoinbaseMaturity = 5;
int nCoinbaseMaturityMultipiler = 5000;
CBlockIndex* pindexGenesisBlock = NULL;
int nBestHeight = -1;
uint256 nBestChainTrust = 0;
Expand Down Expand Up @@ -2742,7 +2741,6 @@ bool LoadBlockIndex(bool fAllowNew)
nStakeMinAge = 2 * 60 * 60; // test net min age is 2 hours
nModifierInterval = 20 * 60; // test modifier interval is 20 minutes
nCoinbaseMaturity = 10; // test maturity is 10 blocks
nCoinbaseMaturityMultipiler = 1;
nStakeTargetSpacing = 1 * 60; // test block spacing is 3 minutes
}

Expand Down
25 changes: 13 additions & 12 deletions src/wallet.cpp
Expand Up @@ -1098,7 +1098,7 @@ void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const
}
}

void CWallet::AvailableCoinsMinConf(vector<COutput>& vCoins, int nConf) const
void CWallet::AvailableCoinsForStaking(vector<COutput>& vCoins, unsigned int nSpendTime) const
{
vCoins.clear();

Expand All @@ -1108,15 +1108,20 @@ void CWallet::AvailableCoinsMinConf(vector<COutput>& vCoins, int nConf) const
{
const CWalletTx* pcoin = &(*it).second;

if (!pcoin->IsFinal())
// Filtering by tx timestamp instead of block timestamp may give false positives but never false negatives
if (pcoin->nTime + nStakeMinAge > nSpendTime)
continue;

if(pcoin->GetDepthInMainChain() < nConf)
if (pcoin->GetBlocksToMaturity() > 0)
continue;

int nDepth = pcoin->GetDepthInMainChain();
if (nDepth < 1)
continue;

for (unsigned int i = 0; i < pcoin->vout.size(); i++)
if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) && pcoin->vout[i].nValue >= nMinimumInputValue)
vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
vCoins.push_back(COutput(pcoin, i, nDepth));
}
}
}
Expand Down Expand Up @@ -1353,10 +1358,10 @@ bool CWallet::SelectCoins(int64 nTargetValue, unsigned int nSpendTime, set<pair<
}

// Select some coins without random shuffle or best subset approximation
bool CWallet::SelectCoinsSimple(int64 nTargetValue, unsigned int nSpendTime, int nMinConf, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
bool CWallet::SelectCoinsForStaking(int64 nTargetValue, unsigned int nSpendTime, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
{
vector<COutput> vCoins;
AvailableCoinsMinConf(vCoins, nMinConf);
AvailableCoinsForStaking(vCoins, nSpendTime);

setCoinsRet.clear();
nValueRet = 0;
Expand All @@ -1370,10 +1375,6 @@ bool CWallet::SelectCoinsSimple(int64 nTargetValue, unsigned int nSpendTime, int
if (nValueRet >= nTargetValue)
break;

// Follow the timestamp rules
if (pcoin->nTime > nSpendTime)
continue;

int64 n = pcoin->vout[i].nValue;

pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
Expand Down Expand Up @@ -1564,7 +1565,7 @@ bool CWallet::GetStakeWeight(const CKeyStore& keystore, uint64& nMinWeight, uint
set<pair<const CWalletTx*,unsigned int> > setCoins;
int64 nValueIn = 0;

if (!SelectCoinsSimple(nBalance - nReserveBalance, GetTime(), nCoinbaseMaturity * nCoinbaseMaturityMultipiler, setCoins, nValueIn))
if (!SelectCoinsForStaking(nBalance - nReserveBalance, GetTime(), setCoins, nValueIn))
return false;

if (setCoins.empty())
Expand Down Expand Up @@ -1645,7 +1646,7 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
int64 nValueIn = 0;

// Select coins with suitable depth
if (!SelectCoinsSimple(nBalance - nReserveBalance, txNew.nTime, nCoinbaseMaturity * nCoinbaseMaturityMultipiler, setCoins, nValueIn))
if (!SelectCoinsForStaking(nBalance - nReserveBalance, txNew.nTime, setCoins, nValueIn))
return false;

if (setCoins.empty())
Expand Down
4 changes: 2 additions & 2 deletions src/wallet.h
Expand Up @@ -91,7 +91,7 @@ class CWallet : public CCryptoKeyStore
{
private:
bool SelectCoins(int64 nTargetValue, unsigned int nSpendTime, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet, const CCoinControl *coinControl=NULL) const;
bool SelectCoinsSimple(int64 nTargetValue, unsigned int nSpendTime, int nMinConf, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
bool SelectCoinsForStaking(int64 nTargetValue, unsigned int nSpendTime, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;

CWalletDB *pwalletdbEncryption;

Expand Down Expand Up @@ -179,7 +179,7 @@ class CWallet : public CCryptoKeyStore
bool CanSupportFeature(enum WalletFeature wf) { AssertLockHeld(cs_wallet); return nWalletMaxVersion >= wf; }

void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const;
void AvailableCoinsMinConf(std::vector<COutput>& vCoins, int nConf) const;
void AvailableCoinsForStaking(std::vector<COutput>& vCoins, unsigned int nSpendTime) const;
bool SelectCoinsMinConf(int64 nTargetValue, unsigned int nSpendTime, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
bool IsLockedCoin(uint256 hash, unsigned int n) const;
void LockCoin(COutPoint& output);
Expand Down

0 comments on commit d9a625a

Please sign in to comment.