Bitcoin ABC 0.32.4
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>
8#include <protocol.h>
9#include <seeder/bitcoin.h>
10#include <seeder/db.h>
11#include <seeder/test/util.h>
12#include <serialize.h>
13#include <streams.h>
14#include <util/chaintype.h>
15#include <version.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, CDataStream &message,
37 PeerMessagingState expectedState) {
38 PeerMessagingState ret = ProcessMessage(strCommand, message);
39 BOOST_CHECK_EQUAL(ret, expectedState);
40 }
41
42 CDataStream 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) {
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 << addr_to << serviceflags << addr_from
86 << nonce << user_agent << GetRequireHeight();
87
88 // Verify the version is set as the initial value
89 BOOST_CHECK_EQUAL(testNode->CSeederNode::GetClientVersion(),
91 testNode->TestProcessMessage(NetMsgType::VERSION, versionMessage,
93 // Verify the version has been updated
94 BOOST_CHECK_EQUAL(testNode->CSeederNode::GetClientVersion(),
95 versionMessage.GetVersion());
96}
97
99 CDataStream verackMessage(SER_NETWORK, 0);
100 verackMessage.SetVersion(INIT_PROTO_VERSION);
101 testNode->TestProcessMessage(NetMsgType::VERACK, verackMessage,
103
104 // Seeder should respond with an ADDR message
105 const CMessageHeader::MessageMagic netMagic = Params().NetMagic();
106 CMessageHeader header(netMagic);
107 CDataStream sendBuffer = testNode->getSendBuffer();
108 sendBuffer >> header;
109 BOOST_CHECK(header.IsValidWithoutConfig(netMagic));
111
112 // Next message should be GETHEADERS
113 sendBuffer >> header;
114 BOOST_CHECK(header.IsValidWithoutConfig(netMagic));
116
117 CBlockLocator locator;
118 uint256 hashStop;
119 sendBuffer >> locator >> hashStop;
120 std::vector<BlockHash> expectedLocator = {
121 Params().Checkpoints().mapCheckpoints.rbegin()->second};
122 BOOST_CHECK(locator.vHave == expectedLocator);
123 BOOST_CHECK(hashStop == uint256());
124}
125
126static CDataStream CreateAddrMessage(std::vector<CAddress> sendAddrs,
127 uint32_t nVersion = INIT_PROTO_VERSION) {
128 CDataStream payload(SER_NETWORK, 0);
129 payload.SetVersion(nVersion);
130 payload << sendAddrs;
131 return payload;
132}
133
135 // First, must send headers to satisfy the criteria that both ADDR/ADDRV2
136 // *and* HEADERS must arrive before TestNode can advance to the Finished
137 // state
138 BlockHash recentCheckpoint =
139 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
140 int recentCheckpointHeight =
141 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
142 auto header = CBlockHeader{};
143 header.hashPrevBlock = recentCheckpoint;
144 testNode->setStartingHeight(recentCheckpointHeight + 1);
145 CDataStream headersMsg(SER_NETWORK, 0);
146 headersMsg.SetVersion(INIT_PROTO_VERSION);
147 WriteCompactSize(headersMsg, 1);
148 headersMsg << header;
149 // sanity check: node is expecting headers
150 BOOST_CHECK(!testNode->IsCheckpointVerified());
151 testNode->TestProcessMessage(NetMsgType::HEADERS, headersMsg,
153 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
154 // node got the checkpointed header; it can advance to Finished after
155 // addr message
156 BOOST_CHECK(testNode->IsCheckpointVerified());
157
158 // vAddrs starts with 1 entry.
159 std::vector<CAddress> sendAddrs(ADDR_SOFT_CAP - 1, vAddr[0]);
160
161 // Happy path
162 // addrs are added normally to vAddr until ADDR_SOFT_CAP is reached.
163 // Add addrs up to the soft cap.
164 CDataStream addrMessage = CreateAddrMessage(sendAddrs);
165 BOOST_CHECK_EQUAL(1, vAddr.size());
166 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
168 BOOST_CHECK_EQUAL(ADDR_SOFT_CAP, vAddr.size());
169
170 // ADDR_SOFT_CAP is exceeded
171 sendAddrs.resize(1);
172 addrMessage = CreateAddrMessage(sendAddrs);
173 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
175 BOOST_CHECK_EQUAL(ADDR_SOFT_CAP + 1, vAddr.size());
176
177 // Test the seeder's behavior after ADDR_SOFT_CAP addrs
178 // Only one addr per ADDR message will be added, the rest are ignored
179 size_t expectedSize = vAddr.size() + 1;
180 for (size_t i = 1; i < 10; i++) {
181 sendAddrs.resize(i, sendAddrs[0]);
182 addrMessage = CreateAddrMessage(sendAddrs);
183 testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
185 BOOST_CHECK_EQUAL(expectedSize, vAddr.size());
186 ++expectedSize;
187 }
188}
189
190BOOST_AUTO_TEST_CASE(ban_too_many_headers) {
191 // Process the maximum number of headers
192 auto header = CBlockHeader{};
193 CDataStream maxHeaderMessages(SER_NETWORK, 0);
194 maxHeaderMessages.SetVersion(INIT_PROTO_VERSION);
195 WriteCompactSize(maxHeaderMessages, MAX_HEADERS_RESULTS);
196 for (size_t i = 0; i < MAX_HEADERS_RESULTS; i++) {
197 maxHeaderMessages << header;
198 WriteCompactSize(maxHeaderMessages, 0);
199 }
200 testNode->TestProcessMessage(NetMsgType::HEADERS, maxHeaderMessages,
202 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
203
204 // Process one too many headers
205 CDataStream tooManyHeadersMessage(SER_NETWORK, 0);
206 tooManyHeadersMessage.SetVersion(INIT_PROTO_VERSION);
207 WriteCompactSize(tooManyHeadersMessage, MAX_HEADERS_RESULTS + 1);
208 // The message processing will abort when seeing the excessive number of
209 // headers from the compact size. No need to actually pack any header data.
210 testNode->TestProcessMessage(NetMsgType::HEADERS, tooManyHeadersMessage,
212 BOOST_CHECK(testNode->GetBan() > 0);
213}
214
215BOOST_AUTO_TEST_CASE(empty_headers) {
216 // Check that an empty headers message does not cause issues
217 CDataStream zeroHeadersMessage(SER_NETWORK, 0);
218 zeroHeadersMessage.SetVersion(INIT_PROTO_VERSION);
219 WriteCompactSize(zeroHeadersMessage, 0);
220 testNode->TestProcessMessage(NetMsgType::HEADERS, zeroHeadersMessage,
222 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
223}
224
226 BlockHash recentCheckpoint =
227 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
228 int recentCheckpointHeight =
229 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
230
231 // Process a HEADERS message with a first header that immediately follows
232 // our most recent checkpoint, check that it is accepted.
233 auto header = CBlockHeader{};
234 header.hashPrevBlock = recentCheckpoint;
235 testNode->setStartingHeight(recentCheckpointHeight + 1);
236 CDataStream headersOnCorrectChain(SER_NETWORK, 0);
237 headersOnCorrectChain.SetVersion(INIT_PROTO_VERSION);
238 WriteCompactSize(headersOnCorrectChain, 1);
239 headersOnCorrectChain << header;
240 testNode->TestProcessMessage(NetMsgType::HEADERS, headersOnCorrectChain,
242 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
243 BOOST_CHECK(testNode->IsCheckpointVerified());
244}
245
247 BlockHash recentCheckpoint =
248 ::Params().Checkpoints().mapCheckpoints.rbegin()->second;
249 int recentCheckpointHeight =
250 ::Params().Checkpoints().mapCheckpoints.rbegin()->first;
251 auto header = CBlockHeader{};
252
253 // We just ignore HEADERS messages sent by nodes with a chaintip before our
254 // most recent checkpoint.
255 testNode->setStartingHeight(recentCheckpointHeight - 1);
256 CDataStream shortHeaderChain(SER_NETWORK, 0);
257 shortHeaderChain.SetVersion(INIT_PROTO_VERSION);
258 WriteCompactSize(shortHeaderChain, 1);
259 shortHeaderChain << header;
260 testNode->TestProcessMessage(NetMsgType::HEADERS, shortHeaderChain,
262 BOOST_CHECK_EQUAL(testNode->GetBan(), 0);
263 BOOST_CHECK(!testNode->IsCheckpointVerified());
264
265 // Process a HEADERS message with a first header that does not follow
266 // our most recent checkpoint, check that the node is banned.
267 BOOST_CHECK(header.hashPrevBlock != recentCheckpoint);
268 testNode->setStartingHeight(recentCheckpointHeight + 1);
269 CDataStream headersOnWrongChain(SER_NETWORK, 0);
270 headersOnWrongChain.SetVersion(INIT_PROTO_VERSION);
271 WriteCompactSize(headersOnWrongChain, 1);
272 headersOnWrongChain << header;
273 testNode->TestProcessMessage(NetMsgType::HEADERS, headersOnWrongChain,
275 BOOST_CHECK(testNode->GetBan() > 0);
276 BOOST_CHECK(!testNode->IsCheckpointVerified());
277}
278
279BOOST_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
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
void SetVersion(int n)
Definition: streams.h:409
int GetVersion() const
Definition: streams.h:410
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:121
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:185
CDataStream vSend
Definition: bitcoin.h:42
PeerMessagingState ProcessMessage(std::string strCommand, CDataStream &recv)
Definition: bitcoin.cpp:41
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
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
static CDataStream CreateAddrMessage(std::vector< CAddress > sendAddrs, uint32_t nVersion=INIT_PROTO_VERSION)
std::ostream & operator<<(std::ostream &os, const PeerMessagingState &state)
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
PeerMessagingState
Definition: bitcoin.h:26
static const unsigned int ADDR_SOFT_CAP
Definition: bitcoin.h:24
static int GetRequireHeight()
Definition: db.h:28
constexpr std::underlying_type< E >::type to_integral(E e)
Definition: util.h:11
@ SER_NETWORK
Definition: serialize.h:154
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1203
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:109
std::vector< BlockHash > vHave
Definition: block.h:110
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
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:14