Bitcoin ABC  0.24.11
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 }
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:514
CAddrDb::nId
int nId
Definition: db.h:268
SeederAddrInfo::clientVersion
int clientVersion
Definition: db.h:88
SeederAddrInfo::stat1D
CAddrStat stat1D
Definition: db.h:85
SeederAddrInfo::stat8H
CAddrStat stat8H
Definition: db.h:84
CAddrDb::GetIPs_
void GetIPs_(std::set< CNetAddr > &ips, uint64_t requestedFlags, uint32_t max, const bool *nets)
Definition: db.cpp:189
SeederAddrInfo::Update
void Update(bool good)
Definition: db.cpp:11
CAddrDb::Bad_
void Bad_(const CService &ip, int ban)
Definition: db.cpp:112
SeederAddrInfo::ignoreTill
int64_t ignoreTill
Definition: db.h:82
SeederAddrInfo::ourLastSuccess
int64_t ourLastSuccess
Definition: db.h:81
SeederAddrInfo::ourLastTry
int64_t ourLastTry
Definition: db.h:80
SeederAddrInfo::stat1M
CAddrStat stat1M
Definition: db.h:87
SeederAddrInfo
Definition: db.h:75
SeederAddrInfo::total
int total
Definition: db.h:90
CAddrDb::Good_
void Good_(const CService &ip, int clientV, std::string clientSV, int blocks, uint64_t services)
Definition: db.cpp:89
GetTime
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:27
MIN_RETRY
#define MIN_RETRY
Definition: db.h:24
SeederAddrInfo::ip
CService ip
Definition: db.h:77
CAddress::nServices
ServiceFlags nServices
Definition: protocol.h:464
CAddress::nTime
uint32_t nTime
Definition: protocol.h:462
SeederAddrInfo::services
uint64_t services
Definition: db.h:78
db.h
SeederAddrInfo::GetIgnoreTime
int64_t GetIgnoreTime() const
Definition: db.h:178
CNetAddr::GetNetwork
enum Network GetNetwork() const
Definition: netaddress.cpp:513
SeederAddrInfo::blocks
int blocks
Definition: db.h:89
CAddrDb::goodId
std::set< int > goodId
Definition: db.h:278
CAddrDb::ipToId
std::map< CService, int > ipToId
Definition: db.h:272
CNetAddr::IsRoutable
bool IsRoutable() const
Definition: netaddress.cpp:476
SeederAddrInfo::IsReliable
bool IsReliable() const
Definition: db.h:116
time.h
SeederAddrInfo::stat1W
CAddrStat stat1W
Definition: db.h:86
CAddrDb::idToInfo
std::map< int, SeederAddrInfo > idToInfo
Definition: db.h:270
SeederAddrInfo::stat2H
CAddrStat stat2H
Definition: db.h:83
CAddrStat::Update
void Update(bool good, int64_t age, double tau)
Definition: db.h:49
SeederAddrInfo::clientSubVersion
std::string clientSubVersion
Definition: db.h:92
CServiceResult
Definition: db.h:244
CAddress
A CService with information about it as peer.
Definition: protocol.h:421
CAddrDb::ourId
std::deque< int > ourId
Definition: db.h:274
CAddrDb::Add_
void Add_(const CAddress &addr, bool force)
Definition: db.cpp:146
CAddrDb::banned
std::map< CService, int64_t > banned
Definition: db.h:301
CServiceResult::ourLastSuccess
int64_t ourLastSuccess
Definition: db.h:252
SeederAddrInfo::success
int success
Definition: db.h:91
CAddrDb::Lookup_
int Lookup_(const CService &ip)
Definition: db.cpp:82
CAddrDb::unkId
std::set< int > unkId
Definition: db.h:276
SeederAddrInfo::GetBanTime
int64_t GetBanTime() const
Definition: db.h:156
SeederAddrInfo::lastTry
int64_t lastTry
Definition: db.h:79
CServiceResult::service
CService service
Definition: db.h:245
CAddrDb::nDirty
int nDirty
Definition: db.h:279
CAddrDb::Get_
bool Get_(CServiceResult &ip, int &wait)
Definition: db.cpp:44