Bitcoin ABC  0.22.13
P2P Digital Currency
db.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2020 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 <util/time.h>
8 
9 #include <cstdlib>
10 
11 void SeederAddrInfo::Update(bool good) {
12  int64_t now = GetTime();
13  if (ourLastTry == 0) {
14  ourLastTry = now - MIN_RETRY;
15  }
16  int age = now - ourLastTry;
17  lastTry = now;
18  ourLastTry = now;
19  total++;
20  if (good) {
21  success++;
22  ourLastSuccess = now;
23  }
24  stat2H.Update(good, age, 3600 * 2);
25  stat8H.Update(good, age, 3600 * 8);
26  stat1D.Update(good, age, 3600 * 24);
27  stat1W.Update(good, age, 3600 * 24 * 7);
28  stat1M.Update(good, age, 3600 * 24 * 30);
29  int64_t ign = GetIgnoreTime();
30  if (ign && (ignoreTill == 0 || ignoreTill < ign + now)) {
31  ignoreTill = ign + now;
32  }
33  // tfm::format(std::cout, "%s: got %s result: success=%i/%i;
34  // 2H:%.2f%%-%.2f%%(%.2f) 8H:%.2f%%-%.2f%%(%.2f) 1D:%.2f%%-%.2f%%(%.2f)
35  // 1W:%.2f%%-%.2f%%(%.2f) \n", ToString(ip), good ? "good" : "bad",
36  // success, total, 100.0 * stat2H.reliability, 100.0 * (stat2H.reliability
37  // + 1.0 - stat2H.weight), stat2H.count, 100.0 * stat8H.reliability, 100.0
38  // * (stat8H.reliability + 1.0 - stat8H.weight), stat8H.count, 100.0 *
39  // stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - stat1D.weight),
40  // stat1D.count, 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability
41  // + 1.0 - stat1W.weight), stat1W.count);
42 }
43 
44 bool CAddrDb::Get_(CServiceResult &ip, int &wait) {
45  int64_t now = GetTime();
46  size_t tot = unkId.size() + ourId.size();
47  if (tot == 0) {
48  wait = 5;
49  return false;
50  }
51 
52  do {
53  size_t rnd = rand() % tot;
54  int ret;
55  if (rnd < unkId.size()) {
56  std::set<int>::iterator it = unkId.end();
57  it--;
58  ret = *it;
59  unkId.erase(it);
60  } else {
61  ret = ourId.front();
62  if (GetTime() - idToInfo[ret].ourLastTry < MIN_RETRY) {
63  return false;
64  }
65  ourId.pop_front();
66  }
67 
68  if (idToInfo[ret].ignoreTill && idToInfo[ret].ignoreTill < now) {
69  ourId.push_back(ret);
70  idToInfo[ret].ourLastTry = now;
71  } else {
72  ip.service = idToInfo[ret].ip;
73  ip.ourLastSuccess = idToInfo[ret].ourLastSuccess;
74  break;
75  }
76  } while (1);
77 
78  nDirty++;
79  return true;
80 }
81 
83  if (ipToId.count(ip)) {
84  return ipToId[ip];
85  }
86  return -1;
87 }
88 
89 void CAddrDb::Good_(const CService &addr, int clientV, std::string clientSV,
90  int blocks) {
91  int id = Lookup_(addr);
92  if (id == -1) {
93  return;
94  }
95  unkId.erase(id);
96  banned.erase(addr);
97  SeederAddrInfo &info = idToInfo[id];
98  info.clientVersion = clientV;
99  info.clientSubVersion = clientSV;
100  info.blocks = blocks;
101  info.Update(true);
102  if (info.IsReliable() && goodId.count(id) == 0) {
103  goodId.insert(id);
104  // tfm::format(std::cout, "%s: good; %i good nodes now\n",
105  // ToString(addr), (int)goodId.size());
106  }
107  nDirty++;
108  ourId.push_back(id);
109 }
110 
111 void CAddrDb::Bad_(const CService &addr, int ban) {
112  int id = Lookup_(addr);
113  if (id == -1) {
114  return;
115  }
116  unkId.erase(id);
117  SeederAddrInfo &info = idToInfo[id];
118  info.Update(false);
119  uint32_t now = GetTime();
120  int ter = info.GetBanTime();
121  if (ter) {
122  // tfm::format(std::cout, "%s: terrible\n", ToString(addr));
123  if (ban < ter) {
124  ban = ter;
125  }
126  }
127  if (ban > 0) {
128  // tfm::format(std::cout, "%s: ban for %i seconds\n",
129  // ToString(addr), ban);
130  banned[info.ip] = ban + now;
131  ipToId.erase(info.ip);
132  goodId.erase(id);
133  idToInfo.erase(id);
134  } else {
135  if ( goodId.count(id) == 1) {
136  goodId.erase(id);
137  // tfm::format(std::cout, "%s: not good; %i good nodes left\n",
138  // ToString(addr), (int)goodId.size());
139  }
140  ourId.push_back(id);
141  }
142  nDirty++;
143 }
144 
145 void CAddrDb::Add_(const CAddress &addr, bool force) {
146  if (!force && !addr.IsRoutable()) {
147  return;
148  }
149  CService ipp(addr);
150  if (banned.count(ipp)) {
151  time_t bantime = banned[ipp];
152  if (force || (bantime < time(nullptr) && addr.nTime > bantime)) {
153  banned.erase(ipp);
154  } else {
155  return;
156  }
157  }
158  if (ipToId.count(ipp)) {
159  SeederAddrInfo &ai = idToInfo[ipToId[ipp]];
160  if (addr.nTime > ai.lastTry || ai.services != addr.nServices) {
161  ai.lastTry = addr.nTime;
162  ai.services |= addr.nServices;
163  // tfm::format(std::cout, "%s: updated\n",
164  // ToString(addr));
165  }
166  if (force) {
167  ai.ignoreTill = 0;
168  }
169  return;
170  }
171 
172  SeederAddrInfo ai;
173  ai.ip = ipp;
174  ai.services = addr.nServices;
175  ai.lastTry = addr.nTime;
176  ai.ourLastTry = 0;
177  ai.total = 0;
178  ai.success = 0;
179  int id = nId++;
180  idToInfo[id] = ai;
181  ipToId[ipp] = id;
182  // tfm::format(std::cout, "%s: added\n", ToString(ipp),
183  // ipToId[ipp]);
184  unkId.insert(id);
185  nDirty++;
186 }
187 
188 void CAddrDb::GetIPs_(std::set<CNetAddr> &ips, uint64_t requestedFlags,
189  uint32_t max, const bool *nets) {
190  if (goodId.size() == 0) {
191  int id = -1;
192  if (ourId.size() == 0) {
193  if (unkId.size() == 0) {
194  return;
195  }
196  id = *unkId.begin();
197  } else {
198  id = *ourId.begin();
199  }
200 
201  if (id >= 0 &&
202  (idToInfo[id].services & requestedFlags) == requestedFlags) {
203  ips.insert(idToInfo[id].ip);
204  }
205  return;
206  }
207 
208  std::vector<int> goodIdFiltered;
209  for (auto &id : goodId) {
210  if ((idToInfo[id].services & requestedFlags) == requestedFlags) {
211  goodIdFiltered.push_back(id);
212  }
213  }
214 
215  if (!goodIdFiltered.size()) {
216  return;
217  }
218 
219  if (max > goodIdFiltered.size() / 2) {
220  max = goodIdFiltered.size() / 2;
221  }
222 
223  if (max < 1) {
224  max = 1;
225  }
226 
227  std::set<int> ids;
228  while (ids.size() < max) {
229  ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]);
230  }
231 
232  for (auto &id : ids) {
233  CService &ip = idToInfo[id].ip;
234  if (nets[ip.GetNetwork()]) {
235  ips.insert(ip);
236  }
237  }
238 }
CAddrStat stat1M
Definition: db.h:92
void GetIPs_(std::set< CNetAddr > &ips, uint64_t requestedFlags, uint32_t max, const bool *nets)
Definition: db.cpp:188
uint8_t ip[16]
Definition: netaddress.h:33
int64_t lastTry
Definition: db.h:84
int64_t ourLastTry
Definition: db.h:85
CAddrStat stat1D
Definition: db.h:90
int clientVersion
Definition: db.h:93
void Update(bool good)
Definition: db.cpp:11
int64_t ignoreTill
Definition: db.h:87
int64_t GetIgnoreTime() const
Definition: db.h:183
void Bad_(const CService &ip, int ban)
Definition: db.cpp:111
CService ip
Definition: db.h:82
enum Network GetNetwork() const
Definition: netaddress.cpp:312
CService service
Definition: db.h:262
bool Get_(CServiceResult &ip, int &wait)
Definition: db.cpp:44
int total
Definition: db.h:95
int blocks
Definition: db.h:94
CAddrStat stat2H
Definition: db.h:88
uint64_t services
Definition: db.h:83
bool IsReliable() const
Definition: db.h:121
void Good_(const CService &ip, int clientV, std::string clientSV, int blocks)
Definition: db.cpp:89
void Update(bool good, int64_t age, double tau)
Definition: db.h:49
int Lookup_(const CService &ip)
Definition: db.cpp:82
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:179
A CService with information about it as peer.
Definition: protocol.h:427
std::string clientSubVersion
Definition: db.h:97
void Add_(const CAddress &addr, bool force)
Definition: db.cpp:145
bool IsRoutable() const
Definition: netaddress.cpp:296
int64_t ourLastSuccess
Definition: db.h:268
CAddrStat stat1W
Definition: db.h:91
unsigned int nTime
Definition: protocol.h:466
ServiceFlags nServices
Definition: protocol.h:463
int success
Definition: db.h:96
#define MIN_RETRY
Definition: db.h:24
int64_t GetBanTime() const
Definition: db.h:161
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:27
CAddrStat stat8H
Definition: db.h:89
int64_t ourLastSuccess
Definition: db.h:86