Skip to content

Commit c3b0e6c

Browse files
committed
validation: make CCheckQueueControl's CCheckQueue non-optional
This simplifies the construction logic and will allow the constructor and destructor to lock and unlock uncondiationally.
1 parent 4c8c90b commit c3b0e6c

File tree

6 files changed

+18
-26
lines changed

6 files changed

+18
-26
lines changed

src/bench/checkqueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
5656

5757
bench.minEpochIterations(10).batch(BATCH_SIZE * BATCHES).unit("job").run([&] {
5858
// Make insecure_rand here so that each iteration is identical.
59-
CCheckQueueControl<PrevectorJob> control(&queue);
59+
CCheckQueueControl<PrevectorJob> control(queue);
6060
for (auto vChecks : vBatches) {
6161
control.Add(std::move(vChecks));
6262
}

src/checkqueue.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,43 +208,35 @@ template <typename T, typename R = std::remove_cvref_t<decltype(std::declval<T>(
208208
class CCheckQueueControl
209209
{
210210
private:
211-
CCheckQueue<T, R> * const pqueue;
211+
CCheckQueue<T, R>& m_queue;
212212
bool fDone;
213213

214214
public:
215215
CCheckQueueControl() = delete;
216216
CCheckQueueControl(const CCheckQueueControl&) = delete;
217217
CCheckQueueControl& operator=(const CCheckQueueControl&) = delete;
218-
explicit CCheckQueueControl(CCheckQueue<T> * const pqueueIn) : pqueue(pqueueIn), fDone(false)
218+
explicit CCheckQueueControl(CCheckQueue<T>& queueIn) : m_queue(queueIn), fDone(false)
219219
{
220-
// passed queue is supposed to be unused, or nullptr
221-
if (pqueue != nullptr) {
222-
ENTER_CRITICAL_SECTION(pqueue->m_control_mutex);
223-
}
220+
ENTER_CRITICAL_SECTION(m_queue.m_control_mutex);
224221
}
225222

226223
std::optional<R> Complete()
227224
{
228-
if (pqueue == nullptr) return std::nullopt;
229-
auto ret = pqueue->Complete();
225+
auto ret = m_queue.Complete();
230226
fDone = true;
231227
return ret;
232228
}
233229

234230
void Add(std::vector<T>&& vChecks)
235231
{
236-
if (pqueue != nullptr) {
237-
pqueue->Add(std::move(vChecks));
238-
}
232+
m_queue.Add(std::move(vChecks));
239233
}
240234

241235
~CCheckQueueControl()
242236
{
243237
if (!fDone)
244238
Complete();
245-
if (pqueue != nullptr) {
246-
LEAVE_CRITICAL_SECTION(pqueue->m_control_mutex);
247-
}
239+
LEAVE_CRITICAL_SECTION(m_queue.m_control_mutex);
248240
}
249241
};
250242

src/test/checkqueue_tests.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void CheckQueueTest::Correct_Queue_range(std::vector<size_t> range)
165165
for (const size_t i : range) {
166166
size_t total = i;
167167
FakeCheckCheckCompletion::n_calls = 0;
168-
CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
168+
CCheckQueueControl<FakeCheckCheckCompletion> control(*small_queue);
169169
while (total) {
170170
vChecks.clear();
171171
vChecks.resize(std::min<size_t>(total, m_rng.randrange(10)));
@@ -220,7 +220,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
220220
{
221221
auto fixed_queue = std::make_unique<Fixed_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
222222
for (size_t i = 0; i < 1001; ++i) {
223-
CCheckQueueControl<FixedCheck> control(fixed_queue.get());
223+
CCheckQueueControl<FixedCheck> control(*fixed_queue);
224224
size_t remaining = i;
225225
while (remaining) {
226226
size_t r = m_rng.randrange(10);
@@ -246,7 +246,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
246246
auto fail_queue = std::make_unique<Fixed_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
247247
for (auto times = 0; times < 10; ++times) {
248248
for (const bool end_fails : {true, false}) {
249-
CCheckQueueControl<FixedCheck> control(fail_queue.get());
249+
CCheckQueueControl<FixedCheck> control(*fail_queue);
250250
{
251251
std::vector<FixedCheck> vChecks;
252252
vChecks.resize(100, FixedCheck(std::nullopt));
@@ -268,7 +268,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
268268
size_t COUNT = 100000;
269269
size_t total = COUNT;
270270
{
271-
CCheckQueueControl<UniqueCheck> control(queue.get());
271+
CCheckQueueControl<UniqueCheck> control(*queue);
272272
while (total) {
273273
size_t r = m_rng.randrange(10);
274274
std::vector<UniqueCheck> vChecks;
@@ -300,7 +300,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
300300
for (size_t i = 0; i < 1000; ++i) {
301301
size_t total = i;
302302
{
303-
CCheckQueueControl<MemoryCheck> control(queue.get());
303+
CCheckQueueControl<MemoryCheck> control(*queue);
304304
while (total) {
305305
size_t r = m_rng.randrange(10);
306306
std::vector<MemoryCheck> vChecks;
@@ -324,7 +324,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
324324
auto queue = std::make_unique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
325325
bool fails = false;
326326
std::thread t0([&]() {
327-
CCheckQueueControl<FrozenCleanupCheck> control(queue.get());
327+
CCheckQueueControl<FrozenCleanupCheck> control(*queue);
328328
std::vector<FrozenCleanupCheck> vChecks(1);
329329
control.Add(std::move(vChecks));
330330
auto result = control.Complete(); // Hangs here
@@ -364,7 +364,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
364364
for (size_t i = 0; i < 3; ++i) {
365365
tg.emplace_back(
366366
[&]{
367-
CCheckQueueControl<FakeCheck> control(queue.get());
367+
CCheckQueueControl<FakeCheck> control(*queue);
368368
// While sleeping, no other thread should execute to this point
369369
auto observed = ++nThreads;
370370
UninterruptibleSleep(std::chrono::milliseconds{10});
@@ -387,7 +387,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
387387
{
388388
std::unique_lock<std::mutex> l(m);
389389
tg.emplace_back([&]{
390-
CCheckQueueControl<FakeCheck> control(queue.get());
390+
CCheckQueueControl<FakeCheck> control(*queue);
391391
std::unique_lock<std::mutex> ll(m);
392392
has_lock = true;
393393
cv.notify_one();

src/test/fuzz/checkqueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ FUZZ_TARGET(checkqueue)
4949
(void)check_queue_1.Complete();
5050
}
5151

52-
CCheckQueueControl<DumbCheck> check_queue_control{&check_queue_2};
52+
CCheckQueueControl<DumbCheck> check_queue_control{check_queue_2};
5353
if (fuzzed_data_provider.ConsumeBool()) {
5454
check_queue_control.Add(std::move(checks_2));
5555
}

src/test/transaction_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
568568
// check all inputs concurrently, with the cache
569569
PrecomputedTransactionData txdata(tx);
570570
CCheckQueue<CScriptCheck> scriptcheckqueue(/*batch_size=*/128, /*worker_threads_num=*/20);
571-
CCheckQueueControl<CScriptCheck> control(&scriptcheckqueue);
571+
CCheckQueueControl<CScriptCheck> control(scriptcheckqueue);
572572

573573
std::vector<Coin> coins;
574574
for(uint32_t i = 0; i < mtx.vin.size(); i++) {

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
26122612
// doesn't invalidate pointers into the vector, and keep txsdata in scope
26132613
// for as long as `control`.
26142614
std::optional<CCheckQueueControl<CScriptCheck>> control;
2615-
if (fScriptChecks && parallel_script_checks) control.emplace(&m_chainman.GetCheckQueue());
2615+
if (fScriptChecks && parallel_script_checks) control.emplace(m_chainman.GetCheckQueue());
26162616

26172617
std::vector<PrecomputedTransactionData> txsdata(block.vtx.size());
26182618

0 commit comments

Comments
 (0)