15 #include <test/test_bitcoin.h> 20 #include <boost/test/unit_test.hpp> 29 static void AddKey(
CWallet& wallet,
const CKey& key)
50 AddKey(wallet, coinbaseKey);
65 AddKey(wallet, coinbaseKey);
83 key.
pushKV(
"timestamp", 0);
100 strprintf(
"[{\"success\":false,\"error\":{\"code\":-1,\"message\":\"Rescan failed for key with creation " 101 "timestamp %d. There was an error reading a block from time %d, which is after or within %d " 102 "seconds of key creation, and could contain transactions pertaining to the key. As a result, " 103 "transactions and coins using this key may not appear in the wallet. This error could be caused " 104 "by pruning or data corruption (see bsha3d log for details) and could be dealt with by " 105 "downloading and rescanning the relevant blocks (see -reindex and -rescan " 106 "options).\"}},{\"success\":true}]",
122 m_coinbase_txns.emplace_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
123 m_coinbase_txns.emplace_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
127 const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW;
129 m_coinbase_txns.emplace_back(CreateAndProcessBlock({},
GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
133 std::string backup_file = (SetDataDir(
"importwallet_rescan") /
"wallet.backup").
string();
138 LOCK(wallet->cs_wallet);
139 wallet->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
140 wallet->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
157 request.params.push_back(backup_file);
162 LOCK(wallet->cs_wallet);
165 for (
size_t i = 0; i < m_coinbase_txns.size(); ++i) {
166 bool found = wallet->GetWalletTx(m_coinbase_txns[i]->GetHash());
167 bool expected = i >= 100;
184 CWalletTx wtx(&wallet, m_coinbase_txns.back());
196 wallet.
AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
200 static int64_t AddTx(
CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
209 assert(inserted.second);
210 const uint256& hash = inserted.first->first;
211 block = inserted.first->second;
212 block->
nTime = blockTime;
216 CWalletTx wtx(&wallet, MakeTransactionRef(tx));
218 wtx.SetMerkleBranch(block, 0);
225 return wallet.mapWallet.at(wtx.GetHash()).nTimeSmart;
278 wallet->LoadWallet(firstRun);
279 AddKey(*
wallet, coinbaseKey);
308 auto it =
wallet->mapWallet.find(tx->GetHash());
319 std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString();
323 std::map<CTxDestination, std::vector<COutput>> list;
329 BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
345 BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
351 std::vector<COutput> available;
355 for (
const auto& group : list) {
356 for (
const auto& coin : group.second) {
363 std::vector<COutput> available;
374 BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
384 BOOST_CHECK(!wallet->GetKeyFromPool(pubkey,
false));
static std::unique_ptr< BerkeleyDatabase > CreateDummy()
Return object for accessing dummy database with no read/write capabilities.
CDiskBlockPos GetBlockPos() const
static std::unique_ptr< BerkeleyDatabase > CreateMock()
Return object for accessing temporary in-memory database.
CAmount GetImmatureBalance() const
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 EXCLUSIVE_LOCKS_REQUIRED(cs_main
populate vCoins with vector of available COutputs.
CAmount GetAvailableBalance(const CCoinControl *coinControl=nullptr) const
std::vector< std::string > GetDestValues(const std::string &prefix) const
Get all destination values matching a prefix.
bool AddWallet(const std::shared_ptr< CWallet > &wallet)
CPubKey GetPubKey() const
Compute the public key from a private key.
void SetMockTime(int64_t nMockTimeIn)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
std::shared_ptr< const CTransaction > CTransactionRef
CWalletTx & AddTx(CRecipient recipient)
int64_t CAmount
Amount in satoshis (Can be negative)
uint256 GetBlockHash() const
BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
bool push_back(const UniValue &val)
void PruneOneBlockFile(const int fileNumber)
Mark one block file as pruned.
std::unique_ptr< CWallet > wallet
boost::variant< CNoDestination, CKeyID, CScriptID, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
An encapsulated public key.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Adds a key to the store, and saves it to disk.
int64_t GetBlockTimeMax() const
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
std::map< CTxDestination, std::vector< COutput > > ListCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_main
Return list of available coins and locked coins grouped by non-change output address.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool pushKV(const std::string &key, const UniValue &val)
An outpoint - a combination of a transaction hash and an index n into its vout.
CCriticalSection cs_wallet
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
bool AddToWallet(const CWalletTx &wtxIn, bool fFlushOnClose=true)
RAII object to check and reserve a wallet rescan.
BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
A transaction with a bunch of additional info that only the owner cares about.
UniValue importmulti(const JSONRPCRequest &request)
CBlockIndex * ScanForWalletTransactions(CBlockIndex *pindexStart, CBlockIndex *pindexStop, const WalletRescanReserver &reserver, bool fUpdate=false)
Scan the block chain (starting in pindexStart) for transactions from or to us.
void LockCoin(const COutPoint &output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
UniValue dumpwallet(const JSONRPCRequest &request)
Capture information about block/transaction validation.
#define BOOST_FIXTURE_TEST_SUITE(a, b)
void SetWalletFlag(uint64_t flags)
set a single wallet flag
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 Hash360 of its serialized public key.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
#define BOOST_AUTO_TEST_SUITE_END()
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet)
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
bool error(const char *fmt, const Args &... args)
A mutable version of CTransaction.
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.
CChain & chainActive
The currently-connected chain of blocks (protected by cs_main).
UniValue importwallet(const JSONRPCRequest &request)
#define BOOST_CHECK(expr)
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex