11 #include "validation.h"
19 #include <boost/test/unit_test.hpp>
31 #define RANDOM_REPEATS 5
33 std::vector<std::unique_ptr<CWalletTx>>
wtxn;
35 typedef std::set<std::pair<const CWalletTx*,unsigned int> >
CoinSet;
40 static std::vector<COutput> vCoins;
42 static void add_coin(
const CAmount& nValue,
int nAge = 6*24,
bool fIsFromMe =
false,
int nInput=0)
44 static int nextLockTime = 0;
47 tx.
vout.resize(nInput+1);
48 tx.
vout[nInput].nValue = nValue;
54 std::unique_ptr<CWalletTx> wtx(
new CWalletTx(&testWallet, MakeTransactionRef(std::move(tx))));
58 COutput output(wtx.get(), nInput, nAge,
true ,
true ,
true );
59 vCoins.push_back(output);
60 wtxn.emplace_back(std::move(wtx));
63 static void empty_wallet(
void)
71 std::pair<CoinSet::iterator, CoinSet::iterator> ret = mismatch(a.begin(), a.end(), b.begin());
72 return ret.first == a.end() && ret.second == b.end();
77 CoinSet setCoinsRet, setCoinsRet2;
109 add_coin(10*CENT, 3,
true);
198 add_coin(0.1*MIN_CHANGE);
199 add_coin(0.2*MIN_CHANGE);
200 add_coin(0.3*MIN_CHANGE);
201 add_coin(0.4*MIN_CHANGE);
202 add_coin(0.5*MIN_CHANGE);
210 add_coin(1111*MIN_CHANGE);
217 add_coin(0.6*MIN_CHANGE);
218 add_coin(0.7*MIN_CHANGE);
227 for (
int j = 0; j < 20; j++)
228 add_coin(50000 * COIN);
239 add_coin(0.5 * CENT);
240 add_coin(0.6 * CENT);
241 add_coin(0.7 * CENT);
242 add_coin(1111 * CENT);
249 add_coin(0.4 * MIN_CHANGE);
250 add_coin(0.6 * MIN_CHANGE);
251 add_coin(0.8 * MIN_CHANGE);
252 add_coin(1111 * MIN_CHANGE);
259 add_coin(0.05 * MIN_CHANGE);
260 add_coin(1 * MIN_CHANGE);
261 add_coin(100 * MIN_CHANGE);
274 for (
CAmount amt=1500; amt < COIN; amt*=10) {
277 for (uint16_t j = 0; j < 676; j++)
280 if (amt - 2000 < MIN_CHANGE) {
282 uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
283 CAmount returnValue = amt * returnSize;
296 for (
int i2 = 0; i2 < 100; i2++)
303 BOOST_CHECK(!equal_sets(setCoinsRet, setCoinsRet2));
312 if (equal_sets(setCoinsRet, setCoinsRet2))
333 if (equal_sets(setCoinsRet, setCoinsRet2))
342 static void AddKey(
CWallet& wallet,
const CKey& key)
364 AddKey(wallet, coinbaseKey);
400 key.
pushKV(
"timestamp", 0);
408 key.
pushKV(
"timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
431 coinbaseTxns.emplace_back(*CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
435 const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW;
437 coinbaseTxns.emplace_back(*CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
439 std::string backup_file = (SetDataDir(
"importwallet_rescan") /
"wallet.backup").
string();
446 wallet.
mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
447 wallet.
AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
471 for (
size_t i = 0; i < coinbaseTxns.size(); ++i) {
472 bool found = wallet.
GetWalletTx(coinbaseTxns[i].GetHash());
473 bool expected = i >= 100;
499 block.
vtx.emplace_back(wtx.
tx);
503 fakeIndex->
pprev = pprev;
523 const std::vector<CTxOut>& vout,
530 CWalletTx wtx(&wallet, MakeTransactionRef(tx));
538 std::vector<CTxIn> vin;
545 const CAmount& nCreditSpendable,
546 const CAmount& nAvailableCredit,
548 const CAmount& nDebitSpendable)
int64_t CAmount
Amount in PIV (Can be negative)
static std::unique_ptr< BerkeleyDatabase > CreateDummy()
Return object for accessing dummy database with no read/write capabilities.
static std::unique_ptr< BerkeleyDatabase > CreateMock()
Return object for accessing temporary in-memory database.
unsigned int nSize
number of used bytes of block file
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
FlatFilePos GetBlockPos() const
uint256 GetBlockHash() const
int64_t GetBlockTimeMax() const
int nHeight
height of the entry in the chain. The genesis block has height 0
const uint256 * phashBlock
pointer to the hash of the block, if any. memory is owned by this CBlockIndex
CBlockIndex * Tip(bool fProofOfStake=false) const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
An encapsulated private key.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
CPubKey GetPubKey() const
Compute the public key from a private key.
An outpoint - a combination of a transaction hash and an index n into its vout.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
The basic transaction that is broadcasted on the network and contained in blocks.
const uint256 & GetHash() const
An input of a transaction.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
bool exists(uint256 hash) const
indexed_transaction_set mapTx
An output of a transaction.
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
std::map< uint256, CWalletTx > mapWallet
void SetLastBlockProcessed(const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Set last block processed height, currently only use in unit test.
std::map< CKeyID, CKeyMetadata > mapKeyMetadata
A transaction with a bunch of additional info that only the owner cares about.
const uint256 & GetHash() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool push_back(const UniValue &val)
bool pushKV(const std::string &key, const UniValue &val)
RAII object to check and reserve a wallet rescan.
BOOST_AUTO_TEST_SUITE_END()
DBErrors LoadWallet(bool &fFirstRunRet)
bool SelectCoinsMinConf(const CAmount &nTargetValue, int nConfMine, int nConfTheirs, uint64_t nMaxAncestors, std::vector< COutput > vCoins, std::set< std::pair< const CWalletTx *, unsigned int > > &setCoinsRet, CAmount &nValueRet) const
Select coins until nTargetValue is reached.
CAmount GetImmatureBalance() const
CAmount GetUnconfirmedBalance(isminetype filter=ISMINE_SPENDABLE_ALL) const
CallResult< CTxDestination > getNewAddress(const std::string &addressLabel, const std::string purpose, const CChainParams::Base58Type addrType=CChainParams::PUBKEY_ADDRESS)
bool IsAmountCached(AmountType type, const isminefilter &filter) const
CBlockIndex * ScanForWalletTransactions(CBlockIndex *pindexStart, CBlockIndex *pindexStop, const WalletRescanReserver &reserver, bool fUpdate=false, bool fromStartup=false)
Scan the block chain (starting in pindexStart) for transactions from or to us.
bool SetMinVersion(enum WalletFeature, WalletBatch *batch_in=nullptr, bool fExplicit=false)
signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVe...
bool LoadToWallet(CWalletTx &wtxIn)
CAmount GetDebit(const isminefilter &filter) const
filter decides which addresses will count towards the debit
const CWalletTx * GetWalletTx(const uint256 &hash) const
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Adds a key to the store, and saves it to disk.
CAmount GetCredit(const isminefilter &filter, bool recalculate=false) const
bool SetupSPKM(bool newKeypool=true, bool memOnly=false)
Generates hd wallet //.
CAmount GetAvailableCredit(bool fUseCache=true, const isminefilter &filter=ISMINE_SPENDABLE) const
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
RecursiveMutex cs_main
Global state.
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a PIVX scriptPubKey for the given CTxDestination.
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
A mutable version of CTransaction.
std::vector< CTxOut > vout
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
std::shared_ptr< const CTransaction > CTransactionRef
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
void SetMockTime(int64_t nMockTimeIn)
For testing.
CTxMemPool mempool(::minRelayTxFee)
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
std::vector< CWalletRef > vpwallets
@ FEATURE_PRE_SPLIT_KEYPOOL
CWalletTx & ReceiveBalanceWith(const std::vector< CTxOut > &vout, CWallet &wallet)
void removeTxFromMempool(CWalletTx &wtx)
std::set< std::pair< const CWalletTx *, unsigned int > > CoinSet
UniValue dumpwallet(const JSONRPCRequest &request)
CWalletTx & BuildAndLoadTxToWallet(const std::vector< CTxIn > &vin, const std::vector< CTxOut > &vout, CWallet &wallet)
void fakeMempoolInsertion(const CTransactionRef &wtxCredit)
UniValue importwallet(const JSONRPCRequest &request)
std::vector< std::unique_ptr< CWalletTx > > wtxn
void CheckBalances(const CWalletTx &tx, const CAmount &nCreditAll, const CAmount &nCreditSpendable, const CAmount &nAvailableCredit, const CAmount &nDebitAll, const CAmount &nDebitSpendable)
CBlockIndex * SimpleFakeMine(CWalletTx &wtx, CWallet &wallet, CBlockIndex *pprev=nullptr)
Mimic block creation.
BOOST_AUTO_TEST_CASE(coin_selection_tests)
BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
UniValue importmulti(const JSONRPCRequest &request)