Sorry, I don’t understand.
0diff --git a/src/test/fuzz/process_message.cpp b/src/test/fuzz/process_message.cpp
1index 3048b09564..ffb7a78b5b 100644
2--- a/src/test/fuzz/process_message.cpp
3+++ b/src/test/fuzz/process_message.cpp
4@@ -28,6 +28,7 @@
5 #include <test/fuzz/FuzzedDataProvider.h>
6 #include <test/fuzz/fuzz.h>
7 #include <test/util/setup_common.h>
8+#include <test/util/mining.h>
9 #include <txdb.h>
10 #include <txmempool.h>
11 #include <util/memory.h>
12@@ -58,23 +59,6 @@ bool ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vR
13
14 namespace {
15
16-size_t GetNumberOfScheduledTasks(const CScheduler& scheduler)
17-{
18- std::chrono::system_clock::time_point first, last;
19- return scheduler.getQueueInfo(first, last);
20-}
21-
22-void SleepUntilSchedulerCompletion(const CScheduler& scheduler, const size_t consider_completed_at_size = 0)
23-{
24- for (int i = 0; i < 10; ++i) {
25- if (GetNumberOfScheduledTasks(scheduler) <= consider_completed_at_size) {
26- return;
27- }
28- UninterruptibleSleep(std::chrono::milliseconds{10});
29- }
30- assert(false);
31-}
32-
33 #ifdef MESSAGE_TYPE
34 #define TO_STRING_(s) #s
35 #define TO_STRING(s) TO_STRING_(s)
36@@ -83,94 +67,6 @@ const std::string LIMIT_TO_MESSAGE_TYPE{TO_STRING(MESSAGE_TYPE)};
37 const std::string LIMIT_TO_MESSAGE_TYPE;
38 #endif
39
40-void CreateAndProcessNextBlock(const CTxMemPool& mempool)
41-{
42- std::unique_ptr<CBlockTemplate> block_template = BlockAssembler(mempool, Params()).CreateNewBlock(CScript() << OP_TRUE);
43- CBlock& block = block_template->block;
44- {
45- LOCK(cs_main);
46- unsigned int extra_nonce;
47- IncrementExtraNonce(&block, ChainActive().Tip(), extra_nonce);
48- }
49- while (!CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus())) {
50- ++block.nNonce;
51- }
52- ProcessNewBlock(Params(), std::make_shared<const CBlock>(block), true, nullptr);
53-}
54-
55-template <typename T>
56-std::string ToString(const T t)
57-{
58- std::ostringstream oss;
59- oss.imbue(std::locale::classic());
60- oss << t;
61- return oss.str();
62-}
63-
64-class FuzzingSetup
65-{
66- boost::thread_group m_thread_group;
67- const fs::path m_path;
68- std::unique_ptr<PeerLogicValidation> m_peer_logic_validation;
69-
70-public:
71- CScheduler m_scheduler;
72- NodeContext m_node_context;
73- std::unique_ptr<CNode> m_dummy_p2p_node;
74-
75- FuzzingSetup(const std::string& fuzzer_name) : m_path{fs::temp_directory_path() / "fuzzers" / fuzzer_name / ToString(FastRandomContext().rand64())}
76- {
77- SelectParams(CBaseChainParams::REGTEST);
78- fs::remove_all(m_path);
79- fs::create_directories(m_path);
80- ::gArgs.ForceSetArg("-datadir", m_path.string());
81- InitLogging();
82- LogInstance().m_print_to_console = false;
83- LogInstance().StartLogging();
84- ::g_chainstate = MakeUnique<CChainState>();
85- ::pblocktree = MakeUnique<CBlockTreeDB>(1 << 20, true);
86- m_dummy_p2p_node = MakeUnique<CNode>(0, ServiceFlags(NODE_NETWORK | NODE_WITNESS | NODE_BLOOM), 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, false);
87- m_dummy_p2p_node->fSuccessfullyConnected = true;
88- m_dummy_p2p_node->nVersion = PROTOCOL_VERSION;
89- m_dummy_p2p_node->SetSendVersion(PROTOCOL_VERSION);
90- m_node_context.mempool = &::mempool;
91- m_node_context.mempool->setSanityCheck(1.0);
92- m_node_context.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
93- m_node_context.connman = MakeUnique<CConnman>(0x1337, 0x1337);
94- m_thread_group.create_thread(std::bind(&CScheduler::serviceQueue, &m_scheduler));
95- GetMainSignals().RegisterBackgroundSignalScheduler(m_scheduler);
96- ChainstateActive().InitCoinsDB(1 << 23, true, false);
97- ChainstateActive().InitCoinsCache();
98- if (!LoadGenesisBlock(Params())) {
99- throw std::runtime_error("LoadGenesisBlock(...) failed.");
100- }
101- BlockValidationState block_validation_state;
102- if (!ActivateBestChain(block_validation_state, Params())) {
103- throw std::runtime_error("ActivateBestChain(...) failed.");
104- }
105- for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
106- CreateAndProcessNextBlock(*m_node_context.mempool);
107- SleepUntilSchedulerCompletion(m_scheduler);
108- }
109- assert(ChainActive().Tip() != nullptr);
110- m_peer_logic_validation = MakeUnique<PeerLogicValidation>(m_node_context.connman.get(), nullptr, m_scheduler);
111- m_peer_logic_validation->InitializeNode(m_dummy_p2p_node.get());
112- assert(GetMainSignals().CallbacksPending() == 0);
113- SleepUntilSchedulerCompletion(m_scheduler, 1);
114- LogInstance().m_print_to_console = true;
115- }
116-
117- ~FuzzingSetup()
118- {
119- LogInstance().DisconnectTestLogger();
120- fs::remove_all(m_path);
121- m_thread_group.interrupt_all();
122- m_thread_group.join_all();
123- GetMainSignals().FlushBackgroundCallbacks();
124- GetMainSignals().UnregisterBackgroundSignalScheduler();
125- }
126-};
127-
128 const std::map<std::string, std::set<std::string>> EXPECTED_DESERIALIZATION_EXCEPTIONS = {
129 {"CDataStream::read(): end of data: iostream error", {"addr", "block", "blocktxn", "cmpctblock", "feefilter", "filteradd", "filterload", "getblocks", "getblocktxn", "getdata", "getheaders", "headers", "inv", "notfound", "ping", "sendcmpct", "tx"}},
130 {"index overflowed 16 bits: iostream error", {"getblocktxn"}},
131@@ -182,12 +78,20 @@ const std::map<std::string, std::set<std::string>> EXPECTED_DESERIALIZATION_EXCE
132 {"Unknown transaction optional data: iostream error", {"block", "blocktxn", "cmpctblock", "tx"}},
133 };
134
135-std::unique_ptr<FuzzingSetup> g_fuzzing_setup;
136+std::unique_ptr<RegTestingSetup> g_fuzzing_setup;
137 } // namespace
138
139 void initialize()
140 {
141- g_fuzzing_setup = MakeUnique<FuzzingSetup>("process_message-" + (LIMIT_TO_MESSAGE_TYPE.empty() ? "all" : LIMIT_TO_MESSAGE_TYPE));
142+ g_fuzzing_setup = MakeUnique<RegTestingSetup>();
143+
144+ for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
145+ MineBlock(g_fuzzing_setup->m_node, CScript() << OP_TRUE);
146+ }
147+ SyncWithValidationInterfaceQueue();
148+ assert(ChainActive().Tip() != nullptr);
149+ assert(GetMainSignals().CallbacksPending() == 0);
150+ LogInstance().m_print_to_console = true;
151 }
152
153 void test_one_input(const std::vector<uint8_t>& buffer)
154@@ -206,9 +110,13 @@ void test_one_input(const std::vector<uint8_t>& buffer)
155 return;
156 }
157 CDataStream random_bytes_data_stream{fuzzed_data_provider.ConsumeRemainingBytes<unsigned char>(), SER_NETWORK, PROTOCOL_VERSION};
158- CNode& p2p_node = *g_fuzzing_setup->m_dummy_p2p_node;
159+ CNode p2p_node{0, ServiceFlags(NODE_NETWORK | NODE_WITNESS | NODE_BLOOM), 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, false};
160+ p2p_node.fSuccessfullyConnected = true;
161+ p2p_node.nVersion = PROTOCOL_VERSION;
162+ p2p_node.SetSendVersion(PROTOCOL_VERSION);
163+ g_fuzzing_setup->m_node.peer_logic->InitializeNode(&p2p_node);
164 try {
165- (void)ProcessMessage(&p2p_node, random_message_type, random_bytes_data_stream, GetTimeMillis(), Params(), g_fuzzing_setup->m_node_context.connman.get(), g_fuzzing_setup->m_node_context.banman.get(), std::atomic<bool>{false});
166+ (void)ProcessMessage(&p2p_node, random_message_type, random_bytes_data_stream, GetTimeMillis(), Params(), g_fuzzing_setup->m_node.connman.get(), g_fuzzing_setup->m_node.banman.get(), std::atomic<bool>{false});
167 } catch (const std::ios_base::failure& e) {
168 const std::string exception_message{e.what()};
169 const auto p = EXPECTED_DESERIALIZATION_EXCEPTIONS.find(exception_message);
170@@ -219,18 +127,6 @@ void test_one_input(const std::vector<uint8_t>& buffer)
171 } catch (...) {
172 assert(false);
173 }
174- p2p_node.fDisconnect = false;
175- p2p_node.fPauseSend = false;
176- p2p_node.nSendSize = 0;
177- {
178- LOCK(p2p_node.cs_inventory);
179- p2p_node.vInventoryBlockToSend.clear();
180- }
181- p2p_node.vRecvGetData.clear();
182- {
183- LOCK(p2p_node.cs_vSend);
184- p2p_node.vSendMsg.clear();
185- }
186 assert(GetMainSignals().CallbacksPending() == 0);
187- SleepUntilSchedulerCompletion(g_fuzzing_setup->m_scheduler, 1);
188+ SyncWithValidationInterfaceQueue();
189 }
190diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp
191index 53eb9ff43b..7fdbbdfd1d 100644
192--- a/src/test/util/setup_common.cpp
193+++ b/src/test/util/setup_common.cpp
194@@ -7,6 +7,7 @@
195 #include <banman.h>
196 #include <chainparams.h>
197 #include <consensus/consensus.h>
198+#include <net_processing.h>
199 #include <consensus/params.h>
200 #include <consensus/validation.h>
201 #include <crypto/sha256.h>
202@@ -62,7 +63,7 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
203 }
204
205 BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
206- : m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / std::to_string(g_insecure_rand_ctx_temp_path.rand32())}
207+ : m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / g_insecure_rand_ctx_temp_path.rand256().ToString()}
208 {
209 fs::create_directories(m_path_root);
210 gArgs.ForceSetArg("-datadir", m_path_root.string());
211@@ -136,6 +137,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
212 m_node.mempool->setSanityCheck(1.0);
213 m_node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
214 m_node.connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
215+ m_node.peer_logic = MakeUnique<PeerLogicValidation>(m_node.connman.get(), m_node.banman.get(), *m_node.scheduler);
216 }
217
218 TestingSetup::~TestingSetup()