Refactor SelectCoinsMinConf() and add unit tests.

AvailableCoins() makes a vector of available outputs which is then passed to SelectCoinsMinConf().  This allows unit tests to test the coin selection algorithm without having the whole blockchain available.
This commit is contained in:
Chris Moore
2012-02-27 04:19:32 -08:00
committed by Luke Dashjr
parent 882ba0e752
commit 9b0369c773
3 changed files with 343 additions and 48 deletions

View File

@@ -14,6 +14,7 @@
class CWalletTx;
class CReserveKey;
class CWalletDB;
class COutput;
/** (client) version numbers for particular wallet features */
enum WalletFeature
@@ -60,7 +61,7 @@ public:
class CWallet : public CCryptoKeyStore
{
private:
bool SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
void AvailableCoins(std::vector<COutput>& vCoins) const;
bool SelectCoins(int64 nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
CWalletDB *pwalletdbEncryption;
@@ -112,6 +113,8 @@ public:
// check whether we are allowed to upgrade (or already support) to the named feature
bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
bool SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
// keystore implementation
// Generate a new key
CPubKey GenerateNewKey();
@@ -602,6 +605,34 @@ public:
};
class COutput
{
public:
const CWalletTx *tx;
int i;
int nDepth;
COutput(const CWalletTx *txIn, int iIn, int nDepthIn)
{
tx = txIn; i = iIn; nDepth = nDepthIn;
}
std::string ToString() const
{
return strprintf("COutput(%s, %d, %d) [%s]", tx->GetHash().ToString().substr(0,10).c_str(), i, nDepth, FormatMoney(tx->vout[i].nValue).c_str());
}
void print() const
{
printf("%s\n", ToString().c_str());
}
};
/** Private key that includes an expiration date in case it never gets used. */
class CWalletKey
{