Bitcoin ABC 0.30.7
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
9static CNetAddr ResolveIP(const std::string &ip) {
10 CNetAddr addr;
11 LookupHost(ip, addr, false);
12 return addr;
13}
14
15static SeederAddrInfo BuildSeederAddrInfo(const CService &ip, bool good,
16 uint64_t services = NODE_NETWORK,
17 int clientVersion = REQUIRE_VERSION,
18 bool checkpointVerified = true) {
19 SeederAddrInfo info{};
21
22 uint8_t version{5};
23
24 // tried must be 1 if we want the deserialization to not abort.
25 uint8_t tried{1};
26
27 // The following values don't matter, some will be updated via
28 // SeederAddrInfo::Update()
29 int64_t lastTry{0};
30 int64_t ourLastTry{0};
31 int64_t ignoreTill{0};
32
33 CAddrStat stat2H;
34 CAddrStat stat8H;
35 CAddrStat stat1D;
36 CAddrStat stat1W;
37 CAddrStat stat1M;
38 int total{0};
39 int success{0};
40 std::string clientSubVersion{};
41 int blocks{0};
42 int64_t ourLastSuccess{0};
43
44 info_stream << version << ip << services << lastTry << tried << ourLastTry
45 << ignoreTill << stat2H << stat8H << stat1D << stat1W << stat1M
46 << total << success << clientVersion << clientSubVersion
47 << blocks << ourLastSuccess << checkpointVerified;
48
49 info_stream >> info;
50 info.Update(good);
51 return info;
52}
53
54BOOST_AUTO_TEST_SUITE(db_tests)
55
56BOOST_AUTO_TEST_CASE(seederaddrinfo_test) {
57 CService ip{ResolveIP("8.8.8.8"), uint16_t{1337}};
58
59 // Any arbitrary port is OK
60 auto info = BuildSeederAddrInfo(ip, /*good=*/true);
61 BOOST_CHECK(info.IsReliable());
62 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::OK);
63
64 info = BuildSeederAddrInfo(CService{CNetAddr{}, uint16_t{1337}},
65 /*good=*/false);
66 BOOST_CHECK(!info.IsReliable());
67 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::NOT_ROUTABLE);
68
69 // Check the effect of successive failure/success
70 info = BuildSeederAddrInfo(ip, /*good=*/false);
71 BOOST_CHECK(!info.IsReliable());
72 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::BAD_UPTIME);
73 info.Update(/*good=*/true);
74 BOOST_CHECK(info.IsReliable());
75 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::OK);
76 // TODO: complete this test with more elaborate reliability scenarii
77
78 // A node without the NODE_NETWORK service is considered unreliable
79 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/0);
80 BOOST_CHECK(!info.IsReliable());
81 BOOST_CHECK(info.GetReliabilityStatus() ==
83
84 // A node with clientVersion < REQUIRE_VERSION is considered unreliable
85 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/NODE_NETWORK,
86 /*clientVersion=*/REQUIRE_VERSION - 1);
87 BOOST_CHECK(!info.IsReliable());
88 BOOST_CHECK(info.GetReliabilityStatus() ==
90
91 // The checkpoint must be verified
92 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/NODE_NETWORK,
93 /*clientVersion=*/REQUIRE_VERSION,
94 /*checkpointVerified=*/false);
95 BOOST_CHECK(!info.IsReliable());
96 BOOST_CHECK(info.GetReliabilityStatus() ==
98}
99
100BOOST_AUTO_TEST_SUITE_END()
Definition: db.h:45
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:191
#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:56
static SeederAddrInfo BuildSeederAddrInfo(const CService &ip, bool good, uint64_t services=NODE_NETWORK, int clientVersion=REQUIRE_VERSION, bool checkpointVerified=true)
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