Changeset View
Changeset View
Standalone View
Standalone View
src/test/coins_tests.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | bool GetCoin(const COutPoint &outpoint, Coin &coin) const override { | ||||
coin = it->second; | coin = it->second; | ||||
if (coin.IsSpent() && InsecureRandBool() == 0) { | if (coin.IsSpent() && InsecureRandBool() == 0) { | ||||
// Randomly return false in case of an empty entry. | // Randomly return false in case of an empty entry. | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool HaveCoin(const COutPoint &outpoint) const override { | |||||
Coin coin; | |||||
return GetCoin(outpoint, coin); | |||||
} | |||||
uint256 GetBestBlock() const override { return hashBestBlock_; } | uint256 GetBestBlock() const override { return hashBestBlock_; } | ||||
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override { | bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override { | ||||
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) { | for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) { | ||||
if (it->second.flags & CCoinsCacheEntry::DIRTY) { | if (it->second.flags & CCoinsCacheEntry::DIRTY) { | ||||
// Same optimization used in CCoinsViewDB is to only write dirty | // Same optimization used in CCoinsViewDB is to only write dirty | ||||
// entries. | // entries. | ||||
map_[it->first] = it->second.coin; | map_[it->first] = it->second.coin; | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(coins_cache_simulation_test) { | ||||
} | } | ||||
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | ||||
// Do a random modification. | // Do a random modification. | ||||
{ | { | ||||
// txid we're going to modify in this iteration. | // txid we're going to modify in this iteration. | ||||
TxId txid = txids[InsecureRandRange(txids.size())]; | TxId txid = txids[InsecureRandRange(txids.size())]; | ||||
Coin &coin = result[COutPoint(txid, 0)]; | Coin &coin = result[COutPoint(txid, 0)]; | ||||
// Determine whether to test HaveCoin before or after Access* (or | |||||
// both). As these functions can influence each other's behaviour by | |||||
// pulling things into the cache, all combinations are tested. | |||||
bool test_havecoin_before = InsecureRandBits(2) == 0; | |||||
bool test_havecoin_after = InsecureRandBits(2) == 0; | |||||
bool result_havecoin = | |||||
test_havecoin_before | |||||
? stack.back()->HaveCoin(COutPoint(txid, 0)) | |||||
: false; | |||||
const Coin &entry = | const Coin &entry = | ||||
(InsecureRandRange(500) == 0) | (InsecureRandRange(500) == 0) | ||||
? AccessByTxid(*stack.back(), txid) | ? AccessByTxid(*stack.back(), txid) | ||||
: stack.back()->AccessCoin(COutPoint(txid, 0)); | : stack.back()->AccessCoin(COutPoint(txid, 0)); | ||||
BOOST_CHECK(coin == entry); | BOOST_CHECK(coin == entry); | ||||
BOOST_CHECK(!test_havecoin_before || | |||||
result_havecoin == !entry.IsSpent()); | |||||
if (test_havecoin_after) { | |||||
bool ret = stack.back()->HaveCoin(COutPoint(txid, 0)); | |||||
BOOST_CHECK(ret == !entry.IsSpent()); | |||||
} | |||||
if (InsecureRandRange(5) == 0 || coin.IsSpent()) { | if (InsecureRandRange(5) == 0 || coin.IsSpent()) { | ||||
CTxOut txout; | CTxOut txout; | ||||
txout.nValue = int64_t(InsecureRand32()) * SATOSHI; | txout.nValue = int64_t(InsecureRand32()) * SATOSHI; | ||||
if (InsecureRandRange(16) == 0 && coin.IsSpent()) { | if (InsecureRandRange(16) == 0 && coin.IsSpent()) { | ||||
txout.scriptPubKey.assign(1 + InsecureRandBits(6), | txout.scriptPubKey.assign(1 + InsecureRandBits(6), | ||||
OP_RETURN); | OP_RETURN); | ||||
BOOST_CHECK(txout.scriptPubKey.IsUnspendable()); | BOOST_CHECK(txout.scriptPubKey.IsUnspendable()); | ||||
▲ Show 20 Lines • Show All 492 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(coin_access) { | ||||
CheckAccessCoin(ABSENT, PRUNED, PRUNED, 0, 0); | CheckAccessCoin(ABSENT, PRUNED, PRUNED, 0, 0); | ||||
CheckAccessCoin(ABSENT, PRUNED, PRUNED, FRESH, FRESH); | CheckAccessCoin(ABSENT, PRUNED, PRUNED, FRESH, FRESH); | ||||
CheckAccessCoin(ABSENT, PRUNED, PRUNED, DIRTY, DIRTY); | CheckAccessCoin(ABSENT, PRUNED, PRUNED, DIRTY, DIRTY); | ||||
CheckAccessCoin(ABSENT, PRUNED, PRUNED, DIRTY | FRESH, DIRTY | FRESH); | CheckAccessCoin(ABSENT, PRUNED, PRUNED, DIRTY | FRESH, DIRTY | FRESH); | ||||
CheckAccessCoin(ABSENT, VALUE2, VALUE2, 0, 0); | CheckAccessCoin(ABSENT, VALUE2, VALUE2, 0, 0); | ||||
CheckAccessCoin(ABSENT, VALUE2, VALUE2, FRESH, FRESH); | CheckAccessCoin(ABSENT, VALUE2, VALUE2, FRESH, FRESH); | ||||
CheckAccessCoin(ABSENT, VALUE2, VALUE2, DIRTY, DIRTY); | CheckAccessCoin(ABSENT, VALUE2, VALUE2, DIRTY, DIRTY); | ||||
CheckAccessCoin(ABSENT, VALUE2, VALUE2, DIRTY | FRESH, DIRTY | FRESH); | CheckAccessCoin(ABSENT, VALUE2, VALUE2, DIRTY | FRESH, DIRTY | FRESH); | ||||
CheckAccessCoin(PRUNED, ABSENT, PRUNED, NO_ENTRY, FRESH); | CheckAccessCoin(PRUNED, ABSENT, ABSENT, NO_ENTRY, NO_ENTRY); | ||||
CheckAccessCoin(PRUNED, PRUNED, PRUNED, 0, 0); | CheckAccessCoin(PRUNED, PRUNED, PRUNED, 0, 0); | ||||
CheckAccessCoin(PRUNED, PRUNED, PRUNED, FRESH, FRESH); | CheckAccessCoin(PRUNED, PRUNED, PRUNED, FRESH, FRESH); | ||||
CheckAccessCoin(PRUNED, PRUNED, PRUNED, DIRTY, DIRTY); | CheckAccessCoin(PRUNED, PRUNED, PRUNED, DIRTY, DIRTY); | ||||
CheckAccessCoin(PRUNED, PRUNED, PRUNED, DIRTY | FRESH, DIRTY | FRESH); | CheckAccessCoin(PRUNED, PRUNED, PRUNED, DIRTY | FRESH, DIRTY | FRESH); | ||||
CheckAccessCoin(PRUNED, VALUE2, VALUE2, 0, 0); | CheckAccessCoin(PRUNED, VALUE2, VALUE2, 0, 0); | ||||
CheckAccessCoin(PRUNED, VALUE2, VALUE2, FRESH, FRESH); | CheckAccessCoin(PRUNED, VALUE2, VALUE2, FRESH, FRESH); | ||||
CheckAccessCoin(PRUNED, VALUE2, VALUE2, DIRTY, DIRTY); | CheckAccessCoin(PRUNED, VALUE2, VALUE2, DIRTY, DIRTY); | ||||
CheckAccessCoin(PRUNED, VALUE2, VALUE2, DIRTY | FRESH, DIRTY | FRESH); | CheckAccessCoin(PRUNED, VALUE2, VALUE2, DIRTY | FRESH, DIRTY | FRESH); | ||||
▲ Show 20 Lines • Show All 228 Lines • Show Last 20 Lines |