1 // Copyright (c) 2012-2020 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 #include <cuckoocache.h>
5 #include <random.h>
6 #include <script/sigcache.h>
7 #include <test/util/setup_common.h>
8 
9 #include <boost/test/unit_test.hpp>
10 
11 #include <deque>
12 #include <mutex>
13 #include <shared_mutex>
14 #include <thread>
15 #include <vector>
16 
17 /** Test Suite for CuckooCache
18  *
19  *  1. All tests should have a deterministic result (using insecure rand
20  *  with deterministic seeds)
21  *  2. Some test methods are templated to allow for easier testing
22  *  against new versions / comparing
23  *  3. Results should be treated as a regression test, i.e., did the behavior
24  *  change significantly from what was expected. This can be OK, depending on
25  *  the nature of the change, but requires updating the tests to reflect the new
26  *  expected behavior. For example improving the hit rate may cause some tests
27  *  using BOOST_CHECK_CLOSE to fail.
28  *
29  */
30 BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
31 
32 /* Test that no values not inserted into the cache are read out of it.
33  *
34  * There are no repeats in the first 200000 insecure_GetRandHash calls
35  */
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)36 BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
37 {
38     SeedInsecureRand(SeedRand::ZEROS);
39     CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
40     size_t megabytes = 4;
41     cc.setup_bytes(megabytes << 20);
42     for (int x = 0; x < 100000; ++x) {
43         cc.insert(InsecureRand256());
44     }
45     for (int x = 0; x < 100000; ++x) {
46         BOOST_CHECK(!cc.contains(InsecureRand256(), false));
47     }
48 };
49 
50 /** This helper returns the hit rate when megabytes*load worth of entries are
51  * inserted into a megabytes sized cache
52  */
53 template <typename Cache>
test_cache(size_t megabytes,double load)54 static double test_cache(size_t megabytes, double load)
55 {
56     SeedInsecureRand(SeedRand::ZEROS);
57     std::vector<uint256> hashes;
58     Cache set{};
59     size_t bytes = megabytes * (1 << 20);
60     set.setup_bytes(bytes);
61     uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
62     hashes.resize(n_insert);
63     for (uint32_t i = 0; i < n_insert; ++i) {
64         uint32_t* ptr = (uint32_t*)hashes[i].begin();
65         for (uint8_t j = 0; j < 8; ++j)
66             *(ptr++) = InsecureRand32();
67     }
68     /** We make a copy of the hashes because future optimizations of the
69      * cuckoocache may overwrite the inserted element, so the test is
70      * "future proofed".
71      */
72     std::vector<uint256> hashes_insert_copy = hashes;
73     /** Do the insert */
74     for (const uint256& h : hashes_insert_copy)
75         set.insert(h);
76     /** Count the hits */
77     uint32_t count = 0;
78     for (const uint256& h : hashes)
79         count += set.contains(h, false);
80     double hit_rate = ((double)count) / ((double)n_insert);
81     return hit_rate;
82 }
83 
84 /** The normalized hit rate for a given load.
85  *
86  * The semantics are a little confusing, so please see the below
87  * explanation.
88  *
89  * Examples:
90  *
91  * 1. at load 0.5, we expect a perfect hit rate, so we multiply by
92  * 1.0
93  * 2. at load 2.0, we expect to see half the entries, so a perfect hit rate
94  * would be 0.5. Therefore, if we see a hit rate of 0.4, 0.4*2.0 = 0.8 is the
95  * normalized hit rate.
96  *
97  * This is basically the right semantics, but has a bit of a glitch depending on
98  * how you measure around load 1.0 as after load 1.0 your normalized hit rate
99  * becomes effectively perfect, ignoring freshness.
100  */
normalize_hit_rate(double hits,double load)101 static double normalize_hit_rate(double hits, double load)
102 {
103     return hits * std::max(load, 1.0);
104 }
105 
106 /** Check the hit rate on loads ranging from 0.1 to 1.6 */
BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)107 BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
108 {
109     /** Arbitrarily selected Hit Rate threshold that happens to work for this test
110      * as a lower bound on performance.
111      */
112     double HitRateThresh = 0.98;
113     size_t megabytes = 4;
114     for (double load = 0.1; load < 2; load *= 2) {
115         double hits = test_cache<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes, load);
116         BOOST_CHECK(normalize_hit_rate(hits, load) > HitRateThresh);
117     }
118 }
119 
120 
121 /** This helper checks that erased elements are preferentially inserted onto and
122  * that the hit rate of "fresher" keys is reasonable*/
123 template <typename Cache>
test_cache_erase(size_t megabytes)124 static void test_cache_erase(size_t megabytes)
125 {
126     double load = 1;
127     SeedInsecureRand(SeedRand::ZEROS);
128     std::vector<uint256> hashes;
129     Cache set{};
130     size_t bytes = megabytes * (1 << 20);
131     set.setup_bytes(bytes);
132     uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
133     hashes.resize(n_insert);
134     for (uint32_t i = 0; i < n_insert; ++i) {
135         uint32_t* ptr = (uint32_t*)hashes[i].begin();
136         for (uint8_t j = 0; j < 8; ++j)
137             *(ptr++) = InsecureRand32();
138     }
139     /** We make a copy of the hashes because future optimizations of the
140      * cuckoocache may overwrite the inserted element, so the test is
141      * "future proofed".
142      */
143     std::vector<uint256> hashes_insert_copy = hashes;
144 
145     /** Insert the first half */
146     for (uint32_t i = 0; i < (n_insert / 2); ++i)
147         set.insert(hashes_insert_copy[i]);
148     /** Erase the first quarter */
149     for (uint32_t i = 0; i < (n_insert / 4); ++i)
150         BOOST_CHECK(set.contains(hashes[i], true));
151     /** Insert the second half */
152     for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
153         set.insert(hashes_insert_copy[i]);
154 
155     /** elements that we marked as erased but are still there */
156     size_t count_erased_but_contained = 0;
157     /** elements that we did not erase but are older */
158     size_t count_stale = 0;
159     /** elements that were most recently inserted */
160     size_t count_fresh = 0;
161 
162     for (uint32_t i = 0; i < (n_insert / 4); ++i)
163         count_erased_but_contained += set.contains(hashes[i], false);
164     for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
165         count_stale += set.contains(hashes[i], false);
166     for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
167         count_fresh += set.contains(hashes[i], false);
168 
169     double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
170     double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
171     double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
172 
173     // Check that our hit_rate_fresh is perfect
174     BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0);
175     // Check that we have a more than 2x better hit rate on stale elements than
176     // erased elements.
177     BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
178 }
179 
BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok)180 BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok)
181 {
182     size_t megabytes = 4;
183     test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
184 }
185 
186 template <typename Cache>
test_cache_erase_parallel(size_t megabytes)187 static void test_cache_erase_parallel(size_t megabytes)
188 {
189     double load = 1;
190     SeedInsecureRand(SeedRand::ZEROS);
191     std::vector<uint256> hashes;
192     Cache set{};
193     size_t bytes = megabytes * (1 << 20);
194     set.setup_bytes(bytes);
195     uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
196     hashes.resize(n_insert);
197     for (uint32_t i = 0; i < n_insert; ++i) {
198         uint32_t* ptr = (uint32_t*)hashes[i].begin();
199         for (uint8_t j = 0; j < 8; ++j)
200             *(ptr++) = InsecureRand32();
201     }
202     /** We make a copy of the hashes because future optimizations of the
203      * cuckoocache may overwrite the inserted element, so the test is
204      * "future proofed".
205      */
206     std::vector<uint256> hashes_insert_copy = hashes;
207     std::shared_mutex mtx;
208 
209     {
210         /** Grab lock to make sure we release inserts */
211         std::unique_lock<std::shared_mutex> l(mtx);
212         /** Insert the first half */
213         for (uint32_t i = 0; i < (n_insert / 2); ++i)
214             set.insert(hashes_insert_copy[i]);
215     }
216 
217     /** Spin up 3 threads to run contains with erase.
218      */
219     std::vector<std::thread> threads;
220     /** Erase the first quarter */
221     for (uint32_t x = 0; x < 3; ++x)
222         /** Each thread is emplaced with x copy-by-value
223         */
224         threads.emplace_back([&, x] {
225             std::shared_lock<std::shared_mutex> l(mtx);
226             size_t ntodo = (n_insert/4)/3;
227             size_t start = ntodo*x;
228             size_t end = ntodo*(x+1);
229             for (uint32_t i = start; i < end; ++i) {
230                 bool contains = set.contains(hashes[i], true);
231                 assert(contains);
232             }
233         });
234 
235     /** Wait for all threads to finish
236      */
237     for (std::thread& t : threads)
238         t.join();
239     /** Grab lock to make sure we observe erases */
240     std::unique_lock<std::shared_mutex> l(mtx);
241     /** Insert the second half */
242     for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
243         set.insert(hashes_insert_copy[i]);
244 
245     /** elements that we marked erased but that are still there */
246     size_t count_erased_but_contained = 0;
247     /** elements that we did not erase but are older */
248     size_t count_stale = 0;
249     /** elements that were most recently inserted */
250     size_t count_fresh = 0;
251 
252     for (uint32_t i = 0; i < (n_insert / 4); ++i)
253         count_erased_but_contained += set.contains(hashes[i], false);
254     for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
255         count_stale += set.contains(hashes[i], false);
256     for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
257         count_fresh += set.contains(hashes[i], false);
258 
259     double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
260     double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
261     double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
262 
263     // Check that our hit_rate_fresh is perfect
264     BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0);
265     // Check that we have a more than 2x better hit rate on stale elements than
266     // erased elements.
267     BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
268 }
BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok)269 BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok)
270 {
271     size_t megabytes = 4;
272     test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
273 }
274 
275 
276 template <typename Cache>
test_cache_generations()277 static void test_cache_generations()
278 {
279     // This test checks that for a simulation of network activity, the fresh hit
280     // rate is never below 99%, and the number of times that it is worse than
281     // 99.9% are less than 1% of the time.
282     double min_hit_rate = 0.99;
283     double tight_hit_rate = 0.999;
284     double max_rate_less_than_tight_hit_rate = 0.01;
285     // A cache that meets this specification is therefore shown to have a hit
286     // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) +
287     // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == 99.89%
288     // hit rate with low variance.
289 
290     // We use deterministic values, but this test has also passed on many
291     // iterations with non-deterministic values, so it isn't "overfit" to the
292     // specific entropy in FastRandomContext(true) and implementation of the
293     // cache.
294     SeedInsecureRand(SeedRand::ZEROS);
295 
296     // block_activity models a chunk of network activity. n_insert elements are
297     // added to the cache. The first and last n/4 are stored for removal later
298     // and the middle n/2 are not stored. This models a network which uses half
299     // the signatures of recently (since the last block) added transactions
300     // immediately and never uses the other half.
301     struct block_activity {
302         std::vector<uint256> reads;
303         block_activity(uint32_t n_insert, Cache& c) : reads()
304         {
305             std::vector<uint256> inserts;
306             inserts.resize(n_insert);
307             reads.reserve(n_insert / 2);
308             for (uint32_t i = 0; i < n_insert; ++i) {
309                 uint32_t* ptr = (uint32_t*)inserts[i].begin();
310                 for (uint8_t j = 0; j < 8; ++j)
311                     *(ptr++) = InsecureRand32();
312             }
313             for (uint32_t i = 0; i < n_insert / 4; ++i)
314                 reads.push_back(inserts[i]);
315             for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i)
316                 reads.push_back(inserts[i]);
317             for (const auto& h : inserts)
318                 c.insert(h);
319         }
320     };
321 
322     const uint32_t BLOCK_SIZE = 1000;
323     // We expect window size 60 to perform reasonably given that each epoch
324     // stores 45% of the cache size (~472k).
325     const uint32_t WINDOW_SIZE = 60;
326     const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2;
327     const double load = 10;
328     const size_t megabytes = 4;
329     const size_t bytes = megabytes * (1 << 20);
330     const uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
331 
332     std::vector<block_activity> hashes;
333     Cache set{};
334     set.setup_bytes(bytes);
335     hashes.reserve(n_insert / BLOCK_SIZE);
336     std::deque<block_activity> last_few;
337     uint32_t out_of_tight_tolerance = 0;
338     uint32_t total = n_insert / BLOCK_SIZE;
339     // we use the deque last_few to model a sliding window of blocks. at each
340     // step, each of the last WINDOW_SIZE block_activities checks the cache for
341     // POP_AMOUNT of the hashes that they inserted, and marks these erased.
342     for (uint32_t i = 0; i < total; ++i) {
343         if (last_few.size() == WINDOW_SIZE)
344             last_few.pop_front();
345         last_few.emplace_back(BLOCK_SIZE, set);
346         uint32_t count = 0;
347         for (auto& act : last_few)
348             for (uint32_t k = 0; k < POP_AMOUNT; ++k) {
349                 count += set.contains(act.reads.back(), true);
350                 act.reads.pop_back();
351             }
352         // We use last_few.size() rather than WINDOW_SIZE for the correct
353         // behavior on the first WINDOW_SIZE iterations where the deque is not
354         // full yet.
355         double hit = (double(count)) / (last_few.size() * POP_AMOUNT);
356         // Loose Check that hit rate is above min_hit_rate
357         BOOST_CHECK(hit > min_hit_rate);
358         // Tighter check, count number of times we are less than tight_hit_rate
359         // (and implicitly, greater than min_hit_rate)
360         out_of_tight_tolerance += hit < tight_hit_rate;
361     }
362     // Check that being out of tolerance happens less than
363     // max_rate_less_than_tight_hit_rate of the time
364     BOOST_CHECK(double(out_of_tight_tolerance) / double(total) < max_rate_less_than_tight_hit_rate);
365 }
BOOST_AUTO_TEST_CASE(cuckoocache_generations)366 BOOST_AUTO_TEST_CASE(cuckoocache_generations)
367 {
368     test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
369 }
370 
371 BOOST_AUTO_TEST_SUITE_END();
372