mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-04 18:22:57 +02:00
Merge bitcoin/bitcoin#28280: Don't empty dbcache on prune flushes: >30% faster IBD
589db872e1validation: don't erase coins cache on prune flushes (Andrew Toth)0e8918755fAdd linked-list test to CCoinsViewCache::SanityCheck (Pieter Wuille)05cf4e1875coins: move Sync logic to CoinsViewCacheCursor (Andrew Toth)7825b8b9aecoins: pass linked list of flagged entries to BatchWrite (Andrew Toth)a14edada8atest: add cache entry linked list tests (Andrew Toth)24ce37cb86coins: track flagged cache entries in linked list (Andrew Toth)58b7ed156dcoins: call ClearFlags in CCoinsCacheEntry destructor (Andrew Toth)8bd3959fearefactor: require self and sentinel parameters for AddFlags (Andrew Toth)75f36d241drefactor: add CoinsCachePair alias (Andrew Toth)f08faeade2refactor: move flags to private uint8_t and rename to m_flags (Andrew Toth)4e4fb4cbabrefactor: disallow setting flags in CCoinsCacheEntry constructors (Andrew Toth)8737c0cefarefactor: encapsulate flags setting with AddFlags and ClearFlags (Andrew Toth)9715d3bf1erefactor: encapsulate flags get access for all other checks (Andrew Toth)df34a94e57refactor: encapsulate flags access for dirty and fresh checks (Andrew Toth) Pull request description: Since https://github.com/bitcoin/bitcoin/pull/17487 we no longer need to clear the coins cache when syncing to disk. A warm coins cache significantly speeds up block connection, and only needs to be fully flushed when nearing the `dbcache` limit. For frequent pruning flushes there's no need to empty the cache and kill connect block speed. However, simply using `Sync` in place of `Flush` actually slows down a pruned full IBD with a high `dbcache` value. This is because as the cache grows, sync takes longer since every coin in the cache is scanned to check if it's dirty. For frequent prune flushes and a large cache this constant scanning starts to really slow IBD down, and just emptying the cache on every prune becomes faster. To fix this, we can add two pointers to each cache entry and construct a doubly linked list of dirty entries. We can then only iterate through all dirty entries on each `Sync`, and simply clear the pointers after. With this approach a full IBD with `dbcache=16384` and `prune=550` was 32% faster than master. For default `dbcache=450` speedup was ~9%. All benchmarks were run with `stopatheight=800000`. | | prune | dbcache | time | max RSS | speedup | |-----------:|----------:|------------:|--------:|-------------:|--------------:| | master | 550 | 16384 | 8:52:57 | 2,417,464k | - | | branch | 550 | 16384 | 6:01:00 | 16,216,736k | 32% | | branch | 550 | 450 | 8:05:08 | 2,818,072k | 8.8% | | master | 10000 | 5000 | 8:19:59 | 2,962,752k | - | | branch | 10000 | 5000| 5:56:39 | 6,179,764k | 28.8% | | master | 0 | 16384 | 4:51:53 | 14,726,408k | - | | branch | 0 | 16384 | 4:43:11 | 16,526,348k | 2.7% | | master | 0 | 450 | 7:08:07 | 3,005,892k | - | | branch | 0 | 450 | 6:57:24 | 3,013,556k |2.6%| While the 2 pointers add memory to each cache entry, it did not slow down IBD. For non-pruned IBD results were similar for this branch and master. When I performed the initial IBD, the full UTXO set could be held in memory when using the max `dbcache` value. For non-pruned IBD with max `dbcache` to tip ended up using 12% more memory, but it was also 2.7% faster somehow. For smaller `dbcache` values the `dbcache` limit is respected so does not consume more memory, and the potentially more frequent flushes were not significant enough to cause any slowdown. For reviewers, the commits in order do the following: First 4 commits encapsulate all accesses to `flags` on cache entries, and then the 5th makes `flags` private. Commits `refactor: add CoinsCachePair alias` to `coins: call ClearFlags in CCoinsCacheEntry destructor` create the linked list head nodes and cache entry self references and pass them into `AddFlags`. Commit `coins: track flagged cache entries in linked list` actually adds the entries into a linked list when they are flagged DIRTY or FRESH and removes them from the linked list when they are destroyed or the flags are cleared manually. However, the linked list is not yet used anywhere. Commit `test: add cache entry linked list tests` adds unit tests for the linked list. Commit `coins: pass linked list of flagged entries to BatchWrite` uses the linked list to iterate through DIRTY entries instead of using the entire coins cache. Commit `validation: don't erase coins cache on prune flushes` uses `Sync` instead of `Flush` for pruning flushes, so the cache is no longer cleared. Inspired by [this comment](https://github.com/bitcoin/bitcoin/pull/15265#issuecomment-457720636). Fixes https://github.com/bitcoin/bitcoin/issues/11315. ACKs for top commit: paplorinc: ACK589db872e1sipa: reACK589db872e1achow101: ACK589db872e1mzumsande: re-ACK589db872e1Tree-SHA512: 23b2bc01c83edacb5b39aa60bb0b766de9a74ce17f0c59bf13b97b4328a7b758ad9aff6581c3ca88e2973f7658380651530d497444f48d6e22ea0bfc51cc921d
This commit is contained in:
@@ -55,10 +55,10 @@ public:
|
||||
|
||||
uint256 GetBestBlock() const override { return hashBestBlock_; }
|
||||
|
||||
bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock, bool erase = true) override
|
||||
bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock) override
|
||||
{
|
||||
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = erase ? mapCoins.erase(it) : std::next(it)) {
|
||||
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
|
||||
for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)){
|
||||
if (it->second.IsDirty()) {
|
||||
// Same optimization used in CCoinsViewDB is to only write dirty entries.
|
||||
map_[it->first] = it->second.coin;
|
||||
if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {
|
||||
@@ -78,7 +78,7 @@ class CCoinsViewCacheTest : public CCoinsViewCache
|
||||
public:
|
||||
explicit CCoinsViewCacheTest(CCoinsView* _base) : CCoinsViewCache(_base) {}
|
||||
|
||||
void SelfTest() const
|
||||
void SelfTest(bool sanity_check = true) const
|
||||
{
|
||||
// Manually recompute the dynamic usage of the whole data, and compare it.
|
||||
size_t ret = memusage::DynamicUsage(cacheCoins);
|
||||
@@ -89,9 +89,13 @@ public:
|
||||
}
|
||||
BOOST_CHECK_EQUAL(GetCacheSize(), count);
|
||||
BOOST_CHECK_EQUAL(DynamicMemoryUsage(), ret);
|
||||
if (sanity_check) {
|
||||
SanityCheck();
|
||||
}
|
||||
}
|
||||
|
||||
CCoinsMap& map() const { return cacheCoins; }
|
||||
CoinsCachePair& sentinel() const { return m_sentinel; }
|
||||
size_t& usage() const { return cachedCoinsUsage; }
|
||||
};
|
||||
|
||||
@@ -576,7 +580,7 @@ static void SetCoinsValue(CAmount value, Coin& coin)
|
||||
}
|
||||
}
|
||||
|
||||
static size_t InsertCoinsMapEntry(CCoinsMap& map, CAmount value, char flags)
|
||||
static size_t InsertCoinsMapEntry(CCoinsMap& map, CoinsCachePair& sentinel, CAmount value, char flags)
|
||||
{
|
||||
if (value == ABSENT) {
|
||||
assert(flags == NO_ENTRY);
|
||||
@@ -584,10 +588,10 @@ static size_t InsertCoinsMapEntry(CCoinsMap& map, CAmount value, char flags)
|
||||
}
|
||||
assert(flags != NO_ENTRY);
|
||||
CCoinsCacheEntry entry;
|
||||
entry.flags = flags;
|
||||
SetCoinsValue(value, entry.coin);
|
||||
auto inserted = map.emplace(OUTPOINT, std::move(entry));
|
||||
assert(inserted.second);
|
||||
inserted.first->second.AddFlags(flags, *inserted.first, sentinel);
|
||||
return inserted.first->second.coin.DynamicMemoryUsage();
|
||||
}
|
||||
|
||||
@@ -603,17 +607,20 @@ void GetCoinsMapEntry(const CCoinsMap& map, CAmount& value, char& flags, const C
|
||||
} else {
|
||||
value = it->second.coin.out.nValue;
|
||||
}
|
||||
flags = it->second.flags;
|
||||
flags = it->second.GetFlags();
|
||||
assert(flags != NO_ENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
void WriteCoinsViewEntry(CCoinsView& view, CAmount value, char flags)
|
||||
{
|
||||
CoinsCachePair sentinel{};
|
||||
sentinel.second.SelfRef(sentinel);
|
||||
CCoinsMapMemoryResource resource;
|
||||
CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
|
||||
InsertCoinsMapEntry(map, value, flags);
|
||||
BOOST_CHECK(view.BatchWrite(map, {}));
|
||||
auto usage{InsertCoinsMapEntry(map, sentinel, value, flags)};
|
||||
auto cursor{CoinsViewCacheCursor(usage, sentinel, map, /*will_erase=*/true)};
|
||||
BOOST_CHECK(view.BatchWrite(cursor, {}));
|
||||
}
|
||||
|
||||
class SingleEntryCacheTest
|
||||
@@ -622,7 +629,7 @@ public:
|
||||
SingleEntryCacheTest(CAmount base_value, CAmount cache_value, char cache_flags)
|
||||
{
|
||||
WriteCoinsViewEntry(base, base_value, base_value == ABSENT ? NO_ENTRY : DIRTY);
|
||||
cache.usage() += InsertCoinsMapEntry(cache.map(), cache_value, cache_flags);
|
||||
cache.usage() += InsertCoinsMapEntry(cache.map(), cache.sentinel(), cache_value, cache_flags);
|
||||
}
|
||||
|
||||
CCoinsView root;
|
||||
@@ -634,7 +641,7 @@ static void CheckAccessCoin(CAmount base_value, CAmount cache_value, CAmount exp
|
||||
{
|
||||
SingleEntryCacheTest test(base_value, cache_value, cache_flags);
|
||||
test.cache.AccessCoin(OUTPOINT);
|
||||
test.cache.SelfTest();
|
||||
test.cache.SelfTest(/*sanity_check=*/false);
|
||||
|
||||
CAmount result_value;
|
||||
char result_flags;
|
||||
@@ -803,7 +810,7 @@ void CheckWriteCoins(CAmount parent_value, CAmount child_value, CAmount expected
|
||||
char result_flags;
|
||||
try {
|
||||
WriteCoinsViewEntry(test.cache, child_value, child_flags);
|
||||
test.cache.SelfTest();
|
||||
test.cache.SelfTest(/*sanity_check=*/false);
|
||||
GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
|
||||
} catch (std::logic_error&) {
|
||||
result_value = FAIL;
|
||||
@@ -916,6 +923,7 @@ void TestFlushBehavior(
|
||||
// Flush in reverse order to ensure that flushes happen from children up.
|
||||
for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
|
||||
auto& cache = *i;
|
||||
cache->SanityCheck();
|
||||
// hashBlock must be filled before flushing to disk; value is
|
||||
// unimportant here. This is normally done during connect/disconnect block.
|
||||
cache->SetBestBlock(InsecureRand256());
|
||||
|
||||
219
src/test/coinscachepair_tests.cpp
Normal file
219
src/test/coinscachepair_tests.cpp
Normal file
@@ -0,0 +1,219 @@
|
||||
// Copyright (c) 2024-present The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <coins.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <list>
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(coinscachepair_tests)
|
||||
|
||||
static constexpr auto NUM_NODES{4};
|
||||
|
||||
std::list<CoinsCachePair> CreatePairs(CoinsCachePair& sentinel)
|
||||
{
|
||||
std::list<CoinsCachePair> nodes;
|
||||
for (auto i{0}; i < NUM_NODES; ++i) {
|
||||
nodes.emplace_back();
|
||||
|
||||
auto node{std::prev(nodes.end())};
|
||||
node->second.AddFlags(CCoinsCacheEntry::DIRTY, *node, sentinel);
|
||||
|
||||
BOOST_CHECK_EQUAL(node->second.GetFlags(), CCoinsCacheEntry::DIRTY);
|
||||
BOOST_CHECK_EQUAL(node->second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &(*node));
|
||||
|
||||
if (i > 0) {
|
||||
BOOST_CHECK_EQUAL(std::prev(node)->second.Next(), &(*node));
|
||||
BOOST_CHECK_EQUAL(node->second.Prev(), &(*std::prev(node)));
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(linked_list_iteration)
|
||||
{
|
||||
CoinsCachePair sentinel;
|
||||
sentinel.second.SelfRef(sentinel);
|
||||
auto nodes{CreatePairs(sentinel)};
|
||||
|
||||
// Check iterating through pairs is identical to iterating through a list
|
||||
auto node{sentinel.second.Next()};
|
||||
for (const auto& expected : nodes) {
|
||||
BOOST_CHECK_EQUAL(&expected, node);
|
||||
node = node->second.Next();
|
||||
}
|
||||
BOOST_CHECK_EQUAL(node, &sentinel);
|
||||
|
||||
// Check iterating through pairs is identical to iterating through a list
|
||||
// Clear the flags during iteration
|
||||
node = sentinel.second.Next();
|
||||
for (const auto& expected : nodes) {
|
||||
BOOST_CHECK_EQUAL(&expected, node);
|
||||
auto next = node->second.Next();
|
||||
node->second.ClearFlags();
|
||||
node = next;
|
||||
}
|
||||
BOOST_CHECK_EQUAL(node, &sentinel);
|
||||
// Check that sentinel's next and prev are itself
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &sentinel);
|
||||
|
||||
// Delete the nodes from the list to make sure there are no dangling pointers
|
||||
for (auto it{nodes.begin()}; it != nodes.end(); it = nodes.erase(it)) {
|
||||
BOOST_CHECK_EQUAL(it->second.GetFlags(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(linked_list_iterate_erase)
|
||||
{
|
||||
CoinsCachePair sentinel;
|
||||
sentinel.second.SelfRef(sentinel);
|
||||
auto nodes{CreatePairs(sentinel)};
|
||||
|
||||
// Check iterating through pairs is identical to iterating through a list
|
||||
// Erase the nodes as we iterate through, but don't clear flags
|
||||
// The flags will be cleared by the CCoinsCacheEntry's destructor
|
||||
auto node{sentinel.second.Next()};
|
||||
for (auto expected{nodes.begin()}; expected != nodes.end(); expected = nodes.erase(expected)) {
|
||||
BOOST_CHECK_EQUAL(&(*expected), node);
|
||||
node = node->second.Next();
|
||||
}
|
||||
BOOST_CHECK_EQUAL(node, &sentinel);
|
||||
|
||||
// Check that sentinel's next and prev are itself
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &sentinel);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(linked_list_random_deletion)
|
||||
{
|
||||
CoinsCachePair sentinel;
|
||||
sentinel.second.SelfRef(sentinel);
|
||||
auto nodes{CreatePairs(sentinel)};
|
||||
|
||||
// Create linked list sentinel->n1->n2->n3->n4->sentinel
|
||||
auto n1{nodes.begin()};
|
||||
auto n2{std::next(n1)};
|
||||
auto n3{std::next(n2)};
|
||||
auto n4{std::next(n3)};
|
||||
|
||||
// Delete n2
|
||||
// sentinel->n1->n3->n4->sentinel
|
||||
nodes.erase(n2);
|
||||
// Check that n1 now points to n3, and n3 still points to n4
|
||||
// Also check that flags were not altered
|
||||
BOOST_CHECK_EQUAL(n1->second.GetFlags(), CCoinsCacheEntry::DIRTY);
|
||||
BOOST_CHECK_EQUAL(n1->second.Next(), &(*n3));
|
||||
BOOST_CHECK_EQUAL(n3->second.GetFlags(), CCoinsCacheEntry::DIRTY);
|
||||
BOOST_CHECK_EQUAL(n3->second.Next(), &(*n4));
|
||||
BOOST_CHECK_EQUAL(n3->second.Prev(), &(*n1));
|
||||
|
||||
// Delete n1
|
||||
// sentinel->n3->n4->sentinel
|
||||
nodes.erase(n1);
|
||||
// Check that sentinel now points to n3, and n3 still points to n4
|
||||
// Also check that flags were not altered
|
||||
BOOST_CHECK_EQUAL(n3->second.GetFlags(), CCoinsCacheEntry::DIRTY);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &(*n3));
|
||||
BOOST_CHECK_EQUAL(n3->second.Next(), &(*n4));
|
||||
BOOST_CHECK_EQUAL(n3->second.Prev(), &sentinel);
|
||||
|
||||
// Delete n4
|
||||
// sentinel->n3->sentinel
|
||||
nodes.erase(n4);
|
||||
// Check that sentinel still points to n3, and n3 points to sentinel
|
||||
// Also check that flags were not altered
|
||||
BOOST_CHECK_EQUAL(n3->second.GetFlags(), CCoinsCacheEntry::DIRTY);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &(*n3));
|
||||
BOOST_CHECK_EQUAL(n3->second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &(*n3));
|
||||
|
||||
// Delete n3
|
||||
// sentinel->sentinel
|
||||
nodes.erase(n3);
|
||||
// Check that sentinel's next and prev are itself
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &sentinel);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(linked_list_add_flags)
|
||||
{
|
||||
CoinsCachePair sentinel;
|
||||
sentinel.second.SelfRef(sentinel);
|
||||
CoinsCachePair n1;
|
||||
CoinsCachePair n2;
|
||||
|
||||
// Check that adding 0 flag has no effect
|
||||
n1.second.AddFlags(0, n1, sentinel);
|
||||
BOOST_CHECK_EQUAL(n1.second.GetFlags(), 0);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &sentinel);
|
||||
|
||||
// Check that adding DIRTY flag inserts it into linked list and sets flags
|
||||
n1.second.AddFlags(CCoinsCacheEntry::DIRTY, n1, sentinel);
|
||||
BOOST_CHECK_EQUAL(n1.second.GetFlags(), CCoinsCacheEntry::DIRTY);
|
||||
BOOST_CHECK_EQUAL(n1.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(n1.second.Prev(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n1);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n1);
|
||||
|
||||
// Check that adding FRESH flag on new node inserts it after n1
|
||||
n2.second.AddFlags(CCoinsCacheEntry::FRESH, n2, sentinel);
|
||||
BOOST_CHECK_EQUAL(n2.second.GetFlags(), CCoinsCacheEntry::FRESH);
|
||||
BOOST_CHECK_EQUAL(n2.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(n2.second.Prev(), &n1);
|
||||
BOOST_CHECK_EQUAL(n1.second.Next(), &n2);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n2);
|
||||
|
||||
// Check that adding 0 flag has no effect, and doesn't change position
|
||||
n1.second.AddFlags(0, n1, sentinel);
|
||||
BOOST_CHECK_EQUAL(n1.second.GetFlags(), CCoinsCacheEntry::DIRTY);
|
||||
BOOST_CHECK_EQUAL(n1.second.Next(), &n2);
|
||||
BOOST_CHECK_EQUAL(n1.second.Prev(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n1);
|
||||
BOOST_CHECK_EQUAL(n2.second.Prev(), &n1);
|
||||
|
||||
// Check that we can add extra flags, but they don't change our position
|
||||
n1.second.AddFlags(CCoinsCacheEntry::FRESH, n1, sentinel);
|
||||
BOOST_CHECK_EQUAL(n1.second.GetFlags(), CCoinsCacheEntry::DIRTY | CCoinsCacheEntry::FRESH);
|
||||
BOOST_CHECK_EQUAL(n1.second.Next(), &n2);
|
||||
BOOST_CHECK_EQUAL(n1.second.Prev(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n1);
|
||||
BOOST_CHECK_EQUAL(n2.second.Prev(), &n1);
|
||||
|
||||
// Check that we can clear flags then re-add them
|
||||
n1.second.ClearFlags();
|
||||
BOOST_CHECK_EQUAL(n1.second.GetFlags(), 0);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n2);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n2);
|
||||
BOOST_CHECK_EQUAL(n2.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(n2.second.Prev(), &sentinel);
|
||||
|
||||
// Check that calling `ClearFlags` with 0 flags has no effect
|
||||
n1.second.ClearFlags();
|
||||
BOOST_CHECK_EQUAL(n1.second.GetFlags(), 0);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n2);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n2);
|
||||
BOOST_CHECK_EQUAL(n2.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(n2.second.Prev(), &sentinel);
|
||||
|
||||
// Adding 0 still has no effect
|
||||
n1.second.AddFlags(0, n1, sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n2);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n2);
|
||||
BOOST_CHECK_EQUAL(n2.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(n2.second.Prev(), &sentinel);
|
||||
|
||||
// But adding DIRTY re-inserts it after n2
|
||||
n1.second.AddFlags(CCoinsCacheEntry::DIRTY, n1, sentinel);
|
||||
BOOST_CHECK_EQUAL(n1.second.GetFlags(), CCoinsCacheEntry::DIRTY);
|
||||
BOOST_CHECK_EQUAL(n2.second.Next(), &n1);
|
||||
BOOST_CHECK_EQUAL(n1.second.Prev(), &n2);
|
||||
BOOST_CHECK_EQUAL(n1.second.Next(), &sentinel);
|
||||
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -120,12 +120,15 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view)
|
||||
random_mutable_transaction = *opt_mutable_transaction;
|
||||
},
|
||||
[&] {
|
||||
CoinsCachePair sentinel{};
|
||||
sentinel.second.SelfRef(sentinel);
|
||||
size_t usage{0};
|
||||
CCoinsMapMemoryResource resource;
|
||||
CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
|
||||
LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
|
||||
{
|
||||
CCoinsCacheEntry coins_cache_entry;
|
||||
coins_cache_entry.flags = fuzzed_data_provider.ConsumeIntegral<unsigned char>();
|
||||
const auto flags{fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
coins_cache_entry.coin = random_coin;
|
||||
} else {
|
||||
@@ -136,11 +139,14 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view)
|
||||
}
|
||||
coins_cache_entry.coin = *opt_coin;
|
||||
}
|
||||
coins_map.emplace(random_out_point, std::move(coins_cache_entry));
|
||||
auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
|
||||
it->second.AddFlags(flags, *it, sentinel);
|
||||
usage += it->second.coin.DynamicMemoryUsage();
|
||||
}
|
||||
bool expected_code_path = false;
|
||||
try {
|
||||
coins_view_cache.BatchWrite(coins_map, fuzzed_data_provider.ConsumeBool() ? ConsumeUInt256(fuzzed_data_provider) : coins_view_cache.GetBestBlock());
|
||||
auto cursor{CoinsViewCacheCursor(usage, sentinel, coins_map, /*will_erase=*/true)};
|
||||
coins_view_cache.BatchWrite(cursor, fuzzed_data_provider.ConsumeBool() ? ConsumeUInt256(fuzzed_data_provider) : coins_view_cache.GetBestBlock());
|
||||
expected_code_path = true;
|
||||
} catch (const std::logic_error& e) {
|
||||
if (e.what() == std::string{"FRESH flag misapplied to coin that exists in parent cache"}) {
|
||||
|
||||
@@ -172,13 +172,13 @@ public:
|
||||
std::unique_ptr<CCoinsViewCursor> Cursor() const final { return {}; }
|
||||
size_t EstimateSize() const final { return m_data.size(); }
|
||||
|
||||
bool BatchWrite(CCoinsMap& data, const uint256&, bool erase) final
|
||||
bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256&) final
|
||||
{
|
||||
for (auto it = data.begin(); it != data.end(); it = erase ? data.erase(it) : std::next(it)) {
|
||||
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
|
||||
for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
|
||||
if (it->second.IsDirty()) {
|
||||
if (it->second.coin.IsSpent() && (it->first.n % 5) != 4) {
|
||||
m_data.erase(it->first);
|
||||
} else if (erase) {
|
||||
} else if (cursor.WillErase(*it)) {
|
||||
m_data[it->first] = std::move(it->second.coin);
|
||||
} else {
|
||||
m_data[it->first] = it->second.coin;
|
||||
|
||||
Reference in New Issue
Block a user