https://github.com/bitcoin/bitcoin/pull/34320/commits/4b32181dbbe32f9cd9f8c2063bd31b27746f794d:
Consider merging both tests as done in the diff below. I also replaced the DB view with a dummy view because CCoinsViewDB is not under test here; the dummy is sufficient to test the cache.
0diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp
1index ad182559a3..baba9c4e75 100644
2--- a/src/test/coins_tests.cpp
3+++ b/src/test/coins_tests.cpp
4@@ -1050,23 +1050,13 @@ BOOST_FIXTURE_TEST_CASE(ccoins_flush_behavior, FlushTest)
5 }
6 }
7
8-BOOST_AUTO_TEST_CASE(ccoins_haveinputs_cache_miss_uses_base_getcoin)
9+BOOST_AUTO_TEST_CASE(ccoins_cache_behavior)
10 {
11- const COutPoint prevout{Txid::FromUint256(m_rng.rand256()), 0};
12-
13- CCoinsViewDB db{{.path = "test", .cache_bytes = 1_MiB, .memory_only = true}, {}};
14- {
15- CCoinsViewCache write_cache{&db};
16- write_cache.SetBestBlock(m_rng.rand256());
17- write_cache.AddCoin(prevout, Coin{CTxOut{1, CScript{}}, 1, false}, /*possible_overwrite=*/false);
18- write_cache.Flush();
19- }
20-
21 class CCoinsViewSpy final : public CCoinsViewBacked
22 {
23 public:
24 const COutPoint expected;
25- mutable size_t havecoin_calls{0}, getcoin_calls{0};
26+ mutable size_t getcoin_calls{0};
27
28 explicit CCoinsViewSpy(CCoinsView* view, const COutPoint& out) : CCoinsViewBacked(view), expected{out} {}
29
30@@ -1074,18 +1064,23 @@ BOOST_AUTO_TEST_CASE(ccoins_haveinputs_cache_miss_uses_base_getcoin)
31 {
32 ++getcoin_calls;
33 BOOST_CHECK(out == expected);
34- return CCoinsViewBacked::GetCoin(out);
35+ return Coin{CTxOut{1, CScript{}}, 1, false};
36 }
37
38 bool HaveCoin(const COutPoint& out) const override
39 {
40- ++havecoin_calls;
41- BOOST_CHECK(out == expected);
42- return CCoinsViewBacked::HaveCoin(out);
43+ // This should never be called, since HaveInputs() should call GetCoin()
44+ // and not call HaveCoin() at all. If this is called, it means that
45+ // HaveInputs() is calling HaveCoin() instead of GetCoin(), which
46+ // is less efficient since it may require two lookups instead of one.
47+ BOOST_FAIL("Base HaveCoin should never be called");
48+ return false;
49 }
50 };
51
52- CCoinsViewSpy base{&db, prevout};
53+ const COutPoint prevout{Txid::FromUint256(m_rng.rand256()), 0};
54+ CCoinsView dummy;
55+ CCoinsViewSpy base{&dummy, prevout};
56 CCoinsViewCache cache{&base};
57
58 CMutableTransaction mtx;
59@@ -1095,46 +1090,14 @@ BOOST_AUTO_TEST_CASE(ccoins_haveinputs_cache_miss_uses_base_getcoin)
60
61 BOOST_CHECK(cache.HaveInputs(tx));
62 BOOST_CHECK_EQUAL(base.getcoin_calls, 1);
63- BOOST_CHECK_EQUAL(base.havecoin_calls, 0);
64-}
65
66-BOOST_AUTO_TEST_CASE(ccoins_cache_hit_does_not_call_base)
67-{
68- class CCoinsViewNoCall final : public CCoinsView
69- {
70- public:
71- std::optional<Coin> GetCoin(const COutPoint&) const override
72- {
73- BOOST_FAIL("Base GetCoin should not be called when input is cached");
74- return std::nullopt;
75- }
76-
77- bool HaveCoin(const COutPoint&) const override
78- {
79- BOOST_FAIL("Base HaveCoin should not be called when input is cached");
80- return false;
81- }
82- };
83-
84- const COutPoint prevout{Txid::FromUint256(m_rng.rand256()), 0};
85- CCoinsViewNoCall base;
86- CCoinsViewCache cache{&base};
87-
88- cache.AddCoin(prevout, Coin{CTxOut{1, CScript{}}, 1, false}, /*possible_overwrite=*/false);
89- BOOST_CHECK(cache.HaveCoinInCache(prevout));
90-
91- BOOST_CHECK(!cache.AccessCoin(prevout).IsSpent());
92- BOOST_CHECK(cache.GetCoin(prevout));
93- BOOST_CHECK(cache.HaveCoin(prevout));
94-
95- CMutableTransaction mtx;
96- mtx.vin.emplace_back(prevout);
97- const CTransaction tx{mtx};
98- BOOST_CHECK(!tx.IsCoinBase());
99 BOOST_CHECK(cache.HaveInputs(tx));
100-
101- BOOST_CHECK(cache.SpendCoin(prevout));
102- BOOST_CHECK(!cache.HaveCoinInCache(prevout));
103+ BOOST_CHECK(cache.GetCoin(prevout));
104+ BOOST_CHECK(!cache.AccessCoin(prevout).IsSpent());
105+ // GetCoin should only have been called once,
106+ // since the result should be cached after
107+ // the first call to HaveInputs().
108+ BOOST_CHECK_EQUAL(base.getcoin_calls, 1);
109 }
110
111 BOOST_AUTO_TEST_CASE(coins_resource_is_used)