Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 274 Lines • ▼ Show 20 Lines | BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) { | ||||
CWallet wallet(Params(), chain.get(), WalletLocation(), | CWallet wallet(Params(), chain.get(), WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
CWalletTx wtx(&wallet, m_coinbase_txns.back()); | CWalletTx wtx(&wallet, m_coinbase_txns.back()); | ||||
auto locked_chain = chain->lock(); | auto locked_chain = chain->lock(); | ||||
LockAssertion lock(::cs_main); | LockAssertion lock(::cs_main); | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wtx.hashBlock = ::ChainActive().Tip()->GetBlockHash(); | wtx.SetConf(CWalletTx::Status::CONFIRMED, | ||||
wtx.nIndex = 0; | ::ChainActive().Tip()->GetBlockHash(), 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(*locked_chain), Amount::zero()); | BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(*locked_chain), Amount::zero()); | ||||
// 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(); | ||||
Show All 15 Lines | if (blockTime > 0) { | ||||
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; | ||||
} | } | ||||
CWalletTx wtx(&wallet, MakeTransactionRef(tx)); | CWalletTx wtx(&wallet, MakeTransactionRef(tx)); | ||||
if (block) { | |||||
wtx.SetMerkleBranch(block->GetBlockHash(), 0); | |||||
} | |||||
{ | |||||
LOCK(cs_main); | LOCK(cs_main); | ||||
LOCK(wallet.cs_wallet); | |||||
// If transaction is already in map, to avoid inconsistencies, | |||||
// unconfirmation is needed before confirm again with different block. | |||||
std::map<TxId, CWalletTx>::iterator it = wallet.mapWallet.find(wtx.GetId()); | |||||
if (it != wallet.mapWallet.end()) { | |||||
wtx.setUnconfirmed(); | |||||
wallet.AddToWallet(wtx); | wallet.AddToWallet(wtx); | ||||
} | } | ||||
LOCK(wallet.cs_wallet); | if (block) { | ||||
wtx.SetConf(CWalletTx::Status::CONFIRMED, block->GetBlockHash(), 0); | |||||
} | |||||
wallet.AddToWallet(wtx); | |||||
return wallet.mapWallet.at(wtx.GetId()).nTimeSmart; | return wallet.mapWallet.at(wtx.GetId()).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_wallet, 1, 100, 120), 100); | ||||
▲ Show 20 Lines • Show All 159 Lines • ▼ Show 20 Lines | CWalletTx &AddTx(CRecipient recipient) { | ||||
} | } | ||||
CreateAndProcessBlock({CMutableTransaction(blocktx)}, | CreateAndProcessBlock({CMutableTransaction(blocktx)}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
auto it = wallet->mapWallet.find(tx->GetId()); | auto it = wallet->mapWallet.find(tx->GetId()); | ||||
BOOST_CHECK(it != wallet->mapWallet.end()); | BOOST_CHECK(it != wallet->mapWallet.end()); | ||||
it->second.SetMerkleBranch(::ChainActive().Tip()->GetBlockHash(), 1); | it->second.SetConf(CWalletTx::Status::CONFIRMED, | ||||
::ChainActive().Tip()->GetBlockHash(), 1); | |||||
return it->second; | return it->second; | ||||
} | } | ||||
std::unique_ptr<interfaces::Chain> m_chain = | std::unique_ptr<interfaces::Chain> m_chain = | ||||
interfaces::MakeChain(m_node, Params()); | interfaces::MakeChain(m_node, Params()); | ||||
std::unique_ptr<CWallet> wallet; | std::unique_ptr<CWallet> wallet; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |