Bitcoin Core  22.99.0
P2P Digital Currency
cuckoocache_tests.cpp
Go to the documentation of this file.
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>
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 
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  */
36 BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
37 {
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 
53 template <typename Cache>
54 static double test_cache(size_t megabytes, double load)
55 {
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  }
72  std::vector<uint256> hashes_insert_copy = hashes;
74  for (const uint256& h : hashes_insert_copy)
75  set.insert(h);
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 
101 static double normalize_hit_rate(double hits, double load)
102 {
103  return hits * std::max(load, 1.0);
104 }
105 
107 BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
108 {
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 
123 template <typename Cache>
124 static void test_cache_erase(size_t megabytes)
125 {
126  double load = 1;
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  }
143  std::vector<uint256> hashes_insert_copy = hashes;
144 
146  for (uint32_t i = 0; i < (n_insert / 2); ++i)
147  set.insert(hashes_insert_copy[i]);
149  for (uint32_t i = 0; i < (n_insert / 4); ++i)
150  BOOST_CHECK(set.contains(hashes[i], true));
152  for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
153  set.insert(hashes_insert_copy[i]);
154 
156  size_t count_erased_but_contained = 0;
158  size_t count_stale = 0;
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 
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>
187 static void test_cache_erase_parallel(size_t megabytes)
188 {
189  double load = 1;
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  }
206  std::vector<uint256> hashes_insert_copy = hashes;
207  std::shared_mutex mtx;
208 
209  {
211  std::unique_lock<std::shared_mutex> l(mtx);
213  for (uint32_t i = 0; i < (n_insert / 2); ++i)
214  set.insert(hashes_insert_copy[i]);
215  }
216 
219  std::vector<std::thread> threads;
221  for (uint32_t x = 0; x < 3; ++x)
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 
237  for (std::thread& t : threads)
238  t.join();
240  std::unique_lock<std::shared_mutex> l(mtx);
242  for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
243  set.insert(hashes_insert_copy[i]);
244 
246  size_t count_erased_but_contained = 0;
248  size_t count_stale = 0;
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 }
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>
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.
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 }
366 BOOST_AUTO_TEST_CASE(cuckoocache_generations)
367 {
368  test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
369 }
370 
count
static int count
Definition: tests.c:41
assert
assert(!tx.IsCoinBase())
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
SeedInsecureRand
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
Definition: setup_common.h:56
CuckooCache::cache
cache implements a cache with properties similar to a cuckoo-set.
Definition: cuckoocache.h:158
setup_common.h
InsecureRand256
static uint256 InsecureRand256()
Definition: setup_common.h:66
normalize_hit_rate
static double normalize_hit_rate(double hits, double load)
The normalized hit rate for a given load.
Definition: cuckoocache_tests.cpp:101
InsecureRand32
static uint32_t InsecureRand32()
Definition: setup_common.h:65
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
test_cache_erase
static void test_cache_erase(size_t megabytes)
This helper checks that erased elements are preferentially inserted onto and that the hit rate of "fr...
Definition: cuckoocache_tests.cpp:124
cuckoocache.h
SeedRand::ZEROS
@ ZEROS
Seed with a compile time constant of zeros.
random.h
test_cache_generations
static void test_cache_generations()
Definition: cuckoocache_tests.cpp:277
uint256
256-bit opaque blob.
Definition: uint256.h:124
test_cache_erase_parallel
static void test_cache_erase_parallel(size_t megabytes)
Definition: cuckoocache_tests.cpp:187
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
Definition: cuckoocache_tests.cpp:36
CuckooCache::cache::setup_bytes
uint32_t setup_bytes(size_t bytes)
setup_bytes is a convenience function which accounts for internal memory usage when deciding how many...
Definition: cuckoocache.h:366
sigcache.h
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
test_cache
static double test_cache(size_t megabytes, double load)
This helper returns the hit rate when megabytes*load worth of entries are inserted into a megabytes s...
Definition: cuckoocache_tests.cpp:54