Bitcoin ABC  0.26.3
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 <protocol.h>
7 #include <seeder/bitcoin.h>
8 #include <seeder/db.h>
9 #include <seeder/test/util.h>
10 #include <serialize.h>
11 #include <streams.h>
12 #include <util/system.h>
13 #include <version.h>
14 
15 #include <boost/test/unit_test.hpp>
16 
17 #include <cstdint>
18 #include <memory>
19 #include <ostream>
20 #include <string>
21 #include <vector>
22 
23 std::ostream &operator<<(std::ostream &os, const PeerMessagingState &state) {
24  os << to_integral(state);
25  return os;
26 }
27 
28 namespace {
29 class CSeederNodeTest : public CSeederNode {
30 public:
31  CSeederNodeTest(const CService &service, std::vector<CAddress> *vAddrIn)
32  : CSeederNode(service, vAddrIn) {}
33 
34  void TestProcessMessage(const std::string &strCommand, CDataStream &message,
35  PeerMessagingState expectedState) {
36  PeerMessagingState ret = ProcessMessage(strCommand, message);
37  BOOST_CHECK_EQUAL(ret, expectedState);
38  }
39 
40  CDataStream getSendBuffer() { return vSend; }
41 };
42 } // namespace
43 
44 static const uint16_t SERVICE_PORT = 18444;
45 
49  CNetAddr ip;
50  ip.SetInternal("bitcoin.test");
51  CService service = {ip, SERVICE_PORT};
52  vAddr.emplace_back(service, ServiceFlags());
53  testNode = std::make_unique<CSeederNodeTest>(service, &vAddr);
54  }
55 
56  std::vector<CAddress> vAddr;
57  std::unique_ptr<CSeederNodeTest> testNode;
58 };
59 
60 BOOST_FIXTURE_TEST_SUITE(p2p_messaging_tests, SeederTestingSetup)
61 
62 static const int SEEDER_INIT_VERSION = 0;
63 
64 BOOST_AUTO_TEST_CASE(process_version_msg) {
66  uint64_t serviceflags = ServiceFlags(NODE_NETWORK);
67  CService addr_to = vAddr[0];
68  uint64_t addr_to_services = vAddr[0].nServices;
69  CService addr_from;
70  uint64_t nonce = 0;
71  std::string user_agent = "/Bitcoin ABC:0.0.0(seeder)/";
72 
73  // Don't include the time in CAddress serialization. See D14753.
74  versionMessage << INIT_PROTO_VERSION << serviceflags << GetTime()
75  << addr_to_services << addr_to << serviceflags << addr_from
76  << nonce << user_agent << GetRequireHeight();
77 
78  // Verify the version is set as the initial value
79  BOOST_CHECK_EQUAL(testNode->CSeederNode::GetClientVersion(),
81  testNode->TestProcessMessage(NetMsgType::VERSION, versionMessage,
83  // Verify the version has been updated
84  BOOST_CHECK_EQUAL(testNode->CSeederNode::GetClientVersion(),
85  versionMessage.GetVersion());
86 }
87 
88 BOOST_AUTO_TEST_CASE(process_verack_msg) {
89  CDataStream verackMessage(SER_NETWORK, 0);
90  verackMessage.SetVersion(INIT_PROTO_VERSION);
91  testNode->TestProcessMessage(NetMsgType::VERACK, verackMessage,
93 
94  // Seeder should respond with an ADDR message
95  const CMessageHeader::MessageMagic netMagic = Params().NetMagic();
96  CMessageHeader header(netMagic);
97  CDataStream sendBuffer = testNode->getSendBuffer();
98  sendBuffer >> header;
99  BOOST_CHECK(header.IsValidWithoutConfig(netMagic));
101 
102  // Next message should be GETHEADERS
103  sendBuffer >> header;
104  BOOST_CHECK(header.IsValidWithoutConfig(netMagic));
106 
107  CBlockLocator locator;
108  uint256 hashStop;
109  sendBuffer >> locator >> hashStop;
110  std::vector<BlockHash> expectedLocator = {
111  Params().Checkpoints().mapCheckpoints.rbegin()->second};
112  BOOST_CHECK(locator.vHave == expectedLocator);
113  BOOST_CHECK(hashStop == uint256());
114 }
115 
116 static CDataStream CreateAddrMessage(std::vector<CAddress> sendAddrs,
117  uint32_t nVersion = INIT_PROTO_VERSION) {
118  CDataStream payload(SER_NETWORK, 0);
119  payload.SetVersion(nVersion);
120  payload << sendAddrs;
121  return payload;
122 }
123 
124 BOOST_AUTO_TEST_CASE(process_addr_msg) {
125  // vAddrs starts with 1 entry.
126  std::vector<CAddress> sendAddrs(ADDR_SOFT_CAP - 1, vAddr[0]);
127 
128  // Happy path
129  // addrs are added normally to vAddr until ADDR_SOFT_CAP is reached.
130  // Add addrs up to the soft cap.
131  CDataStream addrMessage = CreateAddrMessage(sendAddrs);
132  BOOST_CHECK_EQUAL(1, vAddr.size());
133  testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
135  BOOST_CHECK_EQUAL(ADDR_SOFT_CAP, vAddr.size());
136 
137  // ADDR_SOFT_CAP is exceeded
138  sendAddrs.resize(1);
139  addrMessage = CreateAddrMessage(sendAddrs);
140  testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
142  BOOST_CHECK_EQUAL(ADDR_SOFT_CAP + 1, vAddr.size());
143 
144  // Test the seeder's behavior after ADDR_SOFT_CAP addrs
145  // Only one addr per ADDR message will be added, the rest are ignored
146  size_t expectedSize = vAddr.size() + 1;
147  for (size_t i = 1; i < 10; i++) {
148  sendAddrs.resize(i, sendAddrs[0]);
149  addrMessage = CreateAddrMessage(sendAddrs);
150  testNode->TestProcessMessage(NetMsgType::ADDR, addrMessage,
152  BOOST_CHECK_EQUAL(expectedSize, vAddr.size());
153  ++expectedSize;
154  }
155 }
156 
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.
static const std::string REGTEST
const CCheckpointData & Checkpoints() const
Definition: chainparams.h:128
const CMessageHeader::MessageMagic & NetMagic() const
Definition: chainparams.h:88
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:177
void SetVersion(int n)
Definition: streams.h:338
int GetVersion() const
Definition: streams.h:339
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:174
std::string GetCommand() const
Definition: protocol.cpp:115
std::array< uint8_t, MESSAGE_START_SIZE > MessageMagic
Definition: protocol.h:46
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:188
CDataStream vSend
Definition: bitcoin.h:42
PeerMessagingState ProcessMessage(std::string strCommand, CDataStream &recv)
Definition: bitcoin.cpp:38
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 * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:30
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:19
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
#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_AUTO_TEST_CASE(process_version_msg)
static const int SEEDER_INIT_VERSION
ServiceFlags
nServices flags.
Definition: protocol.h:335
@ NODE_NETWORK
Definition: protocol.h:342
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:152
BOOST_FIXTURE_TEST_SUITE(stakingrewards_tests, StakingRewardsActivationTestingSetup) BOOST_AUTO_TEST_CASE(isstakingrewardsactivated)
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
std::vector< BlockHash > vHave
Definition: block.h:106
MapCheckpoints mapCheckpoints
Definition: chainparams.h:28
std::vector< CAddress > vAddr
std::unique_ptr< CSeederNodeTest > testNode
int64_t GetTime()
Definition: time.cpp:109
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:14