15 #include <test/test_fabcoin.h> 20 #include <boost/test/unit_test.hpp> 37 #define RANDOM_REPEATS 5 39 std::vector<std::unique_ptr<CWalletTx>>
wtxn;
45 static const
CWallet testWallet;
48 static
void add_coin(const
CAmount& nValue,
int nAge = 6*24,
bool fIsFromMe = false,
int nInput=0)
50 static int nextLockTime = 0;
53 tx.
vout.resize(nInput+1);
54 tx.
vout[nInput].nValue = nValue;
60 std::unique_ptr<CWalletTx> wtx(
new CWalletTx(&testWallet, MakeTransactionRef(std::move(tx))));
63 wtx->fDebitCached =
true;
64 wtx->nDebitCached = 1;
66 COutput output(wtx.get(), nInput, nAge,
true ,
true ,
true );
67 vCoins.push_back(output);
68 wtxn.emplace_back(std::move(wtx));
71 static void empty_wallet(
void)
79 std::pair<CoinSet::iterator, CoinSet::iterator> ret = mismatch(
a.begin(),
a.end(), b.begin());
80 return ret.first ==
a.end() && ret.second == b.end();
85 CoinSet setCoinsRet, setCoinsRet2;
117 add_coin(10*CENT, 3,
true);
206 add_coin(MIN_CHANGE * 1 / 10);
207 add_coin(MIN_CHANGE * 2 / 10);
208 add_coin(MIN_CHANGE * 3 / 10);
209 add_coin(MIN_CHANGE * 4 / 10);
210 add_coin(MIN_CHANGE * 5 / 10);
218 add_coin(1111*MIN_CHANGE);
225 add_coin(MIN_CHANGE * 6 / 10);
226 add_coin(MIN_CHANGE * 7 / 10);
235 for (
int j = 0; j < 20; j++)
236 add_coin(50000 * COIN);
247 add_coin(MIN_CHANGE * 5 / 10);
248 add_coin(MIN_CHANGE * 6 / 10);
249 add_coin(MIN_CHANGE * 7 / 10);
250 add_coin(1111 * MIN_CHANGE);
257 add_coin(MIN_CHANGE * 4 / 10);
258 add_coin(MIN_CHANGE * 6 / 10);
259 add_coin(MIN_CHANGE * 8 / 10);
260 add_coin(1111 * MIN_CHANGE);
267 add_coin(MIN_CHANGE * 5 / 100);
268 add_coin(MIN_CHANGE * 1);
269 add_coin(MIN_CHANGE * 100);
282 for (
CAmount amt=1500; amt < COIN; amt*=10) {
285 for (uint16_t j = 0; j < 676; j++)
288 if (amt - 2000 < MIN_CHANGE) {
290 uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
291 CAmount returnValue = amt * returnSize;
304 for (
int i2 = 0; i2 < 100; i2++)
311 BOOST_CHECK(!equal_sets(setCoinsRet, setCoinsRet2));
320 if (equal_sets(setCoinsRet, setCoinsRet2))
323 BOOST_CHECK_NE(fails, RANDOM_REPEATS);
341 if (equal_sets(setCoinsRet, setCoinsRet2))
344 BOOST_CHECK_NE(fails, RANDOM_REPEATS);
360 for (
int i = 0; i < 1000; i++)
361 add_coin(1000 * COIN);
371 static void AddKey(
CWallet& wallet,
const CKey& key)
392 AddKey(wallet, coinbaseKey);
405 AddKey(wallet, coinbaseKey);
421 key.
pushKV(
"timestamp", 0);
438 strprintf(
"[{\"success\":false,\"error\":{\"code\":-1,\"message\":\"Rescan failed for key with creation " 439 "timestamp %d. There was an error reading a block from time %d, which is after or within %d " 440 "seconds of key creation, and could contain transactions pertaining to the key. As a result, " 441 "transactions and coins using this key may not appear in the wallet. This error could be caused " 442 "by pruning or data corruption (see fabcoind log for details) and could be dealt with by " 443 "downloading and rescanning the relevant blocks (see -reindex and -rescan " 444 "options).\"}},{\"success\":true}]",
462 coinbaseTxns.emplace_back(*CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
463 coinbaseTxns.emplace_back(*CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
467 const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW;
469 coinbaseTxns.emplace_back(*CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
475 wallet.
mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
476 wallet.
AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
498 for (
size_t i = 0; i < coinbaseTxns.size(); ++i) {
499 bool found = wallet.
GetWalletTx(coinbaseTxns[i].GetHash());
500 bool expected = i >= 800;
518 CWalletTx wtx(&wallet, MakeTransactionRef(coinbaseTxns.back()));
530 wallet.
AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
534 static int64_t AddTx(
CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
543 const uint256& hash = inserted.first->first;
544 block = inserted.first->second;
545 block->
nTime = blockTime;
549 CWalletTx wtx(&wallet, MakeTransactionRef(tx));
590 pwalletMain->
AddDestData(dest,
"misc",
"val_misc");
610 AddKey(*wallet, coinbaseKey);
644 std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString();
651 BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
664 BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
668 std::vector<COutput> available;
671 for (
const auto& group : list) {
672 for (
const auto& coin : group.second) {
683 BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
CBlockIndex * ScanForWalletTransactions(CBlockIndex *pindexStart, bool fUpdate=false)
Scan the block chain (starting in pindexStart) for transactions from or to us.
bool error(const char *fmt, const Args &...args)
CDiskBlockPos GetBlockPos() const
void SetMerkleBranch(const CBlockIndex *pIndex, int posInBlock)
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
std::vector< CWalletRef > vpwallets
void AvailableCoins(std::vector< COutput > &vCoins, bool fOnlySafe=true, const CCoinControl *coinControl=nullptr, const CAmount &nMinimumAmount=1, const CAmount &nMaximumAmount=MAX_MONEY, const CAmount &nMinimumSumAmount=MAX_MONEY, const uint64_t &nMaximumCount=0, const int &nMinDepth=0, const int &nMaxDepth=9999999) const
populate vCoins with vector of available COutputs.
CAmount GetAvailableBalance(const CCoinControl *coinControl=nullptr) const
CCriticalSection cs_wallet
const uint256 & GetHash() const
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Adds a key to the store, and saves it to disk.
void SetMockTime(int64_t nMockTimeIn)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
void Flush(bool fShutdown)
bool SelectCoinsMinConf(const CAmount &nTargetValue, int nConfMine, int nConfTheirs, uint64_t nMaxAncestors, std::vector< COutput > vCoins, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet) const
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
std::hash for asio::adress
assert(len-trim+(2 *lenIndices)<=WIDTH)
void MarkDirty()
make sure balances are recalculated
std::map< CTxDestination, CKeyMetadata > mapKeyMetadata
CAmount GetImmatureCredit(bool fUseCache=true) const
CAmount GetImmatureBalance() const
CWalletTx & AddTx(CRecipient recipient)
bool CreateTransaction(const std::vector< CRecipient > &vecSend, CWalletTx &wtxNew, CReserveKey &reservekey, CAmount &nFeeRet, int &nChangePosInOut, std::string &strFailReason, const CCoinControl &coin_control, bool sign=true, CAmount nGasFee=0, bool hasSender=false)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
int64_t CAmount
Amount in lius (Can be negative)
int64_t GetBlockTimeMax() const
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
An instance of this class represents one database.
bool push_back(const UniValue &val)
void PruneOneBlockFile(const int fileNumber)
Mark one block file as pruned.
DBErrors LoadWallet(bool &fFirstRunRet)
BOOST_AUTO_TEST_CASE(coin_selection_tests)
std::unique_ptr< CWallet > wallet
CPubKey GetPubKey() const
Compute the public key from a private key.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
bool pushKV(const std::string &key, const UniValue &val)
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
An outpoint - a combination of a transaction hash and an index n into its vout.
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
std::vector< CTxOut > vout
void LockCoin(const COutPoint &output)
bool AddToWallet(const CWalletTx &wtxIn, bool fFlushOnClose=true)
A transaction with a bunch of additional info that only the owner cares about.
UniValue importmulti(const JSONRPCRequest &request)
std::map< CTxDestination, std::vector< COutput > > ListCoins() const
Return list of available coins and locked coins grouped by non-change output address.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
UniValue dumpwallet(const JSONRPCRequest &request)
Capture information about block/transaction validation.
const CWalletTx * GetWalletTx(const uint256 &hash) const
#define BOOST_FIXTURE_TEST_SUITE(a, b)
A key allocated from the key pool.
Testing setup and teardown for wallet.
#define BOOST_CHECK_EQUAL(v1, v2)
The block chain is a tree shaped structure starting with the genesis block at the root...
A reference to a CKey: the Hash160 of its serialized public key.
#define BOOST_AUTO_TEST_SUITE_END()
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
std::map< uint256, CWalletTx > mapWallet
A mutable version of CTransaction.
std::vector< std::string > GetDestValues(const std::string &prefix) const
Get all destination values matching a prefix.
std::set< CInputCoin > CoinSet
An encapsulated private key.
bool AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
Adds a destination data tuple to the store, and saves it to disk.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
BOOST_FIXTURE_TEST_CASE(rescan, TestChain800Setup)
uint256 GetBlockHash() const
UniValue importwallet(const JSONRPCRequest &request)
#define BOOST_CHECK(expr)
std::vector< std::unique_ptr< CWalletTx > > wtxn
bool CommitTransaction(CWalletTx &wtxNew, CReserveKey &reservekey, CConnman *connman, CValidationState &state)
Call after CreateTransaction unless you want to abort.
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex