Bitcoin ABC 0.32.11
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 return LookupHost(ip, false).value_or(CNetAddr{});
11}
12
13static SeederAddrInfo BuildSeederAddrInfo(const CService &ip, bool good,
14 uint64_t services = NODE_NETWORK,
15 int clientVersion = REQUIRE_VERSION,
16 bool checkpointVerified = true) {
17 SeederAddrInfo info{};
18 DataStream info_stream{};
19
20 uint8_t version{5};
21
22 // tried must be 1 if we want the deserialization to not abort.
23 uint8_t tried{1};
24
25 // The following values don't matter, some will be updated via
26 // SeederAddrInfo::Update()
27 int64_t lastTry{0};
28 int64_t ourLastTry{0};
29 int64_t ignoreTill{0};
30
31 CAddrStat stat2H;
32 CAddrStat stat8H;
33 CAddrStat stat1D;
34 CAddrStat stat1W;
35 CAddrStat stat1M;
36 int total{0};
37 int success{0};
38 std::string clientSubVersion{};
39 int blocks{0};
40 int64_t ourLastSuccess{0};
41
42 info_stream << version << WithParams(CAddress::V1_DISK, ip) << services
43 << lastTry << tried << ourLastTry << ignoreTill << stat2H
44 << stat8H << stat1D << stat1W << stat1M << total << success
45 << clientVersion << clientSubVersion << blocks << ourLastSuccess
46 << checkpointVerified;
47
48 info_stream >> WithParams(CAddress::V1_DISK, info);
49 info.Update(good);
50 return info;
51}
52
53BOOST_AUTO_TEST_SUITE(db_tests)
54
55BOOST_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 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::OK);
62
63 info = BuildSeederAddrInfo(CService{CNetAddr{}, uint16_t{1337}},
64 /*good=*/false);
65 BOOST_CHECK(!info.IsReliable());
66 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::NOT_ROUTABLE);
67
68 // Check the effect of successive failure/success
69 info = BuildSeederAddrInfo(ip, /*good=*/false);
70 BOOST_CHECK(!info.IsReliable());
71 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::BAD_UPTIME);
72 info.Update(/*good=*/true);
73 BOOST_CHECK(info.IsReliable());
74 BOOST_CHECK(info.GetReliabilityStatus() == ReliabilityStatus::OK);
75 // TODO: complete this test with more elaborate reliability scenarii
76
77 // A node without the NODE_NETWORK service is considered unreliable
78 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/0);
79 BOOST_CHECK(!info.IsReliable());
80 BOOST_CHECK(info.GetReliabilityStatus() ==
82
83 // A node with clientVersion < REQUIRE_VERSION is considered unreliable
84 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/NODE_NETWORK,
85 /*clientVersion=*/REQUIRE_VERSION - 1);
86 BOOST_CHECK(!info.IsReliable());
87 BOOST_CHECK(info.GetReliabilityStatus() ==
89
90 // The checkpoint must be verified
91 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/NODE_NETWORK,
92 /*clientVersion=*/REQUIRE_VERSION,
93 /*checkpointVerified=*/false);
94 BOOST_CHECK(!info.IsReliable());
95 BOOST_CHECK(info.GetReliabilityStatus() ==
97}
98
99BOOST_AUTO_TEST_SUITE_END()
Definition: db.h:44
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:572
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:118
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:188
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ NODE_NETWORK
Definition: protocol.h:343
#define REQUIRE_VERSION
Definition: db.h:25
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, bool checkpointVerified=true)
Definition: db_tests.cpp:13
static CNetAddr ResolveIP(const std::string &ip)
Definition: db_tests.cpp:9
static auto WithParams(const Params &params, T &&t)
Return a wrapper around t that (de)serializes it with specified parameter params.
Definition: serialize.h:1329