mirror of https://github.com/bitcoin/bitcoin.git
fuzz: exercise `EmplaceCoinInternalDANGER` calls
The exception could be triggered during fuzz testing which leaves the accounting in a bad state. Instead of documenting that, the accounting was fixed in a previous commit and the fuzz test simplified. Also added `EmplaceCoinInternalDANGER` alternative to fuzzing to exercise that area of te code as well.
This commit is contained in:
parent
9a1003b2e3
commit
a9eb8dfab1
|
@ -59,25 +59,19 @@ void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend
|
|||
if (random_coin.IsSpent()) {
|
||||
return;
|
||||
}
|
||||
Coin coin = random_coin;
|
||||
bool expected_code_path = false;
|
||||
const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
|
||||
COutPoint outpoint{random_out_point};
|
||||
Coin coin{random_coin};
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
const bool possible_overwrite{fuzzed_data_provider.ConsumeBool()};
|
||||
try {
|
||||
coins_view_cache.AddCoin(random_out_point, std::move(coin), possible_overwrite);
|
||||
expected_code_path = true;
|
||||
coins_view_cache.AddCoin(outpoint, std::move(coin), possible_overwrite);
|
||||
} catch (const std::logic_error& e) {
|
||||
if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
|
||||
assert(e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"});
|
||||
assert(!possible_overwrite);
|
||||
expected_code_path = true;
|
||||
// AddCoin() decreases cachedCoinsUsage by the memory usage of the old coin at the beginning and
|
||||
// increases it by the value of the new coin at the end. If it throws in the process, the value
|
||||
// of cachedCoinsUsage would have been incorrectly decreased, leading to an underflow later on.
|
||||
// To avoid this, use Flush() to reset the value of cachedCoinsUsage in sync with the cacheCoins
|
||||
// mapping.
|
||||
(void)coins_view_cache.Flush();
|
||||
}
|
||||
} else {
|
||||
coins_view_cache.EmplaceCoinInternalDANGER(std::move(outpoint), std::move(coin));
|
||||
}
|
||||
assert(expected_code_path);
|
||||
},
|
||||
[&] {
|
||||
(void)coins_view_cache.Flush();
|
||||
|
|
Loading…
Reference in New Issue