aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-01-30 16:53:58 -0500
committerAndrew Chow <github@achow101.com>2023-01-30 17:16:46 -0500
commitc8cb62272eacc6341cadae82349cadfbabe7700e (patch)
tree2df926aa81a0cadb5964b120352af80dd71fbd44
parent7241b936c5b4d004cdbdc7dc4d0b586856f6716b (diff)
parent2e16054a66b0576ec93d4032d6b74f0930a44fef (diff)
Merge bitcoin/bitcoin#26999: A few follow-ups to #17487 (coins write without cache drop)
2e16054a66b0576ec93d4032d6b74f0930a44fef Add assertions that BatchWrite(erase=true) erases (Pieter Wuille) 941feb6ca28522806c4710f85ae5673abdbdb0b9 Avoid unclear {it = ++it;} (Pieter Wuille) 98db35c2f81098b179136ed6c5f2a8570b3b5900 Follow coding style for named arguments (Pieter Wuille) bb00357add2c44d4b2cf4341be963f6bb9bee63f Make test/fuzz/coins_view exercise CCoinsViewCache::Sync() (Pieter Wuille) Pull request description: This addresses a few nits left open in #17487. ACKs for top commit: jonatack: ACK 2e16054a66b0576ec93d4032d6b74f0930a44fef Sjors: utACK 2e16054a66b0576ec93d4032d6b74f0930a44fef achow101: ACK 2e16054a66b0576ec93d4032d6b74f0930a44fef jamesob: ACK 2e16054a66b0576ec93d4032d6b74f0930a44fef ([`jamesob/ackr/26999.1.sipa.a_few_follow_ups_to_1748`](https://github.com/jamesob/bitcoin/tree/ackr/26999.1.sipa.a_few_follow_ups_to_1748)) Tree-SHA512: 5e64807b850fa12ce858e87470420555ad081f2f63d53649d9904034ed5311592005eb979a8a4df2b70ecb56cf022db9750cd6999e5480bc8226184d4be22ce4
-rw-r--r--src/coins.cpp9
-rw-r--r--src/test/coins_tests.cpp2
-rw-r--r--src/test/fuzz/coins_view.cpp3
3 files changed, 10 insertions, 4 deletions
diff --git a/src/coins.cpp b/src/coins.cpp
index ecbd625cd7..31ac67674a 100644
--- a/src/coins.cpp
+++ b/src/coins.cpp
@@ -249,15 +249,18 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
}
bool CCoinsViewCache::Flush() {
- bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/ true);
- cacheCoins.clear();
+ bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/true);
+ if (fOk && !cacheCoins.empty()) {
+ /* BatchWrite must erase all cacheCoins elements when erase=true. */
+ throw std::logic_error("Not all cached coins were erased");
+ }
cachedCoinsUsage = 0;
return fOk;
}
bool CCoinsViewCache::Sync()
{
- bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/ false);
+ bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/false);
// Instead of clearing `cacheCoins` as we would in Flush(), just clear the
// FRESH/DIRTY flags of any coin that isn't spent.
for (auto it = cacheCoins.begin(); it != cacheCoins.end(); ) {
diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp
index 92bad8dd2e..312f417129 100644
--- a/src/test/coins_tests.cpp
+++ b/src/test/coins_tests.cpp
@@ -55,7 +55,7 @@ public:
bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock, bool erase = true) override
{
- for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = erase ? mapCoins.erase(it) : ++it) {
+ for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = erase ? mapCoins.erase(it) : std::next(it)) {
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
// Same optimization used in CCoinsViewDB is to only write dirty entries.
map_[it->first] = it->second.coin;
diff --git a/src/test/fuzz/coins_view.cpp b/src/test/fuzz/coins_view.cpp
index 46026d8df3..e75dc3ce91 100644
--- a/src/test/fuzz/coins_view.cpp
+++ b/src/test/fuzz/coins_view.cpp
@@ -75,6 +75,9 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
(void)coins_view_cache.Flush();
},
[&] {
+ (void)coins_view_cache.Sync();
+ },
+ [&] {
coins_view_cache.SetBestBlock(ConsumeUInt256(fuzzed_data_provider));
},
[&] {