Changeset View
Changeset View
Standalone View
Standalone View
src/test/coins_tests.cpp
Show First 20 Lines • Show All 574 Lines • ▼ Show 20 Lines | static void SetCoinValue(const Amount value, Coin &coin) { | ||||
if (value != PRUNED) { | if (value != PRUNED) { | ||||
CTxOut out; | CTxOut out; | ||||
out.nValue = value; | out.nValue = value; | ||||
coin = Coin(std::move(out), 1, false); | coin = Coin(std::move(out), 1, false); | ||||
assert(!coin.IsSpent()); | assert(!coin.IsSpent()); | ||||
} | } | ||||
} | } | ||||
size_t InsertCoinMapEntry(CCoinsMap &map, const Amount value, char flags) { | static size_t InsertCoinMapEntry(CCoinsMap &map, const Amount value, | ||||
char flags) { | |||||
if (value == ABSENT) { | if (value == ABSENT) { | ||||
assert(flags == NO_ENTRY); | assert(flags == NO_ENTRY); | ||||
return 0; | return 0; | ||||
} | } | ||||
assert(flags != NO_ENTRY); | assert(flags != NO_ENTRY); | ||||
CCoinsCacheEntry entry; | CCoinsCacheEntry entry; | ||||
entry.flags = flags; | entry.flags = flags; | ||||
SetCoinValue(value, entry.coin); | SetCoinValue(value, entry.coin); | ||||
Show All 34 Lines | SingleEntryCacheTest(const Amount base_value, const Amount cache_value, | ||||
InsertCoinMapEntry(cache.map(), cache_value, cache_flags); | InsertCoinMapEntry(cache.map(), cache_value, cache_flags); | ||||
} | } | ||||
CCoinsView root; | CCoinsView root; | ||||
CCoinsViewCacheTest base{&root}; | CCoinsViewCacheTest base{&root}; | ||||
CCoinsViewCacheTest cache{&base}; | CCoinsViewCacheTest cache{&base}; | ||||
}; | }; | ||||
void CheckAccessCoin(const Amount base_value, const Amount cache_value, | static void CheckAccessCoin(const Amount base_value, const Amount cache_value, | ||||
const Amount expected_value, char cache_flags, | const Amount expected_value, char cache_flags, | ||||
char expected_flags) { | char expected_flags) { | ||||
SingleEntryCacheTest test(base_value, cache_value, cache_flags); | SingleEntryCacheTest test(base_value, cache_value, cache_flags); | ||||
test.cache.AccessCoin(OUTPOINT); | test.cache.AccessCoin(OUTPOINT); | ||||
test.cache.SelfTest(); | test.cache.SelfTest(); | ||||
Amount result_value; | Amount result_value; | ||||
char result_flags; | char result_flags; | ||||
GetCoinMapEntry(test.cache.map(), result_value, result_flags); | GetCoinMapEntry(test.cache.map(), result_value, result_flags); | ||||
BOOST_CHECK_EQUAL(result_value, expected_value); | BOOST_CHECK_EQUAL(result_value, expected_value); | ||||
Show All 32 Lines | BOOST_AUTO_TEST_CASE(coin_access) { | ||||
CheckAccessCoin(VALUE1, PRUNED, PRUNED, DIRTY, DIRTY); | CheckAccessCoin(VALUE1, PRUNED, PRUNED, DIRTY, DIRTY); | ||||
CheckAccessCoin(VALUE1, PRUNED, PRUNED, DIRTY | FRESH, DIRTY | FRESH); | CheckAccessCoin(VALUE1, PRUNED, PRUNED, DIRTY | FRESH, DIRTY | FRESH); | ||||
CheckAccessCoin(VALUE1, VALUE2, VALUE2, 0, 0); | CheckAccessCoin(VALUE1, VALUE2, VALUE2, 0, 0); | ||||
CheckAccessCoin(VALUE1, VALUE2, VALUE2, FRESH, FRESH); | CheckAccessCoin(VALUE1, VALUE2, VALUE2, FRESH, FRESH); | ||||
CheckAccessCoin(VALUE1, VALUE2, VALUE2, DIRTY, DIRTY); | CheckAccessCoin(VALUE1, VALUE2, VALUE2, DIRTY, DIRTY); | ||||
CheckAccessCoin(VALUE1, VALUE2, VALUE2, DIRTY | FRESH, DIRTY | FRESH); | CheckAccessCoin(VALUE1, VALUE2, VALUE2, DIRTY | FRESH, DIRTY | FRESH); | ||||
} | } | ||||
void CheckSpendCoin(Amount base_value, Amount cache_value, | static void CheckSpendCoin(Amount base_value, Amount cache_value, | ||||
Amount expected_value, char cache_flags, | Amount expected_value, char cache_flags, | ||||
char expected_flags) { | char expected_flags) { | ||||
SingleEntryCacheTest test(base_value, cache_value, cache_flags); | SingleEntryCacheTest test(base_value, cache_value, cache_flags); | ||||
test.cache.SpendCoin(OUTPOINT); | test.cache.SpendCoin(OUTPOINT); | ||||
test.cache.SelfTest(); | test.cache.SelfTest(); | ||||
Amount result_value; | Amount result_value; | ||||
char result_flags; | char result_flags; | ||||
GetCoinMapEntry(test.cache.map(), result_value, result_flags); | GetCoinMapEntry(test.cache.map(), result_value, result_flags); | ||||
BOOST_CHECK_EQUAL(result_value, expected_value); | BOOST_CHECK_EQUAL(result_value, expected_value); | ||||
Show All 33 Lines | BOOST_AUTO_TEST_CASE(coin_spend) { | ||||
CheckSpendCoin(VALUE1, PRUNED, PRUNED, DIRTY, DIRTY); | CheckSpendCoin(VALUE1, PRUNED, PRUNED, DIRTY, DIRTY); | ||||
CheckSpendCoin(VALUE1, PRUNED, ABSENT, DIRTY | FRESH, NO_ENTRY); | CheckSpendCoin(VALUE1, PRUNED, ABSENT, DIRTY | FRESH, NO_ENTRY); | ||||
CheckSpendCoin(VALUE1, VALUE2, PRUNED, 0, DIRTY); | CheckSpendCoin(VALUE1, VALUE2, PRUNED, 0, DIRTY); | ||||
CheckSpendCoin(VALUE1, VALUE2, ABSENT, FRESH, NO_ENTRY); | CheckSpendCoin(VALUE1, VALUE2, ABSENT, FRESH, NO_ENTRY); | ||||
CheckSpendCoin(VALUE1, VALUE2, PRUNED, DIRTY, DIRTY); | CheckSpendCoin(VALUE1, VALUE2, PRUNED, DIRTY, DIRTY); | ||||
CheckSpendCoin(VALUE1, VALUE2, ABSENT, DIRTY | FRESH, NO_ENTRY); | CheckSpendCoin(VALUE1, VALUE2, ABSENT, DIRTY | FRESH, NO_ENTRY); | ||||
} | } | ||||
void CheckAddCoinBase(Amount base_value, Amount cache_value, | static void CheckAddCoinBase(Amount base_value, Amount cache_value, | ||||
Amount modify_value, Amount expected_value, | Amount modify_value, Amount expected_value, | ||||
char cache_flags, char expected_flags, bool coinbase) { | char cache_flags, char expected_flags, | ||||
bool coinbase) { | |||||
SingleEntryCacheTest test(base_value, cache_value, cache_flags); | SingleEntryCacheTest test(base_value, cache_value, cache_flags); | ||||
Amount result_value; | Amount result_value; | ||||
char result_flags; | char result_flags; | ||||
try { | try { | ||||
CTxOut output; | CTxOut output; | ||||
output.nValue = modify_value; | output.nValue = modify_value; | ||||
test.cache.AddCoin(OUTPOINT, Coin(std::move(output), 1, coinbase), | test.cache.AddCoin(OUTPOINT, Coin(std::move(output), 1, coinbase), | ||||
Show All 9 Lines | static void CheckAddCoinBase(Amount base_value, Amount cache_value, | ||||
BOOST_CHECK_EQUAL(result_flags, expected_flags); | BOOST_CHECK_EQUAL(result_flags, expected_flags); | ||||
} | } | ||||
// Simple wrapper for CheckAddCoinBase function above that loops through | // Simple wrapper for CheckAddCoinBase function above that loops through | ||||
// different possible base_values, making sure each one gives the same results. | // different possible base_values, making sure each one gives the same results. | ||||
// This wrapper lets the coin_add test below be shorter and less repetitive, | // This wrapper lets the coin_add test below be shorter and less repetitive, | ||||
// while still verifying that the CoinsViewCache::AddCoin implementation ignores | // while still verifying that the CoinsViewCache::AddCoin implementation ignores | ||||
// base values. | // base values. | ||||
template <typename... Args> void CheckAddCoin(Args &&... args) { | template <typename... Args> static void CheckAddCoin(Args &&... args) { | ||||
for (Amount base_value : {ABSENT, PRUNED, VALUE1}) { | for (Amount base_value : {ABSENT, PRUNED, VALUE1}) { | ||||
CheckAddCoinBase(base_value, std::forward<Args>(args)...); | CheckAddCoinBase(base_value, std::forward<Args>(args)...); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(coin_add) { | BOOST_AUTO_TEST_CASE(coin_add) { | ||||
/** | /** | ||||
* Check AddCoin behavior, requesting a new coin from a cache view, writing | * Check AddCoin behavior, requesting a new coin from a cache view, writing | ||||
▲ Show 20 Lines • Show All 127 Lines • Show Last 20 Lines |