Skip to content

Commit 19b0145

Browse files
committed
merge bitcoin#22839: improve addrman logging
1 parent 3910c68 commit 19b0145

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/addrman.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,15 @@ int AddrInfo::GetTriedBucket(const uint256& nKey, const std::vector<bool>& asmap
4545
{
4646
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
4747
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup(asmap) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
48-
int tried_bucket = hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
49-
uint32_t mapped_as = GetMappedAS(asmap);
50-
LogPrint(BCLog::NET, "IP %s mapped to AS%i belongs to tried bucket %i\n", ToStringIP(), mapped_as, tried_bucket);
51-
return tried_bucket;
48+
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
5249
}
5350

5451
int AddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const std::vector<bool>& asmap) const
5552
{
5653
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup(asmap);
5754
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup(asmap) << vchSourceGroupKey).GetCheapHash();
5855
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
59-
int new_bucket = hash2 % ADDRMAN_NEW_BUCKET_COUNT;
60-
uint32_t mapped_as = GetMappedAS(asmap);
61-
LogPrint(BCLog::NET, "IP %s mapped to AS%i belongs to new bucket %i\n", ToStringIP(), mapped_as, new_bucket);
62-
return new_bucket;
56+
return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
6357
}
6458

6559
int AddrInfo::GetBucketPosition(const uint256& nKey, bool fNew, int nBucket) const
@@ -496,6 +490,7 @@ void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
496490
assert(infoDelete.nRefCount > 0);
497491
infoDelete.nRefCount--;
498492
vvNew[nUBucket][nUBucketPos] = -1;
493+
LogPrint(BCLog::ADDRMAN, "Removed %s from new[%i][%i]\n", infoDelete.ToString(), nUBucket, nUBucketPos);
499494
if (infoDelete.nRefCount == 0) {
500495
Delete(nIdDelete);
501496
}
@@ -547,6 +542,8 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
547542
infoOld.nRefCount = 1;
548543
vvNew[nUBucket][nUBucketPos] = nIdEvict;
549544
nNew++;
545+
LogPrint(BCLog::ADDRMAN, "Moved %s from tried[%i][%i] to new[%i][%i] to make space\n",
546+
infoOld.ToString(), nKBucket, nKBucketPos, nUBucket, nUBucketPos);
550547
}
551548
assert(vvTried[nKBucket][nKBucketPos] == -1);
552549

@@ -597,21 +594,24 @@ void AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
597594

598595
// Will moving this address into tried evict another entry?
599596
if (test_before_evict && (vvTried[tried_bucket][tried_bucket_pos] != -1)) {
597+
if (m_tried_collisions.size() < ADDRMAN_SET_TRIED_COLLISION_SIZE) {
598+
m_tried_collisions.insert(nId);
599+
}
600600
// Output the entry we'd be colliding with, for debugging purposes
601601
auto colliding_entry = mapInfo.find(vvTried[tried_bucket][tried_bucket_pos]);
602602
if (fLogIPs) {
603-
LogPrint(BCLog::ADDRMAN, "Collision inserting element into tried table (%s), moving %s to m_tried_collisions=%d\n",
603+
LogPrint(BCLog::ADDRMAN, "Collision with %s while attempting to move %s to tried table. Collisions=%d\n",
604604
colliding_entry != mapInfo.end() ? colliding_entry->second.ToString() : "",
605-
addr.ToString(), m_tried_collisions.size());
606-
}
607-
if (m_tried_collisions.size() < ADDRMAN_SET_TRIED_COLLISION_SIZE) {
608-
m_tried_collisions.insert(nId);
605+
addr.ToString(),
606+
m_tried_collisions.size());
609607
}
610608
} else {
611-
if (fLogIPs) LogPrint(BCLog::ADDRMAN, "Moving %s to tried\n", addr.ToString());
612-
613609
// move nId to the tried tables
614610
MakeTried(info, nId);
611+
if (fLogIPs) {
612+
LogPrint(BCLog::ADDRMAN, "Moved %s mapped to AS%i to tried[%i][%i]\n",
613+
addr.ToString(), addr.GetMappedAS(m_asmap), tried_bucket, tried_bucket_pos);
614+
}
615615
}
616616
}
617617

@@ -683,6 +683,8 @@ bool AddrManImpl::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTi
683683
ClearNew(nUBucket, nUBucketPos);
684684
pinfo->nRefCount++;
685685
vvNew[nUBucket][nUBucketPos] = nId;
686+
LogPrint(BCLog::ADDRMAN, "Added %s mapped to AS%i to new[%i][%i]\n",
687+
addr.ToString(), addr.GetMappedAS(m_asmap), nUBucket, nUBucketPos);
686688
} else {
687689
if (pinfo->nRefCount == 0) {
688690
Delete(nId);
@@ -741,6 +743,7 @@ std::pair<CAddress, int64_t> AddrManImpl::Select_(bool newOnly) const
741743
assert(it_found != mapInfo.end());
742744
const AddrInfo& info{it_found->second};
743745
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
746+
LogPrint(BCLog::ADDRMAN, "Selected %s from tried\n", info.ToString());
744747
return {info, info.nLastTry};
745748
}
746749
fChanceFactor *= 1.2;
@@ -760,6 +763,7 @@ std::pair<CAddress, int64_t> AddrManImpl::Select_(bool newOnly) const
760763
assert(it_found != mapInfo.end());
761764
const AddrInfo& info{it_found->second};
762765
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
766+
LogPrint(BCLog::ADDRMAN, "Selected %s from new\n", info.ToString());
763767
return {info, info.nLastTry};
764768
}
765769
fChanceFactor *= 1.2;
@@ -801,7 +805,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
801805

802806
addresses.push_back(ai);
803807
}
804-
808+
LogPrint(BCLog::ADDRMAN, "GetAddr returned %d random addresses\n", addresses.size());
805809
return addresses;
806810
}
807811

0 commit comments

Comments
 (0)