We currently set the (cache) best block to uint256::ONE and then expect Reset() to bring it back to uint256::ZERO.
That relies on CCoinsViewCache::GetBestBlock() treating 0 as uninitialized and lazily pulling from the base view, and in practice the best block should never be null.
Could we instead set the base best block to some random value and set the cache best block to a different value, then assert that after Reset() the cache’s GetBestBlock() matches the base best block? This would better exercise propagation across layers and avoids special-casing 0/1:
0BOOST_AUTO_TEST_CASE(ccoins_reset)
1{
2 CCoinsViewTest root{m_rng};
3 CCoinsViewCache root_cache{&root};
4 const uint256 base_best_block{m_rng.rand256()};
5 root_cache.SetBestBlock(base_best_block);
6 root_cache.Flush();
7
8 CCoinsViewCacheTest cache{&root};
9
10 const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
11
12 const Coin coin{CTxOut{m_rng.randrange(10), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 1)}, 1, false};
13 cache.EmplaceCoinInternalDANGER(COutPoint{outpoint}, Coin{coin});
14
15 const uint256 cache_best_block{uint256::ONE};
16 cache.SetBestBlock(cache_best_block);
17 cache.SelfTest();
18
19 BOOST_CHECK(cache.AccessCoin(outpoint) == coin);
20 BOOST_CHECK(!cache.AccessCoin(outpoint).IsSpent());
21 BOOST_CHECK_EQUAL(cache.GetCacheSize(), 1);
22 BOOST_CHECK_EQUAL(cache.GetBestBlock(), cache_best_block);
23
24 for (int i = 0; i < 2; ++i) { // Reset is idempotent
25 cache.Reset();
26 BOOST_CHECK(cache.AccessCoin(outpoint).IsSpent());
27 BOOST_CHECK_EQUAL(cache.GetCacheSize(), 0);
28 BOOST_CHECK_EQUAL(cache.GetBestBlock(), base_best_block);
29 }
30}