refactor: Rely on returned value of GetCoin instead of parameter

Also removed the unused coin parameter of GetCoin.

Co-authored-by: Andrew Toth <andrewstoth@gmail.com>
This commit is contained in:
Lőrinc
2024-09-07 23:12:35 +02:00
parent 46dfbf169b
commit 4feaa28728
16 changed files with 85 additions and 113 deletions

View File

@@ -162,22 +162,20 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view)
const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
Coin coin_using_get_coin;
const bool exists_using_get_coin = coins_view_cache.GetCoin(random_out_point, coin_using_get_coin).has_value();
if (exists_using_get_coin) {
assert(coin_using_get_coin == coin_using_access_coin);
if (auto coin{coins_view_cache.GetCoin(random_out_point)}) {
assert(*coin == coin_using_access_coin);
assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
} else {
assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
}
assert((exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin && exists_using_get_coin) ||
(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin && !exists_using_get_coin));
// If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
assert(exists_using_have_coin);
}
Coin coin_using_backend_get_coin;
if (backend_coins_view.GetCoin(random_out_point, coin_using_backend_get_coin)) {
if (auto coin{backend_coins_view.GetCoin(random_out_point)}) {
assert(exists_using_have_coin_in_backend);
// Note we can't assert that `coin_using_get_coin == coin_using_backend_get_coin` because the coin in
// Note we can't assert that `coin_using_get_coin == *coin` because the coin in
// the cache may have been modified but not yet flushed.
} else {
assert(!exists_using_have_coin_in_backend);

View File

@@ -146,15 +146,11 @@ class CoinsViewBottom final : public CCoinsView
std::map<COutPoint, Coin> m_data;
public:
std::optional<Coin> GetCoin(const COutPoint& outpoint, Coin& coin) const final
std::optional<Coin> GetCoin(const COutPoint& outpoint) const final
{
auto it = m_data.find(outpoint);
if (it == m_data.end()) {
return std::nullopt;
} else {
coin = it->second;
return coin; // TODO GetCoin shouldn't return spent coins
}
// TODO GetCoin shouldn't return spent coins
if (auto it = m_data.find(outpoint); it != m_data.end()) return it->second;
return std::nullopt;
}
bool HaveCoin(const COutPoint& outpoint) const final
@@ -265,17 +261,16 @@ FUZZ_TARGET(coinscache_sim)
// Look up in simulation data.
auto sim = lookup(outpointidx);
// Look up in real caches.
Coin realcoin;
auto real = caches.back()->GetCoin(data.outpoints[outpointidx], realcoin);
auto realcoin = caches.back()->GetCoin(data.outpoints[outpointidx]);
// Compare results.
if (!sim.has_value()) {
assert(!real || realcoin.IsSpent());
assert(!realcoin || realcoin->IsSpent());
} else {
assert(real && !realcoin.IsSpent());
assert(realcoin && !realcoin->IsSpent());
const auto& simcoin = data.coins[sim->first];
assert(realcoin.out == simcoin.out);
assert(realcoin.fCoinBase == simcoin.fCoinBase);
assert(realcoin.nHeight == sim->second);
assert(realcoin->out == simcoin.out);
assert(realcoin->fCoinBase == simcoin.fCoinBase);
assert(realcoin->nHeight == sim->second);
}
},
@@ -460,16 +455,15 @@ FUZZ_TARGET(coinscache_sim)
// Compare the bottom coinsview (not a CCoinsViewCache) with sim_cache[0].
for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
Coin realcoin;
auto real = bottom.GetCoin(data.outpoints[outpointidx], realcoin);
auto realcoin = bottom.GetCoin(data.outpoints[outpointidx]);
auto sim = lookup(outpointidx, 0);
if (!sim.has_value()) {
assert(!real || realcoin.IsSpent());
assert(!realcoin || realcoin->IsSpent());
} else {
assert(real && !realcoin.IsSpent());
assert(realcoin.out == data.coins[sim->first].out);
assert(realcoin.fCoinBase == data.coins[sim->first].fCoinBase);
assert(realcoin.nHeight == sim->second);
assert(realcoin && !realcoin->IsSpent());
assert(realcoin->out == data.coins[sim->first].out);
assert(realcoin->fCoinBase == data.coins[sim->first].fCoinBase);
assert(realcoin->nHeight == sim->second);
}
}
}

View File

@@ -214,9 +214,8 @@ FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
// Helper to query an amount
const CCoinsViewMemPool amount_view{WITH_LOCK(::cs_main, return &chainstate.CoinsTip()), tx_pool};
const auto GetAmount = [&](const COutPoint& outpoint) {
Coin c;
Assert(amount_view.GetCoin(outpoint, c));
return c.out.nValue;
auto coin{amount_view.GetCoin(outpoint).value()};
return coin.out.nValue;
};
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)