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(uint8_t(message[i]), uint8_t(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 << versionPayload;
52 
54  serviceFlags, now, addrTo, addrFrom, nonce, user_agent,
55  start_height);
56 }
57 
58 BOOST_AUTO_TEST_CASE(header_empty_payload_message_writer_test) {
60  CMessageHeader verackHeader(Params().NetMagic(), NetMsgType::VERACK, 0);
61  CDataStream expectedVerack(SER_NETWORK, PROTOCOL_VERSION);
62  // This is an empty payload, but is still necessary for the checksum
63  std::vector<uint8_t> payload;
64  uint256 hash = Hash(payload);
65  memcpy(verackHeader.pchChecksum, hash.begin(),
67  expectedVerack << verackHeader;
68 
69  CheckMessage(expectedVerack, NetMsgType::VERACK);
70 }
71 
72 BOOST_AUTO_TEST_CASE(write_getheaders_message_test) {
75  BlockHash bhash(uint256S(
76  "0000000099f5509b5f36b1926bcf82b21d936ebeadee811030dfbbb7fae915d7"));
77  std::vector<BlockHash> vlocator(1, bhash);
78  CBlockLocator locatorhash(vlocator);
79  payload << locatorhash << uint256();
80  uint256 hash = Hash(payload);
81 
82  CMessageHeader msgHeader(Params().NetMagic(), NetMsgType::GETHEADERS,
83  payload.size());
84  memcpy(msgHeader.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
85 
87  expectedMsg << msgHeader << payload;
88 
89  CheckMessage(expectedMsg, NetMsgType::GETHEADERS, locatorhash, uint256());
90 }
91 
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
Definition: chainparams.cpp:51
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:19
A CService with information about it as peer.
Definition: protocol.h:442
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:177
size_type size() const
Definition: streams.h:223
Message header.
Definition: protocol.h:34
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:39
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:72
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
uint8_t * begin()
Definition: uint256.h:85
256-bit opaque blob.
Definition: uint256.h:129
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
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: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
#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:335
@ NODE_NETWORK
Definition: protocol.h:342
@ SER_NETWORK
Definition: serialize.h:152
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:105
int64_t GetTime()
Definition: time.cpp:109
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:143
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11