Bitcoin ABC  0.26.3
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 
82 int CAddrDb::Lookup_(const CService &ip) {
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, uint64_t services) {
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.services = services;
102  info.Update(true);
103  if (info.IsReliable() && goodId.count(id) == 0) {
104  goodId.insert(id);
105  // tfm::format(std::cout, "%s: good; %i good nodes now\n",
106  // ToString(addr), (int)goodId.size());
107  }
108  nDirty++;
109  ourId.push_back(id);
110 }
111 
112 void CAddrDb::Bad_(const CService &addr, int ban) {
113  int id = Lookup_(addr);
114  if (id == -1) {
115  return;
116  }
117  unkId.erase(id);
118  SeederAddrInfo &info = idToInfo[id];
119  info.Update(false);
120  uint32_t now = GetTime();
121  int ter = info.GetBanTime();
122  if (ter) {
123  // tfm::format(std::cout, "%s: terrible\n", ToString(addr));
124  if (ban < ter) {
125  ban = ter;
126  }
127  }
128  if (ban > 0) {
129  // tfm::format(std::cout, "%s: ban for %i seconds\n",
130  // ToString(addr), ban);
131  banned[info.ip] = ban + now;
132  ipToId.erase(info.ip);
133  goodId.erase(id);
134  idToInfo.erase(id);
135  } else {
136  if ( goodId.count(id) == 1) {
137  goodId.erase(id);
138  // tfm::format(std::cout, "%s: not good; %i good nodes left\n",
139  // ToString(addr), (int)goodId.size());
140  }
141  ourId.push_back(id);
142  }
143  nDirty++;
144 }
145 
146 void CAddrDb::Add_(const CAddress &addr, bool force) {
147  if (!force && !addr.IsRoutable()) {
148  return;
149  }
150  CService ipp(addr);
151  if (banned.count(ipp)) {
152  time_t bantime = banned[ipp];
153  if (force || (bantime < time(nullptr) && addr.nTime > bantime)) {
154  banned.erase(ipp);
155  } else {
156  return;
157  }
158  }
159  if (ipToId.count(ipp)) {
160  SeederAddrInfo &ai = idToInfo[ipToId[ipp]];
161  if (addr.nTime > ai.lastTry || ai.services != addr.nServices) {
162  ai.lastTry = addr.nTime;
163  ai.services |= addr.nServices;
164  // tfm::format(std::cout, "%s: updated\n",
165  // ToString(addr));
166  }
167  if (force) {
168  ai.ignoreTill = 0;
169  }
170  return;
171  }
172 
173  SeederAddrInfo ai;
174  ai.ip = ipp;
175  ai.services = addr.nServices;
176  ai.lastTry = addr.nTime;
177  ai.ourLastTry = 0;
178  ai.total = 0;
179  ai.success = 0;
180  int id = nId++;
181  idToInfo[id] = ai;
182  ipToId[ipp] = id;
183  // tfm::format(std::cout, "%s: added\n", ToString(ipp),
184  // ipToId[ipp]);
185  unkId.insert(id);
186  nDirty++;
187 }
188 
189 void CAddrDb::GetIPs_(std::set<CNetAddr> &ips, uint64_t requestedFlags,
190  uint32_t max, const bool *nets) {
191  if (goodId.size() == 0) {
192  int id = -1;
193  if (ourId.size() == 0) {
194  if (unkId.size() == 0) {
195  return;
196  }
197  id = *unkId.begin();
198  } else {
199  id = *ourId.begin();
200  }
201 
202  if (id >= 0 &&
203  (idToInfo[id].services & requestedFlags) == requestedFlags) {
204  ips.insert(idToInfo[id].ip);
205  }
206  return;
207  }
208 
209  std::vector<int> goodIdFiltered;
210  for (auto &id : goodId) {
211  if ((idToInfo[id].services & requestedFlags) == requestedFlags) {
212  goodIdFiltered.push_back(id);
213  }
214  }
215 
216  if (!goodIdFiltered.size()) {
217  return;
218  }
219 
220  if (max > goodIdFiltered.size() / 2) {
221  max = goodIdFiltered.size() / 2;
222  }
223 
224  if (max < 1) {
225  max = 1;
226  }
227 
228  std::set<int> ids;
229  while (ids.size() < max) {
230  ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]);
231  }
232 
233  for (auto &id : ids) {
234  CService &ip = idToInfo[id].ip;
235  if (nets[ip.GetNetwork()]) {
236  ips.insert(ip);
237  }
238  }
239 }
std::set< int > unkId
Definition: db.h:276
void Bad_(const CService &ip, int ban)
Definition: db.cpp:112
void Good_(const CService &ip, int clientV, std::string clientSV, int blocks, uint64_t services)
Definition: db.cpp:89
void GetIPs_(std::set< CNetAddr > &ips, uint64_t requestedFlags, uint32_t max, const bool *nets)
Definition: db.cpp:189
int nId
Definition: db.h:268
std::set< int > goodId
Definition: db.h:278
int nDirty
Definition: db.h:279
std::map< CService, int > ipToId
Definition: db.h:272
std::map< CService, int64_t > banned
Definition: db.h:301
std::deque< int > ourId
Definition: db.h:274
std::map< int, SeederAddrInfo > idToInfo
Definition: db.h:270
void Add_(const CAddress &addr, bool force)
Definition: db.cpp:146
bool Get_(CServiceResult &ip, int &wait)
Definition: db.cpp:44
int Lookup_(const CService &ip)
Definition: db.cpp:82
void Update(bool good, int64_t age, double tau)
Definition: db.h:49
A CService with information about it as peer.
Definition: protocol.h:445
ServiceFlags nServices
Definition: protocol.h:488
uint32_t nTime
Definition: protocol.h:486
bool IsRoutable() const
Definition: netaddress.cpp:514
enum Network GetNetwork() const
Definition: netaddress.cpp:551
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:544
int success
Definition: db.h:91
CAddrStat stat8H
Definition: db.h:84
CAddrStat stat1W
Definition: db.h:86
int64_t lastTry
Definition: db.h:79
int64_t GetIgnoreTime() const
Definition: db.h:178
CAddrStat stat1D
Definition: db.h:85
CAddrStat stat2H
Definition: db.h:83
int64_t ignoreTill
Definition: db.h:82
int64_t GetBanTime() const
Definition: db.h:156
std::string clientSubVersion
Definition: db.h:92
int total
Definition: db.h:90
bool IsReliable() const
Definition: db.h:116
int64_t ourLastTry
Definition: db.h:80
CAddrStat stat1M
Definition: db.h:87
CService ip
Definition: db.h:77
void Update(bool good)
Definition: db.cpp:11
int blocks
Definition: db.h:89
uint64_t services
Definition: db.h:78
int64_t ourLastSuccess
Definition: db.h:81
int clientVersion
Definition: db.h:88
#define MIN_RETRY
Definition: db.h:24
CService service
Definition: db.h:245
int64_t ourLastSuccess
Definition: db.h:252
T GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:71