Bitcoin ABC  0.26.3
P2P Digital Currency
db_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2024 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 <seeder/db.h>
6 
7 #include <boost/test/unit_test.hpp>
8 
9 static CNetAddr ResolveIP(const std::string &ip) {
10  CNetAddr addr;
11  LookupHost(ip, addr, false);
12  return addr;
13 }
14 
15 static SeederAddrInfo BuildSeederAddrInfo(const CService &ip, bool good,
16  uint64_t services = NODE_NETWORK,
17  int clientVersion = REQUIRE_VERSION) {
18  SeederAddrInfo info{};
20 
21  uint8_t version{4};
22 
23  // tried must be 1 if we want the deserialization to not abort.
24  uint8_t tried{1};
25 
26  // The following values don't matter, some will be updated via
27  // SeederAddrInfo::Update()
28  int64_t lastTry{0};
29  int64_t ourLastTry{0};
30  int64_t ignoreTill{0};
31 
32  CAddrStat stat2H;
33  CAddrStat stat8H;
34  CAddrStat stat1D;
35  CAddrStat stat1W;
36  CAddrStat stat1M;
37  int total{0};
38  int success{0};
39  std::string clientSubVersion{};
40  int blocks{0};
41  int64_t ourLastSuccess{0};
42 
43  info_stream << version << ip << services << lastTry << tried << ourLastTry
44  << ignoreTill << stat2H << stat8H << stat1D << stat1W << stat1M
45  << total << success << clientVersion << clientSubVersion
46  << blocks << ourLastSuccess;
47 
48  info_stream >> info;
49  info.Update(good);
50  return info;
51 }
52 
53 BOOST_AUTO_TEST_SUITE(db_tests)
54 
55 BOOST_AUTO_TEST_CASE(seederaddrinfo_test) {
56  CService ip{ResolveIP("8.8.8.8"), uint16_t{1337}};
57 
58  // Any arbitrary port is OK
59  auto info = BuildSeederAddrInfo(ip, /*good=*/true);
60  BOOST_CHECK(info.IsReliable());
61 
62  // Check the effect of successive failure/success
63  info = BuildSeederAddrInfo(ip, /*good=*/false);
64  BOOST_CHECK(!info.IsReliable());
65  info.Update(/*good=*/true);
66  BOOST_CHECK(info.IsReliable());
67  // TODO: complete this test with more elaborate reliability scenarii
68 
69  // A node without the NODE_NETWORK service is considered unreliable
70  info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/0);
71  BOOST_CHECK(!info.IsReliable());
72 
73  // A node with clientVersion < REQUIRE_VERSION is considered unreliable
74  info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/NODE_NETWORK,
75  /*clientVersion=*/REQUIRE_VERSION - 1);
76  BOOST_CHECK(!info.IsReliable());
77 }
78 
79 BOOST_AUTO_TEST_SUITE_END()
Definition: db.h:40
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:177
Network address.
Definition: netaddress.h:121
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
bool LookupHost(const std::string &name, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:190
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ NODE_NETWORK
Definition: protocol.h:342
#define REQUIRE_VERSION
Definition: db.h:26
BOOST_AUTO_TEST_CASE(seederaddrinfo_test)
Definition: db_tests.cpp:55
static SeederAddrInfo BuildSeederAddrInfo(const CService &ip, bool good, uint64_t services=NODE_NETWORK, int clientVersion=REQUIRE_VERSION)
Definition: db_tests.cpp:15
static CNetAddr ResolveIP(const std::string &ip)
Definition: db_tests.cpp:9
@ SER_NETWORK
Definition: serialize.h:152
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11