Bitcoin ABC 0.33.5
P2P Digital Currency
p2p_messaging_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2019 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <chainparams.h>
6#include <common/system.h>
7#include <net_processing.h>
9#include <protocol.h>
10#include <seeder/bitcoin.h>
11#include <seeder/db.h>
12#include <seeder/test/util.h>
13#include <serialize.h>
14#include <streams.h>
15#include <util/chaintype.h>
16
17#include <boost/test/unit_test.hpp>
18
19#include <cstdint>
20#include <memory>
21#include <ostream>
22#include <string>
23#include <vector>
24
25std::ostream &operator<<(std::ostream &os, const PeerMessagingState &state) {
26 os << to_integral(state);
27 return os;
28}
29
30namespace {
31class CSeederNodeTest : public CSeederNode {
32public:
33 CSeederNodeTest(const CService &service, std::vector<CAddress> *vAddrIn)
34 : CSeederNode(service, vAddrIn) {}
35
36 void TestProcessMessage(const std::string &strCommand, DataStream &message,
37 PeerMessagingState expectedState) {
38 PeerMessagingState ret = ProcessMessage(strCommand, message);
39 BOOST_CHECK_EQUAL(ret, expectedState);
40 }
41
42 DataStream getSendBuffer() { return vSend; }
43
44 void setStartingHeight(int starting_height) {
45 nStartingHeight = starting_height;
46 };
47};
48} // namespace
49
50static const uint16_t SERVICE_PORT = 18444;
51
54 SelectParams(chain_type);
55 CNetAddr ip;
56 ip.SetInternal("bitcoin.test");
57 CService service = {ip, SERVICE_PORT};
58 vAddr.emplace_back(service, ServiceFlags());
59 testNode = std::make_unique<CSeederNodeTest>(service, &vAddr);
60 }
61
62 std::vector<CAddress> vAddr;
63 std::unique_ptr<CSeederNodeTest> testNode;
64};
65
68};
69
70BOOST_FIXTURE_TEST_SUITE(p2p_messaging_tests, SeederTestingSetup)
71
72static const int SEEDER_INIT_VERSION = 0;
73
74BOOST_AUTO_TEST_CASE(process_version_msg) {
75 DataStream versionMessage{};
76 uint64_t serviceflags = ServiceFlags(NODE_NETWORK);
77 CService addr_to = vAddr[0];
78 uint64_t addr_to_services = vAddr[0].nServices;
79 CService addr_from;
80 uint64_t nonce = 0;
81 std::string user_agent = "/Bitcoin ABC:0.0.0(seeder)/";
82
83 // Don't include the time in CAddress serialization. See D14753.
84 versionMessage << INIT_PROTO_VERSION << serviceflags << GetTime()
85 << addr_to_services << WithParams(CNetAddr::V1, addr_to)
86 << serviceflags << WithParams(CNetAddr::V1, addr_from)
87 << nonce << user_agent << GetRequireHeight();
88
89 // Verify the version is set as the initial value
90 BOOST_CHECK_EQUAL(testNode->CSeederNode::GetClientVersion(),
92 testNode->TestProcessMessage(NetMsgType::VERSION, versionMessage,
94}
95
97 DataStream verackMessage{};
98 testNode->TestProcessMessage(NetMsgType::VERACK, verackMessage,
100
101 // Seeder should respond with an ADDR message
102 CMessageHeader header;
103 DataStream sendBuffer = testNode->getSendBuffer();
104 sendBuffer >> header;
107
108 // Next message should be GETHEADERS
109 sendBuffer >> header;
112
113 CBlockLocator locator;
114 uint256 hashStop;
115 sendBuffer >> locator >> hashStop;
116 std::vector<BlockHash> expectedLocator = {
117 Params().Checkpoints().mapCheckpoints.rbegin()->second};
118 BOOST_CHECK(locator.vHave == expectedLocator);
119 BOOST_CHECK(hashStop == uint256());
120}
121
122static DataStream CreateAddrMessage(std::vector<CAddress> sendAddrs,
123 uint32_t nVersion = INIT_PROTO_VERSION) {
124 DataStream payload{};
125 payload << WithParams(CAddress::V1_NETWORK, sendAddrs);
126 return payload;
127}
128
130 // First, must send headers to satisfy the criteria that both ADDR/ADDRV2
131 // *and* HEADERS must arrive before TestNode can advance to the Finished
132 // state
133 BlockHash recentCheckpoint =
134 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
135 int recentCheckpointHeight =
136 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
137 auto header = CBlockHeader{};
138 header.hashPrevBlock = recentCheckpoint;
139 testNode->setStartingHeight(recentCheckpointHeight + 1);
140 DataStream headersMsg{};
141 WriteCompactSize(headersMsg, 1);
142 headersMsg << header;
143 // sanity check: node is expecting headers
144 BOOST_CHECK(!testNode->IsCheckpointVerified());
145 testNode->TestProcessMessage(NetMsgType::HEADERS, headersMsg,
147 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
148 // node got the checkpointed header; it can advance to Finished after
149 // addr message
150 BOOST_CHECK(testNode->IsCheckpointVerified());
151
152 // vAddrs starts with 1 entry.
153 std::vector<CAddress> sendAddrs(ADDR_SOFT_CAP - 1, vAddr[0]);
154
155 // Happy path
156 // addrs are added normally to vAddr until ADDR_SOFT_CAP is reached.
157 // Add addrs up to the soft cap.
158 DataStream addrMessage = CreateAddrMessage(sendAddrs);
159 BOOST_CHECK_EQUAL(1, vAddr.size());
160 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
162 BOOST_CHECK_EQUAL(ADDR_SOFT_CAP, vAddr.size());
163
164 // ADDR_SOFT_CAP is exceeded
165 sendAddrs.resize(1);
166 addrMessage = CreateAddrMessage(sendAddrs);
167 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
169 BOOST_CHECK_EQUAL(ADDR_SOFT_CAP + 1, vAddr.size());
170
171 // Test the seeder's behavior after ADDR_SOFT_CAP addrs
172 // Only one addr per ADDR message will be added, the rest are ignored
173 size_t expectedSize = vAddr.size() + 1;
174 for (size_t i = 1; i < 10; i++) {
175 sendAddrs.resize(i, sendAddrs[0]);
176 addrMessage = CreateAddrMessage(sendAddrs);
177 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
179 BOOST_CHECK_EQUAL(expectedSize, vAddr.size());
180 ++expectedSize;
181 }
182}
183
184BOOST_AUTO_TEST_CASE(ban_too_many_headers) {
185 // Process the maximum number of headers
186 auto header = CBlockHeader{};
187 DataStream maxHeaderMessages{};
188 WriteCompactSize(maxHeaderMessages, MAX_HEADERS_RESULTS);
189 for (size_t i = 0; i < MAX_HEADERS_RESULTS; i++) {
190 maxHeaderMessages << header;
191 WriteCompactSize(maxHeaderMessages, 0);
192 }
193 testNode->TestProcessMessage(NetMsgType::HEADERS, maxHeaderMessages,
195 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
196
197 // Process one too many headers
198 DataStream tooManyHeadersMessage{};
199 WriteCompactSize(tooManyHeadersMessage, MAX_HEADERS_RESULTS + 1);
200 // The message processing will abort when seeing the excessive number of
201 // headers from the compact size. No need to actually pack any header data.
202 testNode->TestProcessMessage(NetMsgType::HEADERS, tooManyHeadersMessage,
204 BOOST_CHECK(testNode->GetBan() > 0);
205}
206
207BOOST_AUTO_TEST_CASE(empty_headers) {
208 // Check that an empty headers message does not cause issues
209 DataStream zeroHeadersMessage{};
210 WriteCompactSize(zeroHeadersMessage, 0);
211 testNode->TestProcessMessage(NetMsgType::HEADERS, zeroHeadersMessage,
213 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
214}
215
217 BlockHash recentCheckpoint =
218 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
219 int recentCheckpointHeight =
220 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
221
222 // Process a HEADERS message with a first header that immediately follows
223 // our most recent checkpoint, check that it is accepted.
224 auto header = CBlockHeader{};
225 header.hashPrevBlock = recentCheckpoint;
226 testNode->setStartingHeight(recentCheckpointHeight + 1);
227 DataStream headersOnCorrectChain{};
228 // The following .reserve() call is a workaround for a spurious
229 // [-Werror=stringop-overflow=] warning in gcc <= 12.2.
230 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100366#c20
231 headersOnCorrectChain.reserve(4);
232 WriteCompactSize(headersOnCorrectChain, 1);
233 headersOnCorrectChain << header;
234 testNode->TestProcessMessage(NetMsgType::HEADERS, headersOnCorrectChain,
236 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
237 BOOST_CHECK(testNode->IsCheckpointVerified());
238}
239
241 BlockHash recentCheckpoint =
242 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
243 int recentCheckpointHeight =
244 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
245 auto header = CBlockHeader{};
246
247 // We just ignore HEADERS messages sent by nodes with a chaintip before our
248 // most recent checkpoint.
249 testNode->setStartingHeight(recentCheckpointHeight - 1);
250 DataStream shortHeaderChain{};
251 WriteCompactSize(shortHeaderChain, 1);
252 shortHeaderChain << header;
253 testNode->TestProcessMessage(NetMsgType::HEADERS, shortHeaderChain,
255 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
256 BOOST_CHECK(!testNode->IsCheckpointVerified());
257
258 // Process a HEADERS message with a first header that does not follow
259 // our most recent checkpoint, check that the node is banned.
260 BOOST_CHECK(header.hashPrevBlock != recentCheckpoint);
261 testNode->setStartingHeight(recentCheckpointHeight + 1);
262 DataStream headersOnWrongChain{};
263 WriteCompactSize(headersOnWrongChain, 1);
264 headersOnWrongChain << header;
265 testNode->TestProcessMessage(NetMsgType::HEADERS, headersOnWrongChain,
267 BOOST_CHECK(testNode->GetBan() > 0);
268 BOOST_CHECK(!testNode->IsCheckpointVerified());
269}
270
271BOOST_AUTO_TEST_SUITE_END()
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given BIP70 chain name.
Definition: chainparams.cpp:50
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
ChainType
Definition: chaintype.h:11
static constexpr SerParams V1_NETWORK
Definition: protocol.h:495
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
BlockHash hashPrevBlock
Definition: block.h:27
const CCheckpointData & Checkpoints() const
Definition: chainparams.h:145
Message header.
Definition: protocol.h:34
bool IsMessageTypeValid() const
Definition: protocol.cpp:125
std::string GetMessageType() const
Definition: protocol.cpp:119
Network address.
Definition: netaddress.h:114
static constexpr SerParams V1
Definition: netaddress.h:255
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:188
DataStream vSend
Definition: bitcoin.h:42
PeerMessagingState ProcessMessage(std::string strCommand, DataStream &recv)
Definition: bitcoin.cpp:41
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:573
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:118
void reserve(size_type n)
Definition: streams.h:156
256-bit opaque blob.
Definition: uint256.h:129
static node::NodeContext testNode
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:29
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:31
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:20
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:18
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:27
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
static const unsigned int MAX_HEADERS_RESULTS
Number of headers sent in one getheaders result.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static const uint16_t SERVICE_PORT
std::ostream & operator<<(std::ostream &os, const PeerMessagingState &state)
static DataStream CreateAddrMessage(std::vector< CAddress > sendAddrs, uint32_t nVersion=INIT_PROTO_VERSION)
BOOST_FIXTURE_TEST_CASE(process_verack_msg, MainNetSeederTestingSetup)
BOOST_AUTO_TEST_CASE(process_version_msg)
static const int SEEDER_INIT_VERSION
ServiceFlags
nServices flags.
Definition: protocol.h:335
@ NODE_NETWORK
Definition: protocol.h:342
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
PeerMessagingState
Definition: bitcoin.h:26
static const unsigned int ADDR_SOFT_CAP
Definition: bitcoin.h:24
static int GetRequireHeight()
Definition: db.h:27
constexpr std::underlying_type< E >::type to_integral(E e)
Definition: util.h:11
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition: serialize.h:1258
static auto WithParams(const Params &params, T &&t)
Return a wrapper around t that (de)serializes it with specified parameter params.
Definition: serialize.h:1329
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:108
std::vector< BlockHash > vHave
Definition: block.h:120
MapCheckpoints mapCheckpoints
Definition: chainparams.h:36
std::vector< CAddress > vAddr
std::unique_ptr< CSeederNodeTest > testNode
SeederTestingSetup(const ChainType chain_type=ChainType::REGTEST)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:80