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()) {
|
if (random_coin.IsSpent()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Coin coin = random_coin;
|
COutPoint outpoint{random_out_point};
|
||||||
bool expected_code_path = false;
|
Coin coin{random_coin};
|
||||||
const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
|
if (fuzzed_data_provider.ConsumeBool()) {
|
||||||
try {
|
const bool possible_overwrite{fuzzed_data_provider.ConsumeBool()};
|
||||||
coins_view_cache.AddCoin(random_out_point, std::move(coin), possible_overwrite);
|
try {
|
||||||
expected_code_path = true;
|
coins_view_cache.AddCoin(outpoint, std::move(coin), possible_overwrite);
|
||||||
} catch (const std::logic_error& e) {
|
} 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);
|
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();
|
(void)coins_view_cache.Flush();
|
||||||
|
|
Loading…
Reference in New Issue