|
| 1 | +// Copyright (c) 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 | + |
| 5 | +#include <boost/test/unit_test.hpp> |
| 6 | + |
| 7 | +#include <consensus/validation.h> |
| 8 | +#include <primitives/block.h> |
| 9 | +#include <scheduler.h> |
| 10 | +#include <util/check.h> |
| 11 | +#include <validationinterface.h> |
| 12 | + |
| 13 | +BOOST_AUTO_TEST_SUITE(validationinterface_tests) |
| 14 | + |
| 15 | +class TestInterface : public CValidationInterface |
| 16 | +{ |
| 17 | +public: |
| 18 | + TestInterface(std::function<void()> on_call = nullptr, std::function<void()> on_destroy = nullptr) |
| 19 | + : m_on_call(std::move(on_call)), m_on_destroy(std::move(on_destroy)) |
| 20 | + { |
| 21 | + } |
| 22 | + virtual ~TestInterface() |
| 23 | + { |
| 24 | + if (m_on_destroy) m_on_destroy(); |
| 25 | + } |
| 26 | + void BlockChecked(const CBlock& block, const BlockValidationState& state) override |
| 27 | + { |
| 28 | + if (m_on_call) m_on_call(); |
| 29 | + } |
| 30 | + static void Call() |
| 31 | + { |
| 32 | + CBlock block; |
| 33 | + BlockValidationState state; |
| 34 | + GetMainSignals().BlockChecked(block, state); |
| 35 | + } |
| 36 | + std::function<void()> m_on_call; |
| 37 | + std::function<void()> m_on_destroy; |
| 38 | +}; |
| 39 | + |
| 40 | +// Regression test to ensure UnregisterAllValidationInterfaces calls don't |
| 41 | +// destroy a validation interface while it is being called. Bug: |
| 42 | +// https://github.com/bitcoin/bitcoin/pull/18551 |
| 43 | +BOOST_AUTO_TEST_CASE(unregister_all_during_call) |
| 44 | +{ |
| 45 | + bool destroyed = false; |
| 46 | + |
| 47 | + CScheduler scheduler; |
| 48 | + GetMainSignals().RegisterBackgroundSignalScheduler(scheduler); |
| 49 | + RegisterSharedValidationInterface(std::make_shared<TestInterface>( |
| 50 | + [&] { |
| 51 | + // First call should decrements reference count 2 -> 1 |
| 52 | + UnregisterAllValidationInterfaces(); |
| 53 | + BOOST_CHECK(!destroyed); |
| 54 | + // Second call should not decrement reference count 1 -> 0 |
| 55 | + UnregisterAllValidationInterfaces(); |
| 56 | + BOOST_CHECK(!destroyed); |
| 57 | + }, |
| 58 | + [&] { destroyed = true; })); |
| 59 | + TestInterface::Call(); |
| 60 | + BOOST_CHECK(destroyed); |
| 61 | +} |
| 62 | + |
| 63 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments