Bitcoin Core  26.1.0
P2P Digital Currency
checkqueue_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2022 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 <checkqueue.h>
6 #include <common/args.h>
7 #include <sync.h>
8 #include <test/util/random.h>
10 #include <util/chaintype.h>
11 #include <util/time.h>
12 
13 #include <boost/test/unit_test.hpp>
14 
15 #include <atomic>
16 #include <condition_variable>
17 #include <mutex>
18 #include <thread>
19 #include <unordered_set>
20 #include <utility>
21 #include <vector>
22 
30 #ifdef DEBUG_LOCKCONTENTION
31  : TestingSetup{ChainType::MAIN, /*extra_args=*/{"-debugexclude=lock"}} {}
32 #else
34 #endif
35 };
36 
37 BOOST_FIXTURE_TEST_SUITE(checkqueue_tests, NoLockLoggingTestingSetup)
38 
39 static const unsigned int QUEUE_BATCH_SIZE = 128;
40 static const int SCRIPT_CHECK_THREADS = 3;
41 
42 struct FakeCheck {
43  bool operator()() const
44  {
45  return true;
46  }
47 };
48 
50  static std::atomic<size_t> n_calls;
51  bool operator()()
52  {
53  n_calls.fetch_add(1, std::memory_order_relaxed);
54  return true;
55  }
56 };
57 
58 struct FailingCheck {
59  bool fails;
60  FailingCheck(bool _fails) : fails(_fails){};
61  bool operator()() const
62  {
63  return !fails;
64  }
65 };
66 
67 struct UniqueCheck {
68  static Mutex m;
69  static std::unordered_multiset<size_t> results GUARDED_BY(m);
70  size_t check_id;
71  UniqueCheck(size_t check_id_in) : check_id(check_id_in){};
72  bool operator()()
73  {
74  LOCK(m);
75  results.insert(check_id);
76  return true;
77  }
78 };
79 
80 
81 struct MemoryCheck {
82  static std::atomic<size_t> fake_allocated_memory;
83  bool b {false};
84  bool operator()() const
85  {
86  return true;
87  }
89  {
90  // We have to do this to make sure that destructor calls are paired
91  //
92  // Really, copy constructor should be deletable, but CCheckQueue breaks
93  // if it is deleted because of internal push_back.
94  fake_allocated_memory.fetch_add(b, std::memory_order_relaxed);
95  };
96  MemoryCheck(bool b_) : b(b_)
97  {
98  fake_allocated_memory.fetch_add(b, std::memory_order_relaxed);
99  };
101  {
102  fake_allocated_memory.fetch_sub(b, std::memory_order_relaxed);
103  };
104 };
105 
107  static std::atomic<uint64_t> nFrozen;
108  static std::condition_variable cv;
109  static std::mutex m;
110  bool should_freeze{true};
111  bool operator()() const
112  {
113  return true;
114  }
115  FrozenCleanupCheck() = default;
117  {
118  if (should_freeze) {
119  std::unique_lock<std::mutex> l(m);
120  nFrozen.store(1, std::memory_order_relaxed);
121  cv.notify_one();
122  cv.wait(l, []{ return nFrozen.load(std::memory_order_relaxed) == 0;});
123  }
124  }
126  {
127  should_freeze = other.should_freeze;
128  other.should_freeze = false;
129  }
131  {
132  should_freeze = other.should_freeze;
133  other.should_freeze = false;
134  return *this;
135  }
136 };
137 
138 // Static Allocations
139 std::mutex FrozenCleanupCheck::m{};
140 std::atomic<uint64_t> FrozenCleanupCheck::nFrozen{0};
141 std::condition_variable FrozenCleanupCheck::cv{};
143 std::unordered_multiset<size_t> UniqueCheck::results;
144 std::atomic<size_t> FakeCheckCheckCompletion::n_calls{0};
145 std::atomic<size_t> MemoryCheck::fake_allocated_memory{0};
146 
147 // Queue Typedefs
154 
155 
159 static void Correct_Queue_range(std::vector<size_t> range)
160 {
161  auto small_queue = std::make_unique<Correct_Queue>(QUEUE_BATCH_SIZE);
162  small_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
163  // Make vChecks here to save on malloc (this test can be slow...)
164  std::vector<FakeCheckCheckCompletion> vChecks;
165  vChecks.reserve(9);
166  for (const size_t i : range) {
167  size_t total = i;
169  CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
170  while (total) {
171  vChecks.clear();
172  vChecks.resize(std::min<size_t>(total, InsecureRandRange(10)));
173  total -= vChecks.size();
174  control.Add(std::move(vChecks));
175  }
176  BOOST_REQUIRE(control.Wait());
177  BOOST_REQUIRE_EQUAL(FakeCheckCheckCompletion::n_calls, i);
178  }
179  small_queue->StopWorkerThreads();
180 }
181 
184 BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Zero)
185 {
186  std::vector<size_t> range;
187  range.push_back(size_t{0});
188  Correct_Queue_range(range);
189 }
192 BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_One)
193 {
194  std::vector<size_t> range;
195  range.push_back(size_t{1});
196  Correct_Queue_range(range);
197 }
200 BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Max)
201 {
202  std::vector<size_t> range;
203  range.push_back(100000);
204  Correct_Queue_range(range);
205 }
208 BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
209 {
210  std::vector<size_t> range;
211  range.reserve(100000/1000);
212  for (size_t i = 2; i < 100000; i += std::max((size_t)1, (size_t)InsecureRandRange(std::min((size_t)1000, ((size_t)100000) - i))))
213  range.push_back(i);
214  Correct_Queue_range(range);
215 }
216 
217 
219 BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
220 {
221  auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE);
222  fail_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
223 
224  for (size_t i = 0; i < 1001; ++i) {
225  CCheckQueueControl<FailingCheck> control(fail_queue.get());
226  size_t remaining = i;
227  while (remaining) {
228  size_t r = InsecureRandRange(10);
229 
230  std::vector<FailingCheck> vChecks;
231  vChecks.reserve(r);
232  for (size_t k = 0; k < r && remaining; k++, remaining--)
233  vChecks.emplace_back(remaining == 1);
234  control.Add(std::move(vChecks));
235  }
236  bool success = control.Wait();
237  if (i > 0) {
238  BOOST_REQUIRE(!success);
239  } else if (i == 0) {
240  BOOST_REQUIRE(success);
241  }
242  }
243  fail_queue->StopWorkerThreads();
244 }
245 // Test that a block validation which fails does not interfere with
246 // future blocks, ie, the bad state is cleared.
247 BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
248 {
249  auto fail_queue = std::make_unique<Failing_Queue>(QUEUE_BATCH_SIZE);
250  fail_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
251 
252  for (auto times = 0; times < 10; ++times) {
253  for (const bool end_fails : {true, false}) {
254  CCheckQueueControl<FailingCheck> control(fail_queue.get());
255  {
256  std::vector<FailingCheck> vChecks;
257  vChecks.resize(100, false);
258  vChecks[99] = end_fails;
259  control.Add(std::move(vChecks));
260  }
261  bool r =control.Wait();
262  BOOST_REQUIRE(r != end_fails);
263  }
264  }
265  fail_queue->StopWorkerThreads();
266 }
267 
268 // Test that unique checks are actually all called individually, rather than
269 // just one check being called repeatedly. Test that checks are not called
270 // more than once as well
271 BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
272 {
273  auto queue = std::make_unique<Unique_Queue>(QUEUE_BATCH_SIZE);
274  queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
275 
276  size_t COUNT = 100000;
277  size_t total = COUNT;
278  {
279  CCheckQueueControl<UniqueCheck> control(queue.get());
280  while (total) {
281  size_t r = InsecureRandRange(10);
282  std::vector<UniqueCheck> vChecks;
283  for (size_t k = 0; k < r && total; k++)
284  vChecks.emplace_back(--total);
285  control.Add(std::move(vChecks));
286  }
287  }
288  {
290  bool r = true;
291  BOOST_REQUIRE_EQUAL(UniqueCheck::results.size(), COUNT);
292  for (size_t i = 0; i < COUNT; ++i) {
293  r = r && UniqueCheck::results.count(i) == 1;
294  }
295  BOOST_REQUIRE(r);
296  }
297  queue->StopWorkerThreads();
298 }
299 
300 
301 // Test that blocks which might allocate lots of memory free their memory aggressively.
302 //
303 // This test attempts to catch a pathological case where by lazily freeing
304 // checks might mean leaving a check un-swapped out, and decreasing by 1 each
305 // time could leave the data hanging across a sequence of blocks.
306 BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
307 {
308  auto queue = std::make_unique<Memory_Queue>(QUEUE_BATCH_SIZE);
309  queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
310  for (size_t i = 0; i < 1000; ++i) {
311  size_t total = i;
312  {
313  CCheckQueueControl<MemoryCheck> control(queue.get());
314  while (total) {
315  size_t r = InsecureRandRange(10);
316  std::vector<MemoryCheck> vChecks;
317  for (size_t k = 0; k < r && total; k++) {
318  total--;
319  // Each iteration leaves data at the front, back, and middle
320  // to catch any sort of deallocation failure
321  vChecks.emplace_back(total == 0 || total == i || total == i/2);
322  }
323  control.Add(std::move(vChecks));
324  }
325  }
326  BOOST_REQUIRE_EQUAL(MemoryCheck::fake_allocated_memory, 0U);
327  }
328  queue->StopWorkerThreads();
329 }
330 
331 // Test that a new verification cannot occur until all checks
332 // have been destructed
333 BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
334 {
335  auto queue = std::make_unique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE);
336  bool fails = false;
337  queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
338  std::thread t0([&]() {
339  CCheckQueueControl<FrozenCleanupCheck> control(queue.get());
340  std::vector<FrozenCleanupCheck> vChecks(1);
341  control.Add(std::move(vChecks));
342  bool waitResult = control.Wait(); // Hangs here
343  assert(waitResult);
344  });
345  {
346  std::unique_lock<std::mutex> l(FrozenCleanupCheck::m);
347  // Wait until the queue has finished all jobs and frozen
348  FrozenCleanupCheck::cv.wait(l, [](){return FrozenCleanupCheck::nFrozen == 1;});
349  }
350  // Try to get control of the queue a bunch of times
351  for (auto x = 0; x < 100 && !fails; ++x) {
352  fails = queue->m_control_mutex.try_lock();
353  }
354  {
355  // Unfreeze (we need lock n case of spurious wakeup)
356  std::unique_lock<std::mutex> l(FrozenCleanupCheck::m);
358  }
359  // Awaken frozen destructor
360  FrozenCleanupCheck::cv.notify_one();
361  // Wait for control to finish
362  t0.join();
363  BOOST_REQUIRE(!fails);
364  queue->StopWorkerThreads();
365 }
366 
367 
369 BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
370 {
371  auto queue = std::make_unique<Standard_Queue>(QUEUE_BATCH_SIZE);
372  {
373  std::vector<std::thread> tg;
374  std::atomic<int> nThreads {0};
375  std::atomic<int> fails {0};
376  for (size_t i = 0; i < 3; ++i) {
377  tg.emplace_back(
378  [&]{
379  CCheckQueueControl<FakeCheck> control(queue.get());
380  // While sleeping, no other thread should execute to this point
381  auto observed = ++nThreads;
382  UninterruptibleSleep(std::chrono::milliseconds{10});
383  fails += observed != nThreads;
384  });
385  }
386  for (auto& thread: tg) {
387  if (thread.joinable()) thread.join();
388  }
389  BOOST_REQUIRE_EQUAL(fails, 0);
390  }
391  {
392  std::vector<std::thread> tg;
393  std::mutex m;
394  std::condition_variable cv;
395  bool has_lock{false};
396  bool has_tried{false};
397  bool done{false};
398  bool done_ack{false};
399  {
400  std::unique_lock<std::mutex> l(m);
401  tg.emplace_back([&]{
402  CCheckQueueControl<FakeCheck> control(queue.get());
403  std::unique_lock<std::mutex> ll(m);
404  has_lock = true;
405  cv.notify_one();
406  cv.wait(ll, [&]{return has_tried;});
407  done = true;
408  cv.notify_one();
409  // Wait until the done is acknowledged
410  //
411  cv.wait(ll, [&]{return done_ack;});
412  });
413  // Wait for thread to get the lock
414  cv.wait(l, [&](){return has_lock;});
415  bool fails = false;
416  for (auto x = 0; x < 100 && !fails; ++x) {
417  fails = queue->m_control_mutex.try_lock();
418  }
419  has_tried = true;
420  cv.notify_one();
421  cv.wait(l, [&](){return done;});
422  // Acknowledge the done
423  done_ack = true;
424  cv.notify_one();
425  BOOST_REQUIRE(!fails);
426  }
427  for (auto& thread: tg) {
428  if (thread.joinable()) thread.join();
429  }
430  }
431 }
CCheckQueue< FakeCheckCheckCompletion > Correct_Queue
CCheckQueue< FakeCheck > Standard_Queue
static std::atomic< uint64_t > nFrozen
CCheckQueue< MemoryCheck > Memory_Queue
assert(!tx.IsCoinBase())
static std::atomic< size_t > n_calls
FrozenCleanupCheck(FrozenCleanupCheck &&other) noexcept
MemoryCheck(bool b_)
CCheckQueue< UniqueCheck > Unique_Queue
static const int SCRIPT_CHECK_THREADS
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:17
bool operator()() const
FailingCheck(bool _fails)
UniqueCheck(size_t check_id_in)
static uint64_t InsecureRandRange(uint64_t range)
Definition: random.h:60
bool operator()() const
#define LOCK(cs)
Definition: sync.h:258
static void Correct_Queue_range(std::vector< size_t > range)
This test case checks that the CCheckQueue works properly with each specified size_t Checks pushed...
BOOST_AUTO_TEST_SUITE_END()
bool operator()() const
CCheckQueue< FrozenCleanupCheck > FrozenCleanup_Queue
BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Zero)
Test that 0 checks is correct.
static std::unordered_multiset< size_t > results GUARDED_BY(m)
Identical to TestingSetup but excludes lock contention logging if DEBUG_LOCKCONTENTION is defined...
Queue for verifications that have to be performed.
Definition: checkqueue.h:30
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
Definition: sync.h:92
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:23
FrozenCleanupCheck & operator=(FrozenCleanupCheck &&other) noexcept
static std::atomic< size_t > fake_allocated_memory
FrozenCleanupCheck()=default
static std::mutex m
static const unsigned int QUEUE_BATCH_SIZE
CCheckQueue< FailingCheck > Failing_Queue
MemoryCheck(const MemoryCheck &x)
static int COUNT
Definition: tests.c:39
static std::condition_variable cv
void Add(std::vector< T > &&vChecks)
Definition: checkqueue.h:234
Testing setup that configures a complete environment.
Definition: setup_common.h:77
static Mutex m