Changeset View
Changeset View
Standalone View
Standalone View
src/test/coins_tests.cpp
Show First 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | |||||
public: | public: | ||||
explicit CCoinsViewCacheTest(CCoinsView *_base) : CCoinsViewCache(_base) {} | explicit CCoinsViewCacheTest(CCoinsView *_base) : CCoinsViewCache(_base) {} | ||||
void SelfTest() const { | void SelfTest() const { | ||||
// Manually recompute the dynamic usage of the whole data, and compare | // Manually recompute the dynamic usage of the whole data, and compare | ||||
// it. | // it. | ||||
size_t ret = memusage::DynamicUsage(cacheCoins); | size_t ret = memusage::DynamicUsage(cacheCoins); | ||||
size_t count = 0; | size_t count = 0; | ||||
for (CCoinsMap::iterator it = cacheCoins.begin(); | for (const auto &entry : cacheCoins) { | ||||
it != cacheCoins.end(); it++) { | ret += entry.second.coin.DynamicMemoryUsage(); | ||||
ret += it->second.coin.DynamicMemoryUsage(); | |||||
count++; | count++; | ||||
} | } | ||||
BOOST_CHECK_EQUAL(GetCacheSize(), count); | BOOST_CHECK_EQUAL(GetCacheSize(), count); | ||||
BOOST_CHECK_EQUAL(DynamicMemoryUsage(), ret); | BOOST_CHECK_EQUAL(DynamicMemoryUsage(), ret); | ||||
} | } | ||||
CCoinsMap &map() const { return cacheCoins; } | CCoinsMap &map() const { return cacheCoins; } | ||||
size_t &usage() const { return cachedCoinsUsage; } | size_t &usage() const { return cachedCoinsUsage; } | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | ||||
int cacheid = insecure_rand() % stack.size(); | int cacheid = insecure_rand() % stack.size(); | ||||
stack[cacheid]->Uncache(out); | stack[cacheid]->Uncache(out); | ||||
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out); | uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out); | ||||
} | } | ||||
// Once every 1000 iterations and at the end, verify the full cache. | // Once every 1000 iterations and at the end, verify the full cache. | ||||
if (InsecureRandRange(1000) == 1 || | if (InsecureRandRange(1000) == 1 || | ||||
i == NUM_SIMULATION_ITERATIONS - 1) { | i == NUM_SIMULATION_ITERATIONS - 1) { | ||||
for (auto it = result.begin(); it != result.end(); it++) { | for (const auto &entry : result) { | ||||
bool have = stack.back()->HaveCoin(it->first); | bool have = stack.back()->HaveCoin(entry.first); | ||||
const Coin &coin = stack.back()->AccessCoin(it->first); | const Coin &coin = stack.back()->AccessCoin(entry.first); | ||||
BOOST_CHECK(have == !coin.IsSpent()); | BOOST_CHECK(have == !coin.IsSpent()); | ||||
BOOST_CHECK(coin == it->second); | BOOST_CHECK(coin == entry.second); | ||||
if (coin.IsSpent()) { | if (coin.IsSpent()) { | ||||
missed_an_entry = true; | missed_an_entry = true; | ||||
} else { | } else { | ||||
BOOST_CHECK(stack.back()->HaveCoinInCache(it->first)); | BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first)); | ||||
found_an_entry = true; | found_an_entry = true; | ||||
} | } | ||||
} | } | ||||
for (const CCoinsViewCacheTest *test : stack) { | for (const CCoinsViewCacheTest *test : stack) { | ||||
test->SelfTest(); | test->SelfTest(); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 221 Lines • ▼ Show 20 Lines | for (int64_t i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | ||||
if (!tx.IsCoinBase()) { | if (!tx.IsCoinBase()) { | ||||
utxoset.insert(tx.vin[0].prevout); | utxoset.insert(tx.vin[0].prevout); | ||||
} | } | ||||
} | } | ||||
// Once every 1000 iterations and at the end, verify the full cache. | // Once every 1000 iterations and at the end, verify the full cache. | ||||
if (InsecureRandRange(1000) == 1 || | if (InsecureRandRange(1000) == 1 || | ||||
i == NUM_SIMULATION_ITERATIONS - 1) { | i == NUM_SIMULATION_ITERATIONS - 1) { | ||||
for (auto it = result.begin(); it != result.end(); it++) { | for (const auto &entry : result) { | ||||
bool have = stack.back()->HaveCoin(it->first); | bool have = stack.back()->HaveCoin(entry.first); | ||||
const Coin &coin = stack.back()->AccessCoin(it->first); | const Coin &coin = stack.back()->AccessCoin(entry.first); | ||||
BOOST_CHECK(have == !coin.IsSpent()); | BOOST_CHECK(have == !coin.IsSpent()); | ||||
BOOST_CHECK(coin == it->second); | BOOST_CHECK(coin == entry.second); | ||||
} | } | ||||
} | } | ||||
// One every 10 iterations, remove a random entry from the cache | // One every 10 iterations, remove a random entry from the cache | ||||
if (utxoset.size() > 1 && InsecureRandRange(30)) { | if (utxoset.size() > 1 && InsecureRandRange(30)) { | ||||
stack[insecure_rand() % stack.size()]->Uncache( | stack[insecure_rand() % stack.size()]->Uncache( | ||||
FindRandomFrom(utxoset)->first); | FindRandomFrom(utxoset)->first); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 447 Lines • Show Last 20 Lines |