mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 10:38:42 +01:00
Merge bitcoin/bitcoin#27745: addrman: select addresses by network follow-up
cd8ef5b3e6
test: ensure addrman test is finite (Amiti Uttarwar)b9f1e86f12
addrman: change asserts to Assumes (Amiti Uttarwar)768770771f
doc: update `Select` function description (Amiti Uttarwar)2b6bd12eea
refactor: de-duplicate lookups (Amiti Uttarwar) Pull request description: this PR addresses outstanding review comments from #27214 ACKs for top commit: achow101: ACKcd8ef5b3e6
mzumsande: Code Review ACKcd8ef5b3e6
brunoerg: crACKcd8ef5b3e6
Tree-SHA512: 669f67904263e3f51c39b175eabf5fa1b1e7b6841e889656afec33d0bd93fb446de9403f0a91b186ddeaf29498c8938484a0547b1188256c4e7c90db6f30bb55
This commit is contained in:
commit
6744d840df
@ -757,16 +757,14 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, std::option
|
|||||||
|
|
||||||
// Iterate over the positions of that bucket, starting at the initial one,
|
// Iterate over the positions of that bucket, starting at the initial one,
|
||||||
// and looping around.
|
// and looping around.
|
||||||
int i;
|
int i, position, node_id;
|
||||||
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
|
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
|
||||||
int position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
|
position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
|
||||||
int node_id = GetEntry(search_tried, bucket, position);
|
node_id = GetEntry(search_tried, bucket, position);
|
||||||
if (node_id != -1) {
|
if (node_id != -1) {
|
||||||
if (network.has_value()) {
|
if (network.has_value()) {
|
||||||
const auto it{mapInfo.find(node_id)};
|
const auto it{mapInfo.find(node_id)};
|
||||||
assert(it != mapInfo.end());
|
if (Assume(it != mapInfo.end()) && it->second.GetNetwork() == *network) break;
|
||||||
const auto info{it->second};
|
|
||||||
if (info.GetNetwork() == *network) break;
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -777,9 +775,7 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, std::option
|
|||||||
if (i == ADDRMAN_BUCKET_SIZE) continue;
|
if (i == ADDRMAN_BUCKET_SIZE) continue;
|
||||||
|
|
||||||
// Find the entry to return.
|
// Find the entry to return.
|
||||||
int position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
|
const auto it_found{mapInfo.find(node_id)};
|
||||||
int nId = GetEntry(search_tried, bucket, position);
|
|
||||||
const auto it_found{mapInfo.find(nId)};
|
|
||||||
assert(it_found != mapInfo.end());
|
assert(it_found != mapInfo.end());
|
||||||
const AddrInfo& info{it_found->second};
|
const AddrInfo& info{it_found->second};
|
||||||
|
|
||||||
@ -798,15 +794,17 @@ int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
|
|||||||
{
|
{
|
||||||
AssertLockHeld(cs);
|
AssertLockHeld(cs);
|
||||||
|
|
||||||
assert(position < ADDRMAN_BUCKET_SIZE);
|
|
||||||
|
|
||||||
if (use_tried) {
|
if (use_tried) {
|
||||||
assert(bucket < ADDRMAN_TRIED_BUCKET_COUNT);
|
if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_TRIED_BUCKET_COUNT)) {
|
||||||
return vvTried[bucket][position];
|
return vvTried[bucket][position];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
assert(bucket < ADDRMAN_NEW_BUCKET_COUNT);
|
if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_NEW_BUCKET_COUNT)) {
|
||||||
return vvNew[bucket][position];
|
return vvNew[bucket][position];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
|
std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
|
||||||
|
@ -148,8 +148,10 @@ public:
|
|||||||
*
|
*
|
||||||
* @param[in] new_only Whether to only select addresses from the new table. Passing `true` returns
|
* @param[in] new_only Whether to only select addresses from the new table. Passing `true` returns
|
||||||
* an address from the new table or an empty pair. Passing `false` will return an
|
* an address from the new table or an empty pair. Passing `false` will return an
|
||||||
* address from either the new or tried table (it does not guarantee a tried entry).
|
* empty pair or an address from either the new or tried table (it does not
|
||||||
* @param[in] network Select only addresses of this network (nullopt = all)
|
* guarantee a tried entry).
|
||||||
|
* @param[in] network Select only addresses of this network (nullopt = all). Passing a network may
|
||||||
|
* slow down the search.
|
||||||
* @return CAddress The record for the selected peer.
|
* @return CAddress The record for the selected peer.
|
||||||
* seconds The last time we attempted to connect to that peer.
|
* seconds The last time we attempted to connect to that peer.
|
||||||
*/
|
*/
|
||||||
|
@ -255,7 +255,7 @@ private:
|
|||||||
|
|
||||||
/** Helper to generalize looking up an addrman entry from either table.
|
/** Helper to generalize looking up an addrman entry from either table.
|
||||||
*
|
*
|
||||||
* @return int The nid of the entry or -1 if the addrman position is empty.
|
* @return int The nid of the entry. If the addrman position is empty or not found, returns -1.
|
||||||
* */
|
* */
|
||||||
int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
||||||
|
|
||||||
|
@ -239,8 +239,9 @@ BOOST_AUTO_TEST_CASE(addrman_select_by_network)
|
|||||||
// ensure that both new and tried table are selected from
|
// ensure that both new and tried table are selected from
|
||||||
bool new_selected{false};
|
bool new_selected{false};
|
||||||
bool tried_selected{false};
|
bool tried_selected{false};
|
||||||
|
int counter = 256;
|
||||||
|
|
||||||
while (!new_selected || !tried_selected) {
|
while (--counter > 0 && (!new_selected || !tried_selected)) {
|
||||||
const CAddress selected{addrman->Select(/*new_only=*/false, NET_I2P).first};
|
const CAddress selected{addrman->Select(/*new_only=*/false, NET_I2P).first};
|
||||||
BOOST_REQUIRE(selected == i2p_addr || selected == i2p_addr2);
|
BOOST_REQUIRE(selected == i2p_addr || selected == i2p_addr2);
|
||||||
if (selected == i2p_addr) {
|
if (selected == i2p_addr) {
|
||||||
@ -249,6 +250,9 @@ BOOST_AUTO_TEST_CASE(addrman_select_by_network)
|
|||||||
new_selected = true;
|
new_selected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_CHECK(new_selected);
|
||||||
|
BOOST_CHECK(tried_selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(addrman_select_special)
|
BOOST_AUTO_TEST_CASE(addrman_select_special)
|
||||||
|
Loading…
Reference in New Issue
Block a user