Bitcoin ABC  0.26.3
P2P Digital Currency
message_writer_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 <hash.h>
6 #include <primitives/block.h>
7 #include <protocol.h>
8 #include <seeder/messagewriter.h>
9 #include <streams.h>
10 #include <version.h>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 #include <string>
15 #include <vector>
16 
17 BOOST_AUTO_TEST_SUITE(message_writer_tests)
18 
19 template <typename... Args>
20 static void CheckMessage(CDataStream &expectedMessage, std::string command,
21  Args &&...args) {
23  MessageWriter::WriteMessage(message, command, std::forward<Args>(args)...);
24  BOOST_CHECK_EQUAL(message.size(), expectedMessage.size());
25  for (size_t i = 0; i < message.size(); i++) {
26  BOOST_CHECK_EQUAL(message[i], expectedMessage[i]);
27  }
28 }
29 
30 BOOST_AUTO_TEST_CASE(simple_header_and_payload_message_writer_test) {
32  int64_t now = GetTime();
33  uint64_t nonce = 0;
34  uint64_t serviceFlags = uint64_t(ServiceFlags(NODE_NETWORK));
35  CService service;
36  CAddress addrTo(service, ServiceFlags(NODE_NETWORK));
37  CAddress addrFrom(service, ServiceFlags(NODE_NETWORK));
38  std::string user_agent = "/Bitcoin ABC:0.0.0(seeder)/";
39  int start_height = 1;
40 
41  CDataStream versionPayload(SER_NETWORK, PROTOCOL_VERSION);
42  versionPayload << PROTOCOL_VERSION << serviceFlags << now << addrTo
43  << addrFrom << nonce << user_agent << start_height;
44 
45  CMessageHeader versionhdr(Params().NetMagic(), NetMsgType::VERSION,
46  versionPayload.size());
47  uint256 hash = Hash(versionPayload);
48  memcpy(versionhdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
49 
50  CDataStream expectedVersion(SER_NETWORK, PROTOCOL_VERSION);
51  expectedVersion << versionhdr;
52  expectedVersion += versionPayload;
53 
55  serviceFlags, now, addrTo, addrFrom, nonce, user_agent,
56  start_height);
57 }
58 
59 BOOST_AUTO_TEST_CASE(header_empty_payload_message_writer_test) {
61  CMessageHeader verackHeader(Params().NetMagic(), NetMsgType::VERACK, 0);
62  CDataStream expectedVerack(SER_NETWORK, PROTOCOL_VERSION);
63  // This is an empty payload, but is still necessary for the checksum
64  std::vector<uint8_t> payload;
65  uint256 hash = Hash(payload);
66  memcpy(verackHeader.pchChecksum, hash.begin(),
68  expectedVerack << verackHeader;
69 
70  CheckMessage(expectedVerack, NetMsgType::VERACK);
71 }
72 
73 BOOST_AUTO_TEST_CASE(write_getheaders_message_test) {
76  BlockHash bhash(uint256S(
77  "0000000099f5509b5f36b1926bcf82b21d936ebeadee811030dfbbb7fae915d7"));
78  std::vector<BlockHash> vlocator(1, bhash);
79  CBlockLocator locatorhash(vlocator);
80  payload << locatorhash << uint256();
81  uint256 hash = Hash(payload);
82 
83  CMessageHeader msgHeader(Params().NetMagic(), NetMsgType::GETHEADERS,
84  payload.size());
85  memcpy(msgHeader.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
86 
88  expectedMsg << msgHeader;
89  expectedMsg += payload;
90 
91  CheckMessage(expectedMsg, NetMsgType::GETHEADERS, locatorhash, uint256());
92 }
93 
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
const CChainParams & Params()
Return the currently selected parameters.
A CService with information about it as peer.
Definition: protocol.h:445
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
size_type size() const
Definition: streams.h:280
Message header.
Definition: protocol.h:37
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:42
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:75
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
uint8_t * begin()
Definition: uint256.h:83
256-bit opaque blob.
Definition: uint256.h:127
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
static void CheckMessage(CDataStream &expectedMessage, std::string command, Args &&...args)
BOOST_AUTO_TEST_CASE(simple_header_and_payload_message_writer_test)
static void WriteMessage(CDataStream &stream, std::string command, Args &&...args)
Definition: messagewriter.h:15
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:17
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:26
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:18
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
ServiceFlags
nServices flags.
Definition: protocol.h:338
@ NODE_NETWORK
Definition: protocol.h:345
@ SER_NETWORK
Definition: serialize.h:166
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:100
T GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:71
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:141
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11