Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 121 Lines • ▼ Show 20 Lines | |||||
// importwallet RPC would start the scan at the latest block with timestamp less | // importwallet RPC would start the scan at the latest block with timestamp less | ||||
// than or equal to key birthday. | // than or equal to key birthday. | ||||
BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) { | ||||
// Create two blocks with same timestamp to verify that importwallet rescan | // Create two blocks with same timestamp to verify that importwallet rescan | ||||
// will pick up both blocks, not just the first. | // will pick up both blocks, not just the first. | ||||
const int64_t BLOCK_TIME = chainActive.Tip()->GetBlockTimeMax() + 5; | const int64_t BLOCK_TIME = chainActive.Tip()->GetBlockTimeMax() + 5; | ||||
SetMockTime(BLOCK_TIME); | SetMockTime(BLOCK_TIME); | ||||
m_coinbase_txns.emplace_back( | m_coinbase_txns.emplace_back( | ||||
*CreateAndProcessBlock({}, | CreateAndProcessBlock({}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | ||||
.vtx[0]); | .vtx[0]); | ||||
m_coinbase_txns.emplace_back( | m_coinbase_txns.emplace_back( | ||||
*CreateAndProcessBlock({}, | CreateAndProcessBlock({}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | ||||
.vtx[0]); | .vtx[0]); | ||||
// Set key birthday to block time increased by the timestamp window, so | // Set key birthday to block time increased by the timestamp window, so | ||||
// rescan will start at the block time. | // rescan will start at the block time. | ||||
const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW; | const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW; | ||||
SetMockTime(KEY_TIME); | SetMockTime(KEY_TIME); | ||||
m_coinbase_txns.emplace_back( | m_coinbase_txns.emplace_back( | ||||
*CreateAndProcessBlock({}, | CreateAndProcessBlock({}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | ||||
.vtx[0]); | .vtx[0]); | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
// Import key into wallet and call dumpwallet to create backup file. | // Import key into wallet and call dumpwallet to create backup file. | ||||
{ | { | ||||
CWallet wallet(Params(), "dummy", CWalletDBWrapper::CreateDummy()); | CWallet wallet(Params(), "dummy", CWalletDBWrapper::CreateDummy()); | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wallet.mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = | wallet.mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = | ||||
Show All 17 Lines | // were scanned, and no prior blocks were scanned. | ||||
request.params.push_back((pathTemp / "wallet.backup").string()); | request.params.push_back((pathTemp / "wallet.backup").string()); | ||||
vpwallets[0] = &wallet; | vpwallets[0] = &wallet; | ||||
::importwallet(GetConfig(), request); | ::importwallet(GetConfig(), request); | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3); | BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3); | ||||
BOOST_CHECK_EQUAL(m_coinbase_txns.size(), 103); | BOOST_CHECK_EQUAL(m_coinbase_txns.size(), 103); | ||||
for (size_t i = 0; i < m_coinbase_txns.size(); ++i) { | for (size_t i = 0; i < m_coinbase_txns.size(); ++i) { | ||||
bool found = wallet.GetWalletTx(m_coinbase_txns[i].GetId()); | bool found = wallet.GetWalletTx(m_coinbase_txns[i]->GetId()); | ||||
bool expected = i >= 100; | bool expected = i >= 100; | ||||
BOOST_CHECK_EQUAL(found, expected); | BOOST_CHECK_EQUAL(found, expected); | ||||
} | } | ||||
} | } | ||||
SetMockTime(0); | SetMockTime(0); | ||||
vpwallets.erase(vpwallets.begin()); | vpwallets.erase(vpwallets.begin()); | ||||
} | } | ||||
// Check that GetImmatureCredit() returns a newly calculated value instead of | // Check that GetImmatureCredit() returns a newly calculated value instead of | ||||
// the cached value after a MarkDirty() call. | // the cached value after a MarkDirty() call. | ||||
// | // | ||||
// This is a regression test written to verify a bugfix for the immature credit | // This is a regression test written to verify a bugfix for the immature credit | ||||
// function. Similar tests probably should be written for the other credit and | // function. Similar tests probably should be written for the other credit and | ||||
// debit functions. | // debit functions. | ||||
BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) { | ||||
CWallet wallet(Params(), "dummy", CWalletDBWrapper::CreateDummy()); | CWallet wallet(Params(), "dummy", CWalletDBWrapper::CreateDummy()); | ||||
CWalletTx wtx(&wallet, MakeTransactionRef(m_coinbase_txns.back())); | CWalletTx wtx(&wallet, m_coinbase_txns.back()); | ||||
LOCK2(cs_main, wallet.cs_wallet); | LOCK2(cs_main, wallet.cs_wallet); | ||||
wtx.hashBlock = chainActive.Tip()->GetBlockHash(); | wtx.hashBlock = chainActive.Tip()->GetBlockHash(); | ||||
wtx.nIndex = 0; | wtx.nIndex = 0; | ||||
// Call GetImmatureCredit() once before adding the key to the wallet to | // Call GetImmatureCredit() once before adding the key to the wallet to | ||||
// cache the current immature credit amount, which is 0. | // cache the current immature credit amount, which is 0. | ||||
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), Amount::zero()); | BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), Amount::zero()); | ||||
▲ Show 20 Lines • Show All 178 Lines • Show Last 20 Lines |