Bitcoin Core 29.99.0
P2P Digital Currency
process_messages.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-present 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
6#include <net.h>
7#include <net_processing.h>
8#include <node/warnings.h>
9#include <protocol.h>
10#include <script/script.h>
11#include <sync.h>
13#include <test/fuzz/fuzz.h>
14#include <test/fuzz/util.h>
15#include <test/fuzz/util/net.h>
16#include <test/util/mining.h>
17#include <test/util/net.h>
20#include <util/time.h>
21#include <validationinterface.h>
22
23#include <ios>
24#include <string>
25#include <utility>
26#include <vector>
27
28namespace {
29TestingSetup* g_setup;
30
31void ResetChainman(TestingSetup& setup)
32{
33 SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
34 setup.m_node.chainman.reset();
35 setup.m_make_chainman();
37 for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
38 MineBlock(setup.m_node, {});
39 }
40 setup.m_node.validation_signals->SyncWithValidationInterfaceQueue();
41}
42} // namespace
43
45{
46 static const auto testing_setup{
47 MakeNoLogFileContext<TestingSetup>(
48 /*chain_type=*/ChainType::REGTEST,
49 {}),
50 };
51 g_setup = testing_setup.get();
52 ResetChainman(*g_setup);
53}
54
56{
58 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
59
60 auto& connman = static_cast<ConnmanTestMsg&>(*g_setup->m_node.connman);
61 connman.ResetAddrCache();
62 connman.ResetMaxOutboundCycle();
63 auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
64 const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
65 SetMockTime(1610000000); // any time to successfully reset ibd
66 chainman.ResetIbd();
67 chainman.DisableNextWrite();
68
69 node::Warnings warnings{};
70 NetGroupManager netgroupman{{}};
71 AddrMan addrman{netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0};
72 auto peerman = PeerManager::make(connman, addrman,
73 /*banman=*/nullptr, chainman,
74 *g_setup->m_node.mempool, warnings,
76 .reconcile_txs = true,
77 .deterministic_rng = true,
78 });
79 connman.SetMsgProc(peerman.get());
80
82
83 std::vector<CNode*> peers;
84 const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
85 for (int i = 0; i < num_peers_to_add; ++i) {
86 peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
87 CNode& p2p_node = *peers.back();
88
89 FillNode(fuzzed_data_provider, connman, p2p_node);
90
91 connman.AddTestNode(p2p_node);
92 }
93
94 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
95 {
96 const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
97
98 const auto mock_time = ConsumeTime(fuzzed_data_provider);
99 SetMockTime(mock_time);
100
101 CSerializedNetMsg net_msg;
102 net_msg.m_type = random_message_type;
103 net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
104
105 CNode& random_node = *PickValue(fuzzed_data_provider, peers);
106
107 connman.FlushSendBuffer(random_node);
108 (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
109
110 bool more_work{true};
111 while (more_work) { // Ensure that every message is eventually processed in some way or another
112 random_node.fPauseSend = false;
113
114 try {
115 more_work = connman.ProcessMessagesOnce(random_node);
116 } catch (const std::ios_base::failure&) {
117 }
118 g_setup->m_node.peerman->SendMessages(&random_node);
119 }
120 }
121 g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
122 g_setup->m_node.connman->StopNodes();
123 if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
124 // Reuse the global chainman, but reset it when it is dirty
125 ResetChainman(*g_setup);
126 }
127}
Stochastic address manager.
Definition: addrman.h:89
static constexpr size_t MESSAGE_TYPE_SIZE
Definition: protocol.h:31
Information about a peer.
Definition: net.h:675
std::atomic_bool fPauseSend
Definition: net.h:739
std::string ConsumeBytesAsString(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Definition: net.h:1013
Netgroup manager.
Definition: netgroup.h:16
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
Manages warning messages within a node.
Definition: warnings.h:40
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).
Definition: net.h:64
FUZZ_TARGET(process_messages,.init=initialize_process_messages)
void initialize_process_messages()
node::NodeContext m_node
Definition: setup_common.h:66
std::string m_type
Definition: net.h:132
std::vector< unsigned char > data
Definition: net.h:131
std::function< void()> m_make_chainman
Definition: setup_common.h:110
void LoadVerifyActivateChainstate()
void ResetAddrCache()
Definition: net.cpp:74
Testing setup that configures a complete environment.
Definition: setup_common.h:121
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition: context.h:88
std::unique_ptr< CConnman > connman
Definition: context.h:67
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:68
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
std::unique_ptr< PeerManager > peerman
Definition: context.h:71
#define LOCK(cs)
Definition: sync.h:259
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:290
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
Definition: net.cpp:429
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:267
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:47
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
COutPoint MineBlock(const NodeContext &node, const node::BlockAssembler::Options &assembler_options)
Returns the generated coin.
Definition: mining.cpp:70
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40