Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <coins.h>
6 : : #include <kernel/chainstatemanager_opts.h>
7 : : #include <primitives/block.h>
8 : : #include <primitives/transaction.h>
9 : : #include <primitives/transaction_identifier.h>
10 : : #include <txdb.h>
11 : : #include <uint256.h>
12 : : #include <util/byte_units.h>
13 : : #include <util/threadpool.h>
14 : :
15 : : #include <boost/test/unit_test.hpp>
16 : :
17 : : #include <cstdint>
18 : : #include <cstring>
19 : : #include <memory>
20 : : #include <ranges>
21 : : #include <unordered_set>
22 : : #include <vector>
23 : :
24 : : namespace {
25 : :
26 : 7 : util::NotNullSharedPtr<ThreadPool> MakeStartedThreadPool()
27 : : {
28 : 7 : auto pool{util::MakeNotNullShared<ThreadPool>("fetch_test")};
29 [ + - ]: 7 : pool->Start(DEFAULT_PREVOUTFETCH_THREADS);
30 : 7 : return pool;
31 : 0 : }
32 : :
33 : 8 : CBlock CreateBlock() noexcept
34 : : {
35 : 8 : static constexpr auto NUM_TXS{100};
36 : 8 : CBlock block;
37 : 8 : CMutableTransaction coinbase;
38 : 8 : coinbase.vin.emplace_back();
39 [ - + ]: 8 : block.vtx.push_back(MakeTransactionRef(coinbase));
40 : :
41 : 8 : Txid prevhash{Txid::FromUint256(uint256{1})};
42 : :
43 [ + + ]: 800 : for (const auto i : std::views::iota(1, NUM_TXS)) {
44 : 792 : CMutableTransaction tx;
45 [ + + ]: 792 : const Txid txid{i % 20 == 0 ? prevhash : Txid::FromUint256(uint256(i))};
46 : 792 : tx.vin.emplace_back(txid, 0);
47 : 792 : prevhash = tx.GetHash();
48 [ - + ]: 792 : block.vtx.push_back(MakeTransactionRef(tx));
49 : 792 : }
50 : :
51 : 8 : return block;
52 : 8 : }
53 : :
54 : 8 : void PopulateView(const CBlock& block, CCoinsView& view, bool spent = false)
55 : : {
56 : 8 : CCoinsViewCache cache{&view};
57 [ + - ]: 8 : cache.SetBestBlock(uint256::ONE);
58 : :
59 [ + - ]: 8 : std::unordered_set<Txid, SaltedCoinsCacheHasher> txids{};
60 [ - + + - ]: 8 : txids.reserve(block.vtx.size() - 1);
61 [ + + ]: 800 : for (const auto& tx : block.vtx | std::views::drop(1)) {
62 [ + + ]: 1584 : for (const auto& in : tx->vin) {
63 [ + + ]: 792 : if (txids.contains(in.prevout.hash)) continue;
64 [ + + ]: 760 : if (spent) {
65 [ + - + - : 190 : BOOST_CHECK(cache.SpendCoin(in.prevout));
+ - ]
66 : : } else {
67 : 665 : Coin coin{};
68 : 665 : coin.out.nValue = 1;
69 [ + - ]: 665 : cache.AddCoin(in.prevout, std::move(coin), /*possible_overwrite=*/true);
70 : 665 : }
71 : : }
72 [ + - ]: 792 : txids.emplace(tx->GetHash());
73 : : }
74 : :
75 [ + - ]: 8 : cache.Flush();
76 : 8 : }
77 : :
78 : 8 : void CheckCache(const CBlock& block, const CCoinsViewCache& cache)
79 : : {
80 : 8 : uint32_t counter{0};
81 : 8 : std::unordered_set<Txid, SaltedCoinsCacheHasher> txids{};
82 [ - + + - ]: 8 : txids.reserve(block.vtx.size() - 1);
83 : :
84 [ + + ]: 808 : for (const auto& tx : block.vtx) {
85 [ + + ]: 800 : if (tx->IsCoinBase()) {
86 [ + - + - : 16 : BOOST_CHECK(!cache.HaveCoinInCache(tx->vin[0].prevout));
+ - ]
87 : : } else {
88 [ + + ]: 1584 : for (const auto& in : tx->vin) {
89 : 792 : const auto& outpoint{in.prevout};
90 [ + - ]: 792 : const auto& first{cache.AccessCoin(outpoint)};
91 [ + - ]: 792 : const auto& second{cache.AccessCoin(outpoint)};
92 [ + - + - ]: 792 : BOOST_CHECK_EQUAL(&first, &second);
93 [ + - ]: 792 : const auto have{cache.HaveCoinInCache(outpoint)};
94 [ + - + - ]: 792 : BOOST_CHECK_NE(txids.contains(outpoint.hash), have);
95 : 792 : counter += have;
96 : : }
97 [ + - ]: 792 : txids.emplace(tx->GetHash());
98 : : }
99 : : }
100 [ + - + - : 8 : BOOST_CHECK_EQUAL(cache.GetCacheSize(), counter);
+ - ]
101 : 8 : }
102 : :
103 : : } // namespace
104 : :
105 : : BOOST_AUTO_TEST_SUITE(coinsviewoverlay_tests)
106 : :
107 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(fetch_inputs_from_db)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
108 : : {
109 : 1 : const auto block{CreateBlock()};
110 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
111 [ + - ]: 1 : PopulateView(block, db);
112 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
113 [ + - - + ]: 1 : CoinsViewOverlay view{&main_cache, MakeStartedThreadPool()};
114 : 1 : const auto reset_guard{view.StartFetching(block)};
115 [ + - ]: 1 : const auto& outpoint{block.vtx[1]->vin[0].prevout};
116 : :
117 [ + - + - : 2 : BOOST_CHECK(view.HaveCoin(outpoint));
+ - + - ]
118 [ + - + - : 2 : BOOST_CHECK(view.GetCoin(outpoint).has_value());
+ - + - ]
119 [ + - + - : 2 : BOOST_CHECK(!main_cache.HaveCoinInCache(outpoint));
+ - + - ]
120 : :
121 [ + - ]: 1 : CheckCache(block, view);
122 : : // Check that no coins have been moved up to main cache from db
123 [ + + ]: 101 : for (const auto& tx : block.vtx) {
124 [ + + ]: 200 : for (const auto& in : tx->vin) {
125 [ + - + - : 200 : BOOST_CHECK(!main_cache.HaveCoinInCache(in.prevout));
+ - ]
126 : : }
127 : : }
128 : :
129 [ + - ]: 1 : view.SetBestBlock(uint256::ONE);
130 [ + - + - : 2 : BOOST_CHECK(view.SpendCoin(outpoint));
+ - + - ]
131 [ + - ]: 1 : view.Flush();
132 [ + - + - : 2 : BOOST_CHECK(!main_cache.PeekCoin(outpoint).has_value());
+ - ]
133 [ + - + - ]: 2 : }
134 : :
135 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(fetch_inputs_from_cache)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
136 : : {
137 : 1 : const auto block{CreateBlock()};
138 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
139 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
140 [ + - ]: 1 : PopulateView(block, main_cache);
141 [ + - - + ]: 1 : CoinsViewOverlay view{&main_cache, MakeStartedThreadPool()};
142 : 1 : const auto reset_guard{view.StartFetching(block)};
143 [ + - ]: 1 : CheckCache(block, view);
144 : :
145 [ + - ]: 1 : const auto& outpoint{block.vtx[1]->vin[0].prevout};
146 [ + - ]: 1 : view.SetBestBlock(uint256::ONE);
147 [ + - + - : 2 : BOOST_CHECK(view.SpendCoin(outpoint));
+ - + - ]
148 [ + - ]: 1 : view.Flush();
149 [ + - + - : 2 : BOOST_CHECK(!main_cache.PeekCoin(outpoint).has_value());
+ - ]
150 [ + - + - ]: 2 : }
151 : :
152 : : // Test for the case where a block spends coins that are spent in the cache, but
153 : : // the spentness has not been flushed to the db.
154 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(fetch_no_double_spend)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
155 : : {
156 : 1 : const auto block{CreateBlock()};
157 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
158 [ + - ]: 1 : PopulateView(block, db);
159 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
160 : : // Add all inputs as spent already in cache
161 [ + - ]: 1 : PopulateView(block, main_cache, /*spent=*/true);
162 [ + - - + ]: 1 : CoinsViewOverlay view{&main_cache, MakeStartedThreadPool()};
163 : 1 : const auto reset_guard{view.StartFetching(block)};
164 [ + + ]: 101 : for (const auto& tx : block.vtx) {
165 [ + + ]: 200 : for (const auto& in : tx->vin) {
166 [ + - ]: 100 : const auto& c{view.AccessCoin(in.prevout)};
167 [ + - + - : 200 : BOOST_CHECK(c.IsSpent());
+ - ]
168 [ + - + - : 200 : BOOST_CHECK(!view.HaveCoin(in.prevout));
+ - + - ]
169 [ + - + - : 200 : BOOST_CHECK(!view.GetCoin(in.prevout));
+ - ]
170 : : }
171 : : }
172 : : // Coins are not added to the view, even though they exist unspent in the parent db
173 [ + - + - : 1 : BOOST_CHECK_EQUAL(view.GetCacheSize(), 0);
+ - ]
174 [ + - + - ]: 2 : }
175 : :
176 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(fetch_no_inputs)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
177 : : {
178 : 1 : const auto block{CreateBlock()};
179 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
180 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
181 [ + - - + ]: 1 : CoinsViewOverlay view{&main_cache, MakeStartedThreadPool()};
182 : 1 : const auto reset_guard{view.StartFetching(block)};
183 [ + + ]: 101 : for (const auto& tx : block.vtx) {
184 [ + + ]: 200 : for (const auto& in : tx->vin) {
185 [ + - ]: 100 : const auto& c{view.AccessCoin(in.prevout)};
186 [ + - + - : 200 : BOOST_CHECK(c.IsSpent());
+ - ]
187 [ + - + - : 200 : BOOST_CHECK(!view.HaveCoin(in.prevout));
+ - + - ]
188 [ + - + - : 200 : BOOST_CHECK(!view.GetCoin(in.prevout));
+ - ]
189 : : }
190 : : }
191 [ + - + - : 1 : BOOST_CHECK_EQUAL(view.GetCacheSize(), 0);
+ - ]
192 [ + - + - ]: 2 : }
193 : :
194 : : // Access coins that are not block inputs
195 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(access_non_input_coins)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
196 : : {
197 : 1 : CBlock block;
198 [ + - ]: 1 : CMutableTransaction coinbase;
199 [ + - ]: 1 : coinbase.vin.emplace_back();
200 [ + - + - : 2 : block.vtx.push_back(MakeTransactionRef(coinbase));
- + ]
201 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
202 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
203 : 1 : Coin coin{};
204 : 1 : coin.out.nValue = 1;
205 [ + - ]: 1 : const COutPoint outpoint{Txid::FromUint256(uint256::ZERO), 0};
206 [ + - ]: 1 : main_cache.AddCoin(outpoint, std::move(coin), /*possible_overwrite=*/true);
207 : :
208 [ + - - + ]: 1 : CoinsViewOverlay view{&main_cache, MakeStartedThreadPool()};
209 : : // The block has no non-coinbase transactions, so this fetches nothing and only creates the
210 : : // reset guard. All lookups below use the fallback path.
211 : 1 : const auto reset_guard{view.StartFetching(block)};
212 : :
213 : : // Non-input fallback hit.
214 [ + - + - : 2 : BOOST_CHECK(!view.AccessCoin(outpoint).IsSpent());
+ - + - ]
215 : :
216 : : // Non-input fallback miss.
217 [ + - ]: 1 : const COutPoint missing_outpoint{Txid::FromUint256(uint256::ONE), 0};
218 [ + - + - : 2 : BOOST_CHECK(view.AccessCoin(missing_outpoint).IsSpent());
+ - + - ]
219 [ + - + - : 2 : BOOST_CHECK(!view.HaveCoinInCache(missing_outpoint));
+ - ]
220 [ + - + - ]: 3 : }
221 : :
222 : : // Access a fetched input out of order (i.e. not the next one in m_inputs).
223 : : // FetchCoinFromBase must fall back to base->PeekCoin, and the coin must still
224 : : // be inserted into the cache.
225 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(fetch_out_of_order_input_uses_normal_lookup)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
226 : : {
227 : 1 : const auto block{CreateBlock()};
228 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
229 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
230 [ + - ]: 1 : PopulateView(block, main_cache);
231 : :
232 : 1 : std::vector<COutPoint> fetched_inputs;
233 [ + - ]: 1 : std::unordered_set<Txid, SaltedCoinsCacheHasher> txids;
234 [ - + + - ]: 1 : txids.reserve(block.vtx.size() - 1);
235 [ + + ]: 100 : for (const auto& tx : block.vtx | std::views::drop(1)) {
236 [ + + ]: 198 : for (const auto& input : tx->vin) {
237 [ + + + - ]: 99 : if (!txids.contains(input.prevout.hash)) fetched_inputs.push_back(input.prevout);
238 : : }
239 [ + - ]: 99 : txids.emplace(tx->GetHash());
240 : : }
241 [ + - - + : 1 : BOOST_REQUIRE_GE(fetched_inputs.size(), 2U);
+ - ]
242 : :
243 [ + - - + ]: 1 : CoinsViewOverlay view{&main_cache, MakeStartedThreadPool()};
244 : 1 : const auto reset_guard{view.StartFetching(block)};
245 : :
246 [ + - ]: 1 : const auto& out_of_order_input{fetched_inputs[1]};
247 [ + - + - : 2 : BOOST_CHECK(!view.HaveCoinInCache(out_of_order_input));
+ - + - ]
248 [ + - + - : 2 : BOOST_CHECK(!view.AccessCoin(out_of_order_input).IsSpent());
+ - + - ]
249 [ + - + - : 2 : BOOST_CHECK(view.HaveCoinInCache(out_of_order_input));
+ - + - ]
250 : :
251 [ + - ]: 1 : CheckCache(block, view);
252 [ + - + - ]: 2 : }
253 : :
254 : : // The ResetGuard returned by StartFetching must clear all per-block state when
255 : : // it goes out of scope, so the overlay can be reused for a subsequent block.
256 : : // Flush must also clear all per-block state to be reused.
257 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(fetch_state_is_reusable_after_teardown)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
258 : : {
259 : 1 : const auto block{CreateBlock()};
260 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
261 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
262 [ + - ]: 1 : PopulateView(block, main_cache);
263 [ + - - + ]: 1 : CoinsViewOverlay view{&main_cache, MakeStartedThreadPool()};
264 : :
265 [ + + ]: 4 : for (const bool use_flush : {false, true, false}) {
266 : 3 : {
267 : 3 : const auto reset_guard{view.StartFetching(block)};
268 [ + - ]: 3 : CheckCache(block, view);
269 [ + - + - : 3 : BOOST_CHECK_GT(view.GetCacheSize(), 0U);
+ - ]
270 [ + + ]: 3 : if (use_flush) {
271 [ + - ]: 1 : view.SetBestBlock(uint256::ONE);
272 [ + - ]: 1 : view.Flush();
273 : : }
274 : 0 : }
275 [ + - + - : 3 : BOOST_CHECK_EQUAL(view.GetCacheSize(), 0U);
+ - ]
276 : : }
277 [ + - + - ]: 2 : }
278 : :
279 : : BOOST_AUTO_TEST_SUITE_END()
280 : :
281 : : BOOST_AUTO_TEST_SUITE(coinsviewoverlay_tests_noworkers)
282 : :
283 : : // Test that disabled input fetching falls back to normal cache lookups via base->PeekCoin.
284 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(fetch_unstarted_thread_pool)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
285 : : {
286 : 1 : const auto block{CreateBlock()};
287 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
288 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
289 [ + - ]: 1 : PopulateView(block, main_cache);
290 [ + - ]: 1 : auto thread_pool{util::MakeNotNullShared<ThreadPool>("fetch_none")};
291 [ + - - + ]: 2 : CoinsViewOverlay view{&main_cache, thread_pool};
292 : 1 : const auto reset_guard{view.StartFetching(block)};
293 [ + - ]: 1 : CheckCache(block, view);
294 [ + - + - : 3 : }
+ - ]
295 : :
296 : : // Test that an interrupted thread pool falls back to normal cache lookups via base->PeekCoin.
297 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(fetch_interrupted_thread_pool_uses_normal_lookup)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
298 : : {
299 : 1 : const auto block{CreateBlock()};
300 : 0 : CCoinsViewDB db{{.path = "", .cache_bytes = 1_MiB, .memory_only = true}, {}};
301 [ + - ]: 1 : CCoinsViewCache main_cache{&db};
302 [ + - ]: 1 : PopulateView(block, main_cache);
303 : :
304 [ + - ]: 1 : auto thread_pool{util::MakeNotNullShared<ThreadPool>("fetch_intr")};
305 [ + - ]: 1 : thread_pool->Start(DEFAULT_PREVOUTFETCH_THREADS);
306 [ + - ]: 1 : thread_pool->Interrupt();
307 [ + - - + ]: 2 : CoinsViewOverlay view{&main_cache, thread_pool};
308 : 1 : const auto reset_guard{view.StartFetching(block)};
309 [ + - ]: 1 : CheckCache(block, view);
310 [ + - + - : 3 : }
+ - ]
311 : :
312 : : BOOST_AUTO_TEST_SUITE_END()
|