Bitcoin ABC 0.34.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
14BuildSeederAddrInfo(const CService &ip, bool good,
15 uint64_t services = NODE_NETWORK,
16 int clientVersion = MIN_PEER_PROTO_VERSION,
17 bool checkpointVerified = true) {
18 SeederAddrInfo info{};
19 DataStream info_stream{};
20
21 uint8_t version{5};
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 << WithParams(CAddress::V1_DISK, ip) << services
44 << lastTry << tried << ourLastTry << ignoreTill << stat2H
45 << stat8H << stat1D << stat1W << stat1M << total << success
46 << clientVersion << clientSubVersion << blocks << ourLastSuccess
47 << checkpointVerified;
48
49 info_stream >> WithParams(CAddress::V1_DISK, 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 < MIN_PEER_PROTO_VERSION is considered
85 // unreliable
86 info = BuildSeederAddrInfo(ip, /*good=*/true, /*services=*/NODE_NETWORK,
87 /*clientVersion=*/MIN_PEER_PROTO_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=*/MIN_PEER_PROTO_VERSION,
95 /*checkpointVerified=*/false);
96 BOOST_CHECK(!info.IsReliable());
97 BOOST_CHECK(info.GetReliabilityStatus() ==
99}
100
101BOOST_AUTO_TEST_SUITE_END()
Definition db.h:43
static constexpr SerParams V1_DISK
Definition protocol.h:498
Network address.
Definition netaddress.h:114
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:573
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:211
#define BOOST_CHECK(expr)
Definition object.cpp:17
@ NODE_NETWORK
Definition protocol.h:342
static const int MIN_PEER_PROTO_VERSION
disconnect from peers older than this proto version
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=MIN_PEER_PROTO_VERSION, bool checkpointVerified=true)
Definition db_tests.cpp:14
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:1331