mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-04 20:51:27 +02:00
[addrman] Add Add_() inner function, fix Add() return semantics
Previously, Add() would return true if the function created a new AddressInfo object, even if that object could not be successfully entered into the new table and was deleted. That would happen if the new table position was already taken and the existing entry could not be removed. Instead, return true if the new AddressInfo object is successfully entered into the new table. This fixes a bug in the "Added %i addresses" log, which would not always accurately log how many addresses had been added. p2p_addrv2_relay.py and p2p_addr_relay.py need to be updated since they were incorrectly asserting on the buggy log (assuming that addresses are added to addrman, when there could in fact be new table position collisions that prevent some of those address records from being added).
This commit is contained in:
@ -599,7 +599,6 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
|
|||||||
if (!addr.IsRoutable())
|
if (!addr.IsRoutable())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool fNew = false;
|
|
||||||
int nId;
|
int nId;
|
||||||
AddrInfo* pinfo = Find(addr, &nId);
|
AddrInfo* pinfo = Find(addr, &nId);
|
||||||
|
|
||||||
@ -640,13 +639,12 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
|
|||||||
pinfo = Create(addr, source, &nId);
|
pinfo = Create(addr, source, &nId);
|
||||||
pinfo->nTime = std::max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty);
|
pinfo->nTime = std::max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty);
|
||||||
nNew++;
|
nNew++;
|
||||||
fNew = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int nUBucket = pinfo->GetNewBucket(nKey, source, m_asmap);
|
int nUBucket = pinfo->GetNewBucket(nKey, source, m_asmap);
|
||||||
int nUBucketPos = pinfo->GetBucketPosition(nKey, true, nUBucket);
|
int nUBucketPos = pinfo->GetBucketPosition(nKey, true, nUBucket);
|
||||||
if (vvNew[nUBucket][nUBucketPos] != nId) {
|
|
||||||
bool fInsert = vvNew[nUBucket][nUBucketPos] == -1;
|
bool fInsert = vvNew[nUBucket][nUBucketPos] == -1;
|
||||||
|
if (vvNew[nUBucket][nUBucketPos] != nId) {
|
||||||
if (!fInsert) {
|
if (!fInsert) {
|
||||||
AddrInfo& infoExisting = mapInfo[vvNew[nUBucket][nUBucketPos]];
|
AddrInfo& infoExisting = mapInfo[vvNew[nUBucket][nUBucketPos]];
|
||||||
if (infoExisting.IsTerrible() || (infoExisting.nRefCount > 1 && pinfo->nRefCount == 0)) {
|
if (infoExisting.IsTerrible() || (infoExisting.nRefCount > 1 && pinfo->nRefCount == 0)) {
|
||||||
@ -666,7 +664,19 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fNew;
|
return fInsert;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AddrManImpl::Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty)
|
||||||
|
{
|
||||||
|
int added{0};
|
||||||
|
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) {
|
||||||
|
added += AddSingle(*it, source, nTimePenalty) ? 1 : 0;
|
||||||
|
}
|
||||||
|
if (added > 0) {
|
||||||
|
LogPrint(BCLog::ADDRMAN, "Added %i addresses (of %i) from %s: %i tried, %i new\n", added, vAddr.size(), source.ToString(), nTried, nNew);
|
||||||
|
}
|
||||||
|
return added > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
|
void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
|
||||||
@ -1031,15 +1041,10 @@ size_t AddrManImpl::size() const
|
|||||||
bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty)
|
bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty)
|
||||||
{
|
{
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
int nAdd = 0;
|
|
||||||
Check();
|
Check();
|
||||||
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
|
auto ret = Add_(vAddr, source, nTimePenalty);
|
||||||
nAdd += AddSingle(*it, source, nTimePenalty) ? 1 : 0;
|
|
||||||
Check();
|
Check();
|
||||||
if (nAdd) {
|
return ret;
|
||||||
LogPrint(BCLog::ADDRMAN, "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
|
|
||||||
}
|
|
||||||
return nAdd > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddrManImpl::Good(const CService& addr, int64_t nTime)
|
void AddrManImpl::Good(const CService& addr, int64_t nTime)
|
||||||
|
@ -76,7 +76,8 @@ public:
|
|||||||
* @param[in] source The address of the node that sent us these addr records.
|
* @param[in] source The address of the node that sent us these addr records.
|
||||||
* @param[in] nTimePenalty A "time penalty" to apply to the address record's nTime. If a peer
|
* @param[in] nTimePenalty A "time penalty" to apply to the address record's nTime. If a peer
|
||||||
* sends us an address record with nTime=n, then we'll add it to our
|
* sends us an address record with nTime=n, then we'll add it to our
|
||||||
* addrman with nTime=(n - nTimePenalty). */
|
* addrman with nTime=(n - nTimePenalty).
|
||||||
|
* @return true if at least one address is successfully added. */
|
||||||
bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
|
bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
|
||||||
|
|
||||||
//! Mark an entry as accessible, possibly moving it from "new" to "tried".
|
//! Mark an entry as accessible, possibly moving it from "new" to "tried".
|
||||||
|
@ -248,6 +248,8 @@ private:
|
|||||||
* @see AddrMan::Add() for parameters. */
|
* @see AddrMan::Add() for parameters. */
|
||||||
bool AddSingle(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
bool AddSingle(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
||||||
|
|
||||||
|
bool Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
||||||
|
|
||||||
void Attempt_(const CService& addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
void Attempt_(const CService& addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
||||||
|
|
||||||
std::pair<CAddress, int64_t> Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
std::pair<CAddress, int64_t> Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
||||||
|
@ -347,7 +347,7 @@ BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
|
|||||||
//Test: tried table collision!
|
//Test: tried table collision!
|
||||||
CService addr1 = ResolveService("250.1.1." + ToString(++num_addrs));
|
CService addr1 = ResolveService("250.1.1." + ToString(++num_addrs));
|
||||||
uint32_t collisions{1};
|
uint32_t collisions{1};
|
||||||
BOOST_CHECK(addrman.Add({CAddress(addr1, NODE_NONE)}, source));
|
BOOST_CHECK(!addrman.Add({CAddress(addr1, NODE_NONE)}, source));
|
||||||
BOOST_CHECK_EQUAL(addrman.size(), num_addrs - collisions);
|
BOOST_CHECK_EQUAL(addrman.size(), num_addrs - collisions);
|
||||||
|
|
||||||
CService addr2 = ResolveService("250.1.1." + ToString(++num_addrs));
|
CService addr2 = ResolveService("250.1.1." + ToString(++num_addrs));
|
||||||
|
@ -152,7 +152,6 @@ class AddrTest(BitcoinTestFramework):
|
|||||||
msg = self.setup_addr_msg(num_ipv4_addrs)
|
msg = self.setup_addr_msg(num_ipv4_addrs)
|
||||||
with self.nodes[0].assert_debug_log(
|
with self.nodes[0].assert_debug_log(
|
||||||
[
|
[
|
||||||
'Added {} addresses from 127.0.0.1: 0 tried'.format(num_ipv4_addrs),
|
|
||||||
'received: addr (301 bytes) peer=1',
|
'received: addr (301 bytes) peer=1',
|
||||||
]
|
]
|
||||||
):
|
):
|
||||||
|
@ -72,9 +72,6 @@ class AddrTest(BitcoinTestFramework):
|
|||||||
addr_receiver = self.nodes[0].add_p2p_connection(AddrReceiver())
|
addr_receiver = self.nodes[0].add_p2p_connection(AddrReceiver())
|
||||||
msg.addrs = ADDRS
|
msg.addrs = ADDRS
|
||||||
with self.nodes[0].assert_debug_log([
|
with self.nodes[0].assert_debug_log([
|
||||||
# The I2P address is not added to node's own addrman because it has no
|
|
||||||
# I2P reachability (thus 10 - 1 = 9).
|
|
||||||
'Added 9 addresses from 127.0.0.1: 0 tried',
|
|
||||||
'received: addrv2 (159 bytes) peer=0',
|
'received: addrv2 (159 bytes) peer=0',
|
||||||
'sending addrv2 (159 bytes) peer=1',
|
'sending addrv2 (159 bytes) peer=1',
|
||||||
]):
|
]):
|
||||||
|
Reference in New Issue
Block a user