Bitcoin ABC 0.32.6
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 const CMessageHeader::MessageMagic netMagic = Params().NetMagic();
103 CMessageHeader header(netMagic);
104 DataStream sendBuffer = testNode->getSendBuffer();
105 sendBuffer >> header;
106 BOOST_CHECK(header.IsValidWithoutConfig(netMagic));
108
109 // Next message should be GETHEADERS
110 sendBuffer >> header;
111 BOOST_CHECK(header.IsValidWithoutConfig(netMagic));
113
114 CBlockLocator locator;
115 uint256 hashStop;
116 sendBuffer >> locator >> hashStop;
117 std::vector<BlockHash> expectedLocator = {
118 Params().Checkpoints().mapCheckpoints.rbegin()->second};
119 BOOST_CHECK(locator.vHave == expectedLocator);
120 BOOST_CHECK(hashStop == uint256());
121}
122
123static DataStream CreateAddrMessage(std::vector<CAddress> sendAddrs,
124 uint32_t nVersion = INIT_PROTO_VERSION) {
125 DataStream payload{};
126 payload << WithParams(CAddress::V1_NETWORK, sendAddrs);
127 return payload;
128}
129
131 // First, must send headers to satisfy the criteria that both ADDR/ADDRV2
132 // *and* HEADERS must arrive before TestNode can advance to the Finished
133 // state
134 BlockHash recentCheckpoint =
135 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
136 int recentCheckpointHeight =
137 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
138 auto header = CBlockHeader{};
139 header.hashPrevBlock = recentCheckpoint;
140 testNode->setStartingHeight(recentCheckpointHeight + 1);
141 DataStream headersMsg{};
142 WriteCompactSize(headersMsg, 1);
143 headersMsg << header;
144 // sanity check: node is expecting headers
145 BOOST_CHECK(!testNode->IsCheckpointVerified());
146 testNode->TestProcessMessage(NetMsgType::HEADERS, headersMsg,
148 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
149 // node got the checkpointed header; it can advance to Finished after
150 // addr message
151 BOOST_CHECK(testNode->IsCheckpointVerified());
152
153 // vAddrs starts with 1 entry.
154 std::vector<CAddress> sendAddrs(ADDR_SOFT_CAP - 1, vAddr[0]);
155
156 // Happy path
157 // addrs are added normally to vAddr until ADDR_SOFT_CAP is reached.
158 // Add addrs up to the soft cap.
159 DataStream addrMessage = CreateAddrMessage(sendAddrs);
160 BOOST_CHECK_EQUAL(1, vAddr.size());
161 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
163 BOOST_CHECK_EQUAL(ADDR_SOFT_CAP, vAddr.size());
164
165 // ADDR_SOFT_CAP is exceeded
166 sendAddrs.resize(1);
167 addrMessage = CreateAddrMessage(sendAddrs);
168 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
170 BOOST_CHECK_EQUAL(ADDR_SOFT_CAP + 1, vAddr.size());
171
172 // Test the seeder's behavior after ADDR_SOFT_CAP addrs
173 // Only one addr per ADDR message will be added, the rest are ignored
174 size_t expectedSize = vAddr.size() + 1;
175 for (size_t i = 1; i < 10; i++) {
176 sendAddrs.resize(i, sendAddrs[0]);
177 addrMessage = CreateAddrMessage(sendAddrs);
178 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
180 BOOST_CHECK_EQUAL(expectedSize, vAddr.size());
181 ++expectedSize;
182 }
183}
184
185BOOST_AUTO_TEST_CASE(ban_too_many_headers) {
186 // Process the maximum number of headers
187 auto header = CBlockHeader{};
188 DataStream maxHeaderMessages{};
189 WriteCompactSize(maxHeaderMessages, MAX_HEADERS_RESULTS);
190 for (size_t i = 0; i < MAX_HEADERS_RESULTS; i++) {
191 maxHeaderMessages << header;
192 WriteCompactSize(maxHeaderMessages, 0);
193 }
194 testNode->TestProcessMessage(NetMsgType::HEADERS, maxHeaderMessages,
196 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
197
198 // Process one too many headers
199 DataStream tooManyHeadersMessage{};
200 WriteCompactSize(tooManyHeadersMessage, MAX_HEADERS_RESULTS + 1);
201 // The message processing will abort when seeing the excessive number of
202 // headers from the compact size. No need to actually pack any header data.
203 testNode->TestProcessMessage(NetMsgType::HEADERS, tooManyHeadersMessage,
205 BOOST_CHECK(testNode->GetBan() > 0);
206}
207
208BOOST_AUTO_TEST_CASE(empty_headers) {
209 // Check that an empty headers message does not cause issues
210 DataStream zeroHeadersMessage{};
211 WriteCompactSize(zeroHeadersMessage, 0);
212 testNode->TestProcessMessage(NetMsgType::HEADERS, zeroHeadersMessage,
214 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
215}
216
218 BlockHash recentCheckpoint =
219 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
220 int recentCheckpointHeight =
221 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
222
223 // Process a HEADERS message with a first header that immediately follows
224 // our most recent checkpoint, check that it is accepted.
225 auto header = CBlockHeader{};
226 header.hashPrevBlock = recentCheckpoint;
227 testNode->setStartingHeight(recentCheckpointHeight + 1);
228 DataStream headersOnCorrectChain{};
229 // The following .reserve() call is a workaround for a spurious
230 // [-Werror=stringop-overflow=] warning in gcc <= 12.2.
231 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100366#c20
232 headersOnCorrectChain.reserve(4);
233 WriteCompactSize(headersOnCorrectChain, 1);
234 headersOnCorrectChain << header;
235 testNode->TestProcessMessage(NetMsgType::HEADERS, headersOnCorrectChain,
237 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
238 BOOST_CHECK(testNode->IsCheckpointVerified());
239}
240
242 BlockHash recentCheckpoint =
243 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
244 int recentCheckpointHeight =
245 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
246 auto header = CBlockHeader{};
247
248 // We just ignore HEADERS messages sent by nodes with a chaintip before our
249 // most recent checkpoint.
250 testNode->setStartingHeight(recentCheckpointHeight - 1);
251 DataStream shortHeaderChain{};
252 WriteCompactSize(shortHeaderChain, 1);
253 shortHeaderChain << header;
254 testNode->TestProcessMessage(NetMsgType::HEADERS, shortHeaderChain,
256 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
257 BOOST_CHECK(!testNode->IsCheckpointVerified());
258
259 // Process a HEADERS message with a first header that does not follow
260 // our most recent checkpoint, check that the node is banned.
261 BOOST_CHECK(header.hashPrevBlock != recentCheckpoint);
262 testNode->setStartingHeight(recentCheckpointHeight + 1);
263 DataStream headersOnWrongChain{};
264 WriteCompactSize(headersOnWrongChain, 1);
265 headersOnWrongChain << header;
266 testNode->TestProcessMessage(NetMsgType::HEADERS, headersOnWrongChain,
268 BOOST_CHECK(testNode->GetBan() > 0);
269 BOOST_CHECK(!testNode->IsCheckpointVerified());
270}
271
272BOOST_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:496
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 CMessageHeader::MessageMagic & NetMagic() const
Definition: chainparams.h:100
const CCheckpointData & Checkpoints() const
Definition: chainparams.h:145
Message header.
Definition: protocol.h:34
bool IsValidWithoutConfig(const MessageMagic &magic) const
This is a transition method in order to stay compatible with older code that do not use the config.
Definition: protocol.cpp:180
std::string GetMessageType() const
Definition: protocol.cpp:120
std::array< uint8_t, MESSAGE_START_SIZE > MessageMagic
Definition: protocol.h:47
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:185
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:336
@ NODE_NETWORK
Definition: protocol.h:343
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:105