Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 367 Lines • ▼ Show 20 Lines | BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) { | ||||
// Invalidate the cached value, add the key, and make sure a new immature | // Invalidate the cached value, add the key, and make sure a new immature | ||||
// credit amount is calculated. | // credit amount is calculated. | ||||
wtx.MarkDirty(); | wtx.MarkDirty(); | ||||
BOOST_CHECK(spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey())); | BOOST_CHECK(spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey())); | ||||
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 50 * COIN); | BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 50 * COIN); | ||||
} | } | ||||
static int64_t AddTx(CWallet &wallet, uint32_t lockTime, int64_t mockTime, | static int64_t AddTx(ChainstateManager &chainman, CWallet &wallet, | ||||
int64_t blockTime) { | uint32_t lockTime, int64_t mockTime, int64_t blockTime) { | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
CWalletTx::Confirmation confirm; | CWalletTx::Confirmation confirm; | ||||
tx.nLockTime = lockTime; | tx.nLockTime = lockTime; | ||||
SetMockTime(mockTime); | SetMockTime(mockTime); | ||||
CBlockIndex *block = nullptr; | CBlockIndex *block = nullptr; | ||||
if (blockTime > 0) { | if (blockTime > 0) { | ||||
auto inserted = | LOCK(cs_main); | ||||
::BlockIndex().emplace(BlockHash(GetRandHash()), new CBlockIndex); | auto inserted = chainman.BlockIndex().emplace(BlockHash(GetRandHash()), | ||||
new CBlockIndex); | |||||
assert(inserted.second); | assert(inserted.second); | ||||
const BlockHash &hash = inserted.first->first; | const BlockHash &hash = inserted.first->first; | ||||
block = inserted.first->second; | block = inserted.first->second; | ||||
block->nTime = blockTime; | block->nTime = blockTime; | ||||
block->phashBlock = &hash; | block->phashBlock = &hash; | ||||
confirm = {CWalletTx::Status::CONFIRMED, block->nHeight, hash, 0}; | confirm = {CWalletTx::Status::CONFIRMED, block->nHeight, hash, 0}; | ||||
} | } | ||||
// If transaction is already in map, to avoid inconsistencies, | // If transaction is already in map, to avoid inconsistencies, | ||||
// unconfirmation is needed before confirm again with different block. | // unconfirmation is needed before confirm again with different block. | ||||
return wallet | return wallet | ||||
.AddToWallet(MakeTransactionRef(tx), confirm, | .AddToWallet(MakeTransactionRef(tx), confirm, | ||||
[&](CWalletTx &wtx, bool /* new_tx */) { | [&](CWalletTx &wtx, bool /* new_tx */) { | ||||
wtx.setUnconfirmed(); | wtx.setUnconfirmed(); | ||||
return true; | return true; | ||||
}) | }) | ||||
->nTimeSmart; | ->nTimeSmart; | ||||
} | } | ||||
// Simple test to verify assignment of CWalletTx::nSmartTime value. Could be | // Simple test to verify assignment of CWalletTx::nSmartTime value. Could be | ||||
// expanded to cover more corner cases of smart time logic. | // expanded to cover more corner cases of smart time logic. | ||||
BOOST_AUTO_TEST_CASE(ComputeTimeSmart) { | BOOST_AUTO_TEST_CASE(ComputeTimeSmart) { | ||||
// New transaction should use clock time if lower than block time. | // New transaction should use clock time if lower than block time. | ||||
BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, 100, 120), 100); | BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 100, 120), 100); | ||||
// Test that updating existing transaction does not change smart time. | // Test that updating existing transaction does not change smart time. | ||||
BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, 200, 220), 100); | BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 200, 220), 100); | ||||
// New transaction should use clock time if there's no block time. | // New transaction should use clock time if there's no block time. | ||||
BOOST_CHECK_EQUAL(AddTx(m_wallet, 2, 300, 0), 300); | BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 2, 300, 0), 300); | ||||
// New transaction should use block time if lower than clock time. | // New transaction should use block time if lower than clock time. | ||||
BOOST_CHECK_EQUAL(AddTx(m_wallet, 3, 420, 400), 400); | BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 3, 420, 400), 400); | ||||
// New transaction should use latest entry time if higher than | // New transaction should use latest entry time if higher than | ||||
// min(block time, clock time). | // min(block time, clock time). | ||||
BOOST_CHECK_EQUAL(AddTx(m_wallet, 4, 500, 390), 400); | BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 4, 500, 390), 400); | ||||
// If there are future entries, new transaction should use time of the | // If there are future entries, new transaction should use time of the | ||||
// newest entry that is no more than 300 seconds ahead of the clock time. | // newest entry that is no more than 300 seconds ahead of the clock time. | ||||
BOOST_CHECK_EQUAL(AddTx(m_wallet, 5, 50, 600), 300); | BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 5, 50, 600), 300); | ||||
// Reset mock time for other tests. | // Reset mock time for other tests. | ||||
SetMockTime(0); | SetMockTime(0); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(LoadReceiveRequests) { | BOOST_AUTO_TEST_CASE(LoadReceiveRequests) { | ||||
CTxDestination dest = PKHash(); | CTxDestination dest = PKHash(); | ||||
LOCK(m_wallet.cs_wallet); | LOCK(m_wallet.cs_wallet); | ||||
▲ Show 20 Lines • Show All 432 Lines • Show Last 20 Lines |