Bitcoin ABC 0.32.5
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 << WithParams(CAddress::V1_DISK, ip) << services
45 << lastTry << tried << ourLastTry << ignoreTill << stat2H
46 << stat8H << stat1D << stat1W << stat1M << total << success
47 << clientVersion << clientSubVersion << blocks << ourLastSuccess
48 << checkpointVerified;
49
50 info_stream >> WithParams(CAddress::V1_DISK, info);
51 info.Update(good);
52 return info;
53}
54
55BOOST_AUTO_TEST_SUITE(db_tests)
56
57BOOST_AUTO_TEST_CASE(seederaddrinfo_test) {
58 CService ip{ResolveIP("8.8.8.8"), uint16_t{1337}};
59
60 // Any arbitrary port is OK
61 auto info = BuildSeederAddrInfo(ip, /*good=*/true);
62 BOOST_CHECK(info.IsReliable());
63 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::OK);
64
65 info = BuildSeederAddrInfo(CService{CNetAddr{}, uint16_t{1337}},
66 /*good=*/false);
67 BOOST_CHECK(!info.IsReliable());
68 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::NOT_ROUTABLE);
69
70 // Check the effect of successive failure/success
71 info = BuildSeederAddrInfo(ip, /*good=*/false);
72 BOOST_CHECK(!info.IsReliable());
73 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::BAD_UPTIME);
74 info.Update(/*good=*/true);
75 BOOST_CHECK(info.IsReliable());
76 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::OK);
77 // TODO: complete this test with more elaborate reliability scenarii
78
79 // A node without the NODE_NETWORK service is considered unreliable
80 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/0);
81 BOOST_CHECK(!info.IsReliable());
82 BOOST_CHECK(info.GetReliabilityStatus() ==
84
85 // A node with clientVersion < REQUIRE_VERSION is considered unreliable
86 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/NODE_NETWORK,
87 /*clientVersion=*/REQUIRE_VERSION - 1);
88 BOOST_CHECK(!info.IsReliable());
89 BOOST_CHECK(info.GetReliabilityStatus() ==
91
92 // The checkpoint must be verified
93 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/NODE_NETWORK,
94 /*clientVersion=*/REQUIRE_VERSION,
95 /*checkpointVerified=*/false);
96 BOOST_CHECK(!info.IsReliable());
97 BOOST_CHECK(info.GetReliabilityStatus() ==
99}
100
101BOOST_AUTO_TEST_SUITE_END()
Definition: db.h:45
static constexpr SerParams V1_DISK
Definition: protocol.h:500
Network address.
Definition: netaddress.h:114
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:573
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:189
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ NODE_NETWORK
Definition: protocol.h:343
#define REQUIRE_VERSION
Definition: db.h:26
BOOST_AUTO_TEST_CASE(seederaddrinfo_test)
Definition: db_tests.cpp:57
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:155
static auto WithParams(const Params &params, T &&t)
Return a wrapper around t that (de)serializes it with specified parameter params.
Definition: serialize.h:1353
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11