miner: Avoid stack-use-after-return in validationinterface
This is achieved by switching to a shared_ptr. Also, switch the validationinterfaces in the tests to use shared_ptrs for the same reason.
This commit is contained in:
parent
fa5ceb25fc
commit
7777f2a4bb
|
@ -874,7 +874,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
class submitblock_StateCatcher : public CValidationInterface
|
class submitblock_StateCatcher final : public CValidationInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
uint256 hash;
|
uint256 hash;
|
||||||
|
@ -942,17 +942,17 @@ static UniValue submitblock(const JSONRPCRequest& request)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool new_block;
|
bool new_block;
|
||||||
submitblock_StateCatcher sc(block.GetHash());
|
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
|
||||||
RegisterValidationInterface(&sc);
|
RegisterSharedValidationInterface(sc);
|
||||||
bool accepted = ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
|
bool accepted = ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
|
||||||
UnregisterValidationInterface(&sc);
|
UnregisterSharedValidationInterface(sc);
|
||||||
if (!new_block && accepted) {
|
if (!new_block && accepted) {
|
||||||
return "duplicate";
|
return "duplicate";
|
||||||
}
|
}
|
||||||
if (!sc.found) {
|
if (!sc->found) {
|
||||||
return "inconclusive";
|
return "inconclusive";
|
||||||
}
|
}
|
||||||
return BIP22ValidationResult(sc.state);
|
return BIP22ValidationResult(sc->state);
|
||||||
}
|
}
|
||||||
|
|
||||||
static UniValue submitheader(const JSONRPCRequest& request)
|
static UniValue submitheader(const JSONRPCRequest& request)
|
||||||
|
|
|
@ -32,7 +32,7 @@ struct MinerTestingSetup : public RegTestingSetup {
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_SUITE(validation_block_tests, MinerTestingSetup)
|
BOOST_FIXTURE_TEST_SUITE(validation_block_tests, MinerTestingSetup)
|
||||||
|
|
||||||
struct TestSubscriber : public CValidationInterface {
|
struct TestSubscriber final : public CValidationInterface {
|
||||||
uint256 m_expected_tip;
|
uint256 m_expected_tip;
|
||||||
|
|
||||||
explicit TestSubscriber(uint256 tip) : m_expected_tip(tip) {}
|
explicit TestSubscriber(uint256 tip) : m_expected_tip(tip) {}
|
||||||
|
@ -175,8 +175,8 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
initial_tip = ::ChainActive().Tip();
|
initial_tip = ::ChainActive().Tip();
|
||||||
}
|
}
|
||||||
TestSubscriber sub(initial_tip->GetBlockHash());
|
auto sub = std::make_shared<TestSubscriber>(initial_tip->GetBlockHash());
|
||||||
RegisterValidationInterface(&sub);
|
RegisterSharedValidationInterface(sub);
|
||||||
|
|
||||||
// create a bunch of threads that repeatedly process a block generated above at random
|
// create a bunch of threads that repeatedly process a block generated above at random
|
||||||
// this will create parallelism and randomness inside validation - the ValidationInterface
|
// this will create parallelism and randomness inside validation - the ValidationInterface
|
||||||
|
@ -206,10 +206,10 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
|
||||||
}
|
}
|
||||||
SyncWithValidationInterfaceQueue();
|
SyncWithValidationInterfaceQueue();
|
||||||
|
|
||||||
UnregisterValidationInterface(&sub);
|
UnregisterSharedValidationInterface(sub);
|
||||||
|
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
BOOST_CHECK_EQUAL(sub.m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
|
BOOST_CHECK_EQUAL(sub->m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_SUITE(validationinterface_tests, TestingSetup)
|
BOOST_FIXTURE_TEST_SUITE(validationinterface_tests, TestingSetup)
|
||||||
|
|
||||||
/**
|
|
||||||
struct TestSubscriberNoop final : public CValidationInterface {
|
struct TestSubscriberNoop final : public CValidationInterface {
|
||||||
void BlockChecked(const CBlock&, const BlockValidationState&) override {}
|
void BlockChecked(const CBlock&, const BlockValidationState&) override {}
|
||||||
};
|
};
|
||||||
|
@ -34,9 +33,9 @@ BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
|
||||||
std::thread sub{[&] {
|
std::thread sub{[&] {
|
||||||
// keep going for about 1 sec, which is 250k iterations
|
// keep going for about 1 sec, which is 250k iterations
|
||||||
for (int i = 0; i < 250000; i++) {
|
for (int i = 0; i < 250000; i++) {
|
||||||
TestSubscriberNoop sub{};
|
auto sub = std::make_shared<TestSubscriberNoop>();
|
||||||
RegisterValidationInterface(&sub);
|
RegisterSharedValidationInterface(sub);
|
||||||
UnregisterValidationInterface(&sub);
|
UnregisterSharedValidationInterface(sub);
|
||||||
}
|
}
|
||||||
// tell the other thread we are done
|
// tell the other thread we are done
|
||||||
generate = false;
|
generate = false;
|
||||||
|
@ -46,7 +45,6 @@ BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
|
||||||
sub.join();
|
sub.join();
|
||||||
BOOST_CHECK(!generate);
|
BOOST_CHECK(!generate);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
class TestInterface : public CValidationInterface
|
class TestInterface : public CValidationInterface
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue