Bitcoin ABC  0.24.7
P2P Digital Currency
addrman.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2020 The Bitcoin Core 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 <addrman.h>
6 #include <bench/bench.h>
7 #include <random.h>
8 #include <util/time.h>
9 
10 #include <vector>
11 
12 /*
13  * A "source" is a source address from which we have received a bunch of other
14  * addresses.
15  */
16 
17 static constexpr size_t NUM_SOURCES = 64;
18 static constexpr size_t NUM_ADDRESSES_PER_SOURCE = 256;
19 
20 static std::vector<CAddress> g_sources;
21 static std::vector<std::vector<CAddress>> g_addresses;
22 
23 static void CreateAddresses() {
24  // already created
25  if (g_sources.size() > 0) {
26  return;
27  }
28 
29  FastRandomContext rng(uint256(std::vector<uint8_t>(32, 123)));
30 
31  auto randAddr = [&rng]() {
32  in6_addr addr;
33  memcpy(&addr, rng.randbytes(sizeof(addr)).data(), sizeof(addr));
34 
35  uint16_t port;
36  memcpy(&port, rng.randbytes(sizeof(port)).data(), sizeof(port));
37  if (port == 0) {
38  port = 1;
39  }
40 
41  CAddress ret(CService(addr, port), NODE_NETWORK);
42 
43  ret.nTime = GetAdjustedTime();
44 
45  return ret;
46  };
47 
48  for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) {
49  g_sources.emplace_back(randAddr());
50  g_addresses.emplace_back();
51  for (size_t addr_i = 0; addr_i < NUM_ADDRESSES_PER_SOURCE; ++addr_i) {
52  g_addresses[source_i].emplace_back(randAddr());
53  }
54  }
55 }
56 
57 static void AddAddressesToAddrMan(CAddrMan &addrman) {
58  for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) {
59  addrman.Add(g_addresses[source_i], g_sources[source_i]);
60  }
61 }
62 
63 static void FillAddrMan(CAddrMan &addrman) {
65 
66  AddAddressesToAddrMan(addrman);
67 }
68 
69 /* Benchmarks */
70 
71 static void AddrManAdd(benchmark::Bench &bench) {
73 
74  CAddrMan addrman;
75 
76  bench.run([&] {
77  AddAddressesToAddrMan(addrman);
78  addrman.Clear();
79  });
80 }
81 
82 static void AddrManSelect(benchmark::Bench &bench) {
83  CAddrMan addrman;
84 
85  FillAddrMan(addrman);
86 
87  bench.run([&] {
88  const auto &address = addrman.Select();
89  assert(address.GetPort() > 0);
90  });
91 }
92 
93 static void AddrManGetAddr(benchmark::Bench &bench) {
94  CAddrMan addrman;
95 
96  FillAddrMan(addrman);
97 
98  bench.run([&] {
99  const auto &addresses = addrman.GetAddr(2500, 23);
100  assert(addresses.size() > 0);
101  });
102 }
103 
104 static void AddrManGood(benchmark::Bench &bench) {
105  /*
106  * Create many CAddrMan objects - one to be modified at each loop iteration.
107  * This is necessary because the CAddrMan::Good() method modifies the
108  * object, affecting the timing of subsequent calls to the same method and
109  * we want to do the same amount of work in every loop iteration.
110  */
111 
112  bench.epochs(5).epochIterations(1);
113 
114  std::vector<CAddrMan> addrmans(bench.epochs() * bench.epochIterations());
115  for (auto &addrman : addrmans) {
116  FillAddrMan(addrman);
117  }
118 
119  auto markSomeAsGood = [](CAddrMan &addrman) {
120  for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) {
121  for (size_t addr_i = 0; addr_i < NUM_ADDRESSES_PER_SOURCE;
122  ++addr_i) {
123  if (addr_i % 32 == 0) {
124  addrman.Good(g_addresses[source_i][addr_i]);
125  }
126  }
127  }
128  };
129 
130  uint64_t i = 0;
131  bench.run([&] {
132  markSomeAsGood(addrmans.at(i));
133  ++i;
134  });
135 }
136 
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:514
NUM_SOURCES
static constexpr size_t NUM_SOURCES
Definition: addrman.cpp:17
ankerl::nanobench::Bench::epochIterations
Bench & epochIterations(uint64_t numIters) noexcept
Sets exactly the number of iterations for each epoch.
CAddrMan::Add
bool Add(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty=0)
Add a single address.
Definition: addrman.h:657
AddrManAdd
static void AddrManAdd(benchmark::Bench &bench)
Definition: addrman.cpp:71
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
CAddrMan::Select
CAddrInfo Select(bool newOnly=false)
Choose an address to connect to.
Definition: addrman.h:732
NODE_NETWORK
@ NODE_NETWORK
Definition: protocol.h:321
g_sources
static std::vector< CAddress > g_sources
Definition: addrman.cpp:20
CAddress::nTime
uint32_t nTime
Definition: protocol.h:462
NUM_ADDRESSES_PER_SOURCE
static constexpr size_t NUM_ADDRESSES_PER_SOURCE
Definition: addrman.cpp:18
random.h
FillAddrMan
static void FillAddrMan(CAddrMan &addrman)
Definition: addrman.cpp:63
AddAddressesToAddrMan
static void AddAddressesToAddrMan(CAddrMan &addrman)
Definition: addrman.cpp:57
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
time.h
uint256
256-bit opaque blob.
Definition: uint256.h:127
bench.h
FastRandomContext::randbytes
std::vector< uint8_t > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:689
g_addresses
static std::vector< std::vector< CAddress > > g_addresses
Definition: addrman.cpp:21
AddrManGetAddr
static void AddrManGetAddr(benchmark::Bench &bench)
Definition: addrman.cpp:93
CAddrMan
Stochastical (IP) address manager.
Definition: addrman.h:190
BENCHMARK
BENCHMARK(AddrManAdd)
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:34
CAddress
A CService with information about it as peer.
Definition: protocol.h:421
CAddrMan::Clear
void Clear()
Definition: addrman.h:608
ankerl::nanobench::Bench::epochs
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
addrman.h
CreateAddresses
static void CreateAddresses()
Definition: addrman.cpp:23
CAddrMan::GetAddr
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct)
Return a bunch of addresses, selected at random.
Definition: addrman.h:744
AddrManSelect
static void AddrManSelect(benchmark::Bench &bench)
Definition: addrman.cpp:82
AddrManGood
static void AddrManGood(benchmark::Bench &bench)
Definition: addrman.cpp:104
FastRandomContext
Fast randomness source.
Definition: random.h:129