merge bitcoin#20332: Mock IBD in net_processing fuzzers

This commit is contained in:
Kittywhiskers Van Gogh 2020-11-06 15:03:51 +01:00 committed by pasta
parent d9f01c3c9f
commit 1ecd587183
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
6 changed files with 53 additions and 2 deletions

View File

@ -17,6 +17,7 @@ TEST_UTIL_H = \
test/util/str.h \ test/util/str.h \
test/util/transaction_utils.h \ test/util/transaction_utils.h \
test/util/wallet.h \ test/util/wallet.h \
test/util/validation.h \
test/util/xoroshiro128plusplus.h test/util/xoroshiro128plusplus.h
libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS) libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
@ -30,6 +31,7 @@ libtest_util_a_SOURCES = \
test/util/setup_common.cpp \ test/util/setup_common.cpp \
test/util/str.cpp \ test/util/str.cpp \
test/util/transaction_utils.cpp \ test/util/transaction_utils.cpp \
test/util/validation.cpp \
test/util/wallet.cpp \ test/util/wallet.cpp \
$(TEST_UTIL_H) $(TEST_UTIL_H)

View File

@ -17,6 +17,7 @@
#include <test/util/mining.h> #include <test/util/mining.h>
#include <test/util/net.h> #include <test/util/net.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <test/util/validation.h>
#include <validationinterface.h> #include <validationinterface.h>
#include <version.h> #include <version.h>
@ -74,10 +75,14 @@ void fuzz_target(FuzzBufferType buffer, const std::string& LIMIT_TO_MESSAGE_TYPE
{ {
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
ConnmanTestMsg& connman = *(ConnmanTestMsg*)g_setup->m_node.connman.get(); ConnmanTestMsg& connman = *(ConnmanTestMsg*)g_setup->m_node.connman.get();
TestChainState& chainstate = *(TestChainState*)&g_setup->m_node.chainman->ActiveChainstate();
chainstate.ResetIbd();
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()}; const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) { if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
return; return;
} }
const bool jump_out_of_ibd{fuzzed_data_provider.ConsumeBool()};
if (jump_out_of_ibd) chainstate.JumpOutOfIbd();
CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider).release(); CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider).release();
const bool successfully_connected{fuzzed_data_provider.ConsumeBool()}; const bool successfully_connected{fuzzed_data_provider.ConsumeBool()};

View File

@ -12,6 +12,7 @@
#include <test/util/mining.h> #include <test/util/mining.h>
#include <test/util/net.h> #include <test/util/net.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <test/util/validation.h>
#include <validation.h> #include <validation.h>
#include <validationinterface.h> #include <validationinterface.h>
@ -34,7 +35,10 @@ FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
ConnmanTestMsg& connman = *(ConnmanTestMsg*)g_setup->m_node.connman.get(); ConnmanTestMsg& connman = *(ConnmanTestMsg*)g_setup->m_node.connman.get();
TestChainState& chainstate = *(TestChainState*)&g_setup->m_node.chainman->ActiveChainstate();
chainstate.ResetIbd();
std::vector<CNode*> peers; std::vector<CNode*> peers;
bool jump_out_of_ibd{false};
const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3); const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
for (int i = 0; i < num_peers_to_add; ++i) { for (int i = 0; i < num_peers_to_add; ++i) {
@ -51,6 +55,8 @@ FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
} }
while (fuzzed_data_provider.ConsumeBool()) { while (fuzzed_data_provider.ConsumeBool()) {
if (!jump_out_of_ibd) jump_out_of_ibd = fuzzed_data_provider.ConsumeBool();
if (jump_out_of_ibd && chainstate.IsInitialBlockDownload()) chainstate.JumpOutOfIbd();
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()}; const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
CSerializedNetMsg net_msg; CSerializedNetMsg net_msg;

View File

@ -0,0 +1,22 @@
// 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 <test/util/validation.h>
#include <util/check.h>
#include <util/time.h>
#include <validation.h>
void TestChainState::ResetIbd()
{
m_cached_finished_ibd = false;
assert(IsInitialBlockDownload());
}
void TestChainState::JumpOutOfIbd()
{
Assert(IsInitialBlockDownload());
m_cached_finished_ibd = true;
Assert(!IsInitialBlockDownload());
}

View File

@ -0,0 +1,17 @@
// 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.
#ifndef BITCOIN_TEST_UTIL_VALIDATION_H
#define BITCOIN_TEST_UTIL_VALIDATION_H
#include <validation.h>
struct TestChainState : public CChainState {
/** Reset the ibd cache to its initial state */
void ResetIbd();
/** Toggle IsInitialBlockDownload from true to false */
void JumpOutOfIbd();
};
#endif // BITCOIN_TEST_UTIL_VALIDATION_H

View File

@ -625,8 +625,7 @@ enum class CoinsCacheSizeState
*/ */
class CChainState class CChainState
{ {
private: protected:
/** /**
* Every received block is assigned a unique and increasing identifier, so we * Every received block is assigned a unique and increasing identifier, so we
* know which one to give priority in case of a fork. * know which one to give priority in case of a fork.