Skip to content

Commit 69986de

Browse files
committed
Merge bitcoin/bitcoin#22839: log: improve addrman logging
b65a25a log: improve addrman logging (Martin Zumsande) Pull request description: The addrman helper functions `GetNewBucket()` and `GetTriedBucket()` 1) log into the wrong category (`BCLog::NET` instead of `BCLog::ADDRMAN`) 2) log too unspecifically - especially `GetTriedBucket()` gets called from many different places (e.g. `Check_()`, `Serialize()`), it seems sufficient to me logging these when moving an address from new to tried. Running a node with `-checkaddrman=1`and net logging currently results in a lot of repetitive log entries. This PR moves these log entries to `Add_()` and `Good_()` and also adds logging for `Select_()` (allowing statistics about New/Tried success probabilities), `GetAddr_()`, `ClearNew()` and `MakeTried()`. ACKs for top commit: jnewbery: ACK b65a25a vasild: ACK b65a25a Tree-SHA512: 90ab0f64eb44b7388a198efccb613577b74989fea73194bda7de8bfbd50bdb19127cb12f5ec645c7859afdb89290614a79e255f3af0a63a58d4f21aa8fe7b696
2 parents c8bae2b + b65a25a commit 69986de

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/addrman.cpp

Lines changed: 18 additions & 14 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
@@ -481,6 +475,7 @@ void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
481475
assert(infoDelete.nRefCount > 0);
482476
infoDelete.nRefCount--;
483477
vvNew[nUBucket][nUBucketPos] = -1;
478+
LogPrint(BCLog::ADDRMAN, "Removed %s from new[%i][%i]\n", infoDelete.ToString(), nUBucket, nUBucketPos);
484479
if (infoDelete.nRefCount == 0) {
485480
Delete(nIdDelete);
486481
}
@@ -532,6 +527,8 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
532527
infoOld.nRefCount = 1;
533528
vvNew[nUBucket][nUBucketPos] = nIdEvict;
534529
nNew++;
530+
LogPrint(BCLog::ADDRMAN, "Moved %s from tried[%i][%i] to new[%i][%i] to make space\n",
531+
infoOld.ToString(), nKBucket, nKBucketPos, nUBucket, nUBucketPos);
535532
}
536533
assert(vvTried[nKBucket][nKBucketPos] == -1);
537534

@@ -582,17 +579,20 @@ void AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
582579

583580
// Will moving this address into tried evict another entry?
584581
if (test_before_evict && (vvTried[tried_bucket][tried_bucket_pos] != -1)) {
585-
// Output the entry we'd be colliding with, for debugging purposes
586-
auto colliding_entry = mapInfo.find(vvTried[tried_bucket][tried_bucket_pos]);
587-
LogPrint(BCLog::ADDRMAN, "Collision inserting element into tried table (%s), moving %s to m_tried_collisions=%d\n", colliding_entry != mapInfo.end() ? colliding_entry->second.ToString() : "", addr.ToString(), m_tried_collisions.size());
588582
if (m_tried_collisions.size() < ADDRMAN_SET_TRIED_COLLISION_SIZE) {
589583
m_tried_collisions.insert(nId);
590584
}
585+
// Output the entry we'd be colliding with, for debugging purposes
586+
auto colliding_entry = mapInfo.find(vvTried[tried_bucket][tried_bucket_pos]);
587+
LogPrint(BCLog::ADDRMAN, "Collision with %s while attempting to move %s to tried table. Collisions=%d\n",
588+
colliding_entry != mapInfo.end() ? colliding_entry->second.ToString() : "",
589+
addr.ToString(),
590+
m_tried_collisions.size());
591591
} else {
592-
LogPrint(BCLog::ADDRMAN, "Moving %s to tried\n", addr.ToString());
593-
594592
// move nId to the tried tables
595593
MakeTried(info, nId);
594+
LogPrint(BCLog::ADDRMAN, "Moved %s mapped to AS%i to tried[%i][%i]\n",
595+
addr.ToString(), addr.GetMappedAS(m_asmap), tried_bucket, tried_bucket_pos);
596596
}
597597
}
598598

@@ -662,6 +662,8 @@ bool AddrManImpl::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTi
662662
ClearNew(nUBucket, nUBucketPos);
663663
pinfo->nRefCount++;
664664
vvNew[nUBucket][nUBucketPos] = nId;
665+
LogPrint(BCLog::ADDRMAN, "Added %s mapped to AS%i to new[%i][%i]\n",
666+
addr.ToString(), addr.GetMappedAS(m_asmap), nUBucket, nUBucketPos);
665667
} else {
666668
if (pinfo->nRefCount == 0) {
667669
Delete(nId);
@@ -720,6 +722,7 @@ std::pair<CAddress, int64_t> AddrManImpl::Select_(bool newOnly) const
720722
assert(it_found != mapInfo.end());
721723
const AddrInfo& info{it_found->second};
722724
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
725+
LogPrint(BCLog::ADDRMAN, "Selected %s from tried\n", info.ToString());
723726
return {info, info.nLastTry};
724727
}
725728
fChanceFactor *= 1.2;
@@ -739,6 +742,7 @@ std::pair<CAddress, int64_t> AddrManImpl::Select_(bool newOnly) const
739742
assert(it_found != mapInfo.end());
740743
const AddrInfo& info{it_found->second};
741744
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
745+
LogPrint(BCLog::ADDRMAN, "Selected %s from new\n", info.ToString());
742746
return {info, info.nLastTry};
743747
}
744748
fChanceFactor *= 1.2;
@@ -780,7 +784,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
780784

781785
addresses.push_back(ai);
782786
}
783-
787+
LogPrint(BCLog::ADDRMAN, "GetAddr returned %d random addresses\n", addresses.size());
784788
return addresses;
785789
}
786790

test/functional/p2p_invalid_messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def test_addrv2_unrecognized_network(self):
209209
self.test_addrv2('unrecognized network',
210210
[
211211
'received: addrv2 (25 bytes)',
212-
'IP 9.9.9.9 mapped',
212+
'9.9.9.9:8333 mapped',
213213
'Added 1 addresses',
214214
],
215215
bytes.fromhex(

0 commit comments

Comments
 (0)