Bitcoin ABC  0.24.7
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 
block.h
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:514
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(simple_header_and_payload_message_writer_test)
Definition: message_writer_tests.cpp:30
CheckMessage
static void CheckMessage(CDataStream &expectedMessage, std::string command, Args &&... args)
Definition: message_writer_tests.cpp:20
MessageWriter::WriteMessage
static void WriteMessage(CDataStream &stream, std::string command, Args &&... args)
Definition: messagewriter.h:15
streams.h
GetTime
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:27
ServiceFlags
ServiceFlags
nServices flags.
Definition: protocol.h:314
NODE_NETWORK
@ NODE_NETWORK
Definition: protocol.h:321
messagewriter.h
version.h
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:165
Hash
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
NetMsgType::GETHEADERS
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:27
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
Definition: chainparams.cpp:530
uint256
256-bit opaque blob.
Definition: uint256.h:127
uint256S
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:141
base_blob::begin
uint8_t * begin()
Definition: uint256.h:83
CDataStream::size
size_type size() const
Definition: streams.h:280
BlockHash
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
CBaseChainParams::MAIN
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
Definition: chainparamsbase.h:21
CMessageHeader::CHECKSUM_SIZE
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:42
CAddress
A CService with information about it as peer.
Definition: protocol.h:421
CMessageHeader
Message header.
Definition: protocol.h:37
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:508
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
NetMsgType::VERACK
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
CBlockLocator
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
NetMsgType::VERSION
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:18
CMessageHeader::pchChecksum
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:75
BOOST_AUTO_TEST_SUITE_END
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11