Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 588 Lines • ▼ Show 20 Lines | // were scanned, and no prior blocks were scanned. | ||||
CWallet wallet(Params()); | CWallet wallet(Params()); | ||||
JSONRPCRequest request; | JSONRPCRequest request; | ||||
request.params.setArray(); | request.params.setArray(); | ||||
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); | |||||
BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3); | BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3); | ||||
BOOST_CHECK_EQUAL(coinbaseTxns.size(), 103); | BOOST_CHECK_EQUAL(coinbaseTxns.size(), 103); | ||||
for (size_t i = 0; i < coinbaseTxns.size(); ++i) { | for (size_t i = 0; i < coinbaseTxns.size(); ++i) { | ||||
bool found = wallet.GetWalletTx(coinbaseTxns[i].GetId()); | bool found = wallet.GetWalletTx(coinbaseTxns[i].GetId()); | ||||
bool expected = i >= 100; | bool expected = i >= 100; | ||||
BOOST_CHECK_EQUAL(found, expected); | BOOST_CHECK_EQUAL(found, expected); | ||||
} | } | ||||
} | } | ||||
Show All 28 Lines | |||||
static int64_t AddTx(CWallet &wallet, uint32_t lockTime, int64_t mockTime, | static int64_t AddTx(CWallet &wallet, uint32_t lockTime, int64_t mockTime, | ||||
int64_t blockTime) { | int64_t blockTime) { | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.nLockTime = lockTime; | tx.nLockTime = lockTime; | ||||
SetMockTime(mockTime); | SetMockTime(mockTime); | ||||
CBlockIndex *block = nullptr; | CBlockIndex *block = nullptr; | ||||
if (blockTime > 0) { | if (blockTime > 0) { | ||||
LOCK(cs_main); | |||||
auto inserted = mapBlockIndex.emplace(GetRandHash(), new CBlockIndex); | auto inserted = mapBlockIndex.emplace(GetRandHash(), new CBlockIndex); | ||||
assert(inserted.second); | assert(inserted.second); | ||||
const uint256 &hash = inserted.first->first; | const uint256 &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) { | if (block) { | ||||
wtx.SetMerkleBranch(block, 0); | wtx.SetMerkleBranch(block, 0); | ||||
} | } | ||||
wallet.AddToWallet(wtx); | wallet.AddToWallet(wtx); | ||||
LOCK(wallet.cs_wallet); | |||||
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) { | ||||
CWallet wallet(Params()); | CWallet wallet(Params()); | ||||
Show All 18 Lines | BOOST_AUTO_TEST_CASE(ComputeTimeSmart) { | ||||
BOOST_CHECK_EQUAL(AddTx(wallet, 5, 50, 600), 300); | BOOST_CHECK_EQUAL(AddTx(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 = CKeyID(); | CTxDestination dest = CKeyID(); | ||||
LOCK(pwalletMain->cs_wallet); | |||||
pwalletMain->AddDestData(dest, "misc", "val_misc"); | pwalletMain->AddDestData(dest, "misc", "val_misc"); | ||||
pwalletMain->AddDestData(dest, "rr0", "val_rr0"); | pwalletMain->AddDestData(dest, "rr0", "val_rr0"); | ||||
pwalletMain->AddDestData(dest, "rr1", "val_rr1"); | pwalletMain->AddDestData(dest, "rr1", "val_rr1"); | ||||
auto values = pwalletMain->GetDestValues("rr"); | auto values = pwalletMain->GetDestValues("rr"); | ||||
BOOST_CHECK_EQUAL(values.size(), 2); | BOOST_CHECK_EQUAL(values.size(), 2); | ||||
BOOST_CHECK_EQUAL(values[0], "val_rr0"); | BOOST_CHECK_EQUAL(values[0], "val_rr0"); | ||||
BOOST_CHECK_EQUAL(values[1], "val_rr1"); | BOOST_CHECK_EQUAL(values[1], "val_rr1"); | ||||
Show All 26 Lines | CWalletTx &AddTx(CRecipient recipient) { | ||||
CReserveKey reservekey(wallet.get()); | CReserveKey reservekey(wallet.get()); | ||||
Amount fee; | Amount fee; | ||||
int changePos = -1; | int changePos = -1; | ||||
std::string error; | std::string error; | ||||
BOOST_CHECK(wallet->CreateTransaction({recipient}, wtx, reservekey, fee, | BOOST_CHECK(wallet->CreateTransaction({recipient}, wtx, reservekey, fee, | ||||
changePos, error)); | changePos, error)); | ||||
CValidationState state; | CValidationState state; | ||||
BOOST_CHECK(wallet->CommitTransaction(wtx, reservekey, nullptr, state)); | BOOST_CHECK(wallet->CommitTransaction(wtx, reservekey, nullptr, state)); | ||||
LOCK(wallet->cs_wallet); | |||||
auto it = wallet->mapWallet.find(wtx.GetId()); | auto it = wallet->mapWallet.find(wtx.GetId()); | ||||
BOOST_CHECK(it != wallet->mapWallet.end()); | BOOST_CHECK(it != wallet->mapWallet.end()); | ||||
CreateAndProcessBlock({CMutableTransaction(*it->second.tx)}, | CreateAndProcessBlock({CMutableTransaction(*it->second.tx)}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | ||||
it->second.SetMerkleBranch(chainActive.Tip(), 1); | it->second.SetMerkleBranch(chainActive.Tip(), 1); | ||||
return it->second; | return it->second; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines |