MCPcopy Create free account
hub / github.com/bitcoinxt/bitcoinxt / Add_

Method Add_

src/addrman.cpp:248–314  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

246}
247
248bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty)
249{
250 if (!addr.IsRoutable())
251 return false;
252
253 bool fNew = false;
254 int nId;
255 CAddrInfo* pinfo = Find(addr, &nId);
256
257 if (pinfo) {
258 // periodically update nTime
259 bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
260 int64_t nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
261 if (addr.nTime && (!pinfo->nTime || pinfo->nTime < addr.nTime - nUpdateInterval - nTimePenalty))
262 pinfo->nTime = std::max((int64_t)0, addr.nTime - nTimePenalty);
263
264 // add services
265 pinfo->nServices |= addr.nServices;
266
267 // do not update if no new information is present
268 if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime))
269 return false;
270
271 // do not update if the entry was already in the "tried" table
272 if (pinfo->fInTried)
273 return false;
274
275 // do not update if the max reference count is reached
276 if (pinfo->nRefCount == ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
277 return false;
278
279 // stochastic test: previous nRefCount == N: 2^N times harder to increase it
280 int nFactor = 1;
281 for (int n = 0; n < pinfo->nRefCount; n++)
282 nFactor *= 2;
283 if (nFactor > 1 && (RandomInt(nFactor) != 0))
284 return false;
285 } else {
286 pinfo = Create(addr, source, &nId);
287 pinfo->nTime = std::max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty);
288 nNew++;
289 fNew = true;
290 }
291
292 int nUBucket = pinfo->GetNewBucket(nKey, source);
293 int nUBucketPos = pinfo->GetBucketPosition(nKey, true, nUBucket);
294 if (vvNew[nUBucket][nUBucketPos] != nId) {
295 bool fInsert = vvNew[nUBucket][nUBucketPos] == -1;
296 if (!fInsert) {
297 CAddrInfo& infoExisting = mapInfo[vvNew[nUBucket][nUBucketPos]];
298 if (infoExisting.IsTerrible() || (infoExisting.nRefCount > 1 && pinfo->nRefCount == 0)) {
299 // Overwrite the existing new table entry.
300 fInsert = true;
301 }
302 }
303 if (fInsert) {
304 ClearNew(nUBucket, nUBucketPos);
305 pinfo->nRefCount++;

Callers

nothing calls this directly

Calls 5

GetAdjustedTimeFunction · 0.85
IsRoutableMethod · 0.80
GetBucketPositionMethod · 0.80
IsTerribleMethod · 0.80
GetNewBucketMethod · 0.45

Tested by

no test coverage detected