2020-04-07 08:03:39 -04:00
|
|
|
// Copyright (c) 2020 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <consensus/validation.h>
|
|
|
|
#include <primitives/block.h>
|
|
|
|
#include <scheduler.h>
|
2020-04-08 10:11:46 -04:00
|
|
|
#include <test/util/setup_common.h>
|
2020-04-07 08:03:39 -04:00
|
|
|
#include <util/check.h>
|
|
|
|
#include <validationinterface.h>
|
|
|
|
|
2020-04-08 10:11:46 -04:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(validationinterface_tests, TestingSetup)
|
2020-04-07 08:03:39 -04:00
|
|
|
|
2020-04-23 14:04:02 -04:00
|
|
|
struct TestSubscriberNoop final : public CValidationInterface {
|
|
|
|
void BlockChecked(const CBlock&, const BlockValidationState&) override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
|
|
|
|
{
|
|
|
|
std::atomic<bool> generate{true};
|
|
|
|
|
|
|
|
// Start thread to generate notifications
|
|
|
|
std::thread gen{[&] {
|
|
|
|
const CBlock block_dummy;
|
2020-05-14 12:31:57 -04:00
|
|
|
BlockValidationState state_dummy;
|
2020-04-23 14:04:02 -04:00
|
|
|
while (generate) {
|
|
|
|
GetMainSignals().BlockChecked(block_dummy, state_dummy);
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Start thread to consume notifications
|
|
|
|
std::thread sub{[&] {
|
|
|
|
// keep going for about 1 sec, which is 250k iterations
|
|
|
|
for (int i = 0; i < 250000; i++) {
|
2020-04-27 10:35:32 -04:00
|
|
|
auto sub = std::make_shared<TestSubscriberNoop>();
|
|
|
|
RegisterSharedValidationInterface(sub);
|
|
|
|
UnregisterSharedValidationInterface(sub);
|
2020-04-23 14:04:02 -04:00
|
|
|
}
|
|
|
|
// tell the other thread we are done
|
|
|
|
generate = false;
|
|
|
|
}};
|
|
|
|
|
|
|
|
gen.join();
|
|
|
|
sub.join();
|
|
|
|
BOOST_CHECK(!generate);
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:03:39 -04:00
|
|
|
class TestInterface : public CValidationInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestInterface(std::function<void()> on_call = nullptr, std::function<void()> on_destroy = nullptr)
|
|
|
|
: m_on_call(std::move(on_call)), m_on_destroy(std::move(on_destroy))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~TestInterface()
|
|
|
|
{
|
|
|
|
if (m_on_destroy) m_on_destroy();
|
|
|
|
}
|
|
|
|
void BlockChecked(const CBlock& block, const BlockValidationState& state) override
|
|
|
|
{
|
|
|
|
if (m_on_call) m_on_call();
|
|
|
|
}
|
|
|
|
static void Call()
|
|
|
|
{
|
|
|
|
CBlock block;
|
|
|
|
BlockValidationState state;
|
|
|
|
GetMainSignals().BlockChecked(block, state);
|
|
|
|
}
|
|
|
|
std::function<void()> m_on_call;
|
|
|
|
std::function<void()> m_on_destroy;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Regression test to ensure UnregisterAllValidationInterfaces calls don't
|
|
|
|
// destroy a validation interface while it is being called. Bug:
|
|
|
|
// https://github.com/bitcoin/bitcoin/pull/18551
|
|
|
|
BOOST_AUTO_TEST_CASE(unregister_all_during_call)
|
|
|
|
{
|
|
|
|
bool destroyed = false;
|
|
|
|
RegisterSharedValidationInterface(std::make_shared<TestInterface>(
|
|
|
|
[&] {
|
|
|
|
// First call should decrements reference count 2 -> 1
|
|
|
|
UnregisterAllValidationInterfaces();
|
|
|
|
BOOST_CHECK(!destroyed);
|
|
|
|
// Second call should not decrement reference count 1 -> 0
|
|
|
|
UnregisterAllValidationInterfaces();
|
|
|
|
BOOST_CHECK(!destroyed);
|
|
|
|
},
|
|
|
|
[&] { destroyed = true; }));
|
|
|
|
TestInterface::Call();
|
|
|
|
BOOST_CHECK(destroyed);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|