Bitcoin ABC  0.28.12
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 ||
154  (bantime < time(nullptr) &&
155  addr.nTime > NodeSeconds{std::chrono::seconds{bantime}})) {
156  banned.erase(ipp);
157  } else {
158  return;
159  }
160  }
161  if (ipToId.count(ipp)) {
162  SeederAddrInfo &ai = idToInfo[ipToId[ipp]];
163  if (addr.nTime > NodeSeconds{std::chrono::seconds{ai.lastTry}} ||
164  ai.services != addr.nServices) {
165  ai.lastTry = TicksSinceEpoch<std::chrono::seconds>(addr.nTime);
166  ai.services |= addr.nServices;
167  // tfm::format(std::cout, "%s: updated\n",
168  // ToString(addr));
169  }
170  if (force) {
171  ai.ignoreTill = 0;
172  }
173  return;
174  }
175 
176  SeederAddrInfo ai;
177  ai.ip = ipp;
178  ai.services = addr.nServices;
179  ai.lastTry = TicksSinceEpoch<std::chrono::seconds>(addr.nTime);
180  ai.ourLastTry = 0;
181  ai.total = 0;
182  ai.success = 0;
183  int id = nId++;
184  idToInfo[id] = ai;
185  ipToId[ipp] = id;
186  // tfm::format(std::cout, "%s: added\n", ToString(ipp),
187  // ipToId[ipp]);
188  unkId.insert(id);
189  nDirty++;
190 }
191 
192 void CAddrDb::GetIPs_(std::set<CNetAddr> &ips, uint64_t requestedFlags,
193  uint32_t max, const bool *nets) {
194  if (goodId.size() == 0) {
195  int id = -1;
196  if (ourId.size() == 0) {
197  if (unkId.size() == 0) {
198  return;
199  }
200  id = *unkId.begin();
201  } else {
202  id = *ourId.begin();
203  }
204 
205  if (id >= 0 &&
206  (idToInfo[id].services & requestedFlags) == requestedFlags) {
207  ips.insert(idToInfo[id].ip);
208  }
209  return;
210  }
211 
212  std::vector<int> goodIdFiltered;
213  for (auto &id : goodId) {
214  if ((idToInfo[id].services & requestedFlags) == requestedFlags) {
215  goodIdFiltered.push_back(id);
216  }
217  }
218 
219  if (!goodIdFiltered.size()) {
220  return;
221  }
222 
223  if (max > goodIdFiltered.size() / 2) {
224  max = goodIdFiltered.size() / 2;
225  }
226 
227  if (max < 1) {
228  max = 1;
229  }
230 
231  std::set<int> ids;
232  while (ids.size() < max) {
233  ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]);
234  }
235 
236  for (auto &id : ids) {
237  CService &ip = idToInfo[id].ip;
238  if (nets[ip.GetNetwork()]) {
239  ips.insert(ip);
240  }
241  }
242 }
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:192
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:442
NodeSeconds nTime
Always included in serialization, except in the network format on INIT_PROTO_VERSION.
Definition: protocol.h:544
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:545
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
int64_t GetTime()
Definition: time.cpp:109
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25